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