Repository navigation
Expand file tree
/
Copy pathMACAddress.java
More file actions
502 lines (428 loc) · 15.2 KB
/
Copy pathMACAddress.java
File metadata and controls
502 lines (428 loc) · 15.2 KB
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
/*
* Copyright 2017 Sean C Foley
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* or at
* https://github.com/seancfoley/IPAddress/blob/master/LICENSE
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package inet.ipaddr.mac;
import java.util.Iterator;
import inet.ipaddr.Address;
import inet.ipaddr.AddressTypeException;
import inet.ipaddr.HostIdentifierString;
import inet.ipaddr.MACAddressString;
import inet.ipaddr.format.AddressDivisionGrouping;
import inet.ipaddr.format.AddressDivisionGrouping.StringOptions;
import inet.ipaddr.ipv6.IPv6Address;
import inet.ipaddr.ipv6.IPv6AddressSection;
import inet.ipaddr.mac.MACAddressNetwork.MACAddressCreator;
import inet.ipaddr.mac.MACAddressSection.AddressCache;
/**
* @custom.core
* @author sfoley
*
*/
public class MACAddress extends Address implements Iterable<MACAddress> {
private static final long serialVersionUID = 3L;
public static final char COLON_SEGMENT_SEPARATOR = ':';
public static final char DASH_SEGMENT_SEPARATOR = '-';
public static final char SPACE_SEGMENT_SEPARATOR = ' ';
public static final char DOTTED_SEGMENT_SEPARATOR = '.';
public static final char DASHED_SEGMENT_RANGE_SEPARATOR = '|';
public static final String DASHED_SEGMENT_RANGE_SEPARATOR_STR = String.valueOf(DASHED_SEGMENT_RANGE_SEPARATOR);
public static final int BITS_PER_SEGMENT = 8;
public static final int BYTES_PER_SEGMENT = 1;
public static final int MEDIA_ACCESS_CONTROL_SEGMENT_COUNT = 6;
public static final int MEDIA_ACCESS_CONTROL_DOTTED_SEGMENT_COUNT = 3;
public static final int MEDIA_ACCESS_CONTROL_DOTTED_64_SEGMENT_COUNT = 4;
public static final int MEDIA_ACCESS_CONTROL_DOTTED_BITS_PER_SEGMENT = 16;
public static final int MEDIA_ACCESS_CONTROL_SINGLE_DASHED_SEGMENT_COUNT = 2;
public static final int EXTENDED_UNIQUE_IDENTIFIER_48_SEGMENT_COUNT = MEDIA_ACCESS_CONTROL_SEGMENT_COUNT;
public static final int EXTENDED_UNIQUE_IDENTIFIER_64_SEGMENT_COUNT = 8;
public static final int DEFAULT_TEXTUAL_RADIX = 16;
public static final int MAX_VALUE_PER_SEGMENT = 0xff;
public static final int MAX_VALUE_PER_DOTTED_SEGMENT = 0xffff;
public static final int ORGANIZATIONAL_UNIQUE_IDENTIFIER_SEGMENT_COUNT = 3;
public static final int ORGANIZATIONAL_UNIQUE_IDENTIFIER_BIT_COUNT = ORGANIZATIONAL_UNIQUE_IDENTIFIER_SEGMENT_COUNT * BITS_PER_SEGMENT;
protected static MACAddressNetwork network = new MACAddressNetwork();
transient AddressCache sectionCache;
/**
* Constructs a MAC address.
* @param segments the address segments
*/
public MACAddress(MACAddressSegment[] segments) {
this(segments, null);
}
/**
* Constructs a MAC address.
* @param segments the address segments
*/
public MACAddress(MACAddressSegment[] segments, Integer prefixLength) {
this(getAddressCreator().createSection(segments, segments.length == EXTENDED_UNIQUE_IDENTIFIER_64_SEGMENT_COUNT, prefixLength));
}
/**
* Constructs a MAC address.
* @param section the address segments
*/
public MACAddress(MACAddressSection section) {
super(section);
int segCount = section.getSegmentCount();
if(segCount != MEDIA_ACCESS_CONTROL_SEGMENT_COUNT && segCount != EXTENDED_UNIQUE_IDENTIFIER_64_SEGMENT_COUNT) {
throw new IllegalArgumentException(getMessage("ipaddress.error.mac.invalid.segment.count") + ' ' + segCount);
}
}
/**
* Constructs a MAC address.
*/
public MACAddress(long address) {
this(address, false, null);
}
/**
* Constructs a MAC address.
*/
public MACAddress(long address, boolean extended) {
this(address, extended, null);
}
/**
* Constructs a MAC address.
*/
public MACAddress(long address, Integer prefixLength) {
this(address, false, prefixLength);
}
/**
* Constructs a MAC address.
*
* @param address the bytes
* @param prefixLength the length for which the address represents all addresses with the same prefix identifier such as the 24 bit Organizational Unique Identifier (OUI)
* @param extended if true, treated as an 8-byte EUI-64 address, otherwise treated as a 6-byte MAC or EUI-48
*/
public MACAddress(long address, boolean extended, Integer prefixLength) {
super(getAddressCreator().createSection(address, 0, extended, prefixLength));
}
/**
* Constructs a MAC address.
*/
public MACAddress(byte[] bytes) {
this(bytes, null);
}
/**
* Constructs a MAC address.
*/
public MACAddress(byte[] bytes, Integer prefixLength) {
super(getAddressCreator().createSection(bytes, 0, bytes.length == EXTENDED_UNIQUE_IDENTIFIER_64_SEGMENT_COUNT, prefixLength));
if(bytes.length != MEDIA_ACCESS_CONTROL_SEGMENT_COUNT && bytes.length != EXTENDED_UNIQUE_IDENTIFIER_64_SEGMENT_COUNT) {
throw new IllegalArgumentException(getMessage("ipaddress.error.mac.invalid.byte.count") + ' ' + bytes.length);
}
}
/**
* Constructs a MAC address
*
* @param lowerValueProvider supplies the 1 byte lower values for each segment
* @param upperValueProvider supplies the 1 byte upper values for each segment
*/
public MACAddress(SegmentValueProvider lowerValueProvider, SegmentValueProvider upperValueProvider, boolean extended, Integer prefixLength) {
super(getAddressCreator().createSection(lowerValueProvider, upperValueProvider, 0, extended, prefixLength));
}
/**
* Constructs a MAC address
*
* @param lowerValueProvider supplies the 1 byte lower values for each segment
* @param upperValueProvider supplies the 1 byte upper values for each segment
*/
public MACAddress(SegmentValueProvider lowerValueProvider, SegmentValueProvider upperValueProvider, Integer prefixLength) {
super(getAddressCreator().createSection(lowerValueProvider, upperValueProvider, 0, false, prefixLength));
}
/**
* Constructs a MAC address
*
* @param lowerValueProvider supplies the 1 byte lower values for each segment
* @param upperValueProvider supplies the 1 byte upper values for each segment
*/
public MACAddress(SegmentValueProvider lowerValueProvider, SegmentValueProvider upperValueProvider, boolean extended) {
super(getAddressCreator().createSection(lowerValueProvider, upperValueProvider, 0, extended, null));
}
/**
* Constructs a MAC address
*
* @param lowerValueProvider supplies the 1 byte lower values for each segment
* @param upperValueProvider supplies the 1 byte upper values for each segment
*/
public MACAddress(SegmentValueProvider lowerValueProvider, SegmentValueProvider upperValueProvider) {
this(lowerValueProvider, upperValueProvider, null);
}
protected static String getMessage(String key) {
return Address.getMessage(key);
}
public static MACAddressNetwork network() {
return network;
}
public static MACAddressCreator getAddressCreator() {
return network().getAddressCreator();
}
public boolean isExtended() {
return getSection().isExtended();
}
public boolean isAllAddresses() {
return getSection().isFullRange();
}
@Override
public MACAddressSection getSection() {
return (MACAddressSection) super.getSection();
}
@Override
public MACAddressSegment getSegment(int index) {
return (MACAddressSegment) super.getSegment(index);
}
@Override
public MACAddressSegment[] getSegments() {
return getSection().getSegments();
}
public static int maxSegmentValue() {
return MAX_VALUE_PER_SEGMENT;
}
@Override
public int getMaxSegmentValue() {
return MAX_VALUE_PER_SEGMENT;
}
@Override
public int getByteCount() {
return getSection().getByteCount();
}
@Override
public int getBitCount() {
return getSection().getBitCount();
}
@Override
public int getBytesPerSegment() {
return BYTES_PER_SEGMENT;
}
@Override
public int getBitsPerSegment() {
return BITS_PER_SEGMENT;
}
@Override
protected boolean isFromSameString(HostIdentifierString other) {
if(fromString != null && other instanceof MACAddressString) {
MACAddressString fromString = (MACAddressString) this.fromString;
MACAddressString otherString = (MACAddressString) other;
return (fromString == otherString ||
(fromString.toString().equals(otherString.toString())) &&
fromString.getValidationOptions().equals(otherString.getValidationOptions()));
}
return false;
}
@Override
public boolean contains(Address other) {
if(other instanceof MACAddress) {
return contains((MACAddress) other);
}
return false;
}
/**
*
* @param other
* @return whether this address contains the given address
*/
public boolean contains(MACAddress other) {
if(other == this) {
return true;
}
return getSection().contains(other.getSection());
}
@Override
public Iterable<MACAddress> getIterable() {
return this;
}
@Override
public Iterator<MACAddress> iterator() {
return getSection().iterator(this);
}
@Override
public Iterator<MACAddressSegment[]> segmentsIterator() {
return getSection().segmentsIterator();
}
@Override
public MACAddress getLower() {
return getLowestOrHighest(true);
}
@Override
public MACAddress getUpper() {
return getLowestOrHighest(false);
}
private MACAddress getLowestOrHighest(boolean lowest) {
return getSection().getLowestOrHighest(this, lowest);
}
/**
* Use to produce:
* "MSB format", "IBM format", "Token-Ring format", and "non-canonical form"
*
* See RFC 2469 section 2
*
* Also see https://en.wikipedia.org/wiki/MAC_address
*
* @return
*/
@Override
public MACAddress reverseBits(boolean perByte) {
return checkIdentity(getSection().reverseBits(perByte));
}
@Override
public MACAddress reverseBytes() {
return checkIdentity(getSection().reverseBytes());
}
@Override
public MACAddress reverseBytesPerSegment() {
return this;
}
@Override
public MACAddress reverseSegments() {
return checkIdentity(getSection().reverseSegments());
}
private MACAddress checkIdentity(MACAddressSection newSection) {
MACAddressSection section = getSection();
if(newSection == section) {
return this;
}
return getAddressCreator().createAddress(newSection);
}
@Override
public MACAddress removePrefixLength() {
return checkIdentity(getSection().removePrefixLength());
}
@Override
public MACAddress applyPrefixLength(int networkPrefixLength) {
return checkIdentity(getSection().applyPrefixLength(networkPrefixLength));
}
@Override
public MACAddress adjustPrefixBySegment(boolean nextSegment) {
return checkIdentity(getSection().adjustPrefixBySegment(nextSegment));
}
@Override
public MACAddress adjustPrefixLength(int adjustment) {
return checkIdentity(getSection().adjustPrefixLength(adjustment));
}
@Override
public MACAddress setPrefixLength(int prefixLength) {
return checkIdentity(getSection().setPrefixLength(prefixLength));
}
@Override
public MACAddressSection getSection(int index) {
return getSection().getSection(index);
}
@Override
public MACAddressSection getSection(int index, int endIndex) {
return getSection().getSection(index, endIndex);
}
public MACAddressSection getODISection() {
return getSection().getODISection();
}
public MACAddressSection getOUISection() {
return getSection().getOUISection();
}
public MACAddress toOUIPrefixed() {
return getAddressCreator().createAddress(getSection().toOUIPrefixed());
}
public IPv6Address toLinkLocalIPv6() {
return IPv6Address.network().getAddressCreator().createAddress(IPv6AddressSection.LINK_LOCAL_PREFIX.append(toEUI64IPv6()));
}
public IPv6AddressSection toEUI64IPv6() {
return IPv6Address.network().getAddressCreator().createSection(this);
}
/**
* Whether this section is consistent with an IPv6 EUI64 section,
* which means it came from an extended 8 byte address,
* and the corresponding segments in the middle match 0xff and 0xff/fe for MAC/not-MAC
*
* @param asMAC
* @return
*/
public boolean isEUI64(boolean asMAC) {
if(isExtended()) {//getSegmentCount() == EXTENDED_UNIQUE_IDENTIFIER_64_SEGMENT_COUNT
MACAddressSection section = getSection();
MACAddressSegment seg3 = section.getSegment(3);
MACAddressSegment seg4 = section.getSegment(4);
return seg3.matches(0xff) && seg4.matches(asMAC ? 0xff : 0xfe);
}
return false;
}
/**
* Convert to IPv6 EUI-64 section
*
* http://standards.ieee.org/develop/regauth/tut/eui64.pdf
*
* @param asMAC if true, this address is considered MAC and the EUI-64 is extended using ff-ff, otherwise this address is considered EUI-48 and extended using ff-fe
* Note that IPv6 treats MAC as EUI-48 and extends MAC to IPv6 addresses using ff-fe
* @return
*/
public MACAddress toEUI64(boolean asMAC) {
if(!isExtended()) {//getSegmentCount() == EXTENDED_UNIQUE_IDENTIFIER_48_SEGMENT_COUNT
MACAddressCreator creator = getAddressCreator();
MACAddressSegment segs[] = creator.createSegmentArray(EXTENDED_UNIQUE_IDENTIFIER_64_SEGMENT_COUNT);
MACAddressSection section = getSection();
section.getSegments(0, 3, segs, 0);
segs[3] = MACAddressSegment.FF_SEGMENT;
segs[4] = asMAC ? MACAddressSegment.FF_SEGMENT : MACAddressSegment.FE_SEGMENT;
section.getSegments(3, 6, segs, 5);
return creator.createAddressInternal(segs);
} else {
MACAddressSection section = getSection();
MACAddressSegment seg3 = section.getSegment(3);
MACAddressSegment seg4 = section.getSegment(4);
if(seg3.matches(0xff) && seg4.matches(asMAC ? 0xff : 0xfe)) {
return this;
}
}
throw new AddressTypeException(this, "ipaddress.mac.error.not.eui.convertible");
}
public AddressDivisionGrouping getDottedAddress() {
return getSection().getDottedGrouping();
}
void cache(HostIdentifierString string) {
if(fromString instanceof MACAddressString) {
fromString = string;
}
}
@Override
public MACAddressString toAddressString() {
if(fromString == null) {
fromString = new MACAddressString(this); /* address string creation */
}
return (MACAddressString) fromString;
}
public String toNormalizedString(StringOptions stringOptions) {
return getSection().toNormalizedString(stringOptions);
}
public String toDottedString() {
return getSection().toDottedString();
}
public String toDashedString() {
return toCanonicalString();
}
public String toColonDelimitedString() {
return toNormalizedString();
}
public String toSpaceDelimitedString() {
return getSection().toSpaceDelimitedString();
}
@Override
public boolean isMulticast() {
return getSegment(0).matchesWithMask(1, 0x1);
}
public boolean isUniversal() {
return getSegment(0).matchesWithMask(0, 0x2);
}
@Override
public boolean isLocal() {
return getSegment(0).matchesWithMask(1, 0x2);
}
}