java.lang.Object
net.i2p.router.client.ClientManager

class ClientManagerextends Object
Coordinate connections and various tasks.
  • Field Details

    • _log

      private final Log _log
      Logger for the client manager.
    • _listeners

      protected final List<ClientListenerRunner> _listeners
      Registered client listener runners.
    • _runners

      private final Map<Destination, ClientConnectionRunner> _runners
      The runners, by destination.
    • _runnersByHash

      private final Map<Hash, ClientConnectionRunner> _runnersByHash
      The runners, by destination hash.
    • _pendingRunners

      private final Set<ClientConnectionRunner> _pendingRunners
      Runners waiting for their session to start.
    • _runnerSessionIds

      private final Set<SessionId> _runnerSessionIds
      Session ids taken by the runners.
    • _metaDests

      private final Set<Destination> _metaDests
      Destinations of the metadata sessions.
    • _metaHashes

      private final Set<Hash> _metaHashes
      Destination hashes of the metadata sessions.
    • _ctx

      protected final RouterContext _ctx
      Router context.
    • _port

      protected final int _port
      I2CP listen port.
    • _isStarted

      protected volatile boolean _isStarted
      True after start() completes.
    • _wasStarted

      protected volatile boolean _wasStarted
      True if start() was ever called.
    • _clientTimestamper

      private final SimpleTimer2.TimedEvent _clientTimestamper
      Timer stamping the client sessions' clocks.
    • PROP_DISABLE_EXTERNAL

      private static final String PROP_DISABLE_EXTERNAL
      Disable external interface, allow internal clients only.
      See Also:
    • PROP_ENABLE_SSL

      private static final String PROP_ENABLE_SSL
      SSL interface (only).
      See Also:
    • PROP_DISABLE_LOOPBACK

      private static final String PROP_DISABLE_LOOPBACK
      Disable local-local loopback, force all traffic through tunnels.
      See Also:
    • REQUEST_LEASESET_TIMEOUT

      private static final long REQUEST_LEASESET_TIMEOUT
      Time to wait for a requested lease set.
      See Also:
    • MAX_SESSION_ID

      private static final int MAX_SESSION_ID
      Largest session id assignable.
      See Also:
    • PROP_MAX_SESSIONS

      private static final String PROP_MAX_SESSIONS
      2 bytes; 65535 is reserved for unknown.
      See Also:
    • DEFAULT_MAX_SESSIONS

      private static final int DEFAULT_MAX_SESSIONS
      Default session id pool size.
    • UNKNOWN_SESSION_ID

      public static final SessionId UNKNOWN_SESSION_ID
      Sentinel session ID for unknown/unidentified sessions.
  • Constructor Details

    • ClientManager

      public ClientManager(RouterContext context, int port)
      Does not start the listeners. Caller must call start().
      Parameters:
      context - the router context
      port - the I2CP port
  • Method Details

    • start

      public void start()
      Start the listeners.
    • startListeners

      protected void startListeners()
      Call from synchronized method Todo: Start a 3rd listener for IPV6?
    • restart

      public void restart()
      Restart the client manager, disconnecting and reconnecting all clients.
    • shutdown

      public void shutdown(String msg)
      Shutdown the client manager, disconnecting all clients.
      Parameters:
      msg - message to send to the clients
    • internalConnect

      public I2CPMessageQueue internalConnect() throws I2PSessionException
      The InternalClientManager interface. Connects to the router, receiving a message queue to talk to the router with.
      Returns:
      the message queue for communicating with the router
      Throws:
      I2PSessionException - if the router isn't ready
    • isAlive

      public boolean isAlive()
      Test whether the ClientManager is running.

      As of 0.9.45, this returns true iff the ClientManager is running. Prior to that, it also required all external I2CP listeners that were registered to be running. Since most of our connections are in-JVM, we now return true even if we have I2CP port conflicts.

      Returns:
      true if the ClientManager is running
    • registerConnection

      public void registerConnection(ClientConnectionRunner runner)
      Register a new client connection.
      Parameters:
      runner - the client connection to start and hold as pending until it claims a destination
    • unregisterConnection

      public void unregisterConnection(ClientConnectionRunner runner)
      Remove all sessions for this runner.
      Parameters:
      runner - the client connection whose session IDs and destination mappings are dropped
    • unregisterSession

      public void unregisterSession(SessionId id, Destination dest)
      Remove only the following session. Does not remove the runner if it has more.
      Parameters:
      id - the session ID
      dest - the destination whose registration this session holds
    • unregisterEncryptedDestination

      public void unregisterEncryptedDestination(ClientConnectionRunner runner, Hash hash)
      Remove the hash for the encrypted LS. Call before unregisterConnection, or when the hash changes.
      Parameters:
      runner - the connection the hash belongs to; not consulted by the body
      hash - the hash to remove
    • destinationEstablished

      public int destinationEstablished(ClientConnectionRunner runner, Destination dest)
      Add to the clients list. Check for a dup destination. Side effect: Sets the session ID of the runner. Caller must call runner.disconnectClient() on failure.
      Parameters:
      runner - the connection claiming the destination
      dest - the destination being claimed; must not already be registered
      Returns:
      SessionStatusMessage return code, 1 for success, != 1 for failure
    • registerEncryptedDestination

      public boolean registerEncryptedDestination(ClientConnectionRunner runner, Hash hash)
      Call after destinationEstablished(ClientConnectionRunner, Destination), when an encrypted leaseset is created, so we know it's local. Add to the clients list. Check for a dup hash. Caller must call runner.disconnectClient() on failure.
      Parameters:
      runner - the connection to associate with the hash
      hash - the location of the encrypted LS, will change every day
      Returns:
      success, false on dup
    • registerMetaDest

      public void registerMetaDest(Destination dest) throws I2PSessionException
      Declare that we're going to publish a meta LS for this destination. Must be called before publishing the leaseset.
      Parameters:
      dest - the destination the meta LeaseSet will be published under; must not already be registered
      Throws:
      I2PSessionException - on duplicate dest
    • unregisterMetaDest

      public void unregisterMetaDest(Destination dest)
      Declare that we're no longer going to publish a meta LS for this destination.
      Parameters:
      dest - the destination to stop publishing a meta LeaseSet for
    • locked_getNextSessionId

      private SessionId locked_getNextSessionId()
      Generate a new random, unused sessionId. Caller must synch on _runners.
      Returns:
      null on failure
    • distributeMessage

      void distributeMessage(ClientConnectionRunner sender, Destination fromDest, Destination toDest, Payload payload, MessageId msgId, long messageNonce, long expiration, int flags)
      Distribute message to a local or remote destination.
      Parameters:
      sender - non-null
      fromDest - the sending client's own destination, which must be registered for the remote path
      toDest - the destination being sent to; a local one is delivered inline
      payload - the message payload
      msgId - the router's ID for this message
      messageNonce - the client's ID for this message
      expiration - the expiry deadline carried into the queued message, only used on the remote path
      flags - not used for local delivery
    • requestLeaseSet

      public void requestLeaseSet(Destination dest, LeaseSet set, long timeout, Job onCreateJob, Job onFailedJob)
      Request that a particular client authorize the Leases contained in the LeaseSet, after which the onCreateJob is queued up. If that doesn't occur within the timeout specified, queue up the onFailedJob. This call does not block.

      UNUSED, the call below without jobs is always used.

      Parameters:
      dest - Destination from which the LeaseSet's authorization should be requested
      set - LeaseSet with requested leases - this object must be updated to contain the signed version (as well as any changed/added/removed Leases). The LeaseSet contains Leases only; it is unsigned and does not have the destination set.
      timeout - ms to wait before failing
      onCreateJob - Job to run after the LeaseSet is authorized
      onFailedJob - Job to run after the timeout passes without receiving authorization
    • requestLeaseSet

      public void requestLeaseSet(Hash dest, LeaseSet ls)
      Request that a particular client authorize the Leases contained in the LeaseSet.
      Parameters:
      dest - Destination from which the LeaseSet's authorization should be requested
      ls - LeaseSet with requested leases - this object must be updated to contain the signed version (as well as any changed/added/removed Leases). The LeaseSet contains Leases only; it is unsigned and does not have the destination set.
    • isLocal

      public boolean isLocal(Destination dest)
      Unsynchronized. DOES contain meta destinations.
      Parameters:
      dest - the destination to check
      Returns:
      true if local
    • isLocal

      public boolean isLocal(Hash destHash)
      Unsynchronized. DOES contain meta destinations.
      Parameters:
      destHash - the destination hash to check
      Returns:
      true if local
    • shouldPublishLeaseSet

      public boolean shouldPublishLeaseSet(Hash destHash)
      Determine if we should publish a leaseSet for this hash.
      Parameters:
      destHash - the destination hash
      Returns:
      true if we don't know about this destination at all
    • listClients

      public Set<Destination> listClients()
      Unsynchronized. Does NOT contain meta destinations.
      Returns:
      non-null set of local destinations
    • getRunner

      Looks up a client's runner without holding the lock.
      Parameters:
      dest - the destination key, as opposed to the hash overload
      Returns:
      the runner, or null
    • getClientSessionConfig

      public SessionConfig getClientSessionConfig(Destination dest)
      Return the client's current config, or null if not connected.
      Parameters:
      dest - the destination whose session config is wanted
      Returns:
      the SessionConfig, or null
    • getClientSessionKeyManager

      public SessionKeyManager getClientSessionKeyManager(Hash dest)
      Return the client's SessionKeyManager Use this instead of the I2PAppContext.sessionKeyManager() to prevent correlation attacks across destinations.
      Parameters:
      dest - the destination hash
      Returns:
      the SessionKeyManager, or null
    • getRunner

      private ClientConnectionRunner getRunner(Hash destHash)
      Looks up a client's runner by hash without holding the lock.
      Parameters:
      destHash - the destination hash to look up, null returns null
      Returns:
      the runner registered under that hash, or null
    • messageDeliveryStatusUpdate

      public void messageDeliveryStatusUpdate(Destination fromDest, MessageId id, long messageNonce, int status)
      Update the message delivery status for a client.
      Parameters:
      fromDest - the source destination
      id - the router's ID for this message
      messageNonce - the client's ID for this message, greater than zero
      status - see I2CP MessageStatusMessage for success/failure codes
    • getRunnerDestinations

      Set<Destination> getRunnerDestinations()
      Get the set of all runner destinations.
      Returns:
      unmodifiable, not a copy
    • reportAbuse

      public void reportAbuse(Destination dest, String reason, int severity)
      Reports abuse on one destination, or on every local destination.
      Parameters:
      dest - null for all local destinations
      reason - the abuse reason
      severity - the abuse severity
    • messageReceived

      public void messageReceived(ClientMessage msg)
      Receive and process an incoming client message inline.
      Parameters:
      msg - the message to process
    • getClientFloodfillNetworkDatabaseFacade

      public FloodfillNetworkDatabaseFacade getClientFloodfillNetworkDatabaseFacade(Hash destHash)
      The FloodfillNetworkDatabaseFacade associated with a particular client destination. This is inside the runner, so it won't be there if the runner isn't ready.
      Parameters:
      destHash - destination hash associated with the client who's subDb we're looking for, may be null
      Returns:
      will be null if desthash is null or client does not exist or its netDb is not initialized
    • getPrimaryHashes

      public Set<Hash> getPrimaryHashes()
      All the primary hashes for all the clients as a set.
      Returns:
      non-null