secondary cache feature in vm.
[minix.git] / lib / libc / ip / getifaddrs.c
blob1872ae30b3564355da42d467b9dc31aabf48549d
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <fcntl.h>
5 #include <ifaddrs.h>
6 #include <sys/ioctl.h>
7 #include <sys/socket.h>
8 #include <sys/types.h>
9 #include <netinet/in.h>
11 #include <net/gen/in.h>
12 #include <net/gen/ip_io.h>
13 #include <net/gen/tcp.h>
14 #include <net/gen/udp.h>
16 int
17 getifaddrs(struct ifaddrs **ifap)
19 static int fd = -1;
20 nwio_ipconf_t ipconf;
21 int flags;
22 static struct ifaddrs ifa;
23 static struct sockaddr_in addr, netmask;
25 memset(&ifa, 0, sizeof(ifa));
26 memset(&addr, 0, sizeof(addr));
27 memset(&netmask, 0, sizeof(netmask));
28 ifa.ifa_next = NULL;
29 ifa.ifa_name = "ip";
30 addr.sin_family = netmask.sin_family = AF_INET;
31 ifa.ifa_addr = (struct sockaddr *) &addr;
32 ifa.ifa_netmask = (struct sockaddr *) &netmask;
33 addr.sin_addr.s_addr = 0;
34 netmask.sin_addr.s_addr = 0;
36 if(fd < 0) {
37 char *ipd;
38 if(!(ipd=getenv("IP_DEVICE")))
39 ipd="/dev/ip";
40 if((fd = open(ipd, O_RDWR)) < 0)
41 return -1;
44 /* Code taken from commands/simple/ifconfig.c. */
46 if((flags = fcntl(fd, F_GETFL)) < 0 ||
47 fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0 ||
48 ioctl(fd, NWIOGIPCONF, &ipconf))
49 return 0; /* Report interface as down. */
51 addr.sin_addr.s_addr = ipconf.nwic_ipaddr;
52 netmask.sin_addr.s_addr = ipconf.nwic_netmask;
53 if(addr.sin_addr.s_addr) ifa.ifa_flags = IFF_UP;
55 /* Just report on this interface. */
57 *ifap = &ifa;
59 return 0;
62 void
63 freeifaddrs(struct ifaddrs *ifp)
65 /* getifaddrs points to static data, so no need to free. */