minor refactoring, javadoc

dont add an arbitrary extra Router.CLOCK_FUDGE_FACTOR to the expiration
This commit is contained in:
jrandom
2004-07-02 18:57:42 +00:00
committed by zzz
parent 219a704ee0
commit 4b8ac81669

View File

@@ -97,7 +97,33 @@ public class SendTunnelMessageJob extends JobImpl {
_log.error("Someone br0ke us. where is this message supposed to go again?",
getAddedBy());
return;
} else {
forwardToGateway();
return;
}
}
info.messageProcessed();
if (isEndpoint(info)) {
if (_log.shouldLog(Log.INFO))
_log.info("Tunnel message where we're both the gateway and the endpoint - honor instructions");
honorInstructions(info);
return;
} else if (isGateway(info)) {
handleAsGateway(info);
return;
} else {
handleAsParticipant(info);
return;
}
}
/**
* Forward this job's message to the gateway of the tunnel requested
*
*/
private void forwardToGateway() {
TunnelMessage msg = new TunnelMessage(_context);
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
@@ -124,22 +150,11 @@ public class SendTunnelMessageJob extends JobImpl {
return;
}
info.messageProcessed();
if (isEndpoint(info)) {
if (_log.shouldLog(Log.INFO))
_log.info("Tunnel message where we're both the gateway and the endpoint - honor instructions");
honorInstructions(info);
return;
} else if (isGateway(info)) {
handleAsGateway(info);
return;
} else {
handleAsParticipant(info);
return;
}
}
/**
* We are the gateway for the tunnel this message is bound to,
* so wrap it accordingly and send it on its way.
*
*/
private void handleAsGateway(TunnelInfo info) {
// since we are the gateway, we don't need to verify the data structures
TunnelInfo us = getUs(info);
@@ -176,6 +191,11 @@ public class SendTunnelMessageJob extends JobImpl {
}
}
/**
* We are the participant in the tunnel, so verify the signature / data and
* forward it to the next hop.
*
*/
private void handleAsParticipant(TunnelInfo info) {
// SendTunnelMessageJob shouldn't be used for participants!
if (_log.shouldLog(Log.DEBUG))
@@ -251,6 +271,11 @@ public class SendTunnelMessageJob extends JobImpl {
return (us.getSigningKey() != null); // only the gateway can sign
}
/**
* Build the tunnel message with appropriate instructions for the
* tunnel endpoint, then encrypt and sign it.
*
*/
private TunnelMessage prepareMessage(TunnelInfo info) {
TunnelMessage msg = new TunnelMessage(_context);
@@ -316,6 +341,10 @@ public class SendTunnelMessageJob extends JobImpl {
return msg;
}
/**
* Create and sign the verification structure, using the tunnel's signing key
*
*/
private TunnelVerificationStructure createVerificationStructure(byte encryptedMessage[], TunnelInfo info) {
TunnelVerificationStructure struct = new TunnelVerificationStructure();
struct.setMessageHash(_context.sha().calculateHash(encryptedMessage));
@@ -323,6 +352,11 @@ public class SendTunnelMessageJob extends JobImpl {
return struct;
}
/**
* encrypt the structure (the message or instructions)
*
* @param paddedSize minimum size to pad to
*/
private byte[] encrypt(DataStructure struct, SessionKey key, int paddedSize) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream(paddedSize);
@@ -342,6 +376,12 @@ public class SendTunnelMessageJob extends JobImpl {
return null;
}
/**
* We are both the endpoint and gateway for the tunnel, so honor
* what was requested of us (processing the message locally,
* forwarding to a router, forwarding to a tunnel, etc)
*
*/
private void honorInstructions(TunnelInfo info) {
if (_selector != null)
createFakeOutNetMessage();
@@ -357,6 +397,18 @@ public class SendTunnelMessageJob extends JobImpl {
RouterIdentity ident = _context.router().getRouterInfo().getIdentity();
if (_destRouter != null) {
honorSendRemote(info, ident);
} else {
honorSendLocal(info, ident);
}
}
/**
* We are the gateway and endpoint and we have been asked to forward the
* message to a remote location (either a tunnel or a router).
*
*/
private void honorSendRemote(TunnelInfo info, RouterIdentity ident) {
I2NPMessage msg = null;
if (_targetTunnelId != null) {
if (_log.shouldLog(Log.DEBUG))
@@ -388,13 +440,13 @@ public class SendTunnelMessageJob extends JobImpl {
}
long now = _context.clock().now();
//if (_expiration < now) {
_expiration = now + Router.CLOCK_FUDGE_FACTOR;
//_expiration = now + Router.CLOCK_FUDGE_FACTOR;
//_log.info("Fudging the message send so it expires in the fudge factor...");
//}
if (_expiration - 30*1000 < now) {
if (_log.shouldLog(Log.ERROR))
_log.error("Why are we trying to send a " + _message.getClass().getName()
if (_expiration - 10*1000 < now) {
if (_log.shouldLog(Log.WARN))
_log.warn("Why are we trying to send a " + _message.getClass().getName()
+ " message with " + (_expiration-now) + "ms left?", getAddedBy());
}
@@ -406,7 +458,15 @@ public class SendTunnelMessageJob extends JobImpl {
_context.jobQueue().addJob(new SendMessageDirectJob(_context, msg, _destRouter,
_onSend, _onReply, _onFailure,
null, _expiration, _priority));
} else {
}
/**
* We are the gateway and endpoint, and the instructions say to forward the
* message to, uh, us. The message may be a normal network message or they
* may be a client DataMessage.
*
*/
private void honorSendLocal(TunnelInfo info, RouterIdentity ident) {
if ( (info.getDestination() == null) || !(_message instanceof DataMessage) ) {
// its a network message targeting us...
if (_log.shouldLog(Log.DEBUG))
@@ -445,7 +505,6 @@ public class SendTunnelMessageJob extends JobImpl {
_context.messageHistory().receivePayloadMessage(msg.getUniqueId());
}
}
}
private void createFakeOutNetMessage() {
// now we create a fake outNetMessage to go onto the registry so we can select