Java Code Examples for inet.ipaddr.IPAddress
The following examples show how to use
inet.ipaddr.IPAddress.
These examples are extracted from open source projects.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: IPAddress Author: seancfoley File: TestBase.java License: Apache License 2.0 | 6 votes |
void testHostAddress(String addressStr) { IPAddressString str = createAddress(addressStr); IPAddress address = str.getAddress(); if(address != null) { IPAddress hostAddress = str.getHostAddress(); int prefixIndex = addressStr.indexOf(IPAddress.PREFIX_LEN_SEPARATOR); if(prefixIndex < 0) { if(!address.equals(hostAddress) || !address.contains(hostAddress)) { addFailure(new Failure("failed host address with no prefix: " + hostAddress + " expected: " + address, str)); } } else { String substr = addressStr.substring(0, prefixIndex); IPAddressString str2 = createAddress(substr); IPAddress address2 = str2.getAddress(); if(!address2.equals(hostAddress)) { addFailure(new Failure("failed host address: " + hostAddress + " expected: " + address2, str)); } } } }
Example #2
Source Project: IPAddress Author: seancfoley File: SpecialTypesTest.java License: Apache License 2.0 | 6 votes |
void testAllValues(IPVersion version, BigInteger count) { HostName hostAll = createHost("*", HOST_OPTIONS); IPAddressString addressAllStr = createAddress("*", ADDRESS_OPTIONS); IPAddress addressAll = addressAllStr.getAddress(version); String address2Str = version.isIPv4() ? "*.*.*.*" : "*:*:*:*:*:*:*:*"; IPAddress address = createAddress(address2Str, ADDRESS_OPTIONS).getAddress(); if(!addressAll.equals(address)) { addFailure(new Failure("no match " + address, addressAll)); } else if(addressAll.compareTo(address) != 0) { addFailure(new Failure("no match " + address, addressAll)); } else if(!addressAll.getCount().equals(count)) { addFailure(new Failure("no count match ", addressAll)); } else { addressAll = hostAll.asAddress(version); if(!addressAll.equals(address)) { addFailure(new Failure("no match " + address, addressAll)); } else if(addressAll.compareTo(address) != 0) { addFailure(new Failure("no match " + address, addressAll)); } else if(!addressAll.getCount().equals(count)) { addFailure(new Failure("no count match ", addressAll)); } } incrementTestCount(); }
Example #3
Source Project: IPAddress Author: seancfoley File: IPAddressProvider.java License: Apache License 2.0 | 6 votes |
/** * When a value provider produces no value, equality and comparison are based on the enum IPType, * which can by null. * @param o * @return */ default boolean providerEquals(IPAddressProvider other) throws IncompatibleAddressException { if(this == other) { return true; } IPAddress value = getProviderAddress(); if(value != null) { IPAddress otherValue = other.getProviderAddress(); if(otherValue != null) { return value.equals(otherValue); } else { return false; } } //this works with both null and also non-null since the type is an enum return getType() == other.getType(); }
Example #4
Source Project: IPAddress Author: seancfoley File: TestBase.java License: Apache License 2.0 | 6 votes |
boolean confirmHostStrings(IPAddress ipAddr, HostName ...strs) { for(HostName str : strs) { IPAddress a = str.getAddress(); if(!ipAddr.equals(a)) { addFailure(new Failure("failed produced string: " + str, ipAddr)); return false; } String again = str.toNormalizedString(); str = new HostName(again); a = str.getAddress(); if(!ipAddr.equals(a)) { addFailure(new Failure("failed produced string: " + str, ipAddr)); return false; } } incrementTestCount(); return true; }
Example #5
Source Project: IPAddress Author: seancfoley File: HostTest.java License: Apache License 2.0 | 6 votes |
void testMasked(String masked, String mask, Integer prefixLength, String result) { HostName maskedHostStr = createHost(masked); IPAddress maskAddr = mask != null ? createAddress(mask).getAddress() : null; if(result != null) { IPAddress resultAddr = createAddress(result).getAddress(); IPAddress maskedAddr = maskedHostStr.getAddress(); if(!maskedAddr.equals(resultAddr)) { addFailure(new Failure("masked " + maskedAddr + " instead of expected " + resultAddr, maskedAddr)); } } if(!Objects.equals(maskAddr, maskedHostStr.getMask())) { addFailure(new Failure("masked " + maskAddr + " instead of expected " + maskedHostStr.getMask(), maskedHostStr)); } if(!Objects.equals(maskedHostStr.getNetworkPrefixLength(), prefixLength)) { addFailure(new Failure("masked prefix length was " + maskedHostStr.getNetworkPrefixLength() + " instead of expected " + prefixLength, maskedHostStr)); } incrementTestCount(); }
Example #6
Source Project: IPAddress Author: seancfoley File: IPAddressProvider.java License: Apache License 2.0 | 6 votes |
@Override public int providerCompare(IPAddressProvider other) throws IncompatibleAddressException { if(this == other) { return 0; } if(adjustedVersion == null) { if(other.getType() == IPType.PREFIX_ONLY) {//both are PREFIX_ONLY return other.getProviderNetworkPrefixLength().intValue() - getProviderNetworkPrefixLength().intValue(); } return IPType.PREFIX_ONLY.ordinal() - other.getType().ordinal(); } IPAddress otherValue = other.getProviderAddress(); if(otherValue != null) { return getProviderAddress().compareTo(otherValue); } return IPType.from(adjustedVersion).ordinal() - other.getType().ordinal(); }
Example #7
Source Project: IPAddress Author: seancfoley File: TestBase.java License: Apache License 2.0 | 6 votes |
boolean confirmHostStrings(IPAddress ipAddr, boolean omitZone, String ...strs) { for(String str : strs) { HostName hostName = new HostName(str); IPAddress a = hostName.getAddress(); if(omitZone) { IPv6Address ipv6Addr = ipAddr.toIPv6(); ipAddr = new IPv6Address(ipv6Addr.getSection()); } if(!ipAddr.equals(a)) { addFailure(new Failure("failed produced string: " + str, ipAddr)); return false; } String again = hostName.toNormalizedString(); hostName = new HostName(again); a = hostName.getAddress(); if(!ipAddr.equals(a)) { addFailure(new Failure("failed produced string: " + str, ipAddr)); return false; } } incrementTestCount(); return true; }
Example #8
Source Project: IPAddress Author: seancfoley File: AddressDivisionBase.java License: Apache License 2.0 | 6 votes |
@Override public String toString() { int radix = getDefaultTextualRadix(); IPStringOptions opts; switch(radix) { case 8: opts = OCTAL_PARAMS; break; case 16: opts = HEX_PARAMS; break; case 10: opts = DECIMAL_PARAMS; break; default: opts = new IPStringOptions.Builder(radix).setWildcards(new Wildcards(IPAddress.RANGE_SEPARATOR_STR)).toOptions(); break; } StringBuilder builder = new StringBuilder(34); toParams(opts).appendSingleDivision(this, builder); return builder.toString(); }
Example #9
Source Project: AntiVPN Author: egg82 File: ScoreCommand.java License: MIT License | 5 votes |
private Set<String> getIPs(String mask, int count) { Set<String> retVal = new HashSet<>(); IPAddress range = new IPAddressString(mask).getAddress(); int fails = 0; while (retVal.size() < count && fails < 1000) { long getIndex = fairRoundedRandom(0L, range.getCount().longValue()); long i = 0; for (IPAddress ip : range.getIterable()) { if (i == getIndex) { String str = ip.toCanonicalString(); int idx = str.indexOf('/'); if (idx > -1) { str = str.substring(0, idx); } if (!retVal.add(str)) { fails++; } if (retVal.size() >= count || fails >= 1000) { break; } getIndex = fairRoundedRandom(0L, range.getCount().longValue()); if (getIndex <= i) { break; } } i++; } } return retVal; }
Example #10
Source Project: AntiVPN Author: egg82 File: ScoreCommand.java License: MIT License | 5 votes |
private Set<String> getIPs(String mask, int count) { Set<String> retVal = new HashSet<>(); IPAddress range = new IPAddressString(mask).getAddress(); int fails = 0; while (retVal.size() < count && fails < 1000) { long getIndex = fairRoundedRandom(0L, range.getCount().longValue()); long i = 0; for (IPAddress ip : range.getIterable()) { if (i == getIndex) { String str = ip.toCanonicalString(); int idx = str.indexOf('/'); if (idx > -1) { str = str.substring(0, idx); } if (!retVal.add(str)) { fails++; } if (retVal.size() >= count || fails >= 1000) { break; } getIndex = fairRoundedRandom(0L, range.getCount().longValue()); if (getIndex <= i) { break; } } i++; } } return retVal; }
Example #11
Source Project: IPAddress Author: seancfoley File: IPv4Address.java License: Apache License 2.0 | 5 votes |
@Override public IPv4Address[] subtract(IPAddress other) throws AddressConversionException { IPv4AddressSection thisSection = getSection(); IPv4AddressSection sections[] = thisSection.subtract(convertArg(other).getSection()); if(sections == null) { return null; } IPv4AddressCreator creator = getAddressCreator(); IPv4Address result[] = new IPv4Address[sections.length]; for(int i = 0; i < result.length; i++) { result[i] = creator.createAddress(sections[i]); /* address creation */ } return result; }
Example #12
Source Project: IPAddress Author: seancfoley File: IPv6Address.java License: Apache License 2.0 | 5 votes |
private IPv6Address convertArg(IPAddress arg) throws AddressConversionException{ IPv6Address converted = arg.toIPv6(); if(converted == null) { throw new AddressConversionException(this, arg); } return converted; }
Example #13
Source Project: IPAddress Author: seancfoley File: IPv4Address.java License: Apache License 2.0 | 5 votes |
@Override public IPv4Address coverWithPrefixBlock(IPAddress other) throws AddressConversionException { return IPv4AddressSection.coverWithPrefixBlock( this, convertArg(other), IPv4Address::getLower, IPv4Address::getUpper, Address.ADDRESS_LOW_VALUE_COMPARATOR::compare); }
Example #14
Source Project: IPAddress Author: seancfoley File: IPv4Address.java License: Apache License 2.0 | 5 votes |
@Override public IPv4Address[] spanWithPrefixBlocks(IPAddress other) throws AddressConversionException { return IPAddress.getSpanningPrefixBlocks( this, convertArg(other), IPv4Address::getLower, IPv4Address::getUpper, Address.ADDRESS_LOW_VALUE_COMPARATOR::compare, IPv4Address::assignPrefixForSingleBlock, IPv4Address::withoutPrefixLength, getAddressCreator()::createAddressArray); }
Example #15
Source Project: IPAddress Author: seancfoley File: IPv4Address.java License: Apache License 2.0 | 5 votes |
@Override public IPv4Address[] spanWithSequentialBlocks(IPAddress other) throws AddressConversionException { return IPAddress.getSpanningSequentialBlocks( this, convertArg(other), IPv4Address::getLower, IPv4Address::getUpper, Address.ADDRESS_LOW_VALUE_COMPARATOR::compare, IPv4Address::withoutPrefixLength, getAddressCreator()); }
Example #16
Source Project: IPAddress Author: seancfoley File: ParsedHost.java License: Apache License 2.0 | 5 votes |
private String mapString(IPAddressProvider addressProvider) { if(addressProvider.isProvidingAllAddresses()) { return IPAddress.SEGMENT_WILDCARD_STR; } else if(addressProvider.isProvidingPrefixOnly()) { return IPAddressNetwork.getPrefixString(addressProvider.getProviderNetworkPrefixLength()); } else if(addressProvider.isProvidingEmpty()) { return ""; } return originalStr; }
Example #17
Source Project: IPAddress Author: seancfoley File: IPv4Address.java License: Apache License 2.0 | 5 votes |
@Override public IPv4Address[] mergeToSequentialBlocks(IPAddress ...addresses) throws AddressConversionException { addresses = addresses.clone(); for(int i = 0; i < addresses.length; i++) { addresses[i] = convertArg(addresses[i]); } List<IPAddressSegmentSeries> blocks = getMergedSequentialBlocks(this, addresses, getAddressCreator()); return blocks.toArray(new IPv4Address[blocks.size()]); }
Example #18
Source Project: IPAddress Author: seancfoley File: IPv6Address.java License: Apache License 2.0 | 5 votes |
@Override public IPv6Address[] subtract(IPAddress other) throws AddressConversionException { IPv6AddressSection thisSection = getSection(); IPv6AddressSection sections[] = thisSection.subtract(convertArg(other).getSection()); if(sections == null) { return null; } IPv6Address result[] = new IPv6Address[sections.length]; for(int i = 0; i < result.length; i++) { result[i] = getCreator().createAddress(sections[i]); /* address creation */ } return result; }
Example #19
Source Project: IPAddress Author: seancfoley File: TestBase.java License: Apache License 2.0 | 5 votes |
boolean confirmAddrStrings(IPAddress ipAddr, IPAddressString ...strs) { for(IPAddressString str : strs) { IPAddress addr = str.getAddress(); if(!ipAddr.equals(addr)) { addFailure(new Failure("failed produced string: " + str, ipAddr)); return false; } } incrementTestCount(); return true; }
Example #20
Source Project: IPAddress Author: seancfoley File: Validator.java License: Apache License 2.0 | 5 votes |
/** * Returns the index of the first invalid character of the zone, or -1 if the zone is valid * * @param sequence * @return */ public static int validateZone(CharSequence zone) { for(int i = 0; i < zone.length(); i++) { char c = zone.charAt(i); if (c == IPAddress.PREFIX_LEN_SEPARATOR) { return i; } if (c == IPv6Address.SEGMENT_SEPARATOR) { return i; } } return -1; }
Example #21
Source Project: IPAddress Author: seancfoley File: TrieTest.java License: Apache License 2.0 | 5 votes |
void createSampleTree(IPv6AddressTrie tree, IPAddress addrs[]) { for(IPAddress addr : addrs) { if(addr.isIPv6()) { addr = Partition.checkBlockOrAddress(addr); if(addr != null) { IPv6Address address = addr.toIPv6(); tree.add(address); } } } }
Example #22
Source Project: IPAddress Author: seancfoley File: IPAddressProvider.java License: Apache License 2.0 | 5 votes |
default boolean isSequential() { try { IPAddress addr = getProviderAddress(); if(addr != null) { return addr.isSequential(); } } catch(IncompatibleAddressException e) {} return false; }
Example #23
Source Project: IPAddress Author: seancfoley File: IPAddressProvider.java License: Apache License 2.0 | 5 votes |
default IPAddressSeqRange getProviderSeqRange() { IPAddress addr = getProviderAddress(); if(addr != null) { return addr.toSequentialRange(); } return null; }
Example #24
Source Project: IPAddress Author: seancfoley File: AddressTrie.java License: Apache License 2.0 | 5 votes |
/** * Returns the previous address according to the trie ordering * * @param <E> * @param addr * @return */ @SuppressWarnings("unchecked") public static <E extends Address> E decrement(E addr) { if(addr.isZero()) { return null; } if(addr instanceof IPAddress) { IPAddress ipaddr = (IPAddress) addr; if(addr.isPrefixed()) { return (E) ipaddr.getLower().setPrefixLength(ipaddr.getPrefixLength() + 1).toMaxHost(); } return (E) ipaddr.toPrefixBlock(ipaddr.getBitCount() - (ipaddr.getTrailingBitCount(true) + 1)); } if(addr.isPrefixed()) { return (E) addr.getLower().setPrefixLength(addr.getPrefixLength() + 1).toPrefixBlock().getUpper(); } int trailingBitCount = 0; for(int i = addr.getSegmentCount() - 1; i >= 0; i--) { AddressSegment seg = addr.getSegment(i); if(!seg.isZero()) { trailingBitCount += Integer.numberOfTrailingZeros(seg.getSegmentValue()); break; } trailingBitCount += seg.getBitCount(); } return (E) addr.setPrefixLength(addr.getBitCount() - (trailingBitCount + 1)).toPrefixBlock(); }
Example #25
Source Project: IPAddress Author: seancfoley File: IPAddressProvider.java License: Apache License 2.0 | 5 votes |
default int providerHashCode() throws IncompatibleAddressException { IPAddress value = getProviderAddress(); if(value != null) { return value.hashCode(); } return Objects.hashCode(getType()); }
Example #26
Source Project: IPAddress Author: seancfoley File: IPv6Address.java License: Apache License 2.0 | 5 votes |
@Override public IPv6Address[] spanWithSequentialBlocks(IPAddress other) throws AddressConversionException { return IPAddress.getSpanningSequentialBlocks( this, convertArg(other), IPv6Address::getLower, IPv6Address::getUpper, Address.ADDRESS_LOW_VALUE_COMPARATOR::compare, IPv6Address::withoutPrefixLength, getDefaultCreator()); }
Example #27
Source Project: IPAddress Author: seancfoley File: TestRunner.java License: Apache License 2.0 | 5 votes |
@Override public IPAddress create(IPAddressKey addressKey) { if(addressKey.bytes.length == 4) { return new IPv4Address(addressKey.bytes); } return new IPv6Address(addressKey.bytes); }
Example #28
Source Project: IPAddress Author: seancfoley File: SpecialTypesTest.java License: Apache License 2.0 | 5 votes |
void testEmptyLoopback() { HostName w = createHost("", HOST_OPTIONS); if(w.isLoopback()) { addFailure(new Failure("failed: isSelf is " + w.isSelf(), w)); } IPAddress addressEmptyValue = w.getAddress(); if(!addressEmptyValue.isLoopback()) { addFailure(new Failure("failed: isSelf is " + addressEmptyValue.isLoopback(), w)); } HostName w2 = createHost("", EMPTY_ADDRESS_OPTIONS); if(!w2.isLoopback()) { addFailure(new Failure("failed: isSelf is " + w2.isSelf(), w2)); } incrementTestCount(); }
Example #29
Source Project: IPAddress Author: seancfoley File: TrieTest.java License: Apache License 2.0 | 5 votes |
static <T extends IPAddress> void partitionForTrie(TestBase testBase, AddressTrie<T> trie, T subnet) { Partition.partitionWithSingleBlockSize(subnet).predicateForEach(trie::add); if(trie.size() != 15) { addFailure(testBase, "partition size unexpected " + trie.size() + ", expected " + 15, trie); } Map<T, TrieNode<T>> all = Partition.partitionWithSingleBlockSize(subnet).applyForEach(trie::getAddedNode); if(all.size() != 15) { addFailure(testBase, "map size unexpected " + trie.size() + ", expected " + 15, trie); } HashMap<T, TrieNode<T>> all2 = new HashMap<>(); Partition.partitionWithSingleBlockSize(subnet).forEach(addr -> { TrieNode<T> node = trie.getAddedNode(addr); all2.put(addr, node); }); if(!all.equals(all2)) { addFailure(testBase, "maps not equal " + all + " and " + all2, trie); } trie.clear(); Partition.partitionWithSpanningBlocks(subnet).predicateForEach(trie::add); if(trie.size() != 4) { addFailure(testBase,"partition size unexpected " + trie.size() + ", expected " + 4, trie); } trie.clear(); Partition.partitionWithSingleBlockSize(subnet).predicateForEach(trie::add); Partition.partitionWithSpanningBlocks(subnet).predicateForEach(trie::add); if(trie.size() != 18) { addFailure(testBase,"partition size unexpected " + trie.size() + ", expected " + 18, trie); } boolean allAreThere = Partition.partitionWithSingleBlockSize(subnet).predicateForEach(trie::contains); boolean allAreThere2 = Partition.partitionWithSpanningBlocks(subnet).predicateForEach(trie::contains); if(!(allAreThere && allAreThere2)) { addFailure(testBase,"partition contains check failing", trie); } testBase.incrementTestCount(); }
Example #30
Source Project: IPAddress Author: seancfoley File: TrieTest.java License: Apache License 2.0 | 5 votes |
void testAddressCheck() { IPAddress addr = createAddress("1.2.3.4/16").getAddress(); PrefixConfiguration prefCon = addr.getNetwork().getPrefixConfiguration(); if(!prefCon.allPrefixedAddressesAreSubnets()) { testConvertedBlock(addr, null); } else { testIPAddrBlock("1.2.3.4/16"); } testIPAddrBlock("1.2.3.4"); testIPAddrBlock("::"); testNonBlock("1-3.2.3.4"); testConvertedBlock("1.2.3.4-5", 31); testNonBlock("1.2.3.5-6"); testConvertedBlock("1.2.3.4-7", 30); testNonBlock("::1-2:0"); testNonBlock("::1-2:0/112"); if(!prefCon.prefixedSubnetsAreExplicit()) { testConvertedBlock("::0-3:0/112", 110); testIPAddrBlock("::/64"); testIPAddrBlock("1.2.0.0/16"); } else { testNonBlock("::0-3:0/112"); testConvertedBlock("::/64", null); testConvertedBlock("1.2.0.0/16", null); } MACAddress mac = createMACAddress("a:b:c:*:*:*").getAddress(); mac = mac.setPrefixLength(48, false); testConvertedBlock(mac, 24); testMACAddrBlock("a:b:c:*:*:*"); testNonMACBlock("a:b:c:*:2:*"); testNonBlock("a:b:c:*:2:*"); // passes null into checkBlockOrAddress testMACAddrBlock("a:b:c:1:2:3"); }