inet.c revision 199231
1131377Stjr/* -*- Mode: c; tab-width: 8; indent-tabs-mode: 1; c-basic-offset: 8; -*- */
2131377Stjr/*
3131377Stjr * Copyright (c) 1994, 1995, 1996, 1997, 1998
4131377Stjr *	The Regents of the University of California.  All rights reserved.
5131377Stjr *
6131377Stjr * Redistribution and use in source and binary forms, with or without
7131377Stjr * modification, are permitted provided that the following conditions
8131377Stjr * are met:
9131377Stjr * 1. Redistributions of source code must retain the above copyright
10131377Stjr *    notice, this list of conditions and the following disclaimer.
11131377Stjr * 2. Redistributions in binary form must reproduce the above copyright
12131377Stjr *    notice, this list of conditions and the following disclaimer in the
13131377Stjr *    documentation and/or other materials provided with the distribution.
14131377Stjr * 3. All advertising materials mentioning features or use of this software
15131377Stjr *    must display the following acknowledgement:
16131377Stjr *	This product includes software developed by the Computer Systems
17131377Stjr *	Engineering Group at Lawrence Berkeley Laboratory.
18131377Stjr * 4. Neither the name of the University nor of the Laboratory may be used
19131377Stjr *    to endorse or promote products derived from this software without
20131377Stjr *    specific prior written permission.
21131377Stjr *
22131377Stjr * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23131377Stjr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24131377Stjr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25131377Stjr * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26131377Stjr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27131377Stjr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28131377Stjr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29131377Stjr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30131377Stjr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31131377Stjr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32131377Stjr * SUCH DAMAGE.
33131377Stjr */
34131377Stjr
35131377Stjr#ifndef lint
36131377Stjrstatic const char rcsid[] _U_ =
37131377Stjr    "@(#) $Header: /tcpdump/master/libpcap/inet.c,v 1.75.2.4 2008-04-20 18:19:24 guy Exp $ (LBL)";
38131377Stjr#endif
39131377Stjr
40131377Stjr#ifdef HAVE_CONFIG_H
41131377Stjr#include "config.h"
42131377Stjr#endif
43131377Stjr
44131377Stjr#ifdef WIN32
45131377Stjr#include <pcap-stdinc.h>
46131377Stjr#else /* WIN32 */
47131377Stjr
48131377Stjr#include <sys/param.h>
49131377Stjr#ifndef MSDOS
50131377Stjr#include <sys/file.h>
51131377Stjr#endif
52131377Stjr#include <sys/ioctl.h>
53131377Stjr#include <sys/socket.h>
54131377Stjr#ifdef HAVE_SYS_SOCKIO_H
55131377Stjr#include <sys/sockio.h>
56131377Stjr#endif
57131377Stjr
58131377Stjrstruct mbuf;		/* Squelch compiler warnings on some platforms for */
59131377Stjrstruct rtentry;		/* declarations in <net/if.h> */
60131377Stjr#include <net/if.h>
61131377Stjr#include <netinet/in.h>
62131377Stjr#endif /* WIN32 */
63131377Stjr
64131377Stjr#include <ctype.h>
65131377Stjr#include <errno.h>
66131377Stjr#include <memory.h>
67131377Stjr#include <stdio.h>
68131377Stjr#include <stdlib.h>
69131377Stjr#include <string.h>
70131377Stjr#if !defined(WIN32) && !defined(__BORLANDC__)
71131377Stjr#include <unistd.h>
72131377Stjr#endif /* !WIN32 && !__BORLANDC__ */
73131377Stjr#ifdef HAVE_LIMITS_H
74131377Stjr#include <limits.h>
75131377Stjr#else
76131377Stjr#define INT_MAX		2147483647
77131377Stjr#endif
78131377Stjr
79131377Stjr#include "pcap-int.h"
80131377Stjr
81131377Stjr#ifdef HAVE_OS_PROTO_H
82131377Stjr#include "os-proto.h"
83131377Stjr#endif
84131377Stjr
85131377Stjr/* Not all systems have IFF_LOOPBACK */
86131377Stjr#ifdef IFF_LOOPBACK
87131377Stjr#define ISLOOPBACK(name, flags) ((flags) & IFF_LOOPBACK)
88131377Stjr#else
89131377Stjr#define ISLOOPBACK(name, flags) ((name)[0] == 'l' && (name)[1] == 'o' && \
90131377Stjr    (isdigit((unsigned char)((name)[2])) || (name)[2] == '\0'))
91131377Stjr#endif
92131377Stjr
93131377Stjrstruct sockaddr *
94131377Stjrdup_sockaddr(struct sockaddr *sa, size_t sa_length)
95131377Stjr{
96131377Stjr	struct sockaddr *newsa;
97131377Stjr
98131377Stjr	if ((newsa = malloc(sa_length)) == NULL)
99131377Stjr		return (NULL);
100131377Stjr	return (memcpy(newsa, sa, sa_length));
101131377Stjr}
102131377Stjr
103131377Stjrstatic int
104131377Stjrget_instance(const char *name)
105131377Stjr{
106131377Stjr	const char *cp, *endcp;
107131377Stjr	int n;
108131377Stjr
109131377Stjr	if (strcmp(name, "any") == 0) {
110131377Stjr		/*
111131377Stjr		 * Give the "any" device an artificially high instance
112131377Stjr		 * number, so it shows up after all other non-loopback
113131377Stjr		 * interfaces.
114131377Stjr		 */
115131377Stjr		return INT_MAX;
116131377Stjr	}
117131377Stjr
118131377Stjr	endcp = name + strlen(name);
119131377Stjr	for (cp = name; cp < endcp && !isdigit((unsigned char)*cp); ++cp)
120131377Stjr		continue;
121131377Stjr
122131377Stjr	if (isdigit((unsigned char)*cp))
123131377Stjr		n = atoi(cp);
124131377Stjr	else
125131377Stjr		n = 0;
126131377Stjr	return (n);
127131377Stjr}
128131377Stjr
129131377Stjrint
130131377Stjradd_or_find_if(pcap_if_t **curdev_ret, pcap_if_t **alldevs, const char *name,
131131377Stjr    u_int flags, const char *description, char *errbuf)
132131377Stjr{
133131377Stjr	pcap_t *p;
134131377Stjr	pcap_if_t *curdev, *prevdev, *nextdev;
135131377Stjr	int this_instance;
136131377Stjr
137131377Stjr	/*
138131377Stjr	 * Is there already an entry in the list for this interface?
139131377Stjr	 */
140131377Stjr	for (curdev = *alldevs; curdev != NULL; curdev = curdev->next) {
141131377Stjr		if (strcmp(name, curdev->name) == 0)
142131377Stjr			break;	/* yes, we found it */
143131377Stjr	}
144131377Stjr
145131377Stjr	if (curdev == NULL) {
146131377Stjr		/*
147131377Stjr		 * No, we didn't find it.
148131377Stjr		 *
149131377Stjr		 * Can we open this interface for live capture?
150131377Stjr		 *
151131377Stjr		 * We do this check so that interfaces that are
152131377Stjr		 * supplied by the interface enumeration mechanism
153131377Stjr		 * we're using but that don't support packet capture
154131377Stjr		 * aren't included in the list.  Loopback interfaces
155131377Stjr		 * on Solaris are an example of this; we don't just
156131377Stjr		 * omit loopback interfaces on all platforms because
157131377Stjr		 * you *can* capture on loopback interfaces on some
158131377Stjr		 * OSes.
159131377Stjr		 *
160131377Stjr		 * On OS X, we don't do this check if the device
161131377Stjr		 * name begins with "wlt"; at least some versions
162131377Stjr		 * of OS X offer monitor mode capturing by having
163131377Stjr		 * a separate "monitor mode" device for each wireless
164131377Stjr		 * adapter, rather than by implementing the ioctls
165131377Stjr		 * that {Free,Net,Open,DragonFly}BSD provide.
166131377Stjr		 * Opening that device puts the adapter into monitor
167131377Stjr		 * mode, which, at least for some adapters, causes
168131377Stjr		 * them to deassociate from the network with which
169131377Stjr		 * they're associated.
170131377Stjr		 *
171131377Stjr		 * Instead, we try to open the corresponding "en"
172131377Stjr		 * device (so that we don't end up with, for users
173131377Stjr		 * without sufficient privilege to open capture
174131377Stjr		 * devices, a list of adapters that only includes
175131377Stjr		 * the wlt devices).
176131377Stjr		 */
177131377Stjr#ifdef __APPLE__
178131377Stjr		if (strncmp(name, "wlt", 3) == 0) {
179131377Stjr			char *en_name;
180131377Stjr			size_t en_name_len;
181131377Stjr
182131377Stjr			/*
183131377Stjr			 * Try to allocate a buffer for the "en"
184131377Stjr			 * device's name.
185131377Stjr			 */
186131377Stjr			en_name_len = strlen(name) - 1;
187131377Stjr			en_name = malloc(en_name_len + 1);
188131377Stjr			if (en_name == NULL) {
189131377Stjr				(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
190131377Stjr				    "malloc: %s", pcap_strerror(errno));
191131377Stjr				return (-1);
192131377Stjr			}
193131377Stjr			strcpy(en_name, "en");
194131377Stjr			strcat(en_name, name + 3);
195131377Stjr			p = pcap_open_live(en_name, 68, 0, 0, errbuf);
196131377Stjr			free(en_name);
197131377Stjr		} else
198131377Stjr#endif /* __APPLE */
199131377Stjr		p = pcap_open_live(name, 68, 0, 0, errbuf);
200131377Stjr		if (p == NULL) {
201131377Stjr			/*
202131377Stjr			 * No.  Don't bother including it.
203131377Stjr			 * Don't treat this as an error, though.
204131377Stjr			 */
205131377Stjr			*curdev_ret = NULL;
206131377Stjr			return (0);
207131377Stjr		}
208131377Stjr		pcap_close(p);
209131377Stjr
210131377Stjr		/*
211131377Stjr		 * Yes, we can open it.
212131377Stjr		 * Allocate a new entry.
213131377Stjr		 */
214131377Stjr		curdev = malloc(sizeof(pcap_if_t));
215131377Stjr		if (curdev == NULL) {
216131377Stjr			(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
217131377Stjr			    "malloc: %s", pcap_strerror(errno));
218131377Stjr			return (-1);
219131377Stjr		}
220131377Stjr
221131377Stjr		/*
222131377Stjr		 * Fill in the entry.
223131377Stjr		 */
224131377Stjr		curdev->next = NULL;
225131377Stjr		curdev->name = strdup(name);
226131377Stjr		if (curdev->name == NULL) {
227131377Stjr			(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
228131377Stjr			    "malloc: %s", pcap_strerror(errno));
229131377Stjr			free(curdev);
230131377Stjr			return (-1);
231131377Stjr		}
232131377Stjr		if (description != NULL) {
233131377Stjr			/*
234131377Stjr			 * We have a description for this interface.
235131377Stjr			 */
236131377Stjr			curdev->description = strdup(description);
237131377Stjr			if (curdev->description == NULL) {
238131377Stjr				(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
239131377Stjr				    "malloc: %s", pcap_strerror(errno));
240131377Stjr				free(curdev->name);
241131377Stjr				free(curdev);
242131377Stjr				return (-1);
243131377Stjr			}
244131377Stjr		} else {
245131377Stjr			/*
246131377Stjr			 * We don't.
247131377Stjr			 */
248131377Stjr			curdev->description = NULL;
249131377Stjr		}
250131377Stjr		curdev->addresses = NULL;	/* list starts out as empty */
251131377Stjr		curdev->flags = 0;
252131377Stjr		if (ISLOOPBACK(name, flags))
253131377Stjr			curdev->flags |= PCAP_IF_LOOPBACK;
254131377Stjr
255131377Stjr		/*
256131377Stjr		 * Add it to the list, in the appropriate location.
257131377Stjr		 * First, get the instance number of this interface.
258131377Stjr		 */
259131377Stjr		this_instance = get_instance(name);
260131377Stjr
261131377Stjr		/*
262131377Stjr		 * Now look for the last interface with an instance number
263131377Stjr		 * less than or equal to the new interface's instance
264131377Stjr		 * number - except that non-loopback interfaces are
265131377Stjr		 * arbitrarily treated as having interface numbers less
266131377Stjr		 * than those of loopback interfaces, so the loopback
267131377Stjr		 * interfaces are put at the end of the list.
268131377Stjr		 *
269131377Stjr		 * We start with "prevdev" being NULL, meaning we're before
270131377Stjr		 * the first element in the list.
271131377Stjr		 */
272131377Stjr		prevdev = NULL;
273131377Stjr		for (;;) {
274131377Stjr			/*
275131377Stjr			 * Get the interface after this one.
276131377Stjr			 */
277131377Stjr			if (prevdev == NULL) {
278131377Stjr				/*
279131377Stjr				 * The next element is the first element.
280131377Stjr				 */
281131377Stjr				nextdev = *alldevs;
282131377Stjr			} else
283131377Stjr				nextdev = prevdev->next;
284131377Stjr
285131377Stjr			/*
286131377Stjr			 * Are we at the end of the list?
287131377Stjr			 */
288131377Stjr			if (nextdev == NULL) {
289131377Stjr				/*
290131377Stjr				 * Yes - we have to put the new entry
291131377Stjr				 * after "prevdev".
292131377Stjr				 */
293131377Stjr				break;
294131377Stjr			}
295131377Stjr
296131377Stjr			/*
297131377Stjr			 * Is the new interface a non-loopback interface
298131377Stjr			 * and the next interface a loopback interface?
299131377Stjr			 */
300131377Stjr			if (!(curdev->flags & PCAP_IF_LOOPBACK) &&
301131377Stjr			    (nextdev->flags & PCAP_IF_LOOPBACK)) {
302131377Stjr				/*
303131377Stjr				 * Yes, we should put the new entry
304131377Stjr				 * before "nextdev", i.e. after "prevdev".
305131377Stjr				 */
306131377Stjr				break;
307131377Stjr			}
308131377Stjr
309131377Stjr			/*
310131377Stjr			 * Is the new interface's instance number less
311131377Stjr			 * than the next interface's instance number,
312131377Stjr			 * and is it the case that the new interface is a
313131377Stjr			 * non-loopback interface or the next interface is
314131377Stjr			 * a loopback interface?
315131377Stjr			 *
316131377Stjr			 * (The goal of both loopback tests is to make
317131377Stjr			 * sure that we never put a loopback interface
318131377Stjr			 * before any non-loopback interface and that we
319131377Stjr			 * always put a non-loopback interface before all
320131377Stjr			 * loopback interfaces.)
321131377Stjr			 */
322131377Stjr			if (this_instance < get_instance(nextdev->name) &&
323131377Stjr			    (!(curdev->flags & PCAP_IF_LOOPBACK) ||
324131377Stjr			       (nextdev->flags & PCAP_IF_LOOPBACK))) {
325131377Stjr				/*
326131377Stjr				 * Yes - we should put the new entry
327131377Stjr				 * before "nextdev", i.e. after "prevdev".
328131377Stjr				 */
329131377Stjr				break;
330131377Stjr			}
331131377Stjr
332131377Stjr			prevdev = nextdev;
333131377Stjr		}
334131377Stjr
335131377Stjr		/*
336131377Stjr		 * Insert before "nextdev".
337131377Stjr		 */
338131377Stjr		curdev->next = nextdev;
339131377Stjr
340131377Stjr		/*
341131377Stjr		 * Insert after "prevdev" - unless "prevdev" is null,
342131377Stjr		 * in which case this is the first interface.
343131377Stjr		 */
344131377Stjr		if (prevdev == NULL) {
345131377Stjr			/*
346131377Stjr			 * This is the first interface.  Pass back a
347131377Stjr			 * pointer to it, and put "curdev" before
348131377Stjr			 * "nextdev".
349131377Stjr			 */
350131377Stjr			*alldevs = curdev;
351131377Stjr		} else
352131377Stjr			prevdev->next = curdev;
353131377Stjr	}
354131377Stjr
355131377Stjr	*curdev_ret = curdev;
356131377Stjr	return (0);
357131377Stjr}
358131377Stjr
359131377Stjr/*
360131377Stjr * XXX - on FreeBSDs that support it, should it get the sysctl named
361131377Stjr * "dev.{adapter family name}.{adapter unit}.%desc" to get a description
362131377Stjr * of the adapter?  Note that "dev.an.0.%desc" is "Aironet PC4500/PC4800"
363131377Stjr * with my Cisco 350 card, so the name isn't entirely descriptive.  The
364131377Stjr * "dev.an.0.%pnpinfo" has a better description, although one might argue
365131377Stjr * that the problem is really a driver bug - if it can find out that it's
366131377Stjr * a Cisco 340 or 350, rather than an old Aironet card, it should use
367131377Stjr * that in the description.
368131377Stjr *
369131377Stjr * Do NetBSD, DragonflyBSD, or OpenBSD support this as well?  OpenBSD
370131377Stjr * lets you get a description, but it's not generated by the OS, it's
371131377Stjr * set with another ioctl that ifconfig supports; we use that to get
372131377Stjr * the description in OpenBSD.
373131377Stjr *
374131377Stjr * In OS X, the System Configuration framework can apparently return
375131377Stjr * names in 10.4 and later; it also appears that freedesktop.org's HAL
376131377Stjr * offers an "info.product" string, but the HAL specification says
377131377Stjr * it "should not be used in any UI" and "subsystem/capability
378131377Stjr * specific properties" should be used instead.  Using that would
379131377Stjr * require that libpcap applications be linked with the frameworks/
380131377Stjr * libraries in question, which would be a bit of a pain unless we
381131377Stjr * offer, for example, a pkg-config:
382131377Stjr *
383131377Stjr *	http://pkg-config.freedesktop.org/wiki/
384131377Stjr *
385131377Stjr * script, so applications can just use that script to find out what
386131377Stjr * libraries you need to link with when linking with libpcap.
387131377Stjr * pkg-config is GPLed; I don't know whether that would prevent its
388131377Stjr * use with a BSD-licensed library such as libpcap.
389131377Stjr *
390131377Stjr * Do any other UN*Xes, or desktop environments support getting a
391131377Stjr * description?
392131377Stjr */
393131377Stjrint
394131377Stjradd_addr_to_iflist(pcap_if_t **alldevs, const char *name, u_int flags,
395131377Stjr    struct sockaddr *addr, size_t addr_size,
396131377Stjr    struct sockaddr *netmask, size_t netmask_size,
397131377Stjr    struct sockaddr *broadaddr, size_t broadaddr_size,
398131377Stjr    struct sockaddr *dstaddr, size_t dstaddr_size,
399131377Stjr    char *errbuf)
400131377Stjr{
401131377Stjr	pcap_if_t *curdev;
402131377Stjr	char *description = NULL;
403131377Stjr	pcap_addr_t *curaddr, *prevaddr, *nextaddr;
404131377Stjr#ifdef SIOCGIFDESCR
405131377Stjr	struct ifreq ifrdesc;
406131377Stjr	char ifdescr[IFDESCRSIZE];
407131377Stjr	int s;
408131377Stjr#endif
409131377Stjr
410131377Stjr#ifdef SIOCGIFDESCR
411131377Stjr	/*
412131377Stjr	 * Get the description for the interface.
413131377Stjr	 */
414131377Stjr	memset(&ifrdesc, 0, sizeof ifrdesc);
415131377Stjr	strlcpy(ifrdesc.ifr_name, name, sizeof ifrdesc.ifr_name);
416131377Stjr	ifrdesc.ifr_data = (caddr_t)&ifdescr;
417131377Stjr	s = socket(AF_INET, SOCK_DGRAM, 0);
418131377Stjr	if (s >= 0) {
419131377Stjr		if (ioctl(s, SIOCGIFDESCR, &ifrdesc) == 0 &&
420131377Stjr		    strlen(ifrdesc.ifr_data) != 0)
421131377Stjr			description = ifrdesc.ifr_data;
422131377Stjr		close(s);
423131377Stjr	}
424131377Stjr#endif
425131377Stjr
426131377Stjr	if (add_or_find_if(&curdev, alldevs, name, flags, description,
427131377Stjr	    errbuf) == -1) {
428131377Stjr		/*
429131377Stjr		 * Error - give up.
430131377Stjr		 */
431131377Stjr		return (-1);
432131377Stjr	}
433131377Stjr	if (curdev == NULL) {
434131377Stjr		/*
435131377Stjr		 * Device wasn't added because it can't be opened.
436131377Stjr		 * Not a fatal error.
437131377Stjr		 */
438131377Stjr		return (0);
439131377Stjr	}
440131377Stjr
441131377Stjr	/*
442	 * "curdev" is an entry for this interface; add an entry for this
443	 * address to its list of addresses.
444	 *
445	 * Allocate the new entry and fill it in.
446	 */
447	curaddr = malloc(sizeof(pcap_addr_t));
448	if (curaddr == NULL) {
449		(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
450		    "malloc: %s", pcap_strerror(errno));
451		return (-1);
452	}
453
454	curaddr->next = NULL;
455	if (addr != NULL) {
456		curaddr->addr = dup_sockaddr(addr, addr_size);
457		if (curaddr->addr == NULL) {
458			(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
459			    "malloc: %s", pcap_strerror(errno));
460			free(curaddr);
461			return (-1);
462		}
463	} else
464		curaddr->addr = NULL;
465
466	if (netmask != NULL) {
467		curaddr->netmask = dup_sockaddr(netmask, netmask_size);
468		if (curaddr->netmask == NULL) {
469			(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
470			    "malloc: %s", pcap_strerror(errno));
471			if (curaddr->addr != NULL)
472				free(curaddr->addr);
473			free(curaddr);
474			return (-1);
475		}
476	} else
477		curaddr->netmask = NULL;
478
479	if (broadaddr != NULL) {
480		curaddr->broadaddr = dup_sockaddr(broadaddr, broadaddr_size);
481		if (curaddr->broadaddr == NULL) {
482			(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
483			    "malloc: %s", pcap_strerror(errno));
484			if (curaddr->netmask != NULL)
485				free(curaddr->netmask);
486			if (curaddr->addr != NULL)
487				free(curaddr->addr);
488			free(curaddr);
489			return (-1);
490		}
491	} else
492		curaddr->broadaddr = NULL;
493
494	if (dstaddr != NULL) {
495		curaddr->dstaddr = dup_sockaddr(dstaddr, dstaddr_size);
496		if (curaddr->dstaddr == NULL) {
497			(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
498			    "malloc: %s", pcap_strerror(errno));
499			if (curaddr->broadaddr != NULL)
500				free(curaddr->broadaddr);
501			if (curaddr->netmask != NULL)
502				free(curaddr->netmask);
503			if (curaddr->addr != NULL)
504				free(curaddr->addr);
505			free(curaddr);
506			return (-1);
507		}
508	} else
509		curaddr->dstaddr = NULL;
510
511	/*
512	 * Find the end of the list of addresses.
513	 */
514	for (prevaddr = curdev->addresses; prevaddr != NULL; prevaddr = nextaddr) {
515		nextaddr = prevaddr->next;
516		if (nextaddr == NULL) {
517			/*
518			 * This is the end of the list.
519			 */
520			break;
521		}
522	}
523
524	if (prevaddr == NULL) {
525		/*
526		 * The list was empty; this is the first member.
527		 */
528		curdev->addresses = curaddr;
529	} else {
530		/*
531		 * "prevaddr" is the last member of the list; append
532		 * this member to it.
533		 */
534		prevaddr->next = curaddr;
535	}
536
537	return (0);
538}
539
540int
541pcap_add_if(pcap_if_t **devlist, const char *name, u_int flags,
542    const char *description, char *errbuf)
543{
544	pcap_if_t *curdev;
545
546	return (add_or_find_if(&curdev, devlist, name, flags, description,
547	    errbuf));
548}
549
550
551/*
552 * Free a list of interfaces.
553 */
554void
555pcap_freealldevs(pcap_if_t *alldevs)
556{
557	pcap_if_t *curdev, *nextdev;
558	pcap_addr_t *curaddr, *nextaddr;
559
560	for (curdev = alldevs; curdev != NULL; curdev = nextdev) {
561		nextdev = curdev->next;
562
563		/*
564		 * Free all addresses.
565		 */
566		for (curaddr = curdev->addresses; curaddr != NULL; curaddr = nextaddr) {
567			nextaddr = curaddr->next;
568			if (curaddr->addr)
569				free(curaddr->addr);
570			if (curaddr->netmask)
571				free(curaddr->netmask);
572			if (curaddr->broadaddr)
573				free(curaddr->broadaddr);
574			if (curaddr->dstaddr)
575				free(curaddr->dstaddr);
576			free(curaddr);
577		}
578
579		/*
580		 * Free the name string.
581		 */
582		free(curdev->name);
583
584		/*
585		 * Free the description string, if any.
586		 */
587		if (curdev->description != NULL)
588			free(curdev->description);
589
590		/*
591		 * Free the interface.
592		 */
593		free(curdev);
594	}
595}
596
597#if !defined(WIN32) && !defined(MSDOS)
598
599/*
600 * Return the name of a network interface attached to the system, or NULL
601 * if none can be found.  The interface must be configured up; the
602 * lowest unit number is preferred; loopback is ignored.
603 */
604char *
605pcap_lookupdev(errbuf)
606	register char *errbuf;
607{
608	pcap_if_t *alldevs;
609/* for old BSD systems, including bsdi3 */
610#ifndef IF_NAMESIZE
611#define IF_NAMESIZE IFNAMSIZ
612#endif
613	static char device[IF_NAMESIZE + 1];
614	char *ret;
615
616	if (pcap_findalldevs(&alldevs, errbuf) == -1)
617		return (NULL);
618
619	if (alldevs == NULL || (alldevs->flags & PCAP_IF_LOOPBACK)) {
620		/*
621		 * There are no devices on the list, or the first device
622		 * on the list is a loopback device, which means there
623		 * are no non-loopback devices on the list.  This means
624		 * we can't return any device.
625		 *
626		 * XXX - why not return a loopback device?  If we can't
627		 * capture on it, it won't be on the list, and if it's
628		 * on the list, there aren't any non-loopback devices,
629		 * so why not just supply it as the default device?
630		 */
631		(void)strlcpy(errbuf, "no suitable device found",
632		    PCAP_ERRBUF_SIZE);
633		ret = NULL;
634	} else {
635		/*
636		 * Return the name of the first device on the list.
637		 */
638		(void)strlcpy(device, alldevs->name, sizeof(device));
639		ret = device;
640	}
641
642	pcap_freealldevs(alldevs);
643	return (ret);
644}
645
646int
647pcap_lookupnet(device, netp, maskp, errbuf)
648	register const char *device;
649	register bpf_u_int32 *netp, *maskp;
650	register char *errbuf;
651{
652	register int fd;
653	register struct sockaddr_in *sin4;
654	struct ifreq ifr;
655
656	/*
657	 * The pseudo-device "any" listens on all interfaces and therefore
658	 * has the network address and -mask "0.0.0.0" therefore catching
659	 * all traffic. Using NULL for the interface is the same as "any".
660	 */
661	if (!device || strcmp(device, "any") == 0
662#ifdef HAVE_DAG_API
663	    || strstr(device, "dag") != NULL
664#endif
665#ifdef HAVE_SEPTEL_API
666	    || strstr(device, "septel") != NULL
667#endif
668#ifdef PCAP_SUPPORT_BT
669	    || strstr(device, "bluetooth") != NULL
670#endif
671#ifdef PCAP_SUPPORT_USB
672	    || strstr(device, "usb") != NULL
673#endif
674	    ) {
675		*netp = *maskp = 0;
676		return 0;
677	}
678
679	fd = socket(AF_INET, SOCK_DGRAM, 0);
680	if (fd < 0) {
681		(void)snprintf(errbuf, PCAP_ERRBUF_SIZE, "socket: %s",
682		    pcap_strerror(errno));
683		return (-1);
684	}
685	memset(&ifr, 0, sizeof(ifr));
686#ifdef linux
687	/* XXX Work around Linux kernel bug */
688	ifr.ifr_addr.sa_family = AF_INET;
689#endif
690	(void)strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
691	if (ioctl(fd, SIOCGIFADDR, (char *)&ifr) < 0) {
692		if (errno == EADDRNOTAVAIL) {
693			(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
694			    "%s: no IPv4 address assigned", device);
695		} else {
696			(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
697			    "SIOCGIFADDR: %s: %s",
698			    device, pcap_strerror(errno));
699		}
700		(void)close(fd);
701		return (-1);
702	}
703	sin4 = (struct sockaddr_in *)&ifr.ifr_addr;
704	*netp = sin4->sin_addr.s_addr;
705	if (ioctl(fd, SIOCGIFNETMASK, (char *)&ifr) < 0) {
706		(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
707		    "SIOCGIFNETMASK: %s: %s", device, pcap_strerror(errno));
708		(void)close(fd);
709		return (-1);
710	}
711	(void)close(fd);
712	*maskp = sin4->sin_addr.s_addr;
713	if (*maskp == 0) {
714		if (IN_CLASSA(*netp))
715			*maskp = IN_CLASSA_NET;
716		else if (IN_CLASSB(*netp))
717			*maskp = IN_CLASSB_NET;
718		else if (IN_CLASSC(*netp))
719			*maskp = IN_CLASSC_NET;
720		else {
721			(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
722			    "inet class for 0x%x unknown", *netp);
723			return (-1);
724		}
725	}
726	*netp &= *maskp;
727	return (0);
728}
729
730#elif defined(WIN32)
731
732/*
733 * Return the name of a network interface attached to the system, or NULL
734 * if none can be found.  The interface must be configured up; the
735 * lowest unit number is preferred; loopback is ignored.
736 */
737char *
738pcap_lookupdev(errbuf)
739	register char *errbuf;
740{
741	DWORD dwVersion;
742	DWORD dwWindowsMajorVersion;
743	dwVersion = GetVersion();	/* get the OS version */
744	dwWindowsMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion)));
745
746	if (dwVersion >= 0x80000000 && dwWindowsMajorVersion >= 4) {
747		/*
748		 * Windows 95, 98, ME.
749		 */
750		ULONG NameLength = 8192;
751		static char AdaptersName[8192];
752
753		if (PacketGetAdapterNames(AdaptersName,&NameLength) )
754			return (AdaptersName);
755		else
756			return NULL;
757	} else {
758		/*
759		 * Windows NT (NT 4.0, W2K, WXP). Convert the names to UNICODE for backward compatibility
760		 */
761		ULONG NameLength = 8192;
762		static WCHAR AdaptersName[8192];
763		char *tAstr;
764		WCHAR *tUstr;
765		WCHAR *TAdaptersName = (WCHAR*)malloc(8192 * sizeof(WCHAR));
766		int NAdapts = 0;
767
768		if(TAdaptersName == NULL)
769		{
770			(void)snprintf(errbuf, PCAP_ERRBUF_SIZE, "memory allocation failure");
771			return NULL;
772		}
773
774		if ( !PacketGetAdapterNames((PTSTR)TAdaptersName,&NameLength) )
775		{
776			(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
777				"PacketGetAdapterNames: %s",
778				pcap_win32strerror());
779			free(TAdaptersName);
780			return NULL;
781		}
782
783
784		tAstr = (char*)TAdaptersName;
785		tUstr = (WCHAR*)AdaptersName;
786
787		/*
788		 * Convert and copy the device names
789		 */
790		while(sscanf(tAstr, "%S", tUstr) > 0)
791		{
792			tAstr += strlen(tAstr) + 1;
793			tUstr += wcslen(tUstr) + 1;
794			NAdapts ++;
795		}
796
797		tAstr++;
798		*tUstr = 0;
799		tUstr++;
800
801		/*
802		 * Copy the descriptions
803		 */
804		while(NAdapts--)
805		{
806			strcpy((char*)tUstr, tAstr);
807			(char*)tUstr += strlen(tAstr) + 1;;
808			tAstr += strlen(tAstr) + 1;
809		}
810
811		free(TAdaptersName);
812		return (char *)(AdaptersName);
813	}
814}
815
816
817int
818pcap_lookupnet(device, netp, maskp, errbuf)
819	register const char *device;
820	register bpf_u_int32 *netp, *maskp;
821	register char *errbuf;
822{
823	/*
824	 * We need only the first IPv4 address, so we must scan the array returned by PacketGetNetInfo()
825	 * in order to skip non IPv4 (i.e. IPv6 addresses)
826	 */
827	npf_if_addr if_addrs[MAX_NETWORK_ADDRESSES];
828	LONG if_addr_size = 1;
829	struct sockaddr_in *t_addr;
830	unsigned int i;
831
832	if (!PacketGetNetInfoEx((void *)device, if_addrs, &if_addr_size)) {
833		*netp = *maskp = 0;
834		return (0);
835	}
836
837	for(i=0; i<MAX_NETWORK_ADDRESSES; i++)
838	{
839		if(if_addrs[i].IPAddress.ss_family == AF_INET)
840		{
841			t_addr = (struct sockaddr_in *) &(if_addrs[i].IPAddress);
842			*netp = t_addr->sin_addr.S_un.S_addr;
843			t_addr = (struct sockaddr_in *) &(if_addrs[i].SubnetMask);
844			*maskp = t_addr->sin_addr.S_un.S_addr;
845
846			*netp &= *maskp;
847			return (0);
848		}
849
850	}
851
852	*netp = *maskp = 0;
853	return (0);
854}
855
856#endif /* !WIN32 && !MSDOS */
857