add winpcap 4.0.2 from url http://www.winpcap.org/
[natblaster.git] / winpcap / wpcap / libpcap / pcap.c
blob565d37d42e58984acb684ee032fe4302f389ae19
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 #ifdef HAVE_REMOTE
74 #include <pcap-remote.h>
75 #endif
77 int
78 pcap_dispatch(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
81 #ifdef HAVE_REMOTE
82 /* Checks the capture type */
83 if (p->rmt_clientside)
85 /* We are on an remote capture */
86 if (!p->rmt_capstarted)
88 // if the capture has not started yet, please start it
89 if (pcap_startcapture_remote(p) )
90 return -1;
93 #endif /* HAVE_REMOTE */
95 return p->read_op(p, cnt, callback, user);
99 * XXX - is this necessary?
102 pcap_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
105 return p->read_op(p, cnt, callback, user);
109 pcap_loop(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
111 register int n;
113 #ifdef HAVE_REMOTE
114 /* Checks the capture type */
115 if (p->rmt_clientside)
117 /* We are on an remote capture */
118 if (!p->rmt_capstarted)
120 // if the capture has not started yet, please start it
121 if (pcap_startcapture_remote(p) )
122 return -1;
125 #endif /* HAVE_REMOTE */
127 for (;;) {
128 if (p->sf.rfile != NULL) {
130 * 0 means EOF, so don't loop if we get 0.
132 n = pcap_offline_read(p, cnt, callback, user);
133 } else {
135 * XXX keep reading until we get something
136 * (or an error occurs)
138 do {
139 n = p->read_op(p, cnt, callback, user);
140 } while (n == 0);
142 if (n <= 0)
143 return (n);
144 if (cnt > 0) {
145 cnt -= n;
146 if (cnt <= 0)
147 return (0);
152 struct singleton {
153 struct pcap_pkthdr *hdr;
154 const u_char *pkt;
158 static void
159 pcap_oneshot(u_char *userData, const struct pcap_pkthdr *h, const u_char *pkt)
161 struct singleton *sp = (struct singleton *)userData;
162 *sp->hdr = *h;
163 sp->pkt = pkt;
166 const u_char *
167 pcap_next(pcap_t *p, struct pcap_pkthdr *h)
169 struct singleton s;
171 s.hdr = h;
172 if (pcap_dispatch(p, 1, pcap_oneshot, (u_char*)&s) <= 0)
173 return (0);
174 return (s.pkt);
177 struct pkt_for_fakecallback {
178 struct pcap_pkthdr *hdr;
179 const u_char **pkt;
182 static void
183 pcap_fakecallback(u_char *userData, const struct pcap_pkthdr *h,
184 const u_char *pkt)
186 struct pkt_for_fakecallback *sp = (struct pkt_for_fakecallback *)userData;
188 *sp->hdr = *h;
189 *sp->pkt = pkt;
192 int
193 pcap_next_ex(pcap_t *p, struct pcap_pkthdr **pkt_header,
194 const u_char **pkt_data)
196 struct pkt_for_fakecallback s;
198 s.hdr = &p->pcap_header;
199 s.pkt = pkt_data;
201 /* Saves a pointer to the packet headers */
202 *pkt_header= &p->pcap_header;
204 #ifdef HAVE_REMOTE
205 /* Checks the capture type */
206 if (p->rmt_clientside)
208 /* We are on an remote capture */
209 if (!p->rmt_capstarted)
211 // if the capture has not started yet, please start it
212 if (pcap_startcapture_remote(p) )
213 return -1;
216 return pcap_read_nocb_remote(p, pkt_header, (u_char **) pkt_data);
218 #endif /* HAVE_REMOTE */
220 if (p->sf.rfile != NULL) {
221 int status;
223 /* We are on an offline capture */
224 status = pcap_offline_read(p, 1, pcap_fakecallback,
225 (u_char *)&s);
228 * Return codes for pcap_offline_read() are:
229 * - 0: EOF
230 * - -1: error
231 * - >1: OK
232 * The first one ('0') conflicts with the return code of
233 * 0 from pcap_read() meaning "no packets arrived before
234 * the timeout expired", so we map it to -2 so you can
235 * distinguish between an EOF from a savefile and a
236 * "no packets arrived before the timeout expired, try
237 * again" from a live capture.
239 if (status == 0)
240 return (-2);
241 else
242 return (status);
246 * Return codes for pcap_read() are:
247 * - 0: timeout
248 * - -1: error
249 * - -2: loop was broken out of with pcap_breakloop()
250 * - >1: OK
251 * The first one ('0') conflicts with the return code of 0 from
252 * pcap_offline_read() meaning "end of file".
254 return (p->read_op(p, 1, pcap_fakecallback, (u_char *)&s));
258 * Force the loop in "pcap_read()" or "pcap_read_offline()" to terminate.
260 void
261 pcap_breakloop(pcap_t *p)
263 p->break_loop = 1;
267 pcap_datalink(pcap_t *p)
269 return (p->linktype);
273 pcap_list_datalinks(pcap_t *p, int **dlt_buffer)
275 if (p->dlt_count == 0) {
277 * We couldn't fetch the list of DLTs, which means
278 * this platform doesn't support changing the
279 * DLT for an interface. Return a list of DLTs
280 * containing only the DLT this device supports.
282 *dlt_buffer = (int*)malloc(sizeof(**dlt_buffer));
283 if (*dlt_buffer == NULL) {
284 (void)snprintf(p->errbuf, sizeof(p->errbuf),
285 "malloc: %s", pcap_strerror(errno));
286 return (-1);
288 **dlt_buffer = p->linktype;
289 return (1);
290 } else {
291 *dlt_buffer = (int*)malloc(sizeof(**dlt_buffer) * p->dlt_count);
292 if (*dlt_buffer == NULL) {
293 (void)snprintf(p->errbuf, sizeof(p->errbuf),
294 "malloc: %s", pcap_strerror(errno));
295 return (-1);
297 (void)memcpy(*dlt_buffer, p->dlt_list,
298 sizeof(**dlt_buffer) * p->dlt_count);
299 return (p->dlt_count);
304 pcap_set_datalink(pcap_t *p, int dlt)
306 int i;
307 const char *dlt_name;
309 if (p->dlt_count == 0 || p->set_datalink_op == NULL) {
311 * We couldn't fetch the list of DLTs, or we don't
312 * have a "set datalink" operation, which means
313 * this platform doesn't support changing the
314 * DLT for an interface. Check whether the new
315 * DLT is the one this interface supports.
317 if (p->linktype != dlt)
318 goto unsupported;
321 * It is, so there's nothing we need to do here.
323 return (0);
325 for (i = 0; i < p->dlt_count; i++)
326 if (p->dlt_list[i] == dlt)
327 break;
328 if (i >= p->dlt_count)
329 goto unsupported;
330 if (p->dlt_count == 2 && p->dlt_list[0] == DLT_EN10MB &&
331 dlt == DLT_DOCSIS) {
333 * This is presumably an Ethernet device, as the first
334 * link-layer type it offers is DLT_EN10MB, and the only
335 * other type it offers is DLT_DOCSIS. That means that
336 * we can't tell the driver to supply DOCSIS link-layer
337 * headers - we're just pretending that's what we're
338 * getting, as, presumably, we're capturing on a dedicated
339 * link to a Cisco Cable Modem Termination System, and
340 * it's putting raw DOCSIS frames on the wire inside low-level
341 * Ethernet framing.
343 p->linktype = dlt;
344 return (0);
346 if (p->set_datalink_op(p, dlt) == -1)
347 return (-1);
348 p->linktype = dlt;
349 return (0);
351 unsupported:
352 dlt_name = pcap_datalink_val_to_name(dlt);
353 if (dlt_name != NULL) {
354 (void) snprintf(p->errbuf, sizeof(p->errbuf),
355 "%s is not one of the DLTs supported by this device",
356 dlt_name);
357 } else {
358 (void) snprintf(p->errbuf, sizeof(p->errbuf),
359 "DLT %d is not one of the DLTs supported by this device",
360 dlt);
362 return (-1);
365 struct dlt_choice {
366 const char *name;
367 const char *description;
368 int dlt;
371 #define DLT_CHOICE(code, description) { #code, description, code }
372 #define DLT_CHOICE_SENTINEL { NULL, NULL, 0 }
374 static struct dlt_choice dlt_choices[] = {
375 DLT_CHOICE(DLT_NULL, "BSD loopback"),
376 DLT_CHOICE(DLT_EN10MB, "Ethernet"),
377 DLT_CHOICE(DLT_IEEE802, "Token ring"),
378 DLT_CHOICE(DLT_ARCNET, "ARCNET"),
379 DLT_CHOICE(DLT_SLIP, "SLIP"),
380 DLT_CHOICE(DLT_PPP, "PPP"),
381 DLT_CHOICE(DLT_FDDI, "FDDI"),
382 DLT_CHOICE(DLT_ATM_RFC1483, "RFC 1483 LLC-encapsulated ATM"),
383 DLT_CHOICE(DLT_RAW, "Raw IP"),
384 DLT_CHOICE(DLT_SLIP_BSDOS, "BSD/OS SLIP"),
385 DLT_CHOICE(DLT_PPP_BSDOS, "BSD/OS PPP"),
386 DLT_CHOICE(DLT_ATM_CLIP, "Linux Classical IP-over-ATM"),
387 DLT_CHOICE(DLT_PPP_SERIAL, "PPP over serial"),
388 DLT_CHOICE(DLT_PPP_ETHER, "PPPoE"),
389 DLT_CHOICE(DLT_C_HDLC, "Cisco HDLC"),
390 DLT_CHOICE(DLT_IEEE802_11, "802.11"),
391 DLT_CHOICE(DLT_FRELAY, "Frame Relay"),
392 DLT_CHOICE(DLT_LOOP, "OpenBSD loopback"),
393 DLT_CHOICE(DLT_ENC, "OpenBSD encapsulated IP"),
394 DLT_CHOICE(DLT_LINUX_SLL, "Linux cooked"),
395 DLT_CHOICE(DLT_LTALK, "Localtalk"),
396 DLT_CHOICE(DLT_PFLOG, "OpenBSD pflog file"),
397 DLT_CHOICE(DLT_PRISM_HEADER, "802.11 plus Prism header"),
398 DLT_CHOICE(DLT_IP_OVER_FC, "RFC 2625 IP-over-Fibre Channel"),
399 DLT_CHOICE(DLT_SUNATM, "Sun raw ATM"),
400 DLT_CHOICE(DLT_IEEE802_11_RADIO, "802.11 plus BSD radio information header"),
401 DLT_CHOICE(DLT_APPLE_IP_OVER_IEEE1394, "Apple IP-over-IEEE 1394"),
402 DLT_CHOICE(DLT_ARCNET_LINUX, "Linux ARCNET"),
403 DLT_CHOICE(DLT_DOCSIS, "DOCSIS"),
404 DLT_CHOICE(DLT_LINUX_IRDA, "Linux IrDA"),
405 DLT_CHOICE(DLT_LINUX_LAPD, "Linux vISDN LAPD"),
406 DLT_CHOICE(DLT_IEEE802_11_RADIO_AVS, "802.11 plus AVS radio information header"),
407 DLT_CHOICE(DLT_SYMANTEC_FIREWALL, "Symantec Firewall"),
408 DLT_CHOICE(DLT_JUNIPER_ATM1, "Juniper ATM1 PIC"),
409 DLT_CHOICE(DLT_JUNIPER_ATM2, "Juniper ATM2 PIC"),
410 DLT_CHOICE(DLT_JUNIPER_MLPPP, "Juniper Multi-Link PPP"),
411 DLT_CHOICE(DLT_PPP_PPPD, "PPP for pppd, with direction flag"),
412 DLT_CHOICE(DLT_JUNIPER_PPPOE, "Juniper PPPoE"),
413 DLT_CHOICE(DLT_JUNIPER_PPPOE_ATM, "Juniper PPPoE/ATM"),
414 DLT_CHOICE(DLT_GPRS_LLC, "GPRS LLC"),
415 DLT_CHOICE(DLT_GPF_T, "GPF-T"),
416 DLT_CHOICE(DLT_GPF_F, "GPF-F"),
417 DLT_CHOICE(DLT_JUNIPER_PIC_PEER, "Juniper PIC Peer"),
418 DLT_CHOICE(DLT_JUNIPER_MLFR, "Juniper Multi-Link Frame Relay"),
419 DLT_CHOICE(DLT_ERF_ETH, "Ethernet with Endace ERF header"),
420 DLT_CHOICE(DLT_ERF_POS, "Packet-over-SONET with Endace ERF header"),
421 DLT_CHOICE(DLT_JUNIPER_GGSN, "Juniper GGSN PIC"),
422 DLT_CHOICE(DLT_JUNIPER_ES, "Juniper Encryption Services PIC"),
423 DLT_CHOICE(DLT_JUNIPER_MONITOR, "Juniper Passive Monitor PIC"),
424 DLT_CHOICE(DLT_JUNIPER_SERVICES, "Juniper Advanced Services PIC"),
425 DLT_CHOICE(DLT_JUNIPER_MFR, "Juniper FRF.16 Frame Relay"),
426 DLT_CHOICE(DLT_JUNIPER_ETHER, "Juniper Ethernet"),
427 DLT_CHOICE(DLT_JUNIPER_PPP, "Juniper PPP"),
428 DLT_CHOICE(DLT_JUNIPER_FRELAY, "Juniper Frame Relay"),
429 DLT_CHOICE(DLT_JUNIPER_CHDLC, "Juniper C-HDLC"),
430 DLT_CHOICE(DLT_MFR, "FRF.16 Frame Relay"),
431 DLT_CHOICE(DLT_JUNIPER_VP, "Juniper Voice PIC"),
432 DLT_CHOICE(DLT_A429, "Arinc 429"),
433 DLT_CHOICE(DLT_CAN20B, "Controller Area Network (CAN) v. 2.0B"),
434 DLT_CHOICE(DLT_A653_ICM, "Arinc 653 Interpartition Communication"),
435 DLT_CHOICE_SENTINEL
439 * This array is designed for mapping upper and lower case letter
440 * together for a case independent comparison. The mappings are
441 * based upon ascii character sequences.
443 static const u_char charmap[] = {
444 (u_char)'\000', (u_char)'\001', (u_char)'\002', (u_char)'\003',
445 (u_char)'\004', (u_char)'\005', (u_char)'\006', (u_char)'\007',
446 (u_char)'\010', (u_char)'\011', (u_char)'\012', (u_char)'\013',
447 (u_char)'\014', (u_char)'\015', (u_char)'\016', (u_char)'\017',
448 (u_char)'\020', (u_char)'\021', (u_char)'\022', (u_char)'\023',
449 (u_char)'\024', (u_char)'\025', (u_char)'\026', (u_char)'\027',
450 (u_char)'\030', (u_char)'\031', (u_char)'\032', (u_char)'\033',
451 (u_char)'\034', (u_char)'\035', (u_char)'\036', (u_char)'\037',
452 (u_char)'\040', (u_char)'\041', (u_char)'\042', (u_char)'\043',
453 (u_char)'\044', (u_char)'\045', (u_char)'\046', (u_char)'\047',
454 (u_char)'\050', (u_char)'\051', (u_char)'\052', (u_char)'\053',
455 (u_char)'\054', (u_char)'\055', (u_char)'\056', (u_char)'\057',
456 (u_char)'\060', (u_char)'\061', (u_char)'\062', (u_char)'\063',
457 (u_char)'\064', (u_char)'\065', (u_char)'\066', (u_char)'\067',
458 (u_char)'\070', (u_char)'\071', (u_char)'\072', (u_char)'\073',
459 (u_char)'\074', (u_char)'\075', (u_char)'\076', (u_char)'\077',
460 (u_char)'\100', (u_char)'\141', (u_char)'\142', (u_char)'\143',
461 (u_char)'\144', (u_char)'\145', (u_char)'\146', (u_char)'\147',
462 (u_char)'\150', (u_char)'\151', (u_char)'\152', (u_char)'\153',
463 (u_char)'\154', (u_char)'\155', (u_char)'\156', (u_char)'\157',
464 (u_char)'\160', (u_char)'\161', (u_char)'\162', (u_char)'\163',
465 (u_char)'\164', (u_char)'\165', (u_char)'\166', (u_char)'\167',
466 (u_char)'\170', (u_char)'\171', (u_char)'\172', (u_char)'\133',
467 (u_char)'\134', (u_char)'\135', (u_char)'\136', (u_char)'\137',
468 (u_char)'\140', (u_char)'\141', (u_char)'\142', (u_char)'\143',
469 (u_char)'\144', (u_char)'\145', (u_char)'\146', (u_char)'\147',
470 (u_char)'\150', (u_char)'\151', (u_char)'\152', (u_char)'\153',
471 (u_char)'\154', (u_char)'\155', (u_char)'\156', (u_char)'\157',
472 (u_char)'\160', (u_char)'\161', (u_char)'\162', (u_char)'\163',
473 (u_char)'\164', (u_char)'\165', (u_char)'\166', (u_char)'\167',
474 (u_char)'\170', (u_char)'\171', (u_char)'\172', (u_char)'\173',
475 (u_char)'\174', (u_char)'\175', (u_char)'\176', (u_char)'\177',
476 (u_char)'\200', (u_char)'\201', (u_char)'\202', (u_char)'\203',
477 (u_char)'\204', (u_char)'\205', (u_char)'\206', (u_char)'\207',
478 (u_char)'\210', (u_char)'\211', (u_char)'\212', (u_char)'\213',
479 (u_char)'\214', (u_char)'\215', (u_char)'\216', (u_char)'\217',
480 (u_char)'\220', (u_char)'\221', (u_char)'\222', (u_char)'\223',
481 (u_char)'\224', (u_char)'\225', (u_char)'\226', (u_char)'\227',
482 (u_char)'\230', (u_char)'\231', (u_char)'\232', (u_char)'\233',
483 (u_char)'\234', (u_char)'\235', (u_char)'\236', (u_char)'\237',
484 (u_char)'\240', (u_char)'\241', (u_char)'\242', (u_char)'\243',
485 (u_char)'\244', (u_char)'\245', (u_char)'\246', (u_char)'\247',
486 (u_char)'\250', (u_char)'\251', (u_char)'\252', (u_char)'\253',
487 (u_char)'\254', (u_char)'\255', (u_char)'\256', (u_char)'\257',
488 (u_char)'\260', (u_char)'\261', (u_char)'\262', (u_char)'\263',
489 (u_char)'\264', (u_char)'\265', (u_char)'\266', (u_char)'\267',
490 (u_char)'\270', (u_char)'\271', (u_char)'\272', (u_char)'\273',
491 (u_char)'\274', (u_char)'\275', (u_char)'\276', (u_char)'\277',
492 (u_char)'\300', (u_char)'\341', (u_char)'\342', (u_char)'\343',
493 (u_char)'\344', (u_char)'\345', (u_char)'\346', (u_char)'\347',
494 (u_char)'\350', (u_char)'\351', (u_char)'\352', (u_char)'\353',
495 (u_char)'\354', (u_char)'\355', (u_char)'\356', (u_char)'\357',
496 (u_char)'\360', (u_char)'\361', (u_char)'\362', (u_char)'\363',
497 (u_char)'\364', (u_char)'\365', (u_char)'\366', (u_char)'\367',
498 (u_char)'\370', (u_char)'\371', (u_char)'\372', (u_char)'\333',
499 (u_char)'\334', (u_char)'\335', (u_char)'\336', (u_char)'\337',
500 (u_char)'\340', (u_char)'\341', (u_char)'\342', (u_char)'\343',
501 (u_char)'\344', (u_char)'\345', (u_char)'\346', (u_char)'\347',
502 (u_char)'\350', (u_char)'\351', (u_char)'\352', (u_char)'\353',
503 (u_char)'\354', (u_char)'\355', (u_char)'\356', (u_char)'\357',
504 (u_char)'\360', (u_char)'\361', (u_char)'\362', (u_char)'\363',
505 (u_char)'\364', (u_char)'\365', (u_char)'\366', (u_char)'\367',
506 (u_char)'\370', (u_char)'\371', (u_char)'\372', (u_char)'\373',
507 (u_char)'\374', (u_char)'\375', (u_char)'\376', (u_char)'\377',
511 pcap_strcasecmp(const char *s1, const char *s2)
513 register const u_char *cm = charmap,
514 *us1 = (u_char *)s1,
515 *us2 = (u_char *)s2;
517 while (cm[*us1] == cm[*us2++])
518 if (*us1++ == '\0')
519 return(0);
520 return (cm[*us1] - cm[*--us2]);
524 pcap_datalink_name_to_val(const char *name)
526 int i;
528 for (i = 0; dlt_choices[i].name != NULL; i++) {
529 if (pcap_strcasecmp(dlt_choices[i].name + sizeof("DLT_") - 1,
530 name) == 0)
531 return (dlt_choices[i].dlt);
533 return (-1);
536 const char *
537 pcap_datalink_val_to_name(int dlt)
539 int i;
541 for (i = 0; dlt_choices[i].name != NULL; i++) {
542 if (dlt_choices[i].dlt == dlt)
543 return (dlt_choices[i].name + sizeof("DLT_") - 1);
545 return (NULL);
548 const char *
549 pcap_datalink_val_to_description(int dlt)
551 int i;
553 for (i = 0; dlt_choices[i].name != NULL; i++) {
554 if (dlt_choices[i].dlt == dlt)
555 return (dlt_choices[i].description);
557 return (NULL);
561 pcap_snapshot(pcap_t *p)
563 return (p->snapshot);
567 pcap_is_swapped(pcap_t *p)
569 return (p->sf.swapped);
573 pcap_major_version(pcap_t *p)
575 return (p->sf.version_major);
579 pcap_minor_version(pcap_t *p)
581 return (p->sf.version_minor);
584 FILE *
585 pcap_file(pcap_t *p)
587 return (p->sf.rfile);
591 pcap_fileno(pcap_t *p)
593 #ifdef HAVE_REMOTE
594 if (p->rmt_clientside)
595 return(p->rmt_sockdata);
596 #endif /* HAVE_REMOTE */
598 #ifndef WIN32
599 return (p->fd);
600 #else
601 if (p->adapter != NULL)
602 return ((int)(DWORD)p->adapter->hFile);
603 else
604 return (-1);
605 #endif
608 #if !defined(WIN32) && !defined(MSDOS)
610 pcap_get_selectable_fd(pcap_t *p)
612 return (p->selectable_fd);
614 #endif
616 void
617 pcap_perror(pcap_t *p, char *prefix)
619 fprintf(stderr, "%s: %s\n", prefix, p->errbuf);
622 char *
623 pcap_geterr(pcap_t *p)
625 return (p->errbuf);
629 pcap_getnonblock(pcap_t *p, char *errbuf)
631 return p->getnonblock_op(p, errbuf);
635 * Get the current non-blocking mode setting, under the assumption that
636 * it's just the standard POSIX non-blocking flag.
638 * We don't look at "p->nonblock", in case somebody tweaked the FD
639 * directly.
641 #if !defined(WIN32) && !defined(MSDOS)
643 pcap_getnonblock_fd(pcap_t *p, char *errbuf)
645 int fdflags;
647 fdflags = fcntl(p->fd, F_GETFL, 0);
648 if (fdflags == -1) {
649 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s",
650 pcap_strerror(errno));
651 return (-1);
653 if (fdflags & O_NONBLOCK)
654 return (1);
655 else
656 return (0);
658 #endif
661 pcap_setnonblock(pcap_t *p, int nonblock, char *errbuf)
663 return p->setnonblock_op(p, nonblock, errbuf);
666 #if !defined(WIN32) && !defined(MSDOS)
668 * Set non-blocking mode, under the assumption that it's just the
669 * standard POSIX non-blocking flag. (This can be called by the
670 * per-platform non-blocking-mode routine if that routine also
671 * needs to do some additional work.)
674 pcap_setnonblock_fd(pcap_t *p, int nonblock, char *errbuf)
676 int fdflags;
678 fdflags = fcntl(p->fd, F_GETFL, 0);
679 if (fdflags == -1) {
680 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s",
681 pcap_strerror(errno));
682 return (-1);
684 if (nonblock)
685 fdflags |= O_NONBLOCK;
686 else
687 fdflags &= ~O_NONBLOCK;
688 if (fcntl(p->fd, F_SETFL, fdflags) == -1) {
689 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_SETFL: %s",
690 pcap_strerror(errno));
691 return (-1);
693 return (0);
695 #endif
697 #ifdef WIN32
699 * Generate a string for the last Win32-specific error (i.e. an error generated when
700 * calling a Win32 API).
701 * For errors occurred during standard C calls, we still use pcap_strerror()
703 char *
704 pcap_win32strerror(void)
706 DWORD error;
707 static char errbuf[PCAP_ERRBUF_SIZE+1];
708 int errlen;
709 char *p;
711 error = GetLastError();
712 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, 0, errbuf,
713 PCAP_ERRBUF_SIZE, NULL);
716 * "FormatMessage()" "helpfully" sticks CR/LF at the end of the
717 * message. Get rid of it.
719 errlen = strlen(errbuf);
720 if (errlen >= 2) {
721 errbuf[errlen - 1] = '\0';
722 errbuf[errlen - 2] = '\0';
724 p = strchr(errbuf, '\0');
725 snprintf (p, sizeof(errbuf)-(p-errbuf), " (%lu)", error);
726 return (errbuf);
728 #endif
731 * Not all systems have strerror().
733 char *
734 pcap_strerror(int errnum)
736 #ifdef HAVE_STRERROR
737 return (strerror(errnum));
738 #else
739 extern int sys_nerr;
740 extern const char *const sys_errlist[];
741 static char ebuf[20];
743 if ((unsigned int)errnum < sys_nerr)
744 return ((char *)sys_errlist[errnum]);
745 (void)snprintf(ebuf, sizeof ebuf, "Unknown error: %d", errnum);
746 return(ebuf);
747 #endif
751 pcap_setfilter(pcap_t *p, struct bpf_program *fp)
753 return p->setfilter_op(p, fp);
757 * Set direction flag, which controls whether we accept only incoming
758 * packets, only outgoing packets, or both.
759 * Note that, depending on the platform, some or all direction arguments
760 * might not be supported.
763 pcap_setdirection(pcap_t *p, pcap_direction_t d)
765 if (p->setdirection_op == NULL) {
766 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
767 "Setting direction is not implemented on this platform");
768 return -1;
769 } else
770 return p->setdirection_op(p, d);
774 pcap_stats(pcap_t *p, struct pcap_stat *ps)
776 return p->stats_op(p, ps);
779 static int
780 pcap_stats_dead(pcap_t *p, struct pcap_stat *ps _U_)
782 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
783 "Statistics aren't available from a pcap_open_dead pcap_t");
784 return (-1);
787 void
788 pcap_close_common(pcap_t *p)
790 if (p->buffer != NULL)
791 free(p->buffer);
792 #if !defined(WIN32) && !defined(MSDOS)
793 if (p->fd >= 0)
794 close(p->fd);
795 #endif
798 static void
799 pcap_close_dead(pcap_t *p _U_)
801 /* Nothing to do. */
804 pcap_t *
805 pcap_open_dead(int linktype, int snaplen)
807 pcap_t *p;
809 p = malloc(sizeof(*p));
810 if (p == NULL)
811 return NULL;
812 memset (p, 0, sizeof(*p));
813 p->snapshot = snaplen;
814 p->linktype = linktype;
815 p->stats_op = pcap_stats_dead;
816 p->close_op = pcap_close_dead;
817 return p;
821 * API compatible with WinPcap's "send a packet" routine - returns -1
822 * on error, 0 otherwise.
824 * XXX - what if we get a short write?
827 pcap_sendpacket(pcap_t *p, const u_char *buf, int size)
829 if (p->inject_op(p, buf, size) == -1)
830 return (-1);
831 return (0);
835 * API compatible with OpenBSD's "send a packet" routine - returns -1 on
836 * error, number of bytes written otherwise.
839 pcap_inject(pcap_t *p, const void *buf, size_t size)
841 return (p->inject_op(p, buf, size));
844 void
845 pcap_close(pcap_t *p)
847 p->close_op(p);
848 if (p->dlt_list != NULL)
849 free(p->dlt_list);
850 pcap_freecode(&p->fcode);
851 free(p);
855 * We make the version string static, and return a pointer to it, rather
856 * than exporting the version string directly. On at least some UNIXes,
857 * if you import data from a shared library into an program, the data is
858 * bound into the program binary, so if the string in the version of the
859 * library with which the program was linked isn't the same as the
860 * string in the version of the library with which the program is being
861 * run, various undesirable things may happen (warnings, the string
862 * being the one from the version of the library with which the program
863 * was linked, or even weirder things, such as the string being the one
864 * from the library but being truncated).
866 #ifdef HAVE_VERSION_H
867 #include "version.h"
868 #else
869 static const char pcap_version_string[] = "libpcap version 0.9.5";
870 #endif
872 #ifdef WIN32
874 * XXX - it'd be nice if we could somehow generate the WinPcap and libpcap
875 * version numbers when building WinPcap. (It'd be nice to do so for
876 * the packet.dll version number as well.)
878 static const char wpcap_version_string[] = "4.0.2";
879 static const char pcap_version_string_fmt[] =
880 "WinPcap version %s, based on %s";
881 static const char pcap_version_string_packet_dll_fmt[] =
882 "WinPcap version %s (packet.dll version %s), based on %s";
883 static char *full_pcap_version_string;
885 const char *
886 pcap_lib_version(void)
888 char *packet_version_string;
889 size_t full_pcap_version_string_len;
891 if (full_pcap_version_string == NULL) {
893 * Generate the version string.
895 packet_version_string = PacketGetVersion();
896 if (strcmp(wpcap_version_string, packet_version_string) == 0) {
898 * WinPcap version string and packet.dll version
899 * string are the same; just report the WinPcap
900 * version.
902 full_pcap_version_string_len =
903 (sizeof pcap_version_string_fmt - 4) +
904 strlen(wpcap_version_string) +
905 strlen(pcap_version_string);
906 full_pcap_version_string =
907 malloc(full_pcap_version_string_len);
908 sprintf(full_pcap_version_string,
909 pcap_version_string_fmt, wpcap_version_string,
910 pcap_version_string);
911 } else {
913 * WinPcap version string and packet.dll version
914 * string are different; that shouldn't be the
915 * case (the two libraries should come from the
916 * same version of WinPcap), so we report both
917 * versions.
919 full_pcap_version_string_len =
920 (sizeof pcap_version_string_packet_dll_fmt - 6) +
921 strlen(wpcap_version_string) +
922 strlen(packet_version_string) +
923 strlen(pcap_version_string);
924 full_pcap_version_string = malloc(full_pcap_version_string_len);
926 sprintf(full_pcap_version_string,
927 pcap_version_string_packet_dll_fmt,
928 wpcap_version_string, packet_version_string,
929 pcap_version_string);
932 return (full_pcap_version_string);
935 #elif defined(MSDOS)
937 static char *full_pcap_version_string;
939 const char *
940 pcap_lib_version (void)
942 char *packet_version_string;
943 size_t full_pcap_version_string_len;
944 static char dospfx[] = "DOS-";
946 if (full_pcap_version_string == NULL) {
948 * Generate the version string.
950 full_pcap_version_string_len =
951 sizeof dospfx + strlen(pcap_version_string);
952 full_pcap_version_string =
953 malloc(full_pcap_version_string_len);
954 strcpy(full_pcap_version_string, dospfx);
955 strcat(full_pcap_version_string, pcap_version_string);
957 return (full_pcap_version_string);
960 #else /* UN*X */
962 const char *
963 pcap_lib_version(void)
965 return (pcap_version_string);
967 #endif