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.19 2003-12-18 23:32:33 guy Exp $ (LBL)";
43 #define errno (*_errno())
44 #endif /* __MINGW32__ */
46 static int pcap_setfilter_win32(pcap_t
*, struct bpf_program
*);
47 static int pcap_getnonblock_win32(pcap_t
*, char *);
48 static int pcap_setnonblock_win32(pcap_t
*, int, char *);
50 #define PcapBufSize 256000 /*dimension of the buffer in the pcap_t structure*/
51 #define SIZE_BUF 1000000
54 * Header that the WinPcap driver associates to the packets.
58 struct timeval bh_tstamp
; /* time stamp */
59 bpf_u_int32 bh_caplen
; /* length of captured portion */
60 bpf_u_int32 bh_datalen
; /* original length of packet */
61 u_short bh_hdrlen
; /* length of bpf header (this struct
62 plus alignment padding) */
69 WORD wVersionRequested
;
72 wVersionRequested
= MAKEWORD( 1, 1);
73 err
= WSAStartup( wVersionRequested
, &wsaData
);
83 pcap_stats_win32(pcap_t
*p
, struct pcap_stat
*ps
)
86 if(PacketGetStats(p
->adapter
, (struct bpf_stat
*)ps
) != TRUE
){
87 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "PacketGetStats error: %s", pcap_win32strerror());
95 pcap_read_win32(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
99 register u_char
*bp
, *ep
;
104 * Has "pcap_breakloop()" been called?
108 * Yes - clear the flag that indicates that it
109 * has, and return -2 to indicate that we were
110 * told to break out of the loop.
116 /* capture the packets */
117 if(PacketReceivePacket(p
->adapter
,p
->Packet
,TRUE
)==FALSE
){
118 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "read error: PacketReceivePacket failed");
122 cc
= p
->Packet
->ulBytesReceived
;
124 bp
= p
->Packet
->Buffer
;
130 * Loop through each packet.
132 #define bhp ((struct bpf_hdr *)bp)
135 register int caplen
, hdrlen
;
138 * Has "pcap_breakloop()" been called?
139 * If so, return immediately - if we haven't read any
140 * packets, clear the flag and return -2 to indicate
141 * that we were told to break out of the loop, otherwise
142 * leave the flag set, so that the *next* call will break
143 * out of the loop without having read any packets, and
144 * return the number of packets we've processed so far.
159 caplen
= bhp
->bh_caplen
;
160 hdrlen
= bhp
->bh_hdrlen
;
163 * XXX A bpf_hdr matches a pcap_pkthdr.
165 (*callback
)(user
, (struct pcap_pkthdr
*)bp
, bp
+ hdrlen
);
166 bp
+= BPF_WORDALIGN(caplen
+ hdrlen
);
167 if (++n
>= cnt
&& cnt
> 0) {
180 pcap_close_win32(pcap_t
*p
)
182 if (p
->buffer
!= NULL
)
184 if (p
->adapter
!= NULL
) {
185 PacketCloseAdapter(p
->adapter
);
191 pcap_open_live(const char *device
, int snaplen
, int promisc
, int to_ms
,
200 p
= (pcap_t
*)malloc(sizeof(*p
));
203 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc: %s", pcap_strerror(errno
));
206 memset(p
, 0, sizeof(*p
));
209 p
->adapter
= PacketOpenAdapter((char*)device
);
211 if (p
->adapter
== NULL
)
213 /* Adapter detected but we are not able to open it. Return failure. */
214 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "Error opening adapter: %s", pcap_win32strerror());
219 if(PacketGetNetType (p
->adapter
,&type
) == FALSE
)
221 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "Cannot determine the network type: %s", pcap_win32strerror());
226 switch (type
.LinkType
)
229 p
->linktype
= DLT_EN10MB
;
232 case NdisMedium802_3
:
233 p
->linktype
= DLT_EN10MB
;
235 * This is (presumably) a real Ethernet capture; give it a
236 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
237 * that an application can let you choose it, in case you're
238 * capturing DOCSIS traffic that a Cisco Cable Modem
239 * Termination System is putting out onto an Ethernet (it
240 * doesn't put an Ethernet header onto the wire, it puts raw
241 * DOCSIS frames out on the wire inside the low-level
244 p
->dlt_list
= (u_int
*) malloc(sizeof(u_int
) * 2);
246 * If that fails, just leave the list empty.
248 if (p
->dlt_list
!= NULL
) {
249 p
->dlt_list
[0] = DLT_EN10MB
;
250 p
->dlt_list
[1] = DLT_DOCSIS
;
256 p
->linktype
= DLT_FDDI
;
259 case NdisMedium802_5
:
260 p
->linktype
= DLT_IEEE802
;
263 case NdisMediumArcnetRaw
:
264 p
->linktype
= DLT_ARCNET
;
267 case NdisMediumArcnet878_2
:
268 p
->linktype
= DLT_ARCNET
;
272 p
->linktype
= DLT_ATM_RFC1483
;
276 p
->linktype
= DLT_EN10MB
; /*an unknown adapter is assumed to be ethernet*/
280 /* Set promisquous mode */
281 if (promisc
) PacketSetHwFilter(p
->adapter
,NDIS_PACKET_TYPE_PROMISCUOUS
);
282 else PacketSetHwFilter(p
->adapter
,NDIS_PACKET_TYPE_ALL_LOCAL
);
284 /* Set the buffer size */
285 p
->bufsize
= PcapBufSize
;
287 p
->buffer
= (u_char
*)malloc(PcapBufSize
);
288 if (p
->buffer
== NULL
)
290 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc: %s", pcap_strerror(errno
));
294 p
->snapshot
= snaplen
;
296 /* allocate Packet structure used during the capture */
297 if((p
->Packet
= PacketAllocatePacket())==NULL
)
299 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "failed to allocate the PACKET structure");
303 PacketInitPacket(p
->Packet
,(BYTE
*)p
->buffer
,p
->bufsize
);
305 /* allocate the standard buffer in the driver */
306 if(PacketSetBuff( p
->adapter
, SIZE_BUF
)==FALSE
)
308 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,"driver error: not enough memory to allocate the kernel buffer\n");
312 /* tell the driver to copy the buffer only if it contains at least 16K */
313 if(PacketSetMinToCopy(p
->adapter
,16000)==FALSE
)
315 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,"Error calling PacketSetMinToCopy: %s\n", pcap_win32strerror());
319 PacketSetReadTimeout(p
->adapter
, to_ms
);
321 p
->read_op
= pcap_read_win32
;
322 p
->setfilter_op
= pcap_setfilter_win32
;
323 p
->set_datalink_op
= NULL
; /* can't change data link type */
324 p
->getnonblock_op
= pcap_getnonblock_win32
;
325 p
->setnonblock_op
= pcap_setnonblock_win32
;
326 p
->stats_op
= pcap_stats_win32
;
327 p
->close_op
= pcap_close_win32
;
332 PacketCloseAdapter(p
->adapter
);
333 if (p
->buffer
!= NULL
)
336 * Get rid of any link-layer type list we allocated.
338 if (p
->dlt_list
!= NULL
)
346 pcap_setfilter_win32(pcap_t
*p
, struct bpf_program
*fp
)
348 if(PacketSetBpf(p
->adapter
,fp
)==FALSE
){
349 /* kernel filter not installed. */
350 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "Driver error: cannot set bpf filter: %s", pcap_win32strerror());
358 pcap_getnonblock_win32(pcap_t
*p
, char *errbuf
)
361 * XXX - if there were a PacketGetReadTimeout() call, we
362 * would use it, and return 1 if the timeout is -1
365 return (p
->nonblock
);
369 pcap_setnonblock_win32(pcap_t
*p
, int nonblock
, char *errbuf
)
375 * Set the read timeout to -1 for non-blocking mode.
380 * Restore the timeout set when the device was opened.
381 * (Note that this may be -1, in which case we're not
382 * really leaving non-blocking mode.)
384 newtimeout
= p
->timeout
;
386 if (!PacketSetReadTimeout(p
->adapter
, newtimeout
)) {
387 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
388 "PacketSetReadTimeout: %s", pcap_win32strerror());
391 p
->nonblock
= (newtimeout
== -1);
395 /* Set the driver working mode */
397 pcap_setmode(pcap_t
*p
, int mode
){
399 if (p
->adapter
==NULL
)
401 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "impossible to set mode while reading from a file");
405 if(PacketSetMode(p
->adapter
,mode
)==FALSE
)
407 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "driver error: working mode not recognized");
414 /* Send a packet to the network */
416 pcap_sendpacket(pcap_t
*p
, u_char
*buf
, int size
){
417 LPPACKET PacketToSend
;
419 if (p
->adapter
==NULL
)
421 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "Writing a packet is allowed only on a physical adapter");
425 PacketToSend
=PacketAllocatePacket();
426 PacketInitPacket(PacketToSend
,buf
,size
);
427 if(PacketSendPacket(p
->adapter
,PacketToSend
,TRUE
) == FALSE
){
428 PacketFreePacket(PacketToSend
);
432 PacketFreePacket(PacketToSend
);
436 /* Set the dimension of the kernel-level capture buffer */
438 pcap_setbuff(pcap_t
*p
, int dim
)
440 if (p
->adapter
==NULL
)
442 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "The kernel buffer size cannot be set while reading from a file");
446 if(PacketSetBuff(p
->adapter
,dim
)==FALSE
)
448 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "driver error: not enough memory to allocate the kernel buffer");
454 /*set the minimum amount of data that will release a read call*/
456 pcap_setmintocopy(pcap_t
*p
, int size
)
458 if (p
->adapter
==NULL
)
460 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "Impossible to set the mintocopy parameter on an offline capture");
464 if(PacketSetMinToCopy(p
->adapter
, size
)==FALSE
)
466 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "driver error: unable to set the requested mintocopy size");