/[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.52 by sysadm, Wed Jun 18 04:38:20 2025 UTC Revision 1.89 by sysadm, Tue Dec 16 12:59:14 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"
 #include "menu.h"  
18  #include "login.h"  #include "login.h"
19  #include <stdio.h>  #include "menu.h"
20  #include <stdarg.h>  #include "screen.h"
21    #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  {  {
61          char host1[20];          char org_name[40];
62          char host2[40];          char site_name[40];
63          char ip[40];          char host_name[IP_ADDR_LEN];
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    static void unload_bbsnet_conf(void);
72    
73  int load_bbsnet_conf(const char *file_config)  static int load_bbsnet_conf(const char *file_config)
74  {  {
75          FILE *fp;          FILE *fp;
76          MENU *p_menu;          MENU *p_menu;
77          MENU_ITEM *p_menu_item;          MENU_ITEM *p_menu_item;
78          MENU_ITEM_ID menu_item_id;          MENU_ITEM_ID menu_item_id;
79          char t[256], *t1, *t2, *t3, *t4, *saveptr;          char line[LINE_BUFFER_LEN], *t1, *t2, *t3, *t4, *t5, *t6, *saveptr;
80            int port;
81    
82          fp = fopen(file_config, "r");          unload_bbsnet_conf();
         if (fp == NULL)  
         {  
                 return -1;  
         }  
83    
84          bbsnet_menu.p_menu_pool = calloc(1, sizeof(MENU));          bbsnet_menu.p_menu_pool = calloc(1, sizeof(MENU));
85          if (bbsnet_menu.p_menu_pool == NULL)          if (bbsnet_menu.p_menu_pool == NULL)
86          {          {
87                  log_error("calloc(p_menu_pool) error\n");                  log_error("calloc(p_menu_pool) error\n");
88                  return -3;                  return -1;
89          }          }
90          bbsnet_menu.menu_count = 1;          bbsnet_menu.menu_count = 1;
91    
# Line 83  int load_bbsnet_conf(const char *file_co Line 93  int load_bbsnet_conf(const char *file_co
93          if (bbsnet_menu.p_menu_item_pool == NULL)          if (bbsnet_menu.p_menu_item_pool == NULL)
94          {          {
95                  log_error("calloc(p_menu_item_pool) error\n");                  log_error("calloc(p_menu_item_pool) error\n");
96                  return -3;                  unload_bbsnet_conf();
97                    return -1;
98          }          }
99          bbsnet_menu.menu_item_count = MAXSTATION;          bbsnet_menu.menu_item_count = MAXSTATION;
100    
# Line 94  int load_bbsnet_conf(const char *file_co Line 105  int load_bbsnet_conf(const char *file_co
105          p_menu->title.show = 0;          p_menu->title.show = 0;
106          p_menu->screen_show = 0;          p_menu->screen_show = 0;
107    
108            fp = fopen(file_config, "r");
109            if (fp == NULL)
110            {
111                    unload_bbsnet_conf();
112                    return -2;
113            }
114    
115          menu_item_id = 0;          menu_item_id = 0;
116          while (fgets(t, 255, fp) && menu_item_id < MAXSTATION)          while (fgets(line, sizeof(line), fp) && menu_item_id < MAXSTATION)
117          {          {
118                  t1 = strtok_r(t, MENU_CONF_DELIM, &saveptr);                  t1 = strtok_r(line, MENU_CONF_DELIM, &saveptr);
119                  t2 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);                  t2 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
120                  t3 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);                  t3 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
121                  t4 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);                  t4 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
122                    t5 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
123                    t6 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
124    
125                  if (t1 == NULL || t2 == NULL || t3 == NULL || t4 == NULL || t[0] == '#' || t[0] == '*')                  if (t1 == NULL || t2 == NULL || t3 == NULL || t4 == NULL ||
126                            t5 == NULL || t6 == NULL || line[0] == '#' || line[0] == '*')
127                  {                  {
128                          continue;                          continue;
129                  }                  }
130    
131                  strncpy(bbsnet_conf[menu_item_id].host1, t2, sizeof(bbsnet_conf[menu_item_id].host1) - 1);                  strncpy(bbsnet_conf[menu_item_id].site_name, t2, sizeof(bbsnet_conf[menu_item_id].site_name) - 1);
132                  bbsnet_conf[menu_item_id].host1[sizeof(bbsnet_conf[menu_item_id].host1) - 1] = '\0';                  bbsnet_conf[menu_item_id].site_name[sizeof(bbsnet_conf[menu_item_id].site_name) - 1] = '\0';
133                  strncpy(bbsnet_conf[menu_item_id].host2, t1, sizeof(bbsnet_conf[menu_item_id].host2) - 1);                  strncpy(bbsnet_conf[menu_item_id].org_name, t1, sizeof(bbsnet_conf[menu_item_id].org_name) - 1);
134                  bbsnet_conf[menu_item_id].host2[sizeof(bbsnet_conf[menu_item_id].host2) - 1] = '\0';                  bbsnet_conf[menu_item_id].org_name[sizeof(bbsnet_conf[menu_item_id].org_name) - 1] = '\0';
135                  strncpy(bbsnet_conf[menu_item_id].ip, t3, sizeof(bbsnet_conf[menu_item_id].ip) - 1);                  strncpy(bbsnet_conf[menu_item_id].host_name, t3, sizeof(bbsnet_conf[menu_item_id].host_name) - 1);
136                  bbsnet_conf[menu_item_id].ip[sizeof(bbsnet_conf[menu_item_id].ip) - 1] = '\0';                  bbsnet_conf[menu_item_id].host_name[sizeof(bbsnet_conf[menu_item_id].host_name) - 1] = '\0';
137                  bbsnet_conf[menu_item_id].port = (in_port_t)(t4 ? atoi(t4) : 23);                  port = atoi(t4);
138                    if (port <= 0 || port > 65535)
139                    {
140                            log_error("Invalid port value %d of menu item %d\n", port, menu_item_id);
141                            fclose(fp);
142                            unload_bbsnet_conf();
143                            return -3;
144                    }
145                    bbsnet_conf[menu_item_id].port = (in_port_t)port;
146                    bbsnet_conf[menu_item_id].use_ssh = (toupper(t5[0]) == 'Y');
147                    strncpy(bbsnet_conf[menu_item_id].charset, t6, sizeof(bbsnet_conf[menu_item_id].charset) - 1);
148                    bbsnet_conf[menu_item_id].charset[sizeof(bbsnet_conf[menu_item_id].charset) - 1] = '\0';
149    
150                  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);
151                  if (p_menu_item == NULL)                  if (p_menu_item == NULL)
152                  {                  {
153                          log_error("get_menu_item_by_id(%d) return NULL pointer\n", menu_item_id);                          log_error("get_menu_item_by_id(%d) return NULL pointer\n", menu_item_id);
154                          return -1;                          fclose(fp);
155                            unload_bbsnet_conf();
156                            return -3;
157                  }                  }
158    
159                  p_menu_item->row = (int16_t)(2 + menu_item_id / STATION_PER_LINE);                  p_menu_item->row = (int16_t)(2 + menu_item_id / STATION_PER_LINE);
# Line 131  int load_bbsnet_conf(const char *file_co Line 165  int load_bbsnet_conf(const char *file_co
165                  p_menu_item->name[0] =                  p_menu_item->name[0] =
166                          (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);
167                  p_menu_item->name[1] = '\0';                  p_menu_item->name[1] = '\0';
168                  snprintf(p_menu_item->text, sizeof(p_menu_item->text), "%c. %s",                  snprintf(p_menu_item->text, sizeof(p_menu_item->text), "\033[1;36m%c.\033[m %s",
169                                   p_menu_item->name[0], bbsnet_conf[menu_item_id].host1);                                   p_menu_item->name[0], bbsnet_conf[menu_item_id].site_name);
170    
171                  p_menu->items[p_menu->item_count] = menu_item_id;                  p_menu->items[p_menu->item_count] = menu_item_id;
172                  p_menu->item_count++;                  p_menu->item_count++;
# Line 149  int load_bbsnet_conf(const char *file_co Line 183  int load_bbsnet_conf(const char *file_co
183          return 0;          return 0;
184  }  }
185    
186  void unload_bbsnet_conf(void)  static void unload_bbsnet_conf(void)
187  {  {
188          bbsnet_menu.menu_count = 0;          bbsnet_menu.menu_count = 0;
189          bbsnet_menu.menu_item_count = 0;          bbsnet_menu.menu_item_count = 0;
190    
191          free(bbsnet_menu.p_menu_pool);          if (bbsnet_menu.p_menu_pool)
192          bbsnet_menu.p_menu_pool = NULL;          {
193          free(bbsnet_menu.p_menu_item_pool);                  free(bbsnet_menu.p_menu_pool);
194          bbsnet_menu.p_menu_item_pool = NULL;                  bbsnet_menu.p_menu_pool = NULL;
195            }
196    
197            if (bbsnet_menu.p_menu_item_pool)
198            {
199                    free(bbsnet_menu.p_menu_item_pool);
200                    bbsnet_menu.p_menu_item_pool = NULL;
201            }
202  }  }
203    
204  void process_bar(int n, int len)  static void process_bar(int n, int len)
205  {  {
206          char buf[LINE_BUFFER_LEN];          char buf[LINE_BUFFER_LEN];
207          char buf2[LINE_BUFFER_LEN];          char buf2[LINE_BUFFER_LEN];
208    
209          if (len > LINE_BUFFER_LEN)          if (len <= 0)
210            {
211                    len = 1;
212            }
213            else if (len > LINE_BUFFER_LEN)
214          {          {
215                  len = LINE_BUFFER_LEN - 1;                  len = LINE_BUFFER_LEN - 1;
216          }          }
# Line 178  void process_bar(int n, int len) Line 223  void process_bar(int n, int len)
223                  n = len;                  n = len;
224          }          }
225    
226          moveto(4, 0);          moveto(4, 1);
227          prints(" ------------------------------ \r\n");          prints(" ------------------------------ \r\n");
228          snprintf(buf, sizeof(buf), "            %3d%%              ", n * 100 / len);          snprintf(buf, sizeof(buf), "            %3d%%              ", n * 100 / len);
229          strncpy(buf2, buf, (size_t)n);          memcpy(buf2, buf, (size_t)n);
230          buf2[n] = '\0';          buf2[n] = '\0';
231          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);
232          prints(" ------------------------------ \r\n");          prints(" ------------------------------ \r\n");
233          iflush();          iflush();
234  }  }
235    
236  int bbsnet_connect(int n)  static int bbsnet_connect(int n)
237  {  {
238          int sock, ret, loop, error;          int sock = -1;
239            int ret;
240            int loop;
241            int error;
242          int sock_connected = 0;          int sock_connected = 0;
243          int flags_sock;          int flags_sock = -1;
244          int flags_stdin;          int flags_stdin = -1;
245          int flags_stdout;          int flags_stdout = -1;
         int len;  
246          struct sockaddr_in sin;          struct sockaddr_in sin;
247          char input_buf[LINE_BUFFER_LEN];          char input_buf[LINE_BUFFER_LEN];
248          char output_buf[LINE_BUFFER_LEN];          char output_buf[LINE_BUFFER_LEN];
# Line 203  int bbsnet_connect(int n) Line 250  int bbsnet_connect(int n)
250          int output_buf_len = 0;          int output_buf_len = 0;
251          int input_buf_offset = 0;          int input_buf_offset = 0;
252          int output_buf_offset = 0;          int output_buf_offset = 0;
253            char input_conv[LINE_BUFFER_LEN * 2];
254            char output_conv[LINE_BUFFER_LEN * 2];
255            int input_conv_len = 0;
256            int output_conv_len = 0;
257            int input_conv_offset = 0;
258            int output_conv_offset = 0;
259            iconv_t input_cd = (iconv_t)(-1);
260            iconv_t output_cd = (iconv_t)(-1);
261            char tocode[CHARSET_MAX_LEN + 20];
262    
263    #ifdef HAVE_SYS_EPOLL_H
264          struct epoll_event ev, events[MAX_EVENTS];          struct epoll_event ev, events[MAX_EVENTS];
265          int nfds, epollfd;          int epollfd = -1;
266    #else
267            struct pollfd pfds[3];
268    #endif
269    
270            int nfds;
271          int stdin_read_wait = 0;          int stdin_read_wait = 0;
272          int stdout_write_wait = 0;          int stdout_write_wait = 0;
273          int sock_read_wait = 0;          int sock_read_wait = 0;
# Line 213  int bbsnet_connect(int n) Line 276  int bbsnet_connect(int n)
276          int tos;          int tos;
277          char remote_addr[IP_ADDR_LEN];          char remote_addr[IP_ADDR_LEN];
278          int remote_port;          int remote_port;
279          time_t t_used;          char local_addr[IP_ADDR_LEN];
280            int local_port;
281            socklen_t sock_len;
282            time_t t_used = time(NULL);
283          struct tm *tm_used;          struct tm *tm_used;
284          int ch;          int ch;
285            char remote_user[USERNAME_MAX_LEN + 1];
286            char remote_pass[PASSWORD_MAX_LEN + 1];
287            ssh_session session = NULL;
288            ssh_channel channel = NULL;
289            int ssh_process_config = 0;
290            int ssh_log_level = SSH_LOG_NOLOG;
291            long ssh_timeout = 0;
292    
293            if (user_online_update("BBS_NET") < 0)
294            {
295                    log_error("user_online_update(BBS_NET) error\n");
296            }
297    
298            if (bbsnet_conf[n].use_ssh)
299            {
300                    clearscr();
301    
302                    if (!SSH_v2)
303                    {
304                            moveto(1, 1);
305                            prints("只有在以SSH方式登陆本站时,才能使用SSH站点穿梭。");
306                            press_any_key();
307                            return 0;
308                    }
309    
310                    moveto(1, 1);
311                    prints("通过SSH方式连接[%s]...", bbsnet_conf[n].site_name);
312                    moveto(2, 1);
313                    prints("请输入用户名: ");
314                    iflush();
315                    if (str_input(remote_user, sizeof(remote_user), DOECHO) < 0)
316                    {
317                            return -1;
318                    }
319                    if (remote_user[0] == '\0')
320                    {
321                            return 0;
322                    }
323    
324                    moveto(3, 1);
325                    prints("请输入密码: ");
326                    iflush();
327                    if (str_input(remote_pass, sizeof(remote_pass), NOECHO) < 0)
328                    {
329                            return -1;
330                    }
331                    if (remote_pass[0] == '\0')
332                    {
333                            return 0;
334                    }
335            }
336    
337          clearscr();          clearscr();
338    
339          moveto(0, 0);          moveto(1, 1);
340          prints("\033[1;32mڲ %s (%s) ӣԺ... \033[m\r\n",          prints("\033[1;32m正在测试往 %s (%s) 的连接,请稍候... \033[m\r\n",
341                     bbsnet_conf[n].host1, bbsnet_conf[n].ip);                     bbsnet_conf[n].site_name, bbsnet_conf[n].host_name);
342          iflush();          iflush();
343    
344          p_host = gethostbyname(bbsnet_conf[n].ip);          p_host = gethostbyname(bbsnet_conf[n].host_name);
345    
346          if (p_host == NULL)          if (p_host == NULL)
347          {          {
348                  prints("\033[1;31mʧܣ\033[m\r\n");                  prints("\033[1;31m查找主机名失败!\033[m\r\n");
349                  press_any_key();                  press_any_key();
350                  return -1;                  goto cleanup;
351          }          }
352    
353          sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);          sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
354    
355          if (sock < 0)          if (sock < 0)
356          {          {
357                  prints("\033[1;31m޷socket\033[m\r\n");                  prints("\033[1;31m无法创建socket!\033[m\r\n");
358                  press_any_key();                  press_any_key();
359                  return -1;                  goto cleanup;
360          }          }
361    
362          sin.sin_family = AF_INET;          sin.sin_family = AF_INET;
# Line 250  int bbsnet_connect(int n) Line 367  int bbsnet_connect(int n)
367          {          {
368                  log_error("Bind address %s:%u failed (%d)\n",                  log_error("Bind address %s:%u failed (%d)\n",
369                                    inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), errno);                                    inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), errno);
370                  return -2;                  goto cleanup;
371          }          }
372    
373          memset(&sin, 0, sizeof(sin));          memset(&sin, 0, sizeof(sin));
# Line 262  int bbsnet_connect(int n) Line 379  int bbsnet_connect(int n)
379          remote_addr[sizeof(remote_addr) - 1] = '\0';          remote_addr[sizeof(remote_addr) - 1] = '\0';
380          remote_port = ntohs(sin.sin_port);          remote_port = ntohs(sin.sin_port);
381    
382          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");
383          process_bar(0, MAX_PROCESS_BAR_LEN);          process_bar(0, MAX_PROCESS_BAR_LEN);
384    
385          // Set socket as non-blocking          // Set socket as non-blocking
386          flags_sock = fcntl(sock, F_GETFL, 0);          if ((flags_sock = fcntl(sock, F_GETFL, 0)) == -1)
387          fcntl(sock, F_SETFL, flags_sock | O_NONBLOCK);          {
388                    log_error("fcntl(F_GETFL) error (%d)\n", errno);
389                    goto cleanup;
390            }
391            if ((fcntl(sock, F_SETFL, flags_sock | O_NONBLOCK)) == -1)
392            {
393                    log_error("fcntl(F_SETFL) error (%d)\n", errno);
394                    goto cleanup;
395            }
396    
397          // Set STDIN/STDOUT as non-blocking          // Set STDIN/STDOUT as non-blocking
398          flags_stdin = fcntl(STDIN_FILENO, F_GETFL, 0);          if ((flags_stdin = fcntl(STDIN_FILENO, F_GETFL, 0)) == -1)
399          flags_stdout = fcntl(STDOUT_FILENO, F_GETFL, 0);          {
400          fcntl(STDIN_FILENO, F_SETFL, flags_stdin | O_NONBLOCK);                  log_error("fcntl(F_GETFL) error (%d)\n", errno);
401          fcntl(STDOUT_FILENO, F_SETFL, flags_stdout | O_NONBLOCK);                  goto cleanup;
402            }
403            if ((flags_stdout = fcntl(STDOUT_FILENO, F_GETFL, 0)) == -1)
404            {
405                    log_error("fcntl(F_GETFL) error (%d)\n", errno);
406                    goto cleanup;
407            }
408            if ((fcntl(STDIN_FILENO, F_SETFL, flags_stdin | O_NONBLOCK)) == -1)
409            {
410                    log_error("fcntl(F_SETFL) error (%d)\n", errno);
411                    goto cleanup;
412            }
413            if ((fcntl(STDOUT_FILENO, F_SETFL, flags_stdout | O_NONBLOCK)) == -1)
414            {
415                    log_error("fcntl(F_SETFL) error (%d)\n", errno);
416                    goto cleanup;
417            }
418    
419    #ifdef HAVE_SYS_EPOLL_H
420          epollfd = epoll_create1(0);          epollfd = epoll_create1(0);
421          if (epollfd < 0)          if (epollfd < 0)
422          {          {
423                  log_error("epoll_create1() error (%d)\n", errno);                  log_error("epoll_create1() error (%d)\n", errno);
424                  return -1;                  goto cleanup;
425          }          }
426    
427          ev.events = EPOLLOUT;          ev.events = EPOLLOUT | EPOLLET;
428          ev.data.fd = sock;          ev.data.fd = sock;
429          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sock, &ev) == -1)          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sock, &ev) == -1)
430          {          {
# Line 290  int bbsnet_connect(int n) Line 432  int bbsnet_connect(int n)
432                  goto cleanup;                  goto cleanup;
433          }          }
434    
435          ev.events = EPOLLIN;          ev.events = EPOLLIN | EPOLLET;
436          ev.data.fd = STDIN_FILENO;          ev.data.fd = STDIN_FILENO;
437          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1)          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1)
438          {          {
439                  log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno);                  log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno);
440                  goto cleanup;                  goto cleanup;
441          }          }
442    #endif
443    
444          while (!SYS_server_exit)          while (!SYS_server_exit)
445          {          {
# Line 317  int bbsnet_connect(int n) Line 460  int bbsnet_connect(int n)
460                          {                          {
461                                  log_error("connect(socket) error (%d)\n", errno);                                  log_error("connect(socket) error (%d)\n", errno);
462    
463                                  prints("\033[1;31mʧܣ\033[m\r\n");                                  prints("\033[1;31m连接失败!\033[m\r\n");
464                                  press_any_key();                                  press_any_key();
465    
466                                  goto cleanup;                                  goto cleanup;
# Line 327  int bbsnet_connect(int n) Line 470  int bbsnet_connect(int n)
470    
471          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++)
472          {          {
473    #ifdef HAVE_SYS_EPOLL_H
474                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 500); // 0.5 second                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 500); // 0.5 second
475                    ret = nfds;
476    #else
477                    pfds[0].fd = sock;
478                    pfds[0].events = POLLOUT;
479                    pfds[1].fd = STDIN_FILENO;
480                    pfds[1].events = POLLIN;
481                    nfds = 2;
482                    ret = poll(pfds, (nfds_t)nfds, 500); // 0.5 second
483    #endif
484    
485                  if (nfds < 0)                  if (ret < 0)
486                  {                  {
487                          if (errno != EINTR)                          if (errno != EINTR)
488                          {                          {
489    #ifdef HAVE_SYS_EPOLL_H
490                                  log_error("epoll_wait() error (%d)\n", errno);                                  log_error("epoll_wait() error (%d)\n", errno);
491    #else
492                                    log_error("poll() error (%d)\n", errno);
493    #endif
494                                  break;                                  break;
495                          }                          }
496                  }                  }
497                  else if (nfds == 0) // timeout                  else if (ret == 0) // timeout
498                  {                  {
499                          process_bar(j + 1, MAX_PROCESS_BAR_LEN);                          process_bar(j + 1, MAX_PROCESS_BAR_LEN);
500                  }                  }
# Line 345  int bbsnet_connect(int n) Line 502  int bbsnet_connect(int n)
502                  {                  {
503                          for (int i = 0; i < nfds; i++)                          for (int i = 0; i < nfds; i++)
504                          {                          {
505    #ifdef HAVE_SYS_EPOLL_H
506                                  if (events[i].data.fd == sock)                                  if (events[i].data.fd == sock)
507    #else
508                                    if (pfds[i].fd == sock && (pfds[i].revents & POLLOUT))
509    #endif
510                                  {                                  {
511                                          len = sizeof(error);                                          socklen_t len = sizeof(error);
512                                          if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, (socklen_t *)&len) < 0)                                          if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
513                                          {                                          {
514                                                  log_error("getsockopt() error (%d) !\n", error);                                                  log_error("getsockopt() error (%d) !\n", errno);
515                                                  goto cleanup;                                                  goto cleanup;
516                                          }                                          }
517                                          if (error == 0)                                          if (error == 0)
# Line 358  int bbsnet_connect(int n) Line 519  int bbsnet_connect(int n)
519                                                  sock_connected = 1;                                                  sock_connected = 1;
520                                          }                                          }
521                                  }                                  }
522    #ifdef HAVE_SYS_EPOLL_H
523                                  else if (events[i].data.fd == STDIN_FILENO)                                  else if (events[i].data.fd == STDIN_FILENO)
524    #else
525                                    else if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & POLLIN))
526    #endif
527                                  {                                  {
528                                          ch = igetch(0);                                          do
529                                            {
530                                                    ch = igetch(0);
531                                            } while (ch == 0);
532                                          if (ch == Ctrl('C') || ch == KEY_ESC)                                          if (ch == Ctrl('C') || ch == KEY_ESC)
533                                          {                                          {
534                                                  goto cleanup;                                                  goto cleanup;
# Line 375  int bbsnet_connect(int n) Line 543  int bbsnet_connect(int n)
543          }          }
544          if (!sock_connected)          if (!sock_connected)
545          {          {
546                  prints("\033[1;31mʧܣ\033[m\r\n");                  prints("\033[1;31m连接失败!\033[m\r\n");
547                  press_any_key();                  press_any_key();
548    
549                  goto cleanup;                  goto cleanup;
# Line 387  int bbsnet_connect(int n) Line 555  int bbsnet_connect(int n)
555                  log_error("setsockopt IP_TOS=%d error (%d)\n", tos, errno);                  log_error("setsockopt IP_TOS=%d error (%d)\n", tos, errno);
556          }          }
557    
558          prints("\033[1;31mӳɹ\033[m\r\n");          sock_len = sizeof(sin);
559            if (getsockname(sock, (struct sockaddr *)&sin, &sock_len) < 0)
560            {
561                    log_error("getsockname() error: %d", errno);
562                    goto cleanup;
563            }
564    
565            strncpy(local_addr, inet_ntoa(sin.sin_addr), sizeof(local_addr) - 1);
566            local_addr[sizeof(local_addr) - 1] = '\0';
567            local_port = ntohs(sin.sin_port);
568    
569            if (bbsnet_conf[n].use_ssh)
570            {
571                    session = ssh_new();
572                    if (session == NULL)
573                    {
574                            log_error("ssh_new() error\n");
575                            goto cleanup;
576                    }
577    
578                    if (ssh_options_set(session, SSH_OPTIONS_FD, &sock) < 0 ||
579                            ssh_options_set(session, SSH_OPTIONS_PROCESS_CONFIG, &ssh_process_config) < 0 ||
580                            ssh_options_set(session, SSH_OPTIONS_KNOWNHOSTS, SSH_KNOWN_HOSTS_FILE) < 0 ||
581                            ssh_options_set(session, SSH_OPTIONS_HOST, bbsnet_conf[n].host_name) < 0 ||
582                            ssh_options_set(session, SSH_OPTIONS_USER, remote_user) < 0 ||
583                            ssh_options_set(session, SSH_OPTIONS_HOSTKEYS, "+ssh-rsa") < 0 ||
584                            ssh_options_set(session, SSH_OPTIONS_LOG_VERBOSITY, &ssh_log_level) < 0)
585                    {
586                            log_error("Error setting SSH options: %s\n", ssh_get_error(session));
587                            goto cleanup;
588                    }
589    
590                    ssh_timeout = 5; // second
591                    if (ssh_options_set(session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0)
592                    {
593                            log_error("Error setting SSH options: %s\n", ssh_get_error(session));
594                            goto cleanup;
595                    }
596    
597                    while (!SYS_server_exit)
598                    {
599                            ret = ssh_connect(session);
600                            if (ret == SSH_OK)
601                            {
602                                    break;
603                            }
604                            else if (ret == SSH_ERROR)
605                            {
606                                    log_error("ssh_connect() error\n");
607                                    goto cleanup;
608                            }
609                    }
610    
611                    ret = ssh_session_is_known_server(session);
612                    switch (ret)
613                    {
614                    case SSH_KNOWN_HOSTS_NOT_FOUND:
615                    case SSH_KNOWN_HOSTS_UNKNOWN:
616                            if (ssh_session_update_known_hosts(session) != SSH_OK)
617                            {
618                                    log_error("ssh_session_update_known_hosts(%s) error\n", bbsnet_conf[n].host_name);
619                                    prints("\033[1;31m无法添加服务器证书\033[m");
620                                    press_any_key();
621                                    goto cleanup;
622                            }
623                            log_common("SSH key of (%s) is added into %s\n", bbsnet_conf[n].host_name, SSH_KNOWN_HOSTS_FILE);
624                    case SSH_KNOWN_HOSTS_OK:
625                            break;
626                    case SSH_KNOWN_HOSTS_CHANGED:
627                    case SSH_KNOWN_HOSTS_OTHER:
628                            log_error("ssh_session_is_known_server(%s) error: %d\n", bbsnet_conf[n].host_name, ret);
629                            prints("\033[1;31m服务器证书已变更\033[m");
630                            press_any_key();
631                            goto cleanup;
632                    }
633    
634                    for (int i = 0; !SYS_server_exit;)
635                    {
636                            ret = ssh_userauth_password(session, NULL, remote_pass);
637                            if (ret == SSH_AUTH_SUCCESS)
638                            {
639                                    break;
640                            }
641                            else if (ret == SSH_AUTH_AGAIN)
642                            {
643    #ifdef _DEBUG
644                                    log_error("ssh_userauth_password() error: SSH_AUTH_AGAIN\n");
645    #endif
646                            }
647                            else if (ret == SSH_AUTH_ERROR)
648                            {
649                                    log_error("ssh_userauth_password() error: %d\n", ret);
650                                    goto cleanup;
651                            }
652                            else // if (ret == SSH_AUTH_DENIED)
653                            {
654                                    prints("\033[1;31m身份验证失败!\033[m\r\n");
655                                    i++;
656                                    if (i < BBS_login_retry_times)
657                                    {
658                                            prints("请输入密码: ");
659                                            iflush();
660                                            if (str_input(remote_pass, sizeof(remote_pass), NOECHO) < 0)
661                                            {
662                                                    goto cleanup;
663                                            }
664                                            if (remote_pass[0] == '\0')
665                                            {
666                                                    goto cleanup;
667                                            }
668                                    }
669                                    else
670                                    {
671                                            goto cleanup;
672                                    }
673                            }
674                    }
675    
676                    channel = ssh_channel_new(session);
677                    if (channel == NULL)
678                    {
679                            log_error("ssh_channel_new() error\n");
680                            goto cleanup;
681                    }
682    
683                    while (!SYS_server_exit)
684                    {
685                            ret = ssh_channel_open_session(channel);
686                            if (ret == SSH_OK)
687                            {
688                                    break;
689                            }
690                            else if (ret == SSH_ERROR)
691                            {
692                                    log_error("ssh_channel_open_session() error\n");
693                                    goto cleanup;
694                            }
695                    }
696    
697                    while (!SYS_server_exit)
698                    {
699                            ret = ssh_channel_request_pty(channel);
700                            if (ret == SSH_OK)
701                            {
702                                    break;
703                            }
704                            else if (ret == SSH_ERROR)
705                            {
706                                    log_error("ssh_channel_request_pty() error\n");
707                                    goto cleanup;
708                            }
709                    }
710    
711                    while (!SYS_server_exit)
712                    {
713                            ret = ssh_channel_request_shell(channel);
714                            if (ret == SSH_OK)
715                            {
716                                    break;
717                            }
718                            else if (ret == SSH_ERROR)
719                            {
720                                    log_error("ssh_channel_request_shell() error\n");
721                                    goto cleanup;
722                            }
723                    }
724    
725                    ssh_timeout = 0; // second
726                    if (ssh_options_set(session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0)
727                    {
728                            log_error("Error setting SSH options: %s\n", ssh_get_error(session));
729                            goto cleanup;
730                    }
731    
732                    ssh_set_blocking(session, 0);
733            }
734    
735            prints("\033[1;31m连接成功!\033[m\r\n");
736          iflush();          iflush();
737          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",
738                               remote_addr, remote_port, local_addr, local_port, BBS_username);
739    
740            snprintf(tocode, sizeof(tocode), "%s%s", bbsnet_conf[n].charset,
741                             (strcasecmp(stdio_charset, bbsnet_conf[n].charset) == 0 ? "" : "//IGNORE"));
742            input_cd = iconv_open(tocode, stdio_charset);
743            if (input_cd == (iconv_t)(-1))
744            {
745                    log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, tocode, errno);
746                    goto cleanup;
747            }
748    
749            snprintf(tocode, sizeof(tocode), "%s%s", stdio_charset,
750                             (strcasecmp(bbsnet_conf[n].charset, stdio_charset) == 0 ? "" : "//TRANSLIT"));
751            output_cd = iconv_open(tocode, bbsnet_conf[n].charset);
752            if (output_cd == (iconv_t)(-1))
753            {
754                    log_error("iconv_open(%s->%s) error: %d\n", bbsnet_conf[n].charset, tocode, errno);
755                    goto cleanup;
756            }
757    
758    #ifdef HAVE_SYS_EPOLL_H
759          ev.events = EPOLLIN | EPOLLOUT | EPOLLET;          ev.events = EPOLLIN | EPOLLOUT | EPOLLET;
760          ev.data.fd = sock;          ev.data.fd = sock;
761          if (epoll_ctl(epollfd, EPOLL_CTL_MOD, sock, &ev) == -1)          if (epoll_ctl(epollfd, EPOLL_CTL_MOD, sock, &ev) == -1)
# Line 399  int bbsnet_connect(int n) Line 764  int bbsnet_connect(int n)
764                  goto cleanup;                  goto cleanup;
765          }          }
766    
767          ev.events = EPOLLOUT;          ev.events = EPOLLOUT | EPOLLET;
768          ev.data.fd = STDOUT_FILENO;          ev.data.fd = STDOUT_FILENO;
769          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1)          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1)
770          {          {
771                  log_error("epoll_ctl(STDOUT_FILENO) error (%d)\n", errno);                  log_error("epoll_ctl(STDOUT_FILENO) error (%d)\n", errno);
772                  goto cleanup;                  goto cleanup;
773          }          }
774    #endif
775    
776          BBS_last_access_tm = t_used = time(NULL);          BBS_last_access_tm = t_used = time(NULL);
777          loop = 1;          loop = 1;
# Line 419  int bbsnet_connect(int n) Line 785  int bbsnet_connect(int n)
785                          break;                          break;
786                  }                  }
787    
788                    if (bbsnet_conf[n].use_ssh && ssh_channel_is_closed(channel))
789                    {
790                            log_error("Remote SSH channel is closed\n");
791                            loop = 0;
792                            break;
793                    }
794    
795    #ifdef HAVE_SYS_EPOLL_H
796                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second
797                    ret = nfds;
798    #else
799                    pfds[0].fd = STDIN_FILENO;
800                    pfds[0].events = POLLIN;
801                    pfds[1].fd = sock;
802                    pfds[1].events = POLLIN | POLLOUT;
803                    pfds[2].fd = STDOUT_FILENO;
804                    pfds[2].events = POLLOUT;
805                    nfds = 3;
806                    ret = poll(pfds, (nfds_t)nfds, 100); // 0.1 second
807    #endif
808    
809                  if (nfds < 0)                  if (ret < 0)
810                  {                  {
811                          if (errno != EINTR)                          if (errno != EINTR)
812                          {                          {
813    #ifdef HAVE_SYS_EPOLL_H
814                                  log_error("epoll_wait() error (%d)\n", errno);                                  log_error("epoll_wait() error (%d)\n", errno);
815    #else
816                                    log_error("poll() error (%d)\n", errno);
817    #endif
818                                  break;                                  break;
819                          }                          }
820                          continue;                          continue;
821                  }                  }
822                  else if (nfds == 0) // timeout                  else if (ret == 0) // timeout
823                  {                  {
824                          if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time)
825                          {                          {
826                                  break;                                  break;
827                          }                          }
                         continue;  
828                  }                  }
829    
830                  for (int i = 0; i < nfds; i++)                  for (int i = 0; i < nfds; i++)
831                  {                  {
832                          if (events[i].data.fd == STDIN_FILENO || stdin_read_wait)  #ifdef HAVE_SYS_EPOLL_H
833                            if (events[i].data.fd == STDIN_FILENO)
834    #else
835                            if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & POLLIN))
836    #endif
837                          {                          {
838                                  stdin_read_wait = 1;                                  stdin_read_wait = 1;
839                                  while (input_buf_len < sizeof(input_buf) && !SYS_server_exit)                          }
840    
841    #ifdef HAVE_SYS_EPOLL_H
842                            if (events[i].data.fd == sock)
843    #else
844                            if (pfds[i].fd == sock)
845    #endif
846                            {
847    #ifdef HAVE_SYS_EPOLL_H
848                                    if (events[i].events & EPOLLIN)
849    #else
850                                    if (pfds[i].revents & POLLIN)
851    #endif
852                                  {                                  {
853                                          if (SSH_v2)                                          sock_read_wait = 1;
854                                    }
855    
856    #ifdef HAVE_SYS_EPOLL_H
857                                    if (events[i].events & EPOLLOUT)
858    #else
859                                    if (pfds[i].revents & POLLOUT)
860    #endif
861                                    {
862                                            sock_write_wait = 1;
863                                    }
864                            }
865    
866    #ifdef HAVE_SYS_EPOLL_H
867                            if (events[i].data.fd == STDOUT_FILENO)
868    #else
869                            if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & POLLOUT))
870    #endif
871                            {
872                                    stdout_write_wait = 1;
873                            }
874                    }
875    
876                    if (stdin_read_wait)
877                    {
878                            while (input_buf_len < sizeof(input_buf) && !SYS_server_exit)
879                            {
880                                    if (SSH_v2)
881                                    {
882                                            ret = ssh_channel_read_nonblocking(SSH_channel, input_buf + input_buf_len, sizeof(input_buf) - (uint32_t)input_buf_len, 0);
883                                            if (ret == SSH_ERROR)
884                                          {                                          {
885                                                  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));
886                                                  if (ret == SSH_ERROR)                                                  loop = 0;
887                                                  {                                                  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  
                                                 }  
888                                          }                                          }
889                                          else                                          else if (ret == SSH_EOF)
890                                          {                                          {
891                                                  ret = (int)read(STDIN_FILENO, input_buf + input_buf_len, sizeof(input_buf) - (size_t)input_buf_len);                                                  stdin_read_wait = 0;
892                                                    loop = 0;
893                                                    break;
894                                          }                                          }
895                                          if (ret < 0)                                          else if (ret == 0)
896                                          {                                          {
897                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)                                                  // Send NO-OP to remote server
898                                                  {                                                  input_buf[input_buf_len] = '\0';
899                                                          stdin_read_wait = 0;                                                  input_buf_len++;
900                                                          break;                                                  BBS_last_access_tm = time(NULL);
901                                                  }  
902                                                  else if (errno == EINTR)                                                  stdin_read_wait = 0;
903                                                  {                                                  break; // Check whether channel is still open
                                                         continue;  
                                                 }  
                                                 else  
                                                 {  
                                                         log_error("read(STDIN) error (%d)\n", errno);  
                                                         loop = 0;  
                                                         break;  
                                                 }  
904                                          }                                          }
905                                          else if (ret == 0) // broken pipe                                  }
906                                    else
907                                    {
908                                            ret = (int)read(STDIN_FILENO, input_buf + input_buf_len, sizeof(input_buf) - (size_t)input_buf_len);
909                                    }
910                                    if (ret < 0)
911                                    {
912                                            if (errno == EAGAIN || errno == EWOULDBLOCK)
913                                          {                                          {
                                                 log_common("read(STDIN) EOF\n");  
914                                                  stdin_read_wait = 0;                                                  stdin_read_wait = 0;
                                                 loop = 0;  
915                                                  break;                                                  break;
916                                          }                                          }
917                                          else                                          else if (errno == EINTR)
918                                          {                                          {
                                                 input_buf_len += ret;  
                                                 BBS_last_access_tm = time(NULL);  
919                                                  continue;                                                  continue;
920                                          }                                          }
921                                            else
922                                            {
923                                                    log_error("read(STDIN) error (%d)\n", errno);
924                                                    loop = 0;
925                                                    break;
926                                            }
927                                    }
928                                    else if (ret == 0) // broken pipe
929                                    {
930    #ifdef _DEBUG
931                                            log_error("read(STDIN) EOF\n");
932    #endif
933                                            stdin_read_wait = 0;
934                                            loop = 0;
935                                            break;
936                                    }
937                                    else
938                                    {
939                                            input_buf_len += ret;
940                                            BBS_last_access_tm = time(NULL);
941    
942                                            // Refresh current action while user input
943                                            if (user_online_update("BBS_NET") < 0)
944                                            {
945                                                    log_error("user_online_update(BBS_NET) error\n");
946                                            }
947    
948                                            continue;
949                                    }
950                            }
951                    }
952    
953                    if (sock_write_wait)
954                    {
955                            if (input_buf_offset < input_buf_len)
956                            {
957                                    // For debug
958    #ifdef _DEBUG
959                                    for (int j = input_buf_offset; j < input_buf_len; j++)
960                                    {
961                                            log_error("Debug input: <--[%u]\n", (input_buf[j] + 256) % 256);
962                                  }                                  }
963    #endif
964    
965                                    ret = io_buf_conv(input_cd, input_buf, &input_buf_len, &input_buf_offset, input_conv, sizeof(input_conv), &input_conv_len);
966                                    if (ret < 0)
967                                    {
968                                            log_error("io_buf_conv(input, %d, %d, %d) error\n", input_buf_len, input_buf_offset, input_conv_len);
969                                            input_buf_len = input_buf_offset; // Discard invalid sequence
970                                    }
971    
972                                    // For debug
973    #ifdef _DEBUG
974                                    for (int j = input_conv_offset; j < input_conv_len; j++)
975                                    {
976                                            log_error("Debug input_conv: <--[%u]\n", (input_conv[j] + 256) % 256);
977                                    }
978    #endif
979                          }                          }
980    
981                          if (events[i].data.fd == sock || sock_write_wait) // EPOLLOUT                          while (input_conv_offset < input_conv_len && !SYS_server_exit)
982                          {                          {
983                                  sock_write_wait = 1;                                  if (bbsnet_conf[n].use_ssh)
                                 while (input_buf_offset < input_buf_len && !SYS_server_exit)  
984                                  {                                  {
985                                          ret = (int)write(sock, input_buf + input_buf_offset, (size_t)(input_buf_len - input_buf_offset));                                          ret = ssh_channel_write(channel, input_conv + input_conv_offset, (uint32_t)(input_conv_len - input_conv_offset));
986                                          if (ret < 0)                                          if (ret == SSH_ERROR)
987                                          {                                          {
988                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)                                                  log_error("ssh_channel_write() error: %s\n", ssh_get_error(session));
989                                                  {                                                  loop = 0;
990                                                          sock_write_wait = 0;                                                  break;
                                                         break;  
                                                 }  
                                                 else if (errno == EINTR)  
                                                 {  
                                                         continue;  
                                                 }  
                                                 else  
                                                 {  
                                                         log_error("write(socket) error (%d)\n", errno);  
                                                         loop = 0;  
                                                         break;  
                                                 }  
991                                          }                                          }
992                                          else if (ret == 0) // broken pipe                                  }
993                                    else
994                                    {
995                                            ret = (int)write(sock, input_conv + input_conv_offset, (size_t)(input_conv_len - input_conv_offset));
996                                    }
997                                    if (ret < 0)
998                                    {
999                                            if (errno == EAGAIN || errno == EWOULDBLOCK)
1000                                          {                                          {
                                                 log_common("write(socket) EOF\n");  
1001                                                  sock_write_wait = 0;                                                  sock_write_wait = 0;
                                                 loop = 0;  
1002                                                  break;                                                  break;
1003                                          }                                          }
1004                                          else                                          else if (errno == EINTR)
1005                                          {                                          {
                                                 input_buf_offset += ret;  
                                                 if (input_buf_offset >= input_buf_len) // Output buffer complete  
                                                 {  
                                                         input_buf_offset = 0;  
                                                         input_buf_len = 0;  
                                                         break;  
                                                 }  
1006                                                  continue;                                                  continue;
1007                                          }                                          }
1008                                            else
1009                                            {
1010                                                    log_error("write(socket) error (%d)\n", errno);
1011                                                    loop = 0;
1012                                                    break;
1013                                            }
1014                                    }
1015                                    else if (ret == 0) // broken pipe
1016                                    {
1017    #ifdef _DEBUG
1018                                            log_error("write(socket) EOF\n");
1019    #endif
1020                                            sock_write_wait = 0;
1021                                            loop = 0;
1022                                            break;
1023                                    }
1024                                    else
1025                                    {
1026                                            input_conv_offset += ret;
1027                                            if (input_conv_offset >= input_conv_len) // Output buffer complete
1028                                            {
1029                                                    input_conv_offset = 0;
1030                                                    input_conv_len = 0;
1031                                                    break;
1032                                            }
1033                                            continue;
1034                                  }                                  }
1035                          }                          }
1036                    }
1037    
1038                          if (events[i].data.fd == sock || sock_read_wait) // EPOLLIN                  if (sock_read_wait)
1039                    {
1040                            while (output_buf_len < sizeof(output_buf) && !SYS_server_exit)
1041                          {                          {
1042                                  sock_read_wait = 1;                                  if (bbsnet_conf[n].use_ssh)
                                 while (output_buf_len < sizeof(output_buf) && !SYS_server_exit)  
1043                                  {                                  {
1044                                          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,
1045                                          if (ret < 0)                                                                                                             (uint32_t)(sizeof(output_buf) - (size_t)output_buf_len), 0);
1046                                            if (ret == SSH_ERROR)
1047                                          {                                          {
1048                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)                                                  log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(session));
1049                                                  {                                                  loop = 0;
1050                                                          sock_read_wait = 0;                                                  break;
                                                         break;  
                                                 }  
                                                 else if (errno == EINTR)  
                                                 {  
                                                         continue;  
                                                 }  
                                                 else  
                                                 {  
                                                         log_error("read(socket) error (%d)\n", errno);  
                                                         loop = 0;  
                                                         break;  
                                                 }  
1051                                          }                                          }
1052                                          else if (ret == 0) // broken pipe                                          else if (ret == SSH_EOF)
1053                                          {                                          {
 #ifdef _DEBUG  
                                                 log_error("read(socket) EOF\n");  
 #endif  
1054                                                  sock_read_wait = 0;                                                  sock_read_wait = 0;
1055                                                  loop = 0;                                                  loop = 0;
1056                                                  break;                                                  break;
1057                                          }                                          }
1058                                          else                                          else if (ret == 0)
1059                                            {
1060                                                    sock_read_wait = 0;
1061                                                    break;
1062                                            }
1063                                    }
1064                                    else
1065                                    {
1066                                            ret = (int)read(sock, output_buf + output_buf_len, sizeof(output_buf) - (size_t)output_buf_len);
1067                                    }
1068                                    if (ret < 0)
1069                                    {
1070                                            if (errno == EAGAIN || errno == EWOULDBLOCK)
1071                                            {
1072                                                    sock_read_wait = 0;
1073                                                    break;
1074                                            }
1075                                            else if (errno == EINTR)
1076                                          {                                          {
                                                 output_buf_len += ret;  
1077                                                  continue;                                                  continue;
1078                                          }                                          }
1079                                            else
1080                                            {
1081                                                    log_error("read(socket) error (%d)\n", errno);
1082                                                    loop = 0;
1083                                                    break;
1084                                            }
1085                                    }
1086                                    else if (ret == 0) // broken pipe
1087                                    {
1088    #ifdef _DEBUG
1089                                            log_error("read(socket) EOF\n");
1090    #endif
1091                                            sock_read_wait = 0;
1092                                            loop = 0;
1093                                            break;
1094                                    }
1095                                    else
1096                                    {
1097                                            output_buf_len += ret;
1098                                            continue;
1099                                    }
1100                            }
1101                    }
1102    
1103                    if (stdout_write_wait)
1104                    {
1105                            if (output_buf_offset < output_buf_len)
1106                            {
1107                                    ret = io_buf_conv(output_cd, output_buf, &output_buf_len, &output_buf_offset, output_conv, sizeof(output_conv), &output_conv_len);
1108                                    if (ret < 0)
1109                                    {
1110                                            log_error("io_buf_conv(output, %d, %d, %d) error\n", output_buf_len, output_buf_offset, output_conv_len);
1111                                            output_buf_len = output_buf_offset; // Discard invalid sequence
1112                                  }                                  }
1113                          }                          }
1114    
1115                          if (events[i].data.fd == STDOUT_FILENO || stdout_write_wait)                          while (output_conv_offset < output_conv_len && !SYS_server_exit)
1116                          {                          {
1117                                  stdout_write_wait = 1;                                  if (SSH_v2)
                                 while (output_buf_offset < output_buf_len && !SYS_server_exit)  
1118                                  {                                  {
1119                                          if (SSH_v2)                                          ret = ssh_channel_write(SSH_channel, output_conv + output_conv_offset, (uint32_t)(output_conv_len - output_conv_offset));
1120                                            if (ret == SSH_ERROR)
1121                                          {                                          {
1122                                                  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));
1123                                                  if (ret == SSH_ERROR)                                                  loop = 0;
1124                                                  {                                                  break;
                                                         log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session));  
                                                         loop = 0;  
                                                         break;  
                                                 }  
1125                                          }                                          }
1126                                          else                                  }
1127                                    else
1128                                    {
1129                                            ret = (int)write(STDOUT_FILENO, output_conv + output_conv_offset, (size_t)(output_conv_len - output_conv_offset));
1130                                    }
1131                                    if (ret < 0)
1132                                    {
1133                                            if (errno == EAGAIN || errno == EWOULDBLOCK)
1134                                          {                                          {
1135                                                  ret = (int)write(STDOUT_FILENO, output_buf + output_buf_offset, (size_t)(output_buf_len - output_buf_offset));                                                  stdout_write_wait = 0;
1136                                                    break;
1137                                          }                                          }
1138                                          if (ret < 0)                                          else if (errno == EINTR)
1139                                          {                                          {
1140                                                  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;  
                                                 }  
1141                                          }                                          }
1142                                          else if (ret == 0) // broken pipe                                          else
1143                                          {                                          {
1144  #ifdef _DEBUG                                                  log_error("write(STDOUT) error (%d)\n", errno);
                                                 log_error("write(STDOUT) EOF\n");  
 #endif  
                                                 stdout_write_wait = 0;  
1145                                                  loop = 0;                                                  loop = 0;
1146                                                  break;                                                  break;
1147                                          }                                          }
1148                                          else                                  }
1149                                    else if (ret == 0) // broken pipe
1150                                    {
1151    #ifdef _DEBUG
1152                                            log_error("write(STDOUT) EOF\n");
1153    #endif
1154                                            stdout_write_wait = 0;
1155                                            loop = 0;
1156                                            break;
1157                                    }
1158                                    else
1159                                    {
1160                                            output_conv_offset += ret;
1161                                            if (output_conv_offset >= output_conv_len) // Output buffer complete
1162                                          {                                          {
1163                                                  output_buf_offset += ret;                                                  output_conv_offset = 0;
1164                                                  if (output_buf_offset >= output_buf_len) // Output buffer complete                                                  output_conv_len = 0;
1165                                                  {                                                  break;
                                                         output_buf_offset = 0;  
                                                         output_buf_len = 0;  
                                                         break;  
                                                 }  
                                                 continue;  
1166                                          }                                          }
1167                                            continue;
1168                                  }                                  }
1169                          }                          }
1170                  }                  }
1171          }          }
1172    
1173  cleanup:  cleanup:
1174          if (close(epollfd) < 0)          if (input_cd != (iconv_t)(-1))
1175            {
1176                    iconv_close(input_cd);
1177            }
1178            if (output_cd != (iconv_t)(-1))
1179            {
1180                    iconv_close(output_cd);
1181            }
1182    
1183    #ifdef HAVE_SYS_EPOLL_H
1184            if (epollfd != -1 && close(epollfd) < 0)
1185          {          {
1186                  log_error("close(epoll) error (%d)\n");                  log_error("close(epoll) error (%d)\n");
1187          }          }
1188    #endif
1189    
1190          // Restore STDIN/STDOUT flags          if (bbsnet_conf[n].use_ssh)
1191          fcntl(STDIN_FILENO, F_SETFL, flags_stdin);          {
1192          fcntl(STDOUT_FILENO, F_SETFL, flags_stdout);                  if (channel != NULL)
1193                    {
1194                            ssh_channel_free(channel);
1195                    }
1196                    if (session != NULL)
1197                    {
1198                            ssh_disconnect(session);
1199                            ssh_free(session);
1200                    }
1201            }
1202    
1203          // Restore socket flags          // Restore STDIN/STDOUT flags
1204          fcntl(sock, F_SETFL, flags_sock);          if (flags_stdin != -1 &&fcntl(STDIN_FILENO, F_SETFL, flags_stdin) == -1)
1205            {
1206                    log_error("fcntl(F_SETFL) error (%d)\n", errno);
1207            }
1208            if (flags_stdout != -1 && fcntl(STDOUT_FILENO, F_SETFL, flags_stdout) == -1)
1209            {
1210                    log_error("fcntl(F_SETFL) error (%d)\n", errno);
1211            }
1212    
1213          if (close(sock) == -1)          if (sock != -1 && close(sock) == -1)
1214          {          {
1215                  log_error("Close socket failed\n");                  log_error("Close socket failed\n");
1216          }          }
# Line 678  cleanup: Line 1222  cleanup:
1222                             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,
1223                             tm_used->tm_sec);                             tm_used->tm_sec);
1224    
1225            BBS_last_access_tm = time(NULL);
1226    
1227          return 0;          return 0;
1228  }  }
1229    
1230  static int  static int bbsnet_refresh()
 bbsnet_refresh()  
1231  {  {
1232          clearscr();          clearscr();
1233          moveto(1, 0);  
1234          prints(" ----------------------------------------------------------------------------- ");          moveto(1, 1);
1235            prints(" ------------------------------------------------------------------------------ ");
1236          for (int i = 2; i < 19; i++)          for (int i = 2; i < 19; i++)
1237          {          {
1238                  moveto(i, 0);                  moveto(i, 1);
1239                  prints("|");                  prints("|");
1240                  moveto(i, 79);                  moveto(i, 80);
1241                  prints("|");                  prints("|");
1242          }          }
1243          moveto(19, 0);          moveto(19, 1);
1244          prints("|-----------------------------------------------------------------------------|");          prints("|------------------------------------------------------------------------------|");
1245          moveto(22, 0);          moveto(22, 1);
1246          prints(" ----------------------------------------------------------------------------- ");          prints(" ------------------------------------------------------------------------------ ");
1247          moveto(23, 0);          moveto(23, 1);
1248          prints(" [\x1b[1;32mCtrl+C\x1b[m]˳");          prints(" [\033[1;32mCtrl+C\033[m]退出");
1249    
1250          iflush();          iflush();
1251    
1252          return 0;          return 0;
1253  }  }
1254    
1255  int bbsnet_selchange()  static int bbsnet_selchange()
1256  {  {
1257          int i = bbsnet_menu.menu_item_pos[0];          int i = bbsnet_menu.menu_item_pos[0];
1258    
1259          moveto(20, 0);          moveto(20, 1);
1260          clrtoeol();          clrtoeol();
1261          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%*s  类型: \033[1;33m%s\033[m",
1262                     bbsnet_conf[i].host2, bbsnet_conf[i].host1);                     bbsnet_conf[i].org_name, 20 - str_length(bbsnet_conf[i].org_name, 1), "",
1263          moveto(20, 79);                     bbsnet_conf[i].site_name, 20 - str_length(bbsnet_conf[i].site_name, 1), "",
1264                       (bbsnet_conf[i].use_ssh ? "SSH" : "Telnet"));
1265            moveto(20, 80);
1266          prints("|");          prints("|");
1267          moveto(21, 0);          moveto(21, 1);
1268          clrtoeol();          clrtoeol();
1269          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",
1270          if (bbsnet_conf[i].port != 23)                     bbsnet_conf[i].host_name, bbsnet_conf[i].port, bbsnet_conf[i].charset);
1271          {          moveto(21, 80);
                 prints("  %d", bbsnet_conf[i].port);  
         }  
         prints("\x1b[m");  
         moveto(21, 79);  
1272          prints("|");          prints("|");
1273          iflush();          iflush();
1274    
# Line 735  int bbs_net() Line 1279  int bbs_net()
1279  {  {
1280          int ch, i;          int ch, i;
1281    
1282          load_bbsnet_conf(CONF_BBSNET);          if (load_bbsnet_conf(CONF_BBSNET) < 0)
1283            {
1284          BBS_last_access_tm = time(NULL);                  clearscr();
1285                    moveto(1, 1);
1286                    prints("加载穿梭配置失败!");
1287                    press_any_key();
1288                    return -1;
1289            }
1290    
         clearscr();  
1291          bbsnet_refresh();          bbsnet_refresh();
1292          display_menu(&bbsnet_menu);          display_menu(&bbsnet_menu);
1293          bbsnet_selchange();          bbsnet_selchange();
# Line 748  int bbs_net() Line 1296  int bbs_net()
1296          {          {
1297                  ch = igetch(100);                  ch = igetch(100);
1298    
1299                  if (user_online_update("BBS_NET") < 0)                  if (ch != KEY_NULL && ch != KEY_TIMEOUT)
1300                  {                  {
1301                          log_error("user_online_update(BBS_NET) error\n");                          BBS_last_access_tm = time(NULL);
1302                  }                  }
1303    
1304                  switch (ch)                  switch (ch)
1305                  {                  {
1306                  case KEY_NULL: // broken pipe                  case KEY_NULL: // broken pipe
1307                  case KEY_ESC:                          log_error("KEY_NULL\n");
                 case Ctrl('C'): // user cancel  
1308                          goto cleanup;                          goto cleanup;
1309                  case KEY_TIMEOUT:                  case KEY_TIMEOUT:
1310                          if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time)
1311                          {                          {
1312                                    log_error("User input timeout\n");
1313                                  goto cleanup;                                  goto cleanup;
1314                          }                          }
1315                          continue;                          continue;
1316                    case KEY_ESC:
1317                    case Ctrl('C'): // user cancel
1318                            goto cleanup;
1319                  case CR:                  case CR:
                         igetch_reset();  
1320                          bbsnet_connect(bbsnet_menu.menu_item_pos[0]);                          bbsnet_connect(bbsnet_menu.menu_item_pos[0]);
1321                            // Force cleanup anything remaining in the output buffer
1322                            clearscr();
1323                            iflush();
1324                            // Clear screen and redraw menu
1325                          bbsnet_refresh();                          bbsnet_refresh();
1326                          display_menu(&bbsnet_menu);                          display_menu(&bbsnet_menu);
1327                          bbsnet_selchange();                          bbsnet_selchange();
# Line 809  int bbs_net() Line 1363  int bbs_net()
1363                          bbsnet_selchange();                          bbsnet_selchange();
1364                          break;                          break;
1365                  }                  }
                 BBS_last_access_tm = time(NULL);  
1366          }          }
1367    
1368  cleanup:  cleanup:


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

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