source: trunk/minix/lib/ip/getifaddrs.c@ 9

Last change on this file since 9 was 9, checked in by Mattia Monga, 13 years ago

Minix 3.1.2a

File size: 1.5 KB
Line 
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>
11
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>
18
19int
20getifaddrs(struct ifaddrs **ifap)
21{
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;
27
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;
38
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;
45 }
46
47 /* Code taken from commands/simple/ifconfig.c. */
48
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. */
53
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;
57
58 /* Just report on this interface. */
59
60 *ifap = &ifa;
61
62 return 0;
63}
64
65void
66freeifaddrs(struct ifaddrs *ifp)
67{
68 /* getifaddrs points to static data, so no need to free. */
69 ;
70}
71
Note: See TracBrowser for help on using the repository browser.