aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMartin Schanzenbach <mschanzenbach@posteo.de>2015-03-26 15:51:56 +0000
committerMartin Schanzenbach <mschanzenbach@posteo.de>2015-03-26 15:51:56 +0000
commit726b28a61c858b4b251e4a0a006e4021b01886c0 (patch)
tree8b8d6dded055f1fefd2cee84dabb59dc889dcfe8 /src
parent9982f58b45230373960cea1fe1fb294602bdd56d (diff)
downloadgnunet-726b28a61c858b4b251e4a0a006e4021b01886c0.tar.gz
gnunet-726b28a61c858b4b251e4a0a006e4021b01886c0.zip
-fix mem corruption
Diffstat (limited to 'src')
-rw-r--r--src/identity/plugin_rest_identity.c44
-rw-r--r--src/rest/gnunet-rest-server.c11
-rw-r--r--src/rest/rest.c32
3 files changed, 44 insertions, 43 deletions
diff --git a/src/identity/plugin_rest_identity.c b/src/identity/plugin_rest_identity.c
index d62e56db2..2fb29d87d 100644
--- a/src/identity/plugin_rest_identity.c
+++ b/src/identity/plugin_rest_identity.c
@@ -31,9 +31,7 @@
31#include "microhttpd.h" 31#include "microhttpd.h"
32#include <jansson.h> 32#include <jansson.h>
33 33
34#define API_NAMESPACE "/identity" 34#define GNUNET_REST_API_NS_IDENTITY "/identity"
35
36#define EGO_NAMESPACE "/identity/egos"
37 35
38#define ID_REST_STATE_INIT 0 36#define ID_REST_STATE_INIT 0
39 37
@@ -183,8 +181,6 @@ cleanup_handle (struct RequestHandle *handle)
183 struct EgoEntry *ego_tmp; 181 struct EgoEntry *ego_tmp;
184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
185 "Cleaning up\n"); 183 "Cleaning up\n");
186 if (NULL != handle->json_root)
187 json_decref (handle->json_root);
188 if (NULL != handle->name) 184 if (NULL != handle->name)
189 GNUNET_free (handle->name); 185 GNUNET_free (handle->name);
190 if (NULL != handle->timeout_task) 186 if (NULL != handle->timeout_task)
@@ -301,16 +297,14 @@ ego_info_response (struct RestConnectionDataHandle *con,
301 struct JsonApiObject *json_object; 297 struct JsonApiObject *json_object;
302 struct JsonApiResource *json_resource; 298 struct JsonApiResource *json_resource;
303 json_t *key_str; 299 json_t *key_str;
304 300 if (GNUNET_NO == GNUNET_REST_namespace_match (handle->url, GNUNET_REST_API_NS_IDENTITY))
305 if (GNUNET_NO == GNUNET_REST_namespace_match (handle->url, EGO_NAMESPACE))
306 { 301 {
307 resp = GNUNET_REST_create_json_response (NULL); 302 resp = GNUNET_REST_create_json_response (NULL);
308 handle->proc (handle->proc_cls, resp, MHD_HTTP_BAD_REQUEST); 303 handle->proc (handle->proc_cls, resp, MHD_HTTP_BAD_REQUEST);
309 cleanup_handle (handle); 304 cleanup_handle (handle);
310 GNUNET_break (0);
311 return; 305 return;
312 } 306 }
313 if ( (strlen (EGO_NAMESPACE) == strlen (handle->url) )) { 307 if ( (strlen (GNUNET_REST_API_NS_IDENTITY) == strlen (handle->url) )) {
314 GNUNET_CRYPTO_hash (URL_PARAM_SUBSYS, strlen (URL_PARAM_SUBSYS), &key); 308 GNUNET_CRYPTO_hash (URL_PARAM_SUBSYS, strlen (URL_PARAM_SUBSYS), &key);
315 if ( GNUNET_YES == 309 if ( GNUNET_YES ==
316 GNUNET_CONTAINER_multihashmap_contains (handle->conndata_handle->url_param_map, 310 GNUNET_CONTAINER_multihashmap_contains (handle->conndata_handle->url_param_map,
@@ -331,13 +325,12 @@ ego_info_response (struct RestConnectionDataHandle *con,
331 } 325 }
332 } 326 }
333 json_object = GNUNET_REST_jsonapi_object_new (); 327 json_object = GNUNET_REST_jsonapi_object_new ();
334 egoname = &handle->url[strlen (EGO_NAMESPACE)+1]; 328 egoname = &handle->url[strlen (GNUNET_REST_API_NS_IDENTITY)+1];
335 329
336 if (strlen (EGO_NAMESPACE) == strlen (handle->url)) 330 if (strlen (GNUNET_REST_API_NS_IDENTITY) == strlen (handle->url))
337 { 331 {
338 egoname = NULL; 332 egoname = NULL;
339 } 333 }
340
341 //Return all egos 334 //Return all egos
342 for (ego_entry = handle->ego_head; 335 for (ego_entry = handle->ego_head;
343 NULL != ego_entry; 336 NULL != ego_entry;
@@ -355,7 +348,12 @@ ego_info_response (struct RestConnectionDataHandle *con,
355 GNUNET_free (keystring); 348 GNUNET_free (keystring);
356 GNUNET_REST_jsonapi_object_resource_add (json_object, json_resource); 349 GNUNET_REST_jsonapi_object_resource_add (json_object, json_resource);
357 } 350 }
358 351 if (0 == GNUNET_REST_jsonapi_object_resource_count (json_object))
352 {
353 GNUNET_REST_jsonapi_object_delete (json_object);
354 GNUNET_SCHEDULER_add_now (&do_error, handle);
355 return;
356 }
359 GNUNET_REST_jsonapi_data_serialize (json_object, &result_str); 357 GNUNET_REST_jsonapi_data_serialize (json_object, &result_str);
360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str); 358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str);
361 resp = GNUNET_REST_create_json_response (result_str); 359 resp = GNUNET_REST_create_json_response (result_str);
@@ -430,7 +428,7 @@ ego_create_cont (struct RestConnectionDataHandle *con,
430 char term_data[handle->data_size+1]; 428 char term_data[handle->data_size+1];
431 const char* egoname; 429 const char* egoname;
432 430
433 if (strlen (API_NAMESPACE) != strlen (handle->url)) 431 if (strlen (GNUNET_REST_API_NS_IDENTITY) != strlen (handle->url))
434 { 432 {
435 GNUNET_SCHEDULER_add_now (&do_error, handle); 433 GNUNET_SCHEDULER_add_now (&do_error, handle);
436 return; 434 return;
@@ -513,13 +511,13 @@ subsys_set_cont (struct RestConnectionDataHandle *con,
513 int ego_exists = GNUNET_NO; 511 int ego_exists = GNUNET_NO;
514 json_t *subsys_json; 512 json_t *subsys_json;
515 513
516 if (strlen (API_NAMESPACE) > strlen (handle->url)) 514 if (strlen (GNUNET_REST_API_NS_IDENTITY) > strlen (handle->url))
517 { 515 {
518 GNUNET_SCHEDULER_add_now (&do_error, handle); 516 GNUNET_SCHEDULER_add_now (&do_error, handle);
519 return; 517 return;
520 } 518 }
521 519
522 egoname = &handle->url[strlen(EGO_NAMESPACE)+1]; 520 egoname = &handle->url[strlen(GNUNET_REST_API_NS_IDENTITY)+1];
523 for (ego_entry = handle->ego_head; 521 for (ego_entry = handle->ego_head;
524 NULL != ego_entry; 522 NULL != ego_entry;
525 ego_entry = ego_entry->next) 523 ego_entry = ego_entry->next)
@@ -605,13 +603,13 @@ ego_delete_cont (struct RestConnectionDataHandle *con_handle,
605 struct RequestHandle *handle = cls; 603 struct RequestHandle *handle = cls;
606 int ego_exists = GNUNET_NO; 604 int ego_exists = GNUNET_NO;
607 605
608 if (strlen (API_NAMESPACE) >= strlen (handle->url)) 606 if (strlen (GNUNET_REST_API_NS_IDENTITY) >= strlen (handle->url))
609 { 607 {
610 GNUNET_SCHEDULER_add_now (&do_error, handle); 608 GNUNET_SCHEDULER_add_now (&do_error, handle);
611 return; 609 return;
612 } 610 }
613 611
614 egoname = &handle->url[strlen(API_NAMESPACE)+1]; 612 egoname = &handle->url[strlen(GNUNET_REST_API_NS_IDENTITY)+1];
615 for (ego_entry = handle->ego_head; 613 for (ego_entry = handle->ego_head;
616 NULL != ego_entry; 614 NULL != ego_entry;
617 ego_entry = ego_entry->next) 615 ego_entry = ego_entry->next)
@@ -640,10 +638,10 @@ void
640init_cont (struct RequestHandle *handle) 638init_cont (struct RequestHandle *handle)
641{ 639{
642 static const struct GNUNET_REST_RestConnectionHandler handlers[] = { 640 static const struct GNUNET_REST_RestConnectionHandler handlers[] = {
643 {MHD_HTTP_METHOD_GET, API_NAMESPACE, &ego_info_response}, 641 {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_IDENTITY, &ego_info_response},
644 {MHD_HTTP_METHOD_POST, API_NAMESPACE, &ego_create_cont}, 642 {MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_IDENTITY, &ego_create_cont},
645 {MHD_HTTP_METHOD_PUT, API_NAMESPACE, &subsys_set_cont}, 643 {MHD_HTTP_METHOD_PUT, GNUNET_REST_API_NS_IDENTITY, &subsys_set_cont},
646 {MHD_HTTP_METHOD_DELETE, API_NAMESPACE, &ego_delete_cont}, 644 {MHD_HTTP_METHOD_DELETE, GNUNET_REST_API_NS_IDENTITY, &ego_delete_cont},
647 GNUNET_REST_HANDLER_END 645 GNUNET_REST_HANDLER_END
648 }; 646 };
649 647
@@ -775,7 +773,7 @@ libgnunet_plugin_rest_identity_init (void *cls)
775 plugin.cfg = cfg; 773 plugin.cfg = cfg;
776 api = GNUNET_new (struct GNUNET_REST_Plugin); 774 api = GNUNET_new (struct GNUNET_REST_Plugin);
777 api->cls = &plugin; 775 api->cls = &plugin;
778 api->name = API_NAMESPACE; 776 api->name = GNUNET_REST_API_NS_IDENTITY;
779 api->process_request = &rest_identity_process_request; 777 api->process_request = &rest_identity_process_request;
780 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 778 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
781 _("Identity REST API initialized\n")); 779 _("Identity REST API initialized\n"));
diff --git a/src/rest/gnunet-rest-server.c b/src/rest/gnunet-rest-server.c
index 84fc69d02..aa95e48b3 100644
--- a/src/rest/gnunet-rest-server.c
+++ b/src/rest/gnunet-rest-server.c
@@ -292,7 +292,7 @@ create_response (void *cls,
292 } 292 }
293 if (NULL == con_handle->plugin) 293 if (NULL == con_handle->plugin)
294 { 294 {
295 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
296 "Queueing response with MHD\n"); 296 "Queueing response with MHD\n");
297 GNUNET_free (con_handle); 297 GNUNET_free (con_handle);
298 return MHD_queue_response (con, 298 return MHD_queue_response (con,
@@ -356,15 +356,6 @@ mhd_completed_cb (void *cls,
356 void **con_cls, 356 void **con_cls,
357 enum MHD_RequestTerminationCode toe) 357 enum MHD_RequestTerminationCode toe)
358{ 358{
359 struct MhdConnectionHandle *con_handle;
360
361 con_handle = *con_cls;
362 if (NULL != con_handle)
363 {
364 MHD_destroy_response (con_handle->response);
365 GNUNET_free (con_handle);
366 *con_cls = NULL;
367 }
368 if (MHD_REQUEST_TERMINATED_COMPLETED_OK != toe) 359 if (MHD_REQUEST_TERMINATED_COMPLETED_OK != toe)
369 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 360 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
370 "MHD encountered error handling request: %d\n", 361 "MHD encountered error handling request: %d\n",
diff --git a/src/rest/rest.c b/src/rest/rest.c
index d6fb8a2f3..f3251db1a 100644
--- a/src/rest/rest.c
+++ b/src/rest/rest.c
@@ -91,6 +91,8 @@ GNUNET_REST_jsonapi_resource_new (const char *type, const char *id)
91 return NULL; 91 return NULL;
92 92
93 res = GNUNET_new (struct JsonApiResource); 93 res = GNUNET_new (struct JsonApiResource);
94 res->prev = NULL;
95 res->next = NULL;
94 96
95 res->res_obj = json_object (); 97 res->res_obj = json_object ();
96 98
@@ -236,6 +238,8 @@ add_json_resource (struct JsonApiObject *obj,
236 return; 238 return;
237 239
238 res = GNUNET_new (struct JsonApiResource); 240 res = GNUNET_new (struct JsonApiResource);
241 res->next = NULL;
242 res->prev = NULL;
239 res->res_obj = json_deep_copy (res_json); 243 res->res_obj = json_deep_copy (res_json);
240 GNUNET_REST_jsonapi_object_resource_add (obj, res); 244 GNUNET_REST_jsonapi_object_resource_add (obj, res);
241} 245}
@@ -303,11 +307,18 @@ void
303GNUNET_REST_jsonapi_object_delete (struct JsonApiObject *resp) 307GNUNET_REST_jsonapi_object_delete (struct JsonApiObject *resp)
304{ 308{
305 struct JsonApiResource *res; 309 struct JsonApiResource *res;
310 struct JsonApiResource *res_next;
306 311
307 for (res = resp->res_list_head; 312 for (res = resp->res_list_head;
308 res != NULL; 313 res != NULL;)
309 res = res->next) 314 {
315 GNUNET_CONTAINER_DLL_remove (resp->res_list_head,
316 resp->res_list_tail,
317 res);
318 res_next = res->next;
310 GNUNET_REST_jsonapi_resource_delete (res); 319 GNUNET_REST_jsonapi_resource_delete (res);
320 res = res_next;
321 }
311 GNUNET_free (resp); 322 GNUNET_free (resp);
312} 323}
313 324
@@ -420,7 +431,7 @@ GNUNET_REST_jsonapi_data_serialize (const struct JsonApiObject *resp,
420 } 431 }
421 json_object_set (root_json, GNUNET_REST_JSONAPI_KEY_DATA, res_arr); 432 json_object_set (root_json, GNUNET_REST_JSONAPI_KEY_DATA, res_arr);
422 } 433 }
423 *result = json_dumps (root_json, JSON_COMPACT); 434 *result = json_dumps (root_json, JSON_INDENT(2));
424 return GNUNET_OK; 435 return GNUNET_OK;
425} 436}
426 437
@@ -454,14 +465,17 @@ GNUNET_REST_namespace_match (const char *url, const char *namespace)
454 * @param data JSON result 465 * @param data JSON result
455 * @retun MHD response 466 * @retun MHD response
456 */ 467 */
457 struct MHD_Response* 468struct MHD_Response*
458GNUNET_REST_create_json_response (const char *data) 469GNUNET_REST_create_json_response (const char *data)
459{ 470{
460 struct MHD_Response *resp; 471 struct MHD_Response *resp;
461 size_t len; 472 size_t len;
462 473
463 if (NULL == data) 474 if (NULL == data)
475 {
464 len = 0; 476 len = 0;
477 data = "";
478 }
465 else 479 else
466 len = strlen (data); 480 len = strlen (data);
467 resp = MHD_create_response_from_buffer (len, 481 resp = MHD_create_response_from_buffer (len,
@@ -482,24 +496,22 @@ GNUNET_REST_handle_request (struct RestConnectionDataHandle *conn,
482 char *url; 496 char *url;
483 497
484 count = 0; 498 count = 0;
485
486 while (NULL != handlers[count].method) 499 while (NULL != handlers[count].method)
487 count++; 500 count++;
488 501
489 GNUNET_asprintf (&url, "%s", conn->url); 502 GNUNET_asprintf (&url, "%s", conn->url);
490 if (url[strlen (url)-1] == '/') 503 if (url[strlen (url)-1] == '/')
491 url[strlen (url)-1] = '\0'; 504 url[strlen (url)-1] = '\0';
492
493 for (i = 0; i < count; i++) 505 for (i = 0; i < count; i++)
494 { 506 {
495 if (0 != strcasecmp (conn->method, handlers[count].method)) 507 if (0 != strcasecmp (conn->method, handlers[i].method))
496 break; 508 break;
497 if (strlen (url) < strlen (handlers[count].namespace)) 509 if (strlen (url) < strlen (handlers[i].namespace))
498 break; 510 break;
499 if (GNUNET_NO == GNUNET_REST_namespace_match (url, handlers[count].namespace)) 511 if (GNUNET_NO == GNUNET_REST_namespace_match (url, handlers[i].namespace))
500 break; 512 break;
501 //Match 513 //Match
502 handlers[count].proc (conn, (const char*)url, cls); 514 handlers[i].proc (conn, (const char*)url, cls);
503 GNUNET_free (url); 515 GNUNET_free (url);
504 return GNUNET_YES; 516 return GNUNET_YES;
505 } 517 }