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