net.i2p.util
Class Addresses

java.lang.Object
  extended by net.i2p.util.Addresses

public abstract class Addresses
extends Object

Methods to get the local addresses, and other IP utilities

Since:
0.8.3 moved to core from router/transport
Author:
zzz

Constructor Summary
Addresses()
           
 
Method Summary
static void clearCaches()
           
static SortedSet<String> getAddresses()
           
static SortedSet<String> getAddresses(boolean includeLocal, boolean includeIPv6)
           
static SortedSet<String> getAddresses(boolean includeSiteLocal, boolean includeLoopbackAndWildcard, boolean includeIPv6)
           
static SortedSet<String> getAllAddresses()
           
static String getAnyAddress()
           
static byte[] getIP(String host)
          Caching version of InetAddress.getByName(host).getAddress(), which is slow.
static int getPort(String port)
          Convenience method to convert and validate a port String without throwing an exception.
static boolean isConnected()
          Do we have any non-loop, non-wildcard IPv4 address at all?
static void main(String[] args)
          Print out the local addresses
static String toString(byte[] addr)
          Convenience method to convert an IP address to a String without throwing an exception.
static String toString(byte[] addr, int port)
          Convenience method to convert an IP address and port to a String without throwing an exception.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Addresses

public Addresses()
Method Detail

isConnected

public static boolean isConnected()
Do we have any non-loop, non-wildcard IPv4 address at all?

Since:
0.9.4

getAnyAddress

public static String getAnyAddress()
Returns:
the first non-local address it finds, or null

getAddresses

public static SortedSet<String> getAddresses()
Returns:
a sorted set of all addresses, excluding IPv6, local, broadcast, multicast, etc.

getAllAddresses

public static SortedSet<String> getAllAddresses()
Returns:
a sorted set of all addresses, excluding only link local and multicast
Since:
0.8.3

getAddresses

public static SortedSet<String> getAddresses(boolean includeLocal,
                                             boolean includeIPv6)
Parameters:
includeLocal - whether to include local
includeIPv6 - whether to include IPV6
Returns:
a sorted set of all addresses including wildcard
Since:
0.8.3

getAddresses

public static SortedSet<String> getAddresses(boolean includeSiteLocal,
                                             boolean includeLoopbackAndWildcard,
                                             boolean includeIPv6)
Parameters:
includeSiteLocal - whether to include private like 192.168.x.x
includeLoopbackAndWildcard - whether to include 127.x.x.x and 0.0.0.0
includeIPv6 - whether to include IPV6
Returns:
a sorted set of all addresses
Since:
0.9.4

toString

public static String toString(byte[] addr)
Convenience method to convert an IP address to a String without throwing an exception.

Returns:
"null" for null, and "bad IP length x" if length is invalid
Since:
0.8.12

toString

public static String toString(byte[] addr,
                              int port)
Convenience method to convert an IP address and port to a String without throwing an exception.

Returns:
"ip:port"
Since:
0.8.12

getPort

public static int getPort(String port)
Convenience method to convert and validate a port String without throwing an exception. Does not trim.

Returns:
1-65535 or 0 if invalid
Since:
0.9.3

getIP

public static byte[] getIP(String host)
Caching version of InetAddress.getByName(host).getAddress(), which is slow. Caches numeric host names only. Will resolve but not cache DNS host names. Unlike InetAddress.getByName(), we do NOT allow numeric IPs of the form d.d.d, d.d, or d, as these are almost certainly mistakes.

Parameters:
host - DNS or IPv4 or IPv6 host name; if null returns null
Returns:
IP or null
Since:
0.9.3

clearCaches

public static void clearCaches()
Since:
0.9.3

main

public static void main(String[] args)
Print out the local addresses