Package monero.common

Class NetworkUtils

java.lang.Object
monero.common.NetworkUtils

public class NetworkUtils extends Object
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.
  • Field Details

  • Method Details

    • parseUri

      public static URI parseUri(String uriString)
      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

      public static NetworkUtils.HostAndPort parseHostAndPort(String address, int defaultPort)
      Parse an address of the form "host", "host:port", "[ipv6]" or "[ipv6]:port".
      Parameters:
      address - is the address to parse
      defaultPort - 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 parse
      defaultPort - is the port to use when the address has none
      portRequired - throws if true and the address has no port
      Returns:
      the parsed host and port
      Throws:
      IllegalArgumentException - if the address is malformed
    • formatHostAndPort

      public static String formatHostAndPort(String host, int port)
      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

      public static String stripIpv6Brackets(String host)
      Strip surrounding brackets from an IPv6 literal, if present.
      Parameters:
      host - is the host to strip
      Returns:
      the host without surrounding brackets
    • isIpv6Literal

      public static boolean isIpv6Literal(String host)
      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

      public static boolean isIpv4Literal(String host)
      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

      public static boolean isLocalHost(String uriString)
      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

      public static boolean isLoopbackUrl(String url)
      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

      public static boolean isPrivateIp(String uriString)
      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

      public static boolean isIpv6Uri(String uriString)
      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