From ce186e187243b8a2ec60fe6c8c17a7cf92b00fac Mon Sep 17 00:00:00 2001 From: jrandom Date: Sat, 2 Oct 2004 12:31:15 +0000 Subject: [PATCH] 2004-10-02 jrandom * Command line utility to verify a peer's reachability - simply run net.i2p.router.transport.tcp.ConnectionHandler hostname port# and it will print out whether that peer is reachable or not (using a simple verification handshake). --- history.txt | 8 +- .../src/net/i2p/router/RouterVersion.java | 4 +- .../transport/tcp/ConnectionHandler.java | 113 +++++++++++------- 3 files changed, 80 insertions(+), 45 deletions(-) diff --git a/history.txt b/history.txt index fd4f6b491d..4f00c3ba5a 100644 --- a/history.txt +++ b/history.txt @@ -1,4 +1,10 @@ -$Id: history.txt,v 1.27 2004/10/01 09:35:49 jrandom Exp $ +$Id: history.txt,v 1.28 2004/10/01 12:23:00 jrandom Exp $ + +2004-10-02 jrandom + * Command line utility to verify a peer's reachability - simply run + net.i2p.router.transport.tcp.ConnectionHandler hostname port# and it + will print out whether that peer is reachable or not (using a simple + verification handshake). * 2004-10-01 0.4.1.1 released diff --git a/router/java/src/net/i2p/router/RouterVersion.java b/router/java/src/net/i2p/router/RouterVersion.java index 7e4853b34d..afab6f9308 100644 --- a/router/java/src/net/i2p/router/RouterVersion.java +++ b/router/java/src/net/i2p/router/RouterVersion.java @@ -15,9 +15,9 @@ import net.i2p.CoreVersion; * */ public class RouterVersion { - public final static String ID = "$Revision: 1.37 $ $Date: 2004/10/01 09:35:49 $"; + public final static String ID = "$Revision: 1.38 $ $Date: 2004/10/01 12:23:00 $"; public final static String VERSION = "0.4.1.1"; - public final static long BUILD = 3; + public final static long BUILD = 4; public static void main(String args[]) { System.out.println("I2P Router version: " + VERSION); System.out.println("Router ID: " + RouterVersion.ID); diff --git a/router/java/src/net/i2p/router/transport/tcp/ConnectionHandler.java b/router/java/src/net/i2p/router/transport/tcp/ConnectionHandler.java index 6f8156a6de..77dd30503e 100644 --- a/router/java/src/net/i2p/router/transport/tcp/ConnectionHandler.java +++ b/router/java/src/net/i2p/router/transport/tcp/ConnectionHandler.java @@ -678,49 +678,8 @@ public class ConnectionHandler { if (!_transport.allowAddress(_remoteAddress)) return false; - //if (true) return true; - Socket s = null; try { - s = new Socket(_remoteAddress.getAddress(), _remoteAddress.getPort()); - OutputStream out = s.getOutputStream(); - InputStream in = s.getInputStream(); - - try { s.setSoTimeout(TCPListener.HANDLE_TIMEOUT); } catch (SocketException se) {} - - if (_log.shouldLog(Log.DEBUG)) - _log.debug("Beginning verification of reachability"); - - // send: 0xFFFF + #versions + v1 [+ v2 [etc]] + properties - DataHelper.writeLong(out, 2, FLAG_TEST); - out.write(TCPTransport.SUPPORTED_PROTOCOLS.length); - for (int i = 0; i < TCPTransport.SUPPORTED_PROTOCOLS.length; i++) - out.write(TCPTransport.SUPPORTED_PROTOCOLS[i]); - DataHelper.writeProperties(out, null); - out.flush(); - - if (_log.shouldLog(Log.DEBUG)) - _log.debug("Verification of reachability request sent"); - - // read: 0xFFFF + versionOk + #bytesIP + IP + currentTime + properties - int flag = (int)DataHelper.readLong(in, 2); - if (flag != FLAG_TEST) - throw new IOException("Unable to verify the peer - invalid response"); - int version = in.read(); - if (version == -1) - throw new IOException("Unable to verify the peer - invalid version"); - if (version == FLAG_PROTOCOL_NONE) - throw new IOException("Unable to verify the peer - no matching version"); - int numBytes = in.read(); - if ( (numBytes == -1) || (numBytes > 32) ) - throw new IOException("Unable to verify the peer - invalid num bytes"); - byte ip[] = new byte[numBytes]; - int read = DataHelper.read(in, ip); - if (read != numBytes) - throw new IOException("Unable to verify the peer - invalid num bytes"); - Date now = DataHelper.readDate(in); - Properties opts = DataHelper.readProperties(in); - - return true; + return verifyReachability(_remoteAddress); } catch (IOException ioe) { if (_log.shouldLog(Log.WARN)) _log.warn("Error verifying " @@ -736,6 +695,50 @@ public class ConnectionHandler { } } + private static boolean verifyReachability(TCPAddress address) throws IOException, DataFormatException { + //if (true) return true; + Socket s = new Socket(address.getAddress(), address.getPort()); + OutputStream out = s.getOutputStream(); + InputStream in = s.getInputStream(); + + try { s.setSoTimeout(TCPListener.HANDLE_TIMEOUT); } catch (SocketException se) {} + + //if (_log.shouldLog(Log.DEBUG)) + // _log.debug("Beginning verification of reachability"); + + // send: 0xFFFF + #versions + v1 [+ v2 [etc]] + properties + DataHelper.writeLong(out, 2, FLAG_TEST); + out.write(TCPTransport.SUPPORTED_PROTOCOLS.length); + for (int i = 0; i < TCPTransport.SUPPORTED_PROTOCOLS.length; i++) + out.write(TCPTransport.SUPPORTED_PROTOCOLS[i]); + DataHelper.writeProperties(out, null); + out.flush(); + + //if (_log.shouldLog(Log.DEBUG)) + // _log.debug("Verification of reachability request sent"); + + // read: 0xFFFF + versionOk + #bytesIP + IP + currentTime + properties + int flag = (int)DataHelper.readLong(in, 2); + if (flag != FLAG_TEST) + throw new IOException("Unable to verify the peer - invalid response"); + int version = in.read(); + if (version == -1) + throw new IOException("Unable to verify the peer - invalid version"); + if (version == FLAG_PROTOCOL_NONE) + throw new IOException("Unable to verify the peer - no matching version"); + int numBytes = in.read(); + if ( (numBytes == -1) || (numBytes > 32) ) + throw new IOException("Unable to verify the peer - invalid num bytes"); + byte ip[] = new byte[numBytes]; + int read = DataHelper.read(in, ip); + if (read != numBytes) + throw new IOException("Unable to verify the peer - invalid num bytes"); + Date now = DataHelper.readDate(in); + Properties opts = DataHelper.readProperties(in); + + return true; + } + /** * The peer contacting us is just testing us. Verify that we are reachable * by following the protocol, then close the socket. This is called only @@ -851,4 +854,30 @@ public class ConnectionHandler { if (_log.shouldLog(Log.WARN)) _log.warn(error, e); } + + /** + * Verify the reachability of a peer. + * Usage: ConnectionHandler hostname portNum + */ + public static void main(String args[]) { + if (false) args = new String[] { "dev.i2p.net", "4108" }; + + if ( (args == null) || (args.length != 2) ) { + System.out.println("Usage: ConnectionHandler hostname portNum"); + System.exit(0); + } + + try { + int port = Integer.parseInt(args[1]); + TCPAddress addr = new TCPAddress(args[0], port); + boolean ok = verifyReachability(addr); + if (ok) + System.out.println("Peer is reachable: " + addr.toString()); + else + System.out.println("Peer is not reachable: " + addr.toString()); + } catch (Exception e) { + System.out.println("Peer is not reachable: " + args[0] + ":" + args[1]); + e.printStackTrace(); + } + } }