static bool reissue_connection(struct config_info *conf, struct host **h, char *host_name) { bool ret = false; int buf_len; assert(h != NULL); /* We might be passed an existing socket. If we have been, close * and destroy the associated host, and create a new one. */ if(*h) host_destroy(*h); *h = host_create(HOST_TYPE_TCP, HOST_FAMILY_IPV4); if(!*h) { error("Failed to create TCP client socket.", 1, 8, 0); goto err_out; } m_hide(); buf_len = snprintf(widget_buf, WIDGET_BUF_LEN, "Connecting to \"%s\". Please wait..", host_name); widget_buf[WIDGET_BUF_LEN - 1] = 0; draw_window_box(3, 11, 76, 13, DI_MAIN, DI_DARK, DI_CORNER, 1, 1); write_string(widget_buf, (WIDGET_BUF_LEN - buf_len) >> 1, 12, DI_TEXT, 0); update_screen(); if(!host_connect(*h, host_name, OUTBOUND_PORT)) { buf_len = snprintf(widget_buf, WIDGET_BUF_LEN, "Connection to \"%s\" failed.", host_name); widget_buf[WIDGET_BUF_LEN - 1] = 0; error(widget_buf, 1, 8, 0); } else ret = true; clear_screen(32, 7); m_show(); update_screen(); err_out: return ret; }
int ftp_open_url(url_t *urlp, bool reset_vars) { bool use_proxy; int i; if(reset_vars) ftp_reset_vars(); /* don't assume server is in ascii mode initially even if RFC says so */ ftp->prev_type = '?'; #ifdef HAVE_POSIX_SIGSETJMP if(sigsetjmp(open_timeout_jmp, 1)) #else if(setjmp(open_timeout_jmp)) #endif { ftp_close(); ftp_err(_("Connection timeout after %u seconds\n"), ftp->open_timeout); return 1; } ftp_set_signal(SIGALRM, ftp_open_handler); alarm(ftp->open_timeout); use_proxy = (proxy_type(urlp) != 0); ftp_err(_("Looking up %s... "), use_proxy ? gvProxyUrl->hostname : urlp->hostname); /* Set the default port (22) for SSH if no port is specified. We * need to do this here, 'cause host_lookup() sets it to 21 * (default port for vanilla FTP) */ if(urlp->protocol) { if(strcmp(urlp->protocol, "sftp") == 0) url_setprotocol(urlp, "ssh"); if(strcmp(urlp->protocol, "ssh") == 0 && urlp->port == -1) urlp->port = 22; /* default SSH port */ } ftp->host = host_create(use_proxy ? gvProxyUrl : urlp); if(!host_lookup(ftp->host)) { herror(host_getname(ftp->host)); alarm(0); ftp_set_signal(SIGALRM, SIG_IGN); return -1; } /* keep the value in urlp->port urlp->port = ntohs(ftp->host->port); and set it to 21 if it is -1 */ if(urlp->port == -1) { urlp->port = 21; } fprintf(stderr, "\r "); i = strlen(use_proxy ? gvProxyUrl->hostname : urlp->hostname); while(i--) fprintf(stderr, " "); fprintf(stderr, "\r"); ftp_trace("\n"); #ifdef HAVE_LIBSSH if(urlp->protocol && strcmp(urlp->protocol, "ssh") == 0) { int ret = ssh_open_url(urlp); alarm(0); return ret; } #endif if(urlp->protocol && strcmp(urlp->protocol, "ftp") != 0) { ftp_err(_("Sorry, don't know how to handle your '%s' protocol\n" "trying 'ftp' instead...\n"), urlp->protocol); url_setprotocol(urlp, 0); } if(use_proxy) { ftp_err(_("Connecting to proxy %s at port %d...\n"), host_getoname(ftp->host), urlp->port); } else { ftp_err(_("Connecting to %s at port %d...\n"), host_getoname(ftp->host), urlp->port); } ftp->ctrl = sock_create(); if (ftp->ctrl == 0) { ftp_err(_("Unable to create socket.\n")); alarm(0); ftp_set_signal(SIGALRM, SIG_IGN); return -1; } if(!sock_connect_host(ftp->ctrl, ftp->host)) { alarm(0); ftp_set_signal(SIGALRM, SIG_IGN); return -1; } sock_lowdelay(ftp->ctrl); char* ip = host_getip(ftp->host); ftp_err(_("Connected to %s ([%s]:%d).\n"), host_getoname(ftp->host), ip, urlp->port); free(ip); /* read startup message from server */ ftp_set_tmp_verbosity(vbCommand); ftp_read_reply(); if(ftp->fullcode == 120) { ftp_set_tmp_verbosity(vbCommand); ftp_read_reply(); } alarm(0); ftp_set_signal(SIGALRM, SIG_IGN); if(!sock_connected(ftp->ctrl)) { ftp_close(); return 1; } ftp->connected = (ftp->fullcode == 220); if(ftp->connected) { void (*tracefunq)(const char *fmt, ...); url_destroy(ftp->url); ftp->url = url_clone(urlp); tracefunq = (ftp->verbosity == vbDebug ? ftp_err : ftp_trace); char* remote_addr = printable_address(sock_remote_addr(ftp->ctrl)), *local_addr = printable_address(sock_local_addr(ftp->ctrl)); tracefunq("remote address: %s\n", remote_addr); tracefunq("local address: %s\n", local_addr); free(remote_addr); free(local_addr); return 0; } else { ftp_close(); return 1; } }