aboutsummaryrefslogtreecommitdiff
path: root/src/dht
diff options
context:
space:
mode:
authorNathan S. Evans <evans@in.tum.de>2010-04-09 16:21:05 +0000
committerNathan S. Evans <evans@in.tum.de>2010-04-09 16:21:05 +0000
commitb85f126feb0f722be9c37a5ba0fa1a053b82a704 (patch)
treebb3c4d5226e6d1c7c23e169771329c1b6c8a4a60 /src/dht
parent1ea93db989173e26d88f1274ed00dec633b9a8bc (diff)
downloadgnunet-b85f126feb0f722be9c37a5ba0fa1a053b82a704.tar.gz
gnunet-b85f126feb0f722be9c37a5ba0fa1a053b82a704.zip
inexorably closer to perfection
Diffstat (limited to 'src/dht')
-rw-r--r--src/dht/dht.h29
-rw-r--r--src/dht/dht_api.c151
-rw-r--r--src/dht/gnunet-service-dht.c3
3 files changed, 87 insertions, 96 deletions
diff --git a/src/dht/dht.h b/src/dht/dht.h
index 38503cfe2..2bafc3694 100644
--- a/src/dht/dht.h
+++ b/src/dht/dht.h
@@ -59,10 +59,10 @@ struct GNUNET_DHT_StopMessage
59/** 59/**
60 * Generic DHT message, wrapper for other message types 60 * Generic DHT message, wrapper for other message types
61 */ 61 */
62struct GNUNET_DHT_Message 62struct GNUNET_DHT_RouteMessage
63{ 63{
64 /** 64 /**
65 * Type: GNUNET_MESSAGE_TYPE_DHT_MESSAGE 65 * Type: GNUNET_MESSAGE_TYPE_DHT_ROUTE
66 */ 66 */
67 struct GNUNET_MessageHeader header; 67 struct GNUNET_MessageHeader header;
68 68
@@ -97,6 +97,31 @@ struct GNUNET_DHT_Message
97 97
98}; 98};
99 99
100struct GNUNET_DHT_RouteResultMessage
101{
102 /**
103 * Type: GNUNET_MESSAGE_TYPE_DHT_ROUTE_RESULT
104 */
105 struct GNUNET_MessageHeader header;
106
107 /**
108 * Message options
109 */
110 uint32_t options GNUNET_PACKED;
111
112 /**
113 * The key that was searched for
114 */
115 GNUNET_HashCode key;
116
117 /**
118 * Unique ID identifying this request
119 */
120 uint64_t unique_id GNUNET_PACKED;
121
122 /* GNUNET_MessageHeader *enc actual DHT message, copied to end of this dealy do */
123};
124
100/** 125/**
101 * Message to insert data into the DHT 126 * Message to insert data into the DHT
102 */ 127 */
diff --git a/src/dht/dht_api.c b/src/dht/dht_api.c
index 7dd3305eb..5675cef50 100644
--- a/src/dht/dht_api.c
+++ b/src/dht/dht_api.c
@@ -196,7 +196,7 @@ struct GNUNET_DHT_FindPeerHandle
196 struct GNUNET_DHT_RouteHandle *route_handle; 196 struct GNUNET_DHT_RouteHandle *route_handle;
197 197
198 /** 198 /**
199 * The context of the get request 199 * The context of the find peer request
200 */ 200 */
201 struct GNUNET_DHT_FindPeerContext find_peer_context; 201 struct GNUNET_DHT_FindPeerContext find_peer_context;
202}; 202};
@@ -280,8 +280,7 @@ service_message_handler (void *cls,
280 const struct GNUNET_MessageHeader *msg) 280 const struct GNUNET_MessageHeader *msg)
281{ 281{
282 struct GNUNET_DHT_Handle *handle = cls; 282 struct GNUNET_DHT_Handle *handle = cls;
283 struct GNUNET_DHT_Message *dht_msg; 283 struct GNUNET_DHT_RouteResultMessage *dht_msg;
284 struct GNUNET_DHT_StopMessage *stop_msg;
285 struct GNUNET_MessageHeader *enc_msg; 284 struct GNUNET_MessageHeader *enc_msg;
286 struct GNUNET_DHT_RouteHandle *route_handle; 285 struct GNUNET_DHT_RouteHandle *route_handle;
287 uint64_t uid; 286 uint64_t uid;
@@ -297,7 +296,7 @@ service_message_handler (void *cls,
297 "`%s': Received NULL from server, connection down!\n", 296 "`%s': Received NULL from server, connection down!\n",
298 "DHT API"); 297 "DHT API");
299#endif 298#endif
300 GNUNET_CLIENT_disconnect (handle->client); 299 GNUNET_CLIENT_disconnect (handle->client, GNUNET_YES);
301 handle->client = GNUNET_CLIENT_connect (handle->sched, 300 handle->client = GNUNET_CLIENT_connect (handle->sched,
302 "dht", 301 "dht",
303 handle->cfg); 302 handle->cfg);
@@ -308,41 +307,42 @@ service_message_handler (void *cls,
308 307
309 switch (ntohs (msg->type)) 308 switch (ntohs (msg->type))
310 { 309 {
311 case GNUNET_MESSAGE_TYPE_DHT: 310 case GNUNET_MESSAGE_TYPE_DHT_ROUTE_RESULT:
312 { 311 {
313 dht_msg = (struct GNUNET_DHT_Message *) msg; 312 dht_msg = (struct GNUNET_DHT_RouteResultMessage *) msg;
314 uid = GNUNET_ntohll (dht_msg->unique_id); 313 uid = GNUNET_ntohll (dht_msg->unique_id);
315#if DEBUG_DHT_API 314#if DEBUG_DHT_API
316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
317 "`%s': Received response to message (uid %llu)\n", 316 "`%s': Received response to message (uid %llu)\n",
318 "DHT API", uid); 317 "DHT API", uid);
319#endif 318#endif
320 if (ntohl (dht_msg->unique)) 319
320 hash_from_uid (uid, &uid_hash);
321 route_handle =
322 GNUNET_CONTAINER_multihashmap_get (handle->outstanding_requests,
323 &uid_hash);
324 if (route_handle == NULL) /* We have no recollection of this request */
321 { 325 {
322 hash_from_uid (uid, &uid_hash);
323 route_handle =
324 GNUNET_CONTAINER_multihashmap_get (handle->outstanding_requests,
325 &uid_hash);
326 if (route_handle == NULL) /* We have no recollection of this request */
327 {
328#if DEBUG_DHT_API 326#if DEBUG_DHT_API
329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
330 "`%s': Received response to message (uid %llu), but have no recollection of it!\n", 328 "`%s': Received response to message (uid %llu), but have no recollection of it!\n",
331 "DHT API", uid); 329 "DHT API", uid);
332#endif 330#endif
333 }
334 else
335 {
336 enc_size =
337 ntohs (dht_msg->header.size) -
338 sizeof (struct GNUNET_DHT_Message);
339 GNUNET_assert (enc_size > 0);
340 enc_msg = (struct GNUNET_MessageHeader *) &dht_msg[1];
341 route_handle->iter (route_handle->iter_cls, enc_msg);
342 }
343 } 331 }
332 else
333 {
334 enc_size =
335 ntohs (dht_msg->header.size) -
336 sizeof (struct GNUNET_DHT_RouteResultMessage);
337 GNUNET_assert (enc_size > 0);
338 enc_msg = (struct GNUNET_MessageHeader *) &dht_msg[1];
339 route_handle->iter (route_handle->iter_cls, enc_msg);
340 }
341
344 break; 342 break;
345 } 343 }
344 /* FIXME: we don't want these anymore, call continuation once message is sent. */
345 /*
346 case GNUNET_MESSAGE_TYPE_DHT_STOP: 346 case GNUNET_MESSAGE_TYPE_DHT_STOP:
347 { 347 {
348 stop_msg = (struct GNUNET_DHT_StopMessage *) msg; 348 stop_msg = (struct GNUNET_DHT_StopMessage *) msg;
@@ -371,6 +371,7 @@ service_message_handler (void *cls,
371 } 371 }
372 break; 372 break;
373 } 373 }
374 */
374 default: 375 default:
375 { 376 {
376 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 377 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
@@ -457,37 +458,33 @@ GNUNET_DHT_disconnect (struct GNUNET_DHT_Handle *handle)
457 458
458 459
459/** 460/**
460 * Send complete (or failed), schedule next (or don't) 461 * Send complete (or failed), call continuation if we have one.
461 */ 462 */
462static void 463static void
463finish (struct GNUNET_DHT_Handle *handle, int code) 464finish (struct GNUNET_DHT_Handle *handle, int code)
464{ 465{
465 /* TODO: if code is not GNUNET_OK, do something! */
466 struct PendingMessage *pos = handle->current; 466 struct PendingMessage *pos = handle->current;
467#if DEBUG_DHT_API 467#if DEBUG_DHT_API
468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s': Finish called!\n", "DHT API"); 468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s': Finish called!\n", "DHT API");
469#endif 469#endif
470 GNUNET_assert (pos != NULL); 470 GNUNET_assert (pos != NULL);
471 471
472 if (pos->is_unique)
473 {
474 if (pos->cont != NULL)
475 {
476 if (code == GNUNET_SYSERR)
477 GNUNET_SCHEDULER_add_continuation (handle->sched, pos->cont,
478 pos->cont_cls,
479 GNUNET_SCHEDULER_REASON_TIMEOUT);
480 else
481 GNUNET_SCHEDULER_add_continuation (handle->sched, pos->cont,
482 pos->cont_cls,
483 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
484 }
485 472
486 GNUNET_free (pos->msg); 473 if (pos->cont != NULL)
487 handle->current = NULL; 474 {
488 GNUNET_free (pos); 475 if (code == GNUNET_SYSERR)
476 GNUNET_SCHEDULER_add_continuation (handle->sched, pos->cont,
477 pos->cont_cls,
478 GNUNET_SCHEDULER_REASON_TIMEOUT);
479 else
480 GNUNET_SCHEDULER_add_continuation (handle->sched, pos->cont,
481 pos->cont_cls,
482 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
489 } 483 }
490 /* Otherwise we need to wait for a response to this message! */ 484
485 GNUNET_free (pos->msg);
486 GNUNET_free (pos);
487 handle->current = NULL;
491} 488}
492 489
493 490
@@ -618,9 +615,8 @@ get_reply_iterator (void *cls, const struct GNUNET_MessageHeader *reply)
618 GNUNET_assert (ntohs (reply->size) >= 615 GNUNET_assert (ntohs (reply->size) >=
619 sizeof (struct GNUNET_DHT_GetResultMessage)); 616 sizeof (struct GNUNET_DHT_GetResultMessage));
620 result = (struct GNUNET_DHT_GetResultMessage *) reply; 617 result = (struct GNUNET_DHT_GetResultMessage *) reply;
621 data_size = ntohs (result->data_size); 618 data_size = ntohs (reply->size) - sizeof(struct GNUNET_DHT_GetResultMessage);
622 GNUNET_assert (ntohs (reply->size) == 619
623 sizeof (struct GNUNET_DHT_GetResultMessage) + data_size);
624 result_data = (char *) &result[1]; /* Set data pointer to end of message */ 620 result_data = (char *) &result[1]; /* Set data pointer to end of message */
625 621
626 get_handle->get_context.iter (get_handle->get_context.iter_cls, 622 get_handle->get_context.iter (get_handle->get_context.iter_cls,
@@ -637,32 +633,20 @@ void
637find_peer_reply_iterator (void *cls, const struct GNUNET_MessageHeader *reply) 633find_peer_reply_iterator (void *cls, const struct GNUNET_MessageHeader *reply)
638{ 634{
639 struct GNUNET_DHT_FindPeerHandle *find_peer_handle = cls; 635 struct GNUNET_DHT_FindPeerHandle *find_peer_handle = cls;
640 struct GNUNET_DHT_FindPeerResultMessage *result;
641 size_t data_size;
642 struct GNUNET_MessageHeader *result_data;
643 636
644#if DEBUG_DHT_API 637#if DEBUG_DHT_API
645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 638 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
646 "Find peer iterator called.\n"); 639 "Find peer iterator called.\n");
647#endif 640#endif
648 if (ntohs (reply->type) != GNUNET_MESSAGE_TYPE_DHT_FIND_PEER_RESULT) 641 if (ntohs (reply->type) != GNUNET_MESSAGE_TYPE_HELLO)
649 return; 642 return;
650 643
651 GNUNET_assert (ntohs (reply->size) >= 644 GNUNET_assert (ntohs (reply->size) >=
652 sizeof (struct GNUNET_DHT_FindPeerResultMessage)); 645 sizeof (struct GNUNET_MessageHeader));
653 result = (struct GNUNET_DHT_FindPeerResultMessage *) reply;
654 data_size = ntohs (result->data_size);
655 GNUNET_assert (ntohs (reply->size) ==
656 sizeof (struct GNUNET_DHT_FindPeerResultMessage) + data_size);
657
658 if (data_size > 0)
659 result_data = (struct GNUNET_MessageHeader *) &result[1]; /* Set data pointer to end of message */
660 else
661 result_data = NULL;
662 646
663 find_peer_handle->find_peer_context.proc (find_peer_handle-> 647 find_peer_handle->find_peer_context.proc (find_peer_handle->
664 find_peer_context.proc_cls, 648 find_peer_context.proc_cls,
665 &result->peer, result_data); 649 (struct GNUNET_HELLO_Message *)reply);
666} 650}
667 651
668/** 652/**
@@ -700,13 +684,13 @@ GNUNET_DHT_route_start (struct GNUNET_DHT_Handle *handle,
700{ 684{
701 struct GNUNET_DHT_RouteHandle *route_handle; 685 struct GNUNET_DHT_RouteHandle *route_handle;
702 struct PendingMessage *pending; 686 struct PendingMessage *pending;
703 struct GNUNET_DHT_Message *message; 687 struct GNUNET_DHT_RouteMessage *message;
704 size_t expects_response; 688 size_t expects_response;
705 uint16_t msize; 689 uint16_t msize;
706 GNUNET_HashCode uid_key; 690 GNUNET_HashCode uid_key;
707 uint64_t uid; 691 uint64_t uid;
708 692
709 if (sizeof (struct GNUNET_DHT_Message) + ntohs (enc->size) >= GNUNET_SERVER_MAX_MESSAGE_SIZE) 693 if (sizeof (struct GNUNET_DHT_RouteMessage) + ntohs (enc->size) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
710 { 694 {
711 GNUNET_break (0); 695 GNUNET_break (0);
712 return NULL; 696 return NULL;
@@ -731,10 +715,10 @@ GNUNET_DHT_route_start (struct GNUNET_DHT_Handle *handle,
731 &uid_key, route_handle, 715 &uid_key, route_handle,
732 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE); 716 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
733 } 717 }
734 msize = sizeof (struct GNUNET_DHT_Message) + ntohs (enc->size); 718 msize = sizeof (struct GNUNET_DHT_RouteMessage) + ntohs (enc->size);
735 message = GNUNET_malloc (msize); 719 message = GNUNET_malloc (msize);
736 message->header.size = htons (msize); 720 message->header.size = htons (msize);
737 message->header.type = htons (GNUNET_MESSAGE_TYPE_DHT); 721 message->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_ROUTE);
738 memcpy (&message->key, key, sizeof (GNUNET_HashCode)); 722 memcpy (&message->key, key, sizeof (GNUNET_HashCode));
739 message->options = htonl (options); 723 message->options = htonl (options);
740 message->desired_replication_level = htonl (options); 724 message->desired_replication_level = htonl (options);
@@ -746,7 +730,6 @@ GNUNET_DHT_route_start (struct GNUNET_DHT_Handle *handle,
746 pending->timeout = timeout; 730 pending->timeout = timeout;
747 pending->cont = cont; 731 pending->cont = cont;
748 pending->cont_cls = cont_cls; 732 pending->cont_cls = cont_cls;
749 pending->expects_response = expects_response;
750 pending->unique_id = uid; 733 pending->unique_id = uid;
751 GNUNET_assert (handle->current == NULL); 734 GNUNET_assert (handle->current == NULL);
752 handle->current = pending; 735 handle->current = pending;
@@ -878,7 +861,6 @@ GNUNET_DHT_get_stop (struct GNUNET_DHT_GetHandle *get_handle,
878 * @param timeout timeout for this request to be sent to the 861 * @param timeout timeout for this request to be sent to the
879 * service 862 * service
880 * @param options routing options for this message 863 * @param options routing options for this message
881 * @param message a message to inject at found peers (may be null)
882 * @param key the key to look up 864 * @param key the key to look up
883 * @param proc function to call on each result 865 * @param proc function to call on each result
884 * @param proc_cls closure for proc 866 * @param proc_cls closure for proc
@@ -891,24 +873,18 @@ struct GNUNET_DHT_FindPeerHandle *
891GNUNET_DHT_find_peer_start (struct GNUNET_DHT_Handle *handle, 873GNUNET_DHT_find_peer_start (struct GNUNET_DHT_Handle *handle,
892 struct GNUNET_TIME_Relative timeout, 874 struct GNUNET_TIME_Relative timeout,
893 enum GNUNET_DHT_RouteOption options, 875 enum GNUNET_DHT_RouteOption options,
894 struct GNUNET_MessageHeader *message,
895 const GNUNET_HashCode * key, 876 const GNUNET_HashCode * key,
896 GNUNET_DHT_FindPeerProcessor proc, 877 GNUNET_DHT_FindPeerProcessor proc,
897 void *proc_cls, 878 void *proc_cls,
898 GNUNET_SCHEDULER_Task cont, void *cont_cls) 879 GNUNET_SCHEDULER_Task cont,
880 void *cont_cls)
899{ 881{
900 struct GNUNET_DHT_FindPeerHandle *find_peer_handle; 882 struct GNUNET_DHT_FindPeerHandle *find_peer_handle;
901 struct GNUNET_DHT_FindPeerMessage *find_peer_msg; 883 struct GNUNET_MessageHeader *find_peer_msg;
902 size_t msize;
903 884
904 if (handle->current != NULL) /* Can't send right now, we have a pending message... */ 885 if (handle->current != NULL) /* Can't send right now, we have a pending message... */
905 return NULL; 886 return NULL;
906 887
907 if (message != NULL)
908 msize = ntohs (message->size);
909 else
910 msize = 0;
911
912 find_peer_handle = 888 find_peer_handle =
913 GNUNET_malloc (sizeof (struct GNUNET_DHT_FindPeerHandle)); 889 GNUNET_malloc (sizeof (struct GNUNET_DHT_FindPeerHandle));
914 find_peer_handle->find_peer_context.proc = proc; 890 find_peer_handle->find_peer_context.proc = proc;
@@ -920,20 +896,11 @@ GNUNET_DHT_find_peer_start (struct GNUNET_DHT_Handle *handle,
920 "FIND PEER", GNUNET_h2s (key)); 896 "FIND PEER", GNUNET_h2s (key));
921#endif 897#endif
922 898
923 find_peer_msg = 899 find_peer_msg = GNUNET_malloc(sizeof(struct GNUNET_MessageHeader));
924 GNUNET_malloc (sizeof (struct GNUNET_DHT_FindPeerMessage) + msize); 900 find_peer_msg->size = htons(sizeof(struct GNUNET_MessageHeader));
925 find_peer_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_FIND_PEER); 901 find_peer_msg->type = htons(GNUNET_MESSAGE_TYPE_DHT_FIND_PEER);
926 find_peer_msg->header.size =
927 htons (sizeof (struct GNUNET_DHT_FindPeerMessage));
928 find_peer_msg->msg_len = msize;
929
930 if (message != NULL)
931 {
932 memcpy (&find_peer_msg[1], message, msize);
933 }
934
935 find_peer_handle->route_handle = 902 find_peer_handle->route_handle =
936 GNUNET_DHT_route_start (handle, key, 0, options, &find_peer_msg->header, 903 GNUNET_DHT_route_start (handle, key, 0, options, find_peer_msg,
937 timeout, &find_peer_reply_iterator, 904 timeout, &find_peer_reply_iterator,
938 find_peer_handle, cont, cont_cls); 905 find_peer_handle, cont, cont_cls);
939 return find_peer_handle; 906 return find_peer_handle;
@@ -1012,7 +979,7 @@ GNUNET_DHT_put (struct GNUNET_DHT_Handle *handle,
1012 put_msg->header.size = htons (msize); 979 put_msg->header.size = htons (msize);
1013 put_msg->type = htons (type); 980 put_msg->type = htons (type);
1014 put_msg->data_size = htons (size); 981 put_msg->data_size = htons (size);
1015 put_msg->expiration = exp; 982 put_msg->expiration = GNUNET_TIME_absolute_hton(exp);
1016 memcpy (&put_msg[1], data, size); 983 memcpy (&put_msg[1], data, size);
1017 984
1018 GNUNET_DHT_route_start (handle, key, 0, 0, &put_msg->header, timeout, NULL, 985 GNUNET_DHT_route_start (handle, key, 0, 0, &put_msg->header, timeout, NULL,
diff --git a/src/dht/gnunet-service-dht.c b/src/dht/gnunet-service-dht.c
index 1214a8bab..8907c3642 100644
--- a/src/dht/gnunet-service-dht.c
+++ b/src/dht/gnunet-service-dht.c
@@ -253,7 +253,6 @@ send_generic_reply (void *cls, size_t size, void *buf)
253 client->pending_tail, 253 client->pending_tail,
254 reply); 254 reply);
255 memcpy (&cbuf[off], reply->msg, msize); 255 memcpy (&cbuf[off], reply->msg, msize);
256 GNUNET_free (reply->msg);
257 GNUNET_free (reply); 256 GNUNET_free (reply);
258 off += msize; 257 off += msize;
259 } 258 }
@@ -313,7 +312,7 @@ send_reply_to_client (struct ClientList *client,
313 return; 312 return;
314 } 313 }
315 reply = GNUNET_malloc (tsize); 314 reply = GNUNET_malloc (tsize);
316 reply->header.type = htons (GNUNET_MESSAGE_TYPE_DHT); 315 reply->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_ROUTE_RESULT);
317 reply->header.size = htons (tsize); 316 reply->header.size = htons (tsize);
318 if (uid != 0) 317 if (uid != 0)
319 reply->unique = htonl (GNUNET_YES); // ???? 318 reply->unique = htonl (GNUNET_YES); // ????