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