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).
This commit is contained in:
@ -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
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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: <code>ConnectionHandler hostname portNum</code>
|
||||
*/
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user