From 49b11bb49e0d3569adc773a8e693b97d527ea4a3 Mon Sep 17 00:00:00 2001 From: zzz Date: Sun, 26 Dec 2010 15:07:59 +0000 Subject: [PATCH] refactor Addresses --- .../i2p/router/web/ConfigClientsHelper.java | 20 +--- .../net/i2p/router/web/ConfigNetHelper.java | 5 +- core/java/src/net/i2p/util/Addresses.java | 99 ++++++++++--------- 3 files changed, 57 insertions(+), 67 deletions(-) diff --git a/apps/routerconsole/java/src/net/i2p/router/web/ConfigClientsHelper.java b/apps/routerconsole/java/src/net/i2p/router/web/ConfigClientsHelper.java index 4e25719a0..4ce5c5b99 100644 --- a/apps/routerconsole/java/src/net/i2p/router/web/ConfigClientsHelper.java +++ b/apps/routerconsole/java/src/net/i2p/router/web/ConfigClientsHelper.java @@ -2,8 +2,6 @@ package net.i2p.router.web; import java.text.SimpleDateFormat; import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; import java.util.Date; import java.util.Iterator; import java.util.List; @@ -67,22 +65,8 @@ public class ConfigClientsHelper extends HelperBase { /** @since 0.8.3 */ public String[] intfcAddresses() { - String[] addrs = Addresses.getAllAddresses(); - List aList = new ArrayList(); - aList.addAll(Arrays.asList(addrs)); - boolean ipv6 = false; - for (String a : aList) { - if (a.indexOf(':') >= 0) { - ipv6 = true; - break; - } - } - if (!aList.contains("0.0.0.0")) - aList.add("0.0.0.0"); - if (ipv6 && !aList.contains("0:0:0:0:0:0:0:0")) - aList.add("0:0:0:0:0:0:0:0"); // we could do "::" but all the other ones are probably in long form - Collections.sort(aList); - return aList.toArray(addrs); + ArrayList al = new ArrayList(Addresses.getAllAddresses()); + return al.toArray(new String[al.size()]); } /** @since 0.8.3 */ diff --git a/apps/routerconsole/java/src/net/i2p/router/web/ConfigNetHelper.java b/apps/routerconsole/java/src/net/i2p/router/web/ConfigNetHelper.java index 392b3c06b..9388d06d2 100644 --- a/apps/routerconsole/java/src/net/i2p/router/web/ConfigNetHelper.java +++ b/apps/routerconsole/java/src/net/i2p/router/web/ConfigNetHelper.java @@ -1,5 +1,7 @@ package net.i2p.router.web; +import java.util.ArrayList; + import net.i2p.data.DataHelper; import net.i2p.data.RouterAddress; import net.i2p.router.CommSystemFacade; @@ -147,7 +149,8 @@ public class ConfigNetHelper extends HelperBase { } public String[] getAddresses() { - return Addresses.getAddresses(); + ArrayList al = new ArrayList(Addresses.getAddresses()); + return al.toArray(new String[al.size()]); } public String getInboundRate() { diff --git a/core/java/src/net/i2p/util/Addresses.java b/core/java/src/net/i2p/util/Addresses.java index b81d43c23..3af5ea65a 100644 --- a/core/java/src/net/i2p/util/Addresses.java +++ b/core/java/src/net/i2p/util/Addresses.java @@ -9,10 +9,10 @@ import java.net.Inet4Address; import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; -import java.util.Arrays; import java.util.Enumeration; -import java.util.HashSet; import java.util.Set; +import java.util.SortedSet; +import java.util.TreeSet; /** @@ -21,31 +21,31 @@ import java.util.Set; * @since 0.8.3 moved to core * @author zzz */ -public class Addresses { +public abstract class Addresses { /** @return the first non-local address it finds, or null */ public static String getAnyAddress() { - String[] a = getAddresses(); - if (a.length > 0) - return a[0]; + SortedSet a = getAddresses(); + if (!a.isEmpty()) + return a.first(); return null; } /** - * @return a sorted array of all addresses, excluding + * @return a sorted set of all addresses, excluding * IPv6, local, broadcast, multicast, etc. */ - public static String[] getAddresses() { - return getAddresses(false); + public static SortedSet getAddresses() { + return getAddresses(false, false); } /** - * @return a sorted array of all addresses, excluding + * @return a sorted set of all addresses, excluding * only link local and multicast * @since 0.8.3 */ - public static String[] getAllAddresses() { - return getAddresses(true); + public static SortedSet getAllAddresses() { + return getAddresses(true, true); } /** @@ -54,17 +54,15 @@ public class Addresses { * @return an array of all addresses * @since 0.8.3 */ - public static String[] getAddresses(boolean all) { - Set rv = new HashSet(4); + public static SortedSet getAddresses(boolean includeLocal, boolean includeIPv6) { + SortedSet rv = new TreeSet(); try { InetAddress localhost = InetAddress.getLocalHost(); InetAddress[] allMyIps = InetAddress.getAllByName(localhost.getCanonicalHostName()); if (allMyIps != null) { for (int i = 0; i < allMyIps.length; i++) { - if (all) - addAll(rv, allMyIps[i]); - else - add(rv, allMyIps[i]); + if (shouldInclude(allMyIps[i], includeLocal, includeIPv6)) + rv.add(allMyIps[i].getHostAddress()); } } } catch (UnknownHostException e) {} @@ -74,49 +72,54 @@ public class Addresses { NetworkInterface ifc = ifcs.nextElement(); for(Enumeration addrs = ifc.getInetAddresses(); addrs.hasMoreElements();) { InetAddress addr = addrs.nextElement(); - if (all) - addAll(rv, addr); - else - add(rv, addr); + if (shouldInclude(addr, includeLocal, includeIPv6)) + rv.add(addr.getHostAddress()); } } } catch (SocketException e) {} - String[] rva = rv.toArray(new String[rv.size()]); - Arrays.sort(rva); - return rva; + if (includeLocal) + rv.add("0.0.0.0"); + if (includeLocal && includeIPv6) { + boolean ipv6 = false; + for (String a : rv) { + if (a.indexOf(':') >= 0) { + ipv6 = true; + break; + } + } + if (ipv6) + rv.add("0:0:0:0:0:0:0:0"); // we could do "::" but all the other ones are probably in long form + } + return rv; } - private static void add(Set set, InetAddress ia) { - if (ia.isAnyLocalAddress() || - ia.isLinkLocalAddress() || - ia.isLoopbackAddress() || - ia.isMulticastAddress() || - ia.isSiteLocalAddress() || + private static boolean shouldInclude(InetAddress ia, boolean includeLocal, boolean includeIPv6) { + return + (!ia.isLinkLocalAddress()) && + (!ia.isMulticastAddress()) && + (includeLocal || + ((!ia.isAnyLocalAddress()) && + (!ia.isLoopbackAddress()) && + (!ia.isSiteLocalAddress()))) && // Hamachi 5/8 allocated to RIPE (30 November 2010) // Removed from TransportImpl.isPubliclyRoutable() // Check moved to here, for now, but will eventually need to // remove it from here also. - ia.getHostAddress().startsWith("5.") || - !(ia instanceof Inet4Address)) { -// System.err.println("Skipping: " + ia.getHostAddress()); - return; - } - String ip = ia.getHostAddress(); - set.add(ip); - } - - private static void addAll(Set set, InetAddress ia) { - if (ia.isLinkLocalAddress() || - ia.isMulticastAddress()) - return; - String ip = ia.getHostAddress(); - set.add(ip); + (includeLocal || + (!ia.getHostAddress().startsWith("5."))) && + (includeIPv6 || + (ia instanceof Inet4Address)); } public static void main(String[] args) { - String[] a = getAddresses(true); + System.err.println("External Addresses:"); + Set a = getAddresses(false, false); for (String s : a) - System.err.println("Address: " + s); + System.err.println(s); + System.err.println("All addresses:"); + a = getAddresses(true, true); + for (String s : a) + System.err.println(s); } }