Class EgressInterfaceFinder
- Since:
- 4.2
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classAn exception representing a failure to determine a default egress network interface. -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionAttempt to find the default egress interface on the current system.fromAggregate(com.fasterxml.uuid.EgressInterfaceFinder.Finder[] finders) Attempt to find the default egress interface on the current system, by trying each of the specified discovery mechanisms, in order, until one of them succeeds.fromDefaultMechanisms(int timeoutMillis) Attempt to find the default egress interface on the current system, using the specified connection timeout duration.fromLocalAddress(InetAddress localAddress) Attempt to find the default egress interface on the current system, by finding aNetworkInterfacethat has the specified network address.fromRemoteConnection(int timeoutMillis, InetSocketAddress remoteAddress) Attempt to find the default egress interface on the current system, by connection to the specified address.fromRemoteDatagramSocketConnection(InetSocketAddress remoteAddress) Attempt to find the default egress interface on the current system, using the specified connection timeout duration and connecting with aDatagramSocket.fromRemoteSocketConnection(int timeoutMillis, InetSocketAddress remoteAddress) Attempt to find the default egress interface on the current system, using the specified connection timeout duration and connecting with aSocket.fromRootNameserverConnection(int timeoutMillis) Attempt to find the default egress interface on the current system, by connecting to one of the root name servers (chosen at random).
-
Field Details
-
DEFAULT_TIMEOUT_MILLIS
public static final int DEFAULT_TIMEOUT_MILLIS- See Also:
-
-
Constructor Details
-
EgressInterfaceFinder
public EgressInterfaceFinder()
-
-
Method Details
-
egressInterface
Attempt to find the default egress interface on the current system.This is done on a best efforts basis, as Java does not provide the necessary level of OS integration that is required to do this robustly. However, this utility should do a decent job on Windows, Linux and macOS so long as the local system has a working network connection at the time of execution. If the current system is multihomed with multiple egress interfaces, one such interface will be chosen indeterminately.
Accurately determining the egress interface necessitates us attempting to make outbound network connections. This will be done synchronously and can be a very slow process. You can tune the amount of time allowed to establish the outbound connections by increasing/decreasing the timeout value.
- Returns:
- the egress interface
- Throws:
EgressInterfaceFinder.EgressResolutionException- if an egress interface could not be determined- Since:
- 4.2
-
fromDefaultMechanisms
public NetworkInterface fromDefaultMechanisms(int timeoutMillis) throws EgressInterfaceFinder.EgressResolutionException Attempt to find the default egress interface on the current system, using the specified connection timeout duration.This will attempt to connect to one of the root DNS nameservers (chosen randomly), and failing that, simply to IPv4 address 1.1.1.1 and finally IPv6 address 1::1.
- Parameters:
timeoutMillis- the amount of time (milliseconds) allowed to establish an outbound connection- Returns:
- the egress interface
- Throws:
EgressInterfaceFinder.EgressResolutionException- if an egress interface could not be determined- Since:
- 4.2
-
fromAggregate
public NetworkInterface fromAggregate(com.fasterxml.uuid.EgressInterfaceFinder.Finder[] finders) throws EgressInterfaceFinder.EgressResolutionException Attempt to find the default egress interface on the current system, by trying each of the specified discovery mechanisms, in order, until one of them succeeds.- Parameters:
finders- array of finder callbacks to be executed- Returns:
- the egress interface
- Throws:
EgressInterfaceFinder.EgressResolutionException- if an egress interface could not be determined- Since:
- 4.2
-
fromRootNameserverConnection
public NetworkInterface fromRootNameserverConnection(int timeoutMillis) throws EgressInterfaceFinder.EgressResolutionException Attempt to find the default egress interface on the current system, by connecting to one of the root name servers (chosen at random).- Parameters:
timeoutMillis- the amount of time (milliseconds) allowed to establish an outbound connection- Returns:
- the egress interface
- Throws:
EgressInterfaceFinder.EgressResolutionException- if an egress interface could not be determined- Since:
- 4.2
-
fromRemoteConnection
public NetworkInterface fromRemoteConnection(int timeoutMillis, InetSocketAddress remoteAddress) throws EgressInterfaceFinder.EgressResolutionException Attempt to find the default egress interface on the current system, by connection to the specified address. This will try two different methods:- using a
DatagramSocket, which seems to work well for Windows & Linux, and is faster to uses thanSocketas opening one does not actually require negotiate a handshake connection, but this does not appear to work on MacOS - using a
Socket, which seems to work better for MacOS, but needs to actually negotiate a connection handshake from a remote host
- Parameters:
timeoutMillis- the amount of time (milliseconds) allowed to establish an outbound connectionremoteAddress- the address to which a connection will be attempted in order to determine which interface is used to connect- Returns:
- the egress interface
- Throws:
EgressInterfaceFinder.EgressResolutionException- if an egress interface could not be determined- Since:
- 4.2
- using a
-
fromRemoteSocketConnection
public NetworkInterface fromRemoteSocketConnection(int timeoutMillis, InetSocketAddress remoteAddress) throws EgressInterfaceFinder.EgressResolutionException Attempt to find the default egress interface on the current system, using the specified connection timeout duration and connecting with aSocket.- Parameters:
timeoutMillis- the amount of time (milliseconds) allowed to establish an outbound connectionremoteAddress- the address to which a connection will be attempted in order to determine which interface is used to connect- Returns:
- the egress interface
- Throws:
EgressInterfaceFinder.EgressResolutionException- if an egress interface could not be determined- Since:
- 4.2
-
fromRemoteDatagramSocketConnection
public NetworkInterface fromRemoteDatagramSocketConnection(InetSocketAddress remoteAddress) throws EgressInterfaceFinder.EgressResolutionException Attempt to find the default egress interface on the current system, using the specified connection timeout duration and connecting with aDatagramSocket.- Parameters:
remoteAddress- the address to which a connection will be attempted in order to determine which interface is used to connect- Returns:
- the egress interface
- Throws:
EgressInterfaceFinder.EgressResolutionException- if an egress interface could not be determined- Since:
- 4.2
-
fromLocalAddress
public NetworkInterface fromLocalAddress(InetAddress localAddress) throws EgressInterfaceFinder.EgressResolutionException Attempt to find the default egress interface on the current system, by finding aNetworkInterfacethat has the specified network address. If more than one interface has the specified address, then one of them will be selected indeterminately.- Parameters:
localAddress- the local address which is assigned to an interface- Returns:
- the egress interface
- Throws:
EgressInterfaceFinder.EgressResolutionException- if an egress interface could not be determined- Since:
- 4.2
-