/[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.47 by sysadm, Thu Jun 5 05:24:56 2025 UTC Revision 1.84 by sysadm, Sun Nov 30 10:05:16 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /* SPDX-License-Identifier: GPL-3.0-or-later */
2                                                    bbs_net.c  -  description  /*
3                                                           -------------------   * bbs_net
4          Copyright            : (C) 2004-2025 by Leaflet   *   - user interactive feature of site shuttle
5          Email                : leaflet@leafok.com   *
6   ***************************************************************************/   * Copyright (C) 2004-2025  Leaflet <leaflet@leafok.com>
7     */
8  /***************************************************************************  
9   *                                                                         *  #ifdef HAVE_CONFIG_H
10   *   This program is free software; you can redistribute it and/or modify  *  #include "config.h"
11   *   it under the terms of the GNU General Public License as published by  *  #endif
  *   the Free Software Foundation; either version 3 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
12    
13  #include "bbs.h"  #include "bbs.h"
14    #include "bbs_net.h"
15  #include "common.h"  #include "common.h"
 #include "log.h"  
16  #include "io.h"  #include "io.h"
17  #include "screen.h"  #include "log.h"
18    #include "login.h"
19  #include "menu.h"  #include "menu.h"
20  #include <stdio.h>  #include "screen.h"
21  #include <stdarg.h>  #include "str_process.h"
22  #include <errno.h>  #include <errno.h>
 #include <string.h>  
 #include <stdlib.h>  
23  #include <fcntl.h>  #include <fcntl.h>
24    #include <netdb.h>
25    #include <stdarg.h>
26    #include <stdio.h>
27    #include <stdlib.h>
28    #include <string.h>
29  #include <time.h>  #include <time.h>
30  #include <unistd.h>  #include <unistd.h>
 #include <netdb.h>  
 #include <sys/select.h>  
 #include <sys/ioctl.h>  
 #include <sys/socket.h>  
 #include <sys/epoll.h>  
 #include <netinet/in.h>  
 #include <netinet/ip.h>  
31  #include <arpa/inet.h>  #include <arpa/inet.h>
32    #include <ctype.h>
33  #include <libssh/libssh.h>  #include <libssh/libssh.h>
34  #include <libssh/server.h>  #include <libssh/server.h>
35  #include <libssh/callbacks.h>  #include <libssh/callbacks.h>
36    #include <netinet/in.h>
37    #include <netinet/ip.h>
38    #include <sys/select.h>
39    #include <sys/ioctl.h>
40    #include <sys/socket.h>
41    
42  #define MENU_CONF_DELIM " \t\r\n"  #ifdef HAVE_SYS_EPOLL_H
43    #include <sys/epoll.h>
44    #else
45    #include <poll.h>
46    #endif
47    
48  #define MAX_PROCESS_BAR_LEN 30  static const char MENU_CONF_DELIM[] = " \t\r\n";
49  #define MAXSTATION 26 * 2  
50  #define STATION_PER_LINE 4  enum _bbs_net_constant_t
51    {
52            MAX_PROCESS_BAR_LEN = 30,
53            MAXSTATION = 26 * 2,
54            STATION_PER_LINE = 4,
55            USERNAME_MAX_LEN = 20,
56            PASSWORD_MAX_LEN = 20,
57    };
58    
59  struct _bbsnet_conf  struct _bbsnet_conf
60  {  {
# Line 52  struct _bbsnet_conf Line 62  struct _bbsnet_conf
62          char host2[40];          char host2[40];
63          char ip[40];          char ip[40];
64          in_port_t port;          in_port_t port;
65            int8_t use_ssh;
66            char charset[CHARSET_MAX_LEN + 1];
67  } bbsnet_conf[MAXSTATION];  } bbsnet_conf[MAXSTATION];
68    
69  MENU_SET bbsnet_menu;  static MENU_SET bbsnet_menu;
70    
71  int load_bbsnet_conf(const char *file_config)  static int load_bbsnet_conf(const char *file_config)
72  {  {
73          FILE *fp;          FILE *fp;
74          MENU *p_menu;          MENU *p_menu;
75          MENU_ITEM *p_menu_item;          MENU_ITEM *p_menu_item;
76          MENU_ITEM_ID menu_item_id;          MENU_ITEM_ID menu_item_id;
77          char t[256], *t1, *t2, *t3, *t4, *saveptr;          char t[256], *t1, *t2, *t3, *t4, *t5, *t6, *saveptr;
78    
79          fp = fopen(file_config, "r");          fp = fopen(file_config, "r");
80          if (fp == NULL)          if (fp == NULL)
# Line 100  int load_bbsnet_conf(const char *file_co Line 112  int load_bbsnet_conf(const char *file_co
112                  t2 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);                  t2 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
113                  t3 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);                  t3 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
114                  t4 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);                  t4 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
115                    t5 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
116                    t6 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
117    
118                  if (t1 == NULL || t2 == NULL || t3 == NULL || t4 == NULL || t[0] == '#' || t[0] == '*')                  if (t1 == NULL || t2 == NULL || t3 == NULL || t4 == NULL ||
119                            t5 == NULL || t6 == NULL || t[0] == '#' || t[0] == '*')
120                  {                  {
121                          continue;                          continue;
122                  }                  }
# Line 113  int load_bbsnet_conf(const char *file_co Line 128  int load_bbsnet_conf(const char *file_co
128                  strncpy(bbsnet_conf[menu_item_id].ip, t3, sizeof(bbsnet_conf[menu_item_id].ip) - 1);                  strncpy(bbsnet_conf[menu_item_id].ip, t3, sizeof(bbsnet_conf[menu_item_id].ip) - 1);
129                  bbsnet_conf[menu_item_id].ip[sizeof(bbsnet_conf[menu_item_id].ip) - 1] = '\0';                  bbsnet_conf[menu_item_id].ip[sizeof(bbsnet_conf[menu_item_id].ip) - 1] = '\0';
130                  bbsnet_conf[menu_item_id].port = (in_port_t)(t4 ? atoi(t4) : 23);                  bbsnet_conf[menu_item_id].port = (in_port_t)(t4 ? atoi(t4) : 23);
131                    bbsnet_conf[menu_item_id].use_ssh = (toupper(t5[0]) == 'Y');
132                    strncpy(bbsnet_conf[menu_item_id].charset, t6, sizeof(bbsnet_conf[menu_item_id].charset) - 1);
133                    bbsnet_conf[menu_item_id].charset[sizeof(bbsnet_conf[menu_item_id].charset) - 1] = '\0';
134    
135                  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);
136                  if (p_menu_item == NULL)                  if (p_menu_item == NULL)
# Line 148  int load_bbsnet_conf(const char *file_co Line 166  int load_bbsnet_conf(const char *file_co
166          return 0;          return 0;
167  }  }
168    
169  void unload_bbsnet_conf(void)  static void unload_bbsnet_conf(void)
170  {  {
171          bbsnet_menu.menu_count = 0;          bbsnet_menu.menu_count = 0;
172          bbsnet_menu.menu_item_count = 0;          bbsnet_menu.menu_item_count = 0;
# Line 159  void unload_bbsnet_conf(void) Line 177  void unload_bbsnet_conf(void)
177          bbsnet_menu.p_menu_item_pool = NULL;          bbsnet_menu.p_menu_item_pool = NULL;
178  }  }
179    
180  void process_bar(int n, int len)  static void process_bar(int n, int len)
181  {  {
182          char buf[LINE_BUFFER_LEN];          char buf[LINE_BUFFER_LEN];
183          char buf2[LINE_BUFFER_LEN];          char buf2[LINE_BUFFER_LEN];
# Line 177  void process_bar(int n, int len) Line 195  void process_bar(int n, int len)
195                  n = len;                  n = len;
196          }          }
197    
198          moveto(4, 0);          moveto(4, 1);
199          prints(" ------------------------------ \r\n");          prints(" ------------------------------ \r\n");
200          snprintf(buf, sizeof(buf), "            %3d%%              ", n * 100 / len);          snprintf(buf, sizeof(buf), "            %3d%%              ", n * 100 / len);
201          strncpy(buf2, buf, (size_t)n);          memcpy(buf2, buf, (size_t)n);
202          buf2[n] = '\0';          buf2[n] = '\0';
203          prints("|\033[46m%s\033[44m%s\033[m|\r\n", buf2, buf + n);          prints("|\033[46m%s\033[44m%s\033[m|\r\n", buf2, buf + n);
204          prints(" ------------------------------ \r\n");          prints(" ------------------------------ \r\n");
205          iflush();          iflush();
206  }  }
207    
208  int bbsnet_connect(int n)  static int bbsnet_connect(int n)
209  {  {
210          int sock, ret, loop, error;          int sock, ret, loop, error;
211          int sock_connected = 0;          int sock_connected = 0;
# Line 202  int bbsnet_connect(int n) Line 220  int bbsnet_connect(int n)
220          int output_buf_len = 0;          int output_buf_len = 0;
221          int input_buf_offset = 0;          int input_buf_offset = 0;
222          int output_buf_offset = 0;          int output_buf_offset = 0;
223            char input_conv[LINE_BUFFER_LEN * 2];
224            char output_conv[LINE_BUFFER_LEN * 2];
225            int input_conv_len = 0;
226            int output_conv_len = 0;
227            int input_conv_offset = 0;
228            int output_conv_offset = 0;
229            iconv_t input_cd = NULL;
230            iconv_t output_cd = NULL;
231            char tocode[32];
232    
233    #ifdef HAVE_SYS_EPOLL_H
234          struct epoll_event ev, events[MAX_EVENTS];          struct epoll_event ev, events[MAX_EVENTS];
235          int nfds, epollfd;          int epollfd;
236    #else
237            struct pollfd pfds[3];
238    #endif
239    
240            int nfds;
241          int stdin_read_wait = 0;          int stdin_read_wait = 0;
242          int stdout_write_wait = 0;          int stdout_write_wait = 0;
243          int sock_read_wait = 0;          int sock_read_wait = 0;
# Line 212  int bbsnet_connect(int n) Line 246  int bbsnet_connect(int n)
246          int tos;          int tos;
247          char remote_addr[IP_ADDR_LEN];          char remote_addr[IP_ADDR_LEN];
248          int remote_port;          int remote_port;
249          time_t t_used;          char local_addr[IP_ADDR_LEN];
250            int local_port;
251            socklen_t sock_len;
252            time_t t_used = time(NULL);
253          struct tm *tm_used;          struct tm *tm_used;
254          int ch;          int ch;
255            char remote_user[USERNAME_MAX_LEN + 1];
256            char remote_pass[PASSWORD_MAX_LEN + 1];
257            ssh_session session = NULL;
258            ssh_channel channel = NULL;
259            int ssh_process_config = 0;
260            int ssh_log_level = SSH_LOG_PROTOCOL;
261    
262            if (user_online_update("BBS_NET") < 0)
263            {
264                    log_error("user_online_update(BBS_NET) error\n");
265            }
266    
267            if (bbsnet_conf[n].use_ssh)
268            {
269                    clearscr();
270    
271                    if (!SSH_v2)
272                    {
273                            moveto(1, 1);
274                            prints("只有在以SSH方式登陆本站时,才能使用SSH站点穿梭。");
275                            press_any_key();
276                            return 0;
277                    }
278    
279                    moveto(1, 1);
280                    prints("通过SSH方式连接[%s]...", bbsnet_conf[n].host1);
281                    moveto(2, 1);
282                    prints("请输入用户名: ");
283                    iflush();
284                    if (str_input(remote_user, sizeof(remote_user), DOECHO) < 0)
285                    {
286                            return -1;
287                    }
288                    if (remote_user[0] == '\0')
289                    {
290                            return 0;
291                    }
292    
293                    moveto(3, 1);
294                    prints("请输入密码: ");
295                    iflush();
296                    if (str_input(remote_pass, sizeof(remote_pass), NOECHO) < 0)
297                    {
298                            return -1;
299                    }
300                    if (remote_pass[0] == '\0')
301                    {
302                            return 0;
303                    }
304            }
305    
306          clearscr();          clearscr();
307    
308          moveto(0, 0);          moveto(1, 1);
309          prints("\033[1;32mڲ %s (%s) ӣԺ... \033[m\r\n",          prints("\033[1;32m正在测试往 %s (%s) 的连接,请稍候... \033[m\r\n",
310                     bbsnet_conf[n].host1, bbsnet_conf[n].ip);                     bbsnet_conf[n].host1, bbsnet_conf[n].ip);
311          iflush();          iflush();
312    
# Line 227  int bbsnet_connect(int n) Line 314  int bbsnet_connect(int n)
314    
315          if (p_host == NULL)          if (p_host == NULL)
316          {          {
317                  prints("\033[1;31mʧܣ\033[m\r\n");                  prints("\033[1;31m查找主机名失败!\033[m\r\n");
318                  press_any_key();                  press_any_key();
319                  return -1;                  return -1;
320          }          }
# Line 236  int bbsnet_connect(int n) Line 323  int bbsnet_connect(int n)
323    
324          if (sock < 0)          if (sock < 0)
325          {          {
326                  prints("\033[1;31m޷socket\033[m\r\n");                  prints("\033[1;31m无法创建socket!\033[m\r\n");
327                  press_any_key();                  press_any_key();
328                  return -1;                  return -1;
329          }          }
330    
331          sin.sin_family = AF_INET;          sin.sin_family = AF_INET;
332          sin.sin_addr.s_addr = (hostaddr_server[0] != '\0' ? inet_addr(hostaddr_server) : INADDR_ANY);          sin.sin_addr.s_addr = (BBS_address[0] != '\0' ? inet_addr(BBS_address) : INADDR_ANY);
333          sin.sin_port = 0;          sin.sin_port = 0;
334    
335          if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0)          if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0)
# Line 252  int bbsnet_connect(int n) Line 339  int bbsnet_connect(int n)
339                  return -2;                  return -2;
340          }          }
341    
342          bzero(&sin, sizeof(sin));          memset(&sin, 0, sizeof(sin));
343          sin.sin_family = AF_INET;          sin.sin_family = AF_INET;
344          sin.sin_addr = *(struct in_addr *)p_host->h_addr_list[0];          sin.sin_addr = *(struct in_addr *)p_host->h_addr_list[0];
345          sin.sin_port = htons(bbsnet_conf[n].port);          sin.sin_port = htons(bbsnet_conf[n].port);
# Line 261  int bbsnet_connect(int n) Line 348  int bbsnet_connect(int n)
348          remote_addr[sizeof(remote_addr) - 1] = '\0';          remote_addr[sizeof(remote_addr) - 1] = '\0';
349          remote_port = ntohs(sin.sin_port);          remote_port = ntohs(sin.sin_port);
350    
351          prints("\033[1;32mʾǰʹõʱ䣬\033[1;33mCtrl+C\033[1;32mжϡ\033[m\r\n");          prints("\033[1;32m穿梭进度条提示您当前已使用的时间,按\033[1;33mCtrl+C\033[1;32m中断。\033[m\r\n");
352          process_bar(0, MAX_PROCESS_BAR_LEN);          process_bar(0, MAX_PROCESS_BAR_LEN);
353    
354          // Set socket as non-blocking          // Set socket as non-blocking
# Line 274  int bbsnet_connect(int n) Line 361  int bbsnet_connect(int n)
361          fcntl(STDIN_FILENO, F_SETFL, flags_stdin | O_NONBLOCK);          fcntl(STDIN_FILENO, F_SETFL, flags_stdin | O_NONBLOCK);
362          fcntl(STDOUT_FILENO, F_SETFL, flags_stdout | O_NONBLOCK);          fcntl(STDOUT_FILENO, F_SETFL, flags_stdout | O_NONBLOCK);
363    
364    #ifdef HAVE_SYS_EPOLL_H
365          epollfd = epoll_create1(0);          epollfd = epoll_create1(0);
366          if (epollfd < 0)          if (epollfd < 0)
367          {          {
# Line 281  int bbsnet_connect(int n) Line 369  int bbsnet_connect(int n)
369                  return -1;                  return -1;
370          }          }
371    
372          ev.events = EPOLLOUT;          ev.events = EPOLLOUT | EPOLLET;
373          ev.data.fd = sock;          ev.data.fd = sock;
374          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sock, &ev) == -1)          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sock, &ev) == -1)
375          {          {
# Line 289  int bbsnet_connect(int n) Line 377  int bbsnet_connect(int n)
377                  goto cleanup;                  goto cleanup;
378          }          }
379    
380          ev.events = EPOLLIN;          ev.events = EPOLLIN | EPOLLET;
381          ev.data.fd = STDIN_FILENO;          ev.data.fd = STDIN_FILENO;
382          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1)          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1)
383          {          {
384                  log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno);                  log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno);
385                  goto cleanup;                  goto cleanup;
386          }          }
387    #endif
388    
389          while (!SYS_server_exit)          while (!SYS_server_exit)
390          {          {
# Line 316  int bbsnet_connect(int n) Line 405  int bbsnet_connect(int n)
405                          {                          {
406                                  log_error("connect(socket) error (%d)\n", errno);                                  log_error("connect(socket) error (%d)\n", errno);
407    
408                                  prints("\033[1;31mʧܣ\033[m\r\n");                                  prints("\033[1;31m连接失败!\033[m\r\n");
409                                  press_any_key();                                  press_any_key();
410    
411                                  goto cleanup;                                  goto cleanup;
# Line 326  int bbsnet_connect(int n) Line 415  int bbsnet_connect(int n)
415    
416          for (int j = 0; j < MAX_PROCESS_BAR_LEN && !sock_connected && !SYS_server_exit; j++)          for (int j = 0; j < MAX_PROCESS_BAR_LEN && !sock_connected && !SYS_server_exit; j++)
417          {          {
418    #ifdef HAVE_SYS_EPOLL_H
419                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 500); // 0.5 second                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 500); // 0.5 second
420                    ret = nfds;
421    #else
422                    pfds[0].fd = sock;
423                    pfds[0].events = POLLOUT;
424                    pfds[1].fd = STDIN_FILENO;
425                    pfds[1].events = POLLIN;
426                    nfds = 2;
427                    ret = poll(pfds, (nfds_t)nfds, 500); // 0.5 second
428    #endif
429    
430                  if (nfds < 0)                  if (ret < 0)
431                  {                  {
432                          if (errno != EINTR)                          if (errno != EINTR)
433                          {                          {
434    #ifdef HAVE_SYS_EPOLL_H
435                                  log_error("epoll_wait() error (%d)\n", errno);                                  log_error("epoll_wait() error (%d)\n", errno);
436    #else
437                                    log_error("poll() error (%d)\n", errno);
438    #endif
439                                  break;                                  break;
440                          }                          }
441                  }                  }
442                  else if (nfds == 0) // timeout                  else if (ret == 0) // timeout
443                  {                  {
444                          process_bar(j + 1, MAX_PROCESS_BAR_LEN);                          process_bar(j + 1, MAX_PROCESS_BAR_LEN);
445                  }                  }
# Line 344  int bbsnet_connect(int n) Line 447  int bbsnet_connect(int n)
447                  {                  {
448                          for (int i = 0; i < nfds; i++)                          for (int i = 0; i < nfds; i++)
449                          {                          {
450    #ifdef HAVE_SYS_EPOLL_H
451                                  if (events[i].data.fd == sock)                                  if (events[i].data.fd == sock)
452    #else
453                                    if (pfds[i].fd == sock && (pfds[i].revents & POLLOUT))
454    #endif
455                                  {                                  {
456                                          len = sizeof(error);                                          len = sizeof(error);
457                                          if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, (socklen_t *)&len) < 0)                                          if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, (socklen_t *)&len) < 0)
# Line 357  int bbsnet_connect(int n) Line 464  int bbsnet_connect(int n)
464                                                  sock_connected = 1;                                                  sock_connected = 1;
465                                          }                                          }
466                                  }                                  }
467    #ifdef HAVE_SYS_EPOLL_H
468                                  else if (events[i].data.fd == STDIN_FILENO)                                  else if (events[i].data.fd == STDIN_FILENO)
469    #else
470                                    else if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & POLLIN))
471    #endif
472                                  {                                  {
473                                          ch = igetch(0);                                          do
474                                            {
475                                                    ch = igetch(0);
476                                            } while (ch == 0);
477                                          if (ch == Ctrl('C') || ch == KEY_ESC)                                          if (ch == Ctrl('C') || ch == KEY_ESC)
478                                          {                                          {
479                                                  goto cleanup;                                                  goto cleanup;
# Line 374  int bbsnet_connect(int n) Line 488  int bbsnet_connect(int n)
488          }          }
489          if (!sock_connected)          if (!sock_connected)
490          {          {
491                  prints("\033[1;31mʧܣ\033[m\r\n");                  prints("\033[1;31m连接失败!\033[m\r\n");
492                  press_any_key();                  press_any_key();
493    
494                  goto cleanup;                  goto cleanup;
# Line 386  int bbsnet_connect(int n) Line 500  int bbsnet_connect(int n)
500                  log_error("setsockopt IP_TOS=%d error (%d)\n", tos, errno);                  log_error("setsockopt IP_TOS=%d error (%d)\n", tos, errno);
501          }          }
502    
503          prints("\033[1;31mӳɹ\033[m\r\n");          sock_len = sizeof(sin);
504            if (getsockname(sock, (struct sockaddr *)&sin, &sock_len) < 0)
505            {
506                    log_error("getsockname() error: %d", errno);
507                    goto cleanup;
508            }
509    
510            strncpy(local_addr, inet_ntoa(sin.sin_addr), sizeof(local_addr) - 1);
511            local_addr[sizeof(local_addr) - 1] = '\0';
512            local_port = ntohs(sin.sin_port);
513    
514            if (bbsnet_conf[n].use_ssh)
515            {
516                    session = ssh_new();
517                    if (session == NULL)
518                    {
519                            log_error("ssh_new() error\n");
520                            goto cleanup;
521                    }
522    
523                    if (ssh_options_set(session, SSH_OPTIONS_FD, &sock) < 0 ||
524                            ssh_options_set(session, SSH_OPTIONS_PROCESS_CONFIG, &ssh_process_config) < 0 ||
525                            ssh_options_set(session, SSH_OPTIONS_HOST, bbsnet_conf[n].ip) < 0 ||
526                            ssh_options_set(session, SSH_OPTIONS_USER, remote_user) < 0 ||
527                            ssh_options_set(session, SSH_OPTIONS_HOSTKEYS, "+ssh-rsa") < 0 ||
528                            ssh_options_set(session, SSH_OPTIONS_LOG_VERBOSITY, &ssh_log_level) < 0)
529                    {
530                            log_error("Error setting SSH options: %s\n", ssh_get_error(session));
531                            goto cleanup;
532                    }
533    
534                    while (!SYS_server_exit)
535                    {
536                            ret = ssh_connect(session);
537                            if (ret == SSH_OK)
538                            {
539                                    break;
540                            }
541                            else if (ret == SSH_ERROR)
542                            {
543                                    log_error("ssh_connect() error\n");
544                                    goto cleanup;
545                            }
546                    }
547    
548                    ssh_set_blocking(session, 0);
549    
550                    for (int i = 0; !SYS_server_exit;)
551                    {
552                            ret = ssh_userauth_password(session, NULL, remote_pass);
553                            if (ret == SSH_AUTH_SUCCESS)
554                            {
555                                    break;
556                            }
557                            else if (ret == SSH_AUTH_AGAIN)
558                            {
559    #ifdef _DEBUG
560                                    log_error("ssh_userauth_password() error: SSH_AUTH_AGAIN\n");
561    #endif
562                            }
563                            else if (ret == SSH_AUTH_ERROR)
564                            {
565                                    log_error("ssh_userauth_password() error: %d\n", ret);
566                                    goto cleanup;
567                            }
568                            else // if (ret == SSH_AUTH_DENIED)
569                            {
570                                    prints("\033[1;31m身份验证失败!\033[m\r\n");
571                                    i++;
572                                    if (i < BBS_login_retry_times)
573                                    {
574                                            prints("请输入密码: ");
575                                            iflush();
576                                            if (str_input(remote_pass, sizeof(remote_pass), NOECHO) < 0)
577                                            {
578                                                    goto cleanup;
579                                            }
580                                            if (remote_pass[0] == '\0')
581                                            {
582                                                    goto cleanup;
583                                            }
584                                    }
585                                    else
586                                    {
587                                            goto cleanup;
588                                    }
589                            }
590                    }
591    
592                    channel = ssh_channel_new(session);
593                    if (channel == NULL)
594                    {
595                            log_error("ssh_channel_new() error\n");
596                            goto cleanup;
597                    }
598    
599                    while (!SYS_server_exit)
600                    {
601                            ret = ssh_channel_open_session(channel);
602                            if (ret == SSH_OK)
603                            {
604                                    break;
605                            }
606                            else if (ret == SSH_ERROR)
607                            {
608                                    log_error("ssh_channel_open_session() error\n");
609                                    goto cleanup;
610                            }
611                    }
612    
613                    while (!SYS_server_exit)
614                    {
615                            ret = ssh_channel_request_pty(channel);
616                            if (ret == SSH_OK)
617                            {
618                                    break;
619                            }
620                            else if (ret == SSH_ERROR)
621                            {
622                                    log_error("ssh_channel_request_pty() error\n");
623                                    goto cleanup;
624                            }
625                    }
626    
627                    while (!SYS_server_exit)
628                    {
629                            ret = ssh_channel_request_shell(channel);
630                            if (ret == SSH_OK)
631                            {
632                                    break;
633                            }
634                            else if (ret == SSH_ERROR)
635                            {
636                                    log_error("ssh_channel_request_shell() error\n");
637                                    goto cleanup;
638                            }
639                    }
640            }
641    
642            prints("\033[1;31m连接成功!\033[m\r\n");
643          iflush();          iflush();
644          log_common("BBSNET connect to %s:%d\n", remote_addr, remote_port);          log_common("BBSNET connect to %s:%d from %s:%d by [%s]\n",
645                               remote_addr, remote_port, local_addr, local_port, BBS_username);
646    
647            snprintf(tocode, sizeof(tocode), "%s%s", bbsnet_conf[n].charset,
648                             (strcasecmp(stdio_charset, bbsnet_conf[n].charset) == 0 ? "" : "//IGNORE"));
649            input_cd = iconv_open(tocode, stdio_charset);
650            if (input_cd == (iconv_t)(-1))
651            {
652                    log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, tocode, errno);
653                    goto cleanup;
654            }
655    
656            snprintf(tocode, sizeof(tocode), "%s%s", stdio_charset,
657                             (strcasecmp(bbsnet_conf[n].charset, stdio_charset) == 0 ? "" : "//TRANSLIT"));
658            output_cd = iconv_open(tocode, bbsnet_conf[n].charset);
659            if (output_cd == (iconv_t)(-1))
660            {
661                    log_error("iconv_open(%s->%s) error: %d\n", bbsnet_conf[n].charset, tocode, errno);
662                    iconv_close(input_cd);
663                    goto cleanup;
664            }
665    
666    #ifdef HAVE_SYS_EPOLL_H
667          ev.events = EPOLLIN | EPOLLOUT | EPOLLET;          ev.events = EPOLLIN | EPOLLOUT | EPOLLET;
668          ev.data.fd = sock;          ev.data.fd = sock;
669          if (epoll_ctl(epollfd, EPOLL_CTL_MOD, sock, &ev) == -1)          if (epoll_ctl(epollfd, EPOLL_CTL_MOD, sock, &ev) == -1)
# Line 398  int bbsnet_connect(int n) Line 672  int bbsnet_connect(int n)
672                  goto cleanup;                  goto cleanup;
673          }          }
674    
675          ev.events = EPOLLOUT;          ev.events = EPOLLOUT | EPOLLET;
676          ev.data.fd = STDOUT_FILENO;          ev.data.fd = STDOUT_FILENO;
677          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1)          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1)
678          {          {
679                  log_error("epoll_ctl(STDOUT_FILENO) error (%d)\n", errno);                  log_error("epoll_ctl(STDOUT_FILENO) error (%d)\n", errno);
680                  goto cleanup;                  goto cleanup;
681          }          }
682    #endif
683    
684          BBS_last_access_tm = t_used = time(0);          BBS_last_access_tm = t_used = time(NULL);
685          loop = 1;          loop = 1;
686    
687          while (loop && !SYS_server_exit)          while (loop && !SYS_server_exit)
# Line 418  int bbsnet_connect(int n) Line 693  int bbsnet_connect(int n)
693                          break;                          break;
694                  }                  }
695    
696                    if (bbsnet_conf[n].use_ssh && ssh_channel_is_closed(channel))
697                    {
698                            log_error("Remote SSH channel is closed\n");
699                            loop = 0;
700                            break;
701                    }
702    
703    #ifdef HAVE_SYS_EPOLL_H
704                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second
705                    ret = nfds;
706    #else
707                    pfds[0].fd = STDIN_FILENO;
708                    pfds[0].events = POLLIN;
709                    pfds[1].fd = sock;
710                    pfds[1].events = POLLIN | POLLOUT;
711                    pfds[2].fd = STDOUT_FILENO;
712                    pfds[2].events = POLLOUT;
713                    nfds = 3;
714                    ret = poll(pfds, (nfds_t)nfds, 100); // 0.1 second
715    #endif
716    
717                  if (nfds < 0)                  if (ret < 0)
718                  {                  {
719                          if (errno != EINTR)                          if (errno != EINTR)
720                          {                          {
721    #ifdef HAVE_SYS_EPOLL_H
722                                  log_error("epoll_wait() error (%d)\n", errno);                                  log_error("epoll_wait() error (%d)\n", errno);
723    #else
724                                    log_error("poll() error (%d)\n", errno);
725    #endif
726                                  break;                                  break;
727                          }                          }
728                          continue;                          continue;
729                  }                  }
730                  else if (nfds == 0) // timeout                  else if (ret == 0) // timeout
731                  {                  {
732                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time)
733                          {                          {
734                                  break;                                  break;
735                          }                          }
                         continue;  
736                  }                  }
737    
738                  for (int i = 0; i < nfds; i++)                  for (int i = 0; i < nfds; i++)
739                  {                  {
740                          if (events[i].data.fd == STDIN_FILENO || stdin_read_wait)  #ifdef HAVE_SYS_EPOLL_H
741                            if (events[i].data.fd == STDIN_FILENO)
742    #else
743                            if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & POLLIN))
744    #endif
745                          {                          {
746                                  stdin_read_wait = 1;                                  stdin_read_wait = 1;
747                                  while (input_buf_len < sizeof(input_buf) && !SYS_server_exit)                          }
748    
749    #ifdef HAVE_SYS_EPOLL_H
750                            if (events[i].data.fd == sock)
751    #else
752                            if (pfds[i].fd == sock)
753    #endif
754                            {
755    #ifdef HAVE_SYS_EPOLL_H
756                                    if (events[i].events & EPOLLIN)
757    #else
758                                    if (pfds[i].revents & POLLIN)
759    #endif
760                                  {                                  {
761                                          if (SSH_v2)                                          sock_read_wait = 1;
762                                    }
763    
764    #ifdef HAVE_SYS_EPOLL_H
765                                    if (events[i].events & EPOLLOUT)
766    #else
767                                    if (pfds[i].revents & POLLOUT)
768    #endif
769                                    {
770                                            sock_write_wait = 1;
771                                    }
772                            }
773    
774    #ifdef HAVE_SYS_EPOLL_H
775                            if (events[i].data.fd == STDOUT_FILENO)
776    #else
777                            if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & POLLOUT))
778    #endif
779                            {
780                                    stdout_write_wait = 1;
781                            }
782                    }
783    
784                    if (stdin_read_wait)
785                    {
786                            while (input_buf_len < sizeof(input_buf) && !SYS_server_exit)
787                            {
788                                    if (SSH_v2)
789                                    {
790                                            ret = ssh_channel_read_nonblocking(SSH_channel, input_buf + input_buf_len, sizeof(input_buf) - (uint32_t)input_buf_len, 0);
791                                            if (ret == SSH_ERROR)
792                                          {                                          {
793                                                  ret = ssh_channel_read_nonblocking(SSH_channel, input_buf + input_buf_len, sizeof(input_buf) - (uint32_t)input_buf_len, 0);                                                  log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session));
794                                                  if (ret == SSH_ERROR)                                                  loop = 0;
795                                                  {                                                  break;
                                                         log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session));  
                                                         loop = 0;  
                                                         break;  
                                                 }  
                                                 else if (ret == SSH_EOF)  
                                                 {  
                                                         stdin_read_wait = 0;  
                                                         loop = 0;  
                                                         break;  
                                                 }  
                                                 else if (ret == 0)  
                                                 {  
                                                         stdin_read_wait = 0;  
                                                         break; // Check whether channel is still open  
                                                 }  
796                                          }                                          }
797                                          else                                          else if (ret == SSH_EOF)
798                                          {                                          {
799                                                  ret = (int)read(STDIN_FILENO, input_buf + input_buf_len, sizeof(input_buf) - (size_t)input_buf_len);                                                  stdin_read_wait = 0;
800                                                    loop = 0;
801                                                    break;
802                                          }                                          }
803                                          if (ret < 0)                                          else if (ret == 0)
804                                          {                                          {
805                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)                                                  // Send NO-OP to remote server
806                                                  {                                                  input_buf[input_buf_len] = '\0';
807                                                          stdin_read_wait = 0;                                                  input_buf_len++;
808                                                          break;                                                  BBS_last_access_tm = time(NULL);
809                                                  }  
810                                                  else if (errno == EINTR)                                                  stdin_read_wait = 0;
811                                                  {                                                  break; // Check whether channel is still open
                                                         continue;  
                                                 }  
                                                 else  
                                                 {  
                                                         log_error("read(STDIN) error (%d)\n", errno);  
                                                         loop = 0;  
                                                         break;  
                                                 }  
812                                          }                                          }
813                                          else if (ret == 0) // broken pipe                                  }
814                                    else
815                                    {
816                                            ret = (int)read(STDIN_FILENO, input_buf + input_buf_len, sizeof(input_buf) - (size_t)input_buf_len);
817                                    }
818                                    if (ret < 0)
819                                    {
820                                            if (errno == EAGAIN || errno == EWOULDBLOCK)
821                                          {                                          {
                                                 log_common("read(STDIN) EOF\n");  
822                                                  stdin_read_wait = 0;                                                  stdin_read_wait = 0;
                                                 loop = 0;  
823                                                  break;                                                  break;
824                                          }                                          }
825                                          else                                          else if (errno == EINTR)
826                                          {                                          {
                                                 input_buf_len += ret;  
                                                 BBS_last_access_tm = time(0);  
827                                                  continue;                                                  continue;
828                                          }                                          }
829                                            else
830                                            {
831                                                    log_error("read(STDIN) error (%d)\n", errno);
832                                                    loop = 0;
833                                                    break;
834                                            }
835                                    }
836                                    else if (ret == 0) // broken pipe
837                                    {
838    #ifdef _DEBUG
839                                            log_error("read(STDIN) EOF\n");
840    #endif
841                                            stdin_read_wait = 0;
842                                            loop = 0;
843                                            break;
844                                    }
845                                    else
846                                    {
847                                            input_buf_len += ret;
848                                            BBS_last_access_tm = time(NULL);
849    
850                                            // Refresh current action while user input
851                                            if (user_online_update("BBS_NET") < 0)
852                                            {
853                                                    log_error("user_online_update(BBS_NET) error\n");
854                                            }
855    
856                                            continue;
857                                  }                                  }
858                          }                          }
859                    }
860    
861                          if (events[i].data.fd == sock || sock_write_wait) // EPOLLOUT                  if (sock_write_wait)
862                    {
863                            if (input_buf_offset < input_buf_len)
864                          {                          {
865                                  sock_write_wait = 1;                                  // For debug
866                                  while (input_buf_offset < input_buf_len && !SYS_server_exit)  #ifdef _DEBUG
867                                    for (int j = input_buf_offset; j < input_buf_len; j++)
868                                  {                                  {
869                                          ret = (int)write(sock, input_buf + input_buf_offset, (size_t)(input_buf_len - input_buf_offset));                                          log_error("Debug input: <--[%u]\n", (input_buf[j] + 256) % 256);
870                                          if (ret < 0)                                  }
871    #endif
872    
873                                    ret = io_buf_conv(input_cd, input_buf, &input_buf_len, &input_buf_offset, input_conv, sizeof(input_conv), &input_conv_len);
874                                    if (ret < 0)
875                                    {
876                                            log_error("io_buf_conv(input, %d, %d, %d) error\n", input_buf_len, input_buf_offset, input_conv_len);
877                                            input_buf_len = input_buf_offset; // Discard invalid sequence
878                                    }
879    
880                                    // For debug
881    #ifdef _DEBUG
882                                    for (int j = input_conv_offset; j < input_conv_len; j++)
883                                    {
884                                            log_error("Debug input_conv: <--[%u]\n", (input_conv[j] + 256) % 256);
885                                    }
886    #endif
887                            }
888    
889                            while (input_conv_offset < input_conv_len && !SYS_server_exit)
890                            {
891                                    if (bbsnet_conf[n].use_ssh)
892                                    {
893                                            ret = ssh_channel_write(channel, input_conv + input_conv_offset, (uint32_t)(input_conv_len - input_conv_offset));
894                                            if (ret == SSH_ERROR)
895                                          {                                          {
896                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)                                                  log_error("ssh_channel_write() error: %s\n", ssh_get_error(session));
897                                                  {                                                  loop = 0;
898                                                          sock_write_wait = 0;                                                  break;
                                                         break;  
                                                 }  
                                                 else if (errno == EINTR)  
                                                 {  
                                                         continue;  
                                                 }  
                                                 else  
                                                 {  
                                                         log_error("write(socket) error (%d)\n", errno);  
                                                         loop = 0;  
                                                         break;  
                                                 }  
899                                          }                                          }
900                                          else if (ret == 0) // broken pipe                                  }
901                                    else
902                                    {
903                                            ret = (int)write(sock, input_conv + input_conv_offset, (size_t)(input_conv_len - input_conv_offset));
904                                    }
905                                    if (ret < 0)
906                                    {
907                                            if (errno == EAGAIN || errno == EWOULDBLOCK)
908                                          {                                          {
                                                 log_common("write(socket) EOF\n");  
909                                                  sock_write_wait = 0;                                                  sock_write_wait = 0;
                                                 loop = 0;  
910                                                  break;                                                  break;
911                                          }                                          }
912                                          else                                          else if (errno == EINTR)
913                                          {                                          {
                                                 input_buf_offset += ret;  
                                                 if (input_buf_offset >= input_buf_len) // Output buffer complete  
                                                 {  
                                                         input_buf_offset = 0;  
                                                         input_buf_len = 0;  
                                                         break;  
                                                 }  
914                                                  continue;                                                  continue;
915                                          }                                          }
916                                            else
917                                            {
918                                                    log_error("write(socket) error (%d)\n", errno);
919                                                    loop = 0;
920                                                    break;
921                                            }
922                                    }
923                                    else if (ret == 0) // broken pipe
924                                    {
925    #ifdef _DEBUG
926                                            log_error("write(socket) EOF\n");
927    #endif
928                                            sock_write_wait = 0;
929                                            loop = 0;
930                                            break;
931                                    }
932                                    else
933                                    {
934                                            input_conv_offset += ret;
935                                            if (input_conv_offset >= input_conv_len) // Output buffer complete
936                                            {
937                                                    input_conv_offset = 0;
938                                                    input_conv_len = 0;
939                                                    break;
940                                            }
941                                            continue;
942                                  }                                  }
943                          }                          }
944                    }
945    
946                          if (events[i].data.fd == sock || sock_read_wait) // EPOLLIN                  if (sock_read_wait)
947                    {
948                            while (output_buf_len < sizeof(output_buf) && !SYS_server_exit)
949                          {                          {
950                                  sock_read_wait = 1;                                  if (bbsnet_conf[n].use_ssh)
                                 while (output_buf_len < sizeof(output_buf) && !SYS_server_exit)  
951                                  {                                  {
952                                          ret = (int)read(sock, output_buf + output_buf_len, sizeof(output_buf) - (size_t)output_buf_len);                                          ret = ssh_channel_read_nonblocking(channel, output_buf + output_buf_len,
953                                          if (ret < 0)                                                                                                             (uint32_t)(sizeof(output_buf) - (size_t)output_buf_len), 0);
954                                            if (ret == SSH_ERROR)
955                                          {                                          {
956                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)                                                  log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(session));
957                                                  {                                                  loop = 0;
958                                                          sock_read_wait = 0;                                                  break;
                                                         break;  
                                                 }  
                                                 else if (errno == EINTR)  
                                                 {  
                                                         continue;  
                                                 }  
                                                 else  
                                                 {  
                                                         log_error("read(socket) error (%d)\n", errno);  
                                                         loop = 0;  
                                                         break;  
                                                 }  
959                                          }                                          }
960                                          else if (ret == 0) // broken pipe                                          else if (ret == SSH_EOF)
961                                          {                                          {
                                                 log_common("read(socket) EOF\n");  
962                                                  sock_read_wait = 0;                                                  sock_read_wait = 0;
963                                                  loop = 0;                                                  loop = 0;
964                                                  break;                                                  break;
965                                          }                                          }
966                                          else                                          else if (ret == 0)
967                                            {
968                                                    sock_read_wait = 0;
969                                                    break;
970                                            }
971                                    }
972                                    else
973                                    {
974                                            ret = (int)read(sock, output_buf + output_buf_len, sizeof(output_buf) - (size_t)output_buf_len);
975                                    }
976                                    if (ret < 0)
977                                    {
978                                            if (errno == EAGAIN || errno == EWOULDBLOCK)
979                                            {
980                                                    sock_read_wait = 0;
981                                                    break;
982                                            }
983                                            else if (errno == EINTR)
984                                          {                                          {
                                                 output_buf_len += ret;  
985                                                  continue;                                                  continue;
986                                          }                                          }
987                                            else
988                                            {
989                                                    log_error("read(socket) error (%d)\n", errno);
990                                                    loop = 0;
991                                                    break;
992                                            }
993                                    }
994                                    else if (ret == 0) // broken pipe
995                                    {
996    #ifdef _DEBUG
997                                            log_error("read(socket) EOF\n");
998    #endif
999                                            sock_read_wait = 0;
1000                                            loop = 0;
1001                                            break;
1002                                    }
1003                                    else
1004                                    {
1005                                            output_buf_len += ret;
1006                                            continue;
1007                                    }
1008                            }
1009                    }
1010    
1011                    if (stdout_write_wait)
1012                    {
1013                            if (output_buf_offset < output_buf_len)
1014                            {
1015                                    ret = io_buf_conv(output_cd, output_buf, &output_buf_len, &output_buf_offset, output_conv, sizeof(output_conv), &output_conv_len);
1016                                    if (ret < 0)
1017                                    {
1018                                            log_error("io_buf_conv(output, %d, %d, %d) error\n", output_buf_len, output_buf_offset, output_conv_len);
1019                                            output_buf_len = output_buf_offset; // Discard invalid sequence
1020                                  }                                  }
1021                          }                          }
1022    
1023                          if (events[i].data.fd == STDOUT_FILENO || stdout_write_wait)                          while (output_conv_offset < output_conv_len && !SYS_server_exit)
1024                          {                          {
1025                                  stdout_write_wait = 1;                                  if (SSH_v2)
                                 while (output_buf_offset < output_buf_len && !SYS_server_exit)  
1026                                  {                                  {
1027                                          if (SSH_v2)                                          ret = ssh_channel_write(SSH_channel, output_conv + output_conv_offset, (uint32_t)(output_conv_len - output_conv_offset));
1028                                            if (ret == SSH_ERROR)
1029                                          {                                          {
1030                                                  ret = ssh_channel_write(SSH_channel, output_buf + output_buf_offset, (uint32_t)(output_buf_len - output_buf_offset));                                                  log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session));
1031                                                  if (ret == SSH_ERROR)                                                  loop = 0;
1032                                                  {                                                  break;
                                                         log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session));  
                                                         loop = 0;  
                                                         break;  
                                                 }  
1033                                          }                                          }
1034                                          else                                  }
1035                                    else
1036                                    {
1037                                            ret = (int)write(STDOUT_FILENO, output_conv + output_conv_offset, (size_t)(output_conv_len - output_conv_offset));
1038                                    }
1039                                    if (ret < 0)
1040                                    {
1041                                            if (errno == EAGAIN || errno == EWOULDBLOCK)
1042                                          {                                          {
1043                                                  ret = (int)write(STDOUT_FILENO, output_buf + output_buf_offset, (size_t)(output_buf_len - output_buf_offset));                                                  stdout_write_wait = 0;
1044                                                    break;
1045                                          }                                          }
1046                                          if (ret < 0)                                          else if (errno == EINTR)
1047                                          {                                          {
1048                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)                                                  continue;
                                                 {  
                                                         stdout_write_wait = 0;  
                                                         break;  
                                                 }  
                                                 else if (errno == EINTR)  
                                                 {  
                                                         continue;  
                                                 }  
                                                 else  
                                                 {  
                                                         log_error("write(STDOUT) error (%d)\n", errno);  
                                                         loop = 0;  
                                                         break;  
                                                 }  
1049                                          }                                          }
1050                                          else if (ret == 0) // broken pipe                                          else
1051                                          {                                          {
1052                                                  log_common("write(STDOUT) EOF\n");                                                  log_error("write(STDOUT) error (%d)\n", errno);
                                                 stdout_write_wait = 0;  
1053                                                  loop = 0;                                                  loop = 0;
1054                                                  break;                                                  break;
1055                                          }                                          }
1056                                          else                                  }
1057                                    else if (ret == 0) // broken pipe
1058                                    {
1059    #ifdef _DEBUG
1060                                            log_error("write(STDOUT) EOF\n");
1061    #endif
1062                                            stdout_write_wait = 0;
1063                                            loop = 0;
1064                                            break;
1065                                    }
1066                                    else
1067                                    {
1068                                            output_conv_offset += ret;
1069                                            if (output_conv_offset >= output_conv_len) // Output buffer complete
1070                                          {                                          {
1071                                                  output_buf_offset += ret;                                                  output_conv_offset = 0;
1072                                                  if (output_buf_offset >= output_buf_len) // Output buffer complete                                                  output_conv_len = 0;
1073                                                  {                                                  break;
                                                         output_buf_offset = 0;  
                                                         output_buf_len = 0;  
                                                         break;  
                                                 }  
                                                 continue;  
1074                                          }                                          }
1075                                            continue;
1076                                  }                                  }
1077                          }                          }
1078                  }                  }
1079          }          }
1080    
1081            iconv_close(input_cd);
1082            iconv_close(output_cd);
1083    
1084  cleanup:  cleanup:
1085    #ifdef HAVE_SYS_EPOLL_H
1086          if (close(epollfd) < 0)          if (close(epollfd) < 0)
1087          {          {
1088                  log_error("close(epoll) error (%d)\n");                  log_error("close(epoll) error (%d)\n");
1089          }          }
1090    #endif
1091    
1092            if (bbsnet_conf[n].use_ssh)
1093            {
1094                    ssh_channel_free(channel);
1095                    ssh_disconnect(session);
1096                    ssh_free(session);
1097            }
1098    
1099          // Restore STDIN/STDOUT flags          // Restore STDIN/STDOUT flags
1100          fcntl(STDIN_FILENO, F_SETFL, flags_stdin);          fcntl(STDIN_FILENO, F_SETFL, flags_stdin);
1101          fcntl(STDOUT_FILENO, F_SETFL, flags_stdout);          fcntl(STDOUT_FILENO, F_SETFL, flags_stdout);
1102    
         // Restore socket flags  
         fcntl(sock, F_SETFL, flags_sock);  
   
1103          if (close(sock) == -1)          if (close(sock) == -1)
1104          {          {
1105                  log_error("Close socket failed\n");                  log_error("Close socket failed\n");
1106          }          }
1107    
1108          t_used = time(0) - t_used;          t_used = time(NULL) - t_used;
1109          tm_used = gmtime(&t_used);          tm_used = gmtime(&t_used);
1110    
1111          log_common("BBSNET disconnect, %d days %d hours %d minutes %d seconds used\n",          log_common("BBSNET disconnect, %d days %d hours %d minutes %d seconds used\n",
1112                             tm_used->tm_mday - 1, tm_used->tm_hour, tm_used->tm_min,                             tm_used->tm_mday - 1, tm_used->tm_hour, tm_used->tm_min,
1113                             tm_used->tm_sec);                             tm_used->tm_sec);
1114    
1115            BBS_last_access_tm = time(NULL);
1116    
1117          return 0;          return 0;
1118  }  }
1119    
1120  static int  static int bbsnet_refresh()
 bbsnet_refresh()  
1121  {  {
1122          clearscr();          clearscr();
1123          moveto(1, 0);  
1124          prints(" ----------------------------------------------------------------------------- ");          moveto(1, 1);
1125            prints(" ------------------------------------------------------------------------------ ");
1126          for (int i = 2; i < 19; i++)          for (int i = 2; i < 19; i++)
1127          {          {
1128                  moveto(i, 0);                  moveto(i, 1);
1129                  prints("|");                  prints("|");
1130                  moveto(i, 79);                  moveto(i, 80);
1131                  prints("|");                  prints("|");
1132          }          }
1133          moveto(19, 0);          moveto(19, 1);
1134          prints("|-----------------------------------------------------------------------------|");          prints("|------------------------------------------------------------------------------|");
1135          moveto(22, 0);          moveto(22, 1);
1136          prints(" ----------------------------------------------------------------------------- ");          prints(" ------------------------------------------------------------------------------ ");
1137          moveto(23, 0);          moveto(23, 1);
1138          prints(" [\x1b[1;32mCtrl+C\x1b[m]˳");          prints(" [\033[1;32mCtrl+C\033[m]退出");
1139    
1140          iflush();          iflush();
1141    
1142          return 0;          return 0;
1143  }  }
1144    
1145  int bbsnet_selchange()  static int bbsnet_selchange()
1146  {  {
1147          int i = bbsnet_menu.menu_item_pos[0];          int i = bbsnet_menu.menu_item_pos[0];
1148    
1149          moveto(20, 0);          moveto(20, 1);
1150          clrtoeol();          clrtoeol();
1151          prints("|\x1b[1mλ:\x1b[1;33m%-18s\x1b[m  վ:\x1b[1;33m%s\x1b[m",          prints("|\033[1m单位: \033[1;33m%s\033[m%*s  站名: \033[1;33m%s\033[m",
1152                     bbsnet_conf[i].host2, bbsnet_conf[i].host1);                     bbsnet_conf[i].host2, 20 - str_length(bbsnet_conf[i].host2, 1), "", bbsnet_conf[i].host1);
1153          moveto(20, 79);          moveto(20, 80);
1154          prints("|");          prints("|");
1155          moveto(21, 0);          moveto(21, 1);
1156          clrtoeol();          clrtoeol();
1157          prints("|\x1b[1m:\x1b[1;33m%-20s", bbsnet_conf[i].ip);          prints("|\033[1m连往: \033[1;33m%-20s\033[m  端口: \033[1;33m%-5d\033[m            类型: \033[1;33m%s\033[m",
1158          if (bbsnet_conf[i].port != 23)                     bbsnet_conf[i].ip, bbsnet_conf[i].port, (bbsnet_conf[i].use_ssh ? "SSH" : "Telnet"));
1159          {          moveto(21, 80);
                 prints("  %d", bbsnet_conf[i].port);  
         }  
         prints("\x1b[m");  
         moveto(21, 79);  
1160          prints("|");          prints("|");
1161          iflush();          iflush();
1162    
# Line 732  int bbs_net() Line 1169  int bbs_net()
1169    
1170          load_bbsnet_conf(CONF_BBSNET);          load_bbsnet_conf(CONF_BBSNET);
1171    
         BBS_last_access_tm = time(0);  
   
         clearscr();  
1172          bbsnet_refresh();          bbsnet_refresh();
1173          display_menu(&bbsnet_menu);          display_menu(&bbsnet_menu);
1174          bbsnet_selchange();          bbsnet_selchange();
# Line 742  int bbs_net() Line 1176  int bbs_net()
1176          while (!SYS_server_exit)          while (!SYS_server_exit)
1177          {          {
1178                  ch = igetch(100);                  ch = igetch(100);
1179    
1180                    if (ch != KEY_NULL && ch != KEY_TIMEOUT)
1181                    {
1182                            BBS_last_access_tm = time(NULL);
1183                    }
1184    
1185                  switch (ch)                  switch (ch)
1186                  {                  {
1187                  case KEY_NULL: // broken pipe                  case KEY_NULL: // broken pipe
1188                  case KEY_ESC:                          log_error("KEY_NULL\n");
                 case Ctrl('C'): // user cancel  
1189                          goto cleanup;                          goto cleanup;
1190                  case KEY_TIMEOUT:                  case KEY_TIMEOUT:
1191                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time)
1192                          {                          {
1193                                    log_error("User input timeout\n");
1194                                  goto cleanup;                                  goto cleanup;
1195                          }                          }
1196                          continue;                          continue;
1197                    case KEY_ESC:
1198                    case Ctrl('C'): // user cancel
1199                            goto cleanup;
1200                  case CR:                  case CR:
                         igetch_reset();  
1201                          bbsnet_connect(bbsnet_menu.menu_item_pos[0]);                          bbsnet_connect(bbsnet_menu.menu_item_pos[0]);
1202                            // Force cleanup anything remaining in the output buffer
1203                            clearscr();
1204                            iflush();
1205                            // Clear screen and redraw menu
1206                          bbsnet_refresh();                          bbsnet_refresh();
1207                          display_menu(&bbsnet_menu);                          display_menu(&bbsnet_menu);
1208                          bbsnet_selchange();                          bbsnet_selchange();
# Line 798  int bbs_net() Line 1244  int bbs_net()
1244                          bbsnet_selchange();                          bbsnet_selchange();
1245                          break;                          break;
1246                  }                  }
                 BBS_last_access_tm = time(0);  
1247          }          }
1248    
1249  cleanup:  cleanup:


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

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