java.lang.Object
net.i2p.router.transport.udp.PacketHandler

class PacketHandlerextends Object
Pull inbound packets from the inbound receiver's queue, figure out what peer session they belong to (if any), authenticate and decrypt them with the appropriate keys, and push them to the appropriate handler. Data and ACK packets go to the InboundMessageFragments, the various establishment packets go to the EstablishmentManager, and, once implemented, relay packets will go to the relay manager. At the moment, this is an actual pool of packet handler threads, each pulling off the inbound receiver's queue and pushing them as necessary.

Supports dynamic thread count adjustment via adjustThreads().

  • Field Details

    • _context

      private final RouterContext _context
      The router context.
    • _log

      private final Log _log
      Logger for the packet handler.
    • _transport

      private final UDPTransport _transport
      The transport the packets belong to.
    • _establisher

      private final EstablishmentManager _establisher
      Handler of the peer setup packets.
    • _testManager

      private final PeerTestManager _testManager
      Handler of the peer test packets.
    • _keepReading

      private volatile boolean _keepReading
      True while the handler threads keep reading.
    • _handlers

      private final CopyOnWriteArrayList<PacketHandler.Handler> _handlers
      The running handler threads.
    • _activeHandlers

      private final AtomicInteger _activeHandlers
      Handler threads currently reading.
    • _processingCount

      private final AtomicInteger _processingCount
      Packets being processed right now.
    • _inboundQueue

      private final BlockingQueue<UDPPacket> _inboundQueue
      Packets waiting to be processed.
    • _networkID

      private final int _networkID
      Network id the packets must match.
    • TYPE_POISON

      private static final int TYPE_POISON
      Queue entry type that stops a handler.
      See Also:
    • MIN_QUEUE_SIZE

      private static final int MIN_QUEUE_SIZE
      Smallest packet queue allowed.
    • MAX_QUEUE_SIZE

      private static final int MAX_QUEUE_SIZE
      Largest packet queue allowed.
    • _maxHandlers

      private static volatile int _maxHandlers
      Most handler threads allowed.
    • _threadNum

      private static final AtomicInteger _threadNum
      Sequence number of the handler threads.
    • MIN_VERSION

      private static final int MIN_VERSION
      Oldest SSU version accepted.
      See Also:
    • MAX_VERSION

      private static final int MAX_VERSION
      Newest SSU version accepted.
      See Also:
  • Constructor Details

  • Method Details

    • getMaxHandlers

      public static int getMaxHandlers()
      Returns the current max packet handler threads.
      Returns:
      the max handlers
    • setMaxHandlers

      public static void setMaxHandlers(int handlers)
      The target max packet handler threads. Takes effect immediately via adjustThreads().
      Parameters:
      handlers - the new max handlers
    • getActiveHandlers

      public int getActiveHandlers()
      Returns the current number of active handler threads.
      Returns:
      the active handler count
    • getProcessingCount

      public int getProcessingCount()
      Returns the number of handlers actively processing packets (not parked on take()).
      Returns:
      the processing count
    • getUtilization

      public double getUtilization()
      Packet handler pool utilization as a ratio (0.0-1.0). Returns NaN if not started.
      Returns:
      the utilization ratio
    • startup

      public void startup()
      Start the packet handler.
    • shutdown

      public void shutdown()
      Shut down the packet handler.
    • adjustThreads

      void adjustThreads()
      Dynamically adjust handler thread count to match the target. Handlers are added or removed one at a time. Excess handlers receive a poison packet and exit cleanly.
    • getHandlerStatus

      String getHandlerStatus()
      The handler status string.
      Returns:
      a summary listing how many handlers are currently active, followed by one "handler i" entry per handler in the pool
    • queueReceived

      public void queueReceived(UDPPacket packet) throws InterruptedException
      Queue a received packet for processing.
      Parameters:
      packet - the received packet to hand to the inbound thread
      Throws:
      InterruptedException - if interrupted while waiting
    • stopQueue

      private void stopQueue()
      Blocking for a while.
    • receiveNext

      public UDPPacket receiveNext()
      Blocking call to retrieve the next inbound packet, or null if we have shut down.
      Returns:
      the next packet, or null if shut down
    • receiveSSU2Packet

      private boolean receiveSSU2Packet(RemoteHostId from, UDPPacket packet, InboundEstablishState2 state)
      Decrypt the header and hand off to the state for processing. Packet is trial-decrypted, so fallback processing is possible if this returns false.

      Possible messages here are Session Request, Token Request, Session Confirmed, or Peer Test. Data messages out-of-order from Session Confirmed, or following a Session Confirmed that was lost, or in-order but before the Session Confirmed was processed, will handed to the state to be queued for deferred handling.

      Min packet data size: 56 (token request) if state is null; 40 (data) if state is non-null.

      Parameters:
      from - the remote host the packet came from
      packet - the received packet
      state - must be version 2, but will be null for session request unless retransmitted
      Returns:
      true if the header was validated as a SSU2 packet, cannot fallback to SSU 1
    • receiveSSU2Packet

      private boolean receiveSSU2Packet(UDPPacket packet, OutboundEstablishState2 state)
      Decrypt the header and hand off to the state for processing. Packet is trial-decrypted, so fallback processing is possible if this returns false. But that's probably not necessary.

      Possible messages here are Session Created or Retry

      Min packet data size: 56 (retry).

      Parameters:
      packet - the received packet
      state - must be version 2, non-null
      Returns:
      true if the header was validated as a SSU2 packet, cannot fallback to SSU 1
    • countBadPacket

      private void countBadPacket(RemoteHostId from)
      Count a malformed/unrecognized SSU2 packet from a source that has no established or pending session (e.g. a BAD-Destination-ConnectionID flood). Feeds the optional Banlist.badPacket(String, String) offender tracker, which auto-blocklists an IP once it exceeds router.banlist.maxOffenses within the offense window.

      Only direct (IP+port) senders are tracked; a hash-only peer with no reachable address cannot be blocklisted by IP, so it is skipped. The policy is self-limiting (startup grace + maxOffenses threshold), so sporadic malformed packets from a legitimate peer are harmless.

      Parameters:
      from - the source of the bad packet, never null