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
[] =
35 "@(#) $Header: /tcpdump/master/libpcap/pcap-win32.c,v 1.14 2003-09-22 11:48:40 risso Exp $ (LBL)";
43 #define errno (*_errno())
44 #endif /* __MINGW32__ */
46 static int pcap_setfilter_win32(pcap_t
*, struct bpf_program
*);
48 #define PcapBufSize 256000 /*dimension of the buffer in the pcap_t structure*/
49 #define SIZE_BUF 1000000
52 * Header that the WinPcap driver associates to the packets.
56 struct timeval bh_tstamp
; /* time stamp */
57 bpf_u_int32 bh_caplen
; /* length of captured portion */
58 bpf_u_int32 bh_datalen
; /* original length of packet */
59 u_short bh_hdrlen
; /* length of bpf header (this struct
60 plus alignment padding) */
67 WORD wVersionRequested
;
70 wVersionRequested
= MAKEWORD( 1, 1);
71 err
= WSAStartup( wVersionRequested
, &wsaData
);
81 pcap_stats_win32(pcap_t
*p
, struct pcap_stat
*ps
)
84 if(PacketGetStats(p
->adapter
, (struct bpf_stat
*)ps
) != TRUE
){
85 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "PacketGetStats error: %s", pcap_win32strerror());
93 pcap_read_win32(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
97 register u_char
*bp
, *ep
;
102 /* capture the packets */
103 if(PacketReceivePacket(p
->adapter
,p
->Packet
,TRUE
)==FALSE
){
104 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "read error: PacketReceivePacket failed");
108 cc
= p
->Packet
->ulBytesReceived
;
110 bp
= p
->Packet
->Buffer
;
116 * Loop through each packet.
118 #define bhp ((struct bpf_hdr *)bp)
121 register int caplen
, hdrlen
;
122 caplen
= bhp
->bh_caplen
;
123 hdrlen
= bhp
->bh_hdrlen
;
126 * XXX A bpf_hdr matches a pcap_pkthdr.
128 (*callback
)(user
, (struct pcap_pkthdr
*)bp
, bp
+ hdrlen
);
129 bp
+= BPF_WORDALIGN(caplen
+ hdrlen
);
130 if (++n
>= cnt
&& cnt
> 0) {
143 pcap_close_win32(pcap_t
*p
)
145 if (p
->buffer
!= NULL
)
147 if (p
->adapter
!= NULL
) {
148 PacketCloseAdapter(p
->adapter
);
154 pcap_open_live(const char *device
, int snaplen
, int promisc
, int to_ms
,
163 p
= (pcap_t
*)malloc(sizeof(*p
));
166 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc: %s", pcap_strerror(errno
));
169 memset(p
, 0, sizeof(*p
));
172 p
->adapter
= PacketOpenAdapter((char*)device
);
174 if (p
->adapter
== NULL
)
176 /* Adapter detected but we are not able to open it. Return failure. */
177 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "Error opening adapter: %s", pcap_win32strerror());
182 if(PacketGetNetType (p
->adapter
,&type
) == FALSE
)
184 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "Cannot determine the network type: %s", pcap_win32strerror());
189 switch (type
.LinkType
)
192 p
->linktype
= DLT_EN10MB
;
195 case NdisMedium802_3
:
196 p
->linktype
= DLT_EN10MB
;
200 p
->linktype
= DLT_FDDI
;
203 case NdisMedium802_5
:
204 p
->linktype
= DLT_IEEE802
;
207 case NdisMediumArcnetRaw
:
208 p
->linktype
= DLT_ARCNET
;
211 case NdisMediumArcnet878_2
:
212 p
->linktype
= DLT_ARCNET
;
216 p
->linktype
= DLT_ATM_RFC1483
;
220 p
->linktype
= DLT_EN10MB
; /*an unknown adapter is assumed to be ethernet*/
224 /* Set promisquous mode */
225 if (promisc
) PacketSetHwFilter(p
->adapter
,NDIS_PACKET_TYPE_PROMISCUOUS
);
226 else PacketSetHwFilter(p
->adapter
,NDIS_PACKET_TYPE_ALL_LOCAL
);
228 /* Set the buffer size */
229 p
->bufsize
= PcapBufSize
;
231 p
->buffer
= (u_char
*)malloc(PcapBufSize
);
232 if (p
->buffer
== NULL
)
234 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc: %s", pcap_strerror(errno
));
238 p
->snapshot
= snaplen
;
240 /* allocate Packet structure used during the capture */
241 if((p
->Packet
= PacketAllocatePacket())==NULL
)
243 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "failed to allocate the PACKET structure");
247 PacketInitPacket(p
->Packet
,(BYTE
*)p
->buffer
,p
->bufsize
);
249 /* allocate the standard buffer in the driver */
250 if(PacketSetBuff( p
->adapter
, SIZE_BUF
)==FALSE
)
252 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,"driver error: not enough memory to allocate the kernel buffer\n");
256 /* tell the driver to copy the buffer only if it contains at least 16K */
257 if(PacketSetMinToCopy(p
->adapter
,16000)==FALSE
)
259 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,"Error calling PacketSetMinToCopy: %s\n", pcap_win32strerror());
263 PacketSetReadTimeout(p
->adapter
, to_ms
);
265 p
->read_op
= pcap_read_win32
;
266 p
->setfilter_op
= pcap_setfilter_win32
;
267 p
->set_datalink_op
= NULL
; /* can't change data link type */
268 p
->stats_op
= pcap_stats_win32
;
269 p
->close_op
= pcap_close_win32
;
274 PacketCloseAdapter(p
->adapter
);
275 if (p
->buffer
!= NULL
)
283 pcap_setfilter_win32(pcap_t
*p
, struct bpf_program
*fp
)
285 if(PacketSetBpf(p
->adapter
,fp
)==FALSE
){
286 /* kernel filter not installed. */
287 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "Driver error: cannot set bpf filter: %s", pcap_win32strerror());
294 /* Set the driver working mode */
296 pcap_setmode(pcap_t
*p
, int mode
){
298 if (p
->adapter
==NULL
)
300 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "impossible to set mode while reading from a file");
304 if(PacketSetMode(p
->adapter
,mode
)==FALSE
)
306 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "driver error: working mode not recognized");
313 /* Send a packet to the network */
315 pcap_sendpacket(pcap_t
*p
, u_char
*buf
, int size
){
316 LPPACKET PacketToSend
;
318 if (p
->adapter
==NULL
)
320 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "Writing a packet is allowed only on a physical adapter");
324 PacketToSend
=PacketAllocatePacket();
325 PacketInitPacket(PacketToSend
,buf
,size
);
326 if(PacketSendPacket(p
->adapter
,PacketToSend
,TRUE
) == FALSE
){
327 PacketFreePacket(PacketToSend
);
331 PacketFreePacket(PacketToSend
);
335 /* Set the dimension of the kernel-level capture buffer */
337 pcap_setbuff(pcap_t
*p
, int dim
)
339 if (p
->adapter
==NULL
)
341 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "The kernel buffer size cannot be set while reading from a file");
345 if(PacketSetBuff(p
->adapter
,dim
)==FALSE
)
347 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "driver error: not enough memory to allocate the kernel buffer");
353 /*set the minimum amount of data that will release a read call*/
355 pcap_setmintocopy(pcap_t
*p
, int size
)
357 if (p
->adapter
==NULL
)
359 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "Impossible to set the mintocopy parameter on an offline capture");
363 if(PacketSetMinToCopy(p
->adapter
, size
)==FALSE
)
365 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "driver error: unable to set the requested mintocopy size");