/[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.16 by sysadm, Fri May 2 03:34:58 2025 UTC Revision 1.28 by sysadm, Sat May 10 05:33:22 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 52  int load_bbsnet_conf(const char *file_co Line 52  int load_bbsnet_conf(const char *file_co
52          FILE *fp;          FILE *fp;
53          MENU *p_menu;          MENU *p_menu;
54          MENU_ITEM *p_menuitem;          MENU_ITEM *p_menuitem;
55          char t[256], *t1, *t2, *t3, *t4;          char t[256], *t1, *t2, *t3, *t4, *saveptr;
56          int item_count = 0;          int item_count = 0;
57    
58          fp = fopen(file_config, "r");          fp = fopen(file_config, "r");
59          if (fp == NULL)          if (fp == NULL)
60            {
61                  return -1;                  return -1;
62            }
63    
64          p_menu = bbsnet_menu.p_menu[0] = malloc(sizeof(MENU));          p_menu = bbsnet_menu.p_menu[0] = malloc(sizeof(MENU));
65          strcpy(p_menu->name, "BBSNET");          strncpy(p_menu->name, "BBSNET", sizeof(p_menu->name) - 1);
66            p_menu->name[sizeof(p_menu->name) - 1] = '\0';
67          p_menu->title.show = 0;          p_menu->title.show = 0;
68          p_menu->screen.show = 0;          p_menu->screen.show = 0;
69    
70          while (fgets(t, 255, fp) && item_count < MAXSTATION)          while (fgets(t, 255, fp) && item_count < MAXSTATION)
71          {          {
72                  t1 = strtok(t, " \t");                  t1 = strtok_r(t, MENU_CONF_DELIM, &saveptr);
73                  t2 = strtok(NULL, " \t\n");                  t2 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
74                  t3 = strtok(NULL, " \t\n");                  t3 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
75                  t4 = strtok(NULL, " \t\n");                  t4 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
76    
77                  if (t1[0] == '#' || t1[0] == '*' || t1 == NULL || t2 == NULL || t3 == NULL)                  if (t1 == NULL || t2 == NULL || t3 == NULL || t4 == NULL || t[0] == '#' || t[0] == '*')
78                    {
79                          continue;                          continue;
80                  strncpy(bbsnet_conf[item_count].host1, t2, 18);                  }
81                  bbsnet_conf[item_count].host1[18] = 0;  
82                  strncpy(bbsnet_conf[item_count].host2, t1, 36);                  strncpy(bbsnet_conf[item_count].host1, t2, sizeof(bbsnet_conf[item_count].host1) - 1);
83                  bbsnet_conf[item_count].host2[36] = 0;                  bbsnet_conf[item_count].host1[sizeof(bbsnet_conf[item_count].host1) - 1] = '\0';
84                  strncpy(bbsnet_conf[item_count].ip, t3, 36);                  strncpy(bbsnet_conf[item_count].host2, t1, sizeof(bbsnet_conf[item_count].host2) - 1);
85                  bbsnet_conf[item_count].ip[36] = 0;                  bbsnet_conf[item_count].host2[sizeof(bbsnet_conf[item_count].host2) - 1] = '\0';
86                  bbsnet_conf[item_count].port = t4 ? atoi(t4) : 23;                  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';
88                    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                  sprintf(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);
103    
104                  item_count++;                  item_count++;
105          }          }
# Line 109  int load_bbsnet_conf(const char *file_co Line 115  int load_bbsnet_conf(const char *file_co
115          return 0;          return 0;
116  }  }
117    
118  static void  static void process_bar(int n, int len)
 process_bar(int n, int len)  
119  {  {
120          char buf[256];          char buf[LINE_BUFFER_LEN];
121          char buf2[256];          char buf2[LINE_BUFFER_LEN];
122          char *ptr;  
123          char *ptr2;          if (len > LINE_BUFFER_LEN)
124          char *ptr3;          {
125                    len = LINE_BUFFER_LEN - 1;
126            }
127            if (n < 0)
128            {
129                    n = 0;
130            }
131            else if (n > len)
132            {
133                    n = len;
134            }
135    
136          moveto(4, 0);          moveto(4, 0);
137          prints("┌───────────────┐\r\n");          prints(" ------------------------------ \r\n");
138          sprintf(buf2, "            %3d%%              ", n * 100 / len);          snprintf(buf, sizeof(buf), "            %3d%%              ", n * 100 / len);
139          ptr = buf;          strncpy(buf2, buf, (size_t) n);
140          ptr2 = buf2;          buf2[n] = '\0';
141          ptr3 = buf + n;          prints("|\033[46m%s\033[44m%s\033[m|\r\n", buf2, buf + n);
142          while (ptr != ptr3)          prints(" ------------------------------ \r\n");
                 *ptr++ = *ptr2++;  
         *ptr++ = '\x1b';  
         *ptr++ = '[';  
         *ptr++ = '4';  
         *ptr++ = '4';  
         *ptr++ = 'm';  
         while (*ptr2 != '\0')  
                 *ptr++ = *ptr2++;  
         *ptr++ = '\0';  
         prints("│\033[46m%s\033[m│\r\n", buf);  
         prints("└───────────────┘\r\n");  
143          iflush();          iflush();
144  }  }
145    
146  int bbsnet_connect(int n)  int bbsnet_connect(int n)
147  {  {
148          int sock, ch, result, len, loop;          int sock, ret, loop;
149            ssize_t len;
150          struct sockaddr_in sin;          struct sockaddr_in sin;
151          char buf[256];          char buf[LINE_BUFFER_LEN];
152          fd_set inputs, testfds;          fd_set testfds;
153          struct timeval timeout;          struct timeval timeout;
154          struct hostent *pHost = NULL;          struct hostent *p_host = NULL;
155          int rc, rv, tos = 020, i;          int rv, tos = 020, i;
156          char remote_addr[256];          char remote_addr[IP_ADDR_LEN];
157          int remote_port;          int remote_port;
158          time_t t_used;          time_t t_used;
159          struct tm *tm_used;          struct tm *tm_used;
160            int ch;
161    
162          clearscr();          clearscr();
163    
164          moveto(0, 0);          moveto(0, 0);
165          prints("\033[1;32m正在测试往 %s (%s) 的连接,请稍候... \033[m\r\n",          prints("\033[1;32m正在测试往 %s (%s) 的连接,请稍候... \033[m\r\n",
166                     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);  
167          iflush();          iflush();
168    
169          pHost = gethostbyname(bbsnet_conf[n].ip);          p_host = gethostbyname(bbsnet_conf[n].ip);
170    
171          if (pHost == NULL)          if (p_host == NULL)
172          {          {
173                  prints("\033[1;31m查找主机名失败!\033[m\r\n");                  prints("\033[1;31m查找主机名失败!\033[m\r\n");
174                  press_any_key();                  press_any_key();
# Line 182  int bbsnet_connect(int n) Line 186  int bbsnet_connect(int n)
186    
187          sin.sin_family = AF_INET;          sin.sin_family = AF_INET;
188          sin.sin_addr.s_addr =          sin.sin_addr.s_addr =
189                  (strlen(hostaddr_server) > 0 ? inet_addr(hostaddr_server) : INADDR_ANY);                  (strnlen(hostaddr_server, sizeof(hostaddr_server)) > 0 ? inet_addr(hostaddr_server) : INADDR_ANY);
190          sin.sin_port = 0;          sin.sin_port = 0;
191    
192          if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0)          if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0)
# Line 194  int bbsnet_connect(int n) Line 198  int bbsnet_connect(int n)
198    
199          bzero(&sin, sizeof(sin));          bzero(&sin, sizeof(sin));
200          sin.sin_family = AF_INET;          sin.sin_family = AF_INET;
201          sin.sin_addr = *(struct in_addr *)pHost->h_addr_list[0];          sin.sin_addr = *(struct in_addr *)p_host->h_addr_list[0];
202          sin.sin_port = htons(bbsnet_conf[n].port);          sin.sin_port = htons(bbsnet_conf[n].port);
203    
204          strcpy(remote_addr, inet_ntoa(sin.sin_addr));          strncpy(remote_addr, inet_ntoa(sin.sin_addr), sizeof(remote_addr) - 1);
205            remote_addr[sizeof(remote_addr) - 1] = '\0';
206          remote_port = ntohs(sin.sin_port);          remote_port = ntohs(sin.sin_port);
207    
208          prints("\033[1;32m穿梭进度条提示您当前已使用的时间。\033[m\r\n");          prints("\033[1;32m穿梭进度条提示您当前已使用的时间,按\033[1;33mCtrl+C\033[1;32m中断。\033[m\r\n");
209          process_bar(0, MAX_PROCESS_BAR_LEN);          process_bar(0, MAX_PROCESS_BAR_LEN);
210          for (i = 0; i < MAX_PROCESS_BAR_LEN; i++)          for (i = 0; i < MAX_PROCESS_BAR_LEN; i++)
211          {          {
212                  if (i == 0)                  ch = igetch(0); // 100 ms
213                          rv =                  if (ch == KEY_NULL || ch == Ctrl('C') || SYS_server_exit)
214                                  NonBlockConnectEx(sock, (struct sockaddr *)&sin, sizeof(sin),                  {
215                                                                    500, 1);                          return 0;
216                  else                  }
217                          rv =  
218                                  NonBlockConnectEx(sock, (struct sockaddr *)&sin, sizeof(sin),                  rv = NonBlockConnectEx(sock, (struct sockaddr *)&sin, sizeof(sin),
219                                                                    500, 0);                                                             400, (i == 0 ? 1 : 0)); // 400 ms
220    
221                  if (rv == ERR_TCPLIB_TIMEOUT)                  if (rv == ERR_TCPLIB_TIMEOUT)
222                  {                  {
223                          process_bar(i + 1, MAX_PROCESS_BAR_LEN);                          process_bar(i + 1, MAX_PROCESS_BAR_LEN);
224                          continue;                          continue;
225                  }                  }
226                  else if (rv == 0)                  else if (rv == 0)
227                    {
228                          break;                          break;
229                    }
230                  else                  else
231                  {                  {
232                          prints("\033[1;31m连接失败!\033[m\r\n");                          prints("\033[1;31m连接失败!\033[m\r\n");
# Line 237  int bbsnet_connect(int n) Line 245  int bbsnet_connect(int n)
245          prints("\033[1;31m连接成功!\033[m\r\n");          prints("\033[1;31m连接成功!\033[m\r\n");
246          log_std("BBSNET connect to %s:%d\n", remote_addr, remote_port);          log_std("BBSNET connect to %s:%d\n", remote_addr, remote_port);
247    
         FD_ZERO(&inputs);  
         FD_SET(0, &inputs);  
         FD_SET(sock, &inputs);  
   
248          BBS_last_access_tm = t_used = time(0);          BBS_last_access_tm = t_used = time(0);
249    
250          loop = 1;          loop = 1;
251    
252          while (loop)          while (loop && !SYS_server_exit)
253          {          {
254                  testfds = inputs;                  FD_ZERO(&testfds);
255                  timeout.tv_sec = TIME_OUT;                  FD_SET(STDIN_FILENO, &testfds);
256                  timeout.tv_usec = 0;                  FD_SET(sock, &testfds);
257            
258                    timeout.tv_sec = 0;
259                    timeout.tv_usec = 100 * 1000; // 0.1 second
260    
261                  result = SignalSafeSelect(FD_SETSIZE, &testfds, (fd_set *)NULL,                  ret = select(FD_SETSIZE, &testfds, NULL, NULL, &timeout);
                                                                   (fd_set *)NULL, &timeout);  
262    
263                  if (result == 0)                  if (ret == 0)
264                  {                  {
265                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)
266                          {                          {
267                                  loop = 0;                                  loop = 0;
268                          }                          }
269                  }                  }
270                  if (result < 0)                  else if (ret < 0)
271                  {                  {
272                          log_error("select() error (%d) !\n", result);                          if (errno != EINTR)
273                          loop = 0;                          {
274                                    log_error("select() error (%d) !\n", errno);
275                                    loop = 0;
276                            }
277                  }                  }
278                  if (result > 0)                  else if (ret > 0)
279                  {                  {
280                          if (FD_ISSET(0, &testfds))                          if (FD_ISSET(STDIN_FILENO, &testfds))
281                          {                          {
282                                  len = read(0, buf, 255);                                  len = read(STDIN_FILENO, buf, sizeof(buf));
283                                  if (len == 0)                                  if (len == 0)
284                                  {                                  {
285                                          loop = 0;                                          loop = 0;
286                                  }                                  }
287                                  write(sock, buf, len);                                  write(sock, buf, (size_t)len);
288                          }                          }
289                          if (FD_ISSET(sock, &testfds))                          if (FD_ISSET(sock, &testfds))
290                          {                          {
291                                  len = read(sock, buf, 255);                                  len = read(sock, buf, sizeof(buf));
292                                  if (len == 0)                                  if (len == 0)
293                                  {                                  {
294                                          loop = 0;                                          loop = 0;
295                                  }                                  }
296                                  write(1, buf, len);                                  write(STDOUT_FILENO, buf, (size_t)len);
297                          }                          }
298                          BBS_last_access_tm = time(0);                          BBS_last_access_tm = time(0);
299                  }                  }
# Line 308  int bbsnet_connect(int n) Line 317  int bbsnet_connect(int n)
317  static int  static int
318  bbsnet_refresh()  bbsnet_refresh()
319  {  {
         int i;  
   
320          clearscr();          clearscr();
321          moveto(1, 0);          moveto(1, 0);
322          prints("╭══════════════════════════════════════╮");          prints(" ----------------------------------------------------------------------------- ");
323          for (i = 2; i < 19; i++)          for (int i = 2; i < 19; i++)
324          {          {
325                  moveto(i, 0);                  moveto(i, 0);
326                  prints("║");                  prints("|");
327                  moveto(i, 79);                  moveto(i, 79);
328                  prints("║");                  prints("|");
329          }          }
330          moveto(19, 0);          moveto(19, 0);
331          prints("║——————————————————————————————————————║");          prints("|-----------------------------------------------------------------------------|");
332          moveto(22, 0);          moveto(22, 0);
333          prints("╰══════════════════════════════════════╯");          prints(" ----------------------------------------------------------------------------- ");
334          moveto(23, 0);          moveto(23, 0);
335          prints(" [\x1b[1;32mCtrl+C\x1b[m]退出");          prints(" [\x1b[1;32mCtrl+C\x1b[m]退出");
336    
# Line 336  int bbsnet_selchange(int new_pos) Line 343  int bbsnet_selchange(int new_pos)
343  {  {
344          moveto(20, 0);          moveto(20, 0);
345          clrtoeol();          clrtoeol();
346          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",
347                     bbsnet_conf[new_pos].host2, bbsnet_conf[new_pos].host1);                     bbsnet_conf[new_pos].host2, bbsnet_conf[new_pos].host1);
348          moveto(20, 79);          moveto(20, 79);
349          prints("║");          prints("|");
350          moveto(21, 0);          moveto(21, 0);
351          clrtoeol();          clrtoeol();
352          prints("║\x1b[1m连往:\x1b[1;33m%-20s", bbsnet_conf[new_pos].ip);          prints("|\x1b[1m连往:\x1b[1;33m%-20s", bbsnet_conf[new_pos].ip);
353          if (bbsnet_conf[new_pos].port != 23)          if (bbsnet_conf[new_pos].port != 23)
354            {
355                  prints("  %d", bbsnet_conf[new_pos].port);                  prints("  %d", bbsnet_conf[new_pos].port);
356            }
357          prints("\x1b[m");          prints("\x1b[m");
358          moveto(21, 79);          moveto(21, 79);
359          prints("║");          prints("|");
360          iflush();          iflush();
361    
362          return 0;          return 0;
# Line 356  int bbsnet_selchange(int new_pos) Line 365  int bbsnet_selchange(int new_pos)
365  int bbs_net()  int bbs_net()
366  {  {
367          int ch, pos, i;          int ch, pos, i;
         char file_config[256];  
   
         strcpy(file_config, app_home_dir);  
         strcat(file_config, "conf/bbsnet.conf");  
368    
369          load_bbsnet_conf(file_config);          load_bbsnet_conf(CONF_BBSNET);
370    
371          BBS_last_access_tm = time(0);          BBS_last_access_tm = time(0);
372    
# Line 371  int bbs_net() Line 376  int bbs_net()
376          display_menu(get_menu(&bbsnet_menu, "BBSNET"));          display_menu(get_menu(&bbsnet_menu, "BBSNET"));
377          bbsnet_selchange(pos);          bbsnet_selchange(pos);
378    
379          while (1)          while (!SYS_server_exit)
380          {          {
381                  ch = igetch(0);                  ch = igetch(0);
382                  switch (ch)                  switch (ch)
383                  {                  {
384                  case KEY_NULL:                  case KEY_NULL:
385                            return -1;
386                  case Ctrl('C'):                  case Ctrl('C'):
387                          return 0;                          return 0;
388                  case KEY_TIMEOUT:                  case KEY_TIMEOUT:
389                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)
390                          {                          {
391                                  return -1;                                  return 0;
392                          }                          }
393                          continue;                          break;
394                  case CR:                  case CR:
395                          pos = bbsnet_menu.p_menu[0]->item_cur_pos;                          pos = bbsnet_menu.p_menu[0]->item_cur_pos;
396                          bbsnet_connect(pos);                          bbsnet_connect(pos);
# Line 394  int bbs_net() Line 400  int bbs_net()
400                          break;                          break;
401                  case KEY_UP:                  case KEY_UP:
402                          for (i = 0; i < STATION_PER_LINE; i++)                          for (i = 0; i < STATION_PER_LINE; i++)
403                            {
404                                  menu_control(&bbsnet_menu, KEY_UP);                                  menu_control(&bbsnet_menu, KEY_UP);
405                            }
406                          pos = bbsnet_menu.p_menu[0]->item_cur_pos;                          pos = bbsnet_menu.p_menu[0]->item_cur_pos;
407                          bbsnet_selchange(pos);                          bbsnet_selchange(pos);
408                          break;                          break;
409                  case KEY_DOWN:                  case KEY_DOWN:
410                          for (i = 0; i < STATION_PER_LINE; i++)                          for (i = 0; i < STATION_PER_LINE; i++)
411                            {
412                                  menu_control(&bbsnet_menu, KEY_DOWN);                                  menu_control(&bbsnet_menu, KEY_DOWN);
413                            }
414                          pos = bbsnet_menu.p_menu[0]->item_cur_pos;                          pos = bbsnet_menu.p_menu[0]->item_cur_pos;
415                          bbsnet_selchange(pos);                          bbsnet_selchange(pos);
416                          break;                          break;


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

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