Performance Warning: Many methods in this class are very slow on Windows (approximately 200ms + 50ms per interface). Consider caching results when possible.
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static classCached IPv6 address with its scope.Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final Map<Inet6Address, Addresses.Inet6Addr> Cached interface addresses, null if disabled.Textual IP to bytes, becauseInetAddress.getByName(String)is slow.12 char hex lower case.Hosts recently found not to resolve.private static final intFlag of a deprecated interface address.private static final intFlag of a permanent interface address.private static final intFlag of a temporary interface address.private static final FileProc file listing the IPv6 interfaces.private static longWhen the interface cache was read.private static final booleanTrue where the proc interface cache is usable.private static final longTime the interface cache stays valid.private static final longTime a failed lookup is remembered.private static final StringRegex matching the longest zero run.private static final PatternCompiled matcher for the zero run.Constructor Summary
ConstructorsMethod Summary
Modifier and TypeMethodDescriptionstatic voidClears all internal caches.All non-IPv6 addresses excluding local, broadcast, and multicast addresses.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.All addresses excluding only link-local and multicast addresses.static StringFirst non-local IPv4 address found.static Set<AddressType> Efficiently get all connected address types in one pass.static byte[]Caching version ofgetByName(host).getAddress(), which is slow.static byte[]For literal IP addresses, same as getIP(String).static byte[]Caching version ofgetByName(host).getAddress(), which is slow.static List<byte[]> For literal IP addresses, same as getIP(String).private static byte[]Fast IPv4 address parsing that avoids slowInetAddress.getByName(String)on Windows.private static byte[]Fast IPv6 address parsing that avoids slowInetAddress.getByName(String)on Windows.private static longReturn first line in a file as a long.static intConvenience method to convert and validate a port String without throwing an exception.private static StringRFC 4941.static byte[]Caller should cache results for better performance.static booleanDo we have any non-loop, non-wildcard IPv4 address at all? Use getConnectedAddressTypes() to get all types at once for better performance.static booleanisConnected(AddressType type) Do we have any address of this type? UsegetConnectedAddressTypes()to get all types at once for better performance.static booleanDo we have any non-loop, non-wildcard IPv6 address at all? Use getConnectedAddressTypes() to get all types at once for better performance.static booleanisDeprecated(Inet6Address addr) Determines if an IPv6 address is deprecated.static booleanisDynamic(Inet6Address addr) Determines if an IPv6 address is dynamic.static booleanisIPAddress(String host) Checks if the host is a valid IP address (IPv4 or IPv6).static booleanisIPv4Address(String host) Checks if the host is a valid IPv4 address.static booleanisIPv6Address(String host) Checks if the host is a valid IPv6 address.static booleanisTemporary(Inet6Address addr) Determines if an IPv6 address is temporary.static voidPrint out the local addresses.private static voidPrint the addresses to stdout.private static voidprintCanonical(Set<String> a) Print the addresses in canonical form to stdout.private static voidOnly call if INET6_CACHE_ENABLED.private static booleanshouldInclude(InetAddress ia, boolean includeSiteLocal, boolean includeLoopbackAndWildcard, boolean includeIPv6) Checks whether an address counts as one of ours.private static StringstripScope(String ip) Strip the trailing "%nn" fromInet6Address.getHostAddress().static StringtoCanonicalString(byte[] addr) Same asObject.toString()but returns IPv6 in compressed form per RFC 5952.static StringtoCanonicalString(byte[] addr, int port) Same as toString() but returns IPv6 in compressed form per RFC 5952.static StringtoCanonicalString(String host) Converts IPv6 to compressed form per RFC 5952.static StringtoString(byte[] addr) Convenience method to convert an IP address to a String without throwing an exception.static StringtoString(byte[] addr, int port) Convenience method to convert an IP address and port to a String without throwing an exception.static StringChecks if temporary IPv6 addresses are enabled system-wide.private static Stringx_toCanonicalString(String host) Does the RFC 5952 substitution and drops a redundant leading zero group.
Field Details
IF_INET6_FILE
Proc file listing the IPv6 interfaces.INET6_CACHE_EXPIRE
private static final long INET6_CACHE_EXPIRETime the interface cache stays valid.- See Also:
INET6_CACHE_ENABLED
private static final boolean INET6_CACHE_ENABLEDTrue where the proc interface cache is usable.FLAG_PERMANENT
private static final int FLAG_PERMANENTFlag of a permanent interface address.- See Also:
FLAG_DEPRECATED
private static final int FLAG_DEPRECATEDFlag of a deprecated interface address.- See Also:
FLAG_TEMPORARY
private static final int FLAG_TEMPORARYFlag of a temporary interface address.- See Also:
ifCacheTime
private static long ifCacheTimeWhen the interface cache was read._ifCache
Cached interface addresses, null if disabled._macCache
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
Compiled matcher for the zero run._IPAddress
Textual IP to bytes, becauseInetAddress.getByName(String)is slow._negativeCache
NEG_CACHE_TIME
private static final long NEG_CACHE_TIMETime a failed lookup is remembered.- See Also:
Constructor Details
Addresses
private Addresses()Private constructor to prevent instantiation.
Method Details
isConnected
Do we have any address of this type? UsegetConnectedAddressTypes()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
First non-local IPv4 address found.- Returns:
- the first non-local IPv4 address found, or null
getAddresses
getAllAddresses
getAddresses
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 addressesincludeIPv6- 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 addressesincludeLoopbackAndWildcard- 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 addressesincludeLoopbackAndWildcard- whether to include loopback addresses (127.x.x.x) and wildcard (0.0.0.0)includeIPv6- whether to include IPv6 addressesincludeIPv6Temporary- 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
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
Strip the trailing "%nn" fromInet6Address.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 checkincludeSiteLocal- true to accept site local addressesincludeLoopbackAndWildcard- true to accept loopback and wildcardincludeIPv6- true to accept IPv6 addresses- Returns:
- true if the address is local
toString
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
Convenience method to convert an IP address and port to a String without throwing an exception.- Parameters:
addr- the IP address as byte arrayport- the port number- Returns:
- formatted string as "ipv4:port" or "[ipv6]:port", or error message for invalid input
toCanonicalString
Same asObject.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
Same as toString() but returns IPv6 in compressed form per RFC 5952.- Parameters:
addr- the IP address as byte arrayport- the port number- Returns:
- canonical string representation with port, or error message for invalid input
toCanonicalString
x_toCanonicalString
getPort
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
Caching version ofgetByName(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
Caching version ofgetByName(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
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 nullpreferIPv6- whether to prefer IPv6 addresses- Returns:
- IP address bytes or null
getIPs
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
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
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
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
Fast IPv4 address parsing that avoids slowInetAddress.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
Fast IPv6 address parsing that avoids slowInetAddress.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
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 fromgetAddresses()- Returns:
- true if address is dynamic
isDeprecated
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
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
Print out the local addresses.- Parameters:
args- command line arguments
print
printCanonical
useIPv6TempAddresses
Checks if temporary IPv6 addresses are enabled system-wide.- Returns:
- "true", "false", or "unknown" (for Windows/Mac where detection is not supported)
getPrivacyStatus
getLong
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