class NTCPConnection extends Object implements Closeable
+-------+-------+--//--+---//----+-------+-------+-------+-------+ | sizeof(data) | data | padding | adler checksum of sz+data+pad | +-------+-------+--//--+---//----+-------+-------+-------+-------+That message is then encrypted with the DH/2048 negotiated session key (station to station authenticated per the EstablishState class) using the last 16 bytes of the previous encrypted message as the IV. One special case is a metadata message where the sizeof(data) is 0. In that case, the unencrypted message is encoded as:
+-------+-------+-------+-------+-------+-------+-------+-------+ | 0 | timestamp in seconds | uninterpreted +-------+-------+-------+-------+-------+-------+-------+-------+ uninterpreted | adler checksum of sz+data+pad | +-------+-------+-------+-------+-------+-------+-------+-------+
Modifier and Type | Class and Description |
---|---|
static class |
NTCPConnection.PrepBuffer |
Modifier and Type | Field and Description |
---|---|
static 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.
|
static int |
MAX_MSG_SIZE
2 bytes for length and 4 for CRC
|
Constructor and Description |
---|
NTCPConnection(RouterContext ctx,
NTCPTransport transport,
RouterIdentity remotePeer,
RouterAddress remAddr)
Create an outbound unconnected NTCP connection
|
NTCPConnection(RouterContext ctx,
NTCPTransport transport,
SocketChannel chan,
SelectionKey key)
Create an inbound connected (though not established) NTCP connection
|
Modifier and Type | Method and Description |
---|---|
void |
clearZeroRead()
workaround for EventPumper
|
void |
close() |
void |
close(boolean allowRequeue) |
void |
closeOnTimeout(String cause,
Exception e)
Close and release EstablishState resources.
|
void |
enqueueInfoMessage()
Inject a DatabaseStoreMessage with our RouterInfo
|
void |
finishInboundEstablishment(SessionKey key,
long clockSkew,
byte[] prevWriteEnd,
byte[] prevReadEnd)
We are Bob.
|
void |
finishOutboundEstablishment(SessionKey key,
long clockSkew,
byte[] prevWriteEnd,
byte[] prevReadEnd)
We are Alice.
|
SocketChannel |
getChannel()
Valid for inbound; valid for outbound shortly after creation
|
long |
getClockSkew()
A positive number means our clock is ahead of theirs.
|
long |
getCreated() |
EstablishState |
getEstablishState()
Only valid during establishment; null later
|
SelectionKey |
getKey()
Valid for inbound; valid for outbound shortly after creation
|
boolean |
getMayDisconnect() |
int |
getMessagesReceived() |
int |
getMessagesSent() |
ByteBuffer |
getNextReadBuf() |
ByteBuffer |
getNextWriteBuf() |
int |
getOutboundQueueSize() |
float |
getRecvRate() |
RouterAddress |
getRemoteAddress()
Only valid for outbound; null for inbound
|
RouterIdentity |
getRemotePeer()
Valid for outbound; valid for inbound after handshake
|
float |
getSendRate() |
long |
getTimeSinceCreated() |
long |
getTimeSinceReceive() |
long |
getTimeSinceSend() |
long |
getUptime() |
int |
gotZeroRead()
workaround for EventPumper
|
boolean |
isBacklogged()
private long queueTime() {
OutNetMessage msg = _currentOutbound;
if (msg == null) {
msg = _outbound.peek();
if (msg == null)
return 0;
}
return msg.getSendTime(); // does not include any of the pre-send(...) preparation
}
|
boolean |
isClosed() |
boolean |
isEstablished() |
boolean |
isInbound() |
boolean |
isIPv6() |
boolean |
isWriteBufEmpty()
Replaces getWriteBufCount()
|
void |
outboundConnected()
async callback after the outbound connection was completed (this should NOT block,
as it occurs in the selector thread)
|
(package private) void |
prepareNextWrite(NTCPConnection.PrepBuffer prep)
prepare the next i2np message for transmission.
|
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.
|
void |
queuedWrite(ByteBuffer buf,
FIFOBandwidthLimiter.Request req)
ditto for writes
|
void |
recv(ByteBuffer buf)
The contents of the buffer have been read and can be processed asap.
|
(package private) 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.
|
(package private) static void |
releaseResources()
Call at transport shutdown
|
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
|
void |
setChannel(SocketChannel chan) |
void |
setKey(SelectionKey key) |
void |
setMayDisconnect()
Sets to true.
|
void |
setRemotePeer(RouterIdentity ident) |
boolean |
tooBacklogged() |
String |
toString() |
void |
write(ByteBuffer buf)
The contents of the buffer have been encrypted / padded / etc and have
been fully allocated for the bandwidth limiter.
|
public static final int BUFFER_SIZE
public static final int MAX_MSG_SIZE
public NTCPConnection(RouterContext ctx, NTCPTransport transport, SocketChannel chan, SelectionKey key)
public NTCPConnection(RouterContext ctx, NTCPTransport transport, RouterIdentity remotePeer, RouterAddress remAddr)
public SocketChannel getChannel()
public SelectionKey getKey()
public void setChannel(SocketChannel chan)
public void setKey(SelectionKey key)
public boolean isInbound()
public boolean isEstablished()
public boolean isIPv6()
public EstablishState getEstablishState()
public RouterAddress getRemoteAddress()
public RouterIdentity getRemotePeer()
public void setRemotePeer(RouterIdentity ident)
public void finishInboundEstablishment(SessionKey key, long clockSkew, byte[] prevWriteEnd, byte[] prevReadEnd)
clockSkew
- OUR clock minus ALICE's clock in seconds (may be negative, obviously, but |val| should
be under 1 minute)prevWriteEnd
- exactly 16 bytes, not copied, do not corruptprevReadEnd
- 16 or more bytes, last 16 bytes copiedpublic long getClockSkew()
public long getUptime()
public int getMessagesSent()
public int getMessagesReceived()
public int getOutboundQueueSize()
public long getTimeSinceSend()
public long getTimeSinceReceive()
public long getTimeSinceCreated()
public long getCreated()
public void setMayDisconnect()
public boolean getMayDisconnect()
public void clearZeroRead()
public int gotZeroRead()
public boolean isClosed()
public void close()
close
in interface Closeable
close
in interface AutoCloseable
public void close(boolean allowRequeue)
public void closeOnTimeout(String cause, Exception e)
e
- may be nullpublic void send(OutNetMessage msg)
public boolean isBacklogged()
public boolean tooBacklogged()
public void enqueueInfoMessage()
public void finishOutboundEstablishment(SessionKey key, long clockSkew, byte[] prevWriteEnd, byte[] prevReadEnd)
clockSkew
- OUR clock minus BOB's clock in seconds (may be negative, obviously, but |val| should
be under 1 minute)prevWriteEnd
- exactly 16 bytes, not copied, do not corruptprevReadEnd
- 16 or more bytes, last 16 bytes copiedvoid prepareNextWrite(NTCPConnection.PrepBuffer prep)
prep
- an instance of PrepBuffer to use as scratch spacepublic void outboundConnected()
public void queuedRecv(ByteBuffer buf, FIFOBandwidthLimiter.Request req)
public void queuedWrite(ByteBuffer buf, FIFOBandwidthLimiter.Request req)
public void recv(ByteBuffer buf)
public void write(ByteBuffer buf)
public ByteBuffer getNextReadBuf()
public boolean isWriteBufEmpty()
public ByteBuffer getNextWriteBuf()
public void removeWriteBuf(ByteBuffer buf)
public float getSendRate()
public float getRecvRate()
void recvEncryptedI2NP(ByteBuffer buf)
static void releaseResources()