/[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.58 by sysadm, Wed Oct 1 02:20:50 2025 UTC Revision 1.80 by sysadm, Wed Nov 5 04:19:21 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     */
 /***************************************************************************  
  *                                                                         *  
  *   This program is free software; you can redistribute it and/or modify  *  
  *   it under the terms of the GNU General Public License as published by  *  
  *   the Free Software Foundation; either version 3 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
8    
9  #include "bbs.h"  #include "bbs.h"
10  #include "common.h"  #include "common.h"
# Line 41  Line 33 
33  #include <sys/socket.h>  #include <sys/socket.h>
34  #include <sys/epoll.h>  #include <sys/epoll.h>
35    
36  #define MENU_CONF_DELIM " \t\r\n"  static const char MENU_CONF_DELIM[] = " \t\r\n";
37    
38  #define MAX_PROCESS_BAR_LEN 30  enum _bbs_net_constant_t
39  #define MAXSTATION 26 * 2  {
40  #define STATION_PER_LINE 4          MAX_PROCESS_BAR_LEN = 30,
41            MAXSTATION = 26 * 2,
42            STATION_PER_LINE = 4,
43    };
44    
45  struct _bbsnet_conf  struct _bbsnet_conf
46  {  {
# Line 53  struct _bbsnet_conf Line 48  struct _bbsnet_conf
48          char host2[40];          char host2[40];
49          char ip[40];          char ip[40];
50          in_port_t port;          in_port_t port;
51            char charset[CHARSET_MAX_LEN + 1];
52  } bbsnet_conf[MAXSTATION];  } bbsnet_conf[MAXSTATION];
53    
54  MENU_SET bbsnet_menu;  static MENU_SET bbsnet_menu;
55    
56  int load_bbsnet_conf(const char *file_config)  static int load_bbsnet_conf(const char *file_config)
57  {  {
58          FILE *fp;          FILE *fp;
59          MENU *p_menu;          MENU *p_menu;
60          MENU_ITEM *p_menu_item;          MENU_ITEM *p_menu_item;
61          MENU_ITEM_ID menu_item_id;          MENU_ITEM_ID menu_item_id;
62          char t[256], *t1, *t2, *t3, *t4, *saveptr;          char t[256], *t1, *t2, *t3, *t4, *t5, *saveptr;
63    
64          fp = fopen(file_config, "r");          fp = fopen(file_config, "r");
65          if (fp == NULL)          if (fp == NULL)
# Line 101  int load_bbsnet_conf(const char *file_co Line 97  int load_bbsnet_conf(const char *file_co
97                  t2 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);                  t2 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
98                  t3 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);                  t3 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
99                  t4 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);                  t4 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
100                    t5 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
101    
102                  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] == '*')
103                  {                  {
104                          continue;                          continue;
105                  }                  }
# Line 114  int load_bbsnet_conf(const char *file_co Line 111  int load_bbsnet_conf(const char *file_co
111                  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);
112                  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';
113                  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);
114                    strncpy(bbsnet_conf[menu_item_id].charset, t5, sizeof(bbsnet_conf[menu_item_id].charset) - 1);
115                    bbsnet_conf[menu_item_id].charset[sizeof(bbsnet_conf[menu_item_id].charset) - 1] = '\0';
116    
117                  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);
118                  if (p_menu_item == NULL)                  if (p_menu_item == NULL)
# Line 149  int load_bbsnet_conf(const char *file_co Line 148  int load_bbsnet_conf(const char *file_co
148          return 0;          return 0;
149  }  }
150    
151  void unload_bbsnet_conf(void)  static void unload_bbsnet_conf(void)
152  {  {
153          bbsnet_menu.menu_count = 0;          bbsnet_menu.menu_count = 0;
154          bbsnet_menu.menu_item_count = 0;          bbsnet_menu.menu_item_count = 0;
# Line 160  void unload_bbsnet_conf(void) Line 159  void unload_bbsnet_conf(void)
159          bbsnet_menu.p_menu_item_pool = NULL;          bbsnet_menu.p_menu_item_pool = NULL;
160  }  }
161    
162  void process_bar(int n, int len)  static void process_bar(int n, int len)
163  {  {
164          char buf[LINE_BUFFER_LEN];          char buf[LINE_BUFFER_LEN];
165          char buf2[LINE_BUFFER_LEN];          char buf2[LINE_BUFFER_LEN];
# Line 181  void process_bar(int n, int len) Line 180  void process_bar(int n, int len)
180          moveto(4, 0);          moveto(4, 0);
181          prints(" ------------------------------ \r\n");          prints(" ------------------------------ \r\n");
182          snprintf(buf, sizeof(buf), "            %3d%%              ", n * 100 / len);          snprintf(buf, sizeof(buf), "            %3d%%              ", n * 100 / len);
183          strncpy(buf2, buf, (size_t)n);          memcpy(buf2, buf, (size_t)n);
184          buf2[n] = '\0';          buf2[n] = '\0';
185          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);
186          prints(" ------------------------------ \r\n");          prints(" ------------------------------ \r\n");
187          iflush();          iflush();
188  }  }
189    
190  int bbsnet_connect(int n)  static int bbsnet_connect(int n)
191  {  {
192          int sock, ret, loop, error;          int sock, ret, loop, error;
193          int sock_connected = 0;          int sock_connected = 0;
# Line 203  int bbsnet_connect(int n) Line 202  int bbsnet_connect(int n)
202          int output_buf_len = 0;          int output_buf_len = 0;
203          int input_buf_offset = 0;          int input_buf_offset = 0;
204          int output_buf_offset = 0;          int output_buf_offset = 0;
205            char input_conv[LINE_BUFFER_LEN * 2];
206            char output_conv[LINE_BUFFER_LEN * 2];
207            int input_conv_len = 0;
208            int output_conv_len = 0;
209            int input_conv_offset = 0;
210            int output_conv_offset = 0;
211            iconv_t input_cd = NULL;
212            iconv_t output_cd = NULL;
213            char tocode[32];
214          struct epoll_event ev, events[MAX_EVENTS];          struct epoll_event ev, events[MAX_EVENTS];
215          int nfds, epollfd;          int nfds, epollfd;
216          int stdin_read_wait = 0;          int stdin_read_wait = 0;
# Line 213  int bbsnet_connect(int n) Line 221  int bbsnet_connect(int n)
221          int tos;          int tos;
222          char remote_addr[IP_ADDR_LEN];          char remote_addr[IP_ADDR_LEN];
223          int remote_port;          int remote_port;
224            char local_addr[IP_ADDR_LEN];
225            int local_port;
226            socklen_t sock_len;
227          time_t t_used = time(NULL);          time_t t_used = time(NULL);
228          struct tm *tm_used;          struct tm *tm_used;
229          int ch;          int ch;
# Line 287  int bbsnet_connect(int n) Line 298  int bbsnet_connect(int n)
298                  return -1;                  return -1;
299          }          }
300    
301          ev.events = EPOLLOUT;          ev.events = EPOLLOUT | EPOLLET;
302          ev.data.fd = sock;          ev.data.fd = sock;
303          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sock, &ev) == -1)          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sock, &ev) == -1)
304          {          {
# Line 295  int bbsnet_connect(int n) Line 306  int bbsnet_connect(int n)
306                  goto cleanup;                  goto cleanup;
307          }          }
308    
309          ev.events = EPOLLIN;          ev.events = EPOLLIN | EPOLLET;
310          ev.data.fd = STDIN_FILENO;          ev.data.fd = STDIN_FILENO;
311          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1)          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1)
312          {          {
# Line 392  int bbsnet_connect(int n) Line 403  int bbsnet_connect(int n)
403                  log_error("setsockopt IP_TOS=%d error (%d)\n", tos, errno);                  log_error("setsockopt IP_TOS=%d error (%d)\n", tos, errno);
404          }          }
405    
406            sock_len = sizeof(sin);
407            if (getsockname(sock, (struct sockaddr *)&sin, &sock_len) < 0)
408            {
409                    log_error("getsockname() error: %d", errno);
410                    goto cleanup;
411            }
412    
413            strncpy(local_addr, inet_ntoa(sin.sin_addr), sizeof(local_addr) - 1);
414            local_addr[sizeof(local_addr) - 1] = '\0';
415            local_port = ntohs(sin.sin_port);
416    
417          prints("\033[1;31m连接成功!\033[m\r\n");          prints("\033[1;31m连接成功!\033[m\r\n");
418          iflush();          iflush();
419          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",
420                               remote_addr, remote_port, local_addr, local_port, BBS_username);
421    
422            snprintf(tocode, sizeof(tocode), "%s%s", bbsnet_conf[n].charset,
423                             (strcasecmp(stdio_charset, bbsnet_conf[n].charset) == 0 ? "" : "//IGNORE"));
424            input_cd = iconv_open(tocode, stdio_charset);
425            if (input_cd == (iconv_t)(-1))
426            {
427                    log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, tocode, errno);
428                    goto cleanup;
429            }
430    
431            snprintf(tocode, sizeof(tocode), "%s%s", stdio_charset,
432                             (strcasecmp(bbsnet_conf[n].charset, stdio_charset) == 0 ? "" : "//TRANSLIT"));
433            output_cd = iconv_open(tocode, bbsnet_conf[n].charset);
434            if (output_cd == (iconv_t)(-1))
435            {
436                    log_error("iconv_open(%s->%s) error: %d\n", bbsnet_conf[n].charset, tocode, errno);
437                    iconv_close(input_cd);
438                    goto cleanup;
439            }
440    
441          ev.events = EPOLLIN | EPOLLOUT | EPOLLET;          ev.events = EPOLLIN | EPOLLOUT | EPOLLET;
442          ev.data.fd = sock;          ev.data.fd = sock;
# Line 404  int bbsnet_connect(int n) Line 446  int bbsnet_connect(int n)
446                  goto cleanup;                  goto cleanup;
447          }          }
448    
449          ev.events = EPOLLOUT;          ev.events = EPOLLOUT | EPOLLET;
450          ev.data.fd = STDOUT_FILENO;          ev.data.fd = STDOUT_FILENO;
451          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1)          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1)
452          {          {
# Line 437  int bbsnet_connect(int n) Line 479  int bbsnet_connect(int n)
479                  }                  }
480                  else if (nfds == 0) // timeout                  else if (nfds == 0) // timeout
481                  {                  {
482                          if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time)
483                          {                          {
484                                  break;                                  break;
485                          }                          }
                         continue;  
486                  }                  }
487    
488                  for (int i = 0; i < nfds; i++)                  for (int i = 0; i < nfds; i++)
489                  {                  {
490                          if (events[i].data.fd == STDIN_FILENO || stdin_read_wait)                          if (events[i].data.fd == STDIN_FILENO)
491                          {                          {
492                                  stdin_read_wait = 1;                                  stdin_read_wait = 1;
493                                  while (input_buf_len < sizeof(input_buf) && !SYS_server_exit)                          }
494    
495                            if (events[i].data.fd == sock)
496                            {
497                                    if (events[i].events & EPOLLIN)
498                                  {                                  {
499                                          if (SSH_v2)                                          sock_read_wait = 1;
500                                    }
501                                    if (events[i].events & EPOLLOUT)
502                                    {
503                                            sock_write_wait = 1;
504                                    }
505                            }
506    
507                            if (events[i].data.fd == STDOUT_FILENO)
508                            {
509                                    stdout_write_wait = 1;
510                            }
511                    }
512    
513                    if (stdin_read_wait)
514                    {
515                            while (input_buf_len < sizeof(input_buf) && !SYS_server_exit)
516                            {
517                                    if (SSH_v2)
518                                    {
519                                            ret = ssh_channel_read_nonblocking(SSH_channel, input_buf + input_buf_len, sizeof(input_buf) - (uint32_t)input_buf_len, 0);
520                                            if (ret == SSH_ERROR)
521                                          {                                          {
522                                                  ret = ssh_channel_read_nonblocking(SSH_channel, input_buf + input_buf_len, sizeof(input_buf) - (uint32_t)input_buf_len, 0);                                                  log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session));
523                                                  if (ret == SSH_ERROR)                                                  loop = 0;
524                                                  {                                                  break;
                                                         log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session));  
                                                         loop = 0;  
                                                         break;  
                                                 }  
                                                 else if (ret == SSH_EOF)  
                                                 {  
                                                         stdin_read_wait = 0;  
                                                         loop = 0;  
                                                         break;  
                                                 }  
                                                 else if (ret == 0)  
                                                 {  
                                                         stdin_read_wait = 0;  
                                                         break; // Check whether channel is still open  
                                                 }  
525                                          }                                          }
526                                          else                                          else if (ret == SSH_EOF)
527                                          {                                          {
528                                                  ret = (int)read(STDIN_FILENO, input_buf + input_buf_len, sizeof(input_buf) - (size_t)input_buf_len);                                                  stdin_read_wait = 0;
529                                                    loop = 0;
530                                                    break;
531                                          }                                          }
532                                          if (ret < 0)                                          else if (ret == 0)
533                                          {                                          {
534                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)                                                  // Send NO-OP to remote server
535                                                  {                                                  input_buf[input_buf_len] = '\0';
536                                                          stdin_read_wait = 0;                                                  input_buf_len++;
537                                                          break;                                                  BBS_last_access_tm = time(NULL);
538                                                  }  
539                                                  else if (errno == EINTR)                                                  stdin_read_wait = 0;
540                                                  {                                                  break; // Check whether channel is still open
                                                         continue;  
                                                 }  
                                                 else  
                                                 {  
                                                         log_error("read(STDIN) error (%d)\n", errno);  
                                                         loop = 0;  
                                                         break;  
                                                 }  
541                                          }                                          }
542                                          else if (ret == 0) // broken pipe                                  }
543                                    else
544                                    {
545                                            ret = (int)read(STDIN_FILENO, input_buf + input_buf_len, sizeof(input_buf) - (size_t)input_buf_len);
546                                    }
547                                    if (ret < 0)
548                                    {
549                                            if (errno == EAGAIN || errno == EWOULDBLOCK)
550                                          {                                          {
 #ifdef _DEBUG  
                                                 log_error("read(STDIN) EOF\n");  
 #endif  
551                                                  stdin_read_wait = 0;                                                  stdin_read_wait = 0;
                                                 loop = 0;  
552                                                  break;                                                  break;
553                                          }                                          }
554                                            else if (errno == EINTR)
555                                            {
556                                                    continue;
557                                            }
558                                          else                                          else
559                                          {                                          {
560                                                  input_buf_len += ret;                                                  log_error("read(STDIN) error (%d)\n", errno);
561                                                  BBS_last_access_tm = time(NULL);                                                  loop = 0;
562                                                    break;
563                                                  // Refresh current action while user input                                          }
564                                                  if (user_online_update("BBS_NET") < 0)                                  }
565                                                  {                                  else if (ret == 0) // broken pipe
566                                                          log_error("user_online_update(BBS_NET) error\n");                                  {
567                                                  }  #ifdef _DEBUG
568                                            log_error("read(STDIN) EOF\n");
569    #endif
570                                            stdin_read_wait = 0;
571                                            loop = 0;
572                                            break;
573                                    }
574                                    else
575                                    {
576                                            input_buf_len += ret;
577                                            BBS_last_access_tm = time(NULL);
578    
579                                                  continue;                                          // Refresh current action while user input
580                                            if (user_online_update("BBS_NET") < 0)
581                                            {
582                                                    log_error("user_online_update(BBS_NET) error\n");
583                                          }                                          }
584    
585                                            continue;
586                                  }                                  }
587                          }                          }
588                    }
589    
590                          if (events[i].data.fd == sock || sock_write_wait) // EPOLLOUT                  if (sock_write_wait)
591                    {
592                            if (input_buf_offset < input_buf_len)
593                          {                          {
594                                  sock_write_wait = 1;                                  // For debug
595                                  while (input_buf_offset < input_buf_len && !SYS_server_exit)  #ifdef _DEBUG
596                                    for (int j = input_buf_offset; j < input_buf_len; j++)
597                                  {                                  {
598                                          ret = (int)write(sock, input_buf + input_buf_offset, (size_t)(input_buf_len - input_buf_offset));                                          log_error("Debug input: <--[%u]\n", (input_buf[j] + 256) % 256);
599                                          if (ret < 0)                                  }
600                                          {  #endif
601                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)  
602                                                  {                                  ret = io_buf_conv(input_cd, input_buf, &input_buf_len, &input_buf_offset, input_conv, sizeof(input_conv), &input_conv_len);
603                                                          sock_write_wait = 0;                                  if (ret < 0)
604                                                          break;                                  {
605                                                  }                                          log_error("io_buf_conv(input, %d, %d, %d) error\n", input_buf_len, input_buf_offset, input_conv_len);
606                                                  else if (errno == EINTR)                                          input_buf_len = input_buf_offset; // Discard invalid sequence
607                                                  {                                  }
608                                                          continue;  
609                                                  }                                  // For debug
                                                 else  
                                                 {  
                                                         log_error("write(socket) error (%d)\n", errno);  
                                                         loop = 0;  
                                                         break;  
                                                 }  
                                         }  
                                         else if (ret == 0) // broken pipe  
                                         {  
610  #ifdef _DEBUG  #ifdef _DEBUG
611                                                  log_error("write(socket) EOF\n");                                  for (int j = input_conv_offset; j < input_conv_len; j++)
612                                    {
613                                            log_error("Debug input_conv: <--[%u]\n", (input_conv[j] + 256) % 256);
614                                    }
615  #endif  #endif
616                            }
617    
618                            while (input_conv_offset < input_conv_len && !SYS_server_exit)
619                            {
620                                    ret = (int)write(sock, input_conv + input_conv_offset, (size_t)(input_conv_len - input_conv_offset));
621                                    if (ret < 0)
622                                    {
623                                            if (errno == EAGAIN || errno == EWOULDBLOCK)
624                                            {
625                                                  sock_write_wait = 0;                                                  sock_write_wait = 0;
                                                 loop = 0;  
626                                                  break;                                                  break;
627                                          }                                          }
628                                          else                                          else if (errno == EINTR)
629                                          {                                          {
                                                 input_buf_offset += ret;  
                                                 if (input_buf_offset >= input_buf_len) // Output buffer complete  
                                                 {  
                                                         input_buf_offset = 0;  
                                                         input_buf_len = 0;  
                                                         break;  
                                                 }  
630                                                  continue;                                                  continue;
631                                          }                                          }
632                                            else
633                                            {
634                                                    log_error("write(socket) error (%d)\n", errno);
635                                                    loop = 0;
636                                                    break;
637                                            }
638                                    }
639                                    else if (ret == 0) // broken pipe
640                                    {
641    #ifdef _DEBUG
642                                            log_error("write(socket) EOF\n");
643    #endif
644                                            sock_write_wait = 0;
645                                            loop = 0;
646                                            break;
647                                    }
648                                    else
649                                    {
650                                            input_conv_offset += ret;
651                                            if (input_conv_offset >= input_conv_len) // Output buffer complete
652                                            {
653                                                    input_conv_offset = 0;
654                                                    input_conv_len = 0;
655                                                    break;
656                                            }
657                                            continue;
658                                  }                                  }
659                          }                          }
660                    }
661    
662                          if (events[i].data.fd == sock || sock_read_wait) // EPOLLIN                  if (sock_read_wait)
663                    {
664                            while (output_buf_len < sizeof(output_buf) && !SYS_server_exit)
665                          {                          {
666                                  sock_read_wait = 1;                                  ret = (int)read(sock, output_buf + output_buf_len, sizeof(output_buf) - (size_t)output_buf_len);
667                                  while (output_buf_len < sizeof(output_buf) && !SYS_server_exit)                                  if (ret < 0)
668                                  {                                  {
669                                          ret = (int)read(sock, output_buf + output_buf_len, sizeof(output_buf) - (size_t)output_buf_len);                                          if (errno == EAGAIN || errno == EWOULDBLOCK)
                                         if (ret < 0)  
670                                          {                                          {
                                                 if (errno == EAGAIN || errno == EWOULDBLOCK)  
                                                 {  
                                                         sock_read_wait = 0;  
                                                         break;  
                                                 }  
                                                 else if (errno == EINTR)  
                                                 {  
                                                         continue;  
                                                 }  
                                                 else  
                                                 {  
                                                         log_error("read(socket) error (%d)\n", errno);  
                                                         loop = 0;  
                                                         break;  
                                                 }  
                                         }  
                                         else if (ret == 0) // broken pipe  
                                         {  
 #ifdef _DEBUG  
                                                 log_error("read(socket) EOF\n");  
 #endif  
671                                                  sock_read_wait = 0;                                                  sock_read_wait = 0;
                                                 loop = 0;  
672                                                  break;                                                  break;
673                                          }                                          }
674                                          else                                          else if (errno == EINTR)
675                                          {                                          {
                                                 output_buf_len += ret;  
676                                                  continue;                                                  continue;
677                                          }                                          }
678                                            else
679                                            {
680                                                    log_error("read(socket) error (%d)\n", errno);
681                                                    loop = 0;
682                                                    break;
683                                            }
684                                    }
685                                    else if (ret == 0) // broken pipe
686                                    {
687    #ifdef _DEBUG
688                                            log_error("read(socket) EOF\n");
689    #endif
690                                            sock_read_wait = 0;
691                                            loop = 0;
692                                            break;
693                                    }
694                                    else
695                                    {
696                                            output_buf_len += ret;
697                                            continue;
698                                  }                                  }
699                          }                          }
700                    }
701    
702                          if (events[i].data.fd == STDOUT_FILENO || stdout_write_wait)                  if (stdout_write_wait)
703                    {
704                            if (output_buf_offset < output_buf_len)
705                          {                          {
706                                  stdout_write_wait = 1;                                  ret = io_buf_conv(output_cd, output_buf, &output_buf_len, &output_buf_offset, output_conv, sizeof(output_conv), &output_conv_len);
707                                  while (output_buf_offset < output_buf_len && !SYS_server_exit)                                  if (ret < 0)
708                                  {                                  {
709                                          if (SSH_v2)                                          log_error("io_buf_conv(output, %d, %d, %d) error\n", output_buf_len, output_buf_offset, output_conv_len);
710                                            output_buf_len = output_buf_offset; // Discard invalid sequence
711                                    }
712                            }
713    
714                            while (output_conv_offset < output_conv_len && !SYS_server_exit)
715                            {
716                                    if (SSH_v2)
717                                    {
718                                            ret = ssh_channel_write(SSH_channel, output_conv + output_conv_offset, (uint32_t)(output_conv_len - output_conv_offset));
719                                            if (ret == SSH_ERROR)
720                                          {                                          {
721                                                  ret = ssh_channel_write(SSH_channel, output_buf + output_buf_offset, (uint32_t)(output_buf_len - output_buf_offset));                                                  log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session));
722                                                  if (ret == SSH_ERROR)                                                  loop = 0;
723                                                  {                                                  break;
                                                         log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session));  
                                                         loop = 0;  
                                                         break;  
                                                 }  
724                                          }                                          }
725                                          else                                  }
726                                    else
727                                    {
728                                            ret = (int)write(STDOUT_FILENO, output_conv + output_conv_offset, (size_t)(output_conv_len - output_conv_offset));
729                                    }
730                                    if (ret < 0)
731                                    {
732                                            if (errno == EAGAIN || errno == EWOULDBLOCK)
733                                          {                                          {
734                                                  ret = (int)write(STDOUT_FILENO, output_buf + output_buf_offset, (size_t)(output_buf_len - output_buf_offset));                                                  stdout_write_wait = 0;
735                                                    break;
736                                          }                                          }
737                                          if (ret < 0)                                          else if (errno == EINTR)
738                                          {                                          {
739                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)                                                  continue;
                                                 {  
                                                         stdout_write_wait = 0;  
                                                         break;  
                                                 }  
                                                 else if (errno == EINTR)  
                                                 {  
                                                         continue;  
                                                 }  
                                                 else  
                                                 {  
                                                         log_error("write(STDOUT) error (%d)\n", errno);  
                                                         loop = 0;  
                                                         break;  
                                                 }  
740                                          }                                          }
741                                          else if (ret == 0) // broken pipe                                          else
742                                          {                                          {
743  #ifdef _DEBUG                                                  log_error("write(STDOUT) error (%d)\n", errno);
                                                 log_error("write(STDOUT) EOF\n");  
 #endif  
                                                 stdout_write_wait = 0;  
744                                                  loop = 0;                                                  loop = 0;
745                                                  break;                                                  break;
746                                          }                                          }
747                                          else                                  }
748                                    else if (ret == 0) // broken pipe
749                                    {
750    #ifdef _DEBUG
751                                            log_error("write(STDOUT) EOF\n");
752    #endif
753                                            stdout_write_wait = 0;
754                                            loop = 0;
755                                            break;
756                                    }
757                                    else
758                                    {
759                                            output_conv_offset += ret;
760                                            if (output_conv_offset >= output_conv_len) // Output buffer complete
761                                          {                                          {
762                                                  output_buf_offset += ret;                                                  output_conv_offset = 0;
763                                                  if (output_buf_offset >= output_buf_len) // Output buffer complete                                                  output_conv_len = 0;
764                                                  {                                                  break;
                                                         output_buf_offset = 0;  
                                                         output_buf_len = 0;  
                                                         break;  
                                                 }  
                                                 continue;  
765                                          }                                          }
766                                            continue;
767                                  }                                  }
768                          }                          }
769                  }                  }
770          }          }
771    
772            iconv_close(input_cd);
773            iconv_close(output_cd);
774    
775  cleanup:  cleanup:
776          if (close(epollfd) < 0)          if (close(epollfd) < 0)
777          {          {
# Line 694  cleanup: Line 797  cleanup:
797                             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,
798                             tm_used->tm_sec);                             tm_used->tm_sec);
799    
800            BBS_last_access_tm = time(NULL);
801    
802          return 0;          return 0;
803  }  }
804    
805  static int  static int bbsnet_refresh()
 bbsnet_refresh()  
806  {  {
807          clearscr();          clearscr();
808          moveto(1, 0);          moveto(1, 0);
# Line 722  bbsnet_refresh() Line 826  bbsnet_refresh()
826          return 0;          return 0;
827  }  }
828    
829  int bbsnet_selchange()  static int bbsnet_selchange()
830  {  {
831          int i = bbsnet_menu.menu_item_pos[0];          int i = bbsnet_menu.menu_item_pos[0];
832    
# Line 747  int bbsnet_selchange() Line 851  int bbsnet_selchange()
851          return 0;          return 0;
852  }  }
853    
854  int bbs_net()  extern int bbs_net()
855  {  {
856          int ch, i;          int ch, i;
857    
858          load_bbsnet_conf(CONF_BBSNET);          load_bbsnet_conf(CONF_BBSNET);
859    
         BBS_last_access_tm = time(NULL);  
   
860          clearscr();          clearscr();
861          bbsnet_refresh();          bbsnet_refresh();
862          display_menu(&bbsnet_menu);          display_menu(&bbsnet_menu);
# Line 764  int bbs_net() Line 866  int bbs_net()
866          {          {
867                  ch = igetch(100);                  ch = igetch(100);
868    
869                    if (ch != KEY_NULL && ch != KEY_TIMEOUT)
870                    {
871                            BBS_last_access_tm = time(NULL);
872                    }
873    
874                  switch (ch)                  switch (ch)
875                  {                  {
876                  case KEY_NULL: // broken pipe                  case KEY_NULL: // broken pipe
877                  case KEY_ESC:                          log_error("KEY_NULL\n");
                 case Ctrl('C'): // user cancel  
878                          goto cleanup;                          goto cleanup;
879                  case KEY_TIMEOUT:                  case KEY_TIMEOUT:
880                          if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time)
881                          {                          {
882                                    log_error("User input timeout\n");
883                                  goto cleanup;                                  goto cleanup;
884                          }                          }
885                          continue;                          continue;
886                    case KEY_ESC:
887                    case Ctrl('C'): // user cancel
888                            goto cleanup;
889                  case CR:                  case CR:
                         igetch_reset();  
890                          bbsnet_connect(bbsnet_menu.menu_item_pos[0]);                          bbsnet_connect(bbsnet_menu.menu_item_pos[0]);
891                          bbsnet_refresh();                          bbsnet_refresh();
892                          display_menu(&bbsnet_menu);                          display_menu(&bbsnet_menu);
# Line 820  int bbs_net() Line 929  int bbs_net()
929                          bbsnet_selchange();                          bbsnet_selchange();
930                          break;                          break;
931                  }                  }
                 BBS_last_access_tm = time(NULL);  
932          }          }
933    
934  cleanup:  cleanup:


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

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