forked from robertdavidgraham/masscan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmassip-parse.c
1313 lines (1209 loc) · 45.9 KB
/
massip-parse.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
/*
massip-parse
This module parses IPv4 and IPv6 addresses.
It's not a typical parser. It's optimized around parsing large
files containing millions of addresses and ranges using a
"state-machine parser".
*/
#include "massip.h"
#include "massip-parse.h"
#include "massip-rangesv4.h"
#include "massip-rangesv6.h"
#include "logger.h"
#include "util-bool.h"
#include "util-malloc.h"
#include "string_s.h"
#include "unusedparm.h"
#include <string.h>
struct massip_parser
{
unsigned long long line_number;
unsigned long long char_number;
unsigned state;
unsigned tmp;
unsigned char digit_count;
unsigned addr;
unsigned begin;
unsigned end;
struct {
ipv6address _begin;
ipv6address _end;
unsigned short tmp[8];
unsigned char index;
unsigned char ellision_index;
unsigned is_bracket:1;
unsigned is_second:1;
} ipv6;
};
/***************************************************************************
***************************************************************************/
static struct massip_parser *
_parser_init(struct massip_parser *p)
{
memset(p, 0, sizeof(*p));
p->line_number = 1;
p->ipv6.ellision_index = 8;
return p;
}
/***************************************************************************
***************************************************************************/
static void
_parser_destroy(struct massip_parser *p)
{
UNUSEDPARM(p);
}
/***************************************************************************
***************************************************************************/
static void
_parser_err(struct massip_parser *p, unsigned long long *line_number, unsigned long long *charindex)
{
*line_number = p->line_number;
*charindex = p->char_number;
}
/**
* Called before parsing the first address in a pair, and also
* after the first address, to prepare for parsing the next
* address
*/
static void
_init_next_address(struct massip_parser *p, int is_second)
{
p->tmp = 0;
p->ipv6.ellision_index = 8;
p->ipv6.index = 0;
p->ipv6.is_bracket = 0;
p->digit_count = 0;
p->ipv6.is_second = is_second;
}
static unsigned
_parser_finish_ipv6(struct massip_parser *p)
{
unsigned index = p->ipv6.index;
unsigned ellision = p->ipv6.ellision_index;
/* We must have seen 8 numbers, or an ellision */
if (index < 8 && ellision >= 8)
return 1;
/* Handle ellision */
memmove(
&p->ipv6.tmp[8-(index-ellision)],
&p->ipv6.tmp[ellision],
sizeof(p->ipv6.tmp[0]) * (index-ellision)
);
memset(
&p->ipv6.tmp[ellision],
0,
sizeof(p->ipv6.tmp[0]) * (8 - index)
);
/* Copy over to begin/end. We parse the address as a series of 16-bit
* integers, but return the result as two 64-bit integers */
{
ipv6address a;
a.hi = (uint64_t)p->ipv6.tmp[0] << 48ULL
| (uint64_t)p->ipv6.tmp[1] << 32ULL
| (uint64_t)p->ipv6.tmp[2] << 16ULL
| (uint64_t)p->ipv6.tmp[3] << 0ULL;
a.lo = (uint64_t)p->ipv6.tmp[4] << 48ULL
| (uint64_t)p->ipv6.tmp[5] << 32ULL
| (uint64_t)p->ipv6.tmp[6] << 16ULL
| (uint64_t)p->ipv6.tmp[7] << 0ULL;
if (p->ipv6.is_second)
p->ipv6._end = a;
else {
p->ipv6._begin = a;
/* Set this here in case there is no 'end' address */
p->ipv6._end = a;
}
}
/* Reset the parser to start parsing the next address */
_init_next_address(p, 1);
return 0;
}
/***************************************************************************
* We store the IPv6 addresses that we are building inside the 'state'
* of the state-machine. This function copies them out of the opaque
* state into discrete values.
***************************************************************************/
static void
_parser_get_ipv6(struct massip_parser *state, ipv6address *begin, ipv6address *end)
{
*begin = state->ipv6._begin;
*end = state->ipv6._end;
}
enum parser_state_t {
LINE_START, ADDR_START,
COMMENT,
NUMBER0, NUMBER1, NUMBER2, NUMBER3, NUMBER_ERR,
SECOND0, SECOND1, SECOND2, SECOND3, SECOND_ERR,
IPV4_CIDR_NUM,
UNIDASH1, UNIDASH2,
IPV6_BEGIN, IPV6_COLON, IPV6_CIDR, IPV6_CIDR_NUM,
IPV6_NEXT,
IPV6_END,
ERROR
};
/***************************************************************************
* When we start parsing an address, we don't know whether it's going to
* be IPv4 or IPv6. We assume IPv4, but when we hit a condition indicating
* that it's IPv6 instead, we need change the temporary number we
* are working on from decimal to hex, then move from the middle of
* parsing an IPv4 address to the middle of parsing an IPv6 address.
***************************************************************************/
static int
_switch_to_ipv6(struct massip_parser *p, int old_state)
{
unsigned num = p->tmp;
num = ((num/1000)%10) * 16 * 16 * 16
+ ((num/100)%10) * 16 * 16
+ ((num/10)%10) * 16
+ (num % 10);
//printf("%u -> 0x%x\n", p->tmp, num);
p->tmp = num;
return old_state;
}
enum {
IPV4_n, IPV4_nn, IPV4_nnn, IPV4_nnn_,
IPV4_nnn_n, IPV4_nnn_nn, IPV4_nnn_nnn, IPV4_nnn_nnn_,
IPV4_nnn_nnn_n, IPV4_nnn_nnn_nn, IPV4_nnn_nnn_nnn, IPV4_nnn_nnn_nnn_,
IPV4_nnn_nnn_nnn_n, IPV4_nnn_nnn_nnn_nn, IPV4_nnn_nnn_nnn_nnn, IPV4_nnn_nnn_nnn_nnn_,
IPV4e_n, IPV4e_nn, IPV4e_nnn, IPV4e_nnn_,
IPV4e_nnn_n, IPV4e_nnn_nn, IPV4e_nnn_nnn, IPV4e_nnn_nnn_,
IPV4e_nnn_nnn_n, IPV4e_nnn_nnn_nn, IPV4e_nnn_nnn_nnn, IPV4e_nnn_nnn_nnn_,
IPV4e_nnn_nnn_nnn_n, IPV4e_nnn_nnn_nnn_nn, IPV4e_nnn_nnn_nnn_nnn, IPV4e_nnn_nnn_nnn_nnn_,
};
/**
* Applies a CIDR mask to an IPv4 address to create a begin/end address.
*/
static void
_ipv4_apply_cidr(unsigned *begin, unsigned *end, unsigned bitcount)
{
unsigned long long mask = 0xFFFFFFFF00000000ULL >> bitcount;
/* mask off low-order bits */
*begin &= (unsigned)mask;
/* Set all suffix bits to 1, so that 192.168.1.0/24 has
* an ending address of 192.168.1.255. */
*end = *begin | (unsigned)~mask;
}
/**
* Given an address 'being' and a 'prefix', return the 'begin' and 'end' address of the range.
* @param begin
* An in/out parameter. This may have some extra bits somewhere in the range.
* These will be masked off and set to zero when the function returns.
* @param end
* An out parameter. This will be set to the last address of the range, meaning
* that all the trailing bits will be set to '1'.
* @parame prefix
* The number of bits of the prefix, from [0..128]. If the value is 0,
* then the 'begin' address will be set to all zeroes and the 'end'
* address will be set to all ones. If the value is 128,
* the 'begin' address is unchanged and the 'end' address
* is set to the same as 'begin'.
*/
static void
_ipv6_apply_cidr(ipv6address *begin, ipv6address *end, unsigned prefix)
{
ipv6address mask;
/* For bad prefixes, make sure we return an invalid address */
if (prefix > 128) {
static const ipv6address invalid = {~0ULL, ~0ULL};
*begin = invalid;
*end = invalid;
return;
};
/* Create the mask from the prefix */
if (prefix > 64)
mask.hi = ~0ULL;
else if (prefix == 0)
mask.hi = 0;
else
mask.hi = ~0ULL << (64 - prefix);
if (prefix > 64)
mask.lo = ~0ULL << (128 - prefix);
else
mask.lo = 0;
/* Mask off any non-zero bits from the start
* TODO print warning */
begin->hi &= mask.hi;
begin->lo &= mask.lo;
/* Set all suffix bits to 1, so that 192.168.1.0/24 has
* an ending address of 192.168.1.255. */
end->hi = begin->hi | ~mask.hi;
end->lo = begin->lo | ~mask.lo;
}
/***************************************************************************
* Parse the next IPv4/IPv6 address from a text stream, using a
* 'state-machine parser'.
***************************************************************************/
static enum {Still_Working, Found_Error, Found_IPv4, Found_IPv6}
_parser_next(struct massip_parser *p, const char *buf, size_t *r_offset, size_t length,
unsigned *r_begin, unsigned *r_end)
{
size_t i;
enum parser_state_t state = p->state;
int result = Still_Working;
/* The 'offset' parameter is optional. If NULL, then set it to zero */
if (r_offset)
i = *r_offset;
else
i = 0;
/* For all bytes in this chunk. This loop will exit early once
* we've found a complete IP address. */
while (i < length) {
unsigned char c = buf[i++];
p->char_number++;
switch (state) {
case LINE_START:
case ADDR_START:
_init_next_address(p, 0);
switch (c) {
case ' ': case '\t': case '\r':
/* ignore leading whitespace */
continue;
case '\n':
p->line_number++;
p->char_number = 0;
continue;
case '#': case ';': case '/': case '-':
state = COMMENT;
continue;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
p->tmp = (c - '0');
p->digit_count = 1;
state = NUMBER0;
break;
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
p->tmp = (c - 'a' + 10);
p->digit_count = 1;
state = IPV6_BEGIN;
break;
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
p->tmp = (c - 'A' + 10);
p->digit_count = 1;
state = IPV6_BEGIN;
break;
case ':':
p->ipv6.tmp[p->ipv6.index++] = 0;
state = IPV6_COLON;
break;
case '[':
p->ipv6.is_bracket = 1;
state = IPV6_BEGIN;
break;
default:
state = ERROR;
length = i; /* break out of loop */
break;
}
break;
case IPV6_CIDR:
p->digit_count = 0;
p->tmp = 0;
switch (c) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
p->tmp = (c - '0');
p->digit_count = 1;
state = IPV6_CIDR_NUM;
break;
default:
state = ERROR;
length = i; /* break out of loop */
break;
}
break;
case IPV6_COLON:
p->digit_count = 0;
p->tmp = 0;
if (c == ':') {
if (p->ipv6.ellision_index < 8) {
state = ERROR;
length = i;
} else {
p->ipv6.ellision_index = p->ipv6.index;
state = IPV6_COLON;
}
break;
}
state = IPV6_BEGIN;
/* drop down */
case IPV6_BEGIN:
case IPV6_NEXT:
switch (c) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
if (p->digit_count >= 4) {
state = ERROR;
length = i;
} else {
p->tmp = p->tmp * 16 + (c - '0');
p->digit_count++;
}
break;
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
if (p->digit_count >= 4) {
state = ERROR;
length = i;
} else {
p->tmp = p->tmp * 16 + (c - 'a' + 10);
p->digit_count++;
}
break;
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
if (p->digit_count >= 4) {
state = ERROR;
length = i;
} else {
p->tmp = p->tmp * 16 + (c - 'A' + 10);
p->digit_count++;
}
break;
case ':':
if (p->ipv6.index >= 8) {
state = ERROR;
length = i;
} else {
p->ipv6.tmp[p->ipv6.index++] = (unsigned short)p->tmp;
state = IPV6_COLON;
}
break;
case ']':
if (!p->ipv6.is_bracket) {
state = ERROR;
length = i;
} else {
state = IPV6_END;
}
break;
case '[':
if (p->ipv6.is_bracket) {
state = ERROR;
length = i;
} else {
p->ipv6.is_bracket = 1;
}
break;
case '/':
case ' ':
case '\t':
case '\r':
case '\n':
case ',':
case '-':
i--; /* push back */
state = IPV6_END;
continue;
default:
state = ERROR;
length = i;
break;
}
break;
case IPV6_END:
/* Finish off the trailing number */
p->ipv6.tmp[p->ipv6.index++] = (unsigned short)p->tmp;
/* Do the final processing of this IPv6 address and
* prepare for the next one */
if (_parser_finish_ipv6(p) != 0) {
state = ERROR;
length = i;
continue;
}
/* Now decide the next state, whether this is a single
* address, an address range, or a CIDR address */
switch (c) {
case '/':
result = Still_Working;
state = IPV6_CIDR;
break;
case '-':
result = Still_Working;
state = IPV6_NEXT;
break;
case '\n':
p->line_number++;
p->char_number = 0;
/* drop down */
case ' ':
case '\t':
case '\r':
case ',':
result = Found_IPv6;
state = 0;
length = i; /* shorten the end to break out of loop */
break;
default:
state = ERROR;
length = i;
break;
}
break;
case COMMENT:
if (c == '\n') {
state = LINE_START;
p->line_number++;
p->char_number = 0;
} else
state = COMMENT;
break;
case IPV6_CIDR_NUM:
switch (c) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
if (p->digit_count == 4) {
state = ERROR;
length = i; /* break out of loop */
} else {
p->digit_count++;
p->tmp = p->tmp * 10 + (c - '0');
if (p->tmp > 128) {
state = ERROR;
length = i;
}
continue;
}
break;
case ':':
case ',':
case ' ':
case '\t':
case '\r':
case '\n':
{
_ipv6_apply_cidr(&p->ipv6._begin, &p->ipv6._end, p->tmp);
state = ADDR_START;
length = i; /* break out of loop */
if (c == '\n') {
p->line_number++;
p->char_number = 0;
}
*r_begin = p->begin;
*r_end = p->end;
result = Found_IPv6;
}
break;
default:
state = ERROR;
length = i; /* break out of loop */
break;
}
break;
case IPV4_CIDR_NUM:
switch (c) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
if (p->digit_count == 3) {
state = ERROR;
length = i; /* break out of loop */
} else {
p->digit_count++;
p->tmp = p->tmp * 10 + (c - '0');
if (p->tmp > 32) {
state = ERROR;
length = i;
}
continue;
}
break;
case ':':
case ',':
case ' ':
case '\t':
case '\r':
case '\n':
{
_ipv4_apply_cidr(&p->begin, &p->end, p->tmp);
state = ADDR_START;
length = i; /* break out of loop */
if (c == '\n') {
p->line_number++;
p->char_number = 0;
}
*r_begin = p->begin;
*r_end = p->end;
result = Found_IPv4;
}
break;
default:
state = ERROR;
length = i; /* break out of loop */
break;
}
break;
case UNIDASH1:
if (c == 0x80)
state = UNIDASH2;
else {
state = ERROR;
length = i; /* break out of loop */
}
break;
case UNIDASH2:
/* This covers:
* U+2010 HYPHEN
* U+2011 NON-BREAKING HYPHEN
* U+2012 FIGURE DASH
* U+2013 EN DASH
* U+2014 EM DASH
* U+2015 HORIZONTAL BAR
*/
if (c < 0x90 || 0x95 < c) {
state = ERROR;
length = i; /* break out of loop */
} else {
c = '-';
state = NUMBER3;
/* drop down */
}
case NUMBER0:
case NUMBER1:
case NUMBER2:
case NUMBER3:
case SECOND0:
case SECOND1:
case SECOND2:
case SECOND3:
switch (c) {
case '.':
p->addr = (p->addr << 8) | p->tmp;
p->tmp = 0;
p->digit_count = 0;
if (state == NUMBER3 || state == SECOND3) {
length = i;
state = ERROR;
} else
state++;
break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
p->digit_count++;
p->tmp = p->tmp * 10 + (c - '0');
if (p->tmp > 255 || p->digit_count > 3) {
if (state == NUMBER0) {
/* Assume that we've actually got an
* IPv6 number */
_switch_to_ipv6(p, state);
state = IPV6_BEGIN;
} else {
state = ERROR;
length = i;
}
}
continue;
break;
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
if (state == NUMBER0 || state == SECOND0) {
/* Assume that we've actually got an
* IPv6 number */
_switch_to_ipv6(p, state);
state = IPV6_BEGIN;
i--; /* go back one character */
} else {
state = ERROR;
length = i; /* break out of loop */
}
break;
case 0xe2:
if (state == NUMBER3) {
state = UNIDASH1;
} else {
state = ERROR;
length = i; /* break out of loop */
}
break;
case '-':
case 0x96: /* long dash, comes from copy/pasting into exclude files */
if (state == NUMBER3) {
p->begin = (p->addr << 8) | p->tmp;
p->tmp = 0;
p->digit_count = 0;
p->addr = 0;
state = SECOND0;
} else {
state = NUMBER_ERR;
length = i;
}
break;
case '/':
if (state == NUMBER3) {
p->begin = (p->addr << 8) | p->tmp;
p->tmp = 0;
p->digit_count = 0;
p->addr = 0;
state = IPV4_CIDR_NUM;
} else {
state = NUMBER_ERR;
length = i; /* break out of loop */
}
break;
case ':':
if (state == NUMBER0) {
/* Assume this is an IPv6 address instead of an IPv4 address */
_switch_to_ipv6(p, state);
state = IPV6_BEGIN;
i--;
break;
}
case ',':
case ' ':
case '\t':
case '\r':
case '\n':
if (state == NUMBER3) {
p->begin = (p->addr << 8) | p->tmp;
p->end = p->begin;
p->tmp = 0;
p->digit_count = 0;
p->addr = 0;
state = ADDR_START;
length = i; /* break out of loop */
if (c == '\n') {
p->line_number++;
p->char_number = 0;
}
*r_begin = p->begin;
*r_end = p->end;
result = Found_IPv4;
} else if (state == SECOND3) {
p->end = (p->addr << 8) | p->tmp;
p->tmp = 0;
p->digit_count = 0;
p->addr = 0;
state = ADDR_START;
length = i; /* break out of loop */
if (c == '\n') {
p->line_number++;
p->char_number = 0;
}
*r_begin = p->begin;
*r_end = p->end;
result = Found_IPv4;
} else {
state = NUMBER_ERR;
length = i;
}
break;
default:
state = ERROR;
length = i; /* break out of loop */
break;
}
break;
default:
case ERROR:
case NUMBER_ERR:
case SECOND_ERR:
state = ERROR;
length = i; /* break */
break;
}
}
/* The 'offset' parameter is optional. If NULL, then
* we don't return a value */
if (r_offset)
*r_offset = i;
p->state = state;
if (state == ERROR || state == NUMBER_ERR || state == SECOND_ERR)
result = Found_Error;
return result;
}
/***************************************************************************
* Test errors. We should get exactly which line-number and which character
* in the line caused the error
***************************************************************************/
static int
rangefile_test_error(const char *buf, unsigned long long in_line_number, unsigned long long in_char_number, unsigned which_test)
{
size_t length = strlen(buf);
size_t offset = 0;
struct massip_parser p[1];
unsigned out_begin = 0xa3a3a3a3;
unsigned out_end = 0xa3a3a3a3;
unsigned long long out_line_number;
unsigned long long out_char_number;
int x;
/* test the entire buffer */
_parser_init(p);
x = _parser_next(p, buf, &offset, length, &out_begin, &out_end);
if (x != Found_Error)
goto fail;
_parser_err(p, &out_line_number, &out_char_number);
if (in_line_number != out_line_number || in_char_number != out_char_number)
goto fail;
/* test one byte at a time */
_parser_destroy(p);
_parser_init(p);
offset = 0;
out_begin = 0xa3a3a3a3;
out_end = 0xa3a3a3a3;
x = 0;
while (offset < length) {
x = _parser_next(p, buf, &offset, offset+1, &out_begin, &out_end);
if (x == Found_Error)
break;
}
if (x != Found_Error)
goto fail;
_parser_err(p, &out_line_number, &out_char_number);
if (in_line_number != out_line_number || in_char_number != out_char_number)
goto fail;
_parser_destroy(p);
return 0;
fail:
_parser_destroy(p);
fprintf(stderr, "[-] rangefile test fail, line=%u\n", which_test);
return 1;
}
/***************************************************************************
***************************************************************************/
int
massip_parse_file(struct MassIP *massip, const char *filename)
{
struct RangeList *targets_ipv4 = &massip->ipv4;
struct Range6List *targets_ipv6 = &massip->ipv6;
struct massip_parser p[1];
char buf[65536];
FILE *fp = NULL;
bool is_error = false;
unsigned addr_count = 0;
unsigned long long line_number, char_number;
/* Kludge: should never happen, should fix this when reading in
* config, not this deep in the code. */
if (filename == 0 || filename[0] == '\0') {
fprintf(stderr, "[-] missing filename for ranges\n");
exit(1);
}
/*
* Open the file containing IP addresses, which can potentially be
* many megabytes in size
*/
if (strcmp(filename, "-") == 0) {
fp = stdin;
} else {
int err;
err = fopen_s(&fp, filename, "rb");
if (err || fp == NULL) {
perror(filename);
exit(1);
}
}
/*
* Create a parser for reading in the IP addresses using a state
* machine parser
*/
_parser_init(p);
/*
* Read in the data a block at a time, parsing according to the state
* machine.
*/
while (!is_error) {
size_t count;
size_t offset;
count = fread(buf, 1, sizeof(buf), fp);
if (count <= 0)
break;
offset = 0;
while (offset < count) {
unsigned begin, end;
int err;
err = _parser_next(p, buf, &offset, count, &begin, &end);
switch (err) {
case Still_Working:
if (offset < count) {
/* We reached this somehow in the middle of the buffer, but
* this return is only possible at the end of the buffer */
fprintf(stderr, "[-] rangeparse_next(): unknown coding failure\n");
}
break;
case Found_Error:
default:
_parser_err(p, &line_number, &char_number);
fprintf(stderr, "[-] %s:%llu:%llu: invalid IP address on line #%llu\n", filename, line_number, char_number, line_number);
is_error = true;
count = offset;
break;
case Found_IPv4:
rangelist_add_range(targets_ipv4, begin, end);
addr_count++;
break;
case Found_IPv6:
{
ipv6address found_begin, found_end;
_parser_get_ipv6(p, &found_begin, &found_end);
range6list_add_range(targets_ipv6, found_begin, found_end);
addr_count++;
}
break;
}
}
}
/* Close the file, unless we are reading from <stdin> */
if (fp != stdin && fp != NULL)
fclose(fp);
/* In case the file doesn't end with a newline '\n', then artificially
* add one to the end. This is just a repeat of the code above */
if (!is_error) {
size_t offset = 0;
unsigned begin, end;
int err;
err = _parser_next(p, "\n", &offset, 1, &begin, &end);
switch (err) {
case Still_Working:
break;
case Found_Error:
default:
_parser_err(p, &line_number, &char_number);
fprintf(stderr, "[-] %s:%llu:%llu: invalid IP address on line #%llu\n", filename, line_number, char_number, line_number);
is_error = true;
break;
case Found_IPv4:
rangelist_add_range(targets_ipv4, begin, end);
addr_count++;
break;
case Found_IPv6:
{
ipv6address found_begin, found_end;
_parser_get_ipv6(p, &found_begin, &found_end);
range6list_add_range(targets_ipv6, found_begin, found_end);
addr_count++;
}
break;
}
}
LOG(1, "[+] %s: %u addresses read\n", filename, addr_count);
/* Target list must be sorted every time it's been changed,
* before it can be used */
rangelist_sort(targets_ipv4);
if (is_error)
return -1; /* fail */
else
return 0; /* success*/
}
ipv6address
massip_parse_ipv6(const char *line)
{
struct massip_parser p[1];
size_t count = strlen(line);
size_t offset = 0;
int err;
unsigned begin, end;
ipv6address result;
ipv6address range;
_parser_init(p);
err = _parser_next(p, line, &offset, count, &begin, &end);
again:
switch (err) {
case Still_Working:
if (offset < count) {
/* We reached this somehow in the middle of the buffer, but
* this return is only possible at the end of the buffer */
fprintf(stderr, "[-] _parser_next(): unknown coding failure\n");
goto fail;
} else {
err = _parser_next(p, "\n", 0, 1, &begin, &end);
if (err == Still_Working) {
fprintf(stderr, "[-] _parser_next(): unknown coding failure\n");
goto fail;
} else {
goto again;
}
}
break;
case Found_Error:
default:
goto fail;
case Found_IPv4:
goto fail;
case Found_IPv6:
_parser_get_ipv6(p, &result, &range);
if (!ipv6address_is_equal(result, range))
goto fail;
return result;
}
fail: