Search in sources :

Example 36 with ByteArray

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;
}
Also used : ByteArray(net.i2p.data.ByteArray)

Example 37 with ByteArray

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();
}
Also used : ByteArray(net.i2p.data.ByteArray)

Example 38 with ByteArray

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;
}
Also used : ByteArray(net.i2p.data.ByteArray)

Example 39 with ByteArray

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;
}
Also used : ByteArray(net.i2p.data.ByteArray)

Example 40 with ByteArray

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;
}
Also used : ByteArray(net.i2p.data.ByteArray)

Aggregations

ByteArray (net.i2p.data.ByteArray)53 Test (org.junit.Test)14 IOException (java.io.IOException)9 ArrayList (java.util.ArrayList)3 Destination (net.i2p.data.Destination)3 InterruptedIOException (java.io.InterruptedIOException)2 SessionKey (net.i2p.data.SessionKey)2 I2NPMessage (net.i2p.data.i2np.I2NPMessage)2 I2NPMessageException (net.i2p.data.i2np.I2NPMessageException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InetAddress (java.net.InetAddress)1 ByteBuffer (java.nio.ByteBuffer)1 CancelledKeyException (java.nio.channels.CancelledKeyException)1 NotYetConnectedException (java.nio.channels.NotYetConnectedException)1 SelectionKey (java.nio.channels.SelectionKey)1 ServerSocketChannel (java.nio.channels.ServerSocketChannel)1 SocketChannel (java.nio.channels.SocketChannel)1 MessageDigest (java.security.MessageDigest)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 I2PException (net.i2p.I2PException)1