Package io.aeron.driver.media
Class NetworkUtil
java.lang.Object
io.aeron.driver.media.NetworkUtil
Collection of network specific utility functions.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic ByteBufferallocateDirectAlignedAndPadded(int capacity, int alignment) Allocate a directByteBufferthat is padded at the end with at least alignment bytes.static NetworkInterface[]filterBySubnet(InetAddress address, int subnetPrefix) Search for a list of network interfaces that match the specified address and subnet prefix.static InetAddressfindFirstMatchingLocalAddress(InetAddress address) Scans through to the local interfaces and returns the first bound InetAddress that matches the locally defined address and network mask.static StringformatAddressAndPort(InetAddress address, int port) Format an address and port pair, so they can be used in a URI endpoint.static ProtocolFamilygetProtocolFamily(InetAddress address) Get theProtocolFamilyto which the address belongs.static booleanisMatchWithPrefix(byte[] candidate, byte[] expected, int prefixLength) Matches to network address with the specified prefix length.
-
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 theNetworkInterfaces.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
Allocate a directByteBufferthat is padded at the end with at least alignment bytes.- Parameters:
capacity- for the buffer.alignment- for the buffer.- Returns:
- the direct
ByteBuffer.
-
formatAddressAndPort
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
Get theProtocolFamilyto which the address belongs.- Parameters:
address- to get theProtocolFamilyfor.- Returns:
- the
ProtocolFamilyto which the address belongs.
-
findFirstMatchingLocalAddress
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 matchedexpected- 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.
-