/[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.104 by sysadm, Fri Dec 19 06:16:26 2025 UTC Revision 1.105 by sysadm, Sat Dec 20 03:24:01 2025 UTC
# Line 251  static void progress_bar(int percent, in Line 251  static void progress_bar(int percent, in
251          iflush();          iflush();
252  }  }
253    
254    static int progress_update(struct timespec *p_ts_begin, struct timespec *p_ts_now,
255                                                       int total_time_ms, int *p_progress_last, int bar_len)
256    {
257            int progress;
258    
259            if (clock_gettime(CLOCK_REALTIME, p_ts_now) == -1)
260            {
261                    log_error("clock_gettime() error (%d)", errno);
262                    return -1;
263            }
264    
265            progress = (int)((p_ts_now->tv_sec - p_ts_begin->tv_sec) * 1000 +
266                                             (p_ts_now->tv_nsec - p_ts_begin->tv_nsec) / 1000 / 1000) /
267                                       REMOTE_CONNECT_TIMEOUT / 10 +
268                               1;
269            if (progress < 0)
270            {
271                    progress = 0;
272            }
273            if (progress > 100)
274            {
275                    progress = 100;
276            }
277    
278            if (progress != *p_progress_last)
279            {
280                    *p_progress_last = progress;
281                    progress_bar(progress, PROGRESS_BAR_LEN);
282            }
283    
284            return 0;
285    }
286    
287  static int bbsnet_connect(int n)  static int bbsnet_connect(int n)
288  {  {
289          int sock = -1;          int sock = -1;
# Line 299  static int bbsnet_connect(int n) Line 332  static int bbsnet_connect(int n)
332          socklen_t sock_len;          socklen_t sock_len;
333          time_t t_begin, t_used;          time_t t_begin, t_used;
334          struct timespec ts_begin, ts_now;          struct timespec ts_begin, ts_now;
335          int progress, progress_last;          int progress_last = 0;
336          int ch;          int ch;
337          char remote_user[USERNAME_MAX_LEN + 1];          char remote_user[USERNAME_MAX_LEN + 1];
338          char remote_pass[PASSWORD_MAX_LEN + 1];          char remote_pass[PASSWORD_MAX_LEN + 1];
# Line 357  static int bbsnet_connect(int n) Line 390  static int bbsnet_connect(int n)
390          moveto(1, 1);          moveto(1, 1);
391          prints("\033[1;32m正在测试往 %s (%s) 的连接,请稍候... \033[m\r\n",          prints("\033[1;32m正在测试往 %s (%s) 的连接,请稍候... \033[m\r\n",
392                     bbsnet_conf[n].site_name, bbsnet_conf[n].host_name);                     bbsnet_conf[n].site_name, bbsnet_conf[n].host_name);
393          iflush();          prints("\033[1;32m连接进行中,按\033[1;33mCtrl+C\033[1;32m中断。\033[m\r\n");
394            progress_bar(0, PROGRESS_BAR_LEN);
395    
396            if (clock_gettime(CLOCK_REALTIME, &ts_begin) == -1)
397            {
398                    log_error("clock_gettime() error (%d)", errno);
399                    ret = -1;
400                    goto cleanup;
401            }
402            ts_now = ts_begin;
403    
404          memset(&hints, 0, sizeof(hints));          memset(&hints, 0, sizeof(hints));
405          hints.ai_family = AF_INET;          hints.ai_family = AF_INET;
# Line 488  static int bbsnet_connect(int n) Line 530  static int bbsnet_connect(int n)
530          }          }
531  #endif  #endif
532    
533          while (!SYS_server_exit)          while ((ts_now.tv_sec - ts_begin.tv_sec) * 1000 +
534                                       (ts_now.tv_nsec - ts_begin.tv_nsec) / 1000 / 1000 <
535                               REMOTE_CONNECT_TIMEOUT * 1000 &&
536                       !SYS_server_exit)
537          {          {
538                    if (clock_gettime(CLOCK_REALTIME, &ts_now) == -1)
539                    {
540                            log_error("clock_gettime() error (%d)", errno);
541                            ret = -1;
542                            goto cleanup;
543                    }
544    
545                  if ((ret = connect(sock, res->ai_addr, res->ai_addrlen)) == 0)                  if ((ret = connect(sock, res->ai_addr, res->ai_addrlen)) == 0)
546                  {                  {
547                          sock_connected = 1;                          sock_connected = 1;
# Line 506  static int bbsnet_connect(int n) Line 558  static int bbsnet_connect(int n)
558                          }                          }
559                          else if (errno == EINTR)                          else if (errno == EINTR)
560                          {                          {
                                 continue;  
561                          }                          }
562                          else                          else
563                          {                          {
# Line 516  static int bbsnet_connect(int n) Line 567  static int bbsnet_connect(int n)
567                                  ret = -1;                                  ret = -1;
568                                  goto cleanup;                                  goto cleanup;
569                          }                          }
                 }  
         }  
570    
571          progress = progress_last = 0;                          if (progress_update(&ts_begin, &ts_now,
572          prints("\033[1;32m连接进行中,按\033[1;33mCtrl+C\033[1;32m中断。\033[m\r\n");                                                                  REMOTE_CONNECT_TIMEOUT * 1000,
573          progress_bar(0, PROGRESS_BAR_LEN);                                                                  &progress_last, PROGRESS_BAR_LEN) < 0)
574                            {
575          if (clock_gettime(CLOCK_REALTIME, &ts_begin) == -1)                                  log_error("progress_update() error");
576          {                                  ret = -1;
577                  log_error("clock_gettime() error (%d)", errno);                                  goto cleanup;
578                  ret = -1;                          }
579                  goto cleanup;                  }
580          }          }
         ts_now = ts_begin;  
581    
582          while ((ts_now.tv_sec - ts_begin.tv_sec) * 1000 +          while ((ts_now.tv_sec - ts_begin.tv_sec) * 1000 +
583                                     (ts_now.tv_nsec - ts_begin.tv_nsec) / 1000 / 1000 <                                     (ts_now.tv_nsec - ts_begin.tv_nsec) / 1000 / 1000 <
584                             REMOTE_CONNECT_TIMEOUT * 1000 &&                             REMOTE_CONNECT_TIMEOUT * 1000 &&
585                     !sock_connected && !SYS_server_exit)                     !sock_connected && !SYS_server_exit)
586          {          {
587                    if (clock_gettime(CLOCK_REALTIME, &ts_now) == -1)
588                    {
589                            log_error("clock_gettime() error (%d)", errno);
590                            ret = -1;
591                            goto cleanup;
592                    }
593    
594  #ifdef HAVE_SYS_EPOLL_H  #ifdef HAVE_SYS_EPOLL_H
595                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second
596                  ret = nfds;                  ret = nfds;
# Line 562  static int bbsnet_connect(int n) Line 617  static int bbsnet_connect(int n)
617                  }                  }
618                  else if (ret == 0) // timeout                  else if (ret == 0) // timeout
619                  {                  {
                         if (clock_gettime(CLOCK_REALTIME, &ts_now) == -1)  
                         {  
                                 log_error("clock_gettime() error (%d)", errno);  
                                 ret = -1;  
                                 goto cleanup;  
                         }  
   
                         progress = (int)((ts_now.tv_sec - ts_begin.tv_sec) * 1000 +  
                                                          (ts_now.tv_nsec - ts_begin.tv_nsec) / 1000 / 1000) /  
                                                    REMOTE_CONNECT_TIMEOUT / 10 +  
                                            1;  
                         if (progress < 0)  
                         {  
                                 progress = 0;  
                         }  
                         if (progress > 100)  
                         {  
                                 progress = 100;  
                         }  
   
                         if (progress > progress_last)  
                         {  
                                 progress_last = progress;  
                                 progress_bar(progress, PROGRESS_BAR_LEN);  
                         }  
620                  }                  }
621                  else // ret > 0                  else // ret > 0
622                  {                  {
# Line 629  static int bbsnet_connect(int n) Line 659  static int bbsnet_connect(int n)
659                                  }                                  }
660                          }                          }
661                  }                  }
662    
663                    if (progress_update(&ts_begin, &ts_now,
664                                                            REMOTE_CONNECT_TIMEOUT * 1000,
665                                                            &progress_last, PROGRESS_BAR_LEN) < 0)
666                    {
667                            log_error("progress_update() error");
668                            ret = -1;
669                            goto cleanup;
670                    }
671          }          }
672          if (SYS_server_exit)          if (SYS_server_exit)
673          {          {
# Line 693  static int bbsnet_connect(int n) Line 732  static int bbsnet_connect(int n)
732    
733                  t_begin = time(NULL);                  t_begin = time(NULL);
734                  ret = SSH_ERROR;                  ret = SSH_ERROR;
735                  while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT)                  while ((ts_now.tv_sec - ts_begin.tv_sec) * 1000 +
736                                               (ts_now.tv_nsec - ts_begin.tv_nsec) / 1000 / 1000 <
737                                       REMOTE_CONNECT_TIMEOUT * 1000 &&
738                               !SYS_server_exit)
739                  {                  {
740                            if (clock_gettime(CLOCK_REALTIME, &ts_now) == -1)
741                            {
742                                    log_error("clock_gettime() error (%d)", errno);
743                                    ret = -1;
744                                    goto cleanup;
745                            }
746    
747                          ret = ssh_connect(outbound_session);                          ret = ssh_connect(outbound_session);
748                          if (ret == SSH_OK)                          if (ret == SSH_OK)
749                          {                          {
# Line 710  static int bbsnet_connect(int n) Line 759  static int bbsnet_connect(int n)
759                                  ret = -1;                                  ret = -1;
760                                  goto cleanup;                                  goto cleanup;
761                          }                          }
762    
763                            if (progress_update(&ts_begin, &ts_now,
764                                                                    REMOTE_CONNECT_TIMEOUT * 1000,
765                                                                    &progress_last, PROGRESS_BAR_LEN) < 0)
766                            {
767                                    log_error("progress_update() error");
768                                    ret = -1;
769                                    goto cleanup;
770                            }
771                  }                  }
772                  if (ret != SSH_OK)                  if (ret != SSH_OK)
773                  {                  {
774                            progress_bar(100, PROGRESS_BAR_LEN);
775                          prints("\033[1;31m连接超时!\033[m\r\n");                          prints("\033[1;31m连接超时!\033[m\r\n");
776                          press_any_key();                          press_any_key();
777                          ret = -1;                          ret = -1;
# Line 745  static int bbsnet_connect(int n) Line 804  static int bbsnet_connect(int n)
804                  }                  }
805    
806                  ret = SSH_AUTH_ERROR;                  ret = SSH_AUTH_ERROR;
807                  while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT)                  while ((ts_now.tv_sec - ts_begin.tv_sec) * 1000 +
808                                               (ts_now.tv_nsec - ts_begin.tv_nsec) / 1000 / 1000 <
809                                       REMOTE_CONNECT_TIMEOUT * 1000 &&
810                               !SYS_server_exit)
811                  {                  {
812                            if (clock_gettime(CLOCK_REALTIME, &ts_now) == -1)
813                            {
814                                    log_error("clock_gettime() error (%d)", errno);
815                                    ret = -1;
816                                    goto cleanup;
817                            }
818    
819                          ret = ssh_userauth_password(outbound_session, NULL, remote_pass);                          ret = ssh_userauth_password(outbound_session, NULL, remote_pass);
820                          if (ret == SSH_AUTH_SUCCESS)                          if (ret == SSH_AUTH_SUCCESS)
821                          {                          {
# Line 764  static int bbsnet_connect(int n) Line 833  static int bbsnet_connect(int n)
833                          }                          }
834                          else // if (ret == SSH_AUTH_DENIED)                          else // if (ret == SSH_AUTH_DENIED)
835                          {                          {
836                                  log_error("ssh_userauth_password() error: SSH_AUTH_DENIED");                                  log_debug("ssh_userauth_password() error: SSH_AUTH_DENIED");
837                                  prints("\033[1;31m身份验证失败!\033[m\r\n");                                  prints("\033[1;31m身份验证失败!\033[m\r\n");
838                                  press_any_key();                                  press_any_key();
839                                    ret = 0;
840                                    goto cleanup;
841                            }
842    
843                            if (progress_update(&ts_begin, &ts_now,
844                                                                    REMOTE_CONNECT_TIMEOUT * 1000,
845                                                                    &progress_last, PROGRESS_BAR_LEN) < 0)
846                            {
847                                    log_error("progress_update() error");
848                                  ret = -1;                                  ret = -1;
849                                  goto cleanup;                                  goto cleanup;
850                          }                          }
851                  }                  }
852                  if (ret != SSH_AUTH_SUCCESS)                  if (ret != SSH_AUTH_SUCCESS)
853                  {                  {
854                            progress_bar(100, PROGRESS_BAR_LEN);
855                          prints("\033[1;31m连接超时!\033[m\r\n");                          prints("\033[1;31m连接超时!\033[m\r\n");
856                          press_any_key();                          press_any_key();
857                          ret = -1;                          ret = -1;
# Line 788  static int bbsnet_connect(int n) Line 867  static int bbsnet_connect(int n)
867                  }                  }
868    
869                  ret = SSH_ERROR;                  ret = SSH_ERROR;
870                  while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT)                  while ((ts_now.tv_sec - ts_begin.tv_sec) * 1000 +
871                                               (ts_now.tv_nsec - ts_begin.tv_nsec) / 1000 / 1000 <
872                                       REMOTE_CONNECT_TIMEOUT * 1000 &&
873                               !SYS_server_exit)
874                  {                  {
875                            if (clock_gettime(CLOCK_REALTIME, &ts_now) == -1)
876                            {
877                                    log_error("clock_gettime() error (%d)", errno);
878                                    ret = -1;
879                                    goto cleanup;
880                            }
881    
882                          ret = ssh_channel_open_session(outbound_channel);                          ret = ssh_channel_open_session(outbound_channel);
883                          if (ret == SSH_OK)                          if (ret == SSH_OK)
884                          {                          {
# Line 805  static int bbsnet_connect(int n) Line 894  static int bbsnet_connect(int n)
894                                  ret = -1;                                  ret = -1;
895                                  goto cleanup;                                  goto cleanup;
896                          }                          }
897    
898                            if (progress_update(&ts_begin, &ts_now,
899                                                                    REMOTE_CONNECT_TIMEOUT * 1000,
900                                                                    &progress_last, PROGRESS_BAR_LEN) < 0)
901                            {
902                                    log_error("progress_update() error");
903                                    ret = -1;
904                                    goto cleanup;
905                            }
906                  }                  }
907                  if (ret != SSH_OK)                  if (ret != SSH_OK)
908                  {                  {
909                            progress_bar(100, PROGRESS_BAR_LEN);
910                          prints("\033[1;31m连接超时!\033[m\r\n");                          prints("\033[1;31m连接超时!\033[m\r\n");
911                          press_any_key();                          press_any_key();
912                          ret = -1;                          ret = -1;
# Line 815  static int bbsnet_connect(int n) Line 914  static int bbsnet_connect(int n)
914                  }                  }
915    
916                  ret = SSH_ERROR;                  ret = SSH_ERROR;
917                  while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT)                  while ((ts_now.tv_sec - ts_begin.tv_sec) * 1000 +
918                                               (ts_now.tv_nsec - ts_begin.tv_nsec) / 1000 / 1000 <
919                                       REMOTE_CONNECT_TIMEOUT * 1000 &&
920                               !SYS_server_exit)
921                  {                  {
922                            if (clock_gettime(CLOCK_REALTIME, &ts_now) == -1)
923                            {
924                                    log_error("clock_gettime() error (%d)", errno);
925                                    ret = -1;
926                                    goto cleanup;
927                            }
928    
929                          ret = ssh_channel_request_pty(outbound_channel);                          ret = ssh_channel_request_pty(outbound_channel);
930                          if (ret == SSH_OK)                          if (ret == SSH_OK)
931                          {                          {
# Line 832  static int bbsnet_connect(int n) Line 941  static int bbsnet_connect(int n)
941                                  ret = -1;                                  ret = -1;
942                                  goto cleanup;                                  goto cleanup;
943                          }                          }
944    
945                            if (progress_update(&ts_begin, &ts_now,
946                                                                    REMOTE_CONNECT_TIMEOUT * 1000,
947                                                                    &progress_last, PROGRESS_BAR_LEN) < 0)
948                            {
949                                    log_error("progress_update() error");
950                                    ret = -1;
951                                    goto cleanup;
952                            }
953                  }                  }
954                  if (ret != SSH_OK)                  if (ret != SSH_OK)
955                  {                  {
956                            progress_bar(100, PROGRESS_BAR_LEN);
957                          prints("\033[1;31m连接超时!\033[m\r\n");                          prints("\033[1;31m连接超时!\033[m\r\n");
958                          press_any_key();                          press_any_key();
959                          ret = -1;                          ret = -1;
# Line 842  static int bbsnet_connect(int n) Line 961  static int bbsnet_connect(int n)
961                  }                  }
962    
963                  ret = SSH_ERROR;                  ret = SSH_ERROR;
964                  while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT)                  while ((ts_now.tv_sec - ts_begin.tv_sec) * 1000 +
965                                               (ts_now.tv_nsec - ts_begin.tv_nsec) / 1000 / 1000 <
966                                       REMOTE_CONNECT_TIMEOUT * 1000 &&
967                               !SYS_server_exit)
968                  {                  {
969                            if (clock_gettime(CLOCK_REALTIME, &ts_now) == -1)
970                            {
971                                    log_error("clock_gettime() error (%d)", errno);
972                                    ret = -1;
973                                    goto cleanup;
974                            }
975    
976                          ret = ssh_channel_request_shell(outbound_channel);                          ret = ssh_channel_request_shell(outbound_channel);
977                          if (ret == SSH_OK)                          if (ret == SSH_OK)
978                          {                          {
# Line 859  static int bbsnet_connect(int n) Line 988  static int bbsnet_connect(int n)
988                                  ret = -1;                                  ret = -1;
989                                  goto cleanup;                                  goto cleanup;
990                          }                          }
991    
992                            if (progress_update(&ts_begin, &ts_now,
993                                                                    REMOTE_CONNECT_TIMEOUT * 1000,
994                                                                    &progress_last, PROGRESS_BAR_LEN) < 0)
995                            {
996                                    log_error("progress_update() error");
997                                    ret = -1;
998                                    goto cleanup;
999                            }
1000                  }                  }
1001                  if (ret != SSH_OK)                  if (ret != SSH_OK)
1002                  {                  {
1003                            progress_bar(100, PROGRESS_BAR_LEN);
1004                          prints("\033[1;31m连接超时!\033[m\r\n");                          prints("\033[1;31m连接超时!\033[m\r\n");
1005                          press_any_key();                          press_any_key();
1006                          ret = -1;                          ret = -1;


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

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