/[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.60 by sysadm, Sat Oct 4 15:55:26 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 <iconv.h>
35  #include <libssh/libssh.h>  #include <libssh/libssh.h>
36  #include <libssh/server.h>  #include <libssh/server.h>
37  #include <libssh/callbacks.h>  #include <libssh/callbacks.h>
38    #include <netinet/in.h>
39    #include <netinet/ip.h>
40    #include <sys/select.h>
41    #include <sys/ioctl.h>
42    #include <sys/socket.h>
43    #include <sys/epoll.h>
44    
45  #define MENU_CONF_DELIM " \t\r\n"  #define MENU_CONF_DELIM " \t\r\n"
46    
# Line 52  struct _bbsnet_conf Line 54  struct _bbsnet_conf
54          char host2[40];          char host2[40];
55          char ip[40];          char ip[40];
56          in_port_t port;          in_port_t port;
57            char charset[20];
58  } bbsnet_conf[MAXSTATION];  } bbsnet_conf[MAXSTATION];
59    
60  MENU_SET bbsnet_menu;  MENU_SET bbsnet_menu;
# Line 62  int load_bbsnet_conf(const char *file_co Line 65  int load_bbsnet_conf(const char *file_co
65          MENU *p_menu;          MENU *p_menu;
66          MENU_ITEM *p_menu_item;          MENU_ITEM *p_menu_item;
67          MENU_ITEM_ID menu_item_id;          MENU_ITEM_ID menu_item_id;
68          char t[256], *t1, *t2, *t3, *t4, *saveptr;          char t[256], *t1, *t2, *t3, *t4, *t5, *saveptr;
69    
70          fp = fopen(file_config, "r");          fp = fopen(file_config, "r");
71          if (fp == NULL)          if (fp == NULL)
# Line 100  int load_bbsnet_conf(const char *file_co Line 103  int load_bbsnet_conf(const char *file_co
103                  t2 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);                  t2 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
104                  t3 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);                  t3 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
105                  t4 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);                  t4 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
106                    t5 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
107    
108                  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] == '*')
109                  {                  {
110                          continue;                          continue;
111                  }                  }
# Line 113  int load_bbsnet_conf(const char *file_co Line 117  int load_bbsnet_conf(const char *file_co
117                  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);
118                  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';
119                  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);
120                    strncpy(bbsnet_conf[menu_item_id].charset, t5, sizeof(bbsnet_conf[menu_item_id].charset) - 1);
121                    bbsnet_conf[menu_item_id].charset[sizeof(bbsnet_conf[menu_item_id].charset) - 1] = '\0';
122    
123                  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);
124                  if (p_menu_item == NULL)                  if (p_menu_item == NULL)
# Line 187  void process_bar(int n, int len) Line 193  void process_bar(int n, int len)
193          iflush();          iflush();
194  }  }
195    
196    int bbsnet_io_buf_conv(iconv_t cd, char *p_buf, int *p_buf_len, int *p_buf_offset, char *p_conv, size_t conv_size, int *p_conv_len)
197    {
198            char *in_buf;
199            char *out_buf;
200            size_t in_bytes;
201            size_t out_bytes;
202            int ret;
203    
204            in_buf = p_buf + *p_buf_offset;
205            in_bytes = (size_t)(*p_buf_len - *p_buf_offset);
206            out_buf = p_conv + *p_conv_len;
207            out_bytes = conv_size - (size_t)(*p_conv_len);
208    
209            while (in_bytes > 0)
210            {
211                    ret = (int)iconv(cd, &in_buf, &in_bytes, &out_buf, &out_bytes);
212                    if (ret == -1)
213                    {
214                            if (errno == EINVAL) // Incomplete
215                            {
216                                    *p_buf_len = (int)(p_buf + *p_buf_len - in_buf);
217                                    *p_buf_offset = 0;
218                                    *p_conv_len = (int)(conv_size - out_bytes);
219                                    memmove(p_buf, in_buf, (size_t)(*p_buf_len));
220    
221                                    break;
222                            }
223                            else if (errno == E2BIG)
224                            {
225                                    log_error("iconv(inbytes=%d, outbytes=%d) error: E2BIG\n", in_bytes, out_bytes);
226                                    return -1;
227                            }
228                            else if (errno == EILSEQ)
229                            {
230                                    if (in_bytes > out_bytes || out_bytes <= 0)
231                                    {
232                                            errno = E2BIG;
233                                            return -1;
234                                    }
235    
236                                    *out_buf++ = *in_buf++;
237                                    in_bytes--;
238                                    out_bytes--;
239    
240                                    continue;
241                            }
242                    }
243                    else
244                    {
245                            *p_buf_len = 0;
246                            *p_buf_offset = 0;
247                            *p_conv_len = (int)(conv_size - out_bytes);
248    
249                            break;
250                    }
251            }
252    
253            return 0;
254    }
255    
256  int bbsnet_connect(int n)  int bbsnet_connect(int n)
257  {  {
258          int sock, ret, loop, error;          int sock, ret, loop, error;
# Line 202  int bbsnet_connect(int n) Line 268  int bbsnet_connect(int n)
268          int output_buf_len = 0;          int output_buf_len = 0;
269          int input_buf_offset = 0;          int input_buf_offset = 0;
270          int output_buf_offset = 0;          int output_buf_offset = 0;
271            iconv_t input_cd = NULL;
272            char input_conv[LINE_BUFFER_LEN * 2];
273            char output_conv[LINE_BUFFER_LEN * 2];
274            int input_conv_len = 0;
275            int output_conv_len = 0;
276            int input_conv_offset = 0;
277            int output_conv_offset = 0;
278            iconv_t output_cd = NULL;
279          struct epoll_event ev, events[MAX_EVENTS];          struct epoll_event ev, events[MAX_EVENTS];
280          int nfds, epollfd;          int nfds, epollfd;
281          int stdin_read_wait = 0;          int stdin_read_wait = 0;
# Line 212  int bbsnet_connect(int n) Line 286  int bbsnet_connect(int n)
286          int tos;          int tos;
287          char remote_addr[IP_ADDR_LEN];          char remote_addr[IP_ADDR_LEN];
288          int remote_port;          int remote_port;
289          time_t t_used;          time_t t_used = time(NULL);
290          struct tm *tm_used;          struct tm *tm_used;
291          int ch;          int ch;
292    
293            if (user_online_update("BBS_NET") < 0)
294            {
295                    log_error("user_online_update(BBS_NET) error\n");
296            }
297    
298          clearscr();          clearscr();
299    
300          moveto(0, 0);          moveto(0, 0);
301          prints("\033[1;32m正在测试往 %s (%s) 的连接,请稍候... \033[m\r\n",          prints("\033[1;32m姝e湪娴嬭瘯寰 %s (%s) 鐨勮繛鎺ワ紝璇风◢鍊... \033[m\r\n",
302                     bbsnet_conf[n].host1, bbsnet_conf[n].ip);                     bbsnet_conf[n].host1, bbsnet_conf[n].ip);
303          iflush();          iflush();
304    
# Line 227  int bbsnet_connect(int n) Line 306  int bbsnet_connect(int n)
306    
307          if (p_host == NULL)          if (p_host == NULL)
308          {          {
309                  prints("\033[1;31m查找主机名失败!\033[m\r\n");                  prints("\033[1;31m鏌ユ壘涓绘満鍚嶅け璐ワ紒\033[m\r\n");
310                  press_any_key();                  press_any_key();
311                  return -1;                  return -1;
312          }          }
# Line 236  int bbsnet_connect(int n) Line 315  int bbsnet_connect(int n)
315    
316          if (sock < 0)          if (sock < 0)
317          {          {
318                  prints("\033[1;31m无法创建socket!\033[m\r\n");                  prints("\033[1;31m鏃犳硶鍒涘缓socket锛乗033[m\r\n");
319                  press_any_key();                  press_any_key();
320                  return -1;                  return -1;
321          }          }
# Line 252  int bbsnet_connect(int n) Line 331  int bbsnet_connect(int n)
331                  return -2;                  return -2;
332          }          }
333    
334          bzero(&sin, sizeof(sin));          memset(&sin, 0, sizeof(sin));
335          sin.sin_family = AF_INET;          sin.sin_family = AF_INET;
336          sin.sin_addr = *(struct in_addr *)p_host->h_addr_list[0];          sin.sin_addr = *(struct in_addr *)p_host->h_addr_list[0];
337          sin.sin_port = htons(bbsnet_conf[n].port);          sin.sin_port = htons(bbsnet_conf[n].port);
# Line 261  int bbsnet_connect(int n) Line 340  int bbsnet_connect(int n)
340          remote_addr[sizeof(remote_addr) - 1] = '\0';          remote_addr[sizeof(remote_addr) - 1] = '\0';
341          remote_port = ntohs(sin.sin_port);          remote_port = ntohs(sin.sin_port);
342    
343          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");
344          process_bar(0, MAX_PROCESS_BAR_LEN);          process_bar(0, MAX_PROCESS_BAR_LEN);
345    
346          // Set socket as non-blocking          // Set socket as non-blocking
# Line 281  int bbsnet_connect(int n) Line 360  int bbsnet_connect(int n)
360                  return -1;                  return -1;
361          }          }
362    
363          ev.events = EPOLLOUT;          ev.events = EPOLLOUT | EPOLLET;
364          ev.data.fd = sock;          ev.data.fd = sock;
365          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sock, &ev) == -1)          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sock, &ev) == -1)
366          {          {
# Line 289  int bbsnet_connect(int n) Line 368  int bbsnet_connect(int n)
368                  goto cleanup;                  goto cleanup;
369          }          }
370    
371          ev.events = EPOLLIN;          ev.events = EPOLLIN | EPOLLET;
372          ev.data.fd = STDIN_FILENO;          ev.data.fd = STDIN_FILENO;
373          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1)          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1)
374          {          {
# Line 316  int bbsnet_connect(int n) Line 395  int bbsnet_connect(int n)
395                          {                          {
396                                  log_error("connect(socket) error (%d)\n", errno);                                  log_error("connect(socket) error (%d)\n", errno);
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 374  int bbsnet_connect(int n) Line 453  int bbsnet_connect(int n)
453          }          }
454          if (!sock_connected)          if (!sock_connected)
455          {          {
456                  prints("\033[1;31m连接失败!\033[m\r\n");                  prints("\033[1;31m杩炴帴澶辫触锛乗033[m\r\n");
457                  press_any_key();                  press_any_key();
458    
459                  goto cleanup;                  goto cleanup;
# Line 386  int bbsnet_connect(int n) Line 465  int bbsnet_connect(int n)
465                  log_error("setsockopt IP_TOS=%d error (%d)\n", tos, errno);                  log_error("setsockopt IP_TOS=%d error (%d)\n", tos, errno);
466          }          }
467    
468          prints("\033[1;31m连接成功!\033[m\r\n");          prints("\033[1;31m杩炴帴鎴愬姛锛乗033[m\r\n");
469          iflush();          iflush();
470          log_common("BBSNET connect to %s:%d\n", remote_addr, remote_port);          log_common("BBSNET connect to %s:%d\n", remote_addr, remote_port);
471    
472            input_cd = iconv_open(bbsnet_conf[n].charset, "UTF-8");
473            if (input_cd == (iconv_t)(-1))
474            {
475                    log_error("iconv_open(UTF8->GBK) error: %d\n", errno);
476                    goto cleanup;
477            }
478            output_cd = iconv_open("UTF-8", bbsnet_conf[n].charset);
479            if (input_cd == (iconv_t)(-1))
480            {
481                    log_error("iconv_open(GBK->UTF-8) error: %d\n", errno);
482                    iconv_close(input_cd);
483                    goto cleanup;
484            }
485    
486          ev.events = EPOLLIN | EPOLLOUT | EPOLLET;          ev.events = EPOLLIN | EPOLLOUT | EPOLLET;
487          ev.data.fd = sock;          ev.data.fd = sock;
488          if (epoll_ctl(epollfd, EPOLL_CTL_MOD, sock, &ev) == -1)          if (epoll_ctl(epollfd, EPOLL_CTL_MOD, sock, &ev) == -1)
# Line 398  int bbsnet_connect(int n) Line 491  int bbsnet_connect(int n)
491                  goto cleanup;                  goto cleanup;
492          }          }
493    
494          ev.events = EPOLLOUT;          ev.events = EPOLLOUT | EPOLLET;
495          ev.data.fd = STDOUT_FILENO;          ev.data.fd = STDOUT_FILENO;
496          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1)          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1)
497          {          {
# Line 406  int bbsnet_connect(int n) Line 499  int bbsnet_connect(int n)
499                  goto cleanup;                  goto cleanup;
500          }          }
501    
502          BBS_last_access_tm = t_used = time(0);          BBS_last_access_tm = t_used = time(NULL);
503          loop = 1;          loop = 1;
504    
505          while (loop && !SYS_server_exit)          while (loop && !SYS_server_exit)
# Line 431  int bbsnet_connect(int n) Line 524  int bbsnet_connect(int n)
524                  }                  }
525                  else if (nfds == 0) // timeout                  else if (nfds == 0) // timeout
526                  {                  {
527                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)
528                          {                          {
529                                  break;                                  break;
530                          }                          }
                         continue;  
531                  }                  }
532    
533                  for (int i = 0; i < nfds; i++)                  for (int i = 0; i < nfds; i++)
534                  {                  {
535                          if (events[i].data.fd == STDIN_FILENO || stdin_read_wait)                          if (events[i].data.fd == STDIN_FILENO)
536                          {                          {
537                                  stdin_read_wait = 1;                                  stdin_read_wait = 1;
538                                  while (input_buf_len < sizeof(input_buf) && !SYS_server_exit)                          }
539    
540                            if (events[i].data.fd == sock)
541                            {
542                                    if (events[i].events & EPOLLIN)
543                                  {                                  {
544                                          if (SSH_v2)                                          sock_read_wait = 1;
545                                    }
546                                    if (events[i].events & EPOLLOUT)
547                                    {
548                                            sock_write_wait = 1;
549                                    }
550                            }
551    
552                            if (events[i].data.fd == STDOUT_FILENO)
553                            {
554                                    stdout_write_wait = 1;
555                            }
556                    }
557    
558                    if (stdin_read_wait)
559                    {
560                            while (input_buf_len < sizeof(input_buf) && !SYS_server_exit)
561                            {
562                                    if (SSH_v2)
563                                    {
564                                            ret = ssh_channel_read_nonblocking(SSH_channel, input_buf + input_buf_len, sizeof(input_buf) - (uint32_t)input_buf_len, 0);
565                                            if (ret == SSH_ERROR)
566                                          {                                          {
567                                                  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));
568                                                  if (ret == SSH_ERROR)                                                  loop = 0;
569                                                  {                                                  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  
                                                 }  
570                                          }                                          }
571                                          else                                          else if (ret == SSH_EOF)
572                                          {                                          {
573                                                  ret = (int)read(STDIN_FILENO, input_buf + input_buf_len, sizeof(input_buf) - (size_t)input_buf_len);                                                  stdin_read_wait = 0;
574                                                    loop = 0;
575                                                    break;
576                                          }                                          }
577                                          if (ret < 0)                                          else if (ret == 0)
578                                          {                                          {
579                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)                                                  stdin_read_wait = 0;
580                                                  {                                                  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;  
                                                 }  
581                                          }                                          }
582                                          else if (ret == 0) // broken pipe                                  }
583                                    else
584                                    {
585                                            ret = (int)read(STDIN_FILENO, input_buf + input_buf_len, sizeof(input_buf) - (size_t)input_buf_len);
586                                    }
587                                    if (ret < 0)
588                                    {
589                                            if (errno == EAGAIN || errno == EWOULDBLOCK)
590                                          {                                          {
                                                 log_common("read(STDIN) EOF\n");  
591                                                  stdin_read_wait = 0;                                                  stdin_read_wait = 0;
                                                 loop = 0;  
592                                                  break;                                                  break;
593                                          }                                          }
594                                          else                                          else if (errno == EINTR)
595                                          {                                          {
                                                 input_buf_len += ret;  
                                                 BBS_last_access_tm = time(0);  
596                                                  continue;                                                  continue;
597                                          }                                          }
598                                            else
599                                            {
600                                                    log_error("read(STDIN) error (%d)\n", errno);
601                                                    loop = 0;
602                                                    break;
603                                            }
604                                    }
605                                    else if (ret == 0) // broken pipe
606                                    {
607    #ifdef _DEBUG
608                                            log_error("read(STDIN) EOF\n");
609    #endif
610                                            stdin_read_wait = 0;
611                                            loop = 0;
612                                            break;
613                                    }
614                                    else
615                                    {
616                                            input_buf_len += ret;
617                                            BBS_last_access_tm = time(NULL);
618    
619                                            // Refresh current action while user input
620                                            if (user_online_update("BBS_NET") < 0)
621                                            {
622                                                    log_error("user_online_update(BBS_NET) error\n");
623                                            }
624    
625                                            continue;
626                                    }
627                            }
628                    }
629    
630                    if (sock_write_wait)
631                    {
632                            if (input_buf_offset < input_buf_len)
633                            {
634                                    ret = bbsnet_io_buf_conv(input_cd, input_buf, &input_buf_len, &input_buf_offset, input_conv, sizeof(input_conv), &input_conv_len);
635                                    if (ret < 0)
636                                    {
637                                            log_error("bbsnet_io_buf_conv(input, %d, %d, %d) error\n", input_buf_len, input_buf_offset, input_conv_len);
638                                  }                                  }
639                          }                          }
640    
641                          if (events[i].data.fd == sock || sock_write_wait) // EPOLLOUT                          while (input_conv_offset < input_conv_len && !SYS_server_exit)
642                          {                          {
643                                  sock_write_wait = 1;                                  ret = (int)write(sock, input_conv + input_conv_offset, (size_t)(input_conv_len - input_conv_offset));
644                                  while (input_buf_offset < input_buf_len && !SYS_server_exit)                                  if (ret < 0)
645                                  {                                  {
646                                          ret = (int)write(sock, input_buf + input_buf_offset, (size_t)(input_buf_len - input_buf_offset));                                          if (errno == EAGAIN || errno == EWOULDBLOCK)
                                         if (ret < 0)  
647                                          {                                          {
648                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)                                                  sock_write_wait = 0;
649                                                  {                                                  break;
                                                         sock_write_wait = 0;  
                                                         break;  
                                                 }  
                                                 else if (errno == EINTR)  
                                                 {  
                                                         continue;  
                                                 }  
                                                 else  
                                                 {  
                                                         log_error("write(socket) error (%d)\n", errno);  
                                                         loop = 0;  
                                                         break;  
                                                 }  
650                                          }                                          }
651                                          else if (ret == 0) // broken pipe                                          else if (errno == EINTR)
652                                          {                                          {
653                                                  log_common("write(socket) EOF\n");                                                  continue;
654                                                  sock_write_wait = 0;                                          }
655                                            else
656                                            {
657                                                    log_error("write(socket) error (%d)\n", errno);
658                                                  loop = 0;                                                  loop = 0;
659                                                  break;                                                  break;
660                                          }                                          }
661                                          else                                  }
662                                    else if (ret == 0) // broken pipe
663                                    {
664    #ifdef _DEBUG
665                                            log_error("write(socket) EOF\n");
666    #endif
667                                            sock_write_wait = 0;
668                                            loop = 0;
669                                            break;
670                                    }
671                                    else
672                                    {
673                                            input_conv_offset += ret;
674                                            if (input_conv_offset >= input_conv_len) // Output buffer complete
675                                          {                                          {
676                                                  input_buf_offset += ret;                                                  input_conv_offset = 0;
677                                                  if (input_buf_offset >= input_buf_len) // Output buffer complete                                                  input_conv_len = 0;
678                                                  {                                                  break;
                                                         input_buf_offset = 0;  
                                                         input_buf_len = 0;  
                                                         break;  
                                                 }  
                                                 continue;  
679                                          }                                          }
680                                            continue;
681                                  }                                  }
682                          }                          }
683                    }
684    
685                          if (events[i].data.fd == sock || sock_read_wait) // EPOLLIN                  if (sock_read_wait)
686                    {
687                            while (output_buf_len < sizeof(output_buf) && !SYS_server_exit)
688                          {                          {
689                                  sock_read_wait = 1;                                  ret = (int)read(sock, output_buf + output_buf_len, sizeof(output_buf) - (size_t)output_buf_len);
690                                  while (output_buf_len < sizeof(output_buf) && !SYS_server_exit)                                  if (ret < 0)
691                                  {                                  {
692                                          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)  
693                                          {                                          {
                                                 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");  
694                                                  sock_read_wait = 0;                                                  sock_read_wait = 0;
                                                 loop = 0;  
695                                                  break;                                                  break;
696                                          }                                          }
697                                          else                                          else if (errno == EINTR)
698                                          {                                          {
                                                 output_buf_len += ret;  
699                                                  continue;                                                  continue;
700                                          }                                          }
701                                            else
702                                            {
703                                                    log_error("read(socket) error (%d)\n", errno);
704                                                    loop = 0;
705                                                    break;
706                                            }
707                                    }
708                                    else if (ret == 0) // broken pipe
709                                    {
710    #ifdef _DEBUG
711                                            log_error("read(socket) EOF\n");
712    #endif
713                                            sock_read_wait = 0;
714                                            loop = 0;
715                                            break;
716                                    }
717                                    else
718                                    {
719                                            output_buf_len += ret;
720                                            continue;
721                                  }                                  }
722                          }                          }
723                    }
724    
725                          if (events[i].data.fd == STDOUT_FILENO || stdout_write_wait)                  if (stdout_write_wait)
726                    {
727                            if (output_buf_offset < output_buf_len)
728                          {                          {
729                                  stdout_write_wait = 1;                                  ret = bbsnet_io_buf_conv(output_cd, output_buf, &output_buf_len, &output_buf_offset, output_conv, sizeof(output_conv), &output_conv_len);
730                                  while (output_buf_offset < output_buf_len && !SYS_server_exit)                                  if (ret < 0)
731                                    {
732                                            log_error("bbsnet_io_buf_conv(output, %d, %d, %d) error\n", output_buf_len, output_buf_offset, output_conv_len);
733                                    }
734                            }
735    
736                            while (output_conv_offset < output_conv_len && !SYS_server_exit)
737                            {
738                                    if (SSH_v2)
739                                  {                                  {
740                                          if (SSH_v2)                                          ret = ssh_channel_write(SSH_channel, output_conv + output_conv_offset, (uint32_t)(output_conv_len - output_conv_offset));
741                                            if (ret == SSH_ERROR)
742                                          {                                          {
743                                                  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));
744                                                  if (ret == SSH_ERROR)                                                  loop = 0;
745                                                  {                                                  break;
                                                         log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session));  
                                                         loop = 0;  
                                                         break;  
                                                 }  
746                                          }                                          }
747                                          else                                  }
748                                    else
749                                    {
750                                            ret = (int)write(STDOUT_FILENO, output_conv + output_conv_offset, (size_t)(output_conv_len - output_conv_offset));
751                                    }
752                                    if (ret < 0)
753                                    {
754                                            if (errno == EAGAIN || errno == EWOULDBLOCK)
755                                          {                                          {
756                                                  ret = (int)write(STDOUT_FILENO, output_buf + output_buf_offset, (size_t)(output_buf_len - output_buf_offset));                                                  stdout_write_wait = 0;
757                                                    break;
758                                          }                                          }
759                                          if (ret < 0)                                          else if (errno == EINTR)
760                                          {                                          {
761                                                  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;  
                                                 }  
762                                          }                                          }
763                                          else if (ret == 0) // broken pipe                                          else
764                                          {                                          {
765                                                  log_common("write(STDOUT) EOF\n");                                                  log_error("write(STDOUT) error (%d)\n", errno);
                                                 stdout_write_wait = 0;  
766                                                  loop = 0;                                                  loop = 0;
767                                                  break;                                                  break;
768                                          }                                          }
769                                          else                                  }
770                                    else if (ret == 0) // broken pipe
771                                    {
772    #ifdef _DEBUG
773                                            log_error("write(STDOUT) EOF\n");
774    #endif
775                                            stdout_write_wait = 0;
776                                            loop = 0;
777                                            break;
778                                    }
779                                    else
780                                    {
781                                            output_conv_offset += ret;
782                                            if (output_conv_offset >= output_conv_len) // Output buffer complete
783                                          {                                          {
784                                                  output_buf_offset += ret;                                                  output_conv_offset = 0;
785                                                  if (output_buf_offset >= output_buf_len) // Output buffer complete                                                  output_conv_len = 0;
786                                                  {                                                  break;
                                                         output_buf_offset = 0;  
                                                         output_buf_len = 0;  
                                                         break;  
                                                 }  
                                                 continue;  
787                                          }                                          }
788                                            continue;
789                                  }                                  }
790                          }                          }
791                  }                  }
792          }          }
793    
794            iconv_close(input_cd);
795            iconv_close(output_cd);
796    
797  cleanup:  cleanup:
798          if (close(epollfd) < 0)          if (close(epollfd) < 0)
799          {          {
# Line 666  cleanup: Line 812  cleanup:
812                  log_error("Close socket failed\n");                  log_error("Close socket failed\n");
813          }          }
814    
815          t_used = time(0) - t_used;          t_used = time(NULL) - t_used;
816          tm_used = gmtime(&t_used);          tm_used = gmtime(&t_used);
817    
818          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 840  bbsnet_refresh()
840          moveto(22, 0);          moveto(22, 0);
841          prints(" ----------------------------------------------------------------------------- ");          prints(" ----------------------------------------------------------------------------- ");
842          moveto(23, 0);          moveto(23, 0);
843          prints(" [\x1b[1;32mCtrl+C\x1b[m]退出");          prints(" [\x1b[1;32mCtrl+C\x1b[m]閫鍑");
844    
845          iflush();          iflush();
846    
# Line 707  int bbsnet_selchange() Line 853  int bbsnet_selchange()
853    
854          moveto(20, 0);          moveto(20, 0);
855          clrtoeol();          clrtoeol();
856          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",
857                     bbsnet_conf[i].host2, bbsnet_conf[i].host1);                     bbsnet_conf[i].host2, bbsnet_conf[i].host1);
858          moveto(20, 79);          moveto(20, 79);
859          prints("|");          prints("|");
860          moveto(21, 0);          moveto(21, 0);
861          clrtoeol();          clrtoeol();
862          prints("|\x1b[1m连往:\x1b[1;33m%-20s", bbsnet_conf[i].ip);          prints("|\x1b[1m杩炲線:\x1b[1;33m%-20s", bbsnet_conf[i].ip);
863          if (bbsnet_conf[i].port != 23)          if (bbsnet_conf[i].port != 23)
864          {          {
865                  prints("  %d", bbsnet_conf[i].port);                  prints("  %d", bbsnet_conf[i].port);
# Line 732  int bbs_net() Line 878  int bbs_net()
878    
879          load_bbsnet_conf(CONF_BBSNET);          load_bbsnet_conf(CONF_BBSNET);
880    
881          BBS_last_access_tm = time(0);          BBS_last_access_tm = time(NULL);
882    
883          clearscr();          clearscr();
884          bbsnet_refresh();          bbsnet_refresh();
# Line 742  int bbs_net() Line 888  int bbs_net()
888          while (!SYS_server_exit)          while (!SYS_server_exit)
889          {          {
890                  ch = igetch(100);                  ch = igetch(100);
891    
892                  switch (ch)                  switch (ch)
893                  {                  {
894                  case KEY_NULL: // broken pipe                  case KEY_NULL: // broken pipe
# Line 749  int bbs_net() Line 896  int bbs_net()
896                  case Ctrl('C'): // user cancel                  case Ctrl('C'): // user cancel
897                          goto cleanup;                          goto cleanup;
898                  case KEY_TIMEOUT:                  case KEY_TIMEOUT:
899                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)
900                          {                          {
901                                  goto cleanup;                                  goto cleanup;
902                          }                          }
# Line 798  int bbs_net() Line 945  int bbs_net()
945                          bbsnet_selchange();                          bbsnet_selchange();
946                          break;                          break;
947                  }                  }
948                  BBS_last_access_tm = time(0);                  BBS_last_access_tm = time(NULL);
949          }          }
950    
951  cleanup:  cleanup:


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

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