Package net.i2p.router.transport.ntcp
Class NTCPConnection
java.lang.Object
net.i2p.router.transport.ntcp.NTCPConnection
- All Implemented Interfaces:
Closeable
,AutoCloseable
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
-
Field Summary
Modifier and TypeFieldDescription(package private) static final int
Why 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 final int
(package private) static final int
(package private) static final int
See spec.(package private) static final int
(package private) static final int
(package private) static final int
(package private) static final int
(package private) static final int
(package private) static final int
(package private) static final int
(package private) static final int
(package private) static final int
(package private) static final int
(package private) static final int
(package private) static final int
(package private) static final int
(package private) static final int
(package private) static final int
(package private) static final int
(package private) static final int
(package private) static final int
(package private) static final int
-
Constructor Summary
ConstructorDescriptionNTCPConnection
(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
Modifier and TypeMethodDescription(package private) void
workaround for EventPumpervoid
close()
void
close
(boolean allowRequeue) (package private) void
closeOnTimeout
(String cause, Exception e) Close and release EstablishState resources.(package private) void
Inject a DatabaseStoreMessage with our RouterInfo.(package private) void
failInboundEstablishment
(CipherState sender, byte[] sip_ba, int reason) We are Bob.(package private) void
finishInboundEstablishment
(CipherState sender, CipherState receiver, byte[] sip_ba, byte[] sip_ab, long clockSkew, NTCP2Options hisPadding) We are Bob.(package private) void
finishOutboundEstablishment
(CipherState sender, CipherState receiver, byte[] sip_ab, byte[] sip_ba, long clockSkew) We are Alice.Valid for inbound; valid for outbound shortly after creationlong
A positive number means our clock is ahead of theirs.long
long
(package private) EstablishState
Only valid during establishment; replaced with EstablishState.VERIFIED or FAILED afterwardgetKey()
Valid for inbound; valid for outbound shortly after creationboolean
int
int
(package private) ByteBuffer
(package private) ByteBuffer
Returns but does not remove the buffer.int
float
Only valid for outbound; null for inboundbyte[]
Valid for outbound; valid for inbound after handshakefloat
long
long
getTimeSinceCreated
(long now) long
long
getTimeSinceReceive
(long now) long
long
getTimeSinceSend
(long now) long
int
The NTCP2 version, for the console.(package private) Object
Write lock for pumper delayed writes(package private) int
workaround for EventPumperboolean
boolean
isClosed()
boolean
boolean
boolean
isIPv6()
(package private) boolean
Replaces getWriteBufCount().(package private) void
async callback after the outbound connection was completed (this should NOT block, as it occurs in the selector thread)(package private) void
Prepare the next I2NP message for transmission.(package private) 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.(package private) void
recv
(ByteBuffer buf) The contents of the buffer have been read and can be processed asap.(package private) void
Connection must be established! The contents of the buffer include some fraction of one or more encrypted and encoded I2NP messages.(package private) static void
Call at transport shutdown(package private) void
removeWriteBuf
(ByteBuffer buf) Remove the buffer, which _should_ be the one at the head of _writeBufs.void
send
(OutNetMessage msg) toss the message onto the connection's send queue(package private) void
sendOurRouterInfo
(boolean shouldFlood) NTCP2 only(package private) void
NTCP 1 or 2.void
setChannel
(SocketChannel chan) void
setKey
(SelectionKey key) void
Sets to true.void
setRemotePeer
(RouterIdentity ident) Valid for outbound; valid for inbound after handshakevoid
setVersion
(int ver) Set version 2 from InboundEstablishState.boolean
toString()
(package private) void
wantsWrite
(byte[] data) Call when there is data ready to write.(package private) void
wantsWrite
(byte[] data, int off, int len) Call when there is data ready to write.
-
Field Details
-
BUFFER_SIZE
static final int BUFFER_SIZEWhy 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:
-
NTCP2_MAX_MSG_SIZE
static final int NTCP2_MAX_MSG_SIZESee spec. Max Noise payload 65535, minus 16 byte MAC and 3 byte block header. Includes 9-byte I2NP header.- See Also:
-
REASON_UNSPEC
static final int REASON_UNSPEC- See Also:
-
REASON_TERMINATION
static final int REASON_TERMINATION- See Also:
-
REASON_TIMEOUT
static final int REASON_TIMEOUT- See Also:
-
REASON_AEAD
static final int REASON_AEAD- See Also:
-
REASON_OPTIONS
static final int REASON_OPTIONS- See Also:
-
REASON_SIGTYPE
static final int REASON_SIGTYPE- See Also:
-
REASON_SKEW
static final int REASON_SKEW- See Also:
-
REASON_PADDING
static final int REASON_PADDING- See Also:
-
REASON_FRAMING
static final int REASON_FRAMING- See Also:
-
REASON_PAYLOAD
static final int REASON_PAYLOAD- See Also:
-
REASON_MSG1
static final int REASON_MSG1- See Also:
-
REASON_MSG2
static final int REASON_MSG2- See Also:
-
REASON_MSG3
static final int REASON_MSG3- See Also:
-
REASON_FRAME_TIMEOUT
static final int REASON_FRAME_TIMEOUT- See Also:
-
REASON_SIGFAIL
static final int REASON_SIGFAIL- See Also:
-
REASON_S_MISMATCH
static final int REASON_S_MISMATCH- See Also:
-
REASON_BANNED
static final int REASON_BANNED- See Also:
-
PADDING_MIN_DEFAULT_INT
static final int PADDING_MIN_DEFAULT_INT- See Also:
-
PADDING_MAX_DEFAULT_INT
static final int PADDING_MAX_DEFAULT_INT- See Also:
-
DUMMY_DEFAULT
static final int DUMMY_DEFAULT- See Also:
-
DELAY_DEFAULT
static final int DELAY_DEFAULT- See Also:
-
-
Constructor Details
-
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 Details
-
getChannel
Valid for inbound; valid for outbound shortly after creation -
getKey
Valid for inbound; valid for outbound shortly after creation -
setChannel
-
setKey
-
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
Only valid for outbound; null for inbound -
getRemotePeer
Valid for outbound; valid for inbound after handshake -
setRemotePeer
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 interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
-
close
public void close(boolean allowRequeue) -
closeOnTimeout
Close and release EstablishState resources.- Parameters:
e
- may be null- Since:
- 0.9.16
-
send
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
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
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
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
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
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 Bobreceiver
- use to receive from Bobsip_ab
- 24 bytes to init SipHash to Bobsip_ba
- 24 bytes to init SipHash from BobclockSkew
- 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 Alicereceiver
- use to receive from Alicesip_ba
- 24 bytes to init SipHash to Alicesip_ab
- 24 bytes to init SipHash from AliceclockSkew
- 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
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
-
toString
-