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 #define SUPPORT_DELAYS GNUNET_NO
54 * Size for the hash map for DHT requests from the FS
55 * service. Should be about the number of concurrent
56 * DHT requests we plan to make.
58 #define FS_DHT_HT_SIZE 1024
61 * How often do we flush trust values to disk?
63 #define TRUST_FLUSH_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
66 * How often do we at most PUT content into the DHT?
68 #define MAX_DHT_PUT_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
71 * Inverse of the probability that we will submit the same query
72 * to the same peer again. If the same peer already got the query
73 * repeatedly recently, the probability is multiplied by the inverse
74 * of this number each time. Note that we only try about every TTL_DECREMENT/2
75 * plus MAX_CORK_DELAY (so roughly every 3.5s).
77 #define RETRY_PROBABILITY_INV 3
80 * What is the maximum delay for a P2P FS message (in our interaction
81 * with core)? FS-internal delays are another story. The value is
82 * chosen based on the 32k block size. Given that peers typcially
83 * have at least 1 kb/s bandwidth, 45s waits give us a chance to
84 * transmit one message even to the lowest-bandwidth peers.
86 #define MAX_TRANSMIT_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 45)
89 * Maximum number of requests (from other peers) that we're
90 * willing to have pending at any given point in time.
92 static unsigned long long max_pending_requests = (32 * 1024);
96 * Information we keep for each pending reply. The
97 * actual message follows at the end of this struct.
99 struct PendingMessage;
102 * Function called upon completion of a transmission.
105 * @param pid ID of receiving peer, 0 on transmission error
107 typedef void (*TransmissionContinuation)(void * cls,
108 GNUNET_PEER_Id tpid);
112 * Information we keep for each pending message (GET/PUT). The
113 * actual message follows at the end of this struct.
115 struct PendingMessage
118 * This is a doubly-linked list of messages to the same peer.
120 struct PendingMessage *next;
123 * This is a doubly-linked list of messages to the same peer.
125 struct PendingMessage *prev;
128 * Entry in pending message list for this pending message.
130 struct PendingMessageList *pml;
133 * Function to call immediately once we have transmitted this
136 TransmissionContinuation cont;
144 * Do not transmit this pending message until this deadline.
146 struct GNUNET_TIME_Absolute delay_until;
149 * Size of the reply; actual reply message follows
150 * at the end of this struct.
155 * How important is this message for us?
163 * Information about a peer that we are connected to.
164 * We track data that is useful for determining which
165 * peers should receive our requests. We also keep
166 * a list of messages to transmit to this peer.
172 * List of the last clients for which this peer successfully
175 struct GNUNET_SERVER_Client *last_client_replies[CS2P_SUCCESS_LIST_SIZE];
178 * List of the last PIDs for which
179 * this peer successfully answered a query;
180 * We use 0 to indicate no successful reply.
182 GNUNET_PEER_Id last_p2p_replies[P2P_SUCCESS_LIST_SIZE];
185 * Average delay between sending the peer a request and
186 * getting a reply (only calculated over the requests for
187 * which we actually got a reply). Calculated
188 * as a moving average: new_delay = ((n-1)*last_delay+curr_delay) / n
190 * FIXME: actually, this is currently the delay between us originally
191 * receiving (not forwarding!) a request and us receiving a reply from
192 * this peer (regardless of when we transmitted this request to this peer!)
194 struct GNUNET_TIME_Relative avg_delay;
197 * Point in time until which this peer does not want us to migrate content
200 struct GNUNET_TIME_Absolute migration_blocked;
203 * Time until when we blocked this peer from migrating
206 struct GNUNET_TIME_Absolute last_migration_block;
209 * Transmission times for the last MAX_QUEUE_PER_PEER
210 * requests for this peer. Used as a ring buffer, current
211 * offset is stored in 'last_request_times_off'. If the
212 * oldest entry is more recent than the 'avg_delay', we should
213 * not send any more requests right now.
215 struct GNUNET_TIME_Absolute last_request_times[MAX_QUEUE_PER_PEER];
218 * Handle for an active request for transmission to this
221 struct GNUNET_CORE_TransmitHandle *cth;
224 * Messages (replies, queries, content migration) we would like to
225 * send to this peer in the near future. Sorted by priority, head.
227 struct PendingMessage *pending_messages_head;
230 * Messages (replies, queries, content migration) we would like to
231 * send to this peer in the near future. Sorted by priority, tail.
233 struct PendingMessage *pending_messages_tail;
236 * How long does it typically take for us to transmit a message
237 * to this peer? (delay between the request being issued and
238 * the callback being invoked).
240 struct GNUNET_LOAD_Value *transmission_delay;
243 * Time when the last transmission request was issued.
245 struct GNUNET_TIME_Absolute last_transmission_request_start;
248 * ID of delay task for scheduling transmission.
250 GNUNET_SCHEDULER_TaskIdentifier delayed_transmission_request_task;
253 * Average priority of successful replies. Calculated
254 * as a moving average: new_avg = ((n-1)*last_avg+curr_prio) / n
259 * Increase in traffic preference still to be submitted
260 * to the core service for this peer.
262 uint64_t inc_preference;
265 * Trust rating for this peer
270 * Trust rating for this peer on disk.
275 * The peer's identity.
280 * Size of the linked list of 'pending_messages'.
282 unsigned int pending_requests;
285 * Which offset in "last_p2p_replies" will be updated next?
286 * (we go round-robin).
288 unsigned int last_p2p_replies_woff;
291 * Which offset in "last_client_replies" will be updated next?
292 * (we go round-robin).
294 unsigned int last_client_replies_woff;
297 * Current offset into 'last_request_times' ring buffer.
299 unsigned int last_request_times_off;
305 * Information we keep for each pending request. We should try to
306 * keep this struct as small as possible since its memory consumption
307 * is key to how many requests we can have pending at once.
309 struct PendingRequest;
313 * Doubly-linked list of requests we are performing
314 * on behalf of the same client.
316 struct ClientRequestList
320 * This is a doubly-linked list.
322 struct ClientRequestList *next;
325 * This is a doubly-linked list.
327 struct ClientRequestList *prev;
330 * Request this entry represents.
332 struct PendingRequest *req;
335 * Client list this request belongs to.
337 struct ClientList *client_list;
343 * Replies to be transmitted to the client. The actual
344 * response message is allocated after this struct.
346 struct ClientResponseMessage
349 * This is a doubly-linked list.
351 struct ClientResponseMessage *next;
354 * This is a doubly-linked list.
356 struct ClientResponseMessage *prev;
359 * Client list entry this response belongs to.
361 struct ClientList *client_list;
364 * Number of bytes in the response.
371 * Linked list of clients we are performing requests
377 * This is a linked list.
379 struct ClientList *next;
382 * ID of a client making a request, NULL if this entry is for a
385 struct GNUNET_SERVER_Client *client;
388 * Head of list of requests performed on behalf
389 * of this client right now.
391 struct ClientRequestList *rl_head;
394 * Tail of list of requests performed on behalf
395 * of this client right now.
397 struct ClientRequestList *rl_tail;
400 * Head of linked list of responses.
402 struct ClientResponseMessage *res_head;
405 * Tail of linked list of responses.
407 struct ClientResponseMessage *res_tail;
410 * Context for sending replies.
412 struct GNUNET_CONNECTION_TransmitHandle *th;
418 * Information about a peer that we have forwarded this
419 * request to already.
421 struct UsedTargetEntry
424 * What was the last time we have transmitted this request to this
427 struct GNUNET_TIME_Absolute last_request_time;
430 * How often have we transmitted this request to this peer?
432 unsigned int num_requests;
435 * PID of the target peer.
446 * Doubly-linked list of messages we are performing
447 * due to a pending request.
449 struct PendingMessageList
453 * This is a doubly-linked list of messages on behalf of the same request.
455 struct PendingMessageList *next;
458 * This is a doubly-linked list of messages on behalf of the same request.
460 struct PendingMessageList *prev;
463 * Message this entry represents.
465 struct PendingMessage *pm;
468 * Request this entry belongs to.
470 struct PendingRequest *req;
473 * Peer this message is targeted for.
475 struct ConnectedPeer *target;
481 * Information we keep for each pending request. We should try to
482 * keep this struct as small as possible since its memory consumption
483 * is key to how many requests we can have pending at once.
485 struct PendingRequest
489 * If this request was made by a client, this is our entry in the
490 * client request list; otherwise NULL.
492 struct ClientRequestList *client_request_list;
495 * Entry of peer responsible for this entry (if this request
496 * was made by a peer).
498 struct ConnectedPeer *cp;
501 * If this is a namespace query, pointer to the hash of the public
502 * key of the namespace; otherwise NULL. Pointer will be to the
503 * end of this struct (so no need to free it).
505 const GNUNET_HashCode *namespace;
508 * Bloomfilter we use to filter out replies that we don't care about
509 * (anymore). NULL as long as we are interested in all replies.
511 struct GNUNET_CONTAINER_BloomFilter *bf;
514 * Context of our GNUNET_CORE_peer_change_preference call.
516 struct GNUNET_CORE_InformationRequestContext *irc;
519 * Reference to DHT get operation for this request (or NULL).
521 struct GNUNET_DHT_GetHandle *dht_get;
524 * Hash code of all replies that we have seen so far (only valid
525 * if client is not NULL since we only track replies like this for
528 GNUNET_HashCode *replies_seen;
531 * Node in the heap representing this entry; NULL
532 * if we have no heap node.
534 struct GNUNET_CONTAINER_HeapNode *hnode;
537 * Head of list of messages being performed on behalf of this
540 struct PendingMessageList *pending_head;
543 * Tail of list of messages being performed on behalf of this
546 struct PendingMessageList *pending_tail;
549 * When did we first see this request (form this peer), or, if our
550 * client is initiating, when did we last initiate a search?
552 struct GNUNET_TIME_Absolute start_time;
555 * The query that this request is for.
557 GNUNET_HashCode query;
560 * The task responsible for transmitting queries
563 GNUNET_SCHEDULER_TaskIdentifier task;
566 * (Interned) Peer identifier that identifies a preferred target
569 GNUNET_PEER_Id target_pid;
572 * (Interned) Peer identifiers of peers that have already
573 * received our query for this content.
575 struct UsedTargetEntry *used_targets;
578 * Our entry in the queue (non-NULL while we wait for our
579 * turn to interact with the local database).
581 struct GNUNET_DATASTORE_QueueEntry *qe;
584 * Size of the 'bf' (in bytes).
589 * Desired anonymity level; only valid for requests from a local client.
591 uint32_t anonymity_level;
594 * How many entries in "used_targets" are actually valid?
596 unsigned int used_targets_off;
599 * How long is the "used_targets" array?
601 unsigned int used_targets_size;
604 * Number of results found for this request.
606 unsigned int results_found;
609 * How many entries in "replies_seen" are actually valid?
611 unsigned int replies_seen_off;
614 * How long is the "replies_seen" array?
616 unsigned int replies_seen_size;
619 * Priority with which this request was made. If one of our clients
620 * made the request, then this is the current priority that we are
621 * using when initiating the request. This value is used when
622 * we decide to reward other peers with trust for providing a reply.
627 * Priority points left for us to spend when forwarding this request
630 uint32_t remaining_priority;
633 * Number to mingle hashes for bloom-filter tests with.
638 * TTL with which we saw this request (or, if we initiated, TTL that
639 * we used for the request).
644 * Type of the content that this request is for.
646 enum GNUNET_BLOCK_Type type;
649 * Remove this request after transmission of the current response.
654 * GNUNET_YES if we should not forward this request to other peers.
659 * GNUNET_YES if we should not forward this request to other peers.
667 * Block that is ready for migration to other peers. Actual data is at the end of the block.
669 struct MigrationReadyBlock
673 * This is a doubly-linked list.
675 struct MigrationReadyBlock *next;
678 * This is a doubly-linked list.
680 struct MigrationReadyBlock *prev;
683 * Query for the block.
685 GNUNET_HashCode query;
688 * When does this block expire?
690 struct GNUNET_TIME_Absolute expiration;
693 * Peers we would consider forwarding this
694 * block to. Zero for empty entries.
696 GNUNET_PEER_Id target_list[MIGRATION_LIST_SIZE];
704 * Number of targets already used.
706 unsigned int used_targets;
711 enum GNUNET_BLOCK_Type type;
716 * Our connection to the datastore.
718 static struct GNUNET_DATASTORE_Handle *dsh;
723 static struct GNUNET_BLOCK_Context *block_ctx;
726 * Our block configuration.
728 static struct GNUNET_CONFIGURATION_Handle *block_cfg;
733 static struct GNUNET_SCHEDULER_Handle *sched;
738 static const struct GNUNET_CONFIGURATION_Handle *cfg;
741 * Map of peer identifiers to "struct ConnectedPeer" (for that peer).
743 static struct GNUNET_CONTAINER_MultiHashMap *connected_peers;
746 * Map of peer identifiers to "struct PendingRequest" (for that peer).
748 static struct GNUNET_CONTAINER_MultiHashMap *peer_request_map;
751 * Map of query identifiers to "struct PendingRequest" (for that query).
753 static struct GNUNET_CONTAINER_MultiHashMap *query_request_map;
756 * Heap with the request that will expire next at the top. Contains
757 * pointers of type "struct PendingRequest*"; these will *also* be
758 * aliased from the "requests_by_peer" data structures and the
759 * "requests_by_query" table. Note that requests from our clients
760 * don't expire and are thus NOT in the "requests_by_expiration"
761 * (or the "requests_by_peer" tables).
763 static struct GNUNET_CONTAINER_Heap *requests_by_expiration_heap;
766 * Handle for reporting statistics.
768 static struct GNUNET_STATISTICS_Handle *stats;
771 * Linked list of clients we are currently processing requests for.
773 static struct ClientList *client_list;
776 * Pointer to handle to the core service (points to NULL until we've
779 static struct GNUNET_CORE_Handle *core;
782 * Head of linked list of blocks that can be migrated.
784 static struct MigrationReadyBlock *mig_head;
787 * Tail of linked list of blocks that can be migrated.
789 static struct MigrationReadyBlock *mig_tail;
792 * Request to datastore for migration (or NULL).
794 static struct GNUNET_DATASTORE_QueueEntry *mig_qe;
797 * Request to datastore for DHT PUTs (or NULL).
799 static struct GNUNET_DATASTORE_QueueEntry *dht_qe;
802 * Type we will request for the next DHT PUT round from the datastore.
804 static enum GNUNET_BLOCK_Type dht_put_type = GNUNET_BLOCK_TYPE_FS_KBLOCK;
807 * Where do we store trust information?
809 static char *trustDirectory;
812 * ID of task that collects blocks for migration.
814 static GNUNET_SCHEDULER_TaskIdentifier mig_task;
817 * ID of task that collects blocks for DHT PUTs.
819 static GNUNET_SCHEDULER_TaskIdentifier dht_task;
822 * What is the maximum frequency at which we are allowed to
823 * poll the datastore for migration content?
825 static struct GNUNET_TIME_Relative min_migration_delay;
828 * Handle for DHT operations.
830 static struct GNUNET_DHT_Handle *dht_handle;
833 * Size of the doubly-linked list of migration blocks.
835 static unsigned int mig_size;
838 * Are we allowed to migrate content to this peer.
840 static int active_migration;
843 * How many entires with zero anonymity do we currently estimate
844 * to have in the database?
846 static unsigned int zero_anonymity_count_estimate;
849 * Typical priorities we're seeing from other peers right now. Since
850 * most priorities will be zero, this value is the weighted average of
851 * non-zero priorities seen "recently". In order to ensure that new
852 * values do not dramatically change the ratio, values are first
853 * "capped" to a reasonable range (+N of the current value) and then
854 * averaged into the existing value by a ratio of 1:N. Hence
855 * receiving the largest possible priority can still only raise our
856 * "current_priorities" by at most 1.
858 static double current_priorities;
861 * Datastore 'GET' load tracking.
863 static struct GNUNET_LOAD_Value *datastore_get_load;
866 * Datastore 'PUT' load tracking.
868 static struct GNUNET_LOAD_Value *datastore_put_load;
871 * How long do requests typically stay in the routing table?
873 static struct GNUNET_LOAD_Value *rt_entry_lifetime;
876 * We've just now completed a datastore request. Update our
877 * datastore load calculations.
879 * @param start time when the datastore request was issued
882 update_datastore_delays (struct GNUNET_TIME_Absolute start)
884 struct GNUNET_TIME_Relative delay;
886 delay = GNUNET_TIME_absolute_get_duration (start);
887 GNUNET_LOAD_update (datastore_get_load,
893 * Get the filename under which we would store the GNUNET_HELLO_Message
894 * for the given host and protocol.
895 * @return filename of the form DIRECTORY/HOSTID
898 get_trust_filename (const struct GNUNET_PeerIdentity *id)
900 struct GNUNET_CRYPTO_HashAsciiEncoded fil;
903 GNUNET_CRYPTO_hash_to_enc (&id->hashPubKey, &fil);
904 GNUNET_asprintf (&fn, "%s%s%s", trustDirectory, DIR_SEPARATOR_STR, &fil);
911 * Transmit messages by copying it to the target buffer
912 * "buf". "buf" will be NULL and "size" zero if the socket was closed
913 * for writing in the meantime. In that case, do nothing
914 * (the disconnect or shutdown handler will take care of the rest).
915 * If we were able to transmit messages and there are still more
916 * pending, ask core again for further calls to this function.
918 * @param cls closure, pointer to the 'struct ConnectedPeer*'
919 * @param size number of bytes available in buf
920 * @param buf where the callee should write the message
921 * @return number of bytes written to buf
924 transmit_to_peer (void *cls,
925 size_t size, void *buf);
928 /* ******************* clean up functions ************************ */
931 * Delete the given migration block.
933 * @param mb block to delete
936 delete_migration_block (struct MigrationReadyBlock *mb)
938 GNUNET_CONTAINER_DLL_remove (mig_head,
941 GNUNET_PEER_decrement_rcs (mb->target_list,
942 MIGRATION_LIST_SIZE);
949 * Compare the distance of two peers to a key.
952 * @param p1 first peer
953 * @param p2 second peer
954 * @return GNUNET_YES if P1 is closer to key than P2
957 is_closer (const GNUNET_HashCode *key,
958 const struct GNUNET_PeerIdentity *p1,
959 const struct GNUNET_PeerIdentity *p2)
961 return GNUNET_CRYPTO_hash_xorcmp (&p1->hashPubKey,
968 * Consider migrating content to a given peer.
970 * @param cls 'struct MigrationReadyBlock*' to select
971 * targets for (or NULL for none)
972 * @param key ID of the peer
973 * @param value 'struct ConnectedPeer' of the peer
974 * @return GNUNET_YES (always continue iteration)
977 consider_migration (void *cls,
978 const GNUNET_HashCode *key,
981 struct MigrationReadyBlock *mb = cls;
982 struct ConnectedPeer *cp = value;
983 struct MigrationReadyBlock *pos;
984 struct GNUNET_PeerIdentity cppid;
985 struct GNUNET_PeerIdentity otherpid;
986 struct GNUNET_PeerIdentity worstpid;
991 /* consider 'cp' as a migration target for mb */
992 if (GNUNET_TIME_absolute_get_remaining (cp->migration_blocked).value > 0)
993 return GNUNET_YES; /* peer has requested no migration! */
996 GNUNET_PEER_resolve (cp->pid,
998 repl = MIGRATION_LIST_SIZE;
999 for (i=0;i<MIGRATION_LIST_SIZE;i++)
1001 if (mb->target_list[i] == 0)
1003 mb->target_list[i] = cp->pid;
1004 GNUNET_PEER_change_rc (mb->target_list[i], 1);
1005 repl = MIGRATION_LIST_SIZE;
1008 GNUNET_PEER_resolve (mb->target_list[i],
1010 if ( (repl == MIGRATION_LIST_SIZE) &&
1011 is_closer (&mb->query,
1016 worstpid = otherpid;
1018 else if ( (repl != MIGRATION_LIST_SIZE) &&
1019 (is_closer (&mb->query,
1024 worstpid = otherpid;
1027 if (repl != MIGRATION_LIST_SIZE)
1029 GNUNET_PEER_change_rc (mb->target_list[repl], -1);
1030 mb->target_list[repl] = cp->pid;
1031 GNUNET_PEER_change_rc (mb->target_list[repl], 1);
1035 /* consider scheduling transmission to cp for content migration */
1036 if (cp->cth != NULL)
1042 for (i=0;i<MIGRATION_LIST_SIZE;i++)
1044 if (cp->pid == pos->target_list[i])
1049 msize = GNUNET_MIN (msize,
1057 return GNUNET_YES; /* no content available */
1059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1060 "Trying to migrate at least %u bytes to peer `%s'\n",
1064 if (cp->delayed_transmission_request_task != GNUNET_SCHEDULER_NO_TASK)
1066 GNUNET_SCHEDULER_cancel (sched, cp->delayed_transmission_request_task);
1067 cp->delayed_transmission_request_task = GNUNET_SCHEDULER_NO_TASK;
1070 = GNUNET_CORE_notify_transmit_ready (core,
1071 0, GNUNET_TIME_UNIT_FOREVER_REL,
1072 (const struct GNUNET_PeerIdentity*) key,
1073 msize + sizeof (struct PutMessage),
1081 * Task that is run periodically to obtain blocks for content
1085 * @param tc scheduler context (also unused)
1088 gather_migration_blocks (void *cls,
1089 const struct GNUNET_SCHEDULER_TaskContext *tc);
1095 * Task that is run periodically to obtain blocks for DHT PUTs.
1097 * @param cls type of blocks to gather
1098 * @param tc scheduler context (unused)
1101 gather_dht_put_blocks (void *cls,
1102 const struct GNUNET_SCHEDULER_TaskContext *tc);
1106 * If the migration task is not currently running, consider
1107 * (re)scheduling it with the appropriate delay.
1110 consider_migration_gathering ()
1112 struct GNUNET_TIME_Relative delay;
1118 if (mig_task != GNUNET_SCHEDULER_NO_TASK)
1120 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
1122 delay = GNUNET_TIME_relative_divide (delay,
1123 MAX_MIGRATION_QUEUE);
1124 delay = GNUNET_TIME_relative_max (delay,
1125 min_migration_delay);
1126 mig_task = GNUNET_SCHEDULER_add_delayed (sched,
1128 &gather_migration_blocks,
1134 * If the DHT PUT gathering task is not currently running, consider
1135 * (re)scheduling it with the appropriate delay.
1138 consider_dht_put_gathering (void *cls)
1140 struct GNUNET_TIME_Relative delay;
1146 if (dht_task != GNUNET_SCHEDULER_NO_TASK)
1148 if (zero_anonymity_count_estimate > 0)
1150 delay = GNUNET_TIME_relative_divide (GNUNET_DHT_DEFAULT_REPUBLISH_FREQUENCY,
1151 zero_anonymity_count_estimate);
1152 delay = GNUNET_TIME_relative_min (delay,
1157 /* if we have NO zero-anonymity content yet, wait 5 minutes for some to
1158 (hopefully) appear */
1159 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5);
1161 dht_task = GNUNET_SCHEDULER_add_delayed (sched,
1163 &gather_dht_put_blocks,
1169 * Process content offered for migration.
1171 * @param cls closure
1172 * @param key key for the content
1173 * @param size number of bytes in data
1174 * @param data content stored
1175 * @param type type of the content
1176 * @param priority priority of the content
1177 * @param anonymity anonymity-level for the content
1178 * @param expiration expiration time for the content
1179 * @param uid unique identifier for the datum;
1180 * maybe 0 if no unique identifier is available
1183 process_migration_content (void *cls,
1184 const GNUNET_HashCode * key,
1187 enum GNUNET_BLOCK_Type type,
1190 struct GNUNET_TIME_Absolute
1191 expiration, uint64_t uid)
1193 struct MigrationReadyBlock *mb;
1198 if (mig_size < MAX_MIGRATION_QUEUE)
1199 consider_migration_gathering ();
1202 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
1205 GNUNET_FS_handle_on_demand_block (key, size, data,
1206 type, priority, anonymity,
1208 &process_migration_content,
1211 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
1216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1217 "Retrieved block `%s' of type %u for migration\n",
1221 mb = GNUNET_malloc (sizeof (struct MigrationReadyBlock) + size);
1223 mb->expiration = expiration;
1226 memcpy (&mb[1], data, size);
1227 GNUNET_CONTAINER_DLL_insert_after (mig_head,
1232 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
1233 &consider_migration,
1235 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
1240 * Function called upon completion of the DHT PUT operation.
1243 dht_put_continuation (void *cls,
1244 const struct GNUNET_SCHEDULER_TaskContext *tc)
1246 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
1251 * Store content in DHT.
1253 * @param cls closure
1254 * @param key key for the content
1255 * @param size number of bytes in data
1256 * @param data content stored
1257 * @param type type of the content
1258 * @param priority priority of the content
1259 * @param anonymity anonymity-level for the content
1260 * @param expiration expiration time for the content
1261 * @param uid unique identifier for the datum;
1262 * maybe 0 if no unique identifier is available
1265 process_dht_put_content (void *cls,
1266 const GNUNET_HashCode * key,
1269 enum GNUNET_BLOCK_Type type,
1272 struct GNUNET_TIME_Absolute
1273 expiration, uint64_t uid)
1275 static unsigned int counter;
1276 static GNUNET_HashCode last_vhash;
1277 static GNUNET_HashCode vhash;
1282 consider_dht_put_gathering (cls);
1285 /* slightly funky code to estimate the total number of values with zero
1286 anonymity from the maximum observed length of a monotonically increasing
1287 sequence of hashes over the contents */
1288 GNUNET_CRYPTO_hash (data, size, &vhash);
1289 if (GNUNET_CRYPTO_hash_cmp (&vhash, &last_vhash) <= 0)
1291 if (zero_anonymity_count_estimate > 0)
1292 zero_anonymity_count_estimate /= 2;
1298 if (zero_anonymity_count_estimate < (1 << counter))
1299 zero_anonymity_count_estimate = (1 << counter);
1301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1302 "Retrieved block `%s' of type %u for DHT PUT\n",
1306 GNUNET_DHT_put (dht_handle,
1313 GNUNET_TIME_UNIT_FOREVER_REL,
1314 &dht_put_continuation,
1320 * Task that is run periodically to obtain blocks for content
1324 * @param tc scheduler context (also unused)
1327 gather_migration_blocks (void *cls,
1328 const struct GNUNET_SCHEDULER_TaskContext *tc)
1330 mig_task = GNUNET_SCHEDULER_NO_TASK;
1333 mig_qe = GNUNET_DATASTORE_get_random (dsh, 0, UINT_MAX,
1334 GNUNET_TIME_UNIT_FOREVER_REL,
1335 &process_migration_content, NULL);
1336 GNUNET_assert (mig_qe != NULL);
1342 * Task that is run periodically to obtain blocks for DHT PUTs.
1344 * @param cls type of blocks to gather
1345 * @param tc scheduler context (unused)
1348 gather_dht_put_blocks (void *cls,
1349 const struct GNUNET_SCHEDULER_TaskContext *tc)
1351 dht_task = GNUNET_SCHEDULER_NO_TASK;
1354 if (dht_put_type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
1355 dht_put_type = GNUNET_BLOCK_TYPE_FS_KBLOCK;
1356 dht_qe = GNUNET_DATASTORE_get_zero_anonymity (dsh, 0, UINT_MAX,
1357 GNUNET_TIME_UNIT_FOREVER_REL,
1359 &process_dht_put_content, NULL);
1360 GNUNET_assert (dht_qe != NULL);
1366 * We're done with a particular message list entry.
1367 * Free all associated resources.
1369 * @param pml entry to destroy
1372 destroy_pending_message_list_entry (struct PendingMessageList *pml)
1374 GNUNET_CONTAINER_DLL_remove (pml->req->pending_head,
1375 pml->req->pending_tail,
1377 GNUNET_CONTAINER_DLL_remove (pml->target->pending_messages_head,
1378 pml->target->pending_messages_tail,
1380 pml->target->pending_requests--;
1381 GNUNET_free (pml->pm);
1387 * Destroy the given pending message (and call the respective
1390 * @param pm message to destroy
1391 * @param tpid id of peer that the message was delivered to, or 0 for none
1394 destroy_pending_message (struct PendingMessage *pm,
1395 GNUNET_PEER_Id tpid)
1397 struct PendingMessageList *pml = pm->pml;
1398 TransmissionContinuation cont;
1402 cont_cls = pm->cont_cls;
1405 GNUNET_assert (pml->pm == pm);
1406 GNUNET_assert ( (tpid == 0) || (tpid == pml->target->pid) );
1407 destroy_pending_message_list_entry (pml);
1414 cont (cont_cls, tpid);
1419 * We're done processing a particular request.
1420 * Free all associated resources.
1422 * @param pr request to destroy
1425 destroy_pending_request (struct PendingRequest *pr)
1427 struct GNUNET_PeerIdentity pid;
1430 if (pr->hnode != NULL)
1432 GNUNET_CONTAINER_heap_remove_node (requests_by_expiration_heap,
1436 if (NULL == pr->client_request_list)
1438 GNUNET_STATISTICS_update (stats,
1439 gettext_noop ("# P2P searches active"),
1445 GNUNET_STATISTICS_update (stats,
1446 gettext_noop ("# client searches active"),
1451 GNUNET_CONTAINER_multihashmap_remove (query_request_map,
1455 GNUNET_LOAD_update (rt_entry_lifetime,
1456 GNUNET_TIME_absolute_get_duration (pr->start_time).value);
1460 GNUNET_DATASTORE_cancel (pr->qe);
1463 if (pr->dht_get != NULL)
1465 GNUNET_DHT_get_stop (pr->dht_get);
1468 if (pr->client_request_list != NULL)
1470 GNUNET_CONTAINER_DLL_remove (pr->client_request_list->client_list->rl_head,
1471 pr->client_request_list->client_list->rl_tail,
1472 pr->client_request_list);
1473 GNUNET_free (pr->client_request_list);
1474 pr->client_request_list = NULL;
1478 GNUNET_PEER_resolve (pr->cp->pid,
1480 (void) GNUNET_CONTAINER_multihashmap_remove (peer_request_map,
1487 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
1490 if (pr->irc != NULL)
1492 GNUNET_CORE_peer_change_preference_cancel (pr->irc);
1495 if (pr->replies_seen != NULL)
1497 GNUNET_free (pr->replies_seen);
1498 pr->replies_seen = NULL;
1500 if (pr->task != GNUNET_SCHEDULER_NO_TASK)
1502 GNUNET_SCHEDULER_cancel (sched,
1504 pr->task = GNUNET_SCHEDULER_NO_TASK;
1506 while (NULL != pr->pending_head)
1507 destroy_pending_message_list_entry (pr->pending_head);
1508 GNUNET_PEER_change_rc (pr->target_pid, -1);
1509 if (pr->used_targets != NULL)
1511 for (i=0;i<pr->used_targets_off;i++)
1512 GNUNET_PEER_change_rc (pr->used_targets[i].pid, -1);
1513 GNUNET_free (pr->used_targets);
1514 pr->used_targets_off = 0;
1515 pr->used_targets_size = 0;
1516 pr->used_targets = NULL;
1523 * Method called whenever a given peer connects.
1525 * @param cls closure, not used
1526 * @param peer peer identity this notification is about
1527 * @param latency reported latency of the connection with 'other'
1528 * @param distance reported distance (DV) to 'other'
1531 peer_connect_handler (void *cls,
1533 GNUNET_PeerIdentity * peer,
1534 struct GNUNET_TIME_Relative latency,
1537 struct ConnectedPeer *cp;
1538 struct MigrationReadyBlock *pos;
1542 cp = GNUNET_malloc (sizeof (struct ConnectedPeer));
1543 cp->transmission_delay = GNUNET_LOAD_value_init ();
1544 cp->pid = GNUNET_PEER_intern (peer);
1546 fn = get_trust_filename (peer);
1547 if ((GNUNET_DISK_file_test (fn) == GNUNET_YES) &&
1548 (sizeof (trust) == GNUNET_DISK_fn_read (fn, &trust, sizeof (trust))))
1549 cp->disk_trust = cp->trust = ntohl (trust);
1552 GNUNET_break (GNUNET_OK ==
1553 GNUNET_CONTAINER_multihashmap_put (connected_peers,
1556 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1561 (void) consider_migration (pos, &peer->hashPubKey, cp);
1568 * Increase the host credit by a value.
1570 * @param host which peer to change the trust value on
1571 * @param value is the int value by which the
1572 * host credit is to be increased or decreased
1573 * @returns the actual change in trust (positive or negative)
1576 change_host_trust (struct ConnectedPeer *host, int value)
1578 unsigned int old_trust;
1582 GNUNET_assert (host != NULL);
1583 old_trust = host->trust;
1586 if (host->trust + value < host->trust)
1588 value = UINT32_MAX - host->trust;
1589 host->trust = UINT32_MAX;
1592 host->trust += value;
1596 if (host->trust < -value)
1598 value = -host->trust;
1602 host->trust += value;
1609 * Write host-trust information to a file - flush the buffer entry!
1612 flush_trust (void *cls,
1613 const GNUNET_HashCode *key,
1616 struct ConnectedPeer *host = value;
1619 struct GNUNET_PeerIdentity pid;
1621 if (host->trust == host->disk_trust)
1622 return GNUNET_OK; /* unchanged */
1623 GNUNET_PEER_resolve (host->pid,
1625 fn = get_trust_filename (&pid);
1626 if (host->trust == 0)
1628 if ((0 != UNLINK (fn)) && (errno != ENOENT))
1629 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
1630 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
1634 trust = htonl (host->trust);
1635 if (sizeof(uint32_t) == GNUNET_DISK_fn_write (fn, &trust,
1637 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE
1638 | GNUNET_DISK_PERM_GROUP_READ | GNUNET_DISK_PERM_OTHER_READ))
1639 host->disk_trust = host->trust;
1646 * Call this method periodically to scan data/hosts for new hosts.
1649 cron_flush_trust (void *cls,
1650 const struct GNUNET_SCHEDULER_TaskContext *tc)
1653 if (NULL == connected_peers)
1655 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
1660 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1662 GNUNET_SCHEDULER_add_delayed (tc->sched,
1663 TRUST_FLUSH_FREQ, &cron_flush_trust, NULL);
1668 * Free (each) request made by the peer.
1670 * @param cls closure, points to peer that the request belongs to
1671 * @param key current key code
1672 * @param value value in the hash map
1673 * @return GNUNET_YES (we should continue to iterate)
1676 destroy_request (void *cls,
1677 const GNUNET_HashCode * key,
1680 const struct GNUNET_PeerIdentity * peer = cls;
1681 struct PendingRequest *pr = value;
1683 GNUNET_break (GNUNET_YES ==
1684 GNUNET_CONTAINER_multihashmap_remove (peer_request_map,
1687 destroy_pending_request (pr);
1693 * Method called whenever a peer disconnects.
1695 * @param cls closure, not used
1696 * @param peer peer identity this notification is about
1699 peer_disconnect_handler (void *cls,
1701 GNUNET_PeerIdentity * peer)
1703 struct ConnectedPeer *cp;
1704 struct PendingMessage *pm;
1706 struct MigrationReadyBlock *pos;
1707 struct MigrationReadyBlock *next;
1709 GNUNET_CONTAINER_multihashmap_get_multiple (peer_request_map,
1713 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
1717 for (i=0;i<CS2P_SUCCESS_LIST_SIZE;i++)
1719 if (NULL != cp->last_client_replies[i])
1721 GNUNET_SERVER_client_drop (cp->last_client_replies[i]);
1722 cp->last_client_replies[i] = NULL;
1725 GNUNET_break (GNUNET_YES ==
1726 GNUNET_CONTAINER_multihashmap_remove (connected_peers,
1729 /* remove this peer from migration considerations; schedule
1732 while (NULL != (pos = next))
1735 for (i=0;i<MIGRATION_LIST_SIZE;i++)
1737 if (pos->target_list[i] == cp->pid)
1739 GNUNET_PEER_change_rc (pos->target_list[i], -1);
1740 pos->target_list[i] = 0;
1743 if (pos->used_targets >= GNUNET_CONTAINER_multihashmap_size (connected_peers))
1745 delete_migration_block (pos);
1746 consider_migration_gathering ();
1749 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
1750 &consider_migration,
1753 GNUNET_PEER_change_rc (cp->pid, -1);
1754 GNUNET_PEER_decrement_rcs (cp->last_p2p_replies, P2P_SUCCESS_LIST_SIZE);
1755 if (NULL != cp->cth)
1757 GNUNET_CORE_notify_transmit_ready_cancel (cp->cth);
1760 if (cp->delayed_transmission_request_task != GNUNET_SCHEDULER_NO_TASK)
1762 GNUNET_SCHEDULER_cancel (sched, cp->delayed_transmission_request_task);
1763 cp->delayed_transmission_request_task = GNUNET_SCHEDULER_NO_TASK;
1765 while (NULL != (pm = cp->pending_messages_head))
1766 destroy_pending_message (pm, 0 /* delivery failed */);
1767 GNUNET_LOAD_value_free (cp->transmission_delay);
1768 GNUNET_break (0 == cp->pending_requests);
1774 * Iterator over hash map entries that removes all occurences
1775 * of the given 'client' from the 'last_client_replies' of the
1776 * given connected peer.
1778 * @param cls closure, the 'struct GNUNET_SERVER_Client*' to remove
1779 * @param key current key code (unused)
1780 * @param value value in the hash map (the 'struct ConnectedPeer*' to change)
1781 * @return GNUNET_YES (we should continue to iterate)
1784 remove_client_from_last_client_replies (void *cls,
1785 const GNUNET_HashCode * key,
1788 struct GNUNET_SERVER_Client *client = cls;
1789 struct ConnectedPeer *cp = value;
1792 for (i=0;i<CS2P_SUCCESS_LIST_SIZE;i++)
1794 if (cp->last_client_replies[i] == client)
1796 GNUNET_SERVER_client_drop (cp->last_client_replies[i]);
1797 cp->last_client_replies[i] = NULL;
1805 * A client disconnected. Remove all of its pending queries.
1807 * @param cls closure, NULL
1808 * @param client identification of the client
1811 handle_client_disconnect (void *cls,
1812 struct GNUNET_SERVER_Client
1815 struct ClientList *pos;
1816 struct ClientList *prev;
1817 struct ClientRequestList *rcl;
1818 struct ClientResponseMessage *creply;
1824 while ( (NULL != pos) &&
1825 (pos->client != client) )
1831 return; /* no requests pending for this client */
1832 while (NULL != (rcl = pos->rl_head))
1834 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1835 "Destroying pending request `%s' on disconnect\n",
1836 GNUNET_h2s (&rcl->req->query));
1837 destroy_pending_request (rcl->req);
1840 client_list = pos->next;
1842 prev->next = pos->next;
1843 if (pos->th != NULL)
1845 GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th);
1848 while (NULL != (creply = pos->res_head))
1850 GNUNET_CONTAINER_DLL_remove (pos->res_head,
1853 GNUNET_free (creply);
1855 GNUNET_SERVER_client_drop (pos->client);
1857 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
1858 &remove_client_from_last_client_replies,
1864 * Iterator to free peer entries.
1866 * @param cls closure, unused
1867 * @param key current key code
1868 * @param value value in the hash map (peer entry)
1869 * @return GNUNET_YES (we should continue to iterate)
1872 clean_peer (void *cls,
1873 const GNUNET_HashCode * key,
1876 peer_disconnect_handler (NULL, (const struct GNUNET_PeerIdentity*) key);
1882 * Task run during shutdown.
1888 shutdown_task (void *cls,
1889 const struct GNUNET_SCHEDULER_TaskContext *tc)
1893 GNUNET_DATASTORE_cancel (mig_qe);
1898 GNUNET_DATASTORE_cancel (dht_qe);
1901 if (GNUNET_SCHEDULER_NO_TASK != mig_task)
1903 GNUNET_SCHEDULER_cancel (sched, mig_task);
1904 mig_task = GNUNET_SCHEDULER_NO_TASK;
1906 if (GNUNET_SCHEDULER_NO_TASK != dht_task)
1908 GNUNET_SCHEDULER_cancel (sched, dht_task);
1909 dht_task = GNUNET_SCHEDULER_NO_TASK;
1911 while (client_list != NULL)
1912 handle_client_disconnect (NULL,
1913 client_list->client);
1914 cron_flush_trust (NULL, NULL);
1915 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
1918 GNUNET_break (0 == GNUNET_CONTAINER_heap_get_size (requests_by_expiration_heap));
1919 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
1920 requests_by_expiration_heap = 0;
1921 GNUNET_CONTAINER_multihashmap_destroy (connected_peers);
1922 connected_peers = NULL;
1923 GNUNET_break (0 == GNUNET_CONTAINER_multihashmap_size (query_request_map));
1924 GNUNET_CONTAINER_multihashmap_destroy (query_request_map);
1925 query_request_map = NULL;
1926 GNUNET_LOAD_value_free (rt_entry_lifetime);
1927 rt_entry_lifetime = NULL;
1928 GNUNET_break (0 == GNUNET_CONTAINER_multihashmap_size (peer_request_map));
1929 GNUNET_CONTAINER_multihashmap_destroy (peer_request_map);
1930 peer_request_map = NULL;
1931 GNUNET_assert (NULL != core);
1932 GNUNET_CORE_disconnect (core);
1936 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1941 GNUNET_DATASTORE_disconnect (dsh,
1945 while (mig_head != NULL)
1946 delete_migration_block (mig_head);
1947 GNUNET_assert (0 == mig_size);
1948 GNUNET_DHT_disconnect (dht_handle);
1950 GNUNET_LOAD_value_free (datastore_get_load);
1951 datastore_get_load = NULL;
1952 GNUNET_LOAD_value_free (datastore_put_load);
1953 datastore_put_load = NULL;
1954 GNUNET_BLOCK_context_destroy (block_ctx);
1956 GNUNET_CONFIGURATION_destroy (block_cfg);
1960 GNUNET_free_non_null (trustDirectory);
1961 trustDirectory = NULL;
1965 /* ******************* Utility functions ******************** */
1969 * We've had to delay a request for transmission to core, but now
1970 * we should be ready. Run it.
1972 * @param cls the 'struct ConnectedPeer' for which a request was delayed
1973 * @param tc task context (unused)
1976 delayed_transmission_request (void *cls,
1977 const struct GNUNET_SCHEDULER_TaskContext *tc)
1979 struct ConnectedPeer *cp = cls;
1980 struct GNUNET_PeerIdentity pid;
1981 struct PendingMessage *pm;
1983 pm = cp->pending_messages_head;
1984 cp->delayed_transmission_request_task = GNUNET_SCHEDULER_NO_TASK;
1985 GNUNET_assert (cp->cth == NULL);
1988 GNUNET_PEER_resolve (cp->pid,
1990 cp->last_transmission_request_start = GNUNET_TIME_absolute_get ();
1991 cp->cth = GNUNET_CORE_notify_transmit_ready (core,
1993 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
2002 * Transmit messages by copying it to the target buffer
2003 * "buf". "buf" will be NULL and "size" zero if the socket was closed
2004 * for writing in the meantime. In that case, do nothing
2005 * (the disconnect or shutdown handler will take care of the rest).
2006 * If we were able to transmit messages and there are still more
2007 * pending, ask core again for further calls to this function.
2009 * @param cls closure, pointer to the 'struct ConnectedPeer*'
2010 * @param size number of bytes available in buf
2011 * @param buf where the callee should write the message
2012 * @return number of bytes written to buf
2015 transmit_to_peer (void *cls,
2016 size_t size, void *buf)
2018 struct ConnectedPeer *cp = cls;
2020 struct PendingMessage *pm;
2021 struct PendingMessage *next_pm;
2022 struct GNUNET_TIME_Absolute now;
2023 struct GNUNET_TIME_Relative min_delay;
2024 struct MigrationReadyBlock *mb;
2025 struct MigrationReadyBlock *next;
2026 struct PutMessage migm;
2029 struct GNUNET_PeerIdentity pid;
2035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2036 "Dropping message, core too busy.\n");
2038 GNUNET_LOAD_update (cp->transmission_delay,
2042 GNUNET_LOAD_update (cp->transmission_delay,
2043 GNUNET_TIME_absolute_get_duration (cp->last_transmission_request_start).value);
2044 now = GNUNET_TIME_absolute_get ();
2046 min_delay = GNUNET_TIME_UNIT_FOREVER_REL;
2047 next_pm = cp->pending_messages_head;
2048 while ( (NULL != (pm = next_pm) ) &&
2049 (pm->msize <= size) )
2052 if (pm->delay_until.value > now.value)
2054 min_delay = GNUNET_TIME_relative_min (min_delay,
2055 GNUNET_TIME_absolute_get_remaining (pm->delay_until));
2058 memcpy (&cbuf[msize], &pm[1], pm->msize);
2061 if (NULL == pm->pml)
2063 GNUNET_CONTAINER_DLL_remove (cp->pending_messages_head,
2064 cp->pending_messages_tail,
2066 cp->pending_requests--;
2068 destroy_pending_message (pm, cp->pid);
2071 min_delay = GNUNET_TIME_UNIT_ZERO;
2072 if (NULL != cp->pending_messages_head)
2074 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == cp->delayed_transmission_request_task);
2075 cp->delayed_transmission_request_task
2076 = GNUNET_SCHEDULER_add_delayed (sched,
2078 &delayed_transmission_request,
2083 GNUNET_PEER_resolve (cp->pid,
2086 while (NULL != (mb = next))
2089 for (i=0;i<MIGRATION_LIST_SIZE;i++)
2091 if ( (cp->pid == mb->target_list[i]) &&
2092 (mb->size + sizeof (migm) <= size) )
2094 GNUNET_PEER_change_rc (mb->target_list[i], -1);
2095 mb->target_list[i] = 0;
2097 memset (&migm, 0, sizeof (migm));
2098 migm.header.size = htons (sizeof (migm) + mb->size);
2099 migm.header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
2100 migm.type = htonl (mb->type);
2101 migm.expiration = GNUNET_TIME_absolute_hton (mb->expiration);
2102 memcpy (&cbuf[msize], &migm, sizeof (migm));
2103 msize += sizeof (migm);
2104 size -= sizeof (migm);
2105 memcpy (&cbuf[msize], &mb[1], mb->size);
2109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2110 "Pushing migration block `%s' (%u bytes) to `%s'\n",
2111 GNUNET_h2s (&mb->query),
2112 (unsigned int) mb->size,
2120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2121 "Migration block `%s' (%u bytes) is not on migration list for peer `%s'\n",
2122 GNUNET_h2s (&mb->query),
2123 (unsigned int) mb->size,
2128 if ( (mb->used_targets >= MIGRATION_TARGET_COUNT) ||
2129 (mb->used_targets >= GNUNET_CONTAINER_multihashmap_size (connected_peers)) )
2131 delete_migration_block (mb);
2132 consider_migration_gathering ();
2135 consider_migration (NULL,
2140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2141 "Transmitting %u bytes to peer with PID %u\n",
2142 (unsigned int) msize,
2143 (unsigned int) cp->pid);
2150 * Add a message to the set of pending messages for the given peer.
2152 * @param cp peer to send message to
2153 * @param pm message to queue
2154 * @param pr request on which behalf this message is being queued
2157 add_to_pending_messages_for_peer (struct ConnectedPeer *cp,
2158 struct PendingMessage *pm,
2159 struct PendingRequest *pr)
2161 struct PendingMessage *pos;
2162 struct PendingMessageList *pml;
2163 struct GNUNET_PeerIdentity pid;
2165 GNUNET_assert (pm->next == NULL);
2166 GNUNET_assert (pm->pml == NULL);
2169 pml = GNUNET_malloc (sizeof (struct PendingMessageList));
2174 GNUNET_CONTAINER_DLL_insert (pr->pending_head,
2178 pos = cp->pending_messages_head;
2179 while ( (pos != NULL) &&
2180 (pm->priority < pos->priority) )
2182 GNUNET_CONTAINER_DLL_insert_after (cp->pending_messages_head,
2183 cp->pending_messages_tail,
2186 cp->pending_requests++;
2187 if (cp->pending_requests > MAX_QUEUE_PER_PEER)
2189 GNUNET_STATISTICS_update (stats,
2190 gettext_noop ("# P2P searches discarded (queue length bound)"),
2193 destroy_pending_message (cp->pending_messages_tail, 0);
2195 GNUNET_PEER_resolve (cp->pid, &pid);
2196 if (NULL != cp->cth)
2198 GNUNET_CORE_notify_transmit_ready_cancel (cp->cth);
2201 if (cp->delayed_transmission_request_task != GNUNET_SCHEDULER_NO_TASK)
2203 GNUNET_SCHEDULER_cancel (sched, cp->delayed_transmission_request_task);
2204 cp->delayed_transmission_request_task = GNUNET_SCHEDULER_NO_TASK;
2206 /* need to schedule transmission */
2207 cp->last_transmission_request_start = GNUNET_TIME_absolute_get ();
2208 cp->cth = GNUNET_CORE_notify_transmit_ready (core,
2209 cp->pending_messages_head->priority,
2212 cp->pending_messages_head->msize,
2215 if (cp->cth == NULL)
2218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2219 "Failed to schedule transmission with core!\n");
2221 GNUNET_STATISTICS_update (stats,
2222 gettext_noop ("# CORE transmission failures"),
2230 * Test if the DATABASE (GET) load on this peer is too high
2231 * to even consider processing the query at
2234 * @return GNUNET_YES if the load is too high to do anything (load high)
2235 * GNUNET_NO to process normally (load normal)
2236 * GNUNET_SYSERR to process for free (load low)
2239 test_get_load_too_high (uint32_t priority)
2243 ld = GNUNET_LOAD_get_load (datastore_get_load);
2246 GNUNET_STATISTICS_update (stats,
2247 gettext_noop ("# requests done for free (low load)"),
2250 return GNUNET_SYSERR;
2254 GNUNET_STATISTICS_update (stats,
2255 gettext_noop ("# requests done for a price (normal load)"),
2260 GNUNET_STATISTICS_update (stats,
2261 gettext_noop ("# requests dropped due to high load"),
2271 * Test if the DATABASE (PUT) load on this peer is too high
2272 * to even consider processing the query at
2275 * @return GNUNET_YES if the load is too high to do anything (load high)
2276 * GNUNET_NO to process normally (load normal or low)
2279 test_put_load_too_high (uint32_t priority)
2283 if (GNUNET_LOAD_get_average (datastore_put_load) < 50)
2284 return GNUNET_NO; /* very fast */
2285 ld = GNUNET_LOAD_get_load (datastore_put_load);
2286 if (ld < 2.0 * (1 + priority))
2288 GNUNET_STATISTICS_update (stats,
2289 gettext_noop ("# storage requests dropped due to high load"),
2296 /* ******************* Pending Request Refresh Task ******************** */
2301 * We use a random delay to make the timing of requests less
2302 * predictable. This function returns such a random delay. We add a base
2303 * delay of MAX_CORK_DELAY (1s).
2305 * FIXME: make schedule dependent on the specifics of the request?
2306 * Or bandwidth and number of connected peers and load?
2308 * @return random delay to use for some request, between 1s and 1000+TTL_DECREMENT ms
2310 static struct GNUNET_TIME_Relative
2311 get_processing_delay ()
2314 GNUNET_TIME_relative_add (GNUNET_CONSTANTS_MAX_CORK_DELAY,
2315 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
2316 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2322 * We're processing a GET request from another peer and have decided
2323 * to forward it to other peers. This function is called periodically
2324 * and should forward the request to other peers until we have all
2325 * possible replies. If we have transmitted the *only* reply to
2326 * the initiator we should destroy the pending request. If we have
2327 * many replies in the queue to the initiator, we should delay sending
2328 * out more queries until the reply queue has shrunk some.
2330 * @param cls our "struct ProcessGetContext *"
2334 forward_request_task (void *cls,
2335 const struct GNUNET_SCHEDULER_TaskContext *tc);
2339 * Function called after we either failed or succeeded
2340 * at transmitting a query to a peer.
2342 * @param cls the requests "struct PendingRequest*"
2343 * @param tpid ID of receiving peer, 0 on transmission error
2346 transmit_query_continuation (void *cls,
2347 GNUNET_PEER_Id tpid)
2349 struct PendingRequest *pr = cls;
2352 GNUNET_STATISTICS_update (stats,
2353 gettext_noop ("# queries scheduled for forwarding"),
2359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2360 "Transmission of request failed, will try again later.\n");
2362 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
2363 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
2364 get_processing_delay (),
2365 &forward_request_task,
2370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2371 "Transmitted query `%s'\n",
2372 GNUNET_h2s (&pr->query));
2374 GNUNET_STATISTICS_update (stats,
2375 gettext_noop ("# queries forwarded"),
2378 for (i=0;i<pr->used_targets_off;i++)
2379 if (pr->used_targets[i].pid == tpid)
2380 break; /* found match! */
2381 if (i == pr->used_targets_off)
2383 /* need to create new entry */
2384 if (pr->used_targets_off == pr->used_targets_size)
2385 GNUNET_array_grow (pr->used_targets,
2386 pr->used_targets_size,
2387 pr->used_targets_size * 2 + 2);
2388 GNUNET_PEER_change_rc (tpid, 1);
2389 pr->used_targets[pr->used_targets_off].pid = tpid;
2390 pr->used_targets[pr->used_targets_off].num_requests = 0;
2391 i = pr->used_targets_off++;
2393 pr->used_targets[i].last_request_time = GNUNET_TIME_absolute_get ();
2394 pr->used_targets[i].num_requests++;
2395 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
2396 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
2397 get_processing_delay (),
2398 &forward_request_task,
2404 * How many bytes should a bloomfilter be if we have already seen
2405 * entry_count responses? Note that BLOOMFILTER_K gives us the number
2406 * of bits set per entry. Furthermore, we should not re-size the
2407 * filter too often (to keep it cheap).
2409 * Since other peers will also add entries but not resize the filter,
2410 * we should generally pick a slightly larger size than what the
2411 * strict math would suggest.
2413 * @return must be a power of two and smaller or equal to 2^15.
2416 compute_bloomfilter_size (unsigned int entry_count)
2419 unsigned int ideal = (entry_count * BLOOMFILTER_K) / 4;
2420 uint16_t max = 1 << 15;
2422 if (entry_count > max)
2425 while ((size < max) && (size < ideal))
2434 * Recalculate our bloom filter for filtering replies. This function
2435 * will create a new bloom filter from scratch, so it should only be
2436 * called if we have no bloomfilter at all (and hence can create a
2437 * fresh one of minimal size without problems) OR if our peer is the
2438 * initiator (in which case we may resize to larger than mimimum size).
2440 * @param pr request for which the BF is to be recomputed
2443 refresh_bloomfilter (struct PendingRequest *pr)
2447 GNUNET_HashCode mhash;
2449 nsize = compute_bloomfilter_size (pr->replies_seen_off);
2450 if (nsize == pr->bf_size)
2451 return; /* size not changed */
2453 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
2454 pr->bf_size = nsize;
2455 pr->mingle = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, -1);
2456 pr->bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
2459 for (i=0;i<pr->replies_seen_off;i++)
2461 GNUNET_BLOCK_mingle_hash (&pr->replies_seen[i],
2464 GNUNET_CONTAINER_bloomfilter_add (pr->bf, &mhash);
2470 * Function called after we've tried to reserve a certain amount of
2471 * bandwidth for a reply. Check if we succeeded and if so send our
2474 * @param cls the requests "struct PendingRequest*"
2475 * @param peer identifies the peer
2476 * @param bpm_in set to the current bandwidth limit (receiving) for this peer
2477 * @param bpm_out set to the current bandwidth limit (sending) for this peer
2478 * @param amount set to the amount that was actually reserved or unreserved
2479 * @param preference current traffic preference for the given peer
2482 target_reservation_cb (void *cls,
2484 GNUNET_PeerIdentity * peer,
2485 struct GNUNET_BANDWIDTH_Value32NBO bpm_in,
2486 struct GNUNET_BANDWIDTH_Value32NBO bpm_out,
2488 uint64_t preference)
2490 struct PendingRequest *pr = cls;
2491 struct ConnectedPeer *cp;
2492 struct PendingMessage *pm;
2493 struct GetMessage *gm;
2494 GNUNET_HashCode *ext;
2505 /* error in communication with core, try again later */
2506 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
2507 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
2508 get_processing_delay (),
2509 &forward_request_task,
2513 /* (3) transmit, update ttl/priority */
2514 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
2518 /* Peer must have just left */
2520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2521 "Selected peer disconnected!\n");
2523 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
2524 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
2525 get_processing_delay (),
2526 &forward_request_task,
2530 no_route = GNUNET_NO;
2536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2537 "Failed to reserve bandwidth for reply (got %d/%u bytes only)!\n",
2541 GNUNET_STATISTICS_update (stats,
2542 gettext_noop ("# reply bandwidth reservation requests failed"),
2545 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
2546 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
2547 get_processing_delay (),
2548 &forward_request_task,
2550 return; /* this target round failed */
2552 no_route = GNUNET_YES;
2555 GNUNET_STATISTICS_update (stats,
2556 gettext_noop ("# queries scheduled for forwarding"),
2559 for (i=0;i<pr->used_targets_off;i++)
2560 if (pr->used_targets[i].pid == cp->pid)
2562 GNUNET_STATISTICS_update (stats,
2563 gettext_noop ("# queries retransmitted to same target"),
2569 /* build message and insert message into priority queue */
2571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2572 "Forwarding request `%s' to `%4s'!\n",
2573 GNUNET_h2s (&pr->query),
2578 if (GNUNET_YES == no_route)
2580 bm |= GET_MESSAGE_BIT_RETURN_TO;
2583 if (pr->namespace != NULL)
2585 bm |= GET_MESSAGE_BIT_SKS_NAMESPACE;
2588 if (pr->target_pid != 0)
2590 bm |= GET_MESSAGE_BIT_TRANSMIT_TO;
2593 msize = sizeof (struct GetMessage) + pr->bf_size + k * sizeof(GNUNET_HashCode);
2594 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
2595 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
2597 gm = (struct GetMessage*) &pm[1];
2598 gm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_GET);
2599 gm->header.size = htons (msize);
2600 gm->type = htonl (pr->type);
2601 pr->remaining_priority /= 2;
2602 gm->priority = htonl (pr->remaining_priority);
2603 gm->ttl = htonl (pr->ttl);
2604 gm->filter_mutator = htonl(pr->mingle);
2605 gm->hash_bitmap = htonl (bm);
2606 gm->query = pr->query;
2607 ext = (GNUNET_HashCode*) &gm[1];
2609 if (GNUNET_YES == no_route)
2610 GNUNET_PEER_resolve (pr->cp->pid, (struct GNUNET_PeerIdentity*) &ext[k++]);
2611 if (pr->namespace != NULL)
2612 memcpy (&ext[k++], pr->namespace, sizeof (GNUNET_HashCode));
2613 if (pr->target_pid != 0)
2614 GNUNET_PEER_resolve (pr->target_pid, (struct GNUNET_PeerIdentity*) &ext[k++]);
2615 bfdata = (char *) &ext[k];
2617 GNUNET_CONTAINER_bloomfilter_get_raw_data (pr->bf,
2620 pm->cont = &transmit_query_continuation;
2622 cp->last_request_times[(cp->last_request_times_off++) % MAX_QUEUE_PER_PEER] = GNUNET_TIME_absolute_get ();
2623 add_to_pending_messages_for_peer (cp, pm, pr);
2628 * Closure used for "target_peer_select_cb".
2630 struct PeerSelectionContext
2633 * The request for which we are selecting
2636 struct PendingRequest *pr;
2639 * Current "prime" target.
2641 struct GNUNET_PeerIdentity target;
2644 * How much do we like this target?
2646 double target_score;
2652 * Function called for each connected peer to determine
2653 * which one(s) would make good targets for forwarding.
2655 * @param cls closure (struct PeerSelectionContext)
2656 * @param key current key code (peer identity)
2657 * @param value value in the hash map (struct ConnectedPeer)
2658 * @return GNUNET_YES if we should continue to
2663 target_peer_select_cb (void *cls,
2664 const GNUNET_HashCode * key,
2667 struct PeerSelectionContext *psc = cls;
2668 struct ConnectedPeer *cp = value;
2669 struct PendingRequest *pr = psc->pr;
2670 struct GNUNET_TIME_Relative delay;
2675 /* 1) check that this peer is not the initiator */
2679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2680 "Skipping initiator in forwarding selection\n");
2682 return GNUNET_YES; /* skip */
2685 /* 2) check if we have already (recently) forwarded to this peer */
2686 /* 2a) this particular request */
2688 for (i=0;i<pr->used_targets_off;i++)
2689 if (pr->used_targets[i].pid == cp->pid)
2691 pc = pr->used_targets[i].num_requests;
2692 GNUNET_assert (pc > 0);
2693 if (0 != GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2694 RETRY_PROBABILITY_INV * pc))
2697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2698 "NOT re-trying query that was previously transmitted %u times\n",
2701 return GNUNET_YES; /* skip */
2708 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2709 "Re-trying query that was previously transmitted %u times to this peer\n",
2713 /* 2b) many other requests to this peer */
2714 delay = GNUNET_TIME_absolute_get_duration (cp->last_request_times[cp->last_request_times_off % MAX_QUEUE_PER_PEER]);
2715 if (delay.value <= cp->avg_delay.value)
2718 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2719 "NOT sending query since we send %u others to this peer in the last %llums\n",
2721 cp->avg_delay.value);
2723 return GNUNET_YES; /* skip */
2726 /* 3) calculate how much we'd like to forward to this peer,
2727 starting with a random value that is strong enough
2728 to at least give any peer a chance sometimes
2729 (compared to the other factors that come later) */
2730 /* 3a) count successful (recent) routes from cp for same source */
2733 score = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2734 P2P_SUCCESS_LIST_SIZE);
2735 for (i=0;i<P2P_SUCCESS_LIST_SIZE;i++)
2736 if (cp->last_p2p_replies[i] == pr->cp->pid)
2737 score += 1.0; /* likely successful based on hot path */
2741 score = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2742 CS2P_SUCCESS_LIST_SIZE);
2743 for (i=0;i<CS2P_SUCCESS_LIST_SIZE;i++)
2744 if (cp->last_client_replies[i] == pr->client_request_list->client_list->client)
2745 score += 1.0; /* likely successful based on hot path */
2747 /* 3b) include latency */
2748 if (cp->avg_delay.value < 4 * TTL_DECREMENT)
2749 score += 1.0; /* likely fast based on latency */
2750 /* 3c) include priorities */
2751 if (cp->avg_priority <= pr->remaining_priority / 2.0)
2752 score += 1.0; /* likely successful based on priorities */
2753 /* 3d) penalize for queue size */
2754 score -= (2.0 * cp->pending_requests / (double) MAX_QUEUE_PER_PEER);
2755 /* 3e) include peer proximity */
2756 score -= (2.0 * (GNUNET_CRYPTO_hash_distance_u32 (key,
2757 &pr->query)) / (double) UINT32_MAX);
2758 /* 4) super-bonus for being the known target */
2759 if (pr->target_pid == cp->pid)
2761 /* store best-fit in closure */
2763 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2764 "Peer `%s' gets score %f for forwarding query, max is %f\n",
2769 score++; /* avoid zero */
2770 if (score > psc->target_score)
2772 psc->target_score = score;
2773 psc->target.hashPubKey = *key;
2780 * The priority level imposes a bound on the maximum
2781 * value for the ttl that can be requested.
2783 * @param ttl_in requested ttl
2784 * @param prio given priority
2785 * @return ttl_in if ttl_in is below the limit,
2786 * otherwise the ttl-limit for the given priority
2789 bound_ttl (int32_t ttl_in, uint32_t prio)
2791 unsigned long long allowed;
2795 allowed = ((unsigned long long) prio) * TTL_DECREMENT / 1000;
2796 if (ttl_in > allowed)
2798 if (allowed >= (1 << 30))
2807 * Iterator called on each result obtained for a DHT
2808 * operation that expects a reply
2810 * @param cls closure
2811 * @param exp when will this value expire
2812 * @param key key of the result
2813 * @param get_path NULL-terminated array of pointers
2814 * to the peers on reverse GET path (or NULL if not recorded)
2815 * @param put_path NULL-terminated array of pointers
2816 * to the peers on the PUT path (or NULL if not recorded)
2817 * @param type type of the result
2818 * @param size number of bytes in data
2819 * @param data pointer to the result data
2822 process_dht_reply (void *cls,
2823 struct GNUNET_TIME_Absolute exp,
2824 const GNUNET_HashCode * key,
2825 const struct GNUNET_PeerIdentity * const *get_path,
2826 const struct GNUNET_PeerIdentity * const *put_path,
2827 enum GNUNET_BLOCK_Type type,
2833 * We're processing a GET request and have decided
2834 * to forward it to other peers. This function is called periodically
2835 * and should forward the request to other peers until we have all
2836 * possible replies. If we have transmitted the *only* reply to
2837 * the initiator we should destroy the pending request. If we have
2838 * many replies in the queue to the initiator, we should delay sending
2839 * out more queries until the reply queue has shrunk some.
2841 * @param cls our "struct ProcessGetContext *"
2845 forward_request_task (void *cls,
2846 const struct GNUNET_SCHEDULER_TaskContext *tc)
2848 struct PendingRequest *pr = cls;
2849 struct PeerSelectionContext psc;
2850 struct ConnectedPeer *cp;
2851 struct GNUNET_TIME_Relative delay;
2853 pr->task = GNUNET_SCHEDULER_NO_TASK;
2854 if (pr->irc != NULL)
2857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2858 "Forwarding of query `%s' not attempted due to pending local lookup!\n",
2859 GNUNET_h2s (&pr->query));
2861 return; /* already pending */
2863 if (GNUNET_YES == pr->local_only)
2864 return; /* configured to not do P2P search */
2866 if ( (0 == pr->anonymity_level) &&
2867 (GNUNET_YES != pr->forward_only) &&
2868 (pr->type != GNUNET_BLOCK_TYPE_FS_DBLOCK) &&
2869 (pr->type != GNUNET_BLOCK_TYPE_FS_IBLOCK) )
2871 pr->dht_get = GNUNET_DHT_get_start (dht_handle,
2872 GNUNET_TIME_UNIT_FOREVER_REL,
2879 (pr->namespace != NULL) ? sizeof (GNUNET_HashCode) : 0,
2883 /* (1) select target */
2885 psc.target_score = -DBL_MAX;
2886 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
2887 &target_peer_select_cb,
2889 if (psc.target_score == -DBL_MAX)
2891 delay = get_processing_delay ();
2893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2894 "No peer selected for forwarding of query `%s', will try again in %llu ms!\n",
2895 GNUNET_h2s (&pr->query),
2898 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
2900 &forward_request_task,
2902 return; /* nobody selected */
2904 /* (3) update TTL/priority */
2905 if (pr->client_request_list != NULL)
2907 /* FIXME: use better algorithm!? */
2908 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2911 /* bound priority we use by priorities we see from other peers
2912 rounded up (must round up so that we can see non-zero
2913 priorities, but round up as little as possible to make it
2914 plausible that we forwarded another peers request) */
2915 if (pr->priority > current_priorities + 1.0)
2916 pr->priority = (uint32_t) current_priorities + 1.0;
2917 pr->ttl = bound_ttl (pr->ttl + TTL_DECREMENT * 2,
2920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2921 "Trying query `%s' with priority %u and TTL %d.\n",
2922 GNUNET_h2s (&pr->query),
2928 /* (3) reserve reply bandwidth */
2929 if (GNUNET_NO == pr->forward_only)
2931 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
2932 &psc.target.hashPubKey);
2933 GNUNET_assert (NULL != cp);
2934 pr->irc = GNUNET_CORE_peer_change_preference (sched, cfg,
2936 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
2937 GNUNET_BANDWIDTH_value_init (UINT32_MAX),
2940 &target_reservation_cb,
2942 cp->inc_preference = 0;
2946 /* force forwarding */
2947 static struct GNUNET_BANDWIDTH_Value32NBO zerobw;
2948 target_reservation_cb (pr, &psc.target,
2949 zerobw, zerobw, 0, 0.0);
2954 /* **************************** P2P PUT Handling ************************ */
2958 * Function called after we either failed or succeeded
2959 * at transmitting a reply to a peer.
2961 * @param cls the requests "struct PendingRequest*"
2962 * @param tpid ID of receiving peer, 0 on transmission error
2965 transmit_reply_continuation (void *cls,
2966 GNUNET_PEER_Id tpid)
2968 struct PendingRequest *pr = cls;
2972 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
2973 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
2974 /* only one reply expected, done with the request! */
2975 destroy_pending_request (pr);
2977 case GNUNET_BLOCK_TYPE_ANY:
2978 case GNUNET_BLOCK_TYPE_FS_KBLOCK:
2979 case GNUNET_BLOCK_TYPE_FS_SBLOCK:
2989 * Transmit the given message by copying it to the target buffer
2990 * "buf". "buf" will be NULL and "size" zero if the socket was closed
2991 * for writing in the meantime. In that case, do nothing
2992 * (the disconnect or shutdown handler will take care of the rest).
2993 * If we were able to transmit messages and there are still more
2994 * pending, ask core again for further calls to this function.
2996 * @param cls closure, pointer to the 'struct ClientList*'
2997 * @param size number of bytes available in buf
2998 * @param buf where the callee should write the message
2999 * @return number of bytes written to buf
3002 transmit_to_client (void *cls,
3003 size_t size, void *buf)
3005 struct ClientList *cl = cls;
3007 struct ClientResponseMessage *creply;
3014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3015 "Not sending reply, client communication problem.\n");
3020 while ( (NULL != (creply = cl->res_head) ) &&
3021 (creply->msize <= size) )
3023 memcpy (&cbuf[msize], &creply[1], creply->msize);
3024 msize += creply->msize;
3025 size -= creply->msize;
3026 GNUNET_CONTAINER_DLL_remove (cl->res_head,
3029 GNUNET_free (creply);
3032 cl->th = GNUNET_SERVER_notify_transmit_ready (cl->client,
3034 GNUNET_TIME_UNIT_FOREVER_REL,
3035 &transmit_to_client,
3038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3039 "Transmitted %u bytes to client\n",
3040 (unsigned int) msize);
3047 * Closure for "process_reply" function.
3049 struct ProcessReplyClosure
3052 * The data for the reply.
3057 * Who gave us this reply? NULL for local host (or DHT)
3059 struct ConnectedPeer *sender;
3062 * When the reply expires.
3064 struct GNUNET_TIME_Absolute expiration;
3072 * Type of the block.
3074 enum GNUNET_BLOCK_Type type;
3077 * How much was this reply worth to us?
3082 * Evaluation result (returned).
3084 enum GNUNET_BLOCK_EvaluationResult eval;
3087 * Did we finish processing the associated request?
3092 * Did we find a matching request?
3099 * We have received a reply; handle it!
3101 * @param cls response (struct ProcessReplyClosure)
3102 * @param key our query
3103 * @param value value in the hash map (info about the query)
3104 * @return GNUNET_YES (we should continue to iterate)
3107 process_reply (void *cls,
3108 const GNUNET_HashCode * key,
3111 struct ProcessReplyClosure *prq = cls;
3112 struct PendingRequest *pr = value;
3113 struct PendingMessage *reply;
3114 struct ClientResponseMessage *creply;
3115 struct ClientList *cl;
3116 struct PutMessage *pm;
3117 struct ConnectedPeer *cp;
3118 struct GNUNET_TIME_Relative cur_delay;
3120 struct GNUNET_TIME_Relative art_delay;
3126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3127 "Matched result (type %u) for query `%s' with pending request\n",
3128 (unsigned int) prq->type,
3131 GNUNET_STATISTICS_update (stats,
3132 gettext_noop ("# replies received and matched"),
3135 if (prq->sender != NULL)
3137 for (i=0;i<pr->used_targets_off;i++)
3138 if (pr->used_targets[i].pid == prq->sender->pid)
3140 if (i < pr->used_targets_off)
3142 cur_delay = GNUNET_TIME_absolute_get_duration (pr->used_targets[i].last_request_time);
3143 prq->sender->avg_delay.value
3144 = (prq->sender->avg_delay.value *
3145 (RUNAVG_DELAY_N - 1) + cur_delay.value) / RUNAVG_DELAY_N;
3146 prq->sender->avg_priority
3147 = (prq->sender->avg_priority *
3148 (RUNAVG_DELAY_N - 1) + pr->priority) / (double) RUNAVG_DELAY_N;
3152 GNUNET_PEER_change_rc (prq->sender->last_p2p_replies
3153 [prq->sender->last_p2p_replies_woff % P2P_SUCCESS_LIST_SIZE],
3155 GNUNET_PEER_change_rc (pr->cp->pid, 1);
3156 prq->sender->last_p2p_replies
3157 [(prq->sender->last_p2p_replies_woff++) % P2P_SUCCESS_LIST_SIZE]
3162 if (NULL != prq->sender->last_client_replies
3163 [(prq->sender->last_client_replies_woff) % CS2P_SUCCESS_LIST_SIZE])
3164 GNUNET_SERVER_client_drop (prq->sender->last_client_replies
3165 [(prq->sender->last_client_replies_woff) % CS2P_SUCCESS_LIST_SIZE]);
3166 prq->sender->last_client_replies
3167 [(prq->sender->last_client_replies_woff++) % CS2P_SUCCESS_LIST_SIZE]
3168 = pr->client_request_list->client_list->client;
3169 GNUNET_SERVER_client_keep (pr->client_request_list->client_list->client);
3172 prq->eval = GNUNET_BLOCK_evaluate (block_ctx,
3177 pr->namespace, (pr->namespace != NULL) ? sizeof (GNUNET_HashCode) : 0,
3182 case GNUNET_BLOCK_EVALUATION_OK_MORE:
3184 case GNUNET_BLOCK_EVALUATION_OK_LAST:
3185 while (NULL != pr->pending_head)
3186 destroy_pending_message_list_entry (pr->pending_head);
3189 if (pr->client_request_list != NULL)
3190 GNUNET_SERVER_receive_done (pr->client_request_list->client_list->client,
3192 GNUNET_DATASTORE_cancel (pr->qe);
3195 pr->do_remove = GNUNET_YES;
3196 if (pr->task != GNUNET_SCHEDULER_NO_TASK)
3198 GNUNET_SCHEDULER_cancel (sched,
3200 pr->task = GNUNET_SCHEDULER_NO_TASK;
3202 GNUNET_break (GNUNET_YES ==
3203 GNUNET_CONTAINER_multihashmap_remove (query_request_map,
3206 GNUNET_LOAD_update (rt_entry_lifetime,
3207 GNUNET_TIME_absolute_get_duration (pr->start_time).value);
3209 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
3210 GNUNET_STATISTICS_update (stats,
3211 gettext_noop ("# duplicate replies discarded (bloomfilter)"),
3215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3216 "Duplicate response `%s', discarding.\n",
3217 GNUNET_h2s (&mhash));
3219 return GNUNET_YES; /* duplicate */
3220 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
3221 return GNUNET_YES; /* wrong namespace */
3222 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
3225 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
3228 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
3229 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3230 _("Unsupported block type %u\n"),
3234 if (pr->client_request_list != NULL)
3236 if (pr->replies_seen_size == pr->replies_seen_off)
3237 GNUNET_array_grow (pr->replies_seen,
3238 pr->replies_seen_size,
3239 pr->replies_seen_size * 2 + 4);
3240 GNUNET_CRYPTO_hash (prq->data,
3242 &pr->replies_seen[pr->replies_seen_off++]);
3243 refresh_bloomfilter (pr);
3245 if (NULL == prq->sender)
3248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3249 "Found result for query `%s' in local datastore\n",
3252 GNUNET_STATISTICS_update (stats,
3253 gettext_noop ("# results found locally"),
3257 prq->priority += pr->remaining_priority;
3258 pr->remaining_priority = 0;
3259 pr->results_found++;
3260 prq->request_found = GNUNET_YES;
3261 if (NULL != pr->client_request_list)
3263 GNUNET_STATISTICS_update (stats,
3264 gettext_noop ("# replies received for local clients"),
3267 cl = pr->client_request_list->client_list;
3268 msize = sizeof (struct PutMessage) + prq->size;
3269 creply = GNUNET_malloc (msize + sizeof (struct ClientResponseMessage));
3270 creply->msize = msize;
3271 creply->client_list = cl;
3272 GNUNET_CONTAINER_DLL_insert_after (cl->res_head,
3276 pm = (struct PutMessage*) &creply[1];
3277 pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
3278 pm->header.size = htons (msize);
3279 pm->type = htonl (prq->type);
3280 pm->expiration = GNUNET_TIME_absolute_hton (prq->expiration);
3281 memcpy (&pm[1], prq->data, prq->size);
3285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3286 "Transmitting result for query `%s' to client\n",
3289 cl->th = GNUNET_SERVER_notify_transmit_ready (cl->client,
3291 GNUNET_TIME_UNIT_FOREVER_REL,
3292 &transmit_to_client,
3295 GNUNET_break (cl->th != NULL);
3298 prq->finished = GNUNET_YES;
3299 destroy_pending_request (pr);
3306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3307 "Transmitting result for query `%s' to other peer (PID=%u)\n",
3309 (unsigned int) cp->pid);
3311 GNUNET_STATISTICS_update (stats,
3312 gettext_noop ("# replies received for other peers"),
3315 msize = sizeof (struct PutMessage) + prq->size;
3316 reply = GNUNET_malloc (msize + sizeof (struct PendingMessage));
3317 reply->cont = &transmit_reply_continuation;
3318 reply->cont_cls = pr;
3320 art_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
3321 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
3324 = GNUNET_TIME_relative_to_absolute (art_delay);
3325 GNUNET_STATISTICS_update (stats,
3326 gettext_noop ("cummulative artificial delay introduced (ms)"),
3330 reply->msize = msize;
3331 reply->priority = UINT32_MAX; /* send replies first! */
3332 pm = (struct PutMessage*) &reply[1];
3333 pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
3334 pm->header.size = htons (msize);
3335 pm->type = htonl (prq->type);
3336 pm->expiration = GNUNET_TIME_absolute_hton (prq->expiration);
3337 memcpy (&pm[1], prq->data, prq->size);
3338 add_to_pending_messages_for_peer (cp, reply, pr);
3345 * Iterator called on each result obtained for a DHT
3346 * operation that expects a reply
3348 * @param cls closure
3349 * @param exp when will this value expire
3350 * @param key key of the result
3351 * @param get_path NULL-terminated array of pointers
3352 * to the peers on reverse GET path (or NULL if not recorded)
3353 * @param put_path NULL-terminated array of pointers
3354 * to the peers on the PUT path (or NULL if not recorded)
3355 * @param type type of the result
3356 * @param size number of bytes in data
3357 * @param data pointer to the result data
3360 process_dht_reply (void *cls,
3361 struct GNUNET_TIME_Absolute exp,
3362 const GNUNET_HashCode * key,
3363 const struct GNUNET_PeerIdentity * const *get_path,
3364 const struct GNUNET_PeerIdentity * const *put_path,
3365 enum GNUNET_BLOCK_Type type,
3369 struct PendingRequest *pr = cls;
3370 struct ProcessReplyClosure prq;
3372 memset (&prq, 0, sizeof (prq));
3374 prq.expiration = exp;
3377 process_reply (&prq, key, pr);
3383 * Continuation called to notify client about result of the
3386 * @param cls closure
3387 * @param success GNUNET_SYSERR on failure
3388 * @param msg NULL on success, otherwise an error message
3391 put_migration_continuation (void *cls,
3395 struct GNUNET_TIME_Absolute *start = cls;
3396 struct GNUNET_TIME_Relative delay;
3398 delay = GNUNET_TIME_absolute_get_duration (*start);
3399 GNUNET_free (start);
3400 GNUNET_LOAD_update (datastore_put_load,
3402 if (GNUNET_OK == success)
3404 GNUNET_STATISTICS_update (stats,
3405 gettext_noop ("# datastore 'put' failures"),
3412 * Handle P2P "PUT" message.
3414 * @param cls closure, always NULL
3415 * @param other the other peer involved (sender or receiver, NULL
3416 * for loopback messages where we are both sender and receiver)
3417 * @param message the actual message
3418 * @param latency reported latency of the connection with 'other'
3419 * @param distance reported distance (DV) to 'other'
3420 * @return GNUNET_OK to keep the connection open,
3421 * GNUNET_SYSERR to close it (signal serious error)
3424 handle_p2p_put (void *cls,
3425 const struct GNUNET_PeerIdentity *other,
3426 const struct GNUNET_MessageHeader *message,
3427 struct GNUNET_TIME_Relative latency,
3430 const struct PutMessage *put;
3433 enum GNUNET_BLOCK_Type type;
3434 struct GNUNET_TIME_Absolute expiration;
3435 GNUNET_HashCode query;
3436 struct ProcessReplyClosure prq;
3437 struct GNUNET_TIME_Absolute *start;
3438 struct GNUNET_TIME_Relative block_time;
3440 struct ConnectedPeer *cp;
3441 struct PendingMessage *pm;
3442 struct MigrationStopMessage *msm;
3444 msize = ntohs (message->size);
3445 if (msize < sizeof (struct PutMessage))
3448 return GNUNET_SYSERR;
3450 put = (const struct PutMessage*) message;
3451 dsize = msize - sizeof (struct PutMessage);
3452 type = ntohl (put->type);
3453 expiration = GNUNET_TIME_absolute_ntoh (put->expiration);
3455 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
3456 return GNUNET_SYSERR;
3458 GNUNET_BLOCK_get_key (block_ctx,
3464 GNUNET_break_op (0);
3465 return GNUNET_SYSERR;
3468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3469 "Received result for query `%s' from peer `%4s'\n",
3470 GNUNET_h2s (&query),
3471 GNUNET_i2s (other));
3473 GNUNET_STATISTICS_update (stats,
3474 gettext_noop ("# replies received (overall)"),
3477 /* now, lookup 'query' */
3478 prq.data = (const void*) &put[1];
3480 prq.sender = GNUNET_CONTAINER_multihashmap_get (connected_peers,
3481 &other->hashPubKey);
3486 prq.expiration = expiration;
3488 prq.finished = GNUNET_NO;
3489 prq.request_found = GNUNET_NO;
3490 GNUNET_CONTAINER_multihashmap_get_multiple (query_request_map,
3494 if (prq.sender != NULL)
3496 prq.sender->inc_preference += CONTENT_BANDWIDTH_VALUE + 1000 * prq.priority;
3497 prq.sender->trust += prq.priority;
3499 if ( (GNUNET_YES == active_migration) &&
3500 (GNUNET_NO == test_put_load_too_high (prq.priority)) )
3503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3504 "Replicating result for query `%s' with priority %u\n",
3505 GNUNET_h2s (&query),
3508 start = GNUNET_malloc (sizeof (struct GNUNET_TIME_Absolute));
3509 *start = GNUNET_TIME_absolute_get ();
3510 GNUNET_DATASTORE_put (dsh,
3511 0, &query, dsize, &put[1],
3512 type, prq.priority, 1 /* anonymity */,
3514 1 + prq.priority, MAX_DATASTORE_QUEUE,
3515 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
3516 &put_migration_continuation,
3519 putl = GNUNET_LOAD_get_load (datastore_put_load);
3520 if ( (GNUNET_NO == prq.request_found) &&
3521 ( (GNUNET_YES != active_migration) ||
3522 (putl > 2.5 * (1 + prq.priority)) ) )
3524 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
3525 &other->hashPubKey);
3526 if (GNUNET_TIME_absolute_get_duration (cp->last_migration_block).value < 5000)
3527 return GNUNET_OK; /* already blocked */
3528 /* We're too busy; send MigrationStop message! */
3529 if (GNUNET_YES != active_migration)
3530 putl = 1.0 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 5);
3531 block_time = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
3532 5000 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
3533 (unsigned int) (60000 * putl * putl)));
3535 cp->last_migration_block = GNUNET_TIME_relative_to_absolute (block_time);
3536 pm = GNUNET_malloc (sizeof (struct PendingMessage) +
3537 sizeof (struct MigrationStopMessage));
3538 pm->msize = sizeof (struct MigrationStopMessage);
3539 pm->priority = UINT32_MAX;
3540 msm = (struct MigrationStopMessage*) &pm[1];
3541 msm->header.size = htons (sizeof (struct MigrationStopMessage));
3542 msm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_MIGRATION_STOP);
3543 msm->duration = GNUNET_TIME_relative_hton (block_time);
3544 add_to_pending_messages_for_peer (cp,
3553 * Handle P2P "MIGRATION_STOP" message.
3555 * @param cls closure, always NULL
3556 * @param other the other peer involved (sender or receiver, NULL
3557 * for loopback messages where we are both sender and receiver)
3558 * @param message the actual message
3559 * @param latency reported latency of the connection with 'other'
3560 * @param distance reported distance (DV) to 'other'
3561 * @return GNUNET_OK to keep the connection open,
3562 * GNUNET_SYSERR to close it (signal serious error)
3565 handle_p2p_migration_stop (void *cls,
3566 const struct GNUNET_PeerIdentity *other,
3567 const struct GNUNET_MessageHeader *message,
3568 struct GNUNET_TIME_Relative latency,
3571 struct ConnectedPeer *cp;
3572 const struct MigrationStopMessage *msm;
3574 msm = (const struct MigrationStopMessage*) message;
3575 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
3576 &other->hashPubKey);
3582 cp->migration_blocked = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_ntoh (msm->duration));
3588 /* **************************** P2P GET Handling ************************ */
3592 * Closure for 'check_duplicate_request_{peer,client}'.
3594 struct CheckDuplicateRequestClosure
3597 * The new request we should check if it already exists.
3599 const struct PendingRequest *pr;
3602 * Existing request found by the checker, NULL if none.
3604 struct PendingRequest *have;
3609 * Iterator over entries in the 'query_request_map' that
3610 * tries to see if we have the same request pending from
3611 * the same client already.
3613 * @param cls closure (our 'struct CheckDuplicateRequestClosure')
3614 * @param key current key code (query, ignored, must match)
3615 * @param value value in the hash map (a 'struct PendingRequest'
3616 * that already exists)
3617 * @return GNUNET_YES if we should continue to
3618 * iterate (no match yet)
3619 * GNUNET_NO if not (match found).
3622 check_duplicate_request_client (void *cls,
3623 const GNUNET_HashCode * key,
3626 struct CheckDuplicateRequestClosure *cdc = cls;
3627 struct PendingRequest *have = value;
3629 if (have->client_request_list == NULL)
3631 if ( (cdc->pr->client_request_list->client_list->client == have->client_request_list->client_list->client) &&
3642 * We're processing (local) results for a search request
3643 * from another peer. Pass applicable results to the
3644 * peer and if we are done either clean up (operation
3645 * complete) or forward to other peers (more results possible).
3647 * @param cls our closure (struct LocalGetContext)
3648 * @param key key for the content
3649 * @param size number of bytes in data
3650 * @param data content stored
3651 * @param type type of the content
3652 * @param priority priority of the content
3653 * @param anonymity anonymity-level for the content
3654 * @param expiration expiration time for the content
3655 * @param uid unique identifier for the datum;
3656 * maybe 0 if no unique identifier is available
3659 process_local_reply (void *cls,
3660 const GNUNET_HashCode * key,
3663 enum GNUNET_BLOCK_Type type,
3666 struct GNUNET_TIME_Absolute
3670 struct PendingRequest *pr = cls;
3671 struct ProcessReplyClosure prq;
3672 struct CheckDuplicateRequestClosure cdrc;
3673 GNUNET_HashCode query;
3674 unsigned int old_rf;
3679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3680 "Done processing local replies, forwarding request to other peers.\n");
3683 if (pr->client_request_list != NULL)
3685 GNUNET_SERVER_receive_done (pr->client_request_list->client_list->client,
3687 /* Figure out if this is a duplicate request and possibly
3688 merge 'struct PendingRequest' entries */
3691 GNUNET_CONTAINER_multihashmap_get_multiple (query_request_map,
3693 &check_duplicate_request_client,
3695 if (cdrc.have != NULL)
3698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3699 "Received request for block `%s' twice from client, will only request once.\n",
3700 GNUNET_h2s (&pr->query));
3703 destroy_pending_request (pr);
3708 /* no more results */
3709 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
3710 pr->task = GNUNET_SCHEDULER_add_now (sched,
3711 &forward_request_task,
3716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3717 "New local response to `%s' of type %u.\n",
3721 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
3724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3725 "Found ONDEMAND block, performing on-demand encoding\n");
3727 GNUNET_STATISTICS_update (stats,
3728 gettext_noop ("# on-demand blocks matched requests"),
3732 GNUNET_FS_handle_on_demand_block (key, size, data, type, priority,
3733 anonymity, expiration, uid,
3734 &process_local_reply,
3738 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
3742 old_rf = pr->results_found;
3743 memset (&prq, 0, sizeof (prq));
3745 prq.expiration = expiration;
3748 GNUNET_BLOCK_get_key (block_ctx,
3755 GNUNET_DATASTORE_remove (dsh,
3759 GNUNET_TIME_UNIT_FOREVER_REL,
3761 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
3765 prq.priority = priority;
3766 prq.finished = GNUNET_NO;
3767 prq.request_found = GNUNET_NO;
3768 if ( (old_rf == 0) &&
3769 (pr->results_found == 0) )
3770 update_datastore_delays (pr->start_time);
3771 process_reply (&prq, key, pr);
3772 if (prq.finished == GNUNET_YES)
3775 return; /* done here */
3776 if (prq.eval == GNUNET_BLOCK_EVALUATION_OK_LAST)
3778 GNUNET_DATASTORE_get_next (dsh, GNUNET_NO);
3781 if ( (pr->client_request_list == NULL) &&
3782 ( (GNUNET_YES == test_get_load_too_high (0)) ||
3783 (pr->results_found > 5 + 2 * pr->priority) ) )
3786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3787 "Load too high, done with request\n");
3789 GNUNET_STATISTICS_update (stats,
3790 gettext_noop ("# processing result set cut short due to load"),
3793 GNUNET_DATASTORE_get_next (dsh, GNUNET_NO);
3796 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
3801 * We've received a request with the specified priority. Bound it
3802 * according to how much we trust the given peer.
3804 * @param prio_in requested priority
3805 * @param cp the peer making the request
3806 * @return effective priority
3809 bound_priority (uint32_t prio_in,
3810 struct ConnectedPeer *cp)
3812 #define N ((double)128.0)
3817 ld = test_get_load_too_high (0);
3818 if (ld == GNUNET_SYSERR)
3819 return 0; /* excess resources */
3820 ret = change_host_trust (cp, prio_in);
3823 if (ret > current_priorities + N)
3824 rret = current_priorities + N;
3828 = (current_priorities * (N-1) + rret)/N;
3830 if ( (ld == GNUNET_YES) && (ret > 0) )
3832 /* try with charging */
3833 ld = test_get_load_too_high (ret);
3835 if (ld == GNUNET_YES)
3839 change_host_trust (cp, -ret);
3840 return -1; /* not enough resources */
3848 * Iterator over entries in the 'query_request_map' that
3849 * tries to see if we have the same request pending from
3850 * the same peer already.
3852 * @param cls closure (our 'struct CheckDuplicateRequestClosure')
3853 * @param key current key code (query, ignored, must match)
3854 * @param value value in the hash map (a 'struct PendingRequest'
3855 * that already exists)
3856 * @return GNUNET_YES if we should continue to
3857 * iterate (no match yet)
3858 * GNUNET_NO if not (match found).
3861 check_duplicate_request_peer (void *cls,
3862 const GNUNET_HashCode * key,
3865 struct CheckDuplicateRequestClosure *cdc = cls;
3866 struct PendingRequest *have = value;
3868 if (cdc->pr->target_pid == have->target_pid)
3878 * Handle P2P "GET" request.
3880 * @param cls closure, always NULL
3881 * @param other the other peer involved (sender or receiver, NULL
3882 * for loopback messages where we are both sender and receiver)
3883 * @param message the actual message
3884 * @param latency reported latency of the connection with 'other'
3885 * @param distance reported distance (DV) to 'other'
3886 * @return GNUNET_OK to keep the connection open,
3887 * GNUNET_SYSERR to close it (signal serious error)
3890 handle_p2p_get (void *cls,
3891 const struct GNUNET_PeerIdentity *other,
3892 const struct GNUNET_MessageHeader *message,
3893 struct GNUNET_TIME_Relative latency,
3896 struct PendingRequest *pr;
3897 struct ConnectedPeer *cp;
3898 struct ConnectedPeer *cps;
3899 struct CheckDuplicateRequestClosure cdc;
3900 struct GNUNET_TIME_Relative timeout;
3902 const struct GetMessage *gm;
3904 const GNUNET_HashCode *opt;
3907 uint32_t ttl_decrement;
3909 enum GNUNET_BLOCK_Type type;
3912 msize = ntohs(message->size);
3913 if (msize < sizeof (struct GetMessage))
3915 GNUNET_break_op (0);
3916 return GNUNET_SYSERR;
3918 gm = (const struct GetMessage*) message;
3920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3921 "Received request for `%s'\n",
3922 GNUNET_h2s (&gm->query));
3924 type = ntohl (gm->type);
3925 bm = ntohl (gm->hash_bitmap);
3933 if (msize < sizeof (struct GetMessage) + bits * sizeof (GNUNET_HashCode))
3935 GNUNET_break_op (0);
3936 return GNUNET_SYSERR;
3938 opt = (const GNUNET_HashCode*) &gm[1];
3939 bfsize = msize - sizeof (struct GetMessage) + bits * sizeof (GNUNET_HashCode);
3940 bm = ntohl (gm->hash_bitmap);
3942 cps = GNUNET_CONTAINER_multihashmap_get (connected_peers,
3943 &other->hashPubKey);
3946 /* peer must have just disconnected */
3947 GNUNET_STATISTICS_update (stats,
3948 gettext_noop ("# requests dropped due to initiator not being connected"),
3951 return GNUNET_SYSERR;
3953 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
3954 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
3961 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
3962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3963 "Failed to find RETURN-TO peer `%4s' in connection set. Dropping query.\n",
3964 GNUNET_i2s ((const struct GNUNET_PeerIdentity*) &opt[bits-1]));
3967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3968 "Failed to find peer `%4s' in connection set. Dropping query.\n",
3969 GNUNET_i2s (other));
3971 GNUNET_STATISTICS_update (stats,
3972 gettext_noop ("# requests dropped due to missing reverse route"),
3975 /* FIXME: try connect? */
3978 /* note that we can really only check load here since otherwise
3979 peers could find out that we are overloaded by not being
3980 disconnected after sending us a malformed query... */
3981 priority = bound_priority (ntohl (gm->priority), cps);
3985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3986 "Dropping query from `%s', this peer is too busy.\n",
3987 GNUNET_i2s (other));
3989 GNUNET_STATISTICS_update (stats,
3990 gettext_noop ("# requests dropped due to high load"),
3996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3997 "Received request for `%s' of type %u from peer `%4s' with flags %u\n",
3998 GNUNET_h2s (&gm->query),
3999 (unsigned int) type,
4003 have_ns = (0 != (bm & GET_MESSAGE_BIT_SKS_NAMESPACE));
4004 pr = GNUNET_malloc (sizeof (struct PendingRequest) +
4005 (have_ns ? sizeof(GNUNET_HashCode) : 0));
4008 pr->namespace = (GNUNET_HashCode*) &pr[1];
4009 memcpy (&pr[1], &opt[bits++], sizeof (GNUNET_HashCode));
4011 if ( (GNUNET_LOAD_get_load (cp->transmission_delay) > 3 * (1 + priority)) ||
4012 (GNUNET_LOAD_get_average (cp->transmission_delay) >
4013 GNUNET_CONSTANTS_MAX_CORK_DELAY.value * 2 + GNUNET_LOAD_get_average (rt_entry_lifetime)) )
4015 /* don't have BW to send to peer, or would likely take longer than we have for it,
4016 so at best indirect the query */
4018 pr->forward_only = GNUNET_YES;
4021 pr->mingle = ntohl (gm->filter_mutator);
4022 if (0 != (bm & GET_MESSAGE_BIT_TRANSMIT_TO))
4023 pr->target_pid = GNUNET_PEER_intern ((const struct GNUNET_PeerIdentity*) &opt[bits++]);
4024 pr->anonymity_level = 1;
4025 pr->priority = (uint32_t) priority;
4026 pr->ttl = bound_ttl (ntohl (gm->ttl), pr->priority);
4027 pr->query = gm->query;
4028 /* decrement ttl (always) */
4029 ttl_decrement = 2 * TTL_DECREMENT +
4030 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
4032 if ( (pr->ttl < 0) &&
4033 (((int32_t)(pr->ttl - ttl_decrement)) > 0) )
4036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4037 "Dropping query from `%s' due to TTL underflow (%d - %u).\n",
4042 GNUNET_STATISTICS_update (stats,
4043 gettext_noop ("# requests dropped due TTL underflow"),
4046 /* integer underflow => drop (should be very rare)! */
4050 pr->ttl -= ttl_decrement;
4051 pr->start_time = GNUNET_TIME_absolute_get ();
4053 /* get bloom filter */
4056 pr->bf = GNUNET_CONTAINER_bloomfilter_init ((const char*) &opt[bits],
4059 pr->bf_size = bfsize;
4063 GNUNET_CONTAINER_multihashmap_get_multiple (query_request_map,
4065 &check_duplicate_request_peer,
4067 if (cdc.have != NULL)
4069 if (cdc.have->start_time.value + cdc.have->ttl >=
4070 pr->start_time.value + pr->ttl)
4072 /* existing request has higher TTL, drop new one! */
4073 cdc.have->priority += pr->priority;
4074 destroy_pending_request (pr);
4076 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4077 "Have existing request with higher TTL, dropping new request.\n",
4078 GNUNET_i2s (other));
4080 GNUNET_STATISTICS_update (stats,
4081 gettext_noop ("# requests dropped due to higher-TTL request"),
4088 /* existing request has lower TTL, drop old one! */
4089 pr->priority += cdc.have->priority;
4090 /* Possible optimization: if we have applicable pending
4091 replies in 'cdc.have', we might want to move those over
4092 (this is a really rare special-case, so it is not clear
4093 that this would be worth it) */
4094 destroy_pending_request (cdc.have);
4095 /* keep processing 'pr'! */
4100 GNUNET_break (GNUNET_OK ==
4101 GNUNET_CONTAINER_multihashmap_put (query_request_map,
4104 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
4105 GNUNET_break (GNUNET_OK ==
4106 GNUNET_CONTAINER_multihashmap_put (peer_request_map,
4109 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
4111 pr->hnode = GNUNET_CONTAINER_heap_insert (requests_by_expiration_heap,
4113 pr->start_time.value + pr->ttl);
4115 GNUNET_STATISTICS_update (stats,
4116 gettext_noop ("# P2P searches received"),
4119 GNUNET_STATISTICS_update (stats,
4120 gettext_noop ("# P2P searches active"),
4124 /* calculate change in traffic preference */
4125 cps->inc_preference += pr->priority * 1000 + QUERY_BANDWIDTH_VALUE;
4126 /* process locally */
4127 if (type == GNUNET_BLOCK_TYPE_FS_DBLOCK)
4128 type = GNUNET_BLOCK_TYPE_ANY; /* to get on-demand as well */
4129 timeout = GNUNET_TIME_relative_multiply (BASIC_DATASTORE_REQUEST_DELAY,
4130 (pr->priority + 1));
4131 if (GNUNET_YES != pr->forward_only)
4134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4135 "Handing request for `%s' to datastore\n",
4136 GNUNET_h2s (&gm->query));
4138 pr->qe = GNUNET_DATASTORE_get (dsh,
4142 MAX_DATASTORE_QUEUE,
4144 &process_local_reply,
4148 GNUNET_STATISTICS_update (stats,
4149 gettext_noop ("# requests dropped by datastore (queue length limit)"),
4156 GNUNET_STATISTICS_update (stats,
4157 gettext_noop ("# requests forwarded due to high load"),
4162 /* Are multiple results possible (and did we look locally)? If so, start processing remotely now! */
4165 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
4166 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
4167 /* only one result, wait for datastore */
4168 if (GNUNET_YES != pr->forward_only)
4171 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
4172 pr->task = GNUNET_SCHEDULER_add_now (sched,
4173 &forward_request_task,
4177 /* make sure we don't track too many requests */
4178 if (GNUNET_CONTAINER_heap_get_size (requests_by_expiration_heap) > max_pending_requests)
4180 pr = GNUNET_CONTAINER_heap_peek (requests_by_expiration_heap);
4181 GNUNET_assert (pr != NULL);
4182 destroy_pending_request (pr);
4188 /* **************************** CS GET Handling ************************ */
4192 * Handle START_SEARCH-message (search request from client).
4194 * @param cls closure
4195 * @param client identification of the client
4196 * @param message the actual message
4199 handle_start_search (void *cls,
4200 struct GNUNET_SERVER_Client *client,
4201 const struct GNUNET_MessageHeader *message)
4203 static GNUNET_HashCode all_zeros;
4204 const struct SearchMessage *sm;
4205 struct ClientList *cl;
4206 struct ClientRequestList *crl;
4207 struct PendingRequest *pr;
4210 enum GNUNET_BLOCK_Type type;
4212 msize = ntohs (message->size);
4213 if ( (msize < sizeof (struct SearchMessage)) ||
4214 (0 != (msize - sizeof (struct SearchMessage)) % sizeof (GNUNET_HashCode)) )
4217 GNUNET_SERVER_receive_done (client,
4221 GNUNET_STATISTICS_update (stats,
4222 gettext_noop ("# client searches received"),
4225 sc = (msize - sizeof (struct SearchMessage)) / sizeof (GNUNET_HashCode);
4226 sm = (const struct SearchMessage*) message;
4227 type = ntohl (sm->type);
4229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4230 "Received request for `%s' of type %u from local client\n",
4231 GNUNET_h2s (&sm->query),
4232 (unsigned int) type);
4235 while ( (cl != NULL) &&
4236 (cl->client != client) )
4240 cl = GNUNET_malloc (sizeof (struct ClientList));
4241 cl->client = client;
4242 GNUNET_SERVER_client_keep (client);
4243 cl->next = client_list;
4246 /* detect duplicate KBLOCK requests */
4247 if ( (type == GNUNET_BLOCK_TYPE_FS_KBLOCK) ||
4248 (type == GNUNET_BLOCK_TYPE_FS_NBLOCK) ||
4249 (type == GNUNET_BLOCK_TYPE_ANY) )
4252 while ( (crl != NULL) &&
4253 ( (0 != memcmp (&crl->req->query,
4255 sizeof (GNUNET_HashCode))) ||
4256 (crl->req->type != type) ) )
4261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4262 "Have existing request, merging content-seen lists.\n");
4265 /* Duplicate request (used to send long list of
4266 known/blocked results); merge 'pr->replies_seen'
4267 and update bloom filter */
4268 GNUNET_array_grow (pr->replies_seen,
4269 pr->replies_seen_size,
4270 pr->replies_seen_off + sc);
4271 memcpy (&pr->replies_seen[pr->replies_seen_off],
4273 sc * sizeof (GNUNET_HashCode));
4274 pr->replies_seen_off += sc;
4275 refresh_bloomfilter (pr);
4276 GNUNET_STATISTICS_update (stats,
4277 gettext_noop ("# client searches updated (merged content seen list)"),
4280 GNUNET_SERVER_receive_done (client,
4285 GNUNET_STATISTICS_update (stats,
4286 gettext_noop ("# client searches active"),
4289 pr = GNUNET_malloc (sizeof (struct PendingRequest) +
4290 ((type == GNUNET_BLOCK_TYPE_FS_SBLOCK) ? sizeof(GNUNET_HashCode) : 0));
4291 crl = GNUNET_malloc (sizeof (struct ClientRequestList));
4292 memset (crl, 0, sizeof (struct ClientRequestList));
4293 crl->client_list = cl;
4294 GNUNET_CONTAINER_DLL_insert (cl->rl_head,
4299 pr->client_request_list = crl;
4300 GNUNET_array_grow (pr->replies_seen,
4301 pr->replies_seen_size,
4303 memcpy (pr->replies_seen,
4305 sc * sizeof (GNUNET_HashCode));
4306 pr->replies_seen_off = sc;
4307 pr->anonymity_level = ntohl (sm->anonymity_level);
4308 pr->start_time = GNUNET_TIME_absolute_get ();
4309 refresh_bloomfilter (pr);
4310 pr->query = sm->query;
4311 if (0 == (1 & ntohl (sm->options)))
4312 pr->local_only = GNUNET_NO;
4314 pr->local_only = GNUNET_YES;
4317 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
4318 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
4319 if (0 != memcmp (&sm->target,
4321 sizeof (GNUNET_HashCode)))
4322 pr->target_pid = GNUNET_PEER_intern ((const struct GNUNET_PeerIdentity*) &sm->target);
4324 case GNUNET_BLOCK_TYPE_FS_SBLOCK:
4325 pr->namespace = (GNUNET_HashCode*) &pr[1];
4326 memcpy (&pr[1], &sm->target, sizeof (GNUNET_HashCode));
4331 GNUNET_break (GNUNET_OK ==
4332 GNUNET_CONTAINER_multihashmap_put (query_request_map,
4335 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
4336 if (type == GNUNET_BLOCK_TYPE_FS_DBLOCK)
4337 type = GNUNET_BLOCK_TYPE_ANY; /* get on-demand blocks too! */
4338 pr->qe = GNUNET_DATASTORE_get (dsh,
4342 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
4343 &process_local_reply,
4348 /* **************************** Startup ************************ */
4351 * Process fs requests.
4353 * @param s scheduler to use
4354 * @param server the initialized server
4355 * @param c configuration to use
4358 main_init (struct GNUNET_SCHEDULER_Handle *s,
4359 struct GNUNET_SERVER_Handle *server,
4360 const struct GNUNET_CONFIGURATION_Handle *c)
4362 static const struct GNUNET_CORE_MessageHandler p2p_handlers[] =
4365 GNUNET_MESSAGE_TYPE_FS_GET, 0 },
4367 GNUNET_MESSAGE_TYPE_FS_PUT, 0 },
4368 { &handle_p2p_migration_stop,
4369 GNUNET_MESSAGE_TYPE_FS_MIGRATION_STOP,
4370 sizeof (struct MigrationStopMessage) },
4373 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
4374 {&GNUNET_FS_handle_index_start, NULL,
4375 GNUNET_MESSAGE_TYPE_FS_INDEX_START, 0},
4376 {&GNUNET_FS_handle_index_list_get, NULL,
4377 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_GET, sizeof(struct GNUNET_MessageHeader) },
4378 {&GNUNET_FS_handle_unindex, NULL, GNUNET_MESSAGE_TYPE_FS_UNINDEX,
4379 sizeof (struct UnindexMessage) },
4380 {&handle_start_search, NULL, GNUNET_MESSAGE_TYPE_FS_START_SEARCH,
4384 unsigned long long enc = 128;
4388 stats = GNUNET_STATISTICS_create (sched, "fs", cfg);
4389 min_migration_delay = GNUNET_TIME_UNIT_SECONDS;
4391 GNUNET_CONFIGURATION_get_value_number (cfg,
4393 "MAX_PENDING_REQUESTS",
4394 &max_pending_requests)) ||
4396 GNUNET_CONFIGURATION_get_value_number (cfg,
4398 "EXPECTED_NEIGHBOUR_COUNT",
4401 GNUNET_CONFIGURATION_get_value_time (cfg,
4403 "MIN_MIGRATION_DELAY",
4404 &min_migration_delay)) )
4406 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4407 _("Configuration fails to specify certain parameters, assuming default values."));
4409 connected_peers = GNUNET_CONTAINER_multihashmap_create (enc);
4410 query_request_map = GNUNET_CONTAINER_multihashmap_create (max_pending_requests);
4411 rt_entry_lifetime = GNUNET_LOAD_value_init ();
4412 peer_request_map = GNUNET_CONTAINER_multihashmap_create (enc);
4413 requests_by_expiration_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
4414 core = GNUNET_CORE_connect (sched,
4416 GNUNET_TIME_UNIT_FOREVER_REL,
4419 &peer_connect_handler,
4420 &peer_disconnect_handler,
4427 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4428 _("Failed to connect to `%s' service.\n"),
4430 GNUNET_CONTAINER_multihashmap_destroy (connected_peers);
4431 connected_peers = NULL;
4432 GNUNET_CONTAINER_multihashmap_destroy (query_request_map);
4433 query_request_map = NULL;
4434 GNUNET_LOAD_value_free (rt_entry_lifetime);
4435 rt_entry_lifetime = NULL;
4436 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
4437 requests_by_expiration_heap = NULL;
4438 GNUNET_CONTAINER_multihashmap_destroy (peer_request_map);
4439 peer_request_map = NULL;
4442 GNUNET_DATASTORE_disconnect (dsh, GNUNET_NO);
4445 return GNUNET_SYSERR;
4447 /* FIXME: distinguish between sending and storing in options? */
4448 if (active_migration)
4450 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4451 _("Content migration is enabled, will start to gather data\n"));
4452 consider_migration_gathering ();
4454 consider_dht_put_gathering (NULL);
4455 GNUNET_SERVER_disconnect_notify (server,
4456 &handle_client_disconnect,
4458 GNUNET_assert (GNUNET_OK ==
4459 GNUNET_CONFIGURATION_get_value_filename (cfg,
4463 GNUNET_DISK_directory_create (trustDirectory);
4464 GNUNET_SCHEDULER_add_with_priority (sched,
4465 GNUNET_SCHEDULER_PRIORITY_HIGH,
4466 &cron_flush_trust, NULL);
4469 GNUNET_SERVER_add_handlers (server, handlers);
4470 GNUNET_SCHEDULER_add_delayed (sched,
4471 GNUNET_TIME_UNIT_FOREVER_REL,
4479 * Process fs requests.
4481 * @param cls closure
4482 * @param sched scheduler to use
4483 * @param server the initialized server
4484 * @param cfg configuration to use
4488 struct GNUNET_SCHEDULER_Handle *sched,
4489 struct GNUNET_SERVER_Handle *server,
4490 const struct GNUNET_CONFIGURATION_Handle *cfg)
4492 active_migration = GNUNET_CONFIGURATION_get_value_yesno (cfg,
4495 dsh = GNUNET_DATASTORE_connect (cfg,
4499 GNUNET_SCHEDULER_shutdown (sched);
4502 datastore_get_load = GNUNET_LOAD_value_init ();
4503 datastore_put_load = GNUNET_LOAD_value_init ();
4504 block_cfg = GNUNET_CONFIGURATION_create ();
4505 GNUNET_CONFIGURATION_set_value_string (block_cfg,
4509 block_ctx = GNUNET_BLOCK_context_create (block_cfg);
4510 GNUNET_assert (NULL != block_ctx);
4511 dht_handle = GNUNET_DHT_connect (sched,
4514 if ( (GNUNET_OK != GNUNET_FS_indexing_init (sched, cfg, dsh)) ||
4515 (GNUNET_OK != main_init (sched, server, cfg)) )
4517 GNUNET_SCHEDULER_shutdown (sched);
4518 GNUNET_DATASTORE_disconnect (dsh, GNUNET_NO);
4520 GNUNET_DHT_disconnect (dht_handle);
4522 GNUNET_BLOCK_context_destroy (block_ctx);
4524 GNUNET_CONFIGURATION_destroy (block_cfg);
4526 GNUNET_LOAD_value_free (datastore_get_load);
4527 datastore_get_load = NULL;
4528 GNUNET_LOAD_value_free (datastore_put_load);
4529 datastore_put_load = NULL;
4536 * The main function for the fs service.
4538 * @param argc number of arguments from the command line
4539 * @param argv command line arguments
4540 * @return 0 ok, 1 on error
4543 main (int argc, char *const *argv)
4545 return (GNUNET_OK ==
4546 GNUNET_SERVICE_run (argc,
4549 GNUNET_SERVICE_OPTION_NONE,
4550 &run, NULL)) ? 0 : 1;
4553 /* end of gnunet-service-fs.c */