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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
|
From c3d5cce5c550896fd8e9cf856f66f5f264b49ef7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timo=20Ter=C3=A4s?= <timo.teras@iki.fi>
Date: Tue, 8 Apr 2014 14:03:16 +0000
Subject: [PATCH] reimplement if_nameindex and getifaddrs using netlink
---
src/network/__netlink.c | 38 ++++++
src/network/__netlink.h | 99 ++++++++++++++
src/network/getifaddrs.c | 325 +++++++++++++++++++++++++--------------------
src/network/if_nameindex.c | 107 +++++++++------
4 files changed, 382 insertions(+), 187 deletions(-)
create mode 100644 src/network/__netlink.c
create mode 100644 src/network/__netlink.h
diff --git a/src/network/__netlink.c b/src/network/__netlink.c
new file mode 100644
index 0000000..e75f374
--- /dev/null
+++ b/src/network/__netlink.c
@@ -0,0 +1,38 @@
+#include <errno.h>
+#include <string.h>
+#include <sys/socket.h>
+#include "__netlink.h"
+
+int __netlink_enumerate(int fd, unsigned int seq, int type, int af,
+ int (*cb)(void *ctx, struct nlmsghdr *h), void *ctx)
+{
+ struct nlmsghdr *h;
+ union {
+ uint8_t buf[8192];
+ struct {
+ struct nlmsghdr nlh;
+ struct rtgenmsg g;
+ } req;
+ struct nlmsghdr reply;
+ } u;
+ int r, ret = 0;
+
+ memset(&u.req, 0, sizeof(u.req));
+ u.req.nlh.nlmsg_len = sizeof(u.req);
+ u.req.nlh.nlmsg_type = type;
+ u.req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
+ u.req.nlh.nlmsg_seq = seq;
+ u.req.g.rtgen_family = af;
+ r = send(fd, &u.req, sizeof(u.req), 0);
+ if (r < 0) return r;
+
+ while (1) {
+ r = recv(fd, u.buf, sizeof(u.buf), MSG_DONTWAIT);
+ if (r <= 0) return -1;
+ for (h = &u.reply; NLMSG_OK(h, (void*)&u.buf[r]); h = NLMSG_NEXT(h)) {
+ if (h->nlmsg_type == NLMSG_DONE) return ret;
+ if (h->nlmsg_type == NLMSG_ERROR) return -1;
+ if (!ret) ret = cb(ctx, h);
+ }
+ }
+}
diff --git a/src/network/__netlink.h b/src/network/__netlink.h
new file mode 100644
index 0000000..40b12a2
--- /dev/null
+++ b/src/network/__netlink.h
@@ -0,0 +1,99 @@
+#include <stdint.h>
+
+/* linux/netlink.h */
+
+#define NETLINK_ROUTE 0
+
+struct nlmsghdr {
+ uint32_t nlmsg_len;
+ uint16_t nlmsg_type;
+ uint16_t nlmsg_flags;
+ uint32_t nlmsg_seq;
+ uint32_t nlmsg_pid;
+};
+
+#define NLM_F_REQUEST 1
+#define NLM_F_MULTI 2
+#define NLM_F_ACK 4
+#define NLM_F_ECHO 8
+#define NLM_F_DUMP_INTR 16
+
+#define NLM_F_ROOT 0x100
+#define NLM_F_MATCH 0x200
+#define NLM_F_ATOMIC 0x400
+#define NLM_F_DUMP (NLM_F_ROOT|NLM_F_MATCH)
+
+#define NLMSG_NOOP 0x1
+#define NLMSG_ERROR 0x2
+#define NLMSG_DONE 0x3
+#define NLMSG_OVERRUN 0x4
+
+/* linux/rtnetlink.h */
+
+#define RTM_GETLINK 18
+#define RTM_GETADDR 22
+
+struct rtattr {
+ unsigned short rta_len;
+ unsigned short rta_type;
+};
+
+struct rtgenmsg {
+ unsigned char rtgen_family;
+};
+
+struct ifinfomsg {
+ unsigned char ifi_family;
+ unsigned char __ifi_pad;
+ unsigned short ifi_type;
+ int ifi_index;
+ unsigned ifi_flags;
+ unsigned ifi_change;
+};
+
+/* linux/if_link.h */
+
+#define IFLA_ADDRESS 1
+#define IFLA_BROADCAST 2
+#define IFLA_IFNAME 3
+#define IFLA_STATS 7
+
+/* linux/if_addr.h */
+
+struct ifaddrmsg {
+ uint8_t ifa_family;
+ uint8_t ifa_prefixlen;
+ uint8_t ifa_flags;
+ uint8_t ifa_scope;
+ uint32_t ifa_index;
+};
+
+#define IFA_ADDRESS 1
+#define IFA_LOCAL 2
+#define IFA_LABEL 3
+#define IFA_BROADCAST 4
+
+/* musl */
+
+#define NETLINK_ALIGN(len) (((len)+3) & ~3)
+#define NLMSG_DATA(nlh) ((void*)((char*)(nlh)+sizeof(struct nlmsghdr)))
+#define NLMSG_DATALEN(nlh) ((nlh)->nlmsg_len-sizeof(struct nlmsghdr))
+#define NLMSG_DATAEND(nlh) ((char*)(nlh)+(nlh)->nlmsg_len)
+#define NLMSG_NEXT(nlh) (struct nlmsghdr*)((char*)(nlh)+NETLINK_ALIGN((nlh)->nlmsg_len))
+#define NLMSG_OK(nlh,end) (NLMSG_DATA(nlh) <= (end) && \
+ (nlh)->nlmsg_len >= sizeof(struct nlmsghdr) && \
+ (void*)NLMSG_NEXT(nlh) <= (end))
+
+#define RTA_DATA(rta) ((void*)((char*)(rta)+sizeof(struct rtattr)))
+#define RTA_DATALEN(rta) ((rta)->rta_len-sizeof(struct rtattr))
+#define RTA_DATAEND(rta) ((char*)(rta)+(rta)->rta_len)
+#define RTA_NEXT(rta) (struct rtattr*)((char*)(rta)+NETLINK_ALIGN((rta)->rta_len))
+#define RTA_OK(rta,end) (RTA_DATA(rta) <= (void*)(end) && \
+ (rta)->rta_len >= sizeof(struct rtattr) && \
+ (void*)RTA_NEXT(rta) <= (void*)(end))
+
+#define NLMSG_RTA(nlh,len) ((void*)((char*)(nlh)+sizeof(struct nlmsghdr)+NETLINK_ALIGN(len)))
+#define NLMSG_RTAOK(rta,nlh) RTA_OK(rta,NLMSG_DATAEND(nlh))
+
+int __netlink_enumerate(int fd, unsigned int seq, int type, int af,
+ int (*cb)(void *ctx, struct nlmsghdr *h), void *ctx);
diff --git a/src/network/getifaddrs.c b/src/network/getifaddrs.c
index 5a94cc7..9d4bd5b 100644
--- a/src/network/getifaddrs.c
+++ b/src/network/getifaddrs.c
@@ -1,181 +1,212 @@
-/* (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 <string.h>
+#include <stdlib.h>
#include <unistd.h>
-#include <sys/ioctl.h>
-#include <sys/socket.h>
+#include <ifaddrs.h>
+#include <syscall.h>
+#include <net/if.h>
+#include "__netlink.h"
-typedef union {
- struct sockaddr_in6 v6;
+/* getifaddrs() reports hardware addresses with PF_PACKET that implies
+ * struct sockaddr_ll. But e.g. Infiniband socket address length is
+ * longer than sockaddr_ll.ssl_addr[8] can hold. Use this hack struct
+ * to extend ssl_addr - callers should be able to still use it. */
+struct sockaddr_ll_hack {
+ unsigned short sll_family, sll_protocol;
+ int sll_ifindex;
+ unsigned short sll_hatype;
+ unsigned char sll_pkttype, sll_halen;
+ unsigned char sll_addr[24];
+};
+
+union sockany {
+ struct sockaddr sa;
+ struct sockaddr_ll_hack ll;
struct sockaddr_in v4;
-} soa;
+ struct sockaddr_in6 v6;
+};
-typedef struct ifaddrs_storage {
+struct ifaddrs_storage {
struct ifaddrs ifa;
- soa addr;
- soa netmask;
- soa dst;
+ struct ifaddrs_storage *hash_next;
+ union sockany addr, netmask, ifu;
+ unsigned int index;
char name[IFNAMSIZ+1];
-} stor;
-#define next ifa.ifa_next
+};
-static stor* list_add(stor** list, stor** head, char* ifname)
+#define IFADDRS_HASH_SIZE 64
+struct ifaddrs_ctx {
+ struct ifaddrs_storage *first;
+ struct ifaddrs_storage *last;
+ struct ifaddrs_storage *hash[IFADDRS_HASH_SIZE];
+};
+
+void freeifaddrs(struct ifaddrs *ifp)
{
- 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;
+ struct ifaddrs *n;
+ while (ifp) {
+ n = ifp->ifa_next;
+ free(ifp);
+ ifp = n;
}
- return curr;
}
-void freeifaddrs(struct ifaddrs *ifp)
+static void addifaddrs(struct ifaddrs_ctx *ctx, struct ifaddrs_storage *add)
{
- stor *head = (stor *) ifp;
- while(head) {
- void *p = head;
- head = (stor *) head->next;
- free(p);
+ if (!add->ifa.ifa_name) {
+ free(add);
+ return;
}
+ if (!ctx->first) ctx->first = add;
+ if (ctx->last) ctx->last->ifa.ifa_next = &add->ifa;
+ ctx->last = add;
+}
+
+static struct sockaddr* copy_lladdr(union sockany *sa, struct rtattr *rta, struct ifinfomsg *ifi)
+{
+ if (RTA_DATALEN(rta) > sizeof(sa->ll.sll_addr)) return 0;
+ sa->ll.sll_family = AF_PACKET;
+ sa->ll.sll_ifindex = ifi->ifi_index;
+ sa->ll.sll_hatype = ifi->ifi_type;
+ sa->ll.sll_halen = RTA_DATALEN(rta);
+ memcpy(sa->ll.sll_addr, RTA_DATA(rta), RTA_DATALEN(rta));
+ return &sa->sa;
}
-static void ipv6netmask(unsigned prefix_length, struct sockaddr_in6 *sa)
+static uint8_t *sockany_addr(int af, union sockany *sa, int *len)
{
- unsigned char* hb = sa->sin6_addr.s6_addr;
- unsigned onebytes = prefix_length / 8;
- unsigned bits = prefix_length % 8;
- unsigned nullbytes = 16 - onebytes;
- memset(hb, -1, onebytes);
- memset(hb+onebytes, 0, nullbytes);
- if(bits) {
- unsigned char x = -1;
- x <<= 8 - bits;
- hb[onebytes] = x;
+ switch (af) {
+ case AF_INET: *len = 4; return (uint8_t*) &sa->v4.sin_addr;
+ case AF_INET6: *len = 16; return (uint8_t*) &sa->v6.sin6_addr;
}
+ return 0;
}
-static void dealwithipv6(stor **list, stor** head)
+static struct sockaddr* copy_addr(int af, union sockany *sa, struct rtattr *rta)
{
- FILE* f = fopen("/proc/net/if_inet6", "rbe");
- /* 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;
- }
+ int len;
+ uint8_t *dst = sockany_addr(af, sa, &len);
+ if (!dst || RTA_DATALEN(rta) != len) return 0;
+ sa->sa.sa_family = af;
+ memcpy(dst, RTA_DATA(rta), len);
+ return &sa->sa;
+}
+
+static struct sockaddr *gen_netmask(int af, union sockany *sa, int prefixlen)
+{
+ int maxlen, i;
+ uint8_t *dst = sockany_addr(af, sa, &maxlen);
+ if (!dst) return 0;
+ sa->sa.sa_family = af;
+ if (prefixlen > 8*maxlen) prefixlen = 8*maxlen;
+ i = prefixlen / 8;
+ memset(dst, 0xff, i);
+ if (i<maxlen) {
+ dst[i++] = 0xff << (8 - (prefixlen % 8));
+ if (i<maxlen) memset(&dst[i+1], 0x00, maxlen-i);
}
- out:
- fclose(f);
+ return &sa->sa;
}
-int getifaddrs(struct ifaddrs **ifap)
+static int __handle_link(void *pctx, struct nlmsghdr *h)
{
- 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;
- }
+ struct ifaddrs_ctx *ctx = pctx;
+ struct ifaddrs_storage *ifs;
+ struct ifinfomsg *ifi = NLMSG_DATA(h);
+ struct rtattr *rta;
+ int stats_len = 0;
+
+ for (rta = NLMSG_RTA(h, sizeof(*ifi)); NLMSG_RTAOK(rta, h); rta = RTA_NEXT(rta)) {
+ if (rta->rta_type != IFLA_STATS) continue;
+ stats_len = RTA_DATALEN(rta);
+ break;
}
- if_freenameindex(ii);
-
- int sock = socket(PF_INET, SOCK_DGRAM|SOCK_CLOEXEC, 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;
+
+ ifs = calloc(1, sizeof(struct ifaddrs_storage) + stats_len);
+ if (ifs == 0) return -1;
+
+ ifs->index = ifi->ifi_index;
+ ifs->ifa.ifa_flags = ifi->ifi_flags;
+
+ for (rta = NLMSG_RTA(h, sizeof(*ifi)); NLMSG_RTAOK(rta, h); rta = RTA_NEXT(rta)) {
+ switch (rta->rta_type) {
+ case IFLA_IFNAME:
+ if (RTA_DATALEN(rta) <= IFNAMSIZ) {
+ strncpy(ifs->name, RTA_DATA(rta), RTA_DATALEN(rta));
+ ifs->ifa.ifa_name = ifs->name;
}
+ break;
+ case IFLA_ADDRESS:
+ ifs->ifa.ifa_addr = copy_lladdr(&ifs->addr, rta, ifi);
+ break;
+ case IFLA_BROADCAST:
+ ifs->ifa.ifa_broadaddr = copy_lladdr(&ifs->ifu, rta, ifi);
+ break;
+ case IFLA_STATS:
+ ifs->ifa.ifa_data = (void*)(ifs+1);
+ memcpy(ifs->ifa.ifa_data, RTA_DATA(rta), RTA_DATALEN(rta));
+ 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;
+ }
+ if (ifs->ifa.ifa_name) {
+ ifs->hash_next = ctx->hash[ifs->index%IFADDRS_HASH_SIZE];
+ ctx->hash[ifs->index%IFADDRS_HASH_SIZE] = ifs;
+ }
+ addifaddrs(ctx, ifs);
+ return 0;
+}
+
+static int __handle_addr(void *pctx, struct nlmsghdr *h)
+{
+ struct ifaddrs_ctx *ctx = pctx;
+ struct ifaddrs_storage *ifs, *ifs0;
+ struct ifaddrmsg *ifa = NLMSG_DATA(h);
+ struct rtattr *rta;
+
+ ifs = calloc(1, sizeof(struct ifaddrs_storage));
+ if (ifs == 0) return -1;
+
+ for (ifs0 = ctx->hash[ifa->ifa_index%IFADDRS_HASH_SIZE]; ifs0; ifs0 = ifs0->hash_next)
+ if (ifs0->index == ifa->ifa_index)
+ break;
+ if (!ifs0) return 0;
+
+ ifs->ifa.ifa_name = ifs0->ifa.ifa_name;
+ ifs->ifa.ifa_flags = ifs0->ifa.ifa_flags;
+ for (rta = NLMSG_RTA(h, sizeof(*ifa)); NLMSG_RTAOK(rta, h); rta = RTA_NEXT(rta)) {
+ switch (rta->rta_type) {
+ case IFA_ADDRESS:
+ ifs->ifa.ifa_addr = copy_addr(ifa->ifa_family, &ifs->addr, rta);
+ if (ifs->ifa.ifa_addr)
+ ifs->ifa.ifa_netmask = gen_netmask(ifa->ifa_family, &ifs->netmask, ifa->ifa_prefixlen);
+ break;
+ case IFA_BROADCAST:
+ /* For point-to-point links this is peer, but ifa_broadaddr
+ * and ifa_dstaddr are union, so this works for both. */
+ ifs->ifa.ifa_broadaddr = copy_addr(ifa->ifa_family, &ifs->ifu, rta);
+ break;
}
}
- close(sock);
- void* last = 0;
- for(head = list; head; head=(stor*)head->next) last=head;
- head = last;
- dealwithipv6(&list, &head);
- *ifap = (struct ifaddrs*) list;
+
+ addifaddrs(ctx, ifs);
return 0;
- err:
- close(sock);
- err2:
- freeifaddrs((struct ifaddrs*) list);
- return -1;
}
+int getifaddrs(struct ifaddrs **ifap)
+{
+ struct ifaddrs_ctx _ctx, *ctx = &_ctx;
+ int r = 0, fd;
+
+ fd = socket(PF_NETLINK, SOCK_RAW|SOCK_CLOEXEC, NETLINK_ROUTE);
+ if (fd < 0) return -1;
+
+ memset(ctx, 0, sizeof(*ctx));
+ if (__netlink_enumerate(fd, 1, RTM_GETLINK, AF_UNSPEC, __handle_link, ctx)) r = -1;
+ if (__netlink_enumerate(fd, 1, RTM_GETADDR, AF_UNSPEC, __handle_addr, ctx)) r = -1;
+ __syscall(SYS_close,fd);
+
+ if (r == 0) *ifap = &ctx->first->ifa;
+ else freeifaddrs(&ctx->first->ifa);
+ return r;
+}
diff --git a/src/network/if_nameindex.c b/src/network/if_nameindex.c
index 53b80b2..cb5587c 100644
--- a/src/network/if_nameindex.c
+++ b/src/network/if_nameindex.c
@@ -1,55 +1,82 @@
#define _GNU_SOURCE
#include <net/if.h>
-#include <stdlib.h>
#include <sys/socket.h>
-#include <sys/ioctl.h>
#include <errno.h>
-#include "syscall.h"
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+#include <syscall.h>
+#include "__netlink.h"
+
+struct ifnamemap {
+ unsigned int index;
+ unsigned char namelen;
+ char name[IFNAMSIZ];
+};
+
+struct ifnameindexctx {
+ unsigned int num;
+ unsigned int str_bytes;
+ struct ifnamemap *list;
+};
-static void *do_nameindex(int s, size_t n)
+static int __handle_link(void *pctx, struct nlmsghdr *h)
{
- size_t i, len, k;
- struct ifconf conf;
- struct if_nameindex *idx;
-
- idx = malloc(n * (sizeof(struct if_nameindex)+sizeof(struct ifreq)));
- if (!idx) return 0;
-
- conf.ifc_buf = (void *)&idx[n];
- conf.ifc_len = len = n * sizeof(struct ifreq);
- if (ioctl(s, SIOCGIFCONF, &conf) < 0) {
- free(idx);
- return 0;
- }
- if (conf.ifc_len == len) {
- free(idx);
- return (void *)-1;
- }
+ struct ifnameindexctx *ctx = pctx;
+ struct ifinfomsg *ifim = NLMSG_DATA(h);
+ struct rtattr *rta;
+ struct ifnamemap *e;
+
+ for (rta = NLMSG_RTA(h, sizeof(*ifim)); NLMSG_RTAOK(rta, h); rta = RTA_NEXT(rta)) {
+ if (rta->rta_type != IFLA_IFNAME) continue;
+ if (RTA_DATALEN(rta) > IFNAMSIZ) return -ENOBUFS;
- n = conf.ifc_len / sizeof(struct ifreq);
- for (i=k=0; i<n; i++) {
- if (ioctl(s, SIOCGIFINDEX, &conf.ifc_req[i]) < 0) {
- k++;
- continue;
- }
- idx[i-k].if_index = conf.ifc_req[i].ifr_ifindex;
- idx[i-k].if_name = conf.ifc_req[i].ifr_name;
+ ctx->num++;
+ ctx->str_bytes += RTA_DATALEN(rta) + 1;
+ e = realloc(ctx->list, sizeof(struct ifnamemap[ctx->num]));
+ if (e == 0) return -ENOMEM;
+ ctx->list = e;
+
+ e = &ctx->list[ctx->num-1];
+ e->index = ifim->ifi_index;
+ e->namelen = RTA_DATALEN(rta);
+ memcpy(e->name, RTA_DATA(rta), IFNAMSIZ);
}
- idx[i-k].if_name = 0;
- idx[i-k].if_index = 0;
- return idx;
+ return 0;
}
struct if_nameindex *if_nameindex()
{
- size_t n;
- void *p = 0;
- int s = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
- if (s>=0) {
- for (n=0; (p=do_nameindex(s, n)) == (void *)-1; n++);
- __syscall(SYS_close, s);
+ struct ifnameindexctx _ctx, *ctx = &_ctx;
+ struct if_nameindex *ifs = NULL;
+ int fd, r, i;
+ char *p;
+
+ fd = socket(PF_NETLINK, SOCK_RAW|SOCK_CLOEXEC, NETLINK_ROUTE);
+ if (fd < 0) goto err;
+
+ memset(ctx, 0, sizeof(*ctx));
+ r = __netlink_enumerate(fd, 1, RTM_GETLINK, AF_UNSPEC, __handle_link, ctx);
+ __syscall(SYS_close,fd);
+
+ if (r < 0) goto err;
+
+ ifs = malloc(sizeof(struct if_nameindex[ctx->num+1]) + ctx->str_bytes);
+ if (ifs == 0) goto err;
+
+ p = (char*)ifs + sizeof(struct if_nameindex[ctx->num+1]);
+ for (i = 0; i < ctx->num; i++) {
+ ifs[i].if_index = ctx->list[i].index;
+ ifs[i].if_name = p;
+ memcpy(p, ctx->list[i].name, ctx->list[i].namelen);
+ p += ctx->list[i].namelen;
+ *p++ = 0;
}
- errno = ENOBUFS;
- return p;
+ ifs[i].if_index = 0;
+ ifs[i].if_name = 0;
+err:
+ free(ctx->list);
+ if (ifs == NULL) errno = ENOBUFS;
+ return ifs;
}
--
1.9.2
|