public static boolean isValidIpV6Address(String ip) { return isValidIpV6Address((CharSequence) ip); }
public static boolean isValidIpV6Address(String ip) { return isValidIpV6Address((CharSequence) ip); }
public static boolean isValidIpV6Address(String ip) { return isValidIpV6Address((CharSequence) ip); }
/** * Returns the {@link String} representation of a host port combo. */ public static String toSocketAddressString(String host, int port) { String portStr = String.valueOf(port); return newSocketAddressStringBuilder( host, portStr, !isValidIpV6Address(host)).append(':').append(portStr).toString(); }
/** * Returns the {@link String} representation of a host port combo. */ public static String toSocketAddressString(String host, int port) { String portStr = String.valueOf(port); return newSocketAddressStringBuilder( host, portStr, !isValidIpV6Address(host)).append(':').append(portStr).toString(); }
/** * Validate that the given hostname can be used in SNI extension. */ static boolean isValidHostNameForSNI(String hostname) { return hostname != null && hostname.indexOf('.') > 0 && !hostname.endsWith(".") && !NetUtil.isValidIpV4Address(hostname) && !NetUtil.isValidIpV6Address(hostname); }
/** * Validate that the given hostname can be used in SNI extension. */ static boolean isValidHostNameForSNI(String hostname) { return hostname != null && hostname.indexOf('.') > 0 && !hostname.endsWith(".") && !NetUtil.isValidIpV4Address(hostname) && !NetUtil.isValidIpV6Address(hostname); }
/** * Returns the {@link String} representation of a host port combo. */ public static String toSocketAddressString(String host, int port) { String portStr = String.valueOf(port); return newSocketAddressStringBuilder( host, portStr, !isValidIpV6Address(host)).append(':').append(portStr).toString(); }
/** * Creates an byte[] based on an ipAddressString. No error handling is performed here. */ public static byte[] createByteArrayFromIpAddressString(String ipAddressString) { if (isValidIpV4Address(ipAddressString)) { return validIpV4ToBytes(ipAddressString); } if (isValidIpV6Address(ipAddressString)) { if (ipAddressString.charAt(0) == '[') { ipAddressString = ipAddressString.substring(1, ipAddressString.length() - 1); } int percentPos = ipAddressString.indexOf('%'); if (percentPos >= 0) { ipAddressString = ipAddressString.substring(0, percentPos); } return getIPv6ByName(ipAddressString, true); } return null; }
/** * Returns the {@link String} representation of an {@link InetSocketAddress}. * <p> * The output does not include Scope ID. * @param addr {@link InetSocketAddress} to be converted to an address string * @return {@code String} containing the text-formatted IP address */ public static String toSocketAddressString(InetSocketAddress addr) { String port = String.valueOf(addr.getPort()); final StringBuilder sb; if (addr.isUnresolved()) { String hostname = getHostname(addr); sb = newSocketAddressStringBuilder(hostname, port, !isValidIpV6Address(hostname)); } else { InetAddress address = addr.getAddress(); String hostString = toAddressString(address); sb = newSocketAddressStringBuilder(hostString, port, address instanceof Inet4Address); } return sb.append(':').append(port).toString(); }
/** * Creates an byte[] based on an ipAddressString. No error handling is performed here. */ public static byte[] createByteArrayFromIpAddressString(String ipAddressString) { if (isValidIpV4Address(ipAddressString)) { return validIpV4ToBytes(ipAddressString); } if (isValidIpV6Address(ipAddressString)) { if (ipAddressString.charAt(0) == '[') { ipAddressString = ipAddressString.substring(1, ipAddressString.length() - 1); } int percentPos = ipAddressString.indexOf('%'); if (percentPos >= 0) { ipAddressString = ipAddressString.substring(0, percentPos); } return getIPv6ByName(ipAddressString, true); } return null; }
/** * Returns the {@link String} representation of an {@link InetSocketAddress}. * <p> * The output does not include Scope ID. * @param addr {@link InetSocketAddress} to be converted to an address string * @return {@code String} containing the text-formatted IP address */ public static String toSocketAddressString(InetSocketAddress addr) { String port = String.valueOf(addr.getPort()); final StringBuilder sb; if (addr.isUnresolved()) { String hostname = getHostname(addr); sb = newSocketAddressStringBuilder(hostname, port, !isValidIpV6Address(hostname)); } else { InetAddress address = addr.getAddress(); String hostString = toAddressString(address); sb = newSocketAddressStringBuilder(hostString, port, address instanceof Inet4Address); } return sb.append(':').append(port).toString(); }
/** * Creates an {@link AddressResolverGroup} which always returns the specified {@code ip} address * for any hostname. */ public static MockAddressResolverGroup always(String ip) { requireNonNull(ip, "ip"); checkArgument(NetUtil.isValidIpV4Address(ip) || NetUtil.isValidIpV6Address(ip), "not an IP address: %s", ip); try { final InetAddress resolved = InetAddress.getByName(ip); return new MockAddressResolverGroup(unused -> resolved); } catch (UnknownHostException e) { throw new IllegalArgumentException(e); } }
/** * Formats the host string of an address so it can be used for computing an HTTP component * such as an URL or a Host header * @param addr the address * @return the formatted String */ public static String formatHostnameForHttp(InetSocketAddress addr) { String hostString = NetUtil.getHostname(addr); if (NetUtil.isValidIpV6Address(hostString)) { if (!addr.isUnresolved()) { hostString = NetUtil.toAddressString(addr.getAddress()); } return "[" + hostString + "]"; } return hostString; } }
/** * Creates an byte[] based on an ipAddressString. No error handling is performed here. */ public static byte[] createByteArrayFromIpAddressString(String ipAddressString) { if (isValidIpV4Address(ipAddressString)) { return validIpV4ToBytes(ipAddressString); } if (isValidIpV6Address(ipAddressString)) { if (ipAddressString.charAt(0) == '[') { ipAddressString = ipAddressString.substring(1, ipAddressString.length() - 1); } int percentPos = ipAddressString.indexOf('%'); if (percentPos >= 0) { ipAddressString = ipAddressString.substring(0, percentPos); } return getIPv6ByName(ipAddressString, true); } return null; }
/** * Returns the {@link String} representation of an {@link InetSocketAddress}. * <p> * The output does not include Scope ID. * @param addr {@link InetSocketAddress} to be converted to an address string * @return {@code String} containing the text-formatted IP address */ public static String toSocketAddressString(InetSocketAddress addr) { String port = String.valueOf(addr.getPort()); final StringBuilder sb; if (addr.isUnresolved()) { String hostname = getHostname(addr); sb = newSocketAddressStringBuilder(hostname, port, !isValidIpV6Address(hostname)); } else { InetAddress address = addr.getAddress(); String hostString = toAddressString(address); sb = newSocketAddressStringBuilder(hostString, port, address instanceof Inet4Address); } return sb.append(':').append(port).toString(); }
if (!NetUtil.isValidIpV4Address(maybeIP) && !NetUtil.isValidIpV6Address(maybeIP)) { i = maybeIP.lastIndexOf('.'); if (i + 1 >= maybeIP.length()) {
/** * Returns a new host endpoint with the specified IP address. * * @return the new endpoint with the specified IP address. * {@code this} if this endpoint has the same IP address. * * @throws IllegalStateException if this endpoint is not a host but a group */ public Endpoint withIpAddr(@Nullable String ipAddr) { ensureSingle(); if (ipAddr == null) { return withoutIpAddr(); } if (NetUtil.isValidIpV4Address(ipAddr)) { return withIpAddr(ipAddr, StandardProtocolFamily.INET); } if (NetUtil.isValidIpV6Address(ipAddr)) { if (ipAddr.charAt(0) == '[') { ipAddr = ipAddr.substring(1, ipAddr.length() - 1); } return withIpAddr(ipAddr, StandardProtocolFamily.INET6); } throw new IllegalArgumentException("ipAddr: " + ipAddr + " (expected: an IP address)"); }
private static Endpoint create(String host, int port) { requireNonNull(host, "host"); if (NetUtil.isValidIpV4Address(host)) { return new Endpoint(host, host, port, DEFAULT_WEIGHT, HostType.IPv4_ONLY); } if (NetUtil.isValidIpV6Address(host)) { final String ipV6Addr; if (host.charAt(0) == '[') { // Strip surrounding '[' and ']'. ipV6Addr = host.substring(1, host.length() - 1); } else { ipV6Addr = host; } return new Endpoint(ipV6Addr, ipV6Addr, port, DEFAULT_WEIGHT, HostType.IPv6_ONLY); } return new Endpoint(InternetDomainName.from(host).toString(), null, port, DEFAULT_WEIGHT, HostType.HOSTNAME_ONLY); }
private void sendConnectCommand(ChannelHandlerContext ctx) throws Exception { InetSocketAddress raddr = destinationAddress(); Socks5AddressType addrType; String rhost; if (raddr.isUnresolved()) { addrType = Socks5AddressType.DOMAIN; rhost = raddr.getHostString(); } else { rhost = raddr.getAddress().getHostAddress(); if (NetUtil.isValidIpV4Address(rhost)) { addrType = Socks5AddressType.IPv4; } else if (NetUtil.isValidIpV6Address(rhost)) { addrType = Socks5AddressType.IPv6; } else { throw new ProxyConnectException( exceptionMessage("unknown address type: " + StringUtil.simpleClassName(rhost))); } } ctx.pipeline().replace(decoderName, decoderName, new Socks5CommandResponseDecoder()); sendToProxyServer(new DefaultSocks5CommandRequest(Socks5CommandType.CONNECT, addrType, rhost, raddr.getPort())); } }