--- lbbs/src/bbs_net.c 2025/12/16 12:59:14 1.89 +++ lbbs/src/bbs_net.c 2025/12/18 03:23:48 1.96 @@ -54,6 +54,7 @@ enum _bbs_net_constant_t STATION_PER_LINE = 4, USERNAME_MAX_LEN = 20, PASSWORD_MAX_LEN = 20, + SSH_CONNECT_TIMEOUT = 5, // seconds }; struct _bbsnet_conf @@ -61,7 +62,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]; @@ -142,7 +143,8 @@ static int load_bbsnet_conf(const char * 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'; @@ -272,13 +274,14 @@ 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_begin; time_t t_used = time(NULL); struct tm *tm_used; int ch; @@ -288,7 +291,6 @@ static int bbsnet_connect(int n) ssh_channel 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,43 +343,60 @@ 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)\n", ret); goto cleanup; } - sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + if (inet_ntop(AF_INET, &(((struct sockaddr_in *)res->ai_addr)->sin_addr.s_addr), local_addr, sizeof(local_addr)) == NULL) + { + log_error("inet_ntop() error (%d)\n", errno); + 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); 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); 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; + + if ((ret = getaddrinfo(bbsnet_conf[n].host_name, bbsnet_conf[n].port, &hints, &res)) != 0) + { + log_error("getaddrinfo() error (%d)\n", ret); + prints("\033[1;31m查找主机名失败!\033[m\r\n"); + press_any_key(); + goto cleanup; + } + + if (inet_ntop(AF_INET, &(((struct sockaddr_in *)res->ai_addr)->sin_addr.s_addr), remote_addr, sizeof(remote_addr)) == NULL) + { + log_error("inet_ntop() error (%d)\n", errno); + goto cleanup; + } + remote_port = ntohs(((struct sockaddr_in *)res->ai_addr)->sin_port); prints("\033[1;32m穿梭进度条提示您当前已使用的时间,按\033[1;33mCtrl+C\033[1;32m中断。\033[m\r\n"); process_bar(0, MAX_PROCESS_BAR_LEN); @@ -443,7 +462,7 @@ static int bbsnet_connect(int n) 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) { if (errno == EAGAIN || errno == EALREADY || errno == EINPROGRESS) { @@ -459,10 +478,8 @@ 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(); - goto cleanup; } } @@ -545,7 +562,6 @@ static int bbsnet_connect(int n) { prints("\033[1;31m连接失败!\033[m\r\n"); press_any_key(); - goto cleanup; } @@ -587,26 +603,33 @@ static int bbsnet_connect(int n) 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(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); if (ret == SSH_OK) { break; } - else if (ret == SSH_ERROR) + else if (ret == SSH_AGAIN) + { + // log_error("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"); 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) @@ -616,7 +639,7 @@ static int bbsnet_connect(int n) 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"); + prints("\033[1;31m无法添加服务器证书\033[m\r\n"); press_any_key(); goto cleanup; } @@ -626,12 +649,13 @@ 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(); 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); if (ret == SSH_AUTH_SUCCESS) @@ -640,38 +664,27 @@ static int bbsnet_connect(int n) } else if (ret == SSH_AUTH_AGAIN) { -#ifdef _DEBUG - log_error("ssh_userauth_password() error: SSH_AUTH_AGAIN\n"); -#endif + // log_error("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"); 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(); + 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) @@ -680,56 +693,80 @@ static int bbsnet_connect(int n) 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); if (ret == SSH_OK) { break; } - else if (ret == SSH_ERROR) + 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\n"); + 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; + } - while (!SYS_server_exit) + 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_ERROR) + else if (ret == SSH_AGAIN) { - log_error("ssh_channel_request_pty() error\n"); + // 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; + } - while (!SYS_server_exit) + 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_ERROR) + else if (ret == SSH_AGAIN) { - log_error("ssh_channel_request_shell() error\n"); + // 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; } } - - 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(); goto cleanup; } - - ssh_set_blocking(session, 0); } prints("\033[1;31m连接成功!\033[m\r\n"); @@ -780,14 +817,14 @@ static int bbsnet_connect(int n) { 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)) { - log_error("Remote SSH channel is closed\n"); + log_debug("Remote SSH channel is closed\n"); loop = 0; break; } @@ -830,7 +867,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 +916,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 +934,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; } @@ -927,9 +979,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 +1008,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 +1023,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 } @@ -985,7 +1035,7 @@ static int bbsnet_connect(int n) 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)); + log_debug("ssh_channel_write() error: %s\n", ssh_get_error(session)); loop = 0; break; } @@ -1007,16 +1057,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; @@ -1045,7 +1093,7 @@ static int bbsnet_connect(int n) (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(session)); loop = 0; break; } @@ -1078,16 +1126,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 +1165,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 +1187,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; @@ -1201,7 +1245,7 @@ cleanup: } // 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,12 +1259,16 @@ cleanup: log_error("Close socket failed\n"); } + if (res) + { + freeaddrinfo(res); + } + 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); + tm_used->tm_yday, tm_used->tm_hour, tm_used->tm_min, tm_used->tm_sec); BBS_last_access_tm = time(NULL); @@ -1266,7 +1314,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("|"); @@ -1304,7 +1352,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)