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__ */
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 * Private data for capturing on WinPcap devices.
75 int filtering_in_kernel
; /* using kernel filter */
78 int dag_fcs_bits
; /* Number of checksum bits from link layer */
82 CRITICAL_SECTION g_PcapCompileCriticalSection
;
90 if (dwReason
== DLL_PROCESS_ATTACH
)
92 InitializeCriticalSection(&g_PcapCompileCriticalSection
);
102 WORD wVersionRequested
;
110 wVersionRequested
= MAKEWORD( 1, 1);
111 err
= WSAStartup( wVersionRequested
, &wsaData
);
112 atexit ((void(*)(void))WSACleanup
);
113 InitializeCriticalSection(&g_PcapCompileCriticalSection
);
128 pcap_stats_win32(pcap_t
*p
, struct pcap_stat
*ps
)
130 struct bpf_stat bstats
;
131 char errbuf
[PCAP_ERRBUF_SIZE
+1];
134 * Try to get statistics.
136 * (Please note - "struct pcap_stat" is *not* the same as
137 * WinPcap's "struct bpf_stat". It might currently have the
138 * same layout, but let's not cheat.
140 * Note also that we don't fill in ps_capt, as we might have
141 * been called by code compiled against an earlier version of
142 * WinPcap that didn't have ps_capt, in which case filling it
143 * in would stomp on whatever comes after the structure passed
146 if (!PacketGetStats(p
->adapter
, &bstats
)) {
147 pcap_win32_err_to_str(GetLastError(), errbuf
);
148 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
149 "PacketGetStats error: %s", errbuf
);
152 ps
->ps_recv
= bstats
.bs_recv
;
153 ps
->ps_drop
= bstats
.bs_drop
;
156 * XXX - PacketGetStats() doesn't fill this in, so we just
160 ps
->ps_ifdrop
= bstats
.ps_ifdrop
;
169 * Win32-only routine for getting statistics.
171 * This way is definitely safer than passing the pcap_stat * from the userland.
172 * In fact, there could happen than the user allocates a variable which is not
173 * big enough for the new structure, and the library will write in a zone
174 * which is not allocated to this variable.
176 * In this way, we're pretty sure we are writing on memory allocated to this
179 * XXX - but this is the wrong way to handle statistics. Instead, we should
180 * have an API that returns data in a form like the Options section of a
181 * pcapng Interface Statistics Block:
183 * 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
185 * which would let us add new statistics straightforwardly and indicate which
186 * statistics we are and are *not* providing, rather than having to provide
187 * possibly-bogus values for statistics we can't provide.
190 pcap_stats_ex_win32(pcap_t
*p
, int *pcap_stat_size
)
192 struct bpf_stat bstats
;
193 char errbuf
[PCAP_ERRBUF_SIZE
+1];
195 *pcap_stat_size
= sizeof (p
->stat
);
198 * Try to get statistics.
200 * (Please note - "struct pcap_stat" is *not* the same as
201 * WinPcap's "struct bpf_stat". It might currently have the
202 * same layout, but let's not cheat.)
204 if (!PacketGetStatsEx(p
->adapter
, &bstats
)) {
205 pcap_win32_err_to_str(GetLastError(), errbuf
);
206 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
207 "PacketGetStatsEx error: %s", errbuf
);
210 p
->stat
.ps_recv
= bstats
.bs_recv
;
211 p
->stat
.ps_drop
= bstats
.bs_drop
;
212 p
->stat
.ps_ifdrop
= bstats
.ps_ifdrop
;
213 p
->stat
.ps_capt
= bstats
.bs_capt
;
217 /* Set the dimension of the kernel-level capture buffer */
219 pcap_setbuff_win32(pcap_t
*p
, int dim
)
221 if(PacketSetBuff(p
->adapter
,dim
)==FALSE
)
223 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "driver error: not enough memory to allocate the kernel buffer");
229 /* Set the driver working mode */
231 pcap_setmode_win32(pcap_t
*p
, int mode
)
233 if(PacketSetMode(p
->adapter
,mode
)==FALSE
)
235 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "driver error: working mode not recognized");
242 /*set the minimum amount of data that will release a read call*/
244 pcap_setmintocopy_win32(pcap_t
*p
, int size
)
246 if(PacketSetMinToCopy(p
->adapter
, size
)==FALSE
)
248 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "driver error: unable to set the requested mintocopy size");
255 pcap_getevent_win32(pcap_t
*p
)
257 return PacketGetReadEvent(p
->adapter
);
261 pcap_oid_get_request_win32(pcap_t
*p
, pcap_oid_data_t
*data
)
263 char errbuf
[PCAP_ERRBUF_SIZE
+1];
265 if (!PacketRequest(p
->adapter
, FALSE
, data
)) {
266 pcap_win32_err_to_str(GetLastError(), errbuf
);
267 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
268 "Error calling PacketRequest: %s", errbuf
);
275 pcap_oid_set_request_win32(pcap_t
*p
, pcap_oid_data_t
*data
)
277 char errbuf
[PCAP_ERRBUF_SIZE
+1];
279 if (!PacketRequest(p
->adapter
, TRUE
, data
)) {
280 pcap_win32_err_to_str(GetLastError(), errbuf
);
281 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
282 "Error calling PacketRequest: %s", errbuf
);
289 pcap_sendqueue_transmit_win32(pcap_t
*p
, pcap_send_queue
*queue
, int sync
)
292 char errbuf
[PCAP_ERRBUF_SIZE
+1];
294 if (p
->adapter
==NULL
) {
295 sprintf(p
->errbuf
, "Cannot transmit a queue to an offline capture or to a TurboCap port");
299 res
= PacketSendPackets(p
->adapter
,
304 if(res
!= queue
->len
){
305 pcap_win32_err_to_str(GetLastError(), errbuf
);
306 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
307 "Error opening adapter: %s", errbuf
);
314 pcap_setuserbuffer_win32(pcap_t
*p
, int size
)
316 unsigned char *new_buff
;
319 /* Bogus parameter */
320 sprintf(p
->errbuf
,"Error: invalid size %d",size
);
324 /* Allocate the buffer */
325 new_buff
=(unsigned char*)malloc(sizeof(char)*size
);
328 sprintf(p
->errbuf
,"Error: not enough memory");
341 pcap_live_dump_win32(pcap_t
*p
, char *filename
, int maxsize
, int maxpacks
)
345 /* Set the packet driver in dump mode */
346 res
= PacketSetMode(p
->adapter
, PACKET_MODE_DUMP
);
348 sprintf(p
->errbuf
, "Error setting dump mode");
352 /* Set the name of the dump file */
353 res
= PacketSetDumpName(p
->adapter
, filename
, strlen(filename
));
355 sprintf(p
->errbuf
, "Error setting kernel dump file name");
359 /* Set the limits of the dump file */
360 res
= PacketSetDumpLimits(p
->adapter
, maxsize
, maxpacks
);
366 pcap_live_dump_ended_win32(pcap_t
*p
, int sync
)
368 return PacketIsDumpEnded(p
->adapter
, (BOOLEAN
)sync
);
371 static PAirpcapHandle
372 pcap_get_airpcap_handle_win32(pcap_t
*p
)
374 #ifdef HAVE_AIRPCAP_API
375 return PacketGetAirPcapHandle(p
->adapter
);
378 #endif /* HAVE_AIRPCAP_API */
382 pcap_read_win32_npf(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
387 register u_char
*bp
, *ep
;
389 struct pcap_win
*pw
= p
->priv
;
394 * Has "pcap_breakloop()" been called?
398 * Yes - clear the flag that indicates that it
399 * has, and return PCAP_ERROR_BREAK to indicate
400 * that we were told to break out of the loop.
403 return (PCAP_ERROR_BREAK
);
407 * Capture the packets.
409 * The PACKET structure had a bunch of extra stuff for
410 * Windows 9x/Me, but the only interesting data in it
411 * in the versions of Windows that we support is just
412 * a copy of p->buffer, a copy of p->buflen, and the
413 * actual number of bytes read returned from
414 * PacketReceivePacket(), none of which has to be
415 * retained from call to call, so we just keep one on
418 PacketInitPacket(&Packet
, (BYTE
*)p
->buffer
, p
->bufsize
);
419 if (!PacketReceivePacket(p
->adapter
, &Packet
, TRUE
)) {
420 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "read error: PacketReceivePacket failed");
424 cc
= Packet
.ulBytesReceived
;
432 * Loop through each packet.
434 #define bhp ((struct bpf_hdr *)bp)
437 register int caplen
, hdrlen
;
440 * Has "pcap_breakloop()" been called?
441 * If so, return immediately - if we haven't read any
442 * packets, clear the flag and return PCAP_ERROR_BREAK
443 * to indicate that we were told to break out of the loop,
444 * otherwise leave the flag set, so that the *next* call
445 * will break out of the loop without having read any
446 * packets, and return the number of packets we've
452 return (PCAP_ERROR_BREAK
);
462 caplen
= bhp
->bh_caplen
;
463 hdrlen
= bhp
->bh_hdrlen
;
467 * Short-circuit evaluation: if using BPF filter
468 * in kernel, no need to do it now - we already know
469 * the packet passed the filter.
471 * XXX - bpf_filter() should always return TRUE if
472 * handed a null pointer for the program, but it might
473 * just try to "run" the filter, so we check here.
475 if (pw
->filtering_in_kernel
||
476 p
->fcode
.bf_insns
== NULL
||
477 bpf_filter(p
->fcode
.bf_insns
, datap
, bhp
->bh_datalen
, caplen
)) {
479 * XXX A bpf_hdr matches a pcap_pkthdr.
481 (*callback
)(user
, (struct pcap_pkthdr
*)bp
, datap
);
482 bp
+= Packet_WORDALIGN(caplen
+ hdrlen
);
483 if (++n
>= cnt
&& !PACKET_COUNT_IS_UNLIMITED(cnt
)) {
492 bp
+= Packet_WORDALIGN(caplen
+ hdrlen
);
502 pcap_read_win32_dag(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
504 struct pcap_win
*pw
= p
->priv
;
507 int packet_len
= 0, caplen
= 0;
508 struct pcap_pkthdr pcap_header
;
511 dag_record_t
*header
;
512 unsigned erf_record_len
;
516 unsigned dfp
= p
->adapter
->DagFastProcess
;
519 if (cc
== 0) /* Get new packets only if we have processed all the ones of the previous read */
522 * Get new packets from the network.
524 * The PACKET structure had a bunch of extra stuff for
525 * Windows 9x/Me, but the only interesting data in it
526 * in the versions of Windows that we support is just
527 * a copy of p->buffer, a copy of p->buflen, and the
528 * actual number of bytes read returned from
529 * PacketReceivePacket(), none of which has to be
530 * retained from call to call, so we just keep one on
533 PacketInitPacket(&Packet
, (BYTE
*)p
->buffer
, p
->bufsize
);
534 if (!PacketReceivePacket(p
->adapter
, &Packet
, TRUE
)) {
535 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "read error: PacketReceivePacket failed");
539 cc
= Packet
.ulBytesReceived
;
541 /* The timeout has expired but we no packets arrived */
543 header
= (dag_record_t
*)p
->adapter
->DagBuffer
;
546 header
= (dag_record_t
*)p
->bp
;
548 endofbuf
= (char*)header
+ cc
;
551 * Cycle through the packets
555 erf_record_len
= SWAPS(header
->rlen
);
556 if((char*)header
+ erf_record_len
> endofbuf
)
559 /* Increase the number of captured packets */
562 /* Find the beginning of the packet */
563 dp
= ((u_char
*)header
) + dag_record_size
;
565 /* Determine actual packet len */
569 packet_len
= ATM_SNAPLEN
;
570 caplen
= ATM_SNAPLEN
;
576 swt
= SWAPS(header
->wlen
);
577 packet_len
= swt
- (pw
->dag_fcs_bits
);
578 caplen
= erf_record_len
- dag_record_size
- 2;
579 if (caplen
> packet_len
)
588 swt
= SWAPS(header
->wlen
);
589 packet_len
= swt
- (pw
->dag_fcs_bits
);
590 caplen
= erf_record_len
- dag_record_size
;
591 if (caplen
> packet_len
)
599 if(caplen
> p
->snapshot
)
600 caplen
= p
->snapshot
;
603 * Has "pcap_breakloop()" been called?
604 * If so, return immediately - if we haven't read any
605 * packets, clear the flag and return -2 to indicate
606 * that we were told to break out of the loop, otherwise
607 * leave the flag set, so that the *next* call will break
608 * out of the loop without having read any packets, and
609 * return the number of packets we've processed so far.
620 p
->bp
= (char*)header
;
621 p
->cc
= endofbuf
- (char*)header
;
628 /* convert between timestamp formats */
630 pcap_header
.ts
.tv_sec
= (int)(ts
>> 32);
631 ts
= (ts
& 0xffffffffi
64) * 1000000;
632 ts
+= 0x80000000; /* rounding */
633 pcap_header
.ts
.tv_usec
= (int)(ts
>> 32);
634 if (pcap_header
.ts
.tv_usec
>= 1000000) {
635 pcap_header
.ts
.tv_usec
-= 1000000;
636 pcap_header
.ts
.tv_sec
++;
640 /* No underlaying filtering system. We need to filter on our own */
641 if (p
->fcode
.bf_insns
)
643 if (bpf_filter(p
->fcode
.bf_insns
, dp
, packet_len
, caplen
) == 0)
645 /* Move to next packet */
646 header
= (dag_record_t
*)((char*)header
+ erf_record_len
);
651 /* Fill the header for the user suppplied callback function */
652 pcap_header
.caplen
= caplen
;
653 pcap_header
.len
= packet_len
;
655 /* Call the callback function */
656 (*callback
)(user
, &pcap_header
, dp
);
658 /* Move to next packet */
659 header
= (dag_record_t
*)((char*)header
+ erf_record_len
);
661 /* Stop if the number of packets requested by user has been reached*/
662 if (++n
>= cnt
&& !PACKET_COUNT_IS_UNLIMITED(cnt
))
664 p
->bp
= (char*)header
;
665 p
->cc
= endofbuf
- (char*)header
;
669 while((u_char
*)header
< endofbuf
);
673 #endif /* HAVE_DAG_API */
675 /* Send a packet to the network */
677 pcap_inject_win32(pcap_t
*p
, const void *buf
, size_t size
){
678 LPPACKET PacketToSend
;
680 PacketToSend
=PacketAllocatePacket();
682 if (PacketToSend
== NULL
)
684 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "send error: PacketAllocatePacket failed");
688 PacketInitPacket(PacketToSend
,(PVOID
)buf
,size
);
689 if(PacketSendPacket(p
->adapter
,PacketToSend
,TRUE
) == FALSE
){
690 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "send error: PacketSendPacket failed");
691 PacketFreePacket(PacketToSend
);
695 PacketFreePacket(PacketToSend
);
698 * We assume it all got sent if "PacketSendPacket()" succeeded.
699 * "pcap_inject()" is expected to return the number of bytes
706 pcap_cleanup_win32(pcap_t
*p
)
708 if (p
->adapter
!= NULL
) {
709 PacketCloseAdapter(p
->adapter
);
713 PacketFreePacket(p
->Packet
);
716 pcap_cleanup_live_common(p
);
720 pcap_activate_win32(pcap_t
*p
)
723 struct pcap_win
*pw
= p
->priv
;
726 char errbuf
[PCAP_ERRBUF_SIZE
+1];
730 * No monitor mode on Windows. It could be done on
731 * Vista with drivers that support the native 802.11
732 * mechanism and monitor mode.
734 return (PCAP_ERROR_RFMON_NOTSUP
);
740 p
->adapter
= PacketOpenAdapter(p
->opt
.source
);
742 if (p
->adapter
== NULL
)
744 /* Adapter detected but we are not able to open it. Return failure. */
745 pcap_win32_err_to_str(GetLastError(), errbuf
);
746 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
747 "Error opening adapter: %s", errbuf
);
752 if(PacketGetNetType (p
->adapter
,&type
) == FALSE
)
754 pcap_win32_err_to_str(GetLastError(), errbuf
);
755 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
756 "Cannot determine the network type: %s", errbuf
);
761 switch (type
.LinkType
)
764 p
->linktype
= DLT_EN10MB
;
767 case NdisMedium802_3
:
768 p
->linktype
= DLT_EN10MB
;
770 * This is (presumably) a real Ethernet capture; give it a
771 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
772 * that an application can let you choose it, in case you're
773 * capturing DOCSIS traffic that a Cisco Cable Modem
774 * Termination System is putting out onto an Ethernet (it
775 * doesn't put an Ethernet header onto the wire, it puts raw
776 * DOCSIS frames out on the wire inside the low-level
779 p
->dlt_list
= (u_int
*) malloc(sizeof(u_int
) * 2);
781 * If that fails, just leave the list empty.
783 if (p
->dlt_list
!= NULL
) {
784 p
->dlt_list
[0] = DLT_EN10MB
;
785 p
->dlt_list
[1] = DLT_DOCSIS
;
791 p
->linktype
= DLT_FDDI
;
794 case NdisMedium802_5
:
795 p
->linktype
= DLT_IEEE802
;
798 case NdisMediumArcnetRaw
:
799 p
->linktype
= DLT_ARCNET
;
802 case NdisMediumArcnet878_2
:
803 p
->linktype
= DLT_ARCNET
;
807 p
->linktype
= DLT_ATM_RFC1483
;
810 case NdisMediumCHDLC
:
811 p
->linktype
= DLT_CHDLC
;
814 case NdisMediumPPPSerial
:
815 p
->linktype
= DLT_PPP_SERIAL
;
819 p
->linktype
= DLT_NULL
;
822 case NdisMediumBare80211
:
823 p
->linktype
= DLT_IEEE802_11
;
826 case NdisMediumRadio80211
:
827 p
->linktype
= DLT_IEEE802_11_RADIO
;
831 p
->linktype
= DLT_PPI
;
835 p
->linktype
= DLT_EN10MB
; /*an unknown adapter is assumed to be ethernet*/
839 /* Set promiscuous mode */
843 if (PacketSetHwFilter(p
->adapter
,NDIS_PACKET_TYPE_PROMISCUOUS
) == FALSE
)
845 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "failed to set hardware filter to promiscuous mode");
851 if (PacketSetHwFilter(p
->adapter
,NDIS_PACKET_TYPE_ALL_LOCAL
) == FALSE
)
853 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "failed to set hardware filter to non-promiscuous mode");
858 /* Set the buffer size */
859 p
->bufsize
= WIN32_DEFAULT_USER_BUFFER_SIZE
;
861 if(!(p
->adapter
->Flags
& INFO_FLAG_DAG_CARD
))
864 * Traditional Adapter
867 * If the buffer size wasn't explicitly set, default to
868 * WIN32_DEFAULT_KERNEL_BUFFER_SIZE.
870 if (p
->opt
.buffer_size
== 0)
871 p
->opt
.buffer_size
= WIN32_DEFAULT_KERNEL_BUFFER_SIZE
;
873 if(PacketSetBuff(p
->adapter
,p
->opt
.buffer_size
)==FALSE
)
875 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "driver error: not enough memory to allocate the kernel buffer");
879 p
->buffer
= malloc(p
->bufsize
);
880 if (p
->buffer
== NULL
)
882 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "malloc: %s", pcap_strerror(errno
));
886 if (p
->opt
.immediate
)
888 /* tell the driver to copy the buffer as soon as data arrives */
889 if(PacketSetMinToCopy(p
->adapter
,0)==FALSE
)
891 pcap_win32_err_to_str(GetLastError(), errbuf
);
892 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
893 "Error calling PacketSetMinToCopy: %s",
900 /* tell the driver to copy the buffer only if it contains at least 16K */
901 if(PacketSetMinToCopy(p
->adapter
,16000)==FALSE
)
903 pcap_win32_err_to_str(GetLastError(), errbuf
);
904 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
905 "Error calling PacketSetMinToCopy: %s",
924 snprintf(keyname
, sizeof(keyname
), "%s\\CardParams\\%s",
925 "SYSTEM\\CurrentControlSet\\Services\\DAG",
926 strstr(_strlwr(p
->opt
.source
), "dag"));
929 status
= RegOpenKeyEx(HKEY_LOCAL_MACHINE
, keyname
, 0, KEY_READ
, &dagkey
);
930 if(status
!= ERROR_SUCCESS
)
933 status
= RegQueryValueEx(dagkey
,
940 if(status
!= ERROR_SUCCESS
)
950 p
->snapshot
= PacketSetSnapLen(p
->adapter
, snaplen
);
952 /* Set the length of the FCS associated to any packet. This value
953 * will be subtracted to the packet length */
954 pw
->dag_fcs_bits
= p
->adapter
->DagFcsLen
;
958 #endif /* HAVE_DAG_API */
960 PacketSetReadTimeout(p
->adapter
, p
->opt
.timeout
);
963 if(p
->adapter
->Flags
& INFO_FLAG_DAG_CARD
)
965 /* install dag specific handlers for read and setfilter */
966 p
->read_op
= pcap_read_win32_dag
;
967 p
->setfilter_op
= pcap_setfilter_win32_dag
;
971 #endif /* HAVE_DAG_API */
972 /* install traditional npf handlers for read and setfilter */
973 p
->read_op
= pcap_read_win32_npf
;
974 p
->setfilter_op
= pcap_setfilter_win32_npf
;
977 #endif /* HAVE_DAG_API */
978 p
->setdirection_op
= NULL
; /* Not implemented. */
979 /* XXX - can this be implemented on some versions of Windows? */
980 p
->inject_op
= pcap_inject_win32
;
981 p
->set_datalink_op
= NULL
; /* can't change data link type */
982 p
->getnonblock_op
= pcap_getnonblock_win32
;
983 p
->setnonblock_op
= pcap_setnonblock_win32
;
984 p
->stats_op
= pcap_stats_win32
;
985 p
->stats_ex_op
= pcap_stats_ex_win32
;
986 p
->setbuff_op
= pcap_setbuff_win32
;
987 p
->setmode_op
= pcap_setmode_win32
;
988 p
->setmintocopy_op
= pcap_setmintocopy_win32
;
989 p
->getevent_op
= pcap_getevent_win32
;
990 p
->oid_get_request_op
= pcap_oid_get_request_win32
;
991 p
->oid_set_request_op
= pcap_oid_set_request_win32
;
992 p
->sendqueue_transmit_op
= pcap_sendqueue_transmit_win32
;
993 p
->setuserbuffer_op
= pcap_setuserbuffer_win32
;
994 p
->live_dump_op
= pcap_live_dump_win32
;
995 p
->live_dump_ended_op
= pcap_live_dump_ended_win32
;
996 p
->get_airpcap_handle_op
= pcap_get_airpcap_handle_win32
;
997 p
->cleanup_op
= pcap_cleanup_win32
;
1001 pcap_cleanup_win32(p
);
1002 return (PCAP_ERROR
);
1006 pcap_create_interface(const char *device
, char *ebuf
)
1010 if (strlen(device
) == 1)
1013 * It's probably a unicode string
1014 * Convert to ascii and pass it to pcap_create_common
1016 * This wonderful hack is needed because pcap_lookupdev still returns
1017 * unicode strings, and it's used by windump when no device is specified
1018 * in the command line
1023 length
= wcslen((wchar_t*)device
);
1025 deviceAscii
= (char*)malloc(length
+ 1);
1027 if (deviceAscii
== NULL
)
1029 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "Malloc failed");
1033 snprintf(deviceAscii
, length
+ 1, "%ws", (wchar_t*)device
);
1034 p
= pcap_create_common(deviceAscii
, ebuf
, sizeof (struct pcap_win
));
1039 p
= pcap_create_common(device
, ebuf
, sizeof (struct pcap_win
));
1045 p
->activate_op
= pcap_activate_win32
;
1050 pcap_setfilter_win32_npf(pcap_t
*p
, struct bpf_program
*fp
)
1052 struct pcap_win
*pw
= p
->priv
;
1054 if(PacketSetBpf(p
->adapter
,fp
)==FALSE
){
1056 * Kernel filter not installed.
1058 * XXX - we don't know whether this failed because:
1060 * the kernel rejected the filter program as invalid,
1061 * in which case we should fall back on userland
1064 * the kernel rejected the filter program as too big,
1065 * in which case we should again fall back on
1066 * userland filtering;
1068 * there was some other problem, in which case we
1069 * should probably report an error.
1071 * For NPF devices, the Win32 status will be
1072 * STATUS_INVALID_DEVICE_REQUEST for invalid
1073 * filters, but I don't know what it'd be for
1074 * other problems, and for some other devices
1075 * it might not be set at all.
1077 * So we just fall back on userland filtering in
1082 * install_bpf_program() validates the program.
1084 * XXX - what if we already have a filter in the kernel?
1086 if (install_bpf_program(p
, fp
) < 0)
1088 pw
->filtering_in_kernel
= 0; /* filtering in userland */
1095 pw
->filtering_in_kernel
= 1; /* filtering in the kernel */
1098 * Discard any previously-received packets, as they might have
1099 * passed whatever filter was formerly in effect, but might
1100 * not pass this filter (BIOCSETF discards packets buffered
1101 * in the kernel, so you can lose packets in any case).
1108 * We filter at user level, since the kernel driver does't process the packets
1111 pcap_setfilter_win32_dag(pcap_t
*p
, struct bpf_program
*fp
) {
1115 strncpy(p
->errbuf
, "setfilter: No filter specified", sizeof(p
->errbuf
));
1119 /* Install a user level filter */
1120 if (install_bpf_program(p
, fp
) < 0)
1122 snprintf(p
->errbuf
, sizeof(p
->errbuf
),
1123 "setfilter, unable to install the filter: %s", pcap_strerror(errno
));
1131 pcap_getnonblock_win32(pcap_t
*p
, char *errbuf
)
1133 struct pcap_win
*pw
= p
->priv
;
1136 * XXX - if there were a PacketGetReadTimeout() call, we
1137 * would use it, and return 1 if the timeout is -1
1140 return (pw
->nonblock
);
1144 pcap_setnonblock_win32(pcap_t
*p
, int nonblock
, char *errbuf
)
1146 struct pcap_win
*pw
= p
->priv
;
1148 char win_errbuf
[PCAP_ERRBUF_SIZE
+1];
1152 * Set the read timeout to -1 for non-blocking mode.
1157 * Restore the timeout set when the device was opened.
1158 * (Note that this may be -1, in which case we're not
1159 * really leaving non-blocking mode. However, although
1160 * the timeout argument to pcap_set_timeout() and
1161 * pcap_open_live() is an int, you're not supposed to
1162 * supply a negative value, so that "shouldn't happen".)
1164 newtimeout
= p
->opt
.timeout
;
1166 if (!PacketSetReadTimeout(p
->adapter
, newtimeout
)) {
1167 pcap_win32_err_to_str(GetLastError(), win_errbuf
);
1168 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1169 "PacketSetReadTimeout: %s", win_errbuf
);
1172 pw
->nonblock
= (newtimeout
== -1);