/[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.55 by sysadm, Sat Jun 21 02:15:18 2025 UTC Revision 1.74 by sysadm, Wed Oct 29 03:23:39 2025 UTC
# Line 53  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;  static MENU_SET bbsnet_menu;
60    
61  int load_bbsnet_conf(const char *file_config)  static int load_bbsnet_conf(const char *file_config)
62  {  {
63          FILE *fp;          FILE *fp;
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 101  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 114  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 149  int load_bbsnet_conf(const char *file_co Line 153  int load_bbsnet_conf(const char *file_co
153          return 0;          return 0;
154  }  }
155    
156  void unload_bbsnet_conf(void)  static void unload_bbsnet_conf(void)
157  {  {
158          bbsnet_menu.menu_count = 0;          bbsnet_menu.menu_count = 0;
159          bbsnet_menu.menu_item_count = 0;          bbsnet_menu.menu_item_count = 0;
# Line 160  void unload_bbsnet_conf(void) Line 164  void unload_bbsnet_conf(void)
164          bbsnet_menu.p_menu_item_pool = NULL;          bbsnet_menu.p_menu_item_pool = NULL;
165  }  }
166    
167  void process_bar(int n, int len)  static 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 188  void process_bar(int n, int len) Line 192  void process_bar(int n, int len)
192          iflush();          iflush();
193  }  }
194    
195  int bbsnet_connect(int n)  static int bbsnet_connect(int n)
196  {  {
197          int sock, ret, loop, error;          int sock, ret, loop, error;
198          int sock_connected = 0;          int sock_connected = 0;
# Line 203  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 213  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    
# Line 225  int bbsnet_connect(int n) Line 241  int bbsnet_connect(int n)
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 233  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 242  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 267  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 287  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 295  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 322  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 380  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 392  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 404  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 441  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                                          if (SSH_v2)                                          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                                            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)                                                  // Send NO-OP to remote server
540                                                  {                                                  input_buf[input_buf_len] = '\0';
541                                                          stdin_read_wait = 0;                                                  input_buf_len++;
542                                                          break;                                                  BBS_last_access_tm = time(NULL);
543                                                  }  
544                                                  else if (errno == EINTR)                                                  stdin_read_wait = 0;
545                                                  {                                                  break; // Check whether channel is still open
                                                         continue;  
                                                 }  
                                                 else  
                                                 {  
                                                         log_error("read(STDIN) error (%d)\n", errno);  
                                                         loop = 0;  
                                                         break;  
                                                 }  
546                                          }                                          }
547                                          else if (ret == 0) // broken pipe                                  }
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)
553                                    {
554                                            if (errno == EAGAIN || errno == EWOULDBLOCK)
555                                          {                                          {
 #ifdef _DEBUG  
                                                 log_error("read(STDIN) EOF\n");  
 #endif  
556                                                  stdin_read_wait = 0;                                                  stdin_read_wait = 0;
                                                 loop = 0;  
557                                                  break;                                                  break;
558                                          }                                          }
559                                          else                                          else if (errno == EINTR)
560                                          {                                          {
                                                 input_buf_len += ret;  
                                                 BBS_last_access_tm = time(NULL);  
561                                                  continue;                                                  continue;
562                                          }                                          }
563                                            else
564                                            {
565                                                    log_error("read(STDIN) error (%d)\n", errno);
566                                                    loop = 0;
567                                                    break;
568                                            }
569                                    }
570                                    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;
577                                            break;
578                                    }
579                                    else
580                                    {
581                                            input_buf_len += ret;
582                                            BBS_last_access_tm = time(NULL);
583    
584                                            // 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 (events[i].data.fd == sock || sock_write_wait) // EPOLLOUT                  if (sock_write_wait)
596                    {
597                            if (input_buf_offset < input_buf_len)
598                          {                          {
599                                  sock_write_wait = 1;                                  // For debug
600                                  while (input_buf_offset < input_buf_len && !SYS_server_exit)  #ifdef _DEBUG
601                                    for (int j = input_buf_offset; j < input_buf_len; j++)
602                                  {                                  {
603                                          ret = (int)write(sock, input_buf + input_buf_offset, (size_t)(input_buf_len - input_buf_offset));                                          log_error("Debug input: <--[%u]\n", (input_buf[j] + 256) % 256);
604                                          if (ret < 0)                                  }
605                                          {  #endif
606                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)  
607                                                  {                                  ret = io_buf_conv(input_cd, input_buf, &input_buf_len, &input_buf_offset, input_conv, sizeof(input_conv), &input_conv_len);
608                                                          sock_write_wait = 0;                                  if (ret < 0)
609                                                          break;                                  {
610                                                  }                                          log_error("io_buf_conv(input, %d, %d, %d) error\n", input_buf_len, input_buf_offset, input_conv_len);
611                                                  else if (errno == EINTR)                                          input_buf_len = input_buf_offset; // Discard invalid sequence
612                                                  {                                  }
613                                                          continue;  
614                                                  }                                  // For debug
                                                 else  
                                                 {  
                                                         log_error("write(socket) error (%d)\n", errno);  
                                                         loop = 0;  
                                                         break;  
                                                 }  
                                         }  
                                         else if (ret == 0) // broken pipe  
                                         {  
615  #ifdef _DEBUG  #ifdef _DEBUG
616                                                  log_error("write(socket) EOF\n");                                  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  #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;                                                  sock_write_wait = 0;
                                                 loop = 0;  
631                                                  break;                                                  break;
632                                          }                                          }
633                                          else                                          else if (errno == EINTR)
634                                          {                                          {
                                                 input_buf_offset += ret;  
                                                 if (input_buf_offset >= input_buf_len) // Output buffer complete  
                                                 {  
                                                         input_buf_offset = 0;  
                                                         input_buf_len = 0;  
                                                         break;  
                                                 }  
635                                                  continue;                                                  continue;
636                                          }                                          }
637                                            else
638                                            {
639                                                    log_error("write(socket) error (%d)\n", errno);
640                                                    loop = 0;
641                                                    break;
642                                            }
643                                    }
644                                    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;
651                                            break;
652                                    }
653                                    else
654                                    {
655                                            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 (events[i].data.fd == sock || sock_read_wait) // EPOLLIN                  if (sock_read_wait)
668                    {
669                            while (output_buf_len < sizeof(output_buf) && !SYS_server_exit)
670                          {                          {
671                                  sock_read_wait = 1;                                  ret = (int)read(sock, output_buf + output_buf_len, sizeof(output_buf) - (size_t)output_buf_len);
672                                  while (output_buf_len < sizeof(output_buf) && !SYS_server_exit)                                  if (ret < 0)
673                                  {                                  {
674                                          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  
675                                          {                                          {
 #ifdef _DEBUG  
                                                 log_error("read(socket) EOF\n");  
 #endif  
676                                                  sock_read_wait = 0;                                                  sock_read_wait = 0;
                                                 loop = 0;  
677                                                  break;                                                  break;
678                                          }                                          }
679                                          else                                          else if (errno == EINTR)
680                                          {                                          {
                                                 output_buf_len += ret;  
681                                                  continue;                                                  continue;
682                                          }                                          }
683                                            else
684                                            {
685                                                    log_error("read(socket) error (%d)\n", errno);
686                                                    loop = 0;
687                                                    break;
688                                            }
689                                    }
690                                    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;
697                                            break;
698                                    }
699                                    else
700                                    {
701                                            output_buf_len += ret;
702                                            continue;
703                                  }                                  }
704                          }                          }
705                    }
706    
707                          if (events[i].data.fd == STDOUT_FILENO || stdout_write_wait)                  if (stdout_write_wait)
708                    {
709                            if (output_buf_offset < output_buf_len)
710                          {                          {
711                                  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);
712                                  while (output_buf_offset < output_buf_len && !SYS_server_exit)                                  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                                          if (SSH_v2)                                          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                                                  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));
727                                                  if (ret == SSH_ERROR)                                                  loop = 0;
728                                                  {                                                  break;
                                                         log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session));  
                                                         loop = 0;  
                                                         break;  
                                                 }  
729                                          }                                          }
730                                          else                                  }
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)
736                                    {
737                                            if (errno == EAGAIN || errno == EWOULDBLOCK)
738                                          {                                          {
739                                                  ret = (int)write(STDOUT_FILENO, output_buf + output_buf_offset, (size_t)(output_buf_len - output_buf_offset));                                                  stdout_write_wait = 0;
740                                                    break;
741                                          }                                          }
742                                          if (ret < 0)                                          else if (errno == EINTR)
743                                          {                                          {
744                                                  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;  
                                                 }  
745                                          }                                          }
746                                          else if (ret == 0) // broken pipe                                          else
747                                          {                                          {
748  #ifdef _DEBUG                                                  log_error("write(STDOUT) error (%d)\n", errno);
                                                 log_error("write(STDOUT) EOF\n");  
 #endif  
                                                 stdout_write_wait = 0;  
749                                                  loop = 0;                                                  loop = 0;
750                                                  break;                                                  break;
751                                          }                                          }
752                                          else                                  }
753                                    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;
760                                            break;
761                                    }
762                                    else
763                                    {
764                                            output_conv_offset += ret;
765                                            if (output_conv_offset >= output_conv_len) // Output buffer complete
766                                          {                                          {
767                                                  output_buf_offset += ret;                                                  output_conv_offset = 0;
768                                                  if (output_buf_offset >= output_buf_len) // Output buffer complete                                                  output_conv_len = 0;
769                                                  {                                                  break;
                                                         output_buf_offset = 0;  
                                                         output_buf_len = 0;  
                                                         break;  
                                                 }  
                                                 continue;  
770                                          }                                          }
771                                            continue;
772                                  }                                  }
773                          }                          }
774                  }                  }
775          }          }
776    
777            iconv_close(input_cd);
778            iconv_close(output_cd);
779    
780  cleanup:  cleanup:
781          if (close(epollfd) < 0)          if (close(epollfd) < 0)
782          {          {
# Line 687  cleanup: Line 802  cleanup:
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  }  }
809    
810  static int  static int bbsnet_refresh()
 bbsnet_refresh()  
811  {  {
812          clearscr();          clearscr();
813          moveto(1, 0);          moveto(1, 0);
# Line 708  bbsnet_refresh() Line 824  bbsnet_refresh()
824          moveto(22, 0);          moveto(22, 0);
825          prints(" ----------------------------------------------------------------------------- ");          prints(" ----------------------------------------------------------------------------- ");
826          moveto(23, 0);          moveto(23, 0);
827          prints(" [\x1b[1;32mCtrl+C\x1b[m]退出");          prints(" [\x1b[1;32mCtrl+C\x1b[m]閫鍑");
828    
829          iflush();          iflush();
830    
831          return 0;          return 0;
832  }  }
833    
834  int bbsnet_selchange()  static int bbsnet_selchange()
835  {  {
836          int i = bbsnet_menu.menu_item_pos[0];          int i = bbsnet_menu.menu_item_pos[0];
837    
838          moveto(20, 0);          moveto(20, 0);
839          clrtoeol();          clrtoeol();
840          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",
841                     bbsnet_conf[i].host2, bbsnet_conf[i].host1);                     bbsnet_conf[i].host2, bbsnet_conf[i].host1);
842          moveto(20, 79);          moveto(20, 79);
843          prints("|");          prints("|");
844          moveto(21, 0);          moveto(21, 0);
845          clrtoeol();          clrtoeol();
846          prints("|\x1b[1m连往:\x1b[1;33m%-20s", bbsnet_conf[i].ip);          prints("|\x1b[1m杩炲線:\x1b[1;33m%-20s", bbsnet_conf[i].ip);
847          if (bbsnet_conf[i].port != 23)          if (bbsnet_conf[i].port != 23)
848          {          {
849                  prints("  %d", bbsnet_conf[i].port);                  prints("  %d", bbsnet_conf[i].port);
# Line 740  int bbsnet_selchange() Line 856  int bbsnet_selchange()
856          return 0;          return 0;
857  }  }
858    
859  int bbs_net()  extern int bbs_net()
860  {  {
861          int ch, i;          int ch, i;
862    
863          load_bbsnet_conf(CONF_BBSNET);          load_bbsnet_conf(CONF_BBSNET);
864    
         BBS_last_access_tm = time(NULL);  
   
865          clearscr();          clearscr();
866          bbsnet_refresh();          bbsnet_refresh();
867          display_menu(&bbsnet_menu);          display_menu(&bbsnet_menu);
# Line 757  int bbs_net() Line 871  int bbs_net()
871          {          {
872                  ch = igetch(100);                  ch = igetch(100);
873    
874                    if (ch != KEY_NULL && ch != KEY_TIMEOUT)
875                    {
876                            BBS_last_access_tm = time(NULL);
877                    }
878    
879                  switch (ch)                  switch (ch)
880                  {                  {
881                  case KEY_NULL: // broken pipe                  case KEY_NULL: // broken pipe
882                  case KEY_ESC:                          log_error("KEY_NULL\n");
                 case Ctrl('C'): // user cancel  
883                          goto cleanup;                          goto cleanup;
884                  case KEY_TIMEOUT:                  case KEY_TIMEOUT:
885                          if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)
886                          {                          {
887                                    log_error("User input timeout\n");
888                                  goto cleanup;                                  goto cleanup;
889                          }                          }
890                          continue;                          continue;
891                    case KEY_ESC:
892                    case Ctrl('C'): // user cancel
893                            goto cleanup;
894                  case CR:                  case CR:
                         igetch_reset();  
895                          bbsnet_connect(bbsnet_menu.menu_item_pos[0]);                          bbsnet_connect(bbsnet_menu.menu_item_pos[0]);
896                          bbsnet_refresh();                          bbsnet_refresh();
897                          display_menu(&bbsnet_menu);                          display_menu(&bbsnet_menu);
# Line 813  int bbs_net() Line 934  int bbs_net()
934                          bbsnet_selchange();                          bbsnet_selchange();
935                          break;                          break;
936                  }                  }
                 BBS_last_access_tm = time(NULL);  
937          }          }
938    
939  cleanup:  cleanup:


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

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