java.lang.Object
org.cybergarage.net.HostInterface
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 Summary
Modifier and TypeFieldDescriptionprivate static String/////////////////////////////////////////////static final intIPV4_BITMASK.static final intIPV6_BITMASK.static final intBit selecting addresses that belong to a local network interface.static booleanWhether the loopback address is treated as usable.static booleanUSE_ONLY_IPV4_ADDR.static booleanUSE_ONLY_IPV6_ADDR.Constructor Summary
ConstructorsMethod Summary
Modifier and TypeMethodDescriptionstatic final StringgetHostAddress(int n) Gets the usable host address at a position in the enumeration.static final StringgetHostURL(String host, int port, String uri) Builds an absolute http URL for a host, port and path.static final InetAddress[]getInetAddress(int ipfilter, String[] interfaces) Collects the addresses of the selected network interfaces.static final StringGets the address interface enumeration has been restricted to.static final StringgetIPv4Address.static final StringgetIPv6Address.static final intCounts the usable host addresses the machine has.private static final booleanTrue if the host has an interface worth using.static final booleanhasIPv4Addresses.static final booleanhasIPv6Addresses.static final booleanisIPv4Address(String host) isIPv4Address.static final booleanisIPv6Address(String host) isIPv6Address.private static final booleanisUsableAddress(InetAddress addr) Whether the address is usable for a network interface, loopbacks only when they are enabled.static final voidsetInterface(String ifaddr) Restricts interface enumeration to a single address.
Field Details
USE_LOOPBACK_ADDR
public static boolean USE_LOOPBACK_ADDRWhether the loopback address is treated as usable.USE_ONLY_IPV4_ADDR
public static boolean USE_ONLY_IPV4_ADDRUSE_ONLY_IPV4_ADDR.USE_ONLY_IPV6_ADDR
public static boolean USE_ONLY_IPV6_ADDRUSE_ONLY_IPV6_ADDR.ifAddress
/////////////////////////////////////////////IPV4_BITMASK
IPV6_BITMASK
LOCAL_BITMASK
public static final int LOCAL_BITMASKBit 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
Restricts interface enumeration to a single address.- Parameters:
ifaddr- the address of the single interface to report, empty for all
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
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
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 skippedinterfaces- names of the network interfaces to inspect, or null for all- Returns:
- InetAddress[] the addresses found, possibly empty
getHostAddress
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
isIPv6Address.- Parameters:
host- the host name or address literal to resolve- Returns:
- true if host resolves to an IPv6 address
isIPv4Address
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
getIPv4Address.- Returns:
- the first usable host address that is IPv4, or an empty string if there is none
getIPv6Address
getIPv6Address.- Returns:
- the first usable host address that is IPv6, or an empty string if there is none
getHostURL
Builds an absolute http URL for a host, port and path.- Parameters:
host- the host name or address literal to place in the URLport- the TCP port number to place in the URLuri- 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