use of net.i2p.data.ByteArray in project i2p.i2p by i2p.
the class FragmentedMessage method writeComplete.
/**
**
* public void writeComplete(OutputStream out) throws IOException {
* if (_releasedAfter > 0) {
* RuntimeException e = new RuntimeException("use after free in FragmentedMessage");
* _log.error("FM writeComplete()", e);
* throw e;
* }
* for (int i = 0; i <= _highFragmentNum; i++) {
* ByteArray ba = _fragments[i];
* out.write(ba.getData(), ba.getOffset(), ba.getValid());
* }
* _completed = true;
* }
***
*/
/**
*/
private void writeComplete(byte[] target, int offset) {
if (_releasedAfter > 0) {
RuntimeException e = new RuntimeException("use after free in FragmentedMessage");
_log.error("FM writeComplete() 2", e);
throw e;
}
for (int i = 0; i <= _highFragmentNum; i++) {
ByteArray ba = _fragments[i];
System.arraycopy(ba.getData(), ba.getOffset(), target, offset, ba.getValid());
offset += ba.getValid();
}
_completed = true;
}
use of net.i2p.data.ByteArray in project i2p.i2p by i2p.
the class FragmentedMessage method toString.
/**
**
* public InputStream getInputStream() { return new FragmentInputStream(); }
* private class FragmentInputStream extends InputStream {
* private int _fragment;
* private int _offset;
* public FragmentInputStream() {
* _fragment = 0;
* _offset = 0;
* }
* public int read() throws IOException {
* while (true) {
* ByteArray ba = _fragments[_fragment];
* if (ba == null) return -1;
* if (_offset >= ba.getValid()) {
* _fragment++;
* _offset = 0;
* } else {
* byte rv = ba.getData()[ba.getOffset()+_offset];
* _offset++;
* return rv;
* }
* }
* }
* }
***
*/
/**
* toString
*/
@Override
public String toString() {
StringBuilder buf = new StringBuilder(128);
buf.append("Fragments for ").append(_messageId).append(": ");
for (int i = 0; i <= _highFragmentNum; i++) {
ByteArray ba = _fragments[i];
if (ba != null)
buf.append(i).append(":").append(ba.getValid()).append(" bytes ");
else
buf.append(i).append(":missing ");
}
buf.append(" highest received: ").append(_highFragmentNum);
buf.append(" last received? ").append(_lastReceived);
buf.append(" lifetime: ").append(DataHelper.formatDuration(_context.clock().now() - _createdOn));
if (_toRouter != null) {
buf.append(" targetting ").append(_toRouter.toBase64().substring(0, 4));
if (_toTunnel != null)
buf.append(":").append(_toTunnel.getTunnelId());
}
if (_completed)
buf.append(" completed");
if (_releasedAfter > 0)
buf.append(" released after " + DataHelper.formatDuration(_releasedAfter));
return buf.toString();
}
use of net.i2p.data.ByteArray in project i2p.i2p by i2p.
the class FragmentedMessage method receive.
/**
* Receive a followup fragment, though one of these may arrive at the endpoint
* prior to the fragment # 0.
*
* @param fragmentNum sequence number within the message (1 - 63)
* @param payload data for the fragment non-null
* @param offset index into the payload where the fragment data starts (past headers/etc)
* @param length how much past the offset should we snag?
* @param isLast is this the last fragment in the message?
*/
public boolean receive(int fragmentNum, byte[] payload, int offset, int length, boolean isLast) {
if (fragmentNum <= 0 || fragmentNum >= MAX_FRAGMENTS) {
if (_log.shouldLog(Log.WARN))
_log.warn("Bad followon fragment # == " + fragmentNum + " for messageId " + _messageId);
return false;
}
if (length <= 0 || length > MAX_FRAGMENT_SIZE) {
if (_log.shouldLog(Log.WARN))
_log.warn("Length is impossible (" + length + ") for messageId " + _messageId);
return false;
}
if (offset + length > payload.length) {
if (_log.shouldLog(Log.WARN))
_log.warn("Length is impossible (" + length + "/" + offset + " out of " + payload.length + ") for messageId " + _messageId);
return false;
}
if (_log.shouldLog(Log.DEBUG))
_log.debug("Receive message " + _messageId + " fragment " + fragmentNum + " with " + length + " bytes (last? " + isLast + ") offset = " + offset);
// we should just use payload[] and use an offset/length on it
// new ByteArray(payload, offset, length); //new byte[length]);
ByteArray ba = _cache.acquire();
System.arraycopy(payload, offset, ba.getData(), 0, length);
ba.setValid(length);
ba.setOffset(0);
// System.arraycopy(payload, offset, ba.getData(), 0, length);
// if (_log.shouldLog(Log.DEBUG))
// _log.debug("fragment[" + fragmentNum + "/" + offset + "/" + length + "]: "
// + Base64.encode(ba.getData(), ba.getOffset(), ba.getValid()));
_fragments[fragmentNum] = ba;
_lastReceived = _lastReceived || isLast;
if (fragmentNum > _highFragmentNum)
_highFragmentNum = fragmentNum;
if (isLast && fragmentNum <= 0) {
if (_log.shouldLog(Log.ERROR))
_log.error("hmm, isLast and fragmentNum=" + fragmentNum + " for message " + _messageId);
return false;
}
return true;
}
use of net.i2p.data.ByteArray in project i2p.i2p by i2p.
the class HashSetIVValidator method receiveIV.
public boolean receiveIV(byte[] ivData, int ivOffset, byte[] payload, int payloadOffset) {
// if (true) // foo!
// return true;
byte[] iv = new byte[HopProcessor.IV_LENGTH];
DataHelper.xor(ivData, ivOffset, payload, payloadOffset, iv, 0, HopProcessor.IV_LENGTH);
ByteArray ba = new ByteArray(iv);
boolean isNew = _received.add(ba);
return isNew;
}
use of net.i2p.data.ByteArray in project i2p.i2p by i2p.
the class ConnectionManager method ping.
/**
* blocking
*
* @param timeoutMs greater than zero
* @param payload non-null, include in packet, up to 32 bytes may be returned in pong
* not copied, do not modify
* @return the payload received in the pong, zero-length if none, null on failure or timeout
* @since 0.9.18
*/
public byte[] ping(Destination peer, int fromPort, int toPort, long timeoutMs, byte[] payload) {
PingRequest req = new PingRequest(null);
long id = assignPingId(req);
PacketLocal packet = new PacketLocal(_context, peer, _session);
packet.setSendStreamId(id);
packet.setFlag(Packet.FLAG_ECHO | Packet.FLAG_NO_ACK | Packet.FLAG_SIGNATURE_INCLUDED);
packet.setOptionalFrom();
packet.setLocalPort(fromPort);
packet.setRemotePort(toPort);
packet.setPayload(new ByteArray(payload));
if (timeoutMs > MAX_PING_TIMEOUT)
timeoutMs = MAX_PING_TIMEOUT;
if (_log.shouldLog(Log.INFO)) {
_log.info(String.format("about to ping %s port %d from port %d timeout=%d payload=%d", peer.calculateHash().toString(), toPort, fromPort, timeoutMs, payload.length));
}
_outboundQueue.enqueue(packet);
packet.releasePayload();
synchronized (req) {
if (!req.pongReceived())
try {
req.wait(timeoutMs);
} catch (InterruptedException ie) {
}
}
_pendingPings.remove(id);
boolean ok = req.pongReceived();
if (!ok)
return null;
ByteArray ba = req.getPayload();
if (ba == null)
return new byte[0];
byte[] rv = new byte[ba.getValid()];
System.arraycopy(ba, ba.getOffset(), rv, 0, ba.getValid());
return rv;
}
Aggregations