Home | History | Annotate | Download | only in transport
      1 /*
      2  * Copyright (c) 2006-2011 Christian Plattner. All rights reserved.
      3  * Please refer to the LICENSE.txt for licensing details.
      4  */
      5 package ch.ethz.ssh2.transport;
      6 
      7 import java.io.IOException;
      8 import java.io.InputStream;
      9 import java.io.OutputStream;
     10 
     11 import ch.ethz.ssh2.util.StringEncoder;
     12 
     13 /**
     14  * ClientServerHello.
     15  *
     16  * @author Christian Plattner
     17  * @version $Id: ClientServerHello.java 33 2011-05-28 21:59:37Z dkocher (at) sudo.ch $
     18  */
     19 public class ClientServerHello
     20 {
     21 	String server_line;
     22 	String client_line;
     23 
     24 	String server_versioncomment;
     25 
     26 	public static int readLineRN(InputStream is, byte[] buffer) throws IOException
     27 	{
     28 		int pos = 0;
     29 		boolean need10 = false;
     30 		int len = 0;
     31 		while (true)
     32 		{
     33 			int c = is.read();
     34 			if (c == -1)
     35 				throw new IOException("Premature connection close");
     36 
     37 			buffer[pos++] = (byte) c;
     38 
     39 			if (c == 13)
     40 			{
     41 				need10 = true;
     42 				continue;
     43 			}
     44 
     45 			if (c == 10)
     46 				break;
     47 
     48 			if (need10 == true)
     49 				throw new IOException("Malformed line sent by the server, the line does not end correctly.");
     50 
     51 			len++;
     52 			if (pos >= buffer.length)
     53 				throw new IOException("The server sent a too long line.");
     54 		}
     55 
     56 		return len;
     57 	}
     58 
     59 	public ClientServerHello(String identification, InputStream bi, OutputStream bo) throws IOException
     60 	{
     61 		client_line = "SSH-2.0-" + identification;
     62 
     63 		bo.write(StringEncoder.GetBytes(client_line + "\r\n"));
     64 		bo.flush();
     65 
     66 		byte[] serverVersion = new byte[512];
     67 
     68 		for (int i = 0; i < 50; i++)
     69 		{
     70 			int len = readLineRN(bi, serverVersion);
     71 
     72 			server_line = StringEncoder.GetString(serverVersion, 0, len);
     73 
     74 			if (server_line.startsWith("SSH-"))
     75 				break;
     76 		}
     77 
     78 		if (server_line.startsWith("SSH-") == false)
     79 			throw new IOException(
     80 					"Malformed server identification string. There was no line starting with 'SSH-' amongst the first 50 lines.");
     81 
     82 		if (server_line.startsWith("SSH-1.99-"))
     83 			server_versioncomment = server_line.substring(9);
     84 		else if (server_line.startsWith("SSH-2.0-"))
     85 			server_versioncomment = server_line.substring(8);
     86 		else
     87 			throw new IOException("Server uses incompatible protocol, it is not SSH-2 compatible.");
     88 	}
     89 
     90 	/**
     91 	 * @return Returns the client_versioncomment.
     92 	 */
     93 	public byte[] getClientString()
     94 	{
     95 		return StringEncoder.GetBytes(client_line);
     96 	}
     97 
     98 	/**
     99 	 * @return Returns the server_versioncomment.
    100 	 */
    101 	public byte[] getServerString()
    102 	{
    103 		return StringEncoder.GetBytes(server_line);
    104 	}
    105 }
    106