Search in sources :

Example 6 with I2NPMessage

use of net.i2p.data.i2np.I2NPMessage in project i2p.i2p by i2p.

the class FragmentHandler method receiveComplete.

private void receiveComplete(FragmentedMessage msg) {
    if (msg == null)
        return;
    _completed++;
    String stringified = null;
    if (_log.shouldLog(Log.DEBUG))
        stringified = msg.toString();
    byte[] data = null;
    try {
        int fragmentCount = msg.getFragmentCount();
        // toByteArray destroys the contents of the message completely
        data = msg.toByteArray();
        if (data == null)
            // fragments already released???
            throw new I2NPMessageException("null data");
        if (_log.shouldLog(Log.DEBUG))
            // + Base64.encode(data)
            _log.debug("RECV(" + data.length + "): ");
        // + " " + _context.sha().calculateHash(data).toBase64());
        // TODO read in as unknown message for outbound tunnels,
        // since this will just be packaged in a TunnelGatewayMessage.
        // Not a big savings since most everything is a GarlicMessage
        // and so the readMessage() call is fast.
        // The unencrypted messages at the OBEP are (V)TBMs
        // and perhaps an occasional DatabaseLookupMessage
        I2NPMessage m = new I2NPMessageHandler(_context).readMessage(data);
        // + msg.toString());
        noteReception(m.getUniqueId(), fragmentCount - 1, "complete: ");
        noteCompletion(m.getUniqueId());
        _receiver.receiveComplete(m, msg.getTargetRouter(), msg.getTargetTunnel());
    } catch (I2NPMessageException ime) {
        if (stringified == null)
            stringified = msg.toString();
        if (_log.shouldLog(Log.WARN)) {
            _log.warn("Error receiving fragmented message (corrupt?): " + stringified, ime);
            _log.warn("DUMP:\n" + HexDump.dump(data));
            _log.warn("RAW:\n" + Base64.encode(data));
        }
    }
}
Also used : I2NPMessageException(net.i2p.data.i2np.I2NPMessageException) I2NPMessage(net.i2p.data.i2np.I2NPMessage) I2NPMessageHandler(net.i2p.data.i2np.I2NPMessageHandler)

Example 7 with I2NPMessage

use of net.i2p.data.i2np.I2NPMessage in project i2p.i2p by i2p.

the class FragmentHandler method receiveComplete.

/**
 *  Zero-copy reception of an unfragmented message
 *  @since 0.9
 */
private void receiveComplete(byte[] data, int offset, int len, Hash router, TunnelId tunnelId) {
    _completed++;
    try {
        if (_log.shouldLog(Log.DEBUG))
            _log.debug("RECV unfrag(" + len + ')');
        // TODO read in as unknown message for outbound tunnels,
        // since this will just be packaged in a TunnelGatewayMessage.
        // Not a big savings since most everything is a GarlicMessage
        // and so the readMessage() call is fast.
        // The unencrypted messages at the OBEP are (V)TBMs
        // and perhaps an occasional DatabaseLookupMessage
        I2NPMessageHandler h = new I2NPMessageHandler(_context);
        h.readMessage(data, offset, len);
        I2NPMessage m = h.lastRead();
        // + msg.toString());
        noteReception(m.getUniqueId(), 0, "complete: ");
        noteCompletion(m.getUniqueId());
        _receiver.receiveComplete(m, router, tunnelId);
    } catch (I2NPMessageException ime) {
        if (_log.shouldLog(Log.WARN)) {
            _log.warn("Error receiving unfragmented message (corrupt?)", ime);
            _log.warn("DUMP:\n" + HexDump.dump(data, offset, len));
            _log.warn("RAW:\n" + Base64.encode(data, offset, len));
        }
    }
}
Also used : I2NPMessageException(net.i2p.data.i2np.I2NPMessageException) I2NPMessage(net.i2p.data.i2np.I2NPMessage) I2NPMessageHandler(net.i2p.data.i2np.I2NPMessageHandler)

Example 8 with I2NPMessage

use of net.i2p.data.i2np.I2NPMessage in project i2p.i2p by i2p.

the class OutboundMessageDistributor method distribute.

private void distribute(I2NPMessage msg, RouterInfo target, TunnelId tunnel) {
    I2NPMessage m = msg;
    if (tunnel != null) {
        TunnelGatewayMessage t = new TunnelGatewayMessage(_context);
        t.setMessage(msg);
        t.setTunnelId(tunnel);
        t.setMessageExpiration(m.getMessageExpiration());
        m = t;
    }
    if (_context.routerHash().equals(target.getIdentity().calculateHash())) {
        if (_log.shouldLog(Log.DEBUG))
            _log.debug("queueing inbound message to ourselves: " + m);
        // TODO if UnknownI2NPMessage, convert it.
        // See FragmentHandler.receiveComplete()
        _context.inNetMessagePool().add(m, null, null);
        return;
    } else {
        OutNetMessage out = new OutNetMessage(_context, m, _context.clock().now() + MAX_DISTRIBUTE_TIME, _priority, target);
        if (_log.shouldLog(Log.DEBUG))
            _log.debug("queueing outbound message to " + target.getIdentity().calculateHash());
        _context.outNetMessagePool().add(out);
    }
}
Also used : TunnelGatewayMessage(net.i2p.data.i2np.TunnelGatewayMessage) OutNetMessage(net.i2p.router.OutNetMessage) I2NPMessage(net.i2p.data.i2np.I2NPMessage)

Example 9 with I2NPMessage

use of net.i2p.data.i2np.I2NPMessage in project i2p.i2p by i2p.

the class GatewayITBase method testLarge.

@Test
public void testLarge() throws Exception {
    int runCount = 1;
    List<DataMessage> messages = new ArrayList<DataMessage>(runCount);
    long start = _context.clock().now();
    for (int i = 0; i < runCount; i++) {
        DataMessage m = getTestMessage(1024);
        messages.add(m);
        _gw.add(m, null, null);
    }
    Thread.sleep(1000);
    List<I2NPMessage> received = _receiver.clearReceived();
    for (int i = 0; i < messages.size(); i++) {
        assertTrue(received.contains(((I2NPMessage) messages.get(i))));
    }
}
Also used : DataMessage(net.i2p.data.i2np.DataMessage) I2NPMessage(net.i2p.data.i2np.I2NPMessage) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 10 with I2NPMessage

use of net.i2p.data.i2np.I2NPMessage in project i2p.i2p by i2p.

the class SearchUpdateReplyFoundJob method runJob.

public void runJob() {
    if (_isFloodfillPeer)
        _job.decrementOutstandingFloodfillSearches();
    I2NPMessage message = _message;
    if (_log.shouldLog(Log.INFO))
        _log.info(getJobId() + ": Reply from " + _peer.toBase64() + " with message " + message.getClass().getSimpleName());
    long howLong = System.currentTimeMillis() - _sentOn;
    // assume requests are 1KB (they're almost always much smaller, but tunnels have a fixed size)
    int msgSize = 1024;
    if (_replyTunnel != null) {
        for (int i = 0; i < _replyTunnel.getLength(); i++) getContext().profileManager().tunnelDataPushed(_replyTunnel.getPeer(i), howLong, msgSize);
        _replyTunnel.incrementVerifiedBytesTransferred(msgSize);
    }
    if (_outTunnel != null) {
        for (int i = 0; i < _outTunnel.getLength(); i++) getContext().profileManager().tunnelDataPushed(_outTunnel.getPeer(i), howLong, msgSize);
        _outTunnel.incrementVerifiedBytesTransferred(msgSize);
    }
    if (message instanceof DatabaseStoreMessage) {
        long timeToReply = _state.dataFound(_peer);
        DatabaseStoreMessage msg = (DatabaseStoreMessage) message;
        DatabaseEntry entry = msg.getEntry();
        try {
            _facade.store(msg.getKey(), entry);
            getContext().profileManager().dbLookupSuccessful(_peer, timeToReply);
        } catch (UnsupportedCryptoException iae) {
            // don't blame the peer
            getContext().profileManager().dbLookupSuccessful(_peer, timeToReply);
            _state.abort();
        // searchNext() will call fail()
        } catch (IllegalArgumentException iae) {
            if (_log.shouldLog(Log.WARN))
                _log.warn("Peer " + _peer + " sent us invalid data: ", iae);
            // blame the peer
            getContext().profileManager().dbLookupReply(_peer, 0, 0, 1, 0, timeToReply);
        }
    } else if (message instanceof DatabaseSearchReplyMessage) {
        _job.replyFound((DatabaseSearchReplyMessage) message, _peer);
    } else {
        if (_log.shouldLog(Log.ERROR))
            _log.error(getJobId() + ": What?! Reply job matched a strange message: " + message);
        return;
    }
    _job.searchNext();
}
Also used : I2NPMessage(net.i2p.data.i2np.I2NPMessage) DatabaseStoreMessage(net.i2p.data.i2np.DatabaseStoreMessage) DatabaseSearchReplyMessage(net.i2p.data.i2np.DatabaseSearchReplyMessage) DatabaseEntry(net.i2p.data.DatabaseEntry)

Aggregations

I2NPMessage (net.i2p.data.i2np.I2NPMessage)21 Hash (net.i2p.data.Hash)9 ArrayList (java.util.ArrayList)5 DataMessage (net.i2p.data.i2np.DataMessage)4 I2NPMessageException (net.i2p.data.i2np.I2NPMessageException)4 TunnelInfo (net.i2p.router.TunnelInfo)4 Test (org.junit.Test)4 TunnelId (net.i2p.data.TunnelId)3 DatabaseLookupMessage (net.i2p.data.i2np.DatabaseLookupMessage)3 DatabaseStoreMessage (net.i2p.data.i2np.DatabaseStoreMessage)3 RouterInfo (net.i2p.data.router.RouterInfo)3 OutNetMessage (net.i2p.router.OutNetMessage)3 ByteArray (net.i2p.data.ByteArray)2 I2NPMessageHandler (net.i2p.data.i2np.I2NPMessageHandler)2 Job (net.i2p.router.Job)2 ReplyJob (net.i2p.router.ReplyJob)2 IOException (java.io.IOException)1 ServerSocketChannel (java.nio.channels.ServerSocketChannel)1 SocketChannel (java.nio.channels.SocketChannel)1 HashSet (java.util.HashSet)1