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


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

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