/[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.47 by sysadm, Tue Jun 3 13:27:49 2025 UTC Revision 1.52 by sysadm, Thu Jun 5 08:36:02 2025 UTC
# Line 48  struct process_sockaddr_t Line 48  struct process_sockaddr_t
48  };  };
49  typedef struct process_sockaddr_t PROCESS_SOCKADDR;  typedef struct process_sockaddr_t PROCESS_SOCKADDR;
50    
51  static PROCESS_SOCKADDR process_sockaddr_pool[MAX_CLIENTS_LIMIT];  static PROCESS_SOCKADDR process_sockaddr_pool[MAX_CLIENT_LIMIT];
52    
53  int net_server(const char *hostaddr, in_port_t port)  int net_server(const char *hostaddr, in_port_t port[])
54  {  {
55          unsigned int namelen;          unsigned int addrlen;
56          int ret;          int ret;
57          int flags;          int flags[2];
58          struct sockaddr_in sin;          struct sockaddr_in sin;
59          struct epoll_event ev, events[MAX_EVENTS];          struct epoll_event ev, events[MAX_EVENTS];
60          int nfds, epollfd;          int nfds, epollfd;
# Line 63  int net_server(const char *hostaddr, in_ Line 63  int net_server(const char *hostaddr, in_
63          MENU_SET *p_bbs_menu_new;          MENU_SET *p_bbs_menu_new;
64          int i, j;          int i, j;
65          pid_t pid;          pid_t pid;
66            int ssh_log_level = SSH_LOG_NOLOG;
67    
68          socket_server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);          ssh_init();
69    
70          if (socket_server < 0)          sshbind = ssh_bind_new();
         {  
                 log_error("Create socket failed\n");  
                 return -1;  
         }  
71    
72          sin.sin_family = AF_INET;          if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, hostaddr) < 0 ||
73          sin.sin_addr.s_addr = (hostaddr[0] != '\0' ? inet_addr(hostaddr) : INADDR_ANY);                  ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &port) < 0 ||
74          sin.sin_port = htons(port);                  ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_KEYFILE) < 0 ||
75                    ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_LOG_VERBOSITY, &ssh_log_level) < 0)
         // Reuse address and port  
         flags = 1;  
         if (setsockopt(socket_server, SOL_SOCKET, SO_REUSEADDR, &flags, sizeof(flags)) < 0)  
         {  
                 log_error("setsockopt SO_REUSEADDR error (%d)\n", errno);  
         }  
         if (setsockopt(socket_server, SOL_SOCKET, SO_REUSEPORT, &flags, sizeof(flags)) < 0)  
76          {          {
77                  log_error("setsockopt SO_REUSEPORT error (%d)\n", errno);                  log_error("Error setting SSH bind options: %s\n", ssh_get_error(sshbind));
78                    ssh_bind_free(sshbind);
79                    return -1;
80          }          }
81    
82          if (bind(socket_server, (struct sockaddr *)&sin, sizeof(sin)) < 0)          epollfd = epoll_create1(0);
83            if (epollfd < 0)
84          {          {
85                  log_error("Bind address %s:%u failed (%d)\n",                  log_error("epoll_create1() error (%d)\n", errno);
                                   inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), errno);  
86                  return -1;                  return -1;
87          }          }
88    
89          if (listen(socket_server, 10) < 0)          // Server socket
90            for (i = 0; i < 2; i++)
91          {          {
92                  log_error("Socket listen failed (%d)\n", errno);                  socket_server[i] = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
                 return -1;  
         }  
93    
94          strncpy(hostaddr_server, inet_ntoa(sin.sin_addr), sizeof(hostaddr_server) - 1);                  if (socket_server[i] < 0)
95          hostaddr_server[sizeof(hostaddr_server) - 1] = '\0';                  {
96                            log_error("Create socket_server error (%d)\n", errno);
97                            return -1;
98                    }
99    
100          port_server = ntohs(sin.sin_port);                  sin.sin_family = AF_INET;
101          namelen = sizeof(sin);                  sin.sin_addr.s_addr = (hostaddr[0] != '\0' ? inet_addr(hostaddr) : INADDR_ANY);
102                    sin.sin_port = htons(port[i]);
103    
104                    // Reuse address and port
105                    flags[i] = 1;
106                    if (setsockopt(socket_server[i], SOL_SOCKET, SO_REUSEADDR, &flags[i], sizeof(flags[i])) < 0)
107                    {
108                            log_error("setsockopt SO_REUSEADDR error (%d)\n", errno);
109                    }
110                    if (setsockopt(socket_server[i], SOL_SOCKET, SO_REUSEPORT, &flags[i], sizeof(flags[i])) < 0)
111                    {
112                            log_error("setsockopt SO_REUSEPORT error (%d)\n", errno);
113                    }
114    
115          log_std("Listening at %s:%d\n", hostaddr_server, port_server);                  if (bind(socket_server[i], (struct sockaddr *)&sin, sizeof(sin)) < 0)
116                    {
117                            log_error("Bind address %s:%u error (%d)\n",
118                                              inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), errno);
119                            return -1;
120                    }
121    
122          epollfd = epoll_create1(0);                  if (listen(socket_server[i], 10) < 0)
123          if (epollfd < 0)                  {
124          {                          log_error("Telnet socket listen error (%d)\n", errno);
125                  log_error("epoll_create1() error (%d)\n", errno);                          return -1;
126                  return -1;                  }
         }  
127    
128          ev.events = EPOLLIN;                  log_common("Listening at %s:%u\n", inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
129          ev.data.fd = socket_server;  
130          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, socket_server, &ev) == -1)                  ev.events = EPOLLIN;
131          {                  ev.data.fd = socket_server[i];
132                  log_error("epoll_ctl(socket_server) error (%d)\n", errno);                  if (epoll_ctl(epollfd, EPOLL_CTL_ADD, socket_server[i], &ev) == -1)
                 if (close(epollfd) < 0)  
133                  {                  {
134                          log_error("close(epoll) error (%d)\n");                          log_error("epoll_ctl(socket_server[%d]) error (%d)\n", i, errno);
135                            if (close(epollfd) < 0)
136                            {
137                                    log_error("close(epoll) error (%d)\n");
138                            }
139                            return -1;
140                  }                  }
                 return -1;  
         }  
141    
142          flags = fcntl(socket_server, F_GETFL, 0);                  flags[i] = fcntl(socket_server[i], F_GETFL, 0);
143          fcntl(socket_server, F_SETFL, flags | O_NONBLOCK);                  fcntl(socket_server[i], F_SETFL, flags[i] | O_NONBLOCK);
144            }
145    
146          // Startup complete          // Startup complete
147          sd_notifyf(0, "READY=1\n"          sd_notifyf(0, "READY=1\n"
148                                    "STATUS=Listening at %s:%d\n"                                    "STATUS=Listening at %s:%d (Telnet) and %s:%d (SSH2)\n"
149                                    "MAINPID=%d",                                    "MAINPID=%d",
150                             hostaddr_server, port_server, getpid());                             hostaddr, port[0], hostaddr, port[1], getpid());
151    
152          while (!SYS_server_exit || SYS_child_process_count > 0)          while (!SYS_server_exit || SYS_child_process_count > 0)
153          {          {
# Line 155  int net_server(const char *hostaddr, in_ Line 168  int net_server(const char *hostaddr, in_
168                                  SYS_child_exit = 1; // Retry waitid                                  SYS_child_exit = 1; // Retry waitid
169    
170                                  SYS_child_process_count--;                                  SYS_child_process_count--;
171                                  log_std("Child process (%d) exited\n", siginfo.si_pid);                                  log_common("Child process (%d) exited\n", siginfo.si_pid);
172    
173                                  if (siginfo.si_pid != section_list_loader_pid)                                  if (siginfo.si_pid != section_list_loader_pid)
174                                  {                                  {
# Line 187  int net_server(const char *hostaddr, in_ Line 200  int net_server(const char *hostaddr, in_
200    
201                  if (SYS_server_exit && !SYS_child_exit && SYS_child_process_count > 0)                  if (SYS_server_exit && !SYS_child_exit && SYS_child_process_count > 0)
202                  {                  {
203                          log_std("Notify %d child process to exit\n", SYS_child_process_count);                          log_common("Notify %d child process to exit\n", SYS_child_process_count);
204                          if (kill(0, SIGTERM) < 0)                          if (kill(0, SIGTERM) < 0)
205                          {                          {
206                                  log_error("Send SIGTERM signal failed (%d)\n", errno);                                  log_error("Send SIGTERM signal failed (%d)\n", errno);
# Line 228  int net_server(const char *hostaddr, in_ Line 241  int net_server(const char *hostaddr, in_
241                                  p_bbs_menu = p_bbs_menu_new;                                  p_bbs_menu = p_bbs_menu_new;
242                                  p_bbs_menu_new = NULL;                                  p_bbs_menu_new = NULL;
243    
244                                  log_std("Reload menu successfully\n");                                  log_common("Reload menu successfully\n");
245                          }                          }
246    
247                          sd_notify(0, "READY=1");                          sd_notify(0, "READY=1");
# Line 247  int net_server(const char *hostaddr, in_ Line 260  int net_server(const char *hostaddr, in_
260                                  }                                  }
261                          }                          }
262    
263                          log_std("Reload data files successfully\n");                          log_common("Reload data files successfully\n");
264                          sd_notify(0, "READY=1");                          sd_notify(0, "READY=1");
265                  }                  }
266    
# Line 281  int net_server(const char *hostaddr, in_ Line 294  int net_server(const char *hostaddr, in_
294    
295                  for (int i = 0; i < nfds; i++)                  for (int i = 0; i < nfds; i++)
296                  {                  {
297                          if (events[i].data.fd == socket_server)                          if (events[i].data.fd == socket_server[0] || events[i].data.fd == socket_server[1])
298                          {                          {
299                                    SSH_v2 = (events[i].data.fd == socket_server[1]);
300    
301                                  while (!SYS_server_exit) // Accept all incoming connections until error                                  while (!SYS_server_exit) // Accept all incoming connections until error
302                                  {                                  {
303                                          socket_client = accept(socket_server, (struct sockaddr *)&sin, &namelen);                                          addrlen = sizeof(sin);
304                                            socket_client = accept(events[i].data.fd, (struct sockaddr *)&sin, &addrlen);
305                                          if (socket_client < 0)                                          if (socket_client < 0)
306                                          {                                          {
307                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)
# Line 308  int net_server(const char *hostaddr, in_ Line 324  int net_server(const char *hostaddr, in_
324    
325                                          port_client = ntohs(sin.sin_port);                                          port_client = ntohs(sin.sin_port);
326    
327                                          log_std("Accept connection from %s:%d\n", hostaddr_client, port_client);                                          log_common("Accept connection from %s:%d\n", hostaddr_client, port_client);
328    
329                                          if (SYS_child_process_count - 1 < BBS_max_client)                                          if (SYS_child_process_count - 1 < BBS_max_client)
330                                          {                                          {
# Line 320  int net_server(const char *hostaddr, in_ Line 336  int net_server(const char *hostaddr, in_
336                                                                  j++;                                                                  j++;
337                                                                  if (j >= BBS_max_client_per_ip)                                                                  if (j >= BBS_max_client_per_ip)
338                                                                  {                                                                  {
339                                                                          log_error("Too many client connections (%d) from %s\n", j, hostaddr_client);                                                                          log_common("Too many client connections (%d) from %s\n", j, hostaddr_client);
340                                                                          break;                                                                          break;
341                                                                  }                                                                  }
342                                                          }                                                          }
# Line 374  int net_server(const char *hostaddr, in_ Line 390  int net_server(const char *hostaddr, in_
390                  log_error("close(epoll) error (%d)\n");                  log_error("close(epoll) error (%d)\n");
391          }          }
392    
393          fcntl(socket_server, F_SETFL, flags);          for (i = 0; i < 2; i++)
   
         if (close(socket_server) == -1)  
394          {          {
395                  log_error("Close server socket failed\n");                  fcntl(socket_server[i], F_SETFL, flags[i]);
396    
397                    if (close(socket_server[i]) == -1)
398                    {
399                            log_error("Close server socket failed\n");
400                    }
401          }          }
402    
403            ssh_bind_free(sshbind);
404            ssh_finalize();
405    
406          return 0;          return 0;
407  }  }


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

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