if (isValidIpV4Address(ipAddressString)) { StringTokenizer tokenizer = new StringTokenizer(ipAddressString, "."); String token; if (isValidIpV6Address(ipAddressString)) { if (ipAddressString.charAt(0) == '[') { ipAddressString = ipAddressString.substring(1, ipAddressString.length() - 1); convertToBytes(hexStrings.get(i), ipByteArray, i << 1);
/** * Returns the {@link Inet6Address} representation of a {@link CharSequence} IP address. * <p> * This method will treat all IPv4 type addresses as "IPv4 mapped" (see {@link #getByName(CharSequence, boolean)}) * @param ip {@link CharSequence} IP address to be converted to a {@link Inet6Address} * @return {@link Inet6Address} representation of the {@code ip} or {@code null} if not a valid IP address. */ public static Inet6Address getByName(CharSequence ip) { return getByName(ip, true); }
/** * Converts a 4 character hex word into a 2 byte word equivalent */ private static void convertToBytes(String hexWord, byte[] ipByteArray, int byteIndex) { int hexWordLength = hexWord.length(); int hexWordIndex = 0; ipByteArray[byteIndex] = 0; ipByteArray[byteIndex + 1] = 0; int charValue; // high order 4 bits of first byte if (hexWordLength > 3) { charValue = getIntValue(hexWord.charAt(hexWordIndex ++)); ipByteArray[byteIndex] |= charValue << 4; } // low order 4 bits of the first byte if (hexWordLength > 2) { charValue = getIntValue(hexWord.charAt(hexWordIndex ++)); ipByteArray[byteIndex] |= charValue; } // high order 4 bits of second byte if (hexWordLength > 1) { charValue = getIntValue(hexWord.charAt(hexWordIndex ++)); ipByteArray[byteIndex + 1] |= charValue << 4; } // low order 4 bits of the first byte charValue = getIntValue(hexWord.charAt(hexWordIndex)); ipByteArray[byteIndex + 1] |= charValue & 15; }
return false; if (!isValidIp4Word(word.toString())) { return false; return false; if (!isValidHexChar(c)) { return false; if (numberOfPeriods != 3 || !(isValidIp4Word(word.toString()) && numberOfColons < 7)) { return false;
|| currentIndex >= bytes.length || begin < 0 || (begin == 0 && (i == 3 && (!isValidNumericChar(ip.charAt(2)) || !isValidNumericChar(ip.charAt(1)) || !isValidNumericChar(ip.charAt(0))) || i == 2 && (!isValidNumericChar(ip.charAt(1)) || !isValidNumericChar(ip.charAt(0))) || break; default: if (!isValidHexChar(c) || (ipv4Seperators > 0 && !isValidNumericChar(c))) { return null; value += getIntValue(c) << ((i - begin) << 2); break;
if (!NetUtil.isValidIpV4Address(host)) { throw new IllegalArgumentException(host + " is not a valid IPv4 address"); if (!NetUtil.isValidIpV6Address(host)) { throw new IllegalArgumentException(host + " is not a valid IPv6 address");
if (inRangeEndExclusive(0, shortestStart, shortestEnd)) { b.append("::"); isIpv4Mapped = ipv4Mapped && (shortestEnd == 5 && words[5] == 0xffff); if (!inRangeEndExclusive(i, shortestStart, shortestEnd)) { if (!inRangeEndExclusive(i - 1, shortestStart, shortestEnd)) { b.append(Integer.toHexString(words[i])); } else if (!inRangeEndExclusive(i - 1, shortestStart, shortestEnd)) {
@Override public void encodeAsByteBuf(ChannelBuffer channelBuffer) throws Exception { channelBuffer.writeByte(getProtocolVersion().getByteValue()); channelBuffer.writeByte(cmdType.getByteValue()); channelBuffer.writeByte(0x00); channelBuffer.writeByte(addressType.getByteValue()); switch (addressType) { case IPv4: { channelBuffer.writeBytes(NetUtil.createByteArrayFromIpAddressString(host)); channelBuffer.writeShort(port); break; } case DOMAIN: { channelBuffer.writeByte(host.length()); channelBuffer.writeBytes(host.getBytes("US-ASCII")); channelBuffer.writeShort(port); break; } case IPv6: { channelBuffer.writeBytes(NetUtil.createByteArrayFromIpAddressString(host)); channelBuffer.writeShort(port); break; } } } }
return false; if (!isValidIp4Word(word.toString())) { return false; return false; if (!isValidHexChar(c)) { return false; if (numberOfPeriods != 3 || !(isValidIp4Word(word.toString()) && numberOfColons < 7)) { return false;
String ipAddressString) { if (isValidIpV4Address(ipAddressString)) { StringTokenizer tokenizer = new StringTokenizer(ipAddressString, "."); if (isValidIpV6Address(ipAddressString)) { if (ipAddressString.charAt(0) == '[') { ipAddressString = ipAddressString.substring(1, ipAddressString convertToBytes(hexStrings.get(i), ipByteArray, i * 2);
/** * Converts a 4 character hex word into a 2 byte word equivalent */ private static void convertToBytes(String hexWord, byte[] ipByteArray, int byteIndex) { int hexWordLength = hexWord.length(); int hexWordIndex = 0; ipByteArray[byteIndex] = 0; ipByteArray[byteIndex + 1] = 0; int charValue; // high order 4 bits of first byte if (hexWordLength > 3) { charValue = getIntValue(hexWord.charAt(hexWordIndex++)); ipByteArray[byteIndex] |= charValue << 4; } // low order 4 bits of the first byte if (hexWordLength > 2) { charValue = getIntValue(hexWord.charAt(hexWordIndex++)); ipByteArray[byteIndex] |= charValue; } // high order 4 bits of second byte if (hexWordLength > 1) { charValue = getIntValue(hexWord.charAt(hexWordIndex++)); ipByteArray[byteIndex + 1] |= charValue << 4; } // low order 4 bits of the first byte charValue = getIntValue(hexWord.charAt(hexWordIndex)); ipByteArray[byteIndex + 1] |= charValue & 15; }