forked from I2P_Developers/i2p.i2p
Util: Add methods to validate IP addresses
This commit is contained in:
@ -10,10 +10,9 @@ import javax.servlet.http.HttpServletRequest;
|
|||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
import net.i2p.I2PAppContext;
|
import net.i2p.I2PAppContext;
|
||||||
|
import net.i2p.util.Addresses;
|
||||||
import net.i2p.util.Log;
|
import net.i2p.util.Log;
|
||||||
|
|
||||||
import org.apache.http.conn.util.InetAddressUtils;
|
|
||||||
|
|
||||||
import org.eclipse.jetty.server.Request;
|
import org.eclipse.jetty.server.Request;
|
||||||
import org.eclipse.jetty.server.handler.AbstractHandler;
|
import org.eclipse.jetty.server.handler.AbstractHandler;
|
||||||
|
|
||||||
@ -109,7 +108,7 @@ public class HostCheckHandler extends AbstractHandler {
|
|||||||
if (_listenHosts.contains(host))
|
if (_listenHosts.contains(host))
|
||||||
return true;
|
return true;
|
||||||
// allow all IP addresses
|
// allow all IP addresses
|
||||||
if (InetAddressUtils.isIPv4Address(host) || InetAddressUtils.isIPv6Address(host))
|
if (Addresses.isIPAddress(host))
|
||||||
return true;
|
return true;
|
||||||
//System.out.println(host + " not found in " + s);
|
//System.out.println(host + " not found in " + s);
|
||||||
return false;
|
return false;
|
||||||
|
@ -10,11 +10,10 @@ import javax.servlet.http.HttpServletResponse;
|
|||||||
|
|
||||||
import net.i2p.I2PAppContext;
|
import net.i2p.I2PAppContext;
|
||||||
import net.i2p.data.DataHelper;
|
import net.i2p.data.DataHelper;
|
||||||
|
import net.i2p.util.Addresses;
|
||||||
import net.i2p.util.Log;
|
import net.i2p.util.Log;
|
||||||
import net.i2p.util.PortMapper;
|
import net.i2p.util.PortMapper;
|
||||||
|
|
||||||
import org.apache.http.conn.util.InetAddressUtils;
|
|
||||||
|
|
||||||
import org.eclipse.jetty.server.Request;
|
import org.eclipse.jetty.server.Request;
|
||||||
import org.eclipse.jetty.server.handler.HandlerWrapper;
|
import org.eclipse.jetty.server.handler.HandlerWrapper;
|
||||||
|
|
||||||
@ -128,7 +127,7 @@ public class HostCheckHandler extends HandlerWrapper
|
|||||||
if (_listenHosts.contains(host))
|
if (_listenHosts.contains(host))
|
||||||
return true;
|
return true;
|
||||||
// allow all IP addresses
|
// allow all IP addresses
|
||||||
if (InetAddressUtils.isIPv4Address(host) || InetAddressUtils.isIPv6Address(host))
|
if (Addresses.isIPAddress(host))
|
||||||
return true;
|
return true;
|
||||||
//System.out.println(host + " not found in " + s);
|
//System.out.println(host + " not found in " + s);
|
||||||
return false;
|
return false;
|
||||||
|
@ -42,8 +42,6 @@ import net.i2p.util.SecureDirectory;
|
|||||||
import net.i2p.util.I2PSSLSocketFactory;
|
import net.i2p.util.I2PSSLSocketFactory;
|
||||||
import net.i2p.util.SystemVersion;
|
import net.i2p.util.SystemVersion;
|
||||||
|
|
||||||
import org.apache.http.conn.util.InetAddressUtils;
|
|
||||||
|
|
||||||
import org.eclipse.jetty.security.HashLoginService;
|
import org.eclipse.jetty.security.HashLoginService;
|
||||||
import org.eclipse.jetty.security.ConstraintMapping;
|
import org.eclipse.jetty.security.ConstraintMapping;
|
||||||
import org.eclipse.jetty.security.ConstraintSecurityHandler;
|
import org.eclipse.jetty.security.ConstraintSecurityHandler;
|
||||||
@ -518,9 +516,9 @@ public class RouterConsoleRunner implements RouterApp {
|
|||||||
try {
|
try {
|
||||||
// Test before we add the connector, because Jetty 6 won't start if any of the
|
// Test before we add the connector, because Jetty 6 won't start if any of the
|
||||||
// connectors are bad
|
// connectors are bad
|
||||||
if ((!hasIPV6) && InetAddressUtils.isIPv6Address(host))
|
if ((!hasIPV6) && Addresses.isIPv6Address(host))
|
||||||
throw new IOException("IPv6 addresses unsupported");
|
throw new IOException("IPv6 addresses unsupported");
|
||||||
if ((!hasIPV4) && InetAddressUtils.isIPv4Address(host))
|
if ((!hasIPV4) && Addresses.isIPv4Address(host))
|
||||||
throw new IOException("IPv4 addresses unsupported");
|
throw new IOException("IPv4 addresses unsupported");
|
||||||
ServerSocket testSock = null;
|
ServerSocket testSock = null;
|
||||||
try {
|
try {
|
||||||
@ -608,9 +606,9 @@ public class RouterConsoleRunner implements RouterApp {
|
|||||||
try {
|
try {
|
||||||
// Test before we add the connector, because Jetty 6 won't start if any of the
|
// Test before we add the connector, because Jetty 6 won't start if any of the
|
||||||
// connectors are bad
|
// connectors are bad
|
||||||
if ((!hasIPV6) && InetAddressUtils.isIPv6Address(host))
|
if ((!hasIPV6) && Addresses.isIPv6Address(host))
|
||||||
throw new IOException("IPv6 addresses unsupported");
|
throw new IOException("IPv6 addresses unsupported");
|
||||||
if ((!hasIPV4) && InetAddressUtils.isIPv4Address(host))
|
if ((!hasIPV4) && Addresses.isIPv4Address(host))
|
||||||
throw new IOException("IPv4 addresses unsupported");
|
throw new IOException("IPv4 addresses unsupported");
|
||||||
ServerSocket testSock = null;
|
ServerSocket testSock = null;
|
||||||
try {
|
try {
|
||||||
|
@ -29,8 +29,6 @@ import javax.crypto.spec.DHParameterSpec;
|
|||||||
import javax.crypto.spec.DHPublicKeySpec;
|
import javax.crypto.spec.DHPublicKeySpec;
|
||||||
import javax.security.auth.x500.X500Principal;
|
import javax.security.auth.x500.X500Principal;
|
||||||
|
|
||||||
import org.apache.http.conn.util.InetAddressUtils;
|
|
||||||
|
|
||||||
import static net.i2p.crypto.SigUtil.intToASN1;
|
import static net.i2p.crypto.SigUtil.intToASN1;
|
||||||
import net.i2p.data.DataHelper;
|
import net.i2p.data.DataHelper;
|
||||||
import net.i2p.data.Signature;
|
import net.i2p.data.Signature;
|
||||||
@ -578,9 +576,9 @@ public final class SelfSignedGenerator {
|
|||||||
}
|
}
|
||||||
for (String n : altNames) {
|
for (String n : altNames) {
|
||||||
int len;
|
int len;
|
||||||
if (InetAddressUtils.isIPv4Address(n))
|
if (Addresses.isIPv4Address(n))
|
||||||
len = 4;
|
len = 4;
|
||||||
else if (InetAddressUtils.isIPv6Address(n))
|
else if (Addresses.isIPv6Address(n))
|
||||||
len = 16;
|
len = 16;
|
||||||
else
|
else
|
||||||
len = n.length();
|
len = n.length();
|
||||||
@ -690,8 +688,8 @@ public final class SelfSignedGenerator {
|
|||||||
idx = intToASN1(rv, idx, wrap41len);
|
idx = intToASN1(rv, idx, wrap41len);
|
||||||
for (String n : altNames) {
|
for (String n : altNames) {
|
||||||
byte[] b;
|
byte[] b;
|
||||||
if (InetAddressUtils.isIPv4Address(n) ||
|
if (Addresses.isIPv4Address(n) ||
|
||||||
InetAddressUtils.isIPv6Address(n)) {
|
Addresses.isIPv6Address(n)) {
|
||||||
b = Addresses.getIP(n);
|
b = Addresses.getIP(n);
|
||||||
if (b == null) // shouldn't happen
|
if (b == null) // shouldn't happen
|
||||||
throw new IllegalArgumentException("fail " + n);
|
throw new IllegalArgumentException("fail " + n);
|
||||||
|
@ -14,9 +14,8 @@ import java.io.OutputStream;
|
|||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
|
||||||
import org.apache.http.conn.util.InetAddressUtils;
|
|
||||||
|
|
||||||
import static net.i2p.socks.SOCKS4Constants.*;
|
import static net.i2p.socks.SOCKS4Constants.*;
|
||||||
|
import net.i2p.util.Addresses;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A simple SOCKS 4/4a client.
|
* A simple SOCKS 4/4a client.
|
||||||
@ -73,10 +72,10 @@ public class SOCKS4Client {
|
|||||||
out.writeByte(Command.CONNECT);
|
out.writeByte(Command.CONNECT);
|
||||||
out.writeShort(connPort);
|
out.writeShort(connPort);
|
||||||
boolean isIPv4;
|
boolean isIPv4;
|
||||||
if (InetAddressUtils.isIPv4Address(connHostName)) {
|
if (Addresses.isIPv4Address(connHostName)) {
|
||||||
isIPv4 = true;
|
isIPv4 = true;
|
||||||
out.write(InetAddress.getByName(connHostName).getAddress());
|
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");
|
throw new SOCKSException("IPv6 not supported in SOCKS 4");
|
||||||
} else {
|
} else {
|
||||||
isIPv4 = false;
|
isIPv4 = false;
|
||||||
|
@ -14,9 +14,8 @@ import java.io.OutputStream;
|
|||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
|
||||||
import org.apache.http.conn.util.InetAddressUtils;
|
|
||||||
|
|
||||||
import static net.i2p.socks.SOCKS5Constants.*;
|
import static net.i2p.socks.SOCKS5Constants.*;
|
||||||
|
import net.i2p.util.Addresses;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A simple SOCKS 5 client.
|
* A simple SOCKS 5 client.
|
||||||
@ -152,9 +151,9 @@ public class SOCKS5Client {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int addressType;
|
int addressType;
|
||||||
if (InetAddressUtils.isIPv4Address(connHostName))
|
if (Addresses.isIPv4Address(connHostName))
|
||||||
addressType = AddressType.IPV4;
|
addressType = AddressType.IPV4;
|
||||||
else if (InetAddressUtils.isIPv6Address(connHostName))
|
else if (Addresses.isIPv6Address(connHostName))
|
||||||
addressType = AddressType.IPV6;
|
addressType = AddressType.IPV6;
|
||||||
else
|
else
|
||||||
addressType = AddressType.DOMAINNAME;
|
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"));
|
//I2PAppContext.getGlobalContext().logManager().getLog(Addresses.class).error("lookup of " + host, new Exception("I did it"));
|
||||||
try {
|
try {
|
||||||
rv = InetAddress.getByName(host).getAddress();
|
rv = InetAddress.getByName(host).getAddress();
|
||||||
if (InetAddressUtils.isIPv4Address(host) ||
|
if (isIPAddress(host)) {
|
||||||
InetAddressUtils.isIPv6Address(host)) {
|
|
||||||
synchronized (_IPAddress) {
|
synchronized (_IPAddress) {
|
||||||
_IPAddress.put(host, rv);
|
_IPAddress.put(host, rv);
|
||||||
}
|
}
|
||||||
@ -379,8 +378,7 @@ public abstract class Addresses {
|
|||||||
rv = _IPAddress.get(host);
|
rv = _IPAddress.get(host);
|
||||||
}
|
}
|
||||||
if (rv == null) {
|
if (rv == null) {
|
||||||
if (InetAddressUtils.isIPv4Address(host) ||
|
if (isIPAddress(host)) {
|
||||||
InetAddressUtils.isIPv6Address(host)) {
|
|
||||||
try {
|
try {
|
||||||
rv = InetAddress.getByName(host).getAddress();
|
rv = InetAddress.getByName(host).getAddress();
|
||||||
synchronized (_IPAddress) {
|
synchronized (_IPAddress) {
|
||||||
@ -407,7 +405,7 @@ public abstract class Addresses {
|
|||||||
public static byte[] getIP(String host, boolean preferIPv6) {
|
public static byte[] getIP(String host, boolean preferIPv6) {
|
||||||
if (host == null)
|
if (host == null)
|
||||||
return null;
|
return null;
|
||||||
if (InetAddressUtils.isIPv4Address(host) || InetAddressUtils.isIPv6Address(host))
|
if (isIPAddress(host))
|
||||||
return getIP(host);
|
return getIP(host);
|
||||||
synchronized(_negativeCache) {
|
synchronized(_negativeCache) {
|
||||||
Long when = _negativeCache.get(host);
|
Long when = _negativeCache.get(host);
|
||||||
@ -460,7 +458,7 @@ public abstract class Addresses {
|
|||||||
public static List<byte[]> getIPs(String host) {
|
public static List<byte[]> getIPs(String host) {
|
||||||
if (host == null)
|
if (host == null)
|
||||||
return null;
|
return null;
|
||||||
if (InetAddressUtils.isIPv4Address(host) || InetAddressUtils.isIPv6Address(host)) {
|
if (isIPAddress(host)) {
|
||||||
byte[] brv = getIP(host);
|
byte[] brv = getIP(host);
|
||||||
if (brv == null)
|
if (brv == null)
|
||||||
return null;
|
return null;
|
||||||
@ -491,6 +489,31 @@ public abstract class Addresses {
|
|||||||
return null;
|
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 ///////
|
//////// IPv6 Cache Utils ///////
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -17,8 +17,6 @@ import java.util.Date;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
import org.apache.http.conn.util.InetAddressUtils;
|
|
||||||
|
|
||||||
import net.i2p.data.DataFormatException;
|
import net.i2p.data.DataFormatException;
|
||||||
import net.i2p.data.DataHelper;
|
import net.i2p.data.DataHelper;
|
||||||
import net.i2p.data.DataStructureImpl;
|
import net.i2p.data.DataStructureImpl;
|
||||||
|
@ -24,8 +24,6 @@ import java.util.TreeSet;
|
|||||||
import java.util.Vector;
|
import java.util.Vector;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
import org.apache.http.conn.util.InetAddressUtils;
|
|
||||||
|
|
||||||
import net.i2p.crypto.SigType;
|
import net.i2p.crypto.SigType;
|
||||||
import net.i2p.data.DataHelper;
|
import net.i2p.data.DataHelper;
|
||||||
import net.i2p.data.Hash;
|
import net.i2p.data.Hash;
|
||||||
@ -1002,12 +1000,12 @@ public class NTCPTransport extends TransportImpl {
|
|||||||
String h = hosts[i];
|
String h = hosts[i];
|
||||||
if (h.length() <= 0)
|
if (h.length() <= 0)
|
||||||
continue;
|
continue;
|
||||||
if (InetAddressUtils.isIPv4Address(h)) {
|
if (Addresses.isIPv4Address(h)) {
|
||||||
if (v4)
|
if (v4)
|
||||||
continue;
|
continue;
|
||||||
v4 = true;
|
v4 = true;
|
||||||
ipstrings.add(h);
|
ipstrings.add(h);
|
||||||
} else if (InetAddressUtils.isIPv6Address(h)) {
|
} else if (Addresses.isIPv6Address(h)) {
|
||||||
if (v6)
|
if (v6)
|
||||||
continue;
|
continue;
|
||||||
v6 = true;
|
v6 = true;
|
||||||
|
@ -4,12 +4,11 @@ import java.net.InetAddress;
|
|||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.apache.http.conn.util.InetAddressUtils;
|
|
||||||
|
|
||||||
import net.i2p.data.Base64;
|
import net.i2p.data.Base64;
|
||||||
import net.i2p.data.router.RouterAddress;
|
import net.i2p.data.router.RouterAddress;
|
||||||
import net.i2p.data.SessionKey;
|
import net.i2p.data.SessionKey;
|
||||||
import net.i2p.router.transport.TransportUtil;
|
import net.i2p.router.transport.TransportUtil;
|
||||||
|
import net.i2p.util.Addresses;
|
||||||
import net.i2p.util.LHMCache;
|
import net.i2p.util.LHMCache;
|
||||||
import net.i2p.util.SystemVersion;
|
import net.i2p.util.SystemVersion;
|
||||||
|
|
||||||
@ -339,8 +338,7 @@ class UDPAddress {
|
|||||||
rv = _inetAddressCache.get(host);
|
rv = _inetAddressCache.get(host);
|
||||||
}
|
}
|
||||||
if (rv == null) {
|
if (rv == null) {
|
||||||
if (InetAddressUtils.isIPv4Address(host) ||
|
if (Addresses.isIPAddress(host)) {
|
||||||
InetAddressUtils.isIPv6Address(host)) {
|
|
||||||
try {
|
try {
|
||||||
rv = InetAddress.getByName(host);
|
rv = InetAddress.getByName(host);
|
||||||
synchronized (_inetAddressCache) {
|
synchronized (_inetAddressCache) {
|
||||||
|
@ -20,8 +20,6 @@ import java.util.concurrent.atomic.AtomicLong;
|
|||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.CopyOnWriteArrayList;
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
|
|
||||||
import org.apache.http.conn.util.InetAddressUtils;
|
|
||||||
|
|
||||||
import net.i2p.crypto.SigType;
|
import net.i2p.crypto.SigType;
|
||||||
import net.i2p.data.DatabaseEntry;
|
import net.i2p.data.DatabaseEntry;
|
||||||
import net.i2p.data.DataHelper;
|
import net.i2p.data.DataHelper;
|
||||||
@ -2084,11 +2082,11 @@ public class UDPTransport extends TransportImpl implements TimedWeightedPriority
|
|||||||
String h = hosts[i];
|
String h = hosts[i];
|
||||||
if (h.length() <= 0)
|
if (h.length() <= 0)
|
||||||
continue;
|
continue;
|
||||||
if (InetAddressUtils.isIPv4Address(h)) {
|
if (Addresses.isIPv4Address(h)) {
|
||||||
if (v4)
|
if (v4)
|
||||||
continue;
|
continue;
|
||||||
v4 = true;
|
v4 = true;
|
||||||
} else if (InetAddressUtils.isIPv6Address(h)) {
|
} else if (Addresses.isIPv6Address(h)) {
|
||||||
if (v6)
|
if (v6)
|
||||||
continue;
|
continue;
|
||||||
v6 = true;
|
v6 = true;
|
||||||
|
Reference in New Issue
Block a user