Class NetUtils
java.lang.Object
org.opendaylight.openflowplugin.libraries.liblldp.NetUtils
Utility class containing the common utility functions needed for operating on networking data structures.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
Constant holding the number of bytes in MAC Address.static final int
Constant holding the number of words in MAC Address.static final int
Constant holding the number of bits in a byte. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic int
byteArray4ToInt
(byte[] ba) Converts a 4 bytes array into an integer number.static long
byteArray6ToLong
(byte[] ba) Converts a 6 bytes array into a long number MAC addresses.static boolean
fieldsConflict
(int field1, int field2) static byte[]
Returns Broadcast MAC Address.static InetAddress
gethighestIP
(boolean v6) Returns the highest v4 or v6 InetAddress.static InetAddress
getInetAddress
(int address) Converts an IP address passed as integer value into the respective InetAddress object.static InetAddress
getInetNetworkMask
(int prefixMaskLength, boolean isV6) Return the InetAddress Network Mask given the length of the prefix bit mask.static int
getSubnetMaskLength
(byte[] subnetMask) Returns the prefix size in bits of the specified subnet mask.static int
getSubnetMaskLength
(InetAddress subnetMask) Returns the prefix size in bits of the specified subnet mask.static InetAddress
getSubnetPrefix
(InetAddress ip, int maskLen) Given an IP address and a prefix network mask length, it returns the equivalent subnet prefix IP address Example: for ip = "172.28.30.254" and maskLen = 25 it will return "172.28.30.128".static int
getUnsignedByte
(byte value) Returns the unsigned value of the passed byte variable.static int
getUnsignedShort
(short value) Return the unsigned value of the passed short variable.static boolean
inetAddressConflict
(InetAddress testAddress, InetAddress filterAddress, InetAddress testMask, InetAddress filterMask) Checks if the test address and mask conflicts with the filter address and mask.static byte[]
intToByteArray4
(int number) Converts an integer number into a 4 bytes array.static boolean
isAny
(InetAddress ip) Returns true if the passed InetAddress contains all zero.static boolean
isBroadcastMACAddr
(byte[] macAddress) Returns true if the MAC address is the broadcast MAC address and false otherwise.static boolean
isIPAddressValid
(String cidr) Checks if the passed IP address in string form is a valid v4 or v6 address.static boolean
isIPv4AddressValid
(String cidr) Checks if the passed IP v4 address in string form is valid The address may specify a mask at the end as "/MM".static boolean
isIPv6AddressValid
(String cidr) Checks if the passed IP v6 address in string form is valid The address may specify a mask at the end as "/MMM".static boolean
isMulticastMACAddr
(byte[] macAddress) Returns true if the MAC address is a multicast MAC address and false otherwise.static boolean
isUnicastMACAddr
(byte[] macAddress) Returns true if the MAC address is a unicast MAC address and false otherwise.static boolean
isZeroMAC
(byte[] mac) Returns true if the passed MAC address is all zero.static byte[]
longToByteArray6
(long addr) Converts a long number to a 6 bytes array for MAC addresses.static InetAddress
parseInetAddress
(String addressString)
-
Field Details
-
NUM_BITS_IN_A_BYTE
public static final int NUM_BITS_IN_A_BYTEConstant holding the number of bits in a byte.- See Also:
-
MAC_ADDR_LENGTH_IN_BYTES
public static final int MAC_ADDR_LENGTH_IN_BYTESConstant holding the number of bytes in MAC Address.- See Also:
-
MAC_ADDR_LENGTH_IN_WORDS
public static final int MAC_ADDR_LENGTH_IN_WORDSConstant holding the number of words in MAC Address.- See Also:
-
-
Constructor Details
-
NetUtils
public NetUtils()
-
-
Method Details
-
byteArray4ToInt
public static int byteArray4ToInt(byte[] ba) Converts a 4 bytes array into an integer number.- Parameters:
ba
- the 4 bytes long byte array- Returns:
- the integer number
-
byteArray6ToLong
public static long byteArray6ToLong(byte[] ba) Converts a 6 bytes array into a long number MAC addresses.- Parameters:
ba
- The 6 bytes long byte array.- Returns:
- The long number.
Zero is returned if
ba
isnull
or the length of it is not six.
-
longToByteArray6
public static byte[] longToByteArray6(long addr) Converts a long number to a 6 bytes array for MAC addresses.- Parameters:
addr
- The long number.- Returns:
- The byte array.
-
intToByteArray4
public static byte[] intToByteArray4(int number) Converts an integer number into a 4 bytes array.- Parameters:
number
- the integer number- Returns:
- the byte array
-
getInetAddress
Converts an IP address passed as integer value into the respective InetAddress object.- Parameters:
address
- the IP address in integer form- Returns:
- the IP address in InetAddress form
-
getInetNetworkMask
Return the InetAddress Network Mask given the length of the prefix bit mask. The prefix bit mask indicates the contiguous leading bits that are NOT masked out. Example: A prefix bit mask length of 8 will give an InetAddress Network Mask of 255.0.0.0- Parameters:
prefixMaskLength
- integer representing the length of the prefix network maskisV6
- boolean representing the IP version of the returned address
-
getSubnetMaskLength
public static int getSubnetMaskLength(byte[] subnetMask) Returns the prefix size in bits of the specified subnet mask. Example: For the subnet mask ff.ff.ff.e0 it returns 25 while for ff.00.00.00 it returns 8. If the passed subnetMask array is null, 0 is returned.- Parameters:
subnetMask
- the subnet mask as byte array- Returns:
- the prefix length as number of bits
-
getSubnetMaskLength
Returns the prefix size in bits of the specified subnet mask. Example: For the subnet mask 255.255.255.128 it returns 25 while for 255.0.0.0 it returns 8. If the passed subnetMask object is null, 0 is returned- Parameters:
subnetMask
- the subnet mask as InetAddress- Returns:
- the prefix length as number of bits
-
getSubnetPrefix
Given an IP address and a prefix network mask length, it returns the equivalent subnet prefix IP address Example: for ip = "172.28.30.254" and maskLen = 25 it will return "172.28.30.128".- Parameters:
ip
- the IP address in InetAddress formmaskLen
- the length of the prefix network mask- Returns:
- the subnet prefix IP address in InetAddress form
-
inetAddressConflict
public static boolean inetAddressConflict(InetAddress testAddress, InetAddress filterAddress, InetAddress testMask, InetAddress filterMask) Checks if the test address and mask conflicts with the filter address and mask. For example:testAddress: 172.28.2.23 testMask: 255.255.255.0 filterAddress: 172.28.1.10 testMask: 255.255.255.0 do conflict
testAddress: 172.28.2.23 testMask: 255.255.255.0 filterAddress: 172.28.1.10 testMask: 255.255.0.0 do not conflict
Null parameters are permitted.
-
isZeroMAC
public static boolean isZeroMAC(byte[] mac) Returns true if the passed MAC address is all zero.- Parameters:
mac
- the byte array representing the MAC address- Returns:
- true if all MAC bytes are zero
-
isBroadcastMACAddr
public static boolean isBroadcastMACAddr(byte[] macAddress) Returns true if the MAC address is the broadcast MAC address and false otherwise. -
isUnicastMACAddr
public static boolean isUnicastMACAddr(byte[] macAddress) Returns true if the MAC address is a unicast MAC address and false otherwise. -
isMulticastMACAddr
public static boolean isMulticastMACAddr(byte[] macAddress) Returns true if the MAC address is a multicast MAC address and false otherwise. Note that this explicitly returns false for the broadcast MAC address. -
isAny
Returns true if the passed InetAddress contains all zero.- Parameters:
ip
- the IP address to test- Returns:
- true if the address is all zero
-
fieldsConflict
public static boolean fieldsConflict(int field1, int field2) -
parseInetAddress
-
isIPv4AddressValid
Checks if the passed IP v4 address in string form is valid The address may specify a mask at the end as "/MM".- Parameters:
cidr
- the v4 address as A.B.C.D/MM
-
isIPv6AddressValid
Checks if the passed IP v6 address in string form is valid The address may specify a mask at the end as "/MMM".- Parameters:
cidr
- the v6 address as A::1/MMM
-
isIPAddressValid
Checks if the passed IP address in string form is a valid v4 or v6 address. The address may specify a mask at the end as "/MMM"- Parameters:
cidr
- the v4 or v6 address as IP/MMM
-
getUnsignedByte
public static int getUnsignedByte(byte value) Returns the unsigned value of the passed byte variable.- Parameters:
value
- the byte value- Returns:
- the int variable containing the unsigned byte value
-
getUnsignedShort
public static int getUnsignedShort(short value) Return the unsigned value of the passed short variable.- Parameters:
value
- the short value- Returns:
- the int variable containing the unsigned short value
-
gethighestIP
Returns the highest v4 or v6 InetAddress.- Parameters:
v6
- true for IPv6, false for Ipv4- Returns:
- The highest IPv4 or IPv6 address
-
getBroadcastMACAddr
public static byte[] getBroadcastMACAddr()Returns Broadcast MAC Address.- Returns:
- the byte array containing broadcast mac address
-