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