Class EgressInterfaceFinder

java.lang.Object
com.fasterxml.uuid.EgressInterfaceFinder

public class EgressInterfaceFinder extends Object
A utility to attempt to find the default egress interface on the current system. The egress interface is the interface which is assigned the default network route, such that outbound network traffic is routed out through that interface.
Since:
4.2
  • 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

      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
    • rootNameServerFinder

      private EgressInterfaceFinder.Finder rootNameServerFinder(int timeoutMillis)
    • 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
    • randomRootServerName

      static String randomRootServerName()
    • remoteConnectionFinder

      private EgressInterfaceFinder.Finder remoteConnectionFinder(int timeoutMillis, InetSocketAddress address)
    • 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 than Socket as 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 connection
      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
    • remoteSocketConnectionFinder

      private EgressInterfaceFinder.Finder remoteSocketConnectionFinder(int timeoutMillis, InetSocketAddress address)
      Returns a finder that tries to determine egress interface by connecting to the specified remote address.
      Parameters:
      timeoutMillis - give up after this length of time
      address - the remote address to connect to
      Returns:
      finder callback
    • 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 a Socket.
      Parameters:
      timeoutMillis - the amount of time (milliseconds) allowed to establish an outbound connection
      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
    • remoteDatagramSocketConnectionFinder

      private EgressInterfaceFinder.Finder remoteDatagramSocketConnectionFinder(InetSocketAddress address)
    • 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 a DatagramSocket.
      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 a NetworkInterface that 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