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
)
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 * A number at the end of the device name string is
144 * assumed to be a unit number.
146 cp
= dev
->name
+ strlen(dev
->name
) - 1;
147 while (cp
-1 >= dev
->name
&& *(cp
-1) >= '0' && *(cp
-1) <= '9')
149 if (*cp
>= '0' && *cp
<= '9')
154 if (!(dev
->flags
& PCAP_IF_RUNNING
))
156 if (!(dev
->flags
& PCAP_IF_UP
))
158 if (dev
->flags
& PCAP_IF_LOOPBACK
)
164 * Look for a given device in the specified list of devices.
166 * If we find it, return 0 and set *curdev_ret to point to it.
168 * If we don't find it, check whether we can open it:
170 * If that fails with PCAP_ERROR_NO_SUCH_DEVICE or
171 * PCAP_ERROR_IFACE_NOT_UP, don't attempt to add an entry for
172 * it, as that probably means it exists but doesn't support
175 * Otherwise, attempt to add an entry for it, with the specified
176 * ifnet flags and description, and, if that succeeds, return 0
177 * and set *curdev_ret to point to the new entry, otherwise
178 * return PCAP_ERROR and set errbuf to an error message.
181 add_or_find_if(pcap_if_t
**curdev_ret
, pcap_if_t
**alldevs
, const char *name
,
182 u_int flags
, const char *description
, char *errbuf
)
185 pcap_if_t
*curdev
, *prevdev
, *nextdev
;
186 u_int this_figure_of_merit
, nextdev_figure_of_merit
;
187 char open_errbuf
[PCAP_ERRBUF_SIZE
];
191 * Is there already an entry in the list for this interface?
193 for (curdev
= *alldevs
; curdev
!= NULL
; curdev
= curdev
->next
) {
194 if (strcmp(name
, curdev
->name
) == 0)
195 break; /* yes, we found it */
198 if (curdev
== NULL
) {
200 * No, we didn't find it.
202 * Can we open this interface for live capture?
204 * We do this check so that interfaces that are
205 * supplied by the interface enumeration mechanism
206 * we're using but that don't support packet capture
207 * aren't included in the list. Loopback interfaces
208 * on Solaris are an example of this; we don't just
209 * omit loopback interfaces on all platforms because
210 * you *can* capture on loopback interfaces on some
213 * On OS X, we don't do this check if the device
214 * name begins with "wlt"; at least some versions
215 * of OS X offer monitor mode capturing by having
216 * a separate "monitor mode" device for each wireless
217 * adapter, rather than by implementing the ioctls
218 * that {Free,Net,Open,DragonFly}BSD provide.
219 * Opening that device puts the adapter into monitor
220 * mode, which, at least for some adapters, causes
221 * them to deassociate from the network with which
222 * they're associated.
224 * Instead, we try to open the corresponding "en"
225 * device (so that we don't end up with, for users
226 * without sufficient privilege to open capture
227 * devices, a list of adapters that only includes
231 if (strncmp(name
, "wlt", 3) == 0) {
236 * Try to allocate a buffer for the "en"
239 en_name_len
= strlen(name
) - 1;
240 en_name
= malloc(en_name_len
+ 1);
241 if (en_name
== NULL
) {
242 (void)pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
243 "malloc: %s", pcap_strerror(errno
));
246 strcpy(en_name
, "en");
247 strcat(en_name
, name
+ 3);
248 p
= pcap_create(en_name
, open_errbuf
);
252 p
= pcap_create(name
, open_errbuf
);
255 * The attempt to create the pcap_t failed;
256 * that's probably an indication that we're
259 * Don't bother including this interface,
260 * but don't treat it as an error.
265 /* Small snaplen, so we don't try to allocate much memory. */
266 pcap_set_snaplen(p
, 68);
267 ret
= pcap_activate(p
);
271 case PCAP_ERROR_NO_SUCH_DEVICE
:
272 case PCAP_ERROR_IFACE_NOT_UP
:
274 * We expect these two errors - they're the
275 * reason we try to open the device.
277 * PCAP_ERROR_NO_SUCH_DEVICE typically means
278 * "there's no such device *known to the
279 * OS's capture mechanism*", so, even though
280 * it might be a valid network interface, you
281 * can't capture on it (e.g., the loopback
282 * device in Solaris up to Solaris 10, or
283 * the vmnet devices in OS X with VMware
284 * Fusion). We don't include those devices
285 * in our list of devices, as there's no
286 * point in doing so - they're not available
289 * PCAP_ERROR_IFACE_NOT_UP means that the
290 * OS's capture mechanism doesn't work on
291 * interfaces not marked as up; some capture
292 * mechanisms *do* support that, so we no
293 * longer reject those interfaces out of hand,
294 * but we *do* want to reject them if they
295 * can't be opened for capture.
302 * Yes, we can open it, or we can't, for some other
305 * If we can open it, we want to offer it for
306 * capture, as you can capture on it. If we can't,
307 * we want to offer it for capture, so that, if
308 * the user tries to capture on it, they'll get
309 * an error and they'll know why they can't
310 * capture on it (e.g., insufficient permissions)
311 * or they'll report it as a problem (and then
312 * have the error message to provide as information).
314 * Allocate a new entry.
316 curdev
= malloc(sizeof(pcap_if_t
));
317 if (curdev
== NULL
) {
318 (void)pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
319 "malloc: %s", pcap_strerror(errno
));
327 curdev
->name
= strdup(name
);
328 if (curdev
->name
== NULL
) {
329 (void)pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
330 "malloc: %s", pcap_strerror(errno
));
334 if (description
!= NULL
) {
336 * We have a description for this interface.
338 curdev
->description
= strdup(description
);
339 if (curdev
->description
== NULL
) {
340 (void)pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
341 "malloc: %s", pcap_strerror(errno
));
350 curdev
->description
= NULL
;
352 curdev
->addresses
= NULL
; /* list starts out as empty */
354 if (ISLOOPBACK(name
, flags
))
355 curdev
->flags
|= PCAP_IF_LOOPBACK
;
357 curdev
->flags
|= PCAP_IF_UP
;
358 if (ISRUNNING(flags
))
359 curdev
->flags
|= PCAP_IF_RUNNING
;
362 * Add it to the list, in the appropriate location.
363 * First, get the "figure of merit" for this
366 this_figure_of_merit
= get_figure_of_merit(curdev
);
369 * Now look for the last interface with an figure of merit
370 * less than or equal to the new interface's figure of
373 * We start with "prevdev" being NULL, meaning we're before
374 * the first element in the list.
379 * Get the interface after this one.
381 if (prevdev
== NULL
) {
383 * The next element is the first element.
387 nextdev
= prevdev
->next
;
390 * Are we at the end of the list?
392 if (nextdev
== NULL
) {
394 * Yes - we have to put the new entry
401 * Is the new interface's figure of merit less
402 * than the next interface's figure of merit,
403 * meaning that the new interface is better
404 * than the next interface?
406 nextdev_figure_of_merit
= get_figure_of_merit(nextdev
);
407 if (this_figure_of_merit
< nextdev_figure_of_merit
) {
409 * Yes - we should put the new entry
410 * before "nextdev", i.e. after "prevdev".
419 * Insert before "nextdev".
421 curdev
->next
= nextdev
;
424 * Insert after "prevdev" - unless "prevdev" is null,
425 * in which case this is the first interface.
427 if (prevdev
== NULL
) {
429 * This is the first interface. Pass back a
430 * pointer to it, and put "curdev" before
435 prevdev
->next
= curdev
;
438 *curdev_ret
= curdev
;
443 * Try to get a description for a given device.
444 * Returns a mallocated description if it could and NULL if it couldn't.
446 * XXX - on FreeBSDs that support it, should it get the sysctl named
447 * "dev.{adapter family name}.{adapter unit}.%desc" to get a description
448 * of the adapter? Note that "dev.an.0.%desc" is "Aironet PC4500/PC4800"
449 * with my Cisco 350 card, so the name isn't entirely descriptive. The
450 * "dev.an.0.%pnpinfo" has a better description, although one might argue
451 * that the problem is really a driver bug - if it can find out that it's
452 * a Cisco 340 or 350, rather than an old Aironet card, it should use
453 * that in the description.
455 * Do NetBSD, DragonflyBSD, or OpenBSD support this as well? FreeBSD
456 * and OpenBSD let you get a description, but it's not generated by the OS,
457 * it's set with another ioctl that ifconfig supports; we use that to get
458 * a description in FreeBSD and OpenBSD, but if there is no such
459 * description available, it still might be nice to get some description
460 * string based on the device type or something such as that.
462 * In OS X, the System Configuration framework can apparently return
463 * names in 10.4 and later.
465 * It also appears that freedesktop.org's HAL offers an "info.product"
466 * string, but the HAL specification says it "should not be used in any
467 * UI" and "subsystem/capability specific properties" should be used
468 * instead and, in any case, I think HAL is being deprecated in
469 * favor of other stuff such as DeviceKit. DeviceKit doesn't appear
470 * to have any obvious product information for devices, but maybe
471 * I haven't looked hard enough.
473 * Using the System Configuration framework, or HAL, or DeviceKit, or
474 * whatever, would require that libpcap applications be linked with
475 * the frameworks/libraries in question. That shouldn't be a problem
476 * for programs linking with the shared version of libpcap (unless
477 * you're running on AIX - which I think is the only UN*X that doesn't
478 * support linking a shared library with other libraries on which it
479 * depends, and having an executable linked only with the first shared
480 * library automatically pick up the other libraries when started -
481 * and using HAL or whatever). Programs linked with the static
482 * version of libpcap would have to use pcap-config with the --static
483 * flag in order to get the right linker flags in order to pick up
484 * the additional libraries/frameworks; those programs need that anyway
485 * for libpcap 1.1 and beyond on Linux, as, by default, it requires
488 * Do any other UN*Xes, or desktop environments support getting a
492 get_if_description(const char *name
)
495 char *description
= NULL
;
497 struct ifreq ifrdesc
;
499 size_t descrlen
= 64;
501 size_t descrlen
= IFDESCRSIZE
;
502 #endif /* IFDESCRSIZE */
505 * Get the description for the interface.
507 memset(&ifrdesc
, 0, sizeof ifrdesc
);
508 strlcpy(ifrdesc
.ifr_name
, name
, sizeof ifrdesc
.ifr_name
);
509 s
= socket(AF_INET
, SOCK_DGRAM
, 0);
513 * On FreeBSD, if the buffer isn't big enough for the
514 * description, the ioctl succeeds, but the description
515 * isn't copied, ifr_buffer.length is set to the description
516 * length, and ifr_buffer.buffer is set to NULL.
520 if ((description
= malloc(descrlen
)) != NULL
) {
521 ifrdesc
.ifr_buffer
.buffer
= description
;
522 ifrdesc
.ifr_buffer
.length
= descrlen
;
523 if (ioctl(s
, SIOCGIFDESCR
, &ifrdesc
) == 0) {
524 if (ifrdesc
.ifr_buffer
.buffer
==
528 descrlen
= ifrdesc
.ifr_buffer
.length
;
531 * Failed to get interface description.
540 #else /* __FreeBSD__ */
542 * The only other OS that currently supports
543 * SIOCGIFDESCR is OpenBSD, and it has no way
544 * to get the description length - it's clamped
545 * to a maximum of IFDESCRSIZE.
547 if ((description
= malloc(descrlen
)) != NULL
) {
548 ifrdesc
.ifr_data
= (caddr_t
)description
;
549 if (ioctl(s
, SIOCGIFDESCR
, &ifrdesc
) != 0) {
551 * Failed to get interface description.
557 #endif /* __FreeBSD__ */
559 if (description
!= NULL
&& strlen(description
) == 0) {
565 return (description
);
566 #else /* SIOCGIFDESCR */
568 #endif /* SIOCGIFDESCR */
572 * Try to get a description for a given device, and then look for that
573 * device in the specified list of devices.
575 * If we find it, then, if the specified address isn't null, add it to
576 * the list of addresses for the device and return 0.
578 * If we don't find it, check whether we can open it:
580 * If that fails with PCAP_ERROR_NO_SUCH_DEVICE or
581 * PCAP_ERROR_IFACE_NOT_UP, don't attempt to add an entry for
582 * it, as that probably means it exists but doesn't support
585 * Otherwise, attempt to add an entry for it, with the specified
586 * ifnet flags and description, and, if that succeeds, add the
587 * specified address to its list of addresses if that address is
588 * non-null, set *curdev_ret to point to the new entry, and
589 * return 0, otherwise return PCAP_ERROR and set errbuf to an
592 * (We can get called with a null address because we might get a list
593 * of interface name/address combinations from the underlying OS, with
594 * the address being absent in some cases, rather than a list of
595 * interfaces with each interface having a list of addresses, so this
596 * call may be the only call made to add to the list, and we want to
597 * add interfaces even if they have no addresses.)
600 add_addr_to_iflist(pcap_if_t
**alldevs
, const char *name
, u_int flags
,
601 struct sockaddr
*addr
, size_t addr_size
,
602 struct sockaddr
*netmask
, size_t netmask_size
,
603 struct sockaddr
*broadaddr
, size_t broadaddr_size
,
604 struct sockaddr
*dstaddr
, size_t dstaddr_size
,
610 description
= get_if_description(name
);
611 if (add_or_find_if(&curdev
, alldevs
, name
, flags
, description
,
620 if (curdev
== NULL
) {
622 * Device wasn't added because it can't be opened.
630 * There's no address to add; this entry just meant
631 * "here's a new interface".
637 * "curdev" is an entry for this interface, and we have an
638 * address for it; add an entry for that address to the
639 * interface's list of addresses.
641 * Allocate the new entry and fill it in.
643 return (add_addr_to_dev(curdev
, addr
, addr_size
, netmask
,
644 netmask_size
, broadaddr
, broadaddr_size
, dstaddr
,
645 dstaddr_size
, errbuf
));
649 * Add an entry to the list of addresses for an interface.
650 * "curdev" is the entry for that interface.
651 * If this is the first IP address added to the interface, move it
652 * in the list as appropriate.
655 add_addr_to_dev(pcap_if_t
*curdev
,
656 struct sockaddr
*addr
, size_t addr_size
,
657 struct sockaddr
*netmask
, size_t netmask_size
,
658 struct sockaddr
*broadaddr
, size_t broadaddr_size
,
659 struct sockaddr
*dstaddr
, size_t dstaddr_size
,
662 pcap_addr_t
*curaddr
, *prevaddr
, *nextaddr
;
664 curaddr
= malloc(sizeof(pcap_addr_t
));
665 if (curaddr
== NULL
) {
666 (void)pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
667 "malloc: %s", pcap_strerror(errno
));
671 curaddr
->next
= NULL
;
673 curaddr
->addr
= dup_sockaddr(addr
, addr_size
);
674 if (curaddr
->addr
== NULL
) {
675 (void)pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
676 "malloc: %s", pcap_strerror(errno
));
681 curaddr
->addr
= NULL
;
683 if (netmask
!= NULL
) {
684 curaddr
->netmask
= dup_sockaddr(netmask
, netmask_size
);
685 if (curaddr
->netmask
== NULL
) {
686 (void)pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
687 "malloc: %s", pcap_strerror(errno
));
688 if (curaddr
->addr
!= NULL
)
694 curaddr
->netmask
= NULL
;
696 if (broadaddr
!= NULL
) {
697 curaddr
->broadaddr
= dup_sockaddr(broadaddr
, broadaddr_size
);
698 if (curaddr
->broadaddr
== NULL
) {
699 (void)pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
700 "malloc: %s", pcap_strerror(errno
));
701 if (curaddr
->netmask
!= NULL
)
702 free(curaddr
->netmask
);
703 if (curaddr
->addr
!= NULL
)
709 curaddr
->broadaddr
= NULL
;
711 if (dstaddr
!= NULL
) {
712 curaddr
->dstaddr
= dup_sockaddr(dstaddr
, dstaddr_size
);
713 if (curaddr
->dstaddr
== NULL
) {
714 (void)pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
715 "malloc: %s", pcap_strerror(errno
));
716 if (curaddr
->broadaddr
!= NULL
)
717 free(curaddr
->broadaddr
);
718 if (curaddr
->netmask
!= NULL
)
719 free(curaddr
->netmask
);
720 if (curaddr
->addr
!= NULL
)
726 curaddr
->dstaddr
= NULL
;
729 * Find the end of the list of addresses.
731 for (prevaddr
= curdev
->addresses
; prevaddr
!= NULL
; prevaddr
= nextaddr
) {
732 nextaddr
= prevaddr
->next
;
733 if (nextaddr
== NULL
) {
735 * This is the end of the list.
741 if (prevaddr
== NULL
) {
743 * The list was empty; this is the first member.
745 curdev
->addresses
= curaddr
;
748 * "prevaddr" is the last member of the list; append
751 prevaddr
->next
= curaddr
;
758 * Look for a given device in the specified list of devices.
760 * If we find it, return 0.
762 * If we don't find it, check whether we can open it:
764 * If that fails with PCAP_ERROR_NO_SUCH_DEVICE or
765 * PCAP_ERROR_IFACE_NOT_UP, don't attempt to add an entry for
766 * it, as that probably means it exists but doesn't support
769 * Otherwise, attempt to add an entry for it, with the specified
770 * ifnet flags and description, and, if that succeeds, return 0
771 * and set *curdev_ret to point to the new entry, otherwise
772 * return PCAP_ERROR and set errbuf to an error message.
775 pcap_add_if(pcap_if_t
**devlist
, const char *name
, u_int flags
,
776 const char *description
, char *errbuf
)
780 return (add_or_find_if(&curdev
, devlist
, name
, flags
, description
,
786 * Free a list of interfaces.
789 pcap_freealldevs(pcap_if_t
*alldevs
)
791 pcap_if_t
*curdev
, *nextdev
;
792 pcap_addr_t
*curaddr
, *nextaddr
;
794 for (curdev
= alldevs
; curdev
!= NULL
; curdev
= nextdev
) {
795 nextdev
= curdev
->next
;
798 * Free all addresses.
800 for (curaddr
= curdev
->addresses
; curaddr
!= NULL
; curaddr
= nextaddr
) {
801 nextaddr
= curaddr
->next
;
804 if (curaddr
->netmask
)
805 free(curaddr
->netmask
);
806 if (curaddr
->broadaddr
)
807 free(curaddr
->broadaddr
);
808 if (curaddr
->dstaddr
)
809 free(curaddr
->dstaddr
);
814 * Free the name string.
819 * Free the description string, if any.
821 if (curdev
->description
!= NULL
)
822 free(curdev
->description
);
825 * Free the interface.
831 #if !defined(_WIN32) && !defined(MSDOS)
834 * Return the name of a network interface attached to the system, or NULL
835 * if none can be found. The interface must be configured up; the
836 * lowest unit number is preferred; loopback is ignored.
839 pcap_lookupdev(errbuf
)
840 register char *errbuf
;
843 /* for old BSD systems, including bsdi3 */
845 #define IF_NAMESIZE IFNAMSIZ
847 static char device
[IF_NAMESIZE
+ 1];
850 if (pcap_findalldevs(&alldevs
, errbuf
) == -1)
853 if (alldevs
== NULL
|| (alldevs
->flags
& PCAP_IF_LOOPBACK
)) {
855 * There are no devices on the list, or the first device
856 * on the list is a loopback device, which means there
857 * are no non-loopback devices on the list. This means
858 * we can't return any device.
860 * XXX - why not return a loopback device? If we can't
861 * capture on it, it won't be on the list, and if it's
862 * on the list, there aren't any non-loopback devices,
863 * so why not just supply it as the default device?
865 (void)strlcpy(errbuf
, "no suitable device found",
870 * Return the name of the first device on the list.
872 (void)strlcpy(device
, alldevs
->name
, sizeof(device
));
876 pcap_freealldevs(alldevs
);
881 pcap_lookupnet(device
, netp
, maskp
, errbuf
)
882 register const char *device
;
883 register bpf_u_int32
*netp
, *maskp
;
884 register char *errbuf
;
887 register struct sockaddr_in
*sin4
;
891 * The pseudo-device "any" listens on all interfaces and therefore
892 * has the network address and -mask "0.0.0.0" therefore catching
893 * all traffic. Using NULL for the interface is the same as "any".
895 if (!device
|| strcmp(device
, "any") == 0
897 || strstr(device
, "dag") != NULL
899 #ifdef HAVE_SEPTEL_API
900 || strstr(device
, "septel") != NULL
902 #ifdef PCAP_SUPPORT_BT
903 || strstr(device
, "bluetooth") != NULL
905 #ifdef PCAP_SUPPORT_USB
906 || strstr(device
, "usbmon") != NULL
909 || strstr(device
, "snf") != NULL
916 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
918 (void)pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "socket: %s",
919 pcap_strerror(errno
));
922 memset(&ifr
, 0, sizeof(ifr
));
924 /* XXX Work around Linux kernel bug */
925 ifr
.ifr_addr
.sa_family
= AF_INET
;
927 (void)strlcpy(ifr
.ifr_name
, device
, sizeof(ifr
.ifr_name
));
928 if (ioctl(fd
, SIOCGIFADDR
, (char *)&ifr
) < 0) {
929 if (errno
== EADDRNOTAVAIL
) {
930 (void)pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
931 "%s: no IPv4 address assigned", device
);
933 (void)pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
934 "SIOCGIFADDR: %s: %s",
935 device
, pcap_strerror(errno
));
940 sin4
= (struct sockaddr_in
*)&ifr
.ifr_addr
;
941 *netp
= sin4
->sin_addr
.s_addr
;
942 memset(&ifr
, 0, sizeof(ifr
));
944 /* XXX Work around Linux kernel bug */
945 ifr
.ifr_addr
.sa_family
= AF_INET
;
947 (void)strlcpy(ifr
.ifr_name
, device
, sizeof(ifr
.ifr_name
));
948 if (ioctl(fd
, SIOCGIFNETMASK
, (char *)&ifr
) < 0) {
949 (void)pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
950 "SIOCGIFNETMASK: %s: %s", device
, pcap_strerror(errno
));
955 *maskp
= sin4
->sin_addr
.s_addr
;
957 if (IN_CLASSA(*netp
))
958 *maskp
= IN_CLASSA_NET
;
959 else if (IN_CLASSB(*netp
))
960 *maskp
= IN_CLASSB_NET
;
961 else if (IN_CLASSC(*netp
))
962 *maskp
= IN_CLASSC_NET
;
964 (void)pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
965 "inet class for 0x%x unknown", *netp
);
973 #elif defined(_WIN32)
976 * Return the name of a network interface attached to the system, or NULL
977 * if none can be found. The interface must be configured up; the
978 * lowest unit number is preferred; loopback is ignored.
980 * In the best of all possible worlds, this would be the same as on
981 * UN*X, but there may be software that expects this to return a
982 * full list of devices after the first device.
985 pcap_lookupdev(errbuf
)
986 register char *errbuf
;
989 DWORD dwWindowsMajorVersion
;
990 char our_errbuf
[PCAP_ERRBUF_SIZE
+1];
992 dwVersion
= GetVersion(); /* get the OS version */
993 dwWindowsMajorVersion
= (DWORD
)(LOBYTE(LOWORD(dwVersion
)));
995 if (dwVersion
>= 0x80000000 && dwWindowsMajorVersion
>= 4) {
997 * Windows 95, 98, ME.
999 ULONG NameLength
= 8192;
1000 static char AdaptersName
[8192];
1002 if (PacketGetAdapterNames(AdaptersName
,&NameLength
) )
1003 return (AdaptersName
);
1008 * Windows NT (NT 4.0, W2K, WXP). Convert the names to UNICODE for backward compatibility
1010 ULONG NameLength
= 8192;
1011 static WCHAR AdaptersName
[8192];
1014 WCHAR
*TAdaptersName
= (WCHAR
*)malloc(8192 * sizeof(WCHAR
));
1017 if(TAdaptersName
== NULL
)
1019 (void)pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "memory allocation failure");
1023 if ( !PacketGetAdapterNames((PTSTR
)TAdaptersName
,&NameLength
) )
1025 pcap_win32_err_to_str(GetLastError(), our_errbuf
);
1026 (void)pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1027 "PacketGetAdapterNames: %s", our_errbuf
);
1028 free(TAdaptersName
);
1033 tAstr
= (char*)TAdaptersName
;
1034 tUstr
= (WCHAR
*)AdaptersName
;
1037 * Convert and copy the device names
1039 while(sscanf(tAstr
, "%S", tUstr
) > 0)
1041 tAstr
+= strlen(tAstr
) + 1;
1042 tUstr
+= wcslen(tUstr
) + 1;
1051 * Copy the descriptions
1055 char* tmp
= (char*)tUstr
;
1057 tmp
+= strlen(tAstr
) + 1;
1058 tUstr
= (WCHAR
*)tmp
;
1059 tAstr
+= strlen(tAstr
) + 1;
1062 free(TAdaptersName
);
1063 return (char *)(AdaptersName
);
1069 pcap_lookupnet(device
, netp
, maskp
, errbuf
)
1070 register const char *device
;
1071 register bpf_u_int32
*netp
, *maskp
;
1072 register char *errbuf
;
1075 * We need only the first IPv4 address, so we must scan the array returned by PacketGetNetInfo()
1076 * in order to skip non IPv4 (i.e. IPv6 addresses)
1078 npf_if_addr if_addrs
[MAX_NETWORK_ADDRESSES
];
1079 LONG if_addr_size
= 1;
1080 struct sockaddr_in
*t_addr
;
1083 if (!PacketGetNetInfoEx((void *)device
, if_addrs
, &if_addr_size
)) {
1088 for(i
=0; i
<MAX_NETWORK_ADDRESSES
; i
++)
1090 if(if_addrs
[i
].IPAddress
.ss_family
== AF_INET
)
1092 t_addr
= (struct sockaddr_in
*) &(if_addrs
[i
].IPAddress
);
1093 *netp
= t_addr
->sin_addr
.S_un
.S_addr
;
1094 t_addr
= (struct sockaddr_in
*) &(if_addrs
[i
].SubnetMask
);
1095 *maskp
= t_addr
->sin_addr
.S_un
.S_addr
;
1107 #endif /* !_WIN32 && !MSDOS */