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_constants.h"
30 #include "gnunet_protocols.h"
31 #include "gnunet_statistics_service.h"
32 #include "gnunet-service-dht.h"
33 #include "gnunet-service-dht_clients.h"
34 #include "gnunet-service-dht_datacache.h"
35 #include "gnunet-service-dht_neighbours.h"
40 * Linked list of messages to send to clients.
45 * Pointer to next item in the list
47 struct PendingMessage *next;
50 * Pointer to previous item in the list
52 struct PendingMessage *prev;
55 * Actual message to be sent, allocated at the end of the struct:
56 * // msg = (cast) &pm[1];
57 * // memcpy (&pm[1], data, len);
59 const struct GNUNET_MessageHeader *msg;
65 * Struct containing information about a client,
66 * handle to connect to it, and any pending messages
67 * that need to be sent to it.
72 * Linked list of active clients
74 struct ClientList *next;
77 * Linked list of active clients
79 struct ClientList *prev;
82 * The handle to this client
84 struct GNUNET_SERVER_Client *client_handle;
87 * Handle to the current transmission request, NULL
90 struct GNUNET_CONNECTION_TransmitHandle *transmit_handle;
93 * Linked list of pending messages for this client
95 struct PendingMessage *pending_head;
98 * Tail of linked list of pending messages for this client
100 struct PendingMessage *pending_tail;
106 * Entry in the DHT routing table for a client's GET request.
108 struct ClientQueryRecord
112 * The key this request was about
117 * Client responsible for the request.
119 struct ClientList *client;
122 * Extended query (see gnunet_block_lib.h), allocated at the end of this struct.
127 * Replies we have already seen for this request.
129 GNUNET_HashCode *seen_replies;
132 * Pointer to this nodes heap location in the retry-heap (for fast removal)
134 struct GNUNET_CONTAINER_HeapNode *hnode;
137 * What's the delay between re-try operations that we currently use for this
140 struct GNUNET_TIME_Relative retry_frequency;
143 * What's the next time we should re-try this request?
145 struct GNUNET_TIME_Absolute retry_time;
148 * The unique identifier of this request
153 * Number of bytes in xquery.
158 * Number of entries in 'seen_replies'.
160 unsigned int seen_replies_count;
163 * Desired replication level
165 uint32_t replication;
168 * Any message options for this request
170 uint32_t msg_options;
173 * The type for the data for the GET request.
175 enum GNUNET_BLOCK_Type type;
181 * List of active clients.
183 static struct ClientList *client_head;
186 * List of active clients.
188 static struct ClientList *client_tail;
191 * Hashmap for fast key based lookup, maps keys to 'struct ClientQueryRecord' entries.
193 static struct GNUNET_CONTAINER_MultiHashMap *forward_map;
196 * Heap with all of our client's request, sorted by retry time (earliest on top).
198 static struct GNUNET_CONTAINER_Heap *retry_heap;
201 * Task that re-transmits requests (using retry_heap).
203 static GNUNET_SCHEDULER_TaskIdentifier retry_task;
207 * Find a client if it exists, add it otherwise.
209 * @param client the server handle to the client
211 * @return the client if found, a new client otherwise
213 static struct ClientList *
214 find_active_client (struct GNUNET_SERVER_Client *client)
216 struct ClientList *pos = client_head;
217 struct ClientList *ret;
221 if (pos->client_handle == client)
225 ret = GNUNET_malloc (sizeof (struct ClientList));
226 ret->client_handle = client;
227 GNUNET_CONTAINER_DLL_insert (client_head, client_tail, ret);
233 * Iterator over hash map entries that frees all entries
234 * associated with the given client.
236 * @param cls client to search for in source routes
237 * @param key current key code (ignored)
238 * @param value value in the hash map, a ClientQueryRecord
239 * @return GNUNET_YES (we should continue to iterate)
242 remove_client_records (void *cls, const GNUNET_HashCode * key, void *value)
244 struct ClientList *client = cls;
245 struct ClientQueryRecord *record = value;
247 if (record->client != client)
250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
251 "Removing client %p's record for key %s\n", client,
254 GNUNET_assert (GNUNET_YES ==
255 GNUNET_CONTAINER_multihashmap_remove (forward_map, key,
257 if (NULL != record->hnode)
258 GNUNET_CONTAINER_heap_remove_node (record->hnode);
259 GNUNET_array_grow (record->seen_replies, record->seen_replies_count, 0);
260 GNUNET_free (record);
266 * Functions with this signature are called whenever a client
267 * is disconnected on the network level.
269 * @param cls closure (NULL for dht)
270 * @param client identification of the client; NULL
271 * for the last call when the server is destroyed
274 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
276 struct ClientList *pos;
277 struct PendingMessage *reply;
280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Local client %p disconnects\n", client);
282 pos = find_active_client (client);
283 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, pos);
284 if (pos->transmit_handle != NULL)
285 GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->transmit_handle);
286 while (NULL != (reply = pos->pending_head))
288 GNUNET_CONTAINER_DLL_remove (pos->pending_head, pos->pending_tail, reply);
291 GNUNET_CONTAINER_multihashmap_iterate (forward_map, &remove_client_records,
298 * Route the given request via the DHT. This includes updating
299 * the bloom filter and retransmission times, building the P2P
300 * message and initiating the routing operation.
303 transmit_request (struct ClientQueryRecord *cqr)
305 int32_t reply_bf_mutator;
306 struct GNUNET_CONTAINER_BloomFilter *reply_bf;
307 struct GNUNET_CONTAINER_BloomFilter *peer_bf;
309 GNUNET_STATISTICS_update (GDS_stats,
311 ("# GET requests from clients injected"), 1,
314 (int32_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
317 GNUNET_BLOCK_construct_bloomfilter (reply_bf_mutator, cqr->seen_replies,
318 cqr->seen_replies_count);
320 GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE,
321 GNUNET_CONSTANTS_BLOOMFILTER_K);
322 GDS_NEIGHBOURS_handle_get (cqr->type, cqr->msg_options, cqr->replication,
324 &cqr->key, cqr->xquery, cqr->xquery_size, reply_bf,
325 reply_bf_mutator, peer_bf);
326 GNUNET_CONTAINER_bloomfilter_free (reply_bf);
327 GNUNET_CONTAINER_bloomfilter_free (peer_bf);
329 /* exponential back-off for retries, max 1h */
330 cqr->retry_frequency =
331 GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_HOURS,
332 GNUNET_TIME_relative_multiply
333 (cqr->retry_frequency, 2));
334 cqr->retry_time = GNUNET_TIME_relative_to_absolute (cqr->retry_frequency);
339 * Task that looks at the 'retry_heap' and transmits all of the requests
340 * on the heap that are ready for transmission. Then re-schedules
341 * itself (unless the heap is empty).
344 * @param tc scheduler context
347 transmit_next_request_task (void *cls,
348 const struct GNUNET_SCHEDULER_TaskContext *tc)
350 struct ClientQueryRecord *cqr;
351 struct GNUNET_TIME_Relative delay;
353 retry_task = GNUNET_SCHEDULER_NO_TASK;
354 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
356 while (NULL != (cqr = GNUNET_CONTAINER_heap_remove_root (retry_heap)))
359 delay = GNUNET_TIME_absolute_get_remaining (cqr->retry_time);
360 if (delay.rel_value > 0)
363 GNUNET_CONTAINER_heap_insert (retry_heap, cqr,
364 cqr->retry_time.abs_value);
366 GNUNET_SCHEDULER_add_delayed (delay, &transmit_next_request_task,
370 transmit_request (cqr);
372 GNUNET_CONTAINER_heap_insert (retry_heap, cqr,
373 cqr->retry_time.abs_value);
379 * Handler for PUT messages.
381 * @param cls closure for the service
382 * @param client the client we received this message from
383 * @param message the actual message received
386 handle_dht_local_put (void *cls, struct GNUNET_SERVER_Client *client,
387 const struct GNUNET_MessageHeader *message)
389 const struct GNUNET_DHT_ClientPutMessage *dht_msg;
390 struct GNUNET_CONTAINER_BloomFilter *peer_bf;
393 size = ntohs (message->size);
394 if (size < sizeof (struct GNUNET_DHT_ClientPutMessage))
397 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
400 GNUNET_STATISTICS_update (GDS_stats,
402 ("# PUT requests received from clients"), 1,
404 dht_msg = (const struct GNUNET_DHT_ClientPutMessage *) message;
405 /* give to local clients */
407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
408 "Handling local PUT of %u-bytes for query %s\n",
409 size - sizeof (struct GNUNET_DHT_ClientPutMessage),
410 GNUNET_h2s (&dht_msg->key));
412 GDS_CLIENTS_handle_reply (GNUNET_TIME_absolute_ntoh (dht_msg->expiration),
413 &dht_msg->key, 0, NULL, 0, NULL,
414 ntohl (dht_msg->type),
415 size - sizeof (struct GNUNET_DHT_ClientPutMessage),
418 GDS_DATACACHE_handle_put (GNUNET_TIME_absolute_ntoh (dht_msg->expiration),
419 &dht_msg->key, 0, NULL, ntohl (dht_msg->type),
420 size - sizeof (struct GNUNET_DHT_ClientPutMessage),
422 /* route to other peers */
424 GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE,
425 GNUNET_CONSTANTS_BLOOMFILTER_K);
426 GDS_NEIGHBOURS_handle_put (ntohl (dht_msg->type), ntohl (dht_msg->options),
427 ntohl (dht_msg->desired_replication_level),
428 GNUNET_TIME_absolute_ntoh (dht_msg->expiration),
430 peer_bf, &dht_msg->key, 0, NULL, &dht_msg[1],
432 sizeof (struct GNUNET_DHT_ClientPutMessage));
433 GNUNET_CONTAINER_bloomfilter_free (peer_bf);
434 GNUNET_SERVER_receive_done (client, GNUNET_OK);
439 * Handler for any generic DHT messages, calls the appropriate handler
440 * depending on message type, sends confirmation if responses aren't otherwise
443 * @param cls closure for the service
444 * @param client the client we received this message from
445 * @param message the actual message received
448 handle_dht_local_get (void *cls, struct GNUNET_SERVER_Client *client,
449 const struct GNUNET_MessageHeader *message)
451 const struct GNUNET_DHT_ClientGetMessage *get;
452 struct ClientQueryRecord *cqr;
457 size = ntohs (message->size);
458 if (size < sizeof (struct GNUNET_DHT_ClientGetMessage))
461 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
464 xquery_size = size - sizeof (struct GNUNET_DHT_ClientGetMessage);
465 get = (const struct GNUNET_DHT_ClientGetMessage *) message;
466 xquery = (const char *) &get[1];
467 GNUNET_STATISTICS_update (GDS_stats,
469 ("# GET requests received from clients"), 1,
472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
473 "Received request for %s from local client %p\n",
474 GNUNET_h2s (&get->key), client);
476 cqr = GNUNET_malloc (sizeof (struct ClientQueryRecord) + xquery_size);
478 cqr->client = find_active_client (client);
479 cqr->xquery = (void *) &cqr[1];
480 memcpy (&cqr[1], xquery, xquery_size);
481 cqr->hnode = GNUNET_CONTAINER_heap_insert (retry_heap, cqr, 0);
482 cqr->retry_frequency = GNUNET_TIME_UNIT_MILLISECONDS;
483 cqr->retry_time = GNUNET_TIME_absolute_get ();
484 cqr->unique_id = get->unique_id;
485 cqr->xquery_size = xquery_size;
486 cqr->replication = ntohl (get->desired_replication_level);
487 cqr->msg_options = ntohl (get->options);
488 cqr->type = ntohl (get->type);
489 GNUNET_CONTAINER_multihashmap_put (forward_map, &get->key, cqr,
490 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
491 /* start remote requests */
492 if (GNUNET_SCHEDULER_NO_TASK != retry_task)
493 GNUNET_SCHEDULER_cancel (retry_task);
494 retry_task = GNUNET_SCHEDULER_add_now (&transmit_next_request_task, NULL);
495 /* perform local lookup */
496 GDS_DATACACHE_handle_get (&get->key, cqr->type, cqr->xquery, xquery_size,
498 GNUNET_SERVER_receive_done (client, GNUNET_OK);
503 * Closure for 'remove_by_unique_id'.
505 struct RemoveByUniqueIdContext
508 * Client that issued the removal request.
510 struct ClientList *client;
513 * Unique ID of the request.
520 * Iterator over hash map entries that frees all entries
521 * that match the given client and unique ID.
523 * @param cls unique ID and client to search for in source routes
524 * @param key current key code
525 * @param value value in the hash map, a ClientQueryRecord
526 * @return GNUNET_YES (we should continue to iterate)
529 remove_by_unique_id (void *cls, const GNUNET_HashCode * key, void *value)
531 const struct RemoveByUniqueIdContext *ctx = cls;
532 struct ClientQueryRecord *record = value;
534 if (record->unique_id != ctx->unique_id)
537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
538 "Removing client %p's record for key %s (by unique id)\n",
539 ctx->client->client_handle, GNUNET_h2s (key));
541 return remove_client_records (ctx->client, key, record);
546 * Handler for any generic DHT stop messages, calls the appropriate handler
547 * depending on message type (if processed locally)
549 * @param cls closure for the service
550 * @param client the client we received this message from
551 * @param message the actual message received
555 handle_dht_local_get_stop (void *cls, struct GNUNET_SERVER_Client *client,
556 const struct GNUNET_MessageHeader *message)
558 const struct GNUNET_DHT_ClientGetStopMessage *dht_stop_msg =
559 (const struct GNUNET_DHT_ClientGetStopMessage *) message;
560 struct RemoveByUniqueIdContext ctx;
562 GNUNET_STATISTICS_update (GDS_stats,
564 ("# GET STOP requests received from clients"), 1,
567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %p stopped request for key %s\n",
568 client, GNUNET_h2s (&dht_stop_msg->key));
570 ctx.client = find_active_client (client);
571 ctx.unique_id = dht_stop_msg->unique_id;
572 GNUNET_CONTAINER_multihashmap_get_multiple (forward_map, &dht_stop_msg->key,
573 &remove_by_unique_id, &ctx);
574 GNUNET_SERVER_receive_done (client, GNUNET_OK);
579 * Task run to check for messages that need to be sent to a client.
581 * @param client a ClientList, containing the client and any messages to be sent to it
584 process_pending_messages (struct ClientList *client);
588 * Callback called as a result of issuing a GNUNET_SERVER_notify_transmit_ready
589 * request. A ClientList is passed as closure, take the head of the list
590 * and copy it into buf, which has the result of sending the message to the
593 * @param cls closure to this call
594 * @param size maximum number of bytes available to send
595 * @param buf where to copy the actual message to
597 * @return the number of bytes actually copied, 0 indicates failure
600 send_reply_to_client (void *cls, size_t size, void *buf)
602 struct ClientList *client = cls;
604 struct PendingMessage *reply;
608 client->transmit_handle = NULL;
611 /* client disconnected */
613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
614 "Client %p disconnected, pending messages will be discarded\n",
615 client->client_handle);
620 while ((NULL != (reply = client->pending_head)) &&
621 (size >= off + (msize = ntohs (reply->msg->size))))
623 GNUNET_CONTAINER_DLL_remove (client->pending_head, client->pending_tail,
625 memcpy (&cbuf[off], reply->msg, msize);
628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmitting %u bytes to client %p\n",
629 msize, client->client_handle);
633 process_pending_messages (client);
635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmitted %u/%u bytes to client %p\n",
636 (unsigned int) off, (unsigned int) size, client->client_handle);
643 * Task run to check for messages that need to be sent to a client.
645 * @param client a ClientList, containing the client and any messages to be sent to it
648 process_pending_messages (struct ClientList *client)
650 if ((client->pending_head == NULL) || (client->transmit_handle != NULL))
653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
654 "Not asking for transmission to %p now: %s\n",
655 client->client_handle,
656 client->pending_head ==
657 NULL ? "no more messages" : "request already pending");
662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
663 "Asking for transmission of %u bytes to client %p\n",
664 ntohs (client->pending_head->msg->size), client->client_handle);
666 client->transmit_handle =
667 GNUNET_SERVER_notify_transmit_ready (client->client_handle,
668 ntohs (client->pending_head->
670 GNUNET_TIME_UNIT_FOREVER_REL,
671 &send_reply_to_client, client);
676 * Add a PendingMessage to the clients list of messages to be sent
678 * @param client the active client to send the message to
679 * @param pending_message the actual message to send
682 add_pending_message (struct ClientList *client,
683 struct PendingMessage *pending_message)
685 GNUNET_CONTAINER_DLL_insert_tail (client->pending_head, client->pending_tail,
687 process_pending_messages (client);
692 * Closure for 'forward_reply'
694 struct ForwardReplyContext
698 * Actual message to send to matching clients.
700 struct PendingMessage *pm;
710 enum GNUNET_BLOCK_Type type;
713 * Number of bytes in data.
718 * Do we need to copy 'pm' because it was already used?
726 * Iterator over hash map entries that send a given reply to
727 * each of the matching clients. With some tricky recycling
730 * @param cls the 'struct ForwardReplyContext'
731 * @param key current key
732 * @param value value in the hash map, a ClientQueryRecord
733 * @return GNUNET_YES (we should continue to iterate),
734 * if the result is mal-formed, GNUNET_NO
737 forward_reply (void *cls, const GNUNET_HashCode * key, void *value)
739 struct ForwardReplyContext *frc = cls;
740 struct ClientQueryRecord *record = value;
741 struct PendingMessage *pm;
742 struct GNUNET_DHT_ClientResultMessage *reply;
743 enum GNUNET_BLOCK_EvaluationResult eval;
748 if ((record->type != GNUNET_BLOCK_TYPE_ANY) && (record->type != frc->type))
751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
752 "Record type missmatch, not passing request for key %s to local client\n",
755 GNUNET_STATISTICS_update (GDS_stats,
757 ("# Key match, type mismatches in REPLY to CLIENT"),
759 return GNUNET_YES; /* type mismatch */
761 GNUNET_CRYPTO_hash (frc->data, frc->data_size, &ch);
762 for (i = 0; i < record->seen_replies_count; i++)
763 if (0 == memcmp (&record->seen_replies[i], &ch, sizeof (GNUNET_HashCode)))
766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
767 "Duplicate reply, not passing request for key %s to local client\n",
770 GNUNET_STATISTICS_update (GDS_stats,
772 ("# Duplicate REPLIES to CLIENT request dropped"),
774 return GNUNET_YES; /* duplicate */
777 GNUNET_BLOCK_evaluate (GDS_block_context, record->type, key, NULL, 0,
778 record->xquery, record->xquery_size, frc->data,
781 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
782 "Evaluation result is %d for key %s for local client's query\n",
783 (int) eval, GNUNET_h2s (key));
787 case GNUNET_BLOCK_EVALUATION_OK_LAST:
788 do_free = GNUNET_YES;
790 case GNUNET_BLOCK_EVALUATION_OK_MORE:
791 GNUNET_array_append (record->seen_replies, record->seen_replies_count, ch);
794 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
795 /* should be impossible to encounter here */
798 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
801 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
804 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
807 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
808 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
809 _("Unsupported block type (%u) in request!\n"), record->type);
815 if (GNUNET_NO == frc->do_copy)
817 /* first time, we can use the original data */
819 frc->do_copy = GNUNET_YES;
823 /* two clients waiting for same reply, must copy for queueing */
824 pm = GNUNET_malloc (sizeof (struct PendingMessage) +
825 ntohs (frc->pm->msg->size));
827 sizeof (struct PendingMessage) + ntohs (frc->pm->msg->size));
828 pm->next = pm->prev = NULL;
830 GNUNET_STATISTICS_update (GDS_stats,
831 gettext_noop ("# RESULTS queued for clients"), 1,
833 reply = (struct GNUNET_DHT_ClientResultMessage *) &pm[1];
834 reply->unique_id = record->unique_id;
836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
837 "Queueing reply to query %s for client %p\n", GNUNET_h2s (key),
838 record->client->client_handle);
840 add_pending_message (record->client, pm);
841 if (GNUNET_YES == do_free)
842 remove_client_records (record->client, key, record);
848 * Handle a reply we've received from another peer. If the reply
849 * matches any of our pending queries, forward it to the respective
852 * @param expiration when will the reply expire
853 * @param key the query this reply is for
854 * @param get_path_length number of peers in 'get_path'
855 * @param get_path path the reply took on get
856 * @param put_path_length number of peers in 'put_path'
857 * @param put_path path the reply took on put
858 * @param type type of the reply
859 * @param data_size number of bytes in 'data'
860 * @param data application payload data
863 GDS_CLIENTS_handle_reply (struct GNUNET_TIME_Absolute expiration,
864 const GNUNET_HashCode * key,
865 unsigned int get_path_length,
866 const struct GNUNET_PeerIdentity *get_path,
867 unsigned int put_path_length,
868 const struct GNUNET_PeerIdentity *put_path,
869 enum GNUNET_BLOCK_Type type, size_t data_size,
872 struct ForwardReplyContext frc;
873 struct PendingMessage *pm;
874 struct GNUNET_DHT_ClientResultMessage *reply;
875 struct GNUNET_PeerIdentity *paths;
878 if (NULL == GNUNET_CONTAINER_multihashmap_get (forward_map, key))
880 GNUNET_STATISTICS_update (GDS_stats,
882 ("# REPLIES ignored for CLIENTS (no match)"), 1,
884 return; /* no matching request, fast exit! */
887 sizeof (struct GNUNET_DHT_ClientResultMessage) + data_size +
888 (get_path_length + put_path_length) * sizeof (struct GNUNET_PeerIdentity);
889 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
891 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
892 _("Could not pass reply to client, message too big!\n"));
895 pm = (struct PendingMessage *) GNUNET_malloc (msize +
896 sizeof (struct PendingMessage));
897 reply = (struct GNUNET_DHT_ClientResultMessage *) &pm[1];
898 pm->msg = &reply->header;
899 reply->header.size = htons ((uint16_t) msize);
900 reply->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_CLIENT_RESULT);
901 reply->type = htonl (type);
902 reply->get_path_length = htonl (get_path_length);
903 reply->put_path_length = htonl (put_path_length);
904 reply->unique_id = 0; /* filled in later */
905 reply->expiration = GNUNET_TIME_absolute_hton (expiration);
907 paths = (struct GNUNET_PeerIdentity *) &reply[1];
908 memcpy (paths, put_path,
909 sizeof (struct GNUNET_PeerIdentity) * put_path_length);
910 memcpy (&paths[put_path_length], get_path,
911 sizeof (struct GNUNET_PeerIdentity) * get_path_length);
912 memcpy (&paths[get_path_length + put_path_length], data, data_size);
913 frc.do_copy = GNUNET_NO;
916 frc.data_size = data_size;
918 GNUNET_CONTAINER_multihashmap_get_multiple (forward_map, key, &forward_reply,
920 if (GNUNET_NO == frc.do_copy)
922 /* did not match any of the requests, free! */
923 GNUNET_STATISTICS_update (GDS_stats,
925 ("# REPLIES ignored for CLIENTS (no match)"), 1,
933 * Initialize client subsystem.
935 * @param server the initialized server
938 GDS_CLIENTS_init (struct GNUNET_SERVER_Handle *server)
940 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
941 {&handle_dht_local_put, NULL,
942 GNUNET_MESSAGE_TYPE_DHT_CLIENT_PUT, 0},
943 {&handle_dht_local_get, NULL,
944 GNUNET_MESSAGE_TYPE_DHT_CLIENT_GET, 0},
945 {&handle_dht_local_get_stop, NULL,
946 GNUNET_MESSAGE_TYPE_DHT_CLIENT_GET_STOP,
947 sizeof (struct GNUNET_DHT_ClientGetStopMessage)},
950 forward_map = GNUNET_CONTAINER_multihashmap_create (1024);
951 retry_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
952 GNUNET_SERVER_add_handlers (server, plugin_handlers);
953 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
958 * Shutdown client subsystem.
963 GNUNET_assert (client_head == NULL);
964 GNUNET_assert (client_tail == NULL);
965 if (GNUNET_SCHEDULER_NO_TASK != retry_task)
967 GNUNET_SCHEDULER_cancel (retry_task);
968 retry_task = GNUNET_SCHEDULER_NO_TASK;
970 GNUNET_assert (0 == GNUNET_CONTAINER_heap_get_size (retry_heap));
971 GNUNET_CONTAINER_heap_destroy (retry_heap);
973 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (forward_map));
974 GNUNET_CONTAINER_multihashmap_destroy (forward_map);
978 /* end of gnunet-service-dht_clients.c */