annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1 /*
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
2 * Copyright (c) 2006-2011 Christian Plattner. All rights reserved.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
3 * Please refer to the LICENSE.txt for licensing details.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
4 */
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
5
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
6 package ch.ethz.ssh2.transport;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
7
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
8 import java.io.IOException;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
9 import java.io.InputStream;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
10 import java.io.OutputStream;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
11
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
12 import ch.ethz.ssh2.util.StringEncoder;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
13
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
14 /**
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
15 * @author Christian Plattner
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
16 * @version $Id: ClientServerHello.java 155 2014-04-28 12:01:19Z dkocher@sudo.ch $
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
17 */
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
18 public class ClientServerHello {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
19 private final String client_line;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
20 private final String server_line;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
21
336
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
22 public final static int readLineRN(InputStream is, byte[] buffer) throws IOException {
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
23 int pos = 0;
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
24 boolean need10 = false;
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
25 int len = 0;
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
26
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
27 while (true) {
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
28 int c = is.read();
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
29
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
30 if (c == -1)
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
31 throw new IOException("Premature connection close");
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
32
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
33 buffer[pos++] = (byte) c;
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
34
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
35 if (c == 13) {
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
36 need10 = true;
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
37 continue;
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
38 }
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
39
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
40 if (c == 10)
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
41 break;
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
42
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
43 if (need10 == true)
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
44 throw new IOException("Malformed line sent by the server, the line does not end correctly.");
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
45
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
46 len++;
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
47
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
48 if (pos >= buffer.length)
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
49 throw new IOException("The server sent a too long line.");
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
50 }
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
51
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
52 return len;
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
53 }
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
54
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
55 private ClientServerHello(String client_line, String server_line) {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
56 this.client_line = client_line;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
57 this.server_line = server_line;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
58 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
59
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
60 public static ClientServerHello clientHello(String softwareversion, InputStream bi, OutputStream bo)
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
61 throws IOException {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
62 return exchange(softwareversion, bi, bo, true);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
63 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
64
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
65 public static ClientServerHello serverHello(String softwareversion, InputStream bi, OutputStream bo)
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
66 throws IOException {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
67 return exchange(softwareversion, bi, bo, false);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
68 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
69
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
70 private static ClientServerHello exchange(String softwareversion, InputStream bi, OutputStream bo, boolean clientMode)
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
71 throws IOException {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
72 String localIdentifier = String.format("SSH-2.0-%s", softwareversion);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
73 bo.write(StringEncoder.GetBytes(String.format("%s\r\n", localIdentifier)));
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
74 bo.flush();
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
75 // Expect SSH-protoversion-softwareversion SP comments CR LF
336
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
76 byte[] serverVersion = new byte[512];
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
77 String remoteIdentifier;
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
78 for (int i = 0; i < 50; i++) {
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
79 int len = readLineRN(bi, serverVersion);
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
80 remoteIdentifier = new String(serverVersion, 0, len, "ISO-8859-1");
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
81 if (remoteIdentifier.startsWith("SSH-")) break;
334
097cfe8770dd still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 307
diff changeset
82 }
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
83
336
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
84 if (remoteIdentifier.equals("")) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
85 throw new IOException("Premature connection close");
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
86 }
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
87
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
88 if (!remoteIdentifier.startsWith("SSH-")) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
89 throw new IOException(String.format("Malformed SSH identification %s", remoteIdentifier));
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
90 }
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
91
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
92 if (!remoteIdentifier.startsWith("SSH-1.99-")
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
93 && !remoteIdentifier.startsWith("SSH-2.0-")) {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
94 throw new IOException(String.format("Incompatible remote protocol version %s", remoteIdentifier));
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
95 }
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
96
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
97 if (clientMode) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
98 return new ClientServerHello(localIdentifier, remoteIdentifier);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
99 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
100 else {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
101 return new ClientServerHello(remoteIdentifier, localIdentifier);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
102 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
103 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
104
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
105 /**
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
106 * @return Returns the client_versioncomment.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
107 */
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
108 public byte[] getClientString() {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
109 return StringEncoder.GetBytes(client_line);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
110 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
111
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
112 /**
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
113 * @return Returns the server_versioncomment.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
114 */
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
115 public byte[] getServerString() {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
116 return StringEncoder.GetBytes(server_line);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
117 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
118 }