-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathiplike.c
1214 lines (1105 loc) · 30.3 KB
/
iplike.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
/*
// This file is part of the OpenNMS(R) Application.
//
// OpenNMS(R) is Copyright (C) 2002-2003 The OpenNMS Group, Inc. All rights reserved.
// OpenNMS(R) is a derivative work, containing both original code, included code and modified
// code that was published under the GNU General Public License. Copyrights for modified
// and included code are below.
//
// OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
//
// Copyright (C) 1999-2001 Oculan Corp. All rights reserved.
//
// 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.
//
// For more information contact:
// OpenNMS Licensing <[email protected]>
// http://www.opennms.org/
// http://www.opennms.com/
//
// Tab Size = 8
//
// iplike.c,v 1.1.1.1 2001/11/11 17:40:07 ben Exp
//
*/
#include <stdlib.h>
#include <string.h>
#include <config.h>
#include <ctype.h> /* used for isdigit() & isspace() */
#include <pwd.h> /* so we don't get errors from pgsql's port.h */
#include <netdb.h> /* so we don't get errors from pgsql's port.h */
#ifdef HAVE_WINDEF_H
#include <winsock2.h>
#include <windef.h>
#endif
/*
* These PACKAGE_* defines might be defined both by PostgreSQL and us, so undef
* ours before we include postgres.h (sigh). Maybe we could do the opposite
* and move these if/undef/endifs after the include of postgres.h and then
* include our config.h? It doesn't seem to matter now because we don't use
* any of the PACKAGE_* defines.
*/
#ifdef PACKAGE_BUGREPORT
# undef PACKAGE_BUGREPORT
#endif
#ifdef PACKAGE_NAME
# undef PACKAGE_NAME
#endif
#ifdef PACKAGE_STRING
# undef PACKAGE_STRING
#endif
#ifdef PACKAGE_TARNAME
# undef PACKAGE_TARNAME
#endif
#ifdef PACKAGE_VERSION
# undef PACKAGE_VERSION
#endif
#ifdef UNDEF_FILE_OFFSET_BITS
# undef _FILE_OFFSET_BITS
#endif /* UNDEF_FILE_OFFSET_BITS */
#include <postgres.h> /* PostgreSQL types */
#include <fmgr.h>
#include <utils/builtins.h>
#ifdef PG_MODULE_MAGIC
# ifndef DLLIMPORT
# define DLLIMPORT
# endif
# ifndef DLLEXPORT
# define DLLEXPORT
# endif
PG_MODULE_MAGIC;
#endif
/**
* The OctetRange element is used to hold a single item from
* an iplike match string. An octet may be either specific,
* all, or an inclusive range or a list of any of these filters
* separated by commas.
*/
struct OctetRange
{
#define RANGE_TYPE_SPECIFIC 0
#define RANGE_TYPE_ALL 1
#define RANGE_TYPE_INCLUSIVE 3
int type;
union
{
int specific;
int endpoints[2];
} un;
};
typedef struct OctetRange OctetRange_t;
struct OctetRangeArray
{
int count;
OctetRange_t *ranges;
};
typedef struct OctetRangeArray OctetRangeArray_t;
static const int trim(const char * *const p, int *const len);
static const int getIPv4RangeInfo(const char *p, int len, OctetRangeArray_t *const dest);
static const int getIPv6RangeInfo(const char *p, int len, OctetRangeArray_t *const dest);
void getOctetRangeArrayString(const OctetRangeArray_t array, char *string) {
int i;
strcpy(string, "");
for (i = 0; i < array.count; i++) {
char temp[255] = "";
switch(array.ranges[i].type) {
case RANGE_TYPE_SPECIFIC:
sprintf(temp, "[%d]", array.ranges[i].un.specific);
break;
case RANGE_TYPE_ALL:
sprintf(temp, "[*]");
break;
case RANGE_TYPE_INCLUSIVE:
sprintf(temp, "[%d-%d]", array.ranges[i].un.endpoints[0], array.ranges[i].un.endpoints[1]);
break;
}
strcat(string, temp);
}
}
/**
* Converts a dotted decimal IP Address to its
* component octets. The component octets are
* stored in the destination address which
* must be four in length. No bounds checking
* is performed on the destination buffer!
*
* p - Pointer to the dotted decimal address buffer
* len - The length of the buffer.
* dest - The destination buffer
*
* Returns:
* Returns zero on success, non-zero on error!
*
*/
static
const int convertIPv4(const char *p, int len, int *const dest) {
int octet = 0; /* used for converting the text to binary data */
int ndx = 0; /* index into the dest buffer */
int hadDigit = 0; /* set to one when a digit is encountered */
/*
* decode the octets
*/
ndx = 0;
while(len > 0 && ndx < 4)
{
if(isdigit((int)*p))
{
/*
* convert the digit and multiply the
* current value by 10. Remember it's
* dotted decimal (that's base 10!)
*/
octet = (octet * 10) + (*p - '0');
hadDigit = 1;
}
else if(*p == '.')
{
/**
* Store off the octet, but perform
* some basic checks
*/
if(octet > 255 || hadDigit == 0)
return -1;
hadDigit = 0;
dest[ndx++] = octet;
octet = 0;
}
else
{
/**
* invalid character
*/
return -1;
}
/*
* next please
*/
++p;
--len;
}
/*
* handle the case where we ran out of buffer
*/
if(ndx == 3 && hadDigit != 0)
{
/**
* Again just perform some basic checks
*/
if(octet > 255)
return -1;
dest[ndx++] = octet;
}
/**
* if we got four octets then it was
* successful, else it was not and an
* error condition needs to be returned.
*/
return (ndx == 4 ? 0 : -1);
}
static
const int convertIPv6(const char *p, int len, int *const dest) {
int octet = 0; /* used for converting the text to binary data */
int ndx = 0; /* index into the dest buffer */
int hadDigit = 0; /* set to one when a digit is encountered */
bool hexBase = true;
/*
* decode the octets
*/
ndx = 0;
while(len > 0 && ndx < 9)
{
if(isdigit((int)*p))
{
if (hexBase) {
// Convert the digit and multiply the current
// value by 16. IPv6 is colon-separated
// hexadecimal.
octet = (octet * 16) + (*p - '0');
hadDigit = 1;
} else {
// If we're inside the scope identifier,
// then the digits are decimal integers.
octet = (octet * 10) + (*p - '0');
hadDigit = 1;
}
}
else if(*p >= 'A' && *p <= 'F')
{
/*
* convert the digit and multiply the
* current value by 16. IPv6 is
* colon-separated hexadecimal.
*/
octet = (octet * 16) + (*p - 'A' + 10);
hadDigit = 1;
}
else if(*p >= 'a' && *p <= 'f')
{
/*
* convert the digit and multiply the
* current value by 16. IPv6 is
* colon-separated hexadecimal.
*/
octet = (octet * 16) + (*p - 'a' + 10);
hadDigit = 1;
}
else if(*p == ':' || *p == '%')
{
/**
* Store off the octet, but perform
* some basic checks
*/
if(octet > 65535 || hadDigit == 0)
return -1;
hadDigit = 0;
dest[ndx++] = octet;
octet = 0;
// If we've hit the scope identifier, then switch to decimal conversion mode
if (*p == '%') { hexBase = false; }
}
else
{
/**
* invalid character
*/
return -1;
}
/*
* next please
*/
++p;
--len;
}
/*
* handle the case where we ran out of buffer
*/
if(ndx < 9 && hadDigit != 0)
{
/**
* Again just perform some basic checks
*/
if(octet > 65535)
return -1;
dest[ndx++] = octet;
}
/**
* if we got four octets then it was
* successful, else it was not and an
* error condition needs to be returned.
*/
return ((ndx == 8 || ndx == 9) ? 0 : -1);
}
/**
* Converts an IPv4 or IPv6 Address to its
* component octets. The component octets are
* stored in the destination address which
* must be less than 9 integers in length.
* No bounds checking
* is performed on the destination buffer!
*
* p - Pointer to the address buffer
* len - The length of the buffer
* dest - The destination buffer
*
* Returns:
* Returns zero on success, non-zero on error!
*
*/
static
const int convertIP(const char *p, int len, int *dest)
{
int i;
/*
* check to make sure that the data is value
*/
if(dest == NULL || p == NULL || len <= 0)
return -1;
// Pass by reference so that the current values are updated
if (trim(&p, &len) < 0)
return -1;
// Scan for the first character that can determine whether this
// is an IPv4 address or IPv6 address.
for(i = 0; i < len; i++)
{
const char curr = *(p+i);
if (curr == '.') {
return convertIPv4(p, len, dest);
} else if (curr >= 'a' && curr <= 'f') {
return convertIPv6(p, len, dest);
} else if (curr >= 'A' && curr <= 'F') {
return convertIPv6(p, len, dest);
} else if (curr == ':') {
return convertIPv6(p, len, dest);
} else if (curr == '%') {
return convertIPv6(p, len, dest);
}
}
return -1;
}
static
const int trim(const char * *const p, int *const len) {
// perform some basic checks
if(*p == NULL || *len <= 0)
return -1;
// increment past the space chars
while(isspace((int)**p) && *len > 0) {
++*p, --*len;
}
// shift space off the end
while(*len > 0 && (isspace((int)(*(*p + *len - 1))) || (int)(*(*p + *len - 1)) == 0)) {
--*len;
}
// Another basic check
if(*len <= 0)
return -1;
return 0;
}
enum {
DEFAULT = 0,
ALL = 1,
RANGE = 2
};
enum {
OUT = 0,
IN = 1
};
/**
* Converts a dotted decimal IP Like Address to its
* component octets. The component octets are
* stored in the destination address which
* must be four in length. No bounds checking
* is performed on the destination buffer!
*
* p - Pointer to the dotted decimal address buffer
* len - The length of the buffer.
* dest - The destination buffer
*
* Returns:
* Returns zero on success, non-zero on error!
*
*/
static
const int getRangeInfo(const char *p, int len, OctetRangeArray_t *const dest)
{
int i;
/**
* perform some basic checks
*/
if(p == NULL || len <= 0 || dest == NULL)
return -1;
// Pass by reference so that the current values are updated
if (trim(&p, &len) < 0)
return -1;
// Scan for the first character that can determine whether this
// is an IPv4 address or IPv6 address.
for(i = 0; i < len; i++)
{
const char curr = *(p+i);
if (curr == '.') {
return getIPv4RangeInfo(p, len, dest);
} else if (curr >= 'a' && curr <= 'f') {
return getIPv6RangeInfo(p, len, dest);
} else if (curr >= 'A' && curr <= 'F') {
return getIPv6RangeInfo(p, len, dest);
} else if (curr == ':') {
return getIPv6RangeInfo(p, len, dest);
} else if (curr == '%') {
return getIPv6RangeInfo(p, len, dest);
}
}
return -1;
}
static
const int getIPv4RangeInfo(const char *p, int len, OctetRangeArray_t *const dest)
{
int ndx = 0; /* index into the dest parameter */
int hadDigit = 0; /* set true when a digit is encountered */
int octet = 0; /* the octet value, if any */
int rangeBegin = 0; /* the octet value, if any */
int i;
int state = DEFAULT;
int listState = OUT;
/*
* count the number of dots, must equal 3
*/
for(i = 0; i < len && ndx < 3; i++)
{
if(*(p+i) == '.')
++ndx;
}
if(ndx != 3)
return -1;
/*
* it's ok so decode it
*/
ndx = 0;
// octet = 0;
while(len > 0 && ndx < 4)
{
switch(*p) {
case '*':
/**
* sanity check
*/
// Must not follow a digit, must not occur in a range, and the next character must be
// either '.' or ','
if(hadDigit || octet != 0 || state == RANGE || rangeBegin != 0 || (len > 1 && (*(p+1) != '.' && *(p+1) != ',')))
return -1;
state = ALL;
break;
case ',':
/**
* sanity check
*/
if(octet > 255)
return -1;
listState = IN;
switch(state) {
case RANGE:
dest[ndx].ranges[dest[ndx].count].type = RANGE_TYPE_INCLUSIVE;
// Set the beginning of the range
dest[ndx].ranges[dest[ndx].count].un.endpoints[0] = rangeBegin;
// Set the end of the range
dest[ndx].ranges[dest[ndx].count].un.endpoints[1] = octet;
// If the beginning is greater than the end...
if(dest[ndx].ranges[dest[ndx].count].un.endpoints[0] > dest[ndx].ranges[dest[ndx].count].un.endpoints[1])
{
// ... then swap the digits
int swap = dest[ndx].ranges[dest[ndx].count].un.endpoints[0];
dest[ndx].ranges[dest[ndx].count].un.endpoints[0] = dest[ndx].ranges[dest[ndx].count].un.endpoints[1];
dest[ndx].ranges[dest[ndx].count].un.endpoints[1] = swap;
}
dest[ndx].count++;
rangeBegin = 0;
break;
case ALL:
dest[ndx].ranges[dest[ndx].count++].type = RANGE_TYPE_ALL;
break;
case DEFAULT:
dest[ndx].ranges[dest[ndx].count].type = RANGE_TYPE_SPECIFIC;
dest[ndx].ranges[dest[ndx].count++].un.specific = octet;
break;
}
state = DEFAULT;
hadDigit = 0;
octet = 0;
break;
case '-':
/**
* sanity check
*/
if(!hadDigit || state == ALL || rangeBegin != 0 || octet > 255)
return -1;
if(state != RANGE)
{
state = RANGE;
// Set the beginning of the range
rangeBegin = octet;
}
else
{
// You cannot have two dashes within a range
return -1;
}
hadDigit = 0;
octet = 0;
break;
case '.':
/**
* basic sanity check
*/
if(octet > 255 || (hadDigit == 0 && state != ALL))
return -1;
/**
* what type of value is it?
*/
if(listState == IN)
{
switch(state) {
case RANGE:
dest[ndx].ranges[dest[ndx].count].type = RANGE_TYPE_INCLUSIVE;
// Set the beginning of the range
dest[ndx].ranges[dest[ndx].count].un.endpoints[0] = rangeBegin;
// Set the end of the range
dest[ndx].ranges[dest[ndx].count].un.endpoints[1] = octet;
// If the beginning is greater than the end...
if(dest[ndx].ranges[dest[ndx].count].un.endpoints[0] > dest[ndx].ranges[dest[ndx].count].un.endpoints[1])
{
// ... then swap the digits
int swap = dest[ndx].ranges[dest[ndx].count].un.endpoints[0];
dest[ndx].ranges[dest[ndx].count].un.endpoints[0] = dest[ndx].ranges[dest[ndx].count].un.endpoints[1];
dest[ndx].ranges[dest[ndx].count].un.endpoints[1] = swap;
}
dest[ndx].count++;
rangeBegin = 0;
break;
case ALL:
dest[ndx].ranges[dest[ndx].count++].type = RANGE_TYPE_ALL;
break;
case DEFAULT:
dest[ndx].ranges[dest[ndx].count].type = RANGE_TYPE_SPECIFIC;
dest[ndx].ranges[dest[ndx].count++].un.specific = octet;
break;
}
listState = OUT;
}
else if(state == RANGE)
{
dest[ndx].ranges[dest[ndx].count].type = RANGE_TYPE_INCLUSIVE;
// Set the beginning of the range
dest[ndx].ranges[dest[ndx].count].un.endpoints[0] = rangeBegin;
// Set the end of the range
dest[ndx].ranges[dest[ndx].count].un.endpoints[1] = octet;
// If the beginning is greater than the end...
if(dest[ndx].ranges[dest[ndx].count].un.endpoints[0] > dest[ndx].ranges[dest[ndx].count].un.endpoints[1])
{
// ... then swap the digits
int swap = dest[ndx].ranges[dest[ndx].count].un.endpoints[0];
dest[ndx].ranges[dest[ndx].count].un.endpoints[0] = dest[ndx].ranges[dest[ndx].count].un.endpoints[1];
dest[ndx].ranges[dest[ndx].count].un.endpoints[1] = swap;
}
dest[ndx].count++;
rangeBegin = 0;
}
else if(state == ALL)
{
dest[ndx].ranges[dest[ndx].count++].type = RANGE_TYPE_ALL;
}
else
{
dest[ndx].ranges[dest[ndx].count].type = RANGE_TYPE_SPECIFIC;
dest[ndx].ranges[dest[ndx].count++].un.specific = octet;
}
ndx++;
state = DEFAULT;
hadDigit = 0;
octet = 0;
break;
default:
if(isdigit((int)*p))
{
// Update the decimal octet value
octet = (octet * 10) + (*p - '0');
hadDigit = 1;
break;
}
else
{
// This character sequence was unexpected
return -1;
}
}
/*
* next please
*/
++p;
--len;
}
/*
* handle the case where we ran out of buffer
*/
if(ndx == 3)
{
if(octet > 255 || (hadDigit == 0 && state != ALL) || (listState == IN && state == RANGE))
return -1;
if(listState == IN)
{
switch(state) {
case RANGE:
dest[ndx].ranges[dest[ndx].count].type = RANGE_TYPE_INCLUSIVE;
// Set the beginning of the range
dest[ndx].ranges[dest[ndx].count].un.endpoints[0] = rangeBegin;
// Set the end of the range
dest[ndx].ranges[dest[ndx].count].un.endpoints[1] = octet;
// If the beginning is greater than the end...
if(dest[ndx].ranges[dest[ndx].count].un.endpoints[0] > dest[ndx].ranges[dest[ndx].count].un.endpoints[1])
{
// ... then swap the digits
int swap = dest[ndx].ranges[dest[ndx].count].un.endpoints[0];
dest[ndx].ranges[dest[ndx].count].un.endpoints[0] = dest[ndx].ranges[dest[ndx].count].un.endpoints[1];
dest[ndx].ranges[dest[ndx].count].un.endpoints[1] = swap;
}
dest[ndx].count++;
break;
case ALL:
dest[ndx].ranges[dest[ndx].count++].type = RANGE_TYPE_ALL;
break;
case DEFAULT:
dest[ndx].ranges[dest[ndx].count].type = RANGE_TYPE_SPECIFIC;
dest[ndx].ranges[dest[ndx].count++].un.specific = octet;
break;
}
}
else if(state == RANGE)
{
dest[ndx].ranges[dest[ndx].count].type = RANGE_TYPE_INCLUSIVE;
// Set the beginning of the range
dest[ndx].ranges[dest[ndx].count].un.endpoints[0] = rangeBegin;
// Set the end of the range
dest[ndx].ranges[dest[ndx].count].un.endpoints[1] = octet;
// If the beginning is greater than the end...
if(dest[ndx].ranges[dest[ndx].count].un.endpoints[0] > dest[ndx].ranges[dest[ndx].count].un.endpoints[1])
{
// ... then swap the digits
int swap = dest[ndx].ranges[dest[ndx].count].un.endpoints[0];
dest[ndx].ranges[dest[ndx].count].un.endpoints[0] = dest[ndx].ranges[dest[ndx].count].un.endpoints[1];
dest[ndx].ranges[dest[ndx].count].un.endpoints[1] = swap;
}
dest[ndx].count++;
}
else if(state == ALL)
{
dest[ndx].ranges[dest[ndx].count++].type = RANGE_TYPE_ALL;
}
else
{
dest[ndx].ranges[dest[ndx].count].type = RANGE_TYPE_SPECIFIC;
dest[ndx].ranges[dest[ndx].count++].un.specific = octet;
}
ndx++;
}
/**
* return the result
*/
return (ndx == 4 ? 0 : -1);
}
static
const int getIPv6RangeInfo(const char *p, int len, OctetRangeArray_t *const dest)
{
int ndx = 0; /* index into the dest parameter */
int hadDigit = 0; /* set true when a digit is encountered */
int octet = 0; /* the octet value, if any */
int rangeBegin = 0; /* the octet value, if any */
bool hexBase = true;
int i;
int state = DEFAULT;
int listState = OUT;
/*
* count the number of colons, must equal 7
*/
for(i = 0; i < len && ndx < 7; i++)
{
if(*(p+i) == ':')
++ndx;
}
if(ndx != 7)
return -1;
/*
* it's ok so decode it
*/
ndx = 0;
while(len > 0 && ndx < 9)
{
switch(*p) {
case '*':
/**
* sanity check
*/
// Must not follow a digit, must not occur in a range, and the next character must be
// either ':' or '%' or ','
if(hadDigit || octet != 0 || state == RANGE || rangeBegin != 0 || (len > 1 && (*(p+1) != '%' && *(p+1) != ':' && *(p+1) != ',')))
return -1;
state = ALL;
break;
case ',':
/**
* sanity check
*/
if(octet > 65535)
return -1;
listState = IN;
switch(state) {
case RANGE:
dest[ndx].ranges[dest[ndx].count].type = RANGE_TYPE_INCLUSIVE;
// Set the beginning of the range
dest[ndx].ranges[dest[ndx].count].un.endpoints[0] = rangeBegin;
// Set the end of the range
dest[ndx].ranges[dest[ndx].count].un.endpoints[1] = octet;
// If the beginning is greater than the end...
if(dest[ndx].ranges[dest[ndx].count].un.endpoints[0] > dest[ndx].ranges[dest[ndx].count].un.endpoints[1])
{
// ... then swap the digits
int swap = dest[ndx].ranges[dest[ndx].count].un.endpoints[0];
dest[ndx].ranges[dest[ndx].count].un.endpoints[0] = dest[ndx].ranges[dest[ndx].count].un.endpoints[1];
dest[ndx].ranges[dest[ndx].count].un.endpoints[1] = swap;
}
dest[ndx].count++;
rangeBegin = 0;
break;
case ALL:
dest[ndx].ranges[dest[ndx].count++].type = RANGE_TYPE_ALL;
break;
case DEFAULT:
dest[ndx].ranges[dest[ndx].count].type = RANGE_TYPE_SPECIFIC;
dest[ndx].ranges[dest[ndx].count++].un.specific = octet;
break;
}
state = DEFAULT;
hadDigit = 0;
octet = 0;
break;
case '-':
/**
* sanity check
*/
if(!hadDigit || state == ALL || rangeBegin != 0 || octet > 65535)
return -1;
if(state != RANGE)
{
state = RANGE;
// Set the beginning of the range
rangeBegin = octet;
}
else
{
// You cannot have two dashes within a range
return -1;
}
hadDigit = 0;
octet = 0;
break;
case ':':
case '%':
/**
* basic sanity check
*/
if(octet > 65535 || (hadDigit == 0 && state != ALL))
return -1;
// If we've hit the scope identifier, then switch to decimal conversion mode
if (*p == '%') { hexBase = false; }
/**
* what type of value is it?
*/
if(listState == IN)
{
switch(state) {
case RANGE:
dest[ndx].ranges[dest[ndx].count].type = RANGE_TYPE_INCLUSIVE;
// Set the beginning of the range
dest[ndx].ranges[dest[ndx].count].un.endpoints[0] = rangeBegin;
// Set the end of the range
dest[ndx].ranges[dest[ndx].count].un.endpoints[1] = octet;
// If the beginning is greater than the end...
if(dest[ndx].ranges[dest[ndx].count].un.endpoints[0] > dest[ndx].ranges[dest[ndx].count].un.endpoints[1])
{
// ... then swap the digits
int swap = dest[ndx].ranges[dest[ndx].count].un.endpoints[0];
dest[ndx].ranges[dest[ndx].count].un.endpoints[0] = dest[ndx].ranges[dest[ndx].count].un.endpoints[1];
dest[ndx].ranges[dest[ndx].count].un.endpoints[1] = swap;
}
dest[ndx].count++;
rangeBegin = 0;
break;
case ALL:
dest[ndx].ranges[dest[ndx].count++].type = RANGE_TYPE_ALL;
break;
case DEFAULT:
dest[ndx].ranges[dest[ndx].count].type = RANGE_TYPE_SPECIFIC;
dest[ndx].ranges[dest[ndx].count++].un.specific = octet;
break;
}
listState = OUT;
}
else if(state == RANGE)
{
dest[ndx].ranges[dest[ndx].count].type = RANGE_TYPE_INCLUSIVE;
// Set the beginning of the range
dest[ndx].ranges[dest[ndx].count].un.endpoints[0] = rangeBegin;
// Set the end of the range
dest[ndx].ranges[dest[ndx].count].un.endpoints[1] = octet;
// If the beginning is greater than the end...
if(dest[ndx].ranges[dest[ndx].count].un.endpoints[0] > dest[ndx].ranges[dest[ndx].count].un.endpoints[1])
{
// ... then swap the digits
int swap = dest[ndx].ranges[dest[ndx].count].un.endpoints[0];
dest[ndx].ranges[dest[ndx].count].un.endpoints[0] = dest[ndx].ranges[dest[ndx].count].un.endpoints[1];
dest[ndx].ranges[dest[ndx].count].un.endpoints[1] = swap;
}
dest[ndx].count++;
rangeBegin = 0;
}
else if(state == ALL)
{
dest[ndx].ranges[dest[ndx].count++].type = RANGE_TYPE_ALL;
}
else
{
dest[ndx].ranges[dest[ndx].count].type = RANGE_TYPE_SPECIFIC;
dest[ndx].ranges[dest[ndx].count++].un.specific = octet;
}
ndx++;
state = DEFAULT;
hadDigit = 0;
octet = 0;
break;
default:
if(isdigit((int)*p))
{
if (hexBase) {
// Convert the digit and multiply the current
// value by 16. IPv6 is colon-separated
// hexadecimal.
octet = (octet * 16) + (*p - '0');
} else {
// If we're inside the scope identifier,
// then the digits are decimal integers.
octet = (octet * 10) + (*p - '0');
}
hadDigit = 1;
}
else if(*p >= 'A' && *p <= 'F')
{
/*
* convert the digit and multiply the
* current value by 16. IPv6 is
* colon-separated hexadecimal.
*/
octet = (octet * 16) + (*p - 'A' + 10);
hadDigit = 1;
}
else if(*p >= 'a' && *p <= 'f')
{
/*
* convert the digit and multiply the
* current value by 16. IPv6 is
* colon-separated hexadecimal.
*/
octet = (octet * 16) + (*p - 'a' + 10);
hadDigit = 1;
}
else
{
// This character sequence was unexpected
return -1;
}
}
/*
* next please
*/
++p;
--len;
}
/*
* handle the case where we ran out of buffer
*/
if(ndx < 9)
{
if(octet > 65535 || (hadDigit == 0 && state != ALL) || (listState == IN && state == RANGE))
return -1;
if(listState == IN)
{
switch(state) {
case RANGE:
dest[ndx].ranges[dest[ndx].count].type = RANGE_TYPE_INCLUSIVE;
// Set the beginning of the range
dest[ndx].ranges[dest[ndx].count].un.endpoints[0] = rangeBegin;
// Set the end of the range
dest[ndx].ranges[dest[ndx].count].un.endpoints[1] = octet;
// If the beginning is greater than the end...
if(dest[ndx].ranges[dest[ndx].count].un.endpoints[0] > dest[ndx].ranges[dest[ndx].count].un.endpoints[1])
{