Add resident.conf(5) and varsym.conf(5) manual pages.
[dragonfly/vkernel-mp.git] / contrib / libpcap-0.9 / pcap.c
blobf514970de6bdb59f856e406fd83a4c8d474bc2ed
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.88.2.13 2006/07/27 21:06:17 gianluca 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 #if !defined(_MSC_VER) && !defined(__BORLANDC__)
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 #ifdef MSDOS
63 #include "pcap-dos.h"
64 #endif
66 #include "pcap-int.h"
68 #ifdef HAVE_DAG_API
69 #include <dagnew.h>
70 #include <dagapi.h>
71 #endif
73 int
74 pcap_dispatch(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
77 return p->read_op(p, cnt, callback, user);
81 * XXX - is this necessary?
83 int
84 pcap_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
87 return p->read_op(p, cnt, callback, user);
90 int
91 pcap_loop(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
93 register int n;
95 for (;;) {
96 if (p->sf.rfile != NULL) {
98 * 0 means EOF, so don't loop if we get 0.
100 n = pcap_offline_read(p, cnt, callback, user);
101 } else {
103 * XXX keep reading until we get something
104 * (or an error occurs)
106 do {
107 n = p->read_op(p, cnt, callback, user);
108 } while (n == 0);
110 if (n <= 0)
111 return (n);
112 if (cnt > 0) {
113 cnt -= n;
114 if (cnt <= 0)
115 return (0);
120 struct singleton {
121 struct pcap_pkthdr *hdr;
122 const u_char *pkt;
126 static void
127 pcap_oneshot(u_char *userData, const struct pcap_pkthdr *h, const u_char *pkt)
129 struct singleton *sp = (struct singleton *)userData;
130 *sp->hdr = *h;
131 sp->pkt = pkt;
134 const u_char *
135 pcap_next(pcap_t *p, struct pcap_pkthdr *h)
137 struct singleton s;
139 s.hdr = h;
140 if (pcap_dispatch(p, 1, pcap_oneshot, (u_char*)&s) <= 0)
141 return (0);
142 return (s.pkt);
145 struct pkt_for_fakecallback {
146 struct pcap_pkthdr *hdr;
147 const u_char **pkt;
150 static void
151 pcap_fakecallback(u_char *userData, const struct pcap_pkthdr *h,
152 const u_char *pkt)
154 struct pkt_for_fakecallback *sp = (struct pkt_for_fakecallback *)userData;
156 *sp->hdr = *h;
157 *sp->pkt = pkt;
160 int
161 pcap_next_ex(pcap_t *p, struct pcap_pkthdr **pkt_header,
162 const u_char **pkt_data)
164 struct pkt_for_fakecallback s;
166 s.hdr = &p->pcap_header;
167 s.pkt = pkt_data;
169 /* Saves a pointer to the packet headers */
170 *pkt_header= &p->pcap_header;
172 if (p->sf.rfile != NULL) {
173 int status;
175 /* We are on an offline capture */
176 status = pcap_offline_read(p, 1, pcap_fakecallback,
177 (u_char *)&s);
180 * Return codes for pcap_offline_read() are:
181 * - 0: EOF
182 * - -1: error
183 * - >1: OK
184 * The first one ('0') conflicts with the return code of
185 * 0 from pcap_read() meaning "no packets arrived before
186 * the timeout expired", so we map it to -2 so you can
187 * distinguish between an EOF from a savefile and a
188 * "no packets arrived before the timeout expired, try
189 * again" from a live capture.
191 if (status == 0)
192 return (-2);
193 else
194 return (status);
198 * Return codes for pcap_read() are:
199 * - 0: timeout
200 * - -1: error
201 * - -2: loop was broken out of with pcap_breakloop()
202 * - >1: OK
203 * The first one ('0') conflicts with the return code of 0 from
204 * pcap_offline_read() meaning "end of file".
206 return (p->read_op(p, 1, pcap_fakecallback, (u_char *)&s));
210 * Force the loop in "pcap_read()" or "pcap_read_offline()" to terminate.
212 void
213 pcap_breakloop(pcap_t *p)
215 p->break_loop = 1;
219 pcap_datalink(pcap_t *p)
221 return (p->linktype);
225 pcap_list_datalinks(pcap_t *p, int **dlt_buffer)
227 if (p->dlt_count == 0) {
229 * We couldn't fetch the list of DLTs, which means
230 * this platform doesn't support changing the
231 * DLT for an interface. Return a list of DLTs
232 * containing only the DLT this device supports.
234 *dlt_buffer = (int*)malloc(sizeof(**dlt_buffer));
235 if (*dlt_buffer == NULL) {
236 (void)snprintf(p->errbuf, sizeof(p->errbuf),
237 "malloc: %s", pcap_strerror(errno));
238 return (-1);
240 **dlt_buffer = p->linktype;
241 return (1);
242 } else {
243 *dlt_buffer = (int*)malloc(sizeof(**dlt_buffer) * p->dlt_count);
244 if (*dlt_buffer == NULL) {
245 (void)snprintf(p->errbuf, sizeof(p->errbuf),
246 "malloc: %s", pcap_strerror(errno));
247 return (-1);
249 (void)memcpy(*dlt_buffer, p->dlt_list,
250 sizeof(**dlt_buffer) * p->dlt_count);
251 return (p->dlt_count);
256 pcap_set_datalink(pcap_t *p, int dlt)
258 int i;
259 const char *dlt_name;
261 if (p->dlt_count == 0 || p->set_datalink_op == NULL) {
263 * We couldn't fetch the list of DLTs, or we don't
264 * have a "set datalink" operation, which means
265 * this platform doesn't support changing the
266 * DLT for an interface. Check whether the new
267 * DLT is the one this interface supports.
269 if (p->linktype != dlt)
270 goto unsupported;
273 * It is, so there's nothing we need to do here.
275 return (0);
277 for (i = 0; i < p->dlt_count; i++)
278 if (p->dlt_list[i] == dlt)
279 break;
280 if (i >= p->dlt_count)
281 goto unsupported;
282 if (p->dlt_count == 2 && p->dlt_list[0] == DLT_EN10MB &&
283 dlt == DLT_DOCSIS) {
285 * This is presumably an Ethernet device, as the first
286 * link-layer type it offers is DLT_EN10MB, and the only
287 * other type it offers is DLT_DOCSIS. That means that
288 * we can't tell the driver to supply DOCSIS link-layer
289 * headers - we're just pretending that's what we're
290 * getting, as, presumably, we're capturing on a dedicated
291 * link to a Cisco Cable Modem Termination System, and
292 * it's putting raw DOCSIS frames on the wire inside low-level
293 * Ethernet framing.
295 p->linktype = dlt;
296 return (0);
298 if (p->set_datalink_op(p, dlt) == -1)
299 return (-1);
300 p->linktype = dlt;
301 return (0);
303 unsupported:
304 dlt_name = pcap_datalink_val_to_name(dlt);
305 if (dlt_name != NULL) {
306 (void) snprintf(p->errbuf, sizeof(p->errbuf),
307 "%s is not one of the DLTs supported by this device",
308 dlt_name);
309 } else {
310 (void) snprintf(p->errbuf, sizeof(p->errbuf),
311 "DLT %d is not one of the DLTs supported by this device",
312 dlt);
314 return (-1);
317 struct dlt_choice {
318 const char *name;
319 const char *description;
320 int dlt;
323 #define DLT_CHOICE(code, description) { #code, description, code }
324 #define DLT_CHOICE_SENTINEL { NULL, NULL, 0 }
326 static struct dlt_choice dlt_choices[] = {
327 DLT_CHOICE(DLT_NULL, "BSD loopback"),
328 DLT_CHOICE(DLT_EN10MB, "Ethernet"),
329 DLT_CHOICE(DLT_IEEE802, "Token ring"),
330 DLT_CHOICE(DLT_ARCNET, "ARCNET"),
331 DLT_CHOICE(DLT_SLIP, "SLIP"),
332 DLT_CHOICE(DLT_PPP, "PPP"),
333 DLT_CHOICE(DLT_FDDI, "FDDI"),
334 DLT_CHOICE(DLT_ATM_RFC1483, "RFC 1483 LLC-encapsulated ATM"),
335 DLT_CHOICE(DLT_RAW, "Raw IP"),
336 DLT_CHOICE(DLT_SLIP_BSDOS, "BSD/OS SLIP"),
337 DLT_CHOICE(DLT_PPP_BSDOS, "BSD/OS PPP"),
338 DLT_CHOICE(DLT_ATM_CLIP, "Linux Classical IP-over-ATM"),
339 DLT_CHOICE(DLT_PPP_SERIAL, "PPP over serial"),
340 DLT_CHOICE(DLT_PPP_ETHER, "PPPoE"),
341 DLT_CHOICE(DLT_C_HDLC, "Cisco HDLC"),
342 DLT_CHOICE(DLT_IEEE802_11, "802.11"),
343 DLT_CHOICE(DLT_FRELAY, "Frame Relay"),
344 DLT_CHOICE(DLT_LOOP, "OpenBSD loopback"),
345 DLT_CHOICE(DLT_ENC, "OpenBSD encapsulated IP"),
346 DLT_CHOICE(DLT_LINUX_SLL, "Linux cooked"),
347 DLT_CHOICE(DLT_LTALK, "Localtalk"),
348 DLT_CHOICE(DLT_PFLOG, "OpenBSD pflog file"),
349 DLT_CHOICE(DLT_PRISM_HEADER, "802.11 plus Prism header"),
350 DLT_CHOICE(DLT_IP_OVER_FC, "RFC 2625 IP-over-Fibre Channel"),
351 DLT_CHOICE(DLT_SUNATM, "Sun raw ATM"),
352 DLT_CHOICE(DLT_IEEE802_11_RADIO, "802.11 plus BSD radio information header"),
353 DLT_CHOICE(DLT_APPLE_IP_OVER_IEEE1394, "Apple IP-over-IEEE 1394"),
354 DLT_CHOICE(DLT_ARCNET_LINUX, "Linux ARCNET"),
355 DLT_CHOICE(DLT_DOCSIS, "DOCSIS"),
356 DLT_CHOICE(DLT_LINUX_IRDA, "Linux IrDA"),
357 DLT_CHOICE(DLT_LINUX_LAPD, "Linux vISDN LAPD"),
358 DLT_CHOICE(DLT_IEEE802_11_RADIO_AVS, "802.11 plus AVS radio information header"),
359 DLT_CHOICE(DLT_SYMANTEC_FIREWALL, "Symantec Firewall"),
360 DLT_CHOICE(DLT_JUNIPER_ATM1, "Juniper ATM1 PIC"),
361 DLT_CHOICE(DLT_JUNIPER_ATM2, "Juniper ATM2 PIC"),
362 DLT_CHOICE(DLT_JUNIPER_MLPPP, "Juniper Multi-Link PPP"),
363 DLT_CHOICE(DLT_PPP_PPPD, "PPP for pppd, with direction flag"),
364 DLT_CHOICE(DLT_JUNIPER_PPPOE, "Juniper PPPoE"),
365 DLT_CHOICE(DLT_JUNIPER_PPPOE_ATM, "Juniper PPPoE/ATM"),
366 DLT_CHOICE(DLT_GPRS_LLC, "GPRS LLC"),
367 DLT_CHOICE(DLT_GPF_T, "GPF-T"),
368 DLT_CHOICE(DLT_GPF_F, "GPF-F"),
369 DLT_CHOICE(DLT_JUNIPER_PIC_PEER, "Juniper PIC Peer"),
370 DLT_CHOICE(DLT_JUNIPER_MLFR, "Juniper Multi-Link Frame Relay"),
371 DLT_CHOICE(DLT_ERF_ETH, "Ethernet with Endace ERF header"),
372 DLT_CHOICE(DLT_ERF_POS, "Packet-over-SONET with Endace ERF header"),
373 DLT_CHOICE(DLT_JUNIPER_GGSN, "Juniper GGSN PIC"),
374 DLT_CHOICE(DLT_JUNIPER_ES, "Juniper Encryption Services PIC"),
375 DLT_CHOICE(DLT_JUNIPER_MONITOR, "Juniper Passive Monitor PIC"),
376 DLT_CHOICE(DLT_JUNIPER_SERVICES, "Juniper Advanced Services PIC"),
377 DLT_CHOICE(DLT_JUNIPER_MFR, "Juniper FRF.16 Frame Relay"),
378 DLT_CHOICE(DLT_JUNIPER_ETHER, "Juniper Ethernet"),
379 DLT_CHOICE(DLT_JUNIPER_PPP, "Juniper PPP"),
380 DLT_CHOICE(DLT_JUNIPER_FRELAY, "Juniper Frame Relay"),
381 DLT_CHOICE(DLT_JUNIPER_CHDLC, "Juniper C-HDLC"),
382 DLT_CHOICE(DLT_MFR, "FRF.16 Frame Relay"),
383 DLT_CHOICE(DLT_JUNIPER_VP, "Juniper Voice PIC"),
384 DLT_CHOICE(DLT_A429, "Arinc 429"),
385 DLT_CHOICE(DLT_CAN20B, "Controller Area Network (CAN) v. 2.0B"),
386 DLT_CHOICE(DLT_A653_ICM, "Arinc 653 Interpartition Communication"),
387 DLT_CHOICE_SENTINEL
391 * This array is designed for mapping upper and lower case letter
392 * together for a case independent comparison. The mappings are
393 * based upon ascii character sequences.
395 static const u_char charmap[] = {
396 (u_char)'\000', (u_char)'\001', (u_char)'\002', (u_char)'\003',
397 (u_char)'\004', (u_char)'\005', (u_char)'\006', (u_char)'\007',
398 (u_char)'\010', (u_char)'\011', (u_char)'\012', (u_char)'\013',
399 (u_char)'\014', (u_char)'\015', (u_char)'\016', (u_char)'\017',
400 (u_char)'\020', (u_char)'\021', (u_char)'\022', (u_char)'\023',
401 (u_char)'\024', (u_char)'\025', (u_char)'\026', (u_char)'\027',
402 (u_char)'\030', (u_char)'\031', (u_char)'\032', (u_char)'\033',
403 (u_char)'\034', (u_char)'\035', (u_char)'\036', (u_char)'\037',
404 (u_char)'\040', (u_char)'\041', (u_char)'\042', (u_char)'\043',
405 (u_char)'\044', (u_char)'\045', (u_char)'\046', (u_char)'\047',
406 (u_char)'\050', (u_char)'\051', (u_char)'\052', (u_char)'\053',
407 (u_char)'\054', (u_char)'\055', (u_char)'\056', (u_char)'\057',
408 (u_char)'\060', (u_char)'\061', (u_char)'\062', (u_char)'\063',
409 (u_char)'\064', (u_char)'\065', (u_char)'\066', (u_char)'\067',
410 (u_char)'\070', (u_char)'\071', (u_char)'\072', (u_char)'\073',
411 (u_char)'\074', (u_char)'\075', (u_char)'\076', (u_char)'\077',
412 (u_char)'\100', (u_char)'\141', (u_char)'\142', (u_char)'\143',
413 (u_char)'\144', (u_char)'\145', (u_char)'\146', (u_char)'\147',
414 (u_char)'\150', (u_char)'\151', (u_char)'\152', (u_char)'\153',
415 (u_char)'\154', (u_char)'\155', (u_char)'\156', (u_char)'\157',
416 (u_char)'\160', (u_char)'\161', (u_char)'\162', (u_char)'\163',
417 (u_char)'\164', (u_char)'\165', (u_char)'\166', (u_char)'\167',
418 (u_char)'\170', (u_char)'\171', (u_char)'\172', (u_char)'\133',
419 (u_char)'\134', (u_char)'\135', (u_char)'\136', (u_char)'\137',
420 (u_char)'\140', (u_char)'\141', (u_char)'\142', (u_char)'\143',
421 (u_char)'\144', (u_char)'\145', (u_char)'\146', (u_char)'\147',
422 (u_char)'\150', (u_char)'\151', (u_char)'\152', (u_char)'\153',
423 (u_char)'\154', (u_char)'\155', (u_char)'\156', (u_char)'\157',
424 (u_char)'\160', (u_char)'\161', (u_char)'\162', (u_char)'\163',
425 (u_char)'\164', (u_char)'\165', (u_char)'\166', (u_char)'\167',
426 (u_char)'\170', (u_char)'\171', (u_char)'\172', (u_char)'\173',
427 (u_char)'\174', (u_char)'\175', (u_char)'\176', (u_char)'\177',
428 (u_char)'\200', (u_char)'\201', (u_char)'\202', (u_char)'\203',
429 (u_char)'\204', (u_char)'\205', (u_char)'\206', (u_char)'\207',
430 (u_char)'\210', (u_char)'\211', (u_char)'\212', (u_char)'\213',
431 (u_char)'\214', (u_char)'\215', (u_char)'\216', (u_char)'\217',
432 (u_char)'\220', (u_char)'\221', (u_char)'\222', (u_char)'\223',
433 (u_char)'\224', (u_char)'\225', (u_char)'\226', (u_char)'\227',
434 (u_char)'\230', (u_char)'\231', (u_char)'\232', (u_char)'\233',
435 (u_char)'\234', (u_char)'\235', (u_char)'\236', (u_char)'\237',
436 (u_char)'\240', (u_char)'\241', (u_char)'\242', (u_char)'\243',
437 (u_char)'\244', (u_char)'\245', (u_char)'\246', (u_char)'\247',
438 (u_char)'\250', (u_char)'\251', (u_char)'\252', (u_char)'\253',
439 (u_char)'\254', (u_char)'\255', (u_char)'\256', (u_char)'\257',
440 (u_char)'\260', (u_char)'\261', (u_char)'\262', (u_char)'\263',
441 (u_char)'\264', (u_char)'\265', (u_char)'\266', (u_char)'\267',
442 (u_char)'\270', (u_char)'\271', (u_char)'\272', (u_char)'\273',
443 (u_char)'\274', (u_char)'\275', (u_char)'\276', (u_char)'\277',
444 (u_char)'\300', (u_char)'\341', (u_char)'\342', (u_char)'\343',
445 (u_char)'\344', (u_char)'\345', (u_char)'\346', (u_char)'\347',
446 (u_char)'\350', (u_char)'\351', (u_char)'\352', (u_char)'\353',
447 (u_char)'\354', (u_char)'\355', (u_char)'\356', (u_char)'\357',
448 (u_char)'\360', (u_char)'\361', (u_char)'\362', (u_char)'\363',
449 (u_char)'\364', (u_char)'\365', (u_char)'\366', (u_char)'\367',
450 (u_char)'\370', (u_char)'\371', (u_char)'\372', (u_char)'\333',
451 (u_char)'\334', (u_char)'\335', (u_char)'\336', (u_char)'\337',
452 (u_char)'\340', (u_char)'\341', (u_char)'\342', (u_char)'\343',
453 (u_char)'\344', (u_char)'\345', (u_char)'\346', (u_char)'\347',
454 (u_char)'\350', (u_char)'\351', (u_char)'\352', (u_char)'\353',
455 (u_char)'\354', (u_char)'\355', (u_char)'\356', (u_char)'\357',
456 (u_char)'\360', (u_char)'\361', (u_char)'\362', (u_char)'\363',
457 (u_char)'\364', (u_char)'\365', (u_char)'\366', (u_char)'\367',
458 (u_char)'\370', (u_char)'\371', (u_char)'\372', (u_char)'\373',
459 (u_char)'\374', (u_char)'\375', (u_char)'\376', (u_char)'\377',
463 pcap_strcasecmp(const char *s1, const char *s2)
465 register const u_char *cm = charmap,
466 *us1 = (u_char *)s1,
467 *us2 = (u_char *)s2;
469 while (cm[*us1] == cm[*us2++])
470 if (*us1++ == '\0')
471 return(0);
472 return (cm[*us1] - cm[*--us2]);
476 pcap_datalink_name_to_val(const char *name)
478 int i;
480 for (i = 0; dlt_choices[i].name != NULL; i++) {
481 if (pcap_strcasecmp(dlt_choices[i].name + sizeof("DLT_") - 1,
482 name) == 0)
483 return (dlt_choices[i].dlt);
485 return (-1);
488 const char *
489 pcap_datalink_val_to_name(int dlt)
491 int i;
493 for (i = 0; dlt_choices[i].name != NULL; i++) {
494 if (dlt_choices[i].dlt == dlt)
495 return (dlt_choices[i].name + sizeof("DLT_") - 1);
497 return (NULL);
500 const char *
501 pcap_datalink_val_to_description(int dlt)
503 int i;
505 for (i = 0; dlt_choices[i].name != NULL; i++) {
506 if (dlt_choices[i].dlt == dlt)
507 return (dlt_choices[i].description);
509 return (NULL);
513 pcap_snapshot(pcap_t *p)
515 return (p->snapshot);
519 pcap_is_swapped(pcap_t *p)
521 return (p->sf.swapped);
525 pcap_major_version(pcap_t *p)
527 return (p->sf.version_major);
531 pcap_minor_version(pcap_t *p)
533 return (p->sf.version_minor);
536 FILE *
537 pcap_file(pcap_t *p)
539 return (p->sf.rfile);
543 pcap_fileno(pcap_t *p)
545 #ifndef WIN32
546 return (p->fd);
547 #else
548 if (p->adapter != NULL)
549 return ((int)(DWORD)p->adapter->hFile);
550 else
551 return (-1);
552 #endif
555 #if !defined(WIN32) && !defined(MSDOS)
557 pcap_get_selectable_fd(pcap_t *p)
559 return (p->selectable_fd);
561 #endif
563 void
564 pcap_perror(pcap_t *p, char *prefix)
566 fprintf(stderr, "%s: %s\n", prefix, p->errbuf);
569 char *
570 pcap_geterr(pcap_t *p)
572 return (p->errbuf);
576 pcap_getnonblock(pcap_t *p, char *errbuf)
578 return p->getnonblock_op(p, errbuf);
582 * Get the current non-blocking mode setting, under the assumption that
583 * it's just the standard POSIX non-blocking flag.
585 * We don't look at "p->nonblock", in case somebody tweaked the FD
586 * directly.
588 #if !defined(WIN32) && !defined(MSDOS)
590 pcap_getnonblock_fd(pcap_t *p, char *errbuf)
592 int fdflags;
594 fdflags = fcntl(p->fd, F_GETFL, 0);
595 if (fdflags == -1) {
596 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s",
597 pcap_strerror(errno));
598 return (-1);
600 if (fdflags & O_NONBLOCK)
601 return (1);
602 else
603 return (0);
605 #endif
608 pcap_setnonblock(pcap_t *p, int nonblock, char *errbuf)
610 return p->setnonblock_op(p, nonblock, errbuf);
613 #if !defined(WIN32) && !defined(MSDOS)
615 * Set non-blocking mode, under the assumption that it's just the
616 * standard POSIX non-blocking flag. (This can be called by the
617 * per-platform non-blocking-mode routine if that routine also
618 * needs to do some additional work.)
621 pcap_setnonblock_fd(pcap_t *p, int nonblock, char *errbuf)
623 int fdflags;
625 fdflags = fcntl(p->fd, F_GETFL, 0);
626 if (fdflags == -1) {
627 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s",
628 pcap_strerror(errno));
629 return (-1);
631 if (nonblock)
632 fdflags |= O_NONBLOCK;
633 else
634 fdflags &= ~O_NONBLOCK;
635 if (fcntl(p->fd, F_SETFL, fdflags) == -1) {
636 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_SETFL: %s",
637 pcap_strerror(errno));
638 return (-1);
640 return (0);
642 #endif
644 #ifdef WIN32
646 * Generate a string for the last Win32-specific error (i.e. an error generated when
647 * calling a Win32 API).
648 * For errors occurred during standard C calls, we still use pcap_strerror()
650 char *
651 pcap_win32strerror(void)
653 DWORD error;
654 static char errbuf[PCAP_ERRBUF_SIZE+1];
655 int errlen;
656 char *p;
658 error = GetLastError();
659 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, 0, errbuf,
660 PCAP_ERRBUF_SIZE, NULL);
663 * "FormatMessage()" "helpfully" sticks CR/LF at the end of the
664 * message. Get rid of it.
666 errlen = strlen(errbuf);
667 if (errlen >= 2) {
668 errbuf[errlen - 1] = '\0';
669 errbuf[errlen - 2] = '\0';
671 p = strchr(errbuf, '\0');
672 snprintf (p, sizeof(errbuf)-(p-errbuf), " (%lu)", error);
673 return (errbuf);
675 #endif
678 * Not all systems have strerror().
680 char *
681 pcap_strerror(int errnum)
683 #ifdef HAVE_STRERROR
684 return (strerror(errnum));
685 #else
686 extern int sys_nerr;
687 extern const char *const sys_errlist[];
688 static char ebuf[20];
690 if ((unsigned int)errnum < sys_nerr)
691 return ((char *)sys_errlist[errnum]);
692 (void)snprintf(ebuf, sizeof ebuf, "Unknown error: %d", errnum);
693 return(ebuf);
694 #endif
698 pcap_setfilter(pcap_t *p, struct bpf_program *fp)
700 return p->setfilter_op(p, fp);
704 * Set direction flag, which controls whether we accept only incoming
705 * packets, only outgoing packets, or both.
706 * Note that, depending on the platform, some or all direction arguments
707 * might not be supported.
710 pcap_setdirection(pcap_t *p, pcap_direction_t d)
712 if (p->setdirection_op == NULL) {
713 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
714 "Setting direction is not implemented on this platform");
715 return -1;
716 } else
717 return p->setdirection_op(p, d);
721 pcap_stats(pcap_t *p, struct pcap_stat *ps)
723 return p->stats_op(p, ps);
726 static int
727 pcap_stats_dead(pcap_t *p, struct pcap_stat *ps _U_)
729 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
730 "Statistics aren't available from a pcap_open_dead pcap_t");
731 return (-1);
734 void
735 pcap_close_common(pcap_t *p)
737 if (p->buffer != NULL)
738 free(p->buffer);
739 #if !defined(WIN32) && !defined(MSDOS)
740 if (p->fd >= 0)
741 close(p->fd);
742 #endif
745 static void
746 pcap_close_dead(pcap_t *p _U_)
748 /* Nothing to do. */
751 pcap_t *
752 pcap_open_dead(int linktype, int snaplen)
754 pcap_t *p;
756 p = malloc(sizeof(*p));
757 if (p == NULL)
758 return NULL;
759 memset (p, 0, sizeof(*p));
760 p->snapshot = snaplen;
761 p->linktype = linktype;
762 p->stats_op = pcap_stats_dead;
763 p->close_op = pcap_close_dead;
764 return p;
768 * API compatible with WinPcap's "send a packet" routine - returns -1
769 * on error, 0 otherwise.
771 * XXX - what if we get a short write?
774 pcap_sendpacket(pcap_t *p, const u_char *buf, int size)
776 if (p->inject_op(p, buf, size) == -1)
777 return (-1);
778 return (0);
782 * API compatible with OpenBSD's "send a packet" routine - returns -1 on
783 * error, number of bytes written otherwise.
786 pcap_inject(pcap_t *p, const void *buf, size_t size)
788 return (p->inject_op(p, buf, size));
791 void
792 pcap_close(pcap_t *p)
794 p->close_op(p);
795 if (p->dlt_list != NULL)
796 free(p->dlt_list);
797 pcap_freecode(&p->fcode);
798 free(p);
802 * We make the version string static, and return a pointer to it, rather
803 * than exporting the version string directly. On at least some UNIXes,
804 * if you import data from a shared library into an program, the data is
805 * bound into the program binary, so if the string in the version of the
806 * library with which the program was linked isn't the same as the
807 * string in the version of the library with which the program is being
808 * run, various undesirable things may happen (warnings, the string
809 * being the one from the version of the library with which the program
810 * was linked, or even weirder things, such as the string being the one
811 * from the library but being truncated).
813 #ifdef HAVE_VERSION_H
814 #include "version.h"
815 #else
816 static const char pcap_version_string[] = "libpcap version 0.9.4";
817 #endif
819 #ifdef WIN32
821 * XXX - it'd be nice if we could somehow generate the WinPcap and libpcap
822 * version numbers when building WinPcap. (It'd be nice to do so for
823 * the packet.dll version number as well.)
825 static const char wpcap_version_string[] = "3.2 alpha1";
826 static const char pcap_version_string_fmt[] =
827 "WinPcap version %s, based on %s";
828 static const char pcap_version_string_packet_dll_fmt[] =
829 "WinPcap version %s (packet.dll version %s), based on %s";
830 static char *full_pcap_version_string;
832 const char *
833 pcap_lib_version(void)
835 char *packet_version_string;
836 size_t full_pcap_version_string_len;
838 if (full_pcap_version_string == NULL) {
840 * Generate the version string.
842 packet_version_string = PacketGetVersion();
843 if (strcmp(wpcap_version_string, packet_version_string) == 0) {
845 * WinPcap version string and packet.dll version
846 * string are the same; just report the WinPcap
847 * version.
849 full_pcap_version_string_len =
850 (sizeof pcap_version_string_fmt - 4) +
851 strlen(wpcap_version_string) +
852 strlen(pcap_version_string);
853 full_pcap_version_string =
854 malloc(full_pcap_version_string_len);
855 sprintf(full_pcap_version_string,
856 pcap_version_string_fmt, wpcap_version_string,
857 pcap_version_string);
858 } else {
860 * WinPcap version string and packet.dll version
861 * string are different; that shouldn't be the
862 * case (the two libraries should come from the
863 * same version of WinPcap), so we report both
864 * versions.
866 full_pcap_version_string_len =
867 (sizeof pcap_version_string_packet_dll_fmt - 6) +
868 strlen(wpcap_version_string) +
869 strlen(packet_version_string) +
870 strlen(pcap_version_string);
871 full_pcap_version_string = malloc(full_pcap_version_string_len);
873 sprintf(full_pcap_version_string,
874 pcap_version_string_packet_dll_fmt,
875 wpcap_version_string, packet_version_string,
876 pcap_version_string);
879 return (full_pcap_version_string);
882 #elif defined(MSDOS)
884 static char *full_pcap_version_string;
886 const char *
887 pcap_lib_version (void)
889 char *packet_version_string;
890 size_t full_pcap_version_string_len;
891 static char dospfx[] = "DOS-";
893 if (full_pcap_version_string == NULL) {
895 * Generate the version string.
897 full_pcap_version_string_len =
898 sizeof dospfx + strlen(pcap_version_string);
899 full_pcap_version_string =
900 malloc(full_pcap_version_string_len);
901 strcpy(full_pcap_version_string, dospfx);
902 strcat(full_pcap_version_string, pcap_version_string);
904 return (full_pcap_version_string);
907 #else /* UN*X */
909 const char *
910 pcap_lib_version(void)
912 return (pcap_version_string);
914 #endif