Package monero.common
Class NetworkUtils
java.lang.Object
monero.common.NetworkUtils
Collection of generic network/transport addressing utilities.
No DNS resolution or other I/O is performed: IPv6 and IPv4 literals are
validated by hand, and the address-classification helpers only ever resolve
pre-validated IP literals (which
InetAddress.getByName(String) parses
without consulting a name service). This avoids heavier dependencies.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classImmutable host and port pair. -
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic StringformatHostAndPort(String host, int port) Format a host and port, bracketing the host if it is an IPv6 literal.static booleanisIpv4Literal(String host) Determine if the host is a dotted-quad IPv4 literal (e.g.static booleanisIpv6Literal(String host) Determine if the host is an IPv6 literal (e.g.static booleanDetermine if the given URI's host is an IPv6 literal.static booleanisLocalHost(String uriString) Determine if the given URI's host is the local host (loopback or "localhost").static booleanisLoopbackUrl(String url) Determine if the given URL's host is a loopback address (e.g.static booleanisPrivateIp(String uriString) Determine if the given URI's host is local or a private (non-routable) IP address.static NetworkUtils.HostAndPortparseHostAndPort(String address, int defaultPort) Parse an address of the form "host", "host:port", "[ipv6]" or "[ipv6]:port".static NetworkUtils.HostAndPortparseHostAndPort(String address, int defaultPort, boolean portRequired) Parse an address of the form "host", "host:port", "[ipv6]" or "[ipv6]:port".static URIConvert a string to a URI, defaulting to the http scheme when none is given.static StringstripIpv6Brackets(String host) Strip surrounding brackets from an IPv6 literal, if present.
-
Field Details
-
LOOPBACK_HOST
- See Also:
-
LOCALHOST
- See Also:
-
-
Method Details
-
parseUri
Convert a string to a URI, defaulting to the http scheme when none is given. Unbracketed IPv6 literals are wrapped in brackets so the resulting URI is well-formed (e.g. "::1" becomes "http://[::1]").- Parameters:
uriString- is the string to convert to a URI- Returns:
- the initialized URI
- Throws:
IllegalArgumentException- if the string cannot be parsed as a URI
-
parseHostAndPort
Parse an address of the form "host", "host:port", "[ipv6]" or "[ipv6]:port".- Parameters:
address- is the address to parsedefaultPort- is the port to use when the address has none- Returns:
- the parsed host and port
- Throws:
IllegalArgumentException- if the address is malformed
-
parseHostAndPort
public static NetworkUtils.HostAndPort parseHostAndPort(String address, int defaultPort, boolean portRequired) Parse an address of the form "host", "host:port", "[ipv6]" or "[ipv6]:port".- Parameters:
address- is the address to parsedefaultPort- is the port to use when the address has noneportRequired- throws if true and the address has no port- Returns:
- the parsed host and port
- Throws:
IllegalArgumentException- if the address is malformed
-
formatHostAndPort
Format a host and port, bracketing the host if it is an IPv6 literal.- Parameters:
host- is the host (with or without IPv6 brackets)port- is the port- Returns:
- the formatted "host:port"
-
stripIpv6Brackets
Strip surrounding brackets from an IPv6 literal, if present.- Parameters:
host- is the host to strip- Returns:
- the host without surrounding brackets
-
isIpv6Literal
Determine if the host is an IPv6 literal (e.g. "::1" or "fe80::1%eth0"). Surrounding brackets are tolerated. Pure text validation; performs no DNS resolution or other I/O.- Parameters:
host- is the host to check- Returns:
- true if the host is an IPv6 literal, false otherwise
-
isIpv4Literal
Determine if the host is a dotted-quad IPv4 literal (e.g. "192.168.1.1").- Parameters:
host- is the host to check- Returns:
- true if the host is an IPv4 literal, false otherwise
-
isLocalHost
Determine if the given URI's host is the local host (loopback or "localhost").- Parameters:
uriString- is the URI to check (scheme optional)- Returns:
- true if the host is local, false otherwise or if it cannot be parsed
-
isLoopbackUrl
Determine if the given URL's host is a loopback address (e.g. "127.0.0.1", "::1" or "localhost"). Recognizes the entire IPv4 loopback range (127.0.0.0/8), the IPv6 loopback address (::1) and the conventional "localhost" alias. Hostnames other than "localhost" are treated as non-loopback; no DNS resolution is performed.- Parameters:
url- is the URL to check (scheme optional)- Returns:
- true if the host is a loopback address, false otherwise or if it cannot be parsed
-
isPrivateIp
Determine if the given URI's host is local or a private (non-routable) IP address. Covers loopback, wildcard, link-local, RFC 1918 site-local and IPv6 unique local addresses. Hostnames (non-literals) are treated as not private.- Parameters:
uriString- is the URI to check (scheme optional)- Returns:
- true if the host is local or a private IP, false otherwise
-
isIpv6Uri
Determine if the given URI's host is an IPv6 literal.- Parameters:
uriString- is the URI to check (scheme optional)- Returns:
- true if the host is an IPv6 literal, false otherwise
-