Search in sources :

Example 6 with Lease

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

the class LocalClientConnectionRunner method requestLeaseSet.

/**
 *  Just send the message directly,
 *  don't instantiate a RequestLeaseSetJob
 */
@Override
void requestLeaseSet(Hash h, LeaseSet set, long expirationTime, Job onCreateJob, Job onFailedJob) {
    RequestVariableLeaseSetMessage msg = new RequestVariableLeaseSetMessage();
    msg.setSessionId(getSessionId(h));
    for (int i = 0; i < set.getLeaseCount(); i++) {
        Lease lease = set.getLease(i);
        msg.addEndpoint(lease);
    }
    try {
        doSend(msg);
    } catch (I2CPMessageException ime) {
        ime.printStackTrace();
    }
}
Also used : I2CPMessageException(net.i2p.data.i2cp.I2CPMessageException) Lease(net.i2p.data.Lease) RequestVariableLeaseSetMessage(net.i2p.data.i2cp.RequestVariableLeaseSetMessage)

Example 7 with Lease

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

the class LocalClientMessageEventListener method startCreateSessionJob.

/**
 *  Immediately send a fake leaseset
 */
@Override
protected void startCreateSessionJob(SessionConfig config) {
    long exp = _context.clock().now() + 10 * 60 * 1000;
    LeaseSet ls = new LeaseSet();
    Lease lease = new Lease();
    lease.setGateway(Hash.FAKE_HASH);
    TunnelId id = new TunnelId(1);
    lease.setTunnelId(id);
    Date date = new Date(exp);
    lease.setEndDate(date);
    ls.addLease(lease);
    _runner.requestLeaseSet(config.getDestination().calculateHash(), ls, exp, null, null);
}
Also used : LeaseSet(net.i2p.data.LeaseSet) Lease(net.i2p.data.Lease) TunnelId(net.i2p.data.TunnelId) Date(java.util.Date)

Example 8 with Lease

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

the class RequestLeaseSetJob method runJob.

public void runJob() {
    if (_runner.isDead())
        return;
    LeaseSet requested = _requestState.getRequested();
    long endTime = requested.getEarliestLeaseDate();
    // Add a small number of ms (0 to MAX_FUDGE) that increases as we approach the expire time.
    // Since the earliest date functions as a version number,
    // this will force the floodfill to flood each new version;
    // otherwise it won't if the earliest time hasn't changed.
    long fudge = MAX_FUDGE - ((endTime - getContext().clock().now()) / (10 * 60 * 1000 / MAX_FUDGE));
    // if (_log.shouldLog(Log.DEBUG))
    // _log.debug("Adding fudge " + fudge);
    endTime += fudge;
    SessionId id = _runner.getSessionId(requested.getDestination().calculateHash());
    if (id == null) {
        _runner.failLeaseRequest(_requestState);
        return;
    }
    I2CPMessage msg;
    if (getContext().getProperty(PROP_VARIABLE, DFLT_VARIABLE) && (_runner instanceof QueuedClientConnectionRunner || RequestVariableLeaseSetMessage.isSupported(_runner.getClientVersion()))) {
        // new style - leases will have individual expirations
        RequestVariableLeaseSetMessage rmsg = new RequestVariableLeaseSetMessage();
        rmsg.setSessionId(id);
        for (int i = 0; i < requested.getLeaseCount(); i++) {
            Lease lease = requested.getLease(i);
            if (lease.getEndDate().getTime() < endTime) {
                // don't modify old object, we don't know where it came from
                Lease nl = new Lease();
                nl.setGateway(lease.getGateway());
                nl.setTunnelId(lease.getTunnelId());
                nl.setEndDate(new Date(endTime));
                lease = nl;
            // if (_log.shouldLog(Log.INFO))
            // _log.info("Adjusted end date to " + endTime + " for " + lease);
            }
            rmsg.addEndpoint(lease);
        }
        msg = rmsg;
    } else {
        // old style - all leases will have same expiration
        RequestLeaseSetMessage rmsg = new RequestLeaseSetMessage();
        Date end = new Date(endTime);
        rmsg.setEndDate(end);
        rmsg.setSessionId(id);
        for (int i = 0; i < requested.getLeaseCount(); i++) {
            Lease lease = requested.getLease(i);
            rmsg.addEndpoint(lease.getGateway(), lease.getTunnelId());
        }
        msg = rmsg;
    }
    try {
        // _runner.setLeaseRequest(state);
        _runner.doSend(msg);
        getContext().jobQueue().addJob(new CheckLeaseRequestStatus());
    } catch (I2CPMessageException ime) {
        getContext().statManager().addRateData("client.requestLeaseSetDropped", 1);
        _log.error("Error sending I2CP message requesting the lease set", ime);
        _requestState.setIsSuccessful(false);
        if (_requestState.getOnFailed() != null)
            RequestLeaseSetJob.this.getContext().jobQueue().addJob(_requestState.getOnFailed());
        _runner.failLeaseRequest(_requestState);
    // Don't disconnect, the tunnel will retry
    // _runner.disconnectClient("I2CP error requesting leaseSet");
    }
}
Also used : LeaseSet(net.i2p.data.LeaseSet) I2CPMessageException(net.i2p.data.i2cp.I2CPMessageException) Lease(net.i2p.data.Lease) I2CPMessage(net.i2p.data.i2cp.I2CPMessage) RequestVariableLeaseSetMessage(net.i2p.data.i2cp.RequestVariableLeaseSetMessage) SessionId(net.i2p.data.i2cp.SessionId) Date(java.util.Date) RequestLeaseSetMessage(net.i2p.data.i2cp.RequestLeaseSetMessage)

Example 9 with Lease

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

the class OutboundClientMessageOneShotJob method getNextLease.

/**
 *  Choose a lease from his leaseset to send the message to. Sets _lease.
 *  Sets _wantACK if it's new or changed.
 *  @return success
 */
private boolean getNextLease() {
    // set in runJob if found locally
    if (_leaseSet == null) {
        _leaseSet = getContext().netDb().lookupLeaseSetLocally(_to.calculateHash());
        if (_leaseSet == null) {
            // shouldn't happen
            if (_log.shouldLog(Log.WARN))
                _log.warn(getJobId() + ": Lookup locally didn't find the leaseSet for " + _toString);
            return false;
        }
    }
    // Use the same lease if it's still good
    // Even if _leaseSet changed, _leaseSet.getEncryptionKey() didn't...
    _lease = _cache.leaseCache.get(_hashPair);
    if (_lease != null) {
        // if outbound tunnel length == 0 && lease.firsthop.isBacklogged() don't use it ??
        if (!_lease.isExpired(Router.CLOCK_FUDGE_FACTOR / 4)) {
            // it (due to failure for example) we won't continue to use it.
            for (int i = 0; i < _leaseSet.getLeaseCount(); i++) {
                Lease lease = _leaseSet.getLease(i);
                // if (_lease.equals(lease)) {
                if (_lease.getTunnelId().equals(lease.getTunnelId()) && _lease.getGateway().equals(lease.getGateway())) {
                    if (_log.shouldLog(Log.INFO))
                        _log.info(getJobId() + ": Found in cache - lease for " + _toString);
                    return true;
                }
            }
        }
        // remove only if still equal to _lease (concurrent)
        _cache.leaseCache.remove(_hashPair, _lease);
        if (_log.shouldLog(Log.INFO))
            _log.info(getJobId() + ": Expired from cache - lease for " + _toString);
    }
    // get the possible leases
    List<Lease> leases = new ArrayList<Lease>(_leaseSet.getLeaseCount());
    // first try to get ones that really haven't expired
    for (int i = 0; i < _leaseSet.getLeaseCount(); i++) {
        Lease lease = _leaseSet.getLease(i);
        if (!lease.isExpired(Router.CLOCK_FUDGE_FACTOR / 4))
            leases.add(lease);
    }
    if (leases.isEmpty()) {
        // try again with a fudge factor
        for (int i = 0; i < _leaseSet.getLeaseCount(); i++) {
            Lease lease = _leaseSet.getLease(i);
            if (!lease.isExpired(Router.CLOCK_FUDGE_FACTOR))
                leases.add(lease);
        }
    }
    if (leases.isEmpty()) {
        if (_log.shouldLog(Log.INFO))
            _log.info(getJobId() + ": No leases found from: " + _leaseSet);
        return false;
    }
    // randomize the ordering (so leases with equal # of failures per next
    // sort are randomly ordered)
    Collections.shuffle(leases, getContext().random());
    // Avoid a lease on a gateway we think is unreachable, if possible
    for (int i = 0; i < leases.size(); i++) {
        Lease l = leases.get(i);
        /**
         ***  Anonymity concerns with this, as the dest could act unreachable just to us, then
         ***  look at our lease selection.
         ***  Let's just look at whether the gw thinks it is unreachable instead -
         ***  unfortunately the "U" is rarely seen.
         *            if (!getContext().commSystem().wasUnreachable(l.getGateway())) {
         **
         */
        RouterInfo ri = getContext().netDb().lookupRouterInfoLocally(l.getGateway());
        if (ri == null || ri.getCapabilities().indexOf(Router.CAPABILITY_UNREACHABLE) < 0) {
            _lease = l;
            break;
        }
        if (_log.shouldLog(Log.WARN))
            _log.warn(getJobId() + ": Skipping unreachable gateway " + l.getGateway() + " for " + _toString);
    }
    if (_lease == null) {
        _lease = leases.get(0);
        if (_log.shouldLog(Log.WARN))
            _log.warn(getJobId() + ": All leases are unreachable for " + _toString);
    }
    _cache.leaseCache.put(_hashPair, _lease);
    if (_log.shouldLog(Log.INFO))
        _log.info(getJobId() + ": Added to cache - lease for " + _toString);
    _wantACK = true;
    return true;
}
Also used : Lease(net.i2p.data.Lease) RouterInfo(net.i2p.data.router.RouterInfo) ArrayList(java.util.ArrayList)

Aggregations

Lease (net.i2p.data.Lease)9 LeaseSet (net.i2p.data.LeaseSet)6 Date (java.util.Date)4 Hash (net.i2p.data.Hash)2 TunnelId (net.i2p.data.TunnelId)2 I2CPMessageException (net.i2p.data.i2cp.I2CPMessageException)2 RequestLeaseSetMessage (net.i2p.data.i2cp.RequestLeaseSetMessage)2 RequestVariableLeaseSetMessage (net.i2p.data.i2cp.RequestVariableLeaseSetMessage)2 BigInteger (java.math.BigInteger)1 DecimalFormat (java.text.DecimalFormat)1 ArrayList (java.util.ArrayList)1 TreeSet (java.util.TreeSet)1 DataFormatException (net.i2p.data.DataFormatException)1 Destination (net.i2p.data.Destination)1 I2CPMessage (net.i2p.data.i2cp.I2CPMessage)1 SessionId (net.i2p.data.i2cp.SessionId)1 RouterInfo (net.i2p.data.router.RouterInfo)1 TunnelInfo (net.i2p.router.TunnelInfo)1 TunnelPoolSettings (net.i2p.router.TunnelPoolSettings)1 FloodfillNetworkDatabaseFacade (net.i2p.router.networkdb.kademlia.FloodfillNetworkDatabaseFacade)1