Repository navigation
Expand file tree
/
Copy pathIPAddress.java
More file actions
1258 lines (1120 loc) · 44.1 KB
/
Copy pathIPAddress.java
File metadata and controls
1258 lines (1120 loc) · 44.1 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
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
/*
* 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.io.Serializable;
import java.math.BigInteger;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.UnknownHostException;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.function.IntFunction;
import java.util.function.Supplier;
import inet.ipaddr.IPAddressComparator.CountComparator;
import inet.ipaddr.IPAddressConverter.DefaultAddressConverter;
import inet.ipaddr.IPAddressSection.IPStringBuilderOptions;
import inet.ipaddr.IPAddressSection.StringOptions;
import inet.ipaddr.IPAddressTypeNetwork.IPAddressCreator;
import inet.ipaddr.IPAddressTypeNetwork.IPAddressSegmentCreator;
import inet.ipaddr.format.IPAddressPart;
import inet.ipaddr.format.util.IPAddressPartStringCollection;
import inet.ipaddr.format.util.sql.IPAddressSQLTranslator;
import inet.ipaddr.format.validate.AddressProvider;
import inet.ipaddr.format.validate.ParsedHost;
import inet.ipaddr.ipv4.IPv4Address;
import inet.ipaddr.ipv4.IPv4AddressNetwork.IPv4AddressCreator;
import inet.ipaddr.ipv4.IPv4AddressSegment;
import inet.ipaddr.ipv6.IPv6Address;
import inet.ipaddr.ipv6.IPv6AddressNetwork.IPv6AddressCreator;
import inet.ipaddr.ipv6.IPv6AddressSegment;
/**
* A single IP address, or a subnet of multiple addresses. Subnets have one or more segments that are a range of values.
* <p>
* IPAddress objects are immutable and cannot change values. This also makes them thread-safe.
* <p>
*
* String creation:
* <p>
* There are several public classes used to customize IP address strings.
* For single strings from an address or address section, you use {@link StringOptions} or {@link inet.ipaddr.ipv6.IPv6AddressSection.IPv6StringOptions} along with {@link #toNormalizedString(IPAddressSection.StringOptions)}.
* Or you use one of the methods like {@link #toCanonicalString()} which does the same.
* <p>
* For string collections from an address or address section, use {@link inet.ipaddr.ipv4.IPv4AddressSection.IPv4StringBuilderOptions}, {@link inet.ipaddr.ipv6.IPv6AddressSection.IPv6StringBuilderOptions}, {@link IPStringBuilderOptions} along with {@link #toStringCollection(IPAddressSection.IPStringBuilderOptions)} or {@link #toStrings(IPAddressSection.IPStringBuilderOptions)}.
* Or you use one of the methods {@link #toStandardStringCollection()}, {@link #toAllStringCollection()}, {@link #toStandardStrings()}, {@link #toAllStrings()} which does the same.
* <p>
* @custom.core
* @author sfoley
*
*/
/*
* Internal details of how this works:
*
* 1. Building single strings steps:
* StringOptions, IPv6StringOptions provides options for a user to specify a single string to be produced for a given address or section of an address.
* When calling toNormalizedString, each is mapped to a single IPv4StringParams/IPv6StringParams/StringParams object used to construct the string.
* Each IPv4StringParams/IPv6StringParams/StringParams constructs a single string with its toString method
*
*
* 2. Building string collection steps:
* IPv4StringBuilderOptions, IPv6StringBuilderOptions, IPStringBuilderOptions provides options to create a set of multiple strings from a single IP Address or section of an address.
* toStringCollection constructs a IPv6StringBuilder/IPv4StringBuilder/IPAddressStringBuilder for that address section
* The builder translates the options to a series of IPv4StringParams/IPv6StringParams/StringParams in addAllVariations
* When the set is being created, it will use each IPv4StringParams/IPv6StringParams/StringParams object to construct each unique string, using their toString method
*
*
* Non-public classes:
* IPv6StringParams, IPv4StringParams and the base classes StringParams and IPAddressPartStringParams:
* Used by both single string creation or creating collections of strings, these are not public.
* IPv6StringBuilder/IPv4StringBuilder/IPAddressStringBuilder, used to create collections of strings, are not public either
*
*/
public abstract class IPAddress implements Comparable<IPAddress>, Serializable {
private static final long serialVersionUID = 1L;
/**
* @custom.core
* @author sfoley
*
*/
public enum IPVersion {
IPV4,
IPV6;
public boolean isIPv4() {
return this == IPV4;
}
public boolean isIPv6() {
return this == IPV6;
}
};
public static final char RANGE_SEPARATOR = '-';
public static final String RANGE_SEPARATOR_STR = String.valueOf(RANGE_SEPARATOR);
public static final char SEGMENT_WILDCARD = '*';
public static final String SEGMENT_WILDCARD_STR = String.valueOf(SEGMENT_WILDCARD);
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 char PREFIX_LEN_SEPARATOR = '/';
//The default way by which addresses are converted
public static final IPAddressConverter addressConverter = new DefaultAddressConverter();
//The default way by which addresses are compared
public static final IPAddressComparator addressComparator = new CountComparator();
/* 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 IPAddressSection addressSection;
/* an IPAddressString representing the address, which is the one used to construct the address if the address was constructed from a IPAddressString */
protected IPAddressString fromString;
/* a Host representing the address, which is the one used to construct the address if the address was resolved from a Host.
* Note this is different than if the Host was an address itself, in which case the Host holds a reference to the address
* but there is no backwards reference to the Host.
*/
HostName fromHost;
/* a Host representing the canonical host for this address */
private HostName canonicalHost;
/* the associated InetAddress */
protected transient InetAddress inetAddress;
/**
* Represents an IP address or a set of addresses.
* @param section the address segments
*/
protected IPAddress(IPAddressSection section) {
this.addressSection = section;
}
/**
* If this address was resolved from a host, returns that host. Otherwise, does a reverse name lookup.
*/
public HostName toHostName() {
HostName host = fromHost;
if(host == null) {
fromHost = host = toCanonicalHostName();
}
return host;
}
/**
* Does a reverse name lookup to get the canonical host name.
*/
public HostName toCanonicalHostName() {
HostName host = canonicalHost;
if(host == null) {
if(isMultiple()) {
throw new IPAddressTypeException(this, "ipaddress.error.unavailable.numeric");
}
InetAddress inetAddress = toInetAddress();
//String hostStr1 = inetAddress.getHostName();
String hostStr = inetAddress.getCanonicalHostName();//note: this does not return ipv6 addresses enclosed in brackets []
if(hostStr.equals(inetAddress.getHostAddress())) {
//we got back the address, so the host is me
host = new HostName(hostStr, new ParsedHost(hostStr, AddressProvider.getProviderFor(this)));
host.resolvedAddress = this;
} else {
//the reverse lookup succeeded in finding a host string
//we might not be the default resolved address for the host, so we don't set that field
host = new HostName(hostStr);
}
}
return host;
}
public static IPAddress from(byte bytes[]) {
return from(bytes, null, null, null);
}
public static IPAddress from(byte bytes[], Integer prefixLength) {
return from(bytes, null, prefixLength, null);
}
public static IPAddress from(byte bytes[], byte bytes2[], Integer prefixLength) {
return from(bytes, bytes2, prefixLength, null);
}
protected static IPAddress from(byte bytes[], byte bytes2[], Integer prefixLength, String zone) {
if(bytes.length == IPv4Address.BYTE_COUNT) {
if(zone != null) {
throw new IllegalArgumentException();
}
IPAddressCreator<IPv4Address, ?, IPv4AddressSegment> addressCreator = IPv4Address.network().getAddressCreator();
IPv4AddressSegment segments[] = toSegments(bytes, bytes2, IPv4Address.SEGMENT_COUNT, IPv4Address.BYTES_PER_SEGMENT, IPv4Address.BITS_PER_SEGMENT, addressCreator, prefixLength);
return addressCreator.createAddressInternal(segments); /* address creation */
}
if(bytes.length == IPv6Address.BYTE_COUNT) {
IPAddressCreator<IPv6Address, ?, IPv6AddressSegment> addressCreator = IPv6Address.network().getAddressCreator();
IPv6AddressSegment segments[] = toSegments(bytes, bytes2, IPv6Address.SEGMENT_COUNT, IPv6Address.BYTES_PER_SEGMENT, IPv6Address.BITS_PER_SEGMENT, addressCreator, prefixLength);
return addressCreator.createAddressInternal(segments, zone); /* address creation */
}
throw new IllegalArgumentException();
}
protected static <T extends IPAddressSegment> T[] toSegments(
byte bytes[],
byte bytes2[],
int segmentCount,
int bytesPerSegment,
int bitsPerSegment,
IPAddressSegmentCreator<T> creator,
Integer networkPrefixLength) {
T segments[] = creator.createSegmentArray(segmentCount);
for(int i = 0, segmentIndex = 0; i < bytes.length; i += bytesPerSegment, segmentIndex++) {
Integer segmentPrefixLength = IPAddressSection.getSegmentPrefixLength(bitsPerSegment, networkPrefixLength, segmentIndex);
if(segmentPrefixLength != null && segmentPrefixLength == 0) {
segments[segmentIndex] = creator.createSegment(0, 0);
continue;
}
int value = 0, value2 = 0;
int k = bytesPerSegment + i;
for(int j = i; j < k; j++) {
int byteValue = bytes[j];
value <<= 8;
value |= 0xff & byteValue;
if(bytes2 != null) {
byteValue = bytes2[j];
value2 <<= 8;
value2 |= 0xff & byteValue;
}
}
segments[segmentIndex] = (bytes2 != null) ? creator.createSegment(value, value2, segmentPrefixLength) : creator.createSegment(value, segmentPrefixLength);
}
return segments;
}
protected static String toNormalizedString(byte bytes[], byte bytes2[], Integer prefixLength, String zone) {
if(bytes.length == IPv4Address.BYTE_COUNT) {
return toNormalizedString(bytes, bytes2, prefixLength,
IPv4Address.SEGMENT_COUNT, IPv4Address.BYTES_PER_SEGMENT, IPv4Address.BITS_PER_SEGMENT, IPv4Address.MAX_VALUE_PER_SEGMENT, IPv4Address.SEGMENT_SEPARATOR, IPv4Address.DEFAULT_TEXTUAL_RADIX,
null, IPv4Address.network());
}
if(bytes.length == IPv6Address.BYTE_COUNT) {
return toNormalizedString(bytes, bytes2, prefixLength,
IPv6Address.SEGMENT_COUNT, IPv6Address.BYTES_PER_SEGMENT, IPv6Address.BITS_PER_SEGMENT, IPv6Address.MAX_VALUE_PER_SEGMENT, IPv6Address.SEGMENT_SEPARATOR, IPv6Address.DEFAULT_TEXTUAL_RADIX,
zone, IPv6Address.network());
}
throw new IllegalArgumentException();
}
private static String toNormalizedString(
byte bytes[],
byte bytes2[],
Integer prefixLength,
int segmentCount,
int bytesPerSegment,
int bitsPerSegment,
int segmentMaxValue,
char separator,
int radix,
String zone,
IPAddressNetwork network) {
StringBuilder builder = new StringBuilder(IPv4Address.MAX_STRING_LEN + 8);
for(int i = 0; i < bytes.length; i += bytesPerSegment) {
Integer segmentPrefixLength = IPAddressSection.getSegmentPrefixLength(bitsPerSegment, prefixLength, i);
if(segmentPrefixLength != null && segmentPrefixLength == 0) {
builder.append('0').append(separator);
continue;
}
int value = 0, value2 = 0;
int k = bytesPerSegment + i;
for(int j = i; j < k; j++) {
int byteValue = bytes[j];
value <<= 8;
value |= 0xff & byteValue;
if(bytes2 != null) {
byteValue = bytes2[j];
value2 <<= 8;
value2 |= 0xff & byteValue;
}
}
if(bytes2 == null) {
if(segmentPrefixLength != null) {
value &= network.getSegmentNetworkMask(segmentPrefixLength);
}
IPAddressSegment.toUnsignedStringFast(value, radix, builder);
} else {
if(segmentPrefixLength != null) {
int mask = network.getSegmentNetworkMask(segmentPrefixLength);
value &= mask;
value2 &= mask;
}
if(value == value2) {
IPAddressSegment.toUnsignedStringFast(value, radix, builder);
} else {
if(value > value2) {
int tmp = value2;
value2 = value;
value = tmp;
}
if(value == 0 && value2 == segmentMaxValue) {
builder.append(IPAddress.SEGMENT_WILDCARD_STR);
} else {
IPAddressSegment.getRangeString(value, value2, radix, builder);
}
}
}
builder.append(separator);
}
builder.setLength(builder.length() - 1);
if(zone != null && zone.length() > 0) {
builder.append(IPv6Address.ZONE_SEPARATOR).append(zone);
}
if(prefixLength != null) {
builder.append(IPAddress.PREFIX_LEN_SEPARATOR).append(prefixLength);
}
return builder.toString();
}
public abstract IPAddressNetwork getNetwork();
public static IPAddressNetwork network(IPVersion version) {
return version.isIPv4() ? IPv4Address.network() : IPv6Address.network();
}
public static IPAddress getLoopback(IPVersion version) {
return network(version).getLoopback();
}
public static IPAddress getLocalHost() throws UnknownHostException {
return from(InetAddress.getLocalHost().getAddress());
}
public static String[] getStandardLoopbackStrings(IPVersion version) {
return network(version).getStandardLoopbackStrings();
}
/**
* Returns the address as an address section comprising all segments in the address.
* @return
*/
public IPAddressSection getSection() {
return addressSection;
}
/**
* Returns all the ways of breaking this address down into segments, as selected.
* @return
*/
public IPAddressPart[] getParts(IPStringBuilderOptions options) {
return new IPAddressPart[] { getSection() };
}
public int getMaxSegmentValue() {
return IPAddressSegment.getMaxSegmentValue(getIPVersion());
}
public static int maxSegmentValue(IPVersion version) {
return IPAddressSegment.getMaxSegmentValue(version);
}
public int getBytesPerSegment() {
return IPAddressSegment.getByteCount(getIPVersion());
}
public int getBitsPerSegment() {
return IPAddressSegment.getBitCount(getIPVersion());
}
public static int bitsPerSegment(IPVersion version) {
return IPAddressSegment.getBitCount(version);
}
public abstract int getByteCount();
public static int byteCount(IPVersion version) {
return version.isIPv4() ? IPv4Address.BYTE_COUNT : IPv6Address.BYTE_COUNT;
}
public abstract int getSegmentCount();
public int getSegmentIndex(Integer networkPrefixLength) {
return addressSection.getSegmentIndex(networkPrefixLength);
}
static int getSegmentIndex(Integer networkPrefixLength, int byteLength, int bytesPerSegment) {
return IPAddressSection.getSegmentIndex(networkPrefixLength, byteLength, bytesPerSegment);
}
public int getByteIndex(Integer networkPrefixLength) {
return addressSection.getByteIndex(networkPrefixLength);
}
static int getByteIndex(Integer networkPrefixLength, int byteLength) {
return IPAddressSection.getByteIndex(networkPrefixLength, byteLength);
}
public static int segmentCount(IPVersion version) {
return version.isIPv4() ? IPv4Address.SEGMENT_COUNT : IPv6Address.SEGMENT_COUNT;
}
public abstract int getBitCount();
public static int bitCount(IPVersion version) {
return version.isIPv4() ? IPv4Address.BIT_COUNT : IPv6Address.BIT_COUNT;
}
public boolean isMultipleByNetworkPrefix() {
return addressSection.isMultipleByNetworkPrefix();
}
/**
* @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)
*/
public boolean isMultiple() {
return addressSection.isMultiple();
}
/**
* @return whether this address represents a network prefix or the set of all addresses with the same network prefix
*/
public boolean isPrefixed() {
return addressSection.isPrefixed();
}
public Integer getNetworkPrefixLength() {
return addressSection.getNetworkPrefixLength();
}
public IPAddressSegment getSegment(int index) {
return addressSection.getSegment(index);
}
/**
* Gets the count of addresses that this address may represent.
*
* If this address is not a CIDR network prefix and it has no range, then there is only one such address.
*
* @return
*/
public BigInteger getCount() {
if(!isMultiple()) {
return BigInteger.ONE;
}
return addressSection.getCount();
}
/**
* If this represents an address with ranging values, returns an address representing the lower values of the range.
* If this represents an address with a single value in each segment, returns this.
*
* @return
*/
public abstract IPAddress getLower();
/**
* If this represents an address with ranging values, returns an address representing the upper values of the range
* If this represents an address with a single value in each segment, returns this.
*
* @return
*/
public abstract IPAddress getUpper();
protected static <R extends IPAddressSection, S extends IPAddressSegment> S[] createSingle(R original, IPAddressSegmentCreator<S> segmentCreator, IntFunction<S> segProducer) {
return IPAddressSection.createSingle(original, segmentCreator, segProducer);
}
protected static <T extends IPAddress> T getSingle(
T original,
Supplier<T> singleFromMultipleCreator) {
if(!original.isPrefixed() && !original.isMultiple()) {
return original;
}
return singleFromMultipleCreator.get();
}
public abstract Iterator<? extends IPAddress> iterator();
/**
* @return an object to iterate over the individual addresses represented by this object.
*/
public abstract Iterable<? extends IPAddress> getAddresses();
protected static <T extends IPAddress, R extends IPAddressSection, S extends IPAddressSegment> Iterator<T> iterator(
T original,
IPAddressCreator<T, R, S> creator,
Supplier<S[]> segs,
IntFunction<Iterator<S>> segIteratorProducer) {
return new Iterator<T>() {
private boolean doThis = !original.isMultiple() && !original.isPrefixed(); //note that a non-multiple address can have a prefix (either /32 or /128)
private Iterator<S[]> iterator = original.addressSection.iterator(creator, doThis, segs, segIteratorProducer);
@Override
public boolean hasNext() {
return iterator.hasNext() || doThis;
}
@Override
public T next() {
if(!hasNext()) {
throw new NoSuchElementException();
}
if(doThis) {
doThis = false;
return original;
}
S[] next = iterator.next();
return creator.createAddressInternal(next); /* address creation */
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
public boolean isIPv4() {
return addressSection.isIPv4();
}
public boolean isIPv6() {
return addressSection.isIPv6();
}
public IPVersion getIPVersion() {
return addressSection.getIPVersion();
}
/**
* If this address is IPv4, or can be converted to IPv4, returns that {@link IPv4Address}. Otherwise, returns null.
*
* @see #isIPv4Convertible()
* @return the address
*/
public IPv4Address toIPv4() {
return null;
}
/**
*
* @return If this address is IPv6, or can be converted to IPv6, returns that {@link IPv6Address}. Otherwise, returns null.
*/
public IPv6Address toIPv6() {
return null;
}
/**
* Determines whether this address can be converted to IPv4, if not IPv4 already.
* Override this method to convert in your own way, or call setAddressConverter with your own converter object.
*
* You should also override {@link #toIPv4()} to match the conversion.
*
* This method returns true for all IPv4 addresses.
*
* @return
*/
public abstract boolean isIPv4Convertible();
/**
* Determines whether an address can be converted to IPv6, if not IPv6 already.
* Override this method to convert in your own way, or call setAddressConverter with your own converter object.
*
* You should also override {@link #toIPv6()} to match the conversion.
*
* This method returns true for all IPv6 addresses.
*
* @return
*/
public abstract boolean isIPv6Convertible();
/**
* @see java.net.InetAddress#isLinkLocalAddress()
*/
public abstract boolean isLinkLocal();
/**
* @see java.net.InetAddress#isSiteLocalAddress()
*/
public abstract boolean isSiteLocal();
/**
* @see java.net.InetAddress#isMulticastAddress()
*/
public abstract boolean isMulticast();
/**
* @see java.net.InetAddress#isAnyLocalAddress()
*/
public boolean isAnyLocal() {
return isZero();
}
/**
* @see java.net.InetAddress#isLoopbackAddress()
*/
public abstract boolean isLoopback();
/**
* @throws IPAddressTypeException if this address does not map to a single address.
* If you want to get subnet bytes or mask bytes, call getLowestBytes
*/
public byte[] getBytes() {
if(isMultiple()) {
throw new IPAddressTypeException(this, "ipaddress.error.unavailable.numeric");
}
return getLowestBytes();
}
/**
* Gets the bytes for the lowest address in the range represented by this address.
*
* @return
*/
public byte[] getLowestBytes() {
return addressSection.getLowestBytes();
}
/**
* @throws IPAddressTypeException if this address does not map to a single address, ie it is a subnet
*/
public InetAddress toInetAddress() {
if(inetAddress == null) {
synchronized(this) {
if(inetAddress == null) {
byte bytes[] = getBytes();
try {
inetAddress = InetAddress.getByAddress(bytes);
} catch(UnknownHostException e) { /* will never reach here */ }
}
}
}
return inetAddress;
}
public boolean isZero() {
if(isMultipleByNetworkPrefix()) {
return false;
}
return addressSection.isZero();
}
@Override
public int hashCode() {
return addressSection.hashCode();
}
@Override
public int compareTo(IPAddress other) {
if(this == other) {
return 0;
}
return addressComparator.compare(this, other);
}
public boolean matches(IPAddressString otherString) {
//before converting otherString to an address object, check if the strings match
if(isFromSameString(otherString)) {
return true;
}
IPAddress otherAddr = otherString.getAddress();
return otherAddr != null && isSameAddress(otherAddr);
}
protected boolean isFromSameString(IPAddressString otherString) {
return fromString != null && otherString != null &&
(fromString == otherString || fromString.fullAddr.equals(otherString.fullAddr)) &&
fromString.validationOptions == otherString.validationOptions;//we could do equals here but 99% of the time this gives the right answer in less time because the validation options are not expected to change
}
public boolean isSameAddress(IPAddress other) {
return other == this || getSection().equals(other.getSection());
}
/**
* Two IPAddress objects are equal if they represent the same set of addresses.
* Whether one or the other has an associated network prefix length is not considered.
*
* Also, an IPAddressString and IPAddress are considered equal if they represent the same set of addresses.
*/
@Override
public boolean equals(Object o) {
if(o == this) {
return true;
}
if(o instanceof IPAddress) {
IPAddress other = (IPAddress) o;
if(isFromSameString(other.fromString)) {
return true;
}
return isSameAddress(other);
}
return false;
}
/**
*
* @param other
* @return whether this subnet contains the given address
*/
public boolean contains(IPAddress other) {
if(other == this) {
return true;
}
return addressSection.contains(other.addressSection);
}
/**
* Subtract the give subnet from this subnet, returning an array of sections for the result (the subnets will not be contiguous so an array is required).
*
* Computes the subnet difference, the set of addresses in this address section but not in the provided section.
*
* @param other
* @throws IPAddressTypeException if the two sections are not comparable
* @return the difference
*/
public abstract IPAddress[] subtract(IPAddress other);
public static IPAddress from(InetAddress inetAddress) {
byte bytes[] = inetAddress.getAddress();
if(bytes.length == IPv6Address.BYTE_COUNT) {
Inet6Address inet6Address = (Inet6Address) inetAddress;
NetworkInterface networkInterface = inet6Address.getScopedInterface();
String zone = null;
if(networkInterface == null) {
int scopeId = inet6Address.getScopeId();
if(scopeId != 0) {
zone = Integer.toString(scopeId);
}
} else {
zone = networkInterface.getName();
}
IPv6AddressCreator ipv6Creator = IPv6Address.network().getAddressCreator();
return ipv6Creator.createAddressInternal(bytes, null, zone); /* address creation */
} else {
IPv4AddressCreator creator = IPv4Address.network().getAddressCreator();
return creator.createAddressInternal(bytes, null, null); /* address creation */
}
}
//////////////// string creation below ///////////////////////////////////////////////////////////////////////////////////////////
public String[] getSegmentStrings() {
return addressSection.getSegmentStrings();
}
@Override
public String toString() {
return toNormalizedString();
}
/**
* This produces a canonical string.
*
* RFC 5952 describes canonical representations.
* 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.
*/
public String toCanonicalString() {
return addressSection.toCanonicalString();
}
/**
* This produces a string with no compressed segments and all segments of full length,
* which is 4 characters for IPv6 segments and 3 characters for IPv4 segments.
*
* Each address has a unique full string, not counting CIDR the prefix, which can give two equal addresses different strings.
*/
public String toFullString() {
return addressSection.toFullString();
}
/**
* The normalized string returned by this method is consistent with java.net.Inet4Address and java.net.Inet6Address.
* IPs are not compressed nor mixed in this representation.
*
* The string returned by this method is unique for each address, not counting CIDR the prefix, which can give two equal addresses different strings.
*/
public String toNormalizedString() {
return addressSection.toNormalizedString();
}
protected void cacheNormalizedString(String str) {
addressSection.cacheNormalizedString(str);
}
/**
* This produces the shortest valid string for the address.
*
* Each address has a unique compressed string, not counting the prefix, which can give two equal addresses different strings.
*
* For subnets the string will not have wildcards in host segments (there will be zeros instead), only in network segments.
*/
public String toCompressedString() {
return addressSection.toCompressedString();
}
/**
* Produces a consistent subnet string that looks like 1.2.*.* or 1:2::/16
*
* In the case of IPv4, this means that wildcards are used instead of a network prefix when a network prefix has been supplied.
* In the case of IPv6, when a network prefix has been supplied, the prefix will be shown and the host section will be compressed with ::.
*/
public String toSubnetString() {
return addressSection.toSubnetString();
}
/**
* This produces a string similar to the normalized string but avoids the CIDR prefix.
* CIDR addresses will be shown with wildcards and ranges instead of using the CIDR prefix notation.
*/
public String toNormalizedWildcardString() {
return addressSection.toNormalizedWildcardString();
}
/**
* This produces a string similar to the canonical string but avoids the CIDR prefix.
* Addresses with a network prefix length will be shown with wildcards and ranges instead of using the CIDR prefix length notation.
* IPv6 addresses will be compressed according to the canonical representation.
*/
public String toCanonicalWildcardString() {
return addressSection.toCanonicalWildcardString();
}
/**
* This is similar to toNormalizedWildcardString, avoiding the CIDR prefix, but with compression as well.
*/
public String toCompressedWildcardString() {
return addressSection.toCompressedWildcardString();
}
/**
* This is the same as the string from toNormalizedWildcardString except that
* it uses IPAddress.SEGMENT_SQL_WILDCARD instead of IPAddress.SEGMENT_WILDCARD and also uses IPAddress.SEGMENT_SQL_SINGLE_WILDCARD
*/
public String toSQLWildcardString() {
return addressSection.toSQLWildcardString();
}
/**
* Returns a string with a CIDR network prefix length if this address has a network prefix length.
* For IPv6, the host section will be compressed with ::, for IPv4 the host section will be zeros.
* @return
*/
public String toNetworkPrefixLengthString() {
return addressSection.toNetworkPrefixLengthString();
}
/**
* Returns a mixed string if it represents a convertible IPv4 address, returns the normalized string otherwise.
* @return
*/
public String toConvertedString() {
return toNormalizedString();
}
/**
* Generates the Microsoft UNC path component for this address
*
* @return
*/
public abstract String toUNCHostName();
/**
* Generates the reverse DNS lookup string
* For 8.255.4.4 it is 4.4.255.8.in-addr.arpa
* For 2001:db8::567:89ab it is b.a.9.8.7.6.5.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa
*
*
* @throw IPAddressTypeException if this address is a subnet of multiple addresses
* @return
*/
public abstract String toReverseDNSLookupString();
/**
* Writes this address as hexadecimal, with or without a preceding 0x prefix
*/
public String toHexString(boolean withPrefix) {
return addressSection.toHexString(withPrefix);
}
/**
* Constructs a string representing this address according to the given parameters
*
* @throw IPAddressTypeException if this address is a subnet of multiple addresses, you have selected splitDigits, and the address range cannot be represented in split digits
*
* @param params the parameters for the address string
*/
public String toNormalizedString(StringOptions params) {
return addressSection.toNormalizedString(params);
}
/**
* Returns at most a few dozen string representations:
*
* -mixed (1:2:3:4:5:6:1.2.3.4)
* -full compressions (a:0:b:c:d:0:e:f or a::b:c:d:0:e:f or a:0:b:c:d::e:f)
* -full leading zeros (000a:0000:000b:000c:000d:0000:000e:000f)
* -all uppercase and all lowercase (a::a can be A::A)
* -combinations thereof
*
* @return
*/
public String[] toStandardStrings() {
return toStandardStringCollection().toStrings();
}
/**
* Produces almost all possible string variations
* <p>
* Use this method with care... a single IPv6 address can have thousands of string representations.
* <p>
* Examples:
* <ul>
* <li>"::" has 1297 such variations, but only 9 are considered standard</li>
* <li>"a:b:c:0:d:e:f:1" has 1920 variations, but only 12 are standard</li>
* </ul>
* <p>
* Variations included in this method:
* <ul>
* <li>all standard variations from {@link #toStandardStrings()}</li>
* <li>adding a variable number of leading zeros (::a can be ::0a, ::00a, ::000a)</li>
* <li>choosing any number of zero-segments to compress (:: can be 0:0:0::0:0)</li>
* <li>mixed representation of all variations (1:2:3:4:5:6:1.2.3.4)</li>
* <li>all uppercase and all lowercase (a::a can be A::A)</li>
* <li>all combinations of such variations</li>
* </ul>
* Variations omitted from this method: mixed case of a-f, which you can easily handle yourself with String.equalsIgnoreCase
* <p>
* @return the strings
*/
public String[] toAllStrings() {
return toAllStringCollection().toStrings();
}
/**
* Rather than using toAllStrings or StandardStrings,
* you can use this method to customize the list of strings produced for this address
*/
public String[] toStrings(IPStringBuilderOptions options) {
return toStringCollection(options).toStrings();
}
public IPAddressPartStringCollection toStandardStringCollection() {
return addressSection.toStandardStringCollection();
}
public IPAddressPartStringCollection toAllStringCollection() {
return addressSection.toAllStringCollection();
}
public IPAddressPartStringCollection toStringCollection(IPStringBuilderOptions options) {
return addressSection.toStringCollection(options);
}
/**
* Generates an IPAddressString object for this IPAddress object.
*
* This same IPAddress object can be retrieved from the resulting IPAddressString object using {@link IPAddressString#getAddress()}
*
* In general, users are intended to create IPAddress objects from IPAddressString objects,
* while the reverse direction is generally not all that useful.
*
* However, the reverse direction can be useful under certain circumstances.
*
* Not all IPAddressString objects can be converted to IPAddress objects,
* as is the case with IPAddressString objects corresponding to the types IPType.INVALID and IPType.EMPTY.
*
* Not all IPAddressString objects can be converted to IPAddress objects without specifying the IP version,
* as is the case with IPAddressString objects corresponding to the types IPType.PREFIX and IPType.ALL.
*
* So in the event you wish to store a collection of IPAddress objects with a collection of IPAddressString objects,
* and not all the IPAddressString objects can be converted to IPAddress objects, then you may wish to use a collection
* of only IPAddressString objects, in which case this method is useful.
*
* @return an IPAddressString object for this IPAddress.
*/
public IPAddressString toAddressString() {
if(fromString == null) {
fromString = new IPAddressString(this); /* address string creation */
}