2 * Copyright (c) 2000 - 2002, 2005 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 21745 2007-07-31 16:11:25Z lha $");
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 */
60 #define lifconf if_laddrconf
61 #define lifc_len iflc_len
62 #define lifc_buf iflc_buf
63 #define lifc_req iflc_req
65 #define lifreq if_laddrreq
66 #define lifr_addr iflr_addr
67 #define lifr_name iflr_name
68 #define lifr_dstaddr iflr_dstaddr
69 #define lifr_broadaddr iflr_broadaddr
70 #define lifr_flags iflr_flags
71 #define lifr_index iflr_index
77 * The linux - AF_NETLINK version of getifaddrs - from Usagi.
78 * Linux does not return v6 addresses from SIOCGIFCONF.
81 /* $USAGI: ifaddrs.c,v 1.18 2002/03/06 01:50:46 yoshfuji Exp $ */
83 /**************************************************************************
85 * Copyright (C)2000 Hideaki YOSHIFUJI, All Rights Reserved.
87 * Redistribution and use in source and binary forms, with or without
88 * modification, are permitted provided that the following conditions
90 * 1. Redistributions of source code must retain the above copyright
91 * notice, this list of conditions and the following disclaimer.
92 * 2. Redistributions in binary form must reproduce the above copyright
93 * notice, this list of conditions and the following disclaimer in the
94 * documentation and/or other materials provided with the distribution.
95 * 3. Neither the name of the author nor the names of its contributors
96 * may be used to endorse or promote products derived from this software
97 * without specific prior written permission.
99 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
100 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
101 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
102 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
103 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
104 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
105 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
106 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
107 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
108 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
120 #include <sys/socket.h>
121 #include <asm/types.h>
122 #include <linux/netlink.h>
123 #include <linux/rtnetlink.h>
124 #include <sys/types.h>
125 #include <sys/socket.h>
126 #include <sys/poll.h>
127 #include <netpacket/packet.h>
128 #include <net/ethernet.h> /* the L2 protocols */
131 #include <net/if_arp.h>
133 #include <netinet/in.h>
135 #define __set_errno(e) (errno = (e))
136 #define __close(fd) (close(fd))
138 #define ifa_broadaddr ifa_dstaddr
141 /* ====================================================================== */
143 struct nlmsg_list *nlm_next;
144 struct nlmsghdr *nlh;
149 struct rtmaddr_ifamap {
156 #ifdef HAVE_IFADDRS_IFA_ANYCAST
165 #ifdef HAVE_IFADDRS_IFA_ANYCAST
170 /* ====================================================================== */
172 ifa_sa_len(sa_family_t family, int len)
177 size = sizeof(struct sockaddr_in);
180 size = sizeof(struct sockaddr_in6);
183 size = (size_t)(((struct sockaddr_ll *)NULL)->sll_addr) + len;
184 if (size < sizeof(struct sockaddr_ll))
185 size = sizeof(struct sockaddr_ll);
188 size = (size_t)(((struct sockaddr *)NULL)->sa_data) + len;
189 if (size < sizeof(struct sockaddr))
190 size = sizeof(struct sockaddr);
197 ifa_make_sockaddr(sa_family_t family,
200 uint32_t scope, uint32_t scopeid)
202 if (sa == NULL) return;
205 memcpy(&((struct sockaddr_in*)sa)->sin_addr, (char *)p, len);
208 memcpy(&((struct sockaddr_in6*)sa)->sin6_addr, (char *)p, len);
209 if (IN6_IS_ADDR_LINKLOCAL(p) ||
210 IN6_IS_ADDR_MC_LINKLOCAL(p)){
211 ((struct sockaddr_in6*)sa)->sin6_scope_id = scopeid;
215 memcpy(((struct sockaddr_ll*)sa)->sll_addr, (char *)p, len);
216 ((struct sockaddr_ll*)sa)->sll_halen = len;
219 memcpy(sa->sa_data, p, len); /*XXX*/
222 sa->sa_family = family;
223 #ifdef HAVE_SOCKADDR_SA_LEN
224 sa->sa_len = ifa_sa_len(family, len);
229 static struct sockaddr *
230 ifa_make_sockaddr_mask(sa_family_t family,
236 uint32_t max_prefixlen = 0;
238 if (sa == NULL) return NULL;
241 memset(&((struct sockaddr_in*)sa)->sin_addr, 0, sizeof(((struct sockaddr_in*)sa)->sin_addr));
242 p = (char *)&((struct sockaddr_in*)sa)->sin_addr;
246 memset(&((struct sockaddr_in6*)sa)->sin6_addr, 0, sizeof(((struct sockaddr_in6*)sa)->sin6_addr));
247 p = (char *)&((struct sockaddr_in6*)sa)->sin6_addr;
248 #if 0 /* XXX: fill scope-id? */
249 if (IN6_IS_ADDR_LINKLOCAL(p) ||
250 IN6_IS_ADDR_MC_LINKLOCAL(p)){
251 ((struct sockaddr_in6*)sa)->sin6_scope_id = scopeid;
259 sa->sa_family = family;
260 #ifdef HAVE_SOCKADDR_SA_LEN
261 sa->sa_len = ifa_sa_len(family, len);
264 if (prefixlen > max_prefixlen)
265 prefixlen = max_prefixlen;
266 for (i=0; i<(prefixlen / 8); i++)
269 c <<= (8 - (prefixlen % 8));
276 /* ====================================================================== */
278 nl_sendreq(int sd, int request, int flags, int *seq)
280 char reqbuf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
281 NLMSG_ALIGN(sizeof(struct rtgenmsg))];
282 struct sockaddr_nl nladdr;
283 struct nlmsghdr *req_hdr;
284 struct rtgenmsg *req_msg;
285 time_t t = time(NULL);
288 memset(&reqbuf, 0, sizeof(reqbuf));
289 req_hdr = (struct nlmsghdr *)reqbuf;
290 req_msg = (struct rtgenmsg *)NLMSG_DATA(req_hdr);
291 req_hdr->nlmsg_len = NLMSG_LENGTH(sizeof(*req_msg));
292 req_hdr->nlmsg_type = request;
293 req_hdr->nlmsg_flags = flags | NLM_F_REQUEST;
294 req_hdr->nlmsg_pid = 0;
295 req_hdr->nlmsg_seq = t;
296 req_msg->rtgen_family = AF_UNSPEC;
297 memset(&nladdr, 0, sizeof(nladdr));
298 nladdr.nl_family = AF_NETLINK;
299 return (sendto(sd, (void *)req_hdr, req_hdr->nlmsg_len, 0,
300 (struct sockaddr *)&nladdr, sizeof(nladdr)));
304 nl_recvmsg(int sd, int request, int seq,
305 void *buf, size_t buflen,
309 struct iovec iov = { buf, buflen };
310 struct sockaddr_nl nladdr;
314 msg.msg_name = (void *)&nladdr;
315 msg.msg_namelen = sizeof(nladdr);
318 msg.msg_control = NULL;
319 msg.msg_controllen = 0;
321 read_len = recvmsg(sd, &msg, 0);
322 if ((read_len < 0 && errno == EINTR) || (msg.msg_flags & MSG_TRUNC))
324 if (flags) *flags = msg.msg_flags;
331 nl_getmsg(int sd, int request, int seq,
332 struct nlmsghdr **nlhp,
336 size_t bufsize = 65536, lastbufsize = 0;
338 int result = 0, read_size;
340 pid_t pid = getpid();
342 void *newbuff = realloc(buff, bufsize);
343 if (newbuff == NULL || bufsize < lastbufsize) {
348 result = read_size = nl_recvmsg(sd, request, seq, buff, bufsize, &msg_flags);
349 if (read_size < 0 || (msg_flags & MSG_TRUNC)){
350 lastbufsize = bufsize;
354 if (read_size == 0) break;
355 nh = (struct nlmsghdr *)buff;
356 for (nh = (struct nlmsghdr *)buff;
357 NLMSG_OK(nh, read_size);
358 nh = (struct nlmsghdr *)NLMSG_NEXT(nh, read_size)){
359 if (nh->nlmsg_pid != pid ||
360 nh->nlmsg_seq != seq)
362 if (nh->nlmsg_type == NLMSG_DONE){
366 if (nh->nlmsg_type == NLMSG_ERROR){
367 struct nlmsgerr *nlerr = (struct nlmsgerr *)NLMSG_DATA(nh);
369 if (nh->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr)))
372 __set_errno(-nlerr->error);
380 int saved_errno = errno;
382 __set_errno(saved_errno);
384 *nlhp = (struct nlmsghdr *)buff;
389 nl_getlist(int sd, int seq,
391 struct nlmsg_list **nlm_list,
392 struct nlmsg_list **nlm_end)
394 struct nlmsghdr *nlh = NULL;
400 status = nl_sendreq(sd, request, NLM_F_ROOT|NLM_F_MATCH, &seq);
404 seq = (int)time(NULL);
409 pfd.events = POLLIN | POLLPRI;
411 status = poll(&pfd, 1, 1000);
414 else if (status == 0) {
421 status = nl_getmsg(sd, request, seq, &nlh, &done);
425 struct nlmsg_list *nlm_next = (struct nlmsg_list *)malloc(sizeof(struct nlmsg_list));
426 if (nlm_next == NULL){
427 int saved_errno = errno;
429 __set_errno(saved_errno);
432 nlm_next->nlm_next = NULL;
433 nlm_next->nlh = (struct nlmsghdr *)nlh;
434 nlm_next->size = status;
436 if (*nlm_list == NULL){
437 *nlm_list = nlm_next;
440 (*nlm_end)->nlm_next = nlm_next;
446 return status >= 0 ? seq : status;
449 /* ---------------------------------------------------------------------- */
451 free_nlmsglist(struct nlmsg_list *nlm0)
453 struct nlmsg_list *nlm, *nlm_next;
458 for (nlm=nlm0; nlm; nlm=nlm_next){
461 nlm_next=nlm->nlm_next;
464 __set_errno(saved_errno);
468 free_data(void *data, void *ifdata)
470 int saved_errno = errno;
471 if (data != NULL) free(data);
472 if (ifdata != NULL) free(ifdata);
473 __set_errno(saved_errno);
476 /* ---------------------------------------------------------------------- */
480 int saved_errno = errno;
481 if (sd >= 0) __close(sd);
482 __set_errno(saved_errno);
485 /* ---------------------------------------------------------------------- */
489 struct sockaddr_nl nladdr;
492 sd = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
493 if (sd < 0) return -1;
494 memset(&nladdr, 0, sizeof(nladdr));
495 nladdr.nl_family = AF_NETLINK;
496 if (bind(sd, (struct sockaddr*)&nladdr, sizeof(nladdr)) < 0){
503 /* ====================================================================== */
504 int ROKEN_LIB_FUNCTION
505 rk_getifaddrs(struct ifaddrs **ifap)
508 struct nlmsg_list *nlmsg_list, *nlmsg_end, *nlm;
509 /* - - - - - - - - - - - - - - - */
511 size_t dlen, xlen, nlen;
512 uint32_t max_ifindex = 0;
514 pid_t pid = getpid();
517 int build ; /* 0 or 1 */
519 /* ---------------------------------- */
521 icnt = dlen = xlen = nlen = 0;
522 nlmsg_list = nlmsg_end = NULL;
527 /* ---------------------------------- */
528 /* open socket and bind */
533 /* ---------------------------------- */
535 if ((seq = nl_getlist(sd, 0, RTM_GETLINK,
536 &nlmsg_list, &nlmsg_end)) < 0){
537 free_nlmsglist(nlmsg_list);
541 if ((seq = nl_getlist(sd, seq+1, RTM_GETADDR,
542 &nlmsg_list, &nlmsg_end)) < 0){
543 free_nlmsglist(nlmsg_list);
548 /* ---------------------------------- */
549 /* Estimate size of result buffer and fill it */
550 for (build=0; build<=1; build++){
551 struct ifaddrs *ifl = NULL, *ifa = NULL;
552 struct nlmsghdr *nlh, *nlh0;
553 char *data = NULL, *xdata = NULL;
555 char *ifname = NULL, **iflist = NULL;
556 uint16_t *ifflist = NULL;
557 struct rtmaddr_ifamap ifamap;
561 NLMSG_ALIGN(sizeof(struct ifaddrs[icnt]))
562 + dlen + xlen + nlen);
563 ifa = (struct ifaddrs *)data;
565 NLMSG_ALIGN(sizeof(char *[max_ifindex+1]))
566 + NLMSG_ALIGN(sizeof(uint16_t [max_ifindex+1])));
568 *ifap = (ifdata != NULL) ? ifa : NULL;
570 free_data(data, ifdata);
574 if (data == NULL || ifdata == NULL){
575 free_data(data, ifdata);
580 data += NLMSG_ALIGN(sizeof(struct ifaddrs)) * icnt;
582 ifname = xdata + xlen;
584 ifflist = (uint16_t *)(((char *)iflist) + NLMSG_ALIGN(sizeof(char *[max_ifindex+1])));
587 for (nlm=nlmsg_list; nlm; nlm=nlm->nlm_next){
588 int nlmlen = nlm->size;
589 if (!(nlh0 = nlm->nlh))
592 NLMSG_OK(nlh, nlmlen);
593 nlh=NLMSG_NEXT(nlh,nlmlen)){
594 struct ifinfomsg *ifim = NULL;
595 struct ifaddrmsg *ifam = NULL;
598 size_t nlm_struct_size = 0;
599 sa_family_t nlm_family = 0;
600 uint32_t nlm_scope = 0, nlm_index = 0;
601 size_t sockaddr_size = 0;
602 uint32_t nlm_prefixlen = 0;
605 memset(&ifamap, 0, sizeof(ifamap));
607 /* check if the message is what we want */
608 if (nlh->nlmsg_pid != pid ||
609 nlh->nlmsg_seq != nlm->seq)
611 if (nlh->nlmsg_type == NLMSG_DONE){
614 switch (nlh->nlmsg_type){
616 ifim = (struct ifinfomsg *)NLMSG_DATA(nlh);
617 nlm_struct_size = sizeof(*ifim);
618 nlm_family = ifim->ifi_family;
620 nlm_index = ifim->ifi_index;
623 ifflist[nlm_index] = ifa->ifa_flags = ifim->ifi_flags;
626 ifam = (struct ifaddrmsg *)NLMSG_DATA(nlh);
627 nlm_struct_size = sizeof(*ifam);
628 nlm_family = ifam->ifa_family;
629 nlm_scope = ifam->ifa_scope;
630 nlm_index = ifam->ifa_index;
631 nlm_prefixlen = ifam->ifa_prefixlen;
633 ifa->ifa_flags = ifflist[nlm_index];
640 if (max_ifindex < nlm_index)
641 max_ifindex = nlm_index;
647 rtasize = NLMSG_PAYLOAD(nlh, nlmlen) - NLMSG_ALIGN(nlm_struct_size);
648 for (rta = (struct rtattr *)(((char *)NLMSG_DATA(nlh)) + NLMSG_ALIGN(nlm_struct_size));
649 RTA_OK(rta, rtasize);
650 rta = RTA_NEXT(rta, rtasize)){
651 struct sockaddr **sap = NULL;
652 void *rtadata = RTA_DATA(rta);
653 size_t rtapayload = RTA_PAYLOAD(rta);
656 switch(nlh->nlmsg_type){
658 switch(rta->rta_type){
662 sap = (rta->rta_type == IFLA_ADDRESS) ? &ifa->ifa_addr : &ifa->ifa_broadaddr;
663 *sap = (struct sockaddr *)data;
665 sa_len = ifa_sa_len(AF_PACKET, rtapayload);
666 if (rta->rta_type == IFLA_ADDRESS)
667 sockaddr_size = NLMSG_ALIGN(sa_len);
669 dlen += NLMSG_ALIGN(sa_len);
671 memset(*sap, 0, sa_len);
672 ifa_make_sockaddr(AF_PACKET, *sap, rtadata,rtapayload, 0,0);
673 ((struct sockaddr_ll *)*sap)->sll_ifindex = nlm_index;
674 ((struct sockaddr_ll *)*sap)->sll_hatype = ifim->ifi_type;
675 data += NLMSG_ALIGN(sa_len);
678 case IFLA_IFNAME:/* Name of Interface */
680 nlen += NLMSG_ALIGN(rtapayload + 1);
682 ifa->ifa_name = ifname;
683 if (iflist[nlm_index] == NULL)
684 iflist[nlm_index] = ifa->ifa_name;
685 strncpy(ifa->ifa_name, rtadata, rtapayload);
686 ifa->ifa_name[rtapayload] = '\0';
687 ifname += NLMSG_ALIGN(rtapayload + 1);
690 case IFLA_STATS:/* Statistics of Interface */
692 xlen += NLMSG_ALIGN(rtapayload);
694 ifa->ifa_data = xdata;
695 memcpy(ifa->ifa_data, rtadata, rtapayload);
696 xdata += NLMSG_ALIGN(rtapayload);
712 if (nlm_family == AF_PACKET) break;
713 switch(rta->rta_type){
715 ifamap.address = rtadata;
716 ifamap.address_len = rtapayload;
719 ifamap.local = rtadata;
720 ifamap.local_len = rtapayload;
723 ifamap.broadcast = rtadata;
724 ifamap.broadcast_len = rtapayload;
726 #ifdef HAVE_IFADDRS_IFA_ANYCAST
728 ifamap.anycast = rtadata;
729 ifamap.anycast_len = rtapayload;
734 nlen += NLMSG_ALIGN(rtapayload + 1);
736 ifa->ifa_name = ifname;
737 if (iflist[nlm_index] == NULL)
738 iflist[nlm_index] = ifname;
739 strncpy(ifa->ifa_name, rtadata, rtapayload);
740 ifa->ifa_name[rtapayload] = '\0';
741 ifname += NLMSG_ALIGN(rtapayload + 1);
753 if (nlh->nlmsg_type == RTM_NEWADDR &&
754 nlm_family != AF_PACKET) {
756 ifamap.local = ifamap.address;
757 ifamap.local_len = ifamap.address_len;
759 if (!ifamap.address) {
760 ifamap.address = ifamap.local;
761 ifamap.address_len = ifamap.local_len;
763 if (ifamap.address_len != ifamap.local_len ||
764 (ifamap.address != NULL &&
765 memcmp(ifamap.address, ifamap.local, ifamap.address_len))) {
766 /* p2p; address is peer and local is ours */
767 ifamap.broadcast = ifamap.address;
768 ifamap.broadcast_len = ifamap.address_len;
769 ifamap.address = ifamap.local;
770 ifamap.address_len = ifamap.local_len;
772 if (ifamap.address) {
774 sockaddr_size = NLMSG_ALIGN(ifa_sa_len(nlm_family,ifamap.address_len));
777 dlen += NLMSG_ALIGN(ifa_sa_len(nlm_family,ifamap.address_len));
779 ifa->ifa_addr = (struct sockaddr *)data;
780 ifa_make_sockaddr(nlm_family, ifa->ifa_addr, ifamap.address, ifamap.address_len,
781 nlm_scope, nlm_index);
782 data += NLMSG_ALIGN(ifa_sa_len(nlm_family, ifamap.address_len));
786 if (ifamap.netmask) {
788 dlen += NLMSG_ALIGN(ifa_sa_len(nlm_family,ifamap.netmask_len));
790 ifa->ifa_netmask = (struct sockaddr *)data;
791 ifa_make_sockaddr(nlm_family, ifa->ifa_netmask, ifamap.netmask, ifamap.netmask_len,
792 nlm_scope, nlm_index);
793 data += NLMSG_ALIGN(ifa_sa_len(nlm_family, ifamap.netmask_len));
797 if (ifamap.broadcast) {
799 dlen += NLMSG_ALIGN(ifa_sa_len(nlm_family,ifamap.broadcast_len));
801 ifa->ifa_broadaddr = (struct sockaddr *)data;
802 ifa_make_sockaddr(nlm_family, ifa->ifa_broadaddr, ifamap.broadcast, ifamap.broadcast_len,
803 nlm_scope, nlm_index);
804 data += NLMSG_ALIGN(ifa_sa_len(nlm_family, ifamap.broadcast_len));
807 #ifdef HAVE_IFADDRS_IFA_ANYCAST
808 if (ifamap.anycast) {
810 dlen += NLMSG_ALIGN(ifa_sa_len(nlm_family,ifamap.anycast_len));
812 ifa->ifa_anycast = (struct sockaddr *)data;
813 ifa_make_sockaddr(nlm_family, ifa->ifa_anyaddr, ifamap.anycast, ifamap.anycast_len,
814 nlm_scope, nlm_index);
815 data += NLMSG_ALIGN(ifa_sa_len(nlm_family, ifamap.anycast_len));
822 dlen += sockaddr_size;
826 if (ifa->ifa_name == NULL)
827 ifa->ifa_name = iflist[nlm_index];
830 ifa->ifa_addr->sa_family != AF_UNSPEC &&
831 ifa->ifa_addr->sa_family != AF_PACKET){
832 ifa->ifa_netmask = (struct sockaddr *)data;
833 ifa_make_sockaddr_mask(ifa->ifa_addr->sa_family, ifa->ifa_netmask, nlm_prefixlen);
835 data += sockaddr_size;
842 if (icnt == 0 && (dlen + nlen + xlen == 0)){
845 break; /* cannot found any addresses */
849 free_data(NULL, ifdata);
852 /* ---------------------------------- */
854 free_nlmsglist(nlmsg_list);
859 #else /* !AF_NETLINK */
862 * The generic SIOCGIFCONF version.
866 getifaddrs2(struct ifaddrs **ifap,
867 int af, int siocgifconf, int siocgifflags,
874 struct ifconf ifconf;
877 struct sockaddr sa_zero;
879 struct ifaddrs *start = NULL, **end = &start;
883 memset (&sa_zero, 0, sizeof(sa_zero));
884 fd = socket(af, SOCK_DGRAM, 0);
890 buf = calloc(1, buf_size);
895 ifconf.ifc_len = buf_size;
896 ifconf.ifc_buf = buf;
899 * Solaris returns EINVAL when the buffer is too small.
901 if (ioctl (fd, siocgifconf, &ifconf) < 0 && errno != EINVAL) {
906 * Can the difference between a full and a overfull buf
910 if (ifconf.ifc_len < buf_size)
916 for (p = ifconf.ifc_buf;
917 p < ifconf.ifc_buf + ifconf.ifc_len;
923 ifr = (struct ifreq *)p;
927 salen = sizeof(struct sockaddr);
928 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
930 sz = max(sz, sizeof(ifr->ifr_name) + sa->sa_len);
934 sz = max(sz, sizeof(ifr->ifr_name) + SA_LEN(sa));
936 memset (&ifreq, 0, sizeof(ifreq));
937 memcpy (ifreq.ifr_name, ifr->ifr_name, sizeof(ifr->ifr_name));
939 if (ioctl(fd, siocgifflags, &ifreq) < 0) {
944 *end = malloc(sizeof(**end));
950 (*end)->ifa_next = NULL;
951 (*end)->ifa_name = strdup(ifr->ifr_name);
952 if ((*end)->ifa_name == NULL) {
956 (*end)->ifa_flags = ifreq.ifr_flags;
957 (*end)->ifa_addr = malloc(salen);
958 if ((*end)->ifa_addr == NULL) {
962 memcpy((*end)->ifa_addr, sa, salen);
963 (*end)->ifa_netmask = NULL;
966 /* fix these when we actually need them */
967 if(ifreq.ifr_flags & IFF_BROADCAST) {
968 (*end)->ifa_broadaddr = malloc(sizeof(ifr->ifr_broadaddr));
969 if ((*end)->ifa_broadaddr == NULL) {
973 memcpy((*end)->ifa_broadaddr, &ifr->ifr_broadaddr,
974 sizeof(ifr->ifr_broadaddr));
975 } else if(ifreq.ifr_flags & IFF_POINTOPOINT) {
976 (*end)->ifa_dstaddr = malloc(sizeof(ifr->ifr_dstaddr));
977 if ((*end)->ifa_dstaddr == NULL) {
981 memcpy((*end)->ifa_dstaddr, &ifr->ifr_dstaddr,
982 sizeof(ifr->ifr_dstaddr));
984 (*end)->ifa_dstaddr = NULL;
986 (*end)->ifa_dstaddr = NULL;
989 (*end)->ifa_data = NULL;
991 end = &(*end)->ifa_next;
999 rk_freeifaddrs(start);
1006 #if defined(HAVE_IPV6) && defined(SIOCGLIFCONF) && defined(SIOCGLIFFLAGS)
1008 getlifaddrs2(struct ifaddrs **ifap,
1009 int af, int siocgifconf, int siocgifflags,
1016 struct lifconf ifconf;
1019 struct sockaddr sa_zero;
1021 struct ifaddrs *start = NULL, **end = &start;
1025 memset (&sa_zero, 0, sizeof(sa_zero));
1026 fd = socket(af, SOCK_DGRAM, 0);
1032 buf = calloc(1, buf_size);
1038 ifconf.lifc_family = AF_UNSPEC;
1039 ifconf.lifc_flags = 0;
1041 ifconf.lifc_len = buf_size;
1042 ifconf.lifc_buf = buf;
1045 * Solaris returns EINVAL when the buffer is too small.
1047 if (ioctl (fd, siocgifconf, &ifconf) < 0 && errno != EINVAL) {
1052 * Can the difference between a full and a overfull buf
1056 if (ifconf.lifc_len < buf_size)
1062 for (p = ifconf.lifc_buf;
1063 p < ifconf.lifc_buf + ifconf.lifc_len;
1065 struct lifreq ifreq;
1066 struct sockaddr_storage *sa;
1069 ifr = (struct lifreq *)p;
1070 sa = &ifr->lifr_addr;
1073 salen = sizeof(struct sockaddr_storage);
1074 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
1076 sz = max(sz, sizeof(ifr->ifr_name) + sa->sa_len);
1080 sz = max(sz, sizeof(ifr->ifr_name) + SA_LEN(sa));
1082 memset (&ifreq, 0, sizeof(ifreq));
1083 memcpy (ifreq.lifr_name, ifr->lifr_name, sizeof(ifr->lifr_name));
1085 if (ioctl(fd, siocgifflags, &ifreq) < 0) {
1090 *end = malloc(sizeof(**end));
1096 (*end)->ifa_next = NULL;
1097 (*end)->ifa_name = strdup(ifr->lifr_name);
1098 if ((*end)->ifa_name == NULL) {
1102 (*end)->ifa_flags = ifreq.lifr_flags;
1103 (*end)->ifa_addr = malloc(salen);
1104 if ((*end)->ifa_addr == NULL) {
1108 memcpy((*end)->ifa_addr, sa, salen);
1109 (*end)->ifa_netmask = NULL;
1112 /* fix these when we actually need them */
1113 if(ifreq.ifr_flags & IFF_BROADCAST) {
1114 (*end)->ifa_broadaddr = malloc(sizeof(ifr->ifr_broadaddr));
1115 if ((*end)->ifa_broadaddr == NULL) {
1119 memcpy((*end)->ifa_broadaddr, &ifr->ifr_broadaddr,
1120 sizeof(ifr->ifr_broadaddr));
1121 } else if(ifreq.ifr_flags & IFF_POINTOPOINT) {
1122 (*end)->ifa_dstaddr = malloc(sizeof(ifr->ifr_dstaddr));
1123 if ((*end)->ifa_dstaddr == NULL) {
1127 memcpy((*end)->ifa_dstaddr, &ifr->ifr_dstaddr,
1128 sizeof(ifr->ifr_dstaddr));
1130 (*end)->ifa_dstaddr = NULL;
1132 (*end)->ifa_dstaddr = NULL;
1135 (*end)->ifa_data = NULL;
1137 end = &(*end)->ifa_next;
1145 rk_freeifaddrs(start);
1151 #endif /* defined(HAVE_IPV6) && defined(SIOCGLIFCONF) && defined(SIOCGLIFFLAGS) */
1153 int ROKEN_LIB_FUNCTION
1154 rk_getifaddrs(struct ifaddrs **ifap)
1158 #if defined(AF_INET6) && defined(SIOCGIF6CONF) && defined(SIOCGIF6FLAGS)
1160 ret = getifaddrs2 (ifap, AF_INET6, SIOCGIF6CONF, SIOCGIF6FLAGS,
1161 sizeof(struct in6_ifreq));
1163 #if defined(HAVE_IPV6) && defined(SIOCGLIFCONF) && defined(SIOCGLIFFLAGS)
1165 ret = getlifaddrs2 (ifap, AF_INET6, SIOCGLIFCONF, SIOCGLIFFLAGS,
1166 sizeof(struct lifreq));
1168 #if defined(HAVE_IPV6) && defined(SIOCGIFCONF)
1170 ret = getifaddrs2 (ifap, AF_INET6, SIOCGIFCONF, SIOCGIFFLAGS,
1171 sizeof(struct ifreq));
1173 #if defined(AF_INET) && defined(SIOCGIFCONF) && defined(SIOCGIFFLAGS)
1175 ret = getifaddrs2 (ifap, AF_INET, SIOCGIFCONF, SIOCGIFFLAGS,
1176 sizeof(struct ifreq));
1181 #endif /* !AF_NETLINK */
1183 void ROKEN_LIB_FUNCTION
1184 rk_freeifaddrs(struct ifaddrs *ifp)
1186 struct ifaddrs *p, *q;
1193 free(p->ifa_dstaddr);
1195 free(p->ifa_netmask);
1207 print_addr(const char *s, struct sockaddr *sa)
1210 printf(" %s=%d/", s, sa->sa_family);
1211 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
1212 for(i = 0; i < sa->sa_len - ((long)sa->sa_data - (long)&sa->sa_family); i++)
1213 printf("%02x", ((unsigned char*)sa->sa_data)[i]);
1215 for(i = 0; i < sizeof(sa->sa_data); i++)
1216 printf("%02x", ((unsigned char*)sa->sa_data)[i]);
1222 print_ifaddrs(struct ifaddrs *x)
1226 for(p = x; p; p = p->ifa_next) {
1227 printf("%s\n", p->ifa_name);
1228 printf(" flags=%x\n", p->ifa_flags);
1230 print_addr("addr", p->ifa_addr);
1232 print_addr("dstaddr", p->ifa_dstaddr);
1234 print_addr("netmask", p->ifa_netmask);
1235 printf(" %p\n", p->ifa_data);
1242 struct ifaddrs *a = NULL, *b;
1243 getifaddrs2(&a, AF_INET, SIOCGIFCONF, SIOCGIFFLAGS, sizeof(struct ifreq));