java.lang.Object
net.i2p.router.transport.TransportUtil

public abstract class TransportUtilextends Object
Utility class providing common transport operations and configurations.

This class contains static methods and constants used throughout the transport system for address validation, port management, IPv6 configuration, and network utility functions.

Key Features:

  • IPv4 and IPv6 address validation and parsing
  • Port range validation and assignment
  • IPv6 configuration options for transports
  • Firewall and NAT detection utilities
  • Geographic IP filtering and validation
  • Transport-specific constants and defaults

IPv6 Support:

  • Configuration options for NTCP and SSU transports
  • Address format validation for IPv6
  • Port assignment and range checking
  • Compatibility helpers for mixed IPv4/IPv6 environments

Network Utilities:

  • Port assignment strategies (random vs. configured)
  • Firewall detection and bypass methods
  • Transport protocol constants and defaults
  • Address type detection and validation
  • Field Details

    • NTCP_IPV6_CONFIG

      public static final String NTCP_IPV6_CONFIG
      NTCP_IPV6_CONFIG.
      See Also:
    • SSU_IPV6_CONFIG

      public static final String SSU_IPV6_CONFIG
      SSU_IPV6_CONFIG.
      See Also:
    • PROP_IPV4_FIREWALLED

      public static final String PROP_IPV4_FIREWALLED
      PROP_IPV4_FIREWALLED.
      See Also:
    • PROP_IPV6_FIREWALLED

      public static final String PROP_IPV6_FIREWALLED
      If true, this router believes its IPv6 address is firewalled.
      See Also:
    • PROP_PORT_PFX

      private static final String PROP_PORT_PFX
      Config keys for the data port range and minimums.
      See Also:
    • PROP_MIN_PORT_SFX

      private static final String PROP_MIN_PORT_SFX
      Suffix of a transport's minimum-port property.
      See Also:
    • PROP_MAX_PORT_SFX

      private static final String PROP_MAX_PORT_SFX
      Suffix of a transport's maximum-port property.
      See Also:
    • MIN_RANDOM_PORT

      private static final int MIN_RANDOM_PORT
      8998 is monotone, and 31000 is the wrapper outbound, so let's stay between those Was 9111, increase to skip Tor browser at 9050.
      See Also:
    • MAX_RANDOM_PORT

      private static final int MAX_RANDOM_PORT
      Highest port chosen by the random picker.
      See Also:
    • YGGDRASIL_PATTERN

      private static final Pattern YGGDRASIL_PATTERN
      Matches an address on the Yggdrasil network.
    • BY_NAME

      private static final Map<String, TransportUtil.IPv6Config> BY_NAME
      The IPv6 modes, by config value.
    • DEFAULT_IPV6_CONFIG

      public static final TransportUtil.IPv6Config DEFAULT_IPV6_CONFIG
      DEFAULT_IPV6_CONFIG.
  • Constructor Details

    • TransportUtil

      public TransportUtil()
      Constructor for subclasses; the operations in this base class are static.
  • Method Details

    • getIPv6Config

      public static TransportUtil.IPv6Config getIPv6Config(RouterContext ctx, String transportStyle)
      The IPv6 config for the given transport style.
      Parameters:
      ctx - the router context
      transportStyle - the transport to read the config for, NTCP or SSU
      Returns:
      the IPv6 config
    • getIPv6Config

      public static TransportUtil.IPv6Config getIPv6Config(String cfg)
      The IPv6 config for the given config string.
      Parameters:
      cfg - the config string to look up
      Returns:
      the IPv6 config
    • isIPv4Firewalled

      public static boolean isIPv4Firewalled(RouterContext ctx)
      This returns true if the force-firewalled setting is configured, false otherwise.
      Parameters:
      ctx - the router context
      Returns:
      whether i pv4 firewalled
    • isIPv6Firewalled

      public static boolean isIPv6Firewalled(RouterContext ctx)
      This returns true if the force-firewalled setting is configured, false otherwise.
      Parameters:
      ctx - the router context
      Returns:
      whether IPv6 firewalled
    • isIPv6

      public static boolean isIPv6(RouterAddress addr)
      Addresses without a host (i.e. w/introducers) are assumed to be IPv4 unless a '6' cap is present.
      Parameters:
      addr - non-null
      Returns:
      whether IPv6
    • isUsableRouterAddress

      public static boolean isUsableRouterAddress(RouterAddress ra)
      Is the RouterAddress usable for tunnel building?

      SSU requires protocol v2 plus a valid IP/port or an introduction; SSU2 requires a valid IP/port or an introduction; NTCP/NTCP2 require a valid IP/port.

      Lives here rather than in TunnelPeerSelector because both the tunnel selectors and ProfileOrganizer need it, and the latter must not reach into tunnel.pool — that package already depends on peermanager, so the reverse edge would close a cycle.

      Pure decision: no context access, safe for unit tests.

      Parameters:
      ra - the router address to check (non-null)
      Returns:
      true if the address is usable
    • hasUsableTransportAddress

      public static boolean hasUsableTransportAddress(RouterInfo ri)
      Does this RouterInfo carry at least one address we could still send to?

      A peer whose addresses have all expired or become unusable cannot be selected for a build and cannot be pre-connected to, so admitting it to a selection tier only produces a candidate that is guaranteed to fail. Gate promotion on this.

      Pure decision: takes the RouterInfo rather than consulting the netdb, so callers holding an entry do not pay for a second lookup.

      Parameters:
      ri - the router info to check (non-null)
      Returns:
      true if at least one address is usable
    • isYggdrasil

      public static boolean isYggdrasil(RouterAddress addr)
      Whether the address is an in-network Yggdrasil address.
      Parameters:
      addr - the address to classify
      Returns:
      true if the host string matches the Yggdrasil overlay pattern
    • getType

      public static AddressType getType(RouterAddress addr)
      Address type of the given RouterAddress.
      Parameters:
      addr - the address to classify
      Returns:
      null if unknown
    • getType

      public static AddressType getType(String host)
      Address type of the given host string.
      Parameters:
      host - the host string to classify
      Returns:
      null if unknown
    • getType

      public static AddressType getType(byte[] ip)
      Address type of the given IP address.
      Parameters:
      ip - the raw 4 or 16 byte address to classify
      Returns:
      null if unknown
    • isPubliclyRoutable

      public static boolean isPubliclyRoutable(byte[] addr, boolean allowIPv6)
      Whether the address is publicly routable, optionally allowing IPv6.
      Parameters:
      addr - non-null
      allowIPv6 - true to treat global IPv6 addresses as routable
      Returns:
      whether publicly routable
    • isPubliclyRoutable

      public static boolean isPubliclyRoutable(byte[] addr, boolean allowIPv4, boolean allowIPv6)
      Ref: RFC 5735.
      Parameters:
      addr - non-null
      allowIPv4 - true to treat global IPv4 addresses as routable
      allowIPv6 - true to treat global IPv6 addresses as routable
      Returns:
      whether publicly routable
    • isValidPort

      public static boolean isValidPort(int port)
      Is this a valid port for us or a remote router?

      ref: http://i2p-projekt.i2p/en/docs/ports ref: https://cs.chromium.org/chromium/src/net/base/port_util.cc

      Parameters:
      port - the port to test
      Returns:
      whether valid port
    • logInvalidPort

      public static void logInvalidPort(Log log, String transportStyle, int port)
      Logs an error for an invalid port.
      Parameters:
      log - the logger to write to
      transportStyle - the transport that was configured
      port - the rejected port
    • selectRandomPort

      public static int selectRandomPort(RouterContext ctx, String transportStyle)
      Pick a random port between the configured boundaries.
      Parameters:
      ctx - the router context
      transportStyle - the transport the port is for, NTCP or SSU
      Returns:
      the port chosen