Refine search
/** * 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); } }
private InetSocketAddress createInetSocketAddress(InetSocketAddress resolved, String host) { byte[] addr = NetUtil.createByteArrayFromIpAddressString(resolved.getAddress().getHostAddress()); try { return new InetSocketAddress(InetAddress.getByAddress(host, addr), resolved.getPort()); } catch (UnknownHostException e) { throw new RuntimeException(e); } }
public static void initialize() { if (INITIALIZED.compareAndSet(false, true)) { initialize(NetUtil.isIpV4StackPreferred()); } }
private static boolean isValidIpV4Address(CharSequence ip, int from, int toExcluded) { return ip instanceof String ? isValidIpV4Address((String) ip, from, toExcluded) : ip instanceof AsciiString ? isValidIpV4Address((AsciiString) ip, from, toExcluded) : isValidIpV4Address0(ip, from, toExcluded); }
/** * 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); }
return ip.getHostAddress(); return toAddressString(ip.getAddress(), 0, ipv4Mapped);
@Override public String decodeAddress(Socks5AddressType addrType, ByteBuf in) throws Exception { if (addrType == Socks5AddressType.IPv4) { return NetUtil.intToIpAddress(in.readInt()); } if (addrType == Socks5AddressType.DOMAIN) { final int length = in.readUnsignedByte(); final String domain = in.toString(in.readerIndex(), length, CharsetUtil.US_ASCII); in.skipBytes(length); return domain; } if (addrType == Socks5AddressType.IPv6) { if (in.hasArray()) { final int readerIdx = in.readerIndex(); in.readerIndex(readerIdx + IPv6_LEN); return NetUtil.bytesToIpAddress(in.array(), in.arrayOffset() + readerIdx, IPv6_LEN); } else { byte[] tmp = new byte[IPv6_LEN]; in.readBytes(tmp); return NetUtil.bytesToIpAddress(tmp); } } else { throw new DecoderException("unsupported address type: " + (addrType.byteValue() & 0xFF)); } } };
@Override public void encodeAsByteBuf(ByteBuf byteBuf) { byteBuf.writeByte(protocolVersion().byteValue()); byteBuf.writeByte(cmdType.byteValue()); byteBuf.writeByte(0x00); byteBuf.writeByte(addressType.byteValue()); switch (addressType) { case IPv4: { byteBuf.writeBytes(NetUtil.createByteArrayFromIpAddressString(host)); byteBuf.writeShort(port); break; } case DOMAIN: { byteBuf.writeByte(host.length()); byteBuf.writeCharSequence(host, CharsetUtil.US_ASCII); byteBuf.writeShort(port); break; } case IPv6: { byteBuf.writeBytes(NetUtil.createByteArrayFromIpAddressString(host)); byteBuf.writeShort(port); break; } } } }
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())); } }
@Override public void encodeAddress(Socks5AddressType addrType, String addrValue, ByteBuf out) throws Exception { final byte typeVal = addrType.byteValue(); if (typeVal == Socks5AddressType.IPv4.byteValue()) { if (addrValue != null) { out.writeBytes(NetUtil.createByteArrayFromIpAddressString(addrValue)); } else { out.writeInt(0); } } else if (typeVal == Socks5AddressType.DOMAIN.byteValue()) { if (addrValue != null) { out.writeByte(addrValue.length()); out.writeCharSequence(addrValue, CharsetUtil.US_ASCII); } else { out.writeByte(1); out.writeByte(0); } } else if (typeVal == Socks5AddressType.IPv6.byteValue()) { if (addrValue != null) { out.writeBytes(NetUtil.createByteArrayFromIpAddressString(addrValue)); } else { out.writeLong(0); out.writeLong(0); } } else { throw new EncoderException("unsupported addrType: " + (addrType.byteValue() & 0xFF)); } } };
private static InetSocketAddress toRemoteAddress(PoolKey key) throws UnknownHostException { final InetAddress inetAddr = InetAddress.getByAddress( key.host, NetUtil.createByteArrayFromIpAddressString(key.ipAddr)); return new InetSocketAddress(inetAddr, key.port); }
.map(DnsRawRecord.class::cast) .anyMatch(r -> r.type() == DnsRecordType.A && r.content().getByte(r.content().readerIndex()) == 127); final int contentLen = content.readableBytes(); ipAddr = NetUtil.bytesToIpAddress(addrBytes, 12, 4); ipAddr = NetUtil.bytesToIpAddress(addrBytes, 12, 4); } else { ipAddr = NetUtil.bytesToIpAddress(addrBytes); ipAddr = NetUtil.bytesToIpAddress(addrBytes); ipAddr = NetUtil.bytesToIpAddress(addrBytes);
if (!NetUtil.isValidIpV4Address(bndAddr)) { throw new IllegalArgumentException("bndAddr: " + bndAddr + " (expected: a valid IPv4 address)"); bndAddr = IDN.toASCII(bndAddr); if (bndAddr.length() > 255) { throw new IllegalArgumentException("bndAddr: " + bndAddr + " (expected: less than 256 chars)"); if (!NetUtil.isValidIpV6Address(bndAddr)) { throw new IllegalArgumentException("bndAddr: " + bndAddr + " (expected: a valid IPv6 address)");
/** * 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(); }
/** * 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; } }
/** * Takes a {@link String} and parses it to see if it is a valid IPV4 address. * * @return true, if the string represents an IPV4 address in dotted * notation, false otherwise */ public static boolean isValidIpV4Address(String ip) { return isValidIpV4Address(ip, 0, ip.length()); }
public static boolean isValidIpV6Address(String ip) { return isValidIpV6Address((CharSequence) ip); }
static CharSequence websocketHostValue(URI wsURL) { int port = wsURL.getPort(); if (port == -1) { return wsURL.getHost(); } String host = wsURL.getHost(); if (port == HttpScheme.HTTP.port()) { return HttpScheme.HTTP.name().contentEquals(wsURL.getScheme()) || WebSocketScheme.WS.name().contentEquals(wsURL.getScheme()) ? host : NetUtil.toSocketAddressString(host, port); } if (port == HttpScheme.HTTPS.port()) { return HttpScheme.HTTPS.name().contentEquals(wsURL.getScheme()) || WebSocketScheme.WSS.name().contentEquals(wsURL.getScheme()) ? host : NetUtil.toSocketAddressString(host, port); } // if the port is not standard (80/443) its needed to add the port to the header. // See http://tools.ietf.org/html/rfc6454#section-6.2 return NetUtil.toSocketAddressString(host, port); }