java.lang.Object
org.cybergarage.net.HostInterface

public class HostInterfaceextends Object
Utility class for network host interface management and configuration.

This class provides static methods for discovering and managing network interfaces and IP addresses on the local system. It supports both IPv4 and IPv6 addresses, with configurable options for address selection and interface binding.

Key features:

  • Network interface enumeration and discovery
  • IPv4 and IPv6 address management
  • Loopback address inclusion/exclusion
  • Interface-specific binding support
  • Host name and address resolution

This class is essential for UPnP devices that need to bind to specific network interfaces or discover available network addresses for device advertisement and communication.

  • Field Details

    • USE_LOOPBACK_ADDR

      public static boolean USE_LOOPBACK_ADDR
      Whether the loopback address is treated as usable.
    • USE_ONLY_IPV4_ADDR

      public static boolean USE_ONLY_IPV4_ADDR
      USE_ONLY_IPV4_ADDR.
    • USE_ONLY_IPV6_ADDR

      public static boolean USE_ONLY_IPV6_ADDR
      USE_ONLY_IPV6_ADDR.
    • ifAddress

      private static String ifAddress
      /////////////////////////////////////////////
    • IPV4_BITMASK

      public static final int IPV4_BITMASK
      IPV4_BITMASK.
      See Also:
    • IPV6_BITMASK

      public static final int IPV6_BITMASK
      IPV6_BITMASK.
      See Also:
    • LOCAL_BITMASK

      public static final int LOCAL_BITMASK
      Bit selecting addresses that belong to a local network interface.
      See Also:
  • Constructor Details

    • HostInterface

      public HostInterface()
      Constructor. The interface list is read from the static NetworkInterface and InetAddress helpers below, so an instance carries no state.
  • Method Details

    • setInterface

      public static final void setInterface(String ifaddr)
      Restricts interface enumeration to a single address.
      Parameters:
      ifaddr - the address of the single interface to report, empty for all
    • getInterface

      public static final String getInterface()
      Gets the address interface enumeration has been restricted to.
      Returns:
      the assigned interface address, or an empty string if none was assigned
    • hasAssignedInterface

      private static final boolean hasAssignedInterface()
      True if the host has an interface worth using.
      Returns:
      true if an address was assigned
    • isUsableAddress

      private static final boolean isUsableAddress(InetAddress addr)
      Whether the address is usable for a network interface, loopbacks only when they are enabled.
      Parameters:
      addr - the address to check
      Returns:
      true if the address may be used
    • getNHostAddresses

      public static final int getNHostAddresses()
      Counts the usable host addresses the machine has.
      Returns:
      the number of usable host addresses across the network interfaces, or 1 when a single interface has been assigned
    • getInetAddress

      public static final InetAddress[] getInetAddress(int ipfilter, String[] interfaces)
      Collects the addresses of the selected network interfaces.
      Parameters:
      ipfilter - bitmask of IPV4_BITMASK, IPV6_BITMASK and LOCAL_BITMASK; with LOCAL_BITMASK clear, loopback addresses are skipped
      interfaces - names of the network interfaces to inspect, or null for all
      Returns:
      InetAddress[] the addresses found, possibly empty
    • getHostAddress

      public static final String getHostAddress(int n)
      Gets the usable host address at a position in the enumeration.
      Parameters:
      n - the zero-based index into the enumerated usable host addresses
      Returns:
      the address at that index, or an empty string if there are not that many
    • isIPv6Address

      public static final boolean isIPv6Address(String host)
      isIPv6Address.
      Parameters:
      host - the host name or address literal to resolve
      Returns:
      true if host resolves to an IPv6 address
    • isIPv4Address

      public static final boolean isIPv4Address(String host)
      isIPv4Address.
      Parameters:
      host - the host name or address literal to resolve
      Returns:
      true if host resolves to an IPv4 address
    • hasIPv4Addresses

      public static final boolean hasIPv4Addresses()
      hasIPv4Addresses.
      Returns:
      true if any usable host address is IPv4
    • hasIPv6Addresses

      public static final boolean hasIPv6Addresses()
      hasIPv6Addresses.
      Returns:
      true if any usable host address is IPv6
    • getIPv4Address

      public static final String getIPv4Address()
      getIPv4Address.
      Returns:
      the first usable host address that is IPv4, or an empty string if there is none
    • getIPv6Address

      public static final String getIPv6Address()
      getIPv6Address.
      Returns:
      the first usable host address that is IPv6, or an empty string if there is none
    • getHostURL

      public static final String getHostURL(String host, int port, String uri)
      Builds an absolute http URL for a host, port and path.
      Parameters:
      host - the host name or address literal to place in the URL
      port - the TCP port number to place in the URL
      uri - the path and query to append, beginning with a slash
      Returns:
      the absolute http URL naming that host, port and uri, with an IPv6 address literal bracketed