Search in sources :

Example 86 with Hash

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

the class HandleDatabaseLookupMessageJob method runJob.

public void runJob() {
    if (_log.shouldLog(Log.DEBUG))
        _log.debug("Handling database lookup message for " + _message.getSearchKey());
    Hash fromKey = _message.getFrom();
    if (_log.shouldLog(Log.DEBUG)) {
        if (_message.getReplyTunnel() != null)
            _log.debug("dbLookup received with replies going to " + fromKey + " (tunnel " + _message.getReplyTunnel() + ")");
    }
    // If we are hidden we should not get queries, log and return
    if (getContext().router().isHidden()) {
        if (_log.shouldLog(Log.WARN)) {
            _log.warn("Uninvited dbLookup received with replies going to " + fromKey + " (tunnel " + _message.getReplyTunnel() + ")");
        }
        return;
    }
    // i2pd bug?
    if (_message.getSearchKey().equals(Hash.FAKE_HASH)) {
        if (_log.shouldWarn())
            _log.warn("Zero lookup", new Exception());
        getContext().statManager().addRateData("netDb.DLMAllZeros", 1);
        return;
    }
    DatabaseLookupMessage.Type lookupType = _message.getSearchType();
    // only lookup once, then cast to correct type
    DatabaseEntry dbe = getContext().netDb().lookupLocally(_message.getSearchKey());
    if (dbe != null && dbe.getType() == DatabaseEntry.KEY_TYPE_LEASESET && (lookupType == DatabaseLookupMessage.Type.ANY || lookupType == DatabaseLookupMessage.Type.LS)) {
        LeaseSet ls = (LeaseSet) dbe;
        // We have to be very careful here to decide whether or not to send out the leaseSet,
        // to avoid anonymity vulnerabilities.
        // As this is complex, lots of comments follow...
        boolean isLocal = getContext().clientManager().isLocal(ls.getDestination());
        boolean shouldPublishLocal = isLocal && getContext().clientManager().shouldPublishLeaseSet(_message.getSearchKey());
        // true for received in a DatabaseStoreMessage unsolicited
        if (ls.getReceivedAsPublished()) {
            // Local leasesets are not handled here
            if (_log.shouldLog(Log.INFO))
                _log.info("We have the published LS " + _message.getSearchKey() + ", answering query");
            getContext().statManager().addRateData("netDb.lookupsMatchedReceivedPublished", 1);
            sendData(_message.getSearchKey(), ls, fromKey, _message.getReplyTunnel());
        } else if (shouldPublishLocal && answerAllQueries()) {
            // We are floodfill, and this is our local leaseset, and we publish it.
            // Only send it out if it is in our estimated keyspace.
            // For this, we do NOT use their dontInclude list as it can't be trusted
            // (i.e. it could mess up the closeness calculation)
            Set<Hash> closestHashes = getContext().netDb().findNearestRouters(_message.getSearchKey(), CLOSENESS_THRESHOLD, null);
            if (weAreClosest(closestHashes)) {
                // It's in our keyspace, so give it to them
                if (_log.shouldLog(Log.INFO))
                    _log.info("We have local LS " + _message.getSearchKey() + ", answering query, in our keyspace");
                getContext().statManager().addRateData("netDb.lookupsMatchedLocalClosest", 1);
                sendData(_message.getSearchKey(), ls, fromKey, _message.getReplyTunnel());
            } else {
                // Lie, pretend we don't have it
                if (_log.shouldLog(Log.INFO))
                    _log.info("We have local LS " + _message.getSearchKey() + ", NOT answering query, out of our keyspace");
                getContext().statManager().addRateData("netDb.lookupsMatchedLocalNotClosest", 1);
                Set<Hash> routerHashSet = getNearestRouters(lookupType);
                sendClosest(_message.getSearchKey(), routerHashSet, fromKey, _message.getReplyTunnel());
            }
        } else {
            // Lie, pretend we don't have it
            if (_log.shouldLog(Log.INFO))
                _log.info("We have LS " + _message.getSearchKey() + ", NOT answering query - local? " + isLocal + " shouldPublish? " + shouldPublishLocal + " RAP? " + ls.getReceivedAsPublished() + " RAR? " + ls.getReceivedAsReply());
            getContext().statManager().addRateData("netDb.lookupsMatchedRemoteNotClosest", 1);
            Set<Hash> routerHashSet = getNearestRouters(lookupType);
            sendClosest(_message.getSearchKey(), routerHashSet, fromKey, _message.getReplyTunnel());
        }
    } else if (dbe != null && dbe.getType() == DatabaseEntry.KEY_TYPE_ROUTERINFO && lookupType != DatabaseLookupMessage.Type.LS) {
        RouterInfo info = (RouterInfo) dbe;
        if (info.isCurrent(EXPIRE_DELAY)) {
            if ((info.isHidden()) || (isUnreachable(info) && !publishUnreachable())) {
                if (_log.shouldLog(Log.DEBUG))
                    _log.debug("Not answering a query for a netDb peer who isn't reachable");
                Set<Hash> us = Collections.singleton(getContext().routerHash());
                sendClosest(_message.getSearchKey(), us, fromKey, _message.getReplyTunnel());
            } else {
                // send that routerInfo to the _message.getFromHash peer
                if (_log.shouldLog(Log.DEBUG))
                    _log.debug("We do have key " + _message.getSearchKey() + " locally as a router info.  sending to " + fromKey);
                sendData(_message.getSearchKey(), info, fromKey, _message.getReplyTunnel());
            }
        } else {
            // expired locally - return closest peer hashes
            Set<Hash> routerHashSet = getNearestRouters(lookupType);
            if (_log.shouldLog(Log.DEBUG))
                _log.debug("Expired " + _message.getSearchKey() + " locally.  sending back " + routerHashSet.size() + " peers to " + fromKey);
            sendClosest(_message.getSearchKey(), routerHashSet, fromKey, _message.getReplyTunnel());
        }
    } else {
        // not found locally - return closest peer hashes
        Set<Hash> routerHashSet = getNearestRouters(lookupType);
        if (_log.shouldLog(Log.DEBUG))
            _log.debug("We do not have key " + _message.getSearchKey() + " locally.  sending back " + routerHashSet.size() + " peers to " + fromKey);
        sendClosest(_message.getSearchKey(), routerHashSet, fromKey, _message.getReplyTunnel());
    }
}
Also used : DatabaseLookupMessage(net.i2p.data.i2np.DatabaseLookupMessage) LeaseSet(net.i2p.data.LeaseSet) LeaseSet(net.i2p.data.LeaseSet) Set(java.util.Set) HashSet(java.util.HashSet) RouterInfo(net.i2p.data.router.RouterInfo) DatabaseEntry(net.i2p.data.DatabaseEntry) Hash(net.i2p.data.Hash)

Example 87 with Hash

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

the class ExpireLeasesJob method runJob.

public void runJob() {
    Set<Hash> toExpire = selectKeysToExpire();
    _log.info("Leases to expire: " + toExpire);
    for (Hash key : toExpire) {
        _facade.fail(key);
    // _log.info("Lease " + key + " is expiring, so lets look for it again", new Exception("Expire and search"));
    // _facade.lookupLeaseSet(key, null, null, RERUN_DELAY_MS);
    }
    // _facade.queueForExploration(toExpire); // don't do explicit searches, just explore passively
    requeue(RERUN_DELAY_MS);
}
Also used : Hash(net.i2p.data.Hash)

Example 88 with Hash

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

the class KademliaNetworkDatabaseFacade method publish.

/**
 * Stores to local db only.
 * Overridden in FNDF to actually send to the floodfills.
 * @throws IllegalArgumentException if the local router info is invalid
 */
public void publish(RouterInfo localRouterInfo) throws IllegalArgumentException {
    if (!_initialized)
        return;
    if (_context.router().gracefulShutdownInProgress())
        return;
    // DE-nied!
    if (_context.router().isHidden())
        return;
    Hash h = localRouterInfo.getIdentity().getHash();
    store(h, localRouterInfo);
}
Also used : Hash(net.i2p.data.Hash)

Example 89 with Hash

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

the class RefreshRoutersJob method runJob.

public void runJob() {
    if (_facade.isInitialized()) {
        if (_routers == null) {
            // make a list of all routers, floodfill first
            _routers = _facade.getFloodfillPeers();
            int ff = _routers.size();
            Set<Hash> all = _facade.getAllRouters();
            all.removeAll(_routers);
            int non = all.size();
            _routers.addAll(all);
            if (_log.shouldLog(Log.INFO))
                _log.info("To check: " + ff + " floodfills and " + non + " non-floodfills");
        }
        if (_routers.isEmpty()) {
            if (_log.shouldLog(Log.INFO))
                _log.info("Finished");
            return;
        }
        long expire = getContext().clock().now() - EXPIRE;
        for (Iterator<Hash> iter = _routers.iterator(); iter.hasNext(); ) {
            Hash h = iter.next();
            iter.remove();
            if (h.equals(getContext().routerHash()))
                continue;
            if (_log.shouldLog(Log.DEBUG))
                _log.debug("Checking " + h);
            RouterInfo ri = _facade.lookupRouterInfoLocally(h);
            if (ri == null)
                continue;
            if (ri.getPublished() < expire) {
                if (_log.shouldLog(Log.INFO))
                    _log.info("Refreshing " + h);
                _facade.search(h, null, null, 15 * 1000, false);
                break;
            }
        }
    }
    requeue(RERUN_DELAY_MS);
}
Also used : RouterInfo(net.i2p.data.router.RouterInfo) Hash(net.i2p.data.Hash)

Example 90 with Hash

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

the class ProfileOrganizer method selectPeersLocallyUnreachable.

/**
 * Get the peers the transport layer thinks are unreachable, and
 * add in the peers with the SSU peer testing bug,
 * and peers requiring introducers.
 */
public List<Hash> selectPeersLocallyUnreachable() {
    List<Hash> n;
    int count;
    getReadLock();
    try {
        count = _notFailingPeers.size();
        n = new ArrayList<Hash>(_notFailingPeers.keySet());
    } finally {
        releaseReadLock();
    }
    List<Hash> l = new ArrayList<Hash>(count / 4);
    for (Hash peer : n) {
        if (_context.commSystem().wasUnreachable(peer))
            l.add(peer);
        else {
            // Blacklist <= 0.6.1.32 SSU-only peers, they don't know if they are unreachable,
            // and we may not know either if they contacted us first, so assume they are.
            // Also blacklist all peers requiring SSU introducers, because either
            // a) it's slow; or
            // b) it doesn't work very often; or
            // c) in the event they are advertising NTCP, it probably won't work because
            // they probably don't have a TCP hole punched in their firewall either.
            RouterInfo info = _context.netDb().lookupRouterInfoLocally(peer);
            if (info != null) {
                String v = info.getVersion();
                // this only works if there is no 0.6.1.34!
                if ((!v.equals("0.6.1.33")) && v.startsWith("0.6.1.") && info.getTargetAddress("NTCP") == null)
                    l.add(peer);
                else {
                    RouterAddress ra = info.getTargetAddress("SSU");
                    // as long as they have NTCP
                    if (ra == null) {
                        if (info.getTargetAddress("NTCP") == null)
                            l.add(peer);
                        continue;
                    }
                    // This is the quick way of doing UDPAddress.getIntroducerCount() > 0
                    if (ra.getOption("ihost0") != null)
                        l.add(peer);
                }
            }
        }
    }
    if (_log.shouldLog(Log.DEBUG))
        _log.debug("Unreachable: " + l);
    return l;
}
Also used : RouterInfo(net.i2p.data.router.RouterInfo) ArrayList(java.util.ArrayList) RouterAddress(net.i2p.data.router.RouterAddress) 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