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 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
27#include "nilorea/n_list.h"
28#include "nilorea/n_str.h"
29#include "nilorea/n_log.h"
30#include "nilorea/n_network.h"
32#include "nilorea/n_signals.h"
33
34#include <sys/stat.h>
35
36char* port = NULL;
37char* addr = NULL;
38char* key = NULL;
39char* cert = NULL;
40char* ca_file = NULL;
41char* root_dir = NULL;
42LIST* routes = NULL;
45int ssl_verify = 0;
46int max_connections = 0; /* 0 = unlimited */
47bool done = 0;
48
49NETWORK *server = NULL,
50 *netw = NULL;
52void usage(void) {
53 fprintf(stderr,
54 " -p 'port' : set the https server port\n"
55 " -k 'key file' : SSL key file path\n"
56 " -c 'cert file' : SSL certificate file path\n"
57 " -A 'CA file' : optional, CA certificate file for chain verification\n"
58 " -e : optional, enable SSL peer certificate verification\n"
59 " -a 'address name/ip' : optional, specify where to bind interface\n"
60 " -i 'ipmode' : optional, force 'ipv4' or 'ipv6', default supports both\n"
61 " -s 'size' : optional, maximum http request size (default: %d)\n"
62 " -n 'count' : optional, exit after count connections (0 = unlimited, default)\n"
63 " -d 'html root' : optional, specify a different http root dir (default: ./DATAS/)\n"
64 " -v : version\n"
65 " -h : help\n"
66 " -V 'log level' : optional, set the log level (default: LOG_ERR)\n",
68}
69
70void 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) {
71 int getoptret = 0,
72 log_level = LOG_ERR; /* default log level */
73
74 if (argc_nb == 1) {
75 fprintf(stderr, "No arguments given, help:\n");
76 usage();
77 exit(1);
78 }
79 while ((getoptret = getopt(argc_nb, argv_ptr, "hven:s:V:p:i:a:r:k:c:s:d:A:")) != EOF) {
80 switch (getoptret) {
81 case 'n':
82 max_connections = atoi(optarg);
83 break;
84 case 'i':
85 if (!strcmp("v4", optarg)) {
86 (*ip_version_ptr) = NETWORK_IPV4;
87 n_log(LOG_NOTICE, "IPV4 selected");
88 } else if (!strcmp("v6", optarg)) {
89 (*ip_version_ptr) = NETWORK_IPV6;
90 n_log(LOG_NOTICE, "IPV6 selected");
91 } else {
92 n_log(LOG_NOTICE, "IPV4/6 selected");
93 }
94 break;
95 case 'v':
96 fprintf(stderr, "Date de compilation : %s a %s.\n", __DATE__, __TIME__);
97 exit(1);
98 case 'V':
99 if (!strncmp("LOG_NULL", optarg, 8)) {
101 } else {
102 if (!strncmp("LOG_NOTICE", optarg, 10)) {
104 } else {
105 if (!strncmp("LOG_INFO", optarg, 8)) {
107 } else {
108 if (!strncmp("LOG_ERR", optarg, 7)) {
110 } else {
111 if (!strncmp("LOG_DEBUG", optarg, 9)) {
113 } else {
114 fprintf(stderr, "%s n'est pas un niveau de log valide.\n", optarg);
115 exit(-1);
116 }
117 }
118 }
119 }
120 }
121 break;
122 case 'e':
123 (*ssl_verify_ptr) = 1;
124 break;
125 case 'p':
126 (*port_ptr) = strdup(optarg);
127 break;
128 case 'r':
129 list_push(routes_ptr, strdup(optarg), &free);
130 break;
131 case 'a':
132 (*addr_ptr) = strdup(optarg);
133 break;
134 case 'k':
135 (*key_ptr) = strdup(optarg);
136 break;
137 case 'c':
138 (*cert_ptr) = strdup(optarg);
139 break;
140 case 'A':
141 (*ca_file_ptr) = strdup(optarg);
142 break;
143 case 's':
144 (*max_http_request_size_ptr) = atoi(optarg);
145 break;
146 case 'd':
147 (*root_dir_ptr) = strdup(optarg);
148 break;
149 default:
150 case '?': {
151 if (optopt == 'd') {
152 fprintf(stderr, "\n Missing html root directory\n");
153 }
154 if (optopt == 's') {
155 fprintf(stderr, "\n Missing max http size string\n");
156 }
157 if (optopt == 'k') {
158 fprintf(stderr, "\n Missing key file string\n");
159 }
160 if (optopt == 'c') {
161 fprintf(stderr, "\n Missing certificate file string\n");
162 }
163 if (optopt == 'A') {
164 fprintf(stderr, "\n Missing CA file string\n");
165 }
166 if (optopt == 'r') {
167 fprintf(stderr, "\n Missing route string\n");
168 }
169 if (optopt == 'a') {
170 fprintf(stderr, "\n Missing binding host/addr string\n");
171 }
172 if (optopt == 'i') {
173 fprintf(stderr, "\n Missing ip version (v4 or v6) string \n");
174 } else if (optopt == 'V') {
175 fprintf(stderr, "\n Missing log level string\n");
176 } else if (optopt == 'p') {
177 fprintf(stderr, "\n Missing port\n");
178 } else if (optopt != 's') {
179 fprintf(stderr, "\n Unknow missing option %c\n", optopt);
180 }
181 usage();
182 exit(1);
183 }
184 case 'h': {
185 usage();
186 exit(1);
187 }
188 }
189 }
191} /* void process_args( ... ) */
192
193/* Exit handling */
194void action_on_sig(int recvd_signal) {
195 (void)recvd_signal;
196#ifndef __windows__
197 static int nb_sigterm = 0;
198 switch (recvd_signal) {
199 /* We should not use these signals as they make the debugging going funky */
200 case (SIGABRT):
201 n_log(LOG_ERR, "Caught SIGABRT !");
202 break;
203 case (SIGINT):
204 n_log(LOG_ERR, "Caught SIGINT !");
205 break;
206 case (SIGBUS):
207 n_log(LOG_ERR, "Caught SIGBUS !");
208 break;
209 case (SIGFPE):
210 n_log(LOG_ERR, "Caught SIGFPE !");
211 break;
212 case (SIGSEGV):
213 n_log(LOG_ERR, "Caught SIGSEGV !");
214 break;
215 case (SIGSYS):
216 n_log(LOG_ERR, "Caught SIGSYS !");
217 break;
218 case (SIGTERM):
219 nb_sigterm++;
220 if (nb_sigterm >= 2) {
221 n_log(LOG_ERR, "Caught too much SIGTERM, trying _exit() !!");
222 _exit(-1);
223 }
224 n_log(LOG_ERR, "Caught %d SIGTERM, exiting now !!", nb_sigterm);
225 exit(-1);
226 case (SIGUSR1):
227 done = TRUE;
228 n_log(LOG_ERR, "Caught SIGUSR1 !");
229 break;
230 case (SIGUSR2):
231 done = TRUE;
232 n_log(LOG_ERR, "Caught SIGUSR2 !");
233 break;
234 case (SIGHUP):
235 n_log(LOG_NOTICE, "Caught SIGHUP !");
236 break;
237 default:
238 n_log(LOG_ERR, "Caught unknow signal %d", recvd_signal);
239 break;
240 }
241#endif
242} /* action_on_sig() */
243
244// Function to handle different URLs and return appropriate responses
245void handle_request(NETWORK* netw_ptr, LIST* routes_ptr) {
246 __n_assert(netw_ptr, return);
247 __n_assert(routes_ptr, return);
248
249 bool found = 0;
250 char** split_results = NULL;
251 char* http_url = NULL;
252 N_STR* dynamic_request_answer = NULL;
253
254 // Read request
255 char* http_buffer = NULL;
256 Alloca(http_buffer, (size_t)(max_http_request_size + 1));
257 __n_assert(http_buffer, netw_close(&netw_ptr); return);
258
259 // SSL_read reads up to max_http_request_size bytes (variable-length HTTP request).
260 // recv_ssl_data is intentionally NOT used here: it loops until the entire buffer is
261 // full, which would block indefinitely since HTTP requests are smaller than the buffer.
262 int ssl_read_ret = SSL_read(netw_ptr->ssl, http_buffer, max_http_request_size);
263 if (ssl_read_ret <= 0) {
264 int ssl_error = SSL_get_error(netw_ptr->ssl, ssl_read_ret);
265 if (ssl_error == SSL_ERROR_ZERO_RETURN) {
266 n_log(LOG_INFO, "SSL_read: peer closed the connection cleanly");
267 } else {
268 n_log(LOG_ERR, "SSL_read failed with SSL error %d", ssl_error);
269 }
270 netw_close(&netw_ptr);
271 return;
272 }
273 // n_log( LOG_DEBUG , "http_request: %s" , http_buffer );
274
275 // Extract URL from the request
276 char url[4096] = "";
277 netw_get_url_from_http_request(http_buffer, url, sizeof(url));
278 n_log(LOG_DEBUG, "url: %s", url);
279
280 // Handle the request based on the URL
281 N_STR* origin = new_nstr(32);
282 nstrprintf(origin, "%s:" SOCKET_SIZE_FORMAT, _str(netw_ptr->link.ip), netw_ptr->link.sock);
283
284 NETWORK_HTTP_INFO http_request = netw_extract_http_info(http_buffer);
285 N_STR* http_body = NULL;
286
287 split_results = split(url, "?", 0);
288 if (!split_results || !split_results[0]) {
289 http_body = char_to_nstr("<html><body><h1>Bad Request</h1></body></html>");
290 if (netw_build_http_response(&dynamic_request_answer, 400, "ex_network_ssl server", netw_guess_http_content_type(url), "", http_body) == FALSE) {
291 n_log(LOG_ERR, "couldn't build a Bad Request answer for %s", url);
292 }
293 n_log(LOG_ERR, "%s: %s %s 400", _nstr(origin), http_request.type, url);
294 } else {
295 http_url = split_results[0];
296 n_log(LOG_INFO, "%s: %s %s request...", _nstr(origin), http_request.type, url);
297 if (strcmp("OPTIONS", http_request.type) == 0) {
298 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) {
299 n_log(LOG_ERR, "couldn't build an OPTION answer for %s", url);
300 }
301 n_log(LOG_INFO, "%s: %s %s 200", _nstr(origin), http_request.type, url);
302 } else if (strcmp("GET", http_request.type) == 0) {
303 char system_url[4096] = "";
304 // example assume a root dir at DATAS
305 if (!root_dir) {
306 snprintf(system_url, sizeof(system_url), "./DATAS%s", http_url);
307 } else {
308 snprintf(system_url, sizeof(system_url), "%s%s", root_dir, http_url);
309 }
310
311 n_log(LOG_DEBUG, "%s: searching for file %s...", _nstr(origin), system_url);
312
313 struct stat st;
314 if (stat(system_url, &st) == 0 && S_ISREG(st.st_mode)) {
315 n_log(LOG_DEBUG, "%s: file %s found !", _nstr(origin), system_url);
316 http_body = file_to_nstr(system_url);
317 if (!http_body) {
318 http_body = char_to_nstr("<html><body><h1>Internal Server Error</h1></body></html>");
319 if (netw_build_http_response(&dynamic_request_answer, 500, "ex_network_ssl server", netw_guess_http_content_type(url), "", http_body) == FALSE) {
320 n_log(LOG_ERR, "couldn't build an Internal Server Error answer for %s", url);
321 }
322 n_log(LOG_ERR, "%s: %s %s 500", _nstr(origin), http_request.type, url);
323 } else {
324 if (netw_build_http_response(&dynamic_request_answer, 200, "ex_network_ssl server", netw_guess_http_content_type(url), "", http_body) == FALSE) {
325 n_log(LOG_ERR, "couldn't build an http answer for %s", url);
326 }
327 n_log(LOG_INFO, "%s: %s %s 200", _nstr(origin), http_request.type, url);
328 }
329 } else if (stat(system_url, &st) == 0 && S_ISDIR(st.st_mode)) {
330 /* Directory: check for index.html and redirect */
331 char index_path[4096 + 16] = "";
332 size_t url_len = strlen(http_url);
333 const char* slash = (url_len > 0 && http_url[url_len - 1] == '/') ? "" : "/";
334 snprintf(index_path, sizeof(index_path), "%s%sindex.html", system_url, slash);
335 struct stat idx_st;
336 if (stat(index_path, &idx_st) == 0 && S_ISREG(idx_st.st_mode)) {
337 char location_header[4096] = "";
338 snprintf(location_header, sizeof(location_header), "Location: %s%sindex.html\r\n", http_url, slash);
339 if (netw_build_http_response(&dynamic_request_answer, 301, "ex_network_ssl server", "text/html", location_header, NULL) == FALSE) {
340 n_log(LOG_ERR, "couldn't build a redirect answer for %s", url);
341 }
342 n_log(LOG_INFO, "%s: %s %s 301 -> %s%sindex.html", _nstr(origin), http_request.type, url, http_url, slash);
343 } else {
344 http_body = char_to_nstr("<html><body><h1>404 Not Found</h1></body></html>");
345 if (netw_build_http_response(&dynamic_request_answer, 404, "ex_network_ssl server", netw_guess_http_content_type(url), "", http_body) == FALSE) {
346 n_log(LOG_ERR, "couldn't build a NOT FOUND answer for %s", url);
347 }
348 n_log(LOG_ERR, "%s: %s %s 404", _nstr(origin), http_request.type, url);
349 }
350 } else {
351 http_body = char_to_nstr("<html><body><h1>404 Not Found</h1></body></html>");
352 if (netw_build_http_response(&dynamic_request_answer, 404, "ex_network_ssl server", netw_guess_http_content_type(url), "", http_body) == FALSE) {
353 n_log(LOG_ERR, "couldn't build a NOT FOUND answer for %s", url);
354 }
355 n_log(LOG_ERR, "%s: %s %s 404", _nstr(origin), http_request.type, url);
356 }
357 } else if (strcmp("POST", http_request.type) == 0) {
358 // Parse virtual route
359 found = 0;
360 list_foreach(node, routes_ptr) {
361 if (strcmp(node->ptr, http_url) == 0) {
362 // Handle 200 OK from virtual route
363 HASH_TABLE* post_data = netw_parse_post_data(http_request.body);
364 if (post_data) {
365 HT_FOREACH(hnode, post_data,
366 {
367 n_log(LOG_DEBUG, "%s: POST DATA: %s=%s", _nstr(origin), hnode->key, (char*)hnode->data.ptr);
368 });
369 destroy_ht(&post_data);
370 }
371 http_body = char_to_nstr("{\"status\":\"ok\"}");
372 if (netw_build_http_response(&dynamic_request_answer, 200, "ex_network_ssl server", "application/json", "", http_body) == FALSE) {
373 n_log(LOG_ERR, "couldn't build a route 200 answer for %s", url);
374 }
375 found = 1;
376 n_log(LOG_INFO, "%s: %s virtual:%s 200", _nstr(origin), http_request.type, url);
377 break;
378 }
379 }
380 if (!found) {
381 http_body = char_to_nstr("<html><body><h1>404 Not Found</h1></body></html>");
382 if (netw_build_http_response(&dynamic_request_answer, 404, "ex_network_ssl server", netw_guess_http_content_type(url), "", http_body) == FALSE) {
383 n_log(LOG_ERR, "couldn't build a NOT FOUND answer for %s", url);
384 }
385 n_log(LOG_ERR, "%s: %s %s 404", _nstr(origin), http_request.type, url);
386 }
387 } else {
388 http_body = char_to_nstr("<html><body><h1>Bad Request</h1></body></html>");
389 if (netw_build_http_response(&dynamic_request_answer, 400, "ex_network_ssl server", netw_guess_http_content_type(url), "", http_body) == FALSE) {
390 n_log(LOG_ERR, "couldn't build a Bad Request answer for %s", url);
391 }
392 n_log(LOG_ERR, "%s: %s %s 400", _nstr(origin), http_request.type, url);
393 }
394 free_split_result(&split_results);
395 }
396 if (dynamic_request_answer) {
397 if (dynamic_request_answer->written > UINT32_MAX) {
398 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);
399 } else if (send_ssl_data(netw_ptr, dynamic_request_answer->data, (uint32_t)dynamic_request_answer->written) < 0) {
400 n_log(LOG_ERR, "failed to send response for %s: %s %s", _nstr(origin), http_request.type, url);
401 }
402 free_nstr(&dynamic_request_answer);
403 } else {
404 n_log(LOG_ERR, "couldn't build an answer for %s: %s %s", _nstr(origin), http_request.type, url);
405 }
406 netw_info_destroy(http_request);
407 free_nstr(&origin);
408 free_nstr(&http_body);
409} /* handle_request */
410
418
419void* ssl_network_thread(void* params) {
420 __n_assert(params, return NULL);
422 handle_request(ssl_params->netw, ssl_params->routes);
423 netw_close(&ssl_params->netw);
424 Free(ssl_params);
425 return NULL;
426}
427
428int main(int argc, char* argv[]) {
429 int exit_code = 0;
430 THREAD_POOL* thread_pool = NULL;
432 __n_assert(routes, n_log(LOG_ERR, "could not allocate list !"); exit(1));
433
434 /* processing args and set log_level */
436
437 if (!port) {
438 n_log(LOG_ERR, "No port given. Exiting.");
439 exit_code = 1;
440 goto clean_and_exit;
441 }
442 if (!key) {
443 n_log(LOG_ERR, "No key given. Exiting.");
444 exit_code = 1;
445 goto clean_and_exit;
446 }
447 if (!cert) {
448 n_log(LOG_ERR, "No certificate given. Exiting.");
449 exit_code = 1;
450 goto clean_and_exit;
451 }
452 /*
453 if (routes->nb_items == 0) {
454 n_log(LOG_ERR, "No route given. Exiting.");
455 exit_code = 1;
456 goto clean_and_exit;
457 }
458 */
459
460#ifndef __windows__
461 errno = 0;
462 signal(SIGPIPE, SIG_IGN);
463 /* initializing signal catching */
464 struct sigaction signal_catcher;
465
466 /* quit on sig */
467 signal_catcher.sa_handler = action_on_sig;
468 sigemptyset(&signal_catcher.sa_mask);
469 signal_catcher.sa_flags = 0;
470
471 sigaction(SIGTERM, &signal_catcher, NULL);
472 sigaction(SIGUSR1, &signal_catcher, NULL);
473#endif
474
475 long int cores = get_nb_cpu_cores();
476 int nb_active_threads = (cores > 0) ? (int)cores : 1;
477 int nb_waiting_threads = 10 * nb_active_threads;
478 n_log(LOG_INFO, "Creating a new thread pool of %d active and %d waiting threads", nb_active_threads, nb_waiting_threads);
479 thread_pool = new_thread_pool((size_t)nb_active_threads, (size_t)nb_waiting_threads);
480
481 n_log(LOG_INFO, "Creating listening network for %s:%s %d", _str(addr), _str(port), ip_version);
482 /* create listening network */
483 if (netw_make_listening(&server, addr, port, SOMAXCONN, ip_version) == FALSE) {
484 n_log(LOG_ERR, "Fatal error with network initialization");
485 exit(-1);
486 }
487
488 if (ca_file) {
489 n_log(LOG_INFO, "Using SSL with certificate chain verification (CA: %s)", ca_file);
491 } else {
493 }
494 if (ssl_verify) {
495 n_log(LOG_INFO, "SSL peer certificate verification enabled");
497 }
498 int accepted_count = 0;
499 while (!done) {
500 n_log(LOG_DEBUG, "Blocking on accept...");
501 /* get any accepted client on a network */
502 int return_code = 0;
503 netw = netw_accept_from_ex(server, 0, 0, 0, &return_code);
504 if (!netw) {
505 if (return_code == EINTR) {
506 n_log(LOG_INFO, "accept exited after catching a signal");
507 goto clean_and_exit;
508 } else {
509 n_log(LOG_ERR, "error on accept, NULL netw returned !");
510 }
511 } else {
512 n_log(LOG_INFO, "accepted SSL connection on socket %d", netw->link.sock);
513 NETWORK_SSL_THREAD_PARAMS* netw_ssl_params = NULL;
514 Malloc(netw_ssl_params, NETWORK_SSL_THREAD_PARAMS, 1);
515 netw_ssl_params->netw = netw;
516 netw_ssl_params->routes = routes;
517 if (add_threaded_process(thread_pool, &ssl_network_thread, (void*)netw_ssl_params, DIRECT_PROC) == FALSE) {
518 n_log(LOG_ERR, "Error adding client management to thread pool");
519 }
520 accepted_count++;
521 if (max_connections > 0 && accepted_count >= max_connections) {
522 n_log(LOG_NOTICE, "reached %d connections, exiting", max_connections);
523 break;
524 }
525 }
526 }
527clean_and_exit:
528 if (thread_pool) {
531 }
533 netw_unload();
537 FreeNoLog(key);
541 exit(exit_code);
542}
static void usage(void)
void process_args(int argc, char **argv)
Definition ex_common.c:47
int main(void)
THREAD_POOL * thread_pool
Definition ex_fluid.c:77
int getoptret
Definition ex_fluid.c:60
int log_level
Definition ex_fluid.c:61
NETWORK * netw
Network for server mode, accepting incomming.
Definition ex_network.c:38
int max_http_request_size
int max_connections
bool done
char * root_dir
char * ca_file
NETWORK * server
char * key
int ssl_verify
int ip_version
LIST * routes
void * ssl_network_thread(void *params)
char * addr
char * cert
void action_on_sig(int recvd_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
#define FreeNoLog(__ptr)
Free Handler without log.
Definition n_common.h:271
#define Malloc(__ptr, __struct, __size)
Malloc Handler to get errors and set to 0.
Definition n_common.h:203
#define __n_assert(__ptr, __ret)
macro to assert things
Definition n_common.h:278
#define _str(__PTR)
define true
Definition n_common.h:192
#define Alloca(__ptr, __size)
Malloca Handler to get errors and set to 0.
Definition n_common.h:215
#define Free(__ptr)
Free Handler to get errors.
Definition n_common.h:262
#define _nstr(__PTR)
N_STR or "NULL" string for logging purposes.
Definition n_common.h:198
int destroy_ht(HASH_TABLE **table)
empty a table and destroy it
Definition n_hash.c:2234
#define HT_FOREACH(__ITEM_, __HASH_,...)
ForEach macro helper.
Definition n_hash.h:215
structure of a hash table
Definition n_hash.h:137
int list_push(LIST *list, void *ptr, void(*destructor)(void *ptr))
Add a pointer to the end of the list.
Definition n_list.c:227
#define list_foreach(__ITEM_, __LIST_)
ForEach macro helper, safe for node removal during iteration.
Definition n_list.h:88
int list_destroy(LIST **list)
Empty and Free a list container.
Definition n_list.c:547
LIST * new_generic_list(size_t max_items)
Initialiaze a generic list container to max_items pointers.
Definition n_list.c:36
#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:74
Structure of a generic LIST container.
Definition n_list.h:58
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:88
#define LOG_DEBUG
debug-level messages
Definition n_log.h:83
#define LOG_ERR
error conditions
Definition n_log.h:75
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:120
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:79
#define LOG_NULL
no log output
Definition n_log.h:45
#define LOG_INFO
informational
Definition n_log.h:81
size_t written
size of the written data inside the string
Definition n_str.h:66
char * data
the string
Definition n_str.h:62
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
Definition n_str.h:201
N_STR * char_to_nstr(const char *src)
Convert a char into a N_STR, short version.
Definition n_str.c:254
N_STR * new_nstr(NSTRBYTE size)
create a new N_STR string
Definition n_str.c:206
#define nstrprintf(__nstr_var, __format,...)
Macro to quickly allocate and sprintf to N_STR.
Definition n_str.h:115
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:912
N_STR * file_to_nstr(char *filename)
Load a whole file into a N_STR.
Definition n_str.c:286
int free_split_result(char ***tab)
Free a split result allocated array.
Definition n_str.c:1008
A box including a string and his lenght.
Definition n_str.h:60
char * ip
ip of the connected socket
Definition n_network.h:244
N_SOCKET link
networking socket
Definition n_network.h:326
char * type
Type of request.
Definition n_network.h:389
char * body
Pointer to the body data.
Definition n_network.h:387
SOCKET sock
a normal socket
Definition n_network.h:242
SSL * ssl
SSL handle.
Definition n_network.h:318
ssize_t send_ssl_data(void *netw, char *buf, uint32_t n)
send data onto the socket
Definition n_network.c:3512
int netw_ssl_set_verify(NETWORK *netw, int enable)
enable or disable SSL peer certificate verification
Definition n_network.c:1606
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:4338
int netw_set_crypto(NETWORK *netw, char *key, char *certificate)
activate SSL encryption on selected network, using key and certificate
Definition n_network.c:1321
#define NETWORK_IPV6
Flag to force IPV6
Definition n_network.h:51
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:2240
#define NETWORK_IPV4
Flag to force IPV4
Definition n_network.h:49
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:4578
#define SOCKET_SIZE_FORMAT
socket associated printf style
Definition n_network.h:90
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:2713
#define NETWORK_IPALL
Flag for auto detection by OS of ip version to use.
Definition n_network.h:47
int netw_close(NETWORK **netw)
Closing a specified Network, destroy queues, free the structure.
Definition n_network.c:2041
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:1473
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:4258
int netw_info_destroy(NETWORK_HTTP_INFO http_request)
destroy a NETWORK_HTTP_INFO loaded informations
Definition n_network.c:4325
HASH_TABLE * netw_parse_post_data(const char *post_data)
Function to parse POST data.
Definition n_network.c:4412
const char * netw_guess_http_content_type(const char *url)
function to guess the content type based on URL extension
Definition n_network.c:4458
Structure of a NETWORK.
Definition n_network.h:258
structure for splitting HTTP requests
Definition n_network.h:381
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)
delete a thread_pool, exit the threads and free the structs
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.