Deleted Added
full compact
pcap-win32.c (162012) pcap-win32.c (172677)
1/*
1/*
2 * Copyright (c) 1999 - 2003
3 * NetGroup, Politecnico di Torino (Italy)
2 * Copyright (c) 1999 - 2005 NetGroup, Politecnico di Torino (Italy)
3 * Copyright (c) 2005 - 2007 CACE Technologies, Davis (California)
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
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.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
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.
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
18 * permission.
18 *
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.
30 *
31 */
32
33#ifndef lint
34static const char rcsid[] _U_ =
19 *
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.
31 *
32 */
33
34#ifndef lint
35static const char rcsid[] _U_ =
35 "@(#) $Header: /tcpdump/master/libpcap/pcap-win32.c,v 1.25.2.3 2005/07/10 17:52:54 risso Exp $ (LBL)";
36 "@(#) $Header: /tcpdump/master/libpcap/pcap-win32.c,v 1.25.2.7 2007/06/14 22:07:14 gianluca Exp $ (LBL)";
36#endif
37
38#include <pcap-int.h>
37#endif
38
39#include <pcap-int.h>
39#include <packet32.h>
40#include <Packet32.h>
40#include <Ntddndis.h>
41#ifdef HAVE_DAG_API
42#include <dagnew.h>
43#include <dagapi.h>
44#endif /* HAVE_DAG_API */
45#ifdef __MINGW32__
46int* _errno();
47#define errno (*_errno())
48#endif /* __MINGW32__ */
49
50static int pcap_setfilter_win32_npf(pcap_t *, struct bpf_program *);
51static int pcap_setfilter_win32_dag(pcap_t *, struct bpf_program *);
52static int pcap_getnonblock_win32(pcap_t *, char *);
53static int pcap_setnonblock_win32(pcap_t *, int, char *);
54
55#define PcapBufSize 256000 /*dimension of the buffer in the pcap_t structure*/
56#define SIZE_BUF 1000000
57
58/* Equivalent to ntohs(), but a lot faster under Windows */
59#define SWAPS(_X) ((_X & 0xff) << 8) | (_X >> 8)
60
61/*
62 * Header that the WinPcap driver associates to the packets.
63 * Once was in bpf.h
64 */
65struct bpf_hdr {
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) */
71};
72
73/* Start winsock */
74int
75wsockinit()
76{
77 WORD wVersionRequested;
78 WSADATA wsaData;
79 int err;
80 wVersionRequested = MAKEWORD( 1, 1);
81 err = WSAStartup( wVersionRequested, &wsaData );
82 if ( err != 0 )
83 {
84 return -1;
85 }
86 return 0;
87}
88
89
90static int
91pcap_stats_win32(pcap_t *p, struct pcap_stat *ps)
92{
93
94 if(PacketGetStats(p->adapter, (struct bpf_stat*)ps) != TRUE){
95 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "PacketGetStats error: %s", pcap_win32strerror());
96 return -1;
97 }
98
99 return 0;
100}
101
102static int
103pcap_read_win32_npf(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
104{
105 int cc;
106 int n = 0;
107 register u_char *bp, *ep;
108
109 cc = p->cc;
110 if (p->cc == 0) {
111 /*
112 * Has "pcap_breakloop()" been called?
113 */
114 if (p->break_loop) {
115 /*
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.
119 */
120 p->break_loop = 0;
121 return (-2);
122 }
123
124 /* capture the packets */
125 if(PacketReceivePacket(p->adapter,p->Packet,TRUE)==FALSE){
126 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "read error: PacketReceivePacket failed");
127 return (-1);
128 }
129
130 cc = p->Packet->ulBytesReceived;
131
132 bp = p->Packet->Buffer;
133 }
134 else
135 bp = p->bp;
136
137 /*
138 * Loop through each packet.
139 */
140#define bhp ((struct bpf_hdr *)bp)
141 ep = bp + cc;
142 while (1) {
143 register int caplen, hdrlen;
144
145 /*
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.
153 */
154 if (p->break_loop) {
155 if (n == 0) {
156 p->break_loop = 0;
157 return (-2);
158 } else {
159 p->bp = bp;
160 p->cc = ep - bp;
161 return (n);
162 }
163 }
164 if (bp >= ep)
165 break;
166
167 caplen = bhp->bh_caplen;
168 hdrlen = bhp->bh_hdrlen;
169
170 /*
171 * XXX A bpf_hdr matches a pcap_pkthdr.
172 */
173 (*callback)(user, (struct pcap_pkthdr*)bp, bp + hdrlen);
174 bp += BPF_WORDALIGN(caplen + hdrlen);
175 if (++n >= cnt && cnt > 0) {
176 p->bp = bp;
177 p->cc = ep - bp;
178 return (n);
179 }
180 }
181#undef bhp
182 p->cc = 0;
183 return (n);
184}
185
186#ifdef HAVE_DAG_API
187static int
188pcap_read_win32_dag(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
189{
190 u_char *dp = NULL;
191 int packet_len = 0, caplen = 0;
192 struct pcap_pkthdr pcap_header;
193 u_char *endofbuf;
194 int n = 0;
195 dag_record_t *header;
196 unsigned erf_record_len;
197 ULONGLONG ts;
198 int cc;
199 unsigned swt;
200 unsigned dfp = p->adapter->DagFastProcess;
201
202 cc = p->cc;
203 if (cc == 0) /* Get new packets only if we have processed all the ones of the previous read */
204 {
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");
208 return (-1);
209 }
210
211 cc = p->Packet->ulBytesReceived;
212 if(cc == 0)
213 /* The timeout has expired but we no packets arrived */
214 return 0;
215 header = (dag_record_t*)p->adapter->DagBuffer;
216 }
217 else
218 header = (dag_record_t*)p->bp;
219
220 endofbuf = (char*)header + cc;
221
222 /*
223 * Cycle through the packets
224 */
225 do
226 {
227 erf_record_len = SWAPS(header->rlen);
228 if((char*)header + erf_record_len > endofbuf)
229 break;
230
231 /* Increase the number of captured packets */
232 p->md.stat.ps_recv++;
233
234 /* Find the beginning of the packet */
235 dp = ((u_char *)header) + dag_record_size;
236
237 /* Determine actual packet len */
238 switch(header->type)
239 {
240 case TYPE_ATM:
241 packet_len = ATM_SNAPLEN;
242 caplen = ATM_SNAPLEN;
243 dp += 4;
244
245 break;
246
247 case TYPE_ETH:
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)
252 {
253 caplen = packet_len;
254 }
255 dp += 2;
256
257 break;
258
259 case TYPE_HDLC_POS:
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)
264 {
265 caplen = packet_len;
266 }
267
268 break;
269 }
270
271 if(caplen > p->snapshot)
272 caplen = p->snapshot;
273
274 /*
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.
282 */
283 if (p->break_loop)
284 {
285 if (n == 0)
286 {
287 p->break_loop = 0;
288 return (-2);
289 }
290 else
291 {
292 p->bp = (char*)header;
293 p->cc = endofbuf - (char*)header;
294 return (n);
295 }
296 }
297
298 if(!dfp)
299 {
300 /* convert between timestamp formats */
301 ts = header->ts;
302 pcap_header.ts.tv_sec = (int)(ts >> 32);
303 ts = (ts & 0xffffffffi64) * 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++;
309 }
310 }
311
312 /* No underlaying filtering system. We need to filter on our own */
313 if (p->fcode.bf_insns)
314 {
315 if (bpf_filter(p->fcode.bf_insns, dp, packet_len, caplen) == 0)
316 {
317 /* Move to next packet */
318 header = (dag_record_t*)((char*)header + erf_record_len);
319 continue;
320 }
321 }
322
323 /* Fill the header for the user suppplied callback function */
324 pcap_header.caplen = caplen;
325 pcap_header.len = packet_len;
326
327 /* Call the callback function */
328 (*callback)(user, &pcap_header, dp);
329
330 /* Move to next packet */
331 header = (dag_record_t*)((char*)header + erf_record_len);
332
333 /* Stop if the number of packets requested by user has been reached*/
334 if (++n >= cnt && cnt > 0)
335 {
336 p->bp = (char*)header;
337 p->cc = endofbuf - (char*)header;
338 return (n);
339 }
340 }
341 while((u_char*)header < endofbuf);
342
343 return 1;
344}
345#endif /* HAVE_DAG_API */
346
347/* Send a packet to the network */
348static int
349pcap_inject_win32(pcap_t *p, const void *buf, size_t size){
350 LPPACKET PacketToSend;
351
352 PacketToSend=PacketAllocatePacket();
353
354 if (PacketToSend == NULL)
355 {
356 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send error: PacketAllocatePacket failed");
357 return -1;
358 }
359
360 PacketInitPacket(PacketToSend,(PVOID)buf,size);
361 if(PacketSendPacket(p->adapter,PacketToSend,TRUE) == FALSE){
362 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send error: PacketSendPacket failed");
363 PacketFreePacket(PacketToSend);
364 return -1;
365 }
366
367 PacketFreePacket(PacketToSend);
368
369 /*
370 * We assume it all got sent if "PacketSendPacket()" succeeded.
371 * "pcap_inject()" is expected to return the number of bytes
372 * sent.
373 */
374 return size;
375}
376
377static void
378pcap_close_win32(pcap_t *p)
379{
380 pcap_close_common(p);
381 if (p->adapter != NULL) {
382 PacketCloseAdapter(p->adapter);
383 p->adapter = NULL;
384 }
385 if (p->Packet) {
386 PacketFreePacket(p->Packet);
387 p->Packet = NULL;
388 }
389}
390
391pcap_t *
392pcap_open_live(const char *device, int snaplen, int promisc, int to_ms,
393 char *ebuf)
394{
395 register pcap_t *p;
396 NetType type;
397
398 /* Init WinSock */
399 wsockinit();
400
401 p = (pcap_t *)malloc(sizeof(*p));
402 if (p == NULL)
403 {
404 snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s", pcap_strerror(errno));
405 return (NULL);
406 }
407 memset(p, 0, sizeof(*p));
408 p->adapter=NULL;
409
410 p->adapter = PacketOpenAdapter((char*)device);
411
412 if (p->adapter == NULL)
413 {
414 free(p);
415 /* Adapter detected but we are not able to open it. Return failure. */
416 snprintf(ebuf, PCAP_ERRBUF_SIZE, "Error opening adapter: %s", pcap_win32strerror());
417 return NULL;
418 }
419
420 /*get network type*/
421 if(PacketGetNetType (p->adapter,&type) == FALSE)
422 {
423 snprintf(ebuf, PCAP_ERRBUF_SIZE, "Cannot determine the network type: %s", pcap_win32strerror());
424 goto bad;
425 }
426
427 /*Set the linktype*/
428 switch (type.LinkType)
429 {
430 case NdisMediumWan:
431 p->linktype = DLT_EN10MB;
432 break;
433
434 case NdisMedium802_3:
435 p->linktype = DLT_EN10MB;
436 /*
437 * This is (presumably) a real Ethernet capture; give it a
438 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
439 * that an application can let you choose it, in case you're
440 * capturing DOCSIS traffic that a Cisco Cable Modem
441 * Termination System is putting out onto an Ethernet (it
442 * doesn't put an Ethernet header onto the wire, it puts raw
443 * DOCSIS frames out on the wire inside the low-level
444 * Ethernet framing).
445 */
446 p->dlt_list = (u_int *) malloc(sizeof(u_int) * 2);
447 /*
448 * If that fails, just leave the list empty.
449 */
450 if (p->dlt_list != NULL) {
451 p->dlt_list[0] = DLT_EN10MB;
452 p->dlt_list[1] = DLT_DOCSIS;
453 p->dlt_count = 2;
454 }
455 break;
456
457 case NdisMediumFddi:
458 p->linktype = DLT_FDDI;
459 break;
460
461 case NdisMedium802_5:
462 p->linktype = DLT_IEEE802;
463 break;
464
465 case NdisMediumArcnetRaw:
466 p->linktype = DLT_ARCNET;
467 break;
468
469 case NdisMediumArcnet878_2:
470 p->linktype = DLT_ARCNET;
471 break;
472
473 case NdisMediumAtm:
474 p->linktype = DLT_ATM_RFC1483;
475 break;
476
477 case NdisMediumCHDLC:
478 p->linktype = DLT_CHDLC;
479 break;
480
481 case NdisMediumPPPSerial:
482 p->linktype = DLT_PPP_SERIAL;
483 break;
484
485 case NdisMediumNull:
486 p->linktype = DLT_NULL;
487 break;
488
41#include <Ntddndis.h>
42#ifdef HAVE_DAG_API
43#include <dagnew.h>
44#include <dagapi.h>
45#endif /* HAVE_DAG_API */
46#ifdef __MINGW32__
47int* _errno();
48#define errno (*_errno())
49#endif /* __MINGW32__ */
50
51static int pcap_setfilter_win32_npf(pcap_t *, struct bpf_program *);
52static int pcap_setfilter_win32_dag(pcap_t *, struct bpf_program *);
53static int pcap_getnonblock_win32(pcap_t *, char *);
54static int pcap_setnonblock_win32(pcap_t *, int, char *);
55
56#define PcapBufSize 256000 /*dimension of the buffer in the pcap_t structure*/
57#define SIZE_BUF 1000000
58
59/* Equivalent to ntohs(), but a lot faster under Windows */
60#define SWAPS(_X) ((_X & 0xff) << 8) | (_X >> 8)
61
62/*
63 * Header that the WinPcap driver associates to the packets.
64 * Once was in bpf.h
65 */
66struct bpf_hdr {
67 struct timeval bh_tstamp; /* time stamp */
68 bpf_u_int32 bh_caplen; /* length of captured portion */
69 bpf_u_int32 bh_datalen; /* original length of packet */
70 u_short bh_hdrlen; /* length of bpf header (this struct
71 plus alignment padding) */
72};
73
74/* Start winsock */
75int
76wsockinit()
77{
78 WORD wVersionRequested;
79 WSADATA wsaData;
80 int err;
81 wVersionRequested = MAKEWORD( 1, 1);
82 err = WSAStartup( wVersionRequested, &wsaData );
83 if ( err != 0 )
84 {
85 return -1;
86 }
87 return 0;
88}
89
90
91static int
92pcap_stats_win32(pcap_t *p, struct pcap_stat *ps)
93{
94
95 if(PacketGetStats(p->adapter, (struct bpf_stat*)ps) != TRUE){
96 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "PacketGetStats error: %s", pcap_win32strerror());
97 return -1;
98 }
99
100 return 0;
101}
102
103static int
104pcap_read_win32_npf(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
105{
106 int cc;
107 int n = 0;
108 register u_char *bp, *ep;
109
110 cc = p->cc;
111 if (p->cc == 0) {
112 /*
113 * Has "pcap_breakloop()" been called?
114 */
115 if (p->break_loop) {
116 /*
117 * Yes - clear the flag that indicates that it
118 * has, and return -2 to indicate that we were
119 * told to break out of the loop.
120 */
121 p->break_loop = 0;
122 return (-2);
123 }
124
125 /* capture the packets */
126 if(PacketReceivePacket(p->adapter,p->Packet,TRUE)==FALSE){
127 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "read error: PacketReceivePacket failed");
128 return (-1);
129 }
130
131 cc = p->Packet->ulBytesReceived;
132
133 bp = p->Packet->Buffer;
134 }
135 else
136 bp = p->bp;
137
138 /*
139 * Loop through each packet.
140 */
141#define bhp ((struct bpf_hdr *)bp)
142 ep = bp + cc;
143 while (1) {
144 register int caplen, hdrlen;
145
146 /*
147 * Has "pcap_breakloop()" been called?
148 * If so, return immediately - if we haven't read any
149 * packets, clear the flag and return -2 to indicate
150 * that we were told to break out of the loop, otherwise
151 * leave the flag set, so that the *next* call will break
152 * out of the loop without having read any packets, and
153 * return the number of packets we've processed so far.
154 */
155 if (p->break_loop) {
156 if (n == 0) {
157 p->break_loop = 0;
158 return (-2);
159 } else {
160 p->bp = bp;
161 p->cc = ep - bp;
162 return (n);
163 }
164 }
165 if (bp >= ep)
166 break;
167
168 caplen = bhp->bh_caplen;
169 hdrlen = bhp->bh_hdrlen;
170
171 /*
172 * XXX A bpf_hdr matches a pcap_pkthdr.
173 */
174 (*callback)(user, (struct pcap_pkthdr*)bp, bp + hdrlen);
175 bp += BPF_WORDALIGN(caplen + hdrlen);
176 if (++n >= cnt && cnt > 0) {
177 p->bp = bp;
178 p->cc = ep - bp;
179 return (n);
180 }
181 }
182#undef bhp
183 p->cc = 0;
184 return (n);
185}
186
187#ifdef HAVE_DAG_API
188static int
189pcap_read_win32_dag(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
190{
191 u_char *dp = NULL;
192 int packet_len = 0, caplen = 0;
193 struct pcap_pkthdr pcap_header;
194 u_char *endofbuf;
195 int n = 0;
196 dag_record_t *header;
197 unsigned erf_record_len;
198 ULONGLONG ts;
199 int cc;
200 unsigned swt;
201 unsigned dfp = p->adapter->DagFastProcess;
202
203 cc = p->cc;
204 if (cc == 0) /* Get new packets only if we have processed all the ones of the previous read */
205 {
206 /* Get new packets from the network */
207 if(PacketReceivePacket(p->adapter, p->Packet, TRUE)==FALSE){
208 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "read error: PacketReceivePacket failed");
209 return (-1);
210 }
211
212 cc = p->Packet->ulBytesReceived;
213 if(cc == 0)
214 /* The timeout has expired but we no packets arrived */
215 return 0;
216 header = (dag_record_t*)p->adapter->DagBuffer;
217 }
218 else
219 header = (dag_record_t*)p->bp;
220
221 endofbuf = (char*)header + cc;
222
223 /*
224 * Cycle through the packets
225 */
226 do
227 {
228 erf_record_len = SWAPS(header->rlen);
229 if((char*)header + erf_record_len > endofbuf)
230 break;
231
232 /* Increase the number of captured packets */
233 p->md.stat.ps_recv++;
234
235 /* Find the beginning of the packet */
236 dp = ((u_char *)header) + dag_record_size;
237
238 /* Determine actual packet len */
239 switch(header->type)
240 {
241 case TYPE_ATM:
242 packet_len = ATM_SNAPLEN;
243 caplen = ATM_SNAPLEN;
244 dp += 4;
245
246 break;
247
248 case TYPE_ETH:
249 swt = SWAPS(header->wlen);
250 packet_len = swt - (p->md.dag_fcs_bits);
251 caplen = erf_record_len - dag_record_size - 2;
252 if (caplen > packet_len)
253 {
254 caplen = packet_len;
255 }
256 dp += 2;
257
258 break;
259
260 case TYPE_HDLC_POS:
261 swt = SWAPS(header->wlen);
262 packet_len = swt - (p->md.dag_fcs_bits);
263 caplen = erf_record_len - dag_record_size;
264 if (caplen > packet_len)
265 {
266 caplen = packet_len;
267 }
268
269 break;
270 }
271
272 if(caplen > p->snapshot)
273 caplen = p->snapshot;
274
275 /*
276 * Has "pcap_breakloop()" been called?
277 * If so, return immediately - if we haven't read any
278 * packets, clear the flag and return -2 to indicate
279 * that we were told to break out of the loop, otherwise
280 * leave the flag set, so that the *next* call will break
281 * out of the loop without having read any packets, and
282 * return the number of packets we've processed so far.
283 */
284 if (p->break_loop)
285 {
286 if (n == 0)
287 {
288 p->break_loop = 0;
289 return (-2);
290 }
291 else
292 {
293 p->bp = (char*)header;
294 p->cc = endofbuf - (char*)header;
295 return (n);
296 }
297 }
298
299 if(!dfp)
300 {
301 /* convert between timestamp formats */
302 ts = header->ts;
303 pcap_header.ts.tv_sec = (int)(ts >> 32);
304 ts = (ts & 0xffffffffi64) * 1000000;
305 ts += 0x80000000; /* rounding */
306 pcap_header.ts.tv_usec = (int)(ts >> 32);
307 if (pcap_header.ts.tv_usec >= 1000000) {
308 pcap_header.ts.tv_usec -= 1000000;
309 pcap_header.ts.tv_sec++;
310 }
311 }
312
313 /* No underlaying filtering system. We need to filter on our own */
314 if (p->fcode.bf_insns)
315 {
316 if (bpf_filter(p->fcode.bf_insns, dp, packet_len, caplen) == 0)
317 {
318 /* Move to next packet */
319 header = (dag_record_t*)((char*)header + erf_record_len);
320 continue;
321 }
322 }
323
324 /* Fill the header for the user suppplied callback function */
325 pcap_header.caplen = caplen;
326 pcap_header.len = packet_len;
327
328 /* Call the callback function */
329 (*callback)(user, &pcap_header, dp);
330
331 /* Move to next packet */
332 header = (dag_record_t*)((char*)header + erf_record_len);
333
334 /* Stop if the number of packets requested by user has been reached*/
335 if (++n >= cnt && cnt > 0)
336 {
337 p->bp = (char*)header;
338 p->cc = endofbuf - (char*)header;
339 return (n);
340 }
341 }
342 while((u_char*)header < endofbuf);
343
344 return 1;
345}
346#endif /* HAVE_DAG_API */
347
348/* Send a packet to the network */
349static int
350pcap_inject_win32(pcap_t *p, const void *buf, size_t size){
351 LPPACKET PacketToSend;
352
353 PacketToSend=PacketAllocatePacket();
354
355 if (PacketToSend == NULL)
356 {
357 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send error: PacketAllocatePacket failed");
358 return -1;
359 }
360
361 PacketInitPacket(PacketToSend,(PVOID)buf,size);
362 if(PacketSendPacket(p->adapter,PacketToSend,TRUE) == FALSE){
363 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send error: PacketSendPacket failed");
364 PacketFreePacket(PacketToSend);
365 return -1;
366 }
367
368 PacketFreePacket(PacketToSend);
369
370 /*
371 * We assume it all got sent if "PacketSendPacket()" succeeded.
372 * "pcap_inject()" is expected to return the number of bytes
373 * sent.
374 */
375 return size;
376}
377
378static void
379pcap_close_win32(pcap_t *p)
380{
381 pcap_close_common(p);
382 if (p->adapter != NULL) {
383 PacketCloseAdapter(p->adapter);
384 p->adapter = NULL;
385 }
386 if (p->Packet) {
387 PacketFreePacket(p->Packet);
388 p->Packet = NULL;
389 }
390}
391
392pcap_t *
393pcap_open_live(const char *device, int snaplen, int promisc, int to_ms,
394 char *ebuf)
395{
396 register pcap_t *p;
397 NetType type;
398
399 /* Init WinSock */
400 wsockinit();
401
402 p = (pcap_t *)malloc(sizeof(*p));
403 if (p == NULL)
404 {
405 snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s", pcap_strerror(errno));
406 return (NULL);
407 }
408 memset(p, 0, sizeof(*p));
409 p->adapter=NULL;
410
411 p->adapter = PacketOpenAdapter((char*)device);
412
413 if (p->adapter == NULL)
414 {
415 free(p);
416 /* Adapter detected but we are not able to open it. Return failure. */
417 snprintf(ebuf, PCAP_ERRBUF_SIZE, "Error opening adapter: %s", pcap_win32strerror());
418 return NULL;
419 }
420
421 /*get network type*/
422 if(PacketGetNetType (p->adapter,&type) == FALSE)
423 {
424 snprintf(ebuf, PCAP_ERRBUF_SIZE, "Cannot determine the network type: %s", pcap_win32strerror());
425 goto bad;
426 }
427
428 /*Set the linktype*/
429 switch (type.LinkType)
430 {
431 case NdisMediumWan:
432 p->linktype = DLT_EN10MB;
433 break;
434
435 case NdisMedium802_3:
436 p->linktype = DLT_EN10MB;
437 /*
438 * This is (presumably) a real Ethernet capture; give it a
439 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
440 * that an application can let you choose it, in case you're
441 * capturing DOCSIS traffic that a Cisco Cable Modem
442 * Termination System is putting out onto an Ethernet (it
443 * doesn't put an Ethernet header onto the wire, it puts raw
444 * DOCSIS frames out on the wire inside the low-level
445 * Ethernet framing).
446 */
447 p->dlt_list = (u_int *) malloc(sizeof(u_int) * 2);
448 /*
449 * If that fails, just leave the list empty.
450 */
451 if (p->dlt_list != NULL) {
452 p->dlt_list[0] = DLT_EN10MB;
453 p->dlt_list[1] = DLT_DOCSIS;
454 p->dlt_count = 2;
455 }
456 break;
457
458 case NdisMediumFddi:
459 p->linktype = DLT_FDDI;
460 break;
461
462 case NdisMedium802_5:
463 p->linktype = DLT_IEEE802;
464 break;
465
466 case NdisMediumArcnetRaw:
467 p->linktype = DLT_ARCNET;
468 break;
469
470 case NdisMediumArcnet878_2:
471 p->linktype = DLT_ARCNET;
472 break;
473
474 case NdisMediumAtm:
475 p->linktype = DLT_ATM_RFC1483;
476 break;
477
478 case NdisMediumCHDLC:
479 p->linktype = DLT_CHDLC;
480 break;
481
482 case NdisMediumPPPSerial:
483 p->linktype = DLT_PPP_SERIAL;
484 break;
485
486 case NdisMediumNull:
487 p->linktype = DLT_NULL;
488 break;
489
490 case NdisMediumBare80211:
491 p->linktype = DLT_IEEE802_11;
492 break;
493
494 case NdisMediumRadio80211:
495 p->linktype = DLT_IEEE802_11_RADIO;
496 break;
497
498 case NdisMediumPpi:
499 p->linktype = DLT_PPI;
500 break;
501
489 default:
490 p->linktype = DLT_EN10MB; /*an unknown adapter is assumed to be ethernet*/
491 break;
492 }
493
502 default:
503 p->linktype = DLT_EN10MB; /*an unknown adapter is assumed to be ethernet*/
504 break;
505 }
506
494 /* Set promisquous mode */
495 if (promisc) PacketSetHwFilter(p->adapter,NDIS_PACKET_TYPE_PROMISCUOUS);
496 else PacketSetHwFilter(p->adapter,NDIS_PACKET_TYPE_ALL_LOCAL);
507 /* Set promiscuous mode */
508 if (promisc)
509 {
497
510
511 if (PacketSetHwFilter(p->adapter,NDIS_PACKET_TYPE_PROMISCUOUS) == FALSE)
512 {
513 snprintf(ebuf, PCAP_ERRBUF_SIZE, "failed to set hardware filter to promiscuous mode");
514 goto bad;
515 }
516 }
517 else
518 {
519 if (PacketSetHwFilter(p->adapter,NDIS_PACKET_TYPE_ALL_LOCAL) == FALSE)
520 {
521 snprintf(ebuf, PCAP_ERRBUF_SIZE, "failed to set hardware filter to non-promiscuous mode");
522 goto bad;
523 }
524 }
525
498 /* Set the buffer size */
499 p->bufsize = PcapBufSize;
500
501 /* Store the timeout. Used by pcap_setnonblock() */
502 p->timeout= to_ms;
503
504 /* allocate Packet structure used during the capture */
505 if((p->Packet = PacketAllocatePacket())==NULL)
506 {
507 snprintf(ebuf, PCAP_ERRBUF_SIZE, "failed to allocate the PACKET structure");
508 goto bad;
509 }
510
511 if(!(p->adapter->Flags & INFO_FLAG_DAG_CARD))
512 {
513 /*
514 * Traditional Adapter
515 */
516
517 p->buffer = (u_char *)malloc(PcapBufSize);
518 if (p->buffer == NULL)
519 {
520 snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s", pcap_strerror(errno));
521 goto bad;
522 }
523
524 PacketInitPacket(p->Packet,(BYTE*)p->buffer,p->bufsize);
525
526 p->snapshot = snaplen;
527
528 /* allocate the standard buffer in the driver */
529 if(PacketSetBuff( p->adapter, SIZE_BUF)==FALSE)
530 {
531 snprintf(ebuf, PCAP_ERRBUF_SIZE,"driver error: not enough memory to allocate the kernel buffer\n");
532 goto bad;
533 }
534
535 /* tell the driver to copy the buffer only if it contains at least 16K */
536 if(PacketSetMinToCopy(p->adapter,16000)==FALSE)
537 {
538 snprintf(ebuf, PCAP_ERRBUF_SIZE,"Error calling PacketSetMinToCopy: %s\n", pcap_win32strerror());
539 goto bad;
540 }
541 }
542 else
543#ifdef HAVE_DAG_API
544 {
545 /*
546 * Dag Card
547 */
548 LONG status;
549 HKEY dagkey;
550 DWORD lptype;
551 DWORD lpcbdata;
552 int postype = 0;
553 char keyname[512];
554
555 snprintf(keyname, sizeof(keyname), "%s\\CardParams\\%s",
556 "SYSTEM\\CurrentControlSet\\Services\\DAG",
557 strstr(_strlwr((char*)device), "dag"));
558 do
559 {
560 status = RegOpenKeyEx(HKEY_LOCAL_MACHINE, keyname, 0, KEY_READ, &dagkey);
561 if(status != ERROR_SUCCESS)
562 break;
563
564 status = RegQueryValueEx(dagkey,
565 "PosType",
566 NULL,
567 &lptype,
568 (char*)&postype,
569 &lpcbdata);
570
571 if(status != ERROR_SUCCESS)
572 {
573 postype = 0;
574 }
575
576 RegCloseKey(dagkey);
577 }
578 while(FALSE);
579
580
581 p->snapshot = PacketSetSnapLen(p->adapter, snaplen);
582
583 /* Set the length of the FCS associated to any packet. This value
584 * will be subtracted to the packet length */
585 p->md.dag_fcs_bits = p->adapter->DagFcsLen;
586 }
587#else
588 goto bad;
589#endif /* HAVE_DAG_API */
590
591 PacketSetReadTimeout(p->adapter, to_ms);
592
593#ifdef HAVE_DAG_API
594 if(p->adapter->Flags & INFO_FLAG_DAG_CARD)
595 {
596 /* install dag specific handlers for read and setfilter */
597 p->read_op = pcap_read_win32_dag;
598 p->setfilter_op = pcap_setfilter_win32_dag;
599 }
600 else
601 {
602#endif /* HAVE_DAG_API */
603 /* install traditional npf handlers for read and setfilter */
604 p->read_op = pcap_read_win32_npf;
605 p->setfilter_op = pcap_setfilter_win32_npf;
606#ifdef HAVE_DAG_API
607 }
608#endif /* HAVE_DAG_API */
609 p->setdirection_op = NULL; /* Not implemented. */
610 /* XXX - can this be implemented on some versions of Windows? */
611 p->inject_op = pcap_inject_win32;
612 p->set_datalink_op = NULL; /* can't change data link type */
613 p->getnonblock_op = pcap_getnonblock_win32;
614 p->setnonblock_op = pcap_setnonblock_win32;
615 p->stats_op = pcap_stats_win32;
616 p->close_op = pcap_close_win32;
617
618 return (p);
619bad:
620 if (p->adapter)
621 PacketCloseAdapter(p->adapter);
622 if (p->buffer != NULL)
623 free(p->buffer);
624 if(p->Packet)
625 PacketFreePacket(p->Packet);
626 /*
627 * Get rid of any link-layer type list we allocated.
628 */
629 if (p->dlt_list != NULL)
630 free(p->dlt_list);
631 free(p);
632 return (NULL);
633}
634
635
636static int
637pcap_setfilter_win32_npf(pcap_t *p, struct bpf_program *fp)
638{
639 if(PacketSetBpf(p->adapter,fp)==FALSE){
640 /*
641 * Kernel filter not installed.
642 * XXX - fall back on userland filtering, as is done
643 * on other platforms?
644 */
645 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "Driver error: cannot set bpf filter: %s", pcap_win32strerror());
646 return (-1);
647 }
648
649 /*
650 * Discard any previously-received packets, as they might have
651 * passed whatever filter was formerly in effect, but might
652 * not pass this filter (BIOCSETF discards packets buffered
653 * in the kernel, so you can lose packets in any case).
654 */
655 p->cc = 0;
656 return (0);
657}
658
659/*
660 * We filter at user level, since the kernel driver does't process the packets
661 */
662static int
663pcap_setfilter_win32_dag(pcap_t *p, struct bpf_program *fp) {
664
665 if(!fp)
666 {
667 strncpy(p->errbuf, "setfilter: No filter specified", sizeof(p->errbuf));
668 return -1;
669 }
670
671 /* Install a user level filter */
672 if (install_bpf_program(p, fp) < 0)
673 {
674 snprintf(p->errbuf, sizeof(p->errbuf),
675 "setfilter, unable to install the filter: %s", pcap_strerror(errno));
676 return -1;
677 }
678
679 p->md.use_bpf = 0;
680
681 return (0);
682}
683
684static int
685pcap_getnonblock_win32(pcap_t *p, char *errbuf)
686{
687 /*
688 * XXX - if there were a PacketGetReadTimeout() call, we
689 * would use it, and return 1 if the timeout is -1
690 * and 0 otherwise.
691 */
692 return (p->nonblock);
693}
694
695static int
696pcap_setnonblock_win32(pcap_t *p, int nonblock, char *errbuf)
697{
698 int newtimeout;
699
700 if (nonblock) {
701 /*
702 * Set the read timeout to -1 for non-blocking mode.
703 */
704 newtimeout = -1;
705 } else {
706 /*
707 * Restore the timeout set when the device was opened.
708 * (Note that this may be -1, in which case we're not
709 * really leaving non-blocking mode.)
710 */
711 newtimeout = p->timeout;
712 }
713 if (!PacketSetReadTimeout(p->adapter, newtimeout)) {
714 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
715 "PacketSetReadTimeout: %s", pcap_win32strerror());
716 return (-1);
717 }
718 p->nonblock = (newtimeout == -1);
719 return (0);
720}
721
722/* Set the driver working mode */
723int
724pcap_setmode(pcap_t *p, int mode){
725
726 if (p->adapter==NULL)
727 {
728 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "impossible to set mode while reading from a file");
729 return -1;
730 }
731
732 if(PacketSetMode(p->adapter,mode)==FALSE)
733 {
734 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "driver error: working mode not recognized");
735 return -1;
736 }
737
738 return 0;
739}
740
741/* Set the dimension of the kernel-level capture buffer */
742int
743pcap_setbuff(pcap_t *p, int dim)
744{
745 if (p->adapter==NULL)
746 {
747 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "The kernel buffer size cannot be set while reading from a file");
748 return -1;
749 }
750
751 if(PacketSetBuff(p->adapter,dim)==FALSE)
752 {
753 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "driver error: not enough memory to allocate the kernel buffer");
754 return -1;
755 }
756 return 0;
757}
758
759/*set the minimum amount of data that will release a read call*/
760int
761pcap_setmintocopy(pcap_t *p, int size)
762{
763 if (p->adapter==NULL)
764 {
765 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "Impossible to set the mintocopy parameter on an offline capture");
766 return -1;
767 }
768
769 if(PacketSetMinToCopy(p->adapter, size)==FALSE)
770 {
771 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "driver error: unable to set the requested mintocopy size");
772 return -1;
773 }
774 return 0;
775}
526 /* Set the buffer size */
527 p->bufsize = PcapBufSize;
528
529 /* Store the timeout. Used by pcap_setnonblock() */
530 p->timeout= to_ms;
531
532 /* allocate Packet structure used during the capture */
533 if((p->Packet = PacketAllocatePacket())==NULL)
534 {
535 snprintf(ebuf, PCAP_ERRBUF_SIZE, "failed to allocate the PACKET structure");
536 goto bad;
537 }
538
539 if(!(p->adapter->Flags & INFO_FLAG_DAG_CARD))
540 {
541 /*
542 * Traditional Adapter
543 */
544
545 p->buffer = (u_char *)malloc(PcapBufSize);
546 if (p->buffer == NULL)
547 {
548 snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s", pcap_strerror(errno));
549 goto bad;
550 }
551
552 PacketInitPacket(p->Packet,(BYTE*)p->buffer,p->bufsize);
553
554 p->snapshot = snaplen;
555
556 /* allocate the standard buffer in the driver */
557 if(PacketSetBuff( p->adapter, SIZE_BUF)==FALSE)
558 {
559 snprintf(ebuf, PCAP_ERRBUF_SIZE,"driver error: not enough memory to allocate the kernel buffer\n");
560 goto bad;
561 }
562
563 /* tell the driver to copy the buffer only if it contains at least 16K */
564 if(PacketSetMinToCopy(p->adapter,16000)==FALSE)
565 {
566 snprintf(ebuf, PCAP_ERRBUF_SIZE,"Error calling PacketSetMinToCopy: %s\n", pcap_win32strerror());
567 goto bad;
568 }
569 }
570 else
571#ifdef HAVE_DAG_API
572 {
573 /*
574 * Dag Card
575 */
576 LONG status;
577 HKEY dagkey;
578 DWORD lptype;
579 DWORD lpcbdata;
580 int postype = 0;
581 char keyname[512];
582
583 snprintf(keyname, sizeof(keyname), "%s\\CardParams\\%s",
584 "SYSTEM\\CurrentControlSet\\Services\\DAG",
585 strstr(_strlwr((char*)device), "dag"));
586 do
587 {
588 status = RegOpenKeyEx(HKEY_LOCAL_MACHINE, keyname, 0, KEY_READ, &dagkey);
589 if(status != ERROR_SUCCESS)
590 break;
591
592 status = RegQueryValueEx(dagkey,
593 "PosType",
594 NULL,
595 &lptype,
596 (char*)&postype,
597 &lpcbdata);
598
599 if(status != ERROR_SUCCESS)
600 {
601 postype = 0;
602 }
603
604 RegCloseKey(dagkey);
605 }
606 while(FALSE);
607
608
609 p->snapshot = PacketSetSnapLen(p->adapter, snaplen);
610
611 /* Set the length of the FCS associated to any packet. This value
612 * will be subtracted to the packet length */
613 p->md.dag_fcs_bits = p->adapter->DagFcsLen;
614 }
615#else
616 goto bad;
617#endif /* HAVE_DAG_API */
618
619 PacketSetReadTimeout(p->adapter, to_ms);
620
621#ifdef HAVE_DAG_API
622 if(p->adapter->Flags & INFO_FLAG_DAG_CARD)
623 {
624 /* install dag specific handlers for read and setfilter */
625 p->read_op = pcap_read_win32_dag;
626 p->setfilter_op = pcap_setfilter_win32_dag;
627 }
628 else
629 {
630#endif /* HAVE_DAG_API */
631 /* install traditional npf handlers for read and setfilter */
632 p->read_op = pcap_read_win32_npf;
633 p->setfilter_op = pcap_setfilter_win32_npf;
634#ifdef HAVE_DAG_API
635 }
636#endif /* HAVE_DAG_API */
637 p->setdirection_op = NULL; /* Not implemented. */
638 /* XXX - can this be implemented on some versions of Windows? */
639 p->inject_op = pcap_inject_win32;
640 p->set_datalink_op = NULL; /* can't change data link type */
641 p->getnonblock_op = pcap_getnonblock_win32;
642 p->setnonblock_op = pcap_setnonblock_win32;
643 p->stats_op = pcap_stats_win32;
644 p->close_op = pcap_close_win32;
645
646 return (p);
647bad:
648 if (p->adapter)
649 PacketCloseAdapter(p->adapter);
650 if (p->buffer != NULL)
651 free(p->buffer);
652 if(p->Packet)
653 PacketFreePacket(p->Packet);
654 /*
655 * Get rid of any link-layer type list we allocated.
656 */
657 if (p->dlt_list != NULL)
658 free(p->dlt_list);
659 free(p);
660 return (NULL);
661}
662
663
664static int
665pcap_setfilter_win32_npf(pcap_t *p, struct bpf_program *fp)
666{
667 if(PacketSetBpf(p->adapter,fp)==FALSE){
668 /*
669 * Kernel filter not installed.
670 * XXX - fall back on userland filtering, as is done
671 * on other platforms?
672 */
673 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "Driver error: cannot set bpf filter: %s", pcap_win32strerror());
674 return (-1);
675 }
676
677 /*
678 * Discard any previously-received packets, as they might have
679 * passed whatever filter was formerly in effect, but might
680 * not pass this filter (BIOCSETF discards packets buffered
681 * in the kernel, so you can lose packets in any case).
682 */
683 p->cc = 0;
684 return (0);
685}
686
687/*
688 * We filter at user level, since the kernel driver does't process the packets
689 */
690static int
691pcap_setfilter_win32_dag(pcap_t *p, struct bpf_program *fp) {
692
693 if(!fp)
694 {
695 strncpy(p->errbuf, "setfilter: No filter specified", sizeof(p->errbuf));
696 return -1;
697 }
698
699 /* Install a user level filter */
700 if (install_bpf_program(p, fp) < 0)
701 {
702 snprintf(p->errbuf, sizeof(p->errbuf),
703 "setfilter, unable to install the filter: %s", pcap_strerror(errno));
704 return -1;
705 }
706
707 p->md.use_bpf = 0;
708
709 return (0);
710}
711
712static int
713pcap_getnonblock_win32(pcap_t *p, char *errbuf)
714{
715 /*
716 * XXX - if there were a PacketGetReadTimeout() call, we
717 * would use it, and return 1 if the timeout is -1
718 * and 0 otherwise.
719 */
720 return (p->nonblock);
721}
722
723static int
724pcap_setnonblock_win32(pcap_t *p, int nonblock, char *errbuf)
725{
726 int newtimeout;
727
728 if (nonblock) {
729 /*
730 * Set the read timeout to -1 for non-blocking mode.
731 */
732 newtimeout = -1;
733 } else {
734 /*
735 * Restore the timeout set when the device was opened.
736 * (Note that this may be -1, in which case we're not
737 * really leaving non-blocking mode.)
738 */
739 newtimeout = p->timeout;
740 }
741 if (!PacketSetReadTimeout(p->adapter, newtimeout)) {
742 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
743 "PacketSetReadTimeout: %s", pcap_win32strerror());
744 return (-1);
745 }
746 p->nonblock = (newtimeout == -1);
747 return (0);
748}
749
750/* Set the driver working mode */
751int
752pcap_setmode(pcap_t *p, int mode){
753
754 if (p->adapter==NULL)
755 {
756 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "impossible to set mode while reading from a file");
757 return -1;
758 }
759
760 if(PacketSetMode(p->adapter,mode)==FALSE)
761 {
762 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "driver error: working mode not recognized");
763 return -1;
764 }
765
766 return 0;
767}
768
769/* Set the dimension of the kernel-level capture buffer */
770int
771pcap_setbuff(pcap_t *p, int dim)
772{
773 if (p->adapter==NULL)
774 {
775 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "The kernel buffer size cannot be set while reading from a file");
776 return -1;
777 }
778
779 if(PacketSetBuff(p->adapter,dim)==FALSE)
780 {
781 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "driver error: not enough memory to allocate the kernel buffer");
782 return -1;
783 }
784 return 0;
785}
786
787/*set the minimum amount of data that will release a read call*/
788int
789pcap_setmintocopy(pcap_t *p, int size)
790{
791 if (p->adapter==NULL)
792 {
793 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "Impossible to set the mintocopy parameter on an offline capture");
794 return -1;
795 }
796
797 if(PacketSetMinToCopy(p->adapter, size)==FALSE)
798 {
799 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "driver error: unable to set the requested mintocopy size");
800 return -1;
801 }
802 return 0;
803}