1 2 package ch.ethz.ssh2.transport; 3 4 import java.io.IOException ; 5 import java.io.InputStream ; 6 import java.io.OutputStream ; 7 8 import ch.ethz.ssh2.Connection; 9 10 16 public class ClientServerHello 17 { 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 { 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 ("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 ("Malformed line sent by the server, the line does not end correctly."); 47 48 len++; 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 { 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 (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 ( 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 ("Server uses incompatible protocol, it is not SSH-2 compatible."); 85 } 86 87 90 public byte[] getClientString() 91 { 92 return client_line.getBytes(); 93 } 94 95 98 public byte[] getServerString() 99 { 100 return server_line.getBytes(); 101 } 102 } 103 | Popular Tags |