/[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.92 by sysadm, Wed Dec 17 03:56:39 2025 UTC Revision 1.93 by sysadm, Thu Dec 18 01:39:32 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 1255  cleanup: Line 1270  cleanup:
1270                  log_error("Close socket failed\n");                  log_error("Close socket failed\n");
1271          }          }
1272    
1273            if (res)
1274            {
1275                    freeaddrinfo(res);
1276            }
1277    
1278          t_used = time(NULL) - t_used;          t_used = time(NULL) - t_used;
1279          tm_used = gmtime(&t_used);          tm_used = gmtime(&t_used);
1280    
# Line 1305  static int bbsnet_selchange() Line 1325  static int bbsnet_selchange()
1325          prints("|");          prints("|");
1326          moveto(21, 1);          moveto(21, 1);
1327          clrtoeol();          clrtoeol();
1328          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",
1329                     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);
1330          moveto(21, 80);          moveto(21, 80);
1331          prints("|");          prints("|");


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

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