--- lbbs/src/bbs_net.c 2025/11/04 14:58:56 1.77 +++ lbbs/src/bbs_net.c 2025/12/17 03:47:00 1.91 @@ -6,13 +6,19 @@ * Copyright (C) 2004-2025 Leaflet */ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + #include "bbs.h" +#include "bbs_net.h" #include "common.h" #include "io.h" #include "log.h" #include "login.h" #include "menu.h" #include "screen.h" +#include "str_process.h" #include #include #include @@ -23,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -31,44 +38,55 @@ #include #include #include + +#ifdef HAVE_SYS_EPOLL_H #include +#else +#include +#endif -#define MENU_CONF_DELIM " \t\r\n" +static const char MENU_CONF_DELIM[] = " \t\r\n"; -#define MAX_PROCESS_BAR_LEN 30 -#define MAXSTATION 26 * 2 -#define STATION_PER_LINE 4 +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, + SSH_CONNECT_TIMEOUT = 5, // seconds +}; struct _bbsnet_conf { - char host1[20]; - char host2[40]; - char ip[40]; + char org_name[40]; + char site_name[40]; + char host_name[IP_ADDR_LEN]; in_port_t port; - char charset[20]; + int8_t use_ssh; + char charset[CHARSET_MAX_LEN + 1]; } bbsnet_conf[MAXSTATION]; static MENU_SET bbsnet_menu; +static void unload_bbsnet_conf(void); + static int load_bbsnet_conf(const char *file_config) { FILE *fp; MENU *p_menu; MENU_ITEM *p_menu_item; MENU_ITEM_ID menu_item_id; - char t[256], *t1, *t2, *t3, *t4, *t5, *saveptr; + char line[LINE_BUFFER_LEN], *t1, *t2, *t3, *t4, *t5, *t6, *saveptr; + int port; - fp = fopen(file_config, "r"); - if (fp == NULL) - { - return -1; - } + unload_bbsnet_conf(); bbsnet_menu.p_menu_pool = calloc(1, sizeof(MENU)); if (bbsnet_menu.p_menu_pool == NULL) { log_error("calloc(p_menu_pool) error\n"); - return -3; + return -1; } bbsnet_menu.menu_count = 1; @@ -76,7 +94,8 @@ static int load_bbsnet_conf(const char * if (bbsnet_menu.p_menu_item_pool == NULL) { log_error("calloc(p_menu_item_pool) error\n"); - return -3; + unload_bbsnet_conf(); + return -1; } bbsnet_menu.menu_item_count = MAXSTATION; @@ -87,35 +106,55 @@ static int load_bbsnet_conf(const char * p_menu->title.show = 0; p_menu->screen_show = 0; + fp = fopen(file_config, "r"); + if (fp == NULL) + { + unload_bbsnet_conf(); + return -2; + } + menu_item_id = 0; - while (fgets(t, 255, fp) && menu_item_id < MAXSTATION) + while (fgets(line, sizeof(line), fp) && menu_item_id < MAXSTATION) { - t1 = strtok_r(t, MENU_CONF_DELIM, &saveptr); + t1 = strtok_r(line, MENU_CONF_DELIM, &saveptr); t2 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr); t3 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr); t4 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr); t5 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr); + t6 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr); - if (t1 == NULL || t2 == NULL || t3 == NULL || t4 == NULL || t5 == NULL || t[0] == '#' || t[0] == '*') + if (t1 == NULL || t2 == NULL || t3 == NULL || t4 == NULL || + t5 == NULL || t6 == NULL || line[0] == '#' || line[0] == '*') { continue; } - strncpy(bbsnet_conf[menu_item_id].host1, t2, sizeof(bbsnet_conf[menu_item_id].host1) - 1); - bbsnet_conf[menu_item_id].host1[sizeof(bbsnet_conf[menu_item_id].host1) - 1] = '\0'; - strncpy(bbsnet_conf[menu_item_id].host2, t1, sizeof(bbsnet_conf[menu_item_id].host2) - 1); - bbsnet_conf[menu_item_id].host2[sizeof(bbsnet_conf[menu_item_id].host2) - 1] = '\0'; - strncpy(bbsnet_conf[menu_item_id].ip, t3, sizeof(bbsnet_conf[menu_item_id].ip) - 1); - bbsnet_conf[menu_item_id].ip[sizeof(bbsnet_conf[menu_item_id].ip) - 1] = '\0'; - bbsnet_conf[menu_item_id].port = (in_port_t)(t4 ? atoi(t4) : 23); - strncpy(bbsnet_conf[menu_item_id].charset, t5, sizeof(bbsnet_conf[menu_item_id].charset) - 1); + strncpy(bbsnet_conf[menu_item_id].site_name, t2, sizeof(bbsnet_conf[menu_item_id].site_name) - 1); + bbsnet_conf[menu_item_id].site_name[sizeof(bbsnet_conf[menu_item_id].site_name) - 1] = '\0'; + strncpy(bbsnet_conf[menu_item_id].org_name, t1, sizeof(bbsnet_conf[menu_item_id].org_name) - 1); + 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) + { + log_error("Invalid port value %d 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; + 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'; p_menu_item = get_menu_item_by_id(&bbsnet_menu, menu_item_id); if (p_menu_item == NULL) { log_error("get_menu_item_by_id(%d) return NULL pointer\n", menu_item_id); - return -1; + fclose(fp); + unload_bbsnet_conf(); + return -3; } p_menu_item->row = (int16_t)(2 + menu_item_id / STATION_PER_LINE); @@ -127,8 +166,8 @@ static int load_bbsnet_conf(const char * p_menu_item->name[0] = (char)(menu_item_id < MAXSTATION / 2 ? 'A' + menu_item_id : 'a' + menu_item_id); p_menu_item->name[1] = '\0'; - snprintf(p_menu_item->text, sizeof(p_menu_item->text), "%c. %s", - p_menu_item->name[0], bbsnet_conf[menu_item_id].host1); + 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); p_menu->items[p_menu->item_count] = menu_item_id; p_menu->item_count++; @@ -150,10 +189,17 @@ static void unload_bbsnet_conf(void) bbsnet_menu.menu_count = 0; bbsnet_menu.menu_item_count = 0; - free(bbsnet_menu.p_menu_pool); - bbsnet_menu.p_menu_pool = NULL; - free(bbsnet_menu.p_menu_item_pool); - bbsnet_menu.p_menu_item_pool = NULL; + if (bbsnet_menu.p_menu_pool) + { + free(bbsnet_menu.p_menu_pool); + bbsnet_menu.p_menu_pool = NULL; + } + + if (bbsnet_menu.p_menu_item_pool) + { + free(bbsnet_menu.p_menu_item_pool); + bbsnet_menu.p_menu_item_pool = NULL; + } } static void process_bar(int n, int len) @@ -161,7 +207,11 @@ static void process_bar(int n, int len) char buf[LINE_BUFFER_LEN]; char buf2[LINE_BUFFER_LEN]; - if (len > LINE_BUFFER_LEN) + if (len <= 0) + { + len = 1; + } + else if (len > LINE_BUFFER_LEN) { len = LINE_BUFFER_LEN - 1; } @@ -174,7 +224,7 @@ static void process_bar(int n, int len) n = len; } - moveto(4, 0); + moveto(4, 1); prints(" ------------------------------ \r\n"); snprintf(buf, sizeof(buf), " %3d%% ", n * 100 / len); memcpy(buf2, buf, (size_t)n); @@ -186,12 +236,14 @@ static void process_bar(int n, int len) static int bbsnet_connect(int n) { - int sock, ret, loop, error; + int sock = -1; + int ret; + int loop; + int error; int sock_connected = 0; - int flags_sock; - int flags_stdin; - int flags_stdout; - int len; + int flags_sock = -1; + int flags_stdin = -1; + int flags_stdout = -1; struct sockaddr_in sin; char input_buf[LINE_BUFFER_LEN]; char output_buf[LINE_BUFFER_LEN]; @@ -205,11 +257,18 @@ static int bbsnet_connect(int n) int output_conv_len = 0; int input_conv_offset = 0; int output_conv_offset = 0; - iconv_t input_cd = NULL; - iconv_t output_cd = NULL; - char tocode[32]; + iconv_t input_cd = (iconv_t)(-1); + iconv_t output_cd = (iconv_t)(-1); + char tocode[CHARSET_MAX_LEN + 20]; + +#ifdef HAVE_SYS_EPOLL_H struct epoll_event ev, events[MAX_EVENTS]; - int nfds, epollfd; + int epollfd = -1; +#else + struct pollfd pfds[3]; +#endif + + int nfds; int stdin_read_wait = 0; int stdout_write_wait = 0; int sock_read_wait = 0; @@ -221,29 +280,75 @@ static int bbsnet_connect(int n) char local_addr[IP_ADDR_LEN]; int local_port; socklen_t sock_len; + time_t t_begin; time_t t_used = time(NULL); struct tm *tm_used; int ch; + char remote_user[USERNAME_MAX_LEN + 1]; + char remote_pass[PASSWORD_MAX_LEN + 1]; + ssh_session session = NULL; + ssh_channel channel = NULL; + int ssh_process_config = 0; + int ssh_log_level = SSH_LOG_NOLOG; if (user_online_update("BBS_NET") < 0) { log_error("user_online_update(BBS_NET) error\n"); } + if (bbsnet_conf[n].use_ssh) + { + clearscr(); + + if (!SSH_v2) + { + moveto(1, 1); + prints("只有在以SSH方式登陆本站时,才能使用SSH站点穿梭。"); + press_any_key(); + return 0; + } + + moveto(1, 1); + prints("通过SSH方式连接[%s]...", bbsnet_conf[n].site_name); + moveto(2, 1); + prints("请输入用户名: "); + iflush(); + if (str_input(remote_user, sizeof(remote_user), DOECHO) < 0) + { + return -1; + } + if (remote_user[0] == '\0') + { + return 0; + } + + moveto(3, 1); + prints("请输入密码: "); + iflush(); + if (str_input(remote_pass, sizeof(remote_pass), NOECHO) < 0) + { + return -1; + } + if (remote_pass[0] == '\0') + { + return 0; + } + } + clearscr(); - moveto(0, 0); + moveto(1, 1); prints("\033[1;32m正在测试往 %s (%s) 的连接,请稍候... \033[m\r\n", - bbsnet_conf[n].host1, bbsnet_conf[n].ip); + bbsnet_conf[n].site_name, bbsnet_conf[n].host_name); iflush(); - p_host = gethostbyname(bbsnet_conf[n].ip); + p_host = gethostbyname(bbsnet_conf[n].host_name); if (p_host == NULL) { prints("\033[1;31m查找主机名失败!\033[m\r\n"); press_any_key(); - return -1; + goto cleanup; } sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); @@ -252,7 +357,7 @@ static int bbsnet_connect(int n) { prints("\033[1;31m无法创建socket!\033[m\r\n"); press_any_key(); - return -1; + goto cleanup; } sin.sin_family = AF_INET; @@ -263,7 +368,7 @@ static int bbsnet_connect(int n) { log_error("Bind address %s:%u failed (%d)\n", inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), errno); - return -2; + goto cleanup; } memset(&sin, 0, sizeof(sin)); @@ -279,20 +384,45 @@ static int bbsnet_connect(int n) process_bar(0, MAX_PROCESS_BAR_LEN); // Set socket as non-blocking - flags_sock = fcntl(sock, F_GETFL, 0); - fcntl(sock, F_SETFL, flags_sock | O_NONBLOCK); + if ((flags_sock = fcntl(sock, F_GETFL, 0)) == -1) + { + log_error("fcntl(F_GETFL) error (%d)\n", errno); + goto cleanup; + } + if ((fcntl(sock, F_SETFL, flags_sock | O_NONBLOCK)) == -1) + { + log_error("fcntl(F_SETFL) error (%d)\n", errno); + goto cleanup; + } // Set STDIN/STDOUT as non-blocking - flags_stdin = fcntl(STDIN_FILENO, F_GETFL, 0); - flags_stdout = fcntl(STDOUT_FILENO, F_GETFL, 0); - fcntl(STDIN_FILENO, F_SETFL, flags_stdin | O_NONBLOCK); - fcntl(STDOUT_FILENO, F_SETFL, flags_stdout | O_NONBLOCK); + if ((flags_stdin = fcntl(STDIN_FILENO, F_GETFL, 0)) == -1) + { + log_error("fcntl(F_GETFL) error (%d)\n", errno); + goto cleanup; + } + if ((flags_stdout = fcntl(STDOUT_FILENO, F_GETFL, 0)) == -1) + { + log_error("fcntl(F_GETFL) error (%d)\n", errno); + goto cleanup; + } + if ((fcntl(STDIN_FILENO, F_SETFL, flags_stdin | O_NONBLOCK)) == -1) + { + log_error("fcntl(F_SETFL) error (%d)\n", errno); + goto cleanup; + } + if ((fcntl(STDOUT_FILENO, F_SETFL, flags_stdout | O_NONBLOCK)) == -1) + { + log_error("fcntl(F_SETFL) error (%d)\n", errno); + goto cleanup; + } +#ifdef HAVE_SYS_EPOLL_H epollfd = epoll_create1(0); if (epollfd < 0) { log_error("epoll_create1() error (%d)\n", errno); - return -1; + goto cleanup; } ev.events = EPOLLOUT | EPOLLET; @@ -310,6 +440,7 @@ static int bbsnet_connect(int n) log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno); goto cleanup; } +#endif while (!SYS_server_exit) { @@ -340,17 +471,31 @@ static int bbsnet_connect(int n) for (int j = 0; j < MAX_PROCESS_BAR_LEN && !sock_connected && !SYS_server_exit; j++) { +#ifdef HAVE_SYS_EPOLL_H nfds = epoll_wait(epollfd, events, MAX_EVENTS, 500); // 0.5 second + ret = nfds; +#else + pfds[0].fd = sock; + pfds[0].events = POLLOUT; + pfds[1].fd = STDIN_FILENO; + pfds[1].events = POLLIN; + nfds = 2; + ret = poll(pfds, (nfds_t)nfds, 500); // 0.5 second +#endif - if (nfds < 0) + if (ret < 0) { if (errno != EINTR) { +#ifdef HAVE_SYS_EPOLL_H log_error("epoll_wait() error (%d)\n", errno); +#else + log_error("poll() error (%d)\n", errno); +#endif break; } } - else if (nfds == 0) // timeout + else if (ret == 0) // timeout { process_bar(j + 1, MAX_PROCESS_BAR_LEN); } @@ -358,12 +503,16 @@ static int bbsnet_connect(int n) { for (int i = 0; i < nfds; i++) { +#ifdef HAVE_SYS_EPOLL_H if (events[i].data.fd == sock) +#else + if (pfds[i].fd == sock && (pfds[i].revents & POLLOUT)) +#endif { - len = sizeof(error); - if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, (socklen_t *)&len) < 0) + socklen_t len = sizeof(error); + if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { - log_error("getsockopt() error (%d) !\n", error); + log_error("getsockopt() error (%d) !\n", errno); goto cleanup; } if (error == 0) @@ -371,9 +520,16 @@ static int bbsnet_connect(int n) sock_connected = 1; } } +#ifdef HAVE_SYS_EPOLL_H else if (events[i].data.fd == STDIN_FILENO) +#else + else if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & POLLIN)) +#endif { - ch = igetch(0); + do + { + ch = igetch(0); + } while (ch == 0); if (ch == Ctrl('C') || ch == KEY_ESC) { goto cleanup; @@ -411,6 +567,193 @@ static int bbsnet_connect(int n) local_addr[sizeof(local_addr) - 1] = '\0'; local_port = ntohs(sin.sin_port); + if (bbsnet_conf[n].use_ssh) + { + session = ssh_new(); + if (session == NULL) + { + log_error("ssh_new() error\n"); + 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) + { + log_error("Error setting SSH options: %s\n", ssh_get_error(session)); + goto cleanup; + } + + ssh_set_blocking(session, 0); + + t_begin = time(NULL); + ret = SSH_ERROR; + while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT) + { + ret = ssh_connect(session); + if (ret == SSH_OK) + { + break; + } + else if (ret == SSH_AGAIN) + { + // log_error("ssh_connect() error: SSH_AGAIN\n"); + } + else // if (ret == SSH_ERROR) + { + log_error("ssh_connect() error: SSH_ERROR\n"); + goto cleanup; + } + } + if (ret != SSH_OK) + { + prints("\033[1;31m连接超时!\033[m\r\n"); + press_any_key(); + goto cleanup; + } + + ret = ssh_session_is_known_server(session); + switch (ret) + { + case SSH_KNOWN_HOSTS_NOT_FOUND: + case SSH_KNOWN_HOSTS_UNKNOWN: + if (ssh_session_update_known_hosts(session) != SSH_OK) + { + log_error("ssh_session_update_known_hosts(%s) error\n", bbsnet_conf[n].host_name); + prints("\033[1;31m无法添加服务器证书\033[m\r\n"); + press_any_key(); + goto cleanup; + } + log_common("SSH key of (%s) is added into %s\n", bbsnet_conf[n].host_name, SSH_KNOWN_HOSTS_FILE); + case SSH_KNOWN_HOSTS_OK: + break; + 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\r\n"); + press_any_key(); + goto cleanup; + } + + ret = SSH_AUTH_ERROR; + while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT) + { + ret = ssh_userauth_password(session, NULL, remote_pass); + if (ret == SSH_AUTH_SUCCESS) + { + break; + } + else if (ret == SSH_AUTH_AGAIN) + { + // log_error("ssh_userauth_password() error: SSH_AUTH_AGAIN\n"); + } + else if (ret == SSH_AUTH_ERROR) + { + log_error("ssh_userauth_password() error: SSH_AUTH_ERROR\n"); + 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"); + press_any_key(); + goto cleanup; + } + } + if (ret != SSH_AUTH_SUCCESS) + { + prints("\033[1;31m连接超时!\033[m\r\n"); + press_any_key(); + goto cleanup; + } + + channel = ssh_channel_new(session); + if (channel == NULL) + { + log_error("ssh_channel_new() error\n"); + goto cleanup; + } + + ret = SSH_ERROR; + while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT) + { + ret = ssh_channel_open_session(channel); + if (ret == SSH_OK) + { + break; + } + else if (ret == SSH_AGAIN) + { + // log_error("ssh_channel_open_session() error: SSH_AGAIN\n"); + } + else // if (ret == SSH_ERROR) + { + log_error("ssh_channel_open_session() error: SSH_ERROR\n"); + goto cleanup; + } + } + if (ret != SSH_OK) + { + prints("\033[1;31m连接超时!\033[m\r\n"); + press_any_key(); + goto cleanup; + } + + ret = SSH_ERROR; + while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT) + { + ret = ssh_channel_request_pty(channel); + if (ret == SSH_OK) + { + break; + } + else if (ret == SSH_AGAIN) + { + // log_error("ssh_channel_request_pty() error: SSH_AGAIN\n"); + } + else // if (ret == SSH_ERROR) + { + log_error("ssh_channel_request_pty() error: SSH_ERROR\n"); + goto cleanup; + } + } + if (ret != SSH_OK) + { + prints("\033[1;31m连接超时!\033[m\r\n"); + press_any_key(); + goto cleanup; + } + + ret = SSH_ERROR; + while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT) + { + ret = ssh_channel_request_shell(channel); + if (ret == SSH_OK) + { + break; + } + else if (ret == SSH_AGAIN) + { + // log_error("ssh_channel_request_shell() error: SSH_AGAIN\n"); + } + else // if (ret == SSH_ERROR) + { + log_error("ssh_channel_request_shell() error: SSH_ERROR\n"); + goto cleanup; + } + } + if (ret != SSH_OK) + { + prints("\033[1;31m连接超时!\033[m\r\n"); + press_any_key(); + goto cleanup; + } + } + prints("\033[1;31m连接成功!\033[m\r\n"); iflush(); log_common("BBSNET connect to %s:%d from %s:%d by [%s]\n", @@ -431,10 +774,10 @@ 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); - iconv_close(input_cd); goto cleanup; } +#ifdef HAVE_SYS_EPOLL_H ev.events = EPOLLIN | EPOLLOUT | EPOLLET; ev.data.fd = sock; if (epoll_ctl(epollfd, EPOLL_CTL_MOD, sock, &ev) == -1) @@ -450,6 +793,7 @@ static int bbsnet_connect(int n) log_error("epoll_ctl(STDOUT_FILENO) error (%d)\n", errno); goto cleanup; } +#endif BBS_last_access_tm = t_used = time(NULL); loop = 1; @@ -463,20 +807,43 @@ static int bbsnet_connect(int n) break; } + if (bbsnet_conf[n].use_ssh && ssh_channel_is_closed(channel)) + { + log_error("Remote SSH channel is closed\n"); + loop = 0; + break; + } + +#ifdef HAVE_SYS_EPOLL_H nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second + ret = nfds; +#else + pfds[0].fd = STDIN_FILENO; + pfds[0].events = POLLIN; + pfds[1].fd = sock; + pfds[1].events = POLLIN | POLLOUT; + pfds[2].fd = STDOUT_FILENO; + pfds[2].events = POLLOUT; + nfds = 3; + ret = poll(pfds, (nfds_t)nfds, 100); // 0.1 second +#endif - if (nfds < 0) + if (ret < 0) { if (errno != EINTR) { +#ifdef HAVE_SYS_EPOLL_H log_error("epoll_wait() error (%d)\n", errno); +#else + log_error("poll() error (%d)\n", errno); +#endif break; } continue; } - else if (nfds == 0) // timeout + else if (ret == 0) // timeout { - if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME) + if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time) { break; } @@ -484,24 +851,45 @@ 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) +#else + if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & POLLIN)) +#endif { stdin_read_wait = 1; } +#ifdef HAVE_SYS_EPOLL_H if (events[i].data.fd == sock) +#else + if (pfds[i].fd == sock) +#endif { +#ifdef HAVE_SYS_EPOLL_H if (events[i].events & EPOLLIN) +#else + if (pfds[i].revents & POLLIN) +#endif { sock_read_wait = 1; } + +#ifdef HAVE_SYS_EPOLL_H if (events[i].events & EPOLLOUT) +#else + if (pfds[i].revents & POLLOUT) +#endif { sock_write_wait = 1; } } +#ifdef HAVE_SYS_EPOLL_H if (events[i].data.fd == STDOUT_FILENO) +#else + if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & POLLOUT)) +#endif { stdout_write_wait = 1; } @@ -614,7 +1002,20 @@ static int bbsnet_connect(int n) while (input_conv_offset < input_conv_len && !SYS_server_exit) { - ret = (int)write(sock, input_conv + input_conv_offset, (size_t)(input_conv_len - input_conv_offset)); + if (bbsnet_conf[n].use_ssh) + { + ret = ssh_channel_write(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)); + loop = 0; + break; + } + } + else + { + ret = (int)write(sock, input_conv + input_conv_offset, (size_t)(input_conv_len - input_conv_offset)); + } if (ret < 0) { if (errno == EAGAIN || errno == EWOULDBLOCK) @@ -660,7 +1061,32 @@ static int bbsnet_connect(int n) { while (output_buf_len < sizeof(output_buf) && !SYS_server_exit) { - ret = (int)read(sock, output_buf + output_buf_len, sizeof(output_buf) - (size_t)output_buf_len); + if (bbsnet_conf[n].use_ssh) + { + ret = ssh_channel_read_nonblocking(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)); + loop = 0; + break; + } + else if (ret == SSH_EOF) + { + sock_read_wait = 0; + loop = 0; + break; + } + else if (ret == 0) + { + sock_read_wait = 0; + break; + } + } + else + { + ret = (int)read(sock, output_buf + output_buf_len, sizeof(output_buf) - (size_t)output_buf_len); + } if (ret < 0) { if (errno == EAGAIN || errno == EWOULDBLOCK) @@ -766,23 +1192,47 @@ static int bbsnet_connect(int n) } } - iconv_close(input_cd); - iconv_close(output_cd); - cleanup: - if (close(epollfd) < 0) + if (input_cd != (iconv_t)(-1)) + { + iconv_close(input_cd); + } + if (output_cd != (iconv_t)(-1)) + { + iconv_close(output_cd); + } + +#ifdef HAVE_SYS_EPOLL_H + if (epollfd != -1 && close(epollfd) < 0) { log_error("close(epoll) error (%d)\n"); } +#endif - // Restore STDIN/STDOUT flags - fcntl(STDIN_FILENO, F_SETFL, flags_stdin); - fcntl(STDOUT_FILENO, F_SETFL, flags_stdout); + if (bbsnet_conf[n].use_ssh) + { + if (channel != NULL) + { + ssh_channel_free(channel); + } + if (session != NULL) + { + ssh_disconnect(session); + ssh_free(session); + } + } - // Restore socket flags - fcntl(sock, F_SETFL, flags_sock); + // Restore STDIN/STDOUT flags + if (flags_stdin != -1 && fcntl(STDIN_FILENO, F_SETFL, flags_stdin) == -1) + { + log_error("fcntl(F_SETFL) error (%d)\n", errno); + } + if (flags_stdout != -1 && fcntl(STDOUT_FILENO, F_SETFL, flags_stdout) == -1) + { + log_error("fcntl(F_SETFL) error (%d)\n", errno); + } - if (close(sock) == -1) + if (sock != -1 && close(sock) == -1) { log_error("Close socket failed\n"); } @@ -791,8 +1241,7 @@ cleanup: 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); + tm_used->tm_yday, tm_used->tm_hour, tm_used->tm_min, tm_used->tm_sec); BBS_last_access_tm = time(NULL); @@ -802,21 +1251,22 @@ cleanup: static int bbsnet_refresh() { clearscr(); - moveto(1, 0); - prints(" ----------------------------------------------------------------------------- "); + + moveto(1, 1); + prints(" ------------------------------------------------------------------------------ "); for (int i = 2; i < 19; i++) { - moveto(i, 0); + moveto(i, 1); prints("|"); - moveto(i, 79); + moveto(i, 80); prints("|"); } - moveto(19, 0); - prints("|-----------------------------------------------------------------------------|"); - moveto(22, 0); - prints(" ----------------------------------------------------------------------------- "); - moveto(23, 0); - prints(" [\x1b[1;32mCtrl+C\x1b[m]退出"); + moveto(19, 1); + prints("|------------------------------------------------------------------------------|"); + moveto(22, 1); + prints(" ------------------------------------------------------------------------------ "); + moveto(23, 1); + prints(" [\033[1;32mCtrl+C\033[m]退出"); iflush(); @@ -827,34 +1277,38 @@ static int bbsnet_selchange() { int i = bbsnet_menu.menu_item_pos[0]; - moveto(20, 0); + moveto(20, 1); clrtoeol(); - prints("|\x1b[1m单位:\x1b[1;33m%-18s\x1b[m 站名:\x1b[1;33m%s\x1b[m", - bbsnet_conf[i].host2, bbsnet_conf[i].host1); - moveto(20, 79); + prints("|\033[1m单位: \033[1;33m%s\033[m%*s 站名: \033[1;33m%s\033[m%*s 类型: \033[1;33m%s\033[m", + bbsnet_conf[i].org_name, 20 - str_length(bbsnet_conf[i].org_name, 1), "", + bbsnet_conf[i].site_name, 20 - str_length(bbsnet_conf[i].site_name, 1), "", + (bbsnet_conf[i].use_ssh ? "SSH" : "Telnet")); + moveto(20, 80); prints("|"); - moveto(21, 0); + moveto(21, 1); clrtoeol(); - prints("|\x1b[1m连往:\x1b[1;33m%-20s", bbsnet_conf[i].ip); - if (bbsnet_conf[i].port != 23) - { - prints(" %d", bbsnet_conf[i].port); - } - prints("\x1b[m"); - moveto(21, 79); + prints("|\033[1m连往: \033[1;33m%-20s\033[m 端口: \033[1;33m%-5d\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("|"); iflush(); return 0; } -extern int bbs_net() +int bbs_net() { int ch, i; - load_bbsnet_conf(CONF_BBSNET); + if (load_bbsnet_conf(CONF_BBSNET) < 0) + { + clearscr(); + moveto(1, 1); + prints("加载穿梭配置失败!"); + press_any_key(); + return -1; + } - clearscr(); bbsnet_refresh(); display_menu(&bbsnet_menu); bbsnet_selchange(); @@ -871,10 +1325,12 @@ extern int bbs_net() switch (ch) { case KEY_NULL: // broken pipe +#ifdef _DEBUG log_error("KEY_NULL\n"); +#endif goto cleanup; case KEY_TIMEOUT: - if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME) + if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time) { log_error("User input timeout\n"); goto cleanup; @@ -885,6 +1341,10 @@ extern int bbs_net() goto cleanup; case CR: bbsnet_connect(bbsnet_menu.menu_item_pos[0]); + // Force cleanup anything remaining in the output buffer + clearscr(); + iflush(); + // Clear screen and redraw menu bbsnet_refresh(); display_menu(&bbsnet_menu); bbsnet_selchange();