--- lbbs/src/bbs_net.c 2025/05/15 08:53:23 1.43 +++ lbbs/src/bbs_net.c 2025/06/21 02:15:18 1.55 @@ -16,26 +16,30 @@ #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 #define MENU_CONF_DELIM " \t\r\n" @@ -213,6 +217,11 @@ int bbsnet_connect(int n) 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); @@ -239,7 +248,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) @@ -249,7 +258,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); @@ -357,7 +366,7 @@ 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) { goto cleanup; } @@ -385,7 +394,7 @@ 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; @@ -403,11 +412,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,7 +437,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; } @@ -435,7 +451,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) @@ -456,7 +496,9 @@ int bbsnet_connect(int n) } else if (ret == 0) // broken pipe { - log_std("read(STDIN) EOF\n"); +#ifdef _DEBUG + log_error("read(STDIN) EOF\n"); +#endif stdin_read_wait = 0; loop = 0; break; @@ -464,7 +506,7 @@ int bbsnet_connect(int n) else { input_buf_len += ret; - BBS_last_access_tm = time(0); + BBS_last_access_tm = time(NULL); continue; } } @@ -496,7 +538,9 @@ int bbsnet_connect(int n) } else if (ret == 0) // broken pipe { - log_std("write(socket) EOF\n"); +#ifdef _DEBUG + log_error("write(socket) EOF\n"); +#endif sock_write_wait = 0; loop = 0; break; @@ -541,7 +585,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; @@ -559,7 +605,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) @@ -580,7 +639,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; @@ -619,12 +680,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_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; } @@ -685,7 +746,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,13 +756,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; } @@ -735,16 +798,22 @@ int bbs_net() menu_control(&bbsnet_menu, KEY_DOWN); 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); bbsnet_selchange(); break; } - BBS_last_access_tm = time(0); + BBS_last_access_tm = time(NULL); } cleanup: