summaryrefslogtreecommitdiff
path: root/src/rest/gnunet-rest-server.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/rest/gnunet-rest-server.c')
-rw-r--r--src/rest/gnunet-rest-server.c1022
1 files changed, 512 insertions, 510 deletions
diff --git a/src/rest/gnunet-rest-server.c b/src/rest/gnunet-rest-server.c
index eb930c17b..2456ad50c 100644
--- a/src/rest/gnunet-rest-server.c
+++ b/src/rest/gnunet-rest-server.c
@@ -54,7 +54,7 @@
54 * After how long do we clean up unused MHD SSL/TLS instances? 54 * After how long do we clean up unused MHD SSL/TLS instances?
55 */ 55 */
56#define MHD_CACHE_TIMEOUT \ 56#define MHD_CACHE_TIMEOUT \
57 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5) 57 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
58 58
59#define GN_REST_STATE_INIT 0 59#define GN_REST_STATE_INIT 0
60#define GN_REST_STATE_PROCESSING 1 60#define GN_REST_STATE_PROCESSING 1
@@ -142,7 +142,8 @@ static char *allow_credentials;
142/** 142/**
143 * MHD Connection handle 143 * MHD Connection handle
144 */ 144 */
145struct MhdConnectionHandle { 145struct MhdConnectionHandle
146{
146 struct MHD_Connection *con; 147 struct MHD_Connection *con;
147 148
148 struct MHD_Response *response; 149 struct MHD_Response *response;
@@ -167,21 +168,21 @@ struct MhdConnectionHandle {
167 * @param cls NULL 168 * @param cls NULL
168 */ 169 */
169static void 170static void
170do_httpd(void *cls); 171do_httpd (void *cls);
171 172
172 173
173/** 174/**
174 * Run MHD now, we have extra data ready for the callback. 175 * Run MHD now, we have extra data ready for the callback.
175 */ 176 */
176static void 177static void
177run_mhd_now() 178run_mhd_now ()
178{ 179{
179 if (NULL != httpd_task) 180 if (NULL != httpd_task)
180 { 181 {
181 GNUNET_SCHEDULER_cancel(httpd_task); 182 GNUNET_SCHEDULER_cancel (httpd_task);
182 httpd_task = NULL; 183 httpd_task = NULL;
183 } 184 }
184 httpd_task = GNUNET_SCHEDULER_add_now(&do_httpd, NULL); 185 httpd_task = GNUNET_SCHEDULER_add_now (&do_httpd, NULL);
185} 186}
186 187
187/** 188/**
@@ -193,121 +194,121 @@ run_mhd_now()
193 * @param status #GNUNET_OK if successful 194 * @param status #GNUNET_OK if successful
194 */ 195 */
195static void 196static void
196plugin_callback(void *cls, struct MHD_Response *resp, int status) 197plugin_callback (void *cls, struct MHD_Response *resp, int status)
197{ 198{
198 struct MhdConnectionHandle *handle = cls; 199 struct MhdConnectionHandle *handle = cls;
199 200
200 handle->status = status; 201 handle->status = status;
201 handle->response = resp; 202 handle->response = resp;
202 MHD_resume_connection(handle->con); 203 MHD_resume_connection (handle->con);
203 run_mhd_now(); 204 run_mhd_now ();
204} 205}
205 206
206 207
207static int 208static int
208cleanup_url_map(void *cls, const struct GNUNET_HashCode *key, void *value) 209cleanup_url_map (void *cls, const struct GNUNET_HashCode *key, void *value)
209{ 210{
210 GNUNET_free_non_null(value); 211 GNUNET_free_non_null (value);
211 return GNUNET_YES; 212 return GNUNET_YES;
212} 213}
213 214
214 215
215static void 216static void
216cleanup_handle(struct MhdConnectionHandle *handle) 217cleanup_handle (struct MhdConnectionHandle *handle)
217{ 218{
218 if (NULL != handle->response) 219 if (NULL != handle->response)
219 MHD_destroy_response(handle->response); 220 MHD_destroy_response (handle->response);
220 if (NULL != handle->data_handle) 221 if (NULL != handle->data_handle)
222 {
223 if (NULL != handle->data_handle->header_param_map)
221 { 224 {
222 if (NULL != handle->data_handle->header_param_map) 225 GNUNET_CONTAINER_multihashmap_iterate (handle->data_handle
223 { 226 ->header_param_map,
224 GNUNET_CONTAINER_multihashmap_iterate(handle->data_handle 227 &cleanup_url_map,
225 ->header_param_map, 228 NULL);
226 &cleanup_url_map, 229 GNUNET_CONTAINER_multihashmap_destroy (
227 NULL); 230 handle->data_handle->header_param_map);
228 GNUNET_CONTAINER_multihashmap_destroy(
229 handle->data_handle->header_param_map);
230 }
231 if (NULL != handle->data_handle->url_param_map)
232 {
233 GNUNET_CONTAINER_multihashmap_iterate(handle->data_handle->url_param_map,
234 &cleanup_url_map,
235 NULL);
236 GNUNET_CONTAINER_multihashmap_destroy(
237 handle->data_handle->url_param_map);
238 }
239 GNUNET_free(handle->data_handle);
240 } 231 }
241 GNUNET_free(handle); 232 if (NULL != handle->data_handle->url_param_map)
233 {
234 GNUNET_CONTAINER_multihashmap_iterate (handle->data_handle->url_param_map,
235 &cleanup_url_map,
236 NULL);
237 GNUNET_CONTAINER_multihashmap_destroy (
238 handle->data_handle->url_param_map);
239 }
240 GNUNET_free (handle->data_handle);
241 }
242 GNUNET_free (handle);
242} 243}
243 244
244static int 245static int
245header_iterator(void *cls, 246header_iterator (void *cls,
246 enum MHD_ValueKind kind, 247 enum MHD_ValueKind kind,
247 const char *key, 248 const char *key,
248 const char *value) 249 const char *value)
249{ 250{
250 struct GNUNET_REST_RequestHandle *handle = cls; 251 struct GNUNET_REST_RequestHandle *handle = cls;
251 struct GNUNET_HashCode hkey; 252 struct GNUNET_HashCode hkey;
252 char *val; 253 char *val;
253 char *lowerkey; 254 char *lowerkey;
254 255
255 lowerkey = GNUNET_strdup(key); 256 lowerkey = GNUNET_strdup (key);
256 GNUNET_STRINGS_utf8_tolower(key, lowerkey); 257 GNUNET_STRINGS_utf8_tolower (key, lowerkey);
257 GNUNET_CRYPTO_hash(lowerkey, strlen(lowerkey), &hkey); 258 GNUNET_CRYPTO_hash (lowerkey, strlen (lowerkey), &hkey);
258 GNUNET_asprintf(&val, "%s", value); 259 GNUNET_asprintf (&val, "%s", value);
259 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put( 260 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put (
260 handle->header_param_map, 261 handle->header_param_map,
261 &hkey, 262 &hkey,
262 val, 263 val,
263 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)) 264 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
264 { 265 {
265 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 266 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
266 "Could not load add header `%s'=%s\n", 267 "Could not load add header `%s'=%s\n",
267 lowerkey, 268 lowerkey,
268 value); 269 value);
269 } 270 }
270 GNUNET_free(lowerkey); 271 GNUNET_free (lowerkey);
271 return MHD_YES; 272 return MHD_YES;
272} 273}
273 274
274 275
275static int 276static int
276url_iterator(void *cls, 277url_iterator (void *cls,
277 enum MHD_ValueKind kind, 278 enum MHD_ValueKind kind,
278 const char *key, 279 const char *key,
279 const char *value) 280 const char *value)
280{ 281{
281 struct GNUNET_REST_RequestHandle *handle = cls; 282 struct GNUNET_REST_RequestHandle *handle = cls;
282 struct GNUNET_HashCode hkey; 283 struct GNUNET_HashCode hkey;
283 char *val; 284 char *val;
284 285
285 GNUNET_CRYPTO_hash(key, strlen(key), &hkey); 286 GNUNET_CRYPTO_hash (key, strlen (key), &hkey);
286 GNUNET_asprintf(&val, "%s", value); 287 GNUNET_asprintf (&val, "%s", value);
287 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put( 288 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put (
288 handle->url_param_map, 289 handle->url_param_map,
289 &hkey, 290 &hkey,
290 val, 291 val,
291 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)) 292 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
292 { 293 {
293 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 294 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
294 "Could not load add url param `%s'=%s\n", 295 "Could not load add url param `%s'=%s\n",
295 key, 296 key,
296 value); 297 value);
297 } 298 }
298 return MHD_YES; 299 return MHD_YES;
299} 300}
300 301
301static int 302static int
302post_data_iter(void *cls, 303post_data_iter (void *cls,
303 enum MHD_ValueKind kind, 304 enum MHD_ValueKind kind,
304 const char *key, 305 const char *key,
305 const char *filename, 306 const char *filename,
306 const char *content_type, 307 const char *content_type,
307 const char *transfer_encoding, 308 const char *transfer_encoding,
308 const char *data, 309 const char *data,
309 uint64_t off, 310 uint64_t off,
310 size_t size) 311 size_t size)
311{ 312{
312 struct GNUNET_REST_RequestHandle *handle = cls; 313 struct GNUNET_REST_RequestHandle *handle = cls;
313 struct GNUNET_HashCode hkey; 314 struct GNUNET_HashCode hkey;
@@ -316,20 +317,20 @@ post_data_iter(void *cls,
316 if (MHD_POSTDATA_KIND != kind) 317 if (MHD_POSTDATA_KIND != kind)
317 return MHD_YES; 318 return MHD_YES;
318 319
319 GNUNET_CRYPTO_hash(key, strlen(key), &hkey); 320 GNUNET_CRYPTO_hash (key, strlen (key), &hkey);
320 GNUNET_asprintf(&val, "%s", data); 321 GNUNET_asprintf (&val, "%s", data);
321 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put( 322 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put (
322 handle->url_param_map, 323 handle->url_param_map,
323 &hkey, 324 &hkey,
324 val, 325 val,
325 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)) 326 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
326 { 327 {
327 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 328 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
328 "Could not load add url param '%s'=%s\n", 329 "Could not load add url param '%s'=%s\n",
329 key, 330 key,
330 data); 331 data);
331 GNUNET_free(val); 332 GNUNET_free (val);
332 } 333 }
333 return MHD_YES; 334 return MHD_YES;
334} 335}
335 336
@@ -359,14 +360,14 @@ post_data_iter(void *cls,
359 * error while handling the request 360 * error while handling the request
360 */ 361 */
361static int 362static int
362create_response(void *cls, 363create_response (void *cls,
363 struct MHD_Connection *con, 364 struct MHD_Connection *con,
364 const char *url, 365 const char *url,
365 const char *meth, 366 const char *meth,
366 const char *ver, 367 const char *ver,
367 const char *upload_data, 368 const char *upload_data,
368 size_t *upload_data_size, 369 size_t *upload_data_size,
369 void **con_cls) 370 void **con_cls)
370{ 371{
371 char *plugin_name; 372 char *plugin_name;
372 char *origin; 373 char *origin;
@@ -377,135 +378,135 @@ create_response(void *cls,
377 con_handle = *con_cls; 378 con_handle = *con_cls;
378 379
379 if (NULL == *con_cls) 380 if (NULL == *con_cls)
381 {
382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New connection %s\n", url);
383 char tmp_url[strlen (url) + 1];
384 strcpy (tmp_url, url);
385 con_handle = GNUNET_new (struct MhdConnectionHandle);
386 con_handle->con = con;
387 con_handle->state = GN_REST_STATE_INIT;
388 *con_cls = con_handle;
389
390 plugin_name = strtok (tmp_url, "/");
391
392 if (NULL != plugin_name)
380 { 393 {
381 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "New connection %s\n", url); 394 GNUNET_CRYPTO_hash (plugin_name, strlen (plugin_name), &key);
382 char tmp_url[strlen(url) + 1];
383 strcpy(tmp_url, url);
384 con_handle = GNUNET_new(struct MhdConnectionHandle);
385 con_handle->con = con;
386 con_handle->state = GN_REST_STATE_INIT;
387 *con_cls = con_handle;
388 395
389 plugin_name = strtok(tmp_url, "/"); 396 con_handle->plugin = GNUNET_CONTAINER_multihashmap_get (plugin_map, &key);
390
391 if (NULL != plugin_name)
392 {
393 GNUNET_CRYPTO_hash(plugin_name, strlen(plugin_name), &key);
394
395 con_handle->plugin = GNUNET_CONTAINER_multihashmap_get(plugin_map, &key);
396 }
397 if (NULL == con_handle->plugin)
398 {
399 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Queueing response with MHD\n");
400 GNUNET_free(con_handle);
401 return MHD_queue_response(con, MHD_HTTP_NOT_FOUND, failure_response);
402 }
403
404 return MHD_YES;
405 } 397 }
398 if (NULL == con_handle->plugin)
399 {
400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Queueing response with MHD\n");
401 GNUNET_free (con_handle);
402 return MHD_queue_response (con, MHD_HTTP_NOT_FOUND, failure_response);
403 }
404
405 return MHD_YES;
406 }
406 if (GN_REST_STATE_INIT == con_handle->state) 407 if (GN_REST_STATE_INIT == con_handle->state)
408 {
409 rest_conndata_handle = GNUNET_new (struct GNUNET_REST_RequestHandle);
410 rest_conndata_handle->method = meth;
411 rest_conndata_handle->url = url;
412 rest_conndata_handle->data = upload_data;
413 rest_conndata_handle->data_size = *upload_data_size;
414 rest_conndata_handle->url_param_map =
415 GNUNET_CONTAINER_multihashmap_create (16, GNUNET_NO);
416 rest_conndata_handle->header_param_map =
417 GNUNET_CONTAINER_multihashmap_create (16, GNUNET_NO);
418 con_handle->data_handle = rest_conndata_handle;
419 MHD_get_connection_values (con,
420 MHD_GET_ARGUMENT_KIND,
421 (MHD_KeyValueIterator) & url_iterator,
422 rest_conndata_handle);
423 MHD_get_connection_values (con,
424 MHD_HEADER_KIND,
425 (MHD_KeyValueIterator) & header_iterator,
426 rest_conndata_handle);
427 con_handle->pp = MHD_create_post_processor (con,
428 65536,
429 post_data_iter,
430 rest_conndata_handle);
431 if (*upload_data_size)
407 { 432 {
408 rest_conndata_handle = GNUNET_new(struct GNUNET_REST_RequestHandle); 433 MHD_post_process (con_handle->pp, upload_data, *upload_data_size);
409 rest_conndata_handle->method = meth;
410 rest_conndata_handle->url = url;
411 rest_conndata_handle->data = upload_data;
412 rest_conndata_handle->data_size = *upload_data_size;
413 rest_conndata_handle->url_param_map =
414 GNUNET_CONTAINER_multihashmap_create(16, GNUNET_NO);
415 rest_conndata_handle->header_param_map =
416 GNUNET_CONTAINER_multihashmap_create(16, GNUNET_NO);
417 con_handle->data_handle = rest_conndata_handle;
418 MHD_get_connection_values(con,
419 MHD_GET_ARGUMENT_KIND,
420 (MHD_KeyValueIterator) & url_iterator,
421 rest_conndata_handle);
422 MHD_get_connection_values(con,
423 MHD_HEADER_KIND,
424 (MHD_KeyValueIterator) & header_iterator,
425 rest_conndata_handle);
426 con_handle->pp = MHD_create_post_processor(con,
427 65536,
428 post_data_iter,
429 rest_conndata_handle);
430 if (*upload_data_size)
431 {
432 MHD_post_process(con_handle->pp, upload_data, *upload_data_size);
433 }
434 MHD_destroy_post_processor(con_handle->pp);
435
436 con_handle->state = GN_REST_STATE_PROCESSING;
437 con_handle->plugin->process_request(rest_conndata_handle,
438 &plugin_callback,
439 con_handle);
440 *upload_data_size = 0;
441 run_mhd_now();
442 return MHD_YES;
443 } 434 }
435 MHD_destroy_post_processor (con_handle->pp);
436
437 con_handle->state = GN_REST_STATE_PROCESSING;
438 con_handle->plugin->process_request (rest_conndata_handle,
439 &plugin_callback,
440 con_handle);
441 *upload_data_size = 0;
442 run_mhd_now ();
443 return MHD_YES;
444 }
444 if (NULL == con_handle->response) 445 if (NULL == con_handle->response)
446 {
447 // Suspend connection until plugin is done
448 MHD_suspend_connection (con_handle->con);
449 return MHD_YES;
450 }
451 MHD_resume_connection (con_handle->con);
452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
453 "Queueing response from plugin with MHD\n");
454 // Handle Preflights for extensions
455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Checking origin\n");
456 GNUNET_CRYPTO_hash ("origin", strlen ("origin"), &key);
457 origin = GNUNET_CONTAINER_multihashmap_get (con_handle->data_handle
458 ->header_param_map,
459 &key);
460 if (NULL != origin)
461 {
462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Origin: %s\n", origin);
463 // Only echo for browser plugins
464 if (GNUNET_YES == echo_origin)
445 { 465 {
446 //Suspend connection until plugin is done 466 if ((0 ==
447 MHD_suspend_connection(con_handle->con); 467 strncmp ("moz-extension://", origin, strlen ("moz-extension://"))) ||
448 return MHD_YES; 468 (0 == strncmp ("chrome-extension://",
469 origin,
470 strlen ("chrome-extension://"))))
471 {
472 MHD_add_response_header (con_handle->response,
473 MHD_HTTP_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN,
474 origin);
475 }
449 } 476 }
450 MHD_resume_connection(con_handle->con); 477 if (NULL != allow_origins)
451 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
452 "Queueing response from plugin with MHD\n");
453 //Handle Preflights for extensions
454 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Checking origin\n");
455 GNUNET_CRYPTO_hash("origin", strlen("origin"), &key);
456 origin = GNUNET_CONTAINER_multihashmap_get(con_handle->data_handle
457 ->header_param_map,
458 &key);
459 if (NULL != origin)
460 { 478 {
461 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Origin: %s\n", origin); 479 char *tmp = GNUNET_strdup (allow_origins);
462 //Only echo for browser plugins 480 char *allow_origin = strtok (tmp, ",");
463 if (GNUNET_YES == echo_origin) 481 while (NULL != allow_origin)
482 {
483 if (0 == strncmp (allow_origin, origin, strlen (allow_origin)))
464 { 484 {
465 if ((0 == 485 MHD_add_response_header (con_handle->response,
466 strncmp("moz-extension://", origin, strlen("moz-extension://"))) || 486 MHD_HTTP_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN,
467 (0 == strncmp("chrome-extension://", 487 allow_origin);
468 origin, 488 break;
469 strlen("chrome-extension://"))))
470 {
471 MHD_add_response_header(con_handle->response,
472 MHD_HTTP_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN,
473 origin);
474 }
475 }
476 if (NULL != allow_origins)
477 {
478 char *tmp = GNUNET_strdup(allow_origins);
479 char *allow_origin = strtok(tmp, ",");
480 while (NULL != allow_origin)
481 {
482 if (0 == strncmp(allow_origin, origin, strlen(allow_origin)))
483 {
484 MHD_add_response_header(con_handle->response,
485 MHD_HTTP_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN,
486 allow_origin);
487 break;
488 }
489 allow_origin = strtok(NULL, ",");
490 }
491 GNUNET_free(tmp);
492 } 489 }
490 allow_origin = strtok (NULL, ",");
491 }
492 GNUNET_free (tmp);
493 } 493 }
494 }
494 if (NULL != allow_credentials) 495 if (NULL != allow_credentials)
495 { 496 {
496 MHD_add_response_header(con_handle->response, 497 MHD_add_response_header (con_handle->response,
497 "Access-Control-Allow-Credentials", 498 "Access-Control-Allow-Credentials",
498 allow_credentials); 499 allow_credentials);
499 } 500 }
500 if (NULL != allow_headers) 501 if (NULL != allow_headers)
501 { 502 {
502 MHD_add_response_header(con_handle->response, 503 MHD_add_response_header (con_handle->response,
503 "Access-Control-Allow-Headers", 504 "Access-Control-Allow-Headers",
504 allow_headers); 505 allow_headers);
505 } 506 }
506 run_mhd_now(); 507 run_mhd_now ();
507 int ret = MHD_queue_response(con, con_handle->status, con_handle->response); 508 int ret = MHD_queue_response (con, con_handle->status, con_handle->response);
508 cleanup_handle(con_handle); 509 cleanup_handle (con_handle);
509 return ret; 510 return ret;
510} 511}
511 512
@@ -522,15 +523,15 @@ create_response(void *cls,
522 * @param toe reason for request termination (ignored) 523 * @param toe reason for request termination (ignored)
523 */ 524 */
524static void 525static void
525mhd_completed_cb(void *cls, 526mhd_completed_cb (void *cls,
526 struct MHD_Connection *connection, 527 struct MHD_Connection *connection,
527 void **con_cls, 528 void **con_cls,
528 enum MHD_RequestTerminationCode toe) 529 enum MHD_RequestTerminationCode toe)
529{ 530{
530 if (MHD_REQUEST_TERMINATED_COMPLETED_OK != toe) 531 if (MHD_REQUEST_TERMINATED_COMPLETED_OK != toe)
531 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 532 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
532 "MHD encountered error handling request: %d\n", 533 "MHD encountered error handling request: %d\n",
533 toe); 534 toe);
534} 535}
535 536
536 537
@@ -538,39 +539,39 @@ mhd_completed_cb(void *cls,
538 * Kill the MHD daemon. 539 * Kill the MHD daemon.
539 */ 540 */
540static void 541static void
541kill_httpd() 542kill_httpd ()
542{ 543{
543 if (NULL != httpd) 544 if (NULL != httpd)
544 { 545 {
545 MHD_stop_daemon(httpd); 546 MHD_stop_daemon (httpd);
546 httpd = NULL; 547 httpd = NULL;
547 } 548 }
548 if (NULL != httpd_task) 549 if (NULL != httpd_task)
549 { 550 {
550 GNUNET_SCHEDULER_cancel(httpd_task); 551 GNUNET_SCHEDULER_cancel (httpd_task);
551 httpd_task = NULL; 552 httpd_task = NULL;
552 } 553 }
553 if (NULL != ltask4) 554 if (NULL != ltask4)
554 { 555 {
555 GNUNET_SCHEDULER_cancel(ltask4); 556 GNUNET_SCHEDULER_cancel (ltask4);
556 ltask4 = NULL; 557 ltask4 = NULL;
557 } 558 }
558 if (NULL != ltask6) 559 if (NULL != ltask6)
559 { 560 {
560 GNUNET_SCHEDULER_cancel(ltask6); 561 GNUNET_SCHEDULER_cancel (ltask6);
561 ltask6 = NULL; 562 ltask6 = NULL;
562 } 563 }
563 564
564 if (NULL != lsock4) 565 if (NULL != lsock4)
565 { 566 {
566 GNUNET_NETWORK_socket_close(lsock4); 567 GNUNET_NETWORK_socket_close (lsock4);
567 lsock4 = NULL; 568 lsock4 = NULL;
568 } 569 }
569 if (NULL != lsock6) 570 if (NULL != lsock6)
570 { 571 {
571 GNUNET_NETWORK_socket_close(lsock6); 572 GNUNET_NETWORK_socket_close (lsock6);
572 lsock6 = NULL; 573 lsock6 = NULL;
573 } 574 }
574} 575}
575 576
576 577
@@ -582,7 +583,7 @@ kill_httpd()
582 * @param hd the daemon to schedule 583 * @param hd the daemon to schedule
583 */ 584 */
584static void 585static void
585schedule_httpd() 586schedule_httpd ()
586{ 587{
587 fd_set rs; 588 fd_set rs;
588 fd_set ws; 589 fd_set ws;
@@ -594,50 +595,50 @@ schedule_httpd()
594 MHD_UNSIGNED_LONG_LONG timeout; 595 MHD_UNSIGNED_LONG_LONG timeout;
595 struct GNUNET_TIME_Relative tv; 596 struct GNUNET_TIME_Relative tv;
596 597
597 FD_ZERO(&rs); 598 FD_ZERO (&rs);
598 FD_ZERO(&ws); 599 FD_ZERO (&ws);
599 FD_ZERO(&es); 600 FD_ZERO (&es);
600 max = -1; 601 max = -1;
601 if (MHD_YES != MHD_get_fdset(httpd, &rs, &ws, &es, &max)) 602 if (MHD_YES != MHD_get_fdset (httpd, &rs, &ws, &es, &max))
602 { 603 {
603 kill_httpd(); 604 kill_httpd ();
604 return; 605 return;
605 } 606 }
606 haveto = MHD_get_timeout(httpd, &timeout); 607 haveto = MHD_get_timeout (httpd, &timeout);
607 if (MHD_YES == haveto) 608 if (MHD_YES == haveto)
608 tv.rel_value_us = (uint64_t)timeout * 1000LL; 609 tv.rel_value_us = (uint64_t) timeout * 1000LL;
609 else 610 else
610 tv = GNUNET_TIME_UNIT_FOREVER_REL; 611 tv = GNUNET_TIME_UNIT_FOREVER_REL;
611 if (-1 != max) 612 if (-1 != max)
612 { 613 {
613 wrs = GNUNET_NETWORK_fdset_create(); 614 wrs = GNUNET_NETWORK_fdset_create ();
614 wws = GNUNET_NETWORK_fdset_create(); 615 wws = GNUNET_NETWORK_fdset_create ();
615 GNUNET_NETWORK_fdset_copy_native(wrs, &rs, max + 1); 616 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
616 GNUNET_NETWORK_fdset_copy_native(wws, &ws, max + 1); 617 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
617 } 618 }
618 else 619 else
619 { 620 {
620 wrs = NULL; 621 wrs = NULL;
621 wws = NULL; 622 wws = NULL;
622 } 623 }
623 if (NULL != httpd_task) 624 if (NULL != httpd_task)
624 { 625 {
625 GNUNET_SCHEDULER_cancel(httpd_task); 626 GNUNET_SCHEDULER_cancel (httpd_task);
626 httpd_task = NULL; 627 httpd_task = NULL;
627 } 628 }
628 if ((MHD_YES == haveto) || (-1 != max)) 629 if ((MHD_YES == haveto) || (-1 != max))
629 { 630 {
630 httpd_task = GNUNET_SCHEDULER_add_select(GNUNET_SCHEDULER_PRIORITY_DEFAULT, 631 httpd_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
631 tv, 632 tv,
632 wrs, 633 wrs,
633 wws, 634 wws,
634 &do_httpd, 635 &do_httpd,
635 NULL); 636 NULL);
636 } 637 }
637 if (NULL != wrs) 638 if (NULL != wrs)
638 GNUNET_NETWORK_fdset_destroy(wrs); 639 GNUNET_NETWORK_fdset_destroy (wrs);
639 if (NULL != wws) 640 if (NULL != wws)
640 GNUNET_NETWORK_fdset_destroy(wws); 641 GNUNET_NETWORK_fdset_destroy (wws);
641} 642}
642 643
643/** 644/**
@@ -646,11 +647,11 @@ schedule_httpd()
646 * @param cls NULL 647 * @param cls NULL
647 */ 648 */
648static void 649static void
649do_httpd(void *cls) 650do_httpd (void *cls)
650{ 651{
651 httpd_task = NULL; 652 httpd_task = NULL;
652 MHD_run(httpd); 653 MHD_run (httpd);
653 schedule_httpd(); 654 schedule_httpd ();
654} 655}
655 656
656 657
@@ -661,7 +662,7 @@ do_httpd(void *cls)
661 * @param tc the scheduler context 662 * @param tc the scheduler context
662 */ 663 */
663static void 664static void
664do_accept(void *cls) 665do_accept (void *cls)
665{ 666{
666 struct GNUNET_NETWORK_Handle *lsock = cls; 667 struct GNUNET_NETWORK_Handle *lsock = cls;
667 struct GNUNET_NETWORK_Handle *s; 668 struct GNUNET_NETWORK_Handle *s;
@@ -669,42 +670,42 @@ do_accept(void *cls)
669 const struct sockaddr *addr; 670 const struct sockaddr *addr;
670 socklen_t len; 671 socklen_t len;
671 672
672 GNUNET_assert(NULL != lsock); 673 GNUNET_assert (NULL != lsock);
673 if (lsock == lsock4) 674 if (lsock == lsock4)
674 { 675 {
675 ltask4 = GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL, 676 ltask4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
676 lsock, 677 lsock,
677 &do_accept, 678 &do_accept,
678 lsock); 679 lsock);
679 } 680 }
680 else if (lsock == lsock6) 681 else if (lsock == lsock6)
681 { 682 {
682 ltask6 = GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL, 683 ltask6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
683 lsock, 684 lsock,
684 &do_accept, 685 &do_accept,
685 lsock); 686 lsock);
686 } 687 }
687 else 688 else
688 GNUNET_assert(0); 689 GNUNET_assert (0);
689 s = GNUNET_NETWORK_socket_accept(lsock, NULL, NULL); 690 s = GNUNET_NETWORK_socket_accept (lsock, NULL, NULL);
690 if (NULL == s) 691 if (NULL == s)
691 { 692 {
692 GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR, "accept"); 693 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "accept");
693 return; 694 return;
694 } 695 }
695 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
696 "Got an inbound connection, waiting for data\n"); 697 "Got an inbound connection, waiting for data\n");
697 fd = GNUNET_NETWORK_get_fd(s); 698 fd = GNUNET_NETWORK_get_fd (s);
698 addr = GNUNET_NETWORK_get_addr(s); 699 addr = GNUNET_NETWORK_get_addr (s);
699 len = GNUNET_NETWORK_get_addrlen(s); 700 len = GNUNET_NETWORK_get_addrlen (s);
700 if (MHD_YES != MHD_add_connection(httpd, fd, addr, len)) 701 if (MHD_YES != MHD_add_connection (httpd, fd, addr, len))
701 { 702 {
702 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, 703 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
703 _("Failed to pass client to MHD\n")); 704 _ ("Failed to pass client to MHD\n"));
704 return; 705 return;
705 } 706 }
706 GNUNET_free(s); 707 GNUNET_free (s);
707 schedule_httpd(); 708 schedule_httpd ();
708} 709}
709 710
710 711
@@ -714,12 +715,12 @@ do_accept(void *cls)
714 * @param cls closure 715 * @param cls closure
715 */ 716 */
716static void 717static void
717do_shutdown(void *cls) 718do_shutdown (void *cls)
718{ 719{
719 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Shutting down...\n"); 720 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Shutting down...\n");
720 kill_httpd(); 721 kill_httpd ();
721 GNUNET_free_non_null(allow_credentials); 722 GNUNET_free_non_null (allow_credentials);
722 GNUNET_free_non_null(allow_headers); 723 GNUNET_free_non_null (allow_headers);
723} 724}
724 725
725 726
@@ -729,31 +730,31 @@ do_shutdown(void *cls)
729 * @return NULL on error 730 * @return NULL on error
730 */ 731 */
731static struct GNUNET_NETWORK_Handle * 732static struct GNUNET_NETWORK_Handle *
732bind_v4() 733bind_v4 ()
733{ 734{
734 struct GNUNET_NETWORK_Handle *ls; 735 struct GNUNET_NETWORK_Handle *ls;
735 struct sockaddr_in sa4; 736 struct sockaddr_in sa4;
736 int eno; 737 int eno;
737 738
738 memset(&sa4, 0, sizeof(sa4)); 739 memset (&sa4, 0, sizeof(sa4));
739 sa4.sin_family = AF_INET; 740 sa4.sin_family = AF_INET;
740 sa4.sin_port = htons(port); 741 sa4.sin_port = htons (port);
741 sa4.sin_addr.s_addr = address; 742 sa4.sin_addr.s_addr = address;
742#if HAVE_SOCKADDR_IN_SIN_LEN 743#if HAVE_SOCKADDR_IN_SIN_LEN
743 sa4.sin_len = sizeof(sa4); 744 sa4.sin_len = sizeof(sa4);
744#endif 745#endif
745 ls = GNUNET_NETWORK_socket_create(AF_INET, SOCK_STREAM, 0); 746 ls = GNUNET_NETWORK_socket_create (AF_INET, SOCK_STREAM, 0);
746 if (NULL == ls) 747 if (NULL == ls)
747 return NULL; 748 return NULL;
748 if (GNUNET_OK != GNUNET_NETWORK_socket_bind(ls, 749 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (ls,
749 (const struct sockaddr *)&sa4, 750 (const struct sockaddr *) &sa4,
750 sizeof(sa4))) 751 sizeof(sa4)))
751 { 752 {
752 eno = errno; 753 eno = errno;
753 GNUNET_NETWORK_socket_close(ls); 754 GNUNET_NETWORK_socket_close (ls);
754 errno = eno; 755 errno = eno;
755 return NULL; 756 return NULL;
756 } 757 }
757 return ls; 758 return ls;
758} 759}
759 760
@@ -764,31 +765,31 @@ bind_v4()
764 * @return NULL on error 765 * @return NULL on error
765 */ 766 */
766static struct GNUNET_NETWORK_Handle * 767static struct GNUNET_NETWORK_Handle *
767bind_v6() 768bind_v6 ()
768{ 769{
769 struct GNUNET_NETWORK_Handle *ls; 770 struct GNUNET_NETWORK_Handle *ls;
770 struct sockaddr_in6 sa6; 771 struct sockaddr_in6 sa6;
771 int eno; 772 int eno;
772 773
773 memset(&sa6, 0, sizeof(sa6)); 774 memset (&sa6, 0, sizeof(sa6));
774 sa6.sin6_family = AF_INET6; 775 sa6.sin6_family = AF_INET6;
775 sa6.sin6_port = htons(port); 776 sa6.sin6_port = htons (port);
776 sa6.sin6_addr = address6; 777 sa6.sin6_addr = address6;
777#if HAVE_SOCKADDR_IN_SIN_LEN 778#if HAVE_SOCKADDR_IN_SIN_LEN
778 sa6.sin6_len = sizeof(sa6); 779 sa6.sin6_len = sizeof(sa6);
779#endif 780#endif
780 ls = GNUNET_NETWORK_socket_create(AF_INET6, SOCK_STREAM, 0); 781 ls = GNUNET_NETWORK_socket_create (AF_INET6, SOCK_STREAM, 0);
781 if (NULL == ls) 782 if (NULL == ls)
782 return NULL; 783 return NULL;
783 if (GNUNET_OK != GNUNET_NETWORK_socket_bind(ls, 784 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (ls,
784 (const struct sockaddr *)&sa6, 785 (const struct sockaddr *) &sa6,
785 sizeof(sa6))) 786 sizeof(sa6)))
786 { 787 {
787 eno = errno; 788 eno = errno;
788 GNUNET_NETWORK_socket_close(ls); 789 GNUNET_NETWORK_socket_close (ls);
789 errno = eno; 790 errno = eno;
790 return NULL; 791 return NULL;
791 } 792 }
792 return ls; 793 return ls;
793} 794}
794 795
@@ -801,33 +802,33 @@ bind_v6()
801 * @param lib_ret the object returned by the plugin initializer 802 * @param lib_ret the object returned by the plugin initializer
802 */ 803 */
803static void 804static void
804load_plugin(void *cls, const char *libname, void *lib_ret) 805load_plugin (void *cls, const char *libname, void *lib_ret)
805{ 806{
806 struct GNUNET_REST_Plugin *plugin = lib_ret; 807 struct GNUNET_REST_Plugin *plugin = lib_ret;
807 struct GNUNET_HashCode key; 808 struct GNUNET_HashCode key;
808 809
809 if (NULL == lib_ret) 810 if (NULL == lib_ret)
810 { 811 {
811 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
812 "Could not load plugin `%s'\n", 813 "Could not load plugin `%s'\n",
813 libname); 814 libname);
814 return; 815 return;
815 } 816 }
816 GNUNET_assert(1 < strlen(plugin->name)); 817 GNUNET_assert (1 < strlen (plugin->name));
817 GNUNET_assert('/' == *plugin->name); 818 GNUNET_assert ('/' == *plugin->name);
818 GNUNET_CRYPTO_hash(plugin->name + 1, strlen(plugin->name + 1), &key); 819 GNUNET_CRYPTO_hash (plugin->name + 1, strlen (plugin->name + 1), &key);
819 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put( 820 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put (
820 plugin_map, 821 plugin_map,
821 &key, 822 &key,
822 plugin, 823 plugin,
823 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)) 824 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
824 { 825 {
825 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
826 "Could not load add plugin `%s'\n", 827 "Could not load add plugin `%s'\n",
827 libname); 828 libname);
828 return; 829 return;
829 } 830 }
830 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Loaded plugin `%s'\n", libname); 831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Loaded plugin `%s'\n", libname);
831} 832}
832 833
833 834
@@ -840,175 +841,175 @@ load_plugin(void *cls, const char *libname, void *lib_ret)
840 * @param c configuration 841 * @param c configuration
841 */ 842 */
842static void 843static void
843run(void *cls, 844run (void *cls,
844 char *const *args, 845 char *const *args,
845 const char *cfgfile, 846 const char *cfgfile,
846 const struct GNUNET_CONFIGURATION_Handle *c) 847 const struct GNUNET_CONFIGURATION_Handle *c)
847{ 848{
848 char *addr_str; 849 char *addr_str;
849 850
850 cfg = c; 851 cfg = c;
851 plugin_map = GNUNET_CONTAINER_multihashmap_create(10, GNUNET_NO); 852 plugin_map = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
852 853
853 /* Get port to bind to */ 854 /* Get port to bind to */
854 if (GNUNET_OK != 855 if (GNUNET_OK !=
855 GNUNET_CONFIGURATION_get_value_number(cfg, "rest", "HTTP_PORT", &port)) 856 GNUNET_CONFIGURATION_get_value_number (cfg, "rest", "HTTP_PORT", &port))
856 { 857 {
857 //No address specified 858 // No address specified
858 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Using default port...\n"); 859 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Using default port...\n");
859 port = GNUNET_REST_SERVICE_PORT; 860 port = GNUNET_REST_SERVICE_PORT;
860 } 861 }
861 862
862 /* Get address to bind to */ 863 /* Get address to bind to */
863 if (GNUNET_OK != 864 if (GNUNET_OK !=
864 GNUNET_CONFIGURATION_get_value_string(cfg, "rest", "BIND_TO", &addr_str)) 865 GNUNET_CONFIGURATION_get_value_string (cfg, "rest", "BIND_TO", &addr_str))
865 { 866 {
866 //No address specified 867 // No address specified
867 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Don't know what to bind to...\n"); 868 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Don't know what to bind to...\n");
868 GNUNET_SCHEDULER_shutdown(); 869 GNUNET_SCHEDULER_shutdown ();
869 return; 870 return;
870 } 871 }
871 if (1 != inet_pton(AF_INET, addr_str, &address)) 872 if (1 != inet_pton (AF_INET, addr_str, &address))
872 { 873 {
873 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 874 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
874 "Unable to parse address %s\n", 875 "Unable to parse address %s\n",
875 addr_str); 876 addr_str);
876 GNUNET_free(addr_str); 877 GNUNET_free (addr_str);
877 GNUNET_SCHEDULER_shutdown(); 878 GNUNET_SCHEDULER_shutdown ();
878 return; 879 return;
879 } 880 }
880 GNUNET_free(addr_str); 881 GNUNET_free (addr_str);
881 /* Get address to bind to */ 882 /* Get address to bind to */
882 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, 883 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg,
883 "rest", 884 "rest",
884 "BIND_TO6", 885 "BIND_TO6",
885 &addr_str)) 886 &addr_str))
886 { 887 {
887 //No address specified 888 // No address specified
888 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Don't know what to bind6 to...\n"); 889 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Don't know what to bind6 to...\n");
889 GNUNET_SCHEDULER_shutdown(); 890 GNUNET_SCHEDULER_shutdown ();
890 return; 891 return;
891 } 892 }
892 if (1 != inet_pton(AF_INET6, addr_str, &address6)) 893 if (1 != inet_pton (AF_INET6, addr_str, &address6))
893 { 894 {
894 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 895 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
895 "Unable to parse IPv6 address %s\n", 896 "Unable to parse IPv6 address %s\n",
896 addr_str); 897 addr_str);
897 GNUNET_free(addr_str); 898 GNUNET_free (addr_str);
898 GNUNET_SCHEDULER_shutdown(); 899 GNUNET_SCHEDULER_shutdown ();
899 return; 900 return;
900 } 901 }
901 GNUNET_free(addr_str); 902 GNUNET_free (addr_str);
902 903
903 904
904 /* Get CORS data from cfg */ 905 /* Get CORS data from cfg */
905 echo_origin = 906 echo_origin =
906 GNUNET_CONFIGURATION_get_value_yesno(cfg, 907 GNUNET_CONFIGURATION_get_value_yesno (cfg,
907 "rest", 908 "rest",
908 "REST_ECHO_ORIGIN_WEBEXT"); 909 "REST_ECHO_ORIGIN_WEBEXT");
909 allow_origins = NULL; 910 allow_origins = NULL;
910 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, 911 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg,
911 "rest", 912 "rest",
912 "REST_ALLOW_ORIGIN", 913 "REST_ALLOW_ORIGIN",
913 &allow_origins)) 914 &allow_origins))
914 { 915 {
915 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 916 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
916 "No CORS Access-Control-Allow-Origin header will be sent...\n"); 917 "No CORS Access-Control-Allow-Origin header will be sent...\n");
917 } 918 }
918 if (GNUNET_OK != 919 if (GNUNET_OK !=
919 GNUNET_CONFIGURATION_get_value_string(cfg, 920 GNUNET_CONFIGURATION_get_value_string (cfg,
920 "rest", 921 "rest",
921 "REST_ALLOW_CREDENTIALS", 922 "REST_ALLOW_CREDENTIALS",
922 &allow_credentials)) 923 &allow_credentials))
923 { 924 {
924 //No origin specified 925 // No origin specified
925 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 926 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
926 "No CORS Credential Header will be sent...\n"); 927 "No CORS Credential Header will be sent...\n");
927 } 928 }
928 929
929 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, 930 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg,
930 "rest", 931 "rest",
931 "REST_ALLOW_HEADERS", 932 "REST_ALLOW_HEADERS",
932 &allow_headers)) 933 &allow_headers))
933 { 934 {
934 //No origin specified 935 // No origin specified
935 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 936 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
936 "No CORS Access-Control-Allow-Headers Header will be sent...\n"); 937 "No CORS Access-Control-Allow-Headers Header will be sent...\n");
937 } 938 }
938 939
939 /* Open listen socket proxy */ 940 /* Open listen socket proxy */
940 lsock6 = bind_v6(); 941 lsock6 = bind_v6 ();
941 if (NULL == lsock6) 942 if (NULL == lsock6)
942 { 943 {
943 GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR, "bind"); 944 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
944 } 945 }
945 else 946 else
947 {
948 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (lsock6, 5))
946 { 949 {
947 if (GNUNET_OK != GNUNET_NETWORK_socket_listen(lsock6, 5)) 950 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "listen");
948 { 951 GNUNET_NETWORK_socket_close (lsock6);
949 GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR, "listen"); 952 lsock6 = NULL;
950 GNUNET_NETWORK_socket_close(lsock6);
951 lsock6 = NULL;
952 }
953 else
954 {
955 ltask6 = GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL,
956 lsock6,
957 &do_accept,
958 lsock6);
959 }
960 } 953 }
961 lsock4 = bind_v4(); 954 else
962 if (NULL == lsock4)
963 { 955 {
964 GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR, "bind"); 956 ltask6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
957 lsock6,
958 &do_accept,
959 lsock6);
965 } 960 }
961 }
962 lsock4 = bind_v4 ();
963 if (NULL == lsock4)
964 {
965 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
966 }
966 else 967 else
968 {
969 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (lsock4, 5))
967 { 970 {
968 if (GNUNET_OK != GNUNET_NETWORK_socket_listen(lsock4, 5)) 971 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "listen");
969 { 972 GNUNET_NETWORK_socket_close (lsock4);
970 GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR, "listen"); 973 lsock4 = NULL;
971 GNUNET_NETWORK_socket_close(lsock4);
972 lsock4 = NULL;
973 }
974 else
975 {
976 ltask4 = GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL,
977 lsock4,
978 &do_accept,
979 lsock4);
980 }
981 } 974 }
982 if ((NULL == lsock4) && (NULL == lsock6)) 975 else
983 { 976 {
984 GNUNET_SCHEDULER_shutdown(); 977 ltask4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
985 return; 978 lsock4,
979 &do_accept,
980 lsock4);
986 } 981 }
987 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Service listens on port %llu\n", port); 982 }
988 httpd = MHD_start_daemon(MHD_USE_DEBUG | MHD_USE_NO_LISTEN_SOCKET | 983 if ((NULL == lsock4) && (NULL == lsock6))
989 MHD_ALLOW_SUSPEND_RESUME, 984 {
990 0, 985 GNUNET_SCHEDULER_shutdown ();
991 NULL, 986 return;
992 NULL, 987 }
993 &create_response, 988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service listens on port %llu\n", port);
994 NULL, 989 httpd = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_NO_LISTEN_SOCKET
995 MHD_OPTION_CONNECTION_TIMEOUT, 990 | MHD_ALLOW_SUSPEND_RESUME,
996 (unsigned int)16, 991 0,
997 MHD_OPTION_NOTIFY_COMPLETED, 992 NULL,
998 &mhd_completed_cb, 993 NULL,
999 NULL, 994 &create_response,
1000 MHD_OPTION_END); 995 NULL,
996 MHD_OPTION_CONNECTION_TIMEOUT,
997 (unsigned int) 16,
998 MHD_OPTION_NOTIFY_COMPLETED,
999 &mhd_completed_cb,
1000 NULL,
1001 MHD_OPTION_END);
1001 if (NULL == httpd) 1002 if (NULL == httpd)
1002 { 1003 {
1003 GNUNET_SCHEDULER_shutdown(); 1004 GNUNET_SCHEDULER_shutdown ();
1004 return; 1005 return;
1005 } 1006 }
1006 /* Load plugins */ 1007 /* Load plugins */
1007 GNUNET_PLUGIN_load_all("libgnunet_plugin_rest", 1008 GNUNET_PLUGIN_load_all ("libgnunet_plugin_rest",
1008 (void *)cfg, 1009 (void *) cfg,
1009 &load_plugin, 1010 &load_plugin,
1010 NULL); 1011 NULL);
1011 GNUNET_SCHEDULER_add_shutdown(&do_shutdown, NULL); 1012 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
1012} 1013}
1013 1014
1014 1015
@@ -1022,29 +1023,30 @@ run(void *cls,
1022 * 1023 *
1023 */ 1024 */
1024int 1025int
1025main(int argc, char *const *argv) 1026main (int argc, char *const *argv)
1026{ 1027{
1027 struct GNUNET_GETOPT_CommandLineOption options[] = { GNUNET_GETOPT_OPTION_END }; 1028 struct GNUNET_GETOPT_CommandLineOption options[] =
1029 { GNUNET_GETOPT_OPTION_END };
1028 static const char *err_page = "{}"; 1030 static const char *err_page = "{}";
1029 int ret; 1031 int ret;
1030 1032
1031 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args(argc, argv, &argc, &argv)) 1033 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1032 return 2; 1034 return 2;
1033 GNUNET_log_setup("gnunet-rest-server", "WARNING", NULL); 1035 GNUNET_log_setup ("gnunet-rest-server", "WARNING", NULL);
1034 failure_response = MHD_create_response_from_buffer(strlen(err_page), 1036 failure_response = MHD_create_response_from_buffer (strlen (err_page),
1035 (void *)err_page, 1037 (void *) err_page,
1036 MHD_RESPMEM_PERSISTENT); 1038 MHD_RESPMEM_PERSISTENT);
1037 ret = (GNUNET_OK == GNUNET_PROGRAM_run(argc, 1039 ret = (GNUNET_OK == GNUNET_PROGRAM_run (argc,
1038 argv, 1040 argv,
1039 "gnunet-rest-server", 1041 "gnunet-rest-server",
1040 _("GNUnet REST server"), 1042 _ ("GNUnet REST server"),
1041 options, 1043 options,
1042 &run, 1044 &run,
1043 NULL)) 1045 NULL))
1044 ? 0 1046 ? 0
1045 : 1; 1047 : 1;
1046 MHD_destroy_response(failure_response); 1048 MHD_destroy_response (failure_response);
1047 GNUNET_free_non_null((char *)argv); 1049 GNUNET_free_non_null ((char *) argv);
1048 return ret; 1050 return ret;
1049} 1051}
1050 1052