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 * Struct containing paremeters of monitoring requests.
183 struct ClientMonitorRecord
187 * Next element in DLL.
189 struct ClientMonitorRecord *next;
192 * Previous element in DLL.
194 struct ClientMonitorRecord *prev;
197 * Type of blocks that are of interest
199 enum GNUNET_BLOCK_Type type;
202 * Key of data of interest, NULL for all.
204 GNUNET_HashCode *key;
207 * Client to notify of these requests.
209 struct ClientList *client;
214 * List of active clients.
216 static struct ClientList *client_head;
219 * List of active clients.
221 static struct ClientList *client_tail;
224 * List of active monitoring requests.
226 static struct ClientMonitorRecord *monitor_head;
229 * List of active monitoring requests.
231 static struct ClientMonitorRecord *monitor_tail;
234 * Hashmap for fast key based lookup, maps keys to 'struct ClientQueryRecord' entries.
236 static struct GNUNET_CONTAINER_MultiHashMap *forward_map;
239 * Heap with all of our client's request, sorted by retry time (earliest on top).
241 static struct GNUNET_CONTAINER_Heap *retry_heap;
244 * Task that re-transmits requests (using retry_heap).
246 static GNUNET_SCHEDULER_TaskIdentifier retry_task;
250 * Find a client if it exists, add it otherwise.
252 * @param client the server handle to the client
254 * @return the client if found, a new client otherwise
256 static struct ClientList *
257 find_active_client (struct GNUNET_SERVER_Client *client)
259 struct ClientList *pos = client_head;
260 struct ClientList *ret;
264 if (pos->client_handle == client)
268 ret = GNUNET_malloc (sizeof (struct ClientList));
269 ret->client_handle = client;
270 GNUNET_CONTAINER_DLL_insert (client_head, client_tail, ret);
276 * Iterator over hash map entries that frees all entries
277 * associated with the given client.
279 * @param cls client to search for in source routes
280 * @param key current key code (ignored)
281 * @param value value in the hash map, a ClientQueryRecord
282 * @return GNUNET_YES (we should continue to iterate)
285 remove_client_records (void *cls, const GNUNET_HashCode * key, void *value)
287 struct ClientList *client = cls;
288 struct ClientQueryRecord *record = value;
290 if (record->client != client)
293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
294 "Removing client %p's record for key %s\n", client,
297 GNUNET_assert (GNUNET_YES ==
298 GNUNET_CONTAINER_multihashmap_remove (forward_map, key,
300 if (NULL != record->hnode)
301 GNUNET_CONTAINER_heap_remove_node (record->hnode);
302 GNUNET_array_grow (record->seen_replies, record->seen_replies_count, 0);
303 GNUNET_free (record);
309 * Functions with this signature are called whenever a client
310 * is disconnected on the network level.
312 * @param cls closure (NULL for dht)
313 * @param client identification of the client; NULL
314 * for the last call when the server is destroyed
317 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
319 struct ClientList *pos;
320 struct PendingMessage *reply;
321 struct ClientMonitorRecord *monitor;
324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Local client %p disconnects\n", client);
326 pos = find_active_client (client);
327 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, pos);
328 if (pos->transmit_handle != NULL)
329 GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->transmit_handle);
330 while (NULL != (reply = pos->pending_head))
332 GNUNET_CONTAINER_DLL_remove (pos->pending_head, pos->pending_tail, reply);
335 monitor = monitor_head;
336 while (NULL != monitor)
338 if (monitor->client == pos)
340 struct ClientMonitorRecord *next;
342 GNUNET_free_non_null (monitor->key);
343 next = monitor->next;
344 GNUNET_CONTAINER_DLL_remove (monitor_head, monitor_tail, monitor);
345 GNUNET_free (monitor);
349 monitor = monitor->next;
351 GNUNET_CONTAINER_multihashmap_iterate (forward_map, &remove_client_records,
358 * Route the given request via the DHT. This includes updating
359 * the bloom filter and retransmission times, building the P2P
360 * message and initiating the routing operation.
363 transmit_request (struct ClientQueryRecord *cqr)
365 int32_t reply_bf_mutator;
366 struct GNUNET_CONTAINER_BloomFilter *reply_bf;
367 struct GNUNET_CONTAINER_BloomFilter *peer_bf;
369 GNUNET_STATISTICS_update (GDS_stats,
371 ("# GET requests from clients injected"), 1,
374 (int32_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
377 GNUNET_BLOCK_construct_bloomfilter (reply_bf_mutator, cqr->seen_replies,
378 cqr->seen_replies_count);
380 GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE,
381 GNUNET_CONSTANTS_BLOOMFILTER_K);
382 GDS_NEIGHBOURS_handle_get (cqr->type, cqr->msg_options, cqr->replication,
384 &cqr->key, cqr->xquery, cqr->xquery_size, reply_bf,
385 reply_bf_mutator, peer_bf);
386 GNUNET_CONTAINER_bloomfilter_free (reply_bf);
387 GNUNET_CONTAINER_bloomfilter_free (peer_bf);
389 /* exponential back-off for retries, max 1h */
390 cqr->retry_frequency =
391 GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_HOURS,
392 GNUNET_TIME_relative_multiply
393 (cqr->retry_frequency, 2));
394 cqr->retry_time = GNUNET_TIME_relative_to_absolute (cqr->retry_frequency);
399 * Task that looks at the 'retry_heap' and transmits all of the requests
400 * on the heap that are ready for transmission. Then re-schedules
401 * itself (unless the heap is empty).
404 * @param tc scheduler context
407 transmit_next_request_task (void *cls,
408 const struct GNUNET_SCHEDULER_TaskContext *tc)
410 struct ClientQueryRecord *cqr;
411 struct GNUNET_TIME_Relative delay;
413 retry_task = GNUNET_SCHEDULER_NO_TASK;
414 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
416 while (NULL != (cqr = GNUNET_CONTAINER_heap_remove_root (retry_heap)))
419 delay = GNUNET_TIME_absolute_get_remaining (cqr->retry_time);
420 if (delay.rel_value > 0)
423 GNUNET_CONTAINER_heap_insert (retry_heap, cqr,
424 cqr->retry_time.abs_value);
426 GNUNET_SCHEDULER_add_delayed (delay, &transmit_next_request_task,
430 transmit_request (cqr);
432 GNUNET_CONTAINER_heap_insert (retry_heap, cqr,
433 cqr->retry_time.abs_value);
439 * Handler for PUT messages.
441 * @param cls closure for the service
442 * @param client the client we received this message from
443 * @param message the actual message received
446 handle_dht_local_put (void *cls, struct GNUNET_SERVER_Client *client,
447 const struct GNUNET_MessageHeader *message)
449 const struct GNUNET_DHT_ClientPutMessage *dht_msg;
450 struct GNUNET_CONTAINER_BloomFilter *peer_bf;
453 size = ntohs (message->size);
454 if (size < sizeof (struct GNUNET_DHT_ClientPutMessage))
457 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
460 GNUNET_STATISTICS_update (GDS_stats,
462 ("# PUT requests received from clients"), 1,
464 dht_msg = (const struct GNUNET_DHT_ClientPutMessage *) message;
465 /* give to local clients */
467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
468 "Handling local PUT of %u-bytes for query %s\n",
469 size - sizeof (struct GNUNET_DHT_ClientPutMessage),
470 GNUNET_h2s (&dht_msg->key));
472 GDS_CLIENTS_handle_reply (GNUNET_TIME_absolute_ntoh (dht_msg->expiration),
473 &dht_msg->key, 0, NULL, 0, NULL,
474 ntohl (dht_msg->type),
475 size - sizeof (struct GNUNET_DHT_ClientPutMessage),
478 GDS_DATACACHE_handle_put (GNUNET_TIME_absolute_ntoh (dht_msg->expiration),
479 &dht_msg->key, 0, NULL, ntohl (dht_msg->type),
480 size - sizeof (struct GNUNET_DHT_ClientPutMessage),
482 /* route to other peers */
484 GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE,
485 GNUNET_CONSTANTS_BLOOMFILTER_K);
486 GDS_NEIGHBOURS_handle_put (ntohl (dht_msg->type), ntohl (dht_msg->options),
487 ntohl (dht_msg->desired_replication_level),
488 GNUNET_TIME_absolute_ntoh (dht_msg->expiration),
490 peer_bf, &dht_msg->key, 0, NULL, &dht_msg[1],
492 sizeof (struct GNUNET_DHT_ClientPutMessage));
493 GNUNET_CONTAINER_bloomfilter_free (peer_bf);
494 GNUNET_SERVER_receive_done (client, GNUNET_OK);
499 * Handler for any generic DHT messages, calls the appropriate handler
500 * depending on message type, sends confirmation if responses aren't otherwise
503 * @param cls closure for the service
504 * @param client the client we received this message from
505 * @param message the actual message received
508 handle_dht_local_get (void *cls, struct GNUNET_SERVER_Client *client,
509 const struct GNUNET_MessageHeader *message)
511 const struct GNUNET_DHT_ClientGetMessage *get;
512 struct ClientQueryRecord *cqr;
517 size = ntohs (message->size);
518 if (size < sizeof (struct GNUNET_DHT_ClientGetMessage))
521 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
524 xquery_size = size - sizeof (struct GNUNET_DHT_ClientGetMessage);
525 get = (const struct GNUNET_DHT_ClientGetMessage *) message;
526 xquery = (const char *) &get[1];
527 GNUNET_STATISTICS_update (GDS_stats,
529 ("# GET requests received from clients"), 1,
532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
533 "Received request for %s from local client %p\n",
534 GNUNET_h2s (&get->key), client);
536 cqr = GNUNET_malloc (sizeof (struct ClientQueryRecord) + xquery_size);
538 cqr->client = find_active_client (client);
539 cqr->xquery = (void *) &cqr[1];
540 memcpy (&cqr[1], xquery, xquery_size);
541 cqr->hnode = GNUNET_CONTAINER_heap_insert (retry_heap, cqr, 0);
542 cqr->retry_frequency = GNUNET_TIME_UNIT_MILLISECONDS;
543 cqr->retry_time = GNUNET_TIME_absolute_get ();
544 cqr->unique_id = get->unique_id;
545 cqr->xquery_size = xquery_size;
546 cqr->replication = ntohl (get->desired_replication_level);
547 cqr->msg_options = ntohl (get->options);
548 cqr->type = ntohl (get->type);
549 GNUNET_CONTAINER_multihashmap_put (forward_map, &get->key, cqr,
550 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
551 /* start remote requests */
552 if (GNUNET_SCHEDULER_NO_TASK != retry_task)
553 GNUNET_SCHEDULER_cancel (retry_task);
554 retry_task = GNUNET_SCHEDULER_add_now (&transmit_next_request_task, NULL);
555 /* perform local lookup */
556 GDS_DATACACHE_handle_get (&get->key, cqr->type, cqr->xquery, xquery_size,
558 GNUNET_SERVER_receive_done (client, GNUNET_OK);
563 * Closure for 'remove_by_unique_id'.
565 struct RemoveByUniqueIdContext
568 * Client that issued the removal request.
570 struct ClientList *client;
573 * Unique ID of the request.
580 * Iterator over hash map entries that frees all entries
581 * that match the given client and unique ID.
583 * @param cls unique ID and client to search for in source routes
584 * @param key current key code
585 * @param value value in the hash map, a ClientQueryRecord
586 * @return GNUNET_YES (we should continue to iterate)
589 remove_by_unique_id (void *cls, const GNUNET_HashCode * key, void *value)
591 const struct RemoveByUniqueIdContext *ctx = cls;
592 struct ClientQueryRecord *record = value;
594 if (record->unique_id != ctx->unique_id)
597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
598 "Removing client %p's record for key %s (by unique id)\n",
599 ctx->client->client_handle, GNUNET_h2s (key));
601 return remove_client_records (ctx->client, key, record);
606 * Handler for any generic DHT stop messages, calls the appropriate handler
607 * depending on message type (if processed locally)
609 * @param cls closure for the service
610 * @param client the client we received this message from
611 * @param message the actual message received
615 handle_dht_local_get_stop (void *cls, struct GNUNET_SERVER_Client *client,
616 const struct GNUNET_MessageHeader *message)
618 const struct GNUNET_DHT_ClientGetStopMessage *dht_stop_msg =
619 (const struct GNUNET_DHT_ClientGetStopMessage *) message;
620 struct RemoveByUniqueIdContext ctx;
622 GNUNET_STATISTICS_update (GDS_stats,
624 ("# GET STOP requests received from clients"), 1,
627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %p stopped request for key %s\n",
628 client, GNUNET_h2s (&dht_stop_msg->key));
630 ctx.client = find_active_client (client);
631 ctx.unique_id = dht_stop_msg->unique_id;
632 GNUNET_CONTAINER_multihashmap_get_multiple (forward_map, &dht_stop_msg->key,
633 &remove_by_unique_id, &ctx);
634 GNUNET_SERVER_receive_done (client, GNUNET_OK);
639 * Handler for monitor messages
641 * @param cls closure for the service
642 * @param client the client we received this message from
643 * @param message the actual message received
647 handle_dht_local_monitor (void *cls, struct GNUNET_SERVER_Client *client,
648 const struct GNUNET_MessageHeader *message)
650 struct ClientMonitorRecord *r;
651 const struct GNUNET_DHT_MonitorMessage *msg;
655 msg = (struct GNUNET_DHT_MonitorMessage *) message;
656 r = GNUNET_malloc (sizeof(struct ClientMonitorRecord));
658 r->client = find_active_client(client);
659 r->type = ntohl(msg->type);
660 c = (char *) &msg->key;
661 for (i = 0; i < sizeof (GNUNET_HashCode) && c[i] == 0; i++);
662 if (sizeof (GNUNET_HashCode) == i)
666 r->key = GNUNET_malloc (sizeof (GNUNET_HashCode));
667 memcpy (r->key, &msg->key, sizeof (GNUNET_HashCode));
669 GNUNET_CONTAINER_DLL_insert (monitor_head, monitor_tail, r);
670 // FIXME add remove somewhere
671 GNUNET_SERVER_receive_done (client, GNUNET_OK);
676 * Task run to check for messages that need to be sent to a client.
678 * @param client a ClientList, containing the client and any messages to be sent to it
681 process_pending_messages (struct ClientList *client);
685 * Callback called as a result of issuing a GNUNET_SERVER_notify_transmit_ready
686 * request. A ClientList is passed as closure, take the head of the list
687 * and copy it into buf, which has the result of sending the message to the
690 * @param cls closure to this call
691 * @param size maximum number of bytes available to send
692 * @param buf where to copy the actual message to
694 * @return the number of bytes actually copied, 0 indicates failure
697 send_reply_to_client (void *cls, size_t size, void *buf)
699 struct ClientList *client = cls;
701 struct PendingMessage *reply;
705 client->transmit_handle = NULL;
708 /* client disconnected */
710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
711 "Client %p disconnected, pending messages will be discarded\n",
712 client->client_handle);
717 while ((NULL != (reply = client->pending_head)) &&
718 (size >= off + (msize = ntohs (reply->msg->size))))
720 GNUNET_CONTAINER_DLL_remove (client->pending_head, client->pending_tail,
722 memcpy (&cbuf[off], reply->msg, msize);
725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmitting %u bytes to client %p\n",
726 msize, client->client_handle);
730 process_pending_messages (client);
732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmitted %u/%u bytes to client %p\n",
733 (unsigned int) off, (unsigned int) size, client->client_handle);
740 * Task run to check for messages that need to be sent to a client.
742 * @param client a ClientList, containing the client and any messages to be sent to it
745 process_pending_messages (struct ClientList *client)
747 if ((client->pending_head == NULL) || (client->transmit_handle != NULL))
750 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
751 "Not asking for transmission to %p now: %s\n",
752 client->client_handle,
753 client->pending_head ==
754 NULL ? "no more messages" : "request already pending");
759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
760 "Asking for transmission of %u bytes to client %p\n",
761 ntohs (client->pending_head->msg->size), client->client_handle);
763 client->transmit_handle =
764 GNUNET_SERVER_notify_transmit_ready (client->client_handle,
765 ntohs (client->pending_head->
767 GNUNET_TIME_UNIT_FOREVER_REL,
768 &send_reply_to_client, client);
773 * Add a PendingMessage to the clients list of messages to be sent
775 * @param client the active client to send the message to
776 * @param pending_message the actual message to send
779 add_pending_message (struct ClientList *client,
780 struct PendingMessage *pending_message)
782 GNUNET_CONTAINER_DLL_insert_tail (client->pending_head, client->pending_tail,
784 process_pending_messages (client);
789 * Closure for 'forward_reply'
791 struct ForwardReplyContext
795 * Actual message to send to matching clients.
797 struct PendingMessage *pm;
807 enum GNUNET_BLOCK_Type type;
810 * Number of bytes in data.
815 * Do we need to copy 'pm' because it was already used?
823 * Iterator over hash map entries that send a given reply to
824 * each of the matching clients. With some tricky recycling
827 * @param cls the 'struct ForwardReplyContext'
828 * @param key current key
829 * @param value value in the hash map, a ClientQueryRecord
830 * @return GNUNET_YES (we should continue to iterate),
831 * if the result is mal-formed, GNUNET_NO
834 forward_reply (void *cls, const GNUNET_HashCode * key, void *value)
836 struct ForwardReplyContext *frc = cls;
837 struct ClientQueryRecord *record = value;
838 struct PendingMessage *pm;
839 struct GNUNET_DHT_ClientResultMessage *reply;
840 enum GNUNET_BLOCK_EvaluationResult eval;
845 if ((record->type != GNUNET_BLOCK_TYPE_ANY) && (record->type != frc->type))
848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
849 "Record type missmatch, not passing request for key %s to local client\n",
852 GNUNET_STATISTICS_update (GDS_stats,
854 ("# Key match, type mismatches in REPLY to CLIENT"),
856 return GNUNET_YES; /* type mismatch */
858 GNUNET_CRYPTO_hash (frc->data, frc->data_size, &ch);
859 for (i = 0; i < record->seen_replies_count; i++)
860 if (0 == memcmp (&record->seen_replies[i], &ch, sizeof (GNUNET_HashCode)))
863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
864 "Duplicate reply, not passing request for key %s to local client\n",
867 GNUNET_STATISTICS_update (GDS_stats,
869 ("# Duplicate REPLIES to CLIENT request dropped"),
871 return GNUNET_YES; /* duplicate */
874 GNUNET_BLOCK_evaluate (GDS_block_context, record->type, key, NULL, 0,
875 record->xquery, record->xquery_size, frc->data,
878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
879 "Evaluation result is %d for key %s for local client's query\n",
880 (int) eval, GNUNET_h2s (key));
884 case GNUNET_BLOCK_EVALUATION_OK_LAST:
885 do_free = GNUNET_YES;
887 case GNUNET_BLOCK_EVALUATION_OK_MORE:
888 GNUNET_array_append (record->seen_replies, record->seen_replies_count, ch);
891 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
892 /* should be impossible to encounter here */
895 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
898 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
901 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
904 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
905 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
906 _("Unsupported block type (%u) in request!\n"), record->type);
912 if (GNUNET_NO == frc->do_copy)
914 /* first time, we can use the original data */
916 frc->do_copy = GNUNET_YES;
920 /* two clients waiting for same reply, must copy for queueing */
921 pm = GNUNET_malloc (sizeof (struct PendingMessage) +
922 ntohs (frc->pm->msg->size));
924 sizeof (struct PendingMessage) + ntohs (frc->pm->msg->size));
925 pm->next = pm->prev = NULL;
927 GNUNET_STATISTICS_update (GDS_stats,
928 gettext_noop ("# RESULTS queued for clients"), 1,
930 reply = (struct GNUNET_DHT_ClientResultMessage *) &pm[1];
931 reply->unique_id = record->unique_id;
933 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
934 "Queueing reply to query %s for client %p\n", GNUNET_h2s (key),
935 record->client->client_handle);
937 add_pending_message (record->client, pm);
938 if (GNUNET_YES == do_free)
939 remove_client_records (record->client, key, record);
945 * Handle a reply we've received from another peer. If the reply
946 * matches any of our pending queries, forward it to the respective
949 * @param expiration when will the reply expire
950 * @param key the query this reply is for
951 * @param get_path_length number of peers in 'get_path'
952 * @param get_path path the reply took on get
953 * @param put_path_length number of peers in 'put_path'
954 * @param put_path path the reply took on put
955 * @param type type of the reply
956 * @param data_size number of bytes in 'data'
957 * @param data application payload data
960 GDS_CLIENTS_handle_reply (struct GNUNET_TIME_Absolute expiration,
961 const GNUNET_HashCode * key,
962 unsigned int get_path_length,
963 const struct GNUNET_PeerIdentity *get_path,
964 unsigned int put_path_length,
965 const struct GNUNET_PeerIdentity *put_path,
966 enum GNUNET_BLOCK_Type type, size_t data_size,
969 struct ForwardReplyContext frc;
970 struct PendingMessage *pm;
971 struct GNUNET_DHT_ClientResultMessage *reply;
972 struct GNUNET_PeerIdentity *paths;
975 if (NULL == GNUNET_CONTAINER_multihashmap_get (forward_map, key))
977 GNUNET_STATISTICS_update (GDS_stats,
979 ("# REPLIES ignored for CLIENTS (no match)"), 1,
981 return; /* no matching request, fast exit! */
984 sizeof (struct GNUNET_DHT_ClientResultMessage) + data_size +
985 (get_path_length + put_path_length) * sizeof (struct GNUNET_PeerIdentity);
986 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
988 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
989 _("Could not pass reply to client, message too big!\n"));
992 pm = (struct PendingMessage *) GNUNET_malloc (msize +
993 sizeof (struct PendingMessage));
994 reply = (struct GNUNET_DHT_ClientResultMessage *) &pm[1];
995 pm->msg = &reply->header;
996 reply->header.size = htons ((uint16_t) msize);
997 reply->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_CLIENT_RESULT);
998 reply->type = htonl (type);
999 reply->get_path_length = htonl (get_path_length);
1000 reply->put_path_length = htonl (put_path_length);
1001 reply->unique_id = 0; /* filled in later */
1002 reply->expiration = GNUNET_TIME_absolute_hton (expiration);
1004 paths = (struct GNUNET_PeerIdentity *) &reply[1];
1005 memcpy (paths, put_path,
1006 sizeof (struct GNUNET_PeerIdentity) * put_path_length);
1007 memcpy (&paths[put_path_length], get_path,
1008 sizeof (struct GNUNET_PeerIdentity) * get_path_length);
1009 memcpy (&paths[get_path_length + put_path_length], data, data_size);
1010 frc.do_copy = GNUNET_NO;
1013 frc.data_size = data_size;
1015 GNUNET_CONTAINER_multihashmap_get_multiple (forward_map, key, &forward_reply,
1017 if (GNUNET_NO == frc.do_copy)
1019 /* did not match any of the requests, free! */
1020 GNUNET_STATISTICS_update (GDS_stats,
1022 ("# REPLIES ignored for CLIENTS (no match)"), 1,
1030 * Check if some client is monitoring messages of this type and notify
1033 * @param mtype Type of the DHT message.
1034 * @param exp When will this value expire.
1035 * @param key Key of the result/request.
1036 * @param putl number of entries in get_path.
1037 * @param put_path peers on the PUT path (or NULL if not recorded).
1038 * @param getl number of entries in get_path.
1039 * @param get_path Peers on reply path (or NULL if not recorded).
1040 * @param desired_replication_level Desired replication level.
1041 * @param type Type of the result/request.
1042 * @param data Pointer to the result data.
1043 * @param size Number of bytes in data.
1046 GDS_CLIENTS_process_monitor (uint16_t mtype,
1047 const struct GNUNET_TIME_Absolute exp,
1048 const GNUNET_HashCode *key,
1050 const struct GNUNET_PeerIdentity *put_path,
1052 const struct GNUNET_PeerIdentity *get_path,
1053 uint32_t desired_replication_level,
1054 enum GNUNET_BLOCK_Type type,
1055 const struct GNUNET_MessageHeader *data,
1058 struct ClientMonitorRecord *m;
1059 struct ClientList **cl;
1060 unsigned int cl_size;
1064 for (m = monitor_head; NULL != m; m = m->next)
1066 if ((GNUNET_BLOCK_TYPE_ANY == m->type || m->type == type) &&
1068 memcmp (key, m->key, sizeof(GNUNET_HashCode)) == 0))
1070 struct PendingMessage *pm;
1071 struct GNUNET_DHT_MonitorMessage *mmsg;
1072 struct GNUNET_PeerIdentity *path;
1076 /* Don't send duplicates */
1077 for (i = 0; i < cl_size; i++)
1078 if (cl[i] == m->client)
1082 GNUNET_array_append (cl, cl_size, m->client);
1085 msize += (getl + putl) * sizeof (struct GNUNET_PeerIdentity);
1086 msize += sizeof (struct GNUNET_DHT_MonitorMessage);
1087 msize += sizeof (struct PendingMessage);
1088 pm = (struct PendingMessage *) GNUNET_malloc (msize);
1089 mmsg = (struct GNUNET_DHT_MonitorMessage *) &pm[1];
1090 pm->msg = (struct GNUNET_MessageHeader *) mmsg;
1091 mmsg->header.size = htons (msize - sizeof (struct PendingMessage));
1092 mmsg->header.type = htons (mtype);
1093 mmsg->expiration = GNUNET_TIME_absolute_hton(exp);
1094 memcpy (&mmsg->key, key, sizeof (GNUNET_HashCode));
1095 mmsg->put_path_length = htonl(putl);
1096 mmsg->get_path_length = htonl(getl);
1097 mmsg->desired_replication_level = htonl (desired_replication_level);
1098 path = (struct GNUNET_PeerIdentity *) &mmsg[1];
1101 memcpy (path, put_path, putl * sizeof (struct GNUNET_PeerIdentity));
1105 memcpy (path, get_path, getl * sizeof (struct GNUNET_PeerIdentity));
1107 memcpy (&path[getl], data, size);
1108 add_pending_message (m->client, pm);
1111 GNUNET_free_non_null (cl);
1116 * Initialize client subsystem.
1118 * @param server the initialized server
1121 GDS_CLIENTS_init (struct GNUNET_SERVER_Handle *server)
1123 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
1124 {&handle_dht_local_put, NULL,
1125 GNUNET_MESSAGE_TYPE_DHT_CLIENT_PUT, 0},
1126 {&handle_dht_local_get, NULL,
1127 GNUNET_MESSAGE_TYPE_DHT_CLIENT_GET, 0},
1128 {&handle_dht_local_get_stop, NULL,
1129 GNUNET_MESSAGE_TYPE_DHT_CLIENT_GET_STOP,
1130 sizeof (struct GNUNET_DHT_ClientGetStopMessage)},
1131 {&handle_dht_local_monitor, NULL,
1132 GNUNET_MESSAGE_TYPE_DHT_MONITOR_GET,
1133 sizeof (struct GNUNET_DHT_MonitorMessage)},
1136 forward_map = GNUNET_CONTAINER_multihashmap_create (1024);
1137 retry_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1138 GNUNET_SERVER_add_handlers (server, plugin_handlers);
1139 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
1144 * Shutdown client subsystem.
1149 GNUNET_assert (client_head == NULL);
1150 GNUNET_assert (client_tail == NULL);
1151 if (GNUNET_SCHEDULER_NO_TASK != retry_task)
1153 GNUNET_SCHEDULER_cancel (retry_task);
1154 retry_task = GNUNET_SCHEDULER_NO_TASK;
1156 GNUNET_assert (0 == GNUNET_CONTAINER_heap_get_size (retry_heap));
1157 GNUNET_CONTAINER_heap_destroy (retry_heap);
1159 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (forward_map));
1160 GNUNET_CONTAINER_multihashmap_destroy (forward_map);
1164 /* end of gnunet-service-dht_clients.c */