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


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

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