channel.c 46.2 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/* channel.c
 *
 * $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
J.H.M. Dassen's avatar
J.H.M. Dassen committed
22
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
Niels Möller's avatar
Niels Möller committed
23
24
25
26
27
 */

#include "channel.h"

#include "format.h"
28
#include "io.h"
Niels Möller's avatar
Niels Möller committed
29
#include "read_data.h"
Niels Möller's avatar
Niels Möller committed
30
31
32
33
34
#include "ssh.h"
#include "werror.h"
#include "xalloc.h"

#include <assert.h>
35
#include <string.h>
Niels Möller's avatar
Niels Möller committed
36

37
#define GABA_DEFINE
38
#include "channel.h.x"
39
#undef GABA_DEFINE
40

41
42
#include "channel.c.x"

43
44
struct exception *
make_channel_open_exception(UINT32 error_code, const char *msg)
Niels Möller's avatar
Niels Möller committed
45
46
{
  NEW(channel_open_exception, self);
Niels Möller's avatar
Niels Möller committed
47
48
49
50
51
52
53
54
55
56
57
58
59
60

#define MAX_ERROR 4
  static const char *msgs[MAX_ERROR + 1] = {
    "",
    "Administratively prohibited",
    "Connect failed",
    "Unknown channel type",
    "Resource shortage"
  };

  assert(error_code > 0);
  assert(error_code <= MAX_ERROR);
#undef MAX_ERROR
  
Niels Möller's avatar
Niels Möller committed
61
  self->super.type = EXC_CHANNEL_OPEN;
Niels Möller's avatar
Niels Möller committed
62
  self->super.msg = msg ? msg : msgs[error_code];
Niels Möller's avatar
Niels Möller committed
63
64
65
66
67
  self->error_code = error_code;

  return &self->super;
}

68
/* GABA:
69
70
   (class
     (name connection_service)
71
     (super command)
72
73
74
75
     (vars
       ; Supported global requests 
       (global_requests object alist)

76
       (channel_types object alist) ))
77
78
79
80

       ; Initialize connection (for instance, request channels to be 
       ; opened or services to be forwarded).

81
       ; (start object connection_startup)))
82
83
*/

84

Niels Möller's avatar
Niels Möller committed
85
86
87
88
89
struct lsh_string *format_global_failure(void)
{
  return ssh_format("%c", SSH_MSG_REQUEST_FAILURE);
}

90
91
92
93
94
struct lsh_string *format_global_success(void)
{
  return ssh_format("%c", SSH_MSG_REQUEST_SUCCESS);
}

95
96
struct lsh_string *format_open_confirmation(struct ssh_channel *channel,
					    UINT32 channel_number,
97
					    const char *format, ...)
98
99
100
101
102
103
{
  va_list args;
  UINT32 l1, l2;
  struct lsh_string *packet;

#define CONFIRM_FORMAT "%c%i%i%i%i"
104
105
#define CONFIRM_ARGS \
  SSH_MSG_CHANNEL_OPEN_CONFIRMATION, channel->channel_number, \
106
107
108
109
110
111
112
113
114
115
116
117
118
  channel_number, channel->rec_window_size, channel->rec_max_packet
    
  l1 = ssh_format_length(CONFIRM_FORMAT, CONFIRM_ARGS);

  va_start(args, format);
  l2 = ssh_vformat_length(format, args);
  va_end(args);

  packet = lsh_string_alloc(l1 + l2);

  ssh_format_write(CONFIRM_FORMAT, l1, packet->data, CONFIRM_ARGS);

  va_start(args, format);
119
  ssh_vformat_write(format, l2, packet->data+l1, args);
120
121
122
123
124
125
126
  va_end(args);

  return packet;
#undef CONFIRM_FORMAT
#undef CONFIRM_ARGS
}

Niels Möller's avatar
Niels Möller committed
127
struct lsh_string *format_open_failure(UINT32 channel, UINT32 reason,
128
				       const char *msg, const char *language)
Niels Möller's avatar
Niels Möller committed
129
130
131
132
133
{
  return ssh_format("%c%i%i%z%z", SSH_MSG_CHANNEL_OPEN_FAILURE,
		    channel, reason, msg, language);
}

134
135
136
137
138
struct lsh_string *format_channel_success(UINT32 channel)
{
  return ssh_format("%c%i", SSH_MSG_CHANNEL_SUCCESS, channel);
}

Niels Möller's avatar
Niels Möller committed
139
140
141
142
143
struct lsh_string *format_channel_failure(UINT32 channel)
{
  return ssh_format("%c%i", SSH_MSG_CHANNEL_FAILURE, channel);
}

144
145
146
147
148
149
150
151
152
153
struct lsh_string *prepare_window_adjust(struct ssh_channel *channel,
					 UINT32 add)
{
  channel->rec_window_size += add;
  
  return ssh_format("%c%i%i",
		    SSH_MSG_CHANNEL_WINDOW_ADJUST,
		    channel->channel_number, add);
}

Niels Möller's avatar
Niels Möller committed
154
155
156
157
158
159
/* GABA:
   (class
     (name exc_finish_channel_handler)
     (super exception_handler)
     (vars
       (table object channel_table)
160
161
       ; Non-zero if the channel has already been deallocated.
       (dead . int)
Niels Möller's avatar
Niels Möller committed
162
163
164
165
166
       ; Local channel number 
       (channel_number . UINT32)))
*/

static void do_exc_finish_channel_handler(struct exception_handler *s,
Niels Möller's avatar
Niels Möller committed
167
					  const struct exception *e)
Niels Möller's avatar
Niels Möller committed
168
169
170
171
172
{
  CAST(exc_finish_channel_handler, self, s);

  switch (e->type)
    {
Niels Möller's avatar
Niels Möller committed
173
    case EXC_FINISH_PENDING:
174
175
      if (self->dead)
	werror("channel.c: EXC_FINISH_PENDING on dead channel.\n");
Niels Möller's avatar
Niels Möller committed
176

177
178
      self->table->pending_close = 1;
      
Niels Möller's avatar
Niels Möller committed
179
180
181
182
      if (!self->table->next_channel)
	EXCEPTION_RAISE(self->super.parent, &finish_read_exception);
      break;
      
Niels Möller's avatar
Niels Möller committed
183
184
    case EXC_FINISH_CHANNEL:
      /* NOTE: This type of exception must be handled only once.
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
       * However, there is at least one case where it is difficult to
       * ensure that the exception is raised only once.
       *
       * For instance, in do_channel_close, the CHANNEL_EOF callback
       * can decide to call close_channel, which might raise this
       * exception. When control gets back to do_channel_close, and
       * CHANNEL_SENT_CLOSE is true, it raises the exception again.
       *
       * To get this right, we set a flag when the channel is
       * deallocated. */
      if (self->dead)
	debug("EXC_FINISH_CHANNEL on dead channel.\n");
      else
	{
	  struct ssh_channel *channel
	    = self->table->channels[self->channel_number];

	  assert(channel);
	  assert(channel->resources->super.alive);

	  if (channel->close)
	    CHANNEL_CLOSE(channel);
	
	  KILL_RESOURCE_LIST(channel->resources);
Niels Möller's avatar
Niels Möller committed
209
	
210
211
212
213
214
215
216
217
218
	  dealloc_channel(self->table, self->channel_number);
	  self->dead = 1;

	  if (self->table->pending_close && !self->table->next_channel)
	    {
	      /* FIXME: Send a SSH_DISCONNECT_BY_APPLICATION message? */
	      EXCEPTION_RAISE(self->super.parent, &finish_read_exception);
	    }
	}
Niels Möller's avatar
Niels Möller committed
219
220
221
222
223
224
225
226
227
      break;
    default:
      EXCEPTION_RAISE(self->super.parent, e);
    }
}

static struct exception_handler *
make_exc_finish_channel_handler(struct channel_table *table,
				UINT32 channel_number,
228
229
				struct exception_handler *e,
				const char *context)
Niels Möller's avatar
Niels Möller committed
230
231
232
233
{
  NEW(exc_finish_channel_handler, self);
  self->super.parent = e;
  self->super.raise = do_exc_finish_channel_handler;
234
  self->super.context = context;
Niels Möller's avatar
Niels Möller committed
235

236
237
  self->table = table;
  self->channel_number = channel_number;
238
  self->dead = 0;
239
  
Niels Möller's avatar
Niels Möller committed
240
  return &self->super;
Niels Möller's avatar
Niels Möller committed
241
242
243
}
				

244
/* Channel objects */
Niels Möller's avatar
Niels Möller committed
245
246
247
248
249

#define INITIAL_CHANNELS 32
/* Arbitrary limit */
#define MAX_CHANNELS (1L<<17)

Niels Möller's avatar
Niels Möller committed
250
struct channel_table *make_channel_table(void)
Niels Möller's avatar
Niels Möller committed
251
{
252
  NEW(channel_table, table);
Niels Möller's avatar
Niels Möller committed
253

254
  table->channels = lsh_space_alloc(sizeof(struct ssh_channel *)
255
				      * INITIAL_CHANNELS);
Niels Möller's avatar
Niels Möller committed
256
257
  table->in_use = lsh_space_alloc(INITIAL_CHANNELS);
  
258
259
260
261
  table->allocated_channels = INITIAL_CHANNELS;
  table->next_channel = 0;
  table->used_channels = 0;
  table->max_channels = MAX_CHANNELS;
Niels Möller's avatar
Niels Möller committed
262

Niels Möller's avatar
Niels Möller committed
263
  table->pending_close = 0;
264

265
266
267
  table->global_requests = make_alist(0, -1);
  table->channel_types = make_alist(0, -1);
  
268
269
270
  object_queue_init(&table->local_ports);
  object_queue_init(&table->remote_ports);
  
271
272
  object_queue_init(&table->active_global_requests);
  object_queue_init(&table->pending_global_requests);
Niels Möller's avatar
Niels Möller committed
273
  
274
  return table;
Niels Möller's avatar
Niels Möller committed
275
276
277
};

/* Returns -1 if allocation fails */
278
279
280
/* NOTE: This function returns locally chosen channel numbers, which
 * are always small integers. So there's no problem fitting them in
 * a signed int. */
281
int alloc_channel(struct channel_table *table)
Niels Möller's avatar
Niels Möller committed
282
{
283
  UINT32 i;
284
  
285
  for(i = table->next_channel; i < table->used_channels; i++)
Niels Möller's avatar
Niels Möller committed
286
    {
Niels Möller's avatar
Niels Möller committed
287
      if (!table->in_use[i])
Niels Möller's avatar
Niels Möller committed
288
	{
Niels Möller's avatar
Niels Möller committed
289
290
	  assert(!table->channels[i]);
	  table->in_use[i] = 1;
291
	  table->next_channel = i+1;
292

293
	  verbose("Allocated local channel number %i\n", i);
Niels Möller's avatar
Niels Möller committed
294
295
296
	  return i;
	}
    }
297
  if (i == table->max_channels)
Niels Möller's avatar
Niels Möller committed
298
    return -1;
299

300
  if (i == table->allocated_channels) 
Niels Möller's avatar
Niels Möller committed
301
    {
302
      int new_size = table->allocated_channels * 2;
Niels Möller's avatar
Niels Möller committed
303
      struct ssh_channel **new_channels;
Niels Möller's avatar
Niels Möller committed
304
      UINT8 *new_in_use;
Niels Möller's avatar
Niels Möller committed
305

Niels Möller's avatar
Niels Möller committed
306
307
308
      new_channels = lsh_space_alloc(sizeof(struct ssh_channel *)
				     * new_size);
      memcpy(new_channels, table->channels,
309
	     sizeof(struct ssh_channel *) * table->used_channels);
Niels Möller's avatar
Niels Möller committed
310
311
312
313
314
315
316
317
      lsh_space_free(table->channels);
      table->channels = new_channels;

      new_in_use = lsh_space_alloc(new_size);
      memcpy(new_in_use, table->in_use, table->used_channels);
      lsh_space_free(table->in_use);
      table->in_use = new_in_use;

318
      table->allocated_channels = new_size;
Niels Möller's avatar
Niels Möller committed
319
320
    }

321
  table->next_channel = table->used_channels = i+1;
Niels Möller's avatar
Niels Möller committed
322

323
  table->in_use[i] = 1;
324
  verbose("Allocated local channel number %i\n", i);
325

Niels Möller's avatar
Niels Möller committed
326
327
328
  return i;
}

329
void dealloc_channel(struct channel_table *table, int i)
Niels Möller's avatar
Niels Möller committed
330
331
{
  assert(i >= 0);
332
  assert( (unsigned) i < table->used_channels);
333

334
  verbose("Deallocating local channel %i\n", i);
335
  table->channels[i] = NULL;
Niels Möller's avatar
Niels Möller committed
336
337
  table->in_use[i] = 0;
  
338
  if ( (unsigned) i < table->next_channel)
339
340
341
    table->next_channel = i;
}

342
343
344
345
void
register_channel(struct ssh_connection *connection,
		 UINT32 local_channel_number,
		 struct ssh_channel *channel)
346
{
347
348
  struct channel_table *table = connection->table;
  
Niels Möller's avatar
Niels Möller committed
349
350
  assert(table->in_use[local_channel_number]);
  assert(!table->channels[local_channel_number]);
351

352
353
  verbose("Taking channel %i in use, (local %i).\n",
	  channel->channel_number, local_channel_number);
Niels Möller's avatar
Niels Möller committed
354
355
356
357
358
  table->channels[local_channel_number] = channel;

  /* FIXME: Is this the right place to install this exception handler? */
  channel->e = make_exc_finish_channel_handler(table,
					       local_channel_number,
359
360
					       connection->e,
					       HANDLER_CONTEXT);
361
362

  REMEMBER_RESOURCE(connection->resources, &channel->resources->super);
Niels Möller's avatar
Niels Möller committed
363
364
}

365
struct ssh_channel *lookup_channel(struct channel_table *table, UINT32 i)
Niels Möller's avatar
Niels Möller committed
366
{
367
368
  return (i < table->used_channels)
    ? table->channels[i] : NULL;
Niels Möller's avatar
Niels Möller committed
369
370
}

371
372
373
374
375
376
377
378
379
380
/* FIXME: It seems suboptimal to send a window adjust message for *every* write that we do.
 * A better scheme might be as follows:
 *
 * Delay window adjust messages, keeping track of both the locally
 * maintained window size, which is updated after each write, and the
 * size that has been reported to the remote end. When the difference
 * between these two values gets large enough (say, larger than one
 * half or one third of the maximum window size), we send a
 * window_adjust message to sync them. */
static void adjust_rec_window(struct flow_controlled *f, UINT32 written)
381
{
382
383
  CAST_SUBTYPE(ssh_channel, channel, f);

Niels Möller's avatar
Niels Möller committed
384
  A_WRITE(channel->write,
385
	  prepare_window_adjust(channel, written));
386
387
}

Niels Möller's avatar
Niels Möller committed
388
void channel_start_receive(struct ssh_channel *channel)
389
{
Niels Möller's avatar
Niels Möller committed
390
391
  A_WRITE(channel->write,
	  prepare_window_adjust
392
	  (channel, channel->max_window - channel->rec_window_size));
393
394
}

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

396
397
398
/* Ugly macros to make it a little simpler to free the input packet at
 * the right time. */

Niels Möller's avatar
Niels Möller committed
399
400
401
402
403
#define RETURN goto foo_finish
#define END(s) do { foo_finish: \
                    lsh_string_free((s)); \
                    return; } while(0)

404

Niels Möller's avatar
Niels Möller committed
405
/* Channel related messages */
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424

/* GABA:
   (class
     (name global_request_status)
     (vars
       ; -1 for still active requests,
       ; 0 for failure,
       ; 1 for success
       (status . int)))
*/

static struct global_request_status *make_global_request_status(void)
{
  NEW(global_request_status, self);
  self->status = -1;

  return self;
}

Niels Möller's avatar
Niels Möller committed
425
/* FIXME: Split into a continuation and an exception handler */
426
427
428
429
430
431
432
433
/* GABA:
   (class
     (name global_request_response)
     (super global_request_callback)
     (vars
       (active object global_request_status)))
*/

Niels Möller's avatar
Niels Möller committed
434
static void
435
436
437
438
do_global_request_response(struct global_request_callback *c,
			   int success)
{
  CAST(global_request_response, self, c);
Niels Möller's avatar
Niels Möller committed
439
  struct object_queue *q = &self->super.connection->table->active_global_requests;
440
441
442
443
444
445
446
447
448
449
450

  assert( self->active->status == -1);
  assert( (success == 0) || (success == 1) );
  assert( !object_queue_is_empty(q));
	  
  self->active->status = success;

  for (;;)
    {
      CAST(global_request_status, n, object_queue_peek_head(q));
      if (!n || (n->status < 0))
Niels Möller's avatar
Niels Möller committed
451
	break;
452
453

      object_queue_remove_head(q);
Niels Möller's avatar
Niels Möller committed
454
455
456

      /* FIXME: Perhaps install some exception handler that cancels
       * the queue as soon as a write failes. */
457
      C_WRITE(self->super.connection,
Niels Möller's avatar
Niels Möller committed
458
459
	      (n->status
	       ? format_global_success()
460
	       : format_global_failure()));
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
    }
}

static struct global_request_callback *
make_global_request_response(struct ssh_connection *connection,
			     struct global_request_status *active)
{
  NEW(global_request_response, self);

  self->super.connection = connection;
  self->super.response = do_global_request_response;

  self->active = active;

  return &self->super;
}
     
Niels Möller's avatar
Niels Möller committed
478
479
480
static void do_global_request(struct packet_handler *s UNUSED,
			      struct ssh_connection *connection,
			      struct lsh_string *packet)
Niels Möller's avatar
Niels Möller committed
481
{
482
  /* CAST(global_request_handler, closure, c); */
Niels Möller's avatar
Niels Möller committed
483
484

  struct simple_buffer buffer;
485
  unsigned msg_number;
Niels Möller's avatar
Niels Möller committed
486
487
488
489
490
491
492
493
494
495
496
  int name;
  int want_reply;
  
  simple_buffer_init(&buffer, packet->length, packet->data);

  if (parse_uint8(&buffer, &msg_number)
      && (msg_number == SSH_MSG_GLOBAL_REQUEST)
      && parse_atom(&buffer, &name)
      && parse_boolean(&buffer, &want_reply))
    {
      struct global_request *req;
497
498
      struct global_request_callback *c = NULL;
      
Niels Möller's avatar
Niels Möller committed
499
      if (!name || !(req = ALIST_GET(connection->table->global_requests,
500
				     name)))
501
	{
Niels Möller's avatar
Niels Möller committed
502
	  lsh_string_free(packet);
503

Niels Möller's avatar
Niels Möller committed
504
505
	  C_WRITE(connection, format_global_failure());
	  return;
Niels Möller's avatar
Niels Möller committed
506
507
508
509
510
511
512
	}
      else
	{
	  if (want_reply)
	    {
	      struct global_request_status *a = make_global_request_status();
	      
Niels Möller's avatar
Niels Möller committed
513
	      object_queue_add_tail(&connection->table->active_global_requests,
Niels Möller's avatar
Niels Möller committed
514
515
516
517
518
				    &a->super);
	      
	      c = make_global_request_response(connection, a);
	    }
	  GLOBAL_REQUEST(req, connection, &buffer, c);
519
	}
Niels Möller's avatar
Niels Möller committed
520
    }
Niels Möller's avatar
Niels Möller committed
521
522
  else
    {
523
      PROTOCOL_ERROR(connection->e, "Invalid SSH_MSG_GLOBAL_REQUEST message.");
Niels Möller's avatar
Niels Möller committed
524
525
    }
  lsh_string_free(packet);
Niels Möller's avatar
Niels Möller committed
526
527
}

Niels Möller's avatar
Niels Möller committed
528
529
530
531
static void
do_global_request_success(struct packet_handler *s UNUSED,
			  struct ssh_connection *connection,
			  struct lsh_string *packet)
532
533
{
  if (packet->length != 1)
Niels Möller's avatar
Niels Möller committed
534
    {
535
      PROTOCOL_ERROR(connection->e, "Invalid GLOBAL_REQUEST_SUCCESS message.");
Niels Möller's avatar
Niels Möller committed
536
537
      RETURN;
    }
538
539
540

  assert(packet->data[0] == SSH_MSG_REQUEST_SUCCESS);

Niels Möller's avatar
Niels Möller committed
541
  if (object_queue_is_empty(&connection->table->pending_global_requests))
542
543
    {
      werror("do_global_request_success: Unexpected message, ignoring.\n");
Niels Möller's avatar
Niels Möller committed
544
      return;
545
546
    }
  {
547
    CAST_SUBTYPE(command_context, ctx,
Niels Möller's avatar
Niels Möller committed
548
549
		 object_queue_remove_head(&connection->table->pending_global_requests));
    COMMAND_RETURN(ctx->c, connection);
550
  }
Niels Möller's avatar
Niels Möller committed
551
  END(packet);
552
553
}

554
555
556
struct exception global_request_exception =
STATIC_EXCEPTION(EXC_GLOBAL_REQUEST, "Global request failed");

Niels Möller's avatar
Niels Möller committed
557
558
559
560
static void
do_global_request_failure(struct packet_handler *s UNUSED,
			  struct ssh_connection *connection,
			  struct lsh_string *packet)
561
562
{
  if (packet->length != 1)
Niels Möller's avatar
Niels Möller committed
563
    {
564
      PROTOCOL_ERROR(connection->e, "Invalid GLOBAL_REQUEST_FAILURE message.");
Niels Möller's avatar
Niels Möller committed
565
566
      RETURN;
    }
567
568
569

  assert(packet->data[0] == SSH_MSG_REQUEST_FAILURE);

Niels Möller's avatar
Niels Möller committed
570
  if (object_queue_is_empty(&connection->table->pending_global_requests))
571
572
573
    {
      werror("do_global_request_failure: Unexpected message, ignoring.\n");
    }
Niels Möller's avatar
Niels Möller committed
574
575
576
577
578
579
580
  else
    {
      CAST_SUBTYPE(command_context, ctx,
		   object_queue_remove_head(&connection->table->pending_global_requests));
      EXCEPTION_RAISE(ctx->e, &global_request_exception);
    }
  END(packet);
581
582
}

583

Niels Möller's avatar
Niels Möller committed
584
/* GABA:
585
586
587
588
589
   (class
     (name channel_open_continuation)
     (super command_continuation)
     (vars
       (connection object ssh_connection)
Niels Möller's avatar
Niels Möller committed
590
591
592
593
       (local_channel_number . UINT32)
       (remote_channel_number . UINT32)
       (window_size . UINT32)
       (max_packet . UINT32)))
594
*/
595

Niels Möller's avatar
Niels Möller committed
596
597
598
static void
do_channel_open_continue(struct command_continuation *c,
			 struct lsh_object *value)
599
600
{
  CAST(channel_open_continuation, self, c);
Niels Möller's avatar
Niels Möller committed
601
602
603
604
605
606
607
608
609
610
611
612
  CAST_SUBTYPE(ssh_channel, channel, value);

  assert(channel);

  /* FIXME: This copying could just as well be done by the
   * CHANNEL_OPEN handler? Then we can remove the corresponding fields
   * from the closure as well. */
  channel->send_window_size = self->window_size;
  channel->send_max_packet = self->max_packet;
  channel->channel_number = self->remote_channel_number;

  /* FIXME: Is the channel->write field really needed? */
Niels Möller's avatar
Niels Möller committed
613
  channel->write = self->connection->write;
Niels Möller's avatar
Niels Möller committed
614

615
  register_channel(self->connection,
Niels Möller's avatar
Niels Möller committed
616
		   self->local_channel_number, channel);
Niels Möller's avatar
Niels Möller committed
617
618
619
620
621

  /* FIXME: Doesn't support sending extra arguments with the
   * confirmation message. */

  C_WRITE(self->connection,
Niels Möller's avatar
Niels Möller committed
622
	  format_open_confirmation(channel, self->local_channel_number, ""));
623
}
Niels Möller's avatar
Niels Möller committed
624

Niels Möller's avatar
Niels Möller committed
625
static struct command_continuation *
Niels Möller's avatar
Niels Möller committed
626
627
628
629
630
631
632
make_channel_open_continuation(struct ssh_connection *connection,
			       UINT32 local_channel_number,
			       UINT32 remote_channel_number,
			       UINT32 window_size,
			       UINT32 max_packet)
{
  NEW(channel_open_continuation, self);
Niels Möller's avatar
Niels Möller committed
633

Niels Möller's avatar
Niels Möller committed
634
635
636
637
638
639
640
  self->super.c = do_channel_open_continue;
  self->connection = connection;
  self->local_channel_number = local_channel_number;
  self->remote_channel_number = remote_channel_number;
  self->window_size = window_size;
  self->max_packet = max_packet;

Niels Möller's avatar
Niels Möller committed
641
  return &self->super;
Niels Möller's avatar
Niels Möller committed
642
643
644
645
646
647
648
649
650
}
			       
/* GABA:
   (class
     (name exc_channel_open_handler)
     (super exception_handler)
     (vars
       (connection object ssh_connection)
       (local_channel_number . UINT32)
Niels Möller's avatar
Niels Möller committed
651
       (remote_channel_number . UINT32)))
Niels Möller's avatar
Niels Möller committed
652
653
654
*/

static void do_exc_channel_open_handler(struct exception_handler *s,
Niels Möller's avatar
Niels Möller committed
655
					const struct exception *e)
Niels Möller's avatar
Niels Möller committed
656
657
658
659
660
661
662
663
{
  CAST(exc_channel_open_handler, self, s);

  switch (e->type)
    {
    case EXC_CHANNEL_OPEN:
      {
	CAST_SUBTYPE(channel_open_exception, exc, e);
Niels Möller's avatar
Niels Möller committed
664
	struct channel_table *table = self->connection->table;
Niels Möller's avatar
Niels Möller committed
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
	
	assert(table->in_use[self->local_channel_number]);
	assert(!table->channels[self->local_channel_number]);

	dealloc_channel(table, self->local_channel_number);
	
        C_WRITE(self->connection,
		format_open_failure(self->remote_channel_number,
				    exc->error_code, e->msg, ""));
	break;
      }
    default:
      EXCEPTION_RAISE(self->super.parent, e);
    }      
}

static struct exception_handler *
make_exc_channel_open_handler(struct ssh_connection *connection,
			      UINT32 local_channel_number,
			      UINT32 remote_channel_number,
685
686
			      struct exception_handler *parent,
			      const char *context)
Niels Möller's avatar
Niels Möller committed
687
{
Niels Möller's avatar
Niels Möller committed
688
  NEW(exc_channel_open_handler, self);
Niels Möller's avatar
Niels Möller committed
689
  self->super.parent = parent;
Niels Möller's avatar
Niels Möller committed
690
  self->super.raise = do_exc_channel_open_handler;
691
692
  self->super.context = context;
  
Niels Möller's avatar
Niels Möller committed
693
  self->connection = connection;
Niels Möller's avatar
Niels Möller committed
694
695
696
697
698
699
700
701
702
  self->local_channel_number = local_channel_number;
  self->remote_channel_number = remote_channel_number;

  return &self->super;
}

static void do_channel_open(struct packet_handler *c UNUSED,
			    struct ssh_connection *connection,
			    struct lsh_string *packet)
Niels Möller's avatar
Niels Möller committed
703
{
704
  /* CAST(channel_open_handler, closure, c); */
Niels Möller's avatar
Niels Möller committed
705
706

  struct simple_buffer buffer;
707
  unsigned msg_number;
Niels Möller's avatar
Niels Möller committed
708
  int type;
709
710
711
  UINT32 remote_channel_number;
  UINT32 window_size;
  UINT32 max_packet;
Niels Möller's avatar
Niels Möller committed
712
713
714
715
716
717
  
  simple_buffer_init(&buffer, packet->length, packet->data);

  if (parse_uint8(&buffer, &msg_number)
      && (msg_number == SSH_MSG_CHANNEL_OPEN)
      && parse_atom(&buffer, &type)
718
719
720
      && parse_uint32(&buffer, &remote_channel_number)
      && parse_uint32(&buffer, &window_size)
      && parse_uint32(&buffer, &max_packet))
Niels Möller's avatar
Niels Möller committed
721
722
    {
      struct channel_open *open;
Niels Möller's avatar
Niels Möller committed
723

Niels Möller's avatar
Niels Möller committed
724
725
726
727
      /* NOTE: We can't free the packet yet, as the buffer is passed
       * to the CHANNEL_OPEN method later. */

      if (connection->table->pending_close)
Niels Möller's avatar
Niels Möller committed
728
729
730
731
732
733
734
735
	{
	  /* We are waiting for channels to close. Don't open any new ones. */

	  C_WRITE(connection,
		  format_open_failure(remote_channel_number,
				      SSH_OPEN_ADMINISTRATIVELY_PROHIBITED,
				      "Waiting for channels to close.", ""));
	}
Niels Möller's avatar
Niels Möller committed
736
      else if (!type || !(open = ALIST_GET(connection->table->channel_types,
737
				      type)))
Niels Möller's avatar
Niels Möller committed
738
739
740
741
742
743
744
745
	{
	  C_WRITE(connection,
		  format_open_failure(remote_channel_number,
				      SSH_OPEN_UNKNOWN_CHANNEL_TYPE,
				      "Unknown channel type", ""));
	}
      else
	{
Niels Möller's avatar
Niels Möller committed
746
      	  int local_number = alloc_channel(connection->table);
Niels Möller's avatar
Niels Möller committed
747
748
749
750
751
752
753
754

	  if (local_number < 0)
	    C_WRITE(connection,
		    format_open_failure(remote_channel_number,
					SSH_OPEN_RESOURCE_SHORTAGE,
					"Unknown channel type", ""));

	  
Niels Möller's avatar
Niels Möller committed
755
	  
756
	  CHANNEL_OPEN(open, connection, type, &buffer,
Niels Möller's avatar
Niels Möller committed
757
758
759
760
761
762
763
764
		       make_channel_open_continuation(connection,
						      local_number,
						      remote_channel_number,
						      window_size,
						      max_packet),
		       make_exc_channel_open_handler(connection,
						     local_number,
						     remote_channel_number,
765
766
						     connection->e,
						     HANDLER_CONTEXT));
Niels Möller's avatar
Niels Möller committed
767
	}
Niels Möller's avatar
Niels Möller committed
768
    }
Niels Möller's avatar
Niels Möller committed
769
  else
770
    PROTOCOL_ERROR(connection->e, "Invalid SSH_MSG_CHANNEL_OPEN message.");
Niels Möller's avatar
Niels Möller committed
771

Niels Möller's avatar
Niels Möller committed
772
  lsh_string_free(packet);
Niels Möller's avatar
Niels Möller committed
773
774
}     

Niels Möller's avatar
Niels Möller committed
775
776
777
778
static void
do_channel_request(struct packet_handler *closure UNUSED,
		   struct ssh_connection *connection,
		   struct lsh_string *packet)
Niels Möller's avatar
Niels Möller committed
779
780
{
  struct simple_buffer buffer;
781
  unsigned msg_number;
Niels Möller's avatar
Niels Möller committed
782
783
784
  UINT32 channel_number;
  int type;
  int want_reply;
785
  
Niels Möller's avatar
Niels Möller committed
786
787
788
789
790
791
792
793
  simple_buffer_init(&buffer, packet->length, packet->data);

  if (parse_uint8(&buffer, &msg_number)
      && (msg_number == SSH_MSG_CHANNEL_REQUEST)
      && parse_uint32(&buffer, &channel_number)
      && parse_atom(&buffer, &type)
      && parse_boolean(&buffer, &want_reply))
    {
Niels Möller's avatar
Niels Möller committed
794
      struct ssh_channel *channel = lookup_channel(connection->table,
Niels Möller's avatar
Niels Möller committed
795
796
						   channel_number);

797
798
799
800
      /* NOTE: We can't free packet yet, because it is not yet fully
       * parsed. There may be some more arguments, which are parsed by
       * the CHANNEL_REQUEST method below. */

Niels Möller's avatar
Niels Möller committed
801
802
803
804
      if (channel)
	{
	  struct channel_request *req;

805
806
	  if (type && channel->request_types 
	      && ( (req = ALIST_GET(channel->request_types, type)) ))
Niels Möller's avatar
Niels Möller committed
807
	    CHANNEL_REQUEST(req, channel, connection, want_reply, &buffer);
808
	  else
Niels Möller's avatar
Niels Möller committed
809
810
	    {
	      if (want_reply)
Niels Möller's avatar
Niels Möller committed
811
		C_WRITE(connection,
Niels Möller's avatar
Niels Möller committed
812
813
814
815
816
817
818
			format_channel_failure(channel->channel_number));
	    }
	}
      else
	{
	  werror("SSH_MSG_CHANNEL_REQUEST on nonexistant channel %i\n",
		 channel_number);
Niels Möller's avatar
Niels Möller committed
819
	}
820
    }
Niels Möller's avatar
Niels Möller committed
821
  else
822
    PROTOCOL_ERROR(connection->e, "Invalid SSH_MSG_CHANNEL_REQUEST message.");
Niels Möller's avatar
Niels Möller committed
823
  
Niels Möller's avatar
Niels Möller committed
824
  lsh_string_free(packet);
Niels Möller's avatar
Niels Möller committed
825
826
}
      
Niels Möller's avatar
Niels Möller committed
827
828
829
830
static void
do_window_adjust(struct packet_handler *closure UNUSED,
		 struct ssh_connection *connection,
		 struct lsh_string *packet)
Niels Möller's avatar
Niels Möller committed
831
832
{
  struct simple_buffer buffer;
833
  unsigned msg_number;
Niels Möller's avatar
Niels Möller committed
834
835
836
837
838
839
840
  UINT32 channel_number;
  UINT32 size;

  simple_buffer_init(&buffer, packet->length, packet->data);

  if (parse_uint8(&buffer, &msg_number)
      && (msg_number == SSH_MSG_CHANNEL_WINDOW_ADJUST)
841
      && parse_uint32(&buffer, &channel_number)
Niels Möller's avatar
Niels Möller committed
842
843
844
      && parse_uint32(&buffer, &size)
      && parse_eod(&buffer))
    {
Niels Möller's avatar
Niels Möller committed
845
      struct ssh_channel *channel = lookup_channel(connection->table,
Niels Möller's avatar
Niels Möller committed
846
847
848
849
						   channel_number);

      lsh_string_free(packet);
      
Niels Möller's avatar
Niels Möller committed
850
      if (channel
851
852
	  && !(channel->flags & (CHANNEL_RECEIVED_EOF
				 | CHANNEL_RECEIVED_CLOSE)))
Niels Möller's avatar
Niels Möller committed
853
	{
854
	  if (! (channel->flags & (CHANNEL_SENT_CLOSE | CHANNEL_SENT_EOF)))
Niels Möller's avatar
Niels Möller committed
855
856
857
	    {
	      channel->send_window_size += size;
	      if (channel->send_window_size && channel->send)
858
		CHANNEL_SEND(channel, connection);
Niels Möller's avatar
Niels Möller committed
859
	    }
Niels Möller's avatar
Niels Möller committed
860
	}
Niels Möller's avatar
Niels Möller committed
861
862
863
864
865
866
      else
	{
	  /* FIXME: What to do now? Should unknown channel numbers be
	   * ignored silently? */
	  werror("SSH_MSG_CHANNEL_WINDOW_ADJUST on nonexistant or closed "
		 "channel %i\n", channel_number);
867
	  PROTOCOL_ERROR(connection->e, "Unexpected CHANNEL_WINDOW_ADJUST");
Niels Möller's avatar
Niels Möller committed
868
869
870
871
872
	}
    }
  else
    {
      lsh_string_free(packet);
873
      PROTOCOL_ERROR(connection->e, "Invalid CHANNEL_WINDOW_ADJUST message.");
Niels Möller's avatar
Niels Möller committed
874
875
876
    }
}

Niels Möller's avatar
Niels Möller committed
877
878
879
880
static void
do_channel_data(struct packet_handler *closure UNUSED,
		struct ssh_connection *connection,
		struct lsh_string *packet)
Niels Möller's avatar
Niels Möller committed
881
882
{
  struct simple_buffer buffer;
883
  unsigned msg_number;
Niels Möller's avatar
Niels Möller committed
884
885
886
887
888
889
890
  UINT32 channel_number;
  struct lsh_string *data;
  
  simple_buffer_init(&buffer, packet->length, packet->data);

  if (parse_uint8(&buffer, &msg_number)
      && (msg_number == SSH_MSG_CHANNEL_DATA)
891
      && parse_uint32(&buffer, &channel_number)
Niels Möller's avatar
Niels Möller committed
892
893
894
      && ( (data = parse_string_copy(&buffer)) )
      && parse_eod(&buffer))
    {
Niels Möller's avatar
Niels Möller committed
895
      struct ssh_channel *channel = lookup_channel(connection->table,
Niels Möller's avatar
Niels Möller committed
896
897
898
899
						   channel_number);

      lsh_string_free(packet);
      
900
      if (channel && channel->receive
901
902
	  && !(channel->flags & (CHANNEL_RECEIVED_EOF
				 | CHANNEL_RECEIVED_CLOSE)))
Niels Möller's avatar
Niels Möller committed
903
	{
Niels Möller's avatar
Niels Möller committed
904
	  if (channel->flags & CHANNEL_SENT_CLOSE)
Niels Möller's avatar
Niels Möller committed
905
	    {
Niels Möller's avatar
Niels Möller committed
906
	      lsh_string_free(data);
907
	      werror("Ignoring data on channel which is closing\n");
Niels Möller's avatar
Niels Möller committed
908
	      return;
Niels Möller's avatar
Niels Möller committed
909
	    }
Niels Möller's avatar
Niels Möller committed
910
911
912
913
914
	  else
	    {
	      if (data->length > channel->rec_window_size)
		{
		  /* Truncate data to fit window */
915
		  werror("Channel data overflow. Extra data ignored.\n"); 
Niels Möller's avatar
Niels Möller committed
916
917
		  data->length = channel->rec_window_size;
		}
918
919

	      if (!data->length)
Niels Möller's avatar
Niels Möller committed
920
921
922
923
924
		{
		  /* Ignore data packet */
		  lsh_string_free(data);
		  return;
		}
925
926
	      channel->rec_window_size -= data->length;

Niels Möller's avatar
Niels Möller committed
927
	      CHANNEL_RECEIVE(channel, CHANNEL_DATA, data);
Niels Möller's avatar
Niels Möller committed
928
	    }
Niels Möller's avatar
Niels Möller committed
929
	}
Niels Möller's avatar
Niels Möller committed
930
931
932
933
934
935
936
937
938
939
      else
	{
	  werror("Data on closed or non-existant channel %i\n",
		 channel_number);
	  lsh_string_free(data);
	}
    }
  else
    {
      lsh_string_free(packet);
940
      PROTOCOL_ERROR(connection->e, "Invalid CHANNEL_DATA message.");
Niels Möller's avatar
Niels Möller committed
941
942
943
    }
}

Niels Möller's avatar
Niels Möller committed
944
945
946
947
static void
do_channel_extended_data(struct packet_handler *closure UNUSED,
			 struct ssh_connection *connection,
			 struct lsh_string *packet)
Niels Möller's avatar
Niels Möller committed
948
949
{
  struct simple_buffer buffer;
950
  unsigned msg_number;
Niels Möller's avatar
Niels Möller committed
951
952
953
954
955
956
957
958
  UINT32 channel_number;
  UINT32 type;
  struct lsh_string *data;
  
  simple_buffer_init(&buffer, packet->length, packet->data);

  if (parse_uint8(&buffer, &msg_number)
      && (msg_number == SSH_MSG_CHANNEL_EXTENDED_DATA)
959
      && parse_uint32(&buffer, &channel_number)
Niels Möller's avatar
Niels Möller committed
960
961
962
963
      && parse_uint32(&buffer, &type)
      && ( (data = parse_string_copy(&buffer)) )
      && parse_eod(&buffer))
    {
Niels Möller's avatar
Niels Möller committed
964
      struct ssh_channel *channel = lookup_channel(connection->table,
Niels Möller's avatar
Niels Möller committed
965
966
967
968
						   channel_number);

      lsh_string_free(packet);
      
969
      if (channel && channel->receive
970
971
	  && !(channel->flags & (CHANNEL_RECEIVED_EOF
				 | CHANNEL_RECEIVED_CLOSE)))
Niels Möller's avatar
Niels Möller committed
972
	{
Niels Möller's avatar
Niels Möller committed
973
	  if (channel->flags & CHANNEL_SENT_CLOSE)
Niels Möller's avatar
Niels Möller committed
974
	    {
Niels Möller's avatar
Niels Möller committed
975
	      lsh_string_free(data);
976
	      werror("Ignoring extended data on channel which is closing\n");
Niels Möller's avatar
Niels Möller committed
977
	      return;
Niels Möller's avatar
Niels Möller committed
978
	    }
Niels Möller's avatar
Niels Möller committed
979
	  else
Niels Möller's avatar
Niels Möller committed
980
	    {
Niels Möller's avatar
Niels Möller committed
981
982
983
	      if (data->length > channel->rec_window_size)
		{
		  /* Truncate data to fit window */
984
		  werror("Channel extended data overflow. "
Niels Möller's avatar
Niels Möller committed
985
986
987
988
			 "Extra data ignored.\n");
		  data->length = channel->rec_window_size;
		}
	      
Niels Möller's avatar
Niels Möller committed
989
990
991
992
993
994
	      if (!data->length)
		{
		  /* Ignore data packet */
		  lsh_string_free(data);
		  return;
		}
995

Niels Möller's avatar
Niels Möller committed
996
	      channel->rec_window_size -= data->length;
997

Niels Möller's avatar
Niels Möller committed
998
999
1000
	      switch(type)
		{
		case SSH_EXTENDED_DATA_STDERR:
Niels Möller's avatar
Niels Möller committed
1001
1002
		  CHANNEL_RECEIVE(channel, CHANNEL_STDERR_DATA, data);
		  break;
Niels Möller's avatar
Niels Möller committed
1003
		default:
1004
		  werror("Unknown type %i of extended data.\n",
Niels Möller's avatar
Niels Möller committed
1005
1006
1007
			 type);
		  lsh_string_free(data);
		}
Niels Möller's avatar
Niels Möller committed
1008
1009
	    }
	}
Niels Möller's avatar
Niels Möller committed
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
      else
	{
	  werror("Extended data on closed or non-existant channel %i\n",
		 channel_number);
	  lsh_string_free(data);
	}
    }
  else
    {
      lsh_string_free(packet);
1020
      PROTOCOL_ERROR(connection->e, "Invalid CHANNEL_EXTENDED_DATA message.");
Niels Möller's avatar
Niels Möller committed
1021
1022
1023
    }
}

Niels Möller's avatar
Niels Möller committed
1024
1025
1026
1027
static void
do_channel_eof(struct packet_handler *closure UNUSED,
	       struct ssh_connection *connection,
	       struct lsh_string *packet)
Niels Möller's avatar
Niels Möller committed
1028
1029
{
  struct simple_buffer buffer;
1030
  unsigned msg_number;
Niels Möller's avatar
Niels Möller committed
1031
1032
1033
1034
1035
1036
  UINT32 channel_number;
  
  simple_buffer_init(&buffer, packet->length, packet->data);

  if (parse_uint8(&buffer, &msg_number)
      && (msg_number == SSH_MSG_CHANNEL_EOF)
1037
      && parse_uint32(&buffer, &channel_number)
Niels Möller's avatar
Niels Möller committed
1038
1039
      && parse_eod(&buffer))
    {
Niels Möller's avatar
Niels Möller committed
1040
      struct ssh_channel *channel = lookup_channel(connection->table,
Niels Möller's avatar
Niels Möller committed
1041
1042
1043
1044
						   channel_number);

      lsh_string_free(packet);

Niels Möller's avatar
Niels Möller committed
1045
1046
      if (channel)
	{
1047
	  if (channel->flags & (CHANNEL_RECEIVED_EOF | CHANNEL_RECEIVED_CLOSE))
Niels Möller's avatar
Niels Möller committed
1048
	    {
1049
	      werror("Receiving EOF on channel on closed channel.\n");
1050
1051
	      PROTOCOL_ERROR(connection->e,
			     "Received EOF on channel on closed channel.");
Niels Möller's avatar
Niels Möller committed
1052
	    }
1053
	  else
Niels Möller's avatar
Niels Möller committed
1054
	    {
1055
1056
	      verbose("Receiving EOF on channel %i (local %i)\n",
		      channel->channel_number, channel_number);
1057
	      
Niels Möller's avatar
Niels Möller committed
1058
	      channel->flags |= CHANNEL_RECEIVED_EOF;
1059
	      
Niels Möller's avatar
Niels Möller committed
1060
1061
1062
1063
1064
	      if (channel->eof)
		CHANNEL_EOF(channel);
	      else
		/* FIXME: What is a reasonable default behaviour?
		 * Closing the channel may be the right thing to do. */
1065
		channel_close(channel);
Niels Möller's avatar
Niels Möller committed
1066
1067
	    }
	}
Niels Möller's avatar
Niels Möller committed
1068
1069
1070
1071
      else
	{
	  werror("EOF on non-existant channel %i\n",
		 channel_number);
1072
	  PROTOCOL_ERROR(connection->e, "EOF on non-existant channel");
Niels Möller's avatar
Niels Möller committed
1073
1074
1075
1076
1077
	}
    }
  else
    {
      lsh_string_free(packet);
1078
      PROTOCOL_ERROR(connection->e, "Invalid CHANNEL_EOF message");
Niels Möller's avatar
Niels Möller committed
1079
1080
1081
    }
}

Niels Möller's avatar
Niels Möller committed
1082
1083
1084
1085
static void
do_channel_close(struct packet_handler *closure UNUSED,
		 struct ssh_connection *connection,
		 struct lsh_string *packet)
Niels Möller's avatar
Niels Möller committed
1086
1087
{
  struct simple_buffer buffer;
1088
  unsigned msg_number;
Niels Möller's avatar
Niels Möller committed
1089
1090
1091
1092
1093
1094
  UINT32 channel_number;
  
  simple_buffer_init(&buffer, packet->length, packet->data);

  if (parse_uint8(&buffer, &msg_number)
      && (msg_number == SSH_MSG_CHANNEL_CLOSE)
1095
      && parse_uint32(&buffer, &channel_number)
Niels Möller's avatar
Niels Möller committed
1096
1097
      && parse_eod(&buffer))
    {
Niels Möller's avatar
Niels Möller committed
1098
      struct ssh_channel *channel = lookup_channel(connection->table,
Niels Möller's avatar
Niels Möller committed
1099
1100
1101
1102
						   channel_number);

      lsh_string_free(packet);
      
Niels Möller's avatar
Niels Möller committed
1103
1104
      if (channel)
	{
1105
1106
	  verbose("Receiving CLOSE on channel %i (local %i)\n",
		  channel->channel_number, channel_number);
1107
	      
1108
	  if (channel->flags & CHANNEL_RECEIVED_CLOSE)
Niels Möller's avatar
Niels Möller committed
1109
	    {
1110
	      werror("Receiving multiple CLOSE on channel.\n");
1111
	      PROTOCOL_ERROR(connection->e, "Receiving multiple CLOSE on channel.");
Niels Möller's avatar
Niels Möller committed
1112
	    }
Niels Möller's avatar
Niels Möller committed
1113
	  else
Niels Möller's avatar
Niels Möller committed
1114
	    {
Niels Möller's avatar
Niels Möller committed
1115
	      channel->flags |= CHANNEL_RECEIVED_CLOSE;
Niels Möller's avatar
Niels Möller committed
1116
	  
Niels Möller's avatar
Niels Möller committed
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
	      if (! (channel->flags & (CHANNEL_RECEIVED_EOF | CHANNEL_SENT_EOF
				       | CHANNEL_SENT_CLOSE)))
		{
		  werror("Unexpected channel CLOSE.\n");
		}

	      if (! (channel->flags & (CHANNEL_RECEIVED_EOF))
		  && channel->eof)
		CHANNEL_EOF(channel);

1127
	      if (channel->flags & CHANNEL_SENT_CLOSE)
Niels Möller's avatar
Niels Möller committed
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
		{
		  static const struct exception finish_exception
		    = STATIC_EXCEPTION(EXC_FINISH_CHANNEL, "Received CLOSE message.");
	      
		  EXCEPTION_RAISE(channel->e,
				  &finish_exception);
		}
	      else
		channel_close(channel);
	    }
	}
      else
	{
	  werror("CLOSE on non-existant channel %i\n",
		 channel_number);
1143
	  PROTOCOL_ERROR(connection->e, "CLOSE on non-existant channel");
Niels Möller's avatar
Niels Möller committed
1144
	}
Niels Möller's avatar
Niels Möller committed
1145
    }
Niels Möller's avatar
Niels Möller committed
1146
1147
1148
  else
    {
      lsh_string_free(packet);
1149
      PROTOCOL_ERROR(connection->e, "Invalid CHANNEL_CLOSE message");