debug.c 3.23 KB
Newer Older
1
2
/* debug.c
 *
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 *
 *
 * $Id$ */

/* lsh, an implementation of the ssh protocol
 *
 * Copyright (C) 1998 Niels Mller
 *
 * 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.
24
25
 */

Niels Möller's avatar
Niels Möller committed
26
#include "debug.h"
27
28
29
30
31

#include "connection.h"
#include "format.h"
#include "parse.h"
#include "ssh.h"
Niels Möller's avatar
Niels Möller committed
32
#include "xalloc.h"
33
#include "werror.h"
Niels Möller's avatar
Niels Möller committed
34

35
36
37
struct packet_debug
{
  struct abstract_write_pipe super;
38
  char *prefix;
39
40
};

41
static int do_debug(struct abstract_write **w,
Niels Möller's avatar
Niels Möller committed
42
		    struct lsh_string *packet)
43
{
44
45
46
  struct packet_debug *closure
    = (struct packet_debug *) *w;
  
Niels Möller's avatar
Niels Möller committed
47
  MDEBUG(closure);
48

49
50
51
52
  debug("DEBUG: recieved packet");
  debug_hex(packet->length, packet->data);
  debug("\n");
  
Niels Möller's avatar
Niels Möller committed
53
  return A_WRITE(closure->super.next, packet);
54
55
}

Niels Möller's avatar
Niels Möller committed
56
struct abstract_write *
57
make_packet_debug(struct abstract_write *continuation, char *prefix)
58
{
59
  struct packet_debug *closure = xalloc(sizeof(struct packet_debug));
60

61
  closure->super.super.write = do_debug;
Niels Möller's avatar
Niels Möller committed
62
  closure->super.next = continuation;
63
  closure->prefix = prefix;
64

65
  return &closure->super.super;
66
67
}

68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
static struct lsh_string *make_debug_packet(char *msg, int always_display)
{
  return ssh_format("%c%c%z%z",
		    SSH_MSG_DEBUG,
		    always_display,
		    msg,
		    /* Empty language tag */ 
		    "");
}

/* Send a debug message to the other end. */
int send_debug(struct abstract_write *write, char *msg, int always_display)
{
  return (debug_flag)
    ? A_WRITE(write, make_debug_packet(msg, always_display))
    : WRITE_OK;
  
}

int send_verbose(struct abstract_write *write, char *msg, int always_display)
{
  return (verbose_flag)
    ? A_WRITE(write, make_debug_packet(msg, always_display))
    : WRITE_OK;
}

static int do_rec_debug(struct packet_handler *self,
			struct ssh_connection *connection,
			struct lsh_string *packet)
{
  struct simple_buffer buffer;
  UINT8 msg_number;
  UINT8 always_display;
  UINT32 length;
  UINT8 *msg;
  int language;
  
  simple_buffer_init(&buffer, packet->length, packet->data);

  if (!(parse_uint8(&buffer, &msg_number)
	&& parse_uint8(&buffer, &always_display)
	&& parse_string(&buffer, &length, &msg)
	&& parse_atom(&buffer, &language)
	&& parse_eod(&buffer)))
    {
      lsh_string_free(packet);
      return WRITE_CLOSED;
    }

  if (always_display)
    {
      werror("Recieved debug: ");
      werror_utf8(length, msg);
      werror("\n");
    }
  else
    {
      verbose("Recieved debug: ");
      verbose_utf8(length, msg);
      verbose("\n");
    }
129

130
131
132
133
134
135
136
137
138
139
140
141
  lsh_string_free(packet);
  return WRITE_OK;
}

struct packet_handler *make_rec_debug_handler(void)
{
  struct packet_handler *self = xalloc(sizeof(struct packet_handler));

  self->handler = do_rec_debug;

  return self;
}