about summary refs log tree commit diff
path: root/src/network/getifaddrs.c
blob: 9ccf45796e71a95d48aeb2b533f52f81e7a47e6e (plain) (blame)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/* (C) 2013 John Spencer. released under musl's standard MIT license. */
#undef _GNU_SOURCE
#define _GNU_SOURCE
#include <ifaddrs.h>
#include <stdlib.h>
#include <net/if.h> /* IFNAMSIZ, ifreq, ifconf */
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
#include <arpa/inet.h> /* inet_pton */
#include <unistd.h>
#include <sys/ioctl.h>

typedef union {
	struct sockaddr_in6 v6;
	struct sockaddr_in v4;
} soa;

typedef struct ifaddrs_storage {
	struct ifaddrs ifa;
	soa addr;
	soa netmask;
	soa dst;
	char name[IFNAMSIZ+1];
} stor;
#define next ifa.ifa_next

static stor* list_add(stor** list, stor** head, char* ifname)
{
	stor* curr = calloc(1, sizeof(stor));
	if(curr) {
		strcpy(curr->name, ifname);
		curr->ifa.ifa_name = curr->name;
		if(*head) (*head)->next = (struct ifaddrs*) curr;
		*head = curr;
		if(!*list) *list = curr;
	}
	return curr;
}

void freeifaddrs(struct ifaddrs *ifp)
{
	stor *head = (stor *) ifp;
	while(head) {
		void *p = head;
		head = (stor *) head->next;
		free(p);
	}
}

static void ipv6netmask(unsigned prefix_length, struct sockaddr_in6 *sa)
{
	// FIXME: left for bit-wizard rich
	memset(&sa->sin6_addr, -1, sizeof(sa->sin6_addr));
}

static void dealwithipv6(stor **list, stor** head)
{
	FILE* f = fopen("/proc/net/if_inet6", "r");
	/* 00000000000000000000000000000001 01 80 10 80 lo
	   A                                B  C  D  E  F
	   all numbers in hex
	   A = addr B=netlink device#, C=prefix length,
	   D = scope value (ipv6.h) E = interface flags (rnetlink.h, addrconf.c)
	   F = if name */
	char v6conv[32 + 7 + 1], *v6;
	char *line, linebuf[512];
	if(!f) return;
	while((line = fgets(linebuf, sizeof linebuf, f))) {
		v6 = v6conv;
		size_t i = 0;
		for(; i < 8; i++) {
			memcpy(v6, line, 4);
			v6+=4;
			*v6++=':';
			line+=4;
		}
		--v6; *v6 = 0;
		line++;
		unsigned b, c, d, e;
		char name[IFNAMSIZ+1];
		if(5 == sscanf(line, "%x %x %x %x %s", &b, &c, &d, &e, name)) {
			struct sockaddr_in6 sa = {0};
			if(1 == inet_pton(AF_INET6, v6conv, &sa.sin6_addr)) {
				sa.sin6_family = AF_INET6;
				stor* curr = list_add(list, head, name);
				if(!curr) goto out;
				curr->addr.v6 = sa;
				curr->ifa.ifa_addr = (struct sockaddr*) &curr->addr;
				ipv6netmask(c, &sa);
				curr->netmask.v6 = sa;
				curr->ifa.ifa_netmask = (struct sockaddr*) &curr->netmask;
				/* find ipv4 struct with the same interface name to copy flags */
				stor* scan = *list;
				for(;scan && strcmp(name, scan->name);scan=(stor*)scan->next);
				if(scan) curr->ifa.ifa_flags = scan->ifa.ifa_flags;
				else curr->ifa.ifa_flags = 0;
			} else errno = 0;
		}
	}
	out:
	fclose(f);
}

int getifaddrs(struct ifaddrs **ifap)
{
	stor *list = 0, *head = 0;
	struct if_nameindex* ii = if_nameindex();
	if(!ii) return -1;
	size_t i;
	for(i = 0; ii[i].if_index || ii[i].if_name; i++) {
		stor* curr = list_add(&list, &head, ii[i].if_name);
		if(!curr) {
			if_freenameindex(ii);
			goto err2;
		}
	}
	if_freenameindex(ii);

	int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
	if(sock == -1) goto err2;
	struct ifreq reqs[32]; /* arbitrary chosen boundary */
	struct ifconf conf = {.ifc_len = sizeof reqs, .ifc_req = reqs};
	if(-1 == ioctl(sock, SIOCGIFCONF, &conf)) goto err;
	size_t reqitems = conf.ifc_len / sizeof(struct ifreq);
	for(head = list; head; head = (stor*)head->next) {
		for(i = 0; i < reqitems; i++) {
			// get SIOCGIFADDR of active interfaces.
			if(!strcmp(reqs[i].ifr_name, head->name)) {
				head->addr.v4 = *(struct sockaddr_in*)&reqs[i].ifr_addr;
				head->ifa.ifa_addr = (struct sockaddr*) &head->addr;
				break;
			}
		}
		struct ifreq req;
		snprintf(req.ifr_name, sizeof req.ifr_name, "%s", head->name);
		if(-1 == ioctl(sock, SIOCGIFFLAGS, &req)) goto err;

		head->ifa.ifa_flags = req.ifr_flags;
		if(head->ifa.ifa_addr) {
			/* or'ing flags with IFF_LOWER_UP on active interfaces to mimic glibc */
			head->ifa.ifa_flags |= IFF_LOWER_UP; 
			if(-1 == ioctl(sock, SIOCGIFNETMASK, &req)) goto err;
			head->netmask.v4 = *(struct sockaddr_in*)&req.ifr_netmask;
			head->ifa.ifa_netmask = (struct sockaddr*) &head->netmask;
	
			if(head->ifa.ifa_flags & IFF_POINTOPOINT) {
				if(-1 == ioctl(sock, SIOCGIFDSTADDR, &req)) goto err;
				head->dst.v4 = *(struct sockaddr_in*)&req.ifr_dstaddr;
			} else {
				if(-1 == ioctl(sock, SIOCGIFBRDADDR, &req)) goto err;
				head->dst.v4 = *(struct sockaddr_in*)&req.ifr_broadaddr;
			}
			head->ifa.ifa_ifu.ifu_dstaddr = (struct sockaddr*) &head->dst;
		}
	}
	close(sock);
	void* last = 0;
	for(head = list; head; head=(stor*)head->next) last=head;
	head = last;
	dealwithipv6(&list, &head);
	*ifap = (struct ifaddrs*) list;
	return 0;
	err:
	close(sock);
	err2:
	freeifaddrs((struct ifaddrs*) list);
	return -1;
}