]> The Tcpdump Group git mirrors - libpcap/blob - pcap-win32.c
Fix MSVC compile warnings on the WinPcap specific code.
[libpcap] / pcap-win32.c
1 /*
2 * Copyright (c) 1999 - 2005 NetGroup, Politecnico di Torino (Italy)
3 * Copyright (c) 2005 - 2010 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 #define PCAP_DONT_INCLUDE_PCAP_BPF_H
35 #include <Packet32.h>
36 #include <pcap-int.h>
37 #include <pcap/dlt.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__ */
56 #ifdef HAVE_REMOTE
57 #include "pcap-remote.h"
58 #endif /* HAVE_REMOTE */
59
60 static int pcap_setfilter_win32_npf(pcap_t *, struct bpf_program *);
61 static int pcap_setfilter_win32_dag(pcap_t *, struct bpf_program *);
62 static int pcap_getnonblock_win32(pcap_t *, char *);
63 static int pcap_setnonblock_win32(pcap_t *, int, char *);
64
65 /*dimension of the buffer in the pcap_t structure*/
66 #define WIN32_DEFAULT_USER_BUFFER_SIZE 256000
67
68 /*dimension of the buffer in the kernel driver NPF */
69 #define WIN32_DEFAULT_KERNEL_BUFFER_SIZE 1000000
70
71 /* Equivalent to ntohs(), but a lot faster under Windows */
72 #define SWAPS(_X) ((_X & 0xff) << 8) | (_X >> 8)
73
74 /*
75 * Private data for capturing on WinPcap devices.
76 */
77 struct pcap_win {
78 int nonblock;
79 int rfmon_selfstart; /* a flag tells whether the monitor mode is set by itself */
80 int filtering_in_kernel; /* using kernel filter */
81
82 #ifdef HAVE_DAG_API
83 int dag_fcs_bits; /* Number of checksum bits from link layer */
84 #endif
85 };
86
87 BOOL WINAPI DllMain(
88 HANDLE hinstDLL,
89 DWORD dwReason,
90 LPVOID lpvReserved
91 )
92 {
93 return (TRUE);
94 }
95
96 /*
97 * Define stub versions of the monitor-mode support routines if this
98 * isn't Npcap. NPF_DRIVER_NAME_NORMAL is defined by Npcap but not
99 * WinPcap.
100 */
101 #ifndef NPF_DRIVER_NAME_NORMAL
102 static int
103 PacketIsMonitorModeSupported(PCHAR AdapterName _U_)
104 {
105 /*
106 * We don't support monitor mode.
107 */
108 return (0);
109 }
110
111 static int
112 PacketSetMonitorMode(PCHAR AdapterName _U_, int mode _U_)
113 {
114 /*
115 * This should never be called, as PacketIsMonitorModeSupported()
116 * will return 0, meaning "we don't support monitor mode, so
117 * don't try to turn it on or off".
118 */
119 return (0);
120 }
121
122 static int
123 PacketGetMonitorMode(PCHAR AdapterName _U_)
124 {
125 /*
126 * This should fail, so that pcap_activate_win32() returns
127 * PCAP_ERROR_RFMON_NOTSUP if our caller requested monitor
128 * mode.
129 */
130 return (-1);
131 }
132 #endif
133
134 /* Start winsock */
135 int
136 wsockinit(void)
137 {
138 WORD wVersionRequested;
139 WSADATA wsaData;
140 static int err = -1;
141 static int done = 0;
142
143 if (done)
144 return (err);
145
146 wVersionRequested = MAKEWORD( 1, 1);
147 err = WSAStartup( wVersionRequested, &wsaData );
148 atexit ((void(*)(void))WSACleanup);
149 done = 1;
150
151 if ( err != 0 )
152 err = -1;
153 return (err);
154 }
155
156 int
157 pcap_wsockinit(void)
158 {
159 return (wsockinit());
160 }
161
162 static int
163 pcap_stats_win32(pcap_t *p, struct pcap_stat *ps)
164 {
165 struct bpf_stat bstats;
166 char errbuf[PCAP_ERRBUF_SIZE+1];
167
168 /*
169 * Try to get statistics.
170 *
171 * (Please note - "struct pcap_stat" is *not* the same as
172 * WinPcap's "struct bpf_stat". It might currently have the
173 * same layout, but let's not cheat.
174 *
175 * Note also that we don't fill in ps_capt, as we might have
176 * been called by code compiled against an earlier version of
177 * WinPcap that didn't have ps_capt, in which case filling it
178 * in would stomp on whatever comes after the structure passed
179 * to us.
180 */
181 if (!PacketGetStats(p->adapter, &bstats)) {
182 pcap_win32_err_to_str(GetLastError(), errbuf);
183 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
184 "PacketGetStats error: %s", errbuf);
185 return (-1);
186 }
187 ps->ps_recv = bstats.bs_recv;
188 ps->ps_drop = bstats.bs_drop;
189
190 /*
191 * XXX - PacketGetStats() doesn't fill this in, so we just
192 * return 0.
193 */
194 #if 0
195 ps->ps_ifdrop = bstats.ps_ifdrop;
196 #else
197 ps->ps_ifdrop = 0;
198 #endif
199
200 return (0);
201 }
202
203 /*
204 * Win32-only routine for getting statistics.
205 *
206 * This way is definitely safer than passing the pcap_stat * from the userland.
207 * In fact, there could happen than the user allocates a variable which is not
208 * big enough for the new structure, and the library will write in a zone
209 * which is not allocated to this variable.
210 *
211 * In this way, we're pretty sure we are writing on memory allocated to this
212 * variable.
213 *
214 * XXX - but this is the wrong way to handle statistics. Instead, we should
215 * have an API that returns data in a form like the Options section of a
216 * pcapng Interface Statistics Block:
217 *
218 * http://xml2rfc.tools.ietf.org/cgi-bin/xml2rfc.cgi?url=https://raw.githubusercontent.com/pcapng/pcapng/master/draft-tuexen-opsawg-pcapng.xml&modeAsFormat=html/ascii&type=ascii#rfc.section.4.6
219 *
220 * which would let us add new statistics straightforwardly and indicate which
221 * statistics we are and are *not* providing, rather than having to provide
222 * possibly-bogus values for statistics we can't provide.
223 */
224 struct pcap_stat *
225 pcap_stats_ex_win32(pcap_t *p, int *pcap_stat_size)
226 {
227 struct bpf_stat bstats;
228 char errbuf[PCAP_ERRBUF_SIZE+1];
229
230 *pcap_stat_size = sizeof (p->stat);
231
232 /*
233 * Try to get statistics.
234 *
235 * (Please note - "struct pcap_stat" is *not* the same as
236 * WinPcap's "struct bpf_stat". It might currently have the
237 * same layout, but let's not cheat.)
238 */
239 if (!PacketGetStatsEx(p->adapter, &bstats)) {
240 pcap_win32_err_to_str(GetLastError(), errbuf);
241 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
242 "PacketGetStatsEx error: %s", errbuf);
243 return (NULL);
244 }
245 p->stat.ps_recv = bstats.bs_recv;
246 p->stat.ps_drop = bstats.bs_drop;
247 p->stat.ps_ifdrop = bstats.ps_ifdrop;
248 #ifdef HAVE_REMOTE
249 p->stat.ps_capt = bstats.bs_capt;
250 #endif
251 return (&p->stat);
252 }
253
254 /* Set the dimension of the kernel-level capture buffer */
255 static int
256 pcap_setbuff_win32(pcap_t *p, int dim)
257 {
258 if(PacketSetBuff(p->adapter,dim)==FALSE)
259 {
260 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "driver error: not enough memory to allocate the kernel buffer");
261 return (-1);
262 }
263 return (0);
264 }
265
266 /* Set the driver working mode */
267 static int
268 pcap_setmode_win32(pcap_t *p, int mode)
269 {
270 if(PacketSetMode(p->adapter,mode)==FALSE)
271 {
272 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "driver error: working mode not recognized");
273 return (-1);
274 }
275
276 return (0);
277 }
278
279 /*set the minimum amount of data that will release a read call*/
280 static int
281 pcap_setmintocopy_win32(pcap_t *p, int size)
282 {
283 if(PacketSetMinToCopy(p->adapter, size)==FALSE)
284 {
285 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "driver error: unable to set the requested mintocopy size");
286 return (-1);
287 }
288 return (0);
289 }
290
291 static HANDLE
292 pcap_getevent_win32(pcap_t *p)
293 {
294 return (PacketGetReadEvent(p->adapter));
295 }
296
297 static int
298 pcap_oid_get_request_win32(pcap_t *p, bpf_u_int32 oid, void *data, size_t *lenp)
299 {
300 PACKET_OID_DATA *oid_data_arg;
301 char errbuf[PCAP_ERRBUF_SIZE+1];
302
303 /*
304 * Allocate a PACKET_OID_DATA structure to hand to PacketRequest().
305 * It should be big enough to hold "*lenp" bytes of data; it
306 * will actually be slightly larger, as PACKET_OID_DATA has a
307 * 1-byte data array at the end, standing in for the variable-length
308 * data that's actually there.
309 */
310 oid_data_arg = malloc(sizeof (PACKET_OID_DATA) + *lenp);
311 if (oid_data_arg == NULL) {
312 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
313 "Couldn't allocate argument buffer for PacketRequest");
314 return (PCAP_ERROR);
315 }
316
317 /*
318 * No need to copy the data - we're doing a fetch.
319 */
320 oid_data_arg->Oid = oid;
321 oid_data_arg->Length = (ULONG)(*lenp); /* XXX - check for ridiculously large value? */
322 if (!PacketRequest(p->adapter, FALSE, oid_data_arg)) {
323 pcap_win32_err_to_str(GetLastError(), errbuf);
324 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
325 "Error calling PacketRequest: %s", errbuf);
326 free(oid_data_arg);
327 return (PCAP_ERROR);
328 }
329
330 /*
331 * Get the length actually supplied.
332 */
333 *lenp = oid_data_arg->Length;
334
335 /*
336 * Copy back the data we fetched.
337 */
338 memcpy(data, oid_data_arg->Data, *lenp);
339 free(oid_data_arg);
340 return (0);
341 }
342
343 static int
344 pcap_oid_set_request_win32(pcap_t *p, bpf_u_int32 oid, const void *data,
345 size_t *lenp)
346 {
347 PACKET_OID_DATA *oid_data_arg;
348 char errbuf[PCAP_ERRBUF_SIZE+1];
349
350 /*
351 * Allocate a PACKET_OID_DATA structure to hand to PacketRequest().
352 * It should be big enough to hold "*lenp" bytes of data; it
353 * will actually be slightly larger, as PACKET_OID_DATA has a
354 * 1-byte data array at the end, standing in for the variable-length
355 * data that's actually there.
356 */
357 oid_data_arg = malloc(sizeof (PACKET_OID_DATA) + *lenp);
358 if (oid_data_arg == NULL) {
359 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
360 "Couldn't allocate argument buffer for PacketRequest");
361 return (PCAP_ERROR);
362 }
363
364 oid_data_arg->Oid = oid;
365 oid_data_arg->Length = (ULONG)(*lenp); /* XXX - check for ridiculously large value? */
366 memcpy(oid_data_arg->Data, data, *lenp);
367 if (!PacketRequest(p->adapter, TRUE, oid_data_arg)) {
368 pcap_win32_err_to_str(GetLastError(), errbuf);
369 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
370 "Error calling PacketRequest: %s", errbuf);
371 free(oid_data_arg);
372 return (PCAP_ERROR);
373 }
374
375 /*
376 * Get the length actually copied.
377 */
378 *lenp = oid_data_arg->Length;
379
380 /*
381 * No need to copy the data - we're doing a set.
382 */
383 free(oid_data_arg);
384 return (0);
385 }
386
387 static u_int
388 pcap_sendqueue_transmit_win32(pcap_t *p, pcap_send_queue *queue, int sync)
389 {
390 u_int res;
391 char errbuf[PCAP_ERRBUF_SIZE+1];
392
393 if (p->adapter==NULL) {
394 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
395 "Cannot transmit a queue to an offline capture or to a TurboCap port");
396 return (0);
397 }
398
399 res = PacketSendPackets(p->adapter,
400 queue->buffer,
401 queue->len,
402 (BOOLEAN)sync);
403
404 if(res != queue->len){
405 pcap_win32_err_to_str(GetLastError(), errbuf);
406 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
407 "Error opening adapter: %s", errbuf);
408 }
409
410 return (res);
411 }
412
413 static int
414 pcap_setuserbuffer_win32(pcap_t *p, int size)
415 {
416 unsigned char *new_buff;
417
418 if (size<=0) {
419 /* Bogus parameter */
420 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
421 "Error: invalid size %d",size);
422 return (-1);
423 }
424
425 /* Allocate the buffer */
426 new_buff=(unsigned char*)malloc(sizeof(char)*size);
427
428 if (!new_buff) {
429 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
430 "Error: not enough memory");
431 return (-1);
432 }
433
434 free(p->buffer);
435
436 p->buffer=new_buff;
437 p->bufsize=size;
438
439 return (0);
440 }
441
442 static int
443 pcap_live_dump_win32(pcap_t *p, char *filename, int maxsize, int maxpacks)
444 {
445 BOOLEAN res;
446
447 /* Set the packet driver in dump mode */
448 res = PacketSetMode(p->adapter, PACKET_MODE_DUMP);
449 if(res == FALSE){
450 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
451 "Error setting dump mode");
452 return (-1);
453 }
454
455 /* Set the name of the dump file */
456 res = PacketSetDumpName(p->adapter, filename, (int)strlen(filename));
457 if(res == FALSE){
458 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
459 "Error setting kernel dump file name");
460 return (-1);
461 }
462
463 /* Set the limits of the dump file */
464 res = PacketSetDumpLimits(p->adapter, maxsize, maxpacks);
465
466 return (0);
467 }
468
469 static int
470 pcap_live_dump_ended_win32(pcap_t *p, int sync)
471 {
472 return (PacketIsDumpEnded(p->adapter, (BOOLEAN)sync));
473 }
474
475 static PAirpcapHandle
476 pcap_get_airpcap_handle_win32(pcap_t *p)
477 {
478 #ifdef HAVE_AIRPCAP_API
479 return (PacketGetAirPcapHandle(p->adapter));
480 #else
481 return (NULL);
482 #endif /* HAVE_AIRPCAP_API */
483 }
484
485 static int
486 pcap_read_win32_npf(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
487 {
488 PACKET Packet;
489 int cc;
490 int n = 0;
491 register u_char *bp, *ep;
492 u_char *datap;
493 struct pcap_win *pw = p->priv;
494
495 cc = p->cc;
496 if (p->cc == 0) {
497 /*
498 * Has "pcap_breakloop()" been called?
499 */
500 if (p->break_loop) {
501 /*
502 * Yes - clear the flag that indicates that it
503 * has, and return PCAP_ERROR_BREAK to indicate
504 * that we were told to break out of the loop.
505 */
506 p->break_loop = 0;
507 return (PCAP_ERROR_BREAK);
508 }
509
510 /*
511 * Capture the packets.
512 *
513 * The PACKET structure had a bunch of extra stuff for
514 * Windows 9x/Me, but the only interesting data in it
515 * in the versions of Windows that we support is just
516 * a copy of p->buffer, a copy of p->buflen, and the
517 * actual number of bytes read returned from
518 * PacketReceivePacket(), none of which has to be
519 * retained from call to call, so we just keep one on
520 * the stack.
521 */
522 PacketInitPacket(&Packet, (BYTE *)p->buffer, p->bufsize);
523 if (!PacketReceivePacket(p->adapter, &Packet, TRUE)) {
524 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "read error: PacketReceivePacket failed");
525 return (PCAP_ERROR);
526 }
527
528 cc = Packet.ulBytesReceived;
529
530 bp = p->buffer;
531 }
532 else
533 bp = p->bp;
534
535 /*
536 * Loop through each packet.
537 */
538 #define bhp ((struct bpf_hdr *)bp)
539 ep = bp + cc;
540 while (1) {
541 register int caplen, hdrlen;
542
543 /*
544 * Has "pcap_breakloop()" been called?
545 * If so, return immediately - if we haven't read any
546 * packets, clear the flag and return PCAP_ERROR_BREAK
547 * to indicate that we were told to break out of the loop,
548 * otherwise leave the flag set, so that the *next* call
549 * will break out of the loop without having read any
550 * packets, and return the number of packets we've
551 * processed so far.
552 */
553 if (p->break_loop) {
554 if (n == 0) {
555 p->break_loop = 0;
556 return (PCAP_ERROR_BREAK);
557 } else {
558 p->bp = bp;
559 p->cc = ep - bp;
560 return (n);
561 }
562 }
563 if (bp >= ep)
564 break;
565
566 caplen = bhp->bh_caplen;
567 hdrlen = bhp->bh_hdrlen;
568 datap = bp + hdrlen;
569
570 /*
571 * Short-circuit evaluation: if using BPF filter
572 * in kernel, no need to do it now - we already know
573 * the packet passed the filter.
574 *
575 * XXX - bpf_filter() should always return TRUE if
576 * handed a null pointer for the program, but it might
577 * just try to "run" the filter, so we check here.
578 */
579 if (pw->filtering_in_kernel ||
580 p->fcode.bf_insns == NULL ||
581 bpf_filter(p->fcode.bf_insns, datap, bhp->bh_datalen, caplen)) {
582 /*
583 * XXX A bpf_hdr matches a pcap_pkthdr.
584 */
585 (*callback)(user, (struct pcap_pkthdr*)bp, datap);
586 bp += Packet_WORDALIGN(caplen + hdrlen);
587 if (++n >= cnt && !PACKET_COUNT_IS_UNLIMITED(cnt)) {
588 p->bp = bp;
589 p->cc = ep - bp;
590 return (n);
591 }
592 } else {
593 /*
594 * Skip this packet.
595 */
596 bp += Packet_WORDALIGN(caplen + hdrlen);
597 }
598 }
599 #undef bhp
600 p->cc = 0;
601 return (n);
602 }
603
604 #ifdef HAVE_DAG_API
605 static int
606 pcap_read_win32_dag(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
607 {
608 struct pcap_win *pw = p->priv;
609 PACKET Packet;
610 u_char *dp = NULL;
611 int packet_len = 0, caplen = 0;
612 struct pcap_pkthdr pcap_header;
613 u_char *endofbuf;
614 int n = 0;
615 dag_record_t *header;
616 unsigned erf_record_len;
617 ULONGLONG ts;
618 int cc;
619 unsigned swt;
620 unsigned dfp = p->adapter->DagFastProcess;
621
622 cc = p->cc;
623 if (cc == 0) /* Get new packets only if we have processed all the ones of the previous read */
624 {
625 /*
626 * Get new packets from the network.
627 *
628 * The PACKET structure had a bunch of extra stuff for
629 * Windows 9x/Me, but the only interesting data in it
630 * in the versions of Windows that we support is just
631 * a copy of p->buffer, a copy of p->buflen, and the
632 * actual number of bytes read returned from
633 * PacketReceivePacket(), none of which has to be
634 * retained from call to call, so we just keep one on
635 * the stack.
636 */
637 PacketInitPacket(&Packet, (BYTE *)p->buffer, p->bufsize);
638 if (!PacketReceivePacket(p->adapter, &Packet, TRUE)) {
639 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "read error: PacketReceivePacket failed");
640 return (-1);
641 }
642
643 cc = Packet.ulBytesReceived;
644 if(cc == 0)
645 /* The timeout has expired but we no packets arrived */
646 return (0);
647 header = (dag_record_t*)p->adapter->DagBuffer;
648 }
649 else
650 header = (dag_record_t*)p->bp;
651
652 endofbuf = (char*)header + cc;
653
654 /*
655 * Cycle through the packets
656 */
657 do
658 {
659 erf_record_len = SWAPS(header->rlen);
660 if((char*)header + erf_record_len > endofbuf)
661 break;
662
663 /* Increase the number of captured packets */
664 p->stat.ps_recv++;
665
666 /* Find the beginning of the packet */
667 dp = ((u_char *)header) + dag_record_size;
668
669 /* Determine actual packet len */
670 switch(header->type)
671 {
672 case TYPE_ATM:
673 packet_len = ATM_SNAPLEN;
674 caplen = ATM_SNAPLEN;
675 dp += 4;
676
677 break;
678
679 case TYPE_ETH:
680 swt = SWAPS(header->wlen);
681 packet_len = swt - (pw->dag_fcs_bits);
682 caplen = erf_record_len - dag_record_size - 2;
683 if (caplen > packet_len)
684 {
685 caplen = packet_len;
686 }
687 dp += 2;
688
689 break;
690
691 case TYPE_HDLC_POS:
692 swt = SWAPS(header->wlen);
693 packet_len = swt - (pw->dag_fcs_bits);
694 caplen = erf_record_len - dag_record_size;
695 if (caplen > packet_len)
696 {
697 caplen = packet_len;
698 }
699
700 break;
701 }
702
703 if(caplen > p->snapshot)
704 caplen = p->snapshot;
705
706 /*
707 * Has "pcap_breakloop()" been called?
708 * If so, return immediately - if we haven't read any
709 * packets, clear the flag and return -2 to indicate
710 * that we were told to break out of the loop, otherwise
711 * leave the flag set, so that the *next* call will break
712 * out of the loop without having read any packets, and
713 * return the number of packets we've processed so far.
714 */
715 if (p->break_loop)
716 {
717 if (n == 0)
718 {
719 p->break_loop = 0;
720 return (-2);
721 }
722 else
723 {
724 p->bp = (char*)header;
725 p->cc = endofbuf - (char*)header;
726 return (n);
727 }
728 }
729
730 if(!dfp)
731 {
732 /* convert between timestamp formats */
733 ts = header->ts;
734 pcap_header.ts.tv_sec = (int)(ts >> 32);
735 ts = (ts & 0xffffffffi64) * 1000000;
736 ts += 0x80000000; /* rounding */
737 pcap_header.ts.tv_usec = (int)(ts >> 32);
738 if (pcap_header.ts.tv_usec >= 1000000) {
739 pcap_header.ts.tv_usec -= 1000000;
740 pcap_header.ts.tv_sec++;
741 }
742 }
743
744 /* No underlaying filtering system. We need to filter on our own */
745 if (p->fcode.bf_insns)
746 {
747 if (bpf_filter(p->fcode.bf_insns, dp, packet_len, caplen) == 0)
748 {
749 /* Move to next packet */
750 header = (dag_record_t*)((char*)header + erf_record_len);
751 continue;
752 }
753 }
754
755 /* Fill the header for the user suppplied callback function */
756 pcap_header.caplen = caplen;
757 pcap_header.len = packet_len;
758
759 /* Call the callback function */
760 (*callback)(user, &pcap_header, dp);
761
762 /* Move to next packet */
763 header = (dag_record_t*)((char*)header + erf_record_len);
764
765 /* Stop if the number of packets requested by user has been reached*/
766 if (++n >= cnt && !PACKET_COUNT_IS_UNLIMITED(cnt))
767 {
768 p->bp = (char*)header;
769 p->cc = endofbuf - (char*)header;
770 return (n);
771 }
772 }
773 while((u_char*)header < endofbuf);
774
775 return (1);
776 }
777 #endif /* HAVE_DAG_API */
778
779 /* Send a packet to the network */
780 static int
781 pcap_inject_win32(pcap_t *p, const void *buf, size_t size){
782 LPPACKET PacketToSend;
783
784 PacketToSend=PacketAllocatePacket();
785
786 if (PacketToSend == NULL)
787 {
788 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send error: PacketAllocatePacket failed");
789 return (-1);
790 }
791
792 PacketInitPacket(PacketToSend, (PVOID)buf, (UINT)size);
793 if(PacketSendPacket(p->adapter,PacketToSend,TRUE) == FALSE){
794 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send error: PacketSendPacket failed");
795 PacketFreePacket(PacketToSend);
796 return (-1);
797 }
798
799 PacketFreePacket(PacketToSend);
800
801 /*
802 * We assume it all got sent if "PacketSendPacket()" succeeded.
803 * "pcap_inject()" is expected to return the number of bytes
804 * sent.
805 */
806 return ((int)size);
807 }
808
809 static void
810 pcap_cleanup_win32(pcap_t *p)
811 {
812 struct pcap_win *pw = p->priv;
813 if (p->adapter != NULL) {
814 PacketCloseAdapter(p->adapter);
815 p->adapter = NULL;
816 }
817 if (pw->rfmon_selfstart)
818 {
819 PacketSetMonitorMode(p->opt.device, 0);
820 }
821 pcap_cleanup_live_common(p);
822 }
823
824 static int
825 pcap_activate_win32(pcap_t *p)
826 {
827 struct pcap_win *pw = p->priv;
828 NetType type;
829 int res;
830 char errbuf[PCAP_ERRBUF_SIZE+1];
831
832 if (p->opt.rfmon) {
833 /*
834 * Monitor mode is supported on Windows Vista and later.
835 */
836 if (PacketGetMonitorMode(p->opt.device) == 1)
837 {
838 pw->rfmon_selfstart = 0;
839 }
840 else
841 {
842 if ((res = PacketSetMonitorMode(p->opt.device, 1)) != 1)
843 {
844 pw->rfmon_selfstart = 0;
845 // Monitor mode is not supported.
846 if (res == 0)
847 {
848 return PCAP_ERROR_RFMON_NOTSUP;
849 }
850 else
851 {
852 return PCAP_ERROR;
853 }
854 }
855 else
856 {
857 pw->rfmon_selfstart = 1;
858 }
859 }
860 }
861
862 /* Init WinSock */
863 wsockinit();
864
865 p->adapter = PacketOpenAdapter(p->opt.device);
866
867 if (p->adapter == NULL)
868 {
869 /* Adapter detected but we are not able to open it. Return failure. */
870 pcap_win32_err_to_str(GetLastError(), errbuf);
871 if (pw->rfmon_selfstart)
872 {
873 PacketSetMonitorMode(p->opt.device, 0);
874 }
875 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
876 "Error opening adapter: %s", errbuf);
877 return (PCAP_ERROR);
878 }
879
880 /*get network type*/
881 if(PacketGetNetType (p->adapter,&type) == FALSE)
882 {
883 pcap_win32_err_to_str(GetLastError(), errbuf);
884 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
885 "Cannot determine the network type: %s", errbuf);
886 goto bad;
887 }
888
889 /*Set the linktype*/
890 switch (type.LinkType)
891 {
892 case NdisMediumWan:
893 p->linktype = DLT_EN10MB;
894 break;
895
896 case NdisMedium802_3:
897 p->linktype = DLT_EN10MB;
898 /*
899 * This is (presumably) a real Ethernet capture; give it a
900 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
901 * that an application can let you choose it, in case you're
902 * capturing DOCSIS traffic that a Cisco Cable Modem
903 * Termination System is putting out onto an Ethernet (it
904 * doesn't put an Ethernet header onto the wire, it puts raw
905 * DOCSIS frames out on the wire inside the low-level
906 * Ethernet framing).
907 */
908 p->dlt_list = (u_int *) malloc(sizeof(u_int) * 2);
909 /*
910 * If that fails, just leave the list empty.
911 */
912 if (p->dlt_list != NULL) {
913 p->dlt_list[0] = DLT_EN10MB;
914 p->dlt_list[1] = DLT_DOCSIS;
915 p->dlt_count = 2;
916 }
917 break;
918
919 case NdisMediumFddi:
920 p->linktype = DLT_FDDI;
921 break;
922
923 case NdisMedium802_5:
924 p->linktype = DLT_IEEE802;
925 break;
926
927 case NdisMediumArcnetRaw:
928 p->linktype = DLT_ARCNET;
929 break;
930
931 case NdisMediumArcnet878_2:
932 p->linktype = DLT_ARCNET;
933 break;
934
935 case NdisMediumAtm:
936 p->linktype = DLT_ATM_RFC1483;
937 break;
938
939 case NdisMediumCHDLC:
940 p->linktype = DLT_CHDLC;
941 break;
942
943 case NdisMediumPPPSerial:
944 p->linktype = DLT_PPP_SERIAL;
945 break;
946
947 case NdisMediumNull:
948 p->linktype = DLT_NULL;
949 break;
950
951 case NdisMediumBare80211:
952 p->linktype = DLT_IEEE802_11;
953 break;
954
955 case NdisMediumRadio80211:
956 p->linktype = DLT_IEEE802_11_RADIO;
957 break;
958
959 case NdisMediumPpi:
960 p->linktype = DLT_PPI;
961 break;
962
963 default:
964 p->linktype = DLT_EN10MB; /*an unknown adapter is assumed to be ethernet*/
965 break;
966 }
967
968 /* Set promiscuous mode */
969 if (p->opt.promisc)
970 {
971
972 if (PacketSetHwFilter(p->adapter,NDIS_PACKET_TYPE_PROMISCUOUS) == FALSE)
973 {
974 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "failed to set hardware filter to promiscuous mode");
975 goto bad;
976 }
977 }
978 else
979 {
980 if (PacketSetHwFilter(p->adapter,NDIS_PACKET_TYPE_ALL_LOCAL) == FALSE)
981 {
982 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "failed to set hardware filter to non-promiscuous mode");
983 goto bad;
984 }
985 }
986
987 /* Set the buffer size */
988 p->bufsize = WIN32_DEFAULT_USER_BUFFER_SIZE;
989
990 if(!(p->adapter->Flags & INFO_FLAG_DAG_CARD))
991 {
992 /*
993 * Traditional Adapter
994 */
995 /*
996 * If the buffer size wasn't explicitly set, default to
997 * WIN32_DEFAULT_KERNEL_BUFFER_SIZE.
998 */
999 if (p->opt.buffer_size == 0)
1000 p->opt.buffer_size = WIN32_DEFAULT_KERNEL_BUFFER_SIZE;
1001
1002 if(PacketSetBuff(p->adapter,p->opt.buffer_size)==FALSE)
1003 {
1004 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "driver error: not enough memory to allocate the kernel buffer");
1005 goto bad;
1006 }
1007
1008 p->buffer = malloc(p->bufsize);
1009 if (p->buffer == NULL)
1010 {
1011 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "malloc: %s", pcap_strerror(errno));
1012 goto bad;
1013 }
1014
1015 if (p->opt.immediate)
1016 {
1017 /* tell the driver to copy the buffer as soon as data arrives */
1018 if(PacketSetMinToCopy(p->adapter,0)==FALSE)
1019 {
1020 pcap_win32_err_to_str(GetLastError(), errbuf);
1021 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1022 "Error calling PacketSetMinToCopy: %s",
1023 errbuf);
1024 goto bad;
1025 }
1026 }
1027 else
1028 {
1029 /* tell the driver to copy the buffer only if it contains at least 16K */
1030 if(PacketSetMinToCopy(p->adapter,16000)==FALSE)
1031 {
1032 pcap_win32_err_to_str(GetLastError(), errbuf);
1033 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1034 "Error calling PacketSetMinToCopy: %s",
1035 errbuf);
1036 goto bad;
1037 }
1038 }
1039 }
1040 else
1041 #ifdef HAVE_DAG_API
1042 {
1043 /*
1044 * Dag Card
1045 */
1046 LONG status;
1047 HKEY dagkey;
1048 DWORD lptype;
1049 DWORD lpcbdata;
1050 int postype = 0;
1051 char keyname[512];
1052
1053 pcap_snprintf(keyname, sizeof(keyname), "%s\\CardParams\\%s",
1054 "SYSTEM\\CurrentControlSet\\Services\\DAG",
1055 strstr(_strlwr(p->opt.device), "dag"));
1056 do
1057 {
1058 status = RegOpenKeyEx(HKEY_LOCAL_MACHINE, keyname, 0, KEY_READ, &dagkey);
1059 if(status != ERROR_SUCCESS)
1060 break;
1061
1062 status = RegQueryValueEx(dagkey,
1063 "PosType",
1064 NULL,
1065 &lptype,
1066 (char*)&postype,
1067 &lpcbdata);
1068
1069 if(status != ERROR_SUCCESS)
1070 {
1071 postype = 0;
1072 }
1073
1074 RegCloseKey(dagkey);
1075 }
1076 while(FALSE);
1077
1078
1079 p->snapshot = PacketSetSnapLen(p->adapter, snaplen);
1080
1081 /* Set the length of the FCS associated to any packet. This value
1082 * will be subtracted to the packet length */
1083 pw->dag_fcs_bits = p->adapter->DagFcsLen;
1084 }
1085 #else
1086 goto bad;
1087 #endif /* HAVE_DAG_API */
1088
1089 PacketSetReadTimeout(p->adapter, p->opt.timeout);
1090
1091 #ifdef HAVE_DAG_API
1092 if(p->adapter->Flags & INFO_FLAG_DAG_CARD)
1093 {
1094 /* install dag specific handlers for read and setfilter */
1095 p->read_op = pcap_read_win32_dag;
1096 p->setfilter_op = pcap_setfilter_win32_dag;
1097 }
1098 else
1099 {
1100 #endif /* HAVE_DAG_API */
1101 /* install traditional npf handlers for read and setfilter */
1102 p->read_op = pcap_read_win32_npf;
1103 p->setfilter_op = pcap_setfilter_win32_npf;
1104 #ifdef HAVE_DAG_API
1105 }
1106 #endif /* HAVE_DAG_API */
1107 p->setdirection_op = NULL; /* Not implemented. */
1108 /* XXX - can this be implemented on some versions of Windows? */
1109 p->inject_op = pcap_inject_win32;
1110 p->set_datalink_op = NULL; /* can't change data link type */
1111 p->getnonblock_op = pcap_getnonblock_win32;
1112 p->setnonblock_op = pcap_setnonblock_win32;
1113 p->stats_op = pcap_stats_win32;
1114 p->stats_ex_op = pcap_stats_ex_win32;
1115 p->setbuff_op = pcap_setbuff_win32;
1116 p->setmode_op = pcap_setmode_win32;
1117 p->setmintocopy_op = pcap_setmintocopy_win32;
1118 p->getevent_op = pcap_getevent_win32;
1119 p->oid_get_request_op = pcap_oid_get_request_win32;
1120 p->oid_set_request_op = pcap_oid_set_request_win32;
1121 p->sendqueue_transmit_op = pcap_sendqueue_transmit_win32;
1122 p->setuserbuffer_op = pcap_setuserbuffer_win32;
1123 p->live_dump_op = pcap_live_dump_win32;
1124 p->live_dump_ended_op = pcap_live_dump_ended_win32;
1125 p->get_airpcap_handle_op = pcap_get_airpcap_handle_win32;
1126 p->cleanup_op = pcap_cleanup_win32;
1127
1128 return (0);
1129 bad:
1130 pcap_cleanup_win32(p);
1131 return (PCAP_ERROR);
1132 }
1133
1134 /*
1135 * Check if rfmon mode is supported on the pcap_t for Windows systems.
1136 */
1137 static int
1138 pcap_can_set_rfmon_win32(pcap_t *p)
1139 {
1140 return (PacketIsMonitorModeSupported(p->opt.device) == 1);
1141 }
1142
1143 pcap_t *
1144 pcap_create_interface(const char *device _U_, char *ebuf)
1145 {
1146 pcap_t *p;
1147 #ifdef HAVE_REMOTE
1148 p = pcap_create_common(ebuf, sizeof(struct pcap_win) + sizeof(struct pcap_md));
1149 #else
1150 p = pcap_create_common(ebuf, sizeof(struct pcap_win));
1151 #endif /* HAVE_REMOTE */
1152 if (p == NULL)
1153 return (NULL);
1154
1155 struct pcap_win *pw = p->priv;
1156 pw->rfmon_selfstart = 0;
1157 p->activate_op = pcap_activate_win32;
1158 p->can_set_rfmon_op = pcap_can_set_rfmon_win32;
1159 return (p);
1160 }
1161
1162 static int
1163 pcap_setfilter_win32_npf(pcap_t *p, struct bpf_program *fp)
1164 {
1165 struct pcap_win *pw = p->priv;
1166
1167 if(PacketSetBpf(p->adapter,fp)==FALSE){
1168 /*
1169 * Kernel filter not installed.
1170 *
1171 * XXX - we don't know whether this failed because:
1172 *
1173 * the kernel rejected the filter program as invalid,
1174 * in which case we should fall back on userland
1175 * filtering;
1176 *
1177 * the kernel rejected the filter program as too big,
1178 * in which case we should again fall back on
1179 * userland filtering;
1180 *
1181 * there was some other problem, in which case we
1182 * should probably report an error.
1183 *
1184 * For NPF devices, the Win32 status will be
1185 * STATUS_INVALID_DEVICE_REQUEST for invalid
1186 * filters, but I don't know what it'd be for
1187 * other problems, and for some other devices
1188 * it might not be set at all.
1189 *
1190 * So we just fall back on userland filtering in
1191 * all cases.
1192 */
1193
1194 /*
1195 * install_bpf_program() validates the program.
1196 *
1197 * XXX - what if we already have a filter in the kernel?
1198 */
1199 if (install_bpf_program(p, fp) < 0)
1200 return (-1);
1201 pw->filtering_in_kernel = 0; /* filtering in userland */
1202 return (0);
1203 }
1204
1205 /*
1206 * It worked.
1207 */
1208 pw->filtering_in_kernel = 1; /* filtering in the kernel */
1209
1210 /*
1211 * Discard any previously-received packets, as they might have
1212 * passed whatever filter was formerly in effect, but might
1213 * not pass this filter (BIOCSETF discards packets buffered
1214 * in the kernel, so you can lose packets in any case).
1215 */
1216 p->cc = 0;
1217 return (0);
1218 }
1219
1220 /*
1221 * We filter at user level, since the kernel driver does't process the packets
1222 */
1223 static int
1224 pcap_setfilter_win32_dag(pcap_t *p, struct bpf_program *fp) {
1225
1226 if(!fp)
1227 {
1228 strlcpy(p->errbuf, "setfilter: No filter specified", sizeof(p->errbuf));
1229 return (-1);
1230 }
1231
1232 /* Install a user level filter */
1233 if (install_bpf_program(p, fp) < 0)
1234 {
1235 pcap_snprintf(p->errbuf, sizeof(p->errbuf),
1236 "setfilter, unable to install the filter: %s", pcap_strerror(errno));
1237 return (-1);
1238 }
1239
1240 return (0);
1241 }
1242
1243 static int
1244 pcap_getnonblock_win32(pcap_t *p, char *errbuf)
1245 {
1246 struct pcap_win *pw = p->priv;
1247
1248 /*
1249 * XXX - if there were a PacketGetReadTimeout() call, we
1250 * would use it, and return 1 if the timeout is -1
1251 * and 0 otherwise.
1252 */
1253 return (pw->nonblock);
1254 }
1255
1256 static int
1257 pcap_setnonblock_win32(pcap_t *p, int nonblock, char *errbuf)
1258 {
1259 struct pcap_win *pw = p->priv;
1260 int newtimeout;
1261 char win_errbuf[PCAP_ERRBUF_SIZE+1];
1262
1263 if (nonblock) {
1264 /*
1265 * Set the read timeout to -1 for non-blocking mode.
1266 */
1267 newtimeout = -1;
1268 } else {
1269 /*
1270 * Restore the timeout set when the device was opened.
1271 * (Note that this may be -1, in which case we're not
1272 * really leaving non-blocking mode. However, although
1273 * the timeout argument to pcap_set_timeout() and
1274 * pcap_open_live() is an int, you're not supposed to
1275 * supply a negative value, so that "shouldn't happen".)
1276 */
1277 newtimeout = p->opt.timeout;
1278 }
1279 if (!PacketSetReadTimeout(p->adapter, newtimeout)) {
1280 pcap_win32_err_to_str(GetLastError(), win_errbuf);
1281 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
1282 "PacketSetReadTimeout: %s", win_errbuf);
1283 return (-1);
1284 }
1285 pw->nonblock = (newtimeout == -1);
1286 return (0);
1287 }
1288
1289 static int
1290 pcap_add_if_win32(pcap_if_t **devlist, char *name, bpf_u_int32 flags,
1291 const char *description, char *errbuf)
1292 {
1293 pcap_if_t *curdev;
1294 npf_if_addr if_addrs[MAX_NETWORK_ADDRESSES];
1295 LONG if_addr_size;
1296 int res = 0;
1297
1298 if_addr_size = MAX_NETWORK_ADDRESSES;
1299
1300 /*
1301 * Add an entry for this interface, with no addresses.
1302 */
1303 if (add_or_find_if(&curdev, devlist, name, flags, description,
1304 errbuf) == -1) {
1305 /*
1306 * Failure.
1307 */
1308 return (-1);
1309 }
1310
1311 /*
1312 * Get the list of addresses for the interface.
1313 */
1314 if (!PacketGetNetInfoEx((void *)name, if_addrs, &if_addr_size)) {
1315 /*
1316 * Failure.
1317 *
1318 * We don't return an error, because this can happen with
1319 * NdisWan interfaces, and we want to supply them even
1320 * if we can't supply their addresses.
1321 *
1322 * We return an entry with an empty address list.
1323 */
1324 return (0);
1325 }
1326
1327 /*
1328 * Now add the addresses.
1329 */
1330 while (if_addr_size-- > 0) {
1331 /*
1332 * "curdev" is an entry for this interface; add an entry for
1333 * this address to its list of addresses.
1334 */
1335 if(curdev == NULL)
1336 break;
1337 res = add_addr_to_dev(curdev,
1338 (struct sockaddr *)&if_addrs[if_addr_size].IPAddress,
1339 sizeof (struct sockaddr_storage),
1340 (struct sockaddr *)&if_addrs[if_addr_size].SubnetMask,
1341 sizeof (struct sockaddr_storage),
1342 (struct sockaddr *)&if_addrs[if_addr_size].Broadcast,
1343 sizeof (struct sockaddr_storage),
1344 NULL,
1345 0,
1346 errbuf);
1347 if (res == -1) {
1348 /*
1349 * Failure.
1350 */
1351 break;
1352 }
1353 }
1354
1355 return (res);
1356 }
1357
1358 int
1359 pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf)
1360 {
1361 pcap_if_t *devlist = NULL;
1362 int ret = 0;
1363 const char *desc;
1364 char *AdaptersName;
1365 ULONG NameLength;
1366 char *name;
1367 char our_errbuf[PCAP_ERRBUF_SIZE+1];
1368
1369 /*
1370 * Find out how big a buffer we need.
1371 *
1372 * This call should always return FALSE; if the error is
1373 * ERROR_INSUFFICIENT_BUFFER, NameLength will be set to
1374 * the size of the buffer we need, otherwise there's a
1375 * problem, and NameLength should be set to 0.
1376 *
1377 * It shouldn't require NameLength to be set, but,
1378 * at least as of WinPcap 4.1.3, it checks whether
1379 * NameLength is big enough before it checks for a
1380 * NULL buffer argument, so, while it'll still do
1381 * the right thing if NameLength is uninitialized and
1382 * whatever junk happens to be there is big enough
1383 * (because the pointer argument will be null), it's
1384 * still reading an uninitialized variable.
1385 */
1386 NameLength = 0;
1387 if (!PacketGetAdapterNames(NULL, &NameLength))
1388 {
1389 DWORD last_error = GetLastError();
1390
1391 if (last_error != ERROR_INSUFFICIENT_BUFFER)
1392 {
1393 pcap_win32_err_to_str(last_error, our_errbuf);
1394 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
1395 "PacketGetAdapterNames: %s", our_errbuf);
1396 return (-1);
1397 }
1398 }
1399
1400 if (NameLength > 0)
1401 AdaptersName = (char*) malloc(NameLength);
1402 else
1403 {
1404 *alldevsp = NULL;
1405 return 0;
1406 }
1407 if (AdaptersName == NULL)
1408 {
1409 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "Cannot allocate enough memory to list the adapters.");
1410 return (-1);
1411 }
1412
1413 if (!PacketGetAdapterNames(AdaptersName, &NameLength)) {
1414 pcap_win32_err_to_str(GetLastError(), our_errbuf);
1415 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "PacketGetAdapterNames: %s",
1416 our_errbuf);
1417 free(AdaptersName);
1418 return (-1);
1419 }
1420
1421 /*
1422 * "PacketGetAdapterNames()" returned a list of
1423 * null-terminated ASCII interface name strings,
1424 * terminated by a null string, followed by a list
1425 * of null-terminated ASCII interface description
1426 * strings, terminated by a null string.
1427 * This means there are two ASCII nulls at the end
1428 * of the first list.
1429 *
1430 * Find the end of the first list; that's the
1431 * beginning of the second list.
1432 */
1433 desc = &AdaptersName[0];
1434 while (*desc != '\0' || *(desc + 1) != '\0')
1435 desc++;
1436
1437 /*
1438 * Found it - "desc" points to the first of the two
1439 * nulls at the end of the list of names, so the
1440 * first byte of the list of descriptions is two bytes
1441 * after it.
1442 */
1443 desc += 2;
1444
1445 /*
1446 * Loop over the elements in the first list.
1447 */
1448 name = &AdaptersName[0];
1449 while (*name != '\0') {
1450 bpf_u_int32 flags = 0;
1451 #ifdef HAVE_PACKET_IS_LOOPBACK_ADAPTER
1452 pcap_t *p;
1453
1454 /*
1455 * Is this a loopback interface?
1456 * XXX - it'd be best if there were a way to get a list
1457 * of interfaces *and* flags, including "Up" and "Running"
1458 * as well as "loopback", without having to open the
1459 * interfaces.
1460 */
1461 p = pcap_open_live(name, 68, 0, 0, errbuf);
1462 if (p != NULL) {
1463 if (PacketIsLoopbackAdapter(p->adapter)) {
1464 /* Yes */
1465 flags |= PCAP_IF_LOOPBACK;
1466 }
1467 pcap_close(p);
1468 }
1469 #endif
1470
1471 /*
1472 * Add an entry for this interface.
1473 */
1474 if (pcap_add_if_win32(&devlist, name, flags, desc,
1475 errbuf) == -1) {
1476 /*
1477 * Failure.
1478 */
1479 ret = -1;
1480 break;
1481 }
1482 name += strlen(name) + 1;
1483 desc += strlen(desc) + 1;
1484 }
1485
1486 if (ret == -1) {
1487 /*
1488 * We had an error; free the list we've been constructing.
1489 */
1490 if (devlist != NULL) {
1491 pcap_freealldevs(devlist);
1492 devlist = NULL;
1493 }
1494 }
1495
1496 *alldevsp = devlist;
1497 free(AdaptersName);
1498 return (ret);
1499 }