/[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.48 by sysadm, Thu Jun 5 08:36:02 2025 UTC Revision 1.57 by sysadm, Wed Jul 2 04:17:33 2025 UTC
# Line 16  Line 16 
16    
17  #include "bbs.h"  #include "bbs.h"
18  #include "common.h"  #include "common.h"
 #include "log.h"  
19  #include "io.h"  #include "io.h"
20  #include "screen.h"  #include "log.h"
21    #include "login.h"
22  #include "menu.h"  #include "menu.h"
23  #include <stdio.h>  #include "screen.h"
 #include <stdarg.h>  
24  #include <errno.h>  #include <errno.h>
 #include <string.h>  
 #include <stdlib.h>  
25  #include <fcntl.h>  #include <fcntl.h>
26    #include <netdb.h>
27    #include <stdarg.h>
28    #include <stdio.h>
29    #include <stdlib.h>
30    #include <string.h>
31  #include <time.h>  #include <time.h>
32  #include <unistd.h>  #include <unistd.h>
 #include <netdb.h>  
 #include <sys/select.h>  
 #include <sys/ioctl.h>  
 #include <sys/socket.h>  
 #include <sys/epoll.h>  
 #include <netinet/in.h>  
 #include <netinet/ip.h>  
33  #include <arpa/inet.h>  #include <arpa/inet.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>
37    #include <netinet/in.h>
38    #include <netinet/ip.h>
39    #include <sys/select.h>
40    #include <sys/ioctl.h>
41    #include <sys/socket.h>
42    #include <sys/epoll.h>
43    
44  #define MENU_CONF_DELIM " \t\r\n"  #define MENU_CONF_DELIM " \t\r\n"
45    
# Line 212  int bbsnet_connect(int n) Line 213  int bbsnet_connect(int n)
213          int tos;          int tos;
214          char remote_addr[IP_ADDR_LEN];          char remote_addr[IP_ADDR_LEN];
215          int remote_port;          int remote_port;
216          time_t t_used;          time_t t_used = time(NULL);
217          struct tm *tm_used;          struct tm *tm_used;
218          int ch;          int ch;
219    
220            if (user_online_update("BBS_NET") < 0)
221            {
222                    log_error("user_online_update(BBS_NET) error\n");
223            }
224    
225          clearscr();          clearscr();
226    
227          moveto(0, 0);          moveto(0, 0);
228          prints("\033[1;32m正在测试往 %s (%s) 的连接,请稍候... \033[m\r\n",          prints("\033[1;32m姝e湪娴嬭瘯寰 %s (%s) 鐨勮繛鎺ワ紝璇风◢鍊... \033[m\r\n",
229                     bbsnet_conf[n].host1, bbsnet_conf[n].ip);                     bbsnet_conf[n].host1, bbsnet_conf[n].ip);
230          iflush();          iflush();
231    
# Line 227  int bbsnet_connect(int n) Line 233  int bbsnet_connect(int n)
233    
234          if (p_host == NULL)          if (p_host == NULL)
235          {          {
236                  prints("\033[1;31m查找主机名失败!\033[m\r\n");                  prints("\033[1;31m鏌ユ壘涓绘満鍚嶅け璐ワ紒\033[m\r\n");
237                  press_any_key();                  press_any_key();
238                  return -1;                  return -1;
239          }          }
# Line 236  int bbsnet_connect(int n) Line 242  int bbsnet_connect(int n)
242    
243          if (sock < 0)          if (sock < 0)
244          {          {
245                  prints("\033[1;31m无法创建socket!\033[m\r\n");                  prints("\033[1;31m鏃犳硶鍒涘缓socket锛乗033[m\r\n");
246                  press_any_key();                  press_any_key();
247                  return -1;                  return -1;
248          }          }
# Line 252  int bbsnet_connect(int n) Line 258  int bbsnet_connect(int n)
258                  return -2;                  return -2;
259          }          }
260    
261          bzero(&sin, sizeof(sin));          memset(&sin, 0, sizeof(sin));
262          sin.sin_family = AF_INET;          sin.sin_family = AF_INET;
263          sin.sin_addr = *(struct in_addr *)p_host->h_addr_list[0];          sin.sin_addr = *(struct in_addr *)p_host->h_addr_list[0];
264          sin.sin_port = htons(bbsnet_conf[n].port);          sin.sin_port = htons(bbsnet_conf[n].port);
# Line 261  int bbsnet_connect(int n) Line 267  int bbsnet_connect(int n)
267          remote_addr[sizeof(remote_addr) - 1] = '\0';          remote_addr[sizeof(remote_addr) - 1] = '\0';
268          remote_port = ntohs(sin.sin_port);          remote_port = ntohs(sin.sin_port);
269    
270          prints("\033[1;32m穿梭进度条提示您当前已使用的时间,按\033[1;33mCtrl+C\033[1;32m中断。\033[m\r\n");          prints("\033[1;32m绌挎杩涘害鏉℃彁绀烘偍褰撳墠宸蹭娇鐢ㄧ殑鏃堕棿锛屾寜\033[1;33mCtrl+C\033[1;32m涓柇銆俓033[m\r\n");
271          process_bar(0, MAX_PROCESS_BAR_LEN);          process_bar(0, MAX_PROCESS_BAR_LEN);
272    
273          // Set socket as non-blocking          // Set socket as non-blocking
# Line 316  int bbsnet_connect(int n) Line 322  int bbsnet_connect(int n)
322                          {                          {
323                                  log_error("connect(socket) error (%d)\n", errno);                                  log_error("connect(socket) error (%d)\n", errno);
324    
325                                  prints("\033[1;31m连接失败!\033[m\r\n");                                  prints("\033[1;31m杩炴帴澶辫触锛乗033[m\r\n");
326                                  press_any_key();                                  press_any_key();
327    
328                                  goto cleanup;                                  goto cleanup;
# Line 374  int bbsnet_connect(int n) Line 380  int bbsnet_connect(int n)
380          }          }
381          if (!sock_connected)          if (!sock_connected)
382          {          {
383                  prints("\033[1;31m连接失败!\033[m\r\n");                  prints("\033[1;31m杩炴帴澶辫触锛乗033[m\r\n");
384                  press_any_key();                  press_any_key();
385    
386                  goto cleanup;                  goto cleanup;
# Line 386  int bbsnet_connect(int n) Line 392  int bbsnet_connect(int n)
392                  log_error("setsockopt IP_TOS=%d error (%d)\n", tos, errno);                  log_error("setsockopt IP_TOS=%d error (%d)\n", tos, errno);
393          }          }
394    
395          prints("\033[1;31m连接成功!\033[m\r\n");          prints("\033[1;31m杩炴帴鎴愬姛锛乗033[m\r\n");
396          iflush();          iflush();
397          log_common("BBSNET connect to %s:%d\n", remote_addr, remote_port);          log_common("BBSNET connect to %s:%d\n", remote_addr, remote_port);
398    
# Line 406  int bbsnet_connect(int n) Line 412  int bbsnet_connect(int n)
412                  goto cleanup;                  goto cleanup;
413          }          }
414    
415          BBS_last_access_tm = t_used = time(0);          BBS_last_access_tm = t_used = time(NULL);
416          loop = 1;          loop = 1;
417    
418          while (loop && !SYS_server_exit)          while (loop && !SYS_server_exit)
# Line 431  int bbsnet_connect(int n) Line 437  int bbsnet_connect(int n)
437                  }                  }
438                  else if (nfds == 0) // timeout                  else if (nfds == 0) // timeout
439                  {                  {
440                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)
441                          {                          {
442                                  break;                                  break;
443                          }                          }
# Line 490  int bbsnet_connect(int n) Line 496  int bbsnet_connect(int n)
496                                          }                                          }
497                                          else if (ret == 0) // broken pipe                                          else if (ret == 0) // broken pipe
498                                          {                                          {
499                                                  log_common("read(STDIN) EOF\n");  #ifdef _DEBUG
500                                                    log_error("read(STDIN) EOF\n");
501    #endif
502                                                  stdin_read_wait = 0;                                                  stdin_read_wait = 0;
503                                                  loop = 0;                                                  loop = 0;
504                                                  break;                                                  break;
# Line 498  int bbsnet_connect(int n) Line 506  int bbsnet_connect(int n)
506                                          else                                          else
507                                          {                                          {
508                                                  input_buf_len += ret;                                                  input_buf_len += ret;
509                                                  BBS_last_access_tm = time(0);                                                  BBS_last_access_tm = time(NULL);
510                                                  continue;                                                  continue;
511                                          }                                          }
512                                  }                                  }
# Line 530  int bbsnet_connect(int n) Line 538  int bbsnet_connect(int n)
538                                          }                                          }
539                                          else if (ret == 0) // broken pipe                                          else if (ret == 0) // broken pipe
540                                          {                                          {
541                                                  log_common("write(socket) EOF\n");  #ifdef _DEBUG
542                                                    log_error("write(socket) EOF\n");
543    #endif
544                                                  sock_write_wait = 0;                                                  sock_write_wait = 0;
545                                                  loop = 0;                                                  loop = 0;
546                                                  break;                                                  break;
# Line 575  int bbsnet_connect(int n) Line 585  int bbsnet_connect(int n)
585                                          }                                          }
586                                          else if (ret == 0) // broken pipe                                          else if (ret == 0) // broken pipe
587                                          {                                          {
588                                                  log_common("read(socket) EOF\n");  #ifdef _DEBUG
589                                                    log_error("read(socket) EOF\n");
590    #endif
591                                                  sock_read_wait = 0;                                                  sock_read_wait = 0;
592                                                  loop = 0;                                                  loop = 0;
593                                                  break;                                                  break;
# Line 627  int bbsnet_connect(int n) Line 639  int bbsnet_connect(int n)
639                                          }                                          }
640                                          else if (ret == 0) // broken pipe                                          else if (ret == 0) // broken pipe
641                                          {                                          {
642                                                  log_common("write(STDOUT) EOF\n");  #ifdef _DEBUG
643                                                    log_error("write(STDOUT) EOF\n");
644    #endif
645                                                  stdout_write_wait = 0;                                                  stdout_write_wait = 0;
646                                                  loop = 0;                                                  loop = 0;
647                                                  break;                                                  break;
# Line 666  cleanup: Line 680  cleanup:
680                  log_error("Close socket failed\n");                  log_error("Close socket failed\n");
681          }          }
682    
683          t_used = time(0) - t_used;          t_used = time(NULL) - t_used;
684          tm_used = gmtime(&t_used);          tm_used = gmtime(&t_used);
685    
686          log_common("BBSNET disconnect, %d days %d hours %d minutes %d seconds used\n",          log_common("BBSNET disconnect, %d days %d hours %d minutes %d seconds used\n",
# Line 694  bbsnet_refresh() Line 708  bbsnet_refresh()
708          moveto(22, 0);          moveto(22, 0);
709          prints(" ----------------------------------------------------------------------------- ");          prints(" ----------------------------------------------------------------------------- ");
710          moveto(23, 0);          moveto(23, 0);
711          prints(" [\x1b[1;32mCtrl+C\x1b[m]退出");          prints(" [\x1b[1;32mCtrl+C\x1b[m]閫鍑");
712    
713          iflush();          iflush();
714    
# Line 707  int bbsnet_selchange() Line 721  int bbsnet_selchange()
721    
722          moveto(20, 0);          moveto(20, 0);
723          clrtoeol();          clrtoeol();
724          prints("|\x1b[1m单位:\x1b[1;33m%-18s\x1b[m  站名:\x1b[1;33m%s\x1b[m",          prints("|\x1b[1m鍗曚綅:\x1b[1;33m%-18s\x1b[m  绔欏悕:\x1b[1;33m%s\x1b[m",
725                     bbsnet_conf[i].host2, bbsnet_conf[i].host1);                     bbsnet_conf[i].host2, bbsnet_conf[i].host1);
726          moveto(20, 79);          moveto(20, 79);
727          prints("|");          prints("|");
728          moveto(21, 0);          moveto(21, 0);
729          clrtoeol();          clrtoeol();
730          prints("|\x1b[1m连往:\x1b[1;33m%-20s", bbsnet_conf[i].ip);          prints("|\x1b[1m杩炲線:\x1b[1;33m%-20s", bbsnet_conf[i].ip);
731          if (bbsnet_conf[i].port != 23)          if (bbsnet_conf[i].port != 23)
732          {          {
733                  prints("  %d", bbsnet_conf[i].port);                  prints("  %d", bbsnet_conf[i].port);
# Line 732  int bbs_net() Line 746  int bbs_net()
746    
747          load_bbsnet_conf(CONF_BBSNET);          load_bbsnet_conf(CONF_BBSNET);
748    
749          BBS_last_access_tm = time(0);          BBS_last_access_tm = time(NULL);
750    
751          clearscr();          clearscr();
752          bbsnet_refresh();          bbsnet_refresh();
# Line 742  int bbs_net() Line 756  int bbs_net()
756          while (!SYS_server_exit)          while (!SYS_server_exit)
757          {          {
758                  ch = igetch(100);                  ch = igetch(100);
759    
760                  switch (ch)                  switch (ch)
761                  {                  {
762                  case KEY_NULL: // broken pipe                  case KEY_NULL: // broken pipe
# Line 749  int bbs_net() Line 764  int bbs_net()
764                  case Ctrl('C'): // user cancel                  case Ctrl('C'): // user cancel
765                          goto cleanup;                          goto cleanup;
766                  case KEY_TIMEOUT:                  case KEY_TIMEOUT:
767                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)
768                          {                          {
769                                  goto cleanup;                                  goto cleanup;
770                          }                          }
# Line 798  int bbs_net() Line 813  int bbs_net()
813                          bbsnet_selchange();                          bbsnet_selchange();
814                          break;                          break;
815                  }                  }
816                  BBS_last_access_tm = time(0);                  BBS_last_access_tm = time(NULL);
817          }          }
818    
819  cleanup:  cleanup:


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

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