/[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.56 by sysadm, Sun Jun 29 03:09:39 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>
33  #include <netdb.h>  #include <arpa/inet.h>
34    #include <libssh/libssh.h>
35    #include <libssh/server.h>
36    #include <libssh/callbacks.h>
37    #include <netinet/in.h>
38    #include <netinet/ip.h>
39  #include <sys/select.h>  #include <sys/select.h>
40  #include <sys/ioctl.h>  #include <sys/ioctl.h>
41  #include <sys/socket.h>  #include <sys/socket.h>
42  #include <sys/epoll.h>  #include <sys/epoll.h>
 #include <netinet/in.h>  
 #include <netinet/ip.h>  
 #include <arpa/inet.h>  
43    
44  #define MENU_CONF_DELIM " \t\r\n"  #define MENU_CONF_DELIM " \t\r\n"
45    
# Line 209  int bbsnet_connect(int n) Line 213  int bbsnet_connect(int n)
213          int tos;          int tos;
214          char remote_addr[IP_ADDR_LEN];          char remote_addr[IP_ADDR_LEN];
215          int remote_port;          int remote_port;
216          time_t t_used;          time_t t_used = time(NULL);
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 239  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 249  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 357  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 385  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 403  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 421  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 435  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 456  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");  #ifdef _DEBUG
500                                                    log_error("read(STDIN) EOF\n");
501    #endif
502                                                  stdin_read_wait = 0;                                                  stdin_read_wait = 0;
503                                                  loop = 0;                                                  loop = 0;
504                                                  break;                                                  break;
# Line 464  int bbsnet_connect(int n) Line 506  int bbsnet_connect(int n)
506                                          else                                          else
507                                          {                                          {
508                                                  input_buf_len += ret;                                                  input_buf_len += ret;
509                                                  BBS_last_access_tm = time(0);                                                  BBS_last_access_tm = time(NULL);
510                                                  continue;                                                  continue;
511                                          }                                          }
512                                  }                                  }
# Line 496  int bbsnet_connect(int n) Line 538  int bbsnet_connect(int n)
538                                          }                                          }
539                                          else if (ret == 0) // broken pipe                                          else if (ret == 0) // broken pipe
540                                          {                                          {
541                                                  log_std("write(socket) EOF\n");  #ifdef _DEBUG
542                                                    log_error("write(socket) EOF\n");
543    #endif
544                                                  sock_write_wait = 0;                                                  sock_write_wait = 0;
545                                                  loop = 0;                                                  loop = 0;
546                                                  break;                                                  break;
# Line 541  int bbsnet_connect(int n) Line 585  int bbsnet_connect(int n)
585                                          }                                          }
586                                          else if (ret == 0) // broken pipe                                          else if (ret == 0) // broken pipe
587                                          {                                          {
588                                                  log_std("read(socket) EOF\n");  #ifdef _DEBUG
589                                                    log_error("read(socket) EOF\n");
590    #endif
591                                                  sock_read_wait = 0;                                                  sock_read_wait = 0;
592                                                  loop = 0;                                                  loop = 0;
593                                                  break;                                                  break;
# Line 559  int bbsnet_connect(int n) Line 605  int bbsnet_connect(int n)
605                                  stdout_write_wait = 1;                                  stdout_write_wait = 1;
606                                  while (output_buf_offset < output_buf_len && !SYS_server_exit)                                  while (output_buf_offset < output_buf_len && !SYS_server_exit)
607                                  {                                  {
608                                          ret = (int)write(STDOUT_FILENO, output_buf + output_buf_offset, (size_t)(output_buf_len - output_buf_offset));                                          if (SSH_v2)
609                                            {
610                                                    ret = ssh_channel_write(SSH_channel, output_buf + output_buf_offset, (uint32_t)(output_buf_len - output_buf_offset));
611                                                    if (ret == SSH_ERROR)
612                                                    {
613                                                            log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session));
614                                                            loop = 0;
615                                                            break;
616                                                    }
617                                            }
618                                            else
619                                            {
620                                                    ret = (int)write(STDOUT_FILENO, output_buf + output_buf_offset, (size_t)(output_buf_len - output_buf_offset));
621                                            }
622                                          if (ret < 0)                                          if (ret < 0)
623                                          {                                          {
624                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)
# Line 580  int bbsnet_connect(int n) Line 639  int bbsnet_connect(int n)
639                                          }                                          }
640                                          else if (ret == 0) // broken pipe                                          else if (ret == 0) // broken pipe
641                                          {                                          {
642                                                  log_std("write(STDOUT) EOF\n");  #ifdef _DEBUG
643                                                    log_error("write(STDOUT) EOF\n");
644    #endif
645                                                  stdout_write_wait = 0;                                                  stdout_write_wait = 0;
646                                                  loop = 0;                                                  loop = 0;
647                                                  break;                                                  break;
# Line 619  cleanup: Line 680  cleanup:
680                  log_error("Close socket failed\n");                  log_error("Close socket failed\n");
681          }          }
682    
683          t_used = time(0) - t_used;          t_used = time(NULL) - t_used;
684          tm_used = gmtime(&t_used);          tm_used = gmtime(&t_used);
685    
686          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",
687                          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,
688                          tm_used->tm_sec);                             tm_used->tm_sec);
689    
690          return 0;          return 0;
691  }  }
# Line 654  bbsnet_refresh() Line 715  bbsnet_refresh()
715          return 0;          return 0;
716  }  }
717    
718  int bbsnet_selchange(int new_pos)  int bbsnet_selchange()
719  {  {
720            int i = bbsnet_menu.menu_item_pos[0];
721    
722          moveto(20, 0);          moveto(20, 0);
723          clrtoeol();          clrtoeol();
724          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",
725                     bbsnet_conf[new_pos].host2, bbsnet_conf[new_pos].host1);                     bbsnet_conf[i].host2, bbsnet_conf[i].host1);
726          moveto(20, 79);          moveto(20, 79);
727          prints("|");          prints("|");
728          moveto(21, 0);          moveto(21, 0);
729          clrtoeol();          clrtoeol();
730          prints("|\x1b[1m连往:\x1b[1;33m%-20s", bbsnet_conf[new_pos].ip);          prints("|\x1b[1m连往:\x1b[1;33m%-20s", bbsnet_conf[i].ip);
731          if (bbsnet_conf[new_pos].port != 23)          if (bbsnet_conf[i].port != 23)
732          {          {
733                  prints("  %d", bbsnet_conf[new_pos].port);                  prints("  %d", bbsnet_conf[i].port);
734          }          }
735          prints("\x1b[m");          prints("\x1b[m");
736          moveto(21, 79);          moveto(21, 79);
# Line 679  int bbsnet_selchange(int new_pos) Line 742  int bbsnet_selchange(int new_pos)
742    
743  int bbs_net()  int bbs_net()
744  {  {
745          int ch, pos, i;          int ch, i;
746    
747          load_bbsnet_conf(CONF_BBSNET);          load_bbsnet_conf(CONF_BBSNET);
748    
749          BBS_last_access_tm = time(0);          BBS_last_access_tm = time(NULL);
750    
751          clearscr();          clearscr();
752          bbsnet_refresh();          bbsnet_refresh();
         pos = bbsnet_menu.menu_item_pos[0];  
753          display_menu(&bbsnet_menu);          display_menu(&bbsnet_menu);
754          bbsnet_selchange(pos);          bbsnet_selchange();
755    
756          while (!SYS_server_exit)          while (!SYS_server_exit)
757          {          {
758                  ch = igetch(100);                  ch = igetch(100);
759    
760                  switch (ch)                  switch (ch)
761                  {                  {
762                  case KEY_NULL:  // broken pipe                  case KEY_NULL: // broken pipe
763                    case KEY_ESC:
764                  case Ctrl('C'): // user cancel                  case Ctrl('C'): // user cancel
765                          goto cleanup;                          goto cleanup;
766                  case KEY_TIMEOUT:                  case KEY_TIMEOUT:
767                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)
768                          {                          {
769                                  goto cleanup;                                  goto cleanup;
770                          }                          }
771                          continue;                          continue;
772                  case CR:                  case CR:
773                          igetch_reset();                          igetch_reset();
774                          pos = bbsnet_menu.menu_item_pos[0];                          bbsnet_connect(bbsnet_menu.menu_item_pos[0]);
                         bbsnet_connect(pos);  
775                          bbsnet_refresh();                          bbsnet_refresh();
776                          display_menu(&bbsnet_menu);                          display_menu(&bbsnet_menu);
777                          bbsnet_selchange(pos);                          bbsnet_selchange();
778                          break;                          break;
779                  case KEY_UP:                  case KEY_UP:
780                          for (i = 0; i < STATION_PER_LINE; i++)                          for (i = 0; i < STATION_PER_LINE; i++)
781                          {                          {
782                                  menu_control(&bbsnet_menu, KEY_UP);                                  menu_control(&bbsnet_menu, KEY_UP);
783                          }                          }
784                          pos = bbsnet_menu.menu_item_pos[0];                          bbsnet_selchange();
                         bbsnet_selchange(pos);  
785                          break;                          break;
786                  case KEY_DOWN:                  case KEY_DOWN:
787                          for (i = 0; i < STATION_PER_LINE; i++)                          for (i = 0; i < STATION_PER_LINE; i++)
788                          {                          {
789                                  menu_control(&bbsnet_menu, KEY_DOWN);                                  menu_control(&bbsnet_menu, KEY_DOWN);
790                          }                          }
791                          pos = bbsnet_menu.menu_item_pos[0];                          bbsnet_selchange();
                         bbsnet_selchange(pos);  
792                          break;                          break;
793                  case KEY_LEFT:                  case KEY_LEFT:
794                          menu_control(&bbsnet_menu, KEY_UP);                          menu_control(&bbsnet_menu, KEY_UP);
795                          pos = bbsnet_menu.menu_item_pos[0];                          bbsnet_selchange();
                         bbsnet_selchange(pos);  
796                          break;                          break;
797                  case KEY_RIGHT:                  case KEY_RIGHT:
798                          menu_control(&bbsnet_menu, KEY_DOWN);                          menu_control(&bbsnet_menu, KEY_DOWN);
799                          pos = bbsnet_menu.menu_item_pos[0];                          bbsnet_selchange();
800                          bbsnet_selchange(pos);                          break;
801                    case KEY_HOME:
802                    case KEY_PGUP:
803                            menu_control(&bbsnet_menu, KEY_PGUP);
804                            bbsnet_selchange();
805                            break;
806                    case KEY_END:
807                    case KEY_PGDN:
808                            menu_control(&bbsnet_menu, KEY_PGDN);
809                            bbsnet_selchange();
810                          break;                          break;
811                  default:                  default:
812                          menu_control(&bbsnet_menu, ch);                          menu_control(&bbsnet_menu, ch);
813                          pos = bbsnet_menu.menu_item_pos[0];                          bbsnet_selchange();
                         bbsnet_selchange(pos);  
814                          break;                          break;
815                  }                  }
816                  BBS_last_access_tm = time(0);                  BBS_last_access_tm = time(NULL);
817          }          }
818    
819  cleanup:  cleanup:


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

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