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