/[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.58 by sysadm, Wed Oct 1 02:20:50 2025 UTC Revision 1.59 by sysadm, Sat Oct 4 10:11:44 2025 UTC
# Line 287  int bbsnet_connect(int n) Line 287  int bbsnet_connect(int n)
287                  return -1;                  return -1;
288          }          }
289    
290          ev.events = EPOLLOUT;          ev.events = EPOLLOUT | EPOLLET;
291          ev.data.fd = sock;          ev.data.fd = sock;
292          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sock, &ev) == -1)          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sock, &ev) == -1)
293          {          {
# Line 295  int bbsnet_connect(int n) Line 295  int bbsnet_connect(int n)
295                  goto cleanup;                  goto cleanup;
296          }          }
297    
298          ev.events = EPOLLIN;          ev.events = EPOLLIN | EPOLLET;
299          ev.data.fd = STDIN_FILENO;          ev.data.fd = STDIN_FILENO;
300          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1)          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1)
301          {          {
# Line 404  int bbsnet_connect(int n) Line 404  int bbsnet_connect(int n)
404                  goto cleanup;                  goto cleanup;
405          }          }
406    
407          ev.events = EPOLLOUT;          ev.events = EPOLLOUT | EPOLLET;
408          ev.data.fd = STDOUT_FILENO;          ev.data.fd = STDOUT_FILENO;
409          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1)          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1)
410          {          {
# Line 441  int bbsnet_connect(int n) Line 441  int bbsnet_connect(int n)
441                          {                          {
442                                  break;                                  break;
443                          }                          }
                         continue;  
444                  }                  }
445    
446                  for (int i = 0; i < nfds; i++)                  for (int i = 0; i < nfds; i++)
447                  {                  {
448                          if (events[i].data.fd == STDIN_FILENO || stdin_read_wait)                          if (events[i].data.fd == STDIN_FILENO)
449                          {                          {
450                                  stdin_read_wait = 1;                                  stdin_read_wait = 1;
451                                  while (input_buf_len < sizeof(input_buf) && !SYS_server_exit)                          }
452    
453                            if (events[i].data.fd == sock)
454                            {
455                                    if (events[i].events & EPOLLIN)
456                                    {
457                                            sock_read_wait = 1;
458                                    }
459                                    if (events[i].events & EPOLLOUT)
460                                    {
461                                            sock_write_wait = 1;
462                                    }
463                            }
464    
465                            if (events[i].data.fd == STDOUT_FILENO)
466                            {
467                                    stdout_write_wait = 1;
468                            }
469                    }
470    
471                    if (stdin_read_wait)
472                    {
473                            while (input_buf_len < sizeof(input_buf) && !SYS_server_exit)
474                            {
475                                    if (SSH_v2)
476                                  {                                  {
477                                          if (SSH_v2)                                          ret = ssh_channel_read_nonblocking(SSH_channel, input_buf + input_buf_len, sizeof(input_buf) - (uint32_t)input_buf_len, 0);
478                                            if (ret == SSH_ERROR)
479                                          {                                          {
480                                                  ret = ssh_channel_read_nonblocking(SSH_channel, input_buf + input_buf_len, sizeof(input_buf) - (uint32_t)input_buf_len, 0);                                                  log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session));
481                                                  if (ret == SSH_ERROR)                                                  loop = 0;
482                                                  {                                                  break;
                                                         log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session));  
                                                         loop = 0;  
                                                         break;  
                                                 }  
                                                 else if (ret == SSH_EOF)  
                                                 {  
                                                         stdin_read_wait = 0;  
                                                         loop = 0;  
                                                         break;  
                                                 }  
                                                 else if (ret == 0)  
                                                 {  
                                                         stdin_read_wait = 0;  
                                                         break; // Check whether channel is still open  
                                                 }  
483                                          }                                          }
484                                          else                                          else if (ret == SSH_EOF)
485                                          {                                          {
486                                                  ret = (int)read(STDIN_FILENO, input_buf + input_buf_len, sizeof(input_buf) - (size_t)input_buf_len);                                                  stdin_read_wait = 0;
487                                                    loop = 0;
488                                                    break;
489                                          }                                          }
490                                          if (ret < 0)                                          else if (ret == 0)
491                                          {                                          {
492                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)                                                  stdin_read_wait = 0;
493                                                  {                                                  break; // Check whether channel is still open
                                                         stdin_read_wait = 0;  
                                                         break;  
                                                 }  
                                                 else if (errno == EINTR)  
                                                 {  
                                                         continue;  
                                                 }  
                                                 else  
                                                 {  
                                                         log_error("read(STDIN) error (%d)\n", errno);  
                                                         loop = 0;  
                                                         break;  
                                                 }  
494                                          }                                          }
495                                          else if (ret == 0) // broken pipe                                  }
496                                    else
497                                    {
498                                            ret = (int)read(STDIN_FILENO, input_buf + input_buf_len, sizeof(input_buf) - (size_t)input_buf_len);
499                                    }
500                                    if (ret < 0)
501                                    {
502                                            if (errno == EAGAIN || errno == EWOULDBLOCK)
503                                          {                                          {
 #ifdef _DEBUG  
                                                 log_error("read(STDIN) EOF\n");  
 #endif  
504                                                  stdin_read_wait = 0;                                                  stdin_read_wait = 0;
                                                 loop = 0;  
505                                                  break;                                                  break;
506                                          }                                          }
507                                            else if (errno == EINTR)
508                                            {
509                                                    continue;
510                                            }
511                                          else                                          else
512                                          {                                          {
513                                                  input_buf_len += ret;                                                  log_error("read(STDIN) error (%d)\n", errno);
514                                                  BBS_last_access_tm = time(NULL);                                                  loop = 0;
515                                                    break;
516                                                  // Refresh current action while user input                                          }
517                                                  if (user_online_update("BBS_NET") < 0)                                  }
518                                                  {                                  else if (ret == 0) // broken pipe
519                                                          log_error("user_online_update(BBS_NET) error\n");                                  {
520                                                  }  #ifdef _DEBUG
521                                            log_error("read(STDIN) EOF\n");
522    #endif
523                                            stdin_read_wait = 0;
524                                            loop = 0;
525                                            break;
526                                    }
527                                    else
528                                    {
529                                            input_buf_len += ret;
530                                            BBS_last_access_tm = time(NULL);
531    
532                                                  continue;                                          // Refresh current action while user input
533                                            if (user_online_update("BBS_NET") < 0)
534                                            {
535                                                    log_error("user_online_update(BBS_NET) error\n");
536                                          }                                          }
537    
538                                            continue;
539                                  }                                  }
540                          }                          }
541                    }
542    
543                          if (events[i].data.fd == sock || sock_write_wait) // EPOLLOUT                  if (sock_write_wait)
544                    {
545                            while (input_buf_offset < input_buf_len && !SYS_server_exit)
546                          {                          {
547                                  sock_write_wait = 1;                                  ret = (int)write(sock, input_buf + input_buf_offset, (size_t)(input_buf_len - input_buf_offset));
548                                  while (input_buf_offset < input_buf_len && !SYS_server_exit)                                  if (ret < 0)
549                                  {                                  {
550                                          ret = (int)write(sock, input_buf + input_buf_offset, (size_t)(input_buf_len - input_buf_offset));                                          if (errno == EAGAIN || errno == EWOULDBLOCK)
                                         if (ret < 0)  
551                                          {                                          {
552                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)                                                  sock_write_wait = 0;
553                                                  {                                                  break;
                                                         sock_write_wait = 0;  
                                                         break;  
                                                 }  
                                                 else if (errno == EINTR)  
                                                 {  
                                                         continue;  
                                                 }  
                                                 else  
                                                 {  
                                                         log_error("write(socket) error (%d)\n", errno);  
                                                         loop = 0;  
                                                         break;  
                                                 }  
554                                          }                                          }
555                                          else if (ret == 0) // broken pipe                                          else if (errno == EINTR)
556                                          {                                          {
557  #ifdef _DEBUG                                                  continue;
558                                                  log_error("write(socket) EOF\n");                                          }
559  #endif                                          else
560                                                  sock_write_wait = 0;                                          {
561                                                    log_error("write(socket) error (%d)\n", errno);
562                                                  loop = 0;                                                  loop = 0;
563                                                  break;                                                  break;
564                                          }                                          }
565                                          else                                  }
566                                    else if (ret == 0) // broken pipe
567                                    {
568    #ifdef _DEBUG
569                                            log_error("write(socket) EOF\n");
570    #endif
571                                            sock_write_wait = 0;
572                                            loop = 0;
573                                            break;
574                                    }
575                                    else
576                                    {
577                                            input_buf_offset += ret;
578                                            if (input_buf_offset >= input_buf_len) // Output buffer complete
579                                          {                                          {
580                                                  input_buf_offset += ret;                                                  input_buf_offset = 0;
581                                                  if (input_buf_offset >= input_buf_len) // Output buffer complete                                                  input_buf_len = 0;
582                                                  {                                                  break;
                                                         input_buf_offset = 0;  
                                                         input_buf_len = 0;  
                                                         break;  
                                                 }  
                                                 continue;  
583                                          }                                          }
584                                            continue;
585                                  }                                  }
586                          }                          }
587                    }
588    
589                          if (events[i].data.fd == sock || sock_read_wait) // EPOLLIN                  if (sock_read_wait)
590                    {
591                            while (output_buf_len < sizeof(output_buf) && !SYS_server_exit)
592                          {                          {
593                                  sock_read_wait = 1;                                  ret = (int)read(sock, output_buf + output_buf_len, sizeof(output_buf) - (size_t)output_buf_len);
594                                  while (output_buf_len < sizeof(output_buf) && !SYS_server_exit)                                  if (ret < 0)
595                                  {                                  {
596                                          ret = (int)read(sock, output_buf + output_buf_len, sizeof(output_buf) - (size_t)output_buf_len);                                          if (errno == EAGAIN || errno == EWOULDBLOCK)
                                         if (ret < 0)  
                                         {  
                                                 if (errno == EAGAIN || errno == EWOULDBLOCK)  
                                                 {  
                                                         sock_read_wait = 0;  
                                                         break;  
                                                 }  
                                                 else if (errno == EINTR)  
                                                 {  
                                                         continue;  
                                                 }  
                                                 else  
                                                 {  
                                                         log_error("read(socket) error (%d)\n", errno);  
                                                         loop = 0;  
                                                         break;  
                                                 }  
                                         }  
                                         else if (ret == 0) // broken pipe  
597                                          {                                          {
 #ifdef _DEBUG  
                                                 log_error("read(socket) EOF\n");  
 #endif  
598                                                  sock_read_wait = 0;                                                  sock_read_wait = 0;
                                                 loop = 0;  
599                                                  break;                                                  break;
600                                          }                                          }
601                                          else                                          else if (errno == EINTR)
602                                          {                                          {
                                                 output_buf_len += ret;  
603                                                  continue;                                                  continue;
604                                          }                                          }
605                                            else
606                                            {
607                                                    log_error("read(socket) error (%d)\n", errno);
608                                                    loop = 0;
609                                                    break;
610                                            }
611                                    }
612                                    else if (ret == 0) // broken pipe
613                                    {
614    #ifdef _DEBUG
615                                            log_error("read(socket) EOF\n");
616    #endif
617                                            sock_read_wait = 0;
618                                            loop = 0;
619                                            break;
620                                    }
621                                    else
622                                    {
623                                            output_buf_len += ret;
624                                            continue;
625                                  }                                  }
626                          }                          }
627                    }
628    
629                          if (events[i].data.fd == STDOUT_FILENO || stdout_write_wait)                  if (stdout_write_wait)
630                    {
631                            while (output_buf_offset < output_buf_len && !SYS_server_exit)
632                          {                          {
633                                  stdout_write_wait = 1;                                  if (SSH_v2)
                                 while (output_buf_offset < output_buf_len && !SYS_server_exit)  
634                                  {                                  {
635                                          if (SSH_v2)                                          ret = ssh_channel_write(SSH_channel, output_buf + output_buf_offset, (uint32_t)(output_buf_len - output_buf_offset));
636                                            if (ret == SSH_ERROR)
637                                          {                                          {
638                                                  ret = ssh_channel_write(SSH_channel, output_buf + output_buf_offset, (uint32_t)(output_buf_len - output_buf_offset));                                                  log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session));
639                                                  if (ret == SSH_ERROR)                                                  loop = 0;
640                                                  {                                                  break;
                                                         log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session));  
                                                         loop = 0;  
                                                         break;  
                                                 }  
641                                          }                                          }
642                                          else                                  }
643                                    else
644                                    {
645                                            ret = (int)write(STDOUT_FILENO, output_buf + output_buf_offset, (size_t)(output_buf_len - output_buf_offset));
646                                    }
647                                    if (ret < 0)
648                                    {
649                                            if (errno == EAGAIN || errno == EWOULDBLOCK)
650                                          {                                          {
651                                                  ret = (int)write(STDOUT_FILENO, output_buf + output_buf_offset, (size_t)(output_buf_len - output_buf_offset));                                                  stdout_write_wait = 0;
652                                                    break;
653                                          }                                          }
654                                          if (ret < 0)                                          else if (errno == EINTR)
655                                          {                                          {
656                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)                                                  continue;
                                                 {  
                                                         stdout_write_wait = 0;  
                                                         break;  
                                                 }  
                                                 else if (errno == EINTR)  
                                                 {  
                                                         continue;  
                                                 }  
                                                 else  
                                                 {  
                                                         log_error("write(STDOUT) error (%d)\n", errno);  
                                                         loop = 0;  
                                                         break;  
                                                 }  
657                                          }                                          }
658                                          else if (ret == 0) // broken pipe                                          else
659                                          {                                          {
660  #ifdef _DEBUG                                                  log_error("write(STDOUT) error (%d)\n", errno);
                                                 log_error("write(STDOUT) EOF\n");  
 #endif  
                                                 stdout_write_wait = 0;  
661                                                  loop = 0;                                                  loop = 0;
662                                                  break;                                                  break;
663                                          }                                          }
664                                          else                                  }
665                                    else if (ret == 0) // broken pipe
666                                    {
667    #ifdef _DEBUG
668                                            log_error("write(STDOUT) EOF\n");
669    #endif
670                                            stdout_write_wait = 0;
671                                            loop = 0;
672                                            break;
673                                    }
674                                    else
675                                    {
676                                            output_buf_offset += ret;
677                                            if (output_buf_offset >= output_buf_len) // Output buffer complete
678                                          {                                          {
679                                                  output_buf_offset += ret;                                                  output_buf_offset = 0;
680                                                  if (output_buf_offset >= output_buf_len) // Output buffer complete                                                  output_buf_len = 0;
681                                                  {                                                  break;
                                                         output_buf_offset = 0;  
                                                         output_buf_len = 0;  
                                                         break;  
                                                 }  
                                                 continue;  
682                                          }                                          }
683                                            continue;
684                                  }                                  }
685                          }                          }
686                  }                  }


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

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