/[LeafOK_CVS]/lbbs/src/bbs_net.c
ViewVC logotype

Diff of /lbbs/src/bbs_net.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.59 by sysadm, Sat Oct 4 10:11:44 2025 UTC Revision 1.81 by sysadm, Tue Nov 11 00:28:05 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /* SPDX-License-Identifier: GPL-3.0-or-later */
2                                                    bbs_net.c  -  description  /*
3                                                           -------------------   * bbs_net
4          Copyright            : (C) 2004-2025 by Leaflet   *   - user interactive feature of site shuttle
5          Email                : leaflet@leafok.com   *
6   ***************************************************************************/   * Copyright (C) 2004-2025  Leaflet <leaflet@leafok.com>
7     */
8  /***************************************************************************  
9   *                                                                         *  #ifdef HAVE_CONFIG_H
10   *   This program is free software; you can redistribute it and/or modify  *  #include "config.h"
11   *   it under the terms of the GNU General Public License as published by  *  #endif
  *   the Free Software Foundation; either version 3 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
12    
13  #include "bbs.h"  #include "bbs.h"
14  #include "common.h"  #include "common.h"
# Line 41  Line 37 
37  #include <sys/socket.h>  #include <sys/socket.h>
38  #include <sys/epoll.h>  #include <sys/epoll.h>
39    
40  #define MENU_CONF_DELIM " \t\r\n"  static const char MENU_CONF_DELIM[] = " \t\r\n";
41    
42  #define MAX_PROCESS_BAR_LEN 30  enum _bbs_net_constant_t
43  #define MAXSTATION 26 * 2  {
44  #define STATION_PER_LINE 4          MAX_PROCESS_BAR_LEN = 30,
45            MAXSTATION = 26 * 2,
46            STATION_PER_LINE = 4,
47    };
48    
49  struct _bbsnet_conf  struct _bbsnet_conf
50  {  {
# Line 53  struct _bbsnet_conf Line 52  struct _bbsnet_conf
52          char host2[40];          char host2[40];
53          char ip[40];          char ip[40];
54          in_port_t port;          in_port_t port;
55            char charset[CHARSET_MAX_LEN + 1];
56  } bbsnet_conf[MAXSTATION];  } bbsnet_conf[MAXSTATION];
57    
58  MENU_SET bbsnet_menu;  static MENU_SET bbsnet_menu;
59    
60  int load_bbsnet_conf(const char *file_config)  static int load_bbsnet_conf(const char *file_config)
61  {  {
62          FILE *fp;          FILE *fp;
63          MENU *p_menu;          MENU *p_menu;
64          MENU_ITEM *p_menu_item;          MENU_ITEM *p_menu_item;
65          MENU_ITEM_ID menu_item_id;          MENU_ITEM_ID menu_item_id;
66          char t[256], *t1, *t2, *t3, *t4, *saveptr;          char t[256], *t1, *t2, *t3, *t4, *t5, *saveptr;
67    
68          fp = fopen(file_config, "r");          fp = fopen(file_config, "r");
69          if (fp == NULL)          if (fp == NULL)
# Line 101  int load_bbsnet_conf(const char *file_co Line 101  int load_bbsnet_conf(const char *file_co
101                  t2 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);                  t2 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
102                  t3 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);                  t3 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
103                  t4 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);                  t4 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
104                    t5 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
105    
106                  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] == '*')
107                  {                  {
108                          continue;                          continue;
109                  }                  }
# Line 114  int load_bbsnet_conf(const char *file_co Line 115  int load_bbsnet_conf(const char *file_co
115                  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);
116                  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';
117                  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);
118                    strncpy(bbsnet_conf[menu_item_id].charset, t5, sizeof(bbsnet_conf[menu_item_id].charset) - 1);
119                    bbsnet_conf[menu_item_id].charset[sizeof(bbsnet_conf[menu_item_id].charset) - 1] = '\0';
120    
121                  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);
122                  if (p_menu_item == NULL)                  if (p_menu_item == NULL)
# Line 149  int load_bbsnet_conf(const char *file_co Line 152  int load_bbsnet_conf(const char *file_co
152          return 0;          return 0;
153  }  }
154    
155  void unload_bbsnet_conf(void)  static void unload_bbsnet_conf(void)
156  {  {
157          bbsnet_menu.menu_count = 0;          bbsnet_menu.menu_count = 0;
158          bbsnet_menu.menu_item_count = 0;          bbsnet_menu.menu_item_count = 0;
# Line 160  void unload_bbsnet_conf(void) Line 163  void unload_bbsnet_conf(void)
163          bbsnet_menu.p_menu_item_pool = NULL;          bbsnet_menu.p_menu_item_pool = NULL;
164  }  }
165    
166  void process_bar(int n, int len)  static void process_bar(int n, int len)
167  {  {
168          char buf[LINE_BUFFER_LEN];          char buf[LINE_BUFFER_LEN];
169          char buf2[LINE_BUFFER_LEN];          char buf2[LINE_BUFFER_LEN];
# Line 181  void process_bar(int n, int len) Line 184  void process_bar(int n, int len)
184          moveto(4, 0);          moveto(4, 0);
185          prints(" ------------------------------ \r\n");          prints(" ------------------------------ \r\n");
186          snprintf(buf, sizeof(buf), "            %3d%%              ", n * 100 / len);          snprintf(buf, sizeof(buf), "            %3d%%              ", n * 100 / len);
187          strncpy(buf2, buf, (size_t)n);          memcpy(buf2, buf, (size_t)n);
188          buf2[n] = '\0';          buf2[n] = '\0';
189          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);
190          prints(" ------------------------------ \r\n");          prints(" ------------------------------ \r\n");
191          iflush();          iflush();
192  }  }
193    
194  int bbsnet_connect(int n)  static int bbsnet_connect(int n)
195  {  {
196          int sock, ret, loop, error;          int sock, ret, loop, error;
197          int sock_connected = 0;          int sock_connected = 0;
# Line 203  int bbsnet_connect(int n) Line 206  int bbsnet_connect(int n)
206          int output_buf_len = 0;          int output_buf_len = 0;
207          int input_buf_offset = 0;          int input_buf_offset = 0;
208          int output_buf_offset = 0;          int output_buf_offset = 0;
209            char input_conv[LINE_BUFFER_LEN * 2];
210            char output_conv[LINE_BUFFER_LEN * 2];
211            int input_conv_len = 0;
212            int output_conv_len = 0;
213            int input_conv_offset = 0;
214            int output_conv_offset = 0;
215            iconv_t input_cd = NULL;
216            iconv_t output_cd = NULL;
217            char tocode[32];
218          struct epoll_event ev, events[MAX_EVENTS];          struct epoll_event ev, events[MAX_EVENTS];
219          int nfds, epollfd;          int nfds, epollfd;
220          int stdin_read_wait = 0;          int stdin_read_wait = 0;
# Line 213  int bbsnet_connect(int n) Line 225  int bbsnet_connect(int n)
225          int tos;          int tos;
226          char remote_addr[IP_ADDR_LEN];          char remote_addr[IP_ADDR_LEN];
227          int remote_port;          int remote_port;
228            char local_addr[IP_ADDR_LEN];
229            int local_port;
230            socklen_t sock_len;
231          time_t t_used = time(NULL);          time_t t_used = time(NULL);
232          struct tm *tm_used;          struct tm *tm_used;
233          int ch;          int ch;
# Line 392  int bbsnet_connect(int n) Line 407  int bbsnet_connect(int n)
407                  log_error("setsockopt IP_TOS=%d error (%d)\n", tos, errno);                  log_error("setsockopt IP_TOS=%d error (%d)\n", tos, errno);
408          }          }
409    
410            sock_len = sizeof(sin);
411            if (getsockname(sock, (struct sockaddr *)&sin, &sock_len) < 0)
412            {
413                    log_error("getsockname() error: %d", errno);
414                    goto cleanup;
415            }
416    
417            strncpy(local_addr, inet_ntoa(sin.sin_addr), sizeof(local_addr) - 1);
418            local_addr[sizeof(local_addr) - 1] = '\0';
419            local_port = ntohs(sin.sin_port);
420    
421          prints("\033[1;31m连接成功!\033[m\r\n");          prints("\033[1;31m连接成功!\033[m\r\n");
422          iflush();          iflush();
423          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",
424                               remote_addr, remote_port, local_addr, local_port, BBS_username);
425    
426            snprintf(tocode, sizeof(tocode), "%s%s", bbsnet_conf[n].charset,
427                             (strcasecmp(stdio_charset, bbsnet_conf[n].charset) == 0 ? "" : "//IGNORE"));
428            input_cd = iconv_open(tocode, stdio_charset);
429            if (input_cd == (iconv_t)(-1))
430            {
431                    log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, tocode, errno);
432                    goto cleanup;
433            }
434    
435            snprintf(tocode, sizeof(tocode), "%s%s", stdio_charset,
436                             (strcasecmp(bbsnet_conf[n].charset, stdio_charset) == 0 ? "" : "//TRANSLIT"));
437            output_cd = iconv_open(tocode, bbsnet_conf[n].charset);
438            if (output_cd == (iconv_t)(-1))
439            {
440                    log_error("iconv_open(%s->%s) error: %d\n", bbsnet_conf[n].charset, tocode, errno);
441                    iconv_close(input_cd);
442                    goto cleanup;
443            }
444    
445          ev.events = EPOLLIN | EPOLLOUT | EPOLLET;          ev.events = EPOLLIN | EPOLLOUT | EPOLLET;
446          ev.data.fd = sock;          ev.data.fd = sock;
# Line 437  int bbsnet_connect(int n) Line 483  int bbsnet_connect(int n)
483                  }                  }
484                  else if (nfds == 0) // timeout                  else if (nfds == 0) // timeout
485                  {                  {
486                          if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time)
487                          {                          {
488                                  break;                                  break;
489                          }                          }
# Line 489  int bbsnet_connect(int n) Line 535  int bbsnet_connect(int n)
535                                          }                                          }
536                                          else if (ret == 0)                                          else if (ret == 0)
537                                          {                                          {
538                                                    // Send NO-OP to remote server
539                                                    input_buf[input_buf_len] = '\0';
540                                                    input_buf_len++;
541                                                    BBS_last_access_tm = time(NULL);
542    
543                                                  stdin_read_wait = 0;                                                  stdin_read_wait = 0;
544                                                  break; // Check whether channel is still open                                                  break; // Check whether channel is still open
545                                          }                                          }
# Line 542  int bbsnet_connect(int n) Line 593  int bbsnet_connect(int n)
593    
594                  if (sock_write_wait)                  if (sock_write_wait)
595                  {                  {
596                          while (input_buf_offset < input_buf_len && !SYS_server_exit)                          if (input_buf_offset < input_buf_len)
597                            {
598                                    // For debug
599    #ifdef _DEBUG
600                                    for (int j = input_buf_offset; j < input_buf_len; j++)
601                                    {
602                                            log_error("Debug input: <--[%u]\n", (input_buf[j] + 256) % 256);
603                                    }
604    #endif
605    
606                                    ret = io_buf_conv(input_cd, input_buf, &input_buf_len, &input_buf_offset, input_conv, sizeof(input_conv), &input_conv_len);
607                                    if (ret < 0)
608                                    {
609                                            log_error("io_buf_conv(input, %d, %d, %d) error\n", input_buf_len, input_buf_offset, input_conv_len);
610                                            input_buf_len = input_buf_offset; // Discard invalid sequence
611                                    }
612    
613                                    // For debug
614    #ifdef _DEBUG
615                                    for (int j = input_conv_offset; j < input_conv_len; j++)
616                                    {
617                                            log_error("Debug input_conv: <--[%u]\n", (input_conv[j] + 256) % 256);
618                                    }
619    #endif
620                            }
621    
622                            while (input_conv_offset < input_conv_len && !SYS_server_exit)
623                          {                          {
624                                  ret = (int)write(sock, input_buf + input_buf_offset, (size_t)(input_buf_len - input_buf_offset));                                  ret = (int)write(sock, input_conv + input_conv_offset, (size_t)(input_conv_len - input_conv_offset));
625                                  if (ret < 0)                                  if (ret < 0)
626                                  {                                  {
627                                          if (errno == EAGAIN || errno == EWOULDBLOCK)                                          if (errno == EAGAIN || errno == EWOULDBLOCK)
# Line 574  int bbsnet_connect(int n) Line 651  int bbsnet_connect(int n)
651                                  }                                  }
652                                  else                                  else
653                                  {                                  {
654                                          input_buf_offset += ret;                                          input_conv_offset += ret;
655                                          if (input_buf_offset >= input_buf_len) // Output buffer complete                                          if (input_conv_offset >= input_conv_len) // Output buffer complete
656                                          {                                          {
657                                                  input_buf_offset = 0;                                                  input_conv_offset = 0;
658                                                  input_buf_len = 0;                                                  input_conv_len = 0;
659                                                  break;                                                  break;
660                                          }                                          }
661                                          continue;                                          continue;
# Line 628  int bbsnet_connect(int n) Line 705  int bbsnet_connect(int n)
705    
706                  if (stdout_write_wait)                  if (stdout_write_wait)
707                  {                  {
708                          while (output_buf_offset < output_buf_len && !SYS_server_exit)                          if (output_buf_offset < output_buf_len)
709                            {
710                                    ret = io_buf_conv(output_cd, output_buf, &output_buf_len, &output_buf_offset, output_conv, sizeof(output_conv), &output_conv_len);
711                                    if (ret < 0)
712                                    {
713                                            log_error("io_buf_conv(output, %d, %d, %d) error\n", output_buf_len, output_buf_offset, output_conv_len);
714                                            output_buf_len = output_buf_offset; // Discard invalid sequence
715                                    }
716                            }
717    
718                            while (output_conv_offset < output_conv_len && !SYS_server_exit)
719                          {                          {
720                                  if (SSH_v2)                                  if (SSH_v2)
721                                  {                                  {
722                                          ret = ssh_channel_write(SSH_channel, output_buf + output_buf_offset, (uint32_t)(output_buf_len - output_buf_offset));                                          ret = ssh_channel_write(SSH_channel, output_conv + output_conv_offset, (uint32_t)(output_conv_len - output_conv_offset));
723                                          if (ret == SSH_ERROR)                                          if (ret == SSH_ERROR)
724                                          {                                          {
725                                                  log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session));                                                  log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session));
# Line 642  int bbsnet_connect(int n) Line 729  int bbsnet_connect(int n)
729                                  }                                  }
730                                  else                                  else
731                                  {                                  {
732                                          ret = (int)write(STDOUT_FILENO, output_buf + output_buf_offset, (size_t)(output_buf_len - output_buf_offset));                                          ret = (int)write(STDOUT_FILENO, output_conv + output_conv_offset, (size_t)(output_conv_len - output_conv_offset));
733                                  }                                  }
734                                  if (ret < 0)                                  if (ret < 0)
735                                  {                                  {
# Line 673  int bbsnet_connect(int n) Line 760  int bbsnet_connect(int n)
760                                  }                                  }
761                                  else                                  else
762                                  {                                  {
763                                          output_buf_offset += ret;                                          output_conv_offset += ret;
764                                          if (output_buf_offset >= output_buf_len) // Output buffer complete                                          if (output_conv_offset >= output_conv_len) // Output buffer complete
765                                          {                                          {
766                                                  output_buf_offset = 0;                                                  output_conv_offset = 0;
767                                                  output_buf_len = 0;                                                  output_conv_len = 0;
768                                                  break;                                                  break;
769                                          }                                          }
770                                          continue;                                          continue;
# Line 686  int bbsnet_connect(int n) Line 773  int bbsnet_connect(int n)
773                  }                  }
774          }          }
775    
776            iconv_close(input_cd);
777            iconv_close(output_cd);
778    
779  cleanup:  cleanup:
780          if (close(epollfd) < 0)          if (close(epollfd) < 0)
781          {          {
# Line 711  cleanup: Line 801  cleanup:
801                             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,
802                             tm_used->tm_sec);                             tm_used->tm_sec);
803    
804            BBS_last_access_tm = time(NULL);
805    
806          return 0;          return 0;
807  }  }
808    
809  static int  static int bbsnet_refresh()
 bbsnet_refresh()  
810  {  {
811          clearscr();          clearscr();
812          moveto(1, 0);          moveto(1, 0);
# Line 739  bbsnet_refresh() Line 830  bbsnet_refresh()
830          return 0;          return 0;
831  }  }
832    
833  int bbsnet_selchange()  static int bbsnet_selchange()
834  {  {
835          int i = bbsnet_menu.menu_item_pos[0];          int i = bbsnet_menu.menu_item_pos[0];
836    
# Line 764  int bbsnet_selchange() Line 855  int bbsnet_selchange()
855          return 0;          return 0;
856  }  }
857    
858  int bbs_net()  extern int bbs_net()
859  {  {
860          int ch, i;          int ch, i;
861    
862          load_bbsnet_conf(CONF_BBSNET);          load_bbsnet_conf(CONF_BBSNET);
863    
         BBS_last_access_tm = time(NULL);  
   
864          clearscr();          clearscr();
865          bbsnet_refresh();          bbsnet_refresh();
866          display_menu(&bbsnet_menu);          display_menu(&bbsnet_menu);
# Line 781  int bbs_net() Line 870  int bbs_net()
870          {          {
871                  ch = igetch(100);                  ch = igetch(100);
872    
873                    if (ch != KEY_NULL && ch != KEY_TIMEOUT)
874                    {
875                            BBS_last_access_tm = time(NULL);
876                    }
877    
878                  switch (ch)                  switch (ch)
879                  {                  {
880                  case KEY_NULL: // broken pipe                  case KEY_NULL: // broken pipe
881                  case KEY_ESC:                          log_error("KEY_NULL\n");
                 case Ctrl('C'): // user cancel  
882                          goto cleanup;                          goto cleanup;
883                  case KEY_TIMEOUT:                  case KEY_TIMEOUT:
884                          if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time)
885                          {                          {
886                                    log_error("User input timeout\n");
887                                  goto cleanup;                                  goto cleanup;
888                          }                          }
889                          continue;                          continue;
890                    case KEY_ESC:
891                    case Ctrl('C'): // user cancel
892                            goto cleanup;
893                  case CR:                  case CR:
                         igetch_reset();  
894                          bbsnet_connect(bbsnet_menu.menu_item_pos[0]);                          bbsnet_connect(bbsnet_menu.menu_item_pos[0]);
895                          bbsnet_refresh();                          bbsnet_refresh();
896                          display_menu(&bbsnet_menu);                          display_menu(&bbsnet_menu);
# Line 837  int bbs_net() Line 933  int bbs_net()
933                          bbsnet_selchange();                          bbsnet_selchange();
934                          break;                          break;
935                  }                  }
                 BBS_last_access_tm = time(NULL);  
936          }          }
937    
938  cleanup:  cleanup:


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

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