/[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.59 by sysadm, Sat Oct 4 10:11:44 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 39  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 53  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[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;
69          MENU_ITEM *p_menu_item;          MENU_ITEM *p_menu_item;
70          MENU_ITEM_ID menu_item_id;          MENU_ITEM_ID menu_item_id;
71          char t[256], *t1, *t2, *t3, *t4, *saveptr;          char t[256], *t1, *t2, *t3, *t4, *t5, *saveptr;
72    
73          fp = fopen(file_config, "r");          fp = fopen(file_config, "r");
74          if (fp == NULL)          if (fp == NULL)
# Line 101  int load_bbsnet_conf(const char *file_co Line 106  int load_bbsnet_conf(const char *file_co
106                  t2 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);                  t2 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
107                  t3 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);                  t3 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
108                  t4 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);                  t4 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
109                    t5 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
110    
111                  if (t1 == NULL || t2 == NULL || t3 == NULL || t4 == NULL || t[0] == '#' || t[0] == '*')                  if (t1 == NULL || t2 == NULL || t3 == NULL || t4 == NULL || t5 == NULL || t[0] == '#' || t[0] == '*')
112                  {                  {
113                          continue;                          continue;
114                  }                  }
# Line 114  int load_bbsnet_conf(const char *file_co Line 120  int load_bbsnet_conf(const char *file_co
120                  strncpy(bbsnet_conf[menu_item_id].ip, t3, sizeof(bbsnet_conf[menu_item_id].ip) - 1);                  strncpy(bbsnet_conf[menu_item_id].ip, t3, sizeof(bbsnet_conf[menu_item_id].ip) - 1);
121                  bbsnet_conf[menu_item_id].ip[sizeof(bbsnet_conf[menu_item_id].ip) - 1] = '\0';                  bbsnet_conf[menu_item_id].ip[sizeof(bbsnet_conf[menu_item_id].ip) - 1] = '\0';
122                  bbsnet_conf[menu_item_id].port = (in_port_t)(t4 ? atoi(t4) : 23);                  bbsnet_conf[menu_item_id].port = (in_port_t)(t4 ? atoi(t4) : 23);
123                    strncpy(bbsnet_conf[menu_item_id].charset, t5, sizeof(bbsnet_conf[menu_item_id].charset) - 1);
124                    bbsnet_conf[menu_item_id].charset[sizeof(bbsnet_conf[menu_item_id].charset) - 1] = '\0';
125    
126                  p_menu_item = get_menu_item_by_id(&bbsnet_menu, menu_item_id);                  p_menu_item = get_menu_item_by_id(&bbsnet_menu, menu_item_id);
127                  if (p_menu_item == NULL)                  if (p_menu_item == NULL)
# Line 149  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 160  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 181  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_connect(int n)  static 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 203  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;
214            char input_conv[LINE_BUFFER_LEN * 2];
215            char output_conv[LINE_BUFFER_LEN * 2];
216            int input_conv_len = 0;
217            int output_conv_len = 0;
218            int input_conv_offset = 0;
219            int output_conv_offset = 0;
220            iconv_t input_cd = NULL;
221            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 213  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 280  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 302  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 332  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 350  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 363  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 392  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            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))
466            {
467                    log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, tocode, errno);
468                    goto cleanup;
469            }
470    
471            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(%s->%s) error: %d\n", bbsnet_conf[n].charset, tocode, errno);
477                    iconv_close(input_cd);
478                    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 411  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 424  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 445  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 489  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 542  int bbsnet_connect(int n) Line 668  int bbsnet_connect(int n)
668    
669                  if (sock_write_wait)                  if (sock_write_wait)
670                  {                  {
671                          while (input_buf_offset < input_buf_len && !SYS_server_exit)                          if (input_buf_offset < input_buf_len)
672                            {
673                                    // 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)
683                                    {
684                                            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)
698                          {                          {
699                                  ret = (int)write(sock, input_buf + input_buf_offset, (size_t)(input_buf_len - input_buf_offset));                                  ret = (int)write(sock, input_conv + input_conv_offset, (size_t)(input_conv_len - input_conv_offset));
700                                  if (ret < 0)                                  if (ret < 0)
701                                  {                                  {
702                                          if (errno == EAGAIN || errno == EWOULDBLOCK)                                          if (errno == EAGAIN || errno == EWOULDBLOCK)
# Line 574  int bbsnet_connect(int n) Line 726  int bbsnet_connect(int n)
726                                  }                                  }
727                                  else                                  else
728                                  {                                  {
729                                          input_buf_offset += ret;                                          input_conv_offset += ret;
730                                          if (input_buf_offset >= input_buf_len) // Output buffer complete                                          if (input_conv_offset >= input_conv_len) // Output buffer complete
731                                          {                                          {
732                                                  input_buf_offset = 0;                                                  input_conv_offset = 0;
733                                                  input_buf_len = 0;                                                  input_conv_len = 0;
734                                                  break;                                                  break;
735                                          }                                          }
736                                          continue;                                          continue;
# Line 628  int bbsnet_connect(int n) Line 780  int bbsnet_connect(int n)
780    
781                  if (stdout_write_wait)                  if (stdout_write_wait)
782                  {                  {
783                          while (output_buf_offset < output_buf_len && !SYS_server_exit)                          if (output_buf_offset < output_buf_len)
784                            {
785                                    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)
787                                    {
788                                            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    
793                            while (output_conv_offset < output_conv_len && !SYS_server_exit)
794                          {                          {
795                                  if (SSH_v2)                                  if (SSH_v2)
796                                  {                                  {
797                                          ret = ssh_channel_write(SSH_channel, output_buf + output_buf_offset, (uint32_t)(output_buf_len - output_buf_offset));                                          ret = ssh_channel_write(SSH_channel, output_conv + output_conv_offset, (uint32_t)(output_conv_len - output_conv_offset));
798                                          if (ret == SSH_ERROR)                                          if (ret == SSH_ERROR)
799                                          {                                          {
800                                                  log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session));                                                  log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session));
# Line 642  int bbsnet_connect(int n) Line 804  int bbsnet_connect(int n)
804                                  }                                  }
805                                  else                                  else
806                                  {                                  {
807                                          ret = (int)write(STDOUT_FILENO, output_buf + output_buf_offset, (size_t)(output_buf_len - output_buf_offset));                                          ret = (int)write(STDOUT_FILENO, output_conv + output_conv_offset, (size_t)(output_conv_len - output_conv_offset));
808                                  }                                  }
809                                  if (ret < 0)                                  if (ret < 0)
810                                  {                                  {
# Line 673  int bbsnet_connect(int n) Line 835  int bbsnet_connect(int n)
835                                  }                                  }
836                                  else                                  else
837                                  {                                  {
838                                          output_buf_offset += ret;                                          output_conv_offset += ret;
839                                          if (output_buf_offset >= output_buf_len) // Output buffer complete                                          if (output_conv_offset >= output_conv_len) // Output buffer complete
840                                          {                                          {
841                                                  output_buf_offset = 0;                                                  output_conv_offset = 0;
842                                                  output_buf_len = 0;                                                  output_conv_len = 0;
843                                                  break;                                                  break;
844                                          }                                          }
845                                          continue;                                          continue;
# Line 686  int bbsnet_connect(int n) Line 848  int bbsnet_connect(int n)
848                  }                  }
849          }          }
850    
851            iconv_close(input_cd);
852            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 711  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 739  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 764  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 781  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 837  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