xref: /freebsd/contrib/libpcap/pcap.c (revision 409a390c3341fb4f162cd7de1fd595a323ebbfd8)
1 /*
2  * Copyright (c) 1993, 1994, 1995, 1996, 1997, 1998
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the Computer Systems
16  *	Engineering Group at Lawrence Berkeley Laboratory.
17  * 4. Neither the name of the University nor of the Laboratory may be used
18  *    to endorse or promote products derived from this software without
19  *    specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 static const char rcsid[] _U_ =
36     "@(#) $Header: /tcpdump/master/libpcap/pcap.c,v 1.112.2.12 2008-09-22 20:16:01 guy Exp $ (LBL)";
37 #endif
38 
39 #ifdef HAVE_CONFIG_H
40 #include "config.h"
41 #endif
42 
43 #ifdef WIN32
44 #include <pcap-stdinc.h>
45 #else /* WIN32 */
46 #include <sys/types.h>
47 #include <sys/mman.h>
48 #endif /* WIN32 */
49 
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #if !defined(_MSC_VER) && !defined(__BORLANDC__)
54 #include <unistd.h>
55 #endif
56 #include <fcntl.h>
57 #include <errno.h>
58 
59 #ifdef HAVE_OS_PROTO_H
60 #include "os-proto.h"
61 #endif
62 
63 #ifdef MSDOS
64 #include "pcap-dos.h"
65 #endif
66 
67 #include "pcap-int.h"
68 
69 #ifdef HAVE_DAG_API
70 #include <dagnew.h>
71 #include <dagapi.h>
72 #endif
73 
74 int
75 pcap_not_initialized(pcap_t *pcap)
76 {
77 	/* this means 'not initialized' */
78 	return PCAP_ERROR_NOT_ACTIVATED;
79 }
80 
81 /*
82  * Returns 1 if rfmon mode can be set on the pcap_t, 0 if it can't,
83  * a PCAP_ERROR value on an error.
84  */
85 int
86 pcap_can_set_rfmon(pcap_t *p)
87 {
88 	return (p->can_set_rfmon_op(p));
89 }
90 
91 /*
92  * For systems where rfmon mode is never supported.
93  */
94 static int
95 pcap_cant_set_rfmon(pcap_t *p _U_)
96 {
97 	return (0);
98 }
99 
100 pcap_t *
101 pcap_create_common(const char *source, char *ebuf)
102 {
103 	pcap_t *p;
104 
105 	p = malloc(sizeof(*p));
106 	if (p == NULL) {
107 		snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s",
108 		    pcap_strerror(errno));
109 		return (NULL);
110 	}
111 	memset(p, 0, sizeof(*p));
112 #ifndef WIN32
113 	p->fd = -1;	/* not opened yet */
114 #endif
115 
116 	p->opt.source = strdup(source);
117 	if (p->opt.source == NULL) {
118 		snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s",
119 		    pcap_strerror(errno));
120 		free(p);
121 		return (NULL);
122 	}
123 
124 	/*
125 	 * Default to "can't set rfmon mode"; if it's supported by
126 	 * a platform, it can set the op to its routine to check
127 	 * whether a particular device supports it.
128 	 */
129 	p->can_set_rfmon_op = pcap_cant_set_rfmon;
130 
131 	/*
132 	 * Some operations can be performed only on activated pcap_t's;
133 	 * have those operations handled by a "not supported" handler
134 	 * until the pcap_t is activated.
135 	 */
136 	p->read_op = (read_op_t)pcap_not_initialized;
137 	p->inject_op = (inject_op_t)pcap_not_initialized;
138 	p->setfilter_op = (setfilter_op_t)pcap_not_initialized;
139 	p->setdirection_op = (setdirection_op_t)pcap_not_initialized;
140 	p->set_datalink_op = (set_datalink_op_t)pcap_not_initialized;
141 	p->getnonblock_op = (getnonblock_op_t)pcap_not_initialized;
142 	p->setnonblock_op = (setnonblock_op_t)pcap_not_initialized;
143 	p->stats_op = (stats_op_t)pcap_not_initialized;
144 #ifdef WIN32
145 	p->setbuff_op = (setbuff_op_t)pcap_not_initialized;
146 	p->setmode_op = (setmode_op_t)pcap_not_initialized;
147 	p->setmintocopy_op = (setmintocopy_op_t)pcap_not_initialized;
148 #endif
149 	p->cleanup_op = pcap_cleanup_live_common;
150 
151 	/* put in some defaults*/
152 	pcap_set_timeout(p, 0);
153 	pcap_set_snaplen(p, 65535);	/* max packet size */
154 	p->opt.promisc = 0;
155 	p->opt.buffer_size = 0;
156 	return (p);
157 }
158 
159 int
160 pcap_check_activated(pcap_t *p)
161 {
162 	if (p->activated) {
163 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "can't perform "
164 			" operation on activated capture");
165 		return -1;
166 	}
167 	return 0;
168 }
169 
170 int
171 pcap_set_snaplen(pcap_t *p, int snaplen)
172 {
173 	if (pcap_check_activated(p))
174 		return PCAP_ERROR_ACTIVATED;
175 	p->snapshot = snaplen;
176 	return 0;
177 }
178 
179 int
180 pcap_set_promisc(pcap_t *p, int promisc)
181 {
182 	if (pcap_check_activated(p))
183 		return PCAP_ERROR_ACTIVATED;
184 	p->opt.promisc = promisc;
185 	return 0;
186 }
187 
188 int
189 pcap_set_rfmon(pcap_t *p, int rfmon)
190 {
191 	if (pcap_check_activated(p))
192 		return PCAP_ERROR_ACTIVATED;
193 	p->opt.rfmon = rfmon;
194 	return 0;
195 }
196 
197 int
198 pcap_set_timeout(pcap_t *p, int timeout_ms)
199 {
200 	if (pcap_check_activated(p))
201 		return PCAP_ERROR_ACTIVATED;
202 	p->md.timeout = timeout_ms;
203 	return 0;
204 }
205 
206 int
207 pcap_set_buffer_size(pcap_t *p, int buffer_size)
208 {
209 	if (pcap_check_activated(p))
210 		return PCAP_ERROR_ACTIVATED;
211 	p->opt.buffer_size = buffer_size;
212 	return 0;
213 }
214 
215 int
216 pcap_activate(pcap_t *p)
217 {
218 	int status;
219 
220 	status = p->activate_op(p);
221 	if (status >= 0)
222 		p->activated = 1;
223 	return (status);
224 }
225 
226 pcap_t *
227 pcap_open_live(const char *source, int snaplen, int promisc, int to_ms, char *errbuf)
228 {
229 	pcap_t *p;
230 	int status;
231 
232 	p = pcap_create(source, errbuf);
233 	if (p == NULL)
234 		return (NULL);
235 	status = pcap_set_snaplen(p, snaplen);
236 	if (status < 0)
237 		goto fail;
238 	status = pcap_set_promisc(p, promisc);
239 	if (status < 0)
240 		goto fail;
241 	status = pcap_set_timeout(p, to_ms);
242 	if (status < 0)
243 		goto fail;
244 	/*
245 	 * Mark this as opened with pcap_open_live(), so that, for
246 	 * example, we show the full list of DLT_ values, rather
247 	 * than just the ones that are compatible with capturing
248 	 * when not in monitor mode.  That allows existing applications
249 	 * to work the way they used to work, but allows new applications
250 	 * that know about the new open API to, for example, find out the
251 	 * DLT_ values that they can select without changing whether
252 	 * the adapter is in monitor mode or not.
253 	 */
254 	p->oldstyle = 1;
255 	status = pcap_activate(p);
256 	if (status < 0)
257 		goto fail;
258 	return (p);
259 fail:
260 	if (status == PCAP_ERROR || status == PCAP_ERROR_NO_SUCH_DEVICE ||
261 	    status == PCAP_ERROR_PERM_DENIED)
262 		strlcpy(errbuf, p->errbuf, PCAP_ERRBUF_SIZE);
263 	else
264 		snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s", source,
265 		    pcap_statustostr(status));
266 	pcap_close(p);
267 	return (NULL);
268 }
269 
270 int
271 pcap_dispatch(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
272 {
273 	return p->read_op(p, cnt, callback, user);
274 }
275 
276 /*
277  * XXX - is this necessary?
278  */
279 int
280 pcap_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
281 {
282 
283 	return p->read_op(p, cnt, callback, user);
284 }
285 
286 int
287 pcap_loop(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
288 {
289 	register int n;
290 
291 	for (;;) {
292 		if (p->sf.rfile != NULL) {
293 			/*
294 			 * 0 means EOF, so don't loop if we get 0.
295 			 */
296 			n = pcap_offline_read(p, cnt, callback, user);
297 		} else {
298 			/*
299 			 * XXX keep reading until we get something
300 			 * (or an error occurs)
301 			 */
302 			do {
303 				n = p->read_op(p, cnt, callback, user);
304 			} while (n == 0);
305 		}
306 		if (n <= 0)
307 			return (n);
308 		if (cnt > 0) {
309 			cnt -= n;
310 			if (cnt <= 0)
311 				return (0);
312 		}
313 	}
314 }
315 
316 struct singleton {
317 	struct pcap_pkthdr *hdr;
318 	const u_char *pkt;
319 };
320 
321 
322 static void
323 pcap_oneshot(u_char *userData, const struct pcap_pkthdr *h, const u_char *pkt)
324 {
325 	struct singleton *sp = (struct singleton *)userData;
326 	*sp->hdr = *h;
327 	sp->pkt = pkt;
328 }
329 
330 const u_char *
331 pcap_next(pcap_t *p, struct pcap_pkthdr *h)
332 {
333 	struct singleton s;
334 
335 	s.hdr = h;
336 	if (pcap_dispatch(p, 1, pcap_oneshot, (u_char*)&s) <= 0)
337 		return (0);
338 	return (s.pkt);
339 }
340 
341 struct pkt_for_fakecallback {
342 	struct pcap_pkthdr *hdr;
343 	const u_char **pkt;
344 };
345 
346 static void
347 pcap_fakecallback(u_char *userData, const struct pcap_pkthdr *h,
348     const u_char *pkt)
349 {
350 	struct pkt_for_fakecallback *sp = (struct pkt_for_fakecallback *)userData;
351 
352 	*sp->hdr = *h;
353 	*sp->pkt = pkt;
354 }
355 
356 int
357 pcap_next_ex(pcap_t *p, struct pcap_pkthdr **pkt_header,
358     const u_char **pkt_data)
359 {
360 	struct pkt_for_fakecallback s;
361 
362 	s.hdr = &p->pcap_header;
363 	s.pkt = pkt_data;
364 
365 	/* Saves a pointer to the packet headers */
366 	*pkt_header= &p->pcap_header;
367 
368 	if (p->sf.rfile != NULL) {
369 		int status;
370 
371 		/* We are on an offline capture */
372 		status = pcap_offline_read(p, 1, pcap_fakecallback,
373 		    (u_char *)&s);
374 
375 		/*
376 		 * Return codes for pcap_offline_read() are:
377 		 *   -  0: EOF
378 		 *   - -1: error
379 		 *   - >1: OK
380 		 * The first one ('0') conflicts with the return code of
381 		 * 0 from pcap_read() meaning "no packets arrived before
382 		 * the timeout expired", so we map it to -2 so you can
383 		 * distinguish between an EOF from a savefile and a
384 		 * "no packets arrived before the timeout expired, try
385 		 * again" from a live capture.
386 		 */
387 		if (status == 0)
388 			return (-2);
389 		else
390 			return (status);
391 	}
392 
393 	/*
394 	 * Return codes for pcap_read() are:
395 	 *   -  0: timeout
396 	 *   - -1: error
397 	 *   - -2: loop was broken out of with pcap_breakloop()
398 	 *   - >1: OK
399 	 * The first one ('0') conflicts with the return code of 0 from
400 	 * pcap_offline_read() meaning "end of file".
401 	*/
402 	return (p->read_op(p, 1, pcap_fakecallback, (u_char *)&s));
403 }
404 
405 /*
406  * Force the loop in "pcap_read()" or "pcap_read_offline()" to terminate.
407  */
408 void
409 pcap_breakloop(pcap_t *p)
410 {
411 	p->break_loop = 1;
412 }
413 
414 int
415 pcap_datalink(pcap_t *p)
416 {
417 	return (p->linktype);
418 }
419 
420 int
421 pcap_datalink_ext(pcap_t *p)
422 {
423 	return (p->linktype_ext);
424 }
425 
426 int
427 pcap_list_datalinks(pcap_t *p, int **dlt_buffer)
428 {
429 	if (p->dlt_count == 0) {
430 		/*
431 		 * We couldn't fetch the list of DLTs, which means
432 		 * this platform doesn't support changing the
433 		 * DLT for an interface.  Return a list of DLTs
434 		 * containing only the DLT this device supports.
435 		 */
436 		*dlt_buffer = (int*)malloc(sizeof(**dlt_buffer));
437 		if (*dlt_buffer == NULL) {
438 			(void)snprintf(p->errbuf, sizeof(p->errbuf),
439 			    "malloc: %s", pcap_strerror(errno));
440 			return (-1);
441 		}
442 		**dlt_buffer = p->linktype;
443 		return (1);
444 	} else {
445 		*dlt_buffer = (int*)calloc(sizeof(**dlt_buffer), p->dlt_count);
446 		if (*dlt_buffer == NULL) {
447 			(void)snprintf(p->errbuf, sizeof(p->errbuf),
448 			    "malloc: %s", pcap_strerror(errno));
449 			return (-1);
450 		}
451 		(void)memcpy(*dlt_buffer, p->dlt_list,
452 		    sizeof(**dlt_buffer) * p->dlt_count);
453 		return (p->dlt_count);
454 	}
455 }
456 
457 /*
458  * In Windows, you might have a library built with one version of the
459  * C runtime library and an application built with another version of
460  * the C runtime library, which means that the library might use one
461  * version of malloc() and free() and the application might use another
462  * version of malloc() and free().  If so, that means something
463  * allocated by the library cannot be freed by the application, so we
464  * need to have a pcap_free_datalinks() routine to free up the list
465  * allocated by pcap_list_datalinks(), even though it's just a wrapper
466  * around free().
467  */
468 void
469 pcap_free_datalinks(int *dlt_list)
470 {
471 	free(dlt_list);
472 }
473 
474 int
475 pcap_set_datalink(pcap_t *p, int dlt)
476 {
477 	int i;
478 	const char *dlt_name;
479 
480 	if (p->dlt_count == 0 || p->set_datalink_op == NULL) {
481 		/*
482 		 * We couldn't fetch the list of DLTs, or we don't
483 		 * have a "set datalink" operation, which means
484 		 * this platform doesn't support changing the
485 		 * DLT for an interface.  Check whether the new
486 		 * DLT is the one this interface supports.
487 		 */
488 		if (p->linktype != dlt)
489 			goto unsupported;
490 
491 		/*
492 		 * It is, so there's nothing we need to do here.
493 		 */
494 		return (0);
495 	}
496 	for (i = 0; i < p->dlt_count; i++)
497 		if (p->dlt_list[i] == dlt)
498 			break;
499 	if (i >= p->dlt_count)
500 		goto unsupported;
501 	if (p->dlt_count == 2 && p->dlt_list[0] == DLT_EN10MB &&
502 	    dlt == DLT_DOCSIS) {
503 		/*
504 		 * This is presumably an Ethernet device, as the first
505 		 * link-layer type it offers is DLT_EN10MB, and the only
506 		 * other type it offers is DLT_DOCSIS.  That means that
507 		 * we can't tell the driver to supply DOCSIS link-layer
508 		 * headers - we're just pretending that's what we're
509 		 * getting, as, presumably, we're capturing on a dedicated
510 		 * link to a Cisco Cable Modem Termination System, and
511 		 * it's putting raw DOCSIS frames on the wire inside low-level
512 		 * Ethernet framing.
513 		 */
514 		p->linktype = dlt;
515 		return (0);
516 	}
517 	if (p->set_datalink_op(p, dlt) == -1)
518 		return (-1);
519 	p->linktype = dlt;
520 	return (0);
521 
522 unsupported:
523 	dlt_name = pcap_datalink_val_to_name(dlt);
524 	if (dlt_name != NULL) {
525 		(void) snprintf(p->errbuf, sizeof(p->errbuf),
526 		    "%s is not one of the DLTs supported by this device",
527 		    dlt_name);
528 	} else {
529 		(void) snprintf(p->errbuf, sizeof(p->errbuf),
530 		    "DLT %d is not one of the DLTs supported by this device",
531 		    dlt);
532 	}
533 	return (-1);
534 }
535 
536 struct dlt_choice {
537 	const char *name;
538 	const char *description;
539 	int	dlt;
540 };
541 
542 #define DLT_CHOICE(code, description) { #code, description, code }
543 #define DLT_CHOICE_SENTINEL { NULL, NULL, 0 }
544 
545 static struct dlt_choice dlt_choices[] = {
546 	DLT_CHOICE(DLT_NULL, "BSD loopback"),
547 	DLT_CHOICE(DLT_EN10MB, "Ethernet"),
548 	DLT_CHOICE(DLT_IEEE802, "Token ring"),
549 	DLT_CHOICE(DLT_ARCNET, "BSD ARCNET"),
550 	DLT_CHOICE(DLT_SLIP, "SLIP"),
551 	DLT_CHOICE(DLT_PPP, "PPP"),
552 	DLT_CHOICE(DLT_FDDI, "FDDI"),
553 	DLT_CHOICE(DLT_ATM_RFC1483, "RFC 1483 LLC-encapsulated ATM"),
554 	DLT_CHOICE(DLT_RAW, "Raw IP"),
555 	DLT_CHOICE(DLT_SLIP_BSDOS, "BSD/OS SLIP"),
556 	DLT_CHOICE(DLT_PPP_BSDOS, "BSD/OS PPP"),
557 	DLT_CHOICE(DLT_ATM_CLIP, "Linux Classical IP-over-ATM"),
558 	DLT_CHOICE(DLT_PPP_SERIAL, "PPP over serial"),
559 	DLT_CHOICE(DLT_PPP_ETHER, "PPPoE"),
560         DLT_CHOICE(DLT_SYMANTEC_FIREWALL, "Symantec Firewall"),
561 	DLT_CHOICE(DLT_C_HDLC, "Cisco HDLC"),
562 	DLT_CHOICE(DLT_IEEE802_11, "802.11"),
563 	DLT_CHOICE(DLT_FRELAY, "Frame Relay"),
564 	DLT_CHOICE(DLT_LOOP, "OpenBSD loopback"),
565 	DLT_CHOICE(DLT_ENC, "OpenBSD encapsulated IP"),
566 	DLT_CHOICE(DLT_LINUX_SLL, "Linux cooked"),
567 	DLT_CHOICE(DLT_LTALK, "Localtalk"),
568 	DLT_CHOICE(DLT_PFLOG, "OpenBSD pflog file"),
569 	DLT_CHOICE(DLT_PRISM_HEADER, "802.11 plus Prism header"),
570 	DLT_CHOICE(DLT_IP_OVER_FC, "RFC 2625 IP-over-Fibre Channel"),
571 	DLT_CHOICE(DLT_SUNATM, "Sun raw ATM"),
572 	DLT_CHOICE(DLT_IEEE802_11_RADIO, "802.11 plus radiotap header"),
573 	DLT_CHOICE(DLT_ARCNET_LINUX, "Linux ARCNET"),
574         DLT_CHOICE(DLT_JUNIPER_MLPPP, "Juniper Multi-Link PPP"),
575 	DLT_CHOICE(DLT_JUNIPER_MLFR, "Juniper Multi-Link Frame Relay"),
576         DLT_CHOICE(DLT_JUNIPER_ES, "Juniper Encryption Services PIC"),
577         DLT_CHOICE(DLT_JUNIPER_GGSN, "Juniper GGSN PIC"),
578 	DLT_CHOICE(DLT_JUNIPER_MFR, "Juniper FRF.16 Frame Relay"),
579         DLT_CHOICE(DLT_JUNIPER_ATM2, "Juniper ATM2 PIC"),
580         DLT_CHOICE(DLT_JUNIPER_SERVICES, "Juniper Advanced Services PIC"),
581         DLT_CHOICE(DLT_JUNIPER_ATM1, "Juniper ATM1 PIC"),
582 	DLT_CHOICE(DLT_APPLE_IP_OVER_IEEE1394, "Apple IP-over-IEEE 1394"),
583 	DLT_CHOICE(DLT_MTP2_WITH_PHDR, "SS7 MTP2 with Pseudo-header"),
584 	DLT_CHOICE(DLT_MTP2, "SS7 MTP2"),
585 	DLT_CHOICE(DLT_MTP3, "SS7 MTP3"),
586 	DLT_CHOICE(DLT_SCCP, "SS7 SCCP"),
587 	DLT_CHOICE(DLT_DOCSIS, "DOCSIS"),
588 	DLT_CHOICE(DLT_LINUX_IRDA, "Linux IrDA"),
589 	DLT_CHOICE(DLT_IEEE802_11_RADIO_AVS, "802.11 plus AVS radio information header"),
590         DLT_CHOICE(DLT_JUNIPER_MONITOR, "Juniper Passive Monitor PIC"),
591 	DLT_CHOICE(DLT_PPP_PPPD, "PPP for pppd, with direction flag"),
592 	DLT_CHOICE(DLT_JUNIPER_PPPOE, "Juniper PPPoE"),
593 	DLT_CHOICE(DLT_JUNIPER_PPPOE_ATM, "Juniper PPPoE/ATM"),
594 	DLT_CHOICE(DLT_GPRS_LLC, "GPRS LLC"),
595 	DLT_CHOICE(DLT_GPF_T, "GPF-T"),
596 	DLT_CHOICE(DLT_GPF_F, "GPF-F"),
597 	DLT_CHOICE(DLT_JUNIPER_PIC_PEER, "Juniper PIC Peer"),
598 	DLT_CHOICE(DLT_ERF_ETH,	"Ethernet with Endace ERF header"),
599 	DLT_CHOICE(DLT_ERF_POS, "Packet-over-SONET with Endace ERF header"),
600 	DLT_CHOICE(DLT_LINUX_LAPD, "Linux vISDN LAPD"),
601 	DLT_CHOICE(DLT_JUNIPER_ETHER, "Juniper Ethernet"),
602 	DLT_CHOICE(DLT_JUNIPER_PPP, "Juniper PPP"),
603 	DLT_CHOICE(DLT_JUNIPER_FRELAY, "Juniper Frame Relay"),
604 	DLT_CHOICE(DLT_JUNIPER_CHDLC, "Juniper C-HDLC"),
605 	DLT_CHOICE(DLT_MFR, "FRF.16 Frame Relay"),
606 	DLT_CHOICE(DLT_JUNIPER_VP, "Juniper Voice PIC"),
607 	DLT_CHOICE(DLT_A429, "Arinc 429"),
608 	DLT_CHOICE(DLT_A653_ICM, "Arinc 653 Interpartition Communication"),
609 	DLT_CHOICE(DLT_USB, "USB"),
610 	DLT_CHOICE(DLT_BLUETOOTH_HCI_H4, "Bluetooth HCI UART transport layer"),
611 	DLT_CHOICE(DLT_IEEE802_16_MAC_CPS, "IEEE 802.16 MAC Common Part Sublayer"),
612 	DLT_CHOICE(DLT_USB_LINUX, "USB with Linux header"),
613 	DLT_CHOICE(DLT_CAN20B, "Controller Area Network (CAN) v. 2.0B"),
614 	DLT_CHOICE(DLT_IEEE802_15_4_LINUX, "IEEE 802.15.4 with Linux padding"),
615 	DLT_CHOICE(DLT_PPI, "Per-Packet Information"),
616 	DLT_CHOICE(DLT_IEEE802_16_MAC_CPS_RADIO, "IEEE 802.16 MAC Common Part Sublayer plus radiotap header"),
617 	DLT_CHOICE(DLT_JUNIPER_ISM, "Juniper Integrated Service Module"),
618 	DLT_CHOICE(DLT_IEEE802_15_4, "IEEE 802.15.4"),
619 	DLT_CHOICE(DLT_SITA, "SITA pseudo-header"),
620 	DLT_CHOICE(DLT_ERF, "Endace ERF header"),
621 	DLT_CHOICE(DLT_RAIF1, "Ethernet with u10 Networks pseudo-header"),
622 	DLT_CHOICE(DLT_IPMB, "IPMB"),
623 	DLT_CHOICE(DLT_JUNIPER_ST, "Juniper Secure Tunnel"),
624 	DLT_CHOICE(DLT_BLUETOOTH_HCI_H4_WITH_PHDR, "Bluetooth HCI UART transport layer plus pseudo-header"),
625 	DLT_CHOICE(DLT_AX25_KISS, "AX.25 with KISS header"),
626 	DLT_CHOICE(DLT_IEEE802_15_4_NONASK_PHY, "IEEE 802.15.4 with non-ASK PHY data"),
627 	DLT_CHOICE_SENTINEL
628 };
629 
630 /*
631  * This array is designed for mapping upper and lower case letter
632  * together for a case independent comparison.  The mappings are
633  * based upon ascii character sequences.
634  */
635 static const u_char charmap[] = {
636 	(u_char)'\000', (u_char)'\001', (u_char)'\002', (u_char)'\003',
637 	(u_char)'\004', (u_char)'\005', (u_char)'\006', (u_char)'\007',
638 	(u_char)'\010', (u_char)'\011', (u_char)'\012', (u_char)'\013',
639 	(u_char)'\014', (u_char)'\015', (u_char)'\016', (u_char)'\017',
640 	(u_char)'\020', (u_char)'\021', (u_char)'\022', (u_char)'\023',
641 	(u_char)'\024', (u_char)'\025', (u_char)'\026', (u_char)'\027',
642 	(u_char)'\030', (u_char)'\031', (u_char)'\032', (u_char)'\033',
643 	(u_char)'\034', (u_char)'\035', (u_char)'\036', (u_char)'\037',
644 	(u_char)'\040', (u_char)'\041', (u_char)'\042', (u_char)'\043',
645 	(u_char)'\044', (u_char)'\045', (u_char)'\046', (u_char)'\047',
646 	(u_char)'\050', (u_char)'\051', (u_char)'\052', (u_char)'\053',
647 	(u_char)'\054', (u_char)'\055', (u_char)'\056', (u_char)'\057',
648 	(u_char)'\060', (u_char)'\061', (u_char)'\062', (u_char)'\063',
649 	(u_char)'\064', (u_char)'\065', (u_char)'\066', (u_char)'\067',
650 	(u_char)'\070', (u_char)'\071', (u_char)'\072', (u_char)'\073',
651 	(u_char)'\074', (u_char)'\075', (u_char)'\076', (u_char)'\077',
652 	(u_char)'\100', (u_char)'\141', (u_char)'\142', (u_char)'\143',
653 	(u_char)'\144', (u_char)'\145', (u_char)'\146', (u_char)'\147',
654 	(u_char)'\150', (u_char)'\151', (u_char)'\152', (u_char)'\153',
655 	(u_char)'\154', (u_char)'\155', (u_char)'\156', (u_char)'\157',
656 	(u_char)'\160', (u_char)'\161', (u_char)'\162', (u_char)'\163',
657 	(u_char)'\164', (u_char)'\165', (u_char)'\166', (u_char)'\167',
658 	(u_char)'\170', (u_char)'\171', (u_char)'\172', (u_char)'\133',
659 	(u_char)'\134', (u_char)'\135', (u_char)'\136', (u_char)'\137',
660 	(u_char)'\140', (u_char)'\141', (u_char)'\142', (u_char)'\143',
661 	(u_char)'\144', (u_char)'\145', (u_char)'\146', (u_char)'\147',
662 	(u_char)'\150', (u_char)'\151', (u_char)'\152', (u_char)'\153',
663 	(u_char)'\154', (u_char)'\155', (u_char)'\156', (u_char)'\157',
664 	(u_char)'\160', (u_char)'\161', (u_char)'\162', (u_char)'\163',
665 	(u_char)'\164', (u_char)'\165', (u_char)'\166', (u_char)'\167',
666 	(u_char)'\170', (u_char)'\171', (u_char)'\172', (u_char)'\173',
667 	(u_char)'\174', (u_char)'\175', (u_char)'\176', (u_char)'\177',
668 	(u_char)'\200', (u_char)'\201', (u_char)'\202', (u_char)'\203',
669 	(u_char)'\204', (u_char)'\205', (u_char)'\206', (u_char)'\207',
670 	(u_char)'\210', (u_char)'\211', (u_char)'\212', (u_char)'\213',
671 	(u_char)'\214', (u_char)'\215', (u_char)'\216', (u_char)'\217',
672 	(u_char)'\220', (u_char)'\221', (u_char)'\222', (u_char)'\223',
673 	(u_char)'\224', (u_char)'\225', (u_char)'\226', (u_char)'\227',
674 	(u_char)'\230', (u_char)'\231', (u_char)'\232', (u_char)'\233',
675 	(u_char)'\234', (u_char)'\235', (u_char)'\236', (u_char)'\237',
676 	(u_char)'\240', (u_char)'\241', (u_char)'\242', (u_char)'\243',
677 	(u_char)'\244', (u_char)'\245', (u_char)'\246', (u_char)'\247',
678 	(u_char)'\250', (u_char)'\251', (u_char)'\252', (u_char)'\253',
679 	(u_char)'\254', (u_char)'\255', (u_char)'\256', (u_char)'\257',
680 	(u_char)'\260', (u_char)'\261', (u_char)'\262', (u_char)'\263',
681 	(u_char)'\264', (u_char)'\265', (u_char)'\266', (u_char)'\267',
682 	(u_char)'\270', (u_char)'\271', (u_char)'\272', (u_char)'\273',
683 	(u_char)'\274', (u_char)'\275', (u_char)'\276', (u_char)'\277',
684 	(u_char)'\300', (u_char)'\341', (u_char)'\342', (u_char)'\343',
685 	(u_char)'\344', (u_char)'\345', (u_char)'\346', (u_char)'\347',
686 	(u_char)'\350', (u_char)'\351', (u_char)'\352', (u_char)'\353',
687 	(u_char)'\354', (u_char)'\355', (u_char)'\356', (u_char)'\357',
688 	(u_char)'\360', (u_char)'\361', (u_char)'\362', (u_char)'\363',
689 	(u_char)'\364', (u_char)'\365', (u_char)'\366', (u_char)'\367',
690 	(u_char)'\370', (u_char)'\371', (u_char)'\372', (u_char)'\333',
691 	(u_char)'\334', (u_char)'\335', (u_char)'\336', (u_char)'\337',
692 	(u_char)'\340', (u_char)'\341', (u_char)'\342', (u_char)'\343',
693 	(u_char)'\344', (u_char)'\345', (u_char)'\346', (u_char)'\347',
694 	(u_char)'\350', (u_char)'\351', (u_char)'\352', (u_char)'\353',
695 	(u_char)'\354', (u_char)'\355', (u_char)'\356', (u_char)'\357',
696 	(u_char)'\360', (u_char)'\361', (u_char)'\362', (u_char)'\363',
697 	(u_char)'\364', (u_char)'\365', (u_char)'\366', (u_char)'\367',
698 	(u_char)'\370', (u_char)'\371', (u_char)'\372', (u_char)'\373',
699 	(u_char)'\374', (u_char)'\375', (u_char)'\376', (u_char)'\377',
700 };
701 
702 int
703 pcap_strcasecmp(const char *s1, const char *s2)
704 {
705 	register const u_char	*cm = charmap,
706 				*us1 = (const u_char *)s1,
707 				*us2 = (const u_char *)s2;
708 
709 	while (cm[*us1] == cm[*us2++])
710 		if (*us1++ == '\0')
711 			return(0);
712 	return (cm[*us1] - cm[*--us2]);
713 }
714 
715 int
716 pcap_datalink_name_to_val(const char *name)
717 {
718 	int i;
719 
720 	for (i = 0; dlt_choices[i].name != NULL; i++) {
721 		if (pcap_strcasecmp(dlt_choices[i].name + sizeof("DLT_") - 1,
722 		    name) == 0)
723 			return (dlt_choices[i].dlt);
724 	}
725 	return (-1);
726 }
727 
728 const char *
729 pcap_datalink_val_to_name(int dlt)
730 {
731 	int i;
732 
733 	for (i = 0; dlt_choices[i].name != NULL; i++) {
734 		if (dlt_choices[i].dlt == dlt)
735 			return (dlt_choices[i].name + sizeof("DLT_") - 1);
736 	}
737 	return (NULL);
738 }
739 
740 const char *
741 pcap_datalink_val_to_description(int dlt)
742 {
743 	int i;
744 
745 	for (i = 0; dlt_choices[i].name != NULL; i++) {
746 		if (dlt_choices[i].dlt == dlt)
747 			return (dlt_choices[i].description);
748 	}
749 	return (NULL);
750 }
751 
752 int
753 pcap_snapshot(pcap_t *p)
754 {
755 	return (p->snapshot);
756 }
757 
758 int
759 pcap_is_swapped(pcap_t *p)
760 {
761 	return (p->sf.swapped);
762 }
763 
764 int
765 pcap_major_version(pcap_t *p)
766 {
767 	return (p->sf.version_major);
768 }
769 
770 int
771 pcap_minor_version(pcap_t *p)
772 {
773 	return (p->sf.version_minor);
774 }
775 
776 FILE *
777 pcap_file(pcap_t *p)
778 {
779 	return (p->sf.rfile);
780 }
781 
782 int
783 pcap_fileno(pcap_t *p)
784 {
785 #ifndef WIN32
786 	return (p->fd);
787 #else
788 	if (p->adapter != NULL)
789 		return ((int)(DWORD)p->adapter->hFile);
790 	else
791 		return (-1);
792 #endif
793 }
794 
795 #if !defined(WIN32) && !defined(MSDOS)
796 int
797 pcap_get_selectable_fd(pcap_t *p)
798 {
799 	return (p->selectable_fd);
800 }
801 #endif
802 
803 void
804 pcap_perror(pcap_t *p, char *prefix)
805 {
806 	fprintf(stderr, "%s: %s\n", prefix, p->errbuf);
807 }
808 
809 char *
810 pcap_geterr(pcap_t *p)
811 {
812 	return (p->errbuf);
813 }
814 
815 int
816 pcap_getnonblock(pcap_t *p, char *errbuf)
817 {
818 	return p->getnonblock_op(p, errbuf);
819 }
820 
821 /*
822  * Get the current non-blocking mode setting, under the assumption that
823  * it's just the standard POSIX non-blocking flag.
824  *
825  * We don't look at "p->nonblock", in case somebody tweaked the FD
826  * directly.
827  */
828 #if !defined(WIN32) && !defined(MSDOS)
829 int
830 pcap_getnonblock_fd(pcap_t *p, char *errbuf)
831 {
832 	int fdflags;
833 
834 	fdflags = fcntl(p->fd, F_GETFL, 0);
835 	if (fdflags == -1) {
836 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s",
837 		    pcap_strerror(errno));
838 		return (-1);
839 	}
840 	if (fdflags & O_NONBLOCK)
841 		return (1);
842 	else
843 		return (0);
844 }
845 #endif
846 
847 int
848 pcap_setnonblock(pcap_t *p, int nonblock, char *errbuf)
849 {
850 	return p->setnonblock_op(p, nonblock, errbuf);
851 }
852 
853 #if !defined(WIN32) && !defined(MSDOS)
854 /*
855  * Set non-blocking mode, under the assumption that it's just the
856  * standard POSIX non-blocking flag.  (This can be called by the
857  * per-platform non-blocking-mode routine if that routine also
858  * needs to do some additional work.)
859  */
860 int
861 pcap_setnonblock_fd(pcap_t *p, int nonblock, char *errbuf)
862 {
863 	int fdflags;
864 
865 	fdflags = fcntl(p->fd, F_GETFL, 0);
866 	if (fdflags == -1) {
867 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s",
868 		    pcap_strerror(errno));
869 		return (-1);
870 	}
871 	if (nonblock)
872 		fdflags |= O_NONBLOCK;
873 	else
874 		fdflags &= ~O_NONBLOCK;
875 	if (fcntl(p->fd, F_SETFL, fdflags) == -1) {
876 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_SETFL: %s",
877 		    pcap_strerror(errno));
878 		return (-1);
879 	}
880 	return (0);
881 }
882 #endif
883 
884 #ifdef WIN32
885 /*
886  * Generate a string for the last Win32-specific error (i.e. an error generated when
887  * calling a Win32 API).
888  * For errors occurred during standard C calls, we still use pcap_strerror()
889  */
890 char *
891 pcap_win32strerror(void)
892 {
893 	DWORD error;
894 	static char errbuf[PCAP_ERRBUF_SIZE+1];
895 	int errlen;
896 	char *p;
897 
898 	error = GetLastError();
899 	FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, 0, errbuf,
900 	    PCAP_ERRBUF_SIZE, NULL);
901 
902 	/*
903 	 * "FormatMessage()" "helpfully" sticks CR/LF at the end of the
904 	 * message.  Get rid of it.
905 	 */
906 	errlen = strlen(errbuf);
907 	if (errlen >= 2) {
908 		errbuf[errlen - 1] = '\0';
909 		errbuf[errlen - 2] = '\0';
910 	}
911 	p = strchr(errbuf, '\0');
912 	snprintf (p, sizeof(errbuf)-(p-errbuf), " (%lu)", error);
913 	return (errbuf);
914 }
915 #endif
916 
917 /*
918  * Generate error strings for PCAP_ERROR_ and PCAP_WARNING_ values.
919  */
920 const char *
921 pcap_statustostr(int errnum)
922 {
923 	static char ebuf[15+10+1];
924 
925 	switch (errnum) {
926 
927 	case PCAP_WARNING:
928 		return("Generic warning");
929 
930 	case PCAP_WARNING_PROMISC_NOTSUP:
931 		return ("That device doesn't support promiscuous mode");
932 
933 	case PCAP_ERROR:
934 		return("Generic error");
935 
936 	case PCAP_ERROR_BREAK:
937 		return("Loop terminated by pcap_breakloop");
938 
939 	case PCAP_ERROR_NOT_ACTIVATED:
940 		return("The pcap_t has not been activated");
941 
942 	case PCAP_ERROR_ACTIVATED:
943 		return ("The setting can't be changed after the pcap_t is activated");
944 
945 	case PCAP_ERROR_NO_SUCH_DEVICE:
946 		return ("No such device exists");
947 
948 	case PCAP_ERROR_RFMON_NOTSUP:
949 		return ("That device doesn't support monitor mode");
950 
951 	case PCAP_ERROR_NOT_RFMON:
952 		return ("That operation is supported only in monitor mode");
953 
954 	case PCAP_ERROR_PERM_DENIED:
955 		return ("You don't have permission to capture on that device");
956 
957 	case PCAP_ERROR_IFACE_NOT_UP:
958 		return ("That device is not up");
959 	}
960 	(void)snprintf(ebuf, sizeof ebuf, "Unknown error: %d", errnum);
961 	return(ebuf);
962 }
963 
964 /*
965  * Not all systems have strerror().
966  */
967 const char *
968 pcap_strerror(int errnum)
969 {
970 #ifdef HAVE_STRERROR
971 	return (strerror(errnum));
972 #else
973 	extern int sys_nerr;
974 	extern const char *const sys_errlist[];
975 	static char ebuf[15+10+1];
976 
977 	if ((unsigned int)errnum < sys_nerr)
978 		return ((char *)sys_errlist[errnum]);
979 	(void)snprintf(ebuf, sizeof ebuf, "Unknown error: %d", errnum);
980 	return(ebuf);
981 #endif
982 }
983 
984 int
985 pcap_setfilter(pcap_t *p, struct bpf_program *fp)
986 {
987 	return p->setfilter_op(p, fp);
988 }
989 
990 /*
991  * Set direction flag, which controls whether we accept only incoming
992  * packets, only outgoing packets, or both.
993  * Note that, depending on the platform, some or all direction arguments
994  * might not be supported.
995  */
996 int
997 pcap_setdirection(pcap_t *p, pcap_direction_t d)
998 {
999 	if (p->setdirection_op == NULL) {
1000 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1001 		    "Setting direction is not implemented on this platform");
1002 		return -1;
1003 	} else
1004 		return p->setdirection_op(p, d);
1005 }
1006 
1007 int
1008 pcap_stats(pcap_t *p, struct pcap_stat *ps)
1009 {
1010 	return p->stats_op(p, ps);
1011 }
1012 
1013 static int
1014 pcap_stats_dead(pcap_t *p, struct pcap_stat *ps _U_)
1015 {
1016 	snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1017 	    "Statistics aren't available from a pcap_open_dead pcap_t");
1018 	return (-1);
1019 }
1020 
1021 #ifdef WIN32
1022 int
1023 pcap_setbuff(pcap_t *p, int dim)
1024 {
1025 	return p->setbuff_op(p, dim);
1026 }
1027 
1028 static int
1029 pcap_setbuff_dead(pcap_t *p, int dim)
1030 {
1031 	snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1032 	    "The kernel buffer size cannot be set on a pcap_open_dead pcap_t");
1033 	return (-1);
1034 }
1035 
1036 int
1037 pcap_setmode(pcap_t *p, int mode)
1038 {
1039 	return p->setmode_op(p, mode);
1040 }
1041 
1042 static int
1043 pcap_setmode_dead(pcap_t *p, int mode)
1044 {
1045 	snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1046 	    "impossible to set mode on a pcap_open_dead pcap_t");
1047 	return (-1);
1048 }
1049 
1050 int
1051 pcap_setmintocopy(pcap_t *p, int size)
1052 {
1053 	return p->setmintocopy_op(p, size);
1054 }
1055 
1056 static int
1057 pcap_setmintocopy_dead(pcap_t *p, int size)
1058 {
1059 	snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1060 	    "The mintocopy parameter cannot be set on a pcap_open_dead pcap_t");
1061 	return (-1);
1062 }
1063 #endif
1064 
1065 /*
1066  * On some platforms, we need to clean up promiscuous or monitor mode
1067  * when we close a device - and we want that to happen even if the
1068  * application just exits without explicitl closing devices.
1069  * On those platforms, we need to register a "close all the pcaps"
1070  * routine to be called when we exit, and need to maintain a list of
1071  * pcaps that need to be closed to clean up modes.
1072  *
1073  * XXX - not thread-safe.
1074  */
1075 
1076 /*
1077  * List of pcaps on which we've done something that needs to be
1078  * cleaned up.
1079  * If there are any such pcaps, we arrange to call "pcap_close_all()"
1080  * when we exit, and have it close all of them.
1081  */
1082 static struct pcap *pcaps_to_close;
1083 
1084 /*
1085  * TRUE if we've already called "atexit()" to cause "pcap_close_all()" to
1086  * be called on exit.
1087  */
1088 static int did_atexit;
1089 
1090 static void
1091 pcap_close_all(void)
1092 {
1093 	struct pcap *handle;
1094 
1095 	while ((handle = pcaps_to_close) != NULL)
1096 		pcap_close(handle);
1097 }
1098 
1099 int
1100 pcap_do_addexit(pcap_t *p)
1101 {
1102 	/*
1103 	 * If we haven't already done so, arrange to have
1104 	 * "pcap_close_all()" called when we exit.
1105 	 */
1106 	if (!did_atexit) {
1107 		if (atexit(pcap_close_all) == -1) {
1108 			/*
1109 			 * "atexit()" failed; let our caller know.
1110 			 */
1111 			strncpy(p->errbuf, "atexit failed",
1112 			    PCAP_ERRBUF_SIZE);
1113 			return (0);
1114 		}
1115 		did_atexit = 1;
1116 	}
1117 	return (1);
1118 }
1119 
1120 void
1121 pcap_add_to_pcaps_to_close(pcap_t *p)
1122 {
1123 	p->md.next = pcaps_to_close;
1124 	pcaps_to_close = p;
1125 }
1126 
1127 void
1128 pcap_remove_from_pcaps_to_close(pcap_t *p)
1129 {
1130 	pcap_t *pc, *prevpc;
1131 
1132 	for (pc = pcaps_to_close, prevpc = NULL; pc != NULL;
1133 	    prevpc = pc, pc = pc->md.next) {
1134 		if (pc == p) {
1135 			/*
1136 			 * Found it.  Remove it from the list.
1137 			 */
1138 			if (prevpc == NULL) {
1139 				/*
1140 				 * It was at the head of the list.
1141 				 */
1142 				pcaps_to_close = pc->md.next;
1143 			} else {
1144 				/*
1145 				 * It was in the middle of the list.
1146 				 */
1147 				prevpc->md.next = pc->md.next;
1148 			}
1149 			break;
1150 		}
1151 	}
1152 }
1153 
1154 void
1155 pcap_cleanup_live_common(pcap_t *p)
1156 {
1157 	if (p->buffer != NULL) {
1158 		free(p->buffer);
1159 		p->buffer = NULL;
1160 	}
1161 	if (p->dlt_list != NULL) {
1162 		free(p->dlt_list);
1163 		p->dlt_list = NULL;
1164 		p->dlt_count = 0;
1165 	}
1166 	pcap_freecode(&p->fcode);
1167 #if !defined(WIN32) && !defined(MSDOS)
1168 	if (p->fd >= 0) {
1169 		close(p->fd);
1170 		p->fd = -1;
1171 	}
1172 #endif
1173 }
1174 
1175 static void
1176 pcap_cleanup_dead(pcap_t *p _U_)
1177 {
1178 	/* Nothing to do. */
1179 }
1180 
1181 pcap_t *
1182 pcap_open_dead(int linktype, int snaplen)
1183 {
1184 	pcap_t *p;
1185 
1186 	p = malloc(sizeof(*p));
1187 	if (p == NULL)
1188 		return NULL;
1189 	memset (p, 0, sizeof(*p));
1190 	p->snapshot = snaplen;
1191 	p->linktype = linktype;
1192 	p->stats_op = pcap_stats_dead;
1193 #ifdef WIN32
1194 	p->setbuff_op = pcap_setbuff_dead;
1195 	p->setmode_op = pcap_setmode_dead;
1196 	p->setmintocopy_op = pcap_setmintocopy_dead;
1197 #endif
1198 	p->cleanup_op = pcap_cleanup_dead;
1199 	p->activated = 1;
1200 	return p;
1201 }
1202 
1203 /*
1204  * API compatible with WinPcap's "send a packet" routine - returns -1
1205  * on error, 0 otherwise.
1206  *
1207  * XXX - what if we get a short write?
1208  */
1209 int
1210 pcap_sendpacket(pcap_t *p, const u_char *buf, int size)
1211 {
1212 	if (p->inject_op(p, buf, size) == -1)
1213 		return (-1);
1214 	return (0);
1215 }
1216 
1217 /*
1218  * API compatible with OpenBSD's "send a packet" routine - returns -1 on
1219  * error, number of bytes written otherwise.
1220  */
1221 int
1222 pcap_inject(pcap_t *p, const void *buf, size_t size)
1223 {
1224 	return (p->inject_op(p, buf, size));
1225 }
1226 
1227 void
1228 pcap_close(pcap_t *p)
1229 {
1230 	if (p->opt.source != NULL)
1231 		free(p->opt.source);
1232 	p->cleanup_op(p);
1233 	free(p);
1234 }
1235 
1236 /*
1237  * Given a BPF program, a pcap_pkthdr structure for a packet, and the raw
1238  * data for the packet, check whether the packet passes the filter.
1239  * Returns the return value of the filter program, which will be zero if
1240  * the packet doesn't pass and non-zero if the packet does pass.
1241  */
1242 int
1243 pcap_offline_filter(struct bpf_program *fp, const struct pcap_pkthdr *h,
1244     const u_char *pkt)
1245 {
1246 	struct bpf_insn *fcode = fp->bf_insns;
1247 
1248 	if (fcode != NULL)
1249 		return (bpf_filter(fcode, pkt, h->len, h->caplen));
1250 	else
1251 		return (0);
1252 }
1253 
1254 /*
1255  * We make the version string static, and return a pointer to it, rather
1256  * than exporting the version string directly.  On at least some UNIXes,
1257  * if you import data from a shared library into an program, the data is
1258  * bound into the program binary, so if the string in the version of the
1259  * library with which the program was linked isn't the same as the
1260  * string in the version of the library with which the program is being
1261  * run, various undesirable things may happen (warnings, the string
1262  * being the one from the version of the library with which the program
1263  * was linked, or even weirder things, such as the string being the one
1264  * from the library but being truncated).
1265  */
1266 #ifdef HAVE_VERSION_H
1267 #include "version.h"
1268 #else
1269 static const char pcap_version_string[] = "libpcap version 0.9[.x]";
1270 #endif
1271 
1272 #ifdef WIN32
1273 /*
1274  * XXX - it'd be nice if we could somehow generate the WinPcap and libpcap
1275  * version numbers when building WinPcap.  (It'd be nice to do so for
1276  * the packet.dll version number as well.)
1277  */
1278 static const char wpcap_version_string[] = "4.0";
1279 static const char pcap_version_string_fmt[] =
1280     "WinPcap version %s, based on %s";
1281 static const char pcap_version_string_packet_dll_fmt[] =
1282     "WinPcap version %s (packet.dll version %s), based on %s";
1283 static char *full_pcap_version_string;
1284 
1285 const char *
1286 pcap_lib_version(void)
1287 {
1288 	char *packet_version_string;
1289 	size_t full_pcap_version_string_len;
1290 
1291 	if (full_pcap_version_string == NULL) {
1292 		/*
1293 		 * Generate the version string.
1294 		 */
1295 		packet_version_string = PacketGetVersion();
1296 		if (strcmp(wpcap_version_string, packet_version_string) == 0) {
1297 			/*
1298 			 * WinPcap version string and packet.dll version
1299 			 * string are the same; just report the WinPcap
1300 			 * version.
1301 			 */
1302 			full_pcap_version_string_len =
1303 			    (sizeof pcap_version_string_fmt - 4) +
1304 			    strlen(wpcap_version_string) +
1305 			    strlen(pcap_version_string);
1306 			full_pcap_version_string =
1307 			    malloc(full_pcap_version_string_len);
1308 			sprintf(full_pcap_version_string,
1309 			    pcap_version_string_fmt, wpcap_version_string,
1310 			    pcap_version_string);
1311 		} else {
1312 			/*
1313 			 * WinPcap version string and packet.dll version
1314 			 * string are different; that shouldn't be the
1315 			 * case (the two libraries should come from the
1316 			 * same version of WinPcap), so we report both
1317 			 * versions.
1318 			 */
1319 			full_pcap_version_string_len =
1320 			    (sizeof pcap_version_string_packet_dll_fmt - 6) +
1321 			    strlen(wpcap_version_string) +
1322 			    strlen(packet_version_string) +
1323 			    strlen(pcap_version_string);
1324 			full_pcap_version_string = malloc(full_pcap_version_string_len);
1325 
1326 			sprintf(full_pcap_version_string,
1327 			    pcap_version_string_packet_dll_fmt,
1328 			    wpcap_version_string, packet_version_string,
1329 			    pcap_version_string);
1330 		}
1331 	}
1332 	return (full_pcap_version_string);
1333 }
1334 
1335 #elif defined(MSDOS)
1336 
1337 static char *full_pcap_version_string;
1338 
1339 const char *
1340 pcap_lib_version (void)
1341 {
1342 	char *packet_version_string;
1343 	size_t full_pcap_version_string_len;
1344 	static char dospfx[] = "DOS-";
1345 
1346 	if (full_pcap_version_string == NULL) {
1347 		/*
1348 		 * Generate the version string.
1349 		 */
1350 		full_pcap_version_string_len =
1351 		    sizeof dospfx + strlen(pcap_version_string);
1352 		full_pcap_version_string =
1353 		    malloc(full_pcap_version_string_len);
1354 		strcpy(full_pcap_version_string, dospfx);
1355 		strcat(full_pcap_version_string, pcap_version_string);
1356 	}
1357 	return (full_pcap_version_string);
1358 }
1359 
1360 #else /* UN*X */
1361 
1362 const char *
1363 pcap_lib_version(void)
1364 {
1365 	return (pcap_version_string);
1366 }
1367 #endif
1368