Package utils

Class InterfaceToIp

java.lang.Object
utils.InterfaceToIp

public class InterfaceToIp extends Object
Resolves the process-wide bind address (babel.address) the node binds and announces on.

Why this matters for the demo: for multicast auto-discovery (and for peers to reach each other at all) every node must advertise a reachable address — not 127.0.0.1 and not 0.0.0.0. So the demo never silently defaults to loopback. Instead resolveBindAddress(Properties) picks an address in this order:

  1. an explicit babel.address (the user's override always wins — including loopback, e.g. babel.address=127.0.0.1 for running several nodes on one disconnected machine);
  2. the IPv4 address of the interface named in babel.interface (e.g. babel.interface=en0 on macOS, eth0 on Linux);
  3. experimental auto-detection: the routable IPv4 of the sole real interface — up, non-loopback, non-point-to-point, non-virtual, with a non-link-local IPv4, and whose name is not a bridge / VM / container / VPN adapter (isLikelyNonPhysical(java.net.NetworkInterface)). If several real interfaces qualify it refuses to guess and lists them; if only bridges/VPNs (or nothing) exist it asks the operator to choose.
In the refuse/none cases it fails loudly and tells the operator to pass babel.interface or babel.address explicitly. The auto-detection heuristic is name-based and best-effort — an explicit babel.interface / babel.address always wins and bypasses it.

The parameter keys babel.address / babel.interface / babel.port are owned by babel-core (Babel.PAR_DEFAULT_ADDRESS / Babel.PAR_DEFAULT_INTERFACE / Babel.PAR_DEFAULT_PORT); this class references those constants rather than re-declaring the literals.

  • Method Details

    • getIpOfInterface

      public static String getIpOfInterface(String interfaceName) throws SocketException
      Returns the first non-loopback IPv4 address of interfaceName, or null if it has none / does not exist.
      Throws:
      SocketException
    • autoDetectAddress

      public static String autoDetectAddress() throws SocketException
      Auto-detect a reachable bind address: the routable IPv4 of the sole real (non-loopback, up, non-virtual, non-p2p, non-bridge/VM/VPN) interface. Returns null if none — or more than one — qualifies.
      Throws:
      SocketException
    • resolveBindAddress

      public static String resolveBindAddress(Properties props) throws SocketException, InvalidParameterException
      Resolve the process-wide bind address into babel.address (see the class javadoc for the precedence). On return babel.address is guaranteed to be set to a concrete IPv4 address.
      Returns:
      a short human-readable description of how the address was chosen (for startup logging), e.g. "explicit babel.address", "interface en0", or "auto-detected interface en0".
      Throws:
      InvalidParameterException - if a named babel.interface has no usable address, or no address could be determined at all — in which case the operator must pass babel.interface or babel.address on the command line.
      SocketException
    • describeInterfaces

      public static String describeInterfaces()
      Logs every network interface and its IPv4 addresses (with up/loopback/virtual/ point-to-point flags) to help diagnose a wrong auto-selection. Written to the log file; never throws.