01: package ch.ethz.ssh2.transport;
02:
03: import java.io.IOException;
04: import java.io.InputStream;
05: import java.io.OutputStream;
06:
07: import ch.ethz.ssh2.Connection;
08:
09: /**
10: * ClientServerHello.
11: *
12: * @author Christian Plattner, plattner@inf.ethz.ch
13: * @version $Id: ClientServerHello.java,v 1.8 2006/08/02 11:57:12 cplattne Exp $
14: */
15: public class ClientServerHello {
16: String server_line;
17: String client_line;
18:
19: String server_versioncomment;
20:
21: public final static int readLineRN(InputStream is, byte[] buffer)
22: throws IOException {
23: int pos = 0;
24: boolean need10 = false;
25: int len = 0;
26: while (true) {
27: int c = is.read();
28: if (c == -1)
29: throw new IOException("Premature connection close");
30:
31: buffer[pos++] = (byte) c;
32:
33: if (c == 13) {
34: need10 = true;
35: continue;
36: }
37:
38: if (c == 10)
39: break;
40:
41: if (need10 == true)
42: throw new IOException(
43: "Malformed line sent by the server, the line does not end correctly.");
44:
45: len++;
46: if (pos >= buffer.length)
47: throw new IOException(
48: "The server sent a too long line.");
49: }
50:
51: return len;
52: }
53:
54: public ClientServerHello(InputStream bi, OutputStream bo)
55: throws IOException {
56: client_line = "SSH-2.0-" + Connection.identification;
57:
58: bo.write((client_line + "\r\n").getBytes());
59: bo.flush();
60:
61: byte[] serverVersion = new byte[512];
62:
63: for (int i = 0; i < 50; i++) {
64: int len = readLineRN(bi, serverVersion);
65:
66: server_line = new String(serverVersion, 0, len);
67:
68: if (server_line.startsWith("SSH-"))
69: break;
70: }
71:
72: if (server_line.startsWith("SSH-") == false)
73: throw new IOException(
74: "Malformed server identification string. There was no line starting with 'SSH-' amongst the first 50 lines.");
75:
76: if (server_line.startsWith("SSH-1.99-"))
77: server_versioncomment = server_line.substring(9);
78: else if (server_line.startsWith("SSH-2.0-"))
79: server_versioncomment = server_line.substring(8);
80: else
81: throw new IOException(
82: "Server uses incompatible protocol, it is not SSH-2 compatible.");
83: }
84:
85: /**
86: * @return Returns the client_versioncomment.
87: */
88: public byte[] getClientString() {
89: return client_line.getBytes();
90: }
91:
92: /**
93: * @return Returns the server_versioncomment.
94: */
95: public byte[] getServerString() {
96: return server_line.getBytes();
97: }
98: }
|