Class NTCPConnection

java.lang.Object
net.i2p.router.transport.ntcp.NTCPConnection
All Implemented Interfaces:
Closeable, AutoCloseable

public class NTCPConnection extends Object implements Closeable
Coordinate the connection to a single peer. NTCP 1 or 2. Public only for UI peers page. Not a public API, not for external use.
  • Field Details

  • Constructor Details

  • Method Details

    • getChannel

      public SocketChannel getChannel()
      Valid for inbound; valid for outbound shortly after creation
    • getKey

      public SelectionKey getKey()
      Valid for inbound; valid for outbound shortly after creation
    • setChannel

      public void setChannel(SocketChannel chan)
    • setKey

      public void setKey(SelectionKey key)
    • isInbound

      public boolean isInbound()
    • isEstablished

      public boolean isEstablished()
    • isIPv6

      public boolean isIPv6()
      Since:
      IPv6
    • getRemoteIP

      public byte[] getRemoteIP()
      Returns:
      null if unknown
      Since:
      0.9.53
    • getEstablishState

      EstablishState getEstablishState()
      Only valid during establishment; replaced with EstablishState.VERIFIED or FAILED afterward
    • getRemoteAddress

      public RouterAddress getRemoteAddress()
      Only valid for outbound; null for inbound
    • getRemotePeer

      public RouterIdentity getRemotePeer()
      Valid for outbound; valid for inbound after handshake
    • setRemotePeer

      public void setRemotePeer(RouterIdentity ident)
      Valid for outbound; valid for inbound after handshake
    • getClockSkew

      public long getClockSkew()
      A positive number means our clock is ahead of theirs.
      Returns:
      seconds
    • getUptime

      public long getUptime()
      Returns:
      milliseconds
    • getEstablishedOn

      public long getEstablishedOn()
      Since:
      0.9.55
    • getMessagesSent

      public int getMessagesSent()
    • getMessagesReceived

      public int getMessagesReceived()
    • getOutboundQueueSize

      public int getOutboundQueueSize()
    • getTimeSinceSend

      public long getTimeSinceSend()
      Returns:
      milliseconds
    • getTimeSinceSend

      public long getTimeSinceSend(long now)
      Returns:
      milliseconds
      Since:
      0.9.38
    • getTimeSinceReceive

      public long getTimeSinceReceive()
      Returns:
      milliseconds
    • getTimeSinceReceive

      public long getTimeSinceReceive(long now)
      Returns:
      milliseconds
      Since:
      0.9.38
    • getTimeSinceCreated

      public long getTimeSinceCreated()
      Returns:
      milliseconds
    • getTimeSinceCreated

      public long getTimeSinceCreated(long now)
      Returns:
      milliseconds
      Since:
      0.9.38
    • getCreated

      public long getCreated()
      Returns:
      when this connection was created (not established)
      Since:
      0.9.20
    • getVersion

      public int getVersion()
      The NTCP2 version, for the console. For outbound, will not change. For inbound, defaults to 1, may change to 2 after establishment.
      Returns:
      the version, 1 or 2
      Since:
      0.9.36
    • setVersion

      public void setVersion(int ver)
      Set version 2 from InboundEstablishState. Just for logging, so we know before finishInboundEstablish() is called.
      Since:
      0.9.36
    • setMayDisconnect

      public void setMayDisconnect()
      Sets to true.
      Since:
      0.9.24
    • getMayDisconnect

      public boolean getMayDisconnect()
      Since:
      0.9.24
    • clearZeroRead

      void clearZeroRead()
      workaround for EventPumper
      Since:
      0.8.12
    • gotZeroRead

      int gotZeroRead()
      workaround for EventPumper
      Returns:
      value after incrementing
      Since:
      0.8.12
    • isClosed

      public boolean isClosed()
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
    • close

      public void close(boolean allowRequeue)
    • closeOnTimeout

      void closeOnTimeout(String cause, Exception e)
      Close and release EstablishState resources.
      Parameters:
      e - may be null
      Since:
      0.9.16
    • send

      public void send(OutNetMessage msg)
      toss the message onto the connection's send queue
    • isBacklogged

      public boolean isBacklogged()
    • tooBacklogged

      public boolean tooBacklogged()
    • enqueueInfoMessage

      void enqueueInfoMessage()
      Inject a DatabaseStoreMessage with our RouterInfo. NTCP 1 or 2. Externally, this is only called by NTCPTransport for outbound cons, before the con is established, but we know what version it is. Internally, may be called for outbound or inbound, but only after the con is established, so we know what the version is.
    • prepareNextWrite

      void prepareNextWrite(NTCPConnection.PrepBuffer prep)
      Prepare the next I2NP message for transmission. This should be run from the Writer thread pool. NTCP 1 or 2. This is the entry point as called from Writer.Runner.run()
      Parameters:
      prep - an instance of PrepBuffer to use as scratch space
    • sendOurRouterInfo

      void sendOurRouterInfo(boolean shouldFlood)
      NTCP2 only
      Since:
      0.9.36, pkg private since 0.9.55 for EventPumper
    • sendTerminationAndClose

      void sendTerminationAndClose()
      NTCP 1 or 2. For NTCP1, simply closes the connection immediately. For NTCP2, sends termination and then closes the connection after a brief delay.
      Since:
      0.9.36
    • outboundConnected

      void outboundConnected()
      async callback after the outbound connection was completed (this should NOT block, as it occurs in the selector thread)
    • wantsWrite

      void wantsWrite(byte[] data)
      Call when there is data ready to write. If we have bandwidth, calls write() which calls EventPumnper.wantsWrite(con). If no bandwidth, calls queuedWrite().
      Since:
      moved from EventPumper in 0.9.52
    • wantsWrite

      void wantsWrite(byte[] data, int off, int len)
      Call when there is data ready to write. If we have bandwidth, calls write() which calls EventPumnper.wantsWrite(con). If no bandwidth, calls queuedWrite().
      Since:
      0.9.35 off/len version, moved from EventPumper in 0.9.52
    • queuedRecv

      void queuedRecv(ByteBuffer buf, FIFOBandwidthLimiter.Request req)
      We have read the data in the buffer, but we can't process it locally yet, because we're choked by the bandwidth limiter. Cache the contents of the buffer (not copy) and register ourselves to be notified when the contents have been fully allocated
    • recv

      void recv(ByteBuffer buf)
      The contents of the buffer have been read and can be processed asap. This should not block, and the NTCP connection now owns the buffer to do with as it pleases BUT it should eventually copy out the data and call EventPumper.releaseBuf().
    • getWriteLock

      Object getWriteLock()
      Write lock for pumper delayed writes
      Since:
      0.9.53
    • getNextReadBuf

      ByteBuffer getNextReadBuf()
      Returns:
      null if none available
    • isWriteBufEmpty

      boolean isWriteBufEmpty()
      Replaces getWriteBufCount(). Caller should sync on getWriteLock()
      Since:
      0.8.12
    • getNextWriteBuf

      ByteBuffer getNextWriteBuf()
      Returns but does not remove the buffer. Call removeWriteBuf() after write complete. Caller should sync on getWriteLock()
      Returns:
      null if none available
    • removeWriteBuf

      void removeWriteBuf(ByteBuffer buf)
      Remove the buffer, which _should_ be the one at the head of _writeBufs. Caller must sync on _writeLock
    • getSendRate

      public float getSendRate()
    • getRecvRate

      public float getRecvRate()
    • recvEncryptedI2NP

      void recvEncryptedI2NP(ByteBuffer buf)
      Connection must be established! The contents of the buffer include some fraction of one or more encrypted and encoded I2NP messages. individual i2np messages are encoded as "sizeof(data)+data+pad+crc", and those are encrypted with the session key and the last 16 bytes of the previous encrypted i2np message. The NTCP connection now owns the buffer BUT it must copy out the data as reader will call EventPumper.releaseBuf(). This is the entry point as called from Reader.processRead()
    • releaseResources

      static void releaseResources()
      Call at transport shutdown
      Since:
      0.8.8
    • finishOutboundEstablishment

      void finishOutboundEstablishment(CipherState sender, CipherState receiver, byte[] sip_ab, byte[] sip_ba, long clockSkew)
      We are Alice. NTCP2 only. Caller MUST call recvEncryptedI2NP() after, for any remaining bytes in receive buffer
      Parameters:
      sender - use to send to Bob
      receiver - use to receive from Bob
      sip_ab - 24 bytes to init SipHash to Bob
      sip_ba - 24 bytes to init SipHash from Bob
      clockSkew - OUR clock minus BOB's clock in seconds (may be negative, obviously, but |val| should be under 1 minute)
      Since:
      0.9.36
    • finishInboundEstablishment

      void finishInboundEstablishment(CipherState sender, CipherState receiver, byte[] sip_ba, byte[] sip_ab, long clockSkew, NTCP2Options hisPadding)
      We are Bob. NTCP2 only. Caller MUST call recvEncryptedI2NP() after, for any remaining bytes in receive buffer
      Parameters:
      sender - use to send to Alice
      receiver - use to receive from Alice
      sip_ba - 24 bytes to init SipHash to Alice
      sip_ab - 24 bytes to init SipHash from Alice
      clockSkew - OUR clock minus ALICE's clock in seconds (may be negative, obviously, but |val| should be under 1 minute)
      hisPadding - may be null
      Since:
      0.9.36
    • failInboundEstablishment

      void failInboundEstablishment(CipherState sender, byte[] sip_ba, int reason)
      We are Bob. NTCP2 only. This is only for invalid payload received in message 3. We send a termination and close. There will be no receiving.
      Parameters:
      sender - use to send to Alice
      sip_ba - 24 bytes to init SipHash to Alice
      Since:
      0.9.36
    • toString

      public String toString()
      Overrides:
      toString in class Object