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