aboutsummaryrefslogtreecommitdiff
path: root/libssh/packet.c
blob: 330b6908791e31b6874883422e231fee394bac38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
/* packet.c */	
/* packet building functions */
/*
Copyright 2003 Aris Adamantiadis

This file is part of the SSH Library

The SSH Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or (at your
option) any later version.

The SSH Library 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 Lesser General Public
License for more details.

You should have received a copy of the GNU Lesser General Public License
along with the SSH Library; see the file COPYING.  If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA. */

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include "libssh/priv.h"
#include "libssh/ssh2.h"
#include "libssh/ssh1.h"
#include <netdb.h>
#include <errno.h>
#include "libssh/crypto.h"

/* XXX include selected mac size */
static int macsize=SHA_DIGEST_LEN;

/* completeread will read blocking until len bytes have been read */
static int completeread(int fd, void *buffer, int len){
    int r;
    int total=0;
    int toread=len;
    while((r=read(fd,buffer+total,toread))){
        if(r==-1)
            return SSH_ERROR;
        total += r;
        toread-=r;
        if(total==len)
            return len;
        if(r==0)
            return 0;
    }
    return total ; /* connection closed */
}

/* in nonblocking mode, socket_read will read as much as it can, and return */
/* SSH_OK if it has read at least len bytes, otherwise, SSH_AGAIN. */
/* in blocking mode, it will read at least len bytes and will block until it's ok. */

static int socket_read(SSH_SESSION *session,int len){
    int except, can_write;
    int to_read;
    int r;
    char *buf;
    char buffer[4096];
    if(!session->in_socket_buffer)
        session->in_socket_buffer=buffer_new();
    to_read=len - buffer_get_rest_len(session->in_socket_buffer);
    if(to_read <= 0)
        return SSH_OK;
    if(session->blocking){
        buf=malloc(to_read);
        r=completeread(session->fd,buf,to_read);
        session->data_to_read=0;
        if(r==SSH_ERROR || r ==0){
            ssh_set_error(session,SSH_FATAL,
                (r==0)?"Connection closed by remote host" : "Error reading socket");
            close(session->fd);
            session->fd=-1;
            session->data_except=1;
            return SSH_ERROR;
        }

        buffer_add_data(session->in_socket_buffer,buf,to_read);
        free(buf);
        return SSH_OK;
    }
    /* nonblocking read */
    do {
        ssh_fd_poll(session,&can_write,&except); /* internally sets data_to_read */
        if(!session->data_to_read)
            return SSH_AGAIN;
        session->data_to_read=0;
        /* read as much as we can */
        r=read(session->fd,buffer,sizeof(buffer));
        if(r<=0){
            ssh_set_error(session,SSH_FATAL,
                (r==0)?"Connection closed by remote host" : "Error reading socket");
            close(session->fd);
            session->fd=-1;
            session->data_except=1;
            return SSH_ERROR;
        }
        buffer_add_data(session->in_socket_buffer,buffer,r);
    } while(buffer_get_rest_len(session->in_socket_buffer)<len);
    return SSH_OK;
}

#define PACKET_STATE_INIT 0
#define PACKET_STATE_SIZEREAD 1

static int packet_read2(SSH_SESSION *session){
    u32 len;
    void *packet=NULL;
    unsigned char mac[30];
    char buffer[16];
    int to_be_read;
    int ret;
    u8 padding;
    unsigned int blocksize=(session->current_crypto?session->current_crypto->in_cipher->blocksize:8);
    int current_macsize=session->current_crypto?macsize:0;
    if(!session->alive || session->data_except)
        return SSH_ERROR; // the error message was already set into this session
    switch(session->packet_state){
        case PACKET_STATE_INIT:    
            memset(&session->in_packet,0,sizeof(PACKET));
            if(session->in_buffer)
                buffer_reinit(session->in_buffer);
            else
                session->in_buffer=buffer_new();
            ret=socket_read(session,blocksize);
            if(ret != SSH_OK)
                return ret; // can be SSH_ERROR or SSH_AGAIN
//    be_read=completeread(session->fd,buffer,blocksize);
            memcpy(buffer,buffer_get_rest(session->in_socket_buffer),blocksize);
            buffer_pass_bytes(session->in_socket_buffer,blocksize); // mark them as read
            len=packet_decrypt_len(session,buffer);
            buffer_add_data(session->in_buffer,buffer,blocksize);
            if(len> MAX_PACKET_LEN){
                ssh_set_error(session,SSH_FATAL,"read_packet(): Packet len too high(%uld %.8lx)",len,len);
                return SSH_ERROR;
            }
            to_be_read=len-blocksize+sizeof(u32);
            if(to_be_read<0){
                /* remote sshd sends invalid sizes?*/
                ssh_set_error(session,SSH_FATAL,"given numbers of bytes left to be read <0 (%d)!",to_be_read);
                return SSH_ERROR;
            }
            /* saves the status of the current operations */
            session->in_packet.len=len;
            session->packet_state=PACKET_STATE_SIZEREAD;
        case PACKET_STATE_SIZEREAD:
            len=session->in_packet.len;
            to_be_read=len-blocksize+sizeof(u32) + current_macsize;
            /* if to_be_read is zero, the whole packet was blocksize bytes. */
            if(to_be_read != 0){ 
                ret=socket_read(session,to_be_read);
                if(ret!=SSH_OK)
                    return ret;
                packet=malloc(to_be_read);
                memcpy(packet,buffer_get_rest(session->in_socket_buffer),to_be_read-current_macsize);
                buffer_pass_bytes(session->in_socket_buffer,to_be_read-current_macsize);
                ssh_say(3,"Read a %d bytes packet\n",len);
                buffer_add_data(session->in_buffer,packet,to_be_read-current_macsize);
                free(packet);
            }
            if(session->current_crypto){
                /* decrypt the rest of the packet (blocksize bytes already have been decrypted */
                packet_decrypt(session,buffer_get(session->in_buffer)+blocksize,
                               buffer_get_len(session->in_buffer)-blocksize);
                memcpy(mac,buffer_get_rest(session->in_socket_buffer),macsize);
                buffer_pass_bytes(session->in_socket_buffer,macsize);
                if(packet_hmac_verify(session,session->in_buffer,mac)){
                    ssh_set_error(session,SSH_FATAL,"HMAC error");
                    return SSH_ERROR;
                }
            }
            buffer_pass_bytes(session->in_buffer,sizeof(u32));   
                /*pass the size which has been processed before*/
            if(!buffer_get_u8(session->in_buffer,&padding)){
                ssh_set_error(session,SSH_FATAL,"Packet too short to read padding");
                return SSH_ERROR;
            }
            ssh_say(3,"%hhd bytes padding, %d bytes left in buffer\n",
                    padding,buffer_get_rest_len(session->in_buffer));
            if(padding > buffer_get_rest_len(session->in_buffer)){
                ssh_set_error(session,SSH_FATAL,"invalid padding: %d (%d resting)",
                              padding,buffer_get_rest_len(session->in_buffer));
#ifdef DEBUG_CRYPTO
                ssh_print_hexa("incrimined packet",
                               buffer_get(session->in_buffer),buffer_get_len(session->in_buffer));
#endif
                return SSH_ERROR;
            }
            buffer_pass_bytes_end(session->in_buffer,padding);
            ssh_say(3,"After padding, %d bytes left in buffer\n",buffer_get_rest_len(session->in_buffer));
#ifdef HAVE_LIBZ
            if(session->current_crypto && session->current_crypto->do_compress_in){
                ssh_say(3,"Decompressing ...\n");
                decompress_buffer(session,session->in_buffer);
            }
#endif
            session->recv_seq++;
            session->packet_state=PACKET_STATE_INIT;
            return SSH_OK;
    }
    ssh_set_error(session,SSH_FATAL,"Invalid state into packet_read2() : %d",session->packet_state);
    return SSH_ERROR;
}

#ifdef HAVE_SSH1
/* a slighty modified packet_read2() for SSH-1 protocol */
static int packet_read1(SSH_SESSION *session){
    u32 len;
    void *packet=NULL;
    int ret;
    int to_be_read;
    u32 padding;
    u32 crc;
    ssh_say(3,"packet_read1()\n");
    if(!session->alive || session->data_except)
        return SSH_ERROR; // the error message was already set
    switch (session->packet_state){
        case PACKET_STATE_INIT:
            memset(&session->in_packet,0,sizeof(PACKET));
            if(session->in_buffer)
                buffer_reinit(session->in_buffer);
            else
                session->in_buffer=buffer_new();
            ret=socket_read(session,sizeof(u32));
            if(ret!=SSH_OK)
                return ret; // could be SSH_AGAIN
            buffer_get_u32(session->in_socket_buffer,&len);
            /*            be_read=completeread(session->fd,&len,sizeof(u32)); */
           /* len is not encrypted */
            len=ntohl(len); 
            if(len> MAX_PACKET_LEN){
                ssh_set_error(session,SSH_FATAL,"read_packet(): Packet len too high(%uld %.8lx)",len,len);
                return SSH_ERROR;
            }
            ssh_say(3,"%d bytes packet\n",len);
            session->in_packet.len=len;
            session->packet_state=PACKET_STATE_SIZEREAD;
        case PACKET_STATE_SIZEREAD:
            len=session->in_packet.len;
            /* SSH-1 has a fixed padding lenght */
            padding=8-(len % 8);
            to_be_read=len+padding;
            /* it is *not* possible that to_be_read be < 8. */
            ret=socket_read(session,to_be_read);
            if(ret != SSH_OK)
                return ret; // can be SSH_ERROR or SSH_AGAIN
            packet=malloc(to_be_read);
            memcpy(packet,buffer_get_rest(session->in_socket_buffer),to_be_read);
            buffer_pass_bytes(session->in_socket_buffer,to_be_read);
            buffer_add_data(session->in_buffer,packet,to_be_read);
            free(packet);
            
#ifdef DEBUG_CRYPTO
            ssh_print_hexa("read packet:",buffer_get(session->in_buffer),
                buffer_get_len(session->in_buffer));
#endif
            if(session->current_crypto){
        /* we decrypt everything, missing the lenght part (which was previously
         * read, unencrypted, and is not part of the buffer
         */
                packet_decrypt(session,buffer_get(session->in_buffer),buffer_get_len(session->in_buffer));
            }
#ifdef DEBUG_CRYPTO
            ssh_print_hexa("read packet decrypted:",
                           buffer_get(session->in_buffer),buffer_get_len(session->in_buffer));
#endif
            ssh_say(3,"%d bytes padding\n",padding);
            if((len+padding) != buffer_get_rest_len(session->in_buffer) || (len+padding) < sizeof(u32)){
                ssh_say(2,"no crc32 in packet\n");
                ssh_set_error(session,SSH_FATAL,"no crc32 in packet");
                return SSH_ERROR;
            }
            memcpy(&crc,buffer_get_rest(session->in_buffer)+(len+padding)-sizeof(u32),
            sizeof(u32));
            buffer_pass_bytes_end(session->in_buffer,sizeof(u32));
            crc=ntohl(crc);
            if(ssh_crc32(buffer_get_rest(session->in_buffer),(len+padding)-sizeof(u32))!=crc){
#ifdef DEBUG_CRYPTO
                ssh_print_hexa("crc32 on",buffer_get_rest(session->in_buffer),
                len + padding - sizeof(u32));
#endif
                ssh_say(2,"invalid crc32\n");
                ssh_set_error(session,SSH_FATAL,"invalid crc32 : expected %.8lx, "
                "got %.8lx",crc,
                ssh_crc32(buffer_get_rest(session->in_buffer),len+padding-sizeof(u32)) );
                return SSH_ERROR;
            }
            buffer_pass_bytes(session->in_buffer,padding);   /*pass the padding*/
            ssh_say(3,"the packet is valid\n");
/* will do that later 
#ifdef HAVE_LIBZ
    if(session->current_crypto && session->current_crypto->do_compress_in){
        decompress_buffer(session,session->in_buffer);
    }
#endif
*/
            session->recv_seq++;
            session->packet_state=PACKET_STATE_INIT;
            return SSH_OK;
    }
    ssh_set_error(session,SSH_FATAL,"Invalid state into packet_read2() : %d",session->packet_state);
    return SSH_ERROR;
}

#endif /* HAVE_SSH1 */

/* that's where i'd like C to be object ... */
int packet_read(SSH_SESSION *session){
#ifdef HAVE_SSH1
    if(session->version==1)
        return packet_read1(session);
    else
#endif
        return packet_read2(session);
}

int packet_translate(SSH_SESSION *session){
    memset(&session->in_packet,0,sizeof(PACKET));
    if(!session->in_buffer)
        return -1;
    ssh_say(3,"Final size %d\n",buffer_get_rest_len(session->in_buffer));
    if(!buffer_get_u8(session->in_buffer,&session->in_packet.type)){
        ssh_set_error(session,SSH_FATAL,"Packet too short to read type");
        return -1;
    }
    ssh_say(3,"type %hhd\n",session->in_packet.type);
    session->in_packet.valid=1;
    return 0;
}

static int atomic_write(int fd, void *buffer, int len){
    int written;
    do {
        written=write(fd,buffer,len);
        if(written==0 || written==-1)
            return -1;
        len-=written;
        buffer+=written;
    } while (len > 0);
    return SSH_OK;
}

/* when doing a nonblocking write, you should issue the packet_write only once, then 
 * do packet_nonblocking_flush() until you get a SSH_OK or a SSH_ERROR */
int packet_nonblocking_flush(SSH_SESSION *session){
    int except, can_write;
    int w;
    ssh_fd_poll(session,&can_write,&except); /* internally sets data_to_write */
    while(session->data_to_write && buffer_get_rest_len(session->out_socket_buffer)>0){
        w=write(session->fd,buffer_get_rest(session->out_socket_buffer),
                buffer_get_rest_len(session->out_socket_buffer));
        session->data_to_write=0;
        if(w<0){
            session->data_to_write=0;
            session->data_except=1;
            session->alive=0;
            close(session->fd);
            session->fd=-1;
            ssh_set_error(session,SSH_FATAL,"Writing packet : error on socket (or connection closed): %s",
                          strerror(errno));
            return SSH_ERROR;
        }
        buffer_pass_bytes(session->out_socket_buffer,w);
        /* refresh the socket status */
        ssh_fd_poll(session,&can_write,&except);
    }
    if(buffer_get_rest_len(session->out_socket_buffer)>0)
        return SSH_AGAIN;  /* there is data pending */
    return SSH_OK; // all data written
}

/* blocking packet flush */
int packet_blocking_flush(SSH_SESSION *session){
    if(atomic_write(session->fd,buffer_get_rest(session->out_socket_buffer),
       buffer_get_rest_len(session->out_socket_buffer))){
        session->data_to_write=0;
        session->data_except=1;
        session->alive=0;
        close(session->fd);
        session->fd=-1;
        ssh_set_error(session,SSH_FATAL,"Writing packet : error on socket (or connection closed): %s",
                         strerror(errno));
        return SSH_ERROR;
    }
    session->data_to_write=0;
    buffer_reinit(session->out_socket_buffer);
    return SSH_OK; // no data pending
}

/* this function places the outgoing packet buffer into an outgoing socket buffer */
static int socket_write(SSH_SESSION *session){
    if(!session->out_socket_buffer){
        session->out_socket_buffer=buffer_new();
    }
    buffer_add_data(session->out_socket_buffer,
               buffer_get(session->out_buffer),buffer_get_len(session->out_buffer));
    if(!session->blocking){
        return packet_nonblocking_flush(session);
    } else
        return packet_blocking_flush(session);
}

static int packet_send2(SSH_SESSION *session){
    char padstring[32];
    u32 finallen;
    u8 padding;
    u32 currentlen=buffer_get_len(session->out_buffer);
    unsigned char *hmac;
    int ret=0;
    unsigned int blocksize=(session->current_crypto?session->current_crypto->out_cipher->blocksize:8);
    ssh_say(3,"Writing on the wire a packet having %ld bytes before",currentlen);
#ifdef HAVE_LIBZ
    if(session->current_crypto && session->current_crypto->do_compress_out){
        ssh_say(3,"Compressing ...\n");
        compress_buffer(session,session->out_buffer);
        currentlen=buffer_get_len(session->out_buffer);
    }
#endif
    padding=(blocksize- ((currentlen+5) % blocksize));
    if(padding<4)
        padding+=blocksize;
    if(session->current_crypto)
        ssh_get_random(padstring,padding,0);
    else
        memset(padstring,0,padding);
    finallen=htonl(currentlen+padding+1);
    ssh_say(3,",%d bytes after comp + %d padding bytes = %d bytes packet\n",currentlen,padding,(ntohl(finallen)));
    buffer_add_data_begin(session->out_buffer,&padding,sizeof(u8));
    buffer_add_data_begin(session->out_buffer,&finallen,sizeof(u32));
    buffer_add_data(session->out_buffer,padstring,padding);
    hmac=packet_encrypt(session,buffer_get(session->out_buffer),buffer_get_len(session->out_buffer));
    if(hmac)
        buffer_add_data(session->out_buffer,hmac,20);
    ret=socket_write(session);
    session->send_seq++;
    buffer_reinit(session->out_buffer);
    return ret; /* SSH_OK, AGAIN or ERROR */
}

#ifdef HAVE_SSH1
static int packet_send1(SSH_SESSION *session){
    char padstring[32];
    u32 finallen;
    u8 padding;
    u32 crc;
    u32 currentlen=buffer_get_len(session->out_buffer)+sizeof(u32);
    int ret=0;
    unsigned int blocksize=(session->current_crypto?session->current_crypto->out_cipher->blocksize:8);
    ssh_say(3,"Writing on the wire a packet having %ld bytes before",currentlen);
/*
#ifdef HAVE_LIBZ
    if(session->current_crypto && session->current_crypto->do_compress_out){
        compress_buffer(session,session->out_buffer);
        currentlen=buffer_get_len(session->out_buffer);
    }
#endif
*/
    padding=blocksize-(currentlen % blocksize);
    if(session->current_crypto)
        ssh_get_random(padstring,padding,0);
    else
        memset(padstring,0,padding);
    finallen=htonl(currentlen);
    ssh_say(3,",%d bytes after comp + %d padding bytes = %d bytes packet\n",currentlen,padding,(ntohl(finallen)));
    buffer_add_data_begin(session->out_buffer,&padstring,padding);
    buffer_add_data_begin(session->out_buffer,&finallen,sizeof(u32));
    crc=ssh_crc32(buffer_get(session->out_buffer)+sizeof(u32),buffer_get_len(session->out_buffer)-sizeof(u32));
    buffer_add_u32(session->out_buffer,ntohl(crc));
#ifdef DEBUG_CRYPTO
    ssh_print_hexa("clear packet",buffer_get(session->out_buffer),
            buffer_get_len(session->out_buffer));
#endif
    packet_encrypt(session,buffer_get(session->out_buffer)+sizeof(u32),buffer_get_len(session->out_buffer)-sizeof(u32));
#ifdef DEBUG_CRYPTO
    ssh_print_hexa("encrypted packet",buffer_get(session->out_buffer),
            buffer_get_len(session->out_buffer));
#endif
    ret=socket_write(session);
    session->send_seq++;
    buffer_reinit(session->out_buffer);
    return ret;     /* SSH_OK, AGAIN or ERROR */
}

#endif /* HAVE_SSH1 */

int packet_send(SSH_SESSION *session){
#ifdef HAVE_SSH1
    if (session->version==1)
        return packet_send1(session);
    else 
#endif
        return packet_send2(session);
}

void packet_parse(SSH_SESSION *session){
    int type=session->in_packet.type;
    u32 foo;
    STRING *error_s;
    char *error=NULL;
#ifdef HAVE_SSH1
    if(session->version==1){
        /* SSH-1 */
        switch(type){
            case SSH_MSG_DISCONNECT:
                ssh_say(2,"Received SSH_MSG_DISCONNECT\n");
                ssh_set_error(session,SSH_FATAL,"Received SSH_MSG_DISCONNECT");
                close(session->fd);
                session->fd=-1;
                session->alive=0;
                return;
            case SSH_SMSG_STDOUT_DATA:
            case SSH_SMSG_STDERR_DATA:
            case SSH_SMSG_EXITSTATUS:
                channel_handle1(session,type);
                return;
            default:
                ssh_say(2,"Unexpected message code %d\n",type);
            }
        return;
    } else {
#endif /* HAVE_SSH1 */
    switch(type){
        case SSH2_MSG_DISCONNECT:
            buffer_get_u32(session->in_buffer,&foo);
            error_s=buffer_get_ssh_string(session->in_buffer);
            if(error_s)
                error=string_to_char(error_s);
            ssh_say(2,"Received SSH_MSG_DISCONNECT\n");
            ssh_set_error(session,SSH_FATAL,"Received SSH_MSG_DISCONNECT : %s",error);
            if(error_s){
                free(error_s);
                free(error);
            }
            close(session->fd);
            session->fd=-1;
            session->alive=0;
            return;
        case SSH2_MSG_CHANNEL_WINDOW_ADJUST:
        case SSH2_MSG_CHANNEL_DATA:
        case SSH2_MSG_CHANNEL_EXTENDED_DATA:
        case SSH2_MSG_CHANNEL_REQUEST:
        case SSH2_MSG_CHANNEL_EOF:
        case SSH2_MSG_CHANNEL_CLOSE:

            channel_handle(session,type);
        case SSH2_MSG_IGNORE:
            return;
        default:
            ssh_say(0,"Received unhandled msg %d\n",type);
    }
#ifdef HAVE_SSH1
    }
#endif
}    

#ifdef HAVE_SSH1
static int packet_wait1(SSH_SESSION *session,int type,int blocking){
    ssh_say(3,"packet_wait1 waiting for %d\n",type);
    while(1){
        if(packet_read1(session))
            return -1;
        if(packet_translate(session))
            return -1;
        ssh_say(3,"packet_wait 1 received %d\n",session->in_packet.type);
        switch(session->in_packet.type){
            case SSH_MSG_DISCONNECT:
                packet_parse(session);
                return -1;
            case SSH_SMSG_STDOUT_DATA:
            case SSH_SMSG_STDERR_DATA:
            case SSH_SMSG_EXITSTATUS:
                channel_handle1(session,type);
                break;
/*          case SSH2_MSG_CHANNEL_CLOSE:
               packet_parse(session);
                break;;
           case SSH2_MSG_IGNORE:
               break;
*/               
            default:
               if(type && (type != session->in_packet.type)){
                   ssh_set_error(session,SSH_FATAL,"waitpacket(): Received a %d type packet, was waiting for a %d\n",session->in_packet.type,type);
                   return -1;
               }
               return 0;
           }
        if(blocking==0)
            return 0;
    }
    return 0;
}
#endif /* HAVE_SSH1 */
static int packet_wait2(SSH_SESSION *session,int type,int blocking){
    while(1){
        if(packet_read2(session))
            return -1;
        if(packet_translate(session))
            return -1;
        switch(session->in_packet.type){
           case SSH2_MSG_DISCONNECT:
               packet_parse(session);
                return -1;
           case SSH2_MSG_CHANNEL_WINDOW_ADJUST:
           case SSH2_MSG_CHANNEL_DATA:
           case SSH2_MSG_CHANNEL_EXTENDED_DATA:
           case SSH2_MSG_CHANNEL_REQUEST:
           case SSH2_MSG_CHANNEL_EOF:
           case SSH2_MSG_CHANNEL_CLOSE:
               packet_parse(session);
                break;;
           case SSH2_MSG_IGNORE:
               break;
           default:
               if(type && (type != session->in_packet.type)){
                   ssh_set_error(session,SSH_FATAL,"waitpacket(): Received a %d type packet, was waiting for a %d\n",session->in_packet.type,type);
                   return -1;
               }
               return 0;
           }
        if(blocking==0)
            return 0;
    }
    return 0;
}
int packet_wait(SSH_SESSION *session, int type, int block){
#ifdef HAVE_SSH1
    if(session->version==1)
        return packet_wait1(session,type,block);
    else
#endif
        return packet_wait2(session,type,block);
}


void packet_clear_out(SSH_SESSION *session){
    if(session->out_buffer)
        buffer_reinit(session->out_buffer);
    else
        session->out_buffer=buffer_new();
}