aboutsummaryrefslogtreecommitdiff
path: root/src/dht/gnunet-service-dht.c
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/gnunet-service-dht.c
parent513a6ef355fcbabeb1953055185608ed4dacbe69 (diff)
downloadgnunet-54eccc36f12430505b3b946f88ac61087cc82793.tar.gz
gnunet-54eccc36f12430505b3b946f88ac61087cc82793.zip
breaking DHT code
Diffstat (limited to 'src/dht/gnunet-service-dht.c')
-rw-r--r--src/dht/gnunet-service-dht.c399
1 files changed, 136 insertions, 263 deletions
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,