1 /* -*- Mode: c; tab-width: 8; indent-tabs-mode: 1; c-basic-offset: 8; -*- */
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
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
40 #include <pcap-stdinc.h>
43 #include <sys/param.h>
47 #include <sys/ioctl.h>
48 #include <sys/socket.h>
49 #ifdef HAVE_SYS_SOCKIO_H
50 #include <sys/sockio.h>
53 struct mbuf
; /* Squelch compiler warnings on some platforms for */
54 struct rtentry
; /* declarations in <net/if.h> */
56 #include <netinet/in.h>
65 #if !defined(WIN32) && !defined(__BORLANDC__)
67 #endif /* !WIN32 && !__BORLANDC__ */
71 #define INT_MAX 2147483647
76 #ifdef HAVE_OS_PROTO_H
80 /* Not all systems have IFF_LOOPBACK */
82 #define ISLOOPBACK(name, flags) ((flags) & IFF_LOOPBACK)
84 #define ISLOOPBACK(name, flags) ((name)[0] == 'l' && (name)[1] == 'o' && \
85 (isdigit((unsigned char)((name)[2])) || (name)[2] == '\0'))
89 #define ISUP(flags) ((flags) & IFF_UP)
95 #define ISRUNNING(flags) ((flags) & IFF_RUNNING)
97 #define ISRUNNING(flags) 0
101 dup_sockaddr(struct sockaddr
*sa
, size_t sa_length
)
103 struct sockaddr
*newsa
;
105 if ((newsa
= malloc(sa_length
)) == NULL
)
107 return (memcpy(newsa
, sa
, sa_length
));
111 * Construct a "figure of merit" for an interface, for use when sorting
112 * the list of interfaces, in which interfaces that are up are superior
113 * to interfaces that aren't up, interfaces that are up and running are
114 * superior to interfaces that are up but not running, and non-loopback
115 * interfaces that are up and running are superior to loopback interfaces,
116 * and interfaces with the same flags have a figure of merit that's higher
117 * the lower the instance number.
119 * The goal is to try to put the interfaces most likely to be useful for
120 * capture at the beginning of the list.
122 * The figure of merit, which is lower the "better" the interface is,
123 * has the uppermost bit set if the interface isn't running, the bit
124 * below that set if the interface isn't up, the bit below that set
125 * if the interface is a loopback interface, and the interface index
126 * in the 29 bits below that. (Yes, we assume u_int is 32 bits.)
129 get_figure_of_merit(pcap_if_t
*dev
)
131 const char *cp
, *endcp
;
134 if (strcmp(dev
->name
, "any") == 0) {
136 * Give the "any" device an artificially high instance
137 * number, so it shows up after all other non-loopback
140 n
= 0x1FFFFFFF; /* 29 all-1 bits */
143 endcp
= dev
->name
+ strlen(dev
->name
);
144 for (cp
= dev
->name
; cp
< endcp
&& !isdigit((unsigned char)*cp
); ++cp
)
147 if (isdigit((unsigned char)*cp
))
151 if (!(dev
->flags
& PCAP_IF_RUNNING
))
153 if (!(dev
->flags
& PCAP_IF_UP
))
155 if (dev
->flags
& PCAP_IF_LOOPBACK
)
161 add_or_find_if(pcap_if_t
**curdev_ret
, pcap_if_t
**alldevs
, const char *name
,
162 u_int flags
, const char *description
, char *errbuf
)
165 pcap_if_t
*curdev
, *prevdev
, *nextdev
;
166 u_int this_figure_of_merit
, nextdev_figure_of_merit
;
167 char open_errbuf
[PCAP_ERRBUF_SIZE
];
170 * Is there already an entry in the list for this interface?
172 for (curdev
= *alldevs
; curdev
!= NULL
; curdev
= curdev
->next
) {
173 if (strcmp(name
, curdev
->name
) == 0)
174 break; /* yes, we found it */
177 if (curdev
== NULL
) {
179 * No, we didn't find it.
181 * Can we open this interface for live capture?
183 * We do this check so that interfaces that are
184 * supplied by the interface enumeration mechanism
185 * we're using but that don't support packet capture
186 * aren't included in the list. Loopback interfaces
187 * on Solaris are an example of this; we don't just
188 * omit loopback interfaces on all platforms because
189 * you *can* capture on loopback interfaces on some
192 * On OS X, we don't do this check if the device
193 * name begins with "wlt"; at least some versions
194 * of OS X offer monitor mode capturing by having
195 * a separate "monitor mode" device for each wireless
196 * adapter, rather than by implementing the ioctls
197 * that {Free,Net,Open,DragonFly}BSD provide.
198 * Opening that device puts the adapter into monitor
199 * mode, which, at least for some adapters, causes
200 * them to deassociate from the network with which
201 * they're associated.
203 * Instead, we try to open the corresponding "en"
204 * device (so that we don't end up with, for users
205 * without sufficient privilege to open capture
206 * devices, a list of adapters that only includes
210 if (strncmp(name
, "wlt", 3) == 0) {
215 * Try to allocate a buffer for the "en"
218 en_name_len
= strlen(name
) - 1;
219 en_name
= malloc(en_name_len
+ 1);
220 if (en_name
== NULL
) {
221 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
222 "malloc: %s", pcap_strerror(errno
));
225 strcpy(en_name
, "en");
226 strcat(en_name
, name
+ 3);
227 p
= pcap_open_live(en_name
, 68, 0, 0, open_errbuf
);
231 p
= pcap_open_live(name
, 68, 0, 0, open_errbuf
);
234 * No. Don't bother including it.
235 * Don't treat this as an error, though.
243 * Yes, we can open it.
244 * Allocate a new entry.
246 curdev
= malloc(sizeof(pcap_if_t
));
247 if (curdev
== NULL
) {
248 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
249 "malloc: %s", pcap_strerror(errno
));
257 curdev
->name
= strdup(name
);
258 if (curdev
->name
== NULL
) {
259 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
260 "malloc: %s", pcap_strerror(errno
));
264 if (description
!= NULL
) {
266 * We have a description for this interface.
268 curdev
->description
= strdup(description
);
269 if (curdev
->description
== NULL
) {
270 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
271 "malloc: %s", pcap_strerror(errno
));
280 curdev
->description
= NULL
;
282 curdev
->addresses
= NULL
; /* list starts out as empty */
284 if (ISLOOPBACK(name
, flags
))
285 curdev
->flags
|= PCAP_IF_LOOPBACK
;
287 curdev
->flags
|= PCAP_IF_UP
;
288 if (ISRUNNING(flags
))
289 curdev
->flags
|= PCAP_IF_RUNNING
;
292 * Add it to the list, in the appropriate location.
293 * First, get the "figure of merit" for this
296 this_figure_of_merit
= get_figure_of_merit(curdev
);
299 * Now look for the last interface with an figure of merit
300 * less than or equal to the new interface's figure of
303 * We start with "prevdev" being NULL, meaning we're before
304 * the first element in the list.
309 * Get the interface after this one.
311 if (prevdev
== NULL
) {
313 * The next element is the first element.
317 nextdev
= prevdev
->next
;
320 * Are we at the end of the list?
322 if (nextdev
== NULL
) {
324 * Yes - we have to put the new entry
331 * Is the new interface's figure of merit less
332 * than the next interface's figure of merit,
333 * meaning that the new interface is better
334 * than the next interface?
336 nextdev_figure_of_merit
= get_figure_of_merit(nextdev
);
337 if (this_figure_of_merit
< nextdev_figure_of_merit
) {
339 * Yes - we should put the new entry
340 * before "nextdev", i.e. after "prevdev".
349 * Insert before "nextdev".
351 curdev
->next
= nextdev
;
354 * Insert after "prevdev" - unless "prevdev" is null,
355 * in which case this is the first interface.
357 if (prevdev
== NULL
) {
359 * This is the first interface. Pass back a
360 * pointer to it, and put "curdev" before
365 prevdev
->next
= curdev
;
368 *curdev_ret
= curdev
;
373 * XXX - on FreeBSDs that support it, should it get the sysctl named
374 * "dev.{adapter family name}.{adapter unit}.%desc" to get a description
375 * of the adapter? Note that "dev.an.0.%desc" is "Aironet PC4500/PC4800"
376 * with my Cisco 350 card, so the name isn't entirely descriptive. The
377 * "dev.an.0.%pnpinfo" has a better description, although one might argue
378 * that the problem is really a driver bug - if it can find out that it's
379 * a Cisco 340 or 350, rather than an old Aironet card, it should use
380 * that in the description.
382 * Do NetBSD, DragonflyBSD, or OpenBSD support this as well? FreeBSD
383 * and OpenBSD let you get a description, but it's not generated by the OS,
384 * it's set with another ioctl that ifconfig supports; we use that to get
385 * a description in FreeBSD and OpenBSD, but if there is no such
386 * description available, it still might be nice to get some description
387 * string based on the device type or something such as that.
389 * In OS X, the System Configuration framework can apparently return
390 * names in 10.4 and later.
392 * It also appears that freedesktop.org's HAL offers an "info.product"
393 * string, but the HAL specification says it "should not be used in any
394 * UI" and "subsystem/capability specific properties" should be used
395 * instead and, in any case, I think HAL is being deprecated in
396 * favor of other stuff such as DeviceKit. DeviceKit doesn't appear
397 * to have any obvious product information for devices, but maybe
398 * I haven't looked hard enough.
400 * Using the System Configuration framework, or HAL, or DeviceKit, or
401 * whatever, would require that libpcap applications be linked with
402 * the frameworks/libraries in question. That shouldn't be a problem
403 * for programs linking with the shared version of libpcap (unless
404 * you're running on AIX - which I think is the only UN*X that doesn't
405 * support linking a shared library with other libraries on which it
406 * depends, and having an executable linked only with the first shared
407 * library automatically pick up the other libraries when started -
408 * and using HAL or whatever). Programs linked with the static
409 * version of libpcap would have to use pcap-config with the --static
410 * flag in order to get the right linker flags in order to pick up
411 * the additional libraries/frameworks; those programs need that anyway
412 * for libpcap 1.1 and beyond on Linux, as, by default, it requires
415 * Do any other UN*Xes, or desktop environments support getting a
419 add_addr_to_iflist(pcap_if_t
**alldevs
, const char *name
, u_int flags
,
420 struct sockaddr
*addr
, size_t addr_size
,
421 struct sockaddr
*netmask
, size_t netmask_size
,
422 struct sockaddr
*broadaddr
, size_t broadaddr_size
,
423 struct sockaddr
*dstaddr
, size_t dstaddr_size
,
427 char *description
= NULL
;
430 struct ifreq ifrdesc
;
432 size_t descrlen
= 64;
434 size_t descrlen
= IFDESCRSIZE
;
435 #endif /* IFDESCRSIZE */
436 #endif /* SIOCGIFDESCR */
440 * Get the description for the interface.
442 memset(&ifrdesc
, 0, sizeof ifrdesc
);
443 strlcpy(ifrdesc
.ifr_name
, name
, sizeof ifrdesc
.ifr_name
);
444 s
= socket(AF_INET
, SOCK_DGRAM
, 0);
448 * On FreeBSD, if the buffer isn't big enough for the
449 * description, the ioctl succeeds, but the description
450 * isn't copied, ifr_buffer.length is set to the description
451 * length, and ifr_buffer.buffer is set to NULL.
455 if ((description
= malloc(descrlen
)) != NULL
) {
456 ifrdesc
.ifr_buffer
.buffer
= description
;
457 ifrdesc
.ifr_buffer
.length
= descrlen
;
458 if (ioctl(s
, SIOCGIFDESCR
, &ifrdesc
) == 0) {
459 if (ifrdesc
.ifr_buffer
.buffer
==
463 descrlen
= ifrdesc
.ifr_buffer
.length
;
466 * Failed to get interface description.
475 #else /* __FreeBSD__ */
477 * The only other OS that currently supports
478 * SIOCGIFDESCR is OpenBSD, and it has no way
479 * to get the description length - it's clamped
480 * to a maximum of IFDESCRSIZE.
482 if ((description
= malloc(descrlen
)) != NULL
) {
483 ifrdesc
.ifr_data
= (caddr_t
)description
;
484 if (ioctl(s
, SIOCGIFDESCR
, &ifrdesc
) != 0) {
486 * Failed to get interface description.
492 #endif /* __FreeBSD__ */
494 if (description
!= NULL
&& strlen(description
) == 0) {
499 #endif /* SIOCGIFDESCR */
501 if (add_or_find_if(&curdev
, alldevs
, name
, flags
, description
,
510 if (curdev
== NULL
) {
512 * Device wasn't added because it can't be opened.
519 * "curdev" is an entry for this interface; add an entry for this
520 * address to its list of addresses.
522 * Allocate the new entry and fill it in.
524 return (add_addr_to_dev(curdev
, addr
, addr_size
, netmask
, netmask_size
,
525 broadaddr
, broadaddr_size
, dstaddr
, dstaddr_size
, errbuf
));
529 * Add an entry to the list of addresses for an interface.
530 * "curdev" is the entry for that interface.
531 * If this is the first IP address added to the interface, move it
532 * in the list as appropriate.
535 add_addr_to_dev(pcap_if_t
*curdev
,
536 struct sockaddr
*addr
, size_t addr_size
,
537 struct sockaddr
*netmask
, size_t netmask_size
,
538 struct sockaddr
*broadaddr
, size_t broadaddr_size
,
539 struct sockaddr
*dstaddr
, size_t dstaddr_size
,
542 pcap_addr_t
*curaddr
, *prevaddr
, *nextaddr
;
544 curaddr
= malloc(sizeof(pcap_addr_t
));
545 if (curaddr
== NULL
) {
546 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
547 "malloc: %s", pcap_strerror(errno
));
551 curaddr
->next
= NULL
;
553 curaddr
->addr
= dup_sockaddr(addr
, addr_size
);
554 if (curaddr
->addr
== NULL
) {
555 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
556 "malloc: %s", pcap_strerror(errno
));
561 curaddr
->addr
= NULL
;
563 if (netmask
!= NULL
) {
564 curaddr
->netmask
= dup_sockaddr(netmask
, netmask_size
);
565 if (curaddr
->netmask
== NULL
) {
566 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
567 "malloc: %s", pcap_strerror(errno
));
568 if (curaddr
->addr
!= NULL
)
574 curaddr
->netmask
= NULL
;
576 if (broadaddr
!= NULL
) {
577 curaddr
->broadaddr
= dup_sockaddr(broadaddr
, broadaddr_size
);
578 if (curaddr
->broadaddr
== NULL
) {
579 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
580 "malloc: %s", pcap_strerror(errno
));
581 if (curaddr
->netmask
!= NULL
)
582 free(curaddr
->netmask
);
583 if (curaddr
->addr
!= NULL
)
589 curaddr
->broadaddr
= NULL
;
591 if (dstaddr
!= NULL
) {
592 curaddr
->dstaddr
= dup_sockaddr(dstaddr
, dstaddr_size
);
593 if (curaddr
->dstaddr
== NULL
) {
594 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
595 "malloc: %s", pcap_strerror(errno
));
596 if (curaddr
->broadaddr
!= NULL
)
597 free(curaddr
->broadaddr
);
598 if (curaddr
->netmask
!= NULL
)
599 free(curaddr
->netmask
);
600 if (curaddr
->addr
!= NULL
)
606 curaddr
->dstaddr
= NULL
;
609 * Find the end of the list of addresses.
611 for (prevaddr
= curdev
->addresses
; prevaddr
!= NULL
; prevaddr
= nextaddr
) {
612 nextaddr
= prevaddr
->next
;
613 if (nextaddr
== NULL
) {
615 * This is the end of the list.
621 if (prevaddr
== NULL
) {
623 * The list was empty; this is the first member.
625 curdev
->addresses
= curaddr
;
628 * "prevaddr" is the last member of the list; append
631 prevaddr
->next
= curaddr
;
638 pcap_add_if(pcap_if_t
**devlist
, const char *name
, u_int flags
,
639 const char *description
, char *errbuf
)
643 return (add_or_find_if(&curdev
, devlist
, name
, flags
, description
,
649 * Free a list of interfaces.
652 pcap_freealldevs(pcap_if_t
*alldevs
)
654 pcap_if_t
*curdev
, *nextdev
;
655 pcap_addr_t
*curaddr
, *nextaddr
;
657 for (curdev
= alldevs
; curdev
!= NULL
; curdev
= nextdev
) {
658 nextdev
= curdev
->next
;
661 * Free all addresses.
663 for (curaddr
= curdev
->addresses
; curaddr
!= NULL
; curaddr
= nextaddr
) {
664 nextaddr
= curaddr
->next
;
667 if (curaddr
->netmask
)
668 free(curaddr
->netmask
);
669 if (curaddr
->broadaddr
)
670 free(curaddr
->broadaddr
);
671 if (curaddr
->dstaddr
)
672 free(curaddr
->dstaddr
);
677 * Free the name string.
682 * Free the description string, if any.
684 if (curdev
->description
!= NULL
)
685 free(curdev
->description
);
688 * Free the interface.
694 #if !defined(WIN32) && !defined(MSDOS)
697 * Return the name of a network interface attached to the system, or NULL
698 * if none can be found. The interface must be configured up; the
699 * lowest unit number is preferred; loopback is ignored.
702 pcap_lookupdev(errbuf
)
703 register char *errbuf
;
706 /* for old BSD systems, including bsdi3 */
708 #define IF_NAMESIZE IFNAMSIZ
710 static char device
[IF_NAMESIZE
+ 1];
713 if (pcap_findalldevs(&alldevs
, errbuf
) == -1)
716 if (alldevs
== NULL
|| (alldevs
->flags
& PCAP_IF_LOOPBACK
)) {
718 * There are no devices on the list, or the first device
719 * on the list is a loopback device, which means there
720 * are no non-loopback devices on the list. This means
721 * we can't return any device.
723 * XXX - why not return a loopback device? If we can't
724 * capture on it, it won't be on the list, and if it's
725 * on the list, there aren't any non-loopback devices,
726 * so why not just supply it as the default device?
728 (void)strlcpy(errbuf
, "no suitable device found",
733 * Return the name of the first device on the list.
735 (void)strlcpy(device
, alldevs
->name
, sizeof(device
));
739 pcap_freealldevs(alldevs
);
744 pcap_lookupnet(device
, netp
, maskp
, errbuf
)
745 register const char *device
;
746 register bpf_u_int32
*netp
, *maskp
;
747 register char *errbuf
;
750 register struct sockaddr_in
*sin4
;
754 * The pseudo-device "any" listens on all interfaces and therefore
755 * has the network address and -mask "0.0.0.0" therefore catching
756 * all traffic. Using NULL for the interface is the same as "any".
758 if (!device
|| strcmp(device
, "any") == 0
760 || strstr(device
, "dag") != NULL
762 #ifdef HAVE_SEPTEL_API
763 || strstr(device
, "septel") != NULL
765 #ifdef PCAP_SUPPORT_BT
766 || strstr(device
, "bluetooth") != NULL
768 #ifdef PCAP_SUPPORT_USB
769 || strstr(device
, "usbmon") != NULL
772 || strstr(device
, "snf") != NULL
779 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
781 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "socket: %s",
782 pcap_strerror(errno
));
785 memset(&ifr
, 0, sizeof(ifr
));
787 /* XXX Work around Linux kernel bug */
788 ifr
.ifr_addr
.sa_family
= AF_INET
;
790 (void)strncpy(ifr
.ifr_name
, device
, sizeof(ifr
.ifr_name
));
791 if (ioctl(fd
, SIOCGIFADDR
, (char *)&ifr
) < 0) {
792 if (errno
== EADDRNOTAVAIL
) {
793 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
794 "%s: no IPv4 address assigned", device
);
796 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
797 "SIOCGIFADDR: %s: %s",
798 device
, pcap_strerror(errno
));
803 sin4
= (struct sockaddr_in
*)&ifr
.ifr_addr
;
804 *netp
= sin4
->sin_addr
.s_addr
;
805 memset(&ifr
, 0, sizeof(ifr
));
807 /* XXX Work around Linux kernel bug */
808 ifr
.ifr_addr
.sa_family
= AF_INET
;
810 (void)strncpy(ifr
.ifr_name
, device
, sizeof(ifr
.ifr_name
));
811 if (ioctl(fd
, SIOCGIFNETMASK
, (char *)&ifr
) < 0) {
812 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
813 "SIOCGIFNETMASK: %s: %s", device
, pcap_strerror(errno
));
818 *maskp
= sin4
->sin_addr
.s_addr
;
820 if (IN_CLASSA(*netp
))
821 *maskp
= IN_CLASSA_NET
;
822 else if (IN_CLASSB(*netp
))
823 *maskp
= IN_CLASSB_NET
;
824 else if (IN_CLASSC(*netp
))
825 *maskp
= IN_CLASSC_NET
;
827 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
828 "inet class for 0x%x unknown", *netp
);
839 * Return the name of a network interface attached to the system, or NULL
840 * if none can be found. The interface must be configured up; the
841 * lowest unit number is preferred; loopback is ignored.
844 pcap_lookupdev(errbuf
)
845 register char *errbuf
;
848 DWORD dwWindowsMajorVersion
;
849 dwVersion
= GetVersion(); /* get the OS version */
850 dwWindowsMajorVersion
= (DWORD
)(LOBYTE(LOWORD(dwVersion
)));
852 if (dwVersion
>= 0x80000000 && dwWindowsMajorVersion
>= 4) {
854 * Windows 95, 98, ME.
856 ULONG NameLength
= 8192;
857 static char AdaptersName
[8192];
859 if (PacketGetAdapterNames(AdaptersName
,&NameLength
) )
860 return (AdaptersName
);
865 * Windows NT (NT 4.0, W2K, WXP). Convert the names to UNICODE for backward compatibility
867 ULONG NameLength
= 8192;
868 static WCHAR AdaptersName
[8192];
871 WCHAR
*TAdaptersName
= (WCHAR
*)malloc(8192 * sizeof(WCHAR
));
874 if(TAdaptersName
== NULL
)
876 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "memory allocation failure");
880 if ( !PacketGetAdapterNames((PTSTR
)TAdaptersName
,&NameLength
) )
882 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
883 "PacketGetAdapterNames: %s",
884 pcap_win32strerror());
890 tAstr
= (char*)TAdaptersName
;
891 tUstr
= (WCHAR
*)AdaptersName
;
894 * Convert and copy the device names
896 while(sscanf(tAstr
, "%S", tUstr
) > 0)
898 tAstr
+= strlen(tAstr
) + 1;
899 tUstr
+= wcslen(tUstr
) + 1;
908 * Copy the descriptions
912 char* tmp
= (char*)tUstr
;
914 tmp
+= strlen(tAstr
) + 1;
916 tAstr
+= strlen(tAstr
) + 1;
920 return (char *)(AdaptersName
);
926 pcap_lookupnet(device
, netp
, maskp
, errbuf
)
927 register const char *device
;
928 register bpf_u_int32
*netp
, *maskp
;
929 register char *errbuf
;
932 * We need only the first IPv4 address, so we must scan the array returned by PacketGetNetInfo()
933 * in order to skip non IPv4 (i.e. IPv6 addresses)
935 npf_if_addr if_addrs
[MAX_NETWORK_ADDRESSES
];
936 LONG if_addr_size
= 1;
937 struct sockaddr_in
*t_addr
;
940 if (!PacketGetNetInfoEx((void *)device
, if_addrs
, &if_addr_size
)) {
945 for(i
=0; i
<MAX_NETWORK_ADDRESSES
; i
++)
947 if(if_addrs
[i
].IPAddress
.ss_family
== AF_INET
)
949 t_addr
= (struct sockaddr_in
*) &(if_addrs
[i
].IPAddress
);
950 *netp
= t_addr
->sin_addr
.S_un
.S_addr
;
951 t_addr
= (struct sockaddr_in
*) &(if_addrs
[i
].SubnetMask
);
952 *maskp
= t_addr
->sin_addr
.S_un
.S_addr
;
964 #endif /* !WIN32 && !MSDOS */