public class NTCPConnection extends Object implements Closeable
| Modifier and Type | Class and Description |
|---|---|
(package private) static class | NTCPConnection.PrepBufferBuffer for preparing data to be written to the connection. |
| Modifier and Type | Field and Description |
|---|---|
(package private) static 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. |
(package private) static int | DELAY_DEFAULTDefault delay value |
(package private) static int | DUMMY_DEFAULTDefault dummy value |
(package private) static int | NTCP2_MAX_MSG_SIZESee spec. |
(package private) static int | PADDING_MAX_DEFAULT_INTDefault maximum padding integer |
(package private) static int | PADDING_MIN_DEFAULT_INTDefault minimum padding integer |
(package private) static int | REASON_AEADAEAD failure reason |
(package private) static int | REASON_BANNEDBanned reason |
(package private) static int | REASON_FRAME_TIMEOUTFrame timeout reason |
(package private) static int | REASON_FRAMINGFraming error reason |
(package private) static int | REASON_MSG1Message 1 error reason |
(package private) static int | REASON_MSG2Message 2 error reason |
(package private) static int | REASON_MSG3Message 3 error reason |
(package private) static int | REASON_OPTIONSOptions mismatch reason |
(package private) static int | REASON_PADDINGPadding violation reason |
(package private) static int | REASON_PAYLOADPayload error reason |
(package private) static int | REASON_S_MISMATCHS mismatch reason |
(package private) static int | REASON_SIGFAILSignature failure reason |
(package private) static int | REASON_SIGTYPESignature type mismatch reason |
(package private) static int | REASON_SKEWClock skew reason |
(package private) static int | REASON_TERMINATIONTermination reason |
(package private) static int | REASON_TIMEOUTTimeout reason |
(package private) static int | REASON_UNSPECUnspecified reason |
| Constructor and Description |
|---|
NTCPConnection(RouterContext ctx,
NTCPTransport transport,
RouterIdentity remotePeer,
RouterAddress remAddr,
int version)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)Close the connection. |
(package private) void | closeOnTimeout(String cause,
Exception e)Close and release EstablishState resources. |
boolean | compareAndSetWriteInterestPending(boolean expected,
boolean update) |
(package private) void | enqueueInfoMessage()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. |
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()When this connection was created (not established). |
long | getEstablishedOn()Get the time this connection was established. |
(package private) EstablishState | getEstablishState()Only valid during establishment;
replaced with EstablishState.VERIFIED or FAILED afterwards |
SelectionKey | getKey()Valid for inbound; valid for outbound shortly after creation |
long | getLastActiveTime() |
long | getLastZeroReadTime() |
static int | getMaxWriteBufs()Get the current max write buffers per NTCP connection. |
boolean | getMayDisconnect() |
int | getMessagesReceived() |
int | getMessagesSent() |
(package private) ByteBuffer | getNextReadBuf()Get the next read buffer, or null if none available. |
(package private) ByteBuffer | getNextWriteBuf()Returns but does not remove the buffer. |
int | getOutboundQueueSize() |
float | getRecvRate() |
RouterAddress | getRemoteAddress()Only valid for outbound; null for inbound |
byte[] | getRemoteIP()Get the remote IP address. |
RouterIdentity | getRemotePeer()Valid for outbound; valid for inbound after handshake |
int | getRemotePort()Get the remote port. |
float | getSendRate() |
long | getTimeSinceCreated()Get time since connection creation in milliseconds. |
long | getTimeSinceCreated(long now)Get time since connection creation in milliseconds. |
long | getTimeSinceReceive()Get time since last receive in milliseconds. |
long | getTimeSinceReceive(long now)Get time since last receive in milliseconds. |
long | getTimeSinceSend()Get time since last send in milliseconds. |
long | getTimeSinceSend(long now)Get time since last send in milliseconds. |
long | getUptime()Get the connection uptime in milliseconds. |
int | getVersion()The NTCP2 version, for the console. |
(package private) Object | getWriteLock()Write lock for pumper delayed writes |
int | gotZeroRead()Track time of zero-reads to avoid closing healthy connections |
boolean | hasWriteInterestPending() |
boolean | isBacklogged() |
boolean | isBanned()Check if the peer is blocklisted or banned. |
boolean | isClosed() |
boolean | isEstablished() |
boolean | isInbound() |
boolean | isIPv6() |
(package private) boolean | isWriteBufEmpty()Replaces getWriteBufCount(). |
(package private) 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. |
(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 | 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) 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 | sendTerminationAndClose()NTCP2. |
void | setChannel(SocketChannel chan)Set the socket channel. |
void | setKey(SelectionKey key)Set the selection key. |
static void | setMaxWriteBufs(int max)Set the max write buffers per NTCP connection (called by Tuner). |
void | setMayDisconnect()Sets to true. |
void | setRemotePeer(RouterIdentity ident)Valid for outbound; valid for inbound after handshake |
void | setVersion(int ver)Set version 2 from InboundEstablishState. |
boolean | shouldSetInterest(int ops)Check if the given interest op is not already set |
boolean | tooBacklogged()Returns true if the outbound connection is currently too backlogged to accept new messages,
allowing a short grace period after connection establishment and optionally logging details. |
String | toString() |
void | updateInterestOps(int ops)Update the cached interest ops |
void | updateLastActiveTime()Update last active time to now |
(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. |
static final int BUFFER_SIZE
static final int DELAY_DEFAULT
static final int DUMMY_DEFAULT
static final int NTCP2_MAX_MSG_SIZE
static final int PADDING_MAX_DEFAULT_INT
static final int PADDING_MIN_DEFAULT_INT
static final int REASON_AEAD
static final int REASON_BANNED
static final int REASON_FRAME_TIMEOUT
static final int REASON_FRAMING
static final int REASON_MSG1
static final int REASON_MSG2
static final int REASON_MSG3
static final int REASON_OPTIONS
static final int REASON_PADDING
static final int REASON_PAYLOAD
static final int REASON_S_MISMATCH
static final int REASON_SIGFAIL
static final int REASON_SIGTYPE
static final int REASON_SKEW
static final int REASON_TERMINATION
static final int REASON_TIMEOUT
static final int REASON_UNSPEC
public NTCPConnection(RouterContext ctx, NTCPTransport transport, RouterIdentity remotePeer, RouterAddress remAddr, int version) throws DataFormatException
ctx - the router contexttransport - the NTCP transportremotePeer - the remote peer identityremAddr - the remote router addressversion - must be 2-5DataFormatException - if there's a problem with the addresspublic NTCPConnection(RouterContext ctx, NTCPTransport transport, SocketChannel chan, SelectionKey key)
ctx - the router contexttransport - the NTCP transportchan - the socket channelkey - the selection keypublic void clearZeroRead()
public void close()
close in interface Closeableclose in interface AutoCloseablepublic void close(boolean allowRequeue)
allowRequeue - if true, requeue pending messagesvoid closeOnTimeout(String cause, Exception e)
cause - the cause of the timeoute - may be nullpublic boolean compareAndSetWriteInterestPending(boolean expected,
boolean update)expected - expected valueupdate - new valuevoid enqueueInfoMessage()
void failInboundEstablishment(CipherState sender, byte[] sip_ba, int reason)
sender - use to send to Alicesip_ba - 24 bytes to init SipHash to Alicereason - the reason codevoid finishInboundEstablishment(CipherState sender, CipherState receiver, byte[] sip_ba, byte[] sip_ab, long clockSkew, NTCP2Options hisPadding)
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 nullvoid finishOutboundEstablishment(CipherState sender, CipherState receiver, byte[] sip_ab, byte[] sip_ba, long clockSkew)
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 Bobpublic SocketChannel getChannel()
public long getClockSkew()
public long getCreated()
public long getEstablishedOn()
EstablishState getEstablishState()
public SelectionKey getKey()
public long getLastActiveTime()
public long getLastZeroReadTime()
public static int getMaxWriteBufs()
public boolean getMayDisconnect()
public int getMessagesReceived()
public int getMessagesSent()
ByteBuffer getNextReadBuf()
ByteBuffer getNextWriteBuf()
public int getOutboundQueueSize()
public float getRecvRate()
public RouterAddress getRemoteAddress()
public byte[] getRemoteIP()
public RouterIdentity getRemotePeer()
public int getRemotePort()
public float getSendRate()
public long getTimeSinceCreated()
public long getTimeSinceCreated(long now)
now - current timepublic long getTimeSinceReceive()
public long getTimeSinceReceive(long now)
now - current timepublic long getTimeSinceSend()
public long getTimeSinceSend(long now)
now - current timepublic long getUptime()
public int getVersion()
Object getWriteLock()
public int gotZeroRead()
public boolean hasWriteInterestPending()
public boolean isBacklogged()
public boolean isBanned()
public boolean isClosed()
public boolean isEstablished()
public boolean isInbound()
public boolean isIPv6()
boolean isWriteBufEmpty()
void outboundConnected()
void prepareNextWrite(NTCPConnection.PrepBuffer prep)
prep - an instance of PrepBuffer to use as scratch spacevoid queuedRecv(ByteBuffer buf, FIFOBandwidthLimiter.Request req)
buf - the buffer to queuereq - the bandwidth requestvoid recv(ByteBuffer buf)
buf - the buffer receivedvoid recvEncryptedI2NP(ByteBuffer buf)
buf - the buffer containing encrypted datavoid removeWriteBuf(ByteBuffer buf)
buf - the buffer to removepublic void send(OutNetMessage msg)
msg - the message to sendvoid sendOurRouterInfo(boolean shouldFlood)
shouldFlood - whether to flood the router infovoid sendTerminationAndClose()
public void setChannel(SocketChannel chan)
chan - the channelpublic void setKey(SelectionKey key)
key - the keypublic static void setMaxWriteBufs(int max)
max - the maximum write bufferspublic void setMayDisconnect()
public void setRemotePeer(RouterIdentity ident)
ident - the remote peer identitypublic void setVersion(int ver)
ver - the version to setpublic boolean shouldSetInterest(int ops)
ops - the interest ops to checkpublic boolean tooBacklogged()
public void updateInterestOps(int ops)
ops - the new interest opspublic void updateLastActiveTime()
void wantsWrite(byte[] data)
data - the data to writevoid wantsWrite(byte[] data,
int off,
int len)data - the data to writeoff - the offset in the datalen - the length of the data