debug.c 3.24 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
38
39
40
41
#include "debug.c.x"

/* CLASS:
   (class
     (name packet_debug)
     (super abstract_write_pipe)
     (vars
42
       (prefix simple "const char *")))
43
44
*/

45
static int do_debug(struct abstract_write *w,
Niels Möller's avatar
Niels Möller committed
46
		    struct lsh_string *packet)
47
{
48
  CAST(packet_debug, closure, w);
49
  
Niels Möller's avatar
Niels Möller committed
50
  debug("DEBUG: received packet");
51
52
53
  debug_hex(packet->length, packet->data);
  debug("\n");
  
Niels Möller's avatar
Niels Möller committed
54
  return A_WRITE(closure->super.next, packet);
55
56
}

Niels Möller's avatar
Niels Möller committed
57
struct abstract_write *
58
make_packet_debug(struct abstract_write *continuation, const char *prefix)
59
{
60
  NEW(packet_debug, closure);
61

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

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

69
static struct lsh_string *make_debug_packet(const char *msg, int always_display)
70
71
72
73
74
75
76
77
78
79
{
  return ssh_format("%c%c%z%z",
		    SSH_MSG_DEBUG,
		    always_display,
		    msg,
		    /* Empty language tag */ 
		    "");
}

/* Send a debug message to the other end. */
80
int send_debug(struct abstract_write *write, const char *msg, int always_display)
81
82
83
{
  return (debug_flag)
    ? A_WRITE(write, make_debug_packet(msg, always_display))
Niels Möller's avatar
Niels Möller committed
84
    : LSH_OK | LSH_GOON;
85
86
87
  
}

88
int send_verbose(struct abstract_write *write, const char *msg, int always_display)
89
90
91
{
  return (verbose_flag)
    ? A_WRITE(write, make_debug_packet(msg, always_display))
Niels Möller's avatar
Niels Möller committed
92
    : LSH_OK | LSH_GOON;
93
94
}

95
96
static int do_rec_debug(struct packet_handler *self UNUSED,
			struct ssh_connection *connection UNUSED,
97
98
99
			struct lsh_string *packet)
{
  struct simple_buffer buffer;
100
101
  int msg_number;
  int always_display;
102
103
104
105
106
107
108
109
110
111
112
113
114
  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);
Niels Möller's avatar
Niels Möller committed
115
      return LSH_FAIL | LSH_DIE;
116
117
118
119
    }

  if (always_display)
    {
120
      wwrite("Received debug: ");
121
      werror_utf8(length, msg);
122
      wwrite("\n");
123
124
125
    }
  else
    {
Niels Möller's avatar
Niels Möller committed
126
      verbose("Received debug: ");
127
128
129
      verbose_utf8(length, msg);
      verbose("\n");
    }
130

131
  lsh_string_free(packet);
Niels Möller's avatar
Niels Möller committed
132
  return LSH_OK | LSH_GOON;
133
134
135
136
}

struct packet_handler *make_rec_debug_handler(void)
{
137
  NEW(packet_handler, self);
138
139
140
141
142

  self->handler = do_rec_debug;

  return self;
}