From 07e3ee6bdae8ee2beca68672df75af6c278f6d4c Mon Sep 17 00:00:00 2001 From: zzz Date: Mon, 15 Mar 2021 08:22:52 -0400 Subject: [PATCH] Util: Roll our own IPv6 address parsing Don't bother with ::, still send that to InetAddress.getByName() --- core/java/src/net/i2p/util/Addresses.java | 31 +++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/core/java/src/net/i2p/util/Addresses.java b/core/java/src/net/i2p/util/Addresses.java index 30e589891..79347a6f5 100644 --- a/core/java/src/net/i2p/util/Addresses.java +++ b/core/java/src/net/i2p/util/Addresses.java @@ -499,6 +499,10 @@ public abstract class Addresses { rv = getIPv4(host); if (rv == null) return null; + } else if (host.contains(":") && !host.contains("::")) { + rv = getIPv6(host); + if (rv == null) + return null; } else { rv = InetAddress.getByName(host).getAddress(); } @@ -663,6 +667,33 @@ public abstract class Addresses { return rv; } + /** + * Because InetAddress.getByName() is slow, esp. on Windows + * + * @param host full 0:1:2:3:4:5:6:7 only, no :: + * @return 16 bytes or null + * @since 0.9.50 + */ + private static byte[] getIPv6(String host) { + String[] s = DataHelper.split(host, ":", 8); + if (s.length != 8) + return null; + byte[] rv = new byte[16]; + try { + int j = 0; + for (int i = 0; i < 8; i++) { + int b = Integer.parseInt(s[i], 16); + if (b < 0 || b > 65535) + return null; + rv[j++] = (byte) (b >> 8); + rv[j++] = (byte) b; + } + } catch (NumberFormatException nfe) { + return null; + } + return rv; + } + //////// IPv6 Cache Utils /////// /**