blob: 36da8904a95226c78f5600e001a387a475cc1105 [file] [log] [blame]
djm@openbsd.org930e8d22017-04-30 23:28:41 +00001/* $OpenBSD: nchan.c,v 1.65 2017/04/30 23:28:42 djm Exp $ */
Damien Miller5428f641999-11-25 11:54:57 +11002/*
Damien Millerb51ed392002-01-22 23:29:03 +11003 * Copyright (c) 1999, 2000, 2001, 2002 Markus Friedl. All rights reserved.
Damien Miller5428f641999-11-25 11:54:57 +11004 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
Damien Miller5428f641999-11-25 11:54:57 +110013 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
Damien Millerd4a8b7e1999-10-27 13:42:43 +100026#include "includes.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100027
Damien Millere3b60b52006-07-10 21:08:03 +100028#include <sys/types.h>
29#include <sys/socket.h>
30
Darren Tucker39972492006-07-12 22:22:46 +100031#include <errno.h>
Damien Millere3476ed2006-07-24 14:13:33 +100032#include <string.h>
Damien Millerd7834352006-08-05 12:39:39 +100033#include <stdarg.h>
Darren Tucker39972492006-07-12 22:22:46 +100034
Damien Millerb84886b2008-05-19 15:05:07 +100035#include "openbsd-compat/sys-queue.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000036#include "ssh2.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100037#include "buffer.h"
38#include "packet.h"
39#include "channels.h"
Damien Miller33b13562000-04-04 14:38:59 +100040#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000041#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100042
Ben Lindstrom3b670d02001-06-09 00:57:39 +000043/*
44 * SSH Protocol 1.5 aka New Channel Protocol
45 * Thanks to Martina, Axel and everyone who left Erlangen, leaving me bored.
46 * Written by Markus Friedl in October 1999
47 *
48 * Protocol versions 1.3 and 1.5 differ in the handshake protocol used for the
49 * tear down of channels:
50 *
51 * 1.3: strict request-ack-protocol:
Darren Tuckerfc959702004-07-17 16:12:08 +100052 * CLOSE ->
53 * <- CLOSE_CONFIRM
Ben Lindstrom3b670d02001-06-09 00:57:39 +000054 *
55 * 1.5: uses variations of:
Darren Tuckerfc959702004-07-17 16:12:08 +100056 * IEOF ->
57 * <- OCLOSE
58 * <- IEOF
59 * OCLOSE ->
60 * i.e. both sides have to close the channel
Ben Lindstrom3b670d02001-06-09 00:57:39 +000061 *
62 * 2.0: the EOF messages are optional
63 *
64 * See the debugging output from 'ssh -v' and 'sshd -d' of
65 * ssh-1.2.27 as an example.
66 *
67 */
Kevin Stevesc3422eb2001-09-20 19:33:33 +000068
Damien Miller33b13562000-04-04 14:38:59 +100069/* functions manipulating channel states */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100070/*
Damien Miller95def091999-11-25 00:26:21 +110071 * EVENTS update channel input/output states execute ACTIONS
Damien Millerd4a8b7e1999-10-27 13:42:43 +100072 */
Damien Miller33b13562000-04-04 14:38:59 +100073/*
74 * ACTIONS: should never update the channel states
75 */
Ben Lindstrombba81212001-06-25 05:01:22 +000076static void chan_send_eof2(Channel *);
Damien Millerbab9bd42008-05-19 16:06:47 +100077static void chan_send_eow2(Channel *);
Damien Miller33b13562000-04-04 14:38:59 +100078
Damien Miller33b13562000-04-04 14:38:59 +100079/* helper */
Ben Lindstrombba81212001-06-25 05:01:22 +000080static void chan_shutdown_write(Channel *);
81static void chan_shutdown_read(Channel *);
Damien Miller33b13562000-04-04 14:38:59 +100082
Damien Millerabea8ee2002-01-22 23:27:11 +110083static char *ostates[] = { "open", "drain", "wait_ieof", "closed" };
84static char *istates[] = { "open", "drain", "wait_oclose", "closed" };
85
86static void
87chan_set_istate(Channel *c, u_int next)
88{
89 if (c->istate > CHAN_INPUT_CLOSED || next > CHAN_INPUT_CLOSED)
90 fatal("chan_set_istate: bad state %d -> %d", c->istate, next);
Damien Millerfbdeece2003-09-02 22:52:31 +100091 debug2("channel %d: input %s -> %s", c->self, istates[c->istate],
Damien Millerabea8ee2002-01-22 23:27:11 +110092 istates[next]);
93 c->istate = next;
94}
djm@openbsd.org97f4d302017-04-30 23:13:25 +000095
Damien Millerabea8ee2002-01-22 23:27:11 +110096static void
97chan_set_ostate(Channel *c, u_int next)
98{
99 if (c->ostate > CHAN_OUTPUT_CLOSED || next > CHAN_OUTPUT_CLOSED)
100 fatal("chan_set_ostate: bad state %d -> %d", c->ostate, next);
Damien Millerfbdeece2003-09-02 22:52:31 +1000101 debug2("channel %d: output %s -> %s", c->self, ostates[c->ostate],
Damien Millerabea8ee2002-01-22 23:27:11 +1100102 ostates[next]);
103 c->ostate = next;
104}
105
Damien Miller5144df92002-01-22 23:28:45 +1100106void
107chan_read_failed(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100108{
Damien Millerfbdeece2003-09-02 22:52:31 +1000109 debug2("channel %d: read failed", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100110 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000111 case CHAN_INPUT_OPEN:
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000112 chan_shutdown_read(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100113 chan_set_istate(c, CHAN_INPUT_WAIT_DRAIN);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000114 break;
115 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000116 error("channel %d: chan_read_failed for istate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000117 c->self, c->istate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000118 break;
119 }
120}
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000121
Damien Miller5144df92002-01-22 23:28:45 +1100122void
123chan_ibuf_empty(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100124{
Damien Millerfbdeece2003-09-02 22:52:31 +1000125 debug2("channel %d: ibuf empty", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100126 if (buffer_len(&c->input)) {
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000127 error("channel %d: chan_ibuf_empty for non empty buffer",
Damien Miller33b13562000-04-04 14:38:59 +1000128 c->self);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000129 return;
130 }
Damien Miller95def091999-11-25 00:26:21 +1100131 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000132 case CHAN_INPUT_WAIT_DRAIN:
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000133 if (!(c->flags & (CHAN_CLOSE_SENT|CHAN_LOCAL)))
134 chan_send_eof2(c);
135 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000136 break;
137 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000138 error("channel %d: chan_ibuf_empty for istate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000139 c->self, c->istate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000140 break;
141 }
142}
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000143
Damien Miller5144df92002-01-22 23:28:45 +1100144void
145chan_obuf_empty(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100146{
Damien Millerfbdeece2003-09-02 22:52:31 +1000147 debug2("channel %d: obuf empty", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100148 if (buffer_len(&c->output)) {
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000149 error("channel %d: chan_obuf_empty for non empty buffer",
Damien Miller33b13562000-04-04 14:38:59 +1000150 c->self);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000151 return;
152 }
Damien Miller95def091999-11-25 00:26:21 +1100153 switch (c->ostate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000154 case CHAN_OUTPUT_WAIT_DRAIN:
Damien Miller89a92322002-01-22 23:27:28 +1100155 chan_shutdown_write(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100156 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000157 break;
158 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000159 error("channel %d: internal error: obuf_empty for ostate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000160 c->self, c->ostate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000161 break;
162 }
163}
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000164
165void
166chan_rcvd_eow(Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100167{
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000168 debug2("channel %d: rcvd eow", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100169 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000170 case CHAN_INPUT_OPEN:
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000171 chan_shutdown_read(c);
172 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000173 break;
174 }
175}
Damien Miller33b13562000-04-04 14:38:59 +1000176
Damien Miller33b13562000-04-04 14:38:59 +1000177static void
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000178chan_send_eof2(Channel *c)
179{
180 debug2("channel %d: send eof", c->self);
181 switch (c->istate) {
182 case CHAN_INPUT_WAIT_DRAIN:
183 packet_start(SSH2_MSG_CHANNEL_EOF);
184 packet_put_int(c->remote_id);
185 packet_send();
186 c->flags |= CHAN_EOF_SENT;
187 break;
188 default:
189 error("channel %d: cannot send eof for istate %d",
190 c->self, c->istate);
191 break;
192 }
193}
194
195static void
196chan_send_close2(Channel *c)
197{
198 debug2("channel %d: send close", c->self);
199 if (c->ostate != CHAN_OUTPUT_CLOSED ||
200 c->istate != CHAN_INPUT_CLOSED) {
201 error("channel %d: cannot send close for istate/ostate %d/%d",
202 c->self, c->istate, c->ostate);
203 } else if (c->flags & CHAN_CLOSE_SENT) {
204 error("channel %d: already sent close", c->self);
205 } else {
206 packet_start(SSH2_MSG_CHANNEL_CLOSE);
207 packet_put_int(c->remote_id);
208 packet_send();
209 c->flags |= CHAN_CLOSE_SENT;
210 }
211}
212
213static void
214chan_send_eow2(Channel *c)
215{
216 debug2("channel %d: send eow", c->self);
217 if (c->ostate == CHAN_OUTPUT_CLOSED) {
218 error("channel %d: must not sent eow on closed output",
219 c->self);
220 return;
221 }
222 if (!(datafellows & SSH_NEW_OPENSSH))
223 return;
224 packet_start(SSH2_MSG_CHANNEL_REQUEST);
225 packet_put_int(c->remote_id);
226 packet_put_cstring("eow@openssh.com");
227 packet_put_char(0);
228 packet_send();
229}
230
231/* shared */
232
233void
234chan_rcvd_ieof(Channel *c)
235{
236 debug2("channel %d: rcvd eof", c->self);
237 c->flags |= CHAN_EOF_RCVD;
238 if (c->ostate == CHAN_OUTPUT_OPEN)
239 chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
240 if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN &&
241 buffer_len(&c->output) == 0 &&
242 !CHANNEL_EFD_OUTPUT_ACTIVE(c))
243 chan_obuf_empty(c);
244}
245
246void
247chan_rcvd_oclose(Channel *c)
Damien Miller33b13562000-04-04 14:38:59 +1000248{
Damien Millerfbdeece2003-09-02 22:52:31 +1000249 debug2("channel %d: rcvd close", c->self);
Damien Millere1537f92010-01-26 13:26:22 +1100250 if (!(c->flags & CHAN_LOCAL)) {
251 if (c->flags & CHAN_CLOSE_RCVD)
252 error("channel %d: protocol error: close rcvd twice",
253 c->self);
254 c->flags |= CHAN_CLOSE_RCVD;
255 }
Damien Miller33b13562000-04-04 14:38:59 +1000256 if (c->type == SSH_CHANNEL_LARVAL) {
257 /* tear down larval channels immediately */
Damien Millerabea8ee2002-01-22 23:27:11 +1100258 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
259 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Miller33b13562000-04-04 14:38:59 +1000260 return;
261 }
262 switch (c->ostate) {
263 case CHAN_OUTPUT_OPEN:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000264 /*
265 * wait until a data from the channel is consumed if a CLOSE
266 * is received
267 */
Damien Millerabea8ee2002-01-22 23:27:11 +1100268 chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
Damien Miller33b13562000-04-04 14:38:59 +1000269 break;
270 }
271 switch (c->istate) {
272 case CHAN_INPUT_OPEN:
Damien Miller33b13562000-04-04 14:38:59 +1000273 chan_shutdown_read(c);
Damien Millerebc11d32002-01-22 23:28:13 +1100274 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Miller33b13562000-04-04 14:38:59 +1000275 break;
276 case CHAN_INPUT_WAIT_DRAIN:
Damien Millere1537f92010-01-26 13:26:22 +1100277 if (!(c->flags & CHAN_LOCAL))
278 chan_send_eof2(c);
Damien Millerebc11d32002-01-22 23:28:13 +1100279 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Miller33b13562000-04-04 14:38:59 +1000280 break;
281 }
Damien Miller33b13562000-04-04 14:38:59 +1000282}
Damien Millere1537f92010-01-26 13:26:22 +1100283
Damien Millerbab9bd42008-05-19 16:06:47 +1000284void
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000285chan_write_failed(Channel *c)
Damien Miller33b13562000-04-04 14:38:59 +1000286{
Damien Millerfbdeece2003-09-02 22:52:31 +1000287 debug2("channel %d: write failed", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000288 switch (c->ostate) {
289 case CHAN_OUTPUT_OPEN:
Damien Miller33b13562000-04-04 14:38:59 +1000290 case CHAN_OUTPUT_WAIT_DRAIN:
Damien Miller33b13562000-04-04 14:38:59 +1000291 chan_shutdown_write(c);
Darren Tucker8748b962008-07-02 22:32:43 +1000292 if (strcmp(c->ctype, "session") == 0)
293 chan_send_eow2(c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100294 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
Damien Miller33b13562000-04-04 14:38:59 +1000295 break;
296 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000297 error("channel %d: chan_write_failed for ostate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000298 c->self, c->ostate);
299 break;
300 }
301}
Damien Miller5144df92002-01-22 23:28:45 +1100302
303void
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000304chan_mark_dead(Channel *c)
305{
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000306 c->type = SSH_CHANNEL_ZOMBIE;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000307}
308
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000309int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000310chan_is_dead(Channel *c, int do_send)
Damien Miller33b13562000-04-04 14:38:59 +1000311{
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000312 if (c->type == SSH_CHANNEL_ZOMBIE) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000313 debug2("channel %d: zombie", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000314 return 1;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000315 }
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000316 if (c->istate != CHAN_INPUT_CLOSED || c->ostate != CHAN_OUTPUT_CLOSED)
317 return 0;
Ben Lindstromcf159442002-03-26 03:26:24 +0000318 if ((datafellows & SSH_BUG_EXTEOF) &&
319 c->extended_usage == CHAN_EXTENDED_WRITE &&
320 c->efd != -1 &&
321 buffer_len(&c->extended) > 0) {
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000322 debug2("channel %d: active efd: %d len %d",
323 c->self, c->efd, buffer_len(&c->extended));
Ben Lindstromcf159442002-03-26 03:26:24 +0000324 return 0;
325 }
Damien Millere1537f92010-01-26 13:26:22 +1100326 if (c->flags & CHAN_LOCAL) {
327 debug2("channel %d: is dead (local)", c->self);
328 return 1;
329 }
Ben Lindstromcf159442002-03-26 03:26:24 +0000330 if (!(c->flags & CHAN_CLOSE_SENT)) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000331 if (do_send) {
Ben Lindstromcf159442002-03-26 03:26:24 +0000332 chan_send_close2(c);
333 } else {
334 /* channel would be dead if we sent a close */
335 if (c->flags & CHAN_CLOSE_RCVD) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000336 debug2("channel %d: almost dead",
Ben Lindstromcf159442002-03-26 03:26:24 +0000337 c->self);
338 return 1;
Damien Miller3ec27592001-10-12 11:35:04 +1000339 }
Damien Miller33b13562000-04-04 14:38:59 +1000340 }
Ben Lindstromcf159442002-03-26 03:26:24 +0000341 }
342 if ((c->flags & CHAN_CLOSE_SENT) &&
343 (c->flags & CHAN_CLOSE_RCVD)) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000344 debug2("channel %d: is dead", c->self);
Ben Lindstromcf159442002-03-26 03:26:24 +0000345 return 1;
Damien Miller33b13562000-04-04 14:38:59 +1000346 }
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000347 return 0;
Damien Miller33b13562000-04-04 14:38:59 +1000348}
349
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000350/* helper */
351static void
Damien Miller95def091999-11-25 00:26:21 +1100352chan_shutdown_write(Channel *c)
353{
Damien Miller76765c02002-01-22 23:21:15 +1100354 buffer_clear(&c->output);
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000355 if (c->type == SSH_CHANNEL_LARVAL)
Damien Miller33b13562000-04-04 14:38:59 +1000356 return;
Damien Miller5428f641999-11-25 11:54:57 +1100357 /* shutdown failure is allowed if write failed already */
Damien Millerfbdeece2003-09-02 22:52:31 +1000358 debug2("channel %d: close_write", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000359 if (c->sock != -1) {
360 if (shutdown(c->sock, SHUT_WR) < 0)
Damien Millerfbdeece2003-09-02 22:52:31 +1000361 debug2("channel %d: chan_shutdown_write: "
Darren Tuckerb57fab62008-11-11 16:32:25 +1100362 "shutdown() failed for fd %d: %.100s",
Damien Miller33b13562000-04-04 14:38:59 +1000363 c->self, c->sock, strerror(errno));
364 } else {
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000365 if (channel_close_fd(&c->wfd) < 0)
Damien Miller996acd22003-04-09 20:59:48 +1000366 logit("channel %d: chan_shutdown_write: "
Darren Tuckerb57fab62008-11-11 16:32:25 +1100367 "close() failed for fd %d: %.100s",
Damien Miller33b13562000-04-04 14:38:59 +1000368 c->self, c->wfd, strerror(errno));
Damien Miller33b13562000-04-04 14:38:59 +1000369 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000370}
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000371
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000372static void
Damien Miller95def091999-11-25 00:26:21 +1100373chan_shutdown_read(Channel *c)
374{
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000375 if (c->type == SSH_CHANNEL_LARVAL)
Damien Miller33b13562000-04-04 14:38:59 +1000376 return;
Damien Millerfbdeece2003-09-02 22:52:31 +1000377 debug2("channel %d: close_read", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000378 if (c->sock != -1) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000379 /*
Damien Miller0f091bd2000-08-07 15:47:48 +1000380 * shutdown(sock, SHUT_READ) may return ENOTCONN if the
381 * write side has been closed already. (bug on Linux)
Kevin Steves871f6622001-09-18 16:08:24 +0000382 * HP-UX may return ENOTCONN also.
Damien Miller0f091bd2000-08-07 15:47:48 +1000383 */
384 if (shutdown(c->sock, SHUT_RD) < 0
Kevin Steves871f6622001-09-18 16:08:24 +0000385 && errno != ENOTCONN)
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000386 error("channel %d: chan_shutdown_read: "
Darren Tuckerb57fab62008-11-11 16:32:25 +1100387 "shutdown() failed for fd %d [i%d o%d]: %.100s",
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000388 c->self, c->sock, c->istate, c->ostate,
Kevin Stevesc3422eb2001-09-20 19:33:33 +0000389 strerror(errno));
Damien Miller33b13562000-04-04 14:38:59 +1000390 } else {
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000391 if (channel_close_fd(&c->rfd) < 0)
Damien Miller996acd22003-04-09 20:59:48 +1000392 logit("channel %d: chan_shutdown_read: "
Darren Tuckerb57fab62008-11-11 16:32:25 +1100393 "close() failed for fd %d: %.100s",
Damien Miller33b13562000-04-04 14:38:59 +1000394 c->self, c->rfd, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000395 }
396}