--- lbbs/src/bbs_net.c 2025/10/10 14:02:41 1.63 +++ lbbs/src/bbs_net.c 2025/11/21 07:36:41 1.83 @@ -1,20 +1,17 @@ -/*************************************************************************** - bbs_net.c - description - ------------------- - Copyright : (C) 2004-2025 by Leaflet - Email : leaflet@leafok.com - ***************************************************************************/ - -/*************************************************************************** - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 3 of the License, or * - * (at your option) any later version. * - * * - ***************************************************************************/ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * bbs_net + * - user interactive feature of site shuttle + * + * 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" @@ -31,7 +28,6 @@ #include #include #include -#include #include #include #include @@ -40,15 +36,21 @@ #include #include #include -#include -#define MENU_CONF_DELIM " \t\r\n" +#ifdef HAVE_SYS_EPOLL_H +#include +#else +#include +#endif -#define MAX_PROCESS_BAR_LEN 30 -#define MAXSTATION 26 * 2 -#define STATION_PER_LINE 4 +static const char MENU_CONF_DELIM[] = " \t\r\n"; -#define BBS_NET_DEFAULT_CHARSET "UTF-8" +enum _bbs_net_constant_t +{ + MAX_PROCESS_BAR_LEN = 30, + MAXSTATION = 26 * 2, + STATION_PER_LINE = 4, +}; struct _bbsnet_conf { @@ -56,12 +58,12 @@ struct _bbsnet_conf char host2[40]; char ip[40]; in_port_t port; - char charset[20]; + char charset[CHARSET_MAX_LEN + 1]; } bbsnet_conf[MAXSTATION]; -MENU_SET bbsnet_menu; +static MENU_SET bbsnet_menu; -int load_bbsnet_conf(const char *file_config) +static int load_bbsnet_conf(const char *file_config) { FILE *fp; MENU *p_menu; @@ -156,7 +158,7 @@ int load_bbsnet_conf(const char *file_co return 0; } -void unload_bbsnet_conf(void) +static void unload_bbsnet_conf(void) { bbsnet_menu.menu_count = 0; bbsnet_menu.menu_item_count = 0; @@ -167,7 +169,7 @@ void unload_bbsnet_conf(void) bbsnet_menu.p_menu_item_pool = NULL; } -void process_bar(int n, int len) +static void process_bar(int n, int len) { char buf[LINE_BUFFER_LEN]; char buf2[LINE_BUFFER_LEN]; @@ -188,79 +190,14 @@ void process_bar(int n, int len) moveto(4, 0); prints(" ------------------------------ \r\n"); snprintf(buf, sizeof(buf), " %3d%% ", n * 100 / len); - strncpy(buf2, buf, (size_t)n); + 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"); 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) +static int bbsnet_connect(int n) { int sock, ret, loop, error; int sock_connected = 0; @@ -275,16 +212,24 @@ 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 input_cd = NULL; iconv_t output_cd = NULL; + char tocode[32]; + +#ifdef HAVE_SYS_EPOLL_H struct epoll_event ev, events[MAX_EVENTS]; - int nfds, epollfd; + int epollfd; +#else + struct pollfd pfds[3]; +#endif + + int nfds; int stdin_read_wait = 0; int stdout_write_wait = 0; int sock_read_wait = 0; @@ -363,6 +308,7 @@ int bbsnet_connect(int n) fcntl(STDIN_FILENO, F_SETFL, flags_stdin | O_NONBLOCK); fcntl(STDOUT_FILENO, F_SETFL, flags_stdout | O_NONBLOCK); +#ifdef HAVE_SYS_EPOLL_H epollfd = epoll_create1(0); if (epollfd < 0) { @@ -385,6 +331,7 @@ int bbsnet_connect(int n) log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno); goto cleanup; } +#endif while (!SYS_server_exit) { @@ -415,17 +362,31 @@ 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); } @@ -433,7 +394,11 @@ 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) @@ -446,7 +411,11 @@ 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); if (ch == Ctrl('C') || ch == KEY_ESC) @@ -491,20 +460,26 @@ int bbsnet_connect(int n) 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); + snprintf(tocode, sizeof(tocode), "%s%s", bbsnet_conf[n].charset, + (strcasecmp(stdio_charset, bbsnet_conf[n].charset) == 0 ? "" : "//IGNORE")); + input_cd = iconv_open(tocode, stdio_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); + log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, tocode, errno); goto cleanup; } - output_cd = iconv_open(BBS_NET_DEFAULT_CHARSET, bbsnet_conf[n].charset); - if (input_cd == (iconv_t)(-1)) + + snprintf(tocode, sizeof(tocode), "%s%s", stdio_charset, + (strcasecmp(bbsnet_conf[n].charset, stdio_charset) == 0 ? "" : "//TRANSLIT")); + output_cd = iconv_open(tocode, bbsnet_conf[n].charset); + if (output_cd == (iconv_t)(-1)) { - log_error("iconv_open(%s->%s) error: %d\n", bbsnet_conf[n].charset, BBS_NET_DEFAULT_CHARSET, errno); + 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) @@ -520,6 +495,7 @@ 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; @@ -533,20 +509,36 @@ int bbsnet_connect(int n) 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; } @@ -554,24 +546,45 @@ 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; } @@ -598,6 +611,11 @@ int bbsnet_connect(int n) } else if (ret == 0) { + // Send NO-OP to remote server + input_buf[input_buf_len] = '\0'; + input_buf_len++; + BBS_last_access_tm = time(NULL); + stdin_read_wait = 0; break; // Check whether channel is still open } @@ -653,11 +671,28 @@ int bbsnet_connect(int n) { if (input_buf_offset < input_buf_len) { - ret = bbsnet_io_buf_conv(input_cd, input_buf, &input_buf_len, &input_buf_offset, input_conv, sizeof(input_conv), &input_conv_len); + // For debug +#ifdef _DEBUG + for (int j = input_buf_offset; j < input_buf_len; j++) + { + log_error("Debug input: <--[%u]\n", (input_buf[j] + 256) % 256); + } +#endif + + ret = io_buf_conv(input_cd, input_buf, &input_buf_len, &input_buf_offset, input_conv, sizeof(input_conv), &input_conv_len); if (ret < 0) { - log_error("bbsnet_io_buf_conv(input, %d, %d, %d) error\n", input_buf_len, input_buf_offset, input_conv_len); + log_error("io_buf_conv(input, %d, %d, %d) error\n", input_buf_len, input_buf_offset, input_conv_len); + input_buf_len = input_buf_offset; // Discard invalid sequence + } + + // For debug +#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); } +#endif } while (input_conv_offset < input_conv_len && !SYS_server_exit) @@ -748,10 +783,11 @@ int bbsnet_connect(int n) { if (output_buf_offset < output_buf_len) { - ret = bbsnet_io_buf_conv(output_cd, output_buf, &output_buf_len, &output_buf_offset, output_conv, sizeof(output_conv), &output_conv_len); + ret = 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); + log_error("io_buf_conv(output, %d, %d, %d) error\n", output_buf_len, output_buf_offset, output_conv_len); + output_buf_len = output_buf_offset; // Discard invalid sequence } } @@ -817,18 +853,17 @@ int bbsnet_connect(int n) iconv_close(output_cd); cleanup: +#ifdef HAVE_SYS_EPOLL_H if (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); - // Restore socket flags - fcntl(sock, F_SETFL, flags_sock); - if (close(sock) == -1) { log_error("Close socket failed\n"); @@ -841,11 +876,12 @@ cleanup: tm_used->tm_mday - 1, tm_used->tm_hour, tm_used->tm_min, tm_used->tm_sec); + BBS_last_access_tm = time(NULL); + return 0; } -static int -bbsnet_refresh() +static int bbsnet_refresh() { clearscr(); moveto(1, 0); @@ -869,7 +905,7 @@ bbsnet_refresh() return 0; } -int bbsnet_selchange() +static int bbsnet_selchange() { int i = bbsnet_menu.menu_item_pos[0]; @@ -900,8 +936,6 @@ int bbs_net() load_bbsnet_conf(CONF_BBSNET); - BBS_last_access_tm = time(NULL); - clearscr(); bbsnet_refresh(); display_menu(&bbsnet_menu); @@ -911,20 +945,27 @@ int bbs_net() { ch = igetch(100); + if (ch != KEY_NULL && ch != KEY_TIMEOUT) + { + BBS_last_access_tm = time(NULL); + } + switch (ch) { case KEY_NULL: // broken pipe - case KEY_ESC: - case Ctrl('C'): // user cancel + log_error("KEY_NULL\n"); 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; } continue; + case KEY_ESC: + case Ctrl('C'): // user cancel + goto cleanup; case CR: - igetch_reset(); bbsnet_connect(bbsnet_menu.menu_item_pos[0]); bbsnet_refresh(); display_menu(&bbsnet_menu); @@ -967,7 +1008,6 @@ int bbs_net() bbsnet_selchange(); break; } - BBS_last_access_tm = time(NULL); } cleanup: