java.lang.Object
org.klomp.snark.ConnectionAcceptor
All Implemented Interfaces:
Runnable

class ConnectionAcceptorextends Object implements Runnable
Accepts incoming I2P connections and routes them to the appropriate PeerAcceptor.

This class manages the main I2P server socket that listens for incoming peer connections and routes them to the correct PeerAcceptor based on the torrent's info hash. It handles:

  • Listening on the I2P server socket for incoming connections
  • Basic connection validation and handshake parsing
  • Routing connections to the appropriate PeerAcceptor
  • Connection rate limiting and blacklisting of misbehaving peers
  • Thread management for accepting connections
  • Cleanup of stale connection data

The acceptor supports multi-torrent operation by working with a PeerCoordinatorSet to determine which torrent a connection belongs to based on the info hash in the BitTorrent handshake.

When multi-dest is enabled, each torrent additionally runs a TorrentAcceptLoop on its own server socket, so that incoming connections are received on the torrent's own destination.

Connections that repeatedly fail protocol validation may be temporarily blacklisted to prevent abuse.

  • Field Details

    • _log

      private final Log _log
      Logger for the incoming peer connections.
    • peeracceptor

      private final PeerAcceptor peeracceptor
      The acceptor for the peer handshakes.
    • thread

      private Thread thread
      The thread running the accept loop.
    • _util

      private final I2PSnarkUtil _util
      The plugin's shared utilities.
    • _td

      private final TorrentDest _td
      The torrent destination being advertised.
    • _badCounter

      private final ObjectCounter<Hash> _badCounter
      Bad handshakes counted per peer hash.
    • _badReasons

      private final Map<Hash,String> _badReasons
      Bad handshake reasons, by peer hash.
    • _cleaner

      private final SimpleTimer2.TimedEvent _cleaner
      Timer cleaning the bad peer records.
    • _torrentAcceptors

      private final Map<TorrentDest, ConnectionAcceptor.TorrentAcceptLoop> _torrentAcceptors
      Accept loops per destination, shared by the torrents pooled on it.
    • stop

      private volatile boolean stop
      Stops the accept loop.
    • MAX_BAD

      private static final int MAX_BAD
      Protocol errors within a window before the client is rejected.
      See Also:
    • BAD_CLEAN_INTERVAL

      private static final long BAD_CLEAN_INTERVAL
      Interval between bad peer record cleanups.
      See Also:
    • MAX_HANDLERS

      private static final int MAX_HANDLERS
      Maximum concurrent incoming connections in handshake.
      See Also:
    • activeHandlers

      private final AtomicInteger activeHandlers
      Handshake handlers currently running.
  • Constructor Details

    • ConnectionAcceptor

      public ConnectionAcceptor(I2PSnarkUtil util, PeerCoordinatorSet set)
      Multitorrent. Caller MUST call startAccepting().
      Parameters:
      util - the I2PSnark utility instance supplying the config and storage this acceptor works through
      set - the peer coordinator set covering every torrent, handed to the multitorrent PeerAcceptor this constructor creates
    • ConnectionAcceptor

      public ConnectionAcceptor(I2PSnarkUtil util, PeerAcceptor peeracceptor)
      Unused (single torrent). Do NOT call startAccepting().
      Parameters:
      util - the I2PSnark utility instance supplying the config and storage this acceptor works through
      peeracceptor - the single-torrent acceptor that does the peer handshaking, already created by the caller
    • ConnectionAcceptor

      public ConnectionAcceptor(I2PSnarkUtil util, PeerAcceptor peeracceptor, TorrentDest td)
      Single torrent on its own destination. Do NOT call startAccepting().
      Parameters:
      util - the I2PSnark utility instance supplying the config and storage this acceptor works through
      peeracceptor - the single-torrent acceptor that does the peer handshaking, already created by the caller
      td - the destination this one torrent is published under, used to tell whether an incoming connection is meant for it
  • Method Details

    • startAccepting

      public void startAccepting()
      May be called even when already running. May be called to start up again after halt().
    • halt

      public void halt()
      May be restarted later with startAccepting().
    • addTorrentAcceptor

      public void addTorrentAcceptor(TorrentDest td, PeerCoordinator coordinator)
      Start an accept loop on a torrent's destination, adding the torrent to the destination's loop when one already runs for a pooled destination. Multitorrent only.
      Parameters:
      td - the torrent's destination
      coordinator - the torrent's peer coordinator
    • removeTorrentAcceptor

      public void removeTorrentAcceptor(TorrentDest td, PeerCoordinator coordinator)
      Remove a torrent from its destination's accept loop, halting the loop when it no longer serves any torrent. Multitorrent only.
      Parameters:
      td - the torrent's destination, or null if it never got one
      coordinator - the torrent's peer coordinator
    • locked_halt

      private void locked_halt()
      Caller must synch.
    • getServerSocket

      private I2PServerSocket getServerSocket()
      The server socket for this acceptor's destination.
      Returns:
      the socket, null if there is none
    • getMyDestination

      private Destination getMyDestination()
      The destination of this acceptor.
      Returns:
      the destination, null if there is none
    • restart

      public void restart()
      Effectively unused, would only be called if we changed I2CP host/port, which is hidden in the gui if in router context.
    • getPort

      public int getPort()
      Returns the port this acceptor is listening on.
      Returns:
      the port number
    • run

      public void run()
      Specified by:
      run in interface Runnable
    • run2

      private void run2()
      Runs the accept loop until stopped.
    • handleSocket

      private void handleSocket(I2PSocket socket, Destination myDest, PeerAcceptor pa)
      Validate an accepted socket and dispatch it to a handler thread.
      Parameters:
      socket - the accepted socket
      myDest - the destination of this acceptor, to drop connections from ourselves
      pa - the peer acceptor to route the connection to
    • isBanned

      public boolean isBanned(Hash h)
      Is the given hash banned?
      Parameters:
      h - destination hash being tested
      Returns:
      true if banned
    • ban

      public void ban(Hash h)
      Ban the given hash.
      Parameters:
      h - destination hash to count as bad