I2P Address: [http://git.idk.i2p]

Skip to content
Snippets Groups Projects
Verified Commit 8d60d68e authored by zzz's avatar zzz
Browse files

Util: Stub out yggdrasil address detection

WIP - not hooked in
parent 56fabe31
No related branches found
No related tags found
No related merge requests found
......@@ -260,6 +260,36 @@ public abstract class Addresses {
return rv;
}
/**
* Warning, very slow on Windows. Caller should cache.
*
* @return the IPv6 address with prefix 02xx: or 03xx:
* @since 0.9.49
*/
public static byte[] getYggdrasilAddress() {
if (SystemVersion.isAndroid())
return null;
try {
Enumeration<NetworkInterface> ifcs = NetworkInterface.getNetworkInterfaces();
if (ifcs != null) {
while (ifcs.hasMoreElements()) {
NetworkInterface ifc = ifcs.nextElement();
if (!ifc.isUp())
continue;
for(Enumeration<InetAddress> addrs = ifc.getInetAddresses(); addrs.hasMoreElements();) {
InetAddress addr = addrs.nextElement();
byte[] ip = addr.getAddress();
if (ip.length == 16 && (ip[0] & 0xfe) == 0x02)
return ip;
}
}
}
} catch (SocketException e) {
} catch (java.lang.Error e) {
}
return null;
}
/**
* Strip the trailing "%nn" from Inet6Address.getHostAddress()
* @since IPv6
......@@ -789,6 +819,11 @@ public abstract class Addresses {
System.out.println("\nAll External Addresses:");
a = getAddresses(false, false, true);
print(a);
byte[] ygg = getYggdrasilAddress();
if (ygg != null) {
System.out.println("\nYggdrasil Address:");
System.out.println(toString(ygg));
}
System.out.println("\nAll External and Local Addresses:");
a = getAddresses(true, false, true);
print(a);
......
......@@ -13,6 +13,7 @@ import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Pattern;
import net.i2p.I2PAppContext;
import net.i2p.data.router.RouterAddress;
......@@ -39,6 +40,8 @@ public abstract class TransportUtil {
private static final int MIN_RANDOM_PORT = 9151;
private static final int MAX_RANDOM_PORT = 30777;
private static final Pattern YGGDRASIL_PATTERN = Pattern.compile("^[2-3][0-9a-fA-F]{2}:[0-9a-fA-F:]*");
public enum IPv6Config {
/** IPv6 disabled */
IPV6_DISABLED("false"),
......@@ -128,6 +131,15 @@ public abstract class TransportUtil {
return host != null && host.contains(":");
}
/**
* @since 0.9.49
*/
public static boolean isYggdrasil(RouterAddress addr) {
// do this the fast way, without calling getIP() to parse the host string
String host = addr.getHost();
return host != null && YGGDRASIL_PATTERN.matcher(host).matches();
}
/**
* @param addr non-null
* @since IPv6 moved from TransportImpl
......@@ -290,4 +302,16 @@ public abstract class TransportUtil {
int maxPort = Math.min(65535, Math.max(minPort, ctx.getProperty(maxprop, MAX_RANDOM_PORT)));
return minPort + ctx.random().nextInt(1 + maxPort - minPort);
}
/*
public static void main(String[] args) {
java.util.Set<String> addrs = net.i2p.util.Addresses.getAddresses(true, true, true, true);
net.i2p.util.OrderedProperties props = new net.i2p.util.OrderedProperties();
RouterAddress ra = new RouterAddress("foo", props, 10);
for (String a : addrs) {
props.setProperty("host", a);
System.out.println(a + " - " + isYggdrasil(ra));
}
}
*/
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment