--- lbbs/src/bbs_net.c 2025/05/28 10:26:21 1.45 +++ 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" @@ -239,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) @@ -249,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); @@ -385,7 +389,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 +407,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 +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; } @@ -435,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) @@ -456,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; @@ -464,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; } } @@ -496,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; @@ -541,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; @@ -559,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) @@ -580,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; @@ -619,12 +671,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 +737,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,14 +747,20 @@ int bbs_net() while (!SYS_server_exit) { 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 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; } @@ -751,7 +809,7 @@ int bbs_net() bbsnet_selchange(); break; } - BBS_last_access_tm = time(0); + BBS_last_access_tm = time(NULL); } cleanup: