/[LeafOK_CVS]/lbbs/src/net_server.c
ViewVC logotype

Diff of /lbbs/src/net_server.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.80 by sysadm, Fri Nov 7 04:58:09 2025 UTC Revision 1.85 by sysadm, Sun Nov 16 04:40:52 2025 UTC
# Line 6  Line 6 
6   * Copyright (C) 2004-2025  Leaflet <leaflet@leafok.com>   * Copyright (C) 2004-2025  Leaflet <leaflet@leafok.com>
7   */   */
8    
9    #ifdef HAVE_CONFIG_H
10    #include "config.h"
11    #endif
12    
13  #include "bbs.h"  #include "bbs.h"
14  #include "bbs_main.h"  #include "bbs_main.h"
15  #include "bwf.h"  #include "bwf.h"
16  #include "common.h"  #include "common.h"
17  #include "database.h"  #include "database.h"
18  #include "file_loader.h"  #include "file_loader.h"
19    #include "hash_dict.h"
20  #include "io.h"  #include "io.h"
21  #include "init.h"  #include "init.h"
22  #include "log.h"  #include "log.h"
# Line 48  enum _net_server_constant_t Line 53  enum _net_server_constant_t
53          SSH_AUTH_MAX_DURATION = 60 * 1000, // milliseconds          SSH_AUTH_MAX_DURATION = 60 * 1000, // milliseconds
54  };  };
55    
 struct process_sockaddr_t  
 {  
         pid_t pid;  
         in_addr_t s_addr;  
 };  
 typedef struct process_sockaddr_t PROCESS_SOCKADDR;  
   
 static PROCESS_SOCKADDR process_sockaddr_pool[MAX_CLIENT_LIMIT];  
   
56  static const char SFTP_SERVER_PATH[] = "/usr/lib/sftp-server";  static const char SFTP_SERVER_PATH[] = "/usr/lib/sftp-server";
57    
58  /* A userdata struct for session. */  /* A userdata struct for session. */
# Line 397  static int fork_server(void) Line 393  static int fork_server(void)
393          }          }
394    
395          // Redirect Input          // Redirect Input
         close(STDIN_FILENO);  
396          if (dup2(socket_client, STDIN_FILENO) == -1)          if (dup2(socket_client, STDIN_FILENO) == -1)
397          {          {
398                  log_error("Redirect stdin to client socket failed\n");                  log_error("Redirect stdin to client socket failed\n");
# Line 405  static int fork_server(void) Line 400  static int fork_server(void)
400          }          }
401    
402          // Redirect Output          // Redirect Output
         close(STDOUT_FILENO);  
403          if (dup2(socket_client, STDOUT_FILENO) == -1)          if (dup2(socket_client, STDOUT_FILENO) == -1)
404          {          {
405                  log_error("Redirect stdout to client socket failed\n");                  log_error("Redirect stdout to client socket failed\n");
# Line 452  cleanup: Line 446  cleanup:
446    
447  int net_server(const char *hostaddr, in_port_t port[])  int net_server(const char *hostaddr, in_port_t port[])
448  {  {
449            HASH_DICT *hash_dict_pid_sockaddr = NULL;
450            HASH_DICT *hash_dict_sockaddr_count = NULL;
451    
452          unsigned int addrlen;          unsigned int addrlen;
453          int ret;          int ret;
454          int flags[2];          int flags[2];
# Line 547  int net_server(const char *hostaddr, in_ Line 544  int net_server(const char *hostaddr, in_
544                  fcntl(socket_server[i], F_SETFL, flags[i] | O_NONBLOCK);                  fcntl(socket_server[i], F_SETFL, flags[i] | O_NONBLOCK);
545          }          }
546    
547            hash_dict_pid_sockaddr = hash_dict_create(MAX_CLIENT_LIMIT);
548            if (hash_dict_pid_sockaddr == NULL)
549            {
550                    log_error("hash_dict_create(hash_dict_pid_sockaddr) error\n");
551                    return -1;
552            }
553            hash_dict_sockaddr_count = hash_dict_create(MAX_CLIENT_LIMIT);
554            if (hash_dict_sockaddr_count == NULL)
555            {
556                    log_error("hash_dict_create(hash_dict_sockaddr_count) error\n");
557                    return -1;
558            }
559    
560          // Startup complete          // Startup complete
561          sd_notifyf(0, "READY=1\n"          sd_notifyf(0, "READY=1\n"
562                                    "STATUS=Listening at %s:%d (Telnet) and %s:%d (SSH2)\n"                                    "STATUS=Listening at %s:%d (Telnet) and %s:%d (SSH2)\n"
# Line 576  int net_server(const char *hostaddr, in_ Line 586  int net_server(const char *hostaddr, in_
586    
587                                  if (siginfo.si_pid != section_list_loader_pid)                                  if (siginfo.si_pid != section_list_loader_pid)
588                                  {                                  {
589                                          i = 0;                                          j = 0;
590                                          for (; i < BBS_max_client; i++)                                          ret = hash_dict_get(hash_dict_pid_sockaddr, (uint64_t)siginfo.si_pid, (int64_t *)&j);
591                                            if (ret < 0)
592                                          {                                          {
593                                                  if (process_sockaddr_pool[i].pid == siginfo.si_pid)                                                  log_error("hash_dict_get(hash_dict_pid_sockaddr, %d) error\n", siginfo.si_pid);
                                                 {  
                                                         process_sockaddr_pool[i].pid = 0;  
                                                         break;  
                                                 }  
594                                          }                                          }
595                                          if (i >= BBS_max_client)                                          else
596                                          {                                          {
597                                                  log_error("Child process (%d) not found in process sockaddr pool\n", siginfo.si_pid);                                                  ret = hash_dict_inc(hash_dict_sockaddr_count, (uint64_t)j, -1);
598                                                    if (ret < 0)
599                                                    {
600                                                            log_error("hash_dict_inc(hash_dict_sockaddr_count, %d, -1) error\n", j);
601                                                    }
602    
603                                                    ret = hash_dict_del(hash_dict_pid_sockaddr, (uint64_t)siginfo.si_pid);
604                                                    if (ret < 0)
605                                                    {
606                                                            log_error("hash_dict_del(hash_dict_pid_sockaddr, %d) error\n", siginfo.si_pid);
607                                                    }
608                                          }                                          }
609                                  }                                  }
610                          }                          }
# Line 609  int net_server(const char *hostaddr, in_ Line 626  int net_server(const char *hostaddr, in_
626                                  sd_notifyf(0, "STATUS=Notify %d child process to exit", SYS_child_process_count);                                  sd_notifyf(0, "STATUS=Notify %d child process to exit", SYS_child_process_count);
627                                  log_common("Notify %d child process to exit\n", SYS_child_process_count);                                  log_common("Notify %d child process to exit\n", SYS_child_process_count);
628    
629                                  if (kill(0, SIGTERM) < 0)                                  if (kill(-getpid(), SIGTERM) < 0)
630                                  {                                  {
631                                          log_error("Send SIGTERM signal failed (%d)\n", errno);                                          log_error("Send SIGTERM signal failed (%d)\n", errno);
632                                  }                                  }
# Line 621  int net_server(const char *hostaddr, in_ Line 638  int net_server(const char *hostaddr, in_
638                          {                          {
639                                  sd_notifyf(0, "STATUS=Kill %d child process", SYS_child_process_count);                                  sd_notifyf(0, "STATUS=Kill %d child process", SYS_child_process_count);
640    
641                                  for (i = 0; i < BBS_max_client; i++)                                  if (kill(-getpid(), SIGKILL) < 0)
642                                  {                                  {
643                                          if (process_sockaddr_pool[i].pid != 0)                                          log_error("Send SIGKILL signal failed (%d)\n", errno);
                                         {  
                                                 log_error("Kill child process (pid=%d)\n", process_sockaddr_pool[i].pid);  
                                                 if (kill(process_sockaddr_pool[i].pid, SIGKILL) < 0)  
                                                 {  
                                                         log_error("Send SIGKILL signal failed (%d)\n", errno);  
                                                 }  
                                         }  
644                                  }                                  }
645    
646                                  notify_child_exit = 2;                                  notify_child_exit = 2;
# Line 648  int net_server(const char *hostaddr, in_ Line 658  int net_server(const char *hostaddr, in_
658                          SYS_conf_reload = 0;                          SYS_conf_reload = 0;
659                          sd_notify(0, "RELOADING=1");                          sd_notify(0, "RELOADING=1");
660    
661                            // Restart log
662                            if (log_restart() < 0)
663                            {
664                                    log_error("Restart logging failed\n");
665                            }
666    
667                          // Reload configuration                          // Reload configuration
668                          if (load_conf(CONF_BBSD) < 0)                          if (load_conf(CONF_BBSD) < 0)
669                          {                          {
# Line 704  int net_server(const char *hostaddr, in_ Line 720  int net_server(const char *hostaddr, in_
720                                  log_common("Reload section config and gen_ex successfully\n");                                  log_common("Reload section config and gen_ex successfully\n");
721                          }                          }
722    
723                            // Notify child processes to reload configuration
724                            if (kill(-getpid(), SIGUSR1) < 0)
725                            {
726                                    log_error("Send SIGUSR1 signal failed (%d)\n", errno);
727                            }
728    
729                          sd_notify(0, "READY=1");                          sd_notify(0, "READY=1");
730                  }                  }
731    
# Line 762  int net_server(const char *hostaddr, in_ Line 784  int net_server(const char *hostaddr, in_
784                                          if (SYS_child_process_count - 1 < BBS_max_client)                                          if (SYS_child_process_count - 1 < BBS_max_client)
785                                          {                                          {
786                                                  j = 0;                                                  j = 0;
787                                                  for (i = 0; i < BBS_max_client; i++)                                                  ret = hash_dict_get(hash_dict_sockaddr_count, (uint64_t)sin.sin_addr.s_addr, (int64_t *)&j);
788                                                    if (ret < 0)
789                                                  {                                                  {
790                                                          if (process_sockaddr_pool[i].pid != 0 && process_sockaddr_pool[i].s_addr == sin.sin_addr.s_addr)                                                          log_error("hash_dict_get(hash_dict_sockaddr_count, %s) error\n", hostaddr_client);
                                                         {  
                                                                 j++;  
                                                                 if (j >= BBS_max_client_per_ip)  
                                                                 {  
                                                                         log_common("Too many client connections (%d) from %s\n", j, hostaddr_client);  
                                                                         break;  
                                                                 }  
                                                         }  
791                                                  }                                                  }
792    
793                                                  if (j < BBS_max_client_per_ip)                                                  if (j < BBS_max_client_per_ip)
# Line 783  int net_server(const char *hostaddr, in_ Line 798  int net_server(const char *hostaddr, in_
798                                                          }                                                          }
799                                                          else if (pid > 0)                                                          else if (pid > 0)
800                                                          {                                                          {
801                                                                  i = 0;                                                                  ret = hash_dict_set(hash_dict_pid_sockaddr, (uint64_t)pid, sin.sin_addr.s_addr);
802                                                                  for (; i < BBS_max_client; i++)                                                                  if (ret < 0)
803                                                                  {                                                                  {
804                                                                          if (process_sockaddr_pool[i].pid == 0)                                                                          log_error("hash_dict_set(hash_dict_pid_sockaddr, %d, %s) error\n", pid, hostaddr_client);
                                                                         {  
                                                                                 break;  
                                                                         }  
805                                                                  }                                                                  }
806    
807                                                                  if (i >= BBS_max_client)                                                                  ret = hash_dict_inc(hash_dict_sockaddr_count, (uint64_t)sin.sin_addr.s_addr, 1);
808                                                                    if (ret < 0)
809                                                                  {                                                                  {
810                                                                          log_error("Process sockaddr pool depleted\n");                                                                          log_error("hash_dict_inc(hash_dict_sockaddr_count, %s, %d) error\n", hostaddr_client, 1);
                                                                 }  
                                                                 else  
                                                                 {  
                                                                         process_sockaddr_pool[i].pid = pid;  
                                                                         process_sockaddr_pool[i].s_addr = sin.sin_addr.s_addr;  
811                                                                  }                                                                  }
812                                                          }                                                          }
813                                                  }                                                  }
814                                                    else
815                                                    {
816                                                            log_error("Rejected client connection from %s over limit per IP (%d)\n", hostaddr_client, BBS_max_client_per_ip);
817                                                    }
818                                          }                                          }
819                                          else                                          else
820                                          {                                          {
# Line 833  int net_server(const char *hostaddr, in_ Line 845  int net_server(const char *hostaddr, in_
845                  }                  }
846          }          }
847    
848            hash_dict_destroy(hash_dict_pid_sockaddr);
849            hash_dict_destroy(hash_dict_sockaddr_count);
850    
851          ssh_bind_free(sshbind);          ssh_bind_free(sshbind);
852          ssh_finalize();          ssh_finalize();
853    


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

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