Package de.aristaflow.adept2.util.net
Class NetworkTools
java.lang.Object
de.aristaflow.adept2.util.net.NetworkTools
This class provides tooling related to networking, especially handling of IP addresses with
trailing subnet masks in CIDR notation.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe regexp pattern for an IPv4 address.protected static final AtomicIntegerThe count for unique names to prevent collisions within the same JVM due to the birthday problem.protected static final RandomThe random for creating a unique node name. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic InetAddressfindInterface(byte[] subnetAddress, byte mask) Finds the IPv4 address of a network interface of this host that is in the designated subnet.static InetAddressfindInterface(InetAddress subnetAddress, byte mask) Finds the IPv4 address of a network interface of this host that is in the designated subnet.static StringgetIp4AddressString(byte[] address, byte mask) Gets a string representation for the designated IP (v4) address including a subnet mask if valid (0 < mask <= 32).static StringgetIp4AddressString(Inet4Address address, byte mask) Gets a string representation for the designated IP (v4) address including a subnet mask if valid (0 < mask <= 32).protected static intgetNetworkPrefix(byte[] address, byte mask) Gets the network prefix of the designated IPv4 address using the designated mask (CIDR notation).static StringgetUniqueNodeName(Logger logger) Determines and returns a unique name for the current platform/cluster node.static booleaninSubnet(byte[] address, byte[] subnet, byte mask) Gets whether the designated IPv4 address is in the designated subnet (subnet address + mask).static booleanisLocalAddress(InetAddress ipToCheck) Checks if the specified IPv4 address is one of that addresses which are assigned to this host or if it is an address assigned to a remote host.parseIp4Address(String address) Parses an IPv4 address from the designated string.static Pair<Inet4Address,Byte> resolveIp4Address(String address) Resolves an IPv4 address from the designated string.protected static inttoInt(byte[] address) Converts the (4) designatedbytes to anint.protected static inttoIntMask(byte mask) Converts the designated mask (i.
-
Field Details
-
IP_V4_ADDRESS_PATTERN
The regexp pattern for an IPv4 address.- See Also:
-
random
The random for creating a unique node name. -
nameCount
The count for unique names to prevent collisions within the same JVM due to the birthday problem.
-
-
Constructor Details
-
NetworkTools
public NetworkTools()
-
-
Method Details
-
getIp4AddressString
Gets a string representation for the designated IP (v4) address including a subnet mask if valid (0 < mask <= 32).- Parameters:
address- The IP address asbyte[]for which to get a string representation.mask- The subnet mask for the string representation if (0 < mask <= 32).- Returns:
- A string representation for the designated IP (v4) address including an optional subnet mask.
-
getIp4AddressString
Gets a string representation for the designated IP (v4) address including a subnet mask if valid (0 < mask <= 32).- Parameters:
address- The IP address asbyte[]for which to get a string representation.mask- The subnet mask for the string representation if (0 < mask <= 32).- Returns:
- A string representation for the designated IP (v4) address including an optional subnet mask.
-
parseIp4Address
Parses an IPv4 address from the designated string. This may include a subnet mask in CIDR notation.- Parameters:
address- The IPv4 address/subnet which to parse.- Returns:
- The parsed IPv4 address and the subnet mask. If no subnet mask is provided,
32will be returned as subnet mask to represent a single IP address. - Throws:
IllegalArgumentException- If the designated address is not a valid IPv4 address (optionally including a subnet mask in CIDR notation), anIllegalArgumentExceptionwill be thrown.
-
resolveIp4Address
Resolves an IPv4 address from the designated string. This may include a subnet mask in CIDR notation.- Parameters:
address- The IPv4 address/subnet which to parse and resolve.- Returns:
- The parsed IPv4 address and the subnet mask. If no subnet mask is provided,
32will be returned as subnet mask to represent a single IP address. - Throws:
UnknownHostException- If the designated address cannot be resolved to a corresponding host, anUnknownHostExceptionwill be thrown.
-
toInt
protected static int toInt(byte[] address) Converts the (4) designatedbytes to anint. This is used to represent IPv4 addresses, mostly as subnets.- Parameters:
address- The IPv4 address represented as 4bytes.- Returns:
- An
intrepresenting the designated 4bytes.
-
toIntMask
protected static int toIntMask(byte mask) Converts the designated mask (i. e. a number from CIDR notation) to anintsimply containing the designated amount of consecutive1s starting on the highest bit.- Parameters:
mask- The subnet mask from CIDR notation, which is a number between 0 and 32.- Returns:
- An
inthaving the designated amount of consecutive1s.
-
getNetworkPrefix
protected static int getNetworkPrefix(byte[] address, byte mask) Gets the network prefix of the designated IPv4 address using the designated mask (CIDR notation).- Parameters:
address- The IP address for which to get the network prefix.mask- The subnet mask from CIDR notation, which is a number between 0 and 32.- Returns:
- The network prefix for the designated IPv4 address using the designated mask.
-
inSubnet
public static boolean inSubnet(byte[] address, byte[] subnet, byte mask) Gets whether the designated IPv4 address is in the designated subnet (subnet address + mask). This just returns whether the network prefix ofaddressandsubnetare equal with respect tomask.- Parameters:
address- The IPv4 address which to check for whether it is in the designated subnet.subnet- The address of the subnet.mask- The subnet mask from CIDR notation, which is a number between 0 and 32.- Returns:
- Whether the designated IPv4 address is in the designated subnet (subnet address + mask).
-
findInterface
public static InetAddress findInterface(InetAddress subnetAddress, byte mask) throws SocketException Finds the IPv4 address of a network interface of this host that is in the designated subnet. If no corresponding network interface is found,nullwill be returned.- Parameters:
subnetAddress- The subnet address.mask- The subnet mask from CIDR notation, which is a number between 0 and 32.- Returns:
- The IPv4 address of a network interface of this host that is in the designated subnet
or
nullif no corresponding network interface is found. - Throws:
SocketException- If an I/O error occurs when retrieving all network interfaces of this host, aSocketExceptionwill be thrown.
-
findInterface
Finds the IPv4 address of a network interface of this host that is in the designated subnet. If no corresponding network interface is found,nullwill be returned.- Parameters:
subnetAddress- The subnet address.mask- The subnet mask from CIDR notation, which is a number between 0 and 32.- Returns:
- The IPv4 address of a network interface of this host that is in the designated subnet
or
nullif no corresponding network interface is found. - Throws:
SocketException- If an I/O error occurs when retrieving all network interfaces of this host, aSocketExceptionwill be thrown.
-
isLocalAddress
Checks if the specified IPv4 address is one of that addresses which are assigned to this host or if it is an address assigned to a remote host.- Parameters:
ipToCheck- The IP address to check.- Returns:
- Whether the designated IPv4 address belongs to this host (or a remote host).
- Throws:
SocketException- If an I/O error occurs when retrieving all network interfaces of this host, aSocketExceptionwill be thrown.
-
getUniqueNodeName
Determines and returns a unique name for the current platform/cluster node. This is usually the name of the local host with a trailing random ID. If the host name cannot be determined, a random UUID will be used.- Parameters:
logger- The logger where to log information messages on creating the node name.- Returns:
- A unique node name based on the local host name or a random UUID.
-