Explicitly request literal mode after .Xr.
[netbsd-mini2440.git] / dist / libpcap / inet.c
blobe06d27a87c3947a9ae6543c0a90e3fca142e56a7
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: /pub/NetBSD/misc/repositories/cvsroot/src/dist/libpcap/inet.c,v 1.3 2009/09/29 19:00:45 bouyer 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 = malloc(strlen(name) + 1);
183 strcpy(curdev->name, name);
184 if (description != NULL) {
186 * We have a description for this interface.
188 curdev->description = malloc(strlen(description) + 1);
189 strcpy(curdev->description, description);
190 } else {
192 * We don't.
194 curdev->description = NULL;
196 curdev->addresses = NULL; /* list starts out as empty */
197 curdev->flags = 0;
198 if (ISLOOPBACK(name, flags))
199 curdev->flags |= PCAP_IF_LOOPBACK;
202 * Add it to the list, in the appropriate location.
203 * First, get the instance number of this interface.
205 this_instance = get_instance(name);
208 * Now look for the last interface with an instance number
209 * less than or equal to the new interface's instance
210 * number - except that non-loopback interfaces are
211 * arbitrarily treated as having interface numbers less
212 * than those of loopback interfaces, so the loopback
213 * interfaces are put at the end of the list.
215 * We start with "prevdev" being NULL, meaning we're before
216 * the first element in the list.
218 prevdev = NULL;
219 for (;;) {
221 * Get the interface after this one.
223 if (prevdev == NULL) {
225 * The next element is the first element.
227 nextdev = *alldevs;
228 } else
229 nextdev = prevdev->next;
232 * Are we at the end of the list?
234 if (nextdev == NULL) {
236 * Yes - we have to put the new entry
237 * after "prevdev".
239 break;
243 * Is the new interface a non-loopback interface
244 * and the next interface a loopback interface?
246 if (!(curdev->flags & PCAP_IF_LOOPBACK) &&
247 (nextdev->flags & PCAP_IF_LOOPBACK)) {
249 * Yes, we should put the new entry
250 * before "nextdev", i.e. after "prevdev".
252 break;
256 * Is the new interface's instance number less
257 * than the next interface's instance number,
258 * and is it the case that the new interface is a
259 * non-loopback interface or the next interface is
260 * a loopback interface?
262 * (The goal of both loopback tests is to make
263 * sure that we never put a loopback interface
264 * before any non-loopback interface and that we
265 * always put a non-loopback interface before all
266 * loopback interfaces.)
268 if (this_instance < get_instance(nextdev->name) &&
269 (!(curdev->flags & PCAP_IF_LOOPBACK) ||
270 (nextdev->flags & PCAP_IF_LOOPBACK))) {
272 * Yes - we should put the new entry
273 * before "nextdev", i.e. after "prevdev".
275 break;
278 prevdev = nextdev;
282 * Insert before "nextdev".
284 curdev->next = nextdev;
287 * Insert after "prevdev" - unless "prevdev" is null,
288 * in which case this is the first interface.
290 if (prevdev == NULL) {
292 * This is the first interface. Pass back a
293 * pointer to it, and put "curdev" before
294 * "nextdev".
296 *alldevs = curdev;
297 } else
298 prevdev->next = curdev;
301 *curdev_ret = curdev;
302 return (0);
306 add_addr_to_iflist(pcap_if_t **alldevs, const char *name, u_int flags,
307 struct sockaddr *addr, size_t addr_size,
308 struct sockaddr *netmask, size_t netmask_size,
309 struct sockaddr *broadaddr, size_t broadaddr_size,
310 struct sockaddr *dstaddr, size_t dstaddr_size,
311 char *errbuf)
313 pcap_if_t *curdev;
314 pcap_addr_t *curaddr, *prevaddr, *nextaddr;
316 if (add_or_find_if(&curdev, alldevs, name, flags, NULL, errbuf) == -1) {
318 * Error - give up.
320 return (-1);
322 if (curdev == NULL) {
324 * Device wasn't added because it can't be opened.
325 * Not a fatal error.
327 return (0);
331 * "curdev" is an entry for this interface; add an entry for this
332 * address to its list of addresses.
334 * Allocate the new entry and fill it in.
336 curaddr = malloc(sizeof(pcap_addr_t));
337 if (curaddr == NULL) {
338 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
339 "malloc: %s", pcap_strerror(errno));
340 return (-1);
343 curaddr->next = NULL;
344 if (addr != NULL) {
345 curaddr->addr = dup_sockaddr(addr, addr_size);
346 if (curaddr->addr == NULL) {
347 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
348 "malloc: %s", pcap_strerror(errno));
349 free(curaddr);
350 return (-1);
352 } else
353 curaddr->addr = NULL;
355 if (netmask != NULL) {
356 curaddr->netmask = dup_sockaddr(netmask, netmask_size);
357 if (curaddr->netmask == NULL) {
358 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
359 "malloc: %s", pcap_strerror(errno));
360 free(curaddr);
361 return (-1);
363 } else
364 curaddr->netmask = NULL;
366 if (broadaddr != NULL) {
367 curaddr->broadaddr = dup_sockaddr(broadaddr, broadaddr_size);
368 if (curaddr->broadaddr == NULL) {
369 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
370 "malloc: %s", pcap_strerror(errno));
371 free(curaddr);
372 return (-1);
374 } else
375 curaddr->broadaddr = NULL;
377 if (dstaddr != NULL) {
378 curaddr->dstaddr = dup_sockaddr(dstaddr, dstaddr_size);
379 if (curaddr->dstaddr == NULL) {
380 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
381 "malloc: %s", pcap_strerror(errno));
382 free(curaddr);
383 return (-1);
385 } else
386 curaddr->dstaddr = NULL;
389 * Find the end of the list of addresses.
391 for (prevaddr = curdev->addresses; prevaddr != NULL; prevaddr = nextaddr) {
392 nextaddr = prevaddr->next;
393 if (nextaddr == NULL) {
395 * This is the end of the list.
397 break;
401 if (prevaddr == NULL) {
403 * The list was empty; this is the first member.
405 curdev->addresses = curaddr;
406 } else {
408 * "prevaddr" is the last member of the list; append
409 * this member to it.
411 prevaddr->next = curaddr;
414 return (0);
418 pcap_add_if(pcap_if_t **devlist, const char *name, u_int flags,
419 const char *description, char *errbuf)
421 pcap_if_t *curdev;
423 return (add_or_find_if(&curdev, devlist, name, flags, description,
424 errbuf));
429 * Free a list of interfaces.
431 void
432 pcap_freealldevs(pcap_if_t *alldevs)
434 pcap_if_t *curdev, *nextdev;
435 pcap_addr_t *curaddr, *nextaddr;
437 for (curdev = alldevs; curdev != NULL; curdev = nextdev) {
438 nextdev = curdev->next;
441 * Free all addresses.
443 for (curaddr = curdev->addresses; curaddr != NULL; curaddr = nextaddr) {
444 nextaddr = curaddr->next;
445 if (curaddr->addr)
446 free(curaddr->addr);
447 if (curaddr->netmask)
448 free(curaddr->netmask);
449 if (curaddr->broadaddr)
450 free(curaddr->broadaddr);
451 if (curaddr->dstaddr)
452 free(curaddr->dstaddr);
453 free(curaddr);
457 * Free the name string.
459 free(curdev->name);
462 * Free the description string, if any.
464 if (curdev->description != NULL)
465 free(curdev->description);
468 * Free the interface.
470 free(curdev);
474 #if !defined(WIN32) && !defined(MSDOS)
477 * Return the name of a network interface attached to the system, or NULL
478 * if none can be found. The interface must be configured up; the
479 * lowest unit number is preferred; loopback is ignored.
481 char *
482 pcap_lookupdev(errbuf)
483 register char *errbuf;
485 pcap_if_t *alldevs;
486 /* for old BSD systems, including bsdi3 */
487 #ifndef IF_NAMESIZE
488 #define IF_NAMESIZE IFNAMSIZ
489 #endif
490 static char device[IF_NAMESIZE + 1];
491 char *ret;
493 if (pcap_findalldevs(&alldevs, errbuf) == -1)
494 return (NULL);
496 if (alldevs == NULL || (alldevs->flags & PCAP_IF_LOOPBACK)) {
498 * There are no devices on the list, or the first device
499 * on the list is a loopback device, which means there
500 * are no non-loopback devices on the list. This means
501 * we can't return any device.
503 * XXX - why not return a loopback device? If we can't
504 * capture on it, it won't be on the list, and if it's
505 * on the list, there aren't any non-loopback devices,
506 * so why not just supply it as the default device?
508 (void)strlcpy(errbuf, "no suitable device found",
509 PCAP_ERRBUF_SIZE);
510 ret = NULL;
511 } else {
513 * Return the name of the first device on the list.
515 (void)strlcpy(device, alldevs->name, sizeof(device));
516 ret = device;
519 pcap_freealldevs(alldevs);
520 return (ret);
524 pcap_lookupnet(device, netp, maskp, errbuf)
525 register const char *device;
526 register bpf_u_int32 *netp, *maskp;
527 register char *errbuf;
529 register int fd;
530 register struct sockaddr_in *sin4;
531 struct ifreq ifr;
534 * The pseudo-device "any" listens on all interfaces and therefore
535 * has the network address and -mask "0.0.0.0" therefore catching
536 * all traffic. Using NULL for the interface is the same as "any".
538 if (!device || strcmp(device, "any") == 0
539 #ifdef HAVE_DAG_API
540 || strstr(device, "dag") != NULL
541 #endif
542 #ifdef HAVE_SEPTEL_API
543 || strstr(device, "septel") != NULL
544 #endif
546 *netp = *maskp = 0;
547 return 0;
550 fd = socket(AF_INET, SOCK_DGRAM, 0);
551 if (fd < 0) {
552 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, "socket: %s",
553 pcap_strerror(errno));
554 return (-1);
556 memset(&ifr, 0, sizeof(ifr));
557 #ifdef linux
558 /* XXX Work around Linux kernel bug */
559 ifr.ifr_addr.sa_family = AF_INET;
560 #endif
561 (void)strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
562 if (ioctl(fd, SIOCGIFADDR, (char *)&ifr) < 0) {
563 if (errno == EADDRNOTAVAIL) {
564 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
565 "%s: no IPv4 address assigned", device);
566 } else {
567 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
568 "SIOCGIFADDR: %s: %s",
569 device, pcap_strerror(errno));
571 (void)close(fd);
572 return (-1);
574 sin4 = (struct sockaddr_in *)&ifr.ifr_addr;
575 *netp = sin4->sin_addr.s_addr;
576 memset(&ifr, 0, sizeof(ifr));
577 #ifdef linux
578 /* XXX Work around Linux kernel bug */
579 ifr.ifr_addr.sa_family = AF_INET;
580 #endif
581 (void)strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
582 if (ioctl(fd, SIOCGIFNETMASK, (char *)&ifr) < 0) {
583 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
584 "SIOCGIFNETMASK: %s: %s", device, pcap_strerror(errno));
585 (void)close(fd);
586 return (-1);
588 (void)close(fd);
589 *maskp = sin4->sin_addr.s_addr;
590 if (*maskp == 0) {
591 if (IN_CLASSA(*netp))
592 *maskp = IN_CLASSA_NET;
593 else if (IN_CLASSB(*netp))
594 *maskp = IN_CLASSB_NET;
595 else if (IN_CLASSC(*netp))
596 *maskp = IN_CLASSC_NET;
597 else {
598 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
599 "inet class for 0x%x unknown", *netp);
600 return (-1);
603 *netp &= *maskp;
604 return (0);
607 #elif defined(WIN32)
610 * Return the name of a network interface attached to the system, or NULL
611 * if none can be found. The interface must be configured up; the
612 * lowest unit number is preferred; loopback is ignored.
614 char *
615 pcap_lookupdev(errbuf)
616 register char *errbuf;
618 DWORD dwVersion;
619 DWORD dwWindowsMajorVersion;
620 dwVersion = GetVersion(); /* get the OS version */
621 dwWindowsMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion)));
623 if (dwVersion >= 0x80000000 && dwWindowsMajorVersion >= 4) {
625 * Windows 95, 98, ME.
627 ULONG NameLength = 8192;
628 static char AdaptersName[8192];
630 if (PacketGetAdapterNames(AdaptersName,&NameLength) )
631 return (AdaptersName);
632 else
633 return NULL;
634 } else {
636 * Windows NT (NT 4.0, W2K, WXP). Convert the names to UNICODE for backward compatibility
638 ULONG NameLength = 8192;
639 static WCHAR AdaptersName[8192];
640 char *tAstr;
641 WCHAR *tUstr;
642 WCHAR *TAdaptersName = (WCHAR*)malloc(8192 * sizeof(WCHAR));
643 int NAdapts = 0;
645 if(TAdaptersName == NULL)
647 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, "memory allocation failure");
648 return NULL;
651 if ( !PacketGetAdapterNames((PTSTR)TAdaptersName,&NameLength) )
653 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
654 "PacketGetAdapterNames: %s",
655 pcap_win32strerror());
656 free(TAdaptersName);
657 return NULL;
661 tAstr = (char*)TAdaptersName;
662 tUstr = (WCHAR*)AdaptersName;
665 * Convert and copy the device names
667 while(sscanf(tAstr, "%S", tUstr) > 0)
669 tAstr += strlen(tAstr) + 1;
670 tUstr += wcslen(tUstr) + 1;
671 NAdapts ++;
674 tAstr++;
675 *tUstr = 0;
676 tUstr++;
679 * Copy the descriptions
681 while(NAdapts--)
683 strcpy((char*)tUstr, tAstr);
684 (char*)tUstr += strlen(tAstr) + 1;;
685 tAstr += strlen(tAstr) + 1;
688 free(TAdaptersName);
689 return (char *)(AdaptersName);
695 pcap_lookupnet(device, netp, maskp, errbuf)
696 register const char *device;
697 register bpf_u_int32 *netp, *maskp;
698 register char *errbuf;
701 * We need only the first IPv4 address, so we must scan the array returned by PacketGetNetInfo()
702 * in order to skip non IPv4 (i.e. IPv6 addresses)
704 npf_if_addr if_addrs[MAX_NETWORK_ADDRESSES];
705 LONG if_addr_size = 1;
706 struct sockaddr_in *t_addr;
707 unsigned int i;
709 if (!PacketGetNetInfoEx((void *)device, if_addrs, &if_addr_size)) {
710 *netp = *maskp = 0;
711 return (0);
714 for(i=0; i<MAX_NETWORK_ADDRESSES; i++)
716 if(if_addrs[i].IPAddress.ss_family == AF_INET)
718 t_addr = (struct sockaddr_in *) &(if_addrs[i].IPAddress);
719 *netp = t_addr->sin_addr.S_un.S_addr;
720 t_addr = (struct sockaddr_in *) &(if_addrs[i].SubnetMask);
721 *maskp = t_addr->sin_addr.S_un.S_addr;
723 *netp &= *maskp;
724 return (0);
729 *netp = *maskp = 0;
730 return (0);
733 #endif /* !WIN32 && !MSDOS */