blob: 02c213c2013ab136c9f56d4453c36c5011da8676 [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.
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 Miller0bc1bd82000-11-13 22:57:25 +110026RCSID("$OpenBSD: nchan.c,v 1.20 2000/11/06 23:04:56 markus Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100027
28#include "ssh.h"
29
30#include "buffer.h"
31#include "packet.h"
32#include "channels.h"
33#include "nchan.h"
34
Damien Miller33b13562000-04-04 14:38:59 +100035#include "ssh2.h"
36#include "compat.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100037
Damien Miller33b13562000-04-04 14:38:59 +100038/* functions manipulating channel states */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100039/*
Damien Miller95def091999-11-25 00:26:21 +110040 * EVENTS update channel input/output states execute ACTIONS
Damien Millerd4a8b7e1999-10-27 13:42:43 +100041 */
42/* events concerning the INPUT from socket for channel (istate) */
Damien Miller33b13562000-04-04 14:38:59 +100043chan_event_fn *chan_rcvd_oclose = NULL;
44chan_event_fn *chan_read_failed = NULL;
45chan_event_fn *chan_ibuf_empty = NULL;
46/* events concerning the OUTPUT from channel for socket (ostate) */
47chan_event_fn *chan_rcvd_ieof = NULL;
48chan_event_fn *chan_write_failed = NULL;
49chan_event_fn *chan_obuf_empty = NULL;
50/*
51 * ACTIONS: should never update the channel states
52 */
53static void chan_send_ieof1(Channel *c);
54static void chan_send_oclose1(Channel *c);
55static void chan_send_close2(Channel *c);
56static void chan_send_eof2(Channel *c);
57
58/* channel cleanup */
59chan_event_fn *chan_delete_if_full_closed = NULL;
60
61/* helper */
62static void chan_shutdown_write(Channel *c);
63static void chan_shutdown_read(Channel *c);
64
65/*
66 * SSH1 specific implementation of event functions
67 */
68
69static void
70chan_rcvd_oclose1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +110071{
Damien Miller33b13562000-04-04 14:38:59 +100072 debug("channel %d: rcvd oclose", c->self);
Damien Miller95def091999-11-25 00:26:21 +110073 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +100074 case CHAN_INPUT_WAIT_OCLOSE:
Damien Miller33b13562000-04-04 14:38:59 +100075 debug("channel %d: input wait_oclose -> closed", c->self);
Damien Miller95def091999-11-25 00:26:21 +110076 c->istate = CHAN_INPUT_CLOSED;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100077 break;
78 case CHAN_INPUT_OPEN:
Damien Miller33b13562000-04-04 14:38:59 +100079 debug("channel %d: input open -> closed", c->self);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080 chan_shutdown_read(c);
Damien Miller33b13562000-04-04 14:38:59 +100081 chan_send_ieof1(c);
Damien Miller95def091999-11-25 00:26:21 +110082 c->istate = CHAN_INPUT_CLOSED;
Damien Miller34132e52000-01-14 15:45:46 +110083 break;
84 case CHAN_INPUT_WAIT_DRAIN:
85 /* both local read_failed and remote write_failed */
Damien Miller33b13562000-04-04 14:38:59 +100086 log("channel %d: input drain -> closed", c->self);
87 chan_send_ieof1(c);
Damien Miller34132e52000-01-14 15:45:46 +110088 c->istate = CHAN_INPUT_CLOSED;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089 break;
90 default:
Damien Miller33b13562000-04-04 14:38:59 +100091 error("channel %d: protocol error: chan_rcvd_oclose for istate %d",
92 c->self, c->istate);
Damien Miller34132e52000-01-14 15:45:46 +110093 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100094 }
95}
Damien Miller33b13562000-04-04 14:38:59 +100096static void
97chan_read_failed_12(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +110098{
Damien Miller33b13562000-04-04 14:38:59 +100099 debug("channel %d: read failed", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100100 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000101 case CHAN_INPUT_OPEN:
Damien Miller33b13562000-04-04 14:38:59 +1000102 debug("channel %d: input open -> drain", c->self);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000103 chan_shutdown_read(c);
Damien Miller95def091999-11-25 00:26:21 +1100104 c->istate = CHAN_INPUT_WAIT_DRAIN;
Damien Miller30c3d422000-05-09 11:02:59 +1000105 if (buffer_len(&c->input) == 0) {
106 debug("channel %d: input: no drain shortcut", c->self);
107 chan_ibuf_empty(c);
108 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000109 break;
110 default:
Damien Miller33b13562000-04-04 14:38:59 +1000111 error("channel %d: internal error: we do not read, but chan_read_failed for istate %d",
112 c->self, c->istate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000113 break;
114 }
115}
Damien Miller33b13562000-04-04 14:38:59 +1000116static void
117chan_ibuf_empty1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100118{
Damien Miller33b13562000-04-04 14:38:59 +1000119 debug("channel %d: ibuf empty", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100120 if (buffer_len(&c->input)) {
Damien Miller33b13562000-04-04 14:38:59 +1000121 error("channel %d: internal error: chan_ibuf_empty for non empty buffer",
122 c->self);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000123 return;
124 }
Damien Miller95def091999-11-25 00:26:21 +1100125 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000126 case CHAN_INPUT_WAIT_DRAIN:
Damien Miller33b13562000-04-04 14:38:59 +1000127 debug("channel %d: input drain -> wait_oclose", c->self);
128 chan_send_ieof1(c);
Damien Miller95def091999-11-25 00:26:21 +1100129 c->istate = CHAN_INPUT_WAIT_OCLOSE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000130 break;
131 default:
Damien Miller33b13562000-04-04 14:38:59 +1000132 error("channel %d: internal error: chan_ibuf_empty for istate %d",
133 c->self, c->istate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000134 break;
135 }
136}
Damien Miller33b13562000-04-04 14:38:59 +1000137static void
138chan_rcvd_ieof1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100139{
Damien Miller33b13562000-04-04 14:38:59 +1000140 debug("channel %d: rcvd ieof", c->self);
Damien Millere247cc42000-05-07 12:03:14 +1000141 if (c->type != SSH_CHANNEL_OPEN) {
142 debug("channel %d: non-open", c->self);
143 if (c->istate == CHAN_INPUT_OPEN) {
144 debug("channel %d: non-open: input open -> wait_oclose", c->self);
145 chan_shutdown_read(c);
146 chan_send_ieof1(c);
147 c->istate = CHAN_INPUT_WAIT_OCLOSE;
148 } else {
149 error("channel %d: istate %d != open", c->self, c->istate);
150 }
151 if (c->ostate == CHAN_OUTPUT_OPEN) {
152 debug("channel %d: non-open: output open -> closed", c->self);
153 chan_send_oclose1(c);
154 c->ostate = CHAN_OUTPUT_CLOSED;
155 } else {
156 error("channel %d: ostate %d != open", c->self, c->ostate);
157 }
158 return;
159 }
Damien Miller95def091999-11-25 00:26:21 +1100160 switch (c->ostate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000161 case CHAN_OUTPUT_OPEN:
Damien Miller33b13562000-04-04 14:38:59 +1000162 debug("channel %d: output open -> drain", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100163 c->ostate = CHAN_OUTPUT_WAIT_DRAIN;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000164 break;
165 case CHAN_OUTPUT_WAIT_IEOF:
Damien Miller33b13562000-04-04 14:38:59 +1000166 debug("channel %d: output wait_ieof -> closed", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100167 c->ostate = CHAN_OUTPUT_CLOSED;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000168 break;
169 default:
Damien Miller33b13562000-04-04 14:38:59 +1000170 error("channel %d: protocol error: chan_rcvd_ieof for ostate %d",
171 c->self, c->ostate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000172 break;
173 }
174}
Damien Miller33b13562000-04-04 14:38:59 +1000175static void
176chan_write_failed1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100177{
Damien Miller33b13562000-04-04 14:38:59 +1000178 debug("channel %d: write failed", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100179 switch (c->ostate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000180 case CHAN_OUTPUT_OPEN:
Damien Miller33b13562000-04-04 14:38:59 +1000181 debug("channel %d: output open -> wait_ieof", c->self);
182 chan_send_oclose1(c);
Damien Miller95def091999-11-25 00:26:21 +1100183 c->ostate = CHAN_OUTPUT_WAIT_IEOF;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000184 break;
185 case CHAN_OUTPUT_WAIT_DRAIN:
Damien Miller33b13562000-04-04 14:38:59 +1000186 debug("channel %d: output wait_drain -> closed", c->self);
187 chan_send_oclose1(c);
Damien Miller95def091999-11-25 00:26:21 +1100188 c->ostate = CHAN_OUTPUT_CLOSED;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000189 break;
190 default:
Damien Miller33b13562000-04-04 14:38:59 +1000191 error("channel %d: internal error: chan_write_failed for ostate %d",
192 c->self, c->ostate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000193 break;
194 }
195}
Damien Miller33b13562000-04-04 14:38:59 +1000196static void
197chan_obuf_empty1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100198{
Damien Miller33b13562000-04-04 14:38:59 +1000199 debug("channel %d: obuf empty", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100200 if (buffer_len(&c->output)) {
Damien Miller33b13562000-04-04 14:38:59 +1000201 error("channel %d: internal error: chan_obuf_empty for non empty buffer",
202 c->self);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000203 return;
204 }
Damien Miller95def091999-11-25 00:26:21 +1100205 switch (c->ostate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000206 case CHAN_OUTPUT_WAIT_DRAIN:
Damien Miller33b13562000-04-04 14:38:59 +1000207 debug("channel %d: output drain -> closed", c->self);
208 chan_send_oclose1(c);
Damien Miller95def091999-11-25 00:26:21 +1100209 c->ostate = CHAN_OUTPUT_CLOSED;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000210 break;
211 default:
Damien Miller33b13562000-04-04 14:38:59 +1000212 error("channel %d: internal error: chan_obuf_empty for ostate %d",
213 c->self, c->ostate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000214 break;
215 }
216}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000217static void
Damien Miller33b13562000-04-04 14:38:59 +1000218chan_send_ieof1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100219{
Damien Miller33b13562000-04-04 14:38:59 +1000220 debug("channel %d: send ieof", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100221 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000222 case CHAN_INPUT_OPEN:
223 case CHAN_INPUT_WAIT_DRAIN:
224 packet_start(SSH_MSG_CHANNEL_INPUT_EOF);
225 packet_put_int(c->remote_id);
226 packet_send();
227 break;
228 default:
Damien Miller33b13562000-04-04 14:38:59 +1000229 error("channel %d: internal error: cannot send ieof for istate %d",
230 c->self, c->istate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000231 break;
232 }
233}
234static void
Damien Miller33b13562000-04-04 14:38:59 +1000235chan_send_oclose1(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100236{
Damien Miller33b13562000-04-04 14:38:59 +1000237 debug("channel %d: send oclose", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100238 switch (c->ostate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000239 case CHAN_OUTPUT_OPEN:
240 case CHAN_OUTPUT_WAIT_DRAIN:
241 chan_shutdown_write(c);
242 buffer_consume(&c->output, buffer_len(&c->output));
243 packet_start(SSH_MSG_CHANNEL_OUTPUT_CLOSE);
244 packet_put_int(c->remote_id);
245 packet_send();
246 break;
247 default:
Damien Miller33b13562000-04-04 14:38:59 +1000248 error("channel %d: internal error: cannot send oclose for ostate %d",
249 c->self, c->ostate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000250 break;
251 }
252}
Damien Miller33b13562000-04-04 14:38:59 +1000253static void
254chan_delete_if_full_closed1(Channel *c)
255{
Damien Miller0bc1bd82000-11-13 22:57:25 +1100256 debug3("channel %d: chan_delete_if_full_closed1: istate %d ostate %d",
257 c->self, c->istate, c->ostate);
Damien Miller33b13562000-04-04 14:38:59 +1000258 if (c->istate == CHAN_INPUT_CLOSED && c->ostate == CHAN_OUTPUT_CLOSED) {
259 debug("channel %d: full closed", c->self);
260 channel_free(c->self);
261 }
262}
263
264/*
265 * the same for SSH2
266 */
267static void
268chan_rcvd_oclose2(Channel *c)
269{
270 debug("channel %d: rcvd close", c->self);
271 if (c->flags & CHAN_CLOSE_RCVD)
272 error("channel %d: protocol error: close rcvd twice", c->self);
273 c->flags |= CHAN_CLOSE_RCVD;
274 if (c->type == SSH_CHANNEL_LARVAL) {
275 /* tear down larval channels immediately */
276 c->ostate = CHAN_OUTPUT_CLOSED;
277 c->istate = CHAN_INPUT_CLOSED;
278 return;
279 }
280 switch (c->ostate) {
281 case CHAN_OUTPUT_OPEN:
282 /* wait until a data from the channel is consumed if a CLOSE is received */
283 debug("channel %d: output open -> drain", c->self);
284 c->ostate = CHAN_OUTPUT_WAIT_DRAIN;
285 break;
286 }
287 switch (c->istate) {
288 case CHAN_INPUT_OPEN:
289 debug("channel %d: input open -> closed", c->self);
290 chan_shutdown_read(c);
291 break;
292 case CHAN_INPUT_WAIT_DRAIN:
293 debug("channel %d: input drain -> closed", c->self);
294 chan_send_eof2(c);
295 break;
296 }
297 c->istate = CHAN_INPUT_CLOSED;
298}
299static void
300chan_ibuf_empty2(Channel *c)
301{
302 debug("channel %d: ibuf empty", c->self);
303 if (buffer_len(&c->input)) {
304 error("channel %d: internal error: chan_ibuf_empty for non empty buffer",
305 c->self);
306 return;
307 }
308 switch (c->istate) {
309 case CHAN_INPUT_WAIT_DRAIN:
310 debug("channel %d: input drain -> closed", c->self);
311 if (!(c->flags & CHAN_CLOSE_SENT))
312 chan_send_eof2(c);
313 c->istate = CHAN_INPUT_CLOSED;
314 break;
315 default:
316 error("channel %d: internal error: chan_ibuf_empty for istate %d",
317 c->self, c->istate);
318 break;
319 }
320}
321static void
322chan_rcvd_ieof2(Channel *c)
323{
324 debug("channel %d: rcvd eof", c->self);
325 if (c->ostate == CHAN_OUTPUT_OPEN) {
326 debug("channel %d: output open -> drain", c->self);
327 c->ostate = CHAN_OUTPUT_WAIT_DRAIN;
328 }
329}
330static void
331chan_write_failed2(Channel *c)
332{
333 debug("channel %d: write failed", c->self);
334 switch (c->ostate) {
335 case CHAN_OUTPUT_OPEN:
336 debug("channel %d: output open -> closed", c->self);
Damien Millere247cc42000-05-07 12:03:14 +1000337 chan_shutdown_write(c); /* ?? */
Damien Miller33b13562000-04-04 14:38:59 +1000338 c->ostate = CHAN_OUTPUT_CLOSED;
339 break;
340 case CHAN_OUTPUT_WAIT_DRAIN:
341 debug("channel %d: output drain -> closed", c->self);
342 chan_shutdown_write(c);
343 c->ostate = CHAN_OUTPUT_CLOSED;
344 break;
345 default:
346 error("channel %d: internal error: chan_write_failed for ostate %d",
347 c->self, c->ostate);
348 break;
349 }
350}
351static void
352chan_obuf_empty2(Channel *c)
353{
354 debug("channel %d: obuf empty", c->self);
355 if (buffer_len(&c->output)) {
356 error("internal error: chan_obuf_empty %d for non empty buffer",
357 c->self);
358 return;
359 }
360 switch (c->ostate) {
361 case CHAN_OUTPUT_WAIT_DRAIN:
362 debug("channel %d: output drain -> closed", c->self);
363 chan_shutdown_write(c);
364 c->ostate = CHAN_OUTPUT_CLOSED;
365 break;
366 default:
367 error("channel %d: internal error: chan_obuf_empty for ostate %d",
368 c->self, c->ostate);
369 break;
370 }
371}
372static void
373chan_send_eof2(Channel *c)
374{
375 debug("channel %d: send eof", c->self);
376 switch (c->istate) {
377 case CHAN_INPUT_WAIT_DRAIN:
378 packet_start(SSH2_MSG_CHANNEL_EOF);
379 packet_put_int(c->remote_id);
380 packet_send();
381 break;
382 default:
383 error("channel %d: internal error: cannot send eof for istate %d",
384 c->self, c->istate);
385 break;
386 }
387}
388static void
389chan_send_close2(Channel *c)
390{
391 debug("channel %d: send close", c->self);
392 if (c->ostate != CHAN_OUTPUT_CLOSED ||
393 c->istate != CHAN_INPUT_CLOSED) {
394 error("channel %d: internal error: cannot send close for istate/ostate %d/%d",
395 c->self, c->istate, c->ostate);
396 } else if (c->flags & CHAN_CLOSE_SENT) {
397 error("channel %d: internal error: already sent close", c->self);
398 } else {
399 packet_start(SSH2_MSG_CHANNEL_CLOSE);
400 packet_put_int(c->remote_id);
401 packet_send();
402 c->flags |= CHAN_CLOSE_SENT;
403 }
404}
405static void
406chan_delete_if_full_closed2(Channel *c)
407{
Damien Miller0bc1bd82000-11-13 22:57:25 +1100408 debug3("channel %d: chan_delete_if_full_closed2: istate %d ostate %d",
409 c->self, c->istate, c->ostate);
Damien Miller33b13562000-04-04 14:38:59 +1000410 if (c->istate == CHAN_INPUT_CLOSED && c->ostate == CHAN_OUTPUT_CLOSED) {
411 if (!(c->flags & CHAN_CLOSE_SENT)) {
412 chan_send_close2(c);
413 }
Damien Miller4af51302000-04-16 11:18:38 +1000414 if ((c->flags & CHAN_CLOSE_SENT) &&
Damien Miller33b13562000-04-04 14:38:59 +1000415 (c->flags & CHAN_CLOSE_RCVD)) {
416 debug("channel %d: full closed2", c->self);
417 channel_free(c->self);
Damien Miller4af51302000-04-16 11:18:38 +1000418 }
Damien Miller33b13562000-04-04 14:38:59 +1000419 }
420}
421
422/* shared */
423void
424chan_init_iostates(Channel *c)
425{
426 c->ostate = CHAN_OUTPUT_OPEN;
427 c->istate = CHAN_INPUT_OPEN;
428 c->flags = 0;
429}
430
431/* init */
432void
433chan_init(void)
434{
435 if (compat20) {
436 chan_rcvd_oclose = chan_rcvd_oclose2;
437 chan_read_failed = chan_read_failed_12;
438 chan_ibuf_empty = chan_ibuf_empty2;
439
440 chan_rcvd_ieof = chan_rcvd_ieof2;
441 chan_write_failed = chan_write_failed2;
442 chan_obuf_empty = chan_obuf_empty2;
443
444 chan_delete_if_full_closed = chan_delete_if_full_closed2;
445 } else {
446 chan_rcvd_oclose = chan_rcvd_oclose1;
447 chan_read_failed = chan_read_failed_12;
448 chan_ibuf_empty = chan_ibuf_empty1;
449
450 chan_rcvd_ieof = chan_rcvd_ieof1;
451 chan_write_failed = chan_write_failed1;
452 chan_obuf_empty = chan_obuf_empty1;
453
454 chan_delete_if_full_closed = chan_delete_if_full_closed1;
455 }
456}
Damien Miller95def091999-11-25 00:26:21 +1100457
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000458/* helper */
459static void
Damien Miller95def091999-11-25 00:26:21 +1100460chan_shutdown_write(Channel *c)
461{
Damien Miller33b13562000-04-04 14:38:59 +1000462 buffer_consume(&c->output, buffer_len(&c->output));
463 if (compat20 && c->type == SSH_CHANNEL_LARVAL)
464 return;
Damien Miller5428f641999-11-25 11:54:57 +1100465 /* shutdown failure is allowed if write failed already */
Damien Miller33b13562000-04-04 14:38:59 +1000466 debug("channel %d: close_write", c->self);
467 if (c->sock != -1) {
468 if (shutdown(c->sock, SHUT_WR) < 0)
469 debug("channel %d: chan_shutdown_write: shutdown() failed for fd%d: %.100s",
470 c->self, c->sock, strerror(errno));
471 } else {
472 if (close(c->wfd) < 0)
473 log("channel %d: chan_shutdown_write: close() failed for fd%d: %.100s",
474 c->self, c->wfd, strerror(errno));
475 c->wfd = -1;
476 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000477}
478static void
Damien Miller95def091999-11-25 00:26:21 +1100479chan_shutdown_read(Channel *c)
480{
Damien Miller33b13562000-04-04 14:38:59 +1000481 if (compat20 && c->type == SSH_CHANNEL_LARVAL)
482 return;
483 debug("channel %d: close_read", c->self);
484 if (c->sock != -1) {
Damien Miller0f091bd2000-08-07 15:47:48 +1000485 /*
486 * shutdown(sock, SHUT_READ) may return ENOTCONN if the
487 * write side has been closed already. (bug on Linux)
488 */
489 if (shutdown(c->sock, SHUT_RD) < 0
490 && (errno != ENOTCONN
491 || c->ostate == CHAN_OUTPUT_OPEN
492 || c->ostate == CHAN_OUTPUT_WAIT_DRAIN))
Damien Miller33b13562000-04-04 14:38:59 +1000493 error("channel %d: chan_shutdown_read: shutdown() failed for fd%d [i%d o%d]: %.100s",
494 c->self, c->sock, c->istate, c->ostate, strerror(errno));
495 } else {
496 if (close(c->rfd) < 0)
497 log("channel %d: chan_shutdown_read: close() failed for fd%d: %.100s",
498 c->self, c->rfd, strerror(errno));
499 c->rfd = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000500 }
501}