2 * Copyright (c) 1999 - 2005 NetGroup, Politecnico di Torino (Italy)
3 * Copyright (c) 2005 - 2010 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.
40 #include <ddk/ntddndis.h>
42 #endif /*__MINGW64__*/
45 #endif /*__MINGW32__*/
49 #endif /* HAVE_DAG_API */
52 #define errno (*_errno())
53 #endif /* __MINGW32__ */
57 static int pcap_setfilter_win32_npf(pcap_t
*, struct bpf_program
*);
58 static int pcap_setfilter_win32_dag(pcap_t
*, struct bpf_program
*);
59 static int pcap_getnonblock_win32(pcap_t
*, char *);
60 static int pcap_setnonblock_win32(pcap_t
*, int, char *);
62 /*dimension of the buffer in the pcap_t structure*/
63 #define WIN32_DEFAULT_USER_BUFFER_SIZE 256000
65 /*dimension of the buffer in the kernel driver NPF */
66 #define WIN32_DEFAULT_KERNEL_BUFFER_SIZE 1000000
68 /* Equivalent to ntohs(), but a lot faster under Windows */
69 #define SWAPS(_X) ((_X & 0xff) << 8) | (_X >> 8)
72 * Private data for capturing on WinPcap devices.
77 int filtering_in_kernel
; /* using kernel filter */
80 int dag_fcs_bits
; /* Number of checksum bits from link layer */
84 CRITICAL_SECTION g_PcapCompileCriticalSection
;
92 if (dwReason
== DLL_PROCESS_ATTACH
)
94 InitializeCriticalSection(&g_PcapCompileCriticalSection
);
104 WORD wVersionRequested
;
112 wVersionRequested
= MAKEWORD( 1, 1);
113 err
= WSAStartup( wVersionRequested
, &wsaData
);
114 atexit ((void(*)(void))WSACleanup
);
115 InitializeCriticalSection(&g_PcapCompileCriticalSection
);
129 pcap_stats_win32(pcap_t
*p
, struct pcap_stat
*ps
)
131 struct pcap_win
*pw
= p
->priv
;
132 struct bpf_stat bstats
;
133 char errbuf
[PCAP_ERRBUF_SIZE
+1];
136 * Try to get statistics.
138 * (Please note - "struct pcap_stat" is *not* the same as
139 * WinPcap's "struct bpf_stat". It might currently have the
140 * same layout, but let's not cheat.
142 * Note also that we don't fill in ps_capt, as we might have
143 * been called by code compiled against an earlier version of
144 * WinPcap that didn't have ps_capt, in which case filling it
145 * in would stomp on whatever comes after the structure passed
148 if (!PacketGetStats(p
->adapter
, &bstats
)) {
149 pcap_win32_err_to_str(GetLastError(), errbuf
);
150 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
151 "PacketGetStats error: %s", errbuf
);
154 ps
->ps_recv
= bstats
.bs_recv
;
155 ps
->ps_drop
= bstats
.bs_drop
;
156 ps
->ps_ifdrop
= bstats
.ps_ifdrop
;
162 * Win32-only routine for getting statistics.
164 * This way is definitely safer than passing the pcap_stat * from the userland.
165 * In fact, there could happen than the user allocates a variable which is not
166 * big enough for the new structure, and the library will write in a zone
167 * which is not allocated to this variable.
169 * In this way, we're pretty sure we are writing on memory allocated to this
172 * XXX - but this is the wrong way to handle statistics. Instead, we should
173 * have an API that returns data in a form like the Options section of a
174 * pcapng Interface Statistics Block:
176 * http://xml2rfc.tools.ietf.org/cgi-bin/xml2rfc.cgi?url=https://raw.githubusercontent.com/pcapng/pcapng/master/draft-tuexen-opsawg-pcapng.xml&modeAsFormat=html/ascii&type=ascii#rfc.section.4.6
178 * which would let us add new statistics straightforwardly and indicate which
179 * statistics we are and are *not* providing, rather than having to provide
180 * possibly-bogus values for statistics we can't provide.
183 pcap_stats_ex_win32(pcap_t
*p
, int *pcap_stat_size
)
185 struct bpf_stat bstats
;
186 char errbuf
[PCAP_ERRBUF_SIZE
+1];
188 *pcap_stat_size
= sizeof (p
->stat
);
191 * Try to get statistics.
193 * (Please note - "struct pcap_stat" is *not* the same as
194 * WinPcap's "struct bpf_stat". It might currently have the
195 * same layout, but let's not cheat.)
197 if (!PacketGetStatsEx(p
->adapter
, &bstats
)) {
198 pcap_win32_err_to_str(GetLastError(), errbuf
);
199 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
200 "PacketGetStatsEx error: %s", errbuf
);
203 p
->stat
.ps_recv
= bstats
.bs_recv
;
204 p
->stat
.ps_drop
= bstats
.bs_drop
;
205 p
->stat
.ps_ifdrop
= bstats
.ps_ifdrop
;
206 p
->stat
.ps_capt
= bstats
.bs_capt
;
210 /* Set the dimension of the kernel-level capture buffer */
212 pcap_setbuff_win32(pcap_t
*p
, int dim
)
214 if(PacketSetBuff(p
->adapter
,dim
)==FALSE
)
216 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "driver error: not enough memory to allocate the kernel buffer");
222 /* Set the driver working mode */
224 pcap_setmode_win32(pcap_t
*p
, int mode
)
226 if(PacketSetMode(p
->adapter
,mode
)==FALSE
)
228 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "driver error: working mode not recognized");
235 /*set the minimum amount of data that will release a read call*/
237 pcap_setmintocopy_win32(pcap_t
*p
, int size
)
239 if(PacketSetMinToCopy(p
->adapter
, size
)==FALSE
)
241 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "driver error: unable to set the requested mintocopy size");
248 pcap_getevent_win32(pcap_t
*p
)
250 return PacketGetReadEvent(p
->adapter
);
254 pcap_oid_get_request_win32(pcap_t
*p
, pcap_oid_data_t
*data
)
256 char errbuf
[PCAP_ERRBUF_SIZE
+1];
258 if (!PacketRequest(p
->adapter
, FALSE
, data
)) {
259 pcap_win32_err_to_str(GetLastError(), errbuf
);
260 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
261 "Error calling PacketRequest: %s", errbuf
);
268 pcap_oid_set_request_win32(pcap_t
*p
, pcap_oid_data_t
*data
)
270 char errbuf
[PCAP_ERRBUF_SIZE
+1];
272 if (!PacketRequest(p
->adapter
, TRUE
, data
)) {
273 pcap_win32_err_to_str(GetLastError(), errbuf
);
274 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
275 "Error calling PacketRequest: %s", errbuf
);
282 pcap_sendqueue_transmit_win32(pcap_t
*p
, pcap_send_queue
*queue
, int sync
)
285 char errbuf
[PCAP_ERRBUF_SIZE
+1];
287 if (p
->adapter
==NULL
) {
288 sprintf(p
->errbuf
, "Cannot transmit a queue to an offline capture or to a TurboCap port");
292 res
= PacketSendPackets(p
->adapter
,
297 if(res
!= queue
->len
){
298 pcap_win32_err_to_str(GetLastError(), errbuf
);
299 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
300 "Error opening adapter: %s", errbuf
);
307 pcap_setuserbuffer_win32(pcap_t
*p
, int size
)
309 unsigned char *new_buff
;
312 /* Bogus parameter */
313 sprintf(p
->errbuf
,"Error: invalid size %d",size
);
317 /* Allocate the buffer */
318 new_buff
=(unsigned char*)malloc(sizeof(char)*size
);
321 sprintf(p
->errbuf
,"Error: not enough memory");
330 /* Associate the buffer with the capture packet */
331 PacketInitPacket(p
->Packet
,(BYTE
*)p
->buffer
,p
->bufsize
);
337 pcap_live_dump_win32(pcap_t
*p
, char *filename
, int maxsize
, int maxpacks
)
341 /* Set the packet driver in dump mode */
342 res
= PacketSetMode(p
->adapter
, PACKET_MODE_DUMP
);
344 sprintf(p
->errbuf
, "Error setting dump mode");
348 /* Set the name of the dump file */
349 res
= PacketSetDumpName(p
->adapter
, filename
, strlen(filename
));
351 sprintf(p
->errbuf
, "Error setting kernel dump file name");
355 /* Set the limits of the dump file */
356 res
= PacketSetDumpLimits(p
->adapter
, maxsize
, maxpacks
);
362 pcap_live_dump_ended_win32(pcap_t
*p
, int sync
)
364 return PacketIsDumpEnded(p
->adapter
, (BOOLEAN
)sync
);
367 static PAirpcapHandle
368 pcap_get_airpcap_handle_win32(pcap_t
*p
)
370 #ifdef HAVE_AIRPCAP_API
371 return PacketGetAirPcapHandle(p
->adapter
);
374 #endif /* HAVE_AIRPCAP_API */
378 pcap_read_win32_npf(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
382 register u_char
*bp
, *ep
;
384 struct pcap_win
*pw
= p
->priv
;
389 * Has "pcap_breakloop()" been called?
393 * Yes - clear the flag that indicates that it
394 * has, and return PCAP_ERROR_BREAK to indicate
395 * that we were told to break out of the loop.
398 return (PCAP_ERROR_BREAK
);
401 /* capture the packets */
402 if(PacketReceivePacket(p
->adapter
,p
->Packet
,TRUE
)==FALSE
){
403 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "read error: PacketReceivePacket failed");
407 cc
= p
->Packet
->ulBytesReceived
;
409 bp
= p
->Packet
->Buffer
;
415 * Loop through each packet.
417 #define bhp ((struct bpf_hdr *)bp)
420 register int caplen
, hdrlen
;
423 * Has "pcap_breakloop()" been called?
424 * If so, return immediately - if we haven't read any
425 * packets, clear the flag and return PCAP_ERROR_BREAK
426 * to indicate that we were told to break out of the loop,
427 * otherwise leave the flag set, so that the *next* call
428 * will break out of the loop without having read any
429 * packets, and return the number of packets we've
435 return (PCAP_ERROR_BREAK
);
445 caplen
= bhp
->bh_caplen
;
446 hdrlen
= bhp
->bh_hdrlen
;
450 * Short-circuit evaluation: if using BPF filter
451 * in kernel, no need to do it now - we already know
452 * the packet passed the filter.
454 * XXX - bpf_filter() should always return TRUE if
455 * handed a null pointer for the program, but it might
456 * just try to "run" the filter, so we check here.
458 if (pw
->filtering_in_kernel
||
459 p
->fcode
.bf_insns
== NULL
||
460 bpf_filter(p
->fcode
.bf_insns
, datap
, bhp
->bh_datalen
, caplen
)) {
462 * XXX A bpf_hdr matches a pcap_pkthdr.
464 (*callback
)(user
, (struct pcap_pkthdr
*)bp
, datap
);
465 bp
+= Packet_WORDALIGN(caplen
+ hdrlen
);
466 if (++n
>= cnt
&& !PACKET_COUNT_IS_UNLIMITED(cnt
)) {
475 bp
+= Packet_WORDALIGN(caplen
+ hdrlen
);
485 pcap_read_win32_dag(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
487 struct pcap_win
*pw
= p
->priv
;
489 int packet_len
= 0, caplen
= 0;
490 struct pcap_pkthdr pcap_header
;
493 dag_record_t
*header
;
494 unsigned erf_record_len
;
498 unsigned dfp
= p
->adapter
->DagFastProcess
;
501 if (cc
== 0) /* Get new packets only if we have processed all the ones of the previous read */
503 /* Get new packets from the network */
504 if(PacketReceivePacket(p
->adapter
, p
->Packet
, TRUE
)==FALSE
){
505 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "read error: PacketReceivePacket failed");
509 cc
= p
->Packet
->ulBytesReceived
;
511 /* The timeout has expired but we no packets arrived */
513 header
= (dag_record_t
*)p
->adapter
->DagBuffer
;
516 header
= (dag_record_t
*)p
->bp
;
518 endofbuf
= (char*)header
+ cc
;
521 * Cycle through the packets
525 erf_record_len
= SWAPS(header
->rlen
);
526 if((char*)header
+ erf_record_len
> endofbuf
)
529 /* Increase the number of captured packets */
532 /* Find the beginning of the packet */
533 dp
= ((u_char
*)header
) + dag_record_size
;
535 /* Determine actual packet len */
539 packet_len
= ATM_SNAPLEN
;
540 caplen
= ATM_SNAPLEN
;
546 swt
= SWAPS(header
->wlen
);
547 packet_len
= swt
- (pw
->dag_fcs_bits
);
548 caplen
= erf_record_len
- dag_record_size
- 2;
549 if (caplen
> packet_len
)
558 swt
= SWAPS(header
->wlen
);
559 packet_len
= swt
- (pw
->dag_fcs_bits
);
560 caplen
= erf_record_len
- dag_record_size
;
561 if (caplen
> packet_len
)
569 if(caplen
> p
->snapshot
)
570 caplen
= p
->snapshot
;
573 * Has "pcap_breakloop()" been called?
574 * If so, return immediately - if we haven't read any
575 * packets, clear the flag and return -2 to indicate
576 * that we were told to break out of the loop, otherwise
577 * leave the flag set, so that the *next* call will break
578 * out of the loop without having read any packets, and
579 * return the number of packets we've processed so far.
590 p
->bp
= (char*)header
;
591 p
->cc
= endofbuf
- (char*)header
;
598 /* convert between timestamp formats */
600 pcap_header
.ts
.tv_sec
= (int)(ts
>> 32);
601 ts
= (ts
& 0xffffffffi
64) * 1000000;
602 ts
+= 0x80000000; /* rounding */
603 pcap_header
.ts
.tv_usec
= (int)(ts
>> 32);
604 if (pcap_header
.ts
.tv_usec
>= 1000000) {
605 pcap_header
.ts
.tv_usec
-= 1000000;
606 pcap_header
.ts
.tv_sec
++;
610 /* No underlaying filtering system. We need to filter on our own */
611 if (p
->fcode
.bf_insns
)
613 if (bpf_filter(p
->fcode
.bf_insns
, dp
, packet_len
, caplen
) == 0)
615 /* Move to next packet */
616 header
= (dag_record_t
*)((char*)header
+ erf_record_len
);
621 /* Fill the header for the user suppplied callback function */
622 pcap_header
.caplen
= caplen
;
623 pcap_header
.len
= packet_len
;
625 /* Call the callback function */
626 (*callback
)(user
, &pcap_header
, dp
);
628 /* Move to next packet */
629 header
= (dag_record_t
*)((char*)header
+ erf_record_len
);
631 /* Stop if the number of packets requested by user has been reached*/
632 if (++n
>= cnt
&& !PACKET_COUNT_IS_UNLIMITED(cnt
))
634 p
->bp
= (char*)header
;
635 p
->cc
= endofbuf
- (char*)header
;
639 while((u_char
*)header
< endofbuf
);
643 #endif /* HAVE_DAG_API */
645 /* Send a packet to the network */
647 pcap_inject_win32(pcap_t
*p
, const void *buf
, size_t size
){
648 LPPACKET PacketToSend
;
650 PacketToSend
=PacketAllocatePacket();
652 if (PacketToSend
== NULL
)
654 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "send error: PacketAllocatePacket failed");
658 PacketInitPacket(PacketToSend
,(PVOID
)buf
,size
);
659 if(PacketSendPacket(p
->adapter
,PacketToSend
,TRUE
) == FALSE
){
660 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "send error: PacketSendPacket failed");
661 PacketFreePacket(PacketToSend
);
665 PacketFreePacket(PacketToSend
);
668 * We assume it all got sent if "PacketSendPacket()" succeeded.
669 * "pcap_inject()" is expected to return the number of bytes
676 pcap_cleanup_win32(pcap_t
*p
)
678 if (p
->adapter
!= NULL
) {
679 PacketCloseAdapter(p
->adapter
);
683 PacketFreePacket(p
->Packet
);
686 pcap_cleanup_live_common(p
);
690 pcap_activate_win32(pcap_t
*p
)
692 struct pcap_win
*pw
= p
->priv
;
694 char errbuf
[PCAP_ERRBUF_SIZE
+1];
698 * No monitor mode on Windows. It could be done on
699 * Vista with drivers that support the native 802.11
700 * mechanism and monitor mode.
702 return (PCAP_ERROR_RFMON_NOTSUP
);
708 p
->adapter
= PacketOpenAdapter(p
->opt
.source
);
710 if (p
->adapter
== NULL
)
712 /* Adapter detected but we are not able to open it. Return failure. */
713 pcap_win32_err_to_str(GetLastError(), errbuf
);
714 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
715 "Error opening adapter: %s", errbuf
);
720 if(PacketGetNetType (p
->adapter
,&type
) == FALSE
)
722 pcap_win32_err_to_str(GetLastError(), errbuf
);
723 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
724 "Cannot determine the network type: %s", errbuf
);
729 switch (type
.LinkType
)
732 p
->linktype
= DLT_EN10MB
;
735 case NdisMedium802_3
:
736 p
->linktype
= DLT_EN10MB
;
738 * This is (presumably) a real Ethernet capture; give it a
739 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
740 * that an application can let you choose it, in case you're
741 * capturing DOCSIS traffic that a Cisco Cable Modem
742 * Termination System is putting out onto an Ethernet (it
743 * doesn't put an Ethernet header onto the wire, it puts raw
744 * DOCSIS frames out on the wire inside the low-level
747 p
->dlt_list
= (u_int
*) malloc(sizeof(u_int
) * 2);
749 * If that fails, just leave the list empty.
751 if (p
->dlt_list
!= NULL
) {
752 p
->dlt_list
[0] = DLT_EN10MB
;
753 p
->dlt_list
[1] = DLT_DOCSIS
;
759 p
->linktype
= DLT_FDDI
;
762 case NdisMedium802_5
:
763 p
->linktype
= DLT_IEEE802
;
766 case NdisMediumArcnetRaw
:
767 p
->linktype
= DLT_ARCNET
;
770 case NdisMediumArcnet878_2
:
771 p
->linktype
= DLT_ARCNET
;
775 p
->linktype
= DLT_ATM_RFC1483
;
778 case NdisMediumCHDLC
:
779 p
->linktype
= DLT_CHDLC
;
782 case NdisMediumPPPSerial
:
783 p
->linktype
= DLT_PPP_SERIAL
;
787 p
->linktype
= DLT_NULL
;
790 case NdisMediumBare80211
:
791 p
->linktype
= DLT_IEEE802_11
;
794 case NdisMediumRadio80211
:
795 p
->linktype
= DLT_IEEE802_11_RADIO
;
799 p
->linktype
= DLT_PPI
;
803 p
->linktype
= DLT_EN10MB
; /*an unknown adapter is assumed to be ethernet*/
807 /* Set promiscuous mode */
811 if (PacketSetHwFilter(p
->adapter
,NDIS_PACKET_TYPE_PROMISCUOUS
) == FALSE
)
813 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "failed to set hardware filter to promiscuous mode");
819 if (PacketSetHwFilter(p
->adapter
,NDIS_PACKET_TYPE_ALL_LOCAL
) == FALSE
)
821 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "failed to set hardware filter to non-promiscuous mode");
826 /* Set the buffer size */
827 p
->bufsize
= WIN32_DEFAULT_USER_BUFFER_SIZE
;
829 /* allocate Packet structure used during the capture */
830 if((p
->Packet
= PacketAllocatePacket())==NULL
)
832 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "failed to allocate the PACKET structure");
836 if(!(p
->adapter
->Flags
& INFO_FLAG_DAG_CARD
))
839 * Traditional Adapter
842 * If the buffer size wasn't explicitly set, default to
843 * WIN32_DEFAULT_KERNEL_BUFFER_SIZE.
845 if (p
->opt
.buffer_size
== 0)
846 p
->opt
.buffer_size
= WIN32_DEFAULT_KERNEL_BUFFER_SIZE
;
848 if(PacketSetBuff(p
->adapter
,p
->opt
.buffer_size
)==FALSE
)
850 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "driver error: not enough memory to allocate the kernel buffer");
854 p
->buffer
= malloc(p
->bufsize
);
855 if (p
->buffer
== NULL
)
857 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "malloc: %s", pcap_strerror(errno
));
861 PacketInitPacket(p
->Packet
,(BYTE
*)p
->buffer
,p
->bufsize
);
863 if (p
->opt
.immediate
)
865 /* tell the driver to copy the buffer as soon as data arrives */
866 if(PacketSetMinToCopy(p
->adapter
,0)==FALSE
)
868 pcap_win32_err_to_str(GetLastError(), errbuf
);
869 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
870 "Error calling PacketSetMinToCopy: %s",
877 /* tell the driver to copy the buffer only if it contains at least 16K */
878 if(PacketSetMinToCopy(p
->adapter
,16000)==FALSE
)
880 pcap_win32_err_to_str(GetLastError(), errbuf
);
881 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
882 "Error calling PacketSetMinToCopy: %s",
901 snprintf(keyname
, sizeof(keyname
), "%s\\CardParams\\%s",
902 "SYSTEM\\CurrentControlSet\\Services\\DAG",
903 strstr(_strlwr(p
->opt
.source
), "dag"));
906 status
= RegOpenKeyEx(HKEY_LOCAL_MACHINE
, keyname
, 0, KEY_READ
, &dagkey
);
907 if(status
!= ERROR_SUCCESS
)
910 status
= RegQueryValueEx(dagkey
,
917 if(status
!= ERROR_SUCCESS
)
927 p
->snapshot
= PacketSetSnapLen(p
->adapter
, snaplen
);
929 /* Set the length of the FCS associated to any packet. This value
930 * will be subtracted to the packet length */
931 pw
->dag_fcs_bits
= p
->adapter
->DagFcsLen
;
935 #endif /* HAVE_DAG_API */
937 PacketSetReadTimeout(p
->adapter
, p
->opt
.timeout
);
940 if(p
->adapter
->Flags
& INFO_FLAG_DAG_CARD
)
942 /* install dag specific handlers for read and setfilter */
943 p
->read_op
= pcap_read_win32_dag
;
944 p
->setfilter_op
= pcap_setfilter_win32_dag
;
948 #endif /* HAVE_DAG_API */
949 /* install traditional npf handlers for read and setfilter */
950 p
->read_op
= pcap_read_win32_npf
;
951 p
->setfilter_op
= pcap_setfilter_win32_npf
;
954 #endif /* HAVE_DAG_API */
955 p
->setdirection_op
= NULL
; /* Not implemented. */
956 /* XXX - can this be implemented on some versions of Windows? */
957 p
->inject_op
= pcap_inject_win32
;
958 p
->set_datalink_op
= NULL
; /* can't change data link type */
959 p
->getnonblock_op
= pcap_getnonblock_win32
;
960 p
->setnonblock_op
= pcap_setnonblock_win32
;
961 p
->stats_op
= pcap_stats_win32
;
962 p
->stats_ex_op
= pcap_stats_ex_win32
;
963 p
->setbuff_op
= pcap_setbuff_win32
;
964 p
->setmode_op
= pcap_setmode_win32
;
965 p
->setmintocopy_op
= pcap_setmintocopy_win32
;
966 p
->getevent_op
= pcap_getevent_win32
;
967 p
->oid_get_request_op
= pcap_oid_get_request_win32
;
968 p
->oid_set_request_op
= pcap_oid_set_request_win32
;
969 p
->sendqueue_transmit_op
= pcap_sendqueue_transmit_win32
;
970 p
->live_dump_op
= pcap_live_dump_win32
;
971 p
->live_dump_ended_op
= pcap_live_dump_ended_win32
;
972 p
->get_airpcap_handle_op
= pcap_get_airpcap_handle_win32
;
973 p
->cleanup_op
= pcap_cleanup_win32
;
977 pcap_cleanup_win32(p
);
982 pcap_create_interface(const char *device
, char *ebuf
)
986 if (strlen(device
) == 1)
989 * It's probably a unicode string
990 * Convert to ascii and pass it to pcap_create_common
992 * This wonderful hack is needed because pcap_lookupdev still returns
993 * unicode strings, and it's used by windump when no device is specified
994 * in the command line
999 length
= wcslen((wchar_t*)device
);
1001 deviceAscii
= (char*)malloc(length
+ 1);
1003 if (deviceAscii
== NULL
)
1005 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "Malloc failed");
1009 snprintf(deviceAscii
, length
+ 1, "%ws", (wchar_t*)device
);
1010 p
= pcap_create_common(deviceAscii
, ebuf
, sizeof (struct pcap_win
));
1015 p
= pcap_create_common(device
, ebuf
, sizeof (struct pcap_win
));
1021 p
->activate_op
= pcap_activate_win32
;
1026 pcap_setfilter_win32_npf(pcap_t
*p
, struct bpf_program
*fp
)
1028 struct pcap_win
*pw
= p
->priv
;
1030 if(PacketSetBpf(p
->adapter
,fp
)==FALSE
){
1032 * Kernel filter not installed.
1034 * XXX - we don't know whether this failed because:
1036 * the kernel rejected the filter program as invalid,
1037 * in which case we should fall back on userland
1040 * the kernel rejected the filter program as too big,
1041 * in which case we should again fall back on
1042 * userland filtering;
1044 * there was some other problem, in which case we
1045 * should probably report an error.
1047 * For NPF devices, the Win32 status will be
1048 * STATUS_INVALID_DEVICE_REQUEST for invalid
1049 * filters, but I don't know what it'd be for
1050 * other problems, and for some other devices
1051 * it might not be set at all.
1053 * So we just fall back on userland filtering in
1058 * install_bpf_program() validates the program.
1060 * XXX - what if we already have a filter in the kernel?
1062 if (install_bpf_program(p
, fp
) < 0)
1064 pw
->filtering_in_kernel
= 0; /* filtering in userland */
1071 pw
->filtering_in_kernel
= 1; /* filtering in the kernel */
1074 * Discard any previously-received packets, as they might have
1075 * passed whatever filter was formerly in effect, but might
1076 * not pass this filter (BIOCSETF discards packets buffered
1077 * in the kernel, so you can lose packets in any case).
1084 * We filter at user level, since the kernel driver does't process the packets
1087 pcap_setfilter_win32_dag(pcap_t
*p
, struct bpf_program
*fp
) {
1091 strncpy(p
->errbuf
, "setfilter: No filter specified", sizeof(p
->errbuf
));
1095 /* Install a user level filter */
1096 if (install_bpf_program(p
, fp
) < 0)
1098 snprintf(p
->errbuf
, sizeof(p
->errbuf
),
1099 "setfilter, unable to install the filter: %s", pcap_strerror(errno
));
1107 pcap_getnonblock_win32(pcap_t
*p
, char *errbuf
)
1109 struct pcap_win
*pw
= p
->priv
;
1112 * XXX - if there were a PacketGetReadTimeout() call, we
1113 * would use it, and return 1 if the timeout is -1
1116 return (pw
->nonblock
);
1120 pcap_setnonblock_win32(pcap_t
*p
, int nonblock
, char *errbuf
)
1122 struct pcap_win
*pw
= p
->priv
;
1124 char errbuf
[PCAP_ERRBUF_SIZE
+1];
1128 * Set the read timeout to -1 for non-blocking mode.
1133 * Restore the timeout set when the device was opened.
1134 * (Note that this may be -1, in which case we're not
1135 * really leaving non-blocking mode.)
1137 newtimeout
= p
->opt
.timeout
;
1139 if (!PacketSetReadTimeout(p
->adapter
, newtimeout
)) {
1140 pcap_win32_err_to_str(GetLastError(), errbuf
);
1141 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1142 "PacketSetReadTimeout: %s", errbuf
);
1145 pw
->nonblock
= (newtimeout
== -1);