Updates to Tomato RAF including NGINX && PHP
[tomato.git] / release / src / router / libpcap / pcap.c
blobdd60f47e7a6cd56bfec105cd87afb27297afc2ad
1 /*
2 * Copyright (c) 1993, 1994, 1995, 1996, 1997, 1998
3 * The Regents of the University of California. All rights reserved.
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.
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.
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
39 #ifdef HAVE_CONFIG_H
40 #include "config.h"
41 #endif
43 #ifdef WIN32
44 #include <pcap-stdinc.h>
45 #else /* WIN32 */
46 #if HAVE_INTTYPES_H
47 #include <inttypes.h>
48 #elif HAVE_STDINT_H
49 #include <stdint.h>
50 #endif
51 #ifdef HAVE_SYS_BITYPES_H
52 #include <sys/bitypes.h>
53 #endif
54 #include <sys/types.h>
55 #endif /* WIN32 */
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #if !defined(_MSC_VER) && !defined(__BORLANDC__) && !defined(__MINGW32__)
61 #include <unistd.h>
62 #endif
63 #include <fcntl.h>
64 #include <errno.h>
66 #ifdef HAVE_OS_PROTO_H
67 #include "os-proto.h"
68 #endif
70 #ifdef MSDOS
71 #include "pcap-dos.h"
72 #endif
74 #include "pcap-int.h"
76 #ifdef HAVE_DAG_API
77 #include <dagnew.h>
78 #include <dagapi.h>
79 #endif
81 #ifdef HAVE_REMOTE
82 #include <pcap-remote.h>
83 #endif
85 int
86 pcap_not_initialized(pcap_t *pcap)
88 /* this means 'not initialized' */
89 return (PCAP_ERROR_NOT_ACTIVATED);
93 * Returns 1 if rfmon mode can be set on the pcap_t, 0 if it can't,
94 * a PCAP_ERROR value on an error.
96 int
97 pcap_can_set_rfmon(pcap_t *p)
99 return (p->can_set_rfmon_op(p));
103 * For systems where rfmon mode is never supported.
105 static int
106 pcap_cant_set_rfmon(pcap_t *p _U_)
108 return (0);
112 * Sets *tstamp_typesp to point to an array 1 or more supported time stamp
113 * types; the return value is the number of supported time stamp types.
114 * The list should be freed by a call to pcap_free_tstamp_types() when
115 * you're done with it.
117 * A return value of 0 means "you don't get a choice of time stamp type",
118 * in which case *tstamp_typesp is set to null.
120 * PCAP_ERROR is returned on error.
123 pcap_list_tstamp_types(pcap_t *p, int **tstamp_typesp)
125 if (p->tstamp_type_count == 0) {
127 * We don't support multiple time stamp types.
129 *tstamp_typesp = NULL;
130 } else {
131 *tstamp_typesp = (int*)calloc(sizeof(**tstamp_typesp),
132 p->tstamp_type_count);
133 if (*tstamp_typesp == NULL) {
134 (void)snprintf(p->errbuf, sizeof(p->errbuf),
135 "malloc: %s", pcap_strerror(errno));
136 return (PCAP_ERROR);
138 (void)memcpy(*tstamp_typesp, p->tstamp_type_list,
139 sizeof(**tstamp_typesp) * p->tstamp_type_count);
141 return (p->tstamp_type_count);
145 * In Windows, you might have a library built with one version of the
146 * C runtime library and an application built with another version of
147 * the C runtime library, which means that the library might use one
148 * version of malloc() and free() and the application might use another
149 * version of malloc() and free(). If so, that means something
150 * allocated by the library cannot be freed by the application, so we
151 * need to have a pcap_free_tstamp_types() routine to free up the list
152 * allocated by pcap_list_tstamp_types(), even though it's just a wrapper
153 * around free().
155 void
156 pcap_free_tstamp_types(int *tstamp_type_list)
158 free(tstamp_type_list);
162 * Default one-shot callback; overridden for capture types where the
163 * packet data cannot be guaranteed to be available after the callback
164 * returns, so that a copy must be made.
166 static void
167 pcap_oneshot(u_char *user, const struct pcap_pkthdr *h, const u_char *pkt)
169 struct oneshot_userdata *sp = (struct oneshot_userdata *)user;
171 *sp->hdr = *h;
172 *sp->pkt = pkt;
175 const u_char *
176 pcap_next(pcap_t *p, struct pcap_pkthdr *h)
178 struct oneshot_userdata s;
179 const u_char *pkt;
181 s.hdr = h;
182 s.pkt = &pkt;
183 s.pd = p;
184 if (pcap_dispatch(p, 1, p->oneshot_callback, (u_char *)&s) <= 0)
185 return (0);
186 return (pkt);
189 int
190 pcap_next_ex(pcap_t *p, struct pcap_pkthdr **pkt_header,
191 const u_char **pkt_data)
193 struct oneshot_userdata s;
195 s.hdr = &p->pcap_header;
196 s.pkt = pkt_data;
197 s.pd = p;
199 /* Saves a pointer to the packet headers */
200 *pkt_header= &p->pcap_header;
202 if (p->sf.rfile != NULL) {
203 int status;
205 /* We are on an offline capture */
206 status = pcap_offline_read(p, 1, p->oneshot_callback,
207 (u_char *)&s);
210 * Return codes for pcap_offline_read() are:
211 * - 0: EOF
212 * - -1: error
213 * - >1: OK
214 * The first one ('0') conflicts with the return code of
215 * 0 from pcap_read() meaning "no packets arrived before
216 * the timeout expired", so we map it to -2 so you can
217 * distinguish between an EOF from a savefile and a
218 * "no packets arrived before the timeout expired, try
219 * again" from a live capture.
221 if (status == 0)
222 return (-2);
223 else
224 return (status);
228 * Return codes for pcap_read() are:
229 * - 0: timeout
230 * - -1: error
231 * - -2: loop was broken out of with pcap_breakloop()
232 * - >1: OK
233 * The first one ('0') conflicts with the return code of 0 from
234 * pcap_offline_read() meaning "end of file".
236 return (p->read_op(p, 1, p->oneshot_callback, (u_char *)&s));
239 static void
240 initialize_ops(pcap_t *p)
243 * Set operation pointers for operations that only work on
244 * an activated pcap_t to point to a routine that returns
245 * a "this isn't activated" error.
247 p->read_op = (read_op_t)pcap_not_initialized;
248 p->inject_op = (inject_op_t)pcap_not_initialized;
249 p->setfilter_op = (setfilter_op_t)pcap_not_initialized;
250 p->setdirection_op = (setdirection_op_t)pcap_not_initialized;
251 p->set_datalink_op = (set_datalink_op_t)pcap_not_initialized;
252 p->getnonblock_op = (getnonblock_op_t)pcap_not_initialized;
253 p->setnonblock_op = (setnonblock_op_t)pcap_not_initialized;
254 p->stats_op = (stats_op_t)pcap_not_initialized;
255 #ifdef WIN32
256 p->setbuff_op = (setbuff_op_t)pcap_not_initialized;
257 p->setmode_op = (setmode_op_t)pcap_not_initialized;
258 p->setmintocopy_op = (setmintocopy_op_t)pcap_not_initialized;
259 #endif
262 * Default cleanup operation - implementations can override
263 * this, but should call pcap_cleanup_live_common() after
264 * doing their own additional cleanup.
266 p->cleanup_op = pcap_cleanup_live_common;
269 * In most cases, the standard one-short callback can
270 * be used for pcap_next()/pcap_next_ex().
272 p->oneshot_callback = pcap_oneshot;
275 pcap_t *
276 pcap_create_common(const char *source, char *ebuf)
278 pcap_t *p;
280 p = malloc(sizeof(*p));
281 if (p == NULL) {
282 snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s",
283 pcap_strerror(errno));
284 return (NULL);
286 memset(p, 0, sizeof(*p));
287 #ifndef WIN32
288 p->fd = -1; /* not opened yet */
289 p->selectable_fd = -1;
290 p->send_fd = -1;
291 #endif
293 p->opt.source = strdup(source);
294 if (p->opt.source == NULL) {
295 snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s",
296 pcap_strerror(errno));
297 free(p);
298 return (NULL);
302 * Default to "can't set rfmon mode"; if it's supported by
303 * a platform, the create routine that called us can set
304 * the op to its routine to check whether a particular
305 * device supports it.
307 p->can_set_rfmon_op = pcap_cant_set_rfmon;
309 initialize_ops(p);
311 /* put in some defaults*/
312 pcap_set_timeout(p, 0);
313 pcap_set_snaplen(p, 65535); /* max packet size */
314 p->opt.promisc = 0;
315 p->opt.buffer_size = 0;
316 p->opt.tstamp_type = -1; /* default to not setting time stamp type */
317 return (p);
321 pcap_check_activated(pcap_t *p)
323 if (p->activated) {
324 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "can't perform "
325 " operation on activated capture");
326 return (-1);
328 return (0);
332 pcap_set_snaplen(pcap_t *p, int snaplen)
334 if (pcap_check_activated(p))
335 return (PCAP_ERROR_ACTIVATED);
336 p->snapshot = snaplen;
337 return (0);
341 pcap_set_promisc(pcap_t *p, int promisc)
343 if (pcap_check_activated(p))
344 return (PCAP_ERROR_ACTIVATED);
345 p->opt.promisc = promisc;
346 return (0);
350 pcap_set_rfmon(pcap_t *p, int rfmon)
352 if (pcap_check_activated(p))
353 return (PCAP_ERROR_ACTIVATED);
354 p->opt.rfmon = rfmon;
355 return (0);
359 pcap_set_timeout(pcap_t *p, int timeout_ms)
361 if (pcap_check_activated(p))
362 return (PCAP_ERROR_ACTIVATED);
363 p->md.timeout = timeout_ms;
364 return (0);
368 pcap_set_tstamp_type(pcap_t *p, int tstamp_type)
370 int i;
372 if (pcap_check_activated(p))
373 return (PCAP_ERROR_ACTIVATED);
376 * If p->tstamp_type_count is 0, we don't support setting
377 * the time stamp type at all.
379 if (p->tstamp_type_count == 0)
380 return (PCAP_ERROR_CANTSET_TSTAMP_TYPE);
383 * Check whether we claim to support this type of time stamp.
385 for (i = 0; i < p->tstamp_type_count; i++) {
386 if (p->tstamp_type_list[i] == tstamp_type) {
388 * Yes.
390 p->opt.tstamp_type = tstamp_type;
391 return (0);
396 * No. We support setting the time stamp type, but not to this
397 * particular value.
399 return (PCAP_WARNING_TSTAMP_TYPE_NOTSUP);
403 pcap_set_buffer_size(pcap_t *p, int buffer_size)
405 if (pcap_check_activated(p))
406 return (PCAP_ERROR_ACTIVATED);
407 p->opt.buffer_size = buffer_size;
408 return (0);
412 pcap_activate(pcap_t *p)
414 int status;
417 * Catch attempts to re-activate an already-activated
418 * pcap_t; this should, for example, catch code that
419 * calls pcap_open_live() followed by pcap_activate(),
420 * as some code that showed up in a Stack Exchange
421 * question did.
423 if (pcap_check_activated(p))
424 return (PCAP_ERROR_ACTIVATED);
425 status = p->activate_op(p);
426 if (status >= 0)
427 p->activated = 1;
428 else {
429 if (p->errbuf[0] == '\0') {
431 * No error message supplied by the activate routine;
432 * for the benefit of programs that don't specially
433 * handle errors other than PCAP_ERROR, return the
434 * error message corresponding to the status.
436 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "%s",
437 pcap_statustostr(status));
441 * Undo any operation pointer setting, etc. done by
442 * the activate operation.
444 initialize_ops(p);
446 return (status);
449 pcap_t *
450 pcap_open_live(const char *source, int snaplen, int promisc, int to_ms, char *errbuf)
452 pcap_t *p;
453 int status;
455 p = pcap_create(source, errbuf);
456 if (p == NULL)
457 return (NULL);
458 status = pcap_set_snaplen(p, snaplen);
459 if (status < 0)
460 goto fail;
461 status = pcap_set_promisc(p, promisc);
462 if (status < 0)
463 goto fail;
464 status = pcap_set_timeout(p, to_ms);
465 if (status < 0)
466 goto fail;
468 * Mark this as opened with pcap_open_live(), so that, for
469 * example, we show the full list of DLT_ values, rather
470 * than just the ones that are compatible with capturing
471 * when not in monitor mode. That allows existing applications
472 * to work the way they used to work, but allows new applications
473 * that know about the new open API to, for example, find out the
474 * DLT_ values that they can select without changing whether
475 * the adapter is in monitor mode or not.
477 p->oldstyle = 1;
478 status = pcap_activate(p);
479 if (status < 0)
480 goto fail;
481 return (p);
482 fail:
483 if (status == PCAP_ERROR)
484 snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s", source,
485 p->errbuf);
486 else if (status == PCAP_ERROR_NO_SUCH_DEVICE ||
487 status == PCAP_ERROR_PERM_DENIED ||
488 status == PCAP_ERROR_PROMISC_PERM_DENIED)
489 snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s (%s)", source,
490 pcap_statustostr(status), p->errbuf);
491 else
492 snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s", source,
493 pcap_statustostr(status));
494 pcap_close(p);
495 return (NULL);
499 pcap_dispatch(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
501 return (p->read_op(p, cnt, callback, user));
505 * XXX - is this necessary?
508 pcap_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
511 return (p->read_op(p, cnt, callback, user));
515 pcap_loop(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
517 register int n;
519 #ifdef HAVE_REMOTE
520 /* Checks the capture type */
521 if (p->rmt_clientside)
523 /* We are on an remote capture */
524 if (!p->rmt_capstarted)
526 // if the capture has not started yet, please start it
527 if (pcap_startcapture_remote(p) )
528 return -1;
531 #endif /* HAVE_REMOTE */
533 for (;;) {
534 if (p->sf.rfile != NULL) {
536 * 0 means EOF, so don't loop if we get 0.
538 n = pcap_offline_read(p, cnt, callback, user);
539 } else {
541 * XXX keep reading until we get something
542 * (or an error occurs)
544 do {
545 n = p->read_op(p, cnt, callback, user);
546 } while (n == 0);
548 if (n <= 0)
549 return (n);
550 if (cnt > 0) {
551 cnt -= n;
552 if (cnt <= 0)
553 return (0);
559 * Force the loop in "pcap_read()" or "pcap_read_offline()" to terminate.
561 void
562 pcap_breakloop(pcap_t *p)
564 p->break_loop = 1;
568 pcap_datalink(pcap_t *p)
570 return (p->linktype);
574 pcap_datalink_ext(pcap_t *p)
576 return (p->linktype_ext);
580 pcap_list_datalinks(pcap_t *p, int **dlt_buffer)
582 if (p->dlt_count == 0) {
584 * We couldn't fetch the list of DLTs, which means
585 * this platform doesn't support changing the
586 * DLT for an interface. Return a list of DLTs
587 * containing only the DLT this device supports.
589 *dlt_buffer = (int*)malloc(sizeof(**dlt_buffer));
590 if (*dlt_buffer == NULL) {
591 (void)snprintf(p->errbuf, sizeof(p->errbuf),
592 "malloc: %s", pcap_strerror(errno));
593 return (-1);
595 **dlt_buffer = p->linktype;
596 return (1);
597 } else {
598 *dlt_buffer = (int*)calloc(sizeof(**dlt_buffer), p->dlt_count);
599 if (*dlt_buffer == NULL) {
600 (void)snprintf(p->errbuf, sizeof(p->errbuf),
601 "malloc: %s", pcap_strerror(errno));
602 return (-1);
604 (void)memcpy(*dlt_buffer, p->dlt_list,
605 sizeof(**dlt_buffer) * p->dlt_count);
606 return (p->dlt_count);
611 * In Windows, you might have a library built with one version of the
612 * C runtime library and an application built with another version of
613 * the C runtime library, which means that the library might use one
614 * version of malloc() and free() and the application might use another
615 * version of malloc() and free(). If so, that means something
616 * allocated by the library cannot be freed by the application, so we
617 * need to have a pcap_free_datalinks() routine to free up the list
618 * allocated by pcap_list_datalinks(), even though it's just a wrapper
619 * around free().
621 void
622 pcap_free_datalinks(int *dlt_list)
624 free(dlt_list);
628 pcap_set_datalink(pcap_t *p, int dlt)
630 int i;
631 const char *dlt_name;
633 if (p->dlt_count == 0 || p->set_datalink_op == NULL) {
635 * We couldn't fetch the list of DLTs, or we don't
636 * have a "set datalink" operation, which means
637 * this platform doesn't support changing the
638 * DLT for an interface. Check whether the new
639 * DLT is the one this interface supports.
641 if (p->linktype != dlt)
642 goto unsupported;
645 * It is, so there's nothing we need to do here.
647 return (0);
649 for (i = 0; i < p->dlt_count; i++)
650 if (p->dlt_list[i] == dlt)
651 break;
652 if (i >= p->dlt_count)
653 goto unsupported;
654 if (p->dlt_count == 2 && p->dlt_list[0] == DLT_EN10MB &&
655 dlt == DLT_DOCSIS) {
657 * This is presumably an Ethernet device, as the first
658 * link-layer type it offers is DLT_EN10MB, and the only
659 * other type it offers is DLT_DOCSIS. That means that
660 * we can't tell the driver to supply DOCSIS link-layer
661 * headers - we're just pretending that's what we're
662 * getting, as, presumably, we're capturing on a dedicated
663 * link to a Cisco Cable Modem Termination System, and
664 * it's putting raw DOCSIS frames on the wire inside low-level
665 * Ethernet framing.
667 p->linktype = dlt;
668 return (0);
670 if (p->set_datalink_op(p, dlt) == -1)
671 return (-1);
672 p->linktype = dlt;
673 return (0);
675 unsupported:
676 dlt_name = pcap_datalink_val_to_name(dlt);
677 if (dlt_name != NULL) {
678 (void) snprintf(p->errbuf, sizeof(p->errbuf),
679 "%s is not one of the DLTs supported by this device",
680 dlt_name);
681 } else {
682 (void) snprintf(p->errbuf, sizeof(p->errbuf),
683 "DLT %d is not one of the DLTs supported by this device",
684 dlt);
686 return (-1);
690 * This array is designed for mapping upper and lower case letter
691 * together for a case independent comparison. The mappings are
692 * based upon ascii character sequences.
694 static const u_char charmap[] = {
695 (u_char)'\000', (u_char)'\001', (u_char)'\002', (u_char)'\003',
696 (u_char)'\004', (u_char)'\005', (u_char)'\006', (u_char)'\007',
697 (u_char)'\010', (u_char)'\011', (u_char)'\012', (u_char)'\013',
698 (u_char)'\014', (u_char)'\015', (u_char)'\016', (u_char)'\017',
699 (u_char)'\020', (u_char)'\021', (u_char)'\022', (u_char)'\023',
700 (u_char)'\024', (u_char)'\025', (u_char)'\026', (u_char)'\027',
701 (u_char)'\030', (u_char)'\031', (u_char)'\032', (u_char)'\033',
702 (u_char)'\034', (u_char)'\035', (u_char)'\036', (u_char)'\037',
703 (u_char)'\040', (u_char)'\041', (u_char)'\042', (u_char)'\043',
704 (u_char)'\044', (u_char)'\045', (u_char)'\046', (u_char)'\047',
705 (u_char)'\050', (u_char)'\051', (u_char)'\052', (u_char)'\053',
706 (u_char)'\054', (u_char)'\055', (u_char)'\056', (u_char)'\057',
707 (u_char)'\060', (u_char)'\061', (u_char)'\062', (u_char)'\063',
708 (u_char)'\064', (u_char)'\065', (u_char)'\066', (u_char)'\067',
709 (u_char)'\070', (u_char)'\071', (u_char)'\072', (u_char)'\073',
710 (u_char)'\074', (u_char)'\075', (u_char)'\076', (u_char)'\077',
711 (u_char)'\100', (u_char)'\141', (u_char)'\142', (u_char)'\143',
712 (u_char)'\144', (u_char)'\145', (u_char)'\146', (u_char)'\147',
713 (u_char)'\150', (u_char)'\151', (u_char)'\152', (u_char)'\153',
714 (u_char)'\154', (u_char)'\155', (u_char)'\156', (u_char)'\157',
715 (u_char)'\160', (u_char)'\161', (u_char)'\162', (u_char)'\163',
716 (u_char)'\164', (u_char)'\165', (u_char)'\166', (u_char)'\167',
717 (u_char)'\170', (u_char)'\171', (u_char)'\172', (u_char)'\133',
718 (u_char)'\134', (u_char)'\135', (u_char)'\136', (u_char)'\137',
719 (u_char)'\140', (u_char)'\141', (u_char)'\142', (u_char)'\143',
720 (u_char)'\144', (u_char)'\145', (u_char)'\146', (u_char)'\147',
721 (u_char)'\150', (u_char)'\151', (u_char)'\152', (u_char)'\153',
722 (u_char)'\154', (u_char)'\155', (u_char)'\156', (u_char)'\157',
723 (u_char)'\160', (u_char)'\161', (u_char)'\162', (u_char)'\163',
724 (u_char)'\164', (u_char)'\165', (u_char)'\166', (u_char)'\167',
725 (u_char)'\170', (u_char)'\171', (u_char)'\172', (u_char)'\173',
726 (u_char)'\174', (u_char)'\175', (u_char)'\176', (u_char)'\177',
727 (u_char)'\200', (u_char)'\201', (u_char)'\202', (u_char)'\203',
728 (u_char)'\204', (u_char)'\205', (u_char)'\206', (u_char)'\207',
729 (u_char)'\210', (u_char)'\211', (u_char)'\212', (u_char)'\213',
730 (u_char)'\214', (u_char)'\215', (u_char)'\216', (u_char)'\217',
731 (u_char)'\220', (u_char)'\221', (u_char)'\222', (u_char)'\223',
732 (u_char)'\224', (u_char)'\225', (u_char)'\226', (u_char)'\227',
733 (u_char)'\230', (u_char)'\231', (u_char)'\232', (u_char)'\233',
734 (u_char)'\234', (u_char)'\235', (u_char)'\236', (u_char)'\237',
735 (u_char)'\240', (u_char)'\241', (u_char)'\242', (u_char)'\243',
736 (u_char)'\244', (u_char)'\245', (u_char)'\246', (u_char)'\247',
737 (u_char)'\250', (u_char)'\251', (u_char)'\252', (u_char)'\253',
738 (u_char)'\254', (u_char)'\255', (u_char)'\256', (u_char)'\257',
739 (u_char)'\260', (u_char)'\261', (u_char)'\262', (u_char)'\263',
740 (u_char)'\264', (u_char)'\265', (u_char)'\266', (u_char)'\267',
741 (u_char)'\270', (u_char)'\271', (u_char)'\272', (u_char)'\273',
742 (u_char)'\274', (u_char)'\275', (u_char)'\276', (u_char)'\277',
743 (u_char)'\300', (u_char)'\341', (u_char)'\342', (u_char)'\343',
744 (u_char)'\344', (u_char)'\345', (u_char)'\346', (u_char)'\347',
745 (u_char)'\350', (u_char)'\351', (u_char)'\352', (u_char)'\353',
746 (u_char)'\354', (u_char)'\355', (u_char)'\356', (u_char)'\357',
747 (u_char)'\360', (u_char)'\361', (u_char)'\362', (u_char)'\363',
748 (u_char)'\364', (u_char)'\365', (u_char)'\366', (u_char)'\367',
749 (u_char)'\370', (u_char)'\371', (u_char)'\372', (u_char)'\333',
750 (u_char)'\334', (u_char)'\335', (u_char)'\336', (u_char)'\337',
751 (u_char)'\340', (u_char)'\341', (u_char)'\342', (u_char)'\343',
752 (u_char)'\344', (u_char)'\345', (u_char)'\346', (u_char)'\347',
753 (u_char)'\350', (u_char)'\351', (u_char)'\352', (u_char)'\353',
754 (u_char)'\354', (u_char)'\355', (u_char)'\356', (u_char)'\357',
755 (u_char)'\360', (u_char)'\361', (u_char)'\362', (u_char)'\363',
756 (u_char)'\364', (u_char)'\365', (u_char)'\366', (u_char)'\367',
757 (u_char)'\370', (u_char)'\371', (u_char)'\372', (u_char)'\373',
758 (u_char)'\374', (u_char)'\375', (u_char)'\376', (u_char)'\377',
762 pcap_strcasecmp(const char *s1, const char *s2)
764 register const u_char *cm = charmap,
765 *us1 = (const u_char *)s1,
766 *us2 = (const u_char *)s2;
768 while (cm[*us1] == cm[*us2++])
769 if (*us1++ == '\0')
770 return(0);
771 return (cm[*us1] - cm[*--us2]);
774 struct dlt_choice {
775 const char *name;
776 const char *description;
777 int dlt;
780 #define DLT_CHOICE(code, description) { #code, description, code }
781 #define DLT_CHOICE_SENTINEL { NULL, NULL, 0 }
783 static struct dlt_choice dlt_choices[] = {
784 DLT_CHOICE(DLT_NULL, "BSD loopback"),
785 DLT_CHOICE(DLT_EN10MB, "Ethernet"),
786 DLT_CHOICE(DLT_IEEE802, "Token ring"),
787 DLT_CHOICE(DLT_ARCNET, "BSD ARCNET"),
788 DLT_CHOICE(DLT_SLIP, "SLIP"),
789 DLT_CHOICE(DLT_PPP, "PPP"),
790 DLT_CHOICE(DLT_FDDI, "FDDI"),
791 DLT_CHOICE(DLT_ATM_RFC1483, "RFC 1483 LLC-encapsulated ATM"),
792 DLT_CHOICE(DLT_RAW, "Raw IP"),
793 DLT_CHOICE(DLT_SLIP_BSDOS, "BSD/OS SLIP"),
794 DLT_CHOICE(DLT_PPP_BSDOS, "BSD/OS PPP"),
795 DLT_CHOICE(DLT_ATM_CLIP, "Linux Classical IP-over-ATM"),
796 DLT_CHOICE(DLT_PPP_SERIAL, "PPP over serial"),
797 DLT_CHOICE(DLT_PPP_ETHER, "PPPoE"),
798 DLT_CHOICE(DLT_SYMANTEC_FIREWALL, "Symantec Firewall"),
799 DLT_CHOICE(DLT_C_HDLC, "Cisco HDLC"),
800 DLT_CHOICE(DLT_IEEE802_11, "802.11"),
801 DLT_CHOICE(DLT_FRELAY, "Frame Relay"),
802 DLT_CHOICE(DLT_LOOP, "OpenBSD loopback"),
803 DLT_CHOICE(DLT_ENC, "OpenBSD encapsulated IP"),
804 DLT_CHOICE(DLT_LINUX_SLL, "Linux cooked"),
805 DLT_CHOICE(DLT_LTALK, "Localtalk"),
806 DLT_CHOICE(DLT_PFLOG, "OpenBSD pflog file"),
807 DLT_CHOICE(DLT_PFSYNC, "Packet filter state syncing"),
808 DLT_CHOICE(DLT_PRISM_HEADER, "802.11 plus Prism header"),
809 DLT_CHOICE(DLT_IP_OVER_FC, "RFC 2625 IP-over-Fibre Channel"),
810 DLT_CHOICE(DLT_SUNATM, "Sun raw ATM"),
811 DLT_CHOICE(DLT_IEEE802_11_RADIO, "802.11 plus radiotap header"),
812 DLT_CHOICE(DLT_ARCNET_LINUX, "Linux ARCNET"),
813 DLT_CHOICE(DLT_JUNIPER_MLPPP, "Juniper Multi-Link PPP"),
814 DLT_CHOICE(DLT_JUNIPER_MLFR, "Juniper Multi-Link Frame Relay"),
815 DLT_CHOICE(DLT_JUNIPER_ES, "Juniper Encryption Services PIC"),
816 DLT_CHOICE(DLT_JUNIPER_GGSN, "Juniper GGSN PIC"),
817 DLT_CHOICE(DLT_JUNIPER_MFR, "Juniper FRF.16 Frame Relay"),
818 DLT_CHOICE(DLT_JUNIPER_ATM2, "Juniper ATM2 PIC"),
819 DLT_CHOICE(DLT_JUNIPER_SERVICES, "Juniper Advanced Services PIC"),
820 DLT_CHOICE(DLT_JUNIPER_ATM1, "Juniper ATM1 PIC"),
821 DLT_CHOICE(DLT_APPLE_IP_OVER_IEEE1394, "Apple IP-over-IEEE 1394"),
822 DLT_CHOICE(DLT_MTP2_WITH_PHDR, "SS7 MTP2 with Pseudo-header"),
823 DLT_CHOICE(DLT_MTP2, "SS7 MTP2"),
824 DLT_CHOICE(DLT_MTP3, "SS7 MTP3"),
825 DLT_CHOICE(DLT_SCCP, "SS7 SCCP"),
826 DLT_CHOICE(DLT_DOCSIS, "DOCSIS"),
827 DLT_CHOICE(DLT_LINUX_IRDA, "Linux IrDA"),
828 DLT_CHOICE(DLT_IEEE802_11_RADIO_AVS, "802.11 plus AVS radio information header"),
829 DLT_CHOICE(DLT_JUNIPER_MONITOR, "Juniper Passive Monitor PIC"),
830 DLT_CHOICE(DLT_PPP_PPPD, "PPP for pppd, with direction flag"),
831 DLT_CHOICE(DLT_JUNIPER_PPPOE, "Juniper PPPoE"),
832 DLT_CHOICE(DLT_JUNIPER_PPPOE_ATM, "Juniper PPPoE/ATM"),
833 DLT_CHOICE(DLT_GPRS_LLC, "GPRS LLC"),
834 DLT_CHOICE(DLT_GPF_T, "GPF-T"),
835 DLT_CHOICE(DLT_GPF_F, "GPF-F"),
836 DLT_CHOICE(DLT_JUNIPER_PIC_PEER, "Juniper PIC Peer"),
837 DLT_CHOICE(DLT_ERF_ETH, "Ethernet with Endace ERF header"),
838 DLT_CHOICE(DLT_ERF_POS, "Packet-over-SONET with Endace ERF header"),
839 DLT_CHOICE(DLT_LINUX_LAPD, "Linux vISDN LAPD"),
840 DLT_CHOICE(DLT_JUNIPER_ETHER, "Juniper Ethernet"),
841 DLT_CHOICE(DLT_JUNIPER_PPP, "Juniper PPP"),
842 DLT_CHOICE(DLT_JUNIPER_FRELAY, "Juniper Frame Relay"),
843 DLT_CHOICE(DLT_JUNIPER_CHDLC, "Juniper C-HDLC"),
844 DLT_CHOICE(DLT_MFR, "FRF.16 Frame Relay"),
845 DLT_CHOICE(DLT_JUNIPER_VP, "Juniper Voice PIC"),
846 DLT_CHOICE(DLT_A429, "Arinc 429"),
847 DLT_CHOICE(DLT_A653_ICM, "Arinc 653 Interpartition Communication"),
848 DLT_CHOICE(DLT_USB, "USB"),
849 DLT_CHOICE(DLT_BLUETOOTH_HCI_H4, "Bluetooth HCI UART transport layer"),
850 DLT_CHOICE(DLT_IEEE802_16_MAC_CPS, "IEEE 802.16 MAC Common Part Sublayer"),
851 DLT_CHOICE(DLT_USB_LINUX, "USB with Linux header"),
852 DLT_CHOICE(DLT_CAN20B, "Controller Area Network (CAN) v. 2.0B"),
853 DLT_CHOICE(DLT_IEEE802_15_4_LINUX, "IEEE 802.15.4 with Linux padding"),
854 DLT_CHOICE(DLT_PPI, "Per-Packet Information"),
855 DLT_CHOICE(DLT_IEEE802_16_MAC_CPS_RADIO, "IEEE 802.16 MAC Common Part Sublayer plus radiotap header"),
856 DLT_CHOICE(DLT_JUNIPER_ISM, "Juniper Integrated Service Module"),
857 DLT_CHOICE(DLT_IEEE802_15_4, "IEEE 802.15.4 with FCS"),
858 DLT_CHOICE(DLT_SITA, "SITA pseudo-header"),
859 DLT_CHOICE(DLT_ERF, "Endace ERF header"),
860 DLT_CHOICE(DLT_RAIF1, "Ethernet with u10 Networks pseudo-header"),
861 DLT_CHOICE(DLT_IPMB, "IPMB"),
862 DLT_CHOICE(DLT_JUNIPER_ST, "Juniper Secure Tunnel"),
863 DLT_CHOICE(DLT_BLUETOOTH_HCI_H4_WITH_PHDR, "Bluetooth HCI UART transport layer plus pseudo-header"),
864 DLT_CHOICE(DLT_AX25_KISS, "AX.25 with KISS header"),
865 DLT_CHOICE(DLT_IEEE802_15_4_NONASK_PHY, "IEEE 802.15.4 with non-ASK PHY data"),
866 DLT_CHOICE(DLT_MPLS, "MPLS with label as link-layer header"),
867 DLT_CHOICE(DLT_USB_LINUX_MMAPPED, "USB with padded Linux header"),
868 DLT_CHOICE(DLT_DECT, "DECT"),
869 DLT_CHOICE(DLT_AOS, "AOS Space Data Link protocol"),
870 DLT_CHOICE(DLT_WIHART, "Wireless HART"),
871 DLT_CHOICE(DLT_FC_2, "Fibre Channel FC-2"),
872 DLT_CHOICE(DLT_FC_2_WITH_FRAME_DELIMS, "Fibre Channel FC-2 with frame delimiters"),
873 DLT_CHOICE(DLT_IPNET, "Solaris ipnet"),
874 DLT_CHOICE(DLT_CAN_SOCKETCAN, "CAN-bus with SocketCAN headers"),
875 DLT_CHOICE(DLT_IPV4, "Raw IPv4"),
876 DLT_CHOICE(DLT_IPV6, "Raw IPv6"),
877 DLT_CHOICE(DLT_IEEE802_15_4_NOFCS, "IEEE 802.15.4 without FCS"),
878 DLT_CHOICE(DLT_JUNIPER_VS, "Juniper Virtual Server"),
879 DLT_CHOICE(DLT_JUNIPER_SRX_E2E, "Juniper SRX E2E"),
880 DLT_CHOICE(DLT_JUNIPER_FIBRECHANNEL, "Juniper Fibre Channel"),
881 DLT_CHOICE(DLT_DVB_CI, "DVB-CI"),
882 DLT_CHOICE(DLT_JUNIPER_ATM_CEMIC, "Juniper ATM CEMIC"),
883 DLT_CHOICE(DLT_NFLOG, "Linux netfilter log messages"),
884 DLT_CHOICE(DLT_NETANALYZER, "Ethernet with Hilscher netANALYZER pseudo-header"),
885 DLT_CHOICE(DLT_NETANALYZER_TRANSPARENT, "Ethernet with Hilscher netANALYZER pseudo-header and with preamble and SFD"),
886 DLT_CHOICE(DLT_IPOIB, "RFC 4391 IP-over-Infiniband"),
887 DLT_CHOICE_SENTINEL
891 pcap_datalink_name_to_val(const char *name)
893 int i;
895 for (i = 0; dlt_choices[i].name != NULL; i++) {
896 if (pcap_strcasecmp(dlt_choices[i].name + sizeof("DLT_") - 1,
897 name) == 0)
898 return (dlt_choices[i].dlt);
900 return (-1);
903 const char *
904 pcap_datalink_val_to_name(int dlt)
906 int i;
908 for (i = 0; dlt_choices[i].name != NULL; i++) {
909 if (dlt_choices[i].dlt == dlt)
910 return (dlt_choices[i].name + sizeof("DLT_") - 1);
912 return (NULL);
915 const char *
916 pcap_datalink_val_to_description(int dlt)
918 int i;
920 for (i = 0; dlt_choices[i].name != NULL; i++) {
921 if (dlt_choices[i].dlt == dlt)
922 return (dlt_choices[i].description);
924 return (NULL);
927 struct tstamp_type_choice {
928 const char *name;
929 const char *description;
930 int type;
933 static struct tstamp_type_choice tstamp_type_choices[] = {
934 { "host", "Host", PCAP_TSTAMP_HOST },
935 { "host_lowprec", "Host, low precision", PCAP_TSTAMP_HOST_LOWPREC },
936 { "host_hiprec", "Host, high precision", PCAP_TSTAMP_HOST_HIPREC },
937 { "adapter", "Adapter", PCAP_TSTAMP_ADAPTER },
938 { "adapter_unsynced", "Adapter, not synced with system time", PCAP_TSTAMP_ADAPTER_UNSYNCED },
939 { NULL, NULL, 0 }
943 pcap_tstamp_type_name_to_val(const char *name)
945 int i;
947 for (i = 0; tstamp_type_choices[i].name != NULL; i++) {
948 if (pcap_strcasecmp(tstamp_type_choices[i].name, name) == 0)
949 return (tstamp_type_choices[i].type);
951 return (PCAP_ERROR);
954 const char *
955 pcap_tstamp_type_val_to_name(int tstamp_type)
957 int i;
959 for (i = 0; tstamp_type_choices[i].name != NULL; i++) {
960 if (tstamp_type_choices[i].type == tstamp_type)
961 return (tstamp_type_choices[i].name);
963 return (NULL);
966 const char *
967 pcap_tstamp_type_val_to_description(int tstamp_type)
969 int i;
971 for (i = 0; tstamp_type_choices[i].name != NULL; i++) {
972 if (tstamp_type_choices[i].type == tstamp_type)
973 return (tstamp_type_choices[i].description);
975 return (NULL);
979 pcap_snapshot(pcap_t *p)
981 return (p->snapshot);
985 pcap_is_swapped(pcap_t *p)
987 return (p->sf.swapped);
991 pcap_major_version(pcap_t *p)
993 return (p->sf.version_major);
997 pcap_minor_version(pcap_t *p)
999 return (p->sf.version_minor);
1002 FILE *
1003 pcap_file(pcap_t *p)
1005 return (p->sf.rfile);
1009 pcap_fileno(pcap_t *p)
1011 #ifdef HAVE_REMOTE
1012 if (p->rmt_clientside)
1013 return(p->rmt_sockdata);
1014 #endif /* HAVE_REMOTE */
1016 #ifndef WIN32
1017 return (p->fd);
1018 #else
1019 if (p->adapter != NULL)
1020 return ((int)(DWORD)p->adapter->hFile);
1021 else
1022 return (-1);
1023 #endif
1026 #if !defined(WIN32) && !defined(MSDOS)
1028 pcap_get_selectable_fd(pcap_t *p)
1030 return (p->selectable_fd);
1032 #endif
1034 void
1035 pcap_perror(pcap_t *p, char *prefix)
1037 fprintf(stderr, "%s: %s\n", prefix, p->errbuf);
1040 char *
1041 pcap_geterr(pcap_t *p)
1043 return (p->errbuf);
1047 pcap_getnonblock(pcap_t *p, char *errbuf)
1049 return (p->getnonblock_op(p, errbuf));
1053 * Get the current non-blocking mode setting, under the assumption that
1054 * it's just the standard POSIX non-blocking flag.
1056 * We don't look at "p->nonblock", in case somebody tweaked the FD
1057 * directly.
1059 #if !defined(WIN32) && !defined(MSDOS)
1061 pcap_getnonblock_fd(pcap_t *p, char *errbuf)
1063 int fdflags;
1065 fdflags = fcntl(p->fd, F_GETFL, 0);
1066 if (fdflags == -1) {
1067 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s",
1068 pcap_strerror(errno));
1069 return (-1);
1071 if (fdflags & O_NONBLOCK)
1072 return (1);
1073 else
1074 return (0);
1076 #endif
1079 pcap_setnonblock(pcap_t *p, int nonblock, char *errbuf)
1081 return (p->setnonblock_op(p, nonblock, errbuf));
1084 #if !defined(WIN32) && !defined(MSDOS)
1086 * Set non-blocking mode, under the assumption that it's just the
1087 * standard POSIX non-blocking flag. (This can be called by the
1088 * per-platform non-blocking-mode routine if that routine also
1089 * needs to do some additional work.)
1092 pcap_setnonblock_fd(pcap_t *p, int nonblock, char *errbuf)
1094 int fdflags;
1096 fdflags = fcntl(p->fd, F_GETFL, 0);
1097 if (fdflags == -1) {
1098 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s",
1099 pcap_strerror(errno));
1100 return (-1);
1102 if (nonblock)
1103 fdflags |= O_NONBLOCK;
1104 else
1105 fdflags &= ~O_NONBLOCK;
1106 if (fcntl(p->fd, F_SETFL, fdflags) == -1) {
1107 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_SETFL: %s",
1108 pcap_strerror(errno));
1109 return (-1);
1111 return (0);
1113 #endif
1115 #ifdef WIN32
1117 * Generate a string for the last Win32-specific error (i.e. an error generated when
1118 * calling a Win32 API).
1119 * For errors occurred during standard C calls, we still use pcap_strerror()
1121 char *
1122 pcap_win32strerror(void)
1124 DWORD error;
1125 static char errbuf[PCAP_ERRBUF_SIZE+1];
1126 int errlen;
1127 char *p;
1129 error = GetLastError();
1130 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, 0, errbuf,
1131 PCAP_ERRBUF_SIZE, NULL);
1134 * "FormatMessage()" "helpfully" sticks CR/LF at the end of the
1135 * message. Get rid of it.
1137 errlen = strlen(errbuf);
1138 if (errlen >= 2) {
1139 errbuf[errlen - 1] = '\0';
1140 errbuf[errlen - 2] = '\0';
1142 p = strchr(errbuf, '\0');
1143 snprintf (p, sizeof(errbuf)-(p-errbuf), " (%lu)", error);
1144 return (errbuf);
1146 #endif
1149 * Generate error strings for PCAP_ERROR_ and PCAP_WARNING_ values.
1151 const char *
1152 pcap_statustostr(int errnum)
1154 static char ebuf[15+10+1];
1156 switch (errnum) {
1158 case PCAP_WARNING:
1159 return("Generic warning");
1161 case PCAP_WARNING_TSTAMP_TYPE_NOTSUP:
1162 return ("That type of time stamp is not supported by that device");
1164 case PCAP_WARNING_PROMISC_NOTSUP:
1165 return ("That device doesn't support promiscuous mode");
1167 case PCAP_ERROR:
1168 return("Generic error");
1170 case PCAP_ERROR_BREAK:
1171 return("Loop terminated by pcap_breakloop");
1173 case PCAP_ERROR_NOT_ACTIVATED:
1174 return("The pcap_t has not been activated");
1176 case PCAP_ERROR_ACTIVATED:
1177 return ("The setting can't be changed after the pcap_t is activated");
1179 case PCAP_ERROR_NO_SUCH_DEVICE:
1180 return ("No such device exists");
1182 case PCAP_ERROR_RFMON_NOTSUP:
1183 return ("That device doesn't support monitor mode");
1185 case PCAP_ERROR_NOT_RFMON:
1186 return ("That operation is supported only in monitor mode");
1188 case PCAP_ERROR_PERM_DENIED:
1189 return ("You don't have permission to capture on that device");
1191 case PCAP_ERROR_IFACE_NOT_UP:
1192 return ("That device is not up");
1194 case PCAP_ERROR_CANTSET_TSTAMP_TYPE:
1195 return ("That device doesn't support setting the time stamp type");
1197 case PCAP_ERROR_PROMISC_PERM_DENIED:
1198 return ("You don't have permission to capture in promiscuous mode on that device");
1200 (void)snprintf(ebuf, sizeof ebuf, "Unknown error: %d", errnum);
1201 return(ebuf);
1205 * Not all systems have strerror().
1207 const char *
1208 pcap_strerror(int errnum)
1210 #ifdef HAVE_STRERROR
1211 return (strerror(errnum));
1212 #else
1213 extern int sys_nerr;
1214 extern const char *const sys_errlist[];
1215 static char ebuf[15+10+1];
1217 if ((unsigned int)errnum < sys_nerr)
1218 return ((char *)sys_errlist[errnum]);
1219 (void)snprintf(ebuf, sizeof ebuf, "Unknown error: %d", errnum);
1220 return(ebuf);
1221 #endif
1225 pcap_setfilter(pcap_t *p, struct bpf_program *fp)
1227 return (p->setfilter_op(p, fp));
1231 * Set direction flag, which controls whether we accept only incoming
1232 * packets, only outgoing packets, or both.
1233 * Note that, depending on the platform, some or all direction arguments
1234 * might not be supported.
1237 pcap_setdirection(pcap_t *p, pcap_direction_t d)
1239 if (p->setdirection_op == NULL) {
1240 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1241 "Setting direction is not implemented on this platform");
1242 return (-1);
1243 } else
1244 return (p->setdirection_op(p, d));
1248 pcap_stats(pcap_t *p, struct pcap_stat *ps)
1250 return (p->stats_op(p, ps));
1253 static int
1254 pcap_stats_dead(pcap_t *p, struct pcap_stat *ps _U_)
1256 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1257 "Statistics aren't available from a pcap_open_dead pcap_t");
1258 return (-1);
1261 #ifdef WIN32
1263 pcap_setbuff(pcap_t *p, int dim)
1265 return (p->setbuff_op(p, dim));
1268 static int
1269 pcap_setbuff_dead(pcap_t *p, int dim)
1271 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1272 "The kernel buffer size cannot be set on a pcap_open_dead pcap_t");
1273 return (-1);
1277 pcap_setmode(pcap_t *p, int mode)
1279 return (p->setmode_op(p, mode));
1282 static int
1283 pcap_setmode_dead(pcap_t *p, int mode)
1285 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1286 "impossible to set mode on a pcap_open_dead pcap_t");
1287 return (-1);
1291 pcap_setmintocopy(pcap_t *p, int size)
1293 return (p->setmintocopy_op(p, size));
1296 static int
1297 pcap_setmintocopy_dead(pcap_t *p, int size)
1299 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1300 "The mintocopy parameter cannot be set on a pcap_open_dead pcap_t");
1301 return (-1);
1303 #endif
1306 * On some platforms, we need to clean up promiscuous or monitor mode
1307 * when we close a device - and we want that to happen even if the
1308 * application just exits without explicitl closing devices.
1309 * On those platforms, we need to register a "close all the pcaps"
1310 * routine to be called when we exit, and need to maintain a list of
1311 * pcaps that need to be closed to clean up modes.
1313 * XXX - not thread-safe.
1317 * List of pcaps on which we've done something that needs to be
1318 * cleaned up.
1319 * If there are any such pcaps, we arrange to call "pcap_close_all()"
1320 * when we exit, and have it close all of them.
1322 static struct pcap *pcaps_to_close;
1325 * TRUE if we've already called "atexit()" to cause "pcap_close_all()" to
1326 * be called on exit.
1328 static int did_atexit;
1330 static void
1331 pcap_close_all(void)
1333 struct pcap *handle;
1335 while ((handle = pcaps_to_close) != NULL)
1336 pcap_close(handle);
1340 pcap_do_addexit(pcap_t *p)
1343 * If we haven't already done so, arrange to have
1344 * "pcap_close_all()" called when we exit.
1346 if (!did_atexit) {
1347 if (atexit(pcap_close_all) == -1) {
1349 * "atexit()" failed; let our caller know.
1351 strncpy(p->errbuf, "atexit failed",
1352 PCAP_ERRBUF_SIZE);
1353 return (0);
1355 did_atexit = 1;
1357 return (1);
1360 void
1361 pcap_add_to_pcaps_to_close(pcap_t *p)
1363 p->md.next = pcaps_to_close;
1364 pcaps_to_close = p;
1367 void
1368 pcap_remove_from_pcaps_to_close(pcap_t *p)
1370 pcap_t *pc, *prevpc;
1372 for (pc = pcaps_to_close, prevpc = NULL; pc != NULL;
1373 prevpc = pc, pc = pc->md.next) {
1374 if (pc == p) {
1376 * Found it. Remove it from the list.
1378 if (prevpc == NULL) {
1380 * It was at the head of the list.
1382 pcaps_to_close = pc->md.next;
1383 } else {
1385 * It was in the middle of the list.
1387 prevpc->md.next = pc->md.next;
1389 break;
1394 void
1395 pcap_cleanup_live_common(pcap_t *p)
1397 if (p->buffer != NULL) {
1398 free(p->buffer);
1399 p->buffer = NULL;
1401 if (p->dlt_list != NULL) {
1402 free(p->dlt_list);
1403 p->dlt_list = NULL;
1404 p->dlt_count = 0;
1406 if (p->tstamp_type_list != NULL) {
1407 free(p->tstamp_type_list);
1408 p->tstamp_type_list = NULL;
1409 p->tstamp_type_count = 0;
1411 pcap_freecode(&p->fcode);
1412 #if !defined(WIN32) && !defined(MSDOS)
1413 if (p->fd >= 0) {
1414 close(p->fd);
1415 p->fd = -1;
1417 p->selectable_fd = -1;
1418 p->send_fd = -1;
1419 #endif
1422 static void
1423 pcap_cleanup_dead(pcap_t *p _U_)
1425 /* Nothing to do. */
1428 pcap_t *
1429 pcap_open_dead(int linktype, int snaplen)
1431 pcap_t *p;
1433 p = malloc(sizeof(*p));
1434 if (p == NULL)
1435 return NULL;
1436 memset (p, 0, sizeof(*p));
1437 p->snapshot = snaplen;
1438 p->linktype = linktype;
1439 p->stats_op = pcap_stats_dead;
1440 #ifdef WIN32
1441 p->setbuff_op = pcap_setbuff_dead;
1442 p->setmode_op = pcap_setmode_dead;
1443 p->setmintocopy_op = pcap_setmintocopy_dead;
1444 #endif
1445 p->cleanup_op = pcap_cleanup_dead;
1446 p->activated = 1;
1447 return (p);
1451 * API compatible with WinPcap's "send a packet" routine - returns -1
1452 * on error, 0 otherwise.
1454 * XXX - what if we get a short write?
1457 pcap_sendpacket(pcap_t *p, const u_char *buf, int size)
1459 if (p->inject_op(p, buf, size) == -1)
1460 return (-1);
1461 return (0);
1465 * API compatible with OpenBSD's "send a packet" routine - returns -1 on
1466 * error, number of bytes written otherwise.
1469 pcap_inject(pcap_t *p, const void *buf, size_t size)
1471 return (p->inject_op(p, buf, size));
1474 void
1475 pcap_close(pcap_t *p)
1477 if (p->opt.source != NULL)
1478 free(p->opt.source);
1479 p->cleanup_op(p);
1480 free(p);
1484 * Given a BPF program, a pcap_pkthdr structure for a packet, and the raw
1485 * data for the packet, check whether the packet passes the filter.
1486 * Returns the return value of the filter program, which will be zero if
1487 * the packet doesn't pass and non-zero if the packet does pass.
1490 pcap_offline_filter(struct bpf_program *fp, const struct pcap_pkthdr *h,
1491 const u_char *pkt)
1493 struct bpf_insn *fcode = fp->bf_insns;
1495 if (fcode != NULL)
1496 return (bpf_filter(fcode, pkt, h->len, h->caplen));
1497 else
1498 return (0);
1502 * We make the version string static, and return a pointer to it, rather
1503 * than exporting the version string directly. On at least some UNIXes,
1504 * if you import data from a shared library into an program, the data is
1505 * bound into the program binary, so if the string in the version of the
1506 * library with which the program was linked isn't the same as the
1507 * string in the version of the library with which the program is being
1508 * run, various undesirable things may happen (warnings, the string
1509 * being the one from the version of the library with which the program
1510 * was linked, or even weirder things, such as the string being the one
1511 * from the library but being truncated).
1513 #ifdef HAVE_VERSION_H
1514 #include "version.h"
1515 #else
1516 static const char pcap_version_string[] = "libpcap version 1.x.y";
1517 #endif
1519 #ifdef WIN32
1521 * XXX - it'd be nice if we could somehow generate the WinPcap and libpcap
1522 * version numbers when building WinPcap. (It'd be nice to do so for
1523 * the packet.dll version number as well.)
1525 static const char wpcap_version_string[] = "4.0";
1526 static const char pcap_version_string_fmt[] =
1527 "WinPcap version %s, based on %s";
1528 static const char pcap_version_string_packet_dll_fmt[] =
1529 "WinPcap version %s (packet.dll version %s), based on %s";
1530 static char *full_pcap_version_string;
1532 const char *
1533 pcap_lib_version(void)
1535 char *packet_version_string;
1536 size_t full_pcap_version_string_len;
1538 if (full_pcap_version_string == NULL) {
1540 * Generate the version string.
1542 packet_version_string = PacketGetVersion();
1543 if (strcmp(wpcap_version_string, packet_version_string) == 0) {
1545 * WinPcap version string and packet.dll version
1546 * string are the same; just report the WinPcap
1547 * version.
1549 full_pcap_version_string_len =
1550 (sizeof pcap_version_string_fmt - 4) +
1551 strlen(wpcap_version_string) +
1552 strlen(pcap_version_string);
1553 full_pcap_version_string =
1554 malloc(full_pcap_version_string_len);
1555 sprintf(full_pcap_version_string,
1556 pcap_version_string_fmt, wpcap_version_string,
1557 pcap_version_string);
1558 } else {
1560 * WinPcap version string and packet.dll version
1561 * string are different; that shouldn't be the
1562 * case (the two libraries should come from the
1563 * same version of WinPcap), so we report both
1564 * versions.
1566 full_pcap_version_string_len =
1567 (sizeof pcap_version_string_packet_dll_fmt - 6) +
1568 strlen(wpcap_version_string) +
1569 strlen(packet_version_string) +
1570 strlen(pcap_version_string);
1571 full_pcap_version_string = malloc(full_pcap_version_string_len);
1573 sprintf(full_pcap_version_string,
1574 pcap_version_string_packet_dll_fmt,
1575 wpcap_version_string, packet_version_string,
1576 pcap_version_string);
1579 return (full_pcap_version_string);
1582 #elif defined(MSDOS)
1584 static char *full_pcap_version_string;
1586 const char *
1587 pcap_lib_version (void)
1589 char *packet_version_string;
1590 size_t full_pcap_version_string_len;
1591 static char dospfx[] = "DOS-";
1593 if (full_pcap_version_string == NULL) {
1595 * Generate the version string.
1597 full_pcap_version_string_len =
1598 sizeof dospfx + strlen(pcap_version_string);
1599 full_pcap_version_string =
1600 malloc(full_pcap_version_string_len);
1601 strcpy(full_pcap_version_string, dospfx);
1602 strcat(full_pcap_version_string, pcap_version_string);
1604 return (full_pcap_version_string);
1607 #else /* UN*X */
1609 const char *
1610 pcap_lib_version(void)
1612 return (pcap_version_string);
1614 #endif