/[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.64 by sysadm, Thu Oct 16 11:26:16 2025 UTC Revision 1.67 by sysadm, Sat Oct 18 05:02:15 2025 UTC
# Line 31  Line 31 
31  #include <time.h>  #include <time.h>
32  #include <unistd.h>  #include <unistd.h>
33  #include <arpa/inet.h>  #include <arpa/inet.h>
 #include <iconv.h>  
34  #include <libssh/libssh.h>  #include <libssh/libssh.h>
35  #include <libssh/server.h>  #include <libssh/server.h>
36  #include <libssh/callbacks.h>  #include <libssh/callbacks.h>
# Line 48  Line 47 
47  #define MAXSTATION 26 * 2  #define MAXSTATION 26 * 2
48  #define STATION_PER_LINE 4  #define STATION_PER_LINE 4
49    
 #define BBS_NET_DEFAULT_CHARSET "UTF-8"  
   
50  struct _bbsnet_conf  struct _bbsnet_conf
51  {  {
52          char host1[20];          char host1[20];
# Line 195  void process_bar(int n, int len) Line 192  void process_bar(int n, int len)
192          iflush();          iflush();
193  }  }
194    
 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)  
 {  
         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;  
 }  
   
195  int bbsnet_connect(int n)  int bbsnet_connect(int n)
196  {  {
197          int sock, ret, loop, error;          int sock, ret, loop, error;
# Line 275  int bbsnet_connect(int n) Line 207  int bbsnet_connect(int n)
207          int output_buf_len = 0;          int output_buf_len = 0;
208          int input_buf_offset = 0;          int input_buf_offset = 0;
209          int output_buf_offset = 0;          int output_buf_offset = 0;
         iconv_t input_cd = NULL;  
210          char input_conv[LINE_BUFFER_LEN * 2];          char input_conv[LINE_BUFFER_LEN * 2];
211          char output_conv[LINE_BUFFER_LEN * 2];          char output_conv[LINE_BUFFER_LEN * 2];
212          int input_conv_len = 0;          int input_conv_len = 0;
213          int output_conv_len = 0;          int output_conv_len = 0;
214          int input_conv_offset = 0;          int input_conv_offset = 0;
215          int output_conv_offset = 0;          int output_conv_offset = 0;
216            iconv_t input_cd = NULL;
217          iconv_t output_cd = NULL;          iconv_t output_cd = NULL;
218          struct epoll_event ev, events[MAX_EVENTS];          struct epoll_event ev, events[MAX_EVENTS];
219          int nfds, epollfd;          int nfds, epollfd;
# Line 491  int bbsnet_connect(int n) Line 423  int bbsnet_connect(int n)
423          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",
424                             remote_addr, remote_port, local_addr, local_port, BBS_username);                             remote_addr, remote_port, local_addr, local_port, BBS_username);
425    
426          input_cd = iconv_open(bbsnet_conf[n].charset, BBS_NET_DEFAULT_CHARSET);          input_cd = iconv_open(bbsnet_conf[n].charset, stdio_charset);
427          if (input_cd == (iconv_t)(-1))          if (input_cd == (iconv_t)(-1))
428          {          {
429                  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, bbsnet_conf[n].charset, errno);
430                  goto cleanup;                  goto cleanup;
431          }          }
432          output_cd = iconv_open(BBS_NET_DEFAULT_CHARSET, bbsnet_conf[n].charset);          output_cd = iconv_open(stdio_charset, bbsnet_conf[n].charset);
433          if (input_cd == (iconv_t)(-1))          if (output_cd == (iconv_t)(-1))
434          {          {
435                  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, stdio_charset, errno);
436                  iconv_close(input_cd);                  iconv_close(input_cd);
437                  goto cleanup;                  goto cleanup;
438          }          }
# Line 653  int bbsnet_connect(int n) Line 585  int bbsnet_connect(int n)
585                  {                  {
586                          if (input_buf_offset < input_buf_len)                          if (input_buf_offset < input_buf_len)
587                          {                          {
588                                  ret = bbsnet_io_buf_conv(input_cd, input_buf, &input_buf_len, &input_buf_offset, input_conv, sizeof(input_conv), &input_conv_len);                                  ret = io_buf_conv(input_cd, input_buf, &input_buf_len, &input_buf_offset, input_conv, sizeof(input_conv), &input_conv_len);
589                                  if (ret < 0)                                  if (ret < 0)
590                                  {                                  {
591                                          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);
592                                  }                                  }
593                          }                          }
594    
# Line 748  int bbsnet_connect(int n) Line 680  int bbsnet_connect(int n)
680                  {                  {
681                          if (output_buf_offset < output_buf_len)                          if (output_buf_offset < output_buf_len)
682                          {                          {
683                                  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);
684                                  if (ret < 0)                                  if (ret < 0)
685                                  {                                  {
686                                          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);
687                                  }                                  }
688                          }                          }
689    
# Line 930  int bbs_net() Line 862  int bbs_net()
862                  case Ctrl('C'): // user cancel                  case Ctrl('C'): // user cancel
863                          goto cleanup;                          goto cleanup;
864                  case CR:                  case CR:
                         igetch_reset();  
865                          bbsnet_connect(bbsnet_menu.menu_item_pos[0]);                          bbsnet_connect(bbsnet_menu.menu_item_pos[0]);
866                          bbsnet_refresh();                          bbsnet_refresh();
867                          display_menu(&bbsnet_menu);                          display_menu(&bbsnet_menu);


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

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