/[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.41 by sysadm, Thu May 15 05:14:57 2025 UTC Revision 1.53 by sysadm, Wed Jun 18 05:11:19 2025 UTC
# Line 20  Line 20 
20  #include "io.h"  #include "io.h"
21  #include "screen.h"  #include "screen.h"
22  #include "menu.h"  #include "menu.h"
23    #include "login.h"
24  #include <stdio.h>  #include <stdio.h>
25  #include <stdarg.h>  #include <stdarg.h>
26  #include <errno.h>  #include <errno.h>
# Line 36  Line 37 
37  #include <netinet/in.h>  #include <netinet/in.h>
38  #include <netinet/ip.h>  #include <netinet/ip.h>
39  #include <arpa/inet.h>  #include <arpa/inet.h>
40    #include <libssh/libssh.h>
41    #include <libssh/server.h>
42    #include <libssh/callbacks.h>
43    
44  #define MENU_CONF_DELIM " \t\r\n"  #define MENU_CONF_DELIM " \t\r\n"
45    
# Line 124  int load_bbsnet_conf(const char *file_co Line 128  int load_bbsnet_conf(const char *file_co
128                  p_menu_item->submenu = 0;                  p_menu_item->submenu = 0;
129                  p_menu_item->priv = 0;                  p_menu_item->priv = 0;
130                  p_menu_item->level = 0;                  p_menu_item->level = 0;
                 p_menu_item->display = 0;  
131                  p_menu_item->name[0] =                  p_menu_item->name[0] =
132                          (char)(menu_item_id < MAXSTATION / 2 ? 'A' + menu_item_id : 'a' + menu_item_id);                          (char)(menu_item_id < MAXSTATION / 2 ? 'A' + menu_item_id : 'a' + menu_item_id);
133                  p_menu_item->name[1] = '\0';                  p_menu_item->name[1] = '\0';
# Line 214  int bbsnet_connect(int n) Line 217  int bbsnet_connect(int n)
217          struct tm *tm_used;          struct tm *tm_used;
218          int ch;          int ch;
219    
220            if (user_online_update("BBS_NET") < 0)
221            {
222                    log_error("user_online_update(BBS_NET) error\n");
223            }
224    
225          clearscr();          clearscr();
226    
227          moveto(0, 0);          moveto(0, 0);
# Line 240  int bbsnet_connect(int n) Line 248  int bbsnet_connect(int n)
248          }          }
249    
250          sin.sin_family = AF_INET;          sin.sin_family = AF_INET;
251          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);
252          sin.sin_port = 0;          sin.sin_port = 0;
253    
254          if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0)          if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0)
# Line 250  int bbsnet_connect(int n) Line 258  int bbsnet_connect(int n)
258                  return -2;                  return -2;
259          }          }
260    
261          bzero(&sin, sizeof(sin));          memset(&sin, 0, sizeof(sin));
262          sin.sin_family = AF_INET;          sin.sin_family = AF_INET;
263          sin.sin_addr = *(struct in_addr *)p_host->h_addr_list[0];          sin.sin_addr = *(struct in_addr *)p_host->h_addr_list[0];
264          sin.sin_port = htons(bbsnet_conf[n].port);          sin.sin_port = htons(bbsnet_conf[n].port);
# Line 358  int bbsnet_connect(int n) Line 366  int bbsnet_connect(int n)
366                                  else if (events[i].data.fd == STDIN_FILENO)                                  else if (events[i].data.fd == STDIN_FILENO)
367                                  {                                  {
368                                          ch = igetch(0);                                          ch = igetch(0);
369                                          if (ch == Ctrl('C'))                                          if (ch == Ctrl('C') || ch == KEY_ESC)
370                                          {                                          {
371                                                  goto cleanup;                                                  goto cleanup;
372                                          }                                          }
# Line 386  int bbsnet_connect(int n) Line 394  int bbsnet_connect(int n)
394    
395          prints("\033[1;31m连接成功!\033[m\r\n");          prints("\033[1;31m连接成功!\033[m\r\n");
396          iflush();          iflush();
397          log_std("BBSNET connect to %s:%d\n", remote_addr, remote_port);          log_common("BBSNET connect to %s:%d\n", remote_addr, remote_port);
398    
399          ev.events = EPOLLIN | EPOLLOUT | EPOLLET;          ev.events = EPOLLIN | EPOLLOUT | EPOLLET;
400          ev.data.fd = sock;          ev.data.fd = sock;
# Line 404  int bbsnet_connect(int n) Line 412  int bbsnet_connect(int n)
412                  goto cleanup;                  goto cleanup;
413          }          }
414    
415          BBS_last_access_tm = t_used = time(0);          BBS_last_access_tm = t_used = time(NULL);
416          loop = 1;          loop = 1;
417    
418          while (loop && !SYS_server_exit)          while (loop && !SYS_server_exit)
419          {          {
420                    if (SSH_v2 && ssh_channel_is_closed(SSH_channel))
421                    {
422                            log_error("SSH channel is closed\n");
423                            loop = 0;
424                            break;
425                    }
426    
427                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second
428    
429                  if (nfds < 0)                  if (nfds < 0)
# Line 422  int bbsnet_connect(int n) Line 437  int bbsnet_connect(int n)
437                  }                  }
438                  else if (nfds == 0) // timeout                  else if (nfds == 0) // timeout
439                  {                  {
440                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)
441                          {                          {
442                                  break;                                  break;
443                          }                          }
# Line 436  int bbsnet_connect(int n) Line 451  int bbsnet_connect(int n)
451                                  stdin_read_wait = 1;                                  stdin_read_wait = 1;
452                                  while (input_buf_len < sizeof(input_buf) && !SYS_server_exit)                                  while (input_buf_len < sizeof(input_buf) && !SYS_server_exit)
453                                  {                                  {
454                                          ret = (int)read(STDIN_FILENO, input_buf + input_buf_len, sizeof(input_buf) - (size_t)input_buf_len);                                          if (SSH_v2)
455                                            {
456                                                    ret = ssh_channel_read_nonblocking(SSH_channel, input_buf + input_buf_len, sizeof(input_buf) - (uint32_t)input_buf_len, 0);
457                                                    if (ret == SSH_ERROR)
458                                                    {
459                                                            log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session));
460                                                            loop = 0;
461                                                            break;
462                                                    }
463                                                    else if (ret == SSH_EOF)
464                                                    {
465                                                            stdin_read_wait = 0;
466                                                            loop = 0;
467                                                            break;
468                                                    }
469                                                    else if (ret == 0)
470                                                    {
471                                                            stdin_read_wait = 0;
472                                                            break; // Check whether channel is still open
473                                                    }
474                                            }
475                                            else
476                                            {
477                                                    ret = (int)read(STDIN_FILENO, input_buf + input_buf_len, sizeof(input_buf) - (size_t)input_buf_len);
478                                            }
479                                          if (ret < 0)                                          if (ret < 0)
480                                          {                                          {
481                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)
# Line 457  int bbsnet_connect(int n) Line 496  int bbsnet_connect(int n)
496                                          }                                          }
497                                          else if (ret == 0) // broken pipe                                          else if (ret == 0) // broken pipe
498                                          {                                          {
499                                                  log_std("read(STDIN) EOF\n");                                                  log_common("read(STDIN) EOF\n");
500                                                  stdin_read_wait = 0;                                                  stdin_read_wait = 0;
501                                                  loop = 0;                                                  loop = 0;
502                                                  break;                                                  break;
# Line 465  int bbsnet_connect(int n) Line 504  int bbsnet_connect(int n)
504                                          else                                          else
505                                          {                                          {
506                                                  input_buf_len += ret;                                                  input_buf_len += ret;
507                                                  BBS_last_access_tm = time(0);                                                  BBS_last_access_tm = time(NULL);
508                                                  continue;                                                  continue;
509                                          }                                          }
510                                  }                                  }
# Line 497  int bbsnet_connect(int n) Line 536  int bbsnet_connect(int n)
536                                          }                                          }
537                                          else if (ret == 0) // broken pipe                                          else if (ret == 0) // broken pipe
538                                          {                                          {
539                                                  log_std("write(socket) EOF\n");                                                  log_common("write(socket) EOF\n");
540                                                  sock_write_wait = 0;                                                  sock_write_wait = 0;
541                                                  loop = 0;                                                  loop = 0;
542                                                  break;                                                  break;
# Line 542  int bbsnet_connect(int n) Line 581  int bbsnet_connect(int n)
581                                          }                                          }
582                                          else if (ret == 0) // broken pipe                                          else if (ret == 0) // broken pipe
583                                          {                                          {
584                                                  log_std("read(socket) EOF\n");  #ifdef _DEBUG
585                                                    log_error("read(socket) EOF\n");
586    #endif
587                                                  sock_read_wait = 0;                                                  sock_read_wait = 0;
588                                                  loop = 0;                                                  loop = 0;
589                                                  break;                                                  break;
# Line 560  int bbsnet_connect(int n) Line 601  int bbsnet_connect(int n)
601                                  stdout_write_wait = 1;                                  stdout_write_wait = 1;
602                                  while (output_buf_offset < output_buf_len && !SYS_server_exit)                                  while (output_buf_offset < output_buf_len && !SYS_server_exit)
603                                  {                                  {
604                                          ret = (int)write(STDOUT_FILENO, output_buf + output_buf_offset, (size_t)(output_buf_len - output_buf_offset));                                          if (SSH_v2)
605                                            {
606                                                    ret = ssh_channel_write(SSH_channel, output_buf + output_buf_offset, (uint32_t)(output_buf_len - output_buf_offset));
607                                                    if (ret == SSH_ERROR)
608                                                    {
609                                                            log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session));
610                                                            loop = 0;
611                                                            break;
612                                                    }
613                                            }
614                                            else
615                                            {
616                                                    ret = (int)write(STDOUT_FILENO, output_buf + output_buf_offset, (size_t)(output_buf_len - output_buf_offset));
617                                            }
618                                          if (ret < 0)                                          if (ret < 0)
619                                          {                                          {
620                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)
# Line 581  int bbsnet_connect(int n) Line 635  int bbsnet_connect(int n)
635                                          }                                          }
636                                          else if (ret == 0) // broken pipe                                          else if (ret == 0) // broken pipe
637                                          {                                          {
638                                                  log_std("write(STDOUT) EOF\n");  #ifdef _DEBUG
639                                                    log_error("write(STDOUT) EOF\n");
640    #endif
641                                                  stdout_write_wait = 0;                                                  stdout_write_wait = 0;
642                                                  loop = 0;                                                  loop = 0;
643                                                  break;                                                  break;
# Line 620  cleanup: Line 676  cleanup:
676                  log_error("Close socket failed\n");                  log_error("Close socket failed\n");
677          }          }
678    
679          t_used = time(0) - t_used;          t_used = time(NULL) - t_used;
680          tm_used = gmtime(&t_used);          tm_used = gmtime(&t_used);
681    
682          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",
683                          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,
684                          tm_used->tm_sec);                             tm_used->tm_sec);
685    
686          return 0;          return 0;
687  }  }
# Line 655  bbsnet_refresh() Line 711  bbsnet_refresh()
711          return 0;          return 0;
712  }  }
713    
714  int bbsnet_selchange(int new_pos)  int bbsnet_selchange()
715  {  {
716            int i = bbsnet_menu.menu_item_pos[0];
717    
718          moveto(20, 0);          moveto(20, 0);
719          clrtoeol();          clrtoeol();
720          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",
721                     bbsnet_conf[new_pos].host2, bbsnet_conf[new_pos].host1);                     bbsnet_conf[i].host2, bbsnet_conf[i].host1);
722          moveto(20, 79);          moveto(20, 79);
723          prints("|");          prints("|");
724          moveto(21, 0);          moveto(21, 0);
725          clrtoeol();          clrtoeol();
726          prints("|\x1b[1m连往:\x1b[1;33m%-20s", bbsnet_conf[new_pos].ip);          prints("|\x1b[1m连往:\x1b[1;33m%-20s", bbsnet_conf[i].ip);
727          if (bbsnet_conf[new_pos].port != 23)          if (bbsnet_conf[i].port != 23)
728          {          {
729                  prints("  %d", bbsnet_conf[new_pos].port);                  prints("  %d", bbsnet_conf[i].port);
730          }          }
731          prints("\x1b[m");          prints("\x1b[m");
732          moveto(21, 79);          moveto(21, 79);
# Line 680  int bbsnet_selchange(int new_pos) Line 738  int bbsnet_selchange(int new_pos)
738    
739  int bbs_net()  int bbs_net()
740  {  {
741          int ch, pos, i;          int ch, i;
742    
743          load_bbsnet_conf(CONF_BBSNET);          load_bbsnet_conf(CONF_BBSNET);
744    
745          BBS_last_access_tm = time(0);          BBS_last_access_tm = time(NULL);
746    
747          clearscr();          clearscr();
748          bbsnet_refresh();          bbsnet_refresh();
         pos = bbsnet_menu.menu_item_pos[0];  
749          display_menu(&bbsnet_menu);          display_menu(&bbsnet_menu);
750          bbsnet_selchange(pos);          bbsnet_selchange();
751    
752          while (!SYS_server_exit)          while (!SYS_server_exit)
753          {          {
754                  ch = igetch(100);                  ch = igetch(100);
755    
756                  switch (ch)                  switch (ch)
757                  {                  {
758                  case KEY_NULL:  // broken pipe                  case KEY_NULL: // broken pipe
759                    case KEY_ESC:
760                  case Ctrl('C'): // user cancel                  case Ctrl('C'): // user cancel
761                          goto cleanup;                          goto cleanup;
762                  case KEY_TIMEOUT:                  case KEY_TIMEOUT:
763                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)
764                          {                          {
765                                  goto cleanup;                                  goto cleanup;
766                          }                          }
767                          continue;                          continue;
768                  case CR:                  case CR:
769                          igetch_reset();                          igetch_reset();
770                          pos = bbsnet_menu.menu_item_pos[0];                          bbsnet_connect(bbsnet_menu.menu_item_pos[0]);
                         bbsnet_connect(pos);  
771                          bbsnet_refresh();                          bbsnet_refresh();
772                          display_menu(&bbsnet_menu);                          display_menu(&bbsnet_menu);
773                          bbsnet_selchange(pos);                          bbsnet_selchange();
774                          break;                          break;
775                  case KEY_UP:                  case KEY_UP:
776                          for (i = 0; i < STATION_PER_LINE; i++)                          for (i = 0; i < STATION_PER_LINE; i++)
777                          {                          {
778                                  menu_control(&bbsnet_menu, KEY_UP);                                  menu_control(&bbsnet_menu, KEY_UP);
779                          }                          }
780                          pos = bbsnet_menu.menu_item_pos[0];                          bbsnet_selchange();
                         bbsnet_selchange(pos);  
781                          break;                          break;
782                  case KEY_DOWN:                  case KEY_DOWN:
783                          for (i = 0; i < STATION_PER_LINE; i++)                          for (i = 0; i < STATION_PER_LINE; i++)
784                          {                          {
785                                  menu_control(&bbsnet_menu, KEY_DOWN);                                  menu_control(&bbsnet_menu, KEY_DOWN);
786                          }                          }
787                          pos = bbsnet_menu.menu_item_pos[0];                          bbsnet_selchange();
                         bbsnet_selchange(pos);  
788                          break;                          break;
789                  case KEY_LEFT:                  case KEY_LEFT:
790                          menu_control(&bbsnet_menu, KEY_UP);                          menu_control(&bbsnet_menu, KEY_UP);
791                          pos = bbsnet_menu.menu_item_pos[0];                          bbsnet_selchange();
                         bbsnet_selchange(pos);  
792                          break;                          break;
793                  case KEY_RIGHT:                  case KEY_RIGHT:
794                          menu_control(&bbsnet_menu, KEY_DOWN);                          menu_control(&bbsnet_menu, KEY_DOWN);
795                          pos = bbsnet_menu.menu_item_pos[0];                          bbsnet_selchange();
796                          bbsnet_selchange(pos);                          break;
797                    case KEY_HOME:
798                    case KEY_PGUP:
799                            menu_control(&bbsnet_menu, KEY_PGUP);
800                            bbsnet_selchange();
801                            break;
802                    case KEY_END:
803                    case KEY_PGDN:
804                            menu_control(&bbsnet_menu, KEY_PGDN);
805                            bbsnet_selchange();
806                          break;                          break;
807                  default:                  default:
808                          menu_control(&bbsnet_menu, ch);                          menu_control(&bbsnet_menu, ch);
809                          pos = bbsnet_menu.menu_item_pos[0];                          bbsnet_selchange();
                         bbsnet_selchange(pos);  
810                          break;                          break;
811                  }                  }
812                  BBS_last_access_tm = time(0);                  BBS_last_access_tm = time(NULL);
813          }          }
814    
815  cleanup:  cleanup:


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

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