/[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.42 by sysadm, Thu May 15 06:24:11 2025 UTC Revision 1.49 by sysadm, Mon Jun 16 14:30:44 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 357  int bbsnet_connect(int n) Line 362  int bbsnet_connect(int n)
362                                  else if (events[i].data.fd == STDIN_FILENO)                                  else if (events[i].data.fd == STDIN_FILENO)
363                                  {                                  {
364                                          ch = igetch(0);                                          ch = igetch(0);
365                                          if (ch == Ctrl('C'))                                          if (ch == Ctrl('C') || ch == KEY_ESC)
366                                          {                                          {
367                                                  goto cleanup;                                                  goto cleanup;
368                                          }                                          }
# 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 408  int bbsnet_connect(int n) Line 413  int bbsnet_connect(int n)
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 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 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 622  cleanup: Line 671  cleanup:
671          t_used = time(0) - t_used;          t_used = time(0) - 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 654  bbsnet_refresh() Line 703  bbsnet_refresh()
703          return 0;          return 0;
704  }  }
705    
706  int bbsnet_selchange(int new_pos)  int bbsnet_selchange()
707  {  {
708            int i = bbsnet_menu.menu_item_pos[0];
709    
710          moveto(20, 0);          moveto(20, 0);
711          clrtoeol();          clrtoeol();
712          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",
713                     bbsnet_conf[new_pos].host2, bbsnet_conf[new_pos].host1);                     bbsnet_conf[i].host2, bbsnet_conf[i].host1);
714          moveto(20, 79);          moveto(20, 79);
715          prints("|");          prints("|");
716          moveto(21, 0);          moveto(21, 0);
717          clrtoeol();          clrtoeol();
718          prints("|\x1b[1m连往:\x1b[1;33m%-20s", bbsnet_conf[new_pos].ip);          prints("|\x1b[1m连往:\x1b[1;33m%-20s", bbsnet_conf[i].ip);
719          if (bbsnet_conf[new_pos].port != 23)          if (bbsnet_conf[i].port != 23)
720          {          {
721                  prints("  %d", bbsnet_conf[new_pos].port);                  prints("  %d", bbsnet_conf[i].port);
722          }          }
723          prints("\x1b[m");          prints("\x1b[m");
724          moveto(21, 79);          moveto(21, 79);
# Line 679  int bbsnet_selchange(int new_pos) Line 730  int bbsnet_selchange(int new_pos)
730    
731  int bbs_net()  int bbs_net()
732  {  {
733          int ch, pos, i;          int ch, i;
734    
735          load_bbsnet_conf(CONF_BBSNET);          load_bbsnet_conf(CONF_BBSNET);
736    
# Line 687  int bbs_net() Line 738  int bbs_net()
738    
739          clearscr();          clearscr();
740          bbsnet_refresh();          bbsnet_refresh();
         pos = bbsnet_menu.menu_item_pos[0];  
741          display_menu(&bbsnet_menu);          display_menu(&bbsnet_menu);
742          bbsnet_selchange(pos);          bbsnet_selchange();
743    
744          while (!SYS_server_exit)          while (!SYS_server_exit)
745          {          {
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:
751                  case Ctrl('C'): // user cancel                  case Ctrl('C'): // user cancel
752                          goto cleanup;                          goto cleanup;
753                  case KEY_TIMEOUT:                  case KEY_TIMEOUT:
# Line 707  int bbs_net() Line 758  int bbs_net()
758                          continue;                          continue;
759                  case CR:                  case CR:
760                          igetch_reset();                          igetch_reset();
761                          pos = bbsnet_menu.menu_item_pos[0];                          bbsnet_connect(bbsnet_menu.menu_item_pos[0]);
                         bbsnet_connect(pos);  
762                          bbsnet_refresh();                          bbsnet_refresh();
763                          display_menu(&bbsnet_menu);                          display_menu(&bbsnet_menu);
764                          bbsnet_selchange(pos);                          bbsnet_selchange();
765                          break;                          break;
766                  case KEY_UP:                  case KEY_UP:
767                          for (i = 0; i < STATION_PER_LINE; i++)                          for (i = 0; i < STATION_PER_LINE; i++)
768                          {                          {
769                                  menu_control(&bbsnet_menu, KEY_UP);                                  menu_control(&bbsnet_menu, KEY_UP);
770                          }                          }
771                          pos = bbsnet_menu.menu_item_pos[0];                          bbsnet_selchange();
                         bbsnet_selchange(pos);  
772                          break;                          break;
773                  case KEY_DOWN:                  case KEY_DOWN:
774                          for (i = 0; i < STATION_PER_LINE; i++)                          for (i = 0; i < STATION_PER_LINE; i++)
775                          {                          {
776                                  menu_control(&bbsnet_menu, KEY_DOWN);                                  menu_control(&bbsnet_menu, KEY_DOWN);
777                          }                          }
778                          pos = bbsnet_menu.menu_item_pos[0];                          bbsnet_selchange();
                         bbsnet_selchange(pos);  
779                          break;                          break;
780                  case KEY_LEFT:                  case KEY_LEFT:
781                          menu_control(&bbsnet_menu, KEY_UP);                          menu_control(&bbsnet_menu, KEY_UP);
782                          pos = bbsnet_menu.menu_item_pos[0];                          bbsnet_selchange();
                         bbsnet_selchange(pos);  
783                          break;                          break;
784                  case KEY_RIGHT:                  case KEY_RIGHT:
785                          menu_control(&bbsnet_menu, KEY_DOWN);                          menu_control(&bbsnet_menu, KEY_DOWN);
786                          pos = bbsnet_menu.menu_item_pos[0];                          bbsnet_selchange();
787                          bbsnet_selchange(pos);                          break;
788                    case KEY_HOME:
789                    case KEY_PGUP:
790                            menu_control(&bbsnet_menu, KEY_PGUP);
791                            bbsnet_selchange();
792                            break;
793                    case KEY_END:
794                    case KEY_PGDN:
795                            menu_control(&bbsnet_menu, KEY_PGDN);
796                            bbsnet_selchange();
797                          break;                          break;
798                  default:                  default:
799                          menu_control(&bbsnet_menu, ch);                          menu_control(&bbsnet_menu, ch);
800                          pos = bbsnet_menu.menu_item_pos[0];                          bbsnet_selchange();
                         bbsnet_selchange(pos);  
801                          break;                          break;
802                  }                  }
803                  BBS_last_access_tm = time(0);                  BBS_last_access_tm = time(0);


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

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