For /dev/mem, map in memory to be copied to memory's own address space
[minix3.git] / lib / ip / getifaddrs.c
blob15dd13b54c462c75942ac5bb55cbb40210929882
1 #include <errno.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <fcntl.h>
6 #include <ifaddrs.h>
7 #include <sys/ioctl.h>
8 #include <sys/socket.h>
9 #include <sys/types.h>
10 #include <netinet/in.h>
12 #include <net/gen/in.h>
13 #include <net/gen/ip_io.h>
14 #include <net/gen/tcp.h>
15 #include <net/gen/tcp_io.h>
16 #include <net/gen/udp.h>
17 #include <net/gen/udp_io.h>
19 int
20 getifaddrs(struct ifaddrs **ifap)
22 static int fd = -1;
23 nwio_ipconf_t ipconf;
24 int flags, err, r;
25 static struct ifaddrs ifa;
26 static struct sockaddr_in addr, netmask;
28 memset(&ifa, 0, sizeof(ifa));
29 memset(&addr, 0, sizeof(addr));
30 memset(&netmask, 0, sizeof(netmask));
31 ifa.ifa_next = NULL;
32 ifa.ifa_name = "ip";
33 addr.sin_family = netmask.sin_family = AF_INET;
34 ifa.ifa_addr = (struct sockaddr *) &addr;
35 ifa.ifa_netmask = (struct sockaddr *) &netmask;
36 addr.sin_addr.s_addr = 0;
37 netmask.sin_addr.s_addr = 0;
39 if(fd < 0) {
40 char *ipd;
41 if(!(ipd=getenv("IP_DEVICE")))
42 ipd="/dev/ip";
43 if((fd = open(ipd, O_RDWR)) < 0)
44 return -1;
47 /* Code taken from commands/simple/ifconfig.c. */
49 if((flags = fcntl(fd, F_GETFL)) < 0 ||
50 fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0 ||
51 ioctl(fd, NWIOGIPCONF, &ipconf))
52 return 0; /* Report interface as down. */
54 addr.sin_addr.s_addr = ipconf.nwic_ipaddr;
55 netmask.sin_addr.s_addr = ipconf.nwic_netmask;
56 if(addr.sin_addr.s_addr) ifa.ifa_flags = IFF_UP;
58 /* Just report on this interface. */
60 *ifap = &ifa;
62 return 0;
65 void
66 freeifaddrs(struct ifaddrs *ifp)
68 /* getifaddrs points to static data, so no need to free. */