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

pull/1/head
Michael Peter Christen 11 years ago
parent 65e6ae52fb
commit fe917deb2d

@ -208,10 +208,10 @@ public class Network {
final ConcurrentMap<String, String> map = new ConcurrentHashMap<String, String>(); final ConcurrentMap<String, String> map = new ConcurrentHashMap<String, String>();
map.put(Seed.IP, post.get("peerIP")); map.put(Seed.IP, post.get("peerIP"));
map.put(Seed.PORT, post.get("peerPort")); 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(); 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) { if (response == null) {
prop.put("table_comment",1); prop.put("table_comment",1);

@ -218,7 +218,10 @@ public class Network
@Override @Override
public final void run() { public final void run() {
try { 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 ) { if ( this.result == null ) {
// no or wrong response, delete that address // no or wrong response, delete that address
final String cause = "peer ping to peer resulted in error response (added < 0)"; final String cause = "peer ping to peer resulted in error response (added < 0)";

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

Loading…
Cancel
Save