channel.h 9.79 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 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
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"
Niels Möller's avatar
Niels Möller committed
35

36
#define GABA_DECLARE
37
#include "channel.h.x"
38
#undef GABA_DECLARE
39

Niels Möller's avatar
Niels Möller committed
40
41
42
43
44
45
46
47
/* 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

48
#define CHANNEL_SENT_CLOSE 1
49
#define CHANNEL_RECEIVED_CLOSE 2
50
#define CHANNEL_SENT_EOF 4
51
#define CHANNEL_RECEIVED_EOF 8
52

53
/* Means that we should send close when we have both sent and received EOF. */
54
#define CHANNEL_CLOSE_AT_EOF 0x10
55

56

57
/* GABA:
58
59
   (class
     (name ssh_channel)
60
     (super flow_controlled)
61
62
63
64
     (vars
       ; Remote channel number 
       (channel_number simple UINT32)

65
       ; We try to keep the rec_window_size between max_window / 2
66
67
68
69
70
71
72
73
74
75
76
       ; and max_window.
       (max_window simple UINT32)       

       (rec_window_size simple UINT32)
       (rec_max_packet simple UINT32)

       (send_window_size simple UINT32)
       (send_max_packet simple UINT32)

       ; FIXME: Perhaps this should be moved to the channel_table, and
       ; a pointer to that table be stored here instead?
77
78
       ; Now that we pass the connection pointer to most functions,
       ; is this field needed at all?
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
       (write object abstract_write)
  
       (request_types object alist)

       (flags simple int)

       ; 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.
       (sources simple int)

       ; FIXME: What about return values from these functions? A
       ; channel may fail to process it's data. Is there some way to
       ; propagate a channel broken message to the other end? 

       ; Type is CHANNEL_DATA or CHANNEL_STDERR_DATA
96
       (receive method int "int type" "struct lsh_string *data")
97
98
99
100

       ; Called when we are allowed to send data on the channel. 
       (send method int)

Niels Möller's avatar
Niels Möller committed
101
102
       ; Called when the channel is closed
       ; FIXME: Is this needed for anything?
103
       (close method int)
104

105
       ; Called when eof is received on the channel (or when it is
106
107
108
109
       ; closed, whatever happens first).
       (eof method int)
  
       ; Reply from SSH_MSG_CHANNEL_OPEN_REQUEST
110
111
112
       ;; (open_confirm method int)
       ;; (open_failure method int)
       (open_continuation object command_continuation)
113

114
       ; Queue of channel requests that we expect replies on
115
116
       (pending_requests struct object_queue)))
       
117
       ; Reply from SSH_MSG_CHANNEL_REQUEST 
118
119
120
       ;; (channel_success method int)
       ;; (channel_failure method int)))
*/
121

122
123
#define CHANNEL_RECEIVE(s, t, d) \
((s)->receive((s), (t), (d)))
Niels Möller's avatar
Niels Möller committed
124

125
126
127
128
#define CHANNEL_SEND(s) ((s)->send((s)))
     
#define CHANNEL_CLOSE(s) \
((s)->close((s)))
Niels Möller's avatar
Niels Möller committed
129

130
131
#define CHANNEL_EOF(s) \
((s)->eof((s)))
Niels Möller's avatar
Niels Möller committed
132

133
134
#define CHANNEL_OPEN_CONFIRM(s) \
((s)->open_confirm((s)))
135

136
137
#define CHANNEL_OPEN_FAILURE(s) \
((s)->open_failure((s)))
Niels Möller's avatar
Niels Möller committed
138

139
     
Niels Möller's avatar
Niels Möller committed
140
/* FIXME: Perhaps, this information is better kept in the connection
141
 * object? */
142

143
/* GABA:
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
   (class
     (name channel_table)
     (vars
       ; FIXME: This is relevant only for the server side. It's
       ; probably better to store this in the connection struct.

       ;; uid_t user;  ; Authenticated user 

       ; Channels are indexed by local number
       (channels pointer (object ssh_channel) used_channels)

       ; Allocation of local channel numbers is managed using the same
       ; method as is traditionally used for allocation of unix file 
       ; descriptors.

       (allocated_channels simple UINT32)
       (next_channel simple UINT32)
     
       (used_channels simple UINT32)
       (max_channels simple UINT32) ; Max number of channels allowed 

165
166
       ; Global requests that we have received, and should reply to
       ; in the right order
167
       (active_global_requests struct object_queue)
168
169

       ; Queue of global requests that we expect replies on.
170
       (pending_global_requests struct object_queue)
171
       
172
173
174
175
176
177
178
179
       ; If non-zero, close connection after all active channels have
       ; died.
       (pending_close simple int)

       ; FIXME: Perhaps we should use an flag to indicate whether or
       ; not new channels can be opened?
       ))
*/
Niels Möller's avatar
Niels Möller committed
180
181

/* SSH_MSG_GLOBAL_REQUEST */
182
183
184
185
186
187
188
189
190
191
192
193

/* GABA:
   (class
     (name global_request_callback)
     (vars
       (response method int "int success")
       (connection object ssh_connection)))
*/

#define GLOBAL_REQUEST_CALLBACK(c, s) \
((c) ? ((c)->response((c), (s))) : LSH_OK | LSH_GOON)

194
/* GABA:
195
196
197
   (class
     (name global_request)
     (vars
198
       (handler method int "struct ssh_connection *connection"
199
200
                           "struct simple_buffer *args"
			   "struct global_request_callback *response")))
201
202
*/

203
#define GLOBAL_REQUEST(r, c, a, n) ((r)->handler((r), (c), (a), (n)))
Niels Möller's avatar
Niels Möller committed
204
205

/* SSH_MSG_CHANNEL_OPEN */
206
207
208
209
  
/* Callback function, used to report success or failure for a
 * requested channel open. */
  
210
/* GABA:
211
212
213
214
215
216
217
218
219
220
   (class
     (name channel_open_callback)
     (vars
       (response method int
                "struct ssh_channel *channel"
                "UINT32 error"
                "char *error_msg"
                "struct lsh_string *args")
       (connection object ssh_connection)))
*/
221

222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* xxCLASS:
   (class
     (name channel_open_response)
     (vars
       (response method int
                "struct ssh_channel *channel"
                "UINT32 error"
		; FIXME: Use an lsh_string for error messages
                "char *error_msg"
                "struct lsh_string *args")
       (connection object ssh_connection)
       (remote_channel_number simple UINT32)
       (window_size simple UINT32)
       (max_packet simple UINT32)))
*/

#define CHANNEL_OPEN_CALLBACK(c, ch, e, m, a) \
  (c)->response((c), (ch), (e), (m), (a))

241
/* GABA:
242
243
244
   (class
     (name channel_open)
     (vars
245
       (handler method int
246
                "struct ssh_connection *connection"
247
248
                "struct simple_buffer *data"
                "struct channel_open_callback *response")))
249
250
*/

251
252
#define CHANNEL_OPEN(o, c, d, n) \
((o)->handler((o), (c), (d), (n)))
Niels Möller's avatar
Niels Möller committed
253

Niels Möller's avatar
Niels Möller committed
254
/* SSH_MSG_CHANNEL_REQUEST */
255
/* GABA:
256
257
258
259
260
   (class
     (name channel_request)
     (vars
       (handler method int
		"struct ssh_channel *channel"
261
		"struct ssh_connection *connection"
262
263
264
265
		"int want_reply"
		"struct simple_buffer *args")))
*/

266
267
#define CHANNEL_REQUEST(s, c, conn, w, a) \
((s)->handler((s), (c), (conn), (w), (a)))
Niels Möller's avatar
Niels Möller committed
268

269
/* ;;GABA:
270
271
272
273
   (class
     (name connection_startup)
     (vars
       (start method int
274
	      "struct ssh_connection *connection")))
275
276
*/

277
/* #define CONNECTION_START(c, s) ((c)->start((c), (s))) */
278

279
280
void init_channel(struct ssh_channel *channel);

281
282
283
284
285
struct channel_table *make_channel_table(void);
int alloc_channel(struct channel_table *table);
void dealloc_channel(struct channel_table *table, int i);
int register_channel(struct channel_table *table, struct ssh_channel *channel);
struct ssh_channel *lookup_channel(struct channel_table *table, UINT32 i);
Niels Möller's avatar
Niels Möller committed
286

287
288
289
290
291
292
293
struct abstract_write *make_channel_write(struct ssh_channel *channel);
struct abstract_write *make_channel_write_extended(struct ssh_channel *channel,
						   UINT32 type);

struct read_handler *make_channel_read_data(struct ssh_channel *channel);
struct read_handler *make_channel_read_stderr(struct ssh_channel *channel);

Niels Möller's avatar
Niels Möller committed
294
struct lsh_string *format_global_failure(void);
295
struct lsh_string *format_global_success(void);
296

Niels Möller's avatar
Niels Möller committed
297
struct lsh_string *format_open_failure(UINT32 channel, UINT32 reason,
298
				       const char *msg, const char *language);
299
300
struct lsh_string *format_open_confirmation(struct ssh_channel *channel,
					    UINT32 channel_number,
301
					    const char *format, ...);
302
303

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

Niels Möller's avatar
Niels Möller committed
306
307
308
struct lsh_string *prepare_window_adjust(struct ssh_channel *channel,
					 UINT32 add);

309
struct lsh_string *prepare_channel_open(struct channel_table *table,
310
311
					int type,
					struct ssh_channel *channel,
312
					const char *format, ...);
313

314
315
316
317
struct lsh_string *format_channel_request(int type,
					  struct ssh_channel *channel,
					  int want_reply,
					  const char *format, ...);
318

319
320
struct lsh_string *format_channel_close(struct ssh_channel *channel);
struct lsh_string *format_channel_eof(struct ssh_channel *channel);
321

322
323
int channel_close(struct ssh_channel *channel);
int channel_eof(struct ssh_channel *channel);
324

325
326
struct close_callback *make_channel_close(struct ssh_channel *channel);

327
328
struct lsh_string *channel_transmit_data(struct ssh_channel *channel,
					 struct lsh_string *data);
329

330
331
332
struct lsh_string *channel_transmit_extended(struct ssh_channel *channel,
					     UINT32 type,
					     struct lsh_string *data);
333

334
335
struct command *make_connection_service(struct alist *global_requests,
					struct alist *channel_types);
336

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

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