Package net.i2p.util

Class Addresses

java.lang.Object
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

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
     
    Warning, very slow on Windows, appx.
    getAddresses(boolean includeLocal, boolean includeIPv6)
    Warning: When includeLocal is false, all returned addresses should be routable, but they are not necessarily appropriate for external use.
    getAddresses(boolean includeSiteLocal, boolean includeLoopbackAndWildcard, boolean includeIPv6)
    Warning: When includeSiteLocal and includeLoopbackAndWildcard are false, all returned addresses should be routable, but they are not necessarily appropriate for external use.
    getAddresses(boolean includeSiteLocal, boolean includeLoopbackAndWildcard, boolean includeIPv6, boolean includeIPv6Temporary)
    Warning: When includeSiteLocal and includeLoopbackAndWildcard are false, all returned addresses should be routable, but they are not necessarily appropriate for external use.
    Warning, very slow on Windows, appx.
    static String
    Warning, very slow on Windows, appx.
    Efficiently get all connected address types in one pass.
    static byte[]
    getIP(String host)
    Caching version of InetAddress.getByName(host).getAddress(), which is slow.
    static byte[]
    getIP(String host, boolean preferIPv6)
    For literal IP addresses, this is the same as getIP(String).
    static byte[]
    Caching version of InetAddress.getByName(host).getAddress(), which is slow.
    static List<byte[]>
    getIPs(String host)
    For literal IP addresses, this is the same as getIP(String).
    static int
    Convenience method to convert and validate a port String without throwing an exception.
    static byte[]
    Warning, very slow on Windows.
    static boolean
    Do we have any non-loop, non-wildcard IPv4 address at all? Warning, very slow on Windows, appx.
    static boolean
    Do we have any address of this type? Warning, very slow on Windows, appx.
    static boolean
    Do we have any non-loop, non-wildcard IPv6 address at all? Warning, very slow on Windows, appx.
    static boolean
    Is this address deprecated? Should be reliable on Linux.
    static boolean
    Is this address dynamic? Should be reliable on Linux.
    static boolean
     
    static boolean
     
    static boolean
     
    static boolean
    Is this address temporary? Should be reliable on Linux.
    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.
    static String
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Addresses

      public Addresses()
  • Method Details

    • isConnected

      public static boolean isConnected(AddressType type)
      Do we have any address of this type? Warning, very slow on Windows, appx. 200ms + 50ms/interface Use getConnectedAddressTypes() to get all types at once.
      Since:
      0.9.54
    • isConnected

      public static boolean isConnected()
      Do we have any non-loop, non-wildcard IPv4 address at all? Warning, very slow on Windows, appx. 200ms + 50ms/interface Use getConnectedAddressTypes() to get all types at once.
      Since:
      0.9.4
    • isConnectedIPv6

      public static boolean isConnectedIPv6()
      Do we have any non-loop, non-wildcard IPv6 address at all? Warning, very slow on Windows, appx. 200ms + 50ms/interface Use getConnectedAddressTypes() to get all types at once.
      Since:
      0.9.29
    • getAnyAddress

      public static String getAnyAddress()
      Warning, very slow on Windows, appx. 200ms + 50ms/interface
      Returns:
      the first non-local address IPv4 address it finds, or null
    • getAddresses

      public static SortedSet<String> getAddresses()
      Warning, very slow on Windows, appx. 200ms + 50ms/interface
      Returns:
      a sorted set of all addresses, excluding IPv6, local, broadcast, multicast, etc.
    • getAllAddresses

      public static SortedSet<String> getAllAddresses()
      Warning, very slow on Windows, appx. 200ms + 50ms/interface
      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)
      Warning: When includeLocal is false, all returned addresses should be routable, but they are not necessarily appropriate for external use. For example, Teredo and 6to4 addresses are included with IPv6 results. Additional validation is recommended. See e.g. TransportUtil.isPubliclyRoutable(). Warning, very slow on Windows, appx. 200ms + 50ms/interface
      Parameters:
      includeLocal - whether to include local addresses and deprecated IPv6 addresses
      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)
      Warning: When includeSiteLocal and includeLoopbackAndWildcard are false, all returned addresses should be routable, but they are not necessarily appropriate for external use. For example, Teredo and 6to4 addresses are included with IPv6 results. Additional validation is recommended. See e.g. TransportUtil.isPubliclyRoutable(). Warning, very slow on Windows, appx. 200ms + 50ms/interface
      Parameters:
      includeSiteLocal - whether to include private like 192.168.x.x and deprecated IPv6 addresses
      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
    • getAddresses

      public static SortedSet<String> getAddresses(boolean includeSiteLocal, boolean includeLoopbackAndWildcard, boolean includeIPv6, boolean includeIPv6Temporary)
      Warning: When includeSiteLocal and includeLoopbackAndWildcard are false, all returned addresses should be routable, but they are not necessarily appropriate for external use. For example, Teredo and 6to4 addresses are included with IPv6 results. Additional validation is recommended. See e.g. TransportUtil.isPubliclyRoutable(). Warning, very slow on Windows, appx. 200ms + 50ms/interface
      Parameters:
      includeSiteLocal - whether to include private like 192.168.x.x and deprecated IPv6 addresses
      includeLoopbackAndWildcard - whether to include 127.x.x.x and 0.0.0.0
      includeIPv6 - whether to include IPV6
      includeIPv6Temporary - whether to include IPV6 temporary addresses
      Returns:
      a sorted set of all addresses
      Since:
      0.9.46
    • getYggdrasilAddress

      public static byte[] getYggdrasilAddress()
      Warning, very slow on Windows. Caller should cache.
      Returns:
      the IPv6 address with prefix 02xx: or 03xx:, or null
      Since:
      0.9.49
    • getConnectedAddressTypes

      public static Set<AddressType> getConnectedAddressTypes()
      Efficiently get all connected address types in one pass. Warning, very slow on Windows. Caller should cache.
      Returns:
      the set of connected address types, non-null
      Since:
      0.9.54
    • 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:
      "ipv4:port" or "[ipv6]: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 addresses only. Will resolve but not cache DNS addresses. 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. InetAddress.getByName() is documented to return 127.0.0.1 for a null host; here we return null. InetAddress.getByName() also returns 127.0.0.1 for a host "", but this is undocumented; as of 0.9.49, here we return null.
      Parameters:
      host - DNS or IPv4 or IPv6 address; if null or empty returns null
      Returns:
      IP or null
      Since:
      0.9.3
    • getIPOnly

      public static byte[] getIPOnly(String host)
      Caching version of InetAddress.getByName(host).getAddress(), which is slow. Resolves literal IP addresses only, will not cause a DNS lookup. Will return null for hostnames. 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. InetAddress.getByName() also returns 127.0.0.1 for a host "", but this is undocumented; as of 0.9.50, here we return null.
      Parameters:
      host - literal IPv4 or IPv6 address; if null returns null
      Returns:
      IP or null
      Since:
      0.9.32
    • getIP

      public static byte[] getIP(String host, boolean preferIPv6)
      For literal IP addresses, this is the same as getIP(String). For hostnames, will return the preferred type (IPv4/v6) if available, else the other type if available. Will resolve but not cache DNS hostnames. InetAddress.getByName() also returns 127.0.0.1 for a host "", but this is undocumented; as of 0.9.50, here we return null.
      Parameters:
      host - DNS or IPv4 or IPv6 address; if null returns null
      Returns:
      IP or null
      Since:
      0.9.28
    • getIPs

      public static List<byte[]> getIPs(String host)
      For literal IP addresses, this is the same as getIP(String). For hostnames, may return multiple addresses, both IPv4 and IPv6, even if those addresses are not reachable due to configuration or available interfaces. Will resolve but not cache DNS hostnames. Note that order of returned results, and whether multiple results for either IPv4 or IPv6 or both are actually returned, is platform-specific and may also depend on JVM options such as java.net.preverIPv4Stack and java.net.preferIPv6Addresses. Number of results may also change based on caching at various layers, even if the ultimate name server results did not change. InetAddress.getByName() also returns 127.0.0.1 for a host "", but this is undocumented; as of 0.9.50, here we return null.
      Parameters:
      host - DNS or IPv4 or IPv6 address; if null returns null
      Returns:
      non-empty list IPs, or null if none
      Since:
      0.9.28
    • isIPv4Address

      public static boolean isIPv4Address(String host)
      Since:
      0.9.34
    • isIPv6Address

      public static boolean isIPv6Address(String host)
      Since:
      0.9.34
    • isIPAddress

      public static boolean isIPAddress(String host)
      Returns:
      true if either IPv4 or IPv6
      Since:
      0.9.34
    • isDynamic

      public static boolean isDynamic(Inet6Address addr)
      Is this address dynamic? Should be reliable on Linux. Returns best guess on Windows, Mac, and BSD, only valid if global scope.
      Parameters:
      addr - an address of a local interface, as returned from e.g. getAddresses()
      Since:
      0.9.28
    • isDeprecated

      public static boolean isDeprecated(Inet6Address addr)
      Is this address deprecated? Should be reliable on Linux. Returns false on Windows, Mac, and BSD.
      Parameters:
      addr - an address of a local interface, as returned from e.g. getAddresses()
      Since:
      0.9.28
    • isTemporary

      public static boolean isTemporary(Inet6Address addr)
      Is this address temporary? Should be reliable on Linux. Returns best guess on Windows, Mac, and BSD, only valid if global scope.
      Parameters:
      addr - an address of a local interface, as returned from e.g. getAddresses()
      Since:
      0.9.28
    • clearCaches

      public static void clearCaches()
      Since:
      0.9.3
    • main

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

      public static String useIPv6TempAddresses()
      Returns:
      "true", "false", or "unknown"
      Since:
      0.9.50