2 This file is part of GNUnet.
3 (C) 2009, 2010 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 2, 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 fs/gnunet-service-fs.c
23 * @brief gnunet anonymity protocol implementation
24 * @author Christian Grothoff
27 * - TTL/priority calculations are absent!
29 * - have non-zero preference / priority for requests we initiate!
30 * - implement hot-path routing decision procedure
31 * - implement: bound_priority, test_load_too_high
36 #include "gnunet_constants.h"
37 #include "gnunet_core_service.h"
38 #include "gnunet_datastore_service.h"
39 #include "gnunet_peer_lib.h"
40 #include "gnunet_protocols.h"
41 #include "gnunet_signatures.h"
42 #include "gnunet_statistics_service.h"
43 #include "gnunet_util_lib.h"
44 #include "gnunet-service-fs_indexing.h"
47 #define DEBUG_FS GNUNET_NO
50 * Maximum number of outgoing messages we queue per peer.
51 * FIXME: make configurable?
53 #define MAX_QUEUE_PER_PEER 16
56 * Inverse of the probability that we will submit the same query
57 * to the same peer again. If the same peer already got the query
58 * repeatedly recently, the probability is multiplied by the inverse
59 * of this number each time. Note that we only try about every TTL_DECREMENT/2
60 * plus MAX_CORK_DELAY (so roughly every 3.5s).
62 #define RETRY_PROBABILITY_INV 3
65 * What is the maximum delay for a P2P FS message (in our interaction
66 * with core)? FS-internal delays are another story. The value is
67 * chosen based on the 32k block size. Given that peers typcially
68 * have at least 1 kb/s bandwidth, 45s waits give us a chance to
69 * transmit one message even to the lowest-bandwidth peers.
71 #define MAX_TRANSMIT_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 45)
76 * Maximum number of requests (from other peers) that we're
77 * willing to have pending at any given point in time.
78 * FIXME: set from configuration.
80 static uint64_t max_pending_requests = (32 * 1024);
84 * Information we keep for each pending reply. The
85 * actual message follows at the end of this struct.
87 struct PendingMessage;
90 * Our connection to the datastore.
92 static struct GNUNET_DATASTORE_Handle *dsh;
96 * Function called upon completion of a transmission.
99 * @param pid ID of receiving peer, 0 on transmission error
101 typedef void (*TransmissionContinuation)(void * cls,
102 GNUNET_PEER_Id tpid);
106 * Information we keep for each pending message (GET/PUT). The
107 * actual message follows at the end of this struct.
109 struct PendingMessage
112 * This is a doubly-linked list of messages to the same peer.
114 struct PendingMessage *next;
117 * This is a doubly-linked list of messages to the same peer.
119 struct PendingMessage *prev;
122 * Entry in pending message list for this pending message.
124 struct PendingMessageList *pml;
127 * Function to call immediately once we have transmitted this
130 TransmissionContinuation cont;
138 * Size of the reply; actual reply message follows
139 * at the end of this struct.
144 * How important is this message for us?
152 * Information about a peer that we are connected to.
153 * We track data that is useful for determining which
154 * peers should receive our requests. We also keep
155 * a list of messages to transmit to this peer.
161 * List of the last clients for which this peer successfully
164 struct GNUNET_SERVER_Client *last_client_replies[CS2P_SUCCESS_LIST_SIZE];
167 * List of the last PIDs for which
168 * this peer successfully answered a query;
169 * We use 0 to indicate no successful reply.
171 GNUNET_PEER_Id last_p2p_replies[P2P_SUCCESS_LIST_SIZE];
174 * Average delay between sending the peer a request and
175 * getting a reply (only calculated over the requests for
176 * which we actually got a reply). Calculated
177 * as a moving average: new_delay = ((n-1)*last_delay+curr_delay) / n
179 struct GNUNET_TIME_Relative avg_delay;
182 * Handle for an active request for transmission to this
185 struct GNUNET_CORE_TransmitHandle *cth;
188 * Messages (replies, queries, content migration) we would like to
189 * send to this peer in the near future. Sorted by priority, head.
191 struct PendingMessage *pending_messages_head;
194 * Messages (replies, queries, content migration) we would like to
195 * send to this peer in the near future. Sorted by priority, tail.
197 struct PendingMessage *pending_messages_tail;
200 * Average priority of successful replies. Calculated
201 * as a moving average: new_avg = ((n-1)*last_avg+curr_prio) / n
206 * Increase in traffic preference still to be submitted
207 * to the core service for this peer. FIXME: double or 'uint64_t'?
209 double inc_preference;
212 * The peer's identity.
217 * Size of the linked list of 'pending_messages'.
219 unsigned int pending_requests;
222 * Which offset in "last_p2p_replies" will be updated next?
223 * (we go round-robin).
225 unsigned int last_p2p_replies_woff;
228 * Which offset in "last_client_replies" will be updated next?
229 * (we go round-robin).
231 unsigned int last_client_replies_woff;
237 * Information we keep for each pending request. We should try to
238 * keep this struct as small as possible since its memory consumption
239 * is key to how many requests we can have pending at once.
241 struct PendingRequest;
245 * Doubly-linked list of requests we are performing
246 * on behalf of the same client.
248 struct ClientRequestList
252 * This is a doubly-linked list.
254 struct ClientRequestList *next;
257 * This is a doubly-linked list.
259 struct ClientRequestList *prev;
262 * Request this entry represents.
264 struct PendingRequest *req;
267 * Client list this request belongs to.
269 struct ClientList *client_list;
275 * Replies to be transmitted to the client. The actual
276 * response message is allocated after this struct.
278 struct ClientResponseMessage
281 * This is a doubly-linked list.
283 struct ClientResponseMessage *next;
286 * This is a doubly-linked list.
288 struct ClientResponseMessage *prev;
291 * Client list entry this response belongs to.
293 struct ClientList *client_list;
296 * Number of bytes in the response.
303 * Linked list of clients we are performing requests
309 * This is a linked list.
311 struct ClientList *next;
314 * ID of a client making a request, NULL if this entry is for a
317 struct GNUNET_SERVER_Client *client;
320 * Head of list of requests performed on behalf
321 * of this client right now.
323 struct ClientRequestList *rl_head;
326 * Tail of list of requests performed on behalf
327 * of this client right now.
329 struct ClientRequestList *rl_tail;
332 * Head of linked list of responses.
334 struct ClientResponseMessage *res_head;
337 * Tail of linked list of responses.
339 struct ClientResponseMessage *res_tail;
342 * Context for sending replies.
344 struct GNUNET_CONNECTION_TransmitHandle *th;
350 * Doubly-linked list of messages we are performing
351 * due to a pending request.
353 struct PendingMessageList
357 * This is a doubly-linked list of messages on behalf of the same request.
359 struct PendingMessageList *next;
362 * This is a doubly-linked list of messages on behalf of the same request.
364 struct PendingMessageList *prev;
367 * Message this entry represents.
369 struct PendingMessage *pm;
372 * Request this entry belongs to.
374 struct PendingRequest *req;
377 * Peer this message is targeted for.
379 struct ConnectedPeer *target;
385 * Information we keep for each pending request. We should try to
386 * keep this struct as small as possible since its memory consumption
387 * is key to how many requests we can have pending at once.
389 struct PendingRequest
393 * If this request was made by a client, this is our entry in the
394 * client request list; otherwise NULL.
396 struct ClientRequestList *client_request_list;
399 * Entry of peer responsible for this entry (if this request
400 * was made by a peer).
402 struct ConnectedPeer *cp;
405 * If this is a namespace query, pointer to the hash of the public
406 * key of the namespace; otherwise NULL. Pointer will be to the
407 * end of this struct (so no need to free it).
409 const GNUNET_HashCode *namespace;
412 * Bloomfilter we use to filter out replies that we don't care about
413 * (anymore). NULL as long as we are interested in all replies.
415 struct GNUNET_CONTAINER_BloomFilter *bf;
418 * Context of our GNUNET_CORE_peer_change_preference call.
420 struct GNUNET_CORE_InformationRequestContext *irc;
423 * Hash code of all replies that we have seen so far (only valid
424 * if client is not NULL since we only track replies like this for
427 GNUNET_HashCode *replies_seen;
430 * Node in the heap representing this entry; NULL
431 * if we have no heap node.
433 struct GNUNET_CONTAINER_HeapNode *hnode;
436 * Head of list of messages being performed on behalf of this
439 struct PendingMessageList *pending_head;
442 * Tail of list of messages being performed on behalf of this
445 struct PendingMessageList *pending_tail;
448 * When did we first see this request (form this peer), or, if our
449 * client is initiating, when did we last initiate a search?
451 struct GNUNET_TIME_Absolute start_time;
454 * The query that this request is for.
456 GNUNET_HashCode query;
459 * The task responsible for transmitting queries
462 GNUNET_SCHEDULER_TaskIdentifier task;
465 * (Interned) Peer identifier that identifies a preferred target
468 GNUNET_PEER_Id target_pid;
471 * (Interned) Peer identifiers of peers that have already
472 * received our query for this content.
474 GNUNET_PEER_Id *used_pids;
477 * Our entry in the queue (non-NULL while we wait for our
478 * turn to interact with the local database).
480 struct GNUNET_DATASTORE_QueueEntry *qe;
484 * Size of the 'bf' (in bytes).
489 * Desired anonymity level; only valid for requests from a local client.
491 uint32_t anonymity_level;
494 * How many entries in "used_pids" are actually valid?
496 unsigned int used_pids_off;
499 * How long is the "used_pids" array?
501 unsigned int used_pids_size;
504 * Number of results found for this request.
506 unsigned int results_found;
509 * How many entries in "replies_seen" are actually valid?
511 unsigned int replies_seen_off;
514 * How long is the "replies_seen" array?
516 unsigned int replies_seen_size;
519 * Priority with which this request was made. If one of our clients
520 * made the request, then this is the current priority that we are
521 * using when initiating the request. This value is used when
522 * we decide to reward other peers with trust for providing a reply.
527 * Priority points left for us to spend when forwarding this request
530 uint32_t remaining_priority;
533 * Number to mingle hashes for bloom-filter tests with.
538 * TTL with which we saw this request (or, if we initiated, TTL that
539 * we used for the request).
544 * Type of the content that this request is for.
546 enum GNUNET_BLOCK_Type type;
549 * Remove this request after transmission of the current response.
554 * GNUNET_YES if we should not forward this request to other peers.
562 * Block that is ready for migration to other peers. Actual data is at the end of the block.
564 struct MigrationReadyBlock
568 * This is a doubly-linked list.
570 struct MigrationReadyBlock *next;
573 * This is a doubly-linked list.
575 struct MigrationReadyBlock *prev;
578 * Query for the block.
580 GNUNET_HashCode query;
583 * When does this block expire?
585 struct GNUNET_TIME_Absolute expiration;
588 * Peers we would consider forwarding this
589 * block to. Zero for empty entries.
591 GNUNET_PEER_Id target_list[MIGRATION_LIST_SIZE];
599 * Number of targets already used.
601 unsigned int used_targets;
606 enum GNUNET_BLOCK_Type type;
613 static struct GNUNET_SCHEDULER_Handle *sched;
618 static const struct GNUNET_CONFIGURATION_Handle *cfg;
621 * Map of peer identifiers to "struct ConnectedPeer" (for that peer).
623 static struct GNUNET_CONTAINER_MultiHashMap *connected_peers;
626 * Map of peer identifiers to "struct PendingRequest" (for that peer).
628 static struct GNUNET_CONTAINER_MultiHashMap *peer_request_map;
631 * Map of query identifiers to "struct PendingRequest" (for that query).
633 static struct GNUNET_CONTAINER_MultiHashMap *query_request_map;
636 * Heap with the request that will expire next at the top. Contains
637 * pointers of type "struct PendingRequest*"; these will *also* be
638 * aliased from the "requests_by_peer" data structures and the
639 * "requests_by_query" table. Note that requests from our clients
640 * don't expire and are thus NOT in the "requests_by_expiration"
641 * (or the "requests_by_peer" tables).
643 static struct GNUNET_CONTAINER_Heap *requests_by_expiration_heap;
646 * Handle for reporting statistics.
648 static struct GNUNET_STATISTICS_Handle *stats;
651 * Linked list of clients we are currently processing requests for.
653 static struct ClientList *client_list;
656 * Pointer to handle to the core service (points to NULL until we've
659 static struct GNUNET_CORE_Handle *core;
662 * Head of linked list of blocks that can be migrated.
664 static struct MigrationReadyBlock *mig_head;
667 * Tail of linked list of blocks that can be migrated.
669 static struct MigrationReadyBlock *mig_tail;
672 * Request to datastore for migration (or NULL).
674 static struct GNUNET_DATASTORE_QueueEntry *mig_qe;
677 * ID of task that collects blocks for migration.
679 static GNUNET_SCHEDULER_TaskIdentifier mig_task;
682 * What is the maximum frequency at which we are allowed to
683 * poll the datastore for migration content?
685 static struct GNUNET_TIME_Relative min_migration_delay;
688 * Size of the doubly-linked list of migration blocks.
690 static unsigned int mig_size;
693 * Are we allowed to migrate content to this peer.
695 static int active_migration;
699 * Transmit messages by copying it to the target buffer
700 * "buf". "buf" will be NULL and "size" zero if the socket was closed
701 * for writing in the meantime. In that case, do nothing
702 * (the disconnect or shutdown handler will take care of the rest).
703 * If we were able to transmit messages and there are still more
704 * pending, ask core again for further calls to this function.
706 * @param cls closure, pointer to the 'struct ConnectedPeer*'
707 * @param size number of bytes available in buf
708 * @param buf where the callee should write the message
709 * @return number of bytes written to buf
712 transmit_to_peer (void *cls,
713 size_t size, void *buf);
716 /* ******************* clean up functions ************************ */
720 * Delete the given migration block.
722 * @param mb block to delete
725 delete_migration_block (struct MigrationReadyBlock *mb)
727 GNUNET_CONTAINER_DLL_remove (mig_head,
730 GNUNET_PEER_decrement_rcs (mb->target_list,
731 MIGRATION_LIST_SIZE);
738 * Compare the distance of two peers to a key.
741 * @param p1 first peer
742 * @param p2 second peer
743 * @return GNUNET_YES if P1 is closer to key than P2
746 is_closer (const GNUNET_HashCode *key,
747 const struct GNUNET_PeerIdentity *p1,
748 const struct GNUNET_PeerIdentity *p2)
750 return GNUNET_CRYPTO_hash_xorcmp (&p1->hashPubKey,
757 * Consider migrating content to a given peer.
759 * @param cls 'struct MigrationReadyBlock*' to select
760 * targets for (or NULL for none)
761 * @param key ID of the peer
762 * @param value 'struct ConnectedPeer' of the peer
763 * @return GNUNET_YES (always continue iteration)2
766 consider_migration (void *cls,
767 const GNUNET_HashCode *key,
770 struct MigrationReadyBlock *mb = cls;
771 struct ConnectedPeer *cp = value;
772 struct MigrationReadyBlock *pos;
773 struct GNUNET_PeerIdentity cppid;
774 struct GNUNET_PeerIdentity otherpid;
775 struct GNUNET_PeerIdentity worstpid;
780 /* consider 'cp' as a migration target for mb */
783 GNUNET_PEER_resolve (cp->pid,
785 repl = MIGRATION_LIST_SIZE;
786 for (i=0;i<MIGRATION_LIST_SIZE;i++)
788 if (mb->target_list[i] == 0)
790 mb->target_list[i] = cp->pid;
791 GNUNET_PEER_change_rc (mb->target_list[i], 1);
792 repl = MIGRATION_LIST_SIZE;
795 GNUNET_PEER_resolve (mb->target_list[i],
797 if ( (repl == MIGRATION_LIST_SIZE) &&
798 is_closer (&mb->query,
805 else if ( (repl != MIGRATION_LIST_SIZE) &&
806 (is_closer (&mb->query,
814 if (repl != MIGRATION_LIST_SIZE)
816 GNUNET_PEER_change_rc (mb->target_list[repl], -1);
817 mb->target_list[repl] = cp->pid;
818 GNUNET_PEER_change_rc (mb->target_list[repl], 1);
822 /* consider scheduling transmission to cp for content migration */
829 for (i=0;i<MIGRATION_LIST_SIZE;i++)
831 if (cp->pid == pos->target_list[i])
836 msize = GNUNET_MIN (msize,
844 return GNUNET_YES; /* no content available */
846 = GNUNET_CORE_notify_transmit_ready (core,
847 0, GNUNET_TIME_UNIT_FOREVER_REL,
848 (const struct GNUNET_PeerIdentity*) key,
849 msize + sizeof (struct PutMessage),
857 * Task that is run periodically to obtain blocks for content
861 * @param tc scheduler context (also unused)
864 gather_migration_blocks (void *cls,
865 const struct GNUNET_SCHEDULER_TaskContext *tc);
869 * If the migration task is not currently running, consider
870 * (re)scheduling it with the appropriate delay.
873 consider_migration_gathering ()
875 struct GNUNET_TIME_Relative delay;
879 if (mig_task != GNUNET_SCHEDULER_NO_TASK)
881 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
883 delay = GNUNET_TIME_relative_divide (GNUNET_TIME_UNIT_SECONDS,
884 MAX_MIGRATION_QUEUE);
885 delay = GNUNET_TIME_relative_max (delay,
886 min_migration_delay);
887 mig_task = GNUNET_SCHEDULER_add_delayed (sched,
889 &gather_migration_blocks,
895 * Process content offered for migration.
898 * @param key key for the content
899 * @param size number of bytes in data
900 * @param data content stored
901 * @param type type of the content
902 * @param priority priority of the content
903 * @param anonymity anonymity-level for the content
904 * @param expiration expiration time for the content
905 * @param uid unique identifier for the datum;
906 * maybe 0 if no unique identifier is available
909 process_migration_content (void *cls,
910 const GNUNET_HashCode * key,
913 enum GNUNET_BLOCK_Type type,
916 struct GNUNET_TIME_Absolute
917 expiration, uint64_t uid)
919 struct MigrationReadyBlock *mb;
924 if (mig_size < MAX_MIGRATION_QUEUE)
925 consider_migration_gathering ();
928 if (type == GNUNET_BLOCK_TYPE_ONDEMAND)
931 GNUNET_FS_handle_on_demand_block (key, size, data,
932 type, priority, anonymity,
934 &process_migration_content,
936 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
941 "Retrieved block `%s' of type %u for migration\n",
945 mb = GNUNET_malloc (sizeof (struct MigrationReadyBlock) + size);
947 mb->expiration = expiration;
950 memcpy (&mb[1], data, size);
951 GNUNET_CONTAINER_DLL_insert_after (mig_head,
956 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
959 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
964 * Task that is run periodically to obtain blocks for content
968 * @param tc scheduler context (also unused)
971 gather_migration_blocks (void *cls,
972 const struct GNUNET_SCHEDULER_TaskContext *tc)
974 mig_task = GNUNET_SCHEDULER_NO_TASK;
975 mig_qe = GNUNET_DATASTORE_get_random (dsh, 0, -1,
976 GNUNET_TIME_UNIT_FOREVER_REL,
977 &process_migration_content, NULL);
978 GNUNET_assert (mig_qe != NULL);
983 * We're done with a particular message list entry.
984 * Free all associated resources.
986 * @param pml entry to destroy
989 destroy_pending_message_list_entry (struct PendingMessageList *pml)
991 GNUNET_CONTAINER_DLL_remove (pml->req->pending_head,
992 pml->req->pending_tail,
994 GNUNET_CONTAINER_DLL_remove (pml->target->pending_messages_head,
995 pml->target->pending_messages_tail,
997 pml->target->pending_requests--;
998 GNUNET_free (pml->pm);
1004 * Destroy the given pending message (and call the respective
1007 * @param pm message to destroy
1008 * @param tpid id of peer that the message was delivered to, or 0 for none
1011 destroy_pending_message (struct PendingMessage *pm,
1012 GNUNET_PEER_Id tpid)
1014 struct PendingMessageList *pml = pm->pml;
1015 TransmissionContinuation cont;
1018 GNUNET_assert (pml->pm == pm);
1019 GNUNET_assert ( (tpid == 0) || (tpid == pml->target->pid) );
1021 cont_cls = pm->cont_cls;
1022 destroy_pending_message_list_entry (pml);
1023 cont (cont_cls, tpid);
1028 * We're done processing a particular request.
1029 * Free all associated resources.
1031 * @param pr request to destroy
1034 destroy_pending_request (struct PendingRequest *pr)
1036 struct GNUNET_PeerIdentity pid;
1038 if (pr->hnode != NULL)
1040 GNUNET_CONTAINER_heap_remove_node (requests_by_expiration_heap,
1044 if (NULL == pr->client_request_list)
1046 GNUNET_STATISTICS_update (stats,
1047 gettext_noop ("# P2P searches active"),
1053 GNUNET_STATISTICS_update (stats,
1054 gettext_noop ("# client searches active"),
1058 /* might have already been removed from map in 'process_reply' (if
1059 there was a unique reply) or never inserted if it was a
1060 duplicate; hence ignore the return value here */
1061 (void) GNUNET_CONTAINER_multihashmap_remove (query_request_map,
1066 GNUNET_DATASTORE_cancel (pr->qe);
1069 if (pr->client_request_list != NULL)
1071 GNUNET_CONTAINER_DLL_remove (pr->client_request_list->client_list->rl_head,
1072 pr->client_request_list->client_list->rl_tail,
1073 pr->client_request_list);
1074 GNUNET_free (pr->client_request_list);
1075 pr->client_request_list = NULL;
1079 GNUNET_PEER_resolve (pr->cp->pid,
1081 (void) GNUNET_CONTAINER_multihashmap_remove (peer_request_map,
1088 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
1091 if (pr->irc != NULL)
1093 GNUNET_CORE_peer_change_preference_cancel (pr->irc);
1096 if (pr->replies_seen != NULL)
1098 GNUNET_free (pr->replies_seen);
1099 pr->replies_seen = NULL;
1101 if (pr->task != GNUNET_SCHEDULER_NO_TASK)
1103 GNUNET_SCHEDULER_cancel (sched,
1105 pr->task = GNUNET_SCHEDULER_NO_TASK;
1107 while (NULL != pr->pending_head)
1108 destroy_pending_message_list_entry (pr->pending_head);
1109 GNUNET_PEER_change_rc (pr->target_pid, -1);
1110 if (pr->used_pids != NULL)
1112 GNUNET_PEER_decrement_rcs (pr->used_pids, pr->used_pids_off);
1113 GNUNET_free (pr->used_pids);
1114 pr->used_pids_off = 0;
1115 pr->used_pids_size = 0;
1116 pr->used_pids = NULL;
1123 * Method called whenever a given peer connects.
1125 * @param cls closure, not used
1126 * @param peer peer identity this notification is about
1127 * @param latency reported latency of the connection with 'other'
1128 * @param distance reported distance (DV) to 'other'
1131 peer_connect_handler (void *cls,
1133 GNUNET_PeerIdentity * peer,
1134 struct GNUNET_TIME_Relative latency,
1137 struct ConnectedPeer *cp;
1138 struct MigrationReadyBlock *pos;
1140 cp = GNUNET_malloc (sizeof (struct ConnectedPeer));
1141 cp->pid = GNUNET_PEER_intern (peer);
1142 GNUNET_break (GNUNET_OK ==
1143 GNUNET_CONTAINER_multihashmap_put (connected_peers,
1146 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1151 (void) consider_migration (pos, &peer->hashPubKey, cp);
1159 * Free (each) request made by the peer.
1161 * @param cls closure, points to peer that the request belongs to
1162 * @param key current key code
1163 * @param value value in the hash map
1164 * @return GNUNET_YES (we should continue to iterate)
1167 destroy_request (void *cls,
1168 const GNUNET_HashCode * key,
1171 const struct GNUNET_PeerIdentity * peer = cls;
1172 struct PendingRequest *pr = value;
1174 GNUNET_break (GNUNET_YES ==
1175 GNUNET_CONTAINER_multihashmap_remove (peer_request_map,
1178 destroy_pending_request (pr);
1184 * Method called whenever a peer disconnects.
1186 * @param cls closure, not used
1187 * @param peer peer identity this notification is about
1190 peer_disconnect_handler (void *cls,
1192 GNUNET_PeerIdentity * peer)
1194 struct ConnectedPeer *cp;
1195 struct PendingMessage *pm;
1197 struct MigrationReadyBlock *pos;
1198 struct MigrationReadyBlock *next;
1200 GNUNET_CONTAINER_multihashmap_get_multiple (peer_request_map,
1204 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
1208 for (i=0;i<CS2P_SUCCESS_LIST_SIZE;i++)
1210 if (NULL != cp->last_client_replies[i])
1212 GNUNET_SERVER_client_drop (cp->last_client_replies[i]);
1213 cp->last_client_replies[i] = NULL;
1216 GNUNET_break (GNUNET_YES ==
1217 GNUNET_CONTAINER_multihashmap_remove (connected_peers,
1220 /* remove this peer from migration considerations; schedule
1223 while (NULL != (pos = next))
1226 for (i=0;i<MIGRATION_LIST_SIZE;i++)
1228 if (pos->target_list[i] == cp->pid)
1230 GNUNET_PEER_change_rc (pos->target_list[i], -1);
1231 pos->target_list[i] = 0;
1234 if (pos->used_targets >= GNUNET_CONTAINER_multihashmap_size (connected_peers))
1236 delete_migration_block (pos);
1237 consider_migration_gathering ();
1240 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
1241 &consider_migration,
1245 GNUNET_PEER_change_rc (cp->pid, -1);
1246 GNUNET_PEER_decrement_rcs (cp->last_p2p_replies, P2P_SUCCESS_LIST_SIZE);
1247 if (NULL != cp->cth)
1248 GNUNET_CORE_notify_transmit_ready_cancel (cp->cth);
1249 while (NULL != (pm = cp->pending_messages_head))
1250 destroy_pending_message (pm, 0 /* delivery failed */);
1251 GNUNET_break (0 == cp->pending_requests);
1257 * Iterator over hash map entries that removes all occurences
1258 * of the given 'client' from the 'last_client_replies' of the
1259 * given connected peer.
1261 * @param cls closure, the 'struct GNUNET_SERVER_Client*' to remove
1262 * @param key current key code (unused)
1263 * @param value value in the hash map (the 'struct ConnectedPeer*' to change)
1264 * @return GNUNET_YES (we should continue to iterate)
1267 remove_client_from_last_client_replies (void *cls,
1268 const GNUNET_HashCode * key,
1271 struct GNUNET_SERVER_Client *client = cls;
1272 struct ConnectedPeer *cp = value;
1275 for (i=0;i<CS2P_SUCCESS_LIST_SIZE;i++)
1277 if (cp->last_client_replies[i] == client)
1279 GNUNET_SERVER_client_drop (cp->last_client_replies[i]);
1280 cp->last_client_replies[i] = NULL;
1288 * A client disconnected. Remove all of its pending queries.
1290 * @param cls closure, NULL
1291 * @param client identification of the client
1294 handle_client_disconnect (void *cls,
1295 struct GNUNET_SERVER_Client
1298 struct ClientList *pos;
1299 struct ClientList *prev;
1300 struct ClientRequestList *rcl;
1301 struct ClientResponseMessage *creply;
1307 while ( (NULL != pos) &&
1308 (pos->client != client) )
1314 return; /* no requests pending for this client */
1315 while (NULL != (rcl = pos->rl_head))
1317 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1318 "Destroying pending request `%s' on disconnect\n",
1319 GNUNET_h2s (&rcl->req->query));
1320 destroy_pending_request (rcl->req);
1323 client_list = pos->next;
1325 prev->next = pos->next;
1326 if (pos->th != NULL)
1328 GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th);
1331 while (NULL != (creply = pos->res_head))
1333 GNUNET_CONTAINER_DLL_remove (pos->res_head,
1336 GNUNET_free (creply);
1338 GNUNET_SERVER_client_drop (pos->client);
1340 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
1341 &remove_client_from_last_client_replies,
1347 * Iterator to free peer entries.
1349 * @param cls closure, unused
1350 * @param key current key code
1351 * @param value value in the hash map (peer entry)
1352 * @return GNUNET_YES (we should continue to iterate)
1355 clean_peer (void *cls,
1356 const GNUNET_HashCode * key,
1359 peer_disconnect_handler (NULL, (const struct GNUNET_PeerIdentity*) key);
1365 * Task run during shutdown.
1371 shutdown_task (void *cls,
1372 const struct GNUNET_SCHEDULER_TaskContext *tc)
1376 GNUNET_DATASTORE_cancel (mig_qe);
1379 if (GNUNET_SCHEDULER_NO_TASK != mig_task)
1381 GNUNET_SCHEDULER_cancel (sched, mig_task);
1382 mig_task = GNUNET_SCHEDULER_NO_TASK;
1384 while (client_list != NULL)
1385 handle_client_disconnect (NULL,
1386 client_list->client);
1387 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
1390 GNUNET_break (0 == GNUNET_CONTAINER_heap_get_size (requests_by_expiration_heap));
1391 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
1392 requests_by_expiration_heap = 0;
1393 GNUNET_CONTAINER_multihashmap_destroy (connected_peers);
1394 connected_peers = NULL;
1395 GNUNET_break (0 == GNUNET_CONTAINER_multihashmap_size (query_request_map));
1396 GNUNET_CONTAINER_multihashmap_destroy (query_request_map);
1397 query_request_map = NULL;
1398 GNUNET_break (0 == GNUNET_CONTAINER_multihashmap_size (peer_request_map));
1399 GNUNET_CONTAINER_multihashmap_destroy (peer_request_map);
1400 peer_request_map = NULL;
1401 GNUNET_assert (NULL != core);
1402 GNUNET_CORE_disconnect (core);
1406 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1409 GNUNET_DATASTORE_disconnect (dsh,
1411 while (mig_head != NULL)
1412 delete_migration_block (mig_head);
1413 GNUNET_assert (0 == mig_size);
1420 /* ******************* Utility functions ******************** */
1424 * Transmit messages by copying it to the target buffer
1425 * "buf". "buf" will be NULL and "size" zero if the socket was closed
1426 * for writing in the meantime. In that case, do nothing
1427 * (the disconnect or shutdown handler will take care of the rest).
1428 * If we were able to transmit messages and there are still more
1429 * pending, ask core again for further calls to this function.
1431 * @param cls closure, pointer to the 'struct ConnectedPeer*'
1432 * @param size number of bytes available in buf
1433 * @param buf where the callee should write the message
1434 * @return number of bytes written to buf
1437 transmit_to_peer (void *cls,
1438 size_t size, void *buf)
1440 struct ConnectedPeer *cp = cls;
1442 struct GNUNET_PeerIdentity pid;
1443 struct PendingMessage *pm;
1444 struct MigrationReadyBlock *mb;
1445 struct MigrationReadyBlock *next;
1446 struct PutMessage migm;
1454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1455 "Dropping message, core too busy.\n");
1460 while ( (NULL != (pm = cp->pending_messages_head) ) &&
1461 (pm->msize <= size) )
1463 memcpy (&cbuf[msize], &pm[1], pm->msize);
1466 destroy_pending_message (pm, cp->pid);
1470 GNUNET_PEER_resolve (cp->pid,
1472 cp->cth = GNUNET_CORE_notify_transmit_ready (core,
1474 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1483 while (NULL != (mb = next))
1486 for (i=0;i<MIGRATION_LIST_SIZE;i++)
1488 if ( (cp->pid == mb->target_list[i]) &&
1489 (mb->size + sizeof (migm) <= size) )
1491 GNUNET_PEER_change_rc (mb->target_list[i], -1);
1492 mb->target_list[i] = 0;
1494 migm.header.size = htons (sizeof (migm) + mb->size);
1495 migm.header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
1496 migm.type = htonl (mb->type);
1497 migm.expiration = GNUNET_TIME_absolute_hton (mb->expiration);
1498 memcpy (&cbuf[msize], &migm, sizeof (migm));
1499 msize += sizeof (migm);
1500 size -= sizeof (migm);
1501 memcpy (&cbuf[msize], &mb[1], mb->size);
1507 if ( (mb->used_targets >= MIGRATION_TARGET_COUNT) ||
1508 (mb->used_targets >= GNUNET_CONTAINER_multihashmap_size (connected_peers)) )
1510 delete_migration_block (mb);
1511 consider_migration_gathering ();
1514 consider_migration (NULL,
1519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1520 "Transmitting %u bytes to peer %u\n",
1529 * Add a message to the set of pending messages for the given peer.
1531 * @param cp peer to send message to
1532 * @param pm message to queue
1533 * @param pr request on which behalf this message is being queued
1536 add_to_pending_messages_for_peer (struct ConnectedPeer *cp,
1537 struct PendingMessage *pm,
1538 struct PendingRequest *pr)
1540 struct PendingMessage *pos;
1541 struct PendingMessageList *pml;
1542 struct GNUNET_PeerIdentity pid;
1544 GNUNET_assert (pm->next == NULL);
1545 GNUNET_assert (pm->pml == NULL);
1546 pml = GNUNET_malloc (sizeof (struct PendingMessageList));
1551 GNUNET_CONTAINER_DLL_insert (pr->pending_head,
1554 pos = cp->pending_messages_head;
1555 while ( (pos != NULL) &&
1556 (pm->priority < pos->priority) )
1558 GNUNET_CONTAINER_DLL_insert_after (cp->pending_messages_head,
1559 cp->pending_messages_tail,
1562 cp->pending_requests++;
1563 if (cp->pending_requests > MAX_QUEUE_PER_PEER)
1564 destroy_pending_message (cp->pending_messages_tail, 0);
1565 GNUNET_PEER_resolve (cp->pid, &pid);
1566 if (NULL != cp->cth)
1567 GNUNET_CORE_notify_transmit_ready_cancel (cp->cth);
1568 /* need to schedule transmission */
1569 cp->cth = GNUNET_CORE_notify_transmit_ready (core,
1570 cp->pending_messages_head->priority,
1573 cp->pending_messages_head->msize,
1576 if (cp->cth == NULL)
1579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1580 "Failed to schedule transmission with core!\n");
1582 /* FIXME: call stats (rare, bad case) */
1588 * Mingle hash with the mingle_number to produce different bits.
1591 mingle_hash (const GNUNET_HashCode * in,
1592 int32_t mingle_number,
1593 GNUNET_HashCode * hc)
1597 GNUNET_CRYPTO_hash (&mingle_number,
1600 GNUNET_CRYPTO_hash_xor (&m, in, hc);
1605 * Test if the load on this peer is too high
1606 * to even consider processing the query at
1609 * @return GNUNET_YES if the load is too high, GNUNET_NO otherwise
1612 test_load_too_high ()
1614 return GNUNET_NO; // FIXME
1618 /* ******************* Pending Request Refresh Task ******************** */
1623 * We use a random delay to make the timing of requests less
1624 * predictable. This function returns such a random delay. We add a base
1625 * delay of MAX_CORK_DELAY (1s).
1627 * FIXME: make schedule dependent on the specifics of the request?
1628 * Or bandwidth and number of connected peers and load?
1630 * @return random delay to use for some request, between 1s and 1000+TTL_DECREMENT ms
1632 static struct GNUNET_TIME_Relative
1633 get_processing_delay ()
1636 GNUNET_TIME_relative_add (GNUNET_CONSTANTS_MAX_CORK_DELAY,
1637 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1638 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1644 * We're processing a GET request from another peer and have decided
1645 * to forward it to other peers. This function is called periodically
1646 * and should forward the request to other peers until we have all
1647 * possible replies. If we have transmitted the *only* reply to
1648 * the initiator we should destroy the pending request. If we have
1649 * many replies in the queue to the initiator, we should delay sending
1650 * out more queries until the reply queue has shrunk some.
1652 * @param cls our "struct ProcessGetContext *"
1656 forward_request_task (void *cls,
1657 const struct GNUNET_SCHEDULER_TaskContext *tc);
1661 * Function called after we either failed or succeeded
1662 * at transmitting a query to a peer.
1664 * @param cls the requests "struct PendingRequest*"
1665 * @param tpid ID of receiving peer, 0 on transmission error
1668 transmit_query_continuation (void *cls,
1669 GNUNET_PEER_Id tpid)
1671 struct PendingRequest *pr = cls;
1673 GNUNET_STATISTICS_update (stats,
1674 gettext_noop ("# queries scheduled for forwarding"),
1680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1681 "Transmission of request failed, will try again later.\n");
1683 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
1684 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
1685 get_processing_delay (),
1686 &forward_request_task,
1690 GNUNET_STATISTICS_update (stats,
1691 gettext_noop ("# queries forwarded"),
1694 GNUNET_PEER_change_rc (tpid, 1);
1695 if (pr->used_pids_off == pr->used_pids_size)
1696 GNUNET_array_grow (pr->used_pids,
1698 pr->used_pids_size * 2 + 2);
1699 pr->used_pids[pr->used_pids_off++] = tpid;
1700 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
1701 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
1702 get_processing_delay (),
1703 &forward_request_task,
1709 * How many bytes should a bloomfilter be if we have already seen
1710 * entry_count responses? Note that BLOOMFILTER_K gives us the number
1711 * of bits set per entry. Furthermore, we should not re-size the
1712 * filter too often (to keep it cheap).
1714 * Since other peers will also add entries but not resize the filter,
1715 * we should generally pick a slightly larger size than what the
1716 * strict math would suggest.
1718 * @return must be a power of two and smaller or equal to 2^15.
1721 compute_bloomfilter_size (unsigned int entry_count)
1724 unsigned int ideal = (entry_count * BLOOMFILTER_K) / 4;
1725 uint16_t max = 1 << 15;
1727 if (entry_count > max)
1730 while ((size < max) && (size < ideal))
1739 * Recalculate our bloom filter for filtering replies. This function
1740 * will create a new bloom filter from scratch, so it should only be
1741 * called if we have no bloomfilter at all (and hence can create a
1742 * fresh one of minimal size without problems) OR if our peer is the
1743 * initiator (in which case we may resize to larger than mimimum size).
1745 * @param pr request for which the BF is to be recomputed
1748 refresh_bloomfilter (struct PendingRequest *pr)
1752 GNUNET_HashCode mhash;
1754 nsize = compute_bloomfilter_size (pr->replies_seen_off);
1755 if (nsize == pr->bf_size)
1756 return; /* size not changed */
1758 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
1759 pr->bf_size = nsize;
1760 pr->mingle = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, -1);
1761 pr->bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
1764 for (i=0;i<pr->replies_seen_off;i++)
1766 mingle_hash (&pr->replies_seen[i], pr->mingle, &mhash);
1767 GNUNET_CONTAINER_bloomfilter_add (pr->bf, &mhash);
1773 * Function called after we've tried to reserve a certain amount of
1774 * bandwidth for a reply. Check if we succeeded and if so send our
1777 * @param cls the requests "struct PendingRequest*"
1778 * @param peer identifies the peer
1779 * @param bpm_in set to the current bandwidth limit (receiving) for this peer
1780 * @param bpm_out set to the current bandwidth limit (sending) for this peer
1781 * @param amount set to the amount that was actually reserved or unreserved
1782 * @param preference current traffic preference for the given peer
1785 target_reservation_cb (void *cls,
1787 GNUNET_PeerIdentity * peer,
1788 struct GNUNET_BANDWIDTH_Value32NBO bpm_in,
1789 struct GNUNET_BANDWIDTH_Value32NBO bpm_out,
1791 uint64_t preference)
1793 struct PendingRequest *pr = cls;
1794 struct ConnectedPeer *cp;
1795 struct PendingMessage *pm;
1796 struct GetMessage *gm;
1797 GNUNET_HashCode *ext;
1807 /* error in communication with core, try again later */
1808 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
1809 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
1810 get_processing_delay (),
1811 &forward_request_task,
1815 // (3) transmit, update ttl/priority
1816 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
1820 /* Peer must have just left */
1822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1823 "Selected peer disconnected!\n");
1825 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
1826 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
1827 get_processing_delay (),
1828 &forward_request_task,
1832 no_route = GNUNET_NO;
1833 /* FIXME: check against DBLOCK_SIZE and possibly return
1834 amount to reserve; however, this also needs to work
1835 with testcases which currently start out with a far
1836 too low per-peer bw limit, so they would never send
1837 anything. Big issue. */
1843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1844 "Failed to reserve bandwidth for reply (got %d/%u bytes only)!\n",
1848 GNUNET_STATISTICS_update (stats,
1849 gettext_noop ("# reply bandwidth reservation requests failed"),
1852 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
1853 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
1854 get_processing_delay (),
1855 &forward_request_task,
1857 return; /* this target round failed */
1859 /* FIXME: if we are "quite" busy, we may still want to skip
1860 this round; need more load detection code! */
1861 no_route = GNUNET_YES;
1864 GNUNET_STATISTICS_update (stats,
1865 gettext_noop ("# queries scheduled for forwarding"),
1868 /* build message and insert message into priority queue */
1870 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1871 "Forwarding request `%s' to `%4s'!\n",
1872 GNUNET_h2s (&pr->query),
1877 if (GNUNET_YES == no_route)
1879 bm |= GET_MESSAGE_BIT_RETURN_TO;
1882 if (pr->namespace != NULL)
1884 bm |= GET_MESSAGE_BIT_SKS_NAMESPACE;
1887 if (pr->target_pid != 0)
1889 bm |= GET_MESSAGE_BIT_TRANSMIT_TO;
1892 msize = sizeof (struct GetMessage) + pr->bf_size + k * sizeof(GNUNET_HashCode);
1893 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1894 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1896 gm = (struct GetMessage*) &pm[1];
1897 gm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_GET);
1898 gm->header.size = htons (msize);
1899 gm->type = htonl (pr->type);
1900 pr->remaining_priority /= 2;
1901 gm->priority = htonl (pr->remaining_priority);
1902 gm->ttl = htonl (pr->ttl);
1903 gm->filter_mutator = htonl(pr->mingle);
1904 gm->hash_bitmap = htonl (bm);
1905 gm->query = pr->query;
1906 ext = (GNUNET_HashCode*) &gm[1];
1908 if (GNUNET_YES == no_route)
1909 GNUNET_PEER_resolve (pr->cp->pid, (struct GNUNET_PeerIdentity*) &ext[k++]);
1910 if (pr->namespace != NULL)
1911 memcpy (&ext[k++], pr->namespace, sizeof (GNUNET_HashCode));
1912 if (pr->target_pid != 0)
1913 GNUNET_PEER_resolve (pr->target_pid, (struct GNUNET_PeerIdentity*) &ext[k++]);
1914 bfdata = (char *) &ext[k];
1916 GNUNET_CONTAINER_bloomfilter_get_raw_data (pr->bf,
1919 pm->cont = &transmit_query_continuation;
1921 add_to_pending_messages_for_peer (cp, pm, pr);
1926 * Closure used for "target_peer_select_cb".
1928 struct PeerSelectionContext
1931 * The request for which we are selecting
1934 struct PendingRequest *pr;
1937 * Current "prime" target.
1939 struct GNUNET_PeerIdentity target;
1942 * How much do we like this target?
1944 double target_score;
1950 * Function called for each connected peer to determine
1951 * which one(s) would make good targets for forwarding.
1953 * @param cls closure (struct PeerSelectionContext)
1954 * @param key current key code (peer identity)
1955 * @param value value in the hash map (struct ConnectedPeer)
1956 * @return GNUNET_YES if we should continue to
1961 target_peer_select_cb (void *cls,
1962 const GNUNET_HashCode * key,
1965 struct PeerSelectionContext *psc = cls;
1966 struct ConnectedPeer *cp = value;
1967 struct PendingRequest *pr = psc->pr;
1972 /* 1) check that this peer is not the initiator */
1974 return GNUNET_YES; /* skip */
1976 /* 2) check if we have already (recently) forwarded to this peer */
1978 for (i=0;i<pr->used_pids_off;i++)
1979 if (pr->used_pids[i] == cp->pid)
1982 if (0 != GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1983 RETRY_PROBABILITY_INV))
1986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1987 "NOT re-trying query that was previously transmitted %u times\n",
1988 (unsigned int) pr->used_pids_off);
1990 return GNUNET_YES; /* skip */
1995 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1996 "Re-trying query that was previously transmitted %u times to this peer\n",
1999 // 3) calculate how much we'd like to forward to this peer
2000 score = 42; // FIXME!
2001 // FIXME: also need API to gather data on responsiveness
2002 // of this peer (we have fields for that in 'cp', but
2003 // they are never set!)
2005 /* store best-fit in closure */
2006 if (score > psc->target_score)
2008 psc->target_score = score;
2009 psc->target.hashPubKey = *key;
2016 * The priority level imposes a bound on the maximum
2017 * value for the ttl that can be requested.
2019 * @param ttl_in requested ttl
2020 * @param prio given priority
2021 * @return ttl_in if ttl_in is below the limit,
2022 * otherwise the ttl-limit for the given priority
2025 bound_ttl (int32_t ttl_in, uint32_t prio)
2027 unsigned long long allowed;
2031 allowed = ((unsigned long long) prio) * TTL_DECREMENT / 1000;
2032 if (ttl_in > allowed)
2034 if (allowed >= (1 << 30))
2043 * We're processing a GET request from another peer and have decided
2044 * to forward it to other peers. This function is called periodically
2045 * and should forward the request to other peers until we have all
2046 * possible replies. If we have transmitted the *only* reply to
2047 * the initiator we should destroy the pending request. If we have
2048 * many replies in the queue to the initiator, we should delay sending
2049 * out more queries until the reply queue has shrunk some.
2051 * @param cls our "struct ProcessGetContext *"
2055 forward_request_task (void *cls,
2056 const struct GNUNET_SCHEDULER_TaskContext *tc)
2058 struct PendingRequest *pr = cls;
2059 struct PeerSelectionContext psc;
2060 struct ConnectedPeer *cp;
2061 struct GNUNET_TIME_Relative delay;
2063 pr->task = GNUNET_SCHEDULER_NO_TASK;
2064 if (pr->irc != NULL)
2067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2068 "Forwarding of query `%s' not attempted due to pending local lookup!\n",
2069 GNUNET_h2s (&pr->query));
2071 return; /* already pending */
2073 if (GNUNET_YES == pr->local_only)
2074 return; /* configured to not do P2P search */
2075 /* (1) select target */
2077 psc.target_score = DBL_MIN;
2078 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
2079 &target_peer_select_cb,
2081 if (psc.target_score == DBL_MIN)
2083 delay = get_processing_delay ();
2085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2086 "No peer selected for forwarding of query `%s', will try again in %llu ms!\n",
2087 GNUNET_h2s (&pr->query),
2090 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
2092 &forward_request_task,
2094 return; /* nobody selected */
2096 /* (3) update TTL/priority */
2098 if (pr->client_request_list != NULL)
2100 /* FIXME: use better algorithm!? */
2101 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2104 /* FIXME: bound priority by "customary" priority used by other peers
2106 pr->ttl = bound_ttl (pr->ttl + TTL_DECREMENT * 2,
2109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2110 "Trying query `%s' with priority %u and TTL %d.\n",
2111 GNUNET_h2s (&pr->query),
2118 /* FIXME: should we do something here as well!? */
2121 /* (3) reserve reply bandwidth */
2122 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
2123 &psc.target.hashPubKey);
2124 GNUNET_assert (NULL != cp);
2125 pr->irc = GNUNET_CORE_peer_change_preference (sched, cfg,
2127 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
2128 GNUNET_BANDWIDTH_value_init ((uint32_t) -1 /* no limit */),
2130 (uint64_t) cp->inc_preference,
2131 &target_reservation_cb,
2133 cp->inc_preference = 0.0;
2137 /* **************************** P2P PUT Handling ************************ */
2141 * Function called after we either failed or succeeded
2142 * at transmitting a reply to a peer.
2144 * @param cls the requests "struct PendingRequest*"
2145 * @param tpid ID of receiving peer, 0 on transmission error
2148 transmit_reply_continuation (void *cls,
2149 GNUNET_PEER_Id tpid)
2151 struct PendingRequest *pr = cls;
2155 case GNUNET_BLOCK_TYPE_DBLOCK:
2156 case GNUNET_BLOCK_TYPE_IBLOCK:
2157 /* only one reply expected, done with the request! */
2158 destroy_pending_request (pr);
2160 case GNUNET_BLOCK_TYPE_ANY:
2161 case GNUNET_BLOCK_TYPE_KBLOCK:
2162 case GNUNET_BLOCK_TYPE_SBLOCK:
2172 * Transmit the given message by copying it to the target buffer
2173 * "buf". "buf" will be NULL and "size" zero if the socket was closed
2174 * for writing in the meantime. In that case, do nothing
2175 * (the disconnect or shutdown handler will take care of the rest).
2176 * If we were able to transmit messages and there are still more
2177 * pending, ask core again for further calls to this function.
2179 * @param cls closure, pointer to the 'struct ClientList*'
2180 * @param size number of bytes available in buf
2181 * @param buf where the callee should write the message
2182 * @return number of bytes written to buf
2185 transmit_to_client (void *cls,
2186 size_t size, void *buf)
2188 struct ClientList *cl = cls;
2190 struct ClientResponseMessage *creply;
2197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2198 "Not sending reply, client communication problem.\n");
2203 while ( (NULL != (creply = cl->res_head) ) &&
2204 (creply->msize <= size) )
2206 memcpy (&cbuf[msize], &creply[1], creply->msize);
2207 msize += creply->msize;
2208 size -= creply->msize;
2209 GNUNET_CONTAINER_DLL_remove (cl->res_head,
2212 GNUNET_free (creply);
2215 cl->th = GNUNET_SERVER_notify_transmit_ready (cl->client,
2217 GNUNET_TIME_UNIT_FOREVER_REL,
2218 &transmit_to_client,
2221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2222 "Transmitted %u bytes to client\n",
2223 (unsigned int) msize);
2230 * Closure for "process_reply" function.
2232 struct ProcessReplyClosure
2235 * The data for the reply.
2240 * Who gave us this reply? NULL for local host.
2242 struct ConnectedPeer *sender;
2245 * When the reply expires.
2247 struct GNUNET_TIME_Absolute expiration;
2255 * Namespace that this reply belongs to
2256 * (if it is of type SBLOCK).
2258 GNUNET_HashCode namespace;
2261 * Type of the block.
2263 enum GNUNET_BLOCK_Type type;
2266 * How much was this reply worth to us?
2271 * Did we finish processing the associated request?
2278 * We have received a reply; handle it!
2280 * @param cls response (struct ProcessReplyClosure)
2281 * @param key our query
2282 * @param value value in the hash map (info about the query)
2283 * @return GNUNET_YES (we should continue to iterate)
2286 process_reply (void *cls,
2287 const GNUNET_HashCode * key,
2290 struct ProcessReplyClosure *prq = cls;
2291 struct PendingRequest *pr = value;
2292 struct PendingMessage *reply;
2293 struct ClientResponseMessage *creply;
2294 struct ClientList *cl;
2295 struct PutMessage *pm;
2296 struct ConnectedPeer *cp;
2297 struct GNUNET_TIME_Relative cur_delay;
2298 GNUNET_HashCode chash;
2299 GNUNET_HashCode mhash;
2303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2304 "Matched result (type %u) for query `%s' with pending request\n",
2305 (unsigned int) prq->type,
2308 GNUNET_STATISTICS_update (stats,
2309 gettext_noop ("# replies received and matched"),
2312 if (prq->sender != NULL)
2314 /* FIXME: should we be more precise here and not use
2315 "start_time" but a peer-specific time stamp? */
2316 cur_delay = GNUNET_TIME_absolute_get_duration (pr->start_time);
2317 prq->sender->avg_delay.value
2318 = (prq->sender->avg_delay.value *
2319 (RUNAVG_DELAY_N - 1) + cur_delay.value) / RUNAVG_DELAY_N;
2320 prq->sender->avg_priority
2321 = (prq->sender->avg_priority *
2322 (RUNAVG_DELAY_N - 1) + pr->priority) / (double) RUNAVG_DELAY_N;
2325 GNUNET_PEER_change_rc (prq->sender->last_p2p_replies
2326 [prq->sender->last_p2p_replies_woff % P2P_SUCCESS_LIST_SIZE], -1);
2327 GNUNET_PEER_change_rc (pr->cp->pid, 1);
2328 prq->sender->last_p2p_replies
2329 [(prq->sender->last_p2p_replies_woff++) % P2P_SUCCESS_LIST_SIZE]
2334 if (NULL != prq->sender->last_client_replies
2335 [(prq->sender->last_client_replies_woff) % CS2P_SUCCESS_LIST_SIZE])
2336 GNUNET_SERVER_client_drop (prq->sender->last_client_replies
2337 [(prq->sender->last_client_replies_woff) % CS2P_SUCCESS_LIST_SIZE]);
2338 prq->sender->last_client_replies
2339 [(prq->sender->last_client_replies_woff++) % CS2P_SUCCESS_LIST_SIZE]
2340 = pr->client_request_list->client_list->client;
2341 GNUNET_SERVER_client_keep (pr->client_request_list->client_list->client);
2344 GNUNET_CRYPTO_hash (prq->data,
2349 case GNUNET_BLOCK_TYPE_DBLOCK:
2350 case GNUNET_BLOCK_TYPE_IBLOCK:
2351 /* only possible reply, stop requesting! */
2352 while (NULL != pr->pending_head)
2353 destroy_pending_message_list_entry (pr->pending_head);
2356 if (pr->client_request_list != NULL)
2357 GNUNET_SERVER_receive_done (pr->client_request_list->client_list->client,
2359 GNUNET_DATASTORE_cancel (pr->qe);
2362 pr->do_remove = GNUNET_YES;
2363 if (pr->task != GNUNET_SCHEDULER_NO_TASK)
2365 GNUNET_SCHEDULER_cancel (sched,
2367 pr->task = GNUNET_SCHEDULER_NO_TASK;
2369 GNUNET_break (GNUNET_YES ==
2370 GNUNET_CONTAINER_multihashmap_remove (query_request_map,
2374 case GNUNET_BLOCK_TYPE_SBLOCK:
2375 if (pr->namespace == NULL)
2380 if (0 != memcmp (pr->namespace,
2382 sizeof (GNUNET_HashCode)))
2384 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2385 _("Reply mismatched in terms of namespace. Discarded.\n"));
2386 return GNUNET_YES; /* wrong namespace */
2388 /* then: fall-through! */
2389 case GNUNET_BLOCK_TYPE_KBLOCK:
2390 case GNUNET_BLOCK_TYPE_NBLOCK:
2393 mingle_hash (&chash, pr->mingle, &mhash);
2394 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (pr->bf,
2397 GNUNET_STATISTICS_update (stats,
2398 gettext_noop ("# duplicate replies discarded (bloomfilter)"),
2402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2403 "Duplicate response `%s', discarding.\n",
2404 GNUNET_h2s (&mhash));
2406 return GNUNET_YES; /* duplicate */
2409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2410 "New response `%s', adding to filter.\n",
2411 GNUNET_h2s (&mhash));
2414 if (pr->client_request_list != NULL)
2416 if (pr->replies_seen_size == pr->replies_seen_off)
2417 GNUNET_array_grow (pr->replies_seen,
2418 pr->replies_seen_size,
2419 pr->replies_seen_size * 2 + 4);
2420 pr->replies_seen[pr->replies_seen_off++] = chash;
2422 if ( (pr->bf == NULL) ||
2423 (pr->client_request_list != NULL) )
2424 refresh_bloomfilter (pr);
2425 GNUNET_CONTAINER_bloomfilter_add (pr->bf,
2432 prq->priority += pr->remaining_priority;
2433 pr->remaining_priority = 0;
2434 if (NULL != pr->client_request_list)
2436 GNUNET_STATISTICS_update (stats,
2437 gettext_noop ("# replies received for local clients"),
2440 cl = pr->client_request_list->client_list;
2441 msize = sizeof (struct PutMessage) + prq->size;
2442 creply = GNUNET_malloc (msize + sizeof (struct ClientResponseMessage));
2443 creply->msize = msize;
2444 creply->client_list = cl;
2445 GNUNET_CONTAINER_DLL_insert_after (cl->res_head,
2449 pm = (struct PutMessage*) &creply[1];
2450 pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
2451 pm->header.size = htons (msize);
2452 pm->type = htonl (prq->type);
2453 pm->expiration = GNUNET_TIME_absolute_hton (prq->expiration);
2454 memcpy (&pm[1], prq->data, prq->size);
2458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2459 "Transmitting result for query `%s' to client\n",
2462 cl->th = GNUNET_SERVER_notify_transmit_ready (cl->client,
2464 GNUNET_TIME_UNIT_FOREVER_REL,
2465 &transmit_to_client,
2468 GNUNET_break (cl->th != NULL);
2471 prq->finished = GNUNET_YES;
2472 destroy_pending_request (pr);
2479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2480 "Transmitting result for query `%s' to other peer (PID=%u)\n",
2482 (unsigned int) cp->pid);
2484 GNUNET_STATISTICS_update (stats,
2485 gettext_noop ("# replies received for other peers"),
2488 msize = sizeof (struct PutMessage) + prq->size;
2489 reply = GNUNET_malloc (msize + sizeof (struct PendingMessage));
2490 reply->cont = &transmit_reply_continuation;
2491 reply->cont_cls = pr;
2492 reply->msize = msize;
2493 reply->priority = (uint32_t) -1; /* send replies first! */
2494 pm = (struct PutMessage*) &reply[1];
2495 pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
2496 pm->header.size = htons (msize);
2497 pm->type = htonl (prq->type);
2498 pm->expiration = GNUNET_TIME_absolute_hton (prq->expiration);
2499 memcpy (&pm[1], prq->data, prq->size);
2500 add_to_pending_messages_for_peer (cp, reply, pr);
2507 * Continuation called to notify client about result of the
2510 * @param cls closure
2511 * @param success GNUNET_SYSERR on failure
2512 * @param msg NULL on success, otherwise an error message
2515 put_migration_continuation (void *cls,
2524 * Handle P2P "PUT" message.
2526 * @param cls closure, always NULL
2527 * @param other the other peer involved (sender or receiver, NULL
2528 * for loopback messages where we are both sender and receiver)
2529 * @param message the actual message
2530 * @param latency reported latency of the connection with 'other'
2531 * @param distance reported distance (DV) to 'other'
2532 * @return GNUNET_OK to keep the connection open,
2533 * GNUNET_SYSERR to close it (signal serious error)
2536 handle_p2p_put (void *cls,
2537 const struct GNUNET_PeerIdentity *other,
2538 const struct GNUNET_MessageHeader *message,
2539 struct GNUNET_TIME_Relative latency,
2542 const struct PutMessage *put;
2545 enum GNUNET_BLOCK_Type type;
2546 struct GNUNET_TIME_Absolute expiration;
2547 GNUNET_HashCode query;
2548 struct ProcessReplyClosure prq;
2549 const struct SBlock *sb;
2551 msize = ntohs (message->size);
2552 if (msize < sizeof (struct PutMessage))
2555 return GNUNET_SYSERR;
2557 put = (const struct PutMessage*) message;
2558 dsize = msize - sizeof (struct PutMessage);
2559 type = ntohl (put->type);
2560 expiration = GNUNET_TIME_absolute_ntoh (put->expiration);
2563 GNUNET_BLOCK_check_block (type,
2568 GNUNET_break_op (0);
2569 return GNUNET_SYSERR;
2571 if (type == GNUNET_BLOCK_TYPE_ONDEMAND)
2572 return GNUNET_SYSERR;
2573 if (GNUNET_BLOCK_TYPE_SBLOCK == type)
2575 sb = (const struct SBlock*) &put[1];
2576 GNUNET_CRYPTO_hash (&sb->subspace,
2577 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2583 "Received result for query `%s' from peer `%4s'\n",
2584 GNUNET_h2s (&query),
2585 GNUNET_i2s (other));
2587 GNUNET_STATISTICS_update (stats,
2588 gettext_noop ("# replies received (overall)"),
2591 /* now, lookup 'query' */
2592 prq.data = (const void*) &put[1];
2594 prq.sender = GNUNET_CONTAINER_multihashmap_get (connected_peers,
2595 &other->hashPubKey);
2598 prq.expiration = expiration;
2600 prq.finished = GNUNET_NO;
2601 GNUNET_CONTAINER_multihashmap_get_multiple (query_request_map,
2605 if (GNUNET_YES == active_migration)
2608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2609 "Replicating result for query `%s' with priority %u\n",
2610 GNUNET_h2s (&query),
2613 GNUNET_DATASTORE_put (dsh,
2614 0, &query, dsize, &put[1],
2615 type, prq.priority, 1 /* anonymity */,
2617 1 + prq.priority, MAX_DATASTORE_QUEUE,
2618 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
2619 &put_migration_continuation,
2626 /* **************************** P2P GET Handling ************************ */
2630 * Closure for 'check_duplicate_request_{peer,client}'.
2632 struct CheckDuplicateRequestClosure
2635 * The new request we should check if it already exists.
2637 const struct PendingRequest *pr;
2640 * Existing request found by the checker, NULL if none.
2642 struct PendingRequest *have;
2647 * Iterator over entries in the 'query_request_map' that
2648 * tries to see if we have the same request pending from
2649 * the same client already.
2651 * @param cls closure (our 'struct CheckDuplicateRequestClosure')
2652 * @param key current key code (query, ignored, must match)
2653 * @param value value in the hash map (a 'struct PendingRequest'
2654 * that already exists)
2655 * @return GNUNET_YES if we should continue to
2656 * iterate (no match yet)
2657 * GNUNET_NO if not (match found).
2660 check_duplicate_request_client (void *cls,
2661 const GNUNET_HashCode * key,
2664 struct CheckDuplicateRequestClosure *cdc = cls;
2665 struct PendingRequest *have = value;
2667 if (have->client_request_list == NULL)
2669 if ( (cdc->pr->client_request_list->client_list->client == have->client_request_list->client_list->client) &&
2680 * We're processing (local) results for a search request
2681 * from another peer. Pass applicable results to the
2682 * peer and if we are done either clean up (operation
2683 * complete) or forward to other peers (more results possible).
2685 * @param cls our closure (struct LocalGetContext)
2686 * @param key key for the content
2687 * @param size number of bytes in data
2688 * @param data content stored
2689 * @param type type of the content
2690 * @param priority priority of the content
2691 * @param anonymity anonymity-level for the content
2692 * @param expiration expiration time for the content
2693 * @param uid unique identifier for the datum;
2694 * maybe 0 if no unique identifier is available
2697 process_local_reply (void *cls,
2698 const GNUNET_HashCode * key,
2701 enum GNUNET_BLOCK_Type type,
2704 struct GNUNET_TIME_Absolute
2708 struct PendingRequest *pr = cls;
2709 struct ProcessReplyClosure prq;
2710 struct CheckDuplicateRequestClosure cdrc;
2711 const struct SBlock *sb;
2712 GNUNET_HashCode dhash;
2713 GNUNET_HashCode mhash;
2714 GNUNET_HashCode query;
2719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2720 "Done processing local replies, forwarding request to other peers.\n");
2723 if (pr->client_request_list != NULL)
2725 GNUNET_SERVER_receive_done (pr->client_request_list->client_list->client,
2727 /* Figure out if this is a duplicate request and possibly
2728 merge 'struct PendingRequest' entries */
2731 GNUNET_CONTAINER_multihashmap_get_multiple (query_request_map,
2733 &check_duplicate_request_client,
2735 if (cdrc.have != NULL)
2738 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2739 "Received request for block `%s' twice from client, will only request once.\n",
2740 GNUNET_h2s (&pr->query));
2743 destroy_pending_request (pr);
2748 /* no more results */
2749 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
2750 pr->task = GNUNET_SCHEDULER_add_now (sched,
2751 &forward_request_task,
2756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2757 "New local response to `%s' of type %u.\n",
2761 if (type == GNUNET_BLOCK_TYPE_ONDEMAND)
2764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2765 "Found ONDEMAND block, performing on-demand encoding\n");
2767 GNUNET_STATISTICS_update (stats,
2768 gettext_noop ("# on-demand blocks matched requests"),
2772 GNUNET_FS_handle_on_demand_block (key, size, data, type, priority,
2773 anonymity, expiration, uid,
2774 &process_local_reply,
2777 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
2780 /* check for duplicates */
2781 GNUNET_CRYPTO_hash (data, size, &dhash);
2782 mingle_hash (&dhash,
2785 if ( (pr->bf != NULL) &&
2787 GNUNET_CONTAINER_bloomfilter_test (pr->bf,
2791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2792 "Result from datastore filtered by bloomfilter (duplicate).\n");
2794 GNUNET_STATISTICS_update (stats,
2795 gettext_noop ("# results filtered by query bloomfilter"),
2799 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
2803 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2804 "Found result for query `%s' in local datastore\n",
2807 GNUNET_STATISTICS_update (stats,
2808 gettext_noop ("# results found locally"),
2811 pr->results_found++;
2812 memset (&prq, 0, sizeof (prq));
2814 prq.expiration = expiration;
2816 if (GNUNET_BLOCK_TYPE_SBLOCK == type)
2818 sb = (const struct SBlock*) data;
2819 GNUNET_CRYPTO_hash (&sb->subspace,
2820 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2823 if (GNUNET_OK != GNUNET_BLOCK_check_block (type,
2829 GNUNET_DATASTORE_remove (dsh,
2833 GNUNET_TIME_UNIT_FOREVER_REL,
2835 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
2839 prq.priority = priority;
2840 prq.finished = GNUNET_NO;
2841 process_reply (&prq, key, pr);
2842 if (prq.finished == GNUNET_YES)
2845 return; /* done here */
2846 if ( (type == GNUNET_BLOCK_TYPE_DBLOCK) ||
2847 (type == GNUNET_BLOCK_TYPE_IBLOCK) )
2849 GNUNET_DATASTORE_get_next (dsh, GNUNET_NO);
2852 if ( (pr->client_request_list == NULL) &&
2853 ( (GNUNET_YES == test_load_too_high()) ||
2854 (pr->results_found > 5 + 2 * pr->priority) ) )
2857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2858 "Load too high, done with request\n");
2860 GNUNET_STATISTICS_update (stats,
2861 gettext_noop ("# processing result set cut short due to load"),
2864 GNUNET_DATASTORE_get_next (dsh, GNUNET_NO);
2867 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
2872 * We've received a request with the specified priority. Bound it
2873 * according to how much we trust the given peer.
2875 * @param prio_in requested priority
2876 * @param cp the peer making the request
2877 * @return effective priority
2880 bound_priority (uint32_t prio_in,
2881 struct ConnectedPeer *cp)
2888 * Iterator over entries in the 'query_request_map' that
2889 * tries to see if we have the same request pending from
2890 * the same peer already.
2892 * @param cls closure (our 'struct CheckDuplicateRequestClosure')
2893 * @param key current key code (query, ignored, must match)
2894 * @param value value in the hash map (a 'struct PendingRequest'
2895 * that already exists)
2896 * @return GNUNET_YES if we should continue to
2897 * iterate (no match yet)
2898 * GNUNET_NO if not (match found).
2901 check_duplicate_request_peer (void *cls,
2902 const GNUNET_HashCode * key,
2905 struct CheckDuplicateRequestClosure *cdc = cls;
2906 struct PendingRequest *have = value;
2908 if (cdc->pr->target_pid == have->target_pid)
2918 * Handle P2P "GET" request.
2920 * @param cls closure, always NULL
2921 * @param other the other peer involved (sender or receiver, NULL
2922 * for loopback messages where we are both sender and receiver)
2923 * @param message the actual message
2924 * @param latency reported latency of the connection with 'other'
2925 * @param distance reported distance (DV) to 'other'
2926 * @return GNUNET_OK to keep the connection open,
2927 * GNUNET_SYSERR to close it (signal serious error)
2930 handle_p2p_get (void *cls,
2931 const struct GNUNET_PeerIdentity *other,
2932 const struct GNUNET_MessageHeader *message,
2933 struct GNUNET_TIME_Relative latency,
2936 struct PendingRequest *pr;
2937 struct ConnectedPeer *cp;
2938 struct ConnectedPeer *cps;
2939 struct CheckDuplicateRequestClosure cdc;
2940 struct GNUNET_TIME_Relative timeout;
2942 const struct GetMessage *gm;
2944 const GNUNET_HashCode *opt;
2947 uint32_t ttl_decrement;
2948 enum GNUNET_BLOCK_Type type;
2952 msize = ntohs(message->size);
2953 if (msize < sizeof (struct GetMessage))
2955 GNUNET_break_op (0);
2956 return GNUNET_SYSERR;
2958 gm = (const struct GetMessage*) message;
2959 type = ntohl (gm->type);
2962 case GNUNET_BLOCK_TYPE_ANY:
2963 case GNUNET_BLOCK_TYPE_DBLOCK:
2964 case GNUNET_BLOCK_TYPE_IBLOCK:
2965 case GNUNET_BLOCK_TYPE_KBLOCK:
2966 case GNUNET_BLOCK_TYPE_SBLOCK:
2969 GNUNET_break_op (0);
2970 return GNUNET_SYSERR;
2972 bm = ntohl (gm->hash_bitmap);
2980 if (msize < sizeof (struct GetMessage) + bits * sizeof (GNUNET_HashCode))
2982 GNUNET_break_op (0);
2983 return GNUNET_SYSERR;
2985 opt = (const GNUNET_HashCode*) &gm[1];
2986 bfsize = msize - sizeof (struct GetMessage) + bits * sizeof (GNUNET_HashCode);
2987 bm = ntohl (gm->hash_bitmap);
2988 if ( (0 != (bm & GET_MESSAGE_BIT_SKS_NAMESPACE)) &&
2989 (type != GNUNET_BLOCK_TYPE_SBLOCK) )
2991 GNUNET_break_op (0);
2992 return GNUNET_SYSERR;
2995 cps = GNUNET_CONTAINER_multihashmap_get (connected_peers,
2996 &other->hashPubKey);
2999 /* peer must have just disconnected */
3000 GNUNET_STATISTICS_update (stats,
3001 gettext_noop ("# requests dropped due to initiator not being connected"),
3004 return GNUNET_SYSERR;
3006 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
3007 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
3014 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
3015 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3016 "Failed to find RETURN-TO peer `%4s' in connection set. Dropping query.\n",
3017 GNUNET_i2s ((const struct GNUNET_PeerIdentity*) &opt[bits-1]));
3020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3021 "Failed to find peer `%4s' in connection set. Dropping query.\n",
3022 GNUNET_i2s (other));
3024 GNUNET_STATISTICS_update (stats,
3025 gettext_noop ("# requests dropped due to missing reverse route"),
3028 /* FIXME: try connect? */
3031 /* note that we can really only check load here since otherwise
3032 peers could find out that we are overloaded by not being
3033 disconnected after sending us a malformed query... */
3034 if (GNUNET_YES == test_load_too_high ())
3037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3038 "Dropping query from `%s', this peer is too busy.\n",
3039 GNUNET_i2s (other));
3041 GNUNET_STATISTICS_update (stats,
3042 gettext_noop ("# requests dropped due to high load"),
3049 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3050 "Received request for `%s' of type %u from peer `%4s' with flags %u\n",
3051 GNUNET_h2s (&gm->query),
3052 (unsigned int) type,
3056 have_ns = (0 != (bm & GET_MESSAGE_BIT_SKS_NAMESPACE));
3057 pr = GNUNET_malloc (sizeof (struct PendingRequest) +
3058 (have_ns ? sizeof(GNUNET_HashCode) : 0));
3060 pr->namespace = (GNUNET_HashCode*) &pr[1];
3062 pr->mingle = ntohl (gm->filter_mutator);
3063 if (0 != (bm & GET_MESSAGE_BIT_SKS_NAMESPACE))
3064 memcpy (&pr[1], &opt[bits++], sizeof (GNUNET_HashCode));
3065 if (0 != (bm & GET_MESSAGE_BIT_TRANSMIT_TO))
3066 pr->target_pid = GNUNET_PEER_intern ((const struct GNUNET_PeerIdentity*) &opt[bits++]);
3068 pr->anonymity_level = 1;
3069 pr->priority = bound_priority (ntohl (gm->priority), cps);
3070 pr->ttl = bound_ttl (ntohl (gm->ttl), pr->priority);
3071 pr->query = gm->query;
3072 /* decrement ttl (always) */
3073 ttl_decrement = 2 * TTL_DECREMENT +
3074 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
3076 if ( (pr->ttl < 0) &&
3077 (((int32_t)(pr->ttl - ttl_decrement)) > 0) )
3080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3081 "Dropping query from `%s' due to TTL underflow (%d - %u).\n",
3086 GNUNET_STATISTICS_update (stats,
3087 gettext_noop ("# requests dropped due TTL underflow"),
3090 /* integer underflow => drop (should be very rare)! */
3094 pr->ttl -= ttl_decrement;
3095 pr->start_time = GNUNET_TIME_absolute_get ();
3097 /* get bloom filter */
3100 pr->bf = GNUNET_CONTAINER_bloomfilter_init ((const char*) &opt[bits],
3103 pr->bf_size = bfsize;
3108 GNUNET_CONTAINER_multihashmap_get_multiple (query_request_map,
3110 &check_duplicate_request_peer,
3112 if (cdc.have != NULL)
3114 if (cdc.have->start_time.value + cdc.have->ttl >=
3115 pr->start_time.value + pr->ttl)
3117 /* existing request has higher TTL, drop new one! */
3118 cdc.have->priority += pr->priority;
3119 destroy_pending_request (pr);
3121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3122 "Have existing request with higher TTL, dropping new request.\n",
3123 GNUNET_i2s (other));
3125 GNUNET_STATISTICS_update (stats,
3126 gettext_noop ("# requests dropped due to higher-TTL request"),
3133 /* existing request has lower TTL, drop old one! */
3134 pr->priority += cdc.have->priority;
3135 /* Possible optimization: if we have applicable pending
3136 replies in 'cdc.have', we might want to move those over
3137 (this is a really rare special-case, so it is not clear
3138 that this would be worth it) */
3139 destroy_pending_request (cdc.have);
3140 /* keep processing 'pr'! */
3145 GNUNET_break (GNUNET_OK ==
3146 GNUNET_CONTAINER_multihashmap_put (query_request_map,
3149 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
3150 GNUNET_break (GNUNET_OK ==
3151 GNUNET_CONTAINER_multihashmap_put (peer_request_map,
3154 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
3156 pr->hnode = GNUNET_CONTAINER_heap_insert (requests_by_expiration_heap,
3158 pr->start_time.value + pr->ttl);
3160 GNUNET_STATISTICS_update (stats,
3161 gettext_noop ("# P2P searches received"),
3164 GNUNET_STATISTICS_update (stats,
3165 gettext_noop ("# P2P searches active"),
3169 /* calculate change in traffic preference */
3170 preference = (double) pr->priority;
3171 if (preference < QUERY_BANDWIDTH_VALUE)
3172 preference = QUERY_BANDWIDTH_VALUE;
3173 cps->inc_preference += preference;
3175 /* process locally */
3176 if (type == GNUNET_BLOCK_TYPE_DBLOCK)
3177 type = GNUNET_BLOCK_TYPE_ANY; /* to get on-demand as well */
3178 timeout = GNUNET_TIME_relative_multiply (BASIC_DATASTORE_REQUEST_DELAY,
3179 (pr->priority + 1));
3180 pr->qe = GNUNET_DATASTORE_get (dsh,
3184 MAX_DATASTORE_QUEUE,
3186 &process_local_reply,
3189 /* Are multiple results possible? If so, start processing remotely now! */
3192 case GNUNET_BLOCK_TYPE_DBLOCK:
3193 case GNUNET_BLOCK_TYPE_IBLOCK:
3194 /* only one result, wait for datastore */
3197 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
3198 pr->task = GNUNET_SCHEDULER_add_now (sched,
3199 &forward_request_task,
3203 /* make sure we don't track too many requests */
3204 if (GNUNET_CONTAINER_heap_get_size (requests_by_expiration_heap) > max_pending_requests)
3206 pr = GNUNET_CONTAINER_heap_peek (requests_by_expiration_heap);
3207 destroy_pending_request (pr);
3213 /* **************************** CS GET Handling ************************ */
3217 * Handle START_SEARCH-message (search request from client).
3219 * @param cls closure
3220 * @param client identification of the client
3221 * @param message the actual message
3224 handle_start_search (void *cls,
3225 struct GNUNET_SERVER_Client *client,
3226 const struct GNUNET_MessageHeader *message)
3228 static GNUNET_HashCode all_zeros;
3229 const struct SearchMessage *sm;
3230 struct ClientList *cl;
3231 struct ClientRequestList *crl;
3232 struct PendingRequest *pr;
3235 enum GNUNET_BLOCK_Type type;
3237 msize = ntohs (message->size);
3238 if ( (msize < sizeof (struct SearchMessage)) ||
3239 (0 != (msize - sizeof (struct SearchMessage)) % sizeof (GNUNET_HashCode)) )
3242 GNUNET_SERVER_receive_done (client,
3246 GNUNET_STATISTICS_update (stats,
3247 gettext_noop ("# client searches received"),
3250 sc = (msize - sizeof (struct SearchMessage)) / sizeof (GNUNET_HashCode);
3251 sm = (const struct SearchMessage*) message;
3252 type = ntohl (sm->type);
3254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3255 "Received request for `%s' of type %u from local client\n",
3256 GNUNET_h2s (&sm->query),
3257 (unsigned int) type);
3261 case GNUNET_BLOCK_TYPE_ANY:
3262 case GNUNET_BLOCK_TYPE_DBLOCK:
3263 case GNUNET_BLOCK_TYPE_IBLOCK:
3264 case GNUNET_BLOCK_TYPE_KBLOCK:
3265 case GNUNET_BLOCK_TYPE_SBLOCK:
3266 case GNUNET_BLOCK_TYPE_NBLOCK:
3270 GNUNET_SERVER_receive_done (client,
3276 while ( (cl != NULL) &&
3277 (cl->client != client) )
3281 cl = GNUNET_malloc (sizeof (struct ClientList));
3282 cl->client = client;
3283 GNUNET_SERVER_client_keep (client);
3284 cl->next = client_list;
3287 /* detect duplicate KBLOCK requests */
3288 if ( (type == GNUNET_BLOCK_TYPE_KBLOCK) ||
3289 (type == GNUNET_BLOCK_TYPE_NBLOCK) ||
3290 (type == GNUNET_BLOCK_TYPE_ANY) )
3293 while ( (crl != NULL) &&
3294 ( (0 != memcmp (&crl->req->query,
3296 sizeof (GNUNET_HashCode))) ||
3297 (crl->req->type != type) ) )
3302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3303 "Have existing request, merging content-seen lists.\n");
3306 /* Duplicate request (used to send long list of
3307 known/blocked results); merge 'pr->replies_seen'
3308 and update bloom filter */
3309 GNUNET_array_grow (pr->replies_seen,
3310 pr->replies_seen_size,
3311 pr->replies_seen_off + sc);
3312 memcpy (&pr->replies_seen[pr->replies_seen_off],
3314 sc * sizeof (GNUNET_HashCode));
3315 pr->replies_seen_off += sc;
3316 refresh_bloomfilter (pr);
3317 GNUNET_STATISTICS_update (stats,
3318 gettext_noop ("# client searches updated (merged content seen list)"),
3321 GNUNET_SERVER_receive_done (client,
3326 GNUNET_STATISTICS_update (stats,
3327 gettext_noop ("# client searches active"),
3330 pr = GNUNET_malloc (sizeof (struct PendingRequest) +
3331 ((type == GNUNET_BLOCK_TYPE_SBLOCK) ? sizeof(GNUNET_HashCode) : 0));
3332 crl = GNUNET_malloc (sizeof (struct ClientRequestList));
3333 memset (crl, 0, sizeof (struct ClientRequestList));
3334 crl->client_list = cl;
3335 GNUNET_CONTAINER_DLL_insert (cl->rl_head,
3340 pr->client_request_list = crl;
3341 GNUNET_array_grow (pr->replies_seen,
3342 pr->replies_seen_size,
3344 memcpy (pr->replies_seen,
3346 sc * sizeof (GNUNET_HashCode));
3347 pr->replies_seen_off = sc;
3348 pr->anonymity_level = ntohl (sm->anonymity_level);
3349 refresh_bloomfilter (pr);
3350 pr->query = sm->query;
3351 if (0 == (1 & ntohl (sm->options)))
3352 pr->local_only = GNUNET_NO;
3354 pr->local_only = GNUNET_YES;
3357 case GNUNET_BLOCK_TYPE_DBLOCK:
3358 case GNUNET_BLOCK_TYPE_IBLOCK:
3359 if (0 != memcmp (&sm->target,
3361 sizeof (GNUNET_HashCode)))
3362 pr->target_pid = GNUNET_PEER_intern ((const struct GNUNET_PeerIdentity*) &sm->target);
3364 case GNUNET_BLOCK_TYPE_SBLOCK:
3365 pr->namespace = (GNUNET_HashCode*) &pr[1];
3366 memcpy (&pr[1], &sm->target, sizeof (GNUNET_HashCode));
3371 GNUNET_break (GNUNET_OK ==
3372 GNUNET_CONTAINER_multihashmap_put (query_request_map,
3375 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
3376 if (type == GNUNET_BLOCK_TYPE_DBLOCK)
3377 type = GNUNET_BLOCK_TYPE_ANY; /* get on-demand blocks too! */
3378 pr->qe = GNUNET_DATASTORE_get (dsh,
3382 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
3383 &process_local_reply,
3388 /* **************************** Startup ************************ */
3392 * List of handlers for P2P messages
3393 * that we care about.
3395 static struct GNUNET_CORE_MessageHandler p2p_handlers[] =
3398 GNUNET_MESSAGE_TYPE_FS_GET, 0 },
3400 GNUNET_MESSAGE_TYPE_FS_PUT, 0 },
3406 * List of handlers for the messages understood by this
3409 static struct GNUNET_SERVER_MessageHandler handlers[] = {
3410 {&GNUNET_FS_handle_index_start, NULL,
3411 GNUNET_MESSAGE_TYPE_FS_INDEX_START, 0},
3412 {&GNUNET_FS_handle_index_list_get, NULL,
3413 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_GET, sizeof(struct GNUNET_MessageHeader) },
3414 {&GNUNET_FS_handle_unindex, NULL, GNUNET_MESSAGE_TYPE_FS_UNINDEX,
3415 sizeof (struct UnindexMessage) },
3416 {&handle_start_search, NULL, GNUNET_MESSAGE_TYPE_FS_START_SEARCH,
3423 * Process fs requests.
3425 * @param s scheduler to use
3426 * @param server the initialized server
3427 * @param c configuration to use
3430 main_init (struct GNUNET_SCHEDULER_Handle *s,
3431 struct GNUNET_SERVER_Handle *server,
3432 const struct GNUNET_CONFIGURATION_Handle *c)
3436 stats = GNUNET_STATISTICS_create (sched, "fs", cfg);
3437 min_migration_delay = GNUNET_TIME_UNIT_SECONDS; // FIXME: get from config
3438 connected_peers = GNUNET_CONTAINER_multihashmap_create (128); // FIXME: get size from config
3439 query_request_map = GNUNET_CONTAINER_multihashmap_create (128); // FIXME: get size from config
3440 peer_request_map = GNUNET_CONTAINER_multihashmap_create (128); // FIXME: get size from config
3441 requests_by_expiration_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
3442 core = GNUNET_CORE_connect (sched,
3444 GNUNET_TIME_UNIT_FOREVER_REL,
3447 &peer_connect_handler,
3448 &peer_disconnect_handler,
3454 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3455 _("Failed to connect to `%s' service.\n"),
3457 GNUNET_CONTAINER_multihashmap_destroy (connected_peers);
3458 connected_peers = NULL;
3459 GNUNET_CONTAINER_multihashmap_destroy (query_request_map);
3460 query_request_map = NULL;
3461 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
3462 requests_by_expiration_heap = NULL;
3463 GNUNET_CONTAINER_multihashmap_destroy (peer_request_map);
3464 peer_request_map = NULL;
3467 GNUNET_DATASTORE_disconnect (dsh, GNUNET_NO);
3470 return GNUNET_SYSERR;
3472 /* FIXME: distinguish between sending and storing in options? */
3473 if (active_migration)
3475 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3476 _("Content migration is enabled, will start to gather data\n"));
3477 consider_migration_gathering ();
3479 GNUNET_SERVER_disconnect_notify (server,
3480 &handle_client_disconnect,
3482 GNUNET_SERVER_add_handlers (server, handlers);
3483 GNUNET_SCHEDULER_add_delayed (sched,
3484 GNUNET_TIME_UNIT_FOREVER_REL,
3492 * Process fs requests.
3494 * @param cls closure
3495 * @param sched scheduler to use
3496 * @param server the initialized server
3497 * @param cfg configuration to use
3501 struct GNUNET_SCHEDULER_Handle *sched,
3502 struct GNUNET_SERVER_Handle *server,
3503 const struct GNUNET_CONFIGURATION_Handle *cfg)
3505 active_migration = GNUNET_CONFIGURATION_get_value_yesno (cfg,
3508 dsh = GNUNET_DATASTORE_connect (cfg,
3512 GNUNET_SCHEDULER_shutdown (sched);
3515 if ( (GNUNET_OK != GNUNET_FS_indexing_init (sched, cfg, dsh)) ||
3516 (GNUNET_OK != main_init (sched, server, cfg)) )
3518 GNUNET_SCHEDULER_shutdown (sched);
3519 GNUNET_DATASTORE_disconnect (dsh, GNUNET_NO);
3527 * The main function for the fs service.
3529 * @param argc number of arguments from the command line
3530 * @param argv command line arguments
3531 * @return 0 ok, 1 on error
3534 main (int argc, char *const *argv)
3536 return (GNUNET_OK ==
3537 GNUNET_SERVICE_run (argc,
3540 GNUNET_SERVICE_OPTION_NONE,
3541 &run, NULL)) ? 0 : 1;
3544 /* end of gnunet-service-fs.c */