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 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 fs/gnunet-service-fs.c
23 * @brief gnunet anonymity protocol implementation
24 * @author Christian Grothoff
27 * - introduce random latency in processing
32 #include "gnunet_constants.h"
33 #include "gnunet_core_service.h"
34 #include "gnunet_dht_service.h"
35 #include "gnunet_datastore_service.h"
36 #include "gnunet_load_lib.h"
37 #include "gnunet_peer_lib.h"
38 #include "gnunet_protocols.h"
39 #include "gnunet_signatures.h"
40 #include "gnunet_statistics_service.h"
41 #include "gnunet_util_lib.h"
42 #include "gnunet-service-fs_indexing.h"
45 #define DEBUG_FS GNUNET_NO
48 * Should we introduce random latency in processing? Required for proper
49 * implementation of GAP, but can be disabled for performance evaluation of
50 * the basic routing algorithm.
52 #define SUPPORT_DELAYS GNUNET_NO
55 * Maximum number of outgoing messages we queue per peer.
57 #define MAX_QUEUE_PER_PEER 16
60 * Size for the hash map for DHT requests from the FS
61 * service. Should be about the number of concurrent
62 * DHT requests we plan to make.
64 #define FS_DHT_HT_SIZE 1024
67 * How often do we flush trust values to disk?
69 #define TRUST_FLUSH_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
72 * How often do we at most PUT content into the DHT?
74 #define MAX_DHT_PUT_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
77 * Inverse of the probability that we will submit the same query
78 * to the same peer again. If the same peer already got the query
79 * repeatedly recently, the probability is multiplied by the inverse
80 * of this number each time. Note that we only try about every TTL_DECREMENT/2
81 * plus MAX_CORK_DELAY (so roughly every 3.5s).
83 #define RETRY_PROBABILITY_INV 3
86 * What is the maximum delay for a P2P FS message (in our interaction
87 * with core)? FS-internal delays are another story. The value is
88 * chosen based on the 32k block size. Given that peers typcially
89 * have at least 1 kb/s bandwidth, 45s waits give us a chance to
90 * transmit one message even to the lowest-bandwidth peers.
92 #define MAX_TRANSMIT_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 45)
95 * Maximum number of requests (from other peers) that we're
96 * willing to have pending at any given point in time.
98 static unsigned long long max_pending_requests = (32 * 1024);
102 * Information we keep for each pending reply. The
103 * actual message follows at the end of this struct.
105 struct PendingMessage;
108 * Function called upon completion of a transmission.
111 * @param pid ID of receiving peer, 0 on transmission error
113 typedef void (*TransmissionContinuation)(void * cls,
114 GNUNET_PEER_Id tpid);
118 * Information we keep for each pending message (GET/PUT). The
119 * actual message follows at the end of this struct.
121 struct PendingMessage
124 * This is a doubly-linked list of messages to the same peer.
126 struct PendingMessage *next;
129 * This is a doubly-linked list of messages to the same peer.
131 struct PendingMessage *prev;
134 * Entry in pending message list for this pending message.
136 struct PendingMessageList *pml;
139 * Function to call immediately once we have transmitted this
142 TransmissionContinuation cont;
150 * Do not transmit this pending message until this deadline.
152 struct GNUNET_TIME_Absolute delay_until;
155 * Size of the reply; actual reply message follows
156 * at the end of this struct.
161 * How important is this message for us?
169 * Information about a peer that we are connected to.
170 * We track data that is useful for determining which
171 * peers should receive our requests. We also keep
172 * a list of messages to transmit to this peer.
178 * List of the last clients for which this peer successfully
181 struct GNUNET_SERVER_Client *last_client_replies[CS2P_SUCCESS_LIST_SIZE];
184 * List of the last PIDs for which
185 * this peer successfully answered a query;
186 * We use 0 to indicate no successful reply.
188 GNUNET_PEER_Id last_p2p_replies[P2P_SUCCESS_LIST_SIZE];
191 * Average delay between sending the peer a request and
192 * getting a reply (only calculated over the requests for
193 * which we actually got a reply). Calculated
194 * as a moving average: new_delay = ((n-1)*last_delay+curr_delay) / n
196 struct GNUNET_TIME_Relative avg_delay;
199 * Point in time until which this peer does not want us to migrate content
202 struct GNUNET_TIME_Absolute migration_blocked;
205 * Time until when we blocked this peer from migrating
208 struct GNUNET_TIME_Absolute last_migration_block;
211 * Handle for an active request for transmission to this
214 struct GNUNET_CORE_TransmitHandle *cth;
217 * Messages (replies, queries, content migration) we would like to
218 * send to this peer in the near future. Sorted by priority, head.
220 struct PendingMessage *pending_messages_head;
223 * Messages (replies, queries, content migration) we would like to
224 * send to this peer in the near future. Sorted by priority, tail.
226 struct PendingMessage *pending_messages_tail;
229 * How long does it typically take for us to transmit a message
230 * to this peer? (delay between the request being issued and
231 * the callback being invoked).
233 struct GNUNET_LOAD_Value *transmission_delay;
236 * Time when the last transmission request was issued.
238 struct GNUNET_TIME_Absolute last_transmission_request_start;
241 * ID of delay task for scheduling transmission.
243 GNUNET_SCHEDULER_TaskIdentifier delayed_transmission_request_task;
246 * Average priority of successful replies. Calculated
247 * as a moving average: new_avg = ((n-1)*last_avg+curr_prio) / n
252 * Increase in traffic preference still to be submitted
253 * to the core service for this peer.
255 uint64_t inc_preference;
258 * Trust rating for this peer
263 * Trust rating for this peer on disk.
268 * The peer's identity.
273 * Size of the linked list of 'pending_messages'.
275 unsigned int pending_requests;
278 * Which offset in "last_p2p_replies" will be updated next?
279 * (we go round-robin).
281 unsigned int last_p2p_replies_woff;
284 * Which offset in "last_client_replies" will be updated next?
285 * (we go round-robin).
287 unsigned int last_client_replies_woff;
293 * Information we keep for each pending request. We should try to
294 * keep this struct as small as possible since its memory consumption
295 * is key to how many requests we can have pending at once.
297 struct PendingRequest;
301 * Doubly-linked list of requests we are performing
302 * on behalf of the same client.
304 struct ClientRequestList
308 * This is a doubly-linked list.
310 struct ClientRequestList *next;
313 * This is a doubly-linked list.
315 struct ClientRequestList *prev;
318 * Request this entry represents.
320 struct PendingRequest *req;
323 * Client list this request belongs to.
325 struct ClientList *client_list;
331 * Replies to be transmitted to the client. The actual
332 * response message is allocated after this struct.
334 struct ClientResponseMessage
337 * This is a doubly-linked list.
339 struct ClientResponseMessage *next;
342 * This is a doubly-linked list.
344 struct ClientResponseMessage *prev;
347 * Client list entry this response belongs to.
349 struct ClientList *client_list;
352 * Number of bytes in the response.
359 * Linked list of clients we are performing requests
365 * This is a linked list.
367 struct ClientList *next;
370 * ID of a client making a request, NULL if this entry is for a
373 struct GNUNET_SERVER_Client *client;
376 * Head of list of requests performed on behalf
377 * of this client right now.
379 struct ClientRequestList *rl_head;
382 * Tail of list of requests performed on behalf
383 * of this client right now.
385 struct ClientRequestList *rl_tail;
388 * Head of linked list of responses.
390 struct ClientResponseMessage *res_head;
393 * Tail of linked list of responses.
395 struct ClientResponseMessage *res_tail;
398 * Context for sending replies.
400 struct GNUNET_CONNECTION_TransmitHandle *th;
406 * Doubly-linked list of messages we are performing
407 * due to a pending request.
409 struct PendingMessageList
413 * This is a doubly-linked list of messages on behalf of the same request.
415 struct PendingMessageList *next;
418 * This is a doubly-linked list of messages on behalf of the same request.
420 struct PendingMessageList *prev;
423 * Message this entry represents.
425 struct PendingMessage *pm;
428 * Request this entry belongs to.
430 struct PendingRequest *req;
433 * Peer this message is targeted for.
435 struct ConnectedPeer *target;
441 * Information we keep for each pending request. We should try to
442 * keep this struct as small as possible since its memory consumption
443 * is key to how many requests we can have pending at once.
445 struct PendingRequest
449 * If this request was made by a client, this is our entry in the
450 * client request list; otherwise NULL.
452 struct ClientRequestList *client_request_list;
455 * Entry of peer responsible for this entry (if this request
456 * was made by a peer).
458 struct ConnectedPeer *cp;
461 * If this is a namespace query, pointer to the hash of the public
462 * key of the namespace; otherwise NULL. Pointer will be to the
463 * end of this struct (so no need to free it).
465 const GNUNET_HashCode *namespace;
468 * Bloomfilter we use to filter out replies that we don't care about
469 * (anymore). NULL as long as we are interested in all replies.
471 struct GNUNET_CONTAINER_BloomFilter *bf;
474 * Context of our GNUNET_CORE_peer_change_preference call.
476 struct GNUNET_CORE_InformationRequestContext *irc;
479 * Reference to DHT get operation for this request (or NULL).
481 struct GNUNET_DHT_GetHandle *dht_get;
484 * Hash code of all replies that we have seen so far (only valid
485 * if client is not NULL since we only track replies like this for
488 GNUNET_HashCode *replies_seen;
491 * Node in the heap representing this entry; NULL
492 * if we have no heap node.
494 struct GNUNET_CONTAINER_HeapNode *hnode;
497 * Head of list of messages being performed on behalf of this
500 struct PendingMessageList *pending_head;
503 * Tail of list of messages being performed on behalf of this
506 struct PendingMessageList *pending_tail;
509 * When did we first see this request (form this peer), or, if our
510 * client is initiating, when did we last initiate a search?
512 struct GNUNET_TIME_Absolute start_time;
515 * The query that this request is for.
517 GNUNET_HashCode query;
520 * The task responsible for transmitting queries
523 GNUNET_SCHEDULER_TaskIdentifier task;
526 * (Interned) Peer identifier that identifies a preferred target
529 GNUNET_PEER_Id target_pid;
532 * (Interned) Peer identifiers of peers that have already
533 * received our query for this content.
535 GNUNET_PEER_Id *used_pids;
538 * Our entry in the queue (non-NULL while we wait for our
539 * turn to interact with the local database).
541 struct GNUNET_DATASTORE_QueueEntry *qe;
544 * Size of the 'bf' (in bytes).
549 * Desired anonymity level; only valid for requests from a local client.
551 uint32_t anonymity_level;
554 * How many entries in "used_pids" are actually valid?
556 unsigned int used_pids_off;
559 * How long is the "used_pids" array?
561 unsigned int used_pids_size;
564 * Number of results found for this request.
566 unsigned int results_found;
569 * How many entries in "replies_seen" are actually valid?
571 unsigned int replies_seen_off;
574 * How long is the "replies_seen" array?
576 unsigned int replies_seen_size;
579 * Priority with which this request was made. If one of our clients
580 * made the request, then this is the current priority that we are
581 * using when initiating the request. This value is used when
582 * we decide to reward other peers with trust for providing a reply.
587 * Priority points left for us to spend when forwarding this request
590 uint32_t remaining_priority;
593 * Number to mingle hashes for bloom-filter tests with.
598 * TTL with which we saw this request (or, if we initiated, TTL that
599 * we used for the request).
604 * Type of the content that this request is for.
606 enum GNUNET_BLOCK_Type type;
609 * Remove this request after transmission of the current response.
614 * GNUNET_YES if we should not forward this request to other peers.
619 * GNUNET_YES if we should not forward this request to other peers.
627 * Block that is ready for migration to other peers. Actual data is at the end of the block.
629 struct MigrationReadyBlock
633 * This is a doubly-linked list.
635 struct MigrationReadyBlock *next;
638 * This is a doubly-linked list.
640 struct MigrationReadyBlock *prev;
643 * Query for the block.
645 GNUNET_HashCode query;
648 * When does this block expire?
650 struct GNUNET_TIME_Absolute expiration;
653 * Peers we would consider forwarding this
654 * block to. Zero for empty entries.
656 GNUNET_PEER_Id target_list[MIGRATION_LIST_SIZE];
664 * Number of targets already used.
666 unsigned int used_targets;
671 enum GNUNET_BLOCK_Type type;
676 * Our connection to the datastore.
678 static struct GNUNET_DATASTORE_Handle *dsh;
683 static struct GNUNET_BLOCK_Context *block_ctx;
686 * Our block configuration.
688 static struct GNUNET_CONFIGURATION_Handle *block_cfg;
693 static struct GNUNET_SCHEDULER_Handle *sched;
698 static const struct GNUNET_CONFIGURATION_Handle *cfg;
701 * Map of peer identifiers to "struct ConnectedPeer" (for that peer).
703 static struct GNUNET_CONTAINER_MultiHashMap *connected_peers;
706 * Map of peer identifiers to "struct PendingRequest" (for that peer).
708 static struct GNUNET_CONTAINER_MultiHashMap *peer_request_map;
711 * Map of query identifiers to "struct PendingRequest" (for that query).
713 static struct GNUNET_CONTAINER_MultiHashMap *query_request_map;
716 * Heap with the request that will expire next at the top. Contains
717 * pointers of type "struct PendingRequest*"; these will *also* be
718 * aliased from the "requests_by_peer" data structures and the
719 * "requests_by_query" table. Note that requests from our clients
720 * don't expire and are thus NOT in the "requests_by_expiration"
721 * (or the "requests_by_peer" tables).
723 static struct GNUNET_CONTAINER_Heap *requests_by_expiration_heap;
726 * Handle for reporting statistics.
728 static struct GNUNET_STATISTICS_Handle *stats;
731 * Linked list of clients we are currently processing requests for.
733 static struct ClientList *client_list;
736 * Pointer to handle to the core service (points to NULL until we've
739 static struct GNUNET_CORE_Handle *core;
742 * Head of linked list of blocks that can be migrated.
744 static struct MigrationReadyBlock *mig_head;
747 * Tail of linked list of blocks that can be migrated.
749 static struct MigrationReadyBlock *mig_tail;
752 * Request to datastore for migration (or NULL).
754 static struct GNUNET_DATASTORE_QueueEntry *mig_qe;
757 * Request to datastore for DHT PUTs (or NULL).
759 static struct GNUNET_DATASTORE_QueueEntry *dht_qe;
762 * Type we will request for the next DHT PUT round from the datastore.
764 static enum GNUNET_BLOCK_Type dht_put_type = GNUNET_BLOCK_TYPE_FS_KBLOCK;
767 * Where do we store trust information?
769 static char *trustDirectory;
772 * ID of task that collects blocks for migration.
774 static GNUNET_SCHEDULER_TaskIdentifier mig_task;
777 * ID of task that collects blocks for DHT PUTs.
779 static GNUNET_SCHEDULER_TaskIdentifier dht_task;
782 * What is the maximum frequency at which we are allowed to
783 * poll the datastore for migration content?
785 static struct GNUNET_TIME_Relative min_migration_delay;
788 * Handle for DHT operations.
790 static struct GNUNET_DHT_Handle *dht_handle;
793 * Size of the doubly-linked list of migration blocks.
795 static unsigned int mig_size;
798 * Are we allowed to migrate content to this peer.
800 static int active_migration;
803 * How many entires with zero anonymity do we currently estimate
804 * to have in the database?
806 static unsigned int zero_anonymity_count_estimate;
809 * Typical priorities we're seeing from other peers right now. Since
810 * most priorities will be zero, this value is the weighted average of
811 * non-zero priorities seen "recently". In order to ensure that new
812 * values do not dramatically change the ratio, values are first
813 * "capped" to a reasonable range (+N of the current value) and then
814 * averaged into the existing value by a ratio of 1:N. Hence
815 * receiving the largest possible priority can still only raise our
816 * "current_priorities" by at most 1.
818 static double current_priorities;
821 * Datastore 'GET' load tracking.
823 static struct GNUNET_LOAD_Value *datastore_get_load;
826 * Datastore 'PUT' load tracking.
828 static struct GNUNET_LOAD_Value *datastore_put_load;
831 * How long do requests typically stay in the routing table?
833 static struct GNUNET_LOAD_Value *rt_entry_lifetime;
836 * We've just now completed a datastore request. Update our
837 * datastore load calculations.
839 * @param start time when the datastore request was issued
842 update_datastore_delays (struct GNUNET_TIME_Absolute start)
844 struct GNUNET_TIME_Relative delay;
846 delay = GNUNET_TIME_absolute_get_duration (start);
847 GNUNET_LOAD_update (datastore_get_load,
853 * Get the filename under which we would store the GNUNET_HELLO_Message
854 * for the given host and protocol.
855 * @return filename of the form DIRECTORY/HOSTID
858 get_trust_filename (const struct GNUNET_PeerIdentity *id)
860 struct GNUNET_CRYPTO_HashAsciiEncoded fil;
863 GNUNET_CRYPTO_hash_to_enc (&id->hashPubKey, &fil);
864 GNUNET_asprintf (&fn, "%s%s%s", trustDirectory, DIR_SEPARATOR_STR, &fil);
871 * Transmit messages by copying it to the target buffer
872 * "buf". "buf" will be NULL and "size" zero if the socket was closed
873 * for writing in the meantime. In that case, do nothing
874 * (the disconnect or shutdown handler will take care of the rest).
875 * If we were able to transmit messages and there are still more
876 * pending, ask core again for further calls to this function.
878 * @param cls closure, pointer to the 'struct ConnectedPeer*'
879 * @param size number of bytes available in buf
880 * @param buf where the callee should write the message
881 * @return number of bytes written to buf
884 transmit_to_peer (void *cls,
885 size_t size, void *buf);
888 /* ******************* clean up functions ************************ */
891 * Delete the given migration block.
893 * @param mb block to delete
896 delete_migration_block (struct MigrationReadyBlock *mb)
898 GNUNET_CONTAINER_DLL_remove (mig_head,
901 GNUNET_PEER_decrement_rcs (mb->target_list,
902 MIGRATION_LIST_SIZE);
909 * Compare the distance of two peers to a key.
912 * @param p1 first peer
913 * @param p2 second peer
914 * @return GNUNET_YES if P1 is closer to key than P2
917 is_closer (const GNUNET_HashCode *key,
918 const struct GNUNET_PeerIdentity *p1,
919 const struct GNUNET_PeerIdentity *p2)
921 return GNUNET_CRYPTO_hash_xorcmp (&p1->hashPubKey,
928 * Consider migrating content to a given peer.
930 * @param cls 'struct MigrationReadyBlock*' to select
931 * targets for (or NULL for none)
932 * @param key ID of the peer
933 * @param value 'struct ConnectedPeer' of the peer
934 * @return GNUNET_YES (always continue iteration)
937 consider_migration (void *cls,
938 const GNUNET_HashCode *key,
941 struct MigrationReadyBlock *mb = cls;
942 struct ConnectedPeer *cp = value;
943 struct MigrationReadyBlock *pos;
944 struct GNUNET_PeerIdentity cppid;
945 struct GNUNET_PeerIdentity otherpid;
946 struct GNUNET_PeerIdentity worstpid;
951 /* consider 'cp' as a migration target for mb */
952 if (GNUNET_TIME_absolute_get_remaining (cp->migration_blocked).value > 0)
953 return GNUNET_YES; /* peer has requested no migration! */
956 GNUNET_PEER_resolve (cp->pid,
958 repl = MIGRATION_LIST_SIZE;
959 for (i=0;i<MIGRATION_LIST_SIZE;i++)
961 if (mb->target_list[i] == 0)
963 mb->target_list[i] = cp->pid;
964 GNUNET_PEER_change_rc (mb->target_list[i], 1);
965 repl = MIGRATION_LIST_SIZE;
968 GNUNET_PEER_resolve (mb->target_list[i],
970 if ( (repl == MIGRATION_LIST_SIZE) &&
971 is_closer (&mb->query,
978 else if ( (repl != MIGRATION_LIST_SIZE) &&
979 (is_closer (&mb->query,
987 if (repl != MIGRATION_LIST_SIZE)
989 GNUNET_PEER_change_rc (mb->target_list[repl], -1);
990 mb->target_list[repl] = cp->pid;
991 GNUNET_PEER_change_rc (mb->target_list[repl], 1);
995 /* consider scheduling transmission to cp for content migration */
1002 for (i=0;i<MIGRATION_LIST_SIZE;i++)
1004 if (cp->pid == pos->target_list[i])
1009 msize = GNUNET_MIN (msize,
1017 return GNUNET_YES; /* no content available */
1019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1020 "Trying to migrate at least %u bytes to peer `%s'\n",
1024 if (cp->delayed_transmission_request_task != GNUNET_SCHEDULER_NO_TASK)
1026 GNUNET_SCHEDULER_cancel (sched, cp->delayed_transmission_request_task);
1027 cp->delayed_transmission_request_task = GNUNET_SCHEDULER_NO_TASK;
1030 = GNUNET_CORE_notify_transmit_ready (core,
1031 0, GNUNET_TIME_UNIT_FOREVER_REL,
1032 (const struct GNUNET_PeerIdentity*) key,
1033 msize + sizeof (struct PutMessage),
1041 * Task that is run periodically to obtain blocks for content
1045 * @param tc scheduler context (also unused)
1048 gather_migration_blocks (void *cls,
1049 const struct GNUNET_SCHEDULER_TaskContext *tc);
1055 * Task that is run periodically to obtain blocks for DHT PUTs.
1057 * @param cls type of blocks to gather
1058 * @param tc scheduler context (unused)
1061 gather_dht_put_blocks (void *cls,
1062 const struct GNUNET_SCHEDULER_TaskContext *tc);
1066 * If the migration task is not currently running, consider
1067 * (re)scheduling it with the appropriate delay.
1070 consider_migration_gathering ()
1072 struct GNUNET_TIME_Relative delay;
1078 if (mig_task != GNUNET_SCHEDULER_NO_TASK)
1080 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
1082 delay = GNUNET_TIME_relative_divide (delay,
1083 MAX_MIGRATION_QUEUE);
1084 delay = GNUNET_TIME_relative_max (delay,
1085 min_migration_delay);
1086 mig_task = GNUNET_SCHEDULER_add_delayed (sched,
1088 &gather_migration_blocks,
1094 * If the DHT PUT gathering task is not currently running, consider
1095 * (re)scheduling it with the appropriate delay.
1098 consider_dht_put_gathering (void *cls)
1100 struct GNUNET_TIME_Relative delay;
1106 if (dht_task != GNUNET_SCHEDULER_NO_TASK)
1108 if (zero_anonymity_count_estimate > 0)
1110 delay = GNUNET_TIME_relative_divide (GNUNET_DHT_DEFAULT_REPUBLISH_FREQUENCY,
1111 zero_anonymity_count_estimate);
1112 delay = GNUNET_TIME_relative_min (delay,
1117 /* if we have NO zero-anonymity content yet, wait 5 minutes for some to
1118 (hopefully) appear */
1119 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5);
1121 dht_task = GNUNET_SCHEDULER_add_delayed (sched,
1123 &gather_dht_put_blocks,
1129 * Process content offered for migration.
1131 * @param cls closure
1132 * @param key key for the content
1133 * @param size number of bytes in data
1134 * @param data content stored
1135 * @param type type of the content
1136 * @param priority priority of the content
1137 * @param anonymity anonymity-level for the content
1138 * @param expiration expiration time for the content
1139 * @param uid unique identifier for the datum;
1140 * maybe 0 if no unique identifier is available
1143 process_migration_content (void *cls,
1144 const GNUNET_HashCode * key,
1147 enum GNUNET_BLOCK_Type type,
1150 struct GNUNET_TIME_Absolute
1151 expiration, uint64_t uid)
1153 struct MigrationReadyBlock *mb;
1158 if (mig_size < MAX_MIGRATION_QUEUE)
1159 consider_migration_gathering ();
1162 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
1165 GNUNET_FS_handle_on_demand_block (key, size, data,
1166 type, priority, anonymity,
1168 &process_migration_content,
1171 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
1176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1177 "Retrieved block `%s' of type %u for migration\n",
1181 mb = GNUNET_malloc (sizeof (struct MigrationReadyBlock) + size);
1183 mb->expiration = expiration;
1186 memcpy (&mb[1], data, size);
1187 GNUNET_CONTAINER_DLL_insert_after (mig_head,
1192 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
1193 &consider_migration,
1195 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
1200 * Function called upon completion of the DHT PUT operation.
1203 dht_put_continuation (void *cls,
1204 const struct GNUNET_SCHEDULER_TaskContext *tc)
1206 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
1211 * Store content in DHT.
1213 * @param cls closure
1214 * @param key key for the content
1215 * @param size number of bytes in data
1216 * @param data content stored
1217 * @param type type of the content
1218 * @param priority priority of the content
1219 * @param anonymity anonymity-level for the content
1220 * @param expiration expiration time for the content
1221 * @param uid unique identifier for the datum;
1222 * maybe 0 if no unique identifier is available
1225 process_dht_put_content (void *cls,
1226 const GNUNET_HashCode * key,
1229 enum GNUNET_BLOCK_Type type,
1232 struct GNUNET_TIME_Absolute
1233 expiration, uint64_t uid)
1235 static unsigned int counter;
1236 static GNUNET_HashCode last_vhash;
1237 static GNUNET_HashCode vhash;
1242 consider_dht_put_gathering (cls);
1245 /* slightly funky code to estimate the total number of values with zero
1246 anonymity from the maximum observed length of a monotonically increasing
1247 sequence of hashes over the contents */
1248 GNUNET_CRYPTO_hash (data, size, &vhash);
1249 if (GNUNET_CRYPTO_hash_cmp (&vhash, &last_vhash) <= 0)
1251 if (zero_anonymity_count_estimate > 0)
1252 zero_anonymity_count_estimate /= 2;
1258 if (zero_anonymity_count_estimate < (1 << counter))
1259 zero_anonymity_count_estimate = (1 << counter);
1261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1262 "Retrieved block `%s' of type %u for DHT PUT\n",
1266 GNUNET_DHT_put (dht_handle,
1273 GNUNET_TIME_UNIT_FOREVER_REL,
1274 &dht_put_continuation,
1280 * Task that is run periodically to obtain blocks for content
1284 * @param tc scheduler context (also unused)
1287 gather_migration_blocks (void *cls,
1288 const struct GNUNET_SCHEDULER_TaskContext *tc)
1290 mig_task = GNUNET_SCHEDULER_NO_TASK;
1293 mig_qe = GNUNET_DATASTORE_get_random (dsh, 0, UINT_MAX,
1294 GNUNET_TIME_UNIT_FOREVER_REL,
1295 &process_migration_content, NULL);
1296 GNUNET_assert (mig_qe != NULL);
1302 * Task that is run periodically to obtain blocks for DHT PUTs.
1304 * @param cls type of blocks to gather
1305 * @param tc scheduler context (unused)
1308 gather_dht_put_blocks (void *cls,
1309 const struct GNUNET_SCHEDULER_TaskContext *tc)
1311 dht_task = GNUNET_SCHEDULER_NO_TASK;
1314 if (dht_put_type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
1315 dht_put_type = GNUNET_BLOCK_TYPE_FS_KBLOCK;
1316 dht_qe = GNUNET_DATASTORE_get_zero_anonymity (dsh, 0, UINT_MAX,
1317 GNUNET_TIME_UNIT_FOREVER_REL,
1319 &process_dht_put_content, NULL);
1320 GNUNET_assert (dht_qe != NULL);
1326 * We're done with a particular message list entry.
1327 * Free all associated resources.
1329 * @param pml entry to destroy
1332 destroy_pending_message_list_entry (struct PendingMessageList *pml)
1334 GNUNET_CONTAINER_DLL_remove (pml->req->pending_head,
1335 pml->req->pending_tail,
1337 GNUNET_CONTAINER_DLL_remove (pml->target->pending_messages_head,
1338 pml->target->pending_messages_tail,
1340 pml->target->pending_requests--;
1341 GNUNET_free (pml->pm);
1347 * Destroy the given pending message (and call the respective
1350 * @param pm message to destroy
1351 * @param tpid id of peer that the message was delivered to, or 0 for none
1354 destroy_pending_message (struct PendingMessage *pm,
1355 GNUNET_PEER_Id tpid)
1357 struct PendingMessageList *pml = pm->pml;
1358 TransmissionContinuation cont;
1362 cont_cls = pm->cont_cls;
1365 GNUNET_assert (pml->pm == pm);
1366 GNUNET_assert ( (tpid == 0) || (tpid == pml->target->pid) );
1367 destroy_pending_message_list_entry (pml);
1374 cont (cont_cls, tpid);
1379 * We're done processing a particular request.
1380 * Free all associated resources.
1382 * @param pr request to destroy
1385 destroy_pending_request (struct PendingRequest *pr)
1387 struct GNUNET_PeerIdentity pid;
1389 if (pr->hnode != NULL)
1391 GNUNET_CONTAINER_heap_remove_node (requests_by_expiration_heap,
1395 if (NULL == pr->client_request_list)
1397 GNUNET_STATISTICS_update (stats,
1398 gettext_noop ("# P2P searches active"),
1404 GNUNET_STATISTICS_update (stats,
1405 gettext_noop ("# client searches active"),
1410 GNUNET_CONTAINER_multihashmap_remove (query_request_map,
1414 GNUNET_LOAD_update (rt_entry_lifetime,
1415 GNUNET_TIME_absolute_get_duration (pr->start_time).value);
1419 GNUNET_DATASTORE_cancel (pr->qe);
1422 if (pr->dht_get != NULL)
1424 GNUNET_DHT_get_stop (pr->dht_get);
1427 if (pr->client_request_list != NULL)
1429 GNUNET_CONTAINER_DLL_remove (pr->client_request_list->client_list->rl_head,
1430 pr->client_request_list->client_list->rl_tail,
1431 pr->client_request_list);
1432 GNUNET_free (pr->client_request_list);
1433 pr->client_request_list = NULL;
1437 GNUNET_PEER_resolve (pr->cp->pid,
1439 (void) GNUNET_CONTAINER_multihashmap_remove (peer_request_map,
1446 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
1449 if (pr->irc != NULL)
1451 GNUNET_CORE_peer_change_preference_cancel (pr->irc);
1454 if (pr->replies_seen != NULL)
1456 GNUNET_free (pr->replies_seen);
1457 pr->replies_seen = NULL;
1459 if (pr->task != GNUNET_SCHEDULER_NO_TASK)
1461 GNUNET_SCHEDULER_cancel (sched,
1463 pr->task = GNUNET_SCHEDULER_NO_TASK;
1465 while (NULL != pr->pending_head)
1466 destroy_pending_message_list_entry (pr->pending_head);
1467 GNUNET_PEER_change_rc (pr->target_pid, -1);
1468 if (pr->used_pids != NULL)
1470 GNUNET_PEER_decrement_rcs (pr->used_pids, pr->used_pids_off);
1471 GNUNET_free (pr->used_pids);
1472 pr->used_pids_off = 0;
1473 pr->used_pids_size = 0;
1474 pr->used_pids = NULL;
1481 * Method called whenever a given peer connects.
1483 * @param cls closure, not used
1484 * @param peer peer identity this notification is about
1485 * @param latency reported latency of the connection with 'other'
1486 * @param distance reported distance (DV) to 'other'
1489 peer_connect_handler (void *cls,
1491 GNUNET_PeerIdentity * peer,
1492 struct GNUNET_TIME_Relative latency,
1495 struct ConnectedPeer *cp;
1496 struct MigrationReadyBlock *pos;
1500 cp = GNUNET_malloc (sizeof (struct ConnectedPeer));
1501 cp->transmission_delay = GNUNET_LOAD_value_init ();
1502 cp->pid = GNUNET_PEER_intern (peer);
1504 fn = get_trust_filename (peer);
1505 if ((GNUNET_DISK_file_test (fn) == GNUNET_YES) &&
1506 (sizeof (trust) == GNUNET_DISK_fn_read (fn, &trust, sizeof (trust))))
1507 cp->disk_trust = cp->trust = ntohl (trust);
1510 GNUNET_break (GNUNET_OK ==
1511 GNUNET_CONTAINER_multihashmap_put (connected_peers,
1514 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1519 (void) consider_migration (pos, &peer->hashPubKey, cp);
1526 * Increase the host credit by a value.
1528 * @param host which peer to change the trust value on
1529 * @param value is the int value by which the
1530 * host credit is to be increased or decreased
1531 * @returns the actual change in trust (positive or negative)
1534 change_host_trust (struct ConnectedPeer *host, int value)
1536 unsigned int old_trust;
1540 GNUNET_assert (host != NULL);
1541 old_trust = host->trust;
1544 if (host->trust + value < host->trust)
1546 value = UINT32_MAX - host->trust;
1547 host->trust = UINT32_MAX;
1550 host->trust += value;
1554 if (host->trust < -value)
1556 value = -host->trust;
1560 host->trust += value;
1567 * Write host-trust information to a file - flush the buffer entry!
1570 flush_trust (void *cls,
1571 const GNUNET_HashCode *key,
1574 struct ConnectedPeer *host = value;
1577 struct GNUNET_PeerIdentity pid;
1579 if (host->trust == host->disk_trust)
1580 return GNUNET_OK; /* unchanged */
1581 GNUNET_PEER_resolve (host->pid,
1583 fn = get_trust_filename (&pid);
1584 if (host->trust == 0)
1586 if ((0 != UNLINK (fn)) && (errno != ENOENT))
1587 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
1588 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
1592 trust = htonl (host->trust);
1593 if (sizeof(uint32_t) == GNUNET_DISK_fn_write (fn, &trust,
1595 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE
1596 | GNUNET_DISK_PERM_GROUP_READ | GNUNET_DISK_PERM_OTHER_READ))
1597 host->disk_trust = host->trust;
1604 * Call this method periodically to scan data/hosts for new hosts.
1607 cron_flush_trust (void *cls,
1608 const struct GNUNET_SCHEDULER_TaskContext *tc)
1611 if (NULL == connected_peers)
1613 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
1618 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1620 GNUNET_SCHEDULER_add_delayed (tc->sched,
1621 TRUST_FLUSH_FREQ, &cron_flush_trust, NULL);
1626 * Free (each) request made by the peer.
1628 * @param cls closure, points to peer that the request belongs to
1629 * @param key current key code
1630 * @param value value in the hash map
1631 * @return GNUNET_YES (we should continue to iterate)
1634 destroy_request (void *cls,
1635 const GNUNET_HashCode * key,
1638 const struct GNUNET_PeerIdentity * peer = cls;
1639 struct PendingRequest *pr = value;
1641 GNUNET_break (GNUNET_YES ==
1642 GNUNET_CONTAINER_multihashmap_remove (peer_request_map,
1645 destroy_pending_request (pr);
1651 * Method called whenever a peer disconnects.
1653 * @param cls closure, not used
1654 * @param peer peer identity this notification is about
1657 peer_disconnect_handler (void *cls,
1659 GNUNET_PeerIdentity * peer)
1661 struct ConnectedPeer *cp;
1662 struct PendingMessage *pm;
1664 struct MigrationReadyBlock *pos;
1665 struct MigrationReadyBlock *next;
1667 GNUNET_CONTAINER_multihashmap_get_multiple (peer_request_map,
1671 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
1675 for (i=0;i<CS2P_SUCCESS_LIST_SIZE;i++)
1677 if (NULL != cp->last_client_replies[i])
1679 GNUNET_SERVER_client_drop (cp->last_client_replies[i]);
1680 cp->last_client_replies[i] = NULL;
1683 GNUNET_break (GNUNET_YES ==
1684 GNUNET_CONTAINER_multihashmap_remove (connected_peers,
1687 /* remove this peer from migration considerations; schedule
1690 while (NULL != (pos = next))
1693 for (i=0;i<MIGRATION_LIST_SIZE;i++)
1695 if (pos->target_list[i] == cp->pid)
1697 GNUNET_PEER_change_rc (pos->target_list[i], -1);
1698 pos->target_list[i] = 0;
1701 if (pos->used_targets >= GNUNET_CONTAINER_multihashmap_size (connected_peers))
1703 delete_migration_block (pos);
1704 consider_migration_gathering ();
1707 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
1708 &consider_migration,
1711 GNUNET_PEER_change_rc (cp->pid, -1);
1712 GNUNET_PEER_decrement_rcs (cp->last_p2p_replies, P2P_SUCCESS_LIST_SIZE);
1713 if (NULL != cp->cth)
1715 GNUNET_CORE_notify_transmit_ready_cancel (cp->cth);
1718 if (cp->delayed_transmission_request_task != GNUNET_SCHEDULER_NO_TASK)
1720 GNUNET_SCHEDULER_cancel (sched, cp->delayed_transmission_request_task);
1721 cp->delayed_transmission_request_task = GNUNET_SCHEDULER_NO_TASK;
1723 while (NULL != (pm = cp->pending_messages_head))
1724 destroy_pending_message (pm, 0 /* delivery failed */);
1725 GNUNET_LOAD_value_free (cp->transmission_delay);
1726 GNUNET_break (0 == cp->pending_requests);
1732 * Iterator over hash map entries that removes all occurences
1733 * of the given 'client' from the 'last_client_replies' of the
1734 * given connected peer.
1736 * @param cls closure, the 'struct GNUNET_SERVER_Client*' to remove
1737 * @param key current key code (unused)
1738 * @param value value in the hash map (the 'struct ConnectedPeer*' to change)
1739 * @return GNUNET_YES (we should continue to iterate)
1742 remove_client_from_last_client_replies (void *cls,
1743 const GNUNET_HashCode * key,
1746 struct GNUNET_SERVER_Client *client = cls;
1747 struct ConnectedPeer *cp = value;
1750 for (i=0;i<CS2P_SUCCESS_LIST_SIZE;i++)
1752 if (cp->last_client_replies[i] == client)
1754 GNUNET_SERVER_client_drop (cp->last_client_replies[i]);
1755 cp->last_client_replies[i] = NULL;
1763 * A client disconnected. Remove all of its pending queries.
1765 * @param cls closure, NULL
1766 * @param client identification of the client
1769 handle_client_disconnect (void *cls,
1770 struct GNUNET_SERVER_Client
1773 struct ClientList *pos;
1774 struct ClientList *prev;
1775 struct ClientRequestList *rcl;
1776 struct ClientResponseMessage *creply;
1782 while ( (NULL != pos) &&
1783 (pos->client != client) )
1789 return; /* no requests pending for this client */
1790 while (NULL != (rcl = pos->rl_head))
1792 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1793 "Destroying pending request `%s' on disconnect\n",
1794 GNUNET_h2s (&rcl->req->query));
1795 destroy_pending_request (rcl->req);
1798 client_list = pos->next;
1800 prev->next = pos->next;
1801 if (pos->th != NULL)
1803 GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th);
1806 while (NULL != (creply = pos->res_head))
1808 GNUNET_CONTAINER_DLL_remove (pos->res_head,
1811 GNUNET_free (creply);
1813 GNUNET_SERVER_client_drop (pos->client);
1815 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
1816 &remove_client_from_last_client_replies,
1822 * Iterator to free peer entries.
1824 * @param cls closure, unused
1825 * @param key current key code
1826 * @param value value in the hash map (peer entry)
1827 * @return GNUNET_YES (we should continue to iterate)
1830 clean_peer (void *cls,
1831 const GNUNET_HashCode * key,
1834 peer_disconnect_handler (NULL, (const struct GNUNET_PeerIdentity*) key);
1840 * Task run during shutdown.
1846 shutdown_task (void *cls,
1847 const struct GNUNET_SCHEDULER_TaskContext *tc)
1851 GNUNET_DATASTORE_cancel (mig_qe);
1856 GNUNET_DATASTORE_cancel (dht_qe);
1859 if (GNUNET_SCHEDULER_NO_TASK != mig_task)
1861 GNUNET_SCHEDULER_cancel (sched, mig_task);
1862 mig_task = GNUNET_SCHEDULER_NO_TASK;
1864 if (GNUNET_SCHEDULER_NO_TASK != dht_task)
1866 GNUNET_SCHEDULER_cancel (sched, dht_task);
1867 dht_task = GNUNET_SCHEDULER_NO_TASK;
1869 while (client_list != NULL)
1870 handle_client_disconnect (NULL,
1871 client_list->client);
1872 cron_flush_trust (NULL, NULL);
1873 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
1876 GNUNET_break (0 == GNUNET_CONTAINER_heap_get_size (requests_by_expiration_heap));
1877 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
1878 requests_by_expiration_heap = 0;
1879 GNUNET_CONTAINER_multihashmap_destroy (connected_peers);
1880 connected_peers = NULL;
1881 GNUNET_break (0 == GNUNET_CONTAINER_multihashmap_size (query_request_map));
1882 GNUNET_CONTAINER_multihashmap_destroy (query_request_map);
1883 query_request_map = NULL;
1884 GNUNET_LOAD_value_free (rt_entry_lifetime);
1885 rt_entry_lifetime = NULL;
1886 GNUNET_break (0 == GNUNET_CONTAINER_multihashmap_size (peer_request_map));
1887 GNUNET_CONTAINER_multihashmap_destroy (peer_request_map);
1888 peer_request_map = NULL;
1889 GNUNET_assert (NULL != core);
1890 GNUNET_CORE_disconnect (core);
1894 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1899 GNUNET_DATASTORE_disconnect (dsh,
1903 while (mig_head != NULL)
1904 delete_migration_block (mig_head);
1905 GNUNET_assert (0 == mig_size);
1906 GNUNET_DHT_disconnect (dht_handle);
1908 GNUNET_LOAD_value_free (datastore_get_load);
1909 datastore_get_load = NULL;
1910 GNUNET_LOAD_value_free (datastore_put_load);
1911 datastore_put_load = NULL;
1912 GNUNET_BLOCK_context_destroy (block_ctx);
1914 GNUNET_CONFIGURATION_destroy (block_cfg);
1918 GNUNET_free_non_null (trustDirectory);
1919 trustDirectory = NULL;
1923 /* ******************* Utility functions ******************** */
1927 * We've had to delay a request for transmission to core, but now
1928 * we should be ready. Run it.
1930 * @param cls the 'struct ConnectedPeer' for which a request was delayed
1931 * @param tc task context (unused)
1934 delayed_transmission_request (void *cls,
1935 const struct GNUNET_SCHEDULER_TaskContext *tc)
1937 struct ConnectedPeer *cp = cls;
1938 struct GNUNET_PeerIdentity pid;
1939 struct PendingMessage *pm;
1941 pm = cp->pending_messages_head;
1942 cp->delayed_transmission_request_task = GNUNET_SCHEDULER_NO_TASK;
1943 GNUNET_assert (cp->cth == NULL);
1946 GNUNET_PEER_resolve (cp->pid,
1948 cp->last_transmission_request_start = GNUNET_TIME_absolute_get ();
1949 cp->cth = GNUNET_CORE_notify_transmit_ready (core,
1951 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1960 * Transmit messages by copying it to the target buffer
1961 * "buf". "buf" will be NULL and "size" zero if the socket was closed
1962 * for writing in the meantime. In that case, do nothing
1963 * (the disconnect or shutdown handler will take care of the rest).
1964 * If we were able to transmit messages and there are still more
1965 * pending, ask core again for further calls to this function.
1967 * @param cls closure, pointer to the 'struct ConnectedPeer*'
1968 * @param size number of bytes available in buf
1969 * @param buf where the callee should write the message
1970 * @return number of bytes written to buf
1973 transmit_to_peer (void *cls,
1974 size_t size, void *buf)
1976 struct ConnectedPeer *cp = cls;
1978 struct PendingMessage *pm;
1979 struct PendingMessage *next_pm;
1980 struct GNUNET_TIME_Absolute now;
1981 struct GNUNET_TIME_Relative min_delay;
1982 struct MigrationReadyBlock *mb;
1983 struct MigrationReadyBlock *next;
1984 struct PutMessage migm;
1987 struct GNUNET_PeerIdentity pid;
1993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1994 "Dropping message, core too busy.\n");
1996 GNUNET_LOAD_update (cp->transmission_delay,
2000 GNUNET_LOAD_update (cp->transmission_delay,
2001 GNUNET_TIME_absolute_get_duration (cp->last_transmission_request_start).value);
2002 now = GNUNET_TIME_absolute_get ();
2004 min_delay = GNUNET_TIME_UNIT_FOREVER_REL;
2005 next_pm = cp->pending_messages_head;
2006 while ( (NULL != (pm = next_pm) ) &&
2007 (pm->msize <= size) )
2010 if (pm->delay_until.value > now.value)
2012 min_delay = GNUNET_TIME_relative_min (min_delay,
2013 GNUNET_TIME_absolute_get_remaining (pm->delay_until));
2016 memcpy (&cbuf[msize], &pm[1], pm->msize);
2019 GNUNET_CONTAINER_DLL_remove (cp->pending_messages_head,
2020 cp->pending_messages_tail,
2022 if (NULL == pm->pml)
2023 cp->pending_requests--;
2024 destroy_pending_message (pm, cp->pid);
2027 min_delay = GNUNET_TIME_UNIT_ZERO;
2028 if (NULL != cp->pending_messages_head)
2030 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == cp->delayed_transmission_request_task);
2031 cp->delayed_transmission_request_task
2032 = GNUNET_SCHEDULER_add_delayed (sched,
2034 &delayed_transmission_request,
2039 GNUNET_PEER_resolve (cp->pid,
2042 while (NULL != (mb = next))
2045 for (i=0;i<MIGRATION_LIST_SIZE;i++)
2047 if ( (cp->pid == mb->target_list[i]) &&
2048 (mb->size + sizeof (migm) <= size) )
2050 GNUNET_PEER_change_rc (mb->target_list[i], -1);
2051 mb->target_list[i] = 0;
2053 memset (&migm, 0, sizeof (migm));
2054 migm.header.size = htons (sizeof (migm) + mb->size);
2055 migm.header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
2056 migm.type = htonl (mb->type);
2057 migm.expiration = GNUNET_TIME_absolute_hton (mb->expiration);
2058 memcpy (&cbuf[msize], &migm, sizeof (migm));
2059 msize += sizeof (migm);
2060 size -= sizeof (migm);
2061 memcpy (&cbuf[msize], &mb[1], mb->size);
2065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2066 "Pushing migration block `%s' (%u bytes) to `%s'\n",
2067 GNUNET_h2s (&mb->query),
2068 (unsigned int) mb->size,
2076 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2077 "Migration block `%s' (%u bytes) is not on migration list for peer `%s'\n",
2078 GNUNET_h2s (&mb->query),
2079 (unsigned int) mb->size,
2084 if ( (mb->used_targets >= MIGRATION_TARGET_COUNT) ||
2085 (mb->used_targets >= GNUNET_CONTAINER_multihashmap_size (connected_peers)) )
2087 delete_migration_block (mb);
2088 consider_migration_gathering ();
2091 consider_migration (NULL,
2096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2097 "Transmitting %u bytes to peer with PID %u\n",
2098 (unsigned int) msize,
2099 (unsigned int) cp->pid);
2106 * Add a message to the set of pending messages for the given peer.
2108 * @param cp peer to send message to
2109 * @param pm message to queue
2110 * @param pr request on which behalf this message is being queued
2113 add_to_pending_messages_for_peer (struct ConnectedPeer *cp,
2114 struct PendingMessage *pm,
2115 struct PendingRequest *pr)
2117 struct PendingMessage *pos;
2118 struct PendingMessageList *pml;
2119 struct GNUNET_PeerIdentity pid;
2121 GNUNET_assert (pm->next == NULL);
2122 GNUNET_assert (pm->pml == NULL);
2125 pml = GNUNET_malloc (sizeof (struct PendingMessageList));
2130 GNUNET_CONTAINER_DLL_insert (pr->pending_head,
2134 pos = cp->pending_messages_head;
2135 while ( (pos != NULL) &&
2136 (pm->priority < pos->priority) )
2138 GNUNET_CONTAINER_DLL_insert_after (cp->pending_messages_head,
2139 cp->pending_messages_tail,
2142 cp->pending_requests++;
2143 if (cp->pending_requests > MAX_QUEUE_PER_PEER)
2144 destroy_pending_message (cp->pending_messages_tail, 0);
2145 GNUNET_PEER_resolve (cp->pid, &pid);
2146 if (NULL != cp->cth)
2148 GNUNET_CORE_notify_transmit_ready_cancel (cp->cth);
2151 if (cp->delayed_transmission_request_task != GNUNET_SCHEDULER_NO_TASK)
2153 GNUNET_SCHEDULER_cancel (sched, cp->delayed_transmission_request_task);
2154 cp->delayed_transmission_request_task = GNUNET_SCHEDULER_NO_TASK;
2156 /* need to schedule transmission */
2157 cp->last_transmission_request_start = GNUNET_TIME_absolute_get ();
2158 cp->cth = GNUNET_CORE_notify_transmit_ready (core,
2159 cp->pending_messages_head->priority,
2162 cp->pending_messages_head->msize,
2165 if (cp->cth == NULL)
2168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2169 "Failed to schedule transmission with core!\n");
2171 GNUNET_STATISTICS_update (stats,
2172 gettext_noop ("# CORE transmission failures"),
2180 * Test if the DATABASE (GET) load on this peer is too high
2181 * to even consider processing the query at
2184 * @return GNUNET_YES if the load is too high to do anything (load high)
2185 * GNUNET_NO to process normally (load normal)
2186 * GNUNET_SYSERR to process for free (load low)
2189 test_get_load_too_high (uint32_t priority)
2193 ld = GNUNET_LOAD_get_load (datastore_get_load);
2196 GNUNET_STATISTICS_update (stats,
2197 gettext_noop ("# requests done for free (low load)"),
2200 return GNUNET_SYSERR;
2204 GNUNET_STATISTICS_update (stats,
2205 gettext_noop ("# requests done for a price (normal load)"),
2210 GNUNET_STATISTICS_update (stats,
2211 gettext_noop ("# requests dropped due to high load"),
2221 * Test if the DATABASE (PUT) load on this peer is too high
2222 * to even consider processing the query at
2225 * @return GNUNET_YES if the load is too high to do anything (load high)
2226 * GNUNET_NO to process normally (load normal or low)
2229 test_put_load_too_high (uint32_t priority)
2233 if (GNUNET_LOAD_get_average (datastore_put_load) < 50)
2234 return GNUNET_NO; /* very fast */
2235 ld = GNUNET_LOAD_get_load (datastore_put_load);
2236 if ( (ld < 1) || (ld < priority) )
2238 GNUNET_STATISTICS_update (stats,
2239 gettext_noop ("# storage requests dropped due to high load"),
2246 /* ******************* Pending Request Refresh Task ******************** */
2251 * We use a random delay to make the timing of requests less
2252 * predictable. This function returns such a random delay. We add a base
2253 * delay of MAX_CORK_DELAY (1s).
2255 * FIXME: make schedule dependent on the specifics of the request?
2256 * Or bandwidth and number of connected peers and load?
2258 * @return random delay to use for some request, between 1s and 1000+TTL_DECREMENT ms
2260 static struct GNUNET_TIME_Relative
2261 get_processing_delay ()
2264 GNUNET_TIME_relative_add (GNUNET_CONSTANTS_MAX_CORK_DELAY,
2265 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
2266 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2272 * We're processing a GET request from another peer and have decided
2273 * to forward it to other peers. This function is called periodically
2274 * and should forward the request to other peers until we have all
2275 * possible replies. If we have transmitted the *only* reply to
2276 * the initiator we should destroy the pending request. If we have
2277 * many replies in the queue to the initiator, we should delay sending
2278 * out more queries until the reply queue has shrunk some.
2280 * @param cls our "struct ProcessGetContext *"
2284 forward_request_task (void *cls,
2285 const struct GNUNET_SCHEDULER_TaskContext *tc);
2289 * Function called after we either failed or succeeded
2290 * at transmitting a query to a peer.
2292 * @param cls the requests "struct PendingRequest*"
2293 * @param tpid ID of receiving peer, 0 on transmission error
2296 transmit_query_continuation (void *cls,
2297 GNUNET_PEER_Id tpid)
2299 struct PendingRequest *pr = cls;
2301 GNUNET_STATISTICS_update (stats,
2302 gettext_noop ("# queries scheduled for forwarding"),
2308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2309 "Transmission of request failed, will try again later.\n");
2311 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
2312 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
2313 get_processing_delay (),
2314 &forward_request_task,
2318 GNUNET_STATISTICS_update (stats,
2319 gettext_noop ("# queries forwarded"),
2322 GNUNET_PEER_change_rc (tpid, 1);
2323 if (pr->used_pids_off == pr->used_pids_size)
2324 GNUNET_array_grow (pr->used_pids,
2326 pr->used_pids_size * 2 + 2);
2327 pr->used_pids[pr->used_pids_off++] = tpid;
2328 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
2329 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
2330 get_processing_delay (),
2331 &forward_request_task,
2337 * How many bytes should a bloomfilter be if we have already seen
2338 * entry_count responses? Note that BLOOMFILTER_K gives us the number
2339 * of bits set per entry. Furthermore, we should not re-size the
2340 * filter too often (to keep it cheap).
2342 * Since other peers will also add entries but not resize the filter,
2343 * we should generally pick a slightly larger size than what the
2344 * strict math would suggest.
2346 * @return must be a power of two and smaller or equal to 2^15.
2349 compute_bloomfilter_size (unsigned int entry_count)
2352 unsigned int ideal = (entry_count * BLOOMFILTER_K) / 4;
2353 uint16_t max = 1 << 15;
2355 if (entry_count > max)
2358 while ((size < max) && (size < ideal))
2367 * Recalculate our bloom filter for filtering replies. This function
2368 * will create a new bloom filter from scratch, so it should only be
2369 * called if we have no bloomfilter at all (and hence can create a
2370 * fresh one of minimal size without problems) OR if our peer is the
2371 * initiator (in which case we may resize to larger than mimimum size).
2373 * @param pr request for which the BF is to be recomputed
2376 refresh_bloomfilter (struct PendingRequest *pr)
2380 GNUNET_HashCode mhash;
2382 nsize = compute_bloomfilter_size (pr->replies_seen_off);
2383 if (nsize == pr->bf_size)
2384 return; /* size not changed */
2386 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
2387 pr->bf_size = nsize;
2388 pr->mingle = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, -1);
2389 pr->bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
2392 for (i=0;i<pr->replies_seen_off;i++)
2394 GNUNET_BLOCK_mingle_hash (&pr->replies_seen[i],
2397 GNUNET_CONTAINER_bloomfilter_add (pr->bf, &mhash);
2403 * Function called after we've tried to reserve a certain amount of
2404 * bandwidth for a reply. Check if we succeeded and if so send our
2407 * @param cls the requests "struct PendingRequest*"
2408 * @param peer identifies the peer
2409 * @param bpm_in set to the current bandwidth limit (receiving) for this peer
2410 * @param bpm_out set to the current bandwidth limit (sending) for this peer
2411 * @param amount set to the amount that was actually reserved or unreserved
2412 * @param preference current traffic preference for the given peer
2415 target_reservation_cb (void *cls,
2417 GNUNET_PeerIdentity * peer,
2418 struct GNUNET_BANDWIDTH_Value32NBO bpm_in,
2419 struct GNUNET_BANDWIDTH_Value32NBO bpm_out,
2421 uint64_t preference)
2423 struct PendingRequest *pr = cls;
2424 struct ConnectedPeer *cp;
2425 struct PendingMessage *pm;
2426 struct GetMessage *gm;
2427 GNUNET_HashCode *ext;
2437 /* error in communication with core, try again later */
2438 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
2439 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
2440 get_processing_delay (),
2441 &forward_request_task,
2445 // (3) transmit, update ttl/priority
2446 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
2450 /* Peer must have just left */
2452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2453 "Selected peer disconnected!\n");
2455 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
2456 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
2457 get_processing_delay (),
2458 &forward_request_task,
2462 no_route = GNUNET_NO;
2468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2469 "Failed to reserve bandwidth for reply (got %d/%u bytes only)!\n",
2473 GNUNET_STATISTICS_update (stats,
2474 gettext_noop ("# reply bandwidth reservation requests failed"),
2477 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
2478 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
2479 get_processing_delay (),
2480 &forward_request_task,
2482 return; /* this target round failed */
2484 no_route = GNUNET_YES;
2487 GNUNET_STATISTICS_update (stats,
2488 gettext_noop ("# queries scheduled for forwarding"),
2491 /* build message and insert message into priority queue */
2493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2494 "Forwarding request `%s' to `%4s'!\n",
2495 GNUNET_h2s (&pr->query),
2500 if (GNUNET_YES == no_route)
2502 bm |= GET_MESSAGE_BIT_RETURN_TO;
2505 if (pr->namespace != NULL)
2507 bm |= GET_MESSAGE_BIT_SKS_NAMESPACE;
2510 if (pr->target_pid != 0)
2512 bm |= GET_MESSAGE_BIT_TRANSMIT_TO;
2515 msize = sizeof (struct GetMessage) + pr->bf_size + k * sizeof(GNUNET_HashCode);
2516 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
2517 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
2519 gm = (struct GetMessage*) &pm[1];
2520 gm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_GET);
2521 gm->header.size = htons (msize);
2522 gm->type = htonl (pr->type);
2523 pr->remaining_priority /= 2;
2524 gm->priority = htonl (pr->remaining_priority);
2525 gm->ttl = htonl (pr->ttl);
2526 gm->filter_mutator = htonl(pr->mingle);
2527 gm->hash_bitmap = htonl (bm);
2528 gm->query = pr->query;
2529 ext = (GNUNET_HashCode*) &gm[1];
2531 if (GNUNET_YES == no_route)
2532 GNUNET_PEER_resolve (pr->cp->pid, (struct GNUNET_PeerIdentity*) &ext[k++]);
2533 if (pr->namespace != NULL)
2534 memcpy (&ext[k++], pr->namespace, sizeof (GNUNET_HashCode));
2535 if (pr->target_pid != 0)
2536 GNUNET_PEER_resolve (pr->target_pid, (struct GNUNET_PeerIdentity*) &ext[k++]);
2537 bfdata = (char *) &ext[k];
2539 GNUNET_CONTAINER_bloomfilter_get_raw_data (pr->bf,
2542 pm->cont = &transmit_query_continuation;
2544 add_to_pending_messages_for_peer (cp, pm, pr);
2549 * Closure used for "target_peer_select_cb".
2551 struct PeerSelectionContext
2554 * The request for which we are selecting
2557 struct PendingRequest *pr;
2560 * Current "prime" target.
2562 struct GNUNET_PeerIdentity target;
2565 * How much do we like this target?
2567 double target_score;
2573 * Function called for each connected peer to determine
2574 * which one(s) would make good targets for forwarding.
2576 * @param cls closure (struct PeerSelectionContext)
2577 * @param key current key code (peer identity)
2578 * @param value value in the hash map (struct ConnectedPeer)
2579 * @return GNUNET_YES if we should continue to
2584 target_peer_select_cb (void *cls,
2585 const GNUNET_HashCode * key,
2588 struct PeerSelectionContext *psc = cls;
2589 struct ConnectedPeer *cp = value;
2590 struct PendingRequest *pr = psc->pr;
2595 /* 1) check that this peer is not the initiator */
2599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2600 "Skipping initiator in forwarding selection\n");
2602 return GNUNET_YES; /* skip */
2605 /* 2) check if we have already (recently) forwarded to this peer */
2607 for (i=0;i<pr->used_pids_off;i++)
2608 if (pr->used_pids[i] == cp->pid)
2611 if (0 != GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2612 RETRY_PROBABILITY_INV))
2615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2616 "NOT re-trying query that was previously transmitted %u times\n",
2617 (unsigned int) pr->used_pids_off);
2619 return GNUNET_YES; /* skip */
2624 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2625 "Re-trying query that was previously transmitted %u times to this peer\n",
2628 /* 3) calculate how much we'd like to forward to this peer,
2629 starting with a random value that is strong enough
2630 to at least give any peer a chance sometimes
2631 (compared to the other factors that come later) */
2632 /* 3a) count successful (recent) routes from cp for same source */
2635 score = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2636 P2P_SUCCESS_LIST_SIZE);
2637 for (i=0;i<P2P_SUCCESS_LIST_SIZE;i++)
2638 if (cp->last_p2p_replies[i] == pr->cp->pid)
2639 score += 1.0; /* likely successful based on hot path */
2643 score = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2644 CS2P_SUCCESS_LIST_SIZE);
2645 for (i=0;i<CS2P_SUCCESS_LIST_SIZE;i++)
2646 if (cp->last_client_replies[i] == pr->client_request_list->client_list->client)
2647 score += 1.0; /* likely successful based on hot path */
2649 /* 3b) include latency */
2650 if (cp->avg_delay.value < 4 * TTL_DECREMENT)
2651 score += 1.0; /* likely fast based on latency */
2652 /* 3c) include priorities */
2653 if (cp->avg_priority <= pr->remaining_priority / 2.0)
2654 score += 1.0; /* likely successful based on priorities */
2655 /* 3d) penalize for queue size */
2656 score -= (2.0 * cp->pending_requests / (double) MAX_QUEUE_PER_PEER);
2657 /* 3e) include peer proximity */
2658 score -= (2.0 * (GNUNET_CRYPTO_hash_distance_u32 (key,
2659 &pr->query)) / (double) UINT32_MAX);
2660 /* 4) super-bonus for being the known target */
2661 if (pr->target_pid == cp->pid)
2663 /* store best-fit in closure */
2665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2666 "Peer `%s' gets score %f for forwarding query, max is %f\n",
2671 score++; /* avoid zero */
2672 if (score > psc->target_score)
2674 psc->target_score = score;
2675 psc->target.hashPubKey = *key;
2682 * The priority level imposes a bound on the maximum
2683 * value for the ttl that can be requested.
2685 * @param ttl_in requested ttl
2686 * @param prio given priority
2687 * @return ttl_in if ttl_in is below the limit,
2688 * otherwise the ttl-limit for the given priority
2691 bound_ttl (int32_t ttl_in, uint32_t prio)
2693 unsigned long long allowed;
2697 allowed = ((unsigned long long) prio) * TTL_DECREMENT / 1000;
2698 if (ttl_in > allowed)
2700 if (allowed >= (1 << 30))
2709 * Iterator called on each result obtained for a DHT
2710 * operation that expects a reply
2712 * @param cls closure
2713 * @param exp when will this value expire
2714 * @param key key of the result
2715 * @param get_path NULL-terminated array of pointers
2716 * to the peers on reverse GET path (or NULL if not recorded)
2717 * @param put_path NULL-terminated array of pointers
2718 * to the peers on the PUT path (or NULL if not recorded)
2719 * @param type type of the result
2720 * @param size number of bytes in data
2721 * @param data pointer to the result data
2724 process_dht_reply (void *cls,
2725 struct GNUNET_TIME_Absolute exp,
2726 const GNUNET_HashCode * key,
2727 const struct GNUNET_PeerIdentity * const *get_path,
2728 const struct GNUNET_PeerIdentity * const *put_path,
2729 enum GNUNET_BLOCK_Type type,
2735 * We're processing a GET request and have decided
2736 * to forward it to other peers. This function is called periodically
2737 * and should forward the request to other peers until we have all
2738 * possible replies. If we have transmitted the *only* reply to
2739 * the initiator we should destroy the pending request. If we have
2740 * many replies in the queue to the initiator, we should delay sending
2741 * out more queries until the reply queue has shrunk some.
2743 * @param cls our "struct ProcessGetContext *"
2747 forward_request_task (void *cls,
2748 const struct GNUNET_SCHEDULER_TaskContext *tc)
2750 struct PendingRequest *pr = cls;
2751 struct PeerSelectionContext psc;
2752 struct ConnectedPeer *cp;
2753 struct GNUNET_TIME_Relative delay;
2755 pr->task = GNUNET_SCHEDULER_NO_TASK;
2756 if (pr->irc != NULL)
2759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2760 "Forwarding of query `%s' not attempted due to pending local lookup!\n",
2761 GNUNET_h2s (&pr->query));
2763 return; /* already pending */
2765 if (GNUNET_YES == pr->local_only)
2766 return; /* configured to not do P2P search */
2768 if ( (0 == pr->anonymity_level) &&
2769 (GNUNET_YES != pr->forward_only) &&
2770 (pr->type != GNUNET_BLOCK_TYPE_FS_DBLOCK) &&
2771 (pr->type != GNUNET_BLOCK_TYPE_FS_IBLOCK) )
2773 pr->dht_get = GNUNET_DHT_get_start (dht_handle,
2774 GNUNET_TIME_UNIT_FOREVER_REL,
2781 (pr->namespace != NULL) ? sizeof (GNUNET_HashCode) : 0,
2785 /* (1) select target */
2787 psc.target_score = -DBL_MAX;
2788 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
2789 &target_peer_select_cb,
2791 if (psc.target_score == -DBL_MAX)
2793 delay = get_processing_delay ();
2795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2796 "No peer selected for forwarding of query `%s', will try again in %llu ms!\n",
2797 GNUNET_h2s (&pr->query),
2800 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
2802 &forward_request_task,
2804 return; /* nobody selected */
2806 /* (3) update TTL/priority */
2807 if (pr->client_request_list != NULL)
2809 /* FIXME: use better algorithm!? */
2810 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2813 /* bound priority we use by priorities we see from other peers
2814 rounded up (must round up so that we can see non-zero
2815 priorities, but round up as little as possible to make it
2816 plausible that we forwarded another peers request) */
2817 if (pr->priority > current_priorities + 1.0)
2818 pr->priority = (uint32_t) current_priorities + 1.0;
2819 pr->ttl = bound_ttl (pr->ttl + TTL_DECREMENT * 2,
2822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2823 "Trying query `%s' with priority %u and TTL %d.\n",
2824 GNUNET_h2s (&pr->query),
2830 /* (3) reserve reply bandwidth */
2831 if (GNUNET_NO == pr->forward_only)
2833 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
2834 &psc.target.hashPubKey);
2835 GNUNET_assert (NULL != cp);
2836 pr->irc = GNUNET_CORE_peer_change_preference (sched, cfg,
2838 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
2839 GNUNET_BANDWIDTH_value_init (UINT32_MAX),
2842 &target_reservation_cb,
2844 cp->inc_preference = 0;
2848 /* force forwarding */
2849 static struct GNUNET_BANDWIDTH_Value32NBO zerobw;
2850 target_reservation_cb (pr, &psc.target,
2851 zerobw, zerobw, 0, 0.0);
2856 /* **************************** P2P PUT Handling ************************ */
2860 * Function called after we either failed or succeeded
2861 * at transmitting a reply to a peer.
2863 * @param cls the requests "struct PendingRequest*"
2864 * @param tpid ID of receiving peer, 0 on transmission error
2867 transmit_reply_continuation (void *cls,
2868 GNUNET_PEER_Id tpid)
2870 struct PendingRequest *pr = cls;
2874 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
2875 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
2876 /* only one reply expected, done with the request! */
2877 destroy_pending_request (pr);
2879 case GNUNET_BLOCK_TYPE_ANY:
2880 case GNUNET_BLOCK_TYPE_FS_KBLOCK:
2881 case GNUNET_BLOCK_TYPE_FS_SBLOCK:
2891 * Transmit the given message by copying it to the target buffer
2892 * "buf". "buf" will be NULL and "size" zero if the socket was closed
2893 * for writing in the meantime. In that case, do nothing
2894 * (the disconnect or shutdown handler will take care of the rest).
2895 * If we were able to transmit messages and there are still more
2896 * pending, ask core again for further calls to this function.
2898 * @param cls closure, pointer to the 'struct ClientList*'
2899 * @param size number of bytes available in buf
2900 * @param buf where the callee should write the message
2901 * @return number of bytes written to buf
2904 transmit_to_client (void *cls,
2905 size_t size, void *buf)
2907 struct ClientList *cl = cls;
2909 struct ClientResponseMessage *creply;
2916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2917 "Not sending reply, client communication problem.\n");
2922 while ( (NULL != (creply = cl->res_head) ) &&
2923 (creply->msize <= size) )
2925 memcpy (&cbuf[msize], &creply[1], creply->msize);
2926 msize += creply->msize;
2927 size -= creply->msize;
2928 GNUNET_CONTAINER_DLL_remove (cl->res_head,
2931 GNUNET_free (creply);
2934 cl->th = GNUNET_SERVER_notify_transmit_ready (cl->client,
2936 GNUNET_TIME_UNIT_FOREVER_REL,
2937 &transmit_to_client,
2940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2941 "Transmitted %u bytes to client\n",
2942 (unsigned int) msize);
2949 * Closure for "process_reply" function.
2951 struct ProcessReplyClosure
2954 * The data for the reply.
2959 * Who gave us this reply? NULL for local host (or DHT)
2961 struct ConnectedPeer *sender;
2964 * When the reply expires.
2966 struct GNUNET_TIME_Absolute expiration;
2974 * Type of the block.
2976 enum GNUNET_BLOCK_Type type;
2979 * How much was this reply worth to us?
2984 * Evaluation result (returned).
2986 enum GNUNET_BLOCK_EvaluationResult eval;
2989 * Did we finish processing the associated request?
2994 * Did we find a matching request?
3001 * We have received a reply; handle it!
3003 * @param cls response (struct ProcessReplyClosure)
3004 * @param key our query
3005 * @param value value in the hash map (info about the query)
3006 * @return GNUNET_YES (we should continue to iterate)
3009 process_reply (void *cls,
3010 const GNUNET_HashCode * key,
3013 struct ProcessReplyClosure *prq = cls;
3014 struct PendingRequest *pr = value;
3015 struct PendingMessage *reply;
3016 struct ClientResponseMessage *creply;
3017 struct ClientList *cl;
3018 struct PutMessage *pm;
3019 struct ConnectedPeer *cp;
3020 struct GNUNET_TIME_Relative cur_delay;
3024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3025 "Matched result (type %u) for query `%s' with pending request\n",
3026 (unsigned int) prq->type,
3029 GNUNET_STATISTICS_update (stats,
3030 gettext_noop ("# replies received and matched"),
3033 if (prq->sender != NULL)
3035 cur_delay = GNUNET_TIME_absolute_get_duration (pr->start_time);
3036 prq->sender->avg_delay.value
3037 = (prq->sender->avg_delay.value *
3038 (RUNAVG_DELAY_N - 1) + cur_delay.value) / RUNAVG_DELAY_N;
3039 prq->sender->avg_priority
3040 = (prq->sender->avg_priority *
3041 (RUNAVG_DELAY_N - 1) + pr->priority) / (double) RUNAVG_DELAY_N;
3044 GNUNET_PEER_change_rc (prq->sender->last_p2p_replies
3045 [prq->sender->last_p2p_replies_woff % P2P_SUCCESS_LIST_SIZE],
3047 GNUNET_PEER_change_rc (pr->cp->pid, 1);
3048 prq->sender->last_p2p_replies
3049 [(prq->sender->last_p2p_replies_woff++) % P2P_SUCCESS_LIST_SIZE]
3054 if (NULL != prq->sender->last_client_replies
3055 [(prq->sender->last_client_replies_woff) % CS2P_SUCCESS_LIST_SIZE])
3056 GNUNET_SERVER_client_drop (prq->sender->last_client_replies
3057 [(prq->sender->last_client_replies_woff) % CS2P_SUCCESS_LIST_SIZE]);
3058 prq->sender->last_client_replies
3059 [(prq->sender->last_client_replies_woff++) % CS2P_SUCCESS_LIST_SIZE]
3060 = pr->client_request_list->client_list->client;
3061 GNUNET_SERVER_client_keep (pr->client_request_list->client_list->client);
3064 prq->eval = GNUNET_BLOCK_evaluate (block_ctx,
3069 pr->namespace, (pr->namespace != NULL) ? sizeof (GNUNET_HashCode) : 0,
3074 case GNUNET_BLOCK_EVALUATION_OK_MORE:
3076 case GNUNET_BLOCK_EVALUATION_OK_LAST:
3077 while (NULL != pr->pending_head)
3078 destroy_pending_message_list_entry (pr->pending_head);
3081 if (pr->client_request_list != NULL)
3082 GNUNET_SERVER_receive_done (pr->client_request_list->client_list->client,
3084 GNUNET_DATASTORE_cancel (pr->qe);
3087 pr->do_remove = GNUNET_YES;
3088 if (pr->task != GNUNET_SCHEDULER_NO_TASK)
3090 GNUNET_SCHEDULER_cancel (sched,
3092 pr->task = GNUNET_SCHEDULER_NO_TASK;
3094 GNUNET_break (GNUNET_YES ==
3095 GNUNET_CONTAINER_multihashmap_remove (query_request_map,
3098 GNUNET_LOAD_update (rt_entry_lifetime,
3099 GNUNET_TIME_absolute_get_duration (pr->start_time).value);
3101 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
3102 GNUNET_STATISTICS_update (stats,
3103 gettext_noop ("# duplicate replies discarded (bloomfilter)"),
3107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3108 "Duplicate response `%s', discarding.\n",
3109 GNUNET_h2s (&mhash));
3111 return GNUNET_YES; /* duplicate */
3112 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
3113 return GNUNET_YES; /* wrong namespace */
3114 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
3117 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
3120 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
3121 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3122 _("Unsupported block type %u\n"),
3126 if (pr->client_request_list != NULL)
3128 if (pr->replies_seen_size == pr->replies_seen_off)
3129 GNUNET_array_grow (pr->replies_seen,
3130 pr->replies_seen_size,
3131 pr->replies_seen_size * 2 + 4);
3132 GNUNET_CRYPTO_hash (prq->data,
3134 &pr->replies_seen[pr->replies_seen_off++]);
3135 refresh_bloomfilter (pr);
3137 if (NULL == prq->sender)
3140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3141 "Found result for query `%s' in local datastore\n",
3144 GNUNET_STATISTICS_update (stats,
3145 gettext_noop ("# results found locally"),
3149 prq->priority += pr->remaining_priority;
3150 pr->remaining_priority = 0;
3151 pr->results_found++;
3152 prq->request_found = GNUNET_YES;
3153 if (NULL != pr->client_request_list)
3155 GNUNET_STATISTICS_update (stats,
3156 gettext_noop ("# replies received for local clients"),
3159 cl = pr->client_request_list->client_list;
3160 msize = sizeof (struct PutMessage) + prq->size;
3161 creply = GNUNET_malloc (msize + sizeof (struct ClientResponseMessage));
3162 creply->msize = msize;
3163 creply->client_list = cl;
3164 GNUNET_CONTAINER_DLL_insert_after (cl->res_head,
3168 pm = (struct PutMessage*) &creply[1];
3169 pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
3170 pm->header.size = htons (msize);
3171 pm->type = htonl (prq->type);
3172 pm->expiration = GNUNET_TIME_absolute_hton (prq->expiration);
3173 memcpy (&pm[1], prq->data, prq->size);
3177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3178 "Transmitting result for query `%s' to client\n",
3181 cl->th = GNUNET_SERVER_notify_transmit_ready (cl->client,
3183 GNUNET_TIME_UNIT_FOREVER_REL,
3184 &transmit_to_client,
3187 GNUNET_break (cl->th != NULL);
3190 prq->finished = GNUNET_YES;
3191 destroy_pending_request (pr);
3198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3199 "Transmitting result for query `%s' to other peer (PID=%u)\n",
3201 (unsigned int) cp->pid);
3203 GNUNET_STATISTICS_update (stats,
3204 gettext_noop ("# replies received for other peers"),
3207 msize = sizeof (struct PutMessage) + prq->size;
3208 reply = GNUNET_malloc (msize + sizeof (struct PendingMessage));
3209 reply->cont = &transmit_reply_continuation;
3210 reply->cont_cls = pr;
3213 = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
3214 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
3217 reply->msize = msize;
3218 reply->priority = UINT32_MAX; /* send replies first! */
3219 pm = (struct PutMessage*) &reply[1];
3220 pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
3221 pm->header.size = htons (msize);
3222 pm->type = htonl (prq->type);
3223 pm->expiration = GNUNET_TIME_absolute_hton (prq->expiration);
3224 memcpy (&pm[1], prq->data, prq->size);
3225 add_to_pending_messages_for_peer (cp, reply, pr);
3232 * Iterator called on each result obtained for a DHT
3233 * operation that expects a reply
3235 * @param cls closure
3236 * @param exp when will this value expire
3237 * @param key key of the result
3238 * @param get_path NULL-terminated array of pointers
3239 * to the peers on reverse GET path (or NULL if not recorded)
3240 * @param put_path NULL-terminated array of pointers
3241 * to the peers on the PUT path (or NULL if not recorded)
3242 * @param type type of the result
3243 * @param size number of bytes in data
3244 * @param data pointer to the result data
3247 process_dht_reply (void *cls,
3248 struct GNUNET_TIME_Absolute exp,
3249 const GNUNET_HashCode * key,
3250 const struct GNUNET_PeerIdentity * const *get_path,
3251 const struct GNUNET_PeerIdentity * const *put_path,
3252 enum GNUNET_BLOCK_Type type,
3256 struct PendingRequest *pr = cls;
3257 struct ProcessReplyClosure prq;
3259 memset (&prq, 0, sizeof (prq));
3261 prq.expiration = exp;
3264 process_reply (&prq, key, pr);
3270 * Continuation called to notify client about result of the
3273 * @param cls closure
3274 * @param success GNUNET_SYSERR on failure
3275 * @param msg NULL on success, otherwise an error message
3278 put_migration_continuation (void *cls,
3282 struct GNUNET_TIME_Absolute *start = cls;
3283 struct GNUNET_TIME_Relative delay;
3285 delay = GNUNET_TIME_absolute_get_duration (*start);
3286 GNUNET_free (start);
3287 GNUNET_LOAD_update (datastore_put_load,
3289 if (GNUNET_OK == success)
3291 GNUNET_STATISTICS_update (stats,
3292 gettext_noop ("# datastore 'put' failures"),
3299 * Handle P2P "PUT" message.
3301 * @param cls closure, always NULL
3302 * @param other the other peer involved (sender or receiver, NULL
3303 * for loopback messages where we are both sender and receiver)
3304 * @param message the actual message
3305 * @param latency reported latency of the connection with 'other'
3306 * @param distance reported distance (DV) to 'other'
3307 * @return GNUNET_OK to keep the connection open,
3308 * GNUNET_SYSERR to close it (signal serious error)
3311 handle_p2p_put (void *cls,
3312 const struct GNUNET_PeerIdentity *other,
3313 const struct GNUNET_MessageHeader *message,
3314 struct GNUNET_TIME_Relative latency,
3317 const struct PutMessage *put;
3320 enum GNUNET_BLOCK_Type type;
3321 struct GNUNET_TIME_Absolute expiration;
3322 GNUNET_HashCode query;
3323 struct ProcessReplyClosure prq;
3324 struct GNUNET_TIME_Absolute *start;
3325 struct GNUNET_TIME_Relative block_time;
3327 struct ConnectedPeer *cp;
3328 struct PendingMessage *pm;
3329 struct MigrationStopMessage *msm;
3331 msize = ntohs (message->size);
3332 if (msize < sizeof (struct PutMessage))
3335 return GNUNET_SYSERR;
3337 put = (const struct PutMessage*) message;
3338 dsize = msize - sizeof (struct PutMessage);
3339 type = ntohl (put->type);
3340 expiration = GNUNET_TIME_absolute_ntoh (put->expiration);
3342 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
3343 return GNUNET_SYSERR;
3345 GNUNET_BLOCK_get_key (block_ctx,
3351 GNUNET_break_op (0);
3352 return GNUNET_SYSERR;
3355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3356 "Received result for query `%s' from peer `%4s'\n",
3357 GNUNET_h2s (&query),
3358 GNUNET_i2s (other));
3360 GNUNET_STATISTICS_update (stats,
3361 gettext_noop ("# replies received (overall)"),
3364 /* now, lookup 'query' */
3365 prq.data = (const void*) &put[1];
3367 prq.sender = GNUNET_CONTAINER_multihashmap_get (connected_peers,
3368 &other->hashPubKey);
3373 prq.expiration = expiration;
3375 prq.finished = GNUNET_NO;
3376 prq.request_found = GNUNET_NO;
3377 GNUNET_CONTAINER_multihashmap_get_multiple (query_request_map,
3381 if (prq.sender != NULL)
3383 prq.sender->inc_preference += CONTENT_BANDWIDTH_VALUE + 1000 * prq.priority;
3384 prq.sender->trust += prq.priority;
3386 if ( (GNUNET_YES == active_migration) &&
3387 (GNUNET_NO == test_put_load_too_high (prq.priority)) )
3390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3391 "Replicating result for query `%s' with priority %u\n",
3392 GNUNET_h2s (&query),
3395 start = GNUNET_malloc (sizeof (struct GNUNET_TIME_Absolute));
3396 *start = GNUNET_TIME_absolute_get ();
3397 GNUNET_DATASTORE_put (dsh,
3398 0, &query, dsize, &put[1],
3399 type, prq.priority, 1 /* anonymity */,
3401 1 + prq.priority, MAX_DATASTORE_QUEUE,
3402 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
3403 &put_migration_continuation,
3406 putl = GNUNET_LOAD_get_load (datastore_put_load);
3407 if ( (GNUNET_NO == prq.request_found) &&
3408 ( (GNUNET_YES != active_migration) ||
3411 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
3412 &other->hashPubKey);
3413 if (GNUNET_TIME_absolute_get_duration (cp->last_migration_block).value < 5000)
3414 return GNUNET_OK; /* already blocked */
3415 /* We're too busy; send MigrationStop message! */
3416 if (GNUNET_YES != active_migration)
3417 putl = 1.0 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 5);
3418 block_time = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
3419 5000 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
3420 (unsigned int) (60000 * putl * putl)));
3422 cp->last_migration_block = GNUNET_TIME_relative_to_absolute (block_time);
3423 pm = GNUNET_malloc (sizeof (struct PendingMessage) +
3424 sizeof (struct MigrationStopMessage));
3425 pm->msize = sizeof (struct MigrationStopMessage);
3426 pm->priority = UINT32_MAX;
3427 msm = (struct MigrationStopMessage*) &pm[1];
3428 msm->header.size = htons (sizeof (struct MigrationStopMessage));
3429 msm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_MIGRATION_STOP);
3430 msm->duration = GNUNET_TIME_relative_hton (block_time);
3431 add_to_pending_messages_for_peer (cp,
3440 * Handle P2P "MIGRATION_STOP" message.
3442 * @param cls closure, always NULL
3443 * @param other the other peer involved (sender or receiver, NULL
3444 * for loopback messages where we are both sender and receiver)
3445 * @param message the actual message
3446 * @param latency reported latency of the connection with 'other'
3447 * @param distance reported distance (DV) to 'other'
3448 * @return GNUNET_OK to keep the connection open,
3449 * GNUNET_SYSERR to close it (signal serious error)
3452 handle_p2p_migration_stop (void *cls,
3453 const struct GNUNET_PeerIdentity *other,
3454 const struct GNUNET_MessageHeader *message,
3455 struct GNUNET_TIME_Relative latency,
3458 struct ConnectedPeer *cp;
3459 const struct MigrationStopMessage *msm;
3461 msm = (const struct MigrationStopMessage*) message;
3462 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
3463 &other->hashPubKey);
3469 cp->migration_blocked = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_ntoh (msm->duration));
3475 /* **************************** P2P GET Handling ************************ */
3479 * Closure for 'check_duplicate_request_{peer,client}'.
3481 struct CheckDuplicateRequestClosure
3484 * The new request we should check if it already exists.
3486 const struct PendingRequest *pr;
3489 * Existing request found by the checker, NULL if none.
3491 struct PendingRequest *have;
3496 * Iterator over entries in the 'query_request_map' that
3497 * tries to see if we have the same request pending from
3498 * the same client already.
3500 * @param cls closure (our 'struct CheckDuplicateRequestClosure')
3501 * @param key current key code (query, ignored, must match)
3502 * @param value value in the hash map (a 'struct PendingRequest'
3503 * that already exists)
3504 * @return GNUNET_YES if we should continue to
3505 * iterate (no match yet)
3506 * GNUNET_NO if not (match found).
3509 check_duplicate_request_client (void *cls,
3510 const GNUNET_HashCode * key,
3513 struct CheckDuplicateRequestClosure *cdc = cls;
3514 struct PendingRequest *have = value;
3516 if (have->client_request_list == NULL)
3518 if ( (cdc->pr->client_request_list->client_list->client == have->client_request_list->client_list->client) &&
3529 * We're processing (local) results for a search request
3530 * from another peer. Pass applicable results to the
3531 * peer and if we are done either clean up (operation
3532 * complete) or forward to other peers (more results possible).
3534 * @param cls our closure (struct LocalGetContext)
3535 * @param key key for the content
3536 * @param size number of bytes in data
3537 * @param data content stored
3538 * @param type type of the content
3539 * @param priority priority of the content
3540 * @param anonymity anonymity-level for the content
3541 * @param expiration expiration time for the content
3542 * @param uid unique identifier for the datum;
3543 * maybe 0 if no unique identifier is available
3546 process_local_reply (void *cls,
3547 const GNUNET_HashCode * key,
3550 enum GNUNET_BLOCK_Type type,
3553 struct GNUNET_TIME_Absolute
3557 struct PendingRequest *pr = cls;
3558 struct ProcessReplyClosure prq;
3559 struct CheckDuplicateRequestClosure cdrc;
3560 GNUNET_HashCode query;
3561 unsigned int old_rf;
3566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3567 "Done processing local replies, forwarding request to other peers.\n");
3570 if (pr->client_request_list != NULL)
3572 GNUNET_SERVER_receive_done (pr->client_request_list->client_list->client,
3574 /* Figure out if this is a duplicate request and possibly
3575 merge 'struct PendingRequest' entries */
3578 GNUNET_CONTAINER_multihashmap_get_multiple (query_request_map,
3580 &check_duplicate_request_client,
3582 if (cdrc.have != NULL)
3585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3586 "Received request for block `%s' twice from client, will only request once.\n",
3587 GNUNET_h2s (&pr->query));
3590 destroy_pending_request (pr);
3595 /* no more results */
3596 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
3597 pr->task = GNUNET_SCHEDULER_add_now (sched,
3598 &forward_request_task,
3603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3604 "New local response to `%s' of type %u.\n",
3608 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
3611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3612 "Found ONDEMAND block, performing on-demand encoding\n");
3614 GNUNET_STATISTICS_update (stats,
3615 gettext_noop ("# on-demand blocks matched requests"),
3619 GNUNET_FS_handle_on_demand_block (key, size, data, type, priority,
3620 anonymity, expiration, uid,
3621 &process_local_reply,
3625 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
3629 old_rf = pr->results_found;
3630 memset (&prq, 0, sizeof (prq));
3632 prq.expiration = expiration;
3635 GNUNET_BLOCK_get_key (block_ctx,
3642 GNUNET_DATASTORE_remove (dsh,
3646 GNUNET_TIME_UNIT_FOREVER_REL,
3648 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
3652 prq.priority = priority;
3653 prq.finished = GNUNET_NO;
3654 prq.request_found = GNUNET_NO;
3655 if ( (old_rf == 0) &&
3656 (pr->results_found == 0) )
3657 update_datastore_delays (pr->start_time);
3658 process_reply (&prq, key, pr);
3659 if (prq.finished == GNUNET_YES)
3662 return; /* done here */
3663 if (prq.eval == GNUNET_BLOCK_EVALUATION_OK_LAST)
3665 GNUNET_DATASTORE_get_next (dsh, GNUNET_NO);
3668 if ( (pr->client_request_list == NULL) &&
3669 ( (GNUNET_YES == test_get_load_too_high (0)) ||
3670 (pr->results_found > 5 + 2 * pr->priority) ) )
3673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3674 "Load too high, done with request\n");
3676 GNUNET_STATISTICS_update (stats,
3677 gettext_noop ("# processing result set cut short due to load"),
3680 GNUNET_DATASTORE_get_next (dsh, GNUNET_NO);
3683 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
3688 * We've received a request with the specified priority. Bound it
3689 * according to how much we trust the given peer.
3691 * @param prio_in requested priority
3692 * @param cp the peer making the request
3693 * @return effective priority
3696 bound_priority (uint32_t prio_in,
3697 struct ConnectedPeer *cp)
3699 #define N ((double)128.0)
3704 ld = test_get_load_too_high (0);
3705 if (ld == GNUNET_SYSERR)
3706 return 0; /* excess resources */
3707 ret = change_host_trust (cp, prio_in);
3710 if (ret > current_priorities + N)
3711 rret = current_priorities + N;
3715 = (current_priorities * (N-1) + rret)/N;
3717 if ( (ld == GNUNET_YES) && (ret > 0) )
3719 /* try with charging */
3720 ld = test_get_load_too_high (ret);
3722 if (ld == GNUNET_YES)
3726 change_host_trust (cp, -ret);
3727 return -1; /* not enough resources */
3735 * Iterator over entries in the 'query_request_map' that
3736 * tries to see if we have the same request pending from
3737 * the same peer already.
3739 * @param cls closure (our 'struct CheckDuplicateRequestClosure')
3740 * @param key current key code (query, ignored, must match)
3741 * @param value value in the hash map (a 'struct PendingRequest'
3742 * that already exists)
3743 * @return GNUNET_YES if we should continue to
3744 * iterate (no match yet)
3745 * GNUNET_NO if not (match found).
3748 check_duplicate_request_peer (void *cls,
3749 const GNUNET_HashCode * key,
3752 struct CheckDuplicateRequestClosure *cdc = cls;
3753 struct PendingRequest *have = value;
3755 if (cdc->pr->target_pid == have->target_pid)
3765 * Handle P2P "GET" request.
3767 * @param cls closure, always NULL
3768 * @param other the other peer involved (sender or receiver, NULL
3769 * for loopback messages where we are both sender and receiver)
3770 * @param message the actual message
3771 * @param latency reported latency of the connection with 'other'
3772 * @param distance reported distance (DV) to 'other'
3773 * @return GNUNET_OK to keep the connection open,
3774 * GNUNET_SYSERR to close it (signal serious error)
3777 handle_p2p_get (void *cls,
3778 const struct GNUNET_PeerIdentity *other,
3779 const struct GNUNET_MessageHeader *message,
3780 struct GNUNET_TIME_Relative latency,
3783 struct PendingRequest *pr;
3784 struct ConnectedPeer *cp;
3785 struct ConnectedPeer *cps;
3786 struct CheckDuplicateRequestClosure cdc;
3787 struct GNUNET_TIME_Relative timeout;
3789 const struct GetMessage *gm;
3791 const GNUNET_HashCode *opt;
3794 uint32_t ttl_decrement;
3796 enum GNUNET_BLOCK_Type type;
3799 msize = ntohs(message->size);
3800 if (msize < sizeof (struct GetMessage))
3802 GNUNET_break_op (0);
3803 return GNUNET_SYSERR;
3805 gm = (const struct GetMessage*) message;
3806 type = ntohl (gm->type);
3807 bm = ntohl (gm->hash_bitmap);
3815 if (msize < sizeof (struct GetMessage) + bits * sizeof (GNUNET_HashCode))
3817 GNUNET_break_op (0);
3818 return GNUNET_SYSERR;
3820 opt = (const GNUNET_HashCode*) &gm[1];
3821 bfsize = msize - sizeof (struct GetMessage) + bits * sizeof (GNUNET_HashCode);
3822 bm = ntohl (gm->hash_bitmap);
3824 cps = GNUNET_CONTAINER_multihashmap_get (connected_peers,
3825 &other->hashPubKey);
3828 /* peer must have just disconnected */
3829 GNUNET_STATISTICS_update (stats,
3830 gettext_noop ("# requests dropped due to initiator not being connected"),
3833 return GNUNET_SYSERR;
3835 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
3836 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
3843 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
3844 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3845 "Failed to find RETURN-TO peer `%4s' in connection set. Dropping query.\n",
3846 GNUNET_i2s ((const struct GNUNET_PeerIdentity*) &opt[bits-1]));
3849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3850 "Failed to find peer `%4s' in connection set. Dropping query.\n",
3851 GNUNET_i2s (other));
3853 GNUNET_STATISTICS_update (stats,
3854 gettext_noop ("# requests dropped due to missing reverse route"),
3857 /* FIXME: try connect? */
3860 /* note that we can really only check load here since otherwise
3861 peers could find out that we are overloaded by not being
3862 disconnected after sending us a malformed query... */
3863 priority = bound_priority (ntohl (gm->priority), cps);
3867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3868 "Dropping query from `%s', this peer is too busy.\n",
3869 GNUNET_i2s (other));
3871 GNUNET_STATISTICS_update (stats,
3872 gettext_noop ("# requests dropped due to high load"),
3878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3879 "Received request for `%s' of type %u from peer `%4s' with flags %u\n",
3880 GNUNET_h2s (&gm->query),
3881 (unsigned int) type,
3885 have_ns = (0 != (bm & GET_MESSAGE_BIT_SKS_NAMESPACE));
3886 pr = GNUNET_malloc (sizeof (struct PendingRequest) +
3887 (have_ns ? sizeof(GNUNET_HashCode) : 0));
3890 pr->namespace = (GNUNET_HashCode*) &pr[1];
3891 memcpy (&pr[1], &opt[bits++], sizeof (GNUNET_HashCode));
3893 if ( (GNUNET_LOAD_get_load (cp->transmission_delay) > 3) ||
3894 (GNUNET_LOAD_get_average (cp->transmission_delay) >
3895 GNUNET_CONSTANTS_MAX_CORK_DELAY.value * 2 + GNUNET_LOAD_get_average (rt_entry_lifetime)) )
3897 /* don't have BW to send to peer, or would likely take longer than we have for it,
3898 so at best indirect the query */
3900 pr->forward_only = GNUNET_YES;
3903 pr->mingle = ntohl (gm->filter_mutator);
3904 if (0 != (bm & GET_MESSAGE_BIT_TRANSMIT_TO))
3905 pr->target_pid = GNUNET_PEER_intern ((const struct GNUNET_PeerIdentity*) &opt[bits++]);
3906 pr->anonymity_level = 1;
3907 pr->priority = (uint32_t) priority;
3908 pr->ttl = bound_ttl (ntohl (gm->ttl), pr->priority);
3909 pr->query = gm->query;
3910 /* decrement ttl (always) */
3911 ttl_decrement = 2 * TTL_DECREMENT +
3912 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
3914 if ( (pr->ttl < 0) &&
3915 (((int32_t)(pr->ttl - ttl_decrement)) > 0) )
3918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3919 "Dropping query from `%s' due to TTL underflow (%d - %u).\n",
3924 GNUNET_STATISTICS_update (stats,
3925 gettext_noop ("# requests dropped due TTL underflow"),
3928 /* integer underflow => drop (should be very rare)! */
3932 pr->ttl -= ttl_decrement;
3933 pr->start_time = GNUNET_TIME_absolute_get ();
3935 /* get bloom filter */
3938 pr->bf = GNUNET_CONTAINER_bloomfilter_init ((const char*) &opt[bits],
3941 pr->bf_size = bfsize;
3946 GNUNET_CONTAINER_multihashmap_get_multiple (query_request_map,
3948 &check_duplicate_request_peer,
3950 if (cdc.have != NULL)
3952 if (cdc.have->start_time.value + cdc.have->ttl >=
3953 pr->start_time.value + pr->ttl)
3955 /* existing request has higher TTL, drop new one! */
3956 cdc.have->priority += pr->priority;
3957 destroy_pending_request (pr);
3959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3960 "Have existing request with higher TTL, dropping new request.\n",
3961 GNUNET_i2s (other));
3963 GNUNET_STATISTICS_update (stats,
3964 gettext_noop ("# requests dropped due to higher-TTL request"),
3971 /* existing request has lower TTL, drop old one! */
3972 pr->priority += cdc.have->priority;
3973 /* Possible optimization: if we have applicable pending
3974 replies in 'cdc.have', we might want to move those over
3975 (this is a really rare special-case, so it is not clear
3976 that this would be worth it) */
3977 destroy_pending_request (cdc.have);
3978 /* keep processing 'pr'! */
3983 GNUNET_break (GNUNET_OK ==
3984 GNUNET_CONTAINER_multihashmap_put (query_request_map,
3987 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
3988 GNUNET_break (GNUNET_OK ==
3989 GNUNET_CONTAINER_multihashmap_put (peer_request_map,
3992 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
3994 pr->hnode = GNUNET_CONTAINER_heap_insert (requests_by_expiration_heap,
3996 pr->start_time.value + pr->ttl);
3998 GNUNET_STATISTICS_update (stats,
3999 gettext_noop ("# P2P searches received"),
4002 GNUNET_STATISTICS_update (stats,
4003 gettext_noop ("# P2P searches active"),
4007 /* calculate change in traffic preference */
4008 cps->inc_preference += pr->priority * 1000 + QUERY_BANDWIDTH_VALUE;
4009 /* process locally */
4010 if (type == GNUNET_BLOCK_TYPE_FS_DBLOCK)
4011 type = GNUNET_BLOCK_TYPE_ANY; /* to get on-demand as well */
4012 timeout = GNUNET_TIME_relative_multiply (BASIC_DATASTORE_REQUEST_DELAY,
4013 (pr->priority + 1));
4014 if (GNUNET_YES != pr->forward_only)
4015 pr->qe = GNUNET_DATASTORE_get (dsh,
4019 MAX_DATASTORE_QUEUE,
4021 &process_local_reply,
4024 GNUNET_STATISTICS_update (stats,
4025 gettext_noop ("# requests forwarded due to high load"),
4029 /* Are multiple results possible (and did we look locally)? If so, start processing remotely now! */
4032 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
4033 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
4034 /* only one result, wait for datastore */
4035 if (GNUNET_YES != pr->forward_only)
4038 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
4039 pr->task = GNUNET_SCHEDULER_add_now (sched,
4040 &forward_request_task,
4044 /* make sure we don't track too many requests */
4045 if (GNUNET_CONTAINER_heap_get_size (requests_by_expiration_heap) > max_pending_requests)
4047 pr = GNUNET_CONTAINER_heap_peek (requests_by_expiration_heap);
4048 GNUNET_assert (pr != NULL);
4049 destroy_pending_request (pr);
4055 /* **************************** CS GET Handling ************************ */
4059 * Handle START_SEARCH-message (search request from client).
4061 * @param cls closure
4062 * @param client identification of the client
4063 * @param message the actual message
4066 handle_start_search (void *cls,
4067 struct GNUNET_SERVER_Client *client,
4068 const struct GNUNET_MessageHeader *message)
4070 static GNUNET_HashCode all_zeros;
4071 const struct SearchMessage *sm;
4072 struct ClientList *cl;
4073 struct ClientRequestList *crl;
4074 struct PendingRequest *pr;
4077 enum GNUNET_BLOCK_Type type;
4079 msize = ntohs (message->size);
4080 if ( (msize < sizeof (struct SearchMessage)) ||
4081 (0 != (msize - sizeof (struct SearchMessage)) % sizeof (GNUNET_HashCode)) )
4084 GNUNET_SERVER_receive_done (client,
4088 GNUNET_STATISTICS_update (stats,
4089 gettext_noop ("# client searches received"),
4092 sc = (msize - sizeof (struct SearchMessage)) / sizeof (GNUNET_HashCode);
4093 sm = (const struct SearchMessage*) message;
4094 type = ntohl (sm->type);
4096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4097 "Received request for `%s' of type %u from local client\n",
4098 GNUNET_h2s (&sm->query),
4099 (unsigned int) type);
4102 while ( (cl != NULL) &&
4103 (cl->client != client) )
4107 cl = GNUNET_malloc (sizeof (struct ClientList));
4108 cl->client = client;
4109 GNUNET_SERVER_client_keep (client);
4110 cl->next = client_list;
4113 /* detect duplicate KBLOCK requests */
4114 if ( (type == GNUNET_BLOCK_TYPE_FS_KBLOCK) ||
4115 (type == GNUNET_BLOCK_TYPE_FS_NBLOCK) ||
4116 (type == GNUNET_BLOCK_TYPE_ANY) )
4119 while ( (crl != NULL) &&
4120 ( (0 != memcmp (&crl->req->query,
4122 sizeof (GNUNET_HashCode))) ||
4123 (crl->req->type != type) ) )
4128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4129 "Have existing request, merging content-seen lists.\n");
4132 /* Duplicate request (used to send long list of
4133 known/blocked results); merge 'pr->replies_seen'
4134 and update bloom filter */
4135 GNUNET_array_grow (pr->replies_seen,
4136 pr->replies_seen_size,
4137 pr->replies_seen_off + sc);
4138 memcpy (&pr->replies_seen[pr->replies_seen_off],
4140 sc * sizeof (GNUNET_HashCode));
4141 pr->replies_seen_off += sc;
4142 refresh_bloomfilter (pr);
4143 GNUNET_STATISTICS_update (stats,
4144 gettext_noop ("# client searches updated (merged content seen list)"),
4147 GNUNET_SERVER_receive_done (client,
4152 GNUNET_STATISTICS_update (stats,
4153 gettext_noop ("# client searches active"),
4156 pr = GNUNET_malloc (sizeof (struct PendingRequest) +
4157 ((type == GNUNET_BLOCK_TYPE_FS_SBLOCK) ? sizeof(GNUNET_HashCode) : 0));
4158 crl = GNUNET_malloc (sizeof (struct ClientRequestList));
4159 memset (crl, 0, sizeof (struct ClientRequestList));
4160 crl->client_list = cl;
4161 GNUNET_CONTAINER_DLL_insert (cl->rl_head,
4166 pr->client_request_list = crl;
4167 GNUNET_array_grow (pr->replies_seen,
4168 pr->replies_seen_size,
4170 memcpy (pr->replies_seen,
4172 sc * sizeof (GNUNET_HashCode));
4173 pr->replies_seen_off = sc;
4174 pr->anonymity_level = ntohl (sm->anonymity_level);
4175 pr->start_time = GNUNET_TIME_absolute_get ();
4176 refresh_bloomfilter (pr);
4177 pr->query = sm->query;
4178 if (0 == (1 & ntohl (sm->options)))
4179 pr->local_only = GNUNET_NO;
4181 pr->local_only = GNUNET_YES;
4184 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
4185 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
4186 if (0 != memcmp (&sm->target,
4188 sizeof (GNUNET_HashCode)))
4189 pr->target_pid = GNUNET_PEER_intern ((const struct GNUNET_PeerIdentity*) &sm->target);
4191 case GNUNET_BLOCK_TYPE_FS_SBLOCK:
4192 pr->namespace = (GNUNET_HashCode*) &pr[1];
4193 memcpy (&pr[1], &sm->target, sizeof (GNUNET_HashCode));
4198 GNUNET_break (GNUNET_OK ==
4199 GNUNET_CONTAINER_multihashmap_put (query_request_map,
4202 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
4203 if (type == GNUNET_BLOCK_TYPE_FS_DBLOCK)
4204 type = GNUNET_BLOCK_TYPE_ANY; /* get on-demand blocks too! */
4205 pr->qe = GNUNET_DATASTORE_get (dsh,
4209 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
4210 &process_local_reply,
4215 /* **************************** Startup ************************ */
4218 * Process fs requests.
4220 * @param s scheduler to use
4221 * @param server the initialized server
4222 * @param c configuration to use
4225 main_init (struct GNUNET_SCHEDULER_Handle *s,
4226 struct GNUNET_SERVER_Handle *server,
4227 const struct GNUNET_CONFIGURATION_Handle *c)
4229 static const struct GNUNET_CORE_MessageHandler p2p_handlers[] =
4232 GNUNET_MESSAGE_TYPE_FS_GET, 0 },
4234 GNUNET_MESSAGE_TYPE_FS_PUT, 0 },
4235 { &handle_p2p_migration_stop,
4236 GNUNET_MESSAGE_TYPE_FS_MIGRATION_STOP,
4237 sizeof (struct MigrationStopMessage) },
4240 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
4241 {&GNUNET_FS_handle_index_start, NULL,
4242 GNUNET_MESSAGE_TYPE_FS_INDEX_START, 0},
4243 {&GNUNET_FS_handle_index_list_get, NULL,
4244 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_GET, sizeof(struct GNUNET_MessageHeader) },
4245 {&GNUNET_FS_handle_unindex, NULL, GNUNET_MESSAGE_TYPE_FS_UNINDEX,
4246 sizeof (struct UnindexMessage) },
4247 {&handle_start_search, NULL, GNUNET_MESSAGE_TYPE_FS_START_SEARCH,
4251 unsigned long long enc = 128;
4255 stats = GNUNET_STATISTICS_create (sched, "fs", cfg);
4256 min_migration_delay = GNUNET_TIME_UNIT_SECONDS;
4258 GNUNET_CONFIGURATION_get_value_number (cfg,
4260 "MAX_PENDING_REQUESTS",
4261 &max_pending_requests)) ||
4263 GNUNET_CONFIGURATION_get_value_number (cfg,
4265 "EXPECTED_NEIGHBOUR_COUNT",
4268 GNUNET_CONFIGURATION_get_value_time (cfg,
4270 "MIN_MIGRATION_DELAY",
4271 &min_migration_delay)) )
4273 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4274 _("Configuration fails to specify certain parameters, assuming default values."));
4276 connected_peers = GNUNET_CONTAINER_multihashmap_create (enc);
4277 query_request_map = GNUNET_CONTAINER_multihashmap_create (max_pending_requests);
4278 rt_entry_lifetime = GNUNET_LOAD_value_init ();
4279 peer_request_map = GNUNET_CONTAINER_multihashmap_create (enc);
4280 requests_by_expiration_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
4281 core = GNUNET_CORE_connect (sched,
4283 GNUNET_TIME_UNIT_FOREVER_REL,
4286 &peer_connect_handler,
4287 &peer_disconnect_handler,
4294 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4295 _("Failed to connect to `%s' service.\n"),
4297 GNUNET_CONTAINER_multihashmap_destroy (connected_peers);
4298 connected_peers = NULL;
4299 GNUNET_CONTAINER_multihashmap_destroy (query_request_map);
4300 query_request_map = NULL;
4301 GNUNET_LOAD_value_free (rt_entry_lifetime);
4302 rt_entry_lifetime = NULL;
4303 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
4304 requests_by_expiration_heap = NULL;
4305 GNUNET_CONTAINER_multihashmap_destroy (peer_request_map);
4306 peer_request_map = NULL;
4309 GNUNET_DATASTORE_disconnect (dsh, GNUNET_NO);
4312 return GNUNET_SYSERR;
4314 /* FIXME: distinguish between sending and storing in options? */
4315 if (active_migration)
4317 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4318 _("Content migration is enabled, will start to gather data\n"));
4319 consider_migration_gathering ();
4321 consider_dht_put_gathering (NULL);
4322 GNUNET_SERVER_disconnect_notify (server,
4323 &handle_client_disconnect,
4325 GNUNET_assert (GNUNET_OK ==
4326 GNUNET_CONFIGURATION_get_value_filename (cfg,
4330 GNUNET_DISK_directory_create (trustDirectory);
4331 GNUNET_SCHEDULER_add_with_priority (sched,
4332 GNUNET_SCHEDULER_PRIORITY_HIGH,
4333 &cron_flush_trust, NULL);
4336 GNUNET_SERVER_add_handlers (server, handlers);
4337 GNUNET_SCHEDULER_add_delayed (sched,
4338 GNUNET_TIME_UNIT_FOREVER_REL,
4346 * Process fs requests.
4348 * @param cls closure
4349 * @param sched scheduler to use
4350 * @param server the initialized server
4351 * @param cfg configuration to use
4355 struct GNUNET_SCHEDULER_Handle *sched,
4356 struct GNUNET_SERVER_Handle *server,
4357 const struct GNUNET_CONFIGURATION_Handle *cfg)
4359 active_migration = GNUNET_CONFIGURATION_get_value_yesno (cfg,
4362 dsh = GNUNET_DATASTORE_connect (cfg,
4366 GNUNET_SCHEDULER_shutdown (sched);
4369 datastore_get_load = GNUNET_LOAD_value_init ();
4370 datastore_put_load = GNUNET_LOAD_value_init ();
4371 block_cfg = GNUNET_CONFIGURATION_create ();
4372 GNUNET_CONFIGURATION_set_value_string (block_cfg,
4376 block_ctx = GNUNET_BLOCK_context_create (block_cfg);
4377 GNUNET_assert (NULL != block_ctx);
4378 dht_handle = GNUNET_DHT_connect (sched,
4381 if ( (GNUNET_OK != GNUNET_FS_indexing_init (sched, cfg, dsh)) ||
4382 (GNUNET_OK != main_init (sched, server, cfg)) )
4384 GNUNET_SCHEDULER_shutdown (sched);
4385 GNUNET_DATASTORE_disconnect (dsh, GNUNET_NO);
4387 GNUNET_DHT_disconnect (dht_handle);
4389 GNUNET_BLOCK_context_destroy (block_ctx);
4391 GNUNET_CONFIGURATION_destroy (block_cfg);
4393 GNUNET_LOAD_value_free (datastore_get_load);
4394 datastore_get_load = NULL;
4395 GNUNET_LOAD_value_free (datastore_put_load);
4396 datastore_put_load = NULL;
4403 * The main function for the fs service.
4405 * @param argc number of arguments from the command line
4406 * @param argv command line arguments
4407 * @return 0 ok, 1 on error
4410 main (int argc, char *const *argv)
4412 return (GNUNET_OK ==
4413 GNUNET_SERVICE_run (argc,
4416 GNUNET_SERVICE_OPTION_NONE,
4417 &run, NULL)) ? 0 : 1;
4420 /* end of gnunet-service-fs.c */