Class NetUtils


  • public abstract class NetUtils
    extends Object
    Utility class containing the common utility functions needed for operating on networking data structures.
    • Field Detail

      • NUM_BITS_IN_A_BYTE

        public static final int NUM_BITS_IN_A_BYTE
        Constant holding the number of bits in a byte.
        See Also:
        Constant Field Values
      • MAC_ADDR_LENGTH_IN_BYTES

        public static final int MAC_ADDR_LENGTH_IN_BYTES
        Constant holding the number of bytes in MAC Address.
        See Also:
        Constant Field Values
      • MAC_ADDR_LENGTH_IN_WORDS

        public static final int MAC_ADDR_LENGTH_IN_WORDS
        Constant holding the number of words in MAC Address.
        See Also:
        Constant Field Values
    • Constructor Detail

      • NetUtils

        public NetUtils()
    • Method Detail

      • 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 is null 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

        public static InetAddress getInetAddress​(int address)
        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

        public static InetAddress getInetNetworkMask​(int prefixMaskLength,
                                                     boolean isV6)
        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 mask
        isV6 - 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

        public static int getSubnetMaskLength​(InetAddress subnetMask)
        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

        public 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".
        Parameters:
        ip - the IP address in InetAddress form
        maskLen - 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

        public static boolean isAny​(InetAddress ip)
        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

        public static InetAddress parseInetAddress​(String addressString)
      • isIPv4AddressValid

        public 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".
        Parameters:
        cidr - the v4 address as A.B.C.D/MM
      • isIPv6AddressValid

        public 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".
        Parameters:
        cidr - the v6 address as A::1/MMM
      • isIPAddressValid

        public static boolean isIPAddressValid​(String cidr)
        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

        public static InetAddress gethighestIP​(boolean v6)
        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