blob: 0c8066f87cd9e9a491e9e9cb928d835730e3e66e [file] [log] [blame]
Damien Miller5428f641999-11-25 11:54:57 +11001/*
2 * Copyright (c) 1999 Markus Friedl. All rights reserved.
3 *
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.
12 * 3. All advertising materials mentioning features or use of this software
13 * must display the following acknowledgement:
14 * This product includes software developed by Markus Friedl.
15 * 4. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
Damien Millerd4a8b7e1999-10-27 13:42:43 +100030#include "includes.h"
Damien Miller34132e52000-01-14 15:45:46 +110031RCSID("$Id: nchan.c,v 1.5 2000/01/14 04:45:50 damien Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100032
33#include "ssh.h"
34
35#include "buffer.h"
36#include "packet.h"
37#include "channels.h"
38#include "nchan.h"
39
40static void chan_send_ieof(Channel *c);
41static void chan_send_oclose(Channel *c);
42static void chan_shutdown_write(Channel *c);
43static void chan_shutdown_read(Channel *c);
Damien Miller34132e52000-01-14 15:45:46 +110044static void chan_delete_if_full_closed(Channel *c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100045
46/*
Damien Miller95def091999-11-25 00:26:21 +110047 * EVENTS update channel input/output states execute ACTIONS
Damien Millerd4a8b7e1999-10-27 13:42:43 +100048 */
Damien Miller95def091999-11-25 00:26:21 +110049
Damien Millerd4a8b7e1999-10-27 13:42:43 +100050/* events concerning the INPUT from socket for channel (istate) */
51void
Damien Miller95def091999-11-25 00:26:21 +110052chan_rcvd_oclose(Channel *c)
53{
54 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +100055 case CHAN_INPUT_WAIT_OCLOSE:
56 debug("channel %d: INPUT_WAIT_OCLOSE -> INPUT_CLOSED [rcvd OCLOSE]", c->self);
Damien Miller95def091999-11-25 00:26:21 +110057 c->istate = CHAN_INPUT_CLOSED;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100058 break;
59 case CHAN_INPUT_OPEN:
60 debug("channel %d: INPUT_OPEN -> INPUT_CLOSED [rvcd OCLOSE, send IEOF]", c->self);
61 chan_shutdown_read(c);
62 chan_send_ieof(c);
Damien Miller95def091999-11-25 00:26:21 +110063 c->istate = CHAN_INPUT_CLOSED;
Damien Miller34132e52000-01-14 15:45:46 +110064 break;
65 case CHAN_INPUT_WAIT_DRAIN:
66 /* both local read_failed and remote write_failed */
67 log("channel %d: INPUT_WAIT_DRAIN -> INPUT_CLOSED [rvcd OCLOSE, send IEOF]", c->self);
68 debug("channel %d: INPUT_WAIT_DRAIN -> INPUT_CLOSED [rvcd OCLOSE, send IEOF]", c->self);
69 chan_send_ieof(c);
70 c->istate = CHAN_INPUT_CLOSED;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100071 break;
72 default:
Damien Milleraae6c611999-12-06 11:47:28 +110073 error("protocol error: chan_rcvd_oclose %d for istate %d", c->self, c->istate);
Damien Miller34132e52000-01-14 15:45:46 +110074 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100075 }
Damien Miller34132e52000-01-14 15:45:46 +110076 chan_delete_if_full_closed(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100077}
78void
Damien Miller95def091999-11-25 00:26:21 +110079chan_read_failed(Channel *c)
80{
81 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +100082 case CHAN_INPUT_OPEN:
83 debug("channel %d: INPUT_OPEN -> INPUT_WAIT_DRAIN [read failed]", c->self);
84 chan_shutdown_read(c);
Damien Miller95def091999-11-25 00:26:21 +110085 c->istate = CHAN_INPUT_WAIT_DRAIN;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100086 break;
87 default:
Damien Milleraae6c611999-12-06 11:47:28 +110088 error("internal error: we do not read, but chan_read_failed %d for istate %d",
Damien Miller95def091999-11-25 00:26:21 +110089 c->self, c->istate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100090 break;
91 }
92}
93void
Damien Miller95def091999-11-25 00:26:21 +110094chan_ibuf_empty(Channel *c)
95{
96 if (buffer_len(&c->input)) {
Damien Milleraae6c611999-12-06 11:47:28 +110097 error("internal error: chan_ibuf_empty %d for non empty buffer", c->self);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098 return;
99 }
Damien Miller95def091999-11-25 00:26:21 +1100100 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000101 case CHAN_INPUT_WAIT_DRAIN:
102 debug("channel %d: INPUT_WAIT_DRAIN -> INPUT_WAIT_OCLOSE [inbuf empty, send IEOF]", c->self);
103 chan_send_ieof(c);
Damien Miller95def091999-11-25 00:26:21 +1100104 c->istate = CHAN_INPUT_WAIT_OCLOSE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000105 break;
106 default:
Damien Milleraae6c611999-12-06 11:47:28 +1100107 error("internal error: chan_ibuf_empty %d for istate %d", c->self, c->istate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000108 break;
109 }
110}
Damien Miller95def091999-11-25 00:26:21 +1100111
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000112/* events concerning the OUTPUT from channel for socket (ostate) */
113void
Damien Miller95def091999-11-25 00:26:21 +1100114chan_rcvd_ieof(Channel *c)
115{
116 switch (c->ostate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000117 case CHAN_OUTPUT_OPEN:
118 debug("channel %d: OUTPUT_OPEN -> OUTPUT_WAIT_DRAIN [rvcd IEOF]", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100119 c->ostate = CHAN_OUTPUT_WAIT_DRAIN;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000120 break;
121 case CHAN_OUTPUT_WAIT_IEOF:
122 debug("channel %d: OUTPUT_WAIT_IEOF -> OUTPUT_CLOSED [rvcd IEOF]", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100123 c->ostate = CHAN_OUTPUT_CLOSED;
Damien Miller34132e52000-01-14 15:45:46 +1100124 chan_delete_if_full_closed(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000125 break;
126 default:
Damien Milleraae6c611999-12-06 11:47:28 +1100127 error("protocol error: chan_rcvd_ieof %d for ostate %d", c->self, c->ostate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000128 break;
129 }
130}
131void
Damien Miller95def091999-11-25 00:26:21 +1100132chan_write_failed(Channel *c)
133{
134 switch (c->ostate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000135 case CHAN_OUTPUT_OPEN:
136 debug("channel %d: OUTPUT_OPEN -> OUTPUT_WAIT_IEOF [write failed]", c->self);
137 chan_send_oclose(c);
Damien Miller95def091999-11-25 00:26:21 +1100138 c->ostate = CHAN_OUTPUT_WAIT_IEOF;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000139 break;
140 case CHAN_OUTPUT_WAIT_DRAIN:
141 debug("channel %d: OUTPUT_WAIT_DRAIN -> OUTPUT_CLOSED [write failed]", c->self);
142 chan_send_oclose(c);
Damien Miller95def091999-11-25 00:26:21 +1100143 c->ostate = CHAN_OUTPUT_CLOSED;
Damien Miller34132e52000-01-14 15:45:46 +1100144 chan_delete_if_full_closed(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000145 break;
146 default:
Damien Milleraae6c611999-12-06 11:47:28 +1100147 error("internal error: chan_write_failed %d for ostate %d", c->self, c->ostate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000148 break;
149 }
150}
151void
Damien Miller95def091999-11-25 00:26:21 +1100152chan_obuf_empty(Channel *c)
153{
154 if (buffer_len(&c->output)) {
155 debug("internal error: chan_obuf_empty %d for non empty buffer", c->self);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000156 return;
157 }
Damien Miller95def091999-11-25 00:26:21 +1100158 switch (c->ostate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000159 case CHAN_OUTPUT_WAIT_DRAIN:
160 debug("channel %d: OUTPUT_WAIT_DRAIN -> OUTPUT_CLOSED [obuf empty, send OCLOSE]", c->self);
161 chan_send_oclose(c);
Damien Miller95def091999-11-25 00:26:21 +1100162 c->ostate = CHAN_OUTPUT_CLOSED;
Damien Miller34132e52000-01-14 15:45:46 +1100163 chan_delete_if_full_closed(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000164 break;
165 default:
Damien Milleraae6c611999-12-06 11:47:28 +1100166 error("internal error: chan_obuf_empty %d for ostate %d", c->self, c->ostate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000167 break;
168 }
169}
Damien Miller95def091999-11-25 00:26:21 +1100170
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000171/*
Damien Miller95def091999-11-25 00:26:21 +1100172 * ACTIONS: should never update the channel states: c->istate or c->ostate
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000173 */
174static void
Damien Miller95def091999-11-25 00:26:21 +1100175chan_send_ieof(Channel *c)
176{
177 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000178 case CHAN_INPUT_OPEN:
179 case CHAN_INPUT_WAIT_DRAIN:
180 packet_start(SSH_MSG_CHANNEL_INPUT_EOF);
181 packet_put_int(c->remote_id);
182 packet_send();
183 break;
184 default:
Damien Milleraae6c611999-12-06 11:47:28 +1100185 error("internal error: channel %d: cannot send IEOF for istate %d", c->self, c->istate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000186 break;
187 }
188}
189static void
Damien Miller95def091999-11-25 00:26:21 +1100190chan_send_oclose(Channel *c)
191{
192 switch (c->ostate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000193 case CHAN_OUTPUT_OPEN:
194 case CHAN_OUTPUT_WAIT_DRAIN:
195 chan_shutdown_write(c);
196 buffer_consume(&c->output, buffer_len(&c->output));
197 packet_start(SSH_MSG_CHANNEL_OUTPUT_CLOSE);
198 packet_put_int(c->remote_id);
199 packet_send();
200 break;
201 default:
Damien Milleraae6c611999-12-06 11:47:28 +1100202 error("internal error: channel %d: cannot send OCLOSE for ostate %d", c->self, c->istate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000203 break;
204 }
205}
Damien Miller95def091999-11-25 00:26:21 +1100206
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000207/* helper */
208static void
Damien Miller95def091999-11-25 00:26:21 +1100209chan_shutdown_write(Channel *c)
210{
Damien Miller5428f641999-11-25 11:54:57 +1100211 /* shutdown failure is allowed if write failed already */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000212 debug("channel %d: shutdown_write", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100213 if (shutdown(c->sock, SHUT_WR) < 0)
Damien Miller5428f641999-11-25 11:54:57 +1100214 debug("chan_shutdown_write failed for #%d/fd%d: %.100s",
Damien Miller95def091999-11-25 00:26:21 +1100215 c->self, c->sock, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000216}
217static void
Damien Miller95def091999-11-25 00:26:21 +1100218chan_shutdown_read(Channel *c)
219{
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000220 debug("channel %d: shutdown_read", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100221 if (shutdown(c->sock, SHUT_RD) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100222 error("chan_shutdown_read failed for #%d/fd%d [i%d o%d]: %.100s",
223 c->self, c->sock, c->istate, c->ostate, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000224}
225static void
Damien Miller34132e52000-01-14 15:45:46 +1100226chan_delete_if_full_closed(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100227{
228 if (c->istate == CHAN_INPUT_CLOSED && c->ostate == CHAN_OUTPUT_CLOSED) {
Damien Miller34132e52000-01-14 15:45:46 +1100229 debug("channel %d: full closed", c->self);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000230 channel_free(c->self);
231 }
232}
233void
Damien Miller95def091999-11-25 00:26:21 +1100234chan_init_iostates(Channel *c)
235{
236 c->ostate = CHAN_OUTPUT_OPEN;
237 c->istate = CHAN_INPUT_OPEN;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000238}