/[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.10 by sysadm, Sun Mar 20 17:37:14 2005 UTC Revision 1.51 by sysadm, Thu Jun 5 05:24:56 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /***************************************************************************
2                            net_server.c  -  description                                                    net_server.c  -  description
3                               -------------------                                                           -------------------
4      begin                : Mon Oct 11 2004          Copyright            : (C) 2004-2025 by Leaflet
5      copyright            : (C) 2004 by Leaflet          Email                : leaflet@leafok.com
     email                : leaflet@leafok.com  
6   ***************************************************************************/   ***************************************************************************/
7    
8  /***************************************************************************  /***************************************************************************
9   *                                                                         *   *                                                                         *
10   *   This program is free software; you can redistribute it and/or modify  *   *   This program is free software; you can redistribute it and/or modify  *
11   *   it under the terms of the GNU General Public License as published by  *   *   it under the terms of the GNU General Public License as published by  *
12   *   the Free Software Foundation; either version 2 of the License, or     *   *   the Free Software Foundation; either version 3 of the License, or     *
13   *   (at your option) any later version.                                   *   *   (at your option) any later version.                                   *
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
17    #define _XOPEN_SOURCE 500
18    #define _POSIX_C_SOURCE 200809L
19    #define _GNU_SOURCE
20    
21    #include "net_server.h"
22  #include "common.h"  #include "common.h"
23    #include "log.h"
24  #include "io.h"  #include "io.h"
25    #include "init.h"
26    #include "fork.h"
27    #include "menu.h"
28    #include "file_loader.h"
29    #include "section_list_loader.h"
30    #include <errno.h>
31    #include <fcntl.h>
32    #include <string.h>
33    #include <signal.h>
34    #include <stdlib.h>
35    #include <unistd.h>
36    #include <sys/syscall.h>
37  #include <sys/socket.h>  #include <sys/socket.h>
38  #include <netinet/in.h>  #include <sys/wait.h>
39    #include <sys/epoll.h>
40  #include <arpa/inet.h>  #include <arpa/inet.h>
41    #include <netinet/in.h>
42    #include <systemd/sd-daemon.h>
43    #include <libssh/server.h>
44    
45  int  struct process_sockaddr_t
 net_server (const char *hostaddr, unsigned int port)  
46  {  {
47    int namelen, seq, netint;          pid_t pid;
48    struct sockaddr_in sin;          in_addr_t s_addr;
49    char temp[256];  };
50    typedef struct process_sockaddr_t PROCESS_SOCKADDR;
51    
52    socket_server = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);  static PROCESS_SOCKADDR process_sockaddr_pool[MAX_CLIENT_LIMIT];
53    
54    if (socket_server < 0)  int net_server(const char *hostaddr, in_port_t port)
55      {  {
56        log_error ("Create socket failed\n");          ssh_bind sshbind;
57        exit (1);          unsigned int namelen;
58      }          int ret;
59            int flags;
60            struct sockaddr_in sin;
61            struct epoll_event ev, events[MAX_EVENTS];
62            int nfds, epollfd;
63            siginfo_t siginfo;
64            int sd_notify_stopping = 0;
65            MENU_SET *p_bbs_menu_new;
66            int i, j;
67            pid_t pid;
68            int ssh_log_level = SSH_LOG_NOLOG;
69    
70            ssh_init();
71    
72            sshbind = ssh_bind_new();
73    
74            if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, hostaddr) < 0 ||
75                    ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &port) < 0 ||
76                    ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_KEYFILE) < 0 ||
77                    ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_LOG_VERBOSITY, &ssh_log_level) < 0)
78            {
79                    log_error("Error setting SSH bind options: %s\n", ssh_get_error(sshbind));
80                    ssh_bind_free(sshbind);
81                    return -1;
82            }
83            
84            socket_server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
85    
86    sin.sin_family = AF_INET;          if (socket_server < 0)
87    sin.sin_addr.s_addr =          {
88      (strlen (hostaddr) > 0 ? inet_addr (hostaddr) : INADDR_ANY);                  log_error("Create socket failed\n");
89    sin.sin_port = htons (port);                  return -1;
90            }
91    
92    if (bind (socket_server, (struct sockaddr *) &sin, sizeof (sin)) < 0)          sin.sin_family = AF_INET;
93      {          sin.sin_addr.s_addr = (hostaddr[0] != '\0' ? inet_addr(hostaddr) : INADDR_ANY);
94        log_error ("Bind address %s:%u failed\n",          sin.sin_port = htons(port);
95                   inet_ntoa (sin.sin_addr), ntohs (sin.sin_port));  
96        exit (2);          // Reuse address and port
97      }          flags = 1;
98            if (setsockopt(socket_server, SOL_SOCKET, SO_REUSEADDR, &flags, sizeof(flags)) < 0)
99            {
100                    log_error("setsockopt SO_REUSEADDR error (%d)\n", errno);
101            }
102            if (setsockopt(socket_server, SOL_SOCKET, SO_REUSEPORT, &flags, sizeof(flags)) < 0)
103            {
104                    log_error("setsockopt SO_REUSEPORT error (%d)\n", errno);
105            }
106    
107    if (listen (socket_server, 10) < 0)          if (bind(socket_server, (struct sockaddr *)&sin, sizeof(sin)) < 0)
108      {          {
109        log_error ("Socket listen failed\n");                  log_error("Bind address %s:%u failed (%d)\n",
110        exit (3);                                    inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), errno);
111      }                  return -1;
112            }
113    
114            if (listen(socket_server, 10) < 0)
115            {
116                    log_error("Socket listen failed (%d)\n", errno);
117                    return -1;
118            }
119    
120            strncpy(hostaddr_server, inet_ntoa(sin.sin_addr), sizeof(hostaddr_server) - 1);
121            hostaddr_server[sizeof(hostaddr_server) - 1] = '\0';
122    
123            port_server = ntohs(sin.sin_port);
124            namelen = sizeof(sin);
125    
126    strcpy (hostaddr_server, inet_ntoa (sin.sin_addr));          log_common("Listening at %s:%d\n", hostaddr_server, port_server);
   port_server = ntohs (sin.sin_port);  
127    
128    log_std ("Listening at %s:%d\n", hostaddr_server, port_server);          epollfd = epoll_create1(0);
129            if (epollfd < 0)
130            {
131                    log_error("epoll_create1() error (%d)\n", errno);
132                    return -1;
133            }
134    
135    namelen = sizeof (sin);          ev.events = EPOLLIN;
136    while (1)          ev.data.fd = socket_server;
137      {          if (epoll_ctl(epollfd, EPOLL_CTL_ADD, socket_server, &ev) == -1)
       if ((socket_client =  
            accept (socket_server, (struct sockaddr *) &sin, &namelen)) < 0)  
138          {          {
139            log_error ("Accept connection error\n");                  log_error("epoll_ctl(socket_server) error (%d)\n", errno);
140            continue;                  if (close(epollfd) < 0)
141                    {
142                            log_error("close(epoll) error (%d)\n");
143                    }
144                    return -1;
145          }          }
146    
147        strcpy (hostaddr_client, (const char *) inet_ntoa (sin.sin_addr));          flags = fcntl(socket_server, F_GETFL, 0);
148        port_client = ntohs (sin.sin_port);          fcntl(socket_server, F_SETFL, flags | O_NONBLOCK);
149    
150            // Startup complete
151            sd_notifyf(0, "READY=1\n"
152                                      "STATUS=Listening at %s:%d\n"
153                                      "MAINPID=%d",
154                               hostaddr_server, port_server, getpid());
155    
156        log_std ("Accept connection from %s:%d\n", hostaddr_client,          while (!SYS_server_exit || SYS_child_process_count > 0)
157                 port_client);          {
158                    if (SYS_server_exit && !sd_notify_stopping)
159                    {
160                            sd_notify(0, "STOPPING=1");
161                            sd_notify_stopping = 1;
162                    }
163    
164                    while ((SYS_child_exit || SYS_server_exit) && SYS_child_process_count > 0)
165                    {
166                            SYS_child_exit = 0;
167    
168                            siginfo.si_pid = 0;
169                            ret = waitid(P_ALL, 0, &siginfo, WEXITED | WNOHANG);
170                            if (ret == 0 && siginfo.si_pid > 0)
171                            {
172                                    SYS_child_exit = 1; // Retry waitid
173    
174                                    SYS_child_process_count--;
175                                    log_common("Child process (%d) exited\n", siginfo.si_pid);
176    
177                                    if (siginfo.si_pid != section_list_loader_pid)
178                                    {
179                                            i = 0;
180                                            for (; i < BBS_max_client; i++)
181                                            {
182                                                    if (process_sockaddr_pool[i].pid == siginfo.si_pid)
183                                                    {
184                                                            process_sockaddr_pool[i].pid = 0;
185                                                            break;
186                                                    }
187                                            }
188                                            if (i >= BBS_max_client)
189                                            {
190                                                    log_error("Child process (%d) not found in process sockaddr pool\n", siginfo.si_pid);
191                                            }
192                                    }
193                            }
194                            else if (ret == 0)
195                            {
196                                    break;
197                            }
198                            else if (ret < 0)
199                            {
200                                    log_error("Error in waitid: %d\n", errno);
201                                    break;
202                            }
203                    }
204    
205                    if (SYS_server_exit && !SYS_child_exit && SYS_child_process_count > 0)
206                    {
207                            log_common("Notify %d child process to exit\n", SYS_child_process_count);
208                            if (kill(0, SIGTERM) < 0)
209                            {
210                                    log_error("Send SIGTERM signal failed (%d)\n", errno);
211                            }
212    
213                            sd_notifyf(0, "STATUS=Waiting for %d child process to exit", SYS_child_process_count);
214                    }
215    
216                    if (SYS_conf_reload && !SYS_server_exit)
217                    {
218                            SYS_conf_reload = 0;
219                            sd_notify(0, "RELOADING=1");
220    
221                            // Reload configuration
222                            if (load_conf(CONF_BBSD) < 0)
223                            {
224                                    log_error("Reload conf failed\n");
225                            }
226    
227                            p_bbs_menu_new = calloc(1, sizeof(MENU_SET));
228                            if (p_bbs_menu_new == NULL)
229                            {
230                                    log_error("OOM: calloc(MENU_SET)\n");
231                            }
232                            else if (load_menu(p_bbs_menu_new, CONF_MENU) < 0)
233                            {
234                                    unload_menu(p_bbs_menu_new);
235                                    free(p_bbs_menu_new);
236                                    p_bbs_menu_new = NULL;
237    
238                                    log_error("Reload menu failed\n");
239                            }
240                            else
241                            {
242                                    unload_menu(p_bbs_menu);
243                                    free(p_bbs_menu);
244    
245                                    p_bbs_menu = p_bbs_menu_new;
246                                    p_bbs_menu_new = NULL;
247    
248                                    log_common("Reload menu successfully\n");
249                            }
250    
251                            sd_notify(0, "READY=1");
252                    }
253    
254                    if (SYS_data_file_reload && !SYS_server_exit)
255                    {
256                            SYS_data_file_reload = 0;
257                            sd_notify(0, "RELOADING=1");
258    
259                            for (int i = 0; i < data_files_load_startup_count; i++)
260                            {
261                                    if (load_file(data_files_load_startup[i]) < 0)
262                                    {
263                                            log_error("load_file_mmap(%s) error\n", data_files_load_startup[i]);
264                                    }
265                            }
266    
267                            log_common("Reload data files successfully\n");
268                            sd_notify(0, "READY=1");
269                    }
270    
271                    if (SYS_section_list_reload && !SYS_server_exit)
272                    {
273                            SYS_section_list_reload = 0;
274    
275                            if (section_list_loader_reload() < 0)
276                            {
277                                    log_error("ksection_list_loader_reload() failed\n");
278                            }
279                    }
280    
281                    nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second
282    
283                    if (nfds < 0)
284                    {
285                            if (errno != EINTR)
286                            {
287                                    log_error("epoll_wait() error (%d)\n", errno);
288                                    break;
289                            }
290                            continue;
291                    }
292    
293                    // Stop accept new connection on exit
294                    if (SYS_server_exit)
295                    {
296                            continue;
297                    }
298    
299                    for (int i = 0; i < nfds; i++)
300                    {
301                            if (events[i].data.fd == socket_server)
302                            {
303                                    while (!SYS_server_exit) // Accept all incoming connections until error
304                                    {
305                                            socket_client = accept(socket_server, (struct sockaddr *)&sin, &namelen);
306                                            if (socket_client < 0)
307                                            {
308                                                    if (errno == EAGAIN || errno == EWOULDBLOCK)
309                                                    {
310                                                            break;
311                                                    }
312                                                    else if (errno == EINTR)
313                                                    {
314                                                            continue;
315                                                    }
316                                                    else
317                                                    {
318                                                            log_error("accept(socket_server) error (%d)\n", errno);
319                                                            break;
320                                                    }
321                                            }
322    
323                                            strncpy(hostaddr_client, inet_ntoa(sin.sin_addr), sizeof(hostaddr_client) - 1);
324                                            hostaddr_client[sizeof(hostaddr_client) - 1] = '\0';
325    
326                                            port_client = ntohs(sin.sin_port);
327    
328                                            log_common("Accept connection from %s:%d\n", hostaddr_client, port_client);
329    
330                                            if (SYS_child_process_count - 1 < BBS_max_client)
331                                            {
332                                                    j = 0;
333                                                    for (i = 0; i < BBS_max_client; i++)
334                                                    {
335                                                            if (process_sockaddr_pool[i].pid != 0 && process_sockaddr_pool[i].s_addr == sin.sin_addr.s_addr)
336                                                            {
337                                                                    j++;
338                                                                    if (j >= BBS_max_client_per_ip)
339                                                                    {
340                                                                            log_common("Too many client connections (%d) from %s\n", j, hostaddr_client);
341                                                                            break;
342                                                                    }
343                                                            }
344                                                    }
345    
346                                                    if (j < BBS_max_client_per_ip)
347                                                    {
348                                                            if ((pid = fork_server(sshbind)) < 0)
349                                                            {
350                                                                    log_error("fork_server() error\n");
351                                                            }
352                                                            else if (pid > 0)
353                                                            {
354                                                                    i = 0;
355                                                                    for (; i < BBS_max_client; i++)
356                                                                    {
357                                                                            if (process_sockaddr_pool[i].pid == 0)
358                                                                            {
359                                                                                    break;
360                                                                            }
361                                                                    }
362    
363                                                                    if (i >= BBS_max_client)
364                                                                    {
365                                                                            log_error("Process sockaddr pool depleted\n");
366                                                                    }
367                                                                    else
368                                                                    {
369                                                                            process_sockaddr_pool[i].pid = pid;
370                                                                            process_sockaddr_pool[i].s_addr = sin.sin_addr.s_addr;
371                                                                    }
372                                                            }
373                                                    }
374                                            }
375                                            else
376                                            {
377                                                    log_error("Rejected client connection over limit (%d)\n", SYS_child_process_count - 1);
378                                            }
379    
380                                            if (close(socket_client) == -1)
381                                            {
382                                                    log_error("close(socket_lient) error (%d)\n", errno);
383                                            }
384                                    }
385                            }
386                    }
387            }
388    
389        if (fork_server () < 0)          if (close(epollfd) < 0)
390          {          {
391            log_error ("Fork error\n");                  log_error("close(epoll) error (%d)\n");
392          }          }
393    
394        if (close (socket_client) == -1)          fcntl(socket_server, F_SETFL, flags);
395    
396            if (close(socket_server) == -1)
397          {          {
398            log_error ("Close client socket failed\n");                  log_error("Close server socket failed\n");
399          }          }
     }  
400    
401    if (close (socket_server) == -1)          ssh_bind_free(sshbind);
402      {          ssh_finalize();
       log_error ("Close server socket failed\n");  
     }  
403    
404    return 0;          return 0;
405  }  }


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

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