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