java.lang.Object
org.klomp.snark.ConnectionAcceptor
- All Implemented Interfaces:
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.
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate classPeriodically cleans up stale acceptors.private classHandles one incoming peer handshake.private classAccept loop for a destination, serving every torrent pooled on it, routed by the info hash in the handshake.Field Summary
FieldsModifier and TypeFieldDescriptionprivate final ObjectCounter<Hash> Bad handshakes counted per peer hash.Bad handshake reasons, by peer hash.private final SimpleTimer2.TimedEventTimer cleaning the bad peer records.private final LogLogger for the incoming peer connections.private final TorrentDestThe torrent destination being advertised.private final Map<TorrentDest, ConnectionAcceptor.TorrentAcceptLoop> Accept loops per destination, shared by the torrents pooled on it.private final I2PSnarkUtilThe plugin's shared utilities.private final AtomicIntegerHandshake handlers currently running.private static final longInterval between bad peer record cleanups.private static final intProtocol errors within a window before the client is rejected.private static final intMaximum concurrent incoming connections in handshake.private final PeerAcceptorThe acceptor for the peer handshakes.private booleanStops the accept loop.private ThreadThe thread running the accept loop.Constructor Summary
ConstructorsConstructorDescriptionConnectionAcceptor(I2PSnarkUtil util, PeerAcceptor peeracceptor) Unused (single torrent).ConnectionAcceptor(I2PSnarkUtil util, PeerAcceptor peeracceptor, TorrentDest td) Single torrent on its own destination.ConnectionAcceptor(I2PSnarkUtil util, PeerCoordinatorSet set) Multitorrent.Method Summary
Modifier and TypeMethodDescriptionvoidaddTorrentAcceptor(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.voidBan the given hash.private DestinationThe destination of this acceptor.intgetPort()Returns the port this acceptor is listening on.private I2PServerSocketThe server socket for this acceptor's destination.voidhalt()May be restarted later with startAccepting().private voidhandleSocket(I2PSocket socket, Destination myDest, PeerAcceptor pa) Validate an accepted socket and dispatch it to a handler thread.booleanIs the given hash banned?private voidCaller must synch.voidremoveTorrentAcceptor(TorrentDest td, PeerCoordinator coordinator) Remove a torrent from its destination's accept loop, halting the loop when it no longer serves any torrent.voidrestart()Effectively unused, would only be called if we changed I2CP host/port, which is hidden in the gui if in router context.voidrun()private voidrun2()Runs the accept loop until stopped.voidMay be called even when already running.
Field Details
_log
Logger for the incoming peer connections.peeracceptor
The acceptor for the peer handshakes.thread
The thread running the accept loop._util
The plugin's shared utilities._td
The torrent destination being advertised._badCounter
Bad handshakes counted per peer hash._badReasons
_cleaner
Timer cleaning the bad peer records._torrentAcceptors
Accept loops per destination, shared by the torrents pooled on it.stop
private volatile boolean stopStops the accept loop.MAX_BAD
private static final int MAX_BADProtocol errors within a window before the client is rejected.- See Also:
BAD_CLEAN_INTERVAL
private static final long BAD_CLEAN_INTERVALInterval between bad peer record cleanups.- See Also:
MAX_HANDLERS
private static final int MAX_HANDLERSMaximum concurrent incoming connections in handshake.- See Also:
activeHandlers
Handshake handlers currently running.
Constructor Details
ConnectionAcceptor
Multitorrent. Caller MUST callstartAccepting().- Parameters:
util- the I2PSnark utility instance supplying the config and storage this acceptor works throughset- the peer coordinator set covering every torrent, handed to the multitorrent PeerAcceptor this constructor creates
ConnectionAcceptor
Unused (single torrent). Do NOT call startAccepting().- Parameters:
util- the I2PSnark utility instance supplying the config and storage this acceptor works throughpeeracceptor- the single-torrent acceptor that does the peer handshaking, already created by the caller
ConnectionAcceptor
Single torrent on its own destination. Do NOT call startAccepting().- Parameters:
util- the I2PSnark utility instance supplying the config and storage this acceptor works throughpeeracceptor- the single-torrent acceptor that does the peer handshaking, already created by the callertd- 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 afterhalt().halt
public void halt()May be restarted later with startAccepting().addTorrentAcceptor
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 destinationcoordinator- the torrent's peer coordinator
removeTorrentAcceptor
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 onecoordinator- the torrent's peer coordinator
locked_halt
private void locked_halt()Caller must synch.getServerSocket
The server socket for this acceptor's destination.- Returns:
- the socket, null if there is none
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
run2
private void run2()Runs the accept loop until stopped.handleSocket
Validate an accepted socket and dispatch it to a handler thread.- Parameters:
socket- the accepted socketmyDest- the destination of this acceptor, to drop connections from ourselvespa- the peer acceptor to route the connection to
isBanned
Is the given hash banned?- Parameters:
h- destination hash being tested- Returns:
- true if banned
ban