Lines Matching refs:c
139 static void port_open_helper(Channel *c, char *rtype);
146 Channel *c; in channel_lookup() local
152 c = channels[id]; in channel_lookup()
153 if (c == NULL) { in channel_lookup()
157 return c; in channel_lookup()
166 channel_register_fds(Channel *c, int rfd, int wfd, int efd, in channel_register_fds() argument
176 c->rfd = rfd; in channel_register_fds()
177 c->wfd = wfd; in channel_register_fds()
178 c->sock = (rfd == wfd) ? rfd : -1; in channel_register_fds()
179 c->efd = efd; in channel_register_fds()
180 c->extended_usage = extusage; in channel_register_fds()
183 if (nonblock && isatty(c->rfd)) { in channel_register_fds()
184 debug("channel %d: rfd %d isatty", c->self, c->rfd); in channel_register_fds()
185 c->isatty = 1; in channel_register_fds()
186 if (!isatty(c->wfd)) { in channel_register_fds()
188 c->self, c->wfd); in channel_register_fds()
191 c->isatty = 0; in channel_register_fds()
193 c->wfd_isatty = isatty(c->wfd); in channel_register_fds()
216 Channel *c; in channel_new() local
247 c = channels[found] = xmalloc(sizeof(Channel)); in channel_new()
248 memset(c, 0, sizeof(Channel)); in channel_new()
249 buffer_init(&c->input); in channel_new()
250 buffer_init(&c->output); in channel_new()
251 buffer_init(&c->extended); in channel_new()
252 c->ostate = CHAN_OUTPUT_OPEN; in channel_new()
253 c->istate = CHAN_INPUT_OPEN; in channel_new()
254 c->flags = 0; in channel_new()
255 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock); in channel_new()
256 c->self = found; in channel_new()
257 c->type = type; in channel_new()
258 c->ctype = ctype; in channel_new()
259 c->local_window = window; in channel_new()
260 c->local_window_max = window; in channel_new()
261 c->local_consumed = 0; in channel_new()
262 c->local_maxpacket = maxpack; in channel_new()
263 c->remote_id = -1; in channel_new()
264 c->remote_name = remote_name; in channel_new()
265 c->remote_window = 0; in channel_new()
266 c->remote_maxpacket = 0; in channel_new()
267 c->force_drain = 0; in channel_new()
268 c->single_connection = 0; in channel_new()
269 c->detach_user = NULL; in channel_new()
270 c->confirm = NULL; in channel_new()
271 c->input_filter = NULL; in channel_new()
272 c->delayed = 1; /* prevent call to channel_post handler */ in channel_new()
274 return c; in channel_new()
281 Channel *c; in channel_find_maxfd() local
284 c = channels[i]; in channel_find_maxfd()
285 if (c != NULL) { in channel_find_maxfd()
286 max = MAX(max, c->rfd); in channel_find_maxfd()
287 max = MAX(max, c->wfd); in channel_find_maxfd()
288 max = MAX(max, c->efd); in channel_find_maxfd()
311 channel_close_fds(Channel *c) in channel_close_fds() argument
314 c->self, c->rfd, c->wfd, c->efd); in channel_close_fds()
316 channel_close_fd(&c->sock); in channel_close_fds()
317 channel_close_fd(&c->rfd); in channel_close_fds()
318 channel_close_fd(&c->wfd); in channel_close_fds()
319 channel_close_fd(&c->efd); in channel_close_fds()
325 channel_free(Channel *c) in channel_free() argument
333 debug("channel_free: channel %d: %s, nchannels %d", c->self, in channel_free()
334 c->remote_name ? c->remote_name : "???", n); in channel_free()
340 if (c->sock != -1) in channel_free()
341 shutdown(c->sock, SHUT_RDWR); in channel_free()
342 channel_close_fds(c); in channel_free()
343 buffer_free(&c->input); in channel_free()
344 buffer_free(&c->output); in channel_free()
345 buffer_free(&c->extended); in channel_free()
346 if (c->remote_name) { in channel_free()
347 xfree(c->remote_name); in channel_free()
348 c->remote_name = NULL; in channel_free()
350 channels[c->self] = NULL; in channel_free()
351 xfree(c); in channel_free()
387 Channel *c; in channel_stop_listening() local
390 c = channels[i]; in channel_stop_listening()
391 if (c != NULL) { in channel_stop_listening()
392 switch (c->type) { in channel_stop_listening()
397 channel_close_fd(&c->sock); in channel_stop_listening()
398 channel_free(c); in channel_stop_listening()
414 Channel *c; in channel_not_very_much_buffered_data() local
417 c = channels[i]; in channel_not_very_much_buffered_data()
418 if (c != NULL && c->type == SSH_CHANNEL_OPEN) { in channel_not_very_much_buffered_data()
421 buffer_len(&c->input) > packet_get_maxsize()) { in channel_not_very_much_buffered_data()
423 c->self, buffer_len(&c->input)); in channel_not_very_much_buffered_data()
427 if (buffer_len(&c->output) > packet_get_maxsize()) { in channel_not_very_much_buffered_data()
429 c->self, buffer_len(&c->output), in channel_not_very_much_buffered_data()
444 Channel *c; in channel_still_open() local
447 c = channels[i]; in channel_still_open()
448 if (c == NULL) in channel_still_open()
450 switch (c->type) { in channel_still_open()
474 fatal("channel_still_open: bad channel type %d", c->type); in channel_still_open()
487 Channel *c; in channel_find_open() local
490 c = channels[i]; in channel_find_open()
491 if (c == NULL) in channel_find_open()
493 switch (c->type) { in channel_find_open()
514 fatal("channel_find_open: bad channel type %d", c->type); in channel_find_open()
532 Channel *c; in channel_open_message() local
540 c = channels[i]; in channel_open_message()
541 if (c == NULL) in channel_open_message()
543 switch (c->type) { in channel_open_message()
560 c->self, c->remote_name, in channel_open_message()
561 c->type, c->remote_id, in channel_open_message()
562 c->istate, buffer_len(&c->input), in channel_open_message()
563 c->ostate, buffer_len(&c->output), in channel_open_message()
564 c->rfd, c->wfd); in channel_open_message()
568 fatal("channel_open_message: bad channel type %d", c->type); in channel_open_message()
581 Channel *c = channel_lookup(id); in channel_send_open() local
583 if (c == NULL) { in channel_send_open()
589 packet_put_cstring(c->ctype); in channel_send_open()
590 packet_put_int(c->self); in channel_send_open()
591 packet_put_int(c->local_window); in channel_send_open()
592 packet_put_int(c->local_maxpacket); in channel_send_open()
599 Channel *c = channel_lookup(local_id); in channel_request_start() local
602 if (c == NULL) { in channel_request_start()
607 packet_put_int(c->remote_id); in channel_request_start()
614 Channel *c = channel_lookup(id); in channel_register_confirm() local
616 if (c == NULL) { in channel_register_confirm()
620 c->confirm = fn; in channel_register_confirm()
625 Channel *c = channel_lookup(id); in channel_register_cleanup() local
627 if (c == NULL) { in channel_register_cleanup()
631 c->detach_user = fn; in channel_register_cleanup()
636 Channel *c = channel_lookup(id); in channel_cancel_cleanup() local
638 if (c == NULL) { in channel_cancel_cleanup()
642 c->detach_user = NULL; in channel_cancel_cleanup()
647 Channel *c = channel_lookup(id); in channel_register_filter() local
649 if (c == NULL) { in channel_register_filter()
653 c->input_filter = fn; in channel_register_filter()
660 Channel *c = channel_lookup(id); in channel_set_fds() local
662 if (c == NULL || c->type != SSH_CHANNEL_LARVAL) in channel_set_fds()
664 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock); in channel_set_fds()
665 c->type = SSH_CHANNEL_OPEN; in channel_set_fds()
666 c->local_window = c->local_window_max = window_max; in channel_set_fds()
668 packet_put_int(c->remote_id); in channel_set_fds()
669 packet_put_int(c->local_window); in channel_set_fds()
676 Channel *c = channel_lookup(id); in channel_set_wait_for_exit() local
678 if (c == NULL || c->type != SSH_CHANNEL_OPEN) in channel_set_wait_for_exit()
681 debug3("channel_set_wait_for_exit %d, %d (type: %d)", id, wait_for_exit, c->type); in channel_set_wait_for_exit()
682 c->wait_for_exit = wait_for_exit; in channel_set_wait_for_exit()
693 typedef void chan_fn(Channel *c, fd_set * readset, fd_set * writeset);
698 channel_pre_listener(Channel *c, fd_set * readset, fd_set * writeset) in channel_pre_listener() argument
700 FD_SET(c->sock, readset); in channel_pre_listener()
704 channel_pre_connecting(Channel *c, fd_set * readset, fd_set * writeset) in channel_pre_connecting() argument
706 debug3("channel %d: waiting for connection", c->self); in channel_pre_connecting()
707 FD_SET(c->sock, writeset); in channel_pre_connecting()
711 channel_pre_open_13(Channel *c, fd_set * readset, fd_set * writeset) in channel_pre_open_13() argument
713 if (buffer_len(&c->input) < packet_get_maxsize()) in channel_pre_open_13()
714 FD_SET(c->sock, readset); in channel_pre_open_13()
715 if (buffer_len(&c->output) > 0) in channel_pre_open_13()
716 FD_SET(c->sock, writeset); in channel_pre_open_13()
720 channel_pre_open(Channel *c, fd_set * readset, fd_set * writeset) in channel_pre_open() argument
722 u_int limit = compat20 ? c->remote_window : packet_get_maxsize(); in channel_pre_open()
724 if (c->istate == CHAN_INPUT_OPEN && in channel_pre_open()
726 buffer_len(&c->input) < limit && in channel_pre_open()
727 buffer_check_alloc(&c->input, CHAN_RBUF)) in channel_pre_open()
728 FD_SET(c->rfd, readset); in channel_pre_open()
729 if (c->ostate == CHAN_OUTPUT_OPEN || in channel_pre_open()
730 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) { in channel_pre_open()
731 if (buffer_len(&c->output) > 0) { in channel_pre_open()
732 FD_SET(c->wfd, writeset); in channel_pre_open()
733 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) { in channel_pre_open()
734 if (CHANNEL_EFD_OUTPUT_ACTIVE(c)) in channel_pre_open()
736 c->self, c->efd, buffer_len(&c->extended)); in channel_pre_open()
738 chan_obuf_empty(c); in channel_pre_open()
742 if (compat20 && c->efd != -1) { in channel_pre_open()
743 if (c->extended_usage == CHAN_EXTENDED_WRITE && in channel_pre_open()
744 buffer_len(&c->extended) > 0) in channel_pre_open()
745 FD_SET(c->efd, writeset); in channel_pre_open()
746 else if (!(c->flags & CHAN_EOF_SENT) && in channel_pre_open()
747 c->extended_usage == CHAN_EXTENDED_READ && in channel_pre_open()
748 buffer_len(&c->extended) < c->remote_window) in channel_pre_open()
749 FD_SET(c->efd, readset); in channel_pre_open()
754 channel_pre_input_draining(Channel *c, fd_set * readset, fd_set * writeset) in channel_pre_input_draining() argument
756 if (buffer_len(&c->input) == 0) { in channel_pre_input_draining()
758 packet_put_int(c->remote_id); in channel_pre_input_draining()
760 c->type = SSH_CHANNEL_CLOSED; in channel_pre_input_draining()
761 debug("channel %d: closing after input drain.", c->self); in channel_pre_input_draining()
766 channel_pre_output_draining(Channel *c, fd_set * readset, fd_set * writeset) in channel_pre_output_draining() argument
768 if (buffer_len(&c->output) == 0) in channel_pre_output_draining()
769 chan_mark_dead(c); in channel_pre_output_draining()
771 FD_SET(c->sock, writeset); in channel_pre_output_draining()
842 channel_pre_x11_open_13(Channel *c, fd_set * readset, fd_set * writeset) in channel_pre_x11_open_13() argument
844 int ret = x11_open_helper(&c->output); in channel_pre_x11_open_13()
848 c->type = SSH_CHANNEL_OPEN; in channel_pre_x11_open_13()
849 channel_pre_open_13(c, readset, writeset); in channel_pre_x11_open_13()
856 buffer_clear(&c->input); in channel_pre_x11_open_13()
857 buffer_clear(&c->output); in channel_pre_x11_open_13()
858 channel_close_fd(&c->sock); in channel_pre_x11_open_13()
859 c->sock = -1; in channel_pre_x11_open_13()
860 c->type = SSH_CHANNEL_CLOSED; in channel_pre_x11_open_13()
862 packet_put_int(c->remote_id); in channel_pre_x11_open_13()
868 channel_pre_x11_open(Channel *c, fd_set * readset, fd_set * writeset) in channel_pre_x11_open() argument
870 int ret = x11_open_helper(&c->output); in channel_pre_x11_open()
875 c->type = SSH_CHANNEL_OPEN; in channel_pre_x11_open()
876 channel_pre_open(c, readset, writeset); in channel_pre_x11_open()
879 debug("X11 rejected %d i%d/o%d", c->self, c->istate, c->ostate); in channel_pre_x11_open()
880 chan_read_failed(c); in channel_pre_x11_open()
881 buffer_clear(&c->input); in channel_pre_x11_open()
882 chan_ibuf_empty(c); in channel_pre_x11_open()
883 buffer_clear(&c->output); in channel_pre_x11_open()
886 chan_write_failed(c); in channel_pre_x11_open()
888 c->type = SSH_CHANNEL_OPEN; in channel_pre_x11_open()
889 debug("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate); in channel_pre_x11_open()
895 channel_decode_socks4(Channel *c, fd_set * readset, fd_set * writeset) in channel_decode_socks4() argument
907 debug2("channel %d: decode socks4", c->self); in channel_decode_socks4()
909 have = buffer_len(&c->input); in channel_decode_socks4()
913 p = buffer_ptr(&c->input); in channel_decode_socks4()
922 c->self); in channel_decode_socks4()
928 buffer_get(&c->input, (char *)&s4_req.version, 1); in channel_decode_socks4()
929 buffer_get(&c->input, (char *)&s4_req.command, 1); in channel_decode_socks4()
930 buffer_get(&c->input, (char *)&s4_req.dest_port, 2); in channel_decode_socks4()
931 buffer_get(&c->input, (char *)&s4_req.dest_addr, 4); in channel_decode_socks4()
932 have = buffer_len(&c->input); in channel_decode_socks4()
933 p = buffer_ptr(&c->input); in channel_decode_socks4()
935 debug2("channel %d: decode socks4: user %s/%d", c->self, p, len); in channel_decode_socks4()
938 c->self, len, have); in channel_decode_socks4()
940 buffer_consume(&c->input, len); in channel_decode_socks4()
941 buffer_consume(&c->input, 1); /* trailing '\0' */ in channel_decode_socks4()
944 strlcpy(c->path, host, sizeof(c->path)); in channel_decode_socks4()
945 c->host_port = ntohs(s4_req.dest_port); in channel_decode_socks4()
948 c->self, host, c->host_port, s4_req.command); in channel_decode_socks4()
952 c->self, s4_req.command); in channel_decode_socks4()
959 buffer_append(&c->output, (char *)&s4_rsp, sizeof(s4_rsp)); in channel_decode_socks4()
974 channel_decode_socks5(Channel *c, fd_set *readset, fd_set *writeset) in channel_decode_socks5() argument
988 debug2("channel %d: decode socks5", c->self); in channel_decode_socks5()
989 p = buffer_ptr(&c->input); in channel_decode_socks5()
992 have = buffer_len(&c->input); in channel_decode_socks5()
993 if (!(c->flags & SSH_SOCKS5_AUTHDONE)) { in channel_decode_socks5()
1009 c->self); in channel_decode_socks5()
1011 "SSH_SOCKS5_NOAUTH method not found", c->self); in channel_decode_socks5()
1014 buffer_consume(&c->input, nmethods + 2); in channel_decode_socks5()
1015 buffer_put_char(&c->output, 0x05); /* version */ in channel_decode_socks5()
1016 buffer_put_char(&c->output, SSH_SOCKS5_NOAUTH); /* method */ in channel_decode_socks5()
1017 FD_SET(c->sock, writeset); in channel_decode_socks5()
1018 c->flags |= SSH_SOCKS5_AUTHDONE; in channel_decode_socks5()
1019 debug2("channel %d: socks5 auth done", c->self); in channel_decode_socks5()
1022 debug2("channel %d: socks5 post auth", c->self); in channel_decode_socks5()
1030 "only socks5 connect is supported", c->self); in channel_decode_socks5()
1048 "bad socks5 atyp %d", c->self, s5_req.atyp); in channel_decode_socks5()
1056 buffer_consume(&c->input, sizeof(s5_req)); in channel_decode_socks5()
1058 buffer_consume(&c->input, 1); /* host string length */ in channel_decode_socks5()
1059 buffer_get(&c->input, (char *)&dest_addr, addrlen); in channel_decode_socks5()
1060 buffer_get(&c->input, (char *)&dest_port, 2); in channel_decode_socks5()
1063 strlcpy(c->path, (char *)dest_addr, sizeof(c->path)); in channel_decode_socks5()
1064 else if (inet_ntop(af, dest_addr, c->path, sizeof(c->path)) == NULL) in channel_decode_socks5()
1066 c->host_port = ntohs(dest_port); in channel_decode_socks5()
1069 c->self, c->path, c->host_port, s5_req.command); in channel_decode_socks5()
1079 buffer_append(&c->output, &s5_rsp, sizeof(s5_rsp)); in channel_decode_socks5()
1080 buffer_append(&c->output, &bnd_addr, sizeof(struct in_addr)); in channel_decode_socks5()
1081 buffer_append(&c->output, &dest_port, sizeof(dest_port)); in channel_decode_socks5()
1087 channel_pre_dynamic(Channel *c, fd_set * readset, fd_set * writeset) in channel_pre_dynamic() argument
1092 have = buffer_len(&c->input); in channel_pre_dynamic()
1093 debug2("channel %d: pre_dynamic: have %d", c->self, have); in channel_pre_dynamic()
1098 FD_SET(c->sock, readset); in channel_pre_dynamic()
1102 p = buffer_ptr(&c->input); in channel_pre_dynamic()
1105 ret = channel_decode_socks4(c, readset, writeset); in channel_pre_dynamic()
1108 ret = channel_decode_socks5(c, readset, writeset); in channel_pre_dynamic()
1112 "version 0x%02X", c->self, p[0]); in channel_pre_dynamic()
1117 chan_mark_dead(c); in channel_pre_dynamic()
1119 debug2("channel %d: pre_dynamic: need more", c->self); in channel_pre_dynamic()
1121 FD_SET(c->sock, readset); in channel_pre_dynamic()
1124 c->type = SSH_CHANNEL_OPENING; in channel_pre_dynamic()
1125 port_open_helper(c, "direct-tcpip"); in channel_pre_dynamic()
1131 channel_post_x11_listener(Channel *c, fd_set * readset, fd_set * writeset) in channel_post_x11_listener() argument
1140 if (FD_ISSET(c->sock, readset)) { in channel_post_x11_listener()
1143 newsock = accept(c->sock, &addr, &addrlen); in channel_post_x11_listener()
1144 if (c->single_connection) { in channel_post_x11_listener()
1146 channel_close_fd(&c->sock); in channel_post_x11_listener()
1147 chan_mark_dead(c); in channel_post_x11_listener()
1161 c->local_window_max, c->local_maxpacket, in channel_post_x11_listener()
1190 port_open_helper(Channel *c, char *rtype) in port_open_helper() argument
1194 char *remote_ipaddr = get_peer_ipaddr(c->sock); in port_open_helper()
1195 u_short remote_port = get_peer_port(c->sock); in port_open_helper()
1202 rtype, c->listening_port, c->path, c->host_port, in port_open_helper()
1205 xfree(c->remote_name); in port_open_helper()
1206 c->remote_name = xstrdup(buf); in port_open_helper()
1211 packet_put_int(c->self); in port_open_helper()
1212 packet_put_int(c->local_window_max); in port_open_helper()
1213 packet_put_int(c->local_maxpacket); in port_open_helper()
1216 packet_put_cstring(c->path); in port_open_helper()
1217 packet_put_int(c->host_port); in port_open_helper()
1220 packet_put_cstring(c->path); in port_open_helper()
1221 packet_put_int(c->listening_port); in port_open_helper()
1229 packet_put_int(c->self); in port_open_helper()
1230 packet_put_cstring(c->path); in port_open_helper()
1231 packet_put_int(c->host_port); in port_open_helper()
1234 packet_put_cstring(c->remote_name); in port_open_helper()
1244 channel_post_port_listener(Channel *c, fd_set * readset, fd_set * writeset) in channel_post_port_listener() argument
1252 if (FD_ISSET(c->sock, readset)) { in channel_post_port_listener()
1255 c->listening_port, c->path, c->host_port); in channel_post_port_listener()
1257 if (c->type == SSH_CHANNEL_RPORT_LISTENER) { in channel_post_port_listener()
1261 if (c->host_port == 0) { in channel_post_port_listener()
1271 newsock = accept(c->sock, &addr, &addrlen); in channel_post_port_listener()
1279 c->local_window_max, c->local_maxpacket, in channel_post_port_listener()
1281 nc->listening_port = c->listening_port; in channel_post_port_listener()
1282 nc->host_port = c->host_port; in channel_post_port_listener()
1283 strlcpy(nc->path, c->path, sizeof(nc->path)); in channel_post_port_listener()
1295 channel_post_auth_listener(Channel *c, fd_set * readset, fd_set * writeset) in channel_post_auth_listener() argument
1303 if (FD_ISSET(c->sock, readset)) { in channel_post_auth_listener()
1305 newsock = accept(c->sock, &addr, &addrlen); in channel_post_auth_listener()
1313 c->local_window_max, c->local_maxpacket, in channel_post_auth_listener()
1319 packet_put_int(c->local_window_max); in channel_post_auth_listener()
1320 packet_put_int(c->local_maxpacket); in channel_post_auth_listener()
1330 channel_post_connecting(Channel *c, fd_set * readset, fd_set * writeset) in channel_post_connecting() argument
1335 if (FD_ISSET(c->sock, writeset)) { in channel_post_connecting()
1336 if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, &err, &sz) < 0) { in channel_post_connecting()
1341 debug("channel %d: connected", c->self); in channel_post_connecting()
1342 c->type = SSH_CHANNEL_OPEN; in channel_post_connecting()
1345 packet_put_int(c->remote_id); in channel_post_connecting()
1346 packet_put_int(c->self); in channel_post_connecting()
1347 packet_put_int(c->local_window); in channel_post_connecting()
1348 packet_put_int(c->local_maxpacket); in channel_post_connecting()
1351 packet_put_int(c->remote_id); in channel_post_connecting()
1352 packet_put_int(c->self); in channel_post_connecting()
1356 c->self, strerror(err)); in channel_post_connecting()
1359 packet_put_int(c->remote_id); in channel_post_connecting()
1367 packet_put_int(c->remote_id); in channel_post_connecting()
1369 chan_mark_dead(c); in channel_post_connecting()
1376 channel_handle_rfd(Channel *c, fd_set * readset, fd_set * writeset) in channel_handle_rfd() argument
1381 if (c->rfd != -1 && in channel_handle_rfd()
1382 FD_ISSET(c->rfd, readset)) { in channel_handle_rfd()
1383 len = read(c->rfd, buf, sizeof(buf)); in channel_handle_rfd()
1388 c->self, c->rfd, len); in channel_handle_rfd()
1389 if (c->type != SSH_CHANNEL_OPEN) { in channel_handle_rfd()
1390 debug("channel %d: not open", c->self); in channel_handle_rfd()
1391 chan_mark_dead(c); in channel_handle_rfd()
1394 buffer_clear(&c->output); in channel_handle_rfd()
1395 c->type = SSH_CHANNEL_INPUT_DRAINING; in channel_handle_rfd()
1396 debug("channel %d: input draining.", c->self); in channel_handle_rfd()
1398 chan_read_failed(c); in channel_handle_rfd()
1402 if (c->input_filter != NULL) { in channel_handle_rfd()
1403 if (c->input_filter(c, buf, len) == -1) { in channel_handle_rfd()
1404 debug("channel %d: filter stops", c->self); in channel_handle_rfd()
1405 chan_read_failed(c); in channel_handle_rfd()
1408 buffer_append(&c->input, buf, len); in channel_handle_rfd()
1414 channel_handle_wfd(Channel *c, fd_set * readset, fd_set * writeset) in channel_handle_wfd() argument
1422 if (c->wfd != -1 && in channel_handle_wfd()
1423 FD_ISSET(c->wfd, writeset) && in channel_handle_wfd()
1424 buffer_len(&c->output) > 0) { in channel_handle_wfd()
1425 data = buffer_ptr(&c->output); in channel_handle_wfd()
1426 dlen = buffer_len(&c->output); in channel_handle_wfd()
1429 if (compat20 && c->wfd_isatty && dlen > 8*1024) in channel_handle_wfd()
1432 len = write(c->wfd, data, dlen); in channel_handle_wfd()
1436 if (c->type != SSH_CHANNEL_OPEN) { in channel_handle_wfd()
1437 debug("channel %d: not open", c->self); in channel_handle_wfd()
1438 chan_mark_dead(c); in channel_handle_wfd()
1441 buffer_clear(&c->output); in channel_handle_wfd()
1442 debug("channel %d: input draining.", c->self); in channel_handle_wfd()
1443 c->type = SSH_CHANNEL_INPUT_DRAINING; in channel_handle_wfd()
1445 chan_write_failed(c); in channel_handle_wfd()
1449 if (compat20 && c->isatty && dlen >= 1 && data[0] != '\r') { in channel_handle_wfd()
1450 if (tcgetattr(c->wfd, &tio) == 0 && in channel_handle_wfd()
1462 buffer_consume(&c->output, len); in channel_handle_wfd()
1464 c->local_consumed += len; in channel_handle_wfd()
1470 channel_handle_efd(Channel *c, fd_set * readset, fd_set * writeset) in channel_handle_efd() argument
1476 if (c->efd != -1) { in channel_handle_efd()
1477 if (c->extended_usage == CHAN_EXTENDED_WRITE && in channel_handle_efd()
1478 FD_ISSET(c->efd, writeset) && in channel_handle_efd()
1479 buffer_len(&c->extended) > 0) { in channel_handle_efd()
1480 len = write(c->efd, buffer_ptr(&c->extended), in channel_handle_efd()
1481 buffer_len(&c->extended)); in channel_handle_efd()
1483 c->self, len, c->efd); in channel_handle_efd()
1488 c->self, c->efd); in channel_handle_efd()
1489 channel_close_fd(&c->efd); in channel_handle_efd()
1491 buffer_consume(&c->extended, len); in channel_handle_efd()
1492 c->local_consumed += len; in channel_handle_efd()
1494 } else if (c->extended_usage == CHAN_EXTENDED_READ && in channel_handle_efd()
1495 FD_ISSET(c->efd, readset)) { in channel_handle_efd()
1496 len = read(c->efd, buf, sizeof(buf)); in channel_handle_efd()
1498 c->self, len, c->efd); in channel_handle_efd()
1503 c->self, c->efd); in channel_handle_efd()
1504 channel_close_fd(&c->efd); in channel_handle_efd()
1506 buffer_append(&c->extended, buf, len); in channel_handle_efd()
1513 channel_check_window(Channel *c) in channel_check_window() argument
1515 if (c->type == SSH_CHANNEL_OPEN && in channel_check_window()
1516 !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) && in channel_check_window()
1517 c->local_window < c->local_window_max/2 && in channel_check_window()
1518 c->local_consumed > 0) { in channel_check_window()
1520 packet_put_int(c->remote_id); in channel_check_window()
1521 packet_put_int(c->local_consumed); in channel_check_window()
1524 c->self, c->local_window, in channel_check_window()
1525 c->local_consumed); in channel_check_window()
1526 c->local_window += c->local_consumed; in channel_check_window()
1527 c->local_consumed = 0; in channel_check_window()
1533 channel_post_open(Channel *c, fd_set * readset, fd_set * writeset) in channel_post_open() argument
1535 channel_handle_rfd(c, readset, writeset); in channel_post_open()
1536 channel_handle_wfd(c, readset, writeset); in channel_post_open()
1539 channel_handle_efd(c, readset, writeset); in channel_post_open()
1540 channel_check_window(c); in channel_post_open()
1544 channel_post_output_drain_13(Channel *c, fd_set * readset, fd_set * writeset) in channel_post_output_drain_13() argument
1549 if (FD_ISSET(c->sock, writeset) && buffer_len(&c->output) > 0) { in channel_post_output_drain_13()
1550 len = write(c->sock, buffer_ptr(&c->output), in channel_post_output_drain_13()
1551 buffer_len(&c->output)); in channel_post_output_drain_13()
1553 buffer_clear(&c->output); in channel_post_output_drain_13()
1555 buffer_consume(&c->output, len); in channel_post_output_drain_13()
1640 channel_garbage_collect(Channel *c) in channel_garbage_collect() argument
1642 if (c == NULL) in channel_garbage_collect()
1644 if (c->detach_user != NULL) { in channel_garbage_collect()
1645 if (!chan_is_dead(c, 0)) in channel_garbage_collect()
1647 debug("channel %d: gc: notify user", c->self); in channel_garbage_collect()
1648 c->detach_user(c->self, NULL); in channel_garbage_collect()
1650 if (c->detach_user != NULL) in channel_garbage_collect()
1652 debug("channel %d: gc: user detached", c->self); in channel_garbage_collect()
1654 if (!c->wait_for_exit && !chan_is_dead(c, 1)) in channel_garbage_collect()
1656 debug("channel %d: garbage collecting", c->self); in channel_garbage_collect()
1657 channel_free(c); in channel_garbage_collect()
1665 Channel *c; in channel_handler() local
1672 c = channels[i]; in channel_handler()
1673 if (c == NULL) in channel_handler()
1675 if (c->delayed) { in channel_handler()
1677 c->delayed = 0; in channel_handler()
1681 if (ftab[c->type] != NULL) in channel_handler()
1682 (*ftab[c->type])(c, readset, writeset); in channel_handler()
1683 channel_garbage_collect(c); in channel_handler()
1731 Channel *c; in channel_output_poll() local
1736 c = channels[i]; in channel_output_poll()
1737 if (c == NULL) in channel_output_poll()
1745 if (c->type != SSH_CHANNEL_OPEN && in channel_output_poll()
1746 c->type != SSH_CHANNEL_INPUT_DRAINING) in channel_output_poll()
1749 if (c->type != SSH_CHANNEL_OPEN) in channel_output_poll()
1753 (c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) { in channel_output_poll()
1755 debug3("channel %d: will not send data after close", c->self); in channel_output_poll()
1760 if ((c->istate == CHAN_INPUT_OPEN || in channel_output_poll()
1761 c->istate == CHAN_INPUT_WAIT_DRAIN) && in channel_output_poll()
1762 (len = buffer_len(&c->input)) > 0) { in channel_output_poll()
1768 if (len > c->remote_window) in channel_output_poll()
1769 len = c->remote_window; in channel_output_poll()
1770 if (len > c->remote_maxpacket) in channel_output_poll()
1771 len = c->remote_maxpacket; in channel_output_poll()
1785 packet_put_int(c->remote_id); in channel_output_poll()
1786 packet_put_string(buffer_ptr(&c->input), len); in channel_output_poll()
1788 buffer_consume(&c->input, len); in channel_output_poll()
1789 c->remote_window -= len; in channel_output_poll()
1791 } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) { in channel_output_poll()
1799 if (CHANNEL_EFD_INPUT_ACTIVE(c)) in channel_output_poll()
1801 c->self, c->efd, buffer_len(&c->extended)); in channel_output_poll()
1803 chan_ibuf_empty(c); in channel_output_poll()
1807 !(c->flags & CHAN_EOF_SENT) && in channel_output_poll()
1808 c->remote_window > 0 && in channel_output_poll()
1809 (len = buffer_len(&c->extended)) > 0 && in channel_output_poll()
1810 c->extended_usage == CHAN_EXTENDED_READ) { in channel_output_poll()
1812 c->self, c->remote_window, buffer_len(&c->extended), in channel_output_poll()
1813 c->extended_usage); in channel_output_poll()
1814 if (len > c->remote_window) in channel_output_poll()
1815 len = c->remote_window; in channel_output_poll()
1816 if (len > c->remote_maxpacket) in channel_output_poll()
1817 len = c->remote_maxpacket; in channel_output_poll()
1819 packet_put_int(c->remote_id); in channel_output_poll()
1821 packet_put_string(buffer_ptr(&c->extended), len); in channel_output_poll()
1823 buffer_consume(&c->extended, len); in channel_output_poll()
1824 c->remote_window -= len; in channel_output_poll()
1825 debug2("channel %d: sent ext data %d", c->self, len); in channel_output_poll()
1839 Channel *c; in channel_input_data() local
1843 c = channel_lookup(id); in channel_input_data()
1844 if (c == NULL) in channel_input_data()
1848 if (c->type != SSH_CHANNEL_OPEN && in channel_input_data()
1849 c->type != SSH_CHANNEL_X11_OPEN) in channel_input_data()
1853 if (!compat13 && c->ostate != CHAN_OUTPUT_OPEN) in channel_input_data()
1860 if (data_len > c->local_maxpacket) { in channel_input_data()
1862 c->self, data_len, c->local_maxpacket); in channel_input_data()
1864 if (data_len > c->local_window) { in channel_input_data()
1866 c->self, data_len, c->local_window); in channel_input_data()
1870 c->local_window -= data_len; in channel_input_data()
1873 buffer_append(&c->output, data, data_len); in channel_input_data()
1883 Channel *c; in channel_input_extended_data() local
1887 c = channel_lookup(id); in channel_input_extended_data()
1889 if (c == NULL) in channel_input_extended_data()
1891 if (c->type != SSH_CHANNEL_OPEN) { in channel_input_extended_data()
1895 if (c->flags & CHAN_EOF_RCVD) { in channel_input_extended_data()
1903 if (c->efd == -1 || in channel_input_extended_data()
1904 c->extended_usage != CHAN_EXTENDED_WRITE || in channel_input_extended_data()
1906 log("channel %d: bad ext data", c->self); in channel_input_extended_data()
1911 if (data_len > c->local_window) { in channel_input_extended_data()
1913 c->self, data_len, c->local_window); in channel_input_extended_data()
1917 debug2("channel %d: rcvd ext data %d", c->self, data_len); in channel_input_extended_data()
1918 c->local_window -= data_len; in channel_input_extended_data()
1919 buffer_append(&c->extended, data, data_len); in channel_input_extended_data()
1927 Channel *c; in channel_input_ieof() local
1931 c = channel_lookup(id); in channel_input_ieof()
1932 if (c == NULL) in channel_input_ieof()
1934 chan_rcvd_ieof(c); in channel_input_ieof()
1937 if (c->force_drain && c->istate == CHAN_INPUT_OPEN) { in channel_input_ieof()
1938 debug("channel %d: FORCE input drain", c->self); in channel_input_ieof()
1939 c->istate = CHAN_INPUT_WAIT_DRAIN; in channel_input_ieof()
1940 if (buffer_len(&c->input) == 0) in channel_input_ieof()
1941 chan_ibuf_empty(c); in channel_input_ieof()
1950 Channel *c; in channel_input_close() local
1954 c = channel_lookup(id); in channel_input_close()
1955 if (c == NULL) in channel_input_close()
1963 packet_put_int(c->remote_id); in channel_input_close()
1973 if (c->type != SSH_CHANNEL_CLOSED) { in channel_input_close()
1978 buffer_clear(&c->input); in channel_input_close()
1979 c->type = SSH_CHANNEL_OUTPUT_DRAINING; in channel_input_close()
1988 Channel *c = channel_lookup(id); in channel_input_oclose() local
1991 if (c == NULL) in channel_input_oclose()
1993 chan_rcvd_oclose(c); in channel_input_oclose()
2000 Channel *c = channel_lookup(id); in channel_input_close_confirmation() local
2003 if (c == NULL) in channel_input_close_confirmation()
2006 if (c->type != SSH_CHANNEL_CLOSED) in channel_input_close_confirmation()
2008 "non-closed channel %d (type %d).", id, c->type); in channel_input_close_confirmation()
2009 channel_free(c); in channel_input_close_confirmation()
2016 Channel *c; in channel_input_open_confirmation() local
2019 c = channel_lookup(id); in channel_input_open_confirmation()
2021 if (c==NULL || c->type != SSH_CHANNEL_OPENING) in channel_input_open_confirmation()
2026 c->remote_id = remote_id; in channel_input_open_confirmation()
2027 c->type = SSH_CHANNEL_OPEN; in channel_input_open_confirmation()
2030 c->remote_window = packet_get_int(); in channel_input_open_confirmation()
2031 c->remote_maxpacket = packet_get_int(); in channel_input_open_confirmation()
2032 if (c->confirm) { in channel_input_open_confirmation()
2034 c->confirm(c->self, NULL); in channel_input_open_confirmation()
2037 debug("channel %d: open confirm rwindow %u rmax %u", c->self, in channel_input_open_confirmation()
2038 c->remote_window, c->remote_maxpacket); in channel_input_open_confirmation()
2064 Channel *c; in channel_input_open_failure() local
2067 c = channel_lookup(id); in channel_input_open_failure()
2069 if (c==NULL || c->type != SSH_CHANNEL_OPENING) in channel_input_open_failure()
2087 channel_free(c); in channel_input_open_failure()
2093 Channel *c; in channel_input_window_adjust() local
2102 c = channel_lookup(id); in channel_input_window_adjust()
2104 if (c == NULL || c->type != SSH_CHANNEL_OPEN) { in channel_input_window_adjust()
2112 c->remote_window += adjust; in channel_input_window_adjust()
2118 Channel *c = NULL; in channel_input_port_open() local
2135 c = channel_new("connected socket", in channel_input_port_open()
2138 c->remote_id = remote_id; in channel_input_port_open()
2140 if (c == NULL) { in channel_input_port_open()
2161 Channel *c; in channel_setup_fwd_listener() local
2277 c = channel_new("port listener", type, sock, sock, -1, in channel_setup_fwd_listener()
2280 strlcpy(c->path, host, sizeof(c->path)); in channel_setup_fwd_listener()
2281 c->host_port = port_to_connect; in channel_setup_fwd_listener()
2282 c->listening_port = listen_port; in channel_setup_fwd_listener()
2299 Channel *c = channels[i]; in channel_cancel_rport_listener() local
2301 if (c != NULL && c->type == SSH_CHANNEL_RPORT_LISTENER && in channel_cancel_rport_listener()
2302 strncmp(c->path, host, sizeof(c->path)) == 0 && in channel_cancel_rport_listener()
2303 c->listening_port == port) { in channel_cancel_rport_listener()
2305 channel_free(c); in channel_cancel_rport_listener()
2853 Channel *c = NULL; in x11_input_open() local
2872 c = channel_new("connected x11 socket", in x11_input_open()
2875 c->remote_id = remote_id; in x11_input_open()
2876 c->force_drain = 1; in x11_input_open()
2878 if (c == NULL) { in x11_input_open()
2886 packet_put_int(c->self); in x11_input_open()
2997 Channel *c = NULL; in auth_input_open_request() local
3019 c = channel_new("", SSH_CHANNEL_OPEN, sock, sock, in auth_input_open_request()
3021 c->remote_id = remote_id; in auth_input_open_request()
3022 c->force_drain = 1; in auth_input_open_request()
3024 if (c == NULL) { in auth_input_open_request()
3032 packet_put_int(c->self); in auth_input_open_request()