--- lbbs/src/bbs_net.c 2025/05/05 11:11:06 1.21 +++ lbbs/src/bbs_net.c 2025/05/10 14:37:04 1.30 @@ -1,16 +1,15 @@ /*************************************************************************** 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. * * * ***************************************************************************/ @@ -21,18 +20,23 @@ #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 -#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 @@ -42,7 +46,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; @@ -69,12 +73,12 @@ int load_bbsnet_conf(const char *file_co while (fgets(t, 255, fp) && item_count < MAXSTATION) { - t1 = strtok_r(t, " \t", &saveptr); - t2 = strtok_r(NULL, " \t\n", &saveptr); - t3 = strtok_r(NULL, " \t\n", &saveptr); - t4 = strtok_r(NULL, " \t\n", &saveptr); + 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 == NULL || t2 == NULL || t3 == NULL || t[0] == '#' || t[0] == '*') + if (t1 == NULL || t2 == NULL || t3 == NULL || t4 == NULL || t[0] == '#' || t[0] == '*') { continue; } @@ -85,18 +89,18 @@ int load_bbsnet_conf(const char *file_co 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 = t4 ? atoi(t4) : 23; + 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'; snprintf(p_menuitem->text, sizeof(p_menuitem->text), "%c. %s", p_menuitem->name[0], bbsnet_conf[item_count].host1); @@ -117,59 +121,59 @@ int load_bbsnet_conf(const char *file_co 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, result, len, loop; + int sock, flags, ret, loop, error; + ssize_t len; struct sockaddr_in sin; - char buf[256]; - fd_set inputs, testfds; + char buf[LINE_BUFFER_LEN]; + fd_set read_fds; + fd_set write_fds; struct timeval timeout; - struct hostent *pHost = NULL; - int rv, tos = 020, i; - char remote_addr[256]; + struct hostent *p_host = NULL; + int tos = 020, 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(); @@ -199,39 +203,72 @@ int bbsnet_connect(int n) 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); 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 = fcntl(sock, F_GETFL, 0); + fcntl(sock, F_SETFL, flags | 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) + else if (ret < 0) { - break; + if (errno != EINTR) + { + log_error("select() error (%d) !\n", errno); + return -1; + } } - else + // ret > 0 + else if (FD_ISSET(sock, &read_fds) || FD_ISSET(sock, &write_fds)) { - prints("\033[1;31m连接失败!\033[m\r\n"); - press_any_key(); - return -1; + 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) @@ -240,61 +277,65 @@ int bbsnet_connect(int n) press_any_key(); return -1; } + + fcntl(sock, F_SETFL, flags); /* restore file status flags */ setsockopt(sock, IPPROTO_IP, IP_TOS, &tos, sizeof(int)); prints("\033[1;31m连接成功!\033[m\r\n"); log_std("BBSNET connect to %s:%d\n", remote_addr, remote_port); - FD_ZERO(&inputs); - FD_SET(0, &inputs); - FD_SET(sock, &inputs); - 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); + + 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(FD_SETSIZE, &read_fds, NULL, 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 (FD_ISSET(STDIN_FILENO, &read_fds)) { - len = read(0, buf, 255); + len = read(STDIN_FILENO, buf, sizeof(buf)); if (len == 0) { loop = 0; } - write(sock, buf, len); + write(sock, buf, (size_t)len); + + BBS_last_access_tm = time(0); } - if (FD_ISSET(sock, &testfds)) + if (FD_ISSET(sock, &read_fds)) { - len = read(sock, buf, 255); + len = read(sock, buf, sizeof(buf)); if (len == 0) { loop = 0; } - write(1, buf, len); + write(STDOUT_FILENO, buf, (size_t)len); } - BBS_last_access_tm = time(0); } } @@ -318,18 +359,18 @@ bbsnet_refresh() { clearscr(); moveto(1, 0); - prints("╭═════════════════════════════════════════════════════════════════════════════╮"); + 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]退出"); @@ -342,20 +383,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; @@ -375,18 +416,18 @@ int bbs_net() display_menu(get_menu(&bbsnet_menu, "BBSNET")); bbsnet_selchange(pos); - while (1) + while (!SYS_server_exit) { ch = igetch(0); switch (ch) { - case KEY_NULL: case Ctrl('C'): return 0; + case KEY_NULL: case KEY_TIMEOUT: if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME) { - return -1; + return 0; } continue; case CR: