--- lbbs/src/bbs_net.c 2025/05/13 02:21:39 1.38 +++ lbbs/src/bbs_net.c 2025/06/18 04:38:20 1.52 @@ -20,6 +20,7 @@ #include "io.h" #include "screen.h" #include "menu.h" +#include "login.h" #include #include #include @@ -36,6 +37,9 @@ #include #include #include +#include +#include +#include #define MENU_CONF_DELIM " \t\r\n" @@ -57,9 +61,9 @@ int load_bbsnet_conf(const char *file_co { FILE *fp; MENU *p_menu; - MENU_ITEM *p_menuitem; + MENU_ITEM *p_menu_item; + MENU_ITEM_ID menu_item_id; char t[256], *t1, *t2, *t3, *t4, *saveptr; - int item_count = 0; fp = fopen(file_config, "r"); if (fp == NULL) @@ -67,13 +71,31 @@ int load_bbsnet_conf(const char *file_co return -1; } - p_menu = bbsnet_menu.p_menu[0] = malloc(sizeof(MENU)); + bbsnet_menu.p_menu_pool = calloc(1, sizeof(MENU)); + if (bbsnet_menu.p_menu_pool == NULL) + { + log_error("calloc(p_menu_pool) error\n"); + return -3; + } + bbsnet_menu.menu_count = 1; + + bbsnet_menu.p_menu_item_pool = calloc(MAXSTATION, sizeof(MENU_ITEM)); + if (bbsnet_menu.p_menu_item_pool == NULL) + { + log_error("calloc(p_menu_item_pool) error\n"); + return -3; + } + bbsnet_menu.menu_item_count = MAXSTATION; + + p_menu = (MENU *)get_menu_by_id(&bbsnet_menu, 0); + 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; + p_menu->screen_show = 0; - while (fgets(t, 255, fp) && item_count < MAXSTATION) + menu_item_id = 0; + while (fgets(t, 255, fp) && menu_item_id < MAXSTATION) { t1 = strtok_r(t, MENU_CONF_DELIM, &saveptr); t2 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr); @@ -85,43 +107,60 @@ int load_bbsnet_conf(const char *file_co continue; } - 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; - 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] = - (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); + strncpy(bbsnet_conf[menu_item_id].host1, t2, sizeof(bbsnet_conf[menu_item_id].host1) - 1); + bbsnet_conf[menu_item_id].host1[sizeof(bbsnet_conf[menu_item_id].host1) - 1] = '\0'; + strncpy(bbsnet_conf[menu_item_id].host2, t1, sizeof(bbsnet_conf[menu_item_id].host2) - 1); + bbsnet_conf[menu_item_id].host2[sizeof(bbsnet_conf[menu_item_id].host2) - 1] = '\0'; + 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); - item_count++; - } - fclose(fp); + p_menu_item = get_menu_item_by_id(&bbsnet_menu, menu_item_id); + if (p_menu_item == NULL) + { + log_error("get_menu_item_by_id(%d) return NULL pointer\n", menu_item_id); + return -1; + } - p_menu->item_count = item_count; - p_menu->item_cur_pos = 0; + p_menu_item->row = (int16_t)(2 + menu_item_id / STATION_PER_LINE); + p_menu_item->col = (int16_t)(5 + menu_item_id % STATION_PER_LINE * 20); + snprintf(p_menu_item->action, sizeof(p_menu_item->action), "%d", (int16_t)menu_item_id); + p_menu_item->submenu = 0; + p_menu_item->priv = 0; + p_menu_item->level = 0; + p_menu_item->name[0] = + (char)(menu_item_id < MAXSTATION / 2 ? 'A' + menu_item_id : 'a' + menu_item_id); + p_menu_item->name[1] = '\0'; + snprintf(p_menu_item->text, sizeof(p_menu_item->text), "%c. %s", + p_menu_item->name[0], bbsnet_conf[menu_item_id].host1); + + p_menu->items[p_menu->item_count] = menu_item_id; + p_menu->item_count++; + menu_item_id++; + } + + bbsnet_menu.menu_item_count = (int16_t)menu_item_id; + bbsnet_menu.menu_id_path[0] = 0; + bbsnet_menu.menu_item_pos[0] = 0; + bbsnet_menu.choose_step = 0; - bbsnet_menu.menu_count = 1; - bbsnet_menu.menu_select_depth = 0; - bbsnet_menu.p_menu_select[0] = bbsnet_menu.p_menu[0]; + fclose(fp); return 0; } -static void process_bar(int n, int len) +void unload_bbsnet_conf(void) +{ + bbsnet_menu.menu_count = 0; + bbsnet_menu.menu_item_count = 0; + + free(bbsnet_menu.p_menu_pool); + bbsnet_menu.p_menu_pool = NULL; + free(bbsnet_menu.p_menu_item_pool); + bbsnet_menu.p_menu_item_pool = NULL; +} + +void process_bar(int n, int len) { char buf[LINE_BUFFER_LEN]; char buf2[LINE_BUFFER_LEN]; @@ -204,7 +243,7 @@ int bbsnet_connect(int n) } 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) @@ -214,7 +253,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); @@ -226,6 +265,16 @@ int bbsnet_connect(int 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); + + // 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); + epollfd = epoll_create1(0); if (epollfd < 0) { @@ -238,7 +287,7 @@ int bbsnet_connect(int n) if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sock, &ev) == -1) { log_error("epoll_ctl(socket) error (%d)\n", errno); - return -1; + goto cleanup; } ev.events = EPOLLIN; @@ -246,20 +295,15 @@ int bbsnet_connect(int n) if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1) { log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno); - return -1; + goto cleanup; } - // Set socket as non-blocking - flags_sock = fcntl(sock, F_GETFL, 0); - fcntl(sock, F_SETFL, flags_sock | O_NONBLOCK); - while (!SYS_server_exit) { if ((ret = connect(sock, (struct sockaddr *)&sin, sizeof(sin))) < 0) { if (errno == EAGAIN || errno == EALREADY || errno == EINPROGRESS) { - log_std("Debug: %d\n", errno); // Use select / epoll to check writability of the socket, // then use getsockopt to check the status of the socket. // See man connect(2) @@ -272,9 +316,11 @@ int bbsnet_connect(int n) else { log_error("connect(socket) error (%d)\n", errno); + prints("\033[1;31m连接失败!\033[m\r\n"); press_any_key(); - return -1; + + goto cleanup; } } } @@ -288,7 +334,7 @@ int bbsnet_connect(int n) if (errno != EINTR) { log_error("epoll_wait() error (%d)\n", errno); - return -1; + break; } } else if (nfds == 0) // timeout @@ -305,7 +351,7 @@ int bbsnet_connect(int n) if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, (socklen_t *)&len) < 0) { log_error("getsockopt() error (%d) !\n", error); - return -1; + goto cleanup; } if (error == 0) { @@ -315,9 +361,9 @@ int bbsnet_connect(int n) else if (events[i].data.fd == STDIN_FILENO) { ch = igetch(0); - if (ch == Ctrl('C')) + if (ch == Ctrl('C') || ch == KEY_ESC) { - return 0; + goto cleanup; } } } @@ -325,13 +371,14 @@ int bbsnet_connect(int n) } if (SYS_server_exit) { - return 0; + goto cleanup; } if (!sock_connected) { - prints("\033[1;31m连接超时!\033[m\r\n"); + prints("\033[1;31m连接失败!\033[m\r\n"); press_any_key(); - return -1; + + goto cleanup; } tos = IPTOS_LOWDELAY; @@ -342,14 +389,14 @@ int bbsnet_connect(int n) prints("\033[1;31m连接成功!\033[m\r\n"); iflush(); - log_std("BBSNET connect to %s:%d\n", remote_addr, remote_port); + log_common("BBSNET connect to %s:%d\n", remote_addr, remote_port); ev.events = EPOLLIN | EPOLLOUT | EPOLLET; ev.data.fd = sock; if (epoll_ctl(epollfd, EPOLL_CTL_MOD, sock, &ev) == -1) { log_error("epoll_ctl(socket) error (%d)\n", errno); - return -1; + goto cleanup; } ev.events = EPOLLOUT; @@ -357,20 +404,21 @@ int bbsnet_connect(int n) if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1) { log_error("epoll_ctl(STDOUT_FILENO) error (%d)\n", errno); - return -1; + goto cleanup; } - // 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); + 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) @@ -384,7 +432,7 @@ 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; } @@ -398,7 +446,31 @@ int bbsnet_connect(int n) stdin_read_wait = 1; while (input_buf_len < sizeof(input_buf) && !SYS_server_exit) { - ret = (int)read(STDIN_FILENO, input_buf + input_buf_len, sizeof(input_buf) - (size_t)input_buf_len); + 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) + { + 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 + } + } + 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) @@ -419,7 +491,7 @@ int bbsnet_connect(int n) } else if (ret == 0) // broken pipe { - log_std("read(STDIN) EOF\n"); + log_common("read(STDIN) EOF\n"); stdin_read_wait = 0; loop = 0; break; @@ -427,7 +499,7 @@ int bbsnet_connect(int n) else { input_buf_len += ret; - BBS_last_access_tm = time(0); + BBS_last_access_tm = time(NULL); continue; } } @@ -459,7 +531,7 @@ int bbsnet_connect(int n) } else if (ret == 0) // broken pipe { - log_std("write(socket) EOF\n"); + log_common("write(socket) EOF\n"); sock_write_wait = 0; loop = 0; break; @@ -504,7 +576,9 @@ int bbsnet_connect(int n) } else if (ret == 0) // broken pipe { - log_std("read(socket) EOF\n"); +#ifdef _DEBUG + log_error("read(socket) EOF\n"); +#endif sock_read_wait = 0; loop = 0; break; @@ -522,7 +596,20 @@ int bbsnet_connect(int n) stdout_write_wait = 1; while (output_buf_offset < output_buf_len && !SYS_server_exit) { - ret = (int)write(STDOUT_FILENO, output_buf + output_buf_offset, (size_t)(output_buf_len - output_buf_offset)); + 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) + { + log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session)); + loop = 0; + break; + } + } + 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) @@ -543,7 +630,9 @@ int bbsnet_connect(int n) } else if (ret == 0) // broken pipe { - log_std("write(STDOUT) EOF\n"); +#ifdef _DEBUG + log_error("write(STDOUT) EOF\n"); +#endif stdout_write_wait = 0; loop = 0; break; @@ -564,6 +653,12 @@ int bbsnet_connect(int n) } } +cleanup: + if (close(epollfd) < 0) + { + log_error("close(epoll) error (%d)\n"); + } + // Restore STDIN/STDOUT flags fcntl(STDIN_FILENO, F_SETFL, flags_stdin); fcntl(STDOUT_FILENO, F_SETFL, flags_stdout); @@ -576,12 +671,12 @@ int bbsnet_connect(int n) log_error("Close socket failed\n"); } - t_used = time(0) - t_used; + t_used = time(NULL) - t_used; tm_used = gmtime(&t_used); - log_std("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); + 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); return 0; } @@ -611,20 +706,22 @@ bbsnet_refresh() return 0; } -int bbsnet_selchange(int new_pos) +int bbsnet_selchange() { + int i = bbsnet_menu.menu_item_pos[0]; + moveto(20, 0); clrtoeol(); 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); + 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[new_pos].ip); - if (bbsnet_conf[new_pos].port != 23) + prints("|\x1b[1m连往:\x1b[1;33m%-20s", bbsnet_conf[i].ip); + if (bbsnet_conf[i].port != 23) { - prints(" %d", bbsnet_conf[new_pos].port); + prints(" %d", bbsnet_conf[i].port); } prints("\x1b[m"); moveto(21, 79); @@ -636,73 +733,87 @@ int bbsnet_selchange(int new_pos) int bbs_net() { - int ch, pos, i; + int ch, i; load_bbsnet_conf(CONF_BBSNET); - BBS_last_access_tm = time(0); + BBS_last_access_tm = time(NULL); clearscr(); bbsnet_refresh(); - pos = bbsnet_menu.p_menu[0]->item_cur_pos; - display_menu(get_menu(&bbsnet_menu, "BBSNET")); - bbsnet_selchange(pos); + display_menu(&bbsnet_menu); + bbsnet_selchange(); while (!SYS_server_exit) { - ch = igetch(0); + ch = igetch(100); + + if (user_online_update("BBS_NET") < 0) + { + log_error("user_online_update(BBS_NET) error\n"); + } + switch (ch) { - case KEY_NULL: // broken pipe + case KEY_NULL: // broken pipe + case KEY_ESC: case Ctrl('C'): // user cancel - return 0; + 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) { - return 0; + goto cleanup; } continue; case CR: - pos = bbsnet_menu.p_menu[0]->item_cur_pos; - bbsnet_connect(pos); + igetch_reset(); + bbsnet_connect(bbsnet_menu.menu_item_pos[0]); bbsnet_refresh(); - display_current_menu(&bbsnet_menu); - bbsnet_selchange(pos); + display_menu(&bbsnet_menu); + bbsnet_selchange(); 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); + bbsnet_selchange(); 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); + bbsnet_selchange(); break; case KEY_LEFT: menu_control(&bbsnet_menu, KEY_UP); - pos = bbsnet_menu.p_menu[0]->item_cur_pos; - bbsnet_selchange(pos); + bbsnet_selchange(); break; case KEY_RIGHT: menu_control(&bbsnet_menu, KEY_DOWN); - pos = bbsnet_menu.p_menu[0]->item_cur_pos; - bbsnet_selchange(pos); + bbsnet_selchange(); + break; + case KEY_HOME: + case KEY_PGUP: + menu_control(&bbsnet_menu, KEY_PGUP); + bbsnet_selchange(); + break; + case KEY_END: + case KEY_PGDN: + menu_control(&bbsnet_menu, KEY_PGDN); + bbsnet_selchange(); break; default: menu_control(&bbsnet_menu, ch); - pos = bbsnet_menu.p_menu[0]->item_cur_pos; - bbsnet_selchange(pos); + bbsnet_selchange(); break; } - BBS_last_access_tm = time(0); + BBS_last_access_tm = time(NULL); } +cleanup: + unload_bbsnet_conf(); + return 0; }