Class 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.
    • Constructor Detail

      • NTCPConnection

        public NTCPConnection​(RouterContext ctx,
                              NTCPTransport transport,
                              SocketChannel chan,
                              SelectionKey key)
        Create an inbound connected (though not established) NTCP connection. Caller MUST call transport.establishing(this) after construction. Caller MUST key.attach(this) after construction.
    • Method Detail

      • 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
      • isInbound

        public boolean isInbound()
      • isEstablished

        public boolean isEstablished()
      • isIPv6

        public boolean isIPv6()
        Since:
        IPv6
      • 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
      • 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​(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
      • 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)
      • 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().
      • write

        void write​(ByteBuffer buf)
        The contents of the buffer have been encrypted / padded / etc and have been fully allocated for the bandwidth limiter.
      • getNextReadBuf

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

        boolean isWriteBufEmpty()
        Replaces getWriteBufCount()
        Since:
        0.8.12
      • getNextWriteBuf

        ByteBuffer getNextWriteBuf()
        Returns:
        null if none available
      • removeWriteBuf

        void removeWriteBuf​(ByteBuffer buf)
        Remove the buffer, which _should_ be the one at the head of _writeBufs
      • 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:
        clockSkew - OUR clock minus BOB's clock in seconds (may be negative, obviously, but |val| should be under 1 minute)
        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
        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:
        clockSkew - OUR clock minus ALICE's clock in seconds (may be negative, obviously, but |val| should be under 1 minute)
        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
        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