--- lbbs/src/bbs_net.c 2025/12/16 12:59:14 1.89 +++ lbbs/src/bbs_net.c 2025/12/18 08:22:53 1.99 @@ -49,11 +49,13 @@ static const char MENU_CONF_DELIM[] = " enum _bbs_net_constant_t { - MAX_PROCESS_BAR_LEN = 30, MAXSTATION = 26 * 2, STATION_PER_LINE = 4, USERNAME_MAX_LEN = 20, PASSWORD_MAX_LEN = 20, + REMOTE_CONNECT_TIMEOUT = 10, // seconds + SSH_CONNECT_TIMEOUT = 5, // seconds + PROGRESS_BAR_LEN = 30, }; struct _bbsnet_conf @@ -61,7 +63,7 @@ struct _bbsnet_conf char org_name[40]; char site_name[40]; char host_name[IP_ADDR_LEN]; - in_port_t port; + char port[6]; int8_t use_ssh; char charset[CHARSET_MAX_LEN + 1]; } bbsnet_conf[MAXSTATION]; @@ -77,7 +79,8 @@ static int load_bbsnet_conf(const char * MENU_ITEM *p_menu_item; MENU_ITEM_ID menu_item_id; char line[LINE_BUFFER_LEN], *t1, *t2, *t3, *t4, *t5, *t6, *saveptr; - int port; + long port; + char *endptr; unload_bbsnet_conf(); @@ -123,7 +126,7 @@ static int load_bbsnet_conf(const char * t6 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr); if (t1 == NULL || t2 == NULL || t3 == NULL || t4 == NULL || - t5 == NULL || t6 == NULL || line[0] == '#' || line[0] == '*') + t5 == NULL || t6 == NULL || t1[0] == '#') { continue; } @@ -134,15 +137,16 @@ static int load_bbsnet_conf(const char * bbsnet_conf[menu_item_id].org_name[sizeof(bbsnet_conf[menu_item_id].org_name) - 1] = '\0'; strncpy(bbsnet_conf[menu_item_id].host_name, t3, sizeof(bbsnet_conf[menu_item_id].host_name) - 1); bbsnet_conf[menu_item_id].host_name[sizeof(bbsnet_conf[menu_item_id].host_name) - 1] = '\0'; - port = atoi(t4); - if (port <= 0 || port > 65535) + port = strtol(t4, &endptr, 10); + if (*endptr != '\0' || port <= 0 || port > 65535) { - log_error("Invalid port value %d of menu item %d\n", port, menu_item_id); + log_error("Invalid port value %ld of menu item %d\n", port, menu_item_id); fclose(fp); unload_bbsnet_conf(); return -3; } - bbsnet_conf[menu_item_id].port = (in_port_t)port; + strncpy(bbsnet_conf[menu_item_id].port, t4, sizeof(bbsnet_conf[menu_item_id].port) - 1); + bbsnet_conf[menu_item_id].port[sizeof(bbsnet_conf[menu_item_id].port) - 1] = '\0'; bbsnet_conf[menu_item_id].use_ssh = (toupper(t5[0]) == 'Y'); strncpy(bbsnet_conf[menu_item_id].charset, t6, sizeof(bbsnet_conf[menu_item_id].charset) - 1); bbsnet_conf[menu_item_id].charset[sizeof(bbsnet_conf[menu_item_id].charset) - 1] = '\0'; @@ -163,7 +167,7 @@ static int load_bbsnet_conf(const char * p_menu_item->priv = 0; p_menu_item->level = 0; p_menu_item->name[0] = - (char)(menu_item_id < MAXSTATION / 2 ? 'A' + menu_item_id : 'a' + menu_item_id); + (char)(menu_item_id < MAXSTATION / 2 ? 'A' + menu_item_id : 'a' + menu_item_id - MAXSTATION / 2); p_menu_item->name[1] = '\0'; snprintf(p_menu_item->text, sizeof(p_menu_item->text), "\033[1;36m%c.\033[m %s", p_menu_item->name[0], bbsnet_conf[menu_item_id].site_name); @@ -201,42 +205,56 @@ static void unload_bbsnet_conf(void) } } -static void process_bar(int n, int len) +static void progress_bar(int percent, int len) { + char line[LINE_BUFFER_LEN]; char buf[LINE_BUFFER_LEN]; char buf2[LINE_BUFFER_LEN]; + int pos; - if (len <= 0) + if (len < 4) { - len = 1; + len = 4; } - else if (len > LINE_BUFFER_LEN) + else if (len + 2 > LINE_BUFFER_LEN) { - len = LINE_BUFFER_LEN - 1; + len = LINE_BUFFER_LEN - 3; } - if (n < 0) + if (percent < 0) { - n = 0; + percent = 0; } - else if (n > len) + else if (percent > 100) { - n = len; + percent = 100; } + pos = len * percent / 100; + + line[0] = ' '; + for (int i = 1; i <= len; i++) + { + line[i] = '-'; + } + line[len + 1] = ' '; + line[len + 2] = '\0'; + + snprintf(buf, sizeof(buf), "%*s%3d%%%*s", + (len - 4) / 2, "", percent, (len - 4 + 1) / 2, ""); + memcpy(buf2, buf, (size_t)pos); + buf2[pos] = '\0'; + moveto(4, 1); - prints(" ------------------------------ \r\n"); - snprintf(buf, sizeof(buf), " %3d%% ", n * 100 / len); - memcpy(buf2, buf, (size_t)n); - buf2[n] = '\0'; - prints("|\033[46m%s\033[44m%s\033[m|\r\n", buf2, buf + n); - prints(" ------------------------------ \r\n"); + prints("%s\r\n", line); + prints("|\033[46m%s\033[44m%s\033[m|\r\n", buf2, buf + pos); + prints("%s\r\n", line); iflush(); } static int bbsnet_connect(int n) { int sock = -1; - int ret; + int ret = 0; int loop; int error; int sock_connected = 0; @@ -272,23 +290,23 @@ static int bbsnet_connect(int n) int stdout_write_wait = 0; int sock_read_wait = 0; int sock_write_wait = 0; - struct hostent *p_host = NULL; + struct addrinfo hints, *res = NULL; int tos; - char remote_addr[IP_ADDR_LEN]; + char remote_addr[INET_ADDRSTRLEN]; int remote_port; - char local_addr[IP_ADDR_LEN]; + char local_addr[INET_ADDRSTRLEN]; int local_port; socklen_t sock_len; - time_t t_used = time(NULL); - struct tm *tm_used; + time_t t_begin, t_used; + struct timespec ts_begin, ts_now; + int progress, progress_last; int ch; char remote_user[USERNAME_MAX_LEN + 1]; char remote_pass[PASSWORD_MAX_LEN + 1]; - ssh_session session = NULL; - ssh_channel channel = NULL; + ssh_session outbound_session = NULL; + ssh_channel outbound_channel = NULL; int ssh_process_config = 0; int ssh_log_level = SSH_LOG_NOLOG; - long ssh_timeout = 0; if (user_online_update("BBS_NET") < 0) { @@ -341,56 +359,78 @@ static int bbsnet_connect(int n) bbsnet_conf[n].site_name, bbsnet_conf[n].host_name); iflush(); - p_host = gethostbyname(bbsnet_conf[n].host_name); + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_STREAM; + hints.ai_protocol = IPPROTO_TCP; - if (p_host == NULL) + if ((ret = getaddrinfo(BBS_address, NULL, &hints, &res)) != 0) { - prints("\033[1;31m查找主机名失败!\033[m\r\n"); - press_any_key(); + log_error("getaddrinfo() error (%d): %s\n", ret, gai_strerror(ret)); + ret = -1; goto cleanup; } - sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + if (inet_ntop(AF_INET, &(((struct sockaddr_in *)res->ai_addr)->sin_addr), local_addr, sizeof(local_addr)) == NULL) + { + log_error("inet_ntop() error (%d)\n", errno); + ret = -1; + goto cleanup; + } + local_port = ntohs(((struct sockaddr_in *)res->ai_addr)->sin_port); + sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol); if (sock < 0) { - prints("\033[1;31m无法创建socket!\033[m\r\n"); - press_any_key(); + log_error("socket() error (%d)\n", errno); + ret = -1; goto cleanup; } - sin.sin_family = AF_INET; - sin.sin_addr.s_addr = (BBS_address[0] != '\0' ? inet_addr(BBS_address) : INADDR_ANY); - sin.sin_port = 0; - - if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) + if (bind(sock, res->ai_addr, res->ai_addrlen) < 0) { - log_error("Bind address %s:%u failed (%d)\n", - inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), errno); + log_error("bind(%s:%u) error (%d)\n", local_addr, local_port, errno); + ret = -1; goto cleanup; } - memset(&sin, 0, sizeof(sin)); - sin.sin_family = AF_INET; - sin.sin_addr = *(struct in_addr *)p_host->h_addr_list[0]; - sin.sin_port = htons(bbsnet_conf[n].port); + freeaddrinfo(res); + res = NULL; - strncpy(remote_addr, inet_ntoa(sin.sin_addr), sizeof(remote_addr) - 1); - remote_addr[sizeof(remote_addr) - 1] = '\0'; - remote_port = ntohs(sin.sin_port); + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_INET; + hints.ai_flags = AI_NUMERICSERV; + hints.ai_socktype = SOCK_STREAM; + hints.ai_protocol = IPPROTO_TCP; - prints("\033[1;32m穿梭进度条提示您当前已使用的时间,按\033[1;33mCtrl+C\033[1;32m中断。\033[m\r\n"); - process_bar(0, MAX_PROCESS_BAR_LEN); + if ((ret = getaddrinfo(bbsnet_conf[n].host_name, bbsnet_conf[n].port, &hints, &res)) != 0) + { + log_error("getaddrinfo() error (%d): %s\n", ret, gai_strerror(ret)); + prints("\033[1;31m查找主机名失败!\033[m\r\n"); + press_any_key(); + ret = -1; + goto cleanup; + } + + if (inet_ntop(AF_INET, &(((struct sockaddr_in *)res->ai_addr)->sin_addr), remote_addr, sizeof(remote_addr)) == NULL) + { + log_error("inet_ntop() error (%d)\n", errno); + ret = -1; + goto cleanup; + } + remote_port = ntohs(((struct sockaddr_in *)res->ai_addr)->sin_port); // Set socket as non-blocking if ((flags_sock = fcntl(sock, F_GETFL, 0)) == -1) { log_error("fcntl(F_GETFL) error (%d)\n", errno); + ret = -1; goto cleanup; } if ((fcntl(sock, F_SETFL, flags_sock | O_NONBLOCK)) == -1) { log_error("fcntl(F_SETFL) error (%d)\n", errno); + ret = -1; goto cleanup; } @@ -398,21 +438,25 @@ static int bbsnet_connect(int n) if ((flags_stdin = fcntl(STDIN_FILENO, F_GETFL, 0)) == -1) { log_error("fcntl(F_GETFL) error (%d)\n", errno); + ret = -1; goto cleanup; } if ((flags_stdout = fcntl(STDOUT_FILENO, F_GETFL, 0)) == -1) { log_error("fcntl(F_GETFL) error (%d)\n", errno); + ret = -1; goto cleanup; } if ((fcntl(STDIN_FILENO, F_SETFL, flags_stdin | O_NONBLOCK)) == -1) { log_error("fcntl(F_SETFL) error (%d)\n", errno); + ret = -1; goto cleanup; } if ((fcntl(STDOUT_FILENO, F_SETFL, flags_stdout | O_NONBLOCK)) == -1) { log_error("fcntl(F_SETFL) error (%d)\n", errno); + ret = -1; goto cleanup; } @@ -421,6 +465,7 @@ static int bbsnet_connect(int n) if (epollfd < 0) { log_error("epoll_create1() error (%d)\n", errno); + ret = -1; goto cleanup; } @@ -429,6 +474,7 @@ static int bbsnet_connect(int n) if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sock, &ev) == -1) { log_error("epoll_ctl(socket) error (%d)\n", errno); + ret = -1; goto cleanup; } @@ -437,13 +483,19 @@ static int bbsnet_connect(int n) if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1) { log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno); + ret = -1; goto cleanup; } #endif while (!SYS_server_exit) { - if ((ret = connect(sock, (struct sockaddr *)&sin, sizeof(sin))) < 0) + if ((ret = connect(sock, res->ai_addr, res->ai_addrlen)) == 0) + { + sock_connected = 1; + break; + } + else if (ret < 0) { if (errno == EAGAIN || errno == EALREADY || errno == EINPROGRESS) { @@ -459,19 +511,33 @@ static int bbsnet_connect(int n) else { log_error("connect(socket) error (%d)\n", errno); - prints("\033[1;31m连接失败!\033[m\r\n"); press_any_key(); - + ret = -1; goto cleanup; } } } - for (int j = 0; j < MAX_PROCESS_BAR_LEN && !sock_connected && !SYS_server_exit; j++) + progress = progress_last = 0; + prints("\033[1;32m连接进行中,按\033[1;33mCtrl+C\033[1;32m中断。\033[m\r\n"); + progress_bar(0, PROGRESS_BAR_LEN); + + if (clock_gettime(CLOCK_REALTIME, &ts_begin) == -1) + { + log_error("clock_gettime() error (%d)\n", errno); + ret = -1; + goto cleanup; + } + ts_now = ts_begin; + + while ((ts_now.tv_sec - ts_begin.tv_sec) + + (ts_now.tv_nsec - ts_begin.tv_nsec) / 1000 / 1000 / 1000 < + REMOTE_CONNECT_TIMEOUT && + !sock_connected && !SYS_server_exit) { #ifdef HAVE_SYS_EPOLL_H - nfds = epoll_wait(epollfd, events, MAX_EVENTS, 500); // 0.5 second + nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second ret = nfds; #else pfds[0].fd = sock; @@ -479,7 +545,7 @@ static int bbsnet_connect(int n) pfds[1].fd = STDIN_FILENO; pfds[1].events = POLLIN; nfds = 2; - ret = poll(pfds, (nfds_t)nfds, 500); // 0.5 second + ret = poll(pfds, (nfds_t)nfds, 100); // 0.1 second #endif if (ret < 0) @@ -496,7 +562,23 @@ static int bbsnet_connect(int n) } else if (ret == 0) // timeout { - process_bar(j + 1, MAX_PROCESS_BAR_LEN); + if (clock_gettime(CLOCK_REALTIME, &ts_now) == -1) + { + log_error("clock_gettime() error (%d)\n", errno); + ret = -1; + goto cleanup; + } + + progress = (int)((ts_now.tv_sec - ts_begin.tv_sec) * 1000 + + (ts_now.tv_nsec - ts_begin.tv_nsec) / 1000 / 1000) / + REMOTE_CONNECT_TIMEOUT / 10 + + 1; + + if (progress > progress_last) + { + progress_last = progress; + progress_bar(progress, PROGRESS_BAR_LEN); + } } else // ret > 0 { @@ -512,11 +594,13 @@ static int bbsnet_connect(int n) if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { log_error("getsockopt() error (%d) !\n", errno); + ret = -1; goto cleanup; } if (error == 0) { sock_connected = 1; + break; } } #ifdef HAVE_SYS_EPOLL_H @@ -531,6 +615,7 @@ static int bbsnet_connect(int n) } while (ch == 0); if (ch == Ctrl('C') || ch == KEY_ESC) { + ret = 0; goto cleanup; } } @@ -539,13 +624,15 @@ static int bbsnet_connect(int n) } if (SYS_server_exit) { + ret = 0; goto cleanup; } if (!sock_connected) { + progress_bar(100, PROGRESS_BAR_LEN); prints("\033[1;31m连接失败!\033[m\r\n"); press_any_key(); - + ret = -1; goto cleanup; } @@ -559,65 +646,82 @@ static int bbsnet_connect(int n) if (getsockname(sock, (struct sockaddr *)&sin, &sock_len) < 0) { log_error("getsockname() error: %d", errno); + ret = -1; goto cleanup; } - strncpy(local_addr, inet_ntoa(sin.sin_addr), sizeof(local_addr) - 1); - local_addr[sizeof(local_addr) - 1] = '\0'; + if (inet_ntop(AF_INET, &(sin.sin_addr), local_addr, sizeof(local_addr)) == NULL) + { + log_error("inet_ntop() error (%d)\n", errno); + ret = -1; + goto cleanup; + } local_port = ntohs(sin.sin_port); if (bbsnet_conf[n].use_ssh) { - session = ssh_new(); - if (session == NULL) + outbound_session = ssh_new(); + if (outbound_session == NULL) { log_error("ssh_new() error\n"); + ret = -1; goto cleanup; } - if (ssh_options_set(session, SSH_OPTIONS_FD, &sock) < 0 || - ssh_options_set(session, SSH_OPTIONS_PROCESS_CONFIG, &ssh_process_config) < 0 || - ssh_options_set(session, SSH_OPTIONS_KNOWNHOSTS, SSH_KNOWN_HOSTS_FILE) < 0 || - ssh_options_set(session, SSH_OPTIONS_HOST, bbsnet_conf[n].host_name) < 0 || - ssh_options_set(session, SSH_OPTIONS_USER, remote_user) < 0 || - ssh_options_set(session, SSH_OPTIONS_HOSTKEYS, "+ssh-rsa") < 0 || - ssh_options_set(session, SSH_OPTIONS_LOG_VERBOSITY, &ssh_log_level) < 0) + if (ssh_options_set(outbound_session, SSH_OPTIONS_FD, &sock) < 0 || + ssh_options_set(outbound_session, SSH_OPTIONS_PROCESS_CONFIG, &ssh_process_config) < 0 || + ssh_options_set(outbound_session, SSH_OPTIONS_KNOWNHOSTS, SSH_KNOWN_HOSTS_FILE) < 0 || + ssh_options_set(outbound_session, SSH_OPTIONS_HOST, bbsnet_conf[n].host_name) < 0 || + ssh_options_set(outbound_session, SSH_OPTIONS_USER, remote_user) < 0 || + ssh_options_set(outbound_session, SSH_OPTIONS_HOSTKEYS, "+ssh-ed25519,ecdsa-sha2-nistp256,ssh-rsa") < 0 || + ssh_options_set(outbound_session, SSH_OPTIONS_LOG_VERBOSITY, &ssh_log_level) < 0) { - log_error("Error setting SSH options: %s\n", ssh_get_error(session)); + log_error("Error setting SSH options: %s\n", ssh_get_error(outbound_session)); + ret = -1; goto cleanup; } - ssh_timeout = 5; // second - if (ssh_options_set(session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0) - { - log_error("Error setting SSH options: %s\n", ssh_get_error(session)); - goto cleanup; - } + ssh_set_blocking(outbound_session, 0); - while (!SYS_server_exit) + t_begin = time(NULL); + ret = SSH_ERROR; + while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT) { - ret = ssh_connect(session); + ret = ssh_connect(outbound_session); if (ret == SSH_OK) { break; } - else if (ret == SSH_ERROR) + else if (ret == SSH_AGAIN) + { + // log_debug("ssh_connect() error: SSH_AGAIN\n"); + } + else // if (ret == SSH_ERROR) { - log_error("ssh_connect() error\n"); + log_error("ssh_connect() error: SSH_ERROR\n"); + ret = -1; goto cleanup; } } + if (ret != SSH_OK) + { + prints("\033[1;31m连接超时!\033[m\r\n"); + press_any_key(); + ret = -1; + goto cleanup; + } - ret = ssh_session_is_known_server(session); + ret = ssh_session_is_known_server(outbound_session); switch (ret) { case SSH_KNOWN_HOSTS_NOT_FOUND: case SSH_KNOWN_HOSTS_UNKNOWN: - if (ssh_session_update_known_hosts(session) != SSH_OK) + if (ssh_session_update_known_hosts(outbound_session) != SSH_OK) { log_error("ssh_session_update_known_hosts(%s) error\n", bbsnet_conf[n].host_name); - prints("\033[1;31m无法添加服务器证书\033[m"); + prints("\033[1;31m无法添加服务器证书\033[m\r\n"); press_any_key(); + ret = -1; goto cleanup; } log_common("SSH key of (%s) is added into %s\n", bbsnet_conf[n].host_name, SSH_KNOWN_HOSTS_FILE); @@ -626,110 +730,135 @@ static int bbsnet_connect(int n) case SSH_KNOWN_HOSTS_CHANGED: case SSH_KNOWN_HOSTS_OTHER: log_error("ssh_session_is_known_server(%s) error: %d\n", bbsnet_conf[n].host_name, ret); - prints("\033[1;31m服务器证书已变更\033[m"); + prints("\033[1;31m服务器证书已变更\033[m\r\n"); press_any_key(); + ret = -1; goto cleanup; } - for (int i = 0; !SYS_server_exit;) + ret = SSH_AUTH_ERROR; + while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT) { - ret = ssh_userauth_password(session, NULL, remote_pass); + ret = ssh_userauth_password(outbound_session, NULL, remote_pass); if (ret == SSH_AUTH_SUCCESS) { break; } else if (ret == SSH_AUTH_AGAIN) { -#ifdef _DEBUG - log_error("ssh_userauth_password() error: SSH_AUTH_AGAIN\n"); -#endif + // log_debug("ssh_userauth_password() error: SSH_AUTH_AGAIN\n"); } else if (ret == SSH_AUTH_ERROR) { - log_error("ssh_userauth_password() error: %d\n", ret); + log_error("ssh_userauth_password() error: SSH_AUTH_ERROR\n"); + ret = -1; goto cleanup; } else // if (ret == SSH_AUTH_DENIED) { + log_error("ssh_userauth_password() error: SSH_AUTH_DENIED\n"); prints("\033[1;31m身份验证失败!\033[m\r\n"); - i++; - if (i < BBS_login_retry_times) - { - prints("请输入密码: "); - iflush(); - if (str_input(remote_pass, sizeof(remote_pass), NOECHO) < 0) - { - goto cleanup; - } - if (remote_pass[0] == '\0') - { - goto cleanup; - } - } - else - { - goto cleanup; - } + press_any_key(); + ret = -1; + goto cleanup; } } + if (ret != SSH_AUTH_SUCCESS) + { + prints("\033[1;31m连接超时!\033[m\r\n"); + press_any_key(); + ret = -1; + goto cleanup; + } - channel = ssh_channel_new(session); - if (channel == NULL) + outbound_channel = ssh_channel_new(outbound_session); + if (outbound_channel == NULL) { log_error("ssh_channel_new() error\n"); + ret = -1; goto cleanup; } - while (!SYS_server_exit) + ret = SSH_ERROR; + while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT) { - ret = ssh_channel_open_session(channel); + ret = ssh_channel_open_session(outbound_channel); if (ret == SSH_OK) { break; } - else if (ret == SSH_ERROR) + else if (ret == SSH_AGAIN) { - log_error("ssh_channel_open_session() error\n"); + // log_debug("ssh_channel_open_session() error: SSH_AGAIN\n"); + } + else // if (ret == SSH_ERROR) + { + log_error("ssh_channel_open_session() error: SSH_ERROR\n"); + ret = -1; goto cleanup; } } + if (ret != SSH_OK) + { + prints("\033[1;31m连接超时!\033[m\r\n"); + press_any_key(); + ret = -1; + goto cleanup; + } - while (!SYS_server_exit) + ret = SSH_ERROR; + while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT) { - ret = ssh_channel_request_pty(channel); + ret = ssh_channel_request_pty(outbound_channel); if (ret == SSH_OK) { break; } - else if (ret == SSH_ERROR) + else if (ret == SSH_AGAIN) + { + // log_debug("ssh_channel_request_pty() error: SSH_AGAIN\n"); + } + else // if (ret == SSH_ERROR) { - log_error("ssh_channel_request_pty() error\n"); + log_error("ssh_channel_request_pty() error: SSH_ERROR\n"); + ret = -1; goto cleanup; } } + if (ret != SSH_OK) + { + prints("\033[1;31m连接超时!\033[m\r\n"); + press_any_key(); + ret = -1; + goto cleanup; + } - while (!SYS_server_exit) + ret = SSH_ERROR; + while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT) { - ret = ssh_channel_request_shell(channel); + ret = ssh_channel_request_shell(outbound_channel); if (ret == SSH_OK) { break; } - else if (ret == SSH_ERROR) + else if (ret == SSH_AGAIN) { - log_error("ssh_channel_request_shell() error\n"); + // log_debug("ssh_channel_request_shell() error: SSH_AGAIN\n"); + } + else // if (ret == SSH_ERROR) + { + log_error("ssh_channel_request_shell() error: SSH_ERROR\n"); + ret = -1; goto cleanup; } } - - ssh_timeout = 0; // second - if (ssh_options_set(session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0) + if (ret != SSH_OK) { - log_error("Error setting SSH options: %s\n", ssh_get_error(session)); + prints("\033[1;31m连接超时!\033[m\r\n"); + press_any_key(); + ret = -1; goto cleanup; } - - ssh_set_blocking(session, 0); } prints("\033[1;31m连接成功!\033[m\r\n"); @@ -743,6 +872,7 @@ static int bbsnet_connect(int n) if (input_cd == (iconv_t)(-1)) { log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, tocode, errno); + ret = -1; goto cleanup; } @@ -752,6 +882,7 @@ static int bbsnet_connect(int n) if (output_cd == (iconv_t)(-1)) { log_error("iconv_open(%s->%s) error: %d\n", bbsnet_conf[n].charset, tocode, errno); + ret = -1; goto cleanup; } @@ -761,6 +892,7 @@ static int bbsnet_connect(int n) if (epoll_ctl(epollfd, EPOLL_CTL_MOD, sock, &ev) == -1) { log_error("epoll_ctl(socket) error (%d)\n", errno); + ret = -1; goto cleanup; } @@ -769,25 +901,26 @@ static int bbsnet_connect(int n) if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1) { log_error("epoll_ctl(STDOUT_FILENO) error (%d)\n", errno); + ret = -1; goto cleanup; } #endif - BBS_last_access_tm = t_used = time(NULL); + BBS_last_access_tm = t_begin = time(NULL); loop = 1; while (loop && !SYS_server_exit) { if (SSH_v2 && ssh_channel_is_closed(SSH_channel)) { - log_error("SSH channel is closed\n"); + log_debug("SSH channel is closed\n"); loop = 0; break; } - if (bbsnet_conf[n].use_ssh && ssh_channel_is_closed(channel)) + if (bbsnet_conf[n].use_ssh && ssh_channel_is_closed(outbound_channel)) { - log_error("Remote SSH channel is closed\n"); + log_debug("Outbound channel is closed\n"); loop = 0; break; } @@ -830,7 +963,22 @@ static int bbsnet_connect(int n) for (int i = 0; i < nfds; i++) { #ifdef HAVE_SYS_EPOLL_H - if (events[i].data.fd == STDIN_FILENO) + if (events[i].events & (EPOLLHUP | EPOLLERR)) +#else + if (pfds[i].revents & (POLLHUP | POLLERR)) +#endif + { +#ifdef HAVE_SYS_EPOLL_H + log_debug("FD (%d) error events (%d)\n", events[i].data.fd, events[i].events); +#else + log_debug("FD (%d) error events (%d)\n", pfds[i].fd, pfds[i].revents); +#endif + loop = 0; + break; + } + +#ifdef HAVE_SYS_EPOLL_H + if (events[i].data.fd == STDIN_FILENO && (events[i].events & EPOLLIN)) #else if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & POLLIN)) #endif @@ -864,7 +1012,7 @@ static int bbsnet_connect(int n) } #ifdef HAVE_SYS_EPOLL_H - if (events[i].data.fd == STDOUT_FILENO) + if (events[i].data.fd == STDOUT_FILENO && (events[i].events & EPOLLOUT)) #else if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & POLLOUT)) #endif @@ -882,7 +1030,7 @@ static int bbsnet_connect(int n) ret = ssh_channel_read_nonblocking(SSH_channel, input_buf + input_buf_len, sizeof(input_buf) - (uint32_t)input_buf_len, 0); if (ret == SSH_ERROR) { - log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session)); + log_debug("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session)); loop = 0; break; } @@ -897,8 +1045,8 @@ static int bbsnet_connect(int n) // Send NO-OP to remote server input_buf[input_buf_len] = '\0'; input_buf_len++; - BBS_last_access_tm = time(NULL); + BBS_last_access_tm = time(NULL); stdin_read_wait = 0; break; // Check whether channel is still open } @@ -927,9 +1075,7 @@ static int bbsnet_connect(int n) } else if (ret == 0) // broken pipe { -#ifdef _DEBUG - log_error("read(STDIN) EOF\n"); -#endif + log_debug("read(STDIN) EOF\n"); stdin_read_wait = 0; loop = 0; break; @@ -958,7 +1104,7 @@ static int bbsnet_connect(int n) #ifdef _DEBUG for (int j = input_buf_offset; j < input_buf_len; j++) { - log_error("Debug input: <--[%u]\n", (input_buf[j] + 256) % 256); + log_debug("input: <--[%u]\n", (input_buf[j] + 256) % 256); } #endif @@ -973,7 +1119,7 @@ static int bbsnet_connect(int n) #ifdef _DEBUG for (int j = input_conv_offset; j < input_conv_len; j++) { - log_error("Debug input_conv: <--[%u]\n", (input_conv[j] + 256) % 256); + log_debug("input_conv: <--[%u]\n", (input_conv[j] + 256) % 256); } #endif } @@ -982,10 +1128,10 @@ static int bbsnet_connect(int n) { if (bbsnet_conf[n].use_ssh) { - ret = ssh_channel_write(channel, input_conv + input_conv_offset, (uint32_t)(input_conv_len - input_conv_offset)); + ret = ssh_channel_write(outbound_channel, input_conv + input_conv_offset, (uint32_t)(input_conv_len - input_conv_offset)); if (ret == SSH_ERROR) { - log_error("ssh_channel_write() error: %s\n", ssh_get_error(session)); + log_debug("ssh_channel_write() error: %s\n", ssh_get_error(outbound_session)); loop = 0; break; } @@ -1007,16 +1153,14 @@ static int bbsnet_connect(int n) } else { - log_error("write(socket) error (%d)\n", errno); + log_debug("write(socket) error (%d)\n", errno); loop = 0; break; } } else if (ret == 0) // broken pipe { -#ifdef _DEBUG - log_error("write(socket) EOF\n"); -#endif + log_debug("write(socket) EOF\n"); sock_write_wait = 0; loop = 0; break; @@ -1041,11 +1185,11 @@ static int bbsnet_connect(int n) { if (bbsnet_conf[n].use_ssh) { - ret = ssh_channel_read_nonblocking(channel, output_buf + output_buf_len, + ret = ssh_channel_read_nonblocking(outbound_channel, output_buf + output_buf_len, (uint32_t)(sizeof(output_buf) - (size_t)output_buf_len), 0); if (ret == SSH_ERROR) { - log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(session)); + log_debug("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(outbound_session)); loop = 0; break; } @@ -1078,16 +1222,14 @@ static int bbsnet_connect(int n) } else { - log_error("read(socket) error (%d)\n", errno); + log_debug("read(socket) error (%d)\n", errno); loop = 0; break; } } else if (ret == 0) // broken pipe { -#ifdef _DEBUG - log_error("read(socket) EOF\n"); -#endif + log_debug("read(socket) EOF\n"); sock_read_wait = 0; loop = 0; break; @@ -1119,7 +1261,7 @@ static int bbsnet_connect(int n) ret = ssh_channel_write(SSH_channel, output_conv + output_conv_offset, (uint32_t)(output_conv_len - output_conv_offset)); if (ret == SSH_ERROR) { - log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session)); + log_debug("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session)); loop = 0; break; } @@ -1141,16 +1283,14 @@ static int bbsnet_connect(int n) } else { - log_error("write(STDOUT) error (%d)\n", errno); + log_debug("write(STDOUT) error (%d)\n", errno); loop = 0; break; } } else if (ret == 0) // broken pipe { -#ifdef _DEBUG - log_error("write(STDOUT) EOF\n"); -#endif + log_debug("write(STDOUT) EOF\n"); stdout_write_wait = 0; loop = 0; break; @@ -1170,7 +1310,17 @@ static int bbsnet_connect(int n) } } + ret = 1; // Normal disconnect + BBS_last_access_tm = time(NULL); + t_used = BBS_last_access_tm - t_begin; + log_common("BBSNET disconnect, %ld days %ld hours %ld minutes %ld seconds used\n", + t_used / 86400, t_used % 86400 / 3600, t_used % 3600 / 60, t_used % 60); + cleanup: + // Clear sensitive data + memset(remote_pass, 0, sizeof(remote_pass)); + memset(remote_user, 0, sizeof(remote_user)); + if (input_cd != (iconv_t)(-1)) { iconv_close(input_cd); @@ -1189,19 +1339,21 @@ cleanup: if (bbsnet_conf[n].use_ssh) { - if (channel != NULL) + if (outbound_channel != NULL) { - ssh_channel_free(channel); + ssh_channel_send_eof(outbound_channel); + ssh_channel_close(outbound_channel); + ssh_channel_free(outbound_channel); } - if (session != NULL) + if (outbound_session != NULL) { - ssh_disconnect(session); - ssh_free(session); + ssh_disconnect(outbound_session); + ssh_free(outbound_session); } } // Restore STDIN/STDOUT flags - if (flags_stdin != -1 &&fcntl(STDIN_FILENO, F_SETFL, flags_stdin) == -1) + if (flags_stdin != -1 && fcntl(STDIN_FILENO, F_SETFL, flags_stdin) == -1) { log_error("fcntl(F_SETFL) error (%d)\n", errno); } @@ -1215,16 +1367,12 @@ cleanup: log_error("Close socket failed\n"); } - t_used = time(NULL) - t_used; - tm_used = gmtime(&t_used); - - log_common("BBSNET disconnect, %d days %d hours %d minutes %d seconds used\n", - tm_used->tm_mday - 1, tm_used->tm_hour, tm_used->tm_min, - tm_used->tm_sec); - - BBS_last_access_tm = time(NULL); + if (res) + { + freeaddrinfo(res); + } - return 0; + return ret; } static int bbsnet_refresh() @@ -1266,7 +1414,7 @@ static int bbsnet_selchange() prints("|"); moveto(21, 1); clrtoeol(); - prints("|\033[1m连往: \033[1;33m%-20s\033[m 端口: \033[1;33m%-5d\033[m 编码: \033[1;33m%s\033[m", + prints("|\033[1m连往: \033[1;33m%-20s\033[m 端口: \033[1;33m%-5s\033[m 编码: \033[1;33m%s\033[m", bbsnet_conf[i].host_name, bbsnet_conf[i].port, bbsnet_conf[i].charset); moveto(21, 80); prints("|"); @@ -1277,7 +1425,7 @@ static int bbsnet_selchange() int bbs_net() { - int ch, i; + int ch; if (load_bbsnet_conf(CONF_BBSNET) < 0) { @@ -1304,7 +1452,7 @@ int bbs_net() switch (ch) { case KEY_NULL: // broken pipe - log_error("KEY_NULL\n"); + log_debug("KEY_NULL\n"); goto cleanup; case KEY_TIMEOUT: if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time) @@ -1317,7 +1465,10 @@ int bbs_net() case Ctrl('C'): // user cancel goto cleanup; case CR: - bbsnet_connect(bbsnet_menu.menu_item_pos[0]); + if (bbsnet_connect(bbsnet_menu.menu_item_pos[0]) < 0) + { + log_debug("bbsnet_connect() error\n"); + } // Force cleanup anything remaining in the output buffer clearscr(); iflush(); @@ -1327,14 +1478,14 @@ int bbs_net() bbsnet_selchange(); break; case KEY_UP: - for (i = 0; i < STATION_PER_LINE; i++) + for (int i = 0; i < STATION_PER_LINE; i++) { menu_control(&bbsnet_menu, KEY_UP); } bbsnet_selchange(); break; case KEY_DOWN: - for (i = 0; i < STATION_PER_LINE; i++) + for (int i = 0; i < STATION_PER_LINE; i++) { menu_control(&bbsnet_menu, KEY_DOWN); }