comparison app/src/main/java/ch/ethz/ssh2/transport/ClientServerHello.java @ 438:d29cce60f393

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