2005-02-16 22:37:24 +00:00
|
|
|
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 net.i2p.I2PAppContext;
|
|
|
|
import net.i2p.data.ByteArray;
|
|
|
|
import net.i2p.data.DataHelper;
|
|
|
|
import net.i2p.data.TunnelId;
|
|
|
|
import net.i2p.util.ByteCache;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Defines the message sent between routers as part of the tunnel delivery
|
|
|
|
*
|
2011-12-11 21:04:43 +00:00
|
|
|
* The tunnel ID is changed in-place by TunnelParticipant.send(), so
|
|
|
|
* we can't reuse the checksum on output, but we still subclass
|
|
|
|
* FastI2NPMessageImpl so we don't verify the checksum on input...
|
|
|
|
* because this is a high-usage class.
|
|
|
|
*
|
2005-02-16 22:37:24 +00:00
|
|
|
*/
|
2011-12-11 21:04:43 +00:00
|
|
|
public class TunnelDataMessage extends FastI2NPMessageImpl {
|
2005-07-05 22:08:56 +00:00
|
|
|
private long _tunnelId;
|
|
|
|
private TunnelId _tunnelIdObj;
|
2005-02-16 22:37:24 +00:00
|
|
|
private byte[] _data;
|
2005-07-09 22:58:22 +00:00
|
|
|
private ByteArray _dataBuf;
|
2005-02-16 22:37:24 +00:00
|
|
|
|
|
|
|
public final static int MESSAGE_TYPE = 18;
|
2011-12-11 21:04:43 +00:00
|
|
|
public static final int DATA_SIZE = 1024;
|
2005-02-16 22:37:24 +00:00
|
|
|
/** if we can't deliver a tunnel message in 10s, fuck it */
|
|
|
|
private static final int EXPIRATION_PERIOD = 10*1000;
|
|
|
|
|
2009-12-26 20:12:43 +00:00
|
|
|
private static final ByteCache _cache;
|
2005-02-16 22:37:24 +00:00
|
|
|
/**
|
|
|
|
* When true, it means this tunnelDataMessage is being used as part of a tunnel
|
|
|
|
* processing pipeline, where the byte array is acquired during the TunnelDataMessage's
|
|
|
|
* creation (per readMessage), held onto through several transitions (updating and
|
|
|
|
* moving that array between different TunnelDataMessage instances or the fragment
|
|
|
|
* handler's cache, etc), until it is finally released back into the cache when written
|
|
|
|
* to the next peer (or explicitly by the fragment handler's completion).
|
|
|
|
* Setting this to false just increases memory churn
|
2009-12-26 20:12:43 +00:00
|
|
|
*
|
|
|
|
* Well, this is tricky to get right and avoid data corruption,
|
|
|
|
* here's an example after checks were put in:
|
|
|
|
*
|
|
|
|
*
|
|
|
|
10:57:05.197 CRIT [NTCP read 1 ] 2p.data.i2np.TunnelDataMessage: TDM boom
|
|
|
|
net.i2p.data.i2np.I2NPMessageException: TDM data buf use after free
|
|
|
|
at net.i2p.data.i2np.TunnelDataMessage.writeMessageBody(TunnelDataMessage.java:124)
|
|
|
|
at net.i2p.data.i2np.I2NPMessageImpl.toByteArray(I2NPMessageImpl.java:217)
|
|
|
|
at net.i2p.router.transport.ntcp.NTCPConnection.bufferedPrepare(NTCPConnection.java:678)
|
|
|
|
at net.i2p.router.transport.ntcp.NTCPConnection.send(NTCPConnection.java:293)
|
|
|
|
at net.i2p.router.transport.ntcp.NTCPTransport.outboundMessageReady(NTCPTransport.java:185)
|
|
|
|
at net.i2p.router.transport.TransportImpl.send(TransportImpl.java:357)
|
|
|
|
at net.i2p.router.transport.GetBidsJob.getBids(GetBidsJob.java:80)
|
|
|
|
at net.i2p.router.transport.CommSystemFacadeImpl.processMessage(CommSystemFacadeImpl.java:129)
|
|
|
|
at net.i2p.router.OutNetMessagePool.add(OutNetMessagePool.java:61)
|
|
|
|
at net.i2p.router.transport.TransportImpl.afterSend(TransportImpl.java:252)
|
|
|
|
at net.i2p.router.transport.TransportImpl.afterSend(TransportImpl.java:163)
|
|
|
|
at net.i2p.router.transport.udp.UDPTransport.failed(UDPTransport.java:1314)
|
|
|
|
at net.i2p.router.transport.udp.PeerState.add(PeerState.java:1064)
|
|
|
|
at net.i2p.router.transport.udp.OutboundMessageFragments.add(OutboundMessageFragments.java:146)
|
|
|
|
at net.i2p.router.transport.udp.UDPTransport.send(UDPTransport.java:1098)
|
|
|
|
at net.i2p.router.transport.GetBidsJob.getBids(GetBidsJob.java:80)
|
|
|
|
at net.i2p.router.transport.CommSystemFacadeImpl.processMessage(CommSystemFacadeImpl.java:129)
|
|
|
|
at net.i2p.router.OutNetMessagePool.add(OutNetMessagePool.java:61)
|
|
|
|
at net.i2p.router.tunnel.TunnelParticipant.send(TunnelParticipant.java:172)
|
|
|
|
at net.i2p.router.tunnel.TunnelParticipant.dispatch(TunnelParticipant.java:86)
|
|
|
|
at net.i2p.router.tunnel.TunnelDispatcher.dispatch(TunnelDispatcher.java:351)
|
|
|
|
at net.i2p.router.InNetMessagePool.doShortCircuitTunnelData(InNetMessagePool.java:306)
|
|
|
|
at net.i2p.router.InNetMessagePool.shortCircuitTunnelData(InNetMessagePool.java:291)
|
|
|
|
at net.i2p.router.InNetMessagePool.add(InNetMessagePool.java:160)
|
|
|
|
at net.i2p.router.transport.TransportManager.messageReceived(TransportManager.java:462)
|
|
|
|
at net.i2p.router.transport.TransportImpl.messageReceived(TransportImpl.java:416)
|
|
|
|
at net.i2p.router.transport.ntcp.NTCPConnection$ReadState.receiveLastBlock(NTCPConnection.java:1285)
|
|
|
|
at net.i2p.router.transport.ntcp.NTCPConnection$ReadState.receiveSubsequent(NTCPConnection.java:1248)
|
|
|
|
at net.i2p.router.transport.ntcp.NTCPConnection$ReadState.receiveBlock(NTCPConnection.java:1205)
|
|
|
|
at net.i2p.router.transport.ntcp.NTCPConnection.recvUnencryptedI2NP(NTCPConnection.java:1035)
|
|
|
|
at net.i2p.router.transport.ntcp.NTCPConnection.recvEncryptedI2NP(NTCPConnection.java:1018)
|
|
|
|
at net.i2p.router.transport.ntcp.Reader.processRead(Reader.java:167)
|
|
|
|
at net.i2p.router.transport.ntcp.Reader.access$400(Reader.java:17)
|
|
|
|
at net.i2p.router.transport.ntcp.Reader$Runner.run(Reader.java:106)
|
|
|
|
at java.lang.Thread.run(Thread.java:619)
|
|
|
|
at net.i2p.util.I2PThread.run(I2PThread.java:71)
|
|
|
|
*
|
2005-02-16 22:37:24 +00:00
|
|
|
*/
|
|
|
|
private static final boolean PIPELINED_CACHE = true;
|
|
|
|
|
2009-12-26 20:12:43 +00:00
|
|
|
static {
|
|
|
|
if (PIPELINED_CACHE)
|
|
|
|
_cache = ByteCache.getInstance(512, DATA_SIZE);
|
|
|
|
else
|
|
|
|
_cache = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** For use-after-free checks. Always false if PIPELINED_CACHE is false. */
|
|
|
|
private boolean _hadCache;
|
|
|
|
|
2005-02-16 22:37:24 +00:00
|
|
|
public TunnelDataMessage(I2PAppContext context) {
|
|
|
|
super(context);
|
|
|
|
setMessageExpiration(context.clock().now() + EXPIRATION_PERIOD);
|
|
|
|
}
|
|
|
|
|
2005-07-05 22:08:56 +00:00
|
|
|
public long getTunnelId() { return _tunnelId; }
|
2011-12-11 21:04:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* (correctly) Invalidates stored checksum
|
|
|
|
*/
|
|
|
|
public void setTunnelId(long id) {
|
|
|
|
_hasChecksum = false;
|
|
|
|
_tunnelId = id;
|
|
|
|
}
|
|
|
|
|
2005-07-05 22:08:56 +00:00
|
|
|
public TunnelId getTunnelIdObj() {
|
|
|
|
if (_tunnelIdObj == null)
|
|
|
|
_tunnelIdObj = new TunnelId(_tunnelId); // not thread safe, but immutable, so who cares
|
|
|
|
return _tunnelIdObj;
|
|
|
|
}
|
2011-12-11 21:04:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* (correctly) Invalidates stored checksum
|
|
|
|
*/
|
2005-07-05 22:08:56 +00:00
|
|
|
public void setTunnelId(TunnelId id) {
|
2011-12-11 21:04:43 +00:00
|
|
|
_hasChecksum = false;
|
2005-07-05 22:08:56 +00:00
|
|
|
_tunnelIdObj = id;
|
|
|
|
_tunnelId = id.getTunnelId();
|
|
|
|
}
|
2005-02-16 22:37:24 +00:00
|
|
|
|
2009-12-26 20:12:43 +00:00
|
|
|
public byte[] getData() {
|
|
|
|
if (_hadCache && _dataBuf == null) {
|
|
|
|
RuntimeException e = new RuntimeException("TDM data buf use after free");
|
|
|
|
_log.error("TDM boom", e);
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
return _data;
|
|
|
|
}
|
|
|
|
|
2011-12-11 21:04:43 +00:00
|
|
|
/**
|
|
|
|
* @throws IllegalStateException if data previously set, to protect saved checksum
|
|
|
|
*/
|
2005-02-16 22:37:24 +00:00
|
|
|
public void setData(byte data[]) {
|
2011-12-11 21:04:43 +00:00
|
|
|
if (_data != null)
|
|
|
|
throw new IllegalStateException();
|
2005-02-16 22:37:24 +00:00
|
|
|
if ( (data == null) || (data.length <= 0) )
|
|
|
|
throw new IllegalArgumentException("Empty tunnel payload?");
|
|
|
|
_data = data;
|
|
|
|
}
|
|
|
|
|
2011-12-11 21:04:43 +00:00
|
|
|
public void readMessage(byte data[], int offset, int dataSize, int type) throws I2NPMessageException {
|
2005-02-16 22:37:24 +00:00
|
|
|
if (type != MESSAGE_TYPE) throw new I2NPMessageException("Message type is incorrect for this message");
|
|
|
|
int curIndex = offset;
|
|
|
|
|
2005-07-05 22:08:56 +00:00
|
|
|
_tunnelId = DataHelper.fromLong(data, curIndex, 4);
|
2005-02-16 22:37:24 +00:00
|
|
|
curIndex += 4;
|
|
|
|
|
2005-07-05 22:08:56 +00:00
|
|
|
if (_tunnelId <= 0)
|
2005-02-16 22:37:24 +00:00
|
|
|
throw new I2NPMessageException("Invalid tunnel Id " + _tunnelId);
|
|
|
|
|
|
|
|
// we cant cache it in trivial form, as other components (e.g. HopProcessor)
|
|
|
|
// call getData() and use it as the buffer to write with. it is then used
|
|
|
|
// again to pass to the 'receiver', which may even cache it in a FragmentMessage.
|
2005-07-09 22:58:22 +00:00
|
|
|
if (PIPELINED_CACHE) {
|
|
|
|
_dataBuf = _cache.acquire();
|
|
|
|
_data = _dataBuf.getData();
|
2009-12-26 20:12:43 +00:00
|
|
|
_hadCache = true;
|
2005-07-09 22:58:22 +00:00
|
|
|
} else {
|
2005-02-16 22:37:24 +00:00
|
|
|
_data = new byte[DATA_SIZE];
|
2005-07-09 22:58:22 +00:00
|
|
|
}
|
2005-02-16 22:37:24 +00:00
|
|
|
System.arraycopy(data, curIndex, _data, 0, DATA_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** calculate the message body's length (not including the header and footer */
|
|
|
|
protected int calculateWrittenLength() { return 4 + DATA_SIZE; }
|
|
|
|
/** write the message body to the output array, starting at the given index */
|
|
|
|
protected int writeMessageBody(byte out[], int curIndex) throws I2NPMessageException {
|
2005-07-05 22:08:56 +00:00
|
|
|
if ( (_tunnelId <= 0) || (_data == null) )
|
2011-01-07 00:15:35 +00:00
|
|
|
throw new I2NPMessageException("Not enough data to write out (id=" + _tunnelId + ")");
|
2005-02-16 22:37:24 +00:00
|
|
|
if (_data.length <= 0)
|
|
|
|
throw new I2NPMessageException("Not enough data to write out (data.length=" + _data.length + ")");
|
|
|
|
|
2009-12-26 20:12:43 +00:00
|
|
|
if (_hadCache && _dataBuf == null) {
|
|
|
|
I2NPMessageException e = new I2NPMessageException("TDM data buf use after free");
|
|
|
|
_log.error("TDM boom", e);
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
|
2005-07-05 22:08:56 +00:00
|
|
|
DataHelper.toLong(out, curIndex, 4, _tunnelId);
|
2005-02-16 22:37:24 +00:00
|
|
|
curIndex += 4;
|
|
|
|
System.arraycopy(_data, 0, out, curIndex, DATA_SIZE);
|
|
|
|
curIndex += _data.length;
|
2009-12-26 20:12:43 +00:00
|
|
|
|
|
|
|
// We can use from the cache, we just can't release to the cache, due to the bug
|
|
|
|
// noted above. In effect, this means that transmitted TDMs don't get their
|
|
|
|
// dataBufs released - but received TDMs do (via FragmentHandler)
|
|
|
|
//if (_hadCache) {
|
|
|
|
// _cache.release(_dataBuf);
|
|
|
|
// _dataBuf = null;
|
|
|
|
//}
|
2005-02-16 22:37:24 +00:00
|
|
|
return curIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getType() { return MESSAGE_TYPE; }
|
|
|
|
|
2009-04-21 03:32:38 +00:00
|
|
|
@Override
|
2005-02-16 22:37:24 +00:00
|
|
|
public int hashCode() {
|
2005-07-05 22:08:56 +00:00
|
|
|
return (int)_tunnelId +
|
2005-02-16 22:37:24 +00:00
|
|
|
DataHelper.hashCode(_data);
|
|
|
|
}
|
|
|
|
|
2009-04-21 03:32:38 +00:00
|
|
|
@Override
|
2005-02-16 22:37:24 +00:00
|
|
|
public boolean equals(Object object) {
|
|
|
|
if ( (object != null) && (object instanceof TunnelDataMessage) ) {
|
|
|
|
TunnelDataMessage msg = (TunnelDataMessage)object;
|
2010-05-10 15:58:53 +00:00
|
|
|
return _tunnelId == msg.getTunnelId() &&
|
2005-02-16 22:37:24 +00:00
|
|
|
DataHelper.eq(getData(),msg.getData());
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-21 03:32:38 +00:00
|
|
|
@Override
|
2005-02-16 22:37:24 +00:00
|
|
|
public byte[] toByteArray() {
|
|
|
|
byte rv[] = super.toByteArray();
|
|
|
|
if (rv == null)
|
|
|
|
throw new RuntimeException("unable to toByteArray(): " + toString());
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2009-04-21 03:32:38 +00:00
|
|
|
@Override
|
2005-02-16 22:37:24 +00:00
|
|
|
public String toString() {
|
2009-07-01 16:00:43 +00:00
|
|
|
StringBuilder buf = new StringBuilder();
|
2005-02-16 22:37:24 +00:00
|
|
|
buf.append("[TunnelDataMessage:");
|
2015-04-11 19:34:34 +00:00
|
|
|
buf.append(" MessageId: ").append(getUniqueId());
|
2011-12-11 21:04:43 +00:00
|
|
|
buf.append(" Tunnel ID: ").append(_tunnelId);
|
2005-02-16 22:37:24 +00:00
|
|
|
buf.append("]");
|
|
|
|
return buf.toString();
|
|
|
|
}
|
|
|
|
}
|