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