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