Package org.dellroad.stuff.net
Class IPv4Util
java.lang.Object
org.dellroad.stuff.net.IPv4Util
Utility methods for working with IPv4 addresses and netmasks.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
static final int
-
Method Summary
Modifier and TypeMethodDescriptionstatic Inet4Address
fromString
(String string) Convert from string representation.static Inet4Address
getBaseAddress
(Inet4Address address, Inet4Address netmask) Get the base address (all host bits zero) of the given IP network.static Inet4Address
getBroadcastAddress
(Inet4Address address, Inet4Address netmask) Get the broadcast address (all host bits one) of the given IP network.static Inet4Address
getNetmaskForWidth
(int width) Get netmask with the given width.static int
getWidth
(Inet4Address netmask) Get netmask width.static boolean
isOnNetwork
(Inet4Address address, Inet4Address network, Inet4Address netmask) Determine if the given address lives on the given network.static boolean
isValidHostOnNetwork
(Inet4Address address, Inet4Address network, Inet4Address netmask) Determine if the given address is a valid host address on the given network.static boolean
isValidNetmask
(Inet4Address netmask) Determine if the given address is a valid CIDR netmask.static Inet4Address
toAddress
(byte[] bytes) Convert raw bytes to anInet4Address
.static Inet4Address
toAddress
(int address) Convert from 32-bit integer representation.static int
toInt32
(Inet4Address address) Convert to 32-bit integer representation.static String
toString
(Inet4Address address) Convert an IPv4 address to string representation.static String
toString
(Inet4Address address, Inet4Address netmask) Convert a network address (IPv4 base address and netmask) to string representation.static long
toUInt32
(Inet4Address address) Convert to an unsigned 32-bit integer representation within a 64-bit long.
-
Field Details
-
MIN_VALID_NETMASK_WIDTH
public static final int MIN_VALID_NETMASK_WIDTH- See Also:
-
MAX_VALID_NETMASK_WIDTH
public static final int MAX_VALID_NETMASK_WIDTH- See Also:
-
-
Method Details
-
toAddress
Convert from 32-bit integer representation.- Parameters:
address
- IP address as a 32-bit integer- Returns:
- the corresponding IP address
-
toInt32
Convert to 32-bit integer representation.- Parameters:
address
- IP address- Returns:
- the corresponding 32-bit integer
-
toUInt32
Convert to an unsigned 32-bit integer representation within a 64-bit long.- Parameters:
address
- IP address- Returns:
- the corresponding unsigned 32-bit integer
-
toAddress
Convert raw bytes to anInet4Address
.- Parameters:
bytes
- IP address bytes (big endian)- Returns:
- corresponding IP address
- Throws:
IllegalArgumentException
- if array does not have length four
-
toString
Convert an IPv4 address to string representation.- Parameters:
address
- IP address- Returns:
address
inString
form- Throws:
IllegalArgumentException
- if array does not have length four
-
toString
Convert a network address (IPv4 base address and netmask) to string representation. Example:192.168.1.39/24
- Parameters:
address
- IP addressnetmask
- netmask for network- Returns:
- address plus netmask width
- Throws:
IllegalArgumentException
- if array does not have length four
-
fromString
Convert from string representation.- Parameters:
string
- IP address in string form- Returns:
- parsed IP address
- Throws:
IllegalArgumentException
- if string is not an IP address
-
getWidth
Get netmask width.- Parameters:
netmask
- netmask- Returns:
- netmask width in bits, a value between zero and 32 (inclusive)
- Throws:
IllegalArgumentException
- if the value is not a valid CIDR netmask
-
isValidNetmask
Determine if the given address is a valid CIDR netmask. The netmask must have a width between 8 and 30 (inclusive).- Parameters:
netmask
- netmask- Returns:
- true if
netmask
is a valid netmask
-
getNetmaskForWidth
Get netmask with the given width.- Parameters:
width
- netmask width in bits- Returns:
- netmask with the given width
- Throws:
IllegalArgumentException
- if width is less than zero or greater than 32
-
getBaseAddress
Get the base address (all host bits zero) of the given IP network.- Parameters:
address
- any IP address on the networknetmask
- the netmask of the network- Returns:
- base address for the specified network
- Throws:
IllegalArgumentException
- ifnetmask
is not a valid netmask
-
getBroadcastAddress
Get the broadcast address (all host bits one) of the given IP network.- Parameters:
address
- any IP address on the networknetmask
- the netmask of the network- Returns:
- broadcast address for the specified network
- Throws:
IllegalArgumentException
- ifnetmask
is not a valid netmask
-
isOnNetwork
Determine if the given address lives on the given network.- Parameters:
address
- IP address in questionnetwork
- network addressnetmask
- network netmask- Returns:
- true if
address
is on the specified network - Throws:
IllegalArgumentException
- ifnetmask
is not a valid netmask
-
isValidHostOnNetwork
public static boolean isValidHostOnNetwork(Inet4Address address, Inet4Address network, Inet4Address netmask) Determine if the given address is a valid host address on the given network. The address must lie on the given network and not equal either the base or broadcast addresses.- Parameters:
address
- IP address in questionnetwork
- network addressnetmask
- network netmask- Returns:
- true if
address
is valid on the specified network - Throws:
IllegalArgumentException
- ifnetmask
is not a valid netmask
-