xref: /freebsd/contrib/libpcap/pcap.c (revision 33644623554bb0fc57ed3c7d874193a498679b22)
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.88.2.19 2007/09/19 02:50:52 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_dispatch(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
76 {
77 
78 	return p->read_op(p, cnt, callback, user);
79 }
80 
81 /*
82  * XXX - is this necessary?
83  */
84 int
85 pcap_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
86 {
87 
88 	return p->read_op(p, cnt, callback, user);
89 }
90 
91 int
92 pcap_loop(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
93 {
94 	register int n;
95 
96 	for (;;) {
97 		if (p->sf.rfile != NULL) {
98 			/*
99 			 * 0 means EOF, so don't loop if we get 0.
100 			 */
101 			n = pcap_offline_read(p, cnt, callback, user);
102 		} else {
103 			/*
104 			 * XXX keep reading until we get something
105 			 * (or an error occurs)
106 			 */
107 			do {
108 				n = p->read_op(p, cnt, callback, user);
109 			} while (n == 0);
110 		}
111 		if (n <= 0)
112 			return (n);
113 		if (cnt > 0) {
114 			cnt -= n;
115 			if (cnt <= 0)
116 				return (0);
117 		}
118 	}
119 }
120 
121 struct singleton {
122 	struct pcap_pkthdr *hdr;
123 	const u_char *pkt;
124 };
125 
126 
127 static void
128 pcap_oneshot(u_char *userData, const struct pcap_pkthdr *h, const u_char *pkt)
129 {
130 	struct singleton *sp = (struct singleton *)userData;
131 	*sp->hdr = *h;
132 	sp->pkt = pkt;
133 }
134 
135 const u_char *
136 pcap_next(pcap_t *p, struct pcap_pkthdr *h)
137 {
138 	struct singleton s;
139 
140 	s.hdr = h;
141 	if (pcap_dispatch(p, 1, pcap_oneshot, (u_char*)&s) <= 0)
142 		return (0);
143 	return (s.pkt);
144 }
145 
146 struct pkt_for_fakecallback {
147 	struct pcap_pkthdr *hdr;
148 	const u_char **pkt;
149 };
150 
151 static void
152 pcap_fakecallback(u_char *userData, const struct pcap_pkthdr *h,
153     const u_char *pkt)
154 {
155 	struct pkt_for_fakecallback *sp = (struct pkt_for_fakecallback *)userData;
156 
157 	*sp->hdr = *h;
158 	*sp->pkt = pkt;
159 }
160 
161 int
162 pcap_next_ex(pcap_t *p, struct pcap_pkthdr **pkt_header,
163     const u_char **pkt_data)
164 {
165 	struct pkt_for_fakecallback s;
166 
167 	s.hdr = &p->pcap_header;
168 	s.pkt = pkt_data;
169 
170 	/* Saves a pointer to the packet headers */
171 	*pkt_header= &p->pcap_header;
172 
173 	if (p->sf.rfile != NULL) {
174 		int status;
175 
176 		/* We are on an offline capture */
177 		status = pcap_offline_read(p, 1, pcap_fakecallback,
178 		    (u_char *)&s);
179 
180 		/*
181 		 * Return codes for pcap_offline_read() are:
182 		 *   -  0: EOF
183 		 *   - -1: error
184 		 *   - >1: OK
185 		 * The first one ('0') conflicts with the return code of
186 		 * 0 from pcap_read() meaning "no packets arrived before
187 		 * the timeout expired", so we map it to -2 so you can
188 		 * distinguish between an EOF from a savefile and a
189 		 * "no packets arrived before the timeout expired, try
190 		 * again" from a live capture.
191 		 */
192 		if (status == 0)
193 			return (-2);
194 		else
195 			return (status);
196 	}
197 
198 	/*
199 	 * Return codes for pcap_read() are:
200 	 *   -  0: timeout
201 	 *   - -1: error
202 	 *   - -2: loop was broken out of with pcap_breakloop()
203 	 *   - >1: OK
204 	 * The first one ('0') conflicts with the return code of 0 from
205 	 * pcap_offline_read() meaning "end of file".
206 	*/
207 	return (p->read_op(p, 1, pcap_fakecallback, (u_char *)&s));
208 }
209 
210 /*
211  * Force the loop in "pcap_read()" or "pcap_read_offline()" to terminate.
212  */
213 void
214 pcap_breakloop(pcap_t *p)
215 {
216 	p->break_loop = 1;
217 }
218 
219 int
220 pcap_datalink(pcap_t *p)
221 {
222 	return (p->linktype);
223 }
224 
225 int
226 pcap_list_datalinks(pcap_t *p, int **dlt_buffer)
227 {
228 	if (p->dlt_count == 0) {
229 		/*
230 		 * We couldn't fetch the list of DLTs, which means
231 		 * this platform doesn't support changing the
232 		 * DLT for an interface.  Return a list of DLTs
233 		 * containing only the DLT this device supports.
234 		 */
235 		*dlt_buffer = (int*)malloc(sizeof(**dlt_buffer));
236 		if (*dlt_buffer == NULL) {
237 			(void)snprintf(p->errbuf, sizeof(p->errbuf),
238 			    "malloc: %s", pcap_strerror(errno));
239 			return (-1);
240 		}
241 		**dlt_buffer = p->linktype;
242 		return (1);
243 	} else {
244 		*dlt_buffer = (int*)calloc(sizeof(**dlt_buffer), p->dlt_count);
245 		if (*dlt_buffer == NULL) {
246 			(void)snprintf(p->errbuf, sizeof(p->errbuf),
247 			    "malloc: %s", pcap_strerror(errno));
248 			return (-1);
249 		}
250 		(void)memcpy(*dlt_buffer, p->dlt_list,
251 		    sizeof(**dlt_buffer) * p->dlt_count);
252 		return (p->dlt_count);
253 	}
254 }
255 
256 int
257 pcap_set_datalink(pcap_t *p, int dlt)
258 {
259 	int i;
260 	const char *dlt_name;
261 
262 	if (p->dlt_count == 0 || p->set_datalink_op == NULL) {
263 		/*
264 		 * We couldn't fetch the list of DLTs, or we don't
265 		 * have a "set datalink" operation, which means
266 		 * this platform doesn't support changing the
267 		 * DLT for an interface.  Check whether the new
268 		 * DLT is the one this interface supports.
269 		 */
270 		if (p->linktype != dlt)
271 			goto unsupported;
272 
273 		/*
274 		 * It is, so there's nothing we need to do here.
275 		 */
276 		return (0);
277 	}
278 	for (i = 0; i < p->dlt_count; i++)
279 		if (p->dlt_list[i] == dlt)
280 			break;
281 	if (i >= p->dlt_count)
282 		goto unsupported;
283 	if (p->dlt_count == 2 && p->dlt_list[0] == DLT_EN10MB &&
284 	    dlt == DLT_DOCSIS) {
285 		/*
286 		 * This is presumably an Ethernet device, as the first
287 		 * link-layer type it offers is DLT_EN10MB, and the only
288 		 * other type it offers is DLT_DOCSIS.  That means that
289 		 * we can't tell the driver to supply DOCSIS link-layer
290 		 * headers - we're just pretending that's what we're
291 		 * getting, as, presumably, we're capturing on a dedicated
292 		 * link to a Cisco Cable Modem Termination System, and
293 		 * it's putting raw DOCSIS frames on the wire inside low-level
294 		 * Ethernet framing.
295 		 */
296 		p->linktype = dlt;
297 		return (0);
298 	}
299 	if (p->set_datalink_op(p, dlt) == -1)
300 		return (-1);
301 	p->linktype = dlt;
302 	return (0);
303 
304 unsupported:
305 	dlt_name = pcap_datalink_val_to_name(dlt);
306 	if (dlt_name != NULL) {
307 		(void) snprintf(p->errbuf, sizeof(p->errbuf),
308 		    "%s is not one of the DLTs supported by this device",
309 		    dlt_name);
310 	} else {
311 		(void) snprintf(p->errbuf, sizeof(p->errbuf),
312 		    "DLT %d is not one of the DLTs supported by this device",
313 		    dlt);
314 	}
315 	return (-1);
316 }
317 
318 struct dlt_choice {
319 	const char *name;
320 	const char *description;
321 	int	dlt;
322 };
323 
324 #define DLT_CHOICE(code, description) { #code, description, code }
325 #define DLT_CHOICE_SENTINEL { NULL, NULL, 0 }
326 
327 static struct dlt_choice dlt_choices[] = {
328 	DLT_CHOICE(DLT_NULL, "BSD loopback"),
329 	DLT_CHOICE(DLT_EN10MB, "Ethernet"),
330 	DLT_CHOICE(DLT_IEEE802, "Token ring"),
331 	DLT_CHOICE(DLT_ARCNET, "ARCNET"),
332 	DLT_CHOICE(DLT_SLIP, "SLIP"),
333 	DLT_CHOICE(DLT_PPP, "PPP"),
334 	DLT_CHOICE(DLT_FDDI, "FDDI"),
335 	DLT_CHOICE(DLT_ATM_RFC1483, "RFC 1483 LLC-encapsulated ATM"),
336 	DLT_CHOICE(DLT_RAW, "Raw IP"),
337 	DLT_CHOICE(DLT_SLIP_BSDOS, "BSD/OS SLIP"),
338 	DLT_CHOICE(DLT_PPP_BSDOS, "BSD/OS PPP"),
339 	DLT_CHOICE(DLT_ATM_CLIP, "Linux Classical IP-over-ATM"),
340 	DLT_CHOICE(DLT_PPP_SERIAL, "PPP over serial"),
341 	DLT_CHOICE(DLT_PPP_ETHER, "PPPoE"),
342 	DLT_CHOICE(DLT_C_HDLC, "Cisco HDLC"),
343 	DLT_CHOICE(DLT_IEEE802_11, "802.11"),
344 	DLT_CHOICE(DLT_FRELAY, "Frame Relay"),
345 	DLT_CHOICE(DLT_LOOP, "OpenBSD loopback"),
346 	DLT_CHOICE(DLT_ENC, "OpenBSD encapsulated IP"),
347 	DLT_CHOICE(DLT_LINUX_SLL, "Linux cooked"),
348 	DLT_CHOICE(DLT_LTALK, "Localtalk"),
349 	DLT_CHOICE(DLT_PFLOG, "OpenBSD pflog file"),
350 	DLT_CHOICE(DLT_PRISM_HEADER, "802.11 plus Prism header"),
351 	DLT_CHOICE(DLT_IP_OVER_FC, "RFC 2625 IP-over-Fibre Channel"),
352 	DLT_CHOICE(DLT_SUNATM, "Sun raw ATM"),
353 	DLT_CHOICE(DLT_IEEE802_11_RADIO, "802.11 plus BSD radio information header"),
354 	DLT_CHOICE(DLT_APPLE_IP_OVER_IEEE1394, "Apple IP-over-IEEE 1394"),
355 	DLT_CHOICE(DLT_ARCNET_LINUX, "Linux ARCNET"),
356 	DLT_CHOICE(DLT_DOCSIS, "DOCSIS"),
357 	DLT_CHOICE(DLT_LINUX_IRDA, "Linux IrDA"),
358 	DLT_CHOICE(DLT_LINUX_LAPD, "Linux vISDN LAPD"),
359 	DLT_CHOICE(DLT_IEEE802_11_RADIO_AVS, "802.11 plus AVS radio information header"),
360         DLT_CHOICE(DLT_SYMANTEC_FIREWALL, "Symantec Firewall"),
361         DLT_CHOICE(DLT_JUNIPER_ATM1, "Juniper ATM1 PIC"),
362         DLT_CHOICE(DLT_JUNIPER_ATM2, "Juniper ATM2 PIC"),
363         DLT_CHOICE(DLT_JUNIPER_MLPPP, "Juniper Multi-Link PPP"),
364 	DLT_CHOICE(DLT_PPP_PPPD, "PPP for pppd, with direction flag"),
365 	DLT_CHOICE(DLT_JUNIPER_PPPOE, "Juniper PPPoE"),
366 	DLT_CHOICE(DLT_JUNIPER_PPPOE_ATM, "Juniper PPPoE/ATM"),
367 	DLT_CHOICE(DLT_GPRS_LLC, "GPRS LLC"),
368 	DLT_CHOICE(DLT_GPF_T, "GPF-T"),
369 	DLT_CHOICE(DLT_GPF_F, "GPF-F"),
370 	DLT_CHOICE(DLT_JUNIPER_PIC_PEER, "Juniper PIC Peer"),
371 	DLT_CHOICE(DLT_JUNIPER_MLFR, "Juniper Multi-Link Frame Relay"),
372 	DLT_CHOICE(DLT_ERF_ETH,	"Ethernet with Endace ERF header"),
373 	DLT_CHOICE(DLT_ERF_POS, "Packet-over-SONET with Endace ERF header"),
374         DLT_CHOICE(DLT_JUNIPER_GGSN, "Juniper GGSN PIC"),
375         DLT_CHOICE(DLT_JUNIPER_ES, "Juniper Encryption Services PIC"),
376         DLT_CHOICE(DLT_JUNIPER_MONITOR, "Juniper Passive Monitor PIC"),
377         DLT_CHOICE(DLT_JUNIPER_SERVICES, "Juniper Advanced Services PIC"),
378 	DLT_CHOICE(DLT_JUNIPER_MFR, "Juniper FRF.16 Frame Relay"),
379 	DLT_CHOICE(DLT_JUNIPER_ETHER, "Juniper Ethernet"),
380 	DLT_CHOICE(DLT_JUNIPER_PPP, "Juniper PPP"),
381 	DLT_CHOICE(DLT_JUNIPER_FRELAY, "Juniper Frame Relay"),
382 	DLT_CHOICE(DLT_JUNIPER_CHDLC, "Juniper C-HDLC"),
383 	DLT_CHOICE(DLT_MFR, "FRF.16 Frame Relay"),
384 	DLT_CHOICE(DLT_JUNIPER_VP, "Juniper Voice PIC"),
385 	DLT_CHOICE(DLT_MTP2, "SS7 MTP2"),
386 	DLT_CHOICE(DLT_A429, "Arinc 429"),
387 	DLT_CHOICE(DLT_A653_ICM, "Arinc 653 Interpartition Communication"),
388 	DLT_CHOICE(DLT_USB, "USB"),
389 	DLT_CHOICE(DLT_BLUETOOTH_HCI_H4, "Bluetooth HCI UART transport layer"),
390 	DLT_CHOICE(DLT_CAN20B, "Controller Area Network (CAN) v. 2.0B"),
391 	DLT_CHOICE(DLT_MTP2_WITH_PHDR, "SS7 MTP2 with Pseudo-header"),
392 	DLT_CHOICE_SENTINEL
393 };
394 
395 /*
396  * This array is designed for mapping upper and lower case letter
397  * together for a case independent comparison.  The mappings are
398  * based upon ascii character sequences.
399  */
400 static const u_char charmap[] = {
401 	(u_char)'\000', (u_char)'\001', (u_char)'\002', (u_char)'\003',
402 	(u_char)'\004', (u_char)'\005', (u_char)'\006', (u_char)'\007',
403 	(u_char)'\010', (u_char)'\011', (u_char)'\012', (u_char)'\013',
404 	(u_char)'\014', (u_char)'\015', (u_char)'\016', (u_char)'\017',
405 	(u_char)'\020', (u_char)'\021', (u_char)'\022', (u_char)'\023',
406 	(u_char)'\024', (u_char)'\025', (u_char)'\026', (u_char)'\027',
407 	(u_char)'\030', (u_char)'\031', (u_char)'\032', (u_char)'\033',
408 	(u_char)'\034', (u_char)'\035', (u_char)'\036', (u_char)'\037',
409 	(u_char)'\040', (u_char)'\041', (u_char)'\042', (u_char)'\043',
410 	(u_char)'\044', (u_char)'\045', (u_char)'\046', (u_char)'\047',
411 	(u_char)'\050', (u_char)'\051', (u_char)'\052', (u_char)'\053',
412 	(u_char)'\054', (u_char)'\055', (u_char)'\056', (u_char)'\057',
413 	(u_char)'\060', (u_char)'\061', (u_char)'\062', (u_char)'\063',
414 	(u_char)'\064', (u_char)'\065', (u_char)'\066', (u_char)'\067',
415 	(u_char)'\070', (u_char)'\071', (u_char)'\072', (u_char)'\073',
416 	(u_char)'\074', (u_char)'\075', (u_char)'\076', (u_char)'\077',
417 	(u_char)'\100', (u_char)'\141', (u_char)'\142', (u_char)'\143',
418 	(u_char)'\144', (u_char)'\145', (u_char)'\146', (u_char)'\147',
419 	(u_char)'\150', (u_char)'\151', (u_char)'\152', (u_char)'\153',
420 	(u_char)'\154', (u_char)'\155', (u_char)'\156', (u_char)'\157',
421 	(u_char)'\160', (u_char)'\161', (u_char)'\162', (u_char)'\163',
422 	(u_char)'\164', (u_char)'\165', (u_char)'\166', (u_char)'\167',
423 	(u_char)'\170', (u_char)'\171', (u_char)'\172', (u_char)'\133',
424 	(u_char)'\134', (u_char)'\135', (u_char)'\136', (u_char)'\137',
425 	(u_char)'\140', (u_char)'\141', (u_char)'\142', (u_char)'\143',
426 	(u_char)'\144', (u_char)'\145', (u_char)'\146', (u_char)'\147',
427 	(u_char)'\150', (u_char)'\151', (u_char)'\152', (u_char)'\153',
428 	(u_char)'\154', (u_char)'\155', (u_char)'\156', (u_char)'\157',
429 	(u_char)'\160', (u_char)'\161', (u_char)'\162', (u_char)'\163',
430 	(u_char)'\164', (u_char)'\165', (u_char)'\166', (u_char)'\167',
431 	(u_char)'\170', (u_char)'\171', (u_char)'\172', (u_char)'\173',
432 	(u_char)'\174', (u_char)'\175', (u_char)'\176', (u_char)'\177',
433 	(u_char)'\200', (u_char)'\201', (u_char)'\202', (u_char)'\203',
434 	(u_char)'\204', (u_char)'\205', (u_char)'\206', (u_char)'\207',
435 	(u_char)'\210', (u_char)'\211', (u_char)'\212', (u_char)'\213',
436 	(u_char)'\214', (u_char)'\215', (u_char)'\216', (u_char)'\217',
437 	(u_char)'\220', (u_char)'\221', (u_char)'\222', (u_char)'\223',
438 	(u_char)'\224', (u_char)'\225', (u_char)'\226', (u_char)'\227',
439 	(u_char)'\230', (u_char)'\231', (u_char)'\232', (u_char)'\233',
440 	(u_char)'\234', (u_char)'\235', (u_char)'\236', (u_char)'\237',
441 	(u_char)'\240', (u_char)'\241', (u_char)'\242', (u_char)'\243',
442 	(u_char)'\244', (u_char)'\245', (u_char)'\246', (u_char)'\247',
443 	(u_char)'\250', (u_char)'\251', (u_char)'\252', (u_char)'\253',
444 	(u_char)'\254', (u_char)'\255', (u_char)'\256', (u_char)'\257',
445 	(u_char)'\260', (u_char)'\261', (u_char)'\262', (u_char)'\263',
446 	(u_char)'\264', (u_char)'\265', (u_char)'\266', (u_char)'\267',
447 	(u_char)'\270', (u_char)'\271', (u_char)'\272', (u_char)'\273',
448 	(u_char)'\274', (u_char)'\275', (u_char)'\276', (u_char)'\277',
449 	(u_char)'\300', (u_char)'\341', (u_char)'\342', (u_char)'\343',
450 	(u_char)'\344', (u_char)'\345', (u_char)'\346', (u_char)'\347',
451 	(u_char)'\350', (u_char)'\351', (u_char)'\352', (u_char)'\353',
452 	(u_char)'\354', (u_char)'\355', (u_char)'\356', (u_char)'\357',
453 	(u_char)'\360', (u_char)'\361', (u_char)'\362', (u_char)'\363',
454 	(u_char)'\364', (u_char)'\365', (u_char)'\366', (u_char)'\367',
455 	(u_char)'\370', (u_char)'\371', (u_char)'\372', (u_char)'\333',
456 	(u_char)'\334', (u_char)'\335', (u_char)'\336', (u_char)'\337',
457 	(u_char)'\340', (u_char)'\341', (u_char)'\342', (u_char)'\343',
458 	(u_char)'\344', (u_char)'\345', (u_char)'\346', (u_char)'\347',
459 	(u_char)'\350', (u_char)'\351', (u_char)'\352', (u_char)'\353',
460 	(u_char)'\354', (u_char)'\355', (u_char)'\356', (u_char)'\357',
461 	(u_char)'\360', (u_char)'\361', (u_char)'\362', (u_char)'\363',
462 	(u_char)'\364', (u_char)'\365', (u_char)'\366', (u_char)'\367',
463 	(u_char)'\370', (u_char)'\371', (u_char)'\372', (u_char)'\373',
464 	(u_char)'\374', (u_char)'\375', (u_char)'\376', (u_char)'\377',
465 };
466 
467 int
468 pcap_strcasecmp(const char *s1, const char *s2)
469 {
470 	register const u_char	*cm = charmap,
471 				*us1 = (const u_char *)s1,
472 				*us2 = (const u_char *)s2;
473 
474 	while (cm[*us1] == cm[*us2++])
475 		if (*us1++ == '\0')
476 			return(0);
477 	return (cm[*us1] - cm[*--us2]);
478 }
479 
480 int
481 pcap_datalink_name_to_val(const char *name)
482 {
483 	int i;
484 
485 	for (i = 0; dlt_choices[i].name != NULL; i++) {
486 		if (pcap_strcasecmp(dlt_choices[i].name + sizeof("DLT_") - 1,
487 		    name) == 0)
488 			return (dlt_choices[i].dlt);
489 	}
490 	return (-1);
491 }
492 
493 const char *
494 pcap_datalink_val_to_name(int dlt)
495 {
496 	int i;
497 
498 	for (i = 0; dlt_choices[i].name != NULL; i++) {
499 		if (dlt_choices[i].dlt == dlt)
500 			return (dlt_choices[i].name + sizeof("DLT_") - 1);
501 	}
502 	return (NULL);
503 }
504 
505 const char *
506 pcap_datalink_val_to_description(int dlt)
507 {
508 	int i;
509 
510 	for (i = 0; dlt_choices[i].name != NULL; i++) {
511 		if (dlt_choices[i].dlt == dlt)
512 			return (dlt_choices[i].description);
513 	}
514 	return (NULL);
515 }
516 
517 int
518 pcap_snapshot(pcap_t *p)
519 {
520 	return (p->snapshot);
521 }
522 
523 int
524 pcap_is_swapped(pcap_t *p)
525 {
526 	return (p->sf.swapped);
527 }
528 
529 int
530 pcap_major_version(pcap_t *p)
531 {
532 	return (p->sf.version_major);
533 }
534 
535 int
536 pcap_minor_version(pcap_t *p)
537 {
538 	return (p->sf.version_minor);
539 }
540 
541 FILE *
542 pcap_file(pcap_t *p)
543 {
544 	return (p->sf.rfile);
545 }
546 
547 int
548 pcap_fileno(pcap_t *p)
549 {
550 #ifndef WIN32
551 	return (p->fd);
552 #else
553 	if (p->adapter != NULL)
554 		return ((int)(DWORD)p->adapter->hFile);
555 	else
556 		return (-1);
557 #endif
558 }
559 
560 #if !defined(WIN32) && !defined(MSDOS)
561 int
562 pcap_get_selectable_fd(pcap_t *p)
563 {
564 	return (p->selectable_fd);
565 }
566 #endif
567 
568 void
569 pcap_perror(pcap_t *p, char *prefix)
570 {
571 	fprintf(stderr, "%s: %s\n", prefix, p->errbuf);
572 }
573 
574 char *
575 pcap_geterr(pcap_t *p)
576 {
577 	return (p->errbuf);
578 }
579 
580 int
581 pcap_getnonblock(pcap_t *p, char *errbuf)
582 {
583 	return p->getnonblock_op(p, errbuf);
584 }
585 
586 /*
587  * Get the current non-blocking mode setting, under the assumption that
588  * it's just the standard POSIX non-blocking flag.
589  *
590  * We don't look at "p->nonblock", in case somebody tweaked the FD
591  * directly.
592  */
593 #if !defined(WIN32) && !defined(MSDOS)
594 int
595 pcap_getnonblock_fd(pcap_t *p, char *errbuf)
596 {
597 	int fdflags;
598 
599 	fdflags = fcntl(p->fd, F_GETFL, 0);
600 	if (fdflags == -1) {
601 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s",
602 		    pcap_strerror(errno));
603 		return (-1);
604 	}
605 	if (fdflags & O_NONBLOCK)
606 		return (1);
607 	else
608 		return (0);
609 }
610 #endif
611 
612 int
613 pcap_setnonblock(pcap_t *p, int nonblock, char *errbuf)
614 {
615 	return p->setnonblock_op(p, nonblock, errbuf);
616 }
617 
618 #if !defined(WIN32) && !defined(MSDOS)
619 /*
620  * Set non-blocking mode, under the assumption that it's just the
621  * standard POSIX non-blocking flag.  (This can be called by the
622  * per-platform non-blocking-mode routine if that routine also
623  * needs to do some additional work.)
624  */
625 int
626 pcap_setnonblock_fd(pcap_t *p, int nonblock, char *errbuf)
627 {
628 	int fdflags;
629 
630 	fdflags = fcntl(p->fd, F_GETFL, 0);
631 	if (fdflags == -1) {
632 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s",
633 		    pcap_strerror(errno));
634 		return (-1);
635 	}
636 	if (nonblock)
637 		fdflags |= O_NONBLOCK;
638 	else
639 		fdflags &= ~O_NONBLOCK;
640 	if (fcntl(p->fd, F_SETFL, fdflags) == -1) {
641 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_SETFL: %s",
642 		    pcap_strerror(errno));
643 		return (-1);
644 	}
645 	return (0);
646 }
647 #endif
648 
649 #ifdef WIN32
650 /*
651  * Generate a string for the last Win32-specific error (i.e. an error generated when
652  * calling a Win32 API).
653  * For errors occurred during standard C calls, we still use pcap_strerror()
654  */
655 char *
656 pcap_win32strerror(void)
657 {
658 	DWORD error;
659 	static char errbuf[PCAP_ERRBUF_SIZE+1];
660 	int errlen;
661 	char *p;
662 
663 	error = GetLastError();
664 	FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, 0, errbuf,
665 	    PCAP_ERRBUF_SIZE, NULL);
666 
667 	/*
668 	 * "FormatMessage()" "helpfully" sticks CR/LF at the end of the
669 	 * message.  Get rid of it.
670 	 */
671 	errlen = strlen(errbuf);
672 	if (errlen >= 2) {
673 		errbuf[errlen - 1] = '\0';
674 		errbuf[errlen - 2] = '\0';
675 	}
676 	p = strchr(errbuf, '\0');
677 	snprintf (p, sizeof(errbuf)-(p-errbuf), " (%lu)", error);
678 	return (errbuf);
679 }
680 #endif
681 
682 /*
683  * Not all systems have strerror().
684  */
685 const char *
686 pcap_strerror(int errnum)
687 {
688 #ifdef HAVE_STRERROR
689 	return (strerror(errnum));
690 #else
691 	extern int sys_nerr;
692 	extern const char *const sys_errlist[];
693 	static char ebuf[20];
694 
695 	if ((unsigned int)errnum < sys_nerr)
696 		return ((char *)sys_errlist[errnum]);
697 	(void)snprintf(ebuf, sizeof ebuf, "Unknown error: %d", errnum);
698 	return(ebuf);
699 #endif
700 }
701 
702 int
703 pcap_setfilter(pcap_t *p, struct bpf_program *fp)
704 {
705 	return p->setfilter_op(p, fp);
706 }
707 
708 /*
709  * Set direction flag, which controls whether we accept only incoming
710  * packets, only outgoing packets, or both.
711  * Note that, depending on the platform, some or all direction arguments
712  * might not be supported.
713  */
714 int
715 pcap_setdirection(pcap_t *p, pcap_direction_t d)
716 {
717 	if (p->setdirection_op == NULL) {
718 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
719 		    "Setting direction is not implemented on this platform");
720 		return -1;
721 	} else
722 		return p->setdirection_op(p, d);
723 }
724 
725 int
726 pcap_stats(pcap_t *p, struct pcap_stat *ps)
727 {
728 	return p->stats_op(p, ps);
729 }
730 
731 static int
732 pcap_stats_dead(pcap_t *p, struct pcap_stat *ps _U_)
733 {
734 	snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
735 	    "Statistics aren't available from a pcap_open_dead pcap_t");
736 	return (-1);
737 }
738 
739 void
740 pcap_close_common(pcap_t *p)
741 {
742 #ifdef BIOCSETBUFMODE
743 	/*
744 	 * Check to see if this pcap instance was using the zerocopy buffer
745 	 * mode.  If it was, delete the mappings.  Note that p->buffer
746 	 * gets initialized to one of the mmaped regions in this case, so
747 	 * do not try and free it directly.
748 	 *
749 	 * If the regular buffer mode was selected, then it is safe to free
750 	 * this memory.
751 	 */
752 	if (p->zerocopy) {
753 		if (p->zbuf1 != MAP_FAILED && p->zbuf1 != NULL)
754 			munmap(p->zbuf1, p->zbufsize);
755 		if (p->zbuf2 != MAP_FAILED && p->zbuf2 != NULL)
756 			munmap(p->zbuf2, p->zbufsize);
757 		p->buffer = NULL;
758 	} else
759 #endif
760 	if (p->buffer != NULL)
761 		free(p->buffer);
762 #if !defined(WIN32) && !defined(MSDOS)
763 	if (p->fd >= 0)
764 		close(p->fd);
765 #endif
766 }
767 
768 static void
769 pcap_close_dead(pcap_t *p _U_)
770 {
771 	/* Nothing to do. */
772 }
773 
774 pcap_t *
775 pcap_open_dead(int linktype, int snaplen)
776 {
777 	pcap_t *p;
778 
779 	p = malloc(sizeof(*p));
780 	if (p == NULL)
781 		return NULL;
782 	memset (p, 0, sizeof(*p));
783 	p->snapshot = snaplen;
784 	p->linktype = linktype;
785 	p->stats_op = pcap_stats_dead;
786 	p->close_op = pcap_close_dead;
787 	return p;
788 }
789 
790 /*
791  * API compatible with WinPcap's "send a packet" routine - returns -1
792  * on error, 0 otherwise.
793  *
794  * XXX - what if we get a short write?
795  */
796 int
797 pcap_sendpacket(pcap_t *p, const u_char *buf, int size)
798 {
799 	if (p->inject_op(p, buf, size) == -1)
800 		return (-1);
801 	return (0);
802 }
803 
804 /*
805  * API compatible with OpenBSD's "send a packet" routine - returns -1 on
806  * error, number of bytes written otherwise.
807  */
808 int
809 pcap_inject(pcap_t *p, const void *buf, size_t size)
810 {
811 	return (p->inject_op(p, buf, size));
812 }
813 
814 void
815 pcap_close(pcap_t *p)
816 {
817 	p->close_op(p);
818 	if (p->dlt_list != NULL)
819 		free(p->dlt_list);
820 	pcap_freecode(&p->fcode);
821 	free(p);
822 }
823 
824 /*
825  * We make the version string static, and return a pointer to it, rather
826  * than exporting the version string directly.  On at least some UNIXes,
827  * if you import data from a shared library into an program, the data is
828  * bound into the program binary, so if the string in the version of the
829  * library with which the program was linked isn't the same as the
830  * string in the version of the library with which the program is being
831  * run, various undesirable things may happen (warnings, the string
832  * being the one from the version of the library with which the program
833  * was linked, or even weirder things, such as the string being the one
834  * from the library but being truncated).
835  */
836 #ifdef HAVE_VERSION_H
837 #include "version.h"
838 #else
839 static const char pcap_version_string[] = "libpcap version 0.9.8";
840 #endif
841 
842 #ifdef WIN32
843 /*
844  * XXX - it'd be nice if we could somehow generate the WinPcap and libpcap
845  * version numbers when building WinPcap.  (It'd be nice to do so for
846  * the packet.dll version number as well.)
847  */
848 static const char wpcap_version_string[] = "4.0";
849 static const char pcap_version_string_fmt[] =
850     "WinPcap version %s, based on %s";
851 static const char pcap_version_string_packet_dll_fmt[] =
852     "WinPcap version %s (packet.dll version %s), based on %s";
853 static char *full_pcap_version_string;
854 
855 const char *
856 pcap_lib_version(void)
857 {
858 	char *packet_version_string;
859 	size_t full_pcap_version_string_len;
860 
861 	if (full_pcap_version_string == NULL) {
862 		/*
863 		 * Generate the version string.
864 		 */
865 		packet_version_string = PacketGetVersion();
866 		if (strcmp(wpcap_version_string, packet_version_string) == 0) {
867 			/*
868 			 * WinPcap version string and packet.dll version
869 			 * string are the same; just report the WinPcap
870 			 * version.
871 			 */
872 			full_pcap_version_string_len =
873 			    (sizeof pcap_version_string_fmt - 4) +
874 			    strlen(wpcap_version_string) +
875 			    strlen(pcap_version_string);
876 			full_pcap_version_string =
877 			    malloc(full_pcap_version_string_len);
878 			sprintf(full_pcap_version_string,
879 			    pcap_version_string_fmt, wpcap_version_string,
880 			    pcap_version_string);
881 		} else {
882 			/*
883 			 * WinPcap version string and packet.dll version
884 			 * string are different; that shouldn't be the
885 			 * case (the two libraries should come from the
886 			 * same version of WinPcap), so we report both
887 			 * versions.
888 			 */
889 			full_pcap_version_string_len =
890 			    (sizeof pcap_version_string_packet_dll_fmt - 6) +
891 			    strlen(wpcap_version_string) +
892 			    strlen(packet_version_string) +
893 			    strlen(pcap_version_string);
894 			full_pcap_version_string = malloc(full_pcap_version_string_len);
895 
896 			sprintf(full_pcap_version_string,
897 			    pcap_version_string_packet_dll_fmt,
898 			    wpcap_version_string, packet_version_string,
899 			    pcap_version_string);
900 		}
901 	}
902 	return (full_pcap_version_string);
903 }
904 
905 #elif defined(MSDOS)
906 
907 static char *full_pcap_version_string;
908 
909 const char *
910 pcap_lib_version (void)
911 {
912 	char *packet_version_string;
913 	size_t full_pcap_version_string_len;
914 	static char dospfx[] = "DOS-";
915 
916 	if (full_pcap_version_string == NULL) {
917 		/*
918 		 * Generate the version string.
919 		 */
920 		full_pcap_version_string_len =
921 		    sizeof dospfx + strlen(pcap_version_string);
922 		full_pcap_version_string =
923 		    malloc(full_pcap_version_string_len);
924 		strcpy(full_pcap_version_string, dospfx);
925 		strcat(full_pcap_version_string, pcap_version_string);
926 	}
927 	return (full_pcap_version_string);
928 }
929 
930 #else /* UN*X */
931 
932 const char *
933 pcap_lib_version(void)
934 {
935 	return (pcap_version_string);
936 }
937 #endif
938