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
*);
60 static int pcap_setnonblock_win32(pcap_t
*, int);
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.
76 int rfmon_selfstart
; /* a flag tells whether the monitor mode is set by itself */
77 int filtering_in_kernel
; /* using kernel filter */
80 int dag_fcs_bits
; /* Number of checksum bits from link layer */
84 int samp_npkt
; /* parameter needed for sampling, with '1 out of N' method has been requested */
85 struct timeval samp_time
; /* parameter needed for sampling, with '1 every N ms' method has been requested */
99 * Define stub versions of the monitor-mode support routines if this
100 * isn't Npcap. HAVE_NPCAP_PACKET_API is defined by Npcap but not
103 #ifndef HAVE_NPCAP_PACKET_API
105 PacketIsMonitorModeSupported(PCHAR AdapterName _U_
)
108 * We don't support monitor mode.
114 PacketSetMonitorMode(PCHAR AdapterName _U_
, int mode _U_
)
117 * This should never be called, as PacketIsMonitorModeSupported()
118 * will return 0, meaning "we don't support monitor mode, so
119 * don't try to turn it on or off".
125 PacketGetMonitorMode(PCHAR AdapterName _U_
)
128 * This should fail, so that pcap_activate_win32() returns
129 * PCAP_ERROR_RFMON_NOTSUP if our caller requested monitor
140 WORD wVersionRequested
;
148 wVersionRequested
= MAKEWORD( 1, 1);
149 err
= WSAStartup( wVersionRequested
, &wsaData
);
150 atexit ((void(*)(void))WSACleanup
);
161 return (wsockinit());
165 pcap_stats_win32(pcap_t
*p
, struct pcap_stat
*ps
)
167 struct bpf_stat bstats
;
168 char errbuf
[PCAP_ERRBUF_SIZE
+1];
171 * Try to get statistics.
173 * (Please note - "struct pcap_stat" is *not* the same as
174 * WinPcap's "struct bpf_stat". It might currently have the
175 * same layout, but let's not cheat.
177 * Note also that we don't fill in ps_capt, as we might have
178 * been called by code compiled against an earlier version of
179 * WinPcap that didn't have ps_capt, in which case filling it
180 * in would stomp on whatever comes after the structure passed
183 if (!PacketGetStats(p
->adapter
, &bstats
)) {
184 pcap_win32_err_to_str(GetLastError(), errbuf
);
185 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
186 "PacketGetStats error: %s", errbuf
);
189 ps
->ps_recv
= bstats
.bs_recv
;
190 ps
->ps_drop
= bstats
.bs_drop
;
193 * XXX - PacketGetStats() doesn't fill this in, so we just
197 ps
->ps_ifdrop
= bstats
.ps_ifdrop
;
206 * Win32-only routine for getting statistics.
208 * This way is definitely safer than passing the pcap_stat * from the userland.
209 * In fact, there could happen than the user allocates a variable which is not
210 * big enough for the new structure, and the library will write in a zone
211 * which is not allocated to this variable.
213 * In this way, we're pretty sure we are writing on memory allocated to this
216 * XXX - but this is the wrong way to handle statistics. Instead, we should
217 * have an API that returns data in a form like the Options section of a
218 * pcapng Interface Statistics Block:
220 * 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
222 * which would let us add new statistics straightforwardly and indicate which
223 * statistics we are and are *not* providing, rather than having to provide
224 * possibly-bogus values for statistics we can't provide.
227 pcap_stats_ex_win32(pcap_t
*p
, int *pcap_stat_size
)
229 struct bpf_stat bstats
;
230 char errbuf
[PCAP_ERRBUF_SIZE
+1];
232 *pcap_stat_size
= sizeof (p
->stat
);
235 * Try to get statistics.
237 * (Please note - "struct pcap_stat" is *not* the same as
238 * WinPcap's "struct bpf_stat". It might currently have the
239 * same layout, but let's not cheat.)
241 if (!PacketGetStatsEx(p
->adapter
, &bstats
)) {
242 pcap_win32_err_to_str(GetLastError(), errbuf
);
243 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
244 "PacketGetStatsEx error: %s", errbuf
);
247 p
->stat
.ps_recv
= bstats
.bs_recv
;
248 p
->stat
.ps_drop
= bstats
.bs_drop
;
249 p
->stat
.ps_ifdrop
= bstats
.ps_ifdrop
;
251 p
->stat
.ps_capt
= bstats
.bs_capt
;
256 /* Set the dimension of the kernel-level capture buffer */
258 pcap_setbuff_win32(pcap_t
*p
, int dim
)
260 if(PacketSetBuff(p
->adapter
,dim
)==FALSE
)
262 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "driver error: not enough memory to allocate the kernel buffer");
268 /* Set the driver working mode */
270 pcap_setmode_win32(pcap_t
*p
, int mode
)
272 if(PacketSetMode(p
->adapter
,mode
)==FALSE
)
274 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "driver error: working mode not recognized");
281 /*set the minimum amount of data that will release a read call*/
283 pcap_setmintocopy_win32(pcap_t
*p
, int size
)
285 if(PacketSetMinToCopy(p
->adapter
, size
)==FALSE
)
287 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "driver error: unable to set the requested mintocopy size");
294 pcap_getevent_win32(pcap_t
*p
)
296 return (PacketGetReadEvent(p
->adapter
));
300 pcap_oid_get_request_win32(pcap_t
*p
, bpf_u_int32 oid
, void *data
, size_t *lenp
)
302 PACKET_OID_DATA
*oid_data_arg
;
303 char errbuf
[PCAP_ERRBUF_SIZE
+1];
306 * Allocate a PACKET_OID_DATA structure to hand to PacketRequest().
307 * It should be big enough to hold "*lenp" bytes of data; it
308 * will actually be slightly larger, as PACKET_OID_DATA has a
309 * 1-byte data array at the end, standing in for the variable-length
310 * data that's actually there.
312 oid_data_arg
= malloc(sizeof (PACKET_OID_DATA
) + *lenp
);
313 if (oid_data_arg
== NULL
) {
314 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
315 "Couldn't allocate argument buffer for PacketRequest");
320 * No need to copy the data - we're doing a fetch.
322 oid_data_arg
->Oid
= oid
;
323 oid_data_arg
->Length
= (ULONG
)(*lenp
); /* XXX - check for ridiculously large value? */
324 if (!PacketRequest(p
->adapter
, FALSE
, oid_data_arg
)) {
325 pcap_win32_err_to_str(GetLastError(), errbuf
);
326 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
327 "Error calling PacketRequest: %s", errbuf
);
333 * Get the length actually supplied.
335 *lenp
= oid_data_arg
->Length
;
338 * Copy back the data we fetched.
340 memcpy(data
, oid_data_arg
->Data
, *lenp
);
346 pcap_oid_set_request_win32(pcap_t
*p
, bpf_u_int32 oid
, const void *data
,
349 PACKET_OID_DATA
*oid_data_arg
;
350 char errbuf
[PCAP_ERRBUF_SIZE
+1];
353 * Allocate a PACKET_OID_DATA structure to hand to PacketRequest().
354 * It should be big enough to hold "*lenp" bytes of data; it
355 * will actually be slightly larger, as PACKET_OID_DATA has a
356 * 1-byte data array at the end, standing in for the variable-length
357 * data that's actually there.
359 oid_data_arg
= malloc(sizeof (PACKET_OID_DATA
) + *lenp
);
360 if (oid_data_arg
== NULL
) {
361 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
362 "Couldn't allocate argument buffer for PacketRequest");
366 oid_data_arg
->Oid
= oid
;
367 oid_data_arg
->Length
= (ULONG
)(*lenp
); /* XXX - check for ridiculously large value? */
368 memcpy(oid_data_arg
->Data
, data
, *lenp
);
369 if (!PacketRequest(p
->adapter
, TRUE
, oid_data_arg
)) {
370 pcap_win32_err_to_str(GetLastError(), errbuf
);
371 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
372 "Error calling PacketRequest: %s", errbuf
);
378 * Get the length actually copied.
380 *lenp
= oid_data_arg
->Length
;
383 * No need to copy the data - we're doing a set.
390 pcap_sendqueue_transmit_win32(pcap_t
*p
, pcap_send_queue
*queue
, int sync
)
393 char errbuf
[PCAP_ERRBUF_SIZE
+1];
395 if (p
->adapter
==NULL
) {
396 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
397 "Cannot transmit a queue to an offline capture or to a TurboCap port");
401 res
= PacketSendPackets(p
->adapter
,
406 if(res
!= queue
->len
){
407 pcap_win32_err_to_str(GetLastError(), errbuf
);
408 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
409 "Error opening adapter: %s", errbuf
);
416 pcap_setuserbuffer_win32(pcap_t
*p
, int size
)
418 unsigned char *new_buff
;
421 /* Bogus parameter */
422 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
423 "Error: invalid size %d",size
);
427 /* Allocate the buffer */
428 new_buff
=(unsigned char*)malloc(sizeof(char)*size
);
431 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
432 "Error: not enough memory");
445 pcap_live_dump_win32(pcap_t
*p
, char *filename
, int maxsize
, int maxpacks
)
449 /* Set the packet driver in dump mode */
450 res
= PacketSetMode(p
->adapter
, PACKET_MODE_DUMP
);
452 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
453 "Error setting dump mode");
457 /* Set the name of the dump file */
458 res
= PacketSetDumpName(p
->adapter
, filename
, (int)strlen(filename
));
460 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
461 "Error setting kernel dump file name");
465 /* Set the limits of the dump file */
466 res
= PacketSetDumpLimits(p
->adapter
, maxsize
, maxpacks
);
468 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
469 "Error setting dump limit");
477 pcap_live_dump_ended_win32(pcap_t
*p
, int sync
)
479 return (PacketIsDumpEnded(p
->adapter
, (BOOLEAN
)sync
));
482 static PAirpcapHandle
483 pcap_get_airpcap_handle_win32(pcap_t
*p
)
485 #ifdef HAVE_AIRPCAP_API
486 return (PacketGetAirPcapHandle(p
->adapter
));
489 #endif /* HAVE_AIRPCAP_API */
493 pcap_read_win32_npf(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
498 register u_char
*bp
, *ep
;
500 struct pcap_win
*pw
= p
->priv
;
505 * Has "pcap_breakloop()" been called?
509 * Yes - clear the flag that indicates that it
510 * has, and return PCAP_ERROR_BREAK to indicate
511 * that we were told to break out of the loop.
514 return (PCAP_ERROR_BREAK
);
518 * Capture the packets.
520 * The PACKET structure had a bunch of extra stuff for
521 * Windows 9x/Me, but the only interesting data in it
522 * in the versions of Windows that we support is just
523 * a copy of p->buffer, a copy of p->buflen, and the
524 * actual number of bytes read returned from
525 * PacketReceivePacket(), none of which has to be
526 * retained from call to call, so we just keep one on
529 PacketInitPacket(&Packet
, (BYTE
*)p
->buffer
, p
->bufsize
);
530 if (!PacketReceivePacket(p
->adapter
, &Packet
, TRUE
)) {
531 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "read error: PacketReceivePacket failed");
535 cc
= Packet
.ulBytesReceived
;
543 * Loop through each packet.
545 #define bhp ((struct bpf_hdr *)bp)
548 register int caplen
, hdrlen
;
551 * Has "pcap_breakloop()" been called?
552 * If so, return immediately - if we haven't read any
553 * packets, clear the flag and return PCAP_ERROR_BREAK
554 * to indicate that we were told to break out of the loop,
555 * otherwise leave the flag set, so that the *next* call
556 * will break out of the loop without having read any
557 * packets, and return the number of packets we've
563 return (PCAP_ERROR_BREAK
);
566 p
->cc
= (int) (ep
- bp
);
573 caplen
= bhp
->bh_caplen
;
574 hdrlen
= bhp
->bh_hdrlen
;
578 * Short-circuit evaluation: if using BPF filter
579 * in kernel, no need to do it now - we already know
580 * the packet passed the filter.
582 * XXX - bpf_filter() should always return TRUE if
583 * handed a null pointer for the program, but it might
584 * just try to "run" the filter, so we check here.
586 if (pw
->filtering_in_kernel
||
587 p
->fcode
.bf_insns
== NULL
||
588 bpf_filter(p
->fcode
.bf_insns
, datap
, bhp
->bh_datalen
, caplen
)) {
590 switch (p
->rmt_samp
.method
) {
592 case PCAP_SAMP_1_EVERY_N
:
593 pw
->samp_npkt
= (pw
->samp_npkt
+ 1) % p
->rmt_samp
.value
;
595 /* Discard all packets that are not '1 out of N' */
596 if (pw
->samp_npkt
!= 0) {
597 bp
+= Packet_WORDALIGN(caplen
+ hdrlen
);
602 case PCAP_SAMP_FIRST_AFTER_N_MS
:
604 struct pcap_pkthdr
*pkt_header
= (struct pcap_pkthdr
*) bp
;
607 * Check if the timestamp of the arrived
608 * packet is smaller than our target time.
610 if (pkt_header
->ts
.tv_sec
< pw
->samp_time
.tv_sec
||
611 (pkt_header
->ts
.tv_sec
== pw
->samp_time
.tv_sec
&& pkt_header
->ts
.tv_usec
< pw
->samp_time
.tv_usec
)) {
612 bp
+= Packet_WORDALIGN(caplen
+ hdrlen
);
617 * The arrived packet is suitable for being
618 * delivered to our caller, so let's update
621 pw
->samp_time
.tv_usec
= pkt_header
->ts
.tv_usec
+ p
->rmt_samp
.value
* 1000;
622 if (pw
->samp_time
.tv_usec
> 1000000) {
623 pw
->samp_time
.tv_sec
= pkt_header
->ts
.tv_sec
+ pw
->samp_time
.tv_usec
/ 1000000;
624 pw
->samp_time
.tv_usec
= pw
->samp_time
.tv_usec
% 1000000;
628 #endif /* HAVE_REMOTE */
631 * XXX A bpf_hdr matches a pcap_pkthdr.
633 (*callback
)(user
, (struct pcap_pkthdr
*)bp
, datap
);
634 bp
+= Packet_WORDALIGN(caplen
+ hdrlen
);
635 if (++n
>= cnt
&& !PACKET_COUNT_IS_UNLIMITED(cnt
)) {
637 p
->cc
= (int) (ep
- bp
);
644 bp
+= Packet_WORDALIGN(caplen
+ hdrlen
);
654 pcap_read_win32_dag(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
656 struct pcap_win
*pw
= p
->priv
;
659 int packet_len
= 0, caplen
= 0;
660 struct pcap_pkthdr pcap_header
;
663 dag_record_t
*header
;
664 unsigned erf_record_len
;
668 unsigned dfp
= p
->adapter
->DagFastProcess
;
671 if (cc
== 0) /* Get new packets only if we have processed all the ones of the previous read */
674 * Get new packets from the network.
676 * The PACKET structure had a bunch of extra stuff for
677 * Windows 9x/Me, but the only interesting data in it
678 * in the versions of Windows that we support is just
679 * a copy of p->buffer, a copy of p->buflen, and the
680 * actual number of bytes read returned from
681 * PacketReceivePacket(), none of which has to be
682 * retained from call to call, so we just keep one on
685 PacketInitPacket(&Packet
, (BYTE
*)p
->buffer
, p
->bufsize
);
686 if (!PacketReceivePacket(p
->adapter
, &Packet
, TRUE
)) {
687 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "read error: PacketReceivePacket failed");
691 cc
= Packet
.ulBytesReceived
;
693 /* The timeout has expired but we no packets arrived */
695 header
= (dag_record_t
*)p
->adapter
->DagBuffer
;
698 header
= (dag_record_t
*)p
->bp
;
700 endofbuf
= (char*)header
+ cc
;
703 * Cycle through the packets
707 erf_record_len
= SWAPS(header
->rlen
);
708 if((char*)header
+ erf_record_len
> endofbuf
)
711 /* Increase the number of captured packets */
714 /* Find the beginning of the packet */
715 dp
= ((u_char
*)header
) + dag_record_size
;
717 /* Determine actual packet len */
721 packet_len
= ATM_SNAPLEN
;
722 caplen
= ATM_SNAPLEN
;
728 swt
= SWAPS(header
->wlen
);
729 packet_len
= swt
- (pw
->dag_fcs_bits
);
730 caplen
= erf_record_len
- dag_record_size
- 2;
731 if (caplen
> packet_len
)
740 swt
= SWAPS(header
->wlen
);
741 packet_len
= swt
- (pw
->dag_fcs_bits
);
742 caplen
= erf_record_len
- dag_record_size
;
743 if (caplen
> packet_len
)
751 if(caplen
> p
->snapshot
)
752 caplen
= p
->snapshot
;
755 * Has "pcap_breakloop()" been called?
756 * If so, return immediately - if we haven't read any
757 * packets, clear the flag and return -2 to indicate
758 * that we were told to break out of the loop, otherwise
759 * leave the flag set, so that the *next* call will break
760 * out of the loop without having read any packets, and
761 * return the number of packets we've processed so far.
772 p
->bp
= (char*)header
;
773 p
->cc
= endofbuf
- (char*)header
;
780 /* convert between timestamp formats */
782 pcap_header
.ts
.tv_sec
= (int)(ts
>> 32);
783 ts
= (ts
& 0xffffffffi
64) * 1000000;
784 ts
+= 0x80000000; /* rounding */
785 pcap_header
.ts
.tv_usec
= (int)(ts
>> 32);
786 if (pcap_header
.ts
.tv_usec
>= 1000000) {
787 pcap_header
.ts
.tv_usec
-= 1000000;
788 pcap_header
.ts
.tv_sec
++;
792 /* No underlaying filtering system. We need to filter on our own */
793 if (p
->fcode
.bf_insns
)
795 if (bpf_filter(p
->fcode
.bf_insns
, dp
, packet_len
, caplen
) == 0)
797 /* Move to next packet */
798 header
= (dag_record_t
*)((char*)header
+ erf_record_len
);
803 /* Fill the header for the user suppplied callback function */
804 pcap_header
.caplen
= caplen
;
805 pcap_header
.len
= packet_len
;
807 /* Call the callback function */
808 (*callback
)(user
, &pcap_header
, dp
);
810 /* Move to next packet */
811 header
= (dag_record_t
*)((char*)header
+ erf_record_len
);
813 /* Stop if the number of packets requested by user has been reached*/
814 if (++n
>= cnt
&& !PACKET_COUNT_IS_UNLIMITED(cnt
))
816 p
->bp
= (char*)header
;
817 p
->cc
= endofbuf
- (char*)header
;
821 while((u_char
*)header
< endofbuf
);
825 #endif /* HAVE_DAG_API */
827 /* Send a packet to the network */
829 pcap_inject_win32(pcap_t
*p
, const void *buf
, size_t size
)
833 PacketInitPacket(&pkt
, (PVOID
)buf
, size
);
834 if(PacketSendPacket(p
->adapter
,&pkt
,TRUE
) == FALSE
) {
835 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "send error: PacketSendPacket failed");
840 * We assume it all got sent if "PacketSendPacket()" succeeded.
841 * "pcap_inject()" is expected to return the number of bytes
848 pcap_cleanup_win32(pcap_t
*p
)
850 struct pcap_win
*pw
= p
->priv
;
851 if (p
->adapter
!= NULL
) {
852 PacketCloseAdapter(p
->adapter
);
855 if (pw
->rfmon_selfstart
)
857 PacketSetMonitorMode(p
->opt
.device
, 0);
859 pcap_cleanup_live_common(p
);
863 pcap_activate_win32(pcap_t
*p
)
865 struct pcap_win
*pw
= p
->priv
;
868 char errbuf
[PCAP_ERRBUF_SIZE
+1];
872 * Monitor mode is supported on Windows Vista and later.
874 if (PacketGetMonitorMode(p
->opt
.device
) == 1)
876 pw
->rfmon_selfstart
= 0;
880 if ((res
= PacketSetMonitorMode(p
->opt
.device
, 1)) != 1)
882 pw
->rfmon_selfstart
= 0;
883 // Monitor mode is not supported.
886 return PCAP_ERROR_RFMON_NOTSUP
;
895 pw
->rfmon_selfstart
= 1;
903 p
->adapter
= PacketOpenAdapter(p
->opt
.device
);
905 if (p
->adapter
== NULL
)
907 /* Adapter detected but we are not able to open it. Return failure. */
908 pcap_win32_err_to_str(GetLastError(), errbuf
);
909 if (pw
->rfmon_selfstart
)
911 PacketSetMonitorMode(p
->opt
.device
, 0);
913 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
914 "Error opening adapter: %s", errbuf
);
919 if(PacketGetNetType (p
->adapter
,&type
) == FALSE
)
921 pcap_win32_err_to_str(GetLastError(), errbuf
);
922 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
923 "Cannot determine the network type: %s", errbuf
);
928 switch (type
.LinkType
)
931 p
->linktype
= DLT_EN10MB
;
934 case NdisMedium802_3
:
935 p
->linktype
= DLT_EN10MB
;
937 * This is (presumably) a real Ethernet capture; give it a
938 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
939 * that an application can let you choose it, in case you're
940 * capturing DOCSIS traffic that a Cisco Cable Modem
941 * Termination System is putting out onto an Ethernet (it
942 * doesn't put an Ethernet header onto the wire, it puts raw
943 * DOCSIS frames out on the wire inside the low-level
946 p
->dlt_list
= (u_int
*) malloc(sizeof(u_int
) * 2);
948 * If that fails, just leave the list empty.
950 if (p
->dlt_list
!= NULL
) {
951 p
->dlt_list
[0] = DLT_EN10MB
;
952 p
->dlt_list
[1] = DLT_DOCSIS
;
958 p
->linktype
= DLT_FDDI
;
961 case NdisMedium802_5
:
962 p
->linktype
= DLT_IEEE802
;
965 case NdisMediumArcnetRaw
:
966 p
->linktype
= DLT_ARCNET
;
969 case NdisMediumArcnet878_2
:
970 p
->linktype
= DLT_ARCNET
;
974 p
->linktype
= DLT_ATM_RFC1483
;
977 case NdisMediumCHDLC
:
978 p
->linktype
= DLT_CHDLC
;
981 case NdisMediumPPPSerial
:
982 p
->linktype
= DLT_PPP_SERIAL
;
986 p
->linktype
= DLT_NULL
;
989 case NdisMediumBare80211
:
990 p
->linktype
= DLT_IEEE802_11
;
993 case NdisMediumRadio80211
:
994 p
->linktype
= DLT_IEEE802_11_RADIO
;
998 p
->linktype
= DLT_PPI
;
1002 p
->linktype
= DLT_EN10MB
; /*an unknown adapter is assumed to be ethernet*/
1007 * Turn a negative snapshot value (invalid), a snapshot value of
1008 * 0 (unspecified), or a value bigger than the normal maximum
1009 * value, into the maximum allowed value.
1011 * If some application really *needs* a bigger snapshot
1012 * length, we should just increase MAXIMUM_SNAPLEN.
1014 if (p
->snapshot
<= 0 || p
->snapshot
> MAXIMUM_SNAPLEN
)
1015 p
->snapshot
= MAXIMUM_SNAPLEN
;
1017 /* Set promiscuous mode */
1021 if (PacketSetHwFilter(p
->adapter
,NDIS_PACKET_TYPE_PROMISCUOUS
) == FALSE
)
1023 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "failed to set hardware filter to promiscuous mode");
1029 if (PacketSetHwFilter(p
->adapter
,NDIS_PACKET_TYPE_ALL_LOCAL
) == FALSE
)
1031 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "failed to set hardware filter to non-promiscuous mode");
1036 /* Set the buffer size */
1037 p
->bufsize
= WIN32_DEFAULT_USER_BUFFER_SIZE
;
1039 if(!(p
->adapter
->Flags
& INFO_FLAG_DAG_CARD
))
1042 * Traditional Adapter
1045 * If the buffer size wasn't explicitly set, default to
1046 * WIN32_DEFAULT_KERNEL_BUFFER_SIZE.
1048 if (p
->opt
.buffer_size
== 0)
1049 p
->opt
.buffer_size
= WIN32_DEFAULT_KERNEL_BUFFER_SIZE
;
1051 if(PacketSetBuff(p
->adapter
,p
->opt
.buffer_size
)==FALSE
)
1053 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "driver error: not enough memory to allocate the kernel buffer");
1057 p
->buffer
= malloc(p
->bufsize
);
1058 if (p
->buffer
== NULL
)
1060 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "malloc: %s", pcap_strerror(errno
));
1064 if (p
->opt
.immediate
)
1066 /* tell the driver to copy the buffer as soon as data arrives */
1067 if(PacketSetMinToCopy(p
->adapter
,0)==FALSE
)
1069 pcap_win32_err_to_str(GetLastError(), errbuf
);
1070 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1071 "Error calling PacketSetMinToCopy: %s",
1078 /* tell the driver to copy the buffer only if it contains at least 16K */
1079 if(PacketSetMinToCopy(p
->adapter
,16000)==FALSE
)
1081 pcap_win32_err_to_str(GetLastError(), errbuf
);
1082 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1083 "Error calling PacketSetMinToCopy: %s",
1094 * We have DAG support.
1103 pcap_snprintf(keyname
, sizeof(keyname
), "%s\\CardParams\\%s",
1104 "SYSTEM\\CurrentControlSet\\Services\\DAG",
1105 strstr(_strlwr(p
->opt
.device
), "dag"));
1108 status
= RegOpenKeyEx(HKEY_LOCAL_MACHINE
, keyname
, 0, KEY_READ
, &dagkey
);
1109 if(status
!= ERROR_SUCCESS
)
1112 status
= RegQueryValueEx(dagkey
,
1119 if(status
!= ERROR_SUCCESS
)
1124 RegCloseKey(dagkey
);
1129 p
->snapshot
= PacketSetSnapLen(p
->adapter
, p
->snapshot
);
1131 /* Set the length of the FCS associated to any packet. This value
1132 * will be subtracted to the packet length */
1133 pw
->dag_fcs_bits
= p
->adapter
->DagFcsLen
;
1134 #else /* HAVE_DAG_API */
1139 #endif /* HAVE_DAG_API */
1142 PacketSetReadTimeout(p
->adapter
, p
->opt
.timeout
);
1145 if(p
->adapter
->Flags
& INFO_FLAG_DAG_CARD
)
1147 /* install dag specific handlers for read and setfilter */
1148 p
->read_op
= pcap_read_win32_dag
;
1149 p
->setfilter_op
= pcap_setfilter_win32_dag
;
1153 #endif /* HAVE_DAG_API */
1154 /* install traditional npf handlers for read and setfilter */
1155 p
->read_op
= pcap_read_win32_npf
;
1156 p
->setfilter_op
= pcap_setfilter_win32_npf
;
1159 #endif /* HAVE_DAG_API */
1160 p
->setdirection_op
= NULL
; /* Not implemented. */
1161 /* XXX - can this be implemented on some versions of Windows? */
1162 p
->inject_op
= pcap_inject_win32
;
1163 p
->set_datalink_op
= NULL
; /* can't change data link type */
1164 p
->getnonblock_op
= pcap_getnonblock_win32
;
1165 p
->setnonblock_op
= pcap_setnonblock_win32
;
1166 p
->stats_op
= pcap_stats_win32
;
1167 p
->stats_ex_op
= pcap_stats_ex_win32
;
1168 p
->setbuff_op
= pcap_setbuff_win32
;
1169 p
->setmode_op
= pcap_setmode_win32
;
1170 p
->setmintocopy_op
= pcap_setmintocopy_win32
;
1171 p
->getevent_op
= pcap_getevent_win32
;
1172 p
->oid_get_request_op
= pcap_oid_get_request_win32
;
1173 p
->oid_set_request_op
= pcap_oid_set_request_win32
;
1174 p
->sendqueue_transmit_op
= pcap_sendqueue_transmit_win32
;
1175 p
->setuserbuffer_op
= pcap_setuserbuffer_win32
;
1176 p
->live_dump_op
= pcap_live_dump_win32
;
1177 p
->live_dump_ended_op
= pcap_live_dump_ended_win32
;
1178 p
->get_airpcap_handle_op
= pcap_get_airpcap_handle_win32
;
1179 p
->cleanup_op
= pcap_cleanup_win32
;
1183 pcap_cleanup_win32(p
);
1184 return (PCAP_ERROR
);
1188 * Check if rfmon mode is supported on the pcap_t for Windows systems.
1191 pcap_can_set_rfmon_win32(pcap_t
*p
)
1193 return (PacketIsMonitorModeSupported(p
->opt
.device
) == 1);
1197 pcap_create_interface(const char *device _U_
, char *ebuf
)
1201 p
= pcap_create_common(ebuf
, sizeof(struct pcap_win
));
1205 p
->activate_op
= pcap_activate_win32
;
1206 p
->can_set_rfmon_op
= pcap_can_set_rfmon_win32
;
1211 pcap_setfilter_win32_npf(pcap_t
*p
, struct bpf_program
*fp
)
1213 struct pcap_win
*pw
= p
->priv
;
1215 if(PacketSetBpf(p
->adapter
,fp
)==FALSE
){
1217 * Kernel filter not installed.
1219 * XXX - we don't know whether this failed because:
1221 * the kernel rejected the filter program as invalid,
1222 * in which case we should fall back on userland
1225 * the kernel rejected the filter program as too big,
1226 * in which case we should again fall back on
1227 * userland filtering;
1229 * there was some other problem, in which case we
1230 * should probably report an error.
1232 * For NPF devices, the Win32 status will be
1233 * STATUS_INVALID_DEVICE_REQUEST for invalid
1234 * filters, but I don't know what it'd be for
1235 * other problems, and for some other devices
1236 * it might not be set at all.
1238 * So we just fall back on userland filtering in
1243 * install_bpf_program() validates the program.
1245 * XXX - what if we already have a filter in the kernel?
1247 if (install_bpf_program(p
, fp
) < 0)
1249 pw
->filtering_in_kernel
= 0; /* filtering in userland */
1256 pw
->filtering_in_kernel
= 1; /* filtering in the kernel */
1259 * Discard any previously-received packets, as they might have
1260 * passed whatever filter was formerly in effect, but might
1261 * not pass this filter (BIOCSETF discards packets buffered
1262 * in the kernel, so you can lose packets in any case).
1269 * We filter at user level, since the kernel driver does't process the packets
1272 pcap_setfilter_win32_dag(pcap_t
*p
, struct bpf_program
*fp
) {
1276 strlcpy(p
->errbuf
, "setfilter: No filter specified", sizeof(p
->errbuf
));
1280 /* Install a user level filter */
1281 if (install_bpf_program(p
, fp
) < 0)
1283 pcap_snprintf(p
->errbuf
, sizeof(p
->errbuf
),
1284 "setfilter, unable to install the filter: %s", pcap_strerror(errno
));
1292 pcap_getnonblock_win32(pcap_t
*p
)
1294 struct pcap_win
*pw
= p
->priv
;
1297 * XXX - if there were a PacketGetReadTimeout() call, we
1298 * would use it, and return 1 if the timeout is -1
1301 return (pw
->nonblock
);
1305 pcap_setnonblock_win32(pcap_t
*p
, int nonblock
)
1307 struct pcap_win
*pw
= p
->priv
;
1309 char win_errbuf
[PCAP_ERRBUF_SIZE
+1];
1313 * Set the packet buffer timeout to -1 for non-blocking
1319 * Restore the timeout set when the device was opened.
1320 * (Note that this may be -1, in which case we're not
1321 * really leaving non-blocking mode. However, although
1322 * the timeout argument to pcap_set_timeout() and
1323 * pcap_open_live() is an int, you're not supposed to
1324 * supply a negative value, so that "shouldn't happen".)
1326 newtimeout
= p
->opt
.timeout
;
1328 if (!PacketSetReadTimeout(p
->adapter
, newtimeout
)) {
1329 pcap_win32_err_to_str(GetLastError(), win_errbuf
);
1330 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1331 "PacketSetReadTimeout: %s", win_errbuf
);
1334 pw
->nonblock
= (newtimeout
== -1);
1339 pcap_add_if_win32(pcap_if_list_t
*devlistp
, char *name
, bpf_u_int32 flags
,
1340 const char *description
, char *errbuf
)
1343 npf_if_addr if_addrs
[MAX_NETWORK_ADDRESSES
];
1347 if_addr_size
= MAX_NETWORK_ADDRESSES
;
1350 * Add an entry for this interface, with no addresses.
1352 curdev
= add_dev(devlistp
, name
, flags
, description
, errbuf
);
1353 if (curdev
== NULL
) {
1361 * Get the list of addresses for the interface.
1363 if (!PacketGetNetInfoEx((void *)name
, if_addrs
, &if_addr_size
)) {
1367 * We don't return an error, because this can happen with
1368 * NdisWan interfaces, and we want to supply them even
1369 * if we can't supply their addresses.
1371 * We return an entry with an empty address list.
1377 * Now add the addresses.
1379 while (if_addr_size
-- > 0) {
1381 * "curdev" is an entry for this interface; add an entry for
1382 * this address to its list of addresses.
1384 res
= add_addr_to_dev(curdev
,
1385 (struct sockaddr
*)&if_addrs
[if_addr_size
].IPAddress
,
1386 sizeof (struct sockaddr_storage
),
1387 (struct sockaddr
*)&if_addrs
[if_addr_size
].SubnetMask
,
1388 sizeof (struct sockaddr_storage
),
1389 (struct sockaddr
*)&if_addrs
[if_addr_size
].Broadcast
,
1390 sizeof (struct sockaddr_storage
),
1406 pcap_platform_finddevs(pcap_if_list_t
*devlistp
, char *errbuf
)
1413 char our_errbuf
[PCAP_ERRBUF_SIZE
+1];
1416 * Find out how big a buffer we need.
1418 * This call should always return FALSE; if the error is
1419 * ERROR_INSUFFICIENT_BUFFER, NameLength will be set to
1420 * the size of the buffer we need, otherwise there's a
1421 * problem, and NameLength should be set to 0.
1423 * It shouldn't require NameLength to be set, but,
1424 * at least as of WinPcap 4.1.3, it checks whether
1425 * NameLength is big enough before it checks for a
1426 * NULL buffer argument, so, while it'll still do
1427 * the right thing if NameLength is uninitialized and
1428 * whatever junk happens to be there is big enough
1429 * (because the pointer argument will be null), it's
1430 * still reading an uninitialized variable.
1433 if (!PacketGetAdapterNames(NULL
, &NameLength
))
1435 DWORD last_error
= GetLastError();
1437 if (last_error
!= ERROR_INSUFFICIENT_BUFFER
)
1439 pcap_win32_err_to_str(last_error
, our_errbuf
);
1440 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1441 "PacketGetAdapterNames: %s", our_errbuf
);
1446 if (NameLength
<= 0)
1448 AdaptersName
= (char*) malloc(NameLength
);
1449 if (AdaptersName
== NULL
)
1451 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "Cannot allocate enough memory to list the adapters.");
1455 if (!PacketGetAdapterNames(AdaptersName
, &NameLength
)) {
1456 pcap_win32_err_to_str(GetLastError(), our_errbuf
);
1457 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "PacketGetAdapterNames: %s",
1464 * "PacketGetAdapterNames()" returned a list of
1465 * null-terminated ASCII interface name strings,
1466 * terminated by a null string, followed by a list
1467 * of null-terminated ASCII interface description
1468 * strings, terminated by a null string.
1469 * This means there are two ASCII nulls at the end
1470 * of the first list.
1472 * Find the end of the first list; that's the
1473 * beginning of the second list.
1475 desc
= &AdaptersName
[0];
1476 while (*desc
!= '\0' || *(desc
+ 1) != '\0')
1480 * Found it - "desc" points to the first of the two
1481 * nulls at the end of the list of names, so the
1482 * first byte of the list of descriptions is two bytes
1488 * Loop over the elements in the first list.
1490 name
= &AdaptersName
[0];
1491 while (*name
!= '\0') {
1492 bpf_u_int32 flags
= 0;
1493 #ifdef HAVE_PACKET_IS_LOOPBACK_ADAPTER
1495 * Is this a loopback interface?
1497 if (PacketIsLoopbackAdapter(name
)) {
1499 flags
|= PCAP_IF_LOOPBACK
;
1504 * Add an entry for this interface.
1506 if (pcap_add_if_win32(devlistp
, name
, flags
, desc
,
1514 name
+= strlen(name
) + 1;
1515 desc
+= strlen(desc
) + 1;