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
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
35 static const char rcsid
[] _U_
=
36 "@(#) $Header: /tcpdump/master/libpcap/pcap.c,v 1.112.2.3 2008-04-04 19:39:06 guy Exp $ (LBL)";
44 #include <pcap-stdinc.h>
46 #include <sys/types.h>
52 #if !defined(_MSC_VER) && !defined(__BORLANDC__)
58 #ifdef HAVE_OS_PROTO_H
74 pcap_not_initialized(pcap_t
*pcap
)
76 /* this means 'not initialized' */
77 return PCAP_ERROR_NOT_ACTIVATED
;
81 * Returns 1 if rfmon mode can be set on the pcap_t, 0 if it can't,
82 * a PCAP_ERROR value on an error.
85 pcap_can_set_rfmon(pcap_t
*p
)
87 return (p
->can_set_rfmon_op(p
));
91 * For systems where rfmon mode is never supported.
94 pcap_cant_set_rfmon(pcap_t
*p _U_
)
100 pcap_create_common(const char *source
, char *ebuf
)
104 p
= malloc(sizeof(*p
));
106 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
107 pcap_strerror(errno
));
110 memset(p
, 0, sizeof(*p
));
112 p
->opt
.source
= strdup(source
);
113 if (p
->opt
.source
== NULL
) {
114 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
115 pcap_strerror(errno
));
121 * Default to "can't set rfmon mode"; if it's supported by
122 * a platform, it can set the op to its routine to check
123 * whether a particular device supports it.
125 p
->can_set_rfmon_op
= pcap_cant_set_rfmon
;
128 * Some operations can be performed only on activated pcap_t's;
129 * have those operations handled by a "not supported" handler
130 * until the pcap_t is activated.
132 p
->read_op
= (read_op_t
)pcap_not_initialized
;
133 p
->inject_op
= (inject_op_t
)pcap_not_initialized
;
134 p
->setfilter_op
= (setfilter_op_t
)pcap_not_initialized
;
135 p
->setdirection_op
= (setdirection_op_t
)pcap_not_initialized
;
136 p
->set_datalink_op
= (set_datalink_op_t
)pcap_not_initialized
;
137 p
->getnonblock_op
= (getnonblock_op_t
)pcap_not_initialized
;
138 p
->setnonblock_op
= (setnonblock_op_t
)pcap_not_initialized
;
139 p
->stats_op
= (stats_op_t
)pcap_not_initialized
;
141 p
->setbuff_op
= (setbuff_op_t
)pcap_not_initialized
;
142 p
->setmode_op
= (setmode_op_t
)pcap_not_initialized
;
143 p
->setmintocopy_op
= (setmintocopy_op_t
)pcap_not_initialized
;
145 p
->close_op
= (close_op_t
)pcap_close_common
;
147 /* put in some defaults*/
148 pcap_set_timeout(p
, 0);
149 pcap_set_snaplen(p
, 65535); /* max packet size */
151 p
->opt
.buffer_size
= 0;
156 pcap_check_activated(pcap_t
*p
)
159 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "can't perform "
160 " operation on activated capture");
167 pcap_set_snaplen(pcap_t
*p
, int snaplen
)
169 if (pcap_check_activated(p
))
170 return PCAP_ERROR_ACTIVATED
;
171 p
->snapshot
= snaplen
;
176 pcap_set_promisc(pcap_t
*p
, int promisc
)
178 if (pcap_check_activated(p
))
179 return PCAP_ERROR_ACTIVATED
;
180 p
->opt
.promisc
= promisc
;
185 pcap_set_rfmon(pcap_t
*p
, int rfmon
)
187 if (pcap_check_activated(p
))
188 return PCAP_ERROR_ACTIVATED
;
189 p
->opt
.rfmon
= rfmon
;
194 pcap_set_timeout(pcap_t
*p
, int timeout_ms
)
196 if (pcap_check_activated(p
))
197 return PCAP_ERROR_ACTIVATED
;
198 p
->md
.timeout
= timeout_ms
;
203 pcap_set_buffer_size(pcap_t
*p
, int buffer_size
)
205 if (pcap_check_activated(p
))
206 return PCAP_ERROR_ACTIVATED
;
207 p
->opt
.buffer_size
= buffer_size
;
212 pcap_activate(pcap_t
*p
)
216 err
= p
->activate_op(p
);
223 pcap_open_live(const char *source
, int snaplen
, int promisc
, int to_ms
, char *errbuf
)
227 p
= pcap_create(source
, errbuf
);
230 if (pcap_set_snaplen(p
, snaplen
))
232 if (pcap_set_promisc(p
, promisc
))
234 if (pcap_set_timeout(p
, to_ms
))
237 * Mark this as opened with pcap_open_live(), so that, for
238 * example, we show the full list of DLT_ values, rather
239 * than just the ones that are compatible with capturing
240 * when not in monitor mode. That allows existing applications
241 * to work the way they used to work, but allows new applications
242 * that know about the new open API to, for example, find out the
243 * DLT_ values that they can select without changing whether
244 * the adapter is in monitor mode or not.
247 if (pcap_activate(p
))
251 strncpy(errbuf
, p
->errbuf
, PCAP_ERRBUF_SIZE
);
257 pcap_dispatch(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
259 return p
->read_op(p
, cnt
, callback
, user
);
263 * XXX - is this necessary?
266 pcap_read(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
269 return p
->read_op(p
, cnt
, callback
, user
);
273 pcap_loop(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
278 if (p
->sf
.rfile
!= NULL
) {
280 * 0 means EOF, so don't loop if we get 0.
282 n
= pcap_offline_read(p
, cnt
, callback
, user
);
285 * XXX keep reading until we get something
286 * (or an error occurs)
289 n
= p
->read_op(p
, cnt
, callback
, user
);
303 struct pcap_pkthdr
*hdr
;
309 pcap_oneshot(u_char
*userData
, const struct pcap_pkthdr
*h
, const u_char
*pkt
)
311 struct singleton
*sp
= (struct singleton
*)userData
;
317 pcap_next(pcap_t
*p
, struct pcap_pkthdr
*h
)
322 if (pcap_dispatch(p
, 1, pcap_oneshot
, (u_char
*)&s
) <= 0)
327 struct pkt_for_fakecallback
{
328 struct pcap_pkthdr
*hdr
;
333 pcap_fakecallback(u_char
*userData
, const struct pcap_pkthdr
*h
,
336 struct pkt_for_fakecallback
*sp
= (struct pkt_for_fakecallback
*)userData
;
343 pcap_next_ex(pcap_t
*p
, struct pcap_pkthdr
**pkt_header
,
344 const u_char
**pkt_data
)
346 struct pkt_for_fakecallback s
;
348 s
.hdr
= &p
->pcap_header
;
351 /* Saves a pointer to the packet headers */
352 *pkt_header
= &p
->pcap_header
;
354 if (p
->sf
.rfile
!= NULL
) {
357 /* We are on an offline capture */
358 status
= pcap_offline_read(p
, 1, pcap_fakecallback
,
362 * Return codes for pcap_offline_read() are:
366 * The first one ('0') conflicts with the return code of
367 * 0 from pcap_read() meaning "no packets arrived before
368 * the timeout expired", so we map it to -2 so you can
369 * distinguish between an EOF from a savefile and a
370 * "no packets arrived before the timeout expired, try
371 * again" from a live capture.
380 * Return codes for pcap_read() are:
383 * - -2: loop was broken out of with pcap_breakloop()
385 * The first one ('0') conflicts with the return code of 0 from
386 * pcap_offline_read() meaning "end of file".
388 return (p
->read_op(p
, 1, pcap_fakecallback
, (u_char
*)&s
));
392 * Force the loop in "pcap_read()" or "pcap_read_offline()" to terminate.
395 pcap_breakloop(pcap_t
*p
)
401 pcap_datalink(pcap_t
*p
)
403 return (p
->linktype
);
407 pcap_datalink_ext(pcap_t
*p
)
409 return (p
->linktype_ext
);
413 pcap_list_datalinks(pcap_t
*p
, int **dlt_buffer
)
415 if (p
->dlt_count
== 0) {
417 * We couldn't fetch the list of DLTs, which means
418 * this platform doesn't support changing the
419 * DLT for an interface. Return a list of DLTs
420 * containing only the DLT this device supports.
422 *dlt_buffer
= (int*)malloc(sizeof(**dlt_buffer
));
423 if (*dlt_buffer
== NULL
) {
424 (void)snprintf(p
->errbuf
, sizeof(p
->errbuf
),
425 "malloc: %s", pcap_strerror(errno
));
428 **dlt_buffer
= p
->linktype
;
431 *dlt_buffer
= (int*)calloc(sizeof(**dlt_buffer
), p
->dlt_count
);
432 if (*dlt_buffer
== NULL
) {
433 (void)snprintf(p
->errbuf
, sizeof(p
->errbuf
),
434 "malloc: %s", pcap_strerror(errno
));
437 (void)memcpy(*dlt_buffer
, p
->dlt_list
,
438 sizeof(**dlt_buffer
) * p
->dlt_count
);
439 return (p
->dlt_count
);
444 pcap_set_datalink(pcap_t
*p
, int dlt
)
447 const char *dlt_name
;
449 if (p
->dlt_count
== 0 || p
->set_datalink_op
== NULL
) {
451 * We couldn't fetch the list of DLTs, or we don't
452 * have a "set datalink" operation, which means
453 * this platform doesn't support changing the
454 * DLT for an interface. Check whether the new
455 * DLT is the one this interface supports.
457 if (p
->linktype
!= dlt
)
461 * It is, so there's nothing we need to do here.
465 for (i
= 0; i
< p
->dlt_count
; i
++)
466 if (p
->dlt_list
[i
] == dlt
)
468 if (i
>= p
->dlt_count
)
470 if (p
->dlt_count
== 2 && p
->dlt_list
[0] == DLT_EN10MB
&&
473 * This is presumably an Ethernet device, as the first
474 * link-layer type it offers is DLT_EN10MB, and the only
475 * other type it offers is DLT_DOCSIS. That means that
476 * we can't tell the driver to supply DOCSIS link-layer
477 * headers - we're just pretending that's what we're
478 * getting, as, presumably, we're capturing on a dedicated
479 * link to a Cisco Cable Modem Termination System, and
480 * it's putting raw DOCSIS frames on the wire inside low-level
486 if (p
->set_datalink_op(p
, dlt
) == -1)
492 dlt_name
= pcap_datalink_val_to_name(dlt
);
493 if (dlt_name
!= NULL
) {
494 (void) snprintf(p
->errbuf
, sizeof(p
->errbuf
),
495 "%s is not one of the DLTs supported by this device",
498 (void) snprintf(p
->errbuf
, sizeof(p
->errbuf
),
499 "DLT %d is not one of the DLTs supported by this device",
507 const char *description
;
511 #define DLT_CHOICE(code, description) { #code, description, code }
512 #define DLT_CHOICE_SENTINEL { NULL, NULL, 0 }
514 static struct dlt_choice dlt_choices
[] = {
515 DLT_CHOICE(DLT_NULL
, "BSD loopback"),
516 DLT_CHOICE(DLT_EN10MB
, "Ethernet"),
517 DLT_CHOICE(DLT_IEEE802
, "Token ring"),
518 DLT_CHOICE(DLT_ARCNET
, "BSD ARCNET"),
519 DLT_CHOICE(DLT_SLIP
, "SLIP"),
520 DLT_CHOICE(DLT_PPP
, "PPP"),
521 DLT_CHOICE(DLT_FDDI
, "FDDI"),
522 DLT_CHOICE(DLT_ATM_RFC1483
, "RFC 1483 LLC-encapsulated ATM"),
523 DLT_CHOICE(DLT_RAW
, "Raw IP"),
524 DLT_CHOICE(DLT_SLIP_BSDOS
, "BSD/OS SLIP"),
525 DLT_CHOICE(DLT_PPP_BSDOS
, "BSD/OS PPP"),
526 DLT_CHOICE(DLT_ATM_CLIP
, "Linux Classical IP-over-ATM"),
527 DLT_CHOICE(DLT_PPP_SERIAL
, "PPP over serial"),
528 DLT_CHOICE(DLT_PPP_ETHER
, "PPPoE"),
529 DLT_CHOICE(DLT_SYMANTEC_FIREWALL
, "Symantec Firewall"),
530 DLT_CHOICE(DLT_C_HDLC
, "Cisco HDLC"),
531 DLT_CHOICE(DLT_IEEE802_11
, "802.11"),
532 DLT_CHOICE(DLT_FRELAY
, "Frame Relay"),
533 DLT_CHOICE(DLT_LOOP
, "OpenBSD loopback"),
534 DLT_CHOICE(DLT_ENC
, "OpenBSD encapsulated IP"),
535 DLT_CHOICE(DLT_LINUX_SLL
, "Linux cooked"),
536 DLT_CHOICE(DLT_LTALK
, "Localtalk"),
537 DLT_CHOICE(DLT_PFLOG
, "OpenBSD pflog file"),
538 DLT_CHOICE(DLT_PRISM_HEADER
, "802.11 plus Prism header"),
539 DLT_CHOICE(DLT_IP_OVER_FC
, "RFC 2625 IP-over-Fibre Channel"),
540 DLT_CHOICE(DLT_SUNATM
, "Sun raw ATM"),
541 DLT_CHOICE(DLT_IEEE802_11_RADIO
, "802.11 plus radiotap header"),
542 DLT_CHOICE(DLT_ARCNET_LINUX
, "Linux ARCNET"),
543 DLT_CHOICE(DLT_JUNIPER_MLPPP
, "Juniper Multi-Link PPP"),
544 DLT_CHOICE(DLT_JUNIPER_MLFR
, "Juniper Multi-Link Frame Relay"),
545 DLT_CHOICE(DLT_JUNIPER_ES
, "Juniper Encryption Services PIC"),
546 DLT_CHOICE(DLT_JUNIPER_GGSN
, "Juniper GGSN PIC"),
547 DLT_CHOICE(DLT_JUNIPER_MFR
, "Juniper FRF.16 Frame Relay"),
548 DLT_CHOICE(DLT_JUNIPER_ATM2
, "Juniper ATM2 PIC"),
549 DLT_CHOICE(DLT_JUNIPER_SERVICES
, "Juniper Advanced Services PIC"),
550 DLT_CHOICE(DLT_JUNIPER_ATM1
, "Juniper ATM1 PIC"),
551 DLT_CHOICE(DLT_APPLE_IP_OVER_IEEE1394
, "Apple IP-over-IEEE 1394"),
552 DLT_CHOICE(DLT_MTP2_WITH_PHDR
, "SS7 MTP2 with Pseudo-header"),
553 DLT_CHOICE(DLT_MTP2
, "SS7 MTP2"),
554 DLT_CHOICE(DLT_MTP3
, "SS7 MTP3"),
555 DLT_CHOICE(DLT_SCCP
, "SS7 SCCP"),
556 DLT_CHOICE(DLT_DOCSIS
, "DOCSIS"),
557 DLT_CHOICE(DLT_LINUX_IRDA
, "Linux IrDA"),
558 DLT_CHOICE(DLT_IEEE802_11_RADIO_AVS
, "802.11 plus AVS radio information header"),
559 DLT_CHOICE(DLT_JUNIPER_MONITOR
, "Juniper Passive Monitor PIC"),
560 DLT_CHOICE(DLT_PPP_PPPD
, "PPP for pppd, with direction flag"),
561 DLT_CHOICE(DLT_JUNIPER_PPPOE
, "Juniper PPPoE"),
562 DLT_CHOICE(DLT_JUNIPER_PPPOE_ATM
, "Juniper PPPoE/ATM"),
563 DLT_CHOICE(DLT_GPRS_LLC
, "GPRS LLC"),
564 DLT_CHOICE(DLT_GPF_T
, "GPF-T"),
565 DLT_CHOICE(DLT_GPF_F
, "GPF-F"),
566 DLT_CHOICE(DLT_JUNIPER_PIC_PEER
, "Juniper PIC Peer"),
567 DLT_CHOICE(DLT_ERF_ETH
, "Ethernet with Endace ERF header"),
568 DLT_CHOICE(DLT_ERF_POS
, "Packet-over-SONET with Endace ERF header"),
569 DLT_CHOICE(DLT_LINUX_LAPD
, "Linux vISDN LAPD"),
570 DLT_CHOICE(DLT_JUNIPER_ETHER
, "Juniper Ethernet"),
571 DLT_CHOICE(DLT_JUNIPER_PPP
, "Juniper PPP"),
572 DLT_CHOICE(DLT_JUNIPER_FRELAY
, "Juniper Frame Relay"),
573 DLT_CHOICE(DLT_JUNIPER_CHDLC
, "Juniper C-HDLC"),
574 DLT_CHOICE(DLT_MFR
, "FRF.16 Frame Relay"),
575 DLT_CHOICE(DLT_JUNIPER_VP
, "Juniper Voice PIC"),
576 DLT_CHOICE(DLT_A429
, "Arinc 429"),
577 DLT_CHOICE(DLT_A653_ICM
, "Arinc 653 Interpartition Communication"),
578 DLT_CHOICE(DLT_USB
, "USB"),
579 DLT_CHOICE(DLT_BLUETOOTH_HCI_H4
, "Bluetooth HCI UART transport layer"),
580 DLT_CHOICE(DLT_IEEE802_16_MAC_CPS
, "IEEE 802.16 MAC Common Part Sublayer"),
581 DLT_CHOICE(DLT_USB_LINUX
, "USB with Linux header"),
582 DLT_CHOICE(DLT_CAN20B
, "Controller Area Network (CAN) v. 2.0B"),
583 DLT_CHOICE(DLT_IEEE802_15_4_LINUX
, "IEEE 802.15.4 with Linux padding"),
584 DLT_CHOICE(DLT_PPI
, "Per-Packet Information"),
585 DLT_CHOICE(DLT_IEEE802_16_MAC_CPS_RADIO
, "IEEE 802.16 MAC Common Part Sublayer plus radiotap header"),
586 DLT_CHOICE(DLT_JUNIPER_ISM
, "Juniper Integrated Service Module"),
587 DLT_CHOICE(DLT_IEEE802_15_4
, "IEEE 802.15.4"),
588 DLT_CHOICE(DLT_SITA
, "SITA pseudo-header"),
589 DLT_CHOICE(DLT_ERF
, "Endace ERF header"),
590 DLT_CHOICE(DLT_RAIF1
, "Ethernet with u10 Networks pseudo-header"),
591 DLT_CHOICE(DLT_IPMB
, "IPMB"),
592 DLT_CHOICE(DLT_JUNIPER_ST
, "Juniper Secure Tunnel"),
593 DLT_CHOICE(DLT_BLUETOOTH_HCI_H4_WITH_PHDR
, "Bluetooth HCI UART transport layer plus pseudo-header"),
594 DLT_CHOICE(DLT_AX25_KISS
, "AX.25 with KISS header"),
599 * This array is designed for mapping upper and lower case letter
600 * together for a case independent comparison. The mappings are
601 * based upon ascii character sequences.
603 static const u_char charmap
[] = {
604 (u_char
)'\000', (u_char
)'\001', (u_char
)'\002', (u_char
)'\003',
605 (u_char
)'\004', (u_char
)'\005', (u_char
)'\006', (u_char
)'\007',
606 (u_char
)'\010', (u_char
)'\011', (u_char
)'\012', (u_char
)'\013',
607 (u_char
)'\014', (u_char
)'\015', (u_char
)'\016', (u_char
)'\017',
608 (u_char
)'\020', (u_char
)'\021', (u_char
)'\022', (u_char
)'\023',
609 (u_char
)'\024', (u_char
)'\025', (u_char
)'\026', (u_char
)'\027',
610 (u_char
)'\030', (u_char
)'\031', (u_char
)'\032', (u_char
)'\033',
611 (u_char
)'\034', (u_char
)'\035', (u_char
)'\036', (u_char
)'\037',
612 (u_char
)'\040', (u_char
)'\041', (u_char
)'\042', (u_char
)'\043',
613 (u_char
)'\044', (u_char
)'\045', (u_char
)'\046', (u_char
)'\047',
614 (u_char
)'\050', (u_char
)'\051', (u_char
)'\052', (u_char
)'\053',
615 (u_char
)'\054', (u_char
)'\055', (u_char
)'\056', (u_char
)'\057',
616 (u_char
)'\060', (u_char
)'\061', (u_char
)'\062', (u_char
)'\063',
617 (u_char
)'\064', (u_char
)'\065', (u_char
)'\066', (u_char
)'\067',
618 (u_char
)'\070', (u_char
)'\071', (u_char
)'\072', (u_char
)'\073',
619 (u_char
)'\074', (u_char
)'\075', (u_char
)'\076', (u_char
)'\077',
620 (u_char
)'\100', (u_char
)'\141', (u_char
)'\142', (u_char
)'\143',
621 (u_char
)'\144', (u_char
)'\145', (u_char
)'\146', (u_char
)'\147',
622 (u_char
)'\150', (u_char
)'\151', (u_char
)'\152', (u_char
)'\153',
623 (u_char
)'\154', (u_char
)'\155', (u_char
)'\156', (u_char
)'\157',
624 (u_char
)'\160', (u_char
)'\161', (u_char
)'\162', (u_char
)'\163',
625 (u_char
)'\164', (u_char
)'\165', (u_char
)'\166', (u_char
)'\167',
626 (u_char
)'\170', (u_char
)'\171', (u_char
)'\172', (u_char
)'\133',
627 (u_char
)'\134', (u_char
)'\135', (u_char
)'\136', (u_char
)'\137',
628 (u_char
)'\140', (u_char
)'\141', (u_char
)'\142', (u_char
)'\143',
629 (u_char
)'\144', (u_char
)'\145', (u_char
)'\146', (u_char
)'\147',
630 (u_char
)'\150', (u_char
)'\151', (u_char
)'\152', (u_char
)'\153',
631 (u_char
)'\154', (u_char
)'\155', (u_char
)'\156', (u_char
)'\157',
632 (u_char
)'\160', (u_char
)'\161', (u_char
)'\162', (u_char
)'\163',
633 (u_char
)'\164', (u_char
)'\165', (u_char
)'\166', (u_char
)'\167',
634 (u_char
)'\170', (u_char
)'\171', (u_char
)'\172', (u_char
)'\173',
635 (u_char
)'\174', (u_char
)'\175', (u_char
)'\176', (u_char
)'\177',
636 (u_char
)'\200', (u_char
)'\201', (u_char
)'\202', (u_char
)'\203',
637 (u_char
)'\204', (u_char
)'\205', (u_char
)'\206', (u_char
)'\207',
638 (u_char
)'\210', (u_char
)'\211', (u_char
)'\212', (u_char
)'\213',
639 (u_char
)'\214', (u_char
)'\215', (u_char
)'\216', (u_char
)'\217',
640 (u_char
)'\220', (u_char
)'\221', (u_char
)'\222', (u_char
)'\223',
641 (u_char
)'\224', (u_char
)'\225', (u_char
)'\226', (u_char
)'\227',
642 (u_char
)'\230', (u_char
)'\231', (u_char
)'\232', (u_char
)'\233',
643 (u_char
)'\234', (u_char
)'\235', (u_char
)'\236', (u_char
)'\237',
644 (u_char
)'\240', (u_char
)'\241', (u_char
)'\242', (u_char
)'\243',
645 (u_char
)'\244', (u_char
)'\245', (u_char
)'\246', (u_char
)'\247',
646 (u_char
)'\250', (u_char
)'\251', (u_char
)'\252', (u_char
)'\253',
647 (u_char
)'\254', (u_char
)'\255', (u_char
)'\256', (u_char
)'\257',
648 (u_char
)'\260', (u_char
)'\261', (u_char
)'\262', (u_char
)'\263',
649 (u_char
)'\264', (u_char
)'\265', (u_char
)'\266', (u_char
)'\267',
650 (u_char
)'\270', (u_char
)'\271', (u_char
)'\272', (u_char
)'\273',
651 (u_char
)'\274', (u_char
)'\275', (u_char
)'\276', (u_char
)'\277',
652 (u_char
)'\300', (u_char
)'\341', (u_char
)'\342', (u_char
)'\343',
653 (u_char
)'\344', (u_char
)'\345', (u_char
)'\346', (u_char
)'\347',
654 (u_char
)'\350', (u_char
)'\351', (u_char
)'\352', (u_char
)'\353',
655 (u_char
)'\354', (u_char
)'\355', (u_char
)'\356', (u_char
)'\357',
656 (u_char
)'\360', (u_char
)'\361', (u_char
)'\362', (u_char
)'\363',
657 (u_char
)'\364', (u_char
)'\365', (u_char
)'\366', (u_char
)'\367',
658 (u_char
)'\370', (u_char
)'\371', (u_char
)'\372', (u_char
)'\333',
659 (u_char
)'\334', (u_char
)'\335', (u_char
)'\336', (u_char
)'\337',
660 (u_char
)'\340', (u_char
)'\341', (u_char
)'\342', (u_char
)'\343',
661 (u_char
)'\344', (u_char
)'\345', (u_char
)'\346', (u_char
)'\347',
662 (u_char
)'\350', (u_char
)'\351', (u_char
)'\352', (u_char
)'\353',
663 (u_char
)'\354', (u_char
)'\355', (u_char
)'\356', (u_char
)'\357',
664 (u_char
)'\360', (u_char
)'\361', (u_char
)'\362', (u_char
)'\363',
665 (u_char
)'\364', (u_char
)'\365', (u_char
)'\366', (u_char
)'\367',
666 (u_char
)'\370', (u_char
)'\371', (u_char
)'\372', (u_char
)'\373',
667 (u_char
)'\374', (u_char
)'\375', (u_char
)'\376', (u_char
)'\377',
671 pcap_strcasecmp(const char *s1
, const char *s2
)
673 register const u_char
*cm
= charmap
,
674 *us1
= (const u_char
*)s1
,
675 *us2
= (const u_char
*)s2
;
677 while (cm
[*us1
] == cm
[*us2
++])
680 return (cm
[*us1
] - cm
[*--us2
]);
684 pcap_datalink_name_to_val(const char *name
)
688 for (i
= 0; dlt_choices
[i
].name
!= NULL
; i
++) {
689 if (pcap_strcasecmp(dlt_choices
[i
].name
+ sizeof("DLT_") - 1,
691 return (dlt_choices
[i
].dlt
);
697 pcap_datalink_val_to_name(int dlt
)
701 for (i
= 0; dlt_choices
[i
].name
!= NULL
; i
++) {
702 if (dlt_choices
[i
].dlt
== dlt
)
703 return (dlt_choices
[i
].name
+ sizeof("DLT_") - 1);
709 pcap_datalink_val_to_description(int dlt
)
713 for (i
= 0; dlt_choices
[i
].name
!= NULL
; i
++) {
714 if (dlt_choices
[i
].dlt
== dlt
)
715 return (dlt_choices
[i
].description
);
721 pcap_snapshot(pcap_t
*p
)
723 return (p
->snapshot
);
727 pcap_is_swapped(pcap_t
*p
)
729 return (p
->sf
.swapped
);
733 pcap_major_version(pcap_t
*p
)
735 return (p
->sf
.version_major
);
739 pcap_minor_version(pcap_t
*p
)
741 return (p
->sf
.version_minor
);
747 return (p
->sf
.rfile
);
751 pcap_fileno(pcap_t
*p
)
756 if (p
->adapter
!= NULL
)
757 return ((int)(DWORD
)p
->adapter
->hFile
);
763 #if !defined(WIN32) && !defined(MSDOS)
765 pcap_get_selectable_fd(pcap_t
*p
)
767 return (p
->selectable_fd
);
772 pcap_perror(pcap_t
*p
, char *prefix
)
774 fprintf(stderr
, "%s: %s\n", prefix
, p
->errbuf
);
778 pcap_geterr(pcap_t
*p
)
784 pcap_getnonblock(pcap_t
*p
, char *errbuf
)
786 return p
->getnonblock_op(p
, errbuf
);
790 * Get the current non-blocking mode setting, under the assumption that
791 * it's just the standard POSIX non-blocking flag.
793 * We don't look at "p->nonblock", in case somebody tweaked the FD
796 #if !defined(WIN32) && !defined(MSDOS)
798 pcap_getnonblock_fd(pcap_t
*p
, char *errbuf
)
802 fdflags
= fcntl(p
->fd
, F_GETFL
, 0);
804 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "F_GETFL: %s",
805 pcap_strerror(errno
));
808 if (fdflags
& O_NONBLOCK
)
816 pcap_setnonblock(pcap_t
*p
, int nonblock
, char *errbuf
)
818 return p
->setnonblock_op(p
, nonblock
, errbuf
);
821 #if !defined(WIN32) && !defined(MSDOS)
823 * Set non-blocking mode, under the assumption that it's just the
824 * standard POSIX non-blocking flag. (This can be called by the
825 * per-platform non-blocking-mode routine if that routine also
826 * needs to do some additional work.)
829 pcap_setnonblock_fd(pcap_t
*p
, int nonblock
, char *errbuf
)
833 fdflags
= fcntl(p
->fd
, F_GETFL
, 0);
835 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "F_GETFL: %s",
836 pcap_strerror(errno
));
840 fdflags
|= O_NONBLOCK
;
842 fdflags
&= ~O_NONBLOCK
;
843 if (fcntl(p
->fd
, F_SETFL
, fdflags
) == -1) {
844 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "F_SETFL: %s",
845 pcap_strerror(errno
));
854 * Generate a string for the last Win32-specific error (i.e. an error generated when
855 * calling a Win32 API).
856 * For errors occurred during standard C calls, we still use pcap_strerror()
859 pcap_win32strerror(void)
862 static char errbuf
[PCAP_ERRBUF_SIZE
+1];
866 error
= GetLastError();
867 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
, NULL
, error
, 0, errbuf
,
868 PCAP_ERRBUF_SIZE
, NULL
);
871 * "FormatMessage()" "helpfully" sticks CR/LF at the end of the
872 * message. Get rid of it.
874 errlen
= strlen(errbuf
);
876 errbuf
[errlen
- 1] = '\0';
877 errbuf
[errlen
- 2] = '\0';
879 p
= strchr(errbuf
, '\0');
880 snprintf (p
, sizeof(errbuf
)-(p
-errbuf
), " (%lu)", error
);
886 * Not all systems have strerror().
887 * We also use this to generate error strings for PCAP_ERROR_ values.
890 pcap_strerror(int errnum
)
892 static char ebuf
[128+1];
893 #ifndef HAVE_STRERROR
895 extern const char *const sys_errlist
[];
901 (void)snprintf(ebuf
, sizeof ebuf
, "Generic error");
904 case PCAP_ERROR_BREAK
:
905 (void)snprintf(ebuf
, sizeof ebuf
, "Loop terminated by pcap_breakloop");
908 case PCAP_ERROR_NOT_ACTIVATED
:
909 (void)snprintf(ebuf
, sizeof ebuf
, "The pcap_t has not been activated");
912 case PCAP_ERROR_ACTIVATED
:
913 (void)snprintf(ebuf
, sizeof ebuf
, "The setting can't be changed after the pcap_t is activated");
916 case PCAP_ERROR_NO_SUCH_DEVICE
:
917 (void)snprintf(ebuf
, sizeof ebuf
, "No such device exists");
920 case PCAP_ERROR_RFMON_NOTSUP
:
921 (void)snprintf(ebuf
, sizeof ebuf
, "That device doesn't support monitor mode");
924 case PCAP_ERROR_NOT_RFMON
:
925 (void)snprintf(ebuf
, sizeof ebuf
, "That operation is supported only in monitor mode");
930 return (strerror(errnum
));
932 if ((unsigned int)errnum
< sys_nerr
)
933 return ((char *)sys_errlist
[errnum
]);
936 (void)snprintf(ebuf
, sizeof ebuf
, "Unknown error: %d", errnum
);
941 pcap_setfilter(pcap_t
*p
, struct bpf_program
*fp
)
943 return p
->setfilter_op(p
, fp
);
947 * Set direction flag, which controls whether we accept only incoming
948 * packets, only outgoing packets, or both.
949 * Note that, depending on the platform, some or all direction arguments
950 * might not be supported.
953 pcap_setdirection(pcap_t
*p
, pcap_direction_t d
)
955 if (p
->setdirection_op
== NULL
) {
956 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
957 "Setting direction is not implemented on this platform");
960 return p
->setdirection_op(p
, d
);
964 pcap_stats(pcap_t
*p
, struct pcap_stat
*ps
)
966 return p
->stats_op(p
, ps
);
970 pcap_stats_dead(pcap_t
*p
, struct pcap_stat
*ps _U_
)
972 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
973 "Statistics aren't available from a pcap_open_dead pcap_t");
979 pcap_setbuff(pcap_t
*p
, int dim
)
981 return p
->setbuff_op(p
, dim
);
985 pcap_setbuff_dead(pcap_t
*p
, int dim
)
987 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
988 "The kernel buffer size cannot be set on a pcap_open_dead pcap_t");
993 pcap_setmode(pcap_t
*p
, int mode
)
995 return p
->setmode_op(p
, mode
);
999 pcap_setmode_dead(pcap_t
*p
, int mode
)
1001 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1002 "impossible to set mode on a pcap_open_dead pcap_t");
1007 pcap_setmintocopy(pcap_t
*p
, int size
)
1009 return p
->setmintocopy_op(p
, size
);
1013 pcap_setmintocopy_dead(pcap_t
*p
, int size
)
1015 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1016 "The mintocopy parameter cannot be set on a pcap_open_dead pcap_t");
1022 * On some platforms, we need to clean up promiscuous or monitor mode
1023 * when we close a device - and we want that to happen even if the
1024 * application just exits without explicitl closing devices.
1025 * On those platforms, we need to register a "close all the pcaps"
1026 * routine to be called when we exit, and need to maintain a list of
1027 * pcaps that need to be closed to clean up modes.
1029 * XXX - not thread-safe.
1033 * List of pcaps on which we've done something that needs to be
1035 * If there are any such pcaps, we arrange to call "pcap_close_all()"
1036 * when we exit, and have it close all of them.
1038 static struct pcap
*pcaps_to_close
;
1041 * TRUE if we've already called "atexit()" to cause "pcap_close_all()" to
1042 * be called on exit.
1044 static int did_atexit
;
1047 pcap_close_all(void)
1049 struct pcap
*handle
;
1051 while ((handle
= pcaps_to_close
) != NULL
)
1056 pcap_do_addexit(pcap_t
*p
)
1059 * If we haven't already done so, arrange to have
1060 * "pcap_close_all()" called when we exit.
1063 if (atexit(pcap_close_all
) == -1) {
1065 * "atexit()" failed; let our caller know.
1067 strncpy(p
->errbuf
, "atexit failed",
1077 pcap_add_to_pcaps_to_close(pcap_t
*p
)
1079 p
->md
.next
= pcaps_to_close
;
1084 pcap_remove_from_pcaps_to_close(pcap_t
*p
)
1086 pcap_t
*pc
, *prevpc
;
1088 for (pc
= pcaps_to_close
, prevpc
= NULL
; pc
!= NULL
;
1089 prevpc
= pc
, pc
= pc
->md
.next
) {
1092 * Found it. Remove it from the list.
1094 if (prevpc
== NULL
) {
1096 * It was at the head of the list.
1098 pcaps_to_close
= pc
->md
.next
;
1101 * It was in the middle of the list.
1103 prevpc
->md
.next
= pc
->md
.next
;
1111 pcap_close_common(pcap_t
*p
)
1113 if (p
->buffer
!= NULL
)
1115 if (p
->opt
.source
!= NULL
);
1116 free(p
->opt
.source
);
1117 #if !defined(WIN32) && !defined(MSDOS)
1124 pcap_close_dead(pcap_t
*p _U_
)
1126 /* Nothing to do. */
1130 pcap_open_dead(int linktype
, int snaplen
)
1134 p
= malloc(sizeof(*p
));
1137 memset (p
, 0, sizeof(*p
));
1138 p
->snapshot
= snaplen
;
1139 p
->linktype
= linktype
;
1140 p
->stats_op
= pcap_stats_dead
;
1142 p
->setbuff_op
= pcap_setbuff_dead
;
1143 p
->setmode_op
= pcap_setmode_dead
;
1144 p
->setmintocopy_op
= pcap_setmintocopy_dead
;
1146 p
->close_op
= pcap_close_dead
;
1152 * API compatible with WinPcap's "send a packet" routine - returns -1
1153 * on error, 0 otherwise.
1155 * XXX - what if we get a short write?
1158 pcap_sendpacket(pcap_t
*p
, const u_char
*buf
, int size
)
1160 if (p
->inject_op(p
, buf
, size
) == -1)
1166 * API compatible with OpenBSD's "send a packet" routine - returns -1 on
1167 * error, number of bytes written otherwise.
1170 pcap_inject(pcap_t
*p
, const void *buf
, size_t size
)
1172 return (p
->inject_op(p
, buf
, size
));
1176 pcap_close(pcap_t
*p
)
1179 if (p
->dlt_list
!= NULL
)
1181 pcap_freecode(&p
->fcode
);
1186 * We make the version string static, and return a pointer to it, rather
1187 * than exporting the version string directly. On at least some UNIXes,
1188 * if you import data from a shared library into an program, the data is
1189 * bound into the program binary, so if the string in the version of the
1190 * library with which the program was linked isn't the same as the
1191 * string in the version of the library with which the program is being
1192 * run, various undesirable things may happen (warnings, the string
1193 * being the one from the version of the library with which the program
1194 * was linked, or even weirder things, such as the string being the one
1195 * from the library but being truncated).
1197 #ifdef HAVE_VERSION_H
1198 #include "version.h"
1200 static const char pcap_version_string
[] = "libpcap version 0.9[.x]";
1205 * XXX - it'd be nice if we could somehow generate the WinPcap and libpcap
1206 * version numbers when building WinPcap. (It'd be nice to do so for
1207 * the packet.dll version number as well.)
1209 static const char wpcap_version_string
[] = "4.0";
1210 static const char pcap_version_string_fmt
[] =
1211 "WinPcap version %s, based on %s";
1212 static const char pcap_version_string_packet_dll_fmt
[] =
1213 "WinPcap version %s (packet.dll version %s), based on %s";
1214 static char *full_pcap_version_string
;
1217 pcap_lib_version(void)
1219 char *packet_version_string
;
1220 size_t full_pcap_version_string_len
;
1222 if (full_pcap_version_string
== NULL
) {
1224 * Generate the version string.
1226 packet_version_string
= PacketGetVersion();
1227 if (strcmp(wpcap_version_string
, packet_version_string
) == 0) {
1229 * WinPcap version string and packet.dll version
1230 * string are the same; just report the WinPcap
1233 full_pcap_version_string_len
=
1234 (sizeof pcap_version_string_fmt
- 4) +
1235 strlen(wpcap_version_string
) +
1236 strlen(pcap_version_string
);
1237 full_pcap_version_string
=
1238 malloc(full_pcap_version_string_len
);
1239 sprintf(full_pcap_version_string
,
1240 pcap_version_string_fmt
, wpcap_version_string
,
1241 pcap_version_string
);
1244 * WinPcap version string and packet.dll version
1245 * string are different; that shouldn't be the
1246 * case (the two libraries should come from the
1247 * same version of WinPcap), so we report both
1250 full_pcap_version_string_len
=
1251 (sizeof pcap_version_string_packet_dll_fmt
- 6) +
1252 strlen(wpcap_version_string
) +
1253 strlen(packet_version_string
) +
1254 strlen(pcap_version_string
);
1255 full_pcap_version_string
= malloc(full_pcap_version_string_len
);
1257 sprintf(full_pcap_version_string
,
1258 pcap_version_string_packet_dll_fmt
,
1259 wpcap_version_string
, packet_version_string
,
1260 pcap_version_string
);
1263 return (full_pcap_version_string
);
1266 #elif defined(MSDOS)
1268 static char *full_pcap_version_string
;
1271 pcap_lib_version (void)
1273 char *packet_version_string
;
1274 size_t full_pcap_version_string_len
;
1275 static char dospfx
[] = "DOS-";
1277 if (full_pcap_version_string
== NULL
) {
1279 * Generate the version string.
1281 full_pcap_version_string_len
=
1282 sizeof dospfx
+ strlen(pcap_version_string
);
1283 full_pcap_version_string
=
1284 malloc(full_pcap_version_string_len
);
1285 strcpy(full_pcap_version_string
, dospfx
);
1286 strcat(full_pcap_version_string
, pcap_version_string
);
1288 return (full_pcap_version_string
);
1294 pcap_lib_version(void)
1296 return (pcap_version_string
);