Merge commit 'crater/master'
[dragonfly.git] / contrib / libpcap / inet.c
blobaad87963e78684ca00a5ce98325e09e7140958c3
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.75.2.4 2008-04-20 18:19:24 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 * Is there already an entry in the list for this interface?
140 for (curdev = *alldevs; curdev != NULL; curdev = curdev->next) {
141 if (strcmp(name, curdev->name) == 0)
142 break; /* yes, we found it */
145 if (curdev == NULL) {
147 * No, we didn't find it.
149 * Can we open this interface for live capture?
151 * We do this check so that interfaces that are
152 * supplied by the interface enumeration mechanism
153 * we're using but that don't support packet capture
154 * aren't included in the list. Loopback interfaces
155 * on Solaris are an example of this; we don't just
156 * omit loopback interfaces on all platforms because
157 * you *can* capture on loopback interfaces on some
158 * OSes.
160 * On OS X, we don't do this check if the device
161 * name begins with "wlt"; at least some versions
162 * of OS X offer monitor mode capturing by having
163 * a separate "monitor mode" device for each wireless
164 * adapter, rather than by implementing the ioctls
165 * that {Free,Net,Open,DragonFly}BSD provide.
166 * Opening that device puts the adapter into monitor
167 * mode, which, at least for some adapters, causes
168 * them to deassociate from the network with which
169 * they're associated.
171 * Instead, we try to open the corresponding "en"
172 * device (so that we don't end up with, for users
173 * without sufficient privilege to open capture
174 * devices, a list of adapters that only includes
175 * the wlt devices).
177 #ifdef __APPLE__
178 if (strncmp(name, "wlt", 3) == 0) {
179 char *en_name;
180 size_t en_name_len;
183 * Try to allocate a buffer for the "en"
184 * device's name.
186 en_name_len = strlen(name) - 1;
187 en_name = malloc(en_name_len + 1);
188 if (en_name == NULL) {
189 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
190 "malloc: %s", pcap_strerror(errno));
191 return (-1);
193 strcpy(en_name, "en");
194 strcat(en_name, name + 3);
195 p = pcap_open_live(en_name, 68, 0, 0, errbuf);
196 free(en_name);
197 } else
198 #endif /* __APPLE */
199 p = pcap_open_live(name, 68, 0, 0, errbuf);
200 if (p == NULL) {
202 * No. Don't bother including it.
203 * Don't treat this as an error, though.
205 *curdev_ret = NULL;
206 return (0);
208 pcap_close(p);
211 * Yes, we can open it.
212 * Allocate a new entry.
214 curdev = malloc(sizeof(pcap_if_t));
215 if (curdev == NULL) {
216 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
217 "malloc: %s", pcap_strerror(errno));
218 return (-1);
222 * Fill in the entry.
224 curdev->next = NULL;
225 curdev->name = strdup(name);
226 if (curdev->name == NULL) {
227 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
228 "malloc: %s", pcap_strerror(errno));
229 free(curdev);
230 return (-1);
232 if (description != NULL) {
234 * We have a description for this interface.
236 curdev->description = strdup(description);
237 if (curdev->description == NULL) {
238 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
239 "malloc: %s", pcap_strerror(errno));
240 free(curdev->name);
241 free(curdev);
242 return (-1);
244 } else {
246 * We don't.
248 curdev->description = NULL;
250 curdev->addresses = NULL; /* list starts out as empty */
251 curdev->flags = 0;
252 if (ISLOOPBACK(name, flags))
253 curdev->flags |= PCAP_IF_LOOPBACK;
256 * Add it to the list, in the appropriate location.
257 * First, get the instance number of this interface.
259 this_instance = get_instance(name);
262 * Now look for the last interface with an instance number
263 * less than or equal to the new interface's instance
264 * number - except that non-loopback interfaces are
265 * arbitrarily treated as having interface numbers less
266 * than those of loopback interfaces, so the loopback
267 * interfaces are put at the end of the list.
269 * We start with "prevdev" being NULL, meaning we're before
270 * the first element in the list.
272 prevdev = NULL;
273 for (;;) {
275 * Get the interface after this one.
277 if (prevdev == NULL) {
279 * The next element is the first element.
281 nextdev = *alldevs;
282 } else
283 nextdev = prevdev->next;
286 * Are we at the end of the list?
288 if (nextdev == NULL) {
290 * Yes - we have to put the new entry
291 * after "prevdev".
293 break;
297 * Is the new interface a non-loopback interface
298 * and the next interface a loopback interface?
300 if (!(curdev->flags & PCAP_IF_LOOPBACK) &&
301 (nextdev->flags & PCAP_IF_LOOPBACK)) {
303 * Yes, we should put the new entry
304 * before "nextdev", i.e. after "prevdev".
306 break;
310 * Is the new interface's instance number less
311 * than the next interface's instance number,
312 * and is it the case that the new interface is a
313 * non-loopback interface or the next interface is
314 * a loopback interface?
316 * (The goal of both loopback tests is to make
317 * sure that we never put a loopback interface
318 * before any non-loopback interface and that we
319 * always put a non-loopback interface before all
320 * loopback interfaces.)
322 if (this_instance < get_instance(nextdev->name) &&
323 (!(curdev->flags & PCAP_IF_LOOPBACK) ||
324 (nextdev->flags & PCAP_IF_LOOPBACK))) {
326 * Yes - we should put the new entry
327 * before "nextdev", i.e. after "prevdev".
329 break;
332 prevdev = nextdev;
336 * Insert before "nextdev".
338 curdev->next = nextdev;
341 * Insert after "prevdev" - unless "prevdev" is null,
342 * in which case this is the first interface.
344 if (prevdev == NULL) {
346 * This is the first interface. Pass back a
347 * pointer to it, and put "curdev" before
348 * "nextdev".
350 *alldevs = curdev;
351 } else
352 prevdev->next = curdev;
355 *curdev_ret = curdev;
356 return (0);
360 * XXX - on FreeBSDs that support it, should it get the sysctl named
361 * "dev.{adapter family name}.{adapter unit}.%desc" to get a description
362 * of the adapter? Note that "dev.an.0.%desc" is "Aironet PC4500/PC4800"
363 * with my Cisco 350 card, so the name isn't entirely descriptive. The
364 * "dev.an.0.%pnpinfo" has a better description, although one might argue
365 * that the problem is really a driver bug - if it can find out that it's
366 * a Cisco 340 or 350, rather than an old Aironet card, it should use
367 * that in the description.
369 * Do NetBSD, DragonflyBSD, or OpenBSD support this as well? OpenBSD
370 * lets you get a description, but it's not generated by the OS, it's
371 * set with another ioctl that ifconfig supports; we use that to get
372 * the description in OpenBSD.
374 * In OS X, the System Configuration framework can apparently return
375 * names in 10.4 and later; it also appears that freedesktop.org's HAL
376 * offers an "info.product" string, but the HAL specification says
377 * it "should not be used in any UI" and "subsystem/capability
378 * specific properties" should be used instead. Using that would
379 * require that libpcap applications be linked with the frameworks/
380 * libraries in question, which would be a bit of a pain unless we
381 * offer, for example, a pkg-config:
383 * http://pkg-config.freedesktop.org/wiki/
385 * script, so applications can just use that script to find out what
386 * libraries you need to link with when linking with libpcap.
387 * pkg-config is GPLed; I don't know whether that would prevent its
388 * use with a BSD-licensed library such as libpcap.
390 * Do any other UN*Xes, or desktop environments support getting a
391 * description?
394 add_addr_to_iflist(pcap_if_t **alldevs, const char *name, u_int flags,
395 struct sockaddr *addr, size_t addr_size,
396 struct sockaddr *netmask, size_t netmask_size,
397 struct sockaddr *broadaddr, size_t broadaddr_size,
398 struct sockaddr *dstaddr, size_t dstaddr_size,
399 char *errbuf)
401 pcap_if_t *curdev;
402 char *description = NULL;
403 pcap_addr_t *curaddr, *prevaddr, *nextaddr;
404 #ifdef SIOCGIFDESCR
405 struct ifreq ifrdesc;
406 char ifdescr[IFDESCRSIZE];
407 int s;
408 #endif
410 #ifdef SIOCGIFDESCR
412 * Get the description for the interface.
414 memset(&ifrdesc, 0, sizeof ifrdesc);
415 strlcpy(ifrdesc.ifr_name, name, sizeof ifrdesc.ifr_name);
416 ifrdesc.ifr_data = (caddr_t)&ifdescr;
417 s = socket(AF_INET, SOCK_DGRAM, 0);
418 if (s >= 0) {
419 if (ioctl(s, SIOCGIFDESCR, &ifrdesc) == 0 &&
420 strlen(ifrdesc.ifr_data) != 0)
421 description = ifrdesc.ifr_data;
422 close(s);
424 #endif
426 if (add_or_find_if(&curdev, alldevs, name, flags, description,
427 errbuf) == -1) {
429 * Error - give up.
431 return (-1);
433 if (curdev == NULL) {
435 * Device wasn't added because it can't be opened.
436 * Not a fatal error.
438 return (0);
442 * "curdev" is an entry for this interface; add an entry for this
443 * address to its list of addresses.
445 * Allocate the new entry and fill it in.
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);
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);
463 } else
464 curaddr->addr = NULL;
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);
476 } else
477 curaddr->netmask = NULL;
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);
491 } else
492 curaddr->broadaddr = NULL;
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);
508 } else
509 curaddr->dstaddr = NULL;
512 * Find the end of the list of addresses.
514 for (prevaddr = curdev->addresses; prevaddr != NULL; prevaddr = nextaddr) {
515 nextaddr = prevaddr->next;
516 if (nextaddr == NULL) {
518 * This is the end of the list.
520 break;
524 if (prevaddr == NULL) {
526 * The list was empty; this is the first member.
528 curdev->addresses = curaddr;
529 } else {
531 * "prevaddr" is the last member of the list; append
532 * this member to it.
534 prevaddr->next = curaddr;
537 return (0);
541 pcap_add_if(pcap_if_t **devlist, const char *name, u_int flags,
542 const char *description, char *errbuf)
544 pcap_if_t *curdev;
546 return (add_or_find_if(&curdev, devlist, name, flags, description,
547 errbuf));
552 * Free a list of interfaces.
554 void
555 pcap_freealldevs(pcap_if_t *alldevs)
557 pcap_if_t *curdev, *nextdev;
558 pcap_addr_t *curaddr, *nextaddr;
560 for (curdev = alldevs; curdev != NULL; curdev = nextdev) {
561 nextdev = curdev->next;
564 * Free all addresses.
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);
580 * Free the name string.
582 free(curdev->name);
585 * Free the description string, if any.
587 if (curdev->description != NULL)
588 free(curdev->description);
591 * Free the interface.
593 free(curdev);
597 #if !defined(WIN32) && !defined(MSDOS)
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.
604 char *
605 pcap_lookupdev(errbuf)
606 register char *errbuf;
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;
616 if (pcap_findalldevs(&alldevs, errbuf) == -1)
617 return (NULL);
619 if (alldevs == NULL || (alldevs->flags & PCAP_IF_LOOPBACK)) {
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.
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?
631 (void)strlcpy(errbuf, "no suitable device found",
632 PCAP_ERRBUF_SIZE);
633 ret = NULL;
634 } else {
636 * Return the name of the first device on the list.
638 (void)strlcpy(device, alldevs->name, sizeof(device));
639 ret = device;
642 pcap_freealldevs(alldevs);
643 return (ret);
647 pcap_lookupnet(device, netp, maskp, errbuf)
648 register const char *device;
649 register bpf_u_int32 *netp, *maskp;
650 register char *errbuf;
652 register int fd;
653 register struct sockaddr_in *sin4;
654 struct ifreq ifr;
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".
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
675 *netp = *maskp = 0;
676 return 0;
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);
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));
700 (void)close(fd);
701 return (-1);
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);
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);
726 *netp &= *maskp;
727 return (0);
730 #elif defined(WIN32)
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.
737 char *
738 pcap_lookupdev(errbuf)
739 register char *errbuf;
741 DWORD dwVersion;
742 DWORD dwWindowsMajorVersion;
743 dwVersion = GetVersion(); /* get the OS version */
744 dwWindowsMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion)));
746 if (dwVersion >= 0x80000000 && dwWindowsMajorVersion >= 4) {
748 * Windows 95, 98, ME.
750 ULONG NameLength = 8192;
751 static char AdaptersName[8192];
753 if (PacketGetAdapterNames(AdaptersName,&NameLength) )
754 return (AdaptersName);
755 else
756 return NULL;
757 } else {
759 * Windows NT (NT 4.0, W2K, WXP). Convert the names to UNICODE for backward compatibility
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;
768 if(TAdaptersName == NULL)
770 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, "memory allocation failure");
771 return NULL;
774 if ( !PacketGetAdapterNames((PTSTR)TAdaptersName,&NameLength) )
776 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
777 "PacketGetAdapterNames: %s",
778 pcap_win32strerror());
779 free(TAdaptersName);
780 return NULL;
784 tAstr = (char*)TAdaptersName;
785 tUstr = (WCHAR*)AdaptersName;
788 * Convert and copy the device names
790 while(sscanf(tAstr, "%S", tUstr) > 0)
792 tAstr += strlen(tAstr) + 1;
793 tUstr += wcslen(tUstr) + 1;
794 NAdapts ++;
797 tAstr++;
798 *tUstr = 0;
799 tUstr++;
802 * Copy the descriptions
804 while(NAdapts--)
806 strcpy((char*)tUstr, tAstr);
807 (char*)tUstr += strlen(tAstr) + 1;;
808 tAstr += strlen(tAstr) + 1;
811 free(TAdaptersName);
812 return (char *)(AdaptersName);
818 pcap_lookupnet(device, netp, maskp, errbuf)
819 register const char *device;
820 register bpf_u_int32 *netp, *maskp;
821 register char *errbuf;
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)
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;
832 if (!PacketGetNetInfoEx((void *)device, if_addrs, &if_addr_size)) {
833 *netp = *maskp = 0;
834 return (0);
837 for(i=0; i<MAX_NETWORK_ADDRESSES; i++)
839 if(if_addrs[i].IPAddress.ss_family == AF_INET)
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;
846 *netp &= *maskp;
847 return (0);
852 *netp = *maskp = 0;
853 return (0);
856 #endif /* !WIN32 && !MSDOS */