2 * Copyright (c) 1999 Markus Friedl. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 RCSID("$OpenBSD: nchan.c,v 1.23 2001/02/28 08:54:55 markus Exp $");
37 /* functions manipulating channel states */
39 * EVENTS update channel input/output states execute ACTIONS
41 /* events concerning the INPUT from socket for channel (istate) */
42 chan_event_fn *chan_rcvd_oclose = NULL;
43 chan_event_fn *chan_read_failed = NULL;
44 chan_event_fn *chan_ibuf_empty = NULL;
45 /* events concerning the OUTPUT from channel for socket (ostate) */
46 chan_event_fn *chan_rcvd_ieof = NULL;
47 chan_event_fn *chan_write_failed = NULL;
48 chan_event_fn *chan_obuf_empty = NULL;
50 * ACTIONS: should never update the channel states
52 static void chan_send_ieof1(Channel *c);
53 static void chan_send_oclose1(Channel *c);
54 static void chan_send_close2(Channel *c);
55 static void chan_send_eof2(Channel *c);
58 static void chan_shutdown_write(Channel *c);
59 static void chan_shutdown_read(Channel *c);
62 * SSH1 specific implementation of event functions
66 chan_rcvd_oclose1(Channel *c)
68 debug("channel %d: rcvd oclose", c->self);
70 case CHAN_INPUT_WAIT_OCLOSE:
71 debug("channel %d: input wait_oclose -> closed", c->self);
72 c->istate = CHAN_INPUT_CLOSED;
75 debug("channel %d: input open -> closed", c->self);
76 chan_shutdown_read(c);
78 c->istate = CHAN_INPUT_CLOSED;
80 case CHAN_INPUT_WAIT_DRAIN:
81 /* both local read_failed and remote write_failed */
82 log("channel %d: input drain -> closed", c->self);
84 c->istate = CHAN_INPUT_CLOSED;
87 error("channel %d: protocol error: chan_rcvd_oclose for istate %d",
93 chan_read_failed_12(Channel *c)
95 debug("channel %d: read failed", c->self);
98 debug("channel %d: input open -> drain", c->self);
99 chan_shutdown_read(c);
100 c->istate = CHAN_INPUT_WAIT_DRAIN;
101 if (buffer_len(&c->input) == 0) {
102 debug("channel %d: input: no drain shortcut", c->self);
107 error("channel %d: internal error: we do not read, but chan_read_failed for istate %d",
113 chan_ibuf_empty1(Channel *c)
115 debug("channel %d: ibuf empty", c->self);
116 if (buffer_len(&c->input)) {
117 error("channel %d: internal error: chan_ibuf_empty for non empty buffer",
122 case CHAN_INPUT_WAIT_DRAIN:
123 debug("channel %d: input drain -> wait_oclose", c->self);
125 c->istate = CHAN_INPUT_WAIT_OCLOSE;
128 error("channel %d: internal error: chan_ibuf_empty for istate %d",
134 chan_rcvd_ieof1(Channel *c)
136 debug("channel %d: rcvd ieof", c->self);
137 if (c->type != SSH_CHANNEL_OPEN) {
138 debug("channel %d: non-open", c->self);
139 if (c->istate == CHAN_INPUT_OPEN) {
140 debug("channel %d: non-open: input open -> wait_oclose", c->self);
141 chan_shutdown_read(c);
143 c->istate = CHAN_INPUT_WAIT_OCLOSE;
145 error("channel %d: istate %d != open", c->self, c->istate);
147 if (c->ostate == CHAN_OUTPUT_OPEN) {
148 debug("channel %d: non-open: output open -> closed", c->self);
149 chan_send_oclose1(c);
150 c->ostate = CHAN_OUTPUT_CLOSED;
152 error("channel %d: ostate %d != open", c->self, c->ostate);
157 case CHAN_OUTPUT_OPEN:
158 debug("channel %d: output open -> drain", c->self);
159 c->ostate = CHAN_OUTPUT_WAIT_DRAIN;
161 case CHAN_OUTPUT_WAIT_IEOF:
162 debug("channel %d: output wait_ieof -> closed", c->self);
163 c->ostate = CHAN_OUTPUT_CLOSED;
166 error("channel %d: protocol error: chan_rcvd_ieof for ostate %d",
172 chan_write_failed1(Channel *c)
174 debug("channel %d: write failed", c->self);
176 case CHAN_OUTPUT_OPEN:
177 debug("channel %d: output open -> wait_ieof", c->self);
178 chan_send_oclose1(c);
179 c->ostate = CHAN_OUTPUT_WAIT_IEOF;
181 case CHAN_OUTPUT_WAIT_DRAIN:
182 debug("channel %d: output wait_drain -> closed", c->self);
183 chan_send_oclose1(c);
184 c->ostate = CHAN_OUTPUT_CLOSED;
187 error("channel %d: internal error: chan_write_failed for ostate %d",
193 chan_obuf_empty1(Channel *c)
195 debug("channel %d: obuf empty", c->self);
196 if (buffer_len(&c->output)) {
197 error("channel %d: internal error: chan_obuf_empty for non empty buffer",
202 case CHAN_OUTPUT_WAIT_DRAIN:
203 debug("channel %d: output drain -> closed", c->self);
204 chan_send_oclose1(c);
205 c->ostate = CHAN_OUTPUT_CLOSED;
208 error("channel %d: internal error: chan_obuf_empty for ostate %d",
214 chan_send_ieof1(Channel *c)
216 debug("channel %d: send ieof", c->self);
218 case CHAN_INPUT_OPEN:
219 case CHAN_INPUT_WAIT_DRAIN:
220 packet_start(SSH_MSG_CHANNEL_INPUT_EOF);
221 packet_put_int(c->remote_id);
225 error("channel %d: internal error: cannot send ieof for istate %d",
231 chan_send_oclose1(Channel *c)
233 debug("channel %d: send oclose", c->self);
235 case CHAN_OUTPUT_OPEN:
236 case CHAN_OUTPUT_WAIT_DRAIN:
237 chan_shutdown_write(c);
238 buffer_consume(&c->output, buffer_len(&c->output));
239 packet_start(SSH_MSG_CHANNEL_OUTPUT_CLOSE);
240 packet_put_int(c->remote_id);
244 error("channel %d: internal error: cannot send oclose for ostate %d",
254 chan_rcvd_oclose2(Channel *c)
256 debug("channel %d: rcvd close", c->self);
257 if (c->flags & CHAN_CLOSE_RCVD)
258 error("channel %d: protocol error: close rcvd twice", c->self);
259 c->flags |= CHAN_CLOSE_RCVD;
260 if (c->type == SSH_CHANNEL_LARVAL) {
261 /* tear down larval channels immediately */
262 c->ostate = CHAN_OUTPUT_CLOSED;
263 c->istate = CHAN_INPUT_CLOSED;
267 case CHAN_OUTPUT_OPEN:
268 /* wait until a data from the channel is consumed if a CLOSE is received */
269 debug("channel %d: output open -> drain", c->self);
270 c->ostate = CHAN_OUTPUT_WAIT_DRAIN;
274 case CHAN_INPUT_OPEN:
275 debug("channel %d: input open -> closed", c->self);
276 chan_shutdown_read(c);
278 case CHAN_INPUT_WAIT_DRAIN:
279 debug("channel %d: input drain -> closed", c->self);
283 c->istate = CHAN_INPUT_CLOSED;
286 chan_ibuf_empty2(Channel *c)
288 debug("channel %d: ibuf empty", c->self);
289 if (buffer_len(&c->input)) {
290 error("channel %d: internal error: chan_ibuf_empty for non empty buffer",
295 case CHAN_INPUT_WAIT_DRAIN:
296 debug("channel %d: input drain -> closed", c->self);
297 if (!(c->flags & CHAN_CLOSE_SENT))
299 c->istate = CHAN_INPUT_CLOSED;
302 error("channel %d: internal error: chan_ibuf_empty for istate %d",
308 chan_rcvd_ieof2(Channel *c)
310 debug("channel %d: rcvd eof", c->self);
311 if (c->ostate == CHAN_OUTPUT_OPEN) {
312 debug("channel %d: output open -> drain", c->self);
313 c->ostate = CHAN_OUTPUT_WAIT_DRAIN;
317 chan_write_failed2(Channel *c)
319 debug("channel %d: write failed", c->self);
321 case CHAN_OUTPUT_OPEN:
322 debug("channel %d: output open -> closed", c->self);
323 chan_shutdown_write(c); /* ?? */
324 c->ostate = CHAN_OUTPUT_CLOSED;
326 case CHAN_OUTPUT_WAIT_DRAIN:
327 debug("channel %d: output drain -> closed", c->self);
328 chan_shutdown_write(c);
329 c->ostate = CHAN_OUTPUT_CLOSED;
332 error("channel %d: internal error: chan_write_failed for ostate %d",
338 chan_obuf_empty2(Channel *c)
340 debug("channel %d: obuf empty", c->self);
341 if (buffer_len(&c->output)) {
342 error("internal error: chan_obuf_empty %d for non empty buffer",
347 case CHAN_OUTPUT_WAIT_DRAIN:
348 debug("channel %d: output drain -> closed", c->self);
349 chan_shutdown_write(c);
350 c->ostate = CHAN_OUTPUT_CLOSED;
353 error("channel %d: internal error: chan_obuf_empty for ostate %d",
359 chan_send_eof2(Channel *c)
361 debug("channel %d: send eof", c->self);
363 case CHAN_INPUT_WAIT_DRAIN:
364 packet_start(SSH2_MSG_CHANNEL_EOF);
365 packet_put_int(c->remote_id);
369 error("channel %d: internal error: cannot send eof for istate %d",
375 chan_send_close2(Channel *c)
377 debug("channel %d: send close", c->self);
378 if (c->ostate != CHAN_OUTPUT_CLOSED ||
379 c->istate != CHAN_INPUT_CLOSED) {
380 error("channel %d: internal error: cannot send close for istate/ostate %d/%d",
381 c->self, c->istate, c->ostate);
382 } else if (c->flags & CHAN_CLOSE_SENT) {
383 error("channel %d: internal error: already sent close", c->self);
385 packet_start(SSH2_MSG_CHANNEL_CLOSE);
386 packet_put_int(c->remote_id);
388 c->flags |= CHAN_CLOSE_SENT;
395 chan_is_dead(Channel *c)
397 if (c->istate != CHAN_INPUT_CLOSED || c->ostate != CHAN_OUTPUT_CLOSED)
400 debug("channel %d: is dead", c->self);
404 * we have to delay the close message if the efd (for stderr) is
407 if (((c->extended_usage != CHAN_EXTENDED_IGNORE) &&
408 buffer_len(&c->extended) > 0)
410 || ((c->extended_usage == CHAN_EXTENDED_READ) &&
414 debug2("channel %d: active efd: %d len %d type %s",
415 c->self, c->efd, buffer_len(&c->extended),
416 c->extended_usage==CHAN_EXTENDED_READ ?
419 if (!(c->flags & CHAN_CLOSE_SENT)) {
422 if ((c->flags & CHAN_CLOSE_SENT) &&
423 (c->flags & CHAN_CLOSE_RCVD)) {
424 debug("channel %d: is dead", c->self);
432 chan_init_iostates(Channel *c)
434 c->ostate = CHAN_OUTPUT_OPEN;
435 c->istate = CHAN_INPUT_OPEN;
444 chan_rcvd_oclose = chan_rcvd_oclose2;
445 chan_read_failed = chan_read_failed_12;
446 chan_ibuf_empty = chan_ibuf_empty2;
448 chan_rcvd_ieof = chan_rcvd_ieof2;
449 chan_write_failed = chan_write_failed2;
450 chan_obuf_empty = chan_obuf_empty2;
452 chan_rcvd_oclose = chan_rcvd_oclose1;
453 chan_read_failed = chan_read_failed_12;
454 chan_ibuf_empty = chan_ibuf_empty1;
456 chan_rcvd_ieof = chan_rcvd_ieof1;
457 chan_write_failed = chan_write_failed1;
458 chan_obuf_empty = chan_obuf_empty1;
464 chan_shutdown_write(Channel *c)
466 buffer_consume(&c->output, buffer_len(&c->output));
467 if (compat20 && c->type == SSH_CHANNEL_LARVAL)
469 /* shutdown failure is allowed if write failed already */
470 debug("channel %d: close_write", c->self);
472 if (shutdown(c->sock, SHUT_WR) < 0)
473 debug("channel %d: chan_shutdown_write: shutdown() failed for fd%d: %.100s",
474 c->self, c->sock, strerror(errno));
476 if (close(c->wfd) < 0)
477 log("channel %d: chan_shutdown_write: close() failed for fd%d: %.100s",
478 c->self, c->wfd, strerror(errno));
483 chan_shutdown_read(Channel *c)
485 if (compat20 && c->type == SSH_CHANNEL_LARVAL)
487 debug("channel %d: close_read", c->self);
490 * shutdown(sock, SHUT_READ) may return ENOTCONN if the
491 * write side has been closed already. (bug on Linux)
493 if (shutdown(c->sock, SHUT_RD) < 0
494 && (errno != ENOTCONN
495 || c->ostate == CHAN_OUTPUT_OPEN
496 || c->ostate == CHAN_OUTPUT_WAIT_DRAIN))
497 error("channel %d: chan_shutdown_read: shutdown() failed for fd%d [i%d o%d]: %.100s",
498 c->self, c->sock, c->istate, c->ostate, strerror(errno));
500 if (close(c->rfd) < 0)
501 log("channel %d: chan_shutdown_read: close() failed for fd%d: %.100s",
502 c->self, c->rfd, strerror(errno));