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