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.
34 #define PCAP_DONT_INCLUDE_PCAP_BPF_H
42 #include <ddk/ntddndis.h>
44 #endif /*__MINGW64__*/
47 #endif /*__MINGW32__*/
51 #endif /* HAVE_DAG_API */
54 #define errno (*_errno())
55 #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 */
97 WORD wVersionRequested
;
105 wVersionRequested
= MAKEWORD( 1, 1);
106 err
= WSAStartup( wVersionRequested
, &wsaData
);
107 atexit ((void(*)(void))WSACleanup
);
118 return (wsockinit());
122 pcap_stats_win32(pcap_t
*p
, struct pcap_stat
*ps
)
124 struct bpf_stat bstats
;
125 char errbuf
[PCAP_ERRBUF_SIZE
+1];
128 * Try to get statistics.
130 * (Please note - "struct pcap_stat" is *not* the same as
131 * WinPcap's "struct bpf_stat". It might currently have the
132 * same layout, but let's not cheat.
134 * Note also that we don't fill in ps_capt, as we might have
135 * been called by code compiled against an earlier version of
136 * WinPcap that didn't have ps_capt, in which case filling it
137 * in would stomp on whatever comes after the structure passed
140 if (!PacketGetStats(p
->adapter
, &bstats
)) {
141 pcap_win32_err_to_str(GetLastError(), errbuf
);
142 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
143 "PacketGetStats error: %s", errbuf
);
146 ps
->ps_recv
= bstats
.bs_recv
;
147 ps
->ps_drop
= bstats
.bs_drop
;
150 * XXX - PacketGetStats() doesn't fill this in, so we just
154 ps
->ps_ifdrop
= bstats
.ps_ifdrop
;
163 * Win32-only routine for getting statistics.
165 * This way is definitely safer than passing the pcap_stat * from the userland.
166 * In fact, there could happen than the user allocates a variable which is not
167 * big enough for the new structure, and the library will write in a zone
168 * which is not allocated to this variable.
170 * In this way, we're pretty sure we are writing on memory allocated to this
173 * XXX - but this is the wrong way to handle statistics. Instead, we should
174 * have an API that returns data in a form like the Options section of a
175 * pcapng Interface Statistics Block:
177 * 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
179 * which would let us add new statistics straightforwardly and indicate which
180 * statistics we are and are *not* providing, rather than having to provide
181 * possibly-bogus values for statistics we can't provide.
184 pcap_stats_ex_win32(pcap_t
*p
, int *pcap_stat_size
)
186 struct bpf_stat bstats
;
187 char errbuf
[PCAP_ERRBUF_SIZE
+1];
189 *pcap_stat_size
= sizeof (p
->stat
);
192 * Try to get statistics.
194 * (Please note - "struct pcap_stat" is *not* the same as
195 * WinPcap's "struct bpf_stat". It might currently have the
196 * same layout, but let's not cheat.)
198 if (!PacketGetStatsEx(p
->adapter
, &bstats
)) {
199 pcap_win32_err_to_str(GetLastError(), errbuf
);
200 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
201 "PacketGetStatsEx error: %s", errbuf
);
204 p
->stat
.ps_recv
= bstats
.bs_recv
;
205 p
->stat
.ps_drop
= bstats
.bs_drop
;
206 p
->stat
.ps_ifdrop
= bstats
.ps_ifdrop
;
208 p
->stat
.ps_capt
= bstats
.bs_capt
;
213 /* Set the dimension of the kernel-level capture buffer */
215 pcap_setbuff_win32(pcap_t
*p
, int dim
)
217 if(PacketSetBuff(p
->adapter
,dim
)==FALSE
)
219 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "driver error: not enough memory to allocate the kernel buffer");
225 /* Set the driver working mode */
227 pcap_setmode_win32(pcap_t
*p
, int mode
)
229 if(PacketSetMode(p
->adapter
,mode
)==FALSE
)
231 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "driver error: working mode not recognized");
238 /*set the minimum amount of data that will release a read call*/
240 pcap_setmintocopy_win32(pcap_t
*p
, int size
)
242 if(PacketSetMinToCopy(p
->adapter
, size
)==FALSE
)
244 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "driver error: unable to set the requested mintocopy size");
251 pcap_getevent_win32(pcap_t
*p
)
253 return (PacketGetReadEvent(p
->adapter
));
257 pcap_oid_get_request_win32(pcap_t
*p
, bpf_u_int32 oid
, void *data
, size_t len
)
259 PACKET_OID_DATA
*oid_data_arg
;
260 char errbuf
[PCAP_ERRBUF_SIZE
+1];
263 * Allocate a PACKET_OID_DATA structure to hand to PacketRequest().
264 * It should be big enough to hold "len" bytes of data; it
265 * will actually be slightly larger, as PACKET_OID_DATA has a
266 * 1-byte data array at the end, standing in for the variable-length
267 * data that's actually there.
269 oid_data_arg
= malloc(sizeof (PACKET_OID_DATA
) + len
);
270 if (oid_data_arg
== NULL
) {
271 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
272 "Couldn't allocate argument buffer for PacketRequest");
277 * No need to copy the data - we're doing a fetch.
279 oid_data_arg
->Oid
= oid
;
280 oid_data_arg
->Length
= (ULONG
)len
; /* XXX - check for ridiculously large value? */
281 if (!PacketRequest(p
->adapter
, FALSE
, oid_data_arg
)) {
282 pcap_win32_err_to_str(GetLastError(), errbuf
);
283 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
284 "Error calling PacketRequest: %s", errbuf
);
290 * Copy back the data we fetched.
292 memcpy(data
, oid_data_arg
->Data
, len
);
298 pcap_oid_set_request_win32(pcap_t
*p
, bpf_u_int32 oid
, const void *data
,
301 PACKET_OID_DATA
*oid_data_arg
;
302 char errbuf
[PCAP_ERRBUF_SIZE
+1];
305 * Allocate a PACKET_OID_DATA structure to hand to PacketRequest().
306 * It should be big enough to hold "len" bytes of data; it
307 * will actually be slightly larger, as PACKET_OID_DATA has a
308 * 1-byte data array at the end, standing in for the variable-length
309 * data that's actually there.
311 oid_data_arg
= malloc(sizeof (PACKET_OID_DATA
) + len
);
312 if (oid_data_arg
== NULL
) {
313 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
314 "Couldn't allocate argument buffer for PacketRequest");
318 oid_data_arg
->Oid
= oid
;
319 oid_data_arg
->Length
= (ULONG
)len
; /* XXX - check for ridiculously large value? */
320 memcpy(oid_data_arg
->Data
, data
, len
);
321 if (!PacketRequest(p
->adapter
, TRUE
, oid_data_arg
)) {
322 pcap_win32_err_to_str(GetLastError(), errbuf
);
323 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
324 "Error calling PacketRequest: %s", errbuf
);
330 * No need to copy the data - we're doing a set.
337 pcap_sendqueue_transmit_win32(pcap_t
*p
, pcap_send_queue
*queue
, int sync
)
340 char errbuf
[PCAP_ERRBUF_SIZE
+1];
342 if (p
->adapter
==NULL
) {
343 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
344 "Cannot transmit a queue to an offline capture or to a TurboCap port");
348 res
= PacketSendPackets(p
->adapter
,
353 if(res
!= queue
->len
){
354 pcap_win32_err_to_str(GetLastError(), errbuf
);
355 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
356 "Error opening adapter: %s", errbuf
);
363 pcap_setuserbuffer_win32(pcap_t
*p
, int size
)
365 unsigned char *new_buff
;
368 /* Bogus parameter */
369 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
370 "Error: invalid size %d",size
);
374 /* Allocate the buffer */
375 new_buff
=(unsigned char*)malloc(sizeof(char)*size
);
378 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
379 "Error: not enough memory");
392 pcap_live_dump_win32(pcap_t
*p
, char *filename
, int maxsize
, int maxpacks
)
396 /* Set the packet driver in dump mode */
397 res
= PacketSetMode(p
->adapter
, PACKET_MODE_DUMP
);
399 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
400 "Error setting dump mode");
404 /* Set the name of the dump file */
405 res
= PacketSetDumpName(p
->adapter
, filename
, (int)strlen(filename
));
407 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
408 "Error setting kernel dump file name");
412 /* Set the limits of the dump file */
413 res
= PacketSetDumpLimits(p
->adapter
, maxsize
, maxpacks
);
419 pcap_live_dump_ended_win32(pcap_t
*p
, int sync
)
421 return (PacketIsDumpEnded(p
->adapter
, (BOOLEAN
)sync
));
424 static PAirpcapHandle
425 pcap_get_airpcap_handle_win32(pcap_t
*p
)
427 #ifdef HAVE_AIRPCAP_API
428 return (PacketGetAirPcapHandle(p
->adapter
));
431 #endif /* HAVE_AIRPCAP_API */
435 pcap_read_win32_npf(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
440 register u_char
*bp
, *ep
;
442 struct pcap_win
*pw
= p
->priv
;
447 * Has "pcap_breakloop()" been called?
451 * Yes - clear the flag that indicates that it
452 * has, and return PCAP_ERROR_BREAK to indicate
453 * that we were told to break out of the loop.
456 return (PCAP_ERROR_BREAK
);
460 * Capture the packets.
462 * The PACKET structure had a bunch of extra stuff for
463 * Windows 9x/Me, but the only interesting data in it
464 * in the versions of Windows that we support is just
465 * a copy of p->buffer, a copy of p->buflen, and the
466 * actual number of bytes read returned from
467 * PacketReceivePacket(), none of which has to be
468 * retained from call to call, so we just keep one on
471 PacketInitPacket(&Packet
, (BYTE
*)p
->buffer
, p
->bufsize
);
472 if (!PacketReceivePacket(p
->adapter
, &Packet
, TRUE
)) {
473 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "read error: PacketReceivePacket failed");
477 cc
= Packet
.ulBytesReceived
;
485 * Loop through each packet.
487 #define bhp ((struct bpf_hdr *)bp)
490 register int caplen
, hdrlen
;
493 * Has "pcap_breakloop()" been called?
494 * If so, return immediately - if we haven't read any
495 * packets, clear the flag and return PCAP_ERROR_BREAK
496 * to indicate that we were told to break out of the loop,
497 * otherwise leave the flag set, so that the *next* call
498 * will break out of the loop without having read any
499 * packets, and return the number of packets we've
505 return (PCAP_ERROR_BREAK
);
515 caplen
= bhp
->bh_caplen
;
516 hdrlen
= bhp
->bh_hdrlen
;
520 * Short-circuit evaluation: if using BPF filter
521 * in kernel, no need to do it now - we already know
522 * the packet passed the filter.
524 * XXX - bpf_filter() should always return TRUE if
525 * handed a null pointer for the program, but it might
526 * just try to "run" the filter, so we check here.
528 if (pw
->filtering_in_kernel
||
529 p
->fcode
.bf_insns
== NULL
||
530 bpf_filter(p
->fcode
.bf_insns
, datap
, bhp
->bh_datalen
, caplen
)) {
532 * XXX A bpf_hdr matches a pcap_pkthdr.
534 (*callback
)(user
, (struct pcap_pkthdr
*)bp
, datap
);
535 bp
+= Packet_WORDALIGN(caplen
+ hdrlen
);
536 if (++n
>= cnt
&& !PACKET_COUNT_IS_UNLIMITED(cnt
)) {
545 bp
+= Packet_WORDALIGN(caplen
+ hdrlen
);
555 pcap_read_win32_dag(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
557 struct pcap_win
*pw
= p
->priv
;
560 int packet_len
= 0, caplen
= 0;
561 struct pcap_pkthdr pcap_header
;
564 dag_record_t
*header
;
565 unsigned erf_record_len
;
569 unsigned dfp
= p
->adapter
->DagFastProcess
;
572 if (cc
== 0) /* Get new packets only if we have processed all the ones of the previous read */
575 * Get new packets from the network.
577 * The PACKET structure had a bunch of extra stuff for
578 * Windows 9x/Me, but the only interesting data in it
579 * in the versions of Windows that we support is just
580 * a copy of p->buffer, a copy of p->buflen, and the
581 * actual number of bytes read returned from
582 * PacketReceivePacket(), none of which has to be
583 * retained from call to call, so we just keep one on
586 PacketInitPacket(&Packet
, (BYTE
*)p
->buffer
, p
->bufsize
);
587 if (!PacketReceivePacket(p
->adapter
, &Packet
, TRUE
)) {
588 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "read error: PacketReceivePacket failed");
592 cc
= Packet
.ulBytesReceived
;
594 /* The timeout has expired but we no packets arrived */
596 header
= (dag_record_t
*)p
->adapter
->DagBuffer
;
599 header
= (dag_record_t
*)p
->bp
;
601 endofbuf
= (char*)header
+ cc
;
604 * Cycle through the packets
608 erf_record_len
= SWAPS(header
->rlen
);
609 if((char*)header
+ erf_record_len
> endofbuf
)
612 /* Increase the number of captured packets */
615 /* Find the beginning of the packet */
616 dp
= ((u_char
*)header
) + dag_record_size
;
618 /* Determine actual packet len */
622 packet_len
= ATM_SNAPLEN
;
623 caplen
= ATM_SNAPLEN
;
629 swt
= SWAPS(header
->wlen
);
630 packet_len
= swt
- (pw
->dag_fcs_bits
);
631 caplen
= erf_record_len
- dag_record_size
- 2;
632 if (caplen
> packet_len
)
641 swt
= SWAPS(header
->wlen
);
642 packet_len
= swt
- (pw
->dag_fcs_bits
);
643 caplen
= erf_record_len
- dag_record_size
;
644 if (caplen
> packet_len
)
652 if(caplen
> p
->snapshot
)
653 caplen
= p
->snapshot
;
656 * Has "pcap_breakloop()" been called?
657 * If so, return immediately - if we haven't read any
658 * packets, clear the flag and return -2 to indicate
659 * that we were told to break out of the loop, otherwise
660 * leave the flag set, so that the *next* call will break
661 * out of the loop without having read any packets, and
662 * return the number of packets we've processed so far.
673 p
->bp
= (char*)header
;
674 p
->cc
= endofbuf
- (char*)header
;
681 /* convert between timestamp formats */
683 pcap_header
.ts
.tv_sec
= (int)(ts
>> 32);
684 ts
= (ts
& 0xffffffffi
64) * 1000000;
685 ts
+= 0x80000000; /* rounding */
686 pcap_header
.ts
.tv_usec
= (int)(ts
>> 32);
687 if (pcap_header
.ts
.tv_usec
>= 1000000) {
688 pcap_header
.ts
.tv_usec
-= 1000000;
689 pcap_header
.ts
.tv_sec
++;
693 /* No underlaying filtering system. We need to filter on our own */
694 if (p
->fcode
.bf_insns
)
696 if (bpf_filter(p
->fcode
.bf_insns
, dp
, packet_len
, caplen
) == 0)
698 /* Move to next packet */
699 header
= (dag_record_t
*)((char*)header
+ erf_record_len
);
704 /* Fill the header for the user suppplied callback function */
705 pcap_header
.caplen
= caplen
;
706 pcap_header
.len
= packet_len
;
708 /* Call the callback function */
709 (*callback
)(user
, &pcap_header
, dp
);
711 /* Move to next packet */
712 header
= (dag_record_t
*)((char*)header
+ erf_record_len
);
714 /* Stop if the number of packets requested by user has been reached*/
715 if (++n
>= cnt
&& !PACKET_COUNT_IS_UNLIMITED(cnt
))
717 p
->bp
= (char*)header
;
718 p
->cc
= endofbuf
- (char*)header
;
722 while((u_char
*)header
< endofbuf
);
726 #endif /* HAVE_DAG_API */
728 /* Send a packet to the network */
730 pcap_inject_win32(pcap_t
*p
, const void *buf
, size_t size
){
731 LPPACKET PacketToSend
;
733 PacketToSend
=PacketAllocatePacket();
735 if (PacketToSend
== NULL
)
737 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "send error: PacketAllocatePacket failed");
741 PacketInitPacket(PacketToSend
, (PVOID
)buf
, (UINT
)size
);
742 if(PacketSendPacket(p
->adapter
,PacketToSend
,TRUE
) == FALSE
){
743 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "send error: PacketSendPacket failed");
744 PacketFreePacket(PacketToSend
);
748 PacketFreePacket(PacketToSend
);
751 * We assume it all got sent if "PacketSendPacket()" succeeded.
752 * "pcap_inject()" is expected to return the number of bytes
759 pcap_cleanup_win32(pcap_t
*p
)
761 if (p
->adapter
!= NULL
) {
762 PacketCloseAdapter(p
->adapter
);
765 pcap_cleanup_live_common(p
);
769 pcap_activate_win32(pcap_t
*p
)
772 struct pcap_win
*pw
= p
->priv
;
775 char errbuf
[PCAP_ERRBUF_SIZE
+1];
779 * No monitor mode on Windows. It could be done on
780 * Vista with drivers that support the native 802.11
781 * mechanism and monitor mode.
783 return (PCAP_ERROR_RFMON_NOTSUP
);
789 p
->adapter
= PacketOpenAdapter(p
->opt
.source
);
791 if (p
->adapter
== NULL
)
793 /* Adapter detected but we are not able to open it. Return failure. */
794 pcap_win32_err_to_str(GetLastError(), errbuf
);
795 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
796 "Error opening adapter: %s", errbuf
);
801 if(PacketGetNetType (p
->adapter
,&type
) == FALSE
)
803 pcap_win32_err_to_str(GetLastError(), errbuf
);
804 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
805 "Cannot determine the network type: %s", errbuf
);
810 switch (type
.LinkType
)
813 p
->linktype
= DLT_EN10MB
;
816 case NdisMedium802_3
:
817 p
->linktype
= DLT_EN10MB
;
819 * This is (presumably) a real Ethernet capture; give it a
820 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
821 * that an application can let you choose it, in case you're
822 * capturing DOCSIS traffic that a Cisco Cable Modem
823 * Termination System is putting out onto an Ethernet (it
824 * doesn't put an Ethernet header onto the wire, it puts raw
825 * DOCSIS frames out on the wire inside the low-level
828 p
->dlt_list
= (u_int
*) malloc(sizeof(u_int
) * 2);
830 * If that fails, just leave the list empty.
832 if (p
->dlt_list
!= NULL
) {
833 p
->dlt_list
[0] = DLT_EN10MB
;
834 p
->dlt_list
[1] = DLT_DOCSIS
;
840 p
->linktype
= DLT_FDDI
;
843 case NdisMedium802_5
:
844 p
->linktype
= DLT_IEEE802
;
847 case NdisMediumArcnetRaw
:
848 p
->linktype
= DLT_ARCNET
;
851 case NdisMediumArcnet878_2
:
852 p
->linktype
= DLT_ARCNET
;
856 p
->linktype
= DLT_ATM_RFC1483
;
859 case NdisMediumCHDLC
:
860 p
->linktype
= DLT_CHDLC
;
863 case NdisMediumPPPSerial
:
864 p
->linktype
= DLT_PPP_SERIAL
;
868 p
->linktype
= DLT_NULL
;
871 case NdisMediumBare80211
:
872 p
->linktype
= DLT_IEEE802_11
;
875 case NdisMediumRadio80211
:
876 p
->linktype
= DLT_IEEE802_11_RADIO
;
880 p
->linktype
= DLT_PPI
;
884 p
->linktype
= DLT_EN10MB
; /*an unknown adapter is assumed to be ethernet*/
888 /* Set promiscuous mode */
892 if (PacketSetHwFilter(p
->adapter
,NDIS_PACKET_TYPE_PROMISCUOUS
) == FALSE
)
894 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "failed to set hardware filter to promiscuous mode");
900 if (PacketSetHwFilter(p
->adapter
,NDIS_PACKET_TYPE_ALL_LOCAL
) == FALSE
)
902 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "failed to set hardware filter to non-promiscuous mode");
907 /* Set the buffer size */
908 p
->bufsize
= WIN32_DEFAULT_USER_BUFFER_SIZE
;
910 if(!(p
->adapter
->Flags
& INFO_FLAG_DAG_CARD
))
913 * Traditional Adapter
916 * If the buffer size wasn't explicitly set, default to
917 * WIN32_DEFAULT_KERNEL_BUFFER_SIZE.
919 if (p
->opt
.buffer_size
== 0)
920 p
->opt
.buffer_size
= WIN32_DEFAULT_KERNEL_BUFFER_SIZE
;
922 if(PacketSetBuff(p
->adapter
,p
->opt
.buffer_size
)==FALSE
)
924 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "driver error: not enough memory to allocate the kernel buffer");
928 p
->buffer
= malloc(p
->bufsize
);
929 if (p
->buffer
== NULL
)
931 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "malloc: %s", pcap_strerror(errno
));
935 if (p
->opt
.immediate
)
937 /* tell the driver to copy the buffer as soon as data arrives */
938 if(PacketSetMinToCopy(p
->adapter
,0)==FALSE
)
940 pcap_win32_err_to_str(GetLastError(), errbuf
);
941 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
942 "Error calling PacketSetMinToCopy: %s",
949 /* tell the driver to copy the buffer only if it contains at least 16K */
950 if(PacketSetMinToCopy(p
->adapter
,16000)==FALSE
)
952 pcap_win32_err_to_str(GetLastError(), errbuf
);
953 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
954 "Error calling PacketSetMinToCopy: %s",
973 pcap_snprintf(keyname
, sizeof(keyname
), "%s\\CardParams\\%s",
974 "SYSTEM\\CurrentControlSet\\Services\\DAG",
975 strstr(_strlwr(p
->opt
.source
), "dag"));
978 status
= RegOpenKeyEx(HKEY_LOCAL_MACHINE
, keyname
, 0, KEY_READ
, &dagkey
);
979 if(status
!= ERROR_SUCCESS
)
982 status
= RegQueryValueEx(dagkey
,
989 if(status
!= ERROR_SUCCESS
)
999 p
->snapshot
= PacketSetSnapLen(p
->adapter
, snaplen
);
1001 /* Set the length of the FCS associated to any packet. This value
1002 * will be subtracted to the packet length */
1003 pw
->dag_fcs_bits
= p
->adapter
->DagFcsLen
;
1007 #endif /* HAVE_DAG_API */
1009 PacketSetReadTimeout(p
->adapter
, p
->opt
.timeout
);
1012 if(p
->adapter
->Flags
& INFO_FLAG_DAG_CARD
)
1014 /* install dag specific handlers for read and setfilter */
1015 p
->read_op
= pcap_read_win32_dag
;
1016 p
->setfilter_op
= pcap_setfilter_win32_dag
;
1020 #endif /* HAVE_DAG_API */
1021 /* install traditional npf handlers for read and setfilter */
1022 p
->read_op
= pcap_read_win32_npf
;
1023 p
->setfilter_op
= pcap_setfilter_win32_npf
;
1026 #endif /* HAVE_DAG_API */
1027 p
->setdirection_op
= NULL
; /* Not implemented. */
1028 /* XXX - can this be implemented on some versions of Windows? */
1029 p
->inject_op
= pcap_inject_win32
;
1030 p
->set_datalink_op
= NULL
; /* can't change data link type */
1031 p
->getnonblock_op
= pcap_getnonblock_win32
;
1032 p
->setnonblock_op
= pcap_setnonblock_win32
;
1033 p
->stats_op
= pcap_stats_win32
;
1034 p
->stats_ex_op
= pcap_stats_ex_win32
;
1035 p
->setbuff_op
= pcap_setbuff_win32
;
1036 p
->setmode_op
= pcap_setmode_win32
;
1037 p
->setmintocopy_op
= pcap_setmintocopy_win32
;
1038 p
->getevent_op
= pcap_getevent_win32
;
1039 p
->oid_get_request_op
= pcap_oid_get_request_win32
;
1040 p
->oid_set_request_op
= pcap_oid_set_request_win32
;
1041 p
->sendqueue_transmit_op
= pcap_sendqueue_transmit_win32
;
1042 p
->setuserbuffer_op
= pcap_setuserbuffer_win32
;
1043 p
->live_dump_op
= pcap_live_dump_win32
;
1044 p
->live_dump_ended_op
= pcap_live_dump_ended_win32
;
1045 p
->get_airpcap_handle_op
= pcap_get_airpcap_handle_win32
;
1046 p
->cleanup_op
= pcap_cleanup_win32
;
1050 pcap_cleanup_win32(p
);
1051 return (PCAP_ERROR
);
1055 pcap_create_interface(const char *device
, char *ebuf
)
1059 if (strlen(device
) == 1)
1062 * It's probably a unicode string
1063 * Convert to ascii and pass it to pcap_create_common
1065 * This wonderful hack is needed because pcap_lookupdev still returns
1066 * unicode strings, and it's used by windump when no device is specified
1067 * in the command line
1072 length
= wcslen((wchar_t*)device
);
1074 deviceAscii
= (char*)malloc(length
+ 1);
1076 if (deviceAscii
== NULL
)
1078 pcap_snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "Malloc failed");
1082 pcap_snprintf(deviceAscii
, length
+ 1, "%ws", (wchar_t*)device
);
1083 p
= pcap_create_common(deviceAscii
, ebuf
, sizeof (struct pcap_win
));
1088 p
= pcap_create_common(device
, ebuf
, sizeof (struct pcap_win
));
1094 p
->activate_op
= pcap_activate_win32
;
1099 pcap_setfilter_win32_npf(pcap_t
*p
, struct bpf_program
*fp
)
1101 struct pcap_win
*pw
= p
->priv
;
1103 if(PacketSetBpf(p
->adapter
,fp
)==FALSE
){
1105 * Kernel filter not installed.
1107 * XXX - we don't know whether this failed because:
1109 * the kernel rejected the filter program as invalid,
1110 * in which case we should fall back on userland
1113 * the kernel rejected the filter program as too big,
1114 * in which case we should again fall back on
1115 * userland filtering;
1117 * there was some other problem, in which case we
1118 * should probably report an error.
1120 * For NPF devices, the Win32 status will be
1121 * STATUS_INVALID_DEVICE_REQUEST for invalid
1122 * filters, but I don't know what it'd be for
1123 * other problems, and for some other devices
1124 * it might not be set at all.
1126 * So we just fall back on userland filtering in
1131 * install_bpf_program() validates the program.
1133 * XXX - what if we already have a filter in the kernel?
1135 if (install_bpf_program(p
, fp
) < 0)
1137 pw
->filtering_in_kernel
= 0; /* filtering in userland */
1144 pw
->filtering_in_kernel
= 1; /* filtering in the kernel */
1147 * Discard any previously-received packets, as they might have
1148 * passed whatever filter was formerly in effect, but might
1149 * not pass this filter (BIOCSETF discards packets buffered
1150 * in the kernel, so you can lose packets in any case).
1157 * We filter at user level, since the kernel driver does't process the packets
1160 pcap_setfilter_win32_dag(pcap_t
*p
, struct bpf_program
*fp
) {
1164 strncpy(p
->errbuf
, "setfilter: No filter specified", sizeof(p
->errbuf
));
1168 /* Install a user level filter */
1169 if (install_bpf_program(p
, fp
) < 0)
1171 pcap_snprintf(p
->errbuf
, sizeof(p
->errbuf
),
1172 "setfilter, unable to install the filter: %s", pcap_strerror(errno
));
1180 pcap_getnonblock_win32(pcap_t
*p
, char *errbuf
)
1182 struct pcap_win
*pw
= p
->priv
;
1185 * XXX - if there were a PacketGetReadTimeout() call, we
1186 * would use it, and return 1 if the timeout is -1
1189 return (pw
->nonblock
);
1193 pcap_setnonblock_win32(pcap_t
*p
, int nonblock
, char *errbuf
)
1195 struct pcap_win
*pw
= p
->priv
;
1197 char win_errbuf
[PCAP_ERRBUF_SIZE
+1];
1201 * Set the read timeout to -1 for non-blocking mode.
1206 * Restore the timeout set when the device was opened.
1207 * (Note that this may be -1, in which case we're not
1208 * really leaving non-blocking mode. However, although
1209 * the timeout argument to pcap_set_timeout() and
1210 * pcap_open_live() is an int, you're not supposed to
1211 * supply a negative value, so that "shouldn't happen".)
1213 newtimeout
= p
->opt
.timeout
;
1215 if (!PacketSetReadTimeout(p
->adapter
, newtimeout
)) {
1216 pcap_win32_err_to_str(GetLastError(), win_errbuf
);
1217 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1218 "PacketSetReadTimeout: %s", win_errbuf
);
1221 pw
->nonblock
= (newtimeout
== -1);
1226 pcap_platform_finddevs(pcap_if_t
**alldevsp
, char *errbuf
)