2 * Copyright (c) 1993, 1994, 1995, 1996, 1997, 1998
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the Computer Systems
16 * Engineering Group at Lawrence Berkeley Laboratory.
17 * 4. Neither the name of the University nor of the Laboratory may be used
18 * to endorse or promote products derived from this software without
19 * specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #include <pcap-types.h>
40 #include <sys/param.h>
44 #include <sys/ioctl.h>
45 #include <sys/socket.h>
46 #ifdef HAVE_SYS_SOCKIO_H
47 #include <sys/sockio.h>
50 struct mbuf
; /* Squelch compiler warnings on some platforms for */
51 struct rtentry
; /* declarations in <net/if.h> */
53 #include <netinet/in.h>
59 #if !defined(_MSC_VER) && !defined(__BORLANDC__) && !defined(__MINGW32__)
66 #ifdef HAVE_OS_PROTO_H
80 #endif /* HAVE_DAG_API */
82 #ifdef HAVE_SEPTEL_API
83 #include "pcap-septel.h"
84 #endif /* HAVE_SEPTEL_API */
88 #endif /* HAVE_SNF_API */
92 #endif /* HAVE_TC_API */
94 #ifdef PCAP_SUPPORT_USB
95 #include "pcap-usb-linux.h"
98 #ifdef PCAP_SUPPORT_BT
99 #include "pcap-bt-linux.h"
102 #ifdef PCAP_SUPPORT_BT_MONITOR
103 #include "pcap-bt-monitor-linux.h"
106 #ifdef PCAP_SUPPORT_NETFILTER
107 #include "pcap-netfilter-linux.h"
110 #ifdef PCAP_SUPPORT_NETMAP
111 #include "pcap-netmap.h"
114 #ifdef PCAP_SUPPORT_DBUS
115 #include "pcap-dbus.h"
118 #ifdef PCAP_SUPPORT_RDMASNIFF
119 #include "pcap-rdmasniff.h"
122 #ifdef PCAP_SUPPORT_DPDK
123 #include "pcap-dpdk.h"
128 * DllMain(), required when built as a Windows DLL.
141 * Exported in case some applications using WinPcap/Npcap called it,
142 * even though it wasn't exported.
147 WORD wVersionRequested
;
155 wVersionRequested
= MAKEWORD( 1, 1);
156 err
= WSAStartup( wVersionRequested
, &wsaData
);
157 atexit ((void(*)(void))WSACleanup
);
166 * This is the exported function; new programs should call this.
171 return (wsockinit());
176 * String containing the library version.
177 * Not explicitly exported via a header file - the right API to use
178 * is pcap_lib_version() - but some programs included it, so we
181 * We declare it here, right before defining it, to squelch any
182 * warnings we might get from compilers about the lack of a
185 PCAP_API
char pcap_version
[];
186 PCAP_API_DEF
char pcap_version
[] = PACKAGE_VERSION
;
189 pcap_set_not_initialized_message(pcap_t
*pcap
)
191 if (pcap
->activated
) {
192 /* A module probably forgot to set the function pointer */
193 (void)snprintf(pcap
->errbuf
, sizeof(pcap
->errbuf
),
194 "This operation isn't properly handled by that device");
197 /* in case the caller doesn't check for PCAP_ERROR_NOT_ACTIVATED */
198 (void)snprintf(pcap
->errbuf
, sizeof(pcap
->errbuf
),
199 "This handle hasn't been activated yet");
203 pcap_read_not_initialized(pcap_t
*pcap
, int cnt _U_
, pcap_handler callback _U_
,
206 pcap_set_not_initialized_message(pcap
);
207 /* this means 'not initialized' */
208 return (PCAP_ERROR_NOT_ACTIVATED
);
212 pcap_inject_not_initialized(pcap_t
*pcap
, const void * buf _U_
, int size _U_
)
214 pcap_set_not_initialized_message(pcap
);
215 /* this means 'not initialized' */
216 return (PCAP_ERROR_NOT_ACTIVATED
);
220 pcap_setfilter_not_initialized(pcap_t
*pcap
, struct bpf_program
*fp _U_
)
222 pcap_set_not_initialized_message(pcap
);
223 /* this means 'not initialized' */
224 return (PCAP_ERROR_NOT_ACTIVATED
);
228 pcap_setdirection_not_initialized(pcap_t
*pcap
, pcap_direction_t d _U_
)
230 pcap_set_not_initialized_message(pcap
);
231 /* this means 'not initialized' */
232 return (PCAP_ERROR_NOT_ACTIVATED
);
236 pcap_set_datalink_not_initialized(pcap_t
*pcap
, int dlt _U_
)
238 pcap_set_not_initialized_message(pcap
);
239 /* this means 'not initialized' */
240 return (PCAP_ERROR_NOT_ACTIVATED
);
244 pcap_getnonblock_not_initialized(pcap_t
*pcap
)
246 pcap_set_not_initialized_message(pcap
);
247 /* this means 'not initialized' */
248 return (PCAP_ERROR_NOT_ACTIVATED
);
252 pcap_stats_not_initialized(pcap_t
*pcap
, struct pcap_stat
*ps _U_
)
254 pcap_set_not_initialized_message(pcap
);
255 /* this means 'not initialized' */
256 return (PCAP_ERROR_NOT_ACTIVATED
);
261 pcap_stats_ex_not_initialized(pcap_t
*pcap
, int *pcap_stat_size _U_
)
263 pcap_set_not_initialized_message(pcap
);
268 pcap_setbuff_not_initialized(pcap_t
*pcap
, int dim _U_
)
270 pcap_set_not_initialized_message(pcap
);
271 /* this means 'not initialized' */
272 return (PCAP_ERROR_NOT_ACTIVATED
);
276 pcap_setmode_not_initialized(pcap_t
*pcap
, int mode _U_
)
278 pcap_set_not_initialized_message(pcap
);
279 /* this means 'not initialized' */
280 return (PCAP_ERROR_NOT_ACTIVATED
);
284 pcap_setmintocopy_not_initialized(pcap_t
*pcap
, int size _U_
)
286 pcap_set_not_initialized_message(pcap
);
287 /* this means 'not initialized' */
288 return (PCAP_ERROR_NOT_ACTIVATED
);
292 pcap_getevent_not_initialized(pcap_t
*pcap
)
294 pcap_set_not_initialized_message(pcap
);
295 return (INVALID_HANDLE_VALUE
);
299 pcap_oid_get_request_not_initialized(pcap_t
*pcap
, bpf_u_int32 oid _U_
,
300 void *data _U_
, size_t *lenp _U_
)
302 pcap_set_not_initialized_message(pcap
);
303 return (PCAP_ERROR_NOT_ACTIVATED
);
307 pcap_oid_set_request_not_initialized(pcap_t
*pcap
, bpf_u_int32 oid _U_
,
308 const void *data _U_
, size_t *lenp _U_
)
310 pcap_set_not_initialized_message(pcap
);
311 return (PCAP_ERROR_NOT_ACTIVATED
);
315 pcap_sendqueue_transmit_not_initialized(pcap_t
*pcap
, pcap_send_queue
* queue
, int sync
)
317 pcap_set_not_initialized_message(pcap
);
322 pcap_setuserbuffer_not_initialized(pcap_t
*pcap
, int size _U_
)
324 pcap_set_not_initialized_message(pcap
);
325 return (PCAP_ERROR_NOT_ACTIVATED
);
329 pcap_live_dump_not_initialized(pcap_t
*pcap
, char *filename _U_
, int maxsize _U_
,
332 pcap_set_not_initialized_message(pcap
);
333 return (PCAP_ERROR_NOT_ACTIVATED
);
337 pcap_live_dump_ended_not_initialized(pcap_t
*pcap
, int sync _U_
)
339 pcap_set_not_initialized_message(pcap
);
340 return (PCAP_ERROR_NOT_ACTIVATED
);
343 static PAirpcapHandle
344 pcap_get_airpcap_handle_not_initialized(pcap_t
*pcap
)
346 pcap_set_not_initialized_message(pcap
);
352 * Returns 1 if rfmon mode can be set on the pcap_t, 0 if it can't,
353 * a PCAP_ERROR value on an error.
356 pcap_can_set_rfmon(pcap_t
*p
)
358 return (p
->can_set_rfmon_op(p
));
362 * For systems where rfmon mode is never supported.
365 pcap_cant_set_rfmon(pcap_t
*p _U_
)
371 * Sets *tstamp_typesp to point to an array 1 or more supported time stamp
372 * types; the return value is the number of supported time stamp types.
373 * The list should be freed by a call to pcap_free_tstamp_types() when
374 * you're done with it.
376 * A return value of 0 means "you don't get a choice of time stamp type",
377 * in which case *tstamp_typesp is set to null.
379 * PCAP_ERROR is returned on error.
382 pcap_list_tstamp_types(pcap_t
*p
, int **tstamp_typesp
)
384 if (p
->tstamp_type_count
== 0) {
386 * We don't support multiple time stamp types.
387 * That means the only type we support is PCAP_TSTAMP_HOST;
388 * set up a list containing only that type.
390 *tstamp_typesp
= (int*)malloc(sizeof(**tstamp_typesp
));
391 if (*tstamp_typesp
== NULL
) {
392 pcap_fmt_errmsg_for_errno(p
->errbuf
, sizeof(p
->errbuf
),
396 **tstamp_typesp
= PCAP_TSTAMP_HOST
;
399 *tstamp_typesp
= (int*)calloc(sizeof(**tstamp_typesp
),
400 p
->tstamp_type_count
);
401 if (*tstamp_typesp
== NULL
) {
402 pcap_fmt_errmsg_for_errno(p
->errbuf
, sizeof(p
->errbuf
),
406 (void)memcpy(*tstamp_typesp
, p
->tstamp_type_list
,
407 sizeof(**tstamp_typesp
) * p
->tstamp_type_count
);
408 return (p
->tstamp_type_count
);
413 * In Windows, you might have a library built with one version of the
414 * C runtime library and an application built with another version of
415 * the C runtime library, which means that the library might use one
416 * version of malloc() and free() and the application might use another
417 * version of malloc() and free(). If so, that means something
418 * allocated by the library cannot be freed by the application, so we
419 * need to have a pcap_free_tstamp_types() routine to free up the list
420 * allocated by pcap_list_tstamp_types(), even though it's just a wrapper
424 pcap_free_tstamp_types(int *tstamp_type_list
)
426 free(tstamp_type_list
);
430 * Default one-shot callback; overridden for capture types where the
431 * packet data cannot be guaranteed to be available after the callback
432 * returns, so that a copy must be made.
435 pcap_oneshot(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*pkt
)
437 struct oneshot_userdata
*sp
= (struct oneshot_userdata
*)user
;
444 pcap_next(pcap_t
*p
, struct pcap_pkthdr
*h
)
446 struct oneshot_userdata s
;
452 if (pcap_dispatch(p
, 1, p
->oneshot_callback
, (u_char
*)&s
) <= 0)
458 pcap_next_ex(pcap_t
*p
, struct pcap_pkthdr
**pkt_header
,
459 const u_char
**pkt_data
)
461 struct oneshot_userdata s
;
463 s
.hdr
= &p
->pcap_header
;
467 /* Saves a pointer to the packet headers */
468 *pkt_header
= &p
->pcap_header
;
470 if (p
->rfile
!= NULL
) {
473 /* We are on an offline capture */
474 status
= pcap_offline_read(p
, 1, p
->oneshot_callback
,
478 * Return codes for pcap_offline_read() are:
482 * The first one ('0') conflicts with the return code of
483 * 0 from pcap_read() meaning "no packets arrived before
484 * the timeout expired", so we map it to -2 so you can
485 * distinguish between an EOF from a savefile and a
486 * "no packets arrived before the timeout expired, try
487 * again" from a live capture.
496 * Return codes for pcap_read() are:
499 * - -2: loop was broken out of with pcap_breakloop()
501 * The first one ('0') conflicts with the return code of 0 from
502 * pcap_offline_read() meaning "end of file".
504 return (p
->read_op(p
, 1, p
->oneshot_callback
, (u_char
*)&s
));
508 * Implementation of a pcap_if_list_t.
510 struct pcap_if_list
{
511 pcap_if_t
*beginning
;
514 static struct capture_source_type
{
515 int (*findalldevs_op
)(pcap_if_list_t
*, char *);
516 pcap_t
*(*create_op
)(const char *, char *, int *);
517 } capture_source_types
[] = {
519 { dag_findalldevs
, dag_create
},
521 #ifdef HAVE_SEPTEL_API
522 { septel_findalldevs
, septel_create
},
525 { snf_findalldevs
, snf_create
},
528 { TcFindAllDevs
, TcCreate
},
530 #ifdef PCAP_SUPPORT_BT
531 { bt_findalldevs
, bt_create
},
533 #ifdef PCAP_SUPPORT_BT_MONITOR
534 { bt_monitor_findalldevs
, bt_monitor_create
},
536 #ifdef PCAP_SUPPORT_USB
537 { usb_findalldevs
, usb_create
},
539 #ifdef PCAP_SUPPORT_NETFILTER
540 { netfilter_findalldevs
, netfilter_create
},
542 #ifdef PCAP_SUPPORT_NETMAP
543 { pcap_netmap_findalldevs
, pcap_netmap_create
},
545 #ifdef PCAP_SUPPORT_DBUS
546 { dbus_findalldevs
, dbus_create
},
548 #ifdef PCAP_SUPPORT_RDMASNIFF
549 { rdmasniff_findalldevs
, rdmasniff_create
},
551 #ifdef PCAP_SUPPORT_DPDK
552 { pcap_dpdk_findalldevs
, pcap_dpdk_create
},
558 * Get a list of all capture sources that are up and that we can open.
559 * Returns -1 on error, 0 otherwise.
560 * The list, as returned through "alldevsp", may be null if no interfaces
561 * were up and could be opened.
564 pcap_findalldevs(pcap_if_t
**alldevsp
, char *errbuf
)
567 pcap_if_list_t devlist
;
570 * Find all the local network interfaces on which we
573 devlist
.beginning
= NULL
;
574 if (pcap_platform_finddevs(&devlist
, errbuf
) == -1) {
576 * Failed - free all of the entries we were given
579 if (devlist
.beginning
!= NULL
)
580 pcap_freealldevs(devlist
.beginning
);
586 * Ask each of the non-local-network-interface capture
587 * source types what interfaces they have.
589 for (i
= 0; capture_source_types
[i
].findalldevs_op
!= NULL
; i
++) {
590 if (capture_source_types
[i
].findalldevs_op(&devlist
, errbuf
) == -1) {
592 * We had an error; free the list we've been
595 if (devlist
.beginning
!= NULL
)
596 pcap_freealldevs(devlist
.beginning
);
603 * Return the first entry of the list of all devices.
605 *alldevsp
= devlist
.beginning
;
609 static struct sockaddr
*
610 dup_sockaddr(struct sockaddr
*sa
, size_t sa_length
)
612 struct sockaddr
*newsa
;
614 if ((newsa
= malloc(sa_length
)) == NULL
)
616 return (memcpy(newsa
, sa
, sa_length
));
620 * Construct a "figure of merit" for an interface, for use when sorting
621 * the list of interfaces, in which interfaces that are up are superior
622 * to interfaces that aren't up, interfaces that are up and running are
623 * superior to interfaces that are up but not running, and non-loopback
624 * interfaces that are up and running are superior to loopback interfaces,
625 * and interfaces with the same flags have a figure of merit that's higher
626 * the lower the instance number.
628 * The goal is to try to put the interfaces most likely to be useful for
629 * capture at the beginning of the list.
631 * The figure of merit, which is lower the "better" the interface is,
632 * has the uppermost bit set if the interface isn't running, the bit
633 * below that set if the interface isn't up, the bit below that
634 * set if the interface is a loopback interface, and the bit below
635 * that set if it's the "any" interface.
637 * Note: we don't sort by unit number because 1) not all interfaces have
638 * a unit number (systemd, for example, might assign interface names
639 * based on the interface's MAC address or on the physical location of
640 * the adapter's connector), and 2) if the name does end with a simple
641 * unit number, it's not a global property of the interface, it's only
642 * useful as a sort key for device names with the same prefix, so xyz0
643 * shouldn't necessarily sort before abc2. This means that interfaces
644 * with the same figure of merit will be sorted by the order in which
645 * the mechanism from which we're getting the interfaces supplies them.
648 get_figure_of_merit(pcap_if_t
*dev
)
653 if (!(dev
->flags
& PCAP_IF_RUNNING
))
655 if (!(dev
->flags
& PCAP_IF_UP
))
659 * Give non-wireless interfaces that aren't disconnected a better
660 * figure of merit than interfaces that are disconnected, as
661 * "disconnected" should indicate that the interface isn't
662 * plugged into a network and thus won't give you any traffic.
664 * For wireless interfaces, it means "associated with a network",
665 * which we presume not to necessarily prevent capture, as you
666 * might run the adapter in some flavor of monitor mode.
668 if (!(dev
->flags
& PCAP_IF_WIRELESS
) &&
669 (dev
->flags
& PCAP_IF_CONNECTION_STATUS
) == PCAP_IF_CONNECTION_STATUS_DISCONNECTED
)
673 * Sort loopback devices after non-loopback devices, *except* for
674 * disconnected devices.
676 if (dev
->flags
& PCAP_IF_LOOPBACK
)
680 * Sort the "any" device before loopback and disconnected devices,
681 * but after all other devices.
683 if (strcmp(dev
->name
, "any") == 0)
691 * Try to get a description for a given device.
692 * Returns a mallocated description if it could and NULL if it couldn't.
694 * XXX - on FreeBSDs that support it, should it get the sysctl named
695 * "dev.{adapter family name}.{adapter unit}.%desc" to get a description
696 * of the adapter? Note that "dev.an.0.%desc" is "Aironet PC4500/PC4800"
697 * with my Cisco 350 card, so the name isn't entirely descriptive. The
698 * "dev.an.0.%pnpinfo" has a better description, although one might argue
699 * that the problem is really a driver bug - if it can find out that it's
700 * a Cisco 340 or 350, rather than an old Aironet card, it should use
701 * that in the description.
703 * Do NetBSD, DragonflyBSD, or OpenBSD support this as well? FreeBSD
704 * and OpenBSD let you get a description, but it's not generated by the OS,
705 * it's set with another ioctl that ifconfig supports; we use that to get
706 * a description in FreeBSD and OpenBSD, but if there is no such
707 * description available, it still might be nice to get some description
708 * string based on the device type or something such as that.
710 * In macOS, the System Configuration framework can apparently return
711 * names in 10.4 and later.
713 * It also appears that freedesktop.org's HAL offers an "info.product"
714 * string, but the HAL specification says it "should not be used in any
715 * UI" and "subsystem/capability specific properties" should be used
716 * instead and, in any case, I think HAL is being deprecated in
717 * favor of other stuff such as DeviceKit. DeviceKit doesn't appear
718 * to have any obvious product information for devices, but maybe
719 * I haven't looked hard enough.
721 * Using the System Configuration framework, or HAL, or DeviceKit, or
722 * whatever, would require that libpcap applications be linked with
723 * the frameworks/libraries in question. That shouldn't be a problem
724 * for programs linking with the shared version of libpcap (unless
725 * you're running on AIX - which I think is the only UN*X that doesn't
726 * support linking a shared library with other libraries on which it
727 * depends, and having an executable linked only with the first shared
728 * library automatically pick up the other libraries when started -
729 * and using HAL or whatever). Programs linked with the static
730 * version of libpcap would have to use pcap-config with the --static
731 * flag in order to get the right linker flags in order to pick up
732 * the additional libraries/frameworks; those programs need that anyway
733 * for libpcap 1.1 and beyond on Linux, as, by default, it requires
736 * Do any other UN*Xes, or desktop environments support getting a
741 get_if_description(const char *name
)
743 char *description
= NULL
;
745 struct ifreq ifrdesc
;
747 size_t descrlen
= 64;
749 size_t descrlen
= IFDESCRSIZE
;
750 #endif /* IFDESCRSIZE */
753 * Get the description for the interface.
755 memset(&ifrdesc
, 0, sizeof ifrdesc
);
756 pcap_strlcpy(ifrdesc
.ifr_name
, name
, sizeof ifrdesc
.ifr_name
);
757 s
= socket(AF_INET
, SOCK_DGRAM
, 0);
761 * On FreeBSD, if the buffer isn't big enough for the
762 * description, the ioctl succeeds, but the description
763 * isn't copied, ifr_buffer.length is set to the description
764 * length, and ifr_buffer.buffer is set to NULL.
768 if ((description
= malloc(descrlen
)) != NULL
) {
769 ifrdesc
.ifr_buffer
.buffer
= description
;
770 ifrdesc
.ifr_buffer
.length
= descrlen
;
771 if (ioctl(s
, SIOCGIFDESCR
, &ifrdesc
) == 0) {
772 if (ifrdesc
.ifr_buffer
.buffer
==
776 descrlen
= ifrdesc
.ifr_buffer
.length
;
779 * Failed to get interface description.
788 #else /* __FreeBSD__ */
790 * The only other OS that currently supports
791 * SIOCGIFDESCR is OpenBSD, and it has no way
792 * to get the description length - it's clamped
793 * to a maximum of IFDESCRSIZE.
795 if ((description
= malloc(descrlen
)) != NULL
) {
796 ifrdesc
.ifr_data
= (caddr_t
)description
;
797 if (ioctl(s
, SIOCGIFDESCR
, &ifrdesc
) != 0) {
799 * Failed to get interface description.
805 #endif /* __FreeBSD__ */
807 if (description
!= NULL
&& description
[0] == '\0') {
809 * Description is empty, so discard it.
818 * For FreeBSD, if we didn't get a description, and this is
819 * a device with a name of the form usbusN, label it as a USB
822 if (description
== NULL
) {
823 if (strncmp(name
, "usbus", 5) == 0) {
825 * OK, it begins with "usbus".
831 busnum
= strtol(name
+ 5, &p
, 10);
832 if (errno
== 0 && p
!= name
+ 5 && *p
== '\0' &&
833 busnum
>= 0 && busnum
<= INT_MAX
) {
835 * OK, it's a valid number that's not
836 * bigger than INT_MAX. Construct
837 * a description from it.
838 * (If that fails, we don't worry about
839 * it, we just return NULL.)
841 if (pcap_asprintf(&description
,
842 "USB bus number %ld", busnum
) == -1) {
850 return (description
);
851 #else /* SIOCGIFDESCR */
852 get_if_description(const char *name _U_
)
855 #endif /* SIOCGIFDESCR */
859 * Look for a given device in the specified list of devices.
861 * If we find it, return a pointer to its entry.
863 * If we don't find it, attempt to add an entry for it, with the specified
864 * IFF_ flags and description, and, if that succeeds, return a pointer to
865 * the new entry, otherwise return NULL and set errbuf to an error message.
868 find_or_add_if(pcap_if_list_t
*devlistp
, const char *name
,
869 bpf_u_int32 if_flags
, get_if_flags_func get_flags_func
, char *errbuf
)
871 bpf_u_int32 pcap_flags
;
874 * Convert IFF_ flags to pcap flags.
878 if (if_flags
& IFF_LOOPBACK
)
879 pcap_flags
|= PCAP_IF_LOOPBACK
;
882 * We don't have IFF_LOOPBACK, so look at the device name to
883 * see if it looks like a loopback device.
885 if (name
[0] == 'l' && name
[1] == 'o' &&
886 (PCAP_ISDIGIT(name
[2]) || name
[2] == '\0')
887 pcap_flags
|= PCAP_IF_LOOPBACK
;
890 if (if_flags
& IFF_UP
)
891 pcap_flags
|= PCAP_IF_UP
;
894 if (if_flags
& IFF_RUNNING
)
895 pcap_flags
|= PCAP_IF_RUNNING
;
899 * Attempt to find an entry for this device; if we don't find one,
900 * attempt to add one.
902 return (find_or_add_dev(devlistp
, name
, pcap_flags
,
903 get_flags_func
, get_if_description(name
), errbuf
));
907 * Look for a given device in the specified list of devices.
909 * If we find it, then, if the specified address isn't null, add it to
910 * the list of addresses for the device and return 0.
912 * If we don't find it, attempt to add an entry for it, with the specified
913 * IFF_ flags and description, and, if that succeeds, add the specified
914 * address to its list of addresses if that address is non-null, and
915 * return 0, otherwise return -1 and set errbuf to an error message.
917 * (We can get called with a null address because we might get a list
918 * of interface name/address combinations from the underlying OS, with
919 * the address being absent in some cases, rather than a list of
920 * interfaces with each interface having a list of addresses, so this
921 * call may be the only call made to add to the list, and we want to
922 * add interfaces even if they have no addresses.)
925 add_addr_to_if(pcap_if_list_t
*devlistp
, const char *name
,
926 bpf_u_int32 if_flags
, get_if_flags_func get_flags_func
,
927 struct sockaddr
*addr
, size_t addr_size
,
928 struct sockaddr
*netmask
, size_t netmask_size
,
929 struct sockaddr
*broadaddr
, size_t broadaddr_size
,
930 struct sockaddr
*dstaddr
, size_t dstaddr_size
,
936 * Check whether the device exists and, if not, add it.
938 curdev
= find_or_add_if(devlistp
, name
, if_flags
, get_flags_func
,
940 if (curdev
== NULL
) {
949 * There's no address to add; this entry just meant
950 * "here's a new interface".
956 * "curdev" is an entry for this interface, and we have an
957 * address for it; add an entry for that address to the
958 * interface's list of addresses.
960 return (add_addr_to_dev(curdev
, addr
, addr_size
, netmask
,
961 netmask_size
, broadaddr
, broadaddr_size
, dstaddr
,
962 dstaddr_size
, errbuf
));
967 * Add an entry to the list of addresses for an interface.
968 * "curdev" is the entry for that interface.
971 add_addr_to_dev(pcap_if_t
*curdev
,
972 struct sockaddr
*addr
, size_t addr_size
,
973 struct sockaddr
*netmask
, size_t netmask_size
,
974 struct sockaddr
*broadaddr
, size_t broadaddr_size
,
975 struct sockaddr
*dstaddr
, size_t dstaddr_size
,
978 pcap_addr_t
*curaddr
, *prevaddr
, *nextaddr
;
981 * Allocate the new entry and fill it in.
983 curaddr
= (pcap_addr_t
*)malloc(sizeof(pcap_addr_t
));
984 if (curaddr
== NULL
) {
985 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
990 curaddr
->next
= NULL
;
991 if (addr
!= NULL
&& addr_size
!= 0) {
992 curaddr
->addr
= (struct sockaddr
*)dup_sockaddr(addr
, addr_size
);
993 if (curaddr
->addr
== NULL
) {
994 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
1000 curaddr
->addr
= NULL
;
1002 if (netmask
!= NULL
&& netmask_size
!= 0) {
1003 curaddr
->netmask
= (struct sockaddr
*)dup_sockaddr(netmask
, netmask_size
);
1004 if (curaddr
->netmask
== NULL
) {
1005 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
1007 if (curaddr
->addr
!= NULL
)
1008 free(curaddr
->addr
);
1013 curaddr
->netmask
= NULL
;
1015 if (broadaddr
!= NULL
&& broadaddr_size
!= 0) {
1016 curaddr
->broadaddr
= (struct sockaddr
*)dup_sockaddr(broadaddr
, broadaddr_size
);
1017 if (curaddr
->broadaddr
== NULL
) {
1018 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
1020 if (curaddr
->netmask
!= NULL
)
1021 free(curaddr
->netmask
);
1022 if (curaddr
->addr
!= NULL
)
1023 free(curaddr
->addr
);
1028 curaddr
->broadaddr
= NULL
;
1030 if (dstaddr
!= NULL
&& dstaddr_size
!= 0) {
1031 curaddr
->dstaddr
= (struct sockaddr
*)dup_sockaddr(dstaddr
, dstaddr_size
);
1032 if (curaddr
->dstaddr
== NULL
) {
1033 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
1035 if (curaddr
->broadaddr
!= NULL
)
1036 free(curaddr
->broadaddr
);
1037 if (curaddr
->netmask
!= NULL
)
1038 free(curaddr
->netmask
);
1039 if (curaddr
->addr
!= NULL
)
1040 free(curaddr
->addr
);
1045 curaddr
->dstaddr
= NULL
;
1048 * Find the end of the list of addresses.
1050 for (prevaddr
= curdev
->addresses
; prevaddr
!= NULL
; prevaddr
= nextaddr
) {
1051 nextaddr
= prevaddr
->next
;
1052 if (nextaddr
== NULL
) {
1054 * This is the end of the list.
1060 if (prevaddr
== NULL
) {
1062 * The list was empty; this is the first member.
1064 curdev
->addresses
= curaddr
;
1067 * "prevaddr" is the last member of the list; append
1068 * this member to it.
1070 prevaddr
->next
= curaddr
;
1077 * Look for a given device in the specified list of devices.
1079 * If we find it, return 0 and set *curdev_ret to point to it.
1081 * If we don't find it, attempt to add an entry for it, with the specified
1082 * flags and description, and, if that succeeds, return 0, otherwise
1083 * return -1 and set errbuf to an error message.
1086 find_or_add_dev(pcap_if_list_t
*devlistp
, const char *name
, bpf_u_int32 flags
,
1087 get_if_flags_func get_flags_func
, const char *description
, char *errbuf
)
1092 * Is there already an entry in the list for this device?
1094 curdev
= find_dev(devlistp
, name
);
1095 if (curdev
!= NULL
) {
1103 * No, we didn't find it.
1107 * Try to get additional flags for the device.
1109 if ((*get_flags_func
)(name
, &flags
, errbuf
) == -1) {
1117 * Now, try to add it to the list of devices.
1119 return (add_dev(devlistp
, name
, flags
, description
, errbuf
));
1123 * Look for a given device in the specified list of devices, and return
1124 * the entry for it if we find it or NULL if we don't.
1127 find_dev(pcap_if_list_t
*devlistp
, const char *name
)
1132 * Is there an entry in the list for this device?
1134 for (curdev
= devlistp
->beginning
; curdev
!= NULL
;
1135 curdev
= curdev
->next
) {
1136 if (strcmp(name
, curdev
->name
) == 0) {
1138 * We found it, so, yes, there is. No need to
1139 * add it. Provide the entry we found to our
1153 * Attempt to add an entry for a device, with the specified flags
1154 * and description, and, if that succeeds, return 0 and return a pointer
1155 * to the new entry, otherwise return NULL and set errbuf to an error
1158 * If we weren't given a description, try to get one.
1161 add_dev(pcap_if_list_t
*devlistp
, const char *name
, bpf_u_int32 flags
,
1162 const char *description
, char *errbuf
)
1164 pcap_if_t
*curdev
, *prevdev
, *nextdev
;
1165 u_int this_figure_of_merit
, nextdev_figure_of_merit
;
1167 curdev
= malloc(sizeof(pcap_if_t
));
1168 if (curdev
== NULL
) {
1169 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
1175 * Fill in the entry.
1177 curdev
->next
= NULL
;
1178 curdev
->name
= strdup(name
);
1179 if (curdev
->name
== NULL
) {
1180 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
1185 if (description
== NULL
) {
1187 * We weren't handed a description for the interface.
1189 curdev
->description
= NULL
;
1192 * We were handed a description; make a copy.
1194 curdev
->description
= strdup(description
);
1195 if (curdev
->description
== NULL
) {
1196 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
1203 curdev
->addresses
= NULL
; /* list starts out as empty */
1204 curdev
->flags
= flags
;
1207 * Add it to the list, in the appropriate location.
1208 * First, get the "figure of merit" for this interface.
1210 this_figure_of_merit
= get_figure_of_merit(curdev
);
1213 * Now look for the last interface with an figure of merit
1214 * less than or equal to the new interface's figure of merit.
1216 * We start with "prevdev" being NULL, meaning we're before
1217 * the first element in the list.
1222 * Get the interface after this one.
1224 if (prevdev
== NULL
) {
1226 * The next element is the first element.
1228 nextdev
= devlistp
->beginning
;
1230 nextdev
= prevdev
->next
;
1233 * Are we at the end of the list?
1235 if (nextdev
== NULL
) {
1237 * Yes - we have to put the new entry after "prevdev".
1243 * Is the new interface's figure of merit less
1244 * than the next interface's figure of merit,
1245 * meaning that the new interface is better
1246 * than the next interface?
1248 nextdev_figure_of_merit
= get_figure_of_merit(nextdev
);
1249 if (this_figure_of_merit
< nextdev_figure_of_merit
) {
1251 * Yes - we should put the new entry
1252 * before "nextdev", i.e. after "prevdev".
1261 * Insert before "nextdev".
1263 curdev
->next
= nextdev
;
1266 * Insert after "prevdev" - unless "prevdev" is null,
1267 * in which case this is the first interface.
1269 if (prevdev
== NULL
) {
1271 * This is the first interface. Make it
1272 * the first element in the list of devices.
1274 devlistp
->beginning
= curdev
;
1276 prevdev
->next
= curdev
;
1281 * Free a list of interfaces.
1284 pcap_freealldevs(pcap_if_t
*alldevs
)
1286 pcap_if_t
*curdev
, *nextdev
;
1287 pcap_addr_t
*curaddr
, *nextaddr
;
1289 for (curdev
= alldevs
; curdev
!= NULL
; curdev
= nextdev
) {
1290 nextdev
= curdev
->next
;
1293 * Free all addresses.
1295 for (curaddr
= curdev
->addresses
; curaddr
!= NULL
; curaddr
= nextaddr
) {
1296 nextaddr
= curaddr
->next
;
1298 free(curaddr
->addr
);
1299 if (curaddr
->netmask
)
1300 free(curaddr
->netmask
);
1301 if (curaddr
->broadaddr
)
1302 free(curaddr
->broadaddr
);
1303 if (curaddr
->dstaddr
)
1304 free(curaddr
->dstaddr
);
1309 * Free the name string.
1314 * Free the description string, if any.
1316 if (curdev
->description
!= NULL
)
1317 free(curdev
->description
);
1320 * Free the interface.
1327 * pcap-npf.c has its own pcap_lookupdev(), for compatibility reasons, as
1328 * it actually returns the names of all interfaces, with a NUL separator
1329 * between them; some callers may depend on that.
1331 * MS-DOS has its own pcap_lookupdev(), but that might be useful only
1332 * as an optimization.
1334 * In all other cases, we just use pcap_findalldevs() to get a list of
1335 * devices, and pick from that list.
1337 #if !defined(HAVE_PACKET32) && !defined(MSDOS)
1339 * Return the name of a network interface attached to the system, or NULL
1340 * if none can be found. The interface must be configured up; the
1341 * lowest unit number is preferred; loopback is ignored.
1344 pcap_lookupdev(char *errbuf
)
1349 * Windows - use the same size as the old WinPcap 3.1 code.
1350 * XXX - this is probably bigger than it needs to be.
1352 #define IF_NAMESIZE 8192
1355 * UN*X - use the system's interface name size.
1356 * XXX - that might not be large enough for capture devices
1357 * that aren't regular network interfaces.
1359 /* for old BSD systems, including bsdi3 */
1361 #define IF_NAMESIZE IFNAMSIZ
1364 static char device
[IF_NAMESIZE
+ 1];
1367 if (pcap_findalldevs(&alldevs
, errbuf
) == -1)
1370 if (alldevs
== NULL
|| (alldevs
->flags
& PCAP_IF_LOOPBACK
)) {
1372 * There are no devices on the list, or the first device
1373 * on the list is a loopback device, which means there
1374 * are no non-loopback devices on the list. This means
1375 * we can't return any device.
1377 * XXX - why not return a loopback device? If we can't
1378 * capture on it, it won't be on the list, and if it's
1379 * on the list, there aren't any non-loopback devices,
1380 * so why not just supply it as the default device?
1382 (void)pcap_strlcpy(errbuf
, "no suitable device found",
1387 * Return the name of the first device on the list.
1389 (void)pcap_strlcpy(device
, alldevs
->name
, sizeof(device
));
1393 pcap_freealldevs(alldevs
);
1396 #endif /* !defined(HAVE_PACKET32) && !defined(MSDOS) */
1398 #if !defined(_WIN32) && !defined(MSDOS)
1400 * We don't just fetch the entire list of devices, search for the
1401 * particular device, and use its first IPv4 address, as that's too
1402 * much work to get just one device's netmask.
1404 * If we had an API to get attributes for a given device, we could
1408 pcap_lookupnet(const char *device
, bpf_u_int32
*netp
, bpf_u_int32
*maskp
,
1412 register struct sockaddr_in
*sin4
;
1416 * The pseudo-device "any" listens on all interfaces and therefore
1417 * has the network address and -mask "0.0.0.0" therefore catching
1418 * all traffic. Using NULL for the interface is the same as "any".
1420 if (!device
|| strcmp(device
, "any") == 0
1422 || strstr(device
, "dag") != NULL
1424 #ifdef HAVE_SEPTEL_API
1425 || strstr(device
, "septel") != NULL
1427 #ifdef PCAP_SUPPORT_BT
1428 || strstr(device
, "bluetooth") != NULL
1430 #ifdef PCAP_SUPPORT_USB
1431 || strstr(device
, "usbmon") != NULL
1434 || strstr(device
, "snf") != NULL
1436 #ifdef PCAP_SUPPORT_NETMAP
1437 || strncmp(device
, "netmap:", 7) == 0
1438 || strncmp(device
, "vale", 4) == 0
1440 #ifdef PCAP_SUPPORT_DPDK
1441 || strncmp(device
, "dpdk:", 5) == 0
1448 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
1450 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
1454 memset(&ifr
, 0, sizeof(ifr
));
1456 /* XXX Work around Linux kernel bug */
1457 ifr
.ifr_addr
.sa_family
= AF_INET
;
1459 (void)pcap_strlcpy(ifr
.ifr_name
, device
, sizeof(ifr
.ifr_name
));
1460 if (ioctl(fd
, SIOCGIFADDR
, (char *)&ifr
) < 0) {
1461 if (errno
== EADDRNOTAVAIL
) {
1462 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1463 "%s: no IPv4 address assigned", device
);
1465 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
1466 errno
, "SIOCGIFADDR: %s", device
);
1471 sin4
= (struct sockaddr_in
*)&ifr
.ifr_addr
;
1472 *netp
= sin4
->sin_addr
.s_addr
;
1473 memset(&ifr
, 0, sizeof(ifr
));
1475 /* XXX Work around Linux kernel bug */
1476 ifr
.ifr_addr
.sa_family
= AF_INET
;
1478 (void)pcap_strlcpy(ifr
.ifr_name
, device
, sizeof(ifr
.ifr_name
));
1479 if (ioctl(fd
, SIOCGIFNETMASK
, (char *)&ifr
) < 0) {
1480 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
1481 errno
, "SIOCGIFNETMASK: %s", device
);
1486 *maskp
= sin4
->sin_addr
.s_addr
;
1488 if (IN_CLASSA(*netp
))
1489 *maskp
= IN_CLASSA_NET
;
1490 else if (IN_CLASSB(*netp
))
1491 *maskp
= IN_CLASSB_NET
;
1492 else if (IN_CLASSC(*netp
))
1493 *maskp
= IN_CLASSC_NET
;
1495 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1496 "inet class for 0x%x unknown", *netp
);
1503 #endif /* !defined(_WIN32) && !defined(MSDOS) */
1505 #ifdef ENABLE_REMOTE
1506 #include "pcap-rpcap.h"
1509 * Extract a substring from a string.
1512 get_substring(const char *p
, size_t len
, char *ebuf
)
1516 token
= malloc(len
+ 1);
1517 if (token
== NULL
) {
1518 pcap_fmt_errmsg_for_errno(ebuf
, PCAP_ERRBUF_SIZE
,
1522 memcpy(token
, p
, len
);
1528 * Parse a capture source that might be a URL.
1530 * If the source is not a URL, *schemep, *userinfop, *hostp, and *portp
1531 * are set to NULL, *pathp is set to point to the source, and 0 is
1534 * If source is a URL, and the URL refers to a local device (a special
1535 * case of rpcap:), *schemep, *userinfop, *hostp, and *portp are set
1536 * to NULL, *pathp is set to point to the device name, and 0 is returned.
1538 * If source is a URL, and it's not a special case that refers to a local
1539 * device, and the parse succeeds:
1541 * *schemep is set to point to an allocated string containing the scheme;
1543 * if user information is present in the URL, *userinfop is set to point
1544 * to an allocated string containing the user information, otherwise
1547 * if host information is present in the URL, *hostp is set to point
1548 * to an allocated string containing the host information, otherwise
1551 * if a port number is present in the URL, *portp is set to point
1552 * to an allocated string containing the port number, otherwise
1555 * *pathp is set to point to an allocated string containing the
1558 * and 0 is returned.
1560 * If the parse fails, ebuf is set to an error string, and -1 is returned.
1563 pcap_parse_source(const char *source
, char **schemep
, char **userinfop
,
1564 char **hostp
, char **portp
, char **pathp
, char *ebuf
)
1570 size_t authority_len
;
1572 char *parsep
, *atsignp
, *bracketp
;
1573 char *userinfo
, *host
, *port
, *path
;
1576 * Start out returning nothing.
1587 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
1589 * hier-part = "//" authority path-abempty
1594 * authority = [ userinfo "@" ] host [ ":" port ]
1596 * userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
1598 * Step 1: look for the ":" at the end of the scheme.
1599 * A colon in the source is *NOT* sufficient to indicate that
1600 * this is a URL, as interface names on some platforms might
1601 * include colons (e.g., I think some Solaris interfaces
1604 colonp
= strchr(source
, ':');
1605 if (colonp
== NULL
) {
1607 * The source is the device to open.
1608 * Return a NULL pointer for the scheme, user information,
1609 * host, and port, and return the device as the path.
1611 *pathp
= strdup(source
);
1612 if (*pathp
== NULL
) {
1613 pcap_fmt_errmsg_for_errno(ebuf
, PCAP_ERRBUF_SIZE
,
1621 * All schemes must have "//" after them, i.e. we only support
1622 * hier-part = "//" authority path-abempty, not
1623 * hier-part = path-absolute
1624 * hier-part = path-rootless
1625 * hier-part = path-empty
1627 * We need that in order to distinguish between a local device
1628 * name that happens to contain a colon and a URI.
1630 if (strncmp(colonp
+ 1, "//", 2) != 0) {
1632 * The source is the device to open.
1633 * Return a NULL pointer for the scheme, user information,
1634 * host, and port, and return the device as the path.
1636 *pathp
= strdup(source
);
1637 if (*pathp
== NULL
) {
1638 pcap_fmt_errmsg_for_errno(ebuf
, PCAP_ERRBUF_SIZE
,
1646 * XXX - check whether the purported scheme could be a scheme?
1650 * OK, this looks like a URL.
1653 scheme_len
= colonp
- source
;
1654 scheme
= malloc(scheme_len
+ 1);
1655 if (scheme
== NULL
) {
1656 pcap_fmt_errmsg_for_errno(ebuf
, PCAP_ERRBUF_SIZE
,
1660 memcpy(scheme
, source
, scheme_len
);
1661 scheme
[scheme_len
] = '\0';
1664 * Treat file: specially - take everything after file:// as
1667 if (pcap_strcasecmp(scheme
, "file") == 0) {
1668 *pathp
= strdup(colonp
+ 3);
1669 if (*pathp
== NULL
) {
1670 pcap_fmt_errmsg_for_errno(ebuf
, PCAP_ERRBUF_SIZE
,
1680 * The WinPcap documentation says you can specify a local
1681 * interface with "rpcap://{device}"; we special-case
1682 * that here. If the scheme is "rpcap", and there are
1683 * no slashes past the "//", we just return the device.
1687 if ((pcap_strcasecmp(scheme
, "rpcap") == 0 ||
1688 pcap_strcasecmp(scheme
, "rpcaps") == 0) &&
1689 strchr(colonp
+ 3, '/') == NULL
) {
1693 * Return a NULL pointer for the scheme, user information,
1694 * host, and port, and return the device as the path.
1697 *pathp
= strdup(colonp
+ 3);
1698 if (*pathp
== NULL
) {
1699 pcap_fmt_errmsg_for_errno(ebuf
, PCAP_ERRBUF_SIZE
,
1707 * OK, now start parsing the authority.
1708 * Get token, terminated with / or terminated at the end of
1711 authority_len
= strcspn(colonp
+ 3, "/");
1712 authority
= get_substring(colonp
+ 3, authority_len
, ebuf
);
1713 if (authority
== NULL
) {
1720 endp
= colonp
+ 3 + authority_len
;
1723 * Now carve the authority field into its components.
1728 * Is there a userinfo field?
1730 atsignp
= strchr(parsep
, '@');
1731 if (atsignp
!= NULL
) {
1735 size_t userinfo_len
;
1737 userinfo_len
= atsignp
- parsep
;
1738 userinfo
= get_substring(parsep
, userinfo_len
, ebuf
);
1739 if (userinfo
== NULL
) {
1747 parsep
= atsignp
+ 1;
1756 * Is there a host field?
1758 if (*parsep
== '\0') {
1760 * No; there's no host field or port field.
1771 * Is it an IP-literal?
1773 if (*parsep
== '[') {
1776 * Treat verything up to the closing square
1777 * bracket as the IP-Literal; we don't worry
1778 * about whether it's a valid IPv6address or
1779 * IPvFuture (or an IPv4address, for that
1780 * matter, just in case we get handed a
1781 * URL with an IPv4 IP-Literal, of the sort
1782 * that pcap_createsrcstr() used to generate,
1783 * and that pcap_parsesrcstr(), in the original
1784 * WinPcap code, accepted).
1786 bracketp
= strchr(parsep
, ']');
1787 if (bracketp
== NULL
) {
1789 * There's no closing square bracket.
1791 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
1792 "IP-literal in URL doesn't end with ]");
1798 if (*(bracketp
+ 1) != '\0' &&
1799 *(bracketp
+ 1) != ':') {
1801 * There's extra crud after the
1802 * closing square bracketn.
1804 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
1805 "Extra text after IP-literal in URL");
1811 host_len
= (bracketp
- 1) - parsep
;
1812 host
= get_substring(parsep
+ 1, host_len
, ebuf
);
1822 parsep
= bracketp
+ 1;
1826 * Treat everything up to a : or the end of
1827 * the string as the host.
1829 host_len
= strcspn(parsep
, ":");
1830 host
= get_substring(parsep
, host_len
, ebuf
);
1840 parsep
= parsep
+ host_len
;
1844 * Is there a port field?
1846 if (*parsep
== ':') {
1848 * Yes. It's the rest of the authority field.
1853 port_len
= strlen(parsep
);
1854 port
= get_substring(parsep
, port_len
, ebuf
);
1875 * Everything else is the path. Strip off the leading /.
1880 path
= strdup(endp
+ 1);
1882 pcap_fmt_errmsg_for_errno(ebuf
, PCAP_ERRBUF_SIZE
,
1891 *userinfop
= userinfo
;
1899 pcap_createsrcstr_ex(char *source
, int type
, const char *host
, const char *port
,
1900 const char *name
, unsigned char uses_ssl
, char *errbuf
)
1905 pcap_strlcpy(source
, PCAP_SRC_FILE_STRING
, PCAP_BUF_SIZE
);
1906 if (name
!= NULL
&& *name
!= '\0') {
1907 pcap_strlcat(source
, name
, PCAP_BUF_SIZE
);
1910 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1911 "The file name cannot be NULL.");
1915 case PCAP_SRC_IFREMOTE
:
1916 pcap_strlcpy(source
,
1917 (uses_ssl
? "rpcaps://" : PCAP_SRC_IF_STRING
),
1919 if (host
!= NULL
&& *host
!= '\0') {
1920 if (strchr(host
, ':') != NULL
) {
1922 * The host name contains a colon, so it's
1923 * probably an IPv6 address, and needs to
1924 * be included in square brackets.
1926 pcap_strlcat(source
, "[", PCAP_BUF_SIZE
);
1927 pcap_strlcat(source
, host
, PCAP_BUF_SIZE
);
1928 pcap_strlcat(source
, "]", PCAP_BUF_SIZE
);
1930 pcap_strlcat(source
, host
, PCAP_BUF_SIZE
);
1932 if (port
!= NULL
&& *port
!= '\0') {
1933 pcap_strlcat(source
, ":", PCAP_BUF_SIZE
);
1934 pcap_strlcat(source
, port
, PCAP_BUF_SIZE
);
1937 pcap_strlcat(source
, "/", PCAP_BUF_SIZE
);
1939 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1940 "The host name cannot be NULL.");
1944 if (name
!= NULL
&& *name
!= '\0')
1945 pcap_strlcat(source
, name
, PCAP_BUF_SIZE
);
1949 case PCAP_SRC_IFLOCAL
:
1950 pcap_strlcpy(source
, PCAP_SRC_IF_STRING
, PCAP_BUF_SIZE
);
1952 if (name
!= NULL
&& *name
!= '\0')
1953 pcap_strlcat(source
, name
, PCAP_BUF_SIZE
);
1958 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1959 "The interface type is not valid.");
1966 pcap_createsrcstr(char *source
, int type
, const char *host
, const char *port
,
1967 const char *name
, char *errbuf
)
1969 return (pcap_createsrcstr_ex(source
, type
, host
, port
, name
, 0, errbuf
));
1973 pcap_parsesrcstr_ex(const char *source
, int *type
, char *host
, char *port
,
1974 char *name
, unsigned char *uses_ssl
, char *errbuf
)
1976 char *scheme
, *tmpuserinfo
, *tmphost
, *tmpport
, *tmppath
;
1978 /* Initialization stuff */
1988 /* Parse the source string */
1989 if (pcap_parse_source(source
, &scheme
, &tmpuserinfo
, &tmphost
,
1990 &tmpport
, &tmppath
, errbuf
) == -1) {
1997 if (scheme
== NULL
) {
2001 if (name
&& tmppath
)
2002 pcap_strlcpy(name
, tmppath
, PCAP_BUF_SIZE
);
2004 *type
= PCAP_SRC_IFLOCAL
;
2013 if (strcmp(scheme
, "rpcaps") == 0) {
2015 if (uses_ssl
) *uses_ssl
= 1;
2016 } else if (strcmp(scheme
, "rpcap") == 0) {
2024 * pcap_parse_source() has already handled the case of
2027 if (host
&& tmphost
) {
2029 snprintf(host
, PCAP_BUF_SIZE
, "%s@%s",
2030 tmpuserinfo
, tmphost
);
2032 pcap_strlcpy(host
, tmphost
, PCAP_BUF_SIZE
);
2034 if (port
&& tmpport
)
2035 pcap_strlcpy(port
, tmpport
, PCAP_BUF_SIZE
);
2036 if (name
&& tmppath
)
2037 pcap_strlcpy(name
, tmppath
, PCAP_BUF_SIZE
);
2039 *type
= PCAP_SRC_IFREMOTE
;
2048 if (strcmp(scheme
, "file") == 0) {
2052 if (name
&& tmppath
)
2053 pcap_strlcpy(name
, tmppath
, PCAP_BUF_SIZE
);
2055 *type
= PCAP_SRC_FILE
;
2065 * Neither rpcap: nor file:; just treat the entire string
2066 * as a local device.
2069 pcap_strlcpy(name
, source
, PCAP_BUF_SIZE
);
2071 *type
= PCAP_SRC_IFLOCAL
;
2081 pcap_parsesrcstr(const char *source
, int *type
, char *host
, char *port
,
2082 char *name
, char *errbuf
)
2084 return (pcap_parsesrcstr_ex(source
, type
, host
, port
, name
, NULL
, errbuf
));
2089 pcap_create(const char *device
, char *errbuf
)
2097 * A null device name is equivalent to the "any" device -
2098 * which might not be supported on this platform, but
2099 * this means that you'll get a "not supported" error
2100 * rather than, say, a crash when we try to dereference
2104 device_str
= strdup("any");
2108 * On Windows, for backwards compatibility reasons,
2109 * pcap_lookupdev() returns a pointer to a sequence of
2110 * pairs of UTF-16LE device names and local code page
2111 * description strings.
2113 * This means that if a program uses pcap_lookupdev()
2114 * to get a default device, and hands that to an API
2115 * that opens devices, we'll get handed a UTF-16LE
2116 * string, not a string in the local code page.
2118 * To work around that, we check whether the string
2119 * looks as if it might be a UTF-16LE string and, if
2120 * so, convert it back to the local code page's
2123 * XXX - you *cannot* reliably detect whether a
2124 * string is UTF-16LE or not; "a" could either
2125 * be a one-character ASCII string or the first
2126 * character of a UTF-16LE string. This particular
2127 * version of this heuristic dates back to WinPcap
2128 * 4.1.1; PacketOpenAdapter() does uses the same
2129 * heuristic, with the exact same vulnerability.
2131 if (device
[0] != '\0' && device
[1] == '\0') {
2134 length
= wcslen((wchar_t *)device
);
2135 device_str
= (char *)malloc(length
+ 1);
2136 if (device_str
== NULL
) {
2137 pcap_fmt_errmsg_for_errno(errbuf
,
2138 PCAP_ERRBUF_SIZE
, errno
,
2143 snprintf(device_str
, length
+ 1, "%ws",
2144 (const wchar_t *)device
);
2147 device_str
= strdup(device
);
2149 if (device_str
== NULL
) {
2150 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
2156 * Try each of the non-local-network-interface capture
2157 * source types until we find one that works for this
2158 * device or run out of types.
2160 for (i
= 0; capture_source_types
[i
].create_op
!= NULL
; i
++) {
2162 p
= capture_source_types
[i
].create_op(device_str
, errbuf
,
2166 * The device name refers to a device of the
2167 * type in question; either it succeeded,
2168 * in which case p refers to a pcap_t to
2169 * later activate for the device, or it
2170 * failed, in which case p is null and we
2171 * should return that to report the failure
2176 * We assume the caller filled in errbuf.
2181 p
->opt
.device
= device_str
;
2187 * OK, try it as a regular network interface.
2189 p
= pcap_create_interface(device_str
, errbuf
);
2192 * We assume the caller filled in errbuf.
2197 p
->opt
.device
= device_str
;
2202 * Set nonblocking mode on an unactivated pcap_t; this sets a flag
2203 * checked by pcap_activate(), which sets the mode after calling
2204 * the activate routine.
2207 pcap_setnonblock_unactivated(pcap_t
*p
, int nonblock
)
2209 p
->opt
.nonblock
= nonblock
;
2214 initialize_ops(pcap_t
*p
)
2217 * Set operation pointers for operations that only work on
2218 * an activated pcap_t to point to a routine that returns
2219 * a "this isn't activated" error.
2221 p
->read_op
= pcap_read_not_initialized
;
2222 p
->inject_op
= pcap_inject_not_initialized
;
2223 p
->setfilter_op
= pcap_setfilter_not_initialized
;
2224 p
->setdirection_op
= pcap_setdirection_not_initialized
;
2225 p
->set_datalink_op
= pcap_set_datalink_not_initialized
;
2226 p
->getnonblock_op
= pcap_getnonblock_not_initialized
;
2227 p
->stats_op
= pcap_stats_not_initialized
;
2229 p
->stats_ex_op
= pcap_stats_ex_not_initialized
;
2230 p
->setbuff_op
= pcap_setbuff_not_initialized
;
2231 p
->setmode_op
= pcap_setmode_not_initialized
;
2232 p
->setmintocopy_op
= pcap_setmintocopy_not_initialized
;
2233 p
->getevent_op
= pcap_getevent_not_initialized
;
2234 p
->oid_get_request_op
= pcap_oid_get_request_not_initialized
;
2235 p
->oid_set_request_op
= pcap_oid_set_request_not_initialized
;
2236 p
->sendqueue_transmit_op
= pcap_sendqueue_transmit_not_initialized
;
2237 p
->setuserbuffer_op
= pcap_setuserbuffer_not_initialized
;
2238 p
->live_dump_op
= pcap_live_dump_not_initialized
;
2239 p
->live_dump_ended_op
= pcap_live_dump_ended_not_initialized
;
2240 p
->get_airpcap_handle_op
= pcap_get_airpcap_handle_not_initialized
;
2244 * Default cleanup operation - implementations can override
2245 * this, but should call pcap_cleanup_live_common() after
2246 * doing their own additional cleanup.
2248 p
->cleanup_op
= pcap_cleanup_live_common
;
2251 * In most cases, the standard one-shot callback can
2252 * be used for pcap_next()/pcap_next_ex().
2254 p
->oneshot_callback
= pcap_oneshot
;
2257 * Default breakloop operation - implementations can override
2258 * this, but should call pcap_breakloop_common() before doing
2261 p
->breakloop_op
= pcap_breakloop_common
;
2265 pcap_alloc_pcap_t(char *ebuf
, size_t size
)
2271 * Allocate a chunk of memory big enough for a pcap_t
2272 * plus a structure following it of size "size". The
2273 * structure following it is a private data structure
2274 * for the routines that handle this pcap_t.
2276 * The structure following it must be aligned on
2277 * the appropriate alignment boundary for this platform.
2278 * We align on an 8-byte boundary as that's probably what
2279 * at least some platforms do, even with 32-bit integers,
2280 * and because we can't be sure that some values won't
2281 * require 8-byte alignment even on platforms with 32-bit
2284 #define PCAP_T_ALIGNED_SIZE ((sizeof(pcap_t) + 7U) & ~0x7U)
2285 chunk
= malloc(PCAP_T_ALIGNED_SIZE
+ size
);
2286 if (chunk
== NULL
) {
2287 pcap_fmt_errmsg_for_errno(ebuf
, PCAP_ERRBUF_SIZE
,
2291 memset(chunk
, 0, PCAP_T_ALIGNED_SIZE
+ size
);
2294 * Get a pointer to the pcap_t at the beginning.
2296 p
= (pcap_t
*)chunk
;
2299 p
->handle
= INVALID_HANDLE_VALUE
; /* not opened yet */
2301 p
->fd
= -1; /* not opened yet */
2303 p
->selectable_fd
= -1;
2304 p
->required_select_timeout
= NULL
;
2309 /* No private data was requested. */
2313 * Set the pointer to the private data; that's the structure
2314 * of size "size" following the pcap_t.
2316 p
->priv
= (void *)(chunk
+ PCAP_T_ALIGNED_SIZE
);
2323 pcap_create_common(char *ebuf
, size_t size
)
2327 p
= pcap_alloc_pcap_t(ebuf
, size
);
2332 * Default to "can't set rfmon mode"; if it's supported by
2333 * a platform, the create routine that called us can set
2334 * the op to its routine to check whether a particular
2335 * device supports it.
2337 p
->can_set_rfmon_op
= pcap_cant_set_rfmon
;
2340 * If pcap_setnonblock() is called on a not-yet-activated
2341 * pcap_t, default to setting a flag and turning
2342 * on non-blocking mode when activated.
2344 p
->setnonblock_op
= pcap_setnonblock_unactivated
;
2348 /* put in some defaults*/
2349 p
->snapshot
= 0; /* max packet size unspecified */
2350 p
->opt
.timeout
= 0; /* no timeout specified */
2351 p
->opt
.buffer_size
= 0; /* use the platform's default */
2354 p
->opt
.immediate
= 0;
2355 p
->opt
.tstamp_type
= -1; /* default to not setting time stamp type */
2356 p
->opt
.tstamp_precision
= PCAP_TSTAMP_PRECISION_MICRO
;
2358 * Platform-dependent options.
2361 p
->opt
.protocol
= 0;
2364 p
->opt
.nocapture_local
= 0;
2368 * Start out with no BPF code generation flags set.
2370 p
->bpf_codegen_flags
= 0;
2376 pcap_check_activated(pcap_t
*p
)
2379 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "can't perform "
2380 " operation on activated capture");
2387 pcap_set_snaplen(pcap_t
*p
, int snaplen
)
2389 if (pcap_check_activated(p
))
2390 return (PCAP_ERROR_ACTIVATED
);
2391 p
->snapshot
= snaplen
;
2396 pcap_set_promisc(pcap_t
*p
, int promisc
)
2398 if (pcap_check_activated(p
))
2399 return (PCAP_ERROR_ACTIVATED
);
2400 p
->opt
.promisc
= promisc
;
2405 pcap_set_rfmon(pcap_t
*p
, int rfmon
)
2407 if (pcap_check_activated(p
))
2408 return (PCAP_ERROR_ACTIVATED
);
2409 p
->opt
.rfmon
= rfmon
;
2414 pcap_set_timeout(pcap_t
*p
, int timeout_ms
)
2416 if (pcap_check_activated(p
))
2417 return (PCAP_ERROR_ACTIVATED
);
2418 p
->opt
.timeout
= timeout_ms
;
2423 pcap_set_tstamp_type(pcap_t
*p
, int tstamp_type
)
2427 if (pcap_check_activated(p
))
2428 return (PCAP_ERROR_ACTIVATED
);
2431 * The argument should have been u_int, but that's too late
2432 * to change now - it's an API.
2434 if (tstamp_type
< 0)
2435 return (PCAP_WARNING_TSTAMP_TYPE_NOTSUP
);
2438 * If p->tstamp_type_count is 0, we only support PCAP_TSTAMP_HOST;
2439 * the default time stamp type is PCAP_TSTAMP_HOST.
2441 if (p
->tstamp_type_count
== 0) {
2442 if (tstamp_type
== PCAP_TSTAMP_HOST
) {
2443 p
->opt
.tstamp_type
= tstamp_type
;
2448 * Check whether we claim to support this type of time stamp.
2450 for (i
= 0; i
< p
->tstamp_type_count
; i
++) {
2451 if (p
->tstamp_type_list
[i
] == (u_int
)tstamp_type
) {
2455 p
->opt
.tstamp_type
= tstamp_type
;
2462 * We don't support this type of time stamp.
2464 return (PCAP_WARNING_TSTAMP_TYPE_NOTSUP
);
2468 pcap_set_immediate_mode(pcap_t
*p
, int immediate
)
2470 if (pcap_check_activated(p
))
2471 return (PCAP_ERROR_ACTIVATED
);
2472 p
->opt
.immediate
= immediate
;
2477 pcap_set_buffer_size(pcap_t
*p
, int buffer_size
)
2479 if (pcap_check_activated(p
))
2480 return (PCAP_ERROR_ACTIVATED
);
2481 if (buffer_size
<= 0) {
2483 * Silently ignore invalid values.
2487 p
->opt
.buffer_size
= buffer_size
;
2492 pcap_set_tstamp_precision(pcap_t
*p
, int tstamp_precision
)
2496 if (pcap_check_activated(p
))
2497 return (PCAP_ERROR_ACTIVATED
);
2500 * The argument should have been u_int, but that's too late
2501 * to change now - it's an API.
2503 if (tstamp_precision
< 0)
2504 return (PCAP_ERROR_TSTAMP_PRECISION_NOTSUP
);
2507 * If p->tstamp_precision_count is 0, we only support setting
2508 * the time stamp precision to microsecond precision; every
2509 * pcap module *MUST* support microsecond precision, even if
2510 * it does so by converting the native precision to
2513 if (p
->tstamp_precision_count
== 0) {
2514 if (tstamp_precision
== PCAP_TSTAMP_PRECISION_MICRO
) {
2515 p
->opt
.tstamp_precision
= tstamp_precision
;
2520 * Check whether we claim to support this precision of
2523 for (i
= 0; i
< p
->tstamp_precision_count
; i
++) {
2524 if (p
->tstamp_precision_list
[i
] == (u_int
)tstamp_precision
) {
2528 p
->opt
.tstamp_precision
= tstamp_precision
;
2535 * We don't support this time stamp precision.
2537 return (PCAP_ERROR_TSTAMP_PRECISION_NOTSUP
);
2541 pcap_get_tstamp_precision(pcap_t
*p
)
2543 return (p
->opt
.tstamp_precision
);
2547 pcap_activate(pcap_t
*p
)
2552 * Catch attempts to re-activate an already-activated
2553 * pcap_t; this should, for example, catch code that
2554 * calls pcap_open_live() followed by pcap_activate(),
2555 * as some code that showed up in a Stack Exchange
2558 if (pcap_check_activated(p
))
2559 return (PCAP_ERROR_ACTIVATED
);
2560 status
= p
->activate_op(p
);
2563 * If somebody requested non-blocking mode before
2564 * calling pcap_activate(), turn it on now.
2566 if (p
->opt
.nonblock
) {
2567 status
= p
->setnonblock_op(p
, 1);
2570 * Failed. Undo everything done by
2571 * the activate operation.
2580 if (p
->errbuf
[0] == '\0') {
2582 * No error message supplied by the activate routine;
2583 * for the benefit of programs that don't specially
2584 * handle errors other than PCAP_ERROR, return the
2585 * error message corresponding to the status.
2587 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "%s",
2588 pcap_statustostr(status
));
2592 * Undo any operation pointer setting, etc. done by
2593 * the activate operation.
2601 pcap_open_live(const char *device
, int snaplen
, int promisc
, int to_ms
, char *errbuf
)
2605 #ifdef ENABLE_REMOTE
2606 char host
[PCAP_BUF_SIZE
+ 1];
2607 char port
[PCAP_BUF_SIZE
+ 1];
2608 char name
[PCAP_BUF_SIZE
+ 1];
2612 * A null device name is equivalent to the "any" device -
2613 * which might not be supported on this platform, but
2614 * this means that you'll get a "not supported" error
2615 * rather than, say, a crash when we try to dereference
2622 * Retrofit - we have to make older applications compatible with
2624 * So we're calling pcap_open_remote() from here; this is a very
2626 * Obviously, we cannot exploit all the new features; for instance,
2627 * we cannot send authentication, we cannot use a UDP data connection,
2630 if (pcap_parsesrcstr(device
, &srctype
, host
, port
, name
, errbuf
))
2633 if (srctype
== PCAP_SRC_IFREMOTE
) {
2635 * Although we already have host, port and iface, we prefer
2636 * to pass only 'device' to pcap_open_rpcap(), so that it has
2637 * to call pcap_parsesrcstr() again.
2638 * This is less optimized, but much clearer.
2640 return (pcap_open_rpcap(device
, snaplen
,
2641 promisc
? PCAP_OPENFLAG_PROMISCUOUS
: 0, to_ms
,
2644 if (srctype
== PCAP_SRC_FILE
) {
2645 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "unknown URL scheme \"file\"");
2648 if (srctype
== PCAP_SRC_IFLOCAL
) {
2650 * If it starts with rpcap://, that refers to a local device
2651 * (no host part in the URL). Remove the rpcap://, and
2652 * fall through to the regular open path.
2654 if (strncmp(device
, PCAP_SRC_IF_STRING
, strlen(PCAP_SRC_IF_STRING
)) == 0) {
2655 size_t len
= strlen(device
) - strlen(PCAP_SRC_IF_STRING
) + 1;
2658 device
+= strlen(PCAP_SRC_IF_STRING
);
2661 #endif /* ENABLE_REMOTE */
2663 p
= pcap_create(device
, errbuf
);
2666 status
= pcap_set_snaplen(p
, snaplen
);
2669 status
= pcap_set_promisc(p
, promisc
);
2672 status
= pcap_set_timeout(p
, to_ms
);
2676 * Mark this as opened with pcap_open_live(), so that, for
2677 * example, we show the full list of DLT_ values, rather
2678 * than just the ones that are compatible with capturing
2679 * when not in monitor mode. That allows existing applications
2680 * to work the way they used to work, but allows new applications
2681 * that know about the new open API to, for example, find out the
2682 * DLT_ values that they can select without changing whether
2683 * the adapter is in monitor mode or not.
2686 status
= pcap_activate(p
);
2691 if (status
== PCAP_ERROR
)
2692 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "%s: %.*s", device
,
2693 PCAP_ERRBUF_SIZE
- 3, p
->errbuf
);
2694 else if (status
== PCAP_ERROR_NO_SUCH_DEVICE
||
2695 status
== PCAP_ERROR_PERM_DENIED
||
2696 status
== PCAP_ERROR_PROMISC_PERM_DENIED
)
2697 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "%s: %s (%.*s)", device
,
2698 pcap_statustostr(status
), PCAP_ERRBUF_SIZE
- 6, p
->errbuf
);
2700 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "%s: %s", device
,
2701 pcap_statustostr(status
));
2707 pcap_open_offline_common(char *ebuf
, size_t size
)
2711 p
= pcap_alloc_pcap_t(ebuf
, size
);
2715 p
->opt
.tstamp_precision
= PCAP_TSTAMP_PRECISION_MICRO
;
2721 pcap_dispatch(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
2723 return (p
->read_op(p
, cnt
, callback
, user
));
2727 pcap_loop(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
2732 if (p
->rfile
!= NULL
) {
2734 * 0 means EOF, so don't loop if we get 0.
2736 n
= pcap_offline_read(p
, cnt
, callback
, user
);
2739 * XXX keep reading until we get something
2740 * (or an error occurs)
2743 n
= p
->read_op(p
, cnt
, callback
, user
);
2748 if (!PACKET_COUNT_IS_UNLIMITED(cnt
)) {
2757 * Force the loop in "pcap_read()" or "pcap_read_offline()" to terminate.
2760 pcap_breakloop(pcap_t
*p
)
2766 pcap_datalink(pcap_t
*p
)
2769 return (PCAP_ERROR_NOT_ACTIVATED
);
2770 return (p
->linktype
);
2774 pcap_datalink_ext(pcap_t
*p
)
2777 return (PCAP_ERROR_NOT_ACTIVATED
);
2778 return (p
->linktype_ext
);
2782 pcap_list_datalinks(pcap_t
*p
, int **dlt_buffer
)
2785 return (PCAP_ERROR_NOT_ACTIVATED
);
2786 if (p
->dlt_count
== 0) {
2788 * We couldn't fetch the list of DLTs, which means
2789 * this platform doesn't support changing the
2790 * DLT for an interface. Return a list of DLTs
2791 * containing only the DLT this device supports.
2793 *dlt_buffer
= (int*)malloc(sizeof(**dlt_buffer
));
2794 if (*dlt_buffer
== NULL
) {
2795 pcap_fmt_errmsg_for_errno(p
->errbuf
, sizeof(p
->errbuf
),
2797 return (PCAP_ERROR
);
2799 **dlt_buffer
= p
->linktype
;
2802 *dlt_buffer
= (int*)calloc(sizeof(**dlt_buffer
), p
->dlt_count
);
2803 if (*dlt_buffer
== NULL
) {
2804 pcap_fmt_errmsg_for_errno(p
->errbuf
, sizeof(p
->errbuf
),
2806 return (PCAP_ERROR
);
2808 (void)memcpy(*dlt_buffer
, p
->dlt_list
,
2809 sizeof(**dlt_buffer
) * p
->dlt_count
);
2810 return (p
->dlt_count
);
2815 * In Windows, you might have a library built with one version of the
2816 * C runtime library and an application built with another version of
2817 * the C runtime library, which means that the library might use one
2818 * version of malloc() and free() and the application might use another
2819 * version of malloc() and free(). If so, that means something
2820 * allocated by the library cannot be freed by the application, so we
2821 * need to have a pcap_free_datalinks() routine to free up the list
2822 * allocated by pcap_list_datalinks(), even though it's just a wrapper
2826 pcap_free_datalinks(int *dlt_list
)
2832 pcap_set_datalink(pcap_t
*p
, int dlt
)
2835 const char *dlt_name
;
2840 if (p
->dlt_count
== 0 || p
->set_datalink_op
== NULL
) {
2842 * We couldn't fetch the list of DLTs, or we don't
2843 * have a "set datalink" operation, which means
2844 * this platform doesn't support changing the
2845 * DLT for an interface. Check whether the new
2846 * DLT is the one this interface supports.
2848 if (p
->linktype
!= dlt
)
2852 * It is, so there's nothing we need to do here.
2856 for (i
= 0; i
< p
->dlt_count
; i
++)
2857 if (p
->dlt_list
[i
] == (u_int
)dlt
)
2859 if (i
>= p
->dlt_count
)
2861 if (p
->dlt_count
== 2 && p
->dlt_list
[0] == DLT_EN10MB
&&
2862 dlt
== DLT_DOCSIS
) {
2864 * This is presumably an Ethernet device, as the first
2865 * link-layer type it offers is DLT_EN10MB, and the only
2866 * other type it offers is DLT_DOCSIS. That means that
2867 * we can't tell the driver to supply DOCSIS link-layer
2868 * headers - we're just pretending that's what we're
2869 * getting, as, presumably, we're capturing on a dedicated
2870 * link to a Cisco Cable Modem Termination System, and
2871 * it's putting raw DOCSIS frames on the wire inside low-level
2877 if (p
->set_datalink_op(p
, dlt
) == -1)
2883 dlt_name
= pcap_datalink_val_to_name(dlt
);
2884 if (dlt_name
!= NULL
) {
2885 (void) snprintf(p
->errbuf
, sizeof(p
->errbuf
),
2886 "%s is not one of the DLTs supported by this device",
2889 (void) snprintf(p
->errbuf
, sizeof(p
->errbuf
),
2890 "DLT %d is not one of the DLTs supported by this device",
2897 * This array is designed for mapping upper and lower case letter
2898 * together for a case independent comparison. The mappings are
2899 * based upon ascii character sequences.
2901 static const u_char charmap
[] = {
2902 (u_char
)'\000', (u_char
)'\001', (u_char
)'\002', (u_char
)'\003',
2903 (u_char
)'\004', (u_char
)'\005', (u_char
)'\006', (u_char
)'\007',
2904 (u_char
)'\010', (u_char
)'\011', (u_char
)'\012', (u_char
)'\013',
2905 (u_char
)'\014', (u_char
)'\015', (u_char
)'\016', (u_char
)'\017',
2906 (u_char
)'\020', (u_char
)'\021', (u_char
)'\022', (u_char
)'\023',
2907 (u_char
)'\024', (u_char
)'\025', (u_char
)'\026', (u_char
)'\027',
2908 (u_char
)'\030', (u_char
)'\031', (u_char
)'\032', (u_char
)'\033',
2909 (u_char
)'\034', (u_char
)'\035', (u_char
)'\036', (u_char
)'\037',
2910 (u_char
)'\040', (u_char
)'\041', (u_char
)'\042', (u_char
)'\043',
2911 (u_char
)'\044', (u_char
)'\045', (u_char
)'\046', (u_char
)'\047',
2912 (u_char
)'\050', (u_char
)'\051', (u_char
)'\052', (u_char
)'\053',
2913 (u_char
)'\054', (u_char
)'\055', (u_char
)'\056', (u_char
)'\057',
2914 (u_char
)'\060', (u_char
)'\061', (u_char
)'\062', (u_char
)'\063',
2915 (u_char
)'\064', (u_char
)'\065', (u_char
)'\066', (u_char
)'\067',
2916 (u_char
)'\070', (u_char
)'\071', (u_char
)'\072', (u_char
)'\073',
2917 (u_char
)'\074', (u_char
)'\075', (u_char
)'\076', (u_char
)'\077',
2918 (u_char
)'\100', (u_char
)'\141', (u_char
)'\142', (u_char
)'\143',
2919 (u_char
)'\144', (u_char
)'\145', (u_char
)'\146', (u_char
)'\147',
2920 (u_char
)'\150', (u_char
)'\151', (u_char
)'\152', (u_char
)'\153',
2921 (u_char
)'\154', (u_char
)'\155', (u_char
)'\156', (u_char
)'\157',
2922 (u_char
)'\160', (u_char
)'\161', (u_char
)'\162', (u_char
)'\163',
2923 (u_char
)'\164', (u_char
)'\165', (u_char
)'\166', (u_char
)'\167',
2924 (u_char
)'\170', (u_char
)'\171', (u_char
)'\172', (u_char
)'\133',
2925 (u_char
)'\134', (u_char
)'\135', (u_char
)'\136', (u_char
)'\137',
2926 (u_char
)'\140', (u_char
)'\141', (u_char
)'\142', (u_char
)'\143',
2927 (u_char
)'\144', (u_char
)'\145', (u_char
)'\146', (u_char
)'\147',
2928 (u_char
)'\150', (u_char
)'\151', (u_char
)'\152', (u_char
)'\153',
2929 (u_char
)'\154', (u_char
)'\155', (u_char
)'\156', (u_char
)'\157',
2930 (u_char
)'\160', (u_char
)'\161', (u_char
)'\162', (u_char
)'\163',
2931 (u_char
)'\164', (u_char
)'\165', (u_char
)'\166', (u_char
)'\167',
2932 (u_char
)'\170', (u_char
)'\171', (u_char
)'\172', (u_char
)'\173',
2933 (u_char
)'\174', (u_char
)'\175', (u_char
)'\176', (u_char
)'\177',
2934 (u_char
)'\200', (u_char
)'\201', (u_char
)'\202', (u_char
)'\203',
2935 (u_char
)'\204', (u_char
)'\205', (u_char
)'\206', (u_char
)'\207',
2936 (u_char
)'\210', (u_char
)'\211', (u_char
)'\212', (u_char
)'\213',
2937 (u_char
)'\214', (u_char
)'\215', (u_char
)'\216', (u_char
)'\217',
2938 (u_char
)'\220', (u_char
)'\221', (u_char
)'\222', (u_char
)'\223',
2939 (u_char
)'\224', (u_char
)'\225', (u_char
)'\226', (u_char
)'\227',
2940 (u_char
)'\230', (u_char
)'\231', (u_char
)'\232', (u_char
)'\233',
2941 (u_char
)'\234', (u_char
)'\235', (u_char
)'\236', (u_char
)'\237',
2942 (u_char
)'\240', (u_char
)'\241', (u_char
)'\242', (u_char
)'\243',
2943 (u_char
)'\244', (u_char
)'\245', (u_char
)'\246', (u_char
)'\247',
2944 (u_char
)'\250', (u_char
)'\251', (u_char
)'\252', (u_char
)'\253',
2945 (u_char
)'\254', (u_char
)'\255', (u_char
)'\256', (u_char
)'\257',
2946 (u_char
)'\260', (u_char
)'\261', (u_char
)'\262', (u_char
)'\263',
2947 (u_char
)'\264', (u_char
)'\265', (u_char
)'\266', (u_char
)'\267',
2948 (u_char
)'\270', (u_char
)'\271', (u_char
)'\272', (u_char
)'\273',
2949 (u_char
)'\274', (u_char
)'\275', (u_char
)'\276', (u_char
)'\277',
2950 (u_char
)'\300', (u_char
)'\341', (u_char
)'\342', (u_char
)'\343',
2951 (u_char
)'\344', (u_char
)'\345', (u_char
)'\346', (u_char
)'\347',
2952 (u_char
)'\350', (u_char
)'\351', (u_char
)'\352', (u_char
)'\353',
2953 (u_char
)'\354', (u_char
)'\355', (u_char
)'\356', (u_char
)'\357',
2954 (u_char
)'\360', (u_char
)'\361', (u_char
)'\362', (u_char
)'\363',
2955 (u_char
)'\364', (u_char
)'\365', (u_char
)'\366', (u_char
)'\367',
2956 (u_char
)'\370', (u_char
)'\371', (u_char
)'\372', (u_char
)'\333',
2957 (u_char
)'\334', (u_char
)'\335', (u_char
)'\336', (u_char
)'\337',
2958 (u_char
)'\340', (u_char
)'\341', (u_char
)'\342', (u_char
)'\343',
2959 (u_char
)'\344', (u_char
)'\345', (u_char
)'\346', (u_char
)'\347',
2960 (u_char
)'\350', (u_char
)'\351', (u_char
)'\352', (u_char
)'\353',
2961 (u_char
)'\354', (u_char
)'\355', (u_char
)'\356', (u_char
)'\357',
2962 (u_char
)'\360', (u_char
)'\361', (u_char
)'\362', (u_char
)'\363',
2963 (u_char
)'\364', (u_char
)'\365', (u_char
)'\366', (u_char
)'\367',
2964 (u_char
)'\370', (u_char
)'\371', (u_char
)'\372', (u_char
)'\373',
2965 (u_char
)'\374', (u_char
)'\375', (u_char
)'\376', (u_char
)'\377',
2969 pcap_strcasecmp(const char *s1
, const char *s2
)
2971 register const u_char
*cm
= charmap
,
2972 *us1
= (const u_char
*)s1
,
2973 *us2
= (const u_char
*)s2
;
2975 while (cm
[*us1
] == cm
[*us2
++])
2978 return (cm
[*us1
] - cm
[*--us2
]);
2983 const char *description
;
2987 #define DLT_CHOICE(code, description) { #code, description, DLT_ ## code }
2988 #define DLT_CHOICE_SENTINEL { NULL, NULL, 0 }
2990 static struct dlt_choice dlt_choices
[] = {
2991 DLT_CHOICE(NULL
, "BSD loopback"),
2992 DLT_CHOICE(EN10MB
, "Ethernet"),
2993 DLT_CHOICE(IEEE802
, "Token ring"),
2994 DLT_CHOICE(ARCNET
, "BSD ARCNET"),
2995 DLT_CHOICE(SLIP
, "SLIP"),
2996 DLT_CHOICE(PPP
, "PPP"),
2997 DLT_CHOICE(FDDI
, "FDDI"),
2998 DLT_CHOICE(ATM_RFC1483
, "RFC 1483 LLC-encapsulated ATM"),
2999 DLT_CHOICE(RAW
, "Raw IP"),
3000 DLT_CHOICE(SLIP_BSDOS
, "BSD/OS SLIP"),
3001 DLT_CHOICE(PPP_BSDOS
, "BSD/OS PPP"),
3002 DLT_CHOICE(ATM_CLIP
, "Linux Classical IP-over-ATM"),
3003 DLT_CHOICE(PPP_SERIAL
, "PPP over serial"),
3004 DLT_CHOICE(PPP_ETHER
, "PPPoE"),
3005 DLT_CHOICE(SYMANTEC_FIREWALL
, "Symantec Firewall"),
3006 DLT_CHOICE(C_HDLC
, "Cisco HDLC"),
3007 DLT_CHOICE(IEEE802_11
, "802.11"),
3008 DLT_CHOICE(FRELAY
, "Frame Relay"),
3009 DLT_CHOICE(LOOP
, "OpenBSD loopback"),
3010 DLT_CHOICE(ENC
, "OpenBSD encapsulated IP"),
3011 DLT_CHOICE(LINUX_SLL
, "Linux cooked v1"),
3012 DLT_CHOICE(LTALK
, "Localtalk"),
3013 DLT_CHOICE(PFLOG
, "OpenBSD pflog file"),
3014 DLT_CHOICE(PFSYNC
, "Packet filter state syncing"),
3015 DLT_CHOICE(PRISM_HEADER
, "802.11 plus Prism header"),
3016 DLT_CHOICE(IP_OVER_FC
, "RFC 2625 IP-over-Fibre Channel"),
3017 DLT_CHOICE(SUNATM
, "Sun raw ATM"),
3018 DLT_CHOICE(IEEE802_11_RADIO
, "802.11 plus radiotap header"),
3019 DLT_CHOICE(ARCNET_LINUX
, "Linux ARCNET"),
3020 DLT_CHOICE(JUNIPER_MLPPP
, "Juniper Multi-Link PPP"),
3021 DLT_CHOICE(JUNIPER_MLFR
, "Juniper Multi-Link Frame Relay"),
3022 DLT_CHOICE(JUNIPER_ES
, "Juniper Encryption Services PIC"),
3023 DLT_CHOICE(JUNIPER_GGSN
, "Juniper GGSN PIC"),
3024 DLT_CHOICE(JUNIPER_MFR
, "Juniper FRF.16 Frame Relay"),
3025 DLT_CHOICE(JUNIPER_ATM2
, "Juniper ATM2 PIC"),
3026 DLT_CHOICE(JUNIPER_SERVICES
, "Juniper Advanced Services PIC"),
3027 DLT_CHOICE(JUNIPER_ATM1
, "Juniper ATM1 PIC"),
3028 DLT_CHOICE(APPLE_IP_OVER_IEEE1394
, "Apple IP-over-IEEE 1394"),
3029 DLT_CHOICE(MTP2_WITH_PHDR
, "SS7 MTP2 with Pseudo-header"),
3030 DLT_CHOICE(MTP2
, "SS7 MTP2"),
3031 DLT_CHOICE(MTP3
, "SS7 MTP3"),
3032 DLT_CHOICE(SCCP
, "SS7 SCCP"),
3033 DLT_CHOICE(DOCSIS
, "DOCSIS"),
3034 DLT_CHOICE(LINUX_IRDA
, "Linux IrDA"),
3035 DLT_CHOICE(IEEE802_11_RADIO_AVS
, "802.11 plus AVS radio information header"),
3036 DLT_CHOICE(JUNIPER_MONITOR
, "Juniper Passive Monitor PIC"),
3037 DLT_CHOICE(BACNET_MS_TP
, "BACnet MS/TP"),
3038 DLT_CHOICE(PPP_PPPD
, "PPP for pppd, with direction flag"),
3039 DLT_CHOICE(JUNIPER_PPPOE
, "Juniper PPPoE"),
3040 DLT_CHOICE(JUNIPER_PPPOE_ATM
, "Juniper PPPoE/ATM"),
3041 DLT_CHOICE(GPRS_LLC
, "GPRS LLC"),
3042 DLT_CHOICE(GPF_T
, "GPF-T"),
3043 DLT_CHOICE(GPF_F
, "GPF-F"),
3044 DLT_CHOICE(JUNIPER_PIC_PEER
, "Juniper PIC Peer"),
3045 DLT_CHOICE(ERF_ETH
, "Ethernet with Endace ERF header"),
3046 DLT_CHOICE(ERF_POS
, "Packet-over-SONET with Endace ERF header"),
3047 DLT_CHOICE(LINUX_LAPD
, "Linux vISDN LAPD"),
3048 DLT_CHOICE(JUNIPER_ETHER
, "Juniper Ethernet"),
3049 DLT_CHOICE(JUNIPER_PPP
, "Juniper PPP"),
3050 DLT_CHOICE(JUNIPER_FRELAY
, "Juniper Frame Relay"),
3051 DLT_CHOICE(JUNIPER_CHDLC
, "Juniper C-HDLC"),
3052 DLT_CHOICE(MFR
, "FRF.16 Frame Relay"),
3053 DLT_CHOICE(JUNIPER_VP
, "Juniper Voice PIC"),
3054 DLT_CHOICE(A429
, "Arinc 429"),
3055 DLT_CHOICE(A653_ICM
, "Arinc 653 Interpartition Communication"),
3056 DLT_CHOICE(USB_FREEBSD
, "USB with FreeBSD header"),
3057 DLT_CHOICE(BLUETOOTH_HCI_H4
, "Bluetooth HCI UART transport layer"),
3058 DLT_CHOICE(IEEE802_16_MAC_CPS
, "IEEE 802.16 MAC Common Part Sublayer"),
3059 DLT_CHOICE(USB_LINUX
, "USB with Linux header"),
3060 DLT_CHOICE(CAN20B
, "Controller Area Network (CAN) v. 2.0B"),
3061 DLT_CHOICE(IEEE802_15_4_LINUX
, "IEEE 802.15.4 with Linux padding"),
3062 DLT_CHOICE(PPI
, "Per-Packet Information"),
3063 DLT_CHOICE(IEEE802_16_MAC_CPS_RADIO
, "IEEE 802.16 MAC Common Part Sublayer plus radiotap header"),
3064 DLT_CHOICE(JUNIPER_ISM
, "Juniper Integrated Service Module"),
3065 DLT_CHOICE(IEEE802_15_4
, "IEEE 802.15.4 with FCS"),
3066 DLT_CHOICE(SITA
, "SITA pseudo-header"),
3067 DLT_CHOICE(ERF
, "Endace ERF header"),
3068 DLT_CHOICE(RAIF1
, "Ethernet with u10 Networks pseudo-header"),
3069 DLT_CHOICE(IPMB_KONTRON
, "IPMB with Kontron pseudo-header"),
3070 DLT_CHOICE(JUNIPER_ST
, "Juniper Secure Tunnel"),
3071 DLT_CHOICE(BLUETOOTH_HCI_H4_WITH_PHDR
, "Bluetooth HCI UART transport layer plus pseudo-header"),
3072 DLT_CHOICE(AX25_KISS
, "AX.25 with KISS header"),
3073 DLT_CHOICE(IPMB_LINUX
, "IPMB with Linux/Pigeon Point pseudo-header"),
3074 DLT_CHOICE(IEEE802_15_4_NONASK_PHY
, "IEEE 802.15.4 with non-ASK PHY data"),
3075 DLT_CHOICE(MPLS
, "MPLS with label as link-layer header"),
3076 DLT_CHOICE(LINUX_EVDEV
, "Linux evdev events"),
3077 DLT_CHOICE(USB_LINUX_MMAPPED
, "USB with padded Linux header"),
3078 DLT_CHOICE(DECT
, "DECT"),
3079 DLT_CHOICE(AOS
, "AOS Space Data Link protocol"),
3080 DLT_CHOICE(WIHART
, "Wireless HART"),
3081 DLT_CHOICE(FC_2
, "Fibre Channel FC-2"),
3082 DLT_CHOICE(FC_2_WITH_FRAME_DELIMS
, "Fibre Channel FC-2 with frame delimiters"),
3083 DLT_CHOICE(IPNET
, "Solaris ipnet"),
3084 DLT_CHOICE(CAN_SOCKETCAN
, "CAN-bus with SocketCAN headers"),
3085 DLT_CHOICE(IPV4
, "Raw IPv4"),
3086 DLT_CHOICE(IPV6
, "Raw IPv6"),
3087 DLT_CHOICE(IEEE802_15_4_NOFCS
, "IEEE 802.15.4 without FCS"),
3088 DLT_CHOICE(DBUS
, "D-Bus"),
3089 DLT_CHOICE(JUNIPER_VS
, "Juniper Virtual Server"),
3090 DLT_CHOICE(JUNIPER_SRX_E2E
, "Juniper SRX E2E"),
3091 DLT_CHOICE(JUNIPER_FIBRECHANNEL
, "Juniper Fibre Channel"),
3092 DLT_CHOICE(DVB_CI
, "DVB-CI"),
3093 DLT_CHOICE(MUX27010
, "MUX27010"),
3094 DLT_CHOICE(STANAG_5066_D_PDU
, "STANAG 5066 D_PDUs"),
3095 DLT_CHOICE(JUNIPER_ATM_CEMIC
, "Juniper ATM CEMIC"),
3096 DLT_CHOICE(NFLOG
, "Linux netfilter log messages"),
3097 DLT_CHOICE(NETANALYZER
, "Ethernet with Hilscher netANALYZER pseudo-header"),
3098 DLT_CHOICE(NETANALYZER_TRANSPARENT
, "Ethernet with Hilscher netANALYZER pseudo-header and with preamble and SFD"),
3099 DLT_CHOICE(IPOIB
, "RFC 4391 IP-over-Infiniband"),
3100 DLT_CHOICE(MPEG_2_TS
, "MPEG-2 transport stream"),
3101 DLT_CHOICE(NG40
, "ng40 protocol tester Iub/Iur"),
3102 DLT_CHOICE(NFC_LLCP
, "NFC LLCP PDUs with pseudo-header"),
3103 DLT_CHOICE(INFINIBAND
, "InfiniBand"),
3104 DLT_CHOICE(SCTP
, "SCTP"),
3105 DLT_CHOICE(USBPCAP
, "USB with USBPcap header"),
3106 DLT_CHOICE(RTAC_SERIAL
, "Schweitzer Engineering Laboratories RTAC packets"),
3107 DLT_CHOICE(BLUETOOTH_LE_LL
, "Bluetooth Low Energy air interface"),
3108 DLT_CHOICE(NETLINK
, "Linux netlink"),
3109 DLT_CHOICE(BLUETOOTH_LINUX_MONITOR
, "Bluetooth Linux Monitor"),
3110 DLT_CHOICE(BLUETOOTH_BREDR_BB
, "Bluetooth Basic Rate/Enhanced Data Rate baseband packets"),
3111 DLT_CHOICE(BLUETOOTH_LE_LL_WITH_PHDR
, "Bluetooth Low Energy air interface with pseudo-header"),
3112 DLT_CHOICE(PROFIBUS_DL
, "PROFIBUS data link layer"),
3113 DLT_CHOICE(PKTAP
, "Apple DLT_PKTAP"),
3114 DLT_CHOICE(EPON
, "Ethernet with 802.3 Clause 65 EPON preamble"),
3115 DLT_CHOICE(IPMI_HPM_2
, "IPMI trace packets"),
3116 DLT_CHOICE(ZWAVE_R1_R2
, "Z-Wave RF profile R1 and R2 packets"),
3117 DLT_CHOICE(ZWAVE_R3
, "Z-Wave RF profile R3 packets"),
3118 DLT_CHOICE(WATTSTOPPER_DLM
, "WattStopper Digital Lighting Management (DLM) and Legrand Nitoo Open protocol"),
3119 DLT_CHOICE(ISO_14443
, "ISO 14443 messages"),
3120 DLT_CHOICE(RDS
, "IEC 62106 Radio Data System groups"),
3121 DLT_CHOICE(USB_DARWIN
, "USB with Darwin header"),
3122 DLT_CHOICE(OPENFLOW
, "OpenBSD DLT_OPENFLOW"),
3123 DLT_CHOICE(SDLC
, "IBM SDLC frames"),
3124 DLT_CHOICE(TI_LLN_SNIFFER
, "TI LLN sniffer frames"),
3125 DLT_CHOICE(VSOCK
, "Linux vsock"),
3126 DLT_CHOICE(NORDIC_BLE
, "Nordic Semiconductor Bluetooth LE sniffer frames"),
3127 DLT_CHOICE(DOCSIS31_XRA31
, "Excentis XRA-31 DOCSIS 3.1 RF sniffer frames"),
3128 DLT_CHOICE(ETHERNET_MPACKET
, "802.3br mPackets"),
3129 DLT_CHOICE(DISPLAYPORT_AUX
, "DisplayPort AUX channel monitoring data"),
3130 DLT_CHOICE(LINUX_SLL2
, "Linux cooked v2"),
3131 DLT_CHOICE(OPENVIZSLA
, "OpenVizsla USB"),
3132 DLT_CHOICE(EBHSCR
, "Elektrobit High Speed Capture and Replay (EBHSCR)"),
3133 DLT_CHOICE(VPP_DISPATCH
, "VPP graph dispatch tracer"),
3134 DLT_CHOICE(DSA_TAG_BRCM
, "Broadcom tag"),
3135 DLT_CHOICE(DSA_TAG_BRCM_PREPEND
, "Broadcom tag (prepended)"),
3136 DLT_CHOICE(IEEE802_15_4_TAP
, "IEEE 802.15.4 with pseudo-header"),
3137 DLT_CHOICE(DSA_TAG_DSA
, "Marvell DSA"),
3138 DLT_CHOICE(DSA_TAG_EDSA
, "Marvell EDSA"),
3139 DLT_CHOICE(ELEE
, "ELEE lawful intercept packets"),
3140 DLT_CHOICE(Z_WAVE_SERIAL
, "Z-Wave serial frames between host and chip"),
3141 DLT_CHOICE(USB_2_0
, "USB 2.0/1.1/1.0 as transmitted over the cable"),
3146 pcap_datalink_name_to_val(const char *name
)
3150 for (i
= 0; dlt_choices
[i
].name
!= NULL
; i
++) {
3151 if (pcap_strcasecmp(dlt_choices
[i
].name
, name
) == 0)
3152 return (dlt_choices
[i
].dlt
);
3158 pcap_datalink_val_to_name(int dlt
)
3162 for (i
= 0; dlt_choices
[i
].name
!= NULL
; i
++) {
3163 if (dlt_choices
[i
].dlt
== dlt
)
3164 return (dlt_choices
[i
].name
);
3170 pcap_datalink_val_to_description(int dlt
)
3174 for (i
= 0; dlt_choices
[i
].name
!= NULL
; i
++) {
3175 if (dlt_choices
[i
].dlt
== dlt
)
3176 return (dlt_choices
[i
].description
);
3182 pcap_datalink_val_to_description_or_dlt(int dlt
)
3184 static char unkbuf
[40];
3185 const char *description
;
3187 description
= pcap_datalink_val_to_description(dlt
);
3188 if (description
!= NULL
) {
3191 (void)snprintf(unkbuf
, sizeof(unkbuf
), "DLT %u", dlt
);
3196 struct tstamp_type_choice
{
3198 const char *description
;
3202 static struct tstamp_type_choice tstamp_type_choices
[] = {
3203 { "host", "Host", PCAP_TSTAMP_HOST
},
3204 { "host_lowprec", "Host, low precision", PCAP_TSTAMP_HOST_LOWPREC
},
3205 { "host_hiprec", "Host, high precision", PCAP_TSTAMP_HOST_HIPREC
},
3206 { "adapter", "Adapter", PCAP_TSTAMP_ADAPTER
},
3207 { "adapter_unsynced", "Adapter, not synced with system time", PCAP_TSTAMP_ADAPTER_UNSYNCED
},
3212 pcap_tstamp_type_name_to_val(const char *name
)
3216 for (i
= 0; tstamp_type_choices
[i
].name
!= NULL
; i
++) {
3217 if (pcap_strcasecmp(tstamp_type_choices
[i
].name
, name
) == 0)
3218 return (tstamp_type_choices
[i
].type
);
3220 return (PCAP_ERROR
);
3224 pcap_tstamp_type_val_to_name(int tstamp_type
)
3228 for (i
= 0; tstamp_type_choices
[i
].name
!= NULL
; i
++) {
3229 if (tstamp_type_choices
[i
].type
== tstamp_type
)
3230 return (tstamp_type_choices
[i
].name
);
3236 pcap_tstamp_type_val_to_description(int tstamp_type
)
3240 for (i
= 0; tstamp_type_choices
[i
].name
!= NULL
; i
++) {
3241 if (tstamp_type_choices
[i
].type
== tstamp_type
)
3242 return (tstamp_type_choices
[i
].description
);
3248 pcap_snapshot(pcap_t
*p
)
3251 return (PCAP_ERROR_NOT_ACTIVATED
);
3252 return (p
->snapshot
);
3256 pcap_is_swapped(pcap_t
*p
)
3259 return (PCAP_ERROR_NOT_ACTIVATED
);
3260 return (p
->swapped
);
3264 pcap_major_version(pcap_t
*p
)
3267 return (PCAP_ERROR_NOT_ACTIVATED
);
3268 return (p
->version_major
);
3272 pcap_minor_version(pcap_t
*p
)
3275 return (PCAP_ERROR_NOT_ACTIVATED
);
3276 return (p
->version_minor
);
3280 pcap_bufsize(pcap_t
*p
)
3283 return (PCAP_ERROR_NOT_ACTIVATED
);
3284 return (p
->bufsize
);
3288 pcap_file(pcap_t
*p
)
3294 pcap_fileno(pcap_t
*p
)
3299 if (p
->handle
!= INVALID_HANDLE_VALUE
)
3300 return ((int)(DWORD
)p
->handle
);
3302 return (PCAP_ERROR
);
3306 #if !defined(_WIN32) && !defined(MSDOS)
3308 pcap_get_selectable_fd(pcap_t
*p
)
3310 return (p
->selectable_fd
);
3314 pcap_get_required_select_timeout(pcap_t
*p
)
3316 return (p
->required_select_timeout
);
3321 pcap_perror(pcap_t
*p
, const char *prefix
)
3323 fprintf(stderr
, "%s: %s\n", prefix
, p
->errbuf
);
3327 pcap_geterr(pcap_t
*p
)
3333 pcap_getnonblock(pcap_t
*p
, char *errbuf
)
3337 ret
= p
->getnonblock_op(p
);
3340 * The get nonblock operation sets p->errbuf; this
3341 * function *shouldn't* have had a separate errbuf
3342 * argument, as it didn't need one, but I goofed
3345 * We copy the error message to errbuf, so callers
3346 * can find it in either place.
3348 pcap_strlcpy(errbuf
, p
->errbuf
, PCAP_ERRBUF_SIZE
);
3354 * Get the current non-blocking mode setting, under the assumption that
3355 * it's just the standard POSIX non-blocking flag.
3357 #if !defined(_WIN32) && !defined(MSDOS)
3359 pcap_getnonblock_fd(pcap_t
*p
)
3363 fdflags
= fcntl(p
->fd
, F_GETFL
, 0);
3364 if (fdflags
== -1) {
3365 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
3369 if (fdflags
& O_NONBLOCK
)
3377 pcap_setnonblock(pcap_t
*p
, int nonblock
, char *errbuf
)
3381 ret
= p
->setnonblock_op(p
, nonblock
);
3384 * The set nonblock operation sets p->errbuf; this
3385 * function *shouldn't* have had a separate errbuf
3386 * argument, as it didn't need one, but I goofed
3389 * We copy the error message to errbuf, so callers
3390 * can find it in either place.
3392 pcap_strlcpy(errbuf
, p
->errbuf
, PCAP_ERRBUF_SIZE
);
3397 #if !defined(_WIN32) && !defined(MSDOS)
3399 * Set non-blocking mode, under the assumption that it's just the
3400 * standard POSIX non-blocking flag. (This can be called by the
3401 * per-platform non-blocking-mode routine if that routine also
3402 * needs to do some additional work.)
3405 pcap_setnonblock_fd(pcap_t
*p
, int nonblock
)
3409 fdflags
= fcntl(p
->fd
, F_GETFL
, 0);
3410 if (fdflags
== -1) {
3411 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
3416 fdflags
|= O_NONBLOCK
;
3418 fdflags
&= ~O_NONBLOCK
;
3419 if (fcntl(p
->fd
, F_SETFL
, fdflags
) == -1) {
3420 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
3429 * Generate error strings for PCAP_ERROR_ and PCAP_WARNING_ values.
3432 pcap_statustostr(int errnum
)
3434 static char ebuf
[15+10+1];
3439 return("Generic warning");
3441 case PCAP_WARNING_TSTAMP_TYPE_NOTSUP
:
3442 return ("That type of time stamp is not supported by that device");
3444 case PCAP_WARNING_PROMISC_NOTSUP
:
3445 return ("That device doesn't support promiscuous mode");
3448 return("Generic error");
3450 case PCAP_ERROR_BREAK
:
3451 return("Loop terminated by pcap_breakloop");
3453 case PCAP_ERROR_NOT_ACTIVATED
:
3454 return("The pcap_t has not been activated");
3456 case PCAP_ERROR_ACTIVATED
:
3457 return ("The setting can't be changed after the pcap_t is activated");
3459 case PCAP_ERROR_NO_SUCH_DEVICE
:
3460 return ("No such device exists");
3462 case PCAP_ERROR_RFMON_NOTSUP
:
3463 return ("That device doesn't support monitor mode");
3465 case PCAP_ERROR_NOT_RFMON
:
3466 return ("That operation is supported only in monitor mode");
3468 case PCAP_ERROR_PERM_DENIED
:
3469 return ("You don't have permission to capture on that device");
3471 case PCAP_ERROR_IFACE_NOT_UP
:
3472 return ("That device is not up");
3474 case PCAP_ERROR_CANTSET_TSTAMP_TYPE
:
3475 return ("That device doesn't support setting the time stamp type");
3477 case PCAP_ERROR_PROMISC_PERM_DENIED
:
3478 return ("You don't have permission to capture in promiscuous mode on that device");
3480 case PCAP_ERROR_TSTAMP_PRECISION_NOTSUP
:
3481 return ("That device doesn't support that time stamp precision");
3483 (void)snprintf(ebuf
, sizeof ebuf
, "Unknown error: %d", errnum
);
3488 * Not all systems have strerror().
3491 pcap_strerror(int errnum
)
3493 #ifdef HAVE_STRERROR
3495 static char errbuf
[PCAP_ERRBUF_SIZE
];
3496 errno_t err
= strerror_s(errbuf
, PCAP_ERRBUF_SIZE
, errnum
);
3498 if (err
!= 0) /* err = 0 if successful */
3499 pcap_strlcpy(errbuf
, "strerror_s() error", PCAP_ERRBUF_SIZE
);
3502 return (strerror(errnum
));
3505 extern int sys_nerr
;
3506 extern const char *const sys_errlist
[];
3507 static char errbuf
[PCAP_ERRBUF_SIZE
];
3509 if ((unsigned int)errnum
< sys_nerr
)
3510 return ((char *)sys_errlist
[errnum
]);
3511 (void)snprintf(errbuf
, sizeof errbuf
, "Unknown error: %d", errnum
);
3517 pcap_setfilter(pcap_t
*p
, struct bpf_program
*fp
)
3519 return (p
->setfilter_op(p
, fp
));
3523 * Set direction flag, which controls whether we accept only incoming
3524 * packets, only outgoing packets, or both.
3525 * Note that, depending on the platform, some or all direction arguments
3526 * might not be supported.
3529 pcap_setdirection(pcap_t
*p
, pcap_direction_t d
)
3531 if (p
->setdirection_op
== NULL
) {
3532 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
3533 "Setting direction is not implemented on this platform");
3536 return (p
->setdirection_op(p
, d
));
3540 pcap_stats(pcap_t
*p
, struct pcap_stat
*ps
)
3542 return (p
->stats_op(p
, ps
));
3547 pcap_stats_ex(pcap_t
*p
, int *pcap_stat_size
)
3549 return (p
->stats_ex_op(p
, pcap_stat_size
));
3553 pcap_setbuff(pcap_t
*p
, int dim
)
3555 return (p
->setbuff_op(p
, dim
));
3559 pcap_setmode(pcap_t
*p
, int mode
)
3561 return (p
->setmode_op(p
, mode
));
3565 pcap_setmintocopy(pcap_t
*p
, int size
)
3567 return (p
->setmintocopy_op(p
, size
));
3571 pcap_getevent(pcap_t
*p
)
3573 return (p
->getevent_op(p
));
3577 pcap_oid_get_request(pcap_t
*p
, bpf_u_int32 oid
, void *data
, size_t *lenp
)
3579 return (p
->oid_get_request_op(p
, oid
, data
, lenp
));
3583 pcap_oid_set_request(pcap_t
*p
, bpf_u_int32 oid
, const void *data
, size_t *lenp
)
3585 return (p
->oid_set_request_op(p
, oid
, data
, lenp
));
3589 pcap_sendqueue_alloc(u_int memsize
)
3591 pcap_send_queue
*tqueue
;
3593 /* Allocate the queue */
3594 tqueue
= (pcap_send_queue
*)malloc(sizeof(pcap_send_queue
));
3595 if (tqueue
== NULL
){
3599 /* Allocate the buffer */
3600 tqueue
->buffer
= (char *)malloc(memsize
);
3601 if (tqueue
->buffer
== NULL
) {
3606 tqueue
->maxlen
= memsize
;
3613 pcap_sendqueue_destroy(pcap_send_queue
*queue
)
3615 free(queue
->buffer
);
3620 pcap_sendqueue_queue(pcap_send_queue
*queue
, const struct pcap_pkthdr
*pkt_header
, const u_char
*pkt_data
)
3622 if (queue
->len
+ sizeof(struct pcap_pkthdr
) + pkt_header
->caplen
> queue
->maxlen
){
3626 /* Copy the pcap_pkthdr header*/
3627 memcpy(queue
->buffer
+ queue
->len
, pkt_header
, sizeof(struct pcap_pkthdr
));
3628 queue
->len
+= sizeof(struct pcap_pkthdr
);
3630 /* copy the packet */
3631 memcpy(queue
->buffer
+ queue
->len
, pkt_data
, pkt_header
->caplen
);
3632 queue
->len
+= pkt_header
->caplen
;
3638 pcap_sendqueue_transmit(pcap_t
*p
, pcap_send_queue
*queue
, int sync
)
3640 return (p
->sendqueue_transmit_op(p
, queue
, sync
));
3644 pcap_setuserbuffer(pcap_t
*p
, int size
)
3646 return (p
->setuserbuffer_op(p
, size
));
3650 pcap_live_dump(pcap_t
*p
, char *filename
, int maxsize
, int maxpacks
)
3652 return (p
->live_dump_op(p
, filename
, maxsize
, maxpacks
));
3656 pcap_live_dump_ended(pcap_t
*p
, int sync
)
3658 return (p
->live_dump_ended_op(p
, sync
));
3662 pcap_get_airpcap_handle(pcap_t
*p
)
3664 PAirpcapHandle handle
;
3666 handle
= p
->get_airpcap_handle_op(p
);
3667 if (handle
== NULL
) {
3668 (void)snprintf(p
->errbuf
, sizeof(p
->errbuf
),
3669 "This isn't an AirPcap device");
3676 * On some platforms, we need to clean up promiscuous or monitor mode
3677 * when we close a device - and we want that to happen even if the
3678 * application just exits without explicitl closing devices.
3679 * On those platforms, we need to register a "close all the pcaps"
3680 * routine to be called when we exit, and need to maintain a list of
3681 * pcaps that need to be closed to clean up modes.
3683 * XXX - not thread-safe.
3687 * List of pcaps on which we've done something that needs to be
3689 * If there are any such pcaps, we arrange to call "pcap_close_all()"
3690 * when we exit, and have it close all of them.
3692 static struct pcap
*pcaps_to_close
;
3695 * TRUE if we've already called "atexit()" to cause "pcap_close_all()" to
3696 * be called on exit.
3698 static int did_atexit
;
3701 pcap_close_all(void)
3703 struct pcap
*handle
;
3705 while ((handle
= pcaps_to_close
) != NULL
) {
3709 * If a pcap module adds a pcap_t to the "close all"
3710 * list by calling pcap_add_to_pcaps_to_close(), it
3711 * must have a cleanup routine that removes it from the
3712 * list, by calling pcap_remove_from_pcaps_to_close(),
3713 * and must make that cleanup routine the cleanup_op
3716 * That means that, after pcap_close() - which calls
3717 * the cleanup_op for the pcap_t - the pcap_t must
3718 * have been removed from the list, so pcaps_to_close
3719 * must not be equal to handle.
3721 * We check for that, and abort if handle is still
3722 * at the head of the list, to prevent infinite loops.
3724 if (pcaps_to_close
== handle
)
3730 pcap_do_addexit(pcap_t
*p
)
3733 * If we haven't already done so, arrange to have
3734 * "pcap_close_all()" called when we exit.
3737 if (atexit(pcap_close_all
) != 0) {
3739 * "atexit()" failed; let our caller know.
3741 pcap_strlcpy(p
->errbuf
, "atexit failed", PCAP_ERRBUF_SIZE
);
3750 pcap_add_to_pcaps_to_close(pcap_t
*p
)
3752 p
->next
= pcaps_to_close
;
3757 pcap_remove_from_pcaps_to_close(pcap_t
*p
)
3759 pcap_t
*pc
, *prevpc
;
3761 for (pc
= pcaps_to_close
, prevpc
= NULL
; pc
!= NULL
;
3762 prevpc
= pc
, pc
= pc
->next
) {
3765 * Found it. Remove it from the list.
3767 if (prevpc
== NULL
) {
3769 * It was at the head of the list.
3771 pcaps_to_close
= pc
->next
;
3774 * It was in the middle of the list.
3776 prevpc
->next
= pc
->next
;
3784 pcap_breakloop_common(pcap_t
*p
)
3791 pcap_cleanup_live_common(pcap_t
*p
)
3793 if (p
->buffer
!= NULL
) {
3797 if (p
->dlt_list
!= NULL
) {
3802 if (p
->tstamp_type_list
!= NULL
) {
3803 free(p
->tstamp_type_list
);
3804 p
->tstamp_type_list
= NULL
;
3805 p
->tstamp_type_count
= 0;
3807 if (p
->tstamp_precision_list
!= NULL
) {
3808 free(p
->tstamp_precision_list
);
3809 p
->tstamp_precision_list
= NULL
;
3810 p
->tstamp_precision_count
= 0;
3812 pcap_freecode(&p
->fcode
);
3813 #if !defined(_WIN32) && !defined(MSDOS)
3818 p
->selectable_fd
= -1;
3823 * API compatible with WinPcap's "send a packet" routine - returns -1
3824 * on error, 0 otherwise.
3826 * XXX - what if we get a short write?
3829 pcap_sendpacket(pcap_t
*p
, const u_char
*buf
, int size
)
3832 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
3833 errno
, "The number of bytes to be sent must be positive");
3834 return (PCAP_ERROR
);
3837 if (p
->inject_op(p
, buf
, size
) == -1)
3843 * API compatible with OpenBSD's "send a packet" routine - returns -1 on
3844 * error, number of bytes written otherwise.
3847 pcap_inject(pcap_t
*p
, const void *buf
, size_t size
)
3850 * We return the number of bytes written, so the number of
3851 * bytes to write must fit in an int.
3853 if (size
> INT_MAX
) {
3854 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
3855 errno
, "More than %d bytes cannot be injected", INT_MAX
);
3856 return (PCAP_ERROR
);
3860 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
3861 errno
, "The number of bytes to be injected must not be zero");
3862 return (PCAP_ERROR
);
3865 return (p
->inject_op(p
, buf
, (int)size
));
3869 pcap_close(pcap_t
*p
)
3871 if (p
->opt
.device
!= NULL
)
3872 free(p
->opt
.device
);
3878 * Given a BPF program, a pcap_pkthdr structure for a packet, and the raw
3879 * data for the packet, check whether the packet passes the filter.
3880 * Returns the return value of the filter program, which will be zero if
3881 * the packet doesn't pass and non-zero if the packet does pass.
3884 pcap_offline_filter(const struct bpf_program
*fp
, const struct pcap_pkthdr
*h
,
3887 const struct bpf_insn
*fcode
= fp
->bf_insns
;
3890 return (pcap_filter(fcode
, pkt
, h
->len
, h
->caplen
));
3896 pcap_can_set_rfmon_dead(pcap_t
*p
)
3898 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
3899 "Rfmon mode doesn't apply on a pcap_open_dead pcap_t");
3900 return (PCAP_ERROR
);
3904 pcap_read_dead(pcap_t
*p
, int cnt _U_
, pcap_handler callback _U_
,
3907 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
3908 "Packets aren't available from a pcap_open_dead pcap_t");
3913 pcap_inject_dead(pcap_t
*p
, const void *buf _U_
, int size _U_
)
3915 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
3916 "Packets can't be sent on a pcap_open_dead pcap_t");
3921 pcap_setfilter_dead(pcap_t
*p
, struct bpf_program
*fp _U_
)
3923 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
3924 "A filter cannot be set on a pcap_open_dead pcap_t");
3929 pcap_setdirection_dead(pcap_t
*p
, pcap_direction_t d _U_
)
3931 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
3932 "The packet direction cannot be set on a pcap_open_dead pcap_t");
3937 pcap_set_datalink_dead(pcap_t
*p
, int dlt _U_
)
3939 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
3940 "The link-layer header type cannot be set on a pcap_open_dead pcap_t");
3945 pcap_getnonblock_dead(pcap_t
*p
)
3947 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
3948 "A pcap_open_dead pcap_t does not have a non-blocking mode setting");
3953 pcap_setnonblock_dead(pcap_t
*p
, int nonblock _U_
)
3955 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
3956 "A pcap_open_dead pcap_t does not have a non-blocking mode setting");
3961 pcap_stats_dead(pcap_t
*p
, struct pcap_stat
*ps _U_
)
3963 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
3964 "Statistics aren't available from a pcap_open_dead pcap_t");
3970 pcap_stats_ex_dead(pcap_t
*p
, int *pcap_stat_size _U_
)
3972 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
3973 "Statistics aren't available from a pcap_open_dead pcap_t");
3978 pcap_setbuff_dead(pcap_t
*p
, int dim
)
3980 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
3981 "The kernel buffer size cannot be set on a pcap_open_dead pcap_t");
3986 pcap_setmode_dead(pcap_t
*p
, int mode
)
3988 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
3989 "impossible to set mode on a pcap_open_dead pcap_t");
3994 pcap_setmintocopy_dead(pcap_t
*p
, int size
)
3996 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
3997 "The mintocopy parameter cannot be set on a pcap_open_dead pcap_t");
4002 pcap_getevent_dead(pcap_t
*p
)
4004 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
4005 "A pcap_open_dead pcap_t has no event handle");
4006 return (INVALID_HANDLE_VALUE
);
4010 pcap_oid_get_request_dead(pcap_t
*p
, bpf_u_int32 oid _U_
, void *data _U_
,
4013 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
4014 "An OID get request cannot be performed on a pcap_open_dead pcap_t");
4015 return (PCAP_ERROR
);
4019 pcap_oid_set_request_dead(pcap_t
*p
, bpf_u_int32 oid _U_
, const void *data _U_
,
4022 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
4023 "An OID set request cannot be performed on a pcap_open_dead pcap_t");
4024 return (PCAP_ERROR
);
4028 pcap_sendqueue_transmit_dead(pcap_t
*p
, pcap_send_queue
*queue
, int sync
)
4030 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
4031 "Packets cannot be transmitted on a pcap_open_dead pcap_t");
4036 pcap_setuserbuffer_dead(pcap_t
*p
, int size
)
4038 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
4039 "The user buffer cannot be set on a pcap_open_dead pcap_t");
4044 pcap_live_dump_dead(pcap_t
*p
, char *filename
, int maxsize
, int maxpacks
)
4046 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
4047 "Live packet dumping cannot be performed on a pcap_open_dead pcap_t");
4052 pcap_live_dump_ended_dead(pcap_t
*p
, int sync
)
4054 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
4055 "Live packet dumping cannot be performed on a pcap_open_dead pcap_t");
4059 static PAirpcapHandle
4060 pcap_get_airpcap_handle_dead(pcap_t
*p
)
4067 pcap_cleanup_dead(pcap_t
*p _U_
)
4069 /* Nothing to do. */
4073 pcap_open_dead_with_tstamp_precision(int linktype
, int snaplen
, u_int precision
)
4077 switch (precision
) {
4079 case PCAP_TSTAMP_PRECISION_MICRO
:
4080 case PCAP_TSTAMP_PRECISION_NANO
:
4085 * This doesn't really matter, but we don't have any way
4086 * to report particular errors, so the only failure we
4087 * should have is a memory allocation failure. Just
4088 * pick microsecond precision.
4090 precision
= PCAP_TSTAMP_PRECISION_MICRO
;
4093 p
= malloc(sizeof(*p
));
4096 memset (p
, 0, sizeof(*p
));
4097 p
->snapshot
= snaplen
;
4098 p
->linktype
= linktype
;
4099 p
->opt
.tstamp_precision
= precision
;
4100 p
->can_set_rfmon_op
= pcap_can_set_rfmon_dead
;
4101 p
->read_op
= pcap_read_dead
;
4102 p
->inject_op
= pcap_inject_dead
;
4103 p
->setfilter_op
= pcap_setfilter_dead
;
4104 p
->setdirection_op
= pcap_setdirection_dead
;
4105 p
->set_datalink_op
= pcap_set_datalink_dead
;
4106 p
->getnonblock_op
= pcap_getnonblock_dead
;
4107 p
->setnonblock_op
= pcap_setnonblock_dead
;
4108 p
->stats_op
= pcap_stats_dead
;
4110 p
->stats_ex_op
= pcap_stats_ex_dead
;
4111 p
->setbuff_op
= pcap_setbuff_dead
;
4112 p
->setmode_op
= pcap_setmode_dead
;
4113 p
->setmintocopy_op
= pcap_setmintocopy_dead
;
4114 p
->getevent_op
= pcap_getevent_dead
;
4115 p
->oid_get_request_op
= pcap_oid_get_request_dead
;
4116 p
->oid_set_request_op
= pcap_oid_set_request_dead
;
4117 p
->sendqueue_transmit_op
= pcap_sendqueue_transmit_dead
;
4118 p
->setuserbuffer_op
= pcap_setuserbuffer_dead
;
4119 p
->live_dump_op
= pcap_live_dump_dead
;
4120 p
->live_dump_ended_op
= pcap_live_dump_ended_dead
;
4121 p
->get_airpcap_handle_op
= pcap_get_airpcap_handle_dead
;
4123 p
->cleanup_op
= pcap_cleanup_dead
;
4126 * A "dead" pcap_t never requires special BPF code generation.
4128 p
->bpf_codegen_flags
= 0;
4135 pcap_open_dead(int linktype
, int snaplen
)
4137 return (pcap_open_dead_with_tstamp_precision(linktype
, snaplen
,
4138 PCAP_TSTAMP_PRECISION_MICRO
));
4143 * Set the internal "debug printout" flag for the filter expression parser.
4144 * The code to print that stuff is present only if YYDEBUG is defined, so
4145 * the flag, and the routine to set it, are defined only if YYDEBUG is
4148 * This is intended for libpcap developers, not for general use.
4149 * If you want to set these in a program, you'll have to declare this
4150 * routine yourself, with the appropriate DLL import attribute on Windows;
4151 * it's not declared in any header file, and won't be declared in any
4152 * header file provided by libpcap.
4154 PCAP_API
void pcap_set_parser_debug(int value
);
4157 pcap_set_parser_debug(int value
)