]> Zhao Yanbai Git Server - minix.git/commitdiff
Added getifaddrs()
authorBen Gras <ben@minix3.org>
Mon, 3 Apr 2006 15:03:43 +0000 (15:03 +0000)
committerBen Gras <ben@minix3.org>
Mon, 3 Apr 2006 15:03:43 +0000 (15:03 +0000)
lib/ip/Makefile.in
lib/ip/getifaddrs.c [new file with mode: 0644]

index 0e811915354ec9fa2aee2946fff62587ff436275..784b8fe290f4800864f13f33a5bbefd5e8e82c4f 100644 (file)
@@ -22,6 +22,7 @@ libc_FILES=" \
        gethnmadr.c \
        gethostent.c \
        gethostname.c \
+       getifaddrs.c \
        getnetbyaddr.c \
        getnetbyname.c \
        getnetent.c \
diff --git a/lib/ip/getifaddrs.c b/lib/ip/getifaddrs.c
new file mode 100644 (file)
index 0000000..15dd13b
--- /dev/null
@@ -0,0 +1,71 @@
+#include <errno.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <ifaddrs.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <netinet/in.h>
+
+#include <net/gen/in.h>
+#include <net/gen/ip_io.h>
+#include <net/gen/tcp.h>
+#include <net/gen/tcp_io.h>
+#include <net/gen/udp.h>
+#include <net/gen/udp_io.h>
+
+int
+getifaddrs(struct ifaddrs **ifap)
+{
+       static int fd = -1;
+       nwio_ipconf_t ipconf;
+       int flags, err, r;
+       static struct ifaddrs ifa;
+       static struct sockaddr_in addr, netmask;
+
+       memset(&ifa, 0, sizeof(ifa));
+       memset(&addr, 0, sizeof(addr));
+       memset(&netmask, 0, sizeof(netmask));
+       ifa.ifa_next = NULL;
+       ifa.ifa_name = "ip";
+       addr.sin_family = netmask.sin_family = AF_INET;
+       ifa.ifa_addr = (struct sockaddr *) &addr;
+       ifa.ifa_netmask = (struct sockaddr *) &netmask;
+       addr.sin_addr.s_addr = 0;
+       netmask.sin_addr.s_addr = 0;
+
+       if(fd < 0) {
+               char *ipd;
+               if(!(ipd=getenv("IP_DEVICE")))
+                       ipd="/dev/ip";
+               if((fd = open(ipd, O_RDWR)) < 0)
+                       return -1;
+       }
+
+       /* Code taken from commands/simple/ifconfig.c. */
+
+       if((flags = fcntl(fd, F_GETFL)) < 0 ||
+          fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0 ||
+          ioctl(fd, NWIOGIPCONF, &ipconf))
+               return 0;       /* Report interface as down. */
+
+       addr.sin_addr.s_addr = ipconf.nwic_ipaddr;
+       netmask.sin_addr.s_addr = ipconf.nwic_netmask;
+       if(addr.sin_addr.s_addr) ifa.ifa_flags = IFF_UP;
+
+       /* Just report on this interface. */
+
+       *ifap = &ifa;
+
+       return 0;
+}
+
+void
+freeifaddrs(struct ifaddrs *ifp)
+{
+       /* getifaddrs points to static data, so no need to free. */
+       ;
+}
+