forked from I2P_Developers/i2p.i2p
Util: Add methods to validate IP addresses
This commit is contained in:
@@ -29,8 +29,6 @@ import javax.crypto.spec.DHParameterSpec;
|
||||
import javax.crypto.spec.DHPublicKeySpec;
|
||||
import javax.security.auth.x500.X500Principal;
|
||||
|
||||
import org.apache.http.conn.util.InetAddressUtils;
|
||||
|
||||
import static net.i2p.crypto.SigUtil.intToASN1;
|
||||
import net.i2p.data.DataHelper;
|
||||
import net.i2p.data.Signature;
|
||||
@@ -578,9 +576,9 @@ public final class SelfSignedGenerator {
|
||||
}
|
||||
for (String n : altNames) {
|
||||
int len;
|
||||
if (InetAddressUtils.isIPv4Address(n))
|
||||
if (Addresses.isIPv4Address(n))
|
||||
len = 4;
|
||||
else if (InetAddressUtils.isIPv6Address(n))
|
||||
else if (Addresses.isIPv6Address(n))
|
||||
len = 16;
|
||||
else
|
||||
len = n.length();
|
||||
@@ -690,8 +688,8 @@ public final class SelfSignedGenerator {
|
||||
idx = intToASN1(rv, idx, wrap41len);
|
||||
for (String n : altNames) {
|
||||
byte[] b;
|
||||
if (InetAddressUtils.isIPv4Address(n) ||
|
||||
InetAddressUtils.isIPv6Address(n)) {
|
||||
if (Addresses.isIPv4Address(n) ||
|
||||
Addresses.isIPv6Address(n)) {
|
||||
b = Addresses.getIP(n);
|
||||
if (b == null) // shouldn't happen
|
||||
throw new IllegalArgumentException("fail " + n);
|
||||
|
@@ -14,9 +14,8 @@ import java.io.OutputStream;
|
||||
import java.net.InetAddress;
|
||||
import java.net.Socket;
|
||||
|
||||
import org.apache.http.conn.util.InetAddressUtils;
|
||||
|
||||
import static net.i2p.socks.SOCKS4Constants.*;
|
||||
import net.i2p.util.Addresses;
|
||||
|
||||
/**
|
||||
* A simple SOCKS 4/4a client.
|
||||
@@ -73,10 +72,10 @@ public class SOCKS4Client {
|
||||
out.writeByte(Command.CONNECT);
|
||||
out.writeShort(connPort);
|
||||
boolean isIPv4;
|
||||
if (InetAddressUtils.isIPv4Address(connHostName)) {
|
||||
if (Addresses.isIPv4Address(connHostName)) {
|
||||
isIPv4 = true;
|
||||
out.write(InetAddress.getByName(connHostName).getAddress());
|
||||
} else if (InetAddressUtils.isIPv6Address(connHostName)) {
|
||||
} else if (Addresses.isIPv6Address(connHostName)) {
|
||||
throw new SOCKSException("IPv6 not supported in SOCKS 4");
|
||||
} else {
|
||||
isIPv4 = false;
|
||||
|
@@ -14,9 +14,8 @@ import java.io.OutputStream;
|
||||
import java.net.InetAddress;
|
||||
import java.net.Socket;
|
||||
|
||||
import org.apache.http.conn.util.InetAddressUtils;
|
||||
|
||||
import static net.i2p.socks.SOCKS5Constants.*;
|
||||
import net.i2p.util.Addresses;
|
||||
|
||||
/**
|
||||
* A simple SOCKS 5 client.
|
||||
@@ -152,9 +151,9 @@ public class SOCKS5Client {
|
||||
}
|
||||
|
||||
int addressType;
|
||||
if (InetAddressUtils.isIPv4Address(connHostName))
|
||||
if (Addresses.isIPv4Address(connHostName))
|
||||
addressType = AddressType.IPV4;
|
||||
else if (InetAddressUtils.isIPv6Address(connHostName))
|
||||
else if (Addresses.isIPv6Address(connHostName))
|
||||
addressType = AddressType.IPV6;
|
||||
else
|
||||
addressType = AddressType.DOMAINNAME;
|
||||
|
@@ -343,8 +343,7 @@ public abstract class Addresses {
|
||||
//I2PAppContext.getGlobalContext().logManager().getLog(Addresses.class).error("lookup of " + host, new Exception("I did it"));
|
||||
try {
|
||||
rv = InetAddress.getByName(host).getAddress();
|
||||
if (InetAddressUtils.isIPv4Address(host) ||
|
||||
InetAddressUtils.isIPv6Address(host)) {
|
||||
if (isIPAddress(host)) {
|
||||
synchronized (_IPAddress) {
|
||||
_IPAddress.put(host, rv);
|
||||
}
|
||||
@@ -379,8 +378,7 @@ public abstract class Addresses {
|
||||
rv = _IPAddress.get(host);
|
||||
}
|
||||
if (rv == null) {
|
||||
if (InetAddressUtils.isIPv4Address(host) ||
|
||||
InetAddressUtils.isIPv6Address(host)) {
|
||||
if (isIPAddress(host)) {
|
||||
try {
|
||||
rv = InetAddress.getByName(host).getAddress();
|
||||
synchronized (_IPAddress) {
|
||||
@@ -407,7 +405,7 @@ public abstract class Addresses {
|
||||
public static byte[] getIP(String host, boolean preferIPv6) {
|
||||
if (host == null)
|
||||
return null;
|
||||
if (InetAddressUtils.isIPv4Address(host) || InetAddressUtils.isIPv6Address(host))
|
||||
if (isIPAddress(host))
|
||||
return getIP(host);
|
||||
synchronized(_negativeCache) {
|
||||
Long when = _negativeCache.get(host);
|
||||
@@ -460,7 +458,7 @@ public abstract class Addresses {
|
||||
public static List<byte[]> getIPs(String host) {
|
||||
if (host == null)
|
||||
return null;
|
||||
if (InetAddressUtils.isIPv4Address(host) || InetAddressUtils.isIPv6Address(host)) {
|
||||
if (isIPAddress(host)) {
|
||||
byte[] brv = getIP(host);
|
||||
if (brv == null)
|
||||
return null;
|
||||
@@ -491,6 +489,31 @@ public abstract class Addresses {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 0.9.34
|
||||
*/
|
||||
public static boolean isIPv4Address(String host) {
|
||||
return InetAddressUtils.isIPv4Address(host);
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 0.9.34
|
||||
*/
|
||||
public static boolean isIPv6Address(String host) {
|
||||
return InetAddressUtils.isIPv6Address(host);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return true if either IPv4 or IPv6
|
||||
* @since 0.9.34
|
||||
*/
|
||||
public static boolean isIPAddress(String host) {
|
||||
return InetAddressUtils.isIPv4Address(host) || InetAddressUtils.isIPv6Address(host);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//////// IPv6 Cache Utils ///////
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user