5 #include <sys/socket.h>
7 #include <netinet/in.h>
8 #ifndef SIOCGIFCONF /* whatever works */
9 #include <sys/sockio.h>
18 static int ipmeok
= 0;
22 struct ip_address
*ip
;
25 if (ipme_init() != 1) return -1;
26 for (i
= 0;i
< ipme
.len
;++i
)
27 if (byte_equal(&ipme
.ix
[i
].ip
,4,ip
))
32 static stralloc buf
= {0};
39 struct sockaddr_in
*sin
;
45 if (!ipalloc_readyplus(&ipme
,0)) return 0;
49 if ((s
= socket(AF_INET
,SOCK_STREAM
,0)) == -1) return -1;
53 if (!stralloc_ready(&buf
,len
)) { close(s
); return 0; }
57 if (ioctl(s
,SIOCGIFCONF
,&ifc
) >= 0) /* > is for System V */
58 if (ifc
.ifc_len
+ sizeof(*ifr
) + 64 < len
) { /* what a stupid interface */
59 buf
.len
= ifc
.ifc_len
;
62 if (len
> 200000) { close(s
); return -1; }
63 len
+= 100 + (len
>> 2);
66 while (x
< buf
.s
+ buf
.len
) {
67 ifr
= (struct ifreq
*) x
;
69 len
= sizeof(ifr
->ifr_name
) + ifr
->ifr_addr
.sa_len
;
70 if (len
< sizeof(*ifr
))
72 if (ifr
->ifr_addr
.sa_family
== AF_INET
) {
73 sin
= (struct sockaddr_in
*) &ifr
->ifr_addr
;
74 byte_copy(&ix
.ip
,4,&sin
->sin_addr
);
75 if (ioctl(s
,SIOCGIFFLAGS
,x
) == 0)
76 if (ifr
->ifr_flags
& IFF_UP
)
77 if (!ipalloc_append(&ipme
,&ix
)) { close(s
); return 0; }
81 if (ioctl(s
,SIOCGIFFLAGS
,x
) == 0)
82 if (ifr
->ifr_flags
& IFF_UP
)
83 if (ioctl(s
,SIOCGIFADDR
,x
) == 0)
84 if (ifr
->ifr_addr
.sa_family
== AF_INET
) {
85 sin
= (struct sockaddr_in
*) &ifr
->ifr_addr
;
86 byte_copy(&ix
.ip
,4,&sin
->sin_addr
);
87 if (!ipalloc_append(&ipme
,&ix
)) { close(s
); return 0; }