| 6 |
* Copyright (C) 2004-2025 Leaflet <leaflet@leafok.com> |
* Copyright (C) 2004-2025 Leaflet <leaflet@leafok.com> |
| 7 |
*/ |
*/ |
| 8 |
|
|
| 9 |
|
#ifdef HAVE_CONFIG_H |
| 10 |
|
#include "config.h" |
| 11 |
|
#endif |
| 12 |
|
|
| 13 |
#include "bbs.h" |
#include "bbs.h" |
| 14 |
#include "common.h" |
#include "common.h" |
| 15 |
#include "io.h" |
#include "io.h" |
| 35 |
#include <sys/select.h> |
#include <sys/select.h> |
| 36 |
#include <sys/ioctl.h> |
#include <sys/ioctl.h> |
| 37 |
#include <sys/socket.h> |
#include <sys/socket.h> |
| 38 |
|
|
| 39 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 40 |
#include <sys/epoll.h> |
#include <sys/epoll.h> |
| 41 |
|
#else |
| 42 |
|
#include <poll.h> |
| 43 |
|
#endif |
| 44 |
|
|
| 45 |
#define MENU_CONF_DELIM " \t\r\n" |
static const char MENU_CONF_DELIM[] = " \t\r\n"; |
| 46 |
|
|
| 47 |
#define MAX_PROCESS_BAR_LEN 30 |
enum _bbs_net_constant_t |
| 48 |
#define MAXSTATION 26 * 2 |
{ |
| 49 |
#define STATION_PER_LINE 4 |
MAX_PROCESS_BAR_LEN = 30, |
| 50 |
|
MAXSTATION = 26 * 2, |
| 51 |
|
STATION_PER_LINE = 4, |
| 52 |
|
}; |
| 53 |
|
|
| 54 |
struct _bbsnet_conf |
struct _bbsnet_conf |
| 55 |
{ |
{ |
| 220 |
iconv_t input_cd = NULL; |
iconv_t input_cd = NULL; |
| 221 |
iconv_t output_cd = NULL; |
iconv_t output_cd = NULL; |
| 222 |
char tocode[32]; |
char tocode[32]; |
| 223 |
|
|
| 224 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 225 |
struct epoll_event ev, events[MAX_EVENTS]; |
struct epoll_event ev, events[MAX_EVENTS]; |
| 226 |
int nfds, epollfd; |
int epollfd; |
| 227 |
|
#else |
| 228 |
|
struct pollfd pfds[3]; |
| 229 |
|
#endif |
| 230 |
|
|
| 231 |
|
int nfds; |
| 232 |
int stdin_read_wait = 0; |
int stdin_read_wait = 0; |
| 233 |
int stdout_write_wait = 0; |
int stdout_write_wait = 0; |
| 234 |
int sock_read_wait = 0; |
int sock_read_wait = 0; |
| 307 |
fcntl(STDIN_FILENO, F_SETFL, flags_stdin | O_NONBLOCK); |
fcntl(STDIN_FILENO, F_SETFL, flags_stdin | O_NONBLOCK); |
| 308 |
fcntl(STDOUT_FILENO, F_SETFL, flags_stdout | O_NONBLOCK); |
fcntl(STDOUT_FILENO, F_SETFL, flags_stdout | O_NONBLOCK); |
| 309 |
|
|
| 310 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 311 |
epollfd = epoll_create1(0); |
epollfd = epoll_create1(0); |
| 312 |
if (epollfd < 0) |
if (epollfd < 0) |
| 313 |
{ |
{ |
| 330 |
log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno); |
log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno); |
| 331 |
goto cleanup; |
goto cleanup; |
| 332 |
} |
} |
| 333 |
|
#endif |
| 334 |
|
|
| 335 |
while (!SYS_server_exit) |
while (!SYS_server_exit) |
| 336 |
{ |
{ |
| 361 |
|
|
| 362 |
for (int j = 0; j < MAX_PROCESS_BAR_LEN && !sock_connected && !SYS_server_exit; j++) |
for (int j = 0; j < MAX_PROCESS_BAR_LEN && !sock_connected && !SYS_server_exit; j++) |
| 363 |
{ |
{ |
| 364 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 365 |
nfds = epoll_wait(epollfd, events, MAX_EVENTS, 500); // 0.5 second |
nfds = epoll_wait(epollfd, events, MAX_EVENTS, 500); // 0.5 second |
| 366 |
|
ret = nfds; |
| 367 |
|
#else |
| 368 |
|
pfds[0].fd = sock; |
| 369 |
|
pfds[0].events = POLLOUT; |
| 370 |
|
pfds[1].fd = STDIN_FILENO; |
| 371 |
|
pfds[1].events = POLLIN; |
| 372 |
|
nfds = 2; |
| 373 |
|
ret = poll(pfds, (nfds_t)nfds, 500); // 0.5 second |
| 374 |
|
#endif |
| 375 |
|
|
| 376 |
if (nfds < 0) |
if (ret < 0) |
| 377 |
{ |
{ |
| 378 |
if (errno != EINTR) |
if (errno != EINTR) |
| 379 |
{ |
{ |
| 380 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 381 |
log_error("epoll_wait() error (%d)\n", errno); |
log_error("epoll_wait() error (%d)\n", errno); |
| 382 |
|
#else |
| 383 |
|
log_error("poll() error (%d)\n", errno); |
| 384 |
|
#endif |
| 385 |
break; |
break; |
| 386 |
} |
} |
| 387 |
} |
} |
| 388 |
else if (nfds == 0) // timeout |
else if (ret == 0) // timeout |
| 389 |
{ |
{ |
| 390 |
process_bar(j + 1, MAX_PROCESS_BAR_LEN); |
process_bar(j + 1, MAX_PROCESS_BAR_LEN); |
| 391 |
} |
} |
| 393 |
{ |
{ |
| 394 |
for (int i = 0; i < nfds; i++) |
for (int i = 0; i < nfds; i++) |
| 395 |
{ |
{ |
| 396 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 397 |
if (events[i].data.fd == sock) |
if (events[i].data.fd == sock) |
| 398 |
|
#else |
| 399 |
|
if (pfds[i].fd == sock && (pfds[i].revents & POLLOUT)) |
| 400 |
|
#endif |
| 401 |
{ |
{ |
| 402 |
len = sizeof(error); |
len = sizeof(error); |
| 403 |
if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, (socklen_t *)&len) < 0) |
if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, (socklen_t *)&len) < 0) |
| 410 |
sock_connected = 1; |
sock_connected = 1; |
| 411 |
} |
} |
| 412 |
} |
} |
| 413 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 414 |
else if (events[i].data.fd == STDIN_FILENO) |
else if (events[i].data.fd == STDIN_FILENO) |
| 415 |
|
#else |
| 416 |
|
else if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & POLLIN)) |
| 417 |
|
#endif |
| 418 |
{ |
{ |
| 419 |
ch = igetch(0); |
ch = igetch(0); |
| 420 |
if (ch == Ctrl('C') || ch == KEY_ESC) |
if (ch == Ctrl('C') || ch == KEY_ESC) |
| 478 |
goto cleanup; |
goto cleanup; |
| 479 |
} |
} |
| 480 |
|
|
| 481 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 482 |
ev.events = EPOLLIN | EPOLLOUT | EPOLLET; |
ev.events = EPOLLIN | EPOLLOUT | EPOLLET; |
| 483 |
ev.data.fd = sock; |
ev.data.fd = sock; |
| 484 |
if (epoll_ctl(epollfd, EPOLL_CTL_MOD, sock, &ev) == -1) |
if (epoll_ctl(epollfd, EPOLL_CTL_MOD, sock, &ev) == -1) |
| 494 |
log_error("epoll_ctl(STDOUT_FILENO) error (%d)\n", errno); |
log_error("epoll_ctl(STDOUT_FILENO) error (%d)\n", errno); |
| 495 |
goto cleanup; |
goto cleanup; |
| 496 |
} |
} |
| 497 |
|
#endif |
| 498 |
|
|
| 499 |
BBS_last_access_tm = t_used = time(NULL); |
BBS_last_access_tm = t_used = time(NULL); |
| 500 |
loop = 1; |
loop = 1; |
| 508 |
break; |
break; |
| 509 |
} |
} |
| 510 |
|
|
| 511 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 512 |
nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second |
nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second |
| 513 |
|
ret = nfds; |
| 514 |
|
#else |
| 515 |
|
pfds[0].fd = STDIN_FILENO; |
| 516 |
|
pfds[0].events = POLLIN; |
| 517 |
|
pfds[1].fd = sock; |
| 518 |
|
pfds[1].events = POLLIN | POLLOUT; |
| 519 |
|
pfds[2].fd = STDOUT_FILENO; |
| 520 |
|
pfds[2].events = POLLOUT; |
| 521 |
|
nfds = 3; |
| 522 |
|
ret = poll(pfds, (nfds_t)nfds, 100); // 0.1 second |
| 523 |
|
#endif |
| 524 |
|
|
| 525 |
if (nfds < 0) |
if (ret < 0) |
| 526 |
{ |
{ |
| 527 |
if (errno != EINTR) |
if (errno != EINTR) |
| 528 |
{ |
{ |
| 529 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 530 |
log_error("epoll_wait() error (%d)\n", errno); |
log_error("epoll_wait() error (%d)\n", errno); |
| 531 |
|
#else |
| 532 |
|
log_error("poll() error (%d)\n", errno); |
| 533 |
|
#endif |
| 534 |
break; |
break; |
| 535 |
} |
} |
| 536 |
continue; |
continue; |
| 537 |
} |
} |
| 538 |
else if (nfds == 0) // timeout |
else if (ret == 0) // timeout |
| 539 |
{ |
{ |
| 540 |
if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time) |
if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time) |
| 541 |
{ |
{ |
| 545 |
|
|
| 546 |
for (int i = 0; i < nfds; i++) |
for (int i = 0; i < nfds; i++) |
| 547 |
{ |
{ |
| 548 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 549 |
if (events[i].data.fd == STDIN_FILENO) |
if (events[i].data.fd == STDIN_FILENO) |
| 550 |
|
#else |
| 551 |
|
if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & POLLIN)) |
| 552 |
|
#endif |
| 553 |
{ |
{ |
| 554 |
stdin_read_wait = 1; |
stdin_read_wait = 1; |
| 555 |
} |
} |
| 556 |
|
|
| 557 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 558 |
if (events[i].data.fd == sock) |
if (events[i].data.fd == sock) |
| 559 |
|
#else |
| 560 |
|
if (pfds[i].fd == sock) |
| 561 |
|
#endif |
| 562 |
{ |
{ |
| 563 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 564 |
if (events[i].events & EPOLLIN) |
if (events[i].events & EPOLLIN) |
| 565 |
|
#else |
| 566 |
|
if (pfds[i].revents & POLLIN) |
| 567 |
|
#endif |
| 568 |
{ |
{ |
| 569 |
sock_read_wait = 1; |
sock_read_wait = 1; |
| 570 |
} |
} |
| 571 |
|
|
| 572 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 573 |
if (events[i].events & EPOLLOUT) |
if (events[i].events & EPOLLOUT) |
| 574 |
|
#else |
| 575 |
|
if (pfds[i].revents & POLLOUT) |
| 576 |
|
#endif |
| 577 |
{ |
{ |
| 578 |
sock_write_wait = 1; |
sock_write_wait = 1; |
| 579 |
} |
} |
| 580 |
} |
} |
| 581 |
|
|
| 582 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 583 |
if (events[i].data.fd == STDOUT_FILENO) |
if (events[i].data.fd == STDOUT_FILENO) |
| 584 |
|
#else |
| 585 |
|
if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & POLLOUT)) |
| 586 |
|
#endif |
| 587 |
{ |
{ |
| 588 |
stdout_write_wait = 1; |
stdout_write_wait = 1; |
| 589 |
} |
} |
| 852 |
iconv_close(output_cd); |
iconv_close(output_cd); |
| 853 |
|
|
| 854 |
cleanup: |
cleanup: |
| 855 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 856 |
if (close(epollfd) < 0) |
if (close(epollfd) < 0) |
| 857 |
{ |
{ |
| 858 |
log_error("close(epoll) error (%d)\n"); |
log_error("close(epoll) error (%d)\n"); |
| 859 |
} |
} |
| 860 |
|
#endif |
| 861 |
|
|
| 862 |
// Restore STDIN/STDOUT flags |
// Restore STDIN/STDOUT flags |
| 863 |
fcntl(STDIN_FILENO, F_SETFL, flags_stdin); |
fcntl(STDIN_FILENO, F_SETFL, flags_stdin); |
| 864 |
fcntl(STDOUT_FILENO, F_SETFL, flags_stdout); |
fcntl(STDOUT_FILENO, F_SETFL, flags_stdout); |
| 865 |
|
|
|
// Restore socket flags |
|
|
fcntl(sock, F_SETFL, flags_sock); |
|
|
|
|
| 866 |
if (close(sock) == -1) |
if (close(sock) == -1) |
| 867 |
{ |
{ |
| 868 |
log_error("Close socket failed\n"); |
log_error("Close socket failed\n"); |