]> The Tcpdump Group git mirrors - libpcap/blob - pcap.c
Move URL parsing and construction to pcap.c.
[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 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37
38 #include <pcap-types.h>
39 #ifdef _WIN32
40 #include <pcap-stdinc.h>
41 #else /* _WIN32 */
42 #include <sys/param.h>
43 #ifndef MSDOS
44 #include <sys/file.h>
45 #endif
46 #include <sys/ioctl.h>
47 #include <sys/socket.h>
48 #ifdef HAVE_SYS_SOCKIO_H
49 #include <sys/sockio.h>
50 #endif
51
52 struct mbuf; /* Squelch compiler warnings on some platforms for */
53 struct rtentry; /* declarations in <net/if.h> */
54 #include <net/if.h>
55 #include <netinet/in.h>
56 #endif /* _WIN32 */
57
58 #include <ctype.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #if !defined(_MSC_VER) && !defined(__BORLANDC__) && !defined(__MINGW32__)
63 #include <unistd.h>
64 #endif
65 #include <fcntl.h>
66 #include <errno.h>
67 #ifdef HAVE_LIMITS_H
68 #include <limits.h>
69 #else
70 #define INT_MAX 2147483647
71 #endif
72
73 #ifdef HAVE_OS_PROTO_H
74 #include "os-proto.h"
75 #endif
76
77 #ifdef MSDOS
78 #include "pcap-dos.h"
79 #endif
80
81 #include "pcap-int.h"
82
83 #ifdef HAVE_DAG_API
84 #include "pcap-dag.h"
85 #endif /* HAVE_DAG_API */
86
87 #ifdef HAVE_SEPTEL_API
88 #include "pcap-septel.h"
89 #endif /* HAVE_SEPTEL_API */
90
91 #ifdef HAVE_SNF_API
92 #include "pcap-snf.h"
93 #endif /* HAVE_SNF_API */
94
95 #ifdef HAVE_TC_API
96 #include "pcap-tc.h"
97 #endif /* HAVE_TC_API */
98
99 #ifdef PCAP_SUPPORT_USB
100 #include "pcap-usb-linux.h"
101 #endif
102
103 #ifdef PCAP_SUPPORT_BT
104 #include "pcap-bt-linux.h"
105 #endif
106
107 #ifdef PCAP_SUPPORT_BT_MONITOR
108 #include "pcap-bt-monitor-linux.h"
109 #endif
110
111 #ifdef PCAP_SUPPORT_NETFILTER
112 #include "pcap-netfilter-linux.h"
113 #endif
114
115 #ifdef PCAP_SUPPORT_NETMAP
116 #include "pcap-netmap.h"
117 #endif
118
119 #ifdef PCAP_SUPPORT_DBUS
120 #include "pcap-dbus.h"
121 #endif
122
123 #ifdef PCAP_SUPPORT_RDMASNIFF
124 #include "pcap-rdmasniff.h"
125 #endif
126
127 static int
128 pcap_not_initialized(pcap_t *pcap)
129 {
130 /* in case the caller doesn't check for PCAP_ERROR_NOT_ACTIVATED */
131 (void)pcap_snprintf(pcap->errbuf, sizeof(pcap->errbuf),
132 "This handle hasn't been activated yet");
133 /* this means 'not initialized' */
134 return (PCAP_ERROR_NOT_ACTIVATED);
135 }
136
137 #ifdef _WIN32
138 static void *
139 pcap_not_initialized_ptr(pcap_t *pcap)
140 {
141 (void)pcap_snprintf(pcap->errbuf, sizeof(pcap->errbuf),
142 "This handle hasn't been activated yet");
143 return (NULL);
144 }
145
146 static HANDLE
147 pcap_getevent_not_initialized(pcap_t *pcap)
148 {
149 (void)pcap_snprintf(pcap->errbuf, sizeof(pcap->errbuf),
150 "This handle hasn't been activated yet");
151 return (INVALID_HANDLE_VALUE);
152 }
153
154 static u_int
155 pcap_sendqueue_transmit_not_initialized(pcap_t *pcap, pcap_send_queue* queue, int sync)
156 {
157 (void)pcap_snprintf(pcap->errbuf, sizeof(pcap->errbuf),
158 "This handle hasn't been activated yet");
159 return (0);
160 }
161
162 static PAirpcapHandle
163 pcap_get_airpcap_handle_not_initialized(pcap_t *pcap)
164 {
165 (void)pcap_snprintf(pcap->errbuf, sizeof(pcap->errbuf),
166 "This handle hasn't been activated yet");
167 return (NULL);
168 }
169 #endif
170
171 /*
172 * Returns 1 if rfmon mode can be set on the pcap_t, 0 if it can't,
173 * a PCAP_ERROR value on an error.
174 */
175 int
176 pcap_can_set_rfmon(pcap_t *p)
177 {
178 return (p->can_set_rfmon_op(p));
179 }
180
181 /*
182 * For systems where rfmon mode is never supported.
183 */
184 static int
185 pcap_cant_set_rfmon(pcap_t *p _U_)
186 {
187 return (0);
188 }
189
190 /*
191 * Sets *tstamp_typesp to point to an array 1 or more supported time stamp
192 * types; the return value is the number of supported time stamp types.
193 * The list should be freed by a call to pcap_free_tstamp_types() when
194 * you're done with it.
195 *
196 * A return value of 0 means "you don't get a choice of time stamp type",
197 * in which case *tstamp_typesp is set to null.
198 *
199 * PCAP_ERROR is returned on error.
200 */
201 int
202 pcap_list_tstamp_types(pcap_t *p, int **tstamp_typesp)
203 {
204 if (p->tstamp_type_count == 0) {
205 /*
206 * We don't support multiple time stamp types.
207 */
208 *tstamp_typesp = NULL;
209 } else {
210 *tstamp_typesp = (int*)calloc(sizeof(**tstamp_typesp),
211 p->tstamp_type_count);
212 if (*tstamp_typesp == NULL) {
213 (void)pcap_snprintf(p->errbuf, sizeof(p->errbuf),
214 "malloc: %s", pcap_strerror(errno));
215 return (PCAP_ERROR);
216 }
217 (void)memcpy(*tstamp_typesp, p->tstamp_type_list,
218 sizeof(**tstamp_typesp) * p->tstamp_type_count);
219 }
220 return (p->tstamp_type_count);
221 }
222
223 /*
224 * In Windows, you might have a library built with one version of the
225 * C runtime library and an application built with another version of
226 * the C runtime library, which means that the library might use one
227 * version of malloc() and free() and the application might use another
228 * version of malloc() and free(). If so, that means something
229 * allocated by the library cannot be freed by the application, so we
230 * need to have a pcap_free_tstamp_types() routine to free up the list
231 * allocated by pcap_list_tstamp_types(), even though it's just a wrapper
232 * around free().
233 */
234 void
235 pcap_free_tstamp_types(int *tstamp_type_list)
236 {
237 free(tstamp_type_list);
238 }
239
240 /*
241 * Default one-shot callback; overridden for capture types where the
242 * packet data cannot be guaranteed to be available after the callback
243 * returns, so that a copy must be made.
244 */
245 void
246 pcap_oneshot(u_char *user, const struct pcap_pkthdr *h, const u_char *pkt)
247 {
248 struct oneshot_userdata *sp = (struct oneshot_userdata *)user;
249
250 *sp->hdr = *h;
251 *sp->pkt = pkt;
252 }
253
254 const u_char *
255 pcap_next(pcap_t *p, struct pcap_pkthdr *h)
256 {
257 struct oneshot_userdata s;
258 const u_char *pkt;
259
260 s.hdr = h;
261 s.pkt = &pkt;
262 s.pd = p;
263 if (pcap_dispatch(p, 1, p->oneshot_callback, (u_char *)&s) <= 0)
264 return (0);
265 return (pkt);
266 }
267
268 int
269 pcap_next_ex(pcap_t *p, struct pcap_pkthdr **pkt_header,
270 const u_char **pkt_data)
271 {
272 struct oneshot_userdata s;
273
274 s.hdr = &p->pcap_header;
275 s.pkt = pkt_data;
276 s.pd = p;
277
278 /* Saves a pointer to the packet headers */
279 *pkt_header= &p->pcap_header;
280
281 if (p->rfile != NULL) {
282 int status;
283
284 /* We are on an offline capture */
285 status = pcap_offline_read(p, 1, p->oneshot_callback,
286 (u_char *)&s);
287
288 /*
289 * Return codes for pcap_offline_read() are:
290 * - 0: EOF
291 * - -1: error
292 * - >1: OK
293 * The first one ('0') conflicts with the return code of
294 * 0 from pcap_read() meaning "no packets arrived before
295 * the timeout expired", so we map it to -2 so you can
296 * distinguish between an EOF from a savefile and a
297 * "no packets arrived before the timeout expired, try
298 * again" from a live capture.
299 */
300 if (status == 0)
301 return (-2);
302 else
303 return (status);
304 }
305
306 /*
307 * Return codes for pcap_read() are:
308 * - 0: timeout
309 * - -1: error
310 * - -2: loop was broken out of with pcap_breakloop()
311 * - >1: OK
312 * The first one ('0') conflicts with the return code of 0 from
313 * pcap_offline_read() meaning "end of file".
314 */
315 return (p->read_op(p, 1, p->oneshot_callback, (u_char *)&s));
316 }
317
318 /*
319 * Implementation of a pcap_if_list_t.
320 */
321 struct pcap_if_list {
322 pcap_if_t *beginning;
323 };
324
325 static struct capture_source_type {
326 int (*findalldevs_op)(pcap_if_list_t *, char *);
327 pcap_t *(*create_op)(const char *, char *, int *);
328 } capture_source_types[] = {
329 #ifdef HAVE_DAG_API
330 { dag_findalldevs, dag_create },
331 #endif
332 #ifdef HAVE_SEPTEL_API
333 { septel_findalldevs, septel_create },
334 #endif
335 #ifdef HAVE_SNF_API
336 { snf_findalldevs, snf_create },
337 #endif
338 #ifdef HAVE_TC_API
339 { TcFindAllDevs, TcCreate },
340 #endif
341 #ifdef PCAP_SUPPORT_BT
342 { bt_findalldevs, bt_create },
343 #endif
344 #ifdef PCAP_SUPPORT_BT_MONITOR
345 { bt_monitor_findalldevs, bt_monitor_create },
346 #endif
347 #ifdef PCAP_SUPPORT_USB
348 { usb_findalldevs, usb_create },
349 #endif
350 #ifdef PCAP_SUPPORT_NETFILTER
351 { netfilter_findalldevs, netfilter_create },
352 #endif
353 #ifdef PCAP_SUPPORT_NETMAP
354 { pcap_netmap_findalldevs, pcap_netmap_create },
355 #endif
356 #ifdef PCAP_SUPPORT_DBUS
357 { dbus_findalldevs, dbus_create },
358 #endif
359 #ifdef PCAP_SUPPORT_RDMASNIFF
360 { rdmasniff_findalldevs, rdmasniff_create },
361 #endif
362 { NULL, NULL }
363 };
364
365 /*
366 * Get a list of all capture sources that are up and that we can open.
367 * Returns -1 on error, 0 otherwise.
368 * The list, as returned through "alldevsp", may be null if no interfaces
369 * were up and could be opened.
370 */
371 int
372 pcap_findalldevs(pcap_if_t **alldevsp, char *errbuf)
373 {
374 size_t i;
375 pcap_if_list_t devlist;
376
377 /*
378 * Find all the local network interfaces on which we
379 * can capture.
380 */
381 devlist.beginning = NULL;
382 if (pcap_platform_finddevs(&devlist, errbuf) == -1) {
383 /*
384 * Failed - free all of the entries we were given
385 * before we failed.
386 */
387 if (devlist.beginning != NULL)
388 pcap_freealldevs(devlist.beginning);
389 *alldevsp = NULL;
390 return (-1);
391 }
392
393 /*
394 * Ask each of the non-local-network-interface capture
395 * source types what interfaces they have.
396 */
397 for (i = 0; capture_source_types[i].findalldevs_op != NULL; i++) {
398 if (capture_source_types[i].findalldevs_op(&devlist, errbuf) == -1) {
399 /*
400 * We had an error; free the list we've been
401 * constructing.
402 */
403 if (devlist.beginning != NULL)
404 pcap_freealldevs(devlist.beginning);
405 *alldevsp = NULL;
406 return (-1);
407 }
408 }
409
410 /*
411 * Return the first entry of the list of all devices.
412 */
413 *alldevsp = devlist.beginning;
414 return (0);
415 }
416
417 static struct sockaddr *
418 dup_sockaddr(struct sockaddr *sa, size_t sa_length)
419 {
420 struct sockaddr *newsa;
421
422 if ((newsa = malloc(sa_length)) == NULL)
423 return (NULL);
424 return (memcpy(newsa, sa, sa_length));
425 }
426
427 /*
428 * Construct a "figure of merit" for an interface, for use when sorting
429 * the list of interfaces, in which interfaces that are up are superior
430 * to interfaces that aren't up, interfaces that are up and running are
431 * superior to interfaces that are up but not running, and non-loopback
432 * interfaces that are up and running are superior to loopback interfaces,
433 * and interfaces with the same flags have a figure of merit that's higher
434 * the lower the instance number.
435 *
436 * The goal is to try to put the interfaces most likely to be useful for
437 * capture at the beginning of the list.
438 *
439 * The figure of merit, which is lower the "better" the interface is,
440 * has the uppermost bit set if the interface isn't running, the bit
441 * below that set if the interface isn't up, the bit below that set
442 * if the interface is a loopback interface, and the interface index
443 * in the 29 bits below that. (Yes, we assume u_int is 32 bits.)
444 */
445 static u_int
446 get_figure_of_merit(pcap_if_t *dev)
447 {
448 const char *cp;
449 u_int n;
450
451 if (strcmp(dev->name, "any") == 0) {
452 /*
453 * Give the "any" device an artificially high instance
454 * number, so it shows up after all other non-loopback
455 * interfaces.
456 */
457 n = 0x1FFFFFFF; /* 29 all-1 bits */
458 } else {
459 /*
460 * A number at the end of the device name string is
461 * assumed to be an instance number. Add 1 to the
462 * instance number, and use 0 for "no instance
463 * number", so we don't put "no instance number"
464 * devices and "instance 0" devices together.
465 */
466 cp = dev->name + strlen(dev->name) - 1;
467 while (cp-1 >= dev->name && *(cp-1) >= '0' && *(cp-1) <= '9')
468 cp--;
469 if (*cp >= '0' && *cp <= '9')
470 n = atoi(cp) + 1;
471 else
472 n = 0;
473 }
474 if (!(dev->flags & PCAP_IF_RUNNING))
475 n |= 0x80000000;
476 if (!(dev->flags & PCAP_IF_UP))
477 n |= 0x40000000;
478 if (dev->flags & PCAP_IF_LOOPBACK)
479 n |= 0x20000000;
480 return (n);
481 }
482
483 #ifndef _WIN32
484 /*
485 * Try to get a description for a given device.
486 * Returns a mallocated description if it could and NULL if it couldn't.
487 *
488 * XXX - on FreeBSDs that support it, should it get the sysctl named
489 * "dev.{adapter family name}.{adapter unit}.%desc" to get a description
490 * of the adapter? Note that "dev.an.0.%desc" is "Aironet PC4500/PC4800"
491 * with my Cisco 350 card, so the name isn't entirely descriptive. The
492 * "dev.an.0.%pnpinfo" has a better description, although one might argue
493 * that the problem is really a driver bug - if it can find out that it's
494 * a Cisco 340 or 350, rather than an old Aironet card, it should use
495 * that in the description.
496 *
497 * Do NetBSD, DragonflyBSD, or OpenBSD support this as well? FreeBSD
498 * and OpenBSD let you get a description, but it's not generated by the OS,
499 * it's set with another ioctl that ifconfig supports; we use that to get
500 * a description in FreeBSD and OpenBSD, but if there is no such
501 * description available, it still might be nice to get some description
502 * string based on the device type or something such as that.
503 *
504 * In OS X, the System Configuration framework can apparently return
505 * names in 10.4 and later.
506 *
507 * It also appears that freedesktop.org's HAL offers an "info.product"
508 * string, but the HAL specification says it "should not be used in any
509 * UI" and "subsystem/capability specific properties" should be used
510 * instead and, in any case, I think HAL is being deprecated in
511 * favor of other stuff such as DeviceKit. DeviceKit doesn't appear
512 * to have any obvious product information for devices, but maybe
513 * I haven't looked hard enough.
514 *
515 * Using the System Configuration framework, or HAL, or DeviceKit, or
516 * whatever, would require that libpcap applications be linked with
517 * the frameworks/libraries in question. That shouldn't be a problem
518 * for programs linking with the shared version of libpcap (unless
519 * you're running on AIX - which I think is the only UN*X that doesn't
520 * support linking a shared library with other libraries on which it
521 * depends, and having an executable linked only with the first shared
522 * library automatically pick up the other libraries when started -
523 * and using HAL or whatever). Programs linked with the static
524 * version of libpcap would have to use pcap-config with the --static
525 * flag in order to get the right linker flags in order to pick up
526 * the additional libraries/frameworks; those programs need that anyway
527 * for libpcap 1.1 and beyond on Linux, as, by default, it requires
528 * -lnl.
529 *
530 * Do any other UN*Xes, or desktop environments support getting a
531 * description?
532 */
533 static char *
534 get_if_description(const char *name)
535 {
536 #ifdef SIOCGIFDESCR
537 char *description = NULL;
538 int s;
539 struct ifreq ifrdesc;
540 #ifndef IFDESCRSIZE
541 size_t descrlen = 64;
542 #else
543 size_t descrlen = IFDESCRSIZE;
544 #endif /* IFDESCRSIZE */
545
546 /*
547 * Get the description for the interface.
548 */
549 memset(&ifrdesc, 0, sizeof ifrdesc);
550 strlcpy(ifrdesc.ifr_name, name, sizeof ifrdesc.ifr_name);
551 s = socket(AF_INET, SOCK_DGRAM, 0);
552 if (s >= 0) {
553 #ifdef __FreeBSD__
554 /*
555 * On FreeBSD, if the buffer isn't big enough for the
556 * description, the ioctl succeeds, but the description
557 * isn't copied, ifr_buffer.length is set to the description
558 * length, and ifr_buffer.buffer is set to NULL.
559 */
560 for (;;) {
561 free(description);
562 if ((description = malloc(descrlen)) != NULL) {
563 ifrdesc.ifr_buffer.buffer = description;
564 ifrdesc.ifr_buffer.length = descrlen;
565 if (ioctl(s, SIOCGIFDESCR, &ifrdesc) == 0) {
566 if (ifrdesc.ifr_buffer.buffer ==
567 description)
568 break;
569 else
570 descrlen = ifrdesc.ifr_buffer.length;
571 } else {
572 /*
573 * Failed to get interface description.
574 */
575 free(description);
576 description = NULL;
577 break;
578 }
579 } else
580 break;
581 }
582 #else /* __FreeBSD__ */
583 /*
584 * The only other OS that currently supports
585 * SIOCGIFDESCR is OpenBSD, and it has no way
586 * to get the description length - it's clamped
587 * to a maximum of IFDESCRSIZE.
588 */
589 if ((description = malloc(descrlen)) != NULL) {
590 ifrdesc.ifr_data = (caddr_t)description;
591 if (ioctl(s, SIOCGIFDESCR, &ifrdesc) != 0) {
592 /*
593 * Failed to get interface description.
594 */
595 free(description);
596 description = NULL;
597 }
598 }
599 #endif /* __FreeBSD__ */
600 close(s);
601 if (description != NULL && strlen(description) == 0) {
602 /*
603 * Description is empty, so discard it.
604 */
605 free(description);
606 description = NULL;
607 }
608 }
609
610 #ifdef __FreeBSD__
611 /*
612 * For FreeBSD, if we didn't get a description, and this is
613 * a device with a name of the form usbusN, label it as a USB
614 * bus.
615 */
616 if (description == NULL) {
617 if (strncmp(name, "usbus", 5) == 0) {
618 /*
619 * OK, it begins with "usbus".
620 */
621 long busnum;
622 char *p;
623
624 errno = 0;
625 busnum = strtol(name + 5, &p, 10);
626 if (errno == 0 && p != name + 5 && *p == '\0' &&
627 busnum >= 0 && busnum <= INT_MAX) {
628 /*
629 * OK, it's a valid number that's not
630 * bigger than INT_MAX. Construct
631 * a description from it.
632 */
633 static const char descr_prefix[] = "USB bus number ";
634 size_t descr_size;
635
636 /*
637 * Allow enough room for a 32-bit bus number.
638 * sizeof (descr_prefix) includes the
639 * terminating NUL.
640 */
641 descr_size = sizeof (descr_prefix) + 10;
642 description = malloc(descr_size);
643 if (description != NULL) {
644 pcap_snprintf(description, descr_size,
645 "%s%ld", descr_prefix, busnum);
646 }
647 }
648 }
649 }
650 #endif
651 return (description);
652 #else /* SIOCGIFDESCR */
653 return (NULL);
654 #endif /* SIOCGIFDESCR */
655 }
656
657 /*
658 * Look for a given device in the specified list of devices.
659 *
660 * If we find it, return a pointer to its entry.
661 *
662 * If we don't find it, attempt to add an entry for it, with the specified
663 * IFF_ flags and description, and, if that succeeds, return a pointer to
664 * the new entry, otherwise return NULL and set errbuf to an error message.
665 */
666 pcap_if_t *
667 find_or_add_if(pcap_if_list_t *devlistp, const char *name,
668 bpf_u_int32 if_flags, char *errbuf)
669 {
670 bpf_u_int32 pcap_flags;
671
672 /*
673 * Convert IFF_ flags to pcap flags.
674 */
675 pcap_flags = 0;
676 #ifdef IFF_LOOPBACK
677 if (if_flags & IFF_LOOPBACK)
678 pcap_flags |= PCAP_IF_LOOPBACK;
679 #else
680 /*
681 * We don't have IFF_LOOPBACK, so look at the device name to
682 * see if it looks like a loopback device.
683 */
684 if (name[0] == 'l' && name[1] == 'o' &&
685 (isdigit((unsigned char)(name[2])) || name[2] == '\0')
686 pcap_flags |= PCAP_IF_LOOPBACK;
687 #endif
688 #ifdef IFF_UP
689 if (if_flags & IFF_UP)
690 pcap_flags |= PCAP_IF_UP;
691 #endif
692 #ifdef IFF_RUNNING
693 if (if_flags & IFF_RUNNING)
694 pcap_flags |= PCAP_IF_RUNNING;
695 #endif
696
697 /*
698 * Attempt to find an entry for this device; if we don't find one,
699 * attempt to add one.
700 */
701 return (find_or_add_dev(devlistp, name, pcap_flags,
702 get_if_description(name), errbuf));
703 }
704
705 /*
706 * Look for a given device in the specified list of devices.
707 *
708 * If we find it, then, if the specified address isn't null, add it to
709 * the list of addresses for the device and return 0.
710 *
711 * If we don't find it, attempt to add an entry for it, with the specified
712 * IFF_ flags and description, and, if that succeeds, add the specified
713 * address to its list of addresses if that address is non-null, and
714 * return 0, otherwise return -1 and set errbuf to an error message.
715 *
716 * (We can get called with a null address because we might get a list
717 * of interface name/address combinations from the underlying OS, with
718 * the address being absent in some cases, rather than a list of
719 * interfaces with each interface having a list of addresses, so this
720 * call may be the only call made to add to the list, and we want to
721 * add interfaces even if they have no addresses.)
722 */
723 int
724 add_addr_to_if(pcap_if_list_t *devlistp, const char *name,
725 bpf_u_int32 if_flags,
726 struct sockaddr *addr, size_t addr_size,
727 struct sockaddr *netmask, size_t netmask_size,
728 struct sockaddr *broadaddr, size_t broadaddr_size,
729 struct sockaddr *dstaddr, size_t dstaddr_size,
730 char *errbuf)
731 {
732 pcap_if_t *curdev;
733
734 /*
735 * Check whether the device exists and, if not, add it.
736 */
737 curdev = find_or_add_if(devlistp, name, if_flags, errbuf);
738 if (curdev == NULL) {
739 /*
740 * Error - give up.
741 */
742 return (-1);
743 }
744
745 if (addr == NULL) {
746 /*
747 * There's no address to add; this entry just meant
748 * "here's a new interface".
749 */
750 return (0);
751 }
752
753 /*
754 * "curdev" is an entry for this interface, and we have an
755 * address for it; add an entry for that address to the
756 * interface's list of addresses.
757 */
758 return (add_addr_to_dev(curdev, addr, addr_size, netmask,
759 netmask_size, broadaddr, broadaddr_size, dstaddr,
760 dstaddr_size, errbuf));
761 }
762 #endif /* _WIN32 */
763
764 /*
765 * Add an entry to the list of addresses for an interface.
766 * "curdev" is the entry for that interface.
767 */
768 int
769 add_addr_to_dev(pcap_if_t *curdev,
770 struct sockaddr *addr, size_t addr_size,
771 struct sockaddr *netmask, size_t netmask_size,
772 struct sockaddr *broadaddr, size_t broadaddr_size,
773 struct sockaddr *dstaddr, size_t dstaddr_size,
774 char *errbuf)
775 {
776 pcap_addr_t *curaddr, *prevaddr, *nextaddr;
777
778 /*
779 * Allocate the new entry and fill it in.
780 */
781 curaddr = (pcap_addr_t *)malloc(sizeof(pcap_addr_t));
782 if (curaddr == NULL) {
783 (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
784 "malloc: %s", pcap_strerror(errno));
785 return (-1);
786 }
787
788 curaddr->next = NULL;
789 if (addr != NULL && addr_size != 0) {
790 curaddr->addr = (struct sockaddr *)dup_sockaddr(addr, addr_size);
791 if (curaddr->addr == NULL) {
792 (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
793 "malloc: %s", pcap_strerror(errno));
794 free(curaddr);
795 return (-1);
796 }
797 } else
798 curaddr->addr = NULL;
799
800 if (netmask != NULL && netmask_size != 0) {
801 curaddr->netmask = (struct sockaddr *)dup_sockaddr(netmask, netmask_size);
802 if (curaddr->netmask == NULL) {
803 (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
804 "malloc: %s", pcap_strerror(errno));
805 if (curaddr->addr != NULL)
806 free(curaddr->addr);
807 free(curaddr);
808 return (-1);
809 }
810 } else
811 curaddr->netmask = NULL;
812
813 if (broadaddr != NULL && broadaddr_size != 0) {
814 curaddr->broadaddr = (struct sockaddr *)dup_sockaddr(broadaddr, broadaddr_size);
815 if (curaddr->broadaddr == NULL) {
816 (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
817 "malloc: %s", pcap_strerror(errno));
818 if (curaddr->netmask != NULL)
819 free(curaddr->netmask);
820 if (curaddr->addr != NULL)
821 free(curaddr->addr);
822 free(curaddr);
823 return (-1);
824 }
825 } else
826 curaddr->broadaddr = NULL;
827
828 if (dstaddr != NULL && dstaddr_size != 0) {
829 curaddr->dstaddr = (struct sockaddr *)dup_sockaddr(dstaddr, dstaddr_size);
830 if (curaddr->dstaddr == NULL) {
831 (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
832 "malloc: %s", pcap_strerror(errno));
833 if (curaddr->broadaddr != NULL)
834 free(curaddr->broadaddr);
835 if (curaddr->netmask != NULL)
836 free(curaddr->netmask);
837 if (curaddr->addr != NULL)
838 free(curaddr->addr);
839 free(curaddr);
840 return (-1);
841 }
842 } else
843 curaddr->dstaddr = NULL;
844
845 /*
846 * Find the end of the list of addresses.
847 */
848 for (prevaddr = curdev->addresses; prevaddr != NULL; prevaddr = nextaddr) {
849 nextaddr = prevaddr->next;
850 if (nextaddr == NULL) {
851 /*
852 * This is the end of the list.
853 */
854 break;
855 }
856 }
857
858 if (prevaddr == NULL) {
859 /*
860 * The list was empty; this is the first member.
861 */
862 curdev->addresses = curaddr;
863 } else {
864 /*
865 * "prevaddr" is the last member of the list; append
866 * this member to it.
867 */
868 prevaddr->next = curaddr;
869 }
870
871 return (0);
872 }
873
874 /*
875 * Look for a given device in the specified list of devices.
876 *
877 * If we find it, return 0 and set *curdev_ret to point to it.
878 *
879 * If we don't find it, attempt to add an entry for it, with the specified
880 * flags and description, and, if that succeeds, return 0, otherwise
881 * return -1 and set errbuf to an error message.
882 */
883 pcap_if_t *
884 find_or_add_dev(pcap_if_list_t *devlistp, const char *name, bpf_u_int32 flags,
885 const char *description, char *errbuf)
886 {
887 pcap_if_t *curdev;
888
889 /*
890 * Is there already an entry in the list for this device?
891 */
892 curdev = find_dev(devlistp, name);
893 if (curdev != NULL) {
894 /*
895 * Yes, return it.
896 */
897 return (curdev);
898 }
899
900 /*
901 * No, we didn't find it. Try to add it to the list of devices.
902 */
903 return (add_dev(devlistp, name, flags, description, errbuf));
904 }
905
906 /*
907 * Look for a given device in the specified list of devices, and return
908 * the entry for it if we find it or NULL if we don't.
909 */
910 pcap_if_t *
911 find_dev(pcap_if_list_t *devlistp, const char *name)
912 {
913 pcap_if_t *curdev;
914
915 /*
916 * Is there an entry in the list for this device?
917 */
918 for (curdev = devlistp->beginning; curdev != NULL;
919 curdev = curdev->next) {
920 if (strcmp(name, curdev->name) == 0) {
921 /*
922 * We found it, so, yes, there is. No need to
923 * add it. Provide the entry we found to our
924 * caller.
925 */
926 return (curdev);
927 }
928 }
929
930 /*
931 * No.
932 */
933 return (NULL);
934 }
935
936 /*
937 * Attempt to add an entry for a device, with the specified flags
938 * and description, and, if that succeeds, return 0 and return a pointer
939 * to the new entry, otherwise return NULL and set errbuf to an error
940 * message.
941 *
942 * If we weren't given a description, try to get one.
943 */
944 pcap_if_t *
945 add_dev(pcap_if_list_t *devlistp, const char *name, bpf_u_int32 flags,
946 const char *description, char *errbuf)
947 {
948 pcap_if_t *curdev, *prevdev, *nextdev;
949 u_int this_figure_of_merit, nextdev_figure_of_merit;
950
951 curdev = malloc(sizeof(pcap_if_t));
952 if (curdev == NULL) {
953 (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
954 "malloc: %s", pcap_strerror(errno));
955 return (NULL);
956 }
957
958 /*
959 * Fill in the entry.
960 */
961 curdev->next = NULL;
962 curdev->name = strdup(name);
963 if (curdev->name == NULL) {
964 (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
965 "malloc: %s", pcap_strerror(errno));
966 free(curdev);
967 return (NULL);
968 }
969 if (description == NULL) {
970 /*
971 * We weren't handed a description for the interface.
972 */
973 curdev->description = NULL;
974 } else {
975 /*
976 * We were handed a description; make a copy.
977 */
978 curdev->description = strdup(description);
979 if (curdev->description == NULL) {
980 (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
981 "malloc: %s", pcap_strerror(errno));
982 free(curdev->name);
983 free(curdev);
984 return (NULL);
985 }
986 }
987 curdev->addresses = NULL; /* list starts out as empty */
988 curdev->flags = flags;
989
990 /*
991 * Add it to the list, in the appropriate location.
992 * First, get the "figure of merit" for this interface.
993 */
994 this_figure_of_merit = get_figure_of_merit(curdev);
995
996 /*
997 * Now look for the last interface with an figure of merit
998 * less than or equal to the new interface's figure of merit.
999 *
1000 * We start with "prevdev" being NULL, meaning we're before
1001 * the first element in the list.
1002 */
1003 prevdev = NULL;
1004 for (;;) {
1005 /*
1006 * Get the interface after this one.
1007 */
1008 if (prevdev == NULL) {
1009 /*
1010 * The next element is the first element.
1011 */
1012 nextdev = devlistp->beginning;
1013 } else
1014 nextdev = prevdev->next;
1015
1016 /*
1017 * Are we at the end of the list?
1018 */
1019 if (nextdev == NULL) {
1020 /*
1021 * Yes - we have to put the new entry after "prevdev".
1022 */
1023 break;
1024 }
1025
1026 /*
1027 * Is the new interface's figure of merit less
1028 * than the next interface's figure of merit,
1029 * meaning that the new interface is better
1030 * than the next interface?
1031 */
1032 nextdev_figure_of_merit = get_figure_of_merit(nextdev);
1033 if (this_figure_of_merit < nextdev_figure_of_merit) {
1034 /*
1035 * Yes - we should put the new entry
1036 * before "nextdev", i.e. after "prevdev".
1037 */
1038 break;
1039 }
1040
1041 prevdev = nextdev;
1042 }
1043
1044 /*
1045 * Insert before "nextdev".
1046 */
1047 curdev->next = nextdev;
1048
1049 /*
1050 * Insert after "prevdev" - unless "prevdev" is null,
1051 * in which case this is the first interface.
1052 */
1053 if (prevdev == NULL) {
1054 /*
1055 * This is the first interface. Make it
1056 * the first element in the list of devices.
1057 */
1058 devlistp->beginning = curdev;
1059 } else
1060 prevdev->next = curdev;
1061 return (curdev);
1062 }
1063
1064 /*
1065 * Free a list of interfaces.
1066 */
1067 void
1068 pcap_freealldevs(pcap_if_t *alldevs)
1069 {
1070 pcap_if_t *curdev, *nextdev;
1071 pcap_addr_t *curaddr, *nextaddr;
1072
1073 for (curdev = alldevs; curdev != NULL; curdev = nextdev) {
1074 nextdev = curdev->next;
1075
1076 /*
1077 * Free all addresses.
1078 */
1079 for (curaddr = curdev->addresses; curaddr != NULL; curaddr = nextaddr) {
1080 nextaddr = curaddr->next;
1081 if (curaddr->addr)
1082 free(curaddr->addr);
1083 if (curaddr->netmask)
1084 free(curaddr->netmask);
1085 if (curaddr->broadaddr)
1086 free(curaddr->broadaddr);
1087 if (curaddr->dstaddr)
1088 free(curaddr->dstaddr);
1089 free(curaddr);
1090 }
1091
1092 /*
1093 * Free the name string.
1094 */
1095 free(curdev->name);
1096
1097 /*
1098 * Free the description string, if any.
1099 */
1100 if (curdev->description != NULL)
1101 free(curdev->description);
1102
1103 /*
1104 * Free the interface.
1105 */
1106 free(curdev);
1107 }
1108 }
1109
1110 #ifdef HAVE_REMOTE
1111 #include "pcap-rpcap.h"
1112 #endif
1113
1114 pcap_t *
1115 pcap_create(const char *device, char *errbuf)
1116 {
1117 size_t i;
1118 int is_theirs;
1119 pcap_t *p;
1120 char *device_str;
1121
1122 /*
1123 * A null device name is equivalent to the "any" device -
1124 * which might not be supported on this platform, but
1125 * this means that you'll get a "not supported" error
1126 * rather than, say, a crash when we try to dereference
1127 * the null pointer.
1128 */
1129 if (device == NULL)
1130 device_str = strdup("any");
1131 else {
1132 #ifdef _WIN32
1133 /*
1134 * If the string appears to be little-endian UCS-2/UTF-16,
1135 * convert it to ASCII.
1136 *
1137 * XXX - to UTF-8 instead? Or report an error if any
1138 * character isn't ASCII?
1139 */
1140 if (device[0] != '\0' && device[1] == '\0') {
1141 size_t length;
1142
1143 length = wcslen((wchar_t *)device);
1144 device_str = (char *)malloc(length + 1);
1145 if (device_str == NULL) {
1146 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
1147 "malloc: %s", pcap_strerror(errno));
1148 return (NULL);
1149 }
1150
1151 pcap_snprintf(device_str, length + 1, "%ws",
1152 (const wchar_t *)device);
1153 } else
1154 #endif
1155 device_str = strdup(device);
1156 }
1157 if (device_str == NULL) {
1158 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
1159 "malloc: %s", pcap_strerror(errno));
1160 return (NULL);
1161 }
1162
1163 /*
1164 * Try each of the non-local-network-interface capture
1165 * source types until we find one that works for this
1166 * device or run out of types.
1167 */
1168 for (i = 0; capture_source_types[i].create_op != NULL; i++) {
1169 is_theirs = 0;
1170 p = capture_source_types[i].create_op(device_str, errbuf,
1171 &is_theirs);
1172 if (is_theirs) {
1173 /*
1174 * The device name refers to a device of the
1175 * type in question; either it succeeded,
1176 * in which case p refers to a pcap_t to
1177 * later activate for the device, or it
1178 * failed, in which case p is null and we
1179 * should return that to report the failure
1180 * to create.
1181 */
1182 if (p == NULL) {
1183 /*
1184 * We assume the caller filled in errbuf.
1185 */
1186 free(device_str);
1187 return (NULL);
1188 }
1189 p->opt.device = device_str;
1190 return (p);
1191 }
1192 }
1193
1194 /*
1195 * OK, try it as a regular network interface.
1196 */
1197 p = pcap_create_interface(device_str, errbuf);
1198 if (p == NULL) {
1199 /*
1200 * We assume the caller filled in errbuf.
1201 */
1202 free(device_str);
1203 return (NULL);
1204 }
1205 p->opt.device = device_str;
1206 return (p);
1207 }
1208
1209 /*
1210 * Set nonblocking mode on an unactivated pcap_t; this sets a flag
1211 * checked by pcap_activate(), which sets the mode after calling
1212 * the activate routine.
1213 */
1214 static int
1215 pcap_setnonblock_unactivated(pcap_t *p, int nonblock)
1216 {
1217 p->opt.nonblock = nonblock;
1218 return (0);
1219 }
1220
1221 static void
1222 initialize_ops(pcap_t *p)
1223 {
1224 /*
1225 * Set operation pointers for operations that only work on
1226 * an activated pcap_t to point to a routine that returns
1227 * a "this isn't activated" error.
1228 */
1229 p->read_op = (read_op_t)pcap_not_initialized;
1230 p->inject_op = (inject_op_t)pcap_not_initialized;
1231 p->setfilter_op = (setfilter_op_t)pcap_not_initialized;
1232 p->setdirection_op = (setdirection_op_t)pcap_not_initialized;
1233 p->set_datalink_op = (set_datalink_op_t)pcap_not_initialized;
1234 p->getnonblock_op = (getnonblock_op_t)pcap_not_initialized;
1235 p->stats_op = (stats_op_t)pcap_not_initialized;
1236 #ifdef _WIN32
1237 p->stats_ex_op = (stats_ex_op_t)pcap_not_initialized_ptr;
1238 p->setbuff_op = (setbuff_op_t)pcap_not_initialized;
1239 p->setmode_op = (setmode_op_t)pcap_not_initialized;
1240 p->setmintocopy_op = (setmintocopy_op_t)pcap_not_initialized;
1241 p->getevent_op = pcap_getevent_not_initialized;
1242 p->oid_get_request_op = (oid_get_request_op_t)pcap_not_initialized;
1243 p->oid_set_request_op = (oid_set_request_op_t)pcap_not_initialized;
1244 p->sendqueue_transmit_op = pcap_sendqueue_transmit_not_initialized;
1245 p->setuserbuffer_op = (setuserbuffer_op_t)pcap_not_initialized;
1246 p->live_dump_op = (live_dump_op_t)pcap_not_initialized;
1247 p->live_dump_ended_op = (live_dump_ended_op_t)pcap_not_initialized;
1248 p->get_airpcap_handle_op = pcap_get_airpcap_handle_not_initialized;
1249 #endif
1250
1251 /*
1252 * Default cleanup operation - implementations can override
1253 * this, but should call pcap_cleanup_live_common() after
1254 * doing their own additional cleanup.
1255 */
1256 p->cleanup_op = pcap_cleanup_live_common;
1257
1258 /*
1259 * In most cases, the standard one-shot callback can
1260 * be used for pcap_next()/pcap_next_ex().
1261 */
1262 p->oneshot_callback = pcap_oneshot;
1263 }
1264
1265 static pcap_t *
1266 pcap_alloc_pcap_t(char *ebuf, size_t size)
1267 {
1268 char *chunk;
1269 pcap_t *p;
1270
1271 /*
1272 * Allocate a chunk of memory big enough for a pcap_t
1273 * plus a structure following it of size "size". The
1274 * structure following it is a private data structure
1275 * for the routines that handle this pcap_t.
1276 */
1277 chunk = malloc(sizeof (pcap_t) + size);
1278 if (chunk == NULL) {
1279 pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s",
1280 pcap_strerror(errno));
1281 return (NULL);
1282 }
1283 memset(chunk, 0, sizeof (pcap_t) + size);
1284
1285 /*
1286 * Get a pointer to the pcap_t at the beginning.
1287 */
1288 p = (pcap_t *)chunk;
1289
1290 #ifndef _WIN32
1291 p->fd = -1; /* not opened yet */
1292 p->selectable_fd = -1;
1293 #endif
1294
1295 if (size == 0) {
1296 /* No private data was requested. */
1297 p->priv = NULL;
1298 } else {
1299 /*
1300 * Set the pointer to the private data; that's the structure
1301 * of size "size" following the pcap_t.
1302 */
1303 p->priv = (void *)(chunk + sizeof (pcap_t));
1304 }
1305
1306 return (p);
1307 }
1308
1309 #ifdef HAVE_REMOTE
1310 int
1311 pcap_createsrcstr(char *source, int type, const char *host, const char *port, const char *name, char *errbuf)
1312 {
1313 switch (type)
1314 {
1315 case PCAP_SRC_FILE:
1316 {
1317 strlcpy(source, PCAP_SRC_FILE_STRING, PCAP_BUF_SIZE);
1318 if ((name) && (*name))
1319 {
1320 strlcat(source, name, PCAP_BUF_SIZE);
1321 return 0;
1322 }
1323 else
1324 {
1325 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "The file name cannot be NULL.");
1326 return -1;
1327 }
1328 }
1329
1330 case PCAP_SRC_IFREMOTE:
1331 {
1332 strlcpy(source, PCAP_SRC_IF_STRING, PCAP_BUF_SIZE);
1333 if ((host) && (*host))
1334 {
1335 if ((strcspn(host, "aAbBcCdDeEfFgGhHjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ")) == strlen(host))
1336 {
1337 /* the host name does not contains alphabetic chars. So, it is a numeric address */
1338 /* In this case we have to include it between square brackets */
1339 strlcat(source, "[", PCAP_BUF_SIZE);
1340 strlcat(source, host, PCAP_BUF_SIZE);
1341 strlcat(source, "]", PCAP_BUF_SIZE);
1342 }
1343 else
1344 strlcat(source, host, PCAP_BUF_SIZE);
1345
1346 if ((port) && (*port))
1347 {
1348 strlcat(source, ":", PCAP_BUF_SIZE);
1349 strlcat(source, port, PCAP_BUF_SIZE);
1350 }
1351
1352 strlcat(source, "/", PCAP_BUF_SIZE);
1353 }
1354 else
1355 {
1356 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "The host name cannot be NULL.");
1357 return -1;
1358 }
1359
1360 if ((name) && (*name))
1361 strlcat(source, name, PCAP_BUF_SIZE);
1362
1363 return 0;
1364 }
1365
1366 case PCAP_SRC_IFLOCAL:
1367 {
1368 strlcpy(source, PCAP_SRC_IF_STRING, PCAP_BUF_SIZE);
1369
1370 if ((name) && (*name))
1371 strlcat(source, name, PCAP_BUF_SIZE);
1372
1373 return 0;
1374 }
1375
1376 default:
1377 {
1378 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "The interface type is not valid.");
1379 return -1;
1380 }
1381 }
1382 }
1383
1384 int
1385 pcap_parsesrcstr(const char *source, int *type, char *host, char *port, char *name, char *errbuf)
1386 {
1387 char *ptr;
1388 int ntoken;
1389 char tmpname[PCAP_BUF_SIZE];
1390 char tmphost[PCAP_BUF_SIZE];
1391 char tmpport[PCAP_BUF_SIZE];
1392 int tmptype;
1393
1394 /* Initialization stuff */
1395 tmpname[0] = 0;
1396 tmphost[0] = 0;
1397 tmpport[0] = 0;
1398
1399 if (host)
1400 *host = 0;
1401 if (port)
1402 *port = 0;
1403 if (name)
1404 *name = 0;
1405
1406 /* Look for a 'rpcap://' identifier */
1407 if ((ptr = strstr(source, PCAP_SRC_IF_STRING)) != NULL)
1408 {
1409 if (strlen(PCAP_SRC_IF_STRING) == strlen(source))
1410 {
1411 /* The source identifier contains only the 'rpcap://' string. */
1412 /* So, this is a local capture. */
1413 *type = PCAP_SRC_IFLOCAL;
1414 return 0;
1415 }
1416
1417 ptr += strlen(PCAP_SRC_IF_STRING);
1418
1419 if (strchr(ptr, '[')) /* This is probably a numeric address */
1420 {
1421 ntoken = sscanf(ptr, "[%[1234567890:.]]:%[^/]/%s", tmphost, tmpport, tmpname);
1422
1423 if (ntoken == 1) /* probably the port is missing */
1424 ntoken = sscanf(ptr, "[%[1234567890:.]]/%s", tmphost, tmpname);
1425
1426 tmptype = PCAP_SRC_IFREMOTE;
1427 }
1428 else
1429 {
1430 ntoken = sscanf(ptr, "%[^/:]:%[^/]/%s", tmphost, tmpport, tmpname);
1431
1432 if (ntoken == 1)
1433 {
1434 /*
1435 * This can be due to two reasons:
1436 * - we want a remote capture, but the network port is missing
1437 * - we want to do a local capture
1438 * To distinguish between the two, we look for the '/' char
1439 */
1440 if (strchr(ptr, '/'))
1441 {
1442 /* We're on a remote capture */
1443 sscanf(ptr, "%[^/]/%s", tmphost, tmpname);
1444 tmptype = PCAP_SRC_IFREMOTE;
1445 }
1446 else
1447 {
1448 /* We're on a local capture */
1449 if (*ptr)
1450 strlcpy(tmpname, ptr, PCAP_BUF_SIZE);
1451
1452 /* Clean the host name, since it is a remote capture */
1453 /* NOTE: the host name has been assigned in the previous "ntoken= sscanf(...)" line */
1454 tmphost[0] = 0;
1455
1456 tmptype = PCAP_SRC_IFLOCAL;
1457 }
1458 }
1459 else
1460 tmptype = PCAP_SRC_IFREMOTE;
1461 }
1462
1463 if (host)
1464 strlcpy(host, tmphost, PCAP_BUF_SIZE);
1465 if (port)
1466 strlcpy(port, tmpport, PCAP_BUF_SIZE);
1467 if (type)
1468 *type = tmptype;
1469
1470 if (name)
1471 {
1472 /*
1473 * If the user wants the host name, but it cannot be located into the source string, return error
1474 * However, if the user is not interested in the interface name (e.g. if we're called by
1475 * pcap_findalldevs_ex(), which does not have interface name, do not return error
1476 */
1477 if (tmpname[0])
1478 {
1479 strlcpy(name, tmpname, PCAP_BUF_SIZE);
1480 }
1481 else
1482 {
1483 if (errbuf)
1484 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "The interface name has not been specified in the source string.");
1485
1486 return -1;
1487 }
1488 }
1489
1490 return 0;
1491 }
1492
1493 /* Look for a 'file://' identifier */
1494 if ((ptr = strstr(source, PCAP_SRC_FILE_STRING)) != NULL)
1495 {
1496 ptr += strlen(PCAP_SRC_FILE_STRING);
1497 if (*ptr)
1498 {
1499 if (name)
1500 strlcpy(name, ptr, PCAP_BUF_SIZE);
1501
1502 if (type)
1503 *type = PCAP_SRC_FILE;
1504
1505 return 0;
1506 }
1507 else
1508 {
1509 if (errbuf)
1510 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "The file name has not been specified in the source string.");
1511
1512 return -1;
1513 }
1514
1515 }
1516
1517 /* Backward compatibility; the user didn't use the 'rpcap://, file://' specifiers */
1518 if ((source) && (*source))
1519 {
1520 if (name)
1521 strlcpy(name, source, PCAP_BUF_SIZE);
1522
1523 if (type)
1524 *type = PCAP_SRC_IFLOCAL;
1525
1526 return 0;
1527 }
1528 else
1529 {
1530 if (errbuf)
1531 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "The interface name has not been specified in the source string.");
1532
1533 return -1;
1534 }
1535 }
1536 #endif
1537
1538 pcap_t *
1539 pcap_create_common(char *ebuf, size_t size)
1540 {
1541 pcap_t *p;
1542
1543 p = pcap_alloc_pcap_t(ebuf, size);
1544 if (p == NULL)
1545 return (NULL);
1546
1547 /*
1548 * Default to "can't set rfmon mode"; if it's supported by
1549 * a platform, the create routine that called us can set
1550 * the op to its routine to check whether a particular
1551 * device supports it.
1552 */
1553 p->can_set_rfmon_op = pcap_cant_set_rfmon;
1554
1555 /*
1556 * If pcap_setnonblock() is called on a not-yet-activated
1557 * pcap_t, default to setting a flag and turning
1558 * on non-blocking mode when activated.
1559 */
1560 p->setnonblock_op = pcap_setnonblock_unactivated;
1561
1562 initialize_ops(p);
1563
1564 /* put in some defaults*/
1565 p->snapshot = 0; /* max packet size unspecified */
1566 p->opt.timeout = 0; /* no timeout specified */
1567 p->opt.buffer_size = 0; /* use the platform's default */
1568 p->opt.promisc = 0;
1569 #ifdef __linux__
1570 p->opt.protocol = 0;
1571 #endif
1572 p->opt.rfmon = 0;
1573 p->opt.immediate = 0;
1574 p->opt.tstamp_type = -1; /* default to not setting time stamp type */
1575 p->opt.tstamp_precision = PCAP_TSTAMP_PRECISION_MICRO;
1576
1577 /*
1578 * Start out with no BPF code generation flags set.
1579 */
1580 p->bpf_codegen_flags = 0;
1581
1582 return (p);
1583 }
1584
1585 int
1586 pcap_check_activated(pcap_t *p)
1587 {
1588 if (p->activated) {
1589 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "can't perform "
1590 " operation on activated capture");
1591 return (-1);
1592 }
1593 return (0);
1594 }
1595
1596 int
1597 pcap_set_snaplen(pcap_t *p, int snaplen)
1598 {
1599 if (pcap_check_activated(p))
1600 return (PCAP_ERROR_ACTIVATED);
1601 p->snapshot = snaplen;
1602 return (0);
1603 }
1604
1605 int
1606 pcap_set_promisc(pcap_t *p, int promisc)
1607 {
1608 if (pcap_check_activated(p))
1609 return (PCAP_ERROR_ACTIVATED);
1610 p->opt.promisc = promisc;
1611 return (0);
1612 }
1613
1614 int
1615 pcap_set_rfmon(pcap_t *p, int rfmon)
1616 {
1617 if (pcap_check_activated(p))
1618 return (PCAP_ERROR_ACTIVATED);
1619 p->opt.rfmon = rfmon;
1620 return (0);
1621 }
1622
1623 int
1624 pcap_set_timeout(pcap_t *p, int timeout_ms)
1625 {
1626 if (pcap_check_activated(p))
1627 return (PCAP_ERROR_ACTIVATED);
1628 p->opt.timeout = timeout_ms;
1629 return (0);
1630 }
1631
1632 int
1633 pcap_set_tstamp_type(pcap_t *p, int tstamp_type)
1634 {
1635 int i;
1636
1637 if (pcap_check_activated(p))
1638 return (PCAP_ERROR_ACTIVATED);
1639
1640 /*
1641 * The argument should have been u_int, but that's too late
1642 * to change now - it's an API.
1643 */
1644 if (tstamp_type < 0)
1645 return (PCAP_WARNING_TSTAMP_TYPE_NOTSUP);
1646
1647 /*
1648 * If p->tstamp_type_count is 0, we only support PCAP_TSTAMP_HOST;
1649 * the default time stamp type is PCAP_TSTAMP_HOST.
1650 */
1651 if (p->tstamp_type_count == 0) {
1652 if (tstamp_type == PCAP_TSTAMP_HOST) {
1653 p->opt.tstamp_type = tstamp_type;
1654 return (0);
1655 }
1656 } else {
1657 /*
1658 * Check whether we claim to support this type of time stamp.
1659 */
1660 for (i = 0; i < p->tstamp_type_count; i++) {
1661 if (p->tstamp_type_list[i] == (u_int)tstamp_type) {
1662 /*
1663 * Yes.
1664 */
1665 p->opt.tstamp_type = tstamp_type;
1666 return (0);
1667 }
1668 }
1669 }
1670
1671 /*
1672 * We don't support this type of time stamp.
1673 */
1674 return (PCAP_WARNING_TSTAMP_TYPE_NOTSUP);
1675 }
1676
1677 int
1678 pcap_set_immediate_mode(pcap_t *p, int immediate)
1679 {
1680 if (pcap_check_activated(p))
1681 return (PCAP_ERROR_ACTIVATED);
1682 p->opt.immediate = immediate;
1683 return (0);
1684 }
1685
1686 int
1687 pcap_set_buffer_size(pcap_t *p, int buffer_size)
1688 {
1689 if (pcap_check_activated(p))
1690 return (PCAP_ERROR_ACTIVATED);
1691 if (buffer_size <= 0) {
1692 /*
1693 * Silently ignore invalid values.
1694 */
1695 return (0);
1696 }
1697 p->opt.buffer_size = buffer_size;
1698 return (0);
1699 }
1700
1701 int
1702 pcap_set_tstamp_precision(pcap_t *p, int tstamp_precision)
1703 {
1704 int i;
1705
1706 if (pcap_check_activated(p))
1707 return (PCAP_ERROR_ACTIVATED);
1708
1709 /*
1710 * The argument should have been u_int, but that's too late
1711 * to change now - it's an API.
1712 */
1713 if (tstamp_precision < 0)
1714 return (PCAP_ERROR_TSTAMP_PRECISION_NOTSUP);
1715
1716 /*
1717 * If p->tstamp_precision_count is 0, we only support setting
1718 * the time stamp precision to microsecond precision; every
1719 * pcap module *MUST* support microsecond precision, even if
1720 * it does so by converting the native precision to
1721 * microseconds.
1722 */
1723 if (p->tstamp_precision_count == 0) {
1724 if (tstamp_precision == PCAP_TSTAMP_PRECISION_MICRO) {
1725 p->opt.tstamp_precision = tstamp_precision;
1726 return (0);
1727 }
1728 } else {
1729 /*
1730 * Check whether we claim to support this precision of
1731 * time stamp.
1732 */
1733 for (i = 0; i < p->tstamp_precision_count; i++) {
1734 if (p->tstamp_precision_list[i] == (u_int)tstamp_precision) {
1735 /*
1736 * Yes.
1737 */
1738 p->opt.tstamp_precision = tstamp_precision;
1739 return (0);
1740 }
1741 }
1742 }
1743
1744 /*
1745 * We don't support this time stamp precision.
1746 */
1747 return (PCAP_ERROR_TSTAMP_PRECISION_NOTSUP);
1748 }
1749
1750 int
1751 pcap_get_tstamp_precision(pcap_t *p)
1752 {
1753 return (p->opt.tstamp_precision);
1754 }
1755
1756 int
1757 pcap_activate(pcap_t *p)
1758 {
1759 int status;
1760
1761 /*
1762 * Catch attempts to re-activate an already-activated
1763 * pcap_t; this should, for example, catch code that
1764 * calls pcap_open_live() followed by pcap_activate(),
1765 * as some code that showed up in a Stack Exchange
1766 * question did.
1767 */
1768 if (pcap_check_activated(p))
1769 return (PCAP_ERROR_ACTIVATED);
1770 status = p->activate_op(p);
1771 if (status >= 0) {
1772 /*
1773 * If somebody requested non-blocking mode before
1774 * calling pcap_activate(), turn it on now.
1775 */
1776 if (p->opt.nonblock) {
1777 status = p->setnonblock_op(p, 1);
1778 if (status < 0) {
1779 /*
1780 * Failed. Undo everything done by
1781 * the activate operation.
1782 */
1783 p->cleanup_op(p);
1784 initialize_ops(p);
1785 return (status);
1786 }
1787 }
1788 p->activated = 1;
1789 } else {
1790 if (p->errbuf[0] == '\0') {
1791 /*
1792 * No error message supplied by the activate routine;
1793 * for the benefit of programs that don't specially
1794 * handle errors other than PCAP_ERROR, return the
1795 * error message corresponding to the status.
1796 */
1797 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "%s",
1798 pcap_statustostr(status));
1799 }
1800
1801 /*
1802 * Undo any operation pointer setting, etc. done by
1803 * the activate operation.
1804 */
1805 initialize_ops(p);
1806 }
1807 return (status);
1808 }
1809
1810 pcap_t *
1811 pcap_open_live(const char *device, int snaplen, int promisc, int to_ms, char *errbuf)
1812 {
1813 pcap_t *p;
1814 int status;
1815 #ifdef HAVE_REMOTE
1816 char host[PCAP_BUF_SIZE + 1];
1817 char port[PCAP_BUF_SIZE + 1];
1818 char name[PCAP_BUF_SIZE + 1];
1819 int srctype;
1820
1821 /*
1822 * Retrofit - we have to make older applications compatible with
1823 * remote capture.
1824 * So we're calling pcap_open_remote() from here; this is a very
1825 * dirty hack.
1826 * Obviously, we cannot exploit all the new features; for instance,
1827 * we cannot send authentication, we cannot use a UDP data connection,
1828 * and so on.
1829 */
1830 if (pcap_parsesrcstr(device, &srctype, host, port, name, errbuf))
1831 return (NULL);
1832
1833 if (srctype == PCAP_SRC_IFREMOTE) {
1834 /*
1835 * Although we already have host, port and iface, we prefer
1836 * to pass only 'device' to pcap_open_rpcap(), so that it has
1837 * to call pcap_parsesrcstr() again.
1838 * This is less optimized, but much clearer.
1839 */
1840 return (pcap_open_rpcap(device, snaplen,
1841 promisc ? PCAP_OPENFLAG_PROMISCUOUS : 0, to_ms,
1842 NULL, errbuf));
1843 }
1844 if (srctype == PCAP_SRC_FILE) {
1845 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "unknown URL scheme \"file\"");
1846 return (NULL);
1847 }
1848 if (srctype == PCAP_SRC_IFLOCAL) {
1849 /*
1850 * If it starts with rpcap://, that refers to a local device
1851 * (no host part in the URL). Remove the rpcap://, and
1852 * fall through to the regular open path.
1853 */
1854 if (strncmp(device, PCAP_SRC_IF_STRING, strlen(PCAP_SRC_IF_STRING)) == 0) {
1855 size_t len = strlen(device) - strlen(PCAP_SRC_IF_STRING) + 1;
1856
1857 if (len > 0)
1858 device += strlen(PCAP_SRC_IF_STRING);
1859 }
1860 }
1861 #endif /* HAVE_REMOTE */
1862
1863 p = pcap_create(device, errbuf);
1864 if (p == NULL)
1865 return (NULL);
1866 status = pcap_set_snaplen(p, snaplen);
1867 if (status < 0)
1868 goto fail;
1869 status = pcap_set_promisc(p, promisc);
1870 if (status < 0)
1871 goto fail;
1872 status = pcap_set_timeout(p, to_ms);
1873 if (status < 0)
1874 goto fail;
1875 /*
1876 * Mark this as opened with pcap_open_live(), so that, for
1877 * example, we show the full list of DLT_ values, rather
1878 * than just the ones that are compatible with capturing
1879 * when not in monitor mode. That allows existing applications
1880 * to work the way they used to work, but allows new applications
1881 * that know about the new open API to, for example, find out the
1882 * DLT_ values that they can select without changing whether
1883 * the adapter is in monitor mode or not.
1884 */
1885 p->oldstyle = 1;
1886 status = pcap_activate(p);
1887 if (status < 0)
1888 goto fail;
1889 return (p);
1890 fail:
1891 if (status == PCAP_ERROR)
1892 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s", device,
1893 p->errbuf);
1894 else if (status == PCAP_ERROR_NO_SUCH_DEVICE ||
1895 status == PCAP_ERROR_PERM_DENIED ||
1896 status == PCAP_ERROR_PROMISC_PERM_DENIED)
1897 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s (%s)", device,
1898 pcap_statustostr(status), p->errbuf);
1899 else
1900 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s", device,
1901 pcap_statustostr(status));
1902 pcap_close(p);
1903 return (NULL);
1904 }
1905
1906 pcap_t *
1907 pcap_open_offline_common(char *ebuf, size_t size)
1908 {
1909 pcap_t *p;
1910
1911 p = pcap_alloc_pcap_t(ebuf, size);
1912 if (p == NULL)
1913 return (NULL);
1914
1915 p->opt.tstamp_precision = PCAP_TSTAMP_PRECISION_MICRO;
1916
1917 return (p);
1918 }
1919
1920 int
1921 pcap_dispatch(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
1922 {
1923 return (p->read_op(p, cnt, callback, user));
1924 }
1925
1926 int
1927 pcap_loop(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
1928 {
1929 register int n;
1930
1931 for (;;) {
1932 if (p->rfile != NULL) {
1933 /*
1934 * 0 means EOF, so don't loop if we get 0.
1935 */
1936 n = pcap_offline_read(p, cnt, callback, user);
1937 } else {
1938 /*
1939 * XXX keep reading until we get something
1940 * (or an error occurs)
1941 */
1942 do {
1943 n = p->read_op(p, cnt, callback, user);
1944 } while (n == 0);
1945 }
1946 if (n <= 0)
1947 return (n);
1948 if (!PACKET_COUNT_IS_UNLIMITED(cnt)) {
1949 cnt -= n;
1950 if (cnt <= 0)
1951 return (0);
1952 }
1953 }
1954 }
1955
1956 /*
1957 * Force the loop in "pcap_read()" or "pcap_read_offline()" to terminate.
1958 */
1959 void
1960 pcap_breakloop(pcap_t *p)
1961 {
1962 p->break_loop = 1;
1963 }
1964
1965 int
1966 pcap_datalink(pcap_t *p)
1967 {
1968 if (!p->activated)
1969 return (PCAP_ERROR_NOT_ACTIVATED);
1970 return (p->linktype);
1971 }
1972
1973 int
1974 pcap_datalink_ext(pcap_t *p)
1975 {
1976 if (!p->activated)
1977 return (PCAP_ERROR_NOT_ACTIVATED);
1978 return (p->linktype_ext);
1979 }
1980
1981 int
1982 pcap_list_datalinks(pcap_t *p, int **dlt_buffer)
1983 {
1984 if (!p->activated)
1985 return (PCAP_ERROR_NOT_ACTIVATED);
1986 if (p->dlt_count == 0) {
1987 /*
1988 * We couldn't fetch the list of DLTs, which means
1989 * this platform doesn't support changing the
1990 * DLT for an interface. Return a list of DLTs
1991 * containing only the DLT this device supports.
1992 */
1993 *dlt_buffer = (int*)malloc(sizeof(**dlt_buffer));
1994 if (*dlt_buffer == NULL) {
1995 (void)pcap_snprintf(p->errbuf, sizeof(p->errbuf),
1996 "malloc: %s", pcap_strerror(errno));
1997 return (PCAP_ERROR);
1998 }
1999 **dlt_buffer = p->linktype;
2000 return (1);
2001 } else {
2002 *dlt_buffer = (int*)calloc(sizeof(**dlt_buffer), p->dlt_count);
2003 if (*dlt_buffer == NULL) {
2004 (void)pcap_snprintf(p->errbuf, sizeof(p->errbuf),
2005 "malloc: %s", pcap_strerror(errno));
2006 return (PCAP_ERROR);
2007 }
2008 (void)memcpy(*dlt_buffer, p->dlt_list,
2009 sizeof(**dlt_buffer) * p->dlt_count);
2010 return (p->dlt_count);
2011 }
2012 }
2013
2014 /*
2015 * In Windows, you might have a library built with one version of the
2016 * C runtime library and an application built with another version of
2017 * the C runtime library, which means that the library might use one
2018 * version of malloc() and free() and the application might use another
2019 * version of malloc() and free(). If so, that means something
2020 * allocated by the library cannot be freed by the application, so we
2021 * need to have a pcap_free_datalinks() routine to free up the list
2022 * allocated by pcap_list_datalinks(), even though it's just a wrapper
2023 * around free().
2024 */
2025 void
2026 pcap_free_datalinks(int *dlt_list)
2027 {
2028 free(dlt_list);
2029 }
2030
2031 int
2032 pcap_set_datalink(pcap_t *p, int dlt)
2033 {
2034 int i;
2035 const char *dlt_name;
2036
2037 if (dlt < 0)
2038 goto unsupported;
2039
2040 if (p->dlt_count == 0 || p->set_datalink_op == NULL) {
2041 /*
2042 * We couldn't fetch the list of DLTs, or we don't
2043 * have a "set datalink" operation, which means
2044 * this platform doesn't support changing the
2045 * DLT for an interface. Check whether the new
2046 * DLT is the one this interface supports.
2047 */
2048 if (p->linktype != dlt)
2049 goto unsupported;
2050
2051 /*
2052 * It is, so there's nothing we need to do here.
2053 */
2054 return (0);
2055 }
2056 for (i = 0; i < p->dlt_count; i++)
2057 if (p->dlt_list[i] == (u_int)dlt)
2058 break;
2059 if (i >= p->dlt_count)
2060 goto unsupported;
2061 if (p->dlt_count == 2 && p->dlt_list[0] == DLT_EN10MB &&
2062 dlt == DLT_DOCSIS) {
2063 /*
2064 * This is presumably an Ethernet device, as the first
2065 * link-layer type it offers is DLT_EN10MB, and the only
2066 * other type it offers is DLT_DOCSIS. That means that
2067 * we can't tell the driver to supply DOCSIS link-layer
2068 * headers - we're just pretending that's what we're
2069 * getting, as, presumably, we're capturing on a dedicated
2070 * link to a Cisco Cable Modem Termination System, and
2071 * it's putting raw DOCSIS frames on the wire inside low-level
2072 * Ethernet framing.
2073 */
2074 p->linktype = dlt;
2075 return (0);
2076 }
2077 if (p->set_datalink_op(p, dlt) == -1)
2078 return (-1);
2079 p->linktype = dlt;
2080 return (0);
2081
2082 unsupported:
2083 dlt_name = pcap_datalink_val_to_name(dlt);
2084 if (dlt_name != NULL) {
2085 (void) pcap_snprintf(p->errbuf, sizeof(p->errbuf),
2086 "%s is not one of the DLTs supported by this device",
2087 dlt_name);
2088 } else {
2089 (void) pcap_snprintf(p->errbuf, sizeof(p->errbuf),
2090 "DLT %d is not one of the DLTs supported by this device",
2091 dlt);
2092 }
2093 return (-1);
2094 }
2095
2096 /*
2097 * This array is designed for mapping upper and lower case letter
2098 * together for a case independent comparison. The mappings are
2099 * based upon ascii character sequences.
2100 */
2101 static const u_char charmap[] = {
2102 (u_char)'\000', (u_char)'\001', (u_char)'\002', (u_char)'\003',
2103 (u_char)'\004', (u_char)'\005', (u_char)'\006', (u_char)'\007',
2104 (u_char)'\010', (u_char)'\011', (u_char)'\012', (u_char)'\013',
2105 (u_char)'\014', (u_char)'\015', (u_char)'\016', (u_char)'\017',
2106 (u_char)'\020', (u_char)'\021', (u_char)'\022', (u_char)'\023',
2107 (u_char)'\024', (u_char)'\025', (u_char)'\026', (u_char)'\027',
2108 (u_char)'\030', (u_char)'\031', (u_char)'\032', (u_char)'\033',
2109 (u_char)'\034', (u_char)'\035', (u_char)'\036', (u_char)'\037',
2110 (u_char)'\040', (u_char)'\041', (u_char)'\042', (u_char)'\043',
2111 (u_char)'\044', (u_char)'\045', (u_char)'\046', (u_char)'\047',
2112 (u_char)'\050', (u_char)'\051', (u_char)'\052', (u_char)'\053',
2113 (u_char)'\054', (u_char)'\055', (u_char)'\056', (u_char)'\057',
2114 (u_char)'\060', (u_char)'\061', (u_char)'\062', (u_char)'\063',
2115 (u_char)'\064', (u_char)'\065', (u_char)'\066', (u_char)'\067',
2116 (u_char)'\070', (u_char)'\071', (u_char)'\072', (u_char)'\073',
2117 (u_char)'\074', (u_char)'\075', (u_char)'\076', (u_char)'\077',
2118 (u_char)'\100', (u_char)'\141', (u_char)'\142', (u_char)'\143',
2119 (u_char)'\144', (u_char)'\145', (u_char)'\146', (u_char)'\147',
2120 (u_char)'\150', (u_char)'\151', (u_char)'\152', (u_char)'\153',
2121 (u_char)'\154', (u_char)'\155', (u_char)'\156', (u_char)'\157',
2122 (u_char)'\160', (u_char)'\161', (u_char)'\162', (u_char)'\163',
2123 (u_char)'\164', (u_char)'\165', (u_char)'\166', (u_char)'\167',
2124 (u_char)'\170', (u_char)'\171', (u_char)'\172', (u_char)'\133',
2125 (u_char)'\134', (u_char)'\135', (u_char)'\136', (u_char)'\137',
2126 (u_char)'\140', (u_char)'\141', (u_char)'\142', (u_char)'\143',
2127 (u_char)'\144', (u_char)'\145', (u_char)'\146', (u_char)'\147',
2128 (u_char)'\150', (u_char)'\151', (u_char)'\152', (u_char)'\153',
2129 (u_char)'\154', (u_char)'\155', (u_char)'\156', (u_char)'\157',
2130 (u_char)'\160', (u_char)'\161', (u_char)'\162', (u_char)'\163',
2131 (u_char)'\164', (u_char)'\165', (u_char)'\166', (u_char)'\167',
2132 (u_char)'\170', (u_char)'\171', (u_char)'\172', (u_char)'\173',
2133 (u_char)'\174', (u_char)'\175', (u_char)'\176', (u_char)'\177',
2134 (u_char)'\200', (u_char)'\201', (u_char)'\202', (u_char)'\203',
2135 (u_char)'\204', (u_char)'\205', (u_char)'\206', (u_char)'\207',
2136 (u_char)'\210', (u_char)'\211', (u_char)'\212', (u_char)'\213',
2137 (u_char)'\214', (u_char)'\215', (u_char)'\216', (u_char)'\217',
2138 (u_char)'\220', (u_char)'\221', (u_char)'\222', (u_char)'\223',
2139 (u_char)'\224', (u_char)'\225', (u_char)'\226', (u_char)'\227',
2140 (u_char)'\230', (u_char)'\231', (u_char)'\232', (u_char)'\233',
2141 (u_char)'\234', (u_char)'\235', (u_char)'\236', (u_char)'\237',
2142 (u_char)'\240', (u_char)'\241', (u_char)'\242', (u_char)'\243',
2143 (u_char)'\244', (u_char)'\245', (u_char)'\246', (u_char)'\247',
2144 (u_char)'\250', (u_char)'\251', (u_char)'\252', (u_char)'\253',
2145 (u_char)'\254', (u_char)'\255', (u_char)'\256', (u_char)'\257',
2146 (u_char)'\260', (u_char)'\261', (u_char)'\262', (u_char)'\263',
2147 (u_char)'\264', (u_char)'\265', (u_char)'\266', (u_char)'\267',
2148 (u_char)'\270', (u_char)'\271', (u_char)'\272', (u_char)'\273',
2149 (u_char)'\274', (u_char)'\275', (u_char)'\276', (u_char)'\277',
2150 (u_char)'\300', (u_char)'\341', (u_char)'\342', (u_char)'\343',
2151 (u_char)'\344', (u_char)'\345', (u_char)'\346', (u_char)'\347',
2152 (u_char)'\350', (u_char)'\351', (u_char)'\352', (u_char)'\353',
2153 (u_char)'\354', (u_char)'\355', (u_char)'\356', (u_char)'\357',
2154 (u_char)'\360', (u_char)'\361', (u_char)'\362', (u_char)'\363',
2155 (u_char)'\364', (u_char)'\365', (u_char)'\366', (u_char)'\367',
2156 (u_char)'\370', (u_char)'\371', (u_char)'\372', (u_char)'\333',
2157 (u_char)'\334', (u_char)'\335', (u_char)'\336', (u_char)'\337',
2158 (u_char)'\340', (u_char)'\341', (u_char)'\342', (u_char)'\343',
2159 (u_char)'\344', (u_char)'\345', (u_char)'\346', (u_char)'\347',
2160 (u_char)'\350', (u_char)'\351', (u_char)'\352', (u_char)'\353',
2161 (u_char)'\354', (u_char)'\355', (u_char)'\356', (u_char)'\357',
2162 (u_char)'\360', (u_char)'\361', (u_char)'\362', (u_char)'\363',
2163 (u_char)'\364', (u_char)'\365', (u_char)'\366', (u_char)'\367',
2164 (u_char)'\370', (u_char)'\371', (u_char)'\372', (u_char)'\373',
2165 (u_char)'\374', (u_char)'\375', (u_char)'\376', (u_char)'\377',
2166 };
2167
2168 int
2169 pcap_strcasecmp(const char *s1, const char *s2)
2170 {
2171 register const u_char *cm = charmap,
2172 *us1 = (const u_char *)s1,
2173 *us2 = (const u_char *)s2;
2174
2175 while (cm[*us1] == cm[*us2++])
2176 if (*us1++ == '\0')
2177 return(0);
2178 return (cm[*us1] - cm[*--us2]);
2179 }
2180
2181 struct dlt_choice {
2182 const char *name;
2183 const char *description;
2184 int dlt;
2185 };
2186
2187 #define DLT_CHOICE(code, description) { #code, description, DLT_ ## code }
2188 #define DLT_CHOICE_SENTINEL { NULL, NULL, 0 }
2189
2190 static struct dlt_choice dlt_choices[] = {
2191 DLT_CHOICE(NULL, "BSD loopback"),
2192 DLT_CHOICE(EN10MB, "Ethernet"),
2193 DLT_CHOICE(IEEE802, "Token ring"),
2194 DLT_CHOICE(ARCNET, "BSD ARCNET"),
2195 DLT_CHOICE(SLIP, "SLIP"),
2196 DLT_CHOICE(PPP, "PPP"),
2197 DLT_CHOICE(FDDI, "FDDI"),
2198 DLT_CHOICE(ATM_RFC1483, "RFC 1483 LLC-encapsulated ATM"),
2199 DLT_CHOICE(RAW, "Raw IP"),
2200 DLT_CHOICE(SLIP_BSDOS, "BSD/OS SLIP"),
2201 DLT_CHOICE(PPP_BSDOS, "BSD/OS PPP"),
2202 DLT_CHOICE(ATM_CLIP, "Linux Classical IP-over-ATM"),
2203 DLT_CHOICE(PPP_SERIAL, "PPP over serial"),
2204 DLT_CHOICE(PPP_ETHER, "PPPoE"),
2205 DLT_CHOICE(SYMANTEC_FIREWALL, "Symantec Firewall"),
2206 DLT_CHOICE(C_HDLC, "Cisco HDLC"),
2207 DLT_CHOICE(IEEE802_11, "802.11"),
2208 DLT_CHOICE(FRELAY, "Frame Relay"),
2209 DLT_CHOICE(LOOP, "OpenBSD loopback"),
2210 DLT_CHOICE(ENC, "OpenBSD encapsulated IP"),
2211 DLT_CHOICE(LINUX_SLL, "Linux cooked"),
2212 DLT_CHOICE(LTALK, "Localtalk"),
2213 DLT_CHOICE(PFLOG, "OpenBSD pflog file"),
2214 DLT_CHOICE(PFSYNC, "Packet filter state syncing"),
2215 DLT_CHOICE(PRISM_HEADER, "802.11 plus Prism header"),
2216 DLT_CHOICE(IP_OVER_FC, "RFC 2625 IP-over-Fibre Channel"),
2217 DLT_CHOICE(SUNATM, "Sun raw ATM"),
2218 DLT_CHOICE(IEEE802_11_RADIO, "802.11 plus radiotap header"),
2219 DLT_CHOICE(ARCNET_LINUX, "Linux ARCNET"),
2220 DLT_CHOICE(JUNIPER_MLPPP, "Juniper Multi-Link PPP"),
2221 DLT_CHOICE(JUNIPER_MLFR, "Juniper Multi-Link Frame Relay"),
2222 DLT_CHOICE(JUNIPER_ES, "Juniper Encryption Services PIC"),
2223 DLT_CHOICE(JUNIPER_GGSN, "Juniper GGSN PIC"),
2224 DLT_CHOICE(JUNIPER_MFR, "Juniper FRF.16 Frame Relay"),
2225 DLT_CHOICE(JUNIPER_ATM2, "Juniper ATM2 PIC"),
2226 DLT_CHOICE(JUNIPER_SERVICES, "Juniper Advanced Services PIC"),
2227 DLT_CHOICE(JUNIPER_ATM1, "Juniper ATM1 PIC"),
2228 DLT_CHOICE(APPLE_IP_OVER_IEEE1394, "Apple IP-over-IEEE 1394"),
2229 DLT_CHOICE(MTP2_WITH_PHDR, "SS7 MTP2 with Pseudo-header"),
2230 DLT_CHOICE(MTP2, "SS7 MTP2"),
2231 DLT_CHOICE(MTP3, "SS7 MTP3"),
2232 DLT_CHOICE(SCCP, "SS7 SCCP"),
2233 DLT_CHOICE(DOCSIS, "DOCSIS"),
2234 DLT_CHOICE(LINUX_IRDA, "Linux IrDA"),
2235 DLT_CHOICE(IEEE802_11_RADIO_AVS, "802.11 plus AVS radio information header"),
2236 DLT_CHOICE(JUNIPER_MONITOR, "Juniper Passive Monitor PIC"),
2237 DLT_CHOICE(BACNET_MS_TP, "BACnet MS/TP"),
2238 DLT_CHOICE(PPP_PPPD, "PPP for pppd, with direction flag"),
2239 DLT_CHOICE(JUNIPER_PPPOE, "Juniper PPPoE"),
2240 DLT_CHOICE(JUNIPER_PPPOE_ATM, "Juniper PPPoE/ATM"),
2241 DLT_CHOICE(GPRS_LLC, "GPRS LLC"),
2242 DLT_CHOICE(GPF_T, "GPF-T"),
2243 DLT_CHOICE(GPF_F, "GPF-F"),
2244 DLT_CHOICE(JUNIPER_PIC_PEER, "Juniper PIC Peer"),
2245 DLT_CHOICE(ERF_ETH, "Ethernet with Endace ERF header"),
2246 DLT_CHOICE(ERF_POS, "Packet-over-SONET with Endace ERF header"),
2247 DLT_CHOICE(LINUX_LAPD, "Linux vISDN LAPD"),
2248 DLT_CHOICE(JUNIPER_ETHER, "Juniper Ethernet"),
2249 DLT_CHOICE(JUNIPER_PPP, "Juniper PPP"),
2250 DLT_CHOICE(JUNIPER_FRELAY, "Juniper Frame Relay"),
2251 DLT_CHOICE(JUNIPER_CHDLC, "Juniper C-HDLC"),
2252 DLT_CHOICE(MFR, "FRF.16 Frame Relay"),
2253 DLT_CHOICE(JUNIPER_VP, "Juniper Voice PIC"),
2254 DLT_CHOICE(A429, "Arinc 429"),
2255 DLT_CHOICE(A653_ICM, "Arinc 653 Interpartition Communication"),
2256 DLT_CHOICE(USB_FREEBSD, "USB with FreeBSD header"),
2257 DLT_CHOICE(BLUETOOTH_HCI_H4, "Bluetooth HCI UART transport layer"),
2258 DLT_CHOICE(IEEE802_16_MAC_CPS, "IEEE 802.16 MAC Common Part Sublayer"),
2259 DLT_CHOICE(USB_LINUX, "USB with Linux header"),
2260 DLT_CHOICE(CAN20B, "Controller Area Network (CAN) v. 2.0B"),
2261 DLT_CHOICE(IEEE802_15_4_LINUX, "IEEE 802.15.4 with Linux padding"),
2262 DLT_CHOICE(PPI, "Per-Packet Information"),
2263 DLT_CHOICE(IEEE802_16_MAC_CPS_RADIO, "IEEE 802.16 MAC Common Part Sublayer plus radiotap header"),
2264 DLT_CHOICE(JUNIPER_ISM, "Juniper Integrated Service Module"),
2265 DLT_CHOICE(IEEE802_15_4, "IEEE 802.15.4 with FCS"),
2266 DLT_CHOICE(SITA, "SITA pseudo-header"),
2267 DLT_CHOICE(ERF, "Endace ERF header"),
2268 DLT_CHOICE(RAIF1, "Ethernet with u10 Networks pseudo-header"),
2269 DLT_CHOICE(IPMB, "IPMB"),
2270 DLT_CHOICE(JUNIPER_ST, "Juniper Secure Tunnel"),
2271 DLT_CHOICE(BLUETOOTH_HCI_H4_WITH_PHDR, "Bluetooth HCI UART transport layer plus pseudo-header"),
2272 DLT_CHOICE(AX25_KISS, "AX.25 with KISS header"),
2273 DLT_CHOICE(IEEE802_15_4_NONASK_PHY, "IEEE 802.15.4 with non-ASK PHY data"),
2274 DLT_CHOICE(MPLS, "MPLS with label as link-layer header"),
2275 DLT_CHOICE(LINUX_EVDEV, "Linux evdev events"),
2276 DLT_CHOICE(USB_LINUX_MMAPPED, "USB with padded Linux header"),
2277 DLT_CHOICE(DECT, "DECT"),
2278 DLT_CHOICE(AOS, "AOS Space Data Link protocol"),
2279 DLT_CHOICE(WIHART, "Wireless HART"),
2280 DLT_CHOICE(FC_2, "Fibre Channel FC-2"),
2281 DLT_CHOICE(FC_2_WITH_FRAME_DELIMS, "Fibre Channel FC-2 with frame delimiters"),
2282 DLT_CHOICE(IPNET, "Solaris ipnet"),
2283 DLT_CHOICE(CAN_SOCKETCAN, "CAN-bus with SocketCAN headers"),
2284 DLT_CHOICE(IPV4, "Raw IPv4"),
2285 DLT_CHOICE(IPV6, "Raw IPv6"),
2286 DLT_CHOICE(IEEE802_15_4_NOFCS, "IEEE 802.15.4 without FCS"),
2287 DLT_CHOICE(DBUS, "D-Bus"),
2288 DLT_CHOICE(JUNIPER_VS, "Juniper Virtual Server"),
2289 DLT_CHOICE(JUNIPER_SRX_E2E, "Juniper SRX E2E"),
2290 DLT_CHOICE(JUNIPER_FIBRECHANNEL, "Juniper Fibre Channel"),
2291 DLT_CHOICE(DVB_CI, "DVB-CI"),
2292 DLT_CHOICE(MUX27010, "MUX27010"),
2293 DLT_CHOICE(STANAG_5066_D_PDU, "STANAG 5066 D_PDUs"),
2294 DLT_CHOICE(JUNIPER_ATM_CEMIC, "Juniper ATM CEMIC"),
2295 DLT_CHOICE(NFLOG, "Linux netfilter log messages"),
2296 DLT_CHOICE(NETANALYZER, "Ethernet with Hilscher netANALYZER pseudo-header"),
2297 DLT_CHOICE(NETANALYZER_TRANSPARENT, "Ethernet with Hilscher netANALYZER pseudo-header and with preamble and SFD"),
2298 DLT_CHOICE(IPOIB, "RFC 4391 IP-over-Infiniband"),
2299 DLT_CHOICE(MPEG_2_TS, "MPEG-2 transport stream"),
2300 DLT_CHOICE(NG40, "ng40 protocol tester Iub/Iur"),
2301 DLT_CHOICE(NFC_LLCP, "NFC LLCP PDUs with pseudo-header"),
2302 DLT_CHOICE(INFINIBAND, "InfiniBand"),
2303 DLT_CHOICE(SCTP, "SCTP"),
2304 DLT_CHOICE(USBPCAP, "USB with USBPcap header"),
2305 DLT_CHOICE(RTAC_SERIAL, "Schweitzer Engineering Laboratories RTAC packets"),
2306 DLT_CHOICE(BLUETOOTH_LE_LL, "Bluetooth Low Energy air interface"),
2307 DLT_CHOICE(NETLINK, "Linux netlink"),
2308 DLT_CHOICE(BLUETOOTH_LINUX_MONITOR, "Bluetooth Linux Monitor"),
2309 DLT_CHOICE(BLUETOOTH_BREDR_BB, "Bluetooth Basic Rate/Enhanced Data Rate baseband packets"),
2310 DLT_CHOICE(BLUETOOTH_LE_LL_WITH_PHDR, "Bluetooth Low Energy air interface with pseudo-header"),
2311 DLT_CHOICE(PROFIBUS_DL, "PROFIBUS data link layer"),
2312 DLT_CHOICE(PKTAP, "Apple DLT_PKTAP"),
2313 DLT_CHOICE(EPON, "Ethernet with 802.3 Clause 65 EPON preamble"),
2314 DLT_CHOICE(IPMI_HPM_2, "IPMI trace packets"),
2315 DLT_CHOICE(ZWAVE_R1_R2, "Z-Wave RF profile R1 and R2 packets"),
2316 DLT_CHOICE(ZWAVE_R3, "Z-Wave RF profile R3 packets"),
2317 DLT_CHOICE(WATTSTOPPER_DLM, "WattStopper Digital Lighting Management (DLM) and Legrand Nitoo Open protocol"),
2318 DLT_CHOICE(ISO_14443, "ISO 14443 messages"),
2319 DLT_CHOICE(RDS, "IEC 62106 Radio Data System groups"),
2320 DLT_CHOICE(USB_DARWIN, "USB with Darwin header"),
2321 DLT_CHOICE(OPENFLOW, "OpenBSD DLT_OPENFLOW"),
2322 DLT_CHOICE(SDLC, "IBM SDLC frames"),
2323 DLT_CHOICE(TI_LLN_SNIFFER, "TI LLN sniffer frames"),
2324 DLT_CHOICE(VSOCK, "Linux vsock"),
2325 DLT_CHOICE(NORDIC_BLE, "Nordic Semiconductor Bluetooth LE sniffer frames"),
2326 DLT_CHOICE_SENTINEL
2327 };
2328
2329 int
2330 pcap_datalink_name_to_val(const char *name)
2331 {
2332 int i;
2333
2334 for (i = 0; dlt_choices[i].name != NULL; i++) {
2335 if (pcap_strcasecmp(dlt_choices[i].name, name) == 0)
2336 return (dlt_choices[i].dlt);
2337 }
2338 return (-1);
2339 }
2340
2341 const char *
2342 pcap_datalink_val_to_name(int dlt)
2343 {
2344 int i;
2345
2346 for (i = 0; dlt_choices[i].name != NULL; i++) {
2347 if (dlt_choices[i].dlt == dlt)
2348 return (dlt_choices[i].name);
2349 }
2350 return (NULL);
2351 }
2352
2353 const char *
2354 pcap_datalink_val_to_description(int dlt)
2355 {
2356 int i;
2357
2358 for (i = 0; dlt_choices[i].name != NULL; i++) {
2359 if (dlt_choices[i].dlt == dlt)
2360 return (dlt_choices[i].description);
2361 }
2362 return (NULL);
2363 }
2364
2365 struct tstamp_type_choice {
2366 const char *name;
2367 const char *description;
2368 int type;
2369 };
2370
2371 static struct tstamp_type_choice tstamp_type_choices[] = {
2372 { "host", "Host", PCAP_TSTAMP_HOST },
2373 { "host_lowprec", "Host, low precision", PCAP_TSTAMP_HOST_LOWPREC },
2374 { "host_hiprec", "Host, high precision", PCAP_TSTAMP_HOST_HIPREC },
2375 { "adapter", "Adapter", PCAP_TSTAMP_ADAPTER },
2376 { "adapter_unsynced", "Adapter, not synced with system time", PCAP_TSTAMP_ADAPTER_UNSYNCED },
2377 { NULL, NULL, 0 }
2378 };
2379
2380 int
2381 pcap_tstamp_type_name_to_val(const char *name)
2382 {
2383 int i;
2384
2385 for (i = 0; tstamp_type_choices[i].name != NULL; i++) {
2386 if (pcap_strcasecmp(tstamp_type_choices[i].name, name) == 0)
2387 return (tstamp_type_choices[i].type);
2388 }
2389 return (PCAP_ERROR);
2390 }
2391
2392 const char *
2393 pcap_tstamp_type_val_to_name(int tstamp_type)
2394 {
2395 int i;
2396
2397 for (i = 0; tstamp_type_choices[i].name != NULL; i++) {
2398 if (tstamp_type_choices[i].type == tstamp_type)
2399 return (tstamp_type_choices[i].name);
2400 }
2401 return (NULL);
2402 }
2403
2404 const char *
2405 pcap_tstamp_type_val_to_description(int tstamp_type)
2406 {
2407 int i;
2408
2409 for (i = 0; tstamp_type_choices[i].name != NULL; i++) {
2410 if (tstamp_type_choices[i].type == tstamp_type)
2411 return (tstamp_type_choices[i].description);
2412 }
2413 return (NULL);
2414 }
2415
2416 int
2417 pcap_snapshot(pcap_t *p)
2418 {
2419 if (!p->activated)
2420 return (PCAP_ERROR_NOT_ACTIVATED);
2421 return (p->snapshot);
2422 }
2423
2424 int
2425 pcap_is_swapped(pcap_t *p)
2426 {
2427 if (!p->activated)
2428 return (PCAP_ERROR_NOT_ACTIVATED);
2429 return (p->swapped);
2430 }
2431
2432 int
2433 pcap_major_version(pcap_t *p)
2434 {
2435 if (!p->activated)
2436 return (PCAP_ERROR_NOT_ACTIVATED);
2437 return (p->version_major);
2438 }
2439
2440 int
2441 pcap_minor_version(pcap_t *p)
2442 {
2443 if (!p->activated)
2444 return (PCAP_ERROR_NOT_ACTIVATED);
2445 return (p->version_minor);
2446 }
2447
2448 int
2449 pcap_bufsize(pcap_t *p)
2450 {
2451 if (!p->activated)
2452 return (PCAP_ERROR_NOT_ACTIVATED);
2453 return (p->bufsize);
2454 }
2455
2456 FILE *
2457 pcap_file(pcap_t *p)
2458 {
2459 return (p->rfile);
2460 }
2461
2462 int
2463 pcap_fileno(pcap_t *p)
2464 {
2465 #ifndef _WIN32
2466 return (p->fd);
2467 #else
2468 if (p->adapter != NULL)
2469 return ((int)(DWORD)p->adapter->hFile);
2470 else
2471 return (PCAP_ERROR);
2472 #endif
2473 }
2474
2475 #if !defined(_WIN32) && !defined(MSDOS)
2476 int
2477 pcap_get_selectable_fd(pcap_t *p)
2478 {
2479 return (p->selectable_fd);
2480 }
2481 #endif
2482
2483 void
2484 pcap_perror(pcap_t *p, const char *prefix)
2485 {
2486 fprintf(stderr, "%s: %s\n", prefix, p->errbuf);
2487 }
2488
2489 char *
2490 pcap_geterr(pcap_t *p)
2491 {
2492 return (p->errbuf);
2493 }
2494
2495 int
2496 pcap_getnonblock(pcap_t *p, char *errbuf)
2497 {
2498 int ret;
2499
2500 ret = p->getnonblock_op(p);
2501 if (ret == -1) {
2502 /*
2503 * The get nonblock operation sets p->errbuf; this
2504 * function *shouldn't* have had a separate errbuf
2505 * argument, as it didn't need one, but I goofed
2506 * when adding it.
2507 *
2508 * We copy the error message to errbuf, so callers
2509 * can find it in either place.
2510 */
2511 strlcpy(errbuf, p->errbuf, PCAP_ERRBUF_SIZE);
2512 }
2513 return (ret);
2514 }
2515
2516 /*
2517 * Get the current non-blocking mode setting, under the assumption that
2518 * it's just the standard POSIX non-blocking flag.
2519 */
2520 #if !defined(_WIN32) && !defined(MSDOS)
2521 int
2522 pcap_getnonblock_fd(pcap_t *p)
2523 {
2524 int fdflags;
2525
2526 fdflags = fcntl(p->fd, F_GETFL, 0);
2527 if (fdflags == -1) {
2528 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s",
2529 pcap_strerror(errno));
2530 return (-1);
2531 }
2532 if (fdflags & O_NONBLOCK)
2533 return (1);
2534 else
2535 return (0);
2536 }
2537 #endif
2538
2539 int
2540 pcap_setnonblock(pcap_t *p, int nonblock, char *errbuf)
2541 {
2542 int ret;
2543
2544 ret = p->setnonblock_op(p, nonblock);
2545 if (ret == -1) {
2546 /*
2547 * The set nonblock operation sets p->errbuf; this
2548 * function *shouldn't* have had a separate errbuf
2549 * argument, as it didn't need one, but I goofed
2550 * when adding it.
2551 *
2552 * We copy the error message to errbuf, so callers
2553 * can find it in either place.
2554 */
2555 strlcpy(errbuf, p->errbuf, PCAP_ERRBUF_SIZE);
2556 }
2557 return (ret);
2558 }
2559
2560 #if !defined(_WIN32) && !defined(MSDOS)
2561 /*
2562 * Set non-blocking mode, under the assumption that it's just the
2563 * standard POSIX non-blocking flag. (This can be called by the
2564 * per-platform non-blocking-mode routine if that routine also
2565 * needs to do some additional work.)
2566 */
2567 int
2568 pcap_setnonblock_fd(pcap_t *p, int nonblock)
2569 {
2570 int fdflags;
2571
2572 fdflags = fcntl(p->fd, F_GETFL, 0);
2573 if (fdflags == -1) {
2574 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s",
2575 pcap_strerror(errno));
2576 return (-1);
2577 }
2578 if (nonblock)
2579 fdflags |= O_NONBLOCK;
2580 else
2581 fdflags &= ~O_NONBLOCK;
2582 if (fcntl(p->fd, F_SETFL, fdflags) == -1) {
2583 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_SETFL: %s",
2584 pcap_strerror(errno));
2585 return (-1);
2586 }
2587 return (0);
2588 }
2589 #endif
2590
2591 #ifdef _WIN32
2592 /*
2593 * Generate a string for a Win32-specific error (i.e. an error generated when
2594 * calling a Win32 API).
2595 * For errors occurred during standard C calls, we still use pcap_strerror()
2596 */
2597 void
2598 pcap_win32_err_to_str(DWORD error, char *errbuf)
2599 {
2600 size_t errlen;
2601 char *p;
2602
2603 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, 0, errbuf,
2604 PCAP_ERRBUF_SIZE, NULL);
2605
2606 /*
2607 * "FormatMessage()" "helpfully" sticks CR/LF at the end of the
2608 * message. Get rid of it.
2609 */
2610 errlen = strlen(errbuf);
2611 if (errlen >= 2) {
2612 errbuf[errlen - 1] = '\0';
2613 errbuf[errlen - 2] = '\0';
2614 }
2615 p = strchr(errbuf, '\0');
2616 pcap_snprintf (p, PCAP_ERRBUF_SIZE+1-(p-errbuf), " (%lu)", error);
2617 }
2618 #endif
2619
2620 /*
2621 * Generate error strings for PCAP_ERROR_ and PCAP_WARNING_ values.
2622 */
2623 const char *
2624 pcap_statustostr(int errnum)
2625 {
2626 static char ebuf[15+10+1];
2627
2628 switch (errnum) {
2629
2630 case PCAP_WARNING:
2631 return("Generic warning");
2632
2633 case PCAP_WARNING_TSTAMP_TYPE_NOTSUP:
2634 return ("That type of time stamp is not supported by that device");
2635
2636 case PCAP_WARNING_PROMISC_NOTSUP:
2637 return ("That device doesn't support promiscuous mode");
2638
2639 case PCAP_ERROR:
2640 return("Generic error");
2641
2642 case PCAP_ERROR_BREAK:
2643 return("Loop terminated by pcap_breakloop");
2644
2645 case PCAP_ERROR_NOT_ACTIVATED:
2646 return("The pcap_t has not been activated");
2647
2648 case PCAP_ERROR_ACTIVATED:
2649 return ("The setting can't be changed after the pcap_t is activated");
2650
2651 case PCAP_ERROR_NO_SUCH_DEVICE:
2652 return ("No such device exists");
2653
2654 case PCAP_ERROR_RFMON_NOTSUP:
2655 return ("That device doesn't support monitor mode");
2656
2657 case PCAP_ERROR_NOT_RFMON:
2658 return ("That operation is supported only in monitor mode");
2659
2660 case PCAP_ERROR_PERM_DENIED:
2661 return ("You don't have permission to capture on that device");
2662
2663 case PCAP_ERROR_IFACE_NOT_UP:
2664 return ("That device is not up");
2665
2666 case PCAP_ERROR_CANTSET_TSTAMP_TYPE:
2667 return ("That device doesn't support setting the time stamp type");
2668
2669 case PCAP_ERROR_PROMISC_PERM_DENIED:
2670 return ("You don't have permission to capture in promiscuous mode on that device");
2671
2672 case PCAP_ERROR_TSTAMP_PRECISION_NOTSUP:
2673 return ("That device doesn't support that time stamp precision");
2674 }
2675 (void)pcap_snprintf(ebuf, sizeof ebuf, "Unknown error: %d", errnum);
2676 return(ebuf);
2677 }
2678
2679 /*
2680 * Not all systems have strerror().
2681 */
2682 const char *
2683 pcap_strerror(int errnum)
2684 {
2685 #ifdef HAVE_STRERROR
2686 #ifdef _WIN32
2687 static char errbuf[PCAP_ERRBUF_SIZE];
2688 errno_t err = strerror_s(errbuf, PCAP_ERRBUF_SIZE, errnum);
2689
2690 if (err != 0) /* err = 0 if successful */
2691 strlcpy(errbuf, "strerror_s() error", PCAP_ERRBUF_SIZE);
2692 return (errbuf);
2693 #else
2694 return (strerror(errnum));
2695 #endif /* _WIN32 */
2696 #else
2697 extern int sys_nerr;
2698 extern const char *const sys_errlist[];
2699 static char errbuf[PCAP_ERRBUF_SIZE];
2700
2701 if ((unsigned int)errnum < sys_nerr)
2702 return ((char *)sys_errlist[errnum]);
2703 (void)pcap_snprintf(errbuf, sizeof errbuf, "Unknown error: %d", errnum);
2704 return (errbuf);
2705 #endif
2706 }
2707
2708 int
2709 pcap_setfilter(pcap_t *p, struct bpf_program *fp)
2710 {
2711 return (p->setfilter_op(p, fp));
2712 }
2713
2714 /*
2715 * Set direction flag, which controls whether we accept only incoming
2716 * packets, only outgoing packets, or both.
2717 * Note that, depending on the platform, some or all direction arguments
2718 * might not be supported.
2719 */
2720 int
2721 pcap_setdirection(pcap_t *p, pcap_direction_t d)
2722 {
2723 if (p->setdirection_op == NULL) {
2724 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
2725 "Setting direction is not implemented on this platform");
2726 return (-1);
2727 } else
2728 return (p->setdirection_op(p, d));
2729 }
2730
2731 int
2732 pcap_stats(pcap_t *p, struct pcap_stat *ps)
2733 {
2734 return (p->stats_op(p, ps));
2735 }
2736
2737 static int
2738 pcap_stats_dead(pcap_t *p, struct pcap_stat *ps _U_)
2739 {
2740 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
2741 "Statistics aren't available from a pcap_open_dead pcap_t");
2742 return (-1);
2743 }
2744
2745 #ifdef _WIN32
2746 struct pcap_stat *
2747 pcap_stats_ex(pcap_t *p, int *pcap_stat_size)
2748 {
2749 return (p->stats_ex_op(p, pcap_stat_size));
2750 }
2751
2752 int
2753 pcap_setbuff(pcap_t *p, int dim)
2754 {
2755 return (p->setbuff_op(p, dim));
2756 }
2757
2758 static int
2759 pcap_setbuff_dead(pcap_t *p, int dim)
2760 {
2761 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
2762 "The kernel buffer size cannot be set on a pcap_open_dead pcap_t");
2763 return (-1);
2764 }
2765
2766 int
2767 pcap_setmode(pcap_t *p, int mode)
2768 {
2769 return (p->setmode_op(p, mode));
2770 }
2771
2772 static int
2773 pcap_setmode_dead(pcap_t *p, int mode)
2774 {
2775 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
2776 "impossible to set mode on a pcap_open_dead pcap_t");
2777 return (-1);
2778 }
2779
2780 int
2781 pcap_setmintocopy(pcap_t *p, int size)
2782 {
2783 return (p->setmintocopy_op(p, size));
2784 }
2785
2786 static int
2787 pcap_setmintocopy_dead(pcap_t *p, int size)
2788 {
2789 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
2790 "The mintocopy parameter cannot be set on a pcap_open_dead pcap_t");
2791 return (-1);
2792 }
2793
2794 HANDLE
2795 pcap_getevent(pcap_t *p)
2796 {
2797 return (p->getevent_op(p));
2798 }
2799
2800 static HANDLE
2801 pcap_getevent_dead(pcap_t *p)
2802 {
2803 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
2804 "A pcap_open_dead pcap_t has no event handle");
2805 return (INVALID_HANDLE_VALUE);
2806 }
2807
2808 int
2809 pcap_oid_get_request(pcap_t *p, bpf_u_int32 oid, void *data, size_t *lenp)
2810 {
2811 return (p->oid_get_request_op(p, oid, data, lenp));
2812 }
2813
2814 static int
2815 pcap_oid_get_request_dead(pcap_t *p, bpf_u_int32 oid _U_, void *data _U_,
2816 size_t *lenp _U_)
2817 {
2818 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
2819 "An OID get request cannot be performed on a pcap_open_dead pcap_t");
2820 return (PCAP_ERROR);
2821 }
2822
2823 int
2824 pcap_oid_set_request(pcap_t *p, bpf_u_int32 oid, const void *data, size_t *lenp)
2825 {
2826 return (p->oid_set_request_op(p, oid, data, lenp));
2827 }
2828
2829 static int
2830 pcap_oid_set_request_dead(pcap_t *p, bpf_u_int32 oid _U_, const void *data _U_,
2831 size_t *lenp _U_)
2832 {
2833 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
2834 "An OID set request cannot be performed on a pcap_open_dead pcap_t");
2835 return (PCAP_ERROR);
2836 }
2837
2838 pcap_send_queue *
2839 pcap_sendqueue_alloc(u_int memsize)
2840 {
2841 pcap_send_queue *tqueue;
2842
2843 /* Allocate the queue */
2844 tqueue = (pcap_send_queue *)malloc(sizeof(pcap_send_queue));
2845 if (tqueue == NULL){
2846 return (NULL);
2847 }
2848
2849 /* Allocate the buffer */
2850 tqueue->buffer = (char *)malloc(memsize);
2851 if (tqueue->buffer == NULL) {
2852 free(tqueue);
2853 return (NULL);
2854 }
2855
2856 tqueue->maxlen = memsize;
2857 tqueue->len = 0;
2858
2859 return (tqueue);
2860 }
2861
2862 void
2863 pcap_sendqueue_destroy(pcap_send_queue *queue)
2864 {
2865 free(queue->buffer);
2866 free(queue);
2867 }
2868
2869 int
2870 pcap_sendqueue_queue(pcap_send_queue *queue, const struct pcap_pkthdr *pkt_header, const u_char *pkt_data)
2871 {
2872 if (queue->len + sizeof(struct pcap_pkthdr) + pkt_header->caplen > queue->maxlen){
2873 return (-1);
2874 }
2875
2876 /* Copy the pcap_pkthdr header*/
2877 memcpy(queue->buffer + queue->len, pkt_header, sizeof(struct pcap_pkthdr));
2878 queue->len += sizeof(struct pcap_pkthdr);
2879
2880 /* copy the packet */
2881 memcpy(queue->buffer + queue->len, pkt_data, pkt_header->caplen);
2882 queue->len += pkt_header->caplen;
2883
2884 return (0);
2885 }
2886
2887 u_int
2888 pcap_sendqueue_transmit(pcap_t *p, pcap_send_queue *queue, int sync)
2889 {
2890 return (p->sendqueue_transmit_op(p, queue, sync));
2891 }
2892
2893 static u_int
2894 pcap_sendqueue_transmit_dead(pcap_t *p, pcap_send_queue *queue, int sync)
2895 {
2896 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
2897 "Packets cannot be transmitted on a pcap_open_dead pcap_t");
2898 return (0);
2899 }
2900
2901 int
2902 pcap_setuserbuffer(pcap_t *p, int size)
2903 {
2904 return (p->setuserbuffer_op(p, size));
2905 }
2906
2907 static int
2908 pcap_setuserbuffer_dead(pcap_t *p, int size)
2909 {
2910 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
2911 "The user buffer cannot be set on a pcap_open_dead pcap_t");
2912 return (-1);
2913 }
2914
2915 int
2916 pcap_live_dump(pcap_t *p, char *filename, int maxsize, int maxpacks)
2917 {
2918 return (p->live_dump_op(p, filename, maxsize, maxpacks));
2919 }
2920
2921 static int
2922 pcap_live_dump_dead(pcap_t *p, char *filename, int maxsize, int maxpacks)
2923 {
2924 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
2925 "Live packet dumping cannot be performed on a pcap_open_dead pcap_t");
2926 return (-1);
2927 }
2928
2929 int
2930 pcap_live_dump_ended(pcap_t *p, int sync)
2931 {
2932 return (p->live_dump_ended_op(p, sync));
2933 }
2934
2935 static int
2936 pcap_live_dump_ended_dead(pcap_t *p, int sync)
2937 {
2938 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
2939 "Live packet dumping cannot be performed on a pcap_open_dead pcap_t");
2940 return (-1);
2941 }
2942
2943 PAirpcapHandle
2944 pcap_get_airpcap_handle(pcap_t *p)
2945 {
2946 PAirpcapHandle handle;
2947
2948 handle = p->get_airpcap_handle_op(p);
2949 if (handle == NULL) {
2950 (void)pcap_snprintf(p->errbuf, sizeof(p->errbuf),
2951 "This isn't an AirPcap device");
2952 }
2953 return (handle);
2954 }
2955
2956 static PAirpcapHandle
2957 pcap_get_airpcap_handle_dead(pcap_t *p)
2958 {
2959 return (NULL);
2960 }
2961 #endif
2962
2963 /*
2964 * On some platforms, we need to clean up promiscuous or monitor mode
2965 * when we close a device - and we want that to happen even if the
2966 * application just exits without explicitl closing devices.
2967 * On those platforms, we need to register a "close all the pcaps"
2968 * routine to be called when we exit, and need to maintain a list of
2969 * pcaps that need to be closed to clean up modes.
2970 *
2971 * XXX - not thread-safe.
2972 */
2973
2974 /*
2975 * List of pcaps on which we've done something that needs to be
2976 * cleaned up.
2977 * If there are any such pcaps, we arrange to call "pcap_close_all()"
2978 * when we exit, and have it close all of them.
2979 */
2980 static struct pcap *pcaps_to_close;
2981
2982 /*
2983 * TRUE if we've already called "atexit()" to cause "pcap_close_all()" to
2984 * be called on exit.
2985 */
2986 static int did_atexit;
2987
2988 static void
2989 pcap_close_all(void)
2990 {
2991 struct pcap *handle;
2992
2993 while ((handle = pcaps_to_close) != NULL)
2994 pcap_close(handle);
2995 }
2996
2997 int
2998 pcap_do_addexit(pcap_t *p)
2999 {
3000 /*
3001 * If we haven't already done so, arrange to have
3002 * "pcap_close_all()" called when we exit.
3003 */
3004 if (!did_atexit) {
3005 if (atexit(pcap_close_all) != 0) {
3006 /*
3007 * "atexit()" failed; let our caller know.
3008 */
3009 strlcpy(p->errbuf, "atexit failed", PCAP_ERRBUF_SIZE);
3010 return (0);
3011 }
3012 did_atexit = 1;
3013 }
3014 return (1);
3015 }
3016
3017 void
3018 pcap_add_to_pcaps_to_close(pcap_t *p)
3019 {
3020 p->next = pcaps_to_close;
3021 pcaps_to_close = p;
3022 }
3023
3024 void
3025 pcap_remove_from_pcaps_to_close(pcap_t *p)
3026 {
3027 pcap_t *pc, *prevpc;
3028
3029 for (pc = pcaps_to_close, prevpc = NULL; pc != NULL;
3030 prevpc = pc, pc = pc->next) {
3031 if (pc == p) {
3032 /*
3033 * Found it. Remove it from the list.
3034 */
3035 if (prevpc == NULL) {
3036 /*
3037 * It was at the head of the list.
3038 */
3039 pcaps_to_close = pc->next;
3040 } else {
3041 /*
3042 * It was in the middle of the list.
3043 */
3044 prevpc->next = pc->next;
3045 }
3046 break;
3047 }
3048 }
3049 }
3050
3051 void
3052 pcap_cleanup_live_common(pcap_t *p)
3053 {
3054 if (p->buffer != NULL) {
3055 free(p->buffer);
3056 p->buffer = NULL;
3057 }
3058 if (p->dlt_list != NULL) {
3059 free(p->dlt_list);
3060 p->dlt_list = NULL;
3061 p->dlt_count = 0;
3062 }
3063 if (p->tstamp_type_list != NULL) {
3064 free(p->tstamp_type_list);
3065 p->tstamp_type_list = NULL;
3066 p->tstamp_type_count = 0;
3067 }
3068 if (p->tstamp_precision_list != NULL) {
3069 free(p->tstamp_precision_list);
3070 p->tstamp_precision_list = NULL;
3071 p->tstamp_precision_count = 0;
3072 }
3073 pcap_freecode(&p->fcode);
3074 #if !defined(_WIN32) && !defined(MSDOS)
3075 if (p->fd >= 0) {
3076 close(p->fd);
3077 p->fd = -1;
3078 }
3079 p->selectable_fd = -1;
3080 #endif
3081 }
3082
3083 static void
3084 pcap_cleanup_dead(pcap_t *p _U_)
3085 {
3086 /* Nothing to do. */
3087 }
3088
3089 pcap_t *
3090 pcap_open_dead_with_tstamp_precision(int linktype, int snaplen, u_int precision)
3091 {
3092 pcap_t *p;
3093
3094 switch (precision) {
3095
3096 case PCAP_TSTAMP_PRECISION_MICRO:
3097 case PCAP_TSTAMP_PRECISION_NANO:
3098 break;
3099
3100 default:
3101 return NULL;
3102 }
3103 p = malloc(sizeof(*p));
3104 if (p == NULL)
3105 return NULL;
3106 memset (p, 0, sizeof(*p));
3107 p->snapshot = snaplen;
3108 p->linktype = linktype;
3109 p->opt.tstamp_precision = precision;
3110 p->stats_op = pcap_stats_dead;
3111 #ifdef _WIN32
3112 p->stats_ex_op = (stats_ex_op_t)pcap_not_initialized_ptr;
3113 p->setbuff_op = pcap_setbuff_dead;
3114 p->setmode_op = pcap_setmode_dead;
3115 p->setmintocopy_op = pcap_setmintocopy_dead;
3116 p->getevent_op = pcap_getevent_dead;
3117 p->oid_get_request_op = pcap_oid_get_request_dead;
3118 p->oid_set_request_op = pcap_oid_set_request_dead;
3119 p->sendqueue_transmit_op = pcap_sendqueue_transmit_dead;
3120 p->setuserbuffer_op = pcap_setuserbuffer_dead;
3121 p->live_dump_op = pcap_live_dump_dead;
3122 p->live_dump_ended_op = pcap_live_dump_ended_dead;
3123 p->get_airpcap_handle_op = pcap_get_airpcap_handle_dead;
3124 #endif
3125 p->cleanup_op = pcap_cleanup_dead;
3126
3127 /*
3128 * A "dead" pcap_t never requires special BPF code generation.
3129 */
3130 p->bpf_codegen_flags = 0;
3131
3132 p->activated = 1;
3133 return (p);
3134 }
3135
3136 pcap_t *
3137 pcap_open_dead(int linktype, int snaplen)
3138 {
3139 return (pcap_open_dead_with_tstamp_precision(linktype, snaplen,
3140 PCAP_TSTAMP_PRECISION_MICRO));
3141 }
3142
3143 /*
3144 * API compatible with WinPcap's "send a packet" routine - returns -1
3145 * on error, 0 otherwise.
3146 *
3147 * XXX - what if we get a short write?
3148 */
3149 int
3150 pcap_sendpacket(pcap_t *p, const u_char *buf, int size)
3151 {
3152 if (p->inject_op(p, buf, size) == -1)
3153 return (-1);
3154 return (0);
3155 }
3156
3157 /*
3158 * API compatible with OpenBSD's "send a packet" routine - returns -1 on
3159 * error, number of bytes written otherwise.
3160 */
3161 int
3162 pcap_inject(pcap_t *p, const void *buf, size_t size)
3163 {
3164 return (p->inject_op(p, buf, size));
3165 }
3166
3167 void
3168 pcap_close(pcap_t *p)
3169 {
3170 if (p->opt.device != NULL)
3171 free(p->opt.device);
3172 p->cleanup_op(p);
3173 free(p);
3174 }
3175
3176 /*
3177 * Given a BPF program, a pcap_pkthdr structure for a packet, and the raw
3178 * data for the packet, check whether the packet passes the filter.
3179 * Returns the return value of the filter program, which will be zero if
3180 * the packet doesn't pass and non-zero if the packet does pass.
3181 */
3182 int
3183 pcap_offline_filter(const struct bpf_program *fp, const struct pcap_pkthdr *h,
3184 const u_char *pkt)
3185 {
3186 const struct bpf_insn *fcode = fp->bf_insns;
3187
3188 if (fcode != NULL)
3189 return (bpf_filter(fcode, pkt, h->len, h->caplen));
3190 else
3191 return (0);
3192 }
3193
3194 #include "pcap_version.h"
3195
3196 static const char *pcap_lib_version_string;
3197
3198 #ifdef _WIN32
3199
3200 #ifdef HAVE_VERSION_H
3201 /*
3202 * libpcap being built for Windows, as part of a WinPcap/Npcap source
3203 * tree. Include version.h from that source tree to get the WinPcap/Npcap
3204 * version.
3205 *
3206 * XXX - it'd be nice if we could somehow generate the WinPcap version number
3207 * when building WinPcap. (It'd be nice to do so for the packet.dll version
3208 * number as well.)
3209 */
3210 #include "../../version.h"
3211
3212 static const char wpcap_version_string[] = WINPCAP_VER_STRING;
3213 static const char pcap_version_string_fmt[] =
3214 WINPCAP_PRODUCT_NAME " version %s, based on %s";
3215 static const char pcap_version_string_packet_dll_fmt[] =
3216 WINPCAP_PRODUCT_NAME " version %s (packet.dll version %s), based on %s";
3217
3218 const char *
3219 pcap_lib_version(void)
3220 {
3221 char *packet_version_string;
3222 size_t full_pcap_version_string_len;
3223 char *full_pcap_version_string;
3224
3225 if (pcap_lib_version_string == NULL) {
3226 /*
3227 * Generate the version string.
3228 */
3229 packet_version_string = PacketGetVersion();
3230 if (strcmp(wpcap_version_string, packet_version_string) == 0) {
3231 /*
3232 * WinPcap version string and packet.dll version
3233 * string are the same; just report the WinPcap
3234 * version.
3235 */
3236 full_pcap_version_string_len =
3237 (sizeof pcap_version_string_fmt - 4) +
3238 strlen(wpcap_version_string) +
3239 strlen(pcap_version_string);
3240 full_pcap_version_string =
3241 malloc(full_pcap_version_string_len);
3242 if (full_pcap_version_string == NULL)
3243 return (NULL);
3244 pcap_snprintf(full_pcap_version_string,
3245 full_pcap_version_string_len,
3246 pcap_version_string_fmt,
3247 wpcap_version_string,
3248 pcap_version_string);
3249 } else {
3250 /*
3251 * WinPcap version string and packet.dll version
3252 * string are different; that shouldn't be the
3253 * case (the two libraries should come from the
3254 * same version of WinPcap), so we report both
3255 * versions.
3256 */
3257 full_pcap_version_string_len =
3258 (sizeof pcap_version_string_packet_dll_fmt - 6) +
3259 strlen(wpcap_version_string) +
3260 strlen(packet_version_string) +
3261 strlen(pcap_version_string);
3262 full_pcap_version_string = malloc(full_pcap_version_string_len);
3263 if (full_pcap_version_string == NULL)
3264 return (NULL);
3265 pcap_snprintf(full_pcap_version_string,
3266 full_pcap_version_string_len,
3267 pcap_version_string_packet_dll_fmt,
3268 wpcap_version_string,
3269 packet_version_string,
3270 pcap_version_string);
3271 }
3272 pcap_lib_version_string = full_pcap_version_string;
3273 }
3274 return (pcap_lib_version_string);
3275 }
3276
3277 #else /* HAVE_VERSION_H */
3278
3279 /*
3280 * libpcap being built for Windows, not as part of a WinPcap/Npcap source
3281 * tree.
3282 */
3283 static const char pcap_version_string_packet_dll_fmt[] =
3284 "%s (packet.dll version %s)";
3285 const char *
3286 pcap_lib_version(void)
3287 {
3288 char *packet_version_string;
3289 size_t full_pcap_version_string_len;
3290 char *full_pcap_version_string;
3291
3292 if (pcap_lib_version_string == NULL) {
3293 /*
3294 * Generate the version string. Report the packet.dll
3295 * version.
3296 */
3297 packet_version_string = PacketGetVersion();
3298 full_pcap_version_string_len =
3299 (sizeof pcap_version_string_packet_dll_fmt - 4) +
3300 strlen(pcap_version_string) +
3301 strlen(packet_version_string);
3302 full_pcap_version_string = malloc(full_pcap_version_string_len);
3303 if (full_pcap_version_string == NULL)
3304 return (NULL);
3305 pcap_snprintf(full_pcap_version_string,
3306 full_pcap_version_string_len,
3307 pcap_version_string_packet_dll_fmt,
3308 pcap_version_string,
3309 packet_version_string);
3310 pcap_lib_version_string = full_pcap_version_string;
3311 }
3312 return (pcap_lib_version_string);
3313 }
3314
3315 #endif /* HAVE_VERSION_H */
3316
3317 #elif defined(MSDOS)
3318
3319 const char *
3320 pcap_lib_version(void)
3321 {
3322 char *packet_version_string;
3323 size_t full_pcap_version_string_len;
3324 char *full_pcap_version_string;
3325 static char dospfx[] = "DOS-";
3326
3327 if (pcap_lib_version_string == NULL) {
3328 /*
3329 * Generate the version string.
3330 */
3331 full_pcap_version_string_len =
3332 sizeof dospfx + strlen(pcap_version_string);
3333 full_pcap_version_string =
3334 malloc(full_pcap_version_string_len);
3335 if (full_pcap_version_string == NULL)
3336 return (NULL);
3337 strcpy(full_pcap_version_string, dospfx);
3338 strcat(full_pcap_version_string, pcap_version_string);
3339 pcap_lib_version_string = full_pcap_version_string;
3340 }
3341 return (pcap_lib_version_string);
3342 }
3343
3344 #else /* UN*X */
3345
3346 const char *
3347 pcap_lib_version(void)
3348 {
3349 const char *platform_version_string;
3350 size_t full_pcap_version_string_len;
3351 char *full_pcap_version_string;
3352
3353 if (pcap_lib_version_string == NULL) {
3354 /*
3355 * Generate the version string.
3356 * Get any platform-specific information.
3357 *
3358 * XXX - what about all the local capture modules other
3359 * that the "native interface" one? That could make
3360 * the version string really long.
3361 */
3362 platform_version_string = pcap_platform_lib_version();
3363 if (platform_version_string == NULL) {
3364 /*
3365 * No platform-specific information.
3366 */
3367 pcap_lib_version_string = pcap_version_string;
3368 } else {
3369 /*
3370 * Add on the platform-specific information.
3371 */
3372 full_pcap_version_string_len =
3373 strlen(pcap_version_string) + 2 + strlen(platform_version_string) + 1 + 1;
3374 full_pcap_version_string =
3375 malloc(full_pcap_version_string_len);
3376 if (full_pcap_version_string == NULL)
3377 return (NULL);
3378 pcap_snprintf(full_pcap_version_string,
3379 full_pcap_version_string_len,
3380 "%s (%s)", pcap_version_string,
3381 platform_version_string);
3382 pcap_lib_version_string = full_pcap_version_string;
3383 }
3384 }
3385 return (pcap_lib_version_string);
3386 }
3387 #endif
3388
3389 #ifdef YYDEBUG
3390 /*
3391 * Set the internal "debug printout" flag for the filter expression parser.
3392 * The code to print that stuff is present only if YYDEBUG is defined, so
3393 * the flag, and the routine to set it, are defined only if YYDEBUG is
3394 * defined.
3395 *
3396 * This is intended for libpcap developers, not for general use.
3397 * If you want to set these in a program, you'll have to declare this
3398 * routine yourself, with the appropriate DLL import attribute on Windows;
3399 * it's not declared in any header file, and won't be declared in any
3400 * header file provided by libpcap.
3401 */
3402 PCAP_API void pcap_set_parser_debug(int value);
3403
3404 PCAP_API_DEF void
3405 pcap_set_parser_debug(int value)
3406 {
3407 extern int pcap_debug;
3408
3409 pcap_debug = value;
3410 }
3411 #endif
3412
3413 #ifdef BDEBUG
3414 /*
3415 * Set the internal "debug printout" flag for the filter expression optimizer.
3416 * The code to print that stuff is present only if BDEBUG is defined, so
3417 * the flag, and the routine to set it, are defined only if BDEBUG is
3418 * defined.
3419 *
3420 * This is intended for libpcap developers, not for general use.
3421 * If you want to set these in a program, you'll have to declare this
3422 * routine yourself, with the appropriate DLL import attribute on Windows;
3423 * it's not declared in any header file, and won't be declared in any
3424 * header file provided by libpcap.
3425 */
3426 PCAP_API void pcap_set_optimizer_debug(int value);
3427
3428 PCAP_API_DEF void
3429 pcap_set_optimizer_debug(int value)
3430 {
3431 extern int pcap_optimizer_debug;
3432
3433 pcap_optimizer_debug = value;
3434 }
3435 #endif