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