aboutsummaryrefslogtreecommitdiff
path: root/src/dht/dht_api.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/dht/dht_api.c')
-rw-r--r--src/dht/dht_api.c118
1 files changed, 55 insertions, 63 deletions
diff --git a/src/dht/dht_api.c b/src/dht/dht_api.c
index 101751487..eded50efc 100644
--- a/src/dht/dht_api.c
+++ b/src/dht/dht_api.c
@@ -211,8 +211,8 @@ static size_t transmit_pending (void *cls, size_t size, void *buf);
211 * a demultiplexer which handles numerous message types 211 * a demultiplexer which handles numerous message types
212 * 212 *
213 */ 213 */
214static void 214static void service_message_handler (void *cls,
215service_message_handler (void *cls, const struct GNUNET_MessageHeader *msg); 215 const struct GNUNET_MessageHeader *msg);
216 216
217 217
218 218
@@ -238,9 +238,8 @@ try_connect (struct GNUNET_DHT_Handle *handle)
238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
239 "Starting to process replies from DHT\n"); 239 "Starting to process replies from DHT\n");
240#endif 240#endif
241 GNUNET_CLIENT_receive (handle->client, 241 GNUNET_CLIENT_receive (handle->client, &service_message_handler, handle,
242 &service_message_handler, 242 GNUNET_TIME_UNIT_FOREVER_REL);
243 handle, GNUNET_TIME_UNIT_FOREVER_REL);
244 return GNUNET_YES; 243 return GNUNET_YES;
245} 244}
246 245
@@ -262,8 +261,8 @@ add_request_to_pending (void *cls, const GNUNET_HashCode * key, void *value)
262 261
263 if (GNUNET_NO == rh->message->in_pending_queue) 262 if (GNUNET_NO == rh->message->in_pending_queue)
264 { 263 {
265 GNUNET_CONTAINER_DLL_insert (handle->pending_head, 264 GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail,
266 handle->pending_tail, rh->message); 265 rh->message);
267 rh->message->in_pending_queue = GNUNET_YES; 266 rh->message->in_pending_queue = GNUNET_YES;
268 } 267 }
269 return GNUNET_YES; 268 return GNUNET_YES;
@@ -321,9 +320,8 @@ do_disconnect (struct GNUNET_DHT_Handle *handle)
321 GNUNET_assert (handle->reconnect_task == GNUNET_SCHEDULER_NO_TASK); 320 GNUNET_assert (handle->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
322 GNUNET_CLIENT_disconnect (handle->client, GNUNET_NO); 321 GNUNET_CLIENT_disconnect (handle->client, GNUNET_NO);
323 handle->client = NULL; 322 handle->client = NULL;
324 handle->reconnect_task = GNUNET_SCHEDULER_add_delayed (handle->retry_time, 323 handle->reconnect_task =
325 &try_reconnect, 324 GNUNET_SCHEDULER_add_delayed (handle->retry_time, &try_reconnect, handle);
326 handle);
327} 325}
328 326
329 327
@@ -344,11 +342,12 @@ process_pending_messages (struct GNUNET_DHT_Handle *handle)
344 return; 342 return;
345 if (NULL == (head = handle->pending_head)) 343 if (NULL == (head = handle->pending_head))
346 return; 344 return;
347 handle->th = GNUNET_CLIENT_notify_transmit_ready (handle->client, 345 handle->th =
348 ntohs (head->msg->size), 346 GNUNET_CLIENT_notify_transmit_ready (handle->client,
349 GNUNET_TIME_UNIT_FOREVER_REL, 347 ntohs (head->msg->size),
350 GNUNET_YES, 348 GNUNET_TIME_UNIT_FOREVER_REL,
351 &transmit_pending, handle); 349 GNUNET_YES, &transmit_pending,
350 handle);
352 if (NULL == handle->th) 351 if (NULL == handle->th)
353 { 352 {
354 do_disconnect (handle); 353 do_disconnect (handle);
@@ -385,8 +384,8 @@ transmit_pending (void *cls, size_t size, void *buf)
385 return 0; 384 return 0;
386 } 385 }
387 memcpy (buf, head->msg, tsize); 386 memcpy (buf, head->msg, tsize);
388 GNUNET_CONTAINER_DLL_remove (handle->pending_head, 387 GNUNET_CONTAINER_DLL_remove (handle->pending_head, handle->pending_tail,
389 handle->pending_tail, head); 388 head);
390 if (head->timeout_task != GNUNET_SCHEDULER_NO_TASK) 389 if (head->timeout_task != GNUNET_SCHEDULER_NO_TASK)
391 { 390 {
392 GNUNET_SCHEDULER_cancel (head->timeout_task); 391 GNUNET_SCHEDULER_cancel (head->timeout_task);
@@ -394,8 +393,7 @@ transmit_pending (void *cls, size_t size, void *buf)
394 } 393 }
395 if (NULL != head->cont) 394 if (NULL != head->cont)
396 { 395 {
397 GNUNET_SCHEDULER_add_continuation (head->cont, 396 GNUNET_SCHEDULER_add_continuation (head->cont, head->cont_cls,
398 head->cont_cls,
399 GNUNET_SCHEDULER_REASON_PREREQ_DONE); 397 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
400 head->cont = NULL; 398 head->cont = NULL;
401 head->cont_cls = NULL; 399 head->cont_cls = NULL;
@@ -514,13 +512,12 @@ service_message_handler (void *cls, const struct GNUNET_MessageHeader *msg)
514 GNUNET_h2s (&dht_msg->key), 512 GNUNET_h2s (&dht_msg->key),
515 GNUNET_CONTAINER_multihashmap_size (handle->active_requests)); 513 GNUNET_CONTAINER_multihashmap_size (handle->active_requests));
516 GNUNET_CONTAINER_multihashmap_get_multiple (handle->active_requests, 514 GNUNET_CONTAINER_multihashmap_get_multiple (handle->active_requests,
517 &dht_msg->key, 515 &dht_msg->key, &process_reply,
518 &process_reply, (void *) dht_msg); 516 (void *) dht_msg);
519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
520 "Continuing to process replies from DHT\n"); 518 "Continuing to process replies from DHT\n");
521 GNUNET_CLIENT_receive (handle->client, 519 GNUNET_CLIENT_receive (handle->client, &service_message_handler, handle,
522 &service_message_handler, 520 GNUNET_TIME_UNIT_FOREVER_REL);
523 handle, GNUNET_TIME_UNIT_FOREVER_REL);
524 521
525} 522}
526 523
@@ -574,14 +571,13 @@ GNUNET_DHT_disconnect (struct GNUNET_DHT_Handle *handle)
574 } 571 }
575 while (NULL != (pm = handle->pending_head)) 572 while (NULL != (pm = handle->pending_head))
576 { 573 {
577 GNUNET_CONTAINER_DLL_remove (handle->pending_head, 574 GNUNET_CONTAINER_DLL_remove (handle->pending_head, handle->pending_tail,
578 handle->pending_tail, pm); 575 pm);
579 GNUNET_assert (GNUNET_YES == pm->free_on_send); 576 GNUNET_assert (GNUNET_YES == pm->free_on_send);
580 if (GNUNET_SCHEDULER_NO_TASK != pm->timeout_task) 577 if (GNUNET_SCHEDULER_NO_TASK != pm->timeout_task)
581 GNUNET_SCHEDULER_cancel (pm->timeout_task); 578 GNUNET_SCHEDULER_cancel (pm->timeout_task);
582 if (NULL != pm->cont) 579 if (NULL != pm->cont)
583 GNUNET_SCHEDULER_add_continuation (pm->cont, 580 GNUNET_SCHEDULER_add_continuation (pm->cont, pm->cont_cls,
584 pm->cont_cls,
585 GNUNET_SCHEDULER_REASON_TIMEOUT); 581 GNUNET_SCHEDULER_REASON_TIMEOUT);
586 pm->in_pending_queue = GNUNET_NO; 582 pm->in_pending_queue = GNUNET_NO;
587 GNUNET_free (pm); 583 GNUNET_free (pm);
@@ -622,8 +618,8 @@ timeout_route_request (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
622 return; 618 return;
623 } 619 }
624 handle = pending->handle; 620 handle = pending->handle;
625 GNUNET_CONTAINER_DLL_remove (handle->pending_head, 621 GNUNET_CONTAINER_DLL_remove (handle->pending_head, handle->pending_tail,
626 handle->pending_tail, pending); 622 pending);
627 if (pending->cont != NULL) 623 if (pending->cont != NULL)
628 pending->cont (pending->cont_cls, tc); 624 pending->cont (pending->cont_cls, tc);
629 GNUNET_free (pending); 625 GNUNET_free (pending);
@@ -658,8 +654,7 @@ GNUNET_DHT_route_start (struct GNUNET_DHT_Handle *handle,
658 enum GNUNET_DHT_RouteOption options, 654 enum GNUNET_DHT_RouteOption options,
659 const struct GNUNET_MessageHeader *enc, 655 const struct GNUNET_MessageHeader *enc,
660 struct GNUNET_TIME_Relative timeout, 656 struct GNUNET_TIME_Relative timeout,
661 GNUNET_DHT_ReplyProcessor iter, 657 GNUNET_DHT_ReplyProcessor iter, void *iter_cls,
662 void *iter_cls,
663 GNUNET_SCHEDULER_Task cont, void *cont_cls) 658 GNUNET_SCHEDULER_Task cont, void *cont_cls)
664{ 659{
665 struct PendingMessage *pending; 660 struct PendingMessage *pending;
@@ -702,8 +697,7 @@ GNUNET_DHT_route_start (struct GNUNET_DHT_Handle *handle,
702 route_handle->dht_handle = handle; 697 route_handle->dht_handle = handle;
703 route_handle->uid = handle->uid_gen; 698 route_handle->uid = handle->uid_gen;
704 route_handle->message = pending; 699 route_handle->message = pending;
705 GNUNET_CONTAINER_multihashmap_put (handle->active_requests, 700 GNUNET_CONTAINER_multihashmap_put (handle->active_requests, key,
706 key,
707 route_handle, 701 route_handle,
708 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE); 702 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
709 } 703 }
@@ -711,12 +705,11 @@ GNUNET_DHT_route_start (struct GNUNET_DHT_Handle *handle,
711 { 705 {
712 route_handle = NULL; 706 route_handle = NULL;
713 pending->free_on_send = GNUNET_YES; 707 pending->free_on_send = GNUNET_YES;
714 pending->timeout_task = GNUNET_SCHEDULER_add_delayed (timeout, 708 pending->timeout_task =
715 &timeout_route_request, 709 GNUNET_SCHEDULER_add_delayed (timeout, &timeout_route_request, pending);
716 pending);
717 } 710 }
718 GNUNET_CONTAINER_DLL_insert (handle->pending_head, 711 GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail,
719 handle->pending_tail, pending); 712 pending);
720 pending->in_pending_queue = GNUNET_YES; 713 pending->in_pending_queue = GNUNET_YES;
721 process_pending_messages (handle); 714 process_pending_messages (handle);
722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
@@ -755,21 +748,23 @@ GNUNET_DHT_route_stop (struct GNUNET_DHT_RouteHandle *route_handle)
755 pending->handle = handle; 748 pending->handle = handle;
756 pending->free_on_send = GNUNET_YES; 749 pending->free_on_send = GNUNET_YES;
757 pending->in_pending_queue = GNUNET_YES; 750 pending->in_pending_queue = GNUNET_YES;
758 GNUNET_CONTAINER_DLL_insert (handle->pending_head, 751 GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail,
759 handle->pending_tail, pending); 752 pending);
760 process_pending_messages (handle); 753 process_pending_messages (handle);
761 } 754 }
762 else 755 else
763 { 756 {
764 /* simply remove pending request from message queue before 757 /* simply remove pending request from message queue before
765 * transmission, no need to transmit STOP request! */ 758 * transmission, no need to transmit STOP request! */
766 GNUNET_CONTAINER_DLL_remove (handle->pending_head, 759 GNUNET_CONTAINER_DLL_remove (handle->pending_head, handle->pending_tail,
767 handle->pending_tail, route_handle->message); 760 route_handle->message);
768 } 761 }
769 GNUNET_assert (GNUNET_YES == 762 GNUNET_assert (GNUNET_YES ==
770 GNUNET_CONTAINER_multihashmap_remove 763 GNUNET_CONTAINER_multihashmap_remove (route_handle->
771 (route_handle->dht_handle->active_requests, &route_handle->key, 764 dht_handle->
772 route_handle)); 765 active_requests,
766 &route_handle->key,
767 route_handle));
773 GNUNET_free (route_handle->message); 768 GNUNET_free (route_handle->message);
774 GNUNET_free (route_handle); 769 GNUNET_free (route_handle);
775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "DHT route stop request processed\n"); 770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "DHT route stop request processed\n");
@@ -790,16 +785,16 @@ GNUNET_DHT_route_stop (struct GNUNET_DHT_RouteHandle *route_handle)
790 * @param cont_cls closure for cont 785 * @param cont_cls closure for cont
791 */ 786 */
792static void 787static void
793send_control_message (struct GNUNET_DHT_Handle *handle, 788send_control_message (struct GNUNET_DHT_Handle *handle, uint16_t command,
794 uint16_t command, 789 uint16_t variable, GNUNET_SCHEDULER_Task cont,
795 uint16_t variable, 790 void *cont_cls)
796 GNUNET_SCHEDULER_Task cont, void *cont_cls)
797{ 791{
798 struct GNUNET_DHT_ControlMessage *msg; 792 struct GNUNET_DHT_ControlMessage *msg;
799 struct PendingMessage *pending; 793 struct PendingMessage *pending;
800 794
801 pending = GNUNET_malloc (sizeof (struct PendingMessage) + 795 pending =
802 sizeof (struct GNUNET_DHT_ControlMessage)); 796 GNUNET_malloc (sizeof (struct PendingMessage) +
797 sizeof (struct GNUNET_DHT_ControlMessage));
803 msg = (struct GNUNET_DHT_ControlMessage *) &pending[1]; 798 msg = (struct GNUNET_DHT_ControlMessage *) &pending[1];
804 pending->msg = &msg->header; 799 pending->msg = &msg->header;
805 msg->header.size = htons (sizeof (struct GNUNET_DHT_ControlMessage)); 800 msg->header.size = htons (sizeof (struct GNUNET_DHT_ControlMessage));
@@ -810,8 +805,8 @@ send_control_message (struct GNUNET_DHT_Handle *handle,
810 pending->cont = cont; 805 pending->cont = cont;
811 pending->cont_cls = cont_cls; 806 pending->cont_cls = cont_cls;
812 pending->in_pending_queue = GNUNET_YES; 807 pending->in_pending_queue = GNUNET_YES;
813 GNUNET_CONTAINER_DLL_insert (handle->pending_head, 808 GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail,
814 handle->pending_tail, pending); 809 pending);
815 process_pending_messages (handle); 810 process_pending_messages (handle);
816} 811}
817 812
@@ -831,8 +826,8 @@ void
831GNUNET_DHT_find_peers (struct GNUNET_DHT_Handle *handle, 826GNUNET_DHT_find_peers (struct GNUNET_DHT_Handle *handle,
832 GNUNET_SCHEDULER_Task cont, void *cont_cls) 827 GNUNET_SCHEDULER_Task cont, void *cont_cls)
833{ 828{
834 send_control_message (handle, 829 send_control_message (handle, GNUNET_MESSAGE_TYPE_DHT_FIND_PEER, 0, cont,
835 GNUNET_MESSAGE_TYPE_DHT_FIND_PEER, 0, cont, cont_cls); 830 cont_cls);
836} 831}
837 832
838 833
@@ -858,8 +853,7 @@ GNUNET_DHT_set_malicious_getter (struct GNUNET_DHT_Handle *handle,
858 GNUNET_break (0); 853 GNUNET_break (0);
859 return; 854 return;
860 } 855 }
861 send_control_message (handle, 856 send_control_message (handle, GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_GET,
862 GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_GET,
863 frequency.rel_value, cont, cont_cls); 857 frequency.rel_value, cont, cont_cls);
864} 858}
865 859
@@ -883,8 +877,7 @@ GNUNET_DHT_set_malicious_putter (struct GNUNET_DHT_Handle *handle,
883 return; 877 return;
884 } 878 }
885 879
886 send_control_message (handle, 880 send_control_message (handle, GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_PUT,
887 GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_PUT,
888 frequency.rel_value, cont, cont_cls); 881 frequency.rel_value, cont, cont_cls);
889} 882}
890 883
@@ -902,9 +895,8 @@ void
902GNUNET_DHT_set_malicious_dropper (struct GNUNET_DHT_Handle *handle, 895GNUNET_DHT_set_malicious_dropper (struct GNUNET_DHT_Handle *handle,
903 GNUNET_SCHEDULER_Task cont, void *cont_cls) 896 GNUNET_SCHEDULER_Task cont, void *cont_cls)
904{ 897{
905 send_control_message (handle, 898 send_control_message (handle, GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_DROP, 0, cont,
906 GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_DROP, 0, 899 cont_cls);
907 cont, cont_cls);
908} 900}
909 901
910#endif 902#endif