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