/[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.40 by sysadm, Wed May 14 04:22:45 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 66  int load_bbsnet_conf(const char *file_co Line 67  int load_bbsnet_conf(const char *file_co
67                  return -1;                  return -1;
68          }          }
69    
70          p_menu = bbsnet_menu.p_menu[0] = malloc(sizeof(MENU));          bbsnet_menu.p_menu_name_dict = trie_dict_create();
71    
72            p_menu = malloc(sizeof(MENU));
73            bbsnet_menu.p_menu[0] = p_menu;
74          strncpy(p_menu->name, "BBSNET", sizeof(p_menu->name) - 1);          strncpy(p_menu->name, "BBSNET", sizeof(p_menu->name) - 1);
75          p_menu->name[sizeof(p_menu->name) - 1] = '\0';          p_menu->name[sizeof(p_menu->name) - 1] = '\0';
76          p_menu->title.show = 0;          p_menu->title.show = 0;
77          p_menu->screen.show = 0;          p_menu->screen.show = 0;
78    
79            if (trie_dict_set(bbsnet_menu.p_menu_name_dict, p_menu->name, (int64_t)p_menu) != 1)
80            {
81                    log_error("Error set BBSNET menu dict [%s]\n", p_menu->name);
82            }
83    
84          while (fgets(t, 255, fp) && item_count < MAXSTATION)          while (fgets(t, 255, fp) && item_count < MAXSTATION)
85          {          {
86                  t1 = strtok_r(t, MENU_CONF_DELIM, &saveptr);                  t1 = strtok_r(t, MENU_CONF_DELIM, &saveptr);
# Line 120  int load_bbsnet_conf(const char *file_co Line 129  int load_bbsnet_conf(const char *file_co
129          return 0;          return 0;
130  }  }
131    
132  static void process_bar(int n, int len)  void unload_bbsnet_conf(void)
133    {
134            unload_menu(&bbsnet_menu);
135    }
136    
137    void process_bar(int n, int len)
138  {  {
139          char buf[LINE_BUFFER_LEN];          char buf[LINE_BUFFER_LEN];
140          char buf2[LINE_BUFFER_LEN];          char buf2[LINE_BUFFER_LEN];
# Line 151  static void process_bar(int n, int len) Line 165  static void process_bar(int n, int len)
165  int bbsnet_connect(int n)  int bbsnet_connect(int n)
166  {  {
167          int sock, ret, loop, error;          int sock, ret, loop, error;
168            int sock_connected = 0;
169          int flags_sock;          int flags_sock;
170          int flags_stdin;          int flags_stdin;
171          int flags_stdout;          int flags_stdout;
# Line 162  int bbsnet_connect(int n) Line 177  int bbsnet_connect(int n)
177          int output_buf_len = 0;          int output_buf_len = 0;
178          int input_buf_offset = 0;          int input_buf_offset = 0;
179          int output_buf_offset = 0;          int output_buf_offset = 0;
180          fd_set read_fds;          struct epoll_event ev, events[MAX_EVENTS];
181          fd_set write_fds;          int nfds, epollfd;
182          struct timeval timeout;          int stdin_read_wait = 0;
183            int stdout_write_wait = 0;
184            int sock_read_wait = 0;
185            int sock_write_wait = 0;
186          struct hostent *p_host = NULL;          struct hostent *p_host = NULL;
187          int tos;          int tos;
         int i;  
188          char remote_addr[IP_ADDR_LEN];          char remote_addr[IP_ADDR_LEN];
189          int remote_port;          int remote_port;
190          time_t t_used;          time_t t_used;
# Line 226  int bbsnet_connect(int n) Line 243  int bbsnet_connect(int n)
243          flags_sock = fcntl(sock, F_GETFL, 0);          flags_sock = fcntl(sock, F_GETFL, 0);
244          fcntl(sock, F_SETFL, flags_sock | O_NONBLOCK);          fcntl(sock, F_SETFL, flags_sock | O_NONBLOCK);
245    
246          if ((ret = connect(sock, (struct sockaddr *)&sin, sizeof(sin))) < 0)          // Set STDIN/STDOUT as non-blocking
247            flags_stdin = fcntl(STDIN_FILENO, F_GETFL, 0);
248            flags_stdout = fcntl(STDOUT_FILENO, F_GETFL, 0);
249            fcntl(STDIN_FILENO, F_SETFL, flags_stdin | O_NONBLOCK);
250            fcntl(STDOUT_FILENO, F_SETFL, flags_stdout | O_NONBLOCK);
251    
252            epollfd = epoll_create1(0);
253            if (epollfd < 0)
254          {          {
255                  if (errno != EINPROGRESS)                  log_error("epoll_create1() error (%d)\n", errno);
256                  {                  return -1;
                         prints("\033[1;31m连接失败!\033[m\r\n");  
                         press_any_key();  
                         return -1;  
                 }  
257          }          }
258    
259          for (i = 0; i < MAX_PROCESS_BAR_LEN; i++)          ev.events = EPOLLOUT;
260            ev.data.fd = sock;
261            if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sock, &ev) == -1)
262          {          {
263                  ch = igetch(0); // 0.1 second                  log_error("epoll_ctl(socket) error (%d)\n", errno);
264                  if (ch == Ctrl('C') || SYS_server_exit)                  goto cleanup;
265                  {          }
                         return 0;  
                 }  
   
                 FD_ZERO(&read_fds);  
                 FD_SET(sock, &read_fds);  
266    
267                  FD_ZERO(&write_fds);          ev.events = EPOLLIN;
268                  FD_SET(sock, &write_fds);          ev.data.fd = STDIN_FILENO;
269            if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1)
270            {
271                    log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno);
272                    goto cleanup;
273            }
274    
275                  timeout.tv_sec = 0;          while (!SYS_server_exit)
276                  timeout.tv_usec = 400 * 1000; // 0.4 second          {
277                    if ((ret = connect(sock, (struct sockaddr *)&sin, sizeof(sin))) < 0)
278                    {
279                            if (errno == EAGAIN || errno == EALREADY || errno == EINPROGRESS)
280                            {
281                                    // Use select / epoll to check writability of the socket,
282                                    // then use getsockopt to check the status of the socket.
283                                    // See man connect(2)
284                                    break;
285                            }
286                            else if (errno == EINTR)
287                            {
288                                    continue;
289                            }
290                            else
291                            {
292                                    log_error("connect(socket) error (%d)\n", errno);
293    
294                  ret = select(sock + 1, &read_fds, &write_fds, NULL, &timeout);                                  prints("\033[1;31m连接失败!\033[m\r\n");
295                                    press_any_key();
296    
297                  if (ret == 0) // Timeout                                  goto cleanup;
298                  {                          }
                         process_bar(i + 1, MAX_PROCESS_BAR_LEN);  
299                  }                  }
300                  else if (ret < 0)          }
301    
302            for (int j = 0; j < MAX_PROCESS_BAR_LEN && !sock_connected && !SYS_server_exit; j++)
303            {
304                    nfds = epoll_wait(epollfd, events, MAX_EVENTS, 500); // 0.5 second
305    
306                    if (nfds < 0)
307                  {                  {
308                          if (errno != EINTR)                          if (errno != EINTR)
309                          {                          {
310                                  log_error("select() error (%d) !\n", errno);                                  log_error("epoll_wait() error (%d)\n", errno);
311                                  return -1;                                  break;
312                          }                          }
313                  }                  }
314                  // ret > 0                  else if (nfds == 0) // timeout
315                  else if (FD_ISSET(sock, &read_fds) || FD_ISSET(sock, &write_fds))                  {
316                            process_bar(j + 1, MAX_PROCESS_BAR_LEN);
317                    }
318                    else // ret > 0
319                  {                  {
320                          len = sizeof(error);                          for (int i = 0; i < nfds; i++)
                         if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, (socklen_t *)&len) < 0)  
321                          {                          {
322                                  log_error("getsockopt() error (%d) !\n", error);                                  if (events[i].data.fd == sock)
323                                  return -1;                                  {
324                                            len = sizeof(error);
325                                            if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, (socklen_t *)&len) < 0)
326                                            {
327                                                    log_error("getsockopt() error (%d) !\n", error);
328                                                    goto cleanup;
329                                            }
330                                            if (error == 0)
331                                            {
332                                                    sock_connected = 1;
333                                            }
334                                    }
335                                    else if (events[i].data.fd == STDIN_FILENO)
336                                    {
337                                            ch = igetch(0);
338                                            if (ch == Ctrl('C'))
339                                            {
340                                                    goto cleanup;
341                                            }
342                                    }
343                          }                          }
   
                         break; // connected  
344                  }                  }
345          }          }
346          if (i == MAX_PROCESS_BAR_LEN)          if (SYS_server_exit)
347          {          {
348                  prints("\033[1;31m连接超时!\033[m\r\n");                  goto cleanup;
349            }
350            if (!sock_connected)
351            {
352                    prints("\033[1;31m连接失败!\033[m\r\n");
353                  press_any_key();                  press_any_key();
354                  return -1;  
355                    goto cleanup;
356          }          }
357    
358          tos = IPTOS_LOWDELAY;          tos = IPTOS_LOWDELAY;
# Line 297  int bbsnet_connect(int n) Line 365  int bbsnet_connect(int n)
365          iflush();          iflush();
366          log_std("BBSNET connect to %s:%d\n", remote_addr, remote_port);          log_std("BBSNET connect to %s:%d\n", remote_addr, remote_port);
367    
368          // Set STDIN/STDOUT as non-blocking          ev.events = EPOLLIN | EPOLLOUT | EPOLLET;
369          flags_stdin = fcntl(STDIN_FILENO, F_GETFL, 0);          ev.data.fd = sock;
370          flags_stdout = fcntl(STDOUT_FILENO, F_GETFL, 0);          if (epoll_ctl(epollfd, EPOLL_CTL_MOD, sock, &ev) == -1)
371          fcntl(STDIN_FILENO, F_SETFL, flags_stdin | O_NONBLOCK);          {
372          fcntl(STDOUT_FILENO, F_SETFL, flags_stdout | O_NONBLOCK);                  log_error("epoll_ctl(socket) error (%d)\n", errno);
373                    goto cleanup;
374            }
375    
376            ev.events = EPOLLOUT;
377            ev.data.fd = STDOUT_FILENO;
378            if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1)
379            {
380                    log_error("epoll_ctl(STDOUT_FILENO) error (%d)\n", errno);
381                    goto cleanup;
382            }
383    
384          BBS_last_access_tm = t_used = time(0);          BBS_last_access_tm = t_used = time(0);
385          loop = 1;          loop = 1;
386    
387          while (loop && !SYS_server_exit)          while (loop && !SYS_server_exit)
388          {          {
389                  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  
   
                 ret = select(sock + 1, &read_fds, &write_fds, NULL, &timeout);  
390    
391                  if (ret == 0) // timeout                  if (nfds < 0)
392                  {                  {
393                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (errno != EINTR)
394                          {                          {
395                                  loop = 0;                                  log_error("epoll_wait() error (%d)\n", errno);
396                                    break;
397                          }                          }
398                            continue;
399                  }                  }
400                  else if (ret < 0)                  else if (nfds == 0) // timeout
401                  {                  {
402                          if (errno != EINTR)                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)
403                          {                          {
404                                  log_error("select() error (%d) !\n", errno);                                  break;
                                 loop = 0;  
405                          }                          }
406                            continue;
407                  }                  }
408                  else if (ret > 0)  
409                    for (int i = 0; i < nfds; i++)
410                  {                  {
411                          if ((input_buf_offset >= input_buf_len) && FD_ISSET(STDIN_FILENO, &read_fds))                          if (events[i].data.fd == STDIN_FILENO || stdin_read_wait)
412                          {                          {
413                                  ret = (int)read(STDIN_FILENO, input_buf, sizeof(input_buf));                                  stdin_read_wait = 1;
414                                  if (ret < 0)                                  while (input_buf_len < sizeof(input_buf) && !SYS_server_exit)
415                                  {                                  {
416                                          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);
417                                            if (ret < 0)
418                                          {                                          {
419                                                  log_error("read(STDIN) error (%d)\n", errno);                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)
420                                                    {
421                                                            stdin_read_wait = 0;
422                                                            break;
423                                                    }
424                                                    else if (errno == EINTR)
425                                                    {
426                                                            continue;
427                                                    }
428                                                    else
429                                                    {
430                                                            log_error("read(STDIN) error (%d)\n", errno);
431                                                            loop = 0;
432                                                            break;
433                                                    }
434                                            }
435                                            else if (ret == 0) // broken pipe
436                                            {
437                                                    log_std("read(STDIN) EOF\n");
438                                                    stdin_read_wait = 0;
439                                                  loop = 0;                                                  loop = 0;
440                                                    break;
441                                            }
442                                            else
443                                            {
444                                                    input_buf_len += ret;
445                                                    BBS_last_access_tm = time(0);
446                                                    continue;
447                                          }                                          }
                                 }  
                                 else if (ret == 0) // broken pipe  
                                 {  
                                         loop = 0;  
                                 }  
                                 else  
                                 {  
                                         input_buf_len = ret;  
                                         input_buf_offset = 0;  
   
                                         BBS_last_access_tm = time(0);  
448                                  }                                  }
449                          }                          }
450    
451                          if ((input_buf_offset < input_buf_len) && FD_ISSET(sock, &write_fds))                          if (events[i].data.fd == sock || sock_write_wait) // EPOLLOUT
452                          {                          {
453                                  ret = (int)write(sock, input_buf + input_buf_offset, (size_t)(input_buf_len - input_buf_offset));                                  sock_write_wait = 1;
454                                  if (ret < 0)                                  while (input_buf_offset < input_buf_len && !SYS_server_exit)
455                                  {                                  {
456                                          if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)                                          ret = (int)write(sock, input_buf + input_buf_offset, (size_t)(input_buf_len - input_buf_offset));
457                                            if (ret < 0)
458                                          {                                          {
459                                                  log_error("write(socket) error (%d)\n", errno);                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)
460                                                    {
461                                                            sock_write_wait = 0;
462                                                            break;
463                                                    }
464                                                    else if (errno == EINTR)
465                                                    {
466                                                            continue;
467                                                    }
468                                                    else
469                                                    {
470                                                            log_error("write(socket) error (%d)\n", errno);
471                                                            loop = 0;
472                                                            break;
473                                                    }
474                                            }
475                                            else if (ret == 0) // broken pipe
476                                            {
477                                                    log_std("write(socket) EOF\n");
478                                                    sock_write_wait = 0;
479                                                  loop = 0;                                                  loop = 0;
480                                                    break;
481                                            }
482                                            else
483                                            {
484                                                    input_buf_offset += ret;
485                                                    if (input_buf_offset >= input_buf_len) // Output buffer complete
486                                                    {
487                                                            input_buf_offset = 0;
488                                                            input_buf_len = 0;
489                                                            break;
490                                                    }
491                                                    continue;
492                                          }                                          }
                                 }  
                                 else if (ret == 0) // broken pipe  
                                 {  
                                         loop = 0;  
                                 }  
                                 else  
                                 {  
                                         input_buf_offset += ret;  
493                                  }                                  }
494                          }                          }
495    
496                          if ((output_buf_offset >= output_buf_len) && FD_ISSET(sock, &read_fds))                          if (events[i].data.fd == sock || sock_read_wait) // EPOLLIN
497                          {                          {
498                                  ret = (int)read(sock, output_buf, sizeof(output_buf));                                  sock_read_wait = 1;
499                                  if (ret < 0)                                  while (output_buf_len < sizeof(output_buf) && !SYS_server_exit)
500                                  {                                  {
501                                          if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)                                          ret = (int)read(sock, output_buf + output_buf_len, sizeof(output_buf) - (size_t)output_buf_len);
502                                            if (ret < 0)
503                                          {                                          {
504                                                  log_error("read(socket) error (%d)\n", errno);                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)
505                                                    {
506                                                            sock_read_wait = 0;
507                                                            break;
508                                                    }
509                                                    else if (errno == EINTR)
510                                                    {
511                                                            continue;
512                                                    }
513                                                    else
514                                                    {
515                                                            log_error("read(socket) error (%d)\n", errno);
516                                                            loop = 0;
517                                                            break;
518                                                    }
519                                            }
520                                            else if (ret == 0) // broken pipe
521                                            {
522                                                    log_std("read(socket) EOF\n");
523                                                    sock_read_wait = 0;
524                                                  loop = 0;                                                  loop = 0;
525                                                    break;
526                                            }
527                                            else
528                                            {
529                                                    output_buf_len += ret;
530                                                    continue;
531                                          }                                          }
                                 }  
                                 else if (ret == 0) // broken pipe  
                                 {  
                                         loop = 0;  
                                 }  
                                 else  
                                 {  
                                         output_buf_len = ret;  
                                         output_buf_offset = 0;  
532                                  }                                  }
533                          }                          }
534    
535                          if ((output_buf_offset < output_buf_len) && FD_ISSET(STDOUT_FILENO, &write_fds))                          if (events[i].data.fd == STDOUT_FILENO || stdout_write_wait)
536                          {                          {
537                                  ret = (int)write(STDOUT_FILENO, output_buf + output_buf_offset, (size_t)(output_buf_len - output_buf_offset));                                  stdout_write_wait = 1;
538                                  if (ret < 0)                                  while (output_buf_offset < output_buf_len && !SYS_server_exit)
539                                  {                                  {
540                                          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));
541                                            if (ret < 0)
542                                            {
543                                                    if (errno == EAGAIN || errno == EWOULDBLOCK)
544                                                    {
545                                                            stdout_write_wait = 0;
546                                                            break;
547                                                    }
548                                                    else if (errno == EINTR)
549                                                    {
550                                                            continue;
551                                                    }
552                                                    else
553                                                    {
554                                                            log_error("write(STDOUT) error (%d)\n", errno);
555                                                            loop = 0;
556                                                            break;
557                                                    }
558                                            }
559                                            else if (ret == 0) // broken pipe
560                                          {                                          {
561                                                  log_error("write(STDOUT) error (%d)\n", errno);                                                  log_std("write(STDOUT) EOF\n");
562                                                    stdout_write_wait = 0;
563                                                  loop = 0;                                                  loop = 0;
564                                                    break;
565                                            }
566                                            else
567                                            {
568                                                    output_buf_offset += ret;
569                                                    if (output_buf_offset >= output_buf_len) // Output buffer complete
570                                                    {
571                                                            output_buf_offset = 0;
572                                                            output_buf_len = 0;
573                                                            break;
574                                                    }
575                                                    continue;
576                                          }                                          }
                                 }  
                                 else if (ret == 0) // broken pipe  
                                 {  
                                         loop = 0;  
                                 }  
                                 else  
                                 {  
                                         output_buf_offset += ret;  
577                                  }                                  }
578                          }                          }
579                  }                  }
580          }          }
581    
582    cleanup:
583            if (close(epollfd) < 0)
584            {
585                    log_error("close(epoll) error (%d)\n");
586            }
587    
588          // Restore STDIN/STDOUT flags          // Restore STDIN/STDOUT flags
589          fcntl(STDIN_FILENO, F_SETFL, flags_stdin);          fcntl(STDIN_FILENO, F_SETFL, flags_stdin);
590          fcntl(STDOUT_FILENO, F_SETFL, flags_stdout);          fcntl(STDOUT_FILENO, F_SETFL, flags_stdout);
# Line 514  int bbs_net() Line 671  int bbs_net()
671    
672          while (!SYS_server_exit)          while (!SYS_server_exit)
673          {          {
674                  ch = igetch(0);                  ch = igetch(100);
675                  switch (ch)                  switch (ch)
676                  {                  {
677                  case KEY_NULL:  // broken pipe                  case KEY_NULL:  // broken pipe
678                  case Ctrl('C'): // user cancel                  case Ctrl('C'): // user cancel
679                          return 0;                          goto cleanup;
680                  case KEY_TIMEOUT:                  case KEY_TIMEOUT:
681                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)
682                          {                          {
683                                  return 0;                                  goto cleanup;
684                          }                          }
685                          continue;                          continue;
686                  case CR:                  case CR:
# Line 568  int bbs_net() Line 725  int bbs_net()
725                  BBS_last_access_tm = time(0);                  BBS_last_access_tm = time(0);
726          }          }
727    
728    cleanup:
729            unload_bbsnet_conf();
730    
731          return 0;          return 0;
732  }  }


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

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