blob: 996623fb401ca81fd759b4e645ad39da1e2e4831 [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 Millerb38eff82000-04-01 11:09:21 +100031RCSID("$Id: nchan.c,v 1.6 2000/04/01 01:09:24 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 Millerd4a8b7e1999-10-27 13:42:43 +100044
45/*
Damien Miller95def091999-11-25 00:26:21 +110046 * EVENTS update channel input/output states execute ACTIONS
Damien Millerd4a8b7e1999-10-27 13:42:43 +100047 */
Damien Miller95def091999-11-25 00:26:21 +110048
Damien Millerd4a8b7e1999-10-27 13:42:43 +100049/* events concerning the INPUT from socket for channel (istate) */
50void
Damien Miller95def091999-11-25 00:26:21 +110051chan_rcvd_oclose(Channel *c)
52{
53 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +100054 case CHAN_INPUT_WAIT_OCLOSE:
55 debug("channel %d: INPUT_WAIT_OCLOSE -> INPUT_CLOSED [rcvd OCLOSE]", c->self);
Damien Miller95def091999-11-25 00:26:21 +110056 c->istate = CHAN_INPUT_CLOSED;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100057 break;
58 case CHAN_INPUT_OPEN:
59 debug("channel %d: INPUT_OPEN -> INPUT_CLOSED [rvcd OCLOSE, send IEOF]", c->self);
60 chan_shutdown_read(c);
61 chan_send_ieof(c);
Damien Miller95def091999-11-25 00:26:21 +110062 c->istate = CHAN_INPUT_CLOSED;
Damien Miller34132e52000-01-14 15:45:46 +110063 break;
64 case CHAN_INPUT_WAIT_DRAIN:
65 /* both local read_failed and remote write_failed */
66 log("channel %d: INPUT_WAIT_DRAIN -> INPUT_CLOSED [rvcd OCLOSE, send IEOF]", c->self);
67 debug("channel %d: INPUT_WAIT_DRAIN -> INPUT_CLOSED [rvcd OCLOSE, send IEOF]", c->self);
68 chan_send_ieof(c);
69 c->istate = CHAN_INPUT_CLOSED;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100070 break;
71 default:
Damien Milleraae6c611999-12-06 11:47:28 +110072 error("protocol error: chan_rcvd_oclose %d for istate %d", c->self, c->istate);
Damien Miller34132e52000-01-14 15:45:46 +110073 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100074 }
75}
76void
Damien Miller95def091999-11-25 00:26:21 +110077chan_read_failed(Channel *c)
78{
79 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080 case CHAN_INPUT_OPEN:
81 debug("channel %d: INPUT_OPEN -> INPUT_WAIT_DRAIN [read failed]", c->self);
82 chan_shutdown_read(c);
Damien Miller95def091999-11-25 00:26:21 +110083 c->istate = CHAN_INPUT_WAIT_DRAIN;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100084 break;
85 default:
Damien Milleraae6c611999-12-06 11:47:28 +110086 error("internal error: we do not read, but chan_read_failed %d for istate %d",
Damien Miller95def091999-11-25 00:26:21 +110087 c->self, c->istate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088 break;
89 }
90}
91void
Damien Miller95def091999-11-25 00:26:21 +110092chan_ibuf_empty(Channel *c)
93{
94 if (buffer_len(&c->input)) {
Damien Milleraae6c611999-12-06 11:47:28 +110095 error("internal error: chan_ibuf_empty %d for non empty buffer", c->self);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100096 return;
97 }
Damien Miller95def091999-11-25 00:26:21 +110098 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +100099 case CHAN_INPUT_WAIT_DRAIN:
100 debug("channel %d: INPUT_WAIT_DRAIN -> INPUT_WAIT_OCLOSE [inbuf empty, send IEOF]", c->self);
101 chan_send_ieof(c);
Damien Miller95def091999-11-25 00:26:21 +1100102 c->istate = CHAN_INPUT_WAIT_OCLOSE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000103 break;
104 default:
Damien Milleraae6c611999-12-06 11:47:28 +1100105 error("internal error: chan_ibuf_empty %d for istate %d", c->self, c->istate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000106 break;
107 }
108}
Damien Miller95def091999-11-25 00:26:21 +1100109
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000110/* events concerning the OUTPUT from channel for socket (ostate) */
111void
Damien Miller95def091999-11-25 00:26:21 +1100112chan_rcvd_ieof(Channel *c)
113{
114 switch (c->ostate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000115 case CHAN_OUTPUT_OPEN:
116 debug("channel %d: OUTPUT_OPEN -> OUTPUT_WAIT_DRAIN [rvcd IEOF]", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100117 c->ostate = CHAN_OUTPUT_WAIT_DRAIN;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000118 break;
119 case CHAN_OUTPUT_WAIT_IEOF:
120 debug("channel %d: OUTPUT_WAIT_IEOF -> OUTPUT_CLOSED [rvcd IEOF]", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100121 c->ostate = CHAN_OUTPUT_CLOSED;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000122 break;
123 default:
Damien Milleraae6c611999-12-06 11:47:28 +1100124 error("protocol error: chan_rcvd_ieof %d for ostate %d", c->self, c->ostate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000125 break;
126 }
127}
128void
Damien Miller95def091999-11-25 00:26:21 +1100129chan_write_failed(Channel *c)
130{
131 switch (c->ostate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000132 case CHAN_OUTPUT_OPEN:
133 debug("channel %d: OUTPUT_OPEN -> OUTPUT_WAIT_IEOF [write failed]", c->self);
134 chan_send_oclose(c);
Damien Miller95def091999-11-25 00:26:21 +1100135 c->ostate = CHAN_OUTPUT_WAIT_IEOF;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000136 break;
137 case CHAN_OUTPUT_WAIT_DRAIN:
138 debug("channel %d: OUTPUT_WAIT_DRAIN -> OUTPUT_CLOSED [write failed]", c->self);
139 chan_send_oclose(c);
Damien Miller95def091999-11-25 00:26:21 +1100140 c->ostate = CHAN_OUTPUT_CLOSED;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000141 break;
142 default:
Damien Milleraae6c611999-12-06 11:47:28 +1100143 error("internal error: chan_write_failed %d for ostate %d", c->self, c->ostate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000144 break;
145 }
146}
147void
Damien Miller95def091999-11-25 00:26:21 +1100148chan_obuf_empty(Channel *c)
149{
150 if (buffer_len(&c->output)) {
151 debug("internal error: chan_obuf_empty %d for non empty buffer", c->self);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000152 return;
153 }
Damien Miller95def091999-11-25 00:26:21 +1100154 switch (c->ostate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000155 case CHAN_OUTPUT_WAIT_DRAIN:
156 debug("channel %d: OUTPUT_WAIT_DRAIN -> OUTPUT_CLOSED [obuf empty, send OCLOSE]", c->self);
157 chan_send_oclose(c);
Damien Miller95def091999-11-25 00:26:21 +1100158 c->ostate = CHAN_OUTPUT_CLOSED;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000159 break;
160 default:
Damien Milleraae6c611999-12-06 11:47:28 +1100161 error("internal error: chan_obuf_empty %d for ostate %d", c->self, c->ostate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000162 break;
163 }
164}
Damien Miller95def091999-11-25 00:26:21 +1100165
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000166/*
Damien Miller95def091999-11-25 00:26:21 +1100167 * ACTIONS: should never update the channel states: c->istate or c->ostate
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000168 */
169static void
Damien Miller95def091999-11-25 00:26:21 +1100170chan_send_ieof(Channel *c)
171{
172 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000173 case CHAN_INPUT_OPEN:
174 case CHAN_INPUT_WAIT_DRAIN:
175 packet_start(SSH_MSG_CHANNEL_INPUT_EOF);
176 packet_put_int(c->remote_id);
177 packet_send();
178 break;
179 default:
Damien Milleraae6c611999-12-06 11:47:28 +1100180 error("internal error: channel %d: cannot send IEOF for istate %d", c->self, c->istate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000181 break;
182 }
183}
184static void
Damien Miller95def091999-11-25 00:26:21 +1100185chan_send_oclose(Channel *c)
186{
187 switch (c->ostate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000188 case CHAN_OUTPUT_OPEN:
189 case CHAN_OUTPUT_WAIT_DRAIN:
190 chan_shutdown_write(c);
191 buffer_consume(&c->output, buffer_len(&c->output));
192 packet_start(SSH_MSG_CHANNEL_OUTPUT_CLOSE);
193 packet_put_int(c->remote_id);
194 packet_send();
195 break;
196 default:
Damien Milleraae6c611999-12-06 11:47:28 +1100197 error("internal error: channel %d: cannot send OCLOSE for ostate %d", c->self, c->istate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000198 break;
199 }
200}
Damien Miller95def091999-11-25 00:26:21 +1100201
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000202/* helper */
203static void
Damien Miller95def091999-11-25 00:26:21 +1100204chan_shutdown_write(Channel *c)
205{
Damien Miller5428f641999-11-25 11:54:57 +1100206 /* shutdown failure is allowed if write failed already */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000207 debug("channel %d: shutdown_write", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100208 if (shutdown(c->sock, SHUT_WR) < 0)
Damien Miller5428f641999-11-25 11:54:57 +1100209 debug("chan_shutdown_write failed for #%d/fd%d: %.100s",
Damien Miller95def091999-11-25 00:26:21 +1100210 c->self, c->sock, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000211}
212static void
Damien Miller95def091999-11-25 00:26:21 +1100213chan_shutdown_read(Channel *c)
214{
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000215 debug("channel %d: shutdown_read", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100216 if (shutdown(c->sock, SHUT_RD) < 0)
Damien Miller34132e52000-01-14 15:45:46 +1100217 error("chan_shutdown_read failed for #%d/fd%d [i%d o%d]: %.100s",
218 c->self, c->sock, c->istate, c->ostate, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000219}
Damien Millerb38eff82000-04-01 11:09:21 +1000220void
Damien Miller34132e52000-01-14 15:45:46 +1100221chan_delete_if_full_closed(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100222{
223 if (c->istate == CHAN_INPUT_CLOSED && c->ostate == CHAN_OUTPUT_CLOSED) {
Damien Miller34132e52000-01-14 15:45:46 +1100224 debug("channel %d: full closed", c->self);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000225 channel_free(c->self);
226 }
227}
228void
Damien Miller95def091999-11-25 00:26:21 +1100229chan_init_iostates(Channel *c)
230{
231 c->ostate = CHAN_OUTPUT_OPEN;
232 c->istate = CHAN_INPUT_OPEN;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000233}