/[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.61 by sysadm, Sun Oct 5 00:49:56 2025 UTC Revision 1.82 by sysadm, Mon Nov 17 06:41:18 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 "common.h"  #include "common.h"
# Line 31  Line 27 
27  #include <time.h>  #include <time.h>
28  #include <unistd.h>  #include <unistd.h>
29  #include <arpa/inet.h>  #include <arpa/inet.h>
 #include <iconv.h>  
30  #include <libssh/libssh.h>  #include <libssh/libssh.h>
31  #include <libssh/server.h>  #include <libssh/server.h>
32  #include <libssh/callbacks.h>  #include <libssh/callbacks.h>
# Line 40  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 54  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  MENU_SET bbsnet_menu;  static MENU_SET bbsnet_menu;
64    
65  int load_bbsnet_conf(const char *file_config)  static int load_bbsnet_conf(const char *file_config)
66  {  {
67          FILE *fp;          FILE *fp;
68          MENU *p_menu;          MENU *p_menu;
# Line 154  int load_bbsnet_conf(const char *file_co Line 157  int load_bbsnet_conf(const char *file_co
157          return 0;          return 0;
158  }  }
159    
160  void unload_bbsnet_conf(void)  static void unload_bbsnet_conf(void)
161  {  {
162          bbsnet_menu.menu_count = 0;          bbsnet_menu.menu_count = 0;
163          bbsnet_menu.menu_item_count = 0;          bbsnet_menu.menu_item_count = 0;
# Line 165  void unload_bbsnet_conf(void) Line 168  void unload_bbsnet_conf(void)
168          bbsnet_menu.p_menu_item_pool = NULL;          bbsnet_menu.p_menu_item_pool = NULL;
169  }  }
170    
171  void process_bar(int n, int len)  static void process_bar(int n, int len)
172  {  {
173          char buf[LINE_BUFFER_LEN];          char buf[LINE_BUFFER_LEN];
174          char buf2[LINE_BUFFER_LEN];          char buf2[LINE_BUFFER_LEN];
# Line 186  void process_bar(int n, int len) Line 189  void process_bar(int n, int len)
189          moveto(4, 0);          moveto(4, 0);
190          prints(" ------------------------------ \r\n");          prints(" ------------------------------ \r\n");
191          snprintf(buf, sizeof(buf), "            %3d%%              ", n * 100 / len);          snprintf(buf, sizeof(buf), "            %3d%%              ", n * 100 / len);
192          strncpy(buf2, buf, (size_t)n);          memcpy(buf2, buf, (size_t)n);
193          buf2[n] = '\0';          buf2[n] = '\0';
194          prints("|\033[46m%s\033[44m%s\033[m|\r\n", buf2, buf + n);          prints("|\033[46m%s\033[44m%s\033[m|\r\n", buf2, buf + n);
195          prints(" ------------------------------ \r\n");          prints(" ------------------------------ \r\n");
196          iflush();          iflush();
197  }  }
198    
199  int bbsnet_io_buf_conv(iconv_t cd, char *p_buf, int *p_buf_len, int *p_buf_offset, char *p_conv, size_t conv_size, int *p_conv_len)  static int bbsnet_connect(int n)
 {  
         char *in_buf;  
         char *out_buf;  
         size_t in_bytes;  
         size_t out_bytes;  
         int ret;  
   
         in_buf = p_buf + *p_buf_offset;  
         in_bytes = (size_t)(*p_buf_len - *p_buf_offset);  
         out_buf = p_conv + *p_conv_len;  
         out_bytes = conv_size - (size_t)(*p_conv_len);  
   
         while (in_bytes > 0)  
         {  
                 ret = (int)iconv(cd, &in_buf, &in_bytes, &out_buf, &out_bytes);  
                 if (ret == -1)  
                 {  
                         if (errno == EINVAL) // Incomplete  
                         {  
 #ifdef _DEBUG  
                                 log_error("iconv(inbytes=%d, outbytes=%d) error: EINVAL\n", in_bytes, out_bytes);  
 #endif  
                                 *p_buf_len = (int)(p_buf + *p_buf_len - in_buf);  
                                 *p_buf_offset = 0;  
                                 *p_conv_len = (int)(conv_size - out_bytes);  
                                 memmove(p_buf, in_buf, (size_t)(*p_buf_len));  
   
                                 break;  
                         }  
                         else if (errno == E2BIG)  
                         {  
                                 log_error("iconv(inbytes=%d, outbytes=%d) error: E2BIG\n", in_bytes, out_bytes);  
                                 return -1;  
                         }  
                         else if (errno == EILSEQ)  
                         {  
                                 if (in_bytes > out_bytes || out_bytes <= 0)  
                                 {  
                                         log_error("iconv(inbytes=%d, outbytes=%d) error: EILSEQ and E2BIG\n", in_bytes, out_bytes);  
                                         return -2;  
                                 }  
   
                                 *out_buf = *in_buf;  
                                 in_buf++;  
                                 out_buf++;  
                                 in_bytes--;  
                                 out_bytes--;  
   
                                 continue;  
                         }  
                 }  
                 else  
                 {  
                         *p_buf_len = 0;  
                         *p_buf_offset = 0;  
                         *p_conv_len = (int)(conv_size - out_bytes);  
   
                         break;  
                 }  
         }  
   
         return 0;  
 }  
   
 int bbsnet_connect(int n)  
200  {  {
201          int sock, ret, loop, error;          int sock, ret, loop, error;
202          int sock_connected = 0;          int sock_connected = 0;
# Line 273  int bbsnet_connect(int n) Line 211  int bbsnet_connect(int n)
211          int output_buf_len = 0;          int output_buf_len = 0;
212          int input_buf_offset = 0;          int input_buf_offset = 0;
213          int output_buf_offset = 0;          int output_buf_offset = 0;
         iconv_t input_cd = NULL;  
214          char input_conv[LINE_BUFFER_LEN * 2];          char input_conv[LINE_BUFFER_LEN * 2];
215          char output_conv[LINE_BUFFER_LEN * 2];          char output_conv[LINE_BUFFER_LEN * 2];
216          int input_conv_len = 0;          int input_conv_len = 0;
217          int output_conv_len = 0;          int output_conv_len = 0;
218          int input_conv_offset = 0;          int input_conv_offset = 0;
219          int output_conv_offset = 0;          int output_conv_offset = 0;
220            iconv_t input_cd = NULL;
221          iconv_t output_cd = NULL;          iconv_t output_cd = NULL;
222            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 291  int bbsnet_connect(int n) Line 237  int bbsnet_connect(int n)
237          int tos;          int tos;
238          char remote_addr[IP_ADDR_LEN];          char remote_addr[IP_ADDR_LEN];
239          int remote_port;          int remote_port;
240            char local_addr[IP_ADDR_LEN];
241            int local_port;
242            socklen_t sock_len;
243          time_t t_used = time(NULL);          time_t t_used = time(NULL);
244          struct tm *tm_used;          struct tm *tm_used;
245          int ch;          int ch;
# Line 358  int bbsnet_connect(int n) Line 307  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 380  int bbsnet_connect(int n) Line 330  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 410  int bbsnet_connect(int n) Line 361  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 428  int bbsnet_connect(int n) Line 393  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 441  int bbsnet_connect(int n) Line 410  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 470  int bbsnet_connect(int n) Line 443  int bbsnet_connect(int n)
443                  log_error("setsockopt IP_TOS=%d error (%d)\n", tos, errno);                  log_error("setsockopt IP_TOS=%d error (%d)\n", tos, errno);
444          }          }
445    
446            sock_len = sizeof(sin);
447            if (getsockname(sock, (struct sockaddr *)&sin, &sock_len) < 0)
448            {
449                    log_error("getsockname() error: %d", errno);
450                    goto cleanup;
451            }
452    
453            strncpy(local_addr, inet_ntoa(sin.sin_addr), sizeof(local_addr) - 1);
454            local_addr[sizeof(local_addr) - 1] = '\0';
455            local_port = ntohs(sin.sin_port);
456    
457          prints("\033[1;31m连接成功!\033[m\r\n");          prints("\033[1;31m连接成功!\033[m\r\n");
458          iflush();          iflush();
459          log_common("BBSNET connect to %s:%d\n", remote_addr, remote_port);          log_common("BBSNET connect to %s:%d from %s:%d by [%s]\n",
460                               remote_addr, remote_port, local_addr, local_port, BBS_username);
461    
462          input_cd = iconv_open(bbsnet_conf[n].charset, "UTF-8");          snprintf(tocode, sizeof(tocode), "%s%s", bbsnet_conf[n].charset,
463                             (strcasecmp(stdio_charset, bbsnet_conf[n].charset) == 0 ? "" : "//IGNORE"));
464            input_cd = iconv_open(tocode, stdio_charset);
465          if (input_cd == (iconv_t)(-1))          if (input_cd == (iconv_t)(-1))
466          {          {
467                  log_error("iconv_open(UTF8->GBK) error: %d\n", errno);                  log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, tocode, errno);
468                  goto cleanup;                  goto cleanup;
469          }          }
470          output_cd = iconv_open("UTF-8", bbsnet_conf[n].charset);  
471          if (input_cd == (iconv_t)(-1))          snprintf(tocode, sizeof(tocode), "%s%s", stdio_charset,
472                             (strcasecmp(bbsnet_conf[n].charset, stdio_charset) == 0 ? "" : "//TRANSLIT"));
473            output_cd = iconv_open(tocode, bbsnet_conf[n].charset);
474            if (output_cd == (iconv_t)(-1))
475          {          {
476                  log_error("iconv_open(GBK->UTF-8) error: %d\n", errno);                  log_error("iconv_open(%s->%s) error: %d\n", bbsnet_conf[n].charset, tocode, errno);
477                  iconv_close(input_cd);                  iconv_close(input_cd);
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 503  int bbsnet_connect(int n) Line 494  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 516  int bbsnet_connect(int n) Line 508  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 537  int bbsnet_connect(int n) Line 545  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 581  int bbsnet_connect(int n) Line 610  int bbsnet_connect(int n)
610                                          }                                          }
611                                          else if (ret == 0)                                          else if (ret == 0)
612                                          {                                          {
613                                                    // Send NO-OP to remote server
614                                                    input_buf[input_buf_len] = '\0';
615                                                    input_buf_len++;
616                                                    BBS_last_access_tm = time(NULL);
617    
618                                                  stdin_read_wait = 0;                                                  stdin_read_wait = 0;
619                                                  break; // Check whether channel is still open                                                  break; // Check whether channel is still open
620                                          }                                          }
# Line 636  int bbsnet_connect(int n) Line 670  int bbsnet_connect(int n)
670                  {                  {
671                          if (input_buf_offset < input_buf_len)                          if (input_buf_offset < input_buf_len)
672                          {                          {
673                                  ret = bbsnet_io_buf_conv(input_cd, input_buf, &input_buf_len, &input_buf_offset, input_conv, sizeof(input_conv), &input_conv_len);                                  // For debug
674    #ifdef _DEBUG
675                                    for (int j = input_buf_offset; j < input_buf_len; j++)
676                                    {
677                                            log_error("Debug input: <--[%u]\n", (input_buf[j] + 256) % 256);
678                                    }
679    #endif
680    
681                                    ret = io_buf_conv(input_cd, input_buf, &input_buf_len, &input_buf_offset, input_conv, sizeof(input_conv), &input_conv_len);
682                                  if (ret < 0)                                  if (ret < 0)
683                                  {                                  {
684                                          log_error("bbsnet_io_buf_conv(input, %d, %d, %d) error\n", input_buf_len, input_buf_offset, input_conv_len);                                          log_error("io_buf_conv(input, %d, %d, %d) error\n", input_buf_len, input_buf_offset, input_conv_len);
685                                            input_buf_len = input_buf_offset; // Discard invalid sequence
686                                    }
687    
688                                    // For debug
689    #ifdef _DEBUG
690                                    for (int j = input_conv_offset; j < input_conv_len; j++)
691                                    {
692                                            log_error("Debug input_conv: <--[%u]\n", (input_conv[j] + 256) % 256);
693                                  }                                  }
694    #endif
695                          }                          }
696    
697                          while (input_conv_offset < input_conv_len && !SYS_server_exit)                          while (input_conv_offset < input_conv_len && !SYS_server_exit)
# Line 731  int bbsnet_connect(int n) Line 782  int bbsnet_connect(int n)
782                  {                  {
783                          if (output_buf_offset < output_buf_len)                          if (output_buf_offset < output_buf_len)
784                          {                          {
785                                  ret = bbsnet_io_buf_conv(output_cd, output_buf, &output_buf_len, &output_buf_offset, output_conv, sizeof(output_conv), &output_conv_len);                                  ret = io_buf_conv(output_cd, output_buf, &output_buf_len, &output_buf_offset, output_conv, sizeof(output_conv), &output_conv_len);
786                                  if (ret < 0)                                  if (ret < 0)
787                                  {                                  {
788                                          log_error("bbsnet_io_buf_conv(output, %d, %d, %d) error\n", output_buf_len, output_buf_offset, output_conv_len);                                          log_error("io_buf_conv(output, %d, %d, %d) error\n", output_buf_len, output_buf_offset, output_conv_len);
789                                            output_buf_len = output_buf_offset; // Discard invalid sequence
790                                  }                                  }
791                          }                          }
792    
# Line 800  int bbsnet_connect(int n) Line 852  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 824  cleanup: Line 875  cleanup:
875                             tm_used->tm_mday - 1, tm_used->tm_hour, tm_used->tm_min,                             tm_used->tm_mday - 1, tm_used->tm_hour, tm_used->tm_min,
876                             tm_used->tm_sec);                             tm_used->tm_sec);
877    
878            BBS_last_access_tm = time(NULL);
879    
880          return 0;          return 0;
881  }  }
882    
883  static int  static int bbsnet_refresh()
 bbsnet_refresh()  
884  {  {
885          clearscr();          clearscr();
886          moveto(1, 0);          moveto(1, 0);
# Line 852  bbsnet_refresh() Line 904  bbsnet_refresh()
904          return 0;          return 0;
905  }  }
906    
907  int bbsnet_selchange()  static int bbsnet_selchange()
908  {  {
909          int i = bbsnet_menu.menu_item_pos[0];          int i = bbsnet_menu.menu_item_pos[0];
910    
# Line 877  int bbsnet_selchange() Line 929  int bbsnet_selchange()
929          return 0;          return 0;
930  }  }
931    
932  int bbs_net()  extern int bbs_net()
933  {  {
934          int ch, i;          int ch, i;
935    
936          load_bbsnet_conf(CONF_BBSNET);          load_bbsnet_conf(CONF_BBSNET);
937    
         BBS_last_access_tm = time(NULL);  
   
938          clearscr();          clearscr();
939          bbsnet_refresh();          bbsnet_refresh();
940          display_menu(&bbsnet_menu);          display_menu(&bbsnet_menu);
# Line 894  int bbs_net() Line 944  int bbs_net()
944          {          {
945                  ch = igetch(100);                  ch = igetch(100);
946    
947                    if (ch != KEY_NULL && ch != KEY_TIMEOUT)
948                    {
949                            BBS_last_access_tm = time(NULL);
950                    }
951    
952                  switch (ch)                  switch (ch)
953                  {                  {
954                  case KEY_NULL: // broken pipe                  case KEY_NULL: // broken pipe
955                  case KEY_ESC:                          log_error("KEY_NULL\n");
                 case Ctrl('C'): // user cancel  
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");
961                                  goto cleanup;                                  goto cleanup;
962                          }                          }
963                          continue;                          continue;
964                    case KEY_ESC:
965                    case Ctrl('C'): // user cancel
966                            goto cleanup;
967                  case CR:                  case CR:
                         igetch_reset();  
968                          bbsnet_connect(bbsnet_menu.menu_item_pos[0]);                          bbsnet_connect(bbsnet_menu.menu_item_pos[0]);
969                          bbsnet_refresh();                          bbsnet_refresh();
970                          display_menu(&bbsnet_menu);                          display_menu(&bbsnet_menu);
# Line 950  int bbs_net() Line 1007  int bbs_net()
1007                          bbsnet_selchange();                          bbsnet_selchange();
1008                          break;                          break;
1009                  }                  }
                 BBS_last_access_tm = time(NULL);  
1010          }          }
1011    
1012  cleanup:  cleanup:


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

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