-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_mac_address.go
291 lines (276 loc) · 9.4 KB
/
parse_mac_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
package goip
import (
"sync"
"unsafe"
"github.com/pchchv/goip/address_error"
"github.com/pchchv/goip/address_string_param"
)
type parsedMACAddress struct {
macAddressParseData
originator *MACAddressString
address *MACAddress
params address_string_param.MACAddressStringParams
creationLock *sync.Mutex
}
func (provider *parsedMACAddress) getParameters() address_string_param.MACAddressStringParams {
return provider.params
}
func (parseData *parsedMACAddress) getMACAddressParseData() *macAddressParseData {
return &parseData.macAddressParseData
}
func (parseData *parsedMACAddress) createSection() (*MACAddressSection, address_error.IncompatibleAddressError) {
var segIsMult bool
var finalSegmentCount, initialSegmentCount int
addressString := parseData.str
addressParseData := parseData.getAddressParseData()
actualInitialSegmentCount := addressParseData.getSegmentCount()
creator := macType.getNetwork().getAddressCreator()
isMultiple := false
format := parseData.getFormat()
if format == nil {
if parseData.isExtended() {
initialSegmentCount = ExtendedUniqueIdentifier64SegmentCount
} else {
initialSegmentCount = MediaAccessControlSegmentCount
}
finalSegmentCount = initialSegmentCount
} else if format == dotted {
if parseData.isExtended() {
initialSegmentCount = MediaAccessControlDotted64SegmentCount
} else {
initialSegmentCount = MediaAccessControlDottedSegmentCount
}
if actualInitialSegmentCount <= MediaAccessControlDottedSegmentCount && !parseData.isExtended() {
finalSegmentCount = MediaAccessControlSegmentCount
} else {
finalSegmentCount = ExtendedUniqueIdentifier64SegmentCount
}
} else {
if addressParseData.isSingleSegment() || parseData.isDoubleSegment() {
if parseData.isExtended() {
finalSegmentCount = ExtendedUniqueIdentifier64SegmentCount
} else {
finalSegmentCount = MediaAccessControlSegmentCount
}
} else if actualInitialSegmentCount <= MediaAccessControlSegmentCount && !parseData.isExtended() {
finalSegmentCount = MediaAccessControlSegmentCount
} else {
finalSegmentCount = ExtendedUniqueIdentifier64SegmentCount
}
initialSegmentCount = finalSegmentCount
}
missingCount := initialSegmentCount - actualInitialSegmentCount
expandedSegments := missingCount <= 0
segments := make([]*AddressDivision, finalSegmentCount)
for i, normalizedSegmentIndex := 0, 0; i < actualInitialSegmentCount; i++ {
lower := addressParseData.getValue(i, keyLower)
upper := addressParseData.getValue(i, keyUpper)
if format == dotted { // aaa.bbb.ccc.ddd
// aabb is becoming aa.bb
segLower := SegInt(lower)
segUpper := SegInt(upper)
lowerHalfLower := segLower >> 8
lowerHalfUpper := segUpper >> 8
adjustedLower2 := segLower & 0xff
adjustedUpper2 := segUpper & 0xff
if lowerHalfLower != lowerHalfUpper && adjustedUpper2-adjustedLower2 != 0xff {
return nil, &incompatibleAddressError{addressError{str: addressString, key: "ipaddress.error.invalid.joined.ranges"}}
}
segments[normalizedSegmentIndex], segIsMult = createSegment(
addressString,
lowerHalfLower,
lowerHalfUpper,
false,
addressParseData,
i,
creator)
normalizedSegmentIndex++
isMultiple = isMultiple || segIsMult
segments[normalizedSegmentIndex], segIsMult = createSegment(
addressString,
adjustedLower2,
adjustedUpper2,
false,
addressParseData,
i,
creator)
isMultiple = isMultiple || segIsMult
} else {
if addressParseData.isSingleSegment() || parseData.isDoubleSegment() {
useStringIndicators := true
var count int
if i == actualInitialSegmentCount-1 {
count = missingCount
} else {
count = MACOrganizationalUniqueIdentifierSegmentCount - 1
}
missingCount -= count
isRange := lower != upper
previousAdjustedWasRange := false
for count >= 0 { //add the missing segments
var newLower, newUpper uint64
if isRange {
segmentMask := uint64(MACMaxValuePerSegment)
shift := uint64(count) << macBitsToSegmentBitshift
newLower = (lower >> shift) & segmentMask
newUpper = (upper >> shift) & segmentMask
if previousAdjustedWasRange && newUpper-newLower != MACMaxValuePerSegment {
// any range extending into upper segments must have full range in lower segments
// otherwise there is no way for us to represent the address
// so we need to check whether the lower parts cover the full range
// eg cannot represent 0.0.0x100-0x10f or 0.0.1-1ff, but can do 0.0.0x100-0x1ff or 0.0.0-1ff
return nil, &incompatibleAddressError{addressError{str: addressString, key: "ipaddress.error.invalid.joined.ranges"}}
}
previousAdjustedWasRange = newLower != newUpper
// we may be able to reuse our strings on the final segment
// for previous segments, strings can be reused only when the value is 0, which we do not need to cacheBitCountx. Any other value changes when shifted.
if count == 0 && newLower == lower {
if newUpper != upper {
addressParseData.unsetFlag(i, keyStandardRangeStr)
}
} else {
useStringIndicators = false
}
} else {
newLower = (lower >> uint(count<<3)) & MACMaxValuePerSegment
newUpper = newLower
if count != 0 || newLower != lower {
useStringIndicators = false
}
}
segments[normalizedSegmentIndex], segIsMult = createSegment(
addressString,
SegInt(newLower),
SegInt(newUpper),
useStringIndicators,
addressParseData,
i,
creator)
isMultiple = isMultiple || segIsMult
normalizedSegmentIndex++
count--
}
continue
} // end joined segments
segments[normalizedSegmentIndex], segIsMult = createSegment(
addressString,
SegInt(lower),
SegInt(upper),
true,
addressParseData,
i,
creator)
isMultiple = isMultiple || segIsMult
}
if !expandedSegments {
// check for any missing segments that we should account for here
if addressParseData.isWildcard(i) {
expandSegments := true
for j := i + 1; j < actualInitialSegmentCount; j++ {
if addressParseData.isWildcard(j) { //another wildcard further down
expandSegments = false
break
}
}
if expandSegments {
expandedSegments = true
count := missingCount
for ; count > 0; count-- { // add the missing segments
if format == dotted {
seg, _ := createSegment(
addressString,
0,
MACMaxValuePerSegment,
false,
addressParseData,
i,
creator)
normalizedSegmentIndex++
segments[normalizedSegmentIndex] = seg
normalizedSegmentIndex++
segments[normalizedSegmentIndex] = seg
} else {
normalizedSegmentIndex++
segments[normalizedSegmentIndex], _ = createSegment(
addressString,
0,
MACMaxValuePerSegment,
false,
addressParseData,
i,
creator)
}
isMultiple = true
}
}
}
}
normalizedSegmentIndex++
}
return creator.createSectionInternal(segments, isMultiple).ToMAC(), nil
}
func (parseData *parsedMACAddress) createAddress() (*MACAddress, address_error.IncompatibleAddressError) {
creator := macType.getNetwork().getAddressCreator()
sect, err := parseData.createSection()
if err != nil {
return nil, err
}
return creator.createAddressInternal(sect.ToSectionBase(), parseData.originator).ToMAC(), nil
}
func (parseData *parsedMACAddress) getAddress() (*MACAddress, address_error.IncompatibleAddressError) {
var err address_error.IncompatibleAddressError
addr := (*MACAddress)(atomicLoadPointer((*unsafe.Pointer)(unsafe.Pointer(&parseData.address))))
if addr == nil {
parseData.creationLock.Lock()
addr = parseData.address
if addr == nil {
addr, err = parseData.createAddress()
if err == nil {
parseData.segmentData = nil // no longer needed
dataLoc := (*unsafe.Pointer)(unsafe.Pointer(&parseData.address))
atomicStorePointer(dataLoc, unsafe.Pointer(addr))
}
}
parseData.creationLock.Unlock()
}
return addr, err
}
func createRangeSegment(addressString string, lower, upper SegInt, useFlags bool, parseData *addressParseData, parsedSegIndex int, creator parsedAddressCreator) *AddressDivision {
var result *AddressDivision
if !useFlags {
result = creator.createSegment(lower, upper, nil)
} else {
result = creator.createRangeSegmentInternal(
lower,
upper,
nil,
addressString,
lower,
upper,
parseData.getFlag(parsedSegIndex, keyStandardStr),
parseData.getFlag(parsedSegIndex, keyStandardRangeStr),
parseData.getIndex(parsedSegIndex, keyLowerStrStartIndex),
parseData.getIndex(parsedSegIndex, keyLowerStrEndIndex),
parseData.getIndex(parsedSegIndex, keyUpperStrEndIndex))
}
return result
}
func createSegment(addressString string, val, upperVal SegInt, useFlags bool, parseData *addressParseData, parsedSegIndex int, creator parsedAddressCreator) (div *AddressDivision, isMultiple bool) {
if val != upperVal {
return createRangeSegment(addressString, val, upperVal, useFlags, parseData, parsedSegIndex, creator), true
}
var result *AddressDivision
if !useFlags {
result = creator.createSegment(val, val, nil)
} else {
result = creator.createSegmentInternal(
val,
nil, //prefix length
addressString,
val,
parseData.getFlag(parsedSegIndex, keyStandardStr),
parseData.getIndex(parsedSegIndex, keyLowerStrStartIndex),
parseData.getIndex(parsedSegIndex, keyLowerStrEndIndex))
}
return result, false
}