]> The Tcpdump Group git mirrors - libpcap/blob - pcap-npf.c
811e80136429a80e3d9608eb4da05849e6f6ea17
[libpcap] / pcap-npf.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 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37
38 #include <errno.h>
39 #define PCAP_DONT_INCLUDE_PCAP_BPF_H
40 #include <Packet32.h>
41
42 /*
43 * XXX - TEMPORARY TO FIGURE OUT WHY WE'RE NOT GETTING WARNINGS ABOUT
44 * ASSIGNMENT OF A POINTER TO CONST CHAR VALUE TO A POINTER TO CHAR
45 * VARIABLE.
46 */
47 extern LPCSTR PacketGetLPCSTR(void);
48 extern const char *PacketGetConstCharStar(void);
49 #include <pcap-int.h>
50 #include <pcap/dlt.h>
51
52 /* Old-school MinGW have these headers in a different place.
53 */
54 #if defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)
55 #include <ddk/ntddndis.h>
56 #include <ddk/ndis.h>
57 #else
58 #include <ntddndis.h> /* MSVC/TDM-MinGW/MinGW64 */
59 #endif
60
61 #ifdef HAVE_DAG_API
62 #include <dagnew.h>
63 #include <dagapi.h>
64 #endif /* HAVE_DAG_API */
65
66 #include "diag-control.h"
67
68 #include "pcap-airpcap.h"
69
70 static int pcap_setfilter_npf(pcap_t *, struct bpf_program *);
71 static int pcap_setfilter_win32_dag(pcap_t *, struct bpf_program *);
72 static int pcap_getnonblock_npf(pcap_t *);
73 static int pcap_setnonblock_npf(pcap_t *, int);
74
75 /*dimension of the buffer in the pcap_t structure*/
76 #define WIN32_DEFAULT_USER_BUFFER_SIZE 256000
77
78 /*dimension of the buffer in the kernel driver NPF */
79 #define WIN32_DEFAULT_KERNEL_BUFFER_SIZE 1000000
80
81 /* Equivalent to ntohs(), but a lot faster under Windows */
82 #define SWAPS(_X) ((_X & 0xff) << 8) | (_X >> 8)
83
84 /*
85 * Private data for capturing on WinPcap/Npcap devices.
86 */
87 struct pcap_win {
88 ADAPTER *adapter; /* the packet32 ADAPTER for the device */
89 int nonblock;
90 int rfmon_selfstart; /* a flag tells whether the monitor mode is set by itself */
91 int filtering_in_kernel; /* using kernel filter */
92
93 #ifdef HAVE_DAG_API
94 int dag_fcs_bits; /* Number of checksum bits from link layer */
95 #endif
96
97 #ifdef ENABLE_REMOTE
98 int samp_npkt; /* parameter needed for sampling, with '1 out of N' method has been requested */
99 struct timeval samp_time; /* parameter needed for sampling, with '1 every N ms' method has been requested */
100 #endif
101 };
102
103 /*
104 * Define stub versions of the monitor-mode support routines if this
105 * isn't Npcap. HAVE_NPCAP_PACKET_API is defined by Npcap but not
106 * WinPcap.
107 */
108 #ifndef HAVE_NPCAP_PACKET_API
109 static int
110 PacketIsMonitorModeSupported(PCHAR AdapterName _U_)
111 {
112 /*
113 * We don't support monitor mode.
114 */
115 return (0);
116 }
117
118 static int
119 PacketSetMonitorMode(PCHAR AdapterName _U_, int mode _U_)
120 {
121 /*
122 * This should never be called, as PacketIsMonitorModeSupported()
123 * will return 0, meaning "we don't support monitor mode, so
124 * don't try to turn it on or off".
125 */
126 return (0);
127 }
128
129 static int
130 PacketGetMonitorMode(PCHAR AdapterName _U_)
131 {
132 /*
133 * This should fail, so that pcap_activate_npf() returns
134 * PCAP_ERROR_RFMON_NOTSUP if our caller requested monitor
135 * mode.
136 */
137 return (-1);
138 }
139 #endif
140
141 /*
142 * Sigh. PacketRequest() will have made a DeviceIoControl()
143 * call to the NPF driver to perform the OID request, with a
144 * BIOCQUERYOID ioctl. The kernel code should get back one
145 * of NDIS_STATUS_INVALID_OID, NDIS_STATUS_NOT_SUPPORTED,
146 * or NDIS_STATUS_NOT_RECOGNIZED if the OID request isn't
147 * supported by the OS or the driver, but that doesn't seem
148 * to make it to the caller of PacketRequest() in a
149 * reliable fashion.
150 */
151 #define NDIS_STATUS_INVALID_OID 0xc0010017
152 #define NDIS_STATUS_NOT_SUPPORTED 0xc00000bb /* STATUS_NOT_SUPPORTED */
153 #define NDIS_STATUS_NOT_RECOGNIZED 0x00010001
154
155 static int
156 oid_get_request(ADAPTER *adapter, bpf_u_int32 oid, void *data, size_t *lenp,
157 char *errbuf)
158 {
159 PACKET_OID_DATA *oid_data_arg;
160
161 /*
162 * Allocate a PACKET_OID_DATA structure to hand to PacketRequest().
163 * It should be big enough to hold "*lenp" bytes of data; it
164 * will actually be slightly larger, as PACKET_OID_DATA has a
165 * 1-byte data array at the end, standing in for the variable-length
166 * data that's actually there.
167 */
168 oid_data_arg = malloc(sizeof (PACKET_OID_DATA) + *lenp);
169 if (oid_data_arg == NULL) {
170 snprintf(errbuf, PCAP_ERRBUF_SIZE,
171 "Couldn't allocate argument buffer for PacketRequest");
172 return (PCAP_ERROR);
173 }
174
175 /*
176 * No need to copy the data - we're doing a fetch.
177 */
178 oid_data_arg->Oid = oid;
179 oid_data_arg->Length = (ULONG)(*lenp); /* XXX - check for ridiculously large value? */
180 if (!PacketRequest(adapter, FALSE, oid_data_arg)) {
181 pcap_fmt_errmsg_for_win32_err(errbuf, PCAP_ERRBUF_SIZE,
182 GetLastError(), "Error calling PacketRequest");
183 free(oid_data_arg);
184 return (-1);
185 }
186
187 /*
188 * Get the length actually supplied.
189 */
190 *lenp = oid_data_arg->Length;
191
192 /*
193 * Copy back the data we fetched.
194 */
195 memcpy(data, oid_data_arg->Data, *lenp);
196 free(oid_data_arg);
197 return (0);
198 }
199
200 static int
201 pcap_stats_npf(pcap_t *p, struct pcap_stat *ps)
202 {
203 struct pcap_win *pw = p->priv;
204 struct bpf_stat bstats;
205
206 /*
207 * Try to get statistics.
208 *
209 * (Please note - "struct pcap_stat" is *not* the same as
210 * WinPcap's "struct bpf_stat". It might currently have the
211 * same layout, but let's not cheat.
212 *
213 * Note also that we don't fill in ps_capt, as we might have
214 * been called by code compiled against an earlier version of
215 * WinPcap that didn't have ps_capt, in which case filling it
216 * in would stomp on whatever comes after the structure passed
217 * to us.
218 */
219 if (!PacketGetStats(pw->adapter, &bstats)) {
220 pcap_fmt_errmsg_for_win32_err(p->errbuf, PCAP_ERRBUF_SIZE,
221 GetLastError(), "PacketGetStats error");
222 return (-1);
223 }
224 ps->ps_recv = bstats.bs_recv;
225 ps->ps_drop = bstats.bs_drop;
226
227 /*
228 * XXX - PacketGetStats() doesn't fill this in, so we just
229 * return 0.
230 */
231 #if 0
232 ps->ps_ifdrop = bstats.ps_ifdrop;
233 #else
234 ps->ps_ifdrop = 0;
235 #endif
236
237 return (0);
238 }
239
240 /*
241 * Win32-only routine for getting statistics.
242 *
243 * This way is definitely safer than passing the pcap_stat * from the userland.
244 * In fact, there could happen than the user allocates a variable which is not
245 * big enough for the new structure, and the library will write in a zone
246 * which is not allocated to this variable.
247 *
248 * In this way, we're pretty sure we are writing on memory allocated to this
249 * variable.
250 *
251 * XXX - but this is the wrong way to handle statistics. Instead, we should
252 * have an API that returns data in a form like the Options section of a
253 * pcapng Interface Statistics Block:
254 *
255 * https://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
256 *
257 * which would let us add new statistics straightforwardly and indicate which
258 * statistics we are and are *not* providing, rather than having to provide
259 * possibly-bogus values for statistics we can't provide.
260 */
261 static struct pcap_stat *
262 pcap_stats_ex_npf(pcap_t *p, int *pcap_stat_size)
263 {
264 struct pcap_win *pw = p->priv;
265 struct bpf_stat bstats;
266
267 *pcap_stat_size = sizeof (p->stat);
268
269 /*
270 * Try to get statistics.
271 *
272 * (Please note - "struct pcap_stat" is *not* the same as
273 * WinPcap's "struct bpf_stat". It might currently have the
274 * same layout, but let's not cheat.)
275 */
276 if (!PacketGetStatsEx(pw->adapter, &bstats)) {
277 pcap_fmt_errmsg_for_win32_err(p->errbuf, PCAP_ERRBUF_SIZE,
278 GetLastError(), "PacketGetStatsEx error");
279 return (NULL);
280 }
281 p->stat.ps_recv = bstats.bs_recv;
282 p->stat.ps_drop = bstats.bs_drop;
283 p->stat.ps_ifdrop = bstats.ps_ifdrop;
284 /*
285 * Just in case this is ever compiled for a target other than
286 * Windows, which is somewhere between extemely unlikely and
287 * impossible.
288 */
289 #ifdef _WIN32
290 p->stat.ps_capt = bstats.bs_capt;
291 #endif
292 return (&p->stat);
293 }
294
295 /* Set the dimension of the kernel-level capture buffer */
296 static int
297 pcap_setbuff_npf(pcap_t *p, int dim)
298 {
299 struct pcap_win *pw = p->priv;
300
301 if(PacketSetBuff(pw->adapter,dim)==FALSE)
302 {
303 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "driver error: not enough memory to allocate the kernel buffer");
304 return (-1);
305 }
306 return (0);
307 }
308
309 /* Set the driver working mode */
310 static int
311 pcap_setmode_npf(pcap_t *p, int mode)
312 {
313 struct pcap_win *pw = p->priv;
314
315 if(PacketSetMode(pw->adapter,mode)==FALSE)
316 {
317 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "driver error: working mode not recognized");
318 return (-1);
319 }
320
321 return (0);
322 }
323
324 /*set the minimum amount of data that will release a read call*/
325 static int
326 pcap_setmintocopy_npf(pcap_t *p, int size)
327 {
328 struct pcap_win *pw = p->priv;
329
330 if(PacketSetMinToCopy(pw->adapter, size)==FALSE)
331 {
332 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "driver error: unable to set the requested mintocopy size");
333 return (-1);
334 }
335 return (0);
336 }
337
338 static HANDLE
339 pcap_getevent_npf(pcap_t *p)
340 {
341 struct pcap_win *pw = p->priv;
342
343 return (PacketGetReadEvent(pw->adapter));
344 }
345
346 static int
347 pcap_oid_get_request_npf(pcap_t *p, bpf_u_int32 oid, void *data, size_t *lenp)
348 {
349 struct pcap_win *pw = p->priv;
350
351 return (oid_get_request(pw->adapter, oid, data, lenp, p->errbuf));
352 }
353
354 static int
355 pcap_oid_set_request_npf(pcap_t *p, bpf_u_int32 oid, const void *data,
356 size_t *lenp)
357 {
358 struct pcap_win *pw = p->priv;
359 PACKET_OID_DATA *oid_data_arg;
360
361 /*
362 * Allocate a PACKET_OID_DATA structure to hand to PacketRequest().
363 * It should be big enough to hold "*lenp" bytes of data; it
364 * will actually be slightly larger, as PACKET_OID_DATA has a
365 * 1-byte data array at the end, standing in for the variable-length
366 * data that's actually there.
367 */
368 oid_data_arg = malloc(sizeof (PACKET_OID_DATA) + *lenp);
369 if (oid_data_arg == NULL) {
370 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
371 "Couldn't allocate argument buffer for PacketRequest");
372 return (PCAP_ERROR);
373 }
374
375 oid_data_arg->Oid = oid;
376 oid_data_arg->Length = (ULONG)(*lenp); /* XXX - check for ridiculously large value? */
377 memcpy(oid_data_arg->Data, data, *lenp);
378 if (!PacketRequest(pw->adapter, TRUE, oid_data_arg)) {
379 pcap_fmt_errmsg_for_win32_err(p->errbuf, PCAP_ERRBUF_SIZE,
380 GetLastError(), "Error calling PacketRequest");
381 free(oid_data_arg);
382 return (PCAP_ERROR);
383 }
384
385 /*
386 * Get the length actually copied.
387 */
388 *lenp = oid_data_arg->Length;
389
390 /*
391 * No need to copy the data - we're doing a set.
392 */
393 free(oid_data_arg);
394 return (0);
395 }
396
397 static u_int
398 pcap_sendqueue_transmit_npf(pcap_t *p, pcap_send_queue *queue, int sync)
399 {
400 struct pcap_win *pw = p->priv;
401 u_int res;
402
403 res = PacketSendPackets(pw->adapter,
404 queue->buffer,
405 queue->len,
406 (BOOLEAN)sync);
407
408 if(res != queue->len){
409 pcap_fmt_errmsg_for_win32_err(p->errbuf, PCAP_ERRBUF_SIZE,
410 GetLastError(), "Error queueing packets");
411 }
412
413 return (res);
414 }
415
416 static int
417 pcap_setuserbuffer_npf(pcap_t *p, int size)
418 {
419 unsigned char *new_buff;
420
421 if (size<=0) {
422 /* Bogus parameter */
423 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
424 "Error: invalid size %d",size);
425 return (-1);
426 }
427
428 /* Allocate the buffer */
429 new_buff=(unsigned char*)malloc(sizeof(char)*size);
430
431 if (!new_buff) {
432 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
433 "Error: not enough memory");
434 return (-1);
435 }
436
437 free(p->buffer);
438
439 p->buffer=new_buff;
440 p->bufsize=size;
441
442 return (0);
443 }
444
445 static int
446 pcap_live_dump_npf(pcap_t *p, char *filename, int maxsize, int maxpacks)
447 {
448 struct pcap_win *pw = p->priv;
449 BOOLEAN res;
450
451 /* Set the packet driver in dump mode */
452 res = PacketSetMode(pw->adapter, PACKET_MODE_DUMP);
453 if(res == FALSE){
454 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
455 "Error setting dump mode");
456 return (-1);
457 }
458
459 /* Set the name of the dump file */
460 res = PacketSetDumpName(pw->adapter, filename, (int)strlen(filename));
461 if(res == FALSE){
462 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
463 "Error setting kernel dump file name");
464 return (-1);
465 }
466
467 /* Set the limits of the dump file */
468 res = PacketSetDumpLimits(pw->adapter, maxsize, maxpacks);
469 if(res == FALSE) {
470 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
471 "Error setting dump limit");
472 return (-1);
473 }
474
475 return (0);
476 }
477
478 static int
479 pcap_live_dump_ended_npf(pcap_t *p, int sync)
480 {
481 struct pcap_win *pw = p->priv;
482
483 return (PacketIsDumpEnded(pw->adapter, (BOOLEAN)sync));
484 }
485
486 #ifdef HAVE_AIRPCAP_API
487 static PAirpcapHandle
488 pcap_get_airpcap_handle_npf(pcap_t *p)
489 {
490 struct pcap_win *pw = p->priv;
491
492 return (PacketGetAirPcapHandle(pw->adapter));
493 }
494 #else /* HAVE_AIRPCAP_API */
495 static PAirpcapHandle
496 pcap_get_airpcap_handle_npf(pcap_t *p _U_)
497 {
498 return (NULL);
499 }
500 #endif /* HAVE_AIRPCAP_API */
501
502 static int
503 pcap_read_npf(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
504 {
505 PACKET Packet;
506 int cc;
507 int n;
508 register u_char *bp, *ep;
509 u_char *datap;
510 struct pcap_win *pw = p->priv;
511
512 cc = p->cc;
513 if (cc == 0) {
514 /*
515 * Has "pcap_breakloop()" been called?
516 */
517 if (p->break_loop) {
518 /*
519 * Yes - clear the flag that indicates that it
520 * has, and return PCAP_ERROR_BREAK to indicate
521 * that we were told to break out of the loop.
522 */
523 p->break_loop = 0;
524 return (PCAP_ERROR_BREAK);
525 }
526
527 /*
528 * Capture the packets.
529 *
530 * The PACKET structure had a bunch of extra stuff for
531 * Windows 9x/Me, but the only interesting data in it
532 * in the versions of Windows that we support is just
533 * a copy of p->buffer, a copy of p->buflen, and the
534 * actual number of bytes read returned from
535 * PacketReceivePacket(), none of which has to be
536 * retained from call to call, so we just keep one on
537 * the stack.
538 */
539 PacketInitPacket(&Packet, (BYTE *)p->buffer, p->bufsize);
540 if (!PacketReceivePacket(pw->adapter, &Packet, TRUE)) {
541 /*
542 * Did the device go away?
543 * If so, the error we get is ERROR_GEN_FAILURE.
544 */
545 DWORD errcode = GetLastError();
546
547 if (errcode == ERROR_GEN_FAILURE) {
548 /*
549 * The device on which we're capturing
550 * went away, or it became unusable
551 * by NPF due to a suspend/resume.
552 *
553 * XXX - hopefully no other error
554 * conditions are indicated by this.
555 *
556 * XXX - we really should return an
557 * appropriate error for that, but
558 * pcap_dispatch() etc. aren't
559 * documented as having error returns
560 * other than PCAP_ERROR or PCAP_ERROR_BREAK.
561 */
562 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
563 "The interface disappeared");
564 } else {
565 pcap_fmt_errmsg_for_win32_err(p->errbuf,
566 PCAP_ERRBUF_SIZE, errcode,
567 "PacketReceivePacket error");
568 }
569 return (PCAP_ERROR);
570 }
571
572 cc = Packet.ulBytesReceived;
573
574 bp = p->buffer;
575 }
576 else
577 bp = p->bp;
578
579 /*
580 * Loop through each packet.
581 */
582 #define bhp ((struct bpf_hdr *)bp)
583 n = 0;
584 ep = bp + cc;
585 for (;;) {
586 register u_int caplen, hdrlen;
587
588 /*
589 * Has "pcap_breakloop()" been called?
590 * If so, return immediately - if we haven't read any
591 * packets, clear the flag and return PCAP_ERROR_BREAK
592 * to indicate that we were told to break out of the loop,
593 * otherwise leave the flag set, so that the *next* call
594 * will break out of the loop without having read any
595 * packets, and return the number of packets we've
596 * processed so far.
597 */
598 if (p->break_loop) {
599 if (n == 0) {
600 p->break_loop = 0;
601 return (PCAP_ERROR_BREAK);
602 } else {
603 p->bp = bp;
604 p->cc = (int) (ep - bp);
605 return (n);
606 }
607 }
608 if (bp >= ep)
609 break;
610
611 caplen = bhp->bh_caplen;
612 hdrlen = bhp->bh_hdrlen;
613 datap = bp + hdrlen;
614
615 /*
616 * Short-circuit evaluation: if using BPF filter
617 * in kernel, no need to do it now - we already know
618 * the packet passed the filter.
619 *
620 * XXX - pcap_filter() should always return TRUE if
621 * handed a null pointer for the program, but it might
622 * just try to "run" the filter, so we check here.
623 */
624 if (pw->filtering_in_kernel ||
625 p->fcode.bf_insns == NULL ||
626 pcap_filter(p->fcode.bf_insns, datap, bhp->bh_datalen, caplen)) {
627 #ifdef ENABLE_REMOTE
628 switch (p->rmt_samp.method) {
629
630 case PCAP_SAMP_1_EVERY_N:
631 pw->samp_npkt = (pw->samp_npkt + 1) % p->rmt_samp.value;
632
633 /* Discard all packets that are not '1 out of N' */
634 if (pw->samp_npkt != 0) {
635 bp += Packet_WORDALIGN(caplen + hdrlen);
636 continue;
637 }
638 break;
639
640 case PCAP_SAMP_FIRST_AFTER_N_MS:
641 {
642 struct pcap_pkthdr *pkt_header = (struct pcap_pkthdr*) bp;
643
644 /*
645 * Check if the timestamp of the arrived
646 * packet is smaller than our target time.
647 */
648 if (pkt_header->ts.tv_sec < pw->samp_time.tv_sec ||
649 (pkt_header->ts.tv_sec == pw->samp_time.tv_sec && pkt_header->ts.tv_usec < pw->samp_time.tv_usec)) {
650 bp += Packet_WORDALIGN(caplen + hdrlen);
651 continue;
652 }
653
654 /*
655 * The arrived packet is suitable for being
656 * delivered to our caller, so let's update
657 * the target time.
658 */
659 pw->samp_time.tv_usec = pkt_header->ts.tv_usec + p->rmt_samp.value * 1000;
660 if (pw->samp_time.tv_usec > 1000000) {
661 pw->samp_time.tv_sec = pkt_header->ts.tv_sec + pw->samp_time.tv_usec / 1000000;
662 pw->samp_time.tv_usec = pw->samp_time.tv_usec % 1000000;
663 }
664 }
665 }
666 #endif /* ENABLE_REMOTE */
667
668 /*
669 * XXX A bpf_hdr matches a pcap_pkthdr.
670 */
671 (*callback)(user, (struct pcap_pkthdr*)bp, datap);
672 bp += Packet_WORDALIGN(caplen + hdrlen);
673 if (++n >= cnt && !PACKET_COUNT_IS_UNLIMITED(cnt)) {
674 p->bp = bp;
675 p->cc = (int) (ep - bp);
676 return (n);
677 }
678 } else {
679 /*
680 * Skip this packet.
681 */
682 bp += Packet_WORDALIGN(caplen + hdrlen);
683 }
684 }
685 #undef bhp
686 p->cc = 0;
687 return (n);
688 }
689
690 #ifdef HAVE_DAG_API
691 static int
692 pcap_read_win32_dag(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
693 {
694 struct pcap_win *pw = p->priv;
695 PACKET Packet;
696 u_char *dp = NULL;
697 int packet_len = 0, caplen = 0;
698 struct pcap_pkthdr pcap_header;
699 u_char *endofbuf;
700 int n = 0;
701 dag_record_t *header;
702 unsigned erf_record_len;
703 ULONGLONG ts;
704 int cc;
705 unsigned swt;
706 unsigned dfp = pw->adapter->DagFastProcess;
707
708 cc = p->cc;
709 if (cc == 0) /* Get new packets only if we have processed all the ones of the previous read */
710 {
711 /*
712 * Get new packets from the network.
713 *
714 * The PACKET structure had a bunch of extra stuff for
715 * Windows 9x/Me, but the only interesting data in it
716 * in the versions of Windows that we support is just
717 * a copy of p->buffer, a copy of p->buflen, and the
718 * actual number of bytes read returned from
719 * PacketReceivePacket(), none of which has to be
720 * retained from call to call, so we just keep one on
721 * the stack.
722 */
723 PacketInitPacket(&Packet, (BYTE *)p->buffer, p->bufsize);
724 if (!PacketReceivePacket(pw->adapter, &Packet, TRUE)) {
725 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "read error: PacketReceivePacket failed");
726 return (-1);
727 }
728
729 cc = Packet.ulBytesReceived;
730 if(cc == 0)
731 /* The timeout has expired but we no packets arrived */
732 return (0);
733 header = (dag_record_t*)pw->adapter->DagBuffer;
734 }
735 else
736 header = (dag_record_t*)p->bp;
737
738 endofbuf = (char*)header + cc;
739
740 /*
741 * Cycle through the packets
742 */
743 do
744 {
745 erf_record_len = SWAPS(header->rlen);
746 if((char*)header + erf_record_len > endofbuf)
747 break;
748
749 /* Increase the number of captured packets */
750 p->stat.ps_recv++;
751
752 /* Find the beginning of the packet */
753 dp = ((u_char *)header) + dag_record_size;
754
755 /* Determine actual packet len */
756 switch(header->type)
757 {
758 case TYPE_ATM:
759 packet_len = ATM_SNAPLEN;
760 caplen = ATM_SNAPLEN;
761 dp += 4;
762
763 break;
764
765 case TYPE_ETH:
766 swt = SWAPS(header->wlen);
767 packet_len = swt - (pw->dag_fcs_bits);
768 caplen = erf_record_len - dag_record_size - 2;
769 if (caplen > packet_len)
770 {
771 caplen = packet_len;
772 }
773 dp += 2;
774
775 break;
776
777 case TYPE_HDLC_POS:
778 swt = SWAPS(header->wlen);
779 packet_len = swt - (pw->dag_fcs_bits);
780 caplen = erf_record_len - dag_record_size;
781 if (caplen > packet_len)
782 {
783 caplen = packet_len;
784 }
785
786 break;
787 }
788
789 if(caplen > p->snapshot)
790 caplen = p->snapshot;
791
792 /*
793 * Has "pcap_breakloop()" been called?
794 * If so, return immediately - if we haven't read any
795 * packets, clear the flag and return -2 to indicate
796 * that we were told to break out of the loop, otherwise
797 * leave the flag set, so that the *next* call will break
798 * out of the loop without having read any packets, and
799 * return the number of packets we've processed so far.
800 */
801 if (p->break_loop)
802 {
803 if (n == 0)
804 {
805 p->break_loop = 0;
806 return (-2);
807 }
808 else
809 {
810 p->bp = (char*)header;
811 p->cc = endofbuf - (char*)header;
812 return (n);
813 }
814 }
815
816 if(!dfp)
817 {
818 /* convert between timestamp formats */
819 ts = header->ts;
820 pcap_header.ts.tv_sec = (int)(ts >> 32);
821 ts = (ts & 0xffffffffi64) * 1000000;
822 ts += 0x80000000; /* rounding */
823 pcap_header.ts.tv_usec = (int)(ts >> 32);
824 if (pcap_header.ts.tv_usec >= 1000000) {
825 pcap_header.ts.tv_usec -= 1000000;
826 pcap_header.ts.tv_sec++;
827 }
828 }
829
830 /* No underlaying filtering system. We need to filter on our own */
831 if (p->fcode.bf_insns)
832 {
833 if (pcap_filter(p->fcode.bf_insns, dp, packet_len, caplen) == 0)
834 {
835 /* Move to next packet */
836 header = (dag_record_t*)((char*)header + erf_record_len);
837 continue;
838 }
839 }
840
841 /* Fill the header for the user suppplied callback function */
842 pcap_header.caplen = caplen;
843 pcap_header.len = packet_len;
844
845 /* Call the callback function */
846 (*callback)(user, &pcap_header, dp);
847
848 /* Move to next packet */
849 header = (dag_record_t*)((char*)header + erf_record_len);
850
851 /* Stop if the number of packets requested by user has been reached*/
852 if (++n >= cnt && !PACKET_COUNT_IS_UNLIMITED(cnt))
853 {
854 p->bp = (char*)header;
855 p->cc = endofbuf - (char*)header;
856 return (n);
857 }
858 }
859 while((u_char*)header < endofbuf);
860
861 return (1);
862 }
863 #endif /* HAVE_DAG_API */
864
865 /* Send a packet to the network */
866 static int
867 pcap_inject_npf(pcap_t *p, const void *buf, int size)
868 {
869 struct pcap_win *pw = p->priv;
870 PACKET pkt;
871
872 PacketInitPacket(&pkt, (PVOID)buf, size);
873 if(PacketSendPacket(pw->adapter,&pkt,TRUE) == FALSE) {
874 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send error: PacketSendPacket failed");
875 return (-1);
876 }
877
878 /*
879 * We assume it all got sent if "PacketSendPacket()" succeeded.
880 * "pcap_inject()" is expected to return the number of bytes
881 * sent.
882 */
883 return (size);
884 }
885
886 static void
887 pcap_cleanup_npf(pcap_t *p)
888 {
889 struct pcap_win *pw = p->priv;
890
891 if (pw->adapter != NULL) {
892 PacketCloseAdapter(pw->adapter);
893 pw->adapter = NULL;
894 }
895 if (pw->rfmon_selfstart)
896 {
897 PacketSetMonitorMode(p->opt.device, 0);
898 }
899 pcap_cleanup_live_common(p);
900 }
901
902 static void
903 pcap_breakloop_npf(pcap_t *p)
904 {
905 pcap_breakloop_common(p);
906 struct pcap_win *pw = p->priv;
907
908 /* XXX - what if this fails? */
909 SetEvent(PacketGetReadEvent(pw->adapter));
910 }
911
912 static int
913 pcap_activate_npf(pcap_t *p)
914 {
915 struct pcap_win *pw = p->priv;
916 NetType type;
917 int res;
918 int status = 0;
919
920 if (p->opt.rfmon) {
921 /*
922 * Monitor mode is supported on Windows Vista and later.
923 */
924 if (PacketGetMonitorMode(p->opt.device) == 1)
925 {
926 pw->rfmon_selfstart = 0;
927 }
928 else
929 {
930 if ((res = PacketSetMonitorMode(p->opt.device, 1)) != 1)
931 {
932 pw->rfmon_selfstart = 0;
933 // Monitor mode is not supported.
934 if (res == 0)
935 {
936 return PCAP_ERROR_RFMON_NOTSUP;
937 }
938 else
939 {
940 return PCAP_ERROR;
941 }
942 }
943 else
944 {
945 pw->rfmon_selfstart = 1;
946 }
947 }
948 }
949
950 /* Init Winsock if it hasn't already been initialized */
951 pcap_wsockinit();
952
953 pw->adapter = PacketOpenAdapter(p->opt.device);
954
955 if (pw->adapter == NULL)
956 {
957 DWORD errcode = GetLastError();
958
959 /*
960 * What error did we get when trying to open the adapter?
961 */
962 switch (errcode) {
963
964 case ERROR_BAD_UNIT:
965 /*
966 * There's no such device.
967 */
968 return (PCAP_ERROR_NO_SUCH_DEVICE);
969
970 case ERROR_ACCESS_DENIED:
971 /*
972 * There is, but we don't have permission to
973 * use it.
974 */
975 return (PCAP_ERROR_PERM_DENIED);
976
977 default:
978 /*
979 * Unknown - report details.
980 */
981 pcap_fmt_errmsg_for_win32_err(p->errbuf, PCAP_ERRBUF_SIZE,
982 errcode, "Error opening adapter");
983 if (pw->rfmon_selfstart)
984 {
985 PacketSetMonitorMode(p->opt.device, 0);
986 }
987 return (PCAP_ERROR);
988 }
989 }
990
991 /*get network type*/
992 if(PacketGetNetType (pw->adapter,&type) == FALSE)
993 {
994 pcap_fmt_errmsg_for_win32_err(p->errbuf, PCAP_ERRBUF_SIZE,
995 GetLastError(), "Cannot determine the network type");
996 goto bad;
997 }
998
999 /*Set the linktype*/
1000 switch (type.LinkType)
1001 {
1002 case NdisMediumWan:
1003 p->linktype = DLT_EN10MB;
1004 break;
1005
1006 case NdisMedium802_3:
1007 p->linktype = DLT_EN10MB;
1008 /*
1009 * This is (presumably) a real Ethernet capture; give it a
1010 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
1011 * that an application can let you choose it, in case you're
1012 * capturing DOCSIS traffic that a Cisco Cable Modem
1013 * Termination System is putting out onto an Ethernet (it
1014 * doesn't put an Ethernet header onto the wire, it puts raw
1015 * DOCSIS frames out on the wire inside the low-level
1016 * Ethernet framing).
1017 */
1018 p->dlt_list = (u_int *) malloc(sizeof(u_int) * 2);
1019 /*
1020 * If that fails, just leave the list empty.
1021 */
1022 if (p->dlt_list != NULL) {
1023 p->dlt_list[0] = DLT_EN10MB;
1024 p->dlt_list[1] = DLT_DOCSIS;
1025 p->dlt_count = 2;
1026 }
1027 break;
1028
1029 case NdisMediumFddi:
1030 p->linktype = DLT_FDDI;
1031 break;
1032
1033 case NdisMedium802_5:
1034 p->linktype = DLT_IEEE802;
1035 break;
1036
1037 case NdisMediumArcnetRaw:
1038 p->linktype = DLT_ARCNET;
1039 break;
1040
1041 case NdisMediumArcnet878_2:
1042 p->linktype = DLT_ARCNET;
1043 break;
1044
1045 case NdisMediumAtm:
1046 p->linktype = DLT_ATM_RFC1483;
1047 break;
1048
1049 case NdisMediumCHDLC:
1050 p->linktype = DLT_CHDLC;
1051 break;
1052
1053 case NdisMediumPPPSerial:
1054 p->linktype = DLT_PPP_SERIAL;
1055 break;
1056
1057 case NdisMediumNull:
1058 p->linktype = DLT_NULL;
1059 break;
1060
1061 case NdisMediumBare80211:
1062 p->linktype = DLT_IEEE802_11;
1063 break;
1064
1065 case NdisMediumRadio80211:
1066 p->linktype = DLT_IEEE802_11_RADIO;
1067 break;
1068
1069 case NdisMediumPpi:
1070 p->linktype = DLT_PPI;
1071 break;
1072
1073 case NdisMediumWirelessWan:
1074 p->linktype = DLT_RAW;
1075 break;
1076
1077 default:
1078 /*
1079 * An unknown medium type is assumed to supply Ethernet
1080 * headers; if not, the user will have to report it,
1081 * so that the medium type and link-layer header type
1082 * can be determined. If we were to fail here, we
1083 * might get the link-layer type in the error, but
1084 * the user wouldn't get a capture, so we wouldn't
1085 * be able to determine the link-layer type; we report
1086 * a warning with the link-layer type, so at least
1087 * some programs will report the warning.
1088 */
1089 p->linktype = DLT_EN10MB;
1090 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1091 "Unknown NdisMedium value %d, defaulting to DLT_EN10MB",
1092 type.LinkType);
1093 status = PCAP_WARNING;
1094 break;
1095 }
1096
1097 /*
1098 * Turn a negative snapshot value (invalid), a snapshot value of
1099 * 0 (unspecified), or a value bigger than the normal maximum
1100 * value, into the maximum allowed value.
1101 *
1102 * If some application really *needs* a bigger snapshot
1103 * length, we should just increase MAXIMUM_SNAPLEN.
1104 */
1105 if (p->snapshot <= 0 || p->snapshot > MAXIMUM_SNAPLEN)
1106 p->snapshot = MAXIMUM_SNAPLEN;
1107
1108 /* Set promiscuous mode */
1109 if (p->opt.promisc)
1110 {
1111
1112 if (PacketSetHwFilter(pw->adapter,NDIS_PACKET_TYPE_PROMISCUOUS) == FALSE)
1113 {
1114 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "failed to set hardware filter to promiscuous mode");
1115 goto bad;
1116 }
1117 }
1118 else
1119 {
1120 /* NDIS_PACKET_TYPE_ALL_LOCAL selects "All packets sent by installed
1121 * protocols and all packets indicated by the NIC" but if no protocol
1122 * drivers (like TCP/IP) are installed, NDIS_PACKET_TYPE_DIRECTED,
1123 * NDIS_PACKET_TYPE_BROADCAST, and NDIS_PACKET_TYPE_MULTICAST are needed to
1124 * capture incoming frames.
1125 */
1126 if (PacketSetHwFilter(pw->adapter,
1127 NDIS_PACKET_TYPE_ALL_LOCAL |
1128 NDIS_PACKET_TYPE_DIRECTED |
1129 NDIS_PACKET_TYPE_BROADCAST |
1130 NDIS_PACKET_TYPE_MULTICAST) == FALSE)
1131 {
1132 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "failed to set hardware filter to non-promiscuous mode");
1133 goto bad;
1134 }
1135 }
1136
1137 /* Set the buffer size */
1138 p->bufsize = WIN32_DEFAULT_USER_BUFFER_SIZE;
1139
1140 if(!(pw->adapter->Flags & INFO_FLAG_DAG_CARD))
1141 {
1142 /*
1143 * Traditional Adapter
1144 */
1145 /*
1146 * If the buffer size wasn't explicitly set, default to
1147 * WIN32_DEFAULT_KERNEL_BUFFER_SIZE.
1148 */
1149 if (p->opt.buffer_size == 0)
1150 p->opt.buffer_size = WIN32_DEFAULT_KERNEL_BUFFER_SIZE;
1151
1152 if(PacketSetBuff(pw->adapter,p->opt.buffer_size)==FALSE)
1153 {
1154 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "driver error: not enough memory to allocate the kernel buffer");
1155 goto bad;
1156 }
1157
1158 p->buffer = malloc(p->bufsize);
1159 if (p->buffer == NULL)
1160 {
1161 pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
1162 errno, "malloc");
1163 goto bad;
1164 }
1165
1166 if (p->opt.immediate)
1167 {
1168 /* tell the driver to copy the buffer as soon as data arrives */
1169 if(PacketSetMinToCopy(pw->adapter,0)==FALSE)
1170 {
1171 pcap_fmt_errmsg_for_win32_err(p->errbuf,
1172 PCAP_ERRBUF_SIZE, GetLastError(),
1173 "Error calling PacketSetMinToCopy");
1174 goto bad;
1175 }
1176 }
1177 else
1178 {
1179 /* tell the driver to copy the buffer only if it contains at least 16K */
1180 if(PacketSetMinToCopy(pw->adapter,16000)==FALSE)
1181 {
1182 pcap_fmt_errmsg_for_win32_err(p->errbuf,
1183 PCAP_ERRBUF_SIZE, GetLastError(),
1184 "Error calling PacketSetMinToCopy");
1185 goto bad;
1186 }
1187 }
1188 } else {
1189 /*
1190 * Dag Card
1191 */
1192 #ifdef HAVE_DAG_API
1193 /*
1194 * We have DAG support.
1195 */
1196 LONG status;
1197 HKEY dagkey;
1198 DWORD lptype;
1199 DWORD lpcbdata;
1200 int postype = 0;
1201 char keyname[512];
1202
1203 snprintf(keyname, sizeof(keyname), "%s\\CardParams\\%s",
1204 "SYSTEM\\CurrentControlSet\\Services\\DAG",
1205 strstr(_strlwr(p->opt.device), "dag"));
1206 do
1207 {
1208 status = RegOpenKeyEx(HKEY_LOCAL_MACHINE, keyname, 0, KEY_READ, &dagkey);
1209 if(status != ERROR_SUCCESS)
1210 break;
1211
1212 status = RegQueryValueEx(dagkey,
1213 "PosType",
1214 NULL,
1215 &lptype,
1216 (char*)&postype,
1217 &lpcbdata);
1218
1219 if(status != ERROR_SUCCESS)
1220 {
1221 postype = 0;
1222 }
1223
1224 RegCloseKey(dagkey);
1225 }
1226 while(FALSE);
1227
1228
1229 p->snapshot = PacketSetSnapLen(pw->adapter, p->snapshot);
1230
1231 /* Set the length of the FCS associated to any packet. This value
1232 * will be subtracted to the packet length */
1233 pw->dag_fcs_bits = pw->adapter->DagFcsLen;
1234 #else /* HAVE_DAG_API */
1235 /*
1236 * No DAG support.
1237 */
1238 goto bad;
1239 #endif /* HAVE_DAG_API */
1240 }
1241
1242 PacketSetReadTimeout(pw->adapter, p->opt.timeout);
1243
1244 /* disable loopback capture if requested */
1245 if (p->opt.nocapture_local)
1246 {
1247 if (!PacketSetLoopbackBehavior(pw->adapter, NPF_DISABLE_LOOPBACK))
1248 {
1249 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1250 "Unable to disable the capture of loopback packets.");
1251 goto bad;
1252 }
1253 }
1254
1255 #ifdef HAVE_DAG_API
1256 if(pw->adapter->Flags & INFO_FLAG_DAG_CARD)
1257 {
1258 /* install dag specific handlers for read and setfilter */
1259 p->read_op = pcap_read_win32_dag;
1260 p->setfilter_op = pcap_setfilter_win32_dag;
1261 }
1262 else
1263 {
1264 #endif /* HAVE_DAG_API */
1265 /* install traditional npf handlers for read and setfilter */
1266 p->read_op = pcap_read_npf;
1267 p->setfilter_op = pcap_setfilter_npf;
1268 #ifdef HAVE_DAG_API
1269 }
1270 #endif /* HAVE_DAG_API */
1271 p->setdirection_op = NULL; /* Not implemented. */
1272 /* XXX - can this be implemented on some versions of Windows? */
1273 p->inject_op = pcap_inject_npf;
1274 p->set_datalink_op = NULL; /* can't change data link type */
1275 p->getnonblock_op = pcap_getnonblock_npf;
1276 p->setnonblock_op = pcap_setnonblock_npf;
1277 p->stats_op = pcap_stats_npf;
1278 p->breakloop_op = pcap_breakloop_npf;
1279 p->stats_ex_op = pcap_stats_ex_npf;
1280 p->setbuff_op = pcap_setbuff_npf;
1281 p->setmode_op = pcap_setmode_npf;
1282 p->setmintocopy_op = pcap_setmintocopy_npf;
1283 p->getevent_op = pcap_getevent_npf;
1284 p->oid_get_request_op = pcap_oid_get_request_npf;
1285 p->oid_set_request_op = pcap_oid_set_request_npf;
1286 p->sendqueue_transmit_op = pcap_sendqueue_transmit_npf;
1287 p->setuserbuffer_op = pcap_setuserbuffer_npf;
1288 p->live_dump_op = pcap_live_dump_npf;
1289 p->live_dump_ended_op = pcap_live_dump_ended_npf;
1290 p->get_airpcap_handle_op = pcap_get_airpcap_handle_npf;
1291 p->cleanup_op = pcap_cleanup_npf;
1292
1293 /*
1294 * XXX - this is only done because WinPcap supported
1295 * pcap_fileno() returning the hFile HANDLE from the
1296 * ADAPTER structure. We make no general guarantees
1297 * that the caller can do anything useful with it.
1298 *
1299 * (Not that we make any general guarantee of that
1300 * sort on UN*X, either, any more, given that not
1301 * all capture devices are regular OS network
1302 * interfaces.)
1303 */
1304 p->handle = pw->adapter->hFile;
1305
1306 return (status);
1307 bad:
1308 pcap_cleanup_npf(p);
1309 return (PCAP_ERROR);
1310 }
1311
1312 /*
1313 * Check if rfmon mode is supported on the pcap_t for Windows systems.
1314 */
1315 static int
1316 pcap_can_set_rfmon_npf(pcap_t *p)
1317 {
1318 return (PacketIsMonitorModeSupported(p->opt.device) == 1);
1319 }
1320
1321 pcap_t *
1322 pcap_create_interface(const char *device _U_, char *ebuf)
1323 {
1324 pcap_t *p;
1325
1326 p = PCAP_CREATE_COMMON(ebuf, struct pcap_win);
1327 if (p == NULL)
1328 return (NULL);
1329
1330 p->activate_op = pcap_activate_npf;
1331 p->can_set_rfmon_op = pcap_can_set_rfmon_npf;
1332 return (p);
1333 }
1334
1335 static int
1336 pcap_setfilter_npf(pcap_t *p, struct bpf_program *fp)
1337 {
1338 struct pcap_win *pw = p->priv;
1339
1340 if(PacketSetBpf(pw->adapter,fp)==FALSE){
1341 /*
1342 * Kernel filter not installed.
1343 *
1344 * XXX - we don't know whether this failed because:
1345 *
1346 * the kernel rejected the filter program as invalid,
1347 * in which case we should fall back on userland
1348 * filtering;
1349 *
1350 * the kernel rejected the filter program as too big,
1351 * in which case we should again fall back on
1352 * userland filtering;
1353 *
1354 * there was some other problem, in which case we
1355 * should probably report an error.
1356 *
1357 * For NPF devices, the Win32 status will be
1358 * STATUS_INVALID_DEVICE_REQUEST for invalid
1359 * filters, but I don't know what it'd be for
1360 * other problems, and for some other devices
1361 * it might not be set at all.
1362 *
1363 * So we just fall back on userland filtering in
1364 * all cases.
1365 */
1366
1367 /*
1368 * install_bpf_program() validates the program.
1369 *
1370 * XXX - what if we already have a filter in the kernel?
1371 */
1372 if (install_bpf_program(p, fp) < 0)
1373 return (-1);
1374 pw->filtering_in_kernel = 0; /* filtering in userland */
1375 return (0);
1376 }
1377
1378 /*
1379 * It worked.
1380 */
1381 pw->filtering_in_kernel = 1; /* filtering in the kernel */
1382
1383 /*
1384 * Discard any previously-received packets, as they might have
1385 * passed whatever filter was formerly in effect, but might
1386 * not pass this filter (BIOCSETF discards packets buffered
1387 * in the kernel, so you can lose packets in any case).
1388 */
1389 p->cc = 0;
1390 return (0);
1391 }
1392
1393 /*
1394 * We filter at user level, since the kernel driver does't process the packets
1395 */
1396 static int
1397 pcap_setfilter_win32_dag(pcap_t *p, struct bpf_program *fp) {
1398
1399 if(!fp)
1400 {
1401 pcap_strlcpy(p->errbuf, "setfilter: No filter specified", sizeof(p->errbuf));
1402 return (-1);
1403 }
1404
1405 /* Install a user level filter */
1406 if (install_bpf_program(p, fp) < 0)
1407 return (-1);
1408
1409 return (0);
1410 }
1411
1412 static int
1413 pcap_getnonblock_npf(pcap_t *p)
1414 {
1415 struct pcap_win *pw = p->priv;
1416
1417 /*
1418 * XXX - if there were a PacketGetReadTimeout() call, we
1419 * would use it, and return 1 if the timeout is -1
1420 * and 0 otherwise.
1421 */
1422 return (pw->nonblock);
1423 }
1424
1425 static int
1426 pcap_setnonblock_npf(pcap_t *p, int nonblock)
1427 {
1428 struct pcap_win *pw = p->priv;
1429 int newtimeout;
1430
1431 if (nonblock) {
1432 /*
1433 * Set the packet buffer timeout to -1 for non-blocking
1434 * mode.
1435 */
1436 newtimeout = -1;
1437 } else {
1438 /*
1439 * Restore the timeout set when the device was opened.
1440 * (Note that this may be -1, in which case we're not
1441 * really leaving non-blocking mode. However, although
1442 * the timeout argument to pcap_set_timeout() and
1443 * pcap_open_live() is an int, you're not supposed to
1444 * supply a negative value, so that "shouldn't happen".)
1445 */
1446 newtimeout = p->opt.timeout;
1447 }
1448 if (!PacketSetReadTimeout(pw->adapter, newtimeout)) {
1449 pcap_fmt_errmsg_for_win32_err(p->errbuf, PCAP_ERRBUF_SIZE,
1450 GetLastError(), "PacketSetReadTimeout");
1451 return (-1);
1452 }
1453 pw->nonblock = (newtimeout == -1);
1454 return (0);
1455 }
1456
1457 static int
1458 pcap_add_if_npf(pcap_if_list_t *devlistp, char *name, bpf_u_int32 flags,
1459 const char *description, char *errbuf)
1460 {
1461 pcap_if_t *curdev;
1462 npf_if_addr if_addrs[MAX_NETWORK_ADDRESSES];
1463 LONG if_addr_size;
1464 int res = 0;
1465
1466 if_addr_size = MAX_NETWORK_ADDRESSES;
1467
1468 /*
1469 * Add an entry for this interface, with no addresses.
1470 */
1471 curdev = add_dev(devlistp, name, flags, description, errbuf);
1472 if (curdev == NULL) {
1473 /*
1474 * Failure.
1475 */
1476 return (-1);
1477 }
1478
1479 /*
1480 * Get the list of addresses for the interface.
1481 */
1482 if (!PacketGetNetInfoEx((void *)name, if_addrs, &if_addr_size)) {
1483 /*
1484 * Failure.
1485 *
1486 * We don't return an error, because this can happen with
1487 * NdisWan interfaces, and we want to supply them even
1488 * if we can't supply their addresses.
1489 *
1490 * We return an entry with an empty address list.
1491 */
1492 return (0);
1493 }
1494
1495 /*
1496 * Now add the addresses.
1497 */
1498 while (if_addr_size-- > 0) {
1499 /*
1500 * "curdev" is an entry for this interface; add an entry for
1501 * this address to its list of addresses.
1502 */
1503 res = add_addr_to_dev(curdev,
1504 (struct sockaddr *)&if_addrs[if_addr_size].IPAddress,
1505 sizeof (struct sockaddr_storage),
1506 (struct sockaddr *)&if_addrs[if_addr_size].SubnetMask,
1507 sizeof (struct sockaddr_storage),
1508 (struct sockaddr *)&if_addrs[if_addr_size].Broadcast,
1509 sizeof (struct sockaddr_storage),
1510 NULL,
1511 0,
1512 errbuf);
1513 if (res == -1) {
1514 /*
1515 * Failure.
1516 */
1517 break;
1518 }
1519 }
1520
1521 return (res);
1522 }
1523
1524 static int
1525 get_if_flags(const char *name, bpf_u_int32 *flags, char *errbuf)
1526 {
1527 char *name_copy;
1528 ADAPTER *adapter;
1529 int status;
1530 size_t len;
1531 NDIS_HARDWARE_STATUS hardware_status;
1532 #ifdef OID_GEN_PHYSICAL_MEDIUM
1533 NDIS_PHYSICAL_MEDIUM phys_medium;
1534 bpf_u_int32 gen_physical_medium_oids[] = {
1535 #ifdef OID_GEN_PHYSICAL_MEDIUM_EX
1536 OID_GEN_PHYSICAL_MEDIUM_EX,
1537 #endif
1538 OID_GEN_PHYSICAL_MEDIUM
1539 };
1540 #define N_GEN_PHYSICAL_MEDIUM_OIDS (sizeof gen_physical_medium_oids / sizeof gen_physical_medium_oids[0])
1541 size_t i;
1542 #endif /* OID_GEN_PHYSICAL_MEDIUM */
1543 #ifdef OID_GEN_LINK_STATE
1544 NDIS_LINK_STATE link_state;
1545 #endif
1546 int connect_status;
1547
1548 if (*flags & PCAP_IF_LOOPBACK) {
1549 /*
1550 * Loopback interface, so the connection status doesn't
1551 * apply. and it's not wireless (or wired, for that
1552 * matter...). We presume it's up and running.
1553 */
1554 *flags |= PCAP_IF_UP | PCAP_IF_RUNNING | PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE;
1555 return (0);
1556 }
1557
1558 /*
1559 * We need to open the adapter to get this information.
1560 *
1561 * XXX - PacketOpenAdapter() takes a non-const pointer
1562 * as an argument, so we make a copy of the argument and
1563 * pass that to it.
1564 */
1565 name_copy = strdup(name);
1566 adapter = PacketOpenAdapter(name_copy);
1567 free(name_copy);
1568 if (adapter == NULL) {
1569 /*
1570 * Give up; if they try to open this device, it'll fail.
1571 */
1572 return (0);
1573 }
1574
1575 #ifdef HAVE_AIRPCAP_API
1576 /*
1577 * Airpcap.sys do not support the below 'OID_GEN_x' values.
1578 * Just set these flags (and none of the '*flags' entered with).
1579 */
1580 if (PacketGetAirPcapHandle(adapter)) {
1581 /*
1582 * Must be "up" and "running" if the above if succeeded.
1583 */
1584 *flags = PCAP_IF_UP | PCAP_IF_RUNNING;
1585
1586 /*
1587 * An airpcap device is a wireless device (duh!)
1588 */
1589 *flags |= PCAP_IF_WIRELESS;
1590
1591 /*
1592 * A "network assosiation state" makes no sense for airpcap.
1593 */
1594 *flags |= PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE;
1595 PacketCloseAdapter(adapter);
1596 return (0);
1597 }
1598 #endif
1599
1600 /*
1601 * Get the hardware status, and derive "up" and "running" from
1602 * that.
1603 */
1604 len = sizeof (hardware_status);
1605 status = oid_get_request(adapter, OID_GEN_HARDWARE_STATUS,
1606 &hardware_status, &len, errbuf);
1607 if (status == 0) {
1608 switch (hardware_status) {
1609
1610 case NdisHardwareStatusReady:
1611 /*
1612 * "Available and capable of sending and receiving
1613 * data over the wire", so up and running.
1614 */
1615 *flags |= PCAP_IF_UP | PCAP_IF_RUNNING;
1616 break;
1617
1618 case NdisHardwareStatusInitializing:
1619 case NdisHardwareStatusReset:
1620 /*
1621 * "Initializing" or "Resetting", so up, but
1622 * not running.
1623 */
1624 *flags |= PCAP_IF_UP;
1625 break;
1626
1627 case NdisHardwareStatusClosing:
1628 case NdisHardwareStatusNotReady:
1629 /*
1630 * "Closing" or "Not ready", so neither up nor
1631 * running.
1632 */
1633 break;
1634
1635 default:
1636 /*
1637 * Unknown.
1638 */
1639 break;
1640 }
1641 } else {
1642 /*
1643 * Can't get the hardware status, so assume both up and
1644 * running.
1645 */
1646 *flags |= PCAP_IF_UP | PCAP_IF_RUNNING;
1647 }
1648
1649 /*
1650 * Get the network type.
1651 */
1652 #ifdef OID_GEN_PHYSICAL_MEDIUM
1653 /*
1654 * Try the OIDs we have for this, in order.
1655 */
1656 for (i = 0; i < N_GEN_PHYSICAL_MEDIUM_OIDS; i++) {
1657 len = sizeof (phys_medium);
1658 status = oid_get_request(adapter, gen_physical_medium_oids[i],
1659 &phys_medium, &len, errbuf);
1660 if (status == 0) {
1661 /*
1662 * Success.
1663 */
1664 break;
1665 }
1666 /*
1667 * Failed. We can't determine whether it failed
1668 * because that particular OID isn't supported
1669 * or because some other problem occurred, so we
1670 * just drive on and try the next OID.
1671 */
1672 }
1673 if (status == 0) {
1674 /*
1675 * We got the physical medium.
1676 */
1677 switch (phys_medium) {
1678
1679 case NdisPhysicalMediumWirelessLan:
1680 case NdisPhysicalMediumWirelessWan:
1681 case NdisPhysicalMediumNative802_11:
1682 case NdisPhysicalMediumBluetooth:
1683 case NdisPhysicalMediumUWB:
1684 case NdisPhysicalMediumIrda:
1685 /*
1686 * Wireless.
1687 */
1688 *flags |= PCAP_IF_WIRELESS;
1689 break;
1690
1691 default:
1692 /*
1693 * Not wireless.
1694 */
1695 break;
1696 }
1697 }
1698 #endif
1699
1700 /*
1701 * Get the connection status.
1702 */
1703 #ifdef OID_GEN_LINK_STATE
1704 len = sizeof(link_state);
1705 status = oid_get_request(adapter, OID_GEN_LINK_STATE, &link_state,
1706 &len, errbuf);
1707 if (status == 0) {
1708 /*
1709 * NOTE: this also gives us the receive and transmit
1710 * link state.
1711 */
1712 switch (link_state.MediaConnectState) {
1713
1714 case MediaConnectStateConnected:
1715 /*
1716 * It's connected.
1717 */
1718 *flags |= PCAP_IF_CONNECTION_STATUS_CONNECTED;
1719 break;
1720
1721 case MediaConnectStateDisconnected:
1722 /*
1723 * It's disconnected.
1724 */
1725 *flags |= PCAP_IF_CONNECTION_STATUS_DISCONNECTED;
1726 break;
1727
1728 default:
1729 /*
1730 * It's unknown whether it's connected or not.
1731 */
1732 break;
1733 }
1734 }
1735 #else
1736 /*
1737 * OID_GEN_LINK_STATE isn't supported because it's not in our SDK.
1738 */
1739 status = -1;
1740 #endif
1741 if (status == -1) {
1742 /*
1743 * OK, OID_GEN_LINK_STATE didn't work, try
1744 * OID_GEN_MEDIA_CONNECT_STATUS.
1745 */
1746 status = oid_get_request(adapter, OID_GEN_MEDIA_CONNECT_STATUS,
1747 &connect_status, &len, errbuf);
1748 if (status == 0) {
1749 switch (connect_status) {
1750
1751 case NdisMediaStateConnected:
1752 /*
1753 * It's connected.
1754 */
1755 *flags |= PCAP_IF_CONNECTION_STATUS_CONNECTED;
1756 break;
1757
1758 case NdisMediaStateDisconnected:
1759 /*
1760 * It's disconnected.
1761 */
1762 *flags |= PCAP_IF_CONNECTION_STATUS_DISCONNECTED;
1763 break;
1764 }
1765 }
1766 }
1767 PacketCloseAdapter(adapter);
1768 return (0);
1769 }
1770
1771 int
1772 pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf)
1773 {
1774 int ret = 0;
1775 const char *desc;
1776 char *AdaptersName;
1777 ULONG NameLength;
1778 char *name;
1779
1780 /*
1781 * Find out how big a buffer we need.
1782 *
1783 * This call should always return FALSE; if the error is
1784 * ERROR_INSUFFICIENT_BUFFER, NameLength will be set to
1785 * the size of the buffer we need, otherwise there's a
1786 * problem, and NameLength should be set to 0.
1787 *
1788 * It shouldn't require NameLength to be set, but,
1789 * at least as of WinPcap 4.1.3, it checks whether
1790 * NameLength is big enough before it checks for a
1791 * NULL buffer argument, so, while it'll still do
1792 * the right thing if NameLength is uninitialized and
1793 * whatever junk happens to be there is big enough
1794 * (because the pointer argument will be null), it's
1795 * still reading an uninitialized variable.
1796 */
1797 NameLength = 0;
1798 if (!PacketGetAdapterNames(NULL, &NameLength))
1799 {
1800 DWORD last_error = GetLastError();
1801
1802 if (last_error != ERROR_INSUFFICIENT_BUFFER)
1803 {
1804 pcap_fmt_errmsg_for_win32_err(errbuf, PCAP_ERRBUF_SIZE,
1805 last_error, "PacketGetAdapterNames");
1806 return (-1);
1807 }
1808 }
1809
1810 if (NameLength <= 0)
1811 return 0;
1812 AdaptersName = (char*) malloc(NameLength);
1813 if (AdaptersName == NULL)
1814 {
1815 snprintf(errbuf, PCAP_ERRBUF_SIZE, "Cannot allocate enough memory to list the adapters.");
1816 return (-1);
1817 }
1818
1819 if (!PacketGetAdapterNames(AdaptersName, &NameLength)) {
1820 pcap_fmt_errmsg_for_win32_err(errbuf, PCAP_ERRBUF_SIZE,
1821 GetLastError(), "PacketGetAdapterNames");
1822 free(AdaptersName);
1823 return (-1);
1824 }
1825
1826 /*
1827 * "PacketGetAdapterNames()" returned a list of
1828 * null-terminated ASCII interface name strings,
1829 * terminated by a null string, followed by a list
1830 * of null-terminated ASCII interface description
1831 * strings, terminated by a null string.
1832 * This means there are two ASCII nulls at the end
1833 * of the first list.
1834 *
1835 * Find the end of the first list; that's the
1836 * beginning of the second list.
1837 */
1838 desc = &AdaptersName[0];
1839 while (*desc != '\0' || *(desc + 1) != '\0')
1840 desc++;
1841
1842 /*
1843 * Found it - "desc" points to the first of the two
1844 * nulls at the end of the list of names, so the
1845 * first byte of the list of descriptions is two bytes
1846 * after it.
1847 */
1848 desc += 2;
1849
1850 /*
1851 * Loop over the elements in the first list.
1852 */
1853 name = &AdaptersName[0];
1854 while (*name != '\0') {
1855 bpf_u_int32 flags = 0;
1856
1857 #ifdef HAVE_AIRPCAP_API
1858 /*
1859 * Is this an AirPcap device?
1860 * If so, ignore it; it'll get added later, by the
1861 * AirPcap code.
1862 */
1863 if (device_is_airpcap(name, errbuf) == 1) {
1864 name += strlen(name) + 1;
1865 desc += strlen(desc) + 1;
1866 continue;
1867 }
1868 #endif
1869
1870 #ifdef HAVE_PACKET_IS_LOOPBACK_ADAPTER
1871 /*
1872 * Is this a loopback interface?
1873 */
1874 if (PacketIsLoopbackAdapter(name)) {
1875 /* Yes */
1876 flags |= PCAP_IF_LOOPBACK;
1877 }
1878 #endif
1879 /*
1880 * Get additional flags.
1881 */
1882 if (get_if_flags(name, &flags, errbuf) == -1) {
1883 /*
1884 * Failure.
1885 */
1886 ret = -1;
1887 break;
1888 }
1889
1890 /*
1891 * Add an entry for this interface.
1892 */
1893 if (pcap_add_if_npf(devlistp, name, flags, desc,
1894 errbuf) == -1) {
1895 /*
1896 * Failure.
1897 */
1898 ret = -1;
1899 break;
1900 }
1901 name += strlen(name) + 1;
1902 desc += strlen(desc) + 1;
1903 }
1904
1905 free(AdaptersName);
1906 return (ret);
1907 }
1908
1909 /*
1910 * Return the name of a network interface attached to the system, or NULL
1911 * if none can be found. The interface must be configured up; the
1912 * lowest unit number is preferred; loopback is ignored.
1913 *
1914 * In the best of all possible worlds, this would be the same as on
1915 * UN*X, but there may be software that expects this to return a
1916 * full list of devices after the first device.
1917 */
1918 #define ADAPTERSNAME_LEN 8192
1919 char *
1920 pcap_lookupdev(char *errbuf)
1921 {
1922 DWORD dwVersion;
1923 DWORD dwWindowsMajorVersion;
1924
1925 /*
1926 * We disable this in "new API" mode, because 1) in WinPcap/Npcap,
1927 * it may return UTF-16 strings, for backwards-compatibility
1928 * reasons, and we're also disabling the hack to make that work,
1929 * for not-going-past-the-end-of-a-string reasons, and 2) we
1930 * want its behavior to be consistent.
1931 *
1932 * In addition, it's not thread-safe, so we've marked it as
1933 * deprecated.
1934 */
1935 if (pcap_new_api) {
1936 snprintf(errbuf, PCAP_ERRBUF_SIZE,
1937 "pcap_lookupdev() is deprecated and is not supported in programs calling pcap_init()");
1938 return (NULL);
1939 }
1940
1941 /* disable MSVC's GetVersion() deprecated warning here */
1942 DIAG_OFF_DEPRECATION
1943 dwVersion = GetVersion(); /* get the OS version */
1944 DIAG_ON_DEPRECATION
1945 dwWindowsMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion)));
1946
1947 if (dwVersion >= 0x80000000 && dwWindowsMajorVersion >= 4) {
1948 /*
1949 * Windows 95, 98, ME.
1950 */
1951 ULONG NameLength = ADAPTERSNAME_LEN;
1952 static char AdaptersName[ADAPTERSNAME_LEN];
1953
1954 if (PacketGetAdapterNames(AdaptersName,&NameLength) )
1955 return (AdaptersName);
1956 else
1957 return NULL;
1958 } else {
1959 /*
1960 * Windows NT (NT 4.0 and later).
1961 * Convert the names to Unicode for backward compatibility.
1962 */
1963 ULONG NameLength = ADAPTERSNAME_LEN;
1964 static WCHAR AdaptersName[ADAPTERSNAME_LEN];
1965 size_t BufferSpaceLeft;
1966 char *tAstr;
1967 WCHAR *Unameptr;
1968 char *Adescptr;
1969 size_t namelen, i;
1970 WCHAR *TAdaptersName = (WCHAR*)malloc(ADAPTERSNAME_LEN * sizeof(WCHAR));
1971 int NAdapts = 0;
1972
1973 if(TAdaptersName == NULL)
1974 {
1975 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, "memory allocation failure");
1976 return NULL;
1977 }
1978
1979 if ( !PacketGetAdapterNames((PTSTR)TAdaptersName,&NameLength) )
1980 {
1981 pcap_fmt_errmsg_for_win32_err(errbuf, PCAP_ERRBUF_SIZE,
1982 GetLastError(), "PacketGetAdapterNames");
1983 free(TAdaptersName);
1984 return NULL;
1985 }
1986
1987
1988 BufferSpaceLeft = ADAPTERSNAME_LEN * sizeof(WCHAR);
1989 tAstr = (char*)TAdaptersName;
1990 Unameptr = AdaptersName;
1991
1992 /*
1993 * Convert the device names to Unicode into AdapterName.
1994 */
1995 do {
1996 /*
1997 * Length of the name, including the terminating
1998 * NUL.
1999 */
2000 namelen = strlen(tAstr) + 1;
2001
2002 /*
2003 * Do we have room for the name in the Unicode
2004 * buffer?
2005 */
2006 if (BufferSpaceLeft < namelen * sizeof(WCHAR)) {
2007 /*
2008 * No.
2009 */
2010 goto quit;
2011 }
2012 BufferSpaceLeft -= namelen * sizeof(WCHAR);
2013
2014 /*
2015 * Copy the name, converting ASCII to Unicode.
2016 * namelen includes the NUL, so we copy it as
2017 * well.
2018 */
2019 for (i = 0; i < namelen; i++)
2020 *Unameptr++ = *tAstr++;
2021
2022 /*
2023 * Count this adapter.
2024 */
2025 NAdapts++;
2026 } while (namelen != 1);
2027
2028 /*
2029 * Copy the descriptions, but don't convert them from
2030 * ASCII to Unicode.
2031 */
2032 Adescptr = (char *)Unameptr;
2033 while(NAdapts--)
2034 {
2035 size_t desclen;
2036
2037 desclen = strlen(tAstr) + 1;
2038
2039 /*
2040 * Do we have room for the name in the Unicode
2041 * buffer?
2042 */
2043 if (BufferSpaceLeft < desclen) {
2044 /*
2045 * No.
2046 */
2047 goto quit;
2048 }
2049
2050 /*
2051 * Just copy the ASCII string.
2052 * namelen includes the NUL, so we copy it as
2053 * well.
2054 */
2055 memcpy(Adescptr, tAstr, desclen);
2056 Adescptr += desclen;
2057 tAstr += desclen;
2058 BufferSpaceLeft -= desclen;
2059 }
2060
2061 quit:
2062 free(TAdaptersName);
2063 return (char *)(AdaptersName);
2064 }
2065 }
2066
2067 /*
2068 * We can't use the same code that we use on UN*X, as that's doing
2069 * UN*X-specific calls.
2070 *
2071 * We don't just fetch the entire list of devices, search for the
2072 * particular device, and use its first IPv4 address, as that's too
2073 * much work to get just one device's netmask.
2074 */
2075 int
2076 pcap_lookupnet(const char *device, bpf_u_int32 *netp, bpf_u_int32 *maskp,
2077 char *errbuf)
2078 {
2079 /*
2080 * We need only the first IPv4 address, so we must scan the array returned by PacketGetNetInfo()
2081 * in order to skip non IPv4 (i.e. IPv6 addresses)
2082 */
2083 npf_if_addr if_addrs[MAX_NETWORK_ADDRESSES];
2084 LONG if_addr_size = MAX_NETWORK_ADDRESSES;
2085 struct sockaddr_in *t_addr;
2086 LONG i;
2087
2088 if (!PacketGetNetInfoEx((void *)device, if_addrs, &if_addr_size)) {
2089 *netp = *maskp = 0;
2090 return (0);
2091 }
2092
2093 for(i = 0; i < if_addr_size; i++)
2094 {
2095 if(if_addrs[i].IPAddress.ss_family == AF_INET)
2096 {
2097 t_addr = (struct sockaddr_in *) &(if_addrs[i].IPAddress);
2098 *netp = t_addr->sin_addr.S_un.S_addr;
2099 t_addr = (struct sockaddr_in *) &(if_addrs[i].SubnetMask);
2100 *maskp = t_addr->sin_addr.S_un.S_addr;
2101
2102 *netp &= *maskp;
2103 return (0);
2104 }
2105
2106 }
2107
2108 *netp = *maskp = 0;
2109 return (0);
2110 }
2111
2112 static const char *pcap_lib_version_string;
2113
2114 #ifdef HAVE_VERSION_H
2115 /*
2116 * libpcap being built for Windows, as part of a WinPcap/Npcap source
2117 * tree. Include version.h from that source tree to get the WinPcap/Npcap
2118 * version.
2119 *
2120 * XXX - it'd be nice if we could somehow generate the WinPcap/Npcap version
2121 * number when building as part of WinPcap/Npcap. (It'd be nice to do so
2122 * for the packet.dll version number as well.)
2123 */
2124 #include "../../version.h"
2125
2126 static const char pcap_version_string[] =
2127 WINPCAP_PRODUCT_NAME " version " WINPCAP_VER_STRING ", based on " PCAP_VERSION_STRING;
2128
2129 const char *
2130 pcap_lib_version(void)
2131 {
2132 if (pcap_lib_version_string == NULL) {
2133 /*
2134 * Generate the version string.
2135 */
2136 char *packet_version_string = PacketGetVersion();
2137 char *packet_LPCSTR = PacketGetLPCSTR();
2138 char *packet_const_char_star = PacketGetConstCharStar();
2139
2140 if (strcmp(WINPCAP_VER_STRING, packet_version_string) == 0) {
2141 /*
2142 * WinPcap/Npcap version string and packet.dll version
2143 * string are the same; just report the WinPcap/Npcap
2144 * version.
2145 */
2146 pcap_lib_version_string = pcap_version_string;
2147 } else {
2148 /*
2149 * WinPcap/Npcap version string and packet.dll version
2150 * string are different; that shouldn't be the
2151 * case (the two libraries should come from the
2152 * same version of WinPcap/Npcap), so we report both
2153 * versions.
2154 */
2155 char *full_pcap_version_string;
2156
2157 if (pcap_asprintf(&full_pcap_version_string,
2158 WINPCAP_PRODUCT_NAME " version " WINPCAP_VER_STRING " (packet.dll version %s), based on " PCAP_VERSION_STRING " blah blah blah %s %s",
2159 packet_version_string,
2160 packet_LPCSTR, packet_const_char_star) != -1) {
2161 /* Success */
2162 pcap_lib_version_string = full_pcap_version_string;
2163 }
2164 }
2165 }
2166 return (pcap_lib_version_string);
2167 }
2168
2169 #else /* HAVE_VERSION_H */
2170
2171 /*
2172 * libpcap being built for Windows, not as part of a WinPcap/Npcap source
2173 * tree.
2174 */
2175 const char *
2176 pcap_lib_version(void)
2177 {
2178 if (pcap_lib_version_string == NULL) {
2179 /*
2180 * Generate the version string. Report the packet.dll
2181 * version.
2182 */
2183 char *full_pcap_version_string;
2184
2185 if (pcap_asprintf(&full_pcap_version_string,
2186 PCAP_VERSION_STRING " (packet.dll version %s)",
2187 PacketGetVersion()) != -1) {
2188 /* Success */
2189 pcap_lib_version_string = full_pcap_version_string;
2190 }
2191 }
2192 return (pcap_lib_version_string);
2193 }
2194 #endif /* HAVE_VERSION_H */