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