/[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.50 by sysadm, Tue Jun 17 02:06:48 2025 UTC Revision 1.71 by sysadm, Mon Oct 20 01:55:50 2025 UTC
# Line 14  Line 14 
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
 #define _POSIX_C_SOURCE 200809L  
   
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>
 #include <netdb.h>  
 #include <sys/select.h>  
 #include <sys/ioctl.h>  
 #include <sys/socket.h>  
 #include <sys/epoll.h>  
 #include <netinet/in.h>  
 #include <netinet/ip.h>  
33  #include <arpa/inet.h>  #include <arpa/inet.h>
34  #include <libssh/libssh.h>  #include <libssh/libssh.h>
35  #include <libssh/server.h>  #include <libssh/server.h>
36  #include <libssh/callbacks.h>  #include <libssh/callbacks.h>
37    #include <netinet/in.h>
38    #include <netinet/ip.h>
39    #include <sys/select.h>
40    #include <sys/ioctl.h>
41    #include <sys/socket.h>
42    #include <sys/epoll.h>
43    
44  #define MENU_CONF_DELIM " \t\r\n"  #define MENU_CONF_DELIM " \t\r\n"
45    
# Line 54  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 64  int load_bbsnet_conf(const char *file_co Line 64  int load_bbsnet_conf(const char *file_co
64          MENU *p_menu;          MENU *p_menu;
65          MENU_ITEM *p_menu_item;          MENU_ITEM *p_menu_item;
66          MENU_ITEM_ID menu_item_id;          MENU_ITEM_ID menu_item_id;
67          char t[256], *t1, *t2, *t3, *t4, *saveptr;          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 102  int load_bbsnet_conf(const char *file_co Line 102  int load_bbsnet_conf(const char *file_co
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                  }                  }
# Line 115  int load_bbsnet_conf(const char *file_co Line 116  int load_bbsnet_conf(const char *file_co
116                  strncpy(bbsnet_conf[menu_item_id].ip, t3, sizeof(bbsnet_conf[menu_item_id].ip) - 1);                  strncpy(bbsnet_conf[menu_item_id].ip, t3, sizeof(bbsnet_conf[menu_item_id].ip) - 1);
117                  bbsnet_conf[menu_item_id].ip[sizeof(bbsnet_conf[menu_item_id].ip) - 1] = '\0';                  bbsnet_conf[menu_item_id].ip[sizeof(bbsnet_conf[menu_item_id].ip) - 1] = '\0';
118                  bbsnet_conf[menu_item_id].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                    bbsnet_conf[menu_item_id].charset[sizeof(bbsnet_conf[menu_item_id].charset) - 1] = '\0';
121    
122                  p_menu_item = get_menu_item_by_id(&bbsnet_menu, menu_item_id);                  p_menu_item = get_menu_item_by_id(&bbsnet_menu, menu_item_id);
123                  if (p_menu_item == NULL)                  if (p_menu_item == NULL)
# Line 204  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            char input_conv[LINE_BUFFER_LEN * 2];
211            char output_conv[LINE_BUFFER_LEN * 2];
212            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];          struct epoll_event ev, events[MAX_EVENTS];
220          int nfds, epollfd;          int nfds, epollfd;
221          int stdin_read_wait = 0;          int stdin_read_wait = 0;
# Line 214  int bbsnet_connect(int n) Line 226  int bbsnet_connect(int n)
226          int tos;          int tos;
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姝e湪娴嬭瘯寰 %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 229  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 238  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          }          }
# Line 263  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
# Line 283  int bbsnet_connect(int n) Line 303  int bbsnet_connect(int n)
303                  return -1;                  return -1;
304          }          }
305    
306          ev.events = EPOLLOUT;          ev.events = EPOLLOUT | EPOLLET;
307          ev.data.fd = sock;          ev.data.fd = sock;
308          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sock, &ev) == -1)          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sock, &ev) == -1)
309          {          {
# Line 291  int bbsnet_connect(int n) Line 311  int bbsnet_connect(int n)
311                  goto cleanup;                  goto cleanup;
312          }          }
313    
314          ev.events = EPOLLIN;          ev.events = EPOLLIN | EPOLLET;
315          ev.data.fd = STDIN_FILENO;          ev.data.fd = STDIN_FILENO;
316          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1)          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1)
317          {          {
# Line 318  int bbsnet_connect(int n) Line 338  int bbsnet_connect(int n)
338                          {                          {
339                                  log_error("connect(socket) error (%d)\n", errno);                                  log_error("connect(socket) error (%d)\n", errno);
340    
341                                  prints("\033[1;31m连接失败!\033[m\r\n");                                  prints("\033[1;31m杩炴帴澶辫触锛乗033[m\r\n");
342                                  press_any_key();                                  press_any_key();
343    
344                                  goto cleanup;                                  goto cleanup;
# Line 376  int bbsnet_connect(int n) Line 396  int bbsnet_connect(int n)
396          }          }
397          if (!sock_connected)          if (!sock_connected)
398          {          {
399                  prints("\033[1;31m连接失败!\033[m\r\n");                  prints("\033[1;31m杩炴帴澶辫触锛乗033[m\r\n");
400                  press_any_key();                  press_any_key();
401    
402                  goto cleanup;                  goto cleanup;
# Line 388  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_common("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            snprintf(tocode, sizeof(tocode), "%s%s", bbsnet_conf[n].charset,
428                             (strcasecmp(stdio_charset, bbsnet_conf[n].charset) == 0 ? "" : "//IGNORE"));
429            input_cd = iconv_open(tocode, stdio_charset);
430            if (input_cd == (iconv_t)(-1))
431            {
432                    log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, tocode, errno);
433                    goto cleanup;
434            }
435    
436            snprintf(tocode, sizeof(tocode), "%s%s", stdio_charset,
437                             (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          ev.events = EPOLLIN | EPOLLOUT | EPOLLET;          ev.events = EPOLLIN | EPOLLOUT | EPOLLET;
447          ev.data.fd = sock;          ev.data.fd = sock;
# Line 400  int bbsnet_connect(int n) Line 451  int bbsnet_connect(int n)
451                  goto cleanup;                  goto cleanup;
452          }          }
453    
454          ev.events = EPOLLOUT;          ev.events = EPOLLOUT | EPOLLET;
455          ev.data.fd = STDOUT_FILENO;          ev.data.fd = STDOUT_FILENO;
456          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1)          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1)
457          {          {
# Line 437  int bbsnet_connect(int n) Line 488  int bbsnet_connect(int n)
488                          {                          {
489                                  break;                                  break;
490                          }                          }
                         continue;  
491                  }                  }
492    
493                  for (int i = 0; i < nfds; i++)                  for (int i = 0; i < nfds; i++)
494                  {                  {
495                          if (events[i].data.fd == STDIN_FILENO || stdin_read_wait)                          if (events[i].data.fd == STDIN_FILENO)
496                          {                          {
497                                  stdin_read_wait = 1;                                  stdin_read_wait = 1;
498                                  while (input_buf_len < sizeof(input_buf) && !SYS_server_exit)                          }
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                                    stdout_write_wait = 1;
515                            }
516                    }
517    
518                    if (stdin_read_wait)
519                    {
520                            while (input_buf_len < sizeof(input_buf) && !SYS_server_exit)
521                            {
522                                    if (SSH_v2)
523                                  {                                  {
524                                          if (SSH_v2)                                          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                                                  ret = ssh_channel_read_nonblocking(SSH_channel, input_buf + input_buf_len, sizeof(input_buf) - (uint32_t)input_buf_len, 0);                                                  log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session));
528                                                  if (ret == SSH_ERROR)                                                  loop = 0;
529                                                  {                                                  break;
                                                         log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session));  
                                                         loop = 0;  
                                                         break;  
                                                 }  
                                                 else if (ret == SSH_EOF)  
                                                 {  
                                                         stdin_read_wait = 0;  
                                                         loop = 0;  
                                                         break;  
                                                 }  
                                                 else if (ret == 0)  
                                                 {  
                                                         stdin_read_wait = 0;  
                                                         break; // Check whether channel is still open  
                                                 }  
530                                          }                                          }
531                                          else                                          else if (ret == SSH_EOF)
532                                          {                                          {
533                                                  ret = (int)read(STDIN_FILENO, input_buf + input_buf_len, sizeof(input_buf) - (size_t)input_buf_len);                                                  stdin_read_wait = 0;
534                                                    loop = 0;
535                                                    break;
536                                          }                                          }
537                                          if (ret < 0)                                          else if (ret == 0)
538                                          {                                          {
539                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)                                                  stdin_read_wait = 0;
540                                                  {                                                  break; // Check whether channel is still open
                                                         stdin_read_wait = 0;  
                                                         break;  
                                                 }  
                                                 else if (errno == EINTR)  
                                                 {  
                                                         continue;  
                                                 }  
                                                 else  
                                                 {  
                                                         log_error("read(STDIN) error (%d)\n", errno);  
                                                         loop = 0;  
                                                         break;  
                                                 }  
541                                          }                                          }
542                                          else if (ret == 0) // broken pipe                                  }
543                                    else
544                                    {
545                                            ret = (int)read(STDIN_FILENO, input_buf + input_buf_len, sizeof(input_buf) - (size_t)input_buf_len);
546                                    }
547                                    if (ret < 0)
548                                    {
549                                            if (errno == EAGAIN || errno == EWOULDBLOCK)
550                                          {                                          {
                                                 log_common("read(STDIN) EOF\n");  
551                                                  stdin_read_wait = 0;                                                  stdin_read_wait = 0;
                                                 loop = 0;  
552                                                  break;                                                  break;
553                                          }                                          }
554                                          else                                          else if (errno == EINTR)
555                                          {                                          {
                                                 input_buf_len += ret;  
                                                 BBS_last_access_tm = time(NULL);  
556                                                  continue;                                                  continue;
557                                          }                                          }
558                                            else
559                                            {
560                                                    log_error("read(STDIN) error (%d)\n", errno);
561                                                    loop = 0;
562                                                    break;
563                                            }
564                                    }
565                                    else if (ret == 0) // broken pipe
566                                    {
567    #ifdef _DEBUG
568                                            log_error("read(STDIN) EOF\n");
569    #endif
570                                            stdin_read_wait = 0;
571                                            loop = 0;
572                                            break;
573                                    }
574                                    else
575                                    {
576                                            input_buf_len += ret;
577                                            BBS_last_access_tm = time(NULL);
578    
579                                            // Refresh current action while user input
580                                            if (user_online_update("BBS_NET") < 0)
581                                            {
582                                                    log_error("user_online_update(BBS_NET) error\n");
583                                            }
584    
585                                            continue;
586                                    }
587                            }
588                    }
589    
590                    if (sock_write_wait)
591                    {
592                            if (input_buf_offset < input_buf_len)
593                            {
594                                    ret = io_buf_conv(input_cd, input_buf, &input_buf_len, &input_buf_offset, input_conv, sizeof(input_conv), &input_conv_len);
595                                    if (ret < 0)
596                                    {
597                                            log_error("io_buf_conv(input, %d, %d, %d) error\n", input_buf_len, input_buf_offset, input_conv_len);
598                                            input_buf_len = input_buf_offset; // Discard invalid sequence
599                                  }                                  }
600                          }                          }
601    
602                          if (events[i].data.fd == sock || sock_write_wait) // EPOLLOUT                          while (input_conv_offset < input_conv_len && !SYS_server_exit)
603                          {                          {
604                                  sock_write_wait = 1;                                  ret = (int)write(sock, input_conv + input_conv_offset, (size_t)(input_conv_len - input_conv_offset));
605                                  while (input_buf_offset < input_buf_len && !SYS_server_exit)                                  if (ret < 0)
606                                  {                                  {
607                                          ret = (int)write(sock, input_buf + input_buf_offset, (size_t)(input_buf_len - input_buf_offset));                                          if (errno == EAGAIN || errno == EWOULDBLOCK)
608                                          if (ret < 0)                                          {
609                                                    sock_write_wait = 0;
610                                                    break;
611                                            }
612                                            else if (errno == EINTR)
613                                          {                                          {
614                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)                                                  continue;
                                                 {  
                                                         sock_write_wait = 0;  
                                                         break;  
                                                 }  
                                                 else if (errno == EINTR)  
                                                 {  
                                                         continue;  
                                                 }  
                                                 else  
                                                 {  
                                                         log_error("write(socket) error (%d)\n", errno);  
                                                         loop = 0;  
                                                         break;  
                                                 }  
615                                          }                                          }
616                                          else if (ret == 0) // broken pipe                                          else
617                                          {                                          {
618                                                  log_common("write(socket) EOF\n");                                                  log_error("write(socket) error (%d)\n", errno);
                                                 sock_write_wait = 0;  
619                                                  loop = 0;                                                  loop = 0;
620                                                  break;                                                  break;
621                                          }                                          }
622                                          else                                  }
623                                    else if (ret == 0) // broken pipe
624                                    {
625    #ifdef _DEBUG
626                                            log_error("write(socket) EOF\n");
627    #endif
628                                            sock_write_wait = 0;
629                                            loop = 0;
630                                            break;
631                                    }
632                                    else
633                                    {
634                                            input_conv_offset += ret;
635                                            if (input_conv_offset >= input_conv_len) // Output buffer complete
636                                          {                                          {
637                                                  input_buf_offset += ret;                                                  input_conv_offset = 0;
638                                                  if (input_buf_offset >= input_buf_len) // Output buffer complete                                                  input_conv_len = 0;
639                                                  {                                                  break;
                                                         input_buf_offset = 0;  
                                                         input_buf_len = 0;  
                                                         break;  
                                                 }  
                                                 continue;  
640                                          }                                          }
641                                            continue;
642                                  }                                  }
643                          }                          }
644                    }
645    
646                          if (events[i].data.fd == sock || sock_read_wait) // EPOLLIN                  if (sock_read_wait)
647                    {
648                            while (output_buf_len < sizeof(output_buf) && !SYS_server_exit)
649                          {                          {
650                                  sock_read_wait = 1;                                  ret = (int)read(sock, output_buf + output_buf_len, sizeof(output_buf) - (size_t)output_buf_len);
651                                  while (output_buf_len < sizeof(output_buf) && !SYS_server_exit)                                  if (ret < 0)
652                                  {                                  {
653                                          ret = (int)read(sock, output_buf + output_buf_len, sizeof(output_buf) - (size_t)output_buf_len);                                          if (errno == EAGAIN || errno == EWOULDBLOCK)
                                         if (ret < 0)  
                                         {  
                                                 if (errno == EAGAIN || errno == EWOULDBLOCK)  
                                                 {  
                                                         sock_read_wait = 0;  
                                                         break;  
                                                 }  
                                                 else if (errno == EINTR)  
                                                 {  
                                                         continue;  
                                                 }  
                                                 else  
                                                 {  
                                                         log_error("read(socket) error (%d)\n", errno);  
                                                         loop = 0;  
                                                         break;  
                                                 }  
                                         }  
                                         else if (ret == 0) // broken pipe  
654                                          {                                          {
                                                 log_common("read(socket) EOF\n");  
655                                                  sock_read_wait = 0;                                                  sock_read_wait = 0;
                                                 loop = 0;  
656                                                  break;                                                  break;
657                                          }                                          }
658                                          else                                          else if (errno == EINTR)
659                                          {                                          {
                                                 output_buf_len += ret;  
660                                                  continue;                                                  continue;
661                                          }                                          }
662                                            else
663                                            {
664                                                    log_error("read(socket) error (%d)\n", errno);
665                                                    loop = 0;
666                                                    break;
667                                            }
668                                    }
669                                    else if (ret == 0) // broken pipe
670                                    {
671    #ifdef _DEBUG
672                                            log_error("read(socket) EOF\n");
673    #endif
674                                            sock_read_wait = 0;
675                                            loop = 0;
676                                            break;
677                                    }
678                                    else
679                                    {
680                                            output_buf_len += ret;
681                                            continue;
682                                  }                                  }
683                          }                          }
684                    }
685    
686                          if (events[i].data.fd == STDOUT_FILENO || stdout_write_wait)                  if (stdout_write_wait)
687                    {
688                            if (output_buf_offset < output_buf_len)
689                          {                          {
690                                  stdout_write_wait = 1;                                  ret = io_buf_conv(output_cd, output_buf, &output_buf_len, &output_buf_offset, output_conv, sizeof(output_conv), &output_conv_len);
691                                  while (output_buf_offset < output_buf_len && !SYS_server_exit)                                  if (ret < 0)
692                                    {
693                                            log_error("io_buf_conv(output, %d, %d, %d) error\n", output_buf_len, output_buf_offset, output_conv_len);
694                                            output_buf_len = output_buf_offset; // Discard invalid sequence
695                                    }
696                            }
697    
698                            while (output_conv_offset < output_conv_len && !SYS_server_exit)
699                            {
700                                    if (SSH_v2)
701                                  {                                  {
702                                          if (SSH_v2)                                          ret = ssh_channel_write(SSH_channel, output_conv + output_conv_offset, (uint32_t)(output_conv_len - output_conv_offset));
703                                            if (ret == SSH_ERROR)
704                                          {                                          {
705                                                  ret = ssh_channel_write(SSH_channel, output_buf + output_buf_offset, (uint32_t)(output_buf_len - output_buf_offset));                                                  log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session));
706                                                  if (ret == SSH_ERROR)                                                  loop = 0;
707                                                  {                                                  break;
                                                         log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session));  
                                                         loop = 0;  
                                                         break;  
                                                 }  
708                                          }                                          }
709                                          else                                  }
710                                    else
711                                    {
712                                            ret = (int)write(STDOUT_FILENO, output_conv + output_conv_offset, (size_t)(output_conv_len - output_conv_offset));
713                                    }
714                                    if (ret < 0)
715                                    {
716                                            if (errno == EAGAIN || errno == EWOULDBLOCK)
717                                          {                                          {
718                                                  ret = (int)write(STDOUT_FILENO, output_buf + output_buf_offset, (size_t)(output_buf_len - output_buf_offset));                                                  stdout_write_wait = 0;
719                                                    break;
720                                          }                                          }
721                                          if (ret < 0)                                          else if (errno == EINTR)
722                                          {                                          {
723                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)                                                  continue;
                                                 {  
                                                         stdout_write_wait = 0;  
                                                         break;  
                                                 }  
                                                 else if (errno == EINTR)  
                                                 {  
                                                         continue;  
                                                 }  
                                                 else  
                                                 {  
                                                         log_error("write(STDOUT) error (%d)\n", errno);  
                                                         loop = 0;  
                                                         break;  
                                                 }  
724                                          }                                          }
725                                          else if (ret == 0) // broken pipe                                          else
726                                          {                                          {
727                                                  log_common("write(STDOUT) EOF\n");                                                  log_error("write(STDOUT) error (%d)\n", errno);
                                                 stdout_write_wait = 0;  
728                                                  loop = 0;                                                  loop = 0;
729                                                  break;                                                  break;
730                                          }                                          }
731                                          else                                  }
732                                    else if (ret == 0) // broken pipe
733                                    {
734    #ifdef _DEBUG
735                                            log_error("write(STDOUT) EOF\n");
736    #endif
737                                            stdout_write_wait = 0;
738                                            loop = 0;
739                                            break;
740                                    }
741                                    else
742                                    {
743                                            output_conv_offset += ret;
744                                            if (output_conv_offset >= output_conv_len) // Output buffer complete
745                                          {                                          {
746                                                  output_buf_offset += ret;                                                  output_conv_offset = 0;
747                                                  if (output_buf_offset >= output_buf_len) // Output buffer complete                                                  output_conv_len = 0;
748                                                  {                                                  break;
                                                         output_buf_offset = 0;  
                                                         output_buf_len = 0;  
                                                         break;  
                                                 }  
                                                 continue;  
749                                          }                                          }
750                                            continue;
751                                  }                                  }
752                          }                          }
753                  }                  }
754          }          }
755    
756            iconv_close(input_cd);
757            iconv_close(output_cd);
758    
759  cleanup:  cleanup:
760          if (close(epollfd) < 0)          if (close(epollfd) < 0)
761          {          {
# Line 696  bbsnet_refresh() Line 802  bbsnet_refresh()
802          moveto(22, 0);          moveto(22, 0);
803          prints(" ----------------------------------------------------------------------------- ");          prints(" ----------------------------------------------------------------------------- ");
804          moveto(23, 0);          moveto(23, 0);
805          prints(" [\x1b[1;32mCtrl+C\x1b[m]退出");          prints(" [\x1b[1;32mCtrl+C\x1b[m]閫鍑");
806    
807          iflush();          iflush();
808    
# Line 709  int bbsnet_selchange() Line 815  int bbsnet_selchange()
815    
816          moveto(20, 0);          moveto(20, 0);
817          clrtoeol();          clrtoeol();
818          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",
819                     bbsnet_conf[i].host2, bbsnet_conf[i].host1);                     bbsnet_conf[i].host2, bbsnet_conf[i].host1);
820          moveto(20, 79);          moveto(20, 79);
821          prints("|");          prints("|");
822          moveto(21, 0);          moveto(21, 0);
823          clrtoeol();          clrtoeol();
824          prints("|\x1b[1m连往:\x1b[1;33m%-20s", bbsnet_conf[i].ip);          prints("|\x1b[1m杩炲線:\x1b[1;33m%-20s", bbsnet_conf[i].ip);
825          if (bbsnet_conf[i].port != 23)          if (bbsnet_conf[i].port != 23)
826          {          {
827                  prints("  %d", bbsnet_conf[i].port);                  prints("  %d", bbsnet_conf[i].port);
# Line 734  int bbs_net() Line 840  int bbs_net()
840    
841          load_bbsnet_conf(CONF_BBSNET);          load_bbsnet_conf(CONF_BBSNET);
842    
         BBS_last_access_tm = time(NULL);  
   
843          clearscr();          clearscr();
844          bbsnet_refresh();          bbsnet_refresh();
845          display_menu(&bbsnet_menu);          display_menu(&bbsnet_menu);
# Line 744  int bbs_net() Line 848  int bbs_net()
848          while (!SYS_server_exit)          while (!SYS_server_exit)
849          {          {
850                  ch = igetch(100);                  ch = igetch(100);
851    
852                    if (ch != KEY_NULL && ch != KEY_TIMEOUT)
853                    {
854                            BBS_last_access_tm = time(NULL);
855                    }
856    
857                  switch (ch)                  switch (ch)
858                  {                  {
859                  case KEY_NULL: // broken pipe                  case KEY_NULL: // broken pipe
860                  case KEY_ESC:                          log_error("KEY_NULL\n");
                 case Ctrl('C'): // user cancel  
861                          goto cleanup;                          goto cleanup;
862                  case KEY_TIMEOUT:                  case KEY_TIMEOUT:
863                          if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)
864                          {                          {
865                                    log_error("User input timeout\n");
866                                  goto cleanup;                                  goto cleanup;
867                          }                          }
868                          continue;                          continue;
869                    case KEY_ESC:
870                    case Ctrl('C'): // user cancel
871                            goto cleanup;
872                  case CR:                  case CR:
                         igetch_reset();  
873                          bbsnet_connect(bbsnet_menu.menu_item_pos[0]);                          bbsnet_connect(bbsnet_menu.menu_item_pos[0]);
874                          bbsnet_refresh();                          bbsnet_refresh();
875                          display_menu(&bbsnet_menu);                          display_menu(&bbsnet_menu);
# Line 800  int bbs_net() Line 912  int bbs_net()
912                          bbsnet_selchange();                          bbsnet_selchange();
913                          break;                          break;
914                  }                  }
                 BBS_last_access_tm = time(NULL);  
915          }          }
916    
917  cleanup:  cleanup:


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

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