/[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.60 by sysadm, Sat Oct 4 15:55:26 2025 UTC Revision 1.78 by sysadm, Wed Nov 5 01:04:06 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 57  struct _bbsnet_conf Line 48  struct _bbsnet_conf
48          char charset[20];          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;
# Line 154  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 165  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 186  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_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  
                         {  
                                 *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)  
                                 {  
                                         errno = E2BIG;  
                                         return -1;  
                                 }  
   
                                 *out_buf++ = *in_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)  
188  {  {
189          int sock, ret, loop, error;          int sock, ret, loop, error;
190          int sock_connected = 0;          int sock_connected = 0;
# Line 268  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;
         iconv_t input_cd = NULL;  
202          char input_conv[LINE_BUFFER_LEN * 2];          char input_conv[LINE_BUFFER_LEN * 2];
203          char output_conv[LINE_BUFFER_LEN * 2];          char output_conv[LINE_BUFFER_LEN * 2];
204          int input_conv_len = 0;          int input_conv_len = 0;
205          int output_conv_len = 0;          int output_conv_len = 0;
206          int input_conv_offset = 0;          int input_conv_offset = 0;
207          int output_conv_offset = 0;          int output_conv_offset = 0;
208            iconv_t input_cd = NULL;
209          iconv_t output_cd = NULL;          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 286  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 465  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          input_cd = iconv_open(bbsnet_conf[n].charset, "UTF-8");          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))          if (input_cd == (iconv_t)(-1))
423          {          {
424                  log_error("iconv_open(UTF8->GBK) error: %d\n", errno);                  log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, tocode, errno);
425                  goto cleanup;                  goto cleanup;
426          }          }
427          output_cd = iconv_open("UTF-8", bbsnet_conf[n].charset);  
428          if (input_cd == (iconv_t)(-1))          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(GBK->UTF-8) error: %d\n", errno);                  log_error("iconv_open(%s->%s) error: %d\n", bbsnet_conf[n].charset, tocode, errno);
434                  iconv_close(input_cd);                  iconv_close(input_cd);
435                  goto cleanup;                  goto cleanup;
436          }          }
# Line 524  int bbsnet_connect(int n) Line 476  int bbsnet_connect(int n)
476                  }                  }
477                  else if (nfds == 0) // timeout                  else if (nfds == 0) // timeout
478                  {                  {
479                          if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time)
480                          {                          {
481                                  break;                                  break;
482                          }                          }
# Line 576  int bbsnet_connect(int n) Line 528  int bbsnet_connect(int n)
528                                          }                                          }
529                                          else if (ret == 0)                                          else if (ret == 0)
530                                          {                                          {
531                                                    // Send NO-OP to remote server
532                                                    input_buf[input_buf_len] = '\0';
533                                                    input_buf_len++;
534                                                    BBS_last_access_tm = time(NULL);
535    
536                                                  stdin_read_wait = 0;                                                  stdin_read_wait = 0;
537                                                  break; // Check whether channel is still open                                                  break; // Check whether channel is still open
538                                          }                                          }
# Line 631  int bbsnet_connect(int n) Line 588  int bbsnet_connect(int n)
588                  {                  {
589                          if (input_buf_offset < input_buf_len)                          if (input_buf_offset < input_buf_len)
590                          {                          {
591                                  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
592    #ifdef _DEBUG
593                                    for (int j = input_buf_offset; j < input_buf_len; j++)
594                                    {
595                                            log_error("Debug input: <--[%u]\n", (input_buf[j] + 256) % 256);
596                                    }
597    #endif
598    
599                                    ret = io_buf_conv(input_cd, input_buf, &input_buf_len, &input_buf_offset, input_conv, sizeof(input_conv), &input_conv_len);
600                                  if (ret < 0)                                  if (ret < 0)
601                                  {                                  {
602                                          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);
603                                            input_buf_len = input_buf_offset; // Discard invalid sequence
604                                    }
605    
606                                    // For debug
607    #ifdef _DEBUG
608                                    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
613                          }                          }
614    
615                          while (input_conv_offset < input_conv_len && !SYS_server_exit)                          while (input_conv_offset < input_conv_len && !SYS_server_exit)
# Line 726  int bbsnet_connect(int n) Line 700  int bbsnet_connect(int n)
700                  {                  {
701                          if (output_buf_offset < output_buf_len)                          if (output_buf_offset < output_buf_len)
702                          {                          {
703                                  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);
704                                  if (ret < 0)                                  if (ret < 0)
705                                  {                                  {
706                                          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);
707                                            output_buf_len = output_buf_offset; // Discard invalid sequence
708                                  }                                  }
709                          }                          }
710    
# Line 819  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 847  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 872  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 889  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 >= BBS_max_user_idle_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 945  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