2 * Copyright (c) 1999 - 2003
3 * NetGroup, Politecnico di Torino (Italy)
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 nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 static const char rcsid
[] _U_
=
35 "@(#) $Header: /tcpdump/master/libpcap/pcap-win32.c,v 1.21 2004-03-23 19:18:07 guy Exp $ (LBL)";
44 #endif /* HAVE_DAG_API */
47 #define errno (*_errno())
48 #endif /* __MINGW32__ */
50 static int pcap_setfilter_win32_npf(pcap_t
*, struct bpf_program
*);
51 static int pcap_setfilter_win32_dag(pcap_t
*, struct bpf_program
*);
52 static int pcap_getnonblock_win32(pcap_t
*, char *);
53 static int pcap_setnonblock_win32(pcap_t
*, int, char *);
55 #define PcapBufSize 256000 /*dimension of the buffer in the pcap_t structure*/
56 #define SIZE_BUF 1000000
58 /* Equivalent to ntohs(), but a lot faster under Windows */
59 #define SWAPS(_X) ((_X & 0xff) << 8) | (_X >> 8)
62 * Header that the WinPcap driver associates to the packets.
66 struct timeval bh_tstamp
; /* time stamp */
67 bpf_u_int32 bh_caplen
; /* length of captured portion */
68 bpf_u_int32 bh_datalen
; /* original length of packet */
69 u_short bh_hdrlen
; /* length of bpf header (this struct
70 plus alignment padding) */
77 WORD wVersionRequested
;
80 wVersionRequested
= MAKEWORD( 1, 1);
81 err
= WSAStartup( wVersionRequested
, &wsaData
);
91 pcap_stats_win32(pcap_t
*p
, struct pcap_stat
*ps
)
94 if(PacketGetStats(p
->adapter
, (struct bpf_stat
*)ps
) != TRUE
){
95 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "PacketGetStats error: %s", pcap_win32strerror());
103 pcap_read_win32_npf(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
107 register u_char
*bp
, *ep
;
112 * Has "pcap_breakloop()" been called?
116 * Yes - clear the flag that indicates that it
117 * has, and return -2 to indicate that we were
118 * told to break out of the loop.
124 /* capture the packets */
125 if(PacketReceivePacket(p
->adapter
,p
->Packet
,TRUE
)==FALSE
){
126 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "read error: PacketReceivePacket failed");
130 cc
= p
->Packet
->ulBytesReceived
;
132 bp
= p
->Packet
->Buffer
;
138 * Loop through each packet.
140 #define bhp ((struct bpf_hdr *)bp)
143 register int caplen
, hdrlen
;
146 * Has "pcap_breakloop()" been called?
147 * If so, return immediately - if we haven't read any
148 * packets, clear the flag and return -2 to indicate
149 * that we were told to break out of the loop, otherwise
150 * leave the flag set, so that the *next* call will break
151 * out of the loop without having read any packets, and
152 * return the number of packets we've processed so far.
167 caplen
= bhp
->bh_caplen
;
168 hdrlen
= bhp
->bh_hdrlen
;
171 * XXX A bpf_hdr matches a pcap_pkthdr.
173 (*callback
)(user
, (struct pcap_pkthdr
*)bp
, bp
+ hdrlen
);
174 bp
+= BPF_WORDALIGN(caplen
+ hdrlen
);
175 if (++n
>= cnt
&& cnt
> 0) {
188 pcap_read_win32_dag(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
191 int packet_len
= 0, caplen
= 0;
192 struct pcap_pkthdr pcap_header
;
195 dag_record_t
*header
;
196 unsigned erf_record_len
;
200 unsigned dfp
= p
->adapter
->DagFastProcess
;
203 if (cc
== 0) /* Get new packets only if we have processed all the ones of the previous read */
205 /* Get new packets from the network */
206 if(PacketReceivePacket(p
->adapter
, p
->Packet
, TRUE
)==FALSE
){
207 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "read error: PacketReceivePacket failed");
211 cc
= p
->Packet
->ulBytesReceived
;
213 /* The timeout has expired but we no packets arrived */
215 header
= (dag_record_t
*)p
->adapter
->DagBuffer
;
218 header
= (dag_record_t
*)p
->bp
;
220 endofbuf
= (char*)header
+ cc
;
223 * Cycle through the packets
227 erf_record_len
= SWAPS(header
->rlen
);
228 if((char*)header
+ erf_record_len
> endofbuf
)
231 /* Increase the number of captured packets */
232 p
->md
.stat
.ps_recv
++;
234 /* Find the beginning of the packet */
235 dp
= ((u_char
*)header
) + dag_record_size
;
237 /* Determine actual packet len */
241 packet_len
= ATM_SNAPLEN
;
242 caplen
= ATM_SNAPLEN
;
248 swt
= SWAPS(header
->wlen
);
249 packet_len
= swt
- (p
->md
.dag_fcs_bits
);
250 caplen
= erf_record_len
- dag_record_size
- 2;
251 if (caplen
> packet_len
)
260 swt
= SWAPS(header
->wlen
);
261 packet_len
= swt
- (p
->md
.dag_fcs_bits
);
262 caplen
= erf_record_len
- dag_record_size
;
263 if (caplen
> packet_len
)
271 if(caplen
> p
->snapshot
)
272 caplen
= p
->snapshot
;
275 * Has "pcap_breakloop()" been called?
276 * If so, return immediately - if we haven't read any
277 * packets, clear the flag and return -2 to indicate
278 * that we were told to break out of the loop, otherwise
279 * leave the flag set, so that the *next* call will break
280 * out of the loop without having read any packets, and
281 * return the number of packets we've processed so far.
292 p
->bp
= (char*)header
;
293 p
->cc
= endofbuf
- (char*)header
;
300 /* convert between timestamp formats */
302 pcap_header
.ts
.tv_sec
= (int)(ts
>> 32);
303 ts
= (ts
& 0xffffffffi
64) * 1000000;
304 ts
+= 0x80000000; /* rounding */
305 pcap_header
.ts
.tv_usec
= (int)(ts
>> 32);
306 if (pcap_header
.ts
.tv_usec
>= 1000000) {
307 pcap_header
.ts
.tv_usec
-= 1000000;
308 pcap_header
.ts
.tv_sec
++;
312 /* No underlaying filtering system. We need to filter on our own */
313 if (p
->fcode
.bf_insns
)
315 if (bpf_filter(p
->fcode
.bf_insns
, dp
, packet_len
, caplen
) == 0)
317 /* Move to next packet */
318 header
= (dag_record_t
*)((char*)header
+ erf_record_len
);
323 /* Fill the header for the user suppplied callback function */
324 pcap_header
.caplen
= caplen
;
325 pcap_header
.len
= packet_len
;
327 /* Call the callback function */
328 (*callback
)(user
, &pcap_header
, dp
);
330 /* Move to next packet */
331 header
= (dag_record_t
*)((char*)header
+ erf_record_len
);
333 /* Stop if the number of packets requested by user has been reached*/
334 if (++n
>= cnt
&& cnt
> 0)
336 p
->bp
= (char*)header
;
337 p
->cc
= endofbuf
- (char*)header
;
341 while((u_char
*)header
< endofbuf
);
345 #endif /* HAVE_DAG_API */
347 /* Send a packet to the network */
349 pcap_inject_win32(pcap_t
*p
, const void *buf
, size_t size
){
350 LPPACKET PacketToSend
;
352 PacketToSend
=PacketAllocatePacket();
353 PacketInitPacket(PacketToSend
,buf
,size
);
354 if(PacketSendPacket(p
->adapter
,PacketToSend
,TRUE
) == FALSE
){
355 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "send error: PacketSendPacket failed");
356 PacketFreePacket(PacketToSend
);
360 PacketFreePacket(PacketToSend
);
363 * We assume it all got sent if "PacketSendPacket()" succeeded.
364 * "pcap_inject()" is expected to return the number of bytes
371 pcap_close_win32(pcap_t
*p
)
373 if (p
->buffer
!= NULL
)
375 if (p
->adapter
!= NULL
) {
376 PacketCloseAdapter(p
->adapter
);
382 pcap_open_live(const char *device
, int snaplen
, int promisc
, int to_ms
,
391 p
= (pcap_t
*)malloc(sizeof(*p
));
394 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc: %s", pcap_strerror(errno
));
397 memset(p
, 0, sizeof(*p
));
400 p
->adapter
= PacketOpenAdapter((char*)device
);
402 if (p
->adapter
== NULL
)
404 /* Adapter detected but we are not able to open it. Return failure. */
405 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "Error opening adapter: %s", pcap_win32strerror());
410 if(PacketGetNetType (p
->adapter
,&type
) == FALSE
)
412 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "Cannot determine the network type: %s", pcap_win32strerror());
417 switch (type
.LinkType
)
420 p
->linktype
= DLT_EN10MB
;
423 case NdisMedium802_3
:
424 p
->linktype
= DLT_EN10MB
;
426 * This is (presumably) a real Ethernet capture; give it a
427 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
428 * that an application can let you choose it, in case you're
429 * capturing DOCSIS traffic that a Cisco Cable Modem
430 * Termination System is putting out onto an Ethernet (it
431 * doesn't put an Ethernet header onto the wire, it puts raw
432 * DOCSIS frames out on the wire inside the low-level
435 p
->dlt_list
= (u_int
*) malloc(sizeof(u_int
) * 2);
437 * If that fails, just leave the list empty.
439 if (p
->dlt_list
!= NULL
) {
440 p
->dlt_list
[0] = DLT_EN10MB
;
441 p
->dlt_list
[1] = DLT_DOCSIS
;
447 p
->linktype
= DLT_FDDI
;
450 case NdisMedium802_5
:
451 p
->linktype
= DLT_IEEE802
;
454 case NdisMediumArcnetRaw
:
455 p
->linktype
= DLT_ARCNET
;
458 case NdisMediumArcnet878_2
:
459 p
->linktype
= DLT_ARCNET
;
463 p
->linktype
= DLT_ATM_RFC1483
;
466 case NdisMediumCHDLC
:
467 p
->linktype
= DLT_CHDLC
;
470 case NdisMediumPPPSerial
:
471 p
->linktype
= DLT_PPP_SERIAL
;
475 p
->linktype
= DLT_NULL
;
479 p
->linktype
= DLT_EN10MB
; /*an unknown adapter is assumed to be ethernet*/
483 /* Set promisquous mode */
484 if (promisc
) PacketSetHwFilter(p
->adapter
,NDIS_PACKET_TYPE_PROMISCUOUS
);
485 else PacketSetHwFilter(p
->adapter
,NDIS_PACKET_TYPE_ALL_LOCAL
);
487 /* Set the buffer size */
488 p
->bufsize
= PcapBufSize
;
490 /* allocate Packet structure used during the capture */
491 if((p
->Packet
= PacketAllocatePacket())==NULL
)
493 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "failed to allocate the PACKET structure");
497 if(!(p
->adapter
->Flags
& INFO_FLAG_DAG_CARD
))
500 * Traditional Adapter
503 p
->buffer
= (u_char
*)malloc(PcapBufSize
);
504 if (p
->buffer
== NULL
)
506 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc: %s", pcap_strerror(errno
));
510 PacketInitPacket(p
->Packet
,(BYTE
*)p
->buffer
,p
->bufsize
);
512 p
->snapshot
= snaplen
;
514 /* allocate the standard buffer in the driver */
515 if(PacketSetBuff( p
->adapter
, SIZE_BUF
)==FALSE
)
517 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,"driver error: not enough memory to allocate the kernel buffer\n");
521 /* tell the driver to copy the buffer only if it contains at least 16K */
522 if(PacketSetMinToCopy(p
->adapter
,16000)==FALSE
)
524 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,"Error calling PacketSetMinToCopy: %s\n", pcap_win32strerror());
541 snprintf(keyname
, sizeof(keyname
), "%s\\CardParams\\%s",
542 "SYSTEM\\CurrentControlSet\\Services\\DAG",
543 strstr(_strlwr((char*)device
), "dag"));
546 status
= RegOpenKeyEx(HKEY_LOCAL_MACHINE
, keyname
, 0, KEY_READ
, &dagkey
);
547 if(status
!= ERROR_SUCCESS
)
550 status
= RegQueryValueEx(dagkey
,
557 if(status
!= ERROR_SUCCESS
)
567 p
->snapshot
= PacketSetSnapLen(p
->adapter
, snaplen
);
569 /* Set the length of the FCS associated to any packet. This value
570 * will be subtracted to the packet length */
571 p
->md
.dag_fcs_bits
= p
->adapter
->DagFcsLen
;
575 #endif /* HAVE_DAG_API */
577 PacketSetReadTimeout(p
->adapter
, to_ms
);
580 if(p
->adapter
->Flags
& INFO_FLAG_DAG_CARD
)
582 /* install dag specific handlers for read and setfilter */
583 p
->read_op
= pcap_read_win32_dag
;
584 p
->setfilter_op
= pcap_setfilter_win32_dag
;
588 #endif /* HAVE_DAG_API */
589 /* install traditional npf handlers for read and setfilter */
590 p
->read_op
= pcap_read_win32_npf
;
591 p
->setfilter_op
= pcap_setfilter_win32_npf
;
594 #endif /* HAVE_DAG_API */
595 p
->inject_op
= pcap_inject_win32
;
596 p
->set_datalink_op
= NULL
; /* can't change data link type */
597 p
->getnonblock_op
= pcap_getnonblock_win32
;
598 p
->setnonblock_op
= pcap_setnonblock_win32
;
599 p
->stats_op
= pcap_stats_win32
;
600 p
->close_op
= pcap_close_win32
;
605 PacketCloseAdapter(p
->adapter
);
606 if (p
->buffer
!= NULL
)
609 PacketFreePacket(p
->Packet
);
611 * Get rid of any link-layer type list we allocated.
613 if (p
->dlt_list
!= NULL
)
621 pcap_setfilter_win32_npf(pcap_t
*p
, struct bpf_program
*fp
)
623 if(PacketSetBpf(p
->adapter
,fp
)==FALSE
){
624 /* kernel filter not installed. */
625 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "Driver error: cannot set bpf filter: %s", pcap_win32strerror());
632 * We filter at user level, since the kernel driver does't process the packets
635 pcap_setfilter_win32_dag(pcap_t
*p
, struct bpf_program
*fp
) {
639 strncpy(p
->errbuf
, "setfilter: No filter specified", sizeof(p
->errbuf
));
643 /* Install a user level filter */
644 if (install_bpf_program(p
, fp
) < 0)
646 snprintf(p
->errbuf
, sizeof(p
->errbuf
),
647 "setfilter, unable to install the filter: %s", pcap_strerror(errno
));
657 pcap_getnonblock_win32(pcap_t
*p
, char *errbuf
)
660 * XXX - if there were a PacketGetReadTimeout() call, we
661 * would use it, and return 1 if the timeout is -1
664 return (p
->nonblock
);
668 pcap_setnonblock_win32(pcap_t
*p
, int nonblock
, char *errbuf
)
674 * Set the read timeout to -1 for non-blocking mode.
679 * Restore the timeout set when the device was opened.
680 * (Note that this may be -1, in which case we're not
681 * really leaving non-blocking mode.)
683 newtimeout
= p
->timeout
;
685 if (!PacketSetReadTimeout(p
->adapter
, newtimeout
)) {
686 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
687 "PacketSetReadTimeout: %s", pcap_win32strerror());
690 p
->nonblock
= (newtimeout
== -1);
694 /* Set the driver working mode */
696 pcap_setmode(pcap_t
*p
, int mode
){
698 if (p
->adapter
==NULL
)
700 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "impossible to set mode while reading from a file");
704 if(PacketSetMode(p
->adapter
,mode
)==FALSE
)
706 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "driver error: working mode not recognized");
713 /* Set the dimension of the kernel-level capture buffer */
715 pcap_setbuff(pcap_t
*p
, int dim
)
717 if (p
->adapter
==NULL
)
719 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "The kernel buffer size cannot be set while reading from a file");
723 if(PacketSetBuff(p
->adapter
,dim
)==FALSE
)
725 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "driver error: not enough memory to allocate the kernel buffer");
731 /*set the minimum amount of data that will release a read call*/
733 pcap_setmintocopy(pcap_t
*p
, int size
)
735 if (p
->adapter
==NULL
)
737 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "Impossible to set the mintocopy parameter on an offline capture");
741 if(PacketSetMinToCopy(p
->adapter
, size
)==FALSE
)
743 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "driver error: unable to set the requested mintocopy size");