blob: ff7607b9c4a427fe748b3238ef5f5cc9e34a67e8 [file] [log] [blame]
Damien Miller5428f641999-11-25 11:54:57 +11001/*
Ben Lindstrom44697232001-07-04 03:32:30 +00002 * Copyright (c) 1999, 2000, 2001 Markus Friedl. All rights reserved.
Damien Miller5428f641999-11-25 11:54:57 +11003 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
Damien Miller5428f641999-11-25 11:54:57 +110012 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
Damien Millerd4a8b7e1999-10-27 13:42:43 +100025#include "includes.h"
Damien Miller89a92322002-01-22 23:27:28 +110026RCSID("$OpenBSD: nchan.c,v 1.38 2002/01/14 13:22:35 markus Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100027
Ben Lindstrom226cfa02001-01-22 05:34:40 +000028#include "ssh1.h"
29#include "ssh2.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100030#include "buffer.h"
31#include "packet.h"
32#include "channels.h"
Damien Miller33b13562000-04-04 14:38:59 +100033#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000034#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100035
Ben Lindstrom3b670d02001-06-09 00:57:39 +000036/*
37 * SSH Protocol 1.5 aka New Channel Protocol
38 * Thanks to Martina, Axel and everyone who left Erlangen, leaving me bored.
39 * Written by Markus Friedl in October 1999
40 *
41 * Protocol versions 1.3 and 1.5 differ in the handshake protocol used for the
42 * tear down of channels:
43 *
44 * 1.3: strict request-ack-protocol:
45 * CLOSE ->
46 * <- CLOSE_CONFIRM
47 *
48 * 1.5: uses variations of:
49 * IEOF ->
50 * <- OCLOSE
51 * <- IEOF
52 * OCLOSE ->
53 * i.e. both sides have to close the channel
54 *
55 * 2.0: the EOF messages are optional
56 *
57 * See the debugging output from 'ssh -v' and 'sshd -d' of
58 * ssh-1.2.27 as an example.
59 *
60 */
Kevin Stevesc3422eb2001-09-20 19:33:33 +000061
Damien Miller33b13562000-04-04 14:38:59 +100062/* functions manipulating channel states */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100063/*
Damien Miller95def091999-11-25 00:26:21 +110064 * EVENTS update channel input/output states execute ACTIONS
Damien Millerd4a8b7e1999-10-27 13:42:43 +100065 */
66/* events concerning the INPUT from socket for channel (istate) */
Damien Miller33b13562000-04-04 14:38:59 +100067chan_event_fn *chan_rcvd_oclose = NULL;
68chan_event_fn *chan_read_failed = NULL;
69chan_event_fn *chan_ibuf_empty = NULL;
70/* events concerning the OUTPUT from channel for socket (ostate) */
71chan_event_fn *chan_rcvd_ieof = NULL;
72chan_event_fn *chan_write_failed = NULL;
73chan_event_fn *chan_obuf_empty = NULL;
74/*
75 * ACTIONS: should never update the channel states
76 */
Ben Lindstrombba81212001-06-25 05:01:22 +000077static void chan_send_ieof1(Channel *);
78static void chan_send_oclose1(Channel *);
79static void chan_send_close2(Channel *);
80static void chan_send_eof2(Channel *);
Damien Miller33b13562000-04-04 14:38:59 +100081
Damien Miller33b13562000-04-04 14:38:59 +100082/* helper */
Ben Lindstrombba81212001-06-25 05:01:22 +000083static void chan_shutdown_write(Channel *);
84static void chan_shutdown_read(Channel *);
Damien Miller33b13562000-04-04 14:38:59 +100085
Damien Millerabea8ee2002-01-22 23:27:11 +110086static char *ostates[] = { "open", "drain", "wait_ieof", "closed" };
87static char *istates[] = { "open", "drain", "wait_oclose", "closed" };
88
89static void
90chan_set_istate(Channel *c, u_int next)
91{
92 if (c->istate > CHAN_INPUT_CLOSED || next > CHAN_INPUT_CLOSED)
93 fatal("chan_set_istate: bad state %d -> %d", c->istate, next);
94 debug("channel %d: input %s -> %s", c->self, istates[c->istate],
95 istates[next]);
96 c->istate = next;
97}
98static void
99chan_set_ostate(Channel *c, u_int next)
100{
101 if (c->ostate > CHAN_OUTPUT_CLOSED || next > CHAN_OUTPUT_CLOSED)
102 fatal("chan_set_ostate: bad state %d -> %d", c->ostate, next);
103 debug("channel %d: output %s -> %s", c->self, ostates[c->ostate],
104 ostates[next]);
105 c->ostate = next;
106}
107
Damien Miller33b13562000-04-04 14:38:59 +1000108/*
109 * SSH1 specific implementation of event functions
110 */
111
112static void
113chan_rcvd_oclose1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100114{
Damien Miller33b13562000-04-04 14:38:59 +1000115 debug("channel %d: rcvd oclose", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100116 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000117 case CHAN_INPUT_WAIT_OCLOSE:
Damien Millerabea8ee2002-01-22 23:27:11 +1100118 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000119 break;
120 case CHAN_INPUT_OPEN:
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000121 chan_shutdown_read(c);
Damien Miller33b13562000-04-04 14:38:59 +1000122 chan_send_ieof1(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100123 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Miller34132e52000-01-14 15:45:46 +1100124 break;
125 case CHAN_INPUT_WAIT_DRAIN:
126 /* both local read_failed and remote write_failed */
Damien Miller33b13562000-04-04 14:38:59 +1000127 chan_send_ieof1(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100128 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000129 break;
130 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000131 error("channel %d: protocol error: rcvd_oclose for istate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000132 c->self, c->istate);
Damien Miller34132e52000-01-14 15:45:46 +1100133 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000134 }
135}
Damien Miller33b13562000-04-04 14:38:59 +1000136static void
137chan_read_failed_12(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100138{
Damien Miller33b13562000-04-04 14:38:59 +1000139 debug("channel %d: read failed", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100140 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000141 case CHAN_INPUT_OPEN:
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000142 chan_shutdown_read(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100143 chan_set_istate(c, CHAN_INPUT_WAIT_DRAIN);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000144 break;
145 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000146 error("channel %d: chan_read_failed for istate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000147 c->self, c->istate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000148 break;
149 }
150}
Damien Miller33b13562000-04-04 14:38:59 +1000151static void
152chan_ibuf_empty1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100153{
Damien Miller33b13562000-04-04 14:38:59 +1000154 debug("channel %d: ibuf empty", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100155 if (buffer_len(&c->input)) {
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000156 error("channel %d: chan_ibuf_empty for non empty buffer",
Damien Miller33b13562000-04-04 14:38:59 +1000157 c->self);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000158 return;
159 }
Damien Miller95def091999-11-25 00:26:21 +1100160 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000161 case CHAN_INPUT_WAIT_DRAIN:
Damien Miller33b13562000-04-04 14:38:59 +1000162 chan_send_ieof1(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100163 chan_set_istate(c, CHAN_INPUT_WAIT_OCLOSE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000164 break;
165 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000166 error("channel %d: chan_ibuf_empty for istate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000167 c->self, c->istate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000168 break;
169 }
170}
Damien Miller33b13562000-04-04 14:38:59 +1000171static void
172chan_rcvd_ieof1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100173{
Damien Miller33b13562000-04-04 14:38:59 +1000174 debug("channel %d: rcvd ieof", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100175 switch (c->ostate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000176 case CHAN_OUTPUT_OPEN:
Damien Millerabea8ee2002-01-22 23:27:11 +1100177 chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000178 break;
179 case CHAN_OUTPUT_WAIT_IEOF:
Damien Millerabea8ee2002-01-22 23:27:11 +1100180 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000181 break;
182 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000183 error("channel %d: protocol error: rcvd_ieof for ostate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000184 c->self, c->ostate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000185 break;
186 }
187}
Damien Miller33b13562000-04-04 14:38:59 +1000188static void
189chan_write_failed1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100190{
Damien Miller33b13562000-04-04 14:38:59 +1000191 debug("channel %d: write failed", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100192 switch (c->ostate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000193 case CHAN_OUTPUT_OPEN:
Damien Miller89a92322002-01-22 23:27:28 +1100194 chan_shutdown_write(c);
Damien Miller33b13562000-04-04 14:38:59 +1000195 chan_send_oclose1(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100196 chan_set_ostate(c, CHAN_OUTPUT_WAIT_IEOF);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000197 break;
198 case CHAN_OUTPUT_WAIT_DRAIN:
Damien Miller89a92322002-01-22 23:27:28 +1100199 chan_shutdown_write(c);
Damien Miller33b13562000-04-04 14:38:59 +1000200 chan_send_oclose1(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100201 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000202 break;
203 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000204 error("channel %d: chan_write_failed for ostate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000205 c->self, c->ostate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000206 break;
207 }
208}
Damien Miller33b13562000-04-04 14:38:59 +1000209static void
210chan_obuf_empty1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100211{
Damien Miller33b13562000-04-04 14:38:59 +1000212 debug("channel %d: obuf empty", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100213 if (buffer_len(&c->output)) {
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000214 error("channel %d: chan_obuf_empty for non empty buffer",
Damien Miller33b13562000-04-04 14:38:59 +1000215 c->self);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000216 return;
217 }
Damien Miller95def091999-11-25 00:26:21 +1100218 switch (c->ostate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000219 case CHAN_OUTPUT_WAIT_DRAIN:
Damien Miller89a92322002-01-22 23:27:28 +1100220 chan_shutdown_write(c);
Damien Miller33b13562000-04-04 14:38:59 +1000221 chan_send_oclose1(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100222 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000223 break;
224 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000225 error("channel %d: internal error: obuf_empty for ostate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000226 c->self, c->ostate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000227 break;
228 }
229}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000230static void
Damien Miller33b13562000-04-04 14:38:59 +1000231chan_send_ieof1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100232{
Damien Miller33b13562000-04-04 14:38:59 +1000233 debug("channel %d: send ieof", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100234 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000235 case CHAN_INPUT_OPEN:
236 case CHAN_INPUT_WAIT_DRAIN:
237 packet_start(SSH_MSG_CHANNEL_INPUT_EOF);
238 packet_put_int(c->remote_id);
239 packet_send();
240 break;
241 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000242 error("channel %d: cannot send ieof for istate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000243 c->self, c->istate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000244 break;
245 }
246}
247static void
Damien Miller33b13562000-04-04 14:38:59 +1000248chan_send_oclose1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100249{
Damien Miller33b13562000-04-04 14:38:59 +1000250 debug("channel %d: send oclose", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100251 switch (c->ostate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000252 case CHAN_OUTPUT_OPEN:
253 case CHAN_OUTPUT_WAIT_DRAIN:
Damien Miller76765c02002-01-22 23:21:15 +1100254 buffer_clear(&c->output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000255 packet_start(SSH_MSG_CHANNEL_OUTPUT_CLOSE);
256 packet_put_int(c->remote_id);
257 packet_send();
258 break;
259 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000260 error("channel %d: cannot send oclose for ostate %d",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100261 c->self, c->ostate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000262 break;
263 }
264}
Damien Miller33b13562000-04-04 14:38:59 +1000265
266/*
267 * the same for SSH2
268 */
269static void
270chan_rcvd_oclose2(Channel *c)
271{
272 debug("channel %d: rcvd close", c->self);
273 if (c->flags & CHAN_CLOSE_RCVD)
274 error("channel %d: protocol error: close rcvd twice", c->self);
275 c->flags |= CHAN_CLOSE_RCVD;
276 if (c->type == SSH_CHANNEL_LARVAL) {
277 /* tear down larval channels immediately */
Damien Millerabea8ee2002-01-22 23:27:11 +1100278 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
279 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Miller33b13562000-04-04 14:38:59 +1000280 return;
281 }
282 switch (c->ostate) {
283 case CHAN_OUTPUT_OPEN:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000284 /*
285 * wait until a data from the channel is consumed if a CLOSE
286 * is received
287 */
Damien Millerabea8ee2002-01-22 23:27:11 +1100288 chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
Damien Miller33b13562000-04-04 14:38:59 +1000289 break;
290 }
291 switch (c->istate) {
292 case CHAN_INPUT_OPEN:
Damien Miller33b13562000-04-04 14:38:59 +1000293 chan_shutdown_read(c);
294 break;
295 case CHAN_INPUT_WAIT_DRAIN:
Damien Miller33b13562000-04-04 14:38:59 +1000296 chan_send_eof2(c);
297 break;
298 }
Damien Millerabea8ee2002-01-22 23:27:11 +1100299 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Miller33b13562000-04-04 14:38:59 +1000300}
301static void
302chan_ibuf_empty2(Channel *c)
303{
304 debug("channel %d: ibuf empty", c->self);
305 if (buffer_len(&c->input)) {
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000306 error("channel %d: chan_ibuf_empty for non empty buffer",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100307 c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000308 return;
309 }
310 switch (c->istate) {
311 case CHAN_INPUT_WAIT_DRAIN:
Damien Miller33b13562000-04-04 14:38:59 +1000312 if (!(c->flags & CHAN_CLOSE_SENT))
313 chan_send_eof2(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100314 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Miller33b13562000-04-04 14:38:59 +1000315 break;
316 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000317 error("channel %d: chan_ibuf_empty for istate %d",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100318 c->self, c->istate);
Damien Miller33b13562000-04-04 14:38:59 +1000319 break;
320 }
321}
322static void
323chan_rcvd_ieof2(Channel *c)
324{
325 debug("channel %d: rcvd eof", c->self);
Damien Millerabea8ee2002-01-22 23:27:11 +1100326 if (c->ostate == CHAN_OUTPUT_OPEN)
327 chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
Damien Miller33b13562000-04-04 14:38:59 +1000328}
329static void
330chan_write_failed2(Channel *c)
331{
332 debug("channel %d: write failed", c->self);
333 switch (c->ostate) {
334 case CHAN_OUTPUT_OPEN:
Damien Millere247cc42000-05-07 12:03:14 +1000335 chan_shutdown_write(c); /* ?? */
Damien Millerabea8ee2002-01-22 23:27:11 +1100336 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
Damien Miller33b13562000-04-04 14:38:59 +1000337 break;
338 case CHAN_OUTPUT_WAIT_DRAIN:
Damien Miller33b13562000-04-04 14:38:59 +1000339 chan_shutdown_write(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100340 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
Damien Miller33b13562000-04-04 14:38:59 +1000341 break;
342 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000343 error("channel %d: chan_write_failed for ostate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000344 c->self, c->ostate);
345 break;
346 }
347}
348static void
349chan_obuf_empty2(Channel *c)
350{
351 debug("channel %d: obuf empty", c->self);
352 if (buffer_len(&c->output)) {
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000353 error("channel %d: chan_obuf_empty for non empty buffer",
Damien Miller33b13562000-04-04 14:38:59 +1000354 c->self);
355 return;
356 }
357 switch (c->ostate) {
358 case CHAN_OUTPUT_WAIT_DRAIN:
Damien Miller33b13562000-04-04 14:38:59 +1000359 chan_shutdown_write(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100360 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
Damien Miller33b13562000-04-04 14:38:59 +1000361 break;
362 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000363 error("channel %d: chan_obuf_empty for ostate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000364 c->self, c->ostate);
365 break;
366 }
367}
368static void
369chan_send_eof2(Channel *c)
370{
371 debug("channel %d: send eof", c->self);
372 switch (c->istate) {
373 case CHAN_INPUT_WAIT_DRAIN:
374 packet_start(SSH2_MSG_CHANNEL_EOF);
375 packet_put_int(c->remote_id);
376 packet_send();
377 break;
378 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000379 error("channel %d: cannot send eof for istate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000380 c->self, c->istate);
381 break;
382 }
383}
384static void
385chan_send_close2(Channel *c)
386{
387 debug("channel %d: send close", c->self);
388 if (c->ostate != CHAN_OUTPUT_CLOSED ||
389 c->istate != CHAN_INPUT_CLOSED) {
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000390 error("channel %d: cannot send close for istate/ostate %d/%d",
Damien Miller33b13562000-04-04 14:38:59 +1000391 c->self, c->istate, c->ostate);
392 } else if (c->flags & CHAN_CLOSE_SENT) {
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000393 error("channel %d: already sent close", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000394 } else {
395 packet_start(SSH2_MSG_CHANNEL_CLOSE);
396 packet_put_int(c->remote_id);
397 packet_send();
398 c->flags |= CHAN_CLOSE_SENT;
399 }
400}
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000401
402/* shared */
403
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000404void
405chan_mark_dead(Channel *c)
406{
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000407 c->type = SSH_CHANNEL_ZOMBIE;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000408}
409
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000410int
Damien Miller3ec27592001-10-12 11:35:04 +1000411chan_is_dead(Channel *c, int send)
Damien Miller33b13562000-04-04 14:38:59 +1000412{
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000413 if (c->type == SSH_CHANNEL_ZOMBIE) {
414 debug("channel %d: zombie", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000415 return 1;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000416 }
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000417 if (c->istate != CHAN_INPUT_CLOSED || c->ostate != CHAN_OUTPUT_CLOSED)
418 return 0;
419 if (!compat20) {
420 debug("channel %d: is dead", c->self);
421 return 1;
422 }
423 /*
424 * we have to delay the close message if the efd (for stderr) is
425 * still active
426 */
427 if (((c->extended_usage != CHAN_EXTENDED_IGNORE) &&
428 buffer_len(&c->extended) > 0)
429#if 0
430 || ((c->extended_usage == CHAN_EXTENDED_READ) &&
431 c->efd != -1)
432#endif
433 ) {
434 debug2("channel %d: active efd: %d len %d type %s",
435 c->self, c->efd, buffer_len(&c->extended),
436 c->extended_usage==CHAN_EXTENDED_READ ?
Damien Miller9f0f5c62001-12-21 14:45:46 +1100437 "read": "write");
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000438 } else {
Damien Miller33b13562000-04-04 14:38:59 +1000439 if (!(c->flags & CHAN_CLOSE_SENT)) {
Damien Miller3ec27592001-10-12 11:35:04 +1000440 if (send) {
441 chan_send_close2(c);
442 } else {
443 /* channel would be dead if we sent a close */
444 if (c->flags & CHAN_CLOSE_RCVD) {
445 debug("channel %d: almost dead",
446 c->self);
447 return 1;
448 }
449 }
Damien Miller33b13562000-04-04 14:38:59 +1000450 }
Damien Miller4af51302000-04-16 11:18:38 +1000451 if ((c->flags & CHAN_CLOSE_SENT) &&
Damien Miller33b13562000-04-04 14:38:59 +1000452 (c->flags & CHAN_CLOSE_RCVD)) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000453 debug("channel %d: is dead", c->self);
454 return 1;
Damien Miller4af51302000-04-16 11:18:38 +1000455 }
Damien Miller33b13562000-04-04 14:38:59 +1000456 }
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000457 return 0;
Damien Miller33b13562000-04-04 14:38:59 +1000458}
459
Damien Miller33b13562000-04-04 14:38:59 +1000460void
461chan_init_iostates(Channel *c)
462{
463 c->ostate = CHAN_OUTPUT_OPEN;
464 c->istate = CHAN_INPUT_OPEN;
465 c->flags = 0;
466}
467
468/* init */
469void
470chan_init(void)
471{
472 if (compat20) {
473 chan_rcvd_oclose = chan_rcvd_oclose2;
474 chan_read_failed = chan_read_failed_12;
475 chan_ibuf_empty = chan_ibuf_empty2;
476
477 chan_rcvd_ieof = chan_rcvd_ieof2;
478 chan_write_failed = chan_write_failed2;
479 chan_obuf_empty = chan_obuf_empty2;
Damien Miller33b13562000-04-04 14:38:59 +1000480 } else {
481 chan_rcvd_oclose = chan_rcvd_oclose1;
482 chan_read_failed = chan_read_failed_12;
483 chan_ibuf_empty = chan_ibuf_empty1;
484
485 chan_rcvd_ieof = chan_rcvd_ieof1;
486 chan_write_failed = chan_write_failed1;
487 chan_obuf_empty = chan_obuf_empty1;
Damien Miller33b13562000-04-04 14:38:59 +1000488 }
489}
Damien Miller95def091999-11-25 00:26:21 +1100490
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000491/* helper */
492static void
Damien Miller95def091999-11-25 00:26:21 +1100493chan_shutdown_write(Channel *c)
494{
Damien Miller76765c02002-01-22 23:21:15 +1100495 buffer_clear(&c->output);
Damien Miller33b13562000-04-04 14:38:59 +1000496 if (compat20 && c->type == SSH_CHANNEL_LARVAL)
497 return;
Damien Miller5428f641999-11-25 11:54:57 +1100498 /* shutdown failure is allowed if write failed already */
Damien Miller33b13562000-04-04 14:38:59 +1000499 debug("channel %d: close_write", c->self);
500 if (c->sock != -1) {
501 if (shutdown(c->sock, SHUT_WR) < 0)
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000502 debug("channel %d: chan_shutdown_write: "
503 "shutdown() failed for fd%d: %.100s",
Damien Miller33b13562000-04-04 14:38:59 +1000504 c->self, c->sock, strerror(errno));
505 } else {
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000506 if (channel_close_fd(&c->wfd) < 0)
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000507 log("channel %d: chan_shutdown_write: "
508 "close() failed for fd%d: %.100s",
Damien Miller33b13562000-04-04 14:38:59 +1000509 c->self, c->wfd, strerror(errno));
Damien Miller33b13562000-04-04 14:38:59 +1000510 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000511}
512static void
Damien Miller95def091999-11-25 00:26:21 +1100513chan_shutdown_read(Channel *c)
514{
Damien Miller33b13562000-04-04 14:38:59 +1000515 if (compat20 && c->type == SSH_CHANNEL_LARVAL)
516 return;
517 debug("channel %d: close_read", c->self);
518 if (c->sock != -1) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000519 /*
Damien Miller0f091bd2000-08-07 15:47:48 +1000520 * shutdown(sock, SHUT_READ) may return ENOTCONN if the
521 * write side has been closed already. (bug on Linux)
Kevin Steves871f6622001-09-18 16:08:24 +0000522 * HP-UX may return ENOTCONN also.
Damien Miller0f091bd2000-08-07 15:47:48 +1000523 */
524 if (shutdown(c->sock, SHUT_RD) < 0
Kevin Steves871f6622001-09-18 16:08:24 +0000525 && errno != ENOTCONN)
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000526 error("channel %d: chan_shutdown_read: "
527 "shutdown() failed for fd%d [i%d o%d]: %.100s",
528 c->self, c->sock, c->istate, c->ostate,
Kevin Stevesc3422eb2001-09-20 19:33:33 +0000529 strerror(errno));
Damien Miller33b13562000-04-04 14:38:59 +1000530 } else {
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000531 if (channel_close_fd(&c->rfd) < 0)
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000532 log("channel %d: chan_shutdown_read: "
533 "close() failed for fd%d: %.100s",
Damien Miller33b13562000-04-04 14:38:59 +1000534 c->self, c->rfd, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000535 }
536}