forked from I2P_Developers/i2p.i2p
Transport: Add util methods for AddressType
WIP, not hooked in
This commit is contained in:
@@ -17,6 +17,7 @@ import java.util.regex.Pattern;
|
||||
|
||||
import net.i2p.I2PAppContext;
|
||||
import net.i2p.data.router.RouterAddress;
|
||||
import net.i2p.util.AddressType;
|
||||
import net.i2p.util.Log;
|
||||
import net.i2p.router.RouterContext;
|
||||
|
||||
@@ -145,6 +146,49 @@ public abstract class TransportUtil {
|
||||
return host != null && YGGDRASIL_PATTERN.matcher(host).matches();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return null if unknown
|
||||
* @since 0.9.54
|
||||
*/
|
||||
public static AddressType getType(RouterAddress addr) {
|
||||
String host = addr.getHost();
|
||||
return getType(host);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return null if unknown
|
||||
* @since 0.9.54
|
||||
*/
|
||||
public static AddressType getType(String host) {
|
||||
if (host == null)
|
||||
return null;
|
||||
if (host.indexOf('.') > 0)
|
||||
return AddressType.IPV4;
|
||||
if (host.indexOf(':') >= 0) {
|
||||
if (YGGDRASIL_PATTERN.matcher(host).matches())
|
||||
return AddressType.YGG;
|
||||
return AddressType.IPV6;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return null if unknown
|
||||
* @since 0.9.54
|
||||
*/
|
||||
public static AddressType getType(byte[] ip) {
|
||||
if (ip == null)
|
||||
return null;
|
||||
if (ip.length == 4)
|
||||
return AddressType.IPV4;
|
||||
if (ip.length == 16) {
|
||||
if (ip[0] == 2 || ip[0] == 3)
|
||||
return AddressType.YGG;
|
||||
return AddressType.IPV6;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param addr non-null
|
||||
* @since IPv6 moved from TransportImpl
|
||||
|
||||
Reference in New Issue
Block a user