-
Notifications
You must be signed in to change notification settings - Fork 7
/
init.c
2384 lines (1981 loc) · 64.2 KB
/
init.c
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
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**\file init.c
* \ingroup Main
*
* \brief
* Most things here are called from `wsock_trace_init()`
* which is called from `DllMain()`:
*
* \li Parsing of the `wsock_trace` config-file.
* \li exclude-list handling.
*/
#include <stdint.h>
#include <limits.h>
#include <time.h>
#include <fcntl.h>
#include <errno.h>
#include <math.h>
#include <locale.h>
#include "config.h"
#include "common.h"
#include "wsock_trace.h"
#include "bfd_gcc.h"
#include "dump.h"
#include "geoip.h"
#include "idna.h"
#include "smartlist.h"
#include "stkwalk.h"
#include "overlap.h"
#include "hosts.h"
#include "services.h"
#include "firewall.h"
#include "cpu.h"
#include "asn.h"
#include "iana.h"
#include "dnsbl.h"
#include "inet_addr.h"
#include "init.h"
struct config_table g_cfg;
struct global_data g_data;
/**
* \typedef exclude
*
* Structure for `exclude_list*()` functions.
* This is used to exclude both tracing of functions,
* programs ("addId") and addresses in firewall.c.
*/
typedef struct exclude {
char *name; /**< The `name` to exclude from trace */
char *only_if_prog; /**< But only if `EXCL_FUNCTION == only_if_prog` (optional) */
uint64 num_excludes; /**< Number of times this `name` was excluded */
exclude_type which; /**< A single `exclude_type` of the above `name` */
} exclude;
/* Dynamic array of above exclude structure.
*/
static smartlist_t *exclude_list = NULL;
/* Set and restore the "Invalid Parameter Handler".
*/
static void set_invalid_handler (void);
static void reset_invalid_handler (void);
/*
* Wait on the global semaphore to get freed.
*/
void ws_sema_wait (void)
{
while (g_data.ws_sema && g_data.ws_sema != INVALID_HANDLE_VALUE)
{
DWORD ret = WaitForSingleObject (g_data.ws_sema, 0);
if (ret == WAIT_OBJECT_0)
break;
if (ret == WAIT_FAILED)
{
SetLastError (0);
break;
}
g_data.counts.sema_waits++;
Sleep (5);
}
}
/*
* Release the global semaphore.
*/
void ws_sema_release (void)
{
if (g_data.ws_sema && g_data.ws_sema != INVALID_HANDLE_VALUE)
ReleaseSemaphore (g_data.ws_sema, 1, NULL);
}
/**
* Get the `start-ticks` value for showing time-stamps.
*
* \note
* The `g_data.clocks_per_usec` is not the true CPU-speed.
* On a multicore CPU, this is normally higher than the real
* CPU MHz frequeny.
*/
static void init_timestamp (void)
{
LARGE_INTEGER rc;
uint64 frequency;
double MHz;
QueryPerformanceFrequency (&rc);
frequency = rc.QuadPart;
g_data.clocks_per_usec = frequency / 1000000ULL;
MHz = (double)frequency / 1E3;
if (MHz > 1000.0)
TRACE (2, "QPC speed: %.3f GHz\n", MHz/1000.0);
else TRACE (2, "QPC speed: %.0f MHz\n", MHz);
QueryPerformanceCounter (&rc);
g_data.start_ticks = rc.QuadPart;
}
static void set_time_format (TS_TYPE *ret, const char *val)
{
*ret = TS_NONE;
if (!stricmp(val, "absolute"))
*ret = TS_ABSOLUTE;
else if (!stricmp(val, "relative"))
*ret = TS_RELATIVE;
else if (!stricmp(val, "delta"))
*ret = TS_DELTA;
TRACE (4, "val: %s -> TS_TYPE: %d\n", val, *ret);
}
/**
* Return the preferred time-stamp string.
*
* \todo the below `buf[]` should be a "Thread Local Storage" variable.
* \sa https://docs.microsoft.com/en-us/windows/win32/dlls/using-thread-local-storage-in-a-dynamic-link-library
*/
const char *get_timestamp (void)
{
static LARGE_INTEGER last = {{ S64_SUFFIX(0) }};
static char buf [40];
SYSTEMTIME now;
LARGE_INTEGER ticks;
int64 clocks;
switch (g_cfg.trace_time_format)
{
// case TS_ELAPSED:
case TS_RELATIVE:
case TS_DELTA:
if (last.QuadPart == 0ULL)
last.QuadPart = g_data.start_ticks;
QueryPerformanceCounter (&ticks);
if (g_cfg.trace_time_format == TS_RELATIVE)
clocks = (int64) (ticks.QuadPart - g_data.start_ticks);
else clocks = (int64) (ticks.QuadPart - last.QuadPart);
last = ticks;
if (g_cfg.trace_time_usec)
{
double usec = (double)clocks / (double)g_data.clocks_per_usec;
int dec = (int) fmodl (usec, 1000000.0);
const char *sec = qword_str ((unsigned __int64) (usec/1000000.0));
char *p;
strcpy (buf, sec);
p = strchr (buf, '\0');
if (p) /* could be NULL due to another thread calling this function */
{
*p++ = '.';
_utoa10w (dec, 6, p);
}
strcat (buf, " sec: ");
}
else
{
double msec = (double)clocks / ((double)g_data.clocks_per_usec * 1000.0);
int dec = (int) fmodl (msec, 1000.0);
const char *sec = qword_str ((unsigned __int64) (msec/1000.0));
char *p;
strcpy (buf, sec);
p = strchr (buf, '\0');
if (p) /* could be NULL due to another thread calling this function */
{
*p++ = '.';
_utoa10w (dec, 3, p);
}
strcat (buf, " sec: ");
}
return (buf);
case TS_ABSOLUTE:
GetLocalTime (&now);
if (g_cfg.trace_time_usec)
sprintf (buf, "%02u:%02u:%02u.%06u: ", now.wHour, now.wMinute, now.wSecond, now.wMilliseconds * 1000);
else sprintf (buf, "%02u:%02u:%02u.%03u: ", now.wHour, now.wMinute, now.wSecond, now.wMilliseconds);
return (buf);
case TS_NONE:
return ("");
}
return ("");
}
/*
* Return a time-stamp in micro-seconds as a double.
* Works independently of whether 'init_timestamp()' was called or not.
*/
double get_timestamp_now (void)
{
static uint64 frequency = U64_SUFFIX(0);
LARGE_INTEGER ticks;
double usec;
if (frequency == U64_SUFFIX(0))
QueryPerformanceFrequency ((LARGE_INTEGER*)&frequency);
QueryPerformanceCounter (&ticks);
usec = 1E6 * ((double)ticks.QuadPart / (double)frequency);
return (usec);
}
/**
* Format a date string from a `SYSTEMTIME*`.
*/
const char *get_date_str (const SYSTEMTIME *st)
{
static char time [30];
static char months [3*12] = { "JanFebMarAprMayJunJulAugSepOctNovDec" };
snprintf (time, sizeof(time), "%02d %.3s %04d",
st->wDay, months + 3*(st->wMonth-1), st->wYear);
return (time);
}
/**
* Format a date/time string for current local-time.
*/
const char *get_time_now (void)
{
static char time[50];
SYSTEMTIME now;
GetLocalTime (&now);
snprintf (time, sizeof(time), "%s, %02u:%02u:%02u",
get_date_str(&now), now.wHour, now.wMinute, now.wSecond);
return (time);
}
static bool image_opt_header_is_msvc (HMODULE mod)
{
const IMAGE_DOS_HEADER *dos = (const IMAGE_DOS_HEADER*) mod;
const IMAGE_NT_HEADERS *nt = (const IMAGE_NT_HEADERS*) ((const BYTE*)mod + dos->e_lfanew);
const IMAGE_OPTIONAL_HEADER *opt = (const IMAGE_OPTIONAL_HEADER*) &nt->OptionalHeader;
TRACE (2, "opt->MajorLinkerVersion: %u, opt->MinorLinkerVersion: %u\n",
opt->MajorLinkerVersion, opt->MinorLinkerVersion);
return (opt->MajorLinkerVersion >= 10 && opt->MinorLinkerVersion == 0);
}
static bool image_opt_header_is_mingw (HMODULE mod)
{
const IMAGE_DOS_HEADER *dos = (const IMAGE_DOS_HEADER*) mod;
const IMAGE_NT_HEADERS *nt = (const IMAGE_NT_HEADERS*) ((const BYTE*)mod + dos->e_lfanew);
const IMAGE_OPTIONAL_HEADER *opt = (const IMAGE_OPTIONAL_HEADER*) &nt->OptionalHeader;
TRACE (2, "opt->MajorLinkerVersion: %u, opt->MinorLinkerVersion: %u\n",
opt->MajorLinkerVersion, opt->MinorLinkerVersion);
return (opt->MajorLinkerVersion >= 2 && opt->MajorLinkerVersion < 30);
}
static bool image_opt_header_is_cygwin (HMODULE mod)
{
const IMAGE_DOS_HEADER *dos = (const IMAGE_DOS_HEADER*) mod;
const IMAGE_NT_HEADERS *nt = (const IMAGE_NT_HEADERS*) ((const BYTE*)mod + dos->e_lfanew);
const IMAGE_OPTIONAL_HEADER *opt = (const IMAGE_OPTIONAL_HEADER*) &nt->OptionalHeader;
const IMAGE_FILE_HEADER *fh = (const IMAGE_FILE_HEADER*) &nt->FileHeader;
bool wild_tstamp = false;
/*
* File-headers in CygWin32's .EXEs often seems to contain junk:
*
* TimeDateStamp: 20202020 -> Fri Jan 30 03:38:08 1987
*
* or some time in the future.
*/
if (fh->TimeDateStamp == 0x20202020 || fh->TimeDateStamp > (DWORD)time(NULL))
wild_tstamp = true;
TRACE (2, "opt->MajorLinkerVersion: %u, opt->MinorLinkerVersion: %u, wild_tstamp: %d\n",
opt->MajorLinkerVersion, opt->MinorLinkerVersion, wild_tstamp);
return ((opt->MajorLinkerVersion >= 2 && opt->MajorLinkerVersion < 30) || wild_tstamp);
}
/*
* Check if we're linked to a program that is a GUI app.
* If we are, we might need to disable sound etc.
*/
static bool image_opt_header_is_gui_app (HMODULE mod)
{
const IMAGE_DOS_HEADER *dos = (const IMAGE_DOS_HEADER*) mod;
const IMAGE_NT_HEADERS *nt = (const IMAGE_NT_HEADERS*) ((const BYTE*)mod + dos->e_lfanew);
return (nt->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_GUI);
}
/*
* Return the next line from the config-file with key, value and
* section. Increment line of config-file.
*/
static int config_get_line (FILE *fil,
unsigned *line,
const char **key_p,
const char **val_p,
const char **section_p)
{
static char key [256], val [512], val2 [512], section [40];
static bool seen_a_section = false;
char *p, *q;
int len;
while (1)
{
char buf [500];
if (!fgets(buf, sizeof(buf)-1, fil)) /* EOF */
return (0);
for (p = buf; *p && isspace((int)*p); )
p++;
if (*p == '#' || *p == ';')
{
(*line)++;
continue;
}
if (!seen_a_section)
*section = '\0';
/*
* Hit a '[section]' line. Let the caller switch to another config-table.
*/
if (sscanf(p, "[%[^]\r\n]", section) == 1)
{
(*line)++;
*section_p = section;
seen_a_section = true;
continue;
}
if (sscanf(p, "%[^= ] = %[^\r\n]", key, val) != 2)
{
(*line)++;
continue;
}
q = strrchr (val, '\"');
p = strchr (val, ';');
/* Remove trailing comments
*/
if (p > q)
*p = '\0';
p = strchr (val, '#');
if (p > q)
*p = '\0';
/* Remove trailing space.
*/
for (len = (int)strlen(val)-1; len >= 0 && isspace((int)val[len]); )
val[len--] = '\0';
break;
}
*key_p = key;
*val_p = getenv_expand (val, val2, sizeof(val2), *line);
(*line)++;
return (1);
}
/**
* Given a C-format, extract the 1st word from it and check
* if the function / program should be excluded from tracing.
*
* We always assume that `fmt` starts with a valid name.
* Using `strnicmp()` avoids copying `fmt` into a local
* buffer first.
*/
bool exclude_list_get (const char *fmt, unsigned exclude_which)
{
size_t len;
int i, max;
/* If no tracing of any callers, that should exclude everything.
*/
if (exclude_which == EXCL_FUNCTION && g_cfg.trace_caller <= 0)
return (true);
max = exclude_list ? smartlist_len (exclude_list) : 0;
for (i = 0; i < max; i++)
{
struct exclude *ex = smartlist_get (exclude_list, i);
len = strlen (ex->name);
if ((ex->which & exclude_which) && !strnicmp(fmt, ex->name, len))
{
if (ex->only_if_prog && exclude_which == EXCL_FUNCTION && !StackWalkOurModule(ex->only_if_prog))
return (false);
ex->num_excludes++;
return (true);
}
}
return (false);
}
/**
* Free one element in `exclude_list`.
*/
static void exclude_list_free_one (void *_ex)
{
struct exclude *ex = (struct exclude*) _ex;
free (ex->name);
free (ex->only_if_prog);
free (ex);
}
/**
* Free all elements in `exclude_list`.
*/
bool exclude_list_free (void)
{
smartlist_wipe (exclude_list, exclude_list_free_one);
exclude_list = NULL;
return (true);
}
/*
* \todo: Make 'FD_ISSET' an alias for '__WSAFDIsSet'.
* Print a warning when trying to exclude an unknown Winsock function.
*/
static bool _exclude_list_add (char *name, unsigned exclude_which)
{
static const struct search_list exclude_flags[] = {
{ EXCL_NONE, "EXCL_NONE" },
{ EXCL_FUNCTION, "EXCL_FUNCTION" },
{ EXCL_PROGRAM, "EXCL_PROGRAM" },
{ EXCL_ADDRESS, "EXCL_ADDRESS" },
};
char *prog = name;
char *func = name;
char *only = NULL;
const char *which_str;
size_t len = strlen (prog);
u_char ia4 [4];
u_char ia6 [16];
char program [_MAX_PATH];
exclude_type which = EXCL_NONE;
if (exclude_which & EXCL_ADDRESS)
{
if (INET_addr_pton2(AF_INET, name, ia4) == 1 ||
INET_addr_pton2(AF_INET6, name, ia6) == 1)
which = EXCL_ADDRESS;
}
if (which == EXCL_NONE && (exclude_which & EXCL_PROGRAM))
{
if (strchr(prog+1, '"') > strchr(prog, '"'))
{
len -= 2;
prog++;
}
prog = str_ncpy (program, prog, min(len+1, sizeof(program)));
if (basename(program) > program && !file_exists(program))
TRACE (1, "EXCL_PROGRAM '%s' does not exist.\n", prog);
else which = EXCL_PROGRAM;
}
if (which == EXCL_NONE && (exclude_which & EXCL_FUNCTION))
{
if (isalpha((int)*func))
which = EXCL_FUNCTION;
/**
* Check for:
* `program!function` or
* `"c:\some quoted path\program with spaces.exe"!inet_addr`
*
* to exclude.
*/
only = strchr (func, '!');
if (only && only[1] != '\0')
{
char *p = only;
only = func;
*p++ = '\0';
prog = func = p;
/* Check for missing ".exe" in 'only'
*/
if (strnicmp(p-5, ".exe", 4))
{
str_ncpy (program, only, sizeof(program)-4);
only = strcat (program, ".exe");
}
}
else
only = NULL;
}
if (which != EXCL_NONE)
{
struct exclude *ex;
if (!exclude_list)
exclude_list = smartlist_new();
ex = malloc (sizeof(*ex));
if (ex)
{
ex->num_excludes = 0;
ex->which = which;
ex->name = strdup (prog);
ex->only_if_prog = only ? strdup(only) : NULL;
smartlist_add (exclude_list, ex);
}
}
which_str = flags_decode (which, exclude_flags, DIM(exclude_flags));
TRACE (2, "which: %-14s name: '%s', only: '%s'.\n", which_str[0] ? which_str : "unknown", prog, only ? only : "none");
return (which != EXCL_NONE);
}
/**
* Handler for `"exclude = func1, func2"` or <br>
* `"exclude = prog1, prog2"` or <br>
* `"exclude = addr1, addr2"`.
*
* If `(which & EXCL_PROGRAM) == EXCL_PROGRAM`, allow a `name` with quotes (`""`).
* But remove those before storing the `name`.
*/
bool exclude_list_add (const char *name, unsigned exclude_which)
{
const char *tok_fmt = " ,";
char *tok_end, *end;
char *p, *tok, *copy = strdup (name);
if (!copy)
return (false);
p = copy;
/* If adding a `"program with spaces.exe"`, we must use `str_tok_r (p, ",", &tok_end)`.
*/
if (exclude_which & (EXCL_PROGRAM | EXCL_FUNCTION))
{
while (*p == '"')
p++;
end = strrchr (p, '"');
if (p > copy && end > p)
*end = '\0';
tok_fmt = ",";
}
for (tok = str_tok_r(p, tok_fmt, &tok_end); tok; tok = str_tok_r(NULL, tok_fmt, &tok_end))
{
if (exclude_which & (EXCL_PROGRAM | EXCL_FUNCTION))
while (*tok == ' ')
tok++;
_exclude_list_add (tok, exclude_which);
}
free (copy);
return (true);
}
/*
* Open the config-file given by 'base_name'.
*
* First try file pointed to by %WSOCK_TRACE%,
* then in current_dir.
* then in %APPDATA%.
*/
/*
* Ignore stuff like:
* warning: '%.30s' directive output may be truncated writing 11 bytes into a region
* of size between 0 and 259 [-Wformat-truncation=]
*/
#if !defined(__clang__)
GCC_PRAGMA (GCC diagnostic push)
GCC_PRAGMA (GCC diagnostic ignored "-Wformat-truncation=")
#endif
static FILE *open_config_file (const char *base_name)
{
char *appdata, *env = getenv_expand ("WSOCK_TRACE", g_data.cfg_fname, sizeof(g_data.cfg_fname), 0);
FILE *fil;
TRACE (2, "%%WSOCK_TRACE%%=%s.\n", env);
if (env == g_data.cfg_fname)
{
if (!file_exists(g_data.cfg_fname))
{
WARNING ("%%WSOCK_TRACE=\"%s\" does not exist.\nRunning with default values.\n", env);
return (NULL);
}
}
else
snprintf (g_data.cfg_fname, sizeof(g_data.cfg_fname), "%s\\%.30s", g_data.curr_dir, base_name);
fil = fopen (g_data.cfg_fname, "r");
if (!fil)
{
appdata = getenv ("APPDATA");
if (appdata)
{
snprintf (g_data.cfg_fname, sizeof(g_data.cfg_fname), "%s\\%s", appdata, base_name);
fil = fopen (g_data.cfg_fname, "r");
}
}
TRACE (2, "config-file: \"%s\". %sfound.\n", g_data.cfg_fname, fil ? "" : "not ");
return (fil);
}
#if !defined(__clang__)
GCC_PRAGMA (GCC diagnostic pop)
#endif
/*
* Handler for default section or '[core]' section.
*/
static void parse_core_settings (const char *key, const char *val, unsigned line)
{
if (!stricmp(key, "trace_level"))
g_cfg.trace_level = atoi (val);
else if (!stricmp(key, "trace_overlap"))
g_cfg.trace_overlap = atoi (val);
else if (!stricmp(key, "trace_file"))
g_cfg.trace_file = strdup (val);
else if (!stricmp(key, "trace_binmode"))
g_cfg.trace_binmode = atoi (val);
else if (!stricmp(key, "trace_file_commit"))
g_cfg.trace_file_commit = atoi (val);
else if (!stricmp(key, "trace_caller"))
g_cfg.trace_caller = atoi (val);
else if (!stricmp(key, "trace_indent"))
{
g_cfg.trace_indent = atoi (val);
g_cfg.trace_indent = max (0, g_cfg.trace_indent);
}
else if (!stricmp(key, "trace_report"))
g_cfg.trace_report = atoi (val);
else if (!stricmp(key, "trace_max_len") || !stricmp(key, "trace_max_length"))
g_cfg.trace_max_len = atoi (val);
else if (!stricmp(key, "trace_time"))
set_time_format (&g_cfg.trace_time_format, val);
else if (!stricmp(key, "trace_time_usec"))
g_cfg.trace_time_usec = atoi (val);
else if (!stricmp(key, "pcap_enable"))
g_cfg.PCAP.enable = atoi (val);
else if (!stricmp(key, "pcap_dump"))
g_cfg.PCAP.dump_fname = strdup (val);
else if (!stricmp(key, "show_caller"))
g_cfg.show_caller = atoi (val);
else if (!stricmp(key, "show_tid"))
g_cfg.show_tid = atoi (val);
else if (!stricmp(key, "demangle") || !stricmp(key, "cpp_demangle"))
g_cfg.cpp_demangle = atoi (val);
else if (!stricmp(key, "callee_level"))
g_cfg.callee_level = atoi (val); /* Control how many stack-frames to show. Not used yet */
else if (!stricmp(key, "exclude"))
exclude_list_add (val, EXCL_FUNCTION);
else if (!stricmp(key, "hook_extensions"))
g_cfg.hook_extensions = atoi (val);
else if (!stricmp(key, "short_errors"))
g_cfg.short_errors = atoi (val);
else if (!stricmp(key, "pdb_report"))
g_cfg.pdb_report = atoi (val);
else if (!stricmp(key, "pdb_symsrv"))
g_cfg.pdb_symsrv = atoi (val);
else if (!stricmp(key, "use_sema"))
g_cfg.use_sema = atoi (val);
else if (!stricmp(key, "recv_delay"))
g_cfg.recv_delay = (DWORD) _atoi64 (val);
else if (!stricmp(key, "send_delay"))
g_cfg.send_delay = (DWORD) _atoi64 (val);
else if (!stricmp(key, "select_delay"))
g_cfg.select_delay = (DWORD) _atoi64 (val);
else if (!stricmp(key, "poll_delay"))
g_cfg.poll_delay = (DWORD) _atoi64 (val);
else if (!stricmp(key, "use_toolhlp32"))
g_cfg.use_toolhlp32 = atoi (val);
else if (!stricmp(key, "use_ole32"))
g_cfg.use_ole32 = atoi (val);
else if (!stricmp(key, "use_full_path"))
g_cfg.use_full_path = atoi (val);
else if (!stricmp(key, "use_short_path"))
g_cfg.use_short_path = atoi (val);
else if (!stricmp(key, "color_file"))
get_color (val, &g_cfg.color_file);
else if (!stricmp(key, "color_time"))
get_color (val, &g_cfg.color_time);
else if (!stricmp(key, "color_func"))
get_color (val, &g_cfg.color_func);
else if (!stricmp(key, "color_trace"))
get_color (val, &g_cfg.color_trace);
else if (!stricmp(key, "color_data"))
get_color (val, &g_cfg.color_data);
else if (!stricmp(key, "nice_numbers"))
g_cfg.nice_numbers = atoi (val);
else if (!stricmp(key, "compact"))
g_cfg.compact = atoi (val);
else if (!stricmp(key, "dump_select"))
g_cfg.dump_select = atoi (val);
else if (!stricmp(key, "dump_nameinfo"))
g_cfg.dump_nameinfo = atoi (val);
else if (!stricmp(key, "dump_addrinfo"))
g_cfg.dump_addrinfo = atoi (val);
else if (!stricmp(key, "dump_protoent"))
g_cfg.dump_protoent = atoi (val);
else if (!stricmp(key, "dump_modules"))
g_cfg.dump_modules = atoi (val);
else if (!stricmp(key, "dump_hostent"))
g_cfg.dump_hostent = atoi (val);
else if (!stricmp(key, "dump_servent"))
g_cfg.dump_servent = atoi (val);
else if (!stricmp(key, "dump_data"))
g_cfg.dump_data = atoi (val);
else if (!stricmp(key, "dump_wsaprotocol_info"))
g_cfg.dump_wsaprotocol_info = atoi (val);
else if (!stricmp(key, "dump_wsanetwork_events"))
g_cfg.dump_wsanetwork_events = atoi (val);
else if (!stricmp(key, "dump_namespace_providers"))
g_cfg.dump_namespace_providers = atoi (val);
else if (!stricmp(key, "dump_tcpinfo"))
g_cfg.dump_tcpinfo = atoi (val);
else if (!stricmp(key, "dump_icmp_info"))
g_cfg.dump_icmp_info = atoi (val);
else if (!stricmp(key, "fail_WSAStartup"))
g_cfg.fail_WSAStartup = atoi (val);
else if (!stricmp(key, "max_data"))
g_cfg.max_data = atoi (val);
else if (!stricmp(key, "max_fd_set") || !stricmp(key, "max_fd_sets"))
g_cfg.max_fd_sets = atoi (val);
else if (!stricmp(key, "max_displacement"))
g_cfg.max_displacement = atoi (val);
else if (!stricmp(key, "start_new_line"))
g_cfg.start_new_line = atoi (val);
else if (!stricmp(key, "extra_new_line"))
g_cfg.extra_new_line = atoi (val);
else if (!stricmp(key, "msvc_only"))
g_cfg.msvc_only = atoi (val);
else if (!stricmp(key, "mingw_only"))
g_cfg.mingw_only = atoi (val);
else if (!stricmp(key, "cygwin_only"))
g_cfg.cygwin_only = atoi (val);
else if (!stricmp(key, "no_buffering"))
g_cfg.no_buffering = atoi (val);
else if (!stricmp(key, "no_inv_handler"))
g_cfg.no_inv_handler = atoi (val);
else if (!stricmp(key, "use_winhttp"))
; /* dropped WinHTTP.dll in favour of WinInet.dll */
else if (!stricmp(key, "hosts_file"))
{
if (g_cfg.num_hosts_files < DIM(g_cfg.hosts_file)-1)
g_cfg.hosts_file [g_cfg.num_hosts_files++] = strdup (val);
}
else if (!stricmp(key, "services_file"))
{
if (g_cfg.num_services_files < DIM(g_cfg.services_file)-1)
g_cfg.services_file [g_cfg.num_services_files++] = strdup (val);
}
else if (g_cfg.trace_level >= 1)
debug_printf (NULL, 0, "%s (%u):\n Unknown keyword '%s' = '%s'\n",
g_data.cfg_fname, line, key, val);
}
/*
* Handler for '[lua]' section.
*/
static void parse_lua_settings (const char *key, const char *val, unsigned line)
{
if (!stricmp(key, "enable"))
g_cfg.LUA.enable = atoi (val);
else if (!stricmp(key, "trace_level"))
g_cfg.LUA.trace_level = atoi (val);
else if (!stricmp(key, "profile"))
g_cfg.LUA.profile = atoi (val);
else if (!stricmp(key, "color_head"))
get_color (val, &g_cfg.LUA.color_head);
else if (!stricmp(key, "color_body"))
get_color (val, &g_cfg.LUA.color_body);
else if (!stricmp(key, "lua_init"))
g_cfg.LUA.init_script = strdup (val);
else if (!stricmp(key, "lua_exit"))
g_cfg.LUA.exit_script = strdup (val);
else TRACE (1, "%s (%u):\n Unknown keyword '%s' = '%s'\n",
g_data.cfg_fname, line, key, val);
}
/*
* Handler for '[geoip]' section.
*/
static void parse_geoip_settings (const char *key, const char *val, unsigned line)
{
if (!stricmp(key, "enable"))
g_cfg.GEOIP.enable = (*val > '0') ? true : false;
else if (!stricmp(key, "show_position"))
g_cfg.GEOIP.show_position = atoi (val);
else if (!stricmp(key, "show_map_url"))
g_cfg.GEOIP.show_map_url = atoi (val);
else if (!stricmp(key, "openstreetmap"))
g_cfg.GEOIP.openstreetmap = atoi (val);
else if (!stricmp(key, "map_zoom"))
g_cfg.GEOIP.map_zoom = atoi (val);
else if (!stricmp(key, "ip4_file"))
g_cfg.GEOIP.ip4_file = strdup (val);
else if (!stricmp(key, "ip6_file"))
g_cfg.GEOIP.ip6_file = strdup (val);
else if (!stricmp(key, "ip4_url"))
g_cfg.GEOIP.ip4_url = strdup (val);
else if (!stricmp(key, "ip6_url"))
g_cfg.GEOIP.ip6_url = strdup (val);
else if (!stricmp(key, "proxy"))
g_cfg.GEOIP.proxy = strdup (val);
else if (!stricmp(key, "max_days"))
g_cfg.GEOIP.max_days = atoi (val);
else if (!stricmp(key, "ip2location_bin_file"))
{
if (g_cfg.GEOIP.ip2location_bin_file)
{
WARNING ("'ip2location_bin_file' already set. Replacing with '%s'\n", val);
free (g_cfg.GEOIP.ip2location_bin_file);
}
g_cfg.GEOIP.ip2location_bin_file = strdup (val);
}
else TRACE (1, "%s (%u):\n Unknown keyword '%s' = '%s'\n",
g_data.cfg_fname, line, key, val);
}
/*
* Handler for '[idna]' section.
*/
static void parse_idna_settings (const char *key, const char *val, unsigned line)
{
if (!stricmp(key, "enable"))
g_cfg.IDNA.enable = atoi (val);
else if (!stricmp(key, "use_winidn"))
g_cfg.IDNA.use_winidn = atoi (val);
else if (!stricmp(key, "fix_getaddrinfo"))
g_cfg.IDNA.fix_getaddrinfo = atoi (val);
else if (!stricmp(key, "codepage"))
g_cfg.IDNA.codepage = atoi (val);
else TRACE (1, "%s (%u):\n Unknown keyword '%s' = '%s'\n",
g_data.cfg_fname, line, key, val);
}
/*
* Handler for '[DNSBL]' section.
*/
static void parse_DNSBL_settings (const char *key, const char *val, unsigned line)
{
if (!stricmp(key, "enable"))
g_cfg.DNSBL.enable = atoi (val);
else if (!stricmp(key, "drop_file"))
g_cfg.DNSBL.drop_file = strdup (val);
else if (!stricmp(key, "dropv6_file"))
g_cfg.DNSBL.dropv6_file = strdup (val);
else if (!stricmp(key, "edrop_file"))
g_cfg.DNSBL.edrop_file = strdup (val);
else if (!stricmp(key, "drop_url"))
g_cfg.DNSBL.drop_url = strdup (val);
else if (!stricmp(key, "dropv6_url"))
g_cfg.DNSBL.dropv6_url = strdup (val);
else if (!stricmp(key, "edrop_url"))
g_cfg.DNSBL.edrop_url = strdup (val);
else if (!stricmp(key, "max_days"))
g_cfg.DNSBL.max_days = atoi (val);
else TRACE (1, "%s (%u):\n Unknown keyword '%s' = '%s'\n",
g_data.cfg_fname, line, key, val);
}
/*
* parse a "Hertz, mill-sec" value for the '[firewall]' section.
*/
static void get_freq_msec (const char *val, struct FREQ_MILLISEC *out)
{
struct FREQ_MILLISEC fr = { 0, 0 };
int num = sscanf (val, "%u,%u", &fr.frequency, &fr.milli_sec);
if (num == 2)
{
out->frequency = fr.frequency;
out->milli_sec = fr.milli_sec;