2 * Copyright (c) 2000 - 2002 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 RCSID("$Id: getifaddrs.c,v 1.11 2005/04/30 15:45:47 lha Exp $");
49 #ifdef HAVE_SYS_SOCKIO_H
50 #include <sys/sockio.h>
51 #endif /* HAVE_SYS_SOCKIO_H */
53 #ifdef HAVE_NETINET_IN6_VAR_H
54 #include <netinet/in6_var.h>
55 #endif /* HAVE_NETINET_IN6_VAR_H */
62 * The linux - AF_NETLINK version of getifaddrs - from Usagi.
63 * Linux does not return v6 addresses from SIOCGIFCONF.
66 /* $USAGI: ifaddrs.c,v 1.18 2002/03/06 01:50:46 yoshfuji Exp $ */
68 /**************************************************************************
70 * Copyright (C)2000 Hideaki YOSHIFUJI, All Rights Reserved.
72 * Redistribution and use in source and binary forms, with or without
73 * modification, are permitted provided that the following conditions
75 * 1. Redistributions of source code must retain the above copyright
76 * notice, this list of conditions and the following disclaimer.
77 * 2. Redistributions in binary form must reproduce the above copyright
78 * notice, this list of conditions and the following disclaimer in the
79 * documentation and/or other materials provided with the distribution.
80 * 3. Neither the name of the author nor the names of its contributors
81 * may be used to endorse or promote products derived from this software
82 * without specific prior written permission.
84 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
85 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
86 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
87 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
88 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
89 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
90 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
91 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
92 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
93 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
105 #include <sys/socket.h>
106 #include <asm/types.h>
107 #include <linux/netlink.h>
108 #include <linux/rtnetlink.h>
109 #include <sys/types.h>
110 #include <sys/socket.h>
111 #include <netpacket/packet.h>
112 #include <net/ethernet.h> /* the L2 protocols */
115 #include <net/if_arp.h>
117 #include <netinet/in.h>
119 #define __set_errno(e) (errno = (e))
120 #define __close(fd) (close(fd))
122 #define ifa_broadaddr ifa_dstaddr
125 /* ====================================================================== */
127 struct nlmsg_list *nlm_next;
128 struct nlmsghdr *nlh;
133 struct rtmaddr_ifamap {
140 #ifdef HAVE_IFADDRS_IFA_ANYCAST
149 #ifdef HAVE_IFADDRS_IFA_ANYCAST
154 /* ====================================================================== */
156 ifa_sa_len(sa_family_t family, int len)
161 size = sizeof(struct sockaddr_in);
164 size = sizeof(struct sockaddr_in6);
167 size = (size_t)(((struct sockaddr_ll *)NULL)->sll_addr) + len;
168 if (size < sizeof(struct sockaddr_ll))
169 size = sizeof(struct sockaddr_ll);
172 size = (size_t)(((struct sockaddr *)NULL)->sa_data) + len;
173 if (size < sizeof(struct sockaddr))
174 size = sizeof(struct sockaddr);
181 ifa_make_sockaddr(sa_family_t family,
184 uint32_t scope, uint32_t scopeid)
186 if (sa == NULL) return;
189 memcpy(&((struct sockaddr_in*)sa)->sin_addr, (char *)p, len);
192 memcpy(&((struct sockaddr_in6*)sa)->sin6_addr, (char *)p, len);
193 if (IN6_IS_ADDR_LINKLOCAL(p) ||
194 IN6_IS_ADDR_MC_LINKLOCAL(p)){
195 ((struct sockaddr_in6*)sa)->sin6_scope_id = scopeid;
199 memcpy(((struct sockaddr_ll*)sa)->sll_addr, (char *)p, len);
200 ((struct sockaddr_ll*)sa)->sll_halen = len;
203 memcpy(sa->sa_data, p, len); /*XXX*/
206 sa->sa_family = family;
207 #ifdef HAVE_SOCKADDR_SA_LEN
208 sa->sa_len = ifa_sa_len(family, len);
213 static struct sockaddr *
214 ifa_make_sockaddr_mask(sa_family_t family,
220 uint32_t max_prefixlen = 0;
222 if (sa == NULL) return NULL;
225 memset(&((struct sockaddr_in*)sa)->sin_addr, 0, sizeof(((struct sockaddr_in*)sa)->sin_addr));
226 p = (char *)&((struct sockaddr_in*)sa)->sin_addr;
230 memset(&((struct sockaddr_in6*)sa)->sin6_addr, 0, sizeof(((struct sockaddr_in6*)sa)->sin6_addr));
231 p = (char *)&((struct sockaddr_in6*)sa)->sin6_addr;
232 #if 0 /* XXX: fill scope-id? */
233 if (IN6_IS_ADDR_LINKLOCAL(p) ||
234 IN6_IS_ADDR_MC_LINKLOCAL(p)){
235 ((struct sockaddr_in6*)sa)->sin6_scope_id = scopeid;
243 sa->sa_family = family;
244 #ifdef HAVE_SOCKADDR_SA_LEN
245 sa->sa_len = ifa_sa_len(family, len);
248 if (prefixlen > max_prefixlen)
249 prefixlen = max_prefixlen;
250 for (i=0; i<(prefixlen / 8); i++)
253 c <<= (8 - (prefixlen % 8));
260 /* ====================================================================== */
262 nl_sendreq(int sd, int request, int flags, int *seq)
264 char reqbuf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
265 NLMSG_ALIGN(sizeof(struct rtgenmsg))];
266 struct sockaddr_nl nladdr;
267 struct nlmsghdr *req_hdr;
268 struct rtgenmsg *req_msg;
269 time_t t = time(NULL);
272 memset(&reqbuf, 0, sizeof(reqbuf));
273 req_hdr = (struct nlmsghdr *)reqbuf;
274 req_msg = (struct rtgenmsg *)NLMSG_DATA(req_hdr);
275 req_hdr->nlmsg_len = NLMSG_LENGTH(sizeof(*req_msg));
276 req_hdr->nlmsg_type = request;
277 req_hdr->nlmsg_flags = flags | NLM_F_REQUEST;
278 req_hdr->nlmsg_pid = 0;
279 req_hdr->nlmsg_seq = t;
280 req_msg->rtgen_family = AF_UNSPEC;
281 memset(&nladdr, 0, sizeof(nladdr));
282 nladdr.nl_family = AF_NETLINK;
283 return (sendto(sd, (void *)req_hdr, req_hdr->nlmsg_len, 0,
284 (struct sockaddr *)&nladdr, sizeof(nladdr)));
288 nl_recvmsg(int sd, int request, int seq,
289 void *buf, size_t buflen,
293 struct iovec iov = { buf, buflen };
294 struct sockaddr_nl nladdr;
298 msg.msg_name = (void *)&nladdr;
299 msg.msg_namelen = sizeof(nladdr);
302 msg.msg_control = NULL;
303 msg.msg_controllen = 0;
305 read_len = recvmsg(sd, &msg, 0);
306 if ((read_len < 0 && errno == EINTR) || (msg.msg_flags & MSG_TRUNC))
308 if (flags) *flags = msg.msg_flags;
315 nl_getmsg(int sd, int request, int seq,
316 struct nlmsghdr **nlhp,
320 size_t bufsize = 65536, lastbufsize = 0;
322 int result = 0, read_size;
324 pid_t pid = getpid();
326 void *newbuff = realloc(buff, bufsize);
327 if (newbuff == NULL || bufsize < lastbufsize) {
332 result = read_size = nl_recvmsg(sd, request, seq, buff, bufsize, &msg_flags);
333 if (read_size < 0 || (msg_flags & MSG_TRUNC)){
334 lastbufsize = bufsize;
338 if (read_size == 0) break;
339 nh = (struct nlmsghdr *)buff;
340 for (nh = (struct nlmsghdr *)buff;
341 NLMSG_OK(nh, read_size);
342 nh = (struct nlmsghdr *)NLMSG_NEXT(nh, read_size)){
343 if (nh->nlmsg_pid != pid ||
344 nh->nlmsg_seq != seq)
346 if (nh->nlmsg_type == NLMSG_DONE){
350 if (nh->nlmsg_type == NLMSG_ERROR){
351 struct nlmsgerr *nlerr = (struct nlmsgerr *)NLMSG_DATA(nh);
353 if (nh->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr)))
356 __set_errno(-nlerr->error);
364 int saved_errno = errno;
366 __set_errno(saved_errno);
368 *nlhp = (struct nlmsghdr *)buff;
373 nl_getlist(int sd, int seq,
375 struct nlmsg_list **nlm_list,
376 struct nlmsg_list **nlm_end)
378 struct nlmsghdr *nlh = NULL;
382 status = nl_sendreq(sd, request, NLM_F_ROOT|NLM_F_MATCH, &seq);
386 seq = (int)time(NULL);
388 status = nl_getmsg(sd, request, seq, &nlh, &done);
392 struct nlmsg_list *nlm_next = (struct nlmsg_list *)malloc(sizeof(struct nlmsg_list));
393 if (nlm_next == NULL){
394 int saved_errno = errno;
396 __set_errno(saved_errno);
399 nlm_next->nlm_next = NULL;
400 nlm_next->nlh = (struct nlmsghdr *)nlh;
401 nlm_next->size = status;
403 if (*nlm_list == NULL){
404 *nlm_list = nlm_next;
407 (*nlm_end)->nlm_next = nlm_next;
413 return status >= 0 ? seq : status;
416 /* ---------------------------------------------------------------------- */
418 free_nlmsglist(struct nlmsg_list *nlm0)
420 struct nlmsg_list *nlm;
425 for (nlm=nlm0; nlm; nlm=nlm->nlm_next){
430 __set_errno(saved_errno);
434 free_data(void *data, void *ifdata)
436 int saved_errno = errno;
437 if (data != NULL) free(data);
438 if (ifdata != NULL) free(ifdata);
439 __set_errno(saved_errno);
442 /* ---------------------------------------------------------------------- */
446 int saved_errno = errno;
447 if (sd >= 0) __close(sd);
448 __set_errno(saved_errno);
451 /* ---------------------------------------------------------------------- */
455 struct sockaddr_nl nladdr;
458 sd = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
459 if (sd < 0) return -1;
460 memset(&nladdr, 0, sizeof(nladdr));
461 nladdr.nl_family = AF_NETLINK;
462 if (bind(sd, (struct sockaddr*)&nladdr, sizeof(nladdr)) < 0){
469 /* ====================================================================== */
470 int ROKEN_LIB_FUNCTION
471 getifaddrs(struct ifaddrs **ifap)
474 struct nlmsg_list *nlmsg_list, *nlmsg_end, *nlm;
475 /* - - - - - - - - - - - - - - - */
477 size_t dlen, xlen, nlen;
478 uint32_t max_ifindex = 0;
480 pid_t pid = getpid();
483 int build ; /* 0 or 1 */
485 /* ---------------------------------- */
487 icnt = dlen = xlen = nlen = 0;
488 nlmsg_list = nlmsg_end = NULL;
493 /* ---------------------------------- */
494 /* open socket and bind */
499 /* ---------------------------------- */
501 if ((seq = nl_getlist(sd, 0, RTM_GETLINK,
502 &nlmsg_list, &nlmsg_end)) < 0){
503 free_nlmsglist(nlmsg_list);
507 if ((seq = nl_getlist(sd, seq+1, RTM_GETADDR,
508 &nlmsg_list, &nlmsg_end)) < 0){
509 free_nlmsglist(nlmsg_list);
514 /* ---------------------------------- */
515 /* Estimate size of result buffer and fill it */
516 for (build=0; build<=1; build++){
517 struct ifaddrs *ifl = NULL, *ifa = NULL;
518 struct nlmsghdr *nlh, *nlh0;
519 char *data = NULL, *xdata = NULL;
521 char *ifname = NULL, **iflist = NULL;
522 uint16_t *ifflist = NULL;
523 struct rtmaddr_ifamap ifamap;
527 NLMSG_ALIGN(sizeof(struct ifaddrs[icnt]))
528 + dlen + xlen + nlen);
529 ifa = (struct ifaddrs *)data;
531 NLMSG_ALIGN(sizeof(char *[max_ifindex+1]))
532 + NLMSG_ALIGN(sizeof(uint16_t [max_ifindex+1])));
534 *ifap = (ifdata != NULL) ? ifa : NULL;
536 free_data(data, ifdata);
540 if (data == NULL || ifdata == NULL){
541 free_data(data, ifdata);
546 data += NLMSG_ALIGN(sizeof(struct ifaddrs)) * icnt;
548 ifname = xdata + xlen;
550 ifflist = (uint16_t *)(((char *)iflist) + NLMSG_ALIGN(sizeof(char *[max_ifindex+1])));
553 for (nlm=nlmsg_list; nlm; nlm=nlm->nlm_next){
554 int nlmlen = nlm->size;
555 if (!(nlh0 = nlm->nlh))
558 NLMSG_OK(nlh, nlmlen);
559 nlh=NLMSG_NEXT(nlh,nlmlen)){
560 struct ifinfomsg *ifim = NULL;
561 struct ifaddrmsg *ifam = NULL;
564 size_t nlm_struct_size = 0;
565 sa_family_t nlm_family = 0;
566 uint32_t nlm_scope = 0, nlm_index = 0;
567 size_t sockaddr_size = 0;
568 uint32_t nlm_prefixlen = 0;
571 memset(&ifamap, 0, sizeof(ifamap));
573 /* check if the message is what we want */
574 if (nlh->nlmsg_pid != pid ||
575 nlh->nlmsg_seq != nlm->seq)
577 if (nlh->nlmsg_type == NLMSG_DONE){
580 switch (nlh->nlmsg_type){
582 ifim = (struct ifinfomsg *)NLMSG_DATA(nlh);
583 nlm_struct_size = sizeof(*ifim);
584 nlm_family = ifim->ifi_family;
586 nlm_index = ifim->ifi_index;
589 ifflist[nlm_index] = ifa->ifa_flags = ifim->ifi_flags;
592 ifam = (struct ifaddrmsg *)NLMSG_DATA(nlh);
593 nlm_struct_size = sizeof(*ifam);
594 nlm_family = ifam->ifa_family;
595 nlm_scope = ifam->ifa_scope;
596 nlm_index = ifam->ifa_index;
597 nlm_prefixlen = ifam->ifa_prefixlen;
599 ifa->ifa_flags = ifflist[nlm_index];
606 if (max_ifindex < nlm_index)
607 max_ifindex = nlm_index;
613 rtasize = NLMSG_PAYLOAD(nlh, nlmlen) - NLMSG_ALIGN(nlm_struct_size);
614 for (rta = (struct rtattr *)(((char *)NLMSG_DATA(nlh)) + NLMSG_ALIGN(nlm_struct_size));
615 RTA_OK(rta, rtasize);
616 rta = RTA_NEXT(rta, rtasize)){
617 struct sockaddr **sap = NULL;
618 void *rtadata = RTA_DATA(rta);
619 size_t rtapayload = RTA_PAYLOAD(rta);
622 switch(nlh->nlmsg_type){
624 switch(rta->rta_type){
628 sap = (rta->rta_type == IFLA_ADDRESS) ? &ifa->ifa_addr : &ifa->ifa_broadaddr;
629 *sap = (struct sockaddr *)data;
631 sa_len = ifa_sa_len(AF_PACKET, rtapayload);
632 if (rta->rta_type == IFLA_ADDRESS)
633 sockaddr_size = NLMSG_ALIGN(sa_len);
635 dlen += NLMSG_ALIGN(sa_len);
637 memset(*sap, 0, sa_len);
638 ifa_make_sockaddr(AF_PACKET, *sap, rtadata,rtapayload, 0,0);
639 ((struct sockaddr_ll *)*sap)->sll_ifindex = nlm_index;
640 ((struct sockaddr_ll *)*sap)->sll_hatype = ifim->ifi_type;
641 data += NLMSG_ALIGN(sa_len);
644 case IFLA_IFNAME:/* Name of Interface */
646 nlen += NLMSG_ALIGN(rtapayload + 1);
648 ifa->ifa_name = ifname;
649 if (iflist[nlm_index] == NULL)
650 iflist[nlm_index] = ifa->ifa_name;
651 strncpy(ifa->ifa_name, rtadata, rtapayload);
652 ifa->ifa_name[rtapayload] = '\0';
653 ifname += NLMSG_ALIGN(rtapayload + 1);
656 case IFLA_STATS:/* Statistics of Interface */
658 xlen += NLMSG_ALIGN(rtapayload);
660 ifa->ifa_data = xdata;
661 memcpy(ifa->ifa_data, rtadata, rtapayload);
662 xdata += NLMSG_ALIGN(rtapayload);
678 if (nlm_family == AF_PACKET) break;
679 switch(rta->rta_type){
681 ifamap.address = rtadata;
682 ifamap.address_len = rtapayload;
685 ifamap.local = rtadata;
686 ifamap.local_len = rtapayload;
689 ifamap.broadcast = rtadata;
690 ifamap.broadcast_len = rtapayload;
692 #ifdef HAVE_IFADDRS_IFA_ANYCAST
694 ifamap.anycast = rtadata;
695 ifamap.anycast_len = rtapayload;
700 nlen += NLMSG_ALIGN(rtapayload + 1);
702 ifa->ifa_name = ifname;
703 if (iflist[nlm_index] == NULL)
704 iflist[nlm_index] = ifname;
705 strncpy(ifa->ifa_name, rtadata, rtapayload);
706 ifa->ifa_name[rtapayload] = '\0';
707 ifname += NLMSG_ALIGN(rtapayload + 1);
719 if (nlh->nlmsg_type == RTM_NEWADDR &&
720 nlm_family != AF_PACKET) {
722 ifamap.local = ifamap.address;
723 ifamap.local_len = ifamap.address_len;
725 if (!ifamap.address) {
726 ifamap.address = ifamap.local;
727 ifamap.address_len = ifamap.local_len;
729 if (ifamap.address_len != ifamap.local_len ||
730 (ifamap.address != NULL &&
731 memcmp(ifamap.address, ifamap.local, ifamap.address_len))) {
732 /* p2p; address is peer and local is ours */
733 ifamap.broadcast = ifamap.address;
734 ifamap.broadcast_len = ifamap.address_len;
735 ifamap.address = ifamap.local;
736 ifamap.address_len = ifamap.local_len;
738 if (ifamap.address) {
740 sockaddr_size = NLMSG_ALIGN(ifa_sa_len(nlm_family,ifamap.address_len));
743 dlen += NLMSG_ALIGN(ifa_sa_len(nlm_family,ifamap.address_len));
745 ifa->ifa_addr = (struct sockaddr *)data;
746 ifa_make_sockaddr(nlm_family, ifa->ifa_addr, ifamap.address, ifamap.address_len,
747 nlm_scope, nlm_index);
748 data += NLMSG_ALIGN(ifa_sa_len(nlm_family, ifamap.address_len));
752 if (ifamap.netmask) {
754 dlen += NLMSG_ALIGN(ifa_sa_len(nlm_family,ifamap.netmask_len));
756 ifa->ifa_netmask = (struct sockaddr *)data;
757 ifa_make_sockaddr(nlm_family, ifa->ifa_netmask, ifamap.netmask, ifamap.netmask_len,
758 nlm_scope, nlm_index);
759 data += NLMSG_ALIGN(ifa_sa_len(nlm_family, ifamap.netmask_len));
763 if (ifamap.broadcast) {
765 dlen += NLMSG_ALIGN(ifa_sa_len(nlm_family,ifamap.broadcast_len));
767 ifa->ifa_broadaddr = (struct sockaddr *)data;
768 ifa_make_sockaddr(nlm_family, ifa->ifa_broadaddr, ifamap.broadcast, ifamap.broadcast_len,
769 nlm_scope, nlm_index);
770 data += NLMSG_ALIGN(ifa_sa_len(nlm_family, ifamap.broadcast_len));
773 #ifdef HAVE_IFADDRS_IFA_ANYCAST
774 if (ifamap.anycast) {
776 dlen += NLMSG_ALIGN(ifa_sa_len(nlm_family,ifamap.anycast_len));
778 ifa->ifa_anycast = (struct sockaddr *)data;
779 ifa_make_sockaddr(nlm_family, ifa->ifa_anyaddr, ifamap.anycast, ifamap.anycast_len,
780 nlm_scope, nlm_index);
781 data += NLMSG_ALIGN(ifa_sa_len(nlm_family, ifamap.anycast_len));
788 dlen += sockaddr_size;
792 if (ifa->ifa_name == NULL)
793 ifa->ifa_name = iflist[nlm_index];
796 ifa->ifa_addr->sa_family != AF_UNSPEC &&
797 ifa->ifa_addr->sa_family != AF_PACKET){
798 ifa->ifa_netmask = (struct sockaddr *)data;
799 ifa_make_sockaddr_mask(ifa->ifa_addr->sa_family, ifa->ifa_netmask, nlm_prefixlen);
801 data += sockaddr_size;
808 if (icnt == 0 && (dlen + nlen + xlen == 0)){
811 break; /* cannot found any addresses */
815 free_data(NULL, ifdata);
818 /* ---------------------------------- */
820 free_nlmsglist(nlmsg_list);
825 /* ---------------------------------------------------------------------- */
826 void ROKEN_LIB_FUNCTION
827 freeifaddrs(struct ifaddrs *ifa)
833 #else /* !AF_NETLINK */
836 * The generic SIOCGIFCONF version.
840 getifaddrs2(struct ifaddrs **ifap,
841 int af, int siocgifconf, int siocgifflags,
848 struct ifconf ifconf;
851 struct sockaddr sa_zero;
853 struct ifaddrs *start = NULL, **end = &start;
857 memset (&sa_zero, 0, sizeof(sa_zero));
858 fd = socket(af, SOCK_DGRAM, 0);
864 buf = calloc(1, buf_size);
869 ifconf.ifc_len = buf_size;
870 ifconf.ifc_buf = buf;
873 * Solaris returns EINVAL when the buffer is too small.
875 if (ioctl (fd, siocgifconf, &ifconf) < 0 && errno != EINVAL) {
880 * Can the difference between a full and a overfull buf
884 if (ifconf.ifc_len < buf_size)
890 for (p = ifconf.ifc_buf;
891 p < ifconf.ifc_buf + ifconf.ifc_len;
897 ifr = (struct ifreq *)p;
901 salen = sizeof(struct sockaddr);
902 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
904 sz = max(sz, sizeof(ifr->ifr_name) + sa->sa_len);
908 sz = max(sz, sizeof(ifr->ifr_name) + SA_LEN(sa));
910 memset (&ifreq, 0, sizeof(ifreq));
911 memcpy (ifreq.ifr_name, ifr->ifr_name, sizeof(ifr->ifr_name));
913 if (ioctl(fd, siocgifflags, &ifreq) < 0) {
918 *end = malloc(sizeof(**end));
924 (*end)->ifa_next = NULL;
925 (*end)->ifa_name = strdup(ifr->ifr_name);
926 (*end)->ifa_flags = ifreq.ifr_flags;
927 (*end)->ifa_addr = malloc(salen);
928 memcpy((*end)->ifa_addr, sa, salen);
929 (*end)->ifa_netmask = NULL;
932 /* fix these when we actually need them */
933 if(ifreq.ifr_flags & IFF_BROADCAST) {
934 (*end)->ifa_broadaddr = malloc(sizeof(ifr->ifr_broadaddr));
935 memcpy((*end)->ifa_broadaddr, &ifr->ifr_broadaddr,
936 sizeof(ifr->ifr_broadaddr));
937 } else if(ifreq.ifr_flags & IFF_POINTOPOINT) {
938 (*end)->ifa_dstaddr = malloc(sizeof(ifr->ifr_dstaddr));
939 memcpy((*end)->ifa_dstaddr, &ifr->ifr_dstaddr,
940 sizeof(ifr->ifr_dstaddr));
942 (*end)->ifa_dstaddr = NULL;
944 (*end)->ifa_dstaddr = NULL;
947 (*end)->ifa_data = NULL;
949 end = &(*end)->ifa_next;
964 #if defined(HAVE_IPV6) && defined(SIOCGLIFCONF) && defined(SIOCGLIFFLAGS)
966 getlifaddrs2(struct ifaddrs **ifap,
967 int af, int siocgifconf, int siocgifflags,
974 struct lifconf ifconf;
977 struct sockaddr sa_zero;
979 struct ifaddrs *start = NULL, **end = &start;
983 memset (&sa_zero, 0, sizeof(sa_zero));
984 fd = socket(af, SOCK_DGRAM, 0);
990 buf = calloc(1, buf_size);
995 ifconf.lifc_family = AF_UNSPEC;
996 ifconf.lifc_flags = 0;
997 ifconf.lifc_len = buf_size;
998 ifconf.lifc_buf = buf;
1001 * Solaris returns EINVAL when the buffer is too small.
1003 if (ioctl (fd, siocgifconf, &ifconf) < 0 && errno != EINVAL) {
1008 * Can the difference between a full and a overfull buf
1012 if (ifconf.lifc_len < buf_size)
1018 for (p = ifconf.lifc_buf;
1019 p < ifconf.lifc_buf + ifconf.lifc_len;
1021 struct lifreq ifreq;
1022 struct sockaddr_storage *sa;
1025 ifr = (struct lifreq *)p;
1026 sa = &ifr->lifr_addr;
1029 salen = sizeof(struct sockaddr_storage);
1030 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
1032 sz = max(sz, sizeof(ifr->ifr_name) + sa->sa_len);
1036 sz = max(sz, sizeof(ifr->ifr_name) + SA_LEN(sa));
1038 memset (&ifreq, 0, sizeof(ifreq));
1039 memcpy (ifreq.lifr_name, ifr->lifr_name, sizeof(ifr->lifr_name));
1041 if (ioctl(fd, siocgifflags, &ifreq) < 0) {
1046 *end = malloc(sizeof(**end));
1048 (*end)->ifa_next = NULL;
1049 (*end)->ifa_name = strdup(ifr->lifr_name);
1050 (*end)->ifa_flags = ifreq.lifr_flags;
1051 (*end)->ifa_addr = malloc(salen);
1052 memcpy((*end)->ifa_addr, sa, salen);
1053 (*end)->ifa_netmask = NULL;
1056 /* fix these when we actually need them */
1057 if(ifreq.ifr_flags & IFF_BROADCAST) {
1058 (*end)->ifa_broadaddr = malloc(sizeof(ifr->ifr_broadaddr));
1059 memcpy((*end)->ifa_broadaddr, &ifr->ifr_broadaddr,
1060 sizeof(ifr->ifr_broadaddr));
1061 } else if(ifreq.ifr_flags & IFF_POINTOPOINT) {
1062 (*end)->ifa_dstaddr = malloc(sizeof(ifr->ifr_dstaddr));
1063 memcpy((*end)->ifa_dstaddr, &ifr->ifr_dstaddr,
1064 sizeof(ifr->ifr_dstaddr));
1066 (*end)->ifa_dstaddr = NULL;
1068 (*end)->ifa_dstaddr = NULL;
1071 (*end)->ifa_data = NULL;
1073 end = &(*end)->ifa_next;
1087 #endif /* defined(HAVE_IPV6) && defined(SIOCGLIFCONF) && defined(SIOCGLIFFLAGS) */
1089 int ROKEN_LIB_FUNCTION
1090 getifaddrs(struct ifaddrs **ifap)
1094 #if defined(AF_INET6) && defined(SIOCGIF6CONF) && defined(SIOCGIF6FLAGS)
1096 ret = getifaddrs2 (ifap, AF_INET6, SIOCGIF6CONF, SIOCGIF6FLAGS,
1097 sizeof(struct in6_ifreq));
1099 #if defined(HAVE_IPV6) && defined(SIOCGLIFCONF) && defined(SIOCGLIFFLAGS)
1101 ret = getlifaddrs2 (ifap, AF_INET6, SIOCGLIFCONF, SIOCGLIFFLAGS,
1102 sizeof(struct lifreq));
1104 #if defined(HAVE_IPV6) && defined(SIOCGIFCONF)
1106 ret = getifaddrs2 (ifap, AF_INET6, SIOCGIFCONF, SIOCGIFFLAGS,
1107 sizeof(struct ifreq));
1109 #if defined(AF_INET) && defined(SIOCGIFCONF) && defined(SIOCGIFFLAGS)
1111 ret = getifaddrs2 (ifap, AF_INET, SIOCGIFCONF, SIOCGIFFLAGS,
1112 sizeof(struct ifreq));
1117 void ROKEN_LIB_FUNCTION
1118 freeifaddrs(struct ifaddrs *ifp)
1120 struct ifaddrs *p, *q;
1127 free(p->ifa_dstaddr);
1129 free(p->ifa_netmask);
1138 #endif /* !AF_NETLINK */
1143 print_addr(const char *s, struct sockaddr *sa)
1146 printf(" %s=%d/", s, sa->sa_family);
1147 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
1148 for(i = 0; i < sa->sa_len - ((long)sa->sa_data - (long)&sa->sa_family); i++)
1149 printf("%02x", ((unsigned char*)sa->sa_data)[i]);
1151 for(i = 0; i < sizeof(sa->sa_data); i++)
1152 printf("%02x", ((unsigned char*)sa->sa_data)[i]);
1158 print_ifaddrs(struct ifaddrs *x)
1162 for(p = x; p; p = p->ifa_next) {
1163 printf("%s\n", p->ifa_name);
1164 printf(" flags=%x\n", p->ifa_flags);
1166 print_addr("addr", p->ifa_addr);
1168 print_addr("dstaddr", p->ifa_dstaddr);
1170 print_addr("netmask", p->ifa_netmask);
1171 printf(" %p\n", p->ifa_data);
1178 struct ifaddrs *a = NULL, *b;
1179 getifaddrs2(&a, AF_INET, SIOCGIFCONF, SIOCGIFFLAGS, sizeof(struct ifreq));