Class StunObserver

java.lang.Object
org.cloudburstmc.netty.util.nethernet.StunObserver
All Implemented Interfaces:
AutoCloseable

public final class StunObserver extends Object implements AutoCloseable
Asks one STUN server for this host's public address from the ICE port, through a native monitor on the socket the peers share. Built-in and external signaling both announce what it finds, and it logs the same way for both.

It keeps the latest mapping for each of the server's addresses, which counts while younger than the max age and goes as soon as the server reports another. A monitor that fails is closed, its mappings withdrawn, and opened again after RETRY_DELAY. A server given by name is also looked up again after a max age without a mapping, and asked on one address once its other one proves unreachable.

Its owner polls it, one thread at a time; mappings may be read from any.

  • Field Details

    • RETRY_DELAY

      public static final Duration RETRY_DELAY
      How long a failed monitor waits before it is opened again.
    • FIRST_ANSWER

      public static final Duration FIRST_ANSWER
      How long a server may stay silent after opening before that is reported.
  • Constructor Details

    • StunObserver

      public StunObserver(InetSocketAddress server, Duration maxAge, int icePort, StunObserver.MonitorFactory factory, LongSupplier nanoTime)
      Parameters:
      server - The server, as a numeric address or an unresolved name
      maxAge - How long a mapping counts after its last answer
      icePort - The port the monitor and the peers share
      factory - Opens the monitor
      nanoTime - The clock
  • Method Details

    • nativeMonitors

      public static StunObserver.MonitorFactory nativeMonitors(@Nullable InetAddress bind, int port)
      Monitors of their own on the ICE port, sharing the mux of peers bound to the same address and port.
    • monitor

      public static StunObserver.Monitor monitor(tel.schich.libdatachannel.StunUdpMuxMonitor monitor)
      Reads a native monitor, every address libjuice resolved the server to.
    • reading

      public static StunObserver.Reading reading(tel.schich.libdatachannel.StunBinding binding)
      Throws:
      IllegalStateException - If libjuice reported an address that is not numeric
    • withIcePort

      public static List<InetSocketAddress> withIcePort(Collection<InetSocketAddress> mappings, int icePort)
      Each mapping, and the same address on the ICE port when the mapping is on another, for a router that moved the port on the way out but forwards the ICE port itself.
    • start

      public void start()
      Opens the monitor now rather than on the first poll.
      Throws:
      RuntimeException - If it cannot be opened
    • poll

      public void poll()
      Reads the monitor, opening it first when it has none and is not waiting to retry.
    • report

      public void report(boolean covered)
      Logs what changed since the last call.
      Parameters:
      covered - Whether another server gives this host a public address, which turns a silent or lost server from a warning into a debug line
    • mappings

      public List<InetSocketAddress> mappings()
      The fresh mappings players elsewhere can reach, as of the last poll.
    • observed

      public List<StunObserver.Observed> observed()
      Every address of the server as of the last poll, including ones carried over a reopen.
    • epoch

      public long epoch()
      How many monitors were opened, so a reading can be told from one of an earlier monitor.
    • failed

      public boolean failed()
      Whether the monitor failed and has not been opened again yet.
    • replace

      public void replace(InetSocketAddress server)
      Withdraws what was found and asks another server from the next poll.
      Throws:
      IllegalStateException - If closed
      RuntimeException - If the old monitor fails to close again, keeping it and the old server
    • close

      public void close()
      Stops for good. Closing again retries a monitor that failed to close.
      Specified by:
      close in interface AutoCloseable
      Throws:
      RuntimeException - If the monitor fails to close