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