/[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.63 by sysadm, Fri Oct 10 14:02:41 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 31  Line 23 
23  #include <time.h>  #include <time.h>
24  #include <unistd.h>  #include <unistd.h>
25  #include <arpa/inet.h>  #include <arpa/inet.h>
 #include <iconv.h>  
26  #include <libssh/libssh.h>  #include <libssh/libssh.h>
27  #include <libssh/server.h>  #include <libssh/server.h>
28  #include <libssh/callbacks.h>  #include <libssh/callbacks.h>
# Line 42  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  #define BBS_NET_DEFAULT_CHARSET "UTF-8"          STATION_PER_LINE = 4,
43    };
44    
45  struct _bbsnet_conf  struct _bbsnet_conf
46  {  {
# Line 56  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[20];          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;
# Line 156  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 167  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 188  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_io_buf_conv(iconv_t cd, char *p_buf, int *p_buf_len, int *p_buf_offset, char *p_conv, size_t conv_size, int *p_conv_len)  static int bbsnet_connect(int n)
 {  
         char *in_buf;  
         char *out_buf;  
         size_t in_bytes;  
         size_t out_bytes;  
         int ret;  
   
         in_buf = p_buf + *p_buf_offset;  
         in_bytes = (size_t)(*p_buf_len - *p_buf_offset);  
         out_buf = p_conv + *p_conv_len;  
         out_bytes = conv_size - (size_t)(*p_conv_len);  
   
         while (in_bytes > 0)  
         {  
                 ret = (int)iconv(cd, &in_buf, &in_bytes, &out_buf, &out_bytes);  
                 if (ret == -1)  
                 {  
                         if (errno == EINVAL) // Incomplete  
                         {  
 #ifdef _DEBUG  
                                 log_error("iconv(inbytes=%d, outbytes=%d) error: EINVAL\n", in_bytes, out_bytes);  
 #endif  
                                 *p_buf_len = (int)(p_buf + *p_buf_len - in_buf);  
                                 *p_buf_offset = 0;  
                                 *p_conv_len = (int)(conv_size - out_bytes);  
                                 memmove(p_buf, in_buf, (size_t)(*p_buf_len));  
   
                                 break;  
                         }  
                         else if (errno == E2BIG)  
                         {  
                                 log_error("iconv(inbytes=%d, outbytes=%d) error: E2BIG\n", in_bytes, out_bytes);  
                                 return -1;  
                         }  
                         else if (errno == EILSEQ)  
                         {  
                                 if (in_bytes > out_bytes || out_bytes <= 0)  
                                 {  
                                         log_error("iconv(inbytes=%d, outbytes=%d) error: EILSEQ and E2BIG\n", in_bytes, out_bytes);  
                                         return -2;  
                                 }  
   
                                 *out_buf = *in_buf;  
                                 in_buf++;  
                                 out_buf++;  
                                 in_bytes--;  
                                 out_bytes--;  
   
                                 continue;  
                         }  
                 }  
                 else  
                 {  
                         *p_buf_len = 0;  
                         *p_buf_offset = 0;  
                         *p_conv_len = (int)(conv_size - out_bytes);  
   
                         break;  
                 }  
         }  
   
         return 0;  
 }  
   
 int bbsnet_connect(int n)  
191  {  {
192          int sock, ret, loop, error;          int sock, ret, loop, error;
193          int sock_connected = 0;          int sock_connected = 0;
# Line 275  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;
         iconv_t input_cd = NULL;  
205          char input_conv[LINE_BUFFER_LEN * 2];          char input_conv[LINE_BUFFER_LEN * 2];
206          char output_conv[LINE_BUFFER_LEN * 2];          char output_conv[LINE_BUFFER_LEN * 2];
207          int input_conv_len = 0;          int input_conv_len = 0;
208          int output_conv_len = 0;          int output_conv_len = 0;
209          int input_conv_offset = 0;          int input_conv_offset = 0;
210          int output_conv_offset = 0;          int output_conv_offset = 0;
211            iconv_t input_cd = NULL;
212          iconv_t output_cd = NULL;          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 491  int bbsnet_connect(int n) Line 419  int bbsnet_connect(int n)
419          log_common("BBSNET connect to %s:%d from %s:%d by [%s]\n",          log_common("BBSNET connect to %s:%d from %s:%d by [%s]\n",
420                             remote_addr, remote_port, local_addr, local_port, BBS_username);                             remote_addr, remote_port, local_addr, local_port, BBS_username);
421    
422          input_cd = iconv_open(bbsnet_conf[n].charset, BBS_NET_DEFAULT_CHARSET);          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))          if (input_cd == (iconv_t)(-1))
426          {          {
427                  log_error("iconv_open(%s->%s) error: %d\n", BBS_NET_DEFAULT_CHARSET, bbsnet_conf[n].charset, errno);                  log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, tocode, errno);
428                  goto cleanup;                  goto cleanup;
429          }          }
430          output_cd = iconv_open(BBS_NET_DEFAULT_CHARSET, bbsnet_conf[n].charset);  
431          if (input_cd == (iconv_t)(-1))          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, BBS_NET_DEFAULT_CHARSET, errno);                  log_error("iconv_open(%s->%s) error: %d\n", bbsnet_conf[n].charset, tocode, errno);
437                  iconv_close(input_cd);                  iconv_close(input_cd);
438                  goto cleanup;                  goto cleanup;
439          }          }
# Line 546  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                          }                          }
# Line 598  int bbsnet_connect(int n) Line 531  int bbsnet_connect(int n)
531                                          }                                          }
532                                          else if (ret == 0)                                          else if (ret == 0)
533                                          {                                          {
534                                                    // Send NO-OP to remote server
535                                                    input_buf[input_buf_len] = '\0';
536                                                    input_buf_len++;
537                                                    BBS_last_access_tm = time(NULL);
538    
539                                                  stdin_read_wait = 0;                                                  stdin_read_wait = 0;
540                                                  break; // Check whether channel is still open                                                  break; // Check whether channel is still open
541                                          }                                          }
# Line 653  int bbsnet_connect(int n) Line 591  int bbsnet_connect(int n)
591                  {                  {
592                          if (input_buf_offset < input_buf_len)                          if (input_buf_offset < input_buf_len)
593                          {                          {
594                                  ret = bbsnet_io_buf_conv(input_cd, input_buf, &input_buf_len, &input_buf_offset, input_conv, sizeof(input_conv), &input_conv_len);                                  // For debug
595    #ifdef _DEBUG
596                                    for (int j = input_buf_offset; j < input_buf_len; j++)
597                                    {
598                                            log_error("Debug input: <--[%u]\n", (input_buf[j] + 256) % 256);
599                                    }
600    #endif
601    
602                                    ret = io_buf_conv(input_cd, input_buf, &input_buf_len, &input_buf_offset, input_conv, sizeof(input_conv), &input_conv_len);
603                                  if (ret < 0)                                  if (ret < 0)
604                                  {                                  {
605                                          log_error("bbsnet_io_buf_conv(input, %d, %d, %d) error\n", input_buf_len, input_buf_offset, input_conv_len);                                          log_error("io_buf_conv(input, %d, %d, %d) error\n", input_buf_len, input_buf_offset, input_conv_len);
606                                            input_buf_len = input_buf_offset; // Discard invalid sequence
607                                  }                                  }
608    
609                                    // For debug
610    #ifdef _DEBUG
611                                    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
616                          }                          }
617    
618                          while (input_conv_offset < input_conv_len && !SYS_server_exit)                          while (input_conv_offset < input_conv_len && !SYS_server_exit)
# Line 748  int bbsnet_connect(int n) Line 703  int bbsnet_connect(int n)
703                  {                  {
704                          if (output_buf_offset < output_buf_len)                          if (output_buf_offset < output_buf_len)
705                          {                          {
706                                  ret = bbsnet_io_buf_conv(output_cd, output_buf, &output_buf_len, &output_buf_offset, output_conv, sizeof(output_conv), &output_conv_len);                                  ret = io_buf_conv(output_cd, output_buf, &output_buf_len, &output_buf_offset, output_conv, sizeof(output_conv), &output_conv_len);
707                                  if (ret < 0)                                  if (ret < 0)
708                                  {                                  {
709                                          log_error("bbsnet_io_buf_conv(output, %d, %d, %d) error\n", output_buf_len, output_buf_offset, output_conv_len);                                          log_error("io_buf_conv(output, %d, %d, %d) error\n", output_buf_len, output_buf_offset, output_conv_len);
710                                            output_buf_len = output_buf_offset; // Discard invalid sequence
711                                  }                                  }
712                          }                          }
713    
# Line 841  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 869  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 894  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 911  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 967  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