]> The Tcpdump Group git mirrors - libpcap/blob - pcap.c
pcap_create_interface() needs the interface name on Linux.
[libpcap] / pcap.c
1 /*
2 * Copyright (c) 1993, 1994, 1995, 1996, 1997, 1998
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the Computer Systems
16 * Engineering Group at Lawrence Berkeley Laboratory.
17 * 4. Neither the name of the University nor of the Laboratory may be used
18 * to endorse or promote products derived from this software without
19 * specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #ifdef HAVE_CONFIG_H
35 #include "config.h"
36 #endif
37
38 #ifdef _WIN32
39 #include <pcap-stdinc.h>
40 #else /* _WIN32 */
41 #if HAVE_INTTYPES_H
42 #include <inttypes.h>
43 #elif HAVE_STDINT_H
44 #include <stdint.h>
45 #endif
46 #ifdef HAVE_SYS_BITYPES_H
47 #include <sys/bitypes.h>
48 #endif
49 #include <sys/types.h>
50 #endif /* _WIN32 */
51
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #if !defined(_MSC_VER) && !defined(__BORLANDC__) && !defined(__MINGW32__)
56 #include <unistd.h>
57 #endif
58 #include <fcntl.h>
59 #include <errno.h>
60
61 #ifdef HAVE_OS_PROTO_H
62 #include "os-proto.h"
63 #endif
64
65 #ifdef MSDOS
66 #include "pcap-dos.h"
67 #endif
68
69 #include "pcap-int.h"
70
71 #ifdef HAVE_DAG_API
72 #include "pcap-dag.h"
73 #endif /* HAVE_DAG_API */
74
75 #ifdef HAVE_SEPTEL_API
76 #include "pcap-septel.h"
77 #endif /* HAVE_SEPTEL_API */
78
79 #ifdef HAVE_SNF_API
80 #include "pcap-snf.h"
81 #endif /* HAVE_SNF_API */
82
83 #ifdef HAVE_TC_API
84 #include "pcap-tc.h"
85 #endif /* HAVE_TC_API */
86
87 #ifdef PCAP_SUPPORT_USB
88 #include "pcap-usb-linux.h"
89 #endif
90
91 #ifdef PCAP_SUPPORT_BT
92 #include "pcap-bt-linux.h"
93 #endif
94
95 #ifdef PCAP_SUPPORT_BT_MONITOR
96 #include "pcap-bt-monitor-linux.h"
97 #endif
98
99 #ifdef PCAP_SUPPORT_CAN
100 #include "pcap-can-linux.h"
101 #endif
102
103 #ifdef PCAP_SUPPORT_CANUSB
104 #include "pcap-canusb-linux.h"
105 #endif
106
107 #ifdef PCAP_SUPPORT_NETFILTER
108 #include "pcap-netfilter-linux.h"
109 #endif
110
111 #ifdef PCAP_SUPPORT_DBUS
112 #include "pcap-dbus.h"
113 #endif
114
115 static int
116 pcap_not_initialized(pcap_t *pcap)
117 {
118 /* in case the caller doesn't check for PCAP_ERROR_NOT_ACTIVATED */
119 (void)pcap_snprintf(pcap->errbuf, sizeof(pcap->errbuf),
120 "This handle hasn't been activated yet");
121 /* this means 'not initialized' */
122 return (PCAP_ERROR_NOT_ACTIVATED);
123 }
124
125 #ifdef _WIN32
126 static void *
127 pcap_not_initialized_ptr(pcap_t *pcap)
128 {
129 (void)pcap_snprintf(pcap->errbuf, sizeof(pcap->errbuf),
130 "This handle hasn't been activated yet");
131 return (NULL);
132 }
133
134 static HANDLE
135 pcap_getevent_not_initialized(pcap_t *pcap)
136 {
137 (void)pcap_snprintf(pcap->errbuf, sizeof(pcap->errbuf),
138 "This handle hasn't been activated yet");
139 return (INVALID_HANDLE_VALUE);
140 }
141
142 static u_int
143 pcap_sendqueue_transmit_not_initialized(pcap_t *pcap, pcap_send_queue* queue, int sync)
144 {
145 (void)pcap_snprintf(pcap->errbuf, sizeof(pcap->errbuf),
146 "This handle hasn't been activated yet");
147 return (0);
148 }
149
150 static PAirpcapHandle
151 pcap_get_airpcap_handle_not_initialized(pcap_t *pcap)
152 {
153 (void)pcap_snprintf(pcap->errbuf, sizeof(pcap->errbuf),
154 "This handle hasn't been activated yet");
155 return (NULL);
156 }
157 #endif
158
159 /*
160 * Returns 1 if rfmon mode can be set on the pcap_t, 0 if it can't,
161 * a PCAP_ERROR value on an error.
162 */
163 int
164 pcap_can_set_rfmon(pcap_t *p)
165 {
166 return (p->can_set_rfmon_op(p));
167 }
168
169 /*
170 * For systems where rfmon mode is never supported.
171 */
172 static int
173 pcap_cant_set_rfmon(pcap_t *p _U_)
174 {
175 return (0);
176 }
177
178 /*
179 * Sets *tstamp_typesp to point to an array 1 or more supported time stamp
180 * types; the return value is the number of supported time stamp types.
181 * The list should be freed by a call to pcap_free_tstamp_types() when
182 * you're done with it.
183 *
184 * A return value of 0 means "you don't get a choice of time stamp type",
185 * in which case *tstamp_typesp is set to null.
186 *
187 * PCAP_ERROR is returned on error.
188 */
189 int
190 pcap_list_tstamp_types(pcap_t *p, int **tstamp_typesp)
191 {
192 if (p->tstamp_type_count == 0) {
193 /*
194 * We don't support multiple time stamp types.
195 */
196 *tstamp_typesp = NULL;
197 } else {
198 *tstamp_typesp = (int*)calloc(sizeof(**tstamp_typesp),
199 p->tstamp_type_count);
200 if (*tstamp_typesp == NULL) {
201 (void)pcap_snprintf(p->errbuf, sizeof(p->errbuf),
202 "malloc: %s", pcap_strerror(errno));
203 return (PCAP_ERROR);
204 }
205 (void)memcpy(*tstamp_typesp, p->tstamp_type_list,
206 sizeof(**tstamp_typesp) * p->tstamp_type_count);
207 }
208 return (p->tstamp_type_count);
209 }
210
211 /*
212 * In Windows, you might have a library built with one version of the
213 * C runtime library and an application built with another version of
214 * the C runtime library, which means that the library might use one
215 * version of malloc() and free() and the application might use another
216 * version of malloc() and free(). If so, that means something
217 * allocated by the library cannot be freed by the application, so we
218 * need to have a pcap_free_tstamp_types() routine to free up the list
219 * allocated by pcap_list_tstamp_types(), even though it's just a wrapper
220 * around free().
221 */
222 void
223 pcap_free_tstamp_types(int *tstamp_type_list)
224 {
225 free(tstamp_type_list);
226 }
227
228 /*
229 * Default one-shot callback; overridden for capture types where the
230 * packet data cannot be guaranteed to be available after the callback
231 * returns, so that a copy must be made.
232 */
233 void
234 pcap_oneshot(u_char *user, const struct pcap_pkthdr *h, const u_char *pkt)
235 {
236 struct oneshot_userdata *sp = (struct oneshot_userdata *)user;
237
238 *sp->hdr = *h;
239 *sp->pkt = pkt;
240 }
241
242 const u_char *
243 pcap_next(pcap_t *p, struct pcap_pkthdr *h)
244 {
245 struct oneshot_userdata s;
246 const u_char *pkt;
247
248 s.hdr = h;
249 s.pkt = &pkt;
250 s.pd = p;
251 if (pcap_dispatch(p, 1, p->oneshot_callback, (u_char *)&s) <= 0)
252 return (0);
253 return (pkt);
254 }
255
256 int
257 pcap_next_ex(pcap_t *p, struct pcap_pkthdr **pkt_header,
258 const u_char **pkt_data)
259 {
260 struct oneshot_userdata s;
261
262 s.hdr = &p->pcap_header;
263 s.pkt = pkt_data;
264 s.pd = p;
265
266 /* Saves a pointer to the packet headers */
267 *pkt_header= &p->pcap_header;
268
269 if (p->rfile != NULL) {
270 int status;
271
272 /* We are on an offline capture */
273 status = pcap_offline_read(p, 1, p->oneshot_callback,
274 (u_char *)&s);
275
276 /*
277 * Return codes for pcap_offline_read() are:
278 * - 0: EOF
279 * - -1: error
280 * - >1: OK
281 * The first one ('0') conflicts with the return code of
282 * 0 from pcap_read() meaning "no packets arrived before
283 * the timeout expired", so we map it to -2 so you can
284 * distinguish between an EOF from a savefile and a
285 * "no packets arrived before the timeout expired, try
286 * again" from a live capture.
287 */
288 if (status == 0)
289 return (-2);
290 else
291 return (status);
292 }
293
294 /*
295 * Return codes for pcap_read() are:
296 * - 0: timeout
297 * - -1: error
298 * - -2: loop was broken out of with pcap_breakloop()
299 * - >1: OK
300 * The first one ('0') conflicts with the return code of 0 from
301 * pcap_offline_read() meaning "end of file".
302 */
303 return (p->read_op(p, 1, p->oneshot_callback, (u_char *)&s));
304 }
305
306 #if defined(DAG_ONLY)
307 int
308 pcap_findalldevs(pcap_if_t **alldevsp, char *errbuf)
309 {
310 return (dag_findalldevs(alldevsp, errbuf));
311 }
312
313 pcap_t *
314 pcap_create(const char *device, char *errbuf)
315 {
316 int is_ours;
317 return (dag_create(device, errbuf, &is_ours));
318 }
319 #elif defined(SEPTEL_ONLY)
320 int
321 pcap_findalldevs(pcap_if_t **alldevsp, char *errbuf)
322 {
323 return (septel_findalldevs(alldevsp, errbuf));
324 }
325
326 pcap_t *
327 pcap_create(const char *device, char *errbuf)
328 {
329 int is_ours;
330 return (septel_create(device, errbuf, &is_ours));
331 }
332 #elif defined(SNF_ONLY)
333 int
334 pcap_findalldevs(pcap_if_t **alldevsp, char *errbuf)
335 {
336 return (snf_findalldevs(alldevsp, errbuf));
337 }
338
339 pcap_t *
340 pcap_create(const char *device, char *errbuf)
341 {
342 int is_ours;
343 return (snf_create(device, errbuf, &is_ours));
344 }
345 #else /* regular pcap */
346 struct capture_source_type {
347 int (*findalldevs_op)(pcap_if_t **, char *);
348 pcap_t *(*create_op)(const char *, char *, int *);
349 } capture_source_types[] = {
350 #ifdef HAVE_DAG_API
351 { dag_findalldevs, dag_create },
352 #endif
353 #ifdef HAVE_SEPTEL_API
354 { septel_findalldevs, septel_create },
355 #endif
356 #ifdef HAVE_SNF_API
357 { snf_findalldevs, snf_create },
358 #endif
359 #ifdef HAVE_TC_API
360 { TcFindAllDevs, TcCreate },
361 #endif
362 #ifdef PCAP_SUPPORT_BT
363 { bt_findalldevs, bt_create },
364 #endif
365 #ifdef PCAP_SUPPORT_BT_MONITOR
366 { bt_monitor_findalldevs, bt_monitor_create },
367 #endif
368 #if PCAP_SUPPORT_CANUSB
369 { canusb_findalldevs, canusb_create },
370 #endif
371 #ifdef PCAP_SUPPORT_CAN
372 { can_findalldevs, can_create },
373 #endif
374 #ifdef PCAP_SUPPORT_USB
375 { usb_findalldevs, usb_create },
376 #endif
377 #ifdef PCAP_SUPPORT_NETFILTER
378 { netfilter_findalldevs, netfilter_create },
379 #endif
380 #ifdef PCAP_SUPPORT_DBUS
381 { dbus_findalldevs, dbus_create },
382 #endif
383 { NULL, NULL }
384 };
385
386 /*
387 * Get a list of all capture sources that are up and that we can open.
388 * Returns -1 on error, 0 otherwise.
389 * The list, as returned through "alldevsp", may be null if no interfaces
390 * were up and could be opened.
391 */
392 int
393 pcap_findalldevs(pcap_if_t **alldevsp, char *errbuf)
394 {
395 size_t i;
396
397 /*
398 * Find all the local network interfaces on which we
399 * can capture.
400 */
401 if (pcap_platform_finddevs(alldevsp, errbuf) == -1)
402 return (-1);
403
404 /*
405 * Ask each of the non-local-network-interface capture
406 * source types what interfaces they have.
407 */
408 for (i = 0; capture_source_types[i].findalldevs_op != NULL; i++) {
409 if (capture_source_types[i].findalldevs_op(alldevsp, errbuf) == -1) {
410 /*
411 * We had an error; free the list we've been
412 * constructing.
413 */
414 if (*alldevsp != NULL) {
415 pcap_freealldevs(*alldevsp);
416 *alldevsp = NULL;
417 }
418 return (-1);
419 }
420 }
421
422 return (0);
423 }
424
425 pcap_t *
426 pcap_create(const char *device, char *errbuf)
427 {
428 size_t i;
429 int is_theirs;
430 pcap_t *p;
431 char *device_str;
432
433 /*
434 * A null device name is equivalent to the "any" device -
435 * which might not be supported on this platform, but
436 * this means that you'll get a "not supported" error
437 * rather than, say, a crash when we try to dereference
438 * the null pointer.
439 */
440 if (device == NULL)
441 device_str = strdup("any");
442 else {
443 #ifdef _WIN32
444 /*
445 * If the string appears to be little-endian UCS-2/UTF-16,
446 * convert it to ASCII.
447 *
448 * XXX - to UTF-8 instead? Or report an error if any
449 * character isn't ASCII?
450 */
451 if (device[0] != '\0' && device[1] == '\0') {
452 size_t length;
453 char *device_ascii;
454
455 length = wcslen((wchar_t *)device);
456 device_str = (char *)malloc(length + 1);
457 if (device_str == NULL) {
458 pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE,
459 "malloc: %s", pcap_strerror(errno));
460 return (NULL);
461 }
462
463 pcap_snprintf(device, length + 1, "%ws",
464 (wchar_t *)device);
465 } else
466 #endif
467 device_str = strdup(device);
468 }
469 if (device_str == NULL) {
470 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
471 "malloc: %s", pcap_strerror(errno));
472 return (NULL);
473 }
474
475 /*
476 * Try each of the non-local-network-interface capture
477 * source types until we find one that works for this
478 * device or run out of types.
479 */
480 for (i = 0; capture_source_types[i].create_op != NULL; i++) {
481 is_theirs = 0;
482 p = capture_source_types[i].create_op(device_str, errbuf,
483 &is_theirs);
484 if (is_theirs) {
485 /*
486 * The device name refers to a device of the
487 * type in question; either it succeeded,
488 * in which case p refers to a pcap_t to
489 * later activate for the device, or it
490 * failed, in which case p is null and we
491 * should return that to report the failure
492 * to create.
493 */
494 if (p == NULL) {
495 /*
496 * We assume the caller filled in errbuf.
497 */
498 free(device_str);
499 return (NULL);
500 }
501 p->opt.device = device_str;
502 return (p);
503 }
504 }
505
506 /*
507 * OK, try it as a regular network interface.
508 */
509 p = pcap_create_interface(device_str, errbuf);
510 if (p == NULL) {
511 /*
512 * We assume the caller filled in errbuf.
513 */
514 free(device_str);
515 return (NULL);
516 }
517 p->opt.device = device_str;
518 return (p);
519 }
520 #endif
521
522 static void
523 initialize_ops(pcap_t *p)
524 {
525 /*
526 * Set operation pointers for operations that only work on
527 * an activated pcap_t to point to a routine that returns
528 * a "this isn't activated" error.
529 */
530 p->read_op = (read_op_t)pcap_not_initialized;
531 p->inject_op = (inject_op_t)pcap_not_initialized;
532 p->setfilter_op = (setfilter_op_t)pcap_not_initialized;
533 p->setdirection_op = (setdirection_op_t)pcap_not_initialized;
534 p->set_datalink_op = (set_datalink_op_t)pcap_not_initialized;
535 p->getnonblock_op = (getnonblock_op_t)pcap_not_initialized;
536 p->setnonblock_op = (setnonblock_op_t)pcap_not_initialized;
537 p->stats_op = (stats_op_t)pcap_not_initialized;
538 #ifdef _WIN32
539 p->stats_ex_op = (stats_ex_op_t)pcap_not_initialized_ptr;
540 p->setbuff_op = (setbuff_op_t)pcap_not_initialized;
541 p->setmode_op = (setmode_op_t)pcap_not_initialized;
542 p->setmintocopy_op = (setmintocopy_op_t)pcap_not_initialized;
543 p->getevent_op = pcap_getevent_not_initialized;
544 p->oid_get_request_op = (oid_get_request_op_t)pcap_not_initialized;
545 p->oid_set_request_op = (oid_set_request_op_t)pcap_not_initialized;
546 p->sendqueue_transmit_op = pcap_sendqueue_transmit_not_initialized;
547 p->setuserbuffer_op = (setuserbuffer_op_t)pcap_not_initialized;
548 p->live_dump_op = (live_dump_op_t)pcap_not_initialized;
549 p->live_dump_ended_op = (live_dump_ended_op_t)pcap_not_initialized;
550 p->get_airpcap_handle_op = pcap_get_airpcap_handle_not_initialized;
551 #endif
552
553 /*
554 * Default cleanup operation - implementations can override
555 * this, but should call pcap_cleanup_live_common() after
556 * doing their own additional cleanup.
557 */
558 p->cleanup_op = pcap_cleanup_live_common;
559
560 /*
561 * In most cases, the standard one-shot callback can
562 * be used for pcap_next()/pcap_next_ex().
563 */
564 p->oneshot_callback = pcap_oneshot;
565 }
566
567 static pcap_t *
568 pcap_alloc_pcap_t(char *ebuf, size_t size)
569 {
570 char *chunk;
571 pcap_t *p;
572
573 /*
574 * Allocate a chunk of memory big enough for a pcap_t
575 * plus a structure following it of size "size". The
576 * structure following it is a private data structure
577 * for the routines that handle this pcap_t.
578 */
579 chunk = malloc(sizeof (pcap_t) + size);
580 if (chunk == NULL) {
581 pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s",
582 pcap_strerror(errno));
583 return (NULL);
584 }
585 memset(chunk, 0, sizeof (pcap_t) + size);
586
587 /*
588 * Get a pointer to the pcap_t at the beginning.
589 */
590 p = (pcap_t *)chunk;
591
592 #ifndef _WIN32
593 p->fd = -1; /* not opened yet */
594 p->selectable_fd = -1;
595 #endif
596
597 if (size == 0) {
598 /* No private data was requested. */
599 p->priv = NULL;
600 } else {
601 /*
602 * Set the pointer to the private data; that's the structure
603 * of size "size" following the pcap_t.
604 */
605 p->priv = (void *)(chunk + sizeof (pcap_t));
606 }
607
608 return (p);
609 }
610
611 pcap_t *
612 pcap_create_common(char *ebuf, size_t size)
613 {
614 pcap_t *p;
615
616 p = pcap_alloc_pcap_t(ebuf, size);
617 if (p == NULL)
618 return (NULL);
619
620 /*
621 * Default to "can't set rfmon mode"; if it's supported by
622 * a platform, the create routine that called us can set
623 * the op to its routine to check whether a particular
624 * device supports it.
625 */
626 p->can_set_rfmon_op = pcap_cant_set_rfmon;
627
628 initialize_ops(p);
629
630 /* put in some defaults*/
631 pcap_set_snaplen(p, MAXIMUM_SNAPLEN); /* max packet size */
632 p->opt.timeout = 0; /* no timeout specified */
633 p->opt.buffer_size = 0; /* use the platform's default */
634 p->opt.promisc = 0;
635 p->opt.rfmon = 0;
636 p->opt.immediate = 0;
637 p->opt.tstamp_type = -1; /* default to not setting time stamp type */
638 p->opt.tstamp_precision = PCAP_TSTAMP_PRECISION_MICRO;
639
640 /*
641 * Start out with no BPF code generation flags set.
642 */
643 p->bpf_codegen_flags = 0;
644
645 return (p);
646 }
647
648 int
649 pcap_check_activated(pcap_t *p)
650 {
651 if (p->activated) {
652 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "can't perform "
653 " operation on activated capture");
654 return (-1);
655 }
656 return (0);
657 }
658
659 int
660 pcap_set_snaplen(pcap_t *p, int snaplen)
661 {
662 if (pcap_check_activated(p))
663 return (PCAP_ERROR_ACTIVATED);
664 p->snapshot = snaplen;
665 return (0);
666 }
667
668 int
669 pcap_set_promisc(pcap_t *p, int promisc)
670 {
671 if (pcap_check_activated(p))
672 return (PCAP_ERROR_ACTIVATED);
673 p->opt.promisc = promisc;
674 return (0);
675 }
676
677 int
678 pcap_set_rfmon(pcap_t *p, int rfmon)
679 {
680 if (pcap_check_activated(p))
681 return (PCAP_ERROR_ACTIVATED);
682 p->opt.rfmon = rfmon;
683 return (0);
684 }
685
686 int
687 pcap_set_timeout(pcap_t *p, int timeout_ms)
688 {
689 if (pcap_check_activated(p))
690 return (PCAP_ERROR_ACTIVATED);
691 p->opt.timeout = timeout_ms;
692 return (0);
693 }
694
695 int
696 pcap_set_tstamp_type(pcap_t *p, int tstamp_type)
697 {
698 int i;
699
700 if (pcap_check_activated(p))
701 return (PCAP_ERROR_ACTIVATED);
702
703 /*
704 * If p->tstamp_type_count is 0, we only support PCAP_TSTAMP_HOST;
705 * the default time stamp type is PCAP_TSTAMP_HOST.
706 */
707 if (p->tstamp_type_count == 0) {
708 if (tstamp_type == PCAP_TSTAMP_HOST) {
709 p->opt.tstamp_type = tstamp_type;
710 return (0);
711 }
712 } else {
713 /*
714 * Check whether we claim to support this type of time stamp.
715 */
716 for (i = 0; i < p->tstamp_type_count; i++) {
717 if (p->tstamp_type_list[i] == tstamp_type) {
718 /*
719 * Yes.
720 */
721 p->opt.tstamp_type = tstamp_type;
722 return (0);
723 }
724 }
725 }
726
727 /*
728 * We don't support this type of time stamp.
729 */
730 return (PCAP_WARNING_TSTAMP_TYPE_NOTSUP);
731 }
732
733 int
734 pcap_set_immediate_mode(pcap_t *p, int immediate)
735 {
736 if (pcap_check_activated(p))
737 return (PCAP_ERROR_ACTIVATED);
738 p->opt.immediate = immediate;
739 return (0);
740 }
741
742 int
743 pcap_set_buffer_size(pcap_t *p, int buffer_size)
744 {
745 if (pcap_check_activated(p))
746 return (PCAP_ERROR_ACTIVATED);
747 p->opt.buffer_size = buffer_size;
748 return (0);
749 }
750
751 int
752 pcap_set_tstamp_precision(pcap_t *p, int tstamp_precision)
753 {
754 int i;
755
756 if (pcap_check_activated(p))
757 return (PCAP_ERROR_ACTIVATED);
758
759 /*
760 * If p->tstamp_precision_count is 0, we only support setting
761 * the time stamp precision to microsecond precision; every
762 * pcap module *MUST* support microsecond precision, even if
763 * it does so by converting the native precision to
764 * microseconds.
765 */
766 if (p->tstamp_precision_count == 0) {
767 if (tstamp_precision == PCAP_TSTAMP_PRECISION_MICRO) {
768 p->opt.tstamp_precision = tstamp_precision;
769 return (0);
770 }
771 } else {
772 /*
773 * Check whether we claim to support this precision of
774 * time stamp.
775 */
776 for (i = 0; i < p->tstamp_precision_count; i++) {
777 if (p->tstamp_precision_list[i] == tstamp_precision) {
778 /*
779 * Yes.
780 */
781 p->opt.tstamp_precision = tstamp_precision;
782 return (0);
783 }
784 }
785 }
786
787 /*
788 * We don't support this time stamp precision.
789 */
790 return (PCAP_ERROR_TSTAMP_PRECISION_NOTSUP);
791 }
792
793 int
794 pcap_get_tstamp_precision(pcap_t *p)
795 {
796 return (p->opt.tstamp_precision);
797 }
798
799 int
800 pcap_activate(pcap_t *p)
801 {
802 int status;
803
804 /*
805 * Catch attempts to re-activate an already-activated
806 * pcap_t; this should, for example, catch code that
807 * calls pcap_open_live() followed by pcap_activate(),
808 * as some code that showed up in a Stack Exchange
809 * question did.
810 */
811 if (pcap_check_activated(p))
812 return (PCAP_ERROR_ACTIVATED);
813 status = p->activate_op(p);
814 if (status >= 0)
815 p->activated = 1;
816 else {
817 if (p->errbuf[0] == '\0') {
818 /*
819 * No error message supplied by the activate routine;
820 * for the benefit of programs that don't specially
821 * handle errors other than PCAP_ERROR, return the
822 * error message corresponding to the status.
823 */
824 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "%s",
825 pcap_statustostr(status));
826 }
827
828 /*
829 * Undo any operation pointer setting, etc. done by
830 * the activate operation.
831 */
832 initialize_ops(p);
833 }
834 return (status);
835 }
836
837 pcap_t *
838 pcap_open_live(const char *device, int snaplen, int promisc, int to_ms, char *errbuf)
839 {
840 pcap_t *p;
841 int status;
842
843 p = pcap_create(device, errbuf);
844 if (p == NULL)
845 return (NULL);
846 status = pcap_set_snaplen(p, snaplen);
847 if (status < 0)
848 goto fail;
849 status = pcap_set_promisc(p, promisc);
850 if (status < 0)
851 goto fail;
852 status = pcap_set_timeout(p, to_ms);
853 if (status < 0)
854 goto fail;
855 /*
856 * Mark this as opened with pcap_open_live(), so that, for
857 * example, we show the full list of DLT_ values, rather
858 * than just the ones that are compatible with capturing
859 * when not in monitor mode. That allows existing applications
860 * to work the way they used to work, but allows new applications
861 * that know about the new open API to, for example, find out the
862 * DLT_ values that they can select without changing whether
863 * the adapter is in monitor mode or not.
864 */
865 p->oldstyle = 1;
866 status = pcap_activate(p);
867 if (status < 0)
868 goto fail;
869 return (p);
870 fail:
871 if (status == PCAP_ERROR)
872 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s", device,
873 p->errbuf);
874 else if (status == PCAP_ERROR_NO_SUCH_DEVICE ||
875 status == PCAP_ERROR_PERM_DENIED ||
876 status == PCAP_ERROR_PROMISC_PERM_DENIED)
877 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s (%s)", device,
878 pcap_statustostr(status), p->errbuf);
879 else
880 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s", device,
881 pcap_statustostr(status));
882 pcap_close(p);
883 return (NULL);
884 }
885
886 pcap_t *
887 pcap_open_offline_common(char *ebuf, size_t size)
888 {
889 pcap_t *p;
890
891 p = pcap_alloc_pcap_t(ebuf, size);
892 if (p == NULL)
893 return (NULL);
894
895 p->opt.tstamp_precision = PCAP_TSTAMP_PRECISION_MICRO;
896
897 return (p);
898 }
899
900 int
901 pcap_dispatch(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
902 {
903 return (p->read_op(p, cnt, callback, user));
904 }
905
906 int
907 pcap_loop(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
908 {
909 register int n;
910
911 for (;;) {
912 if (p->rfile != NULL) {
913 /*
914 * 0 means EOF, so don't loop if we get 0.
915 */
916 n = pcap_offline_read(p, cnt, callback, user);
917 } else {
918 /*
919 * XXX keep reading until we get something
920 * (or an error occurs)
921 */
922 do {
923 n = p->read_op(p, cnt, callback, user);
924 } while (n == 0);
925 }
926 if (n <= 0)
927 return (n);
928 if (!PACKET_COUNT_IS_UNLIMITED(cnt)) {
929 cnt -= n;
930 if (cnt <= 0)
931 return (0);
932 }
933 }
934 }
935
936 /*
937 * Force the loop in "pcap_read()" or "pcap_read_offline()" to terminate.
938 */
939 void
940 pcap_breakloop(pcap_t *p)
941 {
942 p->break_loop = 1;
943 }
944
945 int
946 pcap_datalink(pcap_t *p)
947 {
948 if (!p->activated)
949 return (PCAP_ERROR_NOT_ACTIVATED);
950 return (p->linktype);
951 }
952
953 int
954 pcap_datalink_ext(pcap_t *p)
955 {
956 if (!p->activated)
957 return (PCAP_ERROR_NOT_ACTIVATED);
958 return (p->linktype_ext);
959 }
960
961 int
962 pcap_list_datalinks(pcap_t *p, int **dlt_buffer)
963 {
964 if (!p->activated)
965 return (PCAP_ERROR_NOT_ACTIVATED);
966 if (p->dlt_count == 0) {
967 /*
968 * We couldn't fetch the list of DLTs, which means
969 * this platform doesn't support changing the
970 * DLT for an interface. Return a list of DLTs
971 * containing only the DLT this device supports.
972 */
973 *dlt_buffer = (int*)malloc(sizeof(**dlt_buffer));
974 if (*dlt_buffer == NULL) {
975 (void)pcap_snprintf(p->errbuf, sizeof(p->errbuf),
976 "malloc: %s", pcap_strerror(errno));
977 return (PCAP_ERROR);
978 }
979 **dlt_buffer = p->linktype;
980 return (1);
981 } else {
982 *dlt_buffer = (int*)calloc(sizeof(**dlt_buffer), p->dlt_count);
983 if (*dlt_buffer == NULL) {
984 (void)pcap_snprintf(p->errbuf, sizeof(p->errbuf),
985 "malloc: %s", pcap_strerror(errno));
986 return (PCAP_ERROR);
987 }
988 (void)memcpy(*dlt_buffer, p->dlt_list,
989 sizeof(**dlt_buffer) * p->dlt_count);
990 return (p->dlt_count);
991 }
992 }
993
994 /*
995 * In Windows, you might have a library built with one version of the
996 * C runtime library and an application built with another version of
997 * the C runtime library, which means that the library might use one
998 * version of malloc() and free() and the application might use another
999 * version of malloc() and free(). If so, that means something
1000 * allocated by the library cannot be freed by the application, so we
1001 * need to have a pcap_free_datalinks() routine to free up the list
1002 * allocated by pcap_list_datalinks(), even though it's just a wrapper
1003 * around free().
1004 */
1005 void
1006 pcap_free_datalinks(int *dlt_list)
1007 {
1008 free(dlt_list);
1009 }
1010
1011 int
1012 pcap_set_datalink(pcap_t *p, int dlt)
1013 {
1014 int i;
1015 const char *dlt_name;
1016
1017 if (p->dlt_count == 0 || p->set_datalink_op == NULL) {
1018 /*
1019 * We couldn't fetch the list of DLTs, or we don't
1020 * have a "set datalink" operation, which means
1021 * this platform doesn't support changing the
1022 * DLT for an interface. Check whether the new
1023 * DLT is the one this interface supports.
1024 */
1025 if (p->linktype != dlt)
1026 goto unsupported;
1027
1028 /*
1029 * It is, so there's nothing we need to do here.
1030 */
1031 return (0);
1032 }
1033 for (i = 0; i < p->dlt_count; i++)
1034 if (p->dlt_list[i] == dlt)
1035 break;
1036 if (i >= p->dlt_count)
1037 goto unsupported;
1038 if (p->dlt_count == 2 && p->dlt_list[0] == DLT_EN10MB &&
1039 dlt == DLT_DOCSIS) {
1040 /*
1041 * This is presumably an Ethernet device, as the first
1042 * link-layer type it offers is DLT_EN10MB, and the only
1043 * other type it offers is DLT_DOCSIS. That means that
1044 * we can't tell the driver to supply DOCSIS link-layer
1045 * headers - we're just pretending that's what we're
1046 * getting, as, presumably, we're capturing on a dedicated
1047 * link to a Cisco Cable Modem Termination System, and
1048 * it's putting raw DOCSIS frames on the wire inside low-level
1049 * Ethernet framing.
1050 */
1051 p->linktype = dlt;
1052 return (0);
1053 }
1054 if (p->set_datalink_op(p, dlt) == -1)
1055 return (-1);
1056 p->linktype = dlt;
1057 return (0);
1058
1059 unsupported:
1060 dlt_name = pcap_datalink_val_to_name(dlt);
1061 if (dlt_name != NULL) {
1062 (void) pcap_snprintf(p->errbuf, sizeof(p->errbuf),
1063 "%s is not one of the DLTs supported by this device",
1064 dlt_name);
1065 } else {
1066 (void) pcap_snprintf(p->errbuf, sizeof(p->errbuf),
1067 "DLT %d is not one of the DLTs supported by this device",
1068 dlt);
1069 }
1070 return (-1);
1071 }
1072
1073 /*
1074 * This array is designed for mapping upper and lower case letter
1075 * together for a case independent comparison. The mappings are
1076 * based upon ascii character sequences.
1077 */
1078 static const u_char charmap[] = {
1079 (u_char)'\000', (u_char)'\001', (u_char)'\002', (u_char)'\003',
1080 (u_char)'\004', (u_char)'\005', (u_char)'\006', (u_char)'\007',
1081 (u_char)'\010', (u_char)'\011', (u_char)'\012', (u_char)'\013',
1082 (u_char)'\014', (u_char)'\015', (u_char)'\016', (u_char)'\017',
1083 (u_char)'\020', (u_char)'\021', (u_char)'\022', (u_char)'\023',
1084 (u_char)'\024', (u_char)'\025', (u_char)'\026', (u_char)'\027',
1085 (u_char)'\030', (u_char)'\031', (u_char)'\032', (u_char)'\033',
1086 (u_char)'\034', (u_char)'\035', (u_char)'\036', (u_char)'\037',
1087 (u_char)'\040', (u_char)'\041', (u_char)'\042', (u_char)'\043',
1088 (u_char)'\044', (u_char)'\045', (u_char)'\046', (u_char)'\047',
1089 (u_char)'\050', (u_char)'\051', (u_char)'\052', (u_char)'\053',
1090 (u_char)'\054', (u_char)'\055', (u_char)'\056', (u_char)'\057',
1091 (u_char)'\060', (u_char)'\061', (u_char)'\062', (u_char)'\063',
1092 (u_char)'\064', (u_char)'\065', (u_char)'\066', (u_char)'\067',
1093 (u_char)'\070', (u_char)'\071', (u_char)'\072', (u_char)'\073',
1094 (u_char)'\074', (u_char)'\075', (u_char)'\076', (u_char)'\077',
1095 (u_char)'\100', (u_char)'\141', (u_char)'\142', (u_char)'\143',
1096 (u_char)'\144', (u_char)'\145', (u_char)'\146', (u_char)'\147',
1097 (u_char)'\150', (u_char)'\151', (u_char)'\152', (u_char)'\153',
1098 (u_char)'\154', (u_char)'\155', (u_char)'\156', (u_char)'\157',
1099 (u_char)'\160', (u_char)'\161', (u_char)'\162', (u_char)'\163',
1100 (u_char)'\164', (u_char)'\165', (u_char)'\166', (u_char)'\167',
1101 (u_char)'\170', (u_char)'\171', (u_char)'\172', (u_char)'\133',
1102 (u_char)'\134', (u_char)'\135', (u_char)'\136', (u_char)'\137',
1103 (u_char)'\140', (u_char)'\141', (u_char)'\142', (u_char)'\143',
1104 (u_char)'\144', (u_char)'\145', (u_char)'\146', (u_char)'\147',
1105 (u_char)'\150', (u_char)'\151', (u_char)'\152', (u_char)'\153',
1106 (u_char)'\154', (u_char)'\155', (u_char)'\156', (u_char)'\157',
1107 (u_char)'\160', (u_char)'\161', (u_char)'\162', (u_char)'\163',
1108 (u_char)'\164', (u_char)'\165', (u_char)'\166', (u_char)'\167',
1109 (u_char)'\170', (u_char)'\171', (u_char)'\172', (u_char)'\173',
1110 (u_char)'\174', (u_char)'\175', (u_char)'\176', (u_char)'\177',
1111 (u_char)'\200', (u_char)'\201', (u_char)'\202', (u_char)'\203',
1112 (u_char)'\204', (u_char)'\205', (u_char)'\206', (u_char)'\207',
1113 (u_char)'\210', (u_char)'\211', (u_char)'\212', (u_char)'\213',
1114 (u_char)'\214', (u_char)'\215', (u_char)'\216', (u_char)'\217',
1115 (u_char)'\220', (u_char)'\221', (u_char)'\222', (u_char)'\223',
1116 (u_char)'\224', (u_char)'\225', (u_char)'\226', (u_char)'\227',
1117 (u_char)'\230', (u_char)'\231', (u_char)'\232', (u_char)'\233',
1118 (u_char)'\234', (u_char)'\235', (u_char)'\236', (u_char)'\237',
1119 (u_char)'\240', (u_char)'\241', (u_char)'\242', (u_char)'\243',
1120 (u_char)'\244', (u_char)'\245', (u_char)'\246', (u_char)'\247',
1121 (u_char)'\250', (u_char)'\251', (u_char)'\252', (u_char)'\253',
1122 (u_char)'\254', (u_char)'\255', (u_char)'\256', (u_char)'\257',
1123 (u_char)'\260', (u_char)'\261', (u_char)'\262', (u_char)'\263',
1124 (u_char)'\264', (u_char)'\265', (u_char)'\266', (u_char)'\267',
1125 (u_char)'\270', (u_char)'\271', (u_char)'\272', (u_char)'\273',
1126 (u_char)'\274', (u_char)'\275', (u_char)'\276', (u_char)'\277',
1127 (u_char)'\300', (u_char)'\341', (u_char)'\342', (u_char)'\343',
1128 (u_char)'\344', (u_char)'\345', (u_char)'\346', (u_char)'\347',
1129 (u_char)'\350', (u_char)'\351', (u_char)'\352', (u_char)'\353',
1130 (u_char)'\354', (u_char)'\355', (u_char)'\356', (u_char)'\357',
1131 (u_char)'\360', (u_char)'\361', (u_char)'\362', (u_char)'\363',
1132 (u_char)'\364', (u_char)'\365', (u_char)'\366', (u_char)'\367',
1133 (u_char)'\370', (u_char)'\371', (u_char)'\372', (u_char)'\333',
1134 (u_char)'\334', (u_char)'\335', (u_char)'\336', (u_char)'\337',
1135 (u_char)'\340', (u_char)'\341', (u_char)'\342', (u_char)'\343',
1136 (u_char)'\344', (u_char)'\345', (u_char)'\346', (u_char)'\347',
1137 (u_char)'\350', (u_char)'\351', (u_char)'\352', (u_char)'\353',
1138 (u_char)'\354', (u_char)'\355', (u_char)'\356', (u_char)'\357',
1139 (u_char)'\360', (u_char)'\361', (u_char)'\362', (u_char)'\363',
1140 (u_char)'\364', (u_char)'\365', (u_char)'\366', (u_char)'\367',
1141 (u_char)'\370', (u_char)'\371', (u_char)'\372', (u_char)'\373',
1142 (u_char)'\374', (u_char)'\375', (u_char)'\376', (u_char)'\377',
1143 };
1144
1145 int
1146 pcap_strcasecmp(const char *s1, const char *s2)
1147 {
1148 register const u_char *cm = charmap,
1149 *us1 = (const u_char *)s1,
1150 *us2 = (const u_char *)s2;
1151
1152 while (cm[*us1] == cm[*us2++])
1153 if (*us1++ == '\0')
1154 return(0);
1155 return (cm[*us1] - cm[*--us2]);
1156 }
1157
1158 struct dlt_choice {
1159 const char *name;
1160 const char *description;
1161 int dlt;
1162 };
1163
1164 #define DLT_CHOICE(code, description) { #code, description, code }
1165 #define DLT_CHOICE_SENTINEL { NULL, NULL, 0 }
1166
1167 static struct dlt_choice dlt_choices[] = {
1168 DLT_CHOICE(DLT_NULL, "BSD loopback"),
1169 DLT_CHOICE(DLT_EN10MB, "Ethernet"),
1170 DLT_CHOICE(DLT_IEEE802, "Token ring"),
1171 DLT_CHOICE(DLT_ARCNET, "BSD ARCNET"),
1172 DLT_CHOICE(DLT_SLIP, "SLIP"),
1173 DLT_CHOICE(DLT_PPP, "PPP"),
1174 DLT_CHOICE(DLT_FDDI, "FDDI"),
1175 DLT_CHOICE(DLT_ATM_RFC1483, "RFC 1483 LLC-encapsulated ATM"),
1176 DLT_CHOICE(DLT_RAW, "Raw IP"),
1177 DLT_CHOICE(DLT_SLIP_BSDOS, "BSD/OS SLIP"),
1178 DLT_CHOICE(DLT_PPP_BSDOS, "BSD/OS PPP"),
1179 DLT_CHOICE(DLT_ATM_CLIP, "Linux Classical IP-over-ATM"),
1180 DLT_CHOICE(DLT_PPP_SERIAL, "PPP over serial"),
1181 DLT_CHOICE(DLT_PPP_ETHER, "PPPoE"),
1182 DLT_CHOICE(DLT_SYMANTEC_FIREWALL, "Symantec Firewall"),
1183 DLT_CHOICE(DLT_C_HDLC, "Cisco HDLC"),
1184 DLT_CHOICE(DLT_IEEE802_11, "802.11"),
1185 DLT_CHOICE(DLT_FRELAY, "Frame Relay"),
1186 DLT_CHOICE(DLT_LOOP, "OpenBSD loopback"),
1187 DLT_CHOICE(DLT_ENC, "OpenBSD encapsulated IP"),
1188 DLT_CHOICE(DLT_LINUX_SLL, "Linux cooked"),
1189 DLT_CHOICE(DLT_LTALK, "Localtalk"),
1190 DLT_CHOICE(DLT_PFLOG, "OpenBSD pflog file"),
1191 DLT_CHOICE(DLT_PFSYNC, "Packet filter state syncing"),
1192 DLT_CHOICE(DLT_PRISM_HEADER, "802.11 plus Prism header"),
1193 DLT_CHOICE(DLT_IP_OVER_FC, "RFC 2625 IP-over-Fibre Channel"),
1194 DLT_CHOICE(DLT_SUNATM, "Sun raw ATM"),
1195 DLT_CHOICE(DLT_IEEE802_11_RADIO, "802.11 plus radiotap header"),
1196 DLT_CHOICE(DLT_ARCNET_LINUX, "Linux ARCNET"),
1197 DLT_CHOICE(DLT_JUNIPER_MLPPP, "Juniper Multi-Link PPP"),
1198 DLT_CHOICE(DLT_JUNIPER_MLFR, "Juniper Multi-Link Frame Relay"),
1199 DLT_CHOICE(DLT_JUNIPER_ES, "Juniper Encryption Services PIC"),
1200 DLT_CHOICE(DLT_JUNIPER_GGSN, "Juniper GGSN PIC"),
1201 DLT_CHOICE(DLT_JUNIPER_MFR, "Juniper FRF.16 Frame Relay"),
1202 DLT_CHOICE(DLT_JUNIPER_ATM2, "Juniper ATM2 PIC"),
1203 DLT_CHOICE(DLT_JUNIPER_SERVICES, "Juniper Advanced Services PIC"),
1204 DLT_CHOICE(DLT_JUNIPER_ATM1, "Juniper ATM1 PIC"),
1205 DLT_CHOICE(DLT_APPLE_IP_OVER_IEEE1394, "Apple IP-over-IEEE 1394"),
1206 DLT_CHOICE(DLT_MTP2_WITH_PHDR, "SS7 MTP2 with Pseudo-header"),
1207 DLT_CHOICE(DLT_MTP2, "SS7 MTP2"),
1208 DLT_CHOICE(DLT_MTP3, "SS7 MTP3"),
1209 DLT_CHOICE(DLT_SCCP, "SS7 SCCP"),
1210 DLT_CHOICE(DLT_DOCSIS, "DOCSIS"),
1211 DLT_CHOICE(DLT_LINUX_IRDA, "Linux IrDA"),
1212 DLT_CHOICE(DLT_IEEE802_11_RADIO_AVS, "802.11 plus AVS radio information header"),
1213 DLT_CHOICE(DLT_JUNIPER_MONITOR, "Juniper Passive Monitor PIC"),
1214 DLT_CHOICE(DLT_BACNET_MS_TP, "BACnet MS/TP"),
1215 DLT_CHOICE(DLT_PPP_PPPD, "PPP for pppd, with direction flag"),
1216 DLT_CHOICE(DLT_JUNIPER_PPPOE, "Juniper PPPoE"),
1217 DLT_CHOICE(DLT_JUNIPER_PPPOE_ATM, "Juniper PPPoE/ATM"),
1218 DLT_CHOICE(DLT_GPRS_LLC, "GPRS LLC"),
1219 DLT_CHOICE(DLT_GPF_T, "GPF-T"),
1220 DLT_CHOICE(DLT_GPF_F, "GPF-F"),
1221 DLT_CHOICE(DLT_JUNIPER_PIC_PEER, "Juniper PIC Peer"),
1222 DLT_CHOICE(DLT_ERF_ETH, "Ethernet with Endace ERF header"),
1223 DLT_CHOICE(DLT_ERF_POS, "Packet-over-SONET with Endace ERF header"),
1224 DLT_CHOICE(DLT_LINUX_LAPD, "Linux vISDN LAPD"),
1225 DLT_CHOICE(DLT_JUNIPER_ETHER, "Juniper Ethernet"),
1226 DLT_CHOICE(DLT_JUNIPER_PPP, "Juniper PPP"),
1227 DLT_CHOICE(DLT_JUNIPER_FRELAY, "Juniper Frame Relay"),
1228 DLT_CHOICE(DLT_JUNIPER_CHDLC, "Juniper C-HDLC"),
1229 DLT_CHOICE(DLT_MFR, "FRF.16 Frame Relay"),
1230 DLT_CHOICE(DLT_JUNIPER_VP, "Juniper Voice PIC"),
1231 DLT_CHOICE(DLT_A429, "Arinc 429"),
1232 DLT_CHOICE(DLT_A653_ICM, "Arinc 653 Interpartition Communication"),
1233 DLT_CHOICE(DLT_USB_FREEBSD, "USB with FreeBSD header"),
1234 DLT_CHOICE(DLT_BLUETOOTH_HCI_H4, "Bluetooth HCI UART transport layer"),
1235 DLT_CHOICE(DLT_IEEE802_16_MAC_CPS, "IEEE 802.16 MAC Common Part Sublayer"),
1236 DLT_CHOICE(DLT_USB_LINUX, "USB with Linux header"),
1237 DLT_CHOICE(DLT_CAN20B, "Controller Area Network (CAN) v. 2.0B"),
1238 DLT_CHOICE(DLT_IEEE802_15_4_LINUX, "IEEE 802.15.4 with Linux padding"),
1239 DLT_CHOICE(DLT_PPI, "Per-Packet Information"),
1240 DLT_CHOICE(DLT_IEEE802_16_MAC_CPS_RADIO, "IEEE 802.16 MAC Common Part Sublayer plus radiotap header"),
1241 DLT_CHOICE(DLT_JUNIPER_ISM, "Juniper Integrated Service Module"),
1242 DLT_CHOICE(DLT_IEEE802_15_4, "IEEE 802.15.4 with FCS"),
1243 DLT_CHOICE(DLT_SITA, "SITA pseudo-header"),
1244 DLT_CHOICE(DLT_ERF, "Endace ERF header"),
1245 DLT_CHOICE(DLT_RAIF1, "Ethernet with u10 Networks pseudo-header"),
1246 DLT_CHOICE(DLT_IPMB, "IPMB"),
1247 DLT_CHOICE(DLT_JUNIPER_ST, "Juniper Secure Tunnel"),
1248 DLT_CHOICE(DLT_BLUETOOTH_HCI_H4_WITH_PHDR, "Bluetooth HCI UART transport layer plus pseudo-header"),
1249 DLT_CHOICE(DLT_AX25_KISS, "AX.25 with KISS header"),
1250 DLT_CHOICE(DLT_IEEE802_15_4_NONASK_PHY, "IEEE 802.15.4 with non-ASK PHY data"),
1251 DLT_CHOICE(DLT_MPLS, "MPLS with label as link-layer header"),
1252 DLT_CHOICE(DLT_LINUX_EVDEV, "Linux evdev events"),
1253 DLT_CHOICE(DLT_USB_LINUX_MMAPPED, "USB with padded Linux header"),
1254 DLT_CHOICE(DLT_DECT, "DECT"),
1255 DLT_CHOICE(DLT_AOS, "AOS Space Data Link protocol"),
1256 DLT_CHOICE(DLT_WIHART, "Wireless HART"),
1257 DLT_CHOICE(DLT_FC_2, "Fibre Channel FC-2"),
1258 DLT_CHOICE(DLT_FC_2_WITH_FRAME_DELIMS, "Fibre Channel FC-2 with frame delimiters"),
1259 DLT_CHOICE(DLT_IPNET, "Solaris ipnet"),
1260 DLT_CHOICE(DLT_CAN_SOCKETCAN, "CAN-bus with SocketCAN headers"),
1261 DLT_CHOICE(DLT_IPV4, "Raw IPv4"),
1262 DLT_CHOICE(DLT_IPV6, "Raw IPv6"),
1263 DLT_CHOICE(DLT_IEEE802_15_4_NOFCS, "IEEE 802.15.4 without FCS"),
1264 DLT_CHOICE(DLT_DBUS, "D-Bus"),
1265 DLT_CHOICE(DLT_JUNIPER_VS, "Juniper Virtual Server"),
1266 DLT_CHOICE(DLT_JUNIPER_SRX_E2E, "Juniper SRX E2E"),
1267 DLT_CHOICE(DLT_JUNIPER_FIBRECHANNEL, "Juniper Fibre Channel"),
1268 DLT_CHOICE(DLT_DVB_CI, "DVB-CI"),
1269 DLT_CHOICE(DLT_MUX27010, "MUX27010"),
1270 DLT_CHOICE(DLT_STANAG_5066_D_PDU, "STANAG 5066 D_PDUs"),
1271 DLT_CHOICE(DLT_JUNIPER_ATM_CEMIC, "Juniper ATM CEMIC"),
1272 DLT_CHOICE(DLT_NFLOG, "Linux netfilter log messages"),
1273 DLT_CHOICE(DLT_NETANALYZER, "Ethernet with Hilscher netANALYZER pseudo-header"),
1274 DLT_CHOICE(DLT_NETANALYZER_TRANSPARENT, "Ethernet with Hilscher netANALYZER pseudo-header and with preamble and SFD"),
1275 DLT_CHOICE(DLT_IPOIB, "RFC 4391 IP-over-Infiniband"),
1276 DLT_CHOICE(DLT_MPEG_2_TS, "MPEG-2 transport stream"),
1277 DLT_CHOICE(DLT_NG40, "ng40 protocol tester Iub/Iur"),
1278 DLT_CHOICE(DLT_NFC_LLCP, "NFC LLCP PDUs with pseudo-header"),
1279 DLT_CHOICE(DLT_INFINIBAND, "InfiniBand"),
1280 DLT_CHOICE(DLT_SCTP, "SCTP"),
1281 DLT_CHOICE(DLT_USBPCAP, "USB with USBPcap header"),
1282 DLT_CHOICE(DLT_RTAC_SERIAL, "Schweitzer Engineering Laboratories RTAC packets"),
1283 DLT_CHOICE(DLT_BLUETOOTH_LE_LL, "Bluetooth Low Energy air interface"),
1284 DLT_CHOICE(DLT_NETLINK, "Linux netlink"),
1285 DLT_CHOICE(DLT_BLUETOOTH_LINUX_MONITOR, "Bluetooth Linux Monitor"),
1286 DLT_CHOICE(DLT_BLUETOOTH_BREDR_BB, "Bluetooth Basic Rate/Enhanced Data Rate baseband packets"),
1287 DLT_CHOICE(DLT_BLUETOOTH_LE_LL_WITH_PHDR, "Bluetooth Low Energy air interface with pseudo-header"),
1288 DLT_CHOICE(DLT_PROFIBUS_DL, "PROFIBUS data link layer"),
1289 DLT_CHOICE(DLT_PKTAP, "Apple DLT_PKTAP"),
1290 DLT_CHOICE(DLT_EPON, "Ethernet with 802.3 Clause 65 EPON preamble"),
1291 DLT_CHOICE_SENTINEL
1292 };
1293
1294 int
1295 pcap_datalink_name_to_val(const char *name)
1296 {
1297 int i;
1298
1299 for (i = 0; dlt_choices[i].name != NULL; i++) {
1300 if (pcap_strcasecmp(dlt_choices[i].name + sizeof("DLT_") - 1,
1301 name) == 0)
1302 return (dlt_choices[i].dlt);
1303 }
1304 return (-1);
1305 }
1306
1307 const char *
1308 pcap_datalink_val_to_name(int dlt)
1309 {
1310 int i;
1311
1312 for (i = 0; dlt_choices[i].name != NULL; i++) {
1313 if (dlt_choices[i].dlt == dlt)
1314 return (dlt_choices[i].name + sizeof("DLT_") - 1);
1315 }
1316 return (NULL);
1317 }
1318
1319 const char *
1320 pcap_datalink_val_to_description(int dlt)
1321 {
1322 int i;
1323
1324 for (i = 0; dlt_choices[i].name != NULL; i++) {
1325 if (dlt_choices[i].dlt == dlt)
1326 return (dlt_choices[i].description);
1327 }
1328 return (NULL);
1329 }
1330
1331 struct tstamp_type_choice {
1332 const char *name;
1333 const char *description;
1334 int type;
1335 };
1336
1337 static struct tstamp_type_choice tstamp_type_choices[] = {
1338 { "host", "Host", PCAP_TSTAMP_HOST },
1339 { "host_lowprec", "Host, low precision", PCAP_TSTAMP_HOST_LOWPREC },
1340 { "host_hiprec", "Host, high precision", PCAP_TSTAMP_HOST_HIPREC },
1341 { "adapter", "Adapter", PCAP_TSTAMP_ADAPTER },
1342 { "adapter_unsynced", "Adapter, not synced with system time", PCAP_TSTAMP_ADAPTER_UNSYNCED },
1343 { NULL, NULL, 0 }
1344 };
1345
1346 int
1347 pcap_tstamp_type_name_to_val(const char *name)
1348 {
1349 int i;
1350
1351 for (i = 0; tstamp_type_choices[i].name != NULL; i++) {
1352 if (pcap_strcasecmp(tstamp_type_choices[i].name, name) == 0)
1353 return (tstamp_type_choices[i].type);
1354 }
1355 return (PCAP_ERROR);
1356 }
1357
1358 const char *
1359 pcap_tstamp_type_val_to_name(int tstamp_type)
1360 {
1361 int i;
1362
1363 for (i = 0; tstamp_type_choices[i].name != NULL; i++) {
1364 if (tstamp_type_choices[i].type == tstamp_type)
1365 return (tstamp_type_choices[i].name);
1366 }
1367 return (NULL);
1368 }
1369
1370 const char *
1371 pcap_tstamp_type_val_to_description(int tstamp_type)
1372 {
1373 int i;
1374
1375 for (i = 0; tstamp_type_choices[i].name != NULL; i++) {
1376 if (tstamp_type_choices[i].type == tstamp_type)
1377 return (tstamp_type_choices[i].description);
1378 }
1379 return (NULL);
1380 }
1381
1382 int
1383 pcap_snapshot(pcap_t *p)
1384 {
1385 if (!p->activated)
1386 return (PCAP_ERROR_NOT_ACTIVATED);
1387 return (p->snapshot);
1388 }
1389
1390 int
1391 pcap_is_swapped(pcap_t *p)
1392 {
1393 if (!p->activated)
1394 return (PCAP_ERROR_NOT_ACTIVATED);
1395 return (p->swapped);
1396 }
1397
1398 int
1399 pcap_major_version(pcap_t *p)
1400 {
1401 if (!p->activated)
1402 return (PCAP_ERROR_NOT_ACTIVATED);
1403 return (p->version_major);
1404 }
1405
1406 int
1407 pcap_minor_version(pcap_t *p)
1408 {
1409 if (!p->activated)
1410 return (PCAP_ERROR_NOT_ACTIVATED);
1411 return (p->version_minor);
1412 }
1413
1414 FILE *
1415 pcap_file(pcap_t *p)
1416 {
1417 return (p->rfile);
1418 }
1419
1420 int
1421 pcap_fileno(pcap_t *p)
1422 {
1423 #ifndef _WIN32
1424 return (p->fd);
1425 #else
1426 if (p->adapter != NULL)
1427 return ((int)(DWORD)p->adapter->hFile);
1428 else
1429 return (PCAP_ERROR);
1430 #endif
1431 }
1432
1433 #if !defined(_WIN32) && !defined(MSDOS)
1434 int
1435 pcap_get_selectable_fd(pcap_t *p)
1436 {
1437 return (p->selectable_fd);
1438 }
1439 #endif
1440
1441 void
1442 pcap_perror(pcap_t *p, const char *prefix)
1443 {
1444 fprintf(stderr, "%s: %s\n", prefix, p->errbuf);
1445 }
1446
1447 char *
1448 pcap_geterr(pcap_t *p)
1449 {
1450 return (p->errbuf);
1451 }
1452
1453 int
1454 pcap_getnonblock(pcap_t *p, char *errbuf)
1455 {
1456 int ret;
1457
1458 ret = p->getnonblock_op(p, errbuf);
1459 if (ret == -1) {
1460 /*
1461 * In case somebody depended on the bug wherein
1462 * the error message was put into p->errbuf
1463 * by pcap_getnonblock_fd().
1464 */
1465 strlcpy(p->errbuf, errbuf, PCAP_ERRBUF_SIZE);
1466 }
1467 return (ret);
1468 }
1469
1470 /*
1471 * Get the current non-blocking mode setting, under the assumption that
1472 * it's just the standard POSIX non-blocking flag.
1473 */
1474 #if !defined(_WIN32) && !defined(MSDOS)
1475 int
1476 pcap_getnonblock_fd(pcap_t *p, char *errbuf)
1477 {
1478 int fdflags;
1479
1480 fdflags = fcntl(p->fd, F_GETFL, 0);
1481 if (fdflags == -1) {
1482 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s",
1483 pcap_strerror(errno));
1484 return (-1);
1485 }
1486 if (fdflags & O_NONBLOCK)
1487 return (1);
1488 else
1489 return (0);
1490 }
1491 #endif
1492
1493 int
1494 pcap_setnonblock(pcap_t *p, int nonblock, char *errbuf)
1495 {
1496 int ret;
1497
1498 ret = p->setnonblock_op(p, nonblock, errbuf);
1499 if (ret == -1) {
1500 /*
1501 * In case somebody depended on the bug wherein
1502 * the error message was put into p->errbuf
1503 * by pcap_setnonblock_fd().
1504 */
1505 strlcpy(p->errbuf, errbuf, PCAP_ERRBUF_SIZE);
1506 }
1507 return (ret);
1508 }
1509
1510 #if !defined(_WIN32) && !defined(MSDOS)
1511 /*
1512 * Set non-blocking mode, under the assumption that it's just the
1513 * standard POSIX non-blocking flag. (This can be called by the
1514 * per-platform non-blocking-mode routine if that routine also
1515 * needs to do some additional work.)
1516 */
1517 int
1518 pcap_setnonblock_fd(pcap_t *p, int nonblock, char *errbuf)
1519 {
1520 int fdflags;
1521
1522 fdflags = fcntl(p->fd, F_GETFL, 0);
1523 if (fdflags == -1) {
1524 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s",
1525 pcap_strerror(errno));
1526 return (-1);
1527 }
1528 if (nonblock)
1529 fdflags |= O_NONBLOCK;
1530 else
1531 fdflags &= ~O_NONBLOCK;
1532 if (fcntl(p->fd, F_SETFL, fdflags) == -1) {
1533 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "F_SETFL: %s",
1534 pcap_strerror(errno));
1535 return (-1);
1536 }
1537 return (0);
1538 }
1539 #endif
1540
1541 #ifdef _WIN32
1542 /*
1543 * Generate a string for a Win32-specific error (i.e. an error generated when
1544 * calling a Win32 API).
1545 * For errors occurred during standard C calls, we still use pcap_strerror()
1546 */
1547 void
1548 pcap_win32_err_to_str(DWORD error, char *errbuf)
1549 {
1550 size_t errlen;
1551 char *p;
1552
1553 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, 0, errbuf,
1554 PCAP_ERRBUF_SIZE, NULL);
1555
1556 /*
1557 * "FormatMessage()" "helpfully" sticks CR/LF at the end of the
1558 * message. Get rid of it.
1559 */
1560 errlen = strlen(errbuf);
1561 if (errlen >= 2) {
1562 errbuf[errlen - 1] = '\0';
1563 errbuf[errlen - 2] = '\0';
1564 }
1565 p = strchr(errbuf, '\0');
1566 pcap_snprintf (p, PCAP_ERRBUF_SIZE+1-(p-errbuf), " (%lu)", error);
1567 }
1568 #endif
1569
1570 /*
1571 * Generate error strings for PCAP_ERROR_ and PCAP_WARNING_ values.
1572 */
1573 const char *
1574 pcap_statustostr(int errnum)
1575 {
1576 static char ebuf[15+10+1];
1577
1578 switch (errnum) {
1579
1580 case PCAP_WARNING:
1581 return("Generic warning");
1582
1583 case PCAP_WARNING_TSTAMP_TYPE_NOTSUP:
1584 return ("That type of time stamp is not supported by that device");
1585
1586 case PCAP_WARNING_PROMISC_NOTSUP:
1587 return ("That device doesn't support promiscuous mode");
1588
1589 case PCAP_ERROR:
1590 return("Generic error");
1591
1592 case PCAP_ERROR_BREAK:
1593 return("Loop terminated by pcap_breakloop");
1594
1595 case PCAP_ERROR_NOT_ACTIVATED:
1596 return("The pcap_t has not been activated");
1597
1598 case PCAP_ERROR_ACTIVATED:
1599 return ("The setting can't be changed after the pcap_t is activated");
1600
1601 case PCAP_ERROR_NO_SUCH_DEVICE:
1602 return ("No such device exists");
1603
1604 case PCAP_ERROR_RFMON_NOTSUP:
1605 return ("That device doesn't support monitor mode");
1606
1607 case PCAP_ERROR_NOT_RFMON:
1608 return ("That operation is supported only in monitor mode");
1609
1610 case PCAP_ERROR_PERM_DENIED:
1611 return ("You don't have permission to capture on that device");
1612
1613 case PCAP_ERROR_IFACE_NOT_UP:
1614 return ("That device is not up");
1615
1616 case PCAP_ERROR_CANTSET_TSTAMP_TYPE:
1617 return ("That device doesn't support setting the time stamp type");
1618
1619 case PCAP_ERROR_PROMISC_PERM_DENIED:
1620 return ("You don't have permission to capture in promiscuous mode on that device");
1621
1622 case PCAP_ERROR_TSTAMP_PRECISION_NOTSUP:
1623 return ("That device doesn't support that time stamp precision");
1624 }
1625 (void)pcap_snprintf(ebuf, sizeof ebuf, "Unknown error: %d", errnum);
1626 return(ebuf);
1627 }
1628
1629 /*
1630 * Not all systems have strerror().
1631 */
1632 const char *
1633 pcap_strerror(int errnum)
1634 {
1635 #ifdef HAVE_STRERROR
1636 return (strerror(errnum));
1637 #else
1638 extern int sys_nerr;
1639 extern const char *const sys_errlist[];
1640 static char ebuf[15+10+1];
1641
1642 if ((unsigned int)errnum < sys_nerr)
1643 return ((char *)sys_errlist[errnum]);
1644 (void)pcap_snprintf(ebuf, sizeof ebuf, "Unknown error: %d", errnum);
1645 return(ebuf);
1646 #endif
1647 }
1648
1649 int
1650 pcap_setfilter(pcap_t *p, struct bpf_program *fp)
1651 {
1652 return (p->setfilter_op(p, fp));
1653 }
1654
1655 /*
1656 * Set direction flag, which controls whether we accept only incoming
1657 * packets, only outgoing packets, or both.
1658 * Note that, depending on the platform, some or all direction arguments
1659 * might not be supported.
1660 */
1661 int
1662 pcap_setdirection(pcap_t *p, pcap_direction_t d)
1663 {
1664 if (p->setdirection_op == NULL) {
1665 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1666 "Setting direction is not implemented on this platform");
1667 return (-1);
1668 } else
1669 return (p->setdirection_op(p, d));
1670 }
1671
1672 int
1673 pcap_stats(pcap_t *p, struct pcap_stat *ps)
1674 {
1675 return (p->stats_op(p, ps));
1676 }
1677
1678 static int
1679 pcap_stats_dead(pcap_t *p, struct pcap_stat *ps _U_)
1680 {
1681 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1682 "Statistics aren't available from a pcap_open_dead pcap_t");
1683 return (-1);
1684 }
1685
1686 #ifdef _WIN32
1687 struct pcap_stat *
1688 pcap_stats_ex(pcap_t *p, int *pcap_stat_size)
1689 {
1690 return (p->stats_ex_op(p, pcap_stat_size));
1691 }
1692
1693 int
1694 pcap_setbuff(pcap_t *p, int dim)
1695 {
1696 return (p->setbuff_op(p, dim));
1697 }
1698
1699 static int
1700 pcap_setbuff_dead(pcap_t *p, int dim)
1701 {
1702 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1703 "The kernel buffer size cannot be set on a pcap_open_dead pcap_t");
1704 return (-1);
1705 }
1706
1707 int
1708 pcap_setmode(pcap_t *p, int mode)
1709 {
1710 return (p->setmode_op(p, mode));
1711 }
1712
1713 static int
1714 pcap_setmode_dead(pcap_t *p, int mode)
1715 {
1716 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1717 "impossible to set mode on a pcap_open_dead pcap_t");
1718 return (-1);
1719 }
1720
1721 int
1722 pcap_setmintocopy(pcap_t *p, int size)
1723 {
1724 return (p->setmintocopy_op(p, size));
1725 }
1726
1727 static int
1728 pcap_setmintocopy_dead(pcap_t *p, int size)
1729 {
1730 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1731 "The mintocopy parameter cannot be set on a pcap_open_dead pcap_t");
1732 return (-1);
1733 }
1734
1735 HANDLE
1736 pcap_getevent(pcap_t *p)
1737 {
1738 return (p->getevent_op(p));
1739 }
1740
1741 static HANDLE
1742 pcap_getevent_dead(pcap_t *p)
1743 {
1744 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1745 "A pcap_open_dead pcap_t has no event handle");
1746 return (INVALID_HANDLE_VALUE);
1747 }
1748
1749 int
1750 pcap_oid_get_request(pcap_t *p, bpf_u_int32 oid, void *data, size_t *lenp)
1751 {
1752 return (p->oid_get_request_op(p, oid, data, lenp));
1753 }
1754
1755 static int
1756 pcap_oid_get_request_dead(pcap_t *p, bpf_u_int32 oid _U_, void *data _U_,
1757 size_t *lenp _U_)
1758 {
1759 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1760 "An OID get request cannot be performed on a pcap_open_dead pcap_t");
1761 return (PCAP_ERROR);
1762 }
1763
1764 int
1765 pcap_oid_set_request(pcap_t *p, bpf_u_int32 oid, const void *data, size_t *lenp)
1766 {
1767 return (p->oid_set_request_op(p, oid, data, lenp));
1768 }
1769
1770 static int
1771 pcap_oid_set_request_dead(pcap_t *p, bpf_u_int32 oid _U_, const void *data _U_,
1772 size_t *lenp _U_)
1773 {
1774 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1775 "An OID set request cannot be performed on a pcap_open_dead pcap_t");
1776 return (PCAP_ERROR);
1777 }
1778
1779 pcap_send_queue *
1780 pcap_sendqueue_alloc(u_int memsize)
1781 {
1782 pcap_send_queue *tqueue;
1783
1784 /* Allocate the queue */
1785 tqueue = (pcap_send_queue *)malloc(sizeof(pcap_send_queue));
1786 if (tqueue == NULL){
1787 return (NULL);
1788 }
1789
1790 /* Allocate the buffer */
1791 tqueue->buffer = (char *)malloc(memsize);
1792 if (tqueue->buffer == NULL) {
1793 free(tqueue);
1794 return (NULL);
1795 }
1796
1797 tqueue->maxlen = memsize;
1798 tqueue->len = 0;
1799
1800 return (tqueue);
1801 }
1802
1803 void
1804 pcap_sendqueue_destroy(pcap_send_queue *queue)
1805 {
1806 free(queue->buffer);
1807 free(queue);
1808 }
1809
1810 int
1811 pcap_sendqueue_queue(pcap_send_queue *queue, const struct pcap_pkthdr *pkt_header, const u_char *pkt_data)
1812 {
1813 if (queue->len + sizeof(struct pcap_pkthdr) + pkt_header->caplen > queue->maxlen){
1814 return (-1);
1815 }
1816
1817 /* Copy the pcap_pkthdr header*/
1818 memcpy(queue->buffer + queue->len, pkt_header, sizeof(struct pcap_pkthdr));
1819 queue->len += sizeof(struct pcap_pkthdr);
1820
1821 /* copy the packet */
1822 memcpy(queue->buffer + queue->len, pkt_data, pkt_header->caplen);
1823 queue->len += pkt_header->caplen;
1824
1825 return (0);
1826 }
1827
1828 u_int
1829 pcap_sendqueue_transmit(pcap_t *p, pcap_send_queue *queue, int sync)
1830 {
1831 return (p->sendqueue_transmit_op(p, queue, sync));
1832 }
1833
1834 static u_int
1835 pcap_sendqueue_transmit_dead(pcap_t *p, pcap_send_queue *queue, int sync)
1836 {
1837 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1838 "Packets cannot be transmitted on a pcap_open_dead pcap_t");
1839 return (0);
1840 }
1841
1842 int
1843 pcap_setuserbuffer(pcap_t *p, int size)
1844 {
1845 return (p->setuserbuffer_op(p, size));
1846 }
1847
1848 static int
1849 pcap_setuserbuffer_dead(pcap_t *p, int size)
1850 {
1851 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1852 "The user buffer cannot be set on a pcap_open_dead pcap_t");
1853 return (-1);
1854 }
1855
1856 int
1857 pcap_live_dump(pcap_t *p, char *filename, int maxsize, int maxpacks)
1858 {
1859 return (p->live_dump_op(p, filename, maxsize, maxpacks));
1860 }
1861
1862 static int
1863 pcap_live_dump_dead(pcap_t *p, char *filename, int maxsize, int maxpacks)
1864 {
1865 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1866 "Live packet dumping cannot be performed on a pcap_open_dead pcap_t");
1867 return (-1);
1868 }
1869
1870 int
1871 pcap_live_dump_ended(pcap_t *p, int sync)
1872 {
1873 return (p->live_dump_ended_op(p, sync));
1874 }
1875
1876 static int
1877 pcap_live_dump_ended_dead(pcap_t *p, int sync)
1878 {
1879 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1880 "Live packet dumping cannot be performed on a pcap_open_dead pcap_t");
1881 return (-1);
1882 }
1883
1884 PAirpcapHandle
1885 pcap_get_airpcap_handle(pcap_t *p)
1886 {
1887 PAirpcapHandle handle;
1888
1889 handle = p->get_airpcap_handle_op(p);
1890 if (handle == NULL) {
1891 (void)pcap_snprintf(p->errbuf, sizeof(p->errbuf),
1892 "This isn't an AirPcap device");
1893 }
1894 return (handle);
1895 }
1896
1897 static PAirpcapHandle
1898 pcap_get_airpcap_handle_dead(pcap_t *p)
1899 {
1900 return (NULL);
1901 }
1902 #endif
1903
1904 /*
1905 * On some platforms, we need to clean up promiscuous or monitor mode
1906 * when we close a device - and we want that to happen even if the
1907 * application just exits without explicitl closing devices.
1908 * On those platforms, we need to register a "close all the pcaps"
1909 * routine to be called when we exit, and need to maintain a list of
1910 * pcaps that need to be closed to clean up modes.
1911 *
1912 * XXX - not thread-safe.
1913 */
1914
1915 /*
1916 * List of pcaps on which we've done something that needs to be
1917 * cleaned up.
1918 * If there are any such pcaps, we arrange to call "pcap_close_all()"
1919 * when we exit, and have it close all of them.
1920 */
1921 static struct pcap *pcaps_to_close;
1922
1923 /*
1924 * TRUE if we've already called "atexit()" to cause "pcap_close_all()" to
1925 * be called on exit.
1926 */
1927 static int did_atexit;
1928
1929 static void
1930 pcap_close_all(void)
1931 {
1932 struct pcap *handle;
1933
1934 while ((handle = pcaps_to_close) != NULL)
1935 pcap_close(handle);
1936 }
1937
1938 int
1939 pcap_do_addexit(pcap_t *p)
1940 {
1941 /*
1942 * If we haven't already done so, arrange to have
1943 * "pcap_close_all()" called when we exit.
1944 */
1945 if (!did_atexit) {
1946 if (atexit(pcap_close_all) != 0) {
1947 /*
1948 * "atexit()" failed; let our caller know.
1949 */
1950 strlcpy(p->errbuf, "atexit failed", PCAP_ERRBUF_SIZE);
1951 return (0);
1952 }
1953 did_atexit = 1;
1954 }
1955 return (1);
1956 }
1957
1958 void
1959 pcap_add_to_pcaps_to_close(pcap_t *p)
1960 {
1961 p->next = pcaps_to_close;
1962 pcaps_to_close = p;
1963 }
1964
1965 void
1966 pcap_remove_from_pcaps_to_close(pcap_t *p)
1967 {
1968 pcap_t *pc, *prevpc;
1969
1970 for (pc = pcaps_to_close, prevpc = NULL; pc != NULL;
1971 prevpc = pc, pc = pc->next) {
1972 if (pc == p) {
1973 /*
1974 * Found it. Remove it from the list.
1975 */
1976 if (prevpc == NULL) {
1977 /*
1978 * It was at the head of the list.
1979 */
1980 pcaps_to_close = pc->next;
1981 } else {
1982 /*
1983 * It was in the middle of the list.
1984 */
1985 prevpc->next = pc->next;
1986 }
1987 break;
1988 }
1989 }
1990 }
1991
1992 void
1993 pcap_cleanup_live_common(pcap_t *p)
1994 {
1995 if (p->buffer != NULL) {
1996 free(p->buffer);
1997 p->buffer = NULL;
1998 }
1999 if (p->dlt_list != NULL) {
2000 free(p->dlt_list);
2001 p->dlt_list = NULL;
2002 p->dlt_count = 0;
2003 }
2004 if (p->tstamp_type_list != NULL) {
2005 free(p->tstamp_type_list);
2006 p->tstamp_type_list = NULL;
2007 p->tstamp_type_count = 0;
2008 }
2009 if (p->tstamp_precision_list != NULL) {
2010 free(p->tstamp_precision_list);
2011 p->tstamp_precision_list = NULL;
2012 p->tstamp_precision_count = 0;
2013 }
2014 pcap_freecode(&p->fcode);
2015 #if !defined(_WIN32) && !defined(MSDOS)
2016 if (p->fd >= 0) {
2017 close(p->fd);
2018 p->fd = -1;
2019 }
2020 p->selectable_fd = -1;
2021 #endif
2022 }
2023
2024 static void
2025 pcap_cleanup_dead(pcap_t *p _U_)
2026 {
2027 /* Nothing to do. */
2028 }
2029
2030 pcap_t *
2031 pcap_open_dead_with_tstamp_precision(int linktype, int snaplen, u_int precision)
2032 {
2033 pcap_t *p;
2034
2035 switch (precision) {
2036
2037 case PCAP_TSTAMP_PRECISION_MICRO:
2038 case PCAP_TSTAMP_PRECISION_NANO:
2039 break;
2040
2041 default:
2042 return NULL;
2043 }
2044 p = malloc(sizeof(*p));
2045 if (p == NULL)
2046 return NULL;
2047 memset (p, 0, sizeof(*p));
2048 p->snapshot = snaplen;
2049 p->linktype = linktype;
2050 p->opt.tstamp_precision = precision;
2051 p->stats_op = pcap_stats_dead;
2052 #ifdef _WIN32
2053 p->stats_ex_op = (stats_ex_op_t)pcap_not_initialized_ptr;
2054 p->setbuff_op = pcap_setbuff_dead;
2055 p->setmode_op = pcap_setmode_dead;
2056 p->setmintocopy_op = pcap_setmintocopy_dead;
2057 p->getevent_op = pcap_getevent_dead;
2058 p->oid_get_request_op = pcap_oid_get_request_dead;
2059 p->oid_set_request_op = pcap_oid_set_request_dead;
2060 p->sendqueue_transmit_op = pcap_sendqueue_transmit_dead;
2061 p->setuserbuffer_op = pcap_setuserbuffer_dead;
2062 p->live_dump_op = pcap_live_dump_dead;
2063 p->live_dump_ended_op = pcap_live_dump_ended_dead;
2064 p->get_airpcap_handle_op = pcap_get_airpcap_handle_dead;
2065 #endif
2066 p->cleanup_op = pcap_cleanup_dead;
2067
2068 /*
2069 * A "dead" pcap_t never requires special BPF code generation.
2070 */
2071 p->bpf_codegen_flags = 0;
2072
2073 p->activated = 1;
2074 return (p);
2075 }
2076
2077 pcap_t *
2078 pcap_open_dead(int linktype, int snaplen)
2079 {
2080 return (pcap_open_dead_with_tstamp_precision(linktype, snaplen,
2081 PCAP_TSTAMP_PRECISION_MICRO));
2082 }
2083
2084 /*
2085 * API compatible with WinPcap's "send a packet" routine - returns -1
2086 * on error, 0 otherwise.
2087 *
2088 * XXX - what if we get a short write?
2089 */
2090 int
2091 pcap_sendpacket(pcap_t *p, const u_char *buf, int size)
2092 {
2093 if (p->inject_op(p, buf, size) == -1)
2094 return (-1);
2095 return (0);
2096 }
2097
2098 /*
2099 * API compatible with OpenBSD's "send a packet" routine - returns -1 on
2100 * error, number of bytes written otherwise.
2101 */
2102 int
2103 pcap_inject(pcap_t *p, const void *buf, size_t size)
2104 {
2105 return (p->inject_op(p, buf, size));
2106 }
2107
2108 void
2109 pcap_close(pcap_t *p)
2110 {
2111 if (p->opt.device != NULL)
2112 free(p->opt.device);
2113 p->cleanup_op(p);
2114 free(p);
2115 }
2116
2117 /*
2118 * Given a BPF program, a pcap_pkthdr structure for a packet, and the raw
2119 * data for the packet, check whether the packet passes the filter.
2120 * Returns the return value of the filter program, which will be zero if
2121 * the packet doesn't pass and non-zero if the packet does pass.
2122 */
2123 int
2124 pcap_offline_filter(const struct bpf_program *fp, const struct pcap_pkthdr *h,
2125 const u_char *pkt)
2126 {
2127 const struct bpf_insn *fcode = fp->bf_insns;
2128
2129 if (fcode != NULL)
2130 return (bpf_filter(fcode, pkt, h->len, h->caplen));
2131 else
2132 return (0);
2133 }
2134
2135 /*
2136 * We make the version string static, and return a pointer to it, rather
2137 * than exporting the version string directly. On at least some UNIXes,
2138 * if you import data from a shared library into an program, the data is
2139 * bound into the program binary, so if the string in the version of the
2140 * library with which the program was linked isn't the same as the
2141 * string in the version of the library with which the program is being
2142 * run, various undesirable things may happen (warnings, the string
2143 * being the one from the version of the library with which the program
2144 * was linked, or even weirder things, such as the string being the one
2145 * from the library but being truncated).
2146 */
2147 #ifdef HAVE_VERSION_H
2148 #include "version.h"
2149 #else
2150 static const char pcap_version_string[] = "libpcap version 1.x.y";
2151 #endif
2152
2153 #ifdef _WIN32
2154 /*
2155 * XXX - it'd be nice if we could somehow generate the WinPcap and libpcap
2156 * version numbers when building WinPcap. (It'd be nice to do so for
2157 * the packet.dll version number as well.)
2158 */
2159 static const char wpcap_version_string[] = "4.0";
2160 static const char pcap_version_string_fmt[] =
2161 "WinPcap version %s, based on %s";
2162 static const char pcap_version_string_packet_dll_fmt[] =
2163 "WinPcap version %s (packet.dll version %s), based on %s";
2164 static char *full_pcap_version_string;
2165
2166 const char *
2167 pcap_lib_version(void)
2168 {
2169 char *packet_version_string;
2170 size_t full_pcap_version_string_len;
2171
2172 if (full_pcap_version_string == NULL) {
2173 /*
2174 * Generate the version string.
2175 */
2176 packet_version_string = PacketGetVersion();
2177 if (strcmp(wpcap_version_string, packet_version_string) == 0) {
2178 /*
2179 * WinPcap version string and packet.dll version
2180 * string are the same; just report the WinPcap
2181 * version.
2182 */
2183 full_pcap_version_string_len =
2184 (sizeof pcap_version_string_fmt - 4) +
2185 strlen(wpcap_version_string) +
2186 strlen(pcap_version_string);
2187 full_pcap_version_string =
2188 malloc(full_pcap_version_string_len);
2189 if (full_pcap_version_string == NULL)
2190 return (NULL);
2191 sprintf(full_pcap_version_string,
2192 pcap_version_string_fmt, wpcap_version_string,
2193 pcap_version_string);
2194 } else {
2195 /*
2196 * WinPcap version string and packet.dll version
2197 * string are different; that shouldn't be the
2198 * case (the two libraries should come from the
2199 * same version of WinPcap), so we report both
2200 * versions.
2201 */
2202 full_pcap_version_string_len =
2203 (sizeof pcap_version_string_packet_dll_fmt - 6) +
2204 strlen(wpcap_version_string) +
2205 strlen(packet_version_string) +
2206 strlen(pcap_version_string);
2207 full_pcap_version_string = malloc(full_pcap_version_string_len);
2208 if (full_pcap_version_string == NULL)
2209 return (NULL);
2210 sprintf(full_pcap_version_string,
2211 pcap_version_string_packet_dll_fmt,
2212 wpcap_version_string, packet_version_string,
2213 pcap_version_string);
2214 }
2215 }
2216 return (full_pcap_version_string);
2217 }
2218
2219 #elif defined(MSDOS)
2220
2221 static char *full_pcap_version_string;
2222
2223 const char *
2224 pcap_lib_version (void)
2225 {
2226 char *packet_version_string;
2227 size_t full_pcap_version_string_len;
2228 static char dospfx[] = "DOS-";
2229
2230 if (full_pcap_version_string == NULL) {
2231 /*
2232 * Generate the version string.
2233 */
2234 full_pcap_version_string_len =
2235 sizeof dospfx + strlen(pcap_version_string);
2236 full_pcap_version_string =
2237 malloc(full_pcap_version_string_len);
2238 if (full_pcap_version_string == NULL)
2239 return (NULL);
2240 strcpy(full_pcap_version_string, dospfx);
2241 strcat(full_pcap_version_string, pcap_version_string);
2242 }
2243 return (full_pcap_version_string);
2244 }
2245
2246 #else /* UN*X */
2247
2248 const char *
2249 pcap_lib_version(void)
2250 {
2251 return (pcap_version_string);
2252 }
2253 #endif
2254
2255 #ifdef YYDEBUG
2256 /*
2257 * Set the internal "debug printout" flag for the filter expression parser.
2258 * The code to print that stuff is present only if YYDEBUG is defined, so
2259 * the flag, and the routine to set it, are defined only if YYDEBUG is
2260 * defined.
2261 *
2262 * This is intended for libpcap developers, not for general use.
2263 * If you want to set these in a program, you'll have to declare this
2264 * routine yourself, with the appropriate DLL import attribute on Windows;
2265 * it's not declared in any header file, and won't be declared in any
2266 * header file provided by libpcap.
2267 */
2268 PCAP_API void pcap_set_parser_debug(int value);
2269
2270 PCAP_API_DEF void
2271 pcap_set_parser_debug(int value)
2272 {
2273 extern int pcap_debug;
2274
2275 pcap_debug = value;
2276 }
2277 #endif
2278
2279 #ifdef BDEBUG
2280 /*
2281 * Set the internal "debug printout" flag for the filter expression optimizer.
2282 * The code to print that stuff is present only if BDEBUG is defined, so
2283 * the flag, and the routine to set it, are defined only if BDEBUG is
2284 * defined.
2285 *
2286 * This is intended for libpcap developers, not for general use.
2287 * If you want to set these in a program, you'll have to declare this
2288 * routine yourself, with the appropriate DLL import attribute on Windows;
2289 * it's not declared in any header file, and won't be declared in any
2290 * header file provided by libpcap.
2291 */
2292 PCAP_API void pcap_set_optimizer_debug(int value);
2293
2294 PCAP_API_DEF void
2295 pcap_set_optimizer_debug(int value)
2296 {
2297 extern int pcap_optimizer_debug;
2298
2299 pcap_optimizer_debug = value;
2300 }
2301 #endif