]> The Tcpdump Group git mirrors - libpcap/blob - pcap.c
Add a "pcap_breakloop()" API to break out of the loop in
[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[] =
36 "@(#) $Header: /tcpdump/master/libpcap/pcap.c,v 1.63 2003-11-04 07:05:39 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 #ifndef WIN32
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 #include "pcap-int.h"
63
64 int
65 pcap_dispatch(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
66 {
67
68 return p->read_op(p, cnt, callback, user);
69 }
70
71 /*
72 * XXX - is this necessary?
73 */
74 int
75 pcap_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
76 {
77
78 return p->read_op(p, cnt, callback, user);
79 }
80
81 int
82 pcap_loop(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
83 {
84 register int n;
85
86 for (;;) {
87 if (p->sf.rfile != NULL) {
88 /*
89 * 0 means EOF, so don't loop if we get 0.
90 */
91 n = pcap_offline_read(p, cnt, callback, user);
92 } else {
93 /*
94 * XXX keep reading until we get something
95 * (or an error occurs)
96 */
97 do {
98 n = p->read_op(p, cnt, callback, user);
99 } while (n == 0);
100 }
101 if (n <= 0)
102 return (n);
103 if (cnt > 0) {
104 cnt -= n;
105 if (cnt <= 0)
106 return (0);
107 }
108 }
109 }
110
111 struct singleton {
112 struct pcap_pkthdr *hdr;
113 const u_char *pkt;
114 };
115
116
117 static void
118 pcap_oneshot(u_char *userData, const struct pcap_pkthdr *h, const u_char *pkt)
119 {
120 struct singleton *sp = (struct singleton *)userData;
121 *sp->hdr = *h;
122 sp->pkt = pkt;
123 }
124
125 const u_char *
126 pcap_next(pcap_t *p, struct pcap_pkthdr *h)
127 {
128 struct singleton s;
129
130 s.hdr = h;
131 if (pcap_dispatch(p, 1, pcap_oneshot, (u_char*)&s) <= 0)
132 return (0);
133 return (s.pkt);
134 }
135
136 struct pkt_for_fakecallback {
137 struct pcap_pkthdr *hdr;
138 const u_char **pkt;
139 };
140
141 static void
142 pcap_fakecallback(u_char *userData, const struct pcap_pkthdr *h,
143 const u_char *pkt)
144 {
145 struct pkt_for_fakecallback *sp = (struct pkt_for_fakecallback *)userData;
146
147 *sp->hdr = *h;
148 *sp->pkt = pkt;
149 }
150
151 int
152 pcap_next_ex(pcap_t *p, struct pcap_pkthdr **pkt_header,
153 const u_char **pkt_data)
154 {
155 struct pkt_for_fakecallback s;
156
157 s.hdr = &p->pcap_header;
158 s.pkt = pkt_data;
159
160 /* Saves a pointer to the packet headers */
161 *pkt_header= &p->pcap_header;
162
163 if (p->sf.rfile != NULL) {
164 int status;
165
166 /* We are on an offline capture */
167 status = pcap_offline_read(p, 1, pcap_fakecallback,
168 (u_char *)&s);
169
170 /*
171 * Return codes for pcap_offline_read() are:
172 * - 0: EOF
173 * - -1: error
174 * - >1: OK
175 * The first one ('0') conflicts with the return code of
176 * 0 from pcap_read() meaning "no packets arrived before
177 * the timeout expired", so we map it to -2 so you can
178 * distinguish between an EOF from a savefile and a
179 * "no packets arrived before the timeout expired, try
180 * again" from a live capture.
181 */
182 if (status == 0)
183 return (-2);
184 else
185 return (status);
186 }
187
188 /*
189 * Return codes for pcap_read() are:
190 * - 0: timeout
191 * - -1: error
192 * - -2: loop was broken out of with pcap_breakloop()
193 * - >1: OK
194 * The first one ('0') conflicts with the return code of 0 from
195 * pcap_offline_read() meaning "end of file".
196 */
197 return (p->read_op(p, 1, pcap_fakecallback, (u_char *)&s));
198 }
199
200 /*
201 * Force the loop in "pcap_read()" or "pcap_read_offline()" to terminate.
202 */
203 void
204 pcap_breakloop(pcap_t *p)
205 {
206 p->break_loop = 1;
207 }
208
209 int
210 pcap_datalink(pcap_t *p)
211 {
212 return (p->linktype);
213 }
214
215 int
216 pcap_list_datalinks(pcap_t *p, int **dlt_buffer)
217 {
218 if (p->dlt_count == 0) {
219 /*
220 * We couldn't fetch the list of DLTs, which means
221 * this platform doesn't support changing the
222 * DLT for an interface. Return a list of DLTs
223 * containing only the DLT this device supports.
224 */
225 *dlt_buffer = (int*)malloc(sizeof(**dlt_buffer));
226 if (*dlt_buffer == NULL) {
227 (void)snprintf(p->errbuf, sizeof(p->errbuf),
228 "malloc: %s", pcap_strerror(errno));
229 return (-1);
230 }
231 **dlt_buffer = p->linktype;
232 return (1);
233 } else {
234 *dlt_buffer = (int*)malloc(sizeof(**dlt_buffer) * p->dlt_count);
235 if (*dlt_buffer == NULL) {
236 (void)snprintf(p->errbuf, sizeof(p->errbuf),
237 "malloc: %s", pcap_strerror(errno));
238 return (-1);
239 }
240 (void)memcpy(*dlt_buffer, p->dlt_list,
241 sizeof(**dlt_buffer) * p->dlt_count);
242 return (p->dlt_count);
243 }
244 }
245
246 int
247 pcap_set_datalink(pcap_t *p, int dlt)
248 {
249 int i;
250 const char *dlt_name;
251
252 if (p->dlt_count == 0 || p->set_datalink_op == NULL) {
253 /*
254 * We couldn't fetch the list of DLTs, or we don't
255 * have a "set datalink" operation, which means
256 * this platform doesn't support changing the
257 * DLT for an interface. Check whether the new
258 * DLT is the one this interface supports.
259 */
260 if (p->linktype != dlt)
261 goto unsupported;
262
263 /*
264 * It is, so there's nothing we need to do here.
265 */
266 return (0);
267 }
268 for (i = 0; i < p->dlt_count; i++)
269 if (p->dlt_list[i] == dlt)
270 break;
271 if (i >= p->dlt_count)
272 goto unsupported;
273 if (p->set_datalink_op(p, dlt) == -1)
274 return (-1);
275 p->linktype = dlt;
276 return (0);
277
278 unsupported:
279 dlt_name = pcap_datalink_val_to_name(dlt);
280 if (dlt_name != NULL) {
281 (void) snprintf(p->errbuf, sizeof(p->errbuf),
282 "%s is not one of the DLTs supported by this device",
283 dlt_name);
284 } else {
285 (void) snprintf(p->errbuf, sizeof(p->errbuf),
286 "DLT %d is not one of the DLTs supported by this device",
287 dlt);
288 }
289 return (-1);
290 }
291
292 struct dlt_choice {
293 const char *name;
294 int dlt;
295 };
296
297 #define DLT_CHOICE(code) { #code, code }
298 #define DLT_CHOICE_SENTINEL { NULL, 0 }
299
300 static struct dlt_choice dlt_choices[] = {
301 DLT_CHOICE(DLT_NULL),
302 DLT_CHOICE(DLT_EN10MB),
303 DLT_CHOICE(DLT_IEEE802),
304 DLT_CHOICE(DLT_ARCNET),
305 DLT_CHOICE(DLT_SLIP),
306 DLT_CHOICE(DLT_PPP),
307 DLT_CHOICE(DLT_FDDI),
308 DLT_CHOICE(DLT_ATM_RFC1483),
309 DLT_CHOICE(DLT_RAW),
310 DLT_CHOICE(DLT_SLIP_BSDOS),
311 DLT_CHOICE(DLT_PPP_BSDOS),
312 DLT_CHOICE(DLT_ATM_CLIP),
313 DLT_CHOICE(DLT_PPP_SERIAL),
314 DLT_CHOICE(DLT_PPP_ETHER),
315 DLT_CHOICE(DLT_C_HDLC),
316 DLT_CHOICE(DLT_IEEE802_11),
317 DLT_CHOICE(DLT_FRELAY),
318 DLT_CHOICE(DLT_LOOP),
319 DLT_CHOICE(DLT_ENC),
320 DLT_CHOICE(DLT_LINUX_SLL),
321 DLT_CHOICE(DLT_LTALK),
322 DLT_CHOICE(DLT_PFLOG),
323 DLT_CHOICE(DLT_PRISM_HEADER),
324 DLT_CHOICE(DLT_IP_OVER_FC),
325 DLT_CHOICE(DLT_SUNATM),
326 DLT_CHOICE(DLT_IEEE802_11_RADIO),
327 DLT_CHOICE(DLT_ARCNET_LINUX),
328 DLT_CHOICE_SENTINEL
329 };
330
331 /*
332 * This array is designed for mapping upper and lower case letter
333 * together for a case independent comparison. The mappings are
334 * based upon ascii character sequences.
335 */
336 static const u_char charmap[] = {
337 (u_char)'\000', (u_char)'\001', (u_char)'\002', (u_char)'\003',
338 (u_char)'\004', (u_char)'\005', (u_char)'\006', (u_char)'\007',
339 (u_char)'\010', (u_char)'\011', (u_char)'\012', (u_char)'\013',
340 (u_char)'\014', (u_char)'\015', (u_char)'\016', (u_char)'\017',
341 (u_char)'\020', (u_char)'\021', (u_char)'\022', (u_char)'\023',
342 (u_char)'\024', (u_char)'\025', (u_char)'\026', (u_char)'\027',
343 (u_char)'\030', (u_char)'\031', (u_char)'\032', (u_char)'\033',
344 (u_char)'\034', (u_char)'\035', (u_char)'\036', (u_char)'\037',
345 (u_char)'\040', (u_char)'\041', (u_char)'\042', (u_char)'\043',
346 (u_char)'\044', (u_char)'\045', (u_char)'\046', (u_char)'\047',
347 (u_char)'\050', (u_char)'\051', (u_char)'\052', (u_char)'\053',
348 (u_char)'\054', (u_char)'\055', (u_char)'\056', (u_char)'\057',
349 (u_char)'\060', (u_char)'\061', (u_char)'\062', (u_char)'\063',
350 (u_char)'\064', (u_char)'\065', (u_char)'\066', (u_char)'\067',
351 (u_char)'\070', (u_char)'\071', (u_char)'\072', (u_char)'\073',
352 (u_char)'\074', (u_char)'\075', (u_char)'\076', (u_char)'\077',
353 (u_char)'\100', (u_char)'\141', (u_char)'\142', (u_char)'\143',
354 (u_char)'\144', (u_char)'\145', (u_char)'\146', (u_char)'\147',
355 (u_char)'\150', (u_char)'\151', (u_char)'\152', (u_char)'\153',
356 (u_char)'\154', (u_char)'\155', (u_char)'\156', (u_char)'\157',
357 (u_char)'\160', (u_char)'\161', (u_char)'\162', (u_char)'\163',
358 (u_char)'\164', (u_char)'\165', (u_char)'\166', (u_char)'\167',
359 (u_char)'\170', (u_char)'\171', (u_char)'\172', (u_char)'\133',
360 (u_char)'\134', (u_char)'\135', (u_char)'\136', (u_char)'\137',
361 (u_char)'\140', (u_char)'\141', (u_char)'\142', (u_char)'\143',
362 (u_char)'\144', (u_char)'\145', (u_char)'\146', (u_char)'\147',
363 (u_char)'\150', (u_char)'\151', (u_char)'\152', (u_char)'\153',
364 (u_char)'\154', (u_char)'\155', (u_char)'\156', (u_char)'\157',
365 (u_char)'\160', (u_char)'\161', (u_char)'\162', (u_char)'\163',
366 (u_char)'\164', (u_char)'\165', (u_char)'\166', (u_char)'\167',
367 (u_char)'\170', (u_char)'\171', (u_char)'\172', (u_char)'\173',
368 (u_char)'\174', (u_char)'\175', (u_char)'\176', (u_char)'\177',
369 (u_char)'\200', (u_char)'\201', (u_char)'\202', (u_char)'\203',
370 (u_char)'\204', (u_char)'\205', (u_char)'\206', (u_char)'\207',
371 (u_char)'\210', (u_char)'\211', (u_char)'\212', (u_char)'\213',
372 (u_char)'\214', (u_char)'\215', (u_char)'\216', (u_char)'\217',
373 (u_char)'\220', (u_char)'\221', (u_char)'\222', (u_char)'\223',
374 (u_char)'\224', (u_char)'\225', (u_char)'\226', (u_char)'\227',
375 (u_char)'\230', (u_char)'\231', (u_char)'\232', (u_char)'\233',
376 (u_char)'\234', (u_char)'\235', (u_char)'\236', (u_char)'\237',
377 (u_char)'\240', (u_char)'\241', (u_char)'\242', (u_char)'\243',
378 (u_char)'\244', (u_char)'\245', (u_char)'\246', (u_char)'\247',
379 (u_char)'\250', (u_char)'\251', (u_char)'\252', (u_char)'\253',
380 (u_char)'\254', (u_char)'\255', (u_char)'\256', (u_char)'\257',
381 (u_char)'\260', (u_char)'\261', (u_char)'\262', (u_char)'\263',
382 (u_char)'\264', (u_char)'\265', (u_char)'\266', (u_char)'\267',
383 (u_char)'\270', (u_char)'\271', (u_char)'\272', (u_char)'\273',
384 (u_char)'\274', (u_char)'\275', (u_char)'\276', (u_char)'\277',
385 (u_char)'\300', (u_char)'\341', (u_char)'\342', (u_char)'\343',
386 (u_char)'\344', (u_char)'\345', (u_char)'\346', (u_char)'\347',
387 (u_char)'\350', (u_char)'\351', (u_char)'\352', (u_char)'\353',
388 (u_char)'\354', (u_char)'\355', (u_char)'\356', (u_char)'\357',
389 (u_char)'\360', (u_char)'\361', (u_char)'\362', (u_char)'\363',
390 (u_char)'\364', (u_char)'\365', (u_char)'\366', (u_char)'\367',
391 (u_char)'\370', (u_char)'\371', (u_char)'\372', (u_char)'\333',
392 (u_char)'\334', (u_char)'\335', (u_char)'\336', (u_char)'\337',
393 (u_char)'\340', (u_char)'\341', (u_char)'\342', (u_char)'\343',
394 (u_char)'\344', (u_char)'\345', (u_char)'\346', (u_char)'\347',
395 (u_char)'\350', (u_char)'\351', (u_char)'\352', (u_char)'\353',
396 (u_char)'\354', (u_char)'\355', (u_char)'\356', (u_char)'\357',
397 (u_char)'\360', (u_char)'\361', (u_char)'\362', (u_char)'\363',
398 (u_char)'\364', (u_char)'\365', (u_char)'\366', (u_char)'\367',
399 (u_char)'\370', (u_char)'\371', (u_char)'\372', (u_char)'\373',
400 (u_char)'\374', (u_char)'\375', (u_char)'\376', (u_char)'\377',
401 };
402
403 int
404 pcap_strcasecmp(const char *s1, const char *s2)
405 {
406 register const u_char *cm = charmap,
407 *us1 = (u_char *)s1,
408 *us2 = (u_char *)s2;
409
410 while (cm[*us1] == cm[*us2++])
411 if (*us1++ == '\0')
412 return(0);
413 return (cm[*us1] - cm[*--us2]);
414 }
415
416 int
417 pcap_datalink_name_to_val(const char *name)
418 {
419 int i;
420
421 for (i = 0; dlt_choices[i].name != NULL; i++) {
422 if (pcap_strcasecmp(dlt_choices[i].name + sizeof("DLT_") - 1,
423 name) == 0)
424 return (dlt_choices[i].dlt);
425 }
426 return (-1);
427 }
428
429 const char *
430 pcap_datalink_val_to_name(int dlt)
431 {
432 int i;
433
434 for (i = 0; dlt_choices[i].name != NULL; i++) {
435 if (dlt_choices[i].dlt == dlt)
436 return (dlt_choices[i].name + sizeof("DLT_") - 1);
437 }
438 return (NULL);
439 }
440
441 int
442 pcap_snapshot(pcap_t *p)
443 {
444 return (p->snapshot);
445 }
446
447 int
448 pcap_is_swapped(pcap_t *p)
449 {
450 return (p->sf.swapped);
451 }
452
453 int
454 pcap_major_version(pcap_t *p)
455 {
456 return (p->sf.version_major);
457 }
458
459 int
460 pcap_minor_version(pcap_t *p)
461 {
462 return (p->sf.version_minor);
463 }
464
465 FILE *
466 pcap_file(pcap_t *p)
467 {
468 return (p->sf.rfile);
469 }
470
471 int
472 pcap_fileno(pcap_t *p)
473 {
474 #ifndef WIN32
475 return (p->fd);
476 #else
477 if (p->adapter != NULL)
478 return ((int)(DWORD)p->adapter->hFile);
479 else
480 return (-1);
481 #endif
482 }
483
484 void
485 pcap_perror(pcap_t *p, char *prefix)
486 {
487 fprintf(stderr, "%s: %s\n", prefix, p->errbuf);
488 }
489
490 char *
491 pcap_geterr(pcap_t *p)
492 {
493 return (p->errbuf);
494 }
495
496 /*
497 * NOTE: in the future, these may need to call platform-dependent routines,
498 * e.g. on platforms with memory-mapped packet-capture mechanisms where
499 * "pcap_read()" uses "select()" or "poll()" to wait for packets to arrive.
500 */
501 int
502 pcap_getnonblock(pcap_t *p, char *errbuf)
503 {
504 #ifndef WIN32
505 int fdflags;
506 #endif
507
508 if (p->sf.rfile != NULL) {
509 /*
510 * This is a savefile, not a live capture file, so
511 * never say it's in non-blocking mode.
512 */
513 return (0);
514 }
515 #ifndef WIN32
516 fdflags = fcntl(p->fd, F_GETFL, 0);
517 if (fdflags == -1) {
518 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s",
519 pcap_strerror(errno));
520 return (-1);
521 }
522 if (fdflags & O_NONBLOCK)
523 return (1);
524 else
525 return (0);
526 #else
527 return (p->nonblock);
528 #endif
529 }
530
531 int
532 pcap_setnonblock(pcap_t *p, int nonblock, char *errbuf)
533 {
534 #ifndef WIN32
535 int fdflags;
536 #else
537 int newtimeout;
538 #endif
539
540 if (p->sf.rfile != NULL) {
541 /*
542 * This is a savefile, not a live capture file, so
543 * ignore requests to put it in non-blocking mode.
544 */
545 return (0);
546 }
547 #ifndef WIN32
548 fdflags = fcntl(p->fd, F_GETFL, 0);
549 if (fdflags == -1) {
550 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s",
551 pcap_strerror(errno));
552 return (-1);
553 }
554 if (nonblock)
555 fdflags |= O_NONBLOCK;
556 else
557 fdflags &= ~O_NONBLOCK;
558 if (fcntl(p->fd, F_SETFL, fdflags) == -1) {
559 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_SETFL: %s",
560 pcap_strerror(errno));
561 return (-1);
562 }
563 #else
564 if (nonblock) {
565 /*
566 * Set the read timeout to -1 for non-blocking mode.
567 */
568 newtimeout = -1;
569 } else {
570 /*
571 * Restore the timeout set when the device was opened.
572 * (Note that this may be -1, in which case we're not
573 * really leaving non-blocking mode.)
574 */
575 newtimeout = p->timeout;
576 }
577 if (!PacketSetReadTimeout(p->adapter, newtimeout)) {
578 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
579 "PacketSetReadTimeout: %s", pcap_win32strerror());
580 return (-1);
581 }
582 p->nonblock = (newtimeout == -1);
583 #endif
584 return (0);
585 }
586
587 #ifdef WIN32
588 /*
589 * Generate a string for the last Win32-specific error (i.e. an error generated when
590 * calling a Win32 API).
591 * For errors occurred during standard C calls, we still use pcap_strerror()
592 */
593 char *
594 pcap_win32strerror(void)
595 {
596 DWORD error;
597 static char errbuf[PCAP_ERRBUF_SIZE+1];
598 int errlen;
599
600 error = GetLastError();
601 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, 0, errbuf,
602 PCAP_ERRBUF_SIZE, NULL);
603
604 /*
605 * "FormatMessage()" "helpfully" sticks CR/LF at the end of the
606 * message. Get rid of it.
607 */
608 errlen = strlen(errbuf);
609 if (errlen >= 2) {
610 errbuf[errlen - 1] = '\0';
611 errbuf[errlen - 2] = '\0';
612 }
613 return (errbuf);
614 }
615 #endif
616
617 /*
618 * Not all systems have strerror().
619 */
620 char *
621 pcap_strerror(int errnum)
622 {
623 #ifdef HAVE_STRERROR
624 return (strerror(errnum));
625 #else
626 extern int sys_nerr;
627 extern const char *const sys_errlist[];
628 static char ebuf[20];
629
630 if ((unsigned int)errnum < sys_nerr)
631 return ((char *)sys_errlist[errnum]);
632 (void)snprintf(ebuf, sizeof ebuf, "Unknown error: %d", errnum);
633 return(ebuf);
634 #endif
635 }
636
637 int
638 pcap_setfilter(pcap_t *p, struct bpf_program *fp)
639 {
640 return p->setfilter_op(p, fp);
641 }
642
643 int
644 pcap_stats(pcap_t *p, struct pcap_stat *ps)
645 {
646 return p->stats_op(p, ps);
647 }
648
649 static int
650 pcap_stats_dead(pcap_t *p, struct pcap_stat *ps)
651 {
652 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
653 "Statistics aren't available from a pcap_open_dead pcap_t");
654 return (-1);
655 }
656
657 static void
658 pcap_close_dead(pcap_t *p)
659 {
660 /* Nothing to do. */
661 }
662
663 pcap_t *
664 pcap_open_dead(int linktype, int snaplen)
665 {
666 pcap_t *p;
667
668 p = malloc(sizeof(*p));
669 if (p == NULL)
670 return NULL;
671 memset (p, 0, sizeof(*p));
672 p->snapshot = snaplen;
673 p->linktype = linktype;
674 p->stats_op = pcap_stats_dead;
675 p->close_op = pcap_close_dead;
676 return p;
677 }
678
679 void
680 pcap_close(pcap_t *p)
681 {
682 p->close_op(p);
683 if (p->dlt_list != NULL)
684 free(p->dlt_list);
685 pcap_freecode(&p->fcode);
686 free(p);
687 }
688
689 /*
690 * We make the version string static, and return a pointer to it, rather
691 * than exporting the version string directly. On at least some UNIXes,
692 * if you import data from a shared library into an program, the data is
693 * bound into the program binary, so if the string in the version of the
694 * library with which the program was linked isn't the same as the
695 * string in the version of the library with which the program is being
696 * run, various undesirable things may happen (warnings, the string
697 * being the one from the version of the library with which the program
698 * was linked, or even weirder things, such as the string being the one
699 * from the library but being truncated).
700 */
701 #ifdef WIN32
702 /*
703 * XXX - it'd be nice if we could somehow generate the WinPcap and libpcap
704 * version numbers when building WinPcap. (It'd be nice to do so for
705 * the packet.dll version number as well.)
706 */
707 static const char wpcap_version_string[] = "3.0";
708 static const char pcap_version_string_fmt[] =
709 "WinPcap version %s, based on libpcap version 0.8";
710 static const char pcap_version_string_packet_dll_fmt[] =
711 "WinPcap version %s (packet.dll version %s), based on libpcap version 0.8";
712 static char *pcap_version_string;
713
714 const char *
715 pcap_lib_version(void)
716 {
717 char *packet_version_string;
718 size_t pcap_version_string_len;
719
720 if (pcap_version_string == NULL) {
721 /*
722 * Generate the version string.
723 */
724 packet_version_string = PacketGetVersion();
725 if (strcmp(wpcap_version_string, packet_version_string) == 0) {
726 /*
727 * WinPcap version string and packet.dll version
728 * string are the same; just report the WinPcap
729 * version.
730 */
731 pcap_version_string_len =
732 (sizeof pcap_version_string_fmt - 2) +
733 strlen(wpcap_version_string);
734 pcap_version_string = malloc(pcap_version_string_len);
735 sprintf(pcap_version_string, pcap_version_string_fmt,
736 wpcap_version_string);
737 } else {
738 /*
739 * WinPcap version string and packet.dll version
740 * string are different; that shouldn't be the
741 * case (the two libraries should come from the
742 * same version of WinPcap), so we report both
743 * versions.
744 */
745 pcap_version_string_len =
746 (sizeof pcap_version_string_packet_dll_fmt - 4) +
747 strlen(wpcap_version_string) +
748 strlen(packet_version_string);
749 pcap_version_string = malloc(pcap_version_string_len);
750 sprintf(pcap_version_string,
751 pcap_version_string_packet_dll_fmt,
752 wpcap_version_string, packet_version_string);
753 }
754 }
755 return (pcap_version_string);
756 }
757 #else
758 #include "version.h"
759
760 const char *
761 pcap_lib_version(void)
762 {
763 return (pcap_version_string);
764 }
765 #endif