KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ch > ethz > ssh2 > transport > ClientServerHello


1
2 package ch.ethz.ssh2.transport;
3
4 import java.io.IOException JavaDoc;
5 import java.io.InputStream JavaDoc;
6 import java.io.OutputStream JavaDoc;
7
8 import ch.ethz.ssh2.Connection;
9
10 /**
11  * ClientServerHello.
12  *
13  * @author Christian Plattner, plattner@inf.ethz.ch
14  * @version $Id: ClientServerHello.java,v 1.8 2006/08/02 11:57:12 cplattne Exp $
15  */

16 public class ClientServerHello
17 {
18     String JavaDoc server_line;
19     String JavaDoc client_line;
20
21     String JavaDoc server_versioncomment;
22
23     public final static int readLineRN(InputStream JavaDoc is, byte[] buffer) throws IOException JavaDoc
24     {
25         int pos = 0;
26         boolean need10 = false;
27         int len = 0;
28         while (true)
29         {
30             int c = is.read();
31             if (c == -1)
32                 throw new IOException JavaDoc("Premature connection close");
33
34             buffer[pos++] = (byte) c;
35
36             if (c == 13)
37             {
38                 need10 = true;
39                 continue;
40             }
41
42             if (c == 10)
43                 break;
44
45             if (need10 == true)
46                 throw new IOException JavaDoc("Malformed line sent by the server, the line does not end correctly.");
47
48             len++;
49             if (pos >= buffer.length)
50                 throw new IOException JavaDoc("The server sent a too long line.");
51         }
52
53         return len;
54     }
55
56     public ClientServerHello(InputStream JavaDoc bi, OutputStream JavaDoc bo) throws IOException JavaDoc
57     {
58         client_line = "SSH-2.0-" + Connection.identification;
59
60         bo.write((client_line + "\r\n").getBytes());
61         bo.flush();
62
63         byte[] serverVersion = new byte[512];
64
65         for (int i = 0; i < 50; i++)
66         {
67             int len = readLineRN(bi, serverVersion);
68
69             server_line = new String JavaDoc(serverVersion, 0, len);
70
71             if (server_line.startsWith("SSH-"))
72                 break;
73         }
74
75         if (server_line.startsWith("SSH-") == false)
76             throw new IOException JavaDoc(
77                     "Malformed server identification string. There was no line starting with 'SSH-' amongst the first 50 lines.");
78
79         if (server_line.startsWith("SSH-1.99-"))
80             server_versioncomment = server_line.substring(9);
81         else if (server_line.startsWith("SSH-2.0-"))
82             server_versioncomment = server_line.substring(8);
83         else
84             throw new IOException JavaDoc("Server uses incompatible protocol, it is not SSH-2 compatible.");
85     }
86
87     /**
88      * @return Returns the client_versioncomment.
89      */

90     public byte[] getClientString()
91     {
92         return client_line.getBytes();
93     }
94
95     /**
96      * @return Returns the server_versioncomment.
97      */

98     public byte[] getServerString()
99     {
100         return server_line.getBytes();
101     }
102 }
103
Popular Tags