forked from Stichting-MINIX-Research-Foundation/minix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetifaddrs.c
More file actions
68 lines (56 loc) · 1.46 KB
/
Copy pathgetifaddrs.c
File metadata and controls
68 lines (56 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#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/udp.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. */
;
}