This repository has been archived by the owner on Jun 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 82
/
netifaces.c
2918 lines (2465 loc) · 77.4 KB
/
netifaces.c
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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <Python.h>
/* Before Python 2.6, PyUnicode_FromString doesn't exist */
#if PY_MAJOR_VERSION < 2 || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 6)
PyObject *PyUnicode_FromString(const char *s)
{
Py_ssize_t len = strlen(s);
if (!len) {
Py_UNICODE uc = 0;
return PyUnicode_FromUnicode(&uc, 0);
}
return PyUnicode_DecodeUTF8(s, len, NULL);
}
#endif
/* Python 3 compatibility */
#if PY_MAJOR_VERSION >= 3
#define PyInt_FromLong PyLong_FromLong
#define MODULE_ERROR NULL
#define MODULE_RETURN(v) return (v)
#define MODULE_INIT(name) PyMODINIT_FUNC PyInit_##name(void)
#define MODULE_DEF(name,doc,methods) \
static struct PyModuleDef moduledef = { \
PyModuleDef_HEAD_INIT, (name), (doc), -1, (methods), };
#define MODULE_CREATE(obj,name,doc,methods) \
obj = PyModule_Create(&moduledef);
#else /* PY_MAJOR_VERSION < 3 */
#define MODULE_ERROR
#define MODULE_RETURN(v)
#define MODULE_INIT(name) void init##name(void)
#define MODULE_DEF(name,doc,methods)
#define MODULE_CREATE(obj,name,doc,methods) \
obj = Py_InitModule3((name), (methods), (doc));
#endif
#ifndef WIN32
# include <sys/types.h>
# include <sys/socket.h>
# include <net/if.h>
# include <netdb.h>
# if HAVE_PF_ROUTE
# include <net/route.h>
# endif
# if HAVE_SYSCTL_CTL_NET
# include <sys/sysctl.h>
# include <net/route.h>
# endif
/* RTNL_FAMILY_MAX not there yet in old kernels, see linux commit 25239ce */
# if HAVE_PF_NETLINK
# include <asm/types.h>
# include <linux/netlink.h>
# include <linux/rtnetlink.h>
# if !defined(RTNL_FAMILY_MAX)
# include <linux/net.h>
# define RTNL_FAMILY_MAX NPROTO
# endif
# include <arpa/inet.h>
# endif
# if HAVE_GETIFADDRS
# if HAVE_IPV6_SOCKET_IOCTLS
# include <sys/ioctl.h>
# include <netinet/in.h>
# include <netinet/in_var.h>
# endif
# endif
# if HAVE_SOCKET_IOCTLS
# include <sys/ioctl.h>
# include <netinet/in.h>
# include <arpa/inet.h>
# if defined(__sun)
# include <unistd.h>
# include <stropts.h>
# include <sys/sockio.h>
# endif
# endif /* HAVE_SOCKET_IOCTLS */
/* For logical interfaces support we convert all names to same name prefixed
with l */
#if HAVE_SIOCGLIFNUM
#define CNAME(x) l##x
#else
#define CNAME(x) x
#endif
#if HAVE_NET_IF_DL_H
# include <net/if_dl.h>
#endif
/* For the benefit of stupid platforms (Linux), include all the sockaddr
definitions we can lay our hands on. It can also be useful for the benefit
of another stupid platform (FreeBSD, see PR 152036). */
#include <netinet/in.h>
# if HAVE_NETASH_ASH_H
# include <netash/ash.h>
# endif
# if HAVE_NETATALK_AT_H
# include <netatalk/at.h>
# endif
# if HAVE_NETAX25_AX25_H
# include <netax25/ax25.h>
# endif
# if HAVE_NETECONET_EC_H
# include <neteconet/ec.h>
# endif
# if HAVE_NETIPX_IPX_H
# include <netipx/ipx.h>
# endif
# if HAVE_NETPACKET_PACKET_H
# include <netpacket/packet.h>
# endif
# if HAVE_NETROSE_ROSE_H
# include <netrose/rose.h>
# endif
# if HAVE_LINUX_IRDA_H
# include <linux/irda.h>
# endif
# if HAVE_LINUX_ATM_H
# include <linux/atm.h>
# endif
# if HAVE_LINUX_LLC_H
# include <linux/llc.h>
# endif
# if HAVE_LINUX_TIPC_H
# include <linux/tipc.h>
# endif
# if HAVE_LINUX_DN_H
# include <linux/dn.h>
# endif
/* Map address families to sizes of sockaddr structs */
static int af_to_len(int af)
{
switch (af) {
case AF_INET: return sizeof (struct sockaddr_in);
#if defined(AF_INET6) && HAVE_SOCKADDR_IN6
case AF_INET6: return sizeof (struct sockaddr_in6);
#endif
#if defined(AF_AX25) && HAVE_SOCKADDR_AX25
# if defined(AF_NETROM)
case AF_NETROM: /* I'm assuming this is carried over x25 */
# endif
case AF_AX25: return sizeof (struct sockaddr_ax25);
#endif
#if defined(AF_IPX) && HAVE_SOCKADDR_IPX
case AF_IPX: return sizeof (struct sockaddr_ipx);
#endif
#if defined(AF_APPLETALK) && HAVE_SOCKADDR_AT
case AF_APPLETALK: return sizeof (struct sockaddr_at);
#endif
#if defined(AF_ATMPVC) && HAVE_SOCKADDR_ATMPVC
case AF_ATMPVC: return sizeof (struct sockaddr_atmpvc);
#endif
#if defined(AF_ATMSVC) && HAVE_SOCKADDR_ATMSVC
case AF_ATMSVC: return sizeof (struct sockaddr_atmsvc);
#endif
#if defined(AF_X25) && HAVE_SOCKADDR_X25
case AF_X25: return sizeof (struct sockaddr_x25);
#endif
#if defined(AF_ROSE) && HAVE_SOCKADDR_ROSE
case AF_ROSE: return sizeof (struct sockaddr_rose);
#endif
#if defined(AF_DECnet) && HAVE_SOCKADDR_DN
case AF_DECnet: return sizeof (struct sockaddr_dn);
#endif
#if defined(AF_PACKET) && HAVE_SOCKADDR_LL
case AF_PACKET: return sizeof (struct sockaddr_ll);
#endif
#if defined(AF_ASH) && HAVE_SOCKADDR_ASH
case AF_ASH: return sizeof (struct sockaddr_ash);
#endif
#if defined(AF_ECONET) && HAVE_SOCKADDR_EC
case AF_ECONET: return sizeof (struct sockaddr_ec);
#endif
#if defined(AF_IRDA) && HAVE_SOCKADDR_IRDA
case AF_IRDA: return sizeof (struct sockaddr_irda);
#endif
#if defined(AF_LINK) && HAVE_SOCKADDR_DL
case AF_LINK: return sizeof (struct sockaddr_dl);
#endif
}
return sizeof (struct sockaddr);
}
#if !HAVE_SOCKADDR_SA_LEN
#define SA_LEN(sa) af_to_len(sa->sa_family)
#if HAVE_SIOCGLIFNUM
#define SS_LEN(sa) af_to_len(sa->ss_family)
#else
#define SS_LEN(sa) SA_LEN(sa)
#endif
#else
#define SA_LEN(sa) sa->sa_len
#endif /* !HAVE_SOCKADDR_SA_LEN */
# if HAVE_GETIFADDRS
# include <ifaddrs.h>
# endif /* HAVE_GETIFADDRS */
# if !HAVE_GETIFADDRS && (!HAVE_SOCKET_IOCTLS || !HAVE_SIOCGIFCONF)
/* If the platform doesn't define, what we need, barf. If you're seeing this,
it means you need to write suitable code to retrieve interface information
on your system. */
# error You need to add code for your platform.
# endif
#else /* defined(WIN32) */
#define _WIN32_WINNT 0x0501
# include <winsock2.h>
# include <ws2tcpip.h>
# include <iphlpapi.h>
# include <netioapi.h>
#endif /* defined(WIN32) */
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
/* On systems without AF_LINK (Windows, for instance), define it anyway, but
give it a crazy value. On Linux, which has AF_PACKET but not AF_LINK,
define AF_LINK as the latter instead. */
#ifndef AF_LINK
# ifdef AF_PACKET
# define AF_LINK AF_PACKET
# else
# define AF_LINK -1000
# endif
# define HAVE_AF_LINK 0
#else
# define HAVE_AF_LINK 1
#endif
/* -- Utility Functions ----------------------------------------------------- */
#if !defined(WIN32)
#if !HAVE_GETNAMEINFO
#undef getnameinfo
#undef NI_NUMERICHOST
#define getnameinfo our_getnameinfo
#define NI_NUMERICHOST 1
/* A very simple getnameinfo() for platforms without */
static int
getnameinfo (const struct sockaddr *addr, int addr_len,
char *buffer, int buflen,
char *buf2, int buf2len,
int flags)
{
switch (addr->sa_family) {
case AF_INET:
{
const struct sockaddr_in *sin = (struct sockaddr_in *)addr;
const unsigned char *bytes = (unsigned char *)&sin->sin_addr.s_addr;
char tmpbuf[20];
sprintf (tmpbuf, "%d.%d.%d.%d",
bytes[0], bytes[1], bytes[2], bytes[3]);
strncpy (buffer, tmpbuf, buflen);
}
break;
#ifdef AF_INET6
case AF_INET6:
{
const struct sockaddr_in6 *sin = (const struct sockaddr_in6 *)addr;
const unsigned char *bytes = sin->sin6_addr.s6_addr;
int n;
char tmpbuf[80], *ptr = tmpbuf;
int done_double_colon = FALSE;
int colon_mode = FALSE;
for (n = 0; n < 8; ++n) {
unsigned char b1 = bytes[2 * n];
unsigned char b2 = bytes[2 * n + 1];
if (b1) {
if (colon_mode) {
colon_mode = FALSE;
*ptr++ = ':';
}
sprintf (ptr, "%x%02x", b1, b2);
ptr += strlen (ptr);
*ptr++ = ':';
} else if (b2) {
if (colon_mode) {
colon_mode = FALSE;
*ptr++ = ':';
}
sprintf (ptr, "%x", b2);
ptr += strlen (ptr);
*ptr++ = ':';
} else {
if (!colon_mode) {
if (done_double_colon) {
*ptr++ = '0';
*ptr++ = ':';
} else {
if (n == 0)
*ptr++ = ':';
colon_mode = TRUE;
done_double_colon = TRUE;
}
}
}
}
if (colon_mode) {
colon_mode = FALSE;
*ptr++ = ':';
*ptr++ = '\0';
} else {
*--ptr = '\0';
}
strncpy (buffer, tmpbuf, buflen);
}
break;
#endif /* AF_INET6 */
default:
return -1;
}
return 0;
}
#endif
static int
string_from_sockaddr (struct sockaddr *addr,
char *buffer,
size_t buflen)
{
struct sockaddr* bigaddr = 0;
int failure;
struct sockaddr* gniaddr;
socklen_t gnilen;
if (!addr || addr->sa_family == AF_UNSPEC)
return -1;
if (SA_LEN(addr) < af_to_len(addr->sa_family)) {
/* Sometimes ifa_netmask can be truncated. So let's detruncate it. FreeBSD
PR: kern/152036: getifaddrs(3) returns truncated sockaddrs for netmasks
-- http://www.freebsd.org/cgi/query-pr.cgi?pr=152036 */
gnilen = af_to_len(addr->sa_family);
bigaddr = calloc(1, gnilen);
if (!bigaddr)
return -1;
memcpy(bigaddr, addr, SA_LEN(addr));
#if HAVE_SOCKADDR_SA_LEN
bigaddr->sa_len = gnilen;
#endif
gniaddr = bigaddr;
} else {
gnilen = SA_LEN(addr);
gniaddr = addr;
}
failure = getnameinfo (gniaddr, gnilen,
buffer, buflen,
NULL, 0,
NI_NUMERICHOST);
if (bigaddr) {
free(bigaddr);
bigaddr = 0;
}
if (failure) {
size_t n, len;
char *ptr;
const char *data;
len = SA_LEN(addr);
#if HAVE_AF_LINK
/* BSD-like systems have AF_LINK */
if (addr->sa_family == AF_LINK) {
struct sockaddr_dl *dladdr = (struct sockaddr_dl *)addr;
len = dladdr->sdl_alen;
data = LLADDR(dladdr);
} else {
#endif
#if defined(AF_PACKET)
/* Linux has AF_PACKET instead */
if (addr->sa_family == AF_PACKET) {
struct sockaddr_ll *lladdr = (struct sockaddr_ll *)addr;
len = lladdr->sll_halen;
data = (const char *)lladdr->sll_addr;
} else {
#endif
/* We don't know anything about this sockaddr, so just display
the entire data area in binary. */
len -= (sizeof (struct sockaddr) - sizeof (addr->sa_data));
data = addr->sa_data;
#if defined(AF_PACKET)
}
#endif
#if HAVE_AF_LINK
}
#endif
if (buflen < 3 * len)
return -1;
ptr = buffer;
buffer[0] = '\0';
for (n = 0; n < len; ++n) {
sprintf (ptr, "%02x:", data[n] & 0xff);
ptr += 3;
}
if (len)
*--ptr = '\0';
}
if (!buffer[0])
return -1;
return 0;
}
/* Tries to format in CIDR form where possible; falls back to using
string_from_sockaddr(). */
static int
string_from_netmask (struct sockaddr *addr,
char *buffer,
size_t buflen)
{
#ifdef AF_INET6
if (addr && addr->sa_family == AF_INET6) {
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr;
unsigned n = 16;
unsigned zeroes = 0;
unsigned prefix;
unsigned bytes;
char *bufptr = buffer;
char *bufend = buffer + buflen;
char pfxbuf[16];
while (n--) {
unsigned char byte = sin6->sin6_addr.s6_addr[n];
/* We need to count the rightmost zeroes */
unsigned char x = byte;
unsigned zx = 8;
x &= -x;
if (x)
--zx;
if (x & 0x0f)
zx -= 4;
if (x & 0x33)
zx -= 2;
if (x & 0x55)
zx -= 1;
zeroes += zx;
if (byte)
break;
}
prefix = 128 - zeroes;
bytes = 2 * ((prefix + 15) / 16);
for (n = 0; n < bytes; ++n) {
unsigned char byte = sin6->sin6_addr.s6_addr[n];
char ch1, ch2;
if (n && !(n & 1)) {
if (bufptr < bufend)
*bufptr++ = ':';
}
ch1 = '0' + (byte >> 4);
if (ch1 > '9')
ch1 += 'a' - '0' - 10;
ch2 = '0' + (byte & 0xf);
if (ch2 > '9')
ch2 += 'a' - '0' - 10;
if (bufptr < bufend)
*bufptr++ = ch1;
if (bufptr < bufend)
*bufptr++ = ch2;
}
if (bytes < 16) {
if (bufend - bufptr > 2) {
*bufptr++ = ':';
*bufptr++ = ':';
}
}
sprintf (pfxbuf, "/%u", prefix);
if (bufend - bufptr > (int)strlen(pfxbuf))
strcpy (bufptr, pfxbuf);
if (buflen)
buffer[buflen - 1] = '\0';
return 0;
}
#endif
return string_from_sockaddr(addr, buffer, buflen);
}
#endif /* !defined(WIN32) */
#if defined(WIN32)
static int
compare_bits (const void *pva,
const void *pvb,
unsigned bits)
{
const unsigned char *pa = (const unsigned char *)pva;
const unsigned char *pb = (const unsigned char *)pvb;
unsigned char a, b;
static unsigned char masks[] = {
0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe
};
while (bits >= 8) {
a = *pa++;
b = *pb++;
if (a < b)
return -1;
else if (a > b)
return +1;
bits -= 8;
}
if (bits) {
a = *pa++ & masks[bits];
b = *pb++ & masks[bits];
if (a < b)
return -1;
else if (a > b)
return +1;
}
return 0;
}
static PyObject *
netmask_from_prefix (unsigned prefix)
{
char buffer[256];
char *bufptr = buffer;
char *bufend = buffer + sizeof(buffer);
unsigned bytes = 2 * ((prefix + 15) / 16);
static const unsigned char masks[] = {
0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe
};
unsigned n;
unsigned left = prefix;
char pfxbuf[16];
for (n = 0; n < bytes; ++n) {
unsigned char byte;
char ch1, ch2;
if (left >= 8) {
byte = 0xff;
left -= 8;
} else {
byte = masks[left];
left = 0;
}
if (n && !(n & 1)) {
if (bufptr < bufend)
*bufptr++ = ':';
}
ch1 = '0' + (byte >> 4);
if (ch1 > '9')
ch1 += 'a' - '0' - 10;
ch2 = '0' + (byte & 0xf);
if (ch2 > '9')
ch2 += 'a' - '0' - 10;
if (bufptr < bufend)
*bufptr++ = ch1;
if (bufptr < bufend)
*bufptr++ = ch2;
}
if (bytes < 16) {
if (bufend - bufptr > 2) {
*bufptr++ = ':';
*bufptr++ = ':';
}
}
sprintf (pfxbuf, "/%u", prefix);
if ((size_t)(bufend - bufptr) > strlen(pfxbuf))
strcpy (bufptr, pfxbuf);
buffer[sizeof(buffer) - 1] = '\0';
return PyUnicode_FromString(buffer);
}
/* We dynamically bind to WSAAddressToStringW or WSAAddressToStringA
depending on which is available, as the latter is deprecated and
the former doesn't exist on all Windows versions on which this code
might run. */
typedef INT (WSAAPI *WSAAddressToStringWPtr)(LPSOCKADDR, DWORD, LPWSAPROTOCOL_INFOW, LPWSTR, LPDWORD);
typedef INT (WSAAPI *WSAAddressToStringAPtr)(LPSOCKADDR, DWORD, LPWSAPROTOCOL_INFOA, LPSTR, LPDWORD);
static WSAAddressToStringWPtr
get_address_to_string_w(void) {
static int ptr_is_set;
static WSAAddressToStringWPtr ptr;
if (!ptr_is_set) {
HMODULE hmod = LoadLibrary ("ws2_32.dll");
ptr = (WSAAddressToStringWPtr)GetProcAddress (hmod, "WSAAddressToStringW");
if (!ptr)
FreeLibrary (hmod);
ptr_is_set = 1;
}
return ptr;
}
static WSAAddressToStringAPtr
get_address_to_string_a(void) {
static int ptr_is_set;
static WSAAddressToStringAPtr ptr;
if (!ptr_is_set) {
HMODULE hmod = LoadLibrary ("ws2_32.dll");
ptr = (WSAAddressToStringAPtr)GetProcAddress (hmod, "WSAAddressToStringA");
if (!ptr)
FreeLibrary (hmod);
ptr_is_set = 1;
}
return ptr;
}
static PyObject *
string_from_address(SOCKADDR *addr, DWORD addrlen)
{
WSAAddressToStringWPtr AddressToStringW = get_address_to_string_w();
if (AddressToStringW) {
wchar_t buffer[256];
DWORD dwLen = sizeof(buffer) / sizeof(wchar_t);
INT iRet;
iRet = AddressToStringW (addr, addrlen, NULL, buffer, &dwLen);
if (iRet == 0)
return PyUnicode_FromWideChar (buffer, dwLen - 1);
} else {
char buffer[256];
DWORD dwLen = sizeof(buffer);
WSAAddressToStringAPtr AddressToStringA = get_address_to_string_a();
INT iRet;
iRet = AddressToStringA (addr, addrlen, NULL, buffer, &dwLen);
if (iRet == 0)
return PyUnicode_FromString (buffer);
}
return NULL;
}
#endif
static int
add_to_family (PyObject *result, int family, PyObject *obj)
{
PyObject *py_family;
PyObject *list;
if (!PyObject_Size (obj))
return TRUE;
py_family = PyInt_FromLong (family);
list = PyDict_GetItem (result, py_family);
if (!py_family) {
Py_DECREF (obj);
Py_XDECREF (list);
return FALSE;
}
if (!list) {
list = PyList_New (1);
if (!list) {
Py_DECREF (obj);
Py_DECREF (py_family);
return FALSE;
}
PyList_SET_ITEM (list, 0, obj);
PyDict_SetItem (result, py_family, list);
Py_DECREF (list);
} else {
PyList_Append (list, obj);
Py_DECREF (obj);
}
return TRUE;
}
/* -- ifaddresses() --------------------------------------------------------- */
static PyObject *
ifaddrs (PyObject *self, PyObject *args)
{
const char *ifname;
PyObject *result;
int found = FALSE;
#if defined(WIN32)
PIP_ADAPTER_ADDRESSES pAdapterAddresses = NULL, pInfo = NULL;
ULONG ulBufferLength = 0;
DWORD dwRet;
PIP_ADAPTER_UNICAST_ADDRESS pUniAddr;
#elif HAVE_GETIFADDRS
struct ifaddrs *addrs = NULL;
struct ifaddrs *addr = NULL;
#endif
if (!PyArg_ParseTuple (args, "s", &ifname))
return NULL;
result = PyDict_New ();
if (!result)
return NULL;
#if defined(WIN32)
/* .. Win32 ............................................................... */
/* First, retrieve the adapter information. We do this in a loop, in
case someone adds or removes adapters in the meantime. */
do {
dwRet = GetAdaptersAddresses (AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, NULL,
pAdapterAddresses, &ulBufferLength);
if (dwRet == ERROR_BUFFER_OVERFLOW) {
if (pAdapterAddresses)
free (pAdapterAddresses);
pAdapterAddresses = (PIP_ADAPTER_ADDRESSES)malloc (ulBufferLength);
if (!pAdapterAddresses) {
Py_DECREF (result);
PyErr_SetString (PyExc_MemoryError, "Not enough memory");
return NULL;
}
}
} while (dwRet == ERROR_BUFFER_OVERFLOW);
/* If we failed, then fail in Python too */
if (dwRet != ERROR_SUCCESS && dwRet != ERROR_NO_DATA) {
Py_DECREF (result);
if (pAdapterAddresses)
free (pAdapterAddresses);
PyErr_SetString (PyExc_OSError,
"Unable to obtain adapter information.");
return NULL;
}
for (pInfo = pAdapterAddresses; pInfo; pInfo = pInfo->Next) {
char buffer[256];
if (strcmp (pInfo->AdapterName, ifname) != 0)
continue;
found = TRUE;
/* Do the physical address */
if (256 >= 3 * pInfo->PhysicalAddressLength) {
PyObject *hwaddr, *dict;
char *ptr = buffer;
unsigned n;
*ptr = '\0';
for (n = 0; n < pInfo->PhysicalAddressLength; ++n) {
sprintf (ptr, "%02x:", pInfo->PhysicalAddress[n] & 0xff);
ptr += 3;
}
*--ptr = '\0';
hwaddr = PyUnicode_FromString (buffer);
dict = PyDict_New ();
if (!dict) {
Py_XDECREF (hwaddr);
Py_DECREF (result);
free (pAdapterAddresses);
return NULL;
}
PyDict_SetItemString (dict, "addr", hwaddr);
Py_DECREF (hwaddr);
if (!add_to_family (result, AF_LINK, dict)) {
Py_DECREF (result);
free (pAdapterAddresses);
return NULL;
}
}
for (pUniAddr = pInfo->FirstUnicastAddress;
pUniAddr;
pUniAddr = pUniAddr->Next) {
PyObject *addr;
PyObject *mask = NULL;
PyObject *bcast = NULL;
PIP_ADAPTER_PREFIX pPrefix;
short family = pUniAddr->Address.lpSockaddr->sa_family;
addr = string_from_address (pUniAddr->Address.lpSockaddr,
pUniAddr->Address.iSockaddrLength);
if (!addr)
continue;
/* Find the netmask, where possible */
if (family == AF_INET) {
struct sockaddr_in *pAddr
= (struct sockaddr_in *)pUniAddr->Address.lpSockaddr;
int prefix_len = -1;
struct sockaddr_in maskAddr, bcastAddr;
unsigned toDo;
unsigned wholeBytes, remainingBits;
unsigned char *pMaskBits, *pBcastBits;
PIP_ADAPTER_PREFIX pBest = NULL;
for (pPrefix = pInfo->FirstPrefix;
pPrefix;
pPrefix = pPrefix->Next) {
struct sockaddr_in *pPrefixAddr
= (struct sockaddr_in *)pPrefix->Address.lpSockaddr;
if (pPrefixAddr->sin_family != AF_INET
|| (prefix_len >= 0
&& pPrefix->PrefixLength < (unsigned)prefix_len)
|| (prefix_len >= 0 && pPrefix->PrefixLength == 32))
continue;
if (compare_bits (&pPrefixAddr->sin_addr,
&pAddr->sin_addr,
pPrefix->PrefixLength) == 0) {
prefix_len = pPrefix->PrefixLength;
pBest = pPrefix;
}
}
if (!pBest)
continue;
if (prefix_len < 0)
prefix_len = 32;
memcpy (&maskAddr,
pBest->Address.lpSockaddr,
sizeof (maskAddr));
memcpy (&bcastAddr,
pBest->Address.lpSockaddr,
sizeof (bcastAddr));
wholeBytes = prefix_len >> 3;
remainingBits = prefix_len & 7;
toDo = wholeBytes;
pMaskBits = (unsigned char *)&maskAddr.sin_addr;
while (toDo--)
*pMaskBits++ = 0xff;
toDo = 4 - wholeBytes;
pBcastBits = (unsigned char *)&bcastAddr.sin_addr + wholeBytes;
if (remainingBits) {
static const unsigned char masks[] = {
0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe
};
*pMaskBits++ = masks[remainingBits];
*pBcastBits &= masks[remainingBits];
*pBcastBits++ |= ~masks[remainingBits];
--toDo;
}
while (toDo--) {
*pMaskBits++ = 0;
*pBcastBits++ = 0xff;
}
mask = string_from_address ((SOCKADDR *)&maskAddr,
sizeof (maskAddr));
bcast = string_from_address ((SOCKADDR *)&bcastAddr,
sizeof (bcastAddr));
} else if (family == AF_INET6) {
struct sockaddr_in6 *pAddr
= (struct sockaddr_in6 *)pUniAddr->Address.lpSockaddr;
int prefix_len = -1;
struct sockaddr_in6 bcastAddr;
unsigned toDo;
unsigned wholeBytes, remainingBits;
unsigned char *pBcastBits;
PIP_ADAPTER_PREFIX pBest = NULL;
for (pPrefix = pInfo->FirstPrefix;
pPrefix;
pPrefix = pPrefix->Next) {
struct sockaddr_in6 *pPrefixAddr
= (struct sockaddr_in6 *)pPrefix->Address.lpSockaddr;
if (pPrefixAddr->sin6_family != AF_INET6
|| (prefix_len >= 0
&& pPrefix->PrefixLength < (unsigned)prefix_len)
|| (prefix_len >= 0 && pPrefix->PrefixLength == 128))
continue;
if (compare_bits (&pPrefixAddr->sin6_addr,
&pAddr->sin6_addr,
pPrefix->PrefixLength) == 0) {
prefix_len = pPrefix->PrefixLength;
pBest = pPrefix;
}
}
if (!pBest)
continue;
if (prefix_len < 0)
prefix_len = 128;
memcpy (&bcastAddr,
pBest->Address.lpSockaddr,
sizeof (bcastAddr));
wholeBytes = prefix_len >> 3;
remainingBits = prefix_len & 7;
toDo = 16 - wholeBytes;
pBcastBits = (unsigned char *)&bcastAddr.sin6_addr + wholeBytes;
if (remainingBits) {
static const unsigned char masks[] = {
0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe
};
*pBcastBits &= masks[remainingBits];
*pBcastBits++ |= ~masks[remainingBits];
--toDo;
}
while (toDo--)
*pBcastBits++ = 0xff;
mask = netmask_from_prefix (prefix_len);
bcast = string_from_address ((SOCKADDR *)&bcastAddr, sizeof(bcastAddr));
}
{
PyObject *dict;
dict = PyDict_New ();
if (!dict) {
Py_XDECREF (addr);
Py_XDECREF (mask);
Py_XDECREF (bcast);
Py_DECREF (result);
free (pAdapterAddresses);
return NULL;
}
if (addr)
PyDict_SetItemString (dict, "addr", addr);
if (mask)
PyDict_SetItemString (dict, "netmask", mask);
if (bcast)
PyDict_SetItemString (dict, "broadcast", bcast);
Py_XDECREF (addr);