Search in sources :

Example 1 with DataMessage

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

the class OutboundClientMessageJobHelper method buildDataClove.

/**
 * Build a clove that sends the payload to the destination
 */
private static PayloadGarlicConfig buildDataClove(RouterContext ctx, Payload data, Destination dest, long expiration) {
    DeliveryInstructions instructions = new DeliveryInstructions();
    instructions.setDeliveryMode(DeliveryInstructions.DELIVERY_MODE_DESTINATION);
    instructions.setDestination(dest.calculateHash());
    // defaults
    // instructions.setDelayRequested(false);
    // instructions.setDelaySeconds(0);
    // instructions.setEncrypted(false);
    PayloadGarlicConfig clove = new PayloadGarlicConfig();
    clove.setCertificate(Certificate.NULL_CERT);
    clove.setDeliveryInstructions(instructions);
    clove.setExpiration(expiration);
    clove.setId(ctx.random().nextLong(I2NPMessage.MAX_ID_VALUE));
    DataMessage msg = new DataMessage(ctx);
    msg.setData(data.getEncryptedData());
    clove.setPayload(msg);
    return clove;
}
Also used : DeliveryInstructions(net.i2p.data.i2np.DeliveryInstructions) DataMessage(net.i2p.data.i2np.DataMessage)

Example 2 with DataMessage

use of net.i2p.data.i2np.DataMessage 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 3 with DataMessage

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

the class GatewayITBase method getTestMessage.

private static DataMessage getTestMessage(int size) {
    DataMessage m = new DataMessage(_context);
    m.setData(new byte[size]);
    java.util.Arrays.fill(m.getData(), (byte) 0xFF);
    m.setMessageExpiration(_context.clock().now() + 60 * 1000);
    m.setUniqueId(_context.random().nextLong(I2NPMessage.MAX_ID_VALUE));
    // not sure why, maybe side-effect? --zab
    byte[] data = m.toByteArray();
    return m;
}
Also used : DataMessage(net.i2p.data.i2np.DataMessage)

Example 4 with DataMessage

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

the class InboundMessageDistributor method handleClove.

/**
 * Handle a clove removed from the garlic message
 */
public void handleClove(DeliveryInstructions instructions, I2NPMessage data) {
    int type = data.getType();
    switch(instructions.getDeliveryMode()) {
        case DeliveryInstructions.DELIVERY_MODE_LOCAL:
            if (_log.shouldLog(Log.DEBUG))
                _log.debug("local delivery instructions for clove: " + data.getClass().getSimpleName());
            if (type == GarlicMessage.MESSAGE_TYPE) {
                _receiver.receive((GarlicMessage) data);
            } else if (type == DatabaseStoreMessage.MESSAGE_TYPE) {
                // Treat db store explicitly here (not in HandleFloodfillDatabaseStoreMessageJob),
                // since we don't want to republish (or flood)
                // unnecessarily. Reply tokens ignored.
                DatabaseStoreMessage dsm = (DatabaseStoreMessage) data;
                // Ensure the reply info is cleared, just in case
                dsm.setReplyToken(0);
                dsm.setReplyTunnel(null);
                dsm.setReplyGateway(null);
                if (dsm.getEntry().getType() == DatabaseEntry.KEY_TYPE_LEASESET) {
                    // Case 1:
                    // store of our own LS.
                    // This is almost certainly a response to a FloodfillVerifyStoreJob search.
                    // We must send to the InNetMessagePool so the message can be matched
                    // and the verify marked as successful.
                    // Case 2:
                    // Store of somebody else's LS.
                    // This could be an encrypted response to an IterativeSearchJob search.
                    // We must send to the InNetMessagePool so the message can be matched
                    // and the search marked as successful.
                    // Or, it's a normal LS bundled with data and a MessageStatusMessage.
                    // ... and inject it.
                    ((LeaseSet) dsm.getEntry()).setReceivedAsReply();
                    if (_log.shouldLog(Log.INFO))
                        _log.info("Storing garlic LS down tunnel for: " + dsm.getKey() + " sent to: " + _client);
                    _context.inNetMessagePool().add(dsm, null, null);
                } else {
                    if (_client != null) {
                        // drop it, since the data we receive shouldn't include router
                        // references, as that might get us to talk to them (and therefore
                        // open an attack vector)
                        _context.statManager().addRateData("tunnel.dropDangerousClientTunnelMessage", 1, DatabaseStoreMessage.MESSAGE_TYPE);
                        _log.error("Dropped dangerous message down a tunnel for " + _client + ": " + dsm, new Exception("cause"));
                        return;
                    }
                    // ... and inject it.
                    if (_log.shouldLog(Log.INFO))
                        _log.info("Storing garlic RI down tunnel for: " + dsm.getKey() + " sent to: " + _client);
                    _context.inNetMessagePool().add(dsm, null, null);
                }
            } else if (_client != null && type == DatabaseSearchReplyMessage.MESSAGE_TYPE) {
                // DSRMs show up here now that replies are encrypted
                // TODO: Strip in IterativeLookupJob etc. instead, depending on
                // LS or RI and client or expl., so that we can safely follow references
                // in a reply to a LS lookup over client tunnels.
                // ILJ would also have to follow references via client tunnels
                DatabaseSearchReplyMessage orig = (DatabaseSearchReplyMessage) data;
                /**
                 **
                 *                    if (orig.getNumReplies() > 0) {
                 *                        if (_log.shouldLog(Log.INFO))
                 *                            _log.info("Removing replies from a garlic DSRM down a tunnel for " + _client + ": " + data);
                 *                        DatabaseSearchReplyMessage newMsg = new DatabaseSearchReplyMessage(_context);
                 *                        newMsg.setFromHash(orig.getFromHash());
                 *                        newMsg.setSearchKey(orig.getSearchKey());
                 *                        orig = newMsg;
                 *                     }
                 ***
                 */
                _context.inNetMessagePool().add(orig, null, null);
            } else if (type == DataMessage.MESSAGE_TYPE) {
                // a data message targetting the local router is how we send load tests (real
                // data messages target destinations)
                _context.statManager().addRateData("tunnel.handleLoadClove", 1);
                data = null;
            // _context.inNetMessagePool().add(data, null, null);
            } else if (_client != null && type != DeliveryStatusMessage.MESSAGE_TYPE) {
                // drop it, since the data we receive shouldn't include other stuff,
                // as that might open an attack vector
                _context.statManager().addRateData("tunnel.dropDangerousClientTunnelMessage", 1, data.getType());
                _log.error("Dropped dangerous message down a tunnel for " + _client + ": " + data, new Exception("cause"));
            } else {
                _context.inNetMessagePool().add(data, null, null);
            }
            return;
        case DeliveryInstructions.DELIVERY_MODE_DESTINATION:
            Hash to = instructions.getDestination();
            // Can we route UnknownI2NPMessages to a destination too?
            if (type != DataMessage.MESSAGE_TYPE) {
                if (_log.shouldLog(Log.ERROR))
                    _log.error("cant send a " + data.getClass().getSimpleName() + " to a destination");
            } else if (_client != null && _client.equals(to)) {
                if (_log.shouldLog(Log.DEBUG))
                    _log.debug("data message came down a tunnel for " + _client);
                DataMessage dm = (DataMessage) data;
                Payload payload = new Payload();
                payload.setEncryptedData(dm.getData());
                ClientMessage m = new ClientMessage(_client, payload);
                _context.clientManager().messageReceived(m);
            } else if (_client != null) {
                // Shared tunnel?
                TunnelPoolSettings tgt = _context.tunnelManager().getInboundSettings(to);
                if (tgt != null && _client.equals(tgt.getAliasOf())) {
                    // same as above, just different log
                    if (_log.shouldLog(Log.DEBUG))
                        _log.debug("data message came down a tunnel for " + _client + " targeting shared " + to);
                    DataMessage dm = (DataMessage) data;
                    Payload payload = new Payload();
                    payload.setEncryptedData(dm.getData());
                    ClientMessage m = new ClientMessage(to, payload);
                    _context.clientManager().messageReceived(m);
                } else {
                    if (_log.shouldLog(Log.ERROR))
                        _log.error("Data message came down a tunnel for " + _client + " but targetted " + to);
                }
            } else {
                if (_log.shouldLog(Log.ERROR))
                    _log.error("Data message came down an exploratory tunnel targeting " + to);
            }
            return;
        // fall through
        case DeliveryInstructions.DELIVERY_MODE_ROUTER:
        case DeliveryInstructions.DELIVERY_MODE_TUNNEL:
            if (_log.shouldLog(Log.INFO))
                _log.info("clove targetted " + instructions.getRouter() + ":" + instructions.getTunnelId() + ", treat recursively to prevent leakage");
            distribute(data, instructions.getRouter(), instructions.getTunnelId());
            return;
        default:
            if (_log.shouldLog(Log.ERROR))
                _log.error("Unknown instruction " + instructions.getDeliveryMode() + ": " + instructions);
            return;
    }
}
Also used : DataMessage(net.i2p.data.i2np.DataMessage) DatabaseStoreMessage(net.i2p.data.i2np.DatabaseStoreMessage) DatabaseSearchReplyMessage(net.i2p.data.i2np.DatabaseSearchReplyMessage) TunnelPoolSettings(net.i2p.router.TunnelPoolSettings) Payload(net.i2p.data.Payload) ClientMessage(net.i2p.router.ClientMessage) Hash(net.i2p.data.Hash)

Example 5 with DataMessage

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

the class UDPFlooder method run.

public void run() {
    long nextSend = _context.clock().now();
    while (_alive) {
        try {
            synchronized (_peers) {
                if (_peers.isEmpty())
                    _peers.wait();
            }
        } catch (InterruptedException ie) {
        }
        long now = _context.clock().now();
        if (now >= nextSend) {
            // peers always grows, so this is fairly safe
            for (int i = 0; i < _peers.size(); i++) {
                PeerState peer = _peers.get(i);
                DataMessage m = new DataMessage(_context);
                // new byte[4096];
                byte[] data = _floodData;
                // _context.random().nextBytes(data);
                m.setData(data);
                m.setMessageExpiration(_context.clock().now() + 10 * 1000);
                m.setUniqueId(_context.random().nextLong(I2NPMessage.MAX_ID_VALUE));
                if (true) {
                    RouterInfo to = _context.netDb().lookupRouterInfoLocally(peer.getRemotePeer());
                    if (to == null)
                        continue;
                    OutNetMessage msg = new OutNetMessage(_context, m, m.getMessageExpiration(), 500, to);
                    // warning, getStatLog() can be null
                    // _context.statManager().getStatLog().addData(peer.getRemotePeer().toBase64().substring(0,6), "udp.floodDataSent", 1, 0);
                    _transport.send(msg);
                } else {
                    _transport.send(m, peer);
                }
            }
            nextSend = now + calcFloodDelay();
        }
        long delay = nextSend - now;
        if (delay > 0) {
            if (delay > 10 * 1000) {
                long fd = calcFloodDelay();
                if (delay > fd) {
                    nextSend = now + fd;
                    delay = fd;
                }
            }
            try {
                Thread.sleep(delay);
            } catch (InterruptedException ie) {
            }
        }
    }
}
Also used : OutNetMessage(net.i2p.router.OutNetMessage) DataMessage(net.i2p.data.i2np.DataMessage) RouterInfo(net.i2p.data.router.RouterInfo)

Aggregations

DataMessage (net.i2p.data.i2np.DataMessage)10 ArrayList (java.util.ArrayList)4 Hash (net.i2p.data.Hash)4 I2NPMessage (net.i2p.data.i2np.I2NPMessage)4 Test (org.junit.Test)4 Payload (net.i2p.data.Payload)2 TunnelId (net.i2p.data.TunnelId)2 DeliveryInstructions (net.i2p.data.i2np.DeliveryInstructions)2 DatabaseSearchReplyMessage (net.i2p.data.i2np.DatabaseSearchReplyMessage)1 DatabaseStoreMessage (net.i2p.data.i2np.DatabaseStoreMessage)1 RouterInfo (net.i2p.data.router.RouterInfo)1 ClientMessage (net.i2p.router.ClientMessage)1 OutNetMessage (net.i2p.router.OutNetMessage)1 TunnelPoolSettings (net.i2p.router.TunnelPoolSettings)1