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
31 #include "gnunet_constants.h"
32 #include "gnunet_core_service.h"
33 #include "gnunet_dht_service.h"
34 #include "gnunet_datastore_service.h"
35 #include "gnunet_load_lib.h"
36 #include "gnunet_peer_lib.h"
37 #include "gnunet_protocols.h"
38 #include "gnunet_signatures.h"
39 #include "gnunet_statistics_service.h"
40 #include "gnunet_util_lib.h"
41 #include "gnunet-service-fs_indexing.h"
44 #define DEBUG_FS GNUNET_NO
47 * Should we introduce random latency in processing? Required for proper
48 * implementation of GAP, but can be disabled for performance evaluation of
49 * the basic routing algorithm.
51 * Note that with delays enabled, performance can be significantly lower
52 * (several orders of magnitude in 2-peer test runs); if you want to
53 * measure throughput of other components, set this to NO. Also, you
54 * might want to consider changing 'RETRY_PROBABILITY_INV' to 1 for
55 * a rather wasteful mode of operation (that might still get the highest
56 * throughput overall).
58 * Performance measurements (for 50 MB file, 2 peers):
60 * - Without delays: 3300 kb/s
61 * - With delays: 101 kb/s
63 #define SUPPORT_DELAYS GNUNET_NO
66 * Size for the hash map for DHT requests from the FS
67 * service. Should be about the number of concurrent
68 * DHT requests we plan to make.
70 #define FS_DHT_HT_SIZE 1024
73 * At what frequency should our datastore load decrease
74 * automatically (since if we don't use it, clearly the
75 * load must be going down).
77 #define DATASTORE_LOAD_AUTODECLINE GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 250)
80 * How often do we flush trust values to disk?
82 #define TRUST_FLUSH_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
85 * How often do we at most PUT content into the DHT?
87 #define MAX_DHT_PUT_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
90 * Inverse of the probability that we will submit the same query
91 * to the same peer again. If the same peer already got the query
92 * repeatedly recently, the probability is multiplied by the inverse
93 * of this number each time. Note that we only try about every TTL_DECREMENT/2
94 * plus MAX_CORK_DELAY (so roughly every 3.5s).
96 * Note that this factor is a key influence to performance in small
97 * networks (especially test networks of 2 peers) because if there is
98 * only a single peer with the data, this value will determine how
99 * soon we might re-try. For example, a value of 3 can result in
100 * 1.7 MB/s transfer rates for a 10 MB file when a value of 1 would
101 * give us 5 MB/s. OTOH, obviously re-trying the same peer can be
102 * rather inefficient in larger networks, hence picking 1 is in
103 * general not the best choice.
105 * Performance measurements (for 50 MB file, 2 peers, no delays):
107 * - 1: 3300 kb/s (consistently)
108 * - 3: 2046 kb/s, 754 kb/s, 3490 kb/s
109 * - 5: 759 kb/s, 968 kb/s, 1160 kb/s
111 * Note that this does NOT mean that the value should be 1 since
112 * a 2-peer network is far from representative here (and this fails
113 * to take into consideration bandwidth wasted by repeatedly
114 * sending queries to peers that don't have the content). Also,
115 * it is expected that higher values lead to more inconsistent
116 * measurements since this only affects lost messages towards the
117 * end of the download.
119 * Finally, we should probably consider changing this and making
120 * it dependent on the number of connected peers or a related
121 * metric (bad magic constants...).
123 #define RETRY_PROBABILITY_INV 1
126 * What is the maximum delay for a P2P FS message (in our interaction
127 * with core)? FS-internal delays are another story. The value is
128 * chosen based on the 32k block size. Given that peers typcially
129 * have at least 1 kb/s bandwidth, 45s waits give us a chance to
130 * transmit one message even to the lowest-bandwidth peers.
132 #define MAX_TRANSMIT_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 45)
135 * Maximum number of requests (from other peers, overall) that we're
136 * willing to have pending at any given point in time. Can be changed
137 * via the configuration file (32k is just the default).
139 static unsigned long long max_pending_requests = (32 * 1024);
143 * Information we keep for each pending reply. The
144 * actual message follows at the end of this struct.
146 struct PendingMessage;
149 * Function called upon completion of a transmission.
152 * @param pid ID of receiving peer, 0 on transmission error
154 typedef void (*TransmissionContinuation)(void * cls,
155 GNUNET_PEER_Id tpid);
159 * Information we keep for each pending message (GET/PUT). The
160 * actual message follows at the end of this struct.
162 struct PendingMessage
165 * This is a doubly-linked list of messages to the same peer.
167 struct PendingMessage *next;
170 * This is a doubly-linked list of messages to the same peer.
172 struct PendingMessage *prev;
175 * Entry in pending message list for this pending message.
177 struct PendingMessageList *pml;
180 * Function to call immediately once we have transmitted this
183 TransmissionContinuation cont;
191 * Do not transmit this pending message until this deadline.
193 struct GNUNET_TIME_Absolute delay_until;
196 * Size of the reply; actual reply message follows
197 * at the end of this struct.
202 * How important is this message for us?
210 * Information about a peer that we are connected to.
211 * We track data that is useful for determining which
212 * peers should receive our requests. We also keep
213 * a list of messages to transmit to this peer.
219 * List of the last clients for which this peer successfully
222 struct GNUNET_SERVER_Client *last_client_replies[CS2P_SUCCESS_LIST_SIZE];
225 * List of the last PIDs for which
226 * this peer successfully answered a query;
227 * We use 0 to indicate no successful reply.
229 GNUNET_PEER_Id last_p2p_replies[P2P_SUCCESS_LIST_SIZE];
232 * Average delay between sending the peer a request and
233 * getting a reply (only calculated over the requests for
234 * which we actually got a reply). Calculated
235 * as a moving average: new_delay = ((n-1)*last_delay+curr_delay) / n
237 struct GNUNET_TIME_Relative avg_delay;
240 * Point in time until which this peer does not want us to migrate content
243 struct GNUNET_TIME_Absolute migration_blocked;
246 * Time until when we blocked this peer from migrating
249 struct GNUNET_TIME_Absolute last_migration_block;
252 * Transmission times for the last MAX_QUEUE_PER_PEER
253 * requests for this peer. Used as a ring buffer, current
254 * offset is stored in 'last_request_times_off'. If the
255 * oldest entry is more recent than the 'avg_delay', we should
256 * not send any more requests right now.
258 struct GNUNET_TIME_Absolute last_request_times[MAX_QUEUE_PER_PEER];
261 * Handle for an active request for transmission to this
264 struct GNUNET_CORE_TransmitHandle *cth;
267 * Messages (replies, queries, content migration) we would like to
268 * send to this peer in the near future. Sorted by priority, head.
270 struct PendingMessage *pending_messages_head;
273 * Messages (replies, queries, content migration) we would like to
274 * send to this peer in the near future. Sorted by priority, tail.
276 struct PendingMessage *pending_messages_tail;
279 * How long does it typically take for us to transmit a message
280 * to this peer? (delay between the request being issued and
281 * the callback being invoked).
283 struct GNUNET_LOAD_Value *transmission_delay;
286 * Context of our GNUNET_CORE_peer_change_preference call (or NULL).
288 struct GNUNET_CORE_InformationRequestContext *irc;
291 * Request for which 'irc' is currently active (or NULL).
293 struct PendingRequest *pr;
296 * Time when the last transmission request was issued.
298 struct GNUNET_TIME_Absolute last_transmission_request_start;
301 * ID of delay task for scheduling transmission.
303 GNUNET_SCHEDULER_TaskIdentifier delayed_transmission_request_task;
306 * Average priority of successful replies. Calculated
307 * as a moving average: new_avg = ((n-1)*last_avg+curr_prio) / n
312 * Increase in traffic preference still to be submitted
313 * to the core service for this peer.
315 uint64_t inc_preference;
318 * Trust rating for this peer
323 * Trust rating for this peer on disk.
328 * The peer's identity.
333 * Size of the linked list of 'pending_messages'.
335 unsigned int pending_requests;
338 * Which offset in "last_p2p_replies" will be updated next?
339 * (we go round-robin).
341 unsigned int last_p2p_replies_woff;
344 * Which offset in "last_client_replies" will be updated next?
345 * (we go round-robin).
347 unsigned int last_client_replies_woff;
350 * Current offset into 'last_request_times' ring buffer.
352 unsigned int last_request_times_off;
358 * Information we keep for each pending request. We should try to
359 * keep this struct as small as possible since its memory consumption
360 * is key to how many requests we can have pending at once.
362 struct PendingRequest;
366 * Doubly-linked list of requests we are performing
367 * on behalf of the same client.
369 struct ClientRequestList
373 * This is a doubly-linked list.
375 struct ClientRequestList *next;
378 * This is a doubly-linked list.
380 struct ClientRequestList *prev;
383 * Request this entry represents.
385 struct PendingRequest *req;
388 * Client list this request belongs to.
390 struct ClientList *client_list;
396 * Replies to be transmitted to the client. The actual
397 * response message is allocated after this struct.
399 struct ClientResponseMessage
402 * This is a doubly-linked list.
404 struct ClientResponseMessage *next;
407 * This is a doubly-linked list.
409 struct ClientResponseMessage *prev;
412 * Client list entry this response belongs to.
414 struct ClientList *client_list;
417 * Number of bytes in the response.
424 * Linked list of clients we are performing requests
430 * This is a linked list.
432 struct ClientList *next;
435 * ID of a client making a request, NULL if this entry is for a
438 struct GNUNET_SERVER_Client *client;
441 * Head of list of requests performed on behalf
442 * of this client right now.
444 struct ClientRequestList *rl_head;
447 * Tail of list of requests performed on behalf
448 * of this client right now.
450 struct ClientRequestList *rl_tail;
453 * Head of linked list of responses.
455 struct ClientResponseMessage *res_head;
458 * Tail of linked list of responses.
460 struct ClientResponseMessage *res_tail;
463 * Context for sending replies.
465 struct GNUNET_CONNECTION_TransmitHandle *th;
471 * Information about a peer that we have forwarded this
472 * request to already.
474 struct UsedTargetEntry
477 * What was the last time we have transmitted this request to this
480 struct GNUNET_TIME_Absolute last_request_time;
483 * How often have we transmitted this request to this peer?
485 unsigned int num_requests;
488 * PID of the target peer.
499 * Doubly-linked list of messages we are performing
500 * due to a pending request.
502 struct PendingMessageList
506 * This is a doubly-linked list of messages on behalf of the same request.
508 struct PendingMessageList *next;
511 * This is a doubly-linked list of messages on behalf of the same request.
513 struct PendingMessageList *prev;
516 * Message this entry represents.
518 struct PendingMessage *pm;
521 * Request this entry belongs to.
523 struct PendingRequest *req;
526 * Peer this message is targeted for.
528 struct ConnectedPeer *target;
534 * Information we keep for each pending request. We should try to
535 * keep this struct as small as possible since its memory consumption
536 * is key to how many requests we can have pending at once.
538 struct PendingRequest
542 * If this request was made by a client, this is our entry in the
543 * client request list; otherwise NULL.
545 struct ClientRequestList *client_request_list;
548 * Entry of peer responsible for this entry (if this request
549 * was made by a peer).
551 struct ConnectedPeer *cp;
554 * If this is a namespace query, pointer to the hash of the public
555 * key of the namespace; otherwise NULL. Pointer will be to the
556 * end of this struct (so no need to free it).
558 const GNUNET_HashCode *namespace;
561 * Bloomfilter we use to filter out replies that we don't care about
562 * (anymore). NULL as long as we are interested in all replies.
564 struct GNUNET_CONTAINER_BloomFilter *bf;
567 * Reference to DHT get operation for this request (or NULL).
569 struct GNUNET_DHT_GetHandle *dht_get;
572 * Context of our GNUNET_CORE_peer_change_preference call.
574 struct ConnectedPeer *pirc;
577 * Hash code of all replies that we have seen so far (only valid
578 * if client is not NULL since we only track replies like this for
581 GNUNET_HashCode *replies_seen;
584 * Node in the heap representing this entry; NULL
585 * if we have no heap node.
587 struct GNUNET_CONTAINER_HeapNode *hnode;
590 * Head of list of messages being performed on behalf of this
593 struct PendingMessageList *pending_head;
596 * Tail of list of messages being performed on behalf of this
599 struct PendingMessageList *pending_tail;
602 * When did we first see this request (form this peer), or, if our
603 * client is initiating, when did we last initiate a search?
605 struct GNUNET_TIME_Absolute start_time;
608 * The query that this request is for.
610 GNUNET_HashCode query;
613 * The task responsible for transmitting queries
616 GNUNET_SCHEDULER_TaskIdentifier task;
619 * (Interned) Peer identifier that identifies a preferred target
622 GNUNET_PEER_Id target_pid;
625 * (Interned) Peer identifiers of peers that have already
626 * received our query for this content.
628 struct UsedTargetEntry *used_targets;
631 * Our entry in the queue (non-NULL while we wait for our
632 * turn to interact with the local database).
634 struct GNUNET_DATASTORE_QueueEntry *qe;
637 * Size of the 'bf' (in bytes).
642 * Desired anonymity level; only valid for requests from a local client.
644 uint32_t anonymity_level;
647 * How many entries in "used_targets" are actually valid?
649 unsigned int used_targets_off;
652 * How long is the "used_targets" array?
654 unsigned int used_targets_size;
657 * Number of results found for this request.
659 unsigned int results_found;
662 * How many entries in "replies_seen" are actually valid?
664 unsigned int replies_seen_off;
667 * How long is the "replies_seen" array?
669 unsigned int replies_seen_size;
672 * Priority with which this request was made. If one of our clients
673 * made the request, then this is the current priority that we are
674 * using when initiating the request. This value is used when
675 * we decide to reward other peers with trust for providing a reply.
680 * Priority points left for us to spend when forwarding this request
683 uint32_t remaining_priority;
686 * Number to mingle hashes for bloom-filter tests with.
691 * TTL with which we saw this request (or, if we initiated, TTL that
692 * we used for the request).
697 * Type of the content that this request is for.
699 enum GNUNET_BLOCK_Type type;
702 * Remove this request after transmission of the current response.
707 * GNUNET_YES if we should not forward this request to other peers.
712 * GNUNET_YES if we should not forward this request to other peers.
720 * Block that is ready for migration to other peers. Actual data is at the end of the block.
722 struct MigrationReadyBlock
726 * This is a doubly-linked list.
728 struct MigrationReadyBlock *next;
731 * This is a doubly-linked list.
733 struct MigrationReadyBlock *prev;
736 * Query for the block.
738 GNUNET_HashCode query;
741 * When does this block expire?
743 struct GNUNET_TIME_Absolute expiration;
746 * Peers we would consider forwarding this
747 * block to. Zero for empty entries.
749 GNUNET_PEER_Id target_list[MIGRATION_LIST_SIZE];
757 * Number of targets already used.
759 unsigned int used_targets;
764 enum GNUNET_BLOCK_Type type;
769 * Our connection to the datastore.
771 static struct GNUNET_DATASTORE_Handle *dsh;
776 static struct GNUNET_BLOCK_Context *block_ctx;
779 * Our block configuration.
781 static struct GNUNET_CONFIGURATION_Handle *block_cfg;
786 static const struct GNUNET_CONFIGURATION_Handle *cfg;
789 * Map of peer identifiers to "struct ConnectedPeer" (for that peer).
791 static struct GNUNET_CONTAINER_MultiHashMap *connected_peers;
794 * Map of peer identifiers to "struct PendingRequest" (for that peer).
796 static struct GNUNET_CONTAINER_MultiHashMap *peer_request_map;
799 * Map of query identifiers to "struct PendingRequest" (for that query).
801 static struct GNUNET_CONTAINER_MultiHashMap *query_request_map;
804 * Heap with the request that will expire next at the top. Contains
805 * pointers of type "struct PendingRequest*"; these will *also* be
806 * aliased from the "requests_by_peer" data structures and the
807 * "requests_by_query" table. Note that requests from our clients
808 * don't expire and are thus NOT in the "requests_by_expiration"
809 * (or the "requests_by_peer" tables).
811 static struct GNUNET_CONTAINER_Heap *requests_by_expiration_heap;
814 * Handle for reporting statistics.
816 static struct GNUNET_STATISTICS_Handle *stats;
819 * Linked list of clients we are currently processing requests for.
821 static struct ClientList *client_list;
824 * Pointer to handle to the core service (points to NULL until we've
827 static struct GNUNET_CORE_Handle *core;
830 * Head of linked list of blocks that can be migrated.
832 static struct MigrationReadyBlock *mig_head;
835 * Tail of linked list of blocks that can be migrated.
837 static struct MigrationReadyBlock *mig_tail;
840 * Request to datastore for migration (or NULL).
842 static struct GNUNET_DATASTORE_QueueEntry *mig_qe;
845 * Request to datastore for DHT PUTs (or NULL).
847 static struct GNUNET_DATASTORE_QueueEntry *dht_qe;
850 * Type we will request for the next DHT PUT round from the datastore.
852 static enum GNUNET_BLOCK_Type dht_put_type = GNUNET_BLOCK_TYPE_FS_KBLOCK;
855 * Where do we store trust information?
857 static char *trustDirectory;
860 * ID of task that collects blocks for migration.
862 static GNUNET_SCHEDULER_TaskIdentifier mig_task;
865 * ID of task that collects blocks for DHT PUTs.
867 static GNUNET_SCHEDULER_TaskIdentifier dht_task;
870 * What is the maximum frequency at which we are allowed to
871 * poll the datastore for migration content?
873 static struct GNUNET_TIME_Relative min_migration_delay;
876 * Handle for DHT operations.
878 static struct GNUNET_DHT_Handle *dht_handle;
881 * Size of the doubly-linked list of migration blocks.
883 static unsigned int mig_size;
886 * Are we allowed to migrate content to this peer.
888 static int active_to_migration;
891 * Are we allowed to push out content from this peer.
893 static int active_from_migration;
896 * How many entires with zero anonymity do we currently estimate
897 * to have in the database?
899 static unsigned int zero_anonymity_count_estimate;
902 * Typical priorities we're seeing from other peers right now. Since
903 * most priorities will be zero, this value is the weighted average of
904 * non-zero priorities seen "recently". In order to ensure that new
905 * values do not dramatically change the ratio, values are first
906 * "capped" to a reasonable range (+N of the current value) and then
907 * averaged into the existing value by a ratio of 1:N. Hence
908 * receiving the largest possible priority can still only raise our
909 * "current_priorities" by at most 1.
911 static double current_priorities;
914 * Datastore 'GET' load tracking.
916 static struct GNUNET_LOAD_Value *datastore_get_load;
919 * Datastore 'PUT' load tracking.
921 static struct GNUNET_LOAD_Value *datastore_put_load;
924 * How long do requests typically stay in the routing table?
926 static struct GNUNET_LOAD_Value *rt_entry_lifetime;
929 * We've just now completed a datastore request. Update our
930 * datastore load calculations.
932 * @param start time when the datastore request was issued
935 update_datastore_delays (struct GNUNET_TIME_Absolute start)
937 struct GNUNET_TIME_Relative delay;
939 delay = GNUNET_TIME_absolute_get_duration (start);
940 GNUNET_LOAD_update (datastore_get_load,
946 * Get the filename under which we would store the GNUNET_HELLO_Message
947 * for the given host and protocol.
948 * @return filename of the form DIRECTORY/HOSTID
951 get_trust_filename (const struct GNUNET_PeerIdentity *id)
953 struct GNUNET_CRYPTO_HashAsciiEncoded fil;
956 GNUNET_CRYPTO_hash_to_enc (&id->hashPubKey, &fil);
957 GNUNET_asprintf (&fn, "%s%s%s", trustDirectory, DIR_SEPARATOR_STR, &fil);
964 * Transmit messages by copying it to the target buffer
965 * "buf". "buf" will be NULL and "size" zero if the socket was closed
966 * for writing in the meantime. In that case, do nothing
967 * (the disconnect or shutdown handler will take care of the rest).
968 * If we were able to transmit messages and there are still more
969 * pending, ask core again for further calls to this function.
971 * @param cls closure, pointer to the 'struct ConnectedPeer*'
972 * @param size number of bytes available in buf
973 * @param buf where the callee should write the message
974 * @return number of bytes written to buf
977 transmit_to_peer (void *cls,
978 size_t size, void *buf);
981 /* ******************* clean up functions ************************ */
984 * Delete the given migration block.
986 * @param mb block to delete
989 delete_migration_block (struct MigrationReadyBlock *mb)
991 GNUNET_CONTAINER_DLL_remove (mig_head,
994 GNUNET_PEER_decrement_rcs (mb->target_list,
995 MIGRATION_LIST_SIZE);
1002 * Compare the distance of two peers to a key.
1005 * @param p1 first peer
1006 * @param p2 second peer
1007 * @return GNUNET_YES if P1 is closer to key than P2
1010 is_closer (const GNUNET_HashCode *key,
1011 const struct GNUNET_PeerIdentity *p1,
1012 const struct GNUNET_PeerIdentity *p2)
1014 return GNUNET_CRYPTO_hash_xorcmp (&p1->hashPubKey,
1021 * Consider migrating content to a given peer.
1023 * @param cls 'struct MigrationReadyBlock*' to select
1024 * targets for (or NULL for none)
1025 * @param key ID of the peer
1026 * @param value 'struct ConnectedPeer' of the peer
1027 * @return GNUNET_YES (always continue iteration)
1030 consider_migration (void *cls,
1031 const GNUNET_HashCode *key,
1034 struct MigrationReadyBlock *mb = cls;
1035 struct ConnectedPeer *cp = value;
1036 struct MigrationReadyBlock *pos;
1037 struct GNUNET_PeerIdentity cppid;
1038 struct GNUNET_PeerIdentity otherpid;
1039 struct GNUNET_PeerIdentity worstpid;
1044 /* consider 'cp' as a migration target for mb */
1045 if (GNUNET_TIME_absolute_get_remaining (cp->migration_blocked).rel_value > 0)
1046 return GNUNET_YES; /* peer has requested no migration! */
1049 GNUNET_PEER_resolve (cp->pid,
1051 repl = MIGRATION_LIST_SIZE;
1052 for (i=0;i<MIGRATION_LIST_SIZE;i++)
1054 if (mb->target_list[i] == 0)
1056 mb->target_list[i] = cp->pid;
1057 GNUNET_PEER_change_rc (mb->target_list[i], 1);
1058 repl = MIGRATION_LIST_SIZE;
1061 GNUNET_PEER_resolve (mb->target_list[i],
1063 if ( (repl == MIGRATION_LIST_SIZE) &&
1064 is_closer (&mb->query,
1069 worstpid = otherpid;
1071 else if ( (repl != MIGRATION_LIST_SIZE) &&
1072 (is_closer (&mb->query,
1077 worstpid = otherpid;
1080 if (repl != MIGRATION_LIST_SIZE)
1082 GNUNET_PEER_change_rc (mb->target_list[repl], -1);
1083 mb->target_list[repl] = cp->pid;
1084 GNUNET_PEER_change_rc (mb->target_list[repl], 1);
1088 /* consider scheduling transmission to cp for content migration */
1089 if (cp->cth != NULL)
1095 for (i=0;i<MIGRATION_LIST_SIZE;i++)
1097 if (cp->pid == pos->target_list[i])
1102 msize = GNUNET_MIN (msize,
1110 return GNUNET_YES; /* no content available */
1112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1113 "Trying to migrate at least %u bytes to peer `%s'\n",
1117 if (cp->delayed_transmission_request_task != GNUNET_SCHEDULER_NO_TASK)
1119 GNUNET_SCHEDULER_cancel (cp->delayed_transmission_request_task);
1120 cp->delayed_transmission_request_task = GNUNET_SCHEDULER_NO_TASK;
1123 = GNUNET_CORE_notify_transmit_ready (core,
1124 0, GNUNET_TIME_UNIT_FOREVER_REL,
1125 (const struct GNUNET_PeerIdentity*) key,
1126 msize + sizeof (struct PutMessage),
1134 * Task that is run periodically to obtain blocks for content
1138 * @param tc scheduler context (also unused)
1141 gather_migration_blocks (void *cls,
1142 const struct GNUNET_SCHEDULER_TaskContext *tc);
1148 * Task that is run periodically to obtain blocks for DHT PUTs.
1150 * @param cls type of blocks to gather
1151 * @param tc scheduler context (unused)
1154 gather_dht_put_blocks (void *cls,
1155 const struct GNUNET_SCHEDULER_TaskContext *tc);
1159 * If the migration task is not currently running, consider
1160 * (re)scheduling it with the appropriate delay.
1163 consider_migration_gathering ()
1165 struct GNUNET_TIME_Relative delay;
1171 if (mig_task != GNUNET_SCHEDULER_NO_TASK)
1173 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
1175 delay = GNUNET_TIME_relative_divide (delay,
1176 MAX_MIGRATION_QUEUE);
1177 delay = GNUNET_TIME_relative_max (delay,
1178 min_migration_delay);
1179 mig_task = GNUNET_SCHEDULER_add_delayed (delay,
1180 &gather_migration_blocks,
1186 * If the DHT PUT gathering task is not currently running, consider
1187 * (re)scheduling it with the appropriate delay.
1190 consider_dht_put_gathering (void *cls)
1192 struct GNUNET_TIME_Relative delay;
1198 if (dht_task != GNUNET_SCHEDULER_NO_TASK)
1200 if (zero_anonymity_count_estimate > 0)
1202 delay = GNUNET_TIME_relative_divide (GNUNET_DHT_DEFAULT_REPUBLISH_FREQUENCY,
1203 zero_anonymity_count_estimate);
1204 delay = GNUNET_TIME_relative_min (delay,
1209 /* if we have NO zero-anonymity content yet, wait 5 minutes for some to
1210 (hopefully) appear */
1211 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5);
1213 dht_task = GNUNET_SCHEDULER_add_delayed (delay,
1214 &gather_dht_put_blocks,
1220 * Process content offered for migration.
1222 * @param cls closure
1223 * @param key key for the content
1224 * @param size number of bytes in data
1225 * @param data content stored
1226 * @param type type of the content
1227 * @param priority priority of the content
1228 * @param anonymity anonymity-level for the content
1229 * @param expiration expiration time for the content
1230 * @param uid unique identifier for the datum;
1231 * maybe 0 if no unique identifier is available
1234 process_migration_content (void *cls,
1235 const GNUNET_HashCode * key,
1238 enum GNUNET_BLOCK_Type type,
1241 struct GNUNET_TIME_Absolute
1242 expiration, uint64_t uid)
1244 struct MigrationReadyBlock *mb;
1249 if (mig_size < MAX_MIGRATION_QUEUE)
1250 consider_migration_gathering ();
1253 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
1256 GNUNET_FS_handle_on_demand_block (key, size, data,
1257 type, priority, anonymity,
1259 &process_migration_content,
1262 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
1267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1268 "Retrieved block `%s' of type %u for migration\n",
1272 mb = GNUNET_malloc (sizeof (struct MigrationReadyBlock) + size);
1274 mb->expiration = expiration;
1277 memcpy (&mb[1], data, size);
1278 GNUNET_CONTAINER_DLL_insert_after (mig_head,
1283 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
1284 &consider_migration,
1286 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
1291 * Function called upon completion of the DHT PUT operation.
1294 dht_put_continuation (void *cls,
1295 const struct GNUNET_SCHEDULER_TaskContext *tc)
1297 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
1302 * Store content in DHT.
1304 * @param cls closure
1305 * @param key key for the content
1306 * @param size number of bytes in data
1307 * @param data content stored
1308 * @param type type of the content
1309 * @param priority priority of the content
1310 * @param anonymity anonymity-level for the content
1311 * @param expiration expiration time for the content
1312 * @param uid unique identifier for the datum;
1313 * maybe 0 if no unique identifier is available
1316 process_dht_put_content (void *cls,
1317 const GNUNET_HashCode * key,
1320 enum GNUNET_BLOCK_Type type,
1323 struct GNUNET_TIME_Absolute
1324 expiration, uint64_t uid)
1326 static unsigned int counter;
1327 static GNUNET_HashCode last_vhash;
1328 static GNUNET_HashCode vhash;
1333 consider_dht_put_gathering (cls);
1336 /* slightly funky code to estimate the total number of values with zero
1337 anonymity from the maximum observed length of a monotonically increasing
1338 sequence of hashes over the contents */
1339 GNUNET_CRYPTO_hash (data, size, &vhash);
1340 if (GNUNET_CRYPTO_hash_cmp (&vhash, &last_vhash) <= 0)
1342 if (zero_anonymity_count_estimate > 0)
1343 zero_anonymity_count_estimate /= 2;
1349 if (zero_anonymity_count_estimate < (1 << counter))
1350 zero_anonymity_count_estimate = (1 << counter);
1352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1353 "Retrieved block `%s' of type %u for DHT PUT\n",
1357 GNUNET_DHT_put (dht_handle,
1359 DEFAULT_PUT_REPLICATION,
1365 GNUNET_TIME_UNIT_FOREVER_REL,
1366 &dht_put_continuation,
1372 * Task that is run periodically to obtain blocks for content
1376 * @param tc scheduler context (also unused)
1379 gather_migration_blocks (void *cls,
1380 const struct GNUNET_SCHEDULER_TaskContext *tc)
1382 mig_task = GNUNET_SCHEDULER_NO_TASK;
1385 mig_qe = GNUNET_DATASTORE_get_random (dsh, 0, UINT_MAX,
1386 GNUNET_TIME_UNIT_FOREVER_REL,
1387 &process_migration_content, NULL);
1388 GNUNET_assert (mig_qe != NULL);
1394 * Task that is run periodically to obtain blocks for DHT PUTs.
1396 * @param cls type of blocks to gather
1397 * @param tc scheduler context (unused)
1400 gather_dht_put_blocks (void *cls,
1401 const struct GNUNET_SCHEDULER_TaskContext *tc)
1403 dht_task = GNUNET_SCHEDULER_NO_TASK;
1406 if (dht_put_type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
1407 dht_put_type = GNUNET_BLOCK_TYPE_FS_KBLOCK;
1408 dht_qe = GNUNET_DATASTORE_get_zero_anonymity (dsh, 0, UINT_MAX,
1409 GNUNET_TIME_UNIT_FOREVER_REL,
1411 &process_dht_put_content, NULL);
1412 GNUNET_assert (dht_qe != NULL);
1418 * We're done with a particular message list entry.
1419 * Free all associated resources.
1421 * @param pml entry to destroy
1424 destroy_pending_message_list_entry (struct PendingMessageList *pml)
1426 GNUNET_CONTAINER_DLL_remove (pml->req->pending_head,
1427 pml->req->pending_tail,
1429 GNUNET_CONTAINER_DLL_remove (pml->target->pending_messages_head,
1430 pml->target->pending_messages_tail,
1432 pml->target->pending_requests--;
1433 GNUNET_free (pml->pm);
1439 * Destroy the given pending message (and call the respective
1442 * @param pm message to destroy
1443 * @param tpid id of peer that the message was delivered to, or 0 for none
1446 destroy_pending_message (struct PendingMessage *pm,
1447 GNUNET_PEER_Id tpid)
1449 struct PendingMessageList *pml = pm->pml;
1450 TransmissionContinuation cont;
1454 cont_cls = pm->cont_cls;
1457 GNUNET_assert (pml->pm == pm);
1458 GNUNET_assert ( (tpid == 0) || (tpid == pml->target->pid) );
1459 destroy_pending_message_list_entry (pml);
1466 cont (cont_cls, tpid);
1471 * We're done processing a particular request.
1472 * Free all associated resources.
1474 * @param pr request to destroy
1477 destroy_pending_request (struct PendingRequest *pr)
1479 struct GNUNET_PeerIdentity pid;
1482 if (pr->hnode != NULL)
1484 GNUNET_CONTAINER_heap_remove_node (requests_by_expiration_heap,
1488 if (NULL == pr->client_request_list)
1490 GNUNET_STATISTICS_update (stats,
1491 gettext_noop ("# P2P searches active"),
1497 GNUNET_STATISTICS_update (stats,
1498 gettext_noop ("# client searches active"),
1503 GNUNET_CONTAINER_multihashmap_remove (query_request_map,
1507 GNUNET_LOAD_update (rt_entry_lifetime,
1508 GNUNET_TIME_absolute_get_duration (pr->start_time).rel_value);
1512 GNUNET_DATASTORE_cancel (pr->qe);
1515 if (pr->dht_get != NULL)
1517 GNUNET_DHT_get_stop (pr->dht_get);
1520 if (pr->client_request_list != NULL)
1522 GNUNET_CONTAINER_DLL_remove (pr->client_request_list->client_list->rl_head,
1523 pr->client_request_list->client_list->rl_tail,
1524 pr->client_request_list);
1525 GNUNET_free (pr->client_request_list);
1526 pr->client_request_list = NULL;
1530 GNUNET_PEER_resolve (pr->cp->pid,
1532 (void) GNUNET_CONTAINER_multihashmap_remove (peer_request_map,
1539 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
1542 if (pr->pirc != NULL)
1544 GNUNET_CORE_peer_change_preference_cancel (pr->pirc->irc);
1545 pr->pirc->irc = NULL;
1548 if (pr->replies_seen != NULL)
1550 GNUNET_free (pr->replies_seen);
1551 pr->replies_seen = NULL;
1553 if (pr->task != GNUNET_SCHEDULER_NO_TASK)
1555 GNUNET_SCHEDULER_cancel (pr->task);
1556 pr->task = GNUNET_SCHEDULER_NO_TASK;
1558 while (NULL != pr->pending_head)
1559 destroy_pending_message_list_entry (pr->pending_head);
1560 GNUNET_PEER_change_rc (pr->target_pid, -1);
1561 if (pr->used_targets != NULL)
1563 for (i=0;i<pr->used_targets_off;i++)
1564 GNUNET_PEER_change_rc (pr->used_targets[i].pid, -1);
1565 GNUNET_free (pr->used_targets);
1566 pr->used_targets_off = 0;
1567 pr->used_targets_size = 0;
1568 pr->used_targets = NULL;
1575 * Find latency information in 'atsi'.
1577 * @param atsi performance data
1578 * @return connection latency
1580 static struct GNUNET_TIME_Relative
1581 get_latency (const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1583 /* FIXME: extract latency data from 'atsi' */
1584 return GNUNET_TIME_UNIT_SECONDS;
1589 * Method called whenever a given peer connects.
1591 * @param cls closure, not used
1592 * @param peer peer identity this notification is about
1593 * @param atsi performance information
1596 peer_connect_handler (void *cls,
1598 GNUNET_PeerIdentity * peer,
1599 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1601 struct ConnectedPeer *cp;
1602 struct MigrationReadyBlock *pos;
1605 struct GNUNET_TIME_Relative latency;
1607 latency = get_latency (atsi);
1608 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
1615 cp = GNUNET_malloc (sizeof (struct ConnectedPeer));
1616 cp->transmission_delay = GNUNET_LOAD_value_init (latency);
1617 cp->pid = GNUNET_PEER_intern (peer);
1619 fn = get_trust_filename (peer);
1620 if ((GNUNET_DISK_file_test (fn) == GNUNET_YES) &&
1621 (sizeof (trust) == GNUNET_DISK_fn_read (fn, &trust, sizeof (trust))))
1622 cp->disk_trust = cp->trust = ntohl (trust);
1625 GNUNET_break (GNUNET_OK ==
1626 GNUNET_CONTAINER_multihashmap_put (connected_peers,
1629 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1634 (void) consider_migration (pos, &peer->hashPubKey, cp);
1641 * Method called whenever a given peer has a status change.
1643 * @param cls closure
1644 * @param peer peer identity this notification is about
1645 * @param bandwidth_in available amount of inbound bandwidth
1646 * @param bandwidth_out available amount of outbound bandwidth
1647 * @param timeout absolute time when this peer will time out
1648 * unless we see some further activity from it
1649 * @param atsi status information
1652 peer_status_handler (void *cls,
1654 GNUNET_PeerIdentity * peer,
1655 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
1656 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
1657 struct GNUNET_TIME_Absolute timeout,
1658 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1660 struct ConnectedPeer *cp;
1661 struct GNUNET_TIME_Relative latency;
1663 latency = get_latency (atsi);
1664 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
1671 GNUNET_LOAD_value_set_decline (cp->transmission_delay,
1678 * Increase the host credit by a value.
1680 * @param host which peer to change the trust value on
1681 * @param value is the int value by which the
1682 * host credit is to be increased or decreased
1683 * @returns the actual change in trust (positive or negative)
1686 change_host_trust (struct ConnectedPeer *host, int value)
1690 GNUNET_assert (host != NULL);
1693 if (host->trust + value < host->trust)
1695 value = UINT32_MAX - host->trust;
1696 host->trust = UINT32_MAX;
1699 host->trust += value;
1703 if (host->trust < -value)
1705 value = -host->trust;
1709 host->trust += value;
1716 * Write host-trust information to a file - flush the buffer entry!
1719 flush_trust (void *cls,
1720 const GNUNET_HashCode *key,
1723 struct ConnectedPeer *host = value;
1726 struct GNUNET_PeerIdentity pid;
1728 if (host->trust == host->disk_trust)
1729 return GNUNET_OK; /* unchanged */
1730 GNUNET_PEER_resolve (host->pid,
1732 fn = get_trust_filename (&pid);
1733 if (host->trust == 0)
1735 if ((0 != UNLINK (fn)) && (errno != ENOENT))
1736 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
1737 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
1741 trust = htonl (host->trust);
1742 if (sizeof(uint32_t) == GNUNET_DISK_fn_write (fn, &trust,
1744 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE
1745 | GNUNET_DISK_PERM_GROUP_READ | GNUNET_DISK_PERM_OTHER_READ))
1746 host->disk_trust = host->trust;
1753 * Call this method periodically to scan data/hosts for new hosts.
1756 cron_flush_trust (void *cls,
1757 const struct GNUNET_SCHEDULER_TaskContext *tc)
1760 if (NULL == connected_peers)
1762 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
1767 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1769 GNUNET_SCHEDULER_add_delayed (TRUST_FLUSH_FREQ, &cron_flush_trust, NULL);
1774 * Free (each) request made by the peer.
1776 * @param cls closure, points to peer that the request belongs to
1777 * @param key current key code
1778 * @param value value in the hash map
1779 * @return GNUNET_YES (we should continue to iterate)
1782 destroy_request (void *cls,
1783 const GNUNET_HashCode * key,
1786 const struct GNUNET_PeerIdentity * peer = cls;
1787 struct PendingRequest *pr = value;
1789 GNUNET_break (GNUNET_YES ==
1790 GNUNET_CONTAINER_multihashmap_remove (peer_request_map,
1793 destroy_pending_request (pr);
1799 * Method called whenever a peer disconnects.
1801 * @param cls closure, not used
1802 * @param peer peer identity this notification is about
1805 peer_disconnect_handler (void *cls,
1807 GNUNET_PeerIdentity * peer)
1809 struct ConnectedPeer *cp;
1810 struct PendingMessage *pm;
1812 struct MigrationReadyBlock *pos;
1813 struct MigrationReadyBlock *next;
1815 GNUNET_CONTAINER_multihashmap_get_multiple (peer_request_map,
1819 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
1823 for (i=0;i<CS2P_SUCCESS_LIST_SIZE;i++)
1825 if (NULL != cp->last_client_replies[i])
1827 GNUNET_SERVER_client_drop (cp->last_client_replies[i]);
1828 cp->last_client_replies[i] = NULL;
1831 GNUNET_break (GNUNET_YES ==
1832 GNUNET_CONTAINER_multihashmap_remove (connected_peers,
1835 if (cp->irc != NULL)
1837 GNUNET_CORE_peer_change_preference_cancel (cp->irc);
1839 cp->pr->pirc = NULL;
1843 /* remove this peer from migration considerations; schedule
1846 while (NULL != (pos = next))
1849 for (i=0;i<MIGRATION_LIST_SIZE;i++)
1851 if (pos->target_list[i] == cp->pid)
1853 GNUNET_PEER_change_rc (pos->target_list[i], -1);
1854 pos->target_list[i] = 0;
1857 if (pos->used_targets >= GNUNET_CONTAINER_multihashmap_size (connected_peers))
1859 delete_migration_block (pos);
1860 consider_migration_gathering ();
1863 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
1864 &consider_migration,
1867 GNUNET_PEER_change_rc (cp->pid, -1);
1868 GNUNET_PEER_decrement_rcs (cp->last_p2p_replies, P2P_SUCCESS_LIST_SIZE);
1869 if (NULL != cp->cth)
1871 GNUNET_CORE_notify_transmit_ready_cancel (cp->cth);
1874 if (cp->delayed_transmission_request_task != GNUNET_SCHEDULER_NO_TASK)
1876 GNUNET_SCHEDULER_cancel (cp->delayed_transmission_request_task);
1877 cp->delayed_transmission_request_task = GNUNET_SCHEDULER_NO_TASK;
1879 while (NULL != (pm = cp->pending_messages_head))
1880 destroy_pending_message (pm, 0 /* delivery failed */);
1881 GNUNET_LOAD_value_free (cp->transmission_delay);
1882 GNUNET_break (0 == cp->pending_requests);
1888 * Iterator over hash map entries that removes all occurences
1889 * of the given 'client' from the 'last_client_replies' of the
1890 * given connected peer.
1892 * @param cls closure, the 'struct GNUNET_SERVER_Client*' to remove
1893 * @param key current key code (unused)
1894 * @param value value in the hash map (the 'struct ConnectedPeer*' to change)
1895 * @return GNUNET_YES (we should continue to iterate)
1898 remove_client_from_last_client_replies (void *cls,
1899 const GNUNET_HashCode * key,
1902 struct GNUNET_SERVER_Client *client = cls;
1903 struct ConnectedPeer *cp = value;
1906 for (i=0;i<CS2P_SUCCESS_LIST_SIZE;i++)
1908 if (cp->last_client_replies[i] == client)
1910 GNUNET_SERVER_client_drop (cp->last_client_replies[i]);
1911 cp->last_client_replies[i] = NULL;
1919 * A client disconnected. Remove all of its pending queries.
1921 * @param cls closure, NULL
1922 * @param client identification of the client
1925 handle_client_disconnect (void *cls,
1926 struct GNUNET_SERVER_Client
1929 struct ClientList *pos;
1930 struct ClientList *prev;
1931 struct ClientRequestList *rcl;
1932 struct ClientResponseMessage *creply;
1938 while ( (NULL != pos) &&
1939 (pos->client != client) )
1945 return; /* no requests pending for this client */
1946 while (NULL != (rcl = pos->rl_head))
1948 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1949 "Destroying pending request `%s' on disconnect\n",
1950 GNUNET_h2s (&rcl->req->query));
1951 destroy_pending_request (rcl->req);
1954 client_list = pos->next;
1956 prev->next = pos->next;
1957 if (pos->th != NULL)
1959 GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th);
1962 while (NULL != (creply = pos->res_head))
1964 GNUNET_CONTAINER_DLL_remove (pos->res_head,
1967 GNUNET_free (creply);
1969 GNUNET_SERVER_client_drop (pos->client);
1971 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
1972 &remove_client_from_last_client_replies,
1978 * Iterator to free peer entries.
1980 * @param cls closure, unused
1981 * @param key current key code
1982 * @param value value in the hash map (peer entry)
1983 * @return GNUNET_YES (we should continue to iterate)
1986 clean_peer (void *cls,
1987 const GNUNET_HashCode * key,
1990 peer_disconnect_handler (NULL, (const struct GNUNET_PeerIdentity*) key);
1996 * Task run during shutdown.
2002 shutdown_task (void *cls,
2003 const struct GNUNET_SCHEDULER_TaskContext *tc)
2007 GNUNET_DATASTORE_cancel (mig_qe);
2012 GNUNET_DATASTORE_cancel (dht_qe);
2015 if (GNUNET_SCHEDULER_NO_TASK != mig_task)
2017 GNUNET_SCHEDULER_cancel (mig_task);
2018 mig_task = GNUNET_SCHEDULER_NO_TASK;
2020 if (GNUNET_SCHEDULER_NO_TASK != dht_task)
2022 GNUNET_SCHEDULER_cancel (dht_task);
2023 dht_task = GNUNET_SCHEDULER_NO_TASK;
2025 while (client_list != NULL)
2026 handle_client_disconnect (NULL,
2027 client_list->client);
2028 cron_flush_trust (NULL, NULL);
2029 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
2032 GNUNET_break (0 == GNUNET_CONTAINER_heap_get_size (requests_by_expiration_heap));
2033 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
2034 requests_by_expiration_heap = 0;
2035 GNUNET_CONTAINER_multihashmap_destroy (connected_peers);
2036 connected_peers = NULL;
2037 GNUNET_break (0 == GNUNET_CONTAINER_multihashmap_size (query_request_map));
2038 GNUNET_CONTAINER_multihashmap_destroy (query_request_map);
2039 query_request_map = NULL;
2040 GNUNET_LOAD_value_free (rt_entry_lifetime);
2041 rt_entry_lifetime = NULL;
2042 GNUNET_break (0 == GNUNET_CONTAINER_multihashmap_size (peer_request_map));
2043 GNUNET_CONTAINER_multihashmap_destroy (peer_request_map);
2044 peer_request_map = NULL;
2045 GNUNET_assert (NULL != core);
2046 GNUNET_CORE_disconnect (core);
2050 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
2055 GNUNET_DATASTORE_disconnect (dsh,
2059 while (mig_head != NULL)
2060 delete_migration_block (mig_head);
2061 GNUNET_assert (0 == mig_size);
2062 GNUNET_DHT_disconnect (dht_handle);
2064 GNUNET_LOAD_value_free (datastore_get_load);
2065 datastore_get_load = NULL;
2066 GNUNET_LOAD_value_free (datastore_put_load);
2067 datastore_put_load = NULL;
2068 GNUNET_BLOCK_context_destroy (block_ctx);
2070 GNUNET_CONFIGURATION_destroy (block_cfg);
2073 GNUNET_free_non_null (trustDirectory);
2074 trustDirectory = NULL;
2078 /* ******************* Utility functions ******************** */
2082 * We've had to delay a request for transmission to core, but now
2083 * we should be ready. Run it.
2085 * @param cls the 'struct ConnectedPeer' for which a request was delayed
2086 * @param tc task context (unused)
2089 delayed_transmission_request (void *cls,
2090 const struct GNUNET_SCHEDULER_TaskContext *tc)
2092 struct ConnectedPeer *cp = cls;
2093 struct GNUNET_PeerIdentity pid;
2094 struct PendingMessage *pm;
2096 pm = cp->pending_messages_head;
2097 cp->delayed_transmission_request_task = GNUNET_SCHEDULER_NO_TASK;
2098 GNUNET_assert (cp->cth == NULL);
2101 GNUNET_PEER_resolve (cp->pid,
2103 cp->last_transmission_request_start = GNUNET_TIME_absolute_get ();
2104 cp->cth = GNUNET_CORE_notify_transmit_ready (core,
2106 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
2115 * Transmit messages by copying it to the target buffer
2116 * "buf". "buf" will be NULL and "size" zero if the socket was closed
2117 * for writing in the meantime. In that case, do nothing
2118 * (the disconnect or shutdown handler will take care of the rest).
2119 * If we were able to transmit messages and there are still more
2120 * pending, ask core again for further calls to this function.
2122 * @param cls closure, pointer to the 'struct ConnectedPeer*'
2123 * @param size number of bytes available in buf
2124 * @param buf where the callee should write the message
2125 * @return number of bytes written to buf
2128 transmit_to_peer (void *cls,
2129 size_t size, void *buf)
2131 struct ConnectedPeer *cp = cls;
2133 struct PendingMessage *pm;
2134 struct PendingMessage *next_pm;
2135 struct GNUNET_TIME_Absolute now;
2136 struct GNUNET_TIME_Relative min_delay;
2137 struct MigrationReadyBlock *mb;
2138 struct MigrationReadyBlock *next;
2139 struct PutMessage migm;
2142 struct GNUNET_PeerIdentity pid;
2148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2149 "Dropping message, core too busy.\n");
2151 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2152 "Dropping message, core too busy.\n");
2153 GNUNET_LOAD_update (cp->transmission_delay,
2156 if (NULL != (pm = cp->pending_messages_head))
2158 GNUNET_CONTAINER_DLL_remove (cp->pending_messages_head,
2159 cp->pending_messages_tail,
2161 cp->pending_requests--;
2162 destroy_pending_message (pm, 0);
2164 if (NULL != (pm = cp->pending_messages_head))
2166 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == cp->delayed_transmission_request_task);
2167 min_delay = GNUNET_TIME_absolute_get_remaining (pm->delay_until);
2168 cp->delayed_transmission_request_task
2169 = GNUNET_SCHEDULER_add_delayed (min_delay,
2170 &delayed_transmission_request,
2175 GNUNET_LOAD_update (cp->transmission_delay,
2176 GNUNET_TIME_absolute_get_duration (cp->last_transmission_request_start).rel_value);
2177 now = GNUNET_TIME_absolute_get ();
2179 min_delay = GNUNET_TIME_UNIT_FOREVER_REL;
2180 next_pm = cp->pending_messages_head;
2181 while ( (NULL != (pm = next_pm) ) &&
2182 (pm->msize <= size) )
2185 if (pm->delay_until.abs_value > now.abs_value)
2187 min_delay = GNUNET_TIME_relative_min (min_delay,
2188 GNUNET_TIME_absolute_get_remaining (pm->delay_until));
2191 memcpy (&cbuf[msize], &pm[1], pm->msize);
2194 if (NULL == pm->pml)
2196 GNUNET_CONTAINER_DLL_remove (cp->pending_messages_head,
2197 cp->pending_messages_tail,
2199 cp->pending_requests--;
2201 destroy_pending_message (pm, cp->pid);
2204 min_delay = GNUNET_TIME_UNIT_ZERO;
2205 if (NULL != cp->pending_messages_head)
2207 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == cp->delayed_transmission_request_task);
2208 cp->delayed_transmission_request_task
2209 = GNUNET_SCHEDULER_add_delayed (min_delay,
2210 &delayed_transmission_request,
2215 GNUNET_PEER_resolve (cp->pid,
2218 while (NULL != (mb = next))
2221 for (i=0;i<MIGRATION_LIST_SIZE;i++)
2223 if ( (cp->pid == mb->target_list[i]) &&
2224 (mb->size + sizeof (migm) <= size) )
2226 GNUNET_PEER_change_rc (mb->target_list[i], -1);
2227 mb->target_list[i] = 0;
2229 memset (&migm, 0, sizeof (migm));
2230 migm.header.size = htons (sizeof (migm) + mb->size);
2231 migm.header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
2232 migm.type = htonl (mb->type);
2233 migm.expiration = GNUNET_TIME_absolute_hton (mb->expiration);
2234 memcpy (&cbuf[msize], &migm, sizeof (migm));
2235 msize += sizeof (migm);
2236 size -= sizeof (migm);
2237 memcpy (&cbuf[msize], &mb[1], mb->size);
2241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2242 "Pushing migration block `%s' (%u bytes) to `%s'\n",
2243 GNUNET_h2s (&mb->query),
2244 (unsigned int) mb->size,
2252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2253 "Migration block `%s' (%u bytes) is not on migration list for peer `%s'\n",
2254 GNUNET_h2s (&mb->query),
2255 (unsigned int) mb->size,
2260 if ( (mb->used_targets >= MIGRATION_TARGET_COUNT) ||
2261 (mb->used_targets >= GNUNET_CONTAINER_multihashmap_size (connected_peers)) )
2263 delete_migration_block (mb);
2264 consider_migration_gathering ();
2267 consider_migration (NULL,
2272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2273 "Transmitting %u bytes to peer with PID %u\n",
2274 (unsigned int) msize,
2275 (unsigned int) cp->pid);
2282 * Add a message to the set of pending messages for the given peer.
2284 * @param cp peer to send message to
2285 * @param pm message to queue
2286 * @param pr request on which behalf this message is being queued
2289 add_to_pending_messages_for_peer (struct ConnectedPeer *cp,
2290 struct PendingMessage *pm,
2291 struct PendingRequest *pr)
2293 struct PendingMessage *pos;
2294 struct PendingMessageList *pml;
2295 struct GNUNET_PeerIdentity pid;
2297 GNUNET_assert (pm->next == NULL);
2298 GNUNET_assert (pm->pml == NULL);
2301 pml = GNUNET_malloc (sizeof (struct PendingMessageList));
2306 GNUNET_CONTAINER_DLL_insert (pr->pending_head,
2310 pos = cp->pending_messages_head;
2311 while ( (pos != NULL) &&
2312 (pm->priority < pos->priority) )
2314 GNUNET_CONTAINER_DLL_insert_after (cp->pending_messages_head,
2315 cp->pending_messages_tail,
2318 cp->pending_requests++;
2319 if (cp->pending_requests > MAX_QUEUE_PER_PEER)
2321 GNUNET_STATISTICS_update (stats,
2322 gettext_noop ("# P2P searches discarded (queue length bound)"),
2325 destroy_pending_message (cp->pending_messages_tail, 0);
2327 GNUNET_PEER_resolve (cp->pid, &pid);
2328 if (NULL != cp->cth)
2330 GNUNET_CORE_notify_transmit_ready_cancel (cp->cth);
2333 if (cp->delayed_transmission_request_task != GNUNET_SCHEDULER_NO_TASK)
2335 GNUNET_SCHEDULER_cancel (cp->delayed_transmission_request_task);
2336 cp->delayed_transmission_request_task = GNUNET_SCHEDULER_NO_TASK;
2338 /* need to schedule transmission */
2339 cp->last_transmission_request_start = GNUNET_TIME_absolute_get ();
2340 cp->cth = GNUNET_CORE_notify_transmit_ready (core,
2341 cp->pending_messages_head->priority,
2344 cp->pending_messages_head->msize,
2347 if (cp->cth == NULL)
2350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2351 "Failed to schedule transmission with core!\n");
2353 GNUNET_STATISTICS_update (stats,
2354 gettext_noop ("# CORE transmission failures"),
2362 * Test if the DATABASE (GET) load on this peer is too high
2363 * to even consider processing the query at
2366 * @return GNUNET_YES if the load is too high to do anything (load high)
2367 * GNUNET_NO to process normally (load normal)
2368 * GNUNET_SYSERR to process for free (load low)
2371 test_get_load_too_high (uint32_t priority)
2375 ld = GNUNET_LOAD_get_load (datastore_get_load);
2377 return GNUNET_SYSERR;
2387 * Test if the DATABASE (PUT) load on this peer is too high
2388 * to even consider processing the query at
2391 * @return GNUNET_YES if the load is too high to do anything (load high)
2392 * GNUNET_NO to process normally (load normal or low)
2395 test_put_load_too_high (uint32_t priority)
2399 if (GNUNET_LOAD_get_average (datastore_put_load) < 50)
2400 return GNUNET_NO; /* very fast */
2401 ld = GNUNET_LOAD_get_load (datastore_put_load);
2402 if (ld < 2.0 * (1 + priority))
2404 GNUNET_STATISTICS_update (stats,
2405 gettext_noop ("# storage requests dropped due to high load"),
2412 /* ******************* Pending Request Refresh Task ******************** */
2417 * We use a random delay to make the timing of requests less
2418 * predictable. This function returns such a random delay. We add a base
2419 * delay of MAX_CORK_DELAY (1s).
2421 * FIXME: make schedule dependent on the specifics of the request?
2422 * Or bandwidth and number of connected peers and load?
2424 * @return random delay to use for some request, between 1s and 1000+TTL_DECREMENT ms
2426 static struct GNUNET_TIME_Relative
2427 get_processing_delay ()
2430 GNUNET_TIME_relative_add (GNUNET_CONSTANTS_MAX_CORK_DELAY,
2431 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
2432 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2438 * We're processing a GET request from another peer and have decided
2439 * to forward it to other peers. This function is called periodically
2440 * and should forward the request to other peers until we have all
2441 * possible replies. If we have transmitted the *only* reply to
2442 * the initiator we should destroy the pending request. If we have
2443 * many replies in the queue to the initiator, we should delay sending
2444 * out more queries until the reply queue has shrunk some.
2446 * @param cls our "struct ProcessGetContext *"
2450 forward_request_task (void *cls,
2451 const struct GNUNET_SCHEDULER_TaskContext *tc);
2455 * Function called after we either failed or succeeded
2456 * at transmitting a query to a peer.
2458 * @param cls the requests "struct PendingRequest*"
2459 * @param tpid ID of receiving peer, 0 on transmission error
2462 transmit_query_continuation (void *cls,
2463 GNUNET_PEER_Id tpid)
2465 struct PendingRequest *pr = cls;
2468 GNUNET_STATISTICS_update (stats,
2469 gettext_noop ("# queries scheduled for forwarding"),
2475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2476 "Transmission of request failed, will try again later.\n");
2478 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
2479 pr->task = GNUNET_SCHEDULER_add_delayed (get_processing_delay (),
2480 &forward_request_task,
2485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2486 "Transmitted query `%s'\n",
2487 GNUNET_h2s (&pr->query));
2489 GNUNET_STATISTICS_update (stats,
2490 gettext_noop ("# queries forwarded"),
2493 for (i=0;i<pr->used_targets_off;i++)
2494 if (pr->used_targets[i].pid == tpid)
2495 break; /* found match! */
2496 if (i == pr->used_targets_off)
2498 /* need to create new entry */
2499 if (pr->used_targets_off == pr->used_targets_size)
2500 GNUNET_array_grow (pr->used_targets,
2501 pr->used_targets_size,
2502 pr->used_targets_size * 2 + 2);
2503 GNUNET_PEER_change_rc (tpid, 1);
2504 pr->used_targets[pr->used_targets_off].pid = tpid;
2505 pr->used_targets[pr->used_targets_off].num_requests = 0;
2506 i = pr->used_targets_off++;
2508 pr->used_targets[i].last_request_time = GNUNET_TIME_absolute_get ();
2509 pr->used_targets[i].num_requests++;
2510 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
2511 pr->task = GNUNET_SCHEDULER_add_delayed (get_processing_delay (),
2512 &forward_request_task,
2518 * How many bytes should a bloomfilter be if we have already seen
2519 * entry_count responses? Note that BLOOMFILTER_K gives us the number
2520 * of bits set per entry. Furthermore, we should not re-size the
2521 * filter too often (to keep it cheap).
2523 * Since other peers will also add entries but not resize the filter,
2524 * we should generally pick a slightly larger size than what the
2525 * strict math would suggest.
2527 * @return must be a power of two and smaller or equal to 2^15.
2530 compute_bloomfilter_size (unsigned int entry_count)
2533 unsigned int ideal = (entry_count * BLOOMFILTER_K) / 4;
2534 uint16_t max = 1 << 15;
2536 if (entry_count > max)
2539 while ((size < max) && (size < ideal))
2548 * Recalculate our bloom filter for filtering replies. This function
2549 * will create a new bloom filter from scratch, so it should only be
2550 * called if we have no bloomfilter at all (and hence can create a
2551 * fresh one of minimal size without problems) OR if our peer is the
2552 * initiator (in which case we may resize to larger than mimimum size).
2554 * @param pr request for which the BF is to be recomputed
2557 refresh_bloomfilter (struct PendingRequest *pr)
2561 GNUNET_HashCode mhash;
2563 nsize = compute_bloomfilter_size (pr->replies_seen_off);
2564 if (nsize == pr->bf_size)
2565 return; /* size not changed */
2567 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
2568 pr->bf_size = nsize;
2569 pr->mingle = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, -1);
2570 pr->bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
2573 for (i=0;i<pr->replies_seen_off;i++)
2575 GNUNET_BLOCK_mingle_hash (&pr->replies_seen[i],
2578 GNUNET_CONTAINER_bloomfilter_add (pr->bf, &mhash);
2584 * Function called after we've tried to reserve a certain amount of
2585 * bandwidth for a reply. Check if we succeeded and if so send our
2588 * @param cls the requests "struct PendingRequest*"
2589 * @param peer identifies the peer
2590 * @param bpm_out set to the current bandwidth limit (sending) for this peer
2591 * @param amount set to the amount that was actually reserved or unreserved
2592 * @param preference current traffic preference for the given peer
2595 target_reservation_cb (void *cls,
2597 GNUNET_PeerIdentity * peer,
2598 struct GNUNET_BANDWIDTH_Value32NBO bpm_out,
2600 uint64_t preference)
2602 struct PendingRequest *pr = cls;
2603 struct ConnectedPeer *cp;
2604 struct PendingMessage *pm;
2605 struct GetMessage *gm;
2606 GNUNET_HashCode *ext;
2614 /* (3) transmit, update ttl/priority */
2615 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
2619 /* Peer must have just left */
2621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2622 "Selected peer disconnected!\n");
2624 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
2625 pr->task = GNUNET_SCHEDULER_add_delayed (get_processing_delay (),
2626 &forward_request_task,
2634 /* error in communication with core, try again later */
2635 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
2636 pr->task = GNUNET_SCHEDULER_add_delayed (get_processing_delay (),
2637 &forward_request_task,
2641 no_route = GNUNET_NO;
2647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2648 "Failed to reserve bandwidth for reply (got %d/%u bytes only)!\n",
2652 GNUNET_STATISTICS_update (stats,
2653 gettext_noop ("# reply bandwidth reservation requests failed"),
2656 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
2657 pr->task = GNUNET_SCHEDULER_add_delayed (get_processing_delay (),
2658 &forward_request_task,
2660 return; /* this target round failed */
2662 no_route = GNUNET_YES;
2665 GNUNET_STATISTICS_update (stats,
2666 gettext_noop ("# queries scheduled for forwarding"),
2669 for (i=0;i<pr->used_targets_off;i++)
2670 if (pr->used_targets[i].pid == cp->pid)
2672 GNUNET_STATISTICS_update (stats,
2673 gettext_noop ("# queries retransmitted to same target"),
2679 /* build message and insert message into priority queue */
2681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2682 "Forwarding request `%s' to `%4s'!\n",
2683 GNUNET_h2s (&pr->query),
2688 if (GNUNET_YES == no_route)
2690 bm |= GET_MESSAGE_BIT_RETURN_TO;
2693 if (pr->namespace != NULL)
2695 bm |= GET_MESSAGE_BIT_SKS_NAMESPACE;
2698 if (pr->target_pid != 0)
2700 bm |= GET_MESSAGE_BIT_TRANSMIT_TO;
2703 msize = sizeof (struct GetMessage) + pr->bf_size + k * sizeof(GNUNET_HashCode);
2704 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
2705 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
2707 gm = (struct GetMessage*) &pm[1];
2708 gm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_GET);
2709 gm->header.size = htons (msize);
2710 gm->type = htonl (pr->type);
2711 pr->remaining_priority /= 2;
2712 gm->priority = htonl (pr->remaining_priority);
2713 gm->ttl = htonl (pr->ttl);
2714 gm->filter_mutator = htonl(pr->mingle);
2715 gm->hash_bitmap = htonl (bm);
2716 gm->query = pr->query;
2717 ext = (GNUNET_HashCode*) &gm[1];
2719 if (GNUNET_YES == no_route)
2720 GNUNET_PEER_resolve (pr->cp->pid, (struct GNUNET_PeerIdentity*) &ext[k++]);
2721 if (pr->namespace != NULL)
2722 memcpy (&ext[k++], pr->namespace, sizeof (GNUNET_HashCode));
2723 if (pr->target_pid != 0)
2724 GNUNET_PEER_resolve (pr->target_pid, (struct GNUNET_PeerIdentity*) &ext[k++]);
2725 bfdata = (char *) &ext[k];
2727 GNUNET_CONTAINER_bloomfilter_get_raw_data (pr->bf,
2730 pm->cont = &transmit_query_continuation;
2732 cp->last_request_times[(cp->last_request_times_off++) % MAX_QUEUE_PER_PEER] = GNUNET_TIME_absolute_get ();
2733 add_to_pending_messages_for_peer (cp, pm, pr);
2738 * Closure used for "target_peer_select_cb".
2740 struct PeerSelectionContext
2743 * The request for which we are selecting
2746 struct PendingRequest *pr;
2749 * Current "prime" target.
2751 struct GNUNET_PeerIdentity target;
2754 * How much do we like this target?
2756 double target_score;
2759 * Does it make sense to we re-try quickly again?
2767 * Function called for each connected peer to determine
2768 * which one(s) would make good targets for forwarding.
2770 * @param cls closure (struct PeerSelectionContext)
2771 * @param key current key code (peer identity)
2772 * @param value value in the hash map (struct ConnectedPeer)
2773 * @return GNUNET_YES if we should continue to
2778 target_peer_select_cb (void *cls,
2779 const GNUNET_HashCode * key,
2782 struct PeerSelectionContext *psc = cls;
2783 struct ConnectedPeer *cp = value;
2784 struct PendingRequest *pr = psc->pr;
2785 struct GNUNET_TIME_Relative delay;
2790 /* 1) check that this peer is not the initiator */
2794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2795 "Skipping initiator in forwarding selection\n");
2797 return GNUNET_YES; /* skip */
2799 if (cp->irc != NULL)
2801 psc->fast_retry = GNUNET_YES;
2802 return GNUNET_YES; /* skip: already querying core about this peer for other reasons */
2805 /* 2) check if we have already (recently) forwarded to this peer */
2806 /* 2a) this particular request */
2808 for (i=0;i<pr->used_targets_off;i++)
2809 if (pr->used_targets[i].pid == cp->pid)
2811 pc = pr->used_targets[i].num_requests;
2812 GNUNET_assert (pc > 0);
2813 /* FIXME: make re-enabling a peer independent of how often
2814 this function is called??? */
2815 if (0 != GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2816 RETRY_PROBABILITY_INV * pc))
2819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2820 "NOT re-trying query that was previously transmitted %u times\n",
2823 return GNUNET_YES; /* skip */
2830 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2831 "Re-trying query that was previously transmitted %u times to this peer\n",
2835 /* 2b) many other requests to this peer */
2836 delay = GNUNET_TIME_absolute_get_duration (cp->last_request_times[cp->last_request_times_off % MAX_QUEUE_PER_PEER]);
2837 if (delay.rel_value <= cp->avg_delay.rel_value)
2840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2841 "NOT sending query since we send %u others to this peer in the last %llums\n",
2843 cp->avg_delay.rel_value);
2845 return GNUNET_YES; /* skip */
2848 /* 3) calculate how much we'd like to forward to this peer,
2849 starting with a random value that is strong enough
2850 to at least give any peer a chance sometimes
2851 (compared to the other factors that come later) */
2852 /* 3a) count successful (recent) routes from cp for same source */
2855 score = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2856 P2P_SUCCESS_LIST_SIZE);
2857 for (i=0;i<P2P_SUCCESS_LIST_SIZE;i++)
2858 if (cp->last_p2p_replies[i] == pr->cp->pid)
2859 score += 1.0; /* likely successful based on hot path */
2863 score = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2864 CS2P_SUCCESS_LIST_SIZE);
2865 for (i=0;i<CS2P_SUCCESS_LIST_SIZE;i++)
2866 if (cp->last_client_replies[i] == pr->client_request_list->client_list->client)
2867 score += 1.0; /* likely successful based on hot path */
2869 /* 3b) include latency */
2870 if (cp->avg_delay.rel_value < 4 * TTL_DECREMENT)
2871 score += 1.0; /* likely fast based on latency */
2872 /* 3c) include priorities */
2873 if (cp->avg_priority <= pr->remaining_priority / 2.0)
2874 score += 1.0; /* likely successful based on priorities */
2875 /* 3d) penalize for queue size */
2876 score -= (2.0 * cp->pending_requests / (double) MAX_QUEUE_PER_PEER);
2877 /* 3e) include peer proximity */
2878 score -= (2.0 * (GNUNET_CRYPTO_hash_distance_u32 (key,
2879 &pr->query)) / (double) UINT32_MAX);
2880 /* 4) super-bonus for being the known target */
2881 if (pr->target_pid == cp->pid)
2883 /* store best-fit in closure */
2884 score++; /* avoid zero */
2885 if (score > psc->target_score)
2887 psc->target_score = score;
2888 psc->target.hashPubKey = *key;
2891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2892 "Peer `%s' gets score %f for forwarding query, max is %8f\n",
2902 * The priority level imposes a bound on the maximum
2903 * value for the ttl that can be requested.
2905 * @param ttl_in requested ttl
2906 * @param prio given priority
2907 * @return ttl_in if ttl_in is below the limit,
2908 * otherwise the ttl-limit for the given priority
2911 bound_ttl (int32_t ttl_in, uint32_t prio)
2913 unsigned long long allowed;
2917 allowed = ((unsigned long long) prio) * TTL_DECREMENT / 1000;
2918 if (ttl_in > allowed)
2920 if (allowed >= (1 << 30))
2929 * Iterator called on each result obtained for a DHT
2930 * operation that expects a reply
2932 * @param cls closure
2933 * @param exp when will this value expire
2934 * @param key key of the result
2935 * @param get_path NULL-terminated array of pointers
2936 * to the peers on reverse GET path (or NULL if not recorded)
2937 * @param put_path NULL-terminated array of pointers
2938 * to the peers on the PUT path (or NULL if not recorded)
2939 * @param type type of the result
2940 * @param size number of bytes in data
2941 * @param data pointer to the result data
2944 process_dht_reply (void *cls,
2945 struct GNUNET_TIME_Absolute exp,
2946 const GNUNET_HashCode * key,
2947 const struct GNUNET_PeerIdentity * const *get_path,
2948 const struct GNUNET_PeerIdentity * const *put_path,
2949 enum GNUNET_BLOCK_Type type,
2955 * We're processing a GET request and have decided
2956 * to forward it to other peers. This function is called periodically
2957 * and should forward the request to other peers until we have all
2958 * possible replies. If we have transmitted the *only* reply to
2959 * the initiator we should destroy the pending request. If we have
2960 * many replies in the queue to the initiator, we should delay sending
2961 * out more queries until the reply queue has shrunk some.
2963 * @param cls our "struct ProcessGetContext *"
2967 forward_request_task (void *cls,
2968 const struct GNUNET_SCHEDULER_TaskContext *tc)
2970 struct PendingRequest *pr = cls;
2971 struct PeerSelectionContext psc;
2972 struct ConnectedPeer *cp;
2973 struct GNUNET_TIME_Relative delay;
2975 pr->task = GNUNET_SCHEDULER_NO_TASK;
2976 if (pr->pirc != NULL)
2979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2980 "Forwarding of query `%s' not attempted due to pending local lookup!\n",
2981 GNUNET_h2s (&pr->query));
2983 return; /* already pending */
2985 if (GNUNET_YES == pr->local_only)
2986 return; /* configured to not do P2P search */
2988 if ( (0 == pr->anonymity_level) &&
2989 (GNUNET_YES != pr->forward_only) &&
2990 (pr->type != GNUNET_BLOCK_TYPE_FS_DBLOCK) &&
2991 (pr->type != GNUNET_BLOCK_TYPE_FS_IBLOCK) )
2993 pr->dht_get = GNUNET_DHT_get_start (dht_handle,
2994 GNUNET_TIME_UNIT_FOREVER_REL,
2997 DEFAULT_GET_REPLICATION,
3002 (pr->namespace != NULL) ? sizeof (GNUNET_HashCode) : 0,
3006 /* (1) select target */
3008 psc.target_score = -DBL_MAX;
3009 psc.fast_retry = GNUNET_NO;
3010 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
3011 &target_peer_select_cb,
3013 if (psc.target_score == -DBL_MAX)
3015 if (psc.fast_retry == GNUNET_YES)
3016 delay = GNUNET_TIME_UNIT_MILLISECONDS; /* FIXME: store adaptive fast-retry value in 'pr' */
3018 delay = get_processing_delay ();
3020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3021 "No peer selected for forwarding of query `%s', will try again in %llu ms!\n",
3022 GNUNET_h2s (&pr->query),
3025 pr->task = GNUNET_SCHEDULER_add_delayed (delay,
3026 &forward_request_task,
3028 return; /* nobody selected */
3030 /* (3) update TTL/priority */
3031 if (pr->client_request_list != NULL)
3033 /* FIXME: use better algorithm!? */
3034 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
3037 /* bound priority we use by priorities we see from other peers
3038 rounded up (must round up so that we can see non-zero
3039 priorities, but round up as little as possible to make it
3040 plausible that we forwarded another peers request) */
3041 if (pr->priority > current_priorities + 1.0)
3042 pr->priority = (uint32_t) current_priorities + 1.0;
3043 pr->ttl = bound_ttl (pr->ttl + TTL_DECREMENT * 2,
3046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3047 "Trying query `%s' with priority %u and TTL %d.\n",
3048 GNUNET_h2s (&pr->query),
3054 /* (3) reserve reply bandwidth */
3055 if (GNUNET_NO == pr->forward_only)
3057 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
3058 &psc.target.hashPubKey);
3059 GNUNET_assert (NULL != cp);
3060 GNUNET_assert (cp->irc == NULL);
3063 cp->irc = GNUNET_CORE_peer_change_preference (core,
3065 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
3066 GNUNET_BANDWIDTH_value_init (UINT32_MAX),
3069 &target_reservation_cb,
3071 GNUNET_assert (cp->irc != NULL);
3072 cp->inc_preference = 0;
3076 /* force forwarding */
3077 static struct GNUNET_BANDWIDTH_Value32NBO zerobw;
3078 target_reservation_cb (pr, &psc.target,
3084 /* **************************** P2P PUT Handling ************************ */
3088 * Function called after we either failed or succeeded
3089 * at transmitting a reply to a peer.
3091 * @param cls the requests "struct PendingRequest*"
3092 * @param tpid ID of receiving peer, 0 on transmission error
3095 transmit_reply_continuation (void *cls,
3096 GNUNET_PEER_Id tpid)
3098 struct PendingRequest *pr = cls;
3102 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
3103 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
3104 /* only one reply expected, done with the request! */
3105 destroy_pending_request (pr);
3107 case GNUNET_BLOCK_TYPE_ANY:
3108 case GNUNET_BLOCK_TYPE_FS_KBLOCK:
3109 case GNUNET_BLOCK_TYPE_FS_SBLOCK:
3119 * Transmit the given message by copying it to the target buffer
3120 * "buf". "buf" will be NULL and "size" zero if the socket was closed
3121 * for writing in the meantime. In that case, do nothing
3122 * (the disconnect or shutdown handler will take care of the rest).
3123 * If we were able to transmit messages and there are still more
3124 * pending, ask core again for further calls to this function.
3126 * @param cls closure, pointer to the 'struct ClientList*'
3127 * @param size number of bytes available in buf
3128 * @param buf where the callee should write the message
3129 * @return number of bytes written to buf
3132 transmit_to_client (void *cls,
3133 size_t size, void *buf)
3135 struct ClientList *cl = cls;
3137 struct ClientResponseMessage *creply;
3144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3145 "Not sending reply, client communication problem.\n");
3150 while ( (NULL != (creply = cl->res_head) ) &&
3151 (creply->msize <= size) )
3153 memcpy (&cbuf[msize], &creply[1], creply->msize);
3154 msize += creply->msize;
3155 size -= creply->msize;
3156 GNUNET_CONTAINER_DLL_remove (cl->res_head,
3159 GNUNET_free (creply);
3162 cl->th = GNUNET_SERVER_notify_transmit_ready (cl->client,
3164 GNUNET_TIME_UNIT_FOREVER_REL,
3165 &transmit_to_client,
3168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3169 "Transmitted %u bytes to client\n",
3170 (unsigned int) msize);
3177 * Closure for "process_reply" function.
3179 struct ProcessReplyClosure
3182 * The data for the reply.
3187 * Who gave us this reply? NULL for local host (or DHT)
3189 struct ConnectedPeer *sender;
3192 * When the reply expires.
3194 struct GNUNET_TIME_Absolute expiration;
3202 * Type of the block.
3204 enum GNUNET_BLOCK_Type type;
3207 * How much was this reply worth to us?
3212 * Evaluation result (returned).
3214 enum GNUNET_BLOCK_EvaluationResult eval;
3217 * Did we finish processing the associated request?
3222 * Did we find a matching request?
3229 * We have received a reply; handle it!
3231 * @param cls response (struct ProcessReplyClosure)
3232 * @param key our query
3233 * @param value value in the hash map (info about the query)
3234 * @return GNUNET_YES (we should continue to iterate)
3237 process_reply (void *cls,
3238 const GNUNET_HashCode * key,
3241 struct ProcessReplyClosure *prq = cls;
3242 struct PendingRequest *pr = value;
3243 struct PendingMessage *reply;
3244 struct ClientResponseMessage *creply;
3245 struct ClientList *cl;
3246 struct PutMessage *pm;
3247 struct ConnectedPeer *cp;
3248 struct GNUNET_TIME_Relative cur_delay;
3250 struct GNUNET_TIME_Relative art_delay;
3256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3257 "Matched result (type %u) for query `%s' with pending request\n",
3258 (unsigned int) prq->type,
3261 GNUNET_STATISTICS_update (stats,
3262 gettext_noop ("# replies received and matched"),
3265 if (prq->sender != NULL)
3267 for (i=0;i<pr->used_targets_off;i++)
3268 if (pr->used_targets[i].pid == prq->sender->pid)
3270 if (i < pr->used_targets_off)
3272 cur_delay = GNUNET_TIME_absolute_get_duration (pr->used_targets[i].last_request_time);
3273 prq->sender->avg_delay.rel_value
3274 = (prq->sender->avg_delay.rel_value *
3275 (RUNAVG_DELAY_N - 1) + cur_delay.rel_value) / RUNAVG_DELAY_N;
3276 prq->sender->avg_priority
3277 = (prq->sender->avg_priority *
3278 (RUNAVG_DELAY_N - 1) + pr->priority) / (double) RUNAVG_DELAY_N;
3282 GNUNET_PEER_change_rc (prq->sender->last_p2p_replies
3283 [prq->sender->last_p2p_replies_woff % P2P_SUCCESS_LIST_SIZE],
3285 GNUNET_PEER_change_rc (pr->cp->pid, 1);
3286 prq->sender->last_p2p_replies
3287 [(prq->sender->last_p2p_replies_woff++) % P2P_SUCCESS_LIST_SIZE]
3292 if (NULL != prq->sender->last_client_replies
3293 [(prq->sender->last_client_replies_woff) % CS2P_SUCCESS_LIST_SIZE])
3294 GNUNET_SERVER_client_drop (prq->sender->last_client_replies
3295 [(prq->sender->last_client_replies_woff) % CS2P_SUCCESS_LIST_SIZE]);
3296 prq->sender->last_client_replies
3297 [(prq->sender->last_client_replies_woff++) % CS2P_SUCCESS_LIST_SIZE]
3298 = pr->client_request_list->client_list->client;
3299 GNUNET_SERVER_client_keep (pr->client_request_list->client_list->client);
3302 prq->eval = GNUNET_BLOCK_evaluate (block_ctx,
3307 pr->namespace, (pr->namespace != NULL) ? sizeof (GNUNET_HashCode) : 0,
3312 case GNUNET_BLOCK_EVALUATION_OK_MORE:
3314 case GNUNET_BLOCK_EVALUATION_OK_LAST:
3315 while (NULL != pr->pending_head)
3316 destroy_pending_message_list_entry (pr->pending_head);
3319 if (pr->client_request_list != NULL)
3320 GNUNET_SERVER_receive_done (pr->client_request_list->client_list->client,
3322 GNUNET_DATASTORE_cancel (pr->qe);
3325 pr->do_remove = GNUNET_YES;
3326 if (pr->task != GNUNET_SCHEDULER_NO_TASK)
3328 GNUNET_SCHEDULER_cancel (pr->task);
3329 pr->task = GNUNET_SCHEDULER_NO_TASK;
3331 GNUNET_break (GNUNET_YES ==
3332 GNUNET_CONTAINER_multihashmap_remove (query_request_map,
3335 GNUNET_LOAD_update (rt_entry_lifetime,
3336 GNUNET_TIME_absolute_get_duration (pr->start_time).rel_value);
3338 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
3339 GNUNET_STATISTICS_update (stats,
3340 gettext_noop ("# duplicate replies discarded (bloomfilter)"),
3344 /* GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3345 "Duplicate response `%s', discarding.\n",
3346 GNUNET_h2s (&mhash));*/
3348 return GNUNET_YES; /* duplicate */
3349 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
3350 return GNUNET_YES; /* wrong namespace */
3351 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
3354 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
3357 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
3358 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3359 _("Unsupported block type %u\n"),
3363 if (pr->client_request_list != NULL)
3365 if (pr->replies_seen_size == pr->replies_seen_off)
3366 GNUNET_array_grow (pr->replies_seen,
3367 pr->replies_seen_size,
3368 pr->replies_seen_size * 2 + 4);
3369 GNUNET_CRYPTO_hash (prq->data,
3371 &pr->replies_seen[pr->replies_seen_off++]);
3372 refresh_bloomfilter (pr);
3374 if (NULL == prq->sender)
3377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3378 "Found result for query `%s' in local datastore\n",
3381 GNUNET_STATISTICS_update (stats,
3382 gettext_noop ("# results found locally"),
3386 prq->priority += pr->remaining_priority;
3387 pr->remaining_priority = 0;
3388 pr->results_found++;
3389 prq->request_found = GNUNET_YES;
3390 if (NULL != pr->client_request_list)
3392 GNUNET_STATISTICS_update (stats,
3393 gettext_noop ("# replies received for local clients"),
3396 cl = pr->client_request_list->client_list;
3397 msize = sizeof (struct PutMessage) + prq->size;
3398 creply = GNUNET_malloc (msize + sizeof (struct ClientResponseMessage));
3399 creply->msize = msize;
3400 creply->client_list = cl;
3401 GNUNET_CONTAINER_DLL_insert_after (cl->res_head,
3405 pm = (struct PutMessage*) &creply[1];
3406 pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
3407 pm->header.size = htons (msize);
3408 pm->type = htonl (prq->type);
3409 pm->expiration = GNUNET_TIME_absolute_hton (prq->expiration);
3410 memcpy (&pm[1], prq->data, prq->size);
3414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3415 "Transmitting result for query `%s' to client\n",
3418 cl->th = GNUNET_SERVER_notify_transmit_ready (cl->client,
3420 GNUNET_TIME_UNIT_FOREVER_REL,
3421 &transmit_to_client,
3424 GNUNET_break (cl->th != NULL);
3427 prq->finished = GNUNET_YES;
3428 destroy_pending_request (pr);
3435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3436 "Transmitting result for query `%s' to other peer (PID=%u)\n",
3438 (unsigned int) cp->pid);
3440 GNUNET_STATISTICS_update (stats,
3441 gettext_noop ("# replies received for other peers"),
3444 msize = sizeof (struct PutMessage) + prq->size;
3445 reply = GNUNET_malloc (msize + sizeof (struct PendingMessage));
3446 reply->cont = &transmit_reply_continuation;
3447 reply->cont_cls = pr;
3449 art_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
3450 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
3453 = GNUNET_TIME_relative_to_absolute (art_delay);
3454 GNUNET_STATISTICS_update (stats,
3455 gettext_noop ("cummulative artificial delay introduced (ms)"),
3456 art_delay.abs_value,
3459 reply->msize = msize;
3460 reply->priority = UINT32_MAX; /* send replies first! */
3461 pm = (struct PutMessage*) &reply[1];
3462 pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
3463 pm->header.size = htons (msize);
3464 pm->type = htonl (prq->type);
3465 pm->expiration = GNUNET_TIME_absolute_hton (prq->expiration);
3466 memcpy (&pm[1], prq->data, prq->size);
3467 add_to_pending_messages_for_peer (cp, reply, pr);
3474 * Iterator called on each result obtained for a DHT
3475 * operation that expects a reply
3477 * @param cls closure
3478 * @param exp when will this value expire
3479 * @param key key of the result
3480 * @param get_path NULL-terminated array of pointers
3481 * to the peers on reverse GET path (or NULL if not recorded)
3482 * @param put_path NULL-terminated array of pointers
3483 * to the peers on the PUT path (or NULL if not recorded)
3484 * @param type type of the result
3485 * @param size number of bytes in data
3486 * @param data pointer to the result data
3489 process_dht_reply (void *cls,
3490 struct GNUNET_TIME_Absolute exp,
3491 const GNUNET_HashCode * key,
3492 const struct GNUNET_PeerIdentity * const *get_path,
3493 const struct GNUNET_PeerIdentity * const *put_path,
3494 enum GNUNET_BLOCK_Type type,
3498 struct PendingRequest *pr = cls;
3499 struct ProcessReplyClosure prq;
3501 memset (&prq, 0, sizeof (prq));
3503 prq.expiration = exp;
3506 process_reply (&prq, key, pr);
3512 * Continuation called to notify client about result of the
3515 * @param cls closure
3516 * @param success GNUNET_SYSERR on failure
3517 * @param msg NULL on success, otherwise an error message
3520 put_migration_continuation (void *cls,
3524 struct GNUNET_TIME_Absolute *start = cls;
3525 struct GNUNET_TIME_Relative delay;
3527 delay = GNUNET_TIME_absolute_get_duration (*start);
3528 GNUNET_free (start);
3529 GNUNET_LOAD_update (datastore_put_load,
3531 if (GNUNET_OK == success)
3533 GNUNET_STATISTICS_update (stats,
3534 gettext_noop ("# datastore 'put' failures"),
3541 * Handle P2P "PUT" message.
3543 * @param cls closure, always NULL
3544 * @param other the other peer involved (sender or receiver, NULL
3545 * for loopback messages where we are both sender and receiver)
3546 * @param message the actual message
3547 * @param atsi performance information
3548 * @return GNUNET_OK to keep the connection open,
3549 * GNUNET_SYSERR to close it (signal serious error)
3552 handle_p2p_put (void *cls,
3553 const struct GNUNET_PeerIdentity *other,
3554 const struct GNUNET_MessageHeader *message,
3555 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
3557 const struct PutMessage *put;
3560 enum GNUNET_BLOCK_Type type;
3561 struct GNUNET_TIME_Absolute expiration;
3562 GNUNET_HashCode query;
3563 struct ProcessReplyClosure prq;
3564 struct GNUNET_TIME_Absolute *start;
3565 struct GNUNET_TIME_Relative block_time;
3567 struct ConnectedPeer *cp;
3568 struct PendingMessage *pm;
3569 struct MigrationStopMessage *msm;
3571 msize = ntohs (message->size);
3572 if (msize < sizeof (struct PutMessage))
3575 return GNUNET_SYSERR;
3577 put = (const struct PutMessage*) message;
3578 dsize = msize - sizeof (struct PutMessage);
3579 type = ntohl (put->type);
3580 expiration = GNUNET_TIME_absolute_ntoh (put->expiration);
3582 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
3583 return GNUNET_SYSERR;
3585 GNUNET_BLOCK_get_key (block_ctx,
3591 GNUNET_break_op (0);
3592 return GNUNET_SYSERR;
3595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3596 "Received result for query `%s' from peer `%4s'\n",
3597 GNUNET_h2s (&query),
3598 GNUNET_i2s (other));
3600 GNUNET_STATISTICS_update (stats,
3601 gettext_noop ("# replies received (overall)"),
3604 /* now, lookup 'query' */
3605 prq.data = (const void*) &put[1];
3607 prq.sender = GNUNET_CONTAINER_multihashmap_get (connected_peers,
3608 &other->hashPubKey);
3613 prq.expiration = expiration;
3615 prq.finished = GNUNET_NO;
3616 prq.request_found = GNUNET_NO;
3617 GNUNET_CONTAINER_multihashmap_get_multiple (query_request_map,
3621 if (prq.sender != NULL)
3623 prq.sender->inc_preference += CONTENT_BANDWIDTH_VALUE + 1000 * prq.priority;
3624 change_host_trust (prq.sender, prq.priority);
3626 if ( (GNUNET_YES == active_to_migration) &&
3627 (GNUNET_NO == test_put_load_too_high (prq.priority)) )
3630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3631 "Replicating result for query `%s' with priority %u\n",
3632 GNUNET_h2s (&query),
3635 start = GNUNET_malloc (sizeof (struct GNUNET_TIME_Absolute));
3636 *start = GNUNET_TIME_absolute_get ();
3637 GNUNET_DATASTORE_put (dsh,
3638 0, &query, dsize, &put[1],
3639 type, prq.priority, 1 /* anonymity */,
3641 1 + prq.priority, MAX_DATASTORE_QUEUE,
3642 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
3643 &put_migration_continuation,
3646 putl = GNUNET_LOAD_get_load (datastore_put_load);
3647 if ( (GNUNET_NO == prq.request_found) &&
3648 ( (GNUNET_YES != active_to_migration) ||
3649 (putl > 2.5 * (1 + prq.priority)) ) )
3651 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
3652 &other->hashPubKey);
3653 if (GNUNET_TIME_absolute_get_duration (cp->last_migration_block).rel_value < 5000)
3654 return GNUNET_OK; /* already blocked */
3655 /* We're too busy; send MigrationStop message! */
3656 if (GNUNET_YES != active_to_migration)
3657 putl = 1.0 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 5);
3658 block_time = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
3659 5000 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
3660 (unsigned int) (60000 * putl * putl)));
3662 cp->last_migration_block = GNUNET_TIME_relative_to_absolute (block_time);
3663 pm = GNUNET_malloc (sizeof (struct PendingMessage) +
3664 sizeof (struct MigrationStopMessage));
3665 pm->msize = sizeof (struct MigrationStopMessage);
3666 pm->priority = UINT32_MAX;
3667 msm = (struct MigrationStopMessage*) &pm[1];
3668 msm->header.size = htons (sizeof (struct MigrationStopMessage));
3669 msm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_MIGRATION_STOP);
3670 msm->duration = GNUNET_TIME_relative_hton (block_time);
3671 add_to_pending_messages_for_peer (cp,
3680 * Handle P2P "MIGRATION_STOP" message.
3682 * @param cls closure, always NULL
3683 * @param other the other peer involved (sender or receiver, NULL
3684 * for loopback messages where we are both sender and receiver)
3685 * @param message the actual message
3686 * @param atsi performance information
3687 * @return GNUNET_OK to keep the connection open,
3688 * GNUNET_SYSERR to close it (signal serious error)
3691 handle_p2p_migration_stop (void *cls,
3692 const struct GNUNET_PeerIdentity *other,
3693 const struct GNUNET_MessageHeader *message,
3694 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
3696 struct ConnectedPeer *cp;
3697 const struct MigrationStopMessage *msm;
3699 msm = (const struct MigrationStopMessage*) message;
3700 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
3701 &other->hashPubKey);
3707 cp->migration_blocked = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_ntoh (msm->duration));
3713 /* **************************** P2P GET Handling ************************ */
3717 * Closure for 'check_duplicate_request_{peer,client}'.
3719 struct CheckDuplicateRequestClosure
3722 * The new request we should check if it already exists.
3724 const struct PendingRequest *pr;
3727 * Existing request found by the checker, NULL if none.
3729 struct PendingRequest *have;
3734 * Iterator over entries in the 'query_request_map' that
3735 * tries to see if we have the same request pending from
3736 * the same client already.
3738 * @param cls closure (our 'struct CheckDuplicateRequestClosure')
3739 * @param key current key code (query, ignored, must match)
3740 * @param value value in the hash map (a 'struct PendingRequest'
3741 * that already exists)
3742 * @return GNUNET_YES if we should continue to
3743 * iterate (no match yet)
3744 * GNUNET_NO if not (match found).
3747 check_duplicate_request_client (void *cls,
3748 const GNUNET_HashCode * key,
3751 struct CheckDuplicateRequestClosure *cdc = cls;
3752 struct PendingRequest *have = value;
3754 if (have->client_request_list == NULL)
3756 if ( (cdc->pr->client_request_list->client_list->client == have->client_request_list->client_list->client) &&
3767 * We're processing (local) results for a search request
3768 * from another peer. Pass applicable results to the
3769 * peer and if we are done either clean up (operation
3770 * complete) or forward to other peers (more results possible).
3772 * @param cls our closure (struct LocalGetContext)
3773 * @param key key for the content
3774 * @param size number of bytes in data
3775 * @param data content stored
3776 * @param type type of the content
3777 * @param priority priority of the content
3778 * @param anonymity anonymity-level for the content
3779 * @param expiration expiration time for the content
3780 * @param uid unique identifier for the datum;
3781 * maybe 0 if no unique identifier is available
3784 process_local_reply (void *cls,
3785 const GNUNET_HashCode * key,
3788 enum GNUNET_BLOCK_Type type,
3791 struct GNUNET_TIME_Absolute
3795 struct PendingRequest *pr = cls;
3796 struct ProcessReplyClosure prq;
3797 struct CheckDuplicateRequestClosure cdrc;
3798 GNUNET_HashCode query;
3799 unsigned int old_rf;
3804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3805 "Done processing local replies, forwarding request to other peers.\n");
3808 if (pr->client_request_list != NULL)
3810 GNUNET_SERVER_receive_done (pr->client_request_list->client_list->client,
3812 /* Figure out if this is a duplicate request and possibly
3813 merge 'struct PendingRequest' entries */
3816 GNUNET_CONTAINER_multihashmap_get_multiple (query_request_map,
3818 &check_duplicate_request_client,
3820 if (cdrc.have != NULL)
3823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3824 "Received request for block `%s' twice from client, will only request once.\n",
3825 GNUNET_h2s (&pr->query));
3828 destroy_pending_request (pr);
3832 if (pr->local_only == GNUNET_YES)
3834 destroy_pending_request (pr);
3837 /* no more results */
3838 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
3839 pr->task = GNUNET_SCHEDULER_add_now (&forward_request_task,
3844 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3845 "New local response to `%s' of type %u.\n",
3849 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
3852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3853 "Found ONDEMAND block, performing on-demand encoding\n");
3855 GNUNET_STATISTICS_update (stats,
3856 gettext_noop ("# on-demand blocks matched requests"),
3860 GNUNET_FS_handle_on_demand_block (key, size, data, type, priority,
3861 anonymity, expiration, uid,
3862 &process_local_reply,
3866 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
3870 old_rf = pr->results_found;
3871 memset (&prq, 0, sizeof (prq));
3873 prq.expiration = expiration;
3876 GNUNET_BLOCK_get_key (block_ctx,
3883 GNUNET_DATASTORE_remove (dsh,
3887 GNUNET_TIME_UNIT_FOREVER_REL,
3889 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
3893 prq.priority = priority;
3894 prq.finished = GNUNET_NO;
3895 prq.request_found = GNUNET_NO;
3896 if ( (old_rf == 0) &&
3897 (pr->results_found == 0) )
3898 update_datastore_delays (pr->start_time);
3899 process_reply (&prq, key, pr);
3900 if (prq.finished == GNUNET_YES)
3903 return; /* done here */
3904 if (prq.eval == GNUNET_BLOCK_EVALUATION_OK_LAST)
3906 pr->local_only = GNUNET_YES; /* do not forward */
3907 GNUNET_DATASTORE_get_next (dsh, GNUNET_NO);
3910 if ( (pr->client_request_list == NULL) &&
3911 ( (GNUNET_YES == test_get_load_too_high (0)) ||
3912 (pr->results_found > 5 + 2 * pr->priority) ) )
3915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3916 "Load too high, done with request\n");
3918 GNUNET_STATISTICS_update (stats,
3919 gettext_noop ("# processing result set cut short due to load"),
3922 GNUNET_DATASTORE_get_next (dsh, GNUNET_NO);
3925 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
3930 * We've received a request with the specified priority. Bound it
3931 * according to how much we trust the given peer.
3933 * @param prio_in requested priority
3934 * @param cp the peer making the request
3935 * @return effective priority
3938 bound_priority (uint32_t prio_in,
3939 struct ConnectedPeer *cp)
3941 #define N ((double)128.0)
3946 ld = test_get_load_too_high (0);
3947 if (ld == GNUNET_SYSERR)
3949 GNUNET_STATISTICS_update (stats,
3950 gettext_noop ("# requests done for free (low load)"),
3953 return 0; /* excess resources */
3955 if (prio_in > INT32_MAX)
3956 prio_in = INT32_MAX;
3957 ret = - change_host_trust (cp, - (int) prio_in);
3960 if (ret > current_priorities + N)
3961 rret = current_priorities + N;
3965 = (current_priorities * (N-1) + rret)/N;
3967 if ( (ld == GNUNET_YES) && (ret > 0) )
3969 /* try with charging */
3970 ld = test_get_load_too_high (ret);
3972 if (ld == GNUNET_YES)
3974 GNUNET_STATISTICS_update (stats,
3975 gettext_noop ("# request dropped, priority insufficient"),
3979 change_host_trust (cp, (int) ret);
3980 return -1; /* not enough resources */
3984 GNUNET_STATISTICS_update (stats,
3985 gettext_noop ("# requests done for a price (normal load)"),
3995 * Iterator over entries in the 'query_request_map' that
3996 * tries to see if we have the same request pending from
3997 * the same peer already.
3999 * @param cls closure (our 'struct CheckDuplicateRequestClosure')
4000 * @param key current key code (query, ignored, must match)
4001 * @param value value in the hash map (a 'struct PendingRequest'
4002 * that already exists)
4003 * @return GNUNET_YES if we should continue to
4004 * iterate (no match yet)
4005 * GNUNET_NO if not (match found).
4008 check_duplicate_request_peer (void *cls,
4009 const GNUNET_HashCode * key,
4012 struct CheckDuplicateRequestClosure *cdc = cls;
4013 struct PendingRequest *have = value;
4015 if (cdc->pr->target_pid == have->target_pid)
4025 * Handle P2P "GET" request.
4027 * @param cls closure, always NULL
4028 * @param other the other peer involved (sender or receiver, NULL
4029 * for loopback messages where we are both sender and receiver)
4030 * @param message the actual message
4031 * @param atsi performance information
4032 * @return GNUNET_OK to keep the connection open,
4033 * GNUNET_SYSERR to close it (signal serious error)
4036 handle_p2p_get (void *cls,
4037 const struct GNUNET_PeerIdentity *other,
4038 const struct GNUNET_MessageHeader *message,
4039 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
4041 struct PendingRequest *pr;
4042 struct ConnectedPeer *cp;
4043 struct ConnectedPeer *cps;
4044 struct CheckDuplicateRequestClosure cdc;
4045 struct GNUNET_TIME_Relative timeout;
4047 const struct GetMessage *gm;
4049 const GNUNET_HashCode *opt;
4052 uint32_t ttl_decrement;
4054 enum GNUNET_BLOCK_Type type;
4057 msize = ntohs(message->size);
4058 if (msize < sizeof (struct GetMessage))
4060 GNUNET_break_op (0);
4061 return GNUNET_SYSERR;
4063 gm = (const struct GetMessage*) message;
4065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4066 "Received request for `%s'\n",
4067 GNUNET_h2s (&gm->query));
4069 type = ntohl (gm->type);
4070 bm = ntohl (gm->hash_bitmap);
4078 if (msize < sizeof (struct GetMessage) + bits * sizeof (GNUNET_HashCode))
4080 GNUNET_break_op (0);
4081 return GNUNET_SYSERR;
4083 opt = (const GNUNET_HashCode*) &gm[1];
4084 bfsize = msize - sizeof (struct GetMessage) - bits * sizeof (GNUNET_HashCode);
4085 /* bfsize must be power of 2, check! */
4086 if (0 != ( (bfsize - 1) & bfsize))
4088 GNUNET_break_op (0);
4089 return GNUNET_SYSERR;
4091 bm = ntohl (gm->hash_bitmap);
4093 cps = GNUNET_CONTAINER_multihashmap_get (connected_peers,
4094 &other->hashPubKey);
4097 /* peer must have just disconnected */
4098 GNUNET_STATISTICS_update (stats,
4099 gettext_noop ("# requests dropped due to initiator not being connected"),
4102 return GNUNET_SYSERR;
4104 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
4105 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
4112 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
4113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4114 "Failed to find RETURN-TO peer `%4s' in connection set. Dropping query.\n",
4115 GNUNET_i2s ((const struct GNUNET_PeerIdentity*) &opt[bits-1]));
4118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4119 "Failed to find peer `%4s' in connection set. Dropping query.\n",
4120 GNUNET_i2s (other));
4122 GNUNET_STATISTICS_update (stats,
4123 gettext_noop ("# requests dropped due to missing reverse route"),
4126 /* FIXME: try connect? */
4129 /* note that we can really only check load here since otherwise
4130 peers could find out that we are overloaded by not being
4131 disconnected after sending us a malformed query... */
4132 priority = bound_priority (ntohl (gm->priority), cps);
4136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4137 "Dropping query from `%s', this peer is too busy.\n",
4138 GNUNET_i2s (other));
4143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4144 "Received request for `%s' of type %u from peer `%4s' with flags %u\n",
4145 GNUNET_h2s (&gm->query),
4146 (unsigned int) type,
4150 have_ns = (0 != (bm & GET_MESSAGE_BIT_SKS_NAMESPACE));
4151 pr = GNUNET_malloc (sizeof (struct PendingRequest) +
4152 (have_ns ? sizeof(GNUNET_HashCode) : 0));
4155 pr->namespace = (GNUNET_HashCode*) &pr[1];
4156 memcpy (&pr[1], &opt[bits++], sizeof (GNUNET_HashCode));
4158 if ( (GNUNET_LOAD_get_load (cp->transmission_delay) > 3 * (1 + priority)) ||
4159 (GNUNET_LOAD_get_average (cp->transmission_delay) >
4160 GNUNET_CONSTANTS_MAX_CORK_DELAY.rel_value * 2 + GNUNET_LOAD_get_average (rt_entry_lifetime)) )
4162 /* don't have BW to send to peer, or would likely take longer than we have for it,
4163 so at best indirect the query */
4165 pr->forward_only = GNUNET_YES;
4168 pr->mingle = ntohl (gm->filter_mutator);
4169 if (0 != (bm & GET_MESSAGE_BIT_TRANSMIT_TO))
4170 pr->target_pid = GNUNET_PEER_intern ((const struct GNUNET_PeerIdentity*) &opt[bits++]);
4171 pr->anonymity_level = 1;
4172 pr->priority = (uint32_t) priority;
4173 pr->ttl = bound_ttl (ntohl (gm->ttl), pr->priority);
4174 pr->query = gm->query;
4175 /* decrement ttl (always) */
4176 ttl_decrement = 2 * TTL_DECREMENT +
4177 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
4179 if ( (pr->ttl < 0) &&
4180 (((int32_t)(pr->ttl - ttl_decrement)) > 0) )
4183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4184 "Dropping query from `%s' due to TTL underflow (%d - %u).\n",
4189 GNUNET_STATISTICS_update (stats,
4190 gettext_noop ("# requests dropped due TTL underflow"),
4193 /* integer underflow => drop (should be very rare)! */
4197 pr->ttl -= ttl_decrement;
4198 pr->start_time = GNUNET_TIME_absolute_get ();
4200 /* get bloom filter */
4203 pr->bf = GNUNET_CONTAINER_bloomfilter_init ((const char*) &opt[bits],
4206 pr->bf_size = bfsize;
4210 GNUNET_CONTAINER_multihashmap_get_multiple (query_request_map,
4212 &check_duplicate_request_peer,
4214 if (cdc.have != NULL)
4216 if (cdc.have->start_time.abs_value + cdc.have->ttl >=
4217 pr->start_time.abs_value + pr->ttl)
4219 /* existing request has higher TTL, drop new one! */
4220 cdc.have->priority += pr->priority;
4221 destroy_pending_request (pr);
4223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4224 "Have existing request with higher TTL, dropping new request.\n",
4225 GNUNET_i2s (other));
4227 GNUNET_STATISTICS_update (stats,
4228 gettext_noop ("# requests dropped due to higher-TTL request"),
4235 /* existing request has lower TTL, drop old one! */
4236 pr->priority += cdc.have->priority;
4237 /* Possible optimization: if we have applicable pending
4238 replies in 'cdc.have', we might want to move those over
4239 (this is a really rare special-case, so it is not clear
4240 that this would be worth it) */
4241 destroy_pending_request (cdc.have);
4242 /* keep processing 'pr'! */
4247 GNUNET_break (GNUNET_OK ==
4248 GNUNET_CONTAINER_multihashmap_put (query_request_map,
4251 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
4252 GNUNET_break (GNUNET_OK ==
4253 GNUNET_CONTAINER_multihashmap_put (peer_request_map,
4256 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
4258 pr->hnode = GNUNET_CONTAINER_heap_insert (requests_by_expiration_heap,
4260 pr->start_time.abs_value + pr->ttl);
4262 GNUNET_STATISTICS_update (stats,
4263 gettext_noop ("# P2P searches received"),
4266 GNUNET_STATISTICS_update (stats,
4267 gettext_noop ("# P2P searches active"),
4271 /* calculate change in traffic preference */
4272 cps->inc_preference += pr->priority * 1000 + QUERY_BANDWIDTH_VALUE;
4273 /* process locally */
4274 if (type == GNUNET_BLOCK_TYPE_FS_DBLOCK)
4275 type = GNUNET_BLOCK_TYPE_ANY; /* to get on-demand as well */
4276 timeout = GNUNET_TIME_relative_multiply (BASIC_DATASTORE_REQUEST_DELAY,
4277 (pr->priority + 1));
4278 if (GNUNET_YES != pr->forward_only)
4281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4282 "Handing request for `%s' to datastore\n",
4283 GNUNET_h2s (&gm->query));
4285 pr->qe = GNUNET_DATASTORE_get (dsh,
4289 MAX_DATASTORE_QUEUE,
4291 &process_local_reply,
4295 GNUNET_STATISTICS_update (stats,
4296 gettext_noop ("# requests dropped by datastore (queue length limit)"),
4303 GNUNET_STATISTICS_update (stats,
4304 gettext_noop ("# requests forwarded due to high load"),
4309 /* Are multiple results possible (and did we look locally)? If so, start processing remotely now! */
4312 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
4313 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
4314 /* only one result, wait for datastore */
4315 if (GNUNET_YES != pr->forward_only)
4317 GNUNET_STATISTICS_update (stats,
4318 gettext_noop ("# requests not instantly forwarded (waiting for datastore)"),
4324 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
4325 pr->task = GNUNET_SCHEDULER_add_now (&forward_request_task,
4329 /* make sure we don't track too many requests */
4330 if (GNUNET_CONTAINER_heap_get_size (requests_by_expiration_heap) > max_pending_requests)
4332 pr = GNUNET_CONTAINER_heap_peek (requests_by_expiration_heap);
4333 GNUNET_assert (pr != NULL);
4334 destroy_pending_request (pr);
4340 /* **************************** CS GET Handling ************************ */
4344 * Handle START_SEARCH-message (search request from client).
4346 * @param cls closure
4347 * @param client identification of the client
4348 * @param message the actual message
4351 handle_start_search (void *cls,
4352 struct GNUNET_SERVER_Client *client,
4353 const struct GNUNET_MessageHeader *message)
4355 static GNUNET_HashCode all_zeros;
4356 const struct SearchMessage *sm;
4357 struct ClientList *cl;
4358 struct ClientRequestList *crl;
4359 struct PendingRequest *pr;
4362 enum GNUNET_BLOCK_Type type;
4364 msize = ntohs (message->size);
4365 if ( (msize < sizeof (struct SearchMessage)) ||
4366 (0 != (msize - sizeof (struct SearchMessage)) % sizeof (GNUNET_HashCode)) )
4369 GNUNET_SERVER_receive_done (client,
4373 GNUNET_STATISTICS_update (stats,
4374 gettext_noop ("# client searches received"),
4377 sc = (msize - sizeof (struct SearchMessage)) / sizeof (GNUNET_HashCode);
4378 sm = (const struct SearchMessage*) message;
4379 type = ntohl (sm->type);
4381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4382 "Received request for `%s' of type %u from local client\n",
4383 GNUNET_h2s (&sm->query),
4384 (unsigned int) type);
4387 while ( (cl != NULL) &&
4388 (cl->client != client) )
4392 cl = GNUNET_malloc (sizeof (struct ClientList));
4393 cl->client = client;
4394 GNUNET_SERVER_client_keep (client);
4395 cl->next = client_list;
4398 /* detect duplicate KBLOCK requests */
4399 if ( (type == GNUNET_BLOCK_TYPE_FS_KBLOCK) ||
4400 (type == GNUNET_BLOCK_TYPE_FS_NBLOCK) ||
4401 (type == GNUNET_BLOCK_TYPE_ANY) )
4404 while ( (crl != NULL) &&
4405 ( (0 != memcmp (&crl->req->query,
4407 sizeof (GNUNET_HashCode))) ||
4408 (crl->req->type != type) ) )
4413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4414 "Have existing request, merging content-seen lists.\n");
4417 /* Duplicate request (used to send long list of
4418 known/blocked results); merge 'pr->replies_seen'
4419 and update bloom filter */
4420 GNUNET_array_grow (pr->replies_seen,
4421 pr->replies_seen_size,
4422 pr->replies_seen_off + sc);
4423 memcpy (&pr->replies_seen[pr->replies_seen_off],
4425 sc * sizeof (GNUNET_HashCode));
4426 pr->replies_seen_off += sc;
4427 refresh_bloomfilter (pr);
4428 GNUNET_STATISTICS_update (stats,
4429 gettext_noop ("# client searches updated (merged content seen list)"),
4432 GNUNET_SERVER_receive_done (client,
4437 GNUNET_STATISTICS_update (stats,
4438 gettext_noop ("# client searches active"),
4441 pr = GNUNET_malloc (sizeof (struct PendingRequest) +
4442 ((type == GNUNET_BLOCK_TYPE_FS_SBLOCK) ? sizeof(GNUNET_HashCode) : 0));
4443 crl = GNUNET_malloc (sizeof (struct ClientRequestList));
4444 memset (crl, 0, sizeof (struct ClientRequestList));
4445 crl->client_list = cl;
4446 GNUNET_CONTAINER_DLL_insert (cl->rl_head,
4451 pr->client_request_list = crl;
4452 GNUNET_array_grow (pr->replies_seen,
4453 pr->replies_seen_size,
4455 memcpy (pr->replies_seen,
4457 sc * sizeof (GNUNET_HashCode));
4458 pr->replies_seen_off = sc;
4459 pr->anonymity_level = ntohl (sm->anonymity_level);
4460 pr->start_time = GNUNET_TIME_absolute_get ();
4461 refresh_bloomfilter (pr);
4462 pr->query = sm->query;
4463 if (0 == (1 & ntohl (sm->options)))
4464 pr->local_only = GNUNET_NO;
4466 pr->local_only = GNUNET_YES;
4469 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
4470 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
4471 if (0 != memcmp (&sm->target,
4473 sizeof (GNUNET_HashCode)))
4474 pr->target_pid = GNUNET_PEER_intern ((const struct GNUNET_PeerIdentity*) &sm->target);
4476 case GNUNET_BLOCK_TYPE_FS_SBLOCK:
4477 pr->namespace = (GNUNET_HashCode*) &pr[1];
4478 memcpy (&pr[1], &sm->target, sizeof (GNUNET_HashCode));
4483 GNUNET_break (GNUNET_OK ==
4484 GNUNET_CONTAINER_multihashmap_put (query_request_map,
4487 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
4488 if (type == GNUNET_BLOCK_TYPE_FS_DBLOCK)
4489 type = GNUNET_BLOCK_TYPE_ANY; /* get on-demand blocks too! */
4490 pr->qe = GNUNET_DATASTORE_get (dsh,
4494 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
4495 &process_local_reply,
4500 /* **************************** Startup ************************ */
4503 * Process fs requests.
4505 * @param server the initialized server
4506 * @param c configuration to use
4509 main_init (struct GNUNET_SERVER_Handle *server,
4510 const struct GNUNET_CONFIGURATION_Handle *c)
4512 static const struct GNUNET_CORE_MessageHandler p2p_handlers[] =
4515 GNUNET_MESSAGE_TYPE_FS_GET, 0 },
4517 GNUNET_MESSAGE_TYPE_FS_PUT, 0 },
4518 { &handle_p2p_migration_stop,
4519 GNUNET_MESSAGE_TYPE_FS_MIGRATION_STOP,
4520 sizeof (struct MigrationStopMessage) },
4523 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
4524 {&GNUNET_FS_handle_index_start, NULL,
4525 GNUNET_MESSAGE_TYPE_FS_INDEX_START, 0},
4526 {&GNUNET_FS_handle_index_list_get, NULL,
4527 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_GET, sizeof(struct GNUNET_MessageHeader) },
4528 {&GNUNET_FS_handle_unindex, NULL, GNUNET_MESSAGE_TYPE_FS_UNINDEX,
4529 sizeof (struct UnindexMessage) },
4530 {&handle_start_search, NULL, GNUNET_MESSAGE_TYPE_FS_START_SEARCH,
4534 unsigned long long enc = 128;
4537 stats = GNUNET_STATISTICS_create ("fs", cfg);
4538 min_migration_delay = GNUNET_TIME_UNIT_SECONDS;
4540 GNUNET_CONFIGURATION_get_value_number (cfg,
4542 "MAX_PENDING_REQUESTS",
4543 &max_pending_requests)) ||
4545 GNUNET_CONFIGURATION_get_value_number (cfg,
4547 "EXPECTED_NEIGHBOUR_COUNT",
4550 GNUNET_CONFIGURATION_get_value_time (cfg,
4552 "MIN_MIGRATION_DELAY",
4553 &min_migration_delay)) )
4555 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4556 _("Configuration fails to specify certain parameters, assuming default values."));
4558 connected_peers = GNUNET_CONTAINER_multihashmap_create (enc);
4559 query_request_map = GNUNET_CONTAINER_multihashmap_create (max_pending_requests);
4560 rt_entry_lifetime = GNUNET_LOAD_value_init (GNUNET_TIME_UNIT_FOREVER_REL);
4561 peer_request_map = GNUNET_CONTAINER_multihashmap_create (enc);
4562 requests_by_expiration_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
4563 core = GNUNET_CORE_connect (cfg,
4567 &peer_connect_handler,
4568 &peer_disconnect_handler,
4569 &peer_status_handler,
4575 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4576 _("Failed to connect to `%s' service.\n"),
4578 GNUNET_CONTAINER_multihashmap_destroy (connected_peers);
4579 connected_peers = NULL;
4580 GNUNET_CONTAINER_multihashmap_destroy (query_request_map);
4581 query_request_map = NULL;
4582 GNUNET_LOAD_value_free (rt_entry_lifetime);
4583 rt_entry_lifetime = NULL;
4584 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
4585 requests_by_expiration_heap = NULL;
4586 GNUNET_CONTAINER_multihashmap_destroy (peer_request_map);
4587 peer_request_map = NULL;
4590 GNUNET_DATASTORE_disconnect (dsh, GNUNET_NO);
4593 return GNUNET_SYSERR;
4595 if (active_from_migration)
4597 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4598 _("Content migration is enabled, will start to gather data\n"));
4599 consider_migration_gathering ();
4601 consider_dht_put_gathering (NULL);
4602 GNUNET_SERVER_disconnect_notify (server,
4603 &handle_client_disconnect,
4605 GNUNET_assert (GNUNET_OK ==
4606 GNUNET_CONFIGURATION_get_value_filename (cfg,
4610 GNUNET_DISK_directory_create (trustDirectory);
4611 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_HIGH,
4612 &cron_flush_trust, NULL);
4615 GNUNET_SERVER_add_handlers (server, handlers);
4616 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
4624 * Process fs requests.
4626 * @param cls closure
4627 * @param server the initialized server
4628 * @param cfg configuration to use
4632 struct GNUNET_SERVER_Handle *server,
4633 const struct GNUNET_CONFIGURATION_Handle *cfg)
4635 active_to_migration = GNUNET_CONFIGURATION_get_value_yesno (cfg,
4638 active_from_migration = GNUNET_CONFIGURATION_get_value_yesno (cfg,
4641 dsh = GNUNET_DATASTORE_connect (cfg);
4644 GNUNET_SCHEDULER_shutdown ();
4647 datastore_get_load = GNUNET_LOAD_value_init (DATASTORE_LOAD_AUTODECLINE);
4648 datastore_put_load = GNUNET_LOAD_value_init (DATASTORE_LOAD_AUTODECLINE);
4649 block_cfg = GNUNET_CONFIGURATION_create ();
4650 GNUNET_CONFIGURATION_set_value_string (block_cfg,
4654 block_ctx = GNUNET_BLOCK_context_create (block_cfg);
4655 GNUNET_assert (NULL != block_ctx);
4656 dht_handle = GNUNET_DHT_connect (cfg,
4658 if ( (GNUNET_OK != GNUNET_FS_indexing_init (cfg, dsh)) ||
4659 (GNUNET_OK != main_init (server, cfg)) )
4661 GNUNET_SCHEDULER_shutdown ();
4662 GNUNET_DATASTORE_disconnect (dsh, GNUNET_NO);
4664 GNUNET_DHT_disconnect (dht_handle);
4666 GNUNET_BLOCK_context_destroy (block_ctx);
4668 GNUNET_CONFIGURATION_destroy (block_cfg);
4670 GNUNET_LOAD_value_free (datastore_get_load);
4671 datastore_get_load = NULL;
4672 GNUNET_LOAD_value_free (datastore_put_load);
4673 datastore_put_load = NULL;
4680 * The main function for the fs service.
4682 * @param argc number of arguments from the command line
4683 * @param argv command line arguments
4684 * @return 0 ok, 1 on error
4687 main (int argc, char *const *argv)
4689 return (GNUNET_OK ==
4690 GNUNET_SERVICE_run (argc,
4693 GNUNET_SERVICE_OPTION_NONE,
4694 &run, NULL)) ? 0 : 1;
4697 /* end of gnunet-service-fs.c */