]> The Tcpdump Group git mirrors - libpcap/blob - pcap.c
update configure and cmake
[libpcap] / pcap.c
1 /*
2 * Copyright (c) 1993, 1994, 1995, 1996, 1997, 1998
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
20 *
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
31 * SUCH DAMAGE.
32 */
33
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37
38 #include <pcap-types.h>
39 #ifndef _WIN32
40 #include <sys/param.h>
41 #ifndef MSDOS
42 #include <sys/file.h>
43 #endif
44 #include <sys/ioctl.h>
45 #include <sys/socket.h>
46 #ifdef HAVE_SYS_SOCKIO_H
47 #include <sys/sockio.h>
48 #endif
49
50 struct mbuf; /* Squelch compiler warnings on some platforms for */
51 struct rtentry; /* declarations in <net/if.h> */
52 #include <net/if.h>
53 #include <netinet/in.h>
54 #endif /* _WIN32 */
55
56 #include <ctype.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #if !defined(_MSC_VER) && !defined(__BORLANDC__) && !defined(__MINGW32__)
61 #include <unistd.h>
62 #endif
63 #include <fcntl.h>
64 #include <errno.h>
65 #include <limits.h>
66
67 #ifdef HAVE_OS_PROTO_H
68 #include "os-proto.h"
69 #endif
70
71 #ifdef MSDOS
72 #include "pcap-dos.h"
73 #endif
74
75 #include "pcap-int.h"
76
77 #include "optimize.h"
78
79 #ifdef HAVE_DAG_API
80 #include "pcap-dag.h"
81 #endif /* HAVE_DAG_API */
82
83 #ifdef HAVE_SEPTEL_API
84 #include "pcap-septel.h"
85 #endif /* HAVE_SEPTEL_API */
86
87 #ifdef HAVE_SNF_API
88 #include "pcap-snf.h"
89 #endif /* HAVE_SNF_API */
90
91 #ifdef HAVE_TC_API
92 #include "pcap-tc.h"
93 #endif /* HAVE_TC_API */
94
95 #ifdef PCAP_SUPPORT_USB
96 #include "pcap-usb-linux.h"
97 #endif
98
99 #ifdef PCAP_SUPPORT_BT
100 #include "pcap-bt-linux.h"
101 #endif
102
103 #ifdef PCAP_SUPPORT_BT_MONITOR
104 #include "pcap-bt-monitor-linux.h"
105 #endif
106
107 #ifdef PCAP_SUPPORT_NETFILTER
108 #include "pcap-netfilter-linux.h"
109 #endif
110
111 #ifdef PCAP_SUPPORT_NETMAP
112 #include "pcap-netmap.h"
113 #endif
114
115 #ifdef PCAP_SUPPORT_DBUS
116 #include "pcap-dbus.h"
117 #endif
118
119 #ifdef PCAP_SUPPORT_RDMASNIFF
120 #include "pcap-rdmasniff.h"
121 #endif
122
123 #ifdef PCAP_SUPPORT_DPDK
124 #include "pcap-dpdk.h"
125 #endif
126
127 #ifdef _WIN32
128 /*
129 * DllMain(), required when built as a Windows DLL.
130 */
131 BOOL WINAPI DllMain(
132 HANDLE hinstDLL,
133 DWORD dwReason,
134 LPVOID lpvReserved
135 )
136 {
137 return (TRUE);
138 }
139
140 /*
141 * Start WinSock.
142 * Exported in case some applications using WinPcap called it,
143 * even though it wasn't exported.
144 */
145 int
146 wsockinit(void)
147 {
148 WORD wVersionRequested;
149 WSADATA wsaData;
150 static int err = -1;
151 static int done = 0;
152
153 if (done)
154 return (err);
155
156 wVersionRequested = MAKEWORD( 1, 1);
157 err = WSAStartup( wVersionRequested, &wsaData );
158 atexit ((void(*)(void))WSACleanup);
159 done = 1;
160
161 if ( err != 0 )
162 err = -1;
163 return (err);
164 }
165
166 /*
167 * This is the exported function; new programs should call this.
168 */
169 int
170 pcap_wsockinit(void)
171 {
172 return (wsockinit());
173 }
174 #endif /* _WIN32 */
175
176 /*
177 * String containing the library version.
178 * Not explicitly exported via a header file - the right API to use
179 * is pcap_lib_version() - but some programs included it, so we
180 * provide it.
181 *
182 * We declare it here, right before defining it, to squelch any
183 * warnings we might get from compilers about the lack of a
184 * declaration.
185 */
186 PCAP_API char pcap_version[];
187 PCAP_API_DEF char pcap_version[] = PACKAGE_VERSION;
188
189 static int
190 pcap_not_initialized(pcap_t *pcap)
191 {
192 if (pcap->activated) {
193 /* A module probably forgot to set the function pointer */
194 (void)pcap_snprintf(pcap->errbuf, sizeof(pcap->errbuf),
195 "This operation isn't properly handled by that device");
196 return (PCAP_ERROR);
197 }
198 /* in case the caller doesn't check for PCAP_ERROR_NOT_ACTIVATED */
199 (void)pcap_snprintf(pcap->errbuf, sizeof(pcap->errbuf),
200 "This handle hasn't been activated yet");
201 /* this means 'not initialized' */
202 return (PCAP_ERROR_NOT_ACTIVATED);
203 }
204
205 #ifdef _WIN32
206 static void *
207 pcap_not_initialized_ptr(pcap_t *pcap)
208 {
209 if (pcap->activated) {
210 /* A module probably forgot to set the function pointer */
211 (void)pcap_snprintf(pcap->errbuf, sizeof(pcap->errbuf),
212 "This operation isn't properly handled by that device");
213 return (NULL);
214 }
215 (void)pcap_snprintf(pcap->errbuf, sizeof(pcap->errbuf),
216 "This handle hasn't been activated yet");
217 return (NULL);
218 }
219
220 static HANDLE
221 pcap_getevent_not_initialized(pcap_t *pcap)
222 {
223 if (pcap->activated) {
224 /* A module probably forgot to set the function pointer */
225 (void)pcap_snprintf(pcap->errbuf, sizeof(pcap->errbuf),
226 "This operation isn't properly handled by that device");
227 return (INVALID_HANDLE_VALUE);
228 }
229 (void)pcap_snprintf(pcap->errbuf, sizeof(pcap->errbuf),
230 "This handle hasn't been activated yet");
231 return (INVALID_HANDLE_VALUE);
232 }
233
234 static u_int
235 pcap_sendqueue_transmit_not_initialized(pcap_t *pcap, pcap_send_queue* queue, int sync)
236 {
237 if (pcap->activated) {
238 /* A module probably forgot to set the function pointer */
239 (void)pcap_snprintf(pcap->errbuf, sizeof(pcap->errbuf),
240 "This operation isn't properly handled by that device");
241 return (0);
242 }
243 (void)pcap_snprintf(pcap->errbuf, sizeof(pcap->errbuf),
244 "This handle hasn't been activated yet");
245 return (0);
246 }
247
248 static PAirpcapHandle
249 pcap_get_airpcap_handle_not_initialized(pcap_t *pcap)
250 {
251 if (pcap->activated) {
252 /* A module probably forgot to set the function pointer */
253 (void)pcap_snprintf(pcap->errbuf, sizeof(pcap->errbuf),
254 "This operation isn't properly handled by that device");
255 return (NULL);
256 }
257 (void)pcap_snprintf(pcap->errbuf, sizeof(pcap->errbuf),
258 "This handle hasn't been activated yet");
259 return (NULL);
260 }
261 #endif
262
263 /*
264 * Returns 1 if rfmon mode can be set on the pcap_t, 0 if it can't,
265 * a PCAP_ERROR value on an error.
266 */
267 int
268 pcap_can_set_rfmon(pcap_t *p)
269 {
270 return (p->can_set_rfmon_op(p));
271 }
272
273 /*
274 * For systems where rfmon mode is never supported.
275 */
276 static int
277 pcap_cant_set_rfmon(pcap_t *p _U_)
278 {
279 return (0);
280 }
281
282 /*
283 * Sets *tstamp_typesp to point to an array 1 or more supported time stamp
284 * types; the return value is the number of supported time stamp types.
285 * The list should be freed by a call to pcap_free_tstamp_types() when
286 * you're done with it.
287 *
288 * A return value of 0 means "you don't get a choice of time stamp type",
289 * in which case *tstamp_typesp is set to null.
290 *
291 * PCAP_ERROR is returned on error.
292 */
293 int
294 pcap_list_tstamp_types(pcap_t *p, int **tstamp_typesp)
295 {
296 if (p->tstamp_type_count == 0) {
297 /*
298 * We don't support multiple time stamp types.
299 */
300 *tstamp_typesp = NULL;
301 } else {
302 *tstamp_typesp = (int*)calloc(sizeof(**tstamp_typesp),
303 p->tstamp_type_count);
304 if (*tstamp_typesp == NULL) {
305 pcap_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf),
306 errno, "malloc");
307 return (PCAP_ERROR);
308 }
309 (void)memcpy(*tstamp_typesp, p->tstamp_type_list,
310 sizeof(**tstamp_typesp) * p->tstamp_type_count);
311 }
312 return (p->tstamp_type_count);
313 }
314
315 /*
316 * In Windows, you might have a library built with one version of the
317 * C runtime library and an application built with another version of
318 * the C runtime library, which means that the library might use one
319 * version of malloc() and free() and the application might use another
320 * version of malloc() and free(). If so, that means something
321 * allocated by the library cannot be freed by the application, so we
322 * need to have a pcap_free_tstamp_types() routine to free up the list
323 * allocated by pcap_list_tstamp_types(), even though it's just a wrapper
324 * around free().
325 */
326 void
327 pcap_free_tstamp_types(int *tstamp_type_list)
328 {
329 free(tstamp_type_list);
330 }
331
332 /*
333 * Default one-shot callback; overridden for capture types where the
334 * packet data cannot be guaranteed to be available after the callback
335 * returns, so that a copy must be made.
336 */
337 void
338 pcap_oneshot(u_char *user, const struct pcap_pkthdr *h, const u_char *pkt)
339 {
340 struct oneshot_userdata *sp = (struct oneshot_userdata *)user;
341
342 *sp->hdr = *h;
343 *sp->pkt = pkt;
344 }
345
346 const u_char *
347 pcap_next(pcap_t *p, struct pcap_pkthdr *h)
348 {
349 struct oneshot_userdata s;
350 const u_char *pkt;
351
352 s.hdr = h;
353 s.pkt = &pkt;
354 s.pd = p;
355 if (pcap_dispatch(p, 1, p->oneshot_callback, (u_char *)&s) <= 0)
356 return (0);
357 return (pkt);
358 }
359
360 int
361 pcap_next_ex(pcap_t *p, struct pcap_pkthdr **pkt_header,
362 const u_char **pkt_data)
363 {
364 struct oneshot_userdata s;
365
366 s.hdr = &p->pcap_header;
367 s.pkt = pkt_data;
368 s.pd = p;
369
370 /* Saves a pointer to the packet headers */
371 *pkt_header= &p->pcap_header;
372
373 if (p->rfile != NULL) {
374 int status;
375
376 /* We are on an offline capture */
377 status = pcap_offline_read(p, 1, p->oneshot_callback,
378 (u_char *)&s);
379
380 /*
381 * Return codes for pcap_offline_read() are:
382 * - 0: EOF
383 * - -1: error
384 * - >1: OK
385 * The first one ('0') conflicts with the return code of
386 * 0 from pcap_read() meaning "no packets arrived before
387 * the timeout expired", so we map it to -2 so you can
388 * distinguish between an EOF from a savefile and a
389 * "no packets arrived before the timeout expired, try
390 * again" from a live capture.
391 */
392 if (status == 0)
393 return (-2);
394 else
395 return (status);
396 }
397
398 /*
399 * Return codes for pcap_read() are:
400 * - 0: timeout
401 * - -1: error
402 * - -2: loop was broken out of with pcap_breakloop()
403 * - >1: OK
404 * The first one ('0') conflicts with the return code of 0 from
405 * pcap_offline_read() meaning "end of file".
406 */
407 return (p->read_op(p, 1, p->oneshot_callback, (u_char *)&s));
408 }
409
410 /*
411 * Implementation of a pcap_if_list_t.
412 */
413 struct pcap_if_list {
414 pcap_if_t *beginning;
415 };
416
417 static struct capture_source_type {
418 int (*findalldevs_op)(pcap_if_list_t *, char *);
419 pcap_t *(*create_op)(const char *, char *, int *);
420 } capture_source_types[] = {
421 #ifdef HAVE_DAG_API
422 { dag_findalldevs, dag_create },
423 #endif
424 #ifdef HAVE_SEPTEL_API
425 { septel_findalldevs, septel_create },
426 #endif
427 #ifdef HAVE_SNF_API
428 { snf_findalldevs, snf_create },
429 #endif
430 #ifdef HAVE_TC_API
431 { TcFindAllDevs, TcCreate },
432 #endif
433 #ifdef PCAP_SUPPORT_BT
434 { bt_findalldevs, bt_create },
435 #endif
436 #ifdef PCAP_SUPPORT_BT_MONITOR
437 { bt_monitor_findalldevs, bt_monitor_create },
438 #endif
439 #ifdef PCAP_SUPPORT_USB
440 { usb_findalldevs, usb_create },
441 #endif
442 #ifdef PCAP_SUPPORT_NETFILTER
443 { netfilter_findalldevs, netfilter_create },
444 #endif
445 #ifdef PCAP_SUPPORT_NETMAP
446 { pcap_netmap_findalldevs, pcap_netmap_create },
447 #endif
448 #ifdef PCAP_SUPPORT_DBUS
449 { dbus_findalldevs, dbus_create },
450 #endif
451 #ifdef PCAP_SUPPORT_RDMASNIFF
452 { rdmasniff_findalldevs, rdmasniff_create },
453 #endif
454 #ifdef PCAP_SUPPORT_DPDK
455 { pcap_dpdk_findalldevs, pcap_dpdk_create },
456 #endif
457 { NULL, NULL }
458 };
459
460 /*
461 * Get a list of all capture sources that are up and that we can open.
462 * Returns -1 on error, 0 otherwise.
463 * The list, as returned through "alldevsp", may be null if no interfaces
464 * were up and could be opened.
465 */
466 int
467 pcap_findalldevs(pcap_if_t **alldevsp, char *errbuf)
468 {
469 size_t i;
470 pcap_if_list_t devlist;
471
472 /*
473 * Find all the local network interfaces on which we
474 * can capture.
475 */
476 devlist.beginning = NULL;
477 if (pcap_platform_finddevs(&devlist, errbuf) == -1) {
478 /*
479 * Failed - free all of the entries we were given
480 * before we failed.
481 */
482 if (devlist.beginning != NULL)
483 pcap_freealldevs(devlist.beginning);
484 *alldevsp = NULL;
485 return (-1);
486 }
487
488 /*
489 * Ask each of the non-local-network-interface capture
490 * source types what interfaces they have.
491 */
492 for (i = 0; capture_source_types[i].findalldevs_op != NULL; i++) {
493 if (capture_source_types[i].findalldevs_op(&devlist, errbuf) == -1) {
494 /*
495 * We had an error; free the list we've been
496 * constructing.
497 */
498 if (devlist.beginning != NULL)
499 pcap_freealldevs(devlist.beginning);
500 *alldevsp = NULL;
501 return (-1);
502 }
503 }
504
505 /*
506 * Return the first entry of the list of all devices.
507 */
508 *alldevsp = devlist.beginning;
509 return (0);
510 }
511
512 static struct sockaddr *
513 dup_sockaddr(struct sockaddr *sa, size_t sa_length)
514 {
515 struct sockaddr *newsa;
516
517 if ((newsa = malloc(sa_length)) == NULL)
518 return (NULL);
519 return (memcpy(newsa, sa, sa_length));
520 }
521
522 /*
523 * Construct a "figure of merit" for an interface, for use when sorting
524 * the list of interfaces, in which interfaces that are up are superior
525 * to interfaces that aren't up, interfaces that are up and running are
526 * superior to interfaces that are up but not running, and non-loopback
527 * interfaces that are up and running are superior to loopback interfaces,
528 * and interfaces with the same flags have a figure of merit that's higher
529 * the lower the instance number.
530 *
531 * The goal is to try to put the interfaces most likely to be useful for
532 * capture at the beginning of the list.
533 *
534 * The figure of merit, which is lower the "better" the interface is,
535 * has the uppermost bit set if the interface isn't running, the bit
536 * below that set if the interface isn't up, the bit below that set
537 * if the interface is a loopback interface, and the interface index
538 * in the 29 bits below that. (Yes, we assume u_int is 32 bits.)
539 */
540 static u_int
541 get_figure_of_merit(pcap_if_t *dev)
542 {
543 const char *cp;
544 u_int n;
545
546 if (strcmp(dev->name, "any") == 0) {
547 /*
548 * Give the "any" device an artificially high instance
549 * number, so it shows up after all other non-loopback
550 * interfaces.
551 */
552 n = 0x1FFFFFFF; /* 29 all-1 bits */
553 } else {
554 /*
555 * A number at the end of the device name string is
556 * assumed to be an instance number. Add 1 to the
557 * instance number, and use 0 for "no instance
558 * number", so we don't put "no instance number"
559 * devices and "instance 0" devices together.
560 */
561 cp = dev->name + strlen(dev->name) - 1;
562 while (cp-1 >= dev->name && *(cp-1) >= '0' && *(cp-1) <= '9')
563 cp--;
564 if (*cp >= '0' && *cp <= '9')
565 n = atoi(cp) + 1;
566 else
567 n = 0;
568 }
569 if (!(dev->flags & PCAP_IF_RUNNING))
570 n |= 0x80000000;
571 if (!(dev->flags & PCAP_IF_UP))
572 n |= 0x40000000;
573
574 /*
575 * Give non-wireless interfaces that aren't disconnected a better
576 * figure of merit than interfaces that are disconnected, as
577 * "disconnected" should indicate that the interface isn't
578 * plugged into a network and thus won't give you any traffic.
579 *
580 * For wireless interfaces, it means "associated with a network",
581 * which we presume not to necessarily prevent capture, as you
582 * might run the adapter in some flavor of monitor mode.
583 */
584 if (!(dev->flags & PCAP_IF_WIRELESS) &&
585 (dev->flags & PCAP_IF_CONNECTION_STATUS) == PCAP_IF_CONNECTION_STATUS_DISCONNECTED)
586 n |= 0x20000000;
587
588 /*
589 * Sort loopback devices after non-loopback devices, *except* for
590 * disconnected devices.
591 */
592 if (dev->flags & PCAP_IF_LOOPBACK)
593 n |= 0x10000000;
594
595 return (n);
596 }
597
598 #ifndef _WIN32
599 /*
600 * Try to get a description for a given device.
601 * Returns a mallocated description if it could and NULL if it couldn't.
602 *
603 * XXX - on FreeBSDs that support it, should it get the sysctl named
604 * "dev.{adapter family name}.{adapter unit}.%desc" to get a description
605 * of the adapter? Note that "dev.an.0.%desc" is "Aironet PC4500/PC4800"
606 * with my Cisco 350 card, so the name isn't entirely descriptive. The
607 * "dev.an.0.%pnpinfo" has a better description, although one might argue
608 * that the problem is really a driver bug - if it can find out that it's
609 * a Cisco 340 or 350, rather than an old Aironet card, it should use
610 * that in the description.
611 *
612 * Do NetBSD, DragonflyBSD, or OpenBSD support this as well? FreeBSD
613 * and OpenBSD let you get a description, but it's not generated by the OS,
614 * it's set with another ioctl that ifconfig supports; we use that to get
615 * a description in FreeBSD and OpenBSD, but if there is no such
616 * description available, it still might be nice to get some description
617 * string based on the device type or something such as that.
618 *
619 * In macOS, the System Configuration framework can apparently return
620 * names in 10.4 and later.
621 *
622 * It also appears that freedesktop.org's HAL offers an "info.product"
623 * string, but the HAL specification says it "should not be used in any
624 * UI" and "subsystem/capability specific properties" should be used
625 * instead and, in any case, I think HAL is being deprecated in
626 * favor of other stuff such as DeviceKit. DeviceKit doesn't appear
627 * to have any obvious product information for devices, but maybe
628 * I haven't looked hard enough.
629 *
630 * Using the System Configuration framework, or HAL, or DeviceKit, or
631 * whatever, would require that libpcap applications be linked with
632 * the frameworks/libraries in question. That shouldn't be a problem
633 * for programs linking with the shared version of libpcap (unless
634 * you're running on AIX - which I think is the only UN*X that doesn't
635 * support linking a shared library with other libraries on which it
636 * depends, and having an executable linked only with the first shared
637 * library automatically pick up the other libraries when started -
638 * and using HAL or whatever). Programs linked with the static
639 * version of libpcap would have to use pcap-config with the --static
640 * flag in order to get the right linker flags in order to pick up
641 * the additional libraries/frameworks; those programs need that anyway
642 * for libpcap 1.1 and beyond on Linux, as, by default, it requires
643 * -lnl.
644 *
645 * Do any other UN*Xes, or desktop environments support getting a
646 * description?
647 */
648 static char *
649 #ifdef SIOCGIFDESCR
650 get_if_description(const char *name)
651 {
652 char *description = NULL;
653 int s;
654 struct ifreq ifrdesc;
655 #ifndef IFDESCRSIZE
656 size_t descrlen = 64;
657 #else
658 size_t descrlen = IFDESCRSIZE;
659 #endif /* IFDESCRSIZE */
660
661 /*
662 * Get the description for the interface.
663 */
664 memset(&ifrdesc, 0, sizeof ifrdesc);
665 pcap_strlcpy(ifrdesc.ifr_name, name, sizeof ifrdesc.ifr_name);
666 s = socket(AF_INET, SOCK_DGRAM, 0);
667 if (s >= 0) {
668 #ifdef __FreeBSD__
669 /*
670 * On FreeBSD, if the buffer isn't big enough for the
671 * description, the ioctl succeeds, but the description
672 * isn't copied, ifr_buffer.length is set to the description
673 * length, and ifr_buffer.buffer is set to NULL.
674 */
675 for (;;) {
676 free(description);
677 if ((description = malloc(descrlen)) != NULL) {
678 ifrdesc.ifr_buffer.buffer = description;
679 ifrdesc.ifr_buffer.length = descrlen;
680 if (ioctl(s, SIOCGIFDESCR, &ifrdesc) == 0) {
681 if (ifrdesc.ifr_buffer.buffer ==
682 description)
683 break;
684 else
685 descrlen = ifrdesc.ifr_buffer.length;
686 } else {
687 /*
688 * Failed to get interface description.
689 */
690 free(description);
691 description = NULL;
692 break;
693 }
694 } else
695 break;
696 }
697 #else /* __FreeBSD__ */
698 /*
699 * The only other OS that currently supports
700 * SIOCGIFDESCR is OpenBSD, and it has no way
701 * to get the description length - it's clamped
702 * to a maximum of IFDESCRSIZE.
703 */
704 if ((description = malloc(descrlen)) != NULL) {
705 ifrdesc.ifr_data = (caddr_t)description;
706 if (ioctl(s, SIOCGIFDESCR, &ifrdesc) != 0) {
707 /*
708 * Failed to get interface description.
709 */
710 free(description);
711 description = NULL;
712 }
713 }
714 #endif /* __FreeBSD__ */
715 close(s);
716 if (description != NULL && strlen(description) == 0) {
717 /*
718 * Description is empty, so discard it.
719 */
720 free(description);
721 description = NULL;
722 }
723 }
724
725 #ifdef __FreeBSD__
726 /*
727 * For FreeBSD, if we didn't get a description, and this is
728 * a device with a name of the form usbusN, label it as a USB
729 * bus.
730 */
731 if (description == NULL) {
732 if (strncmp(name, "usbus", 5) == 0) {
733 /*
734 * OK, it begins with "usbus".
735 */
736 long busnum;
737 char *p;
738
739 errno = 0;
740 busnum = strtol(name + 5, &p, 10);
741 if (errno == 0 && p != name + 5 && *p == '\0' &&
742 busnum >= 0 && busnum <= INT_MAX) {
743 /*
744 * OK, it's a valid number that's not
745 * bigger than INT_MAX. Construct
746 * a description from it.
747 */
748 static const char descr_prefix[] = "USB bus number ";
749 size_t descr_size;
750
751 /*
752 * Allow enough room for a 32-bit bus number.
753 * sizeof (descr_prefix) includes the
754 * terminating NUL.
755 */
756 descr_size = sizeof (descr_prefix) + 10;
757 description = malloc(descr_size);
758 if (description != NULL) {
759 pcap_snprintf(description, descr_size,
760 "%s%ld", descr_prefix, busnum);
761 }
762 }
763 }
764 }
765 #endif
766 return (description);
767 #else /* SIOCGIFDESCR */
768 get_if_description(const char *name _U_)
769 {
770 return (NULL);
771 #endif /* SIOCGIFDESCR */
772 }
773
774 /*
775 * Look for a given device in the specified list of devices.
776 *
777 * If we find it, return a pointer to its entry.
778 *
779 * If we don't find it, attempt to add an entry for it, with the specified
780 * IFF_ flags and description, and, if that succeeds, return a pointer to
781 * the new entry, otherwise return NULL and set errbuf to an error message.
782 */
783 pcap_if_t *
784 find_or_add_if(pcap_if_list_t *devlistp, const char *name,
785 bpf_u_int32 if_flags, get_if_flags_func get_flags_func, char *errbuf)
786 {
787 bpf_u_int32 pcap_flags;
788
789 /*
790 * Convert IFF_ flags to pcap flags.
791 */
792 pcap_flags = 0;
793 #ifdef IFF_LOOPBACK
794 if (if_flags & IFF_LOOPBACK)
795 pcap_flags |= PCAP_IF_LOOPBACK;
796 #else
797 /*
798 * We don't have IFF_LOOPBACK, so look at the device name to
799 * see if it looks like a loopback device.
800 */
801 if (name[0] == 'l' && name[1] == 'o' &&
802 (isdigit((unsigned char)(name[2])) || name[2] == '\0')
803 pcap_flags |= PCAP_IF_LOOPBACK;
804 #endif
805 #ifdef IFF_UP
806 if (if_flags & IFF_UP)
807 pcap_flags |= PCAP_IF_UP;
808 #endif
809 #ifdef IFF_RUNNING
810 if (if_flags & IFF_RUNNING)
811 pcap_flags |= PCAP_IF_RUNNING;
812 #endif
813
814 /*
815 * Attempt to find an entry for this device; if we don't find one,
816 * attempt to add one.
817 */
818 return (find_or_add_dev(devlistp, name, pcap_flags,
819 get_flags_func, get_if_description(name), errbuf));
820 }
821
822 /*
823 * Look for a given device in the specified list of devices.
824 *
825 * If we find it, then, if the specified address isn't null, add it to
826 * the list of addresses for the device and return 0.
827 *
828 * If we don't find it, attempt to add an entry for it, with the specified
829 * IFF_ flags and description, and, if that succeeds, add the specified
830 * address to its list of addresses if that address is non-null, and
831 * return 0, otherwise return -1 and set errbuf to an error message.
832 *
833 * (We can get called with a null address because we might get a list
834 * of interface name/address combinations from the underlying OS, with
835 * the address being absent in some cases, rather than a list of
836 * interfaces with each interface having a list of addresses, so this
837 * call may be the only call made to add to the list, and we want to
838 * add interfaces even if they have no addresses.)
839 */
840 int
841 add_addr_to_if(pcap_if_list_t *devlistp, const char *name,
842 bpf_u_int32 if_flags, get_if_flags_func get_flags_func,
843 struct sockaddr *addr, size_t addr_size,
844 struct sockaddr *netmask, size_t netmask_size,
845 struct sockaddr *broadaddr, size_t broadaddr_size,
846 struct sockaddr *dstaddr, size_t dstaddr_size,
847 char *errbuf)
848 {
849 pcap_if_t *curdev;
850
851 /*
852 * Check whether the device exists and, if not, add it.
853 */
854 curdev = find_or_add_if(devlistp, name, if_flags, get_flags_func,
855 errbuf);
856 if (curdev == NULL) {
857 /*
858 * Error - give up.
859 */
860 return (-1);
861 }
862
863 if (addr == NULL) {
864 /*
865 * There's no address to add; this entry just meant
866 * "here's a new interface".
867 */
868 return (0);
869 }
870
871 /*
872 * "curdev" is an entry for this interface, and we have an
873 * address for it; add an entry for that address to the
874 * interface's list of addresses.
875 */
876 return (add_addr_to_dev(curdev, addr, addr_size, netmask,
877 netmask_size, broadaddr, broadaddr_size, dstaddr,
878 dstaddr_size, errbuf));
879 }
880 #endif /* _WIN32 */
881
882 /*
883 * Add an entry to the list of addresses for an interface.
884 * "curdev" is the entry for that interface.
885 */
886 int
887 add_addr_to_dev(pcap_if_t *curdev,
888 struct sockaddr *addr, size_t addr_size,
889 struct sockaddr *netmask, size_t netmask_size,
890 struct sockaddr *broadaddr, size_t broadaddr_size,
891 struct sockaddr *dstaddr, size_t dstaddr_size,
892 char *errbuf)
893 {
894 pcap_addr_t *curaddr, *prevaddr, *nextaddr;
895
896 /*
897 * Allocate the new entry and fill it in.
898 */
899 curaddr = (pcap_addr_t *)malloc(sizeof(pcap_addr_t));
900 if (curaddr == NULL) {
901 pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
902 errno, "malloc");
903 return (-1);
904 }
905
906 curaddr->next = NULL;
907 if (addr != NULL && addr_size != 0) {
908 curaddr->addr = (struct sockaddr *)dup_sockaddr(addr, addr_size);
909 if (curaddr->addr == NULL) {
910 pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
911 errno, "malloc");
912 free(curaddr);
913 return (-1);
914 }
915 } else
916 curaddr->addr = NULL;
917
918 if (netmask != NULL && netmask_size != 0) {
919 curaddr->netmask = (struct sockaddr *)dup_sockaddr(netmask, netmask_size);
920 if (curaddr->netmask == NULL) {
921 pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
922 errno, "malloc");
923 if (curaddr->addr != NULL)
924 free(curaddr->addr);
925 free(curaddr);
926 return (-1);
927 }
928 } else
929 curaddr->netmask = NULL;
930
931 if (broadaddr != NULL && broadaddr_size != 0) {
932 curaddr->broadaddr = (struct sockaddr *)dup_sockaddr(broadaddr, broadaddr_size);
933 if (curaddr->broadaddr == NULL) {
934 pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
935 errno, "malloc");
936 if (curaddr->netmask != NULL)
937 free(curaddr->netmask);
938 if (curaddr->addr != NULL)
939 free(curaddr->addr);
940 free(curaddr);
941 return (-1);
942 }
943 } else
944 curaddr->broadaddr = NULL;
945
946 if (dstaddr != NULL && dstaddr_size != 0) {
947 curaddr->dstaddr = (struct sockaddr *)dup_sockaddr(dstaddr, dstaddr_size);
948 if (curaddr->dstaddr == NULL) {
949 pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
950 errno, "malloc");
951 if (curaddr->broadaddr != NULL)
952 free(curaddr->broadaddr);
953 if (curaddr->netmask != NULL)
954 free(curaddr->netmask);
955 if (curaddr->addr != NULL)
956 free(curaddr->addr);
957 free(curaddr);
958 return (-1);
959 }
960 } else
961 curaddr->dstaddr = NULL;
962
963 /*
964 * Find the end of the list of addresses.
965 */
966 for (prevaddr = curdev->addresses; prevaddr != NULL; prevaddr = nextaddr) {
967 nextaddr = prevaddr->next;
968 if (nextaddr == NULL) {
969 /*
970 * This is the end of the list.
971 */
972 break;
973 }
974 }
975
976 if (prevaddr == NULL) {
977 /*
978 * The list was empty; this is the first member.
979 */
980 curdev->addresses = curaddr;
981 } else {
982 /*
983 * "prevaddr" is the last member of the list; append
984 * this member to it.
985 */
986 prevaddr->next = curaddr;
987 }
988
989 return (0);
990 }
991
992 /*
993 * Look for a given device in the specified list of devices.
994 *
995 * If we find it, return 0 and set *curdev_ret to point to it.
996 *
997 * If we don't find it, attempt to add an entry for it, with the specified
998 * flags and description, and, if that succeeds, return 0, otherwise
999 * return -1 and set errbuf to an error message.
1000 */
1001 pcap_if_t *
1002 find_or_add_dev(pcap_if_list_t *devlistp, const char *name, bpf_u_int32 flags,
1003 get_if_flags_func get_flags_func, const char *description, char *errbuf)
1004 {
1005 pcap_if_t *curdev;
1006
1007 /*
1008 * Is there already an entry in the list for this device?
1009 */
1010 curdev = find_dev(devlistp, name);
1011 if (curdev != NULL) {
1012 /*
1013 * Yes, return it.
1014 */
1015 return (curdev);
1016 }
1017
1018 /*
1019 * No, we didn't find it.
1020 */
1021
1022 /*
1023 * Try to get additional flags for the device.
1024 */
1025 if ((*get_flags_func)(name, &flags, errbuf) == -1) {
1026 /*
1027 * Failed.
1028 */
1029 return (NULL);
1030 }
1031
1032 /*
1033 * Now, try to add it to the list of devices.
1034 */
1035 return (add_dev(devlistp, name, flags, description, errbuf));
1036 }
1037
1038 /*
1039 * Look for a given device in the specified list of devices, and return
1040 * the entry for it if we find it or NULL if we don't.
1041 */
1042 pcap_if_t *
1043 find_dev(pcap_if_list_t *devlistp, const char *name)
1044 {
1045 pcap_if_t *curdev;
1046
1047 /*
1048 * Is there an entry in the list for this device?
1049 */
1050 for (curdev = devlistp->beginning; curdev != NULL;
1051 curdev = curdev->next) {
1052 if (strcmp(name, curdev->name) == 0) {
1053 /*
1054 * We found it, so, yes, there is. No need to
1055 * add it. Provide the entry we found to our
1056 * caller.
1057 */
1058 return (curdev);
1059 }
1060 }
1061
1062 /*
1063 * No.
1064 */
1065 return (NULL);
1066 }
1067
1068 /*
1069 * Attempt to add an entry for a device, with the specified flags
1070 * and description, and, if that succeeds, return 0 and return a pointer
1071 * to the new entry, otherwise return NULL and set errbuf to an error
1072 * message.
1073 *
1074 * If we weren't given a description, try to get one.
1075 */
1076 pcap_if_t *
1077 add_dev(pcap_if_list_t *devlistp, const char *name, bpf_u_int32 flags,
1078 const char *description, char *errbuf)
1079 {
1080 pcap_if_t *curdev, *prevdev, *nextdev;
1081 u_int this_figure_of_merit, nextdev_figure_of_merit;
1082
1083 curdev = malloc(sizeof(pcap_if_t));
1084 if (curdev == NULL) {
1085 pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
1086 errno, "malloc");
1087 return (NULL);
1088 }
1089
1090 /*
1091 * Fill in the entry.
1092 */
1093 curdev->next = NULL;
1094 curdev->name = strdup(name);
1095 if (curdev->name == NULL) {
1096 pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
1097 errno, "malloc");
1098 free(curdev);
1099 return (NULL);
1100 }
1101 if (description == NULL) {
1102 /*
1103 * We weren't handed a description for the interface.
1104 */
1105 curdev->description = NULL;
1106 } else {
1107 /*
1108 * We were handed a description; make a copy.
1109 */
1110 curdev->description = strdup(description);
1111 if (curdev->description == NULL) {
1112 pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
1113 errno, "malloc");
1114 free(curdev->name);
1115 free(curdev);
1116 return (NULL);
1117 }
1118 }
1119 curdev->addresses = NULL; /* list starts out as empty */
1120 curdev->flags = flags;
1121
1122 /*
1123 * Add it to the list, in the appropriate location.
1124 * First, get the "figure of merit" for this interface.
1125 */
1126 this_figure_of_merit = get_figure_of_merit(curdev);
1127
1128 /*
1129 * Now look for the last interface with an figure of merit
1130 * less than or equal to the new interface's figure of merit.
1131 *
1132 * We start with "prevdev" being NULL, meaning we're before
1133 * the first element in the list.
1134 */
1135 prevdev = NULL;
1136 for (;;) {
1137 /*
1138 * Get the interface after this one.
1139 */
1140 if (prevdev == NULL) {
1141 /*
1142 * The next element is the first element.
1143 */
1144 nextdev = devlistp->beginning;
1145 } else
1146 nextdev = prevdev->next;
1147
1148 /*
1149 * Are we at the end of the list?
1150 */
1151 if (nextdev == NULL) {
1152 /*
1153 * Yes - we have to put the new entry after "prevdev".
1154 */
1155 break;
1156 }
1157
1158 /*
1159 * Is the new interface's figure of merit less
1160 * than the next interface's figure of merit,
1161 * meaning that the new interface is better
1162 * than the next interface?
1163 */
1164 nextdev_figure_of_merit = get_figure_of_merit(nextdev);
1165 if (this_figure_of_merit < nextdev_figure_of_merit) {
1166 /*
1167 * Yes - we should put the new entry
1168 * before "nextdev", i.e. after "prevdev".
1169 */
1170 break;
1171 }
1172
1173 prevdev = nextdev;
1174 }
1175
1176 /*
1177 * Insert before "nextdev".
1178 */
1179 curdev->next = nextdev;
1180
1181 /*
1182 * Insert after "prevdev" - unless "prevdev" is null,
1183 * in which case this is the first interface.
1184 */
1185 if (prevdev == NULL) {
1186 /*
1187 * This is the first interface. Make it
1188 * the first element in the list of devices.
1189 */
1190 devlistp->beginning = curdev;
1191 } else
1192 prevdev->next = curdev;
1193 return (curdev);
1194 }
1195
1196 /*
1197 * Free a list of interfaces.
1198 */
1199 void
1200 pcap_freealldevs(pcap_if_t *alldevs)
1201 {
1202 pcap_if_t *curdev, *nextdev;
1203 pcap_addr_t *curaddr, *nextaddr;
1204
1205 for (curdev = alldevs; curdev != NULL; curdev = nextdev) {
1206 nextdev = curdev->next;
1207
1208 /*
1209 * Free all addresses.
1210 */
1211 for (curaddr = curdev->addresses; curaddr != NULL; curaddr = nextaddr) {
1212 nextaddr = curaddr->next;
1213 if (curaddr->addr)
1214 free(curaddr->addr);
1215 if (curaddr->netmask)
1216 free(curaddr->netmask);
1217 if (curaddr->broadaddr)
1218 free(curaddr->broadaddr);
1219 if (curaddr->dstaddr)
1220 free(curaddr->dstaddr);
1221 free(curaddr);
1222 }
1223
1224 /*
1225 * Free the name string.
1226 */
1227 free(curdev->name);
1228
1229 /*
1230 * Free the description string, if any.
1231 */
1232 if (curdev->description != NULL)
1233 free(curdev->description);
1234
1235 /*
1236 * Free the interface.
1237 */
1238 free(curdev);
1239 }
1240 }
1241
1242 /*
1243 * pcap-npf.c has its own pcap_lookupdev(), for compatibility reasons, as
1244 * it actually returns the names of all interfaces, with a NUL separator
1245 * between them; some callers may depend on that.
1246 *
1247 * MS-DOS has its own pcap_lookupdev(), but that might be useful only
1248 * as an optimization.
1249 *
1250 * In all other cases, we just use pcap_findalldevs() to get a list of
1251 * devices, and pick from that list.
1252 */
1253 #if !defined(HAVE_PACKET32) && !defined(MSDOS)
1254 /*
1255 * Return the name of a network interface attached to the system, or NULL
1256 * if none can be found. The interface must be configured up; the
1257 * lowest unit number is preferred; loopback is ignored.
1258 */
1259 char *
1260 pcap_lookupdev(char *errbuf)
1261 {
1262 pcap_if_t *alldevs;
1263 #ifdef _WIN32
1264 /*
1265 * Windows - use the same size as the old WinPcap 3.1 code.
1266 * XXX - this is probably bigger than it needs to be.
1267 */
1268 #define IF_NAMESIZE 8192
1269 #else
1270 /*
1271 * UN*X - use the system's interface name size.
1272 * XXX - that might not be large enough for capture devices
1273 * that aren't regular network interfaces.
1274 */
1275 /* for old BSD systems, including bsdi3 */
1276 #ifndef IF_NAMESIZE
1277 #define IF_NAMESIZE IFNAMSIZ
1278 #endif
1279 #endif
1280 static char device[IF_NAMESIZE + 1];
1281 char *ret;
1282
1283 if (pcap_findalldevs(&alldevs, errbuf) == -1)
1284 return (NULL);
1285
1286 if (alldevs == NULL || (alldevs->flags & PCAP_IF_LOOPBACK)) {
1287 /*
1288 * There are no devices on the list, or the first device
1289 * on the list is a loopback device, which means there
1290 * are no non-loopback devices on the list. This means
1291 * we can't return any device.
1292 *
1293 * XXX - why not return a loopback device? If we can't
1294 * capture on it, it won't be on the list, and if it's
1295 * on the list, there aren't any non-loopback devices,
1296 * so why not just supply it as the default device?
1297 */
1298 (void)pcap_strlcpy(errbuf, "no suitable device found",
1299 PCAP_ERRBUF_SIZE);
1300 ret = NULL;
1301 } else {
1302 /*
1303 * Return the name of the first device on the list.
1304 */
1305 (void)pcap_strlcpy(device, alldevs->name, sizeof(device));
1306 ret = device;
1307 }
1308
1309 pcap_freealldevs(alldevs);
1310 return (ret);
1311 }
1312 #endif /* !defined(HAVE_PACKET32) && !defined(MSDOS) */
1313
1314 #if !defined(_WIN32) && !defined(MSDOS)
1315 /*
1316 * We don't just fetch the entire list of devices, search for the
1317 * particular device, and use its first IPv4 address, as that's too
1318 * much work to get just one device's netmask.
1319 *
1320 * If we had an API to get attributes for a given device, we could
1321 * use that.
1322 */
1323 int
1324 pcap_lookupnet(const char *device, bpf_u_int32 *netp, bpf_u_int32 *maskp,
1325 char *errbuf)
1326 {
1327 register int fd;
1328 register struct sockaddr_in *sin4;
1329 struct ifreq ifr;
1330
1331 /*
1332 * The pseudo-device "any" listens on all interfaces and therefore
1333 * has the network address and -mask "0.0.0.0" therefore catching
1334 * all traffic. Using NULL for the interface is the same as "any".
1335 */
1336 if (!device || strcmp(device, "any") == 0
1337 #ifdef HAVE_DAG_API
1338 || strstr(device, "dag") != NULL
1339 #endif
1340 #ifdef HAVE_SEPTEL_API
1341 || strstr(device, "septel") != NULL
1342 #endif
1343 #ifdef PCAP_SUPPORT_BT
1344 || strstr(device, "bluetooth") != NULL
1345 #endif
1346 #ifdef PCAP_SUPPORT_USB
1347 || strstr(device, "usbmon") != NULL
1348 #endif
1349 #ifdef HAVE_SNF_API
1350 || strstr(device, "snf") != NULL
1351 #endif
1352 #ifdef PCAP_SUPPORT_NETMAP
1353 || strncmp(device, "netmap:", 7) == 0
1354 || strncmp(device, "vale", 4) == 0
1355 #endif
1356 #ifdef PCAP_SUPPORT_DPDK
1357 || strncmp(device, "dpdk:", 5) == 0
1358 #endif
1359 ) {
1360 *netp = *maskp = 0;
1361 return 0;
1362 }
1363
1364 fd = socket(AF_INET, SOCK_DGRAM, 0);
1365 if (fd < 0) {
1366 pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
1367 errno, "socket");
1368 return (-1);
1369 }
1370 memset(&ifr, 0, sizeof(ifr));
1371 #ifdef linux
1372 /* XXX Work around Linux kernel bug */
1373 ifr.ifr_addr.sa_family = AF_INET;
1374 #endif
1375 (void)pcap_strlcpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
1376 if (ioctl(fd, SIOCGIFADDR, (char *)&ifr) < 0) {
1377 if (errno == EADDRNOTAVAIL) {
1378 (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
1379 "%s: no IPv4 address assigned", device);
1380 } else {
1381 pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
1382 errno, "SIOCGIFADDR: %s", device);
1383 }
1384 (void)close(fd);
1385 return (-1);
1386 }
1387 sin4 = (struct sockaddr_in *)&ifr.ifr_addr;
1388 *netp = sin4->sin_addr.s_addr;
1389 memset(&ifr, 0, sizeof(ifr));
1390 #ifdef linux
1391 /* XXX Work around Linux kernel bug */
1392 ifr.ifr_addr.sa_family = AF_INET;
1393 #endif
1394 (void)pcap_strlcpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
1395 if (ioctl(fd, SIOCGIFNETMASK, (char *)&ifr) < 0) {
1396 pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
1397 errno, "SIOCGIFNETMASK: %s", device);
1398 (void)close(fd);
1399 return (-1);
1400 }
1401 (void)close(fd);
1402 *maskp = sin4->sin_addr.s_addr;
1403 if (*maskp == 0) {
1404 if (IN_CLASSA(*netp))
1405 *maskp = IN_CLASSA_NET;
1406 else if (IN_CLASSB(*netp))
1407 *maskp = IN_CLASSB_NET;
1408 else if (IN_CLASSC(*netp))
1409 *maskp = IN_CLASSC_NET;
1410 else {
1411 (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
1412 "inet class for 0x%x unknown", *netp);
1413 return (-1);
1414 }
1415 }
1416 *netp &= *maskp;
1417 return (0);
1418 }
1419 #endif /* !defined(_WIN32) && !defined(MSDOS) */
1420
1421 #ifdef ENABLE_REMOTE
1422 #include "pcap-rpcap.h"
1423
1424 /*
1425 * Extract a substring from a string.
1426 */
1427 static char *
1428 get_substring(const char *p, size_t len, char *ebuf)
1429 {
1430 char *token;
1431
1432 token = malloc(len + 1);
1433 if (token == NULL) {
1434 pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
1435 errno, "malloc");
1436 return (NULL);
1437 }
1438 memcpy(token, p, len);
1439 token[len] = '\0';
1440 return (token);
1441 }
1442
1443 /*
1444 * Parse a capture source that might be a URL.
1445 *
1446 * If the source is not a URL, *schemep, *userinfop, *hostp, and *portp
1447 * are set to NULL, *pathp is set to point to the source, and 0 is
1448 * returned.
1449 *
1450 * If source is a URL, and the URL refers to a local device (a special
1451 * case of rpcap:), *schemep, *userinfop, *hostp, and *portp are set
1452 * to NULL, *pathp is set to point to the device name, and 0 is returned.
1453 *
1454 * If source is a URL, and it's not a special case that refers to a local
1455 * device, and the parse succeeds:
1456 *
1457 * *schemep is set to point to an allocated string containing the scheme;
1458 *
1459 * if user information is present in the URL, *userinfop is set to point
1460 * to an allocated string containing the user information, otherwise
1461 * it's set to NULL;
1462 *
1463 * if host information is present in the URL, *hostp is set to point
1464 * to an allocated string containing the host information, otherwise
1465 * it's set to NULL;
1466 *
1467 * if a port number is present in the URL, *portp is set to point
1468 * to an allocated string containing the port number, otherwise
1469 * it's set to NULL;
1470 *
1471 * *pathp is set to point to an allocated string containing the
1472 * path;
1473 *
1474 * and 0 is returned.
1475 *
1476 * If the parse fails, ebuf is set to an error string, and -1 is returned.
1477 */
1478 static int
1479 pcap_parse_source(const char *source, char **schemep, char **userinfop,
1480 char **hostp, char **portp, char **pathp, char *ebuf)
1481 {
1482 char *colonp;
1483 size_t scheme_len;
1484 char *scheme;
1485 const char *endp;
1486 size_t authority_len;
1487 char *authority;
1488 char *parsep, *atsignp, *bracketp;
1489 char *userinfo, *host, *port, *path;
1490
1491 /*
1492 * Start out returning nothing.
1493 */
1494 *schemep = NULL;
1495 *userinfop = NULL;
1496 *hostp = NULL;
1497 *portp = NULL;
1498 *pathp = NULL;
1499
1500 /*
1501 * RFC 3986 says:
1502 *
1503 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
1504 *
1505 * hier-part = "//" authority path-abempty
1506 * / path-absolute
1507 * / path-rootless
1508 * / path-empty
1509 *
1510 * authority = [ userinfo "@" ] host [ ":" port ]
1511 *
1512 * userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
1513 *
1514 * Step 1: look for the ":" at the end of the scheme.
1515 * A colon in the source is *NOT* sufficient to indicate that
1516 * this is a URL, as interface names on some platforms might
1517 * include colons (e.g., I think some Solaris interfaces
1518 * might).
1519 */
1520 colonp = strchr(source, ':');
1521 if (colonp == NULL) {
1522 /*
1523 * The source is the device to open.
1524 * Return a NULL pointer for the scheme, user information,
1525 * host, and port, and return the device as the path.
1526 */
1527 *pathp = strdup(source);
1528 if (*pathp == NULL) {
1529 pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
1530 errno, "malloc");
1531 return (-1);
1532 }
1533 return (0);
1534 }
1535
1536 /*
1537 * All schemes must have "//" after them, i.e. we only support
1538 * hier-part = "//" authority path-abempty, not
1539 * hier-part = path-absolute
1540 * hier-part = path-rootless
1541 * hier-part = path-empty
1542 *
1543 * We need that in order to distinguish between a local device
1544 * name that happens to contain a colon and a URI.
1545 */
1546 if (strncmp(colonp + 1, "//", 2) != 0) {
1547 /*
1548 * The source is the device to open.
1549 * Return a NULL pointer for the scheme, user information,
1550 * host, and port, and return the device as the path.
1551 */
1552 *pathp = strdup(source);
1553 if (*pathp == NULL) {
1554 pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
1555 errno, "malloc");
1556 return (-1);
1557 }
1558 return (0);
1559 }
1560
1561 /*
1562 * XXX - check whether the purported scheme could be a scheme?
1563 */
1564
1565 /*
1566 * OK, this looks like a URL.
1567 * Get the scheme.
1568 */
1569 scheme_len = colonp - source;
1570 scheme = malloc(scheme_len + 1);
1571 if (scheme == NULL) {
1572 pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
1573 errno, "malloc");
1574 return (-1);
1575 }
1576 memcpy(scheme, source, scheme_len);
1577 scheme[scheme_len] = '\0';
1578
1579 /*
1580 * Treat file: specially - take everything after file:// as
1581 * the pathname.
1582 */
1583 if (pcap_strcasecmp(scheme, "file") == 0) {
1584 *schemep = scheme;
1585 *pathp = strdup(colonp + 3);
1586 if (*pathp == NULL) {
1587 pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
1588 errno, "malloc");
1589 return (-1);
1590 }
1591 return (0);
1592 }
1593
1594 /*
1595 * The WinPcap documentation says you can specify a local
1596 * interface with "rpcap://{device}"; we special-case
1597 * that here. If the scheme is "rpcap", and there are
1598 * no slashes past the "//", we just return the device.
1599 *
1600 * XXX - %-escaping?
1601 */
1602 if (pcap_strcasecmp(scheme, "rpcap") == 0 &&
1603 strchr(colonp + 3, '/') == NULL) {
1604 /*
1605 * Local device.
1606 *
1607 * Return a NULL pointer for the scheme, user information,
1608 * host, and port, and return the device as the path.
1609 */
1610 free(scheme);
1611 *pathp = strdup(colonp + 3);
1612 if (*pathp == NULL) {
1613 pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
1614 errno, "malloc");
1615 return (-1);
1616 }
1617 return (0);
1618 }
1619
1620 /*
1621 * OK, now start parsing the authority.
1622 * Get token, terminated with / or terminated at the end of
1623 * the string.
1624 */
1625 authority_len = strcspn(colonp + 3, "/");
1626 authority = get_substring(colonp + 3, authority_len, ebuf);
1627 if (authority == NULL) {
1628 /*
1629 * Error.
1630 */
1631 free(scheme);
1632 return (-1);
1633 }
1634 endp = colonp + 3 + authority_len;
1635
1636 /*
1637 * Now carve the authority field into its components.
1638 */
1639 parsep = authority;
1640
1641 /*
1642 * Is there a userinfo field?
1643 */
1644 atsignp = strchr(parsep, '@');
1645 if (atsignp != NULL) {
1646 /*
1647 * Yes.
1648 */
1649 size_t userinfo_len;
1650
1651 userinfo_len = atsignp - parsep;
1652 userinfo = get_substring(parsep, userinfo_len, ebuf);
1653 if (userinfo == NULL) {
1654 /*
1655 * Error.
1656 */
1657 free(authority);
1658 free(scheme);
1659 return (-1);
1660 }
1661 parsep = atsignp + 1;
1662 } else {
1663 /*
1664 * No.
1665 */
1666 userinfo = NULL;
1667 }
1668
1669 /*
1670 * Is there a host field?
1671 */
1672 if (*parsep == '\0') {
1673 /*
1674 * No; there's no host field or port field.
1675 */
1676 host = NULL;
1677 port = NULL;
1678 } else {
1679 /*
1680 * Yes.
1681 */
1682 size_t host_len;
1683
1684 /*
1685 * Is it an IP-literal?
1686 */
1687 if (*parsep == '[') {
1688 /*
1689 * Yes.
1690 * Treat verything up to the closing square
1691 * bracket as the IP-Literal; we don't worry
1692 * about whether it's a valid IPv6address or
1693 * IPvFuture.
1694 */
1695 bracketp = strchr(parsep, ']');
1696 if (bracketp == NULL) {
1697 /*
1698 * There's no closing square bracket.
1699 */
1700 pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE,
1701 "IP-literal in URL doesn't end with ]");
1702 free(userinfo);
1703 free(authority);
1704 free(scheme);
1705 return (-1);
1706 }
1707 if (*(bracketp + 1) != '\0' &&
1708 *(bracketp + 1) != ':') {
1709 /*
1710 * There's extra crud after the
1711 * closing square bracketn.
1712 */
1713 pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE,
1714 "Extra text after IP-literal in URL");
1715 free(userinfo);
1716 free(authority);
1717 free(scheme);
1718 return (-1);
1719 }
1720 host_len = (bracketp - 1) - parsep;
1721 host = get_substring(parsep + 1, host_len, ebuf);
1722 if (host == NULL) {
1723 /*
1724 * Error.
1725 */
1726 free(userinfo);
1727 free(authority);
1728 free(scheme);
1729 return (-1);
1730 }
1731 parsep = bracketp + 1;
1732 } else {
1733 /*
1734 * No.
1735 * Treat everything up to a : or the end of
1736 * the string as the host.
1737 */
1738 host_len = strcspn(parsep, ":");
1739 host = get_substring(parsep, host_len, ebuf);
1740 if (host == NULL) {
1741 /*
1742 * Error.
1743 */
1744 free(userinfo);
1745 free(authority);
1746 free(scheme);
1747 return (-1);
1748 }
1749 parsep = parsep + host_len;
1750 }
1751
1752 /*
1753 * Is there a port field?
1754 */
1755 if (*parsep == ':') {
1756 /*
1757 * Yes. It's the rest of the authority field.
1758 */
1759 size_t port_len;
1760
1761 parsep++;
1762 port_len = strlen(parsep);
1763 port = get_substring(parsep, port_len, ebuf);
1764 if (port == NULL) {
1765 /*
1766 * Error.
1767 */
1768 free(host);
1769 free(userinfo);
1770 free(authority);
1771 free(scheme);
1772 return (-1);
1773 }
1774 } else {
1775 /*
1776 * No.
1777 */
1778 port = NULL;
1779 }
1780 }
1781 free(authority);
1782
1783 /*
1784 * Everything else is the path. Strip off the leading /.
1785 */
1786 if (*endp == '\0')
1787 path = strdup("");
1788 else
1789 path = strdup(endp + 1);
1790 if (path == NULL) {
1791 pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
1792 errno, "malloc");
1793 free(port);
1794 free(host);
1795 free(userinfo);
1796 free(scheme);
1797 return (-1);
1798 }
1799 *schemep = scheme;
1800 *userinfop = userinfo;
1801 *hostp = host;
1802 *portp = port;
1803 *pathp = path;
1804 return (0);
1805 }
1806
1807 int
1808 pcap_createsrcstr(char *source, int type, const char *host, const char *port,
1809 const char *name, char *errbuf)
1810 {
1811 switch (type) {
1812
1813 case PCAP_SRC_FILE:
1814 pcap_strlcpy(source, PCAP_SRC_FILE_STRING, PCAP_BUF_SIZE);
1815 if (name != NULL && *name != '\0') {
1816 pcap_strlcat(source, name, PCAP_BUF_SIZE);
1817 return (0);
1818 } else {
1819 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
1820 "The file name cannot be NULL.");
1821 return (-1);
1822 }
1823
1824 case PCAP_SRC_IFREMOTE:
1825 pcap_strlcpy(source, PCAP_SRC_IF_STRING, PCAP_BUF_SIZE);
1826 if (host != NULL && *host != '\0') {
1827 if (strchr(host, ':') != NULL) {
1828 /*
1829 * The host name contains a colon, so it's
1830 * probably an IPv6 address, and needs to
1831 * be included in square brackets.
1832 */
1833 pcap_strlcat(source, "[", PCAP_BUF_SIZE);
1834 pcap_strlcat(source, host, PCAP_BUF_SIZE);
1835 pcap_strlcat(source, "]", PCAP_BUF_SIZE);
1836 } else
1837 pcap_strlcat(source, host, PCAP_BUF_SIZE);
1838
1839 if (port != NULL && *port != '\0') {
1840 pcap_strlcat(source, ":", PCAP_BUF_SIZE);
1841 pcap_strlcat(source, port, PCAP_BUF_SIZE);
1842 }
1843
1844 pcap_strlcat(source, "/", PCAP_BUF_SIZE);
1845 } else {
1846 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
1847 "The host name cannot be NULL.");
1848 return (-1);
1849 }
1850
1851 if (name != NULL && *name != '\0')
1852 pcap_strlcat(source, name, PCAP_BUF_SIZE);
1853
1854 return (0);
1855
1856 case PCAP_SRC_IFLOCAL:
1857 pcap_strlcpy(source, PCAP_SRC_IF_STRING, PCAP_BUF_SIZE);
1858
1859 if (name != NULL && *name != '\0')
1860 pcap_strlcat(source, name, PCAP_BUF_SIZE);
1861
1862 return (0);
1863
1864 default:
1865 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
1866 "The interface type is not valid.");
1867 return (-1);
1868 }
1869 }
1870
1871 int
1872 pcap_parsesrcstr(const char *source, int *type, char *host, char *port,
1873 char *name, char *errbuf)
1874 {
1875 char *scheme, *tmpuserinfo, *tmphost, *tmpport, *tmppath;
1876
1877 /* Initialization stuff */
1878 if (host)
1879 *host = '\0';
1880 if (port)
1881 *port = '\0';
1882 if (name)
1883 *name = '\0';
1884
1885 /* Parse the source string */
1886 if (pcap_parse_source(source, &scheme, &tmpuserinfo, &tmphost,
1887 &tmpport, &tmppath, errbuf) == -1) {
1888 /*
1889 * Fail.
1890 */
1891 return (-1);
1892 }
1893
1894 if (scheme == NULL) {
1895 /*
1896 * Local device.
1897 */
1898 if (name && tmppath)
1899 pcap_strlcpy(name, tmppath, PCAP_BUF_SIZE);
1900 if (type)
1901 *type = PCAP_SRC_IFLOCAL;
1902 free(tmppath);
1903 free(tmpport);
1904 free(tmphost);
1905 free(tmpuserinfo);
1906 return (0);
1907 }
1908
1909 if (strcmp(scheme, "rpcap") == 0) {
1910 /*
1911 * rpcap://
1912 *
1913 * pcap_parse_source() has already handled the case of
1914 * rpcap://device
1915 */
1916 if (host && tmphost) {
1917 if (tmpuserinfo)
1918 pcap_snprintf(host, PCAP_BUF_SIZE, "%s@%s",
1919 tmpuserinfo, tmphost);
1920 else
1921 pcap_strlcpy(host, tmphost, PCAP_BUF_SIZE);
1922 }
1923 if (port && tmpport)
1924 pcap_strlcpy(port, tmpport, PCAP_BUF_SIZE);
1925 if (name && tmppath)
1926 pcap_strlcpy(name, tmppath, PCAP_BUF_SIZE);
1927 if (type)
1928 *type = PCAP_SRC_IFREMOTE;
1929 free(tmppath);
1930 free(tmpport);
1931 free(tmphost);
1932 free(tmpuserinfo);
1933 free(scheme);
1934 return (0);
1935 }
1936
1937 if (strcmp(scheme, "file") == 0) {
1938 /*
1939 * file://
1940 */
1941 if (name && tmppath)
1942 pcap_strlcpy(name, tmppath, PCAP_BUF_SIZE);
1943 if (type)
1944 *type = PCAP_SRC_FILE;
1945 free(tmppath);
1946 free(tmpport);
1947 free(tmphost);
1948 free(tmpuserinfo);
1949 free(scheme);
1950 return (0);
1951 }
1952
1953 /*
1954 * Neither rpcap: nor file:; just treat the entire string
1955 * as a local device.
1956 */
1957 if (name)
1958 pcap_strlcpy(name, source, PCAP_BUF_SIZE);
1959 if (type)
1960 *type = PCAP_SRC_IFLOCAL;
1961 free(tmppath);
1962 free(tmpport);
1963 free(tmphost);
1964 free(tmpuserinfo);
1965 free(scheme);
1966 return (0);
1967 }
1968 #endif
1969
1970 pcap_t *
1971 pcap_create(const char *device, char *errbuf)
1972 {
1973 size_t i;
1974 int is_theirs;
1975 pcap_t *p;
1976 char *device_str;
1977
1978 /*
1979 * A null device name is equivalent to the "any" device -
1980 * which might not be supported on this platform, but
1981 * this means that you'll get a "not supported" error
1982 * rather than, say, a crash when we try to dereference
1983 * the null pointer.
1984 */
1985 if (device == NULL)
1986 device_str = strdup("any");
1987 else {
1988 #ifdef _WIN32
1989 /*
1990 * If the string appears to be little-endian UCS-2/UTF-16,
1991 * convert it to ASCII.
1992 *
1993 * XXX - to UTF-8 instead? Or report an error if any
1994 * character isn't ASCII?
1995 */
1996 if (device[0] != '\0' && device[1] == '\0') {
1997 size_t length;
1998
1999 length = wcslen((wchar_t *)device);
2000 device_str = (char *)malloc(length + 1);
2001 if (device_str == NULL) {
2002 pcap_fmt_errmsg_for_errno(errbuf,
2003 PCAP_ERRBUF_SIZE, errno,
2004 "malloc");
2005 return (NULL);
2006 }
2007
2008 pcap_snprintf(device_str, length + 1, "%ws",
2009 (const wchar_t *)device);
2010 } else
2011 #endif
2012 device_str = strdup(device);
2013 }
2014 if (device_str == NULL) {
2015 pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
2016 errno, "malloc");
2017 return (NULL);
2018 }
2019
2020 /*
2021 * Try each of the non-local-network-interface capture
2022 * source types until we find one that works for this
2023 * device or run out of types.
2024 */
2025 for (i = 0; capture_source_types[i].create_op != NULL; i++) {
2026 is_theirs = 0;
2027 p = capture_source_types[i].create_op(device_str, errbuf,
2028 &is_theirs);
2029 if (is_theirs) {
2030 /*
2031 * The device name refers to a device of the
2032 * type in question; either it succeeded,
2033 * in which case p refers to a pcap_t to
2034 * later activate for the device, or it
2035 * failed, in which case p is null and we
2036 * should return that to report the failure
2037 * to create.
2038 */
2039 if (p == NULL) {
2040 /*
2041 * We assume the caller filled in errbuf.
2042 */
2043 free(device_str);
2044 return (NULL);
2045 }
2046 p->opt.device = device_str;
2047 return (p);
2048 }
2049 }
2050
2051 /*
2052 * OK, try it as a regular network interface.
2053 */
2054 p = pcap_create_interface(device_str, errbuf);
2055 if (p == NULL) {
2056 /*
2057 * We assume the caller filled in errbuf.
2058 */
2059 free(device_str);
2060 return (NULL);
2061 }
2062 p->opt.device = device_str;
2063 return (p);
2064 }
2065
2066 /*
2067 * Set nonblocking mode on an unactivated pcap_t; this sets a flag
2068 * checked by pcap_activate(), which sets the mode after calling
2069 * the activate routine.
2070 */
2071 static int
2072 pcap_setnonblock_unactivated(pcap_t *p, int nonblock)
2073 {
2074 p->opt.nonblock = nonblock;
2075 return (0);
2076 }
2077
2078 static void
2079 initialize_ops(pcap_t *p)
2080 {
2081 /*
2082 * Set operation pointers for operations that only work on
2083 * an activated pcap_t to point to a routine that returns
2084 * a "this isn't activated" error.
2085 */
2086 p->read_op = (read_op_t)pcap_not_initialized;
2087 p->inject_op = (inject_op_t)pcap_not_initialized;
2088 p->setfilter_op = (setfilter_op_t)pcap_not_initialized;
2089 p->setdirection_op = (setdirection_op_t)pcap_not_initialized;
2090 p->set_datalink_op = (set_datalink_op_t)pcap_not_initialized;
2091 p->getnonblock_op = (getnonblock_op_t)pcap_not_initialized;
2092 p->stats_op = (stats_op_t)pcap_not_initialized;
2093 #ifdef _WIN32
2094 p->stats_ex_op = (stats_ex_op_t)pcap_not_initialized_ptr;
2095 p->setbuff_op = (setbuff_op_t)pcap_not_initialized;
2096 p->setmode_op = (setmode_op_t)pcap_not_initialized;
2097 p->setmintocopy_op = (setmintocopy_op_t)pcap_not_initialized;
2098 p->getevent_op = pcap_getevent_not_initialized;
2099 p->oid_get_request_op = (oid_get_request_op_t)pcap_not_initialized;
2100 p->oid_set_request_op = (oid_set_request_op_t)pcap_not_initialized;
2101 p->sendqueue_transmit_op = pcap_sendqueue_transmit_not_initialized;
2102 p->setuserbuffer_op = (setuserbuffer_op_t)pcap_not_initialized;
2103 p->live_dump_op = (live_dump_op_t)pcap_not_initialized;
2104 p->live_dump_ended_op = (live_dump_ended_op_t)pcap_not_initialized;
2105 p->get_airpcap_handle_op = pcap_get_airpcap_handle_not_initialized;
2106 #endif
2107
2108 /*
2109 * Default cleanup operation - implementations can override
2110 * this, but should call pcap_cleanup_live_common() after
2111 * doing their own additional cleanup.
2112 */
2113 p->cleanup_op = pcap_cleanup_live_common;
2114
2115 /*
2116 * In most cases, the standard one-shot callback can
2117 * be used for pcap_next()/pcap_next_ex().
2118 */
2119 p->oneshot_callback = pcap_oneshot;
2120
2121 /*
2122 * Default breakloop operation - implementations can override
2123 * this, but should call pcap_breakloop_common() before doing
2124 * their own logic.
2125 */
2126 p->breakloop_op = pcap_breakloop_common;
2127 }
2128
2129 static pcap_t *
2130 pcap_alloc_pcap_t(char *ebuf, size_t size)
2131 {
2132 char *chunk;
2133 pcap_t *p;
2134
2135 /*
2136 * Allocate a chunk of memory big enough for a pcap_t
2137 * plus a structure following it of size "size". The
2138 * structure following it is a private data structure
2139 * for the routines that handle this pcap_t.
2140 *
2141 * The structure following it must be aligned on
2142 * the appropriate alignment boundary for this platform.
2143 * We align on an 8-byte boundary as that's probably what
2144 * at least some platforms do, even with 32-bit integers,
2145 * and because we can't be sure that some values won't
2146 * require 8-byte alignment even on platforms with 32-bit
2147 * integers.
2148 */
2149 #define PCAP_T_ALIGNED_SIZE ((sizeof(pcap_t) + 7) & ~0x7)
2150 chunk = malloc(PCAP_T_ALIGNED_SIZE + size);
2151 if (chunk == NULL) {
2152 pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
2153 errno, "malloc");
2154 return (NULL);
2155 }
2156 memset(chunk, 0, PCAP_T_ALIGNED_SIZE + size);
2157
2158 /*
2159 * Get a pointer to the pcap_t at the beginning.
2160 */
2161 p = (pcap_t *)chunk;
2162
2163 #ifdef _WIN32
2164 p->handle = INVALID_HANDLE_VALUE; /* not opened yet */
2165 #else /* _WIN32 */
2166 p->fd = -1; /* not opened yet */
2167 #ifndef MSDOS
2168 p->selectable_fd = -1;
2169 p->required_select_timeout = NULL;
2170 #endif /* MSDOS */
2171 #endif /* _WIN32 */
2172
2173 if (size == 0) {
2174 /* No private data was requested. */
2175 p->priv = NULL;
2176 } else {
2177 /*
2178 * Set the pointer to the private data; that's the structure
2179 * of size "size" following the pcap_t.
2180 */
2181 p->priv = (void *)(chunk + PCAP_T_ALIGNED_SIZE);
2182 }
2183
2184 return (p);
2185 }
2186
2187 pcap_t *
2188 pcap_create_common(char *ebuf, size_t size)
2189 {
2190 pcap_t *p;
2191
2192 p = pcap_alloc_pcap_t(ebuf, size);
2193 if (p == NULL)
2194 return (NULL);
2195
2196 /*
2197 * Default to "can't set rfmon mode"; if it's supported by
2198 * a platform, the create routine that called us can set
2199 * the op to its routine to check whether a particular
2200 * device supports it.
2201 */
2202 p->can_set_rfmon_op = pcap_cant_set_rfmon;
2203
2204 /*
2205 * If pcap_setnonblock() is called on a not-yet-activated
2206 * pcap_t, default to setting a flag and turning
2207 * on non-blocking mode when activated.
2208 */
2209 p->setnonblock_op = pcap_setnonblock_unactivated;
2210
2211 initialize_ops(p);
2212
2213 /* put in some defaults*/
2214 p->snapshot = 0; /* max packet size unspecified */
2215 p->opt.timeout = 0; /* no timeout specified */
2216 p->opt.buffer_size = 0; /* use the platform's default */
2217 p->opt.promisc = 0;
2218 p->opt.rfmon = 0;
2219 p->opt.immediate = 0;
2220 p->opt.tstamp_type = -1; /* default to not setting time stamp type */
2221 p->opt.tstamp_precision = PCAP_TSTAMP_PRECISION_MICRO;
2222 /*
2223 * Platform-dependent options.
2224 */
2225 #ifdef __linux__
2226 p->opt.protocol = 0;
2227 #endif
2228 #ifdef _WIN32
2229 p->opt.nocapture_local = 0;
2230 #endif
2231
2232 /*
2233 * Start out with no BPF code generation flags set.
2234 */
2235 p->bpf_codegen_flags = 0;
2236
2237 return (p);
2238 }
2239
2240 int
2241 pcap_check_activated(pcap_t *p)
2242 {
2243 if (p->activated) {
2244 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "can't perform "
2245 " operation on activated capture");
2246 return (-1);
2247 }
2248 return (0);
2249 }
2250
2251 int
2252 pcap_set_snaplen(pcap_t *p, int snaplen)
2253 {
2254 if (pcap_check_activated(p))
2255 return (PCAP_ERROR_ACTIVATED);
2256 p->snapshot = snaplen;
2257 return (0);
2258 }
2259
2260 int
2261 pcap_set_promisc(pcap_t *p, int promisc)
2262 {
2263 if (pcap_check_activated(p))
2264 return (PCAP_ERROR_ACTIVATED);
2265 p->opt.promisc = promisc;
2266 return (0);
2267 }
2268
2269 int
2270 pcap_set_rfmon(pcap_t *p, int rfmon)
2271 {
2272 if (pcap_check_activated(p))
2273 return (PCAP_ERROR_ACTIVATED);
2274 p->opt.rfmon = rfmon;
2275 return (0);
2276 }
2277
2278 int
2279 pcap_set_timeout(pcap_t *p, int timeout_ms)
2280 {
2281 if (pcap_check_activated(p))
2282 return (PCAP_ERROR_ACTIVATED);
2283 p->opt.timeout = timeout_ms;
2284 return (0);
2285 }
2286
2287 int
2288 pcap_set_tstamp_type(pcap_t *p, int tstamp_type)
2289 {
2290 int i;
2291
2292 if (pcap_check_activated(p))
2293 return (PCAP_ERROR_ACTIVATED);
2294
2295 /*
2296 * The argument should have been u_int, but that's too late
2297 * to change now - it's an API.
2298 */
2299 if (tstamp_type < 0)
2300 return (PCAP_WARNING_TSTAMP_TYPE_NOTSUP);
2301
2302 /*
2303 * If p->tstamp_type_count is 0, we only support PCAP_TSTAMP_HOST;
2304 * the default time stamp type is PCAP_TSTAMP_HOST.
2305 */
2306 if (p->tstamp_type_count == 0) {
2307 if (tstamp_type == PCAP_TSTAMP_HOST) {
2308 p->opt.tstamp_type = tstamp_type;
2309 return (0);
2310 }
2311 } else {
2312 /*
2313 * Check whether we claim to support this type of time stamp.
2314 */
2315 for (i = 0; i < p->tstamp_type_count; i++) {
2316 if (p->tstamp_type_list[i] == (u_int)tstamp_type) {
2317 /*
2318 * Yes.
2319 */
2320 p->opt.tstamp_type = tstamp_type;
2321 return (0);
2322 }
2323 }
2324 }
2325
2326 /*
2327 * We don't support this type of time stamp.
2328 */
2329 return (PCAP_WARNING_TSTAMP_TYPE_NOTSUP);
2330 }
2331
2332 int
2333 pcap_set_immediate_mode(pcap_t *p, int immediate)
2334 {
2335 if (pcap_check_activated(p))
2336 return (PCAP_ERROR_ACTIVATED);
2337 p->opt.immediate = immediate;
2338 return (0);
2339 }
2340
2341 int
2342 pcap_set_buffer_size(pcap_t *p, int buffer_size)
2343 {
2344 if (pcap_check_activated(p))
2345 return (PCAP_ERROR_ACTIVATED);
2346 if (buffer_size <= 0) {
2347 /*
2348 * Silently ignore invalid values.
2349 */
2350 return (0);
2351 }
2352 p->opt.buffer_size = buffer_size;
2353 return (0);
2354 }
2355
2356 int
2357 pcap_set_tstamp_precision(pcap_t *p, int tstamp_precision)
2358 {
2359 int i;
2360
2361 if (pcap_check_activated(p))
2362 return (PCAP_ERROR_ACTIVATED);
2363
2364 /*
2365 * The argument should have been u_int, but that's too late
2366 * to change now - it's an API.
2367 */
2368 if (tstamp_precision < 0)
2369 return (PCAP_ERROR_TSTAMP_PRECISION_NOTSUP);
2370
2371 /*
2372 * If p->tstamp_precision_count is 0, we only support setting
2373 * the time stamp precision to microsecond precision; every
2374 * pcap module *MUST* support microsecond precision, even if
2375 * it does so by converting the native precision to
2376 * microseconds.
2377 */
2378 if (p->tstamp_precision_count == 0) {
2379 if (tstamp_precision == PCAP_TSTAMP_PRECISION_MICRO) {
2380 p->opt.tstamp_precision = tstamp_precision;
2381 return (0);
2382 }
2383 } else {
2384 /*
2385 * Check whether we claim to support this precision of
2386 * time stamp.
2387 */
2388 for (i = 0; i < p->tstamp_precision_count; i++) {
2389 if (p->tstamp_precision_list[i] == (u_int)tstamp_precision) {
2390 /*
2391 * Yes.
2392 */
2393 p->opt.tstamp_precision = tstamp_precision;
2394 return (0);
2395 }
2396 }
2397 }
2398
2399 /*
2400 * We don't support this time stamp precision.
2401 */
2402 return (PCAP_ERROR_TSTAMP_PRECISION_NOTSUP);
2403 }
2404
2405 int
2406 pcap_get_tstamp_precision(pcap_t *p)
2407 {
2408 return (p->opt.tstamp_precision);
2409 }
2410
2411 int
2412 pcap_activate(pcap_t *p)
2413 {
2414 int status;
2415
2416 /*
2417 * Catch attempts to re-activate an already-activated
2418 * pcap_t; this should, for example, catch code that
2419 * calls pcap_open_live() followed by pcap_activate(),
2420 * as some code that showed up in a Stack Exchange
2421 * question did.
2422 */
2423 if (pcap_check_activated(p))
2424 return (PCAP_ERROR_ACTIVATED);
2425 status = p->activate_op(p);
2426 if (status >= 0) {
2427 /*
2428 * If somebody requested non-blocking mode before
2429 * calling pcap_activate(), turn it on now.
2430 */
2431 if (p->opt.nonblock) {
2432 status = p->setnonblock_op(p, 1);
2433 if (status < 0) {
2434 /*
2435 * Failed. Undo everything done by
2436 * the activate operation.
2437 */
2438 p->cleanup_op(p);
2439 initialize_ops(p);
2440 return (status);
2441 }
2442 }
2443 p->activated = 1;
2444 } else {
2445 if (p->errbuf[0] == '\0') {
2446 /*
2447 * No error message supplied by the activate routine;
2448 * for the benefit of programs that don't specially
2449 * handle errors other than PCAP_ERROR, return the
2450 * error message corresponding to the status.
2451 */
2452 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "%s",
2453 pcap_statustostr(status));
2454 }
2455
2456 /*
2457 * Undo any operation pointer setting, etc. done by
2458 * the activate operation.
2459 */
2460 initialize_ops(p);
2461 }
2462 return (status);
2463 }
2464
2465 pcap_t *
2466 pcap_open_live(const char *device, int snaplen, int promisc, int to_ms, char *errbuf)
2467 {
2468 pcap_t *p;
2469 int status;
2470 #ifdef ENABLE_REMOTE
2471 char host[PCAP_BUF_SIZE + 1];
2472 char port[PCAP_BUF_SIZE + 1];
2473 char name[PCAP_BUF_SIZE + 1];
2474 int srctype;
2475
2476 /*
2477 * Retrofit - we have to make older applications compatible with
2478 * remote capture.
2479 * So we're calling pcap_open_remote() from here; this is a very
2480 * dirty hack.
2481 * Obviously, we cannot exploit all the new features; for instance,
2482 * we cannot send authentication, we cannot use a UDP data connection,
2483 * and so on.
2484 */
2485 if (pcap_parsesrcstr(device, &srctype, host, port, name, errbuf))
2486 return (NULL);
2487
2488 if (srctype == PCAP_SRC_IFREMOTE) {
2489 /*
2490 * Although we already have host, port and iface, we prefer
2491 * to pass only 'device' to pcap_open_rpcap(), so that it has
2492 * to call pcap_parsesrcstr() again.
2493 * This is less optimized, but much clearer.
2494 */
2495 return (pcap_open_rpcap(device, snaplen,
2496 promisc ? PCAP_OPENFLAG_PROMISCUOUS : 0, to_ms,
2497 NULL, errbuf));
2498 }
2499 if (srctype == PCAP_SRC_FILE) {
2500 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "unknown URL scheme \"file\"");
2501 return (NULL);
2502 }
2503 if (srctype == PCAP_SRC_IFLOCAL) {
2504 /*
2505 * If it starts with rpcap://, that refers to a local device
2506 * (no host part in the URL). Remove the rpcap://, and
2507 * fall through to the regular open path.
2508 */
2509 if (strncmp(device, PCAP_SRC_IF_STRING, strlen(PCAP_SRC_IF_STRING)) == 0) {
2510 size_t len = strlen(device) - strlen(PCAP_SRC_IF_STRING) + 1;
2511
2512 if (len > 0)
2513 device += strlen(PCAP_SRC_IF_STRING);
2514 }
2515 }
2516 #endif /* ENABLE_REMOTE */
2517
2518 p = pcap_create(device, errbuf);
2519 if (p == NULL)
2520 return (NULL);
2521 status = pcap_set_snaplen(p, snaplen);
2522 if (status < 0)
2523 goto fail;
2524 status = pcap_set_promisc(p, promisc);
2525 if (status < 0)
2526 goto fail;
2527 status = pcap_set_timeout(p, to_ms);
2528 if (status < 0)
2529 goto fail;
2530 /*
2531 * Mark this as opened with pcap_open_live(), so that, for
2532 * example, we show the full list of DLT_ values, rather
2533 * than just the ones that are compatible with capturing
2534 * when not in monitor mode. That allows existing applications
2535 * to work the way they used to work, but allows new applications
2536 * that know about the new open API to, for example, find out the
2537 * DLT_ values that they can select without changing whether
2538 * the adapter is in monitor mode or not.
2539 */
2540 p->oldstyle = 1;
2541 status = pcap_activate(p);
2542 if (status < 0)
2543 goto fail;
2544 return (p);
2545 fail:
2546 if (status == PCAP_ERROR)
2547 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %.*s", device,
2548 PCAP_ERRBUF_SIZE - 3, p->errbuf);
2549 else if (status == PCAP_ERROR_NO_SUCH_DEVICE ||
2550 status == PCAP_ERROR_PERM_DENIED ||
2551 status == PCAP_ERROR_PROMISC_PERM_DENIED)
2552 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s (%.*s)", device,
2553 pcap_statustostr(status), PCAP_ERRBUF_SIZE - 6, p->errbuf);
2554 else
2555 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s", device,
2556 pcap_statustostr(status));
2557 pcap_close(p);
2558 return (NULL);
2559 }
2560
2561 pcap_t *
2562 pcap_open_offline_common(char *ebuf, size_t size)
2563 {
2564 pcap_t *p;
2565
2566 p = pcap_alloc_pcap_t(ebuf, size);
2567 if (p == NULL)
2568 return (NULL);
2569
2570 p->opt.tstamp_precision = PCAP_TSTAMP_PRECISION_MICRO;
2571
2572 return (p);
2573 }
2574
2575 int
2576 pcap_dispatch(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
2577 {
2578 return (p->read_op(p, cnt, callback, user));
2579 }
2580
2581 int
2582 pcap_loop(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
2583 {
2584 register int n;
2585
2586 for (;;) {
2587 if (p->rfile != NULL) {
2588 /*
2589 * 0 means EOF, so don't loop if we get 0.
2590 */
2591 n = pcap_offline_read(p, cnt, callback, user);
2592 } else {
2593 /*
2594 * XXX keep reading until we get something
2595 * (or an error occurs)
2596 */
2597 do {
2598 n = p->read_op(p, cnt, callback, user);
2599 } while (n == 0);
2600 }
2601 if (n <= 0)
2602 return (n);
2603 if (!PACKET_COUNT_IS_UNLIMITED(cnt)) {
2604 cnt -= n;
2605 if (cnt <= 0)
2606 return (0);
2607 }
2608 }
2609 }
2610
2611 /*
2612 * Force the loop in "pcap_read()" or "pcap_read_offline()" to terminate.
2613 */
2614 void
2615 pcap_breakloop(pcap_t *p)
2616 {
2617 p->breakloop_op(p);
2618 }
2619
2620 int
2621 pcap_datalink(pcap_t *p)
2622 {
2623 if (!p->activated)
2624 return (PCAP_ERROR_NOT_ACTIVATED);
2625 return (p->linktype);
2626 }
2627
2628 int
2629 pcap_datalink_ext(pcap_t *p)
2630 {
2631 if (!p->activated)
2632 return (PCAP_ERROR_NOT_ACTIVATED);
2633 return (p->linktype_ext);
2634 }
2635
2636 int
2637 pcap_list_datalinks(pcap_t *p, int **dlt_buffer)
2638 {
2639 if (!p->activated)
2640 return (PCAP_ERROR_NOT_ACTIVATED);
2641 if (p->dlt_count == 0) {
2642 /*
2643 * We couldn't fetch the list of DLTs, which means
2644 * this platform doesn't support changing the
2645 * DLT for an interface. Return a list of DLTs
2646 * containing only the DLT this device supports.
2647 */
2648 *dlt_buffer = (int*)malloc(sizeof(**dlt_buffer));
2649 if (*dlt_buffer == NULL) {
2650 pcap_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf),
2651 errno, "malloc");
2652 return (PCAP_ERROR);
2653 }
2654 **dlt_buffer = p->linktype;
2655 return (1);
2656 } else {
2657 *dlt_buffer = (int*)calloc(sizeof(**dlt_buffer), p->dlt_count);
2658 if (*dlt_buffer == NULL) {
2659 pcap_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf),
2660 errno, "malloc");
2661 return (PCAP_ERROR);
2662 }
2663 (void)memcpy(*dlt_buffer, p->dlt_list,
2664 sizeof(**dlt_buffer) * p->dlt_count);
2665 return (p->dlt_count);
2666 }
2667 }
2668
2669 /*
2670 * In Windows, you might have a library built with one version of the
2671 * C runtime library and an application built with another version of
2672 * the C runtime library, which means that the library might use one
2673 * version of malloc() and free() and the application might use another
2674 * version of malloc() and free(). If so, that means something
2675 * allocated by the library cannot be freed by the application, so we
2676 * need to have a pcap_free_datalinks() routine to free up the list
2677 * allocated by pcap_list_datalinks(), even though it's just a wrapper
2678 * around free().
2679 */
2680 void
2681 pcap_free_datalinks(int *dlt_list)
2682 {
2683 free(dlt_list);
2684 }
2685
2686 int
2687 pcap_set_datalink(pcap_t *p, int dlt)
2688 {
2689 int i;
2690 const char *dlt_name;
2691
2692 if (dlt < 0)
2693 goto unsupported;
2694
2695 if (p->dlt_count == 0 || p->set_datalink_op == NULL) {
2696 /*
2697 * We couldn't fetch the list of DLTs, or we don't
2698 * have a "set datalink" operation, which means
2699 * this platform doesn't support changing the
2700 * DLT for an interface. Check whether the new
2701 * DLT is the one this interface supports.
2702 */
2703 if (p->linktype != dlt)
2704 goto unsupported;
2705
2706 /*
2707 * It is, so there's nothing we need to do here.
2708 */
2709 return (0);
2710 }
2711 for (i = 0; i < p->dlt_count; i++)
2712 if (p->dlt_list[i] == (u_int)dlt)
2713 break;
2714 if (i >= p->dlt_count)
2715 goto unsupported;
2716 if (p->dlt_count == 2 && p->dlt_list[0] == DLT_EN10MB &&
2717 dlt == DLT_DOCSIS) {
2718 /*
2719 * This is presumably an Ethernet device, as the first
2720 * link-layer type it offers is DLT_EN10MB, and the only
2721 * other type it offers is DLT_DOCSIS. That means that
2722 * we can't tell the driver to supply DOCSIS link-layer
2723 * headers - we're just pretending that's what we're
2724 * getting, as, presumably, we're capturing on a dedicated
2725 * link to a Cisco Cable Modem Termination System, and
2726 * it's putting raw DOCSIS frames on the wire inside low-level
2727 * Ethernet framing.
2728 */
2729 p->linktype = dlt;
2730 return (0);
2731 }
2732 if (p->set_datalink_op(p, dlt) == -1)
2733 return (-1);
2734 p->linktype = dlt;
2735 return (0);
2736
2737 unsupported:
2738 dlt_name = pcap_datalink_val_to_name(dlt);
2739 if (dlt_name != NULL) {
2740 (void) pcap_snprintf(p->errbuf, sizeof(p->errbuf),
2741 "%s is not one of the DLTs supported by this device",
2742 dlt_name);
2743 } else {
2744 (void) pcap_snprintf(p->errbuf, sizeof(p->errbuf),
2745 "DLT %d is not one of the DLTs supported by this device",
2746 dlt);
2747 }
2748 return (-1);
2749 }
2750
2751 /*
2752 * This array is designed for mapping upper and lower case letter
2753 * together for a case independent comparison. The mappings are
2754 * based upon ascii character sequences.
2755 */
2756 static const u_char charmap[] = {
2757 (u_char)'\000', (u_char)'\001', (u_char)'\002', (u_char)'\003',
2758 (u_char)'\004', (u_char)'\005', (u_char)'\006', (u_char)'\007',
2759 (u_char)'\010', (u_char)'\011', (u_char)'\012', (u_char)'\013',
2760 (u_char)'\014', (u_char)'\015', (u_char)'\016', (u_char)'\017',
2761 (u_char)'\020', (u_char)'\021', (u_char)'\022', (u_char)'\023',
2762 (u_char)'\024', (u_char)'\025', (u_char)'\026', (u_char)'\027',
2763 (u_char)'\030', (u_char)'\031', (u_char)'\032', (u_char)'\033',
2764 (u_char)'\034', (u_char)'\035', (u_char)'\036', (u_char)'\037',
2765 (u_char)'\040', (u_char)'\041', (u_char)'\042', (u_char)'\043',
2766 (u_char)'\044', (u_char)'\045', (u_char)'\046', (u_char)'\047',
2767 (u_char)'\050', (u_char)'\051', (u_char)'\052', (u_char)'\053',
2768 (u_char)'\054', (u_char)'\055', (u_char)'\056', (u_char)'\057',
2769 (u_char)'\060', (u_char)'\061', (u_char)'\062', (u_char)'\063',
2770 (u_char)'\064', (u_char)'\065', (u_char)'\066', (u_char)'\067',
2771 (u_char)'\070', (u_char)'\071', (u_char)'\072', (u_char)'\073',
2772 (u_char)'\074', (u_char)'\075', (u_char)'\076', (u_char)'\077',
2773 (u_char)'\100', (u_char)'\141', (u_char)'\142', (u_char)'\143',
2774 (u_char)'\144', (u_char)'\145', (u_char)'\146', (u_char)'\147',
2775 (u_char)'\150', (u_char)'\151', (u_char)'\152', (u_char)'\153',
2776 (u_char)'\154', (u_char)'\155', (u_char)'\156', (u_char)'\157',
2777 (u_char)'\160', (u_char)'\161', (u_char)'\162', (u_char)'\163',
2778 (u_char)'\164', (u_char)'\165', (u_char)'\166', (u_char)'\167',
2779 (u_char)'\170', (u_char)'\171', (u_char)'\172', (u_char)'\133',
2780 (u_char)'\134', (u_char)'\135', (u_char)'\136', (u_char)'\137',
2781 (u_char)'\140', (u_char)'\141', (u_char)'\142', (u_char)'\143',
2782 (u_char)'\144', (u_char)'\145', (u_char)'\146', (u_char)'\147',
2783 (u_char)'\150', (u_char)'\151', (u_char)'\152', (u_char)'\153',
2784 (u_char)'\154', (u_char)'\155', (u_char)'\156', (u_char)'\157',
2785 (u_char)'\160', (u_char)'\161', (u_char)'\162', (u_char)'\163',
2786 (u_char)'\164', (u_char)'\165', (u_char)'\166', (u_char)'\167',
2787 (u_char)'\170', (u_char)'\171', (u_char)'\172', (u_char)'\173',
2788 (u_char)'\174', (u_char)'\175', (u_char)'\176', (u_char)'\177',
2789 (u_char)'\200', (u_char)'\201', (u_char)'\202', (u_char)'\203',
2790 (u_char)'\204', (u_char)'\205', (u_char)'\206', (u_char)'\207',
2791 (u_char)'\210', (u_char)'\211', (u_char)'\212', (u_char)'\213',
2792 (u_char)'\214', (u_char)'\215', (u_char)'\216', (u_char)'\217',
2793 (u_char)'\220', (u_char)'\221', (u_char)'\222', (u_char)'\223',
2794 (u_char)'\224', (u_char)'\225', (u_char)'\226', (u_char)'\227',
2795 (u_char)'\230', (u_char)'\231', (u_char)'\232', (u_char)'\233',
2796 (u_char)'\234', (u_char)'\235', (u_char)'\236', (u_char)'\237',
2797 (u_char)'\240', (u_char)'\241', (u_char)'\242', (u_char)'\243',
2798 (u_char)'\244', (u_char)'\245', (u_char)'\246', (u_char)'\247',
2799 (u_char)'\250', (u_char)'\251', (u_char)'\252', (u_char)'\253',
2800 (u_char)'\254', (u_char)'\255', (u_char)'\256', (u_char)'\257',
2801 (u_char)'\260', (u_char)'\261', (u_char)'\262', (u_char)'\263',
2802 (u_char)'\264', (u_char)'\265', (u_char)'\266', (u_char)'\267',
2803 (u_char)'\270', (u_char)'\271', (u_char)'\272', (u_char)'\273',
2804 (u_char)'\274', (u_char)'\275', (u_char)'\276', (u_char)'\277',
2805 (u_char)'\300', (u_char)'\341', (u_char)'\342', (u_char)'\343',
2806 (u_char)'\344', (u_char)'\345', (u_char)'\346', (u_char)'\347',
2807 (u_char)'\350', (u_char)'\351', (u_char)'\352', (u_char)'\353',
2808 (u_char)'\354', (u_char)'\355', (u_char)'\356', (u_char)'\357',
2809 (u_char)'\360', (u_char)'\361', (u_char)'\362', (u_char)'\363',
2810 (u_char)'\364', (u_char)'\365', (u_char)'\366', (u_char)'\367',
2811 (u_char)'\370', (u_char)'\371', (u_char)'\372', (u_char)'\333',
2812 (u_char)'\334', (u_char)'\335', (u_char)'\336', (u_char)'\337',
2813 (u_char)'\340', (u_char)'\341', (u_char)'\342', (u_char)'\343',
2814 (u_char)'\344', (u_char)'\345', (u_char)'\346', (u_char)'\347',
2815 (u_char)'\350', (u_char)'\351', (u_char)'\352', (u_char)'\353',
2816 (u_char)'\354', (u_char)'\355', (u_char)'\356', (u_char)'\357',
2817 (u_char)'\360', (u_char)'\361', (u_char)'\362', (u_char)'\363',
2818 (u_char)'\364', (u_char)'\365', (u_char)'\366', (u_char)'\367',
2819 (u_char)'\370', (u_char)'\371', (u_char)'\372', (u_char)'\373',
2820 (u_char)'\374', (u_char)'\375', (u_char)'\376', (u_char)'\377',
2821 };
2822
2823 int
2824 pcap_strcasecmp(const char *s1, const char *s2)
2825 {
2826 register const u_char *cm = charmap,
2827 *us1 = (const u_char *)s1,
2828 *us2 = (const u_char *)s2;
2829
2830 while (cm[*us1] == cm[*us2++])
2831 if (*us1++ == '\0')
2832 return(0);
2833 return (cm[*us1] - cm[*--us2]);
2834 }
2835
2836 struct dlt_choice {
2837 const char *name;
2838 const char *description;
2839 int dlt;
2840 };
2841
2842 #define DLT_CHOICE(code, description) { #code, description, DLT_ ## code }
2843 #define DLT_CHOICE_SENTINEL { NULL, NULL, 0 }
2844
2845 static struct dlt_choice dlt_choices[] = {
2846 DLT_CHOICE(NULL, "BSD loopback"),
2847 DLT_CHOICE(EN10MB, "Ethernet"),
2848 DLT_CHOICE(IEEE802, "Token ring"),
2849 DLT_CHOICE(ARCNET, "BSD ARCNET"),
2850 DLT_CHOICE(SLIP, "SLIP"),
2851 DLT_CHOICE(PPP, "PPP"),
2852 DLT_CHOICE(FDDI, "FDDI"),
2853 DLT_CHOICE(ATM_RFC1483, "RFC 1483 LLC-encapsulated ATM"),
2854 DLT_CHOICE(RAW, "Raw IP"),
2855 DLT_CHOICE(SLIP_BSDOS, "BSD/OS SLIP"),
2856 DLT_CHOICE(PPP_BSDOS, "BSD/OS PPP"),
2857 DLT_CHOICE(ATM_CLIP, "Linux Classical IP-over-ATM"),
2858 DLT_CHOICE(PPP_SERIAL, "PPP over serial"),
2859 DLT_CHOICE(PPP_ETHER, "PPPoE"),
2860 DLT_CHOICE(SYMANTEC_FIREWALL, "Symantec Firewall"),
2861 DLT_CHOICE(C_HDLC, "Cisco HDLC"),
2862 DLT_CHOICE(IEEE802_11, "802.11"),
2863 DLT_CHOICE(FRELAY, "Frame Relay"),
2864 DLT_CHOICE(LOOP, "OpenBSD loopback"),
2865 DLT_CHOICE(ENC, "OpenBSD encapsulated IP"),
2866 DLT_CHOICE(LINUX_SLL, "Linux cooked v1"),
2867 DLT_CHOICE(LTALK, "Localtalk"),
2868 DLT_CHOICE(PFLOG, "OpenBSD pflog file"),
2869 DLT_CHOICE(PFSYNC, "Packet filter state syncing"),
2870 DLT_CHOICE(PRISM_HEADER, "802.11 plus Prism header"),
2871 DLT_CHOICE(IP_OVER_FC, "RFC 2625 IP-over-Fibre Channel"),
2872 DLT_CHOICE(SUNATM, "Sun raw ATM"),
2873 DLT_CHOICE(IEEE802_11_RADIO, "802.11 plus radiotap header"),
2874 DLT_CHOICE(ARCNET_LINUX, "Linux ARCNET"),
2875 DLT_CHOICE(JUNIPER_MLPPP, "Juniper Multi-Link PPP"),
2876 DLT_CHOICE(JUNIPER_MLFR, "Juniper Multi-Link Frame Relay"),
2877 DLT_CHOICE(JUNIPER_ES, "Juniper Encryption Services PIC"),
2878 DLT_CHOICE(JUNIPER_GGSN, "Juniper GGSN PIC"),
2879 DLT_CHOICE(JUNIPER_MFR, "Juniper FRF.16 Frame Relay"),
2880 DLT_CHOICE(JUNIPER_ATM2, "Juniper ATM2 PIC"),
2881 DLT_CHOICE(JUNIPER_SERVICES, "Juniper Advanced Services PIC"),
2882 DLT_CHOICE(JUNIPER_ATM1, "Juniper ATM1 PIC"),
2883 DLT_CHOICE(APPLE_IP_OVER_IEEE1394, "Apple IP-over-IEEE 1394"),
2884 DLT_CHOICE(MTP2_WITH_PHDR, "SS7 MTP2 with Pseudo-header"),
2885 DLT_CHOICE(MTP2, "SS7 MTP2"),
2886 DLT_CHOICE(MTP3, "SS7 MTP3"),
2887 DLT_CHOICE(SCCP, "SS7 SCCP"),
2888 DLT_CHOICE(DOCSIS, "DOCSIS"),
2889 DLT_CHOICE(LINUX_IRDA, "Linux IrDA"),
2890 DLT_CHOICE(IEEE802_11_RADIO_AVS, "802.11 plus AVS radio information header"),
2891 DLT_CHOICE(JUNIPER_MONITOR, "Juniper Passive Monitor PIC"),
2892 DLT_CHOICE(BACNET_MS_TP, "BACnet MS/TP"),
2893 DLT_CHOICE(PPP_PPPD, "PPP for pppd, with direction flag"),
2894 DLT_CHOICE(JUNIPER_PPPOE, "Juniper PPPoE"),
2895 DLT_CHOICE(JUNIPER_PPPOE_ATM, "Juniper PPPoE/ATM"),
2896 DLT_CHOICE(GPRS_LLC, "GPRS LLC"),
2897 DLT_CHOICE(GPF_T, "GPF-T"),
2898 DLT_CHOICE(GPF_F, "GPF-F"),
2899 DLT_CHOICE(JUNIPER_PIC_PEER, "Juniper PIC Peer"),
2900 DLT_CHOICE(ERF_ETH, "Ethernet with Endace ERF header"),
2901 DLT_CHOICE(ERF_POS, "Packet-over-SONET with Endace ERF header"),
2902 DLT_CHOICE(LINUX_LAPD, "Linux vISDN LAPD"),
2903 DLT_CHOICE(JUNIPER_ETHER, "Juniper Ethernet"),
2904 DLT_CHOICE(JUNIPER_PPP, "Juniper PPP"),
2905 DLT_CHOICE(JUNIPER_FRELAY, "Juniper Frame Relay"),
2906 DLT_CHOICE(JUNIPER_CHDLC, "Juniper C-HDLC"),
2907 DLT_CHOICE(MFR, "FRF.16 Frame Relay"),
2908 DLT_CHOICE(JUNIPER_VP, "Juniper Voice PIC"),
2909 DLT_CHOICE(A429, "Arinc 429"),
2910 DLT_CHOICE(A653_ICM, "Arinc 653 Interpartition Communication"),
2911 DLT_CHOICE(USB_FREEBSD, "USB with FreeBSD header"),
2912 DLT_CHOICE(BLUETOOTH_HCI_H4, "Bluetooth HCI UART transport layer"),
2913 DLT_CHOICE(IEEE802_16_MAC_CPS, "IEEE 802.16 MAC Common Part Sublayer"),
2914 DLT_CHOICE(USB_LINUX, "USB with Linux header"),
2915 DLT_CHOICE(CAN20B, "Controller Area Network (CAN) v. 2.0B"),
2916 DLT_CHOICE(IEEE802_15_4_LINUX, "IEEE 802.15.4 with Linux padding"),
2917 DLT_CHOICE(PPI, "Per-Packet Information"),
2918 DLT_CHOICE(IEEE802_16_MAC_CPS_RADIO, "IEEE 802.16 MAC Common Part Sublayer plus radiotap header"),
2919 DLT_CHOICE(JUNIPER_ISM, "Juniper Integrated Service Module"),
2920 DLT_CHOICE(IEEE802_15_4, "IEEE 802.15.4 with FCS"),
2921 DLT_CHOICE(SITA, "SITA pseudo-header"),
2922 DLT_CHOICE(ERF, "Endace ERF header"),
2923 DLT_CHOICE(RAIF1, "Ethernet with u10 Networks pseudo-header"),
2924 DLT_CHOICE(IPMB, "IPMB"),
2925 DLT_CHOICE(JUNIPER_ST, "Juniper Secure Tunnel"),
2926 DLT_CHOICE(BLUETOOTH_HCI_H4_WITH_PHDR, "Bluetooth HCI UART transport layer plus pseudo-header"),
2927 DLT_CHOICE(AX25_KISS, "AX.25 with KISS header"),
2928 DLT_CHOICE(IEEE802_15_4_NONASK_PHY, "IEEE 802.15.4 with non-ASK PHY data"),
2929 DLT_CHOICE(MPLS, "MPLS with label as link-layer header"),
2930 DLT_CHOICE(LINUX_EVDEV, "Linux evdev events"),
2931 DLT_CHOICE(USB_LINUX_MMAPPED, "USB with padded Linux header"),
2932 DLT_CHOICE(DECT, "DECT"),
2933 DLT_CHOICE(AOS, "AOS Space Data Link protocol"),
2934 DLT_CHOICE(WIHART, "Wireless HART"),
2935 DLT_CHOICE(FC_2, "Fibre Channel FC-2"),
2936 DLT_CHOICE(FC_2_WITH_FRAME_DELIMS, "Fibre Channel FC-2 with frame delimiters"),
2937 DLT_CHOICE(IPNET, "Solaris ipnet"),
2938 DLT_CHOICE(CAN_SOCKETCAN, "CAN-bus with SocketCAN headers"),
2939 DLT_CHOICE(IPV4, "Raw IPv4"),
2940 DLT_CHOICE(IPV6, "Raw IPv6"),
2941 DLT_CHOICE(IEEE802_15_4_NOFCS, "IEEE 802.15.4 without FCS"),
2942 DLT_CHOICE(DBUS, "D-Bus"),
2943 DLT_CHOICE(JUNIPER_VS, "Juniper Virtual Server"),
2944 DLT_CHOICE(JUNIPER_SRX_E2E, "Juniper SRX E2E"),
2945 DLT_CHOICE(JUNIPER_FIBRECHANNEL, "Juniper Fibre Channel"),
2946 DLT_CHOICE(DVB_CI, "DVB-CI"),
2947 DLT_CHOICE(MUX27010, "MUX27010"),
2948 DLT_CHOICE(STANAG_5066_D_PDU, "STANAG 5066 D_PDUs"),
2949 DLT_CHOICE(JUNIPER_ATM_CEMIC, "Juniper ATM CEMIC"),
2950 DLT_CHOICE(NFLOG, "Linux netfilter log messages"),
2951 DLT_CHOICE(NETANALYZER, "Ethernet with Hilscher netANALYZER pseudo-header"),
2952 DLT_CHOICE(NETANALYZER_TRANSPARENT, "Ethernet with Hilscher netANALYZER pseudo-header and with preamble and SFD"),
2953 DLT_CHOICE(IPOIB, "RFC 4391 IP-over-Infiniband"),
2954 DLT_CHOICE(MPEG_2_TS, "MPEG-2 transport stream"),
2955 DLT_CHOICE(NG40, "ng40 protocol tester Iub/Iur"),
2956 DLT_CHOICE(NFC_LLCP, "NFC LLCP PDUs with pseudo-header"),
2957 DLT_CHOICE(INFINIBAND, "InfiniBand"),
2958 DLT_CHOICE(SCTP, "SCTP"),
2959 DLT_CHOICE(USBPCAP, "USB with USBPcap header"),
2960 DLT_CHOICE(RTAC_SERIAL, "Schweitzer Engineering Laboratories RTAC packets"),
2961 DLT_CHOICE(BLUETOOTH_LE_LL, "Bluetooth Low Energy air interface"),
2962 DLT_CHOICE(NETLINK, "Linux netlink"),
2963 DLT_CHOICE(BLUETOOTH_LINUX_MONITOR, "Bluetooth Linux Monitor"),
2964 DLT_CHOICE(BLUETOOTH_BREDR_BB, "Bluetooth Basic Rate/Enhanced Data Rate baseband packets"),
2965 DLT_CHOICE(BLUETOOTH_LE_LL_WITH_PHDR, "Bluetooth Low Energy air interface with pseudo-header"),
2966 DLT_CHOICE(PROFIBUS_DL, "PROFIBUS data link layer"),
2967 DLT_CHOICE(PKTAP, "Apple DLT_PKTAP"),
2968 DLT_CHOICE(EPON, "Ethernet with 802.3 Clause 65 EPON preamble"),
2969 DLT_CHOICE(IPMI_HPM_2, "IPMI trace packets"),
2970 DLT_CHOICE(ZWAVE_R1_R2, "Z-Wave RF profile R1 and R2 packets"),
2971 DLT_CHOICE(ZWAVE_R3, "Z-Wave RF profile R3 packets"),
2972 DLT_CHOICE(WATTSTOPPER_DLM, "WattStopper Digital Lighting Management (DLM) and Legrand Nitoo Open protocol"),
2973 DLT_CHOICE(ISO_14443, "ISO 14443 messages"),
2974 DLT_CHOICE(RDS, "IEC 62106 Radio Data System groups"),
2975 DLT_CHOICE(USB_DARWIN, "USB with Darwin header"),
2976 DLT_CHOICE(OPENFLOW, "OpenBSD DLT_OPENFLOW"),
2977 DLT_CHOICE(SDLC, "IBM SDLC frames"),
2978 DLT_CHOICE(TI_LLN_SNIFFER, "TI LLN sniffer frames"),
2979 DLT_CHOICE(VSOCK, "Linux vsock"),
2980 DLT_CHOICE(NORDIC_BLE, "Nordic Semiconductor Bluetooth LE sniffer frames"),
2981 DLT_CHOICE(DOCSIS31_XRA31, "Excentis XRA-31 DOCSIS 3.1 RF sniffer frames"),
2982 DLT_CHOICE(ETHERNET_MPACKET, "802.3br mPackets"),
2983 DLT_CHOICE(DISPLAYPORT_AUX, "DisplayPort AUX channel monitoring data"),
2984 DLT_CHOICE(LINUX_SLL2, "Linux cooked v2"),
2985 DLT_CHOICE_SENTINEL
2986 };
2987
2988 int
2989 pcap_datalink_name_to_val(const char *name)
2990 {
2991 int i;
2992
2993 for (i = 0; dlt_choices[i].name != NULL; i++) {
2994 if (pcap_strcasecmp(dlt_choices[i].name, name) == 0)
2995 return (dlt_choices[i].dlt);
2996 }
2997 return (-1);
2998 }
2999
3000 const char *
3001 pcap_datalink_val_to_name(int dlt)
3002 {
3003 int i;
3004
3005 for (i = 0; dlt_choices[i].name != NULL; i++) {
3006 if (dlt_choices[i].dlt == dlt)
3007 return (dlt_choices[i].name);
3008 }
3009 return (NULL);
3010 }
3011
3012 const char *
3013 pcap_datalink_val_to_description(int dlt)
3014 {
3015 int i;
3016
3017 for (i = 0; dlt_choices[i].name != NULL; i++) {
3018 if (dlt_choices[i].dlt == dlt)
3019 return (dlt_choices[i].description);
3020 }
3021 return (NULL);
3022 }
3023
3024 struct tstamp_type_choice {
3025 const char *name;
3026 const char *description;
3027 int type;
3028 };
3029
3030 static struct tstamp_type_choice tstamp_type_choices[] = {
3031 { "host", "Host", PCAP_TSTAMP_HOST },
3032 { "host_lowprec", "Host, low precision", PCAP_TSTAMP_HOST_LOWPREC },
3033 { "host_hiprec", "Host, high precision", PCAP_TSTAMP_HOST_HIPREC },
3034 { "adapter", "Adapter", PCAP_TSTAMP_ADAPTER },
3035 { "adapter_unsynced", "Adapter, not synced with system time", PCAP_TSTAMP_ADAPTER_UNSYNCED },
3036 { NULL, NULL, 0 }
3037 };
3038
3039 int
3040 pcap_tstamp_type_name_to_val(const char *name)
3041 {
3042 int i;
3043
3044 for (i = 0; tstamp_type_choices[i].name != NULL; i++) {
3045 if (pcap_strcasecmp(tstamp_type_choices[i].name, name) == 0)
3046 return (tstamp_type_choices[i].type);
3047 }
3048 return (PCAP_ERROR);
3049 }
3050
3051 const char *
3052 pcap_tstamp_type_val_to_name(int tstamp_type)
3053 {
3054 int i;
3055
3056 for (i = 0; tstamp_type_choices[i].name != NULL; i++) {
3057 if (tstamp_type_choices[i].type == tstamp_type)
3058 return (tstamp_type_choices[i].name);
3059 }
3060 return (NULL);
3061 }
3062
3063 const char *
3064 pcap_tstamp_type_val_to_description(int tstamp_type)
3065 {
3066 int i;
3067
3068 for (i = 0; tstamp_type_choices[i].name != NULL; i++) {
3069 if (tstamp_type_choices[i].type == tstamp_type)
3070 return (tstamp_type_choices[i].description);
3071 }
3072 return (NULL);
3073 }
3074
3075 int
3076 pcap_snapshot(pcap_t *p)
3077 {
3078 if (!p->activated)
3079 return (PCAP_ERROR_NOT_ACTIVATED);
3080 return (p->snapshot);
3081 }
3082
3083 int
3084 pcap_is_swapped(pcap_t *p)
3085 {
3086 if (!p->activated)
3087 return (PCAP_ERROR_NOT_ACTIVATED);
3088 return (p->swapped);
3089 }
3090
3091 int
3092 pcap_major_version(pcap_t *p)
3093 {
3094 if (!p->activated)
3095 return (PCAP_ERROR_NOT_ACTIVATED);
3096 return (p->version_major);
3097 }
3098
3099 int
3100 pcap_minor_version(pcap_t *p)
3101 {
3102 if (!p->activated)
3103 return (PCAP_ERROR_NOT_ACTIVATED);
3104 return (p->version_minor);
3105 }
3106
3107 int
3108 pcap_bufsize(pcap_t *p)
3109 {
3110 if (!p->activated)
3111 return (PCAP_ERROR_NOT_ACTIVATED);
3112 return (p->bufsize);
3113 }
3114
3115 FILE *
3116 pcap_file(pcap_t *p)
3117 {
3118 return (p->rfile);
3119 }
3120
3121 int
3122 pcap_fileno(pcap_t *p)
3123 {
3124 #ifndef _WIN32
3125 return (p->fd);
3126 #else
3127 if (p->handle != INVALID_HANDLE_VALUE)
3128 return ((int)(DWORD)p->handle);
3129 else
3130 return (PCAP_ERROR);
3131 #endif
3132 }
3133
3134 #if !defined(_WIN32) && !defined(MSDOS)
3135 int
3136 pcap_get_selectable_fd(pcap_t *p)
3137 {
3138 return (p->selectable_fd);
3139 }
3140
3141 struct timeval *
3142 pcap_get_required_select_timeout(pcap_t *p)
3143 {
3144 return (p->required_select_timeout);
3145 }
3146 #endif
3147
3148 void
3149 pcap_perror(pcap_t *p, const char *prefix)
3150 {
3151 fprintf(stderr, "%s: %s\n", prefix, p->errbuf);
3152 }
3153
3154 char *
3155 pcap_geterr(pcap_t *p)
3156 {
3157 return (p->errbuf);
3158 }
3159
3160 int
3161 pcap_getnonblock(pcap_t *p, char *errbuf)
3162 {
3163 int ret;
3164
3165 ret = p->getnonblock_op(p);
3166 if (ret == -1) {
3167 /*
3168 * The get nonblock operation sets p->errbuf; this
3169 * function *shouldn't* have had a separate errbuf
3170 * argument, as it didn't need one, but I goofed
3171 * when adding it.
3172 *
3173 * We copy the error message to errbuf, so callers
3174 * can find it in either place.
3175 */
3176 pcap_strlcpy(errbuf, p->errbuf, PCAP_ERRBUF_SIZE);
3177 }
3178 return (ret);
3179 }
3180
3181 /*
3182 * Get the current non-blocking mode setting, under the assumption that
3183 * it's just the standard POSIX non-blocking flag.
3184 */
3185 #if !defined(_WIN32) && !defined(MSDOS)
3186 int
3187 pcap_getnonblock_fd(pcap_t *p)
3188 {
3189 int fdflags;
3190
3191 fdflags = fcntl(p->fd, F_GETFL, 0);
3192 if (fdflags == -1) {
3193 pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
3194 errno, "F_GETFL");
3195 return (-1);
3196 }
3197 if (fdflags & O_NONBLOCK)
3198 return (1);
3199 else
3200 return (0);
3201 }
3202 #endif
3203
3204 int
3205 pcap_setnonblock(pcap_t *p, int nonblock, char *errbuf)
3206 {
3207 int ret;
3208
3209 ret = p->setnonblock_op(p, nonblock);
3210 if (ret == -1) {
3211 /*
3212 * The set nonblock operation sets p->errbuf; this
3213 * function *shouldn't* have had a separate errbuf
3214 * argument, as it didn't need one, but I goofed
3215 * when adding it.
3216 *
3217 * We copy the error message to errbuf, so callers
3218 * can find it in either place.
3219 */
3220 pcap_strlcpy(errbuf, p->errbuf, PCAP_ERRBUF_SIZE);
3221 }
3222 return (ret);
3223 }
3224
3225 #if !defined(_WIN32) && !defined(MSDOS)
3226 /*
3227 * Set non-blocking mode, under the assumption that it's just the
3228 * standard POSIX non-blocking flag. (This can be called by the
3229 * per-platform non-blocking-mode routine if that routine also
3230 * needs to do some additional work.)
3231 */
3232 int
3233 pcap_setnonblock_fd(pcap_t *p, int nonblock)
3234 {
3235 int fdflags;
3236
3237 fdflags = fcntl(p->fd, F_GETFL, 0);
3238 if (fdflags == -1) {
3239 pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
3240 errno, "F_GETFL");
3241 return (-1);
3242 }
3243 if (nonblock)
3244 fdflags |= O_NONBLOCK;
3245 else
3246 fdflags &= ~O_NONBLOCK;
3247 if (fcntl(p->fd, F_SETFL, fdflags) == -1) {
3248 pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
3249 errno, "F_SETFL");
3250 return (-1);
3251 }
3252 return (0);
3253 }
3254 #endif
3255
3256 #ifdef _WIN32
3257 /*
3258 * Generate a string for a Win32-specific error (i.e. an error generated when
3259 * calling a Win32 API).
3260 * For errors occurred during standard C calls, we still use pcap_strerror()
3261 */
3262 void
3263 pcap_win32_err_to_str(DWORD error, char *errbuf)
3264 {
3265 size_t errlen;
3266 char *p;
3267
3268 FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, 0, errbuf,
3269 PCAP_ERRBUF_SIZE, NULL);
3270
3271 /*
3272 * "FormatMessage()" "helpfully" sticks CR/LF at the end of the
3273 * message. Get rid of it.
3274 */
3275 errlen = strlen(errbuf);
3276 if (errlen >= 2) {
3277 errbuf[errlen - 1] = '\0';
3278 errbuf[errlen - 2] = '\0';
3279 }
3280 p = strchr(errbuf, '\0');
3281 pcap_snprintf (p, PCAP_ERRBUF_SIZE+1-(p-errbuf), " (%lu)", error);
3282 }
3283 #endif
3284
3285 /*
3286 * Generate error strings for PCAP_ERROR_ and PCAP_WARNING_ values.
3287 */
3288 const char *
3289 pcap_statustostr(int errnum)
3290 {
3291 static char ebuf[15+10+1];
3292
3293 switch (errnum) {
3294
3295 case PCAP_WARNING:
3296 return("Generic warning");
3297
3298 case PCAP_WARNING_TSTAMP_TYPE_NOTSUP:
3299 return ("That type of time stamp is not supported by that device");
3300
3301 case PCAP_WARNING_PROMISC_NOTSUP:
3302 return ("That device doesn't support promiscuous mode");
3303
3304 case PCAP_ERROR:
3305 return("Generic error");
3306
3307 case PCAP_ERROR_BREAK:
3308 return("Loop terminated by pcap_breakloop");
3309
3310 case PCAP_ERROR_NOT_ACTIVATED:
3311 return("The pcap_t has not been activated");
3312
3313 case PCAP_ERROR_ACTIVATED:
3314 return ("The setting can't be changed after the pcap_t is activated");
3315
3316 case PCAP_ERROR_NO_SUCH_DEVICE:
3317 return ("No such device exists");
3318
3319 case PCAP_ERROR_RFMON_NOTSUP:
3320 return ("That device doesn't support monitor mode");
3321
3322 case PCAP_ERROR_NOT_RFMON:
3323 return ("That operation is supported only in monitor mode");
3324
3325 case PCAP_ERROR_PERM_DENIED:
3326 return ("You don't have permission to capture on that device");
3327
3328 case PCAP_ERROR_IFACE_NOT_UP:
3329 return ("That device is not up");
3330
3331 case PCAP_ERROR_CANTSET_TSTAMP_TYPE:
3332 return ("That device doesn't support setting the time stamp type");
3333
3334 case PCAP_ERROR_PROMISC_PERM_DENIED:
3335 return ("You don't have permission to capture in promiscuous mode on that device");
3336
3337 case PCAP_ERROR_TSTAMP_PRECISION_NOTSUP:
3338 return ("That device doesn't support that time stamp precision");
3339 }
3340 (void)pcap_snprintf(ebuf, sizeof ebuf, "Unknown error: %d", errnum);
3341 return(ebuf);
3342 }
3343
3344 /*
3345 * Not all systems have strerror().
3346 */
3347 const char *
3348 pcap_strerror(int errnum)
3349 {
3350 #ifdef HAVE_STRERROR
3351 #ifdef _WIN32
3352 static char errbuf[PCAP_ERRBUF_SIZE];
3353 errno_t err = strerror_s(errbuf, PCAP_ERRBUF_SIZE, errnum);
3354
3355 if (err != 0) /* err = 0 if successful */
3356 pcap_strlcpy(errbuf, "strerror_s() error", PCAP_ERRBUF_SIZE);
3357 return (errbuf);
3358 #else
3359 return (strerror(errnum));
3360 #endif /* _WIN32 */
3361 #else
3362 extern int sys_nerr;
3363 extern const char *const sys_errlist[];
3364 static char errbuf[PCAP_ERRBUF_SIZE];
3365
3366 if ((unsigned int)errnum < sys_nerr)
3367 return ((char *)sys_errlist[errnum]);
3368 (void)pcap_snprintf(errbuf, sizeof errbuf, "Unknown error: %d", errnum);
3369 return (errbuf);
3370 #endif
3371 }
3372
3373 int
3374 pcap_setfilter(pcap_t *p, struct bpf_program *fp)
3375 {
3376 return (p->setfilter_op(p, fp));
3377 }
3378
3379 /*
3380 * Set direction flag, which controls whether we accept only incoming
3381 * packets, only outgoing packets, or both.
3382 * Note that, depending on the platform, some or all direction arguments
3383 * might not be supported.
3384 */
3385 int
3386 pcap_setdirection(pcap_t *p, pcap_direction_t d)
3387 {
3388 if (p->setdirection_op == NULL) {
3389 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
3390 "Setting direction is not implemented on this platform");
3391 return (-1);
3392 } else
3393 return (p->setdirection_op(p, d));
3394 }
3395
3396 int
3397 pcap_stats(pcap_t *p, struct pcap_stat *ps)
3398 {
3399 return (p->stats_op(p, ps));
3400 }
3401
3402 #ifdef _WIN32
3403 struct pcap_stat *
3404 pcap_stats_ex(pcap_t *p, int *pcap_stat_size)
3405 {
3406 return (p->stats_ex_op(p, pcap_stat_size));
3407 }
3408
3409 int
3410 pcap_setbuff(pcap_t *p, int dim)
3411 {
3412 return (p->setbuff_op(p, dim));
3413 }
3414
3415 int
3416 pcap_setmode(pcap_t *p, int mode)
3417 {
3418 return (p->setmode_op(p, mode));
3419 }
3420
3421 int
3422 pcap_setmintocopy(pcap_t *p, int size)
3423 {
3424 return (p->setmintocopy_op(p, size));
3425 }
3426
3427 HANDLE
3428 pcap_getevent(pcap_t *p)
3429 {
3430 return (p->getevent_op(p));
3431 }
3432
3433 int
3434 pcap_oid_get_request(pcap_t *p, bpf_u_int32 oid, void *data, size_t *lenp)
3435 {
3436 return (p->oid_get_request_op(p, oid, data, lenp));
3437 }
3438
3439 int
3440 pcap_oid_set_request(pcap_t *p, bpf_u_int32 oid, const void *data, size_t *lenp)
3441 {
3442 return (p->oid_set_request_op(p, oid, data, lenp));
3443 }
3444
3445 pcap_send_queue *
3446 pcap_sendqueue_alloc(u_int memsize)
3447 {
3448 pcap_send_queue *tqueue;
3449
3450 /* Allocate the queue */
3451 tqueue = (pcap_send_queue *)malloc(sizeof(pcap_send_queue));
3452 if (tqueue == NULL){
3453 return (NULL);
3454 }
3455
3456 /* Allocate the buffer */
3457 tqueue->buffer = (char *)malloc(memsize);
3458 if (tqueue->buffer == NULL) {
3459 free(tqueue);
3460 return (NULL);
3461 }
3462
3463 tqueue->maxlen = memsize;
3464 tqueue->len = 0;
3465
3466 return (tqueue);
3467 }
3468
3469 void
3470 pcap_sendqueue_destroy(pcap_send_queue *queue)
3471 {
3472 free(queue->buffer);
3473 free(queue);
3474 }
3475
3476 int
3477 pcap_sendqueue_queue(pcap_send_queue *queue, const struct pcap_pkthdr *pkt_header, const u_char *pkt_data)
3478 {
3479 if (queue->len + sizeof(struct pcap_pkthdr) + pkt_header->caplen > queue->maxlen){
3480 return (-1);
3481 }
3482
3483 /* Copy the pcap_pkthdr header*/
3484 memcpy(queue->buffer + queue->len, pkt_header, sizeof(struct pcap_pkthdr));
3485 queue->len += sizeof(struct pcap_pkthdr);
3486
3487 /* copy the packet */
3488 memcpy(queue->buffer + queue->len, pkt_data, pkt_header->caplen);
3489 queue->len += pkt_header->caplen;
3490
3491 return (0);
3492 }
3493
3494 u_int
3495 pcap_sendqueue_transmit(pcap_t *p, pcap_send_queue *queue, int sync)
3496 {
3497 return (p->sendqueue_transmit_op(p, queue, sync));
3498 }
3499
3500 int
3501 pcap_setuserbuffer(pcap_t *p, int size)
3502 {
3503 return (p->setuserbuffer_op(p, size));
3504 }
3505
3506 int
3507 pcap_live_dump(pcap_t *p, char *filename, int maxsize, int maxpacks)
3508 {
3509 return (p->live_dump_op(p, filename, maxsize, maxpacks));
3510 }
3511
3512 int
3513 pcap_live_dump_ended(pcap_t *p, int sync)
3514 {
3515 return (p->live_dump_ended_op(p, sync));
3516 }
3517
3518 PAirpcapHandle
3519 pcap_get_airpcap_handle(pcap_t *p)
3520 {
3521 PAirpcapHandle handle;
3522
3523 handle = p->get_airpcap_handle_op(p);
3524 if (handle == NULL) {
3525 (void)pcap_snprintf(p->errbuf, sizeof(p->errbuf),
3526 "This isn't an AirPcap device");
3527 }
3528 return (handle);
3529 }
3530 #endif
3531
3532 /*
3533 * On some platforms, we need to clean up promiscuous or monitor mode
3534 * when we close a device - and we want that to happen even if the
3535 * application just exits without explicitl closing devices.
3536 * On those platforms, we need to register a "close all the pcaps"
3537 * routine to be called when we exit, and need to maintain a list of
3538 * pcaps that need to be closed to clean up modes.
3539 *
3540 * XXX - not thread-safe.
3541 */
3542
3543 /*
3544 * List of pcaps on which we've done something that needs to be
3545 * cleaned up.
3546 * If there are any such pcaps, we arrange to call "pcap_close_all()"
3547 * when we exit, and have it close all of them.
3548 */
3549 static struct pcap *pcaps_to_close;
3550
3551 /*
3552 * TRUE if we've already called "atexit()" to cause "pcap_close_all()" to
3553 * be called on exit.
3554 */
3555 static int did_atexit;
3556
3557 static void
3558 pcap_close_all(void)
3559 {
3560 struct pcap *handle;
3561
3562 while ((handle = pcaps_to_close) != NULL)
3563 pcap_close(handle);
3564 }
3565
3566 int
3567 pcap_do_addexit(pcap_t *p)
3568 {
3569 /*
3570 * If we haven't already done so, arrange to have
3571 * "pcap_close_all()" called when we exit.
3572 */
3573 if (!did_atexit) {
3574 if (atexit(pcap_close_all) != 0) {
3575 /*
3576 * "atexit()" failed; let our caller know.
3577 */
3578 pcap_strlcpy(p->errbuf, "atexit failed", PCAP_ERRBUF_SIZE);
3579 return (0);
3580 }
3581 did_atexit = 1;
3582 }
3583 return (1);
3584 }
3585
3586 void
3587 pcap_add_to_pcaps_to_close(pcap_t *p)
3588 {
3589 p->next = pcaps_to_close;
3590 pcaps_to_close = p;
3591 }
3592
3593 void
3594 pcap_remove_from_pcaps_to_close(pcap_t *p)
3595 {
3596 pcap_t *pc, *prevpc;
3597
3598 for (pc = pcaps_to_close, prevpc = NULL; pc != NULL;
3599 prevpc = pc, pc = pc->next) {
3600 if (pc == p) {
3601 /*
3602 * Found it. Remove it from the list.
3603 */
3604 if (prevpc == NULL) {
3605 /*
3606 * It was at the head of the list.
3607 */
3608 pcaps_to_close = pc->next;
3609 } else {
3610 /*
3611 * It was in the middle of the list.
3612 */
3613 prevpc->next = pc->next;
3614 }
3615 break;
3616 }
3617 }
3618 }
3619
3620 void
3621 pcap_breakloop_common(pcap_t *p)
3622 {
3623 p->break_loop = 1;
3624 }
3625
3626
3627 void
3628 pcap_cleanup_live_common(pcap_t *p)
3629 {
3630 if (p->buffer != NULL) {
3631 free(p->buffer);
3632 p->buffer = NULL;
3633 }
3634 if (p->dlt_list != NULL) {
3635 free(p->dlt_list);
3636 p->dlt_list = NULL;
3637 p->dlt_count = 0;
3638 }
3639 if (p->tstamp_type_list != NULL) {
3640 free(p->tstamp_type_list);
3641 p->tstamp_type_list = NULL;
3642 p->tstamp_type_count = 0;
3643 }
3644 if (p->tstamp_precision_list != NULL) {
3645 free(p->tstamp_precision_list);
3646 p->tstamp_precision_list = NULL;
3647 p->tstamp_precision_count = 0;
3648 }
3649 pcap_freecode(&p->fcode);
3650 #if !defined(_WIN32) && !defined(MSDOS)
3651 if (p->fd >= 0) {
3652 close(p->fd);
3653 p->fd = -1;
3654 }
3655 p->selectable_fd = -1;
3656 #endif
3657 }
3658
3659 /*
3660 * API compatible with WinPcap's "send a packet" routine - returns -1
3661 * on error, 0 otherwise.
3662 *
3663 * XXX - what if we get a short write?
3664 */
3665 int
3666 pcap_sendpacket(pcap_t *p, const u_char *buf, int size)
3667 {
3668 if (size <= 0) {
3669 pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
3670 errno, "The number of bytes to be sent must be positive");
3671 return (PCAP_ERROR);
3672 }
3673
3674 if (p->inject_op(p, buf, size) == -1)
3675 return (-1);
3676 return (0);
3677 }
3678
3679 /*
3680 * API compatible with OpenBSD's "send a packet" routine - returns -1 on
3681 * error, number of bytes written otherwise.
3682 */
3683 int
3684 pcap_inject(pcap_t *p, const void *buf, size_t size)
3685 {
3686 /*
3687 * We return the number of bytes written, so the number of
3688 * bytes to write must fit in an int.
3689 */
3690 if (size > INT_MAX) {
3691 pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
3692 errno, "More than %d bytes cannot be injected", INT_MAX);
3693 return (PCAP_ERROR);
3694 }
3695
3696 if (size == 0) {
3697 pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
3698 errno, "The number of bytes to be injected must not be zero");
3699 return (PCAP_ERROR);
3700 }
3701
3702 return (p->inject_op(p, buf, (int)size));
3703 }
3704
3705 void
3706 pcap_close(pcap_t *p)
3707 {
3708 if (p->opt.device != NULL)
3709 free(p->opt.device);
3710 p->cleanup_op(p);
3711 free(p);
3712 }
3713
3714 /*
3715 * Given a BPF program, a pcap_pkthdr structure for a packet, and the raw
3716 * data for the packet, check whether the packet passes the filter.
3717 * Returns the return value of the filter program, which will be zero if
3718 * the packet doesn't pass and non-zero if the packet does pass.
3719 */
3720 int
3721 pcap_offline_filter(const struct bpf_program *fp, const struct pcap_pkthdr *h,
3722 const u_char *pkt)
3723 {
3724 const struct bpf_insn *fcode = fp->bf_insns;
3725
3726 if (fcode != NULL)
3727 return (pcap_filter(fcode, pkt, h->len, h->caplen));
3728 else
3729 return (0);
3730 }
3731
3732 static int
3733 pcap_can_set_rfmon_dead(pcap_t *p)
3734 {
3735 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
3736 "Rfmon mode doesn't apply on a pcap_open_dead pcap_t");
3737 return (PCAP_ERROR);
3738 }
3739
3740 static int
3741 pcap_read_dead(pcap_t *p, int cnt _U_, pcap_handler callback _U_,
3742 u_char *user _U_)
3743 {
3744 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
3745 "Packets aren't available from a pcap_open_dead pcap_t");
3746 return (-1);
3747 }
3748
3749 static int
3750 pcap_inject_dead(pcap_t *p, const void *buf _U_, int size _U_)
3751 {
3752 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
3753 "Packets can't be sent on a pcap_open_dead pcap_t");
3754 return (-1);
3755 }
3756
3757 static int
3758 pcap_setfilter_dead(pcap_t *p, struct bpf_program *fp _U_)
3759 {
3760 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
3761 "A filter cannot be set on a pcap_open_dead pcap_t");
3762 return (-1);
3763 }
3764
3765 static int
3766 pcap_setdirection_dead(pcap_t *p, pcap_direction_t d _U_)
3767 {
3768 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
3769 "The packet direction cannot be set on a pcap_open_dead pcap_t");
3770 return (-1);
3771 }
3772
3773 static int
3774 pcap_set_datalink_dead(pcap_t *p, int dlt _U_)
3775 {
3776 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
3777 "The link-layer header type cannot be set on a pcap_open_dead pcap_t");
3778 return (-1);
3779 }
3780
3781 static int
3782 pcap_getnonblock_dead(pcap_t *p)
3783 {
3784 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
3785 "A pcap_open_dead pcap_t does not have a non-blocking mode setting");
3786 return (-1);
3787 }
3788
3789 static int
3790 pcap_setnonblock_dead(pcap_t *p, int nonblock _U_)
3791 {
3792 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
3793 "A pcap_open_dead pcap_t does not have a non-blocking mode setting");
3794 return (-1);
3795 }
3796
3797 static int
3798 pcap_stats_dead(pcap_t *p, struct pcap_stat *ps _U_)
3799 {
3800 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
3801 "Statistics aren't available from a pcap_open_dead pcap_t");
3802 return (-1);
3803 }
3804
3805 #ifdef _WIN32
3806 struct pcap_stat *
3807 pcap_stats_ex_dead(pcap_t *p, int *pcap_stat_size _U_)
3808 {
3809 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
3810 "Statistics aren't available from a pcap_open_dead pcap_t");
3811 return (NULL);
3812 }
3813
3814 static int
3815 pcap_setbuff_dead(pcap_t *p, int dim)
3816 {
3817 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
3818 "The kernel buffer size cannot be set on a pcap_open_dead pcap_t");
3819 return (-1);
3820 }
3821
3822 static int
3823 pcap_setmode_dead(pcap_t *p, int mode)
3824 {
3825 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
3826 "impossible to set mode on a pcap_open_dead pcap_t");
3827 return (-1);
3828 }
3829
3830 static int
3831 pcap_setmintocopy_dead(pcap_t *p, int size)
3832 {
3833 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
3834 "The mintocopy parameter cannot be set on a pcap_open_dead pcap_t");
3835 return (-1);
3836 }
3837
3838 static HANDLE
3839 pcap_getevent_dead(pcap_t *p)
3840 {
3841 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
3842 "A pcap_open_dead pcap_t has no event handle");
3843 return (INVALID_HANDLE_VALUE);
3844 }
3845
3846 static int
3847 pcap_oid_get_request_dead(pcap_t *p, bpf_u_int32 oid _U_, void *data _U_,
3848 size_t *lenp _U_)
3849 {
3850 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
3851 "An OID get request cannot be performed on a pcap_open_dead pcap_t");
3852 return (PCAP_ERROR);
3853 }
3854
3855 static int
3856 pcap_oid_set_request_dead(pcap_t *p, bpf_u_int32 oid _U_, const void *data _U_,
3857 size_t *lenp _U_)
3858 {
3859 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
3860 "An OID set request cannot be performed on a pcap_open_dead pcap_t");
3861 return (PCAP_ERROR);
3862 }
3863
3864 static u_int
3865 pcap_sendqueue_transmit_dead(pcap_t *p, pcap_send_queue *queue, int sync)
3866 {
3867 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
3868 "Packets cannot be transmitted on a pcap_open_dead pcap_t");
3869 return (0);
3870 }
3871
3872 static int
3873 pcap_setuserbuffer_dead(pcap_t *p, int size)
3874 {
3875 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
3876 "The user buffer cannot be set on a pcap_open_dead pcap_t");
3877 return (-1);
3878 }
3879
3880 static int
3881 pcap_live_dump_dead(pcap_t *p, char *filename, int maxsize, int maxpacks)
3882 {
3883 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
3884 "Live packet dumping cannot be performed on a pcap_open_dead pcap_t");
3885 return (-1);
3886 }
3887
3888 static int
3889 pcap_live_dump_ended_dead(pcap_t *p, int sync)
3890 {
3891 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
3892 "Live packet dumping cannot be performed on a pcap_open_dead pcap_t");
3893 return (-1);
3894 }
3895
3896 static PAirpcapHandle
3897 pcap_get_airpcap_handle_dead(pcap_t *p)
3898 {
3899 return (NULL);
3900 }
3901 #endif /* _WIN32 */
3902
3903 static void
3904 pcap_cleanup_dead(pcap_t *p _U_)
3905 {
3906 /* Nothing to do. */
3907 }
3908
3909 pcap_t *
3910 pcap_open_dead_with_tstamp_precision(int linktype, int snaplen, u_int precision)
3911 {
3912 pcap_t *p;
3913
3914 switch (precision) {
3915
3916 case PCAP_TSTAMP_PRECISION_MICRO:
3917 case PCAP_TSTAMP_PRECISION_NANO:
3918 break;
3919
3920 default:
3921 /*
3922 * This doesn't really matter, but we don't have any way
3923 * to report particular errors, so the only failure we
3924 * should have is a memory allocation failure. Just
3925 * pick microsecond precision.
3926 */
3927 precision = PCAP_TSTAMP_PRECISION_MICRO;
3928 break;
3929 }
3930 p = malloc(sizeof(*p));
3931 if (p == NULL)
3932 return NULL;
3933 memset (p, 0, sizeof(*p));
3934 p->snapshot = snaplen;
3935 p->linktype = linktype;
3936 p->opt.tstamp_precision = precision;
3937 p->can_set_rfmon_op = pcap_can_set_rfmon_dead;
3938 p->read_op = pcap_read_dead;
3939 p->inject_op = pcap_inject_dead;
3940 p->setfilter_op = pcap_setfilter_dead;
3941 p->setdirection_op = pcap_setdirection_dead;
3942 p->set_datalink_op = pcap_set_datalink_dead;
3943 p->getnonblock_op = pcap_getnonblock_dead;
3944 p->setnonblock_op = pcap_setnonblock_dead;
3945 p->stats_op = pcap_stats_dead;
3946 #ifdef _WIN32
3947 p->stats_ex_op = pcap_stats_ex_dead;
3948 p->setbuff_op = pcap_setbuff_dead;
3949 p->setmode_op = pcap_setmode_dead;
3950 p->setmintocopy_op = pcap_setmintocopy_dead;
3951 p->getevent_op = pcap_getevent_dead;
3952 p->oid_get_request_op = pcap_oid_get_request_dead;
3953 p->oid_set_request_op = pcap_oid_set_request_dead;
3954 p->sendqueue_transmit_op = pcap_sendqueue_transmit_dead;
3955 p->setuserbuffer_op = pcap_setuserbuffer_dead;
3956 p->live_dump_op = pcap_live_dump_dead;
3957 p->live_dump_ended_op = pcap_live_dump_ended_dead;
3958 p->get_airpcap_handle_op = pcap_get_airpcap_handle_dead;
3959 #endif
3960 p->cleanup_op = pcap_cleanup_dead;
3961
3962 /*
3963 * A "dead" pcap_t never requires special BPF code generation.
3964 */
3965 p->bpf_codegen_flags = 0;
3966
3967 p->activated = 1;
3968 return (p);
3969 }
3970
3971 pcap_t *
3972 pcap_open_dead(int linktype, int snaplen)
3973 {
3974 return (pcap_open_dead_with_tstamp_precision(linktype, snaplen,
3975 PCAP_TSTAMP_PRECISION_MICRO));
3976 }
3977
3978 #ifdef YYDEBUG
3979 /*
3980 * Set the internal "debug printout" flag for the filter expression parser.
3981 * The code to print that stuff is present only if YYDEBUG is defined, so
3982 * the flag, and the routine to set it, are defined only if YYDEBUG is
3983 * defined.
3984 *
3985 * This is intended for libpcap developers, not for general use.
3986 * If you want to set these in a program, you'll have to declare this
3987 * routine yourself, with the appropriate DLL import attribute on Windows;
3988 * it's not declared in any header file, and won't be declared in any
3989 * header file provided by libpcap.
3990 */
3991 PCAP_API void pcap_set_parser_debug(int value);
3992
3993 PCAP_API_DEF void
3994 pcap_set_parser_debug(int value)
3995 {
3996 pcap_debug = value;
3997 }
3998 #endif