channel.h 7.91 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
2
/* channel.h
 *
3
4
 * Information about ssh channels.
 *
Niels Möller's avatar
Niels Möller committed
5
6
 */

Niels Möller's avatar
Niels Möller committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/* 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

Niels Möller's avatar
Niels Möller committed
26
27
28
#ifndef LSH_CHANNEL_H_INCLUDED
#define LSH_CHANNEL_H_INCLUDED

Niels Möller's avatar
Niels Möller committed
29
#include "alist.h"
30
#include "command.h"
31
#include "connection.h"
Niels Möller's avatar
Niels Möller committed
32
#include "parse.h"
Niels Möller's avatar
Niels Möller committed
33
#include "server_pty.h"
Niels Möller's avatar
Niels Möller committed
34

35
36
37
38
39
40
41
42
enum channel_event
{
  /* We receieved a CHANNEL_OPEN_CONFIRMATION. */
  CHANNEL_EVENT_CONFIRM = 1,
#if 0
  /* We received a CHANNEL_OPEN_FAILURE. */
  CHANNEL_EVENT_DENY,
#endif
43
  /* We received a CHANNEL_EOF. */
44
45
46
47
48
49
50
  CHANNEL_EVENT_EOF,
  /* Local buffers are full. Stop sending data. */
  CHANNEL_EVENT_STOP,
  /* Start sending again (subject to the current send window size). */
  CHANNEL_EVENT_START,
};

51
52
struct channel_request_info
{
53
54
  uint32_t type_length;
  const uint8_t *type_data;
55
56
57
58
59
  
  int type;

  int want_reply;
};
60

61
#define GABA_DECLARE
62
#include "channel.h.x"
63
#undef GABA_DECLARE
64

65
66
67
/* Channels are indexed by local channel number in some array. When
   sending messages on the channel, it is identified by the *remote*
   side's index number, and this number must be stored. */
Niels Möller's avatar
Niels Möller committed
68

69
70
71
72
enum channel_data_type {
  CHANNEL_DATA = 0,
  CHANNEL_STDERR_DATA = 1,
};
73

74
75
76
77
78
enum channel_flag {
  CHANNEL_SENT_CLOSE = 1,
  CHANNEL_RECEIVED_CLOSE = 2,
  CHANNEL_SENT_EOF = 4,
  CHANNEL_RECEIVED_EOF = 8,
79

80
81
82
83
84
  /* This flags means that we don't expect any more data from the other
     end, and that we don't want to wait for an SSH_MSG_CHANNEL_EOF
     before closing the channel. */
  CHANNEL_NO_WAIT_FOR_EOF = 0x10
};
85

86
/* FIXME: Inherit resource */
87
/* GABA:
88
89
   (class
     (name ssh_channel)
90
     (super resource)
91
92
     (vars
       ; Remote channel number 
93
94
95
       (local_channel_number . uint32_t)
       (remote_channel_number . uint32_t)
       
96
       ; NOTE: The channel's maximum packet sizes refer to the packet
97
98
       ; payload, i.e. the DATA string in SSH_CHANNEL_DATA and
       ; SSH_MSG_CHANNEL_EXTENDED_DATA.
99

100
101
       (rec_window_size . uint32_t)
       (rec_max_packet . uint32_t)
102

103
104
       (send_window_size . uint32_t)
       (send_max_packet . uint32_t)
105

106
       (connection object ssh_connection)
107
       
108
109
       (request_types object alist)

110
111
       ; If non-NULL, invoked for unknown channel requests.
       (request_fallback object channel_request)
112
       
Niels Möller's avatar
Niels Möller committed
113
       (flags . int)
114

115
116
117
118
119
120
121
122
123
124
125
126
       ; Number of sources connected to this channel. We should not
       ; send CHANNEL_EOF until we have got EOF on all sources (e.g.
       ; stdout and stderr)
       (sources . unsigned)

       ; Number of sinks connected to the channel. We should not send
       ; CHANNEL_CLOSE until we have received CHANNEL_EOF and all
       ; buffered data have been written to the sinks. NOTE: A pending
       ; exit-status/exit-signal message to be sent or received is
       ; also book-keeped as a sink.       
       (sinks . unsigned)
       
127
       ; Type is CHANNEL_DATA or CHANNEL_STDERR_DATA
128
129
       (receive method void "int type"
		"uint32_t length" "const uint8_t *data")
130

131
132
       ; Called when we are allowed to send more data on the channel.
       ; Implies that the send_window_size is non-zero. 
133
       (send_adjust method void "uint32_t increment")
134

135
       (event method void "enum channel_event")
136
  
137
138
       ; Pass on responce from CHANNEL_OPEN
       (channel_open_context object command_context)
139

140
       ; Queue of channel requests that we expect replies on
141
142
143
144
145
       (pending_requests struct object_queue)

       ; Channel requests that we have received, and should reply to
       ; in the right order
       (active_requests struct object_queue)))
146
       
147
*/
148

149
150
#define CHANNEL_RECEIVE(s, t, l, d) \
((s)->receive((s), (t), (l), (d)))
Niels Möller's avatar
Niels Möller committed
151

152
#define CHANNEL_SEND_ADJUST(s, i) ((s)->send_adjust((s), (i)))
153
154
155
     
#define CHANNEL_CLOSE(s) \
((s)->close((s)))
Niels Möller's avatar
Niels Möller committed
156

157
158
#define CHANNEL_EVENT(s, t) \
((s)->event((s), (t)))
Niels Möller's avatar
Niels Möller committed
159

160
161
#define CHANNEL_OPEN_CONFIRM(s) \
((s)->open_confirm((s)))
162

163
164
#define CHANNEL_OPEN_FAILURE(s) \
((s)->open_failure((s)))
Niels Möller's avatar
Niels Möller committed
165

166

167

Niels Möller's avatar
Niels Möller committed
168
/* SSH_MSG_CHANNEL_REQUEST */
169

170
/* GABA:
171
172
173
   (class
     (name channel_request)
     (vars
Niels Möller's avatar
Niels Möller committed
174
       (handler method void
175
		"struct ssh_channel *channel"
176
		"struct channel_request_info *info"
177
178
179
		"struct simple_buffer *args"
		"struct command_continuation *c"
		"struct exception_handler *e")))
180
181
*/

182
183
#define CHANNEL_REQUEST(s, c, i, a, n, e) \
((s)->handler((s), (c), (i), (a), (n), (e)))
184

185
186
187
188
189
190
191
192
193
194
195
196
#define DEFINE_CHANNEL_REQUEST(name)                            \
static void do_##name(struct channel_request *s,                \
		      struct ssh_channel *channel,              \
                      struct channel_request_info *info,        \
		      struct simple_buffer *args,               \
		      struct command_continuation *c,           \
		      struct exception_handler *e);             \
                                                                \
struct channel_request name =                                   \
{ STATIC_HEADER, do_##name };                                   \
                                                                \
static void do_##name
197

198
void
199
init_channel(struct ssh_channel *channel,
200
201
	     void (*kill)(struct resource *),
	     void (*event)(struct ssh_channel *, enum channel_event));
202

203
void
204
register_channel(struct ssh_connection *table,
205
		 uint32_t local_channel_number,
206
207
		 struct ssh_channel *channel,
		 int take_into_use);
Niels Möller's avatar
Niels Möller committed
208

209
/* FIXME: Make static? */
210
struct ssh_channel *
211
lookup_channel(struct ssh_connection *table, uint32_t i);
212
struct ssh_channel *
213
lookup_channel_reserved(struct ssh_connection *table, uint32_t i);
Niels Möller's avatar
Niels Möller committed
214

215
216
void
channel_adjust_rec_window(struct ssh_channel *channel, uint32_t written);
Niels Möller's avatar
Niels Möller committed
217

218
219
void
channel_start_receive(struct ssh_channel *channel,
220
		      uint32_t initial_window_size);
221

222
223
224
225
226
227
228
229
int
channel_open_new_v(struct ssh_connection *connection,
		   struct ssh_channel *channel,
		   uint32_t type_length, const uint8_t *type,
		   const char *format, va_list args);

int
channel_open_new_type(struct ssh_connection *connection,
230
		      struct ssh_channel *channel,
231
232
		      int type,
		      const char *format, ...);
233

234
235
236
struct lsh_string *
format_channel_request_i(struct channel_request_info *info,
			 struct ssh_channel *channel,
237
			 uint32_t args_length, const uint8_t *args_data);
238
239
240
241
242
243

struct lsh_string *
format_channel_request(int type,
		       struct ssh_channel *channel,
		       int want_reply,
		       const char *format, ...);
244

245
246
247
248
struct lsh_string *
format_global_request(int type, int want_reply,
		      const char *format, ...);

249
250
struct lsh_string *format_channel_close(struct ssh_channel *channel);
struct lsh_string *format_channel_eof(struct ssh_channel *channel);
251

252
253
254
255
256
257
258
259
void
channel_eof(struct ssh_channel *channel);

void
channel_close(struct ssh_channel *channel);

void
channel_maybe_close(struct ssh_channel *channel);
260

261
struct lsh_callback *
262
263
264
265
266
make_channel_read_close_callback(struct ssh_channel *channel);

struct exception_handler *
make_channel_io_exception_handler(struct ssh_channel *channel,
				  const char *prefix,
267
				  int silent,
268
269
				  struct exception_handler *parent,
				  const char *context);
270

271
void
272
channel_transmit_data(struct ssh_channel *channel,
273
		      uint32_t length, const uint8_t *data);
274

275
void
276
277
channel_transmit_extended(struct ssh_channel *channel,
			  uint32_t type,
278
			  uint32_t length, const uint8_t *data);
279

Niels Möller's avatar
Niels Möller committed
280
int
281
channel_packet_handler(struct ssh_connection *table,
Niels Möller's avatar
Niels Möller committed
282
		       uint32_t length, const uint8_t *packet);
283

Niels Möller's avatar
Niels Möller committed
284
#endif /* LSH_CHANNEL_H_INCLUDED */