From 7cf98959084e1dab25f672974e186d9f13f6db72 Mon Sep 17 00:00:00 2001 From: zzz <zzz@i2pmail.org> Date: Mon, 28 Mar 2022 06:38:18 -0400 Subject: [PATCH] Transport: Add util methods for AddressType WIP, not hooked in --- .../i2p/router/transport/TransportUtil.java | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/router/java/src/net/i2p/router/transport/TransportUtil.java b/router/java/src/net/i2p/router/transport/TransportUtil.java index 2e4565d202..ce3877250d 100644 --- a/router/java/src/net/i2p/router/transport/TransportUtil.java +++ b/router/java/src/net/i2p/router/transport/TransportUtil.java @@ -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 -- GitLab