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