Search in sources :

Example 21 with ByteArray

use of net.i2p.data.ByteArray in project i2p.i2p by i2p.

the class MessageInputStreamTest method testUpdateAcks_inOrderMsgs.

@Test
public void testUpdateAcks_inOrderMsgs() {
    in.messageReceived(0, new ByteArray());
    in.messageReceived(1, new ByteArray());
    in.messageReceived(2, new ByteArray());
    in.updateAcks(packetLocal);
    verify(packetLocal).setAckThrough(2);
    verify(packetLocal).setNacks(null);
}
Also used : ByteArray(net.i2p.data.ByteArray) Test(org.junit.Test)

Example 22 with ByteArray

use of net.i2p.data.ByteArray in project i2p.i2p by i2p.

the class ByteCache method resize.

private void resize(int maxCachedEntries) {
    if (_maxCached >= maxCachedEntries)
        return;
    _maxCached = maxCachedEntries;
    // make a bigger one, move the cached items over
    Queue<ByteArray> newLBQ = new LinkedBlockingQueue<ByteArray>(maxCachedEntries);
    ByteArray ba;
    while ((ba = _available.poll()) != null) newLBQ.offer(ba);
    _available = newLBQ;
}
Also used : ByteArray(net.i2p.data.ByteArray) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue)

Example 23 with ByteArray

use of net.i2p.data.ByteArray in project i2p.i2p by i2p.

the class InboundMessageState method getCompleteSize.

/**
 *  @throws IllegalStateException if released or not isComplete()
 */
public int getCompleteSize() {
    if (_completeSize < 0) {
        if (_lastFragment < 0)
            throw new IllegalStateException("last fragment not set");
        if (_released)
            throw new IllegalStateException("SSU IMS 2 Use after free");
        int size = 0;
        for (int i = 0; i <= _lastFragment; i++) {
            ByteArray frag = _fragments[i];
            if (frag == null)
                throw new IllegalStateException("null fragment " + i + '/' + _lastFragment);
            size += frag.getValid();
        }
        _completeSize = size;
    }
    return _completeSize;
}
Also used : ByteArray(net.i2p.data.ByteArray)

Example 24 with ByteArray

use of net.i2p.data.ByteArray in project i2p.i2p by i2p.

the class MessageReceiver method readMessage.

/**
 *  Assemble all the fragments into an I2NP message.
 *  This calls state.releaseResources(), do not access state after calling this.
 *
 *  @param buf temp buffer for convenience
 *  @return null on error
 */
private I2NPMessage readMessage(ByteArray buf, InboundMessageState state, I2NPMessageHandler handler) {
    try {
        // byte buf[] = new byte[state.getCompleteSize()];
        I2NPMessage m;
        int numFragments = state.getFragmentCount();
        if (numFragments > 1) {
            ByteArray[] fragments = state.getFragments();
            int off = 0;
            for (int i = 0; i < numFragments; i++) {
                System.arraycopy(fragments[i].getData(), 0, buf.getData(), off, fragments[i].getValid());
                // if (_log.shouldLog(Log.DEBUG))
                // _log.debug("Raw fragment[" + i + "] for " + state.getMessageId() + ": "
                // + Base64.encode(fragments[i].getData(), 0, fragments[i].getValid())
                // + " (valid: " + fragments[i].getValid()
                // + " raw: " + Base64.encode(fragments[i].getData()) + ")");
                off += fragments[i].getValid();
            }
            if (off != state.getCompleteSize()) {
                if (_log.shouldLog(Log.WARN))
                    _log.warn("Hmm, offset of the fragments = " + off + " while the state says " + state.getCompleteSize());
                return null;
            }
            // if (_log.shouldLog(Log.DEBUG))
            // _log.debug("Raw byte array for " + state.getMessageId() + ": " + HexDump.dump(buf.getData(), 0, state.getCompleteSize()));
            m = I2NPMessageImpl.fromRawByteArray(_context, buf.getData(), 0, state.getCompleteSize(), handler);
        } else {
            // zero copy for single fragment
            m = I2NPMessageImpl.fromRawByteArray(_context, state.getFragments()[0].getData(), 0, state.getCompleteSize(), handler);
        }
        m.setUniqueId(state.getMessageId());
        return m;
    } catch (I2NPMessageException ime) {
        if (_log.shouldLog(Log.WARN)) {
            ByteArray ba;
            if (state.getFragmentCount() > 1)
                ba = buf;
            else
                ba = state.getFragments()[0];
            byte[] data = ba.getData();
            _log.warn("Message invalid: " + state + " PeerState: " + _transport.getPeerState(state.getFrom()) + "\nDUMP:\n" + HexDump.dump(data, 0, state.getCompleteSize()) + "\nRAW:\n" + Base64.encode(data, 0, state.getCompleteSize()), ime);
        }
        if (state.getFragments()[0].getData()[0] == DatabaseStoreMessage.MESSAGE_TYPE) {
            PeerState ps = _transport.getPeerState(state.getFrom());
            if (ps != null && ps.getRemotePort() == 65520) {
                // distinct port of buggy router
                _transport.sendDestroy(ps);
                _transport.dropPeer(ps, true, "Corrupt DSM");
                _context.banlist().banlistRouterForever(state.getFrom(), _x("Sent corrupt DSM"));
            }
        }
        _context.messageHistory().droppedInboundMessage(state.getMessageId(), state.getFrom(), "error: " + ime.toString() + ": " + state.toString());
        return null;
    } catch (RuntimeException e) {
        // e.g. AIOOBE
        if (_log.shouldLog(Log.WARN))
            _log.warn("Error handling a message: " + state, e);
        _context.messageHistory().droppedInboundMessage(state.getMessageId(), state.getFrom(), "error: " + e.toString() + ": " + state.toString());
        return null;
    } finally {
        state.releaseResources();
    }
}
Also used : I2NPMessageException(net.i2p.data.i2np.I2NPMessageException) I2NPMessage(net.i2p.data.i2np.I2NPMessage) ByteArray(net.i2p.data.ByteArray)

Example 25 with ByteArray

use of net.i2p.data.ByteArray in project i2p.i2p by i2p.

the class MessageReceiver method loop.

public void loop(I2NPMessageHandler handler) {
    InboundMessageState message = null;
    // ByteArray buf = _cache.acquire();
    ByteArray buf = new ByteArray(new byte[I2NPMessage.MAX_SIZE]);
    while (_alive) {
        int expired = 0;
        long expiredLifetime = 0;
        try {
            while (message == null) {
                message = _completeMessages.take();
                if ((message != null) && (message.getMessageId() == POISON_IMS)) {
                    message = null;
                    break;
                }
                if ((message != null) && (message.isExpired())) {
                    expiredLifetime += message.getLifetime();
                    // message.releaseResources() ??
                    message = null;
                    expired++;
                }
            // remaining = _completeMessages.size();
            }
        } catch (InterruptedException ie) {
        }
        if (expired > 0)
            _context.statManager().addRateData("udp.inboundExpired", expired, expiredLifetime);
        if (message != null) {
            // long before = System.currentTimeMillis();
            // if (remaining > 0)
            // _context.statManager().addRateData("udp.inboundRemaining", remaining, 0);
            int size = message.getCompleteSize();
            // long afterRead = -1;
            try {
                I2NPMessage msg = readMessage(buf, message, handler);
                // afterRead = System.currentTimeMillis();
                if (msg != null)
                    _transport.messageReceived(msg, null, message.getFrom(), message.getLifetime(), size);
            } catch (RuntimeException re) {
                _log.error("b0rked receiving a message.. wazza huzza hmm?", re);
                continue;
            }
            message = null;
        // long after = System.currentTimeMillis();
        // if (afterRead - before > 100)
        // _context.statManager().addRateData("udp.inboundReadTime", afterRead - before, remaining);
        // if (after - afterRead > 100)
        // _context.statManager().addRateData("udp.inboundReceiveProcessTime", after - afterRead, remaining);
        }
    }
// no need to zero it out, as these buffers are only used with an explicit getCompleteSize
// _cache.release(buf, false);
}
Also used : I2NPMessage(net.i2p.data.i2np.I2NPMessage) 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