Search in sources :

Example 16 with LeaseSet

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

the class KademliaNetworkDatabaseFacade method processStoreFailure.

/**
 *  If the validate fails, call this
 *  to determine if it was because of unsupported crypto.
 *
 *  If so, this will banlist-forever the router hash or permanently negative cache the dest hash,
 *  and then throw the exception. Otherwise it does nothing.
 *
 *  @throws UnsupportedCryptoException if that's why it failed.
 *  @since 0.9.16
 */
private void processStoreFailure(Hash h, DatabaseEntry entry) throws UnsupportedCryptoException {
    if (entry.getHash().equals(h)) {
        if (entry.getType() == DatabaseEntry.KEY_TYPE_LEASESET) {
            LeaseSet ls = (LeaseSet) entry;
            Destination d = ls.getDestination();
            Certificate c = d.getCertificate();
            if (c.getCertificateType() == Certificate.CERTIFICATE_TYPE_KEY) {
                try {
                    KeyCertificate kc = c.toKeyCertificate();
                    SigType type = kc.getSigType();
                    if (type == null || !type.isAvailable() || type.getBaseAlgorithm() == SigAlgo.RSA) {
                        failPermanently(d);
                        String stype = (type != null) ? type.toString() : Integer.toString(kc.getSigTypeCode());
                        if (_log.shouldLog(Log.WARN))
                            _log.warn("Unsupported sig type " + stype + " for destination " + h);
                        throw new UnsupportedCryptoException("Sig type " + stype);
                    }
                } catch (DataFormatException dfe) {
                }
            }
        } else if (entry.getType() == DatabaseEntry.KEY_TYPE_ROUTERINFO) {
            RouterInfo ri = (RouterInfo) entry;
            RouterIdentity id = ri.getIdentity();
            Certificate c = id.getCertificate();
            if (c.getCertificateType() == Certificate.CERTIFICATE_TYPE_KEY) {
                try {
                    KeyCertificate kc = c.toKeyCertificate();
                    SigType type = kc.getSigType();
                    if (type == null || !type.isAvailable()) {
                        String stype = (type != null) ? type.toString() : Integer.toString(kc.getSigTypeCode());
                        _context.banlist().banlistRouterForever(h, "Unsupported signature type " + stype);
                        if (_log.shouldLog(Log.WARN))
                            _log.warn("Unsupported sig type " + stype + " for router " + h);
                        throw new UnsupportedCryptoException("Sig type " + stype);
                    }
                } catch (DataFormatException dfe) {
                }
            }
        }
    }
    if (_log.shouldLog(Log.WARN))
        _log.warn("Verify fail, cause unknown: " + entry);
}
Also used : LeaseSet(net.i2p.data.LeaseSet) Destination(net.i2p.data.Destination) KeyCertificate(net.i2p.data.KeyCertificate) DataFormatException(net.i2p.data.DataFormatException) RouterInfo(net.i2p.data.router.RouterInfo) RouterIdentity(net.i2p.data.router.RouterIdentity) SigType(net.i2p.crypto.SigType) Certificate(net.i2p.data.Certificate) KeyCertificate(net.i2p.data.KeyCertificate)

Example 17 with LeaseSet

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

the class RepublishLeaseSetJob method runJob.

public void runJob() {
    if (!getContext().clientManager().shouldPublishLeaseSet(_dest))
        return;
    try {
        if (getContext().clientManager().isLocal(_dest)) {
            LeaseSet ls = _facade.lookupLeaseSetLocally(_dest);
            if (ls != null) {
                if (!ls.isCurrent(Router.CLOCK_FUDGE_FACTOR)) {
                    if (_log.shouldLog(Log.WARN))
                        _log.warn("Not publishing a LOCAL lease that isn't current - " + _dest, new Exception("Publish expired LOCAL lease?"));
                } else {
                    if (_log.shouldLog(Log.INFO))
                        _log.info("Publishing " + ls);
                    getContext().statManager().addRateData("netDb.republishLeaseSetCount", 1);
                    _facade.sendStore(_dest, ls, null, new OnRepublishFailure(getContext(), this), REPUBLISH_LEASESET_TIMEOUT, null);
                    _lastPublished = getContext().clock().now();
                // getContext().jobQueue().addJob(new StoreJob(getContext(), _facade, _dest, ls, new OnSuccess(getContext()), new OnFailure(getContext()), REPUBLISH_LEASESET_TIMEOUT));
                }
            } else {
                if (_log.shouldLog(Log.WARN))
                    _log.warn("Client " + _dest + " is local, but we can't find a valid LeaseSet?  perhaps its being rebuilt?");
            }
            // }
            return;
        } else {
            if (_log.shouldLog(Log.INFO))
                _log.info("Client " + _dest + " is no longer local, so no more republishing their leaseSet");
        }
        _facade.stopPublishing(_dest);
    } catch (RuntimeException re) {
        if (_log.shouldLog(Log.ERROR))
            _log.error("Uncaught error republishing the leaseSet", re);
        _facade.stopPublishing(_dest);
        throw re;
    }
}
Also used : LeaseSet(net.i2p.data.LeaseSet)

Example 18 with LeaseSet

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

the class CreateLeaseSetMessage method doReadMessage.

@Override
protected void doReadMessage(InputStream in, int size) throws I2CPMessageException, IOException {
    try {
        _sessionId = new SessionId();
        _sessionId.readBytes(in);
        // Revocation is unimplemented.
        // As the SPK comes before the LeaseSet, we don't know the key type.
        // We could have some sort of callback or state setting so we get the
        // expected type from the session. But for now, we just assume it's 20 bytes.
        // Clients outside router context should throw in a dummy 20 bytes.
        _signingPrivateKey = new SigningPrivateKey();
        _signingPrivateKey.readBytes(in);
        _privateKey = new PrivateKey();
        _privateKey.readBytes(in);
        _leaseSet = new LeaseSet();
        _leaseSet.readBytes(in);
    } catch (DataFormatException dfe) {
        throw new I2CPMessageException("Error reading the CreateLeaseSetMessage", dfe);
    }
}
Also used : SigningPrivateKey(net.i2p.data.SigningPrivateKey) LeaseSet(net.i2p.data.LeaseSet) SigningPrivateKey(net.i2p.data.SigningPrivateKey) PrivateKey(net.i2p.data.PrivateKey) DataFormatException(net.i2p.data.DataFormatException)

Example 19 with LeaseSet

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

the class TunnelPool method locked_buildNewLeaseSet.

/**
 * Build a leaseSet with the required tunnels that aren't about to expire.
 * Caller must synchronize on _tunnels.
 *
 * @return null on failure
 */
protected LeaseSet locked_buildNewLeaseSet() {
    if (!_alive)
        return null;
    int wanted = Math.min(_settings.getQuantity(), LeaseSet.MAX_LEASES);
    if (_tunnels.size() < wanted) {
        if (_log.shouldLog(Log.WARN))
            _log.warn(toString() + ": Not enough tunnels (" + _tunnels.size() + ", wanted " + wanted + ")");
        // see comment below
        if (_tunnels.isEmpty())
            return null;
    }
    // + _settings.getRebuildPeriod();
    long expireAfter = _context.clock().now();
    TunnelInfo zeroHopTunnel = null;
    Lease zeroHopLease = null;
    TreeSet<Lease> leases = new TreeSet<Lease>(new LeaseComparator());
    for (int i = 0; i < _tunnels.size(); i++) {
        TunnelInfo tunnel = _tunnels.get(i);
        if (tunnel.getExpiration() <= expireAfter)
            // expires too soon, skip it
            continue;
        if (tunnel.getLength() <= 1) {
            // Keep only the one that expires the latest.
            if (zeroHopTunnel != null) {
                if (zeroHopTunnel.getExpiration() > tunnel.getExpiration())
                    continue;
                if (zeroHopLease != null)
                    leases.remove(zeroHopLease);
            }
            zeroHopTunnel = tunnel;
        }
        TunnelId inId = tunnel.getReceiveTunnelId(0);
        Hash gw = tunnel.getPeer(0);
        if ((inId == null) || (gw == null)) {
            _log.error(toString() + ": broken? tunnel has no inbound gateway/tunnelId? " + tunnel);
            continue;
        }
        Lease lease = new Lease();
        // bugfix
        // ExpireJob reduces the expiration, which causes a 2nd leaseset with the same lease
        // to have an earlier expiration, so it isn't stored.
        // Get the "real" expiration from the gateway hop config,
        // HopConfig expirations are the same as the "real" expiration and don't change
        // see configureNewTunnel()
        lease.setEndDate(new Date(((TunnelCreatorConfig) tunnel).getConfig(0).getExpiration()));
        lease.setTunnelId(inId);
        lease.setGateway(gw);
        leases.add(lease);
        // remember in case we want to remove it for a later-expiring zero-hopper
        if (tunnel.getLength() <= 1)
            zeroHopLease = lease;
    }
    // Do we want a config option for this, or are there times when we shouldn't do this?
    if (leases.size() < wanted) {
        if (_log.shouldLog(Log.WARN))
            _log.warn(toString() + ": Not enough leases (" + leases.size() + ", wanted " + wanted + ")");
        if (leases.isEmpty())
            return null;
    }
    LeaseSet ls = new LeaseSet();
    Iterator<Lease> iter = leases.iterator();
    int count = Math.min(leases.size(), wanted);
    for (int i = 0; i < count; i++) ls.addLease(iter.next());
    if (_log.shouldLog(Log.INFO))
        _log.info(toString() + ": built new leaseSet: " + ls);
    return ls;
}
Also used : LeaseSet(net.i2p.data.LeaseSet) Lease(net.i2p.data.Lease) TreeSet(java.util.TreeSet) TunnelInfo(net.i2p.router.TunnelInfo) Hash(net.i2p.data.Hash) TunnelId(net.i2p.data.TunnelId) Date(java.util.Date)

Example 20 with LeaseSet

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

the class AliasedTunnelPool method locked_buildNewLeaseSet.

@Override
protected LeaseSet locked_buildNewLeaseSet() {
    LeaseSet ls = _context.netDb().lookupLeaseSetLocally(_aliasOf.getSettings().getDestination());
    if (ls == null)
        return null;
    // copy everything so it isn't corrupted
    LeaseSet rv = new LeaseSet();
    for (int i = 0; i < ls.getLeaseCount(); i++) {
        Lease old = ls.getLease(i);
        Lease lease = new Lease();
        lease.setEndDate(old.getEndDate());
        lease.setTunnelId(old.getTunnelId());
        lease.setGateway(old.getGateway());
        rv.addLease(lease);
    }
    return rv;
}
Also used : LeaseSet(net.i2p.data.LeaseSet) Lease(net.i2p.data.Lease)

Aggregations

LeaseSet (net.i2p.data.LeaseSet)29 Date (java.util.Date)7 Hash (net.i2p.data.Hash)7 Lease (net.i2p.data.Lease)6 RouterInfo (net.i2p.data.router.RouterInfo)6 Destination (net.i2p.data.Destination)5 DatabaseEntry (net.i2p.data.DatabaseEntry)4 DataFormatException (net.i2p.data.DataFormatException)3 TunnelId (net.i2p.data.TunnelId)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 I2CPMessageException (net.i2p.data.i2cp.I2CPMessageException)2 RequestLeaseSetMessage (net.i2p.data.i2cp.RequestLeaseSetMessage)2 RequestVariableLeaseSetMessage (net.i2p.data.i2cp.RequestVariableLeaseSetMessage)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 EOFException (java.io.EOFException)1 BigInteger (java.math.BigInteger)1 DecimalFormat (java.text.DecimalFormat)1 Collection (java.util.Collection)1