/[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.103 by sysadm, Thu Dec 18 11:18:29 2025 UTC Revision 1.105 by sysadm, Sat Dec 20 03:24:01 2025 UTC
# Line 87  static int load_bbsnet_conf(const char * Line 87  static int load_bbsnet_conf(const char *
87          bbsnet_menu.p_menu_pool = calloc(1, sizeof(MENU));          bbsnet_menu.p_menu_pool = calloc(1, sizeof(MENU));
88          if (bbsnet_menu.p_menu_pool == NULL)          if (bbsnet_menu.p_menu_pool == NULL)
89          {          {
90                  log_error("calloc(p_menu_pool) error\n");                  log_error("calloc(p_menu_pool) error");
91                  return -1;                  return -1;
92          }          }
93          bbsnet_menu.menu_count = 1;          bbsnet_menu.menu_count = 1;
# Line 95  static int load_bbsnet_conf(const char * Line 95  static int load_bbsnet_conf(const char *
95          bbsnet_menu.p_menu_item_pool = calloc(MAXSTATION, sizeof(MENU_ITEM));          bbsnet_menu.p_menu_item_pool = calloc(MAXSTATION, sizeof(MENU_ITEM));
96          if (bbsnet_menu.p_menu_item_pool == NULL)          if (bbsnet_menu.p_menu_item_pool == NULL)
97          {          {
98                  log_error("calloc(p_menu_item_pool) error\n");                  log_error("calloc(p_menu_item_pool) error");
99                  unload_bbsnet_conf();                  unload_bbsnet_conf();
100                  return -1;                  return -1;
101          }          }
# Line 140  static int load_bbsnet_conf(const char * Line 140  static int load_bbsnet_conf(const char *
140                  port = strtol(t4, &endptr, 10);                  port = strtol(t4, &endptr, 10);
141                  if (*endptr != '\0' || port <= 0 || port > 65535)                  if (*endptr != '\0' || port <= 0 || port > 65535)
142                  {                  {
143                          log_error("Invalid port value %ld of menu item %d\n", port, menu_item_id);                          log_error("Invalid port value %ld of menu item %d", port, menu_item_id);
144                          fclose(fp);                          fclose(fp);
145                          unload_bbsnet_conf();                          unload_bbsnet_conf();
146                          return -3;                          return -3;
# Line 154  static int load_bbsnet_conf(const char * Line 154  static int load_bbsnet_conf(const char *
154                  p_menu_item = get_menu_item_by_id(&bbsnet_menu, menu_item_id);                  p_menu_item = get_menu_item_by_id(&bbsnet_menu, menu_item_id);
155                  if (p_menu_item == NULL)                  if (p_menu_item == NULL)
156                  {                  {
157                          log_error("get_menu_item_by_id(%d) return NULL pointer\n", menu_item_id);                          log_error("get_menu_item_by_id(%d) return NULL pointer", menu_item_id);
158                          fclose(fp);                          fclose(fp);
159                          unload_bbsnet_conf();                          unload_bbsnet_conf();
160                          return -3;                          return -3;
# 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 310  static int bbsnet_connect(int n) Line 343  static int bbsnet_connect(int n)
343    
344          if (user_online_update("BBS_NET") < 0)          if (user_online_update("BBS_NET") < 0)
345          {          {
346                  log_error("user_online_update(BBS_NET) error\n");                  log_error("user_online_update(BBS_NET) error");
347          }          }
348    
349          if (bbsnet_conf[n].use_ssh)          if (bbsnet_conf[n].use_ssh)
# 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 366  static int bbsnet_connect(int n) Line 408  static int bbsnet_connect(int n)
408    
409          if ((ret = getaddrinfo(BBS_address, NULL, &hints, &res)) != 0)          if ((ret = getaddrinfo(BBS_address, NULL, &hints, &res)) != 0)
410          {          {
411                  log_error("getaddrinfo() error (%d): %s\n", ret, gai_strerror(ret));                  log_error("getaddrinfo() error (%d): %s", ret, gai_strerror(ret));
412                  ret = -1;                  ret = -1;
413                  goto cleanup;                  goto cleanup;
414          }          }
415    
416          if (inet_ntop(AF_INET, &(((struct sockaddr_in *)res->ai_addr)->sin_addr), local_addr, sizeof(local_addr)) == NULL)          if (inet_ntop(AF_INET, &(((struct sockaddr_in *)res->ai_addr)->sin_addr), local_addr, sizeof(local_addr)) == NULL)
417          {          {
418                  log_error("inet_ntop() error (%d)\n", errno);                  log_error("inet_ntop() error (%d)", errno);
419                  ret = -1;                  ret = -1;
420                  goto cleanup;                  goto cleanup;
421          }          }
# Line 382  static int bbsnet_connect(int n) Line 424  static int bbsnet_connect(int n)
424          sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);          sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
425          if (sock < 0)          if (sock < 0)
426          {          {
427                  log_error("socket() error (%d)\n", errno);                  log_error("socket() error (%d)", errno);
428                  ret = -1;                  ret = -1;
429                  goto cleanup;                  goto cleanup;
430          }          }
431    
432          if (bind(sock, res->ai_addr, res->ai_addrlen) < 0)          if (bind(sock, res->ai_addr, res->ai_addrlen) < 0)
433          {          {
434                  log_error("bind(%s:%u) error (%d)\n", local_addr, local_port, errno);                  log_error("bind(%s:%u) error (%d)", local_addr, local_port, errno);
435                  ret = -1;                  ret = -1;
436                  goto cleanup;                  goto cleanup;
437          }          }
# Line 405  static int bbsnet_connect(int n) Line 447  static int bbsnet_connect(int n)
447    
448          if ((ret = getaddrinfo(bbsnet_conf[n].host_name, bbsnet_conf[n].port, &hints, &res)) != 0)          if ((ret = getaddrinfo(bbsnet_conf[n].host_name, bbsnet_conf[n].port, &hints, &res)) != 0)
449          {          {
450                  log_error("getaddrinfo() error (%d): %s\n", ret, gai_strerror(ret));                  log_error("getaddrinfo() error (%d): %s", ret, gai_strerror(ret));
451                  prints("\033[1;31m查找主机名失败!\033[m\r\n");                  prints("\033[1;31m查找主机名失败!\033[m\r\n");
452                  press_any_key();                  press_any_key();
453                  ret = -1;                  ret = -1;
# Line 414  static int bbsnet_connect(int n) Line 456  static int bbsnet_connect(int n)
456    
457          if (inet_ntop(AF_INET, &(((struct sockaddr_in *)res->ai_addr)->sin_addr), remote_addr, sizeof(remote_addr)) == NULL)          if (inet_ntop(AF_INET, &(((struct sockaddr_in *)res->ai_addr)->sin_addr), remote_addr, sizeof(remote_addr)) == NULL)
458          {          {
459                  log_error("inet_ntop() error (%d)\n", errno);                  log_error("inet_ntop() error (%d)", errno);
460                  ret = -1;                  ret = -1;
461                  goto cleanup;                  goto cleanup;
462          }          }
# Line 423  static int bbsnet_connect(int n) Line 465  static int bbsnet_connect(int n)
465          // Set socket as non-blocking          // Set socket as non-blocking
466          if ((flags_sock = fcntl(sock, F_GETFL, 0)) == -1)          if ((flags_sock = fcntl(sock, F_GETFL, 0)) == -1)
467          {          {
468                  log_error("fcntl(F_GETFL) error (%d)\n", errno);                  log_error("fcntl(F_GETFL) error (%d)", errno);
469                  ret = -1;                  ret = -1;
470                  goto cleanup;                  goto cleanup;
471          }          }
472          if ((fcntl(sock, F_SETFL, flags_sock | O_NONBLOCK)) == -1)          if ((fcntl(sock, F_SETFL, flags_sock | O_NONBLOCK)) == -1)
473          {          {
474                  log_error("fcntl(F_SETFL) error (%d)\n", errno);                  log_error("fcntl(F_SETFL) error (%d)", errno);
475                  ret = -1;                  ret = -1;
476                  goto cleanup;                  goto cleanup;
477          }          }
# Line 437  static int bbsnet_connect(int n) Line 479  static int bbsnet_connect(int n)
479          // Set STDIN/STDOUT as non-blocking          // Set STDIN/STDOUT as non-blocking
480          if ((flags_stdin = fcntl(STDIN_FILENO, F_GETFL, 0)) == -1)          if ((flags_stdin = fcntl(STDIN_FILENO, F_GETFL, 0)) == -1)
481          {          {
482                  log_error("fcntl(F_GETFL) error (%d)\n", errno);                  log_error("fcntl(F_GETFL) error (%d)", errno);
483                  ret = -1;                  ret = -1;
484                  goto cleanup;                  goto cleanup;
485          }          }
486          if ((flags_stdout = fcntl(STDOUT_FILENO, F_GETFL, 0)) == -1)          if ((flags_stdout = fcntl(STDOUT_FILENO, F_GETFL, 0)) == -1)
487          {          {
488                  log_error("fcntl(F_GETFL) error (%d)\n", errno);                  log_error("fcntl(F_GETFL) error (%d)", errno);
489                  ret = -1;                  ret = -1;
490                  goto cleanup;                  goto cleanup;
491          }          }
492          if ((fcntl(STDIN_FILENO, F_SETFL, flags_stdin | O_NONBLOCK)) == -1)          if ((fcntl(STDIN_FILENO, F_SETFL, flags_stdin | O_NONBLOCK)) == -1)
493          {          {
494                  log_error("fcntl(F_SETFL) error (%d)\n", errno);                  log_error("fcntl(F_SETFL) error (%d)", errno);
495                  ret = -1;                  ret = -1;
496                  goto cleanup;                  goto cleanup;
497          }          }
498          if ((fcntl(STDOUT_FILENO, F_SETFL, flags_stdout | O_NONBLOCK)) == -1)          if ((fcntl(STDOUT_FILENO, F_SETFL, flags_stdout | O_NONBLOCK)) == -1)
499          {          {
500                  log_error("fcntl(F_SETFL) error (%d)\n", errno);                  log_error("fcntl(F_SETFL) error (%d)", errno);
501                  ret = -1;                  ret = -1;
502                  goto cleanup;                  goto cleanup;
503          }          }
# Line 464  static int bbsnet_connect(int n) Line 506  static int bbsnet_connect(int n)
506          epollfd = epoll_create1(0);          epollfd = epoll_create1(0);
507          if (epollfd < 0)          if (epollfd < 0)
508          {          {
509                  log_error("epoll_create1() error (%d)\n", errno);                  log_error("epoll_create1() error (%d)", errno);
510                  ret = -1;                  ret = -1;
511                  goto cleanup;                  goto cleanup;
512          }          }
# Line 473  static int bbsnet_connect(int n) Line 515  static int bbsnet_connect(int n)
515          ev.data.fd = sock;          ev.data.fd = sock;
516          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sock, &ev) == -1)          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sock, &ev) == -1)
517          {          {
518                  log_error("epoll_ctl(socket) error (%d)\n", errno);                  log_error("epoll_ctl(socket) error (%d)", errno);
519                  ret = -1;                  ret = -1;
520                  goto cleanup;                  goto cleanup;
521          }          }
# Line 482  static int bbsnet_connect(int n) Line 524  static int bbsnet_connect(int n)
524          ev.data.fd = STDIN_FILENO;          ev.data.fd = STDIN_FILENO;
525          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1)          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1)
526          {          {
527                  log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno);                  log_error("epoll_ctl(STDIN_FILENO) error (%d)", errno);
528                  ret = -1;                  ret = -1;
529                  goto cleanup;                  goto cleanup;
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                          {                          {
564                                  log_error("connect(socket) error (%d)\n", errno);                                  log_error("connect(socket) error (%d)", errno);
565                                  prints("\033[1;31m连接失败!\033[m\r\n");                                  prints("\033[1;31m连接失败!\033[m\r\n");
566                                  press_any_key();                                  press_any_key();
567                                  ret = -1;                                  ret = -1;
568                                  goto cleanup;                                  goto cleanup;
569                          }                          }
                 }  
         }  
   
         progress = progress_last = 0;  
         prints("\033[1;32m连接进行中,按\033[1;33mCtrl+C\033[1;32m中断。\033[m\r\n");  
         progress_bar(0, PROGRESS_BAR_LEN);  
570    
571          if (clock_gettime(CLOCK_REALTIME, &ts_begin) == -1)                          if (progress_update(&ts_begin, &ts_now,
572          {                                                                  REMOTE_CONNECT_TIMEOUT * 1000,
573                  log_error("clock_gettime() error (%d)\n", errno);                                                                  &progress_last, PROGRESS_BAR_LEN) < 0)
574                  ret = -1;                          {
575                  goto cleanup;                                  log_error("progress_update() error");
576                                    ret = -1;
577                                    goto cleanup;
578                            }
579                    }
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 553  static int bbsnet_connect(int n) Line 608  static int bbsnet_connect(int n)
608                          if (errno != EINTR)                          if (errno != EINTR)
609                          {                          {
610  #ifdef HAVE_SYS_EPOLL_H  #ifdef HAVE_SYS_EPOLL_H
611                                  log_error("epoll_wait() error (%d)\n", errno);                                  log_error("epoll_wait() error (%d)", errno);
612  #else  #else
613                                  log_error("poll() error (%d)\n", errno);                                  log_error("poll() error (%d)", errno);
614  #endif  #endif
615                                  break;                                  break;
616                          }                          }
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)\n", 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 601  static int bbsnet_connect(int n) Line 631  static int bbsnet_connect(int n)
631                                          socklen_t len = sizeof(error);                                          socklen_t len = sizeof(error);
632                                          if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len) < 0)                                          if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
633                                          {                                          {
634                                                  log_error("getsockopt() error (%d) !\n", errno);                                                  log_error("getsockopt() error (%d) !", errno);
635                                                  ret = -1;                                                  ret = -1;
636                                                  goto cleanup;                                                  goto cleanup;
637                                          }                                          }
# 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 647  static int bbsnet_connect(int n) Line 686  static int bbsnet_connect(int n)
686          tos = IPTOS_LOWDELAY;          tos = IPTOS_LOWDELAY;
687          if (setsockopt(sock, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)          if (setsockopt(sock, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)
688          {          {
689                  log_error("setsockopt IP_TOS=%d error (%d)\n", tos, errno);                  log_error("setsockopt IP_TOS=%d error (%d)", tos, errno);
690          }          }
691    
692          sock_len = sizeof(sin);          sock_len = sizeof(sin);
# Line 660  static int bbsnet_connect(int n) Line 699  static int bbsnet_connect(int n)
699    
700          if (inet_ntop(AF_INET, &(sin.sin_addr), local_addr, sizeof(local_addr)) == NULL)          if (inet_ntop(AF_INET, &(sin.sin_addr), local_addr, sizeof(local_addr)) == NULL)
701          {          {
702                  log_error("inet_ntop() error (%d)\n", errno);                  log_error("inet_ntop() error (%d)", errno);
703                  ret = -1;                  ret = -1;
704                  goto cleanup;                  goto cleanup;
705          }          }
# Line 671  static int bbsnet_connect(int n) Line 710  static int bbsnet_connect(int n)
710                  outbound_session = ssh_new();                  outbound_session = ssh_new();
711                  if (outbound_session == NULL)                  if (outbound_session == NULL)
712                  {                  {
713                          log_error("ssh_new() error\n");                          log_error("ssh_new() error");
714                          ret = -1;                          ret = -1;
715                          goto cleanup;                          goto cleanup;
716                  }                  }
# Line 684  static int bbsnet_connect(int n) Line 723  static int bbsnet_connect(int n)
723                          ssh_options_set(outbound_session, SSH_OPTIONS_HOSTKEYS, "+ssh-ed25519,ecdsa-sha2-nistp256,ssh-rsa") < 0 ||                          ssh_options_set(outbound_session, SSH_OPTIONS_HOSTKEYS, "+ssh-ed25519,ecdsa-sha2-nistp256,ssh-rsa") < 0 ||
724                          ssh_options_set(outbound_session, SSH_OPTIONS_LOG_VERBOSITY, &ssh_log_level) < 0)                          ssh_options_set(outbound_session, SSH_OPTIONS_LOG_VERBOSITY, &ssh_log_level) < 0)
725                  {                  {
726                          log_error("Error setting SSH options: %s\n", ssh_get_error(outbound_session));                          log_error("Error setting SSH options: %s", ssh_get_error(outbound_session));
727                          ret = -1;                          ret = -1;
728                          goto cleanup;                          goto cleanup;
729                  }                  }
# 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 702  static int bbsnet_connect(int n) Line 751  static int bbsnet_connect(int n)
751                          }                          }
752                          else if (ret == SSH_AGAIN)                          else if (ret == SSH_AGAIN)
753                          {                          {
754                                  // log_debug("ssh_connect() error: SSH_AGAIN\n");                                  // log_debug("ssh_connect() error: SSH_AGAIN");
755                          }                          }
756                          else // if (ret == SSH_ERROR)                          else // if (ret == SSH_ERROR)
757                          {                          {
758                                  log_error("ssh_connect() error: SSH_ERROR\n");                                  log_error("ssh_connect() error: SSH_ERROR");
759                                    ret = -1;
760                                    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;                                  ret = -1;
769                                  goto cleanup;                                  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 726  static int bbsnet_connect(int n) Line 785  static int bbsnet_connect(int n)
785                  case SSH_KNOWN_HOSTS_UNKNOWN:                  case SSH_KNOWN_HOSTS_UNKNOWN:
786                          if (ssh_session_update_known_hosts(outbound_session) != SSH_OK)                          if (ssh_session_update_known_hosts(outbound_session) != SSH_OK)
787                          {                          {
788                                  log_error("ssh_session_update_known_hosts(%s) error\n", bbsnet_conf[n].host_name);                                  log_error("ssh_session_update_known_hosts(%s) error", bbsnet_conf[n].host_name);
789                                  prints("\033[1;31m无法添加服务器证书\033[m\r\n");                                  prints("\033[1;31m无法添加服务器证书\033[m\r\n");
790                                  press_any_key();                                  press_any_key();
791                                  ret = -1;                                  ret = -1;
792                                  goto cleanup;                                  goto cleanup;
793                          }                          }
794                          log_common("SSH key of (%s) is added into %s\n", bbsnet_conf[n].host_name, SSH_KNOWN_HOSTS_FILE);                          log_common("SSH key of (%s) is added into %s", bbsnet_conf[n].host_name, SSH_KNOWN_HOSTS_FILE);
795                  case SSH_KNOWN_HOSTS_OK:                  case SSH_KNOWN_HOSTS_OK:
796                          break;                          break;
797                  case SSH_KNOWN_HOSTS_CHANGED:                  case SSH_KNOWN_HOSTS_CHANGED:
798                  case SSH_KNOWN_HOSTS_OTHER:                  case SSH_KNOWN_HOSTS_OTHER:
799                          log_error("ssh_session_is_known_server(%s) error: %d\n", bbsnet_conf[n].host_name, ret);                          log_error("ssh_session_is_known_server(%s) error: %d", bbsnet_conf[n].host_name, ret);
800                          prints("\033[1;31m服务器证书已变更\033[m\r\n");                          prints("\033[1;31m服务器证书已变更\033[m\r\n");
801                          press_any_key();                          press_any_key();
802                          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 754  static int bbsnet_connect(int n) Line 823  static int bbsnet_connect(int n)
823                          }                          }
824                          else if (ret == SSH_AUTH_AGAIN)                          else if (ret == SSH_AUTH_AGAIN)
825                          {                          {
826                                  // log_debug("ssh_userauth_password() error: SSH_AUTH_AGAIN\n");                                  // log_debug("ssh_userauth_password() error: SSH_AUTH_AGAIN");
827                          }                          }
828                          else if (ret == SSH_AUTH_ERROR)                          else if (ret == SSH_AUTH_ERROR)
829                          {                          {
830                                  log_error("ssh_userauth_password() error: SSH_AUTH_ERROR\n");                                  log_error("ssh_userauth_password() error: SSH_AUTH_ERROR");
831                                  ret = -1;                                  ret = -1;
832                                  goto cleanup;                                  goto cleanup;
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\n");                                  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 782  static int bbsnet_connect(int n) Line 861  static int bbsnet_connect(int n)
861                  outbound_channel = ssh_channel_new(outbound_session);                  outbound_channel = ssh_channel_new(outbound_session);
862                  if (outbound_channel == NULL)                  if (outbound_channel == NULL)
863                  {                  {
864                          log_error("ssh_channel_new() error\n");                          log_error("ssh_channel_new() error");
865                          ret = -1;                          ret = -1;
866                          goto cleanup;                          goto cleanup;
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 797  static int bbsnet_connect(int n) Line 886  static int bbsnet_connect(int n)
886                          }                          }
887                          else if (ret == SSH_AGAIN)                          else if (ret == SSH_AGAIN)
888                          {                          {
889                                  // log_debug("ssh_channel_open_session() error: SSH_AGAIN\n");                                  // log_debug("ssh_channel_open_session() error: SSH_AGAIN");
890                          }                          }
891                          else // if (ret == SSH_ERROR)                          else // if (ret == SSH_ERROR)
892                          {                          {
893                                  log_error("ssh_channel_open_session() error: SSH_ERROR\n");                                  log_error("ssh_channel_open_session() error: SSH_ERROR");
894                                    ret = -1;
895                                    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;                                  ret = -1;
904                                  goto cleanup;                                  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 824  static int bbsnet_connect(int n) Line 933  static int bbsnet_connect(int n)
933                          }                          }
934                          else if (ret == SSH_AGAIN)                          else if (ret == SSH_AGAIN)
935                          {                          {
936                                  // log_debug("ssh_channel_request_pty() error: SSH_AGAIN\n");                                  // log_debug("ssh_channel_request_pty() error: SSH_AGAIN");
937                          }                          }
938                          else // if (ret == SSH_ERROR)                          else // if (ret == SSH_ERROR)
939                          {                          {
940                                  log_error("ssh_channel_request_pty() error: SSH_ERROR\n");                                  log_error("ssh_channel_request_pty() error: SSH_ERROR");
941                                    ret = -1;
942                                    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;                                  ret = -1;
951                                  goto cleanup;                                  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 851  static int bbsnet_connect(int n) Line 980  static int bbsnet_connect(int n)
980                          }                          }
981                          else if (ret == SSH_AGAIN)                          else if (ret == SSH_AGAIN)
982                          {                          {
983                                  // log_debug("ssh_channel_request_shell() error: SSH_AGAIN\n");                                  // log_debug("ssh_channel_request_shell() error: SSH_AGAIN");
984                          }                          }
985                          else // if (ret == SSH_ERROR)                          else // if (ret == SSH_ERROR)
986                          {                          {
987                                  log_error("ssh_channel_request_shell() error: SSH_ERROR\n");                                  log_error("ssh_channel_request_shell() error: SSH_ERROR");
988                                    ret = -1;
989                                    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;                                  ret = -1;
998                                  goto cleanup;                                  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;
# Line 871  static int bbsnet_connect(int n) Line 1010  static int bbsnet_connect(int n)
1010    
1011          prints("\033[1;31m连接成功!\033[m\r\n");          prints("\033[1;31m连接成功!\033[m\r\n");
1012          iflush();          iflush();
1013          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]",
1014                             remote_addr, remote_port, local_addr, local_port, BBS_username);                             remote_addr, remote_port, local_addr, local_port, BBS_username);
1015    
1016          snprintf(tocode, sizeof(tocode), "%s%s", bbsnet_conf[n].charset,          snprintf(tocode, sizeof(tocode), "%s%s", bbsnet_conf[n].charset,
# Line 879  static int bbsnet_connect(int n) Line 1018  static int bbsnet_connect(int n)
1018          input_cd = iconv_open(tocode, stdio_charset);          input_cd = iconv_open(tocode, stdio_charset);
1019          if (input_cd == (iconv_t)(-1))          if (input_cd == (iconv_t)(-1))
1020          {          {
1021                  log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, tocode, errno);                  log_error("iconv_open(%s->%s) error: %d", stdio_charset, tocode, errno);
1022                  ret = -1;                  ret = -1;
1023                  goto cleanup;                  goto cleanup;
1024          }          }
# Line 889  static int bbsnet_connect(int n) Line 1028  static int bbsnet_connect(int n)
1028          output_cd = iconv_open(tocode, bbsnet_conf[n].charset);          output_cd = iconv_open(tocode, bbsnet_conf[n].charset);
1029          if (output_cd == (iconv_t)(-1))          if (output_cd == (iconv_t)(-1))
1030          {          {
1031                  log_error("iconv_open(%s->%s) error: %d\n", bbsnet_conf[n].charset, tocode, errno);                  log_error("iconv_open(%s->%s) error: %d", bbsnet_conf[n].charset, tocode, errno);
1032                  ret = -1;                  ret = -1;
1033                  goto cleanup;                  goto cleanup;
1034          }          }
# Line 899  static int bbsnet_connect(int n) Line 1038  static int bbsnet_connect(int n)
1038          ev.data.fd = sock;          ev.data.fd = sock;
1039          if (epoll_ctl(epollfd, EPOLL_CTL_MOD, sock, &ev) == -1)          if (epoll_ctl(epollfd, EPOLL_CTL_MOD, sock, &ev) == -1)
1040          {          {
1041                  log_error("epoll_ctl(socket) error (%d)\n", errno);                  log_error("epoll_ctl(socket) error (%d)", errno);
1042                  ret = -1;                  ret = -1;
1043                  goto cleanup;                  goto cleanup;
1044          }          }
# Line 908  static int bbsnet_connect(int n) Line 1047  static int bbsnet_connect(int n)
1047          ev.data.fd = STDOUT_FILENO;          ev.data.fd = STDOUT_FILENO;
1048          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1)          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1)
1049          {          {
1050                  log_error("epoll_ctl(STDOUT_FILENO) error (%d)\n", errno);                  log_error("epoll_ctl(STDOUT_FILENO) error (%d)", errno);
1051                  ret = -1;                  ret = -1;
1052                  goto cleanup;                  goto cleanup;
1053          }          }
# Line 921  static int bbsnet_connect(int n) Line 1060  static int bbsnet_connect(int n)
1060          {          {
1061                  if (SSH_v2 && ssh_channel_is_closed(SSH_channel))                  if (SSH_v2 && ssh_channel_is_closed(SSH_channel))
1062                  {                  {
1063                          log_debug("SSH channel is closed\n");                          log_debug("SSH channel is closed");
1064                          loop = 0;                          loop = 0;
1065                          break;                          break;
1066                  }                  }
1067    
1068                  if (bbsnet_conf[n].use_ssh && ssh_channel_is_closed(outbound_channel))                  if (bbsnet_conf[n].use_ssh && ssh_channel_is_closed(outbound_channel))
1069                  {                  {
1070                          log_debug("Outbound channel is closed\n");                          log_debug("Outbound channel is closed");
1071                          loop = 0;                          loop = 0;
1072                          break;                          break;
1073                  }                  }
# Line 952  static int bbsnet_connect(int n) Line 1091  static int bbsnet_connect(int n)
1091                          if (errno != EINTR)                          if (errno != EINTR)
1092                          {                          {
1093  #ifdef HAVE_SYS_EPOLL_H  #ifdef HAVE_SYS_EPOLL_H
1094                                  log_error("epoll_wait() error (%d)\n", errno);                                  log_error("epoll_wait() error (%d)", errno);
1095  #else  #else
1096                                  log_error("poll() error (%d)\n", errno);                                  log_error("poll() error (%d)", errno);
1097  #endif  #endif
1098                                  break;                                  break;
1099                          }                          }
# Line 964  static int bbsnet_connect(int n) Line 1103  static int bbsnet_connect(int n)
1103                  {                  {
1104                          if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time)                          if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time)
1105                          {                          {
1106                                  log_debug("User input timeout\n");                                  log_debug("User input timeout");
1107                                  break;                                  break;
1108                          }                          }
1109                  }                  }
# Line 978  static int bbsnet_connect(int n) Line 1117  static int bbsnet_connect(int n)
1117  #endif  #endif
1118                          {                          {
1119  #ifdef HAVE_SYS_EPOLL_H  #ifdef HAVE_SYS_EPOLL_H
1120                                  log_debug("FD (%d) error events (%d)\n", events[i].data.fd, events[i].events);                                  log_debug("FD (%d) error events (%d)", events[i].data.fd, events[i].events);
1121  #else  #else
1122                                  log_debug("FD (%d) error events (%d)\n", pfds[i].fd, pfds[i].revents);                                  log_debug("FD (%d) error events (%d)", pfds[i].fd, pfds[i].revents);
1123  #endif  #endif
1124                                  loop = 0;                                  loop = 0;
1125                                  break;                                  break;
# Line 1039  static int bbsnet_connect(int n) Line 1178  static int bbsnet_connect(int n)
1178                                          ret = ssh_channel_read_nonblocking(SSH_channel, input_buf + input_buf_len, sizeof(input_buf) - (uint32_t)input_buf_len, 0);                                          ret = ssh_channel_read_nonblocking(SSH_channel, input_buf + input_buf_len, sizeof(input_buf) - (uint32_t)input_buf_len, 0);
1179                                          if (ret == SSH_ERROR)                                          if (ret == SSH_ERROR)
1180                                          {                                          {
1181                                                  log_debug("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session));                                                  log_debug("ssh_channel_read_nonblocking() error: %s", ssh_get_error(SSH_session));
1182                                                  loop = 0;                                                  loop = 0;
1183                                                  break;                                                  break;
1184                                          }                                          }
# Line 1077  static int bbsnet_connect(int n) Line 1216  static int bbsnet_connect(int n)
1216                                          }                                          }
1217                                          else                                          else
1218                                          {                                          {
1219                                                  log_error("read(STDIN) error (%d)\n", errno);                                                  log_error("read(STDIN) error (%d)", errno);
1220                                                  loop = 0;                                                  loop = 0;
1221                                                  break;                                                  break;
1222                                          }                                          }
1223                                  }                                  }
1224                                  else if (ret == 0) // broken pipe                                  else if (ret == 0) // broken pipe
1225                                  {                                  {
1226                                          log_debug("read(STDIN) EOF\n");                                          log_debug("read(STDIN) EOF");
1227                                          stdin_read_wait = 0;                                          stdin_read_wait = 0;
1228                                          loop = 0;                                          loop = 0;
1229                                          break;                                          break;
# Line 1097  static int bbsnet_connect(int n) Line 1236  static int bbsnet_connect(int n)
1236                                          // Refresh current action while user input                                          // Refresh current action while user input
1237                                          if (user_online_update("BBS_NET") < 0)                                          if (user_online_update("BBS_NET") < 0)
1238                                          {                                          {
1239                                                  log_error("user_online_update(BBS_NET) error\n");                                                  log_error("user_online_update(BBS_NET) error");
1240                                          }                                          }
1241    
1242                                          continue;                                          continue;
# Line 1113  static int bbsnet_connect(int n) Line 1252  static int bbsnet_connect(int n)
1252  #ifdef _DEBUG  #ifdef _DEBUG
1253                                  for (int j = input_buf_offset; j < input_buf_len; j++)                                  for (int j = input_buf_offset; j < input_buf_len; j++)
1254                                  {                                  {
1255                                          log_debug("input: <--[%u]\n", (input_buf[j] + 256) % 256);                                          log_debug("input: <--[%u]", (input_buf[j] + 256) % 256);
1256                                  }                                  }
1257  #endif  #endif
1258    
1259                                  ret = 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);
1260                                  if (ret < 0)                                  if (ret < 0)
1261                                  {                                  {
1262                                          log_error("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", input_buf_len, input_buf_offset, input_conv_len);
1263                                          input_buf_len = input_buf_offset; // Discard invalid sequence                                          input_buf_len = input_buf_offset; // Discard invalid sequence
1264                                  }                                  }
1265    
# Line 1128  static int bbsnet_connect(int n) Line 1267  static int bbsnet_connect(int n)
1267  #ifdef _DEBUG  #ifdef _DEBUG
1268                                  for (int j = input_conv_offset; j < input_conv_len; j++)                                  for (int j = input_conv_offset; j < input_conv_len; j++)
1269                                  {                                  {
1270                                          log_debug("input_conv: <--[%u]\n", (input_conv[j] + 256) % 256);                                          log_debug("input_conv: <--[%u]", (input_conv[j] + 256) % 256);
1271                                  }                                  }
1272  #endif  #endif
1273                          }                          }
# Line 1140  static int bbsnet_connect(int n) Line 1279  static int bbsnet_connect(int n)
1279                                          ret = ssh_channel_write(outbound_channel, input_conv + input_conv_offset, (uint32_t)(input_conv_len - input_conv_offset));                                          ret = ssh_channel_write(outbound_channel, input_conv + input_conv_offset, (uint32_t)(input_conv_len - input_conv_offset));
1280                                          if (ret == SSH_ERROR)                                          if (ret == SSH_ERROR)
1281                                          {                                          {
1282                                                  log_debug("ssh_channel_write() error: %s\n", ssh_get_error(outbound_session));                                                  log_debug("ssh_channel_write() error: %s", ssh_get_error(outbound_session));
1283                                                  loop = 0;                                                  loop = 0;
1284                                                  break;                                                  break;
1285                                          }                                          }
# Line 1162  static int bbsnet_connect(int n) Line 1301  static int bbsnet_connect(int n)
1301                                          }                                          }
1302                                          else                                          else
1303                                          {                                          {
1304                                                  log_debug("write(socket) error (%d)\n", errno);                                                  log_debug("write(socket) error (%d)", errno);
1305                                                  loop = 0;                                                  loop = 0;
1306                                                  break;                                                  break;
1307                                          }                                          }
1308                                  }                                  }
1309                                  else if (ret == 0) // broken pipe                                  else if (ret == 0) // broken pipe
1310                                  {                                  {
1311                                          log_debug("write(socket) EOF\n");                                          log_debug("write(socket) EOF");
1312                                          sock_write_wait = 0;                                          sock_write_wait = 0;
1313                                          loop = 0;                                          loop = 0;
1314                                          break;                                          break;
# Line 1198  static int bbsnet_connect(int n) Line 1337  static int bbsnet_connect(int n)
1337                                                                                                             (uint32_t)(sizeof(output_buf) - (size_t)output_buf_len), 0);                                                                                                             (uint32_t)(sizeof(output_buf) - (size_t)output_buf_len), 0);
1338                                          if (ret == SSH_ERROR)                                          if (ret == SSH_ERROR)
1339                                          {                                          {
1340                                                  log_debug("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(outbound_session));                                                  log_debug("ssh_channel_read_nonblocking() error: %s", ssh_get_error(outbound_session));
1341                                                  loop = 0;                                                  loop = 0;
1342                                                  break;                                                  break;
1343                                          }                                          }
# Line 1231  static int bbsnet_connect(int n) Line 1370  static int bbsnet_connect(int n)
1370                                          }                                          }
1371                                          else                                          else
1372                                          {                                          {
1373                                                  log_debug("read(socket) error (%d)\n", errno);                                                  log_debug("read(socket) error (%d)", errno);
1374                                                  loop = 0;                                                  loop = 0;
1375                                                  break;                                                  break;
1376                                          }                                          }
1377                                  }                                  }
1378                                  else if (ret == 0) // broken pipe                                  else if (ret == 0) // broken pipe
1379                                  {                                  {
1380                                          log_debug("read(socket) EOF\n");                                          log_debug("read(socket) EOF");
1381                                          sock_read_wait = 0;                                          sock_read_wait = 0;
1382                                          loop = 0;                                          loop = 0;
1383                                          break;                                          break;
# Line 1258  static int bbsnet_connect(int n) Line 1397  static int bbsnet_connect(int n)
1397                                  ret = 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);
1398                                  if (ret < 0)                                  if (ret < 0)
1399                                  {                                  {
1400                                          log_error("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", output_buf_len, output_buf_offset, output_conv_len);
1401                                          output_buf_len = output_buf_offset; // Discard invalid sequence                                          output_buf_len = output_buf_offset; // Discard invalid sequence
1402                                  }                                  }
1403                          }                          }
# Line 1270  static int bbsnet_connect(int n) Line 1409  static int bbsnet_connect(int n)
1409                                          ret = ssh_channel_write(SSH_channel, output_conv + output_conv_offset, (uint32_t)(output_conv_len - output_conv_offset));                                          ret = ssh_channel_write(SSH_channel, output_conv + output_conv_offset, (uint32_t)(output_conv_len - output_conv_offset));
1410                                          if (ret == SSH_ERROR)                                          if (ret == SSH_ERROR)
1411                                          {                                          {
1412                                                  log_debug("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session));                                                  log_debug("ssh_channel_write() error: %s", ssh_get_error(SSH_session));
1413                                                  loop = 0;                                                  loop = 0;
1414                                                  break;                                                  break;
1415                                          }                                          }
# Line 1292  static int bbsnet_connect(int n) Line 1431  static int bbsnet_connect(int n)
1431                                          }                                          }
1432                                          else                                          else
1433                                          {                                          {
1434                                                  log_debug("write(STDOUT) error (%d)\n", errno);                                                  log_debug("write(STDOUT) error (%d)", errno);
1435                                                  loop = 0;                                                  loop = 0;
1436                                                  break;                                                  break;
1437                                          }                                          }
1438                                  }                                  }
1439                                  else if (ret == 0) // broken pipe                                  else if (ret == 0) // broken pipe
1440                                  {                                  {
1441                                          log_debug("write(STDOUT) EOF\n");                                          log_debug("write(STDOUT) EOF");
1442                                          stdout_write_wait = 0;                                          stdout_write_wait = 0;
1443                                          loop = 0;                                          loop = 0;
1444                                          break;                                          break;
# Line 1322  static int bbsnet_connect(int n) Line 1461  static int bbsnet_connect(int n)
1461          ret = 1; // Normal disconnect          ret = 1; // Normal disconnect
1462          BBS_last_access_tm = time(NULL);          BBS_last_access_tm = time(NULL);
1463          t_used = BBS_last_access_tm - t_begin;          t_used = BBS_last_access_tm - t_begin;
1464          log_common("BBSNET disconnect, %ld days %ld hours %ld minutes %ld seconds used\n",          log_common("BBSNET disconnect, %ld days %ld hours %ld minutes %ld seconds used",
1465                             t_used / 86400, t_used % 86400 / 3600, t_used % 3600 / 60, t_used % 60);                             t_used / 86400, t_used % 86400 / 3600, t_used % 3600 / 60, t_used % 60);
1466    
1467  cleanup:  cleanup:
# Line 1342  cleanup: Line 1481  cleanup:
1481  #ifdef HAVE_SYS_EPOLL_H  #ifdef HAVE_SYS_EPOLL_H
1482          if (epollfd != -1 && close(epollfd) < 0)          if (epollfd != -1 && close(epollfd) < 0)
1483          {          {
1484                  log_error("close(epoll) error (%d)\n");                  log_error("close(epoll) error (%d)");
1485          }          }
1486  #endif  #endif
1487    
# Line 1364  cleanup: Line 1503  cleanup:
1503          // Restore STDIN/STDOUT flags          // Restore STDIN/STDOUT flags
1504          if (flags_stdin != -1 && fcntl(STDIN_FILENO, F_SETFL, flags_stdin) == -1)          if (flags_stdin != -1 && fcntl(STDIN_FILENO, F_SETFL, flags_stdin) == -1)
1505          {          {
1506                  log_error("fcntl(F_SETFL) error (%d)\n", errno);                  log_error("fcntl(F_SETFL) error (%d)", errno);
1507          }          }
1508          if (flags_stdout != -1 && fcntl(STDOUT_FILENO, F_SETFL, flags_stdout) == -1)          if (flags_stdout != -1 && fcntl(STDOUT_FILENO, F_SETFL, flags_stdout) == -1)
1509          {          {
1510                  log_error("fcntl(F_SETFL) error (%d)\n", errno);                  log_error("fcntl(F_SETFL) error (%d)", errno);
1511          }          }
1512    
1513          if (sock != -1 && close(sock) == -1)          if (sock != -1 && close(sock) == -1)
1514          {          {
1515                  log_error("close(socket) error (%d)\n", errno);                  log_error("close(socket) error (%d)", errno);
1516          }          }
1517    
1518          if (res)          if (res)
# Line 1461  int bbs_net() Line 1600  int bbs_net()
1600                  switch (ch)                  switch (ch)
1601                  {                  {
1602                  case KEY_NULL: // broken pipe                  case KEY_NULL: // broken pipe
1603                          log_debug("KEY_NULL\n");                          log_debug("KEY_NULL");
1604                          goto cleanup;                          goto cleanup;
1605                  case KEY_TIMEOUT:                  case KEY_TIMEOUT:
1606                          if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time)                          if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time)
1607                          {                          {
1608                                  log_debug("User input timeout\n");                                  log_debug("User input timeout");
1609                                  goto cleanup;                                  goto cleanup;
1610                          }                          }
1611                          continue;                          continue;
# Line 1476  int bbs_net() Line 1615  int bbs_net()
1615                  case CR:                  case CR:
1616                          if (bbsnet_connect(bbsnet_menu.menu_item_pos[0]) < 0)                          if (bbsnet_connect(bbsnet_menu.menu_item_pos[0]) < 0)
1617                          {                          {
1618                                  log_debug("bbsnet_connect() error\n");                                  log_debug("bbsnet_connect() error");
1619                          }                          }
1620                          // Force cleanup anything remaining in the output buffer                          // Force cleanup anything remaining in the output buffer
1621                          clearscr();                          clearscr();


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

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