/[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.99 by sysadm, Thu Dec 18 08:22:53 2025 UTC Revision 1.108 by sysadm, Sat Dec 20 10:46:42 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 209  static void progress_bar(int percent, in Line 209  static void progress_bar(int percent, in
209  {  {
210          char line[LINE_BUFFER_LEN];          char line[LINE_BUFFER_LEN];
211          char buf[LINE_BUFFER_LEN];          char buf[LINE_BUFFER_LEN];
         char buf2[LINE_BUFFER_LEN];  
212          int pos;          int pos;
213    
214          if (len < 4)          if (len < 4)
# Line 241  static void progress_bar(int percent, in Line 240  static void progress_bar(int percent, in
240    
241          snprintf(buf, sizeof(buf), "%*s%3d%%%*s",          snprintf(buf, sizeof(buf), "%*s%3d%%%*s",
242                           (len - 4) / 2, "", percent, (len - 4 + 1) / 2, "");                           (len - 4) / 2, "", percent, (len - 4 + 1) / 2, "");
         memcpy(buf2, buf, (size_t)pos);  
         buf2[pos] = '\0';  
243    
244          moveto(4, 1);          moveto(4, 1);
245          prints("%s\r\n", line);          prints("%s\r\n", line);
246          prints("|\033[46m%s\033[44m%s\033[m|\r\n", buf2, buf + pos);          prints("|\033[46m%.*s\033[44m%s\033[m|\r\n", pos, buf, buf + pos);
247          prints("%s\r\n", line);          prints("%s\r\n", line);
248          iflush();          iflush();
249  }  }
250    
251    static int connect_timeout(struct timespec *p_ts_begin, struct timespec *p_ts_now,
252                                                       int total_time_ms, int *p_progress_last, int bar_len)
253    {
254            long duration_ms;
255            int progress;
256    
257            if (clock_gettime(CLOCK_REALTIME, p_ts_now) == -1)
258            {
259                    log_error("clock_gettime() error (%d)", errno);
260                    return -1;
261            }
262    
263            duration_ms = (p_ts_now->tv_sec - p_ts_begin->tv_sec) * 1000 +
264                                      (p_ts_now->tv_nsec - p_ts_begin->tv_nsec) / 1000 / 1000;
265    
266            if (duration_ms >= total_time_ms)
267            {
268                    progress_bar(100, bar_len);
269                    prints("\033[1;31m连接超时!\033[m\r\n");
270                    press_any_key();
271    
272                    return 1;
273            }
274    
275            progress = (int)(duration_ms * 100 / total_time_ms) + 1;
276    
277            if (progress != *p_progress_last)
278            {
279                    *p_progress_last = progress;
280                    progress_bar(progress, bar_len);
281            }
282    
283            return 0;
284    }
285    
286  static int bbsnet_connect(int n)  static int bbsnet_connect(int n)
287  {  {
288          int sock = -1;          int sock = -1;
# Line 258  static int bbsnet_connect(int n) Line 290  static int bbsnet_connect(int n)
290          int loop;          int loop;
291          int error;          int error;
292          int sock_connected = 0;          int sock_connected = 0;
293            int ssh_connected = 0;
294          int flags_sock = -1;          int flags_sock = -1;
295          int flags_stdin = -1;          int flags_stdin = -1;
296          int flags_stdout = -1;          int flags_stdout = -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 (!SYS_server_exit &&
534                       !connect_timeout(&ts_begin, &ts_now, REMOTE_CONNECT_TIMEOUT * 1000, &progress_last, PROGRESS_BAR_LEN))
535          {          {
536                  if ((ret = connect(sock, res->ai_addr, res->ai_addrlen)) == 0)                  if ((ret = connect(sock, res->ai_addr, res->ai_addrlen)) == 0)
537                  {                  {
# Line 506  static int bbsnet_connect(int n) Line 549  static int bbsnet_connect(int n)
549                          }                          }
550                          else if (errno == EINTR)                          else if (errno == EINTR)
551                          {                          {
                                 continue;  
552                          }                          }
553                          else                          else
554                          {                          {
555                                  log_error("connect(socket) error (%d)\n", errno);                                  log_error("connect(socket) error (%d)", errno);
556                                  prints("\033[1;31m连接失败!\033[m\r\n");                                  prints("\033[1;31m连接失败!\033[m\r\n");
557                                  press_any_key();                                  press_any_key();
558                                  ret = -1;                                  ret = -1;
# Line 519  static int bbsnet_connect(int n) Line 561  static int bbsnet_connect(int n)
561                  }                  }
562          }          }
563    
564          progress = progress_last = 0;          while (!SYS_server_exit && !sock_connected &&
565          prints("\033[1;32m连接进行中,按\033[1;33mCtrl+C\033[1;32m中断。\033[m\r\n");                     !connect_timeout(&ts_begin, &ts_now, REMOTE_CONNECT_TIMEOUT * 1000, &progress_last, PROGRESS_BAR_LEN))
         progress_bar(0, PROGRESS_BAR_LEN);  
   
         if (clock_gettime(CLOCK_REALTIME, &ts_begin) == -1)  
         {  
                 log_error("clock_gettime() error (%d)\n", errno);  
                 ret = -1;  
                 goto cleanup;  
         }  
         ts_now = ts_begin;  
   
         while ((ts_now.tv_sec - ts_begin.tv_sec) +  
                                    (ts_now.tv_nsec - ts_begin.tv_nsec) / 1000 / 1000 / 1000 <  
                            REMOTE_CONNECT_TIMEOUT &&  
                    !sock_connected && !SYS_server_exit)  
566          {          {
567  #ifdef HAVE_SYS_EPOLL_H  #ifdef HAVE_SYS_EPOLL_H
568                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second
# Line 553  static int bbsnet_connect(int n) Line 581  static int bbsnet_connect(int n)
581                          if (errno != EINTR)                          if (errno != EINTR)
582                          {                          {
583  #ifdef HAVE_SYS_EPOLL_H  #ifdef HAVE_SYS_EPOLL_H
584                                  log_error("epoll_wait() error (%d)\n", errno);                                  log_error("epoll_wait() error (%d)", errno);
585  #else  #else
586                                  log_error("poll() error (%d)\n", errno);                                  log_error("poll() error (%d)", errno);
587  #endif  #endif
588                                  break;                                  break;
589                          }                          }
590                  }                  }
591                  else if (ret == 0) // timeout                  else if (ret == 0) // timeout
592                  {                  {
                         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 > progress_last)  
                         {  
                                 progress_last = progress;  
                                 progress_bar(progress, PROGRESS_BAR_LEN);  
                         }  
593                  }                  }
594                  else // ret > 0                  else // ret > 0
595                  {                  {
# Line 593  static int bbsnet_connect(int n) Line 604  static int bbsnet_connect(int n)
604                                          socklen_t len = sizeof(error);                                          socklen_t len = sizeof(error);
605                                          if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len) < 0)                                          if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
606                                          {                                          {
607                                                  log_error("getsockopt() error (%d) !\n", errno);                                                  log_error("getsockopt() error (%d) !", errno);
608                                                  ret = -1;                                                  ret = -1;
609                                                  goto cleanup;                                                  goto cleanup;
610                                          }                                          }
# Line 622  static int bbsnet_connect(int n) Line 633  static int bbsnet_connect(int n)
633                          }                          }
634                  }                  }
635          }          }
         if (SYS_server_exit)  
         {  
                 ret = 0;  
                 goto cleanup;  
         }  
636          if (!sock_connected)          if (!sock_connected)
637          {          {
                 progress_bar(100, PROGRESS_BAR_LEN);  
                 prints("\033[1;31m连接失败!\033[m\r\n");  
                 press_any_key();  
638                  ret = -1;                  ret = -1;
639                  goto cleanup;                  goto cleanup;
640          }          }
# Line 639  static int bbsnet_connect(int n) Line 642  static int bbsnet_connect(int n)
642          tos = IPTOS_LOWDELAY;          tos = IPTOS_LOWDELAY;
643          if (setsockopt(sock, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)          if (setsockopt(sock, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)
644          {          {
645                  log_error("setsockopt IP_TOS=%d error (%d)\n", tos, errno);                  log_error("setsockopt IP_TOS=%d error (%d)", tos, errno);
646          }          }
647    
648          sock_len = sizeof(sin);          sock_len = sizeof(sin);
# Line 652  static int bbsnet_connect(int n) Line 655  static int bbsnet_connect(int n)
655    
656          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)
657          {          {
658                  log_error("inet_ntop() error (%d)\n", errno);                  log_error("inet_ntop() error (%d)", errno);
659                  ret = -1;                  ret = -1;
660                  goto cleanup;                  goto cleanup;
661          }          }
# Line 663  static int bbsnet_connect(int n) Line 666  static int bbsnet_connect(int n)
666                  outbound_session = ssh_new();                  outbound_session = ssh_new();
667                  if (outbound_session == NULL)                  if (outbound_session == NULL)
668                  {                  {
669                          log_error("ssh_new() error\n");                          log_error("ssh_new() error");
670                          ret = -1;                          ret = -1;
671                          goto cleanup;                          goto cleanup;
672                  }                  }
# Line 676  static int bbsnet_connect(int n) Line 679  static int bbsnet_connect(int n)
679                          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 ||
680                          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)
681                  {                  {
682                          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));
683                          ret = -1;                          ret = -1;
684                          goto cleanup;                          goto cleanup;
685                  }                  }
686    
687                  ssh_set_blocking(outbound_session, 0);                  ssh_set_blocking(outbound_session, 0);
688    
689                  t_begin = time(NULL);                  ssh_connected = 0;
690                  ret = SSH_ERROR;                  while (!SYS_server_exit &&
691                  while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT)                             !connect_timeout(&ts_begin, &ts_now, REMOTE_CONNECT_TIMEOUT * 1000, &progress_last, PROGRESS_BAR_LEN))
692                  {                  {
693                          ret = ssh_connect(outbound_session);                          ret = ssh_connect(outbound_session);
694                          if (ret == SSH_OK)                          if (ret == SSH_OK)
695                          {                          {
696                                    ssh_connected = 1;
697                                  break;                                  break;
698                          }                          }
699                          else if (ret == SSH_AGAIN)                          else if (ret == SSH_AGAIN)
700                          {                          {
701                                  // log_debug("ssh_connect() error: SSH_AGAIN\n");                                  // log_debug("ssh_connect() error: SSH_AGAIN");
702                          }                          }
703                          else // if (ret == SSH_ERROR)                          else // if (ret == SSH_ERROR)
704                          {                          {
705                                  log_error("ssh_connect() error: SSH_ERROR\n");                                  log_error("ssh_connect() error: SSH_ERROR");
706                                  ret = -1;                                  ret = -1;
707                                  goto cleanup;                                  goto cleanup;
708                          }                          }
709                  }                  }
710                  if (ret != SSH_OK)                  if (!ssh_connected)
711                  {                  {
                         prints("\033[1;31m连接超时!\033[m\r\n");  
                         press_any_key();  
712                          ret = -1;                          ret = -1;
713                          goto cleanup;                          goto cleanup;
714                  }                  }
# Line 718  static int bbsnet_connect(int n) Line 720  static int bbsnet_connect(int n)
720                  case SSH_KNOWN_HOSTS_UNKNOWN:                  case SSH_KNOWN_HOSTS_UNKNOWN:
721                          if (ssh_session_update_known_hosts(outbound_session) != SSH_OK)                          if (ssh_session_update_known_hosts(outbound_session) != SSH_OK)
722                          {                          {
723                                  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);
724                                  prints("\033[1;31m无法添加服务器证书\033[m\r\n");                                  prints("\033[1;31m无法添加服务器证书\033[m\r\n");
725                                  press_any_key();                                  press_any_key();
726                                  ret = -1;                                  ret = -1;
727                                  goto cleanup;                                  goto cleanup;
728                          }                          }
729                          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);
730                  case SSH_KNOWN_HOSTS_OK:                  case SSH_KNOWN_HOSTS_OK:
731                          break;                          break;
732                  case SSH_KNOWN_HOSTS_CHANGED:                  case SSH_KNOWN_HOSTS_CHANGED:
733                  case SSH_KNOWN_HOSTS_OTHER:                  case SSH_KNOWN_HOSTS_OTHER:
734                          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);
735                          prints("\033[1;31m服务器证书已变更\033[m\r\n");                          prints("\033[1;31m服务器证书已变更\033[m\r\n");
736                          press_any_key();                          press_any_key();
737                          ret = -1;                          ret = -1;
738                          goto cleanup;                          goto cleanup;
739                  }                  }
740    
741                  ret = SSH_AUTH_ERROR;                  ssh_connected = 0;
742                  while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT)                  while (!SYS_server_exit &&
743                               !connect_timeout(&ts_begin, &ts_now, REMOTE_CONNECT_TIMEOUT * 1000, &progress_last, PROGRESS_BAR_LEN))
744                  {                  {
745                          ret = ssh_userauth_password(outbound_session, NULL, remote_pass);                          ret = ssh_userauth_password(outbound_session, NULL, remote_pass);
746                          if (ret == SSH_AUTH_SUCCESS)                          if (ret == SSH_AUTH_SUCCESS)
747                          {                          {
748                                    ssh_connected = 1;
749                                  break;                                  break;
750                          }                          }
751                          else if (ret == SSH_AUTH_AGAIN)                          else if (ret == SSH_AUTH_AGAIN)
752                          {                          {
753                                  // log_debug("ssh_userauth_password() error: SSH_AUTH_AGAIN\n");                                  // log_debug("ssh_userauth_password() error: SSH_AUTH_AGAIN");
754                          }                          }
755                          else if (ret == SSH_AUTH_ERROR)                          else if (ret == SSH_AUTH_ERROR)
756                          {                          {
757                                  log_error("ssh_userauth_password() error: SSH_AUTH_ERROR\n");                                  log_error("ssh_userauth_password() error: SSH_AUTH_ERROR");
758                                  ret = -1;                                  ret = -1;
759                                  goto cleanup;                                  goto cleanup;
760                          }                          }
761                          else // if (ret == SSH_AUTH_DENIED)                          else // if (ret == SSH_AUTH_DENIED)
762                          {                          {
763                                  log_error("ssh_userauth_password() error: SSH_AUTH_DENIED\n");                                  log_debug("ssh_userauth_password() error: SSH_AUTH_DENIED");
764                                  prints("\033[1;31m身份验证失败!\033[m\r\n");                                  prints("\033[1;31m身份验证失败!\033[m\r\n");
765                                  press_any_key();                                  press_any_key();
766                                  ret = -1;                                  ret = 0;
767                                  goto cleanup;                                  goto cleanup;
768                          }                          }
769                  }                  }
770                  if (ret != SSH_AUTH_SUCCESS)                  if (!ssh_connected)
771                  {                  {
                         prints("\033[1;31m连接超时!\033[m\r\n");  
                         press_any_key();  
772                          ret = -1;                          ret = -1;
773                          goto cleanup;                          goto cleanup;
774                  }                  }
# Line 774  static int bbsnet_connect(int n) Line 776  static int bbsnet_connect(int n)
776                  outbound_channel = ssh_channel_new(outbound_session);                  outbound_channel = ssh_channel_new(outbound_session);
777                  if (outbound_channel == NULL)                  if (outbound_channel == NULL)
778                  {                  {
779                          log_error("ssh_channel_new() error\n");                          log_error("ssh_channel_new() error");
780                          ret = -1;                          ret = -1;
781                          goto cleanup;                          goto cleanup;
782                  }                  }
783    
784                  ret = SSH_ERROR;                  ssh_connected = 0;
785                  while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT)                  while (!SYS_server_exit &&
786                               !connect_timeout(&ts_begin, &ts_now, REMOTE_CONNECT_TIMEOUT * 1000, &progress_last, PROGRESS_BAR_LEN))
787                  {                  {
788                          ret = ssh_channel_open_session(outbound_channel);                          ret = ssh_channel_open_session(outbound_channel);
789                          if (ret == SSH_OK)                          if (ret == SSH_OK)
790                          {                          {
791                                    ssh_connected = 1;
792                                  break;                                  break;
793                          }                          }
794                          else if (ret == SSH_AGAIN)                          else if (ret == SSH_AGAIN)
795                          {                          {
796                                  // log_debug("ssh_channel_open_session() error: SSH_AGAIN\n");                                  // log_debug("ssh_channel_open_session() error: SSH_AGAIN");
797                          }                          }
798                          else // if (ret == SSH_ERROR)                          else // if (ret == SSH_ERROR)
799                          {                          {
800                                  log_error("ssh_channel_open_session() error: SSH_ERROR\n");                                  log_error("ssh_channel_open_session() error: SSH_ERROR");
801                                  ret = -1;                                  ret = -1;
802                                  goto cleanup;                                  goto cleanup;
803                          }                          }
804                  }                  }
805                  if (ret != SSH_OK)                  if (!ssh_connected)
806                  {                  {
                         prints("\033[1;31m连接超时!\033[m\r\n");  
                         press_any_key();  
807                          ret = -1;                          ret = -1;
808                          goto cleanup;                          goto cleanup;
809                  }                  }
810    
811                  ret = SSH_ERROR;                  ssh_connected = 0;
812                  while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT)                  while (!SYS_server_exit &&
813                               !connect_timeout(&ts_begin, &ts_now, REMOTE_CONNECT_TIMEOUT * 1000, &progress_last, PROGRESS_BAR_LEN))
814                  {                  {
815                          ret = ssh_channel_request_pty(outbound_channel);                          ret = ssh_channel_request_pty(outbound_channel);
816                          if (ret == SSH_OK)                          if (ret == SSH_OK)
817                          {                          {
818                                    ssh_connected = 1;
819                                  break;                                  break;
820                          }                          }
821                          else if (ret == SSH_AGAIN)                          else if (ret == SSH_AGAIN)
822                          {                          {
823                                  // log_debug("ssh_channel_request_pty() error: SSH_AGAIN\n");                                  // log_debug("ssh_channel_request_pty() error: SSH_AGAIN");
824                          }                          }
825                          else // if (ret == SSH_ERROR)                          else // if (ret == SSH_ERROR)
826                          {                          {
827                                  log_error("ssh_channel_request_pty() error: SSH_ERROR\n");                                  log_error("ssh_channel_request_pty() error: SSH_ERROR");
828                                  ret = -1;                                  ret = -1;
829                                  goto cleanup;                                  goto cleanup;
830                          }                          }
831                  }                  }
832                  if (ret != SSH_OK)                  if (!ssh_connected)
833                  {                  {
                         prints("\033[1;31m连接超时!\033[m\r\n");  
                         press_any_key();  
834                          ret = -1;                          ret = -1;
835                          goto cleanup;                          goto cleanup;
836                  }                  }
837    
838                  ret = SSH_ERROR;                  ssh_connected = 0;
839                  while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT)                  while (!SYS_server_exit &&
840                               !connect_timeout(&ts_begin, &ts_now, REMOTE_CONNECT_TIMEOUT * 1000, &progress_last, PROGRESS_BAR_LEN))
841                  {                  {
842                          ret = ssh_channel_request_shell(outbound_channel);                          ret = ssh_channel_request_shell(outbound_channel);
843                          if (ret == SSH_OK)                          if (ret == SSH_OK)
844                          {                          {
845                                    ssh_connected = 1;
846                                  break;                                  break;
847                          }                          }
848                          else if (ret == SSH_AGAIN)                          else if (ret == SSH_AGAIN)
849                          {                          {
850                                  // log_debug("ssh_channel_request_shell() error: SSH_AGAIN\n");                                  // log_debug("ssh_channel_request_shell() error: SSH_AGAIN");
851                          }                          }
852                          else // if (ret == SSH_ERROR)                          else // if (ret == SSH_ERROR)
853                          {                          {
854                                  log_error("ssh_channel_request_shell() error: SSH_ERROR\n");                                  log_error("ssh_channel_request_shell() error: SSH_ERROR");
855                                  ret = -1;                                  ret = -1;
856                                  goto cleanup;                                  goto cleanup;
857                          }                          }
858                  }                  }
859                  if (ret != SSH_OK)                  if (!ssh_connected)
860                  {                  {
                         prints("\033[1;31m连接超时!\033[m\r\n");  
                         press_any_key();  
861                          ret = -1;                          ret = -1;
862                          goto cleanup;                          goto cleanup;
863                  }                  }
# Line 863  static int bbsnet_connect(int n) Line 865  static int bbsnet_connect(int n)
865    
866          prints("\033[1;31m连接成功!\033[m\r\n");          prints("\033[1;31m连接成功!\033[m\r\n");
867          iflush();          iflush();
868          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]",
869                             remote_addr, remote_port, local_addr, local_port, BBS_username);                             remote_addr, remote_port, local_addr, local_port, BBS_username);
870    
871          snprintf(tocode, sizeof(tocode), "%s%s", bbsnet_conf[n].charset,          snprintf(tocode, sizeof(tocode), "%s%s", bbsnet_conf[n].charset,
# Line 871  static int bbsnet_connect(int n) Line 873  static int bbsnet_connect(int n)
873          input_cd = iconv_open(tocode, stdio_charset);          input_cd = iconv_open(tocode, stdio_charset);
874          if (input_cd == (iconv_t)(-1))          if (input_cd == (iconv_t)(-1))
875          {          {
876                  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);
877                  ret = -1;                  ret = -1;
878                  goto cleanup;                  goto cleanup;
879          }          }
# Line 881  static int bbsnet_connect(int n) Line 883  static int bbsnet_connect(int n)
883          output_cd = iconv_open(tocode, bbsnet_conf[n].charset);          output_cd = iconv_open(tocode, bbsnet_conf[n].charset);
884          if (output_cd == (iconv_t)(-1))          if (output_cd == (iconv_t)(-1))
885          {          {
886                  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);
887                  ret = -1;                  ret = -1;
888                  goto cleanup;                  goto cleanup;
889          }          }
# Line 891  static int bbsnet_connect(int n) Line 893  static int bbsnet_connect(int n)
893          ev.data.fd = sock;          ev.data.fd = sock;
894          if (epoll_ctl(epollfd, EPOLL_CTL_MOD, sock, &ev) == -1)          if (epoll_ctl(epollfd, EPOLL_CTL_MOD, sock, &ev) == -1)
895          {          {
896                  log_error("epoll_ctl(socket) error (%d)\n", errno);                  log_error("epoll_ctl(socket) error (%d)", errno);
897                  ret = -1;                  ret = -1;
898                  goto cleanup;                  goto cleanup;
899          }          }
# Line 900  static int bbsnet_connect(int n) Line 902  static int bbsnet_connect(int n)
902          ev.data.fd = STDOUT_FILENO;          ev.data.fd = STDOUT_FILENO;
903          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1)          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1)
904          {          {
905                  log_error("epoll_ctl(STDOUT_FILENO) error (%d)\n", errno);                  log_error("epoll_ctl(STDOUT_FILENO) error (%d)", errno);
906                  ret = -1;                  ret = -1;
907                  goto cleanup;                  goto cleanup;
908          }          }
# Line 913  static int bbsnet_connect(int n) Line 915  static int bbsnet_connect(int n)
915          {          {
916                  if (SSH_v2 && ssh_channel_is_closed(SSH_channel))                  if (SSH_v2 && ssh_channel_is_closed(SSH_channel))
917                  {                  {
918                          log_debug("SSH channel is closed\n");                          log_debug("SSH channel is closed");
919                          loop = 0;                          loop = 0;
920                          break;                          break;
921                  }                  }
922    
923                  if (bbsnet_conf[n].use_ssh && ssh_channel_is_closed(outbound_channel))                  if (bbsnet_conf[n].use_ssh && ssh_channel_is_closed(outbound_channel))
924                  {                  {
925                          log_debug("Outbound channel is closed\n");                          log_debug("Outbound channel is closed");
926                          loop = 0;                          loop = 0;
927                          break;                          break;
928                  }                  }
# Line 944  static int bbsnet_connect(int n) Line 946  static int bbsnet_connect(int n)
946                          if (errno != EINTR)                          if (errno != EINTR)
947                          {                          {
948  #ifdef HAVE_SYS_EPOLL_H  #ifdef HAVE_SYS_EPOLL_H
949                                  log_error("epoll_wait() error (%d)\n", errno);                                  log_error("epoll_wait() error (%d)", errno);
950  #else  #else
951                                  log_error("poll() error (%d)\n", errno);                                  log_error("poll() error (%d)", errno);
952  #endif  #endif
953                                  break;                                  break;
954                          }                          }
# Line 956  static int bbsnet_connect(int n) Line 958  static int bbsnet_connect(int n)
958                  {                  {
959                          if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time)                          if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time)
960                          {                          {
961                                    log_debug("User input timeout");
962                                  break;                                  break;
963                          }                          }
964                  }                  }
# Line 969  static int bbsnet_connect(int n) Line 972  static int bbsnet_connect(int n)
972  #endif  #endif
973                          {                          {
974  #ifdef HAVE_SYS_EPOLL_H  #ifdef HAVE_SYS_EPOLL_H
975                                  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);
976  #else  #else
977                                  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);
978  #endif  #endif
979                                  loop = 0;                                  loop = 0;
980                                  break;                                  break;
# Line 1030  static int bbsnet_connect(int n) Line 1033  static int bbsnet_connect(int n)
1033                                          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);
1034                                          if (ret == SSH_ERROR)                                          if (ret == SSH_ERROR)
1035                                          {                                          {
1036                                                  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));
1037                                                  loop = 0;                                                  loop = 0;
1038                                                  break;                                                  break;
1039                                          }                                          }
# Line 1068  static int bbsnet_connect(int n) Line 1071  static int bbsnet_connect(int n)
1071                                          }                                          }
1072                                          else                                          else
1073                                          {                                          {
1074                                                  log_error("read(STDIN) error (%d)\n", errno);                                                  log_error("read(STDIN) error (%d)", errno);
1075                                                  loop = 0;                                                  loop = 0;
1076                                                  break;                                                  break;
1077                                          }                                          }
1078                                  }                                  }
1079                                  else if (ret == 0) // broken pipe                                  else if (ret == 0) // broken pipe
1080                                  {                                  {
1081                                          log_debug("read(STDIN) EOF\n");                                          log_debug("read(STDIN) EOF");
1082                                          stdin_read_wait = 0;                                          stdin_read_wait = 0;
1083                                          loop = 0;                                          loop = 0;
1084                                          break;                                          break;
# Line 1088  static int bbsnet_connect(int n) Line 1091  static int bbsnet_connect(int n)
1091                                          // Refresh current action while user input                                          // Refresh current action while user input
1092                                          if (user_online_update("BBS_NET") < 0)                                          if (user_online_update("BBS_NET") < 0)
1093                                          {                                          {
1094                                                  log_error("user_online_update(BBS_NET) error\n");                                                  log_error("user_online_update(BBS_NET) error");
1095                                          }                                          }
1096    
1097                                          continue;                                          continue;
# Line 1104  static int bbsnet_connect(int n) Line 1107  static int bbsnet_connect(int n)
1107  #ifdef _DEBUG  #ifdef _DEBUG
1108                                  for (int j = input_buf_offset; j < input_buf_len; j++)                                  for (int j = input_buf_offset; j < input_buf_len; j++)
1109                                  {                                  {
1110                                          log_debug("input: <--[%u]\n", (input_buf[j] + 256) % 256);                                          log_debug("input: <--[%u]", (unsigned char)(input_buf[j]));
1111                                  }                                  }
1112  #endif  #endif
1113    
1114                                  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);
1115                                  if (ret < 0)                                  if (ret < 0)
1116                                  {                                  {
1117                                          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);
1118                                          input_buf_len = input_buf_offset; // Discard invalid sequence                                          input_buf_len = input_buf_offset; // Discard invalid sequence
1119                                  }                                  }
1120    
# Line 1119  static int bbsnet_connect(int n) Line 1122  static int bbsnet_connect(int n)
1122  #ifdef _DEBUG  #ifdef _DEBUG
1123                                  for (int j = input_conv_offset; j < input_conv_len; j++)                                  for (int j = input_conv_offset; j < input_conv_len; j++)
1124                                  {                                  {
1125                                          log_debug("input_conv: <--[%u]\n", (input_conv[j] + 256) % 256);                                          log_debug("input_conv: <--[%u]", (unsigned char)(input_conv[j]));
1126                                  }                                  }
1127  #endif  #endif
1128                          }                          }
# Line 1131  static int bbsnet_connect(int n) Line 1134  static int bbsnet_connect(int n)
1134                                          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));
1135                                          if (ret == SSH_ERROR)                                          if (ret == SSH_ERROR)
1136                                          {                                          {
1137                                                  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));
1138                                                  loop = 0;                                                  loop = 0;
1139                                                  break;                                                  break;
1140                                          }                                          }
# Line 1153  static int bbsnet_connect(int n) Line 1156  static int bbsnet_connect(int n)
1156                                          }                                          }
1157                                          else                                          else
1158                                          {                                          {
1159                                                  log_debug("write(socket) error (%d)\n", errno);                                                  log_debug("write(socket) error (%d)", errno);
1160                                                  loop = 0;                                                  loop = 0;
1161                                                  break;                                                  break;
1162                                          }                                          }
1163                                  }                                  }
1164                                  else if (ret == 0) // broken pipe                                  else if (ret == 0) // broken pipe
1165                                  {                                  {
1166                                          log_debug("write(socket) EOF\n");                                          log_debug("write(socket) EOF");
1167                                          sock_write_wait = 0;                                          sock_write_wait = 0;
1168                                          loop = 0;                                          loop = 0;
1169                                          break;                                          break;
# Line 1189  static int bbsnet_connect(int n) Line 1192  static int bbsnet_connect(int n)
1192                                                                                                             (uint32_t)(sizeof(output_buf) - (size_t)output_buf_len), 0);                                                                                                             (uint32_t)(sizeof(output_buf) - (size_t)output_buf_len), 0);
1193                                          if (ret == SSH_ERROR)                                          if (ret == SSH_ERROR)
1194                                          {                                          {
1195                                                  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));
1196                                                  loop = 0;                                                  loop = 0;
1197                                                  break;                                                  break;
1198                                          }                                          }
# Line 1222  static int bbsnet_connect(int n) Line 1225  static int bbsnet_connect(int n)
1225                                          }                                          }
1226                                          else                                          else
1227                                          {                                          {
1228                                                  log_debug("read(socket) error (%d)\n", errno);                                                  log_debug("read(socket) error (%d)", errno);
1229                                                  loop = 0;                                                  loop = 0;
1230                                                  break;                                                  break;
1231                                          }                                          }
1232                                  }                                  }
1233                                  else if (ret == 0) // broken pipe                                  else if (ret == 0) // broken pipe
1234                                  {                                  {
1235                                          log_debug("read(socket) EOF\n");                                          log_debug("read(socket) EOF");
1236                                          sock_read_wait = 0;                                          sock_read_wait = 0;
1237                                          loop = 0;                                          loop = 0;
1238                                          break;                                          break;
# Line 1249  static int bbsnet_connect(int n) Line 1252  static int bbsnet_connect(int n)
1252                                  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);
1253                                  if (ret < 0)                                  if (ret < 0)
1254                                  {                                  {
1255                                          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);
1256                                          output_buf_len = output_buf_offset; // Discard invalid sequence                                          output_buf_len = output_buf_offset; // Discard invalid sequence
1257                                  }                                  }
1258                          }                          }
# Line 1261  static int bbsnet_connect(int n) Line 1264  static int bbsnet_connect(int n)
1264                                          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));
1265                                          if (ret == SSH_ERROR)                                          if (ret == SSH_ERROR)
1266                                          {                                          {
1267                                                  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));
1268                                                  loop = 0;                                                  loop = 0;
1269                                                  break;                                                  break;
1270                                          }                                          }
# Line 1283  static int bbsnet_connect(int n) Line 1286  static int bbsnet_connect(int n)
1286                                          }                                          }
1287                                          else                                          else
1288                                          {                                          {
1289                                                  log_debug("write(STDOUT) error (%d)\n", errno);                                                  log_debug("write(STDOUT) error (%d)", errno);
1290                                                  loop = 0;                                                  loop = 0;
1291                                                  break;                                                  break;
1292                                          }                                          }
1293                                  }                                  }
1294                                  else if (ret == 0) // broken pipe                                  else if (ret == 0) // broken pipe
1295                                  {                                  {
1296                                          log_debug("write(STDOUT) EOF\n");                                          log_debug("write(STDOUT) EOF");
1297                                          stdout_write_wait = 0;                                          stdout_write_wait = 0;
1298                                          loop = 0;                                          loop = 0;
1299                                          break;                                          break;
# Line 1313  static int bbsnet_connect(int n) Line 1316  static int bbsnet_connect(int n)
1316          ret = 1; // Normal disconnect          ret = 1; // Normal disconnect
1317          BBS_last_access_tm = time(NULL);          BBS_last_access_tm = time(NULL);
1318          t_used = BBS_last_access_tm - t_begin;          t_used = BBS_last_access_tm - t_begin;
1319          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",
1320                             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);
1321    
1322  cleanup:  cleanup:
# Line 1321  cleanup: Line 1324  cleanup:
1324          memset(remote_pass, 0, sizeof(remote_pass));          memset(remote_pass, 0, sizeof(remote_pass));
1325          memset(remote_user, 0, sizeof(remote_user));          memset(remote_user, 0, sizeof(remote_user));
1326    
1327          if (input_cd != (iconv_t)(-1))          if (input_cd != (iconv_t)(-1) && iconv_close(input_cd) < 0)
1328          {          {
1329                  iconv_close(input_cd);                  log_error("iconv_close(input) error (%d)", errno);
1330          }          }
1331          if (output_cd != (iconv_t)(-1))          if (output_cd != (iconv_t)(-1) && iconv_close(output_cd) < 0)
1332          {          {
1333                  iconv_close(output_cd);                  log_error("iconv_close(output) error (%d)", errno);
1334          }          }
1335    
1336  #ifdef HAVE_SYS_EPOLL_H  #ifdef HAVE_SYS_EPOLL_H
1337          if (epollfd != -1 && close(epollfd) < 0)          if (epollfd != -1 && close(epollfd) < 0)
1338          {          {
1339                  log_error("close(epoll) error (%d)\n");                  log_error("close(epoll) error (%d)");
1340          }          }
1341  #endif  #endif
1342    
# Line 1355  cleanup: Line 1358  cleanup:
1358          // Restore STDIN/STDOUT flags          // Restore STDIN/STDOUT flags
1359          if (flags_stdin != -1 && fcntl(STDIN_FILENO, F_SETFL, flags_stdin) == -1)          if (flags_stdin != -1 && fcntl(STDIN_FILENO, F_SETFL, flags_stdin) == -1)
1360          {          {
1361                  log_error("fcntl(F_SETFL) error (%d)\n", errno);                  log_error("fcntl(F_SETFL) error (%d)", errno);
1362          }          }
1363          if (flags_stdout != -1 && fcntl(STDOUT_FILENO, F_SETFL, flags_stdout) == -1)          if (flags_stdout != -1 && fcntl(STDOUT_FILENO, F_SETFL, flags_stdout) == -1)
1364          {          {
1365                  log_error("fcntl(F_SETFL) error (%d)\n", errno);                  log_error("fcntl(F_SETFL) error (%d)", errno);
1366          }          }
1367    
1368          if (sock != -1 && close(sock) == -1)          if (sock != -1)
1369          {          {
1370                  log_error("Close socket failed\n");                  close(sock);
1371          }          }
1372    
1373          if (res)          if (res)
# Line 1452  int bbs_net() Line 1455  int bbs_net()
1455                  switch (ch)                  switch (ch)
1456                  {                  {
1457                  case KEY_NULL: // broken pipe                  case KEY_NULL: // broken pipe
1458                          log_debug("KEY_NULL\n");                          log_debug("KEY_NULL");
1459                          goto cleanup;                          goto cleanup;
1460                  case KEY_TIMEOUT:                  case KEY_TIMEOUT:
1461                          if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time)                          if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time)
1462                          {                          {
1463                                  log_error("User input timeout\n");                                  log_debug("User input timeout");
1464                                  goto cleanup;                                  goto cleanup;
1465                          }                          }
1466                          continue;                          continue;
# Line 1467  int bbs_net() Line 1470  int bbs_net()
1470                  case CR:                  case CR:
1471                          if (bbsnet_connect(bbsnet_menu.menu_item_pos[0]) < 0)                          if (bbsnet_connect(bbsnet_menu.menu_item_pos[0]) < 0)
1472                          {                          {
1473                                  log_debug("bbsnet_connect() error\n");                                  log_debug("bbsnet_connect() error");
1474                          }                          }
1475                          // Force cleanup anything remaining in the output buffer                          // Force cleanup anything remaining in the output buffer
1476                          clearscr();                          clearscr();


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

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