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


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

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