2 * Copyright (c) 1999 - 2005 NetGroup, Politecnico di Torino (Italy)
3 * Copyright (c) 2005 - 2007 CACE Technologies, Davis (California)
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the Politecnico di Torino, CACE Technologies
16 * nor the names of its contributors may be used to endorse or promote
17 * products derived from this software without specific prior written
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 static const char rcsid
[] _U_
=
36 "@(#) $Header: /tcpdump/master/libpcap/pcap-win32.c,v 1.25.2.7 2007/06/14 22:07:14 gianluca Exp $ (LBL)";
45 #endif /* HAVE_DAG_API */
48 #define errno (*_errno())
49 #endif /* __MINGW32__ */
51 static int pcap_setfilter_win32_npf(pcap_t
*, struct bpf_program
*);
52 static int pcap_setfilter_win32_dag(pcap_t
*, struct bpf_program
*);
53 static int pcap_getnonblock_win32(pcap_t
*, char *);
54 static int pcap_setnonblock_win32(pcap_t
*, int, char *);
56 #define PcapBufSize 256000 /*dimension of the buffer in the pcap_t structure*/
57 #define SIZE_BUF 1000000
59 /* Equivalent to ntohs(), but a lot faster under Windows */
60 #define SWAPS(_X) ((_X & 0xff) << 8) | (_X >> 8)
63 * Header that the WinPcap driver associates to the packets.
67 struct timeval bh_tstamp
; /* time stamp */
68 bpf_u_int32 bh_caplen
; /* length of captured portion */
69 bpf_u_int32 bh_datalen
; /* original length of packet */
70 u_short bh_hdrlen
; /* length of bpf header (this struct
71 plus alignment padding) */
78 WORD wVersionRequested
;
81 wVersionRequested
= MAKEWORD( 1, 1);
82 err
= WSAStartup( wVersionRequested
, &wsaData
);
92 pcap_stats_win32(pcap_t
*p
, struct pcap_stat
*ps
)
95 if(PacketGetStats(p
->adapter
, (struct bpf_stat
*)ps
) != TRUE
){
96 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "PacketGetStats error: %s", pcap_win32strerror());
104 pcap_read_win32_npf(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
108 register u_char
*bp
, *ep
;
113 * Has "pcap_breakloop()" been called?
117 * Yes - clear the flag that indicates that it
118 * has, and return -2 to indicate that we were
119 * told to break out of the loop.
125 /* capture the packets */
126 if(PacketReceivePacket(p
->adapter
,p
->Packet
,TRUE
)==FALSE
){
127 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "read error: PacketReceivePacket failed");
131 cc
= p
->Packet
->ulBytesReceived
;
133 bp
= p
->Packet
->Buffer
;
139 * Loop through each packet.
141 #define bhp ((struct bpf_hdr *)bp)
144 register int caplen
, hdrlen
;
147 * Has "pcap_breakloop()" been called?
148 * If so, return immediately - if we haven't read any
149 * packets, clear the flag and return -2 to indicate
150 * that we were told to break out of the loop, otherwise
151 * leave the flag set, so that the *next* call will break
152 * out of the loop without having read any packets, and
153 * return the number of packets we've processed so far.
168 caplen
= bhp
->bh_caplen
;
169 hdrlen
= bhp
->bh_hdrlen
;
172 * XXX A bpf_hdr matches a pcap_pkthdr.
174 (*callback
)(user
, (struct pcap_pkthdr
*)bp
, bp
+ hdrlen
);
175 bp
+= BPF_WORDALIGN(caplen
+ hdrlen
);
176 if (++n
>= cnt
&& cnt
> 0) {
189 pcap_read_win32_dag(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
192 int packet_len
= 0, caplen
= 0;
193 struct pcap_pkthdr pcap_header
;
196 dag_record_t
*header
;
197 unsigned erf_record_len
;
201 unsigned dfp
= p
->adapter
->DagFastProcess
;
204 if (cc
== 0) /* Get new packets only if we have processed all the ones of the previous read */
206 /* Get new packets from the network */
207 if(PacketReceivePacket(p
->adapter
, p
->Packet
, TRUE
)==FALSE
){
208 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "read error: PacketReceivePacket failed");
212 cc
= p
->Packet
->ulBytesReceived
;
214 /* The timeout has expired but we no packets arrived */
216 header
= (dag_record_t
*)p
->adapter
->DagBuffer
;
219 header
= (dag_record_t
*)p
->bp
;
221 endofbuf
= (char*)header
+ cc
;
224 * Cycle through the packets
228 erf_record_len
= SWAPS(header
->rlen
);
229 if((char*)header
+ erf_record_len
> endofbuf
)
232 /* Increase the number of captured packets */
233 p
->md
.stat
.ps_recv
++;
235 /* Find the beginning of the packet */
236 dp
= ((u_char
*)header
) + dag_record_size
;
238 /* Determine actual packet len */
242 packet_len
= ATM_SNAPLEN
;
243 caplen
= ATM_SNAPLEN
;
249 swt
= SWAPS(header
->wlen
);
250 packet_len
= swt
- (p
->md
.dag_fcs_bits
);
251 caplen
= erf_record_len
- dag_record_size
- 2;
252 if (caplen
> packet_len
)
261 swt
= SWAPS(header
->wlen
);
262 packet_len
= swt
- (p
->md
.dag_fcs_bits
);
263 caplen
= erf_record_len
- dag_record_size
;
264 if (caplen
> packet_len
)
272 if(caplen
> p
->snapshot
)
273 caplen
= p
->snapshot
;
276 * Has "pcap_breakloop()" been called?
277 * If so, return immediately - if we haven't read any
278 * packets, clear the flag and return -2 to indicate
279 * that we were told to break out of the loop, otherwise
280 * leave the flag set, so that the *next* call will break
281 * out of the loop without having read any packets, and
282 * return the number of packets we've processed so far.
293 p
->bp
= (char*)header
;
294 p
->cc
= endofbuf
- (char*)header
;
301 /* convert between timestamp formats */
303 pcap_header
.ts
.tv_sec
= (int)(ts
>> 32);
304 ts
= (ts
& 0xffffffffi
64) * 1000000;
305 ts
+= 0x80000000; /* rounding */
306 pcap_header
.ts
.tv_usec
= (int)(ts
>> 32);
307 if (pcap_header
.ts
.tv_usec
>= 1000000) {
308 pcap_header
.ts
.tv_usec
-= 1000000;
309 pcap_header
.ts
.tv_sec
++;
313 /* No underlaying filtering system. We need to filter on our own */
314 if (p
->fcode
.bf_insns
)
316 if (bpf_filter(p
->fcode
.bf_insns
, dp
, packet_len
, caplen
) == 0)
318 /* Move to next packet */
319 header
= (dag_record_t
*)((char*)header
+ erf_record_len
);
324 /* Fill the header for the user suppplied callback function */
325 pcap_header
.caplen
= caplen
;
326 pcap_header
.len
= packet_len
;
328 /* Call the callback function */
329 (*callback
)(user
, &pcap_header
, dp
);
331 /* Move to next packet */
332 header
= (dag_record_t
*)((char*)header
+ erf_record_len
);
334 /* Stop if the number of packets requested by user has been reached*/
335 if (++n
>= cnt
&& cnt
> 0)
337 p
->bp
= (char*)header
;
338 p
->cc
= endofbuf
- (char*)header
;
342 while((u_char
*)header
< endofbuf
);
346 #endif /* HAVE_DAG_API */
348 /* Send a packet to the network */
350 pcap_inject_win32(pcap_t
*p
, const void *buf
, size_t size
){
351 LPPACKET PacketToSend
;
353 PacketToSend
=PacketAllocatePacket();
355 if (PacketToSend
== NULL
)
357 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "send error: PacketAllocatePacket failed");
361 PacketInitPacket(PacketToSend
,(PVOID
)buf
,size
);
362 if(PacketSendPacket(p
->adapter
,PacketToSend
,TRUE
) == FALSE
){
363 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "send error: PacketSendPacket failed");
364 PacketFreePacket(PacketToSend
);
368 PacketFreePacket(PacketToSend
);
371 * We assume it all got sent if "PacketSendPacket()" succeeded.
372 * "pcap_inject()" is expected to return the number of bytes
379 pcap_close_win32(pcap_t
*p
)
381 pcap_close_common(p
);
382 if (p
->adapter
!= NULL
) {
383 PacketCloseAdapter(p
->adapter
);
387 PacketFreePacket(p
->Packet
);
393 pcap_open_live(const char *device
, int snaplen
, int promisc
, int to_ms
,
402 p
= (pcap_t
*)malloc(sizeof(*p
));
405 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc: %s", pcap_strerror(errno
));
408 memset(p
, 0, sizeof(*p
));
411 p
->adapter
= PacketOpenAdapter((char*)device
);
413 if (p
->adapter
== NULL
)
416 /* Adapter detected but we are not able to open it. Return failure. */
417 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "Error opening adapter: %s", pcap_win32strerror());
422 if(PacketGetNetType (p
->adapter
,&type
) == FALSE
)
424 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "Cannot determine the network type: %s", pcap_win32strerror());
429 switch (type
.LinkType
)
432 p
->linktype
= DLT_EN10MB
;
435 case NdisMedium802_3
:
436 p
->linktype
= DLT_EN10MB
;
438 * This is (presumably) a real Ethernet capture; give it a
439 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
440 * that an application can let you choose it, in case you're
441 * capturing DOCSIS traffic that a Cisco Cable Modem
442 * Termination System is putting out onto an Ethernet (it
443 * doesn't put an Ethernet header onto the wire, it puts raw
444 * DOCSIS frames out on the wire inside the low-level
447 p
->dlt_list
= (u_int
*) malloc(sizeof(u_int
) * 2);
449 * If that fails, just leave the list empty.
451 if (p
->dlt_list
!= NULL
) {
452 p
->dlt_list
[0] = DLT_EN10MB
;
453 p
->dlt_list
[1] = DLT_DOCSIS
;
459 p
->linktype
= DLT_FDDI
;
462 case NdisMedium802_5
:
463 p
->linktype
= DLT_IEEE802
;
466 case NdisMediumArcnetRaw
:
467 p
->linktype
= DLT_ARCNET
;
470 case NdisMediumArcnet878_2
:
471 p
->linktype
= DLT_ARCNET
;
475 p
->linktype
= DLT_ATM_RFC1483
;
478 case NdisMediumCHDLC
:
479 p
->linktype
= DLT_CHDLC
;
482 case NdisMediumPPPSerial
:
483 p
->linktype
= DLT_PPP_SERIAL
;
487 p
->linktype
= DLT_NULL
;
490 case NdisMediumBare80211
:
491 p
->linktype
= DLT_IEEE802_11
;
494 case NdisMediumRadio80211
:
495 p
->linktype
= DLT_IEEE802_11_RADIO
;
499 p
->linktype
= DLT_PPI
;
503 p
->linktype
= DLT_EN10MB
; /*an unknown adapter is assumed to be ethernet*/
507 /* Set promiscuous mode */
511 if (PacketSetHwFilter(p
->adapter
,NDIS_PACKET_TYPE_PROMISCUOUS
) == FALSE
)
513 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "failed to set hardware filter to promiscuous mode");
519 if (PacketSetHwFilter(p
->adapter
,NDIS_PACKET_TYPE_ALL_LOCAL
) == FALSE
)
521 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "failed to set hardware filter to non-promiscuous mode");
526 /* Set the buffer size */
527 p
->bufsize
= PcapBufSize
;
529 /* Store the timeout. Used by pcap_setnonblock() */
532 /* allocate Packet structure used during the capture */
533 if((p
->Packet
= PacketAllocatePacket())==NULL
)
535 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "failed to allocate the PACKET structure");
539 if(!(p
->adapter
->Flags
& INFO_FLAG_DAG_CARD
))
542 * Traditional Adapter
545 p
->buffer
= (u_char
*)malloc(PcapBufSize
);
546 if (p
->buffer
== NULL
)
548 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc: %s", pcap_strerror(errno
));
552 PacketInitPacket(p
->Packet
,(BYTE
*)p
->buffer
,p
->bufsize
);
554 p
->snapshot
= snaplen
;
556 /* allocate the standard buffer in the driver */
557 if(PacketSetBuff( p
->adapter
, SIZE_BUF
)==FALSE
)
559 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,"driver error: not enough memory to allocate the kernel buffer\n");
563 /* tell the driver to copy the buffer only if it contains at least 16K */
564 if(PacketSetMinToCopy(p
->adapter
,16000)==FALSE
)
566 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,"Error calling PacketSetMinToCopy: %s\n", pcap_win32strerror());
583 snprintf(keyname
, sizeof(keyname
), "%s\\CardParams\\%s",
584 "SYSTEM\\CurrentControlSet\\Services\\DAG",
585 strstr(_strlwr((char*)device
), "dag"));
588 status
= RegOpenKeyEx(HKEY_LOCAL_MACHINE
, keyname
, 0, KEY_READ
, &dagkey
);
589 if(status
!= ERROR_SUCCESS
)
592 status
= RegQueryValueEx(dagkey
,
599 if(status
!= ERROR_SUCCESS
)
609 p
->snapshot
= PacketSetSnapLen(p
->adapter
, snaplen
);
611 /* Set the length of the FCS associated to any packet. This value
612 * will be subtracted to the packet length */
613 p
->md
.dag_fcs_bits
= p
->adapter
->DagFcsLen
;
617 #endif /* HAVE_DAG_API */
619 PacketSetReadTimeout(p
->adapter
, to_ms
);
622 if(p
->adapter
->Flags
& INFO_FLAG_DAG_CARD
)
624 /* install dag specific handlers for read and setfilter */
625 p
->read_op
= pcap_read_win32_dag
;
626 p
->setfilter_op
= pcap_setfilter_win32_dag
;
630 #endif /* HAVE_DAG_API */
631 /* install traditional npf handlers for read and setfilter */
632 p
->read_op
= pcap_read_win32_npf
;
633 p
->setfilter_op
= pcap_setfilter_win32_npf
;
636 #endif /* HAVE_DAG_API */
637 p
->setdirection_op
= NULL
; /* Not implemented. */
638 /* XXX - can this be implemented on some versions of Windows? */
639 p
->inject_op
= pcap_inject_win32
;
640 p
->set_datalink_op
= NULL
; /* can't change data link type */
641 p
->getnonblock_op
= pcap_getnonblock_win32
;
642 p
->setnonblock_op
= pcap_setnonblock_win32
;
643 p
->stats_op
= pcap_stats_win32
;
644 p
->close_op
= pcap_close_win32
;
649 PacketCloseAdapter(p
->adapter
);
650 if (p
->buffer
!= NULL
)
653 PacketFreePacket(p
->Packet
);
655 * Get rid of any link-layer type list we allocated.
657 if (p
->dlt_list
!= NULL
)
665 pcap_setfilter_win32_npf(pcap_t
*p
, struct bpf_program
*fp
)
667 if(PacketSetBpf(p
->adapter
,fp
)==FALSE
){
669 * Kernel filter not installed.
670 * XXX - fall back on userland filtering, as is done
671 * on other platforms?
673 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "Driver error: cannot set bpf filter: %s", pcap_win32strerror());
678 * Discard any previously-received packets, as they might have
679 * passed whatever filter was formerly in effect, but might
680 * not pass this filter (BIOCSETF discards packets buffered
681 * in the kernel, so you can lose packets in any case).
688 * We filter at user level, since the kernel driver does't process the packets
691 pcap_setfilter_win32_dag(pcap_t
*p
, struct bpf_program
*fp
) {
695 strncpy(p
->errbuf
, "setfilter: No filter specified", sizeof(p
->errbuf
));
699 /* Install a user level filter */
700 if (install_bpf_program(p
, fp
) < 0)
702 snprintf(p
->errbuf
, sizeof(p
->errbuf
),
703 "setfilter, unable to install the filter: %s", pcap_strerror(errno
));
713 pcap_getnonblock_win32(pcap_t
*p
, char *errbuf
)
716 * XXX - if there were a PacketGetReadTimeout() call, we
717 * would use it, and return 1 if the timeout is -1
720 return (p
->nonblock
);
724 pcap_setnonblock_win32(pcap_t
*p
, int nonblock
, char *errbuf
)
730 * Set the read timeout to -1 for non-blocking mode.
735 * Restore the timeout set when the device was opened.
736 * (Note that this may be -1, in which case we're not
737 * really leaving non-blocking mode.)
739 newtimeout
= p
->timeout
;
741 if (!PacketSetReadTimeout(p
->adapter
, newtimeout
)) {
742 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
743 "PacketSetReadTimeout: %s", pcap_win32strerror());
746 p
->nonblock
= (newtimeout
== -1);
750 /* Set the driver working mode */
752 pcap_setmode(pcap_t
*p
, int mode
){
754 if (p
->adapter
==NULL
)
756 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "impossible to set mode while reading from a file");
760 if(PacketSetMode(p
->adapter
,mode
)==FALSE
)
762 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "driver error: working mode not recognized");
769 /* Set the dimension of the kernel-level capture buffer */
771 pcap_setbuff(pcap_t
*p
, int dim
)
773 if (p
->adapter
==NULL
)
775 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "The kernel buffer size cannot be set while reading from a file");
779 if(PacketSetBuff(p
->adapter
,dim
)==FALSE
)
781 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "driver error: not enough memory to allocate the kernel buffer");
787 /*set the minimum amount of data that will release a read call*/
789 pcap_setmintocopy(pcap_t
*p
, int size
)
791 if (p
->adapter
==NULL
)
793 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "Impossible to set the mintocopy parameter on an offline capture");
797 if(PacketSetMinToCopy(p
->adapter
, size
)==FALSE
)
799 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "driver error: unable to set the requested mintocopy size");