663
channel_register_confirm(int id, channel_callback_fn *fn, void *ctx)
672
channel_register_status_confirm(int id, channel_confirm_cb *cb,
673
channel_confirm_abandon_cb *abandon_cb, void *ctx)
675
struct channel_confirm *cc;
678
if ((c = channel_lookup(id)) == NULL)
679
fatal("channel_register_expect: %d: bad id", id);
681
cc = xmalloc(sizeof(*cc));
683
cc->abandon_cb = abandon_cb;
685
TAILQ_INSERT_TAIL(&c->status_confirms, cc, entry);
689
channel_register_open_confirm(int id, channel_callback_fn *fn, void *ctx)
665
691
Channel *c = channel_lookup(id);
668
logit("channel_register_comfirm: %d: bad id", id);
694
logit("channel_register_open_comfirm: %d: bad id", id);
672
c->confirm_ctx = ctx;
697
c->open_confirm = fn;
698
c->open_confirm_ctx = ctx;
711
737
c->input_filter = ifn;
712
738
c->output_filter = ofn;
740
c->filter_cleanup = cfn;
716
744
channel_set_fds(int id, int rfd, int wfd, int efd,
717
int extusage, int nonblock, u_int window_max)
745
int extusage, int nonblock, int is_tty, u_int window_max)
719
747
Channel *c = channel_lookup(id);
721
749
if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
722
750
fatal("channel_activate for non-larval channel %d.", id);
723
channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
751
channel_register_fds(c, rfd, wfd, efd, extusage, nonblock, is_tty);
724
752
c->type = SSH_CHANNEL_OPEN;
725
753
c->local_window = c->local_window_max = window_max;
726
754
packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
1421
1452
packet_put_int(c->self);
1424
debug("channel %d: not connected: %s",
1455
debug("channel %d: connection failed: %s",
1425
1456
c->self, strerror(err));
1457
/* Try next address, if any */
1458
if ((sock = connect_next(&c->connect_ctx)) > 0) {
1460
c->sock = c->rfd = c->wfd = sock;
1461
channel_max_fd = channel_find_maxfd();
1464
/* Exhausted all addresses */
1465
error("connect_to %.100s port %d: failed.",
1466
c->connect_ctx.host, c->connect_ctx.port);
1467
channel_connect_ctx_free(&c->connect_ctx);
1426
1468
if (compat20) {
1427
1469
packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1428
1470
packet_put_int(c->remote_id);
2313
2359
originator_string = xstrdup("unknown (remote did not supply name)");
2315
2361
packet_check_eom();
2316
sock = channel_connect_to(host, host_port);
2318
c = channel_new("connected socket",
2319
SSH_CHANNEL_CONNECTING, sock, sock, -1, 0, 0, 0,
2320
originator_string, 1);
2321
c->remote_id = remote_id;
2362
c = channel_connect_to(host, host_port,
2363
"connected socket", originator_string);
2323
2364
xfree(originator_string);
2325
packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2326
packet_put_int(remote_id);
2367
packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2368
packet_put_int(remote_id);
2371
c->remote_id = remote_id;
2376
channel_input_status_confirm(int type, u_int32_t seq, void *ctxt)
2379
struct channel_confirm *cc;
2382
/* Reset keepalive timeout */
2383
keep_alive_timeouts = 0;
2385
remote_id = packet_get_int();
2388
debug2("channel_input_confirm: type %d id %d", type, remote_id);
2390
if ((c = channel_lookup(remote_id)) == NULL) {
2391
logit("channel_input_success_failure: %d: unknown", remote_id);
2395
if ((cc = TAILQ_FIRST(&c->status_confirms)) == NULL)
2397
cc->cb(type, c, cc->ctx);
2398
TAILQ_REMOVE(&c->status_confirms, cc, entry);
2399
bzero(cc, sizeof(*cc));
2333
2403
/* -- tcp forwarding */
2715
2786
num_adm_permitted_opens = 0;
2718
/* return socket to remote host, port */
2790
channel_print_adm_permitted_opens(void)
2794
for (i = 0; i < num_adm_permitted_opens; i++)
2795
if (permitted_adm_opens[i].host_to_connect != NULL)
2796
printf(" %s:%d", permitted_adm_opens[i].host_to_connect,
2797
permitted_adm_opens[i].port_to_connect);
2800
/* Try to start non-blocking connect to next host in cctx list */
2720
connect_to(const char *host, u_short port)
2802
connect_next(struct channel_connect *cctx)
2722
struct addrinfo hints, *ai, *aitop;
2804
int sock, saved_errno;
2723
2805
char ntop[NI_MAXHOST], strport[NI_MAXSERV];
2727
memset(&hints, 0, sizeof(hints));
2728
hints.ai_family = IPv4or6;
2729
hints.ai_socktype = SOCK_STREAM;
2730
snprintf(strport, sizeof strport, "%d", port);
2731
if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) {
2732
error("connect_to %.100s: unknown host (%s)", host,
2733
gai_strerror(gaierr));
2736
for (ai = aitop; ai; ai = ai->ai_next) {
2737
if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2807
for (; cctx->ai; cctx->ai = cctx->ai->ai_next) {
2808
if (cctx->ai->ai_family != AF_INET &&
2809
cctx->ai->ai_family != AF_INET6)
2739
if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2740
strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
2741
error("connect_to: getnameinfo failed");
2811
if (getnameinfo(cctx->ai->ai_addr, cctx->ai->ai_addrlen,
2812
ntop, sizeof(ntop), strport, sizeof(strport),
2813
NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
2814
error("connect_next: getnameinfo failed");
2744
sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
2746
if (ai->ai_next == NULL)
2817
if ((sock = socket(cctx->ai->ai_family, cctx->ai->ai_socktype,
2818
cctx->ai->ai_protocol)) == -1) {
2819
if (cctx->ai->ai_next == NULL)
2747
2820
error("socket: %.100s", strerror(errno));
2749
2822
verbose("socket: %.100s", strerror(errno));
2752
2825
if (set_nonblock(sock) == -1)
2753
2826
fatal("%s: set_nonblock(%d)", __func__, sock);
2754
if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0 &&
2755
errno != EINPROGRESS) {
2756
error("connect_to %.100s port %s: %.100s", ntop, strport,
2827
if (connect(sock, cctx->ai->ai_addr,
2828
cctx->ai->ai_addrlen) == -1 && errno != EINPROGRESS) {
2829
debug("connect_next: host %.100s ([%.100s]:%s): "
2830
"%.100s", cctx->host, ntop, strport,
2757
2831
strerror(errno));
2832
saved_errno = errno;
2834
errno = saved_errno;
2759
2835
continue; /* fail -- try next */
2761
break; /* success */
2764
freeaddrinfo(aitop);
2766
error("connect_to %.100s port %d: failed.", host, port);
2775
channel_connect_by_listen_address(u_short listen_port)
2837
debug("connect_next: host %.100s ([%.100s]:%s) "
2838
"in progress, fd=%d", cctx->host, ntop, strport, sock);
2839
cctx->ai = cctx->ai->ai_next;
2847
channel_connect_ctx_free(struct channel_connect *cctx)
2851
freeaddrinfo(cctx->aitop);
2852
bzero(cctx, sizeof(*cctx));
2854
cctx->ai = cctx->aitop = NULL;
2857
/* Return CONNECTING channel to remote host, port */
2859
connect_to(const char *host, u_short port, char *ctype, char *rname)
2861
struct addrinfo hints;
2864
char strport[NI_MAXSERV];
2865
struct channel_connect cctx;
2868
memset(&cctx, 0, sizeof(cctx));
2869
memset(&hints, 0, sizeof(hints));
2870
hints.ai_family = IPv4or6;
2871
hints.ai_socktype = SOCK_STREAM;
2872
snprintf(strport, sizeof strport, "%d", port);
2873
if ((gaierr = getaddrinfo(host, strport, &hints, &cctx.aitop)) != 0) {
2874
error("connect_to %.100s: unknown host (%s)", host,
2875
ssh_gai_strerror(gaierr));
2879
cctx.host = xstrdup(host);
2881
cctx.ai = cctx.aitop;
2883
if ((sock = connect_next(&cctx)) == -1) {
2884
error("connect to %.100s port %d failed: %s",
2885
host, port, strerror(errno));
2886
channel_connect_ctx_free(&cctx);
2889
c = channel_new(ctype, SSH_CHANNEL_CONNECTING, sock, sock, -1,
2890
CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1);
2891
c->connect_ctx = cctx;
2896
channel_connect_by_listen_address(u_short listen_port, char *ctype, char *rname)
2779
for (i = 0; i < num_permitted_opens; i++)
2900
for (i = 0; i < num_permitted_opens; i++) {
2780
2901
if (permitted_opens[i].host_to_connect != NULL &&
2781
permitted_opens[i].listen_port == listen_port)
2902
permitted_opens[i].listen_port == listen_port) {
2782
2903
return connect_to(
2783
2904
permitted_opens[i].host_to_connect,
2784
permitted_opens[i].port_to_connect);
2905
permitted_opens[i].port_to_connect, ctype, rname);
2785
2908
error("WARNING: Server requests forwarding for unknown listen_port %d",
2790
2913
/* Check if connecting to that port is permitted and connect. */
2792
channel_connect_to(const char *host, u_short port)
2915
channel_connect_to(const char *host, u_short port, char *ctype, char *rname)
2794
2917
int i, permit, permit_adm = 1;