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

public abstract class Addressesextends Object
Methods to get the local addresses, and other IP utilities.

Performance Warning: Many methods in this class are very slow on Windows (approximately 200ms + 50ms per interface). Consider caching results when possible.

  • Field Details

    • IF_INET6_FILE

      private static final File IF_INET6_FILE
      Proc file listing the IPv6 interfaces.
    • INET6_CACHE_EXPIRE

      private static final long INET6_CACHE_EXPIRE
      Time the interface cache stays valid.
      See Also:
    • INET6_CACHE_ENABLED

      private static final boolean INET6_CACHE_ENABLED
      True where the proc interface cache is usable.
    • FLAG_PERMANENT

      private static final int FLAG_PERMANENT
      Flag of a permanent interface address.
      See Also:
    • FLAG_DEPRECATED

      private static final int FLAG_DEPRECATED
      Flag of a deprecated interface address.
      See Also:
    • FLAG_TEMPORARY

      private static final int FLAG_TEMPORARY
      Flag of a temporary interface address.
      See Also:
    • ifCacheTime

      private static long ifCacheTime
      When the interface cache was read.
    • _ifCache

      private static final Map<Inet6Address, Addresses.Inet6Addr> _ifCache
      Cached interface addresses, null if disabled.
    • _macCache

      private static final Set<String> _macCache
      12 char hex lower case.
    • RFC5952_MATCH

      private static final String RFC5952_MATCH
      Regex matching the longest zero run. See also: RFC 5952 (IPv6 address text representation) See also: compressed IPv6 form
      See Also:
    • RFC5952_PATTERN

      private static final Pattern RFC5952_PATTERN
      Compiled matcher for the zero run.
    • _IPAddress

      private static final Map<String,byte[]> _IPAddress
      Textual IP to bytes, because InetAddress.getByName(String) is slow.
    • _negativeCache

      private static final Map<String,Long> _negativeCache
      Hosts recently found not to resolve.
    • NEG_CACHE_TIME

      private static final long NEG_CACHE_TIME
      Time a failed lookup is remembered.
      See Also:
  • Constructor Details

    • Addresses

      private Addresses()
      Private constructor to prevent instantiation.
  • Method Details

    • isConnected

      public static boolean isConnected(AddressType type)
      Do we have any address of this type? Use getConnectedAddressTypes() to get all types at once for better performance.
      Parameters:
      type - the address type to check
      Returns:
      true if connected to the specified address type
    • isConnected

      public static boolean isConnected()
      Do we have any non-loop, non-wildcard IPv4 address at all? Use getConnectedAddressTypes() to get all types at once for better performance.
      Returns:
      true if connected to IPv4
    • isConnectedIPv6

      public static boolean isConnectedIPv6()
      Do we have any non-loop, non-wildcard IPv6 address at all? Use getConnectedAddressTypes() to get all types at once for better performance.
      Returns:
      true if connected to IPv6
    • getAnyAddress

      public static String getAnyAddress()
      First non-local IPv4 address found.
      Returns:
      the first non-local IPv4 address found, or null
    • getAddresses

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

      public static SortedSet<String> getAllAddresses()
      All addresses excluding only link-local and multicast addresses.
      Returns:
      a sorted set of all addresses, excluding only link-local and multicast
    • 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(byte[], boolean).
      Parameters:
      includeLocal - whether to include local addresses and deprecated IPv6 addresses
      includeIPv6 - whether to include IPv6 addresses
      Returns:
      a sorted set of all addresses including wildcard
    • 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(byte[], boolean).
      Parameters:
      includeSiteLocal - whether to include private addresses like 192.168.x.x and deprecated IPv6 addresses
      includeLoopbackAndWildcard - whether to include loopback addresses (127.x.x.x) and wildcard (0.0.0.0)
      includeIPv6 - whether to include IPv6 addresses
      Returns:
      a sorted set of all addresses
    • 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(byte[], boolean).
      Parameters:
      includeSiteLocal - whether to include private addresses like 192.168.x.x and deprecated IPv6 addresses
      includeLoopbackAndWildcard - whether to include loopback addresses (127.x.x.x) and wildcard (0.0.0.0)
      includeIPv6 - whether to include IPv6 addresses
      includeIPv6Temporary - whether to include IPv6 temporary addresses
      Returns:
      a sorted set of all addresses
    • getYggdrasilAddress

      public static byte[] getYggdrasilAddress()
      Caller should cache results for better performance.
      Returns:
      the IPv6 address with prefix 02xx: or 03xx:, or null
    • getConnectedAddressTypes

      public static Set<AddressType> getConnectedAddressTypes()
      Efficiently get all connected address types in one pass. Caller should cache results for better performance.
      Returns:
      the set of connected address types, non-null
    • stripScope

      private static String stripScope(String ip)
      Strip the trailing "%nn" from Inet6Address.getHostAddress().
      Parameters:
      ip - the host address string, possibly with a %nn scope suffix
      Returns:
      the address with any scope suffix removed
    • shouldInclude

      private static boolean shouldInclude(InetAddress ia, boolean includeSiteLocal, boolean includeLoopbackAndWildcard, boolean includeIPv6)
      Checks whether an address counts as one of ours.
      Parameters:
      ia - the address to check
      includeSiteLocal - true to accept site local addresses
      includeLoopbackAndWildcard - true to accept loopback and wildcard
      includeIPv6 - true to accept IPv6 addresses
      Returns:
      true if the address is local
    • toString

      public static String toString(byte[] addr)
      Convenience method to convert an IP address to a String without throwing an exception.
      Parameters:
      addr - the IP address as byte array
      Returns:
      string representation, or "null" for null input, or "Bad IP length x" if length is invalid
    • 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.
      Parameters:
      addr - the IP address as byte array
      port - the port number
      Returns:
      formatted string as "ipv4:port" or "[ipv6]:port", or error message for invalid input
    • toCanonicalString

      public static String toCanonicalString(byte[] addr)
      Same as Object.toString() but returns IPv6 in compressed form per RFC 5952.
      Parameters:
      addr - the IP address as byte array
      Returns:
      canonical string representation, or "null" for null input, or "bad IP length x" if length is invalid
    • toCanonicalString

      public static String toCanonicalString(byte[] addr, int port)
      Same as toString() but returns IPv6 in compressed form per RFC 5952.
      Parameters:
      addr - the IP address as byte array
      port - the port number
      Returns:
      canonical string representation with port, or error message for invalid input
    • toCanonicalString

      public static String toCanonicalString(String host)
      Converts IPv6 to compressed form per RFC 5952. IPv4 returned unchanged.
      Parameters:
      host - the address to compress, an IPv6 literal containing ':' or an IPv4 literal returned unchanged
      Returns:
      canonical string representation, or "null" for null input
    • x_toCanonicalString

      private static String x_toCanonicalString(String host)
      Does the RFC 5952 substitution and drops a redundant leading zero group.
      Parameters:
      host - non-null
      Returns:
      the canonical form of the IPv6 host string
    • getPort

      public static int getPort(String port)
      Convenience method to convert and validate a port String without throwing an exception. Does not trim.
      Parameters:
      port - the port number
      Returns:
      1-65535 or 0 if invalid
    • getIP

      public static byte[] getIP(String host)
      Caching version of getByName(host).getAddress(), which is slow. Caches numeric addresses only. Will resolve but not cache DNS addresses.

      DEPRECATED for IPs in RouterAddresses, blocklists, etc.; use getIPOnly(String) instead.

      Behavior differences from InetAddress.getByName(String):

      • Does not allow numeric IPs of the form d.d.d, d.d, or d (considered mistakes)
      • Returns null for null or empty host (vs. 127.0.0.1 in InetAddress).
      Parameters:
      host - DNS or IPv4 or IPv6 address; if null or empty returns null
      Returns:
      IP address bytes or null
    • getIPOnly

      public static byte[] getIPOnly(String host)
      Caching version of getByName(host).getAddress(), which is slow. Resolves literal IP addresses only without DNS lookups. Returns null for hostnames.

      PREFERRED for IPs in RouterAddresses, blocklists, etc. over getIP(String) because it avoids getByName() in most cases.

      Behavior differences from InetAddress.getByName(String):

      • Does not allow numeric IPs of the form d.d.d, d.d, or d (considered mistakes)
      • Returns null for null or empty host (vs. 127.0.0.1 in InetAddress).
      Parameters:
      host - literal IPv4 or IPv6 address; if null returns null
      Returns:
      IP address bytes or null
    • getIP

      public static byte[] getIP(String host, boolean preferIPv6)
      For literal IP addresses, same as getIP(String). For hostnames, returns the preferred type (IPv4/v6) if available, else the other type. Will resolve but not cache DNS hostnames.

      Behavior differences from InetAddress.getByName(String):

      • Returns null for null or empty host (vs. 127.0.0.1 in InetAddress).
      Parameters:
      host - DNS or IPv4 or IPv6 address; if null returns null
      preferIPv6 - whether to prefer IPv6 addresses
      Returns:
      IP address bytes or null
    • getIPs

      public static List<byte[]> getIPs(String host)
      For literal IP addresses, same as getIP(String). For hostnames, may return multiple addresses (both IPv4 and IPv6), even if unreachable. Will resolve but not cache DNS hostnames.

      Note: Order and number of results are platform-specific and depend on JVM options (java.net.preferIPv4Stack, java.net.preferIPv6Addresses) and caching layers.

      Behavior differences from InetAddress.getByName(String):

      • Returns null for null or empty host (vs. 127.0.0.1 in InetAddress).
      Parameters:
      host - DNS or IPv4 or IPv6 address; if null returns null
      Returns:
      non-empty list of IP addresses, or null if none
    • isIPv4Address

      public static boolean isIPv4Address(String host)
      Checks if the host is a valid IPv4 address.
      Parameters:
      host - the string to test, not resolved or looked up
      Returns:
      true if IPv4 address
    • isIPv6Address

      public static boolean isIPv6Address(String host)
      Checks if the host is a valid IPv6 address.
      Parameters:
      host - the string to test, not resolved or looked up
      Returns:
      true if IPv6 address
    • isIPAddress

      public static boolean isIPAddress(String host)
      Checks if the host is a valid IP address (IPv4 or IPv6).
      Parameters:
      host - the string to test, not resolved or looked up
      Returns:
      true if either IPv4 or IPv6
    • getIPv4

      private static byte[] getIPv4(String host)
      Fast IPv4 address parsing that avoids slow InetAddress.getByName(String) on Windows. Also avoids split(), Integer.parseInt(String, int), and object churn.
      Parameters:
      host - IPv4 address in w.x.y.z format only
      Returns:
      4-byte array or null if invalid
    • getIPv6

      private static byte[] getIPv6(String host)
      Fast IPv6 address parsing that avoids slow InetAddress.getByName(String) on Windows. Also avoids split(), Integer.parseInt(String, int), and object churn.
      Parameters:
      host - full IPv6 address in 0:1:2:3:4:5:6:7 format only (no :: compression)
      Returns:
      16-byte array or null if invalid
    • refreshCache

      private static void refreshCache()
      Only call if INET6_CACHE_ENABLED. Caller must sync on _ifCache.
    • isDynamic

      public static boolean isDynamic(Inet6Address addr)
      Determines if an IPv6 address is dynamic. Reliable on Linux. Best guess on Windows, Mac, and BSD (only valid for global scope).
      Parameters:
      addr - IPv6 address of a local interface, as returned from getAddresses()
      Returns:
      true if address is dynamic
    • isDeprecated

      public static boolean isDeprecated(Inet6Address addr)
      Determines if an IPv6 address is deprecated. Reliable on Linux. Returns false on Windows, Mac, and BSD.
      Parameters:
      addr - IPv6 address of a local interface, as returned from getAddresses()
      Returns:
      true if address is deprecated
    • isTemporary

      public static boolean isTemporary(Inet6Address addr)
      Determines if an IPv6 address is temporary. Reliable on Linux. Best guess on Windows, Mac, and BSD (only valid for global scope).
      Parameters:
      addr - IPv6 address of a local interface, as returned from getAddresses()
      Returns:
      true if address is temporary
    • clearCaches

      public static void clearCaches()
      Clears all internal caches.
    • main

      public static void main(String[] args)
      Print out the local addresses.
      Parameters:
      args - command line arguments
    • print

      private static void print(Set<String> a)
      Print the addresses to stdout.
      Parameters:
      a - the address strings to print, one per line, or "none" if empty
    • printCanonical

      private static void printCanonical(Set<String> a)
      Print the addresses in canonical form to stdout.
      Parameters:
      a - the address strings to print canonically, one per line, or "none" if empty
    • useIPv6TempAddresses

      public static String useIPv6TempAddresses()
      Checks if temporary IPv6 addresses are enabled system-wide.
      Returns:
      "true", "false", or "unknown" (for Windows/Mac where detection is not supported)
    • getPrivacyStatus

      private static String getPrivacyStatus()
      RFC 4941.
      Returns:
      the privacy status
    • getLong

      private static long getLong(String s)
      Return first line in a file as a long.
      Parameters:
      s - the file path whose first line is parsed as a decimal long
      Returns:
      -1 on failure