Drop main() prototype. Syncs with NetBSD-8
[minix.git] / external / bsd / libpcap / dist / pcap-win32.c
blob097f95385f19b4abf9bd737b160386d5665ba5f3
1 /* $NetBSD: pcap-win32.c,v 1.3 2015/03/31 21:39:42 christos Exp $ */
3 /*
4 * Copyright (c) 1999 - 2005 NetGroup, Politecnico di Torino (Italy)
5 * Copyright (c) 2005 - 2008 CACE Technologies, Davis (California)
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Politecnico di Torino, CACE Technologies
18 * nor the names of its contributors may be used to endorse or promote
19 * products derived from this software without specific prior written
20 * permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include <pcap-int.h>
37 #include <Packet32.h>
38 #ifdef __MINGW32__
39 #ifdef __MINGW64__
40 #include <ntddndis.h>
41 #else /*__MINGW64__*/
42 #include <ddk/ntddndis.h>
43 #include <ddk/ndis.h>
44 #endif /*__MINGW64__*/
45 #else /*__MINGW32__*/
46 #include <ntddndis.h>
47 #endif /*__MINGW32__*/
48 #ifdef HAVE_DAG_API
49 #include <dagnew.h>
50 #include <dagapi.h>
51 #endif /* HAVE_DAG_API */
52 #ifdef __MINGW32__
53 int* _errno();
54 #define errno (*_errno())
55 #endif /* __MINGW32__ */
57 static int pcap_setfilter_win32_npf(pcap_t *, struct bpf_program *);
58 static int pcap_setfilter_win32_dag(pcap_t *, struct bpf_program *);
59 static int pcap_getnonblock_win32(pcap_t *, char *);
60 static int pcap_setnonblock_win32(pcap_t *, int, char *);
62 /*dimension of the buffer in the pcap_t structure*/
63 #define WIN32_DEFAULT_USER_BUFFER_SIZE 256000
65 /*dimension of the buffer in the kernel driver NPF */
66 #define WIN32_DEFAULT_KERNEL_BUFFER_SIZE 1000000
68 /* Equivalent to ntohs(), but a lot faster under Windows */
69 #define SWAPS(_X) ((_X & 0xff) << 8) | (_X >> 8)
72 * Private data for capturing on WinPcap devices.
74 struct pcap_win {
75 int nonblock;
77 int filtering_in_kernel; /* using kernel filter */
79 #ifdef HAVE_DAG_API
80 int dag_fcs_bits; /* Number of checksum bits from link layer */
81 #endif
85 * Header that the WinPcap driver associates to the packets.
86 * Once was in bpf.h
88 struct bpf_hdr {
89 struct timeval bh_tstamp; /* time stamp */
90 bpf_u_int32 bh_caplen; /* length of captured portion */
91 bpf_u_int32 bh_datalen; /* original length of packet */
92 u_short bh_hdrlen; /* length of bpf header (this struct
93 plus alignment padding) */
96 CRITICAL_SECTION g_PcapCompileCriticalSection;
98 BOOL WINAPI DllMain(
99 HANDLE hinstDLL,
100 DWORD dwReason,
101 LPVOID lpvReserved
104 if (dwReason == DLL_PROCESS_ATTACH)
106 InitializeCriticalSection(&g_PcapCompileCriticalSection);
109 return TRUE;
112 /* Start winsock */
114 wsockinit()
116 WORD wVersionRequested;
117 WSADATA wsaData;
118 static int err = -1;
119 static int done = 0;
121 if (done)
122 return err;
124 wVersionRequested = MAKEWORD( 1, 1);
125 err = WSAStartup( wVersionRequested, &wsaData );
126 atexit ((void(*)(void))WSACleanup);
127 InitializeCriticalSection(&g_PcapCompileCriticalSection);
128 done = 1;
130 if ( err != 0 )
131 err = -1;
132 return err;
135 int pcap_wsockinit()
137 return wsockinit();
140 static int
141 pcap_stats_win32(pcap_t *p, struct pcap_stat *ps)
144 if(PacketGetStats(p->adapter, (struct bpf_stat*)ps) != TRUE){
145 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "PacketGetStats error: %s", pcap_win32strerror());
146 return -1;
149 return 0;
152 /* Set the dimension of the kernel-level capture buffer */
153 static int
154 pcap_setbuff_win32(pcap_t *p, int dim)
156 if(PacketSetBuff(p->adapter,dim)==FALSE)
158 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "driver error: not enough memory to allocate the kernel buffer");
159 return -1;
161 return 0;
164 /* Set the driver working mode */
165 static int
166 pcap_setmode_win32(pcap_t *p, int mode)
168 if(PacketSetMode(p->adapter,mode)==FALSE)
170 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "driver error: working mode not recognized");
171 return -1;
174 return 0;
177 /*set the minimum amount of data that will release a read call*/
178 static int
179 pcap_setmintocopy_win32(pcap_t *p, int size)
181 if(PacketSetMinToCopy(p->adapter, size)==FALSE)
183 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "driver error: unable to set the requested mintocopy size");
184 return -1;
186 return 0;
189 /*return the Adapter for a pcap_t*/
190 static Adapter *
191 pcap_getadapter_win32(pcap_t *p)
193 return p->adapter;
196 static int
197 pcap_read_win32_npf(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
199 int cc;
200 int n = 0;
201 register u_char *bp, *ep;
202 u_char *datap;
203 struct pcap_win *pw = p->priv;
205 cc = p->cc;
206 if (p->cc == 0) {
208 * Has "pcap_breakloop()" been called?
210 if (p->break_loop) {
212 * Yes - clear the flag that indicates that it
213 * has, and return PCAP_ERROR_BREAK to indicate
214 * that we were told to break out of the loop.
216 p->break_loop = 0;
217 return (PCAP_ERROR_BREAK);
220 /* capture the packets */
221 if(PacketReceivePacket(p->adapter,p->Packet,TRUE)==FALSE){
222 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "read error: PacketReceivePacket failed");
223 return (PCAP_ERROR);
226 cc = p->Packet->ulBytesReceived;
228 bp = p->Packet->Buffer;
230 else
231 bp = p->bp;
234 * Loop through each packet.
236 #define bhp ((struct bpf_hdr *)bp)
237 ep = bp + cc;
238 while (1) {
239 register int caplen, hdrlen;
242 * Has "pcap_breakloop()" been called?
243 * If so, return immediately - if we haven't read any
244 * packets, clear the flag and return PCAP_ERROR_BREAK
245 * to indicate that we were told to break out of the loop,
246 * otherwise leave the flag set, so that the *next* call
247 * will break out of the loop without having read any
248 * packets, and return the number of packets we've
249 * processed so far.
251 if (p->break_loop) {
252 if (n == 0) {
253 p->break_loop = 0;
254 return (PCAP_ERROR_BREAK);
255 } else {
256 p->bp = bp;
257 p->cc = ep - bp;
258 return (n);
261 if (bp >= ep)
262 break;
264 caplen = bhp->bh_caplen;
265 hdrlen = bhp->bh_hdrlen;
266 datap = bp + hdrlen;
269 * Short-circuit evaluation: if using BPF filter
270 * in kernel, no need to do it now - we already know
271 * the packet passed the filter.
273 * XXX - bpf_filter() should always return TRUE if
274 * handed a null pointer for the program, but it might
275 * just try to "run" the filter, so we check here.
277 if (pw->filtering_in_kernel ||
278 p->fcode.bf_insns == NULL ||
279 bpf_filter(p->fcode.bf_insns, datap, bhp->bh_datalen, caplen)) {
281 * XXX A bpf_hdr matches a pcap_pkthdr.
283 (*callback)(user, (struct pcap_pkthdr*)bp, datap);
284 bp += Packet_WORDALIGN(caplen + hdrlen);
285 if (++n >= cnt && !PACKET_COUNT_IS_UNLIMITED(cnt)) {
286 p->bp = bp;
287 p->cc = ep - bp;
288 return (n);
290 } else {
292 * Skip this packet.
294 bp += Packet_WORDALIGN(caplen + hdrlen);
297 #undef bhp
298 p->cc = 0;
299 return (n);
302 #ifdef HAVE_DAG_API
303 static int
304 pcap_read_win32_dag(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
306 struct pcap_win *pw = p->priv;
307 u_char *dp = NULL;
308 int packet_len = 0, caplen = 0;
309 struct pcap_pkthdr pcap_header;
310 u_char *endofbuf;
311 int n = 0;
312 dag_record_t *header;
313 unsigned erf_record_len;
314 ULONGLONG ts;
315 int cc;
316 unsigned swt;
317 unsigned dfp = p->adapter->DagFastProcess;
319 cc = p->cc;
320 if (cc == 0) /* Get new packets only if we have processed all the ones of the previous read */
322 /* Get new packets from the network */
323 if(PacketReceivePacket(p->adapter, p->Packet, TRUE)==FALSE){
324 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "read error: PacketReceivePacket failed");
325 return (-1);
328 cc = p->Packet->ulBytesReceived;
329 if(cc == 0)
330 /* The timeout has expired but we no packets arrived */
331 return 0;
332 header = (dag_record_t*)p->adapter->DagBuffer;
334 else
335 header = (dag_record_t*)p->bp;
337 endofbuf = (char*)header + cc;
340 * Cycle through the packets
344 erf_record_len = SWAPS(header->rlen);
345 if((char*)header + erf_record_len > endofbuf)
346 break;
348 /* Increase the number of captured packets */
349 pw->stat.ps_recv++;
351 /* Find the beginning of the packet */
352 dp = ((u_char *)header) + dag_record_size;
354 /* Determine actual packet len */
355 switch(header->type)
357 case TYPE_ATM:
358 packet_len = ATM_SNAPLEN;
359 caplen = ATM_SNAPLEN;
360 dp += 4;
362 break;
364 case TYPE_ETH:
365 swt = SWAPS(header->wlen);
366 packet_len = swt - (pw->dag_fcs_bits);
367 caplen = erf_record_len - dag_record_size - 2;
368 if (caplen > packet_len)
370 caplen = packet_len;
372 dp += 2;
374 break;
376 case TYPE_HDLC_POS:
377 swt = SWAPS(header->wlen);
378 packet_len = swt - (pw->dag_fcs_bits);
379 caplen = erf_record_len - dag_record_size;
380 if (caplen > packet_len)
382 caplen = packet_len;
385 break;
388 if(caplen > p->snapshot)
389 caplen = p->snapshot;
392 * Has "pcap_breakloop()" been called?
393 * If so, return immediately - if we haven't read any
394 * packets, clear the flag and return -2 to indicate
395 * that we were told to break out of the loop, otherwise
396 * leave the flag set, so that the *next* call will break
397 * out of the loop without having read any packets, and
398 * return the number of packets we've processed so far.
400 if (p->break_loop)
402 if (n == 0)
404 p->break_loop = 0;
405 return (-2);
407 else
409 p->bp = (char*)header;
410 p->cc = endofbuf - (char*)header;
411 return (n);
415 if(!dfp)
417 /* convert between timestamp formats */
418 ts = header->ts;
419 pcap_header.ts.tv_sec = (int)(ts >> 32);
420 ts = (ts & 0xffffffffi64) * 1000000;
421 ts += 0x80000000; /* rounding */
422 pcap_header.ts.tv_usec = (int)(ts >> 32);
423 if (pcap_header.ts.tv_usec >= 1000000) {
424 pcap_header.ts.tv_usec -= 1000000;
425 pcap_header.ts.tv_sec++;
429 /* No underlaying filtering system. We need to filter on our own */
430 if (p->fcode.bf_insns)
432 if (bpf_filter(p->fcode.bf_insns, dp, packet_len, caplen) == 0)
434 /* Move to next packet */
435 header = (dag_record_t*)((char*)header + erf_record_len);
436 continue;
440 /* Fill the header for the user suppplied callback function */
441 pcap_header.caplen = caplen;
442 pcap_header.len = packet_len;
444 /* Call the callback function */
445 (*callback)(user, &pcap_header, dp);
447 /* Move to next packet */
448 header = (dag_record_t*)((char*)header + erf_record_len);
450 /* Stop if the number of packets requested by user has been reached*/
451 if (++n >= cnt && !PACKET_COUNT_IS_UNLIMITED(cnt))
453 p->bp = (char*)header;
454 p->cc = endofbuf - (char*)header;
455 return (n);
458 while((u_char*)header < endofbuf);
460 return 1;
462 #endif /* HAVE_DAG_API */
464 /* Send a packet to the network */
465 static int
466 pcap_inject_win32(pcap_t *p, const void *buf, size_t size){
467 LPPACKET PacketToSend;
469 PacketToSend=PacketAllocatePacket();
471 if (PacketToSend == NULL)
473 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send error: PacketAllocatePacket failed");
474 return -1;
477 PacketInitPacket(PacketToSend,(PVOID)buf,size);
478 if(PacketSendPacket(p->adapter,PacketToSend,TRUE) == FALSE){
479 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send error: PacketSendPacket failed");
480 PacketFreePacket(PacketToSend);
481 return -1;
484 PacketFreePacket(PacketToSend);
487 * We assume it all got sent if "PacketSendPacket()" succeeded.
488 * "pcap_inject()" is expected to return the number of bytes
489 * sent.
491 return size;
494 static void
495 pcap_cleanup_win32(pcap_t *p)
497 if (p->adapter != NULL) {
498 PacketCloseAdapter(p->adapter);
499 p->adapter = NULL;
501 if (p->Packet) {
502 PacketFreePacket(p->Packet);
503 p->Packet = NULL;
505 pcap_cleanup_live_common(p);
508 static int
509 pcap_activate_win32(pcap_t *p)
511 struct pcap_win *pw = p->priv;
512 NetType type;
514 if (p->opt.rfmon) {
516 * No monitor mode on Windows. It could be done on
517 * Vista with drivers that support the native 802.11
518 * mechanism and monitor mode.
520 return (PCAP_ERROR_RFMON_NOTSUP);
523 /* Init WinSock */
524 wsockinit();
526 p->adapter = PacketOpenAdapter(p->opt.source);
528 if (p->adapter == NULL)
530 /* Adapter detected but we are not able to open it. Return failure. */
531 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "Error opening adapter: %s", pcap_win32strerror());
532 return PCAP_ERROR;
535 /*get network type*/
536 if(PacketGetNetType (p->adapter,&type) == FALSE)
538 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "Cannot determine the network type: %s", pcap_win32strerror());
539 goto bad;
542 /*Set the linktype*/
543 switch (type.LinkType)
545 case NdisMediumWan:
546 p->linktype = DLT_EN10MB;
547 break;
549 case NdisMedium802_3:
550 p->linktype = DLT_EN10MB;
552 * This is (presumably) a real Ethernet capture; give it a
553 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
554 * that an application can let you choose it, in case you're
555 * capturing DOCSIS traffic that a Cisco Cable Modem
556 * Termination System is putting out onto an Ethernet (it
557 * doesn't put an Ethernet header onto the wire, it puts raw
558 * DOCSIS frames out on the wire inside the low-level
559 * Ethernet framing).
561 p->dlt_list = (u_int *) malloc(sizeof(u_int) * 2);
563 * If that fails, just leave the list empty.
565 if (p->dlt_list != NULL) {
566 p->dlt_list[0] = DLT_EN10MB;
567 p->dlt_list[1] = DLT_DOCSIS;
568 p->dlt_count = 2;
570 break;
572 case NdisMediumFddi:
573 p->linktype = DLT_FDDI;
574 break;
576 case NdisMedium802_5:
577 p->linktype = DLT_IEEE802;
578 break;
580 case NdisMediumArcnetRaw:
581 p->linktype = DLT_ARCNET;
582 break;
584 case NdisMediumArcnet878_2:
585 p->linktype = DLT_ARCNET;
586 break;
588 case NdisMediumAtm:
589 p->linktype = DLT_ATM_RFC1483;
590 break;
592 case NdisMediumCHDLC:
593 p->linktype = DLT_CHDLC;
594 break;
596 case NdisMediumPPPSerial:
597 p->linktype = DLT_PPP_SERIAL;
598 break;
600 case NdisMediumNull:
601 p->linktype = DLT_NULL;
602 break;
604 case NdisMediumBare80211:
605 p->linktype = DLT_IEEE802_11;
606 break;
608 case NdisMediumRadio80211:
609 p->linktype = DLT_IEEE802_11_RADIO;
610 break;
612 case NdisMediumPpi:
613 p->linktype = DLT_PPI;
614 break;
616 default:
617 p->linktype = DLT_EN10MB; /*an unknown adapter is assumed to be ethernet*/
618 break;
621 /* Set promiscuous mode */
622 if (p->opt.promisc)
625 if (PacketSetHwFilter(p->adapter,NDIS_PACKET_TYPE_PROMISCUOUS) == FALSE)
627 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "failed to set hardware filter to promiscuous mode");
628 goto bad;
631 else
633 if (PacketSetHwFilter(p->adapter,NDIS_PACKET_TYPE_ALL_LOCAL) == FALSE)
635 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "failed to set hardware filter to non-promiscuous mode");
636 goto bad;
640 /* Set the buffer size */
641 p->bufsize = WIN32_DEFAULT_USER_BUFFER_SIZE;
643 /* allocate Packet structure used during the capture */
644 if((p->Packet = PacketAllocatePacket())==NULL)
646 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "failed to allocate the PACKET structure");
647 goto bad;
650 if(!(p->adapter->Flags & INFO_FLAG_DAG_CARD))
653 * Traditional Adapter
656 * If the buffer size wasn't explicitly set, default to
657 * WIN32_DEFAULT_USER_BUFFER_SIZE.
659 if (p->opt.buffer_size == 0)
660 p->opt.buffer_size = WIN32_DEFAULT_KERNEL_BUFFER_SIZE;
662 if(PacketSetBuff(p->adapter,p->opt.buffer_size)==FALSE)
664 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "driver error: not enough memory to allocate the kernel buffer");
665 goto bad;
668 p->buffer = (u_char *)malloc(p->bufsize);
669 if (p->buffer == NULL)
671 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "malloc: %s", pcap_strerror(errno));
672 goto bad;
675 PacketInitPacket(p->Packet,(BYTE*)p->buffer,p->bufsize);
677 if (p->opt.immediate)
679 /* tell the driver to copy the buffer as soon as data arrives */
680 if(PacketSetMinToCopy(p->adapter,0)==FALSE)
682 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,"Error calling PacketSetMinToCopy: %s", pcap_win32strerror());
683 goto bad;
686 else
688 /* tell the driver to copy the buffer only if it contains at least 16K */
689 if(PacketSetMinToCopy(p->adapter,16000)==FALSE)
691 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,"Error calling PacketSetMinToCopy: %s", pcap_win32strerror());
692 goto bad;
696 else
697 #ifdef HAVE_DAG_API
700 * Dag Card
702 LONG status;
703 HKEY dagkey;
704 DWORD lptype;
705 DWORD lpcbdata;
706 int postype = 0;
707 char keyname[512];
709 snprintf(keyname, sizeof(keyname), "%s\\CardParams\\%s",
710 "SYSTEM\\CurrentControlSet\\Services\\DAG",
711 strstr(_strlwr(p->opt.source), "dag"));
714 status = RegOpenKeyEx(HKEY_LOCAL_MACHINE, keyname, 0, KEY_READ, &dagkey);
715 if(status != ERROR_SUCCESS)
716 break;
718 status = RegQueryValueEx(dagkey,
719 "PosType",
720 NULL,
721 &lptype,
722 (char*)&postype,
723 &lpcbdata);
725 if(status != ERROR_SUCCESS)
727 postype = 0;
730 RegCloseKey(dagkey);
732 while(FALSE);
735 p->snapshot = PacketSetSnapLen(p->adapter, snaplen);
737 /* Set the length of the FCS associated to any packet. This value
738 * will be subtracted to the packet length */
739 pw->dag_fcs_bits = p->adapter->DagFcsLen;
741 #else
742 goto bad;
743 #endif /* HAVE_DAG_API */
745 PacketSetReadTimeout(p->adapter, p->opt.timeout);
747 #ifdef HAVE_DAG_API
748 if(p->adapter->Flags & INFO_FLAG_DAG_CARD)
750 /* install dag specific handlers for read and setfilter */
751 p->read_op = pcap_read_win32_dag;
752 p->setfilter_op = pcap_setfilter_win32_dag;
754 else
756 #endif /* HAVE_DAG_API */
757 /* install traditional npf handlers for read and setfilter */
758 p->read_op = pcap_read_win32_npf;
759 p->setfilter_op = pcap_setfilter_win32_npf;
760 #ifdef HAVE_DAG_API
762 #endif /* HAVE_DAG_API */
763 p->setdirection_op = NULL; /* Not implemented. */
764 /* XXX - can this be implemented on some versions of Windows? */
765 p->inject_op = pcap_inject_win32;
766 p->set_datalink_op = NULL; /* can't change data link type */
767 p->getnonblock_op = pcap_getnonblock_win32;
768 p->setnonblock_op = pcap_setnonblock_win32;
769 p->stats_op = pcap_stats_win32;
770 p->setbuff_op = pcap_setbuff_win32;
771 p->setmode_op = pcap_setmode_win32;
772 p->setmintocopy_op = pcap_setmintocopy_win32;
773 p->getadapter_op = pcap_getadapter_win32;
774 p->cleanup_op = pcap_cleanup_win32;
776 return (0);
777 bad:
778 pcap_cleanup_win32(p);
779 return (PCAP_ERROR);
782 pcap_t *
783 pcap_create_interface(const char *device, char *ebuf)
785 pcap_t *p;
787 if (strlen(device) == 1)
790 * It's probably a unicode string
791 * Convert to ascii and pass it to pcap_create_common
793 * This wonderful hack is needed because pcap_lookupdev still returns
794 * unicode strings, and it's used by windump when no device is specified
795 * in the command line
797 size_t length;
798 char* deviceAscii;
800 length = wcslen((wchar_t*)device);
802 deviceAscii = (char*)malloc(length + 1);
804 if (deviceAscii == NULL)
806 snprintf(ebuf, PCAP_ERRBUF_SIZE, "Malloc failed");
807 return NULL;
810 snprintf(deviceAscii, length + 1, "%ws", (wchar_t*)device);
811 p = pcap_create_common(deviceAscii, ebuf, sizeof (struct pcap_win));
812 free(deviceAscii);
814 else
816 p = pcap_create_common(device, ebuf, sizeof (struct pcap_win));
819 if (p == NULL)
820 return (NULL);
822 p->activate_op = pcap_activate_win32;
823 return (p);
826 static int
827 pcap_setfilter_win32_npf(pcap_t *p, struct bpf_program *fp)
829 struct pcap_win *pw = p->priv;
831 if(PacketSetBpf(p->adapter,fp)==FALSE){
833 * Kernel filter not installed.
835 * XXX - we don't know whether this failed because:
837 * the kernel rejected the filter program as invalid,
838 * in which case we should fall back on userland
839 * filtering;
841 * the kernel rejected the filter program as too big,
842 * in which case we should again fall back on
843 * userland filtering;
845 * there was some other problem, in which case we
846 * should probably report an error.
848 * For NPF devices, the Win32 status will be
849 * STATUS_INVALID_DEVICE_REQUEST for invalid
850 * filters, but I don't know what it'd be for
851 * other problems, and for some other devices
852 * it might not be set at all.
854 * So we just fall back on userland filtering in
855 * all cases.
859 * install_bpf_program() validates the program.
861 * XXX - what if we already have a filter in the kernel?
863 if (install_bpf_program(p, fp) < 0)
864 return (-1);
865 pw->filtering_in_kernel = 0; /* filtering in userland */
866 return (0);
870 * It worked.
872 pw->filtering_in_kernel = 1; /* filtering in the kernel */
875 * Discard any previously-received packets, as they might have
876 * passed whatever filter was formerly in effect, but might
877 * not pass this filter (BIOCSETF discards packets buffered
878 * in the kernel, so you can lose packets in any case).
880 p->cc = 0;
881 return (0);
885 * We filter at user level, since the kernel driver does't process the packets
887 static int
888 pcap_setfilter_win32_dag(pcap_t *p, struct bpf_program *fp) {
890 if(!fp)
892 strncpy(p->errbuf, "setfilter: No filter specified", sizeof(p->errbuf));
893 return -1;
896 /* Install a user level filter */
897 if (install_bpf_program(p, fp) < 0)
899 snprintf(p->errbuf, sizeof(p->errbuf),
900 "setfilter, unable to install the filter: %s", pcap_strerror(errno));
901 return -1;
904 return (0);
907 static int
908 pcap_getnonblock_win32(pcap_t *p, char *errbuf)
910 struct pcap_win *pw = p->priv;
913 * XXX - if there were a PacketGetReadTimeout() call, we
914 * would use it, and return 1 if the timeout is -1
915 * and 0 otherwise.
917 return (pw->nonblock);
920 static int
921 pcap_setnonblock_win32(pcap_t *p, int nonblock, char *errbuf)
923 struct pcap_win *pw = p->priv;
924 int newtimeout;
926 if (nonblock) {
928 * Set the read timeout to -1 for non-blocking mode.
930 newtimeout = -1;
931 } else {
933 * Restore the timeout set when the device was opened.
934 * (Note that this may be -1, in which case we're not
935 * really leaving non-blocking mode.)
937 newtimeout = p->opt.timeout;
939 if (!PacketSetReadTimeout(p->adapter, newtimeout)) {
940 snprintf(errbuf, PCAP_ERRBUF_SIZE,
941 "PacketSetReadTimeout: %s", pcap_win32strerror());
942 return (-1);
944 pw->nonblock = (newtimeout == -1);
945 return (0);
948 /*platform-dependent routine to add devices other than NDIS interfaces*/
950 pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf)
952 return (0);