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