/[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.48 by sysadm, Thu Jun 5 08:36:02 2025 UTC Revision 1.67 by sysadm, Sat Oct 18 05:02:15 2025 UTC
# Line 16  Line 16 
16    
17  #include "bbs.h"  #include "bbs.h"
18  #include "common.h"  #include "common.h"
 #include "log.h"  
19  #include "io.h"  #include "io.h"
20  #include "screen.h"  #include "log.h"
21    #include "login.h"
22  #include "menu.h"  #include "menu.h"
23  #include <stdio.h>  #include "screen.h"
 #include <stdarg.h>  
24  #include <errno.h>  #include <errno.h>
 #include <string.h>  
 #include <stdlib.h>  
25  #include <fcntl.h>  #include <fcntl.h>
26    #include <netdb.h>
27    #include <stdarg.h>
28    #include <stdio.h>
29    #include <stdlib.h>
30    #include <string.h>
31  #include <time.h>  #include <time.h>
32  #include <unistd.h>  #include <unistd.h>
 #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 52  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 62  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 100  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 113  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 202  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          struct epoll_event ev, events[MAX_EVENTS];          struct epoll_event ev, events[MAX_EVENTS];
219          int nfds, epollfd;          int nfds, epollfd;
220          int stdin_read_wait = 0;          int stdin_read_wait = 0;
# Line 212  int bbsnet_connect(int n) Line 225  int bbsnet_connect(int n)
225          int tos;          int tos;
226          char remote_addr[IP_ADDR_LEN];          char remote_addr[IP_ADDR_LEN];
227          int remote_port;          int remote_port;
228          time_t t_used;          char local_addr[IP_ADDR_LEN];
229            int local_port;
230            socklen_t sock_len;
231            time_t t_used = time(NULL);
232          struct tm *tm_used;          struct tm *tm_used;
233          int ch;          int ch;
234    
235            if (user_online_update("BBS_NET") < 0)
236            {
237                    log_error("user_online_update(BBS_NET) error\n");
238            }
239    
240          clearscr();          clearscr();
241    
242          moveto(0, 0);          moveto(0, 0);
243          prints("\033[1;32m正在测试往 %s (%s) 的连接,请稍候... \033[m\r\n",          prints("\033[1;32m姝e湪娴嬭瘯寰 %s (%s) 鐨勮繛鎺ワ紝璇风◢鍊... \033[m\r\n",
244                     bbsnet_conf[n].host1, bbsnet_conf[n].ip);                     bbsnet_conf[n].host1, bbsnet_conf[n].ip);
245          iflush();          iflush();
246    
# Line 227  int bbsnet_connect(int n) Line 248  int bbsnet_connect(int n)
248    
249          if (p_host == NULL)          if (p_host == NULL)
250          {          {
251                  prints("\033[1;31m查找主机名失败!\033[m\r\n");                  prints("\033[1;31m鏌ユ壘涓绘満鍚嶅け璐ワ紒\033[m\r\n");
252                  press_any_key();                  press_any_key();
253                  return -1;                  return -1;
254          }          }
# Line 236  int bbsnet_connect(int n) Line 257  int bbsnet_connect(int n)
257    
258          if (sock < 0)          if (sock < 0)
259          {          {
260                  prints("\033[1;31m无法创建socket!\033[m\r\n");                  prints("\033[1;31m鏃犳硶鍒涘缓socket锛乗033[m\r\n");
261                  press_any_key();                  press_any_key();
262                  return -1;                  return -1;
263          }          }
# Line 252  int bbsnet_connect(int n) Line 273  int bbsnet_connect(int n)
273                  return -2;                  return -2;
274          }          }
275    
276          bzero(&sin, sizeof(sin));          memset(&sin, 0, sizeof(sin));
277          sin.sin_family = AF_INET;          sin.sin_family = AF_INET;
278          sin.sin_addr = *(struct in_addr *)p_host->h_addr_list[0];          sin.sin_addr = *(struct in_addr *)p_host->h_addr_list[0];
279          sin.sin_port = htons(bbsnet_conf[n].port);          sin.sin_port = htons(bbsnet_conf[n].port);
# Line 261  int bbsnet_connect(int n) Line 282  int bbsnet_connect(int n)
282          remote_addr[sizeof(remote_addr) - 1] = '\0';          remote_addr[sizeof(remote_addr) - 1] = '\0';
283          remote_port = ntohs(sin.sin_port);          remote_port = ntohs(sin.sin_port);
284    
285          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");
286          process_bar(0, MAX_PROCESS_BAR_LEN);          process_bar(0, MAX_PROCESS_BAR_LEN);
287    
288          // Set socket as non-blocking          // Set socket as non-blocking
# Line 281  int bbsnet_connect(int n) Line 302  int bbsnet_connect(int n)
302                  return -1;                  return -1;
303          }          }
304    
305          ev.events = EPOLLOUT;          ev.events = EPOLLOUT | EPOLLET;
306          ev.data.fd = sock;          ev.data.fd = sock;
307          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sock, &ev) == -1)          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sock, &ev) == -1)
308          {          {
# Line 289  int bbsnet_connect(int n) Line 310  int bbsnet_connect(int n)
310                  goto cleanup;                  goto cleanup;
311          }          }
312    
313          ev.events = EPOLLIN;          ev.events = EPOLLIN | EPOLLET;
314          ev.data.fd = STDIN_FILENO;          ev.data.fd = STDIN_FILENO;
315          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1)          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1)
316          {          {
# Line 316  int bbsnet_connect(int n) Line 337  int bbsnet_connect(int n)
337                          {                          {
338                                  log_error("connect(socket) error (%d)\n", errno);                                  log_error("connect(socket) error (%d)\n", errno);
339    
340                                  prints("\033[1;31m连接失败!\033[m\r\n");                                  prints("\033[1;31m杩炴帴澶辫触锛乗033[m\r\n");
341                                  press_any_key();                                  press_any_key();
342    
343                                  goto cleanup;                                  goto cleanup;
# Line 374  int bbsnet_connect(int n) Line 395  int bbsnet_connect(int n)
395          }          }
396          if (!sock_connected)          if (!sock_connected)
397          {          {
398                  prints("\033[1;31m连接失败!\033[m\r\n");                  prints("\033[1;31m杩炴帴澶辫触锛乗033[m\r\n");
399                  press_any_key();                  press_any_key();
400    
401                  goto cleanup;                  goto cleanup;
# Line 386  int bbsnet_connect(int n) Line 407  int bbsnet_connect(int n)
407                  log_error("setsockopt IP_TOS=%d error (%d)\n", tos, errno);                  log_error("setsockopt IP_TOS=%d error (%d)\n", tos, errno);
408          }          }
409    
410          prints("\033[1;31m连接成功!\033[m\r\n");          sock_len = sizeof(sin);
411            if (getsockname(sock, (struct sockaddr *)&sin, &sock_len) < 0)
412            {
413                    log_error("getsockname() error: %d", errno);
414                    goto cleanup;
415            }
416    
417            strncpy(local_addr, inet_ntoa(sin.sin_addr), sizeof(local_addr) - 1);
418            local_addr[sizeof(local_addr) - 1] = '\0';
419            local_port = ntohs(sin.sin_port);
420    
421            prints("\033[1;31m杩炴帴鎴愬姛锛乗033[m\r\n");
422          iflush();          iflush();
423          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",
424                               remote_addr, remote_port, local_addr, local_port, BBS_username);
425    
426            input_cd = iconv_open(bbsnet_conf[n].charset, stdio_charset);
427            if (input_cd == (iconv_t)(-1))
428            {
429                    log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, bbsnet_conf[n].charset, errno);
430                    goto cleanup;
431            }
432            output_cd = iconv_open(stdio_charset, bbsnet_conf[n].charset);
433            if (output_cd == (iconv_t)(-1))
434            {
435                    log_error("iconv_open(%s->%s) error: %d\n", bbsnet_conf[n].charset, stdio_charset, errno);
436                    iconv_close(input_cd);
437                    goto cleanup;
438            }
439    
440          ev.events = EPOLLIN | EPOLLOUT | EPOLLET;          ev.events = EPOLLIN | EPOLLOUT | EPOLLET;
441          ev.data.fd = sock;          ev.data.fd = sock;
# Line 398  int bbsnet_connect(int n) Line 445  int bbsnet_connect(int n)
445                  goto cleanup;                  goto cleanup;
446          }          }
447    
448          ev.events = EPOLLOUT;          ev.events = EPOLLOUT | EPOLLET;
449          ev.data.fd = STDOUT_FILENO;          ev.data.fd = STDOUT_FILENO;
450          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1)          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1)
451          {          {
# Line 406  int bbsnet_connect(int n) Line 453  int bbsnet_connect(int n)
453                  goto cleanup;                  goto cleanup;
454          }          }
455    
456          BBS_last_access_tm = t_used = time(0);          BBS_last_access_tm = t_used = time(NULL);
457          loop = 1;          loop = 1;
458    
459          while (loop && !SYS_server_exit)          while (loop && !SYS_server_exit)
# Line 431  int bbsnet_connect(int n) Line 478  int bbsnet_connect(int n)
478                  }                  }
479                  else if (nfds == 0) // timeout                  else if (nfds == 0) // timeout
480                  {                  {
481                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)
482                          {                          {
483                                  break;                                  break;
484                          }                          }
                         continue;  
485                  }                  }
486    
487                  for (int i = 0; i < nfds; i++)                  for (int i = 0; i < nfds; i++)
488                  {                  {
489                          if (events[i].data.fd == STDIN_FILENO || stdin_read_wait)                          if (events[i].data.fd == STDIN_FILENO)
490                          {                          {
491                                  stdin_read_wait = 1;                                  stdin_read_wait = 1;
492                                  while (input_buf_len < sizeof(input_buf) && !SYS_server_exit)                          }
493    
494                            if (events[i].data.fd == sock)
495                            {
496                                    if (events[i].events & EPOLLIN)
497                                    {
498                                            sock_read_wait = 1;
499                                    }
500                                    if (events[i].events & EPOLLOUT)
501                                    {
502                                            sock_write_wait = 1;
503                                    }
504                            }
505    
506                            if (events[i].data.fd == STDOUT_FILENO)
507                            {
508                                    stdout_write_wait = 1;
509                            }
510                    }
511    
512                    if (stdin_read_wait)
513                    {
514                            while (input_buf_len < sizeof(input_buf) && !SYS_server_exit)
515                            {
516                                    if (SSH_v2)
517                                  {                                  {
518                                          if (SSH_v2)                                          ret = ssh_channel_read_nonblocking(SSH_channel, input_buf + input_buf_len, sizeof(input_buf) - (uint32_t)input_buf_len, 0);
519                                            if (ret == SSH_ERROR)
520                                          {                                          {
521                                                  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));
522                                                  if (ret == SSH_ERROR)                                                  loop = 0;
523                                                  {                                                  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  
                                                 }  
524                                          }                                          }
525                                          else                                          else if (ret == SSH_EOF)
526                                          {                                          {
527                                                  ret = (int)read(STDIN_FILENO, input_buf + input_buf_len, sizeof(input_buf) - (size_t)input_buf_len);                                                  stdin_read_wait = 0;
528                                                    loop = 0;
529                                                    break;
530                                          }                                          }
531                                          if (ret < 0)                                          else if (ret == 0)
532                                          {                                          {
533                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)                                                  stdin_read_wait = 0;
534                                                  {                                                  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;  
                                                 }  
535                                          }                                          }
536                                          else if (ret == 0) // broken pipe                                  }
537                                    else
538                                    {
539                                            ret = (int)read(STDIN_FILENO, input_buf + input_buf_len, sizeof(input_buf) - (size_t)input_buf_len);
540                                    }
541                                    if (ret < 0)
542                                    {
543                                            if (errno == EAGAIN || errno == EWOULDBLOCK)
544                                          {                                          {
                                                 log_common("read(STDIN) EOF\n");  
545                                                  stdin_read_wait = 0;                                                  stdin_read_wait = 0;
                                                 loop = 0;  
546                                                  break;                                                  break;
547                                          }                                          }
548                                          else                                          else if (errno == EINTR)
549                                          {                                          {
                                                 input_buf_len += ret;  
                                                 BBS_last_access_tm = time(0);  
550                                                  continue;                                                  continue;
551                                          }                                          }
552                                            else
553                                            {
554                                                    log_error("read(STDIN) error (%d)\n", errno);
555                                                    loop = 0;
556                                                    break;
557                                            }
558                                    }
559                                    else if (ret == 0) // broken pipe
560                                    {
561    #ifdef _DEBUG
562                                            log_error("read(STDIN) EOF\n");
563    #endif
564                                            stdin_read_wait = 0;
565                                            loop = 0;
566                                            break;
567                                    }
568                                    else
569                                    {
570                                            input_buf_len += ret;
571                                            BBS_last_access_tm = time(NULL);
572    
573                                            // Refresh current action while user input
574                                            if (user_online_update("BBS_NET") < 0)
575                                            {
576                                                    log_error("user_online_update(BBS_NET) error\n");
577                                            }
578    
579                                            continue;
580                                    }
581                            }
582                    }
583    
584                    if (sock_write_wait)
585                    {
586                            if (input_buf_offset < input_buf_len)
587                            {
588                                    ret = io_buf_conv(input_cd, input_buf, &input_buf_len, &input_buf_offset, input_conv, sizeof(input_conv), &input_conv_len);
589                                    if (ret < 0)
590                                    {
591                                            log_error("io_buf_conv(input, %d, %d, %d) error\n", input_buf_len, input_buf_offset, input_conv_len);
592                                  }                                  }
593                          }                          }
594    
595                          if (events[i].data.fd == sock || sock_write_wait) // EPOLLOUT                          while (input_conv_offset < input_conv_len && !SYS_server_exit)
596                          {                          {
597                                  sock_write_wait = 1;                                  ret = (int)write(sock, input_conv + input_conv_offset, (size_t)(input_conv_len - input_conv_offset));
598                                  while (input_buf_offset < input_buf_len && !SYS_server_exit)                                  if (ret < 0)
599                                  {                                  {
600                                          ret = (int)write(sock, input_buf + input_buf_offset, (size_t)(input_buf_len - input_buf_offset));                                          if (errno == EAGAIN || errno == EWOULDBLOCK)
601                                          if (ret < 0)                                          {
602                                                    sock_write_wait = 0;
603                                                    break;
604                                            }
605                                            else if (errno == EINTR)
606                                          {                                          {
607                                                  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;  
                                                 }  
608                                          }                                          }
609                                          else if (ret == 0) // broken pipe                                          else
610                                          {                                          {
611                                                  log_common("write(socket) EOF\n");                                                  log_error("write(socket) error (%d)\n", errno);
                                                 sock_write_wait = 0;  
612                                                  loop = 0;                                                  loop = 0;
613                                                  break;                                                  break;
614                                          }                                          }
615                                          else                                  }
616                                    else if (ret == 0) // broken pipe
617                                    {
618    #ifdef _DEBUG
619                                            log_error("write(socket) EOF\n");
620    #endif
621                                            sock_write_wait = 0;
622                                            loop = 0;
623                                            break;
624                                    }
625                                    else
626                                    {
627                                            input_conv_offset += ret;
628                                            if (input_conv_offset >= input_conv_len) // Output buffer complete
629                                          {                                          {
630                                                  input_buf_offset += ret;                                                  input_conv_offset = 0;
631                                                  if (input_buf_offset >= input_buf_len) // Output buffer complete                                                  input_conv_len = 0;
632                                                  {                                                  break;
                                                         input_buf_offset = 0;  
                                                         input_buf_len = 0;  
                                                         break;  
                                                 }  
                                                 continue;  
633                                          }                                          }
634                                            continue;
635                                  }                                  }
636                          }                          }
637                    }
638    
639                          if (events[i].data.fd == sock || sock_read_wait) // EPOLLIN                  if (sock_read_wait)
640                    {
641                            while (output_buf_len < sizeof(output_buf) && !SYS_server_exit)
642                          {                          {
643                                  sock_read_wait = 1;                                  ret = (int)read(sock, output_buf + output_buf_len, sizeof(output_buf) - (size_t)output_buf_len);
644                                  while (output_buf_len < sizeof(output_buf) && !SYS_server_exit)                                  if (ret < 0)
645                                  {                                  {
646                                          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)  
647                                          {                                          {
                                                 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  
                                         {  
                                                 log_common("read(socket) EOF\n");  
648                                                  sock_read_wait = 0;                                                  sock_read_wait = 0;
                                                 loop = 0;  
649                                                  break;                                                  break;
650                                          }                                          }
651                                          else                                          else if (errno == EINTR)
652                                          {                                          {
                                                 output_buf_len += ret;  
653                                                  continue;                                                  continue;
654                                          }                                          }
655                                            else
656                                            {
657                                                    log_error("read(socket) error (%d)\n", errno);
658                                                    loop = 0;
659                                                    break;
660                                            }
661                                    }
662                                    else if (ret == 0) // broken pipe
663                                    {
664    #ifdef _DEBUG
665                                            log_error("read(socket) EOF\n");
666    #endif
667                                            sock_read_wait = 0;
668                                            loop = 0;
669                                            break;
670                                    }
671                                    else
672                                    {
673                                            output_buf_len += ret;
674                                            continue;
675                                  }                                  }
676                          }                          }
677                    }
678    
679                          if (events[i].data.fd == STDOUT_FILENO || stdout_write_wait)                  if (stdout_write_wait)
680                    {
681                            if (output_buf_offset < output_buf_len)
682                          {                          {
683                                  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);
684                                  while (output_buf_offset < output_buf_len && !SYS_server_exit)                                  if (ret < 0)
685                                    {
686                                            log_error("io_buf_conv(output, %d, %d, %d) error\n", output_buf_len, output_buf_offset, output_conv_len);
687                                    }
688                            }
689    
690                            while (output_conv_offset < output_conv_len && !SYS_server_exit)
691                            {
692                                    if (SSH_v2)
693                                  {                                  {
694                                          if (SSH_v2)                                          ret = ssh_channel_write(SSH_channel, output_conv + output_conv_offset, (uint32_t)(output_conv_len - output_conv_offset));
695                                            if (ret == SSH_ERROR)
696                                          {                                          {
697                                                  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));
698                                                  if (ret == SSH_ERROR)                                                  loop = 0;
699                                                  {                                                  break;
                                                         log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session));  
                                                         loop = 0;  
                                                         break;  
                                                 }  
700                                          }                                          }
701                                          else                                  }
702                                    else
703                                    {
704                                            ret = (int)write(STDOUT_FILENO, output_conv + output_conv_offset, (size_t)(output_conv_len - output_conv_offset));
705                                    }
706                                    if (ret < 0)
707                                    {
708                                            if (errno == EAGAIN || errno == EWOULDBLOCK)
709                                          {                                          {
710                                                  ret = (int)write(STDOUT_FILENO, output_buf + output_buf_offset, (size_t)(output_buf_len - output_buf_offset));                                                  stdout_write_wait = 0;
711                                                    break;
712                                          }                                          }
713                                          if (ret < 0)                                          else if (errno == EINTR)
714                                          {                                          {
715                                                  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;  
                                                 }  
716                                          }                                          }
717                                          else if (ret == 0) // broken pipe                                          else
718                                          {                                          {
719                                                  log_common("write(STDOUT) EOF\n");                                                  log_error("write(STDOUT) error (%d)\n", errno);
                                                 stdout_write_wait = 0;  
720                                                  loop = 0;                                                  loop = 0;
721                                                  break;                                                  break;
722                                          }                                          }
723                                          else                                  }
724                                    else if (ret == 0) // broken pipe
725                                    {
726    #ifdef _DEBUG
727                                            log_error("write(STDOUT) EOF\n");
728    #endif
729                                            stdout_write_wait = 0;
730                                            loop = 0;
731                                            break;
732                                    }
733                                    else
734                                    {
735                                            output_conv_offset += ret;
736                                            if (output_conv_offset >= output_conv_len) // Output buffer complete
737                                          {                                          {
738                                                  output_buf_offset += ret;                                                  output_conv_offset = 0;
739                                                  if (output_buf_offset >= output_buf_len) // Output buffer complete                                                  output_conv_len = 0;
740                                                  {                                                  break;
                                                         output_buf_offset = 0;  
                                                         output_buf_len = 0;  
                                                         break;  
                                                 }  
                                                 continue;  
741                                          }                                          }
742                                            continue;
743                                  }                                  }
744                          }                          }
745                  }                  }
746          }          }
747    
748            iconv_close(input_cd);
749            iconv_close(output_cd);
750    
751  cleanup:  cleanup:
752          if (close(epollfd) < 0)          if (close(epollfd) < 0)
753          {          {
# Line 666  cleanup: Line 766  cleanup:
766                  log_error("Close socket failed\n");                  log_error("Close socket failed\n");
767          }          }
768    
769          t_used = time(0) - t_used;          t_used = time(NULL) - t_used;
770          tm_used = gmtime(&t_used);          tm_used = gmtime(&t_used);
771    
772          log_common("BBSNET disconnect, %d days %d hours %d minutes %d seconds used\n",          log_common("BBSNET disconnect, %d days %d hours %d minutes %d seconds used\n",
# Line 694  bbsnet_refresh() Line 794  bbsnet_refresh()
794          moveto(22, 0);          moveto(22, 0);
795          prints(" ----------------------------------------------------------------------------- ");          prints(" ----------------------------------------------------------------------------- ");
796          moveto(23, 0);          moveto(23, 0);
797          prints(" [\x1b[1;32mCtrl+C\x1b[m]退出");          prints(" [\x1b[1;32mCtrl+C\x1b[m]閫鍑");
798    
799          iflush();          iflush();
800    
# Line 707  int bbsnet_selchange() Line 807  int bbsnet_selchange()
807    
808          moveto(20, 0);          moveto(20, 0);
809          clrtoeol();          clrtoeol();
810          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",
811                     bbsnet_conf[i].host2, bbsnet_conf[i].host1);                     bbsnet_conf[i].host2, bbsnet_conf[i].host1);
812          moveto(20, 79);          moveto(20, 79);
813          prints("|");          prints("|");
814          moveto(21, 0);          moveto(21, 0);
815          clrtoeol();          clrtoeol();
816          prints("|\x1b[1m连往:\x1b[1;33m%-20s", bbsnet_conf[i].ip);          prints("|\x1b[1m杩炲線:\x1b[1;33m%-20s", bbsnet_conf[i].ip);
817          if (bbsnet_conf[i].port != 23)          if (bbsnet_conf[i].port != 23)
818          {          {
819                  prints("  %d", bbsnet_conf[i].port);                  prints("  %d", bbsnet_conf[i].port);
# Line 732  int bbs_net() Line 832  int bbs_net()
832    
833          load_bbsnet_conf(CONF_BBSNET);          load_bbsnet_conf(CONF_BBSNET);
834    
         BBS_last_access_tm = time(0);  
   
835          clearscr();          clearscr();
836          bbsnet_refresh();          bbsnet_refresh();
837          display_menu(&bbsnet_menu);          display_menu(&bbsnet_menu);
# Line 742  int bbs_net() Line 840  int bbs_net()
840          while (!SYS_server_exit)          while (!SYS_server_exit)
841          {          {
842                  ch = igetch(100);                  ch = igetch(100);
843    
844            if (ch != KEY_NULL && ch != KEY_TIMEOUT)
845            {
846                BBS_last_access_tm = time(NULL);
847            }
848    
849                  switch (ch)                  switch (ch)
850                  {                  {
851                  case KEY_NULL: // broken pipe                  case KEY_NULL: // broken pipe
852                  case KEY_ESC:                          log_error("KEY_NULL\n");
                 case Ctrl('C'): // user cancel  
853                          goto cleanup;                          goto cleanup;
854                  case KEY_TIMEOUT:                  case KEY_TIMEOUT:
855                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)
856                          {                          {
857                                    log_error("User input timeout\n");
858                                  goto cleanup;                                  goto cleanup;
859                          }                          }
860                          continue;                          continue;
861                    case KEY_ESC:
862                    case Ctrl('C'): // user cancel
863                            goto cleanup;
864                  case CR:                  case CR:
                         igetch_reset();  
865                          bbsnet_connect(bbsnet_menu.menu_item_pos[0]);                          bbsnet_connect(bbsnet_menu.menu_item_pos[0]);
866                          bbsnet_refresh();                          bbsnet_refresh();
867                          display_menu(&bbsnet_menu);                          display_menu(&bbsnet_menu);
# Line 798  int bbs_net() Line 904  int bbs_net()
904                          bbsnet_selchange();                          bbsnet_selchange();
905                          break;                          break;
906                  }                  }
                 BBS_last_access_tm = time(0);  
907          }          }
908    
909  cleanup:  cleanup:


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

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