/[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.22 by sysadm, Mon May 5 11:46:04 2025 UTC Revision 1.39 by sysadm, Tue May 13 07:31:48 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 69  int load_bbsnet_conf(const char *file_co Line 75  int load_bbsnet_conf(const char *file_co
75    
76          while (fgets(t, 255, fp) && item_count < MAXSTATION)          while (fgets(t, 255, fp) && item_count < MAXSTATION)
77          {          {
78                  t1 = strtok_r(t, " \t", &saveptr);                  t1 = strtok_r(t, MENU_CONF_DELIM, &saveptr);
79                  t2 = strtok_r(NULL, " \t\n", &saveptr);                  t2 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
80                  t3 = strtok_r(NULL, " \t\n", &saveptr);                  t3 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
81                  t4 = strtok_r(NULL, " \t\n", &saveptr);                  t4 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
82    
83                  if (t1 == NULL || t2 == NULL || t3 == NULL || t[0] == '#' || t[0] == '*')                  if (t1 == NULL || t2 == NULL || t3 == NULL || t4 == NULL || t[0] == '#' || t[0] == '*')
84                  {                  {
85                          continue;                          continue;
86                  }                  }
# Line 85  int load_bbsnet_conf(const char *file_co Line 91  int load_bbsnet_conf(const char *file_co
91                  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';
92                  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);
93                  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';
94                  bbsnet_conf[item_count].port = t4 ? atoi(t4) : 23;                  bbsnet_conf[item_count].port = (in_port_t)(t4 ? atoi(t4) : 23);
95    
96                  p_menuitem = p_menu->items[item_count] = malloc(sizeof(MENU_ITEM));                  p_menuitem = p_menu->items[item_count] = malloc(sizeof(MENU_ITEM));
97                  p_menuitem->row = 2 + item_count / STATION_PER_LINE;                  p_menuitem->row = 2 + item_count / STATION_PER_LINE;
# Line 96  int load_bbsnet_conf(const char *file_co Line 102  int load_bbsnet_conf(const char *file_co
102                  p_menuitem->level = 0;                  p_menuitem->level = 0;
103                  p_menuitem->display = 0;                  p_menuitem->display = 0;
104                  p_menuitem->name[0] =                  p_menuitem->name[0] =
105                          (item_count < MAXSTATION / 2 ? 'A' + item_count : 'a' + item_count);                          (char)(item_count < MAXSTATION / 2 ? 'A' + item_count : 'a' + item_count);
106                  p_menuitem->name[1] = '\0';                  p_menuitem->name[1] = '\0';
107                  snprintf(p_menuitem->text, sizeof(p_menuitem->text), "%c. %s",                  snprintf(p_menuitem->text, sizeof(p_menuitem->text), "%c. %s",
108                                   p_menuitem->name[0], bbsnet_conf[item_count].host1);                                   p_menuitem->name[0], bbsnet_conf[item_count].host1);
# Line 117  int load_bbsnet_conf(const char *file_co Line 123  int load_bbsnet_conf(const char *file_co
123    
124  static void process_bar(int n, int len)  static void process_bar(int n, int len)
125  {  {
126          char buf[256];          char buf[LINE_BUFFER_LEN];
127          char buf2[256];          char buf2[LINE_BUFFER_LEN];
128          char *ptr;  
129          char *ptr2;          if (len > LINE_BUFFER_LEN)
130          char *ptr3;          {
131                    len = LINE_BUFFER_LEN - 1;
132            }
133            if (n < 0)
134            {
135                    n = 0;
136            }
137            else if (n > len)
138            {
139                    n = len;
140            }
141    
142          moveto(4, 0);          moveto(4, 0);
143          prints("┌──────────────────────────────┐\r\n");          prints(" ------------------------------ \r\n");
144          snprintf(buf2, sizeof(buf2), "            %3d%%              ", n * 100 / len);          snprintf(buf, sizeof(buf), "            %3d%%              ", n * 100 / len);
145          ptr = buf;          strncpy(buf2, buf, (size_t)n);
146          ptr2 = buf2;          buf2[n] = '\0';
147          ptr3 = buf + n;          prints("|\033[46m%s\033[44m%s\033[m|\r\n", buf2, buf + n);
148          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");  
149          iflush();          iflush();
150  }  }
151    
152  int bbsnet_connect(int n)  int bbsnet_connect(int n)
153  {  {
154          int sock, result, len, loop;          int sock, ret, loop, error;
155            int sock_connected = 0;
156            int flags_sock;
157            int flags_stdin;
158            int flags_stdout;
159            int len;
160          struct sockaddr_in sin;          struct sockaddr_in sin;
161          char buf[256];          char input_buf[LINE_BUFFER_LEN];
162          fd_set inputs, testfds;          char output_buf[LINE_BUFFER_LEN];
163          struct timeval timeout;          int input_buf_len = 0;
164          struct hostent *pHost = NULL;          int output_buf_len = 0;
165          int rv, tos = 020, i;          int input_buf_offset = 0;
166          char remote_addr[256];          int output_buf_offset = 0;
167            struct epoll_event ev, events[MAX_EVENTS];
168            int nfds, epollfd;
169            int stdin_read_wait = 0;
170            int stdout_write_wait = 0;
171            int sock_read_wait = 0;
172            int sock_write_wait = 0;
173            struct hostent *p_host = NULL;
174            int tos;
175            char remote_addr[IP_ADDR_LEN];
176          int remote_port;          int remote_port;
177          time_t t_used;          time_t t_used;
178          struct tm *tm_used;          struct tm *tm_used;
179            int ch;
180    
181          clearscr();          clearscr();
182    
183          moveto(0, 0);          moveto(0, 0);
184          prints("\033[1;32m正在测试往 %s (%s) 的连接,请稍候... \033[m\r\n",          prints("\033[1;32m正在测试往 %s (%s) 的连接,请稍候... \033[m\r\n",
185                     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);  
186          iflush();          iflush();
187    
188          pHost = gethostbyname(bbsnet_conf[n].ip);          p_host = gethostbyname(bbsnet_conf[n].ip);
189    
190          if (pHost == NULL)          if (p_host == NULL)
191          {          {
192                  prints("\033[1;31m查找主机名失败!\033[m\r\n");                  prints("\033[1;31m查找主机名失败!\033[m\r\n");
193                  press_any_key();                  press_any_key();
# Line 186  int bbsnet_connect(int n) Line 204  int bbsnet_connect(int n)
204          }          }
205    
206          sin.sin_family = AF_INET;          sin.sin_family = AF_INET;
207          sin.sin_addr.s_addr =          sin.sin_addr.s_addr = (hostaddr_server[0] != '\0' ? inet_addr(hostaddr_server) : INADDR_ANY);
                 (strnlen(hostaddr_server, sizeof(hostaddr_server)) > 0 ? inet_addr(hostaddr_server) : INADDR_ANY);  
208          sin.sin_port = 0;          sin.sin_port = 0;
209    
210          if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0)          if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0)
211          {          {
212                  log_error("Bind address %s:%u failed\n",                  log_error("Bind address %s:%u failed (%d)\n",
213                                    inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));                                    inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), errno);
214                  return -2;                  return -2;
215          }          }
216    
217          bzero(&sin, sizeof(sin));          bzero(&sin, sizeof(sin));
218          sin.sin_family = AF_INET;          sin.sin_family = AF_INET;
219          sin.sin_addr = *(struct in_addr *)pHost->h_addr_list[0];          sin.sin_addr = *(struct in_addr *)p_host->h_addr_list[0];
220          sin.sin_port = htons(bbsnet_conf[n].port);          sin.sin_port = htons(bbsnet_conf[n].port);
221    
222          strncpy(remote_addr, inet_ntoa(sin.sin_addr), sizeof(remote_addr) - 1);          strncpy(remote_addr, inet_ntoa(sin.sin_addr), sizeof(remote_addr) - 1);
223          remote_addr[sizeof(remote_addr) - 1] = '\0';          remote_addr[sizeof(remote_addr) - 1] = '\0';
224          remote_port = ntohs(sin.sin_port);          remote_port = ntohs(sin.sin_port);
225    
226          prints("\033[1;32m穿梭进度条提示您当前已使用的时间。\033[m\r\n");          prints("\033[1;32m穿梭进度条提示您当前已使用的时间,按\033[1;33mCtrl+C\033[1;32m中断。\033[m\r\n");
227          process_bar(0, MAX_PROCESS_BAR_LEN);          process_bar(0, MAX_PROCESS_BAR_LEN);
228          for (i = 0; i < MAX_PROCESS_BAR_LEN; i++)  
229            // Set socket as non-blocking
230            flags_sock = fcntl(sock, F_GETFL, 0);
231            fcntl(sock, F_SETFL, flags_sock | O_NONBLOCK);
232    
233            // Set STDIN/STDOUT as non-blocking
234            flags_stdin = fcntl(STDIN_FILENO, F_GETFL, 0);
235            flags_stdout = fcntl(STDOUT_FILENO, F_GETFL, 0);
236            fcntl(STDIN_FILENO, F_SETFL, flags_stdin | O_NONBLOCK);
237            fcntl(STDOUT_FILENO, F_SETFL, flags_stdout | O_NONBLOCK);
238    
239            epollfd = epoll_create1(0);
240            if (epollfd < 0)
241            {
242                    log_error("epoll_create1() error (%d)\n", errno);
243                    return -1;
244            }
245    
246            ev.events = EPOLLOUT;
247            ev.data.fd = sock;
248            if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sock, &ev) == -1)
249            {
250                    log_error("epoll_ctl(socket) error (%d)\n", errno);
251                    goto cleanup;
252            }
253    
254            ev.events = EPOLLIN;
255            ev.data.fd = STDIN_FILENO;
256            if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1)
257            {
258                    log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno);
259                    goto cleanup;
260            }
261    
262            while (!SYS_server_exit)
263          {          {
264                  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)  
265                  {                  {
266                          process_bar(i + 1, MAX_PROCESS_BAR_LEN);                          if (errno == EAGAIN || errno == EALREADY || errno == EINPROGRESS)
267                          continue;                          {
268                                    // Use select / epoll to check writability of the socket,
269                                    // then use getsockopt to check the status of the socket.
270                                    // See man connect(2)
271                                    break;
272                            }
273                            else if (errno == EINTR)
274                            {
275                                    continue;
276                            }
277                            else
278                            {
279                                    log_error("connect(socket) error (%d)\n", errno);
280    
281                                    prints("\033[1;31m连接失败!\033[m\r\n");
282                                    press_any_key();
283    
284                                    goto cleanup;
285                            }
286                  }                  }
287                  else if (rv == 0)          }
288    
289            for (int j = 0; j < MAX_PROCESS_BAR_LEN && !sock_connected && !SYS_server_exit; j++)
290            {
291                    nfds = epoll_wait(epollfd, events, MAX_EVENTS, 500); // 0.5 second
292    
293                    if (nfds < 0)
294                  {                  {
295                          break;                          if (errno != EINTR)
296                            {
297                                    log_error("epoll_wait() error (%d)\n", errno);
298                                    break;
299                            }
300                  }                  }
301                  else                  else if (nfds == 0) // timeout
302                  {                  {
303                          prints("\033[1;31m连接失败!\033[m\r\n");                          process_bar(j + 1, MAX_PROCESS_BAR_LEN);
304                          press_any_key();                  }
305                          return -1;                  else // ret > 0
306                    {
307                            for (int i = 0; i < nfds; i++)
308                            {
309                                    if (events[i].data.fd == sock)
310                                    {
311                                            len = sizeof(error);
312                                            if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, (socklen_t *)&len) < 0)
313                                            {
314                                                    log_error("getsockopt() error (%d) !\n", error);
315                                                    goto cleanup;
316                                            }
317                                            if (error == 0)
318                                            {
319                                                    sock_connected = 1;
320                                            }
321                                    }
322                                    else if (events[i].data.fd == STDIN_FILENO)
323                                    {
324                                            ch = igetch(0);
325                                            if (ch == Ctrl('C'))
326                                            {
327                                                    goto cleanup;
328                                            }
329                                    }
330                            }
331                  }                  }
332          }          }
333          if (i == MAX_PROCESS_BAR_LEN)          if (SYS_server_exit)
334          {          {
335                  prints("\033[1;31m连接超时!\033[m\r\n");                  goto cleanup;
336            }
337            if (!sock_connected)
338            {
339                    prints("\033[1;31m连接失败!\033[m\r\n");
340                  press_any_key();                  press_any_key();
341                  return -1;  
342                    goto cleanup;
343            }
344    
345            tos = IPTOS_LOWDELAY;
346            if (setsockopt(sock, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)
347            {
348                    log_error("setsockopt IP_TOS=%d error (%d)\n", tos, errno);
349          }          }
         setsockopt(sock, IPPROTO_IP, IP_TOS, &tos, sizeof(int));  
350    
351          prints("\033[1;31m连接成功!\033[m\r\n");          prints("\033[1;31m连接成功!\033[m\r\n");
352            iflush();
353          log_std("BBSNET connect to %s:%d\n", remote_addr, remote_port);          log_std("BBSNET connect to %s:%d\n", remote_addr, remote_port);
354    
355          FD_ZERO(&inputs);          ev.events = EPOLLIN | EPOLLOUT | EPOLLET;
356          FD_SET(0, &inputs);          ev.data.fd = sock;
357          FD_SET(sock, &inputs);          if (epoll_ctl(epollfd, EPOLL_CTL_MOD, sock, &ev) == -1)
358            {
359                    log_error("epoll_ctl(socket) error (%d)\n", errno);
360                    goto cleanup;
361            }
362    
363          BBS_last_access_tm = t_used = time(0);          ev.events = EPOLLOUT;
364            ev.data.fd = STDOUT_FILENO;
365            if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1)
366            {
367                    log_error("epoll_ctl(STDOUT_FILENO) error (%d)\n", errno);
368                    goto cleanup;
369            }
370    
371            BBS_last_access_tm = t_used = time(0);
372          loop = 1;          loop = 1;
373    
374          while (loop)          while (loop && !SYS_server_exit)
375          {          {
376                  testfds = inputs;                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second
                 timeout.tv_sec = TIME_OUT;  
                 timeout.tv_usec = 0;  
   
                 result = SignalSafeSelect(FD_SETSIZE, &testfds, (fd_set *)NULL,  
                                                                   (fd_set *)NULL, &timeout);  
377    
378                  if (result == 0)                  if (nfds < 0)
379                  {                  {
380                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (errno != EINTR)
381                          {                          {
382                                  loop = 0;                                  log_error("epoll_wait() error (%d)\n", errno);
383                                    break;
384                          }                          }
385                            continue;
386                  }                  }
387                  if (result < 0)                  else if (nfds == 0) // timeout
388                  {                  {
389                          log_error("select() error (%d) !\n", result);                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)
390                          loop = 0;                          {
391                                    break;
392                            }
393                            continue;
394                  }                  }
395                  if (result > 0)  
396                    for (int i = 0; i < nfds; i++)
397                  {                  {
398                          if (FD_ISSET(0, &testfds))                          if (events[i].data.fd == STDIN_FILENO || stdin_read_wait)
399                          {                          {
400                                  len = read(0, buf, 255);                                  stdin_read_wait = 1;
401                                  if (len == 0)                                  while (input_buf_len < sizeof(input_buf) && !SYS_server_exit)
402                                  {                                  {
403                                          loop = 0;                                          ret = (int)read(STDIN_FILENO, input_buf + input_buf_len, sizeof(input_buf) - (size_t)input_buf_len);
404                                            if (ret < 0)
405                                            {
406                                                    if (errno == EAGAIN || errno == EWOULDBLOCK)
407                                                    {
408                                                            stdin_read_wait = 0;
409                                                            break;
410                                                    }
411                                                    else if (errno == EINTR)
412                                                    {
413                                                            continue;
414                                                    }
415                                                    else
416                                                    {
417                                                            log_error("read(STDIN) error (%d)\n", errno);
418                                                            loop = 0;
419                                                            break;
420                                                    }
421                                            }
422                                            else if (ret == 0) // broken pipe
423                                            {
424                                                    log_std("read(STDIN) EOF\n");
425                                                    stdin_read_wait = 0;
426                                                    loop = 0;
427                                                    break;
428                                            }
429                                            else
430                                            {
431                                                    input_buf_len += ret;
432                                                    BBS_last_access_tm = time(0);
433                                                    continue;
434                                            }
435                                  }                                  }
                                 write(sock, buf, len);  
436                          }                          }
437                          if (FD_ISSET(sock, &testfds))  
438                            if (events[i].data.fd == sock || sock_write_wait) // EPOLLOUT
439                            {
440                                    sock_write_wait = 1;
441                                    while (input_buf_offset < input_buf_len && !SYS_server_exit)
442                                    {
443                                            ret = (int)write(sock, input_buf + input_buf_offset, (size_t)(input_buf_len - input_buf_offset));
444                                            if (ret < 0)
445                                            {
446                                                    if (errno == EAGAIN || errno == EWOULDBLOCK)
447                                                    {
448                                                            sock_write_wait = 0;
449                                                            break;
450                                                    }
451                                                    else if (errno == EINTR)
452                                                    {
453                                                            continue;
454                                                    }
455                                                    else
456                                                    {
457                                                            log_error("write(socket) error (%d)\n", errno);
458                                                            loop = 0;
459                                                            break;
460                                                    }
461                                            }
462                                            else if (ret == 0) // broken pipe
463                                            {
464                                                    log_std("write(socket) EOF\n");
465                                                    sock_write_wait = 0;
466                                                    loop = 0;
467                                                    break;
468                                            }
469                                            else
470                                            {
471                                                    input_buf_offset += ret;
472                                                    if (input_buf_offset >= input_buf_len) // Output buffer complete
473                                                    {
474                                                            input_buf_offset = 0;
475                                                            input_buf_len = 0;
476                                                            break;
477                                                    }
478                                                    continue;
479                                            }
480                                    }
481                            }
482    
483                            if (events[i].data.fd == sock || sock_read_wait) // EPOLLIN
484                          {                          {
485                                  len = read(sock, buf, 255);                                  sock_read_wait = 1;
486                                  if (len == 0)                                  while (output_buf_len < sizeof(output_buf) && !SYS_server_exit)
487                                  {                                  {
488                                          loop = 0;                                          ret = (int)read(sock, output_buf + output_buf_len, sizeof(output_buf) - (size_t)output_buf_len);
489                                            if (ret < 0)
490                                            {
491                                                    if (errno == EAGAIN || errno == EWOULDBLOCK)
492                                                    {
493                                                            sock_read_wait = 0;
494                                                            break;
495                                                    }
496                                                    else if (errno == EINTR)
497                                                    {
498                                                            continue;
499                                                    }
500                                                    else
501                                                    {
502                                                            log_error("read(socket) error (%d)\n", errno);
503                                                            loop = 0;
504                                                            break;
505                                                    }
506                                            }
507                                            else if (ret == 0) // broken pipe
508                                            {
509                                                    log_std("read(socket) EOF\n");
510                                                    sock_read_wait = 0;
511                                                    loop = 0;
512                                                    break;
513                                            }
514                                            else
515                                            {
516                                                    output_buf_len += ret;
517                                                    continue;
518                                            }
519                                    }
520                            }
521    
522                            if (events[i].data.fd == STDOUT_FILENO || stdout_write_wait)
523                            {
524                                    stdout_write_wait = 1;
525                                    while (output_buf_offset < output_buf_len && !SYS_server_exit)
526                                    {
527                                            ret = (int)write(STDOUT_FILENO, output_buf + output_buf_offset, (size_t)(output_buf_len - output_buf_offset));
528                                            if (ret < 0)
529                                            {
530                                                    if (errno == EAGAIN || errno == EWOULDBLOCK)
531                                                    {
532                                                            stdout_write_wait = 0;
533                                                            break;
534                                                    }
535                                                    else if (errno == EINTR)
536                                                    {
537                                                            continue;
538                                                    }
539                                                    else
540                                                    {
541                                                            log_error("write(STDOUT) error (%d)\n", errno);
542                                                            loop = 0;
543                                                            break;
544                                                    }
545                                            }
546                                            else if (ret == 0) // broken pipe
547                                            {
548                                                    log_std("write(STDOUT) EOF\n");
549                                                    stdout_write_wait = 0;
550                                                    loop = 0;
551                                                    break;
552                                            }
553                                            else
554                                            {
555                                                    output_buf_offset += ret;
556                                                    if (output_buf_offset >= output_buf_len) // Output buffer complete
557                                                    {
558                                                            output_buf_offset = 0;
559                                                            output_buf_len = 0;
560                                                            break;
561                                                    }
562                                                    continue;
563                                            }
564                                  }                                  }
                                 write(1, buf, len);  
565                          }                          }
                         BBS_last_access_tm = time(0);  
566                  }                  }
567          }          }
568    
569    cleanup:
570            if (close(epollfd) < 0)
571            {
572                    log_error("close(epoll) error (%d)\n");
573            }
574    
575            // Restore STDIN/STDOUT flags
576            fcntl(STDIN_FILENO, F_SETFL, flags_stdin);
577            fcntl(STDOUT_FILENO, F_SETFL, flags_stdout);
578    
579            // Restore socket flags
580            fcntl(sock, F_SETFL, flags_sock);
581    
582          if (close(sock) == -1)          if (close(sock) == -1)
583          {          {
584                  log_error("Close socket failed\n");                  log_error("Close socket failed\n");
# Line 318  bbsnet_refresh() Line 599  bbsnet_refresh()
599  {  {
600          clearscr();          clearscr();
601          moveto(1, 0);          moveto(1, 0);
602          prints("╭═════════════════════════════════════════════════════════════════════════════╮");          prints(" ----------------------------------------------------------------------------- ");
603          for (int i = 2; i < 19; i++)          for (int i = 2; i < 19; i++)
604          {          {
605                  moveto(i, 0);                  moveto(i, 0);
606                  prints("║");                  prints("|");
607                  moveto(i, 79);                  moveto(i, 79);
608                  prints("║");                  prints("|");
609          }          }
610          moveto(19, 0);          moveto(19, 0);
611          prints("║—————————————————————————————————————————————————————————————————————————————║");          prints("|-----------------------------------------------------------------------------|");
612          moveto(22, 0);          moveto(22, 0);
613          prints("╰═════════════════════════════════════════════════════════════════════════════╯");          prints(" ----------------------------------------------------------------------------- ");
614          moveto(23, 0);          moveto(23, 0);
615          prints(" [\x1b[1;32mCtrl+C\x1b[m]退出");          prints(" [\x1b[1;32mCtrl+C\x1b[m]退出");
616    
# Line 342  int bbsnet_selchange(int new_pos) Line 623  int bbsnet_selchange(int new_pos)
623  {  {
624          moveto(20, 0);          moveto(20, 0);
625          clrtoeol();          clrtoeol();
626          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",
627                     bbsnet_conf[new_pos].host2, bbsnet_conf[new_pos].host1);                     bbsnet_conf[new_pos].host2, bbsnet_conf[new_pos].host1);
628          moveto(20, 79);          moveto(20, 79);
629          prints("║");          prints("|");
630          moveto(21, 0);          moveto(21, 0);
631          clrtoeol();          clrtoeol();
632          prints("║\x1b[1m连往:\x1b[1;33m%-20s", bbsnet_conf[new_pos].ip);          prints("|\x1b[1m连往:\x1b[1;33m%-20s", bbsnet_conf[new_pos].ip);
633          if (bbsnet_conf[new_pos].port != 23)          if (bbsnet_conf[new_pos].port != 23)
634          {          {
635                  prints("  %d", bbsnet_conf[new_pos].port);                  prints("  %d", bbsnet_conf[new_pos].port);
636          }          }
637          prints("\x1b[m");          prints("\x1b[m");
638          moveto(21, 79);          moveto(21, 79);
639          prints("║");          prints("|");
640          iflush();          iflush();
641    
642          return 0;          return 0;
# Line 375  int bbs_net() Line 656  int bbs_net()
656          display_menu(get_menu(&bbsnet_menu, "BBSNET"));          display_menu(get_menu(&bbsnet_menu, "BBSNET"));
657          bbsnet_selchange(pos);          bbsnet_selchange(pos);
658    
659          while (1)          while (!SYS_server_exit)
660          {          {
661                  ch = igetch(0);                  ch = igetch(100);
662                  switch (ch)                  switch (ch)
663                  {                  {
664                  case KEY_NULL:                  case KEY_NULL:  // broken pipe
665                  case Ctrl('C'):                  case Ctrl('C'): // user cancel
666                          return 0;                          return 0;
667                  case KEY_TIMEOUT:                  case KEY_TIMEOUT:
668                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)
669                          {                          {
670                                  return -1;                                  return 0;
671                          }                          }
672                          continue;                          continue;
673                  case CR:                  case CR:


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

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