/[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.91 by sysadm, Wed Dec 17 03:47:00 2025 UTC Revision 1.94 by sysadm, Thu Dec 18 02:17:39 2025 UTC
# Line 62  struct _bbsnet_conf Line 62  struct _bbsnet_conf
62          char org_name[40];          char org_name[40];
63          char site_name[40];          char site_name[40];
64          char host_name[IP_ADDR_LEN];          char host_name[IP_ADDR_LEN];
65          in_port_t port;          char port[6];
66          int8_t use_ssh;          int8_t use_ssh;
67          char charset[CHARSET_MAX_LEN + 1];          char charset[CHARSET_MAX_LEN + 1];
68  } bbsnet_conf[MAXSTATION];  } bbsnet_conf[MAXSTATION];
# Line 143  static int load_bbsnet_conf(const char * Line 143  static int load_bbsnet_conf(const char *
143                          unload_bbsnet_conf();                          unload_bbsnet_conf();
144                          return -3;                          return -3;
145                  }                  }
146                  bbsnet_conf[menu_item_id].port = (in_port_t)port;                  strncpy(bbsnet_conf[menu_item_id].port, t4, sizeof(bbsnet_conf[menu_item_id].port) - 1);
147                    bbsnet_conf[menu_item_id].port[sizeof(bbsnet_conf[menu_item_id].port) - 1] = '\0';
148                  bbsnet_conf[menu_item_id].use_ssh = (toupper(t5[0]) == 'Y');                  bbsnet_conf[menu_item_id].use_ssh = (toupper(t5[0]) == 'Y');
149                  strncpy(bbsnet_conf[menu_item_id].charset, t6, sizeof(bbsnet_conf[menu_item_id].charset) - 1);                  strncpy(bbsnet_conf[menu_item_id].charset, t6, sizeof(bbsnet_conf[menu_item_id].charset) - 1);
150                  bbsnet_conf[menu_item_id].charset[sizeof(bbsnet_conf[menu_item_id].charset) - 1] = '\0';                  bbsnet_conf[menu_item_id].charset[sizeof(bbsnet_conf[menu_item_id].charset) - 1] = '\0';
# Line 273  static int bbsnet_connect(int n) Line 274  static int bbsnet_connect(int n)
274          int stdout_write_wait = 0;          int stdout_write_wait = 0;
275          int sock_read_wait = 0;          int sock_read_wait = 0;
276          int sock_write_wait = 0;          int sock_write_wait = 0;
277          struct hostent *p_host = NULL;          struct addrinfo hints, *res = NULL;
278          int tos;          int tos;
279          char remote_addr[IP_ADDR_LEN];          char remote_addr[INET_ADDRSTRLEN];
280          int remote_port;          int remote_port;
281          char local_addr[IP_ADDR_LEN];          char local_addr[INET_ADDRSTRLEN];
282          int local_port;          int local_port;
283          socklen_t sock_len;          socklen_t sock_len;
284          time_t t_begin;          time_t t_begin;
# Line 342  static int bbsnet_connect(int n) Line 343  static int bbsnet_connect(int n)
343                     bbsnet_conf[n].site_name, bbsnet_conf[n].host_name);                     bbsnet_conf[n].site_name, bbsnet_conf[n].host_name);
344          iflush();          iflush();
345    
346          p_host = gethostbyname(bbsnet_conf[n].host_name);          memset(&hints, 0, sizeof(hints));
347            hints.ai_family = AF_INET;
348            hints.ai_socktype = SOCK_STREAM;
349            hints.ai_protocol = IPPROTO_TCP;
350    
351          if (p_host == NULL)          if ((ret = getaddrinfo(BBS_address, NULL, &hints, &res)) != 0)
352          {          {
353                  prints("\033[1;31m查找主机名失败!\033[m\r\n");                  log_error("getaddrinfo() error (%d)\n", ret);
                 press_any_key();  
354                  goto cleanup;                  goto cleanup;
355          }          }
356    
357          sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);          if (inet_ntop(AF_INET, &(((struct sockaddr_in *)res->ai_addr)->sin_addr.s_addr), local_addr, sizeof(local_addr)) == NULL)
358            {
359                    log_error("inet_ntop() error (%d)\n", errno);
360                    goto cleanup;
361            }
362            local_port = ntohs(((struct sockaddr_in *)res->ai_addr)->sin_port);
363    
364            sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
365          if (sock < 0)          if (sock < 0)
366          {          {
367                  prints("\033[1;31m无法创建socket!\033[m\r\n");                  log_error("socket() error (%d)\n", errno);
                 press_any_key();  
368                  goto cleanup;                  goto cleanup;
369          }          }
370    
371          sin.sin_family = AF_INET;          if (bind(sock, res->ai_addr, res->ai_addrlen) < 0)
         sin.sin_addr.s_addr = (BBS_address[0] != '\0' ? inet_addr(BBS_address) : INADDR_ANY);  
         sin.sin_port = 0;  
   
         if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0)  
372          {          {
373                  log_error("Bind address %s:%u failed (%d)\n",                  log_error("bind(%s:%u) error (%d)\n", local_addr, local_port, errno);
                                   inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), errno);  
374                  goto cleanup;                  goto cleanup;
375          }          }
376    
377          memset(&sin, 0, sizeof(sin));          freeaddrinfo(res);
378          sin.sin_family = AF_INET;          res = NULL;
         sin.sin_addr = *(struct in_addr *)p_host->h_addr_list[0];  
         sin.sin_port = htons(bbsnet_conf[n].port);  
379    
380          strncpy(remote_addr, inet_ntoa(sin.sin_addr), sizeof(remote_addr) - 1);          memset(&hints, 0, sizeof(hints));
381          remote_addr[sizeof(remote_addr) - 1] = '\0';          hints.ai_family = AF_INET;
382          remote_port = ntohs(sin.sin_port);          hints.ai_flags = AI_NUMERICSERV;
383            hints.ai_socktype = SOCK_STREAM;
384            hints.ai_protocol = IPPROTO_TCP;
385    
386            if ((ret = getaddrinfo(bbsnet_conf[n].host_name, bbsnet_conf[n].port, &hints, &res)) != 0)
387            {
388                    log_error("getaddrinfo() error (%d)\n", ret);
389                    prints("\033[1;31m查找主机名失败!\033[m\r\n");
390                    press_any_key();
391                    goto cleanup;
392            }
393    
394            if (inet_ntop(AF_INET, &(((struct sockaddr_in *)res->ai_addr)->sin_addr.s_addr), remote_addr, sizeof(remote_addr)) == NULL)
395            {
396                    log_error("inet_ntop() error (%d)\n", errno);
397                    goto cleanup;
398            }
399            remote_port = ntohs(((struct sockaddr_in *)res->ai_addr)->sin_port);
400    
401          prints("\033[1;32m穿梭进度条提示您当前已使用的时间,按\033[1;33mCtrl+C\033[1;32m中断。\033[m\r\n");          prints("\033[1;32m穿梭进度条提示您当前已使用的时间,按\033[1;33mCtrl+C\033[1;32m中断。\033[m\r\n");
402          process_bar(0, MAX_PROCESS_BAR_LEN);          process_bar(0, MAX_PROCESS_BAR_LEN);
# Line 444  static int bbsnet_connect(int n) Line 462  static int bbsnet_connect(int n)
462    
463          while (!SYS_server_exit)          while (!SYS_server_exit)
464          {          {
465                  if ((ret = connect(sock, (struct sockaddr *)&sin, sizeof(sin))) < 0)                  if ((ret = connect(sock, res->ai_addr, res->ai_addrlen)) < 0)
466                  {                  {
467                          if (errno == EAGAIN || errno == EALREADY || errno == EINPROGRESS)                          if (errno == EAGAIN || errno == EALREADY || errno == EINPROGRESS)
468                          {                          {
# Line 460  static int bbsnet_connect(int n) Line 478  static int bbsnet_connect(int n)
478                          else                          else
479                          {                          {
480                                  log_error("connect(socket) error (%d)\n", errno);                                  log_error("connect(socket) error (%d)\n", errno);
   
481                                  prints("\033[1;31m连接失败!\033[m\r\n");                                  prints("\033[1;31m连接失败!\033[m\r\n");
482                                  press_any_key();                                  press_any_key();
   
483                                  goto cleanup;                                  goto cleanup;
484                          }                          }
485                  }                  }
# Line 546  static int bbsnet_connect(int n) Line 562  static int bbsnet_connect(int n)
562          {          {
563                  prints("\033[1;31m连接失败!\033[m\r\n");                  prints("\033[1;31m连接失败!\033[m\r\n");
564                  press_any_key();                  press_any_key();
   
565                  goto cleanup;                  goto cleanup;
566          }          }
567    
# Line 802  static int bbsnet_connect(int n) Line 817  static int bbsnet_connect(int n)
817          {          {
818                  if (SSH_v2 && ssh_channel_is_closed(SSH_channel))                  if (SSH_v2 && ssh_channel_is_closed(SSH_channel))
819                  {                  {
820    #ifdef _DEBUG
821                          log_error("SSH channel is closed\n");                          log_error("SSH channel is closed\n");
822    #endif
823                          loop = 0;                          loop = 0;
824                          break;                          break;
825                  }                  }
826    
827                  if (bbsnet_conf[n].use_ssh && ssh_channel_is_closed(channel))                  if (bbsnet_conf[n].use_ssh && ssh_channel_is_closed(channel))
828                  {                  {
829    #ifdef _DEBUG
830                          log_error("Remote SSH channel is closed\n");                          log_error("Remote SSH channel is closed\n");
831    #endif
832                          loop = 0;                          loop = 0;
833                          break;                          break;
834                  }                  }
# Line 852  static int bbsnet_connect(int n) Line 871  static int bbsnet_connect(int n)
871                  for (int i = 0; i < nfds; i++)                  for (int i = 0; i < nfds; i++)
872                  {                  {
873  #ifdef HAVE_SYS_EPOLL_H  #ifdef HAVE_SYS_EPOLL_H
874                          if (events[i].data.fd == STDIN_FILENO)                          if (events[i].events & (EPOLLRDHUP | EPOLLHUP | EPOLLERR))
875    #else
876                            if (pfds[i].revents & (POLLRDHUP | POLLHUP | POLLERR))
877    #endif
878                            {
879    #ifdef _DEBUG
880    #ifdef HAVE_SYS_EPOLL_H
881                                    log_error("FD (%d) error events (%d)\n", events[i].data.fd, events[i].events);
882    #else
883                                    log_error("FD (%d) error events (%d)\n", pfds[i].fd, pfds[i].revents);
884    #endif
885    #endif
886                                    loop = 0;
887                                    break;
888                            }
889    
890    #ifdef HAVE_SYS_EPOLL_H
891                            if (events[i].data.fd == STDIN_FILENO && (events[i].events & EPOLLIN))
892  #else  #else
893                          if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & POLLIN))                          if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & POLLIN))
894  #endif  #endif
# Line 886  static int bbsnet_connect(int n) Line 922  static int bbsnet_connect(int n)
922                          }                          }
923    
924  #ifdef HAVE_SYS_EPOLL_H  #ifdef HAVE_SYS_EPOLL_H
925                          if (events[i].data.fd == STDOUT_FILENO)                          if (events[i].data.fd == STDOUT_FILENO && (events[i].events & EPOLLOUT))
926  #else  #else
927                          if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & POLLOUT))                          if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & POLLOUT))
928  #endif  #endif
# Line 904  static int bbsnet_connect(int n) Line 940  static int bbsnet_connect(int n)
940                                          ret = ssh_channel_read_nonblocking(SSH_channel, input_buf + input_buf_len, sizeof(input_buf) - (uint32_t)input_buf_len, 0);                                          ret = ssh_channel_read_nonblocking(SSH_channel, input_buf + input_buf_len, sizeof(input_buf) - (uint32_t)input_buf_len, 0);
941                                          if (ret == SSH_ERROR)                                          if (ret == SSH_ERROR)
942                                          {                                          {
943    #ifdef _DEBUG
944                                                  log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session));                                                  log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session));
945    #endif
946                                                  loop = 0;                                                  loop = 0;
947                                                  break;                                                  break;
948                                          }                                          }
# Line 1007  static int bbsnet_connect(int n) Line 1045  static int bbsnet_connect(int n)
1045                                          ret = ssh_channel_write(channel, input_conv + input_conv_offset, (uint32_t)(input_conv_len - input_conv_offset));                                          ret = ssh_channel_write(channel, input_conv + input_conv_offset, (uint32_t)(input_conv_len - input_conv_offset));
1046                                          if (ret == SSH_ERROR)                                          if (ret == SSH_ERROR)
1047                                          {                                          {
1048    #ifdef _DEBUG
1049                                                  log_error("ssh_channel_write() error: %s\n", ssh_get_error(session));                                                  log_error("ssh_channel_write() error: %s\n", ssh_get_error(session));
1050    #endif
1051                                                  loop = 0;                                                  loop = 0;
1052                                                  break;                                                  break;
1053                                          }                                          }
# Line 1029  static int bbsnet_connect(int n) Line 1069  static int bbsnet_connect(int n)
1069                                          }                                          }
1070                                          else                                          else
1071                                          {                                          {
1072    #ifdef _DEBUG
1073                                                  log_error("write(socket) error (%d)\n", errno);                                                  log_error("write(socket) error (%d)\n", errno);
1074    #endif
1075                                                  loop = 0;                                                  loop = 0;
1076                                                  break;                                                  break;
1077                                          }                                          }
# Line 1067  static int bbsnet_connect(int n) Line 1109  static int bbsnet_connect(int n)
1109                                                                                                             (uint32_t)(sizeof(output_buf) - (size_t)output_buf_len), 0);                                                                                                             (uint32_t)(sizeof(output_buf) - (size_t)output_buf_len), 0);
1110                                          if (ret == SSH_ERROR)                                          if (ret == SSH_ERROR)
1111                                          {                                          {
1112    #ifdef _DEBUG
1113                                                  log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(session));                                                  log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(session));
1114    #endif
1115                                                  loop = 0;                                                  loop = 0;
1116                                                  break;                                                  break;
1117                                          }                                          }
# Line 1100  static int bbsnet_connect(int n) Line 1144  static int bbsnet_connect(int n)
1144                                          }                                          }
1145                                          else                                          else
1146                                          {                                          {
1147    #ifdef _DEBUG
1148                                                  log_error("read(socket) error (%d)\n", errno);                                                  log_error("read(socket) error (%d)\n", errno);
1149    #endif
1150                                                  loop = 0;                                                  loop = 0;
1151                                                  break;                                                  break;
1152                                          }                                          }
# Line 1141  static int bbsnet_connect(int n) Line 1187  static int bbsnet_connect(int n)
1187                                          ret = ssh_channel_write(SSH_channel, output_conv + output_conv_offset, (uint32_t)(output_conv_len - output_conv_offset));                                          ret = ssh_channel_write(SSH_channel, output_conv + output_conv_offset, (uint32_t)(output_conv_len - output_conv_offset));
1188                                          if (ret == SSH_ERROR)                                          if (ret == SSH_ERROR)
1189                                          {                                          {
1190    #ifdef _DEBUG
1191                                                  log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session));                                                  log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session));
1192    #endif
1193                                                  loop = 0;                                                  loop = 0;
1194                                                  break;                                                  break;
1195                                          }                                          }
# Line 1163  static int bbsnet_connect(int n) Line 1211  static int bbsnet_connect(int n)
1211                                          }                                          }
1212                                          else                                          else
1213                                          {                                          {
1214    #ifdef _DEBUG
1215                                                  log_error("write(STDOUT) error (%d)\n", errno);                                                  log_error("write(STDOUT) error (%d)\n", errno);
1216    #endif
1217                                                  loop = 0;                                                  loop = 0;
1218                                                  break;                                                  break;
1219                                          }                                          }
# Line 1237  cleanup: Line 1287  cleanup:
1287                  log_error("Close socket failed\n");                  log_error("Close socket failed\n");
1288          }          }
1289    
1290            if (res)
1291            {
1292                    freeaddrinfo(res);
1293            }
1294    
1295          t_used = time(NULL) - t_used;          t_used = time(NULL) - t_used;
1296          tm_used = gmtime(&t_used);          tm_used = gmtime(&t_used);
1297    
# Line 1287  static int bbsnet_selchange() Line 1342  static int bbsnet_selchange()
1342          prints("|");          prints("|");
1343          moveto(21, 1);          moveto(21, 1);
1344          clrtoeol();          clrtoeol();
1345          prints("|\033[1m连往: \033[1;33m%-20s\033[m  端口: \033[1;33m%-5d\033[m                 编码: \033[1;33m%s\033[m",          prints("|\033[1m连往: \033[1;33m%-20s\033[m  端口: \033[1;33m%-5s\033[m                 编码: \033[1;33m%s\033[m",
1346                     bbsnet_conf[i].host_name, bbsnet_conf[i].port, bbsnet_conf[i].charset);                     bbsnet_conf[i].host_name, bbsnet_conf[i].port, bbsnet_conf[i].charset);
1347          moveto(21, 80);          moveto(21, 80);
1348          prints("|");          prints("|");


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

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