Home | History | Annotate | Line # | Download | only in dist
fad-getad.c revision 1.2
      1  1.2  christos /*	$NetBSD: fad-getad.c,v 1.2 2014/11/19 19:33:30 christos Exp $	*/
      2  1.2  christos 
      3  1.1  christos /* -*- Mode: c; tab-width: 8; indent-tabs-mode: 1; c-basic-offset: 8; -*- */
      4  1.1  christos /*
      5  1.1  christos  * Copyright (c) 1994, 1995, 1996, 1997, 1998
      6  1.1  christos  *	The Regents of the University of California.  All rights reserved.
      7  1.1  christos  *
      8  1.1  christos  * Redistribution and use in source and binary forms, with or without
      9  1.1  christos  * modification, are permitted provided that the following conditions
     10  1.1  christos  * are met:
     11  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     12  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     13  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     15  1.1  christos  *    documentation and/or other materials provided with the distribution.
     16  1.1  christos  * 3. All advertising materials mentioning features or use of this software
     17  1.1  christos  *    must display the following acknowledgement:
     18  1.1  christos  *	This product includes software developed by the Computer Systems
     19  1.1  christos  *	Engineering Group at Lawrence Berkeley Laboratory.
     20  1.1  christos  * 4. Neither the name of the University nor of the Laboratory may be used
     21  1.1  christos  *    to endorse or promote products derived from this software without
     22  1.1  christos  *    specific prior written permission.
     23  1.1  christos  *
     24  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  1.1  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  1.1  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  1.1  christos  * SUCH DAMAGE.
     35  1.1  christos  */
     36  1.1  christos 
     37  1.2  christos #include <sys/cdefs.h>
     38  1.2  christos __RCSID("$NetBSD: fad-getad.c,v 1.2 2014/11/19 19:33:30 christos Exp $");
     39  1.1  christos 
     40  1.1  christos #ifdef HAVE_CONFIG_H
     41  1.1  christos #include "config.h"
     42  1.1  christos #endif
     43  1.1  christos 
     44  1.1  christos #include <sys/types.h>
     45  1.1  christos #include <sys/socket.h>
     46  1.1  christos #include <netinet/in.h>
     47  1.1  christos 
     48  1.1  christos #include <net/if.h>
     49  1.1  christos 
     50  1.1  christos #include <ctype.h>
     51  1.1  christos #include <errno.h>
     52  1.1  christos #include <stdio.h>
     53  1.1  christos #include <stdlib.h>
     54  1.1  christos #include <string.h>
     55  1.1  christos #include <ifaddrs.h>
     56  1.1  christos 
     57  1.1  christos #include "pcap-int.h"
     58  1.1  christos 
     59  1.1  christos #ifdef HAVE_OS_PROTO_H
     60  1.1  christos #include "os-proto.h"
     61  1.1  christos #endif
     62  1.1  christos 
     63  1.2  christos /*
     64  1.2  christos  * We don't do this on Solaris 11 and later, as it appears there aren't
     65  1.2  christos  * any AF_PACKET addresses on interfaces, so we don't need this, and
     66  1.2  christos  * we end up including both the OS's <net/bpf.h> and our <pcap/bpf.h>,
     67  1.2  christos  * and their definitions of some data structures collide.
     68  1.2  christos  */
     69  1.2  christos #if (defined(linux) || defined(__Lynx__)) && defined(AF_PACKET)
     70  1.2  christos # ifdef HAVE_NETPACKET_PACKET_H
     71  1.2  christos /* Linux distributions with newer glibc */
     72  1.2  christos #  include <netpacket/packet.h>
     73  1.2  christos # else /* HAVE_NETPACKET_PACKET_H */
     74  1.2  christos /* LynxOS, Linux distributions with older glibc */
     75  1.1  christos # ifdef __Lynx__
     76  1.1  christos /* LynxOS */
     77  1.1  christos #  include <netpacket/if_packet.h>
     78  1.2  christos # else /* __Lynx__ */
     79  1.1  christos /* Linux */
     80  1.1  christos #  include <linux/types.h>
     81  1.1  christos #  include <linux/if_packet.h>
     82  1.2  christos # endif /* __Lynx__ */
     83  1.2  christos # endif /* HAVE_NETPACKET_PACKET_H */
     84  1.2  christos #endif /* (defined(linux) || defined(__Lynx__)) && defined(AF_PACKET) */
     85  1.1  christos 
     86  1.1  christos /*
     87  1.1  christos  * This is fun.
     88  1.1  christos  *
     89  1.1  christos  * In older BSD systems, socket addresses were fixed-length, and
     90  1.1  christos  * "sizeof (struct sockaddr)" gave the size of the structure.
     91  1.1  christos  * All addresses fit within a "struct sockaddr".
     92  1.1  christos  *
     93  1.1  christos  * In newer BSD systems, the socket address is variable-length, and
     94  1.1  christos  * there's an "sa_len" field giving the length of the structure;
     95  1.1  christos  * this allows socket addresses to be longer than 2 bytes of family
     96  1.1  christos  * and 14 bytes of data.
     97  1.1  christos  *
     98  1.1  christos  * Some commercial UNIXes use the old BSD scheme, some use the RFC 2553
     99  1.1  christos  * variant of the old BSD scheme (with "struct sockaddr_storage" rather
    100  1.1  christos  * than "struct sockaddr"), and some use the new BSD scheme.
    101  1.1  christos  *
    102  1.1  christos  * Some versions of GNU libc use neither scheme, but has an "SA_LEN()"
    103  1.1  christos  * macro that determines the size based on the address family.  Other
    104  1.1  christos  * versions don't have "SA_LEN()" (as it was in drafts of RFC 2553
    105  1.1  christos  * but not in the final version).  On the latter systems, we explicitly
    106  1.1  christos  * check the AF_ type to determine the length; we assume that on
    107  1.1  christos  * all those systems we have "struct sockaddr_storage".
    108  1.1  christos  */
    109  1.1  christos #ifndef SA_LEN
    110  1.1  christos #ifdef HAVE_SOCKADDR_SA_LEN
    111  1.1  christos #define SA_LEN(addr)	((addr)->sa_len)
    112  1.1  christos #else /* HAVE_SOCKADDR_SA_LEN */
    113  1.1  christos #ifdef HAVE_SOCKADDR_STORAGE
    114  1.1  christos static size_t
    115  1.1  christos get_sa_len(struct sockaddr *addr)
    116  1.1  christos {
    117  1.1  christos 	switch (addr->sa_family) {
    118  1.1  christos 
    119  1.1  christos #ifdef AF_INET
    120  1.1  christos 	case AF_INET:
    121  1.1  christos 		return (sizeof (struct sockaddr_in));
    122  1.1  christos #endif
    123  1.1  christos 
    124  1.1  christos #ifdef AF_INET6
    125  1.1  christos 	case AF_INET6:
    126  1.1  christos 		return (sizeof (struct sockaddr_in6));
    127  1.1  christos #endif
    128  1.1  christos 
    129  1.2  christos #if (defined(linux) || defined(__Lynx__)) && defined(AF_PACKET)
    130  1.1  christos 	case AF_PACKET:
    131  1.1  christos 		return (sizeof (struct sockaddr_ll));
    132  1.1  christos #endif
    133  1.1  christos 
    134  1.1  christos 	default:
    135  1.1  christos 		return (sizeof (struct sockaddr));
    136  1.1  christos 	}
    137  1.1  christos }
    138  1.1  christos #define SA_LEN(addr)	(get_sa_len(addr))
    139  1.1  christos #else /* HAVE_SOCKADDR_STORAGE */
    140  1.1  christos #define SA_LEN(addr)	(sizeof (struct sockaddr))
    141  1.1  christos #endif /* HAVE_SOCKADDR_STORAGE */
    142  1.1  christos #endif /* HAVE_SOCKADDR_SA_LEN */
    143  1.1  christos #endif /* SA_LEN */
    144  1.1  christos 
    145  1.1  christos /*
    146  1.1  christos  * Get a list of all interfaces that are up and that we can open.
    147  1.1  christos  * Returns -1 on error, 0 otherwise.
    148  1.1  christos  * The list, as returned through "alldevsp", may be null if no interfaces
    149  1.2  christos  * could be opened.
    150  1.1  christos  */
    151  1.1  christos int
    152  1.2  christos pcap_findalldevs_interfaces(pcap_if_t **alldevsp, char *errbuf)
    153  1.1  christos {
    154  1.1  christos 	pcap_if_t *devlist = NULL;
    155  1.1  christos 	struct ifaddrs *ifap, *ifa;
    156  1.1  christos 	struct sockaddr *addr, *netmask, *broadaddr, *dstaddr;
    157  1.1  christos 	size_t addr_size, broadaddr_size, dstaddr_size;
    158  1.1  christos 	int ret = 0;
    159  1.1  christos 	char *p, *q;
    160  1.1  christos 
    161  1.1  christos 	/*
    162  1.1  christos 	 * Get the list of interface addresses.
    163  1.1  christos 	 *
    164  1.1  christos 	 * Note: this won't return information about interfaces
    165  1.2  christos 	 * with no addresses, so, if a platform has interfaces
    166  1.2  christos 	 * with no interfaces on which traffic can be captured,
    167  1.2  christos 	 * we must check for those interfaces as well (see, for
    168  1.2  christos 	 * example, what's done on Linux).
    169  1.1  christos 	 *
    170  1.1  christos 	 * LAN interfaces will probably have link-layer
    171  1.1  christos 	 * addresses; I don't know whether all implementations
    172  1.1  christos 	 * of "getifaddrs()" now, or in the future, will return
    173  1.1  christos 	 * those.
    174  1.1  christos 	 */
    175  1.1  christos 	if (getifaddrs(&ifap) != 0) {
    176  1.1  christos 		(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
    177  1.1  christos 		    "getifaddrs: %s", pcap_strerror(errno));
    178  1.1  christos 		return (-1);
    179  1.1  christos 	}
    180  1.1  christos 	for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
    181  1.1  christos 		/*
    182  1.1  christos 		 * "ifa_addr" was apparently null on at least one
    183  1.2  christos 		 * interface on some system.  Therefore, we supply
    184  1.2  christos 		 * the address and netmask only if "ifa_addr" is
    185  1.2  christos 		 * non-null (if there's no address, there's obviously
    186  1.2  christos 		 * no netmask).
    187  1.1  christos 		 */
    188  1.1  christos 		if (ifa->ifa_addr != NULL) {
    189  1.1  christos 			addr = ifa->ifa_addr;
    190  1.1  christos 			addr_size = SA_LEN(addr);
    191  1.1  christos 			netmask = ifa->ifa_netmask;
    192  1.1  christos 		} else {
    193  1.1  christos 			addr = NULL;
    194  1.1  christos 			addr_size = 0;
    195  1.1  christos 			netmask = NULL;
    196  1.1  christos 		}
    197  1.2  christos 
    198  1.2  christos 		/*
    199  1.2  christos 		 * Note that, on some platforms, ifa_broadaddr and
    200  1.2  christos 		 * ifa_dstaddr could be the same field (true on at
    201  1.2  christos 		 * least some versions of *BSD and OS X), so we
    202  1.2  christos 		 * can't just check whether the broadcast address
    203  1.2  christos 		 * is null and add it if so and check whether the
    204  1.2  christos 		 * destination address is null and add it if so.
    205  1.2  christos 		 *
    206  1.2  christos 		 * Therefore, we must also check the IFF_BROADCAST
    207  1.2  christos 		 * flag, and only add a broadcast address if it's
    208  1.2  christos 		 * set, and check the IFF_POINTTOPOINT flag, and
    209  1.2  christos 		 * only add a destination address if it's set (as
    210  1.2  christos 		 * per man page recommendations on some of those
    211  1.2  christos 		 * platforms).
    212  1.2  christos 		 */
    213  1.1  christos 		if (ifa->ifa_flags & IFF_BROADCAST &&
    214  1.1  christos 		    ifa->ifa_broadaddr != NULL) {
    215  1.1  christos 			broadaddr = ifa->ifa_broadaddr;
    216  1.1  christos 			broadaddr_size = SA_LEN(broadaddr);
    217  1.1  christos 		} else {
    218  1.1  christos 			broadaddr = NULL;
    219  1.1  christos 			broadaddr_size = 0;
    220  1.1  christos 		}
    221  1.1  christos 		if (ifa->ifa_flags & IFF_POINTOPOINT &&
    222  1.1  christos 		    ifa->ifa_dstaddr != NULL) {
    223  1.1  christos 			dstaddr = ifa->ifa_dstaddr;
    224  1.1  christos 			dstaddr_size = SA_LEN(ifa->ifa_dstaddr);
    225  1.1  christos 		} else {
    226  1.1  christos 			dstaddr = NULL;
    227  1.1  christos 			dstaddr_size = 0;
    228  1.1  christos 		}
    229  1.1  christos 
    230  1.1  christos 		/*
    231  1.1  christos 		 * If this entry has a colon followed by a number at
    232  1.1  christos 		 * the end, we assume it's a logical interface.  Those
    233  1.1  christos 		 * are just the way you assign multiple IP addresses to
    234  1.1  christos 		 * a real interface on Linux, so an entry for a logical
    235  1.1  christos 		 * interface should be treated like the entry for the
    236  1.1  christos 		 * real interface; we do that by stripping off the ":"
    237  1.1  christos 		 * and the number.
    238  1.1  christos 		 *
    239  1.1  christos 		 * XXX - should we do this only on Linux?
    240  1.1  christos 		 */
    241  1.1  christos 		p = strchr(ifa->ifa_name, ':');
    242  1.1  christos 		if (p != NULL) {
    243  1.1  christos 			/*
    244  1.1  christos 			 * We have a ":"; is it followed by a number?
    245  1.1  christos 			 */
    246  1.1  christos 			q = p + 1;
    247  1.1  christos 			while (isdigit((unsigned char)*q))
    248  1.1  christos 				q++;
    249  1.1  christos 			if (*q == '\0') {
    250  1.1  christos 				/*
    251  1.1  christos 				 * All digits after the ":" until the end.
    252  1.1  christos 				 * Strip off the ":" and everything after
    253  1.1  christos 				 * it.
    254  1.1  christos 				 */
    255  1.1  christos 			       *p = '\0';
    256  1.1  christos 			}
    257  1.1  christos 		}
    258  1.1  christos 
    259  1.1  christos 		/*
    260  1.1  christos 		 * Add information for this address to the list.
    261  1.1  christos 		 */
    262  1.1  christos 		if (add_addr_to_iflist(&devlist, ifa->ifa_name,
    263  1.1  christos 		    ifa->ifa_flags, addr, addr_size, netmask, addr_size,
    264  1.1  christos 		    broadaddr, broadaddr_size, dstaddr, dstaddr_size,
    265  1.1  christos 		    errbuf) < 0) {
    266  1.1  christos 			ret = -1;
    267  1.1  christos 			break;
    268  1.1  christos 		}
    269  1.1  christos 	}
    270  1.1  christos 
    271  1.1  christos 	freeifaddrs(ifap);
    272  1.1  christos 
    273  1.1  christos 	if (ret == -1) {
    274  1.1  christos 		/*
    275  1.1  christos 		 * We had an error; free the list we've been constructing.
    276  1.1  christos 		 */
    277  1.1  christos 		if (devlist != NULL) {
    278  1.1  christos 			pcap_freealldevs(devlist);
    279  1.1  christos 			devlist = NULL;
    280  1.1  christos 		}
    281  1.1  christos 	}
    282  1.1  christos 
    283  1.1  christos 	*alldevsp = devlist;
    284  1.1  christos 	return (ret);
    285  1.1  christos }
    286