/[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.75 by sysadm, Sat Nov 1 15:34:45 2025 UTC Revision 1.83 by sysadm, Fri Nov 21 07:36:41 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /* SPDX-License-Identifier: GPL-3.0-or-later */
2                                                    bbs_net.c  -  description  /*
3                                                           -------------------   * bbs_net
4          Copyright            : (C) 2004-2025 by Leaflet   *   - user interactive feature of site shuttle
5          Email                : leaflet@leafok.com   *
6   ***************************************************************************/   * Copyright (C) 2004-2025  Leaflet <leaflet@leafok.com>
7     */
8  /***************************************************************************  
9   *                                                                         *  #ifdef HAVE_CONFIG_H
10   *   This program is free software; you can redistribute it and/or modify  *  #include "config.h"
11   *   it under the terms of the GNU General Public License as published by  *  #endif
  *   the Free Software Foundation; either version 3 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
12    
13  #include "bbs.h"  #include "bbs.h"
14    #include "bbs_net.h"
15  #include "common.h"  #include "common.h"
16  #include "io.h"  #include "io.h"
17  #include "log.h"  #include "log.h"
# Line 39  Line 36 
36  #include <sys/select.h>  #include <sys/select.h>
37  #include <sys/ioctl.h>  #include <sys/ioctl.h>
38  #include <sys/socket.h>  #include <sys/socket.h>
39    
40    #ifdef HAVE_SYS_EPOLL_H
41  #include <sys/epoll.h>  #include <sys/epoll.h>
42    #else
43    #include <poll.h>
44    #endif
45    
46  #define MENU_CONF_DELIM " \t\r\n"  static const char MENU_CONF_DELIM[] = " \t\r\n";
47    
48  #define MAX_PROCESS_BAR_LEN 30  enum _bbs_net_constant_t
49  #define MAXSTATION 26 * 2  {
50  #define STATION_PER_LINE 4          MAX_PROCESS_BAR_LEN = 30,
51            MAXSTATION = 26 * 2,
52            STATION_PER_LINE = 4,
53    };
54    
55  struct _bbsnet_conf  struct _bbsnet_conf
56  {  {
# Line 53  struct _bbsnet_conf Line 58  struct _bbsnet_conf
58          char host2[40];          char host2[40];
59          char ip[40];          char ip[40];
60          in_port_t port;          in_port_t port;
61          char charset[20];          char charset[CHARSET_MAX_LEN + 1];
62  } bbsnet_conf[MAXSTATION];  } bbsnet_conf[MAXSTATION];
63    
64  static MENU_SET bbsnet_menu;  static MENU_SET bbsnet_menu;
# Line 216  static int bbsnet_connect(int n) Line 221  static int bbsnet_connect(int n)
221          iconv_t input_cd = NULL;          iconv_t input_cd = NULL;
222          iconv_t output_cd = NULL;          iconv_t output_cd = NULL;
223          char tocode[32];          char tocode[32];
224    
225    #ifdef HAVE_SYS_EPOLL_H
226          struct epoll_event ev, events[MAX_EVENTS];          struct epoll_event ev, events[MAX_EVENTS];
227          int nfds, epollfd;          int epollfd;
228    #else
229            struct pollfd pfds[3];
230    #endif
231    
232            int nfds;
233          int stdin_read_wait = 0;          int stdin_read_wait = 0;
234          int stdout_write_wait = 0;          int stdout_write_wait = 0;
235          int sock_read_wait = 0;          int sock_read_wait = 0;
# Line 296  static int bbsnet_connect(int n) Line 308  static int bbsnet_connect(int n)
308          fcntl(STDIN_FILENO, F_SETFL, flags_stdin | O_NONBLOCK);          fcntl(STDIN_FILENO, F_SETFL, flags_stdin | O_NONBLOCK);
309          fcntl(STDOUT_FILENO, F_SETFL, flags_stdout | O_NONBLOCK);          fcntl(STDOUT_FILENO, F_SETFL, flags_stdout | O_NONBLOCK);
310    
311    #ifdef HAVE_SYS_EPOLL_H
312          epollfd = epoll_create1(0);          epollfd = epoll_create1(0);
313          if (epollfd < 0)          if (epollfd < 0)
314          {          {
# Line 318  static int bbsnet_connect(int n) Line 331  static int bbsnet_connect(int n)
331                  log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno);                  log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno);
332                  goto cleanup;                  goto cleanup;
333          }          }
334    #endif
335    
336          while (!SYS_server_exit)          while (!SYS_server_exit)
337          {          {
# Line 348  static int bbsnet_connect(int n) Line 362  static int bbsnet_connect(int n)
362    
363          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++)
364          {          {
365    #ifdef HAVE_SYS_EPOLL_H
366                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 500); // 0.5 second                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 500); // 0.5 second
367                    ret = nfds;
368    #else
369                    pfds[0].fd = sock;
370                    pfds[0].events = POLLOUT;
371                    pfds[1].fd = STDIN_FILENO;
372                    pfds[1].events = POLLIN;
373                    nfds = 2;
374                    ret = poll(pfds, (nfds_t)nfds, 500); // 0.5 second
375    #endif
376    
377                  if (nfds < 0)                  if (ret < 0)
378                  {                  {
379                          if (errno != EINTR)                          if (errno != EINTR)
380                          {                          {
381    #ifdef HAVE_SYS_EPOLL_H
382                                  log_error("epoll_wait() error (%d)\n", errno);                                  log_error("epoll_wait() error (%d)\n", errno);
383    #else
384                                    log_error("poll() error (%d)\n", errno);
385    #endif
386                                  break;                                  break;
387                          }                          }
388                  }                  }
389                  else if (nfds == 0) // timeout                  else if (ret == 0) // timeout
390                  {                  {
391                          process_bar(j + 1, MAX_PROCESS_BAR_LEN);                          process_bar(j + 1, MAX_PROCESS_BAR_LEN);
392                  }                  }
# Line 366  static int bbsnet_connect(int n) Line 394  static int bbsnet_connect(int n)
394                  {                  {
395                          for (int i = 0; i < nfds; i++)                          for (int i = 0; i < nfds; i++)
396                          {                          {
397    #ifdef HAVE_SYS_EPOLL_H
398                                  if (events[i].data.fd == sock)                                  if (events[i].data.fd == sock)
399    #else
400                                    if (pfds[i].fd == sock && (pfds[i].revents & POLLOUT))
401    #endif
402                                  {                                  {
403                                          len = sizeof(error);                                          len = sizeof(error);
404                                          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 379  static int bbsnet_connect(int n) Line 411  static int bbsnet_connect(int n)
411                                                  sock_connected = 1;                                                  sock_connected = 1;
412                                          }                                          }
413                                  }                                  }
414    #ifdef HAVE_SYS_EPOLL_H
415                                  else if (events[i].data.fd == STDIN_FILENO)                                  else if (events[i].data.fd == STDIN_FILENO)
416    #else
417                                    else if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & POLLIN))
418    #endif
419                                  {                                  {
420                                          ch = igetch(0);                                          ch = igetch(0);
421                                          if (ch == Ctrl('C') || ch == KEY_ESC)                                          if (ch == Ctrl('C') || ch == KEY_ESC)
# Line 443  static int bbsnet_connect(int n) Line 479  static int bbsnet_connect(int n)
479                  goto cleanup;                  goto cleanup;
480          }          }
481    
482    #ifdef HAVE_SYS_EPOLL_H
483          ev.events = EPOLLIN | EPOLLOUT | EPOLLET;          ev.events = EPOLLIN | EPOLLOUT | EPOLLET;
484          ev.data.fd = sock;          ev.data.fd = sock;
485          if (epoll_ctl(epollfd, EPOLL_CTL_MOD, sock, &ev) == -1)          if (epoll_ctl(epollfd, EPOLL_CTL_MOD, sock, &ev) == -1)
# Line 458  static int bbsnet_connect(int n) Line 495  static int bbsnet_connect(int n)
495                  log_error("epoll_ctl(STDOUT_FILENO) error (%d)\n", errno);                  log_error("epoll_ctl(STDOUT_FILENO) error (%d)\n", errno);
496                  goto cleanup;                  goto cleanup;
497          }          }
498    #endif
499    
500          BBS_last_access_tm = t_used = time(NULL);          BBS_last_access_tm = t_used = time(NULL);
501          loop = 1;          loop = 1;
# Line 471  static int bbsnet_connect(int n) Line 509  static int bbsnet_connect(int n)
509                          break;                          break;
510                  }                  }
511    
512    #ifdef HAVE_SYS_EPOLL_H
513                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second
514                    ret = nfds;
515    #else
516                    pfds[0].fd = STDIN_FILENO;
517                    pfds[0].events = POLLIN;
518                    pfds[1].fd = sock;
519                    pfds[1].events = POLLIN | POLLOUT;
520                    pfds[2].fd = STDOUT_FILENO;
521                    pfds[2].events = POLLOUT;
522                    nfds = 3;
523                    ret = poll(pfds, (nfds_t)nfds, 100); // 0.1 second
524    #endif
525    
526                  if (nfds < 0)                  if (ret < 0)
527                  {                  {
528                          if (errno != EINTR)                          if (errno != EINTR)
529                          {                          {
530    #ifdef HAVE_SYS_EPOLL_H
531                                  log_error("epoll_wait() error (%d)\n", errno);                                  log_error("epoll_wait() error (%d)\n", errno);
532    #else
533                                    log_error("poll() error (%d)\n", errno);
534    #endif
535                                  break;                                  break;
536                          }                          }
537                          continue;                          continue;
538                  }                  }
539                  else if (nfds == 0) // timeout                  else if (ret == 0) // timeout
540                  {                  {
541                          if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time)
542                          {                          {
543                                  break;                                  break;
544                          }                          }
# Line 492  static int bbsnet_connect(int n) Line 546  static int bbsnet_connect(int n)
546    
547                  for (int i = 0; i < nfds; i++)                  for (int i = 0; i < nfds; i++)
548                  {                  {
549    #ifdef HAVE_SYS_EPOLL_H
550                          if (events[i].data.fd == STDIN_FILENO)                          if (events[i].data.fd == STDIN_FILENO)
551    #else
552                            if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & POLLIN))
553    #endif
554                          {                          {
555                                  stdin_read_wait = 1;                                  stdin_read_wait = 1;
556                          }                          }
557    
558    #ifdef HAVE_SYS_EPOLL_H
559                          if (events[i].data.fd == sock)                          if (events[i].data.fd == sock)
560    #else
561                            if (pfds[i].fd == sock)
562    #endif
563                          {                          {
564    #ifdef HAVE_SYS_EPOLL_H
565                                  if (events[i].events & EPOLLIN)                                  if (events[i].events & EPOLLIN)
566    #else
567                                    if (pfds[i].revents & POLLIN)
568    #endif
569                                  {                                  {
570                                          sock_read_wait = 1;                                          sock_read_wait = 1;
571                                  }                                  }
572    
573    #ifdef HAVE_SYS_EPOLL_H
574                                  if (events[i].events & EPOLLOUT)                                  if (events[i].events & EPOLLOUT)
575    #else
576                                    if (pfds[i].revents & POLLOUT)
577    #endif
578                                  {                                  {
579                                          sock_write_wait = 1;                                          sock_write_wait = 1;
580                                  }                                  }
581                          }                          }
582    
583    #ifdef HAVE_SYS_EPOLL_H
584                          if (events[i].data.fd == STDOUT_FILENO)                          if (events[i].data.fd == STDOUT_FILENO)
585    #else
586                            if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & POLLOUT))
587    #endif  
588                          {                          {
589                                  stdout_write_wait = 1;                                  stdout_write_wait = 1;
590                          }                          }
# Line 778  static int bbsnet_connect(int n) Line 853  static int bbsnet_connect(int n)
853          iconv_close(output_cd);          iconv_close(output_cd);
854    
855  cleanup:  cleanup:
856    #ifdef HAVE_SYS_EPOLL_H
857          if (close(epollfd) < 0)          if (close(epollfd) < 0)
858          {          {
859                  log_error("close(epoll) error (%d)\n");                  log_error("close(epoll) error (%d)\n");
860          }          }
861    #endif
862    
863          // Restore STDIN/STDOUT flags          // Restore STDIN/STDOUT flags
864          fcntl(STDIN_FILENO, F_SETFL, flags_stdin);          fcntl(STDIN_FILENO, F_SETFL, flags_stdin);
865          fcntl(STDOUT_FILENO, F_SETFL, flags_stdout);          fcntl(STDOUT_FILENO, F_SETFL, flags_stdout);
866    
         // Restore socket flags  
         fcntl(sock, F_SETFL, flags_sock);  
   
867          if (close(sock) == -1)          if (close(sock) == -1)
868          {          {
869                  log_error("Close socket failed\n");                  log_error("Close socket failed\n");
# Line 856  static int bbsnet_selchange() Line 930  static int bbsnet_selchange()
930          return 0;          return 0;
931  }  }
932    
933  extern int bbs_net()  int bbs_net()
934  {  {
935          int ch, i;          int ch, i;
936    
# Line 882  extern int bbs_net() Line 956  extern int bbs_net()
956                          log_error("KEY_NULL\n");                          log_error("KEY_NULL\n");
957                          goto cleanup;                          goto cleanup;
958                  case KEY_TIMEOUT:                  case KEY_TIMEOUT:
959                          if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time)
960                          {                          {
961                                  log_error("User input timeout\n");                                  log_error("User input timeout\n");
962                                  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