--- lbbs/src/bbs_net.c 2025/06/21 02:15:18 1.55 +++ lbbs/src/bbs_net.c 2025/10/04 10:11:44 1.59 @@ -213,7 +213,7 @@ int bbsnet_connect(int n) int tos; char remote_addr[IP_ADDR_LEN]; int remote_port; - time_t t_used; + time_t t_used = time(NULL); struct tm *tm_used; int ch; @@ -225,7 +225,7 @@ int bbsnet_connect(int n) clearscr(); moveto(0, 0); - prints("\033[1;32m正在测试往 %s (%s) 的连接,请稍候... \033[m\r\n", + prints("\033[1;32m姝e湪娴嬭瘯寰 %s (%s) 鐨勮繛鎺ワ紝璇风◢鍊... \033[m\r\n", bbsnet_conf[n].host1, bbsnet_conf[n].ip); iflush(); @@ -233,7 +233,7 @@ int bbsnet_connect(int n) if (p_host == NULL) { - prints("\033[1;31m查找主机名失败!\033[m\r\n"); + prints("\033[1;31m鏌ユ壘涓绘満鍚嶅け璐ワ紒\033[m\r\n"); press_any_key(); return -1; } @@ -242,7 +242,7 @@ int bbsnet_connect(int n) if (sock < 0) { - prints("\033[1;31m无法创建socket!\033[m\r\n"); + prints("\033[1;31m鏃犳硶鍒涘缓socket锛乗033[m\r\n"); press_any_key(); return -1; } @@ -267,7 +267,7 @@ int bbsnet_connect(int n) remote_addr[sizeof(remote_addr) - 1] = '\0'; remote_port = ntohs(sin.sin_port); - prints("\033[1;32m穿梭进度条提示您当前已使用的时间,按\033[1;33mCtrl+C\033[1;32m中断。\033[m\r\n"); + prints("\033[1;32m绌挎杩涘害鏉℃彁绀烘偍褰撳墠宸蹭娇鐢ㄧ殑鏃堕棿锛屾寜\033[1;33mCtrl+C\033[1;32m涓柇銆俓033[m\r\n"); process_bar(0, MAX_PROCESS_BAR_LEN); // Set socket as non-blocking @@ -287,7 +287,7 @@ int bbsnet_connect(int n) return -1; } - ev.events = EPOLLOUT; + ev.events = EPOLLOUT | EPOLLET; ev.data.fd = sock; if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sock, &ev) == -1) { @@ -295,7 +295,7 @@ int bbsnet_connect(int n) goto cleanup; } - ev.events = EPOLLIN; + ev.events = EPOLLIN | EPOLLET; ev.data.fd = STDIN_FILENO; if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1) { @@ -322,7 +322,7 @@ int bbsnet_connect(int n) { log_error("connect(socket) error (%d)\n", errno); - prints("\033[1;31m连接失败!\033[m\r\n"); + prints("\033[1;31m杩炴帴澶辫触锛乗033[m\r\n"); press_any_key(); goto cleanup; @@ -380,7 +380,7 @@ int bbsnet_connect(int n) } if (!sock_connected) { - prints("\033[1;31m连接失败!\033[m\r\n"); + prints("\033[1;31m杩炴帴澶辫触锛乗033[m\r\n"); press_any_key(); goto cleanup; @@ -392,7 +392,7 @@ int bbsnet_connect(int n) log_error("setsockopt IP_TOS=%d error (%d)\n", tos, errno); } - prints("\033[1;31m连接成功!\033[m\r\n"); + prints("\033[1;31m杩炴帴鎴愬姛锛乗033[m\r\n"); iflush(); log_common("BBSNET connect to %s:%d\n", remote_addr, remote_port); @@ -404,7 +404,7 @@ int bbsnet_connect(int n) goto cleanup; } - ev.events = EPOLLOUT; + ev.events = EPOLLOUT | EPOLLET; ev.data.fd = STDOUT_FILENO; if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1) { @@ -441,222 +441,246 @@ int bbsnet_connect(int n) { break; } - continue; } for (int i = 0; i < nfds; i++) { - if (events[i].data.fd == STDIN_FILENO || stdin_read_wait) + if (events[i].data.fd == STDIN_FILENO) { stdin_read_wait = 1; - while (input_buf_len < sizeof(input_buf) && !SYS_server_exit) + } + + if (events[i].data.fd == sock) + { + if (events[i].events & EPOLLIN) + { + sock_read_wait = 1; + } + if (events[i].events & EPOLLOUT) + { + sock_write_wait = 1; + } + } + + if (events[i].data.fd == STDOUT_FILENO) + { + stdout_write_wait = 1; + } + } + + if (stdin_read_wait) + { + while (input_buf_len < sizeof(input_buf) && !SYS_server_exit) + { + if (SSH_v2) { - if (SSH_v2) + 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) { - 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)); - loop = 0; - break; - } - else if (ret == SSH_EOF) - { - stdin_read_wait = 0; - loop = 0; - break; - } - else if (ret == 0) - { - stdin_read_wait = 0; - break; // Check whether channel is still open - } + log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session)); + loop = 0; + break; } - else + else if (ret == SSH_EOF) { - ret = (int)read(STDIN_FILENO, input_buf + input_buf_len, sizeof(input_buf) - (size_t)input_buf_len); + stdin_read_wait = 0; + loop = 0; + break; } - if (ret < 0) + else if (ret == 0) { - if (errno == EAGAIN || errno == EWOULDBLOCK) - { - stdin_read_wait = 0; - break; - } - else if (errno == EINTR) - { - continue; - } - else - { - log_error("read(STDIN) error (%d)\n", errno); - loop = 0; - break; - } + stdin_read_wait = 0; + break; // Check whether channel is still open } - else if (ret == 0) // broken pipe + } + else + { + ret = (int)read(STDIN_FILENO, input_buf + input_buf_len, sizeof(input_buf) - (size_t)input_buf_len); + } + if (ret < 0) + { + if (errno == EAGAIN || errno == EWOULDBLOCK) { -#ifdef _DEBUG - log_error("read(STDIN) EOF\n"); -#endif stdin_read_wait = 0; - loop = 0; break; } - else + else if (errno == EINTR) { - input_buf_len += ret; - BBS_last_access_tm = time(NULL); continue; } + else + { + log_error("read(STDIN) error (%d)\n", errno); + loop = 0; + break; + } + } + else if (ret == 0) // broken pipe + { +#ifdef _DEBUG + log_error("read(STDIN) EOF\n"); +#endif + stdin_read_wait = 0; + loop = 0; + break; + } + else + { + input_buf_len += ret; + BBS_last_access_tm = time(NULL); + + // Refresh current action while user input + if (user_online_update("BBS_NET") < 0) + { + log_error("user_online_update(BBS_NET) error\n"); + } + + continue; } } + } - if (events[i].data.fd == sock || sock_write_wait) // EPOLLOUT + if (sock_write_wait) + { + while (input_buf_offset < input_buf_len && !SYS_server_exit) { - sock_write_wait = 1; - while (input_buf_offset < input_buf_len && !SYS_server_exit) + ret = (int)write(sock, input_buf + input_buf_offset, (size_t)(input_buf_len - input_buf_offset)); + if (ret < 0) { - ret = (int)write(sock, input_buf + input_buf_offset, (size_t)(input_buf_len - input_buf_offset)); - if (ret < 0) + if (errno == EAGAIN || errno == EWOULDBLOCK) { - if (errno == EAGAIN || errno == EWOULDBLOCK) - { - sock_write_wait = 0; - break; - } - else if (errno == EINTR) - { - continue; - } - else - { - log_error("write(socket) error (%d)\n", errno); - loop = 0; - break; - } + sock_write_wait = 0; + break; } - else if (ret == 0) // broken pipe + else if (errno == EINTR) { -#ifdef _DEBUG - log_error("write(socket) EOF\n"); -#endif - sock_write_wait = 0; + continue; + } + else + { + log_error("write(socket) error (%d)\n", errno); loop = 0; break; } - else + } + else if (ret == 0) // broken pipe + { +#ifdef _DEBUG + log_error("write(socket) EOF\n"); +#endif + sock_write_wait = 0; + loop = 0; + break; + } + else + { + input_buf_offset += ret; + if (input_buf_offset >= input_buf_len) // Output buffer complete { - input_buf_offset += ret; - if (input_buf_offset >= input_buf_len) // Output buffer complete - { - input_buf_offset = 0; - input_buf_len = 0; - break; - } - continue; + input_buf_offset = 0; + input_buf_len = 0; + break; } + continue; } } + } - if (events[i].data.fd == sock || sock_read_wait) // EPOLLIN + if (sock_read_wait) + { + while (output_buf_len < sizeof(output_buf) && !SYS_server_exit) { - sock_read_wait = 1; - 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 (ret < 0) { - 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) - { - sock_read_wait = 0; - break; - } - else if (errno == EINTR) - { - continue; - } - else - { - log_error("read(socket) error (%d)\n", errno); - loop = 0; - break; - } - } - else if (ret == 0) // broken pipe + if (errno == EAGAIN || errno == EWOULDBLOCK) { -#ifdef _DEBUG - log_error("read(socket) EOF\n"); -#endif sock_read_wait = 0; - loop = 0; break; } - else + else if (errno == EINTR) { - output_buf_len += ret; continue; } + else + { + log_error("read(socket) error (%d)\n", errno); + loop = 0; + break; + } + } + else if (ret == 0) // broken pipe + { +#ifdef _DEBUG + log_error("read(socket) EOF\n"); +#endif + sock_read_wait = 0; + loop = 0; + break; + } + else + { + output_buf_len += ret; + continue; } } + } - if (events[i].data.fd == STDOUT_FILENO || stdout_write_wait) + if (stdout_write_wait) + { + while (output_buf_offset < output_buf_len && !SYS_server_exit) { - stdout_write_wait = 1; - while (output_buf_offset < output_buf_len && !SYS_server_exit) + if (SSH_v2) { - if (SSH_v2) + ret = ssh_channel_write(SSH_channel, output_buf + output_buf_offset, (uint32_t)(output_buf_len - output_buf_offset)); + if (ret == SSH_ERROR) { - ret = ssh_channel_write(SSH_channel, output_buf + output_buf_offset, (uint32_t)(output_buf_len - output_buf_offset)); - if (ret == SSH_ERROR) - { - log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session)); - loop = 0; - break; - } + log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session)); + loop = 0; + break; } - else + } + else + { + ret = (int)write(STDOUT_FILENO, output_buf + output_buf_offset, (size_t)(output_buf_len - output_buf_offset)); + } + if (ret < 0) + { + if (errno == EAGAIN || errno == EWOULDBLOCK) { - ret = (int)write(STDOUT_FILENO, output_buf + output_buf_offset, (size_t)(output_buf_len - output_buf_offset)); + stdout_write_wait = 0; + break; } - if (ret < 0) + else if (errno == EINTR) { - if (errno == EAGAIN || errno == EWOULDBLOCK) - { - stdout_write_wait = 0; - break; - } - else if (errno == EINTR) - { - continue; - } - else - { - log_error("write(STDOUT) error (%d)\n", errno); - loop = 0; - break; - } + continue; } - else if (ret == 0) // broken pipe + else { -#ifdef _DEBUG - log_error("write(STDOUT) EOF\n"); -#endif - stdout_write_wait = 0; + log_error("write(STDOUT) error (%d)\n", errno); loop = 0; break; } - else + } + else if (ret == 0) // broken pipe + { +#ifdef _DEBUG + log_error("write(STDOUT) EOF\n"); +#endif + stdout_write_wait = 0; + loop = 0; + break; + } + else + { + output_buf_offset += ret; + if (output_buf_offset >= output_buf_len) // Output buffer complete { - output_buf_offset += ret; - if (output_buf_offset >= output_buf_len) // Output buffer complete - { - output_buf_offset = 0; - output_buf_len = 0; - break; - } - continue; + output_buf_offset = 0; + output_buf_len = 0; + break; } + continue; } } } @@ -708,7 +732,7 @@ bbsnet_refresh() moveto(22, 0); prints(" ----------------------------------------------------------------------------- "); moveto(23, 0); - prints(" [\x1b[1;32mCtrl+C\x1b[m]退出"); + prints(" [\x1b[1;32mCtrl+C\x1b[m]閫鍑"); iflush(); @@ -721,13 +745,13 @@ int bbsnet_selchange() moveto(20, 0); clrtoeol(); - prints("|\x1b[1m单位:\x1b[1;33m%-18s\x1b[m 站名:\x1b[1;33m%s\x1b[m", + 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("|"); moveto(21, 0); clrtoeol(); - prints("|\x1b[1m连往:\x1b[1;33m%-20s", bbsnet_conf[i].ip); + prints("|\x1b[1m杩炲線:\x1b[1;33m%-20s", bbsnet_conf[i].ip); if (bbsnet_conf[i].port != 23) { prints(" %d", bbsnet_conf[i].port);