Search in sources :

Example 66 with Hash

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

the class CreateSessionJob method runJob.

public void runJob() {
    Hash dest = _config.getDestination().calculateHash();
    if (_log.shouldLog(Log.INFO))
        _log.info("Requesting lease set for destination " + dest);
    ClientTunnelSettings settings = new ClientTunnelSettings(dest);
    Properties props = new Properties();
    // We're NOT going to force all clients to use the router's defaults, since that may be
    // excessive.  This means that unless the user says otherwise, we'll be satisfied with whatever
    // is available.  Otherwise, when the router starts up, if there aren't sufficient tunnels with the
    // adequate number of hops, the user will have to wait.  Once peer profiles are persistent, we can
    // reenable this, since on startup we'll have a sufficient number of high enough ranked peers to
    // tunnel through.  (perhaps).
    // XXX take the router's defaults
    // XXX props.putAll(Router.getInstance().getConfigMap());
    // override them by the client's settings
    props.putAll(_config.getOptions());
    // and load 'em up (using anything not yet set as the software defaults)
    settings.readFromProperties(props);
    getContext().tunnelManager().buildTunnels(_config.getDestination(), settings);
}
Also used : ClientTunnelSettings(net.i2p.router.ClientTunnelSettings) Hash(net.i2p.data.Hash) Properties(java.util.Properties)

Example 67 with Hash

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

the class LoadRouterInfoJob method loadRouterInfo.

/**
 *  Loads router.info and either router.keys.dat or router.keys.
 *
 *  See CreateRouterInfoJob for file formats
 */
private void loadRouterInfo() {
    RouterInfo info = null;
    File rif = new File(getContext().getRouterDir(), CreateRouterInfoJob.INFO_FILENAME);
    boolean infoExists = rif.exists();
    File rkf = new File(getContext().getRouterDir(), CreateRouterInfoJob.KEYS_FILENAME);
    boolean keysExist = rkf.exists();
    File rkf2 = new File(getContext().getRouterDir(), CreateRouterInfoJob.KEYS2_FILENAME);
    boolean keys2Exist = rkf2.exists();
    InputStream fis1 = null;
    try {
        // so pretend the RI isn't there if there is no keyfile
        if (infoExists && (keys2Exist || keysExist)) {
            fis1 = new BufferedInputStream(new FileInputStream(rif));
            info = new RouterInfo();
            info.readBytes(fis1);
            // Catch this here before it all gets worse
            if (!info.isValid())
                throw new DataFormatException("Our RouterInfo has a bad signature");
            if (_log.shouldLog(Log.DEBUG))
                _log.debug("Reading in routerInfo from " + rif.getAbsolutePath() + " and it has " + info.getAddresses().size() + " addresses");
            // don't reuse if family name changed
            if (DataHelper.eq(info.getOption(FamilyKeyCrypto.OPT_NAME), getContext().getProperty(FamilyKeyCrypto.PROP_FAMILY_NAME))) {
                _us = info;
            } else {
                _log.logAlways(Log.WARN, "NetDb family name changed");
            }
        }
        if (keys2Exist || keysExist) {
            KeyData kd = readKeyData(rkf, rkf2);
            PublicKey pubkey = kd.routerIdentity.getPublicKey();
            SigningPublicKey signingPubKey = kd.routerIdentity.getSigningPublicKey();
            PrivateKey privkey = kd.privateKey;
            SigningPrivateKey signingPrivKey = kd.signingPrivateKey;
            SigType stype = signingPubKey.getType();
            // check if the sigtype config changed
            SigType cstype = CreateRouterInfoJob.getSigTypeConfig(getContext());
            boolean sigTypeChanged = stype != cstype;
            if (sigTypeChanged && getContext().getProperty(CreateRouterInfoJob.PROP_ROUTER_SIGTYPE) == null) {
                // TODO reduce to ~3 (i.e. increase probability) in future release
                if (getContext().random().nextInt(4) > 0) {
                    sigTypeChanged = false;
                    if (_log.shouldWarn())
                        _log.warn("Deferring RI rekey from " + stype + " to " + cstype);
                }
            }
            if (sigTypeChanged || shouldRebuild(privkey)) {
                if (_us != null) {
                    Hash h = _us.getIdentity().getHash();
                    _log.logAlways(Log.WARN, "Deleting old router identity " + h.toBase64());
                    // the netdb hasn't started yet, but we want to delete the RI
                    File f = PersistentDataStore.getRouterInfoFile(getContext(), h);
                    f.delete();
                    // the banlist can be called at any time
                    getContext().banlist().banlistRouterForever(h, "Our previous identity");
                    _us = null;
                }
                if (sigTypeChanged)
                    _log.logAlways(Log.WARN, "Rebuilding RouterInfo with new signature type " + cstype);
                // windows... close before deleting
                if (fis1 != null) {
                    try {
                        fis1.close();
                    } catch (IOException ioe) {
                    }
                    fis1 = null;
                }
                rif.delete();
                rkf.delete();
                rkf2.delete();
                return;
            }
            getContext().keyManager().setKeys(pubkey, privkey, signingPubKey, signingPrivKey);
        }
    } catch (IOException ioe) {
        _log.log(Log.CRIT, "Error reading the router info from " + rif.getAbsolutePath() + " and the keys from " + rkf.getAbsolutePath(), ioe);
        _us = null;
        // windows... close before deleting
        if (fis1 != null) {
            try {
                fis1.close();
            } catch (IOException ioe2) {
            }
            fis1 = null;
        }
        rif.delete();
        rkf.delete();
        rkf2.delete();
    } catch (DataFormatException dfe) {
        _log.log(Log.CRIT, "Corrupt router info or keys at " + rif.getAbsolutePath() + " / " + rkf.getAbsolutePath(), dfe);
        _us = null;
        // windows... close before deleting
        if (fis1 != null) {
            try {
                fis1.close();
            } catch (IOException ioe) {
            }
            fis1 = null;
        }
        rif.delete();
        rkf.delete();
        rkf2.delete();
    } finally {
        if (fis1 != null)
            try {
                fis1.close();
            } catch (IOException ioe) {
            }
    }
}
Also used : SigningPublicKey(net.i2p.data.SigningPublicKey) PrivateKey(net.i2p.data.PrivateKey) SigningPrivateKey(net.i2p.data.SigningPrivateKey) RouterInfo(net.i2p.data.router.RouterInfo) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SigningPublicKey(net.i2p.data.SigningPublicKey) PublicKey(net.i2p.data.PublicKey) IOException(java.io.IOException) Hash(net.i2p.data.Hash) FileInputStream(java.io.FileInputStream) SigType(net.i2p.crypto.SigType) SigningPrivateKey(net.i2p.data.SigningPrivateKey) DataFormatException(net.i2p.data.DataFormatException) BufferedInputStream(java.io.BufferedInputStream) File(java.io.File) RouterPrivateKeyFile(net.i2p.data.router.RouterPrivateKeyFile)

Example 68 with Hash

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

the class TunnelPeerSelector method selectExplicit.

/**
 *  For debugging, also possibly for restricted routes?
 *  Needs analysis and testing
 *  @return should always be false
 */
protected List<Hash> selectExplicit(TunnelPoolSettings settings, int length) {
    String peers = null;
    Properties opts = settings.getUnknownOptions();
    if (opts != null)
        peers = opts.getProperty("explicitPeers");
    if (peers == null)
        peers = ctx.getProperty("explicitPeers");
    List<Hash> rv = new ArrayList<Hash>();
    StringTokenizer tok = new StringTokenizer(peers, ",");
    while (tok.hasMoreTokens()) {
        String peerStr = tok.nextToken();
        Hash peer = new Hash();
        try {
            peer.fromBase64(peerStr);
            if (ctx.profileOrganizer().isSelectable(peer)) {
                rv.add(peer);
            } else {
                if (log.shouldLog(Log.DEBUG))
                    log.debug("Explicit peer is not selectable: " + peerStr);
            }
        } catch (DataFormatException dfe) {
            if (log.shouldLog(Log.ERROR))
                log.error("Explicit peer is improperly formatted (" + peerStr + ")", dfe);
        }
    }
    int sz = rv.size();
    Collections.shuffle(rv, ctx.random());
    while (rv.size() > length) rv.remove(0);
    if (log.shouldLog(Log.INFO)) {
        StringBuilder buf = new StringBuilder();
        if (settings.getDestinationNickname() != null)
            buf.append("peers for ").append(settings.getDestinationNickname());
        else if (settings.getDestination() != null)
            buf.append("peers for ").append(settings.getDestination().toBase64());
        else
            buf.append("peers for exploratory ");
        if (settings.isInbound())
            buf.append(" inbound");
        else
            buf.append(" outbound");
        buf.append(" peers: ").append(rv);
        buf.append(", out of ").append(sz).append(" (not including self)");
        log.info(buf.toString());
    }
    if (settings.isInbound())
        rv.add(0, ctx.routerHash());
    else
        rv.add(ctx.routerHash());
    return rv;
}
Also used : StringTokenizer(java.util.StringTokenizer) DataFormatException(net.i2p.data.DataFormatException) ArrayList(java.util.ArrayList) Properties(java.util.Properties) Hash(net.i2p.data.Hash)

Example 69 with Hash

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

the class TunnelPeerSelector method checkTunnel.

/**
 *  Connectivity check.
 *  Check that each hop can connect to the next, including us.
 *
 *  @param tunnel ENDPOINT FIRST, GATEWAY LAST!!!!
 *  @return ok
 *  @since 0.9.34
 */
private boolean checkTunnel(List<Hash> tunnel) {
    boolean rv = true;
    for (int i = 0; i < tunnel.size() - 1; i++) {
        // order is backwards!
        Hash hf = tunnel.get(i + 1);
        Hash ht = tunnel.get(i);
        if (!canConnect(hf, ht)) {
            if (log.shouldWarn())
                log.warn("Connect check fail hop " + (i + 1) + " to " + i + " in tunnel (EP<-GW): " + DataHelper.toString(tunnel));
            // Blame them both
            // treat as a timeout in the profile
            // tunnelRejected() would set the last heard from time
            Hash us = ctx.routerHash();
            if (!hf.equals(us))
                ctx.profileManager().tunnelTimedOut(hf);
            if (!ht.equals(us))
                ctx.profileManager().tunnelTimedOut(ht);
            rv = false;
            break;
        }
    }
    return rv;
}
Also used : Hash(net.i2p.data.Hash)

Example 70 with Hash

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

the class TunnelPool method refreshLeaseSet.

/**
 * noop for outbound and exploratory
 */
void refreshLeaseSet() {
    if (_settings.isInbound() && !_settings.isExploratory()) {
        if (_log.shouldLog(Log.DEBUG))
            _log.debug(toString() + ": refreshing leaseSet on tunnel expiration (but prior to grace timeout)");
        LeaseSet ls;
        synchronized (_tunnels) {
            ls = locked_buildNewLeaseSet();
        }
        if (ls != null) {
            _context.clientManager().requestLeaseSet(_settings.getDestination(), ls);
            Set<Hash> aliases = _settings.getAliases();
            if (aliases != null && !aliases.isEmpty()) {
                for (Hash h : aliases) {
                    _context.clientManager().requestLeaseSet(h, ls);
                }
            }
        }
    }
}
Also used : LeaseSet(net.i2p.data.LeaseSet) Hash(net.i2p.data.Hash)

Aggregations

Hash (net.i2p.data.Hash)235 RouterInfo (net.i2p.data.router.RouterInfo)45 ArrayList (java.util.ArrayList)29 TunnelId (net.i2p.data.TunnelId)20 Destination (net.i2p.data.Destination)18 HashSet (java.util.HashSet)17 ConvertToHash (net.i2p.util.ConvertToHash)17 IOException (java.io.IOException)16 TunnelInfo (net.i2p.router.TunnelInfo)15 DataFormatException (net.i2p.data.DataFormatException)14 Properties (java.util.Properties)13 Date (java.util.Date)12 DatabaseEntry (net.i2p.data.DatabaseEntry)11 SessionKey (net.i2p.data.SessionKey)11 RouterAddress (net.i2p.data.router.RouterAddress)11 DatabaseStoreMessage (net.i2p.data.i2np.DatabaseStoreMessage)9 I2NPMessage (net.i2p.data.i2np.I2NPMessage)9 Job (net.i2p.router.Job)9 OutNetMessage (net.i2p.router.OutNetMessage)9 TunnelPoolSettings (net.i2p.router.TunnelPoolSettings)8