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