/[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.61 by sysadm, Sun Oct 5 00:49:56 2025 UTC Revision 1.72 by sysadm, Mon Oct 20 03:38:23 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 193  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 273  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            char tocode[32];
219          struct epoll_event ev, events[MAX_EVENTS];          struct epoll_event ev, events[MAX_EVENTS];
220          int nfds, epollfd;          int nfds, epollfd;
221          int stdin_read_wait = 0;          int stdin_read_wait = 0;
# Line 291  int bbsnet_connect(int n) Line 226  int bbsnet_connect(int n)
226          int tos;          int tos;
227          char remote_addr[IP_ADDR_LEN];          char remote_addr[IP_ADDR_LEN];
228          int remote_port;          int remote_port;
229            char local_addr[IP_ADDR_LEN];
230            int local_port;
231            socklen_t sock_len;
232          time_t t_used = time(NULL);          time_t t_used = time(NULL);
233          struct tm *tm_used;          struct tm *tm_used;
234          int ch;          int ch;
# Line 470  int bbsnet_connect(int n) Line 408  int bbsnet_connect(int n)
408                  log_error("setsockopt IP_TOS=%d error (%d)\n", tos, errno);                  log_error("setsockopt IP_TOS=%d error (%d)\n", tos, errno);
409          }          }
410    
411            sock_len = sizeof(sin);
412            if (getsockname(sock, (struct sockaddr *)&sin, &sock_len) < 0)
413            {
414                    log_error("getsockname() error: %d", errno);
415                    goto cleanup;
416            }
417    
418            strncpy(local_addr, inet_ntoa(sin.sin_addr), sizeof(local_addr) - 1);
419            local_addr[sizeof(local_addr) - 1] = '\0';
420            local_port = ntohs(sin.sin_port);
421    
422          prints("\033[1;31m连接成功!\033[m\r\n");          prints("\033[1;31m连接成功!\033[m\r\n");
423          iflush();          iflush();
424          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",
425                               remote_addr, remote_port, local_addr, local_port, BBS_username);
426    
427          input_cd = iconv_open(bbsnet_conf[n].charset, "UTF-8");          snprintf(tocode, sizeof(tocode), "%s%s", bbsnet_conf[n].charset,
428                             (strcasecmp(stdio_charset, bbsnet_conf[n].charset) == 0 ? "" : "//IGNORE"));
429            input_cd = iconv_open(tocode, stdio_charset);
430          if (input_cd == (iconv_t)(-1))          if (input_cd == (iconv_t)(-1))
431          {          {
432                  log_error("iconv_open(UTF8->GBK) error: %d\n", errno);                  log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, tocode, errno);
433                  goto cleanup;                  goto cleanup;
434          }          }
435          output_cd = iconv_open("UTF-8", bbsnet_conf[n].charset);  
436          if (input_cd == (iconv_t)(-1))          snprintf(tocode, sizeof(tocode), "%s%s", stdio_charset,
437                             (strcasecmp(bbsnet_conf[n].charset, stdio_charset) == 0 ? "" : "//TRANSLIT"));
438            output_cd = iconv_open(tocode, bbsnet_conf[n].charset);
439            if (output_cd == (iconv_t)(-1))
440          {          {
441                  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);
442                  iconv_close(input_cd);                  iconv_close(input_cd);
443                  goto cleanup;                  goto cleanup;
444          }          }
# Line 581  int bbsnet_connect(int n) Line 536  int bbsnet_connect(int n)
536                                          }                                          }
537                                          else if (ret == 0)                                          else if (ret == 0)
538                                          {                                          {
539                                                    // Send NO-OP to remote server
540                                                    input_buf[input_buf_len] = '\0';
541                                                    input_buf_len++;
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 636  int bbsnet_connect(int n) Line 595  int bbsnet_connect(int n)
595                  {                  {
596                          if (input_buf_offset < input_buf_len)                          if (input_buf_offset < input_buf_len)
597                          {                          {
598                                  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
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)                                  if (ret < 0)
608                                  {                                  {
609                                          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);
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)                          while (input_conv_offset < input_conv_len && !SYS_server_exit)
# Line 731  int bbsnet_connect(int n) Line 707  int bbsnet_connect(int n)
707                  {                  {
708                          if (output_buf_offset < output_buf_len)                          if (output_buf_offset < output_buf_len)
709                          {                          {
710                                  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);
711                                  if (ret < 0)                                  if (ret < 0)
712                                  {                                  {
713                                          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);
714                                            output_buf_len = output_buf_offset; // Discard invalid sequence
715                                  }                                  }
716                          }                          }
717    
# Line 824  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    
# Line 883  int bbs_net() Line 862  int bbs_net()
862    
863          load_bbsnet_conf(CONF_BBSNET);          load_bbsnet_conf(CONF_BBSNET);
864    
         BBS_last_access_tm = time(NULL);  
   
865          clearscr();          clearscr();
866          bbsnet_refresh();          bbsnet_refresh();
867          display_menu(&bbsnet_menu);          display_menu(&bbsnet_menu);
# Line 894  int bbs_net() Line 871  int bbs_net()
871          {          {
872                  ch = igetch(100);                  ch = igetch(100);
873    
874                    if (ch != KEY_NULL && ch != KEY_TIMEOUT)
875                    {
876                            BBS_last_access_tm = time(NULL);
877                    }
878    
879                  switch (ch)                  switch (ch)
880                  {                  {
881                  case KEY_NULL: // broken pipe                  case KEY_NULL: // broken pipe
882                  case KEY_ESC:                          log_error("KEY_NULL\n");
                 case Ctrl('C'): // user cancel  
883                          goto cleanup;                          goto cleanup;
884                  case KEY_TIMEOUT:                  case KEY_TIMEOUT:
885                          if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)
886                          {                          {
887                                    log_error("User input timeout\n");
888                                  goto cleanup;                                  goto cleanup;
889                          }                          }
890                          continue;                          continue;
891                    case KEY_ESC:
892                    case Ctrl('C'): // user cancel
893                            goto cleanup;
894                  case CR:                  case CR:
                         igetch_reset();  
895                          bbsnet_connect(bbsnet_menu.menu_item_pos[0]);                          bbsnet_connect(bbsnet_menu.menu_item_pos[0]);
896                          bbsnet_refresh();                          bbsnet_refresh();
897                          display_menu(&bbsnet_menu);                          display_menu(&bbsnet_menu);
# Line 950  int bbs_net() Line 934  int bbs_net()
934                          bbsnet_selchange();                          bbsnet_selchange();
935                          break;                          break;
936                  }                  }
                 BBS_last_access_tm = time(NULL);  
937          }          }
938    
939  cleanup:  cleanup:


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

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