/** * Validates that the argument has form "host:ip-address". Does not validate the form of the * hostname argument, but checks that the second half is valid via {@link * InetAddresses#isInetAddress}. */ public static boolean isValidArg(String arg) { final String[] args = arg.split(":"); if (args.length != 2) { return false; } final String ip = args[1]; return InetAddresses.isInetAddress(ip); } }
/** * Given a full hostname, return the word upto the first dot. * @param fullHostname the full hostname * @return the hostname to the first dot */ public static String simpleHostname(String fullHostname) { if (InetAddresses.isInetAddress(fullHostname)) { return fullHostname; } int offset = fullHostname.indexOf('.'); if (offset != -1) { return fullHostname.substring(0, offset); } return fullHostname; }
/** * Given a full hostname, return the word upto the first dot. * @param fullHostname the full hostname * @return the hostname to the first dot */ public static String simpleHostname(String fullHostname) { if (InetAddresses.isInetAddress(fullHostname)) { return fullHostname; } int offset = fullHostname.indexOf('.'); if (offset != -1) { return fullHostname.substring(0, offset); } return fullHostname; }
@Override public InetAddress toInetAddress(String hostOrAddress) throws UnknownHostException { if (InetAddresses.isInetAddress(hostOrAddress)) { return InetAddresses.forString(hostOrAddress); } return InetAddress.getByName(hostOrAddress); }
public PortMapping(@JsonProperty("ip") @Nullable final String ip, @JsonProperty("internalPort") final int internalPort, @JsonProperty("externalPort") @Nullable final Integer externalPort, @JsonProperty("protocol") @Nullable final String protocol) { this.ip = ip; // Validate IP here instead of in PortMappingParser to guaruntee every instance has a valid IP if (ip != null && !InetAddresses.isInetAddress(this.ip)) { throw new IllegalArgumentException(ip + " is not a valid IP address."); } this.internalPort = internalPort; this.externalPort = externalPort; this.protocol = Optional.fromNullable(protocol).or(TCP); }
if (InetAddresses.isInetAddress(host)) { ips.add(host); } else { if (InetAddresses.isInetAddress(ip)) { ips.add(ip); } else {
/** * Converts a list of domain name Subject Alternative Names into ASN1Encodable GeneralNames objects, for use with * the Bouncy Castle certificate builder. * * @param subjectAlternativeNames domain name SANs to convert * @return a GeneralNames instance that includes the specifie dsubjectAlternativeNames as DNS name fields */ private static GeneralNames getDomainNameSANsAsASN1Encodable(List<String> subjectAlternativeNames) { List<GeneralName> encodedSANs = new ArrayList<>(subjectAlternativeNames.size()); for (String subjectAlternativeName : subjectAlternativeNames) { // IP addresses use the IP Address tag instead of the DNS Name tag in the SAN list boolean isIpAddress = InetAddresses.isInetAddress(subjectAlternativeName); GeneralName generalName = new GeneralName(isIpAddress ? GeneralName.iPAddress : GeneralName.dNSName, subjectAlternativeName); encodedSANs.add(generalName); } return new GeneralNames(encodedSANs.toArray(new GeneralName[encodedSANs.size()])); }
@ValidatorMethod @SuppressWarnings("unused") public void validateHttpBindAddress() throws ValidationException { try { final String host = getHttpBindAddress().getHost(); if (!InetAddresses.isInetAddress(host)) { final InetAddress inetAddress = InetAddress.getByName(host); } } catch (IllegalArgumentException | UnknownHostException e) { throw new ValidationException(e); } }
@Override public Collection<InetAddress> resolveRemapped(String remappedHost) { // special case for IP literals: return the InetAddress without doing a dnsjava lookup. dnsjava seems to handle ipv4 literals // reasonably well, but does not handle ipv6 literals (with or without [] brackets) correctly. // note this does not work properly for ipv6 literals with a scope identifier, which is a known issue for InetAddresses.isInetAddress(). // (dnsjava also handles the situation incorrectly) if (InetAddresses.isInetAddress(remappedHost)) { return Collections.singletonList(InetAddresses.forString(remappedHost)); } // retrieve IPv4 addresses, then retrieve IPv6 addresses only if no IPv4 addresses are found. the current implementation always uses the // first returned address, so there is no need to look for IPv6 addresses if an IPv4 address is found. Collection<InetAddress> ipv4addresses = resolveHostByType(remappedHost, Type.A); if (!ipv4addresses.isEmpty()) { return ipv4addresses; } else { return resolveHostByType(remappedHost, Type.AAAA); } }
public void testForStringIPv6EightColons() throws UnknownHostException { String[] eightColons = { "::7:6:5:4:3:2:1", "::7:6:5:4:3:2:0", "7:6:5:4:3:2:1::", "0:6:5:4:3:2:1::", }; for (int i = 0; i < eightColons.length; i++) { InetAddress ipv6Addr = null; // Shouldn't hit DNS, because it's an IP string literal. ipv6Addr = InetAddress.getByName(eightColons[i]); assertEquals(ipv6Addr, InetAddresses.forString(eightColons[i])); assertTrue(InetAddresses.isInetAddress(eightColons[i])); } }
public void testForStringIPv4Input() throws UnknownHostException { String ipStr = "192.168.0.1"; InetAddress ipv4Addr = null; // Shouldn't hit DNS, because it's an IP string literal. ipv4Addr = InetAddress.getByName(ipStr); assertEquals(ipv4Addr, InetAddresses.forString(ipStr)); assertTrue(InetAddresses.isInetAddress(ipStr)); }
public void testConvertDottedQuadToHex() throws UnknownHostException { String[] ipStrings = { "7::0.128.0.127", "7::0.128.0.128", "7::128.128.0.127", "7::0.128.128.127" }; for (String ipString : ipStrings) { // Shouldn't hit DNS, because it's an IP string literal. InetAddress ipv6Addr = InetAddress.getByName(ipString); assertEquals(ipv6Addr, InetAddresses.forString(ipString)); assertTrue(InetAddresses.isInetAddress(ipString)); } }
throw e; } else if (InetAddresses.isInetAddress(hostEntry)) { //ip address ips.add(hostEntry); } else { //hostname
final String canonicalHostName = address.getCanonicalHostName(); if (!InetAddresses.isInetAddress(canonicalHostName)) { hosts.add(canonicalHostName);
public void testForStringIPv6Input() throws UnknownHostException { String ipStr = "3ffe::1"; InetAddress ipv6Addr = null; // Shouldn't hit DNS, because it's an IP string literal. ipv6Addr = InetAddress.getByName(ipStr); assertEquals(ipv6Addr, InetAddresses.forString(ipStr)); assertTrue(InetAddresses.isInetAddress(ipStr)); }
public void test3ff31() { try { InetAddresses.forString("3ffe:::1"); fail("IllegalArgumentException expected"); } catch (IllegalArgumentException expected) { } assertFalse(InetAddresses.isInetAddress("016.016.016.016")); }
@Override public void validate(final ProcessingReport report, final MessageBundle bundle, final FullData data) throws ProcessingException { final JsonNode instance = data.getInstance().getNode(); final String ipaddr = instance.textValue(); if (InetAddresses.isInetAddress(ipaddr) && InetAddresses .forString(ipaddr).getAddress().length == IPV6_LENGTH) return; report.error(newMsg(data, bundle, "err.format.invalidIPV6Address") .putArgument("value", ipaddr)); } }
@Override public void validate(final ProcessingReport report, final MessageBundle bundle, final FullData data) throws ProcessingException { final String ipaddr = data.getInstance().getNode().textValue(); if (InetAddresses.isInetAddress(ipaddr) && InetAddresses .forString(ipaddr).getAddress().length == IPV4_LENGTH) return; report.error(newMsg(data, bundle, "err.format.invalidIPv4Address") .putArgument("value", ipaddr)); } }
} catch (IllegalArgumentException expected) { assertFalse(InetAddresses.isInetAddress(bogusInputs[i]));
@Override public boolean execute(CommandSender sender, String label, String[] args, CommandMessages commandMessages) { if (!testPermission(sender, commandMessages.getPermissionMessage())) { return true; } if (args.length != 1) { sendUsageMessage(sender, commandMessages); return false; } String ip = args[0]; if (!InetAddresses.isInetAddress(ip)) { new LocalizedStringImpl("pardon-ip.invalid", commandMessages.getResourceBundle()) .sendInColor(ChatColor.RED, sender, ip); return false; } ServerProvider.getServer().unbanIP(ip); new LocalizedStringImpl("pardon-ip.done", commandMessages.getResourceBundle()) .send(sender, ip); return true; } }