aboutsummaryrefslogtreecommitdiff
path: root/src/set
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2016-06-18 18:49:13 +0000
committerChristian Grothoff <christian@grothoff.org>2016-06-18 18:49:13 +0000
commit710e8923105dfcc38d8aefed19e6da670db9c440 (patch)
treec6e46d41beedcf3ac2b13a1e040a790db8aa794a /src/set
parent5dfc382a9f8467cd15b6a0c3a6e022fca9fc4a30 (diff)
downloadgnunet-710e8923105dfcc38d8aefed19e6da670db9c440.tar.gz
gnunet-710e8923105dfcc38d8aefed19e6da670db9c440.zip
partial refactoring, will cause FTBFS, to be completed ASAP
Diffstat (limited to 'src/set')
-rw-r--r--src/set/set_api.c195
1 files changed, 114 insertions, 81 deletions
diff --git a/src/set/set_api.c b/src/set/set_api.c
index 794ae57b7..343b5f881 100644
--- a/src/set/set_api.c
+++ b/src/set/set_api.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 Copyright (C) 2012-2014 GNUnet e.V. 3 Copyright (C) 2012-2016 GNUnet e.V.
4 4
5 GNUnet is free software; you can redistribute it and/or modify 5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published 6 it under the terms of the GNU General Public License as published
@@ -243,7 +243,7 @@ struct GNUNET_SET_ListenHandle
243static struct GNUNET_SET_Handle * 243static struct GNUNET_SET_Handle *
244create_internal (const struct GNUNET_CONFIGURATION_Handle *cfg, 244create_internal (const struct GNUNET_CONFIGURATION_Handle *cfg,
245 enum GNUNET_SET_OperationType op, 245 enum GNUNET_SET_OperationType op,
246 uint32_t *cookie); 246 const uint32_t *cookie);
247 247
248 248
249/** 249/**
@@ -251,21 +251,17 @@ create_internal (const struct GNUNET_CONFIGURATION_Handle *cfg,
251 * iterator and sends an acknowledgement to the service. 251 * iterator and sends an acknowledgement to the service.
252 * 252 *
253 * @param cls the `struct GNUNET_SET_Handle *` 253 * @param cls the `struct GNUNET_SET_Handle *`
254 * @param mh the message 254 * @param msg the message
255 */ 255 */
256static void 256static void
257handle_copy_lazy (void *cls, 257handle_copy_lazy (void *cls,
258 const struct GNUNET_MessageHeader *mh) 258 const struct GNUNET_SET_CopyLazyResponseMessage *msg)
259{ 259{
260 struct GNUNET_SET_CopyLazyResponseMessage *msg;
261 struct GNUNET_SET_Handle *set = cls; 260 struct GNUNET_SET_Handle *set = cls;
262 struct SetCopyRequest *req; 261 struct SetCopyRequest *req;
263 struct GNUNET_SET_Handle *new_set; 262 struct GNUNET_SET_Handle *new_set;
264 263
265 msg = (struct GNUNET_SET_CopyLazyResponseMessage *) mh;
266
267 req = set->copy_req_head; 264 req = set->copy_req_head;
268
269 if (NULL == req) 265 if (NULL == req)
270 { 266 {
271 /* Service sent us unsolicited lazy copy response */ 267 /* Service sent us unsolicited lazy copy response */
@@ -275,53 +271,54 @@ handle_copy_lazy (void *cls,
275 271
276 LOG (GNUNET_ERROR_TYPE_DEBUG, 272 LOG (GNUNET_ERROR_TYPE_DEBUG,
277 "Handling response to lazy copy\n"); 273 "Handling response to lazy copy\n");
278
279 GNUNET_CONTAINER_DLL_remove (set->copy_req_head, 274 GNUNET_CONTAINER_DLL_remove (set->copy_req_head,
280 set->copy_req_tail, 275 set->copy_req_tail,
281 req); 276 req);
282
283
284 // We pass none as operation here, since it doesn't matter when 277 // We pass none as operation here, since it doesn't matter when
285 // cloning. 278 // cloning.
286 new_set = create_internal (set->cfg, GNUNET_SET_OPERATION_NONE, &msg->cookie); 279 new_set = create_internal (set->cfg,
287 280 GNUNET_SET_OPERATION_NONE,
281 &msg->cookie);
288 req->cb (req->cls, new_set); 282 req->cb (req->cls, new_set);
289
290 GNUNET_free (req); 283 GNUNET_free (req);
291} 284}
292 285
293 286
294/** 287/**
288 * Check that the given @a msg is well-formed.
289 *
290 * @param cls closure
291 * @param msg message to check
292 * @return #GNUNET_OK if message is well-formed
293 */
294static int
295check_iter_element (void *cls,
296 const struct GNUNET_SET_IterResponseMessage *msg)
297{
298 /* minimum size was already checked, everything else is OK! */
299 return GNUNET_OK;
300}
301
302
303/**
295 * Handle element for iteration over the set. Notifies the 304 * Handle element for iteration over the set. Notifies the
296 * iterator and sends an acknowledgement to the service. 305 * iterator and sends an acknowledgement to the service.
297 * 306 *
298 * @param cls the `struct GNUNET_SET_Handle *` 307 * @param cls the `struct GNUNET_SET_Handle *`
299 * @param mh the message 308 * @param mh the message
300 */ 309 */
301static void 310 static void
302handle_iter_element (void *cls, 311 handle_iter_element (void *cls,
303 const struct GNUNET_MessageHeader *mh) 312 const struct GNUNET_SET_IterResponseMessage *msg)
304{ 313{
305 struct GNUNET_SET_Handle *set = cls; 314 struct GNUNET_SET_Handle *set = cls;
306 GNUNET_SET_ElementIterator iter = set->iterator; 315 GNUNET_SET_ElementIterator iter = set->iterator;
307 struct GNUNET_SET_Element element; 316 struct GNUNET_SET_Element element;
308 const struct GNUNET_SET_IterResponseMessage *msg;
309 struct GNUNET_SET_IterAckMessage *ack_msg; 317 struct GNUNET_SET_IterAckMessage *ack_msg;
310 struct GNUNET_MQ_Envelope *ev; 318 struct GNUNET_MQ_Envelope *ev;
311 uint16_t msize; 319 uint16_t msize;
312 320
313 msize = ntohs (mh->size); 321 msize = ntohs (msg->header.size);
314 if (msize < sizeof (sizeof (struct GNUNET_SET_IterResponseMessage)))
315 {
316 /* message malformed */
317 GNUNET_break (0);
318 set->iterator = NULL;
319 set->iteration_id++;
320 iter (set->iterator_cls,
321 NULL);
322 iter = NULL;
323 }
324 msg = (const struct GNUNET_SET_IterResponseMessage *) mh;
325 if (set->iteration_id != ntohs (msg->iteration_id)) 322 if (set->iteration_id != ntohs (msg->iteration_id))
326 { 323 {
327 /* element from a previous iteration, skip! */ 324 /* element from a previous iteration, skip! */
@@ -366,6 +363,22 @@ handle_iter_done (void *cls,
366 363
367 364
368/** 365/**
366 * Check that the given @a msg is well-formed.
367 *
368 * @param cls closure
369 * @param msg message to check
370 * @return #GNUNET_OK if message is well-formed
371 */
372static int
373check_result (void *cls,
374 const struct GNUNET_SET_ResultMessage *msg)
375{
376 /* minimum size was already checked, everything else is OK! */
377 return GNUNET_OK;
378}
379
380
381/**
369 * Handle result message for a set operation. 382 * Handle result message for a set operation.
370 * 383 *
371 * @param cls the set 384 * @param cls the set
@@ -373,15 +386,13 @@ handle_iter_done (void *cls,
373 */ 386 */
374static void 387static void
375handle_result (void *cls, 388handle_result (void *cls,
376 const struct GNUNET_MessageHeader *mh) 389 const struct GNUNET_SET_ResultMessage *msg)
377{ 390{
378 struct GNUNET_SET_Handle *set = cls; 391 struct GNUNET_SET_Handle *set = cls;
379 const struct GNUNET_SET_ResultMessage *msg;
380 struct GNUNET_SET_OperationHandle *oh; 392 struct GNUNET_SET_OperationHandle *oh;
381 struct GNUNET_SET_Element e; 393 struct GNUNET_SET_Element e;
382 enum GNUNET_SET_Status result_status; 394 enum GNUNET_SET_Status result_status;
383 395
384 msg = (const struct GNUNET_SET_ResultMessage *) mh;
385 GNUNET_assert (NULL != set->mq); 396 GNUNET_assert (NULL != set->mq);
386 result_status = ntohs (msg->result_status); 397 result_status = ntohs (msg->result_status);
387 LOG (GNUNET_ERROR_TYPE_DEBUG, 398 LOG (GNUNET_ERROR_TYPE_DEBUG,
@@ -442,7 +453,7 @@ do_element:
442 LOG (GNUNET_ERROR_TYPE_DEBUG, 453 LOG (GNUNET_ERROR_TYPE_DEBUG,
443 "Treating result as element\n"); 454 "Treating result as element\n");
444 e.data = &msg[1]; 455 e.data = &msg[1];
445 e.size = ntohs (mh->size) - sizeof (struct GNUNET_SET_ResultMessage); 456 e.size = ntohs (msg->header.size) - sizeof (struct GNUNET_SET_ResultMessage);
446 e.element_type = ntohs (msg->element_type); 457 e.element_type = ntohs (msg->element_type);
447 if (NULL != oh->result_cb) 458 if (NULL != oh->result_cb)
448 oh->result_cb (oh->result_cls, 459 oh->result_cb (oh->result_cls,
@@ -522,7 +533,8 @@ handle_client_set_error (void *cls,
522 enum GNUNET_MQ_Error error) 533 enum GNUNET_MQ_Error error)
523{ 534{
524 struct GNUNET_SET_Handle *set = cls; 535 struct GNUNET_SET_Handle *set = cls;
525 536 GNUNET_SET_ElementIterator iter = set->iterator;
537
526 LOG (GNUNET_ERROR_TYPE_DEBUG, 538 LOG (GNUNET_ERROR_TYPE_DEBUG,
527 "Handling client set error %d\n", 539 "Handling client set error %d\n",
528 error); 540 error);
@@ -534,6 +546,11 @@ handle_client_set_error (void *cls,
534 GNUNET_SET_STATUS_FAILURE); 546 GNUNET_SET_STATUS_FAILURE);
535 set_operation_destroy (set->ops_head); 547 set_operation_destroy (set->ops_head);
536 } 548 }
549 set->iterator = NULL;
550 set->iteration_id++;
551 if (NULL != iter)
552 iter (set->iterator_cls,
553 NULL);
537 set->invalid = GNUNET_YES; 554 set->invalid = GNUNET_YES;
538 if (GNUNET_YES == set->destroy_requested) 555 if (GNUNET_YES == set->destroy_requested)
539 { 556 {
@@ -547,31 +564,34 @@ handle_client_set_error (void *cls,
547static struct GNUNET_SET_Handle * 564static struct GNUNET_SET_Handle *
548create_internal (const struct GNUNET_CONFIGURATION_Handle *cfg, 565create_internal (const struct GNUNET_CONFIGURATION_Handle *cfg,
549 enum GNUNET_SET_OperationType op, 566 enum GNUNET_SET_OperationType op,
550 uint32_t *cookie) 567 const uint32_t *cookie)
551{ 568{
552 static const struct GNUNET_MQ_MessageHandler mq_handlers[] = { 569 GNUNET_MQ_hd_var_size (result,
553 { &handle_result, 570 GNUNET_MESSAGE_TYPE_SET_RESULT,
554 GNUNET_MESSAGE_TYPE_SET_RESULT, 571 struct GNUNET_SET_ResultMessage);
555 0 }, 572 GNUNET_MQ_hd_var_size (iter_element,
556 { &handle_iter_element, 573 GNUNET_MESSAGE_TYPE_SET_ITER_ELEMENT,
557 GNUNET_MESSAGE_TYPE_SET_ITER_ELEMENT, 574 struct GNUNET_SET_IterResponseMessage);
558 0 }, 575 GNUNET_MQ_hd_fixed_size (iter_done,
559 { &handle_iter_done, 576 GNUNET_MESSAGE_TYPE_SET_ITER_DONE,
560 GNUNET_MESSAGE_TYPE_SET_ITER_DONE, 577 struct GNUNET_MessageHeader);
561 sizeof (struct GNUNET_MessageHeader) }, 578 GNUNET_MQ_hd_fixed_size (copy_lazy,
562 { &handle_copy_lazy, 579 GNUNET_MESSAGE_TYPE_SET_COPY_LAZY_RESPONSE,
563 GNUNET_MESSAGE_TYPE_SET_COPY_LAZY_RESPONSE, 580 struct GNUNET_SET_CopyLazyResponseMessage);
564 sizeof (struct GNUNET_SET_CopyLazyResponseMessage) }, 581 struct GNUNET_SET_Handle *set = GNUNET_new (struct GNUNET_SET_Handle);
565 GNUNET_MQ_HANDLERS_END 582 struct GNUNET_MQ_MessageHandler mq_handlers[] = {
583 make_result_handler (set),
584 make_iter_element_handler (set),
585 make_iter_done_handler (set),
586 make_copy_lazy_handler (set),
587 GNUNET_MQ_handler_end ()
566 }; 588 };
567 struct GNUNET_SET_Handle *set;
568 struct GNUNET_MQ_Envelope *mqm; 589 struct GNUNET_MQ_Envelope *mqm;
569 struct GNUNET_SET_CreateMessage *create_msg; 590 struct GNUNET_SET_CreateMessage *create_msg;
570 struct GNUNET_SET_CopyLazyConnectMessage *copy_msg; 591 struct GNUNET_SET_CopyLazyConnectMessage *copy_msg;
571 592
572 set = GNUNET_new (struct GNUNET_SET_Handle);
573 set->client = GNUNET_CLIENT_connect ("set", cfg);
574 set->cfg = cfg; 593 set->cfg = cfg;
594 set->client = GNUNET_CLIENT_connect ("set", cfg);
575 if (NULL == set->client) 595 if (NULL == set->client)
576 { 596 {
577 GNUNET_free (set); 597 GNUNET_free (set);
@@ -796,41 +816,48 @@ listen_connect (void *cls);
796 816
797 817
798/** 818/**
819 * Check validity of request message for a listen operation
820 *
821 * @param cls the listen handle
822 * @param msg the message
823 * @return #GNUNET_OK if the message is well-formed
824 */
825static int
826check_request (void *cls,
827 const struct GNUNET_SET_RequestMessage *msg)
828{
829 const struct GNUNET_MessageHeader *context_msg;
830
831 context_msg = GNUNET_MQ_extract_nested_mh (msg);
832 if (NULL == context_msg)
833 {
834 GNUNET_break_op (0);
835 return GNUNET_SYSERR;
836 }
837 return GNUNET_OK;
838}
839
840
841/**
799 * Handle request message for a listen operation 842 * Handle request message for a listen operation
800 * 843 *
801 * @param cls the listen handle 844 * @param cls the listen handle
802 * @param mh the message 845 * @param msg the message
803 */ 846 */
804static void 847static void
805handle_request (void *cls, 848handle_request (void *cls,
806 const struct GNUNET_MessageHeader *mh) 849 const struct GNUNET_SET_RequestMessage *msg)
807{ 850{
808 struct GNUNET_SET_ListenHandle *lh = cls; 851 struct GNUNET_SET_ListenHandle *lh = cls;
809 const struct GNUNET_SET_RequestMessage *msg;
810 struct GNUNET_SET_Request req; 852 struct GNUNET_SET_Request req;
811 const struct GNUNET_MessageHeader *context_msg; 853 const struct GNUNET_MessageHeader *context_msg;
812 uint16_t msize;
813 struct GNUNET_MQ_Envelope *mqm; 854 struct GNUNET_MQ_Envelope *mqm;
814 struct GNUNET_SET_RejectMessage *rmsg; 855 struct GNUNET_SET_RejectMessage *rmsg;
815 856
816 LOG (GNUNET_ERROR_TYPE_DEBUG, 857 LOG (GNUNET_ERROR_TYPE_DEBUG,
817 "Processing incoming operation request\n"); 858 "Processing incoming operation request\n");
818 msize = ntohs (mh->size);
819 if (msize < sizeof (struct GNUNET_SET_RequestMessage))
820 {
821 GNUNET_break (0);
822 GNUNET_CLIENT_disconnect (lh->client);
823 lh->client = NULL;
824 GNUNET_MQ_destroy (lh->mq);
825 lh->mq = NULL;
826 lh->reconnect_task = GNUNET_SCHEDULER_add_delayed (lh->reconnect_backoff,
827 &listen_connect, lh);
828 lh->reconnect_backoff = GNUNET_TIME_STD_BACKOFF (lh->reconnect_backoff);
829 return;
830 }
831 /* we got another valid request => reset the backoff */ 859 /* we got another valid request => reset the backoff */
832 lh->reconnect_backoff = GNUNET_TIME_UNIT_MILLISECONDS; 860 lh->reconnect_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
833 msg = (const struct GNUNET_SET_RequestMessage *) mh;
834 req.accept_id = ntohl (msg->accept_id); 861 req.accept_id = ntohl (msg->accept_id);
835 req.accepted = GNUNET_NO; 862 req.accepted = GNUNET_NO;
836 context_msg = GNUNET_MQ_extract_nested_mh (msg); 863 context_msg = GNUNET_MQ_extract_nested_mh (msg);
@@ -871,7 +898,8 @@ handle_client_listener_error (void *cls,
871 GNUNET_MQ_destroy (lh->mq); 898 GNUNET_MQ_destroy (lh->mq);
872 lh->mq = NULL; 899 lh->mq = NULL;
873 lh->reconnect_task = GNUNET_SCHEDULER_add_delayed (lh->reconnect_backoff, 900 lh->reconnect_task = GNUNET_SCHEDULER_add_delayed (lh->reconnect_backoff,
874 &listen_connect, lh); 901 &listen_connect,
902 lh);
875 lh->reconnect_backoff = GNUNET_TIME_STD_BACKOFF (lh->reconnect_backoff); 903 lh->reconnect_backoff = GNUNET_TIME_STD_BACKOFF (lh->reconnect_backoff);
876} 904}
877 905
@@ -883,12 +911,15 @@ handle_client_listener_error (void *cls,
883 */ 911 */
884static void 912static void
885listen_connect (void *cls) 913listen_connect (void *cls)
886{ 914{
887 static const struct GNUNET_MQ_MessageHandler mq_handlers[] = { 915 GNUNET_MQ_hd_var_size (request,
888 { &handle_request, GNUNET_MESSAGE_TYPE_SET_REQUEST }, 916 GNUNET_MESSAGE_TYPE_SET_REQUEST,
889 GNUNET_MQ_HANDLERS_END 917 struct GNUNET_SET_RequestMessage);
890 };
891 struct GNUNET_SET_ListenHandle *lh = cls; 918 struct GNUNET_SET_ListenHandle *lh = cls;
919 struct GNUNET_MQ_MessageHandler mq_handlers[] = {
920 make_request_handler (lh),
921 GNUNET_MQ_handler_end ()
922 };
892 struct GNUNET_MQ_Envelope *mqm; 923 struct GNUNET_MQ_Envelope *mqm;
893 struct GNUNET_SET_ListenMessage *msg; 924 struct GNUNET_SET_ListenMessage *msg;
894 925
@@ -900,7 +931,8 @@ listen_connect (void *cls)
900 GNUNET_assert (NULL == lh->mq); 931 GNUNET_assert (NULL == lh->mq);
901 lh->mq = GNUNET_MQ_queue_for_connection_client (lh->client, 932 lh->mq = GNUNET_MQ_queue_for_connection_client (lh->client,
902 mq_handlers, 933 mq_handlers,
903 &handle_client_listener_error, lh); 934 &handle_client_listener_error,
935 lh);
904 mqm = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SET_LISTEN); 936 mqm = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SET_LISTEN);
905 msg->operation = htonl (lh->operation); 937 msg->operation = htonl (lh->operation);
906 msg->app_id = lh->app_id; 938 msg->app_id = lh->app_id;
@@ -1133,11 +1165,12 @@ GNUNET_SET_element_dup (const struct GNUNET_SET_Element *element)
1133 * Hash a set element. 1165 * Hash a set element.
1134 * 1166 *
1135 * @param element the element that should be hashed 1167 * @param element the element that should be hashed
1136 * @param ret_hash a pointer to where the hash of @a element 1168 * @param[out] ret_hash a pointer to where the hash of @a element
1137 * should be stored 1169 * should be stored
1138 */ 1170 */
1139void 1171void
1140GNUNET_SET_element_hash (const struct GNUNET_SET_Element *element, struct GNUNET_HashCode *ret_hash) 1172GNUNET_SET_element_hash (const struct GNUNET_SET_Element *element,
1173 struct GNUNET_HashCode *ret_hash)
1141{ 1174{
1142 struct GNUNET_HashContext *ctx = GNUNET_CRYPTO_hash_context_start (); 1175 struct GNUNET_HashContext *ctx = GNUNET_CRYPTO_hash_context_start ();
1143 1176