-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsed_address.go
1764 lines (1663 loc) · 58.8 KB
/
parsed_address.go
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
package goip
import (
"strconv"
"sync"
"unsafe"
"github.com/pchchv/goip/address_error"
"github.com/pchchv/goip/address_string_param"
)
type translatedResult struct {
sections *sectionResult
rng *SequentialRange[*IPAddress]
mask *IPAddress
}
type boundaryResult struct {
lowerSection *IPAddressSection
upperSection *IPAddressSection
}
func (res *boundaryResult) createMask() *IPAddress {
lowerSection := res.lowerSection
creator := lowerSection.getAddrType().getIPNetwork().getIPAddressCreator()
return creator.createAddressInternalFromSection(res.lowerSection, NoZone, nil)
}
func (res *boundaryResult) createRange() *SequentialRange[*IPAddress] {
// add zone in order to reuse the lower and upper
var rangeUpper *IPAddress
lowerSection := res.lowerSection
creator := lowerSection.getAddrType().getIPNetwork().getIPAddressCreator()
rangeLower := creator.createAddressInternalFromSection(lowerSection, NoZone, nil)
if res.upperSection == nil {
rangeUpper = rangeLower
} else {
rangeUpper = creator.createAddressInternalFromSection(res.upperSection, NoZone, nil)
}
return rangeLower.SpanWithRange(rangeUpper)
}
type sectionResult struct {
section *IPAddressSection
hostSection *IPAddressSection
address *IPAddress
hostAddress *IPAddress
joinHostError address_error.IncompatibleAddressError
joinAddressError address_error.IncompatibleAddressError
mixedError address_error.IncompatibleAddressError
maskError address_error.IncompatibleAddressError
}
func (res *sectionResult) withoutAddressException() bool {
return res.joinAddressError == nil && res.mixedError == nil && res.maskError == nil
}
type parsedIPAddress struct {
ipAddressParseData
ipAddrProvider // provides a few methods like isInvalid
options address_string_param.IPAddressStringParams
originator HostIdentifierString
vals translatedResult
skipCntains *bool
maskers []Masker
mixedMaskers []Masker
creationLock sync.Mutex
}
func (parseData *parsedIPAddress) values() *translatedResult {
return &parseData.vals
}
func (parseData *parsedIPAddress) isProvidingIPAddress() bool {
return true
}
func (parseData *parsedIPAddress) getParameters() address_string_param.IPAddressStringParams {
return parseData.options
}
func (parseData *parsedIPAddress) isProvidingMixedIPv6() bool {
return parseData.ipAddressParseData.isProvidingMixedIPv6()
}
func (parseData *parsedIPAddress) isProvidingIPv6() bool {
return parseData.ipAddressParseData.isProvidingIPv6()
}
func (parseData *parsedIPAddress) isProvidingIPv4() bool {
return parseData.ipAddressParseData.isProvidingIPv4()
}
func (parseData *parsedIPAddress) isProvidingBase85IPv6() bool {
return parseData.ipAddressParseData.isProvidingBase85IPv6()
}
func (parseData *parsedIPAddress) getProviderIPVersion() IPVersion {
return parseData.ipAddressParseData.getProviderIPVersion()
}
func (parseData *parsedIPAddress) getIPAddressParseData() *ipAddressParseData {
return &parseData.ipAddressParseData
}
func (parseData *parsedIPAddress) getVersionedAddress(version IPVersion) (*IPAddress, address_error.IncompatibleAddressError) {
thisVersion := parseData.getProviderIPVersion()
if version != thisVersion {
return nil, nil
}
return parseData.getProviderAddress()
}
// isPrefixSubnet is not called with parsing data from inetAton or single-segment strings, so casting to int is acceptable.
// This is only for addresses with the standard segment counts, although compressed addresses are allowed.
func (parseData *parsedIPAddress) isPrefixSubnet(networkPrefixLength BitCount) bool {
var max SegInt
var bytesPerSegment int
var bitsPerSegment BitCount
if parseData.isProvidingIPv4() {
bytesPerSegment = IPv4BytesPerSegment
bitsPerSegment = IPv4BitsPerSegment
max = IPv4MaxValuePerSegment
} else {
bytesPerSegment = IPv6BytesPerSegment
bitsPerSegment = IPv6BitsPerSegment
max = IPv6MaxValuePerSegment
}
addressParseData := parseData.getAddressParseData()
segmentCount := addressParseData.getSegmentCount()
if parseData.isCompressed() {
compressedCount := IPv6SegmentCount - segmentCount
compressedIndex := addressParseData.getConsecutiveSeparatorSegmentIndex()
return isPrefixSubnet(
func(segmentIndex int) SegInt {
if segmentIndex >= compressedIndex {
if segmentIndex-compressedIndex < compressedCount {
return 0
}
segmentIndex -= compressedCount
}
return SegInt(parseData.getValue(segmentIndex, keyLower))
},
func(segmentIndex int) SegInt {
if segmentIndex >= compressedIndex {
if segmentIndex-compressedIndex < compressedCount {
return 0
}
segmentIndex -= compressedCount
}
return SegInt(parseData.getValue(segmentIndex, keyUpper))
},
segmentCount+compressedCount,
bytesPerSegment,
bitsPerSegment,
max,
networkPrefixLength,
zerosOrFullRange)
}
return isPrefixSubnet(
func(segmentIndex int) SegInt {
return SegInt(parseData.getValue(segmentIndex, keyLower))
},
func(segmentIndex int) SegInt {
return SegInt(parseData.getValue(segmentIndex, keyUpper))
},
segmentCount,
bytesPerSegment,
bitsPerSegment,
max,
networkPrefixLength,
zerosOrFullRange)
}
func (parseData *parsedIPAddress) createSegment(
addressString string,
version IPVersion,
val,
upperVal SegInt,
useFlags bool,
parsedSegIndex int,
segmentPrefixLength PrefixLen,
creator parsedAddressCreator) (div *AddressDivision, isMultiple bool) {
parsed := parseData.getAddressParseData()
if val != upperVal {
return createRangeSeg(addressString, version, val, upperVal,
useFlags, parsed, parsedSegIndex,
segmentPrefixLength, creator), true
}
var result *AddressDivision
if !useFlags {
result = creator.createSegment(val, val, segmentPrefixLength)
} else {
result = creator.createSegmentInternal(
val,
segmentPrefixLength,
addressString,
val,
parsed.getFlag(parsedSegIndex, keyStandardStr),
parsed.getIndex(parsedSegIndex, keyLowerStrStartIndex),
parsed.getIndex(parsedSegIndex, keyLowerStrEndIndex))
}
return result, false
}
func (parseData *parsedIPAddress) getType() ipType {
return fromVersion(parseData.getProviderIPVersion())
}
func (parseData *parsedIPAddress) createIPv4Sections(doSections, doRangeBoundaries, withUpper bool) (sections sectionResult, boundaries boundaryResult) {
var segIsMult bool
isMultiple := false
isHostMultiple := false
qualifier := parseData.getQualifier()
prefLen := getPrefixLength(qualifier)
mask := parseData.getProviderMask()
if mask != nil && mask.GetBlockMaskPrefixLen(true) != nil {
mask = nil //we don't do any masking if the mask is a subnet mask, instead we just map it to the corresponding prefix length
}
hasMask := mask != nil
addrParseData := parseData.getAddressParseData()
segmentCount := addrParseData.getSegmentCount()
if hasMask && parseData.maskers == nil {
parseData.maskers = make([]Masker, segmentCount)
}
creator := ipv4Type.getIPNetwork().getIPAddressCreator()
missingCount := IPv4SegmentCount - segmentCount
var hostSegments, segments, lowerSegments, upperSegments []*AddressDivision
if doSections {
segments = createSegmentArray(IPv4SegmentCount)
} else if doRangeBoundaries {
lowerSegments = createSegmentArray(IPv4SegmentCount)
} else {
return
}
expandedSegments := missingCount <= 0
expandedStart, expandedEnd := -1, -1
addressString := parseData.str
maskedIsDifferent := false
for i, normalizedSegmentIndex := 0, 0; i < segmentCount; i++ {
lower := addrParseData.getValue(i, keyLower)
upper := addrParseData.getValue(i, keyUpper)
if !expandedSegments {
//check for any missing segments that we should account for here
isLastSegment := i == segmentCount-1
isWildcard := addrParseData.isWildcard(i)
expandedSegments = isLastSegment
if !expandedSegments {
// if we are inetAton, we must wait for last segment
// otherwise, we check if we are wildcard and no other wildcard further down
expandedSegments = !parseData.isInetAtonJoined() && isWildcard
if expandedSegments {
for j := i + 1; j < segmentCount; j++ {
if addrParseData.isWildcard(j) { //another wildcard further down
expandedSegments = false
break
}
}
}
}
if expandedSegments {
if isWildcard {
upper = 0xffffffff >> uint((3-missingCount)<<3)
} else {
expandedStart = i
expandedEnd = i + missingCount
}
bits := BitCount(missingCount+1) << ipv4BitsToSegmentBitshift // BitCount(missingCount+1) * IPv4BitsPerSegment
var maskedLower, maskedUpper uint64
if hasMask {
var divMask uint64
for k := 0; k <= missingCount; k++ {
divMask = (divMask << uint(IPv4BitsPerSegment)) | uint64(mask.GetSegment(normalizedSegmentIndex+k).GetSegmentValue())
}
masker := parseData.maskers[i]
if masker == nil {
maxValue := ^(^uint64(0) << uint(bits))
masker = MaskRange(lower, upper, divMask, maxValue)
parseData.maskers[i] = masker
}
if !masker.IsSequential() && sections.maskError == nil {
sections.maskError = &incompatibleAddressError{
addressError: addressError{
str: maskString(lower, upper, divMask),
key: "ipaddress.error.maskMismatch",
},
}
}
maskedLower = masker.GetMaskedLower(lower, divMask)
maskedUpper = masker.GetMaskedUpper(upper, divMask)
maskedIsDifferent = maskedIsDifferent || maskedLower != lower || maskedUpper != upper
} else {
maskedLower = lower
maskedUpper = upper
}
shift := bits
count := missingCount
for count >= 0 { // add the missing segments
shift -= IPv4BitsPerSegment
currentPrefix := getSegmentPrefixLength(IPv4BitsPerSegment, prefLen, normalizedSegmentIndex)
hostSegLower := SegInt((lower >> uint(shift)) & IPv4MaxValuePerSegment)
var hostSegUpper SegInt
if lower == upper {
hostSegUpper = hostSegLower
} else {
hostSegUpper = SegInt((upper >> uint(shift)) & IPv4MaxValuePerSegment)
}
var maskedSegLower, maskedSegUpper SegInt
if hasMask {
maskedSegLower = SegInt((maskedLower >> uint(shift)) & IPv4MaxValuePerSegment)
if maskedLower == maskedUpper {
maskedSegUpper = maskedSegLower
} else {
maskedSegUpper = SegInt((maskedUpper >> uint(shift)) & IPv4MaxValuePerSegment)
}
} else {
maskedSegLower = hostSegLower
maskedSegUpper = hostSegUpper
}
if doSections {
if maskedIsDifferent || currentPrefix != nil {
hostSegments = allocateSegments(hostSegments, segments, IPv4SegmentCount, normalizedSegmentIndex)
hostSegments[normalizedSegmentIndex], segIsMult = parseData.createSegment(
addressString,
IPv4,
hostSegLower,
hostSegUpper,
false,
i,
nil,
creator)
isHostMultiple = isHostMultiple || segIsMult
}
segments[normalizedSegmentIndex], segIsMult = parseData.createSegment(
addressString,
IPv4,
maskedSegLower,
maskedSegUpper,
false,
i,
currentPrefix,
creator)
isMultiple = isMultiple || segIsMult
}
if doRangeBoundaries {
isRange := maskedSegLower != maskedSegUpper
if !doSections || isRange {
if doSections {
lowerSegments = allocateSegments(lowerSegments, segments, IPv4SegmentCount, normalizedSegmentIndex)
} // else segments already allocated
lowerSegments[normalizedSegmentIndex], _ = parseData.createSegment(
addressString,
IPv4,
maskedSegLower,
maskedSegLower,
false,
i,
currentPrefix,
creator)
} else if lowerSegments != nil {
lowerSegments[normalizedSegmentIndex] = segments[normalizedSegmentIndex]
}
if withUpper {
if isRange {
upperSegments = allocateSegments(upperSegments, lowerSegments, IPv4SegmentCount, normalizedSegmentIndex)
upperSegments[normalizedSegmentIndex], _ = parseData.createSegment(
addressString,
IPv4,
maskedSegUpper,
maskedSegUpper,
false,
i,
currentPrefix,
creator)
} else if upperSegments != nil {
upperSegments[normalizedSegmentIndex] = lowerSegments[normalizedSegmentIndex]
}
}
}
normalizedSegmentIndex++
count--
}
addrParseData.setBitLength(i, bits)
continue
} // end handle inetAton joined segments
}
var masker Masker
unmasked := true
hostLower, hostUpper := lower, upper
if hasMask {
masker = parseData.maskers[i]
maskInt := uint64(mask.GetSegment(normalizedSegmentIndex).GetSegmentValue())
if masker == nil {
masker = MaskRange(lower, upper, maskInt, uint64(creator.getMaxValuePerSegment()))
parseData.maskers[i] = masker
}
if !masker.IsSequential() && sections.maskError == nil {
sections.maskError = &incompatibleAddressError{
addressError: addressError{
str: maskString(lower, upper, maskInt),
key: "ipaddress.error.maskMismatch",
},
}
}
lower = masker.GetMaskedLower(lower, maskInt)
upper = masker.GetMaskedUpper(upper, maskInt)
unmasked = hostLower == lower && hostUpper == upper
maskedIsDifferent = maskedIsDifferent || !unmasked
}
segmentPrefixLength := getSegmentPrefixLength(IPv4BitsPerSegment, prefLen, normalizedSegmentIndex)
if doSections {
if maskedIsDifferent || segmentPrefixLength != nil {
hostSegments = allocateSegments(hostSegments, segments, IPv4SegmentCount, normalizedSegmentIndex)
hostSegments[normalizedSegmentIndex], segIsMult = parseData.createSegment(
addressString,
IPv4,
SegInt(hostLower),
SegInt(hostUpper),
true,
i,
nil,
creator)
isHostMultiple = isHostMultiple || segIsMult
}
segments[normalizedSegmentIndex], segIsMult = parseData.createSegment(
addressString,
IPv4,
SegInt(lower),
SegInt(upper),
unmasked,
i,
segmentPrefixLength,
creator)
isMultiple = isMultiple || segIsMult
}
if doRangeBoundaries {
isRange := lower != upper
if !doSections || isRange {
if doSections {
lowerSegments = allocateSegments(lowerSegments, segments, IPv4SegmentCount, normalizedSegmentIndex)
} // else segments already allocated
lowerSegments[normalizedSegmentIndex], _ = parseData.createSegment(
addressString,
IPv4,
SegInt(lower),
SegInt(lower),
false,
i,
segmentPrefixLength,
creator)
} else if lowerSegments != nil {
lowerSegments[normalizedSegmentIndex] = segments[normalizedSegmentIndex]
}
if withUpper {
if isRange {
upperSegments = allocateSegments(upperSegments, lowerSegments, IPv4SegmentCount, normalizedSegmentIndex)
upperSegments[normalizedSegmentIndex], _ = parseData.createSegment(
addressString,
IPv4,
SegInt(upper),
SegInt(upper),
false,
i,
segmentPrefixLength,
creator)
} else if upperSegments != nil {
upperSegments[normalizedSegmentIndex] = lowerSegments[normalizedSegmentIndex]
}
}
}
normalizedSegmentIndex++
addrParseData.setBitLength(i, IPv4BitsPerSegment)
}
var result, hostResult *IPAddressSection
prefLength := getPrefixLength(qualifier)
if doSections {
result = creator.createPrefixedSectionInternal(segments, isMultiple, prefLength)
sections.section = result
if hostSegments != nil {
hostResult = creator.createSectionInternal(hostSegments, isHostMultiple).ToIP()
sections.hostSection = hostResult
if checkExpandedValues(hostResult, expandedStart, expandedEnd) {
sections.joinHostError = &incompatibleAddressError{
addressError{
str: addressString,
key: "ipaddress.error.invalid.joined.ranges",
},
}
}
}
if checkExpandedValues(result, expandedStart, expandedEnd) {
sections.joinAddressError = &incompatibleAddressError{addressError{str: addressString, key: "ipaddress.error.invalid.joined.ranges"}}
if hostResult == nil {
sections.joinHostError = sections.joinAddressError
}
}
}
if doRangeBoundaries {
// if we have a prefix subnet,
// it is possible our lower and upper boundaries exceed what appears in the parsed address
prefixLength := getPrefixLength(qualifier)
isPrefixSub := false
if prefixLength != nil {
var lowerSegs, upperSegs []*AddressDivision
if doSections {
upperSegs = segments
lowerSegs = upperSegs
} else {
lowerSegs = lowerSegments
if upperSegments == nil {
upperSegs = lowerSegments
} else {
upperSegs = upperSegments
}
}
isPrefixSub = isPrefixSubnet(
func(index int) SegInt { return lowerSegs[index].ToSegmentBase().GetSegmentValue() },
func(index int) SegInt { return upperSegs[index].ToSegmentBase().GetUpperSegmentValue() },
len(lowerSegs),
IPv4BytesPerSegment,
IPv4BitsPerSegment,
IPv4MaxValuePerSegment,
prefixLength.bitCount(),
zerosOnly)
if isPrefixSub {
if lowerSegments == nil {
//allocate lower segments from address segments
lowerSegments = allocateSegments(lowerSegments, segments, IPv4SegmentCount, IPv4SegmentCount)
}
if upperSegments == nil {
//allocate upper segments from lower segments
upperSegments = allocateSegments(upperSegments, lowerSegments, IPv4SegmentCount, IPv4SegmentCount)
}
}
}
if lowerSegments != nil {
boundaries.lowerSection = creator.createPrefixedSectionInternalSingle(lowerSegments, false, prefLength)
}
if upperSegments != nil {
section := creator.createPrefixedSectionInternal(upperSegments, false, prefLength)
if isPrefixSub {
section = section.ToPrefixBlock()
}
boundaries.upperSection = section.GetUpper()
}
}
return
}
// getProviderMask is needed for parsed addresses which have associated masks.
func (parseData *parsedIPAddress) getProviderMask() *IPAddress {
return parseData.getQualifier().getMaskLower()
}
func (parseData *parsedIPAddress) getProviderNetworkPrefixLen() PrefixLen {
return parseData.getQualifier().getEquivalentPrefixLen()
}
// skipContains skips contains checking for addresses already parsed -
// so this is not a case of unusual string formatting, because this is not for comparing strings,
// but more a case of whether the parsing data structures are easy to use or not
func (parseData *parsedIPAddress) skipContains() bool {
segmentCount := parseData.getAddressParseData().getSegmentCount()
// first we must excluded cases where the segments line up differently than standard, although we do not exclude ipv6 compressed
if parseData.isProvidingIPv4() {
if segmentCount != IPv4SegmentCount { // accounts for isInetAtonJoined, singleSegment and wildcard segments
return true
}
} else {
if parseData.isProvidingMixedIPv6() || (segmentCount != IPv6SegmentCount && !parseData.isCompressed()) { // accounts for single segment and wildcard segments
return true
}
}
// exclude non-standard masks which will modify segment values from their parsed values
mask := parseData.getProviderMask()
if mask != nil && mask.GetBlockMaskPrefixLen(true) == nil { // handles non-standard masks
return true
}
return false
}
func (parseData *parsedIPAddress) containsProv(other *parsedIPAddress, networkOnly, equals bool) (res boolSetting) {
pd := parseData.getAddressParseData()
otherParseData := other.getAddressParseData()
otherSegmentData := otherParseData.getSegmentData() // grab this field for thread safety, other threads can make it disappear
segmentData := pd.getSegmentData() // grab this field for thread safety, other threads can make it disappear
if segmentData == nil || otherSegmentData == nil {
return
} else if parseData.skipContains() || other.skipContains() { // this excludes mixed addresses, amongst others
return
}
ipVersion := parseData.getProviderIPVersion()
if ipVersion != other.getProviderIPVersion() {
return boolSetting{true, false}
}
var max SegInt
var bitsPerSegment BitCount
var expectedSegCount, bytesPerSegment int
var compressedAlready, otherCompressedAlready bool
otherSegmentCount := otherParseData.getSegmentCount()
segmentCount := pd.getSegmentCount()
if parseData.isProvidingIPv4() {
max = IPv4MaxValuePerSegment
expectedSegCount = IPv4SegmentCount
bitsPerSegment = IPv4BitsPerSegment
bytesPerSegment = IPv4BytesPerSegment
compressedAlready = true
otherCompressedAlready = true
} else {
max = IPv6MaxValuePerSegment
expectedSegCount = IPv6SegmentCount
bitsPerSegment = IPv6BitsPerSegment
bytesPerSegment = IPv6BytesPerSegment
compressedAlready = expectedSegCount == segmentCount
otherCompressedAlready = expectedSegCount == otherSegmentCount
}
var networkSegIndex, hostSegIndex, endIndex, otherHostAllSegIndex, hostAllSegIndex int
otherPref := other.getProviderNetworkPrefixLen()
pref := parseData.getProviderNetworkPrefixLen()
endIndex = segmentCount
// determine what indexes to use for network, host, and prefix block adjustments (hostAllSegIndex and otherHostAllSegIndex)
var adjustedOtherPref PrefixLen
if pref == nil {
networkOnly = false
hostAllSegIndex = expectedSegCount
otherHostAllSegIndex = expectedSegCount
hostSegIndex = expectedSegCount
networkSegIndex = hostSegIndex - 1
} else {
prefLen := pref.bitCount()
if networkOnly {
hostSegIndex = getHostSegmentIndex(prefLen, bytesPerSegment, bitsPerSegment)
hostAllSegIndex = hostSegIndex
otherHostAllSegIndex = hostSegIndex
networkSegIndex = getNetworkSegmentIndex(prefLen, bytesPerSegment, bitsPerSegment)
// we treat the other as if it were a prefix block of the same prefix length
// this allows us to compare entire segments for prefixEquals, ignoring the host values
adjustedOtherPref = pref
} else {
otherHostAllSegIndex = expectedSegCount
hostSegIndex = getHostSegmentIndex(prefLen, bytesPerSegment, bitsPerSegment)
networkSegIndex = getNetworkSegmentIndex(prefLen, bytesPerSegment, bitsPerSegment)
if parseData.isPrefixSubnet(prefLen) {
hostAllSegIndex = hostSegIndex
if !equals {
// no need to look at host for containment when a prefix subnet
networkOnly = true
}
} else {
hostAllSegIndex = expectedSegCount
}
}
}
// Now determine if the other is a prefix block subnet, and if so, adjust otherHostAllSegIndex
if otherPref != nil {
otherPrefLen := otherPref.bitCount()
if adjustedOtherPref == nil || otherPrefLen < adjustedOtherPref.bitCount() {
otherHostIndex := getHostSegmentIndex(otherPrefLen, bytesPerSegment, bitsPerSegment)
if otherHostIndex < otherHostAllSegIndex &&
other.isPrefixSubnet(otherPrefLen) {
otherHostAllSegIndex = otherHostIndex
}
} else {
otherPref = adjustedOtherPref
}
} else {
otherPref = adjustedOtherPref
}
var compressedCount, otherCompressedCount int
i, j, normalizedCount := 0, 0, 0
for i < endIndex || compressedCount > 0 {
if networkOnly && normalizedCount > networkSegIndex {
break
}
var lower, upper SegInt
if compressedCount <= 0 {
lower = SegInt(parseData.getValue(i, keyLower))
upper = SegInt(parseData.getValue(i, keyUpper))
}
if normalizedCount >= hostAllSegIndex { // we've reached the prefixed segment
segPrefLength := getSegmentPrefixLength(bitsPerSegment, pref, normalizedCount)
segPref := segPrefLength.bitCount()
networkMask := ^SegInt(0) << uint(bitsPerSegment-segPref)
hostMask := ^networkMask
lower &= networkMask
upper |= hostMask
}
var otherLower, otherUpper SegInt
if normalizedCount > otherHostAllSegIndex {
otherLower = 0
otherUpper = max
} else {
if otherCompressedCount <= 0 {
otherLower = SegInt(otherParseData.getValue(j, keyLower))
otherUpper = SegInt(otherParseData.getValue(j, keyUpper))
}
if normalizedCount == otherHostAllSegIndex { // we've reached the prefixed segment
segPrefLength := getSegmentPrefixLength(bitsPerSegment, otherPref, normalizedCount)
segPref := segPrefLength.bitCount()
networkMask := ^SegInt(0) << uint(bitsPerSegment-segPref)
hostMask := ^networkMask
otherLower &= networkMask
otherUpper |= hostMask
}
}
if equals {
if lower != otherLower || upper != otherUpper {
return boolSetting{true, false}
}
} else {
if lower > otherLower || upper < otherUpper {
return boolSetting{true, false}
}
}
if !compressedAlready {
if compressedCount > 0 {
compressedCount--
if compressedCount == 0 {
compressedAlready = true
}
} else if parseData.segmentIsCompressed(i) {
i++
compressedCount = expectedSegCount - segmentCount
} else {
i++
}
} else {
i++
}
if !otherCompressedAlready {
if otherCompressedCount > 0 {
otherCompressedCount--
if otherCompressedCount == 0 {
otherCompressedAlready = true
}
} else if other.segmentIsCompressed(j) {
j++
otherCompressedCount = expectedSegCount - otherSegmentCount
} else {
j++
}
} else {
j++
}
normalizedCount++
}
return boolSetting{true, true}
}
func (parseData *parsedIPAddress) createIPv6Sections(doSections, doRangeBoundaries, withUpper bool) (sections sectionResult, boundaries boundaryResult) {
qualifier := parseData.getQualifier()
prefLen := getPrefixLength(qualifier)
mask := parseData.getProviderMask()
if mask != nil && mask.GetBlockMaskPrefixLen(true) != nil {
mask = nil // don't do any masking if the mask is a subnet mask, instead just map it to the corresponding prefix length
}
var segIsMult bool
isMultiple := false
hasMask := mask != nil
isHostMultiple := false
addressParseData := parseData.getAddressParseData()
segmentCount := addressParseData.getSegmentCount()
if hasMask && parseData.maskers == nil {
parseData.maskers = make([]Masker, segmentCount)
}
var hostSegments, segments, lowerSegments, upperSegments []*AddressDivision
creator := ipv6Type.getIPNetwork().getIPAddressCreator()
ipv6SegmentCount := IPv6SegmentCount
if doSections {
segments = createSegmentArray(IPv6SegmentCount)
} else if doRangeBoundaries {
lowerSegments = createSegmentArray(IPv6SegmentCount)
} else {
return
}
mixed := parseData.isProvidingMixedIPv6()
var missingSegmentCount int
normalizedSegmentIndex := 0
if mixed {
missingSegmentCount = IPv6MixedOriginalSegmentCount
} else {
missingSegmentCount = IPv6SegmentCount
}
missingSegmentCount -= segmentCount
expandedSegments := missingSegmentCount <= 0
expandedStart, expandedEnd := -1, -1
addressString := parseData.str
maskedIsDifferent := false
// get the segments for IPv6
for i := 0; i < segmentCount; i++ {
lower := addressParseData.getValue(i, keyLower)
upper := addressParseData.getValue(i, keyUpper)
if !expandedSegments {
isLastSegment := i == segmentCount-1
isWildcard := addressParseData.isWildcard(i)
isCompressed := parseData.segmentIsCompressed(i)
// figure out if this segment should be expanded
expandedSegments = isLastSegment || isCompressed
if !expandedSegments {
// check if we are wildcard and no other wildcard or compressed segment further down
expandedSegments = isWildcard
if expandedSegments {
for j := i + 1; j < segmentCount; j++ {
if addressParseData.isWildcard(j) || parseData.segmentIsCompressed(j) {
expandedSegments = false
break
}
}
}
}
if expandedSegments {
var lowerHighBytes, upperHighBytes uint64
hostIsRange := false
if !isCompressed {
if isWildcard {
if missingSegmentCount > 3 {
upperHighBytes = 0xffffffffffffffff >> uint((7-missingSegmentCount)<<4)
upper = 0xffffffffffffffff
} else {
upperHighBytes = 0
upper = 0xffffffffffffffff >> uint((3-missingSegmentCount)<<4)
}
lower = 0
hostIsRange = true
} else {
if missingSegmentCount > 3 {
lowerHighBytes = addressParseData.getValue(i, keyExtendedLower) // the high half of the lower value
upperHighBytes = addressParseData.getValue(i, keyExtendedUpper) // the high half of the upper value
hostIsRange = (lower != upper) || (lowerHighBytes != upperHighBytes)
} else {
hostIsRange = lower != upper
}
expandedStart = i
expandedEnd = i + missingSegmentCount
}
}
bits := BitCount(missingSegmentCount+1) << ipv6BitsToSegmentBitshift
var maskedLower, maskedUpper, maskedLowerHighBytes, maskedUpperHighBytes uint64
maskedIsRange := false
if hasMask {
// line up the mask segments into two longs
if isCompressed {
parseData.maskers[i] = defaultMasker
} else {
bitsPerSegment := IPv6BitsPerSegment
var maskVal uint64 = 0
if missingSegmentCount >= 4 {
cachedMasker := parseData.maskers[i]
var extendedMaskVal uint64
extendedCount := missingSegmentCount - 3
for k := 0; k < extendedCount; k++ {
extendedMaskVal = (extendedMaskVal << uint(bitsPerSegment)) | mask.GetSegment(normalizedSegmentIndex+k).getDivisionValue()
}
for k := extendedCount; k <= missingSegmentCount; k++ {
maskVal = (maskVal << uint(bitsPerSegment)) | mask.GetSegment(normalizedSegmentIndex+k).getDivisionValue()
}
if cachedMasker == nil {
// shift must be 6 bits at most for this shift to work per the java spec (so it must be less than 2^6 = 64)
extendedMaxValue := ^(^DivInt(0) << uint(bits-DivIntSize))
cachedMasker = MaskExtendedRange(
lower, lowerHighBytes,
upper, upperHighBytes,
maskVal, extendedMaskVal,
0xffffffffffffffff, extendedMaxValue)
parseData.maskers[i] = cachedMasker
}
if !cachedMasker.IsSequential() && sections.maskError == nil {
sections.maskError = &incompatibleAddressError{
addressError: addressError{
str: addressString,
key: "ipaddress.error.maskMismatch",
},
}
}
masker := cachedMasker.(ExtendedMasker)
maskedLowerHighBytes = masker.GetExtendedMaskedLower(lowerHighBytes, extendedMaskVal)
maskedUpperHighBytes = masker.GetExtendedMaskedUpper(upperHighBytes, extendedMaskVal)
maskedLower = masker.GetMaskedLower(lower, maskVal)
maskedUpper = masker.GetMaskedUpper(upper, maskVal)
maskedIsRange = (maskedLower != maskedUpper) || (maskedLowerHighBytes != maskedUpperHighBytes)
maskedIsDifferent = maskedIsDifferent || maskedLower != lower || maskedUpper != upper || maskedLowerHighBytes != lowerHighBytes || maskedUpperHighBytes != upperHighBytes
} else {
masker := parseData.maskers[i]
for k := 0; k <= missingSegmentCount; k++ {
maskVal = (maskVal << uint(bitsPerSegment)) | mask.GetSegment(normalizedSegmentIndex+k).getDivisionValue()
}
if masker == nil {
// shift must be 6 bits at most for this shift to work per the java spec (so it must be less than 2^6 = 64)
maxValue := ^(^DivInt(0) << uint(bits))
masker = MaskRange(lower, upper, maskVal, maxValue)
parseData.maskers[i] = masker
}
if !masker.IsSequential() && sections.maskError == nil {
sections.maskError = &incompatibleAddressError{
addressError: addressError{
str: maskString(lower, upper, maskVal),
key: "ipaddress.error.maskMismatch",
},
}
}
maskedLower = masker.GetMaskedLower(lower, maskVal)
maskedUpper = masker.GetMaskedUpper(upper, maskVal)
maskedIsRange = maskedLower != maskedUpper
maskedIsDifferent = maskedIsDifferent || maskedLower != lower || maskedUpper != upper
}
}
} else {
maskedLowerHighBytes = lowerHighBytes
maskedUpperHighBytes = upperHighBytes
maskedLower = lower
maskedUpper = upper
maskedIsRange = hostIsRange
}
shift := bits
count := missingSegmentCount
for count >= 0 { // add the missing segments
currentPrefix := getSegmentPrefixLength(IPv6BitsPerSegment, prefLen, normalizedSegmentIndex)
var hostSegLower, hostSegUpper, maskedSegLower, maskedSegUpper uint64
if !isCompressed {
shift -= IPv6BitsPerSegment
if count >= 4 {
shorterShift := shift - (IPv6BitsPerSegment << 2)
hostSegLower = (lowerHighBytes >> uint(shorterShift)) & IPv6MaxValuePerSegment
if hostIsRange {
hostSegUpper = (upperHighBytes >> uint(shorterShift)) & IPv6MaxValuePerSegment
} else {
hostSegUpper = hostSegLower
}
if hasMask {
maskedSegLower = (maskedLowerHighBytes >> uint(shorterShift)) & IPv6MaxValuePerSegment
if maskedIsRange {
maskedSegUpper = (maskedUpperHighBytes >> uint(shorterShift)) & IPv6MaxValuePerSegment
} else {
maskedSegUpper = maskedSegLower
}
} else {
maskedSegLower = hostSegLower
maskedSegUpper = hostSegUpper
}
} else {
hostSegLower = (lower >> uint(shift)) & IPv6MaxValuePerSegment
if hostIsRange {
hostSegUpper = (upper >> uint(shift)) & IPv6MaxValuePerSegment
} else {
hostSegUpper = hostSegLower
}
if hasMask {
maskedSegLower = (maskedLower >> uint(shift)) & IPv6MaxValuePerSegment
if maskedIsRange {
maskedSegUpper = (maskedUpper >> uint(shift)) & IPv6MaxValuePerSegment
} else {
maskedSegUpper = maskedSegLower
}
} else {
maskedSegLower = hostSegLower
maskedSegUpper = hostSegUpper
}
}
}
if doSections {
if maskedIsDifferent || currentPrefix != nil {
hostSegments = allocateSegments(hostSegments, segments, ipv6SegmentCount, normalizedSegmentIndex)
hostSegments[normalizedSegmentIndex], segIsMult = parseData.createSegment(