forked from I2P_Developers/i2p.i2p
2005-07-31 jrandom
* Adjust the netDb search and store per peer timeouts to match the average
measured per peer success times, rather than huge fixed values.
* Optimized and reverified the netDb peer selection / retrieval process
within the kbuckets.
* Drop TCP connections that don't have any useful activity in 10 minutes.
* If i2np.udp.fixedPort=true, never change the externally published port,
even if we are autodetecting the IP address.
(also includes most of the new peer/NAT testing, but thats not used atm)
This commit is contained in:
@@ -221,10 +221,10 @@ public class DeliveryInstructions extends DataStructureImpl {
|
||||
return val;
|
||||
}
|
||||
|
||||
private byte[] getAdditionalInfo() throws DataFormatException {
|
||||
private int getAdditionalInfoSize() {
|
||||
int additionalSize = 0;
|
||||
if (getEncrypted()) {
|
||||
if (_encryptionKey == null) throw new DataFormatException("Encryption key is not set");
|
||||
if (_encryptionKey == null) throw new IllegalStateException("Encryption key is not set");
|
||||
additionalSize += SessionKey.KEYSIZE_BYTES;
|
||||
}
|
||||
switch (getDeliveryMode()) {
|
||||
@@ -233,15 +233,15 @@ public class DeliveryInstructions extends DataStructureImpl {
|
||||
_log.debug("mode = local");
|
||||
break;
|
||||
case FLAG_MODE_DESTINATION:
|
||||
if (_destinationHash == null) throw new DataFormatException("Destination hash is not set");
|
||||
if (_destinationHash == null) throw new IllegalStateException("Destination hash is not set");
|
||||
additionalSize += Hash.HASH_LENGTH;
|
||||
break;
|
||||
case FLAG_MODE_ROUTER:
|
||||
if (_routerHash == null) throw new DataFormatException("Router hash is not set");
|
||||
if (_routerHash == null) throw new IllegalStateException("Router hash is not set");
|
||||
additionalSize += Hash.HASH_LENGTH;
|
||||
break;
|
||||
case FLAG_MODE_TUNNEL:
|
||||
if ( (_routerHash == null) || (_tunnelId == null) ) throw new DataFormatException("Router hash or tunnel ID is not set");
|
||||
if ( (_routerHash == null) || (_tunnelId == null) ) throw new IllegalStateException("Router hash or tunnel ID is not set");
|
||||
additionalSize += Hash.HASH_LENGTH;
|
||||
additionalSize += 4; // tunnelId
|
||||
break;
|
||||
@@ -249,13 +249,23 @@ public class DeliveryInstructions extends DataStructureImpl {
|
||||
|
||||
if (getDelayRequested()) {
|
||||
additionalSize += 4;
|
||||
}
|
||||
|
||||
}
|
||||
return additionalSize;
|
||||
}
|
||||
|
||||
private byte[] getAdditionalInfo() {
|
||||
int additionalSize = getAdditionalInfoSize();
|
||||
byte rv[] = new byte[additionalSize];
|
||||
int offset = 0;
|
||||
|
||||
offset += getAdditionalInfo(rv, offset);
|
||||
if (offset != additionalSize)
|
||||
_log.log(Log.CRIT, "wtf, additionalSize = " + additionalSize + ", offset = " + offset);
|
||||
return rv;
|
||||
}
|
||||
private int getAdditionalInfo(byte rv[], int offset) {
|
||||
int origOffset = offset;
|
||||
if (getEncrypted()) {
|
||||
if (_encryptionKey == null) throw new DataFormatException("Encryption key is not set");
|
||||
if (_encryptionKey == null) throw new IllegalStateException("Encryption key is not set");
|
||||
System.arraycopy(_encryptionKey.getData(), 0, rv, offset, SessionKey.KEYSIZE_BYTES);
|
||||
offset += SessionKey.KEYSIZE_BYTES;
|
||||
if (_log.shouldLog(Log.DEBUG))
|
||||
@@ -270,21 +280,21 @@ public class DeliveryInstructions extends DataStructureImpl {
|
||||
_log.debug("mode = local");
|
||||
break;
|
||||
case FLAG_MODE_DESTINATION:
|
||||
if (_destinationHash == null) throw new DataFormatException("Destination hash is not set");
|
||||
if (_destinationHash == null) throw new IllegalStateException("Destination hash is not set");
|
||||
System.arraycopy(_destinationHash.getData(), 0, rv, offset, Hash.HASH_LENGTH);
|
||||
offset += Hash.HASH_LENGTH;
|
||||
if (_log.shouldLog(Log.DEBUG))
|
||||
_log.debug("mode = destination, hash = " + _destinationHash);
|
||||
break;
|
||||
case FLAG_MODE_ROUTER:
|
||||
if (_routerHash == null) throw new DataFormatException("Router hash is not set");
|
||||
if (_routerHash == null) throw new IllegalStateException("Router hash is not set");
|
||||
System.arraycopy(_routerHash.getData(), 0, rv, offset, Hash.HASH_LENGTH);
|
||||
offset += Hash.HASH_LENGTH;
|
||||
if (_log.shouldLog(Log.DEBUG))
|
||||
_log.debug("mode = router, routerHash = " + _routerHash);
|
||||
break;
|
||||
case FLAG_MODE_TUNNEL:
|
||||
if ( (_routerHash == null) || (_tunnelId == null) ) throw new DataFormatException("Router hash or tunnel ID is not set");
|
||||
if ( (_routerHash == null) || (_tunnelId == null) ) throw new IllegalStateException("Router hash or tunnel ID is not set");
|
||||
System.arraycopy(_routerHash.getData(), 0, rv, offset, Hash.HASH_LENGTH);
|
||||
offset += Hash.HASH_LENGTH;
|
||||
DataHelper.toLong(rv, offset, 4, _tunnelId.getTunnelId());
|
||||
@@ -303,7 +313,7 @@ public class DeliveryInstructions extends DataStructureImpl {
|
||||
if (_log.shouldLog(Log.DEBUG))
|
||||
_log.debug("delay NOT requested");
|
||||
}
|
||||
return rv;
|
||||
return offset - origOffset;
|
||||
}
|
||||
|
||||
public void writeBytes(OutputStream out) throws DataFormatException, IOException {
|
||||
@@ -320,6 +330,27 @@ public class DeliveryInstructions extends DataStructureImpl {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* return the number of bytes written to the target
|
||||
*/
|
||||
public int writeBytes(byte target[], int offset) {
|
||||
if ( (_deliveryMode < 0) || (_deliveryMode > FLAG_MODE_TUNNEL) ) throw new IllegalStateException("Invalid data: mode = " + _deliveryMode);
|
||||
long flags = getFlags();
|
||||
if (_log.shouldLog(Log.DEBUG))
|
||||
_log.debug("Write flags: " + flags + " mode: " + getDeliveryMode()
|
||||
+ " =?= " + flagMode(flags));
|
||||
int origOffset = offset;
|
||||
DataHelper.toLong(target, offset, 1, flags);
|
||||
offset++;
|
||||
offset += getAdditionalInfo(target, offset);
|
||||
return offset - origOffset;
|
||||
}
|
||||
|
||||
public int getSize() {
|
||||
return 1 // flags
|
||||
+ getAdditionalInfoSize();
|
||||
}
|
||||
|
||||
public boolean equals(Object obj) {
|
||||
if ( (obj == null) || !(obj instanceof DeliveryInstructions))
|
||||
return false;
|
||||
|
||||
@@ -155,13 +155,35 @@ public class GarlicClove extends DataStructureImpl {
|
||||
if (_log.shouldLog(Log.DEBUG))
|
||||
_log.debug("Written cert: " + _certificate);
|
||||
}
|
||||
|
||||
public byte[] toByteArray() {
|
||||
byte rv[] = new byte[estimateSize()];
|
||||
int offset = 0;
|
||||
offset += _instructions.writeBytes(rv, offset);
|
||||
if (_log.shouldLog(Log.DEBUG))
|
||||
_log.debug("Wrote instructions: " + _instructions);
|
||||
//offset += _msg.toByteArray(rv);
|
||||
try {
|
||||
byte m[] = _msg.toByteArray();
|
||||
System.arraycopy(m, 0, rv, offset, m.length);
|
||||
offset += m.length;
|
||||
} catch (Exception e) { throw new RuntimeException("Unable to write: " + _msg + ": " + e.getMessage()); }
|
||||
DataHelper.toLong(rv, offset, 4, _cloveId);
|
||||
offset += 4;
|
||||
DataHelper.toDate(rv, offset, _expiration.getTime());
|
||||
offset += DataHelper.DATE_LENGTH;
|
||||
offset += _certificate.writeBytes(rv, offset);
|
||||
if (offset != rv.length)
|
||||
_log.log(Log.CRIT, "Clove offset: " + offset + " but estimated length: " + rv.length);
|
||||
return rv;
|
||||
}
|
||||
|
||||
public int estimateSize() {
|
||||
return 64 // instructions (high estimate)
|
||||
return _instructions.getSize()
|
||||
+ _msg.getMessageSize()
|
||||
+ 4 // cloveId
|
||||
+ DataHelper.DATE_LENGTH
|
||||
+ 4; // certificate
|
||||
+ _certificate.size(); // certificate
|
||||
}
|
||||
|
||||
public boolean equals(Object obj) {
|
||||
|
||||
@@ -10,6 +10,7 @@ package net.i2p.data.i2np;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InterruptedIOException;
|
||||
|
||||
import net.i2p.router.RouterContext;
|
||||
import net.i2p.util.I2PThread;
|
||||
@@ -133,6 +134,13 @@ public class I2NPMessageReader {
|
||||
_listener.readError(I2NPMessageReader.this, ime);
|
||||
_listener.disconnected(I2NPMessageReader.this);
|
||||
cancelRunner();
|
||||
} catch (InterruptedIOException iioe) {
|
||||
// not all I2NPMessageReaders support this, but some run off sockets which throw
|
||||
// SocketTimeoutExceptions or InterruptedIOExceptions
|
||||
if (_log.shouldLog(Log.INFO))
|
||||
_log.info("Disconnecting due to inactivity", iioe);
|
||||
_listener.disconnected(I2NPMessageReader.this);
|
||||
cancelRunner();
|
||||
} catch (IOException ioe) {
|
||||
if (_log.shouldLog(Log.WARN))
|
||||
_log.warn("IO Error handling message", ioe);
|
||||
|
||||
Reference in New Issue
Block a user