("" < 3.4) always evaluates to true, which unconditionally
[dragonfly.git] / contrib / libpcap-0.8.3 / pcap.c
blobfbaceb5a06c57e0928245398451b115bed36b59c
1 /*
2 * Copyright (c) 1993, 1994, 1995, 1996, 1997, 1998
3 * The Regents of the University of California. All rights reserved.
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.
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.
34 #ifndef lint
35 static const char rcsid[] _U_ =
36 "@(#) $Header: /tcpdump/master/libpcap/pcap.c,v 1.63.2.9 2004/03/25 22:40:52 guy Exp $ (LBL)";
37 #endif
39 #ifdef HAVE_CONFIG_H
40 #include "config.h"
41 #endif
43 #ifdef WIN32
44 #include <pcap-stdinc.h>
45 #else /* WIN32 */
46 #include <sys/types.h>
47 #endif /* WIN32 */
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #ifndef _MSC_VER
53 #include <unistd.h>
54 #endif
55 #include <fcntl.h>
56 #include <errno.h>
58 #ifdef HAVE_OS_PROTO_H
59 #include "os-proto.h"
60 #endif
62 #include "pcap-int.h"
64 #ifdef HAVE_DAG_API
65 #include <dagnew.h>
66 #include <dagapi.h>
67 #endif
69 int
70 pcap_dispatch(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
73 return p->read_op(p, cnt, callback, user);
77 * XXX - is this necessary?
79 int
80 pcap_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
83 return p->read_op(p, cnt, callback, user);
86 int
87 pcap_loop(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
89 register int n;
91 for (;;) {
92 if (p->sf.rfile != NULL) {
94 * 0 means EOF, so don't loop if we get 0.
96 n = pcap_offline_read(p, cnt, callback, user);
97 } else {
99 * XXX keep reading until we get something
100 * (or an error occurs)
102 do {
103 n = p->read_op(p, cnt, callback, user);
104 } while (n == 0);
106 if (n <= 0)
107 return (n);
108 if (cnt > 0) {
109 cnt -= n;
110 if (cnt <= 0)
111 return (0);
116 struct singleton {
117 struct pcap_pkthdr *hdr;
118 const u_char *pkt;
122 static void
123 pcap_oneshot(u_char *userData, const struct pcap_pkthdr *h, const u_char *pkt)
125 struct singleton *sp = (struct singleton *)userData;
126 *sp->hdr = *h;
127 sp->pkt = pkt;
130 const u_char *
131 pcap_next(pcap_t *p, struct pcap_pkthdr *h)
133 struct singleton s;
135 s.hdr = h;
136 if (pcap_dispatch(p, 1, pcap_oneshot, (u_char*)&s) <= 0)
137 return (0);
138 return (s.pkt);
141 struct pkt_for_fakecallback {
142 struct pcap_pkthdr *hdr;
143 const u_char **pkt;
146 static void
147 pcap_fakecallback(u_char *userData, const struct pcap_pkthdr *h,
148 const u_char *pkt)
150 struct pkt_for_fakecallback *sp = (struct pkt_for_fakecallback *)userData;
152 *sp->hdr = *h;
153 *sp->pkt = pkt;
156 int
157 pcap_next_ex(pcap_t *p, struct pcap_pkthdr **pkt_header,
158 const u_char **pkt_data)
160 struct pkt_for_fakecallback s;
162 s.hdr = &p->pcap_header;
163 s.pkt = pkt_data;
165 /* Saves a pointer to the packet headers */
166 *pkt_header= &p->pcap_header;
168 if (p->sf.rfile != NULL) {
169 int status;
171 /* We are on an offline capture */
172 status = pcap_offline_read(p, 1, pcap_fakecallback,
173 (u_char *)&s);
176 * Return codes for pcap_offline_read() are:
177 * - 0: EOF
178 * - -1: error
179 * - >1: OK
180 * The first one ('0') conflicts with the return code of
181 * 0 from pcap_read() meaning "no packets arrived before
182 * the timeout expired", so we map it to -2 so you can
183 * distinguish between an EOF from a savefile and a
184 * "no packets arrived before the timeout expired, try
185 * again" from a live capture.
187 if (status == 0)
188 return (-2);
189 else
190 return (status);
194 * Return codes for pcap_read() are:
195 * - 0: timeout
196 * - -1: error
197 * - -2: loop was broken out of with pcap_breakloop()
198 * - >1: OK
199 * The first one ('0') conflicts with the return code of 0 from
200 * pcap_offline_read() meaning "end of file".
202 return (p->read_op(p, 1, pcap_fakecallback, (u_char *)&s));
206 * Force the loop in "pcap_read()" or "pcap_read_offline()" to terminate.
208 void
209 pcap_breakloop(pcap_t *p)
211 p->break_loop = 1;
215 pcap_datalink(pcap_t *p)
217 return (p->linktype);
221 pcap_list_datalinks(pcap_t *p, int **dlt_buffer)
223 if (p->dlt_count == 0) {
225 * We couldn't fetch the list of DLTs, which means
226 * this platform doesn't support changing the
227 * DLT for an interface. Return a list of DLTs
228 * containing only the DLT this device supports.
230 *dlt_buffer = (int*)malloc(sizeof(**dlt_buffer));
231 if (*dlt_buffer == NULL) {
232 (void)snprintf(p->errbuf, sizeof(p->errbuf),
233 "malloc: %s", pcap_strerror(errno));
234 return (-1);
236 **dlt_buffer = p->linktype;
237 return (1);
238 } else {
239 *dlt_buffer = (int*)malloc(sizeof(**dlt_buffer) * p->dlt_count);
240 if (*dlt_buffer == NULL) {
241 (void)snprintf(p->errbuf, sizeof(p->errbuf),
242 "malloc: %s", pcap_strerror(errno));
243 return (-1);
245 (void)memcpy(*dlt_buffer, p->dlt_list,
246 sizeof(**dlt_buffer) * p->dlt_count);
247 return (p->dlt_count);
252 pcap_set_datalink(pcap_t *p, int dlt)
254 int i;
255 const char *dlt_name;
257 if (p->dlt_count == 0 || p->set_datalink_op == NULL) {
259 * We couldn't fetch the list of DLTs, or we don't
260 * have a "set datalink" operation, which means
261 * this platform doesn't support changing the
262 * DLT for an interface. Check whether the new
263 * DLT is the one this interface supports.
265 if (p->linktype != dlt)
266 goto unsupported;
269 * It is, so there's nothing we need to do here.
271 return (0);
273 for (i = 0; i < p->dlt_count; i++)
274 if (p->dlt_list[i] == dlt)
275 break;
276 if (i >= p->dlt_count)
277 goto unsupported;
278 if (p->set_datalink_op(p, dlt) == -1)
279 return (-1);
280 p->linktype = dlt;
281 return (0);
283 unsupported:
284 dlt_name = pcap_datalink_val_to_name(dlt);
285 if (dlt_name != NULL) {
286 (void) snprintf(p->errbuf, sizeof(p->errbuf),
287 "%s is not one of the DLTs supported by this device",
288 dlt_name);
289 } else {
290 (void) snprintf(p->errbuf, sizeof(p->errbuf),
291 "DLT %d is not one of the DLTs supported by this device",
292 dlt);
294 return (-1);
297 struct dlt_choice {
298 const char *name;
299 const char *description;
300 int dlt;
303 #define DLT_CHOICE(code, description) { #code, description, code }
304 #define DLT_CHOICE_SENTINEL { NULL, NULL, 0 }
306 static struct dlt_choice dlt_choices[] = {
307 DLT_CHOICE(DLT_NULL, "BSD loopback"),
308 DLT_CHOICE(DLT_EN10MB, "Ethernet"),
309 DLT_CHOICE(DLT_IEEE802, "Token ring"),
310 DLT_CHOICE(DLT_ARCNET, "ARCNET"),
311 DLT_CHOICE(DLT_SLIP, "SLIP"),
312 DLT_CHOICE(DLT_PPP, "PPP"),
313 DLT_CHOICE(DLT_FDDI, "FDDI"),
314 DLT_CHOICE(DLT_ATM_RFC1483, "RFC 1483 IP-over-ATM"),
315 DLT_CHOICE(DLT_RAW, "Raw IP"),
316 DLT_CHOICE(DLT_SLIP_BSDOS, "BSD/OS SLIP"),
317 DLT_CHOICE(DLT_PPP_BSDOS, "BSD/OS PPP"),
318 DLT_CHOICE(DLT_ATM_CLIP, "Linux Classical IP-over-ATM"),
319 DLT_CHOICE(DLT_PPP_SERIAL, "PPP over serial"),
320 DLT_CHOICE(DLT_PPP_ETHER, "PPPoE"),
321 DLT_CHOICE(DLT_C_HDLC, "Cisco HDLC"),
322 DLT_CHOICE(DLT_IEEE802_11, "802.11"),
323 DLT_CHOICE(DLT_FRELAY, "Frame Relay"),
324 DLT_CHOICE(DLT_LOOP, "OpenBSD loopback"),
325 DLT_CHOICE(DLT_ENC, "OpenBSD encapsulated IP"),
326 DLT_CHOICE(DLT_LINUX_SLL, "Linux cooked"),
327 DLT_CHOICE(DLT_LTALK, "Localtalk"),
328 DLT_CHOICE(DLT_PFLOG, "OpenBSD pflog file"),
329 DLT_CHOICE(DLT_PRISM_HEADER, "802.11 plus Prism header"),
330 DLT_CHOICE(DLT_IP_OVER_FC, "RFC 2625 IP-over-Fibre Channel"),
331 DLT_CHOICE(DLT_SUNATM, "Sun raw ATM"),
332 DLT_CHOICE(DLT_IEEE802_11_RADIO, "802.11 plus BSD radio information header"),
333 DLT_CHOICE(DLT_APPLE_IP_OVER_IEEE1394, "Apple IP-over-IEEE 1394"),
334 DLT_CHOICE(DLT_ARCNET_LINUX, "Linux ARCNET"),
335 DLT_CHOICE(DLT_LINUX_IRDA, "Linux IrDA"),
336 DLT_CHOICE(DLT_IEEE802_11_RADIO_AVS, "802.11 plus AVS radio information header"),
337 DLT_CHOICE_SENTINEL
341 * This array is designed for mapping upper and lower case letter
342 * together for a case independent comparison. The mappings are
343 * based upon ascii character sequences.
345 static const u_char charmap[] = {
346 (u_char)'\000', (u_char)'\001', (u_char)'\002', (u_char)'\003',
347 (u_char)'\004', (u_char)'\005', (u_char)'\006', (u_char)'\007',
348 (u_char)'\010', (u_char)'\011', (u_char)'\012', (u_char)'\013',
349 (u_char)'\014', (u_char)'\015', (u_char)'\016', (u_char)'\017',
350 (u_char)'\020', (u_char)'\021', (u_char)'\022', (u_char)'\023',
351 (u_char)'\024', (u_char)'\025', (u_char)'\026', (u_char)'\027',
352 (u_char)'\030', (u_char)'\031', (u_char)'\032', (u_char)'\033',
353 (u_char)'\034', (u_char)'\035', (u_char)'\036', (u_char)'\037',
354 (u_char)'\040', (u_char)'\041', (u_char)'\042', (u_char)'\043',
355 (u_char)'\044', (u_char)'\045', (u_char)'\046', (u_char)'\047',
356 (u_char)'\050', (u_char)'\051', (u_char)'\052', (u_char)'\053',
357 (u_char)'\054', (u_char)'\055', (u_char)'\056', (u_char)'\057',
358 (u_char)'\060', (u_char)'\061', (u_char)'\062', (u_char)'\063',
359 (u_char)'\064', (u_char)'\065', (u_char)'\066', (u_char)'\067',
360 (u_char)'\070', (u_char)'\071', (u_char)'\072', (u_char)'\073',
361 (u_char)'\074', (u_char)'\075', (u_char)'\076', (u_char)'\077',
362 (u_char)'\100', (u_char)'\141', (u_char)'\142', (u_char)'\143',
363 (u_char)'\144', (u_char)'\145', (u_char)'\146', (u_char)'\147',
364 (u_char)'\150', (u_char)'\151', (u_char)'\152', (u_char)'\153',
365 (u_char)'\154', (u_char)'\155', (u_char)'\156', (u_char)'\157',
366 (u_char)'\160', (u_char)'\161', (u_char)'\162', (u_char)'\163',
367 (u_char)'\164', (u_char)'\165', (u_char)'\166', (u_char)'\167',
368 (u_char)'\170', (u_char)'\171', (u_char)'\172', (u_char)'\133',
369 (u_char)'\134', (u_char)'\135', (u_char)'\136', (u_char)'\137',
370 (u_char)'\140', (u_char)'\141', (u_char)'\142', (u_char)'\143',
371 (u_char)'\144', (u_char)'\145', (u_char)'\146', (u_char)'\147',
372 (u_char)'\150', (u_char)'\151', (u_char)'\152', (u_char)'\153',
373 (u_char)'\154', (u_char)'\155', (u_char)'\156', (u_char)'\157',
374 (u_char)'\160', (u_char)'\161', (u_char)'\162', (u_char)'\163',
375 (u_char)'\164', (u_char)'\165', (u_char)'\166', (u_char)'\167',
376 (u_char)'\170', (u_char)'\171', (u_char)'\172', (u_char)'\173',
377 (u_char)'\174', (u_char)'\175', (u_char)'\176', (u_char)'\177',
378 (u_char)'\200', (u_char)'\201', (u_char)'\202', (u_char)'\203',
379 (u_char)'\204', (u_char)'\205', (u_char)'\206', (u_char)'\207',
380 (u_char)'\210', (u_char)'\211', (u_char)'\212', (u_char)'\213',
381 (u_char)'\214', (u_char)'\215', (u_char)'\216', (u_char)'\217',
382 (u_char)'\220', (u_char)'\221', (u_char)'\222', (u_char)'\223',
383 (u_char)'\224', (u_char)'\225', (u_char)'\226', (u_char)'\227',
384 (u_char)'\230', (u_char)'\231', (u_char)'\232', (u_char)'\233',
385 (u_char)'\234', (u_char)'\235', (u_char)'\236', (u_char)'\237',
386 (u_char)'\240', (u_char)'\241', (u_char)'\242', (u_char)'\243',
387 (u_char)'\244', (u_char)'\245', (u_char)'\246', (u_char)'\247',
388 (u_char)'\250', (u_char)'\251', (u_char)'\252', (u_char)'\253',
389 (u_char)'\254', (u_char)'\255', (u_char)'\256', (u_char)'\257',
390 (u_char)'\260', (u_char)'\261', (u_char)'\262', (u_char)'\263',
391 (u_char)'\264', (u_char)'\265', (u_char)'\266', (u_char)'\267',
392 (u_char)'\270', (u_char)'\271', (u_char)'\272', (u_char)'\273',
393 (u_char)'\274', (u_char)'\275', (u_char)'\276', (u_char)'\277',
394 (u_char)'\300', (u_char)'\341', (u_char)'\342', (u_char)'\343',
395 (u_char)'\344', (u_char)'\345', (u_char)'\346', (u_char)'\347',
396 (u_char)'\350', (u_char)'\351', (u_char)'\352', (u_char)'\353',
397 (u_char)'\354', (u_char)'\355', (u_char)'\356', (u_char)'\357',
398 (u_char)'\360', (u_char)'\361', (u_char)'\362', (u_char)'\363',
399 (u_char)'\364', (u_char)'\365', (u_char)'\366', (u_char)'\367',
400 (u_char)'\370', (u_char)'\371', (u_char)'\372', (u_char)'\333',
401 (u_char)'\334', (u_char)'\335', (u_char)'\336', (u_char)'\337',
402 (u_char)'\340', (u_char)'\341', (u_char)'\342', (u_char)'\343',
403 (u_char)'\344', (u_char)'\345', (u_char)'\346', (u_char)'\347',
404 (u_char)'\350', (u_char)'\351', (u_char)'\352', (u_char)'\353',
405 (u_char)'\354', (u_char)'\355', (u_char)'\356', (u_char)'\357',
406 (u_char)'\360', (u_char)'\361', (u_char)'\362', (u_char)'\363',
407 (u_char)'\364', (u_char)'\365', (u_char)'\366', (u_char)'\367',
408 (u_char)'\370', (u_char)'\371', (u_char)'\372', (u_char)'\373',
409 (u_char)'\374', (u_char)'\375', (u_char)'\376', (u_char)'\377',
413 pcap_strcasecmp(const char *s1, const char *s2)
415 register const u_char *cm = charmap,
416 *us1 = (u_char *)s1,
417 *us2 = (u_char *)s2;
419 while (cm[*us1] == cm[*us2++])
420 if (*us1++ == '\0')
421 return(0);
422 return (cm[*us1] - cm[*--us2]);
426 pcap_datalink_name_to_val(const char *name)
428 int i;
430 for (i = 0; dlt_choices[i].name != NULL; i++) {
431 if (pcap_strcasecmp(dlt_choices[i].name + sizeof("DLT_") - 1,
432 name) == 0)
433 return (dlt_choices[i].dlt);
435 return (-1);
438 const char *
439 pcap_datalink_val_to_name(int dlt)
441 int i;
443 for (i = 0; dlt_choices[i].name != NULL; i++) {
444 if (dlt_choices[i].dlt == dlt)
445 return (dlt_choices[i].name + sizeof("DLT_") - 1);
447 return (NULL);
450 const char *
451 pcap_datalink_val_to_description(int dlt)
453 int i;
455 for (i = 0; dlt_choices[i].name != NULL; i++) {
456 if (dlt_choices[i].dlt == dlt)
457 return (dlt_choices[i].description);
459 return (NULL);
463 pcap_snapshot(pcap_t *p)
465 return (p->snapshot);
469 pcap_is_swapped(pcap_t *p)
471 return (p->sf.swapped);
475 pcap_major_version(pcap_t *p)
477 return (p->sf.version_major);
481 pcap_minor_version(pcap_t *p)
483 return (p->sf.version_minor);
486 FILE *
487 pcap_file(pcap_t *p)
489 return (p->sf.rfile);
493 pcap_fileno(pcap_t *p)
495 #ifndef WIN32
496 return (p->fd);
497 #else
498 if (p->adapter != NULL)
499 return ((int)(DWORD)p->adapter->hFile);
500 else
501 return (-1);
502 #endif
505 #ifndef WIN32
507 pcap_get_selectable_fd(pcap_t *p)
509 return (p->selectable_fd);
511 #endif
513 void
514 pcap_perror(pcap_t *p, char *prefix)
516 fprintf(stderr, "%s: %s\n", prefix, p->errbuf);
519 char *
520 pcap_geterr(pcap_t *p)
522 return (p->errbuf);
526 pcap_getnonblock(pcap_t *p, char *errbuf)
528 return p->getnonblock_op(p, errbuf);
532 * Get the current non-blocking mode setting, under the assumption that
533 * it's just the standard POSIX non-blocking flag.
535 * We don't look at "p->nonblock", in case somebody tweaked the FD
536 * directly.
538 #ifndef WIN32
540 pcap_getnonblock_fd(pcap_t *p, char *errbuf)
542 int fdflags;
544 fdflags = fcntl(p->fd, F_GETFL, 0);
545 if (fdflags == -1) {
546 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s",
547 pcap_strerror(errno));
548 return (-1);
550 if (fdflags & O_NONBLOCK)
551 return (1);
552 else
553 return (0);
555 #endif
558 pcap_setnonblock(pcap_t *p, int nonblock, char *errbuf)
560 return p->setnonblock_op(p, nonblock, errbuf);
563 #ifndef WIN32
565 * Set non-blocking mode, under the assumption that it's just the
566 * standard POSIX non-blocking flag. (This can be called by the
567 * per-platform non-blocking-mode routine if that routine also
568 * needs to do some additional work.)
571 pcap_setnonblock_fd(pcap_t *p, int nonblock, char *errbuf)
573 int fdflags;
575 fdflags = fcntl(p->fd, F_GETFL, 0);
576 if (fdflags == -1) {
577 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s",
578 pcap_strerror(errno));
579 return (-1);
581 if (nonblock)
582 fdflags |= O_NONBLOCK;
583 else
584 fdflags &= ~O_NONBLOCK;
585 if (fcntl(p->fd, F_SETFL, fdflags) == -1) {
586 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_SETFL: %s",
587 pcap_strerror(errno));
588 return (-1);
590 return (0);
592 #endif
594 #ifdef WIN32
596 * Generate a string for the last Win32-specific error (i.e. an error generated when
597 * calling a Win32 API).
598 * For errors occurred during standard C calls, we still use pcap_strerror()
600 char *
601 pcap_win32strerror(void)
603 DWORD error;
604 static char errbuf[PCAP_ERRBUF_SIZE+1];
605 int errlen;
607 error = GetLastError();
608 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, 0, errbuf,
609 PCAP_ERRBUF_SIZE, NULL);
612 * "FormatMessage()" "helpfully" sticks CR/LF at the end of the
613 * message. Get rid of it.
615 errlen = strlen(errbuf);
616 if (errlen >= 2) {
617 errbuf[errlen - 1] = '\0';
618 errbuf[errlen - 2] = '\0';
620 return (errbuf);
622 #endif
625 * Not all systems have strerror().
627 char *
628 pcap_strerror(int errnum)
630 #ifdef HAVE_STRERROR
631 return (strerror(errnum));
632 #else
633 extern int sys_nerr;
634 extern const char *const sys_errlist[];
635 static char ebuf[20];
637 if ((unsigned int)errnum < sys_nerr)
638 return ((char *)sys_errlist[errnum]);
639 (void)snprintf(ebuf, sizeof ebuf, "Unknown error: %d", errnum);
640 return(ebuf);
641 #endif
645 pcap_setfilter(pcap_t *p, struct bpf_program *fp)
647 return p->setfilter_op(p, fp);
651 pcap_stats(pcap_t *p, struct pcap_stat *ps)
653 return p->stats_op(p, ps);
656 static int
657 pcap_stats_dead(pcap_t *p, struct pcap_stat *ps)
659 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
660 "Statistics aren't available from a pcap_open_dead pcap_t");
661 return (-1);
664 static void
665 pcap_close_dead(pcap_t *p)
667 /* Nothing to do. */
670 pcap_t *
671 pcap_open_dead(int linktype, int snaplen)
673 pcap_t *p;
675 p = malloc(sizeof(*p));
676 if (p == NULL)
677 return NULL;
678 memset (p, 0, sizeof(*p));
679 p->snapshot = snaplen;
680 p->linktype = linktype;
681 p->stats_op = pcap_stats_dead;
682 p->close_op = pcap_close_dead;
683 return p;
686 void
687 pcap_close(pcap_t *p)
689 p->close_op(p);
690 if (p->dlt_list != NULL)
691 free(p->dlt_list);
692 pcap_freecode(&p->fcode);
693 free(p);
697 * We make the version string static, and return a pointer to it, rather
698 * than exporting the version string directly. On at least some UNIXes,
699 * if you import data from a shared library into an program, the data is
700 * bound into the program binary, so if the string in the version of the
701 * library with which the program was linked isn't the same as the
702 * string in the version of the library with which the program is being
703 * run, various undesirable things may happen (warnings, the string
704 * being the one from the version of the library with which the program
705 * was linked, or even weirder things, such as the string being the one
706 * from the library but being truncated).
708 #ifdef WIN32
710 * XXX - it'd be nice if we could somehow generate the WinPcap and libpcap
711 * version numbers when building WinPcap. (It'd be nice to do so for
712 * the packet.dll version number as well.)
714 static const char wpcap_version_string[] = "3.0";
715 static const char pcap_version_string_fmt[] =
716 "WinPcap version %s, based on libpcap version 0.8";
717 static const char pcap_version_string_packet_dll_fmt[] =
718 "WinPcap version %s (packet.dll version %s), based on libpcap version 0.8";
719 static char *pcap_version_string;
721 const char *
722 pcap_lib_version(void)
724 char *packet_version_string;
725 size_t pcap_version_string_len;
727 if (pcap_version_string == NULL) {
729 * Generate the version string.
731 packet_version_string = PacketGetVersion();
732 if (strcmp(wpcap_version_string, packet_version_string) == 0) {
734 * WinPcap version string and packet.dll version
735 * string are the same; just report the WinPcap
736 * version.
738 pcap_version_string_len =
739 (sizeof pcap_version_string_fmt - 2) +
740 strlen(wpcap_version_string);
741 pcap_version_string = malloc(pcap_version_string_len);
742 sprintf(pcap_version_string, pcap_version_string_fmt,
743 wpcap_version_string);
744 } else {
746 * WinPcap version string and packet.dll version
747 * string are different; that shouldn't be the
748 * case (the two libraries should come from the
749 * same version of WinPcap), so we report both
750 * versions.
752 pcap_version_string_len =
753 (sizeof pcap_version_string_packet_dll_fmt - 4) +
754 strlen(wpcap_version_string) +
755 strlen(packet_version_string);
756 pcap_version_string = malloc(pcap_version_string_len);
757 sprintf(pcap_version_string,
758 pcap_version_string_packet_dll_fmt,
759 wpcap_version_string, packet_version_string);
762 return (pcap_version_string);
764 #else
765 #include "version.h"
767 const char *
768 pcap_lib_version(void)
770 return (pcap_version_string);
772 #endif