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