--- lbbs/src/bbs_net.c 2025/06/04 13:42:53 1.46 +++ lbbs/src/bbs_net.c 2025/10/10 14:02:41 1.63 @@ -16,26 +16,31 @@ #include "bbs.h" #include "common.h" -#include "log.h" #include "io.h" -#include "screen.h" +#include "log.h" +#include "login.h" #include "menu.h" -#include -#include +#include "screen.h" #include -#include -#include #include +#include +#include +#include +#include +#include #include #include -#include +#include +#include +#include +#include +#include +#include +#include #include #include #include #include -#include -#include -#include #define MENU_CONF_DELIM " \t\r\n" @@ -43,12 +48,15 @@ #define MAXSTATION 26 * 2 #define STATION_PER_LINE 4 +#define BBS_NET_DEFAULT_CHARSET "UTF-8" + struct _bbsnet_conf { char host1[20]; char host2[40]; char ip[40]; in_port_t port; + char charset[20]; } bbsnet_conf[MAXSTATION]; MENU_SET bbsnet_menu; @@ -59,7 +67,7 @@ int load_bbsnet_conf(const char *file_co MENU *p_menu; MENU_ITEM *p_menu_item; MENU_ITEM_ID menu_item_id; - char t[256], *t1, *t2, *t3, *t4, *saveptr; + char t[256], *t1, *t2, *t3, *t4, *t5, *saveptr; fp = fopen(file_config, "r"); if (fp == NULL) @@ -97,8 +105,9 @@ int load_bbsnet_conf(const char *file_co 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); - if (t1 == NULL || t2 == NULL || t3 == NULL || t4 == NULL || t[0] == '#' || t[0] == '*') + if (t1 == NULL || t2 == NULL || t3 == NULL || t4 == NULL || t5 == NULL || t[0] == '#' || t[0] == '*') { continue; } @@ -110,6 +119,8 @@ int load_bbsnet_conf(const char *file_co 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); + 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) @@ -184,6 +195,71 @@ void process_bar(int n, int len) iflush(); } +int bbsnet_io_buf_conv(iconv_t cd, char *p_buf, int *p_buf_len, int *p_buf_offset, char *p_conv, size_t conv_size, int *p_conv_len) +{ + char *in_buf; + char *out_buf; + size_t in_bytes; + size_t out_bytes; + int ret; + + in_buf = p_buf + *p_buf_offset; + in_bytes = (size_t)(*p_buf_len - *p_buf_offset); + out_buf = p_conv + *p_conv_len; + out_bytes = conv_size - (size_t)(*p_conv_len); + + while (in_bytes > 0) + { + ret = (int)iconv(cd, &in_buf, &in_bytes, &out_buf, &out_bytes); + if (ret == -1) + { + if (errno == EINVAL) // Incomplete + { +#ifdef _DEBUG + log_error("iconv(inbytes=%d, outbytes=%d) error: EINVAL\n", in_bytes, out_bytes); +#endif + *p_buf_len = (int)(p_buf + *p_buf_len - in_buf); + *p_buf_offset = 0; + *p_conv_len = (int)(conv_size - out_bytes); + memmove(p_buf, in_buf, (size_t)(*p_buf_len)); + + break; + } + else if (errno == E2BIG) + { + log_error("iconv(inbytes=%d, outbytes=%d) error: E2BIG\n", in_bytes, out_bytes); + return -1; + } + else if (errno == EILSEQ) + { + if (in_bytes > out_bytes || out_bytes <= 0) + { + log_error("iconv(inbytes=%d, outbytes=%d) error: EILSEQ and E2BIG\n", in_bytes, out_bytes); + return -2; + } + + *out_buf = *in_buf; + in_buf++; + out_buf++; + in_bytes--; + out_bytes--; + + continue; + } + } + else + { + *p_buf_len = 0; + *p_buf_offset = 0; + *p_conv_len = (int)(conv_size - out_bytes); + + break; + } + } + + return 0; +} + int bbsnet_connect(int n) { int sock, ret, loop, error; @@ -199,6 +275,14 @@ int bbsnet_connect(int n) int output_buf_len = 0; int input_buf_offset = 0; int output_buf_offset = 0; + iconv_t input_cd = NULL; + char input_conv[LINE_BUFFER_LEN * 2]; + char output_conv[LINE_BUFFER_LEN * 2]; + int input_conv_len = 0; + int output_conv_len = 0; + int input_conv_offset = 0; + int output_conv_offset = 0; + iconv_t output_cd = NULL; struct epoll_event ev, events[MAX_EVENTS]; int nfds, epollfd; int stdin_read_wait = 0; @@ -209,14 +293,22 @@ int bbsnet_connect(int n) int tos; char remote_addr[IP_ADDR_LEN]; int remote_port; - time_t t_used; + char local_addr[IP_ADDR_LEN]; + int local_port; + socklen_t sock_len; + time_t t_used = time(NULL); struct tm *tm_used; int ch; + if (user_online_update("BBS_NET") < 0) + { + log_error("user_online_update(BBS_NET) error\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(); @@ -224,7 +316,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; } @@ -233,13 +325,13 @@ 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; } sin.sin_family = AF_INET; - sin.sin_addr.s_addr = (hostaddr_server[0] != '\0' ? inet_addr(hostaddr_server) : INADDR_ANY); + 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) @@ -249,7 +341,7 @@ int bbsnet_connect(int n) return -2; } - bzero(&sin, sizeof(sin)); + 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); @@ -258,7 +350,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 @@ -278,7 +370,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) { @@ -286,7 +378,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) { @@ -313,7 +405,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; @@ -371,7 +463,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; @@ -383,9 +475,35 @@ int bbsnet_connect(int n) log_error("setsockopt IP_TOS=%d error (%d)\n", tos, errno); } - prints("\033[1;31m连接成功!\033[m\r\n"); + sock_len = sizeof(sin); + if (getsockname(sock, (struct sockaddr *)&sin, &sock_len) < 0) + { + log_error("getsockname() error: %d", errno); + goto cleanup; + } + + strncpy(local_addr, inet_ntoa(sin.sin_addr), sizeof(local_addr) - 1); + local_addr[sizeof(local_addr) - 1] = '\0'; + local_port = ntohs(sin.sin_port); + + prints("\033[1;31m杩炴帴鎴愬姛锛乗033[m\r\n"); iflush(); - log_common("BBSNET connect to %s:%d\n", remote_addr, remote_port); + log_common("BBSNET connect to %s:%d from %s:%d by [%s]\n", + remote_addr, remote_port, local_addr, local_port, BBS_username); + + input_cd = iconv_open(bbsnet_conf[n].charset, BBS_NET_DEFAULT_CHARSET); + if (input_cd == (iconv_t)(-1)) + { + log_error("iconv_open(%s->%s) error: %d\n", BBS_NET_DEFAULT_CHARSET, bbsnet_conf[n].charset, errno); + goto cleanup; + } + output_cd = iconv_open(BBS_NET_DEFAULT_CHARSET, bbsnet_conf[n].charset); + if (input_cd == (iconv_t)(-1)) + { + log_error("iconv_open(%s->%s) error: %d\n", bbsnet_conf[n].charset, BBS_NET_DEFAULT_CHARSET, errno); + iconv_close(input_cd); + goto cleanup; + } ev.events = EPOLLIN | EPOLLOUT | EPOLLET; ev.data.fd = sock; @@ -395,7 +513,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) { @@ -403,11 +521,18 @@ int bbsnet_connect(int n) goto cleanup; } - BBS_last_access_tm = t_used = time(0); + BBS_last_access_tm = t_used = 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"); + loop = 0; + break; + } + nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second if (nfds < 0) @@ -421,186 +546,276 @@ int bbsnet_connect(int n) } else if (nfds == 0) // timeout { - if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME) + if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME) { 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) { - ret = (int)read(STDIN_FILENO, input_buf + input_buf_len, sizeof(input_buf) - (size_t)input_buf_len); - if (ret < 0) + 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) + { + 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) { - 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; - } + log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session)); + loop = 0; + break; } - else if (ret == 0) // broken pipe + else if (ret == SSH_EOF) { - log_common("read(STDIN) EOF\n"); stdin_read_wait = 0; loop = 0; break; } - else + else if (ret == 0) + { + stdin_read_wait = 0; + break; // Check whether channel is still open + } + } + 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) + { + stdin_read_wait = 0; + break; + } + else if (errno == EINTR) { - input_buf_len += ret; - BBS_last_access_tm = time(0); 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) + { + if (input_buf_offset < input_buf_len) { - sock_write_wait = 1; - while (input_buf_offset < input_buf_len && !SYS_server_exit) + ret = bbsnet_io_buf_conv(input_cd, input_buf, &input_buf_len, &input_buf_offset, input_conv, sizeof(input_conv), &input_conv_len); + if (ret < 0) { - ret = (int)write(sock, input_buf + input_buf_offset, (size_t)(input_buf_len - input_buf_offset)); - if (ret < 0) + log_error("bbsnet_io_buf_conv(input, %d, %d, %d) error\n", input_buf_len, input_buf_offset, input_conv_len); + } + } + + 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 (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) { - log_common("write(socket) EOF\n"); - 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_conv_offset += ret; + if (input_conv_offset >= input_conv_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_conv_offset = 0; + input_conv_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) { - log_common("read(socket) EOF\n"); 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) + { + if (output_buf_offset < output_buf_len) { - stdout_write_wait = 1; - while (output_buf_offset < output_buf_len && !SYS_server_exit) + ret = bbsnet_io_buf_conv(output_cd, output_buf, &output_buf_len, &output_buf_offset, output_conv, sizeof(output_conv), &output_conv_len); + if (ret < 0) + { + log_error("bbsnet_io_buf_conv(output, %d, %d, %d) error\n", output_buf_len, output_buf_offset, output_conv_len); + } + } + + while (output_conv_offset < output_conv_len && !SYS_server_exit) + { + if (SSH_v2) { - ret = (int)write(STDOUT_FILENO, output_buf + output_buf_offset, (size_t)(output_buf_len - output_buf_offset)); - if (ret < 0) + ret = ssh_channel_write(SSH_channel, output_conv + output_conv_offset, (uint32_t)(output_conv_len - output_conv_offset)); + if (ret == SSH_ERROR) { - 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; - } + log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session)); + loop = 0; + break; } - else if (ret == 0) // broken pipe + } + else + { + ret = (int)write(STDOUT_FILENO, output_conv + output_conv_offset, (size_t)(output_conv_len - output_conv_offset)); + } + if (ret < 0) + { + if (errno == EAGAIN || errno == EWOULDBLOCK) { - log_common("write(STDOUT) EOF\n"); stdout_write_wait = 0; - loop = 0; break; } - else + else if (errno == EINTR) { - output_buf_offset += ret; - if (output_buf_offset >= output_buf_len) // Output buffer complete - { - output_buf_offset = 0; - output_buf_len = 0; - break; - } continue; } + else + { + log_error("write(STDOUT) error (%d)\n", errno); + loop = 0; + break; + } + } + else if (ret == 0) // broken pipe + { +#ifdef _DEBUG + log_error("write(STDOUT) EOF\n"); +#endif + stdout_write_wait = 0; + loop = 0; + break; + } + else + { + output_conv_offset += ret; + if (output_conv_offset >= output_conv_len) // Output buffer complete + { + output_conv_offset = 0; + output_conv_len = 0; + break; + } + continue; } } } } + iconv_close(input_cd); + iconv_close(output_cd); + cleanup: if (close(epollfd) < 0) { @@ -619,12 +834,12 @@ cleanup: log_error("Close socket failed\n"); } - t_used = time(0) - t_used; + 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_mday - 1, tm_used->tm_hour, tm_used->tm_min, + tm_used->tm_sec); return 0; } @@ -647,7 +862,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(); @@ -660,13 +875,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); @@ -685,7 +900,7 @@ int bbs_net() load_bbsnet_conf(CONF_BBSNET); - BBS_last_access_tm = time(0); + BBS_last_access_tm = time(NULL); clearscr(); bbsnet_refresh(); @@ -695,14 +910,15 @@ int bbs_net() while (!SYS_server_exit) { ch = igetch(100); + switch (ch) { - case KEY_NULL: // broken pipe + case KEY_NULL: // broken pipe case KEY_ESC: case Ctrl('C'): // user cancel goto cleanup; case KEY_TIMEOUT: - if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME) + if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME) { goto cleanup; } @@ -751,7 +967,7 @@ int bbs_net() bbsnet_selchange(); break; } - BBS_last_access_tm = time(0); + BBS_last_access_tm = time(NULL); } cleanup: