/[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.18 by sysadm, Mon May 5 03:46:48 2025 UTC Revision 1.40 by sysadm, Wed May 14 04:22:45 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 21  Line 20 
20  #include "io.h"  #include "io.h"
21  #include "screen.h"  #include "screen.h"
22  #include "menu.h"  #include "menu.h"
 #include "tcplib.h"  
23  #include <stdio.h>  #include <stdio.h>
24  #include <stdarg.h>  #include <stdarg.h>
25    #include <errno.h>
26    #include <string.h>
27    #include <stdlib.h>
28    #include <fcntl.h>
29    #include <time.h>
30    #include <unistd.h>
31    #include <netdb.h>
32    #include <sys/select.h>
33  #include <sys/ioctl.h>  #include <sys/ioctl.h>
34  #include <sys/socket.h>  #include <sys/socket.h>
35    #include <sys/epoll.h>
36  #include <netinet/in.h>  #include <netinet/in.h>
37    #include <netinet/ip.h>
38  #include <arpa/inet.h>  #include <arpa/inet.h>
 #include <time.h>  
 #include <unistd.h>  
 #include <netdb.h>  
39    
40  #define TIME_OUT 15  #define MENU_CONF_DELIM " \t\r\n"
41    
42  #define MAX_PROCESS_BAR_LEN 30  #define MAX_PROCESS_BAR_LEN 30
43  #define MAXSTATION 26 * 2  #define MAXSTATION 26 * 2
44  #define STATION_PER_LINE 4  #define STATION_PER_LINE 4
# Line 42  struct _bbsnet_conf Line 48  struct _bbsnet_conf
48          char host1[20];          char host1[20];
49          char host2[40];          char host2[40];
50          char ip[40];          char ip[40];
51          int port;          in_port_t port;
52  } bbsnet_conf[MAXSTATION];  } bbsnet_conf[MAXSTATION];
53    
54  MENU_SET bbsnet_menu;  MENU_SET bbsnet_menu;
# Line 57  int load_bbsnet_conf(const char *file_co Line 63  int load_bbsnet_conf(const char *file_co
63    
64          fp = fopen(file_config, "r");          fp = fopen(file_config, "r");
65          if (fp == NULL)          if (fp == NULL)
66            {
67                  return -1;                  return -1;
68            }
69    
70          p_menu = bbsnet_menu.p_menu[0] = malloc(sizeof(MENU));          bbsnet_menu.p_menu_name_dict = trie_dict_create();
71          strcpy(p_menu->name, "BBSNET");  
72            p_menu = malloc(sizeof(MENU));
73            bbsnet_menu.p_menu[0] = p_menu;
74            strncpy(p_menu->name, "BBSNET", sizeof(p_menu->name) - 1);
75            p_menu->name[sizeof(p_menu->name) - 1] = '\0';
76          p_menu->title.show = 0;          p_menu->title.show = 0;
77          p_menu->screen.show = 0;          p_menu->screen.show = 0;
78    
79          while (fgets(t, 255, fp) && item_count < MAXSTATION)          if (trie_dict_set(bbsnet_menu.p_menu_name_dict, p_menu->name, (int64_t)p_menu) != 1)
80          {          {
81                  if (t[0] == '#' || t[0] == '*')                  log_error("Error set BBSNET menu dict [%s]\n", p_menu->name);
82                  {          }
                         continue;  
                 }  
83    
84                  t1 = strtok_r(t, " \t", &saveptr);          while (fgets(t, 255, fp) && item_count < MAXSTATION)
85                  t2 = strtok_r(NULL, " \t\n", &saveptr);          {
86                  t3 = strtok_r(NULL, " \t\n", &saveptr);                  t1 = strtok_r(t, MENU_CONF_DELIM, &saveptr);
87                  t4 = strtok_r(NULL, " \t\n", &saveptr);                  t2 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
88                    t3 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
89                    t4 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
90    
91                  if (t1 == NULL || t2 == NULL || t3 == NULL)                  if (t1 == NULL || t2 == NULL || t3 == NULL || t4 == NULL || t[0] == '#' || t[0] == '*')
92                  {                  {
93                          continue;                          continue;
94                  }                  }
95    
96                  strncpy(bbsnet_conf[item_count].host1, t2, sizeof(bbsnet_conf[item_count].host1));                  strncpy(bbsnet_conf[item_count].host1, t2, sizeof(bbsnet_conf[item_count].host1) - 1);
97                  strncpy(bbsnet_conf[item_count].host2, t1, sizeof(bbsnet_conf[item_count].host2));                  bbsnet_conf[item_count].host1[sizeof(bbsnet_conf[item_count].host1) - 1] = '\0';
98                  strncpy(bbsnet_conf[item_count].ip, t3, sizeof(bbsnet_conf[item_count].ip));                  strncpy(bbsnet_conf[item_count].host2, t1, sizeof(bbsnet_conf[item_count].host2) - 1);
99                  bbsnet_conf[item_count].port = t4 ? atoi(t4) : 23;                  bbsnet_conf[item_count].host2[sizeof(bbsnet_conf[item_count].host2) - 1] = '\0';
100                    strncpy(bbsnet_conf[item_count].ip, t3, sizeof(bbsnet_conf[item_count].ip) - 1);
101                    bbsnet_conf[item_count].ip[sizeof(bbsnet_conf[item_count].ip) - 1] = '\0';
102                    bbsnet_conf[item_count].port = (in_port_t)(t4 ? atoi(t4) : 23);
103    
104                  p_menuitem = p_menu->items[item_count] = malloc(sizeof(MENU_ITEM));                  p_menuitem = p_menu->items[item_count] = malloc(sizeof(MENU_ITEM));
105                  p_menuitem->row = 2 + item_count / STATION_PER_LINE;                  p_menuitem->row = 2 + item_count / STATION_PER_LINE;
106                  p_menuitem->col = 5 + item_count % STATION_PER_LINE * 20;                  p_menuitem->col = 5 + item_count % STATION_PER_LINE * 20;
107                  sprintf(p_menuitem->action, "%d", item_count);                  snprintf(p_menuitem->action, sizeof(p_menuitem->action), "%d", item_count);
108                  p_menuitem->submenu = 0;                  p_menuitem->submenu = 0;
109                  p_menuitem->priv = 0;                  p_menuitem->priv = 0;
110                  p_menuitem->level = 0;                  p_menuitem->level = 0;
111                  p_menuitem->display = 0;                  p_menuitem->display = 0;
112                  p_menuitem->name[0] =                  p_menuitem->name[0] =
113                          (item_count < MAXSTATION / 2 ? 'A' + item_count : 'a' + item_count);                          (char)(item_count < MAXSTATION / 2 ? 'A' + item_count : 'a' + item_count);
114                  p_menuitem->name[1] = '\0';                  p_menuitem->name[1] = '\0';
115                  snprintf(p_menuitem->text, sizeof(p_menuitem->text), "%c. %s",                  snprintf(p_menuitem->text, sizeof(p_menuitem->text), "%c. %s",
116                                  p_menuitem->name[0], t2);                                   p_menuitem->name[0], bbsnet_conf[item_count].host1);
117    
118                  item_count++;                  item_count++;
119          }          }
# Line 114  int load_bbsnet_conf(const char *file_co Line 129  int load_bbsnet_conf(const char *file_co
129          return 0;          return 0;
130  }  }
131    
132  static void  void unload_bbsnet_conf(void)
 process_bar(int n, int len)  
133  {  {
134          char buf[256];          unload_menu(&bbsnet_menu);
135          char buf2[256];  }
136          char *ptr;  
137          char *ptr2;  void process_bar(int n, int len)
138          char *ptr3;  {
139            char buf[LINE_BUFFER_LEN];
140            char buf2[LINE_BUFFER_LEN];
141    
142            if (len > LINE_BUFFER_LEN)
143            {
144                    len = LINE_BUFFER_LEN - 1;
145            }
146            if (n < 0)
147            {
148                    n = 0;
149            }
150            else if (n > len)
151            {
152                    n = len;
153            }
154    
155          moveto(4, 0);          moveto(4, 0);
156          prints("┌───────────────┐\r\n");          prints(" ------------------------------ \r\n");
157          sprintf(buf2, "            %3d%%              ", n * 100 / len);          snprintf(buf, sizeof(buf), "            %3d%%              ", n * 100 / len);
158          ptr = buf;          strncpy(buf2, buf, (size_t)n);
159          ptr2 = buf2;          buf2[n] = '\0';
160          ptr3 = buf + n;          prints("|\033[46m%s\033[44m%s\033[m|\r\n", buf2, buf + n);
161          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");  
162          iflush();          iflush();
163  }  }
164    
165  int bbsnet_connect(int n)  int bbsnet_connect(int n)
166  {  {
167          int sock, result, len, loop;          int sock, ret, loop, error;
168            int sock_connected = 0;
169            int flags_sock;
170            int flags_stdin;
171            int flags_stdout;
172            int len;
173          struct sockaddr_in sin;          struct sockaddr_in sin;
174          char buf[256];          char input_buf[LINE_BUFFER_LEN];
175          fd_set inputs, testfds;          char output_buf[LINE_BUFFER_LEN];
176          struct timeval timeout;          int input_buf_len = 0;
177          struct hostent *pHost = NULL;          int output_buf_len = 0;
178          int rv, tos = 020, i;          int input_buf_offset = 0;
179          char remote_addr[256];          int output_buf_offset = 0;
180            struct epoll_event ev, events[MAX_EVENTS];
181            int nfds, epollfd;
182            int stdin_read_wait = 0;
183            int stdout_write_wait = 0;
184            int sock_read_wait = 0;
185            int sock_write_wait = 0;
186            struct hostent *p_host = NULL;
187            int tos;
188            char remote_addr[IP_ADDR_LEN];
189          int remote_port;          int remote_port;
190          time_t t_used;          time_t t_used;
191          struct tm *tm_used;          struct tm *tm_used;
192            int ch;
193    
194          clearscr();          clearscr();
195    
196          moveto(0, 0);          moveto(0, 0);
197          prints("\033[1;32m正在测试往 %s (%s) 的连接,请稍候... \033[m\r\n",          prints("\033[1;32m正在测试往 %s (%s) 的连接,请稍候... \033[m\r\n",
198                     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);  
199          iflush();          iflush();
200    
201          pHost = gethostbyname(bbsnet_conf[n].ip);          p_host = gethostbyname(bbsnet_conf[n].ip);
202    
203          if (pHost == NULL)          if (p_host == NULL)
204          {          {
205                  prints("\033[1;31m查找主机名失败!\033[m\r\n");                  prints("\033[1;31m查找主机名失败!\033[m\r\n");
206                  press_any_key();                  press_any_key();
# Line 186  int bbsnet_connect(int n) Line 217  int bbsnet_connect(int n)
217          }          }
218    
219          sin.sin_family = AF_INET;          sin.sin_family = AF_INET;
220          sin.sin_addr.s_addr =          sin.sin_addr.s_addr = (hostaddr_server[0] != '\0' ? inet_addr(hostaddr_server) : INADDR_ANY);
                 (strlen(hostaddr_server) > 0 ? inet_addr(hostaddr_server) : INADDR_ANY);  
221          sin.sin_port = 0;          sin.sin_port = 0;
222    
223          if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0)          if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0)
224          {          {
225                  log_error("Bind address %s:%u failed\n",                  log_error("Bind address %s:%u failed (%d)\n",
226                                    inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));                                    inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), errno);
227                  return -2;                  return -2;
228          }          }
229    
230          bzero(&sin, sizeof(sin));          bzero(&sin, sizeof(sin));
231          sin.sin_family = AF_INET;          sin.sin_family = AF_INET;
232          sin.sin_addr = *(struct in_addr *)pHost->h_addr_list[0];          sin.sin_addr = *(struct in_addr *)p_host->h_addr_list[0];
233          sin.sin_port = htons(bbsnet_conf[n].port);          sin.sin_port = htons(bbsnet_conf[n].port);
234    
235          strcpy(remote_addr, inet_ntoa(sin.sin_addr));          strncpy(remote_addr, inet_ntoa(sin.sin_addr), sizeof(remote_addr) - 1);
236            remote_addr[sizeof(remote_addr) - 1] = '\0';
237          remote_port = ntohs(sin.sin_port);          remote_port = ntohs(sin.sin_port);
238    
239          prints("\033[1;32m穿梭进度条提示您当前已使用的时间。\033[m\r\n");          prints("\033[1;32m穿梭进度条提示您当前已使用的时间,按\033[1;33mCtrl+C\033[1;32m中断。\033[m\r\n");
240          process_bar(0, MAX_PROCESS_BAR_LEN);          process_bar(0, MAX_PROCESS_BAR_LEN);
241          for (i = 0; i < MAX_PROCESS_BAR_LEN; i++)  
242            // Set socket as non-blocking
243            flags_sock = fcntl(sock, F_GETFL, 0);
244            fcntl(sock, F_SETFL, flags_sock | O_NONBLOCK);
245    
246            // Set STDIN/STDOUT as non-blocking
247            flags_stdin = fcntl(STDIN_FILENO, F_GETFL, 0);
248            flags_stdout = fcntl(STDOUT_FILENO, F_GETFL, 0);
249            fcntl(STDIN_FILENO, F_SETFL, flags_stdin | O_NONBLOCK);
250            fcntl(STDOUT_FILENO, F_SETFL, flags_stdout | O_NONBLOCK);
251    
252            epollfd = epoll_create1(0);
253            if (epollfd < 0)
254            {
255                    log_error("epoll_create1() error (%d)\n", errno);
256                    return -1;
257            }
258    
259            ev.events = EPOLLOUT;
260            ev.data.fd = sock;
261            if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sock, &ev) == -1)
262            {
263                    log_error("epoll_ctl(socket) error (%d)\n", errno);
264                    goto cleanup;
265            }
266    
267            ev.events = EPOLLIN;
268            ev.data.fd = STDIN_FILENO;
269            if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1)
270            {
271                    log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno);
272                    goto cleanup;
273            }
274    
275            while (!SYS_server_exit)
276          {          {
277                  if (i == 0)                  if ((ret = connect(sock, (struct sockaddr *)&sin, sizeof(sin))) < 0)
                         rv =  
                                 NonBlockConnectEx(sock, (struct sockaddr *)&sin, sizeof(sin),  
                                                                   500, 1);  
                 else  
                         rv =  
                                 NonBlockConnectEx(sock, (struct sockaddr *)&sin, sizeof(sin),  
                                                                   500, 0);  
                 if (rv == ERR_TCPLIB_TIMEOUT)  
278                  {                  {
279                          process_bar(i + 1, MAX_PROCESS_BAR_LEN);                          if (errno == EAGAIN || errno == EALREADY || errno == EINPROGRESS)
280                          continue;                          {
281                                    // Use select / epoll to check writability of the socket,
282                                    // then use getsockopt to check the status of the socket.
283                                    // See man connect(2)
284                                    break;
285                            }
286                            else if (errno == EINTR)
287                            {
288                                    continue;
289                            }
290                            else
291                            {
292                                    log_error("connect(socket) error (%d)\n", errno);
293    
294                                    prints("\033[1;31m连接失败!\033[m\r\n");
295                                    press_any_key();
296    
297                                    goto cleanup;
298                            }
299                  }                  }
300                  else if (rv == 0)          }
301                          break;  
302                  else          for (int j = 0; j < MAX_PROCESS_BAR_LEN && !sock_connected && !SYS_server_exit; j++)
303            {
304                    nfds = epoll_wait(epollfd, events, MAX_EVENTS, 500); // 0.5 second
305    
306                    if (nfds < 0)
307                  {                  {
308                          prints("\033[1;31m连接失败!\033[m\r\n");                          if (errno != EINTR)
309                          press_any_key();                          {
310                          return -1;                                  log_error("epoll_wait() error (%d)\n", errno);
311                                    break;
312                            }
313                  }                  }
314                    else if (nfds == 0) // timeout
315                    {
316                            process_bar(j + 1, MAX_PROCESS_BAR_LEN);
317                    }
318                    else // ret > 0
319                    {
320                            for (int i = 0; i < nfds; i++)
321                            {
322                                    if (events[i].data.fd == sock)
323                                    {
324                                            len = sizeof(error);
325                                            if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, (socklen_t *)&len) < 0)
326                                            {
327                                                    log_error("getsockopt() error (%d) !\n", error);
328                                                    goto cleanup;
329                                            }
330                                            if (error == 0)
331                                            {
332                                                    sock_connected = 1;
333                                            }
334                                    }
335                                    else if (events[i].data.fd == STDIN_FILENO)
336                                    {
337                                            ch = igetch(0);
338                                            if (ch == Ctrl('C'))
339                                            {
340                                                    goto cleanup;
341                                            }
342                                    }
343                            }
344                    }
345            }
346            if (SYS_server_exit)
347            {
348                    goto cleanup;
349          }          }
350          if (i == MAX_PROCESS_BAR_LEN)          if (!sock_connected)
351          {          {
352                  prints("\033[1;31m连接超时!\033[m\r\n");                  prints("\033[1;31m连接失败!\033[m\r\n");
353                  press_any_key();                  press_any_key();
354                  return -1;  
355                    goto cleanup;
356            }
357    
358            tos = IPTOS_LOWDELAY;
359            if (setsockopt(sock, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)
360            {
361                    log_error("setsockopt IP_TOS=%d error (%d)\n", tos, errno);
362          }          }
         setsockopt(sock, IPPROTO_IP, IP_TOS, &tos, sizeof(int));  
363    
364          prints("\033[1;31m连接成功!\033[m\r\n");          prints("\033[1;31m连接成功!\033[m\r\n");
365            iflush();
366          log_std("BBSNET connect to %s:%d\n", remote_addr, remote_port);          log_std("BBSNET connect to %s:%d\n", remote_addr, remote_port);
367    
368          FD_ZERO(&inputs);          ev.events = EPOLLIN | EPOLLOUT | EPOLLET;
369          FD_SET(0, &inputs);          ev.data.fd = sock;
370          FD_SET(sock, &inputs);          if (epoll_ctl(epollfd, EPOLL_CTL_MOD, sock, &ev) == -1)
371            {
372                    log_error("epoll_ctl(socket) error (%d)\n", errno);
373                    goto cleanup;
374            }
375    
376          BBS_last_access_tm = t_used = time(0);          ev.events = EPOLLOUT;
377            ev.data.fd = STDOUT_FILENO;
378            if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1)
379            {
380                    log_error("epoll_ctl(STDOUT_FILENO) error (%d)\n", errno);
381                    goto cleanup;
382            }
383    
384            BBS_last_access_tm = t_used = time(0);
385          loop = 1;          loop = 1;
386    
387          while (loop)          while (loop && !SYS_server_exit)
388          {          {
389                  testfds = inputs;                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second
                 timeout.tv_sec = TIME_OUT;  
                 timeout.tv_usec = 0;  
390    
391                  result = SignalSafeSelect(FD_SETSIZE, &testfds, (fd_set *)NULL,                  if (nfds < 0)
                                                                   (fd_set *)NULL, &timeout);  
   
                 if (result == 0)  
392                  {                  {
393                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (errno != EINTR)
394                          {                          {
395                                  loop = 0;                                  log_error("epoll_wait() error (%d)\n", errno);
396                                    break;
397                          }                          }
398                            continue;
399                  }                  }
400                  if (result < 0)                  else if (nfds == 0) // timeout
401                  {                  {
402                          log_error("select() error (%d) !\n", result);                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)
403                          loop = 0;                          {
404                                    break;
405                            }
406                            continue;
407                  }                  }
408                  if (result > 0)  
409                    for (int i = 0; i < nfds; i++)
410                  {                  {
411                          if (FD_ISSET(0, &testfds))                          if (events[i].data.fd == STDIN_FILENO || stdin_read_wait)
412                            {
413                                    stdin_read_wait = 1;
414                                    while (input_buf_len < sizeof(input_buf) && !SYS_server_exit)
415                                    {
416                                            ret = (int)read(STDIN_FILENO, input_buf + input_buf_len, sizeof(input_buf) - (size_t)input_buf_len);
417                                            if (ret < 0)
418                                            {
419                                                    if (errno == EAGAIN || errno == EWOULDBLOCK)
420                                                    {
421                                                            stdin_read_wait = 0;
422                                                            break;
423                                                    }
424                                                    else if (errno == EINTR)
425                                                    {
426                                                            continue;
427                                                    }
428                                                    else
429                                                    {
430                                                            log_error("read(STDIN) error (%d)\n", errno);
431                                                            loop = 0;
432                                                            break;
433                                                    }
434                                            }
435                                            else if (ret == 0) // broken pipe
436                                            {
437                                                    log_std("read(STDIN) EOF\n");
438                                                    stdin_read_wait = 0;
439                                                    loop = 0;
440                                                    break;
441                                            }
442                                            else
443                                            {
444                                                    input_buf_len += ret;
445                                                    BBS_last_access_tm = time(0);
446                                                    continue;
447                                            }
448                                    }
449                            }
450    
451                            if (events[i].data.fd == sock || sock_write_wait) // EPOLLOUT
452                          {                          {
453                                  len = read(0, buf, 255);                                  sock_write_wait = 1;
454                                  if (len == 0)                                  while (input_buf_offset < input_buf_len && !SYS_server_exit)
455                                  {                                  {
456                                          loop = 0;                                          ret = (int)write(sock, input_buf + input_buf_offset, (size_t)(input_buf_len - input_buf_offset));
457                                            if (ret < 0)
458                                            {
459                                                    if (errno == EAGAIN || errno == EWOULDBLOCK)
460                                                    {
461                                                            sock_write_wait = 0;
462                                                            break;
463                                                    }
464                                                    else if (errno == EINTR)
465                                                    {
466                                                            continue;
467                                                    }
468                                                    else
469                                                    {
470                                                            log_error("write(socket) error (%d)\n", errno);
471                                                            loop = 0;
472                                                            break;
473                                                    }
474                                            }
475                                            else if (ret == 0) // broken pipe
476                                            {
477                                                    log_std("write(socket) EOF\n");
478                                                    sock_write_wait = 0;
479                                                    loop = 0;
480                                                    break;
481                                            }
482                                            else
483                                            {
484                                                    input_buf_offset += ret;
485                                                    if (input_buf_offset >= input_buf_len) // Output buffer complete
486                                                    {
487                                                            input_buf_offset = 0;
488                                                            input_buf_len = 0;
489                                                            break;
490                                                    }
491                                                    continue;
492                                            }
493                                  }                                  }
                                 write(sock, buf, len);  
494                          }                          }
495                          if (FD_ISSET(sock, &testfds))  
496                            if (events[i].data.fd == sock || sock_read_wait) // EPOLLIN
497                            {
498                                    sock_read_wait = 1;
499                                    while (output_buf_len < sizeof(output_buf) && !SYS_server_exit)
500                                    {
501                                            ret = (int)read(sock, output_buf + output_buf_len, sizeof(output_buf) - (size_t)output_buf_len);
502                                            if (ret < 0)
503                                            {
504                                                    if (errno == EAGAIN || errno == EWOULDBLOCK)
505                                                    {
506                                                            sock_read_wait = 0;
507                                                            break;
508                                                    }
509                                                    else if (errno == EINTR)
510                                                    {
511                                                            continue;
512                                                    }
513                                                    else
514                                                    {
515                                                            log_error("read(socket) error (%d)\n", errno);
516                                                            loop = 0;
517                                                            break;
518                                                    }
519                                            }
520                                            else if (ret == 0) // broken pipe
521                                            {
522                                                    log_std("read(socket) EOF\n");
523                                                    sock_read_wait = 0;
524                                                    loop = 0;
525                                                    break;
526                                            }
527                                            else
528                                            {
529                                                    output_buf_len += ret;
530                                                    continue;
531                                            }
532                                    }
533                            }
534    
535                            if (events[i].data.fd == STDOUT_FILENO || stdout_write_wait)
536                          {                          {
537                                  len = read(sock, buf, 255);                                  stdout_write_wait = 1;
538                                  if (len == 0)                                  while (output_buf_offset < output_buf_len && !SYS_server_exit)
539                                  {                                  {
540                                          loop = 0;                                          ret = (int)write(STDOUT_FILENO, output_buf + output_buf_offset, (size_t)(output_buf_len - output_buf_offset));
541                                            if (ret < 0)
542                                            {
543                                                    if (errno == EAGAIN || errno == EWOULDBLOCK)
544                                                    {
545                                                            stdout_write_wait = 0;
546                                                            break;
547                                                    }
548                                                    else if (errno == EINTR)
549                                                    {
550                                                            continue;
551                                                    }
552                                                    else
553                                                    {
554                                                            log_error("write(STDOUT) error (%d)\n", errno);
555                                                            loop = 0;
556                                                            break;
557                                                    }
558                                            }
559                                            else if (ret == 0) // broken pipe
560                                            {
561                                                    log_std("write(STDOUT) EOF\n");
562                                                    stdout_write_wait = 0;
563                                                    loop = 0;
564                                                    break;
565                                            }
566                                            else
567                                            {
568                                                    output_buf_offset += ret;
569                                                    if (output_buf_offset >= output_buf_len) // Output buffer complete
570                                                    {
571                                                            output_buf_offset = 0;
572                                                            output_buf_len = 0;
573                                                            break;
574                                                    }
575                                                    continue;
576                                            }
577                                  }                                  }
                                 write(1, buf, len);  
578                          }                          }
                         BBS_last_access_tm = time(0);  
579                  }                  }
580          }          }
581    
582    cleanup:
583            if (close(epollfd) < 0)
584            {
585                    log_error("close(epoll) error (%d)\n");
586            }
587    
588            // Restore STDIN/STDOUT flags
589            fcntl(STDIN_FILENO, F_SETFL, flags_stdin);
590            fcntl(STDOUT_FILENO, F_SETFL, flags_stdout);
591    
592            // Restore socket flags
593            fcntl(sock, F_SETFL, flags_sock);
594    
595          if (close(sock) == -1)          if (close(sock) == -1)
596          {          {
597                  log_error("Close socket failed\n");                  log_error("Close socket failed\n");
# Line 313  int bbsnet_connect(int n) Line 610  int bbsnet_connect(int n)
610  static int  static int
611  bbsnet_refresh()  bbsnet_refresh()
612  {  {
         int i;  
   
613          clearscr();          clearscr();
614          moveto(1, 0);          moveto(1, 0);
615          prints("╭══════════════════════════════════════╮");          prints(" ----------------------------------------------------------------------------- ");
616          for (i = 2; i < 19; i++)          for (int i = 2; i < 19; i++)
617          {          {
618                  moveto(i, 0);                  moveto(i, 0);
619                  prints("║");                  prints("|");
620                  moveto(i, 79);                  moveto(i, 79);
621                  prints("║");                  prints("|");
622          }          }
623          moveto(19, 0);          moveto(19, 0);
624          prints("║——————————————————————————————————————║");          prints("|-----------------------------------------------------------------------------|");
625          moveto(22, 0);          moveto(22, 0);
626          prints("╰══════════════════════════════════════╯");          prints(" ----------------------------------------------------------------------------- ");
627          moveto(23, 0);          moveto(23, 0);
628          prints(" [\x1b[1;32mCtrl+C\x1b[m]退出");          prints(" [\x1b[1;32mCtrl+C\x1b[m]退出");
629    
# Line 341  int bbsnet_selchange(int new_pos) Line 636  int bbsnet_selchange(int new_pos)
636  {  {
637          moveto(20, 0);          moveto(20, 0);
638          clrtoeol();          clrtoeol();
639          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",
640                     bbsnet_conf[new_pos].host2, bbsnet_conf[new_pos].host1);                     bbsnet_conf[new_pos].host2, bbsnet_conf[new_pos].host1);
641          moveto(20, 79);          moveto(20, 79);
642          prints("║");          prints("|");
643          moveto(21, 0);          moveto(21, 0);
644          clrtoeol();          clrtoeol();
645          prints("║\x1b[1m连往:\x1b[1;33m%-20s", bbsnet_conf[new_pos].ip);          prints("|\x1b[1m连往:\x1b[1;33m%-20s", bbsnet_conf[new_pos].ip);
646          if (bbsnet_conf[new_pos].port != 23)          if (bbsnet_conf[new_pos].port != 23)
647            {
648                  prints("  %d", bbsnet_conf[new_pos].port);                  prints("  %d", bbsnet_conf[new_pos].port);
649            }
650          prints("\x1b[m");          prints("\x1b[m");
651          moveto(21, 79);          moveto(21, 79);
652          prints("║");          prints("|");
653          iflush();          iflush();
654    
655          return 0;          return 0;
# Line 372  int bbs_net() Line 669  int bbs_net()
669          display_menu(get_menu(&bbsnet_menu, "BBSNET"));          display_menu(get_menu(&bbsnet_menu, "BBSNET"));
670          bbsnet_selchange(pos);          bbsnet_selchange(pos);
671    
672          while (1)          while (!SYS_server_exit)
673          {          {
674                  ch = igetch(0);                  ch = igetch(100);
675                  switch (ch)                  switch (ch)
676                  {                  {
677                  case KEY_NULL:                  case KEY_NULL:  // broken pipe
678                  case Ctrl('C'):                  case Ctrl('C'): // user cancel
679                          return 0;                          goto cleanup;
680                  case KEY_TIMEOUT:                  case KEY_TIMEOUT:
681                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)
682                          {                          {
683                                  return -1;                                  goto cleanup;
684                          }                          }
685                          continue;                          continue;
686                  case CR:                  case CR:
# Line 395  int bbs_net() Line 692  int bbs_net()
692                          break;                          break;
693                  case KEY_UP:                  case KEY_UP:
694                          for (i = 0; i < STATION_PER_LINE; i++)                          for (i = 0; i < STATION_PER_LINE; i++)
695                            {
696                                  menu_control(&bbsnet_menu, KEY_UP);                                  menu_control(&bbsnet_menu, KEY_UP);
697                            }
698                          pos = bbsnet_menu.p_menu[0]->item_cur_pos;                          pos = bbsnet_menu.p_menu[0]->item_cur_pos;
699                          bbsnet_selchange(pos);                          bbsnet_selchange(pos);
700                          break;                          break;
701                  case KEY_DOWN:                  case KEY_DOWN:
702                          for (i = 0; i < STATION_PER_LINE; i++)                          for (i = 0; i < STATION_PER_LINE; i++)
703                            {
704                                  menu_control(&bbsnet_menu, KEY_DOWN);                                  menu_control(&bbsnet_menu, KEY_DOWN);
705                            }
706                          pos = bbsnet_menu.p_menu[0]->item_cur_pos;                          pos = bbsnet_menu.p_menu[0]->item_cur_pos;
707                          bbsnet_selchange(pos);                          bbsnet_selchange(pos);
708                          break;                          break;
# Line 424  int bbs_net() Line 725  int bbs_net()
725                  BBS_last_access_tm = time(0);                  BBS_last_access_tm = time(0);
726          }          }
727    
728    cleanup:
729            unload_bbsnet_conf();
730    
731          return 0;          return 0;
732  }  }


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

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