]> The Tcpdump Group git mirrors - libpcap/blob - pcap.c
Added basic support for BACnet MS/TP
[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 #ifndef lint
35 static const char rcsid[] _U_ =
36 "@(#) $Header: /tcpdump/master/libpcap/pcap.c,v 1.128 2008-12-23 20:13:29 guy Exp $ (LBL)";
37 #endif
38
39 #ifdef HAVE_CONFIG_H
40 #include "config.h"
41 #endif
42
43 #ifdef WIN32
44 #include <pcap-stdinc.h>
45 #else /* WIN32 */
46 #if HAVE_INTTYPES_H
47 #include <inttypes.h>
48 #elif HAVE_STDINT_H
49 #include <stdint.h>
50 #endif
51 #ifdef HAVE_SYS_BITYPES_H
52 #include <sys/bitypes.h>
53 #endif
54 #include <sys/types.h>
55 #endif /* WIN32 */
56
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #if !defined(_MSC_VER) && !defined(__BORLANDC__) && !defined(__MINGW32__)
61 #include <unistd.h>
62 #endif
63 #include <fcntl.h>
64 #include <errno.h>
65
66 #ifdef HAVE_OS_PROTO_H
67 #include "os-proto.h"
68 #endif
69
70 #ifdef MSDOS
71 #include "pcap-dos.h"
72 #endif
73
74 #include "pcap-int.h"
75
76 #ifdef HAVE_DAG_API
77 #include "pcap-dag.h"
78 #endif /* HAVE_DAG_API */
79
80 #ifdef HAVE_SEPTEL_API
81 #include "pcap-septel.h"
82 #endif /* HAVE_SEPTEL_API */
83
84 #ifdef HAVE_SNF_API
85 #include "pcap-snf.h"
86 #endif /* HAVE_SNF_API */
87
88 #ifdef PCAP_SUPPORT_USB
89 #include "pcap-usb-linux.h"
90 #endif
91
92 #ifdef PCAP_SUPPORT_BT
93 #include "pcap-bt-linux.h"
94 #endif
95
96 #ifdef PCAP_SUPPORT_CAN
97 #include "pcap-can-linux.h"
98 #endif
99
100 #ifdef PCAP_SUPPORT_CANUSB
101 #include "pcap-canusb-linux.h"
102 #endif
103
104 #ifdef PCAP_SUPPORT_NETFILTER
105 #include "pcap-netfilter-linux.h"
106 #endif
107
108 #ifdef PCAP_SUPPORT_DBUS
109 #include "pcap-dbus.h"
110 #endif
111
112 int
113 pcap_not_initialized(pcap_t *pcap)
114 {
115 /* this means 'not initialized' */
116 return (PCAP_ERROR_NOT_ACTIVATED);
117 }
118
119 /*
120 * Returns 1 if rfmon mode can be set on the pcap_t, 0 if it can't,
121 * a PCAP_ERROR value on an error.
122 */
123 int
124 pcap_can_set_rfmon(pcap_t *p)
125 {
126 return (p->can_set_rfmon_op(p));
127 }
128
129 /*
130 * For systems where rfmon mode is never supported.
131 */
132 static int
133 pcap_cant_set_rfmon(pcap_t *p _U_)
134 {
135 return (0);
136 }
137
138 /*
139 * Sets *tstamp_typesp to point to an array 1 or more supported time stamp
140 * types; the return value is the number of supported time stamp types.
141 * The list should be freed by a call to pcap_free_tstamp_types() when
142 * you're done with it.
143 *
144 * A return value of 0 means "you don't get a choice of time stamp type",
145 * in which case *tstamp_typesp is set to null.
146 *
147 * PCAP_ERROR is returned on error.
148 */
149 int
150 pcap_list_tstamp_types(pcap_t *p, int **tstamp_typesp)
151 {
152 if (p->tstamp_type_count == 0) {
153 /*
154 * We don't support multiple time stamp types.
155 */
156 *tstamp_typesp = NULL;
157 } else {
158 *tstamp_typesp = (int*)calloc(sizeof(**tstamp_typesp),
159 p->tstamp_type_count);
160 if (*tstamp_typesp == NULL) {
161 (void)snprintf(p->errbuf, sizeof(p->errbuf),
162 "malloc: %s", pcap_strerror(errno));
163 return (PCAP_ERROR);
164 }
165 (void)memcpy(*tstamp_typesp, p->tstamp_type_list,
166 sizeof(**tstamp_typesp) * p->tstamp_type_count);
167 }
168 return (p->tstamp_type_count);
169 }
170
171 /*
172 * In Windows, you might have a library built with one version of the
173 * C runtime library and an application built with another version of
174 * the C runtime library, which means that the library might use one
175 * version of malloc() and free() and the application might use another
176 * version of malloc() and free(). If so, that means something
177 * allocated by the library cannot be freed by the application, so we
178 * need to have a pcap_free_tstamp_types() routine to free up the list
179 * allocated by pcap_list_tstamp_types(), even though it's just a wrapper
180 * around free().
181 */
182 void
183 pcap_free_tstamp_types(int *tstamp_type_list)
184 {
185 free(tstamp_type_list);
186 }
187
188 /*
189 * Default one-shot callback; overridden for capture types where the
190 * packet data cannot be guaranteed to be available after the callback
191 * returns, so that a copy must be made.
192 */
193 static void
194 pcap_oneshot(u_char *user, const struct pcap_pkthdr *h, const u_char *pkt)
195 {
196 struct oneshot_userdata *sp = (struct oneshot_userdata *)user;
197
198 *sp->hdr = *h;
199 *sp->pkt = pkt;
200 }
201
202 const u_char *
203 pcap_next(pcap_t *p, struct pcap_pkthdr *h)
204 {
205 struct oneshot_userdata s;
206 const u_char *pkt;
207
208 s.hdr = h;
209 s.pkt = &pkt;
210 s.pd = p;
211 if (pcap_dispatch(p, 1, p->oneshot_callback, (u_char *)&s) <= 0)
212 return (0);
213 return (pkt);
214 }
215
216 int
217 pcap_next_ex(pcap_t *p, struct pcap_pkthdr **pkt_header,
218 const u_char **pkt_data)
219 {
220 struct oneshot_userdata s;
221
222 s.hdr = &p->pcap_header;
223 s.pkt = pkt_data;
224 s.pd = p;
225
226 /* Saves a pointer to the packet headers */
227 *pkt_header= &p->pcap_header;
228
229 if (p->sf.rfile != NULL) {
230 int status;
231
232 /* We are on an offline capture */
233 status = pcap_offline_read(p, 1, p->oneshot_callback,
234 (u_char *)&s);
235
236 /*
237 * Return codes for pcap_offline_read() are:
238 * - 0: EOF
239 * - -1: error
240 * - >1: OK
241 * The first one ('0') conflicts with the return code of
242 * 0 from pcap_read() meaning "no packets arrived before
243 * the timeout expired", so we map it to -2 so you can
244 * distinguish between an EOF from a savefile and a
245 * "no packets arrived before the timeout expired, try
246 * again" from a live capture.
247 */
248 if (status == 0)
249 return (-2);
250 else
251 return (status);
252 }
253
254 /*
255 * Return codes for pcap_read() are:
256 * - 0: timeout
257 * - -1: error
258 * - -2: loop was broken out of with pcap_breakloop()
259 * - >1: OK
260 * The first one ('0') conflicts with the return code of 0 from
261 * pcap_offline_read() meaning "end of file".
262 */
263 return (p->read_op(p, 1, p->oneshot_callback, (u_char *)&s));
264 }
265
266 #if defined(DAG_ONLY)
267 int
268 pcap_findalldevs(pcap_if_t **alldevsp, char *errbuf)
269 {
270 return (dag_findalldevs(alldevsp, errbuf));
271 }
272
273 pcap_t *
274 pcap_create(const char *source, char *errbuf)
275 {
276 return (dag_create(source, errbuf));
277 }
278 #elif defined(SEPTEL_ONLY)
279 int
280 pcap_findalldevs(pcap_if_t **alldevsp, char *errbuf)
281 {
282 return (septel_findalldevs(alldevsp, errbuf));
283 }
284
285 pcap_t *
286 pcap_create(const char *source, char *errbuf)
287 {
288 return (septel_create(source, errbuf));
289 }
290 #elif defined(SNF_ONLY)
291 int
292 pcap_findalldevs(pcap_if_t **alldevsp, char *errbuf)
293 {
294 return (snf_findalldevs(alldevsp, errbuf));
295 }
296
297 pcap_t *
298 pcap_create(const char *source, char *errbuf)
299 {
300 return (snf_create(source, errbuf));
301 }
302 #else /* regular pcap */
303 struct capture_source_type {
304 int (*findalldevs_op)(pcap_if_t **, char *);
305 pcap_t *(*create_op)(const char *, char *, int *);
306 } capture_source_types[] = {
307 #ifdef HAVE_DAG_API
308 { dag_findalldevs, dag_create },
309 #endif
310 #ifdef HAVE_SEPTEL_API
311 { septel_findalldevs, septel_create },
312 #endif
313 #ifdef HAVE_SNF_API
314 { snf_findalldevs, snf_create },
315 #endif
316 #ifdef PCAP_SUPPORT_BT
317 { bt_findalldevs, bt_create },
318 #endif
319 #if PCAP_SUPPORT_CANUSB
320 { canusb_findalldevs, canusb_create },
321 #endif
322 #ifdef PCAP_SUPPORT_CAN
323 { can_findalldevs, can_create },
324 #endif
325 #ifdef PCAP_SUPPORT_USB
326 { usb_findalldevs, usb_create },
327 #endif
328 #ifdef PCAP_SUPPORT_NETFILTER
329 { netfilter_findalldevs, netfilter_create },
330 #endif
331 #ifdef PCAP_SUPPORT_DBUS
332 { dbus_findalldevs, dbus_create },
333 #endif
334 { NULL, NULL }
335 };
336
337 /*
338 * Get a list of all capture sources that are up and that we can open.
339 * Returns -1 on error, 0 otherwise.
340 * The list, as returned through "alldevsp", may be null if no interfaces
341 * were up and could be opened.
342 */
343 int
344 pcap_findalldevs(pcap_if_t **alldevsp, char *errbuf)
345 {
346 size_t i;
347
348 /*
349 * Get the list of regular interfaces first.
350 */
351 if (pcap_findalldevs_interfaces(alldevsp, errbuf) == -1)
352 return (-1); /* failure */
353
354 /*
355 * Add any interfaces that need a platform-specific mechanism
356 * to find.
357 */
358 if (pcap_platform_finddevs(alldevsp, errbuf) == -1) {
359 /*
360 * We had an error; free the list we've been
361 * constructing.
362 */
363 if (*alldevsp != NULL) {
364 pcap_freealldevs(*alldevsp);
365 *alldevsp = NULL;
366 }
367 return (-1);
368 }
369
370 /*
371 * Ask each of the non-local-network-interface capture
372 * source types what interfaces they have.
373 */
374 for (i = 0; capture_source_types[i].findalldevs_op != NULL; i++) {
375 if (capture_source_types[i].findalldevs_op(alldevsp, errbuf) == -1) {
376 /*
377 * We had an error; free the list we've been
378 * constructing.
379 */
380 if (*alldevsp != NULL) {
381 pcap_freealldevs(*alldevsp);
382 *alldevsp = NULL;
383 }
384 return (-1);
385 }
386 }
387 return (0);
388 }
389
390 pcap_t *
391 pcap_create(const char *source, char *errbuf)
392 {
393 size_t i;
394 int is_theirs;
395 pcap_t *p;
396
397 /*
398 * A null source name is equivalent to the "any" device -
399 * which might not be supported on this platform, but
400 * this means that you'll get a "not supported" error
401 * rather than, say, a crash when we try to dereference
402 * the null pointer.
403 */
404 if (source == NULL)
405 source = "any";
406
407 /*
408 * Try each of the non-local-network-interface capture
409 * source types until we find one that works for this
410 * device or run out of types.
411 */
412 for (i = 0; capture_source_types[i].create_op != NULL; i++) {
413 is_theirs = 0;
414 p = capture_source_types[i].create_op(source, errbuf, &is_theirs);
415 if (is_theirs) {
416 /*
417 * The device name refers to a device of the
418 * type in question; either it succeeded,
419 * in which case p refers to a pcap_t to
420 * later activate for the device, or it
421 * failed, in which case p is null and we
422 * should return that to report the failure
423 * to create.
424 */
425 return (p);
426 }
427 }
428
429 /*
430 * OK, try it as a regular network interface.
431 */
432 return (pcap_create_interface(source, errbuf));
433 }
434 #endif
435
436 static void
437 initialize_ops(pcap_t *p)
438 {
439 /*
440 * Set operation pointers for operations that only work on
441 * an activated pcap_t to point to a routine that returns
442 * a "this isn't activated" error.
443 */
444 p->read_op = (read_op_t)pcap_not_initialized;
445 p->inject_op = (inject_op_t)pcap_not_initialized;
446 p->setfilter_op = (setfilter_op_t)pcap_not_initialized;
447 p->setdirection_op = (setdirection_op_t)pcap_not_initialized;
448 p->set_datalink_op = (set_datalink_op_t)pcap_not_initialized;
449 p->getnonblock_op = (getnonblock_op_t)pcap_not_initialized;
450 p->setnonblock_op = (setnonblock_op_t)pcap_not_initialized;
451 p->stats_op = (stats_op_t)pcap_not_initialized;
452 #ifdef WIN32
453 p->setbuff_op = (setbuff_op_t)pcap_not_initialized;
454 p->setmode_op = (setmode_op_t)pcap_not_initialized;
455 p->setmintocopy_op = (setmintocopy_op_t)pcap_not_initialized;
456 #endif
457
458 /*
459 * Default cleanup operation - implementations can override
460 * this, but should call pcap_cleanup_live_common() after
461 * doing their own additional cleanup.
462 */
463 p->cleanup_op = pcap_cleanup_live_common;
464
465 /*
466 * In most cases, the standard one-short callback can
467 * be used for pcap_next()/pcap_next_ex().
468 */
469 p->oneshot_callback = pcap_oneshot;
470 }
471
472 pcap_t *
473 pcap_create_common(const char *source, char *ebuf)
474 {
475 pcap_t *p;
476
477 p = malloc(sizeof(*p));
478 if (p == NULL) {
479 snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s",
480 pcap_strerror(errno));
481 return (NULL);
482 }
483 memset(p, 0, sizeof(*p));
484 #ifndef WIN32
485 p->fd = -1; /* not opened yet */
486 p->selectable_fd = -1;
487 p->send_fd = -1;
488 #endif
489
490 p->opt.source = strdup(source);
491 if (p->opt.source == NULL) {
492 snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s",
493 pcap_strerror(errno));
494 free(p);
495 return (NULL);
496 }
497
498 /*
499 * Default to "can't set rfmon mode"; if it's supported by
500 * a platform, the create routine that called us can set
501 * the op to its routine to check whether a particular
502 * device supports it.
503 */
504 p->can_set_rfmon_op = pcap_cant_set_rfmon;
505
506 initialize_ops(p);
507
508 /* put in some defaults*/
509 pcap_set_timeout(p, 0);
510 pcap_set_snaplen(p, 65535); /* max packet size */
511 p->opt.promisc = 0;
512 p->opt.buffer_size = 0;
513 p->opt.tstamp_type = -1; /* default to not setting time stamp type */
514 return (p);
515 }
516
517 int
518 pcap_check_activated(pcap_t *p)
519 {
520 if (p->activated) {
521 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "can't perform "
522 " operation on activated capture");
523 return (-1);
524 }
525 return (0);
526 }
527
528 int
529 pcap_set_snaplen(pcap_t *p, int snaplen)
530 {
531 if (pcap_check_activated(p))
532 return (PCAP_ERROR_ACTIVATED);
533 p->snapshot = snaplen;
534 return (0);
535 }
536
537 int
538 pcap_set_promisc(pcap_t *p, int promisc)
539 {
540 if (pcap_check_activated(p))
541 return (PCAP_ERROR_ACTIVATED);
542 p->opt.promisc = promisc;
543 return (0);
544 }
545
546 int
547 pcap_set_rfmon(pcap_t *p, int rfmon)
548 {
549 if (pcap_check_activated(p))
550 return (PCAP_ERROR_ACTIVATED);
551 p->opt.rfmon = rfmon;
552 return (0);
553 }
554
555 int
556 pcap_set_timeout(pcap_t *p, int timeout_ms)
557 {
558 if (pcap_check_activated(p))
559 return (PCAP_ERROR_ACTIVATED);
560 p->md.timeout = timeout_ms;
561 return (0);
562 }
563
564 int
565 pcap_set_tstamp_type(pcap_t *p, int tstamp_type)
566 {
567 int i;
568
569 if (pcap_check_activated(p))
570 return (PCAP_ERROR_ACTIVATED);
571
572 /*
573 * If p->tstamp_type_count is 0, we don't support setting
574 * the time stamp type at all.
575 */
576 if (p->tstamp_type_count == 0)
577 return (PCAP_ERROR_CANTSET_TSTAMP_TYPE);
578
579 /*
580 * Check whether we claim to support this type of time stamp.
581 */
582 for (i = 0; i < p->tstamp_type_count; i++) {
583 if (p->tstamp_type_list[i] == tstamp_type) {
584 /*
585 * Yes.
586 */
587 p->opt.tstamp_type = tstamp_type;
588 return (0);
589 }
590 }
591
592 /*
593 * No. We support setting the time stamp type, but not to this
594 * particular value.
595 */
596 return (PCAP_WARNING_TSTAMP_TYPE_NOTSUP);
597 }
598
599 int
600 pcap_set_buffer_size(pcap_t *p, int buffer_size)
601 {
602 if (pcap_check_activated(p))
603 return (PCAP_ERROR_ACTIVATED);
604 p->opt.buffer_size = buffer_size;
605 return (0);
606 }
607
608 int
609 pcap_activate(pcap_t *p)
610 {
611 int status;
612
613 /*
614 * Catch attempts to re-activate an already-activated
615 * pcap_t; this should, for example, catch code that
616 * calls pcap_open_live() followed by pcap_activate(),
617 * as some code that showed up in a Stack Exchange
618 * question did.
619 */
620 if (pcap_check_activated(p))
621 return (PCAP_ERROR_ACTIVATED);
622 status = p->activate_op(p);
623 if (status >= 0)
624 p->activated = 1;
625 else {
626 if (p->errbuf[0] == '\0') {
627 /*
628 * No error message supplied by the activate routine;
629 * for the benefit of programs that don't specially
630 * handle errors other than PCAP_ERROR, return the
631 * error message corresponding to the status.
632 */
633 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "%s",
634 pcap_statustostr(status));
635 }
636
637 /*
638 * Undo any operation pointer setting, etc. done by
639 * the activate operation.
640 */
641 initialize_ops(p);
642 }
643 return (status);
644 }
645
646 pcap_t *
647 pcap_open_live(const char *source, int snaplen, int promisc, int to_ms, char *errbuf)
648 {
649 pcap_t *p;
650 int status;
651
652 p = pcap_create(source, errbuf);
653 if (p == NULL)
654 return (NULL);
655 status = pcap_set_snaplen(p, snaplen);
656 if (status < 0)
657 goto fail;
658 status = pcap_set_promisc(p, promisc);
659 if (status < 0)
660 goto fail;
661 status = pcap_set_timeout(p, to_ms);
662 if (status < 0)
663 goto fail;
664 /*
665 * Mark this as opened with pcap_open_live(), so that, for
666 * example, we show the full list of DLT_ values, rather
667 * than just the ones that are compatible with capturing
668 * when not in monitor mode. That allows existing applications
669 * to work the way they used to work, but allows new applications
670 * that know about the new open API to, for example, find out the
671 * DLT_ values that they can select without changing whether
672 * the adapter is in monitor mode or not.
673 */
674 p->oldstyle = 1;
675 status = pcap_activate(p);
676 if (status < 0)
677 goto fail;
678 return (p);
679 fail:
680 if (status == PCAP_ERROR)
681 snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s", source,
682 p->errbuf);
683 else if (status == PCAP_ERROR_NO_SUCH_DEVICE ||
684 status == PCAP_ERROR_PERM_DENIED ||
685 status == PCAP_ERROR_PROMISC_PERM_DENIED)
686 snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s (%s)", source,
687 pcap_statustostr(status), p->errbuf);
688 else
689 snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s", source,
690 pcap_statustostr(status));
691 pcap_close(p);
692 return (NULL);
693 }
694
695 int
696 pcap_dispatch(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
697 {
698 return (p->read_op(p, cnt, callback, user));
699 }
700
701 /*
702 * XXX - is this necessary?
703 */
704 int
705 pcap_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
706 {
707
708 return (p->read_op(p, cnt, callback, user));
709 }
710
711 int
712 pcap_loop(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
713 {
714 register int n;
715
716 for (;;) {
717 if (p->sf.rfile != NULL) {
718 /*
719 * 0 means EOF, so don't loop if we get 0.
720 */
721 n = pcap_offline_read(p, cnt, callback, user);
722 } else {
723 /*
724 * XXX keep reading until we get something
725 * (or an error occurs)
726 */
727 do {
728 n = p->read_op(p, cnt, callback, user);
729 } while (n == 0);
730 }
731 if (n <= 0)
732 return (n);
733 if (cnt > 0) {
734 cnt -= n;
735 if (cnt <= 0)
736 return (0);
737 }
738 }
739 }
740
741 /*
742 * Force the loop in "pcap_read()" or "pcap_read_offline()" to terminate.
743 */
744 void
745 pcap_breakloop(pcap_t *p)
746 {
747 p->break_loop = 1;
748 }
749
750 int
751 pcap_datalink(pcap_t *p)
752 {
753 return (p->linktype);
754 }
755
756 int
757 pcap_datalink_ext(pcap_t *p)
758 {
759 return (p->linktype_ext);
760 }
761
762 int
763 pcap_list_datalinks(pcap_t *p, int **dlt_buffer)
764 {
765 if (p->dlt_count == 0) {
766 /*
767 * We couldn't fetch the list of DLTs, which means
768 * this platform doesn't support changing the
769 * DLT for an interface. Return a list of DLTs
770 * containing only the DLT this device supports.
771 */
772 *dlt_buffer = (int*)malloc(sizeof(**dlt_buffer));
773 if (*dlt_buffer == NULL) {
774 (void)snprintf(p->errbuf, sizeof(p->errbuf),
775 "malloc: %s", pcap_strerror(errno));
776 return (-1);
777 }
778 **dlt_buffer = p->linktype;
779 return (1);
780 } else {
781 *dlt_buffer = (int*)calloc(sizeof(**dlt_buffer), p->dlt_count);
782 if (*dlt_buffer == NULL) {
783 (void)snprintf(p->errbuf, sizeof(p->errbuf),
784 "malloc: %s", pcap_strerror(errno));
785 return (-1);
786 }
787 (void)memcpy(*dlt_buffer, p->dlt_list,
788 sizeof(**dlt_buffer) * p->dlt_count);
789 return (p->dlt_count);
790 }
791 }
792
793 /*
794 * In Windows, you might have a library built with one version of the
795 * C runtime library and an application built with another version of
796 * the C runtime library, which means that the library might use one
797 * version of malloc() and free() and the application might use another
798 * version of malloc() and free(). If so, that means something
799 * allocated by the library cannot be freed by the application, so we
800 * need to have a pcap_free_datalinks() routine to free up the list
801 * allocated by pcap_list_datalinks(), even though it's just a wrapper
802 * around free().
803 */
804 void
805 pcap_free_datalinks(int *dlt_list)
806 {
807 free(dlt_list);
808 }
809
810 int
811 pcap_set_datalink(pcap_t *p, int dlt)
812 {
813 int i;
814 const char *dlt_name;
815
816 if (p->dlt_count == 0 || p->set_datalink_op == NULL) {
817 /*
818 * We couldn't fetch the list of DLTs, or we don't
819 * have a "set datalink" operation, which means
820 * this platform doesn't support changing the
821 * DLT for an interface. Check whether the new
822 * DLT is the one this interface supports.
823 */
824 if (p->linktype != dlt)
825 goto unsupported;
826
827 /*
828 * It is, so there's nothing we need to do here.
829 */
830 return (0);
831 }
832 for (i = 0; i < p->dlt_count; i++)
833 if (p->dlt_list[i] == dlt)
834 break;
835 if (i >= p->dlt_count)
836 goto unsupported;
837 if (p->dlt_count == 2 && p->dlt_list[0] == DLT_EN10MB &&
838 dlt == DLT_DOCSIS) {
839 /*
840 * This is presumably an Ethernet device, as the first
841 * link-layer type it offers is DLT_EN10MB, and the only
842 * other type it offers is DLT_DOCSIS. That means that
843 * we can't tell the driver to supply DOCSIS link-layer
844 * headers - we're just pretending that's what we're
845 * getting, as, presumably, we're capturing on a dedicated
846 * link to a Cisco Cable Modem Termination System, and
847 * it's putting raw DOCSIS frames on the wire inside low-level
848 * Ethernet framing.
849 */
850 p->linktype = dlt;
851 return (0);
852 }
853 if (p->set_datalink_op(p, dlt) == -1)
854 return (-1);
855 p->linktype = dlt;
856 return (0);
857
858 unsupported:
859 dlt_name = pcap_datalink_val_to_name(dlt);
860 if (dlt_name != NULL) {
861 (void) snprintf(p->errbuf, sizeof(p->errbuf),
862 "%s is not one of the DLTs supported by this device",
863 dlt_name);
864 } else {
865 (void) snprintf(p->errbuf, sizeof(p->errbuf),
866 "DLT %d is not one of the DLTs supported by this device",
867 dlt);
868 }
869 return (-1);
870 }
871
872 /*
873 * This array is designed for mapping upper and lower case letter
874 * together for a case independent comparison. The mappings are
875 * based upon ascii character sequences.
876 */
877 static const u_char charmap[] = {
878 (u_char)'\000', (u_char)'\001', (u_char)'\002', (u_char)'\003',
879 (u_char)'\004', (u_char)'\005', (u_char)'\006', (u_char)'\007',
880 (u_char)'\010', (u_char)'\011', (u_char)'\012', (u_char)'\013',
881 (u_char)'\014', (u_char)'\015', (u_char)'\016', (u_char)'\017',
882 (u_char)'\020', (u_char)'\021', (u_char)'\022', (u_char)'\023',
883 (u_char)'\024', (u_char)'\025', (u_char)'\026', (u_char)'\027',
884 (u_char)'\030', (u_char)'\031', (u_char)'\032', (u_char)'\033',
885 (u_char)'\034', (u_char)'\035', (u_char)'\036', (u_char)'\037',
886 (u_char)'\040', (u_char)'\041', (u_char)'\042', (u_char)'\043',
887 (u_char)'\044', (u_char)'\045', (u_char)'\046', (u_char)'\047',
888 (u_char)'\050', (u_char)'\051', (u_char)'\052', (u_char)'\053',
889 (u_char)'\054', (u_char)'\055', (u_char)'\056', (u_char)'\057',
890 (u_char)'\060', (u_char)'\061', (u_char)'\062', (u_char)'\063',
891 (u_char)'\064', (u_char)'\065', (u_char)'\066', (u_char)'\067',
892 (u_char)'\070', (u_char)'\071', (u_char)'\072', (u_char)'\073',
893 (u_char)'\074', (u_char)'\075', (u_char)'\076', (u_char)'\077',
894 (u_char)'\100', (u_char)'\141', (u_char)'\142', (u_char)'\143',
895 (u_char)'\144', (u_char)'\145', (u_char)'\146', (u_char)'\147',
896 (u_char)'\150', (u_char)'\151', (u_char)'\152', (u_char)'\153',
897 (u_char)'\154', (u_char)'\155', (u_char)'\156', (u_char)'\157',
898 (u_char)'\160', (u_char)'\161', (u_char)'\162', (u_char)'\163',
899 (u_char)'\164', (u_char)'\165', (u_char)'\166', (u_char)'\167',
900 (u_char)'\170', (u_char)'\171', (u_char)'\172', (u_char)'\133',
901 (u_char)'\134', (u_char)'\135', (u_char)'\136', (u_char)'\137',
902 (u_char)'\140', (u_char)'\141', (u_char)'\142', (u_char)'\143',
903 (u_char)'\144', (u_char)'\145', (u_char)'\146', (u_char)'\147',
904 (u_char)'\150', (u_char)'\151', (u_char)'\152', (u_char)'\153',
905 (u_char)'\154', (u_char)'\155', (u_char)'\156', (u_char)'\157',
906 (u_char)'\160', (u_char)'\161', (u_char)'\162', (u_char)'\163',
907 (u_char)'\164', (u_char)'\165', (u_char)'\166', (u_char)'\167',
908 (u_char)'\170', (u_char)'\171', (u_char)'\172', (u_char)'\173',
909 (u_char)'\174', (u_char)'\175', (u_char)'\176', (u_char)'\177',
910 (u_char)'\200', (u_char)'\201', (u_char)'\202', (u_char)'\203',
911 (u_char)'\204', (u_char)'\205', (u_char)'\206', (u_char)'\207',
912 (u_char)'\210', (u_char)'\211', (u_char)'\212', (u_char)'\213',
913 (u_char)'\214', (u_char)'\215', (u_char)'\216', (u_char)'\217',
914 (u_char)'\220', (u_char)'\221', (u_char)'\222', (u_char)'\223',
915 (u_char)'\224', (u_char)'\225', (u_char)'\226', (u_char)'\227',
916 (u_char)'\230', (u_char)'\231', (u_char)'\232', (u_char)'\233',
917 (u_char)'\234', (u_char)'\235', (u_char)'\236', (u_char)'\237',
918 (u_char)'\240', (u_char)'\241', (u_char)'\242', (u_char)'\243',
919 (u_char)'\244', (u_char)'\245', (u_char)'\246', (u_char)'\247',
920 (u_char)'\250', (u_char)'\251', (u_char)'\252', (u_char)'\253',
921 (u_char)'\254', (u_char)'\255', (u_char)'\256', (u_char)'\257',
922 (u_char)'\260', (u_char)'\261', (u_char)'\262', (u_char)'\263',
923 (u_char)'\264', (u_char)'\265', (u_char)'\266', (u_char)'\267',
924 (u_char)'\270', (u_char)'\271', (u_char)'\272', (u_char)'\273',
925 (u_char)'\274', (u_char)'\275', (u_char)'\276', (u_char)'\277',
926 (u_char)'\300', (u_char)'\341', (u_char)'\342', (u_char)'\343',
927 (u_char)'\344', (u_char)'\345', (u_char)'\346', (u_char)'\347',
928 (u_char)'\350', (u_char)'\351', (u_char)'\352', (u_char)'\353',
929 (u_char)'\354', (u_char)'\355', (u_char)'\356', (u_char)'\357',
930 (u_char)'\360', (u_char)'\361', (u_char)'\362', (u_char)'\363',
931 (u_char)'\364', (u_char)'\365', (u_char)'\366', (u_char)'\367',
932 (u_char)'\370', (u_char)'\371', (u_char)'\372', (u_char)'\333',
933 (u_char)'\334', (u_char)'\335', (u_char)'\336', (u_char)'\337',
934 (u_char)'\340', (u_char)'\341', (u_char)'\342', (u_char)'\343',
935 (u_char)'\344', (u_char)'\345', (u_char)'\346', (u_char)'\347',
936 (u_char)'\350', (u_char)'\351', (u_char)'\352', (u_char)'\353',
937 (u_char)'\354', (u_char)'\355', (u_char)'\356', (u_char)'\357',
938 (u_char)'\360', (u_char)'\361', (u_char)'\362', (u_char)'\363',
939 (u_char)'\364', (u_char)'\365', (u_char)'\366', (u_char)'\367',
940 (u_char)'\370', (u_char)'\371', (u_char)'\372', (u_char)'\373',
941 (u_char)'\374', (u_char)'\375', (u_char)'\376', (u_char)'\377',
942 };
943
944 int
945 pcap_strcasecmp(const char *s1, const char *s2)
946 {
947 register const u_char *cm = charmap,
948 *us1 = (const u_char *)s1,
949 *us2 = (const u_char *)s2;
950
951 while (cm[*us1] == cm[*us2++])
952 if (*us1++ == '\0')
953 return(0);
954 return (cm[*us1] - cm[*--us2]);
955 }
956
957 struct dlt_choice {
958 const char *name;
959 const char *description;
960 int dlt;
961 };
962
963 #define DLT_CHOICE(code, description) { #code, description, code }
964 #define DLT_CHOICE_SENTINEL { NULL, NULL, 0 }
965
966 static struct dlt_choice dlt_choices[] = {
967 DLT_CHOICE(DLT_NULL, "BSD loopback"),
968 DLT_CHOICE(DLT_EN10MB, "Ethernet"),
969 DLT_CHOICE(DLT_IEEE802, "Token ring"),
970 DLT_CHOICE(DLT_ARCNET, "BSD ARCNET"),
971 DLT_CHOICE(DLT_SLIP, "SLIP"),
972 DLT_CHOICE(DLT_PPP, "PPP"),
973 DLT_CHOICE(DLT_FDDI, "FDDI"),
974 DLT_CHOICE(DLT_ATM_RFC1483, "RFC 1483 LLC-encapsulated ATM"),
975 DLT_CHOICE(DLT_RAW, "Raw IP"),
976 DLT_CHOICE(DLT_SLIP_BSDOS, "BSD/OS SLIP"),
977 DLT_CHOICE(DLT_PPP_BSDOS, "BSD/OS PPP"),
978 DLT_CHOICE(DLT_ATM_CLIP, "Linux Classical IP-over-ATM"),
979 DLT_CHOICE(DLT_PPP_SERIAL, "PPP over serial"),
980 DLT_CHOICE(DLT_PPP_ETHER, "PPPoE"),
981 DLT_CHOICE(DLT_SYMANTEC_FIREWALL, "Symantec Firewall"),
982 DLT_CHOICE(DLT_C_HDLC, "Cisco HDLC"),
983 DLT_CHOICE(DLT_IEEE802_11, "802.11"),
984 DLT_CHOICE(DLT_FRELAY, "Frame Relay"),
985 DLT_CHOICE(DLT_LOOP, "OpenBSD loopback"),
986 DLT_CHOICE(DLT_ENC, "OpenBSD encapsulated IP"),
987 DLT_CHOICE(DLT_LINUX_SLL, "Linux cooked"),
988 DLT_CHOICE(DLT_LTALK, "Localtalk"),
989 DLT_CHOICE(DLT_PFLOG, "OpenBSD pflog file"),
990 DLT_CHOICE(DLT_PFSYNC, "Packet filter state syncing"),
991 DLT_CHOICE(DLT_PRISM_HEADER, "802.11 plus Prism header"),
992 DLT_CHOICE(DLT_IP_OVER_FC, "RFC 2625 IP-over-Fibre Channel"),
993 DLT_CHOICE(DLT_SUNATM, "Sun raw ATM"),
994 DLT_CHOICE(DLT_IEEE802_11_RADIO, "802.11 plus radiotap header"),
995 DLT_CHOICE(DLT_ARCNET_LINUX, "Linux ARCNET"),
996 DLT_CHOICE(DLT_JUNIPER_MLPPP, "Juniper Multi-Link PPP"),
997 DLT_CHOICE(DLT_JUNIPER_MLFR, "Juniper Multi-Link Frame Relay"),
998 DLT_CHOICE(DLT_JUNIPER_ES, "Juniper Encryption Services PIC"),
999 DLT_CHOICE(DLT_JUNIPER_GGSN, "Juniper GGSN PIC"),
1000 DLT_CHOICE(DLT_JUNIPER_MFR, "Juniper FRF.16 Frame Relay"),
1001 DLT_CHOICE(DLT_JUNIPER_ATM2, "Juniper ATM2 PIC"),
1002 DLT_CHOICE(DLT_JUNIPER_SERVICES, "Juniper Advanced Services PIC"),
1003 DLT_CHOICE(DLT_JUNIPER_ATM1, "Juniper ATM1 PIC"),
1004 DLT_CHOICE(DLT_APPLE_IP_OVER_IEEE1394, "Apple IP-over-IEEE 1394"),
1005 DLT_CHOICE(DLT_MTP2_WITH_PHDR, "SS7 MTP2 with Pseudo-header"),
1006 DLT_CHOICE(DLT_MTP2, "SS7 MTP2"),
1007 DLT_CHOICE(DLT_MTP3, "SS7 MTP3"),
1008 DLT_CHOICE(DLT_SCCP, "SS7 SCCP"),
1009 DLT_CHOICE(DLT_DOCSIS, "DOCSIS"),
1010 DLT_CHOICE(DLT_LINUX_IRDA, "Linux IrDA"),
1011 DLT_CHOICE(DLT_IEEE802_11_RADIO_AVS, "802.11 plus AVS radio information header"),
1012 DLT_CHOICE(DLT_JUNIPER_MONITOR, "Juniper Passive Monitor PIC"),
1013 DLT_CHOICE(DLT_BACNET_MS_TP, "BACnet MS/TP"),
1014 DLT_CHOICE(DLT_PPP_PPPD, "PPP for pppd, with direction flag"),
1015 DLT_CHOICE(DLT_JUNIPER_PPPOE, "Juniper PPPoE"),
1016 DLT_CHOICE(DLT_JUNIPER_PPPOE_ATM, "Juniper PPPoE/ATM"),
1017 DLT_CHOICE(DLT_GPRS_LLC, "GPRS LLC"),
1018 DLT_CHOICE(DLT_GPF_T, "GPF-T"),
1019 DLT_CHOICE(DLT_GPF_F, "GPF-F"),
1020 DLT_CHOICE(DLT_JUNIPER_PIC_PEER, "Juniper PIC Peer"),
1021 DLT_CHOICE(DLT_ERF_ETH, "Ethernet with Endace ERF header"),
1022 DLT_CHOICE(DLT_ERF_POS, "Packet-over-SONET with Endace ERF header"),
1023 DLT_CHOICE(DLT_LINUX_LAPD, "Linux vISDN LAPD"),
1024 DLT_CHOICE(DLT_JUNIPER_ETHER, "Juniper Ethernet"),
1025 DLT_CHOICE(DLT_JUNIPER_PPP, "Juniper PPP"),
1026 DLT_CHOICE(DLT_JUNIPER_FRELAY, "Juniper Frame Relay"),
1027 DLT_CHOICE(DLT_JUNIPER_CHDLC, "Juniper C-HDLC"),
1028 DLT_CHOICE(DLT_MFR, "FRF.16 Frame Relay"),
1029 DLT_CHOICE(DLT_JUNIPER_VP, "Juniper Voice PIC"),
1030 DLT_CHOICE(DLT_A429, "Arinc 429"),
1031 DLT_CHOICE(DLT_A653_ICM, "Arinc 653 Interpartition Communication"),
1032 DLT_CHOICE(DLT_USB, "USB"),
1033 DLT_CHOICE(DLT_BLUETOOTH_HCI_H4, "Bluetooth HCI UART transport layer"),
1034 DLT_CHOICE(DLT_IEEE802_16_MAC_CPS, "IEEE 802.16 MAC Common Part Sublayer"),
1035 DLT_CHOICE(DLT_USB_LINUX, "USB with Linux header"),
1036 DLT_CHOICE(DLT_CAN20B, "Controller Area Network (CAN) v. 2.0B"),
1037 DLT_CHOICE(DLT_IEEE802_15_4_LINUX, "IEEE 802.15.4 with Linux padding"),
1038 DLT_CHOICE(DLT_PPI, "Per-Packet Information"),
1039 DLT_CHOICE(DLT_IEEE802_16_MAC_CPS_RADIO, "IEEE 802.16 MAC Common Part Sublayer plus radiotap header"),
1040 DLT_CHOICE(DLT_JUNIPER_ISM, "Juniper Integrated Service Module"),
1041 DLT_CHOICE(DLT_IEEE802_15_4, "IEEE 802.15.4 with FCS"),
1042 DLT_CHOICE(DLT_SITA, "SITA pseudo-header"),
1043 DLT_CHOICE(DLT_ERF, "Endace ERF header"),
1044 DLT_CHOICE(DLT_RAIF1, "Ethernet with u10 Networks pseudo-header"),
1045 DLT_CHOICE(DLT_IPMB, "IPMB"),
1046 DLT_CHOICE(DLT_JUNIPER_ST, "Juniper Secure Tunnel"),
1047 DLT_CHOICE(DLT_BLUETOOTH_HCI_H4_WITH_PHDR, "Bluetooth HCI UART transport layer plus pseudo-header"),
1048 DLT_CHOICE(DLT_AX25_KISS, "AX.25 with KISS header"),
1049 DLT_CHOICE(DLT_IEEE802_15_4_NONASK_PHY, "IEEE 802.15.4 with non-ASK PHY data"),
1050 DLT_CHOICE(DLT_MPLS, "MPLS with label as link-layer header"),
1051 DLT_CHOICE(DLT_USB_LINUX_MMAPPED, "USB with padded Linux header"),
1052 DLT_CHOICE(DLT_DECT, "DECT"),
1053 DLT_CHOICE(DLT_AOS, "AOS Space Data Link protocol"),
1054 DLT_CHOICE(DLT_WIHART, "Wireless HART"),
1055 DLT_CHOICE(DLT_FC_2, "Fibre Channel FC-2"),
1056 DLT_CHOICE(DLT_FC_2_WITH_FRAME_DELIMS, "Fibre Channel FC-2 with frame delimiters"),
1057 DLT_CHOICE(DLT_IPNET, "Solaris ipnet"),
1058 DLT_CHOICE(DLT_CAN_SOCKETCAN, "CAN-bus with SocketCAN headers"),
1059 DLT_CHOICE(DLT_IPV4, "Raw IPv4"),
1060 DLT_CHOICE(DLT_IPV6, "Raw IPv6"),
1061 DLT_CHOICE(DLT_IEEE802_15_4_NOFCS, "IEEE 802.15.4 without FCS"),
1062 DLT_CHOICE(DLT_JUNIPER_VS, "Juniper Virtual Server"),
1063 DLT_CHOICE(DLT_JUNIPER_SRX_E2E, "Juniper SRX E2E"),
1064 DLT_CHOICE(DLT_JUNIPER_FIBRECHANNEL, "Juniper Fibre Channel"),
1065 DLT_CHOICE(DLT_DVB_CI, "DVB-CI"),
1066 DLT_CHOICE(DLT_JUNIPER_ATM_CEMIC, "Juniper ATM CEMIC"),
1067 DLT_CHOICE(DLT_NFLOG, "Linux netfilter log messages"),
1068 DLT_CHOICE(DLT_NETANALYZER, "Ethernet with Hilscher netANALYZER pseudo-header"),
1069 DLT_CHOICE(DLT_NETANALYZER_TRANSPARENT, "Ethernet with Hilscher netANALYZER pseudo-header and with preamble and SFD"),
1070 DLT_CHOICE(DLT_IPOIB, "RFC 4391 IP-over-Infiniband"),
1071 DLT_CHOICE_SENTINEL
1072 };
1073
1074 int
1075 pcap_datalink_name_to_val(const char *name)
1076 {
1077 int i;
1078
1079 for (i = 0; dlt_choices[i].name != NULL; i++) {
1080 if (pcap_strcasecmp(dlt_choices[i].name + sizeof("DLT_") - 1,
1081 name) == 0)
1082 return (dlt_choices[i].dlt);
1083 }
1084 return (-1);
1085 }
1086
1087 const char *
1088 pcap_datalink_val_to_name(int dlt)
1089 {
1090 int i;
1091
1092 for (i = 0; dlt_choices[i].name != NULL; i++) {
1093 if (dlt_choices[i].dlt == dlt)
1094 return (dlt_choices[i].name + sizeof("DLT_") - 1);
1095 }
1096 return (NULL);
1097 }
1098
1099 const char *
1100 pcap_datalink_val_to_description(int dlt)
1101 {
1102 int i;
1103
1104 for (i = 0; dlt_choices[i].name != NULL; i++) {
1105 if (dlt_choices[i].dlt == dlt)
1106 return (dlt_choices[i].description);
1107 }
1108 return (NULL);
1109 }
1110
1111 struct tstamp_type_choice {
1112 const char *name;
1113 const char *description;
1114 int type;
1115 };
1116
1117 static struct tstamp_type_choice tstamp_type_choices[] = {
1118 { "host", "Host", PCAP_TSTAMP_HOST },
1119 { "host_lowprec", "Host, low precision", PCAP_TSTAMP_HOST_LOWPREC },
1120 { "host_hiprec", "Host, high precision", PCAP_TSTAMP_HOST_HIPREC },
1121 { "adapter", "Adapter", PCAP_TSTAMP_ADAPTER },
1122 { "adapter_unsynced", "Adapter, not synced with system time", PCAP_TSTAMP_ADAPTER_UNSYNCED },
1123 { NULL, NULL, 0 }
1124 };
1125
1126 int
1127 pcap_tstamp_type_name_to_val(const char *name)
1128 {
1129 int i;
1130
1131 for (i = 0; tstamp_type_choices[i].name != NULL; i++) {
1132 if (pcap_strcasecmp(tstamp_type_choices[i].name, name) == 0)
1133 return (tstamp_type_choices[i].type);
1134 }
1135 return (PCAP_ERROR);
1136 }
1137
1138 const char *
1139 pcap_tstamp_type_val_to_name(int tstamp_type)
1140 {
1141 int i;
1142
1143 for (i = 0; tstamp_type_choices[i].name != NULL; i++) {
1144 if (tstamp_type_choices[i].type == tstamp_type)
1145 return (tstamp_type_choices[i].name);
1146 }
1147 return (NULL);
1148 }
1149
1150 const char *
1151 pcap_tstamp_type_val_to_description(int tstamp_type)
1152 {
1153 int i;
1154
1155 for (i = 0; tstamp_type_choices[i].name != NULL; i++) {
1156 if (tstamp_type_choices[i].type == tstamp_type)
1157 return (tstamp_type_choices[i].description);
1158 }
1159 return (NULL);
1160 }
1161
1162 int
1163 pcap_snapshot(pcap_t *p)
1164 {
1165 return (p->snapshot);
1166 }
1167
1168 int
1169 pcap_is_swapped(pcap_t *p)
1170 {
1171 return (p->sf.swapped);
1172 }
1173
1174 int
1175 pcap_major_version(pcap_t *p)
1176 {
1177 return (p->sf.version_major);
1178 }
1179
1180 int
1181 pcap_minor_version(pcap_t *p)
1182 {
1183 return (p->sf.version_minor);
1184 }
1185
1186 FILE *
1187 pcap_file(pcap_t *p)
1188 {
1189 return (p->sf.rfile);
1190 }
1191
1192 int
1193 pcap_fileno(pcap_t *p)
1194 {
1195 #ifndef WIN32
1196 return (p->fd);
1197 #else
1198 if (p->adapter != NULL)
1199 return ((int)(DWORD)p->adapter->hFile);
1200 else
1201 return (-1);
1202 #endif
1203 }
1204
1205 #if !defined(WIN32) && !defined(MSDOS)
1206 int
1207 pcap_get_selectable_fd(pcap_t *p)
1208 {
1209 return (p->selectable_fd);
1210 }
1211 #endif
1212
1213 void
1214 pcap_perror(pcap_t *p, char *prefix)
1215 {
1216 fprintf(stderr, "%s: %s\n", prefix, p->errbuf);
1217 }
1218
1219 char *
1220 pcap_geterr(pcap_t *p)
1221 {
1222 return (p->errbuf);
1223 }
1224
1225 int
1226 pcap_getnonblock(pcap_t *p, char *errbuf)
1227 {
1228 int ret;
1229
1230 ret = p->getnonblock_op(p, errbuf);
1231 if (ret == -1) {
1232 /*
1233 * In case somebody depended on the bug wherein
1234 * the error message was put into p->errbuf
1235 * by pcap_getnonblock_fd().
1236 */
1237 strlcpy(p->errbuf, errbuf, PCAP_ERRBUF_SIZE);
1238 }
1239 return (ret);
1240 }
1241
1242 /*
1243 * Get the current non-blocking mode setting, under the assumption that
1244 * it's just the standard POSIX non-blocking flag.
1245 *
1246 * We don't look at "p->nonblock", in case somebody tweaked the FD
1247 * directly.
1248 */
1249 #if !defined(WIN32) && !defined(MSDOS)
1250 int
1251 pcap_getnonblock_fd(pcap_t *p, char *errbuf)
1252 {
1253 int fdflags;
1254
1255 fdflags = fcntl(p->fd, F_GETFL, 0);
1256 if (fdflags == -1) {
1257 snprintf(errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s",
1258 pcap_strerror(errno));
1259 return (-1);
1260 }
1261 if (fdflags & O_NONBLOCK)
1262 return (1);
1263 else
1264 return (0);
1265 }
1266 #endif
1267
1268 int
1269 pcap_setnonblock(pcap_t *p, int nonblock, char *errbuf)
1270 {
1271 int ret;
1272
1273 ret = p->setnonblock_op(p, nonblock, errbuf);
1274 if (ret == -1) {
1275 /*
1276 * In case somebody depended on the bug wherein
1277 * the error message was put into p->errbuf
1278 * by pcap_setnonblock_fd().
1279 */
1280 strlcpy(p->errbuf, errbuf, PCAP_ERRBUF_SIZE);
1281 }
1282 return (ret);
1283 }
1284
1285 #if !defined(WIN32) && !defined(MSDOS)
1286 /*
1287 * Set non-blocking mode, under the assumption that it's just the
1288 * standard POSIX non-blocking flag. (This can be called by the
1289 * per-platform non-blocking-mode routine if that routine also
1290 * needs to do some additional work.)
1291 */
1292 int
1293 pcap_setnonblock_fd(pcap_t *p, int nonblock, char *errbuf)
1294 {
1295 int fdflags;
1296
1297 fdflags = fcntl(p->fd, F_GETFL, 0);
1298 if (fdflags == -1) {
1299 snprintf(errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s",
1300 pcap_strerror(errno));
1301 return (-1);
1302 }
1303 if (nonblock)
1304 fdflags |= O_NONBLOCK;
1305 else
1306 fdflags &= ~O_NONBLOCK;
1307 if (fcntl(p->fd, F_SETFL, fdflags) == -1) {
1308 snprintf(errbuf, PCAP_ERRBUF_SIZE, "F_SETFL: %s",
1309 pcap_strerror(errno));
1310 return (-1);
1311 }
1312 return (0);
1313 }
1314 #endif
1315
1316 #ifdef WIN32
1317 /*
1318 * Generate a string for the last Win32-specific error (i.e. an error generated when
1319 * calling a Win32 API).
1320 * For errors occurred during standard C calls, we still use pcap_strerror()
1321 */
1322 char *
1323 pcap_win32strerror(void)
1324 {
1325 DWORD error;
1326 static char errbuf[PCAP_ERRBUF_SIZE+1];
1327 int errlen;
1328 char *p;
1329
1330 error = GetLastError();
1331 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, 0, errbuf,
1332 PCAP_ERRBUF_SIZE, NULL);
1333
1334 /*
1335 * "FormatMessage()" "helpfully" sticks CR/LF at the end of the
1336 * message. Get rid of it.
1337 */
1338 errlen = strlen(errbuf);
1339 if (errlen >= 2) {
1340 errbuf[errlen - 1] = '\0';
1341 errbuf[errlen - 2] = '\0';
1342 }
1343 p = strchr(errbuf, '\0');
1344 snprintf (p, sizeof(errbuf)-(p-errbuf), " (%lu)", error);
1345 return (errbuf);
1346 }
1347 #endif
1348
1349 /*
1350 * Generate error strings for PCAP_ERROR_ and PCAP_WARNING_ values.
1351 */
1352 const char *
1353 pcap_statustostr(int errnum)
1354 {
1355 static char ebuf[15+10+1];
1356
1357 switch (errnum) {
1358
1359 case PCAP_WARNING:
1360 return("Generic warning");
1361
1362 case PCAP_WARNING_TSTAMP_TYPE_NOTSUP:
1363 return ("That type of time stamp is not supported by that device");
1364
1365 case PCAP_WARNING_PROMISC_NOTSUP:
1366 return ("That device doesn't support promiscuous mode");
1367
1368 case PCAP_ERROR:
1369 return("Generic error");
1370
1371 case PCAP_ERROR_BREAK:
1372 return("Loop terminated by pcap_breakloop");
1373
1374 case PCAP_ERROR_NOT_ACTIVATED:
1375 return("The pcap_t has not been activated");
1376
1377 case PCAP_ERROR_ACTIVATED:
1378 return ("The setting can't be changed after the pcap_t is activated");
1379
1380 case PCAP_ERROR_NO_SUCH_DEVICE:
1381 return ("No such device exists");
1382
1383 case PCAP_ERROR_RFMON_NOTSUP:
1384 return ("That device doesn't support monitor mode");
1385
1386 case PCAP_ERROR_NOT_RFMON:
1387 return ("That operation is supported only in monitor mode");
1388
1389 case PCAP_ERROR_PERM_DENIED:
1390 return ("You don't have permission to capture on that device");
1391
1392 case PCAP_ERROR_IFACE_NOT_UP:
1393 return ("That device is not up");
1394
1395 case PCAP_ERROR_CANTSET_TSTAMP_TYPE:
1396 return ("That device doesn't support setting the time stamp type");
1397
1398 case PCAP_ERROR_PROMISC_PERM_DENIED:
1399 return ("You don't have permission to capture in promiscuous mode on that device");
1400 }
1401 (void)snprintf(ebuf, sizeof ebuf, "Unknown error: %d", errnum);
1402 return(ebuf);
1403 }
1404
1405 /*
1406 * Not all systems have strerror().
1407 */
1408 const char *
1409 pcap_strerror(int errnum)
1410 {
1411 #ifdef HAVE_STRERROR
1412 return (strerror(errnum));
1413 #else
1414 extern int sys_nerr;
1415 extern const char *const sys_errlist[];
1416 static char ebuf[15+10+1];
1417
1418 if ((unsigned int)errnum < sys_nerr)
1419 return ((char *)sys_errlist[errnum]);
1420 (void)snprintf(ebuf, sizeof ebuf, "Unknown error: %d", errnum);
1421 return(ebuf);
1422 #endif
1423 }
1424
1425 int
1426 pcap_setfilter(pcap_t *p, struct bpf_program *fp)
1427 {
1428 return (p->setfilter_op(p, fp));
1429 }
1430
1431 /*
1432 * Set direction flag, which controls whether we accept only incoming
1433 * packets, only outgoing packets, or both.
1434 * Note that, depending on the platform, some or all direction arguments
1435 * might not be supported.
1436 */
1437 int
1438 pcap_setdirection(pcap_t *p, pcap_direction_t d)
1439 {
1440 if (p->setdirection_op == NULL) {
1441 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1442 "Setting direction is not implemented on this platform");
1443 return (-1);
1444 } else
1445 return (p->setdirection_op(p, d));
1446 }
1447
1448 int
1449 pcap_stats(pcap_t *p, struct pcap_stat *ps)
1450 {
1451 return (p->stats_op(p, ps));
1452 }
1453
1454 static int
1455 pcap_stats_dead(pcap_t *p, struct pcap_stat *ps _U_)
1456 {
1457 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1458 "Statistics aren't available from a pcap_open_dead pcap_t");
1459 return (-1);
1460 }
1461
1462 #ifdef WIN32
1463 int
1464 pcap_setbuff(pcap_t *p, int dim)
1465 {
1466 return (p->setbuff_op(p, dim));
1467 }
1468
1469 static int
1470 pcap_setbuff_dead(pcap_t *p, int dim)
1471 {
1472 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1473 "The kernel buffer size cannot be set on a pcap_open_dead pcap_t");
1474 return (-1);
1475 }
1476
1477 int
1478 pcap_setmode(pcap_t *p, int mode)
1479 {
1480 return (p->setmode_op(p, mode));
1481 }
1482
1483 static int
1484 pcap_setmode_dead(pcap_t *p, int mode)
1485 {
1486 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1487 "impossible to set mode on a pcap_open_dead pcap_t");
1488 return (-1);
1489 }
1490
1491 int
1492 pcap_setmintocopy(pcap_t *p, int size)
1493 {
1494 return (p->setmintocopy_op(p, size));
1495 }
1496
1497 static int
1498 pcap_setmintocopy_dead(pcap_t *p, int size)
1499 {
1500 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1501 "The mintocopy parameter cannot be set on a pcap_open_dead pcap_t");
1502 return (-1);
1503 }
1504 #endif
1505
1506 /*
1507 * On some platforms, we need to clean up promiscuous or monitor mode
1508 * when we close a device - and we want that to happen even if the
1509 * application just exits without explicitl closing devices.
1510 * On those platforms, we need to register a "close all the pcaps"
1511 * routine to be called when we exit, and need to maintain a list of
1512 * pcaps that need to be closed to clean up modes.
1513 *
1514 * XXX - not thread-safe.
1515 */
1516
1517 /*
1518 * List of pcaps on which we've done something that needs to be
1519 * cleaned up.
1520 * If there are any such pcaps, we arrange to call "pcap_close_all()"
1521 * when we exit, and have it close all of them.
1522 */
1523 static struct pcap *pcaps_to_close;
1524
1525 /*
1526 * TRUE if we've already called "atexit()" to cause "pcap_close_all()" to
1527 * be called on exit.
1528 */
1529 static int did_atexit;
1530
1531 static void
1532 pcap_close_all(void)
1533 {
1534 struct pcap *handle;
1535
1536 while ((handle = pcaps_to_close) != NULL)
1537 pcap_close(handle);
1538 }
1539
1540 int
1541 pcap_do_addexit(pcap_t *p)
1542 {
1543 /*
1544 * If we haven't already done so, arrange to have
1545 * "pcap_close_all()" called when we exit.
1546 */
1547 if (!did_atexit) {
1548 if (atexit(pcap_close_all) == -1) {
1549 /*
1550 * "atexit()" failed; let our caller know.
1551 */
1552 strncpy(p->errbuf, "atexit failed",
1553 PCAP_ERRBUF_SIZE);
1554 return (0);
1555 }
1556 did_atexit = 1;
1557 }
1558 return (1);
1559 }
1560
1561 void
1562 pcap_add_to_pcaps_to_close(pcap_t *p)
1563 {
1564 p->md.next = pcaps_to_close;
1565 pcaps_to_close = p;
1566 }
1567
1568 void
1569 pcap_remove_from_pcaps_to_close(pcap_t *p)
1570 {
1571 pcap_t *pc, *prevpc;
1572
1573 for (pc = pcaps_to_close, prevpc = NULL; pc != NULL;
1574 prevpc = pc, pc = pc->md.next) {
1575 if (pc == p) {
1576 /*
1577 * Found it. Remove it from the list.
1578 */
1579 if (prevpc == NULL) {
1580 /*
1581 * It was at the head of the list.
1582 */
1583 pcaps_to_close = pc->md.next;
1584 } else {
1585 /*
1586 * It was in the middle of the list.
1587 */
1588 prevpc->md.next = pc->md.next;
1589 }
1590 break;
1591 }
1592 }
1593 }
1594
1595 void
1596 pcap_cleanup_live_common(pcap_t *p)
1597 {
1598 if (p->buffer != NULL) {
1599 free(p->buffer);
1600 p->buffer = NULL;
1601 }
1602 if (p->dlt_list != NULL) {
1603 free(p->dlt_list);
1604 p->dlt_list = NULL;
1605 p->dlt_count = 0;
1606 }
1607 if (p->tstamp_type_list != NULL) {
1608 free(p->tstamp_type_list);
1609 p->tstamp_type_list = NULL;
1610 p->tstamp_type_count = 0;
1611 }
1612 pcap_freecode(&p->fcode);
1613 #if !defined(WIN32) && !defined(MSDOS)
1614 if (p->fd >= 0) {
1615 close(p->fd);
1616 p->fd = -1;
1617 }
1618 p->selectable_fd = -1;
1619 p->send_fd = -1;
1620 #endif
1621 }
1622
1623 static void
1624 pcap_cleanup_dead(pcap_t *p _U_)
1625 {
1626 /* Nothing to do. */
1627 }
1628
1629 pcap_t *
1630 pcap_open_dead(int linktype, int snaplen)
1631 {
1632 pcap_t *p;
1633
1634 p = malloc(sizeof(*p));
1635 if (p == NULL)
1636 return NULL;
1637 memset (p, 0, sizeof(*p));
1638 p->snapshot = snaplen;
1639 p->linktype = linktype;
1640 p->stats_op = pcap_stats_dead;
1641 #ifdef WIN32
1642 p->setbuff_op = pcap_setbuff_dead;
1643 p->setmode_op = pcap_setmode_dead;
1644 p->setmintocopy_op = pcap_setmintocopy_dead;
1645 #endif
1646 p->cleanup_op = pcap_cleanup_dead;
1647 p->activated = 1;
1648 return (p);
1649 }
1650
1651 /*
1652 * API compatible with WinPcap's "send a packet" routine - returns -1
1653 * on error, 0 otherwise.
1654 *
1655 * XXX - what if we get a short write?
1656 */
1657 int
1658 pcap_sendpacket(pcap_t *p, const u_char *buf, int size)
1659 {
1660 if (p->inject_op(p, buf, size) == -1)
1661 return (-1);
1662 return (0);
1663 }
1664
1665 /*
1666 * API compatible with OpenBSD's "send a packet" routine - returns -1 on
1667 * error, number of bytes written otherwise.
1668 */
1669 int
1670 pcap_inject(pcap_t *p, const void *buf, size_t size)
1671 {
1672 return (p->inject_op(p, buf, size));
1673 }
1674
1675 void
1676 pcap_close(pcap_t *p)
1677 {
1678 if (p->opt.source != NULL)
1679 free(p->opt.source);
1680 p->cleanup_op(p);
1681 free(p);
1682 }
1683
1684 /*
1685 * Given a BPF program, a pcap_pkthdr structure for a packet, and the raw
1686 * data for the packet, check whether the packet passes the filter.
1687 * Returns the return value of the filter program, which will be zero if
1688 * the packet doesn't pass and non-zero if the packet does pass.
1689 */
1690 int
1691 pcap_offline_filter(const struct bpf_program *fp, const struct pcap_pkthdr *h,
1692 const u_char *pkt)
1693 {
1694 const struct bpf_insn *fcode = fp->bf_insns;
1695
1696 if (fcode != NULL)
1697 return (bpf_filter(fcode, pkt, h->len, h->caplen));
1698 else
1699 return (0);
1700 }
1701
1702 /*
1703 * We make the version string static, and return a pointer to it, rather
1704 * than exporting the version string directly. On at least some UNIXes,
1705 * if you import data from a shared library into an program, the data is
1706 * bound into the program binary, so if the string in the version of the
1707 * library with which the program was linked isn't the same as the
1708 * string in the version of the library with which the program is being
1709 * run, various undesirable things may happen (warnings, the string
1710 * being the one from the version of the library with which the program
1711 * was linked, or even weirder things, such as the string being the one
1712 * from the library but being truncated).
1713 */
1714 #ifdef HAVE_VERSION_H
1715 #include "version.h"
1716 #else
1717 static const char pcap_version_string[] = "libpcap version 1.x.y";
1718 #endif
1719
1720 #ifdef WIN32
1721 /*
1722 * XXX - it'd be nice if we could somehow generate the WinPcap and libpcap
1723 * version numbers when building WinPcap. (It'd be nice to do so for
1724 * the packet.dll version number as well.)
1725 */
1726 static const char wpcap_version_string[] = "4.0";
1727 static const char pcap_version_string_fmt[] =
1728 "WinPcap version %s, based on %s";
1729 static const char pcap_version_string_packet_dll_fmt[] =
1730 "WinPcap version %s (packet.dll version %s), based on %s";
1731 static char *full_pcap_version_string;
1732
1733 const char *
1734 pcap_lib_version(void)
1735 {
1736 char *packet_version_string;
1737 size_t full_pcap_version_string_len;
1738
1739 if (full_pcap_version_string == NULL) {
1740 /*
1741 * Generate the version string.
1742 */
1743 packet_version_string = PacketGetVersion();
1744 if (strcmp(wpcap_version_string, packet_version_string) == 0) {
1745 /*
1746 * WinPcap version string and packet.dll version
1747 * string are the same; just report the WinPcap
1748 * version.
1749 */
1750 full_pcap_version_string_len =
1751 (sizeof pcap_version_string_fmt - 4) +
1752 strlen(wpcap_version_string) +
1753 strlen(pcap_version_string);
1754 full_pcap_version_string =
1755 malloc(full_pcap_version_string_len);
1756 if (full_pcap_version_string == NULL)
1757 return (NULL);
1758 sprintf(full_pcap_version_string,
1759 pcap_version_string_fmt, wpcap_version_string,
1760 pcap_version_string);
1761 } else {
1762 /*
1763 * WinPcap version string and packet.dll version
1764 * string are different; that shouldn't be the
1765 * case (the two libraries should come from the
1766 * same version of WinPcap), so we report both
1767 * versions.
1768 */
1769 full_pcap_version_string_len =
1770 (sizeof pcap_version_string_packet_dll_fmt - 6) +
1771 strlen(wpcap_version_string) +
1772 strlen(packet_version_string) +
1773 strlen(pcap_version_string);
1774 full_pcap_version_string = malloc(full_pcap_version_string_len);
1775 if (full_pcap_version_string == NULL)
1776 return (NULL);
1777 sprintf(full_pcap_version_string,
1778 pcap_version_string_packet_dll_fmt,
1779 wpcap_version_string, packet_version_string,
1780 pcap_version_string);
1781 }
1782 }
1783 return (full_pcap_version_string);
1784 }
1785
1786 #elif defined(MSDOS)
1787
1788 static char *full_pcap_version_string;
1789
1790 const char *
1791 pcap_lib_version (void)
1792 {
1793 char *packet_version_string;
1794 size_t full_pcap_version_string_len;
1795 static char dospfx[] = "DOS-";
1796
1797 if (full_pcap_version_string == NULL) {
1798 /*
1799 * Generate the version string.
1800 */
1801 full_pcap_version_string_len =
1802 sizeof dospfx + strlen(pcap_version_string);
1803 full_pcap_version_string =
1804 malloc(full_pcap_version_string_len);
1805 if (full_pcap_version_string == NULL)
1806 return (NULL);
1807 strcpy(full_pcap_version_string, dospfx);
1808 strcat(full_pcap_version_string, pcap_version_string);
1809 }
1810 return (full_pcap_version_string);
1811 }
1812
1813 #else /* UN*X */
1814
1815 const char *
1816 pcap_lib_version(void)
1817 {
1818 return (pcap_version_string);
1819 }
1820 #endif