/[LeafOK_CVS]/lbbs/src/bbs_net.c
ViewVC logotype

Diff of /lbbs/src/bbs_net.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.76 by sysadm, Tue Nov 4 13:49:51 2025 UTC Revision 1.82 by sysadm, Mon Nov 17 06:41:18 2025 UTC
# Line 3  Line 3 
3   * bbs_net   * bbs_net
4   *   - user interactive feature of site shuttle   *   - user interactive feature of site shuttle
5   *   *
6   * Copyright (C) 2004-2025 by 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"
# Line 31  Line 35 
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  {  {
# Line 45  struct _bbsnet_conf Line 57  struct _bbsnet_conf
57          char host2[40];          char host2[40];
58          char ip[40];          char ip[40];
59          in_port_t port;          in_port_t port;
60          char charset[20];          char charset[CHARSET_MAX_LEN + 1];
61  } bbsnet_conf[MAXSTATION];  } bbsnet_conf[MAXSTATION];
62    
63  static MENU_SET bbsnet_menu;  static MENU_SET bbsnet_menu;
# Line 208  static int bbsnet_connect(int n) Line 220  static int bbsnet_connect(int n)
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;
# Line 288  static int bbsnet_connect(int n) Line 307  static int bbsnet_connect(int n)
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          {          {
# Line 310  static int bbsnet_connect(int n) Line 330  static int bbsnet_connect(int n)
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          {          {
# Line 340  static int bbsnet_connect(int n) Line 361  static int bbsnet_connect(int n)
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                  }                  }
# Line 358  static int bbsnet_connect(int n) Line 393  static int bbsnet_connect(int n)
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)
# Line 371  static int bbsnet_connect(int n) Line 410  static int bbsnet_connect(int n)
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)
# Line 435  static int bbsnet_connect(int n) Line 478  static int bbsnet_connect(int n)
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)
# Line 450  static int bbsnet_connect(int n) Line 494  static int bbsnet_connect(int n)
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;
# Line 463  static int bbsnet_connect(int n) Line 508  static int bbsnet_connect(int n)
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 >= MAX_DELAY_TIME)                          if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time)
541                          {                          {
542                                  break;                                  break;
543                          }                          }
# Line 484  static int bbsnet_connect(int n) Line 545  static int bbsnet_connect(int n)
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                          }                          }
# Line 770  static int bbsnet_connect(int n) Line 852  static int bbsnet_connect(int n)
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");
# Line 874  extern int bbs_net() Line 955  extern int bbs_net()
955                          log_error("KEY_NULL\n");                          log_error("KEY_NULL\n");
956                          goto cleanup;                          goto cleanup;
957                  case KEY_TIMEOUT:                  case KEY_TIMEOUT:
958                          if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time)
959                          {                          {
960                                  log_error("User input timeout\n");                                  log_error("User input timeout\n");
961                                  goto cleanup;                                  goto cleanup;


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1