/[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.44 by sysadm, Thu May 15 09:15:52 2025 UTC Revision 1.52 by sysadm, Wed Jun 18 04:38:20 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 239  int bbsnet_connect(int n) Line 243  int bbsnet_connect(int n)
243          }          }
244    
245          sin.sin_family = AF_INET;          sin.sin_family = AF_INET;
246          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);
247          sin.sin_port = 0;          sin.sin_port = 0;
248    
249          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 253  int bbsnet_connect(int n)
253                  return -2;                  return -2;
254          }          }
255    
256          bzero(&sin, sizeof(sin));          memset(&sin, 0, sizeof(sin));
257          sin.sin_family = AF_INET;          sin.sin_family = AF_INET;
258          sin.sin_addr = *(struct in_addr *)p_host->h_addr_list[0];          sin.sin_addr = *(struct in_addr *)p_host->h_addr_list[0];
259          sin.sin_port = htons(bbsnet_conf[n].port);          sin.sin_port = htons(bbsnet_conf[n].port);
# Line 357  int bbsnet_connect(int n) Line 361  int bbsnet_connect(int n)
361                                  else if (events[i].data.fd == STDIN_FILENO)                                  else if (events[i].data.fd == STDIN_FILENO)
362                                  {                                  {
363                                          ch = igetch(0);                                          ch = igetch(0);
364                                          if (ch == Ctrl('C'))                                          if (ch == Ctrl('C') || ch == KEY_ESC)
365                                          {                                          {
366                                                  goto cleanup;                                                  goto cleanup;
367                                          }                                          }
# Line 385  int bbsnet_connect(int n) Line 389  int bbsnet_connect(int n)
389    
390          prints("\033[1;31m连接成功!\033[m\r\n");          prints("\033[1;31m连接成功!\033[m\r\n");
391          iflush();          iflush();
392          log_std("BBSNET connect to %s:%d\n", remote_addr, remote_port);          log_common("BBSNET connect to %s:%d\n", remote_addr, remote_port);
393    
394          ev.events = EPOLLIN | EPOLLOUT | EPOLLET;          ev.events = EPOLLIN | EPOLLOUT | EPOLLET;
395          ev.data.fd = sock;          ev.data.fd = sock;
# Line 403  int bbsnet_connect(int n) Line 407  int bbsnet_connect(int n)
407                  goto cleanup;                  goto cleanup;
408          }          }
409    
410          BBS_last_access_tm = t_used = time(0);          BBS_last_access_tm = t_used = time(NULL);
411          loop = 1;          loop = 1;
412    
413          while (loop && !SYS_server_exit)          while (loop && !SYS_server_exit)
414          {          {
415                    if (SSH_v2 && ssh_channel_is_closed(SSH_channel))
416                    {
417                            log_error("SSH channel is closed\n");
418                            loop = 0;
419                            break;
420                    }
421    
422                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second
423    
424                  if (nfds < 0)                  if (nfds < 0)
# Line 421  int bbsnet_connect(int n) Line 432  int bbsnet_connect(int n)
432                  }                  }
433                  else if (nfds == 0) // timeout                  else if (nfds == 0) // timeout
434                  {                  {
435                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)
436                          {                          {
437                                  break;                                  break;
438                          }                          }
# Line 435  int bbsnet_connect(int n) Line 446  int bbsnet_connect(int n)
446                                  stdin_read_wait = 1;                                  stdin_read_wait = 1;
447                                  while (input_buf_len < sizeof(input_buf) && !SYS_server_exit)                                  while (input_buf_len < sizeof(input_buf) && !SYS_server_exit)
448                                  {                                  {
449                                          ret = (int)read(STDIN_FILENO, input_buf + input_buf_len, sizeof(input_buf) - (size_t)input_buf_len);                                          if (SSH_v2)
450                                            {
451                                                    ret = ssh_channel_read_nonblocking(SSH_channel, input_buf + input_buf_len, sizeof(input_buf) - (uint32_t)input_buf_len, 0);
452                                                    if (ret == SSH_ERROR)
453                                                    {
454                                                            log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session));
455                                                            loop = 0;
456                                                            break;
457                                                    }
458                                                    else if (ret == SSH_EOF)
459                                                    {
460                                                            stdin_read_wait = 0;
461                                                            loop = 0;
462                                                            break;
463                                                    }
464                                                    else if (ret == 0)
465                                                    {
466                                                            stdin_read_wait = 0;
467                                                            break; // Check whether channel is still open
468                                                    }
469                                            }
470                                            else
471                                            {
472                                                    ret = (int)read(STDIN_FILENO, input_buf + input_buf_len, sizeof(input_buf) - (size_t)input_buf_len);
473                                            }
474                                          if (ret < 0)                                          if (ret < 0)
475                                          {                                          {
476                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)
# Line 456  int bbsnet_connect(int n) Line 491  int bbsnet_connect(int n)
491                                          }                                          }
492                                          else if (ret == 0) // broken pipe                                          else if (ret == 0) // broken pipe
493                                          {                                          {
494                                                  log_std("read(STDIN) EOF\n");                                                  log_common("read(STDIN) EOF\n");
495                                                  stdin_read_wait = 0;                                                  stdin_read_wait = 0;
496                                                  loop = 0;                                                  loop = 0;
497                                                  break;                                                  break;
# Line 464  int bbsnet_connect(int n) Line 499  int bbsnet_connect(int n)
499                                          else                                          else
500                                          {                                          {
501                                                  input_buf_len += ret;                                                  input_buf_len += ret;
502                                                  BBS_last_access_tm = time(0);                                                  BBS_last_access_tm = time(NULL);
503                                                  continue;                                                  continue;
504                                          }                                          }
505                                  }                                  }
# Line 496  int bbsnet_connect(int n) Line 531  int bbsnet_connect(int n)
531                                          }                                          }
532                                          else if (ret == 0) // broken pipe                                          else if (ret == 0) // broken pipe
533                                          {                                          {
534                                                  log_std("write(socket) EOF\n");                                                  log_common("write(socket) EOF\n");
535                                                  sock_write_wait = 0;                                                  sock_write_wait = 0;
536                                                  loop = 0;                                                  loop = 0;
537                                                  break;                                                  break;
# Line 541  int bbsnet_connect(int n) Line 576  int bbsnet_connect(int n)
576                                          }                                          }
577                                          else if (ret == 0) // broken pipe                                          else if (ret == 0) // broken pipe
578                                          {                                          {
579                                                  log_std("read(socket) EOF\n");  #ifdef _DEBUG
580                                                    log_error("read(socket) EOF\n");
581    #endif
582                                                  sock_read_wait = 0;                                                  sock_read_wait = 0;
583                                                  loop = 0;                                                  loop = 0;
584                                                  break;                                                  break;
# Line 559  int bbsnet_connect(int n) Line 596  int bbsnet_connect(int n)
596                                  stdout_write_wait = 1;                                  stdout_write_wait = 1;
597                                  while (output_buf_offset < output_buf_len && !SYS_server_exit)                                  while (output_buf_offset < output_buf_len && !SYS_server_exit)
598                                  {                                  {
599                                          ret = (int)write(STDOUT_FILENO, output_buf + output_buf_offset, (size_t)(output_buf_len - output_buf_offset));                                          if (SSH_v2)
600                                            {
601                                                    ret = ssh_channel_write(SSH_channel, output_buf + output_buf_offset, (uint32_t)(output_buf_len - output_buf_offset));
602                                                    if (ret == SSH_ERROR)
603                                                    {
604                                                            log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session));
605                                                            loop = 0;
606                                                            break;
607                                                    }
608                                            }
609                                            else
610                                            {
611                                                    ret = (int)write(STDOUT_FILENO, output_buf + output_buf_offset, (size_t)(output_buf_len - output_buf_offset));
612                                            }
613                                          if (ret < 0)                                          if (ret < 0)
614                                          {                                          {
615                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)
# Line 580  int bbsnet_connect(int n) Line 630  int bbsnet_connect(int n)
630                                          }                                          }
631                                          else if (ret == 0) // broken pipe                                          else if (ret == 0) // broken pipe
632                                          {                                          {
633                                                  log_std("write(STDOUT) EOF\n");  #ifdef _DEBUG
634                                                    log_error("write(STDOUT) EOF\n");
635    #endif
636                                                  stdout_write_wait = 0;                                                  stdout_write_wait = 0;
637                                                  loop = 0;                                                  loop = 0;
638                                                  break;                                                  break;
# Line 619  cleanup: Line 671  cleanup:
671                  log_error("Close socket failed\n");                  log_error("Close socket failed\n");
672          }          }
673    
674          t_used = time(0) - t_used;          t_used = time(NULL) - t_used;
675          tm_used = gmtime(&t_used);          tm_used = gmtime(&t_used);
676    
677          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",
678                          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,
679                          tm_used->tm_sec);                             tm_used->tm_sec);
680    
681          return 0;          return 0;
682  }  }
# Line 685  int bbs_net() Line 737  int bbs_net()
737    
738          load_bbsnet_conf(CONF_BBSNET);          load_bbsnet_conf(CONF_BBSNET);
739    
740          BBS_last_access_tm = time(0);          BBS_last_access_tm = time(NULL);
741    
742          clearscr();          clearscr();
743          bbsnet_refresh();          bbsnet_refresh();
# Line 695  int bbs_net() Line 747  int bbs_net()
747          while (!SYS_server_exit)          while (!SYS_server_exit)
748          {          {
749                  ch = igetch(100);                  ch = igetch(100);
750    
751                    if (user_online_update("BBS_NET") < 0)
752                    {
753                            log_error("user_online_update(BBS_NET) error\n");
754                    }
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                          }                          }
# Line 750  int bbs_net() Line 809  int bbs_net()
809                          bbsnet_selchange();                          bbsnet_selchange();
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