-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgrepcidr.c
executable file
·1513 lines (1378 loc) · 37.1 KB
/
grepcidr.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
/*
grepcidr 2.99 - Filter IP addresses matching IPv4 and IPv6 CIDR specification
Parts copyright (C) 2004, 2005 Jem E. Berkes <[email protected]>
www.sysdesign.ca
Somewhat rewritten by John Levine <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
$Header: /Users/johnl/grepcidr-2.991/RCS/grepcidr.c,v 1.5 2015/10/25 15:11:06 johnl Exp $
*/
#define _WITH_GETLINE /* hint for FreeBSD */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <assert.h>
#define EXIT_OK 0
#define EXIT_NOMATCH 1
#define EXIT_ERROR 2
#define TXT_VERSION "grepcidr 2.991\nParts copyright (C) 2004, 2005 Jem E. Berkes <[email protected]>\n"
#define TXT_USAGE "Usage:\n" \
"\tgrepcidr [-V] [-cCDvhais] PATTERN [FILE...]\n" \
"\tgrepcidr [-V] [-cCDvhais] [-e PATTERN | -f FILE] [FILE...]\n"
#define MAXFIELD 512
#define TOKEN_SEPS "\t,\r\n" /* so user can specify multiple patterns on command line */
#define INIT_NETWORKS 8192
/*
Specifies a network. Whether originally in CIDR format (IP/mask)
or a range of IPs (IP_start-IP_end), spec is converted to a range.
The range is min to max (32-bit or 128 bit IPs) inclusive.
*/
struct netspec
{
unsigned int min;
unsigned int max;
};
typedef struct v6addr { unsigned char a[16]; } v6addr;
/* redefine this if your memcmp is slow, but it probably isn't */
#define v6cmp(a1, a2) memcmp((a1).a,(a2).a,16)
struct netspec6
{
v6addr min;
v6addr max;
};
/* Global variables */
static unsigned int npatterns = 0; /* total patterns in array */
static unsigned int n6patterns = 0; /* total patterns in v6 array */
static unsigned int capacity = 0; /* current capacity of array */
static unsigned int capacity6 = 0; /* current capacity of v6 array */
static struct netspec* array = NULL; /* array of patterns, network specs */
static struct netspec6* array6 = NULL; /* array of patterns, v6 network specs */
static unsigned int counting = 0; /* when non-zero, counts matches */
static int invert = 0; /* flag for inverted mode */
static int anchor = 0; /* anchor matches at beginning of line */
static int nonames = 0; /* don't show filenames */
static int nmatch = 0; /* count of matches for exit code */
static int igbadpat = 0; /* ignore bad patterns */
static int sloppy = 0; /* don't complain about sloppy CIDR */
static int cidrsearch = 0; /* parse and match CIDR in haystack */
static int didrsearch = 0; /* match CIDR if overlaps with haystack */
static int quick = 0; /* quick match, ignore v4 with dots before or after */
static void scan_block(char *bp, size_t blen, const char *fn);
static void scan_read(FILE *f, const char *fn);
static int applymask6(const v6addr ahi, int size, struct netspec6 *spec);
/* for getline */
char *linep = NULL;
size_t linesize;
/*
Insert new spec inside array of network spec
Dynamically grow array buffer as needed
*/
void array_insert(struct netspec* newspec)
{
/* Initial array allocation */
if(!array) {
capacity = INIT_NETWORKS;
array = (struct netspec*) malloc(capacity*sizeof(struct netspec));
if(!array) {
perror("Out of memory");
exit(EXIT_ERROR);
}
}
if (npatterns == capacity)
{
capacity *= 2;
array = (struct netspec *)realloc(array, capacity*sizeof(struct netspec));
if(!array) {
perror("Out of memory");
exit(EXIT_ERROR);
}
}
array[npatterns++] = *newspec;
}
void array_insert6(struct netspec6* newspec)
{
/* Initial array allocation */
if(!array6) {
capacity6 = INIT_NETWORKS;
array6 = (struct netspec6*) malloc(capacity6*sizeof(struct netspec6));
if(!array6) {
perror("Out of memory");
exit(EXIT_ERROR);
}
}
if (n6patterns == capacity6)
{
capacity6 *= 2;
array6 = (struct netspec6 *)realloc(array6, capacity6*sizeof(struct netspec6));
if(!array6) {
perror("Out of memory");
exit(EXIT_ERROR);
}
}
array6[n6patterns++] = *newspec;
}
/*
Given string, fills in the struct netspec (must be allocated)
Accept CIDR IP/mask format or IP_start-IP_end range.
Returns true (nonzero) on success, false (zero) on failure.
*/
int net_parse(const char* line, struct netspec* spec)
{
unsigned int minip = 0, maxip = 0;
unsigned int octet = 0;
unsigned int size = 0; /* if using CIDR IP/mask format */
unsigned int mask;
char *p;
enum iscan {
I_BEG = 0, /* beginning of line */
I_IP1, /* first octet*/
I_IP1D, /* dot after first octet */
I_IP2, /* second octet */
I_IP2D, /* dot after second octet */
I_IP3, /* third octet */
I_IP3D, /* dot after third octet */
I_IP4, /* fourth octet */
I_MIP1, /* first octet of max IP */
I_MIP1D, /* dot after first octet */
I_MIP2, /* second octet */
I_MIP2D, /* dot after second octet */
I_MIP3, /* third octet */
I_MIP3D, /* dot after third octet */
I_MIP4, /* fourth octet */
I_PIP, /* post first IP */
I_MASK, /* scanning a mask */
I_PD /* post dash */
} state;
state = I_BEG;
for(p = (char *)line;;) {
int ch = *p++;
switch(state) {
case I_BEG:
if(isspace(ch))
continue;
if(isdigit(ch)) { /* start a potential IP */
octet = ch-'0';
state = I_IP1;
continue;
}
break;
case I_IP1: /* in an IP address */
case I_IP2:
case I_IP3:
case I_MIP1: /* in a second IP address */
case I_MIP2:
case I_MIP3:
if(isdigit(ch)) {
octet = octet*10 + ch-'0';
continue;
}
if(ch == '.') {
if(octet > 255) { /* not a real address */
return 0;
}
maxip <<= 8;
maxip += octet;
state++; /* corresponding dot state */
continue;
}
/* otherwise, wasn't a full IP */
return 0;
case I_IP1D: /* saw dot after an octet */
case I_IP2D:
case I_IP3D:
case I_MIP1D: /* saw dot after an octet */
case I_MIP2D:
case I_MIP3D:
if(isdigit(ch)) {
octet = ch-'0';
state++; /* next octet state */
continue;
}
return 0; /* wasn't an IP */
case I_IP4: /* in last octet */
if(isdigit(ch)) {
octet = octet*10 + ch-'0';
continue;
}
/* OK, we have the IP */
if(octet > 255) { /* not a real address */
return 0;
}
maxip <<= 8;
maxip += octet;
minip = maxip; /* until we see otherwise */
if(!ch) break; /* end of string */
if(ch == '/')
state = I_MASK;
else if(ch == '-')
state = I_PD;
else
state = I_PIP;
continue;
case I_MIP4: /* in last octet of range max*/
if(isdigit(ch)) {
octet = octet*10 + ch-'0';
continue;
}
/* OK, we have the IP */
if(octet > 255) { /* not a real address */
return 0;
}
maxip <<= 8;
maxip += octet;
if(ch && !isspace(ch))
return 0; /* junk at end */
break;
case I_PIP:
if(ch == '/')
state = I_MASK;
else if(ch == '-')
state = I_PD;
else if(!ch)
break; /* single IP with spaces after it */
else if(!isspace(ch))
return 0; /* junk */
continue;
case I_PD:
if(isspace(ch))
continue;
if(!isdigit(ch))
return 0; /* junk */
octet = ch-'0';
state = I_MIP1;
continue;
case I_MASK: /* CIDR mask size */
if(isdigit(ch)) {
size = size*10 + ch-'0';
continue;
}
if(ch && !isspace(ch))
return 0; /* junk at end */
if(size > 32)
return 0; /* not a reasonable cidr */
mask = (1L<<(32-size))-1;
if(maxip&mask && !sloppy)
fprintf(stderr, "Invalid cidr: %s\n", line);
minip &= ~mask; /* force to CIDR boundary */
maxip |= mask;
break;
}
if(ch && !isspace(ch)) return 0; /* crud at end of address */
break;
}
/* got something, return it */
spec->min = minip;
spec->max = maxip;
#if DEBUG
if(getenv("RANGES"))printf("range %08x - %08x\n", minip, maxip);
#endif /* DEBUG */
if(minip > maxip)
fprintf(stderr, "Backward range: %s\n", line);
return 1;
}
/*
* parse IPv6 address or CIDR
* no ranges, since they don't seem popular
* This should handle the full syntax in RFC 4291 sec 2.2 and 2.3
*/
/* turn a hex digit to a value, has to be a hex digit */
#define xtod(c) ((c<='9')?(c-'0'):((c&15)+9))
int net_parse6(const char* line, struct netspec6* spec)
{
v6addr ahi; /* high part of address */
v6addr alo; /* low part of address */
int nhi = 0; /* how many bytes in ahi */
int nlo = 0; /* how many bytes in alo */
int octet = -1; /* current v4 octet, -1 means not an octet */
unsigned int chunk = 0; /* current 16 bit chunk */
int size = -1;
enum sv6 {
V_BEG = 0, /* beginning of string */
V_HCH, /* in a hi chunk */
V_HC1, /* hi, seen one colon */
V_HC2, /* hi, seen two colons */
V_LCH, /* in a low chunk */
V_LC1, /* seen a low colon */
V_IC1, /* seen initial colon */
V_EIP1D, /* dot after first octet of embedded IPv4 */
V_EIP2, /* second octet */
V_EIP2D, /* dot after second octet */
V_EIP3, /* third octet */
V_EIP3D, /* dot after third octet */
V_EIP4, /* fourth octet */
V_SIZE /* CIDR size */
} state;
char *p = (char *)line;
state = 0;
for(;;) {
int ch = *p++;
switch(state) {
case V_BEG:
if(isspace(ch)) continue;
if(isxdigit(ch)) { /* first chunk can't be v4 */
chunk = xtod(ch);
state = V_HCH;
continue;
}
if(ch == ':') {
state = V_IC1;
continue;
}
return 0; /* not an IP */
case V_IC1: /* leading colon must be two colons */
if(ch == ':') {
state = V_HC2;
continue;
}
return 0; /* not an IP */
case V_HCH:
if(isxdigit(ch)) {
chunk = (chunk<<4)+xtod(ch);
if(isdigit(ch)) {
if(octet >= 0) octet = octet*10 + ch-'1';
} else
octet = -1; /* not v4 */
continue;
}
/* finish the current chunk */
if(ch == '.') {
if(nhi == 12 && octet >= 0 && octet <= 255) { /* embedded v4 */
ahi.a[nhi++] = octet;
state = V_EIP1D;
continue;
}
return 0; /* not an IP */
}
if(nhi > 14) return 0; /* too many chunks */
ahi.a[nhi++] = chunk >> 8; /* big-endian for memcmp() */
ahi.a[nhi++] = chunk & 255;
if(ch == ':') {
state = V_HC1;
continue;
}
if(ch == '/') {
state = V_SIZE;
continue;
}
break; /* end of the number */
case V_HC1:
if(isxdigit(ch)) {
chunk = xtod(ch);
if(isdigit(ch))
octet = chunk;
else
octet = -1;
state = V_HCH;
continue;
}
if(ch == ':') {
state = V_HC2;
continue;
}
return 0; /* not an IP */
case V_HC2:
if(isxdigit(ch)) { /* two colons and digit, start low half */
chunk = xtod(ch);
if(isdigit(ch))
octet = chunk;
else
octet = -1;
state = V_LCH;
continue;
}
if(ch == '/') {
state = V_SIZE;
continue;
}
break; /* end of only high half */
case V_LCH:
if(isxdigit(ch)) {
chunk = (chunk<<4)+xtod(ch);
if(isdigit(ch)) {
if(octet >= 0) octet = octet*10 + ch-'0';
} else
octet = -1; /* not v4 */
continue;
}
/* finish the current chunk */
if(ch == '.') {
if((nhi+nlo) < 12
&& octet >= 0 && octet <= 255) { /* embedded v4 */
/* move all into ahi */
memset(ahi.a+nhi, 0, 12-(nhi+nlo));
if(nlo) {
memcpy(ahi.a+12-nlo, alo.a, nlo);
nlo = 0;
}
nhi = 12;
ahi.a[nhi++] = octet;
state = V_EIP1D;
continue;
}
return 0; /* not an embedded v4 */
}
if((nhi+nlo) > 12) return 0; /* too many chunks */
if(chunk > 0xffff) return 0; /* too big for a chunk */
alo.a[nlo++] = chunk >> 8; /* big-endian for memcmp() */
alo.a[nlo++] = chunk & 255;
if(ch == ':') {
state = V_LC1;
continue;
}
if(ch == '/') {
state = V_SIZE;
continue;
}
break; /* end of the number */
case V_LC1:
if(isxdigit(ch)) {
chunk = xtod(ch);
if(isdigit(ch))
octet = chunk;
else
octet = -1;
state = V_LCH;
continue;
}
return 0; /* trailing junk, not an IP */
case V_EIP1D: /* dot after first octet of embedded IPv4 */
case V_EIP2D: /* dot after second octet */
case V_EIP3D: /* dot after third octet */
if(isdigit(ch)) {
octet = ch-'0';
state++;
continue;
}
return 0; /* not an IP */
case V_EIP2: /* second octet */
case V_EIP3: /* third octet */
if(isdigit(ch)) {
octet = octet*10 + ch-'0';
continue;
}
if(ch == '.') {
if(octet > 255) return 0; /* not an IP */
ahi.a[nhi++] = octet;
state++;
continue;
}
return 0; /* not an IP */
case V_EIP4: /* fourth octet */
if(isdigit(ch)) {
octet = octet*10 + ch-'0';
continue;
}
if(octet > 255) break; /* not an IP */
ahi.a[nhi++] = octet;
if(ch == '/') {
state = V_SIZE;
continue;
}
break; /* four octets, we're done */
case V_SIZE:
if(isdigit(ch)) {
if (size < 0) size = 0;
size = size*10 + ch-'0';
continue;
}
if(size < 0 || size > 128) return 0; /* no digits or junk at the end */
break;
}
break;
/* accept if \0 or space after an item */
if(ch && !isspace(ch)) return 0; /* crud in the item */
}
/* combine ahi and alo */
if(nlo && (nhi+nlo) >= 16) return 0; /* too many chunks */
if((nhi+nlo) < 16)
memset(ahi.a+nhi, 0, 16-(nhi+nlo));
if(nlo)memcpy(ahi.a+16-nlo, alo.a, nlo);
if (!applymask6(ahi, size, spec) && !sloppy) {
p = strchr(line, '\n');
if(p) *p = 0; /* just a string */
fprintf(stderr, "Bad cidr range: %s\n", line);
}
return 1;
}
/* Return 0 (softfail) if bits were set in host part of CIDR address */
static int applymask6(const v6addr ahi, int size, struct netspec6 *spec)
{
int badbits = 0; /* bits already set, bad CIDR */
assert(size >= 0 && size <= 128);
spec->min = spec->max = ahi;
if(size >= 0) { /* set low bits for the range */
/* and also check that they were already zero */
int nbits = size&7; /* bits within a byte */
int nbytes = size >> 3;
if(nbits) {
int mask = 255>>nbits;
if(ahi.a[nbytes]&mask) badbits = 1;
spec->min.a[nbytes] &= 255-mask;
spec->max.a[nbytes] |= mask;
nbytes++;
}
while(nbytes < 16) {
if(ahi.a[nbytes]) badbits = 1;
spec->min.a[nbytes] = 0;
spec->max.a[nbytes] = 255;
nbytes++;
}
}
return !badbits;
}
/* Compare two netspecs, for sorting. Comparison is done on minimum of range */
int netsort(const void* a, const void* b)
{
unsigned int c1 = ((struct netspec*)a)->min;
unsigned int c2 = ((struct netspec*)b)->min;
if (c1 < c2) return -1;
if (c1 > c2) return +1;
c1 = ((struct netspec*)a)->max;
c2 = ((struct netspec*)b)->max;
if (c1 < c2) return -1;
if (c1 > c2) return +1;
return 0;
}
int netsort6(const void* a, const void* b)
{
int r;
v6addr *c1 = &((struct netspec6*)a)->min;
v6addr *c2 = &((struct netspec6*)b)->min;
r = v6cmp(*c1, *c2);
if(r != 0) return r;
c1 = &((struct netspec6*)a)->max;
c2 = &((struct netspec6*)b)->max;
return v6cmp(*c1, *c2);
}
int main(int argc, char* argv[])
{
static char shortopts[] = "acCDe:f:iqsvV";
char* pat_filename = NULL; /* filename containing patterns */
char* pat_strings = NULL; /* pattern strings on command line */
int foundopt;
if (argc == 1)
{
fprintf(stderr, TXT_USAGE);
return EXIT_ERROR;
}
while ((foundopt = getopt(argc, argv, shortopts)) != -1)
{
switch (foundopt)
{
case 'V':
puts(TXT_VERSION);
return EXIT_ERROR;
case 'c':
counting = 1;
break;
case 'v':
invert = 1;
break;
case 'h':
nonames = 1;
break;
case 'a':
anchor = 1;
break;
case 'i':
igbadpat = 1;
break;
case 'q':
quick = 1;
break;
case 's':
sloppy = 1;
break;
case 'D':
didrsearch = 1;
/* fall through */
case 'C':
cidrsearch = 1;
break;
case 'e':
pat_strings = optarg;
break;
case 'f':
pat_filename = optarg;
break;
default:
fprintf(stderr, TXT_USAGE);
return EXIT_ERROR;
}
}
if (!pat_filename && !pat_strings)
{
if (optind < argc)
pat_strings = argv[optind++];
else
{
fprintf(stderr, "Specify PATTERN or -f FILE to read patterns from\n");
return EXIT_ERROR;
}
}
/* Load patterns defining networks */
if (pat_filename)
{
FILE* data = fopen(pat_filename, "r");
if (data)
{
while (getline(&linep, &linesize, data) > 0)
{
if (*linep != '#') {
if(strchr(linep, ':')) {
struct netspec6 spec6;
if(net_parse6(linep, &spec6))
array_insert6(&spec6);
else if(!igbadpat)
fprintf(stderr, "Not a pattern: %s", linep);
} else {
struct netspec spec;
if (net_parse(linep, &spec))
array_insert(&spec);
else if(!igbadpat)
fprintf(stderr, "Not a pattern: %s", linep);
}
}
}
fclose(data);
}
else
{
perror(pat_filename);
return EXIT_ERROR;
}
}
if (pat_strings)
{
char* token = strtok(pat_strings, TOKEN_SEPS);
while (token)
{
if(strchr(token, ':')) {
struct netspec6 spec6;
if(net_parse6(token, &spec6))
array_insert6(&spec6);
else if(!igbadpat)
fprintf(stderr, "Not a pattern: %s\n", token);
} else {
struct netspec spec;
if (net_parse(token, &spec))
array_insert(&spec);
else if(!igbadpat)
fprintf(stderr, "Not a pattern: %s\n", token);
}
token = strtok(NULL, TOKEN_SEPS);
}
}
if(!npatterns && !n6patterns) {
fprintf(stderr, "No patterns to match\n");
return EXIT_ERROR;
}
/* Prepare array for rapid searching */
if(npatterns) {
struct netspec *inp, *outp;
#if DEBUG
char *dnp;
if((dnp = getenv("PRESORT4")) != 0) {
FILE *f = fopen(dnp, "w");
struct netspec *p;
for(p = array; p < array+npatterns; p++)
fprintf(f, "%d.%d.%d.%d-%d.%d.%d.%d\n", p->min>>24,
(p->min>>16)&255, (p->min>>8)&255, p->min&255,
p->max>>24, (p->max>>16)&255, (p->max>>8)&255, p->max&255);
fclose(f);
}
#endif /* DEBUG */
qsort(array, npatterns, sizeof(struct netspec), netsort);
#if DEBUG
if((dnp = getenv("POSTSORT4")) != 0) {
FILE *f = fopen(dnp, "w");
struct netspec *p;
for(p = array; p < array+npatterns; p++)
fprintf(f, "%d.%d.%d.%d-%d.%d.%d.%d\n", p->min>>24,
(p->min>>16)&255, (p->min>>8)&255, p->min&255,
p->max>>24, (p->max>>16)&255, (p->max>>8)&255, p->max&255);
fclose(f);
}
#endif /* DEBUG */
/* combine overlapping ranges
* outp is clean so far, inp is checked for overlap
*/
outp = array;
for (inp = array+1; inp < array+npatterns; inp++)
{
if (inp->max <= outp->max)
continue; /* contained within previous range, ignore */
if(inp->min <= outp->max) { /* overlapping ranges, combine */
outp->max = inp->max;
continue;
}
if(++outp < inp)
*outp = *inp; /* move down due to previously combined or ignored */
}
npatterns = outp-array+1; /* adjusted count after combinations */
#if DEBUG
if((dnp = getenv("POSTMERGE4")) != 0) {
FILE *f = fopen(dnp, "w");
struct netspec *p;
for(p = array; p < array+npatterns; p++)
fprintf(f, "%d.%d.%d.%d-%d.%d.%d.%d\n", p->min>>24,
(p->min>>16)&255, (p->min>>8)&255, p->min&255,
p->max>>24, (p->max>>16)&255, (p->max>>8)&255, p->max&255);
fclose(f);
}
#endif /* DEBUG */
}
if(n6patterns) {
struct netspec6 *inp, *outp;
qsort(array6, n6patterns, sizeof(struct netspec6), netsort6);
/* combine overlapping ranges
* outp is clean so far, inp is checked for overlap
*/
outp = array6;
for (inp = array6+1; inp < array6+n6patterns; inp++)
{
if (v6cmp(inp->max, outp->max) <= 0)
continue; /* contained within previous range, ignore */
if(v6cmp(inp->min, outp->max)<=0) { /* overlapping ranges, combine */
outp->max = inp->max;
continue;
}
if(++outp < inp)
*outp = *inp; /* move down due to previously combined or ignored */
}
n6patterns = outp-array6+1; /* adjusted count after combinations */
}
# if DEBUG
{ /* DEBUG */
int i,n;
for(n = 0; n < n6patterns; n++) {
printf("min %d:", n);
for(i = 0; i<16; i++) printf(" %02x", array6[n].min.a[i]);
printf("\nmax %d:",n);
for(i = 0; i<16; i++) printf(" %02x", array6[n].max.a[i]);
printf("\n");
}
}
# endif /* DEBUG */
if (optind >= argc) {
scan_read(stdin, NULL);
} else {
if(optind+1 >= argc) nonames = 1; /* just one file, no name */
while(optind < argc) {
char *fn = argv[optind++];
FILE *f = fopen(fn, "r");
char *fmap;
size_t flen;
struct stat statbuf;
if(!f) {
perror(fn);
return EXIT_ERROR;
}
if(fstat(fileno(f), &statbuf) != 0 || (statbuf.st_mode&S_IFMT)!= S_IFREG ) {
scan_read(f, fn); /* can't stat or not a normal file, fall back to read */
fclose(f);
continue;
}
flen = statbuf.st_size;
if(flen == 0) {
fclose(f); /* empty file, forget it */
continue;
}
fmap = mmap(NULL, flen, PROT_READ, MAP_SHARED, fileno(f), (off_t)0);
if(fmap == MAP_FAILED) {
perror("map failed");
scan_read(f, fn); /* can't map, fall back to read */
fclose(f);
continue;
}
scan_block(fmap, flen, fn);
munmap(fmap, flen);
fclose(f);
}
}
/* Cleanup */
if (counting)
printf("%u\n", nmatch);
if (nmatch)
return EXIT_OK;
else
return EXIT_NOMATCH;
}
/* scan a line at a time */
static void scan_read(FILE *f, const char *fn)
{
ssize_t len;
while((len = getline(&linep, &linesize, f)) > 0)
scan_block(linep, len, fn);
}
static int netmatch(const struct netspec ip4);
static int netmatch6(const struct netspec6 ip6);
/* scan some text, must be whole lines
* generally either one line or the whole file
* bp: pointer to buffer
* blen: length of buffer
* fn: filename for printing
* This should handle the full V6 syntax in RFC 4291 sec 2.2 and 2.3 except for
* :: for a zero address
* strings of colons may confuse it
*/
static void scan_block(char *bp, size_t blen, const char *fn)
{
enum sscan {
S_BEG = 0, /* beginning of line */
S_SC, /* scan for IP */
S_NSC, /* saw a dot, scan for non-digit */
S_IP1, /* first octet or maybe first v6 chunk*/
S_IP1D, /* dot after first octet */
S_IP2, /* second octet */
S_IP2D, /* dot after second octet */
S_IP3, /* third octet */
S_IP3D, /* dot after third octet */
S_IP4, /* fourth octet */
S_V4SZ, /* v4 cidr prefix */
S_HCH, /* in a hi v6 chunk */
S_HC1, /* hi, seen one colon */
S_HC2, /* hi, seen two colons */
S_LCH, /* in a low chunk */
S_LC1, /* seen a low colon */
S_IC1, /* seen initial colon */
S_EIP1D, /* dot after first octet in embedded v4 */
S_EIP2, /* second octet */
S_EIP2D, /* dot after second octet */
S_EIP3, /* third octet */
S_EIP3D, /* dot after third octet */
S_EIP4, /* fourth octet */
S_V6SZ, /* v6 cidr prefix */
S_SCNL, /* scan for new line */
S_SCNLP /* scan for new line and print line */
} state;
enum sscan snext = anchor?S_SCNL:S_SC; /* state after not an IP */
char *p = bp; /* current character */
char *plim = bp+blen; /* end of buffer */
char *lp = bp; /* beginning of current line */
unsigned int ip4 = 0; /* IPv4 value */
int octet = 0; /* current octet */
int size = -1; /* CIDR size */
v6addr ahi; /* high part of address */
v6addr alo; /* low part of address */
struct netspec range4; /* IPv4 address or range */
struct netspec6 range6; /* IPv6 address or range */
int nhi = 0; /* how many bytes in ahi */
int nlo = 0; /* how many bytes in alo */
unsigned int chunk = 0; /* current 16 bit chunk */
int seenone = 0; /* seen an address on this line, for -v */
state = S_BEG;
for(p = bp; p < plim;) {
int ch = *p++;
switch(state) {
case S_BEG: /* beginning of line */
lp = p-1;
seenone = 0;
/* skip leading spaces */
while(p < plim && (ch == ' ' || ch == '\t'))
ch = *p++;
/* fall through */
case S_SC: /* normal scanning */
if(isdigit(ch)) { /* start a potential IP of either type */
ip4 = 0;
state = S_IP1;
nhi = nlo = 0;
octet = chunk = ch-'0';
continue;
} else if(isxdigit(ch)) {
state = S_HCH;
nhi = nlo = 0;
octet = -1; /* hex, not v4 */
chunk = xtod(ch);