2 This file is part of GNUnet.
3 (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file dht/gnunet-service-dht_clients.c
23 * @brief GNUnet DHT service's client management code
24 * @author Christian Grothoff
25 * @author Nathan Evans
29 #include "gnunet_protocols.h"
30 #include "gnunet_statistics_service.h"
31 #include "gnunet-service-dht.h"
32 #include "gnunet-service-dht_clients.h"
33 #include "gnunet-service-dht_datacache.h"
34 #include "gnunet-service-dht_neighbours.h"
39 * Linked list of messages to send to clients.
44 * Pointer to next item in the list
46 struct PendingMessage *next;
49 * Pointer to previous item in the list
51 struct PendingMessage *prev;
54 * Actual message to be sent, allocated at the end of the struct:
55 * // msg = (cast) &pm[1];
56 * // memcpy (&pm[1], data, len);
58 const struct GNUNET_MessageHeader *msg;
64 * Struct containing information about a client,
65 * handle to connect to it, and any pending messages
66 * that need to be sent to it.
71 * Linked list of active clients
73 struct ClientList *next;
76 * Linked list of active clients
78 struct ClientList *prev;
81 * The handle to this client
83 struct GNUNET_SERVER_Client *client_handle;
86 * Handle to the current transmission request, NULL
89 struct GNUNET_CONNECTION_TransmitHandle *transmit_handle;
92 * Linked list of pending messages for this client
94 struct PendingMessage *pending_head;
97 * Tail of linked list of pending messages for this client
99 struct PendingMessage *pending_tail;
105 * Entry in the DHT routing table for a client's GET request.
107 struct ClientQueryRecord
111 * The key this request was about
116 * Client responsible for the request.
118 struct ClientList *client;
121 * Extended query (see gnunet_block_lib.h), allocated at the end of this struct.
126 * Replies we have already seen for this request.
128 GNUNET_HashCode *seen_replies;
131 * Pointer to this nodes heap location in the retry-heap (for fast removal)
133 struct GNUNET_CONTAINER_HeapNode *hnode;
136 * What's the delay between re-try operations that we currently use for this
139 struct GNUNET_TIME_Relative retry_frequency;
142 * What's the next time we should re-try this request?
144 struct GNUNET_TIME_Absolute retry_time;
147 * The unique identifier of this request
152 * Number of bytes in xquery.
157 * Number of entries in 'seen_replies'.
159 unsigned int seen_replies_count;
162 * Desired replication level
164 uint32_t replication;
167 * Any message options for this request
169 uint32_t msg_options;
172 * The type for the data for the GET request.
174 enum GNUNET_BLOCK_Type type;
180 * List of active clients.
182 static struct ClientList *client_head;
185 * List of active clients.
187 static struct ClientList *client_tail;
190 * Hashmap for fast key based lookup, maps keys to 'struct ClientQueryRecord' entries.
192 static struct GNUNET_CONTAINER_MultiHashMap *forward_map;
195 * Heap with all of our client's request, sorted by retry time (earliest on top).
197 static struct GNUNET_CONTAINER_Heap *retry_heap;
200 * Task that re-transmits requests (using retry_heap).
202 static GNUNET_SCHEDULER_TaskIdentifier retry_task;
206 * Find a client if it exists, add it otherwise.
208 * @param client the server handle to the client
210 * @return the client if found, a new client otherwise
212 static struct ClientList *
213 find_active_client (struct GNUNET_SERVER_Client *client)
215 struct ClientList *pos = client_head;
216 struct ClientList *ret;
220 if (pos->client_handle == client)
224 ret = GNUNET_malloc (sizeof (struct ClientList));
225 ret->client_handle = client;
226 GNUNET_CONTAINER_DLL_insert (client_head,
234 * Iterator over hash map entries that frees all entries
235 * associated with the given client.
237 * @param cls client to search for in source routes
238 * @param key current key code (ignored)
239 * @param value value in the hash map, a ClientQueryRecord
240 * @return GNUNET_YES (we should continue to iterate)
243 remove_client_records (void *cls, const GNUNET_HashCode * key, void *value)
245 struct ClientList *client = cls;
246 struct ClientQueryRecord *record = value;
248 if (record->client != client)
250 GNUNET_assert (GNUNET_YES ==
251 GNUNET_CONTAINER_multihashmap_remove (forward_map,
253 if (NULL != record->hnode)
254 GNUNET_CONTAINER_heap_remove_node (record->hnode);
255 GNUNET_array_grow (record->seen_replies,
256 record->seen_replies_count,
258 GNUNET_free (record);
264 * Functions with this signature are called whenever a client
265 * is disconnected on the network level.
267 * @param cls closure (NULL for dht)
268 * @param client identification of the client; NULL
269 * for the last call when the server is destroyed
272 handle_client_disconnect (void *cls,
273 struct GNUNET_SERVER_Client *client)
275 struct ClientList *pos;
276 struct PendingMessage *reply;
278 pos = find_active_client (client);
279 GNUNET_CONTAINER_DLL_remove (client_head,
282 if (pos->transmit_handle != NULL)
283 GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->transmit_handle);
284 while (NULL != (reply = pos->pending_head))
286 GNUNET_CONTAINER_DLL_remove (pos->pending_head, pos->pending_tail,
290 GNUNET_CONTAINER_multihashmap_iterate (forward_map,
291 &remove_client_records, pos);
297 * Route the given request via the DHT. This includes updating
298 * the bloom filter and retransmission times, building the P2P
299 * message and initiating the routing operation.
302 transmit_request (struct ClientQueryRecord *cqr)
304 int32_t reply_bf_mutator;
305 struct GNUNET_CONTAINER_BloomFilter *reply_bf;
307 reply_bf_mutator = (int32_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
309 reply_bf = GNUNET_BLOCK_construct_bloomfilter (reply_bf_mutator,
311 cqr->seen_replies_count);
312 GDS_NEIGHBOURS_handle_get (cqr->type,
321 NULL /* no peers blocked initially */);
322 GNUNET_CONTAINER_bloomfilter_free (reply_bf);
324 /* exponential back-off for retries, max 1h */
325 cqr->retry_frequency =
326 GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_HOURS,
327 GNUNET_TIME_relative_multiply (cqr->retry_frequency, 2));
328 cqr->retry_time = GNUNET_TIME_relative_to_absolute (cqr->retry_frequency);
333 * Task that looks at the 'retry_heap' and transmits all of the requests
334 * on the heap that are ready for transmission. Then re-schedules
335 * itself (unless the heap is empty).
338 * @param tc scheduler context
341 transmit_next_request_task (void *cls,
342 const struct GNUNET_SCHEDULER_TaskContext *tc)
344 struct ClientQueryRecord *cqr;
345 struct GNUNET_TIME_Relative delay;
347 retry_task = GNUNET_SCHEDULER_NO_TASK;
348 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
350 while (NULL != (cqr = GNUNET_CONTAINER_heap_remove_root (retry_heap)))
353 delay = GNUNET_TIME_absolute_get_remaining (cqr->retry_time);
354 if (delay.rel_value > 0)
356 cqr->hnode = GNUNET_CONTAINER_heap_insert (retry_heap, cqr,
357 cqr->retry_time.abs_value);
358 retry_task = GNUNET_SCHEDULER_add_delayed (delay,
359 &transmit_next_request_task,
363 transmit_request (cqr);
369 * Handler for PUT messages.
371 * @param cls closure for the service
372 * @param client the client we received this message from
373 * @param message the actual message received
376 handle_dht_local_put (void *cls, struct GNUNET_SERVER_Client *client,
377 const struct GNUNET_MessageHeader *message)
379 const struct GNUNET_DHT_ClientPutMessage *dht_msg;
382 size = ntohs (message->size);
383 if (size < sizeof (struct GNUNET_DHT_ClientPutMessage))
386 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
388 dht_msg = (const struct GNUNET_DHT_ClientPutMessage *) message;
389 /* give to local clients */
390 GDS_CLIENTS_handle_reply (GNUNET_TIME_absolute_ntoh (dht_msg->expiration),
394 ntohl (dht_msg->type),
395 size - sizeof (struct GNUNET_DHT_ClientPutMessage),
398 GDS_DATACACHE_handle_put (GNUNET_TIME_absolute_ntoh (dht_msg->expiration),
401 ntohl (dht_msg->type),
402 size - sizeof (struct GNUNET_DHT_ClientPutMessage),
404 /* route to other peers */
405 GDS_NEIGHBOURS_handle_put (ntohl (dht_msg->type),
406 ntohl (dht_msg->options),
407 ntohl (dht_msg->desired_replication_level),
408 GNUNET_TIME_absolute_ntoh (dht_msg->expiration),
410 NULL /* peer bloom filter */,
414 size - sizeof (struct GNUNET_DHT_ClientPutMessage));
415 GNUNET_SERVER_receive_done (client, GNUNET_OK);
420 * Handler for any generic DHT messages, calls the appropriate handler
421 * depending on message type, sends confirmation if responses aren't otherwise
424 * @param cls closure for the service
425 * @param client the client we received this message from
426 * @param message the actual message received
429 handle_dht_local_get (void *cls, struct GNUNET_SERVER_Client *client,
430 const struct GNUNET_MessageHeader *message)
432 const struct GNUNET_DHT_ClientGetMessage *get;
433 struct ClientQueryRecord *cqr;
438 size = ntohs (message->size);
439 if (size < sizeof (struct GNUNET_DHT_ClientGetMessage))
442 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
445 xquery_size = size - sizeof (struct GNUNET_DHT_ClientGetMessage);
446 get = (const struct GNUNET_DHT_ClientGetMessage *) message;
447 xquery = (const char*) &get[1];
450 cqr = GNUNET_malloc (sizeof (struct ClientQueryRecord) + xquery_size);
452 cqr->client = find_active_client (client);
453 cqr->xquery = (void*) &cqr[1];
454 memcpy (&cqr[1], xquery, xquery_size);
455 cqr->hnode = GNUNET_CONTAINER_heap_insert (retry_heap, cqr, 0);
456 cqr->retry_frequency = GNUNET_TIME_UNIT_MILLISECONDS;
457 cqr->retry_time = GNUNET_TIME_absolute_get ();
458 cqr->unique_id = get->unique_id;
459 cqr->xquery_size = xquery_size;
460 cqr->replication = ntohl (get->desired_replication_level);
461 cqr->msg_options = ntohl (get->options);
462 cqr->type = ntohl (get->type);
463 GNUNET_CONTAINER_multihashmap_put (forward_map, &get->key, cqr,
464 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
465 /* start remote requests */
466 if (GNUNET_SCHEDULER_NO_TASK != retry_task)
467 GNUNET_SCHEDULER_cancel (retry_task);
468 retry_task = GNUNET_SCHEDULER_add_now (&transmit_next_request_task, NULL);
469 /* perform local lookup */
470 GDS_DATACACHE_handle_get (&get->key,
475 GNUNET_SERVER_receive_done (client, GNUNET_OK);
480 * Closure for 'remove_by_unique_id'.
482 struct RemoveByUniqueIdContext
485 * Client that issued the removal request.
487 struct ClientList *client;
490 * Unique ID of the request.
497 * Iterator over hash map entries that frees all entries
498 * that match the given client and unique ID.
500 * @param cls unique ID and client to search for in source routes
501 * @param key current key code
502 * @param value value in the hash map, a ClientQueryRecord
503 * @return GNUNET_YES (we should continue to iterate)
506 remove_by_unique_id (void *cls, const GNUNET_HashCode * key, void *value)
508 const struct RemoveByUniqueIdContext *ctx = cls;
509 struct ClientQueryRecord *record = value;
511 if (record->unique_id != ctx->unique_id)
513 return remove_client_records (ctx->client, key, record);
518 * Handler for any generic DHT stop messages, calls the appropriate handler
519 * depending on message type (if processed locally)
521 * @param cls closure for the service
522 * @param client the client we received this message from
523 * @param message the actual message received
527 handle_dht_local_get_stop (void *cls, struct GNUNET_SERVER_Client *client,
528 const struct GNUNET_MessageHeader *message)
530 const struct GNUNET_DHT_ClientGetStopMessage *dht_stop_msg =
531 (const struct GNUNET_DHT_ClientGetStopMessage *) message;
532 struct RemoveByUniqueIdContext ctx;
534 ctx.client = find_active_client (client);
535 ctx.unique_id = dht_stop_msg->unique_id;
536 GNUNET_CONTAINER_multihashmap_get_multiple (forward_map,
538 &remove_by_unique_id,
540 GNUNET_SERVER_receive_done (client, GNUNET_OK);
545 * Task run to check for messages that need to be sent to a client.
547 * @param client a ClientList, containing the client and any messages to be sent to it
550 process_pending_messages (struct ClientList *client);
554 * Callback called as a result of issuing a GNUNET_SERVER_notify_transmit_ready
555 * request. A ClientList is passed as closure, take the head of the list
556 * and copy it into buf, which has the result of sending the message to the
559 * @param cls closure to this call
560 * @param size maximum number of bytes available to send
561 * @param buf where to copy the actual message to
563 * @return the number of bytes actually copied, 0 indicates failure
566 send_reply_to_client (void *cls, size_t size, void *buf)
568 struct ClientList *client = cls;
570 struct PendingMessage *reply;
574 client->transmit_handle = NULL;
577 /* client disconnected */
581 while ((NULL != (reply = client->pending_head)) &&
582 (size >= off + (msize = ntohs (reply->msg->size))))
584 GNUNET_CONTAINER_DLL_remove (client->pending_head, client->pending_tail,
586 memcpy (&cbuf[off], reply->msg, msize);
590 process_pending_messages (client);
596 * Task run to check for messages that need to be sent to a client.
598 * @param client a ClientList, containing the client and any messages to be sent to it
601 process_pending_messages (struct ClientList *client)
603 if ((client->pending_head == NULL) || (client->transmit_handle != NULL))
605 client->transmit_handle =
606 GNUNET_SERVER_notify_transmit_ready (client->client_handle,
607 ntohs (client->pending_head->
609 GNUNET_TIME_UNIT_FOREVER_REL,
610 &send_reply_to_client, client);
615 * Add a PendingMessage to the clients list of messages to be sent
617 * @param client the active client to send the message to
618 * @param pending_message the actual message to send
621 add_pending_message (struct ClientList *client,
622 struct PendingMessage *pending_message)
624 GNUNET_CONTAINER_DLL_insert_tail (client->pending_head, client->pending_tail,
626 process_pending_messages (client);
631 * Closure for 'forward_reply'
633 struct ForwardReplyContext
637 * Actual message to send to matching clients.
639 struct PendingMessage *pm;
649 enum GNUNET_BLOCK_Type type;
652 * Number of bytes in data.
657 * Do we need to copy 'pm' because it was already used?
665 * Iterator over hash map entries that send a given reply to
666 * each of the matching clients. With some tricky recycling
669 * @param cls the 'struct ForwardReplyContext'
670 * @param key current key
671 * @param value value in the hash map, a ClientQueryRecord
672 * @return GNUNET_YES (we should continue to iterate),
673 * if the result is mal-formed, GNUNET_NO
676 forward_reply (void *cls, const GNUNET_HashCode * key, void *value)
678 struct ForwardReplyContext *frc = cls;
679 struct ClientQueryRecord *record = value;
680 struct PendingMessage *pm;
681 struct GNUNET_DHT_ClientResultMessage *reply;
682 enum GNUNET_BLOCK_EvaluationResult eval;
687 if ( (record->type != GNUNET_BLOCK_TYPE_ANY) &&
688 (record->type != frc->type) )
689 return GNUNET_YES; /* type mismatch */
690 GNUNET_CRYPTO_hash (frc->data,
693 for (i=0;i<record->seen_replies_count;i++)
694 if (0 == memcmp (&record->seen_replies[i],
696 sizeof (GNUNET_HashCode)))
697 return GNUNET_YES; /* duplicate */
699 GNUNET_BLOCK_evaluate (GDS_block_context,
708 case GNUNET_BLOCK_EVALUATION_OK_LAST:
709 do_free = GNUNET_YES;
711 case GNUNET_BLOCK_EVALUATION_OK_MORE:
712 GNUNET_array_append (record->seen_replies,
713 record->seen_replies_count,
717 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
718 /* should be impossible to encounter here */
721 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
724 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
727 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
730 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
731 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
732 "Unsupported block type (%u) in request!\n",
736 if (GNUNET_NO == frc->do_copy)
738 /* first time, we can use the original data */
740 frc->do_copy = GNUNET_YES;
744 /* two clients waiting for same reply, must copy for queueing */
745 pm = GNUNET_malloc (sizeof (struct PendingMessage) +
746 ntohs (frc->pm->msg->size));
748 sizeof (struct PendingMessage) + ntohs (frc->pm->msg->size));
749 pm->next = pm->prev = NULL;
751 reply = (struct GNUNET_DHT_ClientResultMessage*) &pm[1];
752 reply->unique_id = record->unique_id;
753 add_pending_message (record->client, pm);
754 if (GNUNET_YES == do_free)
755 remove_client_records (record->client, key, record);
761 * Handle a reply we've received from another peer. If the reply
762 * matches any of our pending queries, forward it to the respective
765 * @param expiration when will the reply expire
766 * @param key the query this reply is for
767 * @param get_path_length number of peers in 'get_path'
768 * @param get_path path the reply took on get
769 * @param put_path_length number of peers in 'put_path'
770 * @param put_path path the reply took on put
771 * @param type type of the reply
772 * @param data_size number of bytes in 'data'
773 * @param data application payload data
776 GDS_CLIENTS_handle_reply (struct GNUNET_TIME_Absolute expiration,
777 const GNUNET_HashCode *key,
778 unsigned int get_path_length,
779 const struct GNUNET_PeerIdentity *get_path,
780 unsigned int put_path_length,
781 const struct GNUNET_PeerIdentity *put_path,
782 enum GNUNET_BLOCK_Type type,
786 struct ForwardReplyContext frc;
787 struct PendingMessage *pm;
788 struct GNUNET_DHT_ClientResultMessage *reply;
789 struct GNUNET_PeerIdentity *paths;
793 GNUNET_CONTAINER_multihashmap_get (forward_map, key))
794 return; /* no matching request, fast exit! */
795 msize = sizeof(struct GNUNET_DHT_ClientResultMessage) + data_size +
796 (get_path_length + put_path_length) * sizeof (struct GNUNET_PeerIdentity);
797 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
799 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
800 _("Could not pass reply to client, message too big!\n"));
803 pm = (struct PendingMessage *) GNUNET_malloc (msize + sizeof (struct PendingMessage));
804 reply = (struct GNUNET_DHT_ClientResultMessage*) &pm[1];
805 pm->msg = &reply->header;
806 reply->header.size = htons ((uint16_t) msize);
807 reply->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_CLIENT_RESULT);
808 reply->type = htonl (type);
809 reply->get_path_length = htonl (get_path_length);
810 reply->put_path_length = htonl (put_path_length);
811 reply->unique_id = 0; /* filled in later */
812 reply->expiration = GNUNET_TIME_absolute_hton (expiration);
814 paths = (struct GNUNET_PeerIdentity*) &reply[1];
815 memcpy (paths, get_path,
816 sizeof (struct GNUNET_PeerIdentity) * get_path_length);
817 memcpy (&paths[get_path_length],
818 put_path, sizeof (struct GNUNET_PeerIdentity) * put_path_length);
819 memcpy (&paths[get_path_length + put_path_length],
822 frc.do_copy = GNUNET_NO;
825 frc.data_size = data_size;
827 GNUNET_CONTAINER_multihashmap_get_multiple (forward_map, key,
830 if (GNUNET_NO == frc.do_copy)
832 /* did not match any of the requests, free! */
839 * Initialize client subsystem.
841 * @param server the initialized server
844 GDS_CLIENTS_init (struct GNUNET_SERVER_Handle *server)
846 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
847 {&handle_dht_local_put, NULL,
848 GNUNET_MESSAGE_TYPE_DHT_CLIENT_PUT, 0},
849 {&handle_dht_local_get, NULL,
850 GNUNET_MESSAGE_TYPE_DHT_CLIENT_GET, 0},
851 {&handle_dht_local_get_stop, NULL,
852 GNUNET_MESSAGE_TYPE_DHT_CLIENT_GET_STOP,
853 sizeof (struct GNUNET_DHT_ClientGetStopMessage) },
856 forward_map = GNUNET_CONTAINER_multihashmap_create (1024);
857 retry_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
858 GNUNET_SERVER_add_handlers (server, plugin_handlers);
859 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
864 * Shutdown client subsystem.
869 GNUNET_assert (client_head == NULL);
870 GNUNET_assert (client_tail == NULL);
871 if (GNUNET_SCHEDULER_NO_TASK != retry_task)
873 GNUNET_SCHEDULER_cancel (retry_task);
874 retry_task = GNUNET_SCHEDULER_NO_TASK;
876 GNUNET_assert (0 == GNUNET_CONTAINER_heap_get_size (retry_heap));
877 GNUNET_CONTAINER_heap_destroy (retry_heap);
879 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (forward_map));
880 GNUNET_CONTAINER_multihashmap_destroy (forward_map);
884 /* end of gnunet-service-dht_clients.c */