disconnect.c 2.09 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/* disconnect.c
 *
 * $Id$ */

/* lsh, an implementation of the ssh protocol
 *
 * Copyright (C) 1998 Niels Möller
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "disconnect.h"

#include "connection.h"
#include "format.h"
#include "parse.h"
#include "ssh.h"
Niels Möller's avatar
Niels Möller committed
30
#include "werror.h"
Niels Möller's avatar
Niels Möller committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "xalloc.h"

struct lsh_string *format_disconnect(int code, char *msg)
{
  return ssh_format("%c%i%z%z",
		    SSH_MSG_DISCONNECT,
		    code,
		    msg, "");
}

static int do_disconnect(struct packet_handler *closure,
			 struct ssh_connection *connection,
			 struct lsh_string *packet)
{
  struct simple_buffer buffer;
Niels Möller's avatar
Niels Möller committed
46
  UINT8 msg_number;
Niels Möller's avatar
Niels Möller committed
47
48
  UINT32 length;
  UINT32 reason;
Niels Möller's avatar
Niels Möller committed
49
  UINT8 *msg;
Niels Möller's avatar
Niels Möller committed
50
51
52
  
  simple_buffer_init(&buffer, packet->length, packet->data);

Niels Möller's avatar
Niels Möller committed
53
54
  if (parse_uint8(&buffer, &msg_number)
      && (msg_number == SSH_MSG_DISCONNECT)
Niels Möller's avatar
Niels Möller committed
55
      && (parse_uint32(&buffer, &reason))
Niels Möller's avatar
Niels Möller committed
56
      && (parse_string(&buffer, &length, &msg))
Niels Möller's avatar
Niels Möller committed
57
58
      /* FIXME: Language tag is ignored */ )
    {
Niels Möller's avatar
Niels Möller committed
59
60
      /* FIXME: Display a better message */
      werror("Disconnect for reason %d\n", reason);
61
      werror_utf8(length, msg);
Niels Möller's avatar
Niels Möller committed
62
    }
Niels Möller's avatar
Niels Möller committed
63
64
  else
    werror("Invalid disconnect message!\n");
Niels Möller's avatar
Niels Möller committed
65
66
67
68
69
  lsh_string_free(packet);
  
  /* FIXME: Mark the file as closed, somehow (probably a variable in
   * the write buffer) */

Niels Möller's avatar
Niels Möller committed
70
  return LSH_CLOSE;
Niels Möller's avatar
Niels Möller committed
71
72
}

Niels Möller's avatar
Niels Möller committed
73
struct packet_handler *make_disconnect_handler(void)
Niels Möller's avatar
Niels Möller committed
74
75
76
77
78
79
{
  struct packet_handler *res =  xalloc(sizeof(struct packet_handler));

  res->handler = do_disconnect;
  return res;
}