aboutsummaryrefslogtreecommitdiff
path: root/src/dht
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2010-04-09 11:10:25 +0000
committerChristian Grothoff <christian@grothoff.org>2010-04-09 11:10:25 +0000
commit54eccc36f12430505b3b946f88ac61087cc82793 (patch)
tree722953a28bdbac53d006cb0fcb622473ed6dcc50 /src/dht
parent513a6ef355fcbabeb1953055185608ed4dacbe69 (diff)
downloadgnunet-54eccc36f12430505b3b946f88ac61087cc82793.tar.gz
gnunet-54eccc36f12430505b3b946f88ac61087cc82793.zip
breaking DHT code
Diffstat (limited to 'src/dht')
-rw-r--r--src/dht/dht.h95
-rw-r--r--src/dht/dht_api.c177
-rw-r--r--src/dht/gnunet-service-dht.c399
3 files changed, 217 insertions, 454 deletions
diff --git a/src/dht/dht.h b/src/dht/dht.h
index 6c2f4cbd8..38503cfe2 100644
--- a/src/dht/dht.h
+++ b/src/dht/dht.h
@@ -30,11 +30,11 @@
30#define DEBUG_DHT GNUNET_NO 30#define DEBUG_DHT GNUNET_NO
31 31
32typedef void (*GNUNET_DHT_MessageReceivedHandler) (void *cls, 32typedef void (*GNUNET_DHT_MessageReceivedHandler) (void *cls,
33 struct GNUNET_MessageHeader 33 const struct GNUNET_MessageHeader
34 * msg); 34 * msg);
35 35
36/** 36/**
37 * Generic DHT message, wrapper for other message types 37 * FIXME.
38 */ 38 */
39struct GNUNET_DHT_StopMessage 39struct GNUNET_DHT_StopMessage
40{ 40{
@@ -44,9 +44,14 @@ struct GNUNET_DHT_StopMessage
44 struct GNUNET_MessageHeader header; 44 struct GNUNET_MessageHeader header;
45 45
46 /** 46 /**
47 * Always zero.
48 */
49 uint32_t reserved GNUNET_PACKED;
50
51 /**
47 * Unique ID identifying this request 52 * Unique ID identifying this request
48 */ 53 */
49 uint64_t unique_id; 54 uint64_t unique_id GNUNET_PACKED;
50 55
51}; 56};
52 57
@@ -62,34 +67,32 @@ struct GNUNET_DHT_Message
62 struct GNUNET_MessageHeader header; 67 struct GNUNET_MessageHeader header;
63 68
64 /** 69 /**
70 * Message options
71 */
72 uint32_t options GNUNET_PACKED;
73
74 /**
65 * The key to search for 75 * The key to search for
66 */ 76 */
67 GNUNET_HashCode key; 77 GNUNET_HashCode key;
68 78
69 /** 79 /**
70 * Replication level for this message 80 * Unique ID identifying this request
71 */ 81 */
72 uint16_t desired_replication_level; 82 uint64_t unique_id GNUNET_PACKED;
73 83
74 /** 84 /**
75 * Message options 85 * Replication level for this message
76 */ 86 */
77 uint16_t options; 87 uint32_t desired_replication_level GNUNET_PACKED;
78 88
79 /** 89 /**
80 * Is this message uniquely identified? If so it will 90 * Is this message uniquely identified? If so it will
81 * be fire and forget, if not we will wait for a receipt 91 * be fire and forget, if not we will wait for a receipt
82 * from the service. 92 * from the service.
83 */ 93 */
84 uint16_t unique; 94 uint32_t unique GNUNET_PACKED;
85
86 95
87 /**
88 * Unique ID identifying this request
89 */
90 uint64_t unique_id;
91
92 /* */
93 /* GNUNET_MessageHeader *enc actual DHT message, copied to end of this dealy do */ 96 /* GNUNET_MessageHeader *enc actual DHT message, copied to end of this dealy do */
94 97
95}; 98};
@@ -107,17 +110,17 @@ struct GNUNET_DHT_PutMessage
107 /** 110 /**
108 * The type of data to insert. 111 * The type of data to insert.
109 */ 112 */
110 size_t type; 113 size_t type GNUNET_PACKED;
111 114
112 /** 115 /**
113 * The size of the data, appended to the end of this message. 116 * How long should this data persist?
114 */ 117 */
115 size_t data_size; 118 struct GNUNET_TIME_AbsoluteNBO expiration;
116 119
117 /** 120 /**
118 * How long should this data persist? 121 * The size of the data, appended to the end of this message.
119 */ 122 */
120 struct GNUNET_TIME_Absolute expiration; 123 size_t data_size GNUNET_PACKED;
121 124
122}; 125};
123 126
@@ -135,7 +138,7 @@ struct GNUNET_DHT_GetMessage
135 /** 138 /**
136 * The type for the data for the GET request 139 * The type for the data for the GET request
137 */ 140 */
138 size_t type; 141 uint32_t type;
139 142
140}; 143};
141 144
@@ -152,7 +155,7 @@ struct GNUNET_DHT_GetResultMessage
152 /** 155 /**
153 * The type for the data for the GET request 156 * The type for the data for the GET request
154 */ 157 */
155 size_t type; 158 uint32_t type;
156 159
157 /** 160 /**
158 * The key to search for 161 * The key to search for
@@ -164,55 +167,7 @@ struct GNUNET_DHT_GetResultMessage
164 */ 167 */
165 struct GNUNET_TIME_Absolute expiration; 168 struct GNUNET_TIME_Absolute expiration;
166 169
167 /**
168 * The size of the data, appended to the end of this message.
169 */
170 size_t data_size;
171
172};
173
174/**
175 * Message to issue find peer request to the DHT
176 */
177struct GNUNET_DHT_FindPeerMessage
178{
179 /**
180 * Type: GNUNET_MESSAGE_TYPE_DHT_FIND_PEER
181 */
182 struct GNUNET_MessageHeader header;
183
184 /**
185 * Size of inject message (may be zero)
186 */
187 size_t msg_len;
188
189 /* Followed by message to inject at found peers */
190
191}; 170};
192 171
193/**
194 * Message to return data from the DHT
195 */
196struct GNUNET_DHT_FindPeerResultMessage
197{
198 /**
199 * Type: GNUNET_MESSAGE_TYPE_DHT_FIND_PEER_RESULT
200 */
201 struct GNUNET_MessageHeader header;
202
203 /**
204 * The peer that was found
205 */
206 struct GNUNET_PeerIdentity peer;
207
208 /**
209 * The size of the return message from the peer
210 * (defaults to HELLO for the peer),
211 * appended to the end of this message, 0 if
212 * no message.
213 */
214 size_t data_size;
215
216};
217 172
218#endif /* DHT_H_ */ 173#endif /* DHT_H_ */
diff --git a/src/dht/dht_api.c b/src/dht/dht_api.c
index 8722e7b75..7dd3305eb 100644
--- a/src/dht/dht_api.c
+++ b/src/dht/dht_api.c
@@ -248,46 +248,36 @@ struct GNUNET_DHT_Handle
248 struct GNUNET_DHT_NonUniqueHandle *non_unique_request; 248 struct GNUNET_DHT_NonUniqueHandle *non_unique_request;
249 249
250 /** 250 /**
251 * Kill off the connection and any pending messages. 251 * Generator for unique ids.
252 */ 252 */
253 int do_destroy; 253 uint64_t uid_gen;
254 254
255}; 255};
256 256
257static struct GNUNET_TIME_Relative default_request_timeout;
258 257
259/* Forward declaration */ 258/**
260static void process_pending_message (struct GNUNET_DHT_Handle *handle); 259 * Convert unique ID to hash code.
261 260 *
262static GNUNET_HashCode * 261 * @param uid unique ID to convert
263hash_from_uid (uint64_t uid) 262 * @param hash set to uid (extended with zeros)
263 */
264static void
265hash_from_uid (uint64_t uid,
266 GNUNET_HashCode *hash)
264{ 267{
265 int count; 268 memset (hash, 0, sizeof(GNUNET_HashCode));
266 int remaining; 269 *((uint64_t*)hash) = uid;
267 GNUNET_HashCode *hash;
268 hash = GNUNET_malloc (sizeof (GNUNET_HashCode));
269 count = 0;
270
271 while (count < sizeof (GNUNET_HashCode))
272 {
273 remaining = sizeof (GNUNET_HashCode) - count;
274 if (remaining > sizeof (uid))
275 remaining = sizeof (uid);
276
277 memcpy (hash, &uid, remaining);
278 count += remaining;
279 }
280
281 return hash;
282} 270}
283 271
272
284/** 273/**
285 * Handler for messages received from the DHT service 274 * Handler for messages received from the DHT service
286 * a demultiplexer which handles numerous message types 275 * a demultiplexer which handles numerous message types
287 * 276 *
288 */ 277 */
289void 278void
290service_message_handler (void *cls, const struct GNUNET_MessageHeader *msg) 279service_message_handler (void *cls,
280 const struct GNUNET_MessageHeader *msg)
291{ 281{
292 struct GNUNET_DHT_Handle *handle = cls; 282 struct GNUNET_DHT_Handle *handle = cls;
293 struct GNUNET_DHT_Message *dht_msg; 283 struct GNUNET_DHT_Message *dht_msg;
@@ -295,7 +285,7 @@ service_message_handler (void *cls, const struct GNUNET_MessageHeader *msg)
295 struct GNUNET_MessageHeader *enc_msg; 285 struct GNUNET_MessageHeader *enc_msg;
296 struct GNUNET_DHT_RouteHandle *route_handle; 286 struct GNUNET_DHT_RouteHandle *route_handle;
297 uint64_t uid; 287 uint64_t uid;
298 GNUNET_HashCode *uid_hash; 288 GNUNET_HashCode uid_hash;
299 size_t enc_size; 289 size_t enc_size;
300 /* TODO: find out message type, handle callbacks for different types of messages. 290 /* TODO: find out message type, handle callbacks for different types of messages.
301 * Should be a non unique acknowledgment, or unique result. */ 291 * Should be a non unique acknowledgment, or unique result. */
@@ -304,9 +294,15 @@ service_message_handler (void *cls, const struct GNUNET_MessageHeader *msg)
304 { 294 {
305#if DEBUG_DHT_API 295#if DEBUG_DHT_API
306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
307 "`%s': Received NULL from server, connection down?\n", 297 "`%s': Received NULL from server, connection down!\n",
308 "DHT API"); 298 "DHT API");
309#endif 299#endif
300 GNUNET_CLIENT_disconnect (handle->client);
301 handle->client = GNUNET_CLIENT_connect (handle->sched,
302 "dht",
303 handle->cfg);
304 /* FIXME: re-transmit *all* of our GET requests AND re-start
305 receiving responses! */
310 return; 306 return;
311 } 307 }
312 308
@@ -321,13 +317,12 @@ service_message_handler (void *cls, const struct GNUNET_MessageHeader *msg)
321 "`%s': Received response to message (uid %llu)\n", 317 "`%s': Received response to message (uid %llu)\n",
322 "DHT API", uid); 318 "DHT API", uid);
323#endif 319#endif
324 if (ntohs (dht_msg->unique)) 320 if (ntohl (dht_msg->unique))
325 { 321 {
326 uid_hash = hash_from_uid (uid); 322 hash_from_uid (uid, &uid_hash);
327 route_handle = 323 route_handle =
328 GNUNET_CONTAINER_multihashmap_get (handle->outstanding_requests, 324 GNUNET_CONTAINER_multihashmap_get (handle->outstanding_requests,
329 uid_hash); 325 &uid_hash);
330 GNUNET_free (uid_hash);
331 if (route_handle == NULL) /* We have no recollection of this request */ 326 if (route_handle == NULL) /* We have no recollection of this request */
332 { 327 {
333#if DEBUG_DHT_API 328#if DEBUG_DHT_API
@@ -344,7 +339,6 @@ service_message_handler (void *cls, const struct GNUNET_MessageHeader *msg)
344 GNUNET_assert (enc_size > 0); 339 GNUNET_assert (enc_size > 0);
345 enc_msg = (struct GNUNET_MessageHeader *) &dht_msg[1]; 340 enc_msg = (struct GNUNET_MessageHeader *) &dht_msg[1];
346 route_handle->iter (route_handle->iter_cls, enc_msg); 341 route_handle->iter (route_handle->iter_cls, enc_msg);
347
348 } 342 }
349 } 343 }
350 break; 344 break;
@@ -409,25 +403,16 @@ GNUNET_DHT_connect (struct GNUNET_SCHEDULER_Handle *sched,
409 struct GNUNET_DHT_Handle *handle; 403 struct GNUNET_DHT_Handle *handle;
410 404
411 handle = GNUNET_malloc (sizeof (struct GNUNET_DHT_Handle)); 405 handle = GNUNET_malloc (sizeof (struct GNUNET_DHT_Handle));
412
413 default_request_timeout =
414 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5);
415 handle->cfg = cfg; 406 handle->cfg = cfg;
416 handle->sched = sched; 407 handle->sched = sched;
417
418 handle->current = NULL;
419 handle->do_destroy = GNUNET_NO;
420 handle->th = NULL;
421
422 handle->client = GNUNET_CLIENT_connect (sched, "dht", cfg); 408 handle->client = GNUNET_CLIENT_connect (sched, "dht", cfg);
423 handle->outstanding_requests =
424 GNUNET_CONTAINER_multihashmap_create (ht_len);
425
426 if (handle->client == NULL) 409 if (handle->client == NULL)
427 { 410 {
428 GNUNET_free (handle); 411 GNUNET_free (handle);
429 return NULL; 412 return NULL;
430 } 413 }
414 handle->outstanding_requests =
415 GNUNET_CONTAINER_multihashmap_create (ht_len);
431#if DEBUG_DHT_API 416#if DEBUG_DHT_API
432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
433 "`%s': Connection to service in progress\n", "DHT API"); 418 "`%s': Connection to service in progress\n", "DHT API");
@@ -435,7 +420,6 @@ GNUNET_DHT_connect (struct GNUNET_SCHEDULER_Handle *sched,
435 GNUNET_CLIENT_receive (handle->client, 420 GNUNET_CLIENT_receive (handle->client,
436 &service_message_handler, 421 &service_message_handler,
437 handle, GNUNET_TIME_UNIT_FOREVER_REL); 422 handle, GNUNET_TIME_UNIT_FOREVER_REL);
438
439 return handle; 423 return handle;
440} 424}
441 425
@@ -453,7 +437,6 @@ GNUNET_DHT_disconnect (struct GNUNET_DHT_Handle *handle)
453 "`%s': Called GNUNET_DHT_disconnect\n", "DHT API"); 437 "`%s': Called GNUNET_DHT_disconnect\n", "DHT API");
454#endif 438#endif
455 GNUNET_assert (handle != NULL); 439 GNUNET_assert (handle != NULL);
456
457 if (handle->th != NULL) /* We have a live transmit request in the Aether */ 440 if (handle->th != NULL) /* We have a live transmit request in the Aether */
458 { 441 {
459 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th); 442 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
@@ -467,7 +450,8 @@ GNUNET_DHT_disconnect (struct GNUNET_DHT_Handle *handle)
467 GNUNET_CLIENT_disconnect (handle->client, GNUNET_NO); 450 GNUNET_CLIENT_disconnect (handle->client, GNUNET_NO);
468 handle->client = NULL; 451 handle->client = NULL;
469 } 452 }
470 453 /* Either assert that outstanding_requests is empty */
454 /* FIXME: handle->outstanding_requests not freed! */
471 GNUNET_free (handle); 455 GNUNET_free (handle);
472} 456}
473 457
@@ -506,6 +490,7 @@ finish (struct GNUNET_DHT_Handle *handle, int code)
506 /* Otherwise we need to wait for a response to this message! */ 490 /* Otherwise we need to wait for a response to this message! */
507} 491}
508 492
493
509/** 494/**
510 * Transmit the next pending message, called by notify_transmit_ready 495 * Transmit the next pending message, called by notify_transmit_ready
511 */ 496 */
@@ -591,13 +576,6 @@ process_pending_message (struct GNUNET_DHT_Handle *handle)
591 return; 576 return;
592 } 577 }
593 578
594 /* TODO: set do_destroy somewhere's, see what needs to happen in that case! */
595 if (handle->do_destroy)
596 {
597 //GNUNET_DHT_disconnect (handle); /* FIXME: replace with proper disconnect stuffs */
598 }
599
600
601 if (NULL == 579 if (NULL ==
602 (handle->th = GNUNET_CLIENT_notify_transmit_ready (handle->client, 580 (handle->th = GNUNET_CLIENT_notify_transmit_ready (handle->client,
603 ntohs (handle-> 581 ntohs (handle->
@@ -613,6 +591,7 @@ process_pending_message (struct GNUNET_DHT_Handle *handle)
613 "Failed to transmit request to dht service.\n"); 591 "Failed to transmit request to dht service.\n");
614#endif 592#endif
615 finish (handle, GNUNET_SYSERR); 593 finish (handle, GNUNET_SYSERR);
594 return;
616 } 595 }
617#if DEBUG_DHT_API 596#if DEBUG_DHT_API
618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
@@ -722,28 +701,21 @@ GNUNET_DHT_route_start (struct GNUNET_DHT_Handle *handle,
722 struct GNUNET_DHT_RouteHandle *route_handle; 701 struct GNUNET_DHT_RouteHandle *route_handle;
723 struct PendingMessage *pending; 702 struct PendingMessage *pending;
724 struct GNUNET_DHT_Message *message; 703 struct GNUNET_DHT_Message *message;
725 size_t is_unique; 704 size_t expects_response;
726 size_t msize; 705 uint16_t msize;
727 GNUNET_HashCode *uid_key; 706 GNUNET_HashCode uid_key;
728 uint64_t uid; 707 uint64_t uid;
729 708
730 is_unique = GNUNET_YES; 709 if (sizeof (struct GNUNET_DHT_Message) + ntohs (enc->size) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
731 if (iter == NULL)
732 is_unique = GNUNET_NO;
733
734 route_handle = NULL;
735 uid_key = NULL;
736
737 do
738 { 710 {
739 GNUNET_free_non_null (uid_key); 711 GNUNET_break (0);
740 uid = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, -1); 712 return NULL;
741 uid_key = hash_from_uid (uid);
742 } 713 }
743 while (GNUNET_CONTAINER_multihashmap_contains 714 expects_response = GNUNET_YES;
744 (handle->outstanding_requests, uid_key) == GNUNET_YES); 715 if (iter == NULL)
745 716 expects_response = GNUNET_NO;
746 if (is_unique) 717 uid = handle->uid_gen++;
718 if (expects_response)
747 { 719 {
748 route_handle = GNUNET_malloc (sizeof (struct GNUNET_DHT_RouteHandle)); 720 route_handle = GNUNET_malloc (sizeof (struct GNUNET_DHT_RouteHandle));
749 memcpy (&route_handle->key, key, sizeof (GNUNET_HashCode)); 721 memcpy (&route_handle->key, key, sizeof (GNUNET_HashCode));
@@ -755,51 +727,33 @@ GNUNET_DHT_route_start (struct GNUNET_DHT_Handle *handle,
755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
756 "`%s': Unique ID is %llu\n", "DHT API", uid); 728 "`%s': Unique ID is %llu\n", "DHT API", uid);
757#endif 729#endif
758 /**
759 * Store based on random identifier!
760 */
761 GNUNET_CONTAINER_multihashmap_put (handle->outstanding_requests, 730 GNUNET_CONTAINER_multihashmap_put (handle->outstanding_requests,
762 uid_key, route_handle, 731 &uid_key, route_handle,
763 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE); 732 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
764 msize = sizeof (struct GNUNET_DHT_Message) + ntohs (enc->size);
765
766 }
767 else
768 {
769 msize = sizeof (struct GNUNET_DHT_Message) + ntohs (enc->size);
770 } 733 }
771 734 msize = sizeof (struct GNUNET_DHT_Message) + ntohs (enc->size);
772 GNUNET_free (uid_key);
773 message = GNUNET_malloc (msize); 735 message = GNUNET_malloc (msize);
774 message->header.size = htons (msize); 736 message->header.size = htons (msize);
775 message->header.type = htons (GNUNET_MESSAGE_TYPE_DHT); 737 message->header.type = htons (GNUNET_MESSAGE_TYPE_DHT);
776 memcpy (&message->key, key, sizeof (GNUNET_HashCode)); 738 memcpy (&message->key, key, sizeof (GNUNET_HashCode));
777 message->options = htons (options); 739 message->options = htonl (options);
778 message->desired_replication_level = htons (options); 740 message->desired_replication_level = htonl (options);
779 message->unique = htons (is_unique); 741 message->unique = htonl (expects_response);
780 message->unique_id = GNUNET_htonll (uid); 742 message->unique_id = GNUNET_htonll (uid);
781 memcpy (&message[1], enc, ntohs (enc->size)); 743 memcpy (&message[1], enc, ntohs (enc->size));
782
783 pending = GNUNET_malloc (sizeof (struct PendingMessage)); 744 pending = GNUNET_malloc (sizeof (struct PendingMessage));
784 pending->msg = &message->header; 745 pending->msg = &message->header;
785 pending->timeout = timeout; 746 pending->timeout = timeout;
786 pending->cont = cont; 747 pending->cont = cont;
787 pending->cont_cls = cont_cls; 748 pending->cont_cls = cont_cls;
788 pending->is_unique = is_unique; 749 pending->expects_response = expects_response;
789 pending->unique_id = uid; 750 pending->unique_id = uid;
790
791 GNUNET_assert (handle->current == NULL); 751 GNUNET_assert (handle->current == NULL);
792
793 handle->current = pending; 752 handle->current = pending;
794
795 process_pending_message (handle); 753 process_pending_message (handle);
796
797 return route_handle; 754 return route_handle;
798} 755}
799 756
800void
801GNUNET_DHT_route_stop (struct GNUNET_DHT_RouteHandle *route_handle,
802 GNUNET_SCHEDULER_Task cont, void *cont_cls);
803 757
804/** 758/**
805 * Perform an asynchronous GET operation on the DHT identified. 759 * Perform an asynchronous GET operation on the DHT identified.
@@ -851,13 +805,13 @@ GNUNET_DHT_get_start (struct GNUNET_DHT_Handle *handle,
851 return get_handle; 805 return get_handle;
852} 806}
853 807
808
854/** 809/**
855 * Stop a previously issued routing request 810 * Stop a previously issued routing request
856 * 811 *
857 * @param route_handle handle to the request to stop 812 * @param route_handle handle to the request to stop
858 * @param cont continuation to call once this message is sent to the service or times out 813 * @param cont continuation to call once this message is sent to the service or times out
859 * @param cont_cls closure for the continuation 814 * @param cont_cls closure for the continuation
860 *
861 */ 815 */
862void 816void
863GNUNET_DHT_route_stop (struct GNUNET_DHT_RouteHandle *route_handle, 817GNUNET_DHT_route_stop (struct GNUNET_DHT_RouteHandle *route_handle,
@@ -866,10 +820,9 @@ GNUNET_DHT_route_stop (struct GNUNET_DHT_RouteHandle *route_handle,
866 struct PendingMessage *pending; 820 struct PendingMessage *pending;
867 struct GNUNET_DHT_StopMessage *message; 821 struct GNUNET_DHT_StopMessage *message;
868 size_t msize; 822 size_t msize;
869 GNUNET_HashCode *uid_key; 823 GNUNET_HashCode uid_key;
870 824
871 msize = sizeof (struct GNUNET_DHT_StopMessage); 825 msize = sizeof (struct GNUNET_DHT_StopMessage);
872
873 message = GNUNET_malloc (msize); 826 message = GNUNET_malloc (msize);
874 message->header.size = htons (msize); 827 message->header.size = htons (msize);
875 message->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_STOP); 828 message->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_STOP);
@@ -879,37 +832,20 @@ GNUNET_DHT_route_stop (struct GNUNET_DHT_RouteHandle *route_handle,
879 route_handle->uid); 832 route_handle->uid);
880#endif 833#endif
881 message->unique_id = GNUNET_htonll (route_handle->uid); 834 message->unique_id = GNUNET_htonll (route_handle->uid);
882
883 GNUNET_assert (route_handle->dht_handle->current == NULL); 835 GNUNET_assert (route_handle->dht_handle->current == NULL);
884
885 pending = GNUNET_malloc (sizeof (struct PendingMessage)); 836 pending = GNUNET_malloc (sizeof (struct PendingMessage));
886 pending->msg = (struct GNUNET_MessageHeader *) message; 837 pending->msg = (struct GNUNET_MessageHeader *) message;
887 pending->timeout = DEFAULT_DHT_TIMEOUT; 838 pending->timeout = DEFAULT_DHT_TIMEOUT;
888 pending->cont = cont; 839 pending->cont = cont;
889 pending->cont_cls = cont_cls; 840 pending->cont_cls = cont_cls;
890 pending->is_unique = GNUNET_NO;
891 pending->unique_id = route_handle->uid; 841 pending->unique_id = route_handle->uid;
892
893 GNUNET_assert (route_handle->dht_handle->current == NULL); 842 GNUNET_assert (route_handle->dht_handle->current == NULL);
894
895 route_handle->dht_handle->current = pending; 843 route_handle->dht_handle->current = pending;
896
897 process_pending_message (route_handle->dht_handle); 844 process_pending_message (route_handle->dht_handle);
898 845 hash_from_uid (route_handle->uid, &uid_key);
899 uid_key = hash_from_uid (route_handle->uid); 846 GNUNET_assert (GNUNET_CONTAINER_multihashmap_remove
900 847 (route_handle->dht_handle->outstanding_requests, &uid_key,
901 if (GNUNET_CONTAINER_multihashmap_remove 848 route_handle) == GNUNET_YES);
902 (route_handle->dht_handle->outstanding_requests, uid_key,
903 route_handle) != GNUNET_YES)
904 {
905#if DEBUG_DHT_API
906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
907 "`%s': Remove outstanding request from hashmap failed for key %s, uid %llu\n",
908 "DHT API", GNUNET_h2s (uid_key), route_handle->uid);
909#endif
910 }
911 GNUNET_free (uid_key);
912 return;
913} 849}
914 850
915 851
@@ -932,7 +868,6 @@ GNUNET_DHT_get_stop (struct GNUNET_DHT_GetHandle *get_handle,
932#endif 868#endif
933 GNUNET_DHT_route_stop (get_handle->route_handle, cont, cont_cls); 869 GNUNET_DHT_route_stop (get_handle->route_handle, cont, cont_cls);
934 GNUNET_free (get_handle); 870 GNUNET_free (get_handle);
935
936} 871}
937 872
938 873
diff --git a/src/dht/gnunet-service-dht.c b/src/dht/gnunet-service-dht.c
index c1950673e..1214a8bab 100644
--- a/src/dht/gnunet-service-dht.c
+++ b/src/dht/gnunet-service-dht.c
@@ -55,11 +55,6 @@ static struct GNUNET_SCHEDULER_Handle *sched;
55static const struct GNUNET_CONFIGURATION_Handle *cfg; 55static const struct GNUNET_CONFIGURATION_Handle *cfg;
56 56
57/** 57/**
58 * Timeout for transmissions to clients
59 */
60static struct GNUNET_TIME_Relative client_transmit_timeout;
61
62/**
63 * Handle to the core service 58 * Handle to the core service
64 */ 59 */
65static struct GNUNET_CORE_Handle *coreAPI; 60static struct GNUNET_CORE_Handle *coreAPI;
@@ -96,9 +91,14 @@ struct PendingMessage
96 struct PendingMessage *next; 91 struct PendingMessage *next;
97 92
98 /** 93 /**
99 * Actual message to be sent 94 * Pointer to previous item in the list
100 */ 95 */
101 struct GNUNET_MessageHeader *msg; 96 struct PendingMessage *prev;
97
98 /**
99 * Actual message to be sent; // avoid allocation
100 */
101 const struct GNUNET_MessageHeader *msg; // msg = (cast) &pm[1]; // memcpy (&pm[1], data, len);
102 102
103}; 103};
104 104
@@ -130,6 +130,11 @@ struct ClientList
130 */ 130 */
131 struct PendingMessage *pending_head; 131 struct PendingMessage *pending_head;
132 132
133 /**
134 * Tail of linked list of pending messages for this client
135 */
136 struct PendingMessage *pending_tail;
137
133}; 138};
134 139
135/** 140/**
@@ -184,101 +189,30 @@ struct DHT_MessageContext
184 */ 189 */
185static struct ClientList *client_list; 190static struct ClientList *client_list;
186 191
187
188/**
189 * Server handlers for handling locally received dht requests
190 */
191static void
192handle_dht_start_message (void *cls, struct GNUNET_SERVER_Client *client,
193 const struct GNUNET_MessageHeader *message);
194
195static void
196handle_dht_stop_message (void *cls, struct GNUNET_SERVER_Client *client,
197 const struct GNUNET_MessageHeader *message);
198
199static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
200 {&handle_dht_start_message, NULL, GNUNET_MESSAGE_TYPE_DHT, 0},
201 {&handle_dht_stop_message, NULL, GNUNET_MESSAGE_TYPE_DHT_STOP, 0},
202 {NULL, NULL, 0, 0}
203};
204
205
206/**
207 * Core handler for p2p dht get requests.
208 */
209static int handle_dht_p2p_get (void *cls,
210 const struct GNUNET_PeerIdentity *peer,
211 const struct GNUNET_MessageHeader *message,
212 struct GNUNET_TIME_Relative latency,
213 uint32_t distance);
214
215/**
216 * Core handler for p2p dht put requests.
217 */
218static int handle_dht_p2p_put (void *cls,
219 const struct GNUNET_PeerIdentity *peer,
220 const struct GNUNET_MessageHeader *message,
221 struct GNUNET_TIME_Relative latency,
222 uint32_t distance);
223
224/**
225 * Core handler for p2p dht find peer requests.
226 */
227static int handle_dht_p2p_find_peer (void *cls,
228 const struct GNUNET_PeerIdentity *peer,
229 const struct GNUNET_MessageHeader
230 *message,
231 struct GNUNET_TIME_Relative latency,
232 uint32_t distance);
233
234static struct GNUNET_CORE_MessageHandler core_handlers[] = {
235 {&handle_dht_p2p_get, GNUNET_MESSAGE_TYPE_DHT_GET, 0},
236 {&handle_dht_p2p_put, GNUNET_MESSAGE_TYPE_DHT_PUT, 0},
237 {&handle_dht_p2p_find_peer, GNUNET_MESSAGE_TYPE_DHT_FIND_PEER, 0},
238 {NULL, 0, 0}
239};
240
241/** 192/**
242 * Forward declaration. 193 * Forward declaration.
243 */ 194 */
244static size_t send_generic_reply (void *cls, size_t size, void *buf); 195static size_t send_generic_reply (void *cls, size_t size, void *buf);
245 196
197
246/** 198/**
247 * Task run to check for messages that need to be sent to a client. 199 * Task run to check for messages that need to be sent to a client.
248 * 200 *
249 * @param cls a ClientList, containing the client and any messages to be sent to it 201 * @param client a ClientList, containing the client and any messages to be sent to it
250 * @param tc reason this was called
251 */ 202 */
252static void 203static void
253process_pending_messages (void *cls, 204process_pending_messages (struct ClientList *client)
254 const struct GNUNET_SCHEDULER_TaskContext *tc) 205{
255{ 206 if (client->pending_head == NULL)
256 struct ClientList *client = cls; 207 return;
257 208 if (client->transmit_handle != NULL)
258 if (client->pending_head == NULL) /* No messages queued */ 209 return;
259 { 210 client->transmit_handle =
260#if DEBUG_DHT 211 GNUNET_SERVER_notify_transmit_ready (client->client_handle,
261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 212 ntohs (client->pending_head->msg->
262 "`%s': Have no pending messages for client.\n", "DHT"); 213 size),
263#endif 214 GNUNET_TIME_UNIT_FOREVER_REL,
264 return; 215 &send_generic_reply, client);
265 }
266
267 if (client->transmit_handle == NULL) /* No current pending messages, we can try to send! */
268 client->transmit_handle =
269 GNUNET_SERVER_notify_transmit_ready (client->client_handle,
270 ntohs (client->pending_head->msg->
271 size),
272 GNUNET_TIME_relative_multiply
273 (GNUNET_TIME_UNIT_SECONDS, 5),
274 &send_generic_reply, client);
275 else
276 {
277#if DEBUG_DHT
278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
279 "`%s': Transmit handle is non-null.\n", "DHT");
280#endif
281 }
282} 216}
283 217
284/** 218/**
@@ -297,42 +231,41 @@ static size_t
297send_generic_reply (void *cls, size_t size, void *buf) 231send_generic_reply (void *cls, size_t size, void *buf)
298{ 232{
299 struct ClientList *client = cls; 233 struct ClientList *client = cls;
300 struct PendingMessage *reply = client->pending_head; 234 char *cbuf = buf;
301 int ret; 235 struct PendingMessage *reply;
236 size_t off;
237 size_t msize;
302 238
303 client->transmit_handle = NULL; 239 client->transmit_handle = NULL;
304 if (buf == NULL) /* Message timed out, that's crappy... */ 240 if (buf == NULL)
305 { 241 {
242 /* client disconnected */
306#if DEBUG_DHT 243#if DEBUG_DHT
307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s': buffer was NULL\n", "DHT"); 244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s': buffer was NULL\n", "DHT");
308#endif 245#endif
309 client->pending_head = reply->next;
310 GNUNET_free (reply->msg);
311 GNUNET_free (reply);
312 return 0; 246 return 0;
313 } 247 }
314 248 off = 0;
315 if (size >= ntohs (reply->msg->size)) 249 while ( (NULL != (reply = client->pending_head)) &&
250 (size >= off + (msize = ntohs (reply->msg->size))))
316 { 251 {
252 GNUNET_CONTAINER_DLL_remove (client->pending_head,
253 client->pending_tail,
254 reply);
255 memcpy (&cbuf[off], reply->msg, msize);
256 GNUNET_free (reply->msg);
257 GNUNET_free (reply);
258 off += msize;
259 }
317#if DEBUG_DHT 260#if DEBUG_DHT
318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
319 "`%s': Copying reply to buffer, REALLY SENT\n", "DHT"); 262 "`%s': Copying reply to buffer, REALLY SENT\n", "DHT");
320#endif 263#endif
321 memcpy (buf, reply->msg, ntohs (reply->msg->size)); 264 process_pending_messages (client);
322 265 return off;
323 ret = ntohs (reply->msg->size);
324 }
325 else
326 ret = 0;
327
328 client->pending_head = reply->next;
329 GNUNET_free (reply->msg);
330 GNUNET_free (reply);
331
332 GNUNET_SCHEDULER_add_now (sched, &process_pending_messages, client);
333 return ret;
334} 266}
335 267
268
336/** 269/**
337 * Add a PendingMessage to the clients list of messages to be sent 270 * Add a PendingMessage to the clients list of messages to be sent
338 * 271 *
@@ -343,36 +276,14 @@ static void
343add_pending_message (struct ClientList *client, 276add_pending_message (struct ClientList *client,
344 struct PendingMessage *pending_message) 277 struct PendingMessage *pending_message)
345{ 278{
346 struct PendingMessage *pos; 279 GNUNET_CONTAINER_DLL_insert_after (client->pending_head,
347 struct PendingMessage *prev; 280 client->pending_tail,
348 281 client->pending_tail,
349 pos = client->pending_head; 282 pending_message);
350 283 process_pending_messages (client);
351#if DEBUG_DHT
352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
353 "`%s': Adding pending message for client.\n", "DHT");
354#endif
355
356 if (pos == NULL)
357 {
358 client->pending_head = pending_message;
359 }
360 else /* This means another request is already queued, rely on send_reply to process all pending messages */
361 {
362 while (pos != NULL) /* Find end of list */
363 {
364 prev = pos;
365 pos = pos->next;
366 }
367
368 GNUNET_assert (prev != NULL);
369 prev->next = pending_message;
370 }
371
372 GNUNET_SCHEDULER_add_now (sched, &process_pending_messages, client);
373
374} 284}
375 285
286
376/** 287/**
377 * Called when a reply needs to be sent to a client, either as 288 * Called when a reply needs to be sent to a client, either as
378 * a result it found to a GET or FIND PEER request. 289 * a result it found to a GET or FIND PEER request.
@@ -383,13 +294,12 @@ add_pending_message (struct ClientList *client,
383 */ 294 */
384static void 295static void
385send_reply_to_client (struct ClientList *client, 296send_reply_to_client (struct ClientList *client,
386 struct GNUNET_MessageHeader *message, 297 const struct GNUNET_MessageHeader *message,
387 unsigned long long uid) 298 unsigned long long uid)
388{ 299{
389 struct GNUNET_DHT_Message *reply; 300 struct GNUNET_DHT_Message *reply;
390 struct PendingMessage *pending_message; 301 struct PendingMessage *pending_message;
391 302 uint16_t msize;
392 size_t msize;
393 size_t tsize; 303 size_t tsize;
394#if DEBUG_DHT 304#if DEBUG_DHT
395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
@@ -397,17 +307,20 @@ send_reply_to_client (struct ClientList *client,
397#endif 307#endif
398 msize = ntohs (message->size); 308 msize = ntohs (message->size);
399 tsize = sizeof (struct GNUNET_DHT_Message) + msize; 309 tsize = sizeof (struct GNUNET_DHT_Message) + msize;
310 if (tsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
311 {
312 GNUNET_BREAK_op (0);
313 return;
314 }
400 reply = GNUNET_malloc (tsize); 315 reply = GNUNET_malloc (tsize);
401 reply->header.type = htons (GNUNET_MESSAGE_TYPE_DHT); 316 reply->header.type = htons (GNUNET_MESSAGE_TYPE_DHT);
402 reply->header.size = htons (tsize); 317 reply->header.size = htons (tsize);
403 if (uid != 0) 318 if (uid != 0)
404 reply->unique = htons (GNUNET_YES); 319 reply->unique = htonl (GNUNET_YES); // ????
405 reply->unique_id = GNUNET_htonll (uid); 320 reply->unique_id = GNUNET_htonll (uid);
406 memcpy (&reply[1], message, msize); 321 memcpy (&reply[1], message, msize);
407 322 pending_message = GNUNET_malloc (sizeof (struct PendingMessage)); // inline
408 pending_message = GNUNET_malloc (sizeof (struct PendingMessage));
409 pending_message->msg = &reply->header; 323 pending_message->msg = &reply->header;
410
411 add_pending_message (client, pending_message); 324 add_pending_message (client, pending_message);
412} 325}
413 326
@@ -447,57 +360,53 @@ datacache_get_iterator (void *cls,
447 memcpy (&get_result->key, key, sizeof (GNUNET_HashCode)); 360 memcpy (&get_result->key, key, sizeof (GNUNET_HashCode));
448 get_result->type = htons (type); 361 get_result->type = htons (type);
449 memcpy (&get_result[1], data, size); 362 memcpy (&get_result[1], data, size);
450
451 send_reply_to_client (datacache_get_ctx->client, &get_result->header, 363 send_reply_to_client (datacache_get_ctx->client, &get_result->header,
452 datacache_get_ctx->unique_id); 364 datacache_get_ctx->unique_id);
453
454 GNUNET_free (get_result); 365 GNUNET_free (get_result);
455 return GNUNET_OK; 366 return GNUNET_OK;
456} 367}
457 368
369
458/** 370/**
459 * Server handler for initiating local dht get requests 371 * Server handler for initiating local dht get requests
460 * 372 *
461 * @param cls closure for service 373 * @param cls closure for service
462 * @param get_msg the actual get message 374 * @param msg the actual get message
463 * @param message_context struct containing pertinent information about the get request 375 * @param message_context struct containing pertinent information about the get request
464 *
465 */ 376 */
466static void 377static void
467handle_dht_get (void *cls, struct GNUNET_DHT_GetMessage *get_msg, 378handle_dht_get (void *cls,
379 const struct GNUNET_MessageHeader *msg,
468 struct DHT_MessageContext *message_context) 380 struct DHT_MessageContext *message_context)
469{ 381{
470 size_t get_type; 382 const struct GNUNET_DHT_GetMessage *get_msg;
383 uint16_t get_type;
471 unsigned int results; 384 unsigned int results;
472 struct DatacacheGetContext *datacache_get_context; 385 struct DatacacheGetContext datacache_get_context;
473 386
474 GNUNET_assert (ntohs (get_msg->header.size) >= 387 if (ntohs (msg->header.size) != sizeof (struct GNUNET_DHT_GetMessage))
475 sizeof (struct GNUNET_DHT_GetMessage)); 388 {
389 GNUNET_break (0);
390 return;
391 }
392 get_msg = (const struct GNUNET_DHT_GetMessage *) msg;
476 get_type = ntohs (get_msg->type); 393 get_type = ntohs (get_msg->type);
477
478#if DEBUG_DHT 394#if DEBUG_DHT
479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
480 "`%s': Received `%s' request from client, message type %d, key %s, uid %llu\n", 396 "`%s': Received `%s' request from client, message type %u, key %s, uid %llu\n",
481 "DHT", "GET", get_type, GNUNET_h2s (message_context->key), 397 "DHT", "GET", get_type, GNUNET_h2s (message_context->key),
482 message_context->unique_id); 398 message_context->unique_id);
483#endif 399#endif
484 400 datacache_get_context.client = message_context->client;
485 datacache_get_context = GNUNET_malloc (sizeof (struct DatacacheGetContext)); 401 datacache_get_context.unique_id = message_context->unique_id;
486 datacache_get_context->client = message_context->client;
487 datacache_get_context->unique_id = message_context->unique_id;
488
489 results = 0; 402 results = 0;
490 if (datacache != NULL) 403 if (datacache != NULL)
491 results = 404 results =
492 GNUNET_DATACACHE_get (datacache, message_context->key, get_type, 405 GNUNET_DATACACHE_get (datacache, message_context->key, get_type,
493 &datacache_get_iterator, datacache_get_context); 406 &datacache_get_iterator, &datacache_get_context);
494
495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
496 "`%s': Found %d results for local `%s' request\n", "DHT", 408 "`%s': Found %d results for local `%s' request\n", "DHT",
497 results, "GET"); 409 results, "GET");
498
499 GNUNET_free (datacache_get_context);
500 /* FIXME: Implement get functionality here */
501} 410}
502 411
503 412
@@ -510,12 +419,14 @@ handle_dht_get (void *cls, struct GNUNET_DHT_GetMessage *get_msg,
510 * 419 *
511 */ 420 */
512static void 421static void
513handle_dht_find_peer (void *cls, struct GNUNET_DHT_FindPeerMessage *find_msg, 422handle_dht_find_peer (void *cls,
423 const struct GNUNET_MessageHeader *find_msg,
514 struct DHT_MessageContext *message_context) 424 struct DHT_MessageContext *message_context)
515{ 425{
516 struct GNUNET_DHT_FindPeerResultMessage *find_peer_result; 426 struct GNUNET_DHT_FindPeerResultMessage *find_peer_result;
517 size_t hello_size; 427 size_t hello_size;
518 size_t tsize; 428 size_t tsize;
429
519#if DEBUG_DHT 430#if DEBUG_DHT
520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
521 "`%s': Received `%s' request from client, key %s (msg size %d, we expected %d)\n", 432 "`%s': Received `%s' request from client, key %s (msg size %d, we expected %d)\n",
@@ -523,10 +434,6 @@ handle_dht_find_peer (void *cls, struct GNUNET_DHT_FindPeerMessage *find_msg,
523 ntohs (find_msg->header.size), 434 ntohs (find_msg->header.size),
524 sizeof (struct GNUNET_DHT_FindPeerMessage)); 435 sizeof (struct GNUNET_DHT_FindPeerMessage));
525#endif 436#endif
526
527 GNUNET_assert (ntohs (find_msg->header.size) >=
528 sizeof (struct GNUNET_DHT_FindPeerMessage));
529
530 if (my_hello == NULL) 437 if (my_hello == NULL)
531 { 438 {
532#if DEBUG_DHT 439#if DEBUG_DHT
@@ -534,23 +441,18 @@ handle_dht_find_peer (void *cls, struct GNUNET_DHT_FindPeerMessage *find_msg,
534 "`%s': Our HELLO is null, can't return.\n", 441 "`%s': Our HELLO is null, can't return.\n",
535 "DHT"); 442 "DHT");
536#endif 443#endif
537
538 return; 444 return;
539 } 445 }
540
541 /* Simplistic find_peer functionality, always return our hello */ 446 /* Simplistic find_peer functionality, always return our hello */
542 hello_size = ntohs(my_hello->size); 447 hello_size = ntohs(my_hello->size);
543 tsize = hello_size + sizeof (struct GNUNET_DHT_FindPeerResultMessage); 448 tsize = hello_size + sizeof (struct GNUNET_DHT_FindPeerResultMessage);
449 // check tsize < MAX
544 find_peer_result = GNUNET_malloc (tsize); 450 find_peer_result = GNUNET_malloc (tsize);
545 find_peer_result->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_FIND_PEER_RESULT); 451 find_peer_result->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_FIND_PEER_RESULT);
546 find_peer_result->header.size = htons (tsize); 452 find_peer_result->header.size = htons (tsize);
547 find_peer_result->data_size = htons (hello_size);
548 memcpy(&find_peer_result->peer, &my_identity, sizeof(struct GNUNET_PeerIdentity));
549 memcpy (&find_peer_result[1], &my_hello, hello_size); 453 memcpy (&find_peer_result[1], &my_hello, hello_size);
550
551 send_reply_to_client(message_context->client, &find_peer_result->header, message_context->unique_id); 454 send_reply_to_client(message_context->client, &find_peer_result->header, message_context->unique_id);
552 GNUNET_free(find_peer_result); 455 GNUNET_free(find_peer_result);
553 /* FIXME: Implement find peer functionality here */
554} 456}
555 457
556 458
@@ -562,43 +464,32 @@ handle_dht_find_peer (void *cls, struct GNUNET_DHT_FindPeerMessage *find_msg,
562 * @param message_context struct containing pertinent information about the request 464 * @param message_context struct containing pertinent information about the request
563 */ 465 */
564static void 466static void
565handle_dht_put (void *cls, struct GNUNET_DHT_PutMessage *put_msg, 467handle_dht_put (void *cls,
468 const struct GNUNET_MessageHeader *msg,
566 struct DHT_MessageContext *message_context) 469 struct DHT_MessageContext *message_context)
567{ 470{
471 struct GNUNET_DHT_PutMessage *put_msg;
568 size_t put_type; 472 size_t put_type;
569 size_t data_size; 473 size_t data_size;
570 474
571 GNUNET_assert (ntohs (put_msg->header.size) >= 475 GNUNET_assert (ntohs (msg->header.size) >=
572 sizeof (struct GNUNET_DHT_PutMessage)); 476 sizeof (struct GNUNET_DHT_PutMessage));
573 477 put_msg = (struct GNUNET_DHT_PutMessage *)msg;
574 put_type = ntohs (put_msg->type); 478 put_type = ntohl (put_msg->type);
575 data_size = ntohs (put_msg->data_size); 479 data_size = ntohs (put_msg->header.size) - sizeof (struct GNUNET_DHT_PutMessage);
576#if DEBUG_DHT 480#if DEBUG_DHT
577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
578 "`%s': %s msg total size is %d, data size %d, struct size %d\n", 482 "`%s': %s msg total size is %d, data size %d, struct size %d\n",
579 "DHT", "PUT", ntohs (put_msg->header.size), data_size, 483 "DHT", "PUT", ntohs (put_msg->header.size), data_size,
580 sizeof (struct GNUNET_DHT_PutMessage)); 484 sizeof (struct GNUNET_DHT_PutMessage));
581#endif
582 GNUNET_assert (ntohs (put_msg->header.size) ==
583 sizeof (struct GNUNET_DHT_PutMessage) + data_size);
584
585#if DEBUG_DHT
586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
587 "`%s': Received `%s' request from client, message type %d, key %s\n", 486 "`%s': Received `%s' request from client, message type %d, key %s\n",
588 "DHT", "PUT", put_type, GNUNET_h2s (message_context->key)); 487 "DHT", "PUT", put_type, GNUNET_h2s (message_context->key));
589#endif 488#endif
590
591 /**
592 * Simplest DHT functionality, store any message we receive a put request for.
593 */
594 if (datacache != NULL) 489 if (datacache != NULL)
595 GNUNET_DATACACHE_put (datacache, message_context->key, data_size, 490 GNUNET_DATACACHE_put (datacache, message_context->key, data_size,
596 (char *) &put_msg[1], put_type, 491 (char *) &put_msg[1], put_type,
597 put_msg->expiration); 492 put_msg->expiration);
598 /**
599 * FIXME: Implement dht put request functionality here!
600 */
601
602} 493}
603 494
604 495
@@ -626,8 +517,6 @@ find_active_client (struct GNUNET_SERVER_Client *client)
626 ret->client_handle = client; 517 ret->client_handle = client;
627 ret->next = client_list; 518 ret->next = client_list;
628 client_list = ret; 519 client_list = ret;
629 ret->pending_head = NULL;
630
631 return ret; 520 return ret;
632} 521}
633 522
@@ -679,13 +568,12 @@ static void
679handle_dht_start_message (void *cls, struct GNUNET_SERVER_Client *client, 568handle_dht_start_message (void *cls, struct GNUNET_SERVER_Client *client,
680 const struct GNUNET_MessageHeader *message) 569 const struct GNUNET_MessageHeader *message)
681{ 570{
682 struct GNUNET_DHT_Message *dht_msg = (struct GNUNET_DHT_Message *) message; 571 const struct GNUNET_DHT_Message *dht_msg = (const struct GNUNET_DHT_Message *) message;
683 struct GNUNET_MessageHeader *enc_msg; 572 const struct GNUNET_MessageHeader *enc_msg;
684 struct DHT_MessageContext *message_context; 573 struct DHT_MessageContext *message_context;
685
686 size_t enc_type; 574 size_t enc_type;
687 575
688 enc_msg = (struct GNUNET_MessageHeader *) &dht_msg[1]; 576 enc_msg = (const struct GNUNET_MessageHeader *) &dht_msg[1];
689 enc_type = ntohs (enc_msg->type); 577 enc_type = ntohs (enc_msg->type);
690 578
691 579
@@ -700,31 +588,32 @@ handle_dht_start_message (void *cls, struct GNUNET_SERVER_Client *client,
700 message_context->client = find_active_client (client); 588 message_context->client = find_active_client (client);
701 message_context->key = &dht_msg->key; 589 message_context->key = &dht_msg->key;
702 message_context->unique_id = GNUNET_ntohll (dht_msg->unique_id); 590 message_context->unique_id = GNUNET_ntohll (dht_msg->unique_id);
703 message_context->replication = ntohs (dht_msg->desired_replication_level); 591 message_context->replication = ntohl (dht_msg->desired_replication_level);
704 message_context->msg_options = ntohs (dht_msg->options); 592 message_context->msg_options = ntohl (dht_msg->options);
705 593
594 /* FIXME: Implement *remote* DHT operations here (forward request) */
595 /* FIXME: *IF* handling should be local, then do this: */
706 switch (enc_type) 596 switch (enc_type)
707 { 597 {
708 case GNUNET_MESSAGE_TYPE_DHT_GET: 598 case GNUNET_MESSAGE_TYPE_DHT_GET:
709 handle_dht_get (cls, (struct GNUNET_DHT_GetMessage *) enc_msg, 599 handle_dht_get (cls, enc_msg,
710 message_context); 600 message_context);
711 break; 601 break;
712 case GNUNET_MESSAGE_TYPE_DHT_PUT: 602 case GNUNET_MESSAGE_TYPE_DHT_PUT:
713 handle_dht_put (cls, (struct GNUNET_DHT_PutMessage *) enc_msg, 603 handle_dht_put (cls, enc_msg,
714 message_context); 604 message_context);
715 send_client_receipt_confirmation (client, 605 send_client_receipt_confirmation (client,
716 GNUNET_ntohll (dht_msg->unique_id)); 606 GNUNET_ntohll (dht_msg->unique_id));
717 break; 607 break;
718 case GNUNET_MESSAGE_TYPE_DHT_FIND_PEER: 608 case GNUNET_MESSAGE_TYPE_DHT_FIND_PEER:
719 handle_dht_find_peer (cls, 609 handle_dht_find_peer (cls,
720 (struct GNUNET_DHT_FindPeerMessage *) enc_msg, 610 enc_msg,
721 message_context); 611 message_context);
722 break; 612 break;
723 default: 613 default:
724 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 614 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
725 "`%s': Message type (%d) not handled\n", "DHT", enc_type); 615 "`%s': Message type (%d) not handled\n", "DHT", enc_type);
726 } 616 }
727
728 GNUNET_free (message_context); 617 GNUNET_free (message_context);
729 GNUNET_SERVER_receive_done (client, GNUNET_OK); 618 GNUNET_SERVER_receive_done (client, GNUNET_OK);
730 619
@@ -745,75 +634,55 @@ static void
745handle_dht_stop_message (void *cls, struct GNUNET_SERVER_Client *client, 634handle_dht_stop_message (void *cls, struct GNUNET_SERVER_Client *client,
746 const struct GNUNET_MessageHeader *message) 635 const struct GNUNET_MessageHeader *message)
747{ 636{
748 struct GNUNET_DHT_StopMessage *dht_stop_msg = 637 const struct GNUNET_DHT_StopMessage *dht_stop_msg =
749 (struct GNUNET_DHT_StopMessage *) message; 638 (const struct GNUNET_DHT_StopMessage *) message;
750 639
751#if DEBUG_DHT 640#if DEBUG_DHT
752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
753 "`%s': Received `%s' request from client, uid %llu\n", "DHT", 642 "`%s': Received `%s' request from client, uid %llu\n", "DHT",
754 "GENERIC STOP", GNUNET_ntohll (dht_stop_msg->unique_id)); 643 "GENERIC STOP", GNUNET_ntohll (dht_stop_msg->unique_id));
755#endif 644#endif
756 645 /* TODO: actually stop... */
757 /* TODO: Put in demultiplexing here */
758
759 send_client_receipt_confirmation (client,
760 GNUNET_ntohll (dht_stop_msg->unique_id));
761 GNUNET_SERVER_receive_done (client, GNUNET_OK); 646 GNUNET_SERVER_receive_done (client, GNUNET_OK);
762} 647}
763 648
764 649
765/** 650/**
766 * Core handler for p2p dht get requests. 651 * Core handler for p2p route requests.
767 */ 652 */
768static int 653static int
769handle_dht_p2p_get (void *cls, 654handle_dht_p2p_route_request (void *cls,
770 const struct GNUNET_PeerIdentity *peer, 655 const struct GNUNET_PeerIdentity *peer,
771 const struct GNUNET_MessageHeader *message, 656 const struct GNUNET_MessageHeader *message,
772 struct GNUNET_TIME_Relative latency, uint32_t distance) 657 struct GNUNET_TIME_Relative latency, uint32_t distance)
773{ 658{
774#if DEBUG_DHT 659#if DEBUG_DHT
775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
776 "`%s': Received `%s' request from another peer\n", "DHT", 661 "`%s': Received `%s' request from another peer\n", "DHT",
777 "GET"); 662 "GET");
778#endif 663#endif
779 664 // FIXME: setup tracking for sending replies to peer (with timeout)
665 // FIXME: call code from handle_dht_start_message (refactor...)
780 return GNUNET_YES; 666 return GNUNET_YES;
781} 667}
782 668
783/**
784 * Core handler for p2p dht put requests.
785 */
786static int
787handle_dht_p2p_put (void *cls,
788 const struct GNUNET_PeerIdentity *peer,
789 const struct GNUNET_MessageHeader *message,
790 struct GNUNET_TIME_Relative latency, uint32_t distance)
791{
792#if DEBUG_DHT
793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
794 "`%s': Received `%s' request from another peer\n", "DHT",
795 "PUT");
796#endif
797
798 return GNUNET_YES;
799}
800 669
801/** 670/**
802 * Core handler for p2p dht find peer requests. 671 * Core handler for p2p route results.
803 */ 672 */
804static int 673static int
805handle_dht_p2p_find_peer (void *cls, 674handle_dht_p2p_route_result (void *cls,
806 const struct GNUNET_PeerIdentity *peer, 675 const struct GNUNET_PeerIdentity *peer,
807 const struct GNUNET_MessageHeader *message, 676 const struct GNUNET_MessageHeader *message,
808 struct GNUNET_TIME_Relative latency, 677 struct GNUNET_TIME_Relative latency, uint32_t distance)
809 uint32_t distance)
810{ 678{
811#if DEBUG_DHT 679#if DEBUG_DHT
812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
813 "`%s': Received `%s' request from another peer\n", "DHT", 681 "`%s': Received `%s' request from another peer\n", "DHT",
814 "FIND PEER"); 682 "GET");
815#endif 683#endif
816 684 // FIXME: setup tracking for sending replies to peer
685 // FIXME: possibly call code from handle_dht_stop_message? (unique result?) (refactor...)
817 return GNUNET_YES; 686 return GNUNET_YES;
818} 687}
819 688
@@ -898,6 +767,20 @@ core_init (void *cls,
898} 767}
899 768
900 769
770static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
771 {&handle_dht_start_message, NULL, GNUNET_MESSAGE_TYPE_DHT, 0},
772 {&handle_dht_stop_message, NULL, GNUNET_MESSAGE_TYPE_DHT_STOP, 0},
773 {NULL, NULL, 0, 0}
774};
775
776
777static struct GNUNET_CORE_MessageHandler core_handlers[] = {
778 {&handle_dht_p2p_route_request, GNUNET_MESSAGE_TYPE_DHT_ROUTE_REQUEST, 0},
779 {&handle_dht_p2p_route_result, GNUNET_MESSAGE_TYPE_DHT_ROUTE_RESULT, 0},
780 {NULL, 0, 0}
781};
782
783
901/** 784/**
902 * Process dht requests. 785 * Process dht requests.
903 * 786 *
@@ -914,16 +797,11 @@ run (void *cls,
914{ 797{
915 sched = scheduler; 798 sched = scheduler;
916 cfg = c; 799 cfg = c;
917
918 datacache = GNUNET_DATACACHE_create (sched, cfg, "dhtcache"); 800 datacache = GNUNET_DATACACHE_create (sched, cfg, "dhtcache");
919
920 client_transmit_timeout =
921 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5);
922 GNUNET_SERVER_add_handlers (server, plugin_handlers); 801 GNUNET_SERVER_add_handlers (server, plugin_handlers);
923
924 coreAPI = GNUNET_CORE_connect (sched, /* Main scheduler */ 802 coreAPI = GNUNET_CORE_connect (sched, /* Main scheduler */
925 cfg, /* Main configuration */ 803 cfg, /* Main configuration */
926 client_transmit_timeout, /* Delay for connecting */ 804 GNUNET_TIME_UNIT_FOREVER_REL,
927 NULL, /* FIXME: anything we want to pass around? */ 805 NULL, /* FIXME: anything we want to pass around? */
928 &core_init, /* Call core_init once connected */ 806 &core_init, /* Call core_init once connected */
929 NULL, /* Don't care about pre-connects */ 807 NULL, /* Don't care about pre-connects */
@@ -934,18 +812,13 @@ run (void *cls,
934 NULL, /* Don't want notified about all outbound messages */ 812 NULL, /* Don't want notified about all outbound messages */
935 GNUNET_NO, /* For header only outbound notification */ 813 GNUNET_NO, /* For header only outbound notification */
936 core_handlers); /* Register these handlers */ 814 core_handlers); /* Register these handlers */
937 815 if (coreAPI == NULL)
816 return;
938 transport_handle = GNUNET_TRANSPORT_connect(sched, cfg, NULL, NULL, NULL, NULL); 817 transport_handle = GNUNET_TRANSPORT_connect(sched, cfg, NULL, NULL, NULL, NULL);
939
940 if (transport_handle != NULL) 818 if (transport_handle != NULL)
941 GNUNET_TRANSPORT_get_hello (transport_handle, &process_hello, NULL); 819 GNUNET_TRANSPORT_get_hello (transport_handle, &process_hello, NULL);
942 else 820 else
943 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to connect to transport service!\n"); 821 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to connect to transport service!\n");
944
945
946 if (coreAPI == NULL)
947 return;
948
949 /* Scheduled the task to clean up when shutdown is called */ 822 /* Scheduled the task to clean up when shutdown is called */
950 cleanup_task = GNUNET_SCHEDULER_add_delayed (sched, 823 cleanup_task = GNUNET_SCHEDULER_add_delayed (sched,
951 GNUNET_TIME_UNIT_FOREVER_REL, 824 GNUNET_TIME_UNIT_FOREVER_REL,