Package net.i2p.router.transport.ntcp
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.
- 
- 
Nested Class SummaryNested Classes Modifier and Type Class Description (package private) static classNTCPConnection.PrepBuffer
 - 
Field SummaryFields Modifier and Type Field Description (package private) static intBUFFER_SIZEWhy this is 16K, and where it is documented, good question? We claim we can do 32K datagrams so this is a problem.(package private) static intDELAY_DEFAULT(package private) static intDUMMY_DEFAULT(package private) static intNTCP2_MAX_MSG_SIZESee spec.(package private) static intPADDING_MAX_DEFAULT_INT(package private) static intPADDING_MIN_DEFAULT_INT(package private) static intREASON_AEAD(package private) static intREASON_BANNED(package private) static intREASON_FRAME_TIMEOUT(package private) static intREASON_FRAMING(package private) static intREASON_MSG1(package private) static intREASON_MSG2(package private) static intREASON_MSG3(package private) static intREASON_OPTIONS(package private) static intREASON_PADDING(package private) static intREASON_PAYLOAD(package private) static intREASON_S_MISMATCH(package private) static intREASON_SIGFAIL(package private) static intREASON_SIGTYPE(package private) static intREASON_SKEW(package private) static intREASON_TERMINATION(package private) static intREASON_TIMEOUT(package private) static intREASON_UNSPEC
 - 
Constructor SummaryConstructors Constructor Description NTCPConnection(RouterContext ctx, NTCPTransport transport, SocketChannel chan, SelectionKey key)Create an inbound connected (though not established) NTCP connection.NTCPConnection(RouterContext ctx, NTCPTransport transport, RouterIdentity remotePeer, RouterAddress remAddr, int version)Create an outbound unconnected NTCP connection.
 - 
Method SummaryAll Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) voidclearZeroRead()workaround for EventPumpervoidclose()voidclose(boolean allowRequeue)(package private) voidcloseOnTimeout(String cause, Exception e)Close and release EstablishState resources.(package private) voidenqueueInfoMessage()Inject a DatabaseStoreMessage with our RouterInfo.(package private) voidfailInboundEstablishment(CipherState sender, byte[] sip_ba, int reason)We are Bob.(package private) voidfinishInboundEstablishment(CipherState sender, CipherState receiver, byte[] sip_ba, byte[] sip_ab, long clockSkew, NTCP2Options hisPadding)We are Bob.(package private) voidfinishOutboundEstablishment(CipherState sender, CipherState receiver, byte[] sip_ab, byte[] sip_ba, long clockSkew)We are Alice.SocketChannelgetChannel()Valid for inbound; valid for outbound shortly after creationlonggetClockSkew()A positive number means our clock is ahead of theirs.longgetCreated()(package private) EstablishStategetEstablishState()Only valid during establishment; replaced with EstablishState.VERIFIED or FAILED afterwardSelectionKeygetKey()Valid for inbound; valid for outbound shortly after creationbooleangetMayDisconnect()intgetMessagesReceived()intgetMessagesSent()(package private) ByteBuffergetNextReadBuf()(package private) ByteBuffergetNextWriteBuf()intgetOutboundQueueSize()floatgetRecvRate()RouterAddressgetRemoteAddress()Only valid for outbound; null for inboundRouterIdentitygetRemotePeer()Valid for outbound; valid for inbound after handshakefloatgetSendRate()longgetTimeSinceCreated()longgetTimeSinceCreated(long now)longgetTimeSinceReceive()longgetTimeSinceReceive(long now)longgetTimeSinceSend()longgetTimeSinceSend(long now)longgetUptime()intgetVersion()The NTCP2 version, for the console.(package private) intgotZeroRead()workaround for EventPumperbooleanisBacklogged()booleanisClosed()booleanisEstablished()booleanisInbound()booleanisIPv6()(package private) booleanisWriteBufEmpty()Replaces getWriteBufCount()(package private) voidoutboundConnected()async callback after the outbound connection was completed (this should NOT block, as it occurs in the selector thread)(package private) voidprepareNextWrite(NTCPConnection.PrepBuffer prep)Prepare the next I2NP message for transmission.(package private) voidqueuedRecv(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.(package private) voidqueuedWrite(ByteBuffer buf, FIFOBandwidthLimiter.Request req)ditto for writes(package private) voidrecv(ByteBuffer buf)The contents of the buffer have been read and can be processed asap.(package private) voidrecvEncryptedI2NP(ByteBuffer buf)Connection must be established! The contents of the buffer include some fraction of one or more encrypted and encoded I2NP messages.(package private) static voidreleaseResources()Call at transport shutdown(package private) voidremoveWriteBuf(ByteBuffer buf)Remove the buffer, which _should_ be the one at the head of _writeBufsvoidsend(OutNetMessage msg)toss the message onto the connection's send queue(package private) voidsendTerminationAndClose()NTCP 1 or 2.voidsetChannel(SocketChannel chan)voidsetKey(SelectionKey key)voidsetMayDisconnect()Sets to true.voidsetRemotePeer(RouterIdentity ident)Valid for outbound; valid for inbound after handshakevoidsetVersion(int ver)Set version 2 from InboundEstablishState.booleantooBacklogged()StringtoString()(package private) voidwrite(ByteBuffer buf)The contents of the buffer have been encrypted / padded / etc and have been fully allocated for the bandwidth limiter.
 
- 
- 
- 
Field Detail- 
BUFFER_SIZEstatic final int BUFFER_SIZE Why this is 16K, and where it is documented, good question? We claim we can do 32K datagrams so this is a problem. Needs to be fixed. But SSU can handle it? In the meantime, don't let the transport bid on big messages.- See Also:
- Constant Field Values
 
 - 
NTCP2_MAX_MSG_SIZEstatic final int NTCP2_MAX_MSG_SIZE See spec. Max Noise payload 65535, minus 16 byte MAC and 3 byte block header. Includes 9-byte I2NP header.- See Also:
- Constant Field Values
 
 - 
REASON_UNSPECstatic final int REASON_UNSPEC - See Also:
- Constant Field Values
 
 - 
REASON_TERMINATIONstatic final int REASON_TERMINATION - See Also:
- Constant Field Values
 
 - 
REASON_TIMEOUTstatic final int REASON_TIMEOUT - See Also:
- Constant Field Values
 
 - 
REASON_AEADstatic final int REASON_AEAD - See Also:
- Constant Field Values
 
 - 
REASON_OPTIONSstatic final int REASON_OPTIONS - See Also:
- Constant Field Values
 
 - 
REASON_SIGTYPEstatic final int REASON_SIGTYPE - See Also:
- Constant Field Values
 
 - 
REASON_SKEWstatic final int REASON_SKEW - See Also:
- Constant Field Values
 
 - 
REASON_PADDINGstatic final int REASON_PADDING - See Also:
- Constant Field Values
 
 - 
REASON_FRAMINGstatic final int REASON_FRAMING - See Also:
- Constant Field Values
 
 - 
REASON_PAYLOADstatic final int REASON_PAYLOAD - See Also:
- Constant Field Values
 
 - 
REASON_MSG1static final int REASON_MSG1 - See Also:
- Constant Field Values
 
 - 
REASON_MSG2static final int REASON_MSG2 - See Also:
- Constant Field Values
 
 - 
REASON_MSG3static final int REASON_MSG3 - See Also:
- Constant Field Values
 
 - 
REASON_FRAME_TIMEOUTstatic final int REASON_FRAME_TIMEOUT - See Also:
- Constant Field Values
 
 - 
REASON_SIGFAILstatic final int REASON_SIGFAIL - See Also:
- Constant Field Values
 
 - 
REASON_S_MISMATCHstatic final int REASON_S_MISMATCH - See Also:
- Constant Field Values
 
 - 
REASON_BANNEDstatic final int REASON_BANNED - See Also:
- Constant Field Values
 
 - 
PADDING_MIN_DEFAULT_INTstatic final int PADDING_MIN_DEFAULT_INT - See Also:
- Constant Field Values
 
 - 
PADDING_MAX_DEFAULT_INTstatic final int PADDING_MAX_DEFAULT_INT - See Also:
- Constant Field Values
 
 - 
DUMMY_DEFAULTstatic final int DUMMY_DEFAULT - See Also:
- Constant Field Values
 
 - 
DELAY_DEFAULTstatic final int DELAY_DEFAULT - See Also:
- Constant Field Values
 
 
- 
 - 
Constructor Detail- 
NTCPConnectionpublic 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.
 - 
NTCPConnectionpublic NTCPConnection(RouterContext ctx, NTCPTransport transport, RouterIdentity remotePeer, RouterAddress remAddr, int version) throws DataFormatException Create an outbound unconnected NTCP connection. Caller MUST call transport.establishing(this) after construction.- Parameters:
- version- must be 1 or 2
- Throws:
- DataFormatException- if there's a problem with the address
 
 
- 
 - 
Method Detail- 
getChannelpublic SocketChannel getChannel() Valid for inbound; valid for outbound shortly after creation
 - 
getKeypublic SelectionKey getKey() Valid for inbound; valid for outbound shortly after creation
 - 
setChannelpublic void setChannel(SocketChannel chan) 
 - 
setKeypublic void setKey(SelectionKey key) 
 - 
isInboundpublic boolean isInbound() 
 - 
isEstablishedpublic boolean isEstablished() 
 - 
isIPv6public boolean isIPv6() - Since:
- IPv6
 
 - 
getEstablishStateEstablishState getEstablishState() Only valid during establishment; replaced with EstablishState.VERIFIED or FAILED afterward
 - 
getRemoteAddresspublic RouterAddress getRemoteAddress() Only valid for outbound; null for inbound
 - 
getRemotePeerpublic RouterIdentity getRemotePeer() Valid for outbound; valid for inbound after handshake
 - 
setRemotePeerpublic void setRemotePeer(RouterIdentity ident) Valid for outbound; valid for inbound after handshake
 - 
getClockSkewpublic long getClockSkew() A positive number means our clock is ahead of theirs.- Returns:
- seconds
 
 - 
getUptimepublic long getUptime() - Returns:
- milliseconds
 
 - 
getMessagesSentpublic int getMessagesSent() 
 - 
getMessagesReceivedpublic int getMessagesReceived() 
 - 
getOutboundQueueSizepublic int getOutboundQueueSize() 
 - 
getTimeSinceSendpublic long getTimeSinceSend() - Returns:
- milliseconds
 
 - 
getTimeSinceSendpublic long getTimeSinceSend(long now) - Returns:
- milliseconds
- Since:
- 0.9.38
 
 - 
getTimeSinceReceivepublic long getTimeSinceReceive() - Returns:
- milliseconds
 
 - 
getTimeSinceReceivepublic long getTimeSinceReceive(long now) - Returns:
- milliseconds
- Since:
- 0.9.38
 
 - 
getTimeSinceCreatedpublic long getTimeSinceCreated() - Returns:
- milliseconds
 
 - 
getTimeSinceCreatedpublic long getTimeSinceCreated(long now) - Returns:
- milliseconds
- Since:
- 0.9.38
 
 - 
getCreatedpublic long getCreated() - Returns:
- when this connection was created (not established)
- Since:
- 0.9.20
 
 - 
getVersionpublic 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
 
 - 
setVersionpublic void setVersion(int ver) Set version 2 from InboundEstablishState. Just for logging, so we know before finishInboundEstablish() is called.- Since:
- 0.9.36
 
 - 
setMayDisconnectpublic void setMayDisconnect() Sets to true.- Since:
- 0.9.24
 
 - 
getMayDisconnectpublic boolean getMayDisconnect() - Since:
- 0.9.24
 
 - 
clearZeroReadvoid clearZeroRead() workaround for EventPumper- Since:
- 0.8.12
 
 - 
gotZeroReadint gotZeroRead() workaround for EventPumper- Returns:
- value after incrementing
- Since:
- 0.8.12
 
 - 
isClosedpublic boolean isClosed() 
 - 
closepublic void close() - Specified by:
- closein interface- AutoCloseable
- Specified by:
- closein interface- Closeable
 
 - 
closepublic void close(boolean allowRequeue) 
 - 
closeOnTimeoutvoid closeOnTimeout(String cause, Exception e) Close and release EstablishState resources.- Parameters:
- e- may be null
- Since:
- 0.9.16
 
 - 
sendpublic void send(OutNetMessage msg) toss the message onto the connection's send queue
 - 
isBackloggedpublic boolean isBacklogged() 
 - 
tooBackloggedpublic boolean tooBacklogged() 
 - 
enqueueInfoMessagevoid 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.
 - 
prepareNextWritevoid 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
 
 - 
sendTerminationAndClosevoid 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
 
 - 
outboundConnectedvoid outboundConnected() async callback after the outbound connection was completed (this should NOT block, as it occurs in the selector thread)
 - 
queuedRecvvoid 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
 - 
queuedWritevoid queuedWrite(ByteBuffer buf, FIFOBandwidthLimiter.Request req) ditto for writes
 - 
recvvoid 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().
 - 
writevoid write(ByteBuffer buf) The contents of the buffer have been encrypted / padded / etc and have been fully allocated for the bandwidth limiter.
 - 
getNextReadBufByteBuffer getNextReadBuf() - Returns:
- null if none available
 
 - 
isWriteBufEmptyboolean isWriteBufEmpty() Replaces getWriteBufCount()- Since:
- 0.8.12
 
 - 
getNextWriteBufByteBuffer getNextWriteBuf() - Returns:
- null if none available
 
 - 
removeWriteBufvoid removeWriteBuf(ByteBuffer buf) Remove the buffer, which _should_ be the one at the head of _writeBufs
 - 
getSendRatepublic float getSendRate() 
 - 
getRecvRatepublic float getRecvRate() 
 - 
recvEncryptedI2NPvoid 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()
 - 
releaseResourcesstatic void releaseResources() Call at transport shutdown- Since:
- 0.8.8
 
 - 
finishOutboundEstablishmentvoid 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
 
 - 
finishInboundEstablishmentvoid 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
 
 - 
failInboundEstablishmentvoid 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
 
 
- 
 
-