Search in sources :

Example 1 with VariableTunnelBuildMessage

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

the class BuildRequestor method createTunnelBuildMessage.

/**
 * @since 0.7.12
 */
/**
 **
 *we can assume everybody supports variable now...
 *keep this here for the next time we change the build protocol
 *    private static boolean supportsVariable(RouterContext ctx, Hash h) {
 *        RouterInfo ri = ctx.netDb().lookupRouterInfoLocally(h);
 *        if (ri == null)
 *            return false;
 *        String v = ri.getVersion();
 *        return VersionComparator.comp(v, MIN_VARIABLE_VERSION) >= 0;
 *    }
 ***
 */
/**
 *  If the tunnel is short enough, and everybody in the tunnel, and the
 *  OBEP or IBGW for the paired tunnel, all support the new variable-sized tunnel build message,
 *  then use that, otherwise the old 8-entry version.
 *  @return null on error
 */
private static TunnelBuildMessage createTunnelBuildMessage(RouterContext ctx, TunnelPool pool, PooledTunnelCreatorConfig cfg, TunnelInfo pairedTunnel, BuildExecutor exec) {
    Log log = ctx.logManager().getLog(BuildRequestor.class);
    long replyTunnel = 0;
    Hash replyRouter;
    boolean useVariable = SEND_VARIABLE && cfg.getLength() <= MEDIUM_RECORDS;
    if (cfg.isInbound()) {
        // replyTunnel = 0; // as above
        replyRouter = ctx.routerHash();
    /**
     **
     *we can assume everybody supports variable now...
     *keep this here for the next time we change the build protocol
     *            if (useVariable) {
     *                // check the reply OBEP and all the tunnel peers except ourselves
     *                if (!supportsVariable(ctx, pairedTunnel.getPeer(pairedTunnel.getLength() - 1))) {
     *                    useVariable = false;
     *                } else {
     *                    for (int i = 0; i < cfg.getLength() - 1; i++) {
     *                        if (!supportsVariable(ctx, cfg.getPeer(i))) {
     *                            useVariable = false;
     *                            break;
     *                        }
     *                    }
     *                }
     *            }
     ***
     */
    } else {
        replyTunnel = pairedTunnel.getReceiveTunnelId(0).getTunnelId();
        replyRouter = pairedTunnel.getPeer(0);
    /**
     **
     *we can assume everybody supports variable now
     *keep this here for the next time we change the build protocol
     *            if (useVariable) {
     *                // check the reply IBGW and all the tunnel peers except ourselves
     *                if (!supportsVariable(ctx, replyRouter)) {
     *                    useVariable = false;
     *                } else {
     *                    for (int i = 1; i < cfg.getLength() - 1; i++) {
     *                        if (!supportsVariable(ctx, cfg.getPeer(i))) {
     *                            useVariable = false;
     *                            break;
     *                        }
     *                    }
     *                }
     *            }
     ***
     */
    }
    // populate and encrypt the message
    TunnelBuildMessage msg;
    List<Integer> order;
    if (useVariable) {
        if (cfg.getLength() <= SHORT_RECORDS) {
            msg = new VariableTunnelBuildMessage(ctx, SHORT_RECORDS);
            order = new ArrayList<Integer>(SHORT_ORDER);
        } else {
            msg = new VariableTunnelBuildMessage(ctx, MEDIUM_RECORDS);
            order = new ArrayList<Integer>(MEDIUM_ORDER);
        }
    } else {
        msg = new TunnelBuildMessage(ctx);
        order = new ArrayList<Integer>(ORDER);
    }
    // This is in BuildExecutor.buildTunnel() now
    // long replyMessageId = ctx.random().nextLong(I2NPMessage.MAX_ID_VALUE);
    // cfg.setReplyMessageId(replyMessageId);
    // randomized placement within the message
    Collections.shuffle(order, ctx.random());
    cfg.setReplyOrder(order);
    if (log.shouldLog(Log.DEBUG))
        log.debug("Build order: " + order + " for " + cfg);
    for (int i = 0; i < msg.getRecordCount(); i++) {
        int hop = order.get(i).intValue();
        PublicKey key = null;
        if (BuildMessageGenerator.isBlank(cfg, hop)) {
        // erm, blank
        } else {
            Hash peer = cfg.getPeer(hop);
            RouterInfo peerInfo = ctx.netDb().lookupRouterInfoLocally(peer);
            if (peerInfo == null) {
                if (log.shouldLog(Log.WARN))
                    log.warn("Peer selected for hop " + i + "/" + hop + " was not found locally: " + peer + " for " + cfg);
                return null;
            } else {
                key = peerInfo.getIdentity().getPublicKey();
            }
        }
        if (log.shouldLog(Log.DEBUG))
            log.debug(cfg.getReplyMessageId() + ": record " + i + "/" + hop + " has key " + key);
        BuildMessageGenerator.createRecord(i, hop, msg, cfg, replyRouter, replyTunnel, ctx, key);
    }
    BuildMessageGenerator.layeredEncrypt(ctx, msg, cfg, order);
    return msg;
}
Also used : Log(net.i2p.util.Log) PublicKey(net.i2p.data.PublicKey) RouterInfo(net.i2p.data.router.RouterInfo) VariableTunnelBuildMessage(net.i2p.data.i2np.VariableTunnelBuildMessage) TunnelBuildMessage(net.i2p.data.i2np.TunnelBuildMessage) VariableTunnelBuildMessage(net.i2p.data.i2np.VariableTunnelBuildMessage) Hash(net.i2p.data.Hash)

Aggregations

Hash (net.i2p.data.Hash)1 PublicKey (net.i2p.data.PublicKey)1 TunnelBuildMessage (net.i2p.data.i2np.TunnelBuildMessage)1 VariableTunnelBuildMessage (net.i2p.data.i2np.VariableTunnelBuildMessage)1 RouterInfo (net.i2p.data.router.RouterInfo)1 Log (net.i2p.util.Log)1