/[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.42 by sysadm, Tue May 27 03:25:02 2025 UTC Revision 1.72 by sysadm, Mon Oct 13 07:13:39 2025 UTC
# Line 14  Line 14 
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
17  #define _XOPEN_SOURCE 500  #include "bbs.h"
18  #define _POSIX_C_SOURCE 200809L  #include "bbs_main.h"
 #define _GNU_SOURCE  
   
 #include "net_server.h"  
19  #include "common.h"  #include "common.h"
20  #include "log.h"  #include "database.h"
21    #include "file_loader.h"
22  #include "io.h"  #include "io.h"
23  #include "fork.h"  #include "init.h"
24    #include "log.h"
25    #include "login.h"
26  #include "menu.h"  #include "menu.h"
27  #include "file_loader.h"  #include "net_server.h"
28    #include "section_list.h"
29  #include "section_list_loader.h"  #include "section_list_loader.h"
30  #include <errno.h>  #include <errno.h>
31  #include <fcntl.h>  #include <fcntl.h>
 #include <string.h>  
32  #include <signal.h>  #include <signal.h>
33  #include <stdlib.h>  #include <stdlib.h>
34    #include <string.h>
35  #include <unistd.h>  #include <unistd.h>
36  #include <sys/syscall.h>  #include <arpa/inet.h>
37    #include <libssh/callbacks.h>
38    #include <libssh/libssh.h>
39    #include <libssh/server.h>
40    #include <netinet/in.h>
41    #include <sys/epoll.h>
42  #include <sys/socket.h>  #include <sys/socket.h>
43    #include <sys/syscall.h>
44    #include <sys/types.h>
45  #include <sys/wait.h>  #include <sys/wait.h>
 #include <sys/epoll.h>  
 #include <arpa/inet.h>  
46  #include <systemd/sd-daemon.h>  #include <systemd/sd-daemon.h>
47    
48  int net_server(const char *hostaddr, in_port_t port)  #define WAIT_CHILD_PROCESS_EXIT_TIMEOUT 5 // second
49    #define WAIT_CHILD_PROCESS_KILL_TIMEOUT 1 // second
50    
51    struct process_sockaddr_t
52    {
53            pid_t pid;
54            in_addr_t s_addr;
55    };
56    typedef struct process_sockaddr_t PROCESS_SOCKADDR;
57    
58    static PROCESS_SOCKADDR process_sockaddr_pool[MAX_CLIENT_LIMIT];
59    
60    #define SSH_AUTH_MAX_DURATION (60 * 1000) // milliseconds
61    
62    struct ssl_server_cb_data_t
63    {
64            int tries;
65            int error;
66    };
67    
68    static int auth_password(ssh_session session, const char *user,
69                                                     const char *password, void *userdata)
70  {  {
71          unsigned int namelen;          struct ssl_server_cb_data_t *p_data = userdata;
72          int ret;          int ret;
         int flags;  
         struct sockaddr_in sin;  
         struct epoll_event ev, events[MAX_EVENTS];  
         int nfds, epollfd;  
         siginfo_t siginfo;  
         int sd_notify_stopping = 0;  
         MENU_SET *p_bbs_menu_new;  
73    
74          socket_server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);          if (strcmp(user, "guest") == 0)
75            {
76                    ret = load_guest_info();
77            }
78            else
79            {
80                    ret = check_user(user, password);
81            }
82    
83            if (ret == 0)
84            {
85                    return SSH_AUTH_SUCCESS;
86            }
87    
88            if ((++(p_data->tries)) >= BBS_login_retry_times)
89            {
90                    p_data->error = 1;
91            }
92    
93            return SSH_AUTH_DENIED;
94    }
95    
96    static int pty_request(ssh_session session, ssh_channel channel, const char *term,
97                                               int x, int y, int px, int py, void *userdata)
98    {
99            return 0;
100    }
101    
102    static int shell_request(ssh_session session, ssh_channel channel, void *userdata)
103    {
104            return 0;
105    }
106    
107    static struct ssh_channel_callbacks_struct channel_cb = {
108            .channel_pty_request_function = pty_request,
109            .channel_shell_request_function = shell_request};
110    
111    static ssh_channel new_session_channel(ssh_session session, void *userdata)
112    {
113            if (SSH_channel != NULL)
114            {
115                    return NULL;
116            }
117    
118            SSH_channel = ssh_channel_new(session);
119            ssh_callbacks_init(&channel_cb);
120            ssh_set_channel_callbacks(SSH_channel, &channel_cb);
121    
122          if (socket_server < 0)          return SSH_channel;
123    }
124    
125    static int fork_server(void)
126    {
127            ssh_event event;
128            long int ssh_timeout = 0;
129            int pid;
130            int i;
131            int ret;
132    
133            struct ssl_server_cb_data_t cb_data = {
134                    .tries = 0,
135                    .error = 0,
136            };
137    
138            struct ssh_server_callbacks_struct cb = {
139                    .userdata = &cb_data,
140                    .auth_password_function = auth_password,
141                    .channel_open_request_session_function = new_session_channel,
142            };
143    
144            pid = fork();
145    
146            if (pid > 0) // Parent process
147            {
148                    SYS_child_process_count++;
149                    log_common("Child process (%d) start\n", pid);
150                    return pid;
151            }
152            else if (pid < 0) // Error
153          {          {
154                  log_error("Create socket failed\n");                  log_error("fork() error (%d)\n", errno);
155                  return -1;                  return -1;
156          }          }
157    
158          sin.sin_family = AF_INET;          // Child process
         sin.sin_addr.s_addr = (hostaddr[0] != '\0' ? inet_addr(hostaddr) : INADDR_ANY);  
         sin.sin_port = htons(port);  
159    
160          // Reuse address and port          if (close(socket_server[0]) == -1 || close(socket_server[1]) == -1)
         flags = 1;  
         if (setsockopt(socket_server, SOL_SOCKET, SO_REUSEADDR, &flags, sizeof(flags)) < 0)  
161          {          {
162                  log_error("setsockopt SO_REUSEADDR error (%d)\n", errno);                  log_error("Close server socket failed\n");
163          }          }
164          if (setsockopt(socket_server, SOL_SOCKET, SO_REUSEPORT, &flags, sizeof(flags)) < 0)  
165            SSH_session = ssh_new();
166    
167            if (SSH_v2)
168          {          {
169                  log_error("setsockopt SO_REUSEPORT error (%d)\n", errno);                  if (ssh_bind_accept_fd(sshbind, SSH_session, socket_client) != SSH_OK)
170                    {
171                            log_error("ssh_bind_accept_fd() error: %s\n", ssh_get_error(SSH_session));
172                            goto cleanup;
173                    }
174    
175                    ssh_bind_free(sshbind);
176    
177                    ssh_callbacks_init(&cb);
178                    ssh_set_server_callbacks(SSH_session, &cb);
179    
180                    ssh_timeout = 60; // second
181                    if (ssh_options_set(SSH_session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0)
182                    {
183                            log_error("Error setting SSH options: %s\n", ssh_get_error(SSH_session));
184                            goto cleanup;
185                    }
186    
187                    if (ssh_handle_key_exchange(SSH_session))
188                    {
189                            log_error("ssh_handle_key_exchange() error: %s\n", ssh_get_error(SSH_session));
190                            goto cleanup;
191                    }
192                    ssh_set_auth_methods(SSH_session, SSH_AUTH_METHOD_PASSWORD);
193    
194                    event = ssh_event_new();
195                    ssh_event_add_session(event, SSH_session);
196    
197                    for (i = 0; i < SSH_AUTH_MAX_DURATION && !SYS_server_exit && !cb_data.error && SSH_channel == NULL; i += 100)
198                    {
199                            ret = ssh_event_dopoll(event, 100); // 0.1 second
200                            if (ret == SSH_ERROR)
201                            {
202                                    log_error("ssh_event_dopoll() error: %s\n", ssh_get_error(SSH_session));
203                                    goto cleanup;
204                            }
205                    }
206    
207                    if (cb_data.error)
208                    {
209                            log_error("SSH auth error, tried %d times\n", cb_data.tries);
210                            goto cleanup;
211                    }
212    
213                    ssh_timeout = 0;
214                    if (ssh_options_set(SSH_session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0)
215                    {
216                            log_error("Error setting SSH options: %s\n", ssh_get_error(SSH_session));
217                            goto cleanup;
218                    }
219          }          }
220    
221          if (bind(socket_server, (struct sockaddr *)&sin, sizeof(sin)) < 0)          // Redirect Input
222            close(STDIN_FILENO);
223            if (dup2(socket_client, STDIN_FILENO) == -1)
224          {          {
225                  log_error("Bind address %s:%u failed (%d)\n",                  log_error("Redirect stdin to client socket failed\n");
226                                    inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), errno);                  goto cleanup;
                 return -1;  
227          }          }
228    
229          if (listen(socket_server, 10) < 0)          // Redirect Output
230            close(STDOUT_FILENO);
231            if (dup2(socket_client, STDOUT_FILENO) == -1)
232          {          {
233                  log_error("Socket listen failed (%d)\n", errno);                  log_error("Redirect stdout to client socket failed\n");
234                  return -1;                  goto cleanup;
235            }
236    
237            SYS_child_process_count = 0;
238    
239            bbs_main();
240    
241    cleanup:
242            // Child process exit
243            SYS_server_exit = 1;
244    
245            if (SSH_v2)
246            {
247                    ssh_channel_free(SSH_channel);
248                    ssh_disconnect(SSH_session);
249          }          }
250            else if (close(socket_client) == -1)
251            {
252                    log_error("Close client socket failed\n");
253            }
254    
255            ssh_free(SSH_session);
256            ssh_finalize();
257    
258          strncpy(hostaddr_server, inet_ntoa(sin.sin_addr), sizeof(hostaddr_server) - 1);          // Close Input and Output for client
259          hostaddr_server[sizeof(hostaddr_server) - 1] = '\0';          close(STDIN_FILENO);
260            close(STDOUT_FILENO);
261    
262          port_server = ntohs(sin.sin_port);          log_common("Process exit normally\n");
263          namelen = sizeof(sin);          log_end();
264    
265          log_std("Listening at %s:%d\n", hostaddr_server, port_server);          _exit(0);
266    
267            return 0;
268    }
269    
270    int net_server(const char *hostaddr, in_port_t port[])
271    {
272            unsigned int addrlen;
273            int ret;
274            int flags[2];
275            struct sockaddr_in sin;
276            struct epoll_event ev, events[MAX_EVENTS];
277            int nfds, epollfd;
278            siginfo_t siginfo;
279            int notify_child_exit = 0;
280            time_t tm_notify_child_exit = time(NULL);
281            int sd_notify_stopping = 0;
282            MENU_SET bbs_menu_new;
283            MENU_SET top10_menu_new;
284            int i, j;
285            pid_t pid;
286            int ssh_log_level = SSH_LOG_NOLOG;
287    
288            ssh_init();
289    
290            sshbind = ssh_bind_new();
291    
292            if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, hostaddr) < 0 ||
293                    ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &port) < 0 ||
294                    ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_KEYFILE) < 0 ||
295                    ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY_ALGORITHMS, "ssh-rsa,rsa-sha2-512,rsa-sha2-256") < 0 ||
296                    ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_LOG_VERBOSITY, &ssh_log_level) < 0)
297            {
298                    log_error("Error setting SSH bind options: %s\n", ssh_get_error(sshbind));
299                    ssh_bind_free(sshbind);
300                    return -1;
301            }
302    
303          epollfd = epoll_create1(0);          epollfd = epoll_create1(0);
304          if (epollfd < 0)          if (epollfd < 0)
# Line 102  int net_server(const char *hostaddr, in_ Line 307  int net_server(const char *hostaddr, in_
307                  return -1;                  return -1;
308          }          }
309    
310          ev.events = EPOLLIN;          // Server socket
311          ev.data.fd = socket_server;          for (i = 0; i < 2; i++)
         if (epoll_ctl(epollfd, EPOLL_CTL_ADD, socket_server, &ev) == -1)  
312          {          {
313                  log_error("epoll_ctl(socket_server) error (%d)\n", errno);                  socket_server[i] = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
314                  if (close(epollfd) < 0)  
315                    if (socket_server[i] < 0)
316                  {                  {
317                          log_error("close(epoll) error (%d)\n");                          log_error("Create socket_server error (%d)\n", errno);
318                            return -1;
319                    }
320    
321                    sin.sin_family = AF_INET;
322                    sin.sin_addr.s_addr = (hostaddr[0] != '\0' ? inet_addr(hostaddr) : INADDR_ANY);
323                    sin.sin_port = htons(port[i]);
324    
325                    // Reuse address and port
326                    flags[i] = 1;
327                    if (setsockopt(socket_server[i], SOL_SOCKET, SO_REUSEADDR, &flags[i], sizeof(flags[i])) < 0)
328                    {
329                            log_error("setsockopt SO_REUSEADDR error (%d)\n", errno);
330                    }
331                    if (setsockopt(socket_server[i], SOL_SOCKET, SO_REUSEPORT, &flags[i], sizeof(flags[i])) < 0)
332                    {
333                            log_error("setsockopt SO_REUSEPORT error (%d)\n", errno);
334                  }                  }
                 return -1;  
         }  
335    
336          flags = fcntl(socket_server, F_GETFL, 0);                  if (bind(socket_server[i], (struct sockaddr *)&sin, sizeof(sin)) < 0)
337          fcntl(socket_server, F_SETFL, flags | O_NONBLOCK);                  {
338                            log_error("Bind address %s:%u error (%d)\n",
339                                              inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), errno);
340                            return -1;
341                    }
342    
343                    if (listen(socket_server[i], 10) < 0)
344                    {
345                            log_error("Telnet socket listen error (%d)\n", errno);
346                            return -1;
347                    }
348    
349                    log_common("Listening at %s:%u\n", inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
350    
351                    ev.events = EPOLLIN;
352                    ev.data.fd = socket_server[i];
353                    if (epoll_ctl(epollfd, EPOLL_CTL_ADD, socket_server[i], &ev) == -1)
354                    {
355                            log_error("epoll_ctl(socket_server[%d]) error (%d)\n", i, errno);
356                            if (close(epollfd) < 0)
357                            {
358                                    log_error("close(epoll) error (%d)\n");
359                            }
360                            return -1;
361                    }
362    
363                    flags[i] = fcntl(socket_server[i], F_GETFL, 0);
364                    fcntl(socket_server[i], F_SETFL, flags[i] | O_NONBLOCK);
365            }
366    
367          // Startup complete          // Startup complete
368          sd_notifyf(0, "READY=1\n"          sd_notifyf(0, "READY=1\n"
369                                    "STATUS=Listening at %s:%d\n"                                    "STATUS=Listening at %s:%d (Telnet) and %s:%d (SSH2)\n"
370                                    "MAINPID=%d",                                    "MAINPID=%d",
371                             hostaddr_server, port_server, getpid());                             hostaddr, port[0], hostaddr, port[1], getpid());
372    
373          while (!SYS_server_exit || SYS_child_process_count > 0)          while (!SYS_server_exit || SYS_child_process_count > 0)
374          {          {
# Line 142  int net_server(const char *hostaddr, in_ Line 389  int net_server(const char *hostaddr, in_
389                                  SYS_child_exit = 1; // Retry waitid                                  SYS_child_exit = 1; // Retry waitid
390    
391                                  SYS_child_process_count--;                                  SYS_child_process_count--;
392                                  log_std("Child process (%d) exited\n", siginfo.si_pid);                                  log_common("Child process (%d) exited\n", siginfo.si_pid);
393    
394                                    if (siginfo.si_pid != section_list_loader_pid)
395                                    {
396                                            i = 0;
397                                            for (; i < BBS_max_client; i++)
398                                            {
399                                                    if (process_sockaddr_pool[i].pid == siginfo.si_pid)
400                                                    {
401                                                            process_sockaddr_pool[i].pid = 0;
402                                                            break;
403                                                    }
404                                            }
405                                            if (i >= BBS_max_client)
406                                            {
407                                                    log_error("Child process (%d) not found in process sockaddr pool\n", siginfo.si_pid);
408                                            }
409                                    }
410                          }                          }
411                          else if (ret == 0)                          else if (ret == 0)
412                          {                          {
# Line 157  int net_server(const char *hostaddr, in_ Line 421  int net_server(const char *hostaddr, in_
421    
422                  if (SYS_server_exit && !SYS_child_exit && SYS_child_process_count > 0)                  if (SYS_server_exit && !SYS_child_exit && SYS_child_process_count > 0)
423                  {                  {
424                          log_std("Notify %d child process to exit\n", SYS_child_process_count);                          if (notify_child_exit == 0)
                         if (kill(0, SIGTERM) < 0)  
425                          {                          {
426                                  log_error("Send SIGTERM signal failed (%d)\n", errno);                                  sd_notifyf(0, "STATUS=Notify %d child process to exit", SYS_child_process_count);
427                                    log_common("Notify %d child process to exit\n", SYS_child_process_count);
428    
429                                    if (kill(0, SIGTERM) < 0)
430                                    {
431                                            log_error("Send SIGTERM signal failed (%d)\n", errno);
432                                    }
433    
434                                    notify_child_exit = 1;
435                                    tm_notify_child_exit = time(NULL);
436                          }                          }
437                            else if (notify_child_exit == 1 && time(NULL) - tm_notify_child_exit >= WAIT_CHILD_PROCESS_EXIT_TIMEOUT)
438                            {
439                                    sd_notifyf(0, "STATUS=Kill %d child process", SYS_child_process_count);
440    
441                          sd_notifyf(0, "STATUS=Waiting for %d child process to exit", SYS_child_process_count);                                  for (i = 0; i < BBS_max_client; i++)
442                                    {
443                                            if (process_sockaddr_pool[i].pid != 0)
444                                            {
445                                                    log_error("Kill child process (pid=%d)\n", process_sockaddr_pool[i].pid);
446                                                    if (kill(process_sockaddr_pool[i].pid, SIGKILL) < 0)
447                                                    {
448                                                            log_error("Send SIGKILL signal failed (%d)\n", errno);
449                                                    }
450                                            }
451                                    }
452    
453                                    notify_child_exit = 2;
454                                    tm_notify_child_exit = time(NULL);
455                            }
456                            else if (notify_child_exit == 2 && time(NULL) - tm_notify_child_exit >= WAIT_CHILD_PROCESS_KILL_TIMEOUT)
457                            {
458                                    log_error("Main process prepare to exit without waiting for %d child process any longer\n", SYS_child_process_count);
459                                    SYS_child_process_count = 0;
460                            }
461                  }                  }
462    
463                  if (SYS_menu_reload && !SYS_server_exit)                  if (SYS_conf_reload && !SYS_server_exit)
464                  {                  {
465                          SYS_menu_reload = 0;                          SYS_conf_reload = 0;
466                          sd_notify(0, "RELOADING=1");                          sd_notify(0, "RELOADING=1");
467    
468                          p_bbs_menu_new = calloc(1, sizeof(MENU_SET));                          // Reload configuration
469                          if (p_bbs_menu_new == NULL)                          if (load_conf(CONF_BBSD) < 0)
470                          {                          {
471                                  log_error("OOM: calloc(MENU_SET)\n");                                  log_error("Reload conf failed\n");
472                          }                          }
                         else if (load_menu(p_bbs_menu_new, CONF_MENU) < 0)  
                         {  
                                 unload_menu(p_bbs_menu_new);  
                                 free(p_bbs_menu_new);  
                                 p_bbs_menu_new = NULL;  
473    
474                                  log_error("Reload menu failed\n");                          if (load_menu(&bbs_menu_new, CONF_MENU) < 0)
475                            {
476                                    unload_menu(&bbs_menu_new);
477                                    log_error("Reload bbs menu failed\n");
478                          }                          }
479                          else                          else
480                          {                          {
481                                  unload_menu(p_bbs_menu);                                  unload_menu(&bbs_menu);
482                                  free(p_bbs_menu);                                  memcpy(&bbs_menu, &bbs_menu_new, sizeof(bbs_menu_new));
483                                    log_common("Reload bbs menu successfully\n");
                                 p_bbs_menu = p_bbs_menu_new;  
                                 p_bbs_menu_new = NULL;  
   
                                 log_std("Reload menu successfully\n");  
484                          }                          }
485    
486                          sd_notify(0, "READY=1");                          if (load_menu(&top10_menu_new, CONF_TOP10_MENU) < 0)
487                  }                          {
488                                    unload_menu(&top10_menu_new);
489                  if (SYS_data_file_reload && !SYS_server_exit)                                  log_error("Reload top10 menu failed\n");
490                  {                          }
491                          SYS_data_file_reload = 0;                          else
492                          sd_notify(0, "RELOADING=1");                          {
493                                    unload_menu(&top10_menu);
494                                    top10_menu_new.allow_exit = 1;
495                                    memcpy(&top10_menu, &top10_menu_new, sizeof(top10_menu_new));
496                                    log_common("Reload top10 menu successfully\n");
497                            }
498    
499                          for (int i = 0; i < data_files_load_startup_count; i++)                          for (int i = 0; i < data_files_load_startup_count; i++)
500                          {                          {
# Line 210  int net_server(const char *hostaddr, in_ Line 503  int net_server(const char *hostaddr, in_
503                                          log_error("load_file_mmap(%s) error\n", data_files_load_startup[i]);                                          log_error("load_file_mmap(%s) error\n", data_files_load_startup[i]);
504                                  }                                  }
505                          }                          }
506                            log_common("Reload data files successfully\n");
507    
508                          log_std("Reload data files successfully\n");                          // Load section config and gen_ex
509                          sd_notify(0, "READY=1");                          if (load_section_config_from_db(1) < 0)
510                  }                          {
511                                    log_error("load_section_config_from_db(1) error\n");
512                  if (SYS_section_list_reload && !SYS_server_exit)                          }
513                  {                          else
                         SYS_section_list_reload = 0;  
   
                         if (section_list_loader_reload() < 0)  
514                          {                          {
515                                  log_error("ksection_list_loader_reload() failed\n");                                  log_common("Reload section config and gen_ex successfully\n");
516                          }                          }
517    
518                            sd_notify(0, "READY=1");
519                  }                  }
520    
521                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second
# Line 245  int net_server(const char *hostaddr, in_ Line 538  int net_server(const char *hostaddr, in_
538    
539                  for (int i = 0; i < nfds; i++)                  for (int i = 0; i < nfds; i++)
540                  {                  {
541                          if (events[i].data.fd == socket_server)                          if (events[i].data.fd == socket_server[0] || events[i].data.fd == socket_server[1])
542                          {                          {
543                                    SSH_v2 = (events[i].data.fd == socket_server[1] ? 1 : 0);
544    
545                                  while (!SYS_server_exit) // Accept all incoming connections until error                                  while (!SYS_server_exit) // Accept all incoming connections until error
546                                  {                                  {
547                                          socket_client = accept(socket_server, (struct sockaddr *)&sin, &namelen);                                          addrlen = sizeof(sin);
548                                            socket_client = accept(socket_server[SSH_v2], (struct sockaddr *)&sin, &addrlen);
549                                          if (socket_client < 0)                                          if (socket_client < 0)
550                                          {                                          {
551                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)
# Line 272  int net_server(const char *hostaddr, in_ Line 568  int net_server(const char *hostaddr, in_
568    
569                                          port_client = ntohs(sin.sin_port);                                          port_client = ntohs(sin.sin_port);
570    
571                                          log_std("Accept connection from %s:%d\n", hostaddr_client, port_client);                                          log_common("Accept %s connection from %s:%d\n", (SSH_v2 ? "SSH" : "telnet"), hostaddr_client, port_client);
572    
573                                            if (SYS_child_process_count - 1 < BBS_max_client)
574                                            {
575                                                    j = 0;
576                                                    for (i = 0; i < BBS_max_client; i++)
577                                                    {
578                                                            if (process_sockaddr_pool[i].pid != 0 && process_sockaddr_pool[i].s_addr == sin.sin_addr.s_addr)
579                                                            {
580                                                                    j++;
581                                                                    if (j >= BBS_max_client_per_ip)
582                                                                    {
583                                                                            log_common("Too many client connections (%d) from %s\n", j, hostaddr_client);
584                                                                            break;
585                                                                    }
586                                                            }
587                                                    }
588    
589                                          if (fork_server() < 0)                                                  if (j < BBS_max_client_per_ip)
590                                                    {
591                                                            if ((pid = fork_server()) < 0)
592                                                            {
593                                                                    log_error("fork_server() error\n");
594                                                            }
595                                                            else if (pid > 0)
596                                                            {
597                                                                    i = 0;
598                                                                    for (; i < BBS_max_client; i++)
599                                                                    {
600                                                                            if (process_sockaddr_pool[i].pid == 0)
601                                                                            {
602                                                                                    break;
603                                                                            }
604                                                                    }
605    
606                                                                    if (i >= BBS_max_client)
607                                                                    {
608                                                                            log_error("Process sockaddr pool depleted\n");
609                                                                    }
610                                                                    else
611                                                                    {
612                                                                            process_sockaddr_pool[i].pid = pid;
613                                                                            process_sockaddr_pool[i].s_addr = sin.sin_addr.s_addr;
614                                                                    }
615                                                            }
616                                                    }
617                                            }
618                                            else
619                                          {                                          {
620                                                  log_error("fork_server() error\n");                                                  log_error("Rejected client connection over limit (%d)\n", SYS_child_process_count - 1);
621                                          }                                          }
622    
623                                          if (close(socket_client) == -1)                                          if (close(socket_client) == -1)
# Line 293  int net_server(const char *hostaddr, in_ Line 634  int net_server(const char *hostaddr, in_
634                  log_error("close(epoll) error (%d)\n");                  log_error("close(epoll) error (%d)\n");
635          }          }
636    
637          fcntl(socket_server, F_SETFL, flags);          for (i = 0; i < 2; i++)
   
         if (close(socket_server) == -1)  
638          {          {
639                  log_error("Close server socket failed\n");                  fcntl(socket_server[i], F_SETFL, flags[i]);
640    
641                    if (close(socket_server[i]) == -1)
642                    {
643                            log_error("Close server socket failed\n");
644                    }
645          }          }
646    
647            ssh_bind_free(sshbind);
648            ssh_finalize();
649    
650          return 0;          return 0;
651  }  }


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

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