I2NP: Fix TunnelGatewayMessage.calculateWrittenLength()

so it doesn't null out the contained message.
Required for SSU2.
Unrelated TunnelDispatcher cleanup done while trying to find the problem.
This commit is contained in:
zzz
2022-03-08 05:39:47 -05:00
parent eb72e97c03
commit a7a5b06b5c
4 changed files with 44 additions and 21 deletions

View File

@ -1,3 +1,11 @@
2022-03-08 zzz
* I2NP: Fix TunnelGatewayMessage.calculateWrittenLength()
* SSU2: Fixes
2022-03-06 zzz
* Console: Improve class selection on /configlogging
* SSU2: Fixes
2022-03-04 zzz
* Console: Add SSU version column to /peers
* i2psnark: Add js theme preview

View File

@ -50,6 +50,8 @@ public class TunnelGatewayMessage extends FastI2NPMessageImpl {
* this will be an UnknownI2NPMessage.
* If you need a real message class, use UnknownI2NPMessage.convert().
*
* Warning, will be null after message has been written.
*
* Note that if you change the expiration on the embedded message it will
* mess up the checksum of this message, so don't do that.
*/
@ -67,13 +69,16 @@ public class TunnelGatewayMessage extends FastI2NPMessageImpl {
}
protected int calculateWrittenLength() {
int rv = 4 + 2;
synchronized (this) {
if (_msgData == null) {
_msgData = _msg.toByteArray();
_msg = null;
}
if (_msg != null)
rv += _msg.getMessageSize();
else if (_msgData != null)
rv += _msgData.length;
else
throw new IllegalStateException();
}
return _msgData.length + 4 + 2;
return rv;
}
/** write the message body to the output array, starting at the given index */
@ -180,6 +185,7 @@ public class TunnelGatewayMessage extends FastI2NPMessageImpl {
public String toString() {
StringBuilder buf = new StringBuilder();
buf.append("[TunnelGatewayMessage:");
buf.append(" MessageId: ").append(getUniqueId());
buf.append(" Tunnel ID: ").append(getTunnelId());
buf.append(" Message: ").append(_msg);
buf.append("]");

View File

@ -18,7 +18,7 @@ public class RouterVersion {
/** deprecated */
public final static String ID = "Git";
public final static String VERSION = CoreVersion.VERSION;
public final static long BUILD = 4;
public final static long BUILD = 5;
/** for example "-test" */
public final static String EXTRA = "";

View File

@ -574,39 +574,48 @@ public class TunnelDispatcher implements Service {
*/
public void dispatch(TunnelGatewayMessage msg) {
long before = _context.clock().now();
TunnelGateway gw = _inboundGateways.get(msg.getTunnelId());
TunnelId id = msg.getTunnelId();
TunnelGateway gw = _inboundGateways.get(id);
I2NPMessage submsg = msg.getMessage();
// The contained message is nulled out when written
if (submsg == null)
throw new IllegalArgumentException("TGM message is null");
if (gw != null) {
if (_log.shouldLog(Log.DEBUG))
_log.debug("dispatch where we are the inbound gateway: " + gw + ": " + msg);
long minTime = before - Router.CLOCK_FUDGE_FACTOR;
long maxTime = before + MAX_FUTURE_EXPIRATION;
if ( (msg.getMessageExpiration() < minTime) || (msg.getMessage().getMessageExpiration() < minTime) ||
(msg.getMessageExpiration() > maxTime) || (msg.getMessage().getMessageExpiration() > maxTime) ) {
long exp = msg.getMessageExpiration();
long subexp = submsg.getMessageExpiration();
if (exp < minTime || subexp < minTime ||
exp > maxTime || subexp > maxTime) {
if (_log.shouldLog(Log.WARN))
_log.warn("Not dispatching a gateway message for tunnel " + msg.getTunnelId().getTunnelId()
+ " as the wrapper's expiration is in " + DataHelper.formatDuration(msg.getMessageExpiration()-before)
+ " and/or the content's expiration is in " + DataHelper.formatDuration(msg.getMessage().getMessageExpiration()-before)
+ " with messageId " + msg.getUniqueId() + "/" + msg.getMessage().getUniqueId() + " and message type "
+ msg.getMessage().getClass().getSimpleName());
_log.warn("Not dispatching a gateway message for tunnel " + id.getTunnelId()
+ " as the wrapper's expiration is in " + DataHelper.formatDuration(exp - before)
+ " and/or the content's expiration is in " + DataHelper.formatDuration(subexp - before)
+ " with messageId " + id + "/" + submsg.getUniqueId()
+ " messageType: " + submsg.getClass().getSimpleName());
return;
}
//_context.messageHistory().tunnelDispatched("message " + msg.getUniqueId() + "/" + msg.getMessage().getUniqueId() + " on tunnel "
// + msg.getTunnelId().getTunnelId() + " as inbound gateway");
_context.messageHistory().tunnelDispatched(msg.getUniqueId(), msg.getMessage().getUniqueId(), msg.getTunnelId().getTunnelId(), "inbound gateway");
_context.messageHistory().tunnelDispatched(msg.getUniqueId(),
submsg.getUniqueId(),
id.getTunnelId(), "inbound gateway");
gw.add(msg);
_context.statManager().addRateData("tunnel.dispatchInbound", 1);
} else {
_context.messageHistory().droppedTunnelGatewayMessageUnknown(msg.getUniqueId(), msg.getTunnelId().getTunnelId());
_context.messageHistory().droppedTunnelGatewayMessageUnknown(msg.getUniqueId(), id.getTunnelId());
int level = (_context.router().getUptime() > 10*60*1000 ? Log.WARN : Log.INFO);
if (_log.shouldLog(level))
_log.log(level, "no matching tunnel for id=" + msg.getTunnelId().getTunnelId()
_log.log(level, "no matching tunnel for id=" + id.getTunnelId()
+ ": gateway message expiring in "
+ DataHelper.formatDuration(msg.getMessageExpiration()-_context.clock().now())
+ DataHelper.formatDuration(msg.getMessageExpiration() - before)
+ "/"
+ DataHelper.formatDuration(msg.getMessage().getMessageExpiration()-_context.clock().now())
+ " messageId " + msg.getUniqueId()
+ DataHelper.formatDuration(submsg.getMessageExpiration() - before)
+ " messageId " + id
+ "/" + msg.getMessage().getUniqueId()
+ " messageType: " + msg.getMessage().getClass().getSimpleName()
+ " messageType: " + submsg.getClass().getSimpleName()
+ " existing = " + _inboundGateways.size());
}