/[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.66 by sysadm, Sat Oct 18 01:50:48 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    
50    #define BBS_NET_DEFAULT_CHARSET "UTF-8"
51    
52  struct _bbsnet_conf  struct _bbsnet_conf
53  {  {
54          char host1[20];          char host1[20];
# Line 193  void process_bar(int n, int len) Line 194  void process_bar(int n, int len)
194          iflush();          iflush();
195  }  }
196    
 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;  
 }  
   
197  int bbsnet_connect(int n)  int bbsnet_connect(int n)
198  {  {
199          int sock, ret, loop, error;          int sock, ret, loop, error;
# Line 291  int bbsnet_connect(int n) Line 227  int bbsnet_connect(int n)
227          int tos;          int tos;
228          char remote_addr[IP_ADDR_LEN];          char remote_addr[IP_ADDR_LEN];
229          int remote_port;          int remote_port;
230            char local_addr[IP_ADDR_LEN];
231            int local_port;
232            socklen_t sock_len;
233          time_t t_used = time(NULL);          time_t t_used = time(NULL);
234          struct tm *tm_used;          struct tm *tm_used;
235          int ch;          int ch;
# Line 470  int bbsnet_connect(int n) Line 409  int bbsnet_connect(int n)
409                  log_error("setsockopt IP_TOS=%d error (%d)\n", tos, errno);                  log_error("setsockopt IP_TOS=%d error (%d)\n", tos, errno);
410          }          }
411    
412            sock_len = sizeof(sin);
413            if (getsockname(sock, (struct sockaddr *)&sin, &sock_len) < 0)
414            {
415                    log_error("getsockname() error: %d", errno);
416                    goto cleanup;
417            }
418    
419            strncpy(local_addr, inet_ntoa(sin.sin_addr), sizeof(local_addr) - 1);
420            local_addr[sizeof(local_addr) - 1] = '\0';
421            local_port = ntohs(sin.sin_port);
422    
423          prints("\033[1;31m连接成功!\033[m\r\n");          prints("\033[1;31m连接成功!\033[m\r\n");
424          iflush();          iflush();
425          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",
426                               remote_addr, remote_port, local_addr, local_port, BBS_username);
427    
428          input_cd = iconv_open(bbsnet_conf[n].charset, "UTF-8");          input_cd = iconv_open(bbsnet_conf[n].charset, BBS_NET_DEFAULT_CHARSET);
429          if (input_cd == (iconv_t)(-1))          if (input_cd == (iconv_t)(-1))
430          {          {
431                  log_error("iconv_open(UTF8->GBK) error: %d\n", errno);                  log_error("iconv_open(%s->%s) error: %d\n", BBS_NET_DEFAULT_CHARSET, bbsnet_conf[n].charset, errno);
432                  goto cleanup;                  goto cleanup;
433          }          }
434          output_cd = iconv_open("UTF-8", bbsnet_conf[n].charset);          output_cd = iconv_open(BBS_NET_DEFAULT_CHARSET, bbsnet_conf[n].charset);
435          if (input_cd == (iconv_t)(-1))          if (input_cd == (iconv_t)(-1))
436          {          {
437                  log_error("iconv_open(GBK->UTF-8) error: %d\n", errno);                  log_error("iconv_open(%s->%s) error: %d\n", bbsnet_conf[n].charset, BBS_NET_DEFAULT_CHARSET, errno);
438                  iconv_close(input_cd);                  iconv_close(input_cd);
439                  goto cleanup;                  goto cleanup;
440          }          }
# Line 636  int bbsnet_connect(int n) Line 587  int bbsnet_connect(int n)
587                  {                  {
588                          if (input_buf_offset < input_buf_len)                          if (input_buf_offset < input_buf_len)
589                          {                          {
590                                  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);
591                                  if (ret < 0)                                  if (ret < 0)
592                                  {                                  {
593                                          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);
594                                  }                                  }
595                          }                          }
596    
# Line 731  int bbsnet_connect(int n) Line 682  int bbsnet_connect(int n)
682                  {                  {
683                          if (output_buf_offset < output_buf_len)                          if (output_buf_offset < output_buf_len)
684                          {                          {
685                                  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);
686                                  if (ret < 0)                                  if (ret < 0)
687                                  {                                  {
688                                          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);
689                                  }                                  }
690                          }                          }
691    
# Line 883  int bbs_net() Line 834  int bbs_net()
834    
835          load_bbsnet_conf(CONF_BBSNET);          load_bbsnet_conf(CONF_BBSNET);
836    
         BBS_last_access_tm = time(NULL);  
   
837          clearscr();          clearscr();
838          bbsnet_refresh();          bbsnet_refresh();
839          display_menu(&bbsnet_menu);          display_menu(&bbsnet_menu);
# Line 894  int bbs_net() Line 843  int bbs_net()
843          {          {
844                  ch = igetch(100);                  ch = igetch(100);
845    
846            if (ch != KEY_NULL && ch != KEY_TIMEOUT)
847            {
848                BBS_last_access_tm = time(NULL);
849            }
850    
851                  switch (ch)                  switch (ch)
852                  {                  {
853                  case KEY_NULL: // broken pipe                  case KEY_NULL: // broken pipe
854                  case KEY_ESC:                          log_error("KEY_NULL\n");
                 case Ctrl('C'): // user cancel  
855                          goto cleanup;                          goto cleanup;
856                  case KEY_TIMEOUT:                  case KEY_TIMEOUT:
857                          if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)
858                          {                          {
859                                    log_error("User input timeout\n");
860                                  goto cleanup;                                  goto cleanup;
861                          }                          }
862                          continue;                          continue;
863                    case KEY_ESC:
864                    case Ctrl('C'): // user cancel
865                            goto cleanup;
866                  case CR:                  case CR:
                         igetch_reset();  
867                          bbsnet_connect(bbsnet_menu.menu_item_pos[0]);                          bbsnet_connect(bbsnet_menu.menu_item_pos[0]);
868                          bbsnet_refresh();                          bbsnet_refresh();
869                          display_menu(&bbsnet_menu);                          display_menu(&bbsnet_menu);
# Line 950  int bbs_net() Line 906  int bbs_net()
906                          bbsnet_selchange();                          bbsnet_selchange();
907                          break;                          break;
908                  }                  }
                 BBS_last_access_tm = time(NULL);  
909          }          }
910    
911  cleanup:  cleanup:


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

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