summaryrefslogtreecommitdiffstats
path: root/capture/capture-wpcap.c
blob: 4f7d1dca9953c3cd7f18a0b331d1f4d36596820a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
/* capture-wpcap.c
 * WinPcap/Npcap-specific interfaces for capturing.  We load WinPcap/Npcap
 * at run time, so that we only need one Wireshark binary and one TShark
 * binary for Windows, regardless of whether WinPcap/Npcap is installed
 * or not.
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 2001 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "config.h"

#include <wireshark.h>

#include <windows.h>
#include <wchar.h>
#include <tchar.h>

#include <stdio.h>

#include <ws_attributes.h>

#include "capture/capture-wpcap.h"
#include <wsutil/feature_list.h>

bool has_wpcap = false;

#ifdef HAVE_LIBPCAP

#include <gmodule.h>

#include <epan/strutil.h>

#include "capture/capture_ifinfo.h"
#include "capture/capture-pcap-util.h"
#include "capture/capture-pcap-util-int.h"

#include <wsutil/file_util.h>
#include <wsutil/strtoi.h>
#include <wsutil/ws_assert.h>

#define MAX_WIN_IF_NAME_LEN 511

static void    (*p_pcap_close) (pcap_t *);
static int     (*p_pcap_stats) (pcap_t *, struct pcap_stat *);
static int     (*p_pcap_dispatch) (pcap_t *, int, pcap_handler, unsigned char *);
static int     (*p_pcap_snapshot) (pcap_t *);
static int     (*p_pcap_datalink) (pcap_t *);
static int     (*p_pcap_setfilter) (pcap_t *, struct bpf_program *);
static char*   (*p_pcap_geterr) (pcap_t *);
static int     (*p_pcap_compile) (pcap_t *, struct bpf_program *, const char *, int,
			bpf_u_int32);
static int     (*p_pcap_compile_nopcap) (int, int, struct bpf_program *, const char *, int,
			bpf_u_int32);
static int     (*p_pcap_lookupnet) (const char *, bpf_u_int32 *, bpf_u_int32 *,
			char *);
static pcap_t* (*p_pcap_open_live) (const char *, int, int, int, char *);
static int     (*p_pcap_loop) (pcap_t *, int, pcap_handler, unsigned char *);
static pcap_t* (*p_pcap_open_dead) (int, int);
static void    (*p_pcap_freecode) (struct bpf_program *);
static int     (*p_pcap_findalldevs) (pcap_if_t **, char *);
static void    (*p_pcap_freealldevs) (pcap_if_t *);
static int (*p_pcap_datalink_name_to_val) (const char *);
static const char *(*p_pcap_datalink_val_to_name) (int);
static const char *(*p_pcap_datalink_val_to_description) (int);
static void    (*p_pcap_breakloop) (pcap_t *);
static const char *(*p_pcap_lib_version) (void);
static int     (*p_pcap_setbuff) (pcap_t *, int dim);
static int     (*p_pcap_next_ex) (pcap_t *, struct pcap_pkthdr **pkt_header, const u_char **pkt_data);
#ifdef HAVE_PCAP_REMOTE
static pcap_t* (*p_pcap_open) (const char *, int, int, int,
			       struct pcap_rmtauth *, char *);
static int     (*p_pcap_findalldevs_ex) (const char *, struct pcap_rmtauth *,
					 pcap_if_t **, char *);
static int     (*p_pcap_createsrcstr) (char *, int, const char *, const char *,
				       const char *, char *);
#endif
#ifdef HAVE_PCAP_SETSAMPLING
static struct pcap_samp* (*p_pcap_setsampling)(pcap_t *);
#endif

static int 	(*p_pcap_list_datalinks)(pcap_t *, int **);
static int	(*p_pcap_set_datalink)(pcap_t *, int);

#ifdef HAVE_PCAP_FREE_DATALINKS
static int	(*p_pcap_free_datalinks)(int *);
#endif

static char	*(*p_bpf_image)(const struct bpf_insn *, int);

#ifdef HAVE_PCAP_CREATE
static pcap_t	*(*p_pcap_create)(const char *, char *);
static int	(*p_pcap_set_snaplen)(pcap_t *, int);
static int	(*p_pcap_set_promisc)(pcap_t *, int);
static int	(*p_pcap_can_set_rfmon)(pcap_t *);
static int	(*p_pcap_set_rfmon)(pcap_t *, int);
static int	(*p_pcap_set_timeout)(pcap_t *, int);
static int	(*p_pcap_set_buffer_size)(pcap_t *, int);
static int	(*p_pcap_activate)(pcap_t *);
static const char *(*p_pcap_statustostr)(int);
#endif

#ifdef HAVE_PCAP_SET_TSTAMP_TYPE
static int      (*p_pcap_set_tstamp_type)(pcap_t *, int);
static int      (*p_pcap_set_tstamp_precision)(pcap_t *, int);
static int      (*p_pcap_get_tstamp_precision)(pcap_t *);
static int      (*p_pcap_list_tstamp_types)(pcap_t *, int **);
static void     (*p_pcap_free_tstamp_types)(int *);
static int      (*p_pcap_tstamp_type_name_to_val)(const char *);
static const char * (*p_pcap_tstamp_type_val_to_name)(int);
static const char * (*p_pcap_tstamp_type_val_to_description)(int);
#endif

typedef struct {
	const char	*name;
	void *	*ptr;
	bool	optional;
} symbol_table_t;

#define SYM(x, y)	{ G_STRINGIFY(x) , (void *) &G_PASTE(p_,x), y }

void
load_wpcap(void)
{

	/* These are the symbols I need or want from Wpcap */
	static const symbol_table_t	symbols[] = {
		SYM(pcap_close, false),
		SYM(pcap_stats, false),
		SYM(pcap_dispatch, false),
		SYM(pcap_snapshot, false),
		SYM(pcap_datalink, false),
		SYM(pcap_setfilter, false),
		SYM(pcap_geterr, false),
		SYM(pcap_compile, false),
		SYM(pcap_compile_nopcap, false),
		SYM(pcap_lookupnet, false),
#ifdef HAVE_PCAP_REMOTE
		SYM(pcap_open, false),
		SYM(pcap_findalldevs_ex, false),
		SYM(pcap_createsrcstr, false),
#endif
		SYM(pcap_open_live, false),
		SYM(pcap_open_dead, false),
#ifdef HAVE_PCAP_SETSAMPLING
		SYM(pcap_setsampling, true),
#endif
		SYM(pcap_loop, false),
		SYM(pcap_freecode, false),
		SYM(pcap_findalldevs, false),
		SYM(pcap_freealldevs, false),
		SYM(pcap_datalink_name_to_val, false),
		SYM(pcap_datalink_val_to_name, false),
		SYM(pcap_datalink_val_to_description, false),
		SYM(pcap_breakloop, false),
		SYM(pcap_lib_version, false),
		SYM(pcap_setbuff, true),
		SYM(pcap_next_ex, true),
		SYM(pcap_list_datalinks, false),
		SYM(pcap_set_datalink, false),
#ifdef HAVE_PCAP_FREE_DATALINKS
		SYM(pcap_free_datalinks, true),
#endif
		SYM(bpf_image, false),
#ifdef HAVE_PCAP_CREATE
		SYM(pcap_create, true),
		SYM(pcap_set_snaplen, true),
		SYM(pcap_set_promisc, true),
		SYM(pcap_can_set_rfmon, true),
		SYM(pcap_set_rfmon, true),
		SYM(pcap_set_timeout, false),
		SYM(pcap_set_buffer_size, false),
		SYM(pcap_activate, true),
		SYM(pcap_statustostr, true),
#endif
#ifdef HAVE_PCAP_SET_TSTAMP_TYPE
		SYM(pcap_set_tstamp_type, true),
		SYM(pcap_set_tstamp_precision, true),
		SYM(pcap_get_tstamp_precision, true),
		SYM(pcap_list_tstamp_types, true),
		SYM(pcap_free_tstamp_types, true),
		SYM(pcap_tstamp_type_name_to_val, true),
		SYM(pcap_tstamp_type_val_to_name, true),
		SYM(pcap_tstamp_type_val_to_description, true),
#endif
		{ NULL, NULL, false }
	};

	GModule		*wh; /* wpcap handle */
	const symbol_table_t	*sym;

	wh = load_wpcap_module();

	if (!wh) {
		return;
	}

	sym = symbols;
	while (sym->name) {
		if (!g_module_symbol(wh, sym->name, sym->ptr)) {
			if (sym->optional) {
				/*
				 * We don't care if it's missing; we just
				 * don't use it.
				 */
				*sym->ptr = NULL;
			} else {
				/*
				 * We require this symbol.
				 */
				return;
			}
		}
		sym++;
	}


	has_wpcap = true;
}

bool
caplibs_have_npcap(void)
{
	return has_wpcap && g_str_has_prefix(p_pcap_lib_version(), "Npcap");
}

bool
caplibs_get_npcap_version(unsigned int *major, unsigned int *minor)
{
	const char *version;
	static const char prefix[] = "Npcap version ";

	if (!has_wpcap)
		return false;	/* we don't have any pcap */

	version = p_pcap_lib_version();
	if (!g_str_has_prefix(version, prefix))
		return false;	/* we have it, but it's not Npcap */

	/*
	 * This is Npcap; return the major and minor version numbers.
	 * First, skip pas the "Npcap version " prefix.
	 */
	const char *major_version_number;
	const char *minor_version_number;
	const char *p;

	/*
	 * Get the major version number.
	 */
	major_version_number = version + sizeof prefix - 1;
	if (!ws_strtou(major_version_number, &p, major))
		return false;	/* not a number */
	if (*p != '.')
		return false;	/* not followed by a "." */
	p++;	/* skip over the '.' */

	/*
	 * Get the minor version number.
	 */
	minor_version_number = p;
	if (!ws_strtou(minor_version_number, &p, minor))
		return false;	/* not a number */
	if (*p != ',' && *p != '.' && *p != '\0') {
		/*
		 * Not followed by a comma (to separate from "based on
		 * libpcap ..."), not followed by a period (in case Npcap
		 * ever has a dot-dot release), and not followed by a
		 * '\0' (in case it has only the Npcap version number).
		 */
		return false;
	}
	return true;
}

static char *
local_code_page_str_to_utf8(char *str)
{
	ULONG utf16_len;
	wchar_t *utf16_str;
	char *utf8_str;

	if (str == NULL) {
		return NULL;
	}

	utf16_len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
	utf16_str = g_malloc_n(utf16_len, sizeof(wchar_t));
	MultiByteToWideChar(CP_ACP, 0, str, -1, utf16_str, utf16_len);

	utf8_str = g_utf16_to_utf8(utf16_str, -1, NULL, NULL, NULL);

	g_free(utf16_str);
	return utf8_str;
}

static void
prepare_errbuf(char *errbuf)
{
	ws_assert(errbuf);
	errbuf[0] = '\0';
}

static void
convert_errbuf_to_utf8(char *errbuf)
{
	char *utf8_err;
	if (errbuf[0] == '\0') {
		return;
	}
	errbuf[PCAP_ERRBUF_SIZE - 1] = '\0';
	utf8_err = local_code_page_str_to_utf8(errbuf);
	snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s", utf8_err);
	g_free(utf8_err);
}

static char *
cant_load_winpcap_err(const char *app_name)
{
	return ws_strdup_printf(
"Unable to load Npcap or WinPcap (wpcap.dll); %s will not be able to\n"
"capture packets.\n"
"\n"
"In order to capture packets Npcap or WinPcap must be installed. See\n"
"\n"
"        https://npcap.com/\n"
"\n"
"for a downloadable version of Npcap and for instructions on how to\n"
"install it.",
	    app_name);
}

void
pcap_close(pcap_t *a)
{
	ws_assert(has_wpcap);
	p_pcap_close(a);
}

int
pcap_stats(pcap_t *a, struct pcap_stat *b)
{
	ws_assert(has_wpcap);
	return p_pcap_stats(a, b);
}

int
pcap_dispatch(pcap_t *a, int b, pcap_handler c, unsigned char *d)
{
	ws_assert(has_wpcap);
	return p_pcap_dispatch(a, b, c, d);
}

int
pcap_snapshot(pcap_t *a)
{
	ws_assert(has_wpcap);
	return p_pcap_snapshot(a);
}

int
pcap_datalink(pcap_t *a)
{
	ws_assert(has_wpcap);
	return p_pcap_datalink(a);
}

int
pcap_set_datalink(pcap_t *p, int dlt)
{
	ws_assert(has_wpcap);
	return p_pcap_set_datalink(p, dlt);
}

int
pcap_setfilter(pcap_t *a, struct bpf_program *b)
{
	ws_assert(has_wpcap);
	return p_pcap_setfilter(a, b);
}

char*
pcap_geterr(pcap_t *a)
{
	char *errbuf;
	ws_assert(has_wpcap);
	errbuf = p_pcap_geterr(a);
	convert_errbuf_to_utf8(errbuf);
	return errbuf;
}

int
pcap_compile(pcap_t *a, struct bpf_program *b, const char *c, int d,
	     bpf_u_int32 e)
{
	ws_assert(has_wpcap);
	return p_pcap_compile(a, b, c, d, e);
}

int
pcap_compile_nopcap(int a, int b, struct bpf_program *c, const char *d, int e,
		    bpf_u_int32 f)
{
	ws_assert(has_wpcap);
	return p_pcap_compile_nopcap(a, b, c, d, e, f);
}

int
pcap_lookupnet(const char *a, bpf_u_int32 *b, bpf_u_int32 *c, char *errbuf)
{
	int ret;
	ws_assert(has_wpcap);
	ret = p_pcap_lookupnet(a, b, c, errbuf);
	if (ret == -1)
		convert_errbuf_to_utf8(errbuf);
	return ret;
}

pcap_t*
pcap_open_live(const char *a, int b, int c, int d, char *errbuf)
{
	pcap_t *p;
	if (!has_wpcap) {
		snprintf(errbuf, PCAP_ERRBUF_SIZE,
			   "unable to load Npcap or WinPcap (wpcap.dll); can't open %s to capture",
			   a);
		return NULL;
	}
	prepare_errbuf(errbuf);
	p = p_pcap_open_live(a, b, c, d, errbuf);
	convert_errbuf_to_utf8(errbuf);
	return p;
}

pcap_t*
pcap_open_dead(int a, int b)
{
	if (!has_wpcap) {
		return NULL;
	}
	return p_pcap_open_dead(a, b);
}

char *
bpf_image(const struct bpf_insn *a, int b)
{
	if (!has_wpcap) {
		return NULL;
	}
	return p_bpf_image(a, b);
}

#ifdef HAVE_PCAP_REMOTE
pcap_t*
pcap_open(const char *a, int b, int c, int d, struct pcap_rmtauth *e, char *errbuf)
{
	pcap_t *ret;
	if (!has_wpcap) {
		snprintf(errbuf, PCAP_ERRBUF_SIZE,
			   "unable to load Npcap or WinPcap (wpcap.dll); can't open %s to capture",
			   a);
		return NULL;
	}
	prepare_errbuf(errbuf);
	ret = p_pcap_open(a, b, c, d, e, errbuf);
	convert_errbuf_to_utf8(errbuf);
	return ret;
}

int
ws_pcap_findalldevs_ex(const char *a, struct pcap_rmtauth *b, pcap_if_t **c, char *errbuf)
{
	int ret;
	ws_assert(has_wpcap);
	ret = p_pcap_findalldevs_ex(a, b, c, errbuf);
	if (ret == -1)
		convert_errbuf_to_utf8(errbuf);
	return ret;
}

int
pcap_createsrcstr(char *a, int b, const char *c, const char *d, const char *e,
		  char *errbuf)
{
	int ret;
	ws_assert(has_wpcap);
	ret = p_pcap_createsrcstr(a, b, c, d, e, errbuf);
	if (ret == -1)
		convert_errbuf_to_utf8(errbuf);
	return ret;
}
#endif

#ifdef HAVE_PCAP_SETSAMPLING
struct pcap_samp *
pcap_setsampling(pcap_t *a)
{
	ws_assert(has_wpcap);
	if (p_pcap_setsampling != NULL) {
		return p_pcap_setsampling(a);
	}
	return NULL;
}
#endif

int
pcap_loop(pcap_t *a, int b, pcap_handler c, unsigned char *d)
{
	ws_assert(has_wpcap);
	return p_pcap_loop(a, b, c, d);
}

void
pcap_freecode(struct bpf_program *a)
{
	ws_assert(has_wpcap);
	p_pcap_freecode(a);
}

int
pcap_findalldevs(pcap_if_t **a, char *errbuf)
{
	int ret;
	ws_assert(has_wpcap);
	ret = p_pcap_findalldevs(a, errbuf);
	if (ret == -1)
		convert_errbuf_to_utf8(errbuf);
	return ret;
}

void
pcap_freealldevs(pcap_if_t *a)
{
	ws_assert(has_wpcap);
	p_pcap_freealldevs(a);
}

#ifdef HAVE_PCAP_CREATE
pcap_t *
pcap_create(const char *a, char *errbuf)
{
	pcap_t *p;
	ws_assert(has_wpcap && p_pcap_create != NULL);
	p = p_pcap_create(a, errbuf);
	if (p == NULL)
		convert_errbuf_to_utf8(errbuf);
	return p;
}

int
pcap_set_snaplen(pcap_t *a, int b)
{
	ws_assert(has_wpcap && p_pcap_set_snaplen != NULL);
	return p_pcap_set_snaplen(a, b);
}

int
pcap_set_promisc(pcap_t *a, int b)
{
	ws_assert(has_wpcap && p_pcap_set_promisc != NULL);
	return p_pcap_set_promisc(a, b);
}

int
pcap_can_set_rfmon(pcap_t *a)
{
	ws_assert(has_wpcap);
	if (p_pcap_can_set_rfmon != NULL) {
		return p_pcap_can_set_rfmon(a);
	}
	return 0;
}

int
pcap_set_rfmon(pcap_t *a, int b)
{
	ws_assert(has_wpcap && p_pcap_set_rfmon != NULL);
	return p_pcap_set_rfmon(a, b);
}

int
pcap_set_timeout(pcap_t *a, int b)
{
	ws_assert(has_wpcap && p_pcap_set_timeout != NULL);
	return p_pcap_set_timeout(a, b);
}
int
pcap_set_buffer_size(pcap_t *a, int b)
{
	ws_assert(has_wpcap && p_pcap_set_buffer_size != NULL);
	return p_pcap_set_buffer_size(a, b);
}

int
pcap_activate(pcap_t *a)
{
	ws_assert(has_wpcap && p_pcap_activate != NULL);
	return p_pcap_activate(a);

}

const char *
pcap_statustostr(int a)
{
    static char ebuf[15 + 10 + 1];

    ws_assert(has_wpcap);
    if (p_pcap_statustostr != NULL) {
        return p_pcap_statustostr(a);
    }

    /* XXX copy routine from pcap.c ??? */
    (void)snprintf(ebuf, sizeof ebuf, "Don't have pcap_statustostr(), can't translate error: %d", a);
    return(ebuf);

}
#endif

#ifdef HAVE_PCAP_SET_TSTAMP_TYPE
int
pcap_set_tstamp_type(pcap_t *a, int b) {
	ws_assert(has_wpcap);
	if (p_pcap_set_tstamp_type != NULL) {
		return p_pcap_set_tstamp_type(a, b);
	}
	return PCAP_ERROR_CANTSET_TSTAMP_TYPE;
}

int
pcap_set_tstamp_precision(pcap_t *a, int b) {
	ws_assert(has_wpcap);
	if (p_pcap_set_tstamp_precision != NULL) {
		return p_pcap_set_tstamp_precision(a, b);
	}
	// No error code defined so return NOTSUP.
	return PCAP_ERROR_TSTAMP_PRECISION_NOTSUP;
}

int
pcap_get_tstamp_precision(pcap_t *a) {
	ws_assert(has_wpcap);
	if (p_pcap_get_tstamp_precision != NULL) {
		return p_pcap_get_tstamp_precision(a);
	}
	// No error code defined so return MICRO.
	return PCAP_TSTAMP_PRECISION_MICRO;
}

int
pcap_list_tstamp_types(pcap_t *a, int **b) {
	ws_assert(has_wpcap);
	if (p_pcap_list_tstamp_types != NULL) {
		return p_pcap_list_tstamp_types(a, b);
	}
	return PCAP_ERROR;
}

void
pcap_free_tstamp_types(int *a) {
	ws_assert(has_wpcap);
	if (p_pcap_free_tstamp_types != NULL) {
		p_pcap_free_tstamp_types(a);
	}
}

int
pcap_tstamp_type_name_to_val(const char *a) {
	ws_assert(has_wpcap);
	if (p_pcap_tstamp_type_name_to_val != NULL) {
		return p_pcap_tstamp_type_name_to_val(a);
	}
	return PCAP_ERROR;
}

const char *
pcap_tstamp_type_val_to_name(int a) {
	ws_assert(has_wpcap);
	if (p_pcap_tstamp_type_val_to_name != NULL) {
		return p_pcap_tstamp_type_val_to_name(a);
	}
	return NULL;
}

const char *
pcap_tstamp_type_val_to_description(int a) {
	ws_assert(has_wpcap);
	if (p_pcap_tstamp_type_val_to_description != NULL) {
		return p_pcap_tstamp_type_val_to_description(a);
	}
	return NULL;
}
#endif

int
pcap_datalink_name_to_val(const char *name)
{
	if (has_wpcap)
		return p_pcap_datalink_name_to_val(name);
	else
		return -1;
}

int
pcap_list_datalinks(pcap_t *p, int **ddlt)
{
	if (has_wpcap)
		return p_pcap_list_datalinks(p, ddlt);
	else
		return -1;
}

#ifdef HAVE_PCAP_FREE_DATALINKS
void
pcap_free_datalinks(int *ddlt)
{
	ws_assert(has_wpcap);

	/*
	 * If we don't have pcap_free_datalinks() in WinPcap,
	 * we don't free the memory - we can't use free(), as
	 * we might not have been built with the same version
	 * of the C runtime library as WinPcap was, and, if we're
	 * not, free() isn't guaranteed to work on something
	 * allocated by WinPcap.
	 */
	if (p_pcap_free_datalinks != NULL)
		p_pcap_free_datalinks(ddlt);
}
#endif

const char *
pcap_datalink_val_to_name(int dlt)
{
	if (has_wpcap)
		return p_pcap_datalink_val_to_name(dlt);
	else
		return NULL;
}

const char *
pcap_datalink_val_to_description(int dlt)
{
	if (has_wpcap)
		return p_pcap_datalink_val_to_description(dlt);
	return NULL;
}

void pcap_breakloop(pcap_t *a)
{
	p_pcap_breakloop(a);
}

/* setbuff is win32 specific! */
int pcap_setbuff(pcap_t *a, int b)
{
	ws_assert(has_wpcap);
	return p_pcap_setbuff(a, b);
}

int pcap_next_ex(pcap_t *a, struct pcap_pkthdr **b, const u_char **c)
{
	ws_assert(has_wpcap);
	return p_pcap_next_ex(a, b, c);
}

#ifdef HAVE_PCAP_REMOTE
GList *
get_remote_interface_list(const char *hostname, const char *port,
			  int auth_type, const char *username,
			  const char *passwd, int *err, char **err_str)
{
	if (!has_wpcap) {
		/*
		 * We don't have Npcap or WinPcap, so we can't get a list of
		 * interfaces.
		 */
		*err = DONT_HAVE_PCAP;
		if (err_str != NULL)
			*err_str = cant_load_winpcap_err("you");
		return NULL;
	}

	return get_interface_list_findalldevs_ex(hostname, port, auth_type,
	    username, passwd, err, err_str);
}
#endif

GList *
get_interface_list(int *err, char **err_str)
{
	if (!has_wpcap) {
		/*
		 * We don't have Npcap or WinPcap, so we can't get a list of
		 * interfaces.
		 */
		*err = DONT_HAVE_PCAP;
		if (err_str != NULL)
			*err_str = cant_load_winpcap_err("you");
		return NULL;
	}

	return get_interface_list_findalldevs(err, err_str);
}

/*
 * Get an error message string for a CANT_GET_INTERFACE_LIST error from
 * "get_interface_list()".
 */
char *
cant_get_if_list_error_message(const char *err_str)
{
	/*
	 * If the error message includes "Not enough storage is available
	 * to process this command" or "The operation completed successfully",
	 * suggest that they install a WinPcap version later than 3.0.
	 */
	if (strstr(err_str, "Not enough storage is available to process this command") != NULL ||
	    strstr(err_str, "The operation completed successfully") != NULL) {
		return ws_strdup_printf("Can't get list of interfaces: %s\n"
"This might be a problem with WinPcap 3.0. You should try updating to\n"
"Npcap. See https://npcap.com/ for more information.",
		    err_str);
	}
	return ws_strdup_printf("Can't get list of interfaces: %s", err_str);
}

if_capabilities_t *
get_if_capabilities_local(interface_options *interface_opts,
    cap_device_open_status *status, char **status_str)
{
	/*
	 * We're not getting capaibilities for a remote device; use
	 * pcap_create() and pcap_activate() if we have them, so that
	 * we can set various options, otherwise use pcap_open_live().
	 */
#ifdef HAVE_PCAP_CREATE
	if (p_pcap_create != NULL)
		return get_if_capabilities_pcap_create(interface_opts, status,
		    status_str);
#endif
	return get_if_capabilities_pcap_open_live(interface_opts, status,
	    status_str);
}

pcap_t *
open_capture_device_local(capture_options *capture_opts,
    interface_options *interface_opts, int timeout,
    cap_device_open_status *open_status,
    char (*open_status_str)[PCAP_ERRBUF_SIZE])
{
	/*
	 * We're not opening a remote device; use pcap_create() and
	 * pcap_activate() if we have them, so that we can set various
	 * options, otherwise use pcap_open_live().
	 */
#ifdef HAVE_PCAP_CREATE
	if (p_pcap_create != NULL)
		return open_capture_device_pcap_create(capture_opts,
		    interface_opts, timeout, open_status, open_status_str);
#endif
	return open_capture_device_pcap_open_live(interface_opts, timeout,
	    open_status, open_status_str);
}

/*
 * Append the WinPcap or Npcap SDK version with which we were compiled to a GString.
 */
void
gather_caplibs_compile_info(feature_list l)
{
	with_feature(l, "libpcap");
}

void
gather_caplibs_runtime_info(feature_list l)
{
	/*
	 * On Windows, we might have been compiled with WinPcap/Npcap but
	 * might not have it loaded; indicate whether we have it or
	 * not and, if we have it, what version we have.
	 */
	if (has_wpcap) {
		with_feature(l, "%s", p_pcap_lib_version());
	} else
		without_feature(l, "Npcap or WinPcap");
}

/*
 * If npf.sys is running, return true.
 */
bool
npf_sys_is_running(void)
{
	SC_HANDLE h_scm, h_serv;
	SERVICE_STATUS ss;

	h_scm = OpenSCManager(NULL, NULL, 0);
	if (!h_scm)
		return false;

	h_serv = OpenService(h_scm, _T("npcap"), SC_MANAGER_CONNECT|SERVICE_QUERY_STATUS);
	if (!h_serv) {
		h_serv = OpenService(h_scm, _T("npf"), SC_MANAGER_CONNECT|SERVICE_QUERY_STATUS);
		if (!h_serv) {
			CloseServiceHandle(h_scm);
			return false;
		}
	}

	if (QueryServiceStatus(h_serv, &ss)) {
		if (ss.dwCurrentState & SERVICE_RUNNING) {
			CloseServiceHandle(h_serv);
			CloseServiceHandle(h_scm);
			return true;
		}
	}
	CloseServiceHandle(h_serv);
	CloseServiceHandle(h_scm);
	return false;
}

#else /* HAVE_LIBPCAP */

void
load_wpcap(void)
{
	return;
}

/*
 * Append an indication that we were not compiled with WinPcap
 * to a GString.
 */
void
gather_caplibs_compile_info(feature_list l)
{
	without_feature(l, "libpcap");
}

void
gather_caplibs_runtime_info(feature_list l _U_)
{
}

bool
caplibs_have_npcap(void)
{
	return false;
}

#endif /* HAVE_LIBPCAP */

/*
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
 *
 * Local variables:
 * c-basic-offset: 8
 * tab-width: 8
 * indent-tabs-mode: t
 * End:
 *
 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
 * :indentSize=8:tabSize=8:noTabs=false:
 */