Home | History | Annotate | Line # | Download | only in dist
nchan.c revision 1.1.1.2
      1      1.1  christos /*	$NetBSD: nchan.c,v 1.1.1.2 2010/11/21 17:05:49 adam Exp $	*/
      2  1.1.1.2      adam /* $OpenBSD: nchan.c,v 1.63 2010/01/26 01:28:35 djm Exp $ */
      3      1.1  christos /*
      4      1.1  christos  * Copyright (c) 1999, 2000, 2001, 2002 Markus Friedl.  All rights reserved.
      5      1.1  christos  *
      6      1.1  christos  * Redistribution and use in source and binary forms, with or without
      7      1.1  christos  * modification, are permitted provided that the following conditions
      8      1.1  christos  * are met:
      9      1.1  christos  * 1. Redistributions of source code must retain the above copyright
     10      1.1  christos  *    notice, this list of conditions and the following disclaimer.
     11      1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     12      1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     13      1.1  christos  *    documentation and/or other materials provided with the distribution.
     14      1.1  christos  *
     15      1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16      1.1  christos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17      1.1  christos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18      1.1  christos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19      1.1  christos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     20      1.1  christos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     21      1.1  christos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     22      1.1  christos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23      1.1  christos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     24      1.1  christos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25      1.1  christos  */
     26      1.1  christos 
     27      1.1  christos #include <sys/types.h>
     28      1.1  christos #include <sys/socket.h>
     29      1.1  christos #include <sys/queue.h>
     30      1.1  christos 
     31      1.1  christos #include <errno.h>
     32      1.1  christos #include <string.h>
     33      1.1  christos #include <stdarg.h>
     34      1.1  christos 
     35      1.1  christos #include "ssh1.h"
     36      1.1  christos #include "ssh2.h"
     37      1.1  christos #include "buffer.h"
     38      1.1  christos #include "packet.h"
     39      1.1  christos #include "channels.h"
     40      1.1  christos #include "compat.h"
     41      1.1  christos #include "log.h"
     42      1.1  christos 
     43      1.1  christos /*
     44      1.1  christos  * SSH Protocol 1.5 aka New Channel Protocol
     45      1.1  christos  * Thanks to Martina, Axel and everyone who left Erlangen, leaving me bored.
     46      1.1  christos  * Written by Markus Friedl in October 1999
     47      1.1  christos  *
     48      1.1  christos  * Protocol versions 1.3 and 1.5 differ in the handshake protocol used for the
     49      1.1  christos  * tear down of channels:
     50      1.1  christos  *
     51      1.1  christos  * 1.3:	strict request-ack-protocol:
     52      1.1  christos  *	CLOSE	->
     53      1.1  christos  *		<-  CLOSE_CONFIRM
     54      1.1  christos  *
     55      1.1  christos  * 1.5:	uses variations of:
     56      1.1  christos  *	IEOF	->
     57      1.1  christos  *		<-  OCLOSE
     58      1.1  christos  *		<-  IEOF
     59      1.1  christos  *	OCLOSE	->
     60      1.1  christos  *	i.e. both sides have to close the channel
     61      1.1  christos  *
     62      1.1  christos  * 2.0: the EOF messages are optional
     63      1.1  christos  *
     64      1.1  christos  * See the debugging output from 'ssh -v' and 'sshd -d' of
     65      1.1  christos  * ssh-1.2.27 as an example.
     66      1.1  christos  *
     67      1.1  christos  */
     68      1.1  christos 
     69      1.1  christos /* functions manipulating channel states */
     70      1.1  christos /*
     71      1.1  christos  * EVENTS update channel input/output states execute ACTIONS
     72      1.1  christos  */
     73      1.1  christos /*
     74      1.1  christos  * ACTIONS: should never update the channel states
     75      1.1  christos  */
     76      1.1  christos static void	chan_send_ieof1(Channel *);
     77      1.1  christos static void	chan_send_oclose1(Channel *);
     78      1.1  christos static void	chan_send_close2(Channel *);
     79      1.1  christos static void	chan_send_eof2(Channel *);
     80      1.1  christos static void	chan_send_eow2(Channel *);
     81      1.1  christos 
     82      1.1  christos /* helper */
     83      1.1  christos static void	chan_shutdown_write(Channel *);
     84      1.1  christos static void	chan_shutdown_read(Channel *);
     85      1.1  christos 
     86      1.1  christos static char *ostates[] = { "open", "drain", "wait_ieof", "closed" };
     87      1.1  christos static char *istates[] = { "open", "drain", "wait_oclose", "closed" };
     88      1.1  christos 
     89      1.1  christos static void
     90      1.1  christos chan_set_istate(Channel *c, u_int next)
     91      1.1  christos {
     92      1.1  christos 	if (c->istate > CHAN_INPUT_CLOSED || next > CHAN_INPUT_CLOSED)
     93      1.1  christos 		fatal("chan_set_istate: bad state %d -> %d", c->istate, next);
     94      1.1  christos 	debug2("channel %d: input %s -> %s", c->self, istates[c->istate],
     95      1.1  christos 	    istates[next]);
     96      1.1  christos 	c->istate = next;
     97      1.1  christos }
     98      1.1  christos static void
     99      1.1  christos chan_set_ostate(Channel *c, u_int next)
    100      1.1  christos {
    101      1.1  christos 	if (c->ostate > CHAN_OUTPUT_CLOSED || next > CHAN_OUTPUT_CLOSED)
    102      1.1  christos 		fatal("chan_set_ostate: bad state %d -> %d", c->ostate, next);
    103      1.1  christos 	debug2("channel %d: output %s -> %s", c->self, ostates[c->ostate],
    104      1.1  christos 	    ostates[next]);
    105      1.1  christos 	c->ostate = next;
    106      1.1  christos }
    107      1.1  christos 
    108      1.1  christos /*
    109      1.1  christos  * SSH1 specific implementation of event functions
    110      1.1  christos  */
    111      1.1  christos 
    112      1.1  christos static void
    113      1.1  christos chan_rcvd_oclose1(Channel *c)
    114      1.1  christos {
    115      1.1  christos 	debug2("channel %d: rcvd oclose", c->self);
    116      1.1  christos 	switch (c->istate) {
    117      1.1  christos 	case CHAN_INPUT_WAIT_OCLOSE:
    118      1.1  christos 		chan_set_istate(c, CHAN_INPUT_CLOSED);
    119      1.1  christos 		break;
    120      1.1  christos 	case CHAN_INPUT_OPEN:
    121      1.1  christos 		chan_shutdown_read(c);
    122      1.1  christos 		chan_send_ieof1(c);
    123      1.1  christos 		chan_set_istate(c, CHAN_INPUT_CLOSED);
    124      1.1  christos 		break;
    125      1.1  christos 	case CHAN_INPUT_WAIT_DRAIN:
    126      1.1  christos 		/* both local read_failed and remote write_failed  */
    127      1.1  christos 		chan_send_ieof1(c);
    128      1.1  christos 		chan_set_istate(c, CHAN_INPUT_CLOSED);
    129      1.1  christos 		break;
    130      1.1  christos 	default:
    131      1.1  christos 		error("channel %d: protocol error: rcvd_oclose for istate %d",
    132      1.1  christos 		    c->self, c->istate);
    133      1.1  christos 		return;
    134      1.1  christos 	}
    135      1.1  christos }
    136      1.1  christos void
    137      1.1  christos chan_read_failed(Channel *c)
    138      1.1  christos {
    139      1.1  christos 	debug2("channel %d: read failed", c->self);
    140      1.1  christos 	switch (c->istate) {
    141      1.1  christos 	case CHAN_INPUT_OPEN:
    142      1.1  christos 		chan_shutdown_read(c);
    143      1.1  christos 		chan_set_istate(c, CHAN_INPUT_WAIT_DRAIN);
    144      1.1  christos 		break;
    145      1.1  christos 	default:
    146      1.1  christos 		error("channel %d: chan_read_failed for istate %d",
    147      1.1  christos 		    c->self, c->istate);
    148      1.1  christos 		break;
    149      1.1  christos 	}
    150      1.1  christos }
    151      1.1  christos void
    152      1.1  christos chan_ibuf_empty(Channel *c)
    153      1.1  christos {
    154      1.1  christos 	debug2("channel %d: ibuf empty", c->self);
    155      1.1  christos 	if (buffer_len(&c->input)) {
    156      1.1  christos 		error("channel %d: chan_ibuf_empty for non empty buffer",
    157      1.1  christos 		    c->self);
    158      1.1  christos 		return;
    159      1.1  christos 	}
    160      1.1  christos 	switch (c->istate) {
    161      1.1  christos 	case CHAN_INPUT_WAIT_DRAIN:
    162      1.1  christos 		if (compat20) {
    163  1.1.1.2      adam 			if (!(c->flags & (CHAN_CLOSE_SENT|CHAN_LOCAL)))
    164      1.1  christos 				chan_send_eof2(c);
    165      1.1  christos 			chan_set_istate(c, CHAN_INPUT_CLOSED);
    166      1.1  christos 		} else {
    167      1.1  christos 			chan_send_ieof1(c);
    168      1.1  christos 			chan_set_istate(c, CHAN_INPUT_WAIT_OCLOSE);
    169      1.1  christos 		}
    170      1.1  christos 		break;
    171      1.1  christos 	default:
    172      1.1  christos 		error("channel %d: chan_ibuf_empty for istate %d",
    173      1.1  christos 		    c->self, c->istate);
    174      1.1  christos 		break;
    175      1.1  christos 	}
    176      1.1  christos }
    177      1.1  christos static void
    178      1.1  christos chan_rcvd_ieof1(Channel *c)
    179      1.1  christos {
    180      1.1  christos 	debug2("channel %d: rcvd ieof", c->self);
    181      1.1  christos 	switch (c->ostate) {
    182      1.1  christos 	case CHAN_OUTPUT_OPEN:
    183      1.1  christos 		chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
    184      1.1  christos 		break;
    185      1.1  christos 	case CHAN_OUTPUT_WAIT_IEOF:
    186      1.1  christos 		chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
    187      1.1  christos 		break;
    188      1.1  christos 	default:
    189      1.1  christos 		error("channel %d: protocol error: rcvd_ieof for ostate %d",
    190      1.1  christos 		    c->self, c->ostate);
    191      1.1  christos 		break;
    192      1.1  christos 	}
    193      1.1  christos }
    194      1.1  christos static void
    195      1.1  christos chan_write_failed1(Channel *c)
    196      1.1  christos {
    197      1.1  christos 	debug2("channel %d: write failed", c->self);
    198      1.1  christos 	switch (c->ostate) {
    199      1.1  christos 	case CHAN_OUTPUT_OPEN:
    200      1.1  christos 		chan_shutdown_write(c);
    201      1.1  christos 		chan_send_oclose1(c);
    202      1.1  christos 		chan_set_ostate(c, CHAN_OUTPUT_WAIT_IEOF);
    203      1.1  christos 		break;
    204      1.1  christos 	case CHAN_OUTPUT_WAIT_DRAIN:
    205      1.1  christos 		chan_shutdown_write(c);
    206      1.1  christos 		chan_send_oclose1(c);
    207      1.1  christos 		chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
    208      1.1  christos 		break;
    209      1.1  christos 	default:
    210      1.1  christos 		error("channel %d: chan_write_failed for ostate %d",
    211      1.1  christos 		    c->self, c->ostate);
    212      1.1  christos 		break;
    213      1.1  christos 	}
    214      1.1  christos }
    215      1.1  christos void
    216      1.1  christos chan_obuf_empty(Channel *c)
    217      1.1  christos {
    218      1.1  christos 	debug2("channel %d: obuf empty", c->self);
    219      1.1  christos 	if (buffer_len(&c->output)) {
    220      1.1  christos 		error("channel %d: chan_obuf_empty for non empty buffer",
    221      1.1  christos 		    c->self);
    222      1.1  christos 		return;
    223      1.1  christos 	}
    224      1.1  christos 	switch (c->ostate) {
    225      1.1  christos 	case CHAN_OUTPUT_WAIT_DRAIN:
    226      1.1  christos 		chan_shutdown_write(c);
    227      1.1  christos 		if (!compat20)
    228      1.1  christos 			chan_send_oclose1(c);
    229      1.1  christos 		chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
    230      1.1  christos 		break;
    231      1.1  christos 	default:
    232      1.1  christos 		error("channel %d: internal error: obuf_empty for ostate %d",
    233      1.1  christos 		    c->self, c->ostate);
    234      1.1  christos 		break;
    235      1.1  christos 	}
    236      1.1  christos }
    237      1.1  christos static void
    238      1.1  christos chan_send_ieof1(Channel *c)
    239      1.1  christos {
    240      1.1  christos 	debug2("channel %d: send ieof", c->self);
    241      1.1  christos 	switch (c->istate) {
    242      1.1  christos 	case CHAN_INPUT_OPEN:
    243      1.1  christos 	case CHAN_INPUT_WAIT_DRAIN:
    244      1.1  christos 		packet_start(SSH_MSG_CHANNEL_INPUT_EOF);
    245      1.1  christos 		packet_put_int(c->remote_id);
    246      1.1  christos 		packet_send();
    247      1.1  christos 		break;
    248      1.1  christos 	default:
    249      1.1  christos 		error("channel %d: cannot send ieof for istate %d",
    250      1.1  christos 		    c->self, c->istate);
    251      1.1  christos 		break;
    252      1.1  christos 	}
    253      1.1  christos }
    254      1.1  christos static void
    255      1.1  christos chan_send_oclose1(Channel *c)
    256      1.1  christos {
    257      1.1  christos 	debug2("channel %d: send oclose", c->self);
    258      1.1  christos 	switch (c->ostate) {
    259      1.1  christos 	case CHAN_OUTPUT_OPEN:
    260      1.1  christos 	case CHAN_OUTPUT_WAIT_DRAIN:
    261      1.1  christos 		buffer_clear(&c->output);
    262      1.1  christos 		packet_start(SSH_MSG_CHANNEL_OUTPUT_CLOSE);
    263      1.1  christos 		packet_put_int(c->remote_id);
    264      1.1  christos 		packet_send();
    265      1.1  christos 		break;
    266      1.1  christos 	default:
    267      1.1  christos 		error("channel %d: cannot send oclose for ostate %d",
    268      1.1  christos 		    c->self, c->ostate);
    269      1.1  christos 		break;
    270      1.1  christos 	}
    271      1.1  christos }
    272      1.1  christos 
    273      1.1  christos /*
    274      1.1  christos  * the same for SSH2
    275      1.1  christos  */
    276      1.1  christos static void
    277      1.1  christos chan_rcvd_close2(Channel *c)
    278      1.1  christos {
    279      1.1  christos 	debug2("channel %d: rcvd close", c->self);
    280  1.1.1.2      adam 	if (!(c->flags & CHAN_LOCAL)) {
    281  1.1.1.2      adam 		if (c->flags & CHAN_CLOSE_RCVD)
    282  1.1.1.2      adam 			error("channel %d: protocol error: close rcvd twice",
    283  1.1.1.2      adam 			    c->self);
    284  1.1.1.2      adam 		c->flags |= CHAN_CLOSE_RCVD;
    285  1.1.1.2      adam 	}
    286      1.1  christos 	if (c->type == SSH_CHANNEL_LARVAL) {
    287      1.1  christos 		/* tear down larval channels immediately */
    288      1.1  christos 		chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
    289      1.1  christos 		chan_set_istate(c, CHAN_INPUT_CLOSED);
    290      1.1  christos 		return;
    291      1.1  christos 	}
    292      1.1  christos 	switch (c->ostate) {
    293      1.1  christos 	case CHAN_OUTPUT_OPEN:
    294      1.1  christos 		/*
    295      1.1  christos 		 * wait until a data from the channel is consumed if a CLOSE
    296      1.1  christos 		 * is received
    297      1.1  christos 		 */
    298      1.1  christos 		chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
    299      1.1  christos 		break;
    300      1.1  christos 	}
    301      1.1  christos 	switch (c->istate) {
    302      1.1  christos 	case CHAN_INPUT_OPEN:
    303      1.1  christos 		chan_shutdown_read(c);
    304      1.1  christos 		chan_set_istate(c, CHAN_INPUT_CLOSED);
    305      1.1  christos 		break;
    306      1.1  christos 	case CHAN_INPUT_WAIT_DRAIN:
    307  1.1.1.2      adam 		if (!(c->flags & CHAN_LOCAL))
    308  1.1.1.2      adam 			chan_send_eof2(c);
    309      1.1  christos 		chan_set_istate(c, CHAN_INPUT_CLOSED);
    310      1.1  christos 		break;
    311      1.1  christos 	}
    312      1.1  christos }
    313  1.1.1.2      adam 
    314      1.1  christos void
    315      1.1  christos chan_rcvd_eow(Channel *c)
    316      1.1  christos {
    317      1.1  christos 	debug2("channel %d: rcvd eow", c->self);
    318      1.1  christos 	switch (c->istate) {
    319      1.1  christos 	case CHAN_INPUT_OPEN:
    320      1.1  christos 		chan_shutdown_read(c);
    321      1.1  christos 		chan_set_istate(c, CHAN_INPUT_CLOSED);
    322      1.1  christos 		break;
    323      1.1  christos 	}
    324      1.1  christos }
    325      1.1  christos static void
    326      1.1  christos chan_rcvd_eof2(Channel *c)
    327      1.1  christos {
    328      1.1  christos 	debug2("channel %d: rcvd eof", c->self);
    329      1.1  christos 	c->flags |= CHAN_EOF_RCVD;
    330      1.1  christos 	if (c->ostate == CHAN_OUTPUT_OPEN)
    331      1.1  christos 		chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
    332      1.1  christos }
    333      1.1  christos static void
    334      1.1  christos chan_write_failed2(Channel *c)
    335      1.1  christos {
    336      1.1  christos 	debug2("channel %d: write failed", c->self);
    337      1.1  christos 	switch (c->ostate) {
    338      1.1  christos 	case CHAN_OUTPUT_OPEN:
    339      1.1  christos 	case CHAN_OUTPUT_WAIT_DRAIN:
    340      1.1  christos 		chan_shutdown_write(c);
    341      1.1  christos 		if (strcmp(c->ctype, "session") == 0)
    342      1.1  christos 			chan_send_eow2(c);
    343      1.1  christos 		chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
    344      1.1  christos 		break;
    345      1.1  christos 	default:
    346      1.1  christos 		error("channel %d: chan_write_failed for ostate %d",
    347      1.1  christos 		    c->self, c->ostate);
    348      1.1  christos 		break;
    349      1.1  christos 	}
    350      1.1  christos }
    351      1.1  christos static void
    352      1.1  christos chan_send_eof2(Channel *c)
    353      1.1  christos {
    354      1.1  christos 	debug2("channel %d: send eof", c->self);
    355      1.1  christos 	switch (c->istate) {
    356      1.1  christos 	case CHAN_INPUT_WAIT_DRAIN:
    357      1.1  christos 		packet_start(SSH2_MSG_CHANNEL_EOF);
    358      1.1  christos 		packet_put_int(c->remote_id);
    359      1.1  christos 		packet_send();
    360      1.1  christos 		c->flags |= CHAN_EOF_SENT;
    361      1.1  christos 		break;
    362      1.1  christos 	default:
    363      1.1  christos 		error("channel %d: cannot send eof for istate %d",
    364      1.1  christos 		    c->self, c->istate);
    365      1.1  christos 		break;
    366      1.1  christos 	}
    367      1.1  christos }
    368      1.1  christos static void
    369      1.1  christos chan_send_close2(Channel *c)
    370      1.1  christos {
    371      1.1  christos 	debug2("channel %d: send close", c->self);
    372      1.1  christos 	if (c->ostate != CHAN_OUTPUT_CLOSED ||
    373      1.1  christos 	    c->istate != CHAN_INPUT_CLOSED) {
    374      1.1  christos 		error("channel %d: cannot send close for istate/ostate %d/%d",
    375      1.1  christos 		    c->self, c->istate, c->ostate);
    376      1.1  christos 	} else if (c->flags & CHAN_CLOSE_SENT) {
    377      1.1  christos 		error("channel %d: already sent close", c->self);
    378      1.1  christos 	} else {
    379      1.1  christos 		packet_start(SSH2_MSG_CHANNEL_CLOSE);
    380      1.1  christos 		packet_put_int(c->remote_id);
    381      1.1  christos 		packet_send();
    382      1.1  christos 		c->flags |= CHAN_CLOSE_SENT;
    383      1.1  christos 	}
    384      1.1  christos }
    385      1.1  christos static void
    386      1.1  christos chan_send_eow2(Channel *c)
    387      1.1  christos {
    388      1.1  christos 	debug2("channel %d: send eow", c->self);
    389      1.1  christos 	if (c->ostate == CHAN_OUTPUT_CLOSED) {
    390      1.1  christos 		error("channel %d: must not sent eow on closed output",
    391      1.1  christos 		    c->self);
    392      1.1  christos 		return;
    393      1.1  christos 	}
    394      1.1  christos 	if (!(datafellows & SSH_NEW_OPENSSH))
    395      1.1  christos 		return;
    396      1.1  christos 	packet_start(SSH2_MSG_CHANNEL_REQUEST);
    397      1.1  christos 	packet_put_int(c->remote_id);
    398      1.1  christos 	packet_put_cstring("eow (at) openssh.com");
    399      1.1  christos 	packet_put_char(0);
    400      1.1  christos 	packet_send();
    401      1.1  christos }
    402      1.1  christos 
    403      1.1  christos /* shared */
    404      1.1  christos 
    405      1.1  christos void
    406      1.1  christos chan_rcvd_ieof(Channel *c)
    407      1.1  christos {
    408      1.1  christos 	if (compat20)
    409      1.1  christos 		chan_rcvd_eof2(c);
    410      1.1  christos 	else
    411      1.1  christos 		chan_rcvd_ieof1(c);
    412      1.1  christos 	if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN &&
    413      1.1  christos 	    buffer_len(&c->output) == 0 &&
    414      1.1  christos 	    !CHANNEL_EFD_OUTPUT_ACTIVE(c))
    415      1.1  christos 		chan_obuf_empty(c);
    416      1.1  christos }
    417      1.1  christos void
    418      1.1  christos chan_rcvd_oclose(Channel *c)
    419      1.1  christos {
    420      1.1  christos 	if (compat20)
    421      1.1  christos 		chan_rcvd_close2(c);
    422      1.1  christos 	else
    423      1.1  christos 		chan_rcvd_oclose1(c);
    424      1.1  christos }
    425      1.1  christos void
    426      1.1  christos chan_write_failed(Channel *c)
    427      1.1  christos {
    428      1.1  christos 	if (compat20)
    429      1.1  christos 		chan_write_failed2(c);
    430      1.1  christos 	else
    431      1.1  christos 		chan_write_failed1(c);
    432      1.1  christos }
    433      1.1  christos 
    434      1.1  christos void
    435      1.1  christos chan_mark_dead(Channel *c)
    436      1.1  christos {
    437      1.1  christos 	c->type = SSH_CHANNEL_ZOMBIE;
    438      1.1  christos }
    439      1.1  christos 
    440      1.1  christos int
    441      1.1  christos chan_is_dead(Channel *c, int do_send)
    442      1.1  christos {
    443      1.1  christos 	if (c->type == SSH_CHANNEL_ZOMBIE) {
    444      1.1  christos 		debug2("channel %d: zombie", c->self);
    445      1.1  christos 		return 1;
    446      1.1  christos 	}
    447      1.1  christos 	if (c->istate != CHAN_INPUT_CLOSED || c->ostate != CHAN_OUTPUT_CLOSED)
    448      1.1  christos 		return 0;
    449      1.1  christos 	if (!compat20) {
    450      1.1  christos 		debug2("channel %d: is dead", c->self);
    451      1.1  christos 		return 1;
    452      1.1  christos 	}
    453      1.1  christos 	if ((datafellows & SSH_BUG_EXTEOF) &&
    454      1.1  christos 	    c->extended_usage == CHAN_EXTENDED_WRITE &&
    455      1.1  christos 	    c->efd != -1 &&
    456      1.1  christos 	    buffer_len(&c->extended) > 0) {
    457      1.1  christos 		debug2("channel %d: active efd: %d len %d",
    458      1.1  christos 		    c->self, c->efd, buffer_len(&c->extended));
    459      1.1  christos 		return 0;
    460      1.1  christos 	}
    461  1.1.1.2      adam 	if (c->flags & CHAN_LOCAL) {
    462  1.1.1.2      adam 		debug2("channel %d: is dead (local)", c->self);
    463  1.1.1.2      adam 		return 1;
    464  1.1.1.2      adam 	}
    465      1.1  christos 	if (!(c->flags & CHAN_CLOSE_SENT)) {
    466      1.1  christos 		if (do_send) {
    467      1.1  christos 			chan_send_close2(c);
    468      1.1  christos 		} else {
    469      1.1  christos 			/* channel would be dead if we sent a close */
    470      1.1  christos 			if (c->flags & CHAN_CLOSE_RCVD) {
    471      1.1  christos 				debug2("channel %d: almost dead",
    472      1.1  christos 				    c->self);
    473      1.1  christos 				return 1;
    474      1.1  christos 			}
    475      1.1  christos 		}
    476      1.1  christos 	}
    477      1.1  christos 	if ((c->flags & CHAN_CLOSE_SENT) &&
    478      1.1  christos 	    (c->flags & CHAN_CLOSE_RCVD)) {
    479      1.1  christos 		debug2("channel %d: is dead", c->self);
    480      1.1  christos 		return 1;
    481      1.1  christos 	}
    482      1.1  christos 	return 0;
    483      1.1  christos }
    484      1.1  christos 
    485      1.1  christos /* helper */
    486      1.1  christos static void
    487      1.1  christos chan_shutdown_write(Channel *c)
    488      1.1  christos {
    489      1.1  christos 	buffer_clear(&c->output);
    490      1.1  christos 	if (compat20 && c->type == SSH_CHANNEL_LARVAL)
    491      1.1  christos 		return;
    492      1.1  christos 	/* shutdown failure is allowed if write failed already */
    493      1.1  christos 	debug2("channel %d: close_write", c->self);
    494      1.1  christos 	if (c->sock != -1) {
    495      1.1  christos 		if (shutdown(c->sock, SHUT_WR) < 0)
    496      1.1  christos 			debug2("channel %d: chan_shutdown_write: "
    497      1.1  christos 			    "shutdown() failed for fd %d: %.100s",
    498      1.1  christos 			    c->self, c->sock, strerror(errno));
    499      1.1  christos 	} else {
    500      1.1  christos 		if (channel_close_fd(&c->wfd) < 0)
    501      1.1  christos 			logit("channel %d: chan_shutdown_write: "
    502      1.1  christos 			    "close() failed for fd %d: %.100s",
    503      1.1  christos 			    c->self, c->wfd, strerror(errno));
    504      1.1  christos 	}
    505      1.1  christos }
    506      1.1  christos static void
    507      1.1  christos chan_shutdown_read(Channel *c)
    508      1.1  christos {
    509      1.1  christos 	if (compat20 && c->type == SSH_CHANNEL_LARVAL)
    510      1.1  christos 		return;
    511      1.1  christos 	debug2("channel %d: close_read", c->self);
    512      1.1  christos 	if (c->sock != -1) {
    513      1.1  christos 		if (shutdown(c->sock, SHUT_RD) < 0)
    514      1.1  christos 			error("channel %d: chan_shutdown_read: "
    515      1.1  christos 			    "shutdown() failed for fd %d [i%d o%d]: %.100s",
    516      1.1  christos 			    c->self, c->sock, c->istate, c->ostate,
    517      1.1  christos 			    strerror(errno));
    518      1.1  christos 	} else {
    519      1.1  christos 		if (channel_close_fd(&c->rfd) < 0)
    520      1.1  christos 			logit("channel %d: chan_shutdown_read: "
    521      1.1  christos 			    "close() failed for fd %d: %.100s",
    522      1.1  christos 			    c->self, c->rfd, strerror(errno));
    523      1.1  christos 	}
    524      1.1  christos }
    525