Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_network_ssl.c
Go to the documentation of this file.
1
9#include "nilorea/n_list.h"
10#include "nilorea/n_str.h"
11#include "nilorea/n_log.h"
12#include "nilorea/n_network.h"
14#include "nilorea/n_signals.h"
15
16char* port = NULL;
17char* addr = NULL;
18char* key = NULL;
19char* cert = NULL;
20char* root_dir = NULL;
21LIST* routes = NULL;
24bool done = 0;
25
26NETWORK *server = NULL,
27 *netw = NULL;
29void usage(void) {
30 fprintf(stderr,
31 " -p 'port' : set the https server port\n"
32 " -k 'key file' : SSL key file path\n"
33 " -c 'cert file' : SSL certificate file path\n"
34 " -a 'address name/ip' : optional, specify where to bind interface\n"
35 " -i 'ipmode' : optional, force 'ipv4' or 'ipv6', default supports both\n"
36 " -s 'size' : optional, maximum http request size (default: %d)\n"
37 " -d 'html root' : optional, specify a different http root dir (default: ./DATAS/)\n"
38 " -v : version\n"
39 " -h : help\n"
40 " -V 'log level' : optional, set the log level (default: LOG_ERR)\n",
42}
43
44void process_args(int argc_nb, char** argv_ptr, char** addr_ptr, char** port_ptr, char** key_ptr, char** cert_ptr, LIST* routes_ptr, int* ip_version_ptr, int* max_http_request_size_ptr, char** root_dir_ptr) {
45 int getoptret = 0,
46 log_level = LOG_ERR; /* default log level */
47
48 if (argc_nb == 1) {
49 fprintf(stderr, "No arguments given, help:\n");
50 usage();
51 exit(1);
52 }
53 while ((getoptret = getopt(argc_nb, argv_ptr, "hvs:V:p:i:a:r:k:c:s:d:")) != EOF) {
54 switch (getoptret) {
55 case 'i':
56 if (!strcmp("v4", optarg)) {
57 (*ip_version_ptr) = NETWORK_IPV4;
58 n_log(LOG_NOTICE, "IPV4 selected");
59 } else if (!strcmp("v6", optarg)) {
60 (*ip_version_ptr) = NETWORK_IPV6;
61 n_log(LOG_NOTICE, "IPV6 selected");
62 } else {
63 n_log(LOG_NOTICE, "IPV4/6 selected");
64 }
65 break;
66 case 'v':
67 fprintf(stderr, "Date de compilation : %s a %s.\n", __DATE__, __TIME__);
68 exit(1);
69 case 'V':
70 if (!strncmp("LOG_NULL", optarg, 8)) {
72 } else {
73 if (!strncmp("LOG_NOTICE", optarg, 10)) {
75 } else {
76 if (!strncmp("LOG_INFO", optarg, 8)) {
78 } else {
79 if (!strncmp("LOG_ERR", optarg, 7)) {
81 } else {
82 if (!strncmp("LOG_DEBUG", optarg, 9)) {
84 } else {
85 fprintf(stderr, "%s n'est pas un niveau de log valide.\n", optarg);
86 exit(-1);
87 }
88 }
89 }
90 }
91 }
92 break;
93 case 'p':
94 (*port_ptr) = strdup(optarg);
95 break;
96 case 'r':
97 list_push(routes_ptr, strdup(optarg), &free);
98 break;
99 case 'a':
100 (*addr_ptr) = strdup(optarg);
101 break;
102 case 'k':
103 (*key_ptr) = strdup(optarg);
104 break;
105 case 'c':
106 (*cert_ptr) = strdup(optarg);
107 break;
108 case 's':
109 (*max_http_request_size_ptr) = atoi(optarg);
110 break;
111 case 'd':
112 (*root_dir_ptr) = strdup(optarg);
113 break;
114 default:
115 case '?': {
116 if (optopt == 'd') {
117 fprintf(stderr, "\n Missing html root directory\n");
118 }
119 if (optopt == 's') {
120 fprintf(stderr, "\n Missing max http size string\n");
121 }
122 if (optopt == 'k') {
123 fprintf(stderr, "\n Missing key file string\n");
124 }
125 if (optopt == 'c') {
126 fprintf(stderr, "\n Missing certificate file string\n");
127 }
128
129 if (optopt == 'r') {
130 fprintf(stderr, "\n Missing route string\n");
131 }
132 if (optopt == 'a') {
133 fprintf(stderr, "\n Missing binding host/addr string\n");
134 }
135 if (optopt == 'i') {
136 fprintf(stderr, "\n Missing ip version (v4 or v6) string \n");
137 } else if (optopt == 'V') {
138 fprintf(stderr, "\n Missing log level string\n");
139 } else if (optopt == 'p') {
140 fprintf(stderr, "\n Missing port\n");
141 } else if (optopt != 's') {
142 fprintf(stderr, "\n Unknow missing option %c\n", optopt);
143 }
144 usage();
145 exit(1);
146 }
147 case 'h': {
148 usage();
149 exit(1);
150 }
151 }
152 }
154} /* void process_args( ... ) */
155
156/* Exit handling */
157void action_on_sig(int recvd_signal) {
158 (void)recvd_signal;
159#ifndef __windows__
160 static int nb_sigterm = 0;
161 switch (recvd_signal) {
162 /* We should not use these signals as they make the debugging going funky */
163 case (SIGABRT):
164 n_log(LOG_ERR, "Caught SIGABRT !");
165 break;
166 case (SIGINT):
167 n_log(LOG_ERR, "Caught SIGINT !");
168 break;
169 case (SIGBUS):
170 n_log(LOG_ERR, "Caught SIGBUS !");
171 break;
172 case (SIGFPE):
173 n_log(LOG_ERR, "Caught SIGFPE !");
174 break;
175 case (SIGSEGV):
176 n_log(LOG_ERR, "Caught SIGSEGV !");
177 break;
178 case (SIGSYS):
179 n_log(LOG_ERR, "Caught SIGSYS !");
180 break;
181 case (SIGTERM):
182 nb_sigterm++;
183 if (nb_sigterm >= 2) {
184 n_log(LOG_ERR, "Caught too much SIGTERM, trying _exit() !!");
185 _exit(-1);
186 }
187 n_log(LOG_ERR, "Caught %d SIGTERM, exiting now !!", nb_sigterm);
188 exit(-1);
189 case (SIGUSR1):
190 done = TRUE;
191 n_log(LOG_ERR, "Caught SIGUSR1 !");
192 break;
193 case (SIGUSR2):
194 done = TRUE;
195 n_log(LOG_ERR, "Caught SIGUSR1 !");
196 break;
197 case (SIGHUP):
198 n_log(LOG_NOTICE, "Caught SIGHUP !");
199 break;
200 default:
201 n_log(LOG_ERR, "Caught unknow signal %d", recvd_signal);
202 break;
203 }
204#endif
205} /* action_on_sig() */
206
207// Function to handle different URLs and return appropriate responses
208void handle_request(NETWORK* netw_ptr, LIST* routes_ptr) {
209 __n_assert(netw_ptr, return);
210 __n_assert(routes_ptr, return);
211
212 bool found = 0;
213 char** split_results = NULL;
214 char* http_url = NULL;
215 N_STR* dynamic_request_answer = NULL;
216
217 // Read request
218 char* http_buffer = NULL;
219 Alloca(http_buffer, max_http_request_size + 1);
220 __n_assert(http_buffer, netw_close(&netw_ptr); return);
221
222 SSL_read(netw_ptr->ssl, http_buffer, max_http_request_size);
223 // n_log( LOG_DEBUG , "http_request: %s" , http_buffer );
224
225 // Extract URL from the request
226 char url[4096] = "";
227 netw_get_url_from_http_request(http_buffer, url, sizeof(url));
228 n_log(LOG_DEBUG, "url: %s", url);
229
230 // Handle the request based on the URL
231 N_STR* origin = new_nstr(32);
232 nstrprintf(origin, "%s:" SOCKET_SIZE_FORMAT, _str(netw_ptr->link.ip), netw_ptr->link.sock);
233
234 NETWORK_HTTP_INFO http_request = netw_extract_http_info(http_buffer);
235 N_STR* http_body = NULL;
236
237 split_results = split(url, "?", 0);
238 if (!split_results || !split_results[0]) {
239 http_body = char_to_nstr("<html><body><h1>Bad Request</h1></body></html>");
240 if (netw_build_http_response(&dynamic_request_answer, 400, "ex_network_ssl server", netw_guess_http_content_type(url), "", http_body) == FALSE) {
241 n_log(LOG_ERR, "couldn't build a Bad Request answer for %s", url);
242 }
243 n_log(LOG_ERR, "%s: %s %s 400", _nstr(origin), http_request.type, url);
244 } else {
245 http_url = split_results[0];
246 n_log(LOG_INFO, "%s: %s %s request...", _nstr(origin), http_request.type, url);
247 if (strcmp("OPTIONS", http_request.type) == 0) {
248 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) {
249 n_log(LOG_ERR, "couldn't build an OPTION answer for %s", url);
250 }
251 n_log(LOG_INFO, "%s: %s %s 200", _nstr(origin), http_request.type, url);
252 } else if (strcmp("GET", http_request.type) == 0) {
253 char system_url[4096] = "";
254 // example assume a root dir at DATAS
255 if (!root_dir) {
256 snprintf(system_url, sizeof(system_url), "./DATAS%s", http_url);
257 } else {
258 snprintf(system_url, sizeof(system_url), "%s%s", root_dir, http_url);
259 }
260
261 n_log(LOG_DEBUG, "%s: searching for file %s...", _nstr(origin), system_url);
262
263 if (file_exist(system_url)) {
264 n_log(LOG_DEBUG, "%s: file %s found !", _nstr(origin), system_url);
265 http_body = file_to_nstr(system_url);
266 if (!http_body) {
267 http_body = char_to_nstr("<html><body><h1>Internal Server Error</h1></body></html>");
268 if (netw_build_http_response(&dynamic_request_answer, 500, "ex_network_ssl server", netw_guess_http_content_type(url), "", http_body) == FALSE) {
269 n_log(LOG_ERR, "couldn't build an Internal Server Error answer for %s", url);
270 }
271 n_log(LOG_ERR, "%s: %s %s 500", _nstr(origin), http_request.type, url);
272 } else {
273 if (netw_build_http_response(&dynamic_request_answer, 200, "ex_network_ssl server", netw_guess_http_content_type(url), "", http_body) == FALSE) {
274 n_log(LOG_ERR, "couldn't build an http answer for %s", url);
275 }
276 n_log(LOG_INFO, "%s: %s %s 200", _nstr(origin), http_request.type, url);
277 }
278 } else {
279 http_body = char_to_nstr("<html><body><h1>404 Not Found</h1></body></html>");
280 if (netw_build_http_response(&dynamic_request_answer, 404, "ex_network_ssl server", netw_guess_http_content_type(url), "", http_body) == FALSE) {
281 n_log(LOG_ERR, "couldn't build a NOT FOUND answer for %s", url);
282 }
283 n_log(LOG_ERR, "%s: %s %s 404", _nstr(origin), http_request.type, url);
284 }
285 } else if (strcmp("POST", http_request.type) == 0) {
286 // Parse virtual route
287 found = 0;
288 list_foreach(node, routes_ptr) {
289 if (strcmp(node->ptr, http_url) == 0) {
290 // Handle 200 OK from virtual route
291 HASH_TABLE* post_data = netw_parse_post_data(http_request.body);
292 if (post_data) {
293 HT_FOREACH(hnode, post_data,
294 {
295 n_log(LOG_DEBUG, "%s: POST DATA: %s=%s", _nstr(origin), hnode->key, (char*)hnode->data.ptr);
296 });
297 destroy_ht(&post_data);
298 }
299 http_body = char_to_nstr("{\"status\":\"ok\"}");
300 if (netw_build_http_response(&dynamic_request_answer, 200, "ex_network_ssl server", "application/json", "", http_body) == FALSE) {
301 n_log(LOG_ERR, "couldn't build a route 200 answer for %s", url);
302 }
303 found = 1;
304 n_log(LOG_INFO, "%s: %s virtual:%s 200", _nstr(origin), http_request.type, url);
305 break;
306 }
307 }
308 if (!found) {
309 http_body = char_to_nstr("<html><body><h1>404 Not Found</h1></body></html>");
310 if (netw_build_http_response(&dynamic_request_answer, 404, "ex_network_ssl server", netw_guess_http_content_type(url), "", http_body) == FALSE) {
311 n_log(LOG_ERR, "couldn't build a NOT FOUND answer for %s", url);
312 }
313 n_log(LOG_ERR, "%s: %s %s 404", _nstr(origin), http_request.type, url);
314 }
315 } else {
316 http_body = char_to_nstr("<html><body><h1>Bad Request</h1></body></html>");
317 if (netw_build_http_response(&dynamic_request_answer, 400, "ex_network_ssl server", netw_guess_http_content_type(url), "", http_body) == FALSE) {
318 n_log(LOG_ERR, "couldn't build a Bad Request answer for %s", url);
319 }
320 n_log(LOG_ERR, "%s: %s %s 400", _nstr(origin), http_request.type, url);
321 }
322 free_split_result(&split_results);
323 }
324 if (dynamic_request_answer) {
325 SSL_write(netw_ptr->ssl, _nstr(dynamic_request_answer), dynamic_request_answer->written);
326 free_nstr(&dynamic_request_answer);
327 } else {
328 n_log(LOG_ERR, "couldn't build an answer for %s: %s %s", _nstr(origin), http_request.type, url);
329 }
330 netw_info_destroy(http_request);
331 free_nstr(&origin);
332 free_nstr(&http_body);
333} /* handle_request */
334
342
343void* ssl_network_thread(void* params) {
344 __n_assert(params, return NULL);
346 handle_request(ssl_params->netw, ssl_params->routes);
347 netw_close(&ssl_params->netw);
348 Free(ssl_params);
349 return NULL;
350}
351
352int main(int argc, char* argv[]) {
353 int exit_code = 0;
354 THREAD_POOL* thread_pool = NULL;
356 __n_assert(routes, n_log(LOG_ERR, "could not allocate list !"); exit(1));
357
358 /* processing args and set log_level */
360
361 if (!port) {
362 n_log(LOG_ERR, "No port given. Exiting.");
363 exit_code = 1;
364 goto clean_and_exit;
365 }
366 if (!key) {
367 n_log(LOG_ERR, "No key given. Exiting.");
368 exit_code = 1;
369 goto clean_and_exit;
370 }
371 if (!cert) {
372 n_log(LOG_ERR, "No certificate given. Exiting.");
373 exit_code = 1;
374 goto clean_and_exit;
375 }
376 /*
377 if (routes->nb_items == 0) {
378 n_log(LOG_ERR, "No route given. Exiting.");
379 exit_code = 1;
380 goto clean_and_exit;
381 }
382 */
383
384#ifndef __windows__
385 errno = 0;
386 signal(SIGPIPE, SIG_IGN);
387 /* initializing signal catching */
388 struct sigaction signal_catcher;
389
390 /* quit on sig */
391 signal_catcher.sa_handler = action_on_sig;
392 sigemptyset(&signal_catcher.sa_mask);
393 signal_catcher.sa_flags = SA_SIGINFO;
394
395 sigaction(SIGTERM, &signal_catcher, NULL);
396 sigaction(SIGUSR1, &signal_catcher, NULL);
397#endif
398
399 int nb_active_threads = get_nb_cpu_cores();
400 int nb_waiting_threads = 10 * nb_active_threads;
401 n_log(LOG_INFO, "Creating a new thread pool of %d active and %d waiting threads", nb_active_threads, nb_waiting_threads);
402 thread_pool = new_thread_pool(nb_active_threads, nb_waiting_threads);
403
404 n_log(LOG_INFO, "Creating listening network for %s:%s %d", _str(addr), _str(port), ip_version);
405 /* create listening network */
406 if (netw_make_listening(&server, addr, port, SOMAXCONN, ip_version) == FALSE) {
407 n_log(LOG_ERR, "Fatal error with network initialization");
408 exit(-1);
409 }
410
412 while (!done) {
413 n_log(LOG_DEBUG, "Blocking on accept...");
414 /* get any accepted client on a network */
415 int return_code = 0;
416 netw = netw_accept_from_ex(server, 0, 0, 0, &return_code);
417 if (!netw) {
418 if (return_code == EINTR) {
419 n_log(LOG_INFO, "accept exited after catching a signal");
420 goto clean_and_exit;
421 } else {
422 n_log(LOG_ERR, "error on accept, NULL netw returned !");
423 }
424 } else {
425 n_log(LOG_INFO, "accepted SSL connection on socket %d", netw->link.sock);
426 NETWORK_SSL_THREAD_PARAMS* netw_ssl_params = NULL;
427 Malloc(netw_ssl_params, NETWORK_SSL_THREAD_PARAMS, 1);
428 netw_ssl_params->netw = netw;
429 netw_ssl_params->routes = routes;
430 if (add_threaded_process(thread_pool, &ssl_network_thread, (void*)netw_ssl_params, DIRECT_PROC) == FALSE) {
431 n_log(LOG_ERR, "Error adding client management to thread pool");
432 }
433 }
434 }
435clean_and_exit:
436 if (thread_pool) {
439 }
441 netw_unload();
443 exit(exit_code);
444}
void usage(void)
Definition ex_common.c:22
void process_args(int argc, char **argv)
Definition ex_common.c:29
int main(void)
THREAD_POOL * thread_pool
Definition ex_fluid.c:59
int getoptret
Definition ex_fluid.c:42
int log_level
Definition ex_fluid.c:43
NETWORK * netw
Network for server mode, accepting incomming.
Definition ex_network.c:20
int max_http_request_size
bool done
char * root_dir
NETWORK * server
char * key
int ip_version
LIST * routes
void * ssl_network_thread(void *params)
char * addr
char * cert
void action_on_sig(int recvd_signal)
LIST * routes
virtual routes for the server
void handle_request(NETWORK *netw_ptr, LIST *routes_ptr)
char * port
NETWORK * netw
network to use for the receiving thread
structure of a NETWORK_SSL_THREAD_PARAMS
#define Malloc(__ptr, __struct, __size)
Malloc Handler to get errors and set to 0.
Definition n_common.h:185
#define __n_assert(__ptr, __ret)
macro to assert things
Definition n_common.h:256
#define _str(__PTR)
define true
Definition n_common.h:174
#define Alloca(__ptr, __size)
Malloca Handler to get errors and set to 0.
Definition n_common.h:197
int file_exist(const char *filename)
test if file exist and if it's readable
Definition n_common.c:83
#define Free(__ptr)
Free Handler to get errors.
Definition n_common.h:240
#define _nstr(__PTR)
N_STR or "NULL" string for logging purposes.
Definition n_common.h:180
int destroy_ht(HASH_TABLE **table)
empty a table and destroy it
Definition n_hash.c:2148
#define HT_FOREACH(__ITEM_, __HASH_,...)
ForEach macro helper.
Definition n_hash.h:195
structure of a hash table
Definition n_hash.h:117
int list_push(LIST *list, void *ptr, void(*destructor)(void *ptr))
Add a pointer to the end of the list.
Definition n_list.c:200
#define list_foreach(__ITEM_, __LIST_)
ForEach macro helper.
Definition n_list.h:66
int list_destroy(LIST **list)
Empty and Free a list container.
Definition n_list.c:519
LIST * new_generic_list(size_t max_items)
Initialiaze a generic list container to max_items pointers.
Definition n_list.c:19
#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:56
Structure of a generic LIST container.
Definition n_list.h:40
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:70
#define LOG_DEBUG
debug-level messages
Definition n_log.h:65
#define LOG_ERR
error conditions
Definition n_log.h:57
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:104
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:61
#define LOG_NULL
no log output
Definition n_log.h:27
#define LOG_INFO
informational
Definition n_log.h:63
size_t written
size of the written data inside the string
Definition n_str.h:45
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
Definition n_str.h:176
N_STR * char_to_nstr(const char *src)
Convert a char into a N_STR, short version.
Definition n_str.c:228
N_STR * new_nstr(NSTRBYTE size)
create a new N_STR string
Definition n_str.c:180
#define nstrprintf(__nstr_var, __format,...)
Macro to quickly allocate and sprintf to N_STR.
Definition n_str.h:94
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:874
N_STR * file_to_nstr(char *filename)
Load a whole file into a N_STR.
Definition n_str.c:260
int free_split_result(char ***tab)
Free a split result allocated array.
Definition n_str.c:970
A box including a string and his lenght.
Definition n_str.h:39
char * ip
ip of the connected socket
Definition n_network.h:225
N_SOCKET link
networking socket
Definition n_network.h:305
SOCKET sock
a normal socket
Definition n_network.h:223
SSL * ssl
SSL handle.
Definition n_network.h:297
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:3303
int netw_set_crypto(NETWORK *netw, char *key, char *certificate)
activate SSL encryption on selected network, using key and certificate
Definition n_network.c:1160
#define NETWORK_IPV6
Flag to force IPV6
Definition n_network.h:31
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:1644
#define NETWORK_IPV4
Flag to force IPV4
Definition n_network.h:29
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:3544
#define SOCKET_SIZE_FORMAT
socket associated printf style
Definition n_network.h:66
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:1753
#define NETWORK_IPALL
Flag for auto detection by OS of ip version to use.
Definition n_network.h:27
int netw_close(NETWORK **netw)
Closing a specified Network, destroy queues, free the structure.
Definition n_network.c:1503
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:3221
int netw_info_destroy(NETWORK_HTTP_INFO http_request)
destroy a NETWORK_HTTP_INFO loaded informations
Definition n_network.c:3290
HASH_TABLE * netw_parse_post_data(const char *post_data)
Function to parse POST data.
Definition n_network.c:3377
const char * netw_guess_http_content_type(const char *url)
function to guess the content type based on URL extension
Definition n_network.c:3424
Structure of a NETWORK.
Definition n_network.h:239
structure for splitting HTTP requests
Definition n_network.h:342
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 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
int wait_for_threaded_pool(THREAD_POOL *thread_pool, unsigned int delay)
Wait for all the launched process in the thread pool to terminate.
Structure of a trhead 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.