2 * Copyright (c) 1999 - 2005 NetGroup, Politecnico di Torino (Italy)
3 * Copyright (c) 2005 - 2008 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.42 2008-05-21 22:15:25 gianluca Exp $ (LBL)";
45 #endif /*__MINGW32__*/
49 #endif /* HAVE_DAG_API */
52 #define errno (*_errno())
53 #endif /* __MINGW32__ */
55 static int pcap_setfilter_win32_npf(pcap_t
*, struct bpf_program
*);
56 static int pcap_setfilter_win32_dag(pcap_t
*, struct bpf_program
*);
57 static int pcap_getnonblock_win32(pcap_t
*, char *);
58 static int pcap_setnonblock_win32(pcap_t
*, int, char *);
60 /*dimension of the buffer in the pcap_t structure*/
61 #define WIN32_DEFAULT_USER_BUFFER_SIZE 256000
63 /*dimension of the buffer in the kernel driver NPF */
64 #define WIN32_DEFAULT_KERNEL_BUFFER_SIZE 1000000
66 /* Equivalent to ntohs(), but a lot faster under Windows */
67 #define SWAPS(_X) ((_X & 0xff) << 8) | (_X >> 8)
70 * Header that the WinPcap driver associates to the packets.
74 struct timeval bh_tstamp
; /* time stamp */
75 bpf_u_int32 bh_caplen
; /* length of captured portion */
76 bpf_u_int32 bh_datalen
; /* original length of packet */
77 u_short bh_hdrlen
; /* length of bpf header (this struct
78 plus alignment padding) */
81 CRITICAL_SECTION g_PcapCompileCriticalSection
;
89 if (dwReason
== DLL_PROCESS_ATTACH
)
91 OutputDebugString("Hello my dear, I INITIALIZED THE CS\n");
92 InitializeCriticalSection(&g_PcapCompileCriticalSection
);
102 WORD wVersionRequested
;
105 wVersionRequested
= MAKEWORD( 1, 1);
106 err
= WSAStartup( wVersionRequested
, &wsaData
);
116 pcap_stats_win32(pcap_t
*p
, struct pcap_stat
*ps
)
119 if(PacketGetStats(p
->adapter
, (struct bpf_stat
*)ps
) != TRUE
){
120 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "PacketGetStats error: %s", pcap_win32strerror());
127 /* Set the dimension of the kernel-level capture buffer */
129 pcap_setbuff_win32(pcap_t
*p
, int dim
)
131 if(PacketSetBuff(p
->adapter
,dim
)==FALSE
)
133 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "driver error: not enough memory to allocate the kernel buffer");
139 /* Set the driver working mode */
141 pcap_setmode_win32(pcap_t
*p
, int mode
)
143 if(PacketSetMode(p
->adapter
,mode
)==FALSE
)
145 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "driver error: working mode not recognized");
152 /*set the minimum amount of data that will release a read call*/
154 pcap_setmintocopy_win32(pcap_t
*p
, int size
)
156 if(PacketSetMinToCopy(p
->adapter
, size
)==FALSE
)
158 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "driver error: unable to set the requested mintocopy size");
165 pcap_read_win32_npf(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
169 register u_char
*bp
, *ep
;
174 * Has "pcap_breakloop()" been called?
178 * Yes - clear the flag that indicates that it
179 * has, and return -2 to indicate that we were
180 * told to break out of the loop.
186 /* capture the packets */
187 if(PacketReceivePacket(p
->adapter
,p
->Packet
,TRUE
)==FALSE
){
188 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "read error: PacketReceivePacket failed");
192 cc
= p
->Packet
->ulBytesReceived
;
194 bp
= p
->Packet
->Buffer
;
200 * Loop through each packet.
202 #define bhp ((struct bpf_hdr *)bp)
205 register int caplen
, hdrlen
;
208 * Has "pcap_breakloop()" been called?
209 * If so, return immediately - if we haven't read any
210 * packets, clear the flag and return -2 to indicate
211 * that we were told to break out of the loop, otherwise
212 * leave the flag set, so that the *next* call will break
213 * out of the loop without having read any packets, and
214 * return the number of packets we've processed so far.
229 caplen
= bhp
->bh_caplen
;
230 hdrlen
= bhp
->bh_hdrlen
;
233 * XXX A bpf_hdr matches a pcap_pkthdr.
235 (*callback
)(user
, (struct pcap_pkthdr
*)bp
, bp
+ hdrlen
);
236 bp
+= BPF_WORDALIGN(caplen
+ hdrlen
);
237 if (++n
>= cnt
&& cnt
> 0) {
250 pcap_read_win32_dag(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
253 int packet_len
= 0, caplen
= 0;
254 struct pcap_pkthdr pcap_header
;
257 dag_record_t
*header
;
258 unsigned erf_record_len
;
262 unsigned dfp
= p
->adapter
->DagFastProcess
;
265 if (cc
== 0) /* Get new packets only if we have processed all the ones of the previous read */
267 /* Get new packets from the network */
268 if(PacketReceivePacket(p
->adapter
, p
->Packet
, TRUE
)==FALSE
){
269 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "read error: PacketReceivePacket failed");
273 cc
= p
->Packet
->ulBytesReceived
;
275 /* The timeout has expired but we no packets arrived */
277 header
= (dag_record_t
*)p
->adapter
->DagBuffer
;
280 header
= (dag_record_t
*)p
->bp
;
282 endofbuf
= (char*)header
+ cc
;
285 * Cycle through the packets
289 erf_record_len
= SWAPS(header
->rlen
);
290 if((char*)header
+ erf_record_len
> endofbuf
)
293 /* Increase the number of captured packets */
294 p
->md
.stat
.ps_recv
++;
296 /* Find the beginning of the packet */
297 dp
= ((u_char
*)header
) + dag_record_size
;
299 /* Determine actual packet len */
303 packet_len
= ATM_SNAPLEN
;
304 caplen
= ATM_SNAPLEN
;
310 swt
= SWAPS(header
->wlen
);
311 packet_len
= swt
- (p
->md
.dag_fcs_bits
);
312 caplen
= erf_record_len
- dag_record_size
- 2;
313 if (caplen
> packet_len
)
322 swt
= SWAPS(header
->wlen
);
323 packet_len
= swt
- (p
->md
.dag_fcs_bits
);
324 caplen
= erf_record_len
- dag_record_size
;
325 if (caplen
> packet_len
)
333 if(caplen
> p
->snapshot
)
334 caplen
= p
->snapshot
;
337 * Has "pcap_breakloop()" been called?
338 * If so, return immediately - if we haven't read any
339 * packets, clear the flag and return -2 to indicate
340 * that we were told to break out of the loop, otherwise
341 * leave the flag set, so that the *next* call will break
342 * out of the loop without having read any packets, and
343 * return the number of packets we've processed so far.
354 p
->bp
= (char*)header
;
355 p
->cc
= endofbuf
- (char*)header
;
362 /* convert between timestamp formats */
364 pcap_header
.ts
.tv_sec
= (int)(ts
>> 32);
365 ts
= (ts
& 0xffffffffi
64) * 1000000;
366 ts
+= 0x80000000; /* rounding */
367 pcap_header
.ts
.tv_usec
= (int)(ts
>> 32);
368 if (pcap_header
.ts
.tv_usec
>= 1000000) {
369 pcap_header
.ts
.tv_usec
-= 1000000;
370 pcap_header
.ts
.tv_sec
++;
374 /* No underlaying filtering system. We need to filter on our own */
375 if (p
->fcode
.bf_insns
)
377 if (bpf_filter(p
->fcode
.bf_insns
, dp
, packet_len
, caplen
) == 0)
379 /* Move to next packet */
380 header
= (dag_record_t
*)((char*)header
+ erf_record_len
);
385 /* Fill the header for the user suppplied callback function */
386 pcap_header
.caplen
= caplen
;
387 pcap_header
.len
= packet_len
;
389 /* Call the callback function */
390 (*callback
)(user
, &pcap_header
, dp
);
392 /* Move to next packet */
393 header
= (dag_record_t
*)((char*)header
+ erf_record_len
);
395 /* Stop if the number of packets requested by user has been reached*/
396 if (++n
>= cnt
&& cnt
> 0)
398 p
->bp
= (char*)header
;
399 p
->cc
= endofbuf
- (char*)header
;
403 while((u_char
*)header
< endofbuf
);
407 #endif /* HAVE_DAG_API */
409 /* Send a packet to the network */
411 pcap_inject_win32(pcap_t
*p
, const void *buf
, size_t size
){
412 LPPACKET PacketToSend
;
414 PacketToSend
=PacketAllocatePacket();
416 if (PacketToSend
== NULL
)
418 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "send error: PacketAllocatePacket failed");
422 PacketInitPacket(PacketToSend
,(PVOID
)buf
,size
);
423 if(PacketSendPacket(p
->adapter
,PacketToSend
,TRUE
) == FALSE
){
424 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "send error: PacketSendPacket failed");
425 PacketFreePacket(PacketToSend
);
429 PacketFreePacket(PacketToSend
);
432 * We assume it all got sent if "PacketSendPacket()" succeeded.
433 * "pcap_inject()" is expected to return the number of bytes
440 pcap_cleanup_win32(pcap_t
*p
)
442 if (p
->adapter
!= NULL
) {
443 PacketCloseAdapter(p
->adapter
);
447 PacketFreePacket(p
->Packet
);
450 pcap_cleanup_live_common(p
);
454 pcap_activate_win32(pcap_t
*p
)
460 * No monitor mode on Windows. It could be done on
461 * Vista with drivers that support the native 802.11
462 * mechanism and monitor mode.
464 return (PCAP_ERROR_RFMON_NOTSUP
);
470 p
->adapter
= PacketOpenAdapter(p
->opt
.source
);
472 if (p
->adapter
== NULL
)
474 /* Adapter detected but we are not able to open it. Return failure. */
475 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "Error opening adapter: %s", pcap_win32strerror());
480 if(PacketGetNetType (p
->adapter
,&type
) == FALSE
)
482 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "Cannot determine the network type: %s", pcap_win32strerror());
487 switch (type
.LinkType
)
490 p
->linktype
= DLT_EN10MB
;
493 case NdisMedium802_3
:
494 p
->linktype
= DLT_EN10MB
;
496 * This is (presumably) a real Ethernet capture; give it a
497 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
498 * that an application can let you choose it, in case you're
499 * capturing DOCSIS traffic that a Cisco Cable Modem
500 * Termination System is putting out onto an Ethernet (it
501 * doesn't put an Ethernet header onto the wire, it puts raw
502 * DOCSIS frames out on the wire inside the low-level
505 p
->dlt_list
= (u_int
*) malloc(sizeof(u_int
) * 2);
507 * If that fails, just leave the list empty.
509 if (p
->dlt_list
!= NULL
) {
510 p
->dlt_list
[0] = DLT_EN10MB
;
511 p
->dlt_list
[1] = DLT_DOCSIS
;
517 p
->linktype
= DLT_FDDI
;
520 case NdisMedium802_5
:
521 p
->linktype
= DLT_IEEE802
;
524 case NdisMediumArcnetRaw
:
525 p
->linktype
= DLT_ARCNET
;
528 case NdisMediumArcnet878_2
:
529 p
->linktype
= DLT_ARCNET
;
533 p
->linktype
= DLT_ATM_RFC1483
;
536 case NdisMediumCHDLC
:
537 p
->linktype
= DLT_CHDLC
;
540 case NdisMediumPPPSerial
:
541 p
->linktype
= DLT_PPP_SERIAL
;
545 p
->linktype
= DLT_NULL
;
548 case NdisMediumBare80211
:
549 p
->linktype
= DLT_IEEE802_11
;
552 case NdisMediumRadio80211
:
553 p
->linktype
= DLT_IEEE802_11_RADIO
;
557 p
->linktype
= DLT_PPI
;
561 p
->linktype
= DLT_EN10MB
; /*an unknown adapter is assumed to be ethernet*/
565 /* Set promiscuous mode */
569 if (PacketSetHwFilter(p
->adapter
,NDIS_PACKET_TYPE_PROMISCUOUS
) == FALSE
)
571 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "failed to set hardware filter to promiscuous mode");
577 if (PacketSetHwFilter(p
->adapter
,NDIS_PACKET_TYPE_ALL_LOCAL
) == FALSE
)
579 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "failed to set hardware filter to non-promiscuous mode");
584 /* Set the buffer size */
585 p
->bufsize
= WIN32_DEFAULT_USER_BUFFER_SIZE
;
587 /* allocate Packet structure used during the capture */
588 if((p
->Packet
= PacketAllocatePacket())==NULL
)
590 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "failed to allocate the PACKET structure");
594 if(!(p
->adapter
->Flags
& INFO_FLAG_DAG_CARD
))
597 * Traditional Adapter
600 * If the buffer size wasn't explicitly set, default to
601 * WIN32_DEFAULT_USER_BUFFER_SIZE.
603 if (p
->opt
.buffer_size
== 0)
604 p
->opt
.buffer_size
= WIN32_DEFAULT_KERNEL_BUFFER_SIZE
;
606 if(PacketSetBuff(p
->adapter
,p
->opt
.buffer_size
)==FALSE
)
608 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "driver error: not enough memory to allocate the kernel buffer");
612 p
->buffer
= (u_char
*)malloc(p
->bufsize
);
613 if (p
->buffer
== NULL
)
615 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "malloc: %s", pcap_strerror(errno
));
619 PacketInitPacket(p
->Packet
,(BYTE
*)p
->buffer
,p
->bufsize
);
621 /* tell the driver to copy the buffer only if it contains at least 16K */
622 if(PacketSetMinToCopy(p
->adapter
,16000)==FALSE
)
624 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,"Error calling PacketSetMinToCopy: %s", pcap_win32strerror());
641 snprintf(keyname
, sizeof(keyname
), "%s\\CardParams\\%s",
642 "SYSTEM\\CurrentControlSet\\Services\\DAG",
643 strstr(_strlwr(p
->opt
.source
), "dag"));
646 status
= RegOpenKeyEx(HKEY_LOCAL_MACHINE
, keyname
, 0, KEY_READ
, &dagkey
);
647 if(status
!= ERROR_SUCCESS
)
650 status
= RegQueryValueEx(dagkey
,
657 if(status
!= ERROR_SUCCESS
)
667 p
->snapshot
= PacketSetSnapLen(p
->adapter
, snaplen
);
669 /* Set the length of the FCS associated to any packet. This value
670 * will be subtracted to the packet length */
671 p
->md
.dag_fcs_bits
= p
->adapter
->DagFcsLen
;
675 #endif /* HAVE_DAG_API */
677 PacketSetReadTimeout(p
->adapter
, p
->md
.timeout
);
680 if(p
->adapter
->Flags
& INFO_FLAG_DAG_CARD
)
682 /* install dag specific handlers for read and setfilter */
683 p
->read_op
= pcap_read_win32_dag
;
684 p
->setfilter_op
= pcap_setfilter_win32_dag
;
688 #endif /* HAVE_DAG_API */
689 /* install traditional npf handlers for read and setfilter */
690 p
->read_op
= pcap_read_win32_npf
;
691 p
->setfilter_op
= pcap_setfilter_win32_npf
;
694 #endif /* HAVE_DAG_API */
695 p
->setdirection_op
= NULL
; /* Not implemented. */
696 /* XXX - can this be implemented on some versions of Windows? */
697 p
->inject_op
= pcap_inject_win32
;
698 p
->set_datalink_op
= NULL
; /* can't change data link type */
699 p
->getnonblock_op
= pcap_getnonblock_win32
;
700 p
->setnonblock_op
= pcap_setnonblock_win32
;
701 p
->stats_op
= pcap_stats_win32
;
702 p
->setbuff_op
= pcap_setbuff_win32
;
703 p
->setmode_op
= pcap_setmode_win32
;
704 p
->setmintocopy_op
= pcap_setmintocopy_win32
;
705 p
->cleanup_op
= pcap_cleanup_win32
;
709 pcap_cleanup_win32(p
);
714 pcap_create(const char *device
, char *ebuf
)
718 if (strlen(device
) == 1)
721 * It's probably a unicode string
722 * Convert to ascii and pass it to pcap_create_common
724 * This wonderful hack is needed because pcap_lookupdev still returns
725 * unicode strings, and it's used by windump when no device is specified
726 * in the command line
731 length
= wcslen((wchar_t*)device
);
733 deviceAscii
= (char*)malloc(length
+ 1);
735 if (deviceAscii
== NULL
)
737 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "Malloc failed");
741 snprintf(deviceAscii
, length
+ 1, "%ws", (wchar_t*)device
);
742 p
= pcap_create_common(deviceAscii
, ebuf
);
747 p
= pcap_create_common(device
, ebuf
);
753 p
->activate_op
= pcap_activate_win32
;
758 pcap_setfilter_win32_npf(pcap_t
*p
, struct bpf_program
*fp
)
760 if(PacketSetBpf(p
->adapter
,fp
)==FALSE
){
762 * Kernel filter not installed.
763 * XXX - fall back on userland filtering, as is done
764 * on other platforms?
766 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "Driver error: cannot set bpf filter: %s", pcap_win32strerror());
771 * Discard any previously-received packets, as they might have
772 * passed whatever filter was formerly in effect, but might
773 * not pass this filter (BIOCSETF discards packets buffered
774 * in the kernel, so you can lose packets in any case).
781 * We filter at user level, since the kernel driver does't process the packets
784 pcap_setfilter_win32_dag(pcap_t
*p
, struct bpf_program
*fp
) {
788 strncpy(p
->errbuf
, "setfilter: No filter specified", sizeof(p
->errbuf
));
792 /* Install a user level filter */
793 if (install_bpf_program(p
, fp
) < 0)
795 snprintf(p
->errbuf
, sizeof(p
->errbuf
),
796 "setfilter, unable to install the filter: %s", pcap_strerror(errno
));
806 pcap_getnonblock_win32(pcap_t
*p
, char *errbuf
)
809 * XXX - if there were a PacketGetReadTimeout() call, we
810 * would use it, and return 1 if the timeout is -1
813 return (p
->nonblock
);
817 pcap_setnonblock_win32(pcap_t
*p
, int nonblock
, char *errbuf
)
823 * Set the read timeout to -1 for non-blocking mode.
828 * Restore the timeout set when the device was opened.
829 * (Note that this may be -1, in which case we're not
830 * really leaving non-blocking mode.)
832 newtimeout
= p
->md
.timeout
;
834 if (!PacketSetReadTimeout(p
->adapter
, newtimeout
)) {
835 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
836 "PacketSetReadTimeout: %s", pcap_win32strerror());
839 p
->nonblock
= (newtimeout
== -1);
843 /*platform-dependent routine to add devices other than NDIS interfaces*/
845 pcap_platform_finddevs(pcap_if_t
**alldevsp
, char *errbuf
)