annotate src/ch/ethz/ssh2/transport/ClientServerHello.java @ 419:9045b1af94e5

revert notifyUser() changes
author Carl Byington <carl@five-ten-sg.com>
date Wed, 29 Oct 2014 15:25:32 -0700
parents 126af684034e
children
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;
337
Carl Byington <carl@five-ten-sg.com>
parents: 336
diff changeset
10 import java.io.InputStreamReader;
Carl Byington <carl@five-ten-sg.com>
parents: 336
diff changeset
11 import java.io.LineNumberReader;
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
12 import java.io.OutputStream;
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 import ch.ethz.ssh2.util.StringEncoder;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
15
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
16 /**
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
17 * @author Christian Plattner
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
18 * @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
19 */
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
20 public class ClientServerHello {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
21 private final String client_line;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
22 private final String server_line;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
23
336
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
24 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
25 int pos = 0;
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
26 boolean need10 = false;
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
27 int len = 0;
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
28
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
29 while (true) {
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
30 int c = is.read();
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
31
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
32 if (c == -1)
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
33 throw new IOException("Premature connection close");
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 buffer[pos++] = (byte) c;
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
36
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
37 if (c == 13) {
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
38 need10 = true;
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
39 continue;
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
40 }
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
41
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
42 if (c == 10)
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
43 break;
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
44
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
45 if (need10 == true)
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
46 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
47
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
48 len++;
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
49
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
50 if (pos >= buffer.length)
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
51 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
52 }
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 return len;
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
55 }
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
56
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
57 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
58 this.client_line = client_line;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
59 this.server_line = server_line;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
60 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
61
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
62 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
63 throws IOException {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
64 return exchange(softwareversion, bi, bo, true);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
65 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
66
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
67 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
68 throws IOException {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
69 return exchange(softwareversion, bi, bo, false);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
70 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
71
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
72 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
73 throws IOException {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
74 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
75 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
76 bo.flush();
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
77 // 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
78 byte[] serverVersion = new byte[512];
338
126af684034e still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 337
diff changeset
79 String remoteIdentifier = null;
336
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
80 for (int i = 0; i < 50; i++) {
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
81 int len = readLineRN(bi, serverVersion);
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
82 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
83 if (remoteIdentifier.startsWith("SSH-")) break;
334
097cfe8770dd still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 307
diff changeset
84 }
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
85
336
63b23b7c840d still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 335
diff changeset
86 if (remoteIdentifier.equals("")) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
87 throw new IOException("Premature connection close");
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
88 }
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
89
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
90 if (!remoteIdentifier.startsWith("SSH-")) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
91 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
92 }
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
93
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
94 if (!remoteIdentifier.startsWith("SSH-1.99-")
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
95 && !remoteIdentifier.startsWith("SSH-2.0-")) {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
96 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
97 }
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
98
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
99 if (clientMode) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
100 return new ClientServerHello(localIdentifier, remoteIdentifier);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
101 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
102 else {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
103 return new ClientServerHello(remoteIdentifier, localIdentifier);
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
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 * @return Returns the client_versioncomment.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
109 */
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
110 public byte[] getClientString() {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
111 return StringEncoder.GetBytes(client_line);
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
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 * @return Returns the server_versioncomment.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
116 */
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
117 public byte[] getServerString() {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
118 return StringEncoder.GetBytes(server_line);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
119 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
120 }