refactor Addresses
This commit is contained in:
@ -2,8 +2,6 @@ package net.i2p.router.web;
|
|||||||
|
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -67,22 +65,8 @@ public class ConfigClientsHelper extends HelperBase {
|
|||||||
|
|
||||||
/** @since 0.8.3 */
|
/** @since 0.8.3 */
|
||||||
public String[] intfcAddresses() {
|
public String[] intfcAddresses() {
|
||||||
String[] addrs = Addresses.getAllAddresses();
|
ArrayList<String> al = new ArrayList(Addresses.getAllAddresses());
|
||||||
List<String> aList = new ArrayList();
|
return al.toArray(new String[al.size()]);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @since 0.8.3 */
|
/** @since 0.8.3 */
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package net.i2p.router.web;
|
package net.i2p.router.web;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
import net.i2p.data.DataHelper;
|
import net.i2p.data.DataHelper;
|
||||||
import net.i2p.data.RouterAddress;
|
import net.i2p.data.RouterAddress;
|
||||||
import net.i2p.router.CommSystemFacade;
|
import net.i2p.router.CommSystemFacade;
|
||||||
@ -147,7 +149,8 @@ public class ConfigNetHelper extends HelperBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String[] getAddresses() {
|
public String[] getAddresses() {
|
||||||
return Addresses.getAddresses();
|
ArrayList<String> al = new ArrayList(Addresses.getAddresses());
|
||||||
|
return al.toArray(new String[al.size()]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getInboundRate() {
|
public String getInboundRate() {
|
||||||
|
@ -9,10 +9,10 @@ import java.net.Inet4Address;
|
|||||||
import java.net.NetworkInterface;
|
import java.net.NetworkInterface;
|
||||||
import java.net.SocketException;
|
import java.net.SocketException;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Enumeration;
|
import java.util.Enumeration;
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.Set;
|
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
|
* @since 0.8.3 moved to core
|
||||||
* @author zzz
|
* @author zzz
|
||||||
*/
|
*/
|
||||||
public class Addresses {
|
public abstract class Addresses {
|
||||||
|
|
||||||
/** @return the first non-local address it finds, or null */
|
/** @return the first non-local address it finds, or null */
|
||||||
public static String getAnyAddress() {
|
public static String getAnyAddress() {
|
||||||
String[] a = getAddresses();
|
SortedSet<String> a = getAddresses();
|
||||||
if (a.length > 0)
|
if (!a.isEmpty())
|
||||||
return a[0];
|
return a.first();
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return a sorted array of all addresses, excluding
|
* @return a sorted set of all addresses, excluding
|
||||||
* IPv6, local, broadcast, multicast, etc.
|
* IPv6, local, broadcast, multicast, etc.
|
||||||
*/
|
*/
|
||||||
public static String[] getAddresses() {
|
public static SortedSet<String> getAddresses() {
|
||||||
return getAddresses(false);
|
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
|
* only link local and multicast
|
||||||
* @since 0.8.3
|
* @since 0.8.3
|
||||||
*/
|
*/
|
||||||
public static String[] getAllAddresses() {
|
public static SortedSet<String> getAllAddresses() {
|
||||||
return getAddresses(true);
|
return getAddresses(true, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -54,17 +54,15 @@ public class Addresses {
|
|||||||
* @return an array of all addresses
|
* @return an array of all addresses
|
||||||
* @since 0.8.3
|
* @since 0.8.3
|
||||||
*/
|
*/
|
||||||
public static String[] getAddresses(boolean all) {
|
public static SortedSet<String> getAddresses(boolean includeLocal, boolean includeIPv6) {
|
||||||
Set<String> rv = new HashSet(4);
|
SortedSet<String> rv = new TreeSet();
|
||||||
try {
|
try {
|
||||||
InetAddress localhost = InetAddress.getLocalHost();
|
InetAddress localhost = InetAddress.getLocalHost();
|
||||||
InetAddress[] allMyIps = InetAddress.getAllByName(localhost.getCanonicalHostName());
|
InetAddress[] allMyIps = InetAddress.getAllByName(localhost.getCanonicalHostName());
|
||||||
if (allMyIps != null) {
|
if (allMyIps != null) {
|
||||||
for (int i = 0; i < allMyIps.length; i++) {
|
for (int i = 0; i < allMyIps.length; i++) {
|
||||||
if (all)
|
if (shouldInclude(allMyIps[i], includeLocal, includeIPv6))
|
||||||
addAll(rv, allMyIps[i]);
|
rv.add(allMyIps[i].getHostAddress());
|
||||||
else
|
|
||||||
add(rv, allMyIps[i]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (UnknownHostException e) {}
|
} catch (UnknownHostException e) {}
|
||||||
@ -74,49 +72,54 @@ public class Addresses {
|
|||||||
NetworkInterface ifc = ifcs.nextElement();
|
NetworkInterface ifc = ifcs.nextElement();
|
||||||
for(Enumeration<InetAddress> addrs = ifc.getInetAddresses(); addrs.hasMoreElements();) {
|
for(Enumeration<InetAddress> addrs = ifc.getInetAddresses(); addrs.hasMoreElements();) {
|
||||||
InetAddress addr = addrs.nextElement();
|
InetAddress addr = addrs.nextElement();
|
||||||
if (all)
|
if (shouldInclude(addr, includeLocal, includeIPv6))
|
||||||
addAll(rv, addr);
|
rv.add(addr.getHostAddress());
|
||||||
else
|
|
||||||
add(rv, addr);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (SocketException e) {}
|
} catch (SocketException e) {}
|
||||||
|
|
||||||
String[] rva = rv.toArray(new String[rv.size()]);
|
if (includeLocal)
|
||||||
Arrays.sort(rva);
|
rv.add("0.0.0.0");
|
||||||
return rva;
|
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<String> set, InetAddress ia) {
|
private static boolean shouldInclude(InetAddress ia, boolean includeLocal, boolean includeIPv6) {
|
||||||
if (ia.isAnyLocalAddress() ||
|
return
|
||||||
ia.isLinkLocalAddress() ||
|
(!ia.isLinkLocalAddress()) &&
|
||||||
ia.isLoopbackAddress() ||
|
(!ia.isMulticastAddress()) &&
|
||||||
ia.isMulticastAddress() ||
|
(includeLocal ||
|
||||||
ia.isSiteLocalAddress() ||
|
((!ia.isAnyLocalAddress()) &&
|
||||||
|
(!ia.isLoopbackAddress()) &&
|
||||||
|
(!ia.isSiteLocalAddress()))) &&
|
||||||
// Hamachi 5/8 allocated to RIPE (30 November 2010)
|
// Hamachi 5/8 allocated to RIPE (30 November 2010)
|
||||||
// Removed from TransportImpl.isPubliclyRoutable()
|
// Removed from TransportImpl.isPubliclyRoutable()
|
||||||
// Check moved to here, for now, but will eventually need to
|
// Check moved to here, for now, but will eventually need to
|
||||||
// remove it from here also.
|
// remove it from here also.
|
||||||
ia.getHostAddress().startsWith("5.") ||
|
(includeLocal ||
|
||||||
!(ia instanceof Inet4Address)) {
|
(!ia.getHostAddress().startsWith("5."))) &&
|
||||||
// System.err.println("Skipping: " + ia.getHostAddress());
|
(includeIPv6 ||
|
||||||
return;
|
(ia instanceof Inet4Address));
|
||||||
}
|
|
||||||
String ip = ia.getHostAddress();
|
|
||||||
set.add(ip);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void addAll(Set<String> set, InetAddress ia) {
|
|
||||||
if (ia.isLinkLocalAddress() ||
|
|
||||||
ia.isMulticastAddress())
|
|
||||||
return;
|
|
||||||
String ip = ia.getHostAddress();
|
|
||||||
set.add(ip);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
String[] a = getAddresses(true);
|
System.err.println("External Addresses:");
|
||||||
|
Set<String> a = getAddresses(false, false);
|
||||||
for (String s : a)
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user