/[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.65 by sysadm, Fri Oct 17 01:25:08 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 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 48  Line 39 
39  #define MAXSTATION 26 * 2  #define MAXSTATION 26 * 2
40  #define STATION_PER_LINE 4  #define STATION_PER_LINE 4
41    
 #define BBS_NET_DEFAULT_CHARSET "UTF-8"  
   
42  struct _bbsnet_conf  struct _bbsnet_conf
43  {  {
44          char host1[20];          char host1[20];
# Line 59  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 156  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 167  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 188  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  
                         {  
 #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)  
188  {  {
189          int sock, ret, loop, error;          int sock, ret, loop, error;
190          int sock_connected = 0;          int sock_connected = 0;
# Line 275  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 491  int bbsnet_connect(int n) Line 416  int bbsnet_connect(int n)
416          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",
417                             remote_addr, remote_port, local_addr, local_port, BBS_username);                             remote_addr, remote_port, local_addr, local_port, BBS_username);
418    
419          input_cd = iconv_open(bbsnet_conf[n].charset, BBS_NET_DEFAULT_CHARSET);          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(%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);
425                  goto cleanup;                  goto cleanup;
426          }          }
427          output_cd = iconv_open(BBS_NET_DEFAULT_CHARSET, 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(%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);
434                  iconv_close(input_cd);                  iconv_close(input_cd);
435                  goto cleanup;                  goto cleanup;
436          }          }
# Line 598  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 653  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 748  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 841  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 869  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 894  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    
# Line 909  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)                  if (ch != KEY_NULL && ch != KEY_TIMEOUT)
867          {                  {
868              BBS_last_access_tm = time(NULL);                          BBS_last_access_tm = time(NULL);
869          }                  }
870    
871                  switch (ch)                  switch (ch)
872                  {                  {


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

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