/[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.36 by sysadm, Sun May 11 12:47:32 2025 UTC Revision 1.39 by sysadm, Tue May 13 07:31:48 2025 UTC
# Line 32  Line 32 
32  #include <sys/select.h>  #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>  #include <netinet/ip.h>
38  #include <arpa/inet.h>  #include <arpa/inet.h>
# Line 151  static void process_bar(int n, int len) Line 152  static void process_bar(int n, int len)
152  int bbsnet_connect(int n)  int bbsnet_connect(int n)
153  {  {
154          int sock, ret, loop, error;          int sock, ret, loop, error;
155            int sock_connected = 0;
156          int flags_sock;          int flags_sock;
157          int flags_stdin;          int flags_stdin;
158          int flags_stdout;          int flags_stdout;
# Line 162  int bbsnet_connect(int n) Line 164  int bbsnet_connect(int n)
164          int output_buf_len = 0;          int output_buf_len = 0;
165          int input_buf_offset = 0;          int input_buf_offset = 0;
166          int output_buf_offset = 0;          int output_buf_offset = 0;
167          fd_set read_fds;          struct epoll_event ev, events[MAX_EVENTS];
168          fd_set write_fds;          int nfds, epollfd;
169          struct timeval timeout;          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;          struct hostent *p_host = NULL;
174          int tos;          int tos;
         int i;  
175          char remote_addr[IP_ADDR_LEN];          char remote_addr[IP_ADDR_LEN];
176          int remote_port;          int remote_port;
177          time_t t_used;          time_t t_used;
# Line 226  int bbsnet_connect(int n) Line 230  int bbsnet_connect(int n)
230          flags_sock = fcntl(sock, F_GETFL, 0);          flags_sock = fcntl(sock, F_GETFL, 0);
231          fcntl(sock, F_SETFL, flags_sock | O_NONBLOCK);          fcntl(sock, F_SETFL, flags_sock | O_NONBLOCK);
232    
233          if ((ret = connect(sock, (struct sockaddr *)&sin, sizeof(sin))) < 0)          // 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                  if (errno != EINPROGRESS)                  log_error("epoll_create1() error (%d)\n", errno);
243                  {                  return -1;
                         prints("\033[1;31m连接失败!\033[m\r\n");  
                         press_any_key();  
                         return -1;  
                 }  
244          }          }
245    
246          for (i = 0; i < MAX_PROCESS_BAR_LEN; i++)          ev.events = EPOLLOUT;
247            ev.data.fd = sock;
248            if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sock, &ev) == -1)
249          {          {
250                  ch = igetch(0); // 0.1 second                  log_error("epoll_ctl(socket) error (%d)\n", errno);
251                  if (ch == Ctrl('C') || SYS_server_exit)                  goto cleanup;
252                  {          }
                         return 0;  
                 }  
   
                 FD_ZERO(&read_fds);  
                 FD_SET(sock, &read_fds);  
253    
254                  FD_ZERO(&write_fds);          ev.events = EPOLLIN;
255                  FD_SET(sock, &write_fds);          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                  timeout.tv_sec = 0;          while (!SYS_server_exit)
263                  timeout.tv_usec = 400 * 1000; // 0.4 second          {
264                    if ((ret = connect(sock, (struct sockaddr *)&sin, sizeof(sin))) < 0)
265                    {
266                            if (errno == EAGAIN || errno == EALREADY || errno == EINPROGRESS)
267                            {
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                  ret = select(sock + 1, &read_fds, &write_fds, NULL, &timeout);                                  prints("\033[1;31m连接失败!\033[m\r\n");
282                                    press_any_key();
283    
284                  if (ret == 0) // Timeout                                  goto cleanup;
285                  {                          }
                         process_bar(i + 1, MAX_PROCESS_BAR_LEN);  
286                  }                  }
287                  else if (ret < 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                          if (errno != EINTR)                          if (errno != EINTR)
296                          {                          {
297                                  log_error("select() error (%d) !\n", errno);                                  log_error("epoll_wait() error (%d)\n", errno);
298                                  return -1;                                  break;
299                          }                          }
300                  }                  }
301                  // ret > 0                  else if (nfds == 0) // timeout
                 else if (FD_ISSET(sock, &read_fds) || FD_ISSET(sock, &write_fds))  
302                  {                  {
303                          len = sizeof(error);                          process_bar(j + 1, MAX_PROCESS_BAR_LEN);
304                          if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, (socklen_t *)&len) < 0)                  }
305                    else // ret > 0
306                    {
307                            for (int i = 0; i < nfds; i++)
308                          {                          {
309                                  log_error("getsockopt() error (%d) !\n", error);                                  if (events[i].data.fd == sock)
310                                  return -1;                                  {
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                          }                          }
   
                         break; // connected  
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;          tos = IPTOS_LOWDELAY;
# Line 297  int bbsnet_connect(int n) Line 352  int bbsnet_connect(int n)
352          iflush();          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          // Set STDIN/STDOUT as non-blocking          ev.events = EPOLLIN | EPOLLOUT | EPOLLET;
356          flags_stdin = fcntl(STDIN_FILENO, F_GETFL, 0);          ev.data.fd = sock;
357          flags_stdout = fcntl(STDOUT_FILENO, F_GETFL, 0);          if (epoll_ctl(epollfd, EPOLL_CTL_MOD, sock, &ev) == -1)
358          fcntl(STDIN_FILENO, F_SETFL, flags_stdin | O_NONBLOCK);          {
359          fcntl(STDOUT_FILENO, F_SETFL, flags_stdout | O_NONBLOCK);                  log_error("epoll_ctl(socket) error (%d)\n", errno);
360                    goto cleanup;
361            }
362    
363            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);          BBS_last_access_tm = t_used = time(0);
372          loop = 1;          loop = 1;
373    
374          while (loop && !SYS_server_exit)          while (loop && !SYS_server_exit)
375          {          {
376                  FD_ZERO(&read_fds);                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second
                 FD_SET(STDIN_FILENO, &read_fds);  
                 FD_SET(sock, &read_fds);  
   
                 FD_ZERO(&write_fds);  
                 FD_SET(STDOUT_FILENO, &write_fds);  
                 FD_SET(sock, &write_fds);  
   
                 timeout.tv_sec = 0;  
                 timeout.tv_usec = 100 * 1000; // 0.1 second  
377    
378                  ret = select(sock + 1, &read_fds, &write_fds, NULL, &timeout);                  if (nfds < 0)
   
                 if (ret == 0) // timeout  
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                  else if (ret < 0)                  else if (nfds == 0) // timeout
388                  {                  {
389                          if (errno != EINTR)                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)
390                          {                          {
391                                  log_error("select() error (%d) !\n", errno);                                  break;
                                 loop = 0;  
392                          }                          }
393                            continue;
394                  }                  }
395                  else if (ret > 0)  
396                    for (int i = 0; i < nfds; i++)
397                  {                  {
398                          if ((input_buf_offset >= input_buf_len) && FD_ISSET(STDIN_FILENO, &read_fds))                          if (events[i].data.fd == STDIN_FILENO || stdin_read_wait)
399                          {                          {
400                                  ret = (int)read(STDIN_FILENO, input_buf, sizeof(input_buf));                                  stdin_read_wait = 1;
401                                  if (ret < 0)                                  while (input_buf_len < sizeof(input_buf) && !SYS_server_exit)
402                                  {                                  {
403                                          if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)                                          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_error("read(STDIN) error (%d)\n", errno);                                                  log_std("read(STDIN) EOF\n");
425                                                    stdin_read_wait = 0;
426                                                  loop = 0;                                                  loop = 0;
427                                                    break;
428                                            }
429                                            else
430                                            {
431                                                    input_buf_len += ret;
432                                                    BBS_last_access_tm = time(0);
433                                                    continue;
434                                          }                                          }
                                 }  
                                 else if (ret == 0) // broken pipe  
                                 {  
                                         loop = 0;  
                                 }  
                                 else  
                                 {  
                                         input_buf_len = ret;  
                                         input_buf_offset = 0;  
   
                                         BBS_last_access_tm = time(0);  
435                                  }                                  }
436                          }                          }
437    
438                          if ((input_buf_offset < input_buf_len) && FD_ISSET(sock, &write_fds))                          if (events[i].data.fd == sock || sock_write_wait) // EPOLLOUT
439                          {                          {
440                                  ret = (int)write(sock, input_buf + input_buf_offset, (size_t)(input_buf_len - input_buf_offset));                                  sock_write_wait = 1;
441                                  if (ret < 0)                                  while (input_buf_offset < input_buf_len && !SYS_server_exit)
442                                  {                                  {
443                                          if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)                                          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_error("write(socket) error (%d)\n", errno);                                                  log_std("write(socket) EOF\n");
465                                                    sock_write_wait = 0;
466                                                  loop = 0;                                                  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                                          }                                          }
                                 }  
                                 else if (ret == 0) // broken pipe  
                                 {  
                                         loop = 0;  
                                 }  
                                 else  
                                 {  
                                         input_buf_offset += ret;  
480                                  }                                  }
481                          }                          }
482    
483                          if ((output_buf_offset >= output_buf_len) && FD_ISSET(sock, &read_fds))                          if (events[i].data.fd == sock || sock_read_wait) // EPOLLIN
484                          {                          {
485                                  ret = (int)read(sock, output_buf, sizeof(output_buf));                                  sock_read_wait = 1;
486                                  if (ret < 0)                                  while (output_buf_len < sizeof(output_buf) && !SYS_server_exit)
487                                  {                                  {
488                                          if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)                                          ret = (int)read(sock, output_buf + output_buf_len, sizeof(output_buf) - (size_t)output_buf_len);
489                                            if (ret < 0)
490                                          {                                          {
491                                                  log_error("read(socket) error (%d)\n", errno);                                                  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;                                                  loop = 0;
512                                                    break;
513                                            }
514                                            else
515                                            {
516                                                    output_buf_len += ret;
517                                                    continue;
518                                          }                                          }
                                 }  
                                 else if (ret == 0) // broken pipe  
                                 {  
                                         loop = 0;  
                                 }  
                                 else  
                                 {  
                                         output_buf_len = ret;  
                                         output_buf_offset = 0;  
519                                  }                                  }
520                          }                          }
521    
522                          if ((output_buf_offset < output_buf_len) && FD_ISSET(STDOUT_FILENO, &write_fds))                          if (events[i].data.fd == STDOUT_FILENO || stdout_write_wait)
523                          {                          {
524                                  ret = (int)write(STDOUT_FILENO, output_buf + output_buf_offset, (size_t)(output_buf_len - output_buf_offset));                                  stdout_write_wait = 1;
525                                  if (ret < 0)                                  while (output_buf_offset < output_buf_len && !SYS_server_exit)
526                                  {                                  {
527                                          if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)                                          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_error("write(STDOUT) error (%d)\n", errno);                                                  log_std("write(STDOUT) EOF\n");
549                                                    stdout_write_wait = 0;
550                                                  loop = 0;                                                  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                                          }                                          }
                                 }  
                                 else if (ret == 0) // broken pipe  
                                 {  
                                         loop = 0;  
                                 }  
                                 else  
                                 {  
                                         output_buf_offset += ret;  
564                                  }                                  }
565                          }                          }
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          // Restore STDIN/STDOUT flags
576          fcntl(STDIN_FILENO, F_SETFL, flags_stdin);          fcntl(STDIN_FILENO, F_SETFL, flags_stdin);
577          fcntl(STDOUT_FILENO, F_SETFL, flags_stdout);          fcntl(STDOUT_FILENO, F_SETFL, flags_stdout);
# Line 514  int bbs_net() Line 658  int bbs_net()
658    
659          while (!SYS_server_exit)          while (!SYS_server_exit)
660          {          {
661                  ch = igetch(0);                  ch = igetch(100);
662                  switch (ch)                  switch (ch)
663                  {                  {
664                  case KEY_NULL:  // broken pipe                  case KEY_NULL:  // broken pipe


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

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