/[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.92 by sysadm, Wed Dec 17 03:56:39 2025 UTC Revision 1.110 by sysadm, Sat Jan 3 10:27:14 2026 UTC
# Line 3  Line 3 
3   * bbs_net   * bbs_net
4   *   - user interactive feature of site shuttle   *   - user interactive feature of site shuttle
5   *   *
6   * Copyright (C) 2004-2025  Leaflet <leaflet@leafok.com>   * Copyright (C) 2004-2026  Leaflet <leaflet@leafok.com>
7   */   */
8    
9  #ifdef HAVE_CONFIG_H  #ifdef HAVE_CONFIG_H
# Line 45  Line 45 
45  #include <poll.h>  #include <poll.h>
46  #endif  #endif
47    
 static const char MENU_CONF_DELIM[] = " \t\r\n";  
   
48  enum _bbs_net_constant_t  enum _bbs_net_constant_t
49  {  {
         MAX_PROCESS_BAR_LEN = 30,  
50          MAXSTATION = 26 * 2,          MAXSTATION = 26 * 2,
51          STATION_PER_LINE = 4,          STATION_PER_LINE = 4,
52            ORG_NAME_MAX_LEN = 40,
53            SITE_NAME_MAX_LEN = 40,
54            HOSTNAME_MAX_LEN = 253,
55            PORT_STR_MAX_LEN = 5,
56          USERNAME_MAX_LEN = 20,          USERNAME_MAX_LEN = 20,
57          PASSWORD_MAX_LEN = 20,          PASSWORD_MAX_LEN = 20,
58          SSH_CONNECT_TIMEOUT = 5, // seconds          REMOTE_CONNECT_TIMEOUT = 10, // seconds
59            SSH_CONNECT_TIMEOUT = 5,         // seconds
60            PROGRESS_BAR_LEN = 30,
61  };  };
62    
63  struct _bbsnet_conf  struct _bbsnet_conf
64  {  {
65          char org_name[40];          char org_name[ORG_NAME_MAX_LEN + 1];
66          char site_name[40];          char site_name[SITE_NAME_MAX_LEN + 1];
67          char host_name[IP_ADDR_LEN];          char host_name[HOSTNAME_MAX_LEN + 1];
68          in_port_t port;          char port[PORT_STR_MAX_LEN + 1];
69          int8_t use_ssh;          int8_t use_ssh;
70          char charset[CHARSET_MAX_LEN + 1];          char charset[CHARSET_MAX_LEN + 1];
71  } bbsnet_conf[MAXSTATION];  } bbsnet_conf[MAXSTATION];
72    
73    static const char MENU_CONF_DELIM[] = " \t\r\n";
74    
75  static MENU_SET bbsnet_menu;  static MENU_SET bbsnet_menu;
76    
77  static void unload_bbsnet_conf(void);  static void unload_bbsnet_conf(void);
78    
79  static int load_bbsnet_conf(const char *file_config)  static int load_bbsnet_conf(const char *file_config)
80  {  {
81          FILE *fp;          FILE *fin;
82            int fin_line = 0;
83          MENU *p_menu;          MENU *p_menu;
84          MENU_ITEM *p_menu_item;          MENU_ITEM *p_menu_item;
85          MENU_ITEM_ID menu_item_id;          MENU_ITEM_ID menu_item_id;
86          char line[LINE_BUFFER_LEN], *t1, *t2, *t3, *t4, *t5, *t6, *saveptr;          char line[LINE_BUFFER_LEN];
87          int port;          char *p = NULL;
88            char *saveptr = NULL;
89            long port;
90            char *endptr;
91    
92          unload_bbsnet_conf();          unload_bbsnet_conf();
93    
94          bbsnet_menu.p_menu_pool = calloc(1, sizeof(MENU));          bbsnet_menu.p_menu_pool = calloc(1, sizeof(MENU));
95          if (bbsnet_menu.p_menu_pool == NULL)          if (bbsnet_menu.p_menu_pool == NULL)
96          {          {
97                  log_error("calloc(p_menu_pool) error\n");                  log_error("calloc(p_menu_pool) error");
98                  return -1;                  return -1;
99          }          }
100          bbsnet_menu.menu_count = 1;          bbsnet_menu.menu_count = 1;
# Line 93  static int load_bbsnet_conf(const char * Line 102  static int load_bbsnet_conf(const char *
102          bbsnet_menu.p_menu_item_pool = calloc(MAXSTATION, sizeof(MENU_ITEM));          bbsnet_menu.p_menu_item_pool = calloc(MAXSTATION, sizeof(MENU_ITEM));
103          if (bbsnet_menu.p_menu_item_pool == NULL)          if (bbsnet_menu.p_menu_item_pool == NULL)
104          {          {
105                  log_error("calloc(p_menu_item_pool) error\n");                  log_error("calloc(p_menu_item_pool) error");
106                  unload_bbsnet_conf();                  unload_bbsnet_conf();
107                  return -1;                  return -1;
108          }          }
# Line 106  static int load_bbsnet_conf(const char * Line 115  static int load_bbsnet_conf(const char *
115          p_menu->title.show = 0;          p_menu->title.show = 0;
116          p_menu->screen_show = 0;          p_menu->screen_show = 0;
117    
118          fp = fopen(file_config, "r");          fin = fopen(file_config, "r");
119          if (fp == NULL)          if (fin == NULL)
120          {          {
121                  unload_bbsnet_conf();                  unload_bbsnet_conf();
122                  return -2;                  return -2;
123          }          }
124    
125          menu_item_id = 0;          menu_item_id = 0;
126          while (fgets(line, sizeof(line), fp) && menu_item_id < MAXSTATION)          while (fgets(line, sizeof(line), fin) && menu_item_id < MAXSTATION)
127          {          {
128                  t1 = strtok_r(line, MENU_CONF_DELIM, &saveptr);                  fin_line++;
                 t2 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);  
                 t3 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);  
                 t4 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);  
                 t5 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);  
                 t6 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);  
129    
130                  if (t1 == NULL || t2 == NULL || t3 == NULL || t4 == NULL ||                  p = strtok_r(line, MENU_CONF_DELIM, &saveptr);
131                          t5 == NULL || t6 == NULL || line[0] == '#' || line[0] == '*')                  if (p == NULL) // Blank line
132                  {                  {
133                          continue;                          continue;
134                  }                  }
135    
136                  strncpy(bbsnet_conf[menu_item_id].site_name, t2, sizeof(bbsnet_conf[menu_item_id].site_name) - 1);                  if (*p == '#' || *p == '\r' || *p == '\n') // Comment or blank line
137                  bbsnet_conf[menu_item_id].site_name[sizeof(bbsnet_conf[menu_item_id].site_name) - 1] = '\0';                  {
138                  strncpy(bbsnet_conf[menu_item_id].org_name, t1, sizeof(bbsnet_conf[menu_item_id].org_name) - 1);                          continue;
139                    }
140    
141                    if (strlen(p) > sizeof(bbsnet_conf[menu_item_id].org_name) - 1)
142                    {
143                            log_error("Error org_name in BBSNET config line %d", fin_line);
144                            continue;
145                    }
146                    strncpy(bbsnet_conf[menu_item_id].org_name, p, sizeof(bbsnet_conf[menu_item_id].org_name) - 1);
147                  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';
148                  strncpy(bbsnet_conf[menu_item_id].host_name, t3, sizeof(bbsnet_conf[menu_item_id].host_name) - 1);  
149                    p = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
150                    if (p == NULL || strlen(p) > sizeof(bbsnet_conf[menu_item_id].site_name) - 1)
151                    {
152                            log_error("Error site_name in BBSNET config line %d", fin_line);
153                            continue;
154                    }
155                    strncpy(bbsnet_conf[menu_item_id].site_name, p, sizeof(bbsnet_conf[menu_item_id].site_name) - 1);
156                    bbsnet_conf[menu_item_id].site_name[sizeof(bbsnet_conf[menu_item_id].site_name) - 1] = '\0';
157    
158                    p = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
159                    if (p == NULL || strlen(p) > sizeof(bbsnet_conf[menu_item_id].host_name) - 1)
160                    {
161                            log_error("Error host_name in BBSNET config line %d", fin_line);
162                            continue;
163                    }
164                    strncpy(bbsnet_conf[menu_item_id].host_name, p, sizeof(bbsnet_conf[menu_item_id].host_name) - 1);
165                  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';
166                  port = atoi(t4);  
167                  if (port <= 0 || port > 65535)                  p = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
168                    if (p == NULL || strlen(p) > sizeof(bbsnet_conf[menu_item_id].port) - 1)
169                  {                  {
170                          log_error("Invalid port value %d of menu item %d\n", port, menu_item_id);                          log_error("Error port in BBSNET config line %d", fin_line);
171                          fclose(fp);                          continue;
                         unload_bbsnet_conf();  
                         return -3;  
172                  }                  }
173                  bbsnet_conf[menu_item_id].port = (in_port_t)port;                  port = strtol(p, &endptr, 10);
174                  bbsnet_conf[menu_item_id].use_ssh = (toupper(t5[0]) == 'Y');                  if (*endptr != '\0' || port <= 0 || port > 65535)
175                  strncpy(bbsnet_conf[menu_item_id].charset, t6, sizeof(bbsnet_conf[menu_item_id].charset) - 1);                  {
176                            log_error("Invalid port %ld in BBSNET config line %d", port, fin_line);
177                            continue;
178                    }
179                    strncpy(bbsnet_conf[menu_item_id].port, p, sizeof(bbsnet_conf[menu_item_id].port) - 1);
180                    bbsnet_conf[menu_item_id].port[sizeof(bbsnet_conf[menu_item_id].port) - 1] = '\0';
181    
182                    p = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
183                    if (p == NULL || strlen(p) != 1)
184                    {
185                            log_error("Error use_ssh in BBSNET config line %d", fin_line);
186                            continue;
187                    }
188                    bbsnet_conf[menu_item_id].use_ssh = (toupper(p[0]) == 'Y');
189    
190                    p = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
191                    if (p == NULL || strlen(p) > sizeof(bbsnet_conf[menu_item_id].charset) - 1)
192                    {
193                            log_error("Error charset in BBSNET config line %d", fin_line);
194                            continue;
195                    }
196                    strncpy(bbsnet_conf[menu_item_id].charset, p, sizeof(bbsnet_conf[menu_item_id].charset) - 1);
197                  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';
198    
199                  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);
200                  if (p_menu_item == NULL)                  if (p_menu_item == NULL)
201                  {                  {
202                          log_error("get_menu_item_by_id(%d) return NULL pointer\n", menu_item_id);                          log_error("get_menu_item_by_id(%d) error: NULL pointer", menu_item_id);
203                          fclose(fp);                          fclose(fin);
204                          unload_bbsnet_conf();                          unload_bbsnet_conf();
205                          return -3;                          return -3;
206                  }                  }
# Line 164  static int load_bbsnet_conf(const char * Line 212  static int load_bbsnet_conf(const char *
212                  p_menu_item->priv = 0;                  p_menu_item->priv = 0;
213                  p_menu_item->level = 0;                  p_menu_item->level = 0;
214                  p_menu_item->name[0] =                  p_menu_item->name[0] =
215                          (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);
216                  p_menu_item->name[1] = '\0';                  p_menu_item->name[1] = '\0';
217                  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",
218                                   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 179  static int load_bbsnet_conf(const char * Line 227  static int load_bbsnet_conf(const char *
227          bbsnet_menu.menu_item_pos[0] = 0;          bbsnet_menu.menu_item_pos[0] = 0;
228          bbsnet_menu.choose_step = 0;          bbsnet_menu.choose_step = 0;
229    
230          fclose(fp);          fclose(fin);
231    
232          return 0;          return 0;
233  }  }
# Line 202  static void unload_bbsnet_conf(void) Line 250  static void unload_bbsnet_conf(void)
250          }          }
251  }  }
252    
253  static void process_bar(int n, int len)  static void progress_bar(int percent, int len)
254  {  {
255            char line[LINE_BUFFER_LEN];
256          char buf[LINE_BUFFER_LEN];          char buf[LINE_BUFFER_LEN];
257          char buf2[LINE_BUFFER_LEN];          int pos;
258    
259          if (len <= 0)          if (len < 4)
260            {
261                    len = 4;
262            }
263            else if (len + 2 > LINE_BUFFER_LEN)
264          {          {
265                  len = 1;                  len = LINE_BUFFER_LEN - 3;
266          }          }
267          else if (len > LINE_BUFFER_LEN)          if (percent < 0)
268          {          {
269                  len = LINE_BUFFER_LEN - 1;                  percent = 0;
270          }          }
271          if (n < 0)          else if (percent > 100)
272          {          {
273                  n = 0;                  percent = 100;
274          }          }
275          else if (n > len)  
276            pos = len * percent / 100;
277    
278            line[0] = ' ';
279            for (int i = 1; i <= len; i++)
280          {          {
281                  n = len;                  line[i] = '-';
282          }          }
283            line[len + 1] = ' ';
284            line[len + 2] = '\0';
285    
286            snprintf(buf, sizeof(buf), "%*s%3d%%%*s",
287                             (len - 4) / 2, "", percent, (len - 4 + 1) / 2, "");
288    
289          moveto(4, 1);          moveto(4, 1);
290          prints(" ------------------------------ \r\n");          prints("%s\r\n", line);
291          snprintf(buf, sizeof(buf), "            %3d%%              ", n * 100 / len);          prints("|\033[46m%.*s\033[44m%s\033[m|\r\n", pos, buf, buf + pos);
292          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");  
293          iflush();          iflush();
294  }  }
295    
296    static int connect_timeout(struct timespec *p_ts_begin, struct timespec *p_ts_now,
297                                                       int total_time_ms, int *p_progress_last, int bar_len)
298    {
299            long duration_ms;
300            int progress;
301    
302            if (clock_gettime(CLOCK_REALTIME, p_ts_now) == -1)
303            {
304                    log_error("clock_gettime() error (%d)", errno);
305                    return -1;
306            }
307    
308            duration_ms = (p_ts_now->tv_sec - p_ts_begin->tv_sec) * 1000 +
309                                      (p_ts_now->tv_nsec - p_ts_begin->tv_nsec) / 1000 / 1000;
310    
311            if (duration_ms >= total_time_ms)
312            {
313                    progress_bar(100, bar_len);
314                    prints("\033[1;31m连接超时!\033[m\r\n");
315                    press_any_key();
316    
317                    return 1;
318            }
319    
320            progress = (int)(duration_ms * 100 / total_time_ms) + 1;
321    
322            if (progress != *p_progress_last)
323            {
324                    *p_progress_last = progress;
325                    progress_bar(progress, bar_len);
326            }
327    
328            return 0;
329    }
330    
331  static int bbsnet_connect(int n)  static int bbsnet_connect(int n)
332  {  {
333          int sock = -1;          int sock = -1;
334          int ret;          int ret = 0;
335          int loop;          int loop;
336          int error;          int error;
337          int sock_connected = 0;          int sock_connected = 0;
338            int ssh_connected = 0;
339          int flags_sock = -1;          int flags_sock = -1;
340          int flags_stdin = -1;          int flags_stdin = -1;
341          int flags_stdout = -1;          int flags_stdout = -1;
# Line 273  static int bbsnet_connect(int n) Line 368  static int bbsnet_connect(int n)
368          int stdout_write_wait = 0;          int stdout_write_wait = 0;
369          int sock_read_wait = 0;          int sock_read_wait = 0;
370          int sock_write_wait = 0;          int sock_write_wait = 0;
371          struct hostent *p_host = NULL;          struct addrinfo hints, *res = NULL;
372          int tos;          int tos;
373          char remote_addr[IP_ADDR_LEN];          char remote_addr[INET_ADDRSTRLEN];
374          int remote_port;          int remote_port;
375          char local_addr[IP_ADDR_LEN];          char local_addr[INET_ADDRSTRLEN];
376          int local_port;          int local_port;
377          socklen_t sock_len;          socklen_t sock_len;
378          time_t t_begin;          time_t t_begin, t_used;
379          time_t t_used = time(NULL);          struct timespec ts_begin, ts_now;
380          struct tm *tm_used;          int progress_last = 0;
381          int ch;          int ch;
382          char remote_user[USERNAME_MAX_LEN + 1];          char remote_user[USERNAME_MAX_LEN + 1];
383          char remote_pass[PASSWORD_MAX_LEN + 1];          char remote_pass[PASSWORD_MAX_LEN + 1];
384          ssh_session session = NULL;          ssh_session outbound_session = NULL;
385          ssh_channel channel = NULL;          ssh_channel outbound_channel = NULL;
386          int ssh_process_config = 0;          int ssh_process_config = 0;
387          int ssh_log_level = SSH_LOG_NOLOG;          int ssh_log_level = SSH_LOG_NOLOG;
388    
389          if (user_online_update("BBS_NET") < 0)          if (user_online_update("BBS_NET") < 0)
390          {          {
391                  log_error("user_online_update(BBS_NET) error\n");                  log_error("user_online_update(BBS_NET) error");
392          }          }
393    
394          if (bbsnet_conf[n].use_ssh)          if (bbsnet_conf[n].use_ssh)
# Line 309  static int bbsnet_connect(int n) Line 404  static int bbsnet_connect(int n)
404                  }                  }
405    
406                  moveto(1, 1);                  moveto(1, 1);
407                  prints("通过SSH方式连接[%s]...", bbsnet_conf[n].site_name);                  prints("\033[1;32m正在准备往 %s (%s:%s) 的%s连接... \033[m\r\n",
408                  moveto(2, 1);                             bbsnet_conf[n].site_name, bbsnet_conf[n].host_name, bbsnet_conf[n].port,
409                               (bbsnet_conf[n].use_ssh ? "SSH" : "Telnet"));
410                  prints("请输入用户名: ");                  prints("请输入用户名: ");
411                  iflush();                  iflush();
412                  if (str_input(remote_user, sizeof(remote_user), DOECHO) < 0)                  if (str_input(remote_user, sizeof(remote_user), DOECHO) < 0)
# Line 338  static int bbsnet_connect(int n) Line 434  static int bbsnet_connect(int n)
434          clearscr();          clearscr();
435    
436          moveto(1, 1);          moveto(1, 1);
437          prints("\033[1;32m正在测试往 %s (%s) 的连接,请稍候... \033[m\r\n",          prints("\033[1;32m正在测试往 %s (%s:%s) 的%s连接,请稍候... \033[m\r\n",
438                     bbsnet_conf[n].site_name, bbsnet_conf[n].host_name);                     bbsnet_conf[n].site_name, bbsnet_conf[n].host_name, bbsnet_conf[n].port,
439          iflush();                     (bbsnet_conf[n].use_ssh ? "SSH" : "Telnet"));
440            prints("\033[1;32m连接进行中,按\033[1;33mCtrl+C\033[1;32m中断。\033[m\r\n");
441            progress_bar(0, PROGRESS_BAR_LEN);
442    
443            if (clock_gettime(CLOCK_REALTIME, &ts_begin) == -1)
444            {
445                    log_error("clock_gettime() error (%d)", errno);
446                    ret = -1;
447                    goto cleanup;
448            }
449            ts_now = ts_begin;
450    
451          p_host = gethostbyname(bbsnet_conf[n].host_name);          memset(&hints, 0, sizeof(hints));
452            hints.ai_family = AF_INET;
453            hints.ai_socktype = SOCK_STREAM;
454            hints.ai_protocol = IPPROTO_TCP;
455    
456          if (p_host == NULL)          if ((ret = getaddrinfo(BBS_address, NULL, &hints, &res)) != 0)
457          {          {
458                  prints("\033[1;31m查找主机名失败!\033[m\r\n");                  log_error("getaddrinfo() error (%d): %s", ret, gai_strerror(ret));
459                  press_any_key();                  ret = -1;
460                  goto cleanup;                  goto cleanup;
461          }          }
462    
463          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)
464            {
465                    log_error("inet_ntop() error (%d)", errno);
466                    ret = -1;
467                    goto cleanup;
468            }
469            local_port = ntohs(((struct sockaddr_in *)res->ai_addr)->sin_port);
470    
471            sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
472          if (sock < 0)          if (sock < 0)
473          {          {
474                  prints("\033[1;31m无法创建socket!\033[m\r\n");                  log_error("socket() error (%d)", errno);
475                  press_any_key();                  ret = -1;
476                  goto cleanup;                  goto cleanup;
477          }          }
478    
479          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)  
480          {          {
481                  log_error("Bind address %s:%u failed (%d)\n",                  log_error("bind(%s:%u) error (%d)", local_addr, local_port, errno);
482                                    inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), errno);                  ret = -1;
483                  goto cleanup;                  goto cleanup;
484          }          }
485    
486          memset(&sin, 0, sizeof(sin));          freeaddrinfo(res);
487          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);  
488    
489          strncpy(remote_addr, inet_ntoa(sin.sin_addr), sizeof(remote_addr) - 1);          memset(&hints, 0, sizeof(hints));
490          remote_addr[sizeof(remote_addr) - 1] = '\0';          hints.ai_family = AF_INET;
491          remote_port = ntohs(sin.sin_port);          hints.ai_flags = AI_NUMERICSERV;
492            hints.ai_socktype = SOCK_STREAM;
493            hints.ai_protocol = IPPROTO_TCP;
494    
495          prints("\033[1;32m穿梭进度条提示您当前已使用的时间,按\033[1;33mCtrl+C\033[1;32m中断。\033[m\r\n");          if ((ret = getaddrinfo(bbsnet_conf[n].host_name, bbsnet_conf[n].port, &hints, &res)) != 0)
496          process_bar(0, MAX_PROCESS_BAR_LEN);          {
497                    log_error("getaddrinfo() error (%d): %s", ret, gai_strerror(ret));
498                    prints("\033[1;31m查找主机名失败!\033[m\r\n");
499                    press_any_key();
500                    ret = -1;
501                    goto cleanup;
502            }
503    
504            if (inet_ntop(AF_INET, &(((struct sockaddr_in *)res->ai_addr)->sin_addr), remote_addr, sizeof(remote_addr)) == NULL)
505            {
506                    log_error("inet_ntop() error (%d)", errno);
507                    ret = -1;
508                    goto cleanup;
509            }
510            remote_port = ntohs(((struct sockaddr_in *)res->ai_addr)->sin_port);
511    
512          // Set socket as non-blocking          // Set socket as non-blocking
513          if ((flags_sock = fcntl(sock, F_GETFL, 0)) == -1)          if ((flags_sock = fcntl(sock, F_GETFL, 0)) == -1)
514          {          {
515                  log_error("fcntl(F_GETFL) error (%d)\n", errno);                  log_error("fcntl(F_GETFL) error (%d)", errno);
516                    ret = -1;
517                  goto cleanup;                  goto cleanup;
518          }          }
519          if ((fcntl(sock, F_SETFL, flags_sock | O_NONBLOCK)) == -1)          if ((fcntl(sock, F_SETFL, flags_sock | O_NONBLOCK)) == -1)
520          {          {
521                  log_error("fcntl(F_SETFL) error (%d)\n", errno);                  log_error("fcntl(F_SETFL) error (%d)", errno);
522                    ret = -1;
523                  goto cleanup;                  goto cleanup;
524          }          }
525    
526          // Set STDIN/STDOUT as non-blocking          // Set STDIN/STDOUT as non-blocking
527          if ((flags_stdin = fcntl(STDIN_FILENO, F_GETFL, 0)) == -1)          if ((flags_stdin = fcntl(STDIN_FILENO, F_GETFL, 0)) == -1)
528          {          {
529                  log_error("fcntl(F_GETFL) error (%d)\n", errno);                  log_error("fcntl(F_GETFL) error (%d)", errno);
530                    ret = -1;
531                  goto cleanup;                  goto cleanup;
532          }          }
533          if ((flags_stdout = fcntl(STDOUT_FILENO, F_GETFL, 0)) == -1)          if ((flags_stdout = fcntl(STDOUT_FILENO, F_GETFL, 0)) == -1)
534          {          {
535                  log_error("fcntl(F_GETFL) error (%d)\n", errno);                  log_error("fcntl(F_GETFL) error (%d)", errno);
536                    ret = -1;
537                  goto cleanup;                  goto cleanup;
538          }          }
539          if ((fcntl(STDIN_FILENO, F_SETFL, flags_stdin | O_NONBLOCK)) == -1)          if ((fcntl(STDIN_FILENO, F_SETFL, flags_stdin | O_NONBLOCK)) == -1)
540          {          {
541                  log_error("fcntl(F_SETFL) error (%d)\n", errno);                  log_error("fcntl(F_SETFL) error (%d)", errno);
542                    ret = -1;
543                  goto cleanup;                  goto cleanup;
544          }          }
545          if ((fcntl(STDOUT_FILENO, F_SETFL, flags_stdout | O_NONBLOCK)) == -1)          if ((fcntl(STDOUT_FILENO, F_SETFL, flags_stdout | O_NONBLOCK)) == -1)
546          {          {
547                  log_error("fcntl(F_SETFL) error (%d)\n", errno);                  log_error("fcntl(F_SETFL) error (%d)", errno);
548                    ret = -1;
549                  goto cleanup;                  goto cleanup;
550          }          }
551    
# Line 421  static int bbsnet_connect(int n) Line 553  static int bbsnet_connect(int n)
553          epollfd = epoll_create1(0);          epollfd = epoll_create1(0);
554          if (epollfd < 0)          if (epollfd < 0)
555          {          {
556                  log_error("epoll_create1() error (%d)\n", errno);                  log_error("epoll_create1() error (%d)", errno);
557                    ret = -1;
558                  goto cleanup;                  goto cleanup;
559          }          }
560    
# Line 429  static int bbsnet_connect(int n) Line 562  static int bbsnet_connect(int n)
562          ev.data.fd = sock;          ev.data.fd = sock;
563          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sock, &ev) == -1)          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sock, &ev) == -1)
564          {          {
565                  log_error("epoll_ctl(socket) error (%d)\n", errno);                  log_error("epoll_ctl(socket) error (%d)", errno);
566                    ret = -1;
567                  goto cleanup;                  goto cleanup;
568          }          }
569    
# Line 437  static int bbsnet_connect(int n) Line 571  static int bbsnet_connect(int n)
571          ev.data.fd = STDIN_FILENO;          ev.data.fd = STDIN_FILENO;
572          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1)          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1)
573          {          {
574                  log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno);                  log_error("epoll_ctl(STDIN_FILENO) error (%d)", errno);
575                    ret = -1;
576                  goto cleanup;                  goto cleanup;
577          }          }
578  #endif  #endif
579    
580          while (!SYS_server_exit)          while (!SYS_server_exit &&
581                       !connect_timeout(&ts_begin, &ts_now, REMOTE_CONNECT_TIMEOUT * 1000, &progress_last, PROGRESS_BAR_LEN))
582          {          {
583                  if ((ret = connect(sock, (struct sockaddr *)&sin, sizeof(sin))) < 0)                  if ((ret = connect(sock, res->ai_addr, res->ai_addrlen)) == 0)
584                    {
585                            sock_connected = 1;
586                            break;
587                    }
588                    else if (ret < 0)
589                  {                  {
590                          if (errno == EAGAIN || errno == EALREADY || errno == EINPROGRESS)                          if (errno == EAGAIN || errno == EALREADY || errno == EINPROGRESS)
591                          {                          {
# Line 455  static int bbsnet_connect(int n) Line 596  static int bbsnet_connect(int n)
596                          }                          }
597                          else if (errno == EINTR)                          else if (errno == EINTR)
598                          {                          {
                                 continue;  
599                          }                          }
600                          else                          else
601                          {                          {
602                                  log_error("connect(socket) error (%d)\n", errno);                                  log_error("connect(socket) error (%d)", errno);
   
603                                  prints("\033[1;31m连接失败!\033[m\r\n");                                  prints("\033[1;31m连接失败!\033[m\r\n");
604                                  press_any_key();                                  press_any_key();
605                                    ret = -1;
606                                  goto cleanup;                                  goto cleanup;
607                          }                          }
608                  }                  }
609          }          }
610    
611          for (int j = 0; j < MAX_PROCESS_BAR_LEN && !sock_connected && !SYS_server_exit; j++)          while (!SYS_server_exit && !sock_connected &&
612                       !connect_timeout(&ts_begin, &ts_now, REMOTE_CONNECT_TIMEOUT * 1000, &progress_last, PROGRESS_BAR_LEN))
613          {          {
614  #ifdef HAVE_SYS_EPOLL_H  #ifdef HAVE_SYS_EPOLL_H
615                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 500); // 0.5 second                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second
616                  ret = nfds;                  ret = nfds;
617  #else  #else
618                  pfds[0].fd = sock;                  pfds[0].fd = sock;
# Line 480  static int bbsnet_connect(int n) Line 620  static int bbsnet_connect(int n)
620                  pfds[1].fd = STDIN_FILENO;                  pfds[1].fd = STDIN_FILENO;
621                  pfds[1].events = POLLIN;                  pfds[1].events = POLLIN;
622                  nfds = 2;                  nfds = 2;
623                  ret = poll(pfds, (nfds_t)nfds, 500); // 0.5 second                  ret = poll(pfds, (nfds_t)nfds, 100); // 0.1 second
624  #endif  #endif
625    
626                  if (ret < 0)                  if (ret < 0)
# Line 488  static int bbsnet_connect(int n) Line 628  static int bbsnet_connect(int n)
628                          if (errno != EINTR)                          if (errno != EINTR)
629                          {                          {
630  #ifdef HAVE_SYS_EPOLL_H  #ifdef HAVE_SYS_EPOLL_H
631                                  log_error("epoll_wait() error (%d)\n", errno);                                  log_error("epoll_wait() error (%d)", errno);
632  #else  #else
633                                  log_error("poll() error (%d)\n", errno);                                  log_error("poll() error (%d)", errno);
634  #endif  #endif
635                                  break;                                  break;
636                          }                          }
637                  }                  }
638                  else if (ret == 0) // timeout                  else if (ret == 0) // timeout
639                  {                  {
                         process_bar(j + 1, MAX_PROCESS_BAR_LEN);  
640                  }                  }
641                  else // ret > 0                  else // ret > 0
642                  {                  {
# Line 512  static int bbsnet_connect(int n) Line 651  static int bbsnet_connect(int n)
651                                          socklen_t len = sizeof(error);                                          socklen_t len = sizeof(error);
652                                          if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len) < 0)                                          if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
653                                          {                                          {
654                                                  log_error("getsockopt() error (%d) !\n", errno);                                                  log_error("getsockopt() error (%d) !", errno);
655                                                    ret = -1;
656                                                  goto cleanup;                                                  goto cleanup;
657                                          }                                          }
658                                          if (error == 0)                                          if (error == 0)
659                                          {                                          {
660                                                  sock_connected = 1;                                                  sock_connected = 1;
661                                                    break;
662                                          }                                          }
663                                  }                                  }
664  #ifdef HAVE_SYS_EPOLL_H  #ifdef HAVE_SYS_EPOLL_H
# Line 532  static int bbsnet_connect(int n) Line 673  static int bbsnet_connect(int n)
673                                          } while (ch == 0);                                          } while (ch == 0);
674                                          if (ch == Ctrl('C') || ch == KEY_ESC)                                          if (ch == Ctrl('C') || ch == KEY_ESC)
675                                          {                                          {
676                                                    ret = 0;
677                                                  goto cleanup;                                                  goto cleanup;
678                                          }                                          }
679                                  }                                  }
680                          }                          }
681                  }                  }
682          }          }
         if (SYS_server_exit)  
         {  
                 goto cleanup;  
         }  
683          if (!sock_connected)          if (!sock_connected)
684          {          {
685                  prints("\033[1;31m连接失败!\033[m\r\n");                  ret = -1;
                 press_any_key();  
   
686                  goto cleanup;                  goto cleanup;
687          }          }
688    
689          tos = IPTOS_LOWDELAY;          tos = IPTOS_LOWDELAY;
690          if (setsockopt(sock, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)          if (setsockopt(sock, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)
691          {          {
692                  log_error("setsockopt IP_TOS=%d error (%d)\n", tos, errno);                  log_error("setsockopt IP_TOS=%d error (%d)", tos, errno);
693          }          }
694    
695          sock_len = sizeof(sin);          sock_len = sizeof(sin);
696          if (getsockname(sock, (struct sockaddr *)&sin, &sock_len) < 0)          if (getsockname(sock, (struct sockaddr *)&sin, &sock_len) < 0)
697          {          {
698                  log_error("getsockname() error: %d", errno);                  log_error("getsockname() error: %d", errno);
699                    ret = -1;
700                  goto cleanup;                  goto cleanup;
701          }          }
702    
703          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)
704          local_addr[sizeof(local_addr) - 1] = '\0';          {
705                    log_error("inet_ntop() error (%d)", errno);
706                    ret = -1;
707                    goto cleanup;
708            }
709          local_port = ntohs(sin.sin_port);          local_port = ntohs(sin.sin_port);
710    
711          if (bbsnet_conf[n].use_ssh)          if (bbsnet_conf[n].use_ssh)
712          {          {
713                  session = ssh_new();                  outbound_session = ssh_new();
714                  if (session == NULL)                  if (outbound_session == NULL)
715                  {                  {
716                          log_error("ssh_new() error\n");                          log_error("ssh_new() error");
717                            ret = -1;
718                          goto cleanup;                          goto cleanup;
719                  }                  }
720    
721                  if (ssh_options_set(session, SSH_OPTIONS_FD, &sock) < 0 ||                  if (ssh_options_set(outbound_session, SSH_OPTIONS_FD, &sock) < 0 ||
722                          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 ||
723                          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 ||
724                          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 ||
725                          ssh_options_set(session, SSH_OPTIONS_USER, remote_user) < 0 ||                          ssh_options_set(outbound_session, SSH_OPTIONS_USER, remote_user) < 0 ||
726                          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 ||
727                          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)
728                  {                  {
729                          log_error("Error setting SSH options: %s\n", ssh_get_error(session));                          log_error("Error setting SSH options: %s", ssh_get_error(outbound_session));
730                            ret = -1;
731                          goto cleanup;                          goto cleanup;
732                  }                  }
733    
734                  ssh_set_blocking(session, 0);                  ssh_set_blocking(outbound_session, 0);
735    
736                  t_begin = time(NULL);                  ssh_connected = 0;
737                  ret = SSH_ERROR;                  while (!SYS_server_exit &&
738                  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))
739                  {                  {
740                          ret = ssh_connect(session);                          ret = ssh_connect(outbound_session);
741                          if (ret == SSH_OK)                          if (ret == SSH_OK)
742                          {                          {
743                                    ssh_connected = 1;
744                                  break;                                  break;
745                          }                          }
746                          else if (ret == SSH_AGAIN)                          else if (ret == SSH_AGAIN)
747                          {                          {
748                                  // log_error("ssh_connect() error: SSH_AGAIN\n");                                  // log_debug("ssh_connect() error: SSH_AGAIN");
749                          }                          }
750                          else // if (ret == SSH_ERROR)                          else // if (ret == SSH_ERROR)
751                          {                          {
752                                  log_error("ssh_connect() error: SSH_ERROR\n");                                  log_error("ssh_connect() error: SSH_ERROR");
753                                    ret = -1;
754                                  goto cleanup;                                  goto cleanup;
755                          }                          }
756                  }                  }
757                  if (ret != SSH_OK)                  if (!ssh_connected)
758                  {                  {
759                          prints("\033[1;31m连接超时!\033[m\r\n");                          ret = -1;
                         press_any_key();  
760                          goto cleanup;                          goto cleanup;
761                  }                  }
762    
763                  ret = ssh_session_is_known_server(session);                  ret = ssh_session_is_known_server(outbound_session);
764                  switch (ret)                  switch (ret)
765                  {                  {
766                  case SSH_KNOWN_HOSTS_NOT_FOUND:                  case SSH_KNOWN_HOSTS_NOT_FOUND:
767                  case SSH_KNOWN_HOSTS_UNKNOWN:                  case SSH_KNOWN_HOSTS_UNKNOWN:
768                          if (ssh_session_update_known_hosts(session) != SSH_OK)                          if (ssh_session_update_known_hosts(outbound_session) != SSH_OK)
769                          {                          {
770                                  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);
771                                  prints("\033[1;31m无法添加服务器证书\033[m\r\n");                                  prints("\033[1;31m无法添加服务器证书\033[m\r\n");
772                                  press_any_key();                                  press_any_key();
773                                    ret = -1;
774                                  goto cleanup;                                  goto cleanup;
775                          }                          }
776                          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);
777                  case SSH_KNOWN_HOSTS_OK:                  case SSH_KNOWN_HOSTS_OK:
778                          break;                          break;
779                  case SSH_KNOWN_HOSTS_CHANGED:                  case SSH_KNOWN_HOSTS_CHANGED:
780                  case SSH_KNOWN_HOSTS_OTHER:                  case SSH_KNOWN_HOSTS_OTHER:
781                          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);
782                          prints("\033[1;31m服务器证书已变更\033[m\r\n");                          prints("\033[1;31m服务器证书已变更\033[m\r\n");
783                          press_any_key();                          press_any_key();
784                            ret = -1;
785                          goto cleanup;                          goto cleanup;
786                  }                  }
787    
788                  ret = SSH_AUTH_ERROR;                  ssh_connected = 0;
789                  while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT)                  while (!SYS_server_exit &&
790                               !connect_timeout(&ts_begin, &ts_now, REMOTE_CONNECT_TIMEOUT * 1000, &progress_last, PROGRESS_BAR_LEN))
791                  {                  {
792                          ret = ssh_userauth_password(session, NULL, remote_pass);                          ret = ssh_userauth_password(outbound_session, NULL, remote_pass);
793                          if (ret == SSH_AUTH_SUCCESS)                          if (ret == SSH_AUTH_SUCCESS)
794                          {                          {
795                                    ssh_connected = 1;
796                                  break;                                  break;
797                          }                          }
798                          else if (ret == SSH_AUTH_AGAIN)                          else if (ret == SSH_AUTH_AGAIN)
799                          {                          {
800                                  // log_error("ssh_userauth_password() error: SSH_AUTH_AGAIN\n");                                  // log_debug("ssh_userauth_password() error: SSH_AUTH_AGAIN");
801                          }                          }
802                          else if (ret == SSH_AUTH_ERROR)                          else if (ret == SSH_AUTH_ERROR)
803                          {                          {
804                                  log_error("ssh_userauth_password() error: SSH_AUTH_ERROR\n");                                  log_error("ssh_userauth_password() error: SSH_AUTH_ERROR");
805                                    ret = -1;
806                                  goto cleanup;                                  goto cleanup;
807                          }                          }
808                          else // if (ret == SSH_AUTH_DENIED)                          else // if (ret == SSH_AUTH_DENIED)
809                          {                          {
810                                  log_error("ssh_userauth_password() error: SSH_AUTH_DENIED\n");                                  log_debug("ssh_userauth_password() error: SSH_AUTH_DENIED");
811                                  prints("\033[1;31m身份验证失败!\033[m\r\n");                                  prints("\033[1;31m身份验证失败!\033[m\r\n");
812                                  press_any_key();                                  press_any_key();
813                                    ret = 0;
814                                  goto cleanup;                                  goto cleanup;
815                          }                          }
816                  }                  }
817                  if (ret != SSH_AUTH_SUCCESS)                  if (!ssh_connected)
818                  {                  {
819                          prints("\033[1;31m连接超时!\033[m\r\n");                          ret = -1;
                         press_any_key();  
820                          goto cleanup;                          goto cleanup;
821                  }                  }
822    
823                  channel = ssh_channel_new(session);                  outbound_channel = ssh_channel_new(outbound_session);
824                  if (channel == NULL)                  if (outbound_channel == NULL)
825                  {                  {
826                          log_error("ssh_channel_new() error\n");                          log_error("ssh_channel_new() error");
827                            ret = -1;
828                          goto cleanup;                          goto cleanup;
829                  }                  }
830    
831                  ret = SSH_ERROR;                  ssh_connected = 0;
832                  while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT)                  while (!SYS_server_exit &&
833                               !connect_timeout(&ts_begin, &ts_now, REMOTE_CONNECT_TIMEOUT * 1000, &progress_last, PROGRESS_BAR_LEN))
834                  {                  {
835                          ret = ssh_channel_open_session(channel);                          ret = ssh_channel_open_session(outbound_channel);
836                          if (ret == SSH_OK)                          if (ret == SSH_OK)
837                          {                          {
838                                    ssh_connected = 1;
839                                  break;                                  break;
840                          }                          }
841                          else if (ret == SSH_AGAIN)                          else if (ret == SSH_AGAIN)
842                          {                          {
843                                  // log_error("ssh_channel_open_session() error: SSH_AGAIN\n");                                  // log_debug("ssh_channel_open_session() error: SSH_AGAIN");
844                          }                          }
845                          else // if (ret == SSH_ERROR)                          else // if (ret == SSH_ERROR)
846                          {                          {
847                                  log_error("ssh_channel_open_session() error: SSH_ERROR\n");                                  log_error("ssh_channel_open_session() error: SSH_ERROR");
848                                    ret = -1;
849                                  goto cleanup;                                  goto cleanup;
850                          }                          }
851                  }                  }
852                  if (ret != SSH_OK)                  if (!ssh_connected)
853                  {                  {
854                          prints("\033[1;31m连接超时!\033[m\r\n");                          ret = -1;
                         press_any_key();  
855                          goto cleanup;                          goto cleanup;
856                  }                  }
857    
858                  ret = SSH_ERROR;                  ssh_connected = 0;
859                  while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT)                  while (!SYS_server_exit &&
860                               !connect_timeout(&ts_begin, &ts_now, REMOTE_CONNECT_TIMEOUT * 1000, &progress_last, PROGRESS_BAR_LEN))
861                  {                  {
862                          ret = ssh_channel_request_pty(channel);                          ret = ssh_channel_request_pty(outbound_channel);
863                          if (ret == SSH_OK)                          if (ret == SSH_OK)
864                          {                          {
865                                    ssh_connected = 1;
866                                  break;                                  break;
867                          }                          }
868                          else if (ret == SSH_AGAIN)                          else if (ret == SSH_AGAIN)
869                          {                          {
870                                  // log_error("ssh_channel_request_pty() error: SSH_AGAIN\n");                                  // log_debug("ssh_channel_request_pty() error: SSH_AGAIN");
871                          }                          }
872                          else // if (ret == SSH_ERROR)                          else // if (ret == SSH_ERROR)
873                          {                          {
874                                  log_error("ssh_channel_request_pty() error: SSH_ERROR\n");                                  log_error("ssh_channel_request_pty() error: SSH_ERROR");
875                                    ret = -1;
876                                  goto cleanup;                                  goto cleanup;
877                          }                          }
878                  }                  }
879                  if (ret != SSH_OK)                  if (!ssh_connected)
880                  {                  {
881                          prints("\033[1;31m连接超时!\033[m\r\n");                          ret = -1;
                         press_any_key();  
882                          goto cleanup;                          goto cleanup;
883                  }                  }
884    
885                  ret = SSH_ERROR;                  ssh_connected = 0;
886                  while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT)                  while (!SYS_server_exit &&
887                               !connect_timeout(&ts_begin, &ts_now, REMOTE_CONNECT_TIMEOUT * 1000, &progress_last, PROGRESS_BAR_LEN))
888                  {                  {
889                          ret = ssh_channel_request_shell(channel);                          ret = ssh_channel_request_shell(outbound_channel);
890                          if (ret == SSH_OK)                          if (ret == SSH_OK)
891                          {                          {
892                                    ssh_connected = 1;
893                                  break;                                  break;
894                          }                          }
895                          else if (ret == SSH_AGAIN)                          else if (ret == SSH_AGAIN)
896                          {                          {
897                                  // log_error("ssh_channel_request_shell() error: SSH_AGAIN\n");                                  // log_debug("ssh_channel_request_shell() error: SSH_AGAIN");
898                          }                          }
899                          else // if (ret == SSH_ERROR)                          else // if (ret == SSH_ERROR)
900                          {                          {
901                                  log_error("ssh_channel_request_shell() error: SSH_ERROR\n");                                  log_error("ssh_channel_request_shell() error: SSH_ERROR");
902                                    ret = -1;
903                                  goto cleanup;                                  goto cleanup;
904                          }                          }
905                  }                  }
906                  if (ret != SSH_OK)                  if (!ssh_connected)
907                  {                  {
908                          prints("\033[1;31m连接超时!\033[m\r\n");                          ret = -1;
                         press_any_key();  
909                          goto cleanup;                          goto cleanup;
910                  }                  }
911          }          }
912    
913          prints("\033[1;31m连接成功!\033[m\r\n");          prints("\033[1;31m连接成功!\033[m\r\n");
914          iflush();          iflush();
915          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]",
916                             remote_addr, remote_port, local_addr, local_port, BBS_username);                             remote_addr, remote_port, local_addr, local_port, BBS_username);
917    
918          snprintf(tocode, sizeof(tocode), "%s%s", bbsnet_conf[n].charset,          snprintf(tocode, sizeof(tocode), "%s%s", bbsnet_conf[n].charset,
# Line 764  static int bbsnet_connect(int n) Line 920  static int bbsnet_connect(int n)
920          input_cd = iconv_open(tocode, stdio_charset);          input_cd = iconv_open(tocode, stdio_charset);
921          if (input_cd == (iconv_t)(-1))          if (input_cd == (iconv_t)(-1))
922          {          {
923                  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);
924                    ret = -1;
925                  goto cleanup;                  goto cleanup;
926          }          }
927    
# Line 773  static int bbsnet_connect(int n) Line 930  static int bbsnet_connect(int n)
930          output_cd = iconv_open(tocode, bbsnet_conf[n].charset);          output_cd = iconv_open(tocode, bbsnet_conf[n].charset);
931          if (output_cd == (iconv_t)(-1))          if (output_cd == (iconv_t)(-1))
932          {          {
933                  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);
934                    ret = -1;
935                  goto cleanup;                  goto cleanup;
936          }          }
937    
# Line 782  static int bbsnet_connect(int n) Line 940  static int bbsnet_connect(int n)
940          ev.data.fd = sock;          ev.data.fd = sock;
941          if (epoll_ctl(epollfd, EPOLL_CTL_MOD, sock, &ev) == -1)          if (epoll_ctl(epollfd, EPOLL_CTL_MOD, sock, &ev) == -1)
942          {          {
943                  log_error("epoll_ctl(socket) error (%d)\n", errno);                  log_error("epoll_ctl(socket) error (%d)", errno);
944                    ret = -1;
945                  goto cleanup;                  goto cleanup;
946          }          }
947    
# Line 790  static int bbsnet_connect(int n) Line 949  static int bbsnet_connect(int n)
949          ev.data.fd = STDOUT_FILENO;          ev.data.fd = STDOUT_FILENO;
950          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1)          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1)
951          {          {
952                  log_error("epoll_ctl(STDOUT_FILENO) error (%d)\n", errno);                  log_error("epoll_ctl(STDOUT_FILENO) error (%d)", errno);
953                    ret = -1;
954                  goto cleanup;                  goto cleanup;
955          }          }
956  #endif  #endif
957    
958          BBS_last_access_tm = t_used = time(NULL);          BBS_last_access_tm = t_begin = time(NULL);
959          loop = 1;          loop = 1;
960    
961          while (loop && !SYS_server_exit)          while (loop && !SYS_server_exit)
962          {          {
963                  if (SSH_v2 && ssh_channel_is_closed(SSH_channel))                  if (SSH_v2 && ssh_channel_is_closed(SSH_channel))
964                  {                  {
965  #ifdef _DEBUG                          log_debug("SSH channel is closed");
                         log_error("SSH channel is closed\n");  
 #endif  
966                          loop = 0;                          loop = 0;
967                          break;                          break;
968                  }                  }
969    
970                  if (bbsnet_conf[n].use_ssh && ssh_channel_is_closed(channel))                  if (bbsnet_conf[n].use_ssh && ssh_channel_is_closed(outbound_channel))
971                  {                  {
972  #ifdef _DEBUG                          log_debug("Outbound channel is closed");
                         log_error("Remote SSH channel is closed\n");  
 #endif  
973                          loop = 0;                          loop = 0;
974                          break;                          break;
975                  }                  }
# Line 837  static int bbsnet_connect(int n) Line 993  static int bbsnet_connect(int n)
993                          if (errno != EINTR)                          if (errno != EINTR)
994                          {                          {
995  #ifdef HAVE_SYS_EPOLL_H  #ifdef HAVE_SYS_EPOLL_H
996                                  log_error("epoll_wait() error (%d)\n", errno);                                  log_error("epoll_wait() error (%d)", errno);
997  #else  #else
998                                  log_error("poll() error (%d)\n", errno);                                  log_error("poll() error (%d)", errno);
999  #endif  #endif
1000                                  break;                                  break;
1001                          }                          }
# Line 849  static int bbsnet_connect(int n) Line 1005  static int bbsnet_connect(int n)
1005                  {                  {
1006                          if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time)                          if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time)
1007                          {                          {
1008                                    log_debug("User input timeout");
1009                                  break;                                  break;
1010                          }                          }
1011                  }                  }
# Line 856  static int bbsnet_connect(int n) Line 1013  static int bbsnet_connect(int n)
1013                  for (int i = 0; i < nfds; i++)                  for (int i = 0; i < nfds; i++)
1014                  {                  {
1015  #ifdef HAVE_SYS_EPOLL_H  #ifdef HAVE_SYS_EPOLL_H
1016                          if (events[i].data.fd == STDIN_FILENO)                          if (events[i].events & (EPOLLHUP | EPOLLERR))
1017    #else
1018                            if (pfds[i].revents & (POLLHUP | POLLERR))
1019    #endif
1020                            {
1021    #ifdef HAVE_SYS_EPOLL_H
1022                                    log_debug("FD (%d) error events (%d)", events[i].data.fd, events[i].events);
1023    #else
1024                                    log_debug("FD (%d) error events (%d)", pfds[i].fd, pfds[i].revents);
1025    #endif
1026                                    loop = 0;
1027                                    break;
1028                            }
1029    
1030    #ifdef HAVE_SYS_EPOLL_H
1031                            if (events[i].data.fd == STDIN_FILENO && (events[i].events & EPOLLIN))
1032  #else  #else
1033                          if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & POLLIN))                          if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & POLLIN))
1034  #endif  #endif
# Line 890  static int bbsnet_connect(int n) Line 1062  static int bbsnet_connect(int n)
1062                          }                          }
1063    
1064  #ifdef HAVE_SYS_EPOLL_H  #ifdef HAVE_SYS_EPOLL_H
1065                          if (events[i].data.fd == STDOUT_FILENO)                          if (events[i].data.fd == STDOUT_FILENO && (events[i].events & EPOLLOUT))
1066  #else  #else
1067                          if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & POLLOUT))                          if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & POLLOUT))
1068  #endif  #endif
# Line 908  static int bbsnet_connect(int n) Line 1080  static int bbsnet_connect(int n)
1080                                          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);
1081                                          if (ret == SSH_ERROR)                                          if (ret == SSH_ERROR)
1082                                          {                                          {
1083  #ifdef _DEBUG                                                  log_debug("ssh_channel_read_nonblocking() error: %s", ssh_get_error(SSH_session));
                                                 log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session));  
 #endif  
1084                                                  loop = 0;                                                  loop = 0;
1085                                                  break;                                                  break;
1086                                          }                                          }
# Line 925  static int bbsnet_connect(int n) Line 1095  static int bbsnet_connect(int n)
1095                                                  // Send NO-OP to remote server                                                  // Send NO-OP to remote server
1096                                                  input_buf[input_buf_len] = '\0';                                                  input_buf[input_buf_len] = '\0';
1097                                                  input_buf_len++;                                                  input_buf_len++;
                                                 BBS_last_access_tm = time(NULL);  
1098    
1099                                                    BBS_last_access_tm = time(NULL);
1100                                                  stdin_read_wait = 0;                                                  stdin_read_wait = 0;
1101                                                  break; // Check whether channel is still open                                                  break; // Check whether channel is still open
1102                                          }                                          }
# Line 948  static int bbsnet_connect(int n) Line 1118  static int bbsnet_connect(int n)
1118                                          }                                          }
1119                                          else                                          else
1120                                          {                                          {
1121                                                  log_error("read(STDIN) error (%d)\n", errno);                                                  log_error("read(STDIN) error (%d)", errno);
1122                                                  loop = 0;                                                  loop = 0;
1123                                                  break;                                                  break;
1124                                          }                                          }
1125                                  }                                  }
1126                                  else if (ret == 0) // broken pipe                                  else if (ret == 0) // broken pipe
1127                                  {                                  {
1128  #ifdef _DEBUG                                          log_debug("read(STDIN) EOF");
                                         log_error("read(STDIN) EOF\n");  
 #endif  
1129                                          stdin_read_wait = 0;                                          stdin_read_wait = 0;
1130                                          loop = 0;                                          loop = 0;
1131                                          break;                                          break;
# Line 970  static int bbsnet_connect(int n) Line 1138  static int bbsnet_connect(int n)
1138                                          // Refresh current action while user input                                          // Refresh current action while user input
1139                                          if (user_online_update("BBS_NET") < 0)                                          if (user_online_update("BBS_NET") < 0)
1140                                          {                                          {
1141                                                  log_error("user_online_update(BBS_NET) error\n");                                                  log_error("user_online_update(BBS_NET) error");
1142                                          }                                          }
1143    
1144                                          continue;                                          continue;
# Line 986  static int bbsnet_connect(int n) Line 1154  static int bbsnet_connect(int n)
1154  #ifdef _DEBUG  #ifdef _DEBUG
1155                                  for (int j = input_buf_offset; j < input_buf_len; j++)                                  for (int j = input_buf_offset; j < input_buf_len; j++)
1156                                  {                                  {
1157                                          log_error("Debug input: <--[%u]\n", (input_buf[j] + 256) % 256);                                          log_debug("input: <--[%u]", (unsigned char)(input_buf[j]));
1158                                  }                                  }
1159  #endif  #endif
1160    
1161                                  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);
1162                                  if (ret < 0)                                  if (ret < 0)
1163                                  {                                  {
1164                                          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);
1165                                          input_buf_len = input_buf_offset; // Discard invalid sequence                                          input_buf_len = input_buf_offset; // Discard invalid sequence
1166                                  }                                  }
1167    
# Line 1001  static int bbsnet_connect(int n) Line 1169  static int bbsnet_connect(int n)
1169  #ifdef _DEBUG  #ifdef _DEBUG
1170                                  for (int j = input_conv_offset; j < input_conv_len; j++)                                  for (int j = input_conv_offset; j < input_conv_len; j++)
1171                                  {                                  {
1172                                          log_error("Debug input_conv: <--[%u]\n", (input_conv[j] + 256) % 256);                                          log_debug("input_conv: <--[%u]", (unsigned char)(input_conv[j]));
1173                                  }                                  }
1174  #endif  #endif
1175                          }                          }
# Line 1010  static int bbsnet_connect(int n) Line 1178  static int bbsnet_connect(int n)
1178                          {                          {
1179                                  if (bbsnet_conf[n].use_ssh)                                  if (bbsnet_conf[n].use_ssh)
1180                                  {                                  {
1181                                          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));
1182                                          if (ret == SSH_ERROR)                                          if (ret == SSH_ERROR)
1183                                          {                                          {
1184  #ifdef _DEBUG                                                  log_debug("ssh_channel_write() error: %s", ssh_get_error(outbound_session));
                                                 log_error("ssh_channel_write() error: %s\n", ssh_get_error(session));  
 #endif  
1185                                                  loop = 0;                                                  loop = 0;
1186                                                  break;                                                  break;
1187                                          }                                          }
# Line 1037  static int bbsnet_connect(int n) Line 1203  static int bbsnet_connect(int n)
1203                                          }                                          }
1204                                          else                                          else
1205                                          {                                          {
1206  #ifdef _DEBUG                                                  log_debug("write(socket) error (%d)", errno);
                                                 log_error("write(socket) error (%d)\n", errno);  
 #endif  
1207                                                  loop = 0;                                                  loop = 0;
1208                                                  break;                                                  break;
1209                                          }                                          }
1210                                  }                                  }
1211                                  else if (ret == 0) // broken pipe                                  else if (ret == 0) // broken pipe
1212                                  {                                  {
1213  #ifdef _DEBUG                                          log_debug("write(socket) EOF");
                                         log_error("write(socket) EOF\n");  
 #endif  
1214                                          sock_write_wait = 0;                                          sock_write_wait = 0;
1215                                          loop = 0;                                          loop = 0;
1216                                          break;                                          break;
# Line 1073  static int bbsnet_connect(int n) Line 1235  static int bbsnet_connect(int n)
1235                          {                          {
1236                                  if (bbsnet_conf[n].use_ssh)                                  if (bbsnet_conf[n].use_ssh)
1237                                  {                                  {
1238                                          ret = ssh_channel_read_nonblocking(channel, output_buf + output_buf_len,                                          ret = ssh_channel_read_nonblocking(outbound_channel, output_buf + output_buf_len,
1239                                                                                                             (uint32_t)(sizeof(output_buf) - (size_t)output_buf_len), 0);                                                                                                             (uint32_t)(sizeof(output_buf) - (size_t)output_buf_len), 0);
1240                                          if (ret == SSH_ERROR)                                          if (ret == SSH_ERROR)
1241                                          {                                          {
1242  #ifdef _DEBUG                                                  log_debug("ssh_channel_read_nonblocking() error: %s", ssh_get_error(outbound_session));
                                                 log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(session));  
 #endif  
1243                                                  loop = 0;                                                  loop = 0;
1244                                                  break;                                                  break;
1245                                          }                                          }
# Line 1112  static int bbsnet_connect(int n) Line 1272  static int bbsnet_connect(int n)
1272                                          }                                          }
1273                                          else                                          else
1274                                          {                                          {
1275  #ifdef _DEBUG                                                  log_debug("read(socket) error (%d)", errno);
                                                 log_error("read(socket) error (%d)\n", errno);  
 #endif  
1276                                                  loop = 0;                                                  loop = 0;
1277                                                  break;                                                  break;
1278                                          }                                          }
1279                                  }                                  }
1280                                  else if (ret == 0) // broken pipe                                  else if (ret == 0) // broken pipe
1281                                  {                                  {
1282  #ifdef _DEBUG                                          log_debug("read(socket) EOF");
                                         log_error("read(socket) EOF\n");  
 #endif  
1283                                          sock_read_wait = 0;                                          sock_read_wait = 0;
1284                                          loop = 0;                                          loop = 0;
1285                                          break;                                          break;
# Line 1143  static int bbsnet_connect(int n) Line 1299  static int bbsnet_connect(int n)
1299                                  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);
1300                                  if (ret < 0)                                  if (ret < 0)
1301                                  {                                  {
1302                                          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);
1303                                          output_buf_len = output_buf_offset; // Discard invalid sequence                                          output_buf_len = output_buf_offset; // Discard invalid sequence
1304                                  }                                  }
1305                          }                          }
# Line 1155  static int bbsnet_connect(int n) Line 1311  static int bbsnet_connect(int n)
1311                                          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));
1312                                          if (ret == SSH_ERROR)                                          if (ret == SSH_ERROR)
1313                                          {                                          {
1314  #ifdef _DEBUG                                                  log_debug("ssh_channel_write() error: %s", ssh_get_error(SSH_session));
                                                 log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session));  
 #endif  
1315                                                  loop = 0;                                                  loop = 0;
1316                                                  break;                                                  break;
1317                                          }                                          }
# Line 1179  static int bbsnet_connect(int n) Line 1333  static int bbsnet_connect(int n)
1333                                          }                                          }
1334                                          else                                          else
1335                                          {                                          {
1336  #ifdef _DEBUG                                                  log_debug("write(STDOUT) error (%d)", errno);
                                                 log_error("write(STDOUT) error (%d)\n", errno);  
 #endif  
1337                                                  loop = 0;                                                  loop = 0;
1338                                                  break;                                                  break;
1339                                          }                                          }
1340                                  }                                  }
1341                                  else if (ret == 0) // broken pipe                                  else if (ret == 0) // broken pipe
1342                                  {                                  {
1343  #ifdef _DEBUG                                          log_debug("write(STDOUT) EOF");
                                         log_error("write(STDOUT) EOF\n");  
 #endif  
1344                                          stdout_write_wait = 0;                                          stdout_write_wait = 0;
1345                                          loop = 0;                                          loop = 0;
1346                                          break;                                          break;
# Line 1210  static int bbsnet_connect(int n) Line 1360  static int bbsnet_connect(int n)
1360                  }                  }
1361          }          }
1362    
1363            ret = 1; // Normal disconnect
1364            BBS_last_access_tm = time(NULL);
1365            t_used = BBS_last_access_tm - t_begin;
1366            log_common("BBSNET disconnect, %ld days %ld hours %ld minutes %ld seconds used",
1367                               t_used / 86400, t_used % 86400 / 3600, t_used % 3600 / 60, t_used % 60);
1368    
1369  cleanup:  cleanup:
1370          if (input_cd != (iconv_t)(-1))          // Clear sensitive data
1371            memset(remote_pass, 0, sizeof(remote_pass));
1372            memset(remote_user, 0, sizeof(remote_user));
1373    
1374            if (input_cd != (iconv_t)(-1) && iconv_close(input_cd) < 0)
1375          {          {
1376                  iconv_close(input_cd);                  log_error("iconv_close(input) error (%d)", errno);
1377          }          }
1378          if (output_cd != (iconv_t)(-1))          if (output_cd != (iconv_t)(-1) && iconv_close(output_cd) < 0)
1379          {          {
1380                  iconv_close(output_cd);                  log_error("iconv_close(output) error (%d)", errno);
1381          }          }
1382    
1383  #ifdef HAVE_SYS_EPOLL_H  #ifdef HAVE_SYS_EPOLL_H
1384          if (epollfd != -1 && close(epollfd) < 0)          if (epollfd != -1 && close(epollfd) < 0)
1385          {          {
1386                  log_error("close(epoll) error (%d)\n");                  log_error("close(epoll) error (%d)");
1387          }          }
1388  #endif  #endif
1389    
1390          if (bbsnet_conf[n].use_ssh)          if (bbsnet_conf[n].use_ssh)
1391          {          {
1392                  if (channel != NULL)                  if (outbound_channel != NULL)
1393                  {                  {
1394                          ssh_channel_free(channel);                          ssh_channel_send_eof(outbound_channel);
1395                            ssh_channel_close(outbound_channel);
1396                            ssh_channel_free(outbound_channel);
1397                  }                  }
1398                  if (session != NULL)                  if (outbound_session != NULL)
1399                  {                  {
1400                          ssh_disconnect(session);                          ssh_disconnect(outbound_session);
1401                          ssh_free(session);                          ssh_free(outbound_session);
1402                  }                  }
1403          }          }
1404    
1405          // Restore STDIN/STDOUT flags          // Restore STDIN/STDOUT flags
1406          if (flags_stdin != -1 && fcntl(STDIN_FILENO, F_SETFL, flags_stdin) == -1)          if (flags_stdin != -1 && fcntl(STDIN_FILENO, F_SETFL, flags_stdin) == -1)
1407          {          {
1408                  log_error("fcntl(F_SETFL) error (%d)\n", errno);                  log_error("fcntl(F_SETFL) error (%d)", errno);
1409          }          }
1410          if (flags_stdout != -1 && fcntl(STDOUT_FILENO, F_SETFL, flags_stdout) == -1)          if (flags_stdout != -1 && fcntl(STDOUT_FILENO, F_SETFL, flags_stdout) == -1)
1411          {          {
1412                  log_error("fcntl(F_SETFL) error (%d)\n", errno);                  log_error("fcntl(F_SETFL) error (%d)", errno);
1413          }          }
1414    
1415          if (sock != -1 && close(sock) == -1)          if (sock != -1)
1416          {          {
1417                  log_error("Close socket failed\n");                  close(sock);
1418          }          }
1419    
1420          t_used = time(NULL) - t_used;          if (res)
1421          tm_used = gmtime(&t_used);          {
1422                    freeaddrinfo(res);
1423          log_common("BBSNET disconnect, %d days %d hours %d minutes %d seconds used\n",          }
                            tm_used->tm_yday, tm_used->tm_hour, tm_used->tm_min, tm_used->tm_sec);  
   
         BBS_last_access_tm = time(NULL);  
1424    
1425          return 0;          return ret;
1426  }  }
1427    
1428  static int bbsnet_refresh()  static int bbsnet_refresh()
# Line 1305  static int bbsnet_selchange() Line 1464  static int bbsnet_selchange()
1464          prints("|");          prints("|");
1465          moveto(21, 1);          moveto(21, 1);
1466          clrtoeol();          clrtoeol();
1467          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",
1468                     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);
1469          moveto(21, 80);          moveto(21, 80);
1470          prints("|");          prints("|");
# Line 1316  static int bbsnet_selchange() Line 1475  static int bbsnet_selchange()
1475    
1476  int bbs_net()  int bbs_net()
1477  {  {
1478          int ch, i;          int ch;
1479    
1480          if (load_bbsnet_conf(CONF_BBSNET) < 0)          if (load_bbsnet_conf(CONF_BBSNET) < 0)
1481          {          {
# Line 1343  int bbs_net() Line 1502  int bbs_net()
1502                  switch (ch)                  switch (ch)
1503                  {                  {
1504                  case KEY_NULL: // broken pipe                  case KEY_NULL: // broken pipe
1505  #ifdef _DEBUG                          log_debug("KEY_NULL");
                         log_error("KEY_NULL\n");  
 #endif  
1506                          goto cleanup;                          goto cleanup;
1507                  case KEY_TIMEOUT:                  case KEY_TIMEOUT:
1508                          if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time)                          if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time)
1509                          {                          {
1510                                  log_error("User input timeout\n");                                  log_debug("User input timeout");
1511                                  goto cleanup;                                  goto cleanup;
1512                          }                          }
1513                          continue;                          continue;
# Line 1358  int bbs_net() Line 1515  int bbs_net()
1515                  case Ctrl('C'): // user cancel                  case Ctrl('C'): // user cancel
1516                          goto cleanup;                          goto cleanup;
1517                  case CR:                  case CR:
1518                          bbsnet_connect(bbsnet_menu.menu_item_pos[0]);                          if (bbsnet_connect(bbsnet_menu.menu_item_pos[0]) < 0)
1519                            {
1520                                    log_debug("bbsnet_connect() error");
1521                            }
1522                          // Force cleanup anything remaining in the output buffer                          // Force cleanup anything remaining in the output buffer
1523                          clearscr();                          clearscr();
1524                          iflush();                          iflush();
# Line 1368  int bbs_net() Line 1528  int bbs_net()
1528                          bbsnet_selchange();                          bbsnet_selchange();
1529                          break;                          break;
1530                  case KEY_UP:                  case KEY_UP:
1531                          for (i = 0; i < STATION_PER_LINE; i++)                          for (int i = 0; i < STATION_PER_LINE; i++)
1532                          {                          {
1533                                  menu_control(&bbsnet_menu, KEY_UP);                                  menu_control(&bbsnet_menu, KEY_UP);
1534                          }                          }
1535                          bbsnet_selchange();                          bbsnet_selchange();
1536                          break;                          break;
1537                  case KEY_DOWN:                  case KEY_DOWN:
1538                          for (i = 0; i < STATION_PER_LINE; i++)                          for (int i = 0; i < STATION_PER_LINE; i++)
1539                          {                          {
1540                                  menu_control(&bbsnet_menu, KEY_DOWN);                                  menu_control(&bbsnet_menu, KEY_DOWN);
1541                          }                          }


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

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