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