use of net.i2p.data.router.RouterInfo in project i2p.i2p by i2p.
the class SSUDemo method run.
public void run(boolean testNTCP) {
String cfgFile = "router.config";
Properties envProps = getEnv(testNTCP);
Router r = new Router(cfgFile, envProps);
r.runRouter();
_us = r.getContext();
setupHandlers();
// wait for it to warm up a bit
try {
Thread.sleep(30 * 1000);
} catch (InterruptedException ie) {
}
// now write out our ident and info
RouterInfo myInfo = _us.router().getRouterInfo();
storeMyInfo(myInfo);
// look for any other peers written to the same directory, and send each
// a single Foo message (0x0123), unless they've already contacted us first.
// this call never returns
loadPeers();
}
use of net.i2p.data.router.RouterInfo in project i2p.i2p by i2p.
the class RouterGenerator method createRouters.
private void createRouters(int numRouters, String outDir) {
File dir = new File(outDir);
if (!dir.exists())
dir.mkdirs();
int numSuccess = 0;
for (int i = 1; numSuccess < numRouters; i++) {
RouterInfo ri = createRouterInfo(i);
String hash = ri.getIdentity().getHash().toBase64();
if (!hash.startsWith("fwI")) {
System.out.print(".");
if ((i % 100) == 0)
System.out.println();
continue;
}
System.out.println("Router " + i + " created: \t" + hash);
numSuccess++;
FileOutputStream fos = null;
try {
fos = new FileOutputStream(new File(dir, "routerInfo-" + hash + ".dat"));
ri.writeBytes(fos);
} catch (Exception e) {
System.err.println("Error writing router - " + e.getMessage());
e.printStackTrace();
return;
} finally {
if (fos != null)
try {
fos.close();
} catch (Exception e) {
}
}
}
}
use of net.i2p.data.router.RouterInfo 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());
}
}
use of net.i2p.data.router.RouterInfo in project i2p.i2p by i2p.
the class PublishLocalRouterInfoJob method runJob.
public void runJob() {
long last = getContext().netDb().getLastRouterInfoPublishTime();
long now = getContext().clock().now();
if (last + MIN_PUBLISH_DELAY > now) {
long delay = getDelay();
requeue(last + delay - now);
return;
}
RouterInfo oldRI = getContext().router().getRouterInfo();
if (_log.shouldLog(Log.DEBUG))
_log.debug("Old routerInfo contains " + oldRI.getAddresses().size() + " addresses and " + oldRI.getOptionsMap().size() + " options");
try {
List<RouterAddress> oldAddrs = new ArrayList<RouterAddress>(oldRI.getAddresses());
List<RouterAddress> newAddrs = getContext().commSystem().createAddresses();
int count = _runCount.incrementAndGet();
RouterInfo ri = new RouterInfo(oldRI);
if (_notFirstTime && (count % 4) != 0 && oldAddrs.size() == newAddrs.size()) {
// 3 times out of 4, we don't republish if everything is the same...
// If something changed, including the cost, then publish,
// otherwise don't.
String newcaps = getContext().router().getCapabilities();
boolean different = !oldRI.getCapabilities().equals(newcaps);
if (!different) {
Comparator<RouterAddress> comp = new AddrComparator();
Collections.sort(oldAddrs, comp);
Collections.sort(newAddrs, comp);
for (int i = 0; i < oldAddrs.size(); i++) {
// deepEquals() includes cost
if (!oldAddrs.get(i).deepEquals(newAddrs.get(i))) {
different = true;
break;
}
}
if (!different) {
if (_log.shouldLog(Log.INFO))
_log.info("Not republishing early because costs and caps and addresses are the same");
requeue(getDelay());
return;
}
}
if (_log.shouldLog(Log.INFO))
_log.info("Republishing early because addresses or costs or caps have changed -" + " oldCaps: " + oldRI.getCapabilities() + " newCaps: " + newcaps + " old:\n" + oldAddrs + "\nnew:\n" + newAddrs);
}
ri.setPublished(getContext().clock().now());
Properties stats = getContext().statPublisher().publishStatistics();
ri.setOptions(stats);
ri.setAddresses(newAddrs);
SigningPrivateKey key = getContext().keyManager().getSigningPrivateKey();
if (key == null) {
_log.log(Log.CRIT, "Internal error - signing private key not known? rescheduling publish for 30s");
requeue(30 * 1000);
return;
}
ri.sign(key);
getContext().router().setRouterInfo(ri);
if (_log.shouldLog(Log.INFO))
_log.info("Newly updated routerInfo is published with " + stats.size() + "/" + ri.getOptionsMap().size() + " options on " + new Date(ri.getPublished()));
try {
// This won't really publish until the netdb is initialized.
getContext().netDb().publish(ri);
} catch (IllegalArgumentException iae) {
_log.log(Log.CRIT, "Error publishing our identity - corrupt? Restart required", iae);
getContext().router().rebuildNewIdentity();
}
} catch (DataFormatException dfe) {
_log.error("Error signing the updated local router info!", dfe);
}
if (_notFirstTime) {
requeue(getDelay());
} else {
requeue(FIRST_TIME_DELAY);
_notFirstTime = true;
}
}
use of net.i2p.data.router.RouterInfo in project i2p.i2p by i2p.
the class KademliaNetworkDatabaseFacade method restart.
public synchronized void restart() {
_dbDir = _context.router().getConfigSetting(PROP_DB_DIR);
if (_dbDir == null) {
_log.info("No DB dir specified [" + PROP_DB_DIR + "], using [" + DEFAULT_DB_DIR + "]");
_dbDir = DEFAULT_DB_DIR;
}
_ds.restart();
_exploreKeys.clear();
_initialized = true;
RouterInfo ri = _context.router().getRouterInfo();
publish(ri);
}
Aggregations