forked from sleinen/samplicator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_config.c
968 lines (873 loc) · 25.2 KB
/
read_config.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
/*
read_config.c
Date Created: Sun Sep 18 22:03:40 2011
Author: Simon Leinen <[email protected]>
*/
#include "config.h"
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#include <stdarg.h>
#include <sys/types.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <sys/socket.h>
#include <netinet/in.h>
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#if STDC_HEADERS
#define bzero(b,n) memset(b,0,n)
#else
#include <strings.h>
#ifndef HAVE_MEMCPY
#define memcpy(d, s, n) bcopy ((s), (d), (n))
#endif
#endif
#ifdef HAVE_CTYPE_H
#include <ctype.h>
#endif
#include "samplicator.h"
#include "read_config.h"
#include "inet.h"
#include "rawsend.h"
#define PORT_SEPARATOR '/'
#define FREQ_SEPARATOR '/'
#define TTL_SEPARATOR ','
#define FLOWPORT "2000"
#define DEFAULT_SOCKBUFLEN 65536
#define DEFAULT_PDULEN 65536
#define MAX_PEERS 100
#define MAX_LINELEN 8000
static int parse_line (struct samplicator_context *, char *, const char *);
static int parse_addr_mask (const char *, const char *,
const struct samplicator_context *,
struct sockaddr_storage *, struct sockaddr_storage *, socklen_t *);
static void short_usage (const char *);
static void usage (const char *);
static int
parse_error (const struct samplicator_context *ctx, const char *fmt, ...)
{
va_list fmt_args;
va_start (fmt_args, fmt);
fprintf (stderr, "%s, line %d: ", ctx->config_file_name, ctx->config_file_lineno);
vfprintf (stderr, fmt, fmt_args);
fprintf (stderr, "\n");
va_end (fmt_args);
return -1;
}
/* copy_string_start_end (start, end)
Copy a string defined by a start and end pointer to a fresh
null-terminated string. This is useful when using library
functions that expect such strings.
Will return a null pointer if allocating space fails.
The copy should be free()'d when the caller is done with it.
*/
static char *
copy_string_start_end (start, end)
const char *start;
const char *end;
{
size_t len = end - start;
char *copy = malloc (len + 1);
if (copy == 0)
return 0;
strncpy (copy, start, len);
copy[len] = 0;
return copy;
}
/* read_cf_file (file, ctx)
Read configuration file FILE into samplicator context CTX.
The file is opened and parsed line by line. The parser fills in
values of CTX.
The file name and a line counter are stored in CTX for use in
parser error messages.
Return value:
0, if the file could be parsed.
-1, if an error occurred during parsing.
If an error occurs, the parser will give up immediately. Contents
preceding the errors may have been processed and filled into CTX.
*/
int
read_cf_file (file, ctx)
const char *file;
struct samplicator_context *ctx;
{
FILE *cf;
char tmp_s[MAX_LINELEN];
ctx->config_file_name = file;
ctx->config_file_lineno = 0;
if ((cf = fopen (file, "r")) == NULL)
{
fprintf (stderr, "read_cf_file: cannot open %s. Aborting.\n", file);
return -1;
}
while (!feof (cf))
{
if (fgets (tmp_s, MAX_LINELEN, cf) == (char *) 0)
{
break;
}
++ctx->config_file_lineno;
if (parse_line (ctx, tmp_s, tmp_s + strlen (tmp_s)) == -1)
{
return -1;
}
}
fclose (cf);
return 0;
}
/* resolve_addr (string, ctx, addrp, addrlenp)
Parses, and possibly resolves, an IP address given as a string.
The preferences in CTX are used to determine whether to return an
IPv4 or IPv6 address.
The address is stored in the sockaddr_storage structure pointed to
by ADDRP.
If ADDRLENP is non-null, it the length of the address structure
will be stored to it.
If an error occurs during parsing or resolution, the function will
return -1, and nothing will be stored in ADDRP or ADDRLENP.
*/
static int
resolve_addr (const char *addrstring,
const struct samplicator_context *ctx, struct sockaddr_storage *addrp, socklen_t * addrlenp)
{
struct addrinfo hints, *res;
init_hints_from_preferences (&hints, ctx);
if (getaddrinfo (addrstring, 0, &hints, &res) != 0 || res == 0)
{
return parse_error (ctx, "Could not parse address %s", addrstring);
}
memcpy (addrp, res->ai_addr, res->ai_addrlen);
if (addrlenp != 0)
*addrlenp = res->ai_addrlen;
freeaddrinfo (res);
return 0;
}
static int
parse_addr_1 (const char *start,
const char *end,
const struct samplicator_context *ctx, struct sockaddr_storage *addrp, socklen_t * addrlenp)
{
char *copy = copy_string_start_end (start, end);
int result;
if (copy == 0)
{
return parse_error (ctx, "Out of memory");
}
result = resolve_addr (copy, ctx, addrp, addrlenp);
free (copy);
return result;
}
static int
parse_addr (const char *start,
const char *end,
const struct samplicator_context *ctx, struct sockaddr_storage *addrp, socklen_t * addrlenp)
{
while (start < end && isspace (*start))
++start;
while (start < end && isspace (*(end - 1)))
--end;
if (start < end && *start == '[')
{
++start;
if (start < end && *(end - 1) == ']')
--end;
else
{
return parse_error (ctx, "Mismatched brackets");
}
}
return parse_addr_1 (start, end, ctx, addrp, addrlenp);
}
static void
set_ipv4_netmask (struct sockaddr_in *maskp, int preflen)
{
in_addr_t *addrp = &maskp->sin_addr.s_addr;
int k;
unsigned long bit;
uint32_t haddr;
haddr = 0xffffffff;
preflen = 32 - preflen;
for (k = 0, bit = 1; k < preflen; ++k, bit <<= 1)
{
haddr &= ~bit;
}
*addrp = htonl (haddr);
}
static void
set_ipv6_netmask (struct sockaddr_in6 *maskp, int preflen)
{
uint8_t *addrp = maskp->sin6_addr.s6_addr;
int k;
unsigned bit;
unsigned octet_index;
memset (addrp, 0, 16);
for (k = 0, bit = 128, octet_index = 0; k < preflen; ++k, bit >>= 1)
{
if (bit < 1)
{
bit = 128, octet_index++;
}
addrp[octet_index] |= bit;
}
}
static int
parse_mask (const char *start,
const char *end,
const struct samplicator_context *ctx,
struct sockaddr_storage *maskp, struct sockaddr_storage *addrp)
{
if (addrp->ss_family == AF_INET)
{
const char *cp = start;
while (cp < end && *cp != '.')
++cp;
if (cp < end) /* contains a dot, assume full netmask */
{
socklen_t addrlen1;
if (parse_addr_1 (start, end, ctx, maskp, &addrlen1) != 0)
{
return parse_error (ctx, "Cannot parse mask");
}
else
{
if (addrlen1 != sizeof (struct sockaddr_in))
{
return parse_error (ctx, "Inconsistent addr/mask structures");
}
return 0;
}
}
else
{ /* no dot, assume this is a prefix length */
int preflen;
char *int_end;
preflen = strtol (start, &int_end, 10);
if (int_end == start || int_end < end || preflen < 0 || preflen > 32)
{
return parse_error (ctx, "Bogus prefix length");
}
bzero (maskp, sizeof (struct sockaddr_in));
((struct sockaddr_in *) maskp)->sin_family = AF_INET;
set_ipv4_netmask ((struct sockaddr_in *) maskp, preflen);
return 0;
}
}
else if (addrp->ss_family == AF_INET6)
{
int preflen;
char *int_end;
preflen = strtol (start, &int_end, 10);
if (int_end == start || int_end < end || preflen < 0 || preflen > 128)
{
return parse_error (ctx, "Bogus prefix length");
}
bzero (maskp, sizeof (struct sockaddr_in6));
((struct sockaddr_in6 *) maskp)->sin6_family = AF_INET6;
set_ipv6_netmask ((struct sockaddr_in6 *) maskp, preflen);
return 0;
}
else
{
return parse_error (ctx, "Unsupported address family");
}
}
static int
set_default_mask (const struct samplicator_context *ctx,
struct sockaddr_storage *maskp, struct sockaddr_storage *addrp)
{
if (addrp->ss_family == AF_INET)
{
bzero (maskp, sizeof (struct sockaddr_in));
maskp->ss_family = AF_INET;
memset (&((struct sockaddr_in *) maskp)->sin_addr, 0xff, 4);
return 0;
}
else if (addrp->ss_family == AF_INET6)
{
bzero (maskp, sizeof (struct sockaddr_in6));
maskp->ss_family = AF_INET6;
memset (&((struct sockaddr_in6 *) maskp)->sin6_addr, 0xff, 16);
return 0;
}
return parse_error (ctx, "Unknown address family %d", addrp->ss_family);
}
static int
parse_addr_mask (start, end, ctx, addrp, maskp, addrlenp)
const char *start, *end;
const struct samplicator_context *ctx;
struct sockaddr_storage *addrp;
struct sockaddr_storage *maskp;
socklen_t *addrlenp;
{
const char *c, *slash = 0;
int unmatched = -1;
/*
an unmatched source is denoted by * in the configuration file
packets from a source not explicitly matched from the standard
sources will be forwarded to these receivers
a standard source of 0.0.0.0/0 prevents the unmatched category
*/
c = start;
while (c < end)
{
if (*c == '/')
slash = c;
/* asterick signifies unmatched sources */
else if (*c == '*')
unmatched = 1;
c++;
}
/* found marker for unmatched sources */
if (unmatched > 0)
{
/*
set match criteria to 0.0.0.0/0 so everything that hits this
category will match
*/
char *zero = "0.0.0.0";
resolve_addr (zero, ctx, addrp, addrlenp);
resolve_addr (zero, ctx, maskp, addrlenp);
return CAT_UNMATCHED;
}
if (parse_addr (start, slash == 0 ? end : slash, ctx, addrp, addrlenp) == -1)
return -1;
if (slash != 0)
{
if (parse_mask (slash + 1, end, ctx, maskp, addrp) == -1)
return -1;
}
else
set_default_mask (ctx, maskp, addrp);
return CAT_STANDARD;
}
/*
parse_line (ctx, start, end)
Parse a single line of configuration file.
*/
static int
parse_line (ctx, start, end)
struct samplicator_context *ctx;
char *start;
const char *end;
{
struct source_context *sctx;
int argc;
const char *argv[MAX_PEERS];
const char *c, *e;
const char *lhs_start, *lhs_end;
const char *rhs_start;
/* move end before the start of any comment at the end of the line,
and before any whitepace preceding such a comment or EOL. */
for (c = start; c < end && *c != '#'; ++c);
if (c < end)
end = c;
while (end > start && isspace (*(end - 1)))
--end;
if (start == end)
return 0; /* empty line; skip. */
/* non-empty lines should look like this:
ipadd[/mask]: dest[:port[/freq][,ttl]] dest2[:port2[/freq2][,ttl2]]...
The problematic case is where the ipadd is an IPv6 address,
because IPv6 addresses usually contain colons. We insist on the
convention that IPv6 addresses be enclosed in brackets. In
addition, for IPv6 we only accept prefix lengths, not arbitrary
netmasks.
[2001:db0:0:1::]/64: [2001:db0:0:2::3]:8000 [2001:db0:0:2::4]:8000
*/
c = start;
while (c < end && isspace (*c))
++c;
lhs_start = c;
if (*c == '[')
{
while (c < end && *c != ']')
++c;
while (c < end && *c != ':')
++c;
}
else
{
c = start;
while (c < end && *c != ':')
++c;
}
/* Now c either points at the colon that separates the left-hand
address/mask from the right hand destinations, or c is equal to
end because such a colon was not found. */
if (c < end)
{
lhs_end = c;
++c; /* skip colon */
while (c < end && isspace (*c))
++c;
rhs_start = c;
sctx = calloc (1, sizeof (struct source_context));
int ret = parse_addr_mask (lhs_start, lhs_end, ctx,
&sctx->source, &sctx->mask, &sctx->addrlen);
if (ret < 0)
return -1;
argc = 0;
while (c < end)
{
while (c < end && isspace (*c))
c++;
if (c >= end)
break;
e = c;
while ((*e != 0) && !isspace ((int) *e))
e++;
argv[argc++] = copy_string_start_end (c, e);
c = e;
if (c < end)
c++;
}
/*
no receivers is now treated as a blacklisted source
where packets are not forwarded
prevent the combination of blacklist and unmatched
*/
unsigned category = (unsigned) ret;
if (category == CAT_UNMATCHED && argc == 0)
return -1;
if (argc == 0)
category = CAT_BLACKLIST;
if (parse_receivers (argc, argv, ctx, sctx, category) == -1)
return -1;
}
else
{
return parse_error (ctx, "Missing colon");
}
return 0;
}
static int
parse_receiver (struct receiver *receiverp, const char *arg, struct samplicator_context *ctx)
{
const char *start, *end;
const char *host_start, *host_end;
char portspec[NI_MAXSERV];
struct addrinfo hints, *res;
int result;
receiverp->flags = ctx->default_receiver_flags;
receiverp->freqcount = 0;
receiverp->freq = 1;
receiverp->ttl = DEFAULT_TTL;
start = arg;
end = start + strlen (arg);
while (start < end && isspace (*start))
++start;
while (start < end && isspace (*(end - 1)))
--end;
if (start < end && *start == '[')
{
host_end = host_start = start + 1;
while (host_end < end && *host_end != ']')
++host_end;
if (host_end == end)
{
return parse_error (ctx, "Missing closing bracket");
}
start = host_end + 1;
}
else
{
host_end = host_start = start;
while (host_end < end && *host_end != PORT_SEPARATOR)
++host_end;
start = host_end;
}
/* extract the port part */
if (*start == PORT_SEPARATOR)
{
const char *port_start, *port_end;
++start;
port_end = port_start = start;
while (port_end < end && *port_end != FREQ_SEPARATOR)
++port_end;
if (port_end < end)
{
const char *freq_start, *freq_end;
freq_end = freq_start = port_end + 1;
/* extract the frequency part */
while (freq_end < end && *freq_end != TTL_SEPARATOR)
++freq_end;
if (freq_start < freq_end)
{
int freq;
char *freq_parse_end;
freq = strtol (freq_start, &freq_parse_end, 10);
if (freq_parse_end == freq_start || freq_parse_end != freq_end || freq < 1)
{
return parse_error (ctx, "Illegal frequency .*s", freq_end - freq_start, freq_start);
}
else
{
receiverp->freq = freq;
}
}
if (freq_end < end)
{
int ttl;
const char *ttl_start = freq_end + 1;
const char *ttl_end = end;
char *ttl_parse_end;
ttl = strtol (ttl_start, &ttl_parse_end, 10);
if (ttl_parse_end == ttl_start || ttl_parse_end != ttl_end || ttl < 1 || ttl > 255)
{
return parse_error (ctx, "Illegal TTL");
}
else
{
receiverp->ttl = ttl;
}
}
}
if (port_end - port_start >= NI_MAXSERV)
{
return parse_error (ctx, "Service name/port number (%.*s) too long", port_end - port_start, port_start);
}
strncpy (portspec, port_start, port_end - port_start);
portspec[port_end - port_start] = 0;
}
else
strcpy (portspec, FLOWPORT);
init_hints_from_preferences (&hints, ctx);
{
char *tmp_buf = copy_string_start_end (host_start, host_end);
if (tmp_buf == 0)
{
return parse_error (ctx, "Out of memory");
}
result = getaddrinfo (tmp_buf, portspec, &hints, &res);
if (result != 0)
{
return parse_error (ctx, "Parsing IP address (%s with port spec %s) failed: %s",
tmp_buf, portspec, gai_strerror (result));
}
memcpy (&receiverp->addr, res->ai_addr, res->ai_addrlen);
receiverp->addrlen = res->ai_addrlen;
freeaddrinfo (res);
return 0;
}
}
int
expand_port_ranges (argc, argv, exp_argc, exp_argv)
int argc;
const char **argv;
int *exp_argc;
const char ***exp_argv;
{
int j, k;
*exp_argc = 0;
*exp_argv = NULL;
/* expand port definition ranges */
for (j = 0; j < argc; j++)
{
char *port_begin, *port_end = NULL;
int just_copy = 0;
port_begin = strchr (argv[j], PORT_SEPARATOR);
if (port_begin == NULL)
just_copy = 1;
else
{
char *range_start = NULL, *inc_start = NULL;
range_start = strchr (port_begin, '-');
inc_start = strchr (port_begin, '+');
if (!range_start && !inc_start)
just_copy = 1;
else
{
int first = atoi (port_begin + 1);
int last;
char *suffix;
if (range_start && inc_start)
{
fprintf (stderr, "Invalid port specification: at %s\n", port_begin);
exit (1);
}
if (range_start)
{
last = atoi (range_start + 1);
suffix = range_start + 1;
}
if (inc_start)
{
last = first + atoi (inc_start + 1) - 1;
suffix = inc_start + 1;
}
while (*suffix && isdigit (*suffix))
suffix++;
for (k = first; k <= last; k++)
{
char *newarg = (char *) malloc (strlen (argv[j] + 1));
if (!newarg)
return -1;
memcpy (newarg, argv[j], port_begin - argv[j] + 1);
sprintf (newarg + (port_begin - argv[j]) + 1, "%d%s", k, suffix);
*exp_argv = (const char **) realloc (*exp_argv, (1 + *exp_argc) * sizeof (char *));
if (*exp_argv == NULL)
return -1;
(*exp_argv)[*exp_argc] = newarg;
*exp_argc = *exp_argc + 1;
}
}
}
if (just_copy)
{
/* Let parse receiver handle this case */
*exp_argv = (const char **) realloc (*exp_argv, (1 + *exp_argc) * sizeof (char *));
if (*exp_argv == NULL)
return -1;
(*exp_argv)[*exp_argc] = argv[j];
*exp_argc = *exp_argc + 1;
}
}
return 0;
}
int
parse_receivers (argc, argv, ctx, sctx, category)
int argc;
const char **argv;
struct samplicator_context *ctx;
struct source_context *sctx;
unsigned category;
{
/* validate category is within bounds */
if (category >= CAT_UNKNOWN)
return -1;
int i;
int exp_argc;
const char **exp_argv;
if (expand_port_ranges (argc, argv, &exp_argc, &exp_argv))
{
fprintf (stderr, "Out of memory.");
exit (1);
}
/* allocate for argc receiver entries */
sctx->nreceivers = exp_argc;
if (!(sctx->receivers = (struct receiver *) calloc (sctx->nreceivers, sizeof (struct receiver))))
{
return parse_error (ctx, "Out of memory");
}
/* blacklist has no receivers */
if (category != CAT_BLACKLIST)
{
/* fill in receiver entries */
for (i = 0; i < exp_argc; ++i)
{
if (parse_receiver (&sctx->receivers[i], exp_argv[i], ctx) != 0)
return -1;
}
}
/* append to the correct source_context category */
if (ctx->sources[category] == NULL)
{
ctx->sources[category] = sctx;
}
else
{
struct source_context *ptr;
for (ptr = ctx->sources[category]; ptr->next != NULL; ptr = ptr->next);
ptr->next = sctx;
}
return 0;
}
int
parse_args (argc, argv, ctx)
int argc;
const char **argv;
struct samplicator_context *ctx;
{
extern char *optarg;
extern int errno, optind;
int i;
ctx->config_file_name = "<command line>";
ctx->config_file_lineno = 1;
ctx->sockbuflen = DEFAULT_SOCKBUFLEN;
ctx->pdulen = DEFAULT_PDULEN;
ctx->tx_delay = 0;
ctx->faddr_spec = 0;
bzero (&ctx->faddr, sizeof ctx->faddr);
ctx->fport_spec = FLOWPORT;
ctx->debug = 0;
ctx->timeout = 0;
ctx->ipv4_only = 0;
ctx->ipv6_only = 0;
ctx->fork = 0;
ctx->pid_file = (const char *) 0;
ctx->default_receiver_flags = pf_CHECKSUM;
unsigned category;
for (category = CAT_BLACKLIST; category < NUM_CATEGORIES; category++)
ctx->sources[category] = NULL;
category = CAT_STANDARD;
optind = 1;
while ((i = getopt (argc, (char **) argv, "hu:b:d:t:m:p:s:x:c:fSn46X")) != -1)
{
switch (i)
{
case 'b': /* buflen */
ctx->sockbuflen = atol (optarg);
break;
case 'u': /* pdu length */
ctx->pdulen = atol (optarg);
break;
case 'd': /* debug */
ctx->debug = atoi (optarg);
break;
case 't': /* Timeout */
ctx->timeout = atoi (optarg);
break;
case 'n': /* no UDP checksums */
ctx->default_receiver_flags &= ~pf_CHECKSUM;
break;
case 'p': /* flow port */
ctx->fport_spec = optarg;
break;
case 'm': /* make PID file */
ctx->pid_file = optarg;
break;
case 's': /* flow address */
ctx->faddr_spec = optarg;
break;
case 'x': /* transmit delay */
ctx->tx_delay = atoi (optarg);
break;
case 'S': /* spoof */
ctx->default_receiver_flags |= pf_SPOOF;
break;
case 'c': /* config file */
if (read_cf_file (optarg, ctx) != 0)
{
return -1;
}
break;
case 'f': /* fork */
ctx->fork = 1;
break;
case 'h': /* help */
usage (argv[0]);
exit (0);
break;
case '4':
ctx->ipv6_only = 0;
ctx->ipv4_only = 1;
break;
case '6':
ctx->ipv4_only = 0;
ctx->ipv6_only = 1;
break;
case 'X':
/* uses command line receivers for unmatched sources */
category = CAT_UNMATCHED;
break;
default:
short_usage (argv[0]);
return -1;
}
}
if (argc - optind > 0)
{
/* assume that command-line supplied receivers want to get all data */
struct source_context *sctx = calloc (1, sizeof (struct source_context));
if (sctx == 0)
{
fprintf (stderr, "Out of memory\n");
return -1;
}
char *zero = "0.0.0.0";
resolve_addr (zero, ctx, &sctx->source, &sctx->addrlen);
resolve_addr (zero, ctx, &sctx->mask, &sctx->addrlen);
if (parse_receivers (argc - optind, argv + optind, ctx, sctx, category) == -1)
{
short_usage (argv[0]);
return -1;
}
}
return 0;
}
void
short_usage (progname)
const char *progname;
{
fprintf (stderr, "Run \"%s -h\" for usage information.\n", progname);
}
static void
usage (progname)
const char *progname;
{
fprintf (stderr, "Usage: %s [option...] receiver...\n\
\n\
Supported options:\n\
\n\
-p <port> UDP port to accept flows on (default %s)\n\
-s <address> Interface address to accept flows on (default any)\n\
-d <level> debug level\n\
-t <timeout_ms> Exit with RC 5 if no data is received for this\n\
amount of milliseconds\n\
-b <size> set socket buffer size (default %lu)\n\
-n don't compute UDP checksum (leave at 0)\n\
-S maintain (spoof) source addresses\n\
-x <delay> transmit delay in microseconds\n\
-c <configfile> specify a config file to read\n\
-f fork program into background\n\
-m <pidfile> write process ID to file\n\
-4 IPv4 only\n\
-6 IPv6 only\n\
-h print this usage message and exit\n\
-u <pdulen> size of max pdu on listened socket (default 65536)\n\
-X use command line receivers for unmatched sources\n\
\n\
Specifying receivers:\n\
\n\
A.B.C.D[%cport[%cfreq][%cttl]]...\n\
where:\n\
A.B.C.D is the receiver's IP address\n\
port is the UDP port to send to (default %s)\n\
freq is the sampling rate (default 1)\n\
ttl is the outgoing packets' TTL value (default %d)\n\
\n\
The port can be a number, a range, or a number plus the number of instances:\n\
7000 means port 7000\n\
7000-7010 means from port 7000 to 7010 (both inclusive)\n\
7000+10 means 10 instances starting at 7000, so 7000 to 7009\n\
\n\
Config file format:\n\
\n\
source: [receiver ...]\n\
\n\
a.b.c.d[/e.f.g.h]: receiver ...\n\
a.b.c.d[/e.f.g.h]:\n\
*: receiver ...\n\
\n\
where:\n\
a.b.c.d is the senders IP address\n\
e.f.g.h is a mask to apply to the sender (default 255.255.255.255)\n\
* denotes receivers for unmatched sources\n\
receiver see above\n\
\n\
The source address of incoming packets is matched against three categories in\n\
order: 1) blacklist, 2) standard, 3) unmatched.\n\
Source addresses will only be processed by the first category where a match\n\
is found.\n\
\n\
Receivers from *all* matching sources defined in the configuration will get\n\
copies as sources may overlap with masks. Sources defined without a receiver\n\
are the exception. They are explicitly blacklisted and dropped before any\n\
further matches are processed.\n\
\n\
The wildcard source is used to denote the unmatched category. A source address\n\
that does not match any known sources from the configuration are processed here.\n\
Unmatched sources are dropped unless the wildcard source is explicitly defined.\n\
\n\
Any receivers defined on the command line will get all packets as they match\n\
as 0.0.0.0/0. The -X option changes this behavior and places all command line\n\
receivers in the unmatched category.\n\
", progname, FLOWPORT, (unsigned long) DEFAULT_SOCKBUFLEN, PORT_SEPARATOR, FREQ_SEPARATOR, TTL_SEPARATOR, FLOWPORT, DEFAULT_TTL);
}