# HG changeset patch # User Carl Byington # Date 1406843937 25200 # Node ID 63b23b7c840daa863625fe09b043e9724dd4259d # Parent e25e377d29d3421098ed5deac11707527f01eb79 still hangs during connection diff -r e25e377d29d3 -r 63b23b7c840d src/ch/ethz/ssh2/transport/ClientServerHello.java --- a/src/ch/ethz/ssh2/transport/ClientServerHello.java Thu Jul 31 14:35:37 2014 -0700 +++ b/src/ch/ethz/ssh2/transport/ClientServerHello.java Thu Jul 31 14:58:57 2014 -0700 @@ -7,8 +7,6 @@ import java.io.IOException; import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.LineNumberReader; import java.io.OutputStream; import ch.ethz.ssh2.util.StringEncoder; @@ -21,6 +19,39 @@ private final String client_line; private final String server_line; + public final static int readLineRN(InputStream is, byte[] buffer) throws IOException { + int pos = 0; + boolean need10 = false; + int len = 0; + + while (true) { + int c = is.read(); + + if (c == -1) + throw new IOException("Premature connection close"); + + buffer[pos++] = (byte) c; + + if (c == 13) { + need10 = true; + continue; + } + + if (c == 10) + break; + + if (need10 == true) + throw new IOException("Malformed line sent by the server, the line does not end correctly."); + + len++; + + if (pos >= buffer.length) + throw new IOException("The server sent a too long line."); + } + + return len; + } + private ClientServerHello(String client_line, String server_line) { this.client_line = client_line; this.server_line = server_line; @@ -42,14 +73,15 @@ bo.write(StringEncoder.GetBytes(String.format("%s\r\n", localIdentifier))); bo.flush(); // Expect SSH-protoversion-softwareversion SP comments CR LF - InputStreamReader isr = new InputStreamReader(bi); - LineNumberReader lnr = new LineNumberReader(isr); - String remoteIdentifier = lnr.readLine(); - if ((bi.available()==0) && lnr.ready()) { - int a= 1; // break + byte[] serverVersion = new byte[512]; + String remoteIdentifier; + for (int i = 0; i < 50; i++) { + int len = readLineRN(bi, serverVersion); + remoteIdentifier = new String(serverVersion, 0, len, "ISO-8859-1"); + if (remoteIdentifier.startsWith("SSH-")) break; } - if (null == remoteIdentifier) { + if (remoteIdentifier.equals("")) { throw new IOException("Premature connection close"); } diff -r e25e377d29d3 -r 63b23b7c840d src/ch/ethz/ssh2/transport/ClientTransportManager.java --- a/src/ch/ethz/ssh2/transport/ClientTransportManager.java Thu Jul 31 14:35:37 2014 -0700 +++ b/src/ch/ethz/ssh2/transport/ClientTransportManager.java Thu Jul 31 14:58:57 2014 -0700 @@ -46,8 +46,9 @@ this.startReceiver(); } - protected void connect(String hostname, int port, int connectTimeout) - throws IOException { + protected void connect(String hostname, int port, int connectTimeout) throws IOException { + log.debug(String.format("client transport manager connecting to %s:%d", hostname, port)); sock.connect(new InetSocketAddress(hostname, port), connectTimeout); + log.debug(String.format("client transport manager connected to %s:%d", hostname, port)); } } \ No newline at end of file