Class NetworkUtil
-
Field Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
available
(int port) Checks to see if a specific port is available.static byte[]
cidr2Netmask
(short cidr, int byteLength) Converts a given CIDR mask to valid IPv4 or IPv6 network mask.static InetAddress
createInetAddress
(String str) Creates aInetAddress
by parsing the given string.static Set<InetAddress>
Returns a list of the IP addresses of all network interfaces of the local computer.static InetAddress
static NetworkInterface
Returns the default network interface of this system.static Inet4Address
Determines the external IPv4 address.static Inet6Address
Determines the external IPv6 address.static byte[]
getIpv4MappedIPv6AddressBytes
(InetAddress address) static InetAddress
getLocalAddressForRemoteAddress
(InetSocketAddress remoteAddress) Establishes a connection toremoteAddress
and returns the local address used for it.If an error occurs,null
is returned.static String
Returns the local host name.static short
getNetworkPrefixLength
(InetAddress address) Returns the network prefix length for given address.static boolean
isValidNonSpecialIPAddress
(InetAddress address) Check if the given address is a normal IP address that is neither a loopback, link local nor multicast address.static boolean
isValidPort
(int port) Checks if a port is valid or not.static boolean
sameNetwork
(byte[] x, byte[] y, short cidr) Checks if two given addresses are in the same network.static boolean
sameNetwork
(InetAddress a, InetAddress b, short mask) Checks if two given addresses are in the same network.
-
Field Details
-
MIN_PORT_NUMBER
public static final int MIN_PORT_NUMBERThe minimum server port number.- See Also:
-
MAX_PORT_NUMBER
public static final int MAX_PORT_NUMBERThe maximum server port number.- See Also:
-
LOCAL_ADDRESS_FOR_REMOTE_TIMEOUT
-
EXTERNAL_IP_ADDRESS_TIMEOUT
-
-
Method Details
-
getExternalIPv4Address
Determines the external IPv4 address.Note: This is a blocking method, because it connects to external server that may react slowly or not at all.
- Returns:
- the external IPv4 address or
null
in case of error
-
getExternalIPv6Address
Determines the external IPv6 address.Note: This is a blocking method, because it connects to external server that may react slowly or not at all.
- Returns:
- the external IPv6 address or
null
in case of error
-
available
public static boolean available(int port) Checks to see if a specific port is available.Source: Apache camel
- Parameters:
port
- the port number to check for availability- Returns:
- true if the port is available, or false if not
- Throws:
IllegalArgumentException
- is thrown if the port number is out of range
-
isValidPort
public static boolean isValidPort(int port) Checks if a port is valid or not.- Parameters:
port
- port that should be validated.- Returns:
- true if valid, otherwise false
-
getAddresses
Returns a list of the IP addresses of all network interfaces of the local computer. If no IP addresses can be obtained, the loopback address is returned.- Returns:
- list of IP addresses of all network interfaces of local computer or loopback address if no address can be obtained
-
isValidNonSpecialIPAddress
Check if the given address is a normal IP address that is neither a loopback, link local nor multicast address.- Parameters:
address
- the address that should be checked- Returns:
- true if the address is a normal IP address, false otherwise
-
getLocalHostName
Returns the local host name. If no host name can be determined,null
is returned.- Returns:
- the local host name. If no host name can be determined,
null
is returned.
-
createInetAddress
Creates aInetAddress
by parsing the given string.This convenience factory method works as if by invoking the
InetAddress.getByName(java.lang.String)
constructor; anyUnknownHostException
thrown by the constructor is caught and wrapped in a newIllegalArgumentException
object, which is then thrown.This method is provided for use in situations where it is known that the given string is a legal InetAddress, for example for InetAddress constants declared within a program, and so it would be considered a programming error for the string not to parse as such. The constructors, which throw
UnknownHostException
directly, should be used in situations where a InetAddress is being constructed from user input or from some other source that may be prone to errors.- Parameters:
str
- The string to be parsed into a InetAddress- Returns:
- The new InetAddress
- Throws:
IllegalArgumentException
- if no IP address for thestr
could be found, or if a scope_id was specified for a global IPv6 address.
-
getNetworkPrefixLength
Returns the network prefix length for given address. This is also known as the subnet mask in the context of IPv4 addresses. Typical IPv4 values would be 8 (255.0.0.0), 16 (255.255.0.0) or 24 (255.255.255.0).Typical IPv6 values would be 128 (::1/128) or 10 (fe80::203:baff:fe27:1243/10)
- Parameters:
address
- TheInetAddress
to search with.- Returns:
- a
short
representing the prefix length for the subnet of that address or-1
if there is no network interface with given IP address. - Throws:
NullPointerException
- If the specified address isnull
.
-
sameNetwork
Checks if two given addresses are in the same network.- Parameters:
a
- first addressb
- second addressmask
- the network mask in CIDR notation- Returns:
- true if the two given addresses are in the same network, false otherwise
-
sameNetwork
public static boolean sameNetwork(byte[] x, byte[] y, short cidr) Checks if two given addresses are in the same network.- Parameters:
x
- first addressy
- second addresscidr
- the network mask in CIDR notation- Returns:
- true if the two given addresses are in the same network, false otherwise
-
cidr2Netmask
public static byte[] cidr2Netmask(short cidr, int byteLength) Converts a given CIDR mask to valid IPv4 or IPv6 network mask.- Parameters:
cidr
- the cidrbyteLength
- the final mask length (IPv4 = 4 bytes, IPv6 = 16 bytes)- Returns:
- the CIDR as network mask byte array
- Throws:
IllegalArgumentException
- if thebyteLength
argument is invalid
-
getDefaultGateway
-
getIpv4MappedIPv6AddressBytes
-
getLocalAddressForRemoteAddress
Establishes a connection toremoteAddress
and returns the local address used for it.If an error occurs,null
is returned.- Parameters:
remoteAddress
- The remote address- Returns:
- local address used to connect to
remoteAddress
, ornull
in case of error
-
getDefaultInterface
Returns the default network interface of this system.- Returns:
- the default interface
-