2 This file is part of GNUnet.
3 (C) 2009, 2010 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file fs/gnunet-service-fs.c
23 * @brief gnunet anonymity protocol implementation
24 * @author Christian Grothoff
27 * - implement test_load_too_high, make decision priority-based, implement forwarding, etc.
28 * - consider more precise latency estimation (per-peer & request)
29 * - introduce random latency in processing
30 * - tell other peers to stop migration if our PUTs fail (or if
31 * we don't support migration per configuration?)
36 #include "gnunet_constants.h"
37 #include "gnunet_core_service.h"
38 #include "gnunet_datastore_service.h"
39 #include "gnunet_peer_lib.h"
40 #include "gnunet_protocols.h"
41 #include "gnunet_signatures.h"
42 #include "gnunet_statistics_service.h"
43 #include "gnunet_util_lib.h"
44 #include "gnunet-service-fs_indexing.h"
47 #define DEBUG_FS GNUNET_NO
50 * Maximum number of outgoing messages we queue per peer.
52 #define MAX_QUEUE_PER_PEER 16
55 * How often do we flush trust values to disk?
57 #define TRUST_FLUSH_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
60 * Inverse of the probability that we will submit the same query
61 * to the same peer again. If the same peer already got the query
62 * repeatedly recently, the probability is multiplied by the inverse
63 * of this number each time. Note that we only try about every TTL_DECREMENT/2
64 * plus MAX_CORK_DELAY (so roughly every 3.5s).
66 #define RETRY_PROBABILITY_INV 3
69 * What is the maximum delay for a P2P FS message (in our interaction
70 * with core)? FS-internal delays are another story. The value is
71 * chosen based on the 32k block size. Given that peers typcially
72 * have at least 1 kb/s bandwidth, 45s waits give us a chance to
73 * transmit one message even to the lowest-bandwidth peers.
75 #define MAX_TRANSMIT_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 45)
78 * Maximum number of requests (from other peers) that we're
79 * willing to have pending at any given point in time.
81 static unsigned long long max_pending_requests = (32 * 1024);
85 * Information we keep for each pending reply. The
86 * actual message follows at the end of this struct.
88 struct PendingMessage;
91 * Function called upon completion of a transmission.
94 * @param pid ID of receiving peer, 0 on transmission error
96 typedef void (*TransmissionContinuation)(void * cls,
101 * Information we keep for each pending message (GET/PUT). The
102 * actual message follows at the end of this struct.
104 struct PendingMessage
107 * This is a doubly-linked list of messages to the same peer.
109 struct PendingMessage *next;
112 * This is a doubly-linked list of messages to the same peer.
114 struct PendingMessage *prev;
117 * Entry in pending message list for this pending message.
119 struct PendingMessageList *pml;
122 * Function to call immediately once we have transmitted this
125 TransmissionContinuation cont;
133 * Size of the reply; actual reply message follows
134 * at the end of this struct.
139 * How important is this message for us?
147 * Information about a peer that we are connected to.
148 * We track data that is useful for determining which
149 * peers should receive our requests. We also keep
150 * a list of messages to transmit to this peer.
156 * List of the last clients for which this peer successfully
159 struct GNUNET_SERVER_Client *last_client_replies[CS2P_SUCCESS_LIST_SIZE];
162 * List of the last PIDs for which
163 * this peer successfully answered a query;
164 * We use 0 to indicate no successful reply.
166 GNUNET_PEER_Id last_p2p_replies[P2P_SUCCESS_LIST_SIZE];
169 * Average delay between sending the peer a request and
170 * getting a reply (only calculated over the requests for
171 * which we actually got a reply). Calculated
172 * as a moving average: new_delay = ((n-1)*last_delay+curr_delay) / n
174 struct GNUNET_TIME_Relative avg_delay;
177 * Handle for an active request for transmission to this
180 struct GNUNET_CORE_TransmitHandle *cth;
183 * Messages (replies, queries, content migration) we would like to
184 * send to this peer in the near future. Sorted by priority, head.
186 struct PendingMessage *pending_messages_head;
189 * Messages (replies, queries, content migration) we would like to
190 * send to this peer in the near future. Sorted by priority, tail.
192 struct PendingMessage *pending_messages_tail;
195 * Average priority of successful replies. Calculated
196 * as a moving average: new_avg = ((n-1)*last_avg+curr_prio) / n
201 * Increase in traffic preference still to be submitted
202 * to the core service for this peer.
204 uint64_t inc_preference;
207 * Trust rating for this peer
212 * Trust rating for this peer on disk.
217 * The peer's identity.
222 * Size of the linked list of 'pending_messages'.
224 unsigned int pending_requests;
227 * Which offset in "last_p2p_replies" will be updated next?
228 * (we go round-robin).
230 unsigned int last_p2p_replies_woff;
233 * Which offset in "last_client_replies" will be updated next?
234 * (we go round-robin).
236 unsigned int last_client_replies_woff;
242 * Information we keep for each pending request. We should try to
243 * keep this struct as small as possible since its memory consumption
244 * is key to how many requests we can have pending at once.
246 struct PendingRequest;
250 * Doubly-linked list of requests we are performing
251 * on behalf of the same client.
253 struct ClientRequestList
257 * This is a doubly-linked list.
259 struct ClientRequestList *next;
262 * This is a doubly-linked list.
264 struct ClientRequestList *prev;
267 * Request this entry represents.
269 struct PendingRequest *req;
272 * Client list this request belongs to.
274 struct ClientList *client_list;
280 * Replies to be transmitted to the client. The actual
281 * response message is allocated after this struct.
283 struct ClientResponseMessage
286 * This is a doubly-linked list.
288 struct ClientResponseMessage *next;
291 * This is a doubly-linked list.
293 struct ClientResponseMessage *prev;
296 * Client list entry this response belongs to.
298 struct ClientList *client_list;
301 * Number of bytes in the response.
308 * Linked list of clients we are performing requests
314 * This is a linked list.
316 struct ClientList *next;
319 * ID of a client making a request, NULL if this entry is for a
322 struct GNUNET_SERVER_Client *client;
325 * Head of list of requests performed on behalf
326 * of this client right now.
328 struct ClientRequestList *rl_head;
331 * Tail of list of requests performed on behalf
332 * of this client right now.
334 struct ClientRequestList *rl_tail;
337 * Head of linked list of responses.
339 struct ClientResponseMessage *res_head;
342 * Tail of linked list of responses.
344 struct ClientResponseMessage *res_tail;
347 * Context for sending replies.
349 struct GNUNET_CONNECTION_TransmitHandle *th;
355 * Doubly-linked list of messages we are performing
356 * due to a pending request.
358 struct PendingMessageList
362 * This is a doubly-linked list of messages on behalf of the same request.
364 struct PendingMessageList *next;
367 * This is a doubly-linked list of messages on behalf of the same request.
369 struct PendingMessageList *prev;
372 * Message this entry represents.
374 struct PendingMessage *pm;
377 * Request this entry belongs to.
379 struct PendingRequest *req;
382 * Peer this message is targeted for.
384 struct ConnectedPeer *target;
390 * Information we keep for each pending request. We should try to
391 * keep this struct as small as possible since its memory consumption
392 * is key to how many requests we can have pending at once.
394 struct PendingRequest
398 * If this request was made by a client, this is our entry in the
399 * client request list; otherwise NULL.
401 struct ClientRequestList *client_request_list;
404 * Entry of peer responsible for this entry (if this request
405 * was made by a peer).
407 struct ConnectedPeer *cp;
410 * If this is a namespace query, pointer to the hash of the public
411 * key of the namespace; otherwise NULL. Pointer will be to the
412 * end of this struct (so no need to free it).
414 const GNUNET_HashCode *namespace;
417 * Bloomfilter we use to filter out replies that we don't care about
418 * (anymore). NULL as long as we are interested in all replies.
420 struct GNUNET_CONTAINER_BloomFilter *bf;
423 * Context of our GNUNET_CORE_peer_change_preference call.
425 struct GNUNET_CORE_InformationRequestContext *irc;
428 * Hash code of all replies that we have seen so far (only valid
429 * if client is not NULL since we only track replies like this for
432 GNUNET_HashCode *replies_seen;
435 * Node in the heap representing this entry; NULL
436 * if we have no heap node.
438 struct GNUNET_CONTAINER_HeapNode *hnode;
441 * Head of list of messages being performed on behalf of this
444 struct PendingMessageList *pending_head;
447 * Tail of list of messages being performed on behalf of this
450 struct PendingMessageList *pending_tail;
453 * When did we first see this request (form this peer), or, if our
454 * client is initiating, when did we last initiate a search?
456 struct GNUNET_TIME_Absolute start_time;
459 * The query that this request is for.
461 GNUNET_HashCode query;
464 * The task responsible for transmitting queries
467 GNUNET_SCHEDULER_TaskIdentifier task;
470 * (Interned) Peer identifier that identifies a preferred target
473 GNUNET_PEER_Id target_pid;
476 * (Interned) Peer identifiers of peers that have already
477 * received our query for this content.
479 GNUNET_PEER_Id *used_pids;
482 * Our entry in the queue (non-NULL while we wait for our
483 * turn to interact with the local database).
485 struct GNUNET_DATASTORE_QueueEntry *qe;
488 * Size of the 'bf' (in bytes).
493 * Desired anonymity level; only valid for requests from a local client.
495 uint32_t anonymity_level;
498 * How many entries in "used_pids" are actually valid?
500 unsigned int used_pids_off;
503 * How long is the "used_pids" array?
505 unsigned int used_pids_size;
508 * Number of results found for this request.
510 unsigned int results_found;
513 * How many entries in "replies_seen" are actually valid?
515 unsigned int replies_seen_off;
518 * How long is the "replies_seen" array?
520 unsigned int replies_seen_size;
523 * Priority with which this request was made. If one of our clients
524 * made the request, then this is the current priority that we are
525 * using when initiating the request. This value is used when
526 * we decide to reward other peers with trust for providing a reply.
531 * Priority points left for us to spend when forwarding this request
534 uint32_t remaining_priority;
537 * Number to mingle hashes for bloom-filter tests with.
542 * TTL with which we saw this request (or, if we initiated, TTL that
543 * we used for the request).
548 * Type of the content that this request is for.
550 enum GNUNET_BLOCK_Type type;
553 * Remove this request after transmission of the current response.
558 * GNUNET_YES if we should not forward this request to other peers.
566 * Block that is ready for migration to other peers. Actual data is at the end of the block.
568 struct MigrationReadyBlock
572 * This is a doubly-linked list.
574 struct MigrationReadyBlock *next;
577 * This is a doubly-linked list.
579 struct MigrationReadyBlock *prev;
582 * Query for the block.
584 GNUNET_HashCode query;
587 * When does this block expire?
589 struct GNUNET_TIME_Absolute expiration;
592 * Peers we would consider forwarding this
593 * block to. Zero for empty entries.
595 GNUNET_PEER_Id target_list[MIGRATION_LIST_SIZE];
603 * Number of targets already used.
605 unsigned int used_targets;
610 enum GNUNET_BLOCK_Type type;
615 * Our connection to the datastore.
617 static struct GNUNET_DATASTORE_Handle *dsh;
622 static struct GNUNET_BLOCK_Context *block_ctx;
625 * Our block configuration.
627 static struct GNUNET_CONFIGURATION_Handle *block_cfg;
632 static struct GNUNET_SCHEDULER_Handle *sched;
637 static const struct GNUNET_CONFIGURATION_Handle *cfg;
640 * Map of peer identifiers to "struct ConnectedPeer" (for that peer).
642 static struct GNUNET_CONTAINER_MultiHashMap *connected_peers;
645 * Map of peer identifiers to "struct PendingRequest" (for that peer).
647 static struct GNUNET_CONTAINER_MultiHashMap *peer_request_map;
650 * Map of query identifiers to "struct PendingRequest" (for that query).
652 static struct GNUNET_CONTAINER_MultiHashMap *query_request_map;
655 * Heap with the request that will expire next at the top. Contains
656 * pointers of type "struct PendingRequest*"; these will *also* be
657 * aliased from the "requests_by_peer" data structures and the
658 * "requests_by_query" table. Note that requests from our clients
659 * don't expire and are thus NOT in the "requests_by_expiration"
660 * (or the "requests_by_peer" tables).
662 static struct GNUNET_CONTAINER_Heap *requests_by_expiration_heap;
665 * Handle for reporting statistics.
667 static struct GNUNET_STATISTICS_Handle *stats;
670 * Linked list of clients we are currently processing requests for.
672 static struct ClientList *client_list;
675 * Pointer to handle to the core service (points to NULL until we've
678 static struct GNUNET_CORE_Handle *core;
681 * Head of linked list of blocks that can be migrated.
683 static struct MigrationReadyBlock *mig_head;
686 * Tail of linked list of blocks that can be migrated.
688 static struct MigrationReadyBlock *mig_tail;
691 * Request to datastore for migration (or NULL).
693 static struct GNUNET_DATASTORE_QueueEntry *mig_qe;
696 * Where do we store trust information?
698 static char *trustDirectory;
701 * ID of task that collects blocks for migration.
703 static GNUNET_SCHEDULER_TaskIdentifier mig_task;
706 * What is the maximum frequency at which we are allowed to
707 * poll the datastore for migration content?
709 static struct GNUNET_TIME_Relative min_migration_delay;
712 * Size of the doubly-linked list of migration blocks.
714 static unsigned int mig_size;
717 * Are we allowed to migrate content to this peer.
719 static int active_migration;
722 * Typical priorities we're seeing from other peers right now. Since
723 * most priorities will be zero, this value is the weighted average of
724 * non-zero priorities seen "recently". In order to ensure that new
725 * values do not dramatically change the ratio, values are first
726 * "capped" to a reasonable range (+N of the current value) and then
727 * averaged into the existing value by a ratio of 1:N. Hence
728 * receiving the largest possible priority can still only raise our
729 * "current_priorities" by at most 1.
731 static double current_priorities;
734 * Get the filename under which we would store the GNUNET_HELLO_Message
735 * for the given host and protocol.
736 * @return filename of the form DIRECTORY/HOSTID
739 get_trust_filename (const struct GNUNET_PeerIdentity *id)
741 struct GNUNET_CRYPTO_HashAsciiEncoded fil;
744 GNUNET_CRYPTO_hash_to_enc (&id->hashPubKey, &fil);
745 GNUNET_asprintf (&fn, "%s%s%s", trustDirectory, DIR_SEPARATOR_STR, &fil);
752 * Transmit messages by copying it to the target buffer
753 * "buf". "buf" will be NULL and "size" zero if the socket was closed
754 * for writing in the meantime. In that case, do nothing
755 * (the disconnect or shutdown handler will take care of the rest).
756 * If we were able to transmit messages and there are still more
757 * pending, ask core again for further calls to this function.
759 * @param cls closure, pointer to the 'struct ConnectedPeer*'
760 * @param size number of bytes available in buf
761 * @param buf where the callee should write the message
762 * @return number of bytes written to buf
765 transmit_to_peer (void *cls,
766 size_t size, void *buf);
769 /* ******************* clean up functions ************************ */
773 * Delete the given migration block.
775 * @param mb block to delete
778 delete_migration_block (struct MigrationReadyBlock *mb)
780 GNUNET_CONTAINER_DLL_remove (mig_head,
783 GNUNET_PEER_decrement_rcs (mb->target_list,
784 MIGRATION_LIST_SIZE);
791 * Compare the distance of two peers to a key.
794 * @param p1 first peer
795 * @param p2 second peer
796 * @return GNUNET_YES if P1 is closer to key than P2
799 is_closer (const GNUNET_HashCode *key,
800 const struct GNUNET_PeerIdentity *p1,
801 const struct GNUNET_PeerIdentity *p2)
803 return GNUNET_CRYPTO_hash_xorcmp (&p1->hashPubKey,
810 * Consider migrating content to a given peer.
812 * @param cls 'struct MigrationReadyBlock*' to select
813 * targets for (or NULL for none)
814 * @param key ID of the peer
815 * @param value 'struct ConnectedPeer' of the peer
816 * @return GNUNET_YES (always continue iteration)
819 consider_migration (void *cls,
820 const GNUNET_HashCode *key,
823 struct MigrationReadyBlock *mb = cls;
824 struct ConnectedPeer *cp = value;
825 struct MigrationReadyBlock *pos;
826 struct GNUNET_PeerIdentity cppid;
827 struct GNUNET_PeerIdentity otherpid;
828 struct GNUNET_PeerIdentity worstpid;
833 /* consider 'cp' as a migration target for mb */
836 GNUNET_PEER_resolve (cp->pid,
838 repl = MIGRATION_LIST_SIZE;
839 for (i=0;i<MIGRATION_LIST_SIZE;i++)
841 if (mb->target_list[i] == 0)
843 mb->target_list[i] = cp->pid;
844 GNUNET_PEER_change_rc (mb->target_list[i], 1);
845 repl = MIGRATION_LIST_SIZE;
848 GNUNET_PEER_resolve (mb->target_list[i],
850 if ( (repl == MIGRATION_LIST_SIZE) &&
851 is_closer (&mb->query,
858 else if ( (repl != MIGRATION_LIST_SIZE) &&
859 (is_closer (&mb->query,
867 if (repl != MIGRATION_LIST_SIZE)
869 GNUNET_PEER_change_rc (mb->target_list[repl], -1);
870 mb->target_list[repl] = cp->pid;
871 GNUNET_PEER_change_rc (mb->target_list[repl], 1);
875 /* consider scheduling transmission to cp for content migration */
882 for (i=0;i<MIGRATION_LIST_SIZE;i++)
884 if (cp->pid == pos->target_list[i])
889 msize = GNUNET_MIN (msize,
897 return GNUNET_YES; /* no content available */
899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
900 "Trying to migrate at least %u bytes to peer `%s'\n",
905 = GNUNET_CORE_notify_transmit_ready (core,
906 0, GNUNET_TIME_UNIT_FOREVER_REL,
907 (const struct GNUNET_PeerIdentity*) key,
908 msize + sizeof (struct PutMessage),
916 * Task that is run periodically to obtain blocks for content
920 * @param tc scheduler context (also unused)
923 gather_migration_blocks (void *cls,
924 const struct GNUNET_SCHEDULER_TaskContext *tc);
928 * If the migration task is not currently running, consider
929 * (re)scheduling it with the appropriate delay.
932 consider_migration_gathering ()
934 struct GNUNET_TIME_Relative delay;
940 if (mig_task != GNUNET_SCHEDULER_NO_TASK)
942 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
944 delay = GNUNET_TIME_relative_divide (delay,
945 MAX_MIGRATION_QUEUE);
946 delay = GNUNET_TIME_relative_max (delay,
947 min_migration_delay);
948 mig_task = GNUNET_SCHEDULER_add_delayed (sched,
950 &gather_migration_blocks,
956 * Process content offered for migration.
959 * @param key key for the content
960 * @param size number of bytes in data
961 * @param data content stored
962 * @param type type of the content
963 * @param priority priority of the content
964 * @param anonymity anonymity-level for the content
965 * @param expiration expiration time for the content
966 * @param uid unique identifier for the datum;
967 * maybe 0 if no unique identifier is available
970 process_migration_content (void *cls,
971 const GNUNET_HashCode * key,
974 enum GNUNET_BLOCK_Type type,
977 struct GNUNET_TIME_Absolute
978 expiration, uint64_t uid)
980 struct MigrationReadyBlock *mb;
985 if (mig_size < MAX_MIGRATION_QUEUE)
986 consider_migration_gathering ();
989 if (type == GNUNET_BLOCK_TYPE_ONDEMAND)
992 GNUNET_FS_handle_on_demand_block (key, size, data,
993 type, priority, anonymity,
995 &process_migration_content,
997 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
1001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1002 "Retrieved block `%s' of type %u for migration\n",
1006 mb = GNUNET_malloc (sizeof (struct MigrationReadyBlock) + size);
1008 mb->expiration = expiration;
1011 memcpy (&mb[1], data, size);
1012 GNUNET_CONTAINER_DLL_insert_after (mig_head,
1017 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
1018 &consider_migration,
1020 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
1025 * Task that is run periodically to obtain blocks for content
1029 * @param tc scheduler context (also unused)
1032 gather_migration_blocks (void *cls,
1033 const struct GNUNET_SCHEDULER_TaskContext *tc)
1035 mig_task = GNUNET_SCHEDULER_NO_TASK;
1038 mig_qe = GNUNET_DATASTORE_get_random (dsh, 0, -1,
1039 GNUNET_TIME_UNIT_FOREVER_REL,
1040 &process_migration_content, NULL);
1041 GNUNET_assert (mig_qe != NULL);
1047 * We're done with a particular message list entry.
1048 * Free all associated resources.
1050 * @param pml entry to destroy
1053 destroy_pending_message_list_entry (struct PendingMessageList *pml)
1055 GNUNET_CONTAINER_DLL_remove (pml->req->pending_head,
1056 pml->req->pending_tail,
1058 GNUNET_CONTAINER_DLL_remove (pml->target->pending_messages_head,
1059 pml->target->pending_messages_tail,
1061 pml->target->pending_requests--;
1062 GNUNET_free (pml->pm);
1068 * Destroy the given pending message (and call the respective
1071 * @param pm message to destroy
1072 * @param tpid id of peer that the message was delivered to, or 0 for none
1075 destroy_pending_message (struct PendingMessage *pm,
1076 GNUNET_PEER_Id tpid)
1078 struct PendingMessageList *pml = pm->pml;
1079 TransmissionContinuation cont;
1082 GNUNET_assert (pml->pm == pm);
1083 GNUNET_assert ( (tpid == 0) || (tpid == pml->target->pid) );
1085 cont_cls = pm->cont_cls;
1086 destroy_pending_message_list_entry (pml);
1087 cont (cont_cls, tpid);
1092 * We're done processing a particular request.
1093 * Free all associated resources.
1095 * @param pr request to destroy
1098 destroy_pending_request (struct PendingRequest *pr)
1100 struct GNUNET_PeerIdentity pid;
1102 if (pr->hnode != NULL)
1104 GNUNET_CONTAINER_heap_remove_node (requests_by_expiration_heap,
1108 if (NULL == pr->client_request_list)
1110 GNUNET_STATISTICS_update (stats,
1111 gettext_noop ("# P2P searches active"),
1117 GNUNET_STATISTICS_update (stats,
1118 gettext_noop ("# client searches active"),
1122 /* might have already been removed from map in 'process_reply' (if
1123 there was a unique reply) or never inserted if it was a
1124 duplicate; hence ignore the return value here */
1125 (void) GNUNET_CONTAINER_multihashmap_remove (query_request_map,
1130 GNUNET_DATASTORE_cancel (pr->qe);
1133 if (pr->client_request_list != NULL)
1135 GNUNET_CONTAINER_DLL_remove (pr->client_request_list->client_list->rl_head,
1136 pr->client_request_list->client_list->rl_tail,
1137 pr->client_request_list);
1138 GNUNET_free (pr->client_request_list);
1139 pr->client_request_list = NULL;
1143 GNUNET_PEER_resolve (pr->cp->pid,
1145 (void) GNUNET_CONTAINER_multihashmap_remove (peer_request_map,
1152 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
1155 if (pr->irc != NULL)
1157 GNUNET_CORE_peer_change_preference_cancel (pr->irc);
1160 if (pr->replies_seen != NULL)
1162 GNUNET_free (pr->replies_seen);
1163 pr->replies_seen = NULL;
1165 if (pr->task != GNUNET_SCHEDULER_NO_TASK)
1167 GNUNET_SCHEDULER_cancel (sched,
1169 pr->task = GNUNET_SCHEDULER_NO_TASK;
1171 while (NULL != pr->pending_head)
1172 destroy_pending_message_list_entry (pr->pending_head);
1173 GNUNET_PEER_change_rc (pr->target_pid, -1);
1174 if (pr->used_pids != NULL)
1176 GNUNET_PEER_decrement_rcs (pr->used_pids, pr->used_pids_off);
1177 GNUNET_free (pr->used_pids);
1178 pr->used_pids_off = 0;
1179 pr->used_pids_size = 0;
1180 pr->used_pids = NULL;
1187 * Method called whenever a given peer connects.
1189 * @param cls closure, not used
1190 * @param peer peer identity this notification is about
1191 * @param latency reported latency of the connection with 'other'
1192 * @param distance reported distance (DV) to 'other'
1195 peer_connect_handler (void *cls,
1197 GNUNET_PeerIdentity * peer,
1198 struct GNUNET_TIME_Relative latency,
1201 struct ConnectedPeer *cp;
1202 struct MigrationReadyBlock *pos;
1206 cp = GNUNET_malloc (sizeof (struct ConnectedPeer));
1207 cp->pid = GNUNET_PEER_intern (peer);
1209 fn = get_trust_filename (peer);
1210 if ((GNUNET_DISK_file_test (fn) == GNUNET_YES) &&
1211 (sizeof (trust) == GNUNET_DISK_fn_read (fn, &trust, sizeof (trust))))
1212 cp->disk_trust = cp->trust = ntohl (trust);
1215 GNUNET_break (GNUNET_OK ==
1216 GNUNET_CONTAINER_multihashmap_put (connected_peers,
1219 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1224 (void) consider_migration (pos, &peer->hashPubKey, cp);
1231 * Increase the host credit by a value.
1233 * @param host which peer to change the trust value on
1234 * @param value is the int value by which the
1235 * host credit is to be increased or decreased
1236 * @returns the actual change in trust (positive or negative)
1239 change_host_trust (struct ConnectedPeer *host, int value)
1241 unsigned int old_trust;
1245 GNUNET_assert (host != NULL);
1246 old_trust = host->trust;
1249 if (host->trust + value < host->trust)
1251 value = UINT32_MAX - host->trust;
1252 host->trust = UINT32_MAX;
1255 host->trust += value;
1259 if (host->trust < -value)
1261 value = -host->trust;
1265 host->trust += value;
1272 * Write host-trust information to a file - flush the buffer entry!
1275 flush_trust (void *cls,
1276 const GNUNET_HashCode *key,
1279 struct ConnectedPeer *host = value;
1282 struct GNUNET_PeerIdentity pid;
1284 if (host->trust == host->disk_trust)
1285 return GNUNET_OK; /* unchanged */
1286 GNUNET_PEER_resolve (host->pid,
1288 fn = get_trust_filename (&pid);
1289 if (host->trust == 0)
1291 if ((0 != UNLINK (fn)) && (errno != ENOENT))
1292 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
1293 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
1297 trust = htonl (host->trust);
1298 if (sizeof(uint32_t) == GNUNET_DISK_fn_write (fn, &trust,
1300 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE
1301 | GNUNET_DISK_PERM_GROUP_READ | GNUNET_DISK_PERM_OTHER_READ))
1302 host->disk_trust = host->trust;
1309 * Call this method periodically to scan data/hosts for new hosts.
1312 cron_flush_trust (void *cls,
1313 const struct GNUNET_SCHEDULER_TaskContext *tc)
1316 if (NULL == connected_peers)
1318 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
1323 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1325 GNUNET_SCHEDULER_add_delayed (tc->sched,
1326 TRUST_FLUSH_FREQ, &cron_flush_trust, NULL);
1331 * Free (each) request made by the peer.
1333 * @param cls closure, points to peer that the request belongs to
1334 * @param key current key code
1335 * @param value value in the hash map
1336 * @return GNUNET_YES (we should continue to iterate)
1339 destroy_request (void *cls,
1340 const GNUNET_HashCode * key,
1343 const struct GNUNET_PeerIdentity * peer = cls;
1344 struct PendingRequest *pr = value;
1346 GNUNET_break (GNUNET_YES ==
1347 GNUNET_CONTAINER_multihashmap_remove (peer_request_map,
1350 destroy_pending_request (pr);
1356 * Method called whenever a peer disconnects.
1358 * @param cls closure, not used
1359 * @param peer peer identity this notification is about
1362 peer_disconnect_handler (void *cls,
1364 GNUNET_PeerIdentity * peer)
1366 struct ConnectedPeer *cp;
1367 struct PendingMessage *pm;
1369 struct MigrationReadyBlock *pos;
1370 struct MigrationReadyBlock *next;
1372 GNUNET_CONTAINER_multihashmap_get_multiple (peer_request_map,
1376 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
1380 for (i=0;i<CS2P_SUCCESS_LIST_SIZE;i++)
1382 if (NULL != cp->last_client_replies[i])
1384 GNUNET_SERVER_client_drop (cp->last_client_replies[i]);
1385 cp->last_client_replies[i] = NULL;
1388 GNUNET_break (GNUNET_YES ==
1389 GNUNET_CONTAINER_multihashmap_remove (connected_peers,
1392 /* remove this peer from migration considerations; schedule
1395 while (NULL != (pos = next))
1398 for (i=0;i<MIGRATION_LIST_SIZE;i++)
1400 if (pos->target_list[i] == cp->pid)
1402 GNUNET_PEER_change_rc (pos->target_list[i], -1);
1403 pos->target_list[i] = 0;
1406 if (pos->used_targets >= GNUNET_CONTAINER_multihashmap_size (connected_peers))
1408 delete_migration_block (pos);
1409 consider_migration_gathering ();
1412 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
1413 &consider_migration,
1416 GNUNET_PEER_change_rc (cp->pid, -1);
1417 GNUNET_PEER_decrement_rcs (cp->last_p2p_replies, P2P_SUCCESS_LIST_SIZE);
1418 if (NULL != cp->cth)
1419 GNUNET_CORE_notify_transmit_ready_cancel (cp->cth);
1420 while (NULL != (pm = cp->pending_messages_head))
1421 destroy_pending_message (pm, 0 /* delivery failed */);
1422 GNUNET_break (0 == cp->pending_requests);
1428 * Iterator over hash map entries that removes all occurences
1429 * of the given 'client' from the 'last_client_replies' of the
1430 * given connected peer.
1432 * @param cls closure, the 'struct GNUNET_SERVER_Client*' to remove
1433 * @param key current key code (unused)
1434 * @param value value in the hash map (the 'struct ConnectedPeer*' to change)
1435 * @return GNUNET_YES (we should continue to iterate)
1438 remove_client_from_last_client_replies (void *cls,
1439 const GNUNET_HashCode * key,
1442 struct GNUNET_SERVER_Client *client = cls;
1443 struct ConnectedPeer *cp = value;
1446 for (i=0;i<CS2P_SUCCESS_LIST_SIZE;i++)
1448 if (cp->last_client_replies[i] == client)
1450 GNUNET_SERVER_client_drop (cp->last_client_replies[i]);
1451 cp->last_client_replies[i] = NULL;
1459 * A client disconnected. Remove all of its pending queries.
1461 * @param cls closure, NULL
1462 * @param client identification of the client
1465 handle_client_disconnect (void *cls,
1466 struct GNUNET_SERVER_Client
1469 struct ClientList *pos;
1470 struct ClientList *prev;
1471 struct ClientRequestList *rcl;
1472 struct ClientResponseMessage *creply;
1478 while ( (NULL != pos) &&
1479 (pos->client != client) )
1485 return; /* no requests pending for this client */
1486 while (NULL != (rcl = pos->rl_head))
1488 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1489 "Destroying pending request `%s' on disconnect\n",
1490 GNUNET_h2s (&rcl->req->query));
1491 destroy_pending_request (rcl->req);
1494 client_list = pos->next;
1496 prev->next = pos->next;
1497 if (pos->th != NULL)
1499 GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th);
1502 while (NULL != (creply = pos->res_head))
1504 GNUNET_CONTAINER_DLL_remove (pos->res_head,
1507 GNUNET_free (creply);
1509 GNUNET_SERVER_client_drop (pos->client);
1511 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
1512 &remove_client_from_last_client_replies,
1518 * Iterator to free peer entries.
1520 * @param cls closure, unused
1521 * @param key current key code
1522 * @param value value in the hash map (peer entry)
1523 * @return GNUNET_YES (we should continue to iterate)
1526 clean_peer (void *cls,
1527 const GNUNET_HashCode * key,
1530 peer_disconnect_handler (NULL, (const struct GNUNET_PeerIdentity*) key);
1536 * Task run during shutdown.
1542 shutdown_task (void *cls,
1543 const struct GNUNET_SCHEDULER_TaskContext *tc)
1547 GNUNET_DATASTORE_cancel (mig_qe);
1550 if (GNUNET_SCHEDULER_NO_TASK != mig_task)
1552 GNUNET_SCHEDULER_cancel (sched, mig_task);
1553 mig_task = GNUNET_SCHEDULER_NO_TASK;
1555 while (client_list != NULL)
1556 handle_client_disconnect (NULL,
1557 client_list->client);
1558 cron_flush_trust (NULL, NULL);
1559 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
1562 GNUNET_break (0 == GNUNET_CONTAINER_heap_get_size (requests_by_expiration_heap));
1563 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
1564 requests_by_expiration_heap = 0;
1565 GNUNET_CONTAINER_multihashmap_destroy (connected_peers);
1566 connected_peers = NULL;
1567 GNUNET_break (0 == GNUNET_CONTAINER_multihashmap_size (query_request_map));
1568 GNUNET_CONTAINER_multihashmap_destroy (query_request_map);
1569 query_request_map = NULL;
1570 GNUNET_break (0 == GNUNET_CONTAINER_multihashmap_size (peer_request_map));
1571 GNUNET_CONTAINER_multihashmap_destroy (peer_request_map);
1572 peer_request_map = NULL;
1573 GNUNET_assert (NULL != core);
1574 GNUNET_CORE_disconnect (core);
1578 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1583 GNUNET_DATASTORE_disconnect (dsh,
1587 while (mig_head != NULL)
1588 delete_migration_block (mig_head);
1589 GNUNET_assert (0 == mig_size);
1590 GNUNET_BLOCK_context_destroy (block_ctx);
1592 GNUNET_CONFIGURATION_destroy (block_cfg);
1596 GNUNET_free_non_null (trustDirectory);
1597 trustDirectory = NULL;
1601 /* ******************* Utility functions ******************** */
1605 * Transmit messages by copying it to the target buffer
1606 * "buf". "buf" will be NULL and "size" zero if the socket was closed
1607 * for writing in the meantime. In that case, do nothing
1608 * (the disconnect or shutdown handler will take care of the rest).
1609 * If we were able to transmit messages and there are still more
1610 * pending, ask core again for further calls to this function.
1612 * @param cls closure, pointer to the 'struct ConnectedPeer*'
1613 * @param size number of bytes available in buf
1614 * @param buf where the callee should write the message
1615 * @return number of bytes written to buf
1618 transmit_to_peer (void *cls,
1619 size_t size, void *buf)
1621 struct ConnectedPeer *cp = cls;
1623 struct GNUNET_PeerIdentity pid;
1624 struct PendingMessage *pm;
1625 struct MigrationReadyBlock *mb;
1626 struct MigrationReadyBlock *next;
1627 struct PutMessage migm;
1635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1636 "Dropping message, core too busy.\n");
1641 while ( (NULL != (pm = cp->pending_messages_head) ) &&
1642 (pm->msize <= size) )
1644 memcpy (&cbuf[msize], &pm[1], pm->msize);
1647 destroy_pending_message (pm, cp->pid);
1651 GNUNET_PEER_resolve (cp->pid,
1653 cp->cth = GNUNET_CORE_notify_transmit_ready (core,
1655 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1664 while (NULL != (mb = next))
1667 for (i=0;i<MIGRATION_LIST_SIZE;i++)
1669 if ( (cp->pid == mb->target_list[i]) &&
1670 (mb->size + sizeof (migm) <= size) )
1672 GNUNET_PEER_change_rc (mb->target_list[i], -1);
1673 mb->target_list[i] = 0;
1675 memset (&migm, 0, sizeof (migm));
1676 migm.header.size = htons (sizeof (migm) + mb->size);
1677 migm.header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
1678 migm.type = htonl (mb->type);
1679 migm.expiration = GNUNET_TIME_absolute_hton (mb->expiration);
1680 memcpy (&cbuf[msize], &migm, sizeof (migm));
1681 msize += sizeof (migm);
1682 size -= sizeof (migm);
1683 memcpy (&cbuf[msize], &mb[1], mb->size);
1687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1688 "Pushing migration block `%s' (%u bytes) to `%s'\n",
1689 GNUNET_h2s (&mb->query),
1698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1699 "Migration block `%s' (%u bytes) is not on migration list for peer `%s'\n",
1700 GNUNET_h2s (&mb->query),
1706 if ( (mb->used_targets >= MIGRATION_TARGET_COUNT) ||
1707 (mb->used_targets >= GNUNET_CONTAINER_multihashmap_size (connected_peers)) )
1709 delete_migration_block (mb);
1710 consider_migration_gathering ();
1713 consider_migration (NULL,
1718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1719 "Transmitting %u bytes to peer %u\n",
1728 * Add a message to the set of pending messages for the given peer.
1730 * @param cp peer to send message to
1731 * @param pm message to queue
1732 * @param pr request on which behalf this message is being queued
1735 add_to_pending_messages_for_peer (struct ConnectedPeer *cp,
1736 struct PendingMessage *pm,
1737 struct PendingRequest *pr)
1739 struct PendingMessage *pos;
1740 struct PendingMessageList *pml;
1741 struct GNUNET_PeerIdentity pid;
1743 GNUNET_assert (pm->next == NULL);
1744 GNUNET_assert (pm->pml == NULL);
1745 pml = GNUNET_malloc (sizeof (struct PendingMessageList));
1750 GNUNET_CONTAINER_DLL_insert (pr->pending_head,
1753 pos = cp->pending_messages_head;
1754 while ( (pos != NULL) &&
1755 (pm->priority < pos->priority) )
1757 GNUNET_CONTAINER_DLL_insert_after (cp->pending_messages_head,
1758 cp->pending_messages_tail,
1761 cp->pending_requests++;
1762 if (cp->pending_requests > MAX_QUEUE_PER_PEER)
1763 destroy_pending_message (cp->pending_messages_tail, 0);
1764 GNUNET_PEER_resolve (cp->pid, &pid);
1765 if (NULL != cp->cth)
1766 GNUNET_CORE_notify_transmit_ready_cancel (cp->cth);
1767 /* need to schedule transmission */
1768 cp->cth = GNUNET_CORE_notify_transmit_ready (core,
1769 cp->pending_messages_head->priority,
1772 cp->pending_messages_head->msize,
1775 if (cp->cth == NULL)
1778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1779 "Failed to schedule transmission with core!\n");
1781 GNUNET_STATISTICS_update (stats,
1782 gettext_noop ("# CORE transmission failures"),
1790 * Test if the load on this peer is too high
1791 * to even consider processing the query at
1794 * @return GNUNET_YES if the load is too high to do anything, GNUNET_NO to forward (load high, but not too high), GNUNET_SYSERR to indirect (load low)
1797 test_load_too_high ()
1799 return GNUNET_SYSERR; // FIXME
1803 /* ******************* Pending Request Refresh Task ******************** */
1808 * We use a random delay to make the timing of requests less
1809 * predictable. This function returns such a random delay. We add a base
1810 * delay of MAX_CORK_DELAY (1s).
1812 * FIXME: make schedule dependent on the specifics of the request?
1813 * Or bandwidth and number of connected peers and load?
1815 * @return random delay to use for some request, between 1s and 1000+TTL_DECREMENT ms
1817 static struct GNUNET_TIME_Relative
1818 get_processing_delay ()
1821 GNUNET_TIME_relative_add (GNUNET_CONSTANTS_MAX_CORK_DELAY,
1822 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1823 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1829 * We're processing a GET request from another peer and have decided
1830 * to forward it to other peers. This function is called periodically
1831 * and should forward the request to other peers until we have all
1832 * possible replies. If we have transmitted the *only* reply to
1833 * the initiator we should destroy the pending request. If we have
1834 * many replies in the queue to the initiator, we should delay sending
1835 * out more queries until the reply queue has shrunk some.
1837 * @param cls our "struct ProcessGetContext *"
1841 forward_request_task (void *cls,
1842 const struct GNUNET_SCHEDULER_TaskContext *tc);
1846 * Function called after we either failed or succeeded
1847 * at transmitting a query to a peer.
1849 * @param cls the requests "struct PendingRequest*"
1850 * @param tpid ID of receiving peer, 0 on transmission error
1853 transmit_query_continuation (void *cls,
1854 GNUNET_PEER_Id tpid)
1856 struct PendingRequest *pr = cls;
1858 GNUNET_STATISTICS_update (stats,
1859 gettext_noop ("# queries scheduled for forwarding"),
1865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1866 "Transmission of request failed, will try again later.\n");
1868 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
1869 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
1870 get_processing_delay (),
1871 &forward_request_task,
1875 GNUNET_STATISTICS_update (stats,
1876 gettext_noop ("# queries forwarded"),
1879 GNUNET_PEER_change_rc (tpid, 1);
1880 if (pr->used_pids_off == pr->used_pids_size)
1881 GNUNET_array_grow (pr->used_pids,
1883 pr->used_pids_size * 2 + 2);
1884 pr->used_pids[pr->used_pids_off++] = tpid;
1885 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
1886 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
1887 get_processing_delay (),
1888 &forward_request_task,
1894 * How many bytes should a bloomfilter be if we have already seen
1895 * entry_count responses? Note that BLOOMFILTER_K gives us the number
1896 * of bits set per entry. Furthermore, we should not re-size the
1897 * filter too often (to keep it cheap).
1899 * Since other peers will also add entries but not resize the filter,
1900 * we should generally pick a slightly larger size than what the
1901 * strict math would suggest.
1903 * @return must be a power of two and smaller or equal to 2^15.
1906 compute_bloomfilter_size (unsigned int entry_count)
1909 unsigned int ideal = (entry_count * BLOOMFILTER_K) / 4;
1910 uint16_t max = 1 << 15;
1912 if (entry_count > max)
1915 while ((size < max) && (size < ideal))
1924 * Recalculate our bloom filter for filtering replies. This function
1925 * will create a new bloom filter from scratch, so it should only be
1926 * called if we have no bloomfilter at all (and hence can create a
1927 * fresh one of minimal size without problems) OR if our peer is the
1928 * initiator (in which case we may resize to larger than mimimum size).
1930 * @param pr request for which the BF is to be recomputed
1933 refresh_bloomfilter (struct PendingRequest *pr)
1937 GNUNET_HashCode mhash;
1939 nsize = compute_bloomfilter_size (pr->replies_seen_off);
1940 if (nsize == pr->bf_size)
1941 return; /* size not changed */
1943 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
1944 pr->bf_size = nsize;
1945 pr->mingle = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, -1);
1946 pr->bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
1949 for (i=0;i<pr->replies_seen_off;i++)
1951 GNUNET_BLOCK_mingle_hash (&pr->replies_seen[i],
1954 GNUNET_CONTAINER_bloomfilter_add (pr->bf, &mhash);
1960 * Function called after we've tried to reserve a certain amount of
1961 * bandwidth for a reply. Check if we succeeded and if so send our
1964 * @param cls the requests "struct PendingRequest*"
1965 * @param peer identifies the peer
1966 * @param bpm_in set to the current bandwidth limit (receiving) for this peer
1967 * @param bpm_out set to the current bandwidth limit (sending) for this peer
1968 * @param amount set to the amount that was actually reserved or unreserved
1969 * @param preference current traffic preference for the given peer
1972 target_reservation_cb (void *cls,
1974 GNUNET_PeerIdentity * peer,
1975 struct GNUNET_BANDWIDTH_Value32NBO bpm_in,
1976 struct GNUNET_BANDWIDTH_Value32NBO bpm_out,
1978 uint64_t preference)
1980 struct PendingRequest *pr = cls;
1981 struct ConnectedPeer *cp;
1982 struct PendingMessage *pm;
1983 struct GetMessage *gm;
1984 GNUNET_HashCode *ext;
1994 /* error in communication with core, try again later */
1995 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
1996 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
1997 get_processing_delay (),
1998 &forward_request_task,
2002 // (3) transmit, update ttl/priority
2003 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
2007 /* Peer must have just left */
2009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2010 "Selected peer disconnected!\n");
2012 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
2013 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
2014 get_processing_delay (),
2015 &forward_request_task,
2019 no_route = GNUNET_NO;
2025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2026 "Failed to reserve bandwidth for reply (got %d/%u bytes only)!\n",
2030 GNUNET_STATISTICS_update (stats,
2031 gettext_noop ("# reply bandwidth reservation requests failed"),
2034 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
2035 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
2036 get_processing_delay (),
2037 &forward_request_task,
2039 return; /* this target round failed */
2041 /* FIXME: if we are "quite" busy, we may still want to skip
2042 this round; need more load detection code! */
2043 no_route = GNUNET_YES;
2046 GNUNET_STATISTICS_update (stats,
2047 gettext_noop ("# queries scheduled for forwarding"),
2050 /* build message and insert message into priority queue */
2052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2053 "Forwarding request `%s' to `%4s'!\n",
2054 GNUNET_h2s (&pr->query),
2059 if (GNUNET_YES == no_route)
2061 bm |= GET_MESSAGE_BIT_RETURN_TO;
2064 if (pr->namespace != NULL)
2066 bm |= GET_MESSAGE_BIT_SKS_NAMESPACE;
2069 if (pr->target_pid != 0)
2071 bm |= GET_MESSAGE_BIT_TRANSMIT_TO;
2074 msize = sizeof (struct GetMessage) + pr->bf_size + k * sizeof(GNUNET_HashCode);
2075 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
2076 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
2078 gm = (struct GetMessage*) &pm[1];
2079 gm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_GET);
2080 gm->header.size = htons (msize);
2081 gm->type = htonl (pr->type);
2082 pr->remaining_priority /= 2;
2083 gm->priority = htonl (pr->remaining_priority);
2084 gm->ttl = htonl (pr->ttl);
2085 gm->filter_mutator = htonl(pr->mingle);
2086 gm->hash_bitmap = htonl (bm);
2087 gm->query = pr->query;
2088 ext = (GNUNET_HashCode*) &gm[1];
2090 if (GNUNET_YES == no_route)
2091 GNUNET_PEER_resolve (pr->cp->pid, (struct GNUNET_PeerIdentity*) &ext[k++]);
2092 if (pr->namespace != NULL)
2093 memcpy (&ext[k++], pr->namespace, sizeof (GNUNET_HashCode));
2094 if (pr->target_pid != 0)
2095 GNUNET_PEER_resolve (pr->target_pid, (struct GNUNET_PeerIdentity*) &ext[k++]);
2096 bfdata = (char *) &ext[k];
2098 GNUNET_CONTAINER_bloomfilter_get_raw_data (pr->bf,
2101 pm->cont = &transmit_query_continuation;
2103 add_to_pending_messages_for_peer (cp, pm, pr);
2108 * Closure used for "target_peer_select_cb".
2110 struct PeerSelectionContext
2113 * The request for which we are selecting
2116 struct PendingRequest *pr;
2119 * Current "prime" target.
2121 struct GNUNET_PeerIdentity target;
2124 * How much do we like this target?
2126 double target_score;
2132 * Function called for each connected peer to determine
2133 * which one(s) would make good targets for forwarding.
2135 * @param cls closure (struct PeerSelectionContext)
2136 * @param key current key code (peer identity)
2137 * @param value value in the hash map (struct ConnectedPeer)
2138 * @return GNUNET_YES if we should continue to
2143 target_peer_select_cb (void *cls,
2144 const GNUNET_HashCode * key,
2147 struct PeerSelectionContext *psc = cls;
2148 struct ConnectedPeer *cp = value;
2149 struct PendingRequest *pr = psc->pr;
2154 /* 1) check that this peer is not the initiator */
2158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2159 "Skipping initiator in forwarding selection\n");
2161 return GNUNET_YES; /* skip */
2164 /* 2) check if we have already (recently) forwarded to this peer */
2166 for (i=0;i<pr->used_pids_off;i++)
2167 if (pr->used_pids[i] == cp->pid)
2170 if (0 != GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2171 RETRY_PROBABILITY_INV))
2174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2175 "NOT re-trying query that was previously transmitted %u times\n",
2176 (unsigned int) pr->used_pids_off);
2178 return GNUNET_YES; /* skip */
2183 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2184 "Re-trying query that was previously transmitted %u times to this peer\n",
2187 /* 3) calculate how much we'd like to forward to this peer,
2188 starting with a random value that is strong enough
2189 to at least give any peer a chance sometimes
2190 (compared to the other factors that come later) */
2191 /* 3a) count successful (recent) routes from cp for same source */
2194 score = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2195 P2P_SUCCESS_LIST_SIZE);
2196 for (i=0;i<P2P_SUCCESS_LIST_SIZE;i++)
2197 if (cp->last_p2p_replies[i] == pr->cp->pid)
2198 score += 1; /* likely successful based on hot path */
2202 score = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2203 CS2P_SUCCESS_LIST_SIZE);
2204 for (i=0;i<CS2P_SUCCESS_LIST_SIZE;i++)
2205 if (cp->last_client_replies[i] == pr->client_request_list->client_list->client)
2206 score += 1; /* likely successful based on hot path */
2208 /* 3b) include latency */
2209 if (cp->avg_delay.value < 4 * TTL_DECREMENT)
2210 score += 1; /* likely fast based on latency */
2211 /* 3c) include priorities */
2212 if (cp->avg_priority <= pr->remaining_priority / 2.0)
2213 score += 1; /* likely successful based on priorities */
2214 /* 3d) penalize for queue size */
2215 score -= (2.0 * cp->pending_requests / (double) MAX_QUEUE_PER_PEER);
2216 /* 3e) include peer proximity */
2217 score -= (2.0 * (GNUNET_CRYPTO_hash_distance_u32 (key,
2218 &pr->query)) / (double) UINT32_MAX);
2219 /* 4) super-bonus for being the known target */
2220 if (pr->target_pid == cp->pid)
2222 /* store best-fit in closure */
2224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2225 "Peer `%s' gets score %f for forwarding query, max is %f\n",
2230 score++; /* avoid zero */
2231 if (score > psc->target_score)
2233 psc->target_score = score;
2234 psc->target.hashPubKey = *key;
2241 * The priority level imposes a bound on the maximum
2242 * value for the ttl that can be requested.
2244 * @param ttl_in requested ttl
2245 * @param prio given priority
2246 * @return ttl_in if ttl_in is below the limit,
2247 * otherwise the ttl-limit for the given priority
2250 bound_ttl (int32_t ttl_in, uint32_t prio)
2252 unsigned long long allowed;
2256 allowed = ((unsigned long long) prio) * TTL_DECREMENT / 1000;
2257 if (ttl_in > allowed)
2259 if (allowed >= (1 << 30))
2268 * We're processing a GET request and have decided
2269 * to forward it to other peers. This function is called periodically
2270 * and should forward the request to other peers until we have all
2271 * possible replies. If we have transmitted the *only* reply to
2272 * the initiator we should destroy the pending request. If we have
2273 * many replies in the queue to the initiator, we should delay sending
2274 * out more queries until the reply queue has shrunk some.
2276 * @param cls our "struct ProcessGetContext *"
2280 forward_request_task (void *cls,
2281 const struct GNUNET_SCHEDULER_TaskContext *tc)
2283 struct PendingRequest *pr = cls;
2284 struct PeerSelectionContext psc;
2285 struct ConnectedPeer *cp;
2286 struct GNUNET_TIME_Relative delay;
2288 pr->task = GNUNET_SCHEDULER_NO_TASK;
2289 if (pr->irc != NULL)
2292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2293 "Forwarding of query `%s' not attempted due to pending local lookup!\n",
2294 GNUNET_h2s (&pr->query));
2296 return; /* already pending */
2298 if (GNUNET_YES == pr->local_only)
2299 return; /* configured to not do P2P search */
2300 /* (1) select target */
2302 psc.target_score = -DBL_MAX;
2303 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
2304 &target_peer_select_cb,
2306 if (psc.target_score == -DBL_MAX)
2308 delay = get_processing_delay ();
2310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2311 "No peer selected for forwarding of query `%s', will try again in %llu ms!\n",
2312 GNUNET_h2s (&pr->query),
2315 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
2317 &forward_request_task,
2319 return; /* nobody selected */
2321 /* (3) update TTL/priority */
2322 if (pr->client_request_list != NULL)
2324 /* FIXME: use better algorithm!? */
2325 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2328 /* bound priority we use by priorities we see from other peers
2329 rounded up (must round up so that we can see non-zero
2330 priorities, but round up as little as possible to make it
2331 plausible that we forwarded another peers request) */
2332 if (pr->priority > current_priorities + 1.0)
2333 pr->priority = (uint32_t) current_priorities + 1.0;
2334 pr->ttl = bound_ttl (pr->ttl + TTL_DECREMENT * 2,
2337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2338 "Trying query `%s' with priority %u and TTL %d.\n",
2339 GNUNET_h2s (&pr->query),
2345 /* (3) reserve reply bandwidth */
2346 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
2347 &psc.target.hashPubKey);
2348 GNUNET_assert (NULL != cp);
2349 pr->irc = GNUNET_CORE_peer_change_preference (sched, cfg,
2351 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
2352 GNUNET_BANDWIDTH_value_init (UINT32_MAX),
2355 &target_reservation_cb,
2357 cp->inc_preference = 0;
2361 /* **************************** P2P PUT Handling ************************ */
2365 * Function called after we either failed or succeeded
2366 * at transmitting a reply to a peer.
2368 * @param cls the requests "struct PendingRequest*"
2369 * @param tpid ID of receiving peer, 0 on transmission error
2372 transmit_reply_continuation (void *cls,
2373 GNUNET_PEER_Id tpid)
2375 struct PendingRequest *pr = cls;
2379 case GNUNET_BLOCK_TYPE_DBLOCK:
2380 case GNUNET_BLOCK_TYPE_IBLOCK:
2381 /* only one reply expected, done with the request! */
2382 destroy_pending_request (pr);
2384 case GNUNET_BLOCK_TYPE_ANY:
2385 case GNUNET_BLOCK_TYPE_KBLOCK:
2386 case GNUNET_BLOCK_TYPE_SBLOCK:
2396 * Transmit the given message by copying it to the target buffer
2397 * "buf". "buf" will be NULL and "size" zero if the socket was closed
2398 * for writing in the meantime. In that case, do nothing
2399 * (the disconnect or shutdown handler will take care of the rest).
2400 * If we were able to transmit messages and there are still more
2401 * pending, ask core again for further calls to this function.
2403 * @param cls closure, pointer to the 'struct ClientList*'
2404 * @param size number of bytes available in buf
2405 * @param buf where the callee should write the message
2406 * @return number of bytes written to buf
2409 transmit_to_client (void *cls,
2410 size_t size, void *buf)
2412 struct ClientList *cl = cls;
2414 struct ClientResponseMessage *creply;
2421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2422 "Not sending reply, client communication problem.\n");
2427 while ( (NULL != (creply = cl->res_head) ) &&
2428 (creply->msize <= size) )
2430 memcpy (&cbuf[msize], &creply[1], creply->msize);
2431 msize += creply->msize;
2432 size -= creply->msize;
2433 GNUNET_CONTAINER_DLL_remove (cl->res_head,
2436 GNUNET_free (creply);
2439 cl->th = GNUNET_SERVER_notify_transmit_ready (cl->client,
2441 GNUNET_TIME_UNIT_FOREVER_REL,
2442 &transmit_to_client,
2445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2446 "Transmitted %u bytes to client\n",
2447 (unsigned int) msize);
2454 * Closure for "process_reply" function.
2456 struct ProcessReplyClosure
2459 * The data for the reply.
2464 * Who gave us this reply? NULL for local host.
2466 struct ConnectedPeer *sender;
2469 * When the reply expires.
2471 struct GNUNET_TIME_Absolute expiration;
2479 * Namespace that this reply belongs to
2480 * (if it is of type SBLOCK).
2482 GNUNET_HashCode namespace;
2485 * Type of the block.
2487 enum GNUNET_BLOCK_Type type;
2490 * How much was this reply worth to us?
2495 * Did we finish processing the associated request?
2502 * We have received a reply; handle it!
2504 * @param cls response (struct ProcessReplyClosure)
2505 * @param key our query
2506 * @param value value in the hash map (info about the query)
2507 * @return GNUNET_YES (we should continue to iterate)
2510 process_reply (void *cls,
2511 const GNUNET_HashCode * key,
2514 struct ProcessReplyClosure *prq = cls;
2515 struct PendingRequest *pr = value;
2516 struct PendingMessage *reply;
2517 struct ClientResponseMessage *creply;
2518 struct ClientList *cl;
2519 struct PutMessage *pm;
2520 struct ConnectedPeer *cp;
2521 struct GNUNET_TIME_Relative cur_delay;
2522 enum GNUNET_BLOCK_EvaluationResult eval;
2526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2527 "Matched result (type %u) for query `%s' with pending request\n",
2528 (unsigned int) prq->type,
2531 GNUNET_STATISTICS_update (stats,
2532 gettext_noop ("# replies received and matched"),
2535 if (prq->sender != NULL)
2537 /* FIXME: should we be more precise here and not use
2538 "start_time" but a peer-specific time stamp? */
2539 cur_delay = GNUNET_TIME_absolute_get_duration (pr->start_time);
2540 prq->sender->avg_delay.value
2541 = (prq->sender->avg_delay.value *
2542 (RUNAVG_DELAY_N - 1) + cur_delay.value) / RUNAVG_DELAY_N;
2543 prq->sender->avg_priority
2544 = (prq->sender->avg_priority *
2545 (RUNAVG_DELAY_N - 1) + pr->priority) / (double) RUNAVG_DELAY_N;
2548 GNUNET_PEER_change_rc (prq->sender->last_p2p_replies
2549 [prq->sender->last_p2p_replies_woff % P2P_SUCCESS_LIST_SIZE],
2551 GNUNET_PEER_change_rc (pr->cp->pid, 1);
2552 prq->sender->last_p2p_replies
2553 [(prq->sender->last_p2p_replies_woff++) % P2P_SUCCESS_LIST_SIZE]
2558 if (NULL != prq->sender->last_client_replies
2559 [(prq->sender->last_client_replies_woff) % CS2P_SUCCESS_LIST_SIZE])
2560 GNUNET_SERVER_client_drop (prq->sender->last_client_replies
2561 [(prq->sender->last_client_replies_woff) % CS2P_SUCCESS_LIST_SIZE]);
2562 prq->sender->last_client_replies
2563 [(prq->sender->last_client_replies_woff++) % CS2P_SUCCESS_LIST_SIZE]
2564 = pr->client_request_list->client_list->client;
2565 GNUNET_SERVER_client_keep (pr->client_request_list->client_list->client);
2568 eval = GNUNET_BLOCK_evaluate (block_ctx,
2573 pr->namespace, (pr->namespace != NULL) ? sizeof (GNUNET_HashCode) : 0,
2578 case GNUNET_BLOCK_EVALUATION_OK_MORE:
2580 case GNUNET_BLOCK_EVALUATION_OK_LAST:
2581 while (NULL != pr->pending_head)
2582 destroy_pending_message_list_entry (pr->pending_head);
2585 if (pr->client_request_list != NULL)
2586 GNUNET_SERVER_receive_done (pr->client_request_list->client_list->client,
2588 GNUNET_DATASTORE_cancel (pr->qe);
2591 pr->do_remove = GNUNET_YES;
2592 if (pr->task != GNUNET_SCHEDULER_NO_TASK)
2594 GNUNET_SCHEDULER_cancel (sched,
2596 pr->task = GNUNET_SCHEDULER_NO_TASK;
2598 GNUNET_break (GNUNET_YES ==
2599 GNUNET_CONTAINER_multihashmap_remove (query_request_map,
2603 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
2604 GNUNET_STATISTICS_update (stats,
2605 gettext_noop ("# duplicate replies discarded (bloomfilter)"),
2609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2610 "Duplicate response `%s', discarding.\n",
2611 GNUNET_h2s (&mhash));
2613 return GNUNET_YES; /* duplicate */
2614 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
2615 return GNUNET_YES; /* wrong namespace */
2616 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
2619 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
2622 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
2623 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2624 _("Unsupported block type %u\n"),
2628 if (pr->client_request_list != NULL)
2630 if (pr->replies_seen_size == pr->replies_seen_off)
2631 GNUNET_array_grow (pr->replies_seen,
2632 pr->replies_seen_size,
2633 pr->replies_seen_size * 2 + 4);
2634 GNUNET_CRYPTO_hash (prq->data,
2636 &pr->replies_seen[pr->replies_seen_off++]);
2637 refresh_bloomfilter (pr);
2639 prq->priority += pr->remaining_priority;
2640 pr->remaining_priority = 0;
2641 if (NULL != pr->client_request_list)
2643 GNUNET_STATISTICS_update (stats,
2644 gettext_noop ("# replies received for local clients"),
2647 cl = pr->client_request_list->client_list;
2648 msize = sizeof (struct PutMessage) + prq->size;
2649 creply = GNUNET_malloc (msize + sizeof (struct ClientResponseMessage));
2650 creply->msize = msize;
2651 creply->client_list = cl;
2652 GNUNET_CONTAINER_DLL_insert_after (cl->res_head,
2656 pm = (struct PutMessage*) &creply[1];
2657 pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
2658 pm->header.size = htons (msize);
2659 pm->type = htonl (prq->type);
2660 pm->expiration = GNUNET_TIME_absolute_hton (prq->expiration);
2661 memcpy (&pm[1], prq->data, prq->size);
2665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2666 "Transmitting result for query `%s' to client\n",
2669 cl->th = GNUNET_SERVER_notify_transmit_ready (cl->client,
2671 GNUNET_TIME_UNIT_FOREVER_REL,
2672 &transmit_to_client,
2675 GNUNET_break (cl->th != NULL);
2678 prq->finished = GNUNET_YES;
2679 destroy_pending_request (pr);
2686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2687 "Transmitting result for query `%s' to other peer (PID=%u)\n",
2689 (unsigned int) cp->pid);
2691 GNUNET_STATISTICS_update (stats,
2692 gettext_noop ("# replies received for other peers"),
2695 msize = sizeof (struct PutMessage) + prq->size;
2696 reply = GNUNET_malloc (msize + sizeof (struct PendingMessage));
2697 reply->cont = &transmit_reply_continuation;
2698 reply->cont_cls = pr;
2699 reply->msize = msize;
2700 reply->priority = UINT32_MAX; /* send replies first! */
2701 pm = (struct PutMessage*) &reply[1];
2702 pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
2703 pm->header.size = htons (msize);
2704 pm->type = htonl (prq->type);
2705 pm->expiration = GNUNET_TIME_absolute_hton (prq->expiration);
2706 memcpy (&pm[1], prq->data, prq->size);
2707 add_to_pending_messages_for_peer (cp, reply, pr);
2714 * Continuation called to notify client about result of the
2717 * @param cls closure
2718 * @param success GNUNET_SYSERR on failure
2719 * @param msg NULL on success, otherwise an error message
2722 put_migration_continuation (void *cls,
2731 * Handle P2P "PUT" message.
2733 * @param cls closure, always NULL
2734 * @param other the other peer involved (sender or receiver, NULL
2735 * for loopback messages where we are both sender and receiver)
2736 * @param message the actual message
2737 * @param latency reported latency of the connection with 'other'
2738 * @param distance reported distance (DV) to 'other'
2739 * @return GNUNET_OK to keep the connection open,
2740 * GNUNET_SYSERR to close it (signal serious error)
2743 handle_p2p_put (void *cls,
2744 const struct GNUNET_PeerIdentity *other,
2745 const struct GNUNET_MessageHeader *message,
2746 struct GNUNET_TIME_Relative latency,
2749 const struct PutMessage *put;
2752 enum GNUNET_BLOCK_Type type;
2753 struct GNUNET_TIME_Absolute expiration;
2754 GNUNET_HashCode query;
2755 struct ProcessReplyClosure prq;
2756 const struct SBlock *sb;
2758 msize = ntohs (message->size);
2759 if (msize < sizeof (struct PutMessage))
2762 return GNUNET_SYSERR;
2764 put = (const struct PutMessage*) message;
2765 dsize = msize - sizeof (struct PutMessage);
2766 type = ntohl (put->type);
2767 expiration = GNUNET_TIME_absolute_ntoh (put->expiration);
2769 if (type == GNUNET_BLOCK_TYPE_ONDEMAND)
2770 return GNUNET_SYSERR;
2772 GNUNET_BLOCK_get_key (block_ctx,
2778 GNUNET_break_op (0);
2779 return GNUNET_SYSERR;
2781 if (GNUNET_BLOCK_TYPE_SBLOCK == type)
2783 sb = (const struct SBlock*) &put[1];
2784 GNUNET_CRYPTO_hash (&sb->subspace,
2785 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2791 "Received result for query `%s' from peer `%4s'\n",
2792 GNUNET_h2s (&query),
2793 GNUNET_i2s (other));
2795 GNUNET_STATISTICS_update (stats,
2796 gettext_noop ("# replies received (overall)"),
2799 /* now, lookup 'query' */
2800 prq.data = (const void*) &put[1];
2802 prq.sender = GNUNET_CONTAINER_multihashmap_get (connected_peers,
2803 &other->hashPubKey);
2808 prq.expiration = expiration;
2810 prq.finished = GNUNET_NO;
2811 GNUNET_CONTAINER_multihashmap_get_multiple (query_request_map,
2815 if (prq.sender != NULL)
2817 prq.sender->inc_preference += CONTENT_BANDWIDTH_VALUE + 1000 * prq.priority;
2818 prq.sender->trust += prq.priority;
2820 if (GNUNET_YES == active_migration)
2823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2824 "Replicating result for query `%s' with priority %u\n",
2825 GNUNET_h2s (&query),
2828 GNUNET_DATASTORE_put (dsh,
2829 0, &query, dsize, &put[1],
2830 type, prq.priority, 1 /* anonymity */,
2832 1 + prq.priority, MAX_DATASTORE_QUEUE,
2833 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
2834 &put_migration_continuation,
2841 /* **************************** P2P GET Handling ************************ */
2845 * Closure for 'check_duplicate_request_{peer,client}'.
2847 struct CheckDuplicateRequestClosure
2850 * The new request we should check if it already exists.
2852 const struct PendingRequest *pr;
2855 * Existing request found by the checker, NULL if none.
2857 struct PendingRequest *have;
2862 * Iterator over entries in the 'query_request_map' that
2863 * tries to see if we have the same request pending from
2864 * the same client already.
2866 * @param cls closure (our 'struct CheckDuplicateRequestClosure')
2867 * @param key current key code (query, ignored, must match)
2868 * @param value value in the hash map (a 'struct PendingRequest'
2869 * that already exists)
2870 * @return GNUNET_YES if we should continue to
2871 * iterate (no match yet)
2872 * GNUNET_NO if not (match found).
2875 check_duplicate_request_client (void *cls,
2876 const GNUNET_HashCode * key,
2879 struct CheckDuplicateRequestClosure *cdc = cls;
2880 struct PendingRequest *have = value;
2882 if (have->client_request_list == NULL)
2884 if ( (cdc->pr->client_request_list->client_list->client == have->client_request_list->client_list->client) &&
2895 * We're processing (local) results for a search request
2896 * from another peer. Pass applicable results to the
2897 * peer and if we are done either clean up (operation
2898 * complete) or forward to other peers (more results possible).
2900 * @param cls our closure (struct LocalGetContext)
2901 * @param key key for the content
2902 * @param size number of bytes in data
2903 * @param data content stored
2904 * @param type type of the content
2905 * @param priority priority of the content
2906 * @param anonymity anonymity-level for the content
2907 * @param expiration expiration time for the content
2908 * @param uid unique identifier for the datum;
2909 * maybe 0 if no unique identifier is available
2912 process_local_reply (void *cls,
2913 const GNUNET_HashCode * key,
2916 enum GNUNET_BLOCK_Type type,
2919 struct GNUNET_TIME_Absolute
2923 struct PendingRequest *pr = cls;
2924 struct ProcessReplyClosure prq;
2925 struct CheckDuplicateRequestClosure cdrc;
2926 const struct SBlock *sb;
2927 GNUNET_HashCode dhash;
2928 GNUNET_HashCode mhash;
2929 GNUNET_HashCode query;
2934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2935 "Done processing local replies, forwarding request to other peers.\n");
2938 if (pr->client_request_list != NULL)
2940 GNUNET_SERVER_receive_done (pr->client_request_list->client_list->client,
2942 /* Figure out if this is a duplicate request and possibly
2943 merge 'struct PendingRequest' entries */
2946 GNUNET_CONTAINER_multihashmap_get_multiple (query_request_map,
2948 &check_duplicate_request_client,
2950 if (cdrc.have != NULL)
2953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2954 "Received request for block `%s' twice from client, will only request once.\n",
2955 GNUNET_h2s (&pr->query));
2958 destroy_pending_request (pr);
2963 /* no more results */
2964 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
2965 pr->task = GNUNET_SCHEDULER_add_now (sched,
2966 &forward_request_task,
2971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2972 "New local response to `%s' of type %u.\n",
2976 if (type == GNUNET_BLOCK_TYPE_ONDEMAND)
2979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2980 "Found ONDEMAND block, performing on-demand encoding\n");
2982 GNUNET_STATISTICS_update (stats,
2983 gettext_noop ("# on-demand blocks matched requests"),
2987 GNUNET_FS_handle_on_demand_block (key, size, data, type, priority,
2988 anonymity, expiration, uid,
2989 &process_local_reply,
2992 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
2995 /* check for duplicates */
2996 GNUNET_CRYPTO_hash (data, size, &dhash);
2997 GNUNET_BLOCK_mingle_hash (&dhash,
3000 if ( (pr->bf != NULL) &&
3002 GNUNET_CONTAINER_bloomfilter_test (pr->bf,
3006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3007 "Result from datastore filtered by bloomfilter (duplicate).\n");
3009 GNUNET_STATISTICS_update (stats,
3010 gettext_noop ("# results filtered by query bloomfilter"),
3014 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
3018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3019 "Found result for query `%s' in local datastore\n",
3022 GNUNET_STATISTICS_update (stats,
3023 gettext_noop ("# results found locally"),
3026 pr->results_found++;
3027 memset (&prq, 0, sizeof (prq));
3029 prq.expiration = expiration;
3031 if (GNUNET_BLOCK_TYPE_SBLOCK == type)
3033 sb = (const struct SBlock*) data;
3034 GNUNET_CRYPTO_hash (&sb->subspace,
3035 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3039 GNUNET_BLOCK_get_key (block_ctx,
3046 GNUNET_DATASTORE_remove (dsh,
3050 GNUNET_TIME_UNIT_FOREVER_REL,
3052 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
3056 prq.priority = priority;
3057 prq.finished = GNUNET_NO;
3058 process_reply (&prq, key, pr);
3059 if (prq.finished == GNUNET_YES)
3062 return; /* done here */
3063 if ( (type == GNUNET_BLOCK_TYPE_DBLOCK) ||
3064 (type == GNUNET_BLOCK_TYPE_IBLOCK) )
3066 GNUNET_DATASTORE_get_next (dsh, GNUNET_NO);
3069 if ( (pr->client_request_list == NULL) &&
3070 ( (GNUNET_YES == test_load_too_high()) ||
3071 (pr->results_found > 5 + 2 * pr->priority) ) )
3074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3075 "Load too high, done with request\n");
3077 GNUNET_STATISTICS_update (stats,
3078 gettext_noop ("# processing result set cut short due to load"),
3081 GNUNET_DATASTORE_get_next (dsh, GNUNET_NO);
3084 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
3089 * We've received a request with the specified priority. Bound it
3090 * according to how much we trust the given peer.
3092 * @param prio_in requested priority
3093 * @param cp the peer making the request
3094 * @return effective priority
3097 bound_priority (uint32_t prio_in,
3098 struct ConnectedPeer *cp)
3100 #define N ((double)128.0)
3105 ld = test_load_too_high ();
3106 if (ld == GNUNET_SYSERR)
3107 return 0; /* excess resources */
3108 ret = change_host_trust (cp, prio_in);
3111 if (ret > current_priorities + N)
3112 rret = current_priorities + N;
3116 = (current_priorities * (N-1) + rret)/N;
3124 * Iterator over entries in the 'query_request_map' that
3125 * tries to see if we have the same request pending from
3126 * the same peer already.
3128 * @param cls closure (our 'struct CheckDuplicateRequestClosure')
3129 * @param key current key code (query, ignored, must match)
3130 * @param value value in the hash map (a 'struct PendingRequest'
3131 * that already exists)
3132 * @return GNUNET_YES if we should continue to
3133 * iterate (no match yet)
3134 * GNUNET_NO if not (match found).
3137 check_duplicate_request_peer (void *cls,
3138 const GNUNET_HashCode * key,
3141 struct CheckDuplicateRequestClosure *cdc = cls;
3142 struct PendingRequest *have = value;
3144 if (cdc->pr->target_pid == have->target_pid)
3154 * Handle P2P "GET" request.
3156 * @param cls closure, always NULL
3157 * @param other the other peer involved (sender or receiver, NULL
3158 * for loopback messages where we are both sender and receiver)
3159 * @param message the actual message
3160 * @param latency reported latency of the connection with 'other'
3161 * @param distance reported distance (DV) to 'other'
3162 * @return GNUNET_OK to keep the connection open,
3163 * GNUNET_SYSERR to close it (signal serious error)
3166 handle_p2p_get (void *cls,
3167 const struct GNUNET_PeerIdentity *other,
3168 const struct GNUNET_MessageHeader *message,
3169 struct GNUNET_TIME_Relative latency,
3172 struct PendingRequest *pr;
3173 struct ConnectedPeer *cp;
3174 struct ConnectedPeer *cps;
3175 struct CheckDuplicateRequestClosure cdc;
3176 struct GNUNET_TIME_Relative timeout;
3178 const struct GetMessage *gm;
3180 const GNUNET_HashCode *opt;
3183 uint32_t ttl_decrement;
3184 enum GNUNET_BLOCK_Type type;
3188 msize = ntohs(message->size);
3189 if (msize < sizeof (struct GetMessage))
3191 GNUNET_break_op (0);
3192 return GNUNET_SYSERR;
3194 gm = (const struct GetMessage*) message;
3195 type = ntohl (gm->type);
3196 bm = ntohl (gm->hash_bitmap);
3204 if (msize < sizeof (struct GetMessage) + bits * sizeof (GNUNET_HashCode))
3206 GNUNET_break_op (0);
3207 return GNUNET_SYSERR;
3209 opt = (const GNUNET_HashCode*) &gm[1];
3210 bfsize = msize - sizeof (struct GetMessage) + bits * sizeof (GNUNET_HashCode);
3211 bm = ntohl (gm->hash_bitmap);
3212 if ( (0 != (bm & GET_MESSAGE_BIT_SKS_NAMESPACE)) &&
3213 (type != GNUNET_BLOCK_TYPE_SBLOCK) )
3215 GNUNET_break_op (0);
3216 return GNUNET_SYSERR;
3219 cps = GNUNET_CONTAINER_multihashmap_get (connected_peers,
3220 &other->hashPubKey);
3223 /* peer must have just disconnected */
3224 GNUNET_STATISTICS_update (stats,
3225 gettext_noop ("# requests dropped due to initiator not being connected"),
3228 return GNUNET_SYSERR;
3230 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
3231 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
3238 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
3239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3240 "Failed to find RETURN-TO peer `%4s' in connection set. Dropping query.\n",
3241 GNUNET_i2s ((const struct GNUNET_PeerIdentity*) &opt[bits-1]));
3244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3245 "Failed to find peer `%4s' in connection set. Dropping query.\n",
3246 GNUNET_i2s (other));
3248 GNUNET_STATISTICS_update (stats,
3249 gettext_noop ("# requests dropped due to missing reverse route"),
3252 /* FIXME: try connect? */
3255 /* note that we can really only check load here since otherwise
3256 peers could find out that we are overloaded by not being
3257 disconnected after sending us a malformed query... */
3259 /* FIXME: query priority should play
3260 a major role here! */
3261 ld = test_load_too_high ();
3262 if (GNUNET_YES == ld)
3265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3266 "Dropping query from `%s', this peer is too busy.\n",
3267 GNUNET_i2s (other));
3269 GNUNET_STATISTICS_update (stats,
3270 gettext_noop ("# requests dropped due to high load"),
3275 /* FIXME: if ld == GNUNET_NO, forward
3276 instead of indirecting! */
3279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3280 "Received request for `%s' of type %u from peer `%4s' with flags %u\n",
3281 GNUNET_h2s (&gm->query),
3282 (unsigned int) type,
3286 have_ns = (0 != (bm & GET_MESSAGE_BIT_SKS_NAMESPACE));
3287 pr = GNUNET_malloc (sizeof (struct PendingRequest) +
3288 (have_ns ? sizeof(GNUNET_HashCode) : 0));
3291 pr->namespace = (GNUNET_HashCode*) &pr[1];
3292 memcpy (&pr[1], &opt[bits++], sizeof (GNUNET_HashCode));
3295 pr->mingle = ntohl (gm->filter_mutator);
3296 if (0 != (bm & GET_MESSAGE_BIT_TRANSMIT_TO))
3297 pr->target_pid = GNUNET_PEER_intern ((const struct GNUNET_PeerIdentity*) &opt[bits++]);
3299 pr->anonymity_level = 1;
3300 pr->priority = bound_priority (ntohl (gm->priority), cps);
3301 pr->ttl = bound_ttl (ntohl (gm->ttl), pr->priority);
3302 pr->query = gm->query;
3303 /* decrement ttl (always) */
3304 ttl_decrement = 2 * TTL_DECREMENT +
3305 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
3307 if ( (pr->ttl < 0) &&
3308 (((int32_t)(pr->ttl - ttl_decrement)) > 0) )
3311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3312 "Dropping query from `%s' due to TTL underflow (%d - %u).\n",
3317 GNUNET_STATISTICS_update (stats,
3318 gettext_noop ("# requests dropped due TTL underflow"),
3321 /* integer underflow => drop (should be very rare)! */
3325 pr->ttl -= ttl_decrement;
3326 pr->start_time = GNUNET_TIME_absolute_get ();
3328 /* get bloom filter */
3331 pr->bf = GNUNET_CONTAINER_bloomfilter_init ((const char*) &opt[bits],
3334 pr->bf_size = bfsize;
3339 GNUNET_CONTAINER_multihashmap_get_multiple (query_request_map,
3341 &check_duplicate_request_peer,
3343 if (cdc.have != NULL)
3345 if (cdc.have->start_time.value + cdc.have->ttl >=
3346 pr->start_time.value + pr->ttl)
3348 /* existing request has higher TTL, drop new one! */
3349 cdc.have->priority += pr->priority;
3350 destroy_pending_request (pr);
3352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3353 "Have existing request with higher TTL, dropping new request.\n",
3354 GNUNET_i2s (other));
3356 GNUNET_STATISTICS_update (stats,
3357 gettext_noop ("# requests dropped due to higher-TTL request"),
3364 /* existing request has lower TTL, drop old one! */
3365 pr->priority += cdc.have->priority;
3366 /* Possible optimization: if we have applicable pending
3367 replies in 'cdc.have', we might want to move those over
3368 (this is a really rare special-case, so it is not clear
3369 that this would be worth it) */
3370 destroy_pending_request (cdc.have);
3371 /* keep processing 'pr'! */
3376 GNUNET_break (GNUNET_OK ==
3377 GNUNET_CONTAINER_multihashmap_put (query_request_map,
3380 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
3381 GNUNET_break (GNUNET_OK ==
3382 GNUNET_CONTAINER_multihashmap_put (peer_request_map,
3385 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
3387 pr->hnode = GNUNET_CONTAINER_heap_insert (requests_by_expiration_heap,
3389 pr->start_time.value + pr->ttl);
3391 GNUNET_STATISTICS_update (stats,
3392 gettext_noop ("# P2P searches received"),
3395 GNUNET_STATISTICS_update (stats,
3396 gettext_noop ("# P2P searches active"),
3400 /* calculate change in traffic preference */
3401 cps->inc_preference += pr->priority * 1000 + QUERY_BANDWIDTH_VALUE;
3402 /* process locally */
3403 if (type == GNUNET_BLOCK_TYPE_DBLOCK)
3404 type = GNUNET_BLOCK_TYPE_ANY; /* to get on-demand as well */
3405 timeout = GNUNET_TIME_relative_multiply (BASIC_DATASTORE_REQUEST_DELAY,
3406 (pr->priority + 1));
3407 pr->qe = GNUNET_DATASTORE_get (dsh,
3411 MAX_DATASTORE_QUEUE,
3413 &process_local_reply,
3416 /* Are multiple results possible? If so, start processing remotely now! */
3419 case GNUNET_BLOCK_TYPE_DBLOCK:
3420 case GNUNET_BLOCK_TYPE_IBLOCK:
3421 /* only one result, wait for datastore */
3424 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
3425 pr->task = GNUNET_SCHEDULER_add_now (sched,
3426 &forward_request_task,
3430 /* make sure we don't track too many requests */
3431 if (GNUNET_CONTAINER_heap_get_size (requests_by_expiration_heap) > max_pending_requests)
3433 pr = GNUNET_CONTAINER_heap_peek (requests_by_expiration_heap);
3434 GNUNET_assert (pr != NULL);
3435 destroy_pending_request (pr);
3441 /* **************************** CS GET Handling ************************ */
3445 * Handle START_SEARCH-message (search request from client).
3447 * @param cls closure
3448 * @param client identification of the client
3449 * @param message the actual message
3452 handle_start_search (void *cls,
3453 struct GNUNET_SERVER_Client *client,
3454 const struct GNUNET_MessageHeader *message)
3456 static GNUNET_HashCode all_zeros;
3457 const struct SearchMessage *sm;
3458 struct ClientList *cl;
3459 struct ClientRequestList *crl;
3460 struct PendingRequest *pr;
3463 enum GNUNET_BLOCK_Type type;
3465 msize = ntohs (message->size);
3466 if ( (msize < sizeof (struct SearchMessage)) ||
3467 (0 != (msize - sizeof (struct SearchMessage)) % sizeof (GNUNET_HashCode)) )
3470 GNUNET_SERVER_receive_done (client,
3474 GNUNET_STATISTICS_update (stats,
3475 gettext_noop ("# client searches received"),
3478 sc = (msize - sizeof (struct SearchMessage)) / sizeof (GNUNET_HashCode);
3479 sm = (const struct SearchMessage*) message;
3480 type = ntohl (sm->type);
3482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3483 "Received request for `%s' of type %u from local client\n",
3484 GNUNET_h2s (&sm->query),
3485 (unsigned int) type);
3488 while ( (cl != NULL) &&
3489 (cl->client != client) )
3493 cl = GNUNET_malloc (sizeof (struct ClientList));
3494 cl->client = client;
3495 GNUNET_SERVER_client_keep (client);
3496 cl->next = client_list;
3499 /* detect duplicate KBLOCK requests */
3500 if ( (type == GNUNET_BLOCK_TYPE_KBLOCK) ||
3501 (type == GNUNET_BLOCK_TYPE_NBLOCK) ||
3502 (type == GNUNET_BLOCK_TYPE_ANY) )
3505 while ( (crl != NULL) &&
3506 ( (0 != memcmp (&crl->req->query,
3508 sizeof (GNUNET_HashCode))) ||
3509 (crl->req->type != type) ) )
3514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3515 "Have existing request, merging content-seen lists.\n");
3518 /* Duplicate request (used to send long list of
3519 known/blocked results); merge 'pr->replies_seen'
3520 and update bloom filter */
3521 GNUNET_array_grow (pr->replies_seen,
3522 pr->replies_seen_size,
3523 pr->replies_seen_off + sc);
3524 memcpy (&pr->replies_seen[pr->replies_seen_off],
3526 sc * sizeof (GNUNET_HashCode));
3527 pr->replies_seen_off += sc;
3528 refresh_bloomfilter (pr);
3529 GNUNET_STATISTICS_update (stats,
3530 gettext_noop ("# client searches updated (merged content seen list)"),
3533 GNUNET_SERVER_receive_done (client,
3538 GNUNET_STATISTICS_update (stats,
3539 gettext_noop ("# client searches active"),
3542 pr = GNUNET_malloc (sizeof (struct PendingRequest) +
3543 ((type == GNUNET_BLOCK_TYPE_SBLOCK) ? sizeof(GNUNET_HashCode) : 0));
3544 crl = GNUNET_malloc (sizeof (struct ClientRequestList));
3545 memset (crl, 0, sizeof (struct ClientRequestList));
3546 crl->client_list = cl;
3547 GNUNET_CONTAINER_DLL_insert (cl->rl_head,
3552 pr->client_request_list = crl;
3553 GNUNET_array_grow (pr->replies_seen,
3554 pr->replies_seen_size,
3556 memcpy (pr->replies_seen,
3558 sc * sizeof (GNUNET_HashCode));
3559 pr->replies_seen_off = sc;
3560 pr->anonymity_level = ntohl (sm->anonymity_level);
3561 refresh_bloomfilter (pr);
3562 pr->query = sm->query;
3563 if (0 == (1 & ntohl (sm->options)))
3564 pr->local_only = GNUNET_NO;
3566 pr->local_only = GNUNET_YES;
3569 case GNUNET_BLOCK_TYPE_DBLOCK:
3570 case GNUNET_BLOCK_TYPE_IBLOCK:
3571 if (0 != memcmp (&sm->target,
3573 sizeof (GNUNET_HashCode)))
3574 pr->target_pid = GNUNET_PEER_intern ((const struct GNUNET_PeerIdentity*) &sm->target);
3576 case GNUNET_BLOCK_TYPE_SBLOCK:
3577 pr->namespace = (GNUNET_HashCode*) &pr[1];
3578 memcpy (&pr[1], &sm->target, sizeof (GNUNET_HashCode));
3583 GNUNET_break (GNUNET_OK ==
3584 GNUNET_CONTAINER_multihashmap_put (query_request_map,
3587 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
3588 if (type == GNUNET_BLOCK_TYPE_DBLOCK)
3589 type = GNUNET_BLOCK_TYPE_ANY; /* get on-demand blocks too! */
3590 pr->qe = GNUNET_DATASTORE_get (dsh,
3594 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
3595 &process_local_reply,
3600 /* **************************** Startup ************************ */
3603 * Process fs requests.
3605 * @param s scheduler to use
3606 * @param server the initialized server
3607 * @param c configuration to use
3610 main_init (struct GNUNET_SCHEDULER_Handle *s,
3611 struct GNUNET_SERVER_Handle *server,
3612 const struct GNUNET_CONFIGURATION_Handle *c)
3614 static const struct GNUNET_CORE_MessageHandler p2p_handlers[] =
3617 GNUNET_MESSAGE_TYPE_FS_GET, 0 },
3619 GNUNET_MESSAGE_TYPE_FS_PUT, 0 },
3622 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
3623 {&GNUNET_FS_handle_index_start, NULL,
3624 GNUNET_MESSAGE_TYPE_FS_INDEX_START, 0},
3625 {&GNUNET_FS_handle_index_list_get, NULL,
3626 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_GET, sizeof(struct GNUNET_MessageHeader) },
3627 {&GNUNET_FS_handle_unindex, NULL, GNUNET_MESSAGE_TYPE_FS_UNINDEX,
3628 sizeof (struct UnindexMessage) },
3629 {&handle_start_search, NULL, GNUNET_MESSAGE_TYPE_FS_START_SEARCH,
3633 unsigned long long enc = 128;
3637 stats = GNUNET_STATISTICS_create (sched, "fs", cfg);
3638 min_migration_delay = GNUNET_TIME_UNIT_SECONDS;
3640 GNUNET_CONFIGURATION_get_value_number (cfg,
3642 "MAX_PENDING_REQUESTS",
3643 &max_pending_requests)) ||
3645 GNUNET_CONFIGURATION_get_value_number (cfg,
3647 "EXPECTED_NEIGHBOUR_COUNT",
3650 GNUNET_CONFIGURATION_get_value_time (cfg,
3652 "MIN_MIGRATION_DELAY",
3653 &min_migration_delay)) )
3655 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3656 _("Configuration fails to specify certain parameters, assuming default values."));
3658 connected_peers = GNUNET_CONTAINER_multihashmap_create (enc);
3659 query_request_map = GNUNET_CONTAINER_multihashmap_create (max_pending_requests);
3660 peer_request_map = GNUNET_CONTAINER_multihashmap_create (enc);
3661 requests_by_expiration_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
3662 core = GNUNET_CORE_connect (sched,
3664 GNUNET_TIME_UNIT_FOREVER_REL,
3667 &peer_connect_handler,
3668 &peer_disconnect_handler,
3675 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3676 _("Failed to connect to `%s' service.\n"),
3678 GNUNET_CONTAINER_multihashmap_destroy (connected_peers);
3679 connected_peers = NULL;
3680 GNUNET_CONTAINER_multihashmap_destroy (query_request_map);
3681 query_request_map = NULL;
3682 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
3683 requests_by_expiration_heap = NULL;
3684 GNUNET_CONTAINER_multihashmap_destroy (peer_request_map);
3685 peer_request_map = NULL;
3688 GNUNET_DATASTORE_disconnect (dsh, GNUNET_NO);
3691 return GNUNET_SYSERR;
3693 /* FIXME: distinguish between sending and storing in options? */
3694 if (active_migration)
3696 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3697 _("Content migration is enabled, will start to gather data\n"));
3698 consider_migration_gathering ();
3700 GNUNET_SERVER_disconnect_notify (server,
3701 &handle_client_disconnect,
3703 GNUNET_assert (GNUNET_OK ==
3704 GNUNET_CONFIGURATION_get_value_filename (cfg,
3708 GNUNET_DISK_directory_create (trustDirectory);
3709 GNUNET_SCHEDULER_add_with_priority (sched,
3710 GNUNET_SCHEDULER_PRIORITY_HIGH,
3711 &cron_flush_trust, NULL);
3714 GNUNET_SERVER_add_handlers (server, handlers);
3715 GNUNET_SCHEDULER_add_delayed (sched,
3716 GNUNET_TIME_UNIT_FOREVER_REL,
3724 * Process fs requests.
3726 * @param cls closure
3727 * @param sched scheduler to use
3728 * @param server the initialized server
3729 * @param cfg configuration to use
3733 struct GNUNET_SCHEDULER_Handle *sched,
3734 struct GNUNET_SERVER_Handle *server,
3735 const struct GNUNET_CONFIGURATION_Handle *cfg)
3737 active_migration = GNUNET_CONFIGURATION_get_value_yesno (cfg,
3740 dsh = GNUNET_DATASTORE_connect (cfg,
3744 GNUNET_SCHEDULER_shutdown (sched);
3747 block_cfg = GNUNET_CONFIGURATION_create ();
3748 GNUNET_CONFIGURATION_set_value_string (block_cfg,
3752 block_ctx = GNUNET_BLOCK_context_create (block_cfg);
3753 GNUNET_assert (NULL != block_ctx);
3754 if ( (GNUNET_OK != GNUNET_FS_indexing_init (sched, cfg, dsh)) ||
3755 (GNUNET_OK != main_init (sched, server, cfg)) )
3757 GNUNET_SCHEDULER_shutdown (sched);
3758 GNUNET_DATASTORE_disconnect (dsh, GNUNET_NO);
3760 GNUNET_BLOCK_context_destroy (block_ctx);
3762 GNUNET_CONFIGURATION_destroy (block_cfg);
3770 * The main function for the fs service.
3772 * @param argc number of arguments from the command line
3773 * @param argv command line arguments
3774 * @return 0 ok, 1 on error
3777 main (int argc, char *const *argv)
3779 return (GNUNET_OK ==
3780 GNUNET_SERVICE_run (argc,
3783 GNUNET_SERVICE_OPTION_NONE,
3784 &run, NULL)) ? 0 : 1;
3787 /* end of gnunet-service-fs.c */