client.c 2.59 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
2
3
4
/* client.c
 *
 */

Niels Möller's avatar
Niels Möller committed
5
6
#include <stdio.h>

Niels Möller's avatar
Niels Möller committed
7
8
#include "client.h"
#include "version.h"
Niels Möller's avatar
Niels Möller committed
9
10
11
12
13
14
15
16
17
18
19
20
#include "session.h"
#include "abstract_io.h"
#include "read_line.h"
#include "read_packet.h"
#include "debug.h"
#include "format.h"
#include "werror.h"
#include "void.c"
#include "xalloc.h"

struct read_handler *make_client_read_line();
struct callback *make_client_close_handler();
Niels Möller's avatar
Niels Möller committed
21
22

static int client_initiate(struct client_callback *closure,
Niels Möller's avatar
Niels Möller committed
23
			   int fd)
Niels Möller's avatar
Niels Möller committed
24
{
Niels Möller's avatar
Niels Möller committed
25
26
27
28
29
30
31
32
  struct ssh_session *session = ssh_session_alloc();
  struct abstract_write *write =
    io_read_write(closure->backend, fd,
		  make_client_read_line(),
		  closure->block_size,
		  make_client_close_handler());
  
  session->client_version = ssh_format("SSH-" PROTOCOL_VERSION
Niels Möller's avatar
Niels Möller committed
33
34
				     "-" SOFTWARE_VERSION " %lS\r\n",
				     closure->id_comment);
Niels Möller's avatar
Niels Möller committed
35
36
  /* Copies the version string, so that it is isn't freed */
  return A_WRITE(write, ssh_format("%lS", session->client_version));
Niels Möller's avatar
Niels Möller committed
37
38
}

Niels Möller's avatar
Niels Möller committed
39
struct client_line_handler
Niels Möller's avatar
Niels Möller committed
40
{
Niels Möller's avatar
Niels Möller committed
41
  struct line_handler super;
Niels Möller's avatar
Niels Möller committed
42
  struct ssh_session *session;
Niels Möller's avatar
Niels Möller committed
43
};
Niels Möller's avatar
Niels Möller committed
44

Niels Möller's avatar
Niels Möller committed
45
46
47
48
49
static struct read_handler *do_line(struct client_line_handler *closure,
				    UINT32 length,
				    UINT8 *line)
{
  if ( (length >= 4) && !memcmp(line, "SSH-", 4))
Niels Möller's avatar
Niels Möller committed
50
51
    {
      /* Parse and remember format string */
Niels Möller's avatar
Niels Möller committed
52
53
      if ( ((length >= 8) && !memcmp(line + 4, "2.0-", 4))
	   || ((length >= 9) && !memcmp(line + 4, "1.99-", 5)))
Niels Möller's avatar
Niels Möller committed
54
	{
Niels Möller's avatar
Niels Möller committed
55
56
57
58
59
60
	  struct read_handler *new
	    = make_read_packet(make_debug_processor(make_packet_void(),
						    stderr),
			       closure->session->max_packet);
	  
	  closure->session->server_version
Niels Möller's avatar
Niels Möller committed
61
	    = ssh_format("%s", length, line);
Niels Möller's avatar
Niels Möller committed
62

Niels Möller's avatar
Niels Möller committed
63
64
65
66
	  /* FIXME: Cleanup properly. */
	  free(closure);

	  return new;
Niels Möller's avatar
Niels Möller committed
67
68
69
70
	}
      else
	{
	  werror("Unsupported protocol version: ");
Niels Möller's avatar
Niels Möller committed
71
	  werror_safe(length, line);
Niels Möller's avatar
Niels Möller committed
72
	  werror("\n");
Niels Möller's avatar
Niels Möller committed
73
74
	  /* FIXME: What could be returned here? */
	  fatal("client.c: do_line: Unsupported version.\n");
Niels Möller's avatar
Niels Möller committed
75
76
77
78
79
80
	  return 0;
	}
    }
  else
    {
      /* Display line */
Niels Möller's avatar
Niels Möller committed
81
      werror_safe(length, line);
Niels Möller's avatar
Niels Möller committed
82
83

      /* Read next line */
Niels Möller's avatar
Niels Möller committed
84
      return 0;
Niels Möller's avatar
Niels Möller committed
85
86
87
    }
}

Niels Möller's avatar
Niels Möller committed
88
89
90
91
92
93
94
95
96
97
98
struct read_handler *make_client_read_line(struct ssh_session *s)
{
  struct client_line_handler *closure
    = xalloc(sizeof(struct client_line_handler));
  
  closure->super.handler = (line_handler_f) do_line;
  closure->session = s;
  
  return make_read_line( (struct line_handler *) closure);
}
  
Niels Möller's avatar
Niels Möller committed
99
100
101
struct fd_callback *make_client_callback(struct io_backend *b,
					 UINT32 block_size)
{
Niels Möller's avatar
Niels Möller committed
102
  struct client_callback *connected = xalloc(sizeof(struct client_callback));
Niels Möller's avatar
Niels Möller committed
103
104
105
106
107
108
109

  connected->c.f = (fd_callback_f) client_initiate;
  connected->backend = b;
  connected->block_size = block_size;
  
  return (struct fd_callback *) connected;
}