/[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.21 by sysadm, Mon May 5 11:11:06 2025 UTC Revision 1.26 by sysadm, Sat May 10 02:14:46 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /***************************************************************************
2                                                    bbs_net.c  -  description                                                    bbs_net.c  -  description
3                                                           -------------------                                                           -------------------
4          begin                : Mon Oct 18 2004          Copyright            : (C) 2004-2025 by Leaflet
5          copyright            : (C) 2004 by Leaflet          Email                : leaflet@leafok.com
         email                : leaflet@leafok.com  
6   ***************************************************************************/   ***************************************************************************/
7    
8  /***************************************************************************  /***************************************************************************
9   *                                                                         *   *                                                                         *
10   *   This program is free software; you can redistribute it and/or modify  *   *   This program is free software; you can redistribute it and/or modify  *
11   *   it under the terms of the GNU General Public License as published by  *   *   it under the terms of the GNU General Public License as published by  *
12   *   the Free Software Foundation; either version 2 of the License, or     *   *   the Free Software Foundation; either version 3 of the License, or     *
13   *   (at your option) any later version.                                   *   *   (at your option) any later version.                                   *
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
# Line 32  Line 31 
31  #include <unistd.h>  #include <unistd.h>
32  #include <netdb.h>  #include <netdb.h>
33    
34  #define TIME_OUT 15  #define MENU_CONF_DELIM " \t\r\n"
35    
36  #define MAX_PROCESS_BAR_LEN 30  #define MAX_PROCESS_BAR_LEN 30
37  #define MAXSTATION 26 * 2  #define MAXSTATION 26 * 2
38  #define STATION_PER_LINE 4  #define STATION_PER_LINE 4
# Line 42  struct _bbsnet_conf Line 42  struct _bbsnet_conf
42          char host1[20];          char host1[20];
43          char host2[40];          char host2[40];
44          char ip[40];          char ip[40];
45          int port;          in_port_t port;
46  } bbsnet_conf[MAXSTATION];  } bbsnet_conf[MAXSTATION];
47    
48  MENU_SET bbsnet_menu;  MENU_SET bbsnet_menu;
# Line 69  int load_bbsnet_conf(const char *file_co Line 69  int load_bbsnet_conf(const char *file_co
69    
70          while (fgets(t, 255, fp) && item_count < MAXSTATION)          while (fgets(t, 255, fp) && item_count < MAXSTATION)
71          {          {
72                  t1 = strtok_r(t, " \t", &saveptr);                  t1 = strtok_r(t, MENU_CONF_DELIM, &saveptr);
73                  t2 = strtok_r(NULL, " \t\n", &saveptr);                  t2 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
74                  t3 = strtok_r(NULL, " \t\n", &saveptr);                  t3 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
75                  t4 = strtok_r(NULL, " \t\n", &saveptr);                  t4 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
76    
77                  if (t1 == NULL || t2 == NULL || t3 == NULL || t[0] == '#' || t[0] == '*')                  if (t1 == NULL || t2 == NULL || t3 == NULL || t4 == NULL || t[0] == '#' || t[0] == '*')
78                  {                  {
79                          continue;                          continue;
80                  }                  }
# Line 85  int load_bbsnet_conf(const char *file_co Line 85  int load_bbsnet_conf(const char *file_co
85                  bbsnet_conf[item_count].host2[sizeof(bbsnet_conf[item_count].host2) - 1] = '\0';                  bbsnet_conf[item_count].host2[sizeof(bbsnet_conf[item_count].host2) - 1] = '\0';
86                  strncpy(bbsnet_conf[item_count].ip, t3, sizeof(bbsnet_conf[item_count].ip) - 1);                  strncpy(bbsnet_conf[item_count].ip, t3, sizeof(bbsnet_conf[item_count].ip) - 1);
87                  bbsnet_conf[item_count].ip[sizeof(bbsnet_conf[item_count].ip) - 1] = '\0';                  bbsnet_conf[item_count].ip[sizeof(bbsnet_conf[item_count].ip) - 1] = '\0';
88                  bbsnet_conf[item_count].port = t4 ? atoi(t4) : 23;                  bbsnet_conf[item_count].port = (in_port_t)(t4 ? atoi(t4) : 23);
89    
90                  p_menuitem = p_menu->items[item_count] = malloc(sizeof(MENU_ITEM));                  p_menuitem = p_menu->items[item_count] = malloc(sizeof(MENU_ITEM));
91                  p_menuitem->row = 2 + item_count / STATION_PER_LINE;                  p_menuitem->row = 2 + item_count / STATION_PER_LINE;
92                  p_menuitem->col = 5 + item_count % STATION_PER_LINE * 20;                  p_menuitem->col = 5 + item_count % STATION_PER_LINE * 20;
93                  sprintf(p_menuitem->action, "%d", item_count);                  snprintf(p_menuitem->action, sizeof(p_menuitem->action), "%d", item_count);
94                  p_menuitem->submenu = 0;                  p_menuitem->submenu = 0;
95                  p_menuitem->priv = 0;                  p_menuitem->priv = 0;
96                  p_menuitem->level = 0;                  p_menuitem->level = 0;
97                  p_menuitem->display = 0;                  p_menuitem->display = 0;
98                  p_menuitem->name[0] =                  p_menuitem->name[0] =
99                          (item_count < MAXSTATION / 2 ? 'A' + item_count : 'a' + item_count);                          (char)(item_count < MAXSTATION / 2 ? 'A' + item_count : 'a' + item_count);
100                  p_menuitem->name[1] = '\0';                  p_menuitem->name[1] = '\0';
101                  snprintf(p_menuitem->text, sizeof(p_menuitem->text), "%c. %s",                  snprintf(p_menuitem->text, sizeof(p_menuitem->text), "%c. %s",
102                                   p_menuitem->name[0], bbsnet_conf[item_count].host1);                                   p_menuitem->name[0], bbsnet_conf[item_count].host1);
# Line 124  static void process_bar(int n, int len) Line 124  static void process_bar(int n, int len)
124          char *ptr3;          char *ptr3;
125    
126          moveto(4, 0);          moveto(4, 0);
127          prints("┌──────────────────────────────┐\r\n");          prints(" ------------------------------ \r\n");
128          sprintf(buf2, "            %3d%%              ", n * 100 / len);          snprintf(buf2, sizeof(buf2), "            %3d%%              ", n * 100 / len);
129          ptr = buf;          ptr = buf;
130          ptr2 = buf2;          ptr2 = buf2;
131          ptr3 = buf + n;          ptr3 = buf + n;
# Line 139  static void process_bar(int n, int len) Line 139  static void process_bar(int n, int len)
139          while (*ptr2 != '\0')          while (*ptr2 != '\0')
140                  *ptr++ = *ptr2++;                  *ptr++ = *ptr2++;
141          *ptr++ = '\0';          *ptr++ = '\0';
142          prints("│\033[46m%s\033[m│\r\n", buf);          prints("|\033[46m%s\033[m|\r\n", buf);
143          prints("└──────────────────────────────┘\r\n");          prints(" ------------------------------ \r\n");
144          iflush();          iflush();
145  }  }
146    
147  int bbsnet_connect(int n)  int bbsnet_connect(int n)
148  {  {
149          int sock, result, len, loop;          int sock, result, loop;
150            ssize_t len;
151          struct sockaddr_in sin;          struct sockaddr_in sin;
152          char buf[256];          char buf[LINE_BUFFER_LEN];
153          fd_set inputs, testfds;          fd_set testfds;
154          struct timeval timeout;          struct timeval timeout;
155          struct hostent *pHost = NULL;          struct hostent *p_host = NULL;
156          int rv, tos = 020, i;          int rv, tos = 020, i;
157          char remote_addr[256];          char remote_addr[IP_ADDR_LEN];
158          int remote_port;          int remote_port;
159          time_t t_used;          time_t t_used;
160          struct tm *tm_used;          struct tm *tm_used;
161            int ch;
162    
163          clearscr();          clearscr();
164    
165          moveto(0, 0);          moveto(0, 0);
166          prints("\033[1;32m正在测试往 %s (%s) 的连接,请稍候... \033[m\r\n",          prints("\033[1;32m正在测试往 %s (%s) 的连接,请稍候... \033[m\r\n",
167                     bbsnet_conf[n].host1, bbsnet_conf[n].ip);                     bbsnet_conf[n].host1, bbsnet_conf[n].ip);
         prints("\033[1;32m如果在 %d 秒内无法连上,穿梭程序将放弃连接。\033[m\r\n",  
                    TIME_OUT);  
168          iflush();          iflush();
169    
170          pHost = gethostbyname(bbsnet_conf[n].ip);          p_host = gethostbyname(bbsnet_conf[n].ip);
171    
172          if (pHost == NULL)          if (p_host == NULL)
173          {          {
174                  prints("\033[1;31m查找主机名失败!\033[m\r\n");                  prints("\033[1;31m查找主机名失败!\033[m\r\n");
175                  press_any_key();                  press_any_key();
# Line 199  int bbsnet_connect(int n) Line 199  int bbsnet_connect(int n)
199    
200          bzero(&sin, sizeof(sin));          bzero(&sin, sizeof(sin));
201          sin.sin_family = AF_INET;          sin.sin_family = AF_INET;
202          sin.sin_addr = *(struct in_addr *)pHost->h_addr_list[0];          sin.sin_addr = *(struct in_addr *)p_host->h_addr_list[0];
203          sin.sin_port = htons(bbsnet_conf[n].port);          sin.sin_port = htons(bbsnet_conf[n].port);
204    
205          strncpy(remote_addr, inet_ntoa(sin.sin_addr), sizeof(remote_addr) - 1);          strncpy(remote_addr, inet_ntoa(sin.sin_addr), sizeof(remote_addr) - 1);
206          remote_addr[sizeof(remote_addr) - 1] = '\0';          remote_addr[sizeof(remote_addr) - 1] = '\0';
207          remote_port = ntohs(sin.sin_port);          remote_port = ntohs(sin.sin_port);
208    
209          prints("\033[1;32m穿梭进度条提示您当前已使用的时间。\033[m\r\n");          prints("\033[1;32m穿梭进度条提示您当前已使用的时间,按Ctrl-C中断。\033[m\r\n");
210          process_bar(0, MAX_PROCESS_BAR_LEN);          process_bar(0, MAX_PROCESS_BAR_LEN);
211          for (i = 0; i < MAX_PROCESS_BAR_LEN; i++)          for (i = 0; i < MAX_PROCESS_BAR_LEN; i++)
212          {          {
213                  if (i == 0)                  ch = igetch(0); // 100 ms
214                          rv =                  if (ch == KEY_NULL || ch == Ctrl('C') || SYS_server_exit)
215                                  NonBlockConnectEx(sock, (struct sockaddr *)&sin, sizeof(sin),                  {
216                                                                    500, 1);                          return 0;
217                  else                  }
218                          rv =  
219                                  NonBlockConnectEx(sock, (struct sockaddr *)&sin, sizeof(sin),                  rv = NonBlockConnectEx(sock, (struct sockaddr *)&sin, sizeof(sin),
220                                                                    500, 0);                                                             400, (i == 0 ? 1 : 0)); // 400 ms
221    
222                  if (rv == ERR_TCPLIB_TIMEOUT)                  if (rv == ERR_TCPLIB_TIMEOUT)
223                  {                  {
224                          process_bar(i + 1, MAX_PROCESS_BAR_LEN);                          process_bar(i + 1, MAX_PROCESS_BAR_LEN);
# Line 245  int bbsnet_connect(int n) Line 246  int bbsnet_connect(int n)
246          prints("\033[1;31m连接成功!\033[m\r\n");          prints("\033[1;31m连接成功!\033[m\r\n");
247          log_std("BBSNET connect to %s:%d\n", remote_addr, remote_port);          log_std("BBSNET connect to %s:%d\n", remote_addr, remote_port);
248    
         FD_ZERO(&inputs);  
         FD_SET(0, &inputs);  
         FD_SET(sock, &inputs);  
   
249          BBS_last_access_tm = t_used = time(0);          BBS_last_access_tm = t_used = time(0);
250    
251          loop = 1;          loop = 1;
252    
253          while (loop)          while (loop && !SYS_server_exit)
254          {          {
255                  testfds = inputs;                  FD_ZERO(&testfds);
256                  timeout.tv_sec = TIME_OUT;                  FD_SET(STDIN_FILENO, &testfds);
257                  timeout.tv_usec = 0;                  FD_SET(sock, &testfds);
258            
259                    timeout.tv_sec = 0;
260                    timeout.tv_usec = 100 * 1000; // 0.1 second
261    
262                  result = SignalSafeSelect(FD_SETSIZE, &testfds, (fd_set *)NULL,                  result = SignalSafeSelect(FD_SETSIZE, &testfds, (fd_set *)NULL,
263                                                                    (fd_set *)NULL, &timeout);                                                                    (fd_set *)NULL, &timeout);
# Line 276  int bbsnet_connect(int n) Line 276  int bbsnet_connect(int n)
276                  }                  }
277                  if (result > 0)                  if (result > 0)
278                  {                  {
279                          if (FD_ISSET(0, &testfds))                          if (FD_ISSET(STDIN_FILENO, &testfds))
280                          {                          {
281                                  len = read(0, buf, 255);                                  len = read(STDIN_FILENO, buf, sizeof(buf));
282                                  if (len == 0)                                  if (len == 0)
283                                  {                                  {
284                                          loop = 0;                                          loop = 0;
285                                  }                                  }
286                                  write(sock, buf, len);                                  write(sock, buf, (size_t)len);
287                          }                          }
288                          if (FD_ISSET(sock, &testfds))                          if (FD_ISSET(sock, &testfds))
289                          {                          {
290                                  len = read(sock, buf, 255);                                  len = read(sock, buf, sizeof(buf));
291                                  if (len == 0)                                  if (len == 0)
292                                  {                                  {
293                                          loop = 0;                                          loop = 0;
294                                  }                                  }
295                                  write(1, buf, len);                                  write(STDOUT_FILENO, buf, (size_t)len);
296                          }                          }
297                          BBS_last_access_tm = time(0);                          BBS_last_access_tm = time(0);
298                  }                  }
# Line 318  bbsnet_refresh() Line 318  bbsnet_refresh()
318  {  {
319          clearscr();          clearscr();
320          moveto(1, 0);          moveto(1, 0);
321          prints("╭═════════════════════════════════════════════════════════════════════════════╮");          prints(" ----------------------------------------------------------------------------- ");
322          for (int i = 2; i < 19; i++)          for (int i = 2; i < 19; i++)
323          {          {
324                  moveto(i, 0);                  moveto(i, 0);
325                  prints("║");                  prints("|");
326                  moveto(i, 79);                  moveto(i, 79);
327                  prints("║");                  prints("|");
328          }          }
329          moveto(19, 0);          moveto(19, 0);
330          prints("║—————————————————————————————————————————————————————————————————————————————║");          prints("|-----------------------------------------------------------------------------|");
331          moveto(22, 0);          moveto(22, 0);
332          prints("╰═════════════════════════════════════════════════════════════════════════════╯");          prints(" ----------------------------------------------------------------------------- ");
333          moveto(23, 0);          moveto(23, 0);
334          prints(" [\x1b[1;32mCtrl+C\x1b[m]退出");          prints(" [\x1b[1;32mCtrl+C\x1b[m]退出");
335    
# Line 342  int bbsnet_selchange(int new_pos) Line 342  int bbsnet_selchange(int new_pos)
342  {  {
343          moveto(20, 0);          moveto(20, 0);
344          clrtoeol();          clrtoeol();
345          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",
346                     bbsnet_conf[new_pos].host2, bbsnet_conf[new_pos].host1);                     bbsnet_conf[new_pos].host2, bbsnet_conf[new_pos].host1);
347          moveto(20, 79);          moveto(20, 79);
348          prints("║");          prints("|");
349          moveto(21, 0);          moveto(21, 0);
350          clrtoeol();          clrtoeol();
351          prints("║\x1b[1m连往:\x1b[1;33m%-20s", bbsnet_conf[new_pos].ip);          prints("|\x1b[1m连往:\x1b[1;33m%-20s", bbsnet_conf[new_pos].ip);
352          if (bbsnet_conf[new_pos].port != 23)          if (bbsnet_conf[new_pos].port != 23)
353          {          {
354                  prints("  %d", bbsnet_conf[new_pos].port);                  prints("  %d", bbsnet_conf[new_pos].port);
355          }          }
356          prints("\x1b[m");          prints("\x1b[m");
357          moveto(21, 79);          moveto(21, 79);
358          prints("║");          prints("|");
359          iflush();          iflush();
360    
361          return 0;          return 0;
# Line 375  int bbs_net() Line 375  int bbs_net()
375          display_menu(get_menu(&bbsnet_menu, "BBSNET"));          display_menu(get_menu(&bbsnet_menu, "BBSNET"));
376          bbsnet_selchange(pos);          bbsnet_selchange(pos);
377    
378          while (1)          while (!SYS_server_exit)
379          {          {
380                  ch = igetch(0);                  ch = igetch(0);
381                  switch (ch)                  switch (ch)
382                  {                  {
383                  case KEY_NULL:                  case KEY_NULL:
384                            return -1;
385                  case Ctrl('C'):                  case Ctrl('C'):
386                          return 0;                          return 0;
387                  case KEY_TIMEOUT:                  case KEY_TIMEOUT:
388                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)
389                          {                          {
390                                  return -1;                                  return 0;
391                          }                          }
392                          continue;                          break;
393                  case CR:                  case CR:
394                          pos = bbsnet_menu.p_menu[0]->item_cur_pos;                          pos = bbsnet_menu.p_menu[0]->item_cur_pos;
395                          bbsnet_connect(pos);                          bbsnet_connect(pos);


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

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