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