/[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.8 by sysadm, Tue Mar 22 08:59:31 2005 UTC Revision 1.93 by sysadm, Thu Dec 18 01:39:32 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /* SPDX-License-Identifier: GPL-3.0-or-later */
2                            bbs_net.c  -  description  /*
3                               -------------------   * bbs_net
4      begin                : Mon Oct 18 2004   *   - user interactive feature of site shuttle
5      copyright            : (C) 2004 by Leaflet   *
6      email                : leaflet@leafok.com   * Copyright (C) 2004-2025  Leaflet <leaflet@leafok.com>
7   ***************************************************************************/   */
8    
9  /***************************************************************************  #ifdef HAVE_CONFIG_H
10   *                                                                         *  #include "config.h"
11   *   This program is free software; you can redistribute it and/or modify  *  #endif
  *   it under the terms of the GNU General Public License as published by  *  
  *   the Free Software Foundation; either version 2 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"
16  #include "io.h"  #include "io.h"
17    #include "log.h"
18    #include "login.h"
19  #include "menu.h"  #include "menu.h"
20  #include "tcplib.h"  #include "screen.h"
21  #include <stdio.h>  #include "str_process.h"
22    #include <errno.h>
23    #include <fcntl.h>
24    #include <netdb.h>
25  #include <stdarg.h>  #include <stdarg.h>
26  #include <sys/ioctl.h>  #include <stdio.h>
27  #include <sys/socket.h>  #include <stdlib.h>
28  #include <netinet/in.h>  #include <string.h>
 #include <arpa/inet.h>  
29  #include <time.h>  #include <time.h>
30  #include <unistd.h>  #include <unistd.h>
31  #include <netdb.h>  #include <arpa/inet.h>
32    #include <ctype.h>
33    #include <libssh/libssh.h>
34    #include <libssh/server.h>
35    #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 TIME_OUT                15  #ifdef HAVE_SYS_EPOLL_H
43  #define MAX_PROCESS_BAR_LEN     30  #include <sys/epoll.h>
44  #define MAXSTATION              26*2  #else
45  #define STATION_PER_LINE        4  #include <poll.h>
46    #endif
47    
48    static const char MENU_CONF_DELIM[] = " \t\r\n";
49    
50    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            SSH_CONNECT_TIMEOUT = 5, // seconds
58    };
59    
60  struct _bbsnet_conf  struct _bbsnet_conf
61  {  {
62    char host1[20];          char org_name[40];
63    char host2[40];          char site_name[40];
64    char ip[40];          char host_name[IP_ADDR_LEN];
65    int port;          char port[6];
66            int8_t use_ssh;
67            char charset[CHARSET_MAX_LEN + 1];
68  } bbsnet_conf[MAXSTATION];  } bbsnet_conf[MAXSTATION];
69    
70  MENU_SET bbsnet_menu;  static MENU_SET bbsnet_menu;
71    
72    static void unload_bbsnet_conf(void);
73    
74  int  static int load_bbsnet_conf(const char *file_config)
 load_bbsnet_conf (const char *file_config)  
75  {  {
76    FILE *fp;          FILE *fp;
77    MENU *p_menu;          MENU *p_menu;
78    MENU_ITEM *p_menuitem;          MENU_ITEM *p_menu_item;
79    char t[256], *t1, *t2, *t3, *t4;          MENU_ITEM_ID menu_item_id;
80    int item_count = 0;          char line[LINE_BUFFER_LEN], *t1, *t2, *t3, *t4, *t5, *t6, *saveptr;
81            int port;
   fp = fopen (file_config, "r");  
   if (fp == NULL)  
     return -1;  
   
   p_menu = bbsnet_menu.p_menu[0] = malloc (sizeof (MENU));  
   strcpy (p_menu->name, "BBSNET");  
   p_menu->title.show = 0;  
   p_menu->screen.show = 0;  
   
   while (fgets (t, 255, fp) && item_count < MAXSTATION)  
     {  
       t1 = strtok (t, " \t");  
       t2 = strtok (NULL, " \t\n");  
       t3 = strtok (NULL, " \t\n");  
       t4 = strtok (NULL, " \t\n");  
   
       if (t1[0] == '#' || t1[0] == '*' || t1 == NULL || t2 == NULL  
           || t3 == NULL)  
         continue;  
       strncpy (bbsnet_conf[item_count].host1, t2, 18);  
       bbsnet_conf[item_count].host1[18] = 0;  
       strncpy (bbsnet_conf[item_count].host2, t1, 36);  
       bbsnet_conf[item_count].host2[36] = 0;  
       strncpy (bbsnet_conf[item_count].ip, t3, 36);  
       bbsnet_conf[item_count].ip[36] = 0;  
       bbsnet_conf[item_count].port = t4 ? atoi (t4) : 23;  
   
       p_menuitem = p_menu->items[item_count] = malloc (sizeof (MENU_ITEM));  
       p_menuitem->row = 2 + item_count / STATION_PER_LINE;  
       p_menuitem->col = 5 + item_count % STATION_PER_LINE * 20;  
       sprintf (p_menuitem->action, "%d", item_count);  
       p_menuitem->submenu = 0;  
       p_menuitem->priv = 0;  
       p_menuitem->level = 0;  
       p_menuitem->display = 0;  
       p_menuitem->name[0] =  
         (item_count < MAXSTATION / 2 ? 'A' + item_count : 'a' + item_count);  
       p_menuitem->name[1] = '\0';  
       sprintf (p_menuitem->text, "%c. %s",  
                p_menuitem->name[0], bbsnet_conf[item_count].host1);  
   
       item_count++;  
     }  
   fclose (fp);  
   
   p_menu->item_count = item_count;  
   p_menu->item_cur_pos = 0;  
   
   bbsnet_menu.menu_count = 1;  
   bbsnet_menu.menu_select_depth = 0;  
   bbsnet_menu.p_menu_select[0] = bbsnet_menu.p_menu[0];  
82    
83    return 0;          unload_bbsnet_conf();
84    
85            bbsnet_menu.p_menu_pool = calloc(1, sizeof(MENU));
86            if (bbsnet_menu.p_menu_pool == NULL)
87            {
88                    log_error("calloc(p_menu_pool) error\n");
89                    return -1;
90            }
91            bbsnet_menu.menu_count = 1;
92    
93            bbsnet_menu.p_menu_item_pool = calloc(MAXSTATION, sizeof(MENU_ITEM));
94            if (bbsnet_menu.p_menu_item_pool == NULL)
95            {
96                    log_error("calloc(p_menu_item_pool) error\n");
97                    unload_bbsnet_conf();
98                    return -1;
99            }
100            bbsnet_menu.menu_item_count = MAXSTATION;
101    
102            p_menu = (MENU *)get_menu_by_id(&bbsnet_menu, 0);
103    
104            strncpy(p_menu->name, "BBSNET", sizeof(p_menu->name) - 1);
105            p_menu->name[sizeof(p_menu->name) - 1] = '\0';
106            p_menu->title.show = 0;
107            p_menu->screen_show = 0;
108    
109            fp = fopen(file_config, "r");
110            if (fp == NULL)
111            {
112                    unload_bbsnet_conf();
113                    return -2;
114            }
115    
116            menu_item_id = 0;
117            while (fgets(line, sizeof(line), fp) && menu_item_id < MAXSTATION)
118            {
119                    t1 = strtok_r(line, MENU_CONF_DELIM, &saveptr);
120                    t2 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
121                    t3 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
122                    t4 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
123                    t5 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
124                    t6 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
125    
126                    if (t1 == NULL || t2 == NULL || t3 == NULL || t4 == NULL ||
127                            t5 == NULL || t6 == NULL || line[0] == '#' || line[0] == '*')
128                    {
129                            continue;
130                    }
131    
132                    strncpy(bbsnet_conf[menu_item_id].site_name, t2, sizeof(bbsnet_conf[menu_item_id].site_name) - 1);
133                    bbsnet_conf[menu_item_id].site_name[sizeof(bbsnet_conf[menu_item_id].site_name) - 1] = '\0';
134                    strncpy(bbsnet_conf[menu_item_id].org_name, t1, sizeof(bbsnet_conf[menu_item_id].org_name) - 1);
135                    bbsnet_conf[menu_item_id].org_name[sizeof(bbsnet_conf[menu_item_id].org_name) - 1] = '\0';
136                    strncpy(bbsnet_conf[menu_item_id].host_name, t3, sizeof(bbsnet_conf[menu_item_id].host_name) - 1);
137                    bbsnet_conf[menu_item_id].host_name[sizeof(bbsnet_conf[menu_item_id].host_name) - 1] = '\0';
138                    port = atoi(t4);
139                    if (port <= 0 || port > 65535)
140                    {
141                            log_error("Invalid port value %d of menu item %d\n", port, menu_item_id);
142                            fclose(fp);
143                            unload_bbsnet_conf();
144                            return -3;
145                    }
146                    strncpy(bbsnet_conf[menu_item_id].port, t4, sizeof(bbsnet_conf[menu_item_id].port) - 1);
147                    bbsnet_conf[menu_item_id].port[sizeof(bbsnet_conf[menu_item_id].port) - 1] = '\0';
148                    bbsnet_conf[menu_item_id].use_ssh = (toupper(t5[0]) == 'Y');
149                    strncpy(bbsnet_conf[menu_item_id].charset, t6, sizeof(bbsnet_conf[menu_item_id].charset) - 1);
150                    bbsnet_conf[menu_item_id].charset[sizeof(bbsnet_conf[menu_item_id].charset) - 1] = '\0';
151    
152                    p_menu_item = get_menu_item_by_id(&bbsnet_menu, menu_item_id);
153                    if (p_menu_item == NULL)
154                    {
155                            log_error("get_menu_item_by_id(%d) return NULL pointer\n", menu_item_id);
156                            fclose(fp);
157                            unload_bbsnet_conf();
158                            return -3;
159                    }
160    
161                    p_menu_item->row = (int16_t)(2 + menu_item_id / STATION_PER_LINE);
162                    p_menu_item->col = (int16_t)(5 + menu_item_id % STATION_PER_LINE * 20);
163                    snprintf(p_menu_item->action, sizeof(p_menu_item->action), "%d", (int16_t)menu_item_id);
164                    p_menu_item->submenu = 0;
165                    p_menu_item->priv = 0;
166                    p_menu_item->level = 0;
167                    p_menu_item->name[0] =
168                            (char)(menu_item_id < MAXSTATION / 2 ? 'A' + menu_item_id : 'a' + menu_item_id);
169                    p_menu_item->name[1] = '\0';
170                    snprintf(p_menu_item->text, sizeof(p_menu_item->text), "\033[1;36m%c.\033[m %s",
171                                     p_menu_item->name[0], bbsnet_conf[menu_item_id].site_name);
172    
173                    p_menu->items[p_menu->item_count] = menu_item_id;
174                    p_menu->item_count++;
175                    menu_item_id++;
176            }
177    
178            bbsnet_menu.menu_item_count = (int16_t)menu_item_id;
179            bbsnet_menu.menu_id_path[0] = 0;
180            bbsnet_menu.menu_item_pos[0] = 0;
181            bbsnet_menu.choose_step = 0;
182    
183            fclose(fp);
184    
185            return 0;
186    }
187    
188    static void unload_bbsnet_conf(void)
189    {
190            bbsnet_menu.menu_count = 0;
191            bbsnet_menu.menu_item_count = 0;
192    
193            if (bbsnet_menu.p_menu_pool)
194            {
195                    free(bbsnet_menu.p_menu_pool);
196                    bbsnet_menu.p_menu_pool = NULL;
197            }
198    
199            if (bbsnet_menu.p_menu_item_pool)
200            {
201                    free(bbsnet_menu.p_menu_item_pool);
202                    bbsnet_menu.p_menu_item_pool = NULL;
203            }
204  }  }
205    
206  static void  static void process_bar(int n, int len)
 process_bar (int n, int len)  
207  {  {
208    char buf[256];          char buf[LINE_BUFFER_LEN];
209    char buf2[256];          char buf2[LINE_BUFFER_LEN];
210    char *ptr;  
211    char *ptr2;          if (len <= 0)
212    char *ptr3;          {
213                    len = 1;
214    moveto (4, 0);          }
215    prints ("\n");          else if (len > LINE_BUFFER_LEN)
216    sprintf (buf2, "            %3d%%              ", n * 100 / len);          {
217    ptr = buf;                  len = LINE_BUFFER_LEN - 1;
218    ptr2 = buf2;          }
219    ptr3 = buf + n;          if (n < 0)
220    while (ptr != ptr3)          {
221      *ptr++ = *ptr2++;                  n = 0;
222    *ptr++ = '\x1b';          }
223    *ptr++ = '[';          else if (n > len)
224    *ptr++ = '4';          {
225    *ptr++ = '4';                  n = len;
226    *ptr++ = 'm';          }
227    while (*ptr2 != '\0')  
228      *ptr++ = *ptr2++;          moveto(4, 1);
229    *ptr++ = '\0';          prints(" ------------------------------ \r\n");
230    prints ("\033[46m%s\033[m\n", buf);          snprintf(buf, sizeof(buf), "            %3d%%              ", n * 100 / len);
231    prints ("\n");          memcpy(buf2, buf, (size_t)n);
232            buf2[n] = '\0';
233            prints("|\033[46m%s\033[44m%s\033[m|\r\n", buf2, buf + n);
234            prints(" ------------------------------ \r\n");
235            iflush();
236  }  }
237    
238  int  static int bbsnet_connect(int n)
 bbsnet_connect (int n)  
239  {  {
240    int sock, ch, result, len, loop;          int sock = -1;
241    struct sockaddr_in sin;          int ret;
242    char buf[256];          int loop;
243    fd_set inputs, testfds;          int error;
244    struct timeval timeout;          int sock_connected = 0;
245    struct hostent *pHost = NULL;          int flags_sock = -1;
246    int rc, rv, tos = 020, i;          int flags_stdin = -1;
247    char remote_addr[256];          int flags_stdout = -1;
248    int remote_port;          struct sockaddr_in sin;
249    time_t t_used;          char input_buf[LINE_BUFFER_LEN];
250    struct tm * tm_used;          char output_buf[LINE_BUFFER_LEN];
251            int input_buf_len = 0;
252    prints ("\033[1;32mڲ %s (%s) ӣԺ... \033[m\n",          int output_buf_len = 0;
253            bbsnet_conf[n].host1, bbsnet_conf[n].ip);          int input_buf_offset = 0;
254    prints ("\033[1;32m %d ޷ϣ򽫷ӡ\033[m\n",          int output_buf_offset = 0;
255            TIME_OUT);          char input_conv[LINE_BUFFER_LEN * 2];
256            char output_conv[LINE_BUFFER_LEN * 2];
257    pHost = gethostbyname (bbsnet_conf[n].ip);          int input_conv_len = 0;
258            int output_conv_len = 0;
259    if (pHost == NULL)          int input_conv_offset = 0;
260      {          int output_conv_offset = 0;
261        prints ("\033[1;31mʧܣ\033[m\n");          iconv_t input_cd = (iconv_t)(-1);
262        press_any_key ();          iconv_t output_cd = (iconv_t)(-1);
263        return -1;          char tocode[CHARSET_MAX_LEN + 20];
264      }  
265    #ifdef HAVE_SYS_EPOLL_H
266    sock = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);          struct epoll_event ev, events[MAX_EVENTS];
267            int epollfd = -1;
268    if (sock < 0)  #else
269      {          struct pollfd pfds[3];
270        prints ("\033[1;31m޷socket\033[m\n");  #endif
271        press_any_key ();  
272        return -1;          int nfds;
273      }          int stdin_read_wait = 0;
274            int stdout_write_wait = 0;
275    sin.sin_family = AF_INET;          int sock_read_wait = 0;
276    sin.sin_addr.s_addr =          int sock_write_wait = 0;
277      (strlen (hostaddr_server) > 0 ? inet_addr (hostaddr_server) : INADDR_ANY);          struct addrinfo hints, *res = NULL;
278    sin.sin_port = 0;          int tos;
279            char remote_addr[INET_ADDRSTRLEN];
280    if (bind (sock, (struct sockaddr *) &sin, sizeof (sin)) < 0)          int remote_port;
281      {          char local_addr[INET_ADDRSTRLEN];
282        log_error ("Bind address %s:%u failed\n",          int local_port;
283                   inet_ntoa (sin.sin_addr), ntohs (sin.sin_port));          socklen_t sock_len;
284        return -2;          time_t t_begin;
285      }          time_t t_used = time(NULL);
286              struct tm *tm_used;
287    bzero (&sin, sizeof (sin));          int ch;
288    sin.sin_family = AF_INET;          char remote_user[USERNAME_MAX_LEN + 1];
289    sin.sin_addr = *(struct in_addr *) pHost->h_addr_list[0];          char remote_pass[PASSWORD_MAX_LEN + 1];
290    sin.sin_port = htons (bbsnet_conf[n].port);          ssh_session session = NULL;
291            ssh_channel channel = NULL;
292    strcpy (remote_addr, inet_ntoa (sin.sin_addr));          int ssh_process_config = 0;
293    remote_port = ntohs (sin.sin_port);          int ssh_log_level = SSH_LOG_NOLOG;
294    
295    prints ("\033[1;32mʾǰʹõʱ䡣\033[m\n");          if (user_online_update("BBS_NET") < 0)
296    process_bar (0, MAX_PROCESS_BAR_LEN);          {
297    for (i = 0; i < MAX_PROCESS_BAR_LEN; i++)                  log_error("user_online_update(BBS_NET) error\n");
298      {          }
299        if (i == 0)  
300          rv =          if (bbsnet_conf[n].use_ssh)
301            NonBlockConnectEx (sock, (struct sockaddr *) &sin, sizeof (sin),          {
302                               500, 1);                  clearscr();
303        else  
304          rv =                  if (!SSH_v2)
305            NonBlockConnectEx (sock, (struct sockaddr *) &sin, sizeof (sin),                  {
306                               500, 0);                          moveto(1, 1);
307        if (rv == ERR_TCPLIB_TIMEOUT)                          prints("只有在以SSH方式登陆本站时,才能使用SSH站点穿梭。");
308          {                          press_any_key();
309            process_bar (i + 1, MAX_PROCESS_BAR_LEN);                          return 0;
310            continue;                  }
311          }  
312        else if (rv == 0)                  moveto(1, 1);
313          break;                  prints("通过SSH方式连接[%s]...", bbsnet_conf[n].site_name);
314        else                  moveto(2, 1);
315          {                  prints("请输入用户名: ");
316            prints ("\033[1;31mʧܣ\033[m\n");                  iflush();
317            press_any_key ();                  if (str_input(remote_user, sizeof(remote_user), DOECHO) < 0)
318            return -1;                  {
319          }                          return -1;
320      }                  }
321    if (i == MAX_PROCESS_BAR_LEN)                  if (remote_user[0] == '\0')
322      {                  {
323        prints ("\033[1;31mӳʱ\033[m\n");                          return 0;
324        press_any_key ();                  }
325        return -1;  
326      }                  moveto(3, 1);
327    setsockopt (sock, IPPROTO_IP, IP_TOS, &tos, sizeof (int));                  prints("请输入密码: ");
328                    iflush();
329    prints ("\033[1;31mӳɹ\033[m\n");                  if (str_input(remote_pass, sizeof(remote_pass), NOECHO) < 0)
330    log_std ("BBSNET connect to %s:%d\n",                  {
331      remote_addr, remote_port);                          return -1;
332                    }
333    FD_ZERO (&inputs);                  if (remote_pass[0] == '\0')
334    FD_SET (0, &inputs);                  {
335    FD_SET (sock, &inputs);                          return 0;
336                    }
337    BBS_last_access_tm = t_used = time (0);          }
338    
339    loop = 1;          clearscr();
340    
341    while (loop)          moveto(1, 1);
342      {          prints("\033[1;32m正在测试往 %s (%s) 的连接,请稍候... \033[m\r\n",
343        testfds = inputs;                     bbsnet_conf[n].site_name, bbsnet_conf[n].host_name);
344        timeout.tv_sec = TIME_OUT;          iflush();
345        timeout.tv_usec = 0;  
346            memset(&hints, 0, sizeof(hints));
347        result = select (FD_SETSIZE, &testfds, (fd_set *) NULL,          hints.ai_family = AF_INET;
348                         (fd_set *) NULL, &timeout);          hints.ai_socktype = SOCK_STREAM;
349            hints.ai_protocol = IPPROTO_TCP;
350        switch (result)  
351          {          if ((ret = getaddrinfo(BBS_address, NULL, &hints, &res)) != 0)
352          case 0:          {
353            if (time (0) - BBS_last_access_tm >= MAX_DELAY_TIME)                  log_error("getaddrinfo() error (%d)\n", ret);
354              {                  goto cleanup;
355                loop = 0;          }
356              }  
357            break;          if (inet_ntop(AF_INET, &(((struct sockaddr_in *)res->ai_addr)->sin_addr.s_addr), local_addr, sizeof(local_addr)) == NULL)
358          case -1:          {
359            log_error ("select() error!\n");                  log_error("inet_ntop() error (%d)\n", errno);
360            break;                  goto cleanup;
361          default:          }
362            if (FD_ISSET (0, &testfds))          local_port = ntohs(((struct sockaddr_in *)res->ai_addr)->sin_port);
363              {  
364                len = read (0, buf, 255);          sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
365                if (len == 0)          if (sock < 0)
366                  {          {
367                    loop = 0;                  log_error("socket() error (%d)\n", errno);
368                    break;                  goto cleanup;
369                  }          }
370                write (sock, buf, len);  
371              }          if (bind(sock, res->ai_addr, res->ai_addrlen) < 0)
372            if (FD_ISSET (sock, &testfds))          {
373              {                  log_error("bind(%s:%u) error (%d)\n", local_addr, local_port, errno);
374                len = read (sock, buf, 255);                  goto cleanup;
375                if (len == 0)          }
376                  {  
377                    loop = 0;          freeaddrinfo(res);
378                    break;          res = NULL;
379                  }  
380                write (1, buf, len);          memset(&hints, 0, sizeof(hints));
381              }          hints.ai_family = AF_INET;
382            break;          hints.ai_flags = AI_NUMERICSERV;
383            BBS_last_access_tm = time (0);          hints.ai_socktype = SOCK_STREAM;
384          }          hints.ai_protocol = IPPROTO_TCP;
385      }  
386            if ((ret = getaddrinfo(bbsnet_conf[n].host_name, bbsnet_conf[n].port, &hints, &res)) != 0)
387    if (close (sock) == -1)          {
388      {                  log_error("getaddrinfo() error (%d)\n", ret);
389        log_error ("Close socket failed\n");                  prints("\033[1;31m查找主机名失败!\033[m\r\n");
390      }                  press_any_key();
391                    goto cleanup;
392            }
393    
394            if (inet_ntop(AF_INET, &(((struct sockaddr_in *)res->ai_addr)->sin_addr.s_addr), remote_addr, sizeof(remote_addr)) == NULL)
395            {
396                    log_error("inet_ntop() error (%d)\n", errno);
397                    goto cleanup;
398            }
399            remote_port = ntohs(((struct sockaddr_in *)res->ai_addr)->sin_port);
400    
401            prints("\033[1;32m穿梭进度条提示您当前已使用的时间,按\033[1;33mCtrl+C\033[1;32m中断。\033[m\r\n");
402            process_bar(0, MAX_PROCESS_BAR_LEN);
403    
404            // Set socket as non-blocking
405            if ((flags_sock = fcntl(sock, F_GETFL, 0)) == -1)
406            {
407                    log_error("fcntl(F_GETFL) error (%d)\n", errno);
408                    goto cleanup;
409            }
410            if ((fcntl(sock, F_SETFL, flags_sock | O_NONBLOCK)) == -1)
411            {
412                    log_error("fcntl(F_SETFL) error (%d)\n", errno);
413                    goto cleanup;
414            }
415    
416            // Set STDIN/STDOUT as non-blocking
417            if ((flags_stdin = fcntl(STDIN_FILENO, F_GETFL, 0)) == -1)
418            {
419                    log_error("fcntl(F_GETFL) error (%d)\n", errno);
420                    goto cleanup;
421            }
422            if ((flags_stdout = fcntl(STDOUT_FILENO, F_GETFL, 0)) == -1)
423            {
424                    log_error("fcntl(F_GETFL) error (%d)\n", errno);
425                    goto cleanup;
426            }
427            if ((fcntl(STDIN_FILENO, F_SETFL, flags_stdin | O_NONBLOCK)) == -1)
428            {
429                    log_error("fcntl(F_SETFL) error (%d)\n", errno);
430                    goto cleanup;
431            }
432            if ((fcntl(STDOUT_FILENO, F_SETFL, flags_stdout | O_NONBLOCK)) == -1)
433            {
434                    log_error("fcntl(F_SETFL) error (%d)\n", errno);
435                    goto cleanup;
436            }
437    
438    #ifdef HAVE_SYS_EPOLL_H
439            epollfd = epoll_create1(0);
440            if (epollfd < 0)
441            {
442                    log_error("epoll_create1() error (%d)\n", errno);
443                    goto cleanup;
444            }
445    
446            ev.events = EPOLLOUT | EPOLLET;
447            ev.data.fd = sock;
448            if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sock, &ev) == -1)
449            {
450                    log_error("epoll_ctl(socket) error (%d)\n", errno);
451                    goto cleanup;
452            }
453    
454            ev.events = EPOLLIN | EPOLLET;
455            ev.data.fd = STDIN_FILENO;
456            if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1)
457            {
458                    log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno);
459                    goto cleanup;
460            }
461    #endif
462    
463            while (!SYS_server_exit)
464            {
465                    if ((ret = connect(sock, res->ai_addr, res->ai_addrlen)) < 0)
466                    {
467                            if (errno == EAGAIN || errno == EALREADY || errno == EINPROGRESS)
468                            {
469                                    // Use select / epoll to check writability of the socket,
470                                    // then use getsockopt to check the status of the socket.
471                                    // See man connect(2)
472                                    break;
473                            }
474                            else if (errno == EINTR)
475                            {
476                                    continue;
477                            }
478                            else
479                            {
480                                    log_error("connect(socket) error (%d)\n", errno);
481                                    prints("\033[1;31m连接失败!\033[m\r\n");
482                                    press_any_key();
483                                    goto cleanup;
484                            }
485                    }
486            }
487    
488            for (int j = 0; j < MAX_PROCESS_BAR_LEN && !sock_connected && !SYS_server_exit; j++)
489            {
490    #ifdef HAVE_SYS_EPOLL_H
491                    nfds = epoll_wait(epollfd, events, MAX_EVENTS, 500); // 0.5 second
492                    ret = nfds;
493    #else
494                    pfds[0].fd = sock;
495                    pfds[0].events = POLLOUT;
496                    pfds[1].fd = STDIN_FILENO;
497                    pfds[1].events = POLLIN;
498                    nfds = 2;
499                    ret = poll(pfds, (nfds_t)nfds, 500); // 0.5 second
500    #endif
501    
502                    if (ret < 0)
503                    {
504                            if (errno != EINTR)
505                            {
506    #ifdef HAVE_SYS_EPOLL_H
507                                    log_error("epoll_wait() error (%d)\n", errno);
508    #else
509                                    log_error("poll() error (%d)\n", errno);
510    #endif
511                                    break;
512                            }
513                    }
514                    else if (ret == 0) // timeout
515                    {
516                            process_bar(j + 1, MAX_PROCESS_BAR_LEN);
517                    }
518                    else // ret > 0
519                    {
520                            for (int i = 0; i < nfds; i++)
521                            {
522    #ifdef HAVE_SYS_EPOLL_H
523                                    if (events[i].data.fd == sock)
524    #else
525                                    if (pfds[i].fd == sock && (pfds[i].revents & POLLOUT))
526    #endif
527                                    {
528                                            socklen_t len = sizeof(error);
529                                            if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
530                                            {
531                                                    log_error("getsockopt() error (%d) !\n", errno);
532                                                    goto cleanup;
533                                            }
534                                            if (error == 0)
535                                            {
536                                                    sock_connected = 1;
537                                            }
538                                    }
539    #ifdef HAVE_SYS_EPOLL_H
540                                    else if (events[i].data.fd == STDIN_FILENO)
541    #else
542                                    else if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & POLLIN))
543    #endif
544                                    {
545                                            do
546                                            {
547                                                    ch = igetch(0);
548                                            } while (ch == 0);
549                                            if (ch == Ctrl('C') || ch == KEY_ESC)
550                                            {
551                                                    goto cleanup;
552                                            }
553                                    }
554                            }
555                    }
556            }
557            if (SYS_server_exit)
558            {
559                    goto cleanup;
560            }
561            if (!sock_connected)
562            {
563                    prints("\033[1;31m连接失败!\033[m\r\n");
564                    press_any_key();
565                    goto cleanup;
566            }
567    
568            tos = IPTOS_LOWDELAY;
569            if (setsockopt(sock, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)
570            {
571                    log_error("setsockopt IP_TOS=%d error (%d)\n", tos, errno);
572            }
573    
574            sock_len = sizeof(sin);
575            if (getsockname(sock, (struct sockaddr *)&sin, &sock_len) < 0)
576            {
577                    log_error("getsockname() error: %d", errno);
578                    goto cleanup;
579            }
580    
581            strncpy(local_addr, inet_ntoa(sin.sin_addr), sizeof(local_addr) - 1);
582            local_addr[sizeof(local_addr) - 1] = '\0';
583            local_port = ntohs(sin.sin_port);
584    
585            if (bbsnet_conf[n].use_ssh)
586            {
587                    session = ssh_new();
588                    if (session == NULL)
589                    {
590                            log_error("ssh_new() error\n");
591                            goto cleanup;
592                    }
593    
594                    if (ssh_options_set(session, SSH_OPTIONS_FD, &sock) < 0 ||
595                            ssh_options_set(session, SSH_OPTIONS_PROCESS_CONFIG, &ssh_process_config) < 0 ||
596                            ssh_options_set(session, SSH_OPTIONS_KNOWNHOSTS, SSH_KNOWN_HOSTS_FILE) < 0 ||
597                            ssh_options_set(session, SSH_OPTIONS_HOST, bbsnet_conf[n].host_name) < 0 ||
598                            ssh_options_set(session, SSH_OPTIONS_USER, remote_user) < 0 ||
599                            ssh_options_set(session, SSH_OPTIONS_HOSTKEYS, "+ssh-rsa") < 0 ||
600                            ssh_options_set(session, SSH_OPTIONS_LOG_VERBOSITY, &ssh_log_level) < 0)
601                    {
602                            log_error("Error setting SSH options: %s\n", ssh_get_error(session));
603                            goto cleanup;
604                    }
605    
606                    ssh_set_blocking(session, 0);
607    
608                    t_begin = time(NULL);
609                    ret = SSH_ERROR;
610                    while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT)
611                    {
612                            ret = ssh_connect(session);
613                            if (ret == SSH_OK)
614                            {
615                                    break;
616                            }
617                            else if (ret == SSH_AGAIN)
618                            {
619                                    // log_error("ssh_connect() error: SSH_AGAIN\n");
620                            }
621                            else // if (ret == SSH_ERROR)
622                            {
623                                    log_error("ssh_connect() error: SSH_ERROR\n");
624                                    goto cleanup;
625                            }
626                    }
627                    if (ret != SSH_OK)
628                    {
629                            prints("\033[1;31m连接超时!\033[m\r\n");
630                            press_any_key();
631                            goto cleanup;
632                    }
633    
634                    ret = ssh_session_is_known_server(session);
635                    switch (ret)
636                    {
637                    case SSH_KNOWN_HOSTS_NOT_FOUND:
638                    case SSH_KNOWN_HOSTS_UNKNOWN:
639                            if (ssh_session_update_known_hosts(session) != SSH_OK)
640                            {
641                                    log_error("ssh_session_update_known_hosts(%s) error\n", bbsnet_conf[n].host_name);
642                                    prints("\033[1;31m无法添加服务器证书\033[m\r\n");
643                                    press_any_key();
644                                    goto cleanup;
645                            }
646                            log_common("SSH key of (%s) is added into %s\n", bbsnet_conf[n].host_name, SSH_KNOWN_HOSTS_FILE);
647                    case SSH_KNOWN_HOSTS_OK:
648                            break;
649                    case SSH_KNOWN_HOSTS_CHANGED:
650                    case SSH_KNOWN_HOSTS_OTHER:
651                            log_error("ssh_session_is_known_server(%s) error: %d\n", bbsnet_conf[n].host_name, ret);
652                            prints("\033[1;31m服务器证书已变更\033[m\r\n");
653                            press_any_key();
654                            goto cleanup;
655                    }
656    
657                    ret = SSH_AUTH_ERROR;
658                    while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT)
659                    {
660                            ret = ssh_userauth_password(session, NULL, remote_pass);
661                            if (ret == SSH_AUTH_SUCCESS)
662                            {
663                                    break;
664                            }
665                            else if (ret == SSH_AUTH_AGAIN)
666                            {
667                                    // log_error("ssh_userauth_password() error: SSH_AUTH_AGAIN\n");
668                            }
669                            else if (ret == SSH_AUTH_ERROR)
670                            {
671                                    log_error("ssh_userauth_password() error: SSH_AUTH_ERROR\n");
672                                    goto cleanup;
673                            }
674                            else // if (ret == SSH_AUTH_DENIED)
675                            {
676                                    log_error("ssh_userauth_password() error: SSH_AUTH_DENIED\n");
677                                    prints("\033[1;31m身份验证失败!\033[m\r\n");
678                                    press_any_key();
679                                    goto cleanup;
680                            }
681                    }
682                    if (ret != SSH_AUTH_SUCCESS)
683                    {
684                            prints("\033[1;31m连接超时!\033[m\r\n");
685                            press_any_key();
686                            goto cleanup;
687                    }
688    
689                    channel = ssh_channel_new(session);
690                    if (channel == NULL)
691                    {
692                            log_error("ssh_channel_new() error\n");
693                            goto cleanup;
694                    }
695    
696                    ret = SSH_ERROR;
697                    while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT)
698                    {
699                            ret = ssh_channel_open_session(channel);
700                            if (ret == SSH_OK)
701                            {
702                                    break;
703                            }
704                            else if (ret == SSH_AGAIN)
705                            {
706                                    // log_error("ssh_channel_open_session() error: SSH_AGAIN\n");
707                            }
708                            else // if (ret == SSH_ERROR)
709                            {
710                                    log_error("ssh_channel_open_session() error: SSH_ERROR\n");
711                                    goto cleanup;
712                            }
713                    }
714                    if (ret != SSH_OK)
715                    {
716                            prints("\033[1;31m连接超时!\033[m\r\n");
717                            press_any_key();
718                            goto cleanup;
719                    }
720    
721                    ret = SSH_ERROR;
722                    while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT)
723                    {
724                            ret = ssh_channel_request_pty(channel);
725                            if (ret == SSH_OK)
726                            {
727                                    break;
728                            }
729                            else if (ret == SSH_AGAIN)
730                            {
731                                    // log_error("ssh_channel_request_pty() error: SSH_AGAIN\n");
732                            }
733                            else // if (ret == SSH_ERROR)
734                            {
735                                    log_error("ssh_channel_request_pty() error: SSH_ERROR\n");
736                                    goto cleanup;
737                            }
738                    }
739                    if (ret != SSH_OK)
740                    {
741                            prints("\033[1;31m连接超时!\033[m\r\n");
742                            press_any_key();
743                            goto cleanup;
744                    }
745    
746                    ret = SSH_ERROR;
747                    while (!SYS_server_exit && time(NULL) - t_begin < SSH_CONNECT_TIMEOUT)
748                    {
749                            ret = ssh_channel_request_shell(channel);
750                            if (ret == SSH_OK)
751                            {
752                                    break;
753                            }
754                            else if (ret == SSH_AGAIN)
755                            {
756                                    // log_error("ssh_channel_request_shell() error: SSH_AGAIN\n");
757                            }
758                            else // if (ret == SSH_ERROR)
759                            {
760                                    log_error("ssh_channel_request_shell() error: SSH_ERROR\n");
761                                    goto cleanup;
762                            }
763                    }
764                    if (ret != SSH_OK)
765                    {
766                            prints("\033[1;31m连接超时!\033[m\r\n");
767                            press_any_key();
768                            goto cleanup;
769                    }
770            }
771    
772            prints("\033[1;31m连接成功!\033[m\r\n");
773            iflush();
774            log_common("BBSNET connect to %s:%d from %s:%d by [%s]\n",
775                               remote_addr, remote_port, local_addr, local_port, BBS_username);
776    
777            snprintf(tocode, sizeof(tocode), "%s%s", bbsnet_conf[n].charset,
778                             (strcasecmp(stdio_charset, bbsnet_conf[n].charset) == 0 ? "" : "//IGNORE"));
779            input_cd = iconv_open(tocode, stdio_charset);
780            if (input_cd == (iconv_t)(-1))
781            {
782                    log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, tocode, errno);
783                    goto cleanup;
784            }
785    
786            snprintf(tocode, sizeof(tocode), "%s%s", stdio_charset,
787                             (strcasecmp(bbsnet_conf[n].charset, stdio_charset) == 0 ? "" : "//TRANSLIT"));
788            output_cd = iconv_open(tocode, bbsnet_conf[n].charset);
789            if (output_cd == (iconv_t)(-1))
790            {
791                    log_error("iconv_open(%s->%s) error: %d\n", bbsnet_conf[n].charset, tocode, errno);
792                    goto cleanup;
793            }
794    
795    #ifdef HAVE_SYS_EPOLL_H
796            ev.events = EPOLLIN | EPOLLOUT | EPOLLET;
797            ev.data.fd = sock;
798            if (epoll_ctl(epollfd, EPOLL_CTL_MOD, sock, &ev) == -1)
799            {
800                    log_error("epoll_ctl(socket) error (%d)\n", errno);
801                    goto cleanup;
802            }
803    
804            ev.events = EPOLLOUT | EPOLLET;
805            ev.data.fd = STDOUT_FILENO;
806            if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1)
807            {
808                    log_error("epoll_ctl(STDOUT_FILENO) error (%d)\n", errno);
809                    goto cleanup;
810            }
811    #endif
812    
813            BBS_last_access_tm = t_used = time(NULL);
814            loop = 1;
815    
816            while (loop && !SYS_server_exit)
817            {
818                    if (SSH_v2 && ssh_channel_is_closed(SSH_channel))
819                    {
820    #ifdef _DEBUG
821                            log_error("SSH channel is closed\n");
822    #endif
823                            loop = 0;
824                            break;
825                    }
826    
827                    if (bbsnet_conf[n].use_ssh && ssh_channel_is_closed(channel))
828                    {
829    #ifdef _DEBUG
830                            log_error("Remote SSH channel is closed\n");
831    #endif
832                            loop = 0;
833                            break;
834                    }
835    
836    #ifdef HAVE_SYS_EPOLL_H
837                    nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second
838                    ret = nfds;
839    #else
840                    pfds[0].fd = STDIN_FILENO;
841                    pfds[0].events = POLLIN;
842                    pfds[1].fd = sock;
843                    pfds[1].events = POLLIN | POLLOUT;
844                    pfds[2].fd = STDOUT_FILENO;
845                    pfds[2].events = POLLOUT;
846                    nfds = 3;
847                    ret = poll(pfds, (nfds_t)nfds, 100); // 0.1 second
848    #endif
849    
850                    if (ret < 0)
851                    {
852                            if (errno != EINTR)
853                            {
854    #ifdef HAVE_SYS_EPOLL_H
855                                    log_error("epoll_wait() error (%d)\n", errno);
856    #else
857                                    log_error("poll() error (%d)\n", errno);
858    #endif
859                                    break;
860                            }
861                            continue;
862                    }
863                    else if (ret == 0) // timeout
864                    {
865                            if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time)
866                            {
867                                    break;
868                            }
869                    }
870    
871    t_used = time (0) - t_used;                  for (int i = 0; i < nfds; i++)
872    tm_used = gmtime (&t_used);                  {
873    #ifdef HAVE_SYS_EPOLL_H
874                            if (events[i].data.fd == STDIN_FILENO)
875    #else
876                            if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & POLLIN))
877    #endif
878                            {
879                                    stdin_read_wait = 1;
880                            }
881    
882    #ifdef HAVE_SYS_EPOLL_H
883                            if (events[i].data.fd == sock)
884    #else
885                            if (pfds[i].fd == sock)
886    #endif
887                            {
888    #ifdef HAVE_SYS_EPOLL_H
889                                    if (events[i].events & EPOLLIN)
890    #else
891                                    if (pfds[i].revents & POLLIN)
892    #endif
893                                    {
894                                            sock_read_wait = 1;
895                                    }
896    
897    #ifdef HAVE_SYS_EPOLL_H
898                                    if (events[i].events & EPOLLOUT)
899    #else
900                                    if (pfds[i].revents & POLLOUT)
901    #endif
902                                    {
903                                            sock_write_wait = 1;
904                                    }
905                            }
906    
907    #ifdef HAVE_SYS_EPOLL_H
908                            if (events[i].data.fd == STDOUT_FILENO)
909    #else
910                            if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & POLLOUT))
911    #endif
912                            {
913                                    stdout_write_wait = 1;
914                            }
915                    }
916    
917    log_std ("BBSNET disconnect, %d days %d hours %d minutes %d seconds used\n",                  if (stdin_read_wait)
918      tm_used->tm_mday - 1, tm_used->tm_hour, tm_used->tm_min, tm_used->tm_sec);                  {
919                            while (input_buf_len < sizeof(input_buf) && !SYS_server_exit)
920                            {
921                                    if (SSH_v2)
922                                    {
923                                            ret = ssh_channel_read_nonblocking(SSH_channel, input_buf + input_buf_len, sizeof(input_buf) - (uint32_t)input_buf_len, 0);
924                                            if (ret == SSH_ERROR)
925                                            {
926    #ifdef _DEBUG
927                                                    log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session));
928    #endif
929                                                    loop = 0;
930                                                    break;
931                                            }
932                                            else if (ret == SSH_EOF)
933                                            {
934                                                    stdin_read_wait = 0;
935                                                    loop = 0;
936                                                    break;
937                                            }
938                                            else if (ret == 0)
939                                            {
940                                                    // Send NO-OP to remote server
941                                                    input_buf[input_buf_len] = '\0';
942                                                    input_buf_len++;
943                                                    BBS_last_access_tm = time(NULL);
944    
945                                                    stdin_read_wait = 0;
946                                                    break; // Check whether channel is still open
947                                            }
948                                    }
949                                    else
950                                    {
951                                            ret = (int)read(STDIN_FILENO, input_buf + input_buf_len, sizeof(input_buf) - (size_t)input_buf_len);
952                                    }
953                                    if (ret < 0)
954                                    {
955                                            if (errno == EAGAIN || errno == EWOULDBLOCK)
956                                            {
957                                                    stdin_read_wait = 0;
958                                                    break;
959                                            }
960                                            else if (errno == EINTR)
961                                            {
962                                                    continue;
963                                            }
964                                            else
965                                            {
966                                                    log_error("read(STDIN) error (%d)\n", errno);
967                                                    loop = 0;
968                                                    break;
969                                            }
970                                    }
971                                    else if (ret == 0) // broken pipe
972                                    {
973    #ifdef _DEBUG
974                                            log_error("read(STDIN) EOF\n");
975    #endif
976                                            stdin_read_wait = 0;
977                                            loop = 0;
978                                            break;
979                                    }
980                                    else
981                                    {
982                                            input_buf_len += ret;
983                                            BBS_last_access_tm = time(NULL);
984    
985                                            // Refresh current action while user input
986                                            if (user_online_update("BBS_NET") < 0)
987                                            {
988                                                    log_error("user_online_update(BBS_NET) error\n");
989                                            }
990    
991                                            continue;
992                                    }
993                            }
994                    }
995    
996    return 0;                  if (sock_write_wait)
997                    {
998                            if (input_buf_offset < input_buf_len)
999                            {
1000                                    // For debug
1001    #ifdef _DEBUG
1002                                    for (int j = input_buf_offset; j < input_buf_len; j++)
1003                                    {
1004                                            log_error("Debug input: <--[%u]\n", (input_buf[j] + 256) % 256);
1005                                    }
1006    #endif
1007    
1008                                    ret = io_buf_conv(input_cd, input_buf, &input_buf_len, &input_buf_offset, input_conv, sizeof(input_conv), &input_conv_len);
1009                                    if (ret < 0)
1010                                    {
1011                                            log_error("io_buf_conv(input, %d, %d, %d) error\n", input_buf_len, input_buf_offset, input_conv_len);
1012                                            input_buf_len = input_buf_offset; // Discard invalid sequence
1013                                    }
1014    
1015                                    // For debug
1016    #ifdef _DEBUG
1017                                    for (int j = input_conv_offset; j < input_conv_len; j++)
1018                                    {
1019                                            log_error("Debug input_conv: <--[%u]\n", (input_conv[j] + 256) % 256);
1020                                    }
1021    #endif
1022                            }
1023    
1024                            while (input_conv_offset < input_conv_len && !SYS_server_exit)
1025                            {
1026                                    if (bbsnet_conf[n].use_ssh)
1027                                    {
1028                                            ret = ssh_channel_write(channel, input_conv + input_conv_offset, (uint32_t)(input_conv_len - input_conv_offset));
1029                                            if (ret == SSH_ERROR)
1030                                            {
1031    #ifdef _DEBUG
1032                                                    log_error("ssh_channel_write() error: %s\n", ssh_get_error(session));
1033    #endif
1034                                                    loop = 0;
1035                                                    break;
1036                                            }
1037                                    }
1038                                    else
1039                                    {
1040                                            ret = (int)write(sock, input_conv + input_conv_offset, (size_t)(input_conv_len - input_conv_offset));
1041                                    }
1042                                    if (ret < 0)
1043                                    {
1044                                            if (errno == EAGAIN || errno == EWOULDBLOCK)
1045                                            {
1046                                                    sock_write_wait = 0;
1047                                                    break;
1048                                            }
1049                                            else if (errno == EINTR)
1050                                            {
1051                                                    continue;
1052                                            }
1053                                            else
1054                                            {
1055    #ifdef _DEBUG
1056                                                    log_error("write(socket) error (%d)\n", errno);
1057    #endif
1058                                                    loop = 0;
1059                                                    break;
1060                                            }
1061                                    }
1062                                    else if (ret == 0) // broken pipe
1063                                    {
1064    #ifdef _DEBUG
1065                                            log_error("write(socket) EOF\n");
1066    #endif
1067                                            sock_write_wait = 0;
1068                                            loop = 0;
1069                                            break;
1070                                    }
1071                                    else
1072                                    {
1073                                            input_conv_offset += ret;
1074                                            if (input_conv_offset >= input_conv_len) // Output buffer complete
1075                                            {
1076                                                    input_conv_offset = 0;
1077                                                    input_conv_len = 0;
1078                                                    break;
1079                                            }
1080                                            continue;
1081                                    }
1082                            }
1083                    }
1084    
1085                    if (sock_read_wait)
1086                    {
1087                            while (output_buf_len < sizeof(output_buf) && !SYS_server_exit)
1088                            {
1089                                    if (bbsnet_conf[n].use_ssh)
1090                                    {
1091                                            ret = ssh_channel_read_nonblocking(channel, output_buf + output_buf_len,
1092                                                                                                               (uint32_t)(sizeof(output_buf) - (size_t)output_buf_len), 0);
1093                                            if (ret == SSH_ERROR)
1094                                            {
1095    #ifdef _DEBUG
1096                                                    log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(session));
1097    #endif
1098                                                    loop = 0;
1099                                                    break;
1100                                            }
1101                                            else if (ret == SSH_EOF)
1102                                            {
1103                                                    sock_read_wait = 0;
1104                                                    loop = 0;
1105                                                    break;
1106                                            }
1107                                            else if (ret == 0)
1108                                            {
1109                                                    sock_read_wait = 0;
1110                                                    break;
1111                                            }
1112                                    }
1113                                    else
1114                                    {
1115                                            ret = (int)read(sock, output_buf + output_buf_len, sizeof(output_buf) - (size_t)output_buf_len);
1116                                    }
1117                                    if (ret < 0)
1118                                    {
1119                                            if (errno == EAGAIN || errno == EWOULDBLOCK)
1120                                            {
1121                                                    sock_read_wait = 0;
1122                                                    break;
1123                                            }
1124                                            else if (errno == EINTR)
1125                                            {
1126                                                    continue;
1127                                            }
1128                                            else
1129                                            {
1130    #ifdef _DEBUG
1131                                                    log_error("read(socket) error (%d)\n", errno);
1132    #endif
1133                                                    loop = 0;
1134                                                    break;
1135                                            }
1136                                    }
1137                                    else if (ret == 0) // broken pipe
1138                                    {
1139    #ifdef _DEBUG
1140                                            log_error("read(socket) EOF\n");
1141    #endif
1142                                            sock_read_wait = 0;
1143                                            loop = 0;
1144                                            break;
1145                                    }
1146                                    else
1147                                    {
1148                                            output_buf_len += ret;
1149                                            continue;
1150                                    }
1151                            }
1152                    }
1153    
1154                    if (stdout_write_wait)
1155                    {
1156                            if (output_buf_offset < output_buf_len)
1157                            {
1158                                    ret = io_buf_conv(output_cd, output_buf, &output_buf_len, &output_buf_offset, output_conv, sizeof(output_conv), &output_conv_len);
1159                                    if (ret < 0)
1160                                    {
1161                                            log_error("io_buf_conv(output, %d, %d, %d) error\n", output_buf_len, output_buf_offset, output_conv_len);
1162                                            output_buf_len = output_buf_offset; // Discard invalid sequence
1163                                    }
1164                            }
1165    
1166                            while (output_conv_offset < output_conv_len && !SYS_server_exit)
1167                            {
1168                                    if (SSH_v2)
1169                                    {
1170                                            ret = ssh_channel_write(SSH_channel, output_conv + output_conv_offset, (uint32_t)(output_conv_len - output_conv_offset));
1171                                            if (ret == SSH_ERROR)
1172                                            {
1173    #ifdef _DEBUG
1174                                                    log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session));
1175    #endif
1176                                                    loop = 0;
1177                                                    break;
1178                                            }
1179                                    }
1180                                    else
1181                                    {
1182                                            ret = (int)write(STDOUT_FILENO, output_conv + output_conv_offset, (size_t)(output_conv_len - output_conv_offset));
1183                                    }
1184                                    if (ret < 0)
1185                                    {
1186                                            if (errno == EAGAIN || errno == EWOULDBLOCK)
1187                                            {
1188                                                    stdout_write_wait = 0;
1189                                                    break;
1190                                            }
1191                                            else if (errno == EINTR)
1192                                            {
1193                                                    continue;
1194                                            }
1195                                            else
1196                                            {
1197    #ifdef _DEBUG
1198                                                    log_error("write(STDOUT) error (%d)\n", errno);
1199    #endif
1200                                                    loop = 0;
1201                                                    break;
1202                                            }
1203                                    }
1204                                    else if (ret == 0) // broken pipe
1205                                    {
1206    #ifdef _DEBUG
1207                                            log_error("write(STDOUT) EOF\n");
1208    #endif
1209                                            stdout_write_wait = 0;
1210                                            loop = 0;
1211                                            break;
1212                                    }
1213                                    else
1214                                    {
1215                                            output_conv_offset += ret;
1216                                            if (output_conv_offset >= output_conv_len) // Output buffer complete
1217                                            {
1218                                                    output_conv_offset = 0;
1219                                                    output_conv_len = 0;
1220                                                    break;
1221                                            }
1222                                            continue;
1223                                    }
1224                            }
1225                    }
1226            }
1227    
1228    cleanup:
1229            if (input_cd != (iconv_t)(-1))
1230            {
1231                    iconv_close(input_cd);
1232            }
1233            if (output_cd != (iconv_t)(-1))
1234            {
1235                    iconv_close(output_cd);
1236            }
1237    
1238    #ifdef HAVE_SYS_EPOLL_H
1239            if (epollfd != -1 && close(epollfd) < 0)
1240            {
1241                    log_error("close(epoll) error (%d)\n");
1242            }
1243    #endif
1244    
1245            if (bbsnet_conf[n].use_ssh)
1246            {
1247                    if (channel != NULL)
1248                    {
1249                            ssh_channel_free(channel);
1250                    }
1251                    if (session != NULL)
1252                    {
1253                            ssh_disconnect(session);
1254                            ssh_free(session);
1255                    }
1256            }
1257    
1258            // Restore STDIN/STDOUT flags
1259            if (flags_stdin != -1 && fcntl(STDIN_FILENO, F_SETFL, flags_stdin) == -1)
1260            {
1261                    log_error("fcntl(F_SETFL) error (%d)\n", errno);
1262            }
1263            if (flags_stdout != -1 && fcntl(STDOUT_FILENO, F_SETFL, flags_stdout) == -1)
1264            {
1265                    log_error("fcntl(F_SETFL) error (%d)\n", errno);
1266            }
1267    
1268            if (sock != -1 && close(sock) == -1)
1269            {
1270                    log_error("Close socket failed\n");
1271            }
1272    
1273            if (res)
1274            {
1275                    freeaddrinfo(res);
1276            }
1277    
1278            t_used = time(NULL) - t_used;
1279            tm_used = gmtime(&t_used);
1280    
1281            log_common("BBSNET disconnect, %d days %d hours %d minutes %d seconds used\n",
1282                               tm_used->tm_yday, tm_used->tm_hour, tm_used->tm_min, tm_used->tm_sec);
1283    
1284            BBS_last_access_tm = time(NULL);
1285    
1286            return 0;
1287  }  }
1288    
1289  static int  static int bbsnet_refresh()
 bbsnet_refresh ()  
1290  {  {
1291    int i;          clearscr();
1292    
1293    clearscr ();          moveto(1, 1);
1294    moveto (1, 0);          prints(" ------------------------------------------------------------------------------ ");
1295    prints          for (int i = 2; i < 19; i++)
1296      ("qTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTr");          {
1297    for (i = 2; i < 19; i++)                  moveto(i, 1);
1298      {                  prints("|");
1299        moveto (i, 0);                  moveto(i, 80);
1300        prints ("U");                  prints("|");
1301        moveto (i, 79);          }
1302        prints ("U");          moveto(19, 1);
1303      }          prints("|------------------------------------------------------------------------------|");
1304    moveto (19, 0);          moveto(22, 1);
1305    prints          prints(" ------------------------------------------------------------------------------ ");
1306      ("UU");          moveto(23, 1);
1307    moveto (22, 0);          prints(" [\033[1;32mCtrl+C\033[m]退出");
   prints  
     ("tTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTs");  
   moveto (23, 0);  
   prints (" [\x1b[1;32mCtrl+C\x1b[m]˳");  
   iflush ();  
1308    
1309    return 0;          iflush();
1310    
1311            return 0;
1312  }  }
1313    
1314  int  static int bbsnet_selchange()
 bbsnet_selchange (int new_pos)  
1315  {  {
1316    moveto (20, 0);          int i = bbsnet_menu.menu_item_pos[0];
1317    clrtoeol ();  
1318    prints ("U\x1b[1mλ:\x1b[1;33m%-18s\x1b[m  վ:\x1b[1;33m%s\x1b[m",          moveto(20, 1);
1319            bbsnet_conf[new_pos].host2, bbsnet_conf[new_pos].host1);          clrtoeol();
1320    moveto (20, 79);          prints("|\033[1m单位: \033[1;33m%s\033[m%*s  站名: \033[1;33m%s\033[m%*s  类型: \033[1;33m%s\033[m",
1321    prints ("U");                     bbsnet_conf[i].org_name, 20 - str_length(bbsnet_conf[i].org_name, 1), "",
1322    moveto (21, 0);                     bbsnet_conf[i].site_name, 20 - str_length(bbsnet_conf[i].site_name, 1), "",
1323    clrtoeol ();                     (bbsnet_conf[i].use_ssh ? "SSH" : "Telnet"));
1324    prints ("U\x1b[1m:\x1b[1;33m%-20s", bbsnet_conf[new_pos].ip);          moveto(20, 80);
1325    if (bbsnet_conf[new_pos].port != 23)          prints("|");
1326      prints ("  %d", bbsnet_conf[new_pos].port);          moveto(21, 1);
1327    prints ("\x1b[m");          clrtoeol();
1328    moveto (21, 79);          prints("|\033[1m连往: \033[1;33m%-20s\033[m  端口: \033[1;33m%-5s\033[m                 编码: \033[1;33m%s\033[m",
1329    prints ("U");                     bbsnet_conf[i].host_name, bbsnet_conf[i].port, bbsnet_conf[i].charset);
1330    iflush ();          moveto(21, 80);
1331            prints("|");
1332            iflush();
1333    
1334    return 0;          return 0;
1335  }  }
1336    
1337  int  int bbs_net()
 bbs_net ()  
1338  {  {
1339    int ch, pos, i;          int ch, i;
   char file_config[256];  
1340    
1341    strcpy (file_config, app_home_dir);          if (load_bbsnet_conf(CONF_BBSNET) < 0)
1342    strcat (file_config, "conf/bbsnet.conf");          {
1343                    clearscr();
1344                    moveto(1, 1);
1345                    prints("加载穿梭配置失败!");
1346                    press_any_key();
1347                    return -1;
1348            }
1349    
1350    load_bbsnet_conf (file_config);          bbsnet_refresh();
1351            display_menu(&bbsnet_menu);
1352            bbsnet_selchange();
1353    
1354    BBS_last_access_tm = time (0);          while (!SYS_server_exit)
1355            {
1356    clearscr ();                  ch = igetch(100);
1357    bbsnet_refresh ();  
1358    pos = bbsnet_menu.p_menu[0]->item_cur_pos;                  if (ch != KEY_NULL && ch != KEY_TIMEOUT)
1359    display_menu (get_menu (&bbsnet_menu, "BBSNET"));                  {
1360    bbsnet_selchange (pos);                          BBS_last_access_tm = time(NULL);
1361                    }
1362    while (1)  
1363      {                  switch (ch)
1364        ch = igetch ();                  {
1365        switch (ch)                  case KEY_NULL: // broken pipe
1366          {  #ifdef _DEBUG
1367          case KEY_NULL:                          log_error("KEY_NULL\n");
1368          case Ctrl ('C'):  #endif
1369            return 0;                          goto cleanup;
1370          case KEY_TIMEOUT:                  case KEY_TIMEOUT:
1371            if (time (0) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time)
1372              {                          {
1373                return -1;                                  log_error("User input timeout\n");
1374              }                                  goto cleanup;
1375            continue;                          }
1376          case CR:                          continue;
1377            pos = bbsnet_menu.p_menu[0]->item_cur_pos;                  case KEY_ESC:
1378            bbsnet_connect (pos);                  case Ctrl('C'): // user cancel
1379            bbsnet_refresh ();                          goto cleanup;
1380            display_current_menu (&bbsnet_menu);                  case CR:
1381            bbsnet_selchange (pos);                          bbsnet_connect(bbsnet_menu.menu_item_pos[0]);
1382            break;                          // Force cleanup anything remaining in the output buffer
1383          case KEY_UP:                          clearscr();
1384            for (i = 0; i < STATION_PER_LINE; i++)                          iflush();
1385              menu_control (&bbsnet_menu, KEY_UP);                          // Clear screen and redraw menu
1386            pos = bbsnet_menu.p_menu[0]->item_cur_pos;                          bbsnet_refresh();
1387            bbsnet_selchange (pos);                          display_menu(&bbsnet_menu);
1388            break;                          bbsnet_selchange();
1389          case KEY_DOWN:                          break;
1390            for (i = 0; i < STATION_PER_LINE; i++)                  case KEY_UP:
1391              menu_control (&bbsnet_menu, KEY_DOWN);                          for (i = 0; i < STATION_PER_LINE; i++)
1392            pos = bbsnet_menu.p_menu[0]->item_cur_pos;                          {
1393            bbsnet_selchange (pos);                                  menu_control(&bbsnet_menu, KEY_UP);
1394            break;                          }
1395          case KEY_LEFT:                          bbsnet_selchange();
1396            menu_control (&bbsnet_menu, KEY_UP);                          break;
1397            pos = bbsnet_menu.p_menu[0]->item_cur_pos;                  case KEY_DOWN:
1398            bbsnet_selchange (pos);                          for (i = 0; i < STATION_PER_LINE; i++)
1399            break;                          {
1400          case KEY_RIGHT:                                  menu_control(&bbsnet_menu, KEY_DOWN);
1401            menu_control (&bbsnet_menu, KEY_DOWN);                          }
1402            pos = bbsnet_menu.p_menu[0]->item_cur_pos;                          bbsnet_selchange();
1403            bbsnet_selchange (pos);                          break;
1404            break;                  case KEY_LEFT:
1405          default:                          menu_control(&bbsnet_menu, KEY_UP);
1406            menu_control (&bbsnet_menu, ch);                          bbsnet_selchange();
1407            pos = bbsnet_menu.p_menu[0]->item_cur_pos;                          break;
1408            bbsnet_selchange (pos);                  case KEY_RIGHT:
1409            break;                          menu_control(&bbsnet_menu, KEY_DOWN);
1410                            bbsnet_selchange();
1411                            break;
1412                    case KEY_HOME:
1413                    case KEY_PGUP:
1414                            menu_control(&bbsnet_menu, KEY_PGUP);
1415                            bbsnet_selchange();
1416                            break;
1417                    case KEY_END:
1418                    case KEY_PGDN:
1419                            menu_control(&bbsnet_menu, KEY_PGDN);
1420                            bbsnet_selchange();
1421                            break;
1422                    default:
1423                            menu_control(&bbsnet_menu, ch);
1424                            bbsnet_selchange();
1425                            break;
1426                    }
1427          }          }
       BBS_last_access_tm = time (0);  
     }  
1428    
1429    return 0;  cleanup:
1430            unload_bbsnet_conf();
1431    
1432            return 0;
1433  }  }


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

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