Search in sources :

Example 21 with Log

use of net.i2p.util.Log in project i2p.i2p by i2p.

the class BuildRequestor method request.

/**
 *  Send out a build request message.
 *
 *  @param cfg ReplyMessageId must be set
 *  @return success
 */
public static boolean request(RouterContext ctx, TunnelPool pool, PooledTunnelCreatorConfig cfg, BuildExecutor exec) {
    // new style crypto fills in all the blanks, while the old style waits for replies to fill in the next hop, etc
    prepare(ctx, cfg);
    if (cfg.getLength() <= 1) {
        buildZeroHop(ctx, pool, cfg, exec);
        return true;
    }
    Log log = ctx.logManager().getLog(BuildRequestor.class);
    cfg.setTunnelPool(pool);
    TunnelInfo pairedTunnel = null;
    Hash farEnd = cfg.getFarEnd();
    TunnelManagerFacade mgr = ctx.tunnelManager();
    boolean isInbound = pool.getSettings().isInbound();
    if (pool.getSettings().isExploratory() || !usePairedTunnels(ctx)) {
        if (isInbound)
            pairedTunnel = mgr.selectOutboundExploratoryTunnel(farEnd);
        else
            pairedTunnel = mgr.selectInboundExploratoryTunnel(farEnd);
    } else {
        // building a client tunnel
        if (isInbound)
            pairedTunnel = mgr.selectOutboundTunnel(pool.getSettings().getDestination(), farEnd);
        else
            pairedTunnel = mgr.selectInboundTunnel(pool.getSettings().getDestination(), farEnd);
        if (pairedTunnel == null) {
            if (isInbound) {
                // random more reliable than closest ??
                // pairedTunnel = mgr.selectOutboundExploratoryTunnel(farEnd);
                pairedTunnel = mgr.selectOutboundTunnel();
                if (pairedTunnel != null && pairedTunnel.getLength() <= 1 && mgr.getOutboundSettings().getLength() > 0 && mgr.getOutboundSettings().getLength() + mgr.getOutboundSettings().getLengthVariance() > 0) {
                    // don't build using a zero-hop expl.,
                    // as it is both very bad for anonomyity,
                    // and it takes a build slot away from exploratory
                    pairedTunnel = null;
                }
            } else {
                // random more reliable than closest ??
                // pairedTunnel = mgr.selectInboundExploratoryTunnel(farEnd);
                pairedTunnel = mgr.selectInboundTunnel();
                if (pairedTunnel != null && pairedTunnel.getLength() <= 1 && mgr.getInboundSettings().getLength() > 0 && mgr.getInboundSettings().getLength() + mgr.getInboundSettings().getLengthVariance() > 0) {
                    // ditto
                    pairedTunnel = null;
                }
            }
            if (pairedTunnel != null && log.shouldLog(Log.INFO))
                log.info("Couldn't find a paired tunnel for " + cfg + ", using exploratory tunnel");
        }
    }
    if (pairedTunnel == null) {
        if (log.shouldLog(Log.WARN))
            log.warn("Tunnel build failed, as we couldn't find a paired tunnel for " + cfg);
        exec.buildComplete(cfg, pool);
        // Not even an exploratory tunnel? We are in big trouble.
        // Let's not spin through here too fast.
        // But don't let a client tunnel waiting for exploratories slow things down too much,
        // as there may be other tunnel pools who can build
        int ms = pool.getSettings().isExploratory() ? 250 : 25;
        try {
            Thread.sleep(ms);
        } catch (InterruptedException ie) {
        }
        return false;
    }
    // long beforeCreate = System.currentTimeMillis();
    TunnelBuildMessage msg = createTunnelBuildMessage(ctx, pool, cfg, pairedTunnel, exec);
    // long createTime = System.currentTimeMillis()-beforeCreate;
    if (msg == null) {
        if (log.shouldLog(Log.WARN))
            log.warn("Tunnel build failed, as we couldn't create the tunnel build message for " + cfg);
        exec.buildComplete(cfg, pool);
        return false;
    }
    // long beforeDispatch = System.currentTimeMillis();
    if (cfg.isInbound()) {
        if (log.shouldLog(Log.INFO))
            log.info("Sending the tunnel build request " + msg.getUniqueId() + " out the tunnel " + pairedTunnel + " to " + cfg.getPeer(0) + " for " + cfg + " waiting for the reply of " + cfg.getReplyMessageId());
        // send it out a tunnel targetting the first hop
        // TODO - would be nice to have a TunnelBuildFirstHopFailJob queued if the
        // pairedTunnel is zero-hop, but no way to do that?
        ctx.tunnelDispatcher().dispatchOutbound(msg, pairedTunnel.getSendTunnelId(0), cfg.getPeer(0));
    } else {
        if (log.shouldLog(Log.INFO))
            log.info("Sending the tunnel build request directly to " + cfg.getPeer(1) + " for " + cfg + " waiting for the reply of " + cfg.getReplyMessageId() + " with msgId=" + msg.getUniqueId());
        // send it directly to the first hop
        // Add some fuzz to the TBM expiration to make it harder to guess how many hops
        // or placement in the tunnel
        msg.setMessageExpiration(ctx.clock().now() + BUILD_MSG_TIMEOUT + ctx.random().nextLong(20 * 1000));
        // We set the OutNetMessage expiration much shorter, so that the
        // TunnelBuildFirstHopFailJob fires before the 13s build expiration.
        RouterInfo peer = ctx.netDb().lookupRouterInfoLocally(cfg.getPeer(1));
        if (peer == null) {
            if (log.shouldLog(Log.WARN))
                log.warn("Could not find the next hop to send the outbound request to: " + cfg);
            exec.buildComplete(cfg, pool);
            return false;
        }
        OutNetMessage outMsg = new OutNetMessage(ctx, msg, ctx.clock().now() + FIRST_HOP_TIMEOUT, PRIORITY, peer);
        outMsg.setOnFailedSendJob(new TunnelBuildFirstHopFailJob(ctx, pool, cfg, exec));
        ctx.outNetMessagePool().add(outMsg);
    }
    // + "ms and dispatched in " + (System.currentTimeMillis()-beforeDispatch));
    return true;
}
Also used : OutNetMessage(net.i2p.router.OutNetMessage) Log(net.i2p.util.Log) RouterInfo(net.i2p.data.router.RouterInfo) VariableTunnelBuildMessage(net.i2p.data.i2np.VariableTunnelBuildMessage) TunnelBuildMessage(net.i2p.data.i2np.TunnelBuildMessage) TunnelInfo(net.i2p.router.TunnelInfo) Hash(net.i2p.data.Hash) TunnelManagerFacade(net.i2p.router.TunnelManagerFacade)

Example 22 with Log

use of net.i2p.util.Log in project i2p.i2p by i2p.

the class PooledTunnelCreatorConfig method setTunnelPool.

public void setTunnelPool(TunnelPool pool) {
    if (pool != null) {
        _pool = pool;
    } else {
        Log log = _context.logManager().getLog(getClass());
        log.error("Null tunnel pool?", new Exception("foo"));
    }
}
Also used : Log(net.i2p.util.Log)

Example 23 with Log

use of net.i2p.util.Log in project i2p.i2p by i2p.

the class FloodOnlyLookupTimeoutJob method runJob.

public void runJob() {
    Log log = getContext().logManager().getLog(getClass());
    if (log.shouldLog(Log.INFO))
        log.info(_search.getJobId() + ": search timed out");
    _search.failed();
}
Also used : Log(net.i2p.util.Log)

Example 24 with Log

use of net.i2p.util.Log in project i2p.i2p by i2p.

the class GarlicClove method toByteArray.

/**
 */
@Override
public byte[] toByteArray() {
    byte[] rv = new byte[estimateSize()];
    int offset = 0;
    offset += _instructions.writeBytes(rv, offset);
    // offset += _msg.toByteArray(rv);
    try {
        byte[] m = _msg.toByteArray();
        System.arraycopy(m, 0, rv, offset, m.length);
        offset += m.length;
    } catch (RuntimeException e) {
        throw new RuntimeException("Unable to write: " + _msg + ": " + e.getMessage());
    }
    DataHelper.toLong(rv, offset, 4, _cloveId);
    offset += 4;
    DataHelper.toDate(rv, offset, _expiration.getTime());
    offset += DataHelper.DATE_LENGTH;
    offset += _certificate.writeBytes(rv, offset);
    if (offset != rv.length) {
        Log log = I2PAppContext.getGlobalContext().logManager().getLog(GarlicClove.class);
        log.error("Clove offset: " + offset + " but estimated length: " + rv.length);
    }
    return rv;
}
Also used : Log(net.i2p.util.Log)

Example 25 with Log

use of net.i2p.util.Log in project i2p.i2p by i2p.

the class GarlicMessageBuilder method buildCloveSet.

/**
 **
 *    private static void noteWrap(RouterContext ctx, GarlicMessage wrapper, GarlicConfig contained) {
 *        for (int i = 0; i < contained.getCloveCount(); i++) {
 *            GarlicConfig config = contained.getClove(i);
 *            if (config instanceof PayloadGarlicConfig) {
 *                I2NPMessage msg = ((PayloadGarlicConfig)config).getPayload();
 *                String bodyType = msg.getClass().getName();
 *                ctx.messageHistory().wrap(bodyType, msg.getUniqueId(), GarlicMessage.class.getName(), wrapper.getUniqueId());
 *            }
 *        }
 *    }
 ***
 */
/**
 * Build the unencrypted GarlicMessage specified by the config.
 * It contains the number of cloves, followed by each clove,
 * followed by a certificate, ID, and expiration date.
 *
 * @throws IllegalArgumentException on error
 */
private static byte[] buildCloveSet(RouterContext ctx, GarlicConfig config) {
    ByteArrayOutputStream baos = null;
    Log log = ctx.logManager().getLog(GarlicMessageBuilder.class);
    try {
        if (config instanceof PayloadGarlicConfig) {
            byte[] clove = buildClove(ctx, (PayloadGarlicConfig) config);
            baos = new ByteArrayOutputStream(clove.length + 16);
            baos.write((byte) 1);
            baos.write(clove);
        } else {
            byte[][] cloves = new byte[config.getCloveCount()][];
            for (int i = 0; i < config.getCloveCount(); i++) {
                GarlicConfig c = config.getClove(i);
                if (c instanceof PayloadGarlicConfig) {
                    // log.debug("Subclove IS a payload garlic clove");
                    cloves[i] = buildClove(ctx, (PayloadGarlicConfig) c);
                } else {
                    log.debug("Subclove IS NOT a payload garlic clove");
                    // See notes below
                    cloves[i] = buildClove(ctx, c);
                }
            }
            int len = 1;
            for (int i = 0; i < cloves.length; i++) len += cloves[i].length;
            baos = new ByteArrayOutputStream(len + 16);
            baos.write((byte) cloves.length);
            for (int i = 0; i < cloves.length; i++) baos.write(cloves[i]);
        }
        config.getCertificate().writeBytes(baos);
        DataHelper.writeLong(baos, 4, config.getId());
        DataHelper.writeLong(baos, DataHelper.DATE_LENGTH, config.getExpiration());
    } catch (IOException ioe) {
        log.error("Error building the clove set", ioe);
        throw new IllegalArgumentException("Error building the clove set", ioe);
    } catch (DataFormatException dfe) {
        log.error("Error building the clove set", dfe);
        throw new IllegalArgumentException("Error building the clove set", dfe);
    }
    return baos.toByteArray();
}
Also used : DataFormatException(net.i2p.data.DataFormatException) Log(net.i2p.util.Log) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Aggregations

Log (net.i2p.util.Log)94 IOException (java.io.IOException)30 File (java.io.File)13 Properties (java.util.Properties)11 DataFormatException (net.i2p.data.DataFormatException)11 FileInputStream (java.io.FileInputStream)7 GeneralSecurityException (java.security.GeneralSecurityException)7 ArrayList (java.util.ArrayList)7 Hash (net.i2p.data.Hash)6 HashMap (java.util.HashMap)5 InputStream (java.io.InputStream)4 EventLog (net.i2p.router.util.EventLog)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Map (java.util.Map)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 I2PAppContext (net.i2p.I2PAppContext)3 I2PSession (net.i2p.client.I2PSession)3 I2PSessionException (net.i2p.client.I2PSessionException)3 SigType (net.i2p.crypto.SigType)3 RouterInfo (net.i2p.data.router.RouterInfo)3