Home | History | Annotate | Line # | Download | only in net
getifaddrs.c revision 1.5
      1  1.5  christos /*	$NetBSD: getifaddrs.c,v 1.5 2000/07/06 02:53:12 christos Exp $	*/
      2  1.1    itojun 
      3  1.1    itojun /*
      4  1.1    itojun  * Copyright (c) 1995, 1999
      5  1.1    itojun  *	Berkeley Software Design, Inc.  All rights reserved.
      6  1.1    itojun  *
      7  1.1    itojun  * Redistribution and use in source and binary forms, with or without
      8  1.1    itojun  * modification, are permitted provided that the following conditions
      9  1.1    itojun  * are met:
     10  1.1    itojun  * 1. Redistributions of source code must retain the above copyright
     11  1.1    itojun  *    notice, this list of conditions and the following disclaimer.
     12  1.1    itojun  *
     13  1.1    itojun  * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND
     14  1.1    itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     15  1.1    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     16  1.1    itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE
     17  1.1    itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     18  1.1    itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     19  1.1    itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     20  1.1    itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     21  1.1    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     22  1.1    itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     23  1.1    itojun  * SUCH DAMAGE.
     24  1.1    itojun  *
     25  1.2    itojun  *	BSDI getifaddrs.c,v 2.12 2000/02/23 14:51:59 dab Exp
     26  1.1    itojun  */
     27  1.4    itojun 
     28  1.4    itojun #include <sys/cdefs.h>
     29  1.4    itojun #if defined(LIBC_SCCS) && !defined(lint)
     30  1.5  christos __RCSID("$NetBSD: getifaddrs.c,v 1.5 2000/07/06 02:53:12 christos Exp $");
     31  1.4    itojun #endif /* LIBC_SCCS and not lint */
     32  1.4    itojun 
     33  1.3    itojun #include "namespace.h"
     34  1.1    itojun #include <sys/types.h>
     35  1.1    itojun #include <sys/ioctl.h>
     36  1.1    itojun #include <sys/socket.h>
     37  1.1    itojun #include <net/if.h>
     38  1.1    itojun #ifdef	NET_RT_IFLIST
     39  1.1    itojun #include <sys/param.h>
     40  1.1    itojun #include <net/route.h>
     41  1.1    itojun #include <sys/sysctl.h>
     42  1.1    itojun #include <net/if_dl.h>
     43  1.1    itojun #endif
     44  1.1    itojun 
     45  1.1    itojun #include <errno.h>
     46  1.1    itojun #include <ifaddrs.h>
     47  1.1    itojun #include <stdlib.h>
     48  1.1    itojun #include <string.h>
     49  1.3    itojun 
     50  1.3    itojun #ifdef __weak_alias
     51  1.3    itojun __weak_alias(getifaddrs,_getifaddrs)
     52  1.3    itojun __weak_alias(freeifaddrs,_freeifaddrs)
     53  1.3    itojun #endif
     54  1.1    itojun 
     55  1.1    itojun #if !defined(AF_LINK)
     56  1.1    itojun #define	SA_LEN(sa)	sizeof(struct sockaddr)
     57  1.1    itojun #endif
     58  1.1    itojun 
     59  1.1    itojun #if !defined(SA_LEN)
     60  1.1    itojun #define	SA_LEN(sa)	(sa)->sa_len
     61  1.1    itojun #endif
     62  1.1    itojun 
     63  1.1    itojun #define	SALIGN	(sizeof(long) - 1)
     64  1.1    itojun #define	SA_RLEN(sa)	((sa)->sa_len ? (((sa)->sa_len + SALIGN) & ~SALIGN) : (SALIGN + 1))
     65  1.1    itojun 
     66  1.1    itojun #ifndef	ALIGNBYTES
     67  1.1    itojun /*
     68  1.1    itojun  * On systems with a routing socket, ALIGNBYTES should match the value
     69  1.1    itojun  * that the kernel uses when building the messages.
     70  1.1    itojun  */
     71  1.1    itojun #define	ALIGNBYTES	XXX
     72  1.1    itojun #endif
     73  1.1    itojun #ifndef	ALIGN
     74  1.1    itojun #define	ALIGN(p)	(((u_long)(p) + ALIGNBYTES) &~ ALIGNBYTES)
     75  1.1    itojun #endif
     76  1.1    itojun 
     77  1.1    itojun #if	_BSDI_VERSION >= 199701
     78  1.1    itojun #define	HAVE_IFM_DATA
     79  1.1    itojun #endif
     80  1.1    itojun 
     81  1.1    itojun #if	_BSDI_VERSION >= 199802
     82  1.1    itojun #define	HAVE_IFAM_DATA
     83  1.1    itojun #endif
     84  1.1    itojun 
     85  1.1    itojun int
     86  1.1    itojun getifaddrs(struct ifaddrs **pif)
     87  1.1    itojun {
     88  1.1    itojun 	int icnt = 1;
     89  1.1    itojun 	int dcnt = 0;
     90  1.1    itojun 	int ncnt = 0;
     91  1.1    itojun #ifdef	NET_RT_IFLIST
     92  1.1    itojun 	int mib[6];
     93  1.1    itojun 	size_t needed;
     94  1.1    itojun 	char *buf;
     95  1.1    itojun 	char *next;
     96  1.1    itojun 	struct ifaddrs *cif = 0;
     97  1.1    itojun 	char *p, *p0;
     98  1.1    itojun 	struct rt_msghdr *rtm;
     99  1.1    itojun 	struct if_msghdr *ifm;
    100  1.1    itojun 	struct ifa_msghdr *ifam;
    101  1.1    itojun 	struct sockaddr_dl *dl;
    102  1.1    itojun 	struct sockaddr *sa;
    103  1.1    itojun 	struct ifaddrs *ifa, *ift;
    104  1.5  christos 	u_short idx = 0;
    105  1.1    itojun #else	/* NET_RT_IFLIST */
    106  1.1    itojun 	char buf[1024];
    107  1.1    itojun 	int m, sock;
    108  1.1    itojun 	struct ifconf ifc;
    109  1.1    itojun 	struct ifreq *ifr;
    110  1.1    itojun 	struct ifreq *lifr;
    111  1.1    itojun #endif	/* NET_RT_IFLIST */
    112  1.1    itojun 	int i;
    113  1.1    itojun 	size_t len, alen;
    114  1.1    itojun 	char *data;
    115  1.1    itojun 	char *names;
    116  1.1    itojun 
    117  1.1    itojun #ifdef	NET_RT_IFLIST
    118  1.1    itojun 	mib[0] = CTL_NET;
    119  1.1    itojun 	mib[1] = PF_ROUTE;
    120  1.1    itojun 	mib[2] = 0;             /* protocol */
    121  1.1    itojun 	mib[3] = 0;             /* wildcard address family */
    122  1.1    itojun 	mib[4] = NET_RT_IFLIST;
    123  1.1    itojun 	mib[5] = 0;             /* no flags */
    124  1.1    itojun 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
    125  1.1    itojun 		return (-1);
    126  1.1    itojun 	if ((buf = malloc(needed)) == NULL)
    127  1.1    itojun 		return (-1);
    128  1.1    itojun 	if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
    129  1.1    itojun 		free(buf);
    130  1.1    itojun 		return (-1);
    131  1.1    itojun 	}
    132  1.1    itojun 
    133  1.1    itojun 	for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
    134  1.5  christos 		rtm = (struct rt_msghdr *)(void *)next;
    135  1.1    itojun 		if (rtm->rtm_version != RTM_VERSION)
    136  1.1    itojun 			continue;
    137  1.1    itojun 		switch (rtm->rtm_type) {
    138  1.1    itojun 		case RTM_IFINFO:
    139  1.5  christos 			ifm = (struct if_msghdr *)(void *)rtm;
    140  1.1    itojun 			if (ifm->ifm_addrs & RTA_IFP) {
    141  1.5  christos 				idx = ifm->ifm_index;
    142  1.1    itojun 				++icnt;
    143  1.5  christos 				dl = (struct sockaddr_dl *)(void *)(ifm + 1);
    144  1.5  christos 				dcnt += SA_RLEN((struct sockaddr *)(void*)dl) +
    145  1.5  christos 				    ALIGNBYTES;
    146  1.1    itojun #ifdef	HAVE_IFM_DATA
    147  1.1    itojun 				dcnt += sizeof(ifm->ifm_data);
    148  1.1    itojun #endif	/* HAVE_IFM_DATA */
    149  1.1    itojun 				ncnt += dl->sdl_nlen + 1;
    150  1.1    itojun 			} else
    151  1.5  christos 				idx = 0;
    152  1.1    itojun 			break;
    153  1.1    itojun 
    154  1.1    itojun 		case RTM_NEWADDR:
    155  1.5  christos 			ifam = (struct ifa_msghdr *)(void *)rtm;
    156  1.5  christos 			if (idx && ifam->ifam_index != idx)
    157  1.1    itojun 				abort();	/* this cannot happen */
    158  1.1    itojun 
    159  1.1    itojun #define	RTA_MASKS	(RTA_NETMASK | RTA_IFA | RTA_BRD)
    160  1.5  christos 			if (idx == 0 || (ifam->ifam_addrs & RTA_MASKS) == 0)
    161  1.1    itojun 				break;
    162  1.5  christos 			p = (char *)(void *)(ifam + 1);
    163  1.1    itojun 			++icnt;
    164  1.1    itojun #ifdef	HAVE_IFAM_DATA
    165  1.1    itojun 			dcnt += sizeof(ifam->ifam_data) + ALIGNBYTES;
    166  1.1    itojun #endif	/* HAVE_IFAM_DATA */
    167  1.1    itojun 			/* Scan to look for length of address */
    168  1.1    itojun 			alen = 0;
    169  1.1    itojun 			for (p0 = p, i = 0; i < RTAX_MAX; i++) {
    170  1.1    itojun 				if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
    171  1.1    itojun 				    == 0)
    172  1.1    itojun 					continue;
    173  1.5  christos 				sa = (struct sockaddr *)(void *)p;
    174  1.1    itojun 				len = SA_RLEN(sa);
    175  1.1    itojun 				if (i == RTAX_IFA) {
    176  1.1    itojun 					alen = len;
    177  1.1    itojun 					break;
    178  1.1    itojun 				}
    179  1.1    itojun 				p += len;
    180  1.1    itojun 			}
    181  1.1    itojun 			for (p = p0, i = 0; i < RTAX_MAX; i++) {
    182  1.1    itojun 				if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
    183  1.1    itojun 				    == 0)
    184  1.1    itojun 					continue;
    185  1.5  christos 				sa = (struct sockaddr *)(void *)p;
    186  1.1    itojun 				len = SA_RLEN(sa);
    187  1.1    itojun 				if (i == RTAX_NETMASK && SA_LEN(sa) == 0)
    188  1.1    itojun 					dcnt += alen;
    189  1.1    itojun 				else
    190  1.1    itojun 					dcnt += len;
    191  1.1    itojun 				p += len;
    192  1.1    itojun 			}
    193  1.1    itojun 			break;
    194  1.1    itojun 		}
    195  1.1    itojun 	}
    196  1.1    itojun #else	/* NET_RT_IFLIST */
    197  1.1    itojun 	ifc.ifc_buf = buf;
    198  1.1    itojun 	ifc.ifc_len = sizeof(buf);
    199  1.1    itojun 
    200  1.1    itojun 	if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    201  1.1    itojun 		return (-1);
    202  1.1    itojun 	i =  ioctl(sock, SIOCGIFCONF, (char *)&ifc);
    203  1.1    itojun 	close(sock);
    204  1.1    itojun 	if (i < 0)
    205  1.1    itojun 		return (-1);
    206  1.1    itojun 
    207  1.1    itojun 	ifr = ifc.ifc_req;
    208  1.1    itojun 	lifr = (struct ifreq *)&ifc.ifc_buf[ifc.ifc_len];
    209  1.1    itojun 
    210  1.1    itojun 	while (ifr < lifr) {
    211  1.1    itojun 		struct sockaddr *sa;
    212  1.1    itojun 
    213  1.1    itojun 		sa = &ifr->ifr_addr;
    214  1.1    itojun 		++icnt;
    215  1.1    itojun 		dcnt += SA_RLEN(sa);
    216  1.1    itojun 		ncnt += sizeof(ifr->ifr_name) + 1;
    217  1.1    itojun 
    218  1.1    itojun 		ifr = (struct ifreq *)(((char *)sa) + SA_LEN(sa));
    219  1.1    itojun 	}
    220  1.1    itojun #endif	/* NET_RT_IFLIST */
    221  1.1    itojun 
    222  1.1    itojun 	if (icnt + dcnt + ncnt == 1) {
    223  1.1    itojun 		*pif = NULL;
    224  1.1    itojun 		free(buf);
    225  1.1    itojun 		return (0);
    226  1.1    itojun 	}
    227  1.1    itojun 	data = malloc(sizeof(struct ifaddrs) * icnt + dcnt + ncnt);
    228  1.1    itojun 	if (data == NULL) {
    229  1.1    itojun 		free(buf);
    230  1.1    itojun 		return(-1);
    231  1.1    itojun 	}
    232  1.1    itojun 
    233  1.5  christos 	ifa = (struct ifaddrs *)(void *)data;
    234  1.1    itojun 	data += sizeof(struct ifaddrs) * icnt;
    235  1.1    itojun 	names = data + dcnt;
    236  1.1    itojun 
    237  1.1    itojun 	memset(ifa, 0, sizeof(struct ifaddrs) * icnt);
    238  1.1    itojun 	ift = ifa;
    239  1.1    itojun 
    240  1.1    itojun #ifdef	NET_RT_IFLIST
    241  1.5  christos 	idx = 0;
    242  1.1    itojun 	for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
    243  1.5  christos 		rtm = (struct rt_msghdr *)(void *)next;
    244  1.1    itojun 		if (rtm->rtm_version != RTM_VERSION)
    245  1.1    itojun 			continue;
    246  1.1    itojun 		switch (rtm->rtm_type) {
    247  1.1    itojun 		case RTM_IFINFO:
    248  1.5  christos 			ifm = (struct if_msghdr *)(void *)rtm;
    249  1.1    itojun 			if (ifm->ifm_addrs & RTA_IFP) {
    250  1.5  christos 				idx = ifm->ifm_index;
    251  1.5  christos 				dl = (struct sockaddr_dl *)(void *)(ifm + 1);
    252  1.1    itojun 
    253  1.1    itojun 				cif = ift;
    254  1.1    itojun 				ift->ifa_name = names;
    255  1.1    itojun 				ift->ifa_flags = (int)ifm->ifm_flags;
    256  1.5  christos 				memcpy(names, dl->sdl_data,
    257  1.5  christos 				    (size_t)dl->sdl_nlen);
    258  1.1    itojun 				names[dl->sdl_nlen] = 0;
    259  1.1    itojun 				names += dl->sdl_nlen + 1;
    260  1.1    itojun 
    261  1.5  christos 				ift->ifa_addr = (struct sockaddr *)(void *)data;
    262  1.5  christos 				memcpy(data, dl,
    263  1.5  christos 				    (size_t)SA_LEN((struct sockaddr *)
    264  1.5  christos 				    (void *)dl));
    265  1.5  christos 				data += SA_RLEN((struct sockaddr *)(void *)dl);
    266  1.1    itojun 
    267  1.1    itojun #ifdef	HAVE_IFM_DATA
    268  1.1    itojun 				/* ifm_data needs to be aligned */
    269  1.1    itojun 				ift->ifa_data = data = (void *)ALIGN(data);
    270  1.1    itojun 				memcpy(data, &ifm->ifm_data, sizeof(ifm->ifm_data));
    271  1.1    itojun  				data += sizeof(ifm->ifm_data);
    272  1.1    itojun #else	/* HAVE_IFM_DATA */
    273  1.1    itojun 				ift->ifa_data = NULL;
    274  1.1    itojun #endif	/* HAVE_IFM_DATA */
    275  1.1    itojun 
    276  1.1    itojun 				ift = (ift->ifa_next = ift + 1);
    277  1.1    itojun 			} else
    278  1.5  christos 				idx = 0;
    279  1.1    itojun 			break;
    280  1.1    itojun 
    281  1.1    itojun 		case RTM_NEWADDR:
    282  1.5  christos 			ifam = (struct ifa_msghdr *)(void *)rtm;
    283  1.5  christos 			if (idx && ifam->ifam_index != idx)
    284  1.1    itojun 				abort();	/* this cannot happen */
    285  1.1    itojun 
    286  1.5  christos 			if (idx == 0 || (ifam->ifam_addrs & RTA_MASKS) == 0)
    287  1.1    itojun 				break;
    288  1.1    itojun 			ift->ifa_name = cif->ifa_name;
    289  1.1    itojun 			ift->ifa_flags = cif->ifa_flags;
    290  1.1    itojun 			ift->ifa_data = NULL;
    291  1.5  christos 			p = (char *)(void *)(ifam + 1);
    292  1.1    itojun 			/* Scan to look for length of address */
    293  1.1    itojun 			alen = 0;
    294  1.1    itojun 			for (p0 = p, i = 0; i < RTAX_MAX; i++) {
    295  1.1    itojun 				if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
    296  1.1    itojun 				    == 0)
    297  1.1    itojun 					continue;
    298  1.5  christos 				sa = (struct sockaddr *)(void *)p;
    299  1.1    itojun 				len = SA_RLEN(sa);
    300  1.1    itojun 				if (i == RTAX_IFA) {
    301  1.1    itojun 					alen = len;
    302  1.1    itojun 					break;
    303  1.1    itojun 				}
    304  1.1    itojun 				p += len;
    305  1.1    itojun 			}
    306  1.1    itojun 			for (p = p0, i = 0; i < RTAX_MAX; i++) {
    307  1.1    itojun 				if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
    308  1.1    itojun 				    == 0)
    309  1.1    itojun 					continue;
    310  1.5  christos 				sa = (struct sockaddr *)(void *)p;
    311  1.1    itojun 				len = SA_RLEN(sa);
    312  1.1    itojun 				switch (i) {
    313  1.1    itojun 				case RTAX_IFA:
    314  1.5  christos 					ift->ifa_addr =
    315  1.5  christos 					    (struct sockaddr *)(void *)data;
    316  1.1    itojun 					memcpy(data, p, len);
    317  1.1    itojun 					data += len;
    318  1.1    itojun 					break;
    319  1.1    itojun 
    320  1.1    itojun 				case RTAX_NETMASK:
    321  1.1    itojun 					ift->ifa_netmask =
    322  1.5  christos 					    (struct sockaddr *)(void *)data;
    323  1.1    itojun 					if (SA_LEN(sa) == 0) {
    324  1.1    itojun 						memset(data, 0, alen);
    325  1.1    itojun 						data += alen;
    326  1.1    itojun 						break;
    327  1.1    itojun 					}
    328  1.1    itojun 					memcpy(data, p, len);
    329  1.1    itojun 					data += len;
    330  1.1    itojun 					break;
    331  1.1    itojun 
    332  1.1    itojun 				case RTAX_BRD:
    333  1.1    itojun 					ift->ifa_broadaddr =
    334  1.5  christos 					    (struct sockaddr *)(void *)data;
    335  1.1    itojun 					memcpy(data, p, len);
    336  1.1    itojun 					data += len;
    337  1.1    itojun 					break;
    338  1.1    itojun 				}
    339  1.1    itojun 				p += len;
    340  1.1    itojun 			}
    341  1.1    itojun 
    342  1.1    itojun #ifdef	HAVE_IFAM_DATA
    343  1.1    itojun 			/* ifam_data needs to be aligned */
    344  1.1    itojun 			ift->ifa_data = data = (void *)ALIGN(data);
    345  1.1    itojun 			memcpy(data, &ifam->ifam_data, sizeof(ifam->ifam_data));
    346  1.1    itojun 			data += sizeof(ifam->ifam_data);
    347  1.1    itojun #endif	/* HAVE_IFAM_DATA */
    348  1.1    itojun 
    349  1.1    itojun 			ift = (ift->ifa_next = ift + 1);
    350  1.1    itojun 			break;
    351  1.1    itojun 		}
    352  1.1    itojun 	}
    353  1.1    itojun 
    354  1.1    itojun 	free(buf);
    355  1.1    itojun #else	/* NET_RT_IFLIST */
    356  1.1    itojun 	ifr = ifc.ifc_req;
    357  1.1    itojun 	lifr = (struct ifreq *)&ifc.ifc_buf[ifc.ifc_len];
    358  1.1    itojun 
    359  1.1    itojun 	while (ifr < lifr) {
    360  1.1    itojun 		struct sockaddr *sa;
    361  1.1    itojun 
    362  1.1    itojun 		ift->ifa_name = names;
    363  1.1    itojun 		names[sizeof(ifr->ifr_name)] = 0;
    364  1.1    itojun 		strncpy(names, ifr->ifr_name, sizeof(ifr->ifr_name));
    365  1.1    itojun 		while (*names++)
    366  1.1    itojun 			;
    367  1.1    itojun 
    368  1.1    itojun 		ift->ifa_addr = (struct sockaddr *)data;
    369  1.1    itojun 		sa = &ifr->ifr_addr;
    370  1.1    itojun 		memcpy(data, sa, SA_LEN(sa));
    371  1.1    itojun 		data += SA_RLEN(sa);
    372  1.1    itojun 
    373  1.1    itojun 		ifr = (struct ifreq *)(((char *)sa) + SA_LEN(sa));
    374  1.1    itojun 		ift = (ift->ifa_next = ift + 1);
    375  1.1    itojun 	}
    376  1.1    itojun #endif	/* NET_RT_IFLIST */
    377  1.1    itojun 	if (--ift >= ifa) {
    378  1.1    itojun 		ift->ifa_next = NULL;
    379  1.1    itojun 		*pif = ifa;
    380  1.1    itojun 	} else {
    381  1.1    itojun 		*pif = NULL;
    382  1.1    itojun 		free(ifa);
    383  1.1    itojun 	}
    384  1.1    itojun 	return (0);
    385  1.2    itojun }
    386  1.2    itojun 
    387  1.2    itojun void
    388  1.2    itojun freeifaddrs(struct ifaddrs *ifp)
    389  1.2    itojun {
    390  1.2    itojun 	free(ifp);
    391  1.1    itojun }
    392