Class NetworkUtil

java.lang.Object
io.aeron.driver.media.NetworkUtil

public final class NetworkUtil extends Object
Collection of network specific utility functions.
  • Constructor Details

    • NetworkUtil

      public NetworkUtil()
  • Method Details

    • filterBySubnet

      public static NetworkInterface[] filterBySubnet(InetAddress address, int subnetPrefix) throws SocketException
      Search for a list of network interfaces that match the specified address and subnet prefix. The results will be ordered by the length of the subnet prefix (InterfaceAddress.getNetworkPrefixLength()). If no results match, then the collection will be empty.
      Parameters:
      address - to search for on the NetworkInterfaces.
      subnetPrefix - to limit the search.
      Returns:
      NetworkInterfaces that match the supplied criteria, ordered by the length of the subnet prefix. Empty if none match.
      Throws:
      SocketException - if an error occurs
    • allocateDirectAlignedAndPadded

      public static ByteBuffer allocateDirectAlignedAndPadded(int capacity, int alignment)
      Allocate a direct ByteBuffer that is padded at the end with at least alignment bytes.
      Parameters:
      capacity - for the buffer.
      alignment - for the buffer.
      Returns:
      the direct ByteBuffer.
    • formatAddressAndPort

      public static String formatAddressAndPort(InetAddress address, int port)
      Format an address and port pair, so they can be used in a URI endpoint.
      Parameters:
      address - part of the endpoint.
      port - part of the endpoint.
      Returns:
      The formatted string for an address, IPv4 or IPv6, and port separated by a ':'.
    • getProtocolFamily

      public static ProtocolFamily getProtocolFamily(InetAddress address)
      Get the ProtocolFamily to which the address belongs.
      Parameters:
      address - to get the ProtocolFamily for.
      Returns:
      the ProtocolFamily to which the address belongs.
    • findFirstMatchingLocalAddress

      public static InetAddress findFirstMatchingLocalAddress(InetAddress address) throws SocketException
      Scans through to the local interfaces and returns the first bound InetAddress that matches the locally defined address and network mask. Useful for determining a return path network address for machines with a simple network topology. May not give the ideal result when a machine has multiple interfaces that match an outgoing network address.
      Parameters:
      address - used to scan for a matching local address
      Returns:
      first matching address bound to a local interface or null if none match.
      Throws:
      SocketException - if an underlying SocketException is thrown, e.g. when getting the network interfaces.
    • isMatchWithPrefix

      public static boolean isMatchWithPrefix(byte[] candidate, byte[] expected, int prefixLength)
      Matches to network address with the specified prefix length. Only works with 4 and 16 byte addresses.
      Parameters:
      candidate - address to be matched
      expected - address to match against (could be a network address - with 0s at the end)
      prefixLength - the number of bit required to match.
      Returns:
      true if the leading prefixLength number of bits of the two addresses match.