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 Summary
Nested Classes Modifier and Type Class Description (package private) static classNTCPConnection.PrepBuffer
-
Field Summary
Fields 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 Summary
Constructors 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 Summary
All 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_SIZE
static 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_SIZE
static 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_UNSPEC
static final int REASON_UNSPEC
- See Also:
- Constant Field Values
-
REASON_TERMINATION
static final int REASON_TERMINATION
- See Also:
- Constant Field Values
-
REASON_TIMEOUT
static final int REASON_TIMEOUT
- See Also:
- Constant Field Values
-
REASON_AEAD
static final int REASON_AEAD
- See Also:
- Constant Field Values
-
REASON_OPTIONS
static final int REASON_OPTIONS
- See Also:
- Constant Field Values
-
REASON_SIGTYPE
static final int REASON_SIGTYPE
- See Also:
- Constant Field Values
-
REASON_SKEW
static final int REASON_SKEW
- See Also:
- Constant Field Values
-
REASON_PADDING
static final int REASON_PADDING
- See Also:
- Constant Field Values
-
REASON_FRAMING
static final int REASON_FRAMING
- See Also:
- Constant Field Values
-
REASON_PAYLOAD
static final int REASON_PAYLOAD
- See Also:
- Constant Field Values
-
REASON_MSG1
static final int REASON_MSG1
- See Also:
- Constant Field Values
-
REASON_MSG2
static final int REASON_MSG2
- See Also:
- Constant Field Values
-
REASON_MSG3
static final int REASON_MSG3
- See Also:
- Constant Field Values
-
REASON_FRAME_TIMEOUT
static final int REASON_FRAME_TIMEOUT
- See Also:
- Constant Field Values
-
REASON_SIGFAIL
static final int REASON_SIGFAIL
- See Also:
- Constant Field Values
-
REASON_S_MISMATCH
static final int REASON_S_MISMATCH
- See Also:
- Constant Field Values
-
REASON_BANNED
static final int REASON_BANNED
- See Also:
- Constant Field Values
-
PADDING_MIN_DEFAULT_INT
static final int PADDING_MIN_DEFAULT_INT
- See Also:
- Constant Field Values
-
PADDING_MAX_DEFAULT_INT
static final int PADDING_MAX_DEFAULT_INT
- See Also:
- Constant Field Values
-
DUMMY_DEFAULT
static final int DUMMY_DEFAULT
- See Also:
- Constant Field Values
-
DELAY_DEFAULT
static final int DELAY_DEFAULT
- See Also:
- Constant Field Values
-
-
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.
-
NTCPConnection
public 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
-
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
-
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()
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
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
-
queuedWrite
void queuedWrite(ByteBuffer buf, FIFOBandwidthLimiter.Request req)
ditto for writes
-
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 Bobreceiver- use to receive from Bobsip_ab- 24 bytes to init SipHash to Bobsip_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 Alicereceiver- use to receive from Alicesip_ba- 24 bytes to init SipHash to Alicesip_ab- 24 bytes to init SipHash from AlicehisPadding- 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 Alicesip_ba- 24 bytes to init SipHash to Alice- Since:
- 0.9.36
-
-