--- lbbs/src/bbs_net.c 2025/04/28 03:30:59 1.14 +++ lbbs/src/bbs_net.c 2025/05/11 12:47:32 1.36 @@ -1,36 +1,43 @@ /*************************************************************************** bbs_net.c - description ------------------- - begin : Mon Oct 18 2004 - copyright : (C) 2004 by Leaflet - email : leaflet@leafok.com + 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 2 of the License, or * + * the Free Software Foundation; either version 3 of the License, or * * (at your option) any later version. * * * ***************************************************************************/ #include "bbs.h" #include "common.h" +#include "log.h" #include "io.h" +#include "screen.h" #include "menu.h" -#include "tcplib.h" #include #include +#include +#include +#include +#include +#include +#include +#include +#include #include #include #include +#include #include -#include -#include -#include -#define TIME_OUT 15 +#define MENU_CONF_DELIM " \t\r\n" + #define MAX_PROCESS_BAR_LEN 30 #define MAXSTATION 26 * 2 #define STATION_PER_LINE 4 @@ -40,7 +47,7 @@ struct _bbsnet_conf char host1[20]; char host2[40]; char ip[40]; - int port; + in_port_t port; } bbsnet_conf[MAXSTATION]; MENU_SET bbsnet_menu; @@ -50,48 +57,54 @@ int load_bbsnet_conf(const char *file_co FILE *fp; MENU *p_menu; MENU_ITEM *p_menuitem; - char t[256], *t1, *t2, *t3, *t4; + char t[256], *t1, *t2, *t3, *t4, *saveptr; int item_count = 0; fp = fopen(file_config, "r"); if (fp == NULL) + { return -1; + } p_menu = bbsnet_menu.p_menu[0] = malloc(sizeof(MENU)); - strcpy(p_menu->name, "BBSNET"); + strncpy(p_menu->name, "BBSNET", sizeof(p_menu->name) - 1); + p_menu->name[sizeof(p_menu->name) - 1] = '\0'; p_menu->title.show = 0; p_menu->screen.show = 0; while (fgets(t, 255, fp) && item_count < MAXSTATION) { - t1 = strtok(t, " \t"); - t2 = strtok(NULL, " \t\n"); - t3 = strtok(NULL, " \t\n"); - t4 = strtok(NULL, " \t\n"); + t1 = strtok_r(t, 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); - if (t1[0] == '#' || t1[0] == '*' || t1 == NULL || t2 == NULL || t3 == NULL) + if (t1 == NULL || t2 == NULL || t3 == NULL || t4 == NULL || t[0] == '#' || t[0] == '*') + { continue; - strncpy(bbsnet_conf[item_count].host1, t2, 18); - bbsnet_conf[item_count].host1[18] = 0; - strncpy(bbsnet_conf[item_count].host2, t1, 36); - bbsnet_conf[item_count].host2[36] = 0; - strncpy(bbsnet_conf[item_count].ip, t3, 36); - bbsnet_conf[item_count].ip[36] = 0; - bbsnet_conf[item_count].port = t4 ? atoi(t4) : 23; + } + + strncpy(bbsnet_conf[item_count].host1, t2, sizeof(bbsnet_conf[item_count].host1) - 1); + bbsnet_conf[item_count].host1[sizeof(bbsnet_conf[item_count].host1) - 1] = '\0'; + strncpy(bbsnet_conf[item_count].host2, t1, sizeof(bbsnet_conf[item_count].host2) - 1); + bbsnet_conf[item_count].host2[sizeof(bbsnet_conf[item_count].host2) - 1] = '\0'; + strncpy(bbsnet_conf[item_count].ip, t3, sizeof(bbsnet_conf[item_count].ip) - 1); + bbsnet_conf[item_count].ip[sizeof(bbsnet_conf[item_count].ip) - 1] = '\0'; + bbsnet_conf[item_count].port = (in_port_t)(t4 ? atoi(t4) : 23); p_menuitem = p_menu->items[item_count] = malloc(sizeof(MENU_ITEM)); p_menuitem->row = 2 + item_count / STATION_PER_LINE; p_menuitem->col = 5 + item_count % STATION_PER_LINE * 20; - sprintf(p_menuitem->action, "%d", item_count); + snprintf(p_menuitem->action, sizeof(p_menuitem->action), "%d", item_count); p_menuitem->submenu = 0; p_menuitem->priv = 0; p_menuitem->level = 0; p_menuitem->display = 0; p_menuitem->name[0] = - (item_count < MAXSTATION / 2 ? 'A' + item_count : 'a' + item_count); + (char)(item_count < MAXSTATION / 2 ? 'A' + item_count : 'a' + item_count); p_menuitem->name[1] = '\0'; - sprintf(p_menuitem->text, "%c. %s", - p_menuitem->name[0], bbsnet_conf[item_count].host1); + snprintf(p_menuitem->text, sizeof(p_menuitem->text), "%c. %s", + p_menuitem->name[0], bbsnet_conf[item_count].host1); item_count++; } @@ -107,62 +120,70 @@ int load_bbsnet_conf(const char *file_co return 0; } -static void -process_bar(int n, int len) +static void process_bar(int n, int len) { - char buf[256]; - char buf2[256]; - char *ptr; - char *ptr2; - char *ptr3; + char buf[LINE_BUFFER_LEN]; + char buf2[LINE_BUFFER_LEN]; + + if (len > LINE_BUFFER_LEN) + { + len = LINE_BUFFER_LEN - 1; + } + if (n < 0) + { + n = 0; + } + else if (n > len) + { + n = len; + } moveto(4, 0); - prints("┌───────────────┐\r\n"); - sprintf(buf2, " %3d%% ", n * 100 / len); - ptr = buf; - ptr2 = buf2; - ptr3 = buf + n; - while (ptr != ptr3) - *ptr++ = *ptr2++; - *ptr++ = '\x1b'; - *ptr++ = '['; - *ptr++ = '4'; - *ptr++ = '4'; - *ptr++ = 'm'; - while (*ptr2 != '\0') - *ptr++ = *ptr2++; - *ptr++ = '\0'; - prints("│\033[46m%s\033[m│\r\n", buf); - prints("└───────────────┘\r\n"); + prints(" ------------------------------ \r\n"); + snprintf(buf, sizeof(buf), " %3d%% ", n * 100 / len); + strncpy(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_connect(int n) { - int sock, ch, result, len, loop; + int sock, ret, loop, error; + int flags_sock; + int flags_stdin; + int flags_stdout; + int len; struct sockaddr_in sin; - char buf[256]; - fd_set inputs, testfds; + char input_buf[LINE_BUFFER_LEN]; + char output_buf[LINE_BUFFER_LEN]; + int input_buf_len = 0; + int output_buf_len = 0; + int input_buf_offset = 0; + int output_buf_offset = 0; + fd_set read_fds; + fd_set write_fds; struct timeval timeout; - struct hostent *pHost = NULL; - int rc, rv, tos = 020, i; - char remote_addr[256]; + struct hostent *p_host = NULL; + int tos; + int i; + char remote_addr[IP_ADDR_LEN]; int remote_port; time_t t_used; struct tm *tm_used; + int ch; clearscr(); moveto(0, 0); prints("\033[1;32m正在测试往 %s (%s) 的连接,请稍候... \033[m\r\n", bbsnet_conf[n].host1, bbsnet_conf[n].ip); - prints("\033[1;32m如果在 %d 秒内无法连上,穿梭程序将放弃连接。\033[m\r\n", - TIME_OUT); iflush(); - pHost = gethostbyname(bbsnet_conf[n].ip); + p_host = gethostbyname(bbsnet_conf[n].ip); - if (pHost == NULL) + if (p_host == NULL) { prints("\033[1;31m查找主机名失败!\033[m\r\n"); press_any_key(); @@ -179,49 +200,84 @@ int bbsnet_connect(int n) } sin.sin_family = AF_INET; - sin.sin_addr.s_addr = - (strlen(hostaddr_server) > 0 ? inet_addr(hostaddr_server) : INADDR_ANY); + sin.sin_addr.s_addr = (hostaddr_server[0] != '\0' ? inet_addr(hostaddr_server) : INADDR_ANY); sin.sin_port = 0; if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) { - log_error("Bind address %s:%u failed\n", - inet_ntoa(sin.sin_addr), ntohs(sin.sin_port)); + log_error("Bind address %s:%u failed (%d)\n", + inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), errno); return -2; } bzero(&sin, sizeof(sin)); sin.sin_family = AF_INET; - sin.sin_addr = *(struct in_addr *)pHost->h_addr_list[0]; + sin.sin_addr = *(struct in_addr *)p_host->h_addr_list[0]; sin.sin_port = htons(bbsnet_conf[n].port); - strcpy(remote_addr, inet_ntoa(sin.sin_addr)); + 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); - prints("\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 + flags_sock = fcntl(sock, F_GETFL, 0); + fcntl(sock, F_SETFL, flags_sock | O_NONBLOCK); + + if ((ret = connect(sock, (struct sockaddr *)&sin, sizeof(sin))) < 0) + { + if (errno != EINPROGRESS) + { + prints("\033[1;31m连接失败!\033[m\r\n"); + press_any_key(); + return -1; + } + } + for (i = 0; i < MAX_PROCESS_BAR_LEN; i++) { - if (i == 0) - rv = - NonBlockConnectEx(sock, (struct sockaddr *)&sin, sizeof(sin), - 500, 1); - else - rv = - NonBlockConnectEx(sock, (struct sockaddr *)&sin, sizeof(sin), - 500, 0); - if (rv == ERR_TCPLIB_TIMEOUT) + ch = igetch(0); // 0.1 second + if (ch == Ctrl('C') || SYS_server_exit) + { + return 0; + } + + FD_ZERO(&read_fds); + FD_SET(sock, &read_fds); + + FD_ZERO(&write_fds); + FD_SET(sock, &write_fds); + + timeout.tv_sec = 0; + timeout.tv_usec = 400 * 1000; // 0.4 second + + ret = select(sock + 1, &read_fds, &write_fds, NULL, &timeout); + + if (ret == 0) // Timeout { process_bar(i + 1, MAX_PROCESS_BAR_LEN); - continue; } - else if (rv == 0) - break; - else + else if (ret < 0) { - prints("\033[1;31m连接失败!\033[m\r\n"); - press_any_key(); - return -1; + if (errno != EINTR) + { + log_error("select() error (%d) !\n", errno); + return -1; + } + } + // ret > 0 + else if (FD_ISSET(sock, &read_fds) || FD_ISSET(sock, &write_fds)) + { + len = sizeof(error); + if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, (socklen_t *)&len) < 0) + { + log_error("getsockopt() error (%d) !\n", error); + return -1; + } + + break; // connected } } if (i == MAX_PROCESS_BAR_LEN) @@ -230,64 +286,155 @@ int bbsnet_connect(int n) press_any_key(); return -1; } - setsockopt(sock, IPPROTO_IP, IP_TOS, &tos, sizeof(int)); + + tos = IPTOS_LOWDELAY; + if (setsockopt(sock, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0) + { + log_error("setsockopt IP_TOS=%d error (%d)\n", tos, errno); + } prints("\033[1;31m连接成功!\033[m\r\n"); + iflush(); log_std("BBSNET connect to %s:%d\n", remote_addr, remote_port); - FD_ZERO(&inputs); - FD_SET(0, &inputs); - FD_SET(sock, &inputs); + // 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); BBS_last_access_tm = t_used = time(0); - loop = 1; - while (loop) + while (loop && !SYS_server_exit) { - testfds = inputs; - timeout.tv_sec = TIME_OUT; - timeout.tv_usec = 0; + FD_ZERO(&read_fds); + FD_SET(STDIN_FILENO, &read_fds); + FD_SET(sock, &read_fds); + + FD_ZERO(&write_fds); + FD_SET(STDOUT_FILENO, &write_fds); + FD_SET(sock, &write_fds); + + timeout.tv_sec = 0; + timeout.tv_usec = 100 * 1000; // 0.1 second - result = SignalSafeSelect(FD_SETSIZE, &testfds, (fd_set *)NULL, - (fd_set *)NULL, &timeout); + ret = select(sock + 1, &read_fds, &write_fds, NULL, &timeout); - if (result == 0) + if (ret == 0) // timeout { if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME) { loop = 0; } } - if (result < 0) + else if (ret < 0) { - log_error("select() error (%d) !\n", result); - loop = 0; + if (errno != EINTR) + { + log_error("select() error (%d) !\n", errno); + loop = 0; + } } - if (result > 0) + else if (ret > 0) { - if (FD_ISSET(0, &testfds)) + if ((input_buf_offset >= input_buf_len) && FD_ISSET(STDIN_FILENO, &read_fds)) + { + ret = (int)read(STDIN_FILENO, input_buf, sizeof(input_buf)); + if (ret < 0) + { + if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) + { + log_error("read(STDIN) error (%d)\n", errno); + loop = 0; + } + } + else if (ret == 0) // broken pipe + { + loop = 0; + } + else + { + input_buf_len = ret; + input_buf_offset = 0; + + BBS_last_access_tm = time(0); + } + } + + if ((input_buf_offset < input_buf_len) && FD_ISSET(sock, &write_fds)) + { + 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 && errno != EINTR) + { + log_error("write(socket) error (%d)\n", errno); + loop = 0; + } + } + else if (ret == 0) // broken pipe + { + loop = 0; + } + else + { + input_buf_offset += ret; + } + } + + if ((output_buf_offset >= output_buf_len) && FD_ISSET(sock, &read_fds)) { - len = read(0, buf, 255); - if (len == 0) + ret = (int)read(sock, output_buf, sizeof(output_buf)); + if (ret < 0) + { + if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) + { + log_error("read(socket) error (%d)\n", errno); + loop = 0; + } + } + else if (ret == 0) // broken pipe { loop = 0; } - write(sock, buf, len); + else + { + output_buf_len = ret; + output_buf_offset = 0; + } } - if (FD_ISSET(sock, &testfds)) + + if ((output_buf_offset < output_buf_len) && FD_ISSET(STDOUT_FILENO, &write_fds)) { - len = read(sock, buf, 255); - if (len == 0) + 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 && errno != EINTR) + { + log_error("write(STDOUT) error (%d)\n", errno); + loop = 0; + } + } + else if (ret == 0) // broken pipe { loop = 0; } - write(1, buf, len); + else + { + output_buf_offset += ret; + } } - BBS_last_access_tm = time(0); } } + // 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"); @@ -306,22 +453,20 @@ int bbsnet_connect(int n) static int bbsnet_refresh() { - int i; - clearscr(); moveto(1, 0); - prints("╭══════════════════════════════════════╮"); - for (i = 2; i < 19; i++) + prints(" ----------------------------------------------------------------------------- "); + for (int i = 2; i < 19; i++) { moveto(i, 0); - prints("║"); + prints("|"); moveto(i, 79); - prints("║"); + prints("|"); } moveto(19, 0); - prints("║——————————————————————————————————————║"); + prints("|-----------------------------------------------------------------------------|"); moveto(22, 0); - prints("╰══════════════════════════════════════╯"); + prints(" ----------------------------------------------------------------------------- "); moveto(23, 0); prints(" [\x1b[1;32mCtrl+C\x1b[m]退出"); @@ -334,18 +479,20 @@ int bbsnet_selchange(int new_pos) { 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[new_pos].host2, bbsnet_conf[new_pos].host1); moveto(20, 79); - prints("║"); + prints("|"); moveto(21, 0); clrtoeol(); - prints("║\x1b[1m连往:\x1b[1;33m%-20s", bbsnet_conf[new_pos].ip); + prints("|\x1b[1m连往:\x1b[1;33m%-20s", bbsnet_conf[new_pos].ip); if (bbsnet_conf[new_pos].port != 23) + { prints(" %d", bbsnet_conf[new_pos].port); + } prints("\x1b[m"); moveto(21, 79); - prints("║"); + prints("|"); iflush(); return 0; @@ -354,12 +501,8 @@ int bbsnet_selchange(int new_pos) int bbs_net() { int ch, pos, i; - char file_config[256]; - strcpy(file_config, app_home_dir); - strcat(file_config, "conf/bbsnet.conf"); - - load_bbsnet_conf(file_config); + load_bbsnet_conf(CONF_BBSNET); BBS_last_access_tm = time(0); @@ -369,18 +512,18 @@ int bbs_net() display_menu(get_menu(&bbsnet_menu, "BBSNET")); bbsnet_selchange(pos); - while (1) + while (!SYS_server_exit) { - ch = igetch(); + ch = igetch(0); switch (ch) { - case KEY_NULL: - case Ctrl('C'): + case KEY_NULL: // broken pipe + case Ctrl('C'): // user cancel return 0; case KEY_TIMEOUT: if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME) { - return -1; + return 0; } continue; case CR: @@ -392,13 +535,17 @@ int bbs_net() break; case KEY_UP: for (i = 0; i < STATION_PER_LINE; i++) + { menu_control(&bbsnet_menu, KEY_UP); + } pos = bbsnet_menu.p_menu[0]->item_cur_pos; bbsnet_selchange(pos); break; case KEY_DOWN: for (i = 0; i < STATION_PER_LINE; i++) + { menu_control(&bbsnet_menu, KEY_DOWN); + } pos = bbsnet_menu.p_menu[0]->item_cur_pos; bbsnet_selchange(pos); break;