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 *lenp
)
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 "*lenp" 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
) + *lenp
);
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
)(*lenp
); /* 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 * Get the length actually supplied.
292 *lenp
= oid_data_arg
->Length
;
295 * Copy back the data we fetched.
297 memcpy(data
, oid_data_arg
->Data
, *lenp
);
303 pcap_oid_set_request_win32(pcap_t
*p
, bpf_u_int32 oid
, const void *data
,
306 PACKET_OID_DATA
*oid_data_arg
;
307 char errbuf
[PCAP_ERRBUF_SIZE
+1];
310 * Allocate a PACKET_OID_DATA structure to hand to PacketRequest().
311 * It should be big enough to hold "*lenp" bytes of data; it
312 * will actually be slightly larger, as PACKET_OID_DATA has a
313 * 1-byte data array at the end, standing in for the variable-length
314 * data that's actually there.
316 oid_data_arg
= malloc(sizeof (PACKET_OID_DATA
) + *lenp
);
317 if (oid_data_arg
== NULL
) {
318 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
319 "Couldn't allocate argument buffer for PacketRequest");
323 oid_data_arg
->Oid
= oid
;
324 oid_data_arg
->Length
= (ULONG
)(*lenp
); /* XXX - check for ridiculously large value? */
325 memcpy(oid_data_arg
->Data
, data
, *lenp
);
326 if (!PacketRequest(p
->adapter
, TRUE
, oid_data_arg
)) {
327 pcap_win32_err_to_str(GetLastError(), errbuf
);
328 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
329 "Error calling PacketRequest: %s", errbuf
);
335 * Get the length actually copied.
337 *lenp
= oid_data_arg
->Length
;
340 * No need to copy the data - we're doing a set.
347 pcap_sendqueue_transmit_win32(pcap_t
*p
, pcap_send_queue
*queue
, int sync
)
350 char errbuf
[PCAP_ERRBUF_SIZE
+1];
352 if (p
->adapter
==NULL
) {
353 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
354 "Cannot transmit a queue to an offline capture or to a TurboCap port");
358 res
= PacketSendPackets(p
->adapter
,
363 if(res
!= queue
->len
){
364 pcap_win32_err_to_str(GetLastError(), errbuf
);
365 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
366 "Error opening adapter: %s", errbuf
);
373 pcap_setuserbuffer_win32(pcap_t
*p
, int size
)
375 unsigned char *new_buff
;
378 /* Bogus parameter */
379 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
380 "Error: invalid size %d",size
);
384 /* Allocate the buffer */
385 new_buff
=(unsigned char*)malloc(sizeof(char)*size
);
388 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
389 "Error: not enough memory");
402 pcap_live_dump_win32(pcap_t
*p
, char *filename
, int maxsize
, int maxpacks
)
406 /* Set the packet driver in dump mode */
407 res
= PacketSetMode(p
->adapter
, PACKET_MODE_DUMP
);
409 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
410 "Error setting dump mode");
414 /* Set the name of the dump file */
415 res
= PacketSetDumpName(p
->adapter
, filename
, (int)strlen(filename
));
417 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
418 "Error setting kernel dump file name");
422 /* Set the limits of the dump file */
423 res
= PacketSetDumpLimits(p
->adapter
, maxsize
, maxpacks
);
429 pcap_live_dump_ended_win32(pcap_t
*p
, int sync
)
431 return (PacketIsDumpEnded(p
->adapter
, (BOOLEAN
)sync
));
434 static PAirpcapHandle
435 pcap_get_airpcap_handle_win32(pcap_t
*p
)
437 #ifdef HAVE_AIRPCAP_API
438 return (PacketGetAirPcapHandle(p
->adapter
));
441 #endif /* HAVE_AIRPCAP_API */
445 pcap_read_win32_npf(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
450 register u_char
*bp
, *ep
;
452 struct pcap_win
*pw
= p
->priv
;
457 * Has "pcap_breakloop()" been called?
461 * Yes - clear the flag that indicates that it
462 * has, and return PCAP_ERROR_BREAK to indicate
463 * that we were told to break out of the loop.
466 return (PCAP_ERROR_BREAK
);
470 * Capture the packets.
472 * The PACKET structure had a bunch of extra stuff for
473 * Windows 9x/Me, but the only interesting data in it
474 * in the versions of Windows that we support is just
475 * a copy of p->buffer, a copy of p->buflen, and the
476 * actual number of bytes read returned from
477 * PacketReceivePacket(), none of which has to be
478 * retained from call to call, so we just keep one on
481 PacketInitPacket(&Packet
, (BYTE
*)p
->buffer
, p
->bufsize
);
482 if (!PacketReceivePacket(p
->adapter
, &Packet
, TRUE
)) {
483 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "read error: PacketReceivePacket failed");
487 cc
= Packet
.ulBytesReceived
;
495 * Loop through each packet.
497 #define bhp ((struct bpf_hdr *)bp)
500 register int caplen
, hdrlen
;
503 * Has "pcap_breakloop()" been called?
504 * If so, return immediately - if we haven't read any
505 * packets, clear the flag and return PCAP_ERROR_BREAK
506 * to indicate that we were told to break out of the loop,
507 * otherwise leave the flag set, so that the *next* call
508 * will break out of the loop without having read any
509 * packets, and return the number of packets we've
515 return (PCAP_ERROR_BREAK
);
525 caplen
= bhp
->bh_caplen
;
526 hdrlen
= bhp
->bh_hdrlen
;
530 * Short-circuit evaluation: if using BPF filter
531 * in kernel, no need to do it now - we already know
532 * the packet passed the filter.
534 * XXX - bpf_filter() should always return TRUE if
535 * handed a null pointer for the program, but it might
536 * just try to "run" the filter, so we check here.
538 if (pw
->filtering_in_kernel
||
539 p
->fcode
.bf_insns
== NULL
||
540 bpf_filter(p
->fcode
.bf_insns
, datap
, bhp
->bh_datalen
, caplen
)) {
542 * XXX A bpf_hdr matches a pcap_pkthdr.
544 (*callback
)(user
, (struct pcap_pkthdr
*)bp
, datap
);
545 bp
+= Packet_WORDALIGN(caplen
+ hdrlen
);
546 if (++n
>= cnt
&& !PACKET_COUNT_IS_UNLIMITED(cnt
)) {
555 bp
+= Packet_WORDALIGN(caplen
+ hdrlen
);
565 pcap_read_win32_dag(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
567 struct pcap_win
*pw
= p
->priv
;
570 int packet_len
= 0, caplen
= 0;
571 struct pcap_pkthdr pcap_header
;
574 dag_record_t
*header
;
575 unsigned erf_record_len
;
579 unsigned dfp
= p
->adapter
->DagFastProcess
;
582 if (cc
== 0) /* Get new packets only if we have processed all the ones of the previous read */
585 * Get new packets from the network.
587 * The PACKET structure had a bunch of extra stuff for
588 * Windows 9x/Me, but the only interesting data in it
589 * in the versions of Windows that we support is just
590 * a copy of p->buffer, a copy of p->buflen, and the
591 * actual number of bytes read returned from
592 * PacketReceivePacket(), none of which has to be
593 * retained from call to call, so we just keep one on
596 PacketInitPacket(&Packet
, (BYTE
*)p
->buffer
, p
->bufsize
);
597 if (!PacketReceivePacket(p
->adapter
, &Packet
, TRUE
)) {
598 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "read error: PacketReceivePacket failed");
602 cc
= Packet
.ulBytesReceived
;
604 /* The timeout has expired but we no packets arrived */
606 header
= (dag_record_t
*)p
->adapter
->DagBuffer
;
609 header
= (dag_record_t
*)p
->bp
;
611 endofbuf
= (char*)header
+ cc
;
614 * Cycle through the packets
618 erf_record_len
= SWAPS(header
->rlen
);
619 if((char*)header
+ erf_record_len
> endofbuf
)
622 /* Increase the number of captured packets */
625 /* Find the beginning of the packet */
626 dp
= ((u_char
*)header
) + dag_record_size
;
628 /* Determine actual packet len */
632 packet_len
= ATM_SNAPLEN
;
633 caplen
= ATM_SNAPLEN
;
639 swt
= SWAPS(header
->wlen
);
640 packet_len
= swt
- (pw
->dag_fcs_bits
);
641 caplen
= erf_record_len
- dag_record_size
- 2;
642 if (caplen
> packet_len
)
651 swt
= SWAPS(header
->wlen
);
652 packet_len
= swt
- (pw
->dag_fcs_bits
);
653 caplen
= erf_record_len
- dag_record_size
;
654 if (caplen
> packet_len
)
662 if(caplen
> p
->snapshot
)
663 caplen
= p
->snapshot
;
666 * Has "pcap_breakloop()" been called?
667 * If so, return immediately - if we haven't read any
668 * packets, clear the flag and return -2 to indicate
669 * that we were told to break out of the loop, otherwise
670 * leave the flag set, so that the *next* call will break
671 * out of the loop without having read any packets, and
672 * return the number of packets we've processed so far.
683 p
->bp
= (char*)header
;
684 p
->cc
= endofbuf
- (char*)header
;
691 /* convert between timestamp formats */
693 pcap_header
.ts
.tv_sec
= (int)(ts
>> 32);
694 ts
= (ts
& 0xffffffffi
64) * 1000000;
695 ts
+= 0x80000000; /* rounding */
696 pcap_header
.ts
.tv_usec
= (int)(ts
>> 32);
697 if (pcap_header
.ts
.tv_usec
>= 1000000) {
698 pcap_header
.ts
.tv_usec
-= 1000000;
699 pcap_header
.ts
.tv_sec
++;
703 /* No underlaying filtering system. We need to filter on our own */
704 if (p
->fcode
.bf_insns
)
706 if (bpf_filter(p
->fcode
.bf_insns
, dp
, packet_len
, caplen
) == 0)
708 /* Move to next packet */
709 header
= (dag_record_t
*)((char*)header
+ erf_record_len
);
714 /* Fill the header for the user suppplied callback function */
715 pcap_header
.caplen
= caplen
;
716 pcap_header
.len
= packet_len
;
718 /* Call the callback function */
719 (*callback
)(user
, &pcap_header
, dp
);
721 /* Move to next packet */
722 header
= (dag_record_t
*)((char*)header
+ erf_record_len
);
724 /* Stop if the number of packets requested by user has been reached*/
725 if (++n
>= cnt
&& !PACKET_COUNT_IS_UNLIMITED(cnt
))
727 p
->bp
= (char*)header
;
728 p
->cc
= endofbuf
- (char*)header
;
732 while((u_char
*)header
< endofbuf
);
736 #endif /* HAVE_DAG_API */
738 /* Send a packet to the network */
740 pcap_inject_win32(pcap_t
*p
, const void *buf
, size_t size
){
741 LPPACKET PacketToSend
;
743 PacketToSend
=PacketAllocatePacket();
745 if (PacketToSend
== NULL
)
747 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "send error: PacketAllocatePacket failed");
751 PacketInitPacket(PacketToSend
, (PVOID
)buf
, (UINT
)size
);
752 if(PacketSendPacket(p
->adapter
,PacketToSend
,TRUE
) == FALSE
){
753 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "send error: PacketSendPacket failed");
754 PacketFreePacket(PacketToSend
);
758 PacketFreePacket(PacketToSend
);
761 * We assume it all got sent if "PacketSendPacket()" succeeded.
762 * "pcap_inject()" is expected to return the number of bytes
769 pcap_cleanup_win32(pcap_t
*p
)
771 if (p
->adapter
!= NULL
) {
772 PacketCloseAdapter(p
->adapter
);
775 pcap_cleanup_live_common(p
);
779 pcap_activate_win32(pcap_t
*p
)
782 struct pcap_win
*pw
= p
->priv
;
785 char errbuf
[PCAP_ERRBUF_SIZE
+1];
789 * No monitor mode on Windows. It could be done on
790 * Vista with drivers that support the native 802.11
791 * mechanism and monitor mode.
793 return (PCAP_ERROR_RFMON_NOTSUP
);
799 p
->adapter
= PacketOpenAdapter(p
->opt
.device
);
801 if (p
->adapter
== NULL
)
803 /* Adapter detected but we are not able to open it. Return failure. */
804 pcap_win32_err_to_str(GetLastError(), errbuf
);
805 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
806 "Error opening adapter: %s", errbuf
);
811 if(PacketGetNetType (p
->adapter
,&type
) == FALSE
)
813 pcap_win32_err_to_str(GetLastError(), errbuf
);
814 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
815 "Cannot determine the network type: %s", errbuf
);
820 switch (type
.LinkType
)
823 p
->linktype
= DLT_EN10MB
;
826 case NdisMedium802_3
:
827 p
->linktype
= DLT_EN10MB
;
829 * This is (presumably) a real Ethernet capture; give it a
830 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
831 * that an application can let you choose it, in case you're
832 * capturing DOCSIS traffic that a Cisco Cable Modem
833 * Termination System is putting out onto an Ethernet (it
834 * doesn't put an Ethernet header onto the wire, it puts raw
835 * DOCSIS frames out on the wire inside the low-level
838 p
->dlt_list
= (u_int
*) malloc(sizeof(u_int
) * 2);
840 * If that fails, just leave the list empty.
842 if (p
->dlt_list
!= NULL
) {
843 p
->dlt_list
[0] = DLT_EN10MB
;
844 p
->dlt_list
[1] = DLT_DOCSIS
;
850 p
->linktype
= DLT_FDDI
;
853 case NdisMedium802_5
:
854 p
->linktype
= DLT_IEEE802
;
857 case NdisMediumArcnetRaw
:
858 p
->linktype
= DLT_ARCNET
;
861 case NdisMediumArcnet878_2
:
862 p
->linktype
= DLT_ARCNET
;
866 p
->linktype
= DLT_ATM_RFC1483
;
869 case NdisMediumCHDLC
:
870 p
->linktype
= DLT_CHDLC
;
873 case NdisMediumPPPSerial
:
874 p
->linktype
= DLT_PPP_SERIAL
;
878 p
->linktype
= DLT_NULL
;
881 case NdisMediumBare80211
:
882 p
->linktype
= DLT_IEEE802_11
;
885 case NdisMediumRadio80211
:
886 p
->linktype
= DLT_IEEE802_11_RADIO
;
890 p
->linktype
= DLT_PPI
;
894 p
->linktype
= DLT_EN10MB
; /*an unknown adapter is assumed to be ethernet*/
898 /* Set promiscuous mode */
902 if (PacketSetHwFilter(p
->adapter
,NDIS_PACKET_TYPE_PROMISCUOUS
) == FALSE
)
904 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "failed to set hardware filter to promiscuous mode");
910 if (PacketSetHwFilter(p
->adapter
,NDIS_PACKET_TYPE_ALL_LOCAL
) == FALSE
)
912 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "failed to set hardware filter to non-promiscuous mode");
917 /* Set the buffer size */
918 p
->bufsize
= WIN32_DEFAULT_USER_BUFFER_SIZE
;
920 if(!(p
->adapter
->Flags
& INFO_FLAG_DAG_CARD
))
923 * Traditional Adapter
926 * If the buffer size wasn't explicitly set, default to
927 * WIN32_DEFAULT_KERNEL_BUFFER_SIZE.
929 if (p
->opt
.buffer_size
== 0)
930 p
->opt
.buffer_size
= WIN32_DEFAULT_KERNEL_BUFFER_SIZE
;
932 if(PacketSetBuff(p
->adapter
,p
->opt
.buffer_size
)==FALSE
)
934 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "driver error: not enough memory to allocate the kernel buffer");
938 p
->buffer
= malloc(p
->bufsize
);
939 if (p
->buffer
== NULL
)
941 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "malloc: %s", pcap_strerror(errno
));
945 if (p
->opt
.immediate
)
947 /* tell the driver to copy the buffer as soon as data arrives */
948 if(PacketSetMinToCopy(p
->adapter
,0)==FALSE
)
950 pcap_win32_err_to_str(GetLastError(), errbuf
);
951 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
952 "Error calling PacketSetMinToCopy: %s",
959 /* tell the driver to copy the buffer only if it contains at least 16K */
960 if(PacketSetMinToCopy(p
->adapter
,16000)==FALSE
)
962 pcap_win32_err_to_str(GetLastError(), errbuf
);
963 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
964 "Error calling PacketSetMinToCopy: %s",
983 pcap_snprintf(keyname
, sizeof(keyname
), "%s\\CardParams\\%s",
984 "SYSTEM\\CurrentControlSet\\Services\\DAG",
985 strstr(_strlwr(p
->opt
.device
), "dag"));
988 status
= RegOpenKeyEx(HKEY_LOCAL_MACHINE
, keyname
, 0, KEY_READ
, &dagkey
);
989 if(status
!= ERROR_SUCCESS
)
992 status
= RegQueryValueEx(dagkey
,
999 if(status
!= ERROR_SUCCESS
)
1004 RegCloseKey(dagkey
);
1009 p
->snapshot
= PacketSetSnapLen(p
->adapter
, snaplen
);
1011 /* Set the length of the FCS associated to any packet. This value
1012 * will be subtracted to the packet length */
1013 pw
->dag_fcs_bits
= p
->adapter
->DagFcsLen
;
1017 #endif /* HAVE_DAG_API */
1019 PacketSetReadTimeout(p
->adapter
, p
->opt
.timeout
);
1022 if(p
->adapter
->Flags
& INFO_FLAG_DAG_CARD
)
1024 /* install dag specific handlers for read and setfilter */
1025 p
->read_op
= pcap_read_win32_dag
;
1026 p
->setfilter_op
= pcap_setfilter_win32_dag
;
1030 #endif /* HAVE_DAG_API */
1031 /* install traditional npf handlers for read and setfilter */
1032 p
->read_op
= pcap_read_win32_npf
;
1033 p
->setfilter_op
= pcap_setfilter_win32_npf
;
1036 #endif /* HAVE_DAG_API */
1037 p
->setdirection_op
= NULL
; /* Not implemented. */
1038 /* XXX - can this be implemented on some versions of Windows? */
1039 p
->inject_op
= pcap_inject_win32
;
1040 p
->set_datalink_op
= NULL
; /* can't change data link type */
1041 p
->getnonblock_op
= pcap_getnonblock_win32
;
1042 p
->setnonblock_op
= pcap_setnonblock_win32
;
1043 p
->stats_op
= pcap_stats_win32
;
1044 p
->stats_ex_op
= pcap_stats_ex_win32
;
1045 p
->setbuff_op
= pcap_setbuff_win32
;
1046 p
->setmode_op
= pcap_setmode_win32
;
1047 p
->setmintocopy_op
= pcap_setmintocopy_win32
;
1048 p
->getevent_op
= pcap_getevent_win32
;
1049 p
->oid_get_request_op
= pcap_oid_get_request_win32
;
1050 p
->oid_set_request_op
= pcap_oid_set_request_win32
;
1051 p
->sendqueue_transmit_op
= pcap_sendqueue_transmit_win32
;
1052 p
->setuserbuffer_op
= pcap_setuserbuffer_win32
;
1053 p
->live_dump_op
= pcap_live_dump_win32
;
1054 p
->live_dump_ended_op
= pcap_live_dump_ended_win32
;
1055 p
->get_airpcap_handle_op
= pcap_get_airpcap_handle_win32
;
1056 p
->cleanup_op
= pcap_cleanup_win32
;
1060 pcap_cleanup_win32(p
);
1061 return (PCAP_ERROR
);
1065 pcap_create_interface(char *ebuf
)
1069 p
= pcap_create_common(ebuf
, sizeof (struct pcap_win
));
1073 p
->activate_op
= pcap_activate_win32
;
1078 pcap_setfilter_win32_npf(pcap_t
*p
, struct bpf_program
*fp
)
1080 struct pcap_win
*pw
= p
->priv
;
1082 if(PacketSetBpf(p
->adapter
,fp
)==FALSE
){
1084 * Kernel filter not installed.
1086 * XXX - we don't know whether this failed because:
1088 * the kernel rejected the filter program as invalid,
1089 * in which case we should fall back on userland
1092 * the kernel rejected the filter program as too big,
1093 * in which case we should again fall back on
1094 * userland filtering;
1096 * there was some other problem, in which case we
1097 * should probably report an error.
1099 * For NPF devices, the Win32 status will be
1100 * STATUS_INVALID_DEVICE_REQUEST for invalid
1101 * filters, but I don't know what it'd be for
1102 * other problems, and for some other devices
1103 * it might not be set at all.
1105 * So we just fall back on userland filtering in
1110 * install_bpf_program() validates the program.
1112 * XXX - what if we already have a filter in the kernel?
1114 if (install_bpf_program(p
, fp
) < 0)
1116 pw
->filtering_in_kernel
= 0; /* filtering in userland */
1123 pw
->filtering_in_kernel
= 1; /* filtering in the kernel */
1126 * Discard any previously-received packets, as they might have
1127 * passed whatever filter was formerly in effect, but might
1128 * not pass this filter (BIOCSETF discards packets buffered
1129 * in the kernel, so you can lose packets in any case).
1136 * We filter at user level, since the kernel driver does't process the packets
1139 pcap_setfilter_win32_dag(pcap_t
*p
, struct bpf_program
*fp
) {
1143 strncpy(p
->errbuf
, "setfilter: No filter specified", sizeof(p
->errbuf
));
1147 /* Install a user level filter */
1148 if (install_bpf_program(p
, fp
) < 0)
1150 pcap_snprintf(p
->errbuf
, sizeof(p
->errbuf
),
1151 "setfilter, unable to install the filter: %s", pcap_strerror(errno
));
1159 pcap_getnonblock_win32(pcap_t
*p
, char *errbuf
)
1161 struct pcap_win
*pw
= p
->priv
;
1164 * XXX - if there were a PacketGetReadTimeout() call, we
1165 * would use it, and return 1 if the timeout is -1
1168 return (pw
->nonblock
);
1172 pcap_setnonblock_win32(pcap_t
*p
, int nonblock
, char *errbuf
)
1174 struct pcap_win
*pw
= p
->priv
;
1176 char win_errbuf
[PCAP_ERRBUF_SIZE
+1];
1180 * Set the read timeout to -1 for non-blocking mode.
1185 * Restore the timeout set when the device was opened.
1186 * (Note that this may be -1, in which case we're not
1187 * really leaving non-blocking mode. However, although
1188 * the timeout argument to pcap_set_timeout() and
1189 * pcap_open_live() is an int, you're not supposed to
1190 * supply a negative value, so that "shouldn't happen".)
1192 newtimeout
= p
->opt
.timeout
;
1194 if (!PacketSetReadTimeout(p
->adapter
, newtimeout
)) {
1195 pcap_win32_err_to_str(GetLastError(), win_errbuf
);
1196 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1197 "PacketSetReadTimeout: %s", win_errbuf
);
1200 pw
->nonblock
= (newtimeout
== -1);
1205 pcap_add_if_win32(pcap_if_t
**devlist
, char *name
, bpf_u_int32 flags
,
1206 const char *description
, char *errbuf
)
1209 npf_if_addr if_addrs
[MAX_NETWORK_ADDRESSES
];
1213 if_addr_size
= MAX_NETWORK_ADDRESSES
;
1216 * Add an entry for this interface, with no addresses.
1218 if (add_or_find_if(&curdev
, devlist
, name
, flags
, description
,
1227 * Get the list of addresses for the interface.
1229 if (!PacketGetNetInfoEx((void *)name
, if_addrs
, &if_addr_size
)) {
1233 * We don't return an error, because this can happen with
1234 * NdisWan interfaces, and we want to supply them even
1235 * if we can't supply their addresses.
1237 * We return an entry with an empty address list.
1243 * Now add the addresses.
1245 while (if_addr_size
-- > 0) {
1247 * "curdev" is an entry for this interface; add an entry for
1248 * this address to its list of addresses.
1252 res
= add_addr_to_dev(curdev
,
1253 (struct sockaddr
*)&if_addrs
[if_addr_size
].IPAddress
,
1254 sizeof (struct sockaddr_storage
),
1255 (struct sockaddr
*)&if_addrs
[if_addr_size
].SubnetMask
,
1256 sizeof (struct sockaddr_storage
),
1257 (struct sockaddr
*)&if_addrs
[if_addr_size
].Broadcast
,
1258 sizeof (struct sockaddr_storage
),
1274 pcap_platform_finddevs(pcap_if_t
**alldevsp
, char *errbuf
)
1276 pcap_if_t
*devlist
= NULL
;
1282 char our_errbuf
[PCAP_ERRBUF_SIZE
+1];
1285 * Find out how big a buffer we need.
1287 * This call should always return FALSE; if the error is
1288 * ERROR_INSUFFICIENT_BUFFER, NameLength will be set to
1289 * the size of the buffer we need, otherwise there's a
1290 * problem, and NameLength should be set to 0.
1292 * It shouldn't require NameLength to be set, but,
1293 * at least as of WinPcap 4.1.3, it checks whether
1294 * NameLength is big enough before it checks for a
1295 * NULL buffer argument, so, while it'll still do
1296 * the right thing if NameLength is uninitialized and
1297 * whatever junk happens to be there is big enough
1298 * (because the pointer argument will be null), it's
1299 * still reading an uninitialized variable.
1302 if (!PacketGetAdapterNames(NULL
, &NameLength
))
1304 DWORD last_error
= GetLastError();
1306 if (last_error
!= ERROR_INSUFFICIENT_BUFFER
)
1308 pcap_win32_err_to_str(last_error
, our_errbuf
);
1309 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1310 "PacketGetAdapterNames: %s", our_errbuf
);
1316 AdaptersName
= (char*) malloc(NameLength
);
1322 if (AdaptersName
== NULL
)
1324 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "Cannot allocate enough memory to list the adapters.");
1328 if (!PacketGetAdapterNames(AdaptersName
, &NameLength
)) {
1329 pcap_win32_err_to_str(GetLastError(), our_errbuf
);
1330 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "PacketGetAdapterNames: %s",
1337 * "PacketGetAdapterNames()" returned a list of
1338 * null-terminated ASCII interface name strings,
1339 * terminated by a null string, followed by a list
1340 * of null-terminated ASCII interface description
1341 * strings, terminated by a null string.
1342 * This means there are two ASCII nulls at the end
1343 * of the first list.
1345 * Find the end of the first list; that's the
1346 * beginning of the second list.
1348 desc
= &AdaptersName
[0];
1349 while (*desc
!= '\0' || *(desc
+ 1) != '\0')
1353 * Found it - "desc" points to the first of the two
1354 * nulls at the end of the list of names, so the
1355 * first byte of the list of descriptions is two bytes
1361 * Loop over the elements in the first list.
1363 name
= &AdaptersName
[0];
1364 while (*name
!= '\0') {
1365 #ifdef HAVE_PACKET_IS_LOOPBACK_ADAPTER
1366 bpf_u_int32 flags
= 0;
1370 * Is this a loopback interface?
1371 * XXX - it'd be best if there were a way to get a list
1372 * of interfaces *and* flags, including "Up" and "Running"
1373 * as well as "loopback", without having to open the
1376 p
= pcap_open_live(name
, 68, 0, 0, errbuf
);
1378 if (PacketIsLoopbackAdapter(p
->adapter
)) {
1380 flags
|= PCAP_IF_LOOPBACK
;
1387 * Add an entry for this interface.
1389 if (pcap_add_if_win32(&devlist
, name
, flags
, desc
,
1397 name
+= strlen(name
) + 1;
1398 desc
+= strlen(desc
) + 1;
1403 * We had an error; free the list we've been constructing.
1405 if (devlist
!= NULL
) {
1406 pcap_freealldevs(devlist
);
1411 *alldevsp
= devlist
;