Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_network_ssl.c
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 */
19
27
28#include "nilorea/n_list.h"
29#include "nilorea/n_str.h"
30#include "nilorea/n_log.h"
31#include "nilorea/n_network.h"
33#include "nilorea/n_signals.h"
34
35#include <sys/stat.h>
36
37char* port = NULL;
38char* addr = NULL;
39char* key = NULL;
40char* cert = NULL;
41char* ca_file = NULL;
42char* root_dir = NULL;
43LIST* routes = NULL;
46int ssl_verify = 0;
47int max_connections = 0; /* 0 = unlimited */
48/* Seconds to wait for a connection before giving up, 0 waits forever.
49 * Without it a server whose client never showed up sat in accept()
50 * until the test harness killed it, reported as a bare exit 124. */
52volatile sig_atomic_t done = 0;
53/* set by the SIGTERM handler so the exit status still reports the kill */
54volatile sig_atomic_t killed_by_signal = 0;
55
56NETWORK *server = NULL,
57 *netw = NULL;
58
59void usage(void) {
60 fprintf(stderr,
61 " -p 'port' : set the https server port\n"
62 " -k 'key file' : SSL key file path\n"
63 " -c 'cert file' : SSL certificate file path\n"
64 " -A 'CA file' : optional, CA certificate file for chain verification\n"
65 " -e : optional, enable SSL peer certificate verification\n"
66 " -a 'address name/ip' : optional, specify where to bind interface\n"
67 " -i 'ipmode' : optional, force 'ipv4' or 'ipv6', default supports both\n"
68 " -s 'size' : optional, maximum http request size (default: %d)\n"
69 " -n 'count' : optional, exit after count connections (0 = unlimited, default)\n"
70 " -w 'seconds' : optional, give up after seconds without a connection (0 = wait forever, default: 30)\n"
71 " -d 'html root' : optional, specify a different http root dir (default: ./DATAS/)\n"
72 " -v : version\n"
73 " -h : help\n"
74 " -V 'log level' : optional, set the log level (default: LOG_ERR)\n",
76}
77
78void process_args(int argc_nb, char** argv_ptr, char** addr_ptr, char** port_ptr, char** key_ptr, char** cert_ptr, char** ca_file_ptr, int* ssl_verify_ptr, LIST* routes_ptr, int* ip_version_ptr, int* max_http_request_size_ptr, char** root_dir_ptr) {
79 int getoptret = 0,
80 log_level = LOG_ERR; /* default log level */
81
82 if (argc_nb == 1) {
83 fprintf(stderr, "No arguments given, help:\n");
84 usage();
85 exit(1);
86 }
87 while ((getoptret = getopt(argc_nb, argv_ptr, "hven:s:V:p:i:a:r:k:c:s:d:A:w:")) != EOF) {
88 switch (getoptret) {
89 case 'n':
90 max_connections = atoi(optarg);
91 break;
92 case 'w':
93 accept_timeout = atoi(optarg);
94 break;
95 case 'i':
96 if (!strcmp("v4", optarg)) {
97 (*ip_version_ptr) = NETWORK_IPV4;
98 n_log(LOG_NOTICE, "IPV4 selected");
99 } else if (!strcmp("v6", optarg)) {
100 (*ip_version_ptr) = NETWORK_IPV6;
101 n_log(LOG_NOTICE, "IPV6 selected");
102 } else {
103 n_log(LOG_NOTICE, "IPV4/6 selected");
104 }
105 break;
106 case 'v':
107 fprintf(stderr, "Date de compilation : %s a %s.\n", __DATE__, __TIME__);
108 exit(1);
109 case 'V':
110 if (!strncmp("LOG_NULL", optarg, 8)) {
112 } else {
113 if (!strncmp("LOG_NOTICE", optarg, 10)) {
115 } else {
116 if (!strncmp("LOG_INFO", optarg, 8)) {
118 } else {
119 if (!strncmp("LOG_ERR", optarg, 7)) {
121 } else {
122 if (!strncmp("LOG_DEBUG", optarg, 9)) {
124 } else {
125 fprintf(stderr, "%s n'est pas un niveau de log valide.\n", optarg);
126 exit(-1);
127 }
128 }
129 }
130 }
131 }
132 break;
133 case 'e':
134 (*ssl_verify_ptr) = 1;
135 break;
136 case 'p':
137 (*port_ptr) = strdup(optarg);
138 break;
139 case 'r':
140 list_push(routes_ptr, strdup(optarg), &free);
141 break;
142 case 'a':
143 (*addr_ptr) = strdup(optarg);
144 break;
145 case 'k':
146 (*key_ptr) = strdup(optarg);
147 break;
148 case 'c':
149 (*cert_ptr) = strdup(optarg);
150 break;
151 case 'A':
152 (*ca_file_ptr) = strdup(optarg);
153 break;
154 case 's':
155 (*max_http_request_size_ptr) = atoi(optarg);
156 break;
157 case 'd':
158 (*root_dir_ptr) = strdup(optarg);
159 break;
160 default:
161 case '?': {
162 if (optopt == 'd') {
163 fprintf(stderr, "\n Missing html root directory\n");
164 }
165 if (optopt == 's') {
166 fprintf(stderr, "\n Missing max http size string\n");
167 }
168 if (optopt == 'k') {
169 fprintf(stderr, "\n Missing key file string\n");
170 }
171 if (optopt == 'c') {
172 fprintf(stderr, "\n Missing certificate file string\n");
173 }
174 if (optopt == 'A') {
175 fprintf(stderr, "\n Missing CA file string\n");
176 }
177 if (optopt == 'r') {
178 fprintf(stderr, "\n Missing route string\n");
179 }
180 if (optopt == 'a') {
181 fprintf(stderr, "\n Missing binding host/addr string\n");
182 }
183 if (optopt == 'i') {
184 fprintf(stderr, "\n Missing ip version (v4 or v6) string \n");
185 } else if (optopt == 'V') {
186 fprintf(stderr, "\n Missing log level string\n");
187 } else if (optopt == 'p') {
188 fprintf(stderr, "\n Missing port\n");
189 } else if (optopt != 's') {
190 fprintf(stderr, "\n Unknow missing option %c\n", optopt);
191 }
192 usage();
193 exit(1);
194 }
195 case 'h': {
196 usage();
197 exit(1);
198 }
199 }
200 }
202} /* void process_args( ... ) */
203
204/* Exit handling */
205void action_on_sig(int recvd_signal) {
206 (void)recvd_signal;
207#ifndef __windows__
208 static int nb_sigterm = 0;
209 switch (recvd_signal) {
210 /* We should not use these signals as they make the debugging going funky */
211 case (SIGABRT):
212 n_log(LOG_ERR, "Caught SIGABRT !");
213 break;
214 case (SIGINT):
215 n_log(LOG_ERR, "Caught SIGINT !");
216 break;
217 case (SIGBUS):
218 n_log(LOG_ERR, "Caught SIGBUS !");
219 break;
220 case (SIGFPE):
221 n_log(LOG_ERR, "Caught SIGFPE !");
222 break;
223 case (SIGSEGV):
224 n_log(LOG_ERR, "Caught SIGSEGV !");
225 break;
226 case (SIGSYS):
227 n_log(LOG_ERR, "Caught SIGSYS !");
228 break;
229 case (SIGTERM):
230 nb_sigterm++;
231 if (nb_sigterm >= 2) {
232 /* the accept loop did not pick the flag up, leave without
233 * unwinding: exit() would run OpenSSL's atexit cleanup and
234 * free() is not async-signal-safe */
235 _exit(-1);
236 }
237 /* no n_log() here either, just raise the flag and let the accept
238 * loop report it and run the normal teardown */
240 done = TRUE;
241 break;
242 case (SIGUSR1):
243 done = TRUE;
244 n_log(LOG_ERR, "Caught SIGUSR1 !");
245 break;
246 case (SIGUSR2):
247 done = TRUE;
248 n_log(LOG_ERR, "Caught SIGUSR2 !");
249 break;
250 case (SIGHUP):
251 n_log(LOG_NOTICE, "Caught SIGHUP !");
252 break;
253 default:
254 n_log(LOG_ERR, "Caught unknow signal %d", recvd_signal);
255 break;
256 }
257#endif
258} /* action_on_sig() */
259
260// Function to handle different URLs and return appropriate responses
261void handle_request(NETWORK* netw_ptr, LIST* routes_ptr) {
262 __n_assert(netw_ptr, return);
263 __n_assert(routes_ptr, return);
264
265 bool found = 0;
266 char** split_results = NULL;
267 char* http_url = NULL;
268 N_STR* dynamic_request_answer = NULL;
269
270 /* the caller (ssl_network_thread) owns netw_ptr and closes it, closing it
271 * here too would leave it with a dangling pointer to close a second time */
272 __n_assert(netw_ptr->ssl, n_log(LOG_ERR, "no SSL session on socket %d", netw_ptr->link.sock); return);
273
274 // Read request
275 char* http_buffer = NULL;
276 Alloca(http_buffer, (size_t)(max_http_request_size + 1));
277 __n_assert(http_buffer, return);
278
279 // SSL_read reads up to max_http_request_size bytes (variable-length HTTP request).
280 // recv_ssl_data is intentionally NOT used here: it loops until the entire buffer is
281 // full, which would block indefinitely since HTTP requests are smaller than the buffer.
282 int ssl_read_ret = SSL_read(netw_ptr->ssl, http_buffer, max_http_request_size);
283 if (ssl_read_ret <= 0) {
284 int ssl_error = SSL_get_error(netw_ptr->ssl, ssl_read_ret);
285 if (ssl_error == SSL_ERROR_ZERO_RETURN) {
286 n_log(LOG_INFO, "SSL_read: peer closed the connection cleanly");
287 } else {
288 n_log(LOG_ERR, "SSL_read failed with SSL error %d", ssl_error);
289 }
290 return;
291 }
292 // n_log( LOG_DEBUG , "http_request: %s" , http_buffer );
293
294 // Extract URL from the request
295 char url[4096] = "";
296 netw_get_url_from_http_request(http_buffer, url, sizeof(url));
297 n_log(LOG_DEBUG, "url: %s", url);
298
299 // Handle the request based on the URL
300 N_STR* origin = new_nstr(32);
301 nstrprintf(origin, "%s:" SOCKET_SIZE_FORMAT, _str(netw_ptr->link.ip), netw_ptr->link.sock);
302
303 NETWORK_HTTP_INFO http_request = netw_extract_http_info(http_buffer);
304 N_STR* http_body = NULL;
305
306 split_results = split(url, "?", 0);
307 if (!split_results || !split_results[0]) {
308 http_body = char_to_nstr("<html><body><h1>Bad Request</h1></body></html>");
309 if (netw_build_http_response(&dynamic_request_answer, 400, "ex_network_ssl server", netw_guess_http_content_type(url), "", http_body) == FALSE) {
310 n_log(LOG_ERR, "couldn't build a Bad Request answer for %s", url);
311 }
312 n_log(LOG_ERR, "%s: %s %s 400", _nstr(origin), http_request.type, url);
313 } else {
314 http_url = split_results[0];
315 n_log(LOG_INFO, "%s: %s %s request...", _nstr(origin), http_request.type, url);
316 if (strcmp("OPTIONS", http_request.type) == 0) {
317 if (netw_build_http_response(&dynamic_request_answer, 200, "ex_network_ssl server", netw_guess_http_content_type(url), "Allow: OPTIONS, GET, POST\r\n", NULL) == FALSE) {
318 n_log(LOG_ERR, "couldn't build an OPTION answer for %s", url);
319 }
320 n_log(LOG_INFO, "%s: %s %s 200", _nstr(origin), http_request.type, url);
321 } else if (strcmp("GET", http_request.type) == 0) {
322 char system_url[4096] = "";
323 // example assume a root dir at DATAS
324 if (!root_dir) {
325 snprintf(system_url, sizeof(system_url), "./DATAS%s", http_url);
326 } else {
327 snprintf(system_url, sizeof(system_url), "%s%s", root_dir, http_url);
328 }
329
330 n_log(LOG_DEBUG, "%s: searching for file %s...", _nstr(origin), system_url);
331
332 struct stat st;
333 if (stat(system_url, &st) == 0 && S_ISREG(st.st_mode)) {
334 n_log(LOG_DEBUG, "%s: file %s found !", _nstr(origin), system_url);
335 http_body = file_to_nstr(system_url);
336 if (!http_body) {
337 http_body = char_to_nstr("<html><body><h1>Internal Server Error</h1></body></html>");
338 if (netw_build_http_response(&dynamic_request_answer, 500, "ex_network_ssl server", netw_guess_http_content_type(url), "", http_body) == FALSE) {
339 n_log(LOG_ERR, "couldn't build an Internal Server Error answer for %s", url);
340 }
341 n_log(LOG_ERR, "%s: %s %s 500", _nstr(origin), http_request.type, url);
342 } else {
343 if (netw_build_http_response(&dynamic_request_answer, 200, "ex_network_ssl server", netw_guess_http_content_type(url), "", http_body) == FALSE) {
344 n_log(LOG_ERR, "couldn't build an http answer for %s", url);
345 }
346 n_log(LOG_INFO, "%s: %s %s 200", _nstr(origin), http_request.type, url);
347 }
348 } else if (stat(system_url, &st) == 0 && S_ISDIR(st.st_mode)) {
349 /* Directory: check for index.html and redirect */
350 char index_path[4096 + 16] = "";
351 size_t url_len = strlen(http_url);
352 const char* slash = (url_len > 0 && http_url[url_len - 1] == '/') ? "" : "/";
353 snprintf(index_path, sizeof(index_path), "%s%sindex.html", system_url, slash);
354 struct stat idx_st;
355 if (stat(index_path, &idx_st) == 0 && S_ISREG(idx_st.st_mode)) {
356 char location_header[4096] = "";
357 snprintf(location_header, sizeof(location_header), "Location: %s%sindex.html\r\n", http_url, slash);
358 if (netw_build_http_response(&dynamic_request_answer, 301, "ex_network_ssl server", "text/html", location_header, NULL) == FALSE) {
359 n_log(LOG_ERR, "couldn't build a redirect answer for %s", url);
360 }
361 n_log(LOG_INFO, "%s: %s %s 301 -> %s%sindex.html", _nstr(origin), http_request.type, url, http_url, slash);
362 } else {
363 http_body = char_to_nstr("<html><body><h1>404 Not Found</h1></body></html>");
364 if (netw_build_http_response(&dynamic_request_answer, 404, "ex_network_ssl server", netw_guess_http_content_type(url), "", http_body) == FALSE) {
365 n_log(LOG_ERR, "couldn't build a NOT FOUND answer for %s", url);
366 }
367 n_log(LOG_ERR, "%s: %s %s 404", _nstr(origin), http_request.type, url);
368 }
369 } else {
370 http_body = char_to_nstr("<html><body><h1>404 Not Found</h1></body></html>");
371 if (netw_build_http_response(&dynamic_request_answer, 404, "ex_network_ssl server", netw_guess_http_content_type(url), "", http_body) == FALSE) {
372 n_log(LOG_ERR, "couldn't build a NOT FOUND answer for %s", url);
373 }
374 n_log(LOG_ERR, "%s: %s %s 404", _nstr(origin), http_request.type, url);
375 }
376 } else if (strcmp("POST", http_request.type) == 0) {
377 // Parse virtual route
378 found = 0;
379 list_foreach(node, routes_ptr) {
380 if (strcmp(node->ptr, http_url) == 0) {
381 // Handle 200 OK from virtual route
382 HASH_TABLE* post_data = netw_parse_post_data(http_request.body);
383 if (post_data) {
384 HT_FOREACH(hnode, post_data,
385 {
386 n_log(LOG_DEBUG, "%s: POST DATA: %s=%s", _nstr(origin), hnode->key, (char*)hnode->data.ptr);
387 });
388 destroy_ht(&post_data);
389 }
390 http_body = char_to_nstr("{\"status\":\"ok\"}");
391 if (netw_build_http_response(&dynamic_request_answer, 200, "ex_network_ssl server", "application/json", "", http_body) == FALSE) {
392 n_log(LOG_ERR, "couldn't build a route 200 answer for %s", url);
393 }
394 found = 1;
395 n_log(LOG_INFO, "%s: %s virtual:%s 200", _nstr(origin), http_request.type, url);
396 break;
397 }
398 }
399 if (!found) {
400 http_body = char_to_nstr("<html><body><h1>404 Not Found</h1></body></html>");
401 if (netw_build_http_response(&dynamic_request_answer, 404, "ex_network_ssl server", netw_guess_http_content_type(url), "", http_body) == FALSE) {
402 n_log(LOG_ERR, "couldn't build a NOT FOUND answer for %s", url);
403 }
404 n_log(LOG_ERR, "%s: %s %s 404", _nstr(origin), http_request.type, url);
405 }
406 } else {
407 http_body = char_to_nstr("<html><body><h1>Bad Request</h1></body></html>");
408 if (netw_build_http_response(&dynamic_request_answer, 400, "ex_network_ssl server", netw_guess_http_content_type(url), "", http_body) == FALSE) {
409 n_log(LOG_ERR, "couldn't build a Bad Request answer for %s", url);
410 }
411 n_log(LOG_ERR, "%s: %s %s 400", _nstr(origin), http_request.type, url);
412 }
413 free_split_result(&split_results);
414 }
415 if (dynamic_request_answer) {
416 if (dynamic_request_answer->written > UINT32_MAX) {
417 n_log(LOG_ERR, "response too large to send for %s: %s %s (size: %zu)", _nstr(origin), http_request.type, url, dynamic_request_answer->written);
418 } else if (send_ssl_data(netw_ptr, dynamic_request_answer->data, (uint32_t)dynamic_request_answer->written) < 0) {
419 n_log(LOG_ERR, "failed to send response for %s: %s %s", _nstr(origin), http_request.type, url);
420 }
421 free_nstr(&dynamic_request_answer);
422 } else {
423 n_log(LOG_ERR, "couldn't build an answer for %s: %s %s", _nstr(origin), http_request.type, url);
424 }
425 netw_info_destroy(http_request);
426 free_nstr(&origin);
427 free_nstr(&http_body);
428} /* handle_request */
429
437
438void* ssl_network_thread(void* params) {
439 __n_assert(params, return NULL);
441 handle_request(ssl_params->netw, ssl_params->routes);
442 netw_close(&ssl_params->netw);
443 Free(ssl_params);
444 return NULL;
445}
446
447int main(int argc, char* argv[]) {
448 int exit_code = 0;
449 int crypto_ok = FALSE;
450 THREAD_POOL* thread_pool = NULL;
452 __n_assert(routes, n_log(LOG_ERR, "could not allocate list !"); exit(1));
453
454 /* processing args and set log_level */
456
457 if (!port) {
458 n_log(LOG_ERR, "No port given. Exiting.");
459 exit_code = 1;
460 goto clean_and_exit;
461 }
462 if (!key) {
463 n_log(LOG_ERR, "No key given. Exiting.");
464 exit_code = 1;
465 goto clean_and_exit;
466 }
467 if (!cert) {
468 n_log(LOG_ERR, "No certificate given. Exiting.");
469 exit_code = 1;
470 goto clean_and_exit;
471 }
472 /*
473 if (routes->nb_items == 0) {
474 n_log(LOG_ERR, "No route given. Exiting.");
475 exit_code = 1;
476 goto clean_and_exit;
477 }
478 */
479
480#ifndef __windows__
481 errno = 0;
482 signal(SIGPIPE, SIG_IGN);
483 /* initializing signal catching */
484 struct sigaction signal_catcher;
485
486 /* quit on sig */
487 signal_catcher.sa_handler = action_on_sig;
488 sigemptyset(&signal_catcher.sa_mask);
489 signal_catcher.sa_flags = 0;
490
491 sigaction(SIGTERM, &signal_catcher, NULL);
492 sigaction(SIGUSR1, &signal_catcher, NULL);
493#endif
494
495 long int cores = get_nb_cpu_cores();
496 int nb_active_threads = (cores > 0) ? (int)cores : 1;
497 int nb_waiting_threads = 10 * nb_active_threads;
498 n_log(LOG_INFO, "Creating a new thread pool of %d active and %d waiting threads", nb_active_threads, nb_waiting_threads);
499 thread_pool = new_thread_pool((size_t)nb_active_threads, (size_t)nb_waiting_threads);
500
501 n_log(LOG_INFO, "Creating listening network for %s:%s %d", _str(addr), _str(port), ip_version);
502 /* create listening network */
503 if (netw_make_listening(&server, addr, port, SOMAXCONN, ip_version) == FALSE) {
504 n_log(LOG_ERR, "Fatal error with network initialization");
505 exit(-1);
506 }
507
508 /* an unreadable or malformed key/cert leaves the listener in plaintext,
509 * which then fails every client handshake, so treat it as fatal here */
510 if (ca_file) {
511 n_log(LOG_INFO, "Using SSL with certificate chain verification (CA: %s)", ca_file);
513 } else {
514 crypto_ok = netw_set_crypto(server, key, cert);
515 }
516 if (crypto_ok == FALSE) {
517 n_log(LOG_ERR, "Could not load key '%s' and certificate '%s'. Exiting.", _str(key), _str(cert));
518 exit_code = 1;
519 goto clean_and_exit;
520 }
521 if (ssl_verify) {
522 n_log(LOG_INFO, "SSL peer certificate verification enabled");
524 }
525 int accepted_count = 0;
526 /* idle watchdog: get_msec() gives the delta since the previous poll, so
527 * accumulating it measures the time since the last accepted connection */
528 N_TIME idle_timer;
529 start_HiTimer(&idle_timer);
530 time_t idle_msec = 0;
531 while (!done) {
532 n_log(LOG_DEBUG, "Waiting on accept...");
533 /* A 500ms accept window rather than a blocking accept: it is the only
534 * way the `done` flag raised by the signal handler is ever seen, and
535 * it lets the watchdog below fire when no client ever shows up
536 * instead of sitting here until the test harness kills us */
537 int return_code = 0;
538 netw = netw_accept_from_ex(server, 0, 0, 500, &return_code);
539 if (!netw) {
540 idle_msec += get_msec(&idle_timer);
541 if (return_code == EINTR) {
542 /* a signal cut select() short. Do not leave here: the loop
543 * condition is what decides whether that signal asked us to
544 * stop, and leaving directly would skip the status below */
545 n_log(LOG_INFO, "accept interrupted by a signal");
546 } else if (return_code != 0 && return_code != EAGAIN && return_code != EWOULDBLOCK) {
547 /* 0 / EAGAIN / EWOULDBLOCK just mean the window expired with
548 * nothing waiting, which is the normal idle case */
549 n_log(LOG_ERR, "error on accept, NULL netw returned !");
550 u_sleep(100000);
551 }
552 if (accept_timeout > 0 && idle_msec > (time_t)accept_timeout * 1000) {
553 n_log(LOG_ERR, "no connection for %d seconds, giving up with %d accepted", accept_timeout, accepted_count);
554 exit_code = 1;
555 break;
556 }
557 continue;
558 }
559 n_log(LOG_INFO, "accepted SSL connection on socket %d", netw->link.sock);
560 NETWORK_SSL_THREAD_PARAMS* netw_ssl_params = NULL;
561 Malloc(netw_ssl_params, NETWORK_SSL_THREAD_PARAMS, 1);
562 netw_ssl_params->netw = netw;
563 netw_ssl_params->routes = routes;
564 if (add_threaded_process(thread_pool, &ssl_network_thread, (void*)netw_ssl_params, DIRECT_PROC) == FALSE) {
565 n_log(LOG_ERR, "Error adding client management to thread pool");
566 }
567 accepted_count++;
568 idle_msec = 0;
569 get_msec(&idle_timer);
570 if (max_connections > 0 && accepted_count >= max_connections) {
571 n_log(LOG_NOTICE, "reached %d connections, exiting", max_connections);
572 break;
573 }
574 }
575 if (killed_by_signal) {
576 n_log(LOG_ERR, "caught SIGTERM, shutting down after %d connections", accepted_count);
577 exit_code = 1;
578 }
579clean_and_exit:
580 if (thread_pool) {
583 }
585 netw_unload();
589 FreeNoLog(key);
593 exit(exit_code);
594}
static void usage(void)
void process_args(int argc, char **argv)
int main(void)
Definition ex_atlas.c:117
static int log_level
THREAD_POOL * thread_pool
Definition ex_fluid.c:76
int getoptret
Definition ex_fluid.c:59
NETWORK * netw
Network for server mode, accepting incomming.
Definition ex_network.c:39
int max_http_request_size
int max_connections
int accept_timeout
char * root_dir
char * ca_file
NETWORK * server
int ssl_verify
int ip_version
LIST * routes
void * ssl_network_thread(void *params)
volatile sig_atomic_t done
char * addr
char * cert
void action_on_sig(int recvd_signal)
volatile sig_atomic_t killed_by_signal
void handle_request(NETWORK *netw_ptr, LIST *routes_ptr)
char * port
LIST * routes
virtual routes for the server
NETWORK * netw
network to use for the receiving thread
structure of a NETWORK_SSL_THREAD_PARAMS
volatile char key[227]
TRUE while the key is held.
#define FreeNoLog(__ptr)
Free Handler without log.
Definition n_common.h:272
#define Malloc(__ptr, __struct, __size)
Malloc Handler to get errors and set to 0.
Definition n_common.h:204
#define __n_assert(__ptr, __ret)
macro to assert things
Definition n_common.h:279
#define _str(__PTR)
define true
Definition n_common.h:193
#define Alloca(__ptr, __size)
Malloca Handler to get errors and set to 0.
Definition n_common.h:216
#define Free(__ptr)
Free Handler to get errors.
Definition n_common.h:263
#define _nstr(__PTR)
N_STR or "NULL" string for logging purposes.
Definition n_common.h:199
int destroy_ht(HASH_TABLE **table)
empty a table and destroy it
Definition n_hash.c:2349
#define HT_FOREACH(__ITEM_, __HASH_,...)
ForEach macro helper.
Definition n_hash.h:216
structure of a hash table
Definition n_hash.h:138
int list_push(LIST *list, void *ptr, void(*destructor)(void *ptr))
Add a pointer to the end of the list.
Definition n_list.c:228
#define list_foreach(__ITEM_, __LIST_)
ForEach macro helper, safe for node removal during iteration.
Definition n_list.h:89
int list_destroy(LIST **list)
Empty and Free a list container.
Definition n_list.c:548
LIST * new_generic_list(size_t max_items)
Initialiaze a generic list container to max_items pointers.
Definition n_list.c:37
#define MAX_LIST_ITEMS
flag to pass to new_generic_list for the maximum possible number of item in a list
Definition n_list.h:75
Structure of a generic LIST container.
Definition n_list.h:59
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:113
#define LOG_DEBUG
debug-level messages
Definition n_log.h:108
#define LOG_ERR
error conditions
Definition n_log.h:100
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL ).
Definition n_log.c:123
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:104
#define LOG_NULL
no log output
Definition n_log.h:70
#define LOG_INFO
informational
Definition n_log.h:106
size_t written
number of meaningful bytes in data, excluding the null terminator; the size including the null termin...
Definition n_str.h:68
char * data
the string
Definition n_str.h:63
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
Definition n_str.h:203
N_STR * char_to_nstr(const char *src)
Convert a char into a N_STR, short version.
Definition n_str.c:262
N_STR * new_nstr(NSTRBYTE size)
create a new N_STR string
Definition n_str.c:209
#define nstrprintf(__nstr_var, __format,...)
Macro to quickly allocate and sprintf to N_STR.
Definition n_str.h:117
char ** split(const char *str, const char *delim, int empty)
split the strings into a an array of char *pointer , ended by a NULL one.
Definition n_str.c:927
N_STR * file_to_nstr(char *filename)
Load a whole file into a N_STR.
Definition n_str.c:295
int free_split_result(char ***tab)
Free a split result allocated array.
Definition n_str.c:1023
A box including a string and his lenght.
Definition n_str.h:61
void u_sleep(unsigned int usec)
wrapper around usleep for API consistency
Definition n_time.c:55
int start_HiTimer(N_TIME *timer)
Initialize or restart from zero any N_TIME HiTimer.
Definition n_time.c:86
time_t get_msec(N_TIME *timer)
Poll any N_TIME HiTimer, returning msec, and moving currentTime to startTime.
Definition n_time.c:129
Timing Structure.
Definition n_time.h:49
char * ip
ip of the connected socket
Definition n_network.h:358
N_SOCKET link
networking socket
Definition n_network.h:454
char * type
Type of request.
Definition n_network.h:643
char * body
Pointer to the body data.
Definition n_network.h:641
SOCKET sock
a normal socket
Definition n_network.h:356
SSL * ssl
SSL handle.
Definition n_network.h:446
ssize_t send_ssl_data(void *netw, char *buf, uint32_t n)
send data onto the socket
Definition n_network.c:5172
int netw_ssl_set_verify(NETWORK *netw, int enable)
enable or disable SSL peer certificate verification
Definition n_network.c:2162
int netw_get_url_from_http_request(const char *request, char *url, size_t size)
Helper function to extract the URL from the HTTP request line.
Definition n_network.c:6250
int netw_set_crypto(NETWORK *netw, char *key, char *certificate)
activate SSL encryption on selected network, using key and certificate
Definition n_network.c:1489
#define NETWORK_IPV6
Flag to force IPV6.
Definition n_network.h:52
int netw_make_listening(NETWORK **netw, char *addr, char *port, int nbpending, int ip_version)
Make a NETWORK be a Listening network.
Definition n_network.c:3602
#define NETWORK_IPV4
Flag to force IPV4.
Definition n_network.h:50
int netw_build_http_response(N_STR **http_response, int status_code, const char *server_name, const char *content_type, char *additional_headers, N_STR *body)
function to dynamically generate an HTTP response
Definition n_network.c:6897
#define SOCKET_SIZE_FORMAT
socket associated printf style
Definition n_network.h:113
NETWORK * netw_accept_from_ex(NETWORK *from, size_t send_list_limit, size_t recv_list_limit, int blocking, int *retval)
make a normal 'accept' .
Definition n_network.c:4101
#define NETWORK_IPALL
Flag for auto detection by OS of ip version to use.
Definition n_network.h:48
int netw_close(NETWORK **netw)
Closing a specified Network, destroy queues, free the structure.
Definition n_network.c:3367
int netw_set_crypto_chain(NETWORK *netw, char *key, char *certificate, char *ca_file)
activate SSL encryption using key/certificate files and a CA file for chain verification
Definition n_network.c:1704
NETWORK_HTTP_INFO netw_extract_http_info(char *request)
extract a lot of informations, mostly as pointers, and populate a NETWORK_HTTP_INFO structure
Definition n_network.c:6164
int netw_info_destroy(NETWORK_HTTP_INFO http_request)
destroy a NETWORK_HTTP_INFO loaded informations
Definition n_network.c:6237
HASH_TABLE * netw_parse_post_data(const char *post_data)
Function to parse POST data.
Definition n_network.c:6324
const char * netw_guess_http_content_type(const char *url)
function to guess the content type based on URL extension
Definition n_network.c:6370
Structure of a NETWORK.
Definition n_network.h:375
structure for splitting HTTP requests
Definition n_network.h:635
THREAD_POOL * new_thread_pool(size_t nbmaxthr, size_t nb_max_waiting)
Create a new pool of nbmaxthr threads.
int add_threaded_process(THREAD_POOL *thread_pool, void *(*func_ptr)(void *param), void *param, int mode)
add a function and params to a thread pool
int wait_for_threaded_pool(THREAD_POOL *thread_pool)
Wait for the thread pool to become idle (no active threads, empty waiting list), blocking without pol...
int destroy_threaded_pool(THREAD_POOL **pool, unsigned int delay)
Stop every thread of a pool and free it.
long int get_nb_cpu_cores()
get number of core of current system
#define DIRECT_PROC
processing mode for added func, direct start, not queued
Structure of a thread pool.
List structures and definitions.
Generic log system.
Network Engine.
Signals general handling with stack printing, from https://gist.github.com/jvranish/4441299.
N_STR and string function declaration.
Thread pool declaration.