For bge_rxeof(), return immediately if no RX descs need to be processed, this
[dragonfly/port-amd64.git] / contrib / libpcap-0.9 / inet.c
blob792df35aebb881dcd080639261c8b15433703e70
1 /* -*- Mode: c; tab-width: 8; indent-tabs-mode: 1; c-basic-offset: 8; -*- */
2 /*
3 * Copyright (c) 1994, 1995, 1996, 1997, 1998
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by the Computer Systems
17 * Engineering Group at Lawrence Berkeley Laboratory.
18 * 4. Neither the name of the University nor of the Laboratory may be used
19 * to endorse or promote products derived from this software without
20 * specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
35 #ifndef lint
36 static const char rcsid[] _U_ =
37 "@(#) $Header: /tcpdump/master/libpcap/inet.c,v 1.66.2.2 2006/01/21 10:46:13 guy Exp $ (LBL)";
38 #endif
40 #ifdef HAVE_CONFIG_H
41 #include "config.h"
42 #endif
44 #ifdef WIN32
45 #include <pcap-stdinc.h>
46 #else /* WIN32 */
48 #include <sys/param.h>
49 #ifndef MSDOS
50 #include <sys/file.h>
51 #endif
52 #include <sys/ioctl.h>
53 #include <sys/socket.h>
54 #ifdef HAVE_SYS_SOCKIO_H
55 #include <sys/sockio.h>
56 #endif
58 struct mbuf; /* Squelch compiler warnings on some platforms for */
59 struct rtentry; /* declarations in <net/if.h> */
60 #include <net/if.h>
61 #include <netinet/in.h>
62 #endif /* WIN32 */
64 #include <ctype.h>
65 #include <errno.h>
66 #include <memory.h>
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <string.h>
70 #if !defined(WIN32) && !defined(__BORLANDC__)
71 #include <unistd.h>
72 #endif /* !WIN32 && !__BORLANDC__ */
73 #ifdef HAVE_LIMITS_H
74 #include <limits.h>
75 #else
76 #define INT_MAX 2147483647
77 #endif
79 #include "pcap-int.h"
81 #ifdef HAVE_OS_PROTO_H
82 #include "os-proto.h"
83 #endif
85 /* Not all systems have IFF_LOOPBACK */
86 #ifdef IFF_LOOPBACK
87 #define ISLOOPBACK(name, flags) ((flags) & IFF_LOOPBACK)
88 #else
89 #define ISLOOPBACK(name, flags) ((name)[0] == 'l' && (name)[1] == 'o' && \
90 (isdigit((unsigned char)((name)[2])) || (name)[2] == '\0'))
91 #endif
93 struct sockaddr *
94 dup_sockaddr(struct sockaddr *sa, size_t sa_length)
96 struct sockaddr *newsa;
98 if ((newsa = malloc(sa_length)) == NULL)
99 return (NULL);
100 return (memcpy(newsa, sa, sa_length));
103 static int
104 get_instance(const char *name)
106 const char *cp, *endcp;
107 int n;
109 if (strcmp(name, "any") == 0) {
111 * Give the "any" device an artificially high instance
112 * number, so it shows up after all other non-loopback
113 * interfaces.
115 return INT_MAX;
118 endcp = name + strlen(name);
119 for (cp = name; cp < endcp && !isdigit((unsigned char)*cp); ++cp)
120 continue;
122 if (isdigit((unsigned char)*cp))
123 n = atoi(cp);
124 else
125 n = 0;
126 return (n);
130 add_or_find_if(pcap_if_t **curdev_ret, pcap_if_t **alldevs, const char *name,
131 u_int flags, const char *description, char *errbuf)
133 pcap_t *p;
134 pcap_if_t *curdev, *prevdev, *nextdev;
135 int this_instance;
138 * Can we open this interface for live capture?
140 * We do this check so that interfaces that ae supplied
141 * by the interface enumeration mechanism we're using
142 * but that don't support packet capture aren't included
143 * in the list. An example of this is loopback interfaces
144 * on Solaris; we don't just omit loopback interfaces
145 * becaue you *can* capture on loopback interfaces on some
146 * OSes.
148 p = pcap_open_live(name, 68, 0, 0, errbuf);
149 if (p == NULL) {
151 * No. Don't bother including it.
152 * Don't treat this as an error, though.
154 *curdev_ret = NULL;
155 return (0);
157 pcap_close(p);
160 * Is there already an entry in the list for this interface?
162 for (curdev = *alldevs; curdev != NULL; curdev = curdev->next) {
163 if (strcmp(name, curdev->name) == 0)
164 break; /* yes, we found it */
166 if (curdev == NULL) {
168 * No, we didn't find it.
169 * Allocate a new entry.
171 curdev = malloc(sizeof(pcap_if_t));
172 if (curdev == NULL) {
173 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
174 "malloc: %s", pcap_strerror(errno));
175 return (-1);
179 * Fill in the entry.
181 curdev->next = NULL;
182 curdev->name = strdup(name);
183 if (curdev->name == NULL) {
184 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
185 "malloc: %s", pcap_strerror(errno));
186 free(curdev);
187 return (-1);
189 if (description != NULL) {
191 * We have a description for this interface.
193 curdev->description = strdup(description);
194 if (curdev->description == NULL) {
195 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
196 "malloc: %s", pcap_strerror(errno));
197 free(curdev->name);
198 free(curdev);
199 return (-1);
201 } else {
203 * We don't.
205 curdev->description = NULL;
207 curdev->addresses = NULL; /* list starts out as empty */
208 curdev->flags = 0;
209 if (ISLOOPBACK(name, flags))
210 curdev->flags |= PCAP_IF_LOOPBACK;
213 * Add it to the list, in the appropriate location.
214 * First, get the instance number of this interface.
216 this_instance = get_instance(name);
219 * Now look for the last interface with an instance number
220 * less than or equal to the new interface's instance
221 * number - except that non-loopback interfaces are
222 * arbitrarily treated as having interface numbers less
223 * than those of loopback interfaces, so the loopback
224 * interfaces are put at the end of the list.
226 * We start with "prevdev" being NULL, meaning we're before
227 * the first element in the list.
229 prevdev = NULL;
230 for (;;) {
232 * Get the interface after this one.
234 if (prevdev == NULL) {
236 * The next element is the first element.
238 nextdev = *alldevs;
239 } else
240 nextdev = prevdev->next;
243 * Are we at the end of the list?
245 if (nextdev == NULL) {
247 * Yes - we have to put the new entry
248 * after "prevdev".
250 break;
254 * Is the new interface a non-loopback interface
255 * and the next interface a loopback interface?
257 if (!(curdev->flags & PCAP_IF_LOOPBACK) &&
258 (nextdev->flags & PCAP_IF_LOOPBACK)) {
260 * Yes, we should put the new entry
261 * before "nextdev", i.e. after "prevdev".
263 break;
267 * Is the new interface's instance number less
268 * than the next interface's instance number,
269 * and is it the case that the new interface is a
270 * non-loopback interface or the next interface is
271 * a loopback interface?
273 * (The goal of both loopback tests is to make
274 * sure that we never put a loopback interface
275 * before any non-loopback interface and that we
276 * always put a non-loopback interface before all
277 * loopback interfaces.)
279 if (this_instance < get_instance(nextdev->name) &&
280 (!(curdev->flags & PCAP_IF_LOOPBACK) ||
281 (nextdev->flags & PCAP_IF_LOOPBACK))) {
283 * Yes - we should put the new entry
284 * before "nextdev", i.e. after "prevdev".
286 break;
289 prevdev = nextdev;
293 * Insert before "nextdev".
295 curdev->next = nextdev;
298 * Insert after "prevdev" - unless "prevdev" is null,
299 * in which case this is the first interface.
301 if (prevdev == NULL) {
303 * This is the first interface. Pass back a
304 * pointer to it, and put "curdev" before
305 * "nextdev".
307 *alldevs = curdev;
308 } else
309 prevdev->next = curdev;
312 *curdev_ret = curdev;
313 return (0);
317 add_addr_to_iflist(pcap_if_t **alldevs, const char *name, u_int flags,
318 struct sockaddr *addr, size_t addr_size,
319 struct sockaddr *netmask, size_t netmask_size,
320 struct sockaddr *broadaddr, size_t broadaddr_size,
321 struct sockaddr *dstaddr, size_t dstaddr_size,
322 char *errbuf)
324 pcap_if_t *curdev;
325 pcap_addr_t *curaddr, *prevaddr, *nextaddr;
327 if (add_or_find_if(&curdev, alldevs, name, flags, NULL, errbuf) == -1) {
329 * Error - give up.
331 return (-1);
333 if (curdev == NULL) {
335 * Device wasn't added because it can't be opened.
336 * Not a fatal error.
338 return (0);
342 * "curdev" is an entry for this interface; add an entry for this
343 * address to its list of addresses.
345 * Allocate the new entry and fill it in.
347 curaddr = malloc(sizeof(pcap_addr_t));
348 if (curaddr == NULL) {
349 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
350 "malloc: %s", pcap_strerror(errno));
351 return (-1);
354 curaddr->next = NULL;
355 if (addr != NULL) {
356 curaddr->addr = dup_sockaddr(addr, addr_size);
357 if (curaddr->addr == NULL) {
358 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
359 "malloc: %s", pcap_strerror(errno));
360 free(curaddr);
361 return (-1);
363 } else
364 curaddr->addr = NULL;
366 if (netmask != NULL) {
367 curaddr->netmask = dup_sockaddr(netmask, netmask_size);
368 if (curaddr->netmask == NULL) {
369 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
370 "malloc: %s", pcap_strerror(errno));
371 if (curaddr->addr != NULL)
372 free(curaddr->addr);
373 free(curaddr);
374 return (-1);
376 } else
377 curaddr->netmask = NULL;
379 if (broadaddr != NULL) {
380 curaddr->broadaddr = dup_sockaddr(broadaddr, broadaddr_size);
381 if (curaddr->broadaddr == NULL) {
382 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
383 "malloc: %s", pcap_strerror(errno));
384 if (curaddr->netmask != NULL)
385 free(curaddr->netmask);
386 if (curaddr->addr != NULL)
387 free(curaddr->addr);
388 free(curaddr);
389 return (-1);
391 } else
392 curaddr->broadaddr = NULL;
394 if (dstaddr != NULL) {
395 curaddr->dstaddr = dup_sockaddr(dstaddr, dstaddr_size);
396 if (curaddr->dstaddr == NULL) {
397 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
398 "malloc: %s", pcap_strerror(errno));
399 if (curaddr->broadaddr != NULL)
400 free(curaddr->broadaddr);
401 if (curaddr->netmask != NULL)
402 free(curaddr->netmask);
403 if (curaddr->addr != NULL)
404 free(curaddr->addr);
405 free(curaddr);
406 return (-1);
408 } else
409 curaddr->dstaddr = NULL;
412 * Find the end of the list of addresses.
414 for (prevaddr = curdev->addresses; prevaddr != NULL; prevaddr = nextaddr) {
415 nextaddr = prevaddr->next;
416 if (nextaddr == NULL) {
418 * This is the end of the list.
420 break;
424 if (prevaddr == NULL) {
426 * The list was empty; this is the first member.
428 curdev->addresses = curaddr;
429 } else {
431 * "prevaddr" is the last member of the list; append
432 * this member to it.
434 prevaddr->next = curaddr;
437 return (0);
441 pcap_add_if(pcap_if_t **devlist, const char *name, u_int flags,
442 const char *description, char *errbuf)
444 pcap_if_t *curdev;
446 return (add_or_find_if(&curdev, devlist, name, flags, description,
447 errbuf));
452 * Free a list of interfaces.
454 void
455 pcap_freealldevs(pcap_if_t *alldevs)
457 pcap_if_t *curdev, *nextdev;
458 pcap_addr_t *curaddr, *nextaddr;
460 for (curdev = alldevs; curdev != NULL; curdev = nextdev) {
461 nextdev = curdev->next;
464 * Free all addresses.
466 for (curaddr = curdev->addresses; curaddr != NULL; curaddr = nextaddr) {
467 nextaddr = curaddr->next;
468 if (curaddr->addr)
469 free(curaddr->addr);
470 if (curaddr->netmask)
471 free(curaddr->netmask);
472 if (curaddr->broadaddr)
473 free(curaddr->broadaddr);
474 if (curaddr->dstaddr)
475 free(curaddr->dstaddr);
476 free(curaddr);
480 * Free the name string.
482 free(curdev->name);
485 * Free the description string, if any.
487 if (curdev->description != NULL)
488 free(curdev->description);
491 * Free the interface.
493 free(curdev);
497 #if !defined(WIN32) && !defined(MSDOS)
500 * Return the name of a network interface attached to the system, or NULL
501 * if none can be found. The interface must be configured up; the
502 * lowest unit number is preferred; loopback is ignored.
504 char *
505 pcap_lookupdev(errbuf)
506 register char *errbuf;
508 pcap_if_t *alldevs;
509 /* for old BSD systems, including bsdi3 */
510 #ifndef IF_NAMESIZE
511 #define IF_NAMESIZE IFNAMSIZ
512 #endif
513 static char device[IF_NAMESIZE + 1];
514 char *ret;
516 if (pcap_findalldevs(&alldevs, errbuf) == -1)
517 return (NULL);
519 if (alldevs == NULL || (alldevs->flags & PCAP_IF_LOOPBACK)) {
521 * There are no devices on the list, or the first device
522 * on the list is a loopback device, which means there
523 * are no non-loopback devices on the list. This means
524 * we can't return any device.
526 * XXX - why not return a loopback device? If we can't
527 * capture on it, it won't be on the list, and if it's
528 * on the list, there aren't any non-loopback devices,
529 * so why not just supply it as the default device?
531 (void)strlcpy(errbuf, "no suitable device found",
532 PCAP_ERRBUF_SIZE);
533 ret = NULL;
534 } else {
536 * Return the name of the first device on the list.
538 (void)strlcpy(device, alldevs->name, sizeof(device));
539 ret = device;
542 pcap_freealldevs(alldevs);
543 return (ret);
547 pcap_lookupnet(device, netp, maskp, errbuf)
548 register const char *device;
549 register bpf_u_int32 *netp, *maskp;
550 register char *errbuf;
552 register int fd;
553 register struct sockaddr_in *sin;
554 struct ifreq ifr;
557 * The pseudo-device "any" listens on all interfaces and therefore
558 * has the network address and -mask "0.0.0.0" therefore catching
559 * all traffic. Using NULL for the interface is the same as "any".
561 if (!device || strcmp(device, "any") == 0
562 #ifdef HAVE_DAG_API
563 || strstr(device, "dag") != NULL
564 #endif
565 #ifdef HAVE_SEPTEL_API
566 || strstr(device, "septel") != NULL
567 #endif
569 *netp = *maskp = 0;
570 return 0;
573 fd = socket(AF_INET, SOCK_DGRAM, 0);
574 if (fd < 0) {
575 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, "socket: %s",
576 pcap_strerror(errno));
577 return (-1);
579 memset(&ifr, 0, sizeof(ifr));
580 #ifdef linux
581 /* XXX Work around Linux kernel bug */
582 ifr.ifr_addr.sa_family = AF_INET;
583 #endif
584 (void)strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
585 if (ioctl(fd, SIOCGIFADDR, (char *)&ifr) < 0) {
586 if (errno == EADDRNOTAVAIL) {
587 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
588 "%s: no IPv4 address assigned", device);
589 } else {
590 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
591 "SIOCGIFADDR: %s: %s",
592 device, pcap_strerror(errno));
594 (void)close(fd);
595 return (-1);
597 sin = (struct sockaddr_in *)&ifr.ifr_addr;
598 *netp = sin->sin_addr.s_addr;
599 if (ioctl(fd, SIOCGIFNETMASK, (char *)&ifr) < 0) {
600 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
601 "SIOCGIFNETMASK: %s: %s", device, pcap_strerror(errno));
602 (void)close(fd);
603 return (-1);
605 (void)close(fd);
606 *maskp = sin->sin_addr.s_addr;
607 if (*maskp == 0) {
608 if (IN_CLASSA(*netp))
609 *maskp = IN_CLASSA_NET;
610 else if (IN_CLASSB(*netp))
611 *maskp = IN_CLASSB_NET;
612 else if (IN_CLASSC(*netp))
613 *maskp = IN_CLASSC_NET;
614 else {
615 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
616 "inet class for 0x%x unknown", *netp);
617 return (-1);
620 *netp &= *maskp;
621 return (0);
624 #elif defined(WIN32)
627 * Return the name of a network interface attached to the system, or NULL
628 * if none can be found. The interface must be configured up; the
629 * lowest unit number is preferred; loopback is ignored.
631 char *
632 pcap_lookupdev(errbuf)
633 register char *errbuf;
635 DWORD dwVersion;
636 DWORD dwWindowsMajorVersion;
637 dwVersion = GetVersion(); /* get the OS version */
638 dwWindowsMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion)));
640 if (dwVersion >= 0x80000000 && dwWindowsMajorVersion >= 4) {
642 * Windows 95, 98, ME.
644 ULONG NameLength = 8192;
645 static char AdaptersName[8192];
647 if (PacketGetAdapterNames(AdaptersName,&NameLength) )
648 return (AdaptersName);
649 else
650 return NULL;
651 } else {
653 * Windows NT (NT 4.0, W2K, WXP). Convert the names to UNICODE for backward compatibility
655 ULONG NameLength = 8192;
656 static WCHAR AdaptersName[8192];
657 char *tAstr;
658 WCHAR *tUstr;
659 WCHAR *TAdaptersName = (WCHAR*)malloc(8192 * sizeof(WCHAR));
660 int NAdapts = 0;
662 if(TAdaptersName == NULL)
664 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, "memory allocation failure");
665 return NULL;
668 if ( !PacketGetAdapterNames((PTSTR)TAdaptersName,&NameLength) )
670 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
671 "PacketGetAdapterNames: %s",
672 pcap_win32strerror());
673 free(TAdaptersName);
674 return NULL;
678 tAstr = (char*)TAdaptersName;
679 tUstr = (WCHAR*)AdaptersName;
682 * Convert and copy the device names
684 while(sscanf(tAstr, "%S", tUstr) > 0)
686 tAstr += strlen(tAstr) + 1;
687 tUstr += wcslen(tUstr) + 1;
688 NAdapts ++;
691 tAstr++;
692 *tUstr = 0;
693 tUstr++;
696 * Copy the descriptions
698 while(NAdapts--)
700 strcpy((char*)tUstr, tAstr);
701 (char*)tUstr += strlen(tAstr) + 1;;
702 tAstr += strlen(tAstr) + 1;
705 free(TAdaptersName);
706 return (char *)(AdaptersName);
712 pcap_lookupnet(device, netp, maskp, errbuf)
713 register const char *device;
714 register bpf_u_int32 *netp, *maskp;
715 register char *errbuf;
718 * We need only the first IPv4 address, so we must scan the array returned by PacketGetNetInfo()
719 * in order to skip non IPv4 (i.e. IPv6 addresses)
721 npf_if_addr if_addrs[MAX_NETWORK_ADDRESSES];
722 LONG if_addr_size = 1;
723 struct sockaddr_in *t_addr;
724 unsigned int i;
726 if (!PacketGetNetInfoEx((void *)device, if_addrs, &if_addr_size)) {
727 *netp = *maskp = 0;
728 return (0);
731 for(i=0; i<MAX_NETWORK_ADDRESSES; i++)
733 if(if_addrs[i].IPAddress.ss_family == AF_INET)
735 t_addr = (struct sockaddr_in *) &(if_addrs[i].IPAddress);
736 *netp = t_addr->sin_addr.S_un.S_addr;
737 t_addr = (struct sockaddr_in *) &(if_addrs[i].SubnetMask);
738 *maskp = t_addr->sin_addr.S_un.S_addr;
740 *netp &= *maskp;
741 return (0);
746 *netp = *maskp = 0;
747 return (0);
750 #endif /* !WIN32 && !MSDOS */