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 #define SUPPORT_DELAYS GNUNET_NO
61 * Currently experimental code...
63 #define ENABLE_LOAD_MGMT GNUNET_YES
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 #define RETRY_PROBABILITY_INV 1
108 * What is the maximum delay for a P2P FS message (in our interaction
109 * with core)? FS-internal delays are another story. The value is
110 * chosen based on the 32k block size. Given that peers typcially
111 * have at least 1 kb/s bandwidth, 45s waits give us a chance to
112 * transmit one message even to the lowest-bandwidth peers.
114 #define MAX_TRANSMIT_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 45)
117 * Maximum number of requests (from other peers, overall) that we're
118 * willing to have pending at any given point in time. Can be changed
119 * via the configuration file (32k is just the default).
121 static unsigned long long max_pending_requests = (32 * 1024);
125 * Information we keep for each pending reply. The
126 * actual message follows at the end of this struct.
128 struct PendingMessage;
131 * Function called upon completion of a transmission.
134 * @param pid ID of receiving peer, 0 on transmission error
136 typedef void (*TransmissionContinuation)(void * cls,
137 GNUNET_PEER_Id tpid);
141 * Information we keep for each pending message (GET/PUT). The
142 * actual message follows at the end of this struct.
144 struct PendingMessage
147 * This is a doubly-linked list of messages to the same peer.
149 struct PendingMessage *next;
152 * This is a doubly-linked list of messages to the same peer.
154 struct PendingMessage *prev;
157 * Entry in pending message list for this pending message.
159 struct PendingMessageList *pml;
162 * Function to call immediately once we have transmitted this
165 TransmissionContinuation cont;
173 * Do not transmit this pending message until this deadline.
175 struct GNUNET_TIME_Absolute delay_until;
178 * Size of the reply; actual reply message follows
179 * at the end of this struct.
184 * How important is this message for us?
192 * Information about a peer that we are connected to.
193 * We track data that is useful for determining which
194 * peers should receive our requests. We also keep
195 * a list of messages to transmit to this peer.
201 * List of the last clients for which this peer successfully
204 struct GNUNET_SERVER_Client *last_client_replies[CS2P_SUCCESS_LIST_SIZE];
207 * List of the last PIDs for which
208 * this peer successfully answered a query;
209 * We use 0 to indicate no successful reply.
211 GNUNET_PEER_Id last_p2p_replies[P2P_SUCCESS_LIST_SIZE];
214 * Average delay between sending the peer a request and
215 * getting a reply (only calculated over the requests for
216 * which we actually got a reply). Calculated
217 * as a moving average: new_delay = ((n-1)*last_delay+curr_delay) / n
219 struct GNUNET_TIME_Relative avg_delay;
222 * Point in time until which this peer does not want us to migrate content
225 struct GNUNET_TIME_Absolute migration_blocked;
228 * Time until when we blocked this peer from migrating
231 struct GNUNET_TIME_Absolute last_migration_block;
234 * Transmission times for the last MAX_QUEUE_PER_PEER
235 * requests for this peer. Used as a ring buffer, current
236 * offset is stored in 'last_request_times_off'. If the
237 * oldest entry is more recent than the 'avg_delay', we should
238 * not send any more requests right now.
240 struct GNUNET_TIME_Absolute last_request_times[MAX_QUEUE_PER_PEER];
243 * Handle for an active request for transmission to this
246 struct GNUNET_CORE_TransmitHandle *cth;
249 * Messages (replies, queries, content migration) we would like to
250 * send to this peer in the near future. Sorted by priority, head.
252 struct PendingMessage *pending_messages_head;
255 * Messages (replies, queries, content migration) we would like to
256 * send to this peer in the near future. Sorted by priority, tail.
258 struct PendingMessage *pending_messages_tail;
261 * How long does it typically take for us to transmit a message
262 * to this peer? (delay between the request being issued and
263 * the callback being invoked).
265 struct GNUNET_LOAD_Value *transmission_delay;
268 * Time when the last transmission request was issued.
270 struct GNUNET_TIME_Absolute last_transmission_request_start;
273 * ID of delay task for scheduling transmission.
275 GNUNET_SCHEDULER_TaskIdentifier delayed_transmission_request_task;
278 * Average priority of successful replies. Calculated
279 * as a moving average: new_avg = ((n-1)*last_avg+curr_prio) / n
284 * Increase in traffic preference still to be submitted
285 * to the core service for this peer.
287 uint64_t inc_preference;
290 * Trust rating for this peer
295 * Trust rating for this peer on disk.
300 * The peer's identity.
305 * Size of the linked list of 'pending_messages'.
307 unsigned int pending_requests;
310 * Which offset in "last_p2p_replies" will be updated next?
311 * (we go round-robin).
313 unsigned int last_p2p_replies_woff;
316 * Which offset in "last_client_replies" will be updated next?
317 * (we go round-robin).
319 unsigned int last_client_replies_woff;
322 * Current offset into 'last_request_times' ring buffer.
324 unsigned int last_request_times_off;
330 * Information we keep for each pending request. We should try to
331 * keep this struct as small as possible since its memory consumption
332 * is key to how many requests we can have pending at once.
334 struct PendingRequest;
338 * Doubly-linked list of requests we are performing
339 * on behalf of the same client.
341 struct ClientRequestList
345 * This is a doubly-linked list.
347 struct ClientRequestList *next;
350 * This is a doubly-linked list.
352 struct ClientRequestList *prev;
355 * Request this entry represents.
357 struct PendingRequest *req;
360 * Client list this request belongs to.
362 struct ClientList *client_list;
368 * Replies to be transmitted to the client. The actual
369 * response message is allocated after this struct.
371 struct ClientResponseMessage
374 * This is a doubly-linked list.
376 struct ClientResponseMessage *next;
379 * This is a doubly-linked list.
381 struct ClientResponseMessage *prev;
384 * Client list entry this response belongs to.
386 struct ClientList *client_list;
389 * Number of bytes in the response.
396 * Linked list of clients we are performing requests
402 * This is a linked list.
404 struct ClientList *next;
407 * ID of a client making a request, NULL if this entry is for a
410 struct GNUNET_SERVER_Client *client;
413 * Head of list of requests performed on behalf
414 * of this client right now.
416 struct ClientRequestList *rl_head;
419 * Tail of list of requests performed on behalf
420 * of this client right now.
422 struct ClientRequestList *rl_tail;
425 * Head of linked list of responses.
427 struct ClientResponseMessage *res_head;
430 * Tail of linked list of responses.
432 struct ClientResponseMessage *res_tail;
435 * Context for sending replies.
437 struct GNUNET_CONNECTION_TransmitHandle *th;
443 * Information about a peer that we have forwarded this
444 * request to already.
446 struct UsedTargetEntry
449 * What was the last time we have transmitted this request to this
452 struct GNUNET_TIME_Absolute last_request_time;
455 * How often have we transmitted this request to this peer?
457 unsigned int num_requests;
460 * PID of the target peer.
471 * Doubly-linked list of messages we are performing
472 * due to a pending request.
474 struct PendingMessageList
478 * This is a doubly-linked list of messages on behalf of the same request.
480 struct PendingMessageList *next;
483 * This is a doubly-linked list of messages on behalf of the same request.
485 struct PendingMessageList *prev;
488 * Message this entry represents.
490 struct PendingMessage *pm;
493 * Request this entry belongs to.
495 struct PendingRequest *req;
498 * Peer this message is targeted for.
500 struct ConnectedPeer *target;
506 * Information we keep for each pending request. We should try to
507 * keep this struct as small as possible since its memory consumption
508 * is key to how many requests we can have pending at once.
510 struct PendingRequest
514 * If this request was made by a client, this is our entry in the
515 * client request list; otherwise NULL.
517 struct ClientRequestList *client_request_list;
520 * Entry of peer responsible for this entry (if this request
521 * was made by a peer).
523 struct ConnectedPeer *cp;
526 * If this is a namespace query, pointer to the hash of the public
527 * key of the namespace; otherwise NULL. Pointer will be to the
528 * end of this struct (so no need to free it).
530 const GNUNET_HashCode *namespace;
533 * Bloomfilter we use to filter out replies that we don't care about
534 * (anymore). NULL as long as we are interested in all replies.
536 struct GNUNET_CONTAINER_BloomFilter *bf;
539 * Context of our GNUNET_CORE_peer_change_preference call.
541 struct GNUNET_CORE_InformationRequestContext *irc;
544 * Reference to DHT get operation for this request (or NULL).
546 struct GNUNET_DHT_GetHandle *dht_get;
549 * Hash code of all replies that we have seen so far (only valid
550 * if client is not NULL since we only track replies like this for
553 GNUNET_HashCode *replies_seen;
556 * Node in the heap representing this entry; NULL
557 * if we have no heap node.
559 struct GNUNET_CONTAINER_HeapNode *hnode;
562 * Head of list of messages being performed on behalf of this
565 struct PendingMessageList *pending_head;
568 * Tail of list of messages being performed on behalf of this
571 struct PendingMessageList *pending_tail;
574 * When did we first see this request (form this peer), or, if our
575 * client is initiating, when did we last initiate a search?
577 struct GNUNET_TIME_Absolute start_time;
580 * The query that this request is for.
582 GNUNET_HashCode query;
585 * The task responsible for transmitting queries
588 GNUNET_SCHEDULER_TaskIdentifier task;
591 * (Interned) Peer identifier that identifies a preferred target
594 GNUNET_PEER_Id target_pid;
597 * (Interned) Peer identifiers of peers that have already
598 * received our query for this content.
600 struct UsedTargetEntry *used_targets;
603 * Our entry in the queue (non-NULL while we wait for our
604 * turn to interact with the local database).
606 struct GNUNET_DATASTORE_QueueEntry *qe;
609 * Size of the 'bf' (in bytes).
614 * Desired anonymity level; only valid for requests from a local client.
616 uint32_t anonymity_level;
619 * How many entries in "used_targets" are actually valid?
621 unsigned int used_targets_off;
624 * How long is the "used_targets" array?
626 unsigned int used_targets_size;
629 * Number of results found for this request.
631 unsigned int results_found;
634 * How many entries in "replies_seen" are actually valid?
636 unsigned int replies_seen_off;
639 * How long is the "replies_seen" array?
641 unsigned int replies_seen_size;
644 * Priority with which this request was made. If one of our clients
645 * made the request, then this is the current priority that we are
646 * using when initiating the request. This value is used when
647 * we decide to reward other peers with trust for providing a reply.
652 * Priority points left for us to spend when forwarding this request
655 uint32_t remaining_priority;
658 * Number to mingle hashes for bloom-filter tests with.
663 * TTL with which we saw this request (or, if we initiated, TTL that
664 * we used for the request).
669 * Type of the content that this request is for.
671 enum GNUNET_BLOCK_Type type;
674 * Remove this request after transmission of the current response.
679 * GNUNET_YES if we should not forward this request to other peers.
684 * GNUNET_YES if we should not forward this request to other peers.
692 * Block that is ready for migration to other peers. Actual data is at the end of the block.
694 struct MigrationReadyBlock
698 * This is a doubly-linked list.
700 struct MigrationReadyBlock *next;
703 * This is a doubly-linked list.
705 struct MigrationReadyBlock *prev;
708 * Query for the block.
710 GNUNET_HashCode query;
713 * When does this block expire?
715 struct GNUNET_TIME_Absolute expiration;
718 * Peers we would consider forwarding this
719 * block to. Zero for empty entries.
721 GNUNET_PEER_Id target_list[MIGRATION_LIST_SIZE];
729 * Number of targets already used.
731 unsigned int used_targets;
736 enum GNUNET_BLOCK_Type type;
741 * Our connection to the datastore.
743 static struct GNUNET_DATASTORE_Handle *dsh;
748 static struct GNUNET_BLOCK_Context *block_ctx;
751 * Our block configuration.
753 static struct GNUNET_CONFIGURATION_Handle *block_cfg;
758 static struct GNUNET_SCHEDULER_Handle *sched;
763 static const struct GNUNET_CONFIGURATION_Handle *cfg;
766 * Map of peer identifiers to "struct ConnectedPeer" (for that peer).
768 static struct GNUNET_CONTAINER_MultiHashMap *connected_peers;
771 * Map of peer identifiers to "struct PendingRequest" (for that peer).
773 static struct GNUNET_CONTAINER_MultiHashMap *peer_request_map;
776 * Map of query identifiers to "struct PendingRequest" (for that query).
778 static struct GNUNET_CONTAINER_MultiHashMap *query_request_map;
781 * Heap with the request that will expire next at the top. Contains
782 * pointers of type "struct PendingRequest*"; these will *also* be
783 * aliased from the "requests_by_peer" data structures and the
784 * "requests_by_query" table. Note that requests from our clients
785 * don't expire and are thus NOT in the "requests_by_expiration"
786 * (or the "requests_by_peer" tables).
788 static struct GNUNET_CONTAINER_Heap *requests_by_expiration_heap;
791 * Handle for reporting statistics.
793 static struct GNUNET_STATISTICS_Handle *stats;
796 * Linked list of clients we are currently processing requests for.
798 static struct ClientList *client_list;
801 * Pointer to handle to the core service (points to NULL until we've
804 static struct GNUNET_CORE_Handle *core;
807 * Head of linked list of blocks that can be migrated.
809 static struct MigrationReadyBlock *mig_head;
812 * Tail of linked list of blocks that can be migrated.
814 static struct MigrationReadyBlock *mig_tail;
817 * Request to datastore for migration (or NULL).
819 static struct GNUNET_DATASTORE_QueueEntry *mig_qe;
822 * Request to datastore for DHT PUTs (or NULL).
824 static struct GNUNET_DATASTORE_QueueEntry *dht_qe;
827 * Type we will request for the next DHT PUT round from the datastore.
829 static enum GNUNET_BLOCK_Type dht_put_type = GNUNET_BLOCK_TYPE_FS_KBLOCK;
832 * Where do we store trust information?
834 static char *trustDirectory;
837 * ID of task that collects blocks for migration.
839 static GNUNET_SCHEDULER_TaskIdentifier mig_task;
842 * ID of task that collects blocks for DHT PUTs.
844 static GNUNET_SCHEDULER_TaskIdentifier dht_task;
847 * What is the maximum frequency at which we are allowed to
848 * poll the datastore for migration content?
850 static struct GNUNET_TIME_Relative min_migration_delay;
853 * Handle for DHT operations.
855 static struct GNUNET_DHT_Handle *dht_handle;
858 * Size of the doubly-linked list of migration blocks.
860 static unsigned int mig_size;
863 * Are we allowed to migrate content to this peer.
865 static int active_migration;
868 * How many entires with zero anonymity do we currently estimate
869 * to have in the database?
871 static unsigned int zero_anonymity_count_estimate;
874 * Typical priorities we're seeing from other peers right now. Since
875 * most priorities will be zero, this value is the weighted average of
876 * non-zero priorities seen "recently". In order to ensure that new
877 * values do not dramatically change the ratio, values are first
878 * "capped" to a reasonable range (+N of the current value) and then
879 * averaged into the existing value by a ratio of 1:N. Hence
880 * receiving the largest possible priority can still only raise our
881 * "current_priorities" by at most 1.
883 static double current_priorities;
886 * Datastore 'GET' load tracking.
888 static struct GNUNET_LOAD_Value *datastore_get_load;
891 * Datastore 'PUT' load tracking.
893 static struct GNUNET_LOAD_Value *datastore_put_load;
896 * How long do requests typically stay in the routing table?
898 static struct GNUNET_LOAD_Value *rt_entry_lifetime;
901 * We've just now completed a datastore request. Update our
902 * datastore load calculations.
904 * @param start time when the datastore request was issued
907 update_datastore_delays (struct GNUNET_TIME_Absolute start)
909 struct GNUNET_TIME_Relative delay;
911 delay = GNUNET_TIME_absolute_get_duration (start);
912 GNUNET_LOAD_update (datastore_get_load,
918 * Get the filename under which we would store the GNUNET_HELLO_Message
919 * for the given host and protocol.
920 * @return filename of the form DIRECTORY/HOSTID
923 get_trust_filename (const struct GNUNET_PeerIdentity *id)
925 struct GNUNET_CRYPTO_HashAsciiEncoded fil;
928 GNUNET_CRYPTO_hash_to_enc (&id->hashPubKey, &fil);
929 GNUNET_asprintf (&fn, "%s%s%s", trustDirectory, DIR_SEPARATOR_STR, &fil);
936 * Transmit messages by copying it to the target buffer
937 * "buf". "buf" will be NULL and "size" zero if the socket was closed
938 * for writing in the meantime. In that case, do nothing
939 * (the disconnect or shutdown handler will take care of the rest).
940 * If we were able to transmit messages and there are still more
941 * pending, ask core again for further calls to this function.
943 * @param cls closure, pointer to the 'struct ConnectedPeer*'
944 * @param size number of bytes available in buf
945 * @param buf where the callee should write the message
946 * @return number of bytes written to buf
949 transmit_to_peer (void *cls,
950 size_t size, void *buf);
953 /* ******************* clean up functions ************************ */
956 * Delete the given migration block.
958 * @param mb block to delete
961 delete_migration_block (struct MigrationReadyBlock *mb)
963 GNUNET_CONTAINER_DLL_remove (mig_head,
966 GNUNET_PEER_decrement_rcs (mb->target_list,
967 MIGRATION_LIST_SIZE);
974 * Compare the distance of two peers to a key.
977 * @param p1 first peer
978 * @param p2 second peer
979 * @return GNUNET_YES if P1 is closer to key than P2
982 is_closer (const GNUNET_HashCode *key,
983 const struct GNUNET_PeerIdentity *p1,
984 const struct GNUNET_PeerIdentity *p2)
986 return GNUNET_CRYPTO_hash_xorcmp (&p1->hashPubKey,
993 * Consider migrating content to a given peer.
995 * @param cls 'struct MigrationReadyBlock*' to select
996 * targets for (or NULL for none)
997 * @param key ID of the peer
998 * @param value 'struct ConnectedPeer' of the peer
999 * @return GNUNET_YES (always continue iteration)
1002 consider_migration (void *cls,
1003 const GNUNET_HashCode *key,
1006 struct MigrationReadyBlock *mb = cls;
1007 struct ConnectedPeer *cp = value;
1008 struct MigrationReadyBlock *pos;
1009 struct GNUNET_PeerIdentity cppid;
1010 struct GNUNET_PeerIdentity otherpid;
1011 struct GNUNET_PeerIdentity worstpid;
1016 /* consider 'cp' as a migration target for mb */
1017 if (GNUNET_TIME_absolute_get_remaining (cp->migration_blocked).value > 0)
1018 return GNUNET_YES; /* peer has requested no migration! */
1021 GNUNET_PEER_resolve (cp->pid,
1023 repl = MIGRATION_LIST_SIZE;
1024 for (i=0;i<MIGRATION_LIST_SIZE;i++)
1026 if (mb->target_list[i] == 0)
1028 mb->target_list[i] = cp->pid;
1029 GNUNET_PEER_change_rc (mb->target_list[i], 1);
1030 repl = MIGRATION_LIST_SIZE;
1033 GNUNET_PEER_resolve (mb->target_list[i],
1035 if ( (repl == MIGRATION_LIST_SIZE) &&
1036 is_closer (&mb->query,
1041 worstpid = otherpid;
1043 else if ( (repl != MIGRATION_LIST_SIZE) &&
1044 (is_closer (&mb->query,
1049 worstpid = otherpid;
1052 if (repl != MIGRATION_LIST_SIZE)
1054 GNUNET_PEER_change_rc (mb->target_list[repl], -1);
1055 mb->target_list[repl] = cp->pid;
1056 GNUNET_PEER_change_rc (mb->target_list[repl], 1);
1060 /* consider scheduling transmission to cp for content migration */
1061 if (cp->cth != NULL)
1067 for (i=0;i<MIGRATION_LIST_SIZE;i++)
1069 if (cp->pid == pos->target_list[i])
1074 msize = GNUNET_MIN (msize,
1082 return GNUNET_YES; /* no content available */
1084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1085 "Trying to migrate at least %u bytes to peer `%s'\n",
1089 if (cp->delayed_transmission_request_task != GNUNET_SCHEDULER_NO_TASK)
1091 GNUNET_SCHEDULER_cancel (sched, cp->delayed_transmission_request_task);
1092 cp->delayed_transmission_request_task = GNUNET_SCHEDULER_NO_TASK;
1095 = GNUNET_CORE_notify_transmit_ready (core,
1096 0, GNUNET_TIME_UNIT_FOREVER_REL,
1097 (const struct GNUNET_PeerIdentity*) key,
1098 msize + sizeof (struct PutMessage),
1106 * Task that is run periodically to obtain blocks for content
1110 * @param tc scheduler context (also unused)
1113 gather_migration_blocks (void *cls,
1114 const struct GNUNET_SCHEDULER_TaskContext *tc);
1120 * Task that is run periodically to obtain blocks for DHT PUTs.
1122 * @param cls type of blocks to gather
1123 * @param tc scheduler context (unused)
1126 gather_dht_put_blocks (void *cls,
1127 const struct GNUNET_SCHEDULER_TaskContext *tc);
1131 * If the migration task is not currently running, consider
1132 * (re)scheduling it with the appropriate delay.
1135 consider_migration_gathering ()
1137 struct GNUNET_TIME_Relative delay;
1143 if (mig_task != GNUNET_SCHEDULER_NO_TASK)
1145 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
1147 delay = GNUNET_TIME_relative_divide (delay,
1148 MAX_MIGRATION_QUEUE);
1149 delay = GNUNET_TIME_relative_max (delay,
1150 min_migration_delay);
1151 mig_task = GNUNET_SCHEDULER_add_delayed (sched,
1153 &gather_migration_blocks,
1159 * If the DHT PUT gathering task is not currently running, consider
1160 * (re)scheduling it with the appropriate delay.
1163 consider_dht_put_gathering (void *cls)
1165 struct GNUNET_TIME_Relative delay;
1171 if (dht_task != GNUNET_SCHEDULER_NO_TASK)
1173 if (zero_anonymity_count_estimate > 0)
1175 delay = GNUNET_TIME_relative_divide (GNUNET_DHT_DEFAULT_REPUBLISH_FREQUENCY,
1176 zero_anonymity_count_estimate);
1177 delay = GNUNET_TIME_relative_min (delay,
1182 /* if we have NO zero-anonymity content yet, wait 5 minutes for some to
1183 (hopefully) appear */
1184 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5);
1186 dht_task = GNUNET_SCHEDULER_add_delayed (sched,
1188 &gather_dht_put_blocks,
1194 * Process content offered for migration.
1196 * @param cls closure
1197 * @param key key for the content
1198 * @param size number of bytes in data
1199 * @param data content stored
1200 * @param type type of the content
1201 * @param priority priority of the content
1202 * @param anonymity anonymity-level for the content
1203 * @param expiration expiration time for the content
1204 * @param uid unique identifier for the datum;
1205 * maybe 0 if no unique identifier is available
1208 process_migration_content (void *cls,
1209 const GNUNET_HashCode * key,
1212 enum GNUNET_BLOCK_Type type,
1215 struct GNUNET_TIME_Absolute
1216 expiration, uint64_t uid)
1218 struct MigrationReadyBlock *mb;
1223 if (mig_size < MAX_MIGRATION_QUEUE)
1224 consider_migration_gathering ();
1227 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
1230 GNUNET_FS_handle_on_demand_block (key, size, data,
1231 type, priority, anonymity,
1233 &process_migration_content,
1236 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
1241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1242 "Retrieved block `%s' of type %u for migration\n",
1246 mb = GNUNET_malloc (sizeof (struct MigrationReadyBlock) + size);
1248 mb->expiration = expiration;
1251 memcpy (&mb[1], data, size);
1252 GNUNET_CONTAINER_DLL_insert_after (mig_head,
1257 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
1258 &consider_migration,
1260 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
1265 * Function called upon completion of the DHT PUT operation.
1268 dht_put_continuation (void *cls,
1269 const struct GNUNET_SCHEDULER_TaskContext *tc)
1271 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
1276 * Store content in DHT.
1278 * @param cls closure
1279 * @param key key for the content
1280 * @param size number of bytes in data
1281 * @param data content stored
1282 * @param type type of the content
1283 * @param priority priority of the content
1284 * @param anonymity anonymity-level for the content
1285 * @param expiration expiration time for the content
1286 * @param uid unique identifier for the datum;
1287 * maybe 0 if no unique identifier is available
1290 process_dht_put_content (void *cls,
1291 const GNUNET_HashCode * key,
1294 enum GNUNET_BLOCK_Type type,
1297 struct GNUNET_TIME_Absolute
1298 expiration, uint64_t uid)
1300 static unsigned int counter;
1301 static GNUNET_HashCode last_vhash;
1302 static GNUNET_HashCode vhash;
1307 consider_dht_put_gathering (cls);
1310 /* slightly funky code to estimate the total number of values with zero
1311 anonymity from the maximum observed length of a monotonically increasing
1312 sequence of hashes over the contents */
1313 GNUNET_CRYPTO_hash (data, size, &vhash);
1314 if (GNUNET_CRYPTO_hash_cmp (&vhash, &last_vhash) <= 0)
1316 if (zero_anonymity_count_estimate > 0)
1317 zero_anonymity_count_estimate /= 2;
1323 if (zero_anonymity_count_estimate < (1 << counter))
1324 zero_anonymity_count_estimate = (1 << counter);
1326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1327 "Retrieved block `%s' of type %u for DHT PUT\n",
1331 GNUNET_DHT_put (dht_handle,
1338 GNUNET_TIME_UNIT_FOREVER_REL,
1339 &dht_put_continuation,
1345 * Task that is run periodically to obtain blocks for content
1349 * @param tc scheduler context (also unused)
1352 gather_migration_blocks (void *cls,
1353 const struct GNUNET_SCHEDULER_TaskContext *tc)
1355 mig_task = GNUNET_SCHEDULER_NO_TASK;
1358 mig_qe = GNUNET_DATASTORE_get_random (dsh, 0, UINT_MAX,
1359 GNUNET_TIME_UNIT_FOREVER_REL,
1360 &process_migration_content, NULL);
1361 GNUNET_assert (mig_qe != NULL);
1367 * Task that is run periodically to obtain blocks for DHT PUTs.
1369 * @param cls type of blocks to gather
1370 * @param tc scheduler context (unused)
1373 gather_dht_put_blocks (void *cls,
1374 const struct GNUNET_SCHEDULER_TaskContext *tc)
1376 dht_task = GNUNET_SCHEDULER_NO_TASK;
1379 if (dht_put_type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
1380 dht_put_type = GNUNET_BLOCK_TYPE_FS_KBLOCK;
1381 dht_qe = GNUNET_DATASTORE_get_zero_anonymity (dsh, 0, UINT_MAX,
1382 GNUNET_TIME_UNIT_FOREVER_REL,
1384 &process_dht_put_content, NULL);
1385 GNUNET_assert (dht_qe != NULL);
1391 * We're done with a particular message list entry.
1392 * Free all associated resources.
1394 * @param pml entry to destroy
1397 destroy_pending_message_list_entry (struct PendingMessageList *pml)
1399 GNUNET_CONTAINER_DLL_remove (pml->req->pending_head,
1400 pml->req->pending_tail,
1402 GNUNET_CONTAINER_DLL_remove (pml->target->pending_messages_head,
1403 pml->target->pending_messages_tail,
1405 pml->target->pending_requests--;
1406 GNUNET_free (pml->pm);
1412 * Destroy the given pending message (and call the respective
1415 * @param pm message to destroy
1416 * @param tpid id of peer that the message was delivered to, or 0 for none
1419 destroy_pending_message (struct PendingMessage *pm,
1420 GNUNET_PEER_Id tpid)
1422 struct PendingMessageList *pml = pm->pml;
1423 TransmissionContinuation cont;
1427 cont_cls = pm->cont_cls;
1430 GNUNET_assert (pml->pm == pm);
1431 GNUNET_assert ( (tpid == 0) || (tpid == pml->target->pid) );
1432 destroy_pending_message_list_entry (pml);
1439 cont (cont_cls, tpid);
1444 * We're done processing a particular request.
1445 * Free all associated resources.
1447 * @param pr request to destroy
1450 destroy_pending_request (struct PendingRequest *pr)
1452 struct GNUNET_PeerIdentity pid;
1455 if (pr->hnode != NULL)
1457 GNUNET_CONTAINER_heap_remove_node (requests_by_expiration_heap,
1461 if (NULL == pr->client_request_list)
1463 GNUNET_STATISTICS_update (stats,
1464 gettext_noop ("# P2P searches active"),
1470 GNUNET_STATISTICS_update (stats,
1471 gettext_noop ("# client searches active"),
1476 GNUNET_CONTAINER_multihashmap_remove (query_request_map,
1480 GNUNET_LOAD_update (rt_entry_lifetime,
1481 GNUNET_TIME_absolute_get_duration (pr->start_time).value);
1485 GNUNET_DATASTORE_cancel (pr->qe);
1488 if (pr->dht_get != NULL)
1490 GNUNET_DHT_get_stop (pr->dht_get);
1493 if (pr->client_request_list != NULL)
1495 GNUNET_CONTAINER_DLL_remove (pr->client_request_list->client_list->rl_head,
1496 pr->client_request_list->client_list->rl_tail,
1497 pr->client_request_list);
1498 GNUNET_free (pr->client_request_list);
1499 pr->client_request_list = NULL;
1503 GNUNET_PEER_resolve (pr->cp->pid,
1505 (void) GNUNET_CONTAINER_multihashmap_remove (peer_request_map,
1512 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
1515 if (pr->irc != NULL)
1517 GNUNET_CORE_peer_change_preference_cancel (pr->irc);
1520 if (pr->replies_seen != NULL)
1522 GNUNET_free (pr->replies_seen);
1523 pr->replies_seen = NULL;
1525 if (pr->task != GNUNET_SCHEDULER_NO_TASK)
1527 GNUNET_SCHEDULER_cancel (sched,
1529 pr->task = GNUNET_SCHEDULER_NO_TASK;
1531 while (NULL != pr->pending_head)
1532 destroy_pending_message_list_entry (pr->pending_head);
1533 GNUNET_PEER_change_rc (pr->target_pid, -1);
1534 if (pr->used_targets != NULL)
1536 for (i=0;i<pr->used_targets_off;i++)
1537 GNUNET_PEER_change_rc (pr->used_targets[i].pid, -1);
1538 GNUNET_free (pr->used_targets);
1539 pr->used_targets_off = 0;
1540 pr->used_targets_size = 0;
1541 pr->used_targets = NULL;
1548 * Method called whenever a given peer connects.
1550 * @param cls closure, not used
1551 * @param peer peer identity this notification is about
1552 * @param latency reported latency of the connection with 'other'
1553 * @param distance reported distance (DV) to 'other'
1556 peer_connect_handler (void *cls,
1558 GNUNET_PeerIdentity * peer,
1559 struct GNUNET_TIME_Relative latency,
1562 struct ConnectedPeer *cp;
1563 struct MigrationReadyBlock *pos;
1567 cp = GNUNET_malloc (sizeof (struct ConnectedPeer));
1568 cp->transmission_delay = GNUNET_LOAD_value_init (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1569 cp->pid = GNUNET_PEER_intern (peer);
1571 fn = get_trust_filename (peer);
1572 if ((GNUNET_DISK_file_test (fn) == GNUNET_YES) &&
1573 (sizeof (trust) == GNUNET_DISK_fn_read (fn, &trust, sizeof (trust))))
1574 cp->disk_trust = cp->trust = ntohl (trust);
1577 GNUNET_break (GNUNET_OK ==
1578 GNUNET_CONTAINER_multihashmap_put (connected_peers,
1581 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1586 (void) consider_migration (pos, &peer->hashPubKey, cp);
1593 * Increase the host credit by a value.
1595 * @param host which peer to change the trust value on
1596 * @param value is the int value by which the
1597 * host credit is to be increased or decreased
1598 * @returns the actual change in trust (positive or negative)
1601 change_host_trust (struct ConnectedPeer *host, int value)
1603 unsigned int old_trust;
1607 GNUNET_assert (host != NULL);
1608 old_trust = host->trust;
1611 if (host->trust + value < host->trust)
1613 value = UINT32_MAX - host->trust;
1614 host->trust = UINT32_MAX;
1617 host->trust += value;
1621 if (host->trust < -value)
1623 value = -host->trust;
1627 host->trust += value;
1634 * Write host-trust information to a file - flush the buffer entry!
1637 flush_trust (void *cls,
1638 const GNUNET_HashCode *key,
1641 struct ConnectedPeer *host = value;
1644 struct GNUNET_PeerIdentity pid;
1646 if (host->trust == host->disk_trust)
1647 return GNUNET_OK; /* unchanged */
1648 GNUNET_PEER_resolve (host->pid,
1650 fn = get_trust_filename (&pid);
1651 if (host->trust == 0)
1653 if ((0 != UNLINK (fn)) && (errno != ENOENT))
1654 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
1655 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
1659 trust = htonl (host->trust);
1660 if (sizeof(uint32_t) == GNUNET_DISK_fn_write (fn, &trust,
1662 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE
1663 | GNUNET_DISK_PERM_GROUP_READ | GNUNET_DISK_PERM_OTHER_READ))
1664 host->disk_trust = host->trust;
1671 * Call this method periodically to scan data/hosts for new hosts.
1674 cron_flush_trust (void *cls,
1675 const struct GNUNET_SCHEDULER_TaskContext *tc)
1678 if (NULL == connected_peers)
1680 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
1685 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1687 GNUNET_SCHEDULER_add_delayed (tc->sched,
1688 TRUST_FLUSH_FREQ, &cron_flush_trust, NULL);
1693 * Free (each) request made by the peer.
1695 * @param cls closure, points to peer that the request belongs to
1696 * @param key current key code
1697 * @param value value in the hash map
1698 * @return GNUNET_YES (we should continue to iterate)
1701 destroy_request (void *cls,
1702 const GNUNET_HashCode * key,
1705 const struct GNUNET_PeerIdentity * peer = cls;
1706 struct PendingRequest *pr = value;
1708 GNUNET_break (GNUNET_YES ==
1709 GNUNET_CONTAINER_multihashmap_remove (peer_request_map,
1712 destroy_pending_request (pr);
1718 * Method called whenever a peer disconnects.
1720 * @param cls closure, not used
1721 * @param peer peer identity this notification is about
1724 peer_disconnect_handler (void *cls,
1726 GNUNET_PeerIdentity * peer)
1728 struct ConnectedPeer *cp;
1729 struct PendingMessage *pm;
1731 struct MigrationReadyBlock *pos;
1732 struct MigrationReadyBlock *next;
1734 GNUNET_CONTAINER_multihashmap_get_multiple (peer_request_map,
1738 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
1742 for (i=0;i<CS2P_SUCCESS_LIST_SIZE;i++)
1744 if (NULL != cp->last_client_replies[i])
1746 GNUNET_SERVER_client_drop (cp->last_client_replies[i]);
1747 cp->last_client_replies[i] = NULL;
1750 GNUNET_break (GNUNET_YES ==
1751 GNUNET_CONTAINER_multihashmap_remove (connected_peers,
1754 /* remove this peer from migration considerations; schedule
1757 while (NULL != (pos = next))
1760 for (i=0;i<MIGRATION_LIST_SIZE;i++)
1762 if (pos->target_list[i] == cp->pid)
1764 GNUNET_PEER_change_rc (pos->target_list[i], -1);
1765 pos->target_list[i] = 0;
1768 if (pos->used_targets >= GNUNET_CONTAINER_multihashmap_size (connected_peers))
1770 delete_migration_block (pos);
1771 consider_migration_gathering ();
1774 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
1775 &consider_migration,
1778 GNUNET_PEER_change_rc (cp->pid, -1);
1779 GNUNET_PEER_decrement_rcs (cp->last_p2p_replies, P2P_SUCCESS_LIST_SIZE);
1780 if (NULL != cp->cth)
1782 GNUNET_CORE_notify_transmit_ready_cancel (cp->cth);
1785 if (cp->delayed_transmission_request_task != GNUNET_SCHEDULER_NO_TASK)
1787 GNUNET_SCHEDULER_cancel (sched, cp->delayed_transmission_request_task);
1788 cp->delayed_transmission_request_task = GNUNET_SCHEDULER_NO_TASK;
1790 while (NULL != (pm = cp->pending_messages_head))
1791 destroy_pending_message (pm, 0 /* delivery failed */);
1792 GNUNET_LOAD_value_free (cp->transmission_delay);
1793 GNUNET_break (0 == cp->pending_requests);
1799 * Iterator over hash map entries that removes all occurences
1800 * of the given 'client' from the 'last_client_replies' of the
1801 * given connected peer.
1803 * @param cls closure, the 'struct GNUNET_SERVER_Client*' to remove
1804 * @param key current key code (unused)
1805 * @param value value in the hash map (the 'struct ConnectedPeer*' to change)
1806 * @return GNUNET_YES (we should continue to iterate)
1809 remove_client_from_last_client_replies (void *cls,
1810 const GNUNET_HashCode * key,
1813 struct GNUNET_SERVER_Client *client = cls;
1814 struct ConnectedPeer *cp = value;
1817 for (i=0;i<CS2P_SUCCESS_LIST_SIZE;i++)
1819 if (cp->last_client_replies[i] == client)
1821 GNUNET_SERVER_client_drop (cp->last_client_replies[i]);
1822 cp->last_client_replies[i] = NULL;
1830 * A client disconnected. Remove all of its pending queries.
1832 * @param cls closure, NULL
1833 * @param client identification of the client
1836 handle_client_disconnect (void *cls,
1837 struct GNUNET_SERVER_Client
1840 struct ClientList *pos;
1841 struct ClientList *prev;
1842 struct ClientRequestList *rcl;
1843 struct ClientResponseMessage *creply;
1849 while ( (NULL != pos) &&
1850 (pos->client != client) )
1856 return; /* no requests pending for this client */
1857 while (NULL != (rcl = pos->rl_head))
1859 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1860 "Destroying pending request `%s' on disconnect\n",
1861 GNUNET_h2s (&rcl->req->query));
1862 destroy_pending_request (rcl->req);
1865 client_list = pos->next;
1867 prev->next = pos->next;
1868 if (pos->th != NULL)
1870 GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th);
1873 while (NULL != (creply = pos->res_head))
1875 GNUNET_CONTAINER_DLL_remove (pos->res_head,
1878 GNUNET_free (creply);
1880 GNUNET_SERVER_client_drop (pos->client);
1882 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
1883 &remove_client_from_last_client_replies,
1889 * Iterator to free peer entries.
1891 * @param cls closure, unused
1892 * @param key current key code
1893 * @param value value in the hash map (peer entry)
1894 * @return GNUNET_YES (we should continue to iterate)
1897 clean_peer (void *cls,
1898 const GNUNET_HashCode * key,
1901 peer_disconnect_handler (NULL, (const struct GNUNET_PeerIdentity*) key);
1907 * Task run during shutdown.
1913 shutdown_task (void *cls,
1914 const struct GNUNET_SCHEDULER_TaskContext *tc)
1918 GNUNET_DATASTORE_cancel (mig_qe);
1923 GNUNET_DATASTORE_cancel (dht_qe);
1926 if (GNUNET_SCHEDULER_NO_TASK != mig_task)
1928 GNUNET_SCHEDULER_cancel (sched, mig_task);
1929 mig_task = GNUNET_SCHEDULER_NO_TASK;
1931 if (GNUNET_SCHEDULER_NO_TASK != dht_task)
1933 GNUNET_SCHEDULER_cancel (sched, dht_task);
1934 dht_task = GNUNET_SCHEDULER_NO_TASK;
1936 while (client_list != NULL)
1937 handle_client_disconnect (NULL,
1938 client_list->client);
1939 cron_flush_trust (NULL, NULL);
1940 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
1943 GNUNET_break (0 == GNUNET_CONTAINER_heap_get_size (requests_by_expiration_heap));
1944 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
1945 requests_by_expiration_heap = 0;
1946 GNUNET_CONTAINER_multihashmap_destroy (connected_peers);
1947 connected_peers = NULL;
1948 GNUNET_break (0 == GNUNET_CONTAINER_multihashmap_size (query_request_map));
1949 GNUNET_CONTAINER_multihashmap_destroy (query_request_map);
1950 query_request_map = NULL;
1951 GNUNET_LOAD_value_free (rt_entry_lifetime);
1952 rt_entry_lifetime = NULL;
1953 GNUNET_break (0 == GNUNET_CONTAINER_multihashmap_size (peer_request_map));
1954 GNUNET_CONTAINER_multihashmap_destroy (peer_request_map);
1955 peer_request_map = NULL;
1956 GNUNET_assert (NULL != core);
1957 GNUNET_CORE_disconnect (core);
1961 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1966 GNUNET_DATASTORE_disconnect (dsh,
1970 while (mig_head != NULL)
1971 delete_migration_block (mig_head);
1972 GNUNET_assert (0 == mig_size);
1973 GNUNET_DHT_disconnect (dht_handle);
1975 GNUNET_LOAD_value_free (datastore_get_load);
1976 datastore_get_load = NULL;
1977 GNUNET_LOAD_value_free (datastore_put_load);
1978 datastore_put_load = NULL;
1979 GNUNET_BLOCK_context_destroy (block_ctx);
1981 GNUNET_CONFIGURATION_destroy (block_cfg);
1985 GNUNET_free_non_null (trustDirectory);
1986 trustDirectory = NULL;
1990 /* ******************* Utility functions ******************** */
1994 * We've had to delay a request for transmission to core, but now
1995 * we should be ready. Run it.
1997 * @param cls the 'struct ConnectedPeer' for which a request was delayed
1998 * @param tc task context (unused)
2001 delayed_transmission_request (void *cls,
2002 const struct GNUNET_SCHEDULER_TaskContext *tc)
2004 struct ConnectedPeer *cp = cls;
2005 struct GNUNET_PeerIdentity pid;
2006 struct PendingMessage *pm;
2008 pm = cp->pending_messages_head;
2009 cp->delayed_transmission_request_task = GNUNET_SCHEDULER_NO_TASK;
2010 GNUNET_assert (cp->cth == NULL);
2013 GNUNET_PEER_resolve (cp->pid,
2015 cp->last_transmission_request_start = GNUNET_TIME_absolute_get ();
2016 cp->cth = GNUNET_CORE_notify_transmit_ready (core,
2018 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
2027 * Transmit messages by copying it to the target buffer
2028 * "buf". "buf" will be NULL and "size" zero if the socket was closed
2029 * for writing in the meantime. In that case, do nothing
2030 * (the disconnect or shutdown handler will take care of the rest).
2031 * If we were able to transmit messages and there are still more
2032 * pending, ask core again for further calls to this function.
2034 * @param cls closure, pointer to the 'struct ConnectedPeer*'
2035 * @param size number of bytes available in buf
2036 * @param buf where the callee should write the message
2037 * @return number of bytes written to buf
2040 transmit_to_peer (void *cls,
2041 size_t size, void *buf)
2043 struct ConnectedPeer *cp = cls;
2045 struct PendingMessage *pm;
2046 struct PendingMessage *next_pm;
2047 struct GNUNET_TIME_Absolute now;
2048 struct GNUNET_TIME_Relative min_delay;
2049 struct MigrationReadyBlock *mb;
2050 struct MigrationReadyBlock *next;
2051 struct PutMessage migm;
2054 struct GNUNET_PeerIdentity pid;
2060 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2061 "Dropping message, core too busy.\n");
2063 GNUNET_LOAD_update (cp->transmission_delay,
2067 GNUNET_LOAD_update (cp->transmission_delay,
2068 GNUNET_TIME_absolute_get_duration (cp->last_transmission_request_start).value);
2069 now = GNUNET_TIME_absolute_get ();
2071 min_delay = GNUNET_TIME_UNIT_FOREVER_REL;
2072 next_pm = cp->pending_messages_head;
2073 while ( (NULL != (pm = next_pm) ) &&
2074 (pm->msize <= size) )
2077 if (pm->delay_until.value > now.value)
2079 min_delay = GNUNET_TIME_relative_min (min_delay,
2080 GNUNET_TIME_absolute_get_remaining (pm->delay_until));
2083 memcpy (&cbuf[msize], &pm[1], pm->msize);
2086 if (NULL == pm->pml)
2088 GNUNET_CONTAINER_DLL_remove (cp->pending_messages_head,
2089 cp->pending_messages_tail,
2091 cp->pending_requests--;
2093 destroy_pending_message (pm, cp->pid);
2096 min_delay = GNUNET_TIME_UNIT_ZERO;
2097 if (NULL != cp->pending_messages_head)
2099 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == cp->delayed_transmission_request_task);
2100 cp->delayed_transmission_request_task
2101 = GNUNET_SCHEDULER_add_delayed (sched,
2103 &delayed_transmission_request,
2108 GNUNET_PEER_resolve (cp->pid,
2111 while (NULL != (mb = next))
2114 for (i=0;i<MIGRATION_LIST_SIZE;i++)
2116 if ( (cp->pid == mb->target_list[i]) &&
2117 (mb->size + sizeof (migm) <= size) )
2119 GNUNET_PEER_change_rc (mb->target_list[i], -1);
2120 mb->target_list[i] = 0;
2122 memset (&migm, 0, sizeof (migm));
2123 migm.header.size = htons (sizeof (migm) + mb->size);
2124 migm.header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
2125 migm.type = htonl (mb->type);
2126 migm.expiration = GNUNET_TIME_absolute_hton (mb->expiration);
2127 memcpy (&cbuf[msize], &migm, sizeof (migm));
2128 msize += sizeof (migm);
2129 size -= sizeof (migm);
2130 memcpy (&cbuf[msize], &mb[1], mb->size);
2134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2135 "Pushing migration block `%s' (%u bytes) to `%s'\n",
2136 GNUNET_h2s (&mb->query),
2137 (unsigned int) mb->size,
2145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2146 "Migration block `%s' (%u bytes) is not on migration list for peer `%s'\n",
2147 GNUNET_h2s (&mb->query),
2148 (unsigned int) mb->size,
2153 if ( (mb->used_targets >= MIGRATION_TARGET_COUNT) ||
2154 (mb->used_targets >= GNUNET_CONTAINER_multihashmap_size (connected_peers)) )
2156 delete_migration_block (mb);
2157 consider_migration_gathering ();
2160 consider_migration (NULL,
2165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2166 "Transmitting %u bytes to peer with PID %u\n",
2167 (unsigned int) msize,
2168 (unsigned int) cp->pid);
2175 * Add a message to the set of pending messages for the given peer.
2177 * @param cp peer to send message to
2178 * @param pm message to queue
2179 * @param pr request on which behalf this message is being queued
2182 add_to_pending_messages_for_peer (struct ConnectedPeer *cp,
2183 struct PendingMessage *pm,
2184 struct PendingRequest *pr)
2186 struct PendingMessage *pos;
2187 struct PendingMessageList *pml;
2188 struct GNUNET_PeerIdentity pid;
2190 GNUNET_assert (pm->next == NULL);
2191 GNUNET_assert (pm->pml == NULL);
2194 pml = GNUNET_malloc (sizeof (struct PendingMessageList));
2199 GNUNET_CONTAINER_DLL_insert (pr->pending_head,
2203 pos = cp->pending_messages_head;
2204 while ( (pos != NULL) &&
2205 (pm->priority < pos->priority) )
2207 GNUNET_CONTAINER_DLL_insert_after (cp->pending_messages_head,
2208 cp->pending_messages_tail,
2211 cp->pending_requests++;
2212 if (cp->pending_requests > MAX_QUEUE_PER_PEER)
2214 GNUNET_STATISTICS_update (stats,
2215 gettext_noop ("# P2P searches discarded (queue length bound)"),
2218 destroy_pending_message (cp->pending_messages_tail, 0);
2220 GNUNET_PEER_resolve (cp->pid, &pid);
2221 if (NULL != cp->cth)
2223 GNUNET_CORE_notify_transmit_ready_cancel (cp->cth);
2226 if (cp->delayed_transmission_request_task != GNUNET_SCHEDULER_NO_TASK)
2228 GNUNET_SCHEDULER_cancel (sched, cp->delayed_transmission_request_task);
2229 cp->delayed_transmission_request_task = GNUNET_SCHEDULER_NO_TASK;
2231 /* need to schedule transmission */
2232 cp->last_transmission_request_start = GNUNET_TIME_absolute_get ();
2233 cp->cth = GNUNET_CORE_notify_transmit_ready (core,
2234 cp->pending_messages_head->priority,
2237 cp->pending_messages_head->msize,
2240 if (cp->cth == NULL)
2243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2244 "Failed to schedule transmission with core!\n");
2246 GNUNET_STATISTICS_update (stats,
2247 gettext_noop ("# CORE transmission failures"),
2255 * Test if the DATABASE (GET) load on this peer is too high
2256 * to even consider processing the query at
2259 * @return GNUNET_YES if the load is too high to do anything (load high)
2260 * GNUNET_NO to process normally (load normal)
2261 * GNUNET_SYSERR to process for free (load low)
2264 test_get_load_too_high (uint32_t priority)
2268 ld = GNUNET_LOAD_get_load (datastore_get_load);
2270 return GNUNET_SYSERR;
2280 * Test if the DATABASE (PUT) load on this peer is too high
2281 * to even consider processing the query at
2284 * @return GNUNET_YES if the load is too high to do anything (load high)
2285 * GNUNET_NO to process normally (load normal or low)
2288 test_put_load_too_high (uint32_t priority)
2292 if (GNUNET_LOAD_get_average (datastore_put_load) < 50)
2293 return GNUNET_NO; /* very fast */
2294 ld = GNUNET_LOAD_get_load (datastore_put_load);
2295 if (ld < 2.0 * (1 + priority))
2297 GNUNET_STATISTICS_update (stats,
2298 gettext_noop ("# storage requests dropped due to high load"),
2305 /* ******************* Pending Request Refresh Task ******************** */
2310 * We use a random delay to make the timing of requests less
2311 * predictable. This function returns such a random delay. We add a base
2312 * delay of MAX_CORK_DELAY (1s).
2314 * FIXME: make schedule dependent on the specifics of the request?
2315 * Or bandwidth and number of connected peers and load?
2317 * @return random delay to use for some request, between 1s and 1000+TTL_DECREMENT ms
2319 static struct GNUNET_TIME_Relative
2320 get_processing_delay ()
2323 GNUNET_TIME_relative_add (GNUNET_CONSTANTS_MAX_CORK_DELAY,
2324 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
2325 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2331 * We're processing a GET request from another peer and have decided
2332 * to forward it to other peers. This function is called periodically
2333 * and should forward the request to other peers until we have all
2334 * possible replies. If we have transmitted the *only* reply to
2335 * the initiator we should destroy the pending request. If we have
2336 * many replies in the queue to the initiator, we should delay sending
2337 * out more queries until the reply queue has shrunk some.
2339 * @param cls our "struct ProcessGetContext *"
2343 forward_request_task (void *cls,
2344 const struct GNUNET_SCHEDULER_TaskContext *tc);
2348 * Function called after we either failed or succeeded
2349 * at transmitting a query to a peer.
2351 * @param cls the requests "struct PendingRequest*"
2352 * @param tpid ID of receiving peer, 0 on transmission error
2355 transmit_query_continuation (void *cls,
2356 GNUNET_PEER_Id tpid)
2358 struct PendingRequest *pr = cls;
2361 GNUNET_STATISTICS_update (stats,
2362 gettext_noop ("# queries scheduled for forwarding"),
2368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2369 "Transmission of request failed, will try again later.\n");
2371 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
2372 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
2373 get_processing_delay (),
2374 &forward_request_task,
2379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2380 "Transmitted query `%s'\n",
2381 GNUNET_h2s (&pr->query));
2383 GNUNET_STATISTICS_update (stats,
2384 gettext_noop ("# queries forwarded"),
2387 for (i=0;i<pr->used_targets_off;i++)
2388 if (pr->used_targets[i].pid == tpid)
2389 break; /* found match! */
2390 if (i == pr->used_targets_off)
2392 /* need to create new entry */
2393 if (pr->used_targets_off == pr->used_targets_size)
2394 GNUNET_array_grow (pr->used_targets,
2395 pr->used_targets_size,
2396 pr->used_targets_size * 2 + 2);
2397 GNUNET_PEER_change_rc (tpid, 1);
2398 pr->used_targets[pr->used_targets_off].pid = tpid;
2399 pr->used_targets[pr->used_targets_off].num_requests = 0;
2400 i = pr->used_targets_off++;
2402 pr->used_targets[i].last_request_time = GNUNET_TIME_absolute_get ();
2403 pr->used_targets[i].num_requests++;
2404 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
2405 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
2406 get_processing_delay (),
2407 &forward_request_task,
2413 * How many bytes should a bloomfilter be if we have already seen
2414 * entry_count responses? Note that BLOOMFILTER_K gives us the number
2415 * of bits set per entry. Furthermore, we should not re-size the
2416 * filter too often (to keep it cheap).
2418 * Since other peers will also add entries but not resize the filter,
2419 * we should generally pick a slightly larger size than what the
2420 * strict math would suggest.
2422 * @return must be a power of two and smaller or equal to 2^15.
2425 compute_bloomfilter_size (unsigned int entry_count)
2428 unsigned int ideal = (entry_count * BLOOMFILTER_K) / 4;
2429 uint16_t max = 1 << 15;
2431 if (entry_count > max)
2434 while ((size < max) && (size < ideal))
2443 * Recalculate our bloom filter for filtering replies. This function
2444 * will create a new bloom filter from scratch, so it should only be
2445 * called if we have no bloomfilter at all (and hence can create a
2446 * fresh one of minimal size without problems) OR if our peer is the
2447 * initiator (in which case we may resize to larger than mimimum size).
2449 * @param pr request for which the BF is to be recomputed
2452 refresh_bloomfilter (struct PendingRequest *pr)
2456 GNUNET_HashCode mhash;
2458 nsize = compute_bloomfilter_size (pr->replies_seen_off);
2459 if (nsize == pr->bf_size)
2460 return; /* size not changed */
2462 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
2463 pr->bf_size = nsize;
2464 pr->mingle = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, -1);
2465 pr->bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
2468 for (i=0;i<pr->replies_seen_off;i++)
2470 GNUNET_BLOCK_mingle_hash (&pr->replies_seen[i],
2473 GNUNET_CONTAINER_bloomfilter_add (pr->bf, &mhash);
2479 * Function called after we've tried to reserve a certain amount of
2480 * bandwidth for a reply. Check if we succeeded and if so send our
2483 * @param cls the requests "struct PendingRequest*"
2484 * @param peer identifies the peer
2485 * @param bpm_in set to the current bandwidth limit (receiving) for this peer
2486 * @param bpm_out set to the current bandwidth limit (sending) for this peer
2487 * @param amount set to the amount that was actually reserved or unreserved
2488 * @param preference current traffic preference for the given peer
2491 target_reservation_cb (void *cls,
2493 GNUNET_PeerIdentity * peer,
2494 struct GNUNET_BANDWIDTH_Value32NBO bpm_in,
2495 struct GNUNET_BANDWIDTH_Value32NBO bpm_out,
2497 uint64_t preference)
2499 struct PendingRequest *pr = cls;
2500 struct ConnectedPeer *cp;
2501 struct PendingMessage *pm;
2502 struct GetMessage *gm;
2503 GNUNET_HashCode *ext;
2514 /* error in communication with core, try again later */
2515 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
2516 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
2517 get_processing_delay (),
2518 &forward_request_task,
2522 /* (3) transmit, update ttl/priority */
2523 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
2527 /* Peer must have just left */
2529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2530 "Selected peer disconnected!\n");
2532 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
2533 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
2534 get_processing_delay (),
2535 &forward_request_task,
2539 no_route = GNUNET_NO;
2545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2546 "Failed to reserve bandwidth for reply (got %d/%u bytes only)!\n",
2550 GNUNET_STATISTICS_update (stats,
2551 gettext_noop ("# reply bandwidth reservation requests failed"),
2554 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
2555 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
2556 get_processing_delay (),
2557 &forward_request_task,
2559 return; /* this target round failed */
2561 no_route = GNUNET_YES;
2564 GNUNET_STATISTICS_update (stats,
2565 gettext_noop ("# queries scheduled for forwarding"),
2568 for (i=0;i<pr->used_targets_off;i++)
2569 if (pr->used_targets[i].pid == cp->pid)
2571 GNUNET_STATISTICS_update (stats,
2572 gettext_noop ("# queries retransmitted to same target"),
2578 /* build message and insert message into priority queue */
2580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2581 "Forwarding request `%s' to `%4s'!\n",
2582 GNUNET_h2s (&pr->query),
2587 if (GNUNET_YES == no_route)
2589 bm |= GET_MESSAGE_BIT_RETURN_TO;
2592 if (pr->namespace != NULL)
2594 bm |= GET_MESSAGE_BIT_SKS_NAMESPACE;
2597 if (pr->target_pid != 0)
2599 bm |= GET_MESSAGE_BIT_TRANSMIT_TO;
2602 msize = sizeof (struct GetMessage) + pr->bf_size + k * sizeof(GNUNET_HashCode);
2603 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
2604 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
2606 gm = (struct GetMessage*) &pm[1];
2607 gm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_GET);
2608 gm->header.size = htons (msize);
2609 gm->type = htonl (pr->type);
2610 pr->remaining_priority /= 2;
2611 gm->priority = htonl (pr->remaining_priority);
2612 gm->ttl = htonl (pr->ttl);
2613 gm->filter_mutator = htonl(pr->mingle);
2614 gm->hash_bitmap = htonl (bm);
2615 gm->query = pr->query;
2616 ext = (GNUNET_HashCode*) &gm[1];
2618 if (GNUNET_YES == no_route)
2619 GNUNET_PEER_resolve (pr->cp->pid, (struct GNUNET_PeerIdentity*) &ext[k++]);
2620 if (pr->namespace != NULL)
2621 memcpy (&ext[k++], pr->namespace, sizeof (GNUNET_HashCode));
2622 if (pr->target_pid != 0)
2623 GNUNET_PEER_resolve (pr->target_pid, (struct GNUNET_PeerIdentity*) &ext[k++]);
2624 bfdata = (char *) &ext[k];
2626 GNUNET_CONTAINER_bloomfilter_get_raw_data (pr->bf,
2629 pm->cont = &transmit_query_continuation;
2631 cp->last_request_times[(cp->last_request_times_off++) % MAX_QUEUE_PER_PEER] = GNUNET_TIME_absolute_get ();
2632 add_to_pending_messages_for_peer (cp, pm, pr);
2637 * Closure used for "target_peer_select_cb".
2639 struct PeerSelectionContext
2642 * The request for which we are selecting
2645 struct PendingRequest *pr;
2648 * Current "prime" target.
2650 struct GNUNET_PeerIdentity target;
2653 * How much do we like this target?
2655 double target_score;
2661 * Function called for each connected peer to determine
2662 * which one(s) would make good targets for forwarding.
2664 * @param cls closure (struct PeerSelectionContext)
2665 * @param key current key code (peer identity)
2666 * @param value value in the hash map (struct ConnectedPeer)
2667 * @return GNUNET_YES if we should continue to
2672 target_peer_select_cb (void *cls,
2673 const GNUNET_HashCode * key,
2676 struct PeerSelectionContext *psc = cls;
2677 struct ConnectedPeer *cp = value;
2678 struct PendingRequest *pr = psc->pr;
2679 struct GNUNET_TIME_Relative delay;
2684 /* 1) check that this peer is not the initiator */
2688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2689 "Skipping initiator in forwarding selection\n");
2691 return GNUNET_YES; /* skip */
2694 /* 2) check if we have already (recently) forwarded to this peer */
2695 /* 2a) this particular request */
2697 for (i=0;i<pr->used_targets_off;i++)
2698 if (pr->used_targets[i].pid == cp->pid)
2700 pc = pr->used_targets[i].num_requests;
2701 GNUNET_assert (pc > 0);
2702 if (0 != GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2703 RETRY_PROBABILITY_INV * pc))
2706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2707 "NOT re-trying query that was previously transmitted %u times\n",
2710 return GNUNET_YES; /* skip */
2717 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2718 "Re-trying query that was previously transmitted %u times to this peer\n",
2722 /* 2b) many other requests to this peer */
2723 delay = GNUNET_TIME_absolute_get_duration (cp->last_request_times[cp->last_request_times_off % MAX_QUEUE_PER_PEER]);
2724 if (delay.value <= cp->avg_delay.value)
2727 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2728 "NOT sending query since we send %u others to this peer in the last %llums\n",
2730 cp->avg_delay.value);
2732 return GNUNET_YES; /* skip */
2735 /* 3) calculate how much we'd like to forward to this peer,
2736 starting with a random value that is strong enough
2737 to at least give any peer a chance sometimes
2738 (compared to the other factors that come later) */
2739 /* 3a) count successful (recent) routes from cp for same source */
2742 score = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2743 P2P_SUCCESS_LIST_SIZE);
2744 for (i=0;i<P2P_SUCCESS_LIST_SIZE;i++)
2745 if (cp->last_p2p_replies[i] == pr->cp->pid)
2746 score += 1.0; /* likely successful based on hot path */
2750 score = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2751 CS2P_SUCCESS_LIST_SIZE);
2752 for (i=0;i<CS2P_SUCCESS_LIST_SIZE;i++)
2753 if (cp->last_client_replies[i] == pr->client_request_list->client_list->client)
2754 score += 1.0; /* likely successful based on hot path */
2756 /* 3b) include latency */
2757 if (cp->avg_delay.value < 4 * TTL_DECREMENT)
2758 score += 1.0; /* likely fast based on latency */
2759 /* 3c) include priorities */
2760 if (cp->avg_priority <= pr->remaining_priority / 2.0)
2761 score += 1.0; /* likely successful based on priorities */
2762 /* 3d) penalize for queue size */
2763 score -= (2.0 * cp->pending_requests / (double) MAX_QUEUE_PER_PEER);
2764 /* 3e) include peer proximity */
2765 score -= (2.0 * (GNUNET_CRYPTO_hash_distance_u32 (key,
2766 &pr->query)) / (double) UINT32_MAX);
2767 /* 4) super-bonus for being the known target */
2768 if (pr->target_pid == cp->pid)
2770 /* store best-fit in closure */
2772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2773 "Peer `%s' gets score %f for forwarding query, max is %f\n",
2778 score++; /* avoid zero */
2779 if (score > psc->target_score)
2781 psc->target_score = score;
2782 psc->target.hashPubKey = *key;
2789 * The priority level imposes a bound on the maximum
2790 * value for the ttl that can be requested.
2792 * @param ttl_in requested ttl
2793 * @param prio given priority
2794 * @return ttl_in if ttl_in is below the limit,
2795 * otherwise the ttl-limit for the given priority
2798 bound_ttl (int32_t ttl_in, uint32_t prio)
2800 unsigned long long allowed;
2804 allowed = ((unsigned long long) prio) * TTL_DECREMENT / 1000;
2805 if (ttl_in > allowed)
2807 if (allowed >= (1 << 30))
2816 * Iterator called on each result obtained for a DHT
2817 * operation that expects a reply
2819 * @param cls closure
2820 * @param exp when will this value expire
2821 * @param key key of the result
2822 * @param get_path NULL-terminated array of pointers
2823 * to the peers on reverse GET path (or NULL if not recorded)
2824 * @param put_path NULL-terminated array of pointers
2825 * to the peers on the PUT path (or NULL if not recorded)
2826 * @param type type of the result
2827 * @param size number of bytes in data
2828 * @param data pointer to the result data
2831 process_dht_reply (void *cls,
2832 struct GNUNET_TIME_Absolute exp,
2833 const GNUNET_HashCode * key,
2834 const struct GNUNET_PeerIdentity * const *get_path,
2835 const struct GNUNET_PeerIdentity * const *put_path,
2836 enum GNUNET_BLOCK_Type type,
2842 * We're processing a GET request and have decided
2843 * to forward it to other peers. This function is called periodically
2844 * and should forward the request to other peers until we have all
2845 * possible replies. If we have transmitted the *only* reply to
2846 * the initiator we should destroy the pending request. If we have
2847 * many replies in the queue to the initiator, we should delay sending
2848 * out more queries until the reply queue has shrunk some.
2850 * @param cls our "struct ProcessGetContext *"
2854 forward_request_task (void *cls,
2855 const struct GNUNET_SCHEDULER_TaskContext *tc)
2857 struct PendingRequest *pr = cls;
2858 struct PeerSelectionContext psc;
2859 struct ConnectedPeer *cp;
2860 struct GNUNET_TIME_Relative delay;
2862 pr->task = GNUNET_SCHEDULER_NO_TASK;
2863 if (pr->irc != NULL)
2866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2867 "Forwarding of query `%s' not attempted due to pending local lookup!\n",
2868 GNUNET_h2s (&pr->query));
2870 return; /* already pending */
2872 if (GNUNET_YES == pr->local_only)
2873 return; /* configured to not do P2P search */
2875 if ( (0 == pr->anonymity_level) &&
2876 (GNUNET_YES != pr->forward_only) &&
2877 (pr->type != GNUNET_BLOCK_TYPE_FS_DBLOCK) &&
2878 (pr->type != GNUNET_BLOCK_TYPE_FS_IBLOCK) )
2880 pr->dht_get = GNUNET_DHT_get_start (dht_handle,
2881 GNUNET_TIME_UNIT_FOREVER_REL,
2888 (pr->namespace != NULL) ? sizeof (GNUNET_HashCode) : 0,
2892 /* (1) select target */
2894 psc.target_score = -DBL_MAX;
2895 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
2896 &target_peer_select_cb,
2898 if (psc.target_score == -DBL_MAX)
2900 delay = get_processing_delay ();
2902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2903 "No peer selected for forwarding of query `%s', will try again in %llu ms!\n",
2904 GNUNET_h2s (&pr->query),
2907 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
2909 &forward_request_task,
2911 return; /* nobody selected */
2913 /* (3) update TTL/priority */
2914 if (pr->client_request_list != NULL)
2916 /* FIXME: use better algorithm!? */
2917 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2920 /* bound priority we use by priorities we see from other peers
2921 rounded up (must round up so that we can see non-zero
2922 priorities, but round up as little as possible to make it
2923 plausible that we forwarded another peers request) */
2924 if (pr->priority > current_priorities + 1.0)
2925 pr->priority = (uint32_t) current_priorities + 1.0;
2926 pr->ttl = bound_ttl (pr->ttl + TTL_DECREMENT * 2,
2929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2930 "Trying query `%s' with priority %u and TTL %d.\n",
2931 GNUNET_h2s (&pr->query),
2937 /* (3) reserve reply bandwidth */
2938 if (GNUNET_NO == pr->forward_only)
2940 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
2941 &psc.target.hashPubKey);
2942 GNUNET_assert (NULL != cp);
2943 pr->irc = GNUNET_CORE_peer_change_preference (sched, cfg,
2945 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
2946 GNUNET_BANDWIDTH_value_init (UINT32_MAX),
2949 &target_reservation_cb,
2951 cp->inc_preference = 0;
2955 /* force forwarding */
2956 static struct GNUNET_BANDWIDTH_Value32NBO zerobw;
2957 target_reservation_cb (pr, &psc.target,
2958 zerobw, zerobw, 0, 0.0);
2963 /* **************************** P2P PUT Handling ************************ */
2967 * Function called after we either failed or succeeded
2968 * at transmitting a reply to a peer.
2970 * @param cls the requests "struct PendingRequest*"
2971 * @param tpid ID of receiving peer, 0 on transmission error
2974 transmit_reply_continuation (void *cls,
2975 GNUNET_PEER_Id tpid)
2977 struct PendingRequest *pr = cls;
2981 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
2982 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
2983 /* only one reply expected, done with the request! */
2984 destroy_pending_request (pr);
2986 case GNUNET_BLOCK_TYPE_ANY:
2987 case GNUNET_BLOCK_TYPE_FS_KBLOCK:
2988 case GNUNET_BLOCK_TYPE_FS_SBLOCK:
2998 * Transmit the given message by copying it to the target buffer
2999 * "buf". "buf" will be NULL and "size" zero if the socket was closed
3000 * for writing in the meantime. In that case, do nothing
3001 * (the disconnect or shutdown handler will take care of the rest).
3002 * If we were able to transmit messages and there are still more
3003 * pending, ask core again for further calls to this function.
3005 * @param cls closure, pointer to the 'struct ClientList*'
3006 * @param size number of bytes available in buf
3007 * @param buf where the callee should write the message
3008 * @return number of bytes written to buf
3011 transmit_to_client (void *cls,
3012 size_t size, void *buf)
3014 struct ClientList *cl = cls;
3016 struct ClientResponseMessage *creply;
3023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3024 "Not sending reply, client communication problem.\n");
3029 while ( (NULL != (creply = cl->res_head) ) &&
3030 (creply->msize <= size) )
3032 memcpy (&cbuf[msize], &creply[1], creply->msize);
3033 msize += creply->msize;
3034 size -= creply->msize;
3035 GNUNET_CONTAINER_DLL_remove (cl->res_head,
3038 GNUNET_free (creply);
3041 cl->th = GNUNET_SERVER_notify_transmit_ready (cl->client,
3043 GNUNET_TIME_UNIT_FOREVER_REL,
3044 &transmit_to_client,
3047 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3048 "Transmitted %u bytes to client\n",
3049 (unsigned int) msize);
3056 * Closure for "process_reply" function.
3058 struct ProcessReplyClosure
3061 * The data for the reply.
3066 * Who gave us this reply? NULL for local host (or DHT)
3068 struct ConnectedPeer *sender;
3071 * When the reply expires.
3073 struct GNUNET_TIME_Absolute expiration;
3081 * Type of the block.
3083 enum GNUNET_BLOCK_Type type;
3086 * How much was this reply worth to us?
3091 * Evaluation result (returned).
3093 enum GNUNET_BLOCK_EvaluationResult eval;
3096 * Did we finish processing the associated request?
3101 * Did we find a matching request?
3108 * We have received a reply; handle it!
3110 * @param cls response (struct ProcessReplyClosure)
3111 * @param key our query
3112 * @param value value in the hash map (info about the query)
3113 * @return GNUNET_YES (we should continue to iterate)
3116 process_reply (void *cls,
3117 const GNUNET_HashCode * key,
3120 struct ProcessReplyClosure *prq = cls;
3121 struct PendingRequest *pr = value;
3122 struct PendingMessage *reply;
3123 struct ClientResponseMessage *creply;
3124 struct ClientList *cl;
3125 struct PutMessage *pm;
3126 struct ConnectedPeer *cp;
3127 struct GNUNET_TIME_Relative cur_delay;
3129 struct GNUNET_TIME_Relative art_delay;
3135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3136 "Matched result (type %u) for query `%s' with pending request\n",
3137 (unsigned int) prq->type,
3140 GNUNET_STATISTICS_update (stats,
3141 gettext_noop ("# replies received and matched"),
3144 if (prq->sender != NULL)
3146 for (i=0;i<pr->used_targets_off;i++)
3147 if (pr->used_targets[i].pid == prq->sender->pid)
3149 if (i < pr->used_targets_off)
3151 cur_delay = GNUNET_TIME_absolute_get_duration (pr->used_targets[i].last_request_time);
3152 prq->sender->avg_delay.value
3153 = (prq->sender->avg_delay.value *
3154 (RUNAVG_DELAY_N - 1) + cur_delay.value) / RUNAVG_DELAY_N;
3155 prq->sender->avg_priority
3156 = (prq->sender->avg_priority *
3157 (RUNAVG_DELAY_N - 1) + pr->priority) / (double) RUNAVG_DELAY_N;
3161 GNUNET_PEER_change_rc (prq->sender->last_p2p_replies
3162 [prq->sender->last_p2p_replies_woff % P2P_SUCCESS_LIST_SIZE],
3164 GNUNET_PEER_change_rc (pr->cp->pid, 1);
3165 prq->sender->last_p2p_replies
3166 [(prq->sender->last_p2p_replies_woff++) % P2P_SUCCESS_LIST_SIZE]
3171 if (NULL != prq->sender->last_client_replies
3172 [(prq->sender->last_client_replies_woff) % CS2P_SUCCESS_LIST_SIZE])
3173 GNUNET_SERVER_client_drop (prq->sender->last_client_replies
3174 [(prq->sender->last_client_replies_woff) % CS2P_SUCCESS_LIST_SIZE]);
3175 prq->sender->last_client_replies
3176 [(prq->sender->last_client_replies_woff++) % CS2P_SUCCESS_LIST_SIZE]
3177 = pr->client_request_list->client_list->client;
3178 GNUNET_SERVER_client_keep (pr->client_request_list->client_list->client);
3181 prq->eval = GNUNET_BLOCK_evaluate (block_ctx,
3186 pr->namespace, (pr->namespace != NULL) ? sizeof (GNUNET_HashCode) : 0,
3191 case GNUNET_BLOCK_EVALUATION_OK_MORE:
3193 case GNUNET_BLOCK_EVALUATION_OK_LAST:
3194 while (NULL != pr->pending_head)
3195 destroy_pending_message_list_entry (pr->pending_head);
3198 if (pr->client_request_list != NULL)
3199 GNUNET_SERVER_receive_done (pr->client_request_list->client_list->client,
3201 GNUNET_DATASTORE_cancel (pr->qe);
3204 pr->do_remove = GNUNET_YES;
3205 if (pr->task != GNUNET_SCHEDULER_NO_TASK)
3207 GNUNET_SCHEDULER_cancel (sched,
3209 pr->task = GNUNET_SCHEDULER_NO_TASK;
3211 GNUNET_break (GNUNET_YES ==
3212 GNUNET_CONTAINER_multihashmap_remove (query_request_map,
3215 GNUNET_LOAD_update (rt_entry_lifetime,
3216 GNUNET_TIME_absolute_get_duration (pr->start_time).value);
3218 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
3219 GNUNET_STATISTICS_update (stats,
3220 gettext_noop ("# duplicate replies discarded (bloomfilter)"),
3224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3225 "Duplicate response `%s', discarding.\n",
3226 GNUNET_h2s (&mhash));
3228 return GNUNET_YES; /* duplicate */
3229 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
3230 return GNUNET_YES; /* wrong namespace */
3231 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
3234 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
3237 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
3238 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3239 _("Unsupported block type %u\n"),
3243 if (pr->client_request_list != NULL)
3245 if (pr->replies_seen_size == pr->replies_seen_off)
3246 GNUNET_array_grow (pr->replies_seen,
3247 pr->replies_seen_size,
3248 pr->replies_seen_size * 2 + 4);
3249 GNUNET_CRYPTO_hash (prq->data,
3251 &pr->replies_seen[pr->replies_seen_off++]);
3252 refresh_bloomfilter (pr);
3254 if (NULL == prq->sender)
3257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3258 "Found result for query `%s' in local datastore\n",
3261 GNUNET_STATISTICS_update (stats,
3262 gettext_noop ("# results found locally"),
3266 prq->priority += pr->remaining_priority;
3267 pr->remaining_priority = 0;
3268 pr->results_found++;
3269 prq->request_found = GNUNET_YES;
3270 if (NULL != pr->client_request_list)
3272 GNUNET_STATISTICS_update (stats,
3273 gettext_noop ("# replies received for local clients"),
3276 cl = pr->client_request_list->client_list;
3277 msize = sizeof (struct PutMessage) + prq->size;
3278 creply = GNUNET_malloc (msize + sizeof (struct ClientResponseMessage));
3279 creply->msize = msize;
3280 creply->client_list = cl;
3281 GNUNET_CONTAINER_DLL_insert_after (cl->res_head,
3285 pm = (struct PutMessage*) &creply[1];
3286 pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
3287 pm->header.size = htons (msize);
3288 pm->type = htonl (prq->type);
3289 pm->expiration = GNUNET_TIME_absolute_hton (prq->expiration);
3290 memcpy (&pm[1], prq->data, prq->size);
3294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3295 "Transmitting result for query `%s' to client\n",
3298 cl->th = GNUNET_SERVER_notify_transmit_ready (cl->client,
3300 GNUNET_TIME_UNIT_FOREVER_REL,
3301 &transmit_to_client,
3304 GNUNET_break (cl->th != NULL);
3307 prq->finished = GNUNET_YES;
3308 destroy_pending_request (pr);
3315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3316 "Transmitting result for query `%s' to other peer (PID=%u)\n",
3318 (unsigned int) cp->pid);
3320 GNUNET_STATISTICS_update (stats,
3321 gettext_noop ("# replies received for other peers"),
3324 msize = sizeof (struct PutMessage) + prq->size;
3325 reply = GNUNET_malloc (msize + sizeof (struct PendingMessage));
3326 reply->cont = &transmit_reply_continuation;
3327 reply->cont_cls = pr;
3329 art_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
3330 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
3333 = GNUNET_TIME_relative_to_absolute (art_delay);
3334 GNUNET_STATISTICS_update (stats,
3335 gettext_noop ("cummulative artificial delay introduced (ms)"),
3339 reply->msize = msize;
3340 reply->priority = UINT32_MAX; /* send replies first! */
3341 pm = (struct PutMessage*) &reply[1];
3342 pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
3343 pm->header.size = htons (msize);
3344 pm->type = htonl (prq->type);
3345 pm->expiration = GNUNET_TIME_absolute_hton (prq->expiration);
3346 memcpy (&pm[1], prq->data, prq->size);
3347 add_to_pending_messages_for_peer (cp, reply, pr);
3354 * Iterator called on each result obtained for a DHT
3355 * operation that expects a reply
3357 * @param cls closure
3358 * @param exp when will this value expire
3359 * @param key key of the result
3360 * @param get_path NULL-terminated array of pointers
3361 * to the peers on reverse GET path (or NULL if not recorded)
3362 * @param put_path NULL-terminated array of pointers
3363 * to the peers on the PUT path (or NULL if not recorded)
3364 * @param type type of the result
3365 * @param size number of bytes in data
3366 * @param data pointer to the result data
3369 process_dht_reply (void *cls,
3370 struct GNUNET_TIME_Absolute exp,
3371 const GNUNET_HashCode * key,
3372 const struct GNUNET_PeerIdentity * const *get_path,
3373 const struct GNUNET_PeerIdentity * const *put_path,
3374 enum GNUNET_BLOCK_Type type,
3378 struct PendingRequest *pr = cls;
3379 struct ProcessReplyClosure prq;
3381 memset (&prq, 0, sizeof (prq));
3383 prq.expiration = exp;
3386 process_reply (&prq, key, pr);
3392 * Continuation called to notify client about result of the
3395 * @param cls closure
3396 * @param success GNUNET_SYSERR on failure
3397 * @param msg NULL on success, otherwise an error message
3400 put_migration_continuation (void *cls,
3404 struct GNUNET_TIME_Absolute *start = cls;
3405 struct GNUNET_TIME_Relative delay;
3407 delay = GNUNET_TIME_absolute_get_duration (*start);
3408 GNUNET_free (start);
3409 GNUNET_LOAD_update (datastore_put_load,
3411 if (GNUNET_OK == success)
3413 GNUNET_STATISTICS_update (stats,
3414 gettext_noop ("# datastore 'put' failures"),
3421 * Handle P2P "PUT" message.
3423 * @param cls closure, always NULL
3424 * @param other the other peer involved (sender or receiver, NULL
3425 * for loopback messages where we are both sender and receiver)
3426 * @param message the actual message
3427 * @param latency reported latency of the connection with 'other'
3428 * @param distance reported distance (DV) to 'other'
3429 * @return GNUNET_OK to keep the connection open,
3430 * GNUNET_SYSERR to close it (signal serious error)
3433 handle_p2p_put (void *cls,
3434 const struct GNUNET_PeerIdentity *other,
3435 const struct GNUNET_MessageHeader *message,
3436 struct GNUNET_TIME_Relative latency,
3439 const struct PutMessage *put;
3442 enum GNUNET_BLOCK_Type type;
3443 struct GNUNET_TIME_Absolute expiration;
3444 GNUNET_HashCode query;
3445 struct ProcessReplyClosure prq;
3446 struct GNUNET_TIME_Absolute *start;
3447 struct GNUNET_TIME_Relative block_time;
3449 struct ConnectedPeer *cp;
3450 struct PendingMessage *pm;
3451 struct MigrationStopMessage *msm;
3453 msize = ntohs (message->size);
3454 if (msize < sizeof (struct PutMessage))
3457 return GNUNET_SYSERR;
3459 put = (const struct PutMessage*) message;
3460 dsize = msize - sizeof (struct PutMessage);
3461 type = ntohl (put->type);
3462 expiration = GNUNET_TIME_absolute_ntoh (put->expiration);
3464 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
3465 return GNUNET_SYSERR;
3467 GNUNET_BLOCK_get_key (block_ctx,
3473 GNUNET_break_op (0);
3474 return GNUNET_SYSERR;
3477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3478 "Received result for query `%s' from peer `%4s'\n",
3479 GNUNET_h2s (&query),
3480 GNUNET_i2s (other));
3482 GNUNET_STATISTICS_update (stats,
3483 gettext_noop ("# replies received (overall)"),
3486 /* now, lookup 'query' */
3487 prq.data = (const void*) &put[1];
3489 prq.sender = GNUNET_CONTAINER_multihashmap_get (connected_peers,
3490 &other->hashPubKey);
3495 prq.expiration = expiration;
3497 prq.finished = GNUNET_NO;
3498 prq.request_found = GNUNET_NO;
3499 GNUNET_CONTAINER_multihashmap_get_multiple (query_request_map,
3503 if (prq.sender != NULL)
3505 prq.sender->inc_preference += CONTENT_BANDWIDTH_VALUE + 1000 * prq.priority;
3506 prq.sender->trust += prq.priority;
3508 if ( (GNUNET_YES == active_migration) &&
3509 (GNUNET_NO == test_put_load_too_high (prq.priority)) )
3512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3513 "Replicating result for query `%s' with priority %u\n",
3514 GNUNET_h2s (&query),
3517 start = GNUNET_malloc (sizeof (struct GNUNET_TIME_Absolute));
3518 *start = GNUNET_TIME_absolute_get ();
3519 GNUNET_DATASTORE_put (dsh,
3520 0, &query, dsize, &put[1],
3521 type, prq.priority, 1 /* anonymity */,
3523 1 + prq.priority, MAX_DATASTORE_QUEUE,
3524 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
3525 &put_migration_continuation,
3528 putl = GNUNET_LOAD_get_load (datastore_put_load);
3529 if ( (GNUNET_NO == prq.request_found) &&
3530 ( (GNUNET_YES != active_migration) ||
3531 (putl > 2.5 * (1 + prq.priority)) ) )
3533 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
3534 &other->hashPubKey);
3535 if (GNUNET_TIME_absolute_get_duration (cp->last_migration_block).value < 5000)
3536 return GNUNET_OK; /* already blocked */
3537 /* We're too busy; send MigrationStop message! */
3538 if (GNUNET_YES != active_migration)
3539 putl = 1.0 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 5);
3540 block_time = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
3541 5000 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
3542 (unsigned int) (60000 * putl * putl)));
3544 cp->last_migration_block = GNUNET_TIME_relative_to_absolute (block_time);
3545 pm = GNUNET_malloc (sizeof (struct PendingMessage) +
3546 sizeof (struct MigrationStopMessage));
3547 pm->msize = sizeof (struct MigrationStopMessage);
3548 pm->priority = UINT32_MAX;
3549 msm = (struct MigrationStopMessage*) &pm[1];
3550 msm->header.size = htons (sizeof (struct MigrationStopMessage));
3551 msm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_MIGRATION_STOP);
3552 msm->duration = GNUNET_TIME_relative_hton (block_time);
3553 add_to_pending_messages_for_peer (cp,
3562 * Handle P2P "MIGRATION_STOP" message.
3564 * @param cls closure, always NULL
3565 * @param other the other peer involved (sender or receiver, NULL
3566 * for loopback messages where we are both sender and receiver)
3567 * @param message the actual message
3568 * @param latency reported latency of the connection with 'other'
3569 * @param distance reported distance (DV) to 'other'
3570 * @return GNUNET_OK to keep the connection open,
3571 * GNUNET_SYSERR to close it (signal serious error)
3574 handle_p2p_migration_stop (void *cls,
3575 const struct GNUNET_PeerIdentity *other,
3576 const struct GNUNET_MessageHeader *message,
3577 struct GNUNET_TIME_Relative latency,
3580 struct ConnectedPeer *cp;
3581 const struct MigrationStopMessage *msm;
3583 msm = (const struct MigrationStopMessage*) message;
3584 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
3585 &other->hashPubKey);
3591 cp->migration_blocked = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_ntoh (msm->duration));
3597 /* **************************** P2P GET Handling ************************ */
3601 * Closure for 'check_duplicate_request_{peer,client}'.
3603 struct CheckDuplicateRequestClosure
3606 * The new request we should check if it already exists.
3608 const struct PendingRequest *pr;
3611 * Existing request found by the checker, NULL if none.
3613 struct PendingRequest *have;
3618 * Iterator over entries in the 'query_request_map' that
3619 * tries to see if we have the same request pending from
3620 * the same client already.
3622 * @param cls closure (our 'struct CheckDuplicateRequestClosure')
3623 * @param key current key code (query, ignored, must match)
3624 * @param value value in the hash map (a 'struct PendingRequest'
3625 * that already exists)
3626 * @return GNUNET_YES if we should continue to
3627 * iterate (no match yet)
3628 * GNUNET_NO if not (match found).
3631 check_duplicate_request_client (void *cls,
3632 const GNUNET_HashCode * key,
3635 struct CheckDuplicateRequestClosure *cdc = cls;
3636 struct PendingRequest *have = value;
3638 if (have->client_request_list == NULL)
3640 if ( (cdc->pr->client_request_list->client_list->client == have->client_request_list->client_list->client) &&
3651 * We're processing (local) results for a search request
3652 * from another peer. Pass applicable results to the
3653 * peer and if we are done either clean up (operation
3654 * complete) or forward to other peers (more results possible).
3656 * @param cls our closure (struct LocalGetContext)
3657 * @param key key for the content
3658 * @param size number of bytes in data
3659 * @param data content stored
3660 * @param type type of the content
3661 * @param priority priority of the content
3662 * @param anonymity anonymity-level for the content
3663 * @param expiration expiration time for the content
3664 * @param uid unique identifier for the datum;
3665 * maybe 0 if no unique identifier is available
3668 process_local_reply (void *cls,
3669 const GNUNET_HashCode * key,
3672 enum GNUNET_BLOCK_Type type,
3675 struct GNUNET_TIME_Absolute
3679 struct PendingRequest *pr = cls;
3680 struct ProcessReplyClosure prq;
3681 struct CheckDuplicateRequestClosure cdrc;
3682 GNUNET_HashCode query;
3683 unsigned int old_rf;
3688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3689 "Done processing local replies, forwarding request to other peers.\n");
3692 if (pr->client_request_list != NULL)
3694 GNUNET_SERVER_receive_done (pr->client_request_list->client_list->client,
3696 /* Figure out if this is a duplicate request and possibly
3697 merge 'struct PendingRequest' entries */
3700 GNUNET_CONTAINER_multihashmap_get_multiple (query_request_map,
3702 &check_duplicate_request_client,
3704 if (cdrc.have != NULL)
3707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3708 "Received request for block `%s' twice from client, will only request once.\n",
3709 GNUNET_h2s (&pr->query));
3712 destroy_pending_request (pr);
3716 if (pr->local_only == GNUNET_YES)
3718 destroy_pending_request (pr);
3721 /* no more results */
3722 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
3723 pr->task = GNUNET_SCHEDULER_add_now (sched,
3724 &forward_request_task,
3729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3730 "New local response to `%s' of type %u.\n",
3734 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
3737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3738 "Found ONDEMAND block, performing on-demand encoding\n");
3740 GNUNET_STATISTICS_update (stats,
3741 gettext_noop ("# on-demand blocks matched requests"),
3745 GNUNET_FS_handle_on_demand_block (key, size, data, type, priority,
3746 anonymity, expiration, uid,
3747 &process_local_reply,
3751 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
3755 old_rf = pr->results_found;
3756 memset (&prq, 0, sizeof (prq));
3758 prq.expiration = expiration;
3761 GNUNET_BLOCK_get_key (block_ctx,
3768 GNUNET_DATASTORE_remove (dsh,
3772 GNUNET_TIME_UNIT_FOREVER_REL,
3774 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
3778 prq.priority = priority;
3779 prq.finished = GNUNET_NO;
3780 prq.request_found = GNUNET_NO;
3781 if ( (old_rf == 0) &&
3782 (pr->results_found == 0) )
3783 update_datastore_delays (pr->start_time);
3784 process_reply (&prq, key, pr);
3785 if (prq.finished == GNUNET_YES)
3788 return; /* done here */
3789 if (prq.eval == GNUNET_BLOCK_EVALUATION_OK_LAST)
3791 pr->local_only = GNUNET_YES; /* do not forward */
3792 GNUNET_DATASTORE_get_next (dsh, GNUNET_NO);
3795 if ( (pr->client_request_list == NULL) &&
3796 ( (GNUNET_YES == test_get_load_too_high (0)) ||
3797 (pr->results_found > 5 + 2 * pr->priority) ) )
3800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3801 "Load too high, done with request\n");
3803 GNUNET_STATISTICS_update (stats,
3804 gettext_noop ("# processing result set cut short due to load"),
3807 /* FIXME: if this is activated, we might stall large downloads
3808 indefinitely since (presumably) the load can never go down again! */
3809 #if ENABLE_LOAD_MGMT
3810 GNUNET_DATASTORE_get_next (dsh, GNUNET_NO);
3814 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
3819 * We've received a request with the specified priority. Bound it
3820 * according to how much we trust the given peer.
3822 * @param prio_in requested priority
3823 * @param cp the peer making the request
3824 * @return effective priority
3827 bound_priority (uint32_t prio_in,
3828 struct ConnectedPeer *cp)
3830 #define N ((double)128.0)
3835 ld = test_get_load_too_high (0);
3836 if (ld == GNUNET_SYSERR)
3838 GNUNET_STATISTICS_update (stats,
3839 gettext_noop ("# requests done for free (low load)"),
3842 return 0; /* excess resources */
3844 ret = change_host_trust (cp, prio_in);
3847 if (ret > current_priorities + N)
3848 rret = current_priorities + N;
3852 = (current_priorities * (N-1) + rret)/N;
3854 if ( (ld == GNUNET_YES) && (ret > 0) )
3856 /* try with charging */
3857 ld = test_get_load_too_high (ret);
3859 if (ld == GNUNET_YES)
3861 GNUNET_STATISTICS_update (stats,
3862 gettext_noop ("# request dropped, priority insufficient"),
3867 change_host_trust (cp, -ret);
3868 return -1; /* not enough resources */
3872 GNUNET_STATISTICS_update (stats,
3873 gettext_noop ("# requests done for a price (normal load)"),
3883 * Iterator over entries in the 'query_request_map' that
3884 * tries to see if we have the same request pending from
3885 * the same peer already.
3887 * @param cls closure (our 'struct CheckDuplicateRequestClosure')
3888 * @param key current key code (query, ignored, must match)
3889 * @param value value in the hash map (a 'struct PendingRequest'
3890 * that already exists)
3891 * @return GNUNET_YES if we should continue to
3892 * iterate (no match yet)
3893 * GNUNET_NO if not (match found).
3896 check_duplicate_request_peer (void *cls,
3897 const GNUNET_HashCode * key,
3900 struct CheckDuplicateRequestClosure *cdc = cls;
3901 struct PendingRequest *have = value;
3903 if (cdc->pr->target_pid == have->target_pid)
3913 * Handle P2P "GET" request.
3915 * @param cls closure, always NULL
3916 * @param other the other peer involved (sender or receiver, NULL
3917 * for loopback messages where we are both sender and receiver)
3918 * @param message the actual message
3919 * @param latency reported latency of the connection with 'other'
3920 * @param distance reported distance (DV) to 'other'
3921 * @return GNUNET_OK to keep the connection open,
3922 * GNUNET_SYSERR to close it (signal serious error)
3925 handle_p2p_get (void *cls,
3926 const struct GNUNET_PeerIdentity *other,
3927 const struct GNUNET_MessageHeader *message,
3928 struct GNUNET_TIME_Relative latency,
3931 struct PendingRequest *pr;
3932 struct ConnectedPeer *cp;
3933 struct ConnectedPeer *cps;
3934 struct CheckDuplicateRequestClosure cdc;
3935 struct GNUNET_TIME_Relative timeout;
3937 const struct GetMessage *gm;
3939 const GNUNET_HashCode *opt;
3942 uint32_t ttl_decrement;
3944 enum GNUNET_BLOCK_Type type;
3947 msize = ntohs(message->size);
3948 if (msize < sizeof (struct GetMessage))
3950 GNUNET_break_op (0);
3951 return GNUNET_SYSERR;
3953 gm = (const struct GetMessage*) message;
3955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3956 "Received request for `%s'\n",
3957 GNUNET_h2s (&gm->query));
3959 type = ntohl (gm->type);
3960 bm = ntohl (gm->hash_bitmap);
3968 if (msize < sizeof (struct GetMessage) + bits * sizeof (GNUNET_HashCode))
3970 GNUNET_break_op (0);
3971 return GNUNET_SYSERR;
3973 opt = (const GNUNET_HashCode*) &gm[1];
3974 bfsize = msize - sizeof (struct GetMessage) + bits * sizeof (GNUNET_HashCode);
3975 bm = ntohl (gm->hash_bitmap);
3977 cps = GNUNET_CONTAINER_multihashmap_get (connected_peers,
3978 &other->hashPubKey);
3981 /* peer must have just disconnected */
3982 GNUNET_STATISTICS_update (stats,
3983 gettext_noop ("# requests dropped due to initiator not being connected"),
3986 return GNUNET_SYSERR;
3988 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
3989 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
3996 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
3997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3998 "Failed to find RETURN-TO peer `%4s' in connection set. Dropping query.\n",
3999 GNUNET_i2s ((const struct GNUNET_PeerIdentity*) &opt[bits-1]));
4002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4003 "Failed to find peer `%4s' in connection set. Dropping query.\n",
4004 GNUNET_i2s (other));
4006 GNUNET_STATISTICS_update (stats,
4007 gettext_noop ("# requests dropped due to missing reverse route"),
4010 /* FIXME: try connect? */
4013 /* note that we can really only check load here since otherwise
4014 peers could find out that we are overloaded by not being
4015 disconnected after sending us a malformed query... */
4016 priority = bound_priority (ntohl (gm->priority), cps);
4020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4021 "Dropping query from `%s', this peer is too busy.\n",
4022 GNUNET_i2s (other));
4024 GNUNET_STATISTICS_update (stats,
4025 gettext_noop ("# requests dropped due to high load"),
4028 #if ENABLE_LOAD_MGMT
4029 /* FIXME: this causes problems... */
4034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4035 "Received request for `%s' of type %u from peer `%4s' with flags %u\n",
4036 GNUNET_h2s (&gm->query),
4037 (unsigned int) type,
4041 have_ns = (0 != (bm & GET_MESSAGE_BIT_SKS_NAMESPACE));
4042 pr = GNUNET_malloc (sizeof (struct PendingRequest) +
4043 (have_ns ? sizeof(GNUNET_HashCode) : 0));
4046 pr->namespace = (GNUNET_HashCode*) &pr[1];
4047 memcpy (&pr[1], &opt[bits++], sizeof (GNUNET_HashCode));
4049 if ( (GNUNET_LOAD_get_load (cp->transmission_delay) > 3 * (1 + priority)) ||
4050 (GNUNET_LOAD_get_average (cp->transmission_delay) >
4051 GNUNET_CONSTANTS_MAX_CORK_DELAY.value * 2 + GNUNET_LOAD_get_average (rt_entry_lifetime)) )
4053 /* don't have BW to send to peer, or would likely take longer than we have for it,
4054 so at best indirect the query */
4056 #if ENABLE_LOAD_MGMT
4057 /* FIXME: if this line is enabled, the 'perf' test for larger files simply "hangs";
4058 the cause seems to be that the load goes up (to the point where we do this)
4059 and then never goes down again... (outch) */
4060 pr->forward_only = GNUNET_YES;
4064 pr->mingle = ntohl (gm->filter_mutator);
4065 if (0 != (bm & GET_MESSAGE_BIT_TRANSMIT_TO))
4066 pr->target_pid = GNUNET_PEER_intern ((const struct GNUNET_PeerIdentity*) &opt[bits++]);
4067 pr->anonymity_level = 1;
4068 pr->priority = (uint32_t) priority;
4069 pr->ttl = bound_ttl (ntohl (gm->ttl), pr->priority);
4070 pr->query = gm->query;
4071 /* decrement ttl (always) */
4072 ttl_decrement = 2 * TTL_DECREMENT +
4073 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
4075 if ( (pr->ttl < 0) &&
4076 (((int32_t)(pr->ttl - ttl_decrement)) > 0) )
4079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4080 "Dropping query from `%s' due to TTL underflow (%d - %u).\n",
4085 GNUNET_STATISTICS_update (stats,
4086 gettext_noop ("# requests dropped due TTL underflow"),
4089 /* integer underflow => drop (should be very rare)! */
4093 pr->ttl -= ttl_decrement;
4094 pr->start_time = GNUNET_TIME_absolute_get ();
4096 /* get bloom filter */
4099 pr->bf = GNUNET_CONTAINER_bloomfilter_init ((const char*) &opt[bits],
4102 pr->bf_size = bfsize;
4106 GNUNET_CONTAINER_multihashmap_get_multiple (query_request_map,
4108 &check_duplicate_request_peer,
4110 if (cdc.have != NULL)
4112 if (cdc.have->start_time.value + cdc.have->ttl >=
4113 pr->start_time.value + pr->ttl)
4115 /* existing request has higher TTL, drop new one! */
4116 cdc.have->priority += pr->priority;
4117 destroy_pending_request (pr);
4119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4120 "Have existing request with higher TTL, dropping new request.\n",
4121 GNUNET_i2s (other));
4123 GNUNET_STATISTICS_update (stats,
4124 gettext_noop ("# requests dropped due to higher-TTL request"),
4131 /* existing request has lower TTL, drop old one! */
4132 pr->priority += cdc.have->priority;
4133 /* Possible optimization: if we have applicable pending
4134 replies in 'cdc.have', we might want to move those over
4135 (this is a really rare special-case, so it is not clear
4136 that this would be worth it) */
4137 destroy_pending_request (cdc.have);
4138 /* keep processing 'pr'! */
4143 GNUNET_break (GNUNET_OK ==
4144 GNUNET_CONTAINER_multihashmap_put (query_request_map,
4147 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
4148 GNUNET_break (GNUNET_OK ==
4149 GNUNET_CONTAINER_multihashmap_put (peer_request_map,
4152 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
4154 pr->hnode = GNUNET_CONTAINER_heap_insert (requests_by_expiration_heap,
4156 pr->start_time.value + pr->ttl);
4158 GNUNET_STATISTICS_update (stats,
4159 gettext_noop ("# P2P searches received"),
4162 GNUNET_STATISTICS_update (stats,
4163 gettext_noop ("# P2P searches active"),
4167 /* calculate change in traffic preference */
4168 cps->inc_preference += pr->priority * 1000 + QUERY_BANDWIDTH_VALUE;
4169 /* process locally */
4170 if (type == GNUNET_BLOCK_TYPE_FS_DBLOCK)
4171 type = GNUNET_BLOCK_TYPE_ANY; /* to get on-demand as well */
4172 timeout = GNUNET_TIME_relative_multiply (BASIC_DATASTORE_REQUEST_DELAY,
4173 (pr->priority + 1));
4174 if (GNUNET_YES != pr->forward_only)
4177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4178 "Handing request for `%s' to datastore\n",
4179 GNUNET_h2s (&gm->query));
4181 pr->qe = GNUNET_DATASTORE_get (dsh,
4185 MAX_DATASTORE_QUEUE,
4187 &process_local_reply,
4191 GNUNET_STATISTICS_update (stats,
4192 gettext_noop ("# requests dropped by datastore (queue length limit)"),
4199 GNUNET_STATISTICS_update (stats,
4200 gettext_noop ("# requests forwarded due to high load"),
4205 /* Are multiple results possible (and did we look locally)? If so, start processing remotely now! */
4208 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
4209 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
4210 /* only one result, wait for datastore */
4211 if (GNUNET_YES != pr->forward_only)
4213 GNUNET_STATISTICS_update (stats,
4214 gettext_noop ("# requests not instantly forwarded (waiting for datastore)"),
4220 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
4221 pr->task = GNUNET_SCHEDULER_add_now (sched,
4222 &forward_request_task,
4226 /* make sure we don't track too many requests */
4227 if (GNUNET_CONTAINER_heap_get_size (requests_by_expiration_heap) > max_pending_requests)
4229 pr = GNUNET_CONTAINER_heap_peek (requests_by_expiration_heap);
4230 GNUNET_assert (pr != NULL);
4231 destroy_pending_request (pr);
4237 /* **************************** CS GET Handling ************************ */
4241 * Handle START_SEARCH-message (search request from client).
4243 * @param cls closure
4244 * @param client identification of the client
4245 * @param message the actual message
4248 handle_start_search (void *cls,
4249 struct GNUNET_SERVER_Client *client,
4250 const struct GNUNET_MessageHeader *message)
4252 static GNUNET_HashCode all_zeros;
4253 const struct SearchMessage *sm;
4254 struct ClientList *cl;
4255 struct ClientRequestList *crl;
4256 struct PendingRequest *pr;
4259 enum GNUNET_BLOCK_Type type;
4261 msize = ntohs (message->size);
4262 if ( (msize < sizeof (struct SearchMessage)) ||
4263 (0 != (msize - sizeof (struct SearchMessage)) % sizeof (GNUNET_HashCode)) )
4266 GNUNET_SERVER_receive_done (client,
4270 GNUNET_STATISTICS_update (stats,
4271 gettext_noop ("# client searches received"),
4274 sc = (msize - sizeof (struct SearchMessage)) / sizeof (GNUNET_HashCode);
4275 sm = (const struct SearchMessage*) message;
4276 type = ntohl (sm->type);
4278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4279 "Received request for `%s' of type %u from local client\n",
4280 GNUNET_h2s (&sm->query),
4281 (unsigned int) type);
4284 while ( (cl != NULL) &&
4285 (cl->client != client) )
4289 cl = GNUNET_malloc (sizeof (struct ClientList));
4290 cl->client = client;
4291 GNUNET_SERVER_client_keep (client);
4292 cl->next = client_list;
4295 /* detect duplicate KBLOCK requests */
4296 if ( (type == GNUNET_BLOCK_TYPE_FS_KBLOCK) ||
4297 (type == GNUNET_BLOCK_TYPE_FS_NBLOCK) ||
4298 (type == GNUNET_BLOCK_TYPE_ANY) )
4301 while ( (crl != NULL) &&
4302 ( (0 != memcmp (&crl->req->query,
4304 sizeof (GNUNET_HashCode))) ||
4305 (crl->req->type != type) ) )
4310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4311 "Have existing request, merging content-seen lists.\n");
4314 /* Duplicate request (used to send long list of
4315 known/blocked results); merge 'pr->replies_seen'
4316 and update bloom filter */
4317 GNUNET_array_grow (pr->replies_seen,
4318 pr->replies_seen_size,
4319 pr->replies_seen_off + sc);
4320 memcpy (&pr->replies_seen[pr->replies_seen_off],
4322 sc * sizeof (GNUNET_HashCode));
4323 pr->replies_seen_off += sc;
4324 refresh_bloomfilter (pr);
4325 GNUNET_STATISTICS_update (stats,
4326 gettext_noop ("# client searches updated (merged content seen list)"),
4329 GNUNET_SERVER_receive_done (client,
4334 GNUNET_STATISTICS_update (stats,
4335 gettext_noop ("# client searches active"),
4338 pr = GNUNET_malloc (sizeof (struct PendingRequest) +
4339 ((type == GNUNET_BLOCK_TYPE_FS_SBLOCK) ? sizeof(GNUNET_HashCode) : 0));
4340 crl = GNUNET_malloc (sizeof (struct ClientRequestList));
4341 memset (crl, 0, sizeof (struct ClientRequestList));
4342 crl->client_list = cl;
4343 GNUNET_CONTAINER_DLL_insert (cl->rl_head,
4348 pr->client_request_list = crl;
4349 GNUNET_array_grow (pr->replies_seen,
4350 pr->replies_seen_size,
4352 memcpy (pr->replies_seen,
4354 sc * sizeof (GNUNET_HashCode));
4355 pr->replies_seen_off = sc;
4356 pr->anonymity_level = ntohl (sm->anonymity_level);
4357 pr->start_time = GNUNET_TIME_absolute_get ();
4358 refresh_bloomfilter (pr);
4359 pr->query = sm->query;
4360 if (0 == (1 & ntohl (sm->options)))
4361 pr->local_only = GNUNET_NO;
4363 pr->local_only = GNUNET_YES;
4366 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
4367 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
4368 if (0 != memcmp (&sm->target,
4370 sizeof (GNUNET_HashCode)))
4371 pr->target_pid = GNUNET_PEER_intern ((const struct GNUNET_PeerIdentity*) &sm->target);
4373 case GNUNET_BLOCK_TYPE_FS_SBLOCK:
4374 pr->namespace = (GNUNET_HashCode*) &pr[1];
4375 memcpy (&pr[1], &sm->target, sizeof (GNUNET_HashCode));
4380 GNUNET_break (GNUNET_OK ==
4381 GNUNET_CONTAINER_multihashmap_put (query_request_map,
4384 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
4385 if (type == GNUNET_BLOCK_TYPE_FS_DBLOCK)
4386 type = GNUNET_BLOCK_TYPE_ANY; /* get on-demand blocks too! */
4387 pr->qe = GNUNET_DATASTORE_get (dsh,
4391 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
4392 &process_local_reply,
4397 /* **************************** Startup ************************ */
4400 * Process fs requests.
4402 * @param s scheduler to use
4403 * @param server the initialized server
4404 * @param c configuration to use
4407 main_init (struct GNUNET_SCHEDULER_Handle *s,
4408 struct GNUNET_SERVER_Handle *server,
4409 const struct GNUNET_CONFIGURATION_Handle *c)
4411 static const struct GNUNET_CORE_MessageHandler p2p_handlers[] =
4414 GNUNET_MESSAGE_TYPE_FS_GET, 0 },
4416 GNUNET_MESSAGE_TYPE_FS_PUT, 0 },
4417 { &handle_p2p_migration_stop,
4418 GNUNET_MESSAGE_TYPE_FS_MIGRATION_STOP,
4419 sizeof (struct MigrationStopMessage) },
4422 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
4423 {&GNUNET_FS_handle_index_start, NULL,
4424 GNUNET_MESSAGE_TYPE_FS_INDEX_START, 0},
4425 {&GNUNET_FS_handle_index_list_get, NULL,
4426 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_GET, sizeof(struct GNUNET_MessageHeader) },
4427 {&GNUNET_FS_handle_unindex, NULL, GNUNET_MESSAGE_TYPE_FS_UNINDEX,
4428 sizeof (struct UnindexMessage) },
4429 {&handle_start_search, NULL, GNUNET_MESSAGE_TYPE_FS_START_SEARCH,
4433 unsigned long long enc = 128;
4437 stats = GNUNET_STATISTICS_create (sched, "fs", cfg);
4438 min_migration_delay = GNUNET_TIME_UNIT_SECONDS;
4440 GNUNET_CONFIGURATION_get_value_number (cfg,
4442 "MAX_PENDING_REQUESTS",
4443 &max_pending_requests)) ||
4445 GNUNET_CONFIGURATION_get_value_number (cfg,
4447 "EXPECTED_NEIGHBOUR_COUNT",
4450 GNUNET_CONFIGURATION_get_value_time (cfg,
4452 "MIN_MIGRATION_DELAY",
4453 &min_migration_delay)) )
4455 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4456 _("Configuration fails to specify certain parameters, assuming default values."));
4458 connected_peers = GNUNET_CONTAINER_multihashmap_create (enc);
4459 query_request_map = GNUNET_CONTAINER_multihashmap_create (max_pending_requests);
4460 rt_entry_lifetime = GNUNET_LOAD_value_init (GNUNET_TIME_UNIT_SECONDS);
4461 peer_request_map = GNUNET_CONTAINER_multihashmap_create (enc);
4462 requests_by_expiration_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
4463 core = GNUNET_CORE_connect (sched,
4465 GNUNET_TIME_UNIT_FOREVER_REL,
4468 &peer_connect_handler,
4469 &peer_disconnect_handler,
4476 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4477 _("Failed to connect to `%s' service.\n"),
4479 GNUNET_CONTAINER_multihashmap_destroy (connected_peers);
4480 connected_peers = NULL;
4481 GNUNET_CONTAINER_multihashmap_destroy (query_request_map);
4482 query_request_map = NULL;
4483 GNUNET_LOAD_value_free (rt_entry_lifetime);
4484 rt_entry_lifetime = NULL;
4485 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
4486 requests_by_expiration_heap = NULL;
4487 GNUNET_CONTAINER_multihashmap_destroy (peer_request_map);
4488 peer_request_map = NULL;
4491 GNUNET_DATASTORE_disconnect (dsh, GNUNET_NO);
4494 return GNUNET_SYSERR;
4496 /* FIXME: distinguish between sending and storing in options? */
4497 if (active_migration)
4499 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4500 _("Content migration is enabled, will start to gather data\n"));
4501 consider_migration_gathering ();
4503 consider_dht_put_gathering (NULL);
4504 GNUNET_SERVER_disconnect_notify (server,
4505 &handle_client_disconnect,
4507 GNUNET_assert (GNUNET_OK ==
4508 GNUNET_CONFIGURATION_get_value_filename (cfg,
4512 GNUNET_DISK_directory_create (trustDirectory);
4513 GNUNET_SCHEDULER_add_with_priority (sched,
4514 GNUNET_SCHEDULER_PRIORITY_HIGH,
4515 &cron_flush_trust, NULL);
4518 GNUNET_SERVER_add_handlers (server, handlers);
4519 GNUNET_SCHEDULER_add_delayed (sched,
4520 GNUNET_TIME_UNIT_FOREVER_REL,
4528 * Process fs requests.
4530 * @param cls closure
4531 * @param sched scheduler to use
4532 * @param server the initialized server
4533 * @param cfg configuration to use
4537 struct GNUNET_SCHEDULER_Handle *sched,
4538 struct GNUNET_SERVER_Handle *server,
4539 const struct GNUNET_CONFIGURATION_Handle *cfg)
4541 active_migration = GNUNET_CONFIGURATION_get_value_yesno (cfg,
4544 dsh = GNUNET_DATASTORE_connect (cfg,
4548 GNUNET_SCHEDULER_shutdown (sched);
4551 datastore_get_load = GNUNET_LOAD_value_init (DATASTORE_LOAD_AUTODECLINE);
4552 datastore_put_load = GNUNET_LOAD_value_init (DATASTORE_LOAD_AUTODECLINE);
4553 block_cfg = GNUNET_CONFIGURATION_create ();
4554 GNUNET_CONFIGURATION_set_value_string (block_cfg,
4558 block_ctx = GNUNET_BLOCK_context_create (block_cfg);
4559 GNUNET_assert (NULL != block_ctx);
4560 dht_handle = GNUNET_DHT_connect (sched,
4563 if ( (GNUNET_OK != GNUNET_FS_indexing_init (sched, cfg, dsh)) ||
4564 (GNUNET_OK != main_init (sched, server, cfg)) )
4566 GNUNET_SCHEDULER_shutdown (sched);
4567 GNUNET_DATASTORE_disconnect (dsh, GNUNET_NO);
4569 GNUNET_DHT_disconnect (dht_handle);
4571 GNUNET_BLOCK_context_destroy (block_ctx);
4573 GNUNET_CONFIGURATION_destroy (block_cfg);
4575 GNUNET_LOAD_value_free (datastore_get_load);
4576 datastore_get_load = NULL;
4577 GNUNET_LOAD_value_free (datastore_put_load);
4578 datastore_put_load = NULL;
4585 * The main function for the fs service.
4587 * @param argc number of arguments from the command line
4588 * @param argv command line arguments
4589 * @return 0 ok, 1 on error
4592 main (int argc, char *const *argv)
4594 return (GNUNET_OK ==
4595 GNUNET_SERVICE_run (argc,
4598 GNUNET_SERVICE_OPTION_NONE,
4599 &run, NULL)) ? 0 : 1;
4602 /* end of gnunet-service-fs.c */