comparison src/ch/ethz/ssh2/transport/ClientServerHello.java @ 336:63b23b7c840d ganymed

still hangs during connection
author Carl Byington <carl@five-ten-sg.com>
date Thu, 31 Jul 2014 14:58:57 -0700
parents e25e377d29d3
children 0d0d2fc9918c
comparison
equal deleted inserted replaced
335:e25e377d29d3 336:63b23b7c840d
5 5
6 package ch.ethz.ssh2.transport; 6 package ch.ethz.ssh2.transport;
7 7
8 import java.io.IOException; 8 import java.io.IOException;
9 import java.io.InputStream; 9 import java.io.InputStream;
10 import java.io.InputStreamReader;
11 import java.io.LineNumberReader;
12 import java.io.OutputStream; 10 import java.io.OutputStream;
13 11
14 import ch.ethz.ssh2.util.StringEncoder; 12 import ch.ethz.ssh2.util.StringEncoder;
15 13
16 /** 14 /**
18 * @version $Id: ClientServerHello.java 155 2014-04-28 12:01:19Z dkocher@sudo.ch $ 16 * @version $Id: ClientServerHello.java 155 2014-04-28 12:01:19Z dkocher@sudo.ch $
19 */ 17 */
20 public class ClientServerHello { 18 public class ClientServerHello {
21 private final String client_line; 19 private final String client_line;
22 private final String server_line; 20 private final String server_line;
21
22 public final static int readLineRN(InputStream is, byte[] buffer) throws IOException {
23 int pos = 0;
24 boolean need10 = false;
25 int len = 0;
26
27 while (true) {
28 int c = is.read();
29
30 if (c == -1)
31 throw new IOException("Premature connection close");
32
33 buffer[pos++] = (byte) c;
34
35 if (c == 13) {
36 need10 = true;
37 continue;
38 }
39
40 if (c == 10)
41 break;
42
43 if (need10 == true)
44 throw new IOException("Malformed line sent by the server, the line does not end correctly.");
45
46 len++;
47
48 if (pos >= buffer.length)
49 throw new IOException("The server sent a too long line.");
50 }
51
52 return len;
53 }
23 54
24 private ClientServerHello(String client_line, String server_line) { 55 private ClientServerHello(String client_line, String server_line) {
25 this.client_line = client_line; 56 this.client_line = client_line;
26 this.server_line = server_line; 57 this.server_line = server_line;
27 } 58 }
40 throws IOException { 71 throws IOException {
41 String localIdentifier = String.format("SSH-2.0-%s", softwareversion); 72 String localIdentifier = String.format("SSH-2.0-%s", softwareversion);
42 bo.write(StringEncoder.GetBytes(String.format("%s\r\n", localIdentifier))); 73 bo.write(StringEncoder.GetBytes(String.format("%s\r\n", localIdentifier)));
43 bo.flush(); 74 bo.flush();
44 // Expect SSH-protoversion-softwareversion SP comments CR LF 75 // Expect SSH-protoversion-softwareversion SP comments CR LF
45 InputStreamReader isr = new InputStreamReader(bi); 76 byte[] serverVersion = new byte[512];
46 LineNumberReader lnr = new LineNumberReader(isr); 77 String remoteIdentifier;
47 String remoteIdentifier = lnr.readLine(); 78 for (int i = 0; i < 50; i++) {
48 if ((bi.available()==0) && lnr.ready()) { 79 int len = readLineRN(bi, serverVersion);
49 int a= 1; // break 80 remoteIdentifier = new String(serverVersion, 0, len, "ISO-8859-1");
81 if (remoteIdentifier.startsWith("SSH-")) break;
50 } 82 }
51 83
52 if (null == remoteIdentifier) { 84 if (remoteIdentifier.equals("")) {
53 throw new IOException("Premature connection close"); 85 throw new IOException("Premature connection close");
54 } 86 }
55 87
56 if (!remoteIdentifier.startsWith("SSH-")) { 88 if (!remoteIdentifier.startsWith("SSH-")) {
57 throw new IOException(String.format("Malformed SSH identification %s", remoteIdentifier)); 89 throw new IOException(String.format("Malformed SSH identification %s", remoteIdentifier));