annotate src/ch/ethz/ssh2/transport/ClientServerHello.java @ 335:e25e377d29d3 ganymed

still hangs during connection
author Carl Byington <carl@five-ten-sg.com>
date Thu, 31 Jul 2014 14:35:37 -0700
parents 097cfe8770dd
children 63b23b7c840d
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.InputStreamReader;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
11 import java.io.LineNumberReader;
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
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
24 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
25 this.client_line = client_line;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
26 this.server_line = server_line;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
27 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
28
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
29 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
30 throws IOException {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
31 return exchange(softwareversion, bi, bo, true);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
32 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
33
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
34 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
35 throws IOException {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
36 return exchange(softwareversion, bi, bo, false);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
37 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
38
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
39 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
40 throws IOException {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
41 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
42 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
43 bo.flush();
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
44 // Expect SSH-protoversion-softwareversion SP comments CR LF
334
097cfe8770dd still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 307
diff changeset
45 InputStreamReader isr = new InputStreamReader(bi);
097cfe8770dd still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 307
diff changeset
46 LineNumberReader lnr = new LineNumberReader(isr);
097cfe8770dd still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 307
diff changeset
47 String remoteIdentifier = lnr.readLine();
335
e25e377d29d3 still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 334
diff changeset
48 if ((bi.available()==0) && lnr.ready()) {
334
097cfe8770dd still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 307
diff changeset
49 int a= 1; // break
097cfe8770dd still hangs during connection
Carl Byington <carl@five-ten-sg.com>
parents: 307
diff changeset
50 }
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
51
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
52 if (null == remoteIdentifier) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
53 throw new IOException("Premature connection close");
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
54 }
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
55
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
56 if (!remoteIdentifier.startsWith("SSH-")) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
57 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
58 }
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
59
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
60 if (!remoteIdentifier.startsWith("SSH-1.99-")
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
61 && !remoteIdentifier.startsWith("SSH-2.0-")) {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
62 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
63 }
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
64
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
65 if (clientMode) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
66 return new ClientServerHello(localIdentifier, remoteIdentifier);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
67 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
68 else {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
69 return new ClientServerHello(remoteIdentifier, localIdentifier);
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
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
73 /**
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
74 * @return Returns the client_versioncomment.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
75 */
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
76 public byte[] getClientString() {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
77 return StringEncoder.GetBytes(client_line);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
78 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
79
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
80 /**
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
81 * @return Returns the server_versioncomment.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
82 */
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
83 public byte[] getServerString() {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
84 return StringEncoder.GetBytes(server_line);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
85 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
86 }