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

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

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

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

37
38
39
struct channel_open_info
{
  UINT32 type_length;
40

41
42
43
44
45
  /* NOTE: This is a pointer into the packet, so if it is needed later
   * it must be copied. */
  const UINT8 *type_data;
  
  int type;
46

47
48
49
50
51
52
53
54
55
56
57
58
59
60
  UINT32 remote_channel_number;
  UINT32 send_window_size;
  UINT32 send_max_packet;
};

struct channel_request_info
{
  UINT32 type_length;
  const UINT8 *type_data;
  
  int type;

  int want_reply;
};
61

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

Niels Möller's avatar
Niels Möller committed
66
67
68
69
70
71
72
73
/* Channels are indexed by local channel number in some array. This
 * index is not stored in the channel struct. When sending messages on
 * the channel, it is identified by the *remote* sides index number,
 * and this number must be stored. */

#define CHANNEL_DATA 0
#define CHANNEL_STDERR_DATA 1

74
#define CHANNEL_SENT_CLOSE 1
75
#define CHANNEL_RECEIVED_CLOSE 2
76
#define CHANNEL_SENT_EOF 4
77
#define CHANNEL_RECEIVED_EOF 8
78

79
80
81
82
/* Normally, this flag is set, and we initiate channel close as soon
 * as we have both sent and received SSH_MSG_CHANNEL_EOF. Clearing
 * this flag keeps the channel open. */

83
#define CHANNEL_CLOSE_AT_EOF 0x10
84
85
86
87
88
89

/* 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. */

#define CHANNEL_NO_WAIT_FOR_EOF 0x20
90

91
/* GABA:
92
93
   (class
     (name ssh_channel)
94
     (super flow_controlled)
95
96
     (vars
       ; Remote channel number 
Niels Möller's avatar
Niels Möller committed
97
       (channel_number . UINT32)
98

99
100
101
102
103
104
105
       ; Where to pass errors. This is used for two different
       ; purposes: If opening the channel fails, EXC_CHANNEL_OPEN is
       ; raised. Once the channel is open, this handler is used for
       ; EXC_FINISH_CHANNEL and EXC_FINISH_PENDING. If the channel was
       ; opened on the peer's request, the connection's exception
       ; handler is a parent of the channel's. But that is not true in
       ; general.
Niels Möller's avatar
Niels Möller committed
106
       (e object exception_handler)
107
108
109
110

       ; Resources associated with the channel. This object is also
       ; put onto the connections resource list.
       (resources object resource_list)
Niels Möller's avatar
Niels Möller committed
111
       
112
113
114
       ; NOTE: The channels maximum packet sizes refer to the packet
       ; payload, i.e. the DATA string in SSH_CHANNEL_DATA and
       ; SSH_MSG_CHANNEL_EXTENDED_DATA.
115

Niels Möller's avatar
Niels Möller committed
116
117
       (rec_window_size . UINT32)
       (rec_max_packet . UINT32)
118

Niels Möller's avatar
Niels Möller committed
119
120
       (send_window_size . UINT32)
       (send_max_packet . UINT32)
121

122
123
       (connection object ssh_connection)
       
124
125
       (request_types object alist)

126
127
       ; If non-NULL, invoked for unknown channel requests.
       (request_fallback object channel_request)
128
       
Niels Möller's avatar
Niels Möller committed
129
       (flags . int)
130
131
132
133
134

       ; Number of files connected to this channel. For instance,
       ; stdout and stderr can be multiplexed on the same channel. We
       ; should not close the channel until we have got an EOF on both
       ; sources.
Niels Möller's avatar
Niels Möller committed
135
       (sources . int)
136
137

       ; Type is CHANNEL_DATA or CHANNEL_STDERR_DATA
Niels Möller's avatar
Niels Möller committed
138
       (receive method void "int type" "struct lsh_string *data")
139

140
141
142
       ; Called when we are allowed to send more data on the channel.
       ; Implies that the send_window_size is non-zero. 
       (send_adjust method void "UINT32 increment")
143

144
145
       ; Called when the channel is closed.
       ; Used by client_session and gateway_channel.
Niels Möller's avatar
Niels Möller committed
146
       (close method void)
147

148
       ; Called when eof is received on the channel (or when it is
149
       ; closed, whatever happens first).
Niels Möller's avatar
Niels Möller committed
150
       (eof method void)
151
152
  
       ; Reply from SSH_MSG_CHANNEL_OPEN_REQUEST
153
       (open_continuation object command_continuation)
154

155
       ; Queue of channel requests that we expect replies on
156
157
158
159
160
       (pending_requests struct object_queue)

       ; Channel requests that we have received, and should reply to
       ; in the right order
       (active_requests struct object_queue)))
161
       
162
*/
163

164
165
#define CHANNEL_RECEIVE(s, t, d) \
((s)->receive((s), (t), (d)))
Niels Möller's avatar
Niels Möller committed
166

167
#define CHANNEL_SEND_ADJUST(s, i) ((s)->send_adjust((s), (i)))
168
169
170
     
#define CHANNEL_CLOSE(s) \
((s)->close((s)))
Niels Möller's avatar
Niels Möller committed
171

172
173
#define CHANNEL_EOF(s) \
((s)->eof((s)))
Niels Möller's avatar
Niels Möller committed
174

175
176
#define CHANNEL_OPEN_CONFIRM(s) \
((s)->open_confirm((s)))
177

178
179
#define CHANNEL_OPEN_FAILURE(s) \
((s)->open_failure((s)))
Niels Möller's avatar
Niels Möller committed
180

181
182
183
184
185
/* Values used in the in_use array. */
#define CHANNEL_FREE 0
#define CHANNEL_RESERVED 1
#define CHANNEL_IN_USE 2

186
/* GABA:
187
188
189
190
   (class
     (name channel_table)
     (vars
       ; Channels are indexed by local number
Niels Möller's avatar
Niels Möller committed
191
192
       (channels space (object ssh_channel) used_channels)
       
193
194
195
196
       ; Global requests that we support
       (global_requests object alist)
       ; Channel types that we can open
       (channel_types object alist)
197
198
199
200
201

       ; FIXME: Add fallbacks, to be used for unknown requests
       ; and channel types.
       ; (global_fallback object ...)
       (open_fallback object channel_open)
202
       
203
204
205
206
       ; Allocation of local channel numbers is managed using the same
       ; method as is traditionally used for allocation of unix file 
       ; descriptors.

207
208
209
       ; Channel numbers can be reserved before there is any actual
       ; channel assigned to them. So the channels table is not enough
       ; for keeping track of which numbers are in use.
Niels Möller's avatar
Niels Möller committed
210
       (in_use space UINT8)
211
212

       ; Allocated size of the arrays.
Niels Möller's avatar
Niels Möller committed
213
       (allocated_channels . UINT32)
214
215
216

       ; Number of entries in the arrays that are in use and
       ; initialized.
Niels Möller's avatar
Niels Möller committed
217
       (used_channels . UINT32)
218
219
220
221
222
223
224

       ; The smallest channel number that is likely to be free
       (next_channel . UINT32)

       ; Number of currently allocated channel numbers.
       (channel_count . UINT32)
       
Niels Möller's avatar
Niels Möller committed
225
       (max_channels . UINT32) ; Max number of channels allowed 
226

227
228
229
       ; Forwarded TCP ports
       (local_ports struct object_queue)
       (remote_ports struct object_queue)
230
231
232
233
234
235

       ; Used if we're currently forwarding X11
       ; To support several screens at the same time,
       ; this should be replaced with a list, analogous to
       ; the remote_ports list above.
       (x11_display object client_x11_display)
236
       
237
238
       ; Global requests that we have received, and should reply to
       ; in the right order
239
       (active_global_requests struct object_queue)
240
241

       ; Queue of global requests that we expect replies on.
242
       (pending_global_requests struct object_queue)
243
       
244
       ; If non-zero, close connection after all active channels have
245
       ; died, and don't allow any new channels to be opened.
Niels Möller's avatar
Niels Möller committed
246
       (pending_close . int)))
247
*/
Niels Möller's avatar
Niels Möller committed
248
249

/* SSH_MSG_GLOBAL_REQUEST */
250

251
/* GABA:
252
253
254
   (class
     (name global_request)
     (vars
Niels Möller's avatar
Niels Möller committed
255
       (handler method void "struct ssh_connection *connection"
256
                            "UINT32 type"
257
258
			    ; want-reply is needed only by
			    ; do_gateway_global_request.
259
                            "int want_reply"
Niels Möller's avatar
Niels Möller committed
260
                            "struct simple_buffer *args"
261
262
			    "struct command_continuation *c"
			    "struct exception_handler *e")))
263
264
*/

265
266
#define GLOBAL_REQUEST(r, c, t, w, a, n, e) \
((r)->handler((r), (c), (t), (w), (a), (n), (e)))
Niels Möller's avatar
Niels Möller committed
267
268

/* SSH_MSG_CHANNEL_OPEN */
269
  
Niels Möller's avatar
Niels Möller committed
270
271
/* Raised if opening of a channel fails. Used both on the client and
 * the server side.*/
Niels Möller's avatar
Niels Möller committed
272
273
274
275
276
277
278
279
/* GABA:
   (class
     (name channel_open_exception)
     (super exception)
     (vars
       (error_code . UINT32)))
*/

280
281
struct exception *
make_channel_open_exception(UINT32 error_code, const char *msg);
Niels Möller's avatar
Niels Möller committed
282

283

284
/* GABA:
285
286
287
   (class
     (name channel_open)
     (vars
Niels Möller's avatar
Niels Möller committed
288
       (handler method void
289
                "struct ssh_connection *connection"
290
		"struct channel_open_info *info"
291
                "struct simple_buffer *data"
Niels Möller's avatar
Niels Möller committed
292
293
                "struct command_continuation *c"
		"struct exception_handler *e")))
294
295
*/

296
297
#define CHANNEL_OPEN(o, c, i, d, r, e) \
((o)->handler((o), (c), (i), (d), (r), (e)))
Niels Möller's avatar
Niels Möller committed
298

299
300
301
302
303
304
305
306
307
308
309
310
311
#define DEFINE_CHANNEL_OPEN(name)                       \
static void do_##name(struct channel_open *s,           \
		     struct ssh_connection *connection, \
		     struct channel_open_info *info,    \
		     struct simple_buffer *args,        \
		     struct command_continuation *c,    \
		     struct exception_handler *e);      \
                                                        \
struct channel_open name =                              \
{ STATIC_HEADER, do_##name };                           \
                                                        \
static void do_##name

Niels Möller's avatar
Niels Möller committed
312
/* SSH_MSG_CHANNEL_REQUEST */
313

314
/* GABA:
315
316
317
   (class
     (name channel_request)
     (vars
Niels Möller's avatar
Niels Möller committed
318
       (handler method void
319
		"struct ssh_channel *channel"
320
		"struct channel_request_info *info"
321
322
323
		"struct simple_buffer *args"
		"struct command_continuation *c"
		"struct exception_handler *e")))
324
325
*/

326
327
#define CHANNEL_REQUEST(s, c, i, a, n, e) \
((s)->handler((s), (c), (i), (a), (n), (e)))
328

329
330
331
332
333
334
335
336
337
338
339
340
#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
341

342
343
void init_channel(struct ssh_channel *channel);

344
345
346
struct channel_table *make_channel_table(void);
int alloc_channel(struct channel_table *table);
void dealloc_channel(struct channel_table *table, int i);
Niels Möller's avatar
Niels Möller committed
347

348
349
350
351
void
use_channel(struct ssh_connection *connection,
	    UINT32 local_channel_number);

352
void
353
register_channel(UINT32 local_channel_number,
354
355
		 struct ssh_channel *channel,
		 int take_into_use);
Niels Möller's avatar
Niels Möller committed
356

357
358
359
360
struct ssh_channel *
lookup_channel(struct channel_table *table, UINT32 i);
struct ssh_channel *
lookup_channel_reserved(struct channel_table *table, UINT32 i);
Niels Möller's avatar
Niels Möller committed
361

362
363
364
365
struct abstract_write *make_channel_write(struct ssh_channel *channel);
struct abstract_write *make_channel_write_extended(struct ssh_channel *channel,
						   UINT32 type);

366
367
struct io_callback *make_channel_read_data(struct ssh_channel *channel);
struct io_callback *make_channel_read_stderr(struct ssh_channel *channel);
368

Niels Möller's avatar
Niels Möller committed
369
struct lsh_string *format_global_failure(void);
370
struct lsh_string *format_global_success(void);
371

Niels Möller's avatar
Niels Möller committed
372
struct lsh_string *format_open_failure(UINT32 channel, UINT32 reason,
373
				       const char *msg, const char *language);
374
375
struct lsh_string *format_open_confirmation(struct ssh_channel *channel,
					    UINT32 channel_number,
376
					    const char *format, ...);
377
378

struct lsh_string *format_channel_success(UINT32 channel);
Niels Möller's avatar
Niels Möller committed
379
380
struct lsh_string *format_channel_failure(UINT32 channel);

Niels Möller's avatar
Niels Möller committed
381
382
383
struct lsh_string *prepare_window_adjust(struct ssh_channel *channel,
					 UINT32 add);

384
385
386
void
channel_start_receive(struct ssh_channel *channel,
		      UINT32 initial_window_size);
387

388
struct lsh_string *
389
format_channel_open_s(struct lsh_string *type,
390
391
392
393
		      UINT32 local_channel_number,
		      struct ssh_channel *channel,
		      struct lsh_string *args);

394
395
396
397
struct lsh_string *
format_channel_open(int type, UINT32 local_channel_number,
		    struct ssh_channel *channel,
		    const char *format, ...);
398

399
400
401
402
403
404
405
406
407
408
struct lsh_string *
format_channel_request_i(struct channel_request_info *info,
			 struct ssh_channel *channel,
			 UINT32 args_length, const UINT8 *args_data);

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

410
411
412
413
struct lsh_string *
format_global_request(int type, int want_reply,
		      const char *format, ...);

414
415
struct lsh_string *format_channel_close(struct ssh_channel *channel);
struct lsh_string *format_channel_eof(struct ssh_channel *channel);
416

Niels Möller's avatar
Niels Möller committed
417
418
void channel_close(struct ssh_channel *channel);
void channel_eof(struct ssh_channel *channel);
419

420
struct lsh_callback *
421
422
423
424
425
make_channel_read_close_callback(struct ssh_channel *channel);

struct exception_handler *
make_channel_io_exception_handler(struct ssh_channel *channel,
				  const char *prefix,
426
				  int silent,
427
428
				  struct exception_handler *parent,
				  const char *context);
429

430
431
struct lsh_string *channel_transmit_data(struct ssh_channel *channel,
					 struct lsh_string *data);
432

433
434
435
struct lsh_string *channel_transmit_extended(struct ssh_channel *channel,
					     UINT32 type,
					     struct lsh_string *data);
436

437
438
void init_connection_service(struct ssh_connection *connection);

439
440
extern struct command connection_service_command;
#define INIT_CONNECTION_SERVICE (&connection_service_command.super)
441

Niels Möller's avatar
Niels Möller committed
442

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