forked from I2P_Developers/i2p.i2p
NTCP: Return unused DH keypairs to the pool
This commit is contained in:
@ -64,6 +64,7 @@ import net.i2p.util.SimpleByteCache;
|
|||||||
class EstablishState {
|
class EstablishState {
|
||||||
|
|
||||||
public static final VerifiedEstablishState VERIFIED = new VerifiedEstablishState();
|
public static final VerifiedEstablishState VERIFIED = new VerifiedEstablishState();
|
||||||
|
public static final FailedEstablishState FAILED = new FailedEstablishState();
|
||||||
|
|
||||||
private final RouterContext _context;
|
private final RouterContext _context;
|
||||||
private final Log _log;
|
private final Log _log;
|
||||||
@ -120,7 +121,7 @@ class EstablishState {
|
|||||||
private static final int HXY_SIZE = 32; //Hash.HASH_LENGTH;
|
private static final int HXY_SIZE = 32; //Hash.HASH_LENGTH;
|
||||||
private static final int HXY_TSB_PAD_SIZE = HXY_SIZE + 4 + 12; // 48
|
private static final int HXY_TSB_PAD_SIZE = HXY_SIZE + 4 + 12; // 48
|
||||||
|
|
||||||
private State _state;
|
protected State _state;
|
||||||
|
|
||||||
private enum State {
|
private enum State {
|
||||||
OB_INIT,
|
OB_INIT,
|
||||||
@ -163,7 +164,6 @@ class EstablishState {
|
|||||||
_transport = null;
|
_transport = null;
|
||||||
_con = null;
|
_con = null;
|
||||||
_e_hXY_tsB = null;
|
_e_hXY_tsB = null;
|
||||||
_state = State.VERIFIED;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public EstablishState(RouterContext ctx, NTCPTransport transport, NTCPConnection con) {
|
public EstablishState(RouterContext ctx, NTCPTransport transport, NTCPConnection con) {
|
||||||
@ -900,6 +900,15 @@ class EstablishState {
|
|||||||
*/
|
*/
|
||||||
public synchronized byte[] getExtraBytes() { return _extra; }
|
public synchronized byte[] getExtraBytes() { return _extra; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Release resources on timeout.
|
||||||
|
* @param e may be null
|
||||||
|
* @since 0.9.16
|
||||||
|
*/
|
||||||
|
public synchronized void close(String reason, Exception e) {
|
||||||
|
fail(reason, e);
|
||||||
|
}
|
||||||
|
|
||||||
/** Caller must synch. */
|
/** Caller must synch. */
|
||||||
private void fail(String reason) { fail(reason, null); }
|
private void fail(String reason) { fail(reason, null); }
|
||||||
|
|
||||||
@ -919,7 +928,10 @@ class EstablishState {
|
|||||||
releaseBufs();
|
releaseBufs();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Only call once. Caller must synch. */
|
/**
|
||||||
|
* Only call once. Caller must synch.
|
||||||
|
* @since 0.9.16
|
||||||
|
*/
|
||||||
private void releaseBufs() {
|
private void releaseBufs() {
|
||||||
// null or longer for OB
|
// null or longer for OB
|
||||||
if (_prevEncrypted != null && _prevEncrypted.length == AES_SIZE)
|
if (_prevEncrypted != null && _prevEncrypted.length == AES_SIZE)
|
||||||
@ -927,8 +939,12 @@ class EstablishState {
|
|||||||
SimpleByteCache.release(_curEncrypted);
|
SimpleByteCache.release(_curEncrypted);
|
||||||
SimpleByteCache.release(_curDecrypted);
|
SimpleByteCache.release(_curDecrypted);
|
||||||
SimpleByteCache.release(_hX_xor_bobIdentHash);
|
SimpleByteCache.release(_hX_xor_bobIdentHash);
|
||||||
SimpleByteCache.release(_X);
|
if (_dh.getPeerPublicValue() == null)
|
||||||
SimpleByteCache.release(_Y);
|
_transport.returnUnused(_dh);
|
||||||
|
if (_con.isInbound())
|
||||||
|
SimpleByteCache.release(_X);
|
||||||
|
else
|
||||||
|
SimpleByteCache.release(_Y);
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized String getError() { return _err; }
|
public synchronized String getError() { return _err; }
|
||||||
@ -1024,12 +1040,36 @@ class EstablishState {
|
|||||||
* @since 0.9.8
|
* @since 0.9.8
|
||||||
*/
|
*/
|
||||||
private static class VerifiedEstablishState extends EstablishState {
|
private static class VerifiedEstablishState extends EstablishState {
|
||||||
@Override public boolean isComplete() { return true; }
|
|
||||||
|
public VerifiedEstablishState() {
|
||||||
|
super();
|
||||||
|
_state = State.VERIFIED;
|
||||||
|
}
|
||||||
|
|
||||||
@Override public void prepareOutbound() {
|
@Override public void prepareOutbound() {
|
||||||
Log log =RouterContext.getCurrentContext().logManager().getLog(VerifiedEstablishState.class);
|
Log log =RouterContext.getCurrentContext().logManager().getLog(VerifiedEstablishState.class);
|
||||||
log.warn("prepareOutbound() on verified state, doing nothing!");
|
log.warn("prepareOutbound() on verified state, doing nothing!");
|
||||||
}
|
}
|
||||||
@Override public String toString() { return "VerfiedEstablishState";}
|
|
||||||
|
@Override public String toString() { return "VerifiedEstablishState";}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @since 0.9.16
|
||||||
|
*/
|
||||||
|
private static class FailedEstablishState extends EstablishState {
|
||||||
|
|
||||||
|
public FailedEstablishState() {
|
||||||
|
super();
|
||||||
|
_state = State.CORRUPT;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public void prepareOutbound() {
|
||||||
|
Log log =RouterContext.getCurrentContext().logManager().getLog(VerifiedEstablishState.class);
|
||||||
|
log.warn("prepareOutbound() on verified state, doing nothing!");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public String toString() { return "FailedEstablishState";}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @deprecated unused */
|
/** @deprecated unused */
|
||||||
|
@ -532,14 +532,14 @@ class EventPumper implements Runnable {
|
|||||||
con.outboundConnected();
|
con.outboundConnected();
|
||||||
_context.statManager().addRateData("ntcp.connectSuccessful", 1);
|
_context.statManager().addRateData("ntcp.connectSuccessful", 1);
|
||||||
} else {
|
} else {
|
||||||
con.close();
|
con.closeOnTimeout("connect failed", null);
|
||||||
_transport.markUnreachable(con.getRemotePeer().calculateHash());
|
_transport.markUnreachable(con.getRemotePeer().calculateHash());
|
||||||
_context.statManager().addRateData("ntcp.connectFailedTimeout", 1);
|
_context.statManager().addRateData("ntcp.connectFailedTimeout", 1);
|
||||||
}
|
}
|
||||||
} catch (IOException ioe) { // this is the usual failure path for a timeout or connect refused
|
} catch (IOException ioe) { // this is the usual failure path for a timeout or connect refused
|
||||||
if (_log.shouldLog(Log.INFO))
|
if (_log.shouldLog(Log.INFO))
|
||||||
_log.info("Failed outbound " + con, ioe);
|
_log.info("Failed outbound " + con, ioe);
|
||||||
con.close();
|
con.closeOnTimeout("connect failed", ioe);
|
||||||
//_context.banlist().banlistRouter(con.getRemotePeer().calculateHash(), "Error connecting", NTCPTransport.STYLE);
|
//_context.banlist().banlistRouter(con.getRemotePeer().calculateHash(), "Error connecting", NTCPTransport.STYLE);
|
||||||
_transport.markUnreachable(con.getRemotePeer().calculateHash());
|
_transport.markUnreachable(con.getRemotePeer().calculateHash());
|
||||||
_context.statManager().addRateData("ntcp.connectFailedTimeoutIOE", 1);
|
_context.statManager().addRateData("ntcp.connectFailedTimeoutIOE", 1);
|
||||||
|
@ -371,10 +371,21 @@ class NTCPConnection {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Close and release EstablishState resources.
|
||||||
|
* @param e may be null
|
||||||
|
* @since 0.9.16
|
||||||
|
*/
|
||||||
|
public void closeOnTimeout(String cause, Exception e) {
|
||||||
|
EstablishState es = _establishState;
|
||||||
|
close();
|
||||||
|
es.close(cause, e);
|
||||||
|
}
|
||||||
|
|
||||||
private synchronized NTCPConnection locked_close(boolean allowRequeue) {
|
private synchronized NTCPConnection locked_close(boolean allowRequeue) {
|
||||||
if (_chan != null) try { _chan.close(); } catch (IOException ioe) { }
|
if (_chan != null) try { _chan.close(); } catch (IOException ioe) { }
|
||||||
if (_conKey != null) _conKey.cancel();
|
if (_conKey != null) _conKey.cancel();
|
||||||
_establishState = EstablishState.VERIFIED;
|
_establishState = EstablishState.FAILED;
|
||||||
NTCPConnection old = _transport.removeCon(this);
|
NTCPConnection old = _transport.removeCon(this);
|
||||||
_transport.getReader().connectionClosed(this);
|
_transport.getReader().connectionClosed(this);
|
||||||
_transport.getWriter().connectionClosed(this);
|
_transport.getWriter().connectionClosed(this);
|
||||||
|
Reference in New Issue
Block a user