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_SERVER_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
114 struct GNUNET_HashCode key;
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 struct 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 struct GNUNET_HashCode *key;
207 * Flag whether to notify about GET messages.
212 * Flag whether to notify about GET_REPONSE messages.
217 * Flag whether to notify about PUT messages.
222 * Client to notify of these requests.
224 struct ClientList *client;
229 * List of active clients.
231 static struct ClientList *client_head;
234 * List of active clients.
236 static struct ClientList *client_tail;
239 * List of active monitoring requests.
241 static struct ClientMonitorRecord *monitor_head;
244 * List of active monitoring requests.
246 static struct ClientMonitorRecord *monitor_tail;
249 * Hashmap for fast key based lookup, maps keys to 'struct ClientQueryRecord' entries.
251 static struct GNUNET_CONTAINER_MultiHashMap *forward_map;
254 * Heap with all of our client's request, sorted by retry time (earliest on top).
256 static struct GNUNET_CONTAINER_Heap *retry_heap;
259 * Task that re-transmits requests (using retry_heap).
261 static GNUNET_SCHEDULER_TaskIdentifier retry_task;
265 * Task run to check for messages that need to be sent to a client.
267 * @param client a ClientList, containing the client and any messages to be sent to it
270 process_pending_messages (struct ClientList *client);
274 * Add a PendingMessage to the clients list of messages to be sent
276 * @param client the active client to send the message to
277 * @param pending_message the actual message to send
280 add_pending_message (struct ClientList *client,
281 struct PendingMessage *pending_message)
283 GNUNET_CONTAINER_DLL_insert_tail (client->pending_head, client->pending_tail,
285 process_pending_messages (client);
290 * Find a client if it exists, add it otherwise.
292 * @param client the server handle to the client
294 * @return the client if found, a new client otherwise
296 static struct ClientList *
297 find_active_client (struct GNUNET_SERVER_Client *client)
299 struct ClientList *pos = client_head;
300 struct ClientList *ret;
304 if (pos->client_handle == client)
308 ret = GNUNET_malloc (sizeof (struct ClientList));
309 ret->client_handle = client;
310 GNUNET_CONTAINER_DLL_insert (client_head, client_tail, ret);
316 * Iterator over hash map entries that frees all entries
317 * associated with the given client.
319 * @param cls client to search for in source routes
320 * @param key current key code (ignored)
321 * @param value value in the hash map, a ClientQueryRecord
322 * @return GNUNET_YES (we should continue to iterate)
325 remove_client_records (void *cls, const struct GNUNET_HashCode * key, void *value)
327 struct ClientList *client = cls;
328 struct ClientQueryRecord *record = value;
330 if (record->client != client)
332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
333 "Removing client %p's record for key %s\n", client,
335 GNUNET_assert (GNUNET_YES ==
336 GNUNET_CONTAINER_multihashmap_remove (forward_map, key,
338 if (NULL != record->hnode)
339 GNUNET_CONTAINER_heap_remove_node (record->hnode);
340 GNUNET_array_grow (record->seen_replies, record->seen_replies_count, 0);
341 GNUNET_free (record);
347 * Functions with this signature are called whenever a client
348 * is disconnected on the network level.
350 * @param cls closure (NULL for dht)
351 * @param client identification of the client; NULL
352 * for the last call when the server is destroyed
355 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
357 struct ClientList *pos;
358 struct PendingMessage *reply;
359 struct ClientMonitorRecord *monitor;
361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Local client %p disconnects\n", client);
362 pos = find_active_client (client);
363 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, pos);
364 if (pos->transmit_handle != NULL)
365 GNUNET_SERVER_notify_transmit_ready_cancel (pos->transmit_handle);
366 while (NULL != (reply = pos->pending_head))
368 GNUNET_CONTAINER_DLL_remove (pos->pending_head, pos->pending_tail, reply);
371 monitor = monitor_head;
372 while (NULL != monitor)
374 if (monitor->client == pos)
376 struct ClientMonitorRecord *next;
378 GNUNET_free_non_null (monitor->key);
379 next = monitor->next;
380 GNUNET_CONTAINER_DLL_remove (monitor_head, monitor_tail, monitor);
381 GNUNET_free (monitor);
385 monitor = monitor->next;
387 GNUNET_CONTAINER_multihashmap_iterate (forward_map, &remove_client_records,
394 * Route the given request via the DHT. This includes updating
395 * the bloom filter and retransmission times, building the P2P
396 * message and initiating the routing operation.
399 transmit_request (struct ClientQueryRecord *cqr)
401 int32_t reply_bf_mutator;
402 struct GNUNET_CONTAINER_BloomFilter *reply_bf;
403 struct GNUNET_CONTAINER_BloomFilter *peer_bf;
405 GNUNET_STATISTICS_update (GDS_stats,
407 ("# GET requests from clients injected"), 1,
410 (int32_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
413 GNUNET_BLOCK_construct_bloomfilter (reply_bf_mutator, cqr->seen_replies,
414 cqr->seen_replies_count);
416 GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE,
417 GNUNET_CONSTANTS_BLOOMFILTER_K);
418 GDS_NEIGHBOURS_handle_get (cqr->type, cqr->msg_options, cqr->replication,
420 &cqr->key, cqr->xquery, cqr->xquery_size, reply_bf,
421 reply_bf_mutator, peer_bf);
422 GNUNET_CONTAINER_bloomfilter_free (reply_bf);
423 GNUNET_CONTAINER_bloomfilter_free (peer_bf);
425 /* exponential back-off for retries, max 1h */
426 cqr->retry_frequency =
427 GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_HOURS,
428 GNUNET_TIME_relative_multiply
429 (cqr->retry_frequency, 2));
430 cqr->retry_time = GNUNET_TIME_relative_to_absolute (cqr->retry_frequency);
435 * Task that looks at the 'retry_heap' and transmits all of the requests
436 * on the heap that are ready for transmission. Then re-schedules
437 * itself (unless the heap is empty).
440 * @param tc scheduler context
443 transmit_next_request_task (void *cls,
444 const struct GNUNET_SCHEDULER_TaskContext *tc)
446 struct ClientQueryRecord *cqr;
447 struct GNUNET_TIME_Relative delay;
449 retry_task = GNUNET_SCHEDULER_NO_TASK;
450 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
452 while (NULL != (cqr = GNUNET_CONTAINER_heap_remove_root (retry_heap)))
455 delay = GNUNET_TIME_absolute_get_remaining (cqr->retry_time);
456 if (delay.rel_value > 0)
459 GNUNET_CONTAINER_heap_insert (retry_heap, cqr,
460 cqr->retry_time.abs_value);
462 GNUNET_SCHEDULER_add_delayed (delay, &transmit_next_request_task,
466 transmit_request (cqr);
468 GNUNET_CONTAINER_heap_insert (retry_heap, cqr,
469 cqr->retry_time.abs_value);
475 * Handler for PUT messages.
477 * @param cls closure for the service
478 * @param client the client we received this message from
479 * @param message the actual message received
482 handle_dht_local_put (void *cls, struct GNUNET_SERVER_Client *client,
483 const struct GNUNET_MessageHeader *message)
485 const struct GNUNET_DHT_ClientPutMessage *dht_msg;
486 struct GNUNET_CONTAINER_BloomFilter *peer_bf;
488 struct PendingMessage *pm;
489 struct GNUNET_DHT_ClientPutConfirmationMessage *conf;
491 size = ntohs (message->size);
492 if (size < sizeof (struct GNUNET_DHT_ClientPutMessage))
495 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
498 GNUNET_STATISTICS_update (GDS_stats,
500 ("# PUT requests received from clients"), 1,
502 dht_msg = (const struct GNUNET_DHT_ClientPutMessage *) message;
503 /* give to local clients */
504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
505 "Handling local PUT of %u-bytes for query %s\n",
506 size - sizeof (struct GNUNET_DHT_ClientPutMessage),
507 GNUNET_h2s (&dht_msg->key));
508 GDS_CLIENTS_handle_reply (GNUNET_TIME_absolute_ntoh (dht_msg->expiration),
509 &dht_msg->key, 0, NULL, 0, NULL,
510 ntohl (dht_msg->type),
511 size - sizeof (struct GNUNET_DHT_ClientPutMessage),
514 GDS_DATACACHE_handle_put (GNUNET_TIME_absolute_ntoh (dht_msg->expiration),
515 &dht_msg->key, 0, NULL, ntohl (dht_msg->type),
516 size - sizeof (struct GNUNET_DHT_ClientPutMessage),
518 /* route to other peers */
520 GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE,
521 GNUNET_CONSTANTS_BLOOMFILTER_K);
522 GDS_NEIGHBOURS_handle_put (ntohl (dht_msg->type), ntohl (dht_msg->options),
523 ntohl (dht_msg->desired_replication_level),
524 GNUNET_TIME_absolute_ntoh (dht_msg->expiration),
526 peer_bf, &dht_msg->key, 0, NULL, &dht_msg[1],
528 sizeof (struct GNUNET_DHT_ClientPutMessage));
529 GDS_CLIENTS_process_put (ntohl (dht_msg->options),
530 ntohl (dht_msg->type),
532 ntohl (dht_msg->desired_replication_level),
534 GDS_NEIGHBOURS_get_id(),
535 GNUNET_TIME_absolute_ntoh (dht_msg->expiration),
538 size - sizeof (struct GNUNET_DHT_ClientPutMessage));
539 GNUNET_CONTAINER_bloomfilter_free (peer_bf);
540 pm = GNUNET_malloc (sizeof (struct PendingMessage) +
541 sizeof (struct GNUNET_DHT_ClientPutConfirmationMessage));
542 conf = (struct GNUNET_DHT_ClientPutConfirmationMessage *) &pm[1];
543 conf->header.size = htons (sizeof (struct GNUNET_DHT_ClientPutConfirmationMessage));
544 conf->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_CLIENT_PUT_OK);
545 conf->reserved = htonl (0);
546 conf->unique_id = dht_msg->unique_id;
547 pm->msg = &conf->header;
548 add_pending_message (find_active_client (client), pm);
549 GNUNET_SERVER_receive_done (client, GNUNET_OK);
554 * Handler for any generic DHT messages, calls the appropriate handler
555 * depending on message type, sends confirmation if responses aren't otherwise
558 * @param cls closure for the service
559 * @param client the client we received this message from
560 * @param message the actual message received
563 handle_dht_local_get (void *cls, struct GNUNET_SERVER_Client *client,
564 const struct GNUNET_MessageHeader *message)
566 const struct GNUNET_DHT_ClientGetMessage *get;
567 struct ClientQueryRecord *cqr;
572 size = ntohs (message->size);
573 if (size < sizeof (struct GNUNET_DHT_ClientGetMessage))
576 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
579 xquery_size = size - sizeof (struct GNUNET_DHT_ClientGetMessage);
580 get = (const struct GNUNET_DHT_ClientGetMessage *) message;
581 xquery = (const char *) &get[1];
582 GNUNET_STATISTICS_update (GDS_stats,
584 ("# GET requests received from clients"), 1,
586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
587 "Received request for %s from local client %p\n",
588 GNUNET_h2s (&get->key), client);
589 cqr = GNUNET_malloc (sizeof (struct ClientQueryRecord) + xquery_size);
591 cqr->client = find_active_client (client);
592 cqr->xquery = (void *) &cqr[1];
593 memcpy (&cqr[1], xquery, xquery_size);
594 cqr->hnode = GNUNET_CONTAINER_heap_insert (retry_heap, cqr, 0);
595 cqr->retry_frequency = GNUNET_TIME_UNIT_MILLISECONDS;
596 cqr->retry_time = GNUNET_TIME_absolute_get ();
597 cqr->unique_id = get->unique_id;
598 cqr->xquery_size = xquery_size;
599 cqr->replication = ntohl (get->desired_replication_level);
600 cqr->msg_options = ntohl (get->options);
601 cqr->type = ntohl (get->type);
602 // FIXME use cqr->key, set multihashmap create to GNUNET_YES
603 GNUNET_CONTAINER_multihashmap_put (forward_map, &get->key, cqr,
604 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
605 GDS_CLIENTS_process_get (ntohl (get->options),
608 ntohl (get->desired_replication_level),
610 GDS_NEIGHBOURS_get_id(),
612 /* start remote requests */
613 if (GNUNET_SCHEDULER_NO_TASK != retry_task)
614 GNUNET_SCHEDULER_cancel (retry_task);
615 retry_task = GNUNET_SCHEDULER_add_now (&transmit_next_request_task, NULL);
616 /* perform local lookup */
617 GDS_DATACACHE_handle_get (&get->key, cqr->type, cqr->xquery, xquery_size,
619 GNUNET_SERVER_receive_done (client, GNUNET_OK);
624 * Closure for 'remove_by_unique_id'.
626 struct RemoveByUniqueIdContext
629 * Client that issued the removal request.
631 struct ClientList *client;
634 * Unique ID of the request.
641 * Iterator over hash map entries that frees all entries
642 * that match the given client and unique ID.
644 * @param cls unique ID and client to search for in source routes
645 * @param key current key code
646 * @param value value in the hash map, a ClientQueryRecord
647 * @return GNUNET_YES (we should continue to iterate)
650 remove_by_unique_id (void *cls, const struct GNUNET_HashCode * key, void *value)
652 const struct RemoveByUniqueIdContext *ctx = cls;
653 struct ClientQueryRecord *record = value;
655 if (record->unique_id != ctx->unique_id)
657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
658 "Removing client %p's record for key %s (by unique id)\n",
659 ctx->client->client_handle, GNUNET_h2s (key));
660 return remove_client_records (ctx->client, key, record);
665 * Handler for any generic DHT stop messages, calls the appropriate handler
666 * depending on message type (if processed locally)
668 * @param cls closure for the service
669 * @param client the client we received this message from
670 * @param message the actual message received
674 handle_dht_local_get_stop (void *cls, struct GNUNET_SERVER_Client *client,
675 const struct GNUNET_MessageHeader *message)
677 const struct GNUNET_DHT_ClientGetStopMessage *dht_stop_msg =
678 (const struct GNUNET_DHT_ClientGetStopMessage *) message;
679 struct RemoveByUniqueIdContext ctx;
681 GNUNET_STATISTICS_update (GDS_stats,
683 ("# GET STOP requests received from clients"), 1,
685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %p stopped request for key %s\n",
686 client, GNUNET_h2s (&dht_stop_msg->key));
687 ctx.client = find_active_client (client);
688 ctx.unique_id = dht_stop_msg->unique_id;
689 GNUNET_CONTAINER_multihashmap_get_multiple (forward_map, &dht_stop_msg->key,
690 &remove_by_unique_id, &ctx);
691 GNUNET_SERVER_receive_done (client, GNUNET_OK);
696 * Handler for monitor start messages
698 * @param cls closure for the service
699 * @param client the client we received this message from
700 * @param message the actual message received
704 handle_dht_local_monitor (void *cls, struct GNUNET_SERVER_Client *client,
705 const struct GNUNET_MessageHeader *message)
707 struct ClientMonitorRecord *r;
708 const struct GNUNET_DHT_MonitorStartStopMessage *msg;
710 msg = (struct GNUNET_DHT_MonitorStartStopMessage *) message;
711 r = GNUNET_malloc (sizeof(struct ClientMonitorRecord));
713 r->client = find_active_client(client);
714 r->type = ntohl(msg->type);
715 r->get = ntohs(msg->get);
716 r->get_resp = ntohs(msg->get_resp);
717 r->put = ntohs(msg->put);
718 if (0 == ntohs(msg->filter_key))
722 r->key = GNUNET_malloc (sizeof (struct GNUNET_HashCode));
723 memcpy (r->key, &msg->key, sizeof (struct GNUNET_HashCode));
725 GNUNET_CONTAINER_DLL_insert (monitor_head, monitor_tail, r);
726 GNUNET_SERVER_receive_done (client, GNUNET_OK);
730 * Handler for monitor stop messages
732 * @param cls closure for the service
733 * @param client the client we received this message from
734 * @param message the actual message received
738 handle_dht_local_monitor_stop (void *cls, struct GNUNET_SERVER_Client *client,
739 const struct GNUNET_MessageHeader *message)
741 struct ClientMonitorRecord *r;
742 const struct GNUNET_DHT_MonitorStartStopMessage *msg;
745 msg = (struct GNUNET_DHT_MonitorStartStopMessage *) message;
751 keys_match = (0 == ntohs(msg->filter_key));
754 keys_match = (0 != ntohs(msg->filter_key)
755 && !memcmp(r->key, &msg->key, sizeof(struct GNUNET_HashCode)));
757 if (find_active_client(client) == r->client
758 && ntohl(msg->type) == r->type
759 && r->get == msg->get
760 && r->get_resp == msg->get_resp
761 && r->put == msg->put
765 GNUNET_CONTAINER_DLL_remove (monitor_head, monitor_tail, r);
766 GNUNET_free_non_null (r->key);
768 GNUNET_SERVER_receive_done (client, GNUNET_OK);
769 return; /* Delete only ONE entry */
774 GNUNET_SERVER_receive_done (client, GNUNET_OK);
779 * Callback called as a result of issuing a GNUNET_SERVER_notify_transmit_ready
780 * request. A ClientList is passed as closure, take the head of the list
781 * and copy it into buf, which has the result of sending the message to the
784 * @param cls closure to this call
785 * @param size maximum number of bytes available to send
786 * @param buf where to copy the actual message to
788 * @return the number of bytes actually copied, 0 indicates failure
791 send_reply_to_client (void *cls, size_t size, void *buf)
793 struct ClientList *client = cls;
795 struct PendingMessage *reply;
799 client->transmit_handle = NULL;
802 /* client disconnected */
803 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
804 "Client %p disconnected, pending messages will be discarded\n",
805 client->client_handle);
809 while ((NULL != (reply = client->pending_head)) &&
810 (size >= off + (msize = ntohs (reply->msg->size))))
812 GNUNET_CONTAINER_DLL_remove (client->pending_head, client->pending_tail,
814 memcpy (&cbuf[off], reply->msg, msize);
816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmitting %u bytes to client %p\n",
817 msize, client->client_handle);
820 process_pending_messages (client);
821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmitted %u/%u bytes to client %p\n",
822 (unsigned int) off, (unsigned int) size, client->client_handle);
828 * Task run to check for messages that need to be sent to a client.
830 * @param client a ClientList, containing the client and any messages to be sent to it
833 process_pending_messages (struct ClientList *client)
835 if ((client->pending_head == NULL) || (client->transmit_handle != NULL))
837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
838 "Not asking for transmission to %p now: %s\n",
839 client->client_handle,
840 client->pending_head ==
841 NULL ? "no more messages" : "request already pending");
844 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
845 "Asking for transmission of %u bytes to client %p\n",
846 ntohs (client->pending_head->msg->size), client->client_handle);
847 client->transmit_handle =
848 GNUNET_SERVER_notify_transmit_ready (client->client_handle,
849 ntohs (client->pending_head->
851 GNUNET_TIME_UNIT_FOREVER_REL,
852 &send_reply_to_client, client);
857 * Closure for 'forward_reply'
859 struct ForwardReplyContext
863 * Actual message to send to matching clients.
865 struct PendingMessage *pm;
875 enum GNUNET_BLOCK_Type type;
878 * Number of bytes in data.
883 * Do we need to copy 'pm' because it was already used?
891 * Iterator over hash map entries that send a given reply to
892 * each of the matching clients. With some tricky recycling
895 * @param cls the 'struct ForwardReplyContext'
896 * @param key current key
897 * @param value value in the hash map, a ClientQueryRecord
898 * @return GNUNET_YES (we should continue to iterate),
899 * if the result is mal-formed, GNUNET_NO
902 forward_reply (void *cls, const struct GNUNET_HashCode * key, void *value)
904 struct ForwardReplyContext *frc = cls;
905 struct ClientQueryRecord *record = value;
906 struct PendingMessage *pm;
907 struct GNUNET_DHT_ClientResultMessage *reply;
908 enum GNUNET_BLOCK_EvaluationResult eval;
910 struct GNUNET_HashCode ch;
913 if ((record->type != GNUNET_BLOCK_TYPE_ANY) && (record->type != frc->type))
915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
916 "Record type missmatch, not passing request for key %s to local client\n",
918 GNUNET_STATISTICS_update (GDS_stats,
920 ("# Key match, type mismatches in REPLY to CLIENT"),
922 return GNUNET_YES; /* type mismatch */
924 GNUNET_CRYPTO_hash (frc->data, frc->data_size, &ch);
925 for (i = 0; i < record->seen_replies_count; i++)
926 if (0 == memcmp (&record->seen_replies[i], &ch, sizeof (struct GNUNET_HashCode)))
928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
929 "Duplicate reply, not passing request for key %s to local client\n",
931 GNUNET_STATISTICS_update (GDS_stats,
933 ("# Duplicate REPLIES to CLIENT request dropped"),
935 return GNUNET_YES; /* duplicate */
938 GNUNET_BLOCK_evaluate (GDS_block_context, record->type, key, NULL, 0,
939 record->xquery, record->xquery_size, frc->data,
941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
942 "Evaluation result is %d for key %s for local client's query\n",
943 (int) eval, GNUNET_h2s (key));
946 case GNUNET_BLOCK_EVALUATION_OK_LAST:
947 do_free = GNUNET_YES;
949 case GNUNET_BLOCK_EVALUATION_OK_MORE:
950 GNUNET_array_append (record->seen_replies, record->seen_replies_count, ch);
953 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
954 /* should be impossible to encounter here */
957 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
960 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
963 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
966 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
967 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
968 _("Unsupported block type (%u) in request!\n"), record->type);
974 if (GNUNET_NO == frc->do_copy)
976 /* first time, we can use the original data */
978 frc->do_copy = GNUNET_YES;
982 /* two clients waiting for same reply, must copy for queueing */
983 pm = GNUNET_malloc (sizeof (struct PendingMessage) +
984 ntohs (frc->pm->msg->size));
986 sizeof (struct PendingMessage) + ntohs (frc->pm->msg->size));
987 pm->next = pm->prev = NULL;
988 pm->msg = (struct GNUNET_MessageHeader *) &pm[1];
990 GNUNET_STATISTICS_update (GDS_stats,
991 gettext_noop ("# RESULTS queued for clients"), 1,
993 reply = (struct GNUNET_DHT_ClientResultMessage *) &pm[1];
994 reply->unique_id = record->unique_id;
995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
996 "Queueing reply to query %s for client %p\n", GNUNET_h2s (key),
997 record->client->client_handle);
998 add_pending_message (record->client, pm);
999 if (GNUNET_YES == do_free)
1000 remove_client_records (record->client, key, record);
1006 * Handle a reply we've received from another peer. If the reply
1007 * matches any of our pending queries, forward it to the respective
1010 * @param expiration when will the reply expire
1011 * @param key the query this reply is for
1012 * @param get_path_length number of peers in 'get_path'
1013 * @param get_path path the reply took on get
1014 * @param put_path_length number of peers in 'put_path'
1015 * @param put_path path the reply took on put
1016 * @param type type of the reply
1017 * @param data_size number of bytes in 'data'
1018 * @param data application payload data
1021 GDS_CLIENTS_handle_reply (struct GNUNET_TIME_Absolute expiration,
1022 const struct GNUNET_HashCode * key,
1023 unsigned int get_path_length,
1024 const struct GNUNET_PeerIdentity *get_path,
1025 unsigned int put_path_length,
1026 const struct GNUNET_PeerIdentity *put_path,
1027 enum GNUNET_BLOCK_Type type, size_t data_size,
1030 struct ForwardReplyContext frc;
1031 struct PendingMessage *pm;
1032 struct GNUNET_DHT_ClientResultMessage *reply;
1033 struct GNUNET_PeerIdentity *paths;
1036 if (NULL == GNUNET_CONTAINER_multihashmap_get (forward_map, key))
1038 GNUNET_STATISTICS_update (GDS_stats,
1040 ("# REPLIES ignored for CLIENTS (no match)"), 1,
1042 return; /* no matching request, fast exit! */
1045 sizeof (struct GNUNET_DHT_ClientResultMessage) + data_size +
1046 (get_path_length + put_path_length) * sizeof (struct GNUNET_PeerIdentity);
1047 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1049 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1050 _("Could not pass reply to client, message too big!\n"));
1053 pm = (struct PendingMessage *) GNUNET_malloc (msize +
1054 sizeof (struct PendingMessage));
1055 reply = (struct GNUNET_DHT_ClientResultMessage *) &pm[1];
1056 pm->msg = &reply->header;
1057 reply->header.size = htons ((uint16_t) msize);
1058 reply->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_CLIENT_RESULT);
1059 reply->type = htonl (type);
1060 reply->get_path_length = htonl (get_path_length);
1061 reply->put_path_length = htonl (put_path_length);
1062 reply->unique_id = 0; /* filled in later */
1063 reply->expiration = GNUNET_TIME_absolute_hton (expiration);
1065 paths = (struct GNUNET_PeerIdentity *) &reply[1];
1066 memcpy (paths, put_path,
1067 sizeof (struct GNUNET_PeerIdentity) * put_path_length);
1068 memcpy (&paths[put_path_length], get_path,
1069 sizeof (struct GNUNET_PeerIdentity) * get_path_length);
1070 memcpy (&paths[get_path_length + put_path_length], data, data_size);
1071 frc.do_copy = GNUNET_NO;
1074 frc.data_size = data_size;
1076 GNUNET_CONTAINER_multihashmap_get_multiple (forward_map, key, &forward_reply,
1078 if (GNUNET_NO == frc.do_copy)
1080 /* did not match any of the requests, free! */
1081 GNUNET_STATISTICS_update (GDS_stats,
1083 ("# REPLIES ignored for CLIENTS (no match)"), 1,
1091 * Check if some client is monitoring GET messages and notify
1092 * them in that case.
1094 * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
1095 * @param type The type of data in the request.
1096 * @param hop_count Hop count so far.
1097 * @param path_length number of entries in path (or 0 if not recorded).
1098 * @param path peers on the GET path (or NULL if not recorded).
1099 * @param desired_replication_level Desired replication level.
1100 * @param key Key of the requested data.
1103 GDS_CLIENTS_process_get (uint32_t options,
1104 enum GNUNET_BLOCK_Type type,
1106 uint32_t desired_replication_level,
1107 unsigned int path_length,
1108 const struct GNUNET_PeerIdentity *path,
1109 const struct GNUNET_HashCode * key)
1111 struct ClientMonitorRecord *m;
1112 struct ClientList **cl;
1113 unsigned int cl_size;
1117 for (m = monitor_head; NULL != m; m = m->next)
1119 if ((GNUNET_BLOCK_TYPE_ANY == m->type || m->type == type) &&
1121 memcmp (key, m->key, sizeof(struct GNUNET_HashCode)) == 0))
1123 struct PendingMessage *pm;
1124 struct GNUNET_DHT_MonitorGetMessage *mmsg;
1125 struct GNUNET_PeerIdentity *msg_path;
1129 /* Don't send duplicates */
1130 for (i = 0; i < cl_size; i++)
1131 if (cl[i] == m->client)
1135 GNUNET_array_append (cl, cl_size, m->client);
1137 msize = path_length * sizeof (struct GNUNET_PeerIdentity);
1138 msize += sizeof (struct GNUNET_DHT_MonitorGetMessage);
1139 msize += sizeof (struct PendingMessage);
1140 pm = (struct PendingMessage *) GNUNET_malloc (msize);
1141 mmsg = (struct GNUNET_DHT_MonitorGetMessage *) &pm[1];
1142 pm->msg = &mmsg->header;
1143 mmsg->header.size = htons (msize - sizeof (struct PendingMessage));
1144 mmsg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_MONITOR_GET);
1145 mmsg->options = htonl(options);
1146 mmsg->type = htonl(type);
1147 mmsg->hop_count = htonl(hop_count);
1148 mmsg->desired_replication_level = htonl(desired_replication_level);
1149 mmsg->get_path_length = htonl(path_length);
1150 memcpy (&mmsg->key, key, sizeof (struct GNUNET_HashCode));
1151 msg_path = (struct GNUNET_PeerIdentity *) &mmsg[1];
1152 if (path_length > 0)
1153 memcpy (msg_path, path,
1154 path_length * sizeof (struct GNUNET_PeerIdentity));
1155 add_pending_message (m->client, pm);
1158 GNUNET_free_non_null (cl);
1163 * Check if some client is monitoring GET RESP messages and notify
1164 * them in that case.
1166 * @param type The type of data in the result.
1167 * @param get_path Peers on GET path (or NULL if not recorded).
1168 * @param get_path_length number of entries in get_path.
1169 * @param put_path peers on the PUT path (or NULL if not recorded).
1170 * @param put_path_length number of entries in get_path.
1171 * @param exp Expiration time of the data.
1172 * @param key Key of the data.
1173 * @param data Pointer to the result data.
1174 * @param size Number of bytes in data.
1177 GDS_CLIENTS_process_get_resp (enum GNUNET_BLOCK_Type type,
1178 const struct GNUNET_PeerIdentity *get_path,
1179 unsigned int get_path_length,
1180 const struct GNUNET_PeerIdentity *put_path,
1181 unsigned int put_path_length,
1182 struct GNUNET_TIME_Absolute exp,
1183 const struct GNUNET_HashCode * key,
1187 struct ClientMonitorRecord *m;
1188 struct ClientList **cl;
1189 unsigned int cl_size;
1193 for (m = monitor_head; NULL != m; m = m->next)
1195 if ((GNUNET_BLOCK_TYPE_ANY == m->type || m->type == type) &&
1197 memcmp (key, m->key, sizeof(struct GNUNET_HashCode)) == 0))
1199 struct PendingMessage *pm;
1200 struct GNUNET_DHT_MonitorGetRespMessage *mmsg;
1201 struct GNUNET_PeerIdentity *path;
1205 /* Don't send duplicates */
1206 for (i = 0; i < cl_size; i++)
1207 if (cl[i] == m->client)
1211 GNUNET_array_append (cl, cl_size, m->client);
1214 msize += (get_path_length + put_path_length)
1215 * sizeof (struct GNUNET_PeerIdentity);
1216 msize += sizeof (struct GNUNET_DHT_MonitorGetRespMessage);
1217 msize += sizeof (struct PendingMessage);
1218 pm = (struct PendingMessage *) GNUNET_malloc (msize);
1219 mmsg = (struct GNUNET_DHT_MonitorGetRespMessage *) &pm[1];
1220 pm->msg = (struct GNUNET_MessageHeader *) mmsg;
1221 mmsg->header.size = htons (msize - sizeof (struct PendingMessage));
1222 mmsg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_MONITOR_GET_RESP);
1223 mmsg->type = htonl(type);
1224 mmsg->put_path_length = htonl(put_path_length);
1225 mmsg->get_path_length = htonl(get_path_length);
1226 path = (struct GNUNET_PeerIdentity *) &mmsg[1];
1227 if (put_path_length > 0)
1229 memcpy (path, put_path,
1230 put_path_length * sizeof (struct GNUNET_PeerIdentity));
1231 path = &path[put_path_length];
1233 if (get_path_length > 0)
1234 memcpy (path, get_path,
1235 get_path_length * sizeof (struct GNUNET_PeerIdentity));
1236 mmsg->expiration_time = GNUNET_TIME_absolute_hton(exp);
1237 memcpy (&mmsg->key, key, sizeof (struct GNUNET_HashCode));
1239 memcpy (&path[get_path_length], data, size);
1240 add_pending_message (m->client, pm);
1243 GNUNET_free_non_null (cl);
1248 * Check if some client is monitoring PUT messages and notify
1249 * them in that case.
1251 * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
1252 * @param type The type of data in the request.
1253 * @param hop_count Hop count so far.
1254 * @param path_length number of entries in path (or 0 if not recorded).
1255 * @param path peers on the PUT path (or NULL if not recorded).
1256 * @param desired_replication_level Desired replication level.
1257 * @param exp Expiration time of the data.
1258 * @param key Key under which data is to be stored.
1259 * @param data Pointer to the data carried.
1260 * @param size Number of bytes in data.
1263 GDS_CLIENTS_process_put (uint32_t options,
1264 enum GNUNET_BLOCK_Type type,
1266 uint32_t desired_replication_level,
1267 unsigned int path_length,
1268 const struct GNUNET_PeerIdentity *path,
1269 struct GNUNET_TIME_Absolute exp,
1270 const struct GNUNET_HashCode * key,
1274 struct ClientMonitorRecord *m;
1275 struct ClientList **cl;
1276 unsigned int cl_size;
1280 for (m = monitor_head; NULL != m; m = m->next)
1282 if ((GNUNET_BLOCK_TYPE_ANY == m->type || m->type == type) &&
1284 memcmp (key, m->key, sizeof(struct GNUNET_HashCode)) == 0))
1286 struct PendingMessage *pm;
1287 struct GNUNET_DHT_MonitorPutMessage *mmsg;
1288 struct GNUNET_PeerIdentity *msg_path;
1292 /* Don't send duplicates */
1293 for (i = 0; i < cl_size; i++)
1294 if (cl[i] == m->client)
1298 GNUNET_array_append (cl, cl_size, m->client);
1301 msize += path_length * sizeof (struct GNUNET_PeerIdentity);
1302 msize += sizeof (struct GNUNET_DHT_MonitorPutMessage);
1303 msize += sizeof (struct PendingMessage);
1304 pm = (struct PendingMessage *) GNUNET_malloc (msize);
1305 mmsg = (struct GNUNET_DHT_MonitorPutMessage *) &pm[1];
1306 pm->msg = (struct GNUNET_MessageHeader *) mmsg;
1307 mmsg->header.size = htons (msize - sizeof (struct PendingMessage));
1308 mmsg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_MONITOR_PUT);
1309 mmsg->options = htonl(options);
1310 mmsg->type = htonl(type);
1311 mmsg->hop_count = htonl(hop_count);
1312 mmsg->desired_replication_level = htonl(desired_replication_level);
1313 mmsg->put_path_length = htonl(path_length);
1314 msg_path = (struct GNUNET_PeerIdentity *) &mmsg[1];
1315 if (path_length > 0)
1317 memcpy (msg_path, path,
1318 path_length * sizeof (struct GNUNET_PeerIdentity));
1320 mmsg->expiration_time = GNUNET_TIME_absolute_hton(exp);
1321 memcpy (&mmsg->key, key, sizeof (struct GNUNET_HashCode));
1323 memcpy (&msg_path[path_length], data, size);
1324 add_pending_message (m->client, pm);
1327 GNUNET_free_non_null (cl);
1332 * Initialize client subsystem.
1334 * @param server the initialized server
1337 GDS_CLIENTS_init (struct GNUNET_SERVER_Handle *server)
1339 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
1340 {&handle_dht_local_put, NULL,
1341 GNUNET_MESSAGE_TYPE_DHT_CLIENT_PUT, 0},
1342 {&handle_dht_local_get, NULL,
1343 GNUNET_MESSAGE_TYPE_DHT_CLIENT_GET, 0},
1344 {&handle_dht_local_get_stop, NULL,
1345 GNUNET_MESSAGE_TYPE_DHT_CLIENT_GET_STOP,
1346 sizeof (struct GNUNET_DHT_ClientGetStopMessage)},
1347 {&handle_dht_local_monitor, NULL,
1348 GNUNET_MESSAGE_TYPE_DHT_MONITOR_START,
1349 sizeof (struct GNUNET_DHT_MonitorStartStopMessage)},
1350 {&handle_dht_local_monitor_stop, NULL,
1351 GNUNET_MESSAGE_TYPE_DHT_MONITOR_STOP,
1352 sizeof (struct GNUNET_DHT_MonitorStartStopMessage)},
1355 forward_map = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_NO);
1356 retry_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1357 GNUNET_SERVER_add_handlers (server, plugin_handlers);
1358 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
1363 * Shutdown client subsystem.
1368 GNUNET_assert (client_head == NULL);
1369 GNUNET_assert (client_tail == NULL);
1370 if (GNUNET_SCHEDULER_NO_TASK != retry_task)
1372 GNUNET_SCHEDULER_cancel (retry_task);
1373 retry_task = GNUNET_SCHEDULER_NO_TASK;
1375 if (NULL != retry_heap)
1377 GNUNET_assert (0 == GNUNET_CONTAINER_heap_get_size (retry_heap));
1378 GNUNET_CONTAINER_heap_destroy (retry_heap);
1381 if (NULL != forward_map)
1383 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (forward_map));
1384 GNUNET_CONTAINER_multihashmap_destroy (forward_map);
1389 /* end of gnunet-service-dht_clients.c */