comparison src/com/trilead/ssh2/transport/ClientServerHello.java @ 0:0ce5cc452d02

initial version
author Carl Byington <carl@five-ten-sg.com>
date Thu, 22 May 2014 10:41:19 -0700
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:0ce5cc452d02
1
2 package com.trilead.ssh2.transport;
3
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.OutputStream;
7 import java.io.UnsupportedEncodingException;
8
9 import com.trilead.ssh2.Connection;
10
11 /**
12 * ClientServerHello.
13 *
14 * @author Christian Plattner, plattner@trilead.com
15 * @version $Id: ClientServerHello.java,v 1.2 2008/04/01 12:38:09 cplattne Exp $
16 */
17 public class ClientServerHello {
18 String server_line;
19 String client_line;
20
21 String server_versioncomment;
22
23 public final static int readLineRN(InputStream is, byte[] buffer) throws IOException {
24 int pos = 0;
25 boolean need10 = false;
26 int len = 0;
27
28 while (true) {
29 int c = is.read();
30
31 if (c == -1)
32 throw new IOException("Premature connection close");
33
34 buffer[pos++] = (byte) c;
35
36 if (c == 13) {
37 need10 = true;
38 continue;
39 }
40
41 if (c == 10)
42 break;
43
44 if (need10 == true)
45 throw new IOException("Malformed line sent by the server, the line does not end correctly.");
46
47 len++;
48
49 if (pos >= buffer.length)
50 throw new IOException("The server sent a too long line.");
51 }
52
53 return len;
54 }
55
56 public ClientServerHello(InputStream bi, OutputStream bo) throws IOException {
57 client_line = "SSH-2.0-" + Connection.identification;
58 bo.write((client_line + "\r\n").getBytes("ISO-8859-1"));
59 bo.flush();
60 byte[] serverVersion = new byte[512];
61
62 for (int i = 0; i < 50; i++) {
63 int len = readLineRN(bi, serverVersion);
64 server_line = new String(serverVersion, 0, len, "ISO-8859-1");
65
66 if (server_line.startsWith("SSH-"))
67 break;
68 }
69
70 if (server_line.startsWith("SSH-") == false)
71 throw new IOException(
72 "Malformed server identification string. There was no line starting with 'SSH-' amongst the first 50 lines.");
73
74 if (server_line.startsWith("SSH-1.99-"))
75 server_versioncomment = server_line.substring(9);
76 else if (server_line.startsWith("SSH-2.0-"))
77 server_versioncomment = server_line.substring(8);
78 else
79 throw new IOException("Server uses incompatible protocol, it is not SSH-2 compatible.");
80 }
81
82 /**
83 * @return Returns the client_versioncomment.
84 */
85 public byte[] getClientString() {
86 byte[] result;
87
88 try {
89 result = client_line.getBytes("ISO-8859-1");
90 }
91 catch (UnsupportedEncodingException ign) {
92 result = client_line.getBytes();
93 }
94
95 return result;
96 }
97
98 /**
99 * @return Returns the server_versioncomment.
100 */
101 public byte[] getServerString() {
102 byte[] result;
103
104 try {
105 result = server_line.getBytes("ISO-8859-1");
106 }
107 catch (UnsupportedEncodingException ign) {
108 result = server_line.getBytes();
109 }
110
111 return result;
112 }
113 }