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