forked from I2P_Developers/i2p.i2p
* I2NP: Add UnknownI2NPMessage so we can route unknown message types
This commit is contained in:
@ -397,10 +397,9 @@ public abstract class I2NPMessageImpl extends DataStructureImpl implements I2NPM
|
||||
default:
|
||||
// unused
|
||||
Builder builder = _builders.get(Integer.valueOf(type));
|
||||
if (builder == null)
|
||||
return null;
|
||||
else
|
||||
if (builder != null)
|
||||
return builder.build(context);
|
||||
return new UnknownI2NPMessage(context, type);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
114
router/java/src/net/i2p/data/i2np/UnknownI2NPMessage.java
Normal file
114
router/java/src/net/i2p/data/i2np/UnknownI2NPMessage.java
Normal file
@ -0,0 +1,114 @@
|
||||
package net.i2p.data.i2np;
|
||||
/*
|
||||
* free (adj.): unencumbered; not under the control of others
|
||||
* Written by jrandom in 2003 and released into the public domain
|
||||
* with no warranty of any kind, either expressed or implied.
|
||||
* It probably won't make your computer catch on fire, or eat
|
||||
* your children, but it might. Use at your own risk.
|
||||
*
|
||||
*/
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import net.i2p.I2PAppContext;
|
||||
import net.i2p.data.DataHelper;
|
||||
|
||||
/**
|
||||
* This is the same as DataMessage but with a variable message type.
|
||||
* This is defined so routers can route messages they don't know about.
|
||||
* We don't extend DataMessage so that any code that does (instanceof DataMessage)
|
||||
* won't return true for this type. Load tests use DataMessage, for example.
|
||||
* See InboundMessageDistributor.
|
||||
*
|
||||
* There is no setData() method, the only way to create one of these is to
|
||||
* read it with readMessage() (i.e., it came from some other router)
|
||||
*
|
||||
* @since 0.7.12
|
||||
*/
|
||||
public class UnknownI2NPMessage extends I2NPMessageImpl {
|
||||
private byte _data[];
|
||||
private int _type;
|
||||
|
||||
/** @param type 0-255 */
|
||||
public UnknownI2NPMessage(I2PAppContext context, int type) {
|
||||
super(context);
|
||||
_type = type;
|
||||
}
|
||||
|
||||
/** warning - only public for equals() */
|
||||
public byte[] getData() {
|
||||
verifyUnwritten();
|
||||
return _data;
|
||||
}
|
||||
|
||||
public void readMessage(byte data[], int offset, int dataSize, int type) throws I2NPMessageException, IOException {
|
||||
if (type != _type) throw new I2NPMessageException("Message type is incorrect for this message");
|
||||
int curIndex = offset;
|
||||
long size = DataHelper.fromLong(data, curIndex, 4);
|
||||
curIndex += 4;
|
||||
if (size > MAX_SIZE)
|
||||
throw new I2NPMessageException("wtf, size=" + size);
|
||||
_data = new byte[(int)size];
|
||||
System.arraycopy(data, curIndex, _data, 0, (int)size);
|
||||
}
|
||||
|
||||
/** calculate the message body's length (not including the header and footer */
|
||||
protected int calculateWrittenLength() {
|
||||
if (_data == null)
|
||||
return 4;
|
||||
else
|
||||
return 4 + _data.length;
|
||||
}
|
||||
|
||||
/** write the message body to the output array, starting at the given index */
|
||||
protected int writeMessageBody(byte out[], int curIndex) {
|
||||
verifyUnwritten();
|
||||
if (_data == null) {
|
||||
out[curIndex++] = 0x0;
|
||||
out[curIndex++] = 0x0;
|
||||
out[curIndex++] = 0x0;
|
||||
out[curIndex++] = 0x0;
|
||||
} else {
|
||||
byte len[] = DataHelper.toLong(4, _data.length);
|
||||
System.arraycopy(len, 0, out, curIndex, 4);
|
||||
curIndex += 4;
|
||||
System.arraycopy(_data, 0, out, curIndex, _data.length);
|
||||
curIndex += _data.length;
|
||||
}
|
||||
return curIndex;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void written() {
|
||||
super.written();
|
||||
_data = null;
|
||||
}
|
||||
|
||||
/** @return 0-255 */
|
||||
public int getType() { return _type; }
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return _type + DataHelper.hashCode(getData());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object object) {
|
||||
if ( (object != null) && (object instanceof UnknownI2NPMessage) ) {
|
||||
UnknownI2NPMessage msg = (UnknownI2NPMessage)object;
|
||||
return _type == msg.getType() && DataHelper.eq(getData(), msg.getData());
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
buf.append("[UnknownI2NPMessage: ");
|
||||
buf.append("\n\tType: ").append(_type);
|
||||
buf.append("\n\tLength: ").append(calculateWrittenLength() - 4);
|
||||
buf.append("]");
|
||||
return buf.toString();
|
||||
}
|
||||
}
|
@ -42,6 +42,9 @@ public class InNetMessagePool implements Service {
|
||||
private boolean _alive;
|
||||
private boolean _dispatchThreaded;
|
||||
|
||||
/** Make this >= the max I2NP message type number (currently 24) */
|
||||
private static final int MAX_I2NP_MESSAGE_TYPE = 31;
|
||||
|
||||
/**
|
||||
* If set to true, we will have two additional threads - one for dispatching
|
||||
* tunnel data messages, and another for dispatching tunnel gateway messages.
|
||||
@ -62,8 +65,7 @@ public class InNetMessagePool implements Service {
|
||||
|
||||
public InNetMessagePool(RouterContext context) {
|
||||
_context = context;
|
||||
// 32 is greater than the max I2NP message type number (currently 22) + 1
|
||||
_handlerJobBuilders = new HandlerJobBuilder[32];
|
||||
_handlerJobBuilders = new HandlerJobBuilder[MAX_I2NP_MESSAGE_TYPE + 1];
|
||||
if (DISPATCH_DIRECT) {
|
||||
// keep the compiler happy since they are final
|
||||
_pendingDataMessages = null;
|
||||
@ -160,6 +162,7 @@ public class InNetMessagePool implements Service {
|
||||
shortCircuitTunnelData(messageBody, fromRouterHash);
|
||||
allowMatches = false;
|
||||
} else {
|
||||
// why don't we allow type 0? There used to be a message of type 0 long ago...
|
||||
if ( (type > 0) && (type < _handlerJobBuilders.length) ) {
|
||||
HandlerJobBuilder builder = _handlerJobBuilders[type];
|
||||
|
||||
|
@ -204,6 +204,7 @@ public class InboundMessageDistributor implements GarlicMessageReceiver.CloveRec
|
||||
return;
|
||||
}
|
||||
case DeliveryInstructions.DELIVERY_MODE_DESTINATION:
|
||||
// Can we route UnknownI2NPMessages to a destination too?
|
||||
if (!(data instanceof DataMessage)) {
|
||||
if (_log.shouldLog(Log.ERROR))
|
||||
_log.error("cant send a " + data.getClass().getName() + " to a destination");
|
||||
|
Reference in New Issue
Block a user