comparison src/ch/ethz/ssh2/transport/ClientServerHello.java @ 273:91a31873c42a ganymed

start conversion from trilead to ganymed
author Carl Byington <carl@five-ten-sg.com>
date Fri, 18 Jul 2014 11:21:46 -0700
parents
children 071eccdff8ea
comparison
equal deleted inserted replaced
272:ce2f4e397703 273:91a31873c42a
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 private ClientServerHello(String client_line, String server_line) {
25 this.client_line = client_line;
26 this.server_line = server_line;
27 }
28
29 public static ClientServerHello clientHello(String softwareversion, InputStream bi, OutputStream bo)
30 throws IOException {
31 return exchange(softwareversion, bi, bo, true);
32 }
33
34 public static ClientServerHello serverHello(String softwareversion, InputStream bi, OutputStream bo)
35 throws IOException {
36 return exchange(softwareversion, bi, bo, false);
37 }
38
39 private static ClientServerHello exchange(String softwareversion, InputStream bi, OutputStream bo, boolean clientMode)
40 throws IOException {
41 String localIdentifier = String.format("SSH-2.0-%s", softwareversion);
42
43 bo.write(StringEncoder.GetBytes(String.format("%s\r\n", localIdentifier)));
44 bo.flush();
45
46 // Expect SSH-protoversion-softwareversion SP comments CR LF
47 String remoteIdentifier = new LineNumberReader(new InputStreamReader(bi)).readLine();
48 if(null == remoteIdentifier) {
49 throw new IOException("Premature connection close");
50 }
51 if(!remoteIdentifier.startsWith("SSH-")) {
52 throw new IOException(String.format("Malformed SSH identification %s", remoteIdentifier));
53 }
54 if(!remoteIdentifier.startsWith("SSH-1.99-")
55 && !remoteIdentifier.startsWith("SSH-2.0-")) {
56 throw new IOException(String.format("Incompatible remote protocol version %s", remoteIdentifier));
57 }
58 if(clientMode) {
59 return new ClientServerHello(localIdentifier, remoteIdentifier);
60 }
61 else {
62 return new ClientServerHello(remoteIdentifier, localIdentifier);
63 }
64 }
65
66 /**
67 * @return Returns the client_versioncomment.
68 */
69 public byte[] getClientString() {
70 return StringEncoder.GetBytes(client_line);
71 }
72
73 /**
74 * @return Returns the server_versioncomment.
75 */
76 public byte[] getServerString() {
77 return StringEncoder.GetBytes(server_line);
78 }
79 }