Repository navigation
Expand file tree
/
Copy pathAddress.java
More file actions
567 lines (475 loc) · 15.5 KB
/
Copy pathAddress.java
File metadata and controls
567 lines (475 loc) · 15.5 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
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
/*
* 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;
import java.math.BigInteger;
import java.util.Iterator;
import java.util.function.Function;
import inet.ipaddr.AddressComparator.CountComparator;
import inet.ipaddr.format.AddressDivisionSeries;
import inet.ipaddr.ipv4.IPv4AddressNetwork;
import inet.ipaddr.ipv6.IPv6AddressNetwork;
import inet.ipaddr.mac.MACAddressNetwork;
/**
* @custom.core
* @author sfoley
*
*/
public abstract class Address implements AddressSegmentSeries, Comparable<Address> {
private static final long serialVersionUID = 4L;
/**
* @custom.core
* @author sfoley
*
*/
public static interface AddressProvider {
int getSegmentCount();
SegmentValueProvider getValues();
SegmentValueProvider getUpperValues();
Integer getPrefixLength();
String getZone();
}
/**
* @custom.core
* @author sfoley
*
*/
public static interface SegmentValueProvider {
int getValue(int segmentIndex);
}
public static final String HEX_PREFIX = "0x";
public static final String OCTAL_PREFIX = "0";
public static final char RANGE_SEPARATOR = '-';
public static final String RANGE_SEPARATOR_STR = String.valueOf(RANGE_SEPARATOR);
public static final char ALTERNATIVE_RANGE_SEPARATOR = '»';
public static final String ALTERNATIVE_RANGE_SEPARATOR_STR = String.valueOf(ALTERNATIVE_RANGE_SEPARATOR);
public static final char SEGMENT_WILDCARD = '*';
public static final String SEGMENT_WILDCARD_STR = String.valueOf(SEGMENT_WILDCARD);
public static final String ALTERNATIVE_SEGMENT_WILDCARD_STR = "¿";
public static final char SEGMENT_SQL_WILDCARD = '%';
public static final String SEGMENT_SQL_WILDCARD_STR = String.valueOf(SEGMENT_SQL_WILDCARD);
public static final char SEGMENT_SQL_SINGLE_WILDCARD = '_';
public static final String SEGMENT_SQL_SINGLE_WILDCARD_STR = String.valueOf(SEGMENT_SQL_SINGLE_WILDCARD);
public static final AddressComparator DEFAULT_ADDRESS_COMPARATOR = new CountComparator();
private static MACAddressNetwork macNetwork;
private static IPv6AddressNetwork ipv6Network;
private static IPv4AddressNetwork ipv4Network;
/* the segments. For IPv4, each element is actually just 1 byte and the array has 4 elements,
* while for IPv6, each element is 2 bytes and the array has 8 elements. */
final AddressSection addressSection;
/* an object encapsulating a string representing the address, which is the one used to construct the address if the address was constructed from a string */
protected HostIdentifierString fromString;
/**
* Constructs an address.
* @param section the address segments
*/
protected Address(AddressSection section) {
addressSection = section;
if(!getNetwork().equals(addressSection.getNetwork())) {
throw new NetworkMismatchException(addressSection);
}
}
protected Address(Function<Address, AddressSection> supplier) {
addressSection = supplier.apply(this);
if(!getNetwork().equals(addressSection.getNetwork())) {
throw new NetworkMismatchException(addressSection);
}
}
public static IPv6AddressNetwork defaultIpv6Network() {
if(ipv6Network == null) {
synchronized(Address.class) {
if(ipv6Network == null) {
ipv6Network = new IPv6AddressNetwork();
}
}
}
return ipv6Network;
}
public static IPv4AddressNetwork defaultIpv4Network() {
if(ipv4Network == null) {
synchronized(Address.class) {
if(ipv4Network == null) {
ipv4Network = new IPv4AddressNetwork();
}
}
}
return ipv4Network;
}
public static MACAddressNetwork defaultMACNetwork() {
if(macNetwork == null) {
synchronized(Address.class) {
if(macNetwork == null) {
macNetwork = new MACAddressNetwork();
}
}
}
return macNetwork;
}
protected static String getMessage(String key) {
return HostIdentifierException.getMessage(key);
}
@Override
public int getSegmentCount() {
return getSection().getSegmentCount();
}
@Override
public int getDivisionCount() {
return getSection().getDivisionCount();
}
@Override
public int getBitCount() {
return getSection().getBitCount();
}
@Override
public int getByteCount() {
return getSection().getByteCount();
}
@Override
public AddressSection getSection() {
return addressSection;
}
@Override
public void getSegments(AddressSegment segs[]) {
getSection().getSegments(segs);
}
@Override
public void getSegments(int start, int end, AddressSegment segs[], int index) {
getSection().getSegments(start, end, segs, index);
}
/**
* @return the maximum possible segment value for this type of address.
* Note this is not the maximum value of the segments in this specific address.
*/
public abstract int getMaxSegmentValue();
@Override
public abstract Iterable<? extends Address> getIterable();
@Override
public abstract Iterator<? extends Address> iterator();
@Override
public abstract Iterator<? extends Address> prefixBlockIterator();
@Override
public abstract Address increment(long increment);
@Override
public abstract Address incrementBoundary(long increment);
@Override
public abstract Address getLower();
@Override
public abstract Address getUpper();
/**
* @return whether this address represents more than one address.
* Such addresses include CIDR/IP addresses (eg 1.2.3.4/11) or wildcard addresses (eg 1.2.*.4) or range addresses (eg 1.2.3-4.5)
*/
@Override
public boolean isMultiple() {
return getSection().isMultiple();
}
/**
* @return whether this address has an associated prefix length
*/
@Override
public boolean isPrefixed() {
return getSection().isPrefixed();
}
/**
* the largest number of high bits for which this address represents all addresses with the same set of high bits
*/
@Override
public Integer getPrefixLength() {
return getSection().getPrefixLength();
}
/**
* Returns the smallest prefix length possible such that this includes the block of addresses for that prefix.
* <p>
* If the entire range can be dictated this way, then this method returns the same value as {@link #getPrefixLengthForSingleBlock()}.
* Otherwise, this method will return the minimal possible prefix that can be paired with this address, while {@link #getPrefixLengthForSingleBlock()} will return null.
*<p>
* In cases where the final bit in this address division series is constant, this returns the bit length of this address division series.
*
* @return the prefix length
*/
@Override
public int getMinPrefixLengthForBlock() {
return getSection().getMinPrefixLengthForBlock();
}
/**
* Returns a prefix length for which the range of this address subnet matches the the block of addresses for that prefix.
* <p>
* If the range can be dictated this way, then this method returns the same value as {@link #getMinPrefixLengthForBlock()}.
* <p>
* If no such prefix exists, returns null.
* <p>
* If this segment grouping represents a single value, returns the bit length of this address division series.
* <p>
* IP address examples:
* 1.2.3.4 returns 32
* 1.2.*.* returns 16
* 1.2.*.0/24 returns 16 in the case of PrefixConfiguration == ALL_PREFIXES_ARE_SUBNETS, 32 otherwise
* 1.2.*.4 returns null
* 1.2.252-255.* returns 22
* 1.2.3.4/x returns x in the case of PrefixConfiguration == ALL_PREFIXES_ARE_SUBNETS, 32 otherwise
* 1.2.0.0/16 returns 16 in the case of PrefixConfiguration == ALL_PREFIXES_ARE_SUBNETS or PREFIXED_ZERO_HOSTS_ARE_SUBNETS, 32 otherwise
*
* @return the prefix length or null if it does not exist
*/
@Override
public Integer getPrefixLengthForSingleBlock() {
return getSection().getPrefixLengthForSingleBlock();
}
/**
* Whether the MAC address or IP address or other form of address is multicast.
*
* @see java.net.InetAddress#isMulticastAddress()
*/
public abstract boolean isMulticast();
/**
* Gets the count of addresses that this address may represent.
*
* If this address is not a subnet block of multiple addresses or has no range of values, then there is only one such address.
*
* @return
*/
@Override
public BigInteger getCount() {
return getSection().getCount();
}
/**
* If this has a prefix length, the count of the range of values in the prefix.
*
* If this has no prefix, returns the same value as {@link #getCount()}
*
* @return
*/
@Override
public BigInteger getPrefixCount() {
return getSection().getPrefixCount();
}
@Override
public int isMore(AddressDivisionSeries other) {
return getSection().isMore(other);
}
@Override
public byte[] getBytes() {
return getSection().getBytes();
}
@Override
public byte[] getBytes(byte bytes[]) {
return getSection().getBytes(bytes);
}
@Override
public byte[] getBytes(byte bytes[], int index) {
return getSection().getBytes(bytes, index);
}
/**
* Gets the bytes for the highest address in the range of addresses represented by this address instance.
*
* @return
*/
@Override
public byte[] getUpperBytes() {
return getSection().getUpperBytes();
}
@Override
public byte[] getUpperBytes(byte bytes[]) {
return getSection().getUpperBytes(bytes);
}
@Override
public byte[] getUpperBytes(byte bytes[], int index) {
return getSection().getUpperBytes(bytes, index);
}
@Override
public BigInteger getValue() {
return getSection().getValue();
}
@Override
public BigInteger getUpperValue() {
return getSection().getUpperValue();
}
@Override
public boolean isZero() {
return getSection().isZero();
}
@Override
public boolean includesZero() {
return getSection().includesZero();
}
@Override
public boolean isMax() {
return getSection().isMax();
}
@Override
public boolean includesMax() {
return getSection().includesMax();
}
@Override
public boolean isFullRange() {
return getSection().isFullRange();
}
/**
* Whether the address can be considered a local address (as opposed to a global one)
* @return
*/
public abstract boolean isLocal();
@Override
public int hashCode() {
return getSection().hashCode();
}
@Override
public int compareTo(Address other) {
if(this == other) {
return 0;
}
return DEFAULT_ADDRESS_COMPARATOR.compare(this, other);
}
protected abstract boolean isFromSameString(HostIdentifierString otherString);
public boolean isSameAddress(Address other) {
return other == this || getSection().equals(other.getSection());
}
/**
* Two Address objects are equal if they represent the same set of addresses.
*/
@Override
public boolean equals(Object o) {
if(o == this) {
return true;
}
if(o instanceof Address) {
Address other = (Address) o;
if(isFromSameString(other.fromString)) {
return true;
}
return isSameAddress(other);
}
return false;
}
public abstract boolean contains(Address other);
/**
* Returns a host identifier string representation for this address,
* which will be already validated.
*
* @return
*/
public HostIdentifierString toAddressString() {
return fromString;
}
/**
* Writes this address as a single hexadecimal value with always the exact same number of characters, with or without a preceding 0x prefix.
*
* If this section represents a range of values outside of the network prefix length, then this is printed as a range of two hex values.
*/
@Override
public String toHexString(boolean with0xPrefix) {
return getSection().toHexString(with0xPrefix);
}
/**
* The normalized string returned by this method is a common and consistent representation of the address.
*
* The string returned by this method is unique for each address.
*/
@Override
public String toNormalizedString() {
return getSection().toNormalizedString();
}
/**
* This produces a canonical string.
*
* RFC 5952 describes canonical representations for Ipv6
* http://en.wikipedia.org/wiki/IPv6_address#Recommended_representation_as_text
* http://tools.ietf.org/html/rfc5952
*
* Each address has a unique canonical string, not counting the prefix, which can give two equal addresses different strings.
*/
@Override
public String toCanonicalString() {
return getSection().toCanonicalString();
}
/**
* Produce short strings for the address in the usual address format.
*
* Each address has a unique compressed string.
*
*/
@Override
public String toCompressedString() {
return getSection().toCompressedString();
}
@Override
public String toString() {
return toCanonicalString();
}
@Override
public String[] getDivisionStrings() {
return getSection().getDivisionStrings();
}
@Override
public String[] getSegmentStrings() {
return getSection().getSegmentStrings();
}
@Override
public abstract Address reverseSegments();
@Override
public abstract Address reverseBits(boolean perByte);
@Override
public abstract Address reverseBytes();
@Override
public abstract Address reverseBytesPerSegment();
/**
* Returns whether the address range has a prefix length and includes the block of values for its prefix length.
*/
@Override
public boolean isPrefixBlock() {
return getSection().isPrefixBlock();
}
@Override
public boolean containsPrefixBlock(int prefixLength) {
return getSection().containsPrefixBlock(prefixLength);
}
/**
* Returns whether the address range the block of values for a single prefix identified by its prefix length.
* This is similar to {@link #isPrefixBlock()} except that it returns false when
* the subnet has multiple prefixes.
*
* For instance, 1.*.*.* /16 return false for this method and returns true for {@link #isPrefixBlock()}
*/
@Override
public boolean isSinglePrefixBlock() {
return getSection().isSinglePrefixBlock();
}
@Override
public boolean containsSinglePrefixBlock(int prefixLength) {
return getSection().containsSinglePrefixBlock(prefixLength);
}
@Override
public abstract Address toPrefixBlock();
@Override
public abstract Address removePrefixLength();
@Override
public abstract Address removePrefixLength(boolean zeroed);
@Override
public abstract Address adjustPrefixBySegment(boolean nextSegment);
@Override
public abstract Address adjustPrefixBySegment(boolean nextSegment, boolean zeroed);
@Override
public abstract Address adjustPrefixLength(int adjustment);
@Override
public abstract Address adjustPrefixLength(int adjustment, boolean zeroed);
@Override
public abstract Address setPrefixLength(int prefixLength);
@Override
public abstract Address setPrefixLength(int prefixLength, boolean zeroed);
@Override
public abstract Address applyPrefixLength(int networkPrefixLength);
}