java.lang.Object
net.i2p.router.transport.ntcp.EstablishBase
- All Implemented Interfaces:
EstablishState
- Direct Known Subclasses:
EstablishBase.FailedEstablishState, EstablishBase.VerifiedEstablishState, InboundEstablishState
Base class for NTCP connection establishment. Provides common functionality for NTCP 2 handshake state machines, including cryptographic operations, timing calculations, and peer skew management. Inbound NTCP 2 only - OutboundNTCP2State does not extend this.
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static classEstablish state that failed, to retry later.protected static enumStates for NTCP connection establishment process.private static classEstablish state that passed signature verification.Field Summary
Modifier and TypeFieldDescriptionprotected final NTCPConnectionConnection being established by this handshake.protected final RouterContextRouter context, supplied to the subclass that constructs this.protected StringWhy the handshake failed, or null while it is still viable.private final AtomicBooleanSet when the handshake has finished.private final AtomicBooleanSet when the handshake has failed for good.protected final LogLog scoped to the subclass that constructs this.protected longOUR clock minus HIS clock, in seconds.protected byte[]Previously received encrypted block (or the IV).protected intBytes received so far.protected EstablishBase.StateCurrent handshake state, replaced under the state lock.protected final ObjectMonitor serialising every handshake state change.protected final NTCPTransportNTCP transport running this handshake.protected final byte[]Key block Bob receives and Alice sends, doubled as padding on inbound handshakes.protected final byte[]Key block Alice receives and Bob sends, null on outbound handshakes.protected static final intAES block and key size in bytes.static final EstablishBase.FailedEstablishStateShared instance marking a handshake that ended in corruption.protected static final Set<EstablishBase.State> The two terminal states, VERIFIED and CORRUPT.static final EstablishBase.VerifiedEstablishStateShared instance marking a handshake that completed and verified successfully.Constructor Summary
ConstructorsModifierConstructorDescriptionprivateCreates the instance used only for its constants, with no context.protectedEstablishBase(RouterContext ctx, NTCPTransport transport, NTCPConnection con) Binds the context, transport and connection, sizing the padding buffer.Method Summary
Modifier and TypeMethodDescriptionprotected static final StringMarker function for i18n, returns the string unchanged.protected voidchangeState(EstablishBase.State state) Change the handshake state and update completion/corruption flags.voidRelease resources on timeout.protected voidFail the handshake.protected voidFail the handshake with exception.protected voidFail the handshake optionally suppressing skew stat.Failure reason if the handshake failed, or null if not failed/corrupt.abstract intNTCP version.booleanIf synchronized on this, fails with deadlocks from all over viaCSFI.isEstablished().booleanDid the handshake fail for some reason?protected Stringprefix()Logging prefix string.voidDoes nothing.voidreceive(ByteBuffer src) Parse the contents of the buffer as part of the handshake.protected voidreleaseBufs(boolean isVerified) Only call once.(package private) static StringThe bare cause from a netdb store rejection, stripped of the "Invalid NetDbStore attempt" wrapper, capability flags and router ID.toString()String representation of this state.
Field Details
VERIFIED
Shared instance marking a handshake that completed and verified successfully.FAILED
Shared instance marking a handshake that ended in corruption._context
Router context, supplied to the subclass that constructs this._log
Log scoped to the subclass that constructs this._X
protected final byte[] _XKey block Bob receives and Alice sends, doubled as padding on inbound handshakes._Y
protected final byte[] _YKey block Alice receives and Bob sends, null on outbound handshakes._peerSkew
protected long _peerSkewOUR clock minus HIS clock, in seconds. Inbound: tsB - tsA - rtt/2, Outbound: tsA - tsB - rtt/2._prevEncrypted
protected byte[] _prevEncryptedPreviously received encrypted block (or the IV)._received
protected int _receivedBytes received so far._transport
NTCP transport running this handshake._con
Connection being established by this handshake.AES_SIZE
_stateLock
Monitor serialising every handshake state change._state
Current handshake state, replaced under the state lock._isCorrupt
Set when the handshake has failed for good._isComplete
Set when the handshake has finished._failReason
Why the handshake failed, or null while it is still viable.STATES_DONE
The two terminal states, VERIFIED and CORRUPT.
Constructor Details
EstablishBase
private EstablishBase()Creates the instance used only for its constants, with no context.EstablishBase
Binds the context, transport and connection, sizing the padding buffer.- Parameters:
ctx- the router context, supplying the log managertransport- the NTCP transport this handshake runs undercon- the connection being established, whose inbound flag sizes the padding buffer
Method Details
changeState
Change the handshake state and update completion/corruption flags.- Parameters:
state- the new state
receive
Parse the contents of the buffer as part of the handshake.All data must be copied out of the buffer as
Reader.processRead(NTCPConnection)will return it to the pool.If there are additional data in the buffer after the handshake is complete, the EstablishState is responsible for passing it to NTCPConnection.
- Specified by:
receivein interfaceEstablishState- Parameters:
src- the buffer to parse
prepareOutbound
public void prepareOutbound()Does nothing. Outbound (Alice) must override. We are establishing an outbound connection, so prepare ourselves by queueing up the write of the first part of the handshake.- Specified by:
prepareOutboundin interfaceEstablishState
isCorrupt
public boolean isCorrupt()Did the handshake fail for some reason?- Specified by:
isCorruptin interfaceEstablishState- Returns:
- whether the handshake failed
isComplete
public boolean isComplete()If synchronized on this, fails with deadlocks from all over viaCSFI.isEstablished(). AlsoCSFI.getFramedAveragePeerClockSkew().- Specified by:
isCompletein interfaceEstablishState- Returns:
- is the handshake complete and valid?
getVersion
public abstract int getVersion()NTCP version.- Specified by:
getVersionin interfaceEstablishState- Returns:
- 1, 2, or 0 if unknown
close
Release resources on timeout.- Specified by:
closein interfaceEstablishState- Parameters:
reason- the text logged with the warning and returned byEstablishState.getFailReason()e- may be null
fail
Fail the handshake. Caller must synch.- Parameters:
reason- the text logged with the warning and returned bygetFailReason()
fail
Fail the handshake with exception. Caller must synch.- Parameters:
reason- the text logged with the warning and returned bygetFailReason()e- the exception cause, may be null
fail
Fail the handshake optionally suppressing skew stat. Caller must synch.- Parameters:
reason- the text logged with the warning and returned bygetFailReason()e- the exception cause, may be nullbySkew- true to suppress the skew stat, as when we caused the skew ourselves
getFailReason
Failure reason if the handshake failed, or null if not failed/corrupt.- Specified by:
getFailReasonin interfaceEstablishState- Returns:
- failure reason string or null
releaseBufs
protected void releaseBufs(boolean isVerified) Only call once. Caller must synch.- Parameters:
isVerified- true when the handshake completed and was not marked corrupt
prefix
toString
storeFailCause
The bare cause from a netdb store rejection, stripped of the "Invalid NetDbStore attempt" wrapper, capability flags and router ID.- Parameters:
iae- the rejection from the netdb- Returns:
- the cause, for log output
_x