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