when pinging other peers, be able to select the right IP option

This commit is contained in:
Michael Peter Christen 2014-10-01 03:47:57 +02:00
parent 65e6ae52fb
commit fe917deb2d
3 changed files with 25 additions and 31 deletions

View File

@ -208,10 +208,10 @@ public class Network {
final ConcurrentMap<String, String> map = new ConcurrentHashMap<String, String>();
map.put(Seed.IP, post.get("peerIP"));
map.put(Seed.PORT, post.get("peerPort"));
Seed peer = new Seed(post.get("peerHash"), map);
Seed peer = post.get("peerHash") == null ? null : new Seed(post.get("peerHash"), map);
sb.updateMySeed();
final Map<String, String> response = Protocol.hello(sb.peers.mySeed(), sb.peers.peerActions, peer);
final Map<String, String> response = Protocol.hello(sb.peers.mySeed(), sb.peers.peerActions, peer.getPublicAddress(post.get("peerIP")), peer.hash);
if (response == null) {
prop.put("table_comment",1);

View File

@ -218,7 +218,10 @@ public class Network
@Override
public final void run() {
try {
this.result = Protocol.hello(Network.this.sb.peers.mySeed(), Network.this.sb.peers.peerActions, this.seed);
for (String ip: this.seed.getIPs()) {
this.result = Protocol.hello(Network.this.sb.peers.mySeed(), Network.this.sb.peers.peerActions, this.seed.getPublicAddress(ip), this.seed.hash);
if (this.result != null) break;
}
if ( this.result == null ) {
// no or wrong response, delete that address
final String cause = "peer ping to peer resulted in error response (added < 0)";

View File

@ -170,8 +170,8 @@ public final class Protocol {
public static Map<String, String> hello(
final Seed mySeed,
final PeerActions peerActions,
final Seed otherSeed) {
final String address = otherSeed.getPublicAddress(otherSeed.getIP());
final String targetAddress,
final String targetHash) {
Map<String, String> result = null;
final String salt = crypt.randomSalt();
@ -190,8 +190,8 @@ public final class Protocol {
final HTTPClient httpClient = new HTTPClient(ClientIdentification.yacyInternetCrawlerAgent, 30000);
content =
httpClient.POSTbytes(
new MultiProtocolURL("http://" + address + "/yacy/hello.html"),
Seed.b64Hash2hexHash(otherSeed.hash) + ".yacyh",
new MultiProtocolURL("http://" + targetAddress + "/yacy/hello.html"),
Seed.b64Hash2hexHash(targetHash) + ".yacyh",
parts,
false, true);
responseTime = System.currentTimeMillis() - start;
@ -203,7 +203,7 @@ public final class Protocol {
+ "' interrupted.");
return null;
}
Network.log.info("yacyClient.hello thread '" + Thread.currentThread().getName() + "', peer " + address + "; exception: " + e.getMessage());
Network.log.info("yacyClient.hello thread '" + Thread.currentThread().getName() + "', peer " + targetAddress + "; exception: " + e.getMessage());
// try again (go into loop)
result = null;
}
@ -216,7 +216,7 @@ public final class Protocol {
Network.log.info("yacyClient.hello thread '"
+ Thread.currentThread().getName()
+ "' contacted peer at "
+ address
+ targetAddress
+ ", received "
+ ((content == null) ? "null" : content.length)
+ " bytes, time = "
@ -226,21 +226,21 @@ public final class Protocol {
// check consistency with expectation
Seed otherPeer = null;
String seed;
if ( (otherSeed.hash != null) && (otherSeed.hash.length() > 0) && ((seed = result.get("seed0")) != null) ) {
if ( (targetHash != null) && (targetHash.length() > 0) && ((seed = result.get("seed0")) != null) ) {
if ( seed.length() > Seed.maxsize ) {
Network.log.info("hello/client 0: rejected contacting seed; too large (" + seed.length() + " > " + Seed.maxsize + ")");
} else {
try {
// patch the remote peer address to avoid that remote peers spoof the network with wrong addresses
final int p = address.lastIndexOf(':');
final int p = targetAddress.lastIndexOf(':');
if ( p < 0 ) return null;
String h = address.substring(0, p);
String h = targetAddress.substring(0, p);
if (h.charAt(0) == '[') h = h.substring(1);
if (h.charAt(h.length() - 1) == ']') h = h.substring(0, h.length() - 1);
InetAddress ie = Domains.dnsResolve(h);
otherPeer = Seed.genRemoteSeed(seed, false, ie.getHostAddress());
if ( !otherPeer.hash.equals(otherSeed.hash) ) {
Network.log.info("yacyClient.hello: consistency error: otherPeer.hash = " + otherPeer.hash + ", otherHash = " + otherSeed.hash);
if ( !otherPeer.hash.equals(targetHash) ) {
Network.log.info("yacyClient.hello: consistency error: otherPeer.hash = " + otherPeer.hash + ", otherHash = " + targetHash);
return null; // no success
}
} catch (final IOException e ) {
@ -285,7 +285,7 @@ public final class Protocol {
accessible.IWasAccessed = false;
}
accessible.lastUpdated = System.currentTimeMillis();
Network.amIAccessibleDB.put(otherSeed.hash, accessible);
Network.amIAccessibleDB.put(targetHash, accessible);
/*
* If we were reported as junior we have to check if your port forwarding channel is broken
@ -329,7 +329,6 @@ public final class Protocol {
// read the seeds that the peer returned and integrate them into own database
int i = 0;
int count = 0;
String seedStr;
Seed s;
final int connectedBefore = peerActions.sizeConnected();
@ -337,39 +336,31 @@ public final class Protocol {
// integrate new seed into own database
// the first seed, "seed0" is the seed of the responding peer
if ( seedStr.length() > Seed.maxsize ) {
Network.log.info("hello/client: rejected contacting seed; too large ("
+ seedStr.length()
+ " > "
+ Seed.maxsize
+ ")");
Network.log.info("hello/client: rejected contacting seed; too large ("+ seedStr.length() + " > " + Seed.maxsize + ")");
} else {
try {
if ( i == 1 ) {
final int p = address.indexOf(':');
final int p = targetAddress.lastIndexOf(':');
if ( p < 0 ) {
return null;
}
InetAddress ia = Domains.dnsResolve(address.substring(0, p));
InetAddress ia = Domains.dnsResolve(targetAddress.substring(0, p));
if (ia == null) continue;
final String host = ia.getHostAddress();
final String host = ia.getHostAddress(); // the actual address of the target as we had been successful when contacting them is patched here
s = Seed.genRemoteSeed(seedStr, false, host);
} else {
s = Seed.genRemoteSeed(seedStr, false, null);
}
if ( peerActions.peerArrival(s, (i == 1)) ) {
count++;
}
peerActions.peerArrival(s, (i == 1));
} catch (final IOException e ) {
Network.log.info("hello/client: rejected contacting seed; bad ("
+ e.getMessage()
+ ")");
Network.log.info("hello/client: rejected contacting seed; bad (" + e.getMessage() + ")");
}
}
}
final int connectedAfter = peerActions.sizeConnected();
// update event tracker
EventTracker.update(EventTracker.EClass.PEERPING, new ProfilingGraph.EventPing(mySeed.getName(), otherSeed.getName(), true, connectedAfter - connectedBefore), false);
EventTracker.update(EventTracker.EClass.PEERPING, new ProfilingGraph.EventPing(mySeed.getName(), targetHash, true, connectedAfter - connectedBefore), false);
return result;
}