/[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.45 by sysadm, Wed May 28 10:26:21 2025 UTC Revision 1.50 by sysadm, Tue Jun 17 02:06:48 2025 UTC
# Line 14  Line 14 
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
17    #define _POSIX_C_SOURCE 200809L
18    
19  #include "bbs.h"  #include "bbs.h"
20  #include "common.h"  #include "common.h"
21  #include "log.h"  #include "log.h"
# Line 36  Line 38 
38  #include <netinet/in.h>  #include <netinet/in.h>
39  #include <netinet/ip.h>  #include <netinet/ip.h>
40  #include <arpa/inet.h>  #include <arpa/inet.h>
41    #include <libssh/libssh.h>
42    #include <libssh/server.h>
43    #include <libssh/callbacks.h>
44    
45  #define MENU_CONF_DELIM " \t\r\n"  #define MENU_CONF_DELIM " \t\r\n"
46    
# Line 239  int bbsnet_connect(int n) Line 244  int bbsnet_connect(int n)
244          }          }
245    
246          sin.sin_family = AF_INET;          sin.sin_family = AF_INET;
247          sin.sin_addr.s_addr = (hostaddr_server[0] != '\0' ? inet_addr(hostaddr_server) : INADDR_ANY);          sin.sin_addr.s_addr = (BBS_address[0] != '\0' ? inet_addr(BBS_address) : INADDR_ANY);
248          sin.sin_port = 0;          sin.sin_port = 0;
249    
250          if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0)          if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0)
# Line 249  int bbsnet_connect(int n) Line 254  int bbsnet_connect(int n)
254                  return -2;                  return -2;
255          }          }
256    
257          bzero(&sin, sizeof(sin));          memset(&sin, 0, sizeof(sin));
258          sin.sin_family = AF_INET;          sin.sin_family = AF_INET;
259          sin.sin_addr = *(struct in_addr *)p_host->h_addr_list[0];          sin.sin_addr = *(struct in_addr *)p_host->h_addr_list[0];
260          sin.sin_port = htons(bbsnet_conf[n].port);          sin.sin_port = htons(bbsnet_conf[n].port);
# Line 385  int bbsnet_connect(int n) Line 390  int bbsnet_connect(int n)
390    
391          prints("\033[1;31m连接成功!\033[m\r\n");          prints("\033[1;31m连接成功!\033[m\r\n");
392          iflush();          iflush();
393          log_std("BBSNET connect to %s:%d\n", remote_addr, remote_port);          log_common("BBSNET connect to %s:%d\n", remote_addr, remote_port);
394    
395          ev.events = EPOLLIN | EPOLLOUT | EPOLLET;          ev.events = EPOLLIN | EPOLLOUT | EPOLLET;
396          ev.data.fd = sock;          ev.data.fd = sock;
# Line 403  int bbsnet_connect(int n) Line 408  int bbsnet_connect(int n)
408                  goto cleanup;                  goto cleanup;
409          }          }
410    
411          BBS_last_access_tm = t_used = time(0);          BBS_last_access_tm = t_used = time(NULL);
412          loop = 1;          loop = 1;
413    
414          while (loop && !SYS_server_exit)          while (loop && !SYS_server_exit)
415          {          {
416                    if (SSH_v2 && ssh_channel_is_closed(SSH_channel))
417                    {
418                            log_error("SSH channel is closed\n");
419                            loop = 0;
420                            break;
421                    }
422    
423                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second
424    
425                  if (nfds < 0)                  if (nfds < 0)
# Line 421  int bbsnet_connect(int n) Line 433  int bbsnet_connect(int n)
433                  }                  }
434                  else if (nfds == 0) // timeout                  else if (nfds == 0) // timeout
435                  {                  {
436                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)
437                          {                          {
438                                  break;                                  break;
439                          }                          }
# Line 435  int bbsnet_connect(int n) Line 447  int bbsnet_connect(int n)
447                                  stdin_read_wait = 1;                                  stdin_read_wait = 1;
448                                  while (input_buf_len < sizeof(input_buf) && !SYS_server_exit)                                  while (input_buf_len < sizeof(input_buf) && !SYS_server_exit)
449                                  {                                  {
450                                          ret = (int)read(STDIN_FILENO, input_buf + input_buf_len, sizeof(input_buf) - (size_t)input_buf_len);                                          if (SSH_v2)
451                                            {
452                                                    ret = ssh_channel_read_nonblocking(SSH_channel, input_buf + input_buf_len, sizeof(input_buf) - (uint32_t)input_buf_len, 0);
453                                                    if (ret == SSH_ERROR)
454                                                    {
455                                                            log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session));
456                                                            loop = 0;
457                                                            break;
458                                                    }
459                                                    else if (ret == SSH_EOF)
460                                                    {
461                                                            stdin_read_wait = 0;
462                                                            loop = 0;
463                                                            break;
464                                                    }
465                                                    else if (ret == 0)
466                                                    {
467                                                            stdin_read_wait = 0;
468                                                            break; // Check whether channel is still open
469                                                    }
470                                            }
471                                            else
472                                            {
473                                                    ret = (int)read(STDIN_FILENO, input_buf + input_buf_len, sizeof(input_buf) - (size_t)input_buf_len);
474                                            }
475                                          if (ret < 0)                                          if (ret < 0)
476                                          {                                          {
477                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)
# Line 456  int bbsnet_connect(int n) Line 492  int bbsnet_connect(int n)
492                                          }                                          }
493                                          else if (ret == 0) // broken pipe                                          else if (ret == 0) // broken pipe
494                                          {                                          {
495                                                  log_std("read(STDIN) EOF\n");                                                  log_common("read(STDIN) EOF\n");
496                                                  stdin_read_wait = 0;                                                  stdin_read_wait = 0;
497                                                  loop = 0;                                                  loop = 0;
498                                                  break;                                                  break;
# Line 464  int bbsnet_connect(int n) Line 500  int bbsnet_connect(int n)
500                                          else                                          else
501                                          {                                          {
502                                                  input_buf_len += ret;                                                  input_buf_len += ret;
503                                                  BBS_last_access_tm = time(0);                                                  BBS_last_access_tm = time(NULL);
504                                                  continue;                                                  continue;
505                                          }                                          }
506                                  }                                  }
# Line 496  int bbsnet_connect(int n) Line 532  int bbsnet_connect(int n)
532                                          }                                          }
533                                          else if (ret == 0) // broken pipe                                          else if (ret == 0) // broken pipe
534                                          {                                          {
535                                                  log_std("write(socket) EOF\n");                                                  log_common("write(socket) EOF\n");
536                                                  sock_write_wait = 0;                                                  sock_write_wait = 0;
537                                                  loop = 0;                                                  loop = 0;
538                                                  break;                                                  break;
# Line 541  int bbsnet_connect(int n) Line 577  int bbsnet_connect(int n)
577                                          }                                          }
578                                          else if (ret == 0) // broken pipe                                          else if (ret == 0) // broken pipe
579                                          {                                          {
580                                                  log_std("read(socket) EOF\n");                                                  log_common("read(socket) EOF\n");
581                                                  sock_read_wait = 0;                                                  sock_read_wait = 0;
582                                                  loop = 0;                                                  loop = 0;
583                                                  break;                                                  break;
# Line 559  int bbsnet_connect(int n) Line 595  int bbsnet_connect(int n)
595                                  stdout_write_wait = 1;                                  stdout_write_wait = 1;
596                                  while (output_buf_offset < output_buf_len && !SYS_server_exit)                                  while (output_buf_offset < output_buf_len && !SYS_server_exit)
597                                  {                                  {
598                                          ret = (int)write(STDOUT_FILENO, output_buf + output_buf_offset, (size_t)(output_buf_len - output_buf_offset));                                          if (SSH_v2)
599                                            {
600                                                    ret = ssh_channel_write(SSH_channel, output_buf + output_buf_offset, (uint32_t)(output_buf_len - output_buf_offset));
601                                                    if (ret == SSH_ERROR)
602                                                    {
603                                                            log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session));
604                                                            loop = 0;
605                                                            break;
606                                                    }
607                                            }
608                                            else
609                                            {
610                                                    ret = (int)write(STDOUT_FILENO, output_buf + output_buf_offset, (size_t)(output_buf_len - output_buf_offset));
611                                            }
612                                          if (ret < 0)                                          if (ret < 0)
613                                          {                                          {
614                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)
# Line 580  int bbsnet_connect(int n) Line 629  int bbsnet_connect(int n)
629                                          }                                          }
630                                          else if (ret == 0) // broken pipe                                          else if (ret == 0) // broken pipe
631                                          {                                          {
632                                                  log_std("write(STDOUT) EOF\n");                                                  log_common("write(STDOUT) EOF\n");
633                                                  stdout_write_wait = 0;                                                  stdout_write_wait = 0;
634                                                  loop = 0;                                                  loop = 0;
635                                                  break;                                                  break;
# Line 619  cleanup: Line 668  cleanup:
668                  log_error("Close socket failed\n");                  log_error("Close socket failed\n");
669          }          }
670    
671          t_used = time(0) - t_used;          t_used = time(NULL) - t_used;
672          tm_used = gmtime(&t_used);          tm_used = gmtime(&t_used);
673    
674          log_std("BBSNET disconnect, %d days %d hours %d minutes %d seconds used\n",          log_common("BBSNET disconnect, %d days %d hours %d minutes %d seconds used\n",
675                          tm_used->tm_mday - 1, tm_used->tm_hour, tm_used->tm_min,                             tm_used->tm_mday - 1, tm_used->tm_hour, tm_used->tm_min,
676                          tm_used->tm_sec);                             tm_used->tm_sec);
677    
678          return 0;          return 0;
679  }  }
# Line 685  int bbs_net() Line 734  int bbs_net()
734    
735          load_bbsnet_conf(CONF_BBSNET);          load_bbsnet_conf(CONF_BBSNET);
736    
737          BBS_last_access_tm = time(0);          BBS_last_access_tm = time(NULL);
738    
739          clearscr();          clearscr();
740          bbsnet_refresh();          bbsnet_refresh();
# Line 697  int bbs_net() Line 746  int bbs_net()
746                  ch = igetch(100);                  ch = igetch(100);
747                  switch (ch)                  switch (ch)
748                  {                  {
749                  case KEY_NULL:  // broken pipe                  case KEY_NULL: // broken pipe
750                  case KEY_ESC:                  case KEY_ESC:
751                  case Ctrl('C'): // user cancel                  case Ctrl('C'): // user cancel
752                          goto cleanup;                          goto cleanup;
753                  case KEY_TIMEOUT:                  case KEY_TIMEOUT:
754                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)
755                          {                          {
756                                  goto cleanup;                                  goto cleanup;
757                          }                          }
# Line 751  int bbs_net() Line 800  int bbs_net()
800                          bbsnet_selchange();                          bbsnet_selchange();
801                          break;                          break;
802                  }                  }
803                  BBS_last_access_tm = time(0);                  BBS_last_access_tm = time(NULL);
804          }          }
805    
806  cleanup:  cleanup:


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

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