/[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.36 by sysadm, Sun May 11 12:47:32 2025 UTC Revision 1.78 by sysadm, Wed Nov 5 01:04:06 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /* SPDX-License-Identifier: GPL-3.0-or-later */
2                                                    bbs_net.c  -  description  /*
3                                                           -------------------   * bbs_net
4          Copyright            : (C) 2004-2025 by Leaflet   *   - user interactive feature of site shuttle
5          Email                : leaflet@leafok.com   *
6   ***************************************************************************/   * Copyright (C) 2004-2025  Leaflet <leaflet@leafok.com>
7     */
 /***************************************************************************  
  *                                                                         *  
  *   This program is free software; you can redistribute it and/or modify  *  
  *   it under the terms of the GNU General Public License as published by  *  
  *   the Free Software Foundation; either version 3 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
8    
9  #include "bbs.h"  #include "bbs.h"
10  #include "common.h"  #include "common.h"
 #include "log.h"  
11  #include "io.h"  #include "io.h"
12  #include "screen.h"  #include "log.h"
13    #include "login.h"
14  #include "menu.h"  #include "menu.h"
15  #include <stdio.h>  #include "screen.h"
 #include <stdarg.h>  
16  #include <errno.h>  #include <errno.h>
 #include <string.h>  
 #include <stdlib.h>  
17  #include <fcntl.h>  #include <fcntl.h>
18    #include <netdb.h>
19    #include <stdarg.h>
20    #include <stdio.h>
21    #include <stdlib.h>
22    #include <string.h>
23  #include <time.h>  #include <time.h>
24  #include <unistd.h>  #include <unistd.h>
25  #include <netdb.h>  #include <arpa/inet.h>
26    #include <libssh/libssh.h>
27    #include <libssh/server.h>
28    #include <libssh/callbacks.h>
29    #include <netinet/in.h>
30    #include <netinet/ip.h>
31  #include <sys/select.h>  #include <sys/select.h>
32  #include <sys/ioctl.h>  #include <sys/ioctl.h>
33  #include <sys/socket.h>  #include <sys/socket.h>
34  #include <netinet/in.h>  #include <sys/epoll.h>
 #include <netinet/ip.h>  
 #include <arpa/inet.h>  
35    
36  #define MENU_CONF_DELIM " \t\r\n"  #define MENU_CONF_DELIM " \t\r\n"
37    
# Line 48  struct _bbsnet_conf Line 45  struct _bbsnet_conf
45          char host2[40];          char host2[40];
46          char ip[40];          char ip[40];
47          in_port_t port;          in_port_t port;
48            char charset[20];
49  } bbsnet_conf[MAXSTATION];  } bbsnet_conf[MAXSTATION];
50    
51  MENU_SET bbsnet_menu;  static MENU_SET bbsnet_menu;
52    
53  int load_bbsnet_conf(const char *file_config)  static int load_bbsnet_conf(const char *file_config)
54  {  {
55          FILE *fp;          FILE *fp;
56          MENU *p_menu;          MENU *p_menu;
57          MENU_ITEM *p_menuitem;          MENU_ITEM *p_menu_item;
58          char t[256], *t1, *t2, *t3, *t4, *saveptr;          MENU_ITEM_ID menu_item_id;
59          int item_count = 0;          char t[256], *t1, *t2, *t3, *t4, *t5, *saveptr;
60    
61          fp = fopen(file_config, "r");          fp = fopen(file_config, "r");
62          if (fp == NULL)          if (fp == NULL)
# Line 66  int load_bbsnet_conf(const char *file_co Line 64  int load_bbsnet_conf(const char *file_co
64                  return -1;                  return -1;
65          }          }
66    
67          p_menu = bbsnet_menu.p_menu[0] = malloc(sizeof(MENU));          bbsnet_menu.p_menu_pool = calloc(1, sizeof(MENU));
68            if (bbsnet_menu.p_menu_pool == NULL)
69            {
70                    log_error("calloc(p_menu_pool) error\n");
71                    return -3;
72            }
73            bbsnet_menu.menu_count = 1;
74    
75            bbsnet_menu.p_menu_item_pool = calloc(MAXSTATION, sizeof(MENU_ITEM));
76            if (bbsnet_menu.p_menu_item_pool == NULL)
77            {
78                    log_error("calloc(p_menu_item_pool) error\n");
79                    return -3;
80            }
81            bbsnet_menu.menu_item_count = MAXSTATION;
82    
83            p_menu = (MENU *)get_menu_by_id(&bbsnet_menu, 0);
84    
85          strncpy(p_menu->name, "BBSNET", sizeof(p_menu->name) - 1);          strncpy(p_menu->name, "BBSNET", sizeof(p_menu->name) - 1);
86          p_menu->name[sizeof(p_menu->name) - 1] = '\0';          p_menu->name[sizeof(p_menu->name) - 1] = '\0';
87          p_menu->title.show = 0;          p_menu->title.show = 0;
88          p_menu->screen.show = 0;          p_menu->screen_show = 0;
89    
90          while (fgets(t, 255, fp) && item_count < MAXSTATION)          menu_item_id = 0;
91            while (fgets(t, 255, fp) && menu_item_id < MAXSTATION)
92          {          {
93                  t1 = strtok_r(t, MENU_CONF_DELIM, &saveptr);                  t1 = strtok_r(t, MENU_CONF_DELIM, &saveptr);
94                  t2 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);                  t2 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
95                  t3 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);                  t3 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
96                  t4 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);                  t4 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
97                    t5 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
98    
99                  if (t1 == NULL || t2 == NULL || t3 == NULL || t4 == NULL || t[0] == '#' || t[0] == '*')                  if (t1 == NULL || t2 == NULL || t3 == NULL || t4 == NULL || t5 == NULL || t[0] == '#' || t[0] == '*')
100                  {                  {
101                          continue;                          continue;
102                  }                  }
103    
104                  strncpy(bbsnet_conf[item_count].host1, t2, sizeof(bbsnet_conf[item_count].host1) - 1);                  strncpy(bbsnet_conf[menu_item_id].host1, t2, sizeof(bbsnet_conf[menu_item_id].host1) - 1);
105                  bbsnet_conf[item_count].host1[sizeof(bbsnet_conf[item_count].host1) - 1] = '\0';                  bbsnet_conf[menu_item_id].host1[sizeof(bbsnet_conf[menu_item_id].host1) - 1] = '\0';
106                  strncpy(bbsnet_conf[item_count].host2, t1, sizeof(bbsnet_conf[item_count].host2) - 1);                  strncpy(bbsnet_conf[menu_item_id].host2, t1, sizeof(bbsnet_conf[menu_item_id].host2) - 1);
107                  bbsnet_conf[item_count].host2[sizeof(bbsnet_conf[item_count].host2) - 1] = '\0';                  bbsnet_conf[menu_item_id].host2[sizeof(bbsnet_conf[menu_item_id].host2) - 1] = '\0';
108                  strncpy(bbsnet_conf[item_count].ip, t3, sizeof(bbsnet_conf[item_count].ip) - 1);                  strncpy(bbsnet_conf[menu_item_id].ip, t3, sizeof(bbsnet_conf[menu_item_id].ip) - 1);
109                  bbsnet_conf[item_count].ip[sizeof(bbsnet_conf[item_count].ip) - 1] = '\0';                  bbsnet_conf[menu_item_id].ip[sizeof(bbsnet_conf[menu_item_id].ip) - 1] = '\0';
110                  bbsnet_conf[item_count].port = (in_port_t)(t4 ? atoi(t4) : 23);                  bbsnet_conf[menu_item_id].port = (in_port_t)(t4 ? atoi(t4) : 23);
111                    strncpy(bbsnet_conf[menu_item_id].charset, t5, sizeof(bbsnet_conf[menu_item_id].charset) - 1);
112                  p_menuitem = p_menu->items[item_count] = malloc(sizeof(MENU_ITEM));                  bbsnet_conf[menu_item_id].charset[sizeof(bbsnet_conf[menu_item_id].charset) - 1] = '\0';
                 p_menuitem->row = 2 + item_count / STATION_PER_LINE;  
                 p_menuitem->col = 5 + item_count % STATION_PER_LINE * 20;  
                 snprintf(p_menuitem->action, sizeof(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] =  
                         (char)(item_count < MAXSTATION / 2 ? 'A' + item_count : 'a' + item_count);  
                 p_menuitem->name[1] = '\0';  
                 snprintf(p_menuitem->text, sizeof(p_menuitem->text), "%c. %s",  
                                  p_menuitem->name[0], bbsnet_conf[item_count].host1);  
113    
114                  item_count++;                  p_menu_item = get_menu_item_by_id(&bbsnet_menu, menu_item_id);
115          }                  if (p_menu_item == NULL)
116          fclose(fp);                  {
117                            log_error("get_menu_item_by_id(%d) return NULL pointer\n", menu_item_id);
118                            return -1;
119                    }
120    
121          p_menu->item_count = item_count;                  p_menu_item->row = (int16_t)(2 + menu_item_id / STATION_PER_LINE);
122          p_menu->item_cur_pos = 0;                  p_menu_item->col = (int16_t)(5 + menu_item_id % STATION_PER_LINE * 20);
123                    snprintf(p_menu_item->action, sizeof(p_menu_item->action), "%d", (int16_t)menu_item_id);
124                    p_menu_item->submenu = 0;
125                    p_menu_item->priv = 0;
126                    p_menu_item->level = 0;
127                    p_menu_item->name[0] =
128                            (char)(menu_item_id < MAXSTATION / 2 ? 'A' + menu_item_id : 'a' + menu_item_id);
129                    p_menu_item->name[1] = '\0';
130                    snprintf(p_menu_item->text, sizeof(p_menu_item->text), "%c. %s",
131                                     p_menu_item->name[0], bbsnet_conf[menu_item_id].host1);
132    
133                    p_menu->items[p_menu->item_count] = menu_item_id;
134                    p_menu->item_count++;
135                    menu_item_id++;
136            }
137    
138            bbsnet_menu.menu_item_count = (int16_t)menu_item_id;
139            bbsnet_menu.menu_id_path[0] = 0;
140            bbsnet_menu.menu_item_pos[0] = 0;
141            bbsnet_menu.choose_step = 0;
142    
143          bbsnet_menu.menu_count = 1;          fclose(fp);
         bbsnet_menu.menu_select_depth = 0;  
         bbsnet_menu.p_menu_select[0] = bbsnet_menu.p_menu[0];  
144    
145          return 0;          return 0;
146  }  }
147    
148    static void unload_bbsnet_conf(void)
149    {
150            bbsnet_menu.menu_count = 0;
151            bbsnet_menu.menu_item_count = 0;
152    
153            free(bbsnet_menu.p_menu_pool);
154            bbsnet_menu.p_menu_pool = NULL;
155            free(bbsnet_menu.p_menu_item_pool);
156            bbsnet_menu.p_menu_item_pool = NULL;
157    }
158    
159  static void process_bar(int n, int len)  static void process_bar(int n, int len)
160  {  {
161          char buf[LINE_BUFFER_LEN];          char buf[LINE_BUFFER_LEN];
# Line 141  static void process_bar(int n, int len) Line 177  static void process_bar(int n, int len)
177          moveto(4, 0);          moveto(4, 0);
178          prints(" ------------------------------ \r\n");          prints(" ------------------------------ \r\n");
179          snprintf(buf, sizeof(buf), "            %3d%%              ", n * 100 / len);          snprintf(buf, sizeof(buf), "            %3d%%              ", n * 100 / len);
180          strncpy(buf2, buf, (size_t)n);          memcpy(buf2, buf, (size_t)n);
181          buf2[n] = '\0';          buf2[n] = '\0';
182          prints("|\033[46m%s\033[44m%s\033[m|\r\n", buf2, buf + n);          prints("|\033[46m%s\033[44m%s\033[m|\r\n", buf2, buf + n);
183          prints(" ------------------------------ \r\n");          prints(" ------------------------------ \r\n");
184          iflush();          iflush();
185  }  }
186    
187  int bbsnet_connect(int n)  static int bbsnet_connect(int n)
188  {  {
189          int sock, ret, loop, error;          int sock, ret, loop, error;
190            int sock_connected = 0;
191          int flags_sock;          int flags_sock;
192          int flags_stdin;          int flags_stdin;
193          int flags_stdout;          int flags_stdout;
# Line 162  int bbsnet_connect(int n) Line 199  int bbsnet_connect(int n)
199          int output_buf_len = 0;          int output_buf_len = 0;
200          int input_buf_offset = 0;          int input_buf_offset = 0;
201          int output_buf_offset = 0;          int output_buf_offset = 0;
202          fd_set read_fds;          char input_conv[LINE_BUFFER_LEN * 2];
203          fd_set write_fds;          char output_conv[LINE_BUFFER_LEN * 2];
204          struct timeval timeout;          int input_conv_len = 0;
205            int output_conv_len = 0;
206            int input_conv_offset = 0;
207            int output_conv_offset = 0;
208            iconv_t input_cd = NULL;
209            iconv_t output_cd = NULL;
210            char tocode[32];
211            struct epoll_event ev, events[MAX_EVENTS];
212            int nfds, epollfd;
213            int stdin_read_wait = 0;
214            int stdout_write_wait = 0;
215            int sock_read_wait = 0;
216            int sock_write_wait = 0;
217          struct hostent *p_host = NULL;          struct hostent *p_host = NULL;
218          int tos;          int tos;
         int i;  
219          char remote_addr[IP_ADDR_LEN];          char remote_addr[IP_ADDR_LEN];
220          int remote_port;          int remote_port;
221          time_t t_used;          char local_addr[IP_ADDR_LEN];
222            int local_port;
223            socklen_t sock_len;
224            time_t t_used = time(NULL);
225          struct tm *tm_used;          struct tm *tm_used;
226          int ch;          int ch;
227    
228            if (user_online_update("BBS_NET") < 0)
229            {
230                    log_error("user_online_update(BBS_NET) error\n");
231            }
232    
233          clearscr();          clearscr();
234    
235          moveto(0, 0);          moveto(0, 0);
236          prints("\033[1;32mڲ %s (%s) ӣԺ... \033[m\r\n",          prints("\033[1;32m正在测试往 %s (%s) 的连接,请稍候... \033[m\r\n",
237                     bbsnet_conf[n].host1, bbsnet_conf[n].ip);                     bbsnet_conf[n].host1, bbsnet_conf[n].ip);
238          iflush();          iflush();
239    
# Line 185  int bbsnet_connect(int n) Line 241  int bbsnet_connect(int n)
241    
242          if (p_host == NULL)          if (p_host == NULL)
243          {          {
244                  prints("\033[1;31mʧܣ\033[m\r\n");                  prints("\033[1;31m查找主机名失败!\033[m\r\n");
245                  press_any_key();                  press_any_key();
246                  return -1;                  return -1;
247          }          }
# Line 194  int bbsnet_connect(int n) Line 250  int bbsnet_connect(int n)
250    
251          if (sock < 0)          if (sock < 0)
252          {          {
253                  prints("\033[1;31m޷socket\033[m\r\n");                  prints("\033[1;31m无法创建socket!\033[m\r\n");
254                  press_any_key();                  press_any_key();
255                  return -1;                  return -1;
256          }          }
257    
258          sin.sin_family = AF_INET;          sin.sin_family = AF_INET;
259          sin.sin_addr.s_addr = (hostaddr_server[0] != '\0' ? inet_addr(hostaddr_server) : INADDR_ANY);          sin.sin_addr.s_addr = (BBS_address[0] != '\0' ? inet_addr(BBS_address) : INADDR_ANY);
260          sin.sin_port = 0;          sin.sin_port = 0;
261    
262          if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0)          if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0)
# Line 210  int bbsnet_connect(int n) Line 266  int bbsnet_connect(int n)
266                  return -2;                  return -2;
267          }          }
268    
269          bzero(&sin, sizeof(sin));          memset(&sin, 0, sizeof(sin));
270          sin.sin_family = AF_INET;          sin.sin_family = AF_INET;
271          sin.sin_addr = *(struct in_addr *)p_host->h_addr_list[0];          sin.sin_addr = *(struct in_addr *)p_host->h_addr_list[0];
272          sin.sin_port = htons(bbsnet_conf[n].port);          sin.sin_port = htons(bbsnet_conf[n].port);
# Line 219  int bbsnet_connect(int n) Line 275  int bbsnet_connect(int n)
275          remote_addr[sizeof(remote_addr) - 1] = '\0';          remote_addr[sizeof(remote_addr) - 1] = '\0';
276          remote_port = ntohs(sin.sin_port);          remote_port = ntohs(sin.sin_port);
277    
278          prints("\033[1;32mʾǰʹõʱ䣬\033[1;33mCtrl+C\033[1;32mжϡ\033[m\r\n");          prints("\033[1;32m穿梭进度条提示您当前已使用的时间,按\033[1;33mCtrl+C\033[1;32m中断。\033[m\r\n");
279          process_bar(0, MAX_PROCESS_BAR_LEN);          process_bar(0, MAX_PROCESS_BAR_LEN);
280    
281          // Set socket as non-blocking          // Set socket as non-blocking
282          flags_sock = fcntl(sock, F_GETFL, 0);          flags_sock = fcntl(sock, F_GETFL, 0);
283          fcntl(sock, F_SETFL, flags_sock | O_NONBLOCK);          fcntl(sock, F_SETFL, flags_sock | O_NONBLOCK);
284    
285          if ((ret = connect(sock, (struct sockaddr *)&sin, sizeof(sin))) < 0)          // Set STDIN/STDOUT as non-blocking
286            flags_stdin = fcntl(STDIN_FILENO, F_GETFL, 0);
287            flags_stdout = fcntl(STDOUT_FILENO, F_GETFL, 0);
288            fcntl(STDIN_FILENO, F_SETFL, flags_stdin | O_NONBLOCK);
289            fcntl(STDOUT_FILENO, F_SETFL, flags_stdout | O_NONBLOCK);
290    
291            epollfd = epoll_create1(0);
292            if (epollfd < 0)
293          {          {
294                  if (errno != EINPROGRESS)                  log_error("epoll_create1() error (%d)\n", errno);
295                  {                  return -1;
                         prints("\033[1;31mʧܣ\033[m\r\n");  
                         press_any_key();  
                         return -1;  
                 }  
296          }          }
297    
298          for (i = 0; i < MAX_PROCESS_BAR_LEN; i++)          ev.events = EPOLLOUT | EPOLLET;
299            ev.data.fd = sock;
300            if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sock, &ev) == -1)
301          {          {
302                  ch = igetch(0); // 0.1 second                  log_error("epoll_ctl(socket) error (%d)\n", errno);
303                  if (ch == Ctrl('C') || SYS_server_exit)                  goto cleanup;
304                  {          }
                         return 0;  
                 }  
   
                 FD_ZERO(&read_fds);  
                 FD_SET(sock, &read_fds);  
305    
306                  FD_ZERO(&write_fds);          ev.events = EPOLLIN | EPOLLET;
307                  FD_SET(sock, &write_fds);          ev.data.fd = STDIN_FILENO;
308            if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1)
309            {
310                    log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno);
311                    goto cleanup;
312            }
313    
314                  timeout.tv_sec = 0;          while (!SYS_server_exit)
315                  timeout.tv_usec = 400 * 1000; // 0.4 second          {
316                    if ((ret = connect(sock, (struct sockaddr *)&sin, sizeof(sin))) < 0)
317                    {
318                            if (errno == EAGAIN || errno == EALREADY || errno == EINPROGRESS)
319                            {
320                                    // Use select / epoll to check writability of the socket,
321                                    // then use getsockopt to check the status of the socket.
322                                    // See man connect(2)
323                                    break;
324                            }
325                            else if (errno == EINTR)
326                            {
327                                    continue;
328                            }
329                            else
330                            {
331                                    log_error("connect(socket) error (%d)\n", errno);
332    
333                  ret = select(sock + 1, &read_fds, &write_fds, NULL, &timeout);                                  prints("\033[1;31m连接失败!\033[m\r\n");
334                                    press_any_key();
335    
336                  if (ret == 0) // Timeout                                  goto cleanup;
337                  {                          }
                         process_bar(i + 1, MAX_PROCESS_BAR_LEN);  
338                  }                  }
339                  else if (ret < 0)          }
340    
341            for (int j = 0; j < MAX_PROCESS_BAR_LEN && !sock_connected && !SYS_server_exit; j++)
342            {
343                    nfds = epoll_wait(epollfd, events, MAX_EVENTS, 500); // 0.5 second
344    
345                    if (nfds < 0)
346                  {                  {
347                          if (errno != EINTR)                          if (errno != EINTR)
348                          {                          {
349                                  log_error("select() error (%d) !\n", errno);                                  log_error("epoll_wait() error (%d)\n", errno);
350                                  return -1;                                  break;
351                          }                          }
352                  }                  }
353                  // ret > 0                  else if (nfds == 0) // timeout
                 else if (FD_ISSET(sock, &read_fds) || FD_ISSET(sock, &write_fds))  
354                  {                  {
355                          len = sizeof(error);                          process_bar(j + 1, MAX_PROCESS_BAR_LEN);
356                          if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, (socklen_t *)&len) < 0)                  }
357                    else // ret > 0
358                    {
359                            for (int i = 0; i < nfds; i++)
360                          {                          {
361                                  log_error("getsockopt() error (%d) !\n", error);                                  if (events[i].data.fd == sock)
362                                  return -1;                                  {
363                                            len = sizeof(error);
364                                            if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, (socklen_t *)&len) < 0)
365                                            {
366                                                    log_error("getsockopt() error (%d) !\n", error);
367                                                    goto cleanup;
368                                            }
369                                            if (error == 0)
370                                            {
371                                                    sock_connected = 1;
372                                            }
373                                    }
374                                    else if (events[i].data.fd == STDIN_FILENO)
375                                    {
376                                            ch = igetch(0);
377                                            if (ch == Ctrl('C') || ch == KEY_ESC)
378                                            {
379                                                    goto cleanup;
380                                            }
381                                    }
382                          }                          }
   
                         break; // connected  
383                  }                  }
384          }          }
385          if (i == MAX_PROCESS_BAR_LEN)          if (SYS_server_exit)
386          {          {
387                  prints("\033[1;31mӳʱ\033[m\r\n");                  goto cleanup;
388            }
389            if (!sock_connected)
390            {
391                    prints("\033[1;31m连接失败!\033[m\r\n");
392                  press_any_key();                  press_any_key();
393                  return -1;  
394                    goto cleanup;
395          }          }
396    
397          tos = IPTOS_LOWDELAY;          tos = IPTOS_LOWDELAY;
# Line 293  int bbsnet_connect(int n) Line 400  int bbsnet_connect(int n)
400                  log_error("setsockopt IP_TOS=%d error (%d)\n", tos, errno);                  log_error("setsockopt IP_TOS=%d error (%d)\n", tos, errno);
401          }          }
402    
403          prints("\033[1;31mӳɹ\033[m\r\n");          sock_len = sizeof(sin);
404            if (getsockname(sock, (struct sockaddr *)&sin, &sock_len) < 0)
405            {
406                    log_error("getsockname() error: %d", errno);
407                    goto cleanup;
408            }
409    
410            strncpy(local_addr, inet_ntoa(sin.sin_addr), sizeof(local_addr) - 1);
411            local_addr[sizeof(local_addr) - 1] = '\0';
412            local_port = ntohs(sin.sin_port);
413    
414            prints("\033[1;31m连接成功!\033[m\r\n");
415          iflush();          iflush();
416          log_std("BBSNET connect to %s:%d\n", remote_addr, remote_port);          log_common("BBSNET connect to %s:%d from %s:%d by [%s]\n",
417                               remote_addr, remote_port, local_addr, local_port, BBS_username);
418    
419          // Set STDIN/STDOUT as non-blocking          snprintf(tocode, sizeof(tocode), "%s%s", bbsnet_conf[n].charset,
420          flags_stdin = fcntl(STDIN_FILENO, F_GETFL, 0);                           (strcasecmp(stdio_charset, bbsnet_conf[n].charset) == 0 ? "" : "//IGNORE"));
421          flags_stdout = fcntl(STDOUT_FILENO, F_GETFL, 0);          input_cd = iconv_open(tocode, stdio_charset);
422          fcntl(STDIN_FILENO, F_SETFL, flags_stdin | O_NONBLOCK);          if (input_cd == (iconv_t)(-1))
423          fcntl(STDOUT_FILENO, F_SETFL, flags_stdout | O_NONBLOCK);          {
424                    log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, tocode, errno);
425                    goto cleanup;
426            }
427    
428          BBS_last_access_tm = t_used = time(0);          snprintf(tocode, sizeof(tocode), "%s%s", stdio_charset,
429          loop = 1;                           (strcasecmp(bbsnet_conf[n].charset, stdio_charset) == 0 ? "" : "//TRANSLIT"));
430            output_cd = iconv_open(tocode, bbsnet_conf[n].charset);
431            if (output_cd == (iconv_t)(-1))
432            {
433                    log_error("iconv_open(%s->%s) error: %d\n", bbsnet_conf[n].charset, tocode, errno);
434                    iconv_close(input_cd);
435                    goto cleanup;
436            }
437    
438          while (loop && !SYS_server_exit)          ev.events = EPOLLIN | EPOLLOUT | EPOLLET;
439            ev.data.fd = sock;
440            if (epoll_ctl(epollfd, EPOLL_CTL_MOD, sock, &ev) == -1)
441            {
442                    log_error("epoll_ctl(socket) error (%d)\n", errno);
443                    goto cleanup;
444            }
445    
446            ev.events = EPOLLOUT | EPOLLET;
447            ev.data.fd = STDOUT_FILENO;
448            if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1)
449          {          {
450                  FD_ZERO(&read_fds);                  log_error("epoll_ctl(STDOUT_FILENO) error (%d)\n", errno);
451                  FD_SET(STDIN_FILENO, &read_fds);                  goto cleanup;
452                  FD_SET(sock, &read_fds);          }
453    
454                  FD_ZERO(&write_fds);          BBS_last_access_tm = t_used = time(NULL);
455                  FD_SET(STDOUT_FILENO, &write_fds);          loop = 1;
                 FD_SET(sock, &write_fds);  
456    
457                  timeout.tv_sec = 0;          while (loop && !SYS_server_exit)
458                  timeout.tv_usec = 100 * 1000; // 0.1 second          {
459                    if (SSH_v2 && ssh_channel_is_closed(SSH_channel))
460                    {
461                            log_error("SSH channel is closed\n");
462                            loop = 0;
463                            break;
464                    }
465    
466                  ret = select(sock + 1, &read_fds, &write_fds, NULL, &timeout);                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second
467    
468                  if (ret == 0) // timeout                  if (nfds < 0)
469                    {
470                            if (errno != EINTR)
471                            {
472                                    log_error("epoll_wait() error (%d)\n", errno);
473                                    break;
474                            }
475                            continue;
476                    }
477                    else if (nfds == 0) // timeout
478                  {                  {
479                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time)
480                          {                          {
481                                  loop = 0;                                  break;
482                          }                          }
483                  }                  }
484                  else if (ret < 0)  
485                    for (int i = 0; i < nfds; i++)
486                  {                  {
487                          if (errno != EINTR)                          if (events[i].data.fd == STDIN_FILENO)
488                            {
489                                    stdin_read_wait = 1;
490                            }
491    
492                            if (events[i].data.fd == sock)
493                            {
494                                    if (events[i].events & EPOLLIN)
495                                    {
496                                            sock_read_wait = 1;
497                                    }
498                                    if (events[i].events & EPOLLOUT)
499                                    {
500                                            sock_write_wait = 1;
501                                    }
502                            }
503    
504                            if (events[i].data.fd == STDOUT_FILENO)
505                          {                          {
506                                  log_error("select() error (%d) !\n", errno);                                  stdout_write_wait = 1;
                                 loop = 0;  
507                          }                          }
508                  }                  }
509                  else if (ret > 0)  
510                    if (stdin_read_wait)
511                  {                  {
512                          if ((input_buf_offset >= input_buf_len) && FD_ISSET(STDIN_FILENO, &read_fds))                          while (input_buf_len < sizeof(input_buf) && !SYS_server_exit)
513                          {                          {
514                                  ret = (int)read(STDIN_FILENO, input_buf, sizeof(input_buf));                                  if (SSH_v2)
515                                    {
516                                            ret = ssh_channel_read_nonblocking(SSH_channel, input_buf + input_buf_len, sizeof(input_buf) - (uint32_t)input_buf_len, 0);
517                                            if (ret == SSH_ERROR)
518                                            {
519                                                    log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session));
520                                                    loop = 0;
521                                                    break;
522                                            }
523                                            else if (ret == SSH_EOF)
524                                            {
525                                                    stdin_read_wait = 0;
526                                                    loop = 0;
527                                                    break;
528                                            }
529                                            else if (ret == 0)
530                                            {
531                                                    // Send NO-OP to remote server
532                                                    input_buf[input_buf_len] = '\0';
533                                                    input_buf_len++;
534                                                    BBS_last_access_tm = time(NULL);
535    
536                                                    stdin_read_wait = 0;
537                                                    break; // Check whether channel is still open
538                                            }
539                                    }
540                                    else
541                                    {
542                                            ret = (int)read(STDIN_FILENO, input_buf + input_buf_len, sizeof(input_buf) - (size_t)input_buf_len);
543                                    }
544                                  if (ret < 0)                                  if (ret < 0)
545                                  {                                  {
546                                          if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)                                          if (errno == EAGAIN || errno == EWOULDBLOCK)
547                                            {
548                                                    stdin_read_wait = 0;
549                                                    break;
550                                            }
551                                            else if (errno == EINTR)
552                                            {
553                                                    continue;
554                                            }
555                                            else
556                                          {                                          {
557                                                  log_error("read(STDIN) error (%d)\n", errno);                                                  log_error("read(STDIN) error (%d)\n", errno);
558                                                  loop = 0;                                                  loop = 0;
559                                                    break;
560                                          }                                          }
561                                  }                                  }
562                                  else if (ret == 0) // broken pipe                                  else if (ret == 0) // broken pipe
563                                  {                                  {
564    #ifdef _DEBUG
565                                            log_error("read(STDIN) EOF\n");
566    #endif
567                                            stdin_read_wait = 0;
568                                          loop = 0;                                          loop = 0;
569                                            break;
570                                  }                                  }
571                                  else                                  else
572                                  {                                  {
573                                          input_buf_len = ret;                                          input_buf_len += ret;
574                                          input_buf_offset = 0;                                          BBS_last_access_tm = time(NULL);
575    
576                                          BBS_last_access_tm = time(0);                                          // Refresh current action while user input
577                                            if (user_online_update("BBS_NET") < 0)
578                                            {
579                                                    log_error("user_online_update(BBS_NET) error\n");
580                                            }
581    
582                                            continue;
583                                  }                                  }
584                          }                          }
585                    }
586    
587                          if ((input_buf_offset < input_buf_len) && FD_ISSET(sock, &write_fds))                  if (sock_write_wait)
588                    {
589                            if (input_buf_offset < input_buf_len)
590                          {                          {
591                                  ret = (int)write(sock, input_buf + input_buf_offset, (size_t)(input_buf_len - input_buf_offset));                                  // For debug
592    #ifdef _DEBUG
593                                    for (int j = input_buf_offset; j < input_buf_len; j++)
594                                    {
595                                            log_error("Debug input: <--[%u]\n", (input_buf[j] + 256) % 256);
596                                    }
597    #endif
598    
599                                    ret = io_buf_conv(input_cd, input_buf, &input_buf_len, &input_buf_offset, input_conv, sizeof(input_conv), &input_conv_len);
600                                  if (ret < 0)                                  if (ret < 0)
601                                  {                                  {
602                                          if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)                                          log_error("io_buf_conv(input, %d, %d, %d) error\n", input_buf_len, input_buf_offset, input_conv_len);
603                                            input_buf_len = input_buf_offset; // Discard invalid sequence
604                                    }
605    
606                                    // For debug
607    #ifdef _DEBUG
608                                    for (int j = input_conv_offset; j < input_conv_len; j++)
609                                    {
610                                            log_error("Debug input_conv: <--[%u]\n", (input_conv[j] + 256) % 256);
611                                    }
612    #endif
613                            }
614    
615                            while (input_conv_offset < input_conv_len && !SYS_server_exit)
616                            {
617                                    ret = (int)write(sock, input_conv + input_conv_offset, (size_t)(input_conv_len - input_conv_offset));
618                                    if (ret < 0)
619                                    {
620                                            if (errno == EAGAIN || errno == EWOULDBLOCK)
621                                            {
622                                                    sock_write_wait = 0;
623                                                    break;
624                                            }
625                                            else if (errno == EINTR)
626                                            {
627                                                    continue;
628                                            }
629                                            else
630                                          {                                          {
631                                                  log_error("write(socket) error (%d)\n", errno);                                                  log_error("write(socket) error (%d)\n", errno);
632                                                  loop = 0;                                                  loop = 0;
633                                                    break;
634                                          }                                          }
635                                  }                                  }
636                                  else if (ret == 0) // broken pipe                                  else if (ret == 0) // broken pipe
637                                  {                                  {
638    #ifdef _DEBUG
639                                            log_error("write(socket) EOF\n");
640    #endif
641                                            sock_write_wait = 0;
642                                          loop = 0;                                          loop = 0;
643                                            break;
644                                  }                                  }
645                                  else                                  else
646                                  {                                  {
647                                          input_buf_offset += ret;                                          input_conv_offset += ret;
648                                            if (input_conv_offset >= input_conv_len) // Output buffer complete
649                                            {
650                                                    input_conv_offset = 0;
651                                                    input_conv_len = 0;
652                                                    break;
653                                            }
654                                            continue;
655                                  }                                  }
656                          }                          }
657                    }
658    
659                          if ((output_buf_offset >= output_buf_len) && FD_ISSET(sock, &read_fds))                  if (sock_read_wait)
660                    {
661                            while (output_buf_len < sizeof(output_buf) && !SYS_server_exit)
662                          {                          {
663                                  ret = (int)read(sock, output_buf, sizeof(output_buf));                                  ret = (int)read(sock, output_buf + output_buf_len, sizeof(output_buf) - (size_t)output_buf_len);
664                                  if (ret < 0)                                  if (ret < 0)
665                                  {                                  {
666                                          if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)                                          if (errno == EAGAIN || errno == EWOULDBLOCK)
667                                            {
668                                                    sock_read_wait = 0;
669                                                    break;
670                                            }
671                                            else if (errno == EINTR)
672                                            {
673                                                    continue;
674                                            }
675                                            else
676                                          {                                          {
677                                                  log_error("read(socket) error (%d)\n", errno);                                                  log_error("read(socket) error (%d)\n", errno);
678                                                  loop = 0;                                                  loop = 0;
679                                                    break;
680                                          }                                          }
681                                  }                                  }
682                                  else if (ret == 0) // broken pipe                                  else if (ret == 0) // broken pipe
683                                  {                                  {
684    #ifdef _DEBUG
685                                            log_error("read(socket) EOF\n");
686    #endif
687                                            sock_read_wait = 0;
688                                          loop = 0;                                          loop = 0;
689                                            break;
690                                  }                                  }
691                                  else                                  else
692                                  {                                  {
693                                          output_buf_len = ret;                                          output_buf_len += ret;
694                                          output_buf_offset = 0;                                          continue;
695                                  }                                  }
696                          }                          }
697                    }
698    
699                          if ((output_buf_offset < output_buf_len) && FD_ISSET(STDOUT_FILENO, &write_fds))                  if (stdout_write_wait)
700                    {
701                            if (output_buf_offset < output_buf_len)
702                          {                          {
703                                  ret = (int)write(STDOUT_FILENO, output_buf + output_buf_offset, (size_t)(output_buf_len - output_buf_offset));                                  ret = io_buf_conv(output_cd, output_buf, &output_buf_len, &output_buf_offset, output_conv, sizeof(output_conv), &output_conv_len);
704                                    if (ret < 0)
705                                    {
706                                            log_error("io_buf_conv(output, %d, %d, %d) error\n", output_buf_len, output_buf_offset, output_conv_len);
707                                            output_buf_len = output_buf_offset; // Discard invalid sequence
708                                    }
709                            }
710    
711                            while (output_conv_offset < output_conv_len && !SYS_server_exit)
712                            {
713                                    if (SSH_v2)
714                                    {
715                                            ret = ssh_channel_write(SSH_channel, output_conv + output_conv_offset, (uint32_t)(output_conv_len - output_conv_offset));
716                                            if (ret == SSH_ERROR)
717                                            {
718                                                    log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session));
719                                                    loop = 0;
720                                                    break;
721                                            }
722                                    }
723                                    else
724                                    {
725                                            ret = (int)write(STDOUT_FILENO, output_conv + output_conv_offset, (size_t)(output_conv_len - output_conv_offset));
726                                    }
727                                  if (ret < 0)                                  if (ret < 0)
728                                  {                                  {
729                                          if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)                                          if (errno == EAGAIN || errno == EWOULDBLOCK)
730                                            {
731                                                    stdout_write_wait = 0;
732                                                    break;
733                                            }
734                                            else if (errno == EINTR)
735                                            {
736                                                    continue;
737                                            }
738                                            else
739                                          {                                          {
740                                                  log_error("write(STDOUT) error (%d)\n", errno);                                                  log_error("write(STDOUT) error (%d)\n", errno);
741                                                  loop = 0;                                                  loop = 0;
742                                                    break;
743                                          }                                          }
744                                  }                                  }
745                                  else if (ret == 0) // broken pipe                                  else if (ret == 0) // broken pipe
746                                  {                                  {
747    #ifdef _DEBUG
748                                            log_error("write(STDOUT) EOF\n");
749    #endif
750                                            stdout_write_wait = 0;
751                                          loop = 0;                                          loop = 0;
752                                            break;
753                                  }                                  }
754                                  else                                  else
755                                  {                                  {
756                                          output_buf_offset += ret;                                          output_conv_offset += ret;
757                                            if (output_conv_offset >= output_conv_len) // Output buffer complete
758                                            {
759                                                    output_conv_offset = 0;
760                                                    output_conv_len = 0;
761                                                    break;
762                                            }
763                                            continue;
764                                  }                                  }
765                          }                          }
766                  }                  }
767          }          }
768    
769            iconv_close(input_cd);
770            iconv_close(output_cd);
771    
772    cleanup:
773            if (close(epollfd) < 0)
774            {
775                    log_error("close(epoll) error (%d)\n");
776            }
777    
778          // Restore STDIN/STDOUT flags          // Restore STDIN/STDOUT flags
779          fcntl(STDIN_FILENO, F_SETFL, flags_stdin);          fcntl(STDIN_FILENO, F_SETFL, flags_stdin);
780          fcntl(STDOUT_FILENO, F_SETFL, flags_stdout);          fcntl(STDOUT_FILENO, F_SETFL, flags_stdout);
# Line 440  int bbsnet_connect(int n) Line 787  int bbsnet_connect(int n)
787                  log_error("Close socket failed\n");                  log_error("Close socket failed\n");
788          }          }
789    
790          t_used = time(0) - t_used;          t_used = time(NULL) - t_used;
791          tm_used = gmtime(&t_used);          tm_used = gmtime(&t_used);
792    
793          log_std("BBSNET disconnect, %d days %d hours %d minutes %d seconds used\n",          log_common("BBSNET disconnect, %d days %d hours %d minutes %d seconds used\n",
794                          tm_used->tm_mday - 1, tm_used->tm_hour, tm_used->tm_min,                             tm_used->tm_mday - 1, tm_used->tm_hour, tm_used->tm_min,
795                          tm_used->tm_sec);                             tm_used->tm_sec);
796    
797            BBS_last_access_tm = time(NULL);
798    
799          return 0;          return 0;
800  }  }
801    
802  static int  static int bbsnet_refresh()
 bbsnet_refresh()  
803  {  {
804          clearscr();          clearscr();
805          moveto(1, 0);          moveto(1, 0);
# Line 468  bbsnet_refresh() Line 816  bbsnet_refresh()
816          moveto(22, 0);          moveto(22, 0);
817          prints(" ----------------------------------------------------------------------------- ");          prints(" ----------------------------------------------------------------------------- ");
818          moveto(23, 0);          moveto(23, 0);
819          prints(" [\x1b[1;32mCtrl+C\x1b[m]˳");          prints(" [\x1b[1;32mCtrl+C\x1b[m]退出");
820    
821          iflush();          iflush();
822    
823          return 0;          return 0;
824  }  }
825    
826  int bbsnet_selchange(int new_pos)  static int bbsnet_selchange()
827  {  {
828            int i = bbsnet_menu.menu_item_pos[0];
829    
830          moveto(20, 0);          moveto(20, 0);
831          clrtoeol();          clrtoeol();
832          prints("|\x1b[1mλ:\x1b[1;33m%-18s\x1b[m  վ:\x1b[1;33m%s\x1b[m",          prints("|\x1b[1m单位:\x1b[1;33m%-18s\x1b[m  站名:\x1b[1;33m%s\x1b[m",
833                     bbsnet_conf[new_pos].host2, bbsnet_conf[new_pos].host1);                     bbsnet_conf[i].host2, bbsnet_conf[i].host1);
834          moveto(20, 79);          moveto(20, 79);
835          prints("|");          prints("|");
836          moveto(21, 0);          moveto(21, 0);
837          clrtoeol();          clrtoeol();
838          prints("|\x1b[1m:\x1b[1;33m%-20s", bbsnet_conf[new_pos].ip);          prints("|\x1b[1m连往:\x1b[1;33m%-20s", bbsnet_conf[i].ip);
839          if (bbsnet_conf[new_pos].port != 23)          if (bbsnet_conf[i].port != 23)
840          {          {
841                  prints("  %d", bbsnet_conf[new_pos].port);                  prints("  %d", bbsnet_conf[i].port);
842          }          }
843          prints("\x1b[m");          prints("\x1b[m");
844          moveto(21, 79);          moveto(21, 79);
# Line 498  int bbsnet_selchange(int new_pos) Line 848  int bbsnet_selchange(int new_pos)
848          return 0;          return 0;
849  }  }
850    
851  int bbs_net()  extern int bbs_net()
852  {  {
853          int ch, pos, i;          int ch, i;
854    
855          load_bbsnet_conf(CONF_BBSNET);          load_bbsnet_conf(CONF_BBSNET);
856    
         BBS_last_access_tm = time(0);  
   
857          clearscr();          clearscr();
858          bbsnet_refresh();          bbsnet_refresh();
859          pos = bbsnet_menu.p_menu[0]->item_cur_pos;          display_menu(&bbsnet_menu);
860          display_menu(get_menu(&bbsnet_menu, "BBSNET"));          bbsnet_selchange();
         bbsnet_selchange(pos);  
861    
862          while (!SYS_server_exit)          while (!SYS_server_exit)
863          {          {
864                  ch = igetch(0);                  ch = igetch(100);
865    
866                    if (ch != KEY_NULL && ch != KEY_TIMEOUT)
867                    {
868                            BBS_last_access_tm = time(NULL);
869                    }
870    
871                  switch (ch)                  switch (ch)
872                  {                  {
873                  case KEY_NULL:  // broken pipe                  case KEY_NULL: // broken pipe
874                  case Ctrl('C'): // user cancel                          log_error("KEY_NULL\n");
875                          return 0;                          goto cleanup;
876                  case KEY_TIMEOUT:                  case KEY_TIMEOUT:
877                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time)
878                          {                          {
879                                  return 0;                                  log_error("User input timeout\n");
880                                    goto cleanup;
881                          }                          }
882                          continue;                          continue;
883                    case KEY_ESC:
884                    case Ctrl('C'): // user cancel
885                            goto cleanup;
886                  case CR:                  case CR:
887                          pos = bbsnet_menu.p_menu[0]->item_cur_pos;                          bbsnet_connect(bbsnet_menu.menu_item_pos[0]);
                         bbsnet_connect(pos);  
888                          bbsnet_refresh();                          bbsnet_refresh();
889                          display_current_menu(&bbsnet_menu);                          display_menu(&bbsnet_menu);
890                          bbsnet_selchange(pos);                          bbsnet_selchange();
891                          break;                          break;
892                  case KEY_UP:                  case KEY_UP:
893                          for (i = 0; i < STATION_PER_LINE; i++)                          for (i = 0; i < STATION_PER_LINE; i++)
894                          {                          {
895                                  menu_control(&bbsnet_menu, KEY_UP);                                  menu_control(&bbsnet_menu, KEY_UP);
896                          }                          }
897                          pos = bbsnet_menu.p_menu[0]->item_cur_pos;                          bbsnet_selchange();
                         bbsnet_selchange(pos);  
898                          break;                          break;
899                  case KEY_DOWN:                  case KEY_DOWN:
900                          for (i = 0; i < STATION_PER_LINE; i++)                          for (i = 0; i < STATION_PER_LINE; i++)
901                          {                          {
902                                  menu_control(&bbsnet_menu, KEY_DOWN);                                  menu_control(&bbsnet_menu, KEY_DOWN);
903                          }                          }
904                          pos = bbsnet_menu.p_menu[0]->item_cur_pos;                          bbsnet_selchange();
                         bbsnet_selchange(pos);  
905                          break;                          break;
906                  case KEY_LEFT:                  case KEY_LEFT:
907                          menu_control(&bbsnet_menu, KEY_UP);                          menu_control(&bbsnet_menu, KEY_UP);
908                          pos = bbsnet_menu.p_menu[0]->item_cur_pos;                          bbsnet_selchange();
                         bbsnet_selchange(pos);  
909                          break;                          break;
910                  case KEY_RIGHT:                  case KEY_RIGHT:
911                          menu_control(&bbsnet_menu, KEY_DOWN);                          menu_control(&bbsnet_menu, KEY_DOWN);
912                          pos = bbsnet_menu.p_menu[0]->item_cur_pos;                          bbsnet_selchange();
913                          bbsnet_selchange(pos);                          break;
914                    case KEY_HOME:
915                    case KEY_PGUP:
916                            menu_control(&bbsnet_menu, KEY_PGUP);
917                            bbsnet_selchange();
918                            break;
919                    case KEY_END:
920                    case KEY_PGDN:
921                            menu_control(&bbsnet_menu, KEY_PGDN);
922                            bbsnet_selchange();
923                          break;                          break;
924                  default:                  default:
925                          menu_control(&bbsnet_menu, ch);                          menu_control(&bbsnet_menu, ch);
926                          pos = bbsnet_menu.p_menu[0]->item_cur_pos;                          bbsnet_selchange();
                         bbsnet_selchange(pos);  
927                          break;                          break;
928                  }                  }
                 BBS_last_access_tm = time(0);  
929          }          }
930    
931    cleanup:
932            unload_bbsnet_conf();
933    
934          return 0;          return 0;
935  }  }


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

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