/[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.89 by sysadm, Tue Dec 16 12:59:14 2025 UTC Revision 1.100 by sysadm, Thu Dec 18 08:38:55 2025 UTC
# Line 49  static const char MENU_CONF_DELIM[] = " Line 49  static const char MENU_CONF_DELIM[] = "
49    
50  enum _bbs_net_constant_t  enum _bbs_net_constant_t
51  {  {
         MAX_PROCESS_BAR_LEN = 30,  
52          MAXSTATION = 26 * 2,          MAXSTATION = 26 * 2,
53          STATION_PER_LINE = 4,          STATION_PER_LINE = 4,
54          USERNAME_MAX_LEN = 20,          USERNAME_MAX_LEN = 20,
55          PASSWORD_MAX_LEN = 20,          PASSWORD_MAX_LEN = 20,
56            REMOTE_CONNECT_TIMEOUT = 10, // seconds
57            SSH_CONNECT_TIMEOUT = 5,         // seconds
58            PROGRESS_BAR_LEN = 30,
59  };  };
60    
61  struct _bbsnet_conf  struct _bbsnet_conf
# Line 61  struct _bbsnet_conf Line 63  struct _bbsnet_conf
63          char org_name[40];          char org_name[40];
64          char site_name[40];          char site_name[40];
65          char host_name[IP_ADDR_LEN];          char host_name[IP_ADDR_LEN];
66          in_port_t port;          char port[6];
67          int8_t use_ssh;          int8_t use_ssh;
68          char charset[CHARSET_MAX_LEN + 1];          char charset[CHARSET_MAX_LEN + 1];
69  } bbsnet_conf[MAXSTATION];  } bbsnet_conf[MAXSTATION];
# Line 77  static int load_bbsnet_conf(const char * Line 79  static int load_bbsnet_conf(const char *
79          MENU_ITEM *p_menu_item;          MENU_ITEM *p_menu_item;
80          MENU_ITEM_ID menu_item_id;          MENU_ITEM_ID menu_item_id;
81          char line[LINE_BUFFER_LEN], *t1, *t2, *t3, *t4, *t5, *t6, *saveptr;          char line[LINE_BUFFER_LEN], *t1, *t2, *t3, *t4, *t5, *t6, *saveptr;
82          int port;          long port;
83            char *endptr;
84    
85          unload_bbsnet_conf();          unload_bbsnet_conf();
86    
# Line 123  static int load_bbsnet_conf(const char * Line 126  static int load_bbsnet_conf(const char *
126                  t6 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);                  t6 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
127    
128                  if (t1 == NULL || t2 == NULL || t3 == NULL || t4 == NULL ||                  if (t1 == NULL || t2 == NULL || t3 == NULL || t4 == NULL ||
129                          t5 == NULL || t6 == NULL || line[0] == '#' || line[0] == '*')                          t5 == NULL || t6 == NULL || t1[0] == '#')
130                  {                  {
131                          continue;                          continue;
132                  }                  }
# Line 134  static int load_bbsnet_conf(const char * Line 137  static int load_bbsnet_conf(const char *
137                  bbsnet_conf[menu_item_id].org_name[sizeof(bbsnet_conf[menu_item_id].org_name) - 1] = '\0';                  bbsnet_conf[menu_item_id].org_name[sizeof(bbsnet_conf[menu_item_id].org_name) - 1] = '\0';
138                  strncpy(bbsnet_conf[menu_item_id].host_name, t3, sizeof(bbsnet_conf[menu_item_id].host_name) - 1);                  strncpy(bbsnet_conf[menu_item_id].host_name, t3, sizeof(bbsnet_conf[menu_item_id].host_name) - 1);
139                  bbsnet_conf[menu_item_id].host_name[sizeof(bbsnet_conf[menu_item_id].host_name) - 1] = '\0';                  bbsnet_conf[menu_item_id].host_name[sizeof(bbsnet_conf[menu_item_id].host_name) - 1] = '\0';
140                  port = atoi(t4);                  port = strtol(t4, &endptr, 10);
141                  if (port <= 0 || port > 65535)                  if (*endptr != '\0' || port <= 0 || port > 65535)
142                  {                  {
143                          log_error("Invalid port value %d of menu item %d\n", port, menu_item_id);                          log_error("Invalid port value %ld of menu item %d\n", port, menu_item_id);
144                          fclose(fp);                          fclose(fp);
145                          unload_bbsnet_conf();                          unload_bbsnet_conf();
146                          return -3;                          return -3;
147                  }                  }
148                  bbsnet_conf[menu_item_id].port = (in_port_t)port;                  strncpy(bbsnet_conf[menu_item_id].port, t4, sizeof(bbsnet_conf[menu_item_id].port) - 1);
149                    bbsnet_conf[menu_item_id].port[sizeof(bbsnet_conf[menu_item_id].port) - 1] = '\0';
150                  bbsnet_conf[menu_item_id].use_ssh = (toupper(t5[0]) == 'Y');                  bbsnet_conf[menu_item_id].use_ssh = (toupper(t5[0]) == 'Y');
151                  strncpy(bbsnet_conf[menu_item_id].charset, t6, sizeof(bbsnet_conf[menu_item_id].charset) - 1);                  strncpy(bbsnet_conf[menu_item_id].charset, t6, sizeof(bbsnet_conf[menu_item_id].charset) - 1);
152                  bbsnet_conf[menu_item_id].charset[sizeof(bbsnet_conf[menu_item_id].charset) - 1] = '\0';                  bbsnet_conf[menu_item_id].charset[sizeof(bbsnet_conf[menu_item_id].charset) - 1] = '\0';
# Line 163  static int load_bbsnet_conf(const char * Line 167  static int load_bbsnet_conf(const char *
167                  p_menu_item->priv = 0;                  p_menu_item->priv = 0;
168                  p_menu_item->level = 0;                  p_menu_item->level = 0;
169                  p_menu_item->name[0] =                  p_menu_item->name[0] =
170                          (char)(menu_item_id < MAXSTATION / 2 ? 'A' + menu_item_id : 'a' + menu_item_id);                          (char)(menu_item_id < MAXSTATION / 2 ? 'A' + menu_item_id : 'a' + menu_item_id - MAXSTATION / 2);
171                  p_menu_item->name[1] = '\0';                  p_menu_item->name[1] = '\0';
172                  snprintf(p_menu_item->text, sizeof(p_menu_item->text), "\033[1;36m%c.\033[m %s",                  snprintf(p_menu_item->text, sizeof(p_menu_item->text), "\033[1;36m%c.\033[m %s",
173                                   p_menu_item->name[0], bbsnet_conf[menu_item_id].site_name);                                   p_menu_item->name[0], bbsnet_conf[menu_item_id].site_name);
# Line 201  static void unload_bbsnet_conf(void) Line 205  static void unload_bbsnet_conf(void)
205          }          }
206  }  }
207    
208  static void process_bar(int n, int len)  static void progress_bar(int percent, int len)
209  {  {
210            char line[LINE_BUFFER_LEN];
211          char buf[LINE_BUFFER_LEN];          char buf[LINE_BUFFER_LEN];
212          char buf2[LINE_BUFFER_LEN];          char buf2[LINE_BUFFER_LEN];
213            int pos;
214    
215          if (len <= 0)          if (len < 4)
216          {          {
217                  len = 1;                  len = 4;
218          }          }
219          else if (len > LINE_BUFFER_LEN)          else if (len + 2 > LINE_BUFFER_LEN)
220          {          {
221                  len = LINE_BUFFER_LEN - 1;                  len = LINE_BUFFER_LEN - 3;
222          }          }
223          if (n < 0)          if (percent < 0)
224          {          {
225                  n = 0;                  percent = 0;
226          }          }
227          else if (n > len)          else if (percent > 100)
228          {          {
229                  n = len;                  percent = 100;
230          }          }
231    
232            pos = len * percent / 100;
233    
234            line[0] = ' ';
235            for (int i = 1; i <= len; i++)
236            {
237                    line[i] = '-';
238            }
239            line[len + 1] = ' ';
240            line[len + 2] = '\0';
241    
242            snprintf(buf, sizeof(buf), "%*s%3d%%%*s",
243                             (len - 4) / 2, "", percent, (len - 4 + 1) / 2, "");
244            memcpy(buf2, buf, (size_t)pos);
245            buf2[pos] = '\0';
246    
247          moveto(4, 1);          moveto(4, 1);
248          prints(" ------------------------------ \r\n");          prints("%s\r\n", line);
249          snprintf(buf, sizeof(buf), "            %3d%%              ", n * 100 / len);          prints("|\033[46m%s\033[44m%s\033[m|\r\n", buf2, buf + pos);
250          memcpy(buf2, buf, (size_t)n);          prints("%s\r\n", line);
         buf2[n] = '\0';  
         prints("|\033[46m%s\033[44m%s\033[m|\r\n", buf2, buf + n);  
         prints(" ------------------------------ \r\n");  
251          iflush();          iflush();
252  }  }
253    
254  static int bbsnet_connect(int n)  static int bbsnet_connect(int n)
255  {  {
256          int sock = -1;          int sock = -1;
257          int ret;          int ret = 0;
258          int loop;          int loop;
259          int error;          int error;
260          int sock_connected = 0;          int sock_connected = 0;
# Line 272  static int bbsnet_connect(int n) Line 290  static int bbsnet_connect(int n)
290          int stdout_write_wait = 0;          int stdout_write_wait = 0;
291          int sock_read_wait = 0;          int sock_read_wait = 0;
292          int sock_write_wait = 0;          int sock_write_wait = 0;
293          struct hostent *p_host = NULL;          struct addrinfo hints, *res = NULL;
294          int tos;          int tos;
295          char remote_addr[IP_ADDR_LEN];          char remote_addr[INET_ADDRSTRLEN];
296          int remote_port;          int remote_port;
297          char local_addr[IP_ADDR_LEN];          char local_addr[INET_ADDRSTRLEN];
298          int local_port;          int local_port;
299          socklen_t sock_len;          socklen_t sock_len;
300          time_t t_used = time(NULL);          time_t t_begin, t_used;
301          struct tm *tm_used;          struct timespec ts_begin, ts_now;
302            int progress, progress_last;
303          int ch;          int ch;
304          char remote_user[USERNAME_MAX_LEN + 1];          char remote_user[USERNAME_MAX_LEN + 1];
305          char remote_pass[PASSWORD_MAX_LEN + 1];          char remote_pass[PASSWORD_MAX_LEN + 1];
306          ssh_session session = NULL;          ssh_session outbound_session = NULL;
307          ssh_channel channel = NULL;          ssh_channel outbound_channel = NULL;
308          int ssh_process_config = 0;          int ssh_process_config = 0;
309          int ssh_log_level = SSH_LOG_NOLOG;          int ssh_log_level = SSH_LOG_NOLOG;
         long ssh_timeout = 0;  
310    
311          if (user_online_update("BBS_NET") < 0)          if (user_online_update("BBS_NET") < 0)
312          {          {
# Line 341  static int bbsnet_connect(int n) Line 359  static int bbsnet_connect(int n)
359                     bbsnet_conf[n].site_name, bbsnet_conf[n].host_name);                     bbsnet_conf[n].site_name, bbsnet_conf[n].host_name);
360          iflush();          iflush();
361    
362          p_host = gethostbyname(bbsnet_conf[n].host_name);          memset(&hints, 0, sizeof(hints));
363            hints.ai_family = AF_INET;
364            hints.ai_socktype = SOCK_STREAM;
365            hints.ai_protocol = IPPROTO_TCP;
366    
367          if (p_host == NULL)          if ((ret = getaddrinfo(BBS_address, NULL, &hints, &res)) != 0)
368          {          {
369                  prints("\033[1;31m查找主机名失败!\033[m\r\n");                  log_error("getaddrinfo() error (%d): %s\n", ret, gai_strerror(ret));
370                  press_any_key();                  ret = -1;
371                  goto cleanup;                  goto cleanup;
372          }          }
373    
374          sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);          if (inet_ntop(AF_INET, &(((struct sockaddr_in *)res->ai_addr)->sin_addr), local_addr, sizeof(local_addr)) == NULL)
375            {
376                    log_error("inet_ntop() error (%d)\n", errno);
377                    ret = -1;
378                    goto cleanup;
379            }
380            local_port = ntohs(((struct sockaddr_in *)res->ai_addr)->sin_port);
381    
382            sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
383          if (sock < 0)          if (sock < 0)
384          {          {
385                  prints("\033[1;31m无法创建socket!\033[m\r\n");                  log_error("socket() error (%d)\n", errno);
386                  press_any_key();                  ret = -1;
387                  goto cleanup;                  goto cleanup;
388          }          }
389    
390          sin.sin_family = AF_INET;          if (bind(sock, res->ai_addr, res->ai_addrlen) < 0)
         sin.sin_addr.s_addr = (BBS_address[0] != '\0' ? inet_addr(BBS_address) : INADDR_ANY);  
         sin.sin_port = 0;  
   
         if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0)  
391          {          {
392                  log_error("Bind address %s:%u failed (%d)\n",                  log_error("bind(%s:%u) error (%d)\n", local_addr, local_port, errno);
393                                    inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), errno);                  ret = -1;
394                  goto cleanup;                  goto cleanup;
395          }          }
396    
397          memset(&sin, 0, sizeof(sin));          freeaddrinfo(res);
398          sin.sin_family = AF_INET;          res = NULL;
         sin.sin_addr = *(struct in_addr *)p_host->h_addr_list[0];  
         sin.sin_port = htons(bbsnet_conf[n].port);  
399    
400          strncpy(remote_addr, inet_ntoa(sin.sin_addr), sizeof(remote_addr) - 1);          memset(&hints, 0, sizeof(hints));
401          remote_addr[sizeof(remote_addr) - 1] = '\0';          hints.ai_family = AF_INET;
402          remote_port = ntohs(sin.sin_port);          hints.ai_flags = AI_NUMERICSERV;
403            hints.ai_socktype = SOCK_STREAM;
404            hints.ai_protocol = IPPROTO_TCP;
405    
406            if ((ret = getaddrinfo(bbsnet_conf[n].host_name, bbsnet_conf[n].port, &hints, &res)) != 0)
407            {
408                    log_error("getaddrinfo() error (%d): %s\n", ret, gai_strerror(ret));
409                    prints("\033[1;31m查找主机名失败!\033[m\r\n");
410                    press_any_key();
411                    ret = -1;
412                    goto cleanup;
413            }
414    
415          prints("\033[1;32m穿梭进度条提示您当前已使用的时间,按\033[1;33mCtrl+C\033[1;32m中断。\033[m\r\n");          if (inet_ntop(AF_INET, &(((struct sockaddr_in *)res->ai_addr)->sin_addr), remote_addr, sizeof(remote_addr)) == NULL)
416          process_bar(0, MAX_PROCESS_BAR_LEN);          {
417                    log_error("inet_ntop() error (%d)\n", errno);
418                    ret = -1;
419                    goto cleanup;
420            }
421            remote_port = ntohs(((struct sockaddr_in *)res->ai_addr)->sin_port);
422    
423          // Set socket as non-blocking          // Set socket as non-blocking
424          if ((flags_sock = fcntl(sock, F_GETFL, 0)) == -1)          if ((flags_sock = fcntl(sock, F_GETFL, 0)) == -1)
425          {          {
426                  log_error("fcntl(F_GETFL) error (%d)\n", errno);                  log_error("fcntl(F_GETFL) error (%d)\n", errno);
427                    ret = -1;
428                  goto cleanup;                  goto cleanup;
429          }          }
430          if ((fcntl(sock, F_SETFL, flags_sock | O_NONBLOCK)) == -1)          if ((fcntl(sock, F_SETFL, flags_sock | O_NONBLOCK)) == -1)
431          {          {
432                  log_error("fcntl(F_SETFL) error (%d)\n", errno);                  log_error("fcntl(F_SETFL) error (%d)\n", errno);
433                    ret = -1;
434                  goto cleanup;                  goto cleanup;
435          }          }
436    
# Line 398  static int bbsnet_connect(int n) Line 438  static int bbsnet_connect(int n)
438          if ((flags_stdin = fcntl(STDIN_FILENO, F_GETFL, 0)) == -1)          if ((flags_stdin = fcntl(STDIN_FILENO, F_GETFL, 0)) == -1)
439          {          {
440                  log_error("fcntl(F_GETFL) error (%d)\n", errno);                  log_error("fcntl(F_GETFL) error (%d)\n", errno);
441                    ret = -1;
442                  goto cleanup;                  goto cleanup;
443          }          }
444          if ((flags_stdout = fcntl(STDOUT_FILENO, F_GETFL, 0)) == -1)          if ((flags_stdout = fcntl(STDOUT_FILENO, F_GETFL, 0)) == -1)
445          {          {
446                  log_error("fcntl(F_GETFL) error (%d)\n", errno);                  log_error("fcntl(F_GETFL) error (%d)\n", errno);
447                    ret = -1;
448                  goto cleanup;                  goto cleanup;
449          }          }
450          if ((fcntl(STDIN_FILENO, F_SETFL, flags_stdin | O_NONBLOCK)) == -1)          if ((fcntl(STDIN_FILENO, F_SETFL, flags_stdin | O_NONBLOCK)) == -1)
451          {          {
452                  log_error("fcntl(F_SETFL) error (%d)\n", errno);                  log_error("fcntl(F_SETFL) error (%d)\n", errno);
453                    ret = -1;
454                  goto cleanup;                  goto cleanup;
455          }          }
456          if ((fcntl(STDOUT_FILENO, F_SETFL, flags_stdout | O_NONBLOCK)) == -1)          if ((fcntl(STDOUT_FILENO, F_SETFL, flags_stdout | O_NONBLOCK)) == -1)
457          {          {
458                  log_error("fcntl(F_SETFL) error (%d)\n", errno);                  log_error("fcntl(F_SETFL) error (%d)\n", errno);
459                    ret = -1;
460                  goto cleanup;                  goto cleanup;
461          }          }
462    
# Line 421  static int bbsnet_connect(int n) Line 465  static int bbsnet_connect(int n)
465          if (epollfd < 0)          if (epollfd < 0)
466          {          {
467                  log_error("epoll_create1() error (%d)\n", errno);                  log_error("epoll_create1() error (%d)\n", errno);
468                    ret = -1;
469                  goto cleanup;                  goto cleanup;
470          }          }
471    
# Line 429  static int bbsnet_connect(int n) Line 474  static int bbsnet_connect(int n)
474          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sock, &ev) == -1)          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sock, &ev) == -1)
475          {          {
476                  log_error("epoll_ctl(socket) error (%d)\n", errno);                  log_error("epoll_ctl(socket) error (%d)\n", errno);
477                    ret = -1;
478                  goto cleanup;                  goto cleanup;
479          }          }
480    
# Line 437  static int bbsnet_connect(int n) Line 483  static int bbsnet_connect(int n)
483          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1)          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1)
484          {          {
485                  log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno);                  log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno);
486                    ret = -1;
487                  goto cleanup;                  goto cleanup;
488          }          }
489  #endif  #endif
490    
491          while (!SYS_server_exit)          while (!SYS_server_exit)
492          {          {
493                  if ((ret = connect(sock, (struct sockaddr *)&sin, sizeof(sin))) < 0)                  if ((ret = connect(sock, res->ai_addr, res->ai_addrlen)) == 0)
494                    {
495                            sock_connected = 1;
496                            break;
497                    }
498                    else if (ret < 0)
499                  {                  {
500                          if (errno == EAGAIN || errno == EALREADY || errno == EINPROGRESS)                          if (errno == EAGAIN || errno == EALREADY || errno == EINPROGRESS)
501                          {                          {
# Line 459  static int bbsnet_connect(int n) Line 511  static int bbsnet_connect(int n)
511                          else                          else
512                          {                          {
513                                  log_error("connect(socket) error (%d)\n", errno);                                  log_error("connect(socket) error (%d)\n", errno);
   
514                                  prints("\033[1;31m连接失败!\033[m\r\n");                                  prints("\033[1;31m连接失败!\033[m\r\n");
515                                  press_any_key();                                  press_any_key();
516                                    ret = -1;
517                                  goto cleanup;                                  goto cleanup;
518                          }                          }
519                  }                  }
520          }          }
521    
522          for (int j = 0; j < MAX_PROCESS_BAR_LEN && !sock_connected && !SYS_server_exit; j++)          progress = progress_last = 0;
523            prints("\033[1;32m连接进行中,按\033[1;33mCtrl+C\033[1;32m中断。\033[m\r\n");
524            progress_bar(0, PROGRESS_BAR_LEN);
525    
526            if (clock_gettime(CLOCK_REALTIME, &ts_begin) == -1)
527            {
528                    log_error("clock_gettime() error (%d)\n", errno);
529                    ret = -1;
530                    goto cleanup;
531            }
532            ts_now = ts_begin;
533    
534            while ((ts_now.tv_sec - ts_begin.tv_sec) +
535                                       (ts_now.tv_nsec - ts_begin.tv_nsec) / 1000 / 1000 / 1000 <
536                               REMOTE_CONNECT_TIMEOUT &&
537                       !sock_connected && !SYS_server_exit)
538          {          {
539  #ifdef HAVE_SYS_EPOLL_H  #ifdef HAVE_SYS_EPOLL_H
540                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 500); // 0.5 second                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second
541                  ret = nfds;                  ret = nfds;
542  #else  #else
543                  pfds[0].fd = sock;                  pfds[0].fd = sock;
# Line 479  static int bbsnet_connect(int n) Line 545  static int bbsnet_connect(int n)
545                  pfds[1].fd = STDIN_FILENO;                  pfds[1].fd = STDIN_FILENO;
546                  pfds[1].events = POLLIN;                  pfds[1].events = POLLIN;
547                  nfds = 2;                  nfds = 2;
548                  ret = poll(pfds, (nfds_t)nfds, 500); // 0.5 second                  ret = poll(pfds, (nfds_t)nfds, 100); // 0.1 second
549  #endif  #endif
550    
551                  if (ret < 0)                  if (ret < 0)
# Line 496  static int bbsnet_connect(int n) Line 562  static int bbsnet_connect(int n)
562                  }                  }
563                  else if (ret == 0) // timeout                  else if (ret == 0) // timeout
564                  {                  {
565                          process_bar(j + 1, MAX_PROCESS_BAR_LEN);                          if (clock_gettime(CLOCK_REALTIME, &ts_now) == -1)
566                            {
567                                    log_error("clock_gettime() error (%d)\n", errno);
568                                    ret = -1;
569                                    goto cleanup;
570                            }
571    
572                            progress = (int)((ts_now.tv_sec - ts_begin.tv_sec) * 1000 +
573                                                             (ts_now.tv_nsec - ts_begin.tv_nsec) / 1000 / 1000) /
574                                                       REMOTE_CONNECT_TIMEOUT / 10 +
575                                               1;
576                            if (progress < 0)
577                            {
578                                    progress = 0;
579                            }
580                            if (progress > 100)
581                            {
582                                    progress = 100;
583                            }
584    
585                            if (progress > progress_last)
586                            {
587                                    progress_last = progress;
588                                    progress_bar(progress, PROGRESS_BAR_LEN);
589                            }
590                  }                  }
591                  else // ret > 0                  else // ret > 0
592                  {                  {
# Line 512  static int bbsnet_connect(int n) Line 602  static int bbsnet_connect(int n)
602                                          if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len) < 0)                                          if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
603                                          {                                          {
604                                                  log_error("getsockopt() error (%d) !\n", errno);                                                  log_error("getsockopt() error (%d) !\n", errno);
605                                                    ret = -1;
606                                                  goto cleanup;                                                  goto cleanup;
607                                          }                                          }
608                                          if (error == 0)                                          if (error == 0)
609                                          {                                          {
610                                                  sock_connected = 1;                                                  sock_connected = 1;
611                                                    break;
612                                          }                                          }
613                                  }                                  }
614  #ifdef HAVE_SYS_EPOLL_H  #ifdef HAVE_SYS_EPOLL_H
# Line 531  static int bbsnet_connect(int n) Line 623  static int bbsnet_connect(int n)
623                                          } while (ch == 0);                                          } while (ch == 0);
624                                          if (ch == Ctrl('C') || ch == KEY_ESC)                                          if (ch == Ctrl('C') || ch == KEY_ESC)
625                                          {                                          {
626                                                    ret = 0;
627                                                  goto cleanup;                                                  goto cleanup;
628                                          }                                          }
629                                  }                                  }
# Line 539  static int bbsnet_connect(int n) Line 632  static int bbsnet_connect(int n)
632          }          }
633          if (SYS_server_exit)          if (SYS_server_exit)
634          {          {
635                    ret = 0;
636                  goto cleanup;                  goto cleanup;
637          }          }
638          if (!sock_connected)          if (!sock_connected)
639          {          {
640                    progress_bar(100, PROGRESS_BAR_LEN);
641                  prints("\033[1;31m连接失败!\033[m\r\n");                  prints("\033[1;31m连接失败!\033[m\r\n");
642                  press_any_key();                  press_any_key();
643                    ret = -1;
644                  goto cleanup;                  goto cleanup;
645          }          }
646    
# Line 559  static int bbsnet_connect(int n) Line 654  static int bbsnet_connect(int n)
654          if (getsockname(sock, (struct sockaddr *)&sin, &sock_len) < 0)          if (getsockname(sock, (struct sockaddr *)&sin, &sock_len) < 0)
655          {          {
656                  log_error("getsockname() error: %d", errno);                  log_error("getsockname() error: %d", errno);
657                    ret = -1;
658                  goto cleanup;                  goto cleanup;
659          }          }
660    
661          strncpy(local_addr, inet_ntoa(sin.sin_addr), sizeof(local_addr) - 1);          if (inet_ntop(AF_INET, &(sin.sin_addr), local_addr, sizeof(local_addr)) == NULL)
662          local_addr[sizeof(local_addr) - 1] = '\0';          {
663                    log_error("inet_ntop() error (%d)\n", errno);
664                    ret = -1;
665                    goto cleanup;
666            }
667          local_port = ntohs(sin.sin_port);          local_port = ntohs(sin.sin_port);
668    
669          if (bbsnet_conf[n].use_ssh)          if (bbsnet_conf[n].use_ssh)
670          {          {
671                  session = ssh_new();                  outbound_session = ssh_new();
672                  if (session == NULL)                  if (outbound_session == NULL)
673                  {                  {
674                          log_error("ssh_new() error\n");                          log_error("ssh_new() error\n");
675                            ret = -1;
676                          goto cleanup;                          goto cleanup;
677                  }                  }
678    
679                  if (ssh_options_set(session, SSH_OPTIONS_FD, &sock) < 0 ||                  if (ssh_options_set(outbound_session, SSH_OPTIONS_FD, &sock) < 0 ||
680                          ssh_options_set(session, SSH_OPTIONS_PROCESS_CONFIG, &ssh_process_config) < 0 ||                          ssh_options_set(outbound_session, SSH_OPTIONS_PROCESS_CONFIG, &ssh_process_config) < 0 ||
681                          ssh_options_set(session, SSH_OPTIONS_KNOWNHOSTS, SSH_KNOWN_HOSTS_FILE) < 0 ||                          ssh_options_set(outbound_session, SSH_OPTIONS_KNOWNHOSTS, SSH_KNOWN_HOSTS_FILE) < 0 ||
682                          ssh_options_set(session, SSH_OPTIONS_HOST, bbsnet_conf[n].host_name) < 0 ||                          ssh_options_set(outbound_session, SSH_OPTIONS_HOST, bbsnet_conf[n].host_name) < 0 ||
683                          ssh_options_set(session, SSH_OPTIONS_USER, remote_user) < 0 ||                          ssh_options_set(outbound_session, SSH_OPTIONS_USER, remote_user) < 0 ||
684                          ssh_options_set(session, SSH_OPTIONS_HOSTKEYS, "+ssh-rsa") < 0 ||                          ssh_options_set(outbound_session, SSH_OPTIONS_HOSTKEYS, "+ssh-ed25519,ecdsa-sha2-nistp256,ssh-rsa") < 0 ||
685                          ssh_options_set(session, SSH_OPTIONS_LOG_VERBOSITY, &ssh_log_level) < 0)                          ssh_options_set(outbound_session, SSH_OPTIONS_LOG_VERBOSITY, &ssh_log_level) < 0)
686                  {                  {
687                          log_error("Error setting SSH options: %s\n", ssh_get_error(session));                          log_error("Error setting SSH options: %s\n", ssh_get_error(outbound_session));
688                            ret = -1;
689                          goto cleanup;                          goto cleanup;
690                  }                  }
691    
692                  ssh_timeout = 5; // second                  ssh_set_blocking(outbound_session, 0);
                 if (ssh_options_set(session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0)  
                 {  
                         log_error("Error setting SSH options: %s\n", ssh_get_error(session));  
                         goto cleanup;  
                 }  
693    
694                  while (!SYS_server_exit)                  t_begin = time(NULL);
695                    ret = SSH_ERROR;
696                    while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT)
697                  {                  {
698                          ret = ssh_connect(session);                          ret = ssh_connect(outbound_session);
699                          if (ret == SSH_OK)                          if (ret == SSH_OK)
700                          {                          {
701                                  break;                                  break;
702                          }                          }
703                          else if (ret == SSH_ERROR)                          else if (ret == SSH_AGAIN)
704                          {                          {
705                                  log_error("ssh_connect() error\n");                                  // log_debug("ssh_connect() error: SSH_AGAIN\n");
706                            }
707                            else // if (ret == SSH_ERROR)
708                            {
709                                    log_error("ssh_connect() error: SSH_ERROR\n");
710                                    ret = -1;
711                                  goto cleanup;                                  goto cleanup;
712                          }                          }
713                  }                  }
714                    if (ret != SSH_OK)
715                    {
716                            prints("\033[1;31m连接超时!\033[m\r\n");
717                            press_any_key();
718                            ret = -1;
719                            goto cleanup;
720                    }
721    
722                  ret = ssh_session_is_known_server(session);                  ret = ssh_session_is_known_server(outbound_session);
723                  switch (ret)                  switch (ret)
724                  {                  {
725                  case SSH_KNOWN_HOSTS_NOT_FOUND:                  case SSH_KNOWN_HOSTS_NOT_FOUND:
726                  case SSH_KNOWN_HOSTS_UNKNOWN:                  case SSH_KNOWN_HOSTS_UNKNOWN:
727                          if (ssh_session_update_known_hosts(session) != SSH_OK)                          if (ssh_session_update_known_hosts(outbound_session) != SSH_OK)
728                          {                          {
729                                  log_error("ssh_session_update_known_hosts(%s) error\n", bbsnet_conf[n].host_name);                                  log_error("ssh_session_update_known_hosts(%s) error\n", bbsnet_conf[n].host_name);
730                                  prints("\033[1;31m无法添加服务器证书\033[m");                                  prints("\033[1;31m无法添加服务器证书\033[m\r\n");
731                                  press_any_key();                                  press_any_key();
732                                    ret = -1;
733                                  goto cleanup;                                  goto cleanup;
734                          }                          }
735                          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\n", bbsnet_conf[n].host_name, SSH_KNOWN_HOSTS_FILE);
# Line 626  static int bbsnet_connect(int n) Line 738  static int bbsnet_connect(int n)
738                  case SSH_KNOWN_HOSTS_CHANGED:                  case SSH_KNOWN_HOSTS_CHANGED:
739                  case SSH_KNOWN_HOSTS_OTHER:                  case SSH_KNOWN_HOSTS_OTHER:
740                          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\n", bbsnet_conf[n].host_name, ret);
741                          prints("\033[1;31m服务器证书已变更\033[m");                          prints("\033[1;31m服务器证书已变更\033[m\r\n");
742                          press_any_key();                          press_any_key();
743                            ret = -1;
744                          goto cleanup;                          goto cleanup;
745                  }                  }
746    
747                  for (int i = 0; !SYS_server_exit;)                  ret = SSH_AUTH_ERROR;
748                    while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT)
749                  {                  {
750                          ret = ssh_userauth_password(session, NULL, remote_pass);                          ret = ssh_userauth_password(outbound_session, NULL, remote_pass);
751                          if (ret == SSH_AUTH_SUCCESS)                          if (ret == SSH_AUTH_SUCCESS)
752                          {                          {
753                                  break;                                  break;
754                          }                          }
755                          else if (ret == SSH_AUTH_AGAIN)                          else if (ret == SSH_AUTH_AGAIN)
756                          {                          {
757  #ifdef _DEBUG                                  // log_debug("ssh_userauth_password() error: SSH_AUTH_AGAIN\n");
                                 log_error("ssh_userauth_password() error: SSH_AUTH_AGAIN\n");  
 #endif  
758                          }                          }
759                          else if (ret == SSH_AUTH_ERROR)                          else if (ret == SSH_AUTH_ERROR)
760                          {                          {
761                                  log_error("ssh_userauth_password() error: %d\n", ret);                                  log_error("ssh_userauth_password() error: SSH_AUTH_ERROR\n");
762                                    ret = -1;
763                                  goto cleanup;                                  goto cleanup;
764                          }                          }
765                          else // if (ret == SSH_AUTH_DENIED)                          else // if (ret == SSH_AUTH_DENIED)
766                          {                          {
767                                    log_error("ssh_userauth_password() error: SSH_AUTH_DENIED\n");
768                                  prints("\033[1;31m身份验证失败!\033[m\r\n");                                  prints("\033[1;31m身份验证失败!\033[m\r\n");
769                                  i++;                                  press_any_key();
770                                  if (i < BBS_login_retry_times)                                  ret = -1;
771                                  {                                  goto cleanup;
                                         prints("请输入密码: ");  
                                         iflush();  
                                         if (str_input(remote_pass, sizeof(remote_pass), NOECHO) < 0)  
                                         {  
                                                 goto cleanup;  
                                         }  
                                         if (remote_pass[0] == '\0')  
                                         {  
                                                 goto cleanup;  
                                         }  
                                 }  
                                 else  
                                 {  
                                         goto cleanup;  
                                 }  
772                          }                          }
773                  }                  }
774                    if (ret != SSH_AUTH_SUCCESS)
775                    {
776                            prints("\033[1;31m连接超时!\033[m\r\n");
777                            press_any_key();
778                            ret = -1;
779                            goto cleanup;
780                    }
781    
782                  channel = ssh_channel_new(session);                  outbound_channel = ssh_channel_new(outbound_session);
783                  if (channel == NULL)                  if (outbound_channel == NULL)
784                  {                  {
785                          log_error("ssh_channel_new() error\n");                          log_error("ssh_channel_new() error\n");
786                            ret = -1;
787                          goto cleanup;                          goto cleanup;
788                  }                  }
789    
790                  while (!SYS_server_exit)                  ret = SSH_ERROR;
791                    while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT)
792                  {                  {
793                          ret = ssh_channel_open_session(channel);                          ret = ssh_channel_open_session(outbound_channel);
794                          if (ret == SSH_OK)                          if (ret == SSH_OK)
795                          {                          {
796                                  break;                                  break;
797                          }                          }
798                          else if (ret == SSH_ERROR)                          else if (ret == SSH_AGAIN)
799                            {
800                                    // log_debug("ssh_channel_open_session() error: SSH_AGAIN\n");
801                            }
802                            else // if (ret == SSH_ERROR)
803                          {                          {
804                                  log_error("ssh_channel_open_session() error\n");                                  log_error("ssh_channel_open_session() error: SSH_ERROR\n");
805                                    ret = -1;
806                                  goto cleanup;                                  goto cleanup;
807                          }                          }
808                  }                  }
809                    if (ret != SSH_OK)
810                    {
811                            prints("\033[1;31m连接超时!\033[m\r\n");
812                            press_any_key();
813                            ret = -1;
814                            goto cleanup;
815                    }
816    
817                  while (!SYS_server_exit)                  ret = SSH_ERROR;
818                    while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT)
819                  {                  {
820                          ret = ssh_channel_request_pty(channel);                          ret = ssh_channel_request_pty(outbound_channel);
821                          if (ret == SSH_OK)                          if (ret == SSH_OK)
822                          {                          {
823                                  break;                                  break;
824                          }                          }
825                          else if (ret == SSH_ERROR)                          else if (ret == SSH_AGAIN)
826                          {                          {
827                                  log_error("ssh_channel_request_pty() error\n");                                  // log_debug("ssh_channel_request_pty() error: SSH_AGAIN\n");
828                            }
829                            else // if (ret == SSH_ERROR)
830                            {
831                                    log_error("ssh_channel_request_pty() error: SSH_ERROR\n");
832                                    ret = -1;
833                                  goto cleanup;                                  goto cleanup;
834                          }                          }
835                  }                  }
836                    if (ret != SSH_OK)
837                    {
838                            prints("\033[1;31m连接超时!\033[m\r\n");
839                            press_any_key();
840                            ret = -1;
841                            goto cleanup;
842                    }
843    
844                  while (!SYS_server_exit)                  ret = SSH_ERROR;
845                    while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT)
846                  {                  {
847                          ret = ssh_channel_request_shell(channel);                          ret = ssh_channel_request_shell(outbound_channel);
848                          if (ret == SSH_OK)                          if (ret == SSH_OK)
849                          {                          {
850                                  break;                                  break;
851                          }                          }
852                          else if (ret == SSH_ERROR)                          else if (ret == SSH_AGAIN)
853                            {
854                                    // log_debug("ssh_channel_request_shell() error: SSH_AGAIN\n");
855                            }
856                            else // if (ret == SSH_ERROR)
857                          {                          {
858                                  log_error("ssh_channel_request_shell() error\n");                                  log_error("ssh_channel_request_shell() error: SSH_ERROR\n");
859                                    ret = -1;
860                                  goto cleanup;                                  goto cleanup;
861                          }                          }
862                  }                  }
863                    if (ret != SSH_OK)
                 ssh_timeout = 0; // second  
                 if (ssh_options_set(session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0)  
864                  {                  {
865                          log_error("Error setting SSH options: %s\n", ssh_get_error(session));                          prints("\033[1;31m连接超时!\033[m\r\n");
866                            press_any_key();
867                            ret = -1;
868                          goto cleanup;                          goto cleanup;
869                  }                  }
   
                 ssh_set_blocking(session, 0);  
870          }          }
871    
872          prints("\033[1;31m连接成功!\033[m\r\n");          prints("\033[1;31m连接成功!\033[m\r\n");
# Line 743  static int bbsnet_connect(int n) Line 880  static int bbsnet_connect(int n)
880          if (input_cd == (iconv_t)(-1))          if (input_cd == (iconv_t)(-1))
881          {          {
882                  log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, tocode, errno);                  log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, tocode, errno);
883                    ret = -1;
884                  goto cleanup;                  goto cleanup;
885          }          }
886    
# Line 752  static int bbsnet_connect(int n) Line 890  static int bbsnet_connect(int n)
890          if (output_cd == (iconv_t)(-1))          if (output_cd == (iconv_t)(-1))
891          {          {
892                  log_error("iconv_open(%s->%s) error: %d\n", bbsnet_conf[n].charset, tocode, errno);                  log_error("iconv_open(%s->%s) error: %d\n", bbsnet_conf[n].charset, tocode, errno);
893                    ret = -1;
894                  goto cleanup;                  goto cleanup;
895          }          }
896    
# Line 761  static int bbsnet_connect(int n) Line 900  static int bbsnet_connect(int n)
900          if (epoll_ctl(epollfd, EPOLL_CTL_MOD, sock, &ev) == -1)          if (epoll_ctl(epollfd, EPOLL_CTL_MOD, sock, &ev) == -1)
901          {          {
902                  log_error("epoll_ctl(socket) error (%d)\n", errno);                  log_error("epoll_ctl(socket) error (%d)\n", errno);
903                    ret = -1;
904                  goto cleanup;                  goto cleanup;
905          }          }
906    
# Line 769  static int bbsnet_connect(int n) Line 909  static int bbsnet_connect(int n)
909          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1)          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1)
910          {          {
911                  log_error("epoll_ctl(STDOUT_FILENO) error (%d)\n", errno);                  log_error("epoll_ctl(STDOUT_FILENO) error (%d)\n", errno);
912                    ret = -1;
913                  goto cleanup;                  goto cleanup;
914          }          }
915  #endif  #endif
916    
917          BBS_last_access_tm = t_used = time(NULL);          BBS_last_access_tm = t_begin = time(NULL);
918          loop = 1;          loop = 1;
919    
920          while (loop && !SYS_server_exit)          while (loop && !SYS_server_exit)
921          {          {
922                  if (SSH_v2 && ssh_channel_is_closed(SSH_channel))                  if (SSH_v2 && ssh_channel_is_closed(SSH_channel))
923                  {                  {
924                          log_error("SSH channel is closed\n");                          log_debug("SSH channel is closed\n");
925                          loop = 0;                          loop = 0;
926                          break;                          break;
927                  }                  }
928    
929                  if (bbsnet_conf[n].use_ssh && ssh_channel_is_closed(channel))                  if (bbsnet_conf[n].use_ssh && ssh_channel_is_closed(outbound_channel))
930                  {                  {
931                          log_error("Remote SSH channel is closed\n");                          log_debug("Outbound channel is closed\n");
932                          loop = 0;                          loop = 0;
933                          break;                          break;
934                  }                  }
# Line 830  static int bbsnet_connect(int n) Line 971  static int bbsnet_connect(int n)
971                  for (int i = 0; i < nfds; i++)                  for (int i = 0; i < nfds; i++)
972                  {                  {
973  #ifdef HAVE_SYS_EPOLL_H  #ifdef HAVE_SYS_EPOLL_H
974                          if (events[i].data.fd == STDIN_FILENO)                          if (events[i].events & (EPOLLHUP | EPOLLERR))
975    #else
976                            if (pfds[i].revents & (POLLHUP | POLLERR))
977    #endif
978                            {
979    #ifdef HAVE_SYS_EPOLL_H
980                                    log_debug("FD (%d) error events (%d)\n", events[i].data.fd, events[i].events);
981    #else
982                                    log_debug("FD (%d) error events (%d)\n", pfds[i].fd, pfds[i].revents);
983    #endif
984                                    loop = 0;
985                                    break;
986                            }
987    
988    #ifdef HAVE_SYS_EPOLL_H
989                            if (events[i].data.fd == STDIN_FILENO && (events[i].events & EPOLLIN))
990  #else  #else
991                          if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & POLLIN))                          if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & POLLIN))
992  #endif  #endif
# Line 864  static int bbsnet_connect(int n) Line 1020  static int bbsnet_connect(int n)
1020                          }                          }
1021    
1022  #ifdef HAVE_SYS_EPOLL_H  #ifdef HAVE_SYS_EPOLL_H
1023                          if (events[i].data.fd == STDOUT_FILENO)                          if (events[i].data.fd == STDOUT_FILENO && (events[i].events & EPOLLOUT))
1024  #else  #else
1025                          if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & POLLOUT))                          if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & POLLOUT))
1026  #endif  #endif
# Line 882  static int bbsnet_connect(int n) Line 1038  static int bbsnet_connect(int n)
1038                                          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);
1039                                          if (ret == SSH_ERROR)                                          if (ret == SSH_ERROR)
1040                                          {                                          {
1041                                                  log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session));                                                  log_debug("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session));
1042                                                  loop = 0;                                                  loop = 0;
1043                                                  break;                                                  break;
1044                                          }                                          }
# Line 897  static int bbsnet_connect(int n) Line 1053  static int bbsnet_connect(int n)
1053                                                  // Send NO-OP to remote server                                                  // Send NO-OP to remote server
1054                                                  input_buf[input_buf_len] = '\0';                                                  input_buf[input_buf_len] = '\0';
1055                                                  input_buf_len++;                                                  input_buf_len++;
                                                 BBS_last_access_tm = time(NULL);  
1056    
1057                                                    BBS_last_access_tm = time(NULL);
1058                                                  stdin_read_wait = 0;                                                  stdin_read_wait = 0;
1059                                                  break; // Check whether channel is still open                                                  break; // Check whether channel is still open
1060                                          }                                          }
# Line 927  static int bbsnet_connect(int n) Line 1083  static int bbsnet_connect(int n)
1083                                  }                                  }
1084                                  else if (ret == 0) // broken pipe                                  else if (ret == 0) // broken pipe
1085                                  {                                  {
1086  #ifdef _DEBUG                                          log_debug("read(STDIN) EOF\n");
                                         log_error("read(STDIN) EOF\n");  
 #endif  
1087                                          stdin_read_wait = 0;                                          stdin_read_wait = 0;
1088                                          loop = 0;                                          loop = 0;
1089                                          break;                                          break;
# Line 958  static int bbsnet_connect(int n) Line 1112  static int bbsnet_connect(int n)
1112  #ifdef _DEBUG  #ifdef _DEBUG
1113                                  for (int j = input_buf_offset; j < input_buf_len; j++)                                  for (int j = input_buf_offset; j < input_buf_len; j++)
1114                                  {                                  {
1115                                          log_error("Debug input: <--[%u]\n", (input_buf[j] + 256) % 256);                                          log_debug("input: <--[%u]\n", (input_buf[j] + 256) % 256);
1116                                  }                                  }
1117  #endif  #endif
1118    
# Line 973  static int bbsnet_connect(int n) Line 1127  static int bbsnet_connect(int n)
1127  #ifdef _DEBUG  #ifdef _DEBUG
1128                                  for (int j = input_conv_offset; j < input_conv_len; j++)                                  for (int j = input_conv_offset; j < input_conv_len; j++)
1129                                  {                                  {
1130                                          log_error("Debug input_conv: <--[%u]\n", (input_conv[j] + 256) % 256);                                          log_debug("input_conv: <--[%u]\n", (input_conv[j] + 256) % 256);
1131                                  }                                  }
1132  #endif  #endif
1133                          }                          }
# Line 982  static int bbsnet_connect(int n) Line 1136  static int bbsnet_connect(int n)
1136                          {                          {
1137                                  if (bbsnet_conf[n].use_ssh)                                  if (bbsnet_conf[n].use_ssh)
1138                                  {                                  {
1139                                          ret = ssh_channel_write(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));
1140                                          if (ret == SSH_ERROR)                                          if (ret == SSH_ERROR)
1141                                          {                                          {
1142                                                  log_error("ssh_channel_write() error: %s\n", ssh_get_error(session));                                                  log_debug("ssh_channel_write() error: %s\n", ssh_get_error(outbound_session));
1143                                                  loop = 0;                                                  loop = 0;
1144                                                  break;                                                  break;
1145                                          }                                          }
# Line 1007  static int bbsnet_connect(int n) Line 1161  static int bbsnet_connect(int n)
1161                                          }                                          }
1162                                          else                                          else
1163                                          {                                          {
1164                                                  log_error("write(socket) error (%d)\n", errno);                                                  log_debug("write(socket) error (%d)\n", errno);
1165                                                  loop = 0;                                                  loop = 0;
1166                                                  break;                                                  break;
1167                                          }                                          }
1168                                  }                                  }
1169                                  else if (ret == 0) // broken pipe                                  else if (ret == 0) // broken pipe
1170                                  {                                  {
1171  #ifdef _DEBUG                                          log_debug("write(socket) EOF\n");
                                         log_error("write(socket) EOF\n");  
 #endif  
1172                                          sock_write_wait = 0;                                          sock_write_wait = 0;
1173                                          loop = 0;                                          loop = 0;
1174                                          break;                                          break;
# Line 1041  static int bbsnet_connect(int n) Line 1193  static int bbsnet_connect(int n)
1193                          {                          {
1194                                  if (bbsnet_conf[n].use_ssh)                                  if (bbsnet_conf[n].use_ssh)
1195                                  {                                  {
1196                                          ret = ssh_channel_read_nonblocking(channel, output_buf + output_buf_len,                                          ret = ssh_channel_read_nonblocking(outbound_channel, output_buf + output_buf_len,
1197                                                                                                             (uint32_t)(sizeof(output_buf) - (size_t)output_buf_len), 0);                                                                                                             (uint32_t)(sizeof(output_buf) - (size_t)output_buf_len), 0);
1198                                          if (ret == SSH_ERROR)                                          if (ret == SSH_ERROR)
1199                                          {                                          {
1200                                                  log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(session));                                                  log_debug("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(outbound_session));
1201                                                  loop = 0;                                                  loop = 0;
1202                                                  break;                                                  break;
1203                                          }                                          }
# Line 1078  static int bbsnet_connect(int n) Line 1230  static int bbsnet_connect(int n)
1230                                          }                                          }
1231                                          else                                          else
1232                                          {                                          {
1233                                                  log_error("read(socket) error (%d)\n", errno);                                                  log_debug("read(socket) error (%d)\n", errno);
1234                                                  loop = 0;                                                  loop = 0;
1235                                                  break;                                                  break;
1236                                          }                                          }
1237                                  }                                  }
1238                                  else if (ret == 0) // broken pipe                                  else if (ret == 0) // broken pipe
1239                                  {                                  {
1240  #ifdef _DEBUG                                          log_debug("read(socket) EOF\n");
                                         log_error("read(socket) EOF\n");  
 #endif  
1241                                          sock_read_wait = 0;                                          sock_read_wait = 0;
1242                                          loop = 0;                                          loop = 0;
1243                                          break;                                          break;
# Line 1119  static int bbsnet_connect(int n) Line 1269  static int bbsnet_connect(int n)
1269                                          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));
1270                                          if (ret == SSH_ERROR)                                          if (ret == SSH_ERROR)
1271                                          {                                          {
1272                                                  log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session));                                                  log_debug("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session));
1273                                                  loop = 0;                                                  loop = 0;
1274                                                  break;                                                  break;
1275                                          }                                          }
# Line 1141  static int bbsnet_connect(int n) Line 1291  static int bbsnet_connect(int n)
1291                                          }                                          }
1292                                          else                                          else
1293                                          {                                          {
1294                                                  log_error("write(STDOUT) error (%d)\n", errno);                                                  log_debug("write(STDOUT) error (%d)\n", errno);
1295                                                  loop = 0;                                                  loop = 0;
1296                                                  break;                                                  break;
1297                                          }                                          }
1298                                  }                                  }
1299                                  else if (ret == 0) // broken pipe                                  else if (ret == 0) // broken pipe
1300                                  {                                  {
1301  #ifdef _DEBUG                                          log_debug("write(STDOUT) EOF\n");
                                         log_error("write(STDOUT) EOF\n");  
 #endif  
1302                                          stdout_write_wait = 0;                                          stdout_write_wait = 0;
1303                                          loop = 0;                                          loop = 0;
1304                                          break;                                          break;
# Line 1170  static int bbsnet_connect(int n) Line 1318  static int bbsnet_connect(int n)
1318                  }                  }
1319          }          }
1320    
1321            ret = 1; // Normal disconnect
1322            BBS_last_access_tm = time(NULL);
1323            t_used = BBS_last_access_tm - t_begin;
1324            log_common("BBSNET disconnect, %ld days %ld hours %ld minutes %ld seconds used\n",
1325                               t_used / 86400, t_used % 86400 / 3600, t_used % 3600 / 60, t_used % 60);
1326    
1327  cleanup:  cleanup:
1328            // Clear sensitive data
1329            memset(remote_pass, 0, sizeof(remote_pass));
1330            memset(remote_user, 0, sizeof(remote_user));
1331    
1332          if (input_cd != (iconv_t)(-1))          if (input_cd != (iconv_t)(-1))
1333          {          {
1334                  iconv_close(input_cd);                  iconv_close(input_cd);
# Line 1189  cleanup: Line 1347  cleanup:
1347    
1348          if (bbsnet_conf[n].use_ssh)          if (bbsnet_conf[n].use_ssh)
1349          {          {
1350                  if (channel != NULL)                  if (outbound_channel != NULL)
1351                  {                  {
1352                          ssh_channel_free(channel);                          ssh_channel_send_eof(outbound_channel);
1353                            ssh_channel_close(outbound_channel);
1354                            ssh_channel_free(outbound_channel);
1355                  }                  }
1356                  if (session != NULL)                  if (outbound_session != NULL)
1357                  {                  {
1358                          ssh_disconnect(session);                          ssh_disconnect(outbound_session);
1359                          ssh_free(session);                          ssh_free(outbound_session);
1360                  }                  }
1361          }          }
1362    
1363          // Restore STDIN/STDOUT flags          // Restore STDIN/STDOUT flags
1364          if (flags_stdin != -1 &&fcntl(STDIN_FILENO, F_SETFL, flags_stdin) == -1)          if (flags_stdin != -1 && fcntl(STDIN_FILENO, F_SETFL, flags_stdin) == -1)
1365          {          {
1366                  log_error("fcntl(F_SETFL) error (%d)\n", errno);                  log_error("fcntl(F_SETFL) error (%d)\n", errno);
1367          }          }
# Line 1215  cleanup: Line 1375  cleanup:
1375                  log_error("Close socket failed\n");                  log_error("Close socket failed\n");
1376          }          }
1377    
1378          t_used = time(NULL) - t_used;          if (res)
1379          tm_used = gmtime(&t_used);          {
1380                    freeaddrinfo(res);
1381          log_common("BBSNET disconnect, %d days %d hours %d minutes %d seconds used\n",          }
                            tm_used->tm_mday - 1, tm_used->tm_hour, tm_used->tm_min,  
                            tm_used->tm_sec);  
   
         BBS_last_access_tm = time(NULL);  
1382    
1383          return 0;          return ret;
1384  }  }
1385    
1386  static int bbsnet_refresh()  static int bbsnet_refresh()
# Line 1266  static int bbsnet_selchange() Line 1422  static int bbsnet_selchange()
1422          prints("|");          prints("|");
1423          moveto(21, 1);          moveto(21, 1);
1424          clrtoeol();          clrtoeol();
1425          prints("|\033[1m连往: \033[1;33m%-20s\033[m  端口: \033[1;33m%-5d\033[m                 编码: \033[1;33m%s\033[m",          prints("|\033[1m连往: \033[1;33m%-20s\033[m  端口: \033[1;33m%-5s\033[m                 编码: \033[1;33m%s\033[m",
1426                     bbsnet_conf[i].host_name, bbsnet_conf[i].port, bbsnet_conf[i].charset);                     bbsnet_conf[i].host_name, bbsnet_conf[i].port, bbsnet_conf[i].charset);
1427          moveto(21, 80);          moveto(21, 80);
1428          prints("|");          prints("|");
# Line 1277  static int bbsnet_selchange() Line 1433  static int bbsnet_selchange()
1433    
1434  int bbs_net()  int bbs_net()
1435  {  {
1436          int ch, i;          int ch;
1437    
1438          if (load_bbsnet_conf(CONF_BBSNET) < 0)          if (load_bbsnet_conf(CONF_BBSNET) < 0)
1439          {          {
# Line 1304  int bbs_net() Line 1460  int bbs_net()
1460                  switch (ch)                  switch (ch)
1461                  {                  {
1462                  case KEY_NULL: // broken pipe                  case KEY_NULL: // broken pipe
1463                          log_error("KEY_NULL\n");                          log_debug("KEY_NULL\n");
1464                          goto cleanup;                          goto cleanup;
1465                  case KEY_TIMEOUT:                  case KEY_TIMEOUT:
1466                          if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time)                          if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time)
# Line 1317  int bbs_net() Line 1473  int bbs_net()
1473                  case Ctrl('C'): // user cancel                  case Ctrl('C'): // user cancel
1474                          goto cleanup;                          goto cleanup;
1475                  case CR:                  case CR:
1476                          bbsnet_connect(bbsnet_menu.menu_item_pos[0]);                          if (bbsnet_connect(bbsnet_menu.menu_item_pos[0]) < 0)
1477                            {
1478                                    log_debug("bbsnet_connect() error\n");
1479                            }
1480                          // Force cleanup anything remaining in the output buffer                          // Force cleanup anything remaining in the output buffer
1481                          clearscr();                          clearscr();
1482                          iflush();                          iflush();
# Line 1327  int bbs_net() Line 1486  int bbs_net()
1486                          bbsnet_selchange();                          bbsnet_selchange();
1487                          break;                          break;
1488                  case KEY_UP:                  case KEY_UP:
1489                          for (i = 0; i < STATION_PER_LINE; i++)                          for (int i = 0; i < STATION_PER_LINE; i++)
1490                          {                          {
1491                                  menu_control(&bbsnet_menu, KEY_UP);                                  menu_control(&bbsnet_menu, KEY_UP);
1492                          }                          }
1493                          bbsnet_selchange();                          bbsnet_selchange();
1494                          break;                          break;
1495                  case KEY_DOWN:                  case KEY_DOWN:
1496                          for (i = 0; i < STATION_PER_LINE; i++)                          for (int i = 0; i < STATION_PER_LINE; i++)
1497                          {                          {
1498                                  menu_control(&bbsnet_menu, KEY_DOWN);                                  menu_control(&bbsnet_menu, KEY_DOWN);
1499                          }                          }


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

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