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 * Mingle hash with the mingle_number to produce different bits.
1793 mingle_hash (const GNUNET_HashCode * in,
1794 int32_t mingle_number,
1795 GNUNET_HashCode * hc)
1799 GNUNET_CRYPTO_hash (&mingle_number,
1802 GNUNET_CRYPTO_hash_xor (&m, in, hc);
1807 * Test if the load on this peer is too high
1808 * to even consider processing the query at
1811 * @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)
1814 test_load_too_high ()
1816 return GNUNET_SYSERR; // FIXME
1820 /* ******************* Pending Request Refresh Task ******************** */
1825 * We use a random delay to make the timing of requests less
1826 * predictable. This function returns such a random delay. We add a base
1827 * delay of MAX_CORK_DELAY (1s).
1829 * FIXME: make schedule dependent on the specifics of the request?
1830 * Or bandwidth and number of connected peers and load?
1832 * @return random delay to use for some request, between 1s and 1000+TTL_DECREMENT ms
1834 static struct GNUNET_TIME_Relative
1835 get_processing_delay ()
1838 GNUNET_TIME_relative_add (GNUNET_CONSTANTS_MAX_CORK_DELAY,
1839 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1840 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1846 * We're processing a GET request from another peer and have decided
1847 * to forward it to other peers. This function is called periodically
1848 * and should forward the request to other peers until we have all
1849 * possible replies. If we have transmitted the *only* reply to
1850 * the initiator we should destroy the pending request. If we have
1851 * many replies in the queue to the initiator, we should delay sending
1852 * out more queries until the reply queue has shrunk some.
1854 * @param cls our "struct ProcessGetContext *"
1858 forward_request_task (void *cls,
1859 const struct GNUNET_SCHEDULER_TaskContext *tc);
1863 * Function called after we either failed or succeeded
1864 * at transmitting a query to a peer.
1866 * @param cls the requests "struct PendingRequest*"
1867 * @param tpid ID of receiving peer, 0 on transmission error
1870 transmit_query_continuation (void *cls,
1871 GNUNET_PEER_Id tpid)
1873 struct PendingRequest *pr = cls;
1875 GNUNET_STATISTICS_update (stats,
1876 gettext_noop ("# queries scheduled for forwarding"),
1882 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1883 "Transmission of request failed, will try again later.\n");
1885 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
1886 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
1887 get_processing_delay (),
1888 &forward_request_task,
1892 GNUNET_STATISTICS_update (stats,
1893 gettext_noop ("# queries forwarded"),
1896 GNUNET_PEER_change_rc (tpid, 1);
1897 if (pr->used_pids_off == pr->used_pids_size)
1898 GNUNET_array_grow (pr->used_pids,
1900 pr->used_pids_size * 2 + 2);
1901 pr->used_pids[pr->used_pids_off++] = tpid;
1902 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
1903 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
1904 get_processing_delay (),
1905 &forward_request_task,
1911 * How many bytes should a bloomfilter be if we have already seen
1912 * entry_count responses? Note that BLOOMFILTER_K gives us the number
1913 * of bits set per entry. Furthermore, we should not re-size the
1914 * filter too often (to keep it cheap).
1916 * Since other peers will also add entries but not resize the filter,
1917 * we should generally pick a slightly larger size than what the
1918 * strict math would suggest.
1920 * @return must be a power of two and smaller or equal to 2^15.
1923 compute_bloomfilter_size (unsigned int entry_count)
1926 unsigned int ideal = (entry_count * BLOOMFILTER_K) / 4;
1927 uint16_t max = 1 << 15;
1929 if (entry_count > max)
1932 while ((size < max) && (size < ideal))
1941 * Recalculate our bloom filter for filtering replies. This function
1942 * will create a new bloom filter from scratch, so it should only be
1943 * called if we have no bloomfilter at all (and hence can create a
1944 * fresh one of minimal size without problems) OR if our peer is the
1945 * initiator (in which case we may resize to larger than mimimum size).
1947 * @param pr request for which the BF is to be recomputed
1950 refresh_bloomfilter (struct PendingRequest *pr)
1954 GNUNET_HashCode mhash;
1956 nsize = compute_bloomfilter_size (pr->replies_seen_off);
1957 if (nsize == pr->bf_size)
1958 return; /* size not changed */
1960 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
1961 pr->bf_size = nsize;
1962 pr->mingle = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, -1);
1963 pr->bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
1966 for (i=0;i<pr->replies_seen_off;i++)
1968 mingle_hash (&pr->replies_seen[i], pr->mingle, &mhash);
1969 GNUNET_CONTAINER_bloomfilter_add (pr->bf, &mhash);
1975 * Function called after we've tried to reserve a certain amount of
1976 * bandwidth for a reply. Check if we succeeded and if so send our
1979 * @param cls the requests "struct PendingRequest*"
1980 * @param peer identifies the peer
1981 * @param bpm_in set to the current bandwidth limit (receiving) for this peer
1982 * @param bpm_out set to the current bandwidth limit (sending) for this peer
1983 * @param amount set to the amount that was actually reserved or unreserved
1984 * @param preference current traffic preference for the given peer
1987 target_reservation_cb (void *cls,
1989 GNUNET_PeerIdentity * peer,
1990 struct GNUNET_BANDWIDTH_Value32NBO bpm_in,
1991 struct GNUNET_BANDWIDTH_Value32NBO bpm_out,
1993 uint64_t preference)
1995 struct PendingRequest *pr = cls;
1996 struct ConnectedPeer *cp;
1997 struct PendingMessage *pm;
1998 struct GetMessage *gm;
1999 GNUNET_HashCode *ext;
2009 /* error in communication with core, try again later */
2010 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
2011 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
2012 get_processing_delay (),
2013 &forward_request_task,
2017 // (3) transmit, update ttl/priority
2018 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
2022 /* Peer must have just left */
2024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2025 "Selected peer disconnected!\n");
2027 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
2028 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
2029 get_processing_delay (),
2030 &forward_request_task,
2034 no_route = GNUNET_NO;
2040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2041 "Failed to reserve bandwidth for reply (got %d/%u bytes only)!\n",
2045 GNUNET_STATISTICS_update (stats,
2046 gettext_noop ("# reply bandwidth reservation requests failed"),
2049 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
2050 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
2051 get_processing_delay (),
2052 &forward_request_task,
2054 return; /* this target round failed */
2056 /* FIXME: if we are "quite" busy, we may still want to skip
2057 this round; need more load detection code! */
2058 no_route = GNUNET_YES;
2061 GNUNET_STATISTICS_update (stats,
2062 gettext_noop ("# queries scheduled for forwarding"),
2065 /* build message and insert message into priority queue */
2067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2068 "Forwarding request `%s' to `%4s'!\n",
2069 GNUNET_h2s (&pr->query),
2074 if (GNUNET_YES == no_route)
2076 bm |= GET_MESSAGE_BIT_RETURN_TO;
2079 if (pr->namespace != NULL)
2081 bm |= GET_MESSAGE_BIT_SKS_NAMESPACE;
2084 if (pr->target_pid != 0)
2086 bm |= GET_MESSAGE_BIT_TRANSMIT_TO;
2089 msize = sizeof (struct GetMessage) + pr->bf_size + k * sizeof(GNUNET_HashCode);
2090 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
2091 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
2093 gm = (struct GetMessage*) &pm[1];
2094 gm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_GET);
2095 gm->header.size = htons (msize);
2096 gm->type = htonl (pr->type);
2097 pr->remaining_priority /= 2;
2098 gm->priority = htonl (pr->remaining_priority);
2099 gm->ttl = htonl (pr->ttl);
2100 gm->filter_mutator = htonl(pr->mingle);
2101 gm->hash_bitmap = htonl (bm);
2102 gm->query = pr->query;
2103 ext = (GNUNET_HashCode*) &gm[1];
2105 if (GNUNET_YES == no_route)
2106 GNUNET_PEER_resolve (pr->cp->pid, (struct GNUNET_PeerIdentity*) &ext[k++]);
2107 if (pr->namespace != NULL)
2108 memcpy (&ext[k++], pr->namespace, sizeof (GNUNET_HashCode));
2109 if (pr->target_pid != 0)
2110 GNUNET_PEER_resolve (pr->target_pid, (struct GNUNET_PeerIdentity*) &ext[k++]);
2111 bfdata = (char *) &ext[k];
2113 GNUNET_CONTAINER_bloomfilter_get_raw_data (pr->bf,
2116 pm->cont = &transmit_query_continuation;
2118 add_to_pending_messages_for_peer (cp, pm, pr);
2123 * Closure used for "target_peer_select_cb".
2125 struct PeerSelectionContext
2128 * The request for which we are selecting
2131 struct PendingRequest *pr;
2134 * Current "prime" target.
2136 struct GNUNET_PeerIdentity target;
2139 * How much do we like this target?
2141 double target_score;
2147 * Function called for each connected peer to determine
2148 * which one(s) would make good targets for forwarding.
2150 * @param cls closure (struct PeerSelectionContext)
2151 * @param key current key code (peer identity)
2152 * @param value value in the hash map (struct ConnectedPeer)
2153 * @return GNUNET_YES if we should continue to
2158 target_peer_select_cb (void *cls,
2159 const GNUNET_HashCode * key,
2162 struct PeerSelectionContext *psc = cls;
2163 struct ConnectedPeer *cp = value;
2164 struct PendingRequest *pr = psc->pr;
2169 /* 1) check that this peer is not the initiator */
2173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2174 "Skipping initiator in forwarding selection\n");
2176 return GNUNET_YES; /* skip */
2179 /* 2) check if we have already (recently) forwarded to this peer */
2181 for (i=0;i<pr->used_pids_off;i++)
2182 if (pr->used_pids[i] == cp->pid)
2185 if (0 != GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2186 RETRY_PROBABILITY_INV))
2189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2190 "NOT re-trying query that was previously transmitted %u times\n",
2191 (unsigned int) pr->used_pids_off);
2193 return GNUNET_YES; /* skip */
2198 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2199 "Re-trying query that was previously transmitted %u times to this peer\n",
2202 /* 3) calculate how much we'd like to forward to this peer,
2203 starting with a random value that is strong enough
2204 to at least give any peer a chance sometimes
2205 (compared to the other factors that come later) */
2206 /* 3a) count successful (recent) routes from cp for same source */
2209 score = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2210 P2P_SUCCESS_LIST_SIZE);
2211 for (i=0;i<P2P_SUCCESS_LIST_SIZE;i++)
2212 if (cp->last_p2p_replies[i] == pr->cp->pid)
2213 score += 1; /* likely successful based on hot path */
2217 score = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2218 CS2P_SUCCESS_LIST_SIZE);
2219 for (i=0;i<CS2P_SUCCESS_LIST_SIZE;i++)
2220 if (cp->last_client_replies[i] == pr->client_request_list->client_list->client)
2221 score += 1; /* likely successful based on hot path */
2223 /* 3b) include latency */
2224 if (cp->avg_delay.value < 4 * TTL_DECREMENT)
2225 score += 1; /* likely fast based on latency */
2226 /* 3c) include priorities */
2227 if (cp->avg_priority <= pr->remaining_priority / 2.0)
2228 score += 1; /* likely successful based on priorities */
2229 /* 3d) penalize for queue size */
2230 score -= (2.0 * cp->pending_requests / (double) MAX_QUEUE_PER_PEER);
2231 /* 3e) include peer proximity */
2232 score -= (2.0 * (GNUNET_CRYPTO_hash_distance_u32 (key,
2233 &pr->query)) / (double) UINT32_MAX);
2234 /* 4) super-bonus for being the known target */
2235 if (pr->target_pid == cp->pid)
2237 /* store best-fit in closure */
2239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2240 "Peer `%s' gets score %f for forwarding query, max is %f\n",
2245 score++; /* avoid zero */
2246 if (score > psc->target_score)
2248 psc->target_score = score;
2249 psc->target.hashPubKey = *key;
2256 * The priority level imposes a bound on the maximum
2257 * value for the ttl that can be requested.
2259 * @param ttl_in requested ttl
2260 * @param prio given priority
2261 * @return ttl_in if ttl_in is below the limit,
2262 * otherwise the ttl-limit for the given priority
2265 bound_ttl (int32_t ttl_in, uint32_t prio)
2267 unsigned long long allowed;
2271 allowed = ((unsigned long long) prio) * TTL_DECREMENT / 1000;
2272 if (ttl_in > allowed)
2274 if (allowed >= (1 << 30))
2283 * We're processing a GET request and have decided
2284 * to forward it to other peers. This function is called periodically
2285 * and should forward the request to other peers until we have all
2286 * possible replies. If we have transmitted the *only* reply to
2287 * the initiator we should destroy the pending request. If we have
2288 * many replies in the queue to the initiator, we should delay sending
2289 * out more queries until the reply queue has shrunk some.
2291 * @param cls our "struct ProcessGetContext *"
2295 forward_request_task (void *cls,
2296 const struct GNUNET_SCHEDULER_TaskContext *tc)
2298 struct PendingRequest *pr = cls;
2299 struct PeerSelectionContext psc;
2300 struct ConnectedPeer *cp;
2301 struct GNUNET_TIME_Relative delay;
2303 pr->task = GNUNET_SCHEDULER_NO_TASK;
2304 if (pr->irc != NULL)
2307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2308 "Forwarding of query `%s' not attempted due to pending local lookup!\n",
2309 GNUNET_h2s (&pr->query));
2311 return; /* already pending */
2313 if (GNUNET_YES == pr->local_only)
2314 return; /* configured to not do P2P search */
2315 /* (1) select target */
2317 psc.target_score = -DBL_MAX;
2318 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
2319 &target_peer_select_cb,
2321 if (psc.target_score == -DBL_MAX)
2323 delay = get_processing_delay ();
2325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2326 "No peer selected for forwarding of query `%s', will try again in %llu ms!\n",
2327 GNUNET_h2s (&pr->query),
2330 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
2332 &forward_request_task,
2334 return; /* nobody selected */
2336 /* (3) update TTL/priority */
2337 if (pr->client_request_list != NULL)
2339 /* FIXME: use better algorithm!? */
2340 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2343 /* bound priority we use by priorities we see from other peers
2344 rounded up (must round up so that we can see non-zero
2345 priorities, but round up as little as possible to make it
2346 plausible that we forwarded another peers request) */
2347 if (pr->priority > current_priorities + 1.0)
2348 pr->priority = (uint32_t) current_priorities + 1.0;
2349 pr->ttl = bound_ttl (pr->ttl + TTL_DECREMENT * 2,
2352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2353 "Trying query `%s' with priority %u and TTL %d.\n",
2354 GNUNET_h2s (&pr->query),
2360 /* (3) reserve reply bandwidth */
2361 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
2362 &psc.target.hashPubKey);
2363 GNUNET_assert (NULL != cp);
2364 pr->irc = GNUNET_CORE_peer_change_preference (sched, cfg,
2366 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
2367 GNUNET_BANDWIDTH_value_init (UINT32_MAX),
2370 &target_reservation_cb,
2372 cp->inc_preference = 0;
2376 /* **************************** P2P PUT Handling ************************ */
2380 * Function called after we either failed or succeeded
2381 * at transmitting a reply to a peer.
2383 * @param cls the requests "struct PendingRequest*"
2384 * @param tpid ID of receiving peer, 0 on transmission error
2387 transmit_reply_continuation (void *cls,
2388 GNUNET_PEER_Id tpid)
2390 struct PendingRequest *pr = cls;
2394 case GNUNET_BLOCK_TYPE_DBLOCK:
2395 case GNUNET_BLOCK_TYPE_IBLOCK:
2396 /* only one reply expected, done with the request! */
2397 destroy_pending_request (pr);
2399 case GNUNET_BLOCK_TYPE_ANY:
2400 case GNUNET_BLOCK_TYPE_KBLOCK:
2401 case GNUNET_BLOCK_TYPE_SBLOCK:
2411 * Transmit the given message by copying it to the target buffer
2412 * "buf". "buf" will be NULL and "size" zero if the socket was closed
2413 * for writing in the meantime. In that case, do nothing
2414 * (the disconnect or shutdown handler will take care of the rest).
2415 * If we were able to transmit messages and there are still more
2416 * pending, ask core again for further calls to this function.
2418 * @param cls closure, pointer to the 'struct ClientList*'
2419 * @param size number of bytes available in buf
2420 * @param buf where the callee should write the message
2421 * @return number of bytes written to buf
2424 transmit_to_client (void *cls,
2425 size_t size, void *buf)
2427 struct ClientList *cl = cls;
2429 struct ClientResponseMessage *creply;
2436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2437 "Not sending reply, client communication problem.\n");
2442 while ( (NULL != (creply = cl->res_head) ) &&
2443 (creply->msize <= size) )
2445 memcpy (&cbuf[msize], &creply[1], creply->msize);
2446 msize += creply->msize;
2447 size -= creply->msize;
2448 GNUNET_CONTAINER_DLL_remove (cl->res_head,
2451 GNUNET_free (creply);
2454 cl->th = GNUNET_SERVER_notify_transmit_ready (cl->client,
2456 GNUNET_TIME_UNIT_FOREVER_REL,
2457 &transmit_to_client,
2460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2461 "Transmitted %u bytes to client\n",
2462 (unsigned int) msize);
2469 * Closure for "process_reply" function.
2471 struct ProcessReplyClosure
2474 * The data for the reply.
2479 * Who gave us this reply? NULL for local host.
2481 struct ConnectedPeer *sender;
2484 * When the reply expires.
2486 struct GNUNET_TIME_Absolute expiration;
2494 * Namespace that this reply belongs to
2495 * (if it is of type SBLOCK).
2497 GNUNET_HashCode namespace;
2500 * Type of the block.
2502 enum GNUNET_BLOCK_Type type;
2505 * How much was this reply worth to us?
2510 * Did we finish processing the associated request?
2517 * We have received a reply; handle it!
2519 * @param cls response (struct ProcessReplyClosure)
2520 * @param key our query
2521 * @param value value in the hash map (info about the query)
2522 * @return GNUNET_YES (we should continue to iterate)
2525 process_reply (void *cls,
2526 const GNUNET_HashCode * key,
2529 struct ProcessReplyClosure *prq = cls;
2530 struct PendingRequest *pr = value;
2531 struct PendingMessage *reply;
2532 struct ClientResponseMessage *creply;
2533 struct ClientList *cl;
2534 struct PutMessage *pm;
2535 struct ConnectedPeer *cp;
2536 struct GNUNET_TIME_Relative cur_delay;
2537 GNUNET_HashCode chash;
2538 GNUNET_HashCode mhash;
2542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2543 "Matched result (type %u) for query `%s' with pending request\n",
2544 (unsigned int) prq->type,
2547 GNUNET_STATISTICS_update (stats,
2548 gettext_noop ("# replies received and matched"),
2551 if (prq->sender != NULL)
2553 /* FIXME: should we be more precise here and not use
2554 "start_time" but a peer-specific time stamp? */
2555 cur_delay = GNUNET_TIME_absolute_get_duration (pr->start_time);
2556 prq->sender->avg_delay.value
2557 = (prq->sender->avg_delay.value *
2558 (RUNAVG_DELAY_N - 1) + cur_delay.value) / RUNAVG_DELAY_N;
2559 prq->sender->avg_priority
2560 = (prq->sender->avg_priority *
2561 (RUNAVG_DELAY_N - 1) + pr->priority) / (double) RUNAVG_DELAY_N;
2564 GNUNET_PEER_change_rc (prq->sender->last_p2p_replies
2565 [prq->sender->last_p2p_replies_woff % P2P_SUCCESS_LIST_SIZE],
2567 GNUNET_PEER_change_rc (pr->cp->pid, 1);
2568 prq->sender->last_p2p_replies
2569 [(prq->sender->last_p2p_replies_woff++) % P2P_SUCCESS_LIST_SIZE]
2574 if (NULL != prq->sender->last_client_replies
2575 [(prq->sender->last_client_replies_woff) % CS2P_SUCCESS_LIST_SIZE])
2576 GNUNET_SERVER_client_drop (prq->sender->last_client_replies
2577 [(prq->sender->last_client_replies_woff) % CS2P_SUCCESS_LIST_SIZE]);
2578 prq->sender->last_client_replies
2579 [(prq->sender->last_client_replies_woff++) % CS2P_SUCCESS_LIST_SIZE]
2580 = pr->client_request_list->client_list->client;
2581 GNUNET_SERVER_client_keep (pr->client_request_list->client_list->client);
2584 GNUNET_CRYPTO_hash (prq->data,
2589 case GNUNET_BLOCK_TYPE_DBLOCK:
2590 case GNUNET_BLOCK_TYPE_IBLOCK:
2591 /* only possible reply, stop requesting! */
2592 while (NULL != pr->pending_head)
2593 destroy_pending_message_list_entry (pr->pending_head);
2596 if (pr->client_request_list != NULL)
2597 GNUNET_SERVER_receive_done (pr->client_request_list->client_list->client,
2599 GNUNET_DATASTORE_cancel (pr->qe);
2602 pr->do_remove = GNUNET_YES;
2603 if (pr->task != GNUNET_SCHEDULER_NO_TASK)
2605 GNUNET_SCHEDULER_cancel (sched,
2607 pr->task = GNUNET_SCHEDULER_NO_TASK;
2609 GNUNET_break (GNUNET_YES ==
2610 GNUNET_CONTAINER_multihashmap_remove (query_request_map,
2614 case GNUNET_BLOCK_TYPE_SBLOCK:
2615 if (pr->namespace == NULL)
2620 if (0 != memcmp (pr->namespace,
2622 sizeof (GNUNET_HashCode)))
2624 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2625 _("Reply mismatched in terms of namespace. Discarded.\n"));
2626 return GNUNET_YES; /* wrong namespace */
2628 /* then: fall-through! */
2629 case GNUNET_BLOCK_TYPE_KBLOCK:
2630 case GNUNET_BLOCK_TYPE_NBLOCK:
2633 mingle_hash (&chash, pr->mingle, &mhash);
2634 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (pr->bf,
2637 GNUNET_STATISTICS_update (stats,
2638 gettext_noop ("# duplicate replies discarded (bloomfilter)"),
2642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2643 "Duplicate response `%s', discarding.\n",
2644 GNUNET_h2s (&mhash));
2646 return GNUNET_YES; /* duplicate */
2649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2650 "New response `%s', adding to filter.\n",
2651 GNUNET_h2s (&mhash));
2654 if (pr->client_request_list != NULL)
2656 if (pr->replies_seen_size == pr->replies_seen_off)
2657 GNUNET_array_grow (pr->replies_seen,
2658 pr->replies_seen_size,
2659 pr->replies_seen_size * 2 + 4);
2660 pr->replies_seen[pr->replies_seen_off++] = chash;
2662 if ( (pr->bf == NULL) ||
2663 (pr->client_request_list != NULL) )
2664 refresh_bloomfilter (pr);
2665 GNUNET_CONTAINER_bloomfilter_add (pr->bf,
2672 prq->priority += pr->remaining_priority;
2673 pr->remaining_priority = 0;
2674 if (NULL != pr->client_request_list)
2676 GNUNET_STATISTICS_update (stats,
2677 gettext_noop ("# replies received for local clients"),
2680 cl = pr->client_request_list->client_list;
2681 msize = sizeof (struct PutMessage) + prq->size;
2682 creply = GNUNET_malloc (msize + sizeof (struct ClientResponseMessage));
2683 creply->msize = msize;
2684 creply->client_list = cl;
2685 GNUNET_CONTAINER_DLL_insert_after (cl->res_head,
2689 pm = (struct PutMessage*) &creply[1];
2690 pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
2691 pm->header.size = htons (msize);
2692 pm->type = htonl (prq->type);
2693 pm->expiration = GNUNET_TIME_absolute_hton (prq->expiration);
2694 memcpy (&pm[1], prq->data, prq->size);
2698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2699 "Transmitting result for query `%s' to client\n",
2702 cl->th = GNUNET_SERVER_notify_transmit_ready (cl->client,
2704 GNUNET_TIME_UNIT_FOREVER_REL,
2705 &transmit_to_client,
2708 GNUNET_break (cl->th != NULL);
2711 prq->finished = GNUNET_YES;
2712 destroy_pending_request (pr);
2719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2720 "Transmitting result for query `%s' to other peer (PID=%u)\n",
2722 (unsigned int) cp->pid);
2724 GNUNET_STATISTICS_update (stats,
2725 gettext_noop ("# replies received for other peers"),
2728 msize = sizeof (struct PutMessage) + prq->size;
2729 reply = GNUNET_malloc (msize + sizeof (struct PendingMessage));
2730 reply->cont = &transmit_reply_continuation;
2731 reply->cont_cls = pr;
2732 reply->msize = msize;
2733 reply->priority = UINT32_MAX; /* send replies first! */
2734 pm = (struct PutMessage*) &reply[1];
2735 pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
2736 pm->header.size = htons (msize);
2737 pm->type = htonl (prq->type);
2738 pm->expiration = GNUNET_TIME_absolute_hton (prq->expiration);
2739 memcpy (&pm[1], prq->data, prq->size);
2740 add_to_pending_messages_for_peer (cp, reply, pr);
2747 * Continuation called to notify client about result of the
2750 * @param cls closure
2751 * @param success GNUNET_SYSERR on failure
2752 * @param msg NULL on success, otherwise an error message
2755 put_migration_continuation (void *cls,
2764 * Handle P2P "PUT" message.
2766 * @param cls closure, always NULL
2767 * @param other the other peer involved (sender or receiver, NULL
2768 * for loopback messages where we are both sender and receiver)
2769 * @param message the actual message
2770 * @param latency reported latency of the connection with 'other'
2771 * @param distance reported distance (DV) to 'other'
2772 * @return GNUNET_OK to keep the connection open,
2773 * GNUNET_SYSERR to close it (signal serious error)
2776 handle_p2p_put (void *cls,
2777 const struct GNUNET_PeerIdentity *other,
2778 const struct GNUNET_MessageHeader *message,
2779 struct GNUNET_TIME_Relative latency,
2782 const struct PutMessage *put;
2785 enum GNUNET_BLOCK_Type type;
2786 struct GNUNET_TIME_Absolute expiration;
2787 GNUNET_HashCode query;
2788 struct ProcessReplyClosure prq;
2789 const struct SBlock *sb;
2791 msize = ntohs (message->size);
2792 if (msize < sizeof (struct PutMessage))
2795 return GNUNET_SYSERR;
2797 put = (const struct PutMessage*) message;
2798 dsize = msize - sizeof (struct PutMessage);
2799 type = ntohl (put->type);
2800 expiration = GNUNET_TIME_absolute_ntoh (put->expiration);
2802 if (type == GNUNET_BLOCK_TYPE_ONDEMAND)
2803 return GNUNET_SYSERR;
2805 GNUNET_BLOCK_get_key (block_ctx,
2811 GNUNET_break_op (0);
2812 return GNUNET_SYSERR;
2814 if (GNUNET_BLOCK_TYPE_SBLOCK == type)
2816 sb = (const struct SBlock*) &put[1];
2817 GNUNET_CRYPTO_hash (&sb->subspace,
2818 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2824 "Received result for query `%s' from peer `%4s'\n",
2825 GNUNET_h2s (&query),
2826 GNUNET_i2s (other));
2828 GNUNET_STATISTICS_update (stats,
2829 gettext_noop ("# replies received (overall)"),
2832 /* now, lookup 'query' */
2833 prq.data = (const void*) &put[1];
2835 prq.sender = GNUNET_CONTAINER_multihashmap_get (connected_peers,
2836 &other->hashPubKey);
2841 prq.expiration = expiration;
2843 prq.finished = GNUNET_NO;
2844 GNUNET_CONTAINER_multihashmap_get_multiple (query_request_map,
2848 if (prq.sender != NULL)
2850 prq.sender->inc_preference += CONTENT_BANDWIDTH_VALUE + 1000 * prq.priority;
2851 prq.sender->trust += prq.priority;
2853 if (GNUNET_YES == active_migration)
2856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2857 "Replicating result for query `%s' with priority %u\n",
2858 GNUNET_h2s (&query),
2861 GNUNET_DATASTORE_put (dsh,
2862 0, &query, dsize, &put[1],
2863 type, prq.priority, 1 /* anonymity */,
2865 1 + prq.priority, MAX_DATASTORE_QUEUE,
2866 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
2867 &put_migration_continuation,
2874 /* **************************** P2P GET Handling ************************ */
2878 * Closure for 'check_duplicate_request_{peer,client}'.
2880 struct CheckDuplicateRequestClosure
2883 * The new request we should check if it already exists.
2885 const struct PendingRequest *pr;
2888 * Existing request found by the checker, NULL if none.
2890 struct PendingRequest *have;
2895 * Iterator over entries in the 'query_request_map' that
2896 * tries to see if we have the same request pending from
2897 * the same client already.
2899 * @param cls closure (our 'struct CheckDuplicateRequestClosure')
2900 * @param key current key code (query, ignored, must match)
2901 * @param value value in the hash map (a 'struct PendingRequest'
2902 * that already exists)
2903 * @return GNUNET_YES if we should continue to
2904 * iterate (no match yet)
2905 * GNUNET_NO if not (match found).
2908 check_duplicate_request_client (void *cls,
2909 const GNUNET_HashCode * key,
2912 struct CheckDuplicateRequestClosure *cdc = cls;
2913 struct PendingRequest *have = value;
2915 if (have->client_request_list == NULL)
2917 if ( (cdc->pr->client_request_list->client_list->client == have->client_request_list->client_list->client) &&
2928 * We're processing (local) results for a search request
2929 * from another peer. Pass applicable results to the
2930 * peer and if we are done either clean up (operation
2931 * complete) or forward to other peers (more results possible).
2933 * @param cls our closure (struct LocalGetContext)
2934 * @param key key for the content
2935 * @param size number of bytes in data
2936 * @param data content stored
2937 * @param type type of the content
2938 * @param priority priority of the content
2939 * @param anonymity anonymity-level for the content
2940 * @param expiration expiration time for the content
2941 * @param uid unique identifier for the datum;
2942 * maybe 0 if no unique identifier is available
2945 process_local_reply (void *cls,
2946 const GNUNET_HashCode * key,
2949 enum GNUNET_BLOCK_Type type,
2952 struct GNUNET_TIME_Absolute
2956 struct PendingRequest *pr = cls;
2957 struct ProcessReplyClosure prq;
2958 struct CheckDuplicateRequestClosure cdrc;
2959 const struct SBlock *sb;
2960 GNUNET_HashCode dhash;
2961 GNUNET_HashCode mhash;
2962 GNUNET_HashCode query;
2967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2968 "Done processing local replies, forwarding request to other peers.\n");
2971 if (pr->client_request_list != NULL)
2973 GNUNET_SERVER_receive_done (pr->client_request_list->client_list->client,
2975 /* Figure out if this is a duplicate request and possibly
2976 merge 'struct PendingRequest' entries */
2979 GNUNET_CONTAINER_multihashmap_get_multiple (query_request_map,
2981 &check_duplicate_request_client,
2983 if (cdrc.have != NULL)
2986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2987 "Received request for block `%s' twice from client, will only request once.\n",
2988 GNUNET_h2s (&pr->query));
2991 destroy_pending_request (pr);
2996 /* no more results */
2997 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
2998 pr->task = GNUNET_SCHEDULER_add_now (sched,
2999 &forward_request_task,
3004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3005 "New local response to `%s' of type %u.\n",
3009 if (type == GNUNET_BLOCK_TYPE_ONDEMAND)
3012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3013 "Found ONDEMAND block, performing on-demand encoding\n");
3015 GNUNET_STATISTICS_update (stats,
3016 gettext_noop ("# on-demand blocks matched requests"),
3020 GNUNET_FS_handle_on_demand_block (key, size, data, type, priority,
3021 anonymity, expiration, uid,
3022 &process_local_reply,
3025 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
3028 /* check for duplicates */
3029 GNUNET_CRYPTO_hash (data, size, &dhash);
3030 mingle_hash (&dhash,
3033 if ( (pr->bf != NULL) &&
3035 GNUNET_CONTAINER_bloomfilter_test (pr->bf,
3039 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3040 "Result from datastore filtered by bloomfilter (duplicate).\n");
3042 GNUNET_STATISTICS_update (stats,
3043 gettext_noop ("# results filtered by query bloomfilter"),
3047 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
3051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3052 "Found result for query `%s' in local datastore\n",
3055 GNUNET_STATISTICS_update (stats,
3056 gettext_noop ("# results found locally"),
3059 pr->results_found++;
3060 memset (&prq, 0, sizeof (prq));
3062 prq.expiration = expiration;
3064 if (GNUNET_BLOCK_TYPE_SBLOCK == type)
3066 sb = (const struct SBlock*) data;
3067 GNUNET_CRYPTO_hash (&sb->subspace,
3068 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3072 GNUNET_BLOCK_get_key (block_ctx,
3079 GNUNET_DATASTORE_remove (dsh,
3083 GNUNET_TIME_UNIT_FOREVER_REL,
3085 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
3089 prq.priority = priority;
3090 prq.finished = GNUNET_NO;
3091 process_reply (&prq, key, pr);
3092 if (prq.finished == GNUNET_YES)
3095 return; /* done here */
3096 if ( (type == GNUNET_BLOCK_TYPE_DBLOCK) ||
3097 (type == GNUNET_BLOCK_TYPE_IBLOCK) )
3099 GNUNET_DATASTORE_get_next (dsh, GNUNET_NO);
3102 if ( (pr->client_request_list == NULL) &&
3103 ( (GNUNET_YES == test_load_too_high()) ||
3104 (pr->results_found > 5 + 2 * pr->priority) ) )
3107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3108 "Load too high, done with request\n");
3110 GNUNET_STATISTICS_update (stats,
3111 gettext_noop ("# processing result set cut short due to load"),
3114 GNUNET_DATASTORE_get_next (dsh, GNUNET_NO);
3117 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
3122 * We've received a request with the specified priority. Bound it
3123 * according to how much we trust the given peer.
3125 * @param prio_in requested priority
3126 * @param cp the peer making the request
3127 * @return effective priority
3130 bound_priority (uint32_t prio_in,
3131 struct ConnectedPeer *cp)
3133 #define N ((double)128.0)
3138 ld = test_load_too_high ();
3139 if (ld == GNUNET_SYSERR)
3140 return 0; /* excess resources */
3141 ret = change_host_trust (cp, prio_in);
3144 if (ret > current_priorities + N)
3145 rret = current_priorities + N;
3149 = (current_priorities * (N-1) + rret)/N;
3157 * Iterator over entries in the 'query_request_map' that
3158 * tries to see if we have the same request pending from
3159 * the same peer already.
3161 * @param cls closure (our 'struct CheckDuplicateRequestClosure')
3162 * @param key current key code (query, ignored, must match)
3163 * @param value value in the hash map (a 'struct PendingRequest'
3164 * that already exists)
3165 * @return GNUNET_YES if we should continue to
3166 * iterate (no match yet)
3167 * GNUNET_NO if not (match found).
3170 check_duplicate_request_peer (void *cls,
3171 const GNUNET_HashCode * key,
3174 struct CheckDuplicateRequestClosure *cdc = cls;
3175 struct PendingRequest *have = value;
3177 if (cdc->pr->target_pid == have->target_pid)
3187 * Handle P2P "GET" request.
3189 * @param cls closure, always NULL
3190 * @param other the other peer involved (sender or receiver, NULL
3191 * for loopback messages where we are both sender and receiver)
3192 * @param message the actual message
3193 * @param latency reported latency of the connection with 'other'
3194 * @param distance reported distance (DV) to 'other'
3195 * @return GNUNET_OK to keep the connection open,
3196 * GNUNET_SYSERR to close it (signal serious error)
3199 handle_p2p_get (void *cls,
3200 const struct GNUNET_PeerIdentity *other,
3201 const struct GNUNET_MessageHeader *message,
3202 struct GNUNET_TIME_Relative latency,
3205 struct PendingRequest *pr;
3206 struct ConnectedPeer *cp;
3207 struct ConnectedPeer *cps;
3208 struct CheckDuplicateRequestClosure cdc;
3209 struct GNUNET_TIME_Relative timeout;
3211 const struct GetMessage *gm;
3213 const GNUNET_HashCode *opt;
3216 uint32_t ttl_decrement;
3217 enum GNUNET_BLOCK_Type type;
3221 msize = ntohs(message->size);
3222 if (msize < sizeof (struct GetMessage))
3224 GNUNET_break_op (0);
3225 return GNUNET_SYSERR;
3227 gm = (const struct GetMessage*) message;
3228 type = ntohl (gm->type);
3231 case GNUNET_BLOCK_TYPE_ANY:
3232 case GNUNET_BLOCK_TYPE_DBLOCK:
3233 case GNUNET_BLOCK_TYPE_IBLOCK:
3234 case GNUNET_BLOCK_TYPE_KBLOCK:
3235 case GNUNET_BLOCK_TYPE_SBLOCK:
3238 GNUNET_break_op (0);
3239 return GNUNET_SYSERR;
3241 bm = ntohl (gm->hash_bitmap);
3249 if (msize < sizeof (struct GetMessage) + bits * sizeof (GNUNET_HashCode))
3251 GNUNET_break_op (0);
3252 return GNUNET_SYSERR;
3254 opt = (const GNUNET_HashCode*) &gm[1];
3255 bfsize = msize - sizeof (struct GetMessage) + bits * sizeof (GNUNET_HashCode);
3256 bm = ntohl (gm->hash_bitmap);
3257 if ( (0 != (bm & GET_MESSAGE_BIT_SKS_NAMESPACE)) &&
3258 (type != GNUNET_BLOCK_TYPE_SBLOCK) )
3260 GNUNET_break_op (0);
3261 return GNUNET_SYSERR;
3264 cps = GNUNET_CONTAINER_multihashmap_get (connected_peers,
3265 &other->hashPubKey);
3268 /* peer must have just disconnected */
3269 GNUNET_STATISTICS_update (stats,
3270 gettext_noop ("# requests dropped due to initiator not being connected"),
3273 return GNUNET_SYSERR;
3275 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
3276 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
3283 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
3284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3285 "Failed to find RETURN-TO peer `%4s' in connection set. Dropping query.\n",
3286 GNUNET_i2s ((const struct GNUNET_PeerIdentity*) &opt[bits-1]));
3289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3290 "Failed to find peer `%4s' in connection set. Dropping query.\n",
3291 GNUNET_i2s (other));
3293 GNUNET_STATISTICS_update (stats,
3294 gettext_noop ("# requests dropped due to missing reverse route"),
3297 /* FIXME: try connect? */
3300 /* note that we can really only check load here since otherwise
3301 peers could find out that we are overloaded by not being
3302 disconnected after sending us a malformed query... */
3304 /* FIXME: query priority should play
3305 a major role here! */
3306 ld = test_load_too_high ();
3307 if (GNUNET_YES == ld)
3310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3311 "Dropping query from `%s', this peer is too busy.\n",
3312 GNUNET_i2s (other));
3314 GNUNET_STATISTICS_update (stats,
3315 gettext_noop ("# requests dropped due to high load"),
3320 /* FIXME: if ld == GNUNET_NO, forward
3321 instead of indirecting! */
3324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3325 "Received request for `%s' of type %u from peer `%4s' with flags %u\n",
3326 GNUNET_h2s (&gm->query),
3327 (unsigned int) type,
3331 have_ns = (0 != (bm & GET_MESSAGE_BIT_SKS_NAMESPACE));
3332 pr = GNUNET_malloc (sizeof (struct PendingRequest) +
3333 (have_ns ? sizeof(GNUNET_HashCode) : 0));
3336 pr->namespace = (GNUNET_HashCode*) &pr[1];
3337 memcpy (&pr[1], &opt[bits++], sizeof (GNUNET_HashCode));
3340 pr->mingle = ntohl (gm->filter_mutator);
3341 if (0 != (bm & GET_MESSAGE_BIT_TRANSMIT_TO))
3342 pr->target_pid = GNUNET_PEER_intern ((const struct GNUNET_PeerIdentity*) &opt[bits++]);
3344 pr->anonymity_level = 1;
3345 pr->priority = bound_priority (ntohl (gm->priority), cps);
3346 pr->ttl = bound_ttl (ntohl (gm->ttl), pr->priority);
3347 pr->query = gm->query;
3348 /* decrement ttl (always) */
3349 ttl_decrement = 2 * TTL_DECREMENT +
3350 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
3352 if ( (pr->ttl < 0) &&
3353 (((int32_t)(pr->ttl - ttl_decrement)) > 0) )
3356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3357 "Dropping query from `%s' due to TTL underflow (%d - %u).\n",
3362 GNUNET_STATISTICS_update (stats,
3363 gettext_noop ("# requests dropped due TTL underflow"),
3366 /* integer underflow => drop (should be very rare)! */
3370 pr->ttl -= ttl_decrement;
3371 pr->start_time = GNUNET_TIME_absolute_get ();
3373 /* get bloom filter */
3376 pr->bf = GNUNET_CONTAINER_bloomfilter_init ((const char*) &opt[bits],
3379 pr->bf_size = bfsize;
3384 GNUNET_CONTAINER_multihashmap_get_multiple (query_request_map,
3386 &check_duplicate_request_peer,
3388 if (cdc.have != NULL)
3390 if (cdc.have->start_time.value + cdc.have->ttl >=
3391 pr->start_time.value + pr->ttl)
3393 /* existing request has higher TTL, drop new one! */
3394 cdc.have->priority += pr->priority;
3395 destroy_pending_request (pr);
3397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3398 "Have existing request with higher TTL, dropping new request.\n",
3399 GNUNET_i2s (other));
3401 GNUNET_STATISTICS_update (stats,
3402 gettext_noop ("# requests dropped due to higher-TTL request"),
3409 /* existing request has lower TTL, drop old one! */
3410 pr->priority += cdc.have->priority;
3411 /* Possible optimization: if we have applicable pending
3412 replies in 'cdc.have', we might want to move those over
3413 (this is a really rare special-case, so it is not clear
3414 that this would be worth it) */
3415 destroy_pending_request (cdc.have);
3416 /* keep processing 'pr'! */
3421 GNUNET_break (GNUNET_OK ==
3422 GNUNET_CONTAINER_multihashmap_put (query_request_map,
3425 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
3426 GNUNET_break (GNUNET_OK ==
3427 GNUNET_CONTAINER_multihashmap_put (peer_request_map,
3430 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
3432 pr->hnode = GNUNET_CONTAINER_heap_insert (requests_by_expiration_heap,
3434 pr->start_time.value + pr->ttl);
3436 GNUNET_STATISTICS_update (stats,
3437 gettext_noop ("# P2P searches received"),
3440 GNUNET_STATISTICS_update (stats,
3441 gettext_noop ("# P2P searches active"),
3445 /* calculate change in traffic preference */
3446 cps->inc_preference += pr->priority * 1000 + QUERY_BANDWIDTH_VALUE;
3447 /* process locally */
3448 if (type == GNUNET_BLOCK_TYPE_DBLOCK)
3449 type = GNUNET_BLOCK_TYPE_ANY; /* to get on-demand as well */
3450 timeout = GNUNET_TIME_relative_multiply (BASIC_DATASTORE_REQUEST_DELAY,
3451 (pr->priority + 1));
3452 pr->qe = GNUNET_DATASTORE_get (dsh,
3456 MAX_DATASTORE_QUEUE,
3458 &process_local_reply,
3461 /* Are multiple results possible? If so, start processing remotely now! */
3464 case GNUNET_BLOCK_TYPE_DBLOCK:
3465 case GNUNET_BLOCK_TYPE_IBLOCK:
3466 /* only one result, wait for datastore */
3469 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
3470 pr->task = GNUNET_SCHEDULER_add_now (sched,
3471 &forward_request_task,
3475 /* make sure we don't track too many requests */
3476 if (GNUNET_CONTAINER_heap_get_size (requests_by_expiration_heap) > max_pending_requests)
3478 pr = GNUNET_CONTAINER_heap_peek (requests_by_expiration_heap);
3479 GNUNET_assert (pr != NULL);
3480 destroy_pending_request (pr);
3486 /* **************************** CS GET Handling ************************ */
3490 * Handle START_SEARCH-message (search request from client).
3492 * @param cls closure
3493 * @param client identification of the client
3494 * @param message the actual message
3497 handle_start_search (void *cls,
3498 struct GNUNET_SERVER_Client *client,
3499 const struct GNUNET_MessageHeader *message)
3501 static GNUNET_HashCode all_zeros;
3502 const struct SearchMessage *sm;
3503 struct ClientList *cl;
3504 struct ClientRequestList *crl;
3505 struct PendingRequest *pr;
3508 enum GNUNET_BLOCK_Type type;
3510 msize = ntohs (message->size);
3511 if ( (msize < sizeof (struct SearchMessage)) ||
3512 (0 != (msize - sizeof (struct SearchMessage)) % sizeof (GNUNET_HashCode)) )
3515 GNUNET_SERVER_receive_done (client,
3519 GNUNET_STATISTICS_update (stats,
3520 gettext_noop ("# client searches received"),
3523 sc = (msize - sizeof (struct SearchMessage)) / sizeof (GNUNET_HashCode);
3524 sm = (const struct SearchMessage*) message;
3525 type = ntohl (sm->type);
3527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3528 "Received request for `%s' of type %u from local client\n",
3529 GNUNET_h2s (&sm->query),
3530 (unsigned int) type);
3534 case GNUNET_BLOCK_TYPE_ANY:
3535 case GNUNET_BLOCK_TYPE_DBLOCK:
3536 case GNUNET_BLOCK_TYPE_IBLOCK:
3537 case GNUNET_BLOCK_TYPE_KBLOCK:
3538 case GNUNET_BLOCK_TYPE_SBLOCK:
3539 case GNUNET_BLOCK_TYPE_NBLOCK:
3543 GNUNET_SERVER_receive_done (client,
3549 while ( (cl != NULL) &&
3550 (cl->client != client) )
3554 cl = GNUNET_malloc (sizeof (struct ClientList));
3555 cl->client = client;
3556 GNUNET_SERVER_client_keep (client);
3557 cl->next = client_list;
3560 /* detect duplicate KBLOCK requests */
3561 if ( (type == GNUNET_BLOCK_TYPE_KBLOCK) ||
3562 (type == GNUNET_BLOCK_TYPE_NBLOCK) ||
3563 (type == GNUNET_BLOCK_TYPE_ANY) )
3566 while ( (crl != NULL) &&
3567 ( (0 != memcmp (&crl->req->query,
3569 sizeof (GNUNET_HashCode))) ||
3570 (crl->req->type != type) ) )
3575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3576 "Have existing request, merging content-seen lists.\n");
3579 /* Duplicate request (used to send long list of
3580 known/blocked results); merge 'pr->replies_seen'
3581 and update bloom filter */
3582 GNUNET_array_grow (pr->replies_seen,
3583 pr->replies_seen_size,
3584 pr->replies_seen_off + sc);
3585 memcpy (&pr->replies_seen[pr->replies_seen_off],
3587 sc * sizeof (GNUNET_HashCode));
3588 pr->replies_seen_off += sc;
3589 refresh_bloomfilter (pr);
3590 GNUNET_STATISTICS_update (stats,
3591 gettext_noop ("# client searches updated (merged content seen list)"),
3594 GNUNET_SERVER_receive_done (client,
3599 GNUNET_STATISTICS_update (stats,
3600 gettext_noop ("# client searches active"),
3603 pr = GNUNET_malloc (sizeof (struct PendingRequest) +
3604 ((type == GNUNET_BLOCK_TYPE_SBLOCK) ? sizeof(GNUNET_HashCode) : 0));
3605 crl = GNUNET_malloc (sizeof (struct ClientRequestList));
3606 memset (crl, 0, sizeof (struct ClientRequestList));
3607 crl->client_list = cl;
3608 GNUNET_CONTAINER_DLL_insert (cl->rl_head,
3613 pr->client_request_list = crl;
3614 GNUNET_array_grow (pr->replies_seen,
3615 pr->replies_seen_size,
3617 memcpy (pr->replies_seen,
3619 sc * sizeof (GNUNET_HashCode));
3620 pr->replies_seen_off = sc;
3621 pr->anonymity_level = ntohl (sm->anonymity_level);
3622 refresh_bloomfilter (pr);
3623 pr->query = sm->query;
3624 if (0 == (1 & ntohl (sm->options)))
3625 pr->local_only = GNUNET_NO;
3627 pr->local_only = GNUNET_YES;
3630 case GNUNET_BLOCK_TYPE_DBLOCK:
3631 case GNUNET_BLOCK_TYPE_IBLOCK:
3632 if (0 != memcmp (&sm->target,
3634 sizeof (GNUNET_HashCode)))
3635 pr->target_pid = GNUNET_PEER_intern ((const struct GNUNET_PeerIdentity*) &sm->target);
3637 case GNUNET_BLOCK_TYPE_SBLOCK:
3638 pr->namespace = (GNUNET_HashCode*) &pr[1];
3639 memcpy (&pr[1], &sm->target, sizeof (GNUNET_HashCode));
3644 GNUNET_break (GNUNET_OK ==
3645 GNUNET_CONTAINER_multihashmap_put (query_request_map,
3648 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
3649 if (type == GNUNET_BLOCK_TYPE_DBLOCK)
3650 type = GNUNET_BLOCK_TYPE_ANY; /* get on-demand blocks too! */
3651 pr->qe = GNUNET_DATASTORE_get (dsh,
3655 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
3656 &process_local_reply,
3661 /* **************************** Startup ************************ */
3664 * Process fs requests.
3666 * @param s scheduler to use
3667 * @param server the initialized server
3668 * @param c configuration to use
3671 main_init (struct GNUNET_SCHEDULER_Handle *s,
3672 struct GNUNET_SERVER_Handle *server,
3673 const struct GNUNET_CONFIGURATION_Handle *c)
3675 static const struct GNUNET_CORE_MessageHandler p2p_handlers[] =
3678 GNUNET_MESSAGE_TYPE_FS_GET, 0 },
3680 GNUNET_MESSAGE_TYPE_FS_PUT, 0 },
3683 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
3684 {&GNUNET_FS_handle_index_start, NULL,
3685 GNUNET_MESSAGE_TYPE_FS_INDEX_START, 0},
3686 {&GNUNET_FS_handle_index_list_get, NULL,
3687 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_GET, sizeof(struct GNUNET_MessageHeader) },
3688 {&GNUNET_FS_handle_unindex, NULL, GNUNET_MESSAGE_TYPE_FS_UNINDEX,
3689 sizeof (struct UnindexMessage) },
3690 {&handle_start_search, NULL, GNUNET_MESSAGE_TYPE_FS_START_SEARCH,
3694 unsigned long long enc = 128;
3698 stats = GNUNET_STATISTICS_create (sched, "fs", cfg);
3699 min_migration_delay = GNUNET_TIME_UNIT_SECONDS;
3701 GNUNET_CONFIGURATION_get_value_number (cfg,
3703 "MAX_PENDING_REQUESTS",
3704 &max_pending_requests)) ||
3706 GNUNET_CONFIGURATION_get_value_number (cfg,
3708 "EXPECTED_NEIGHBOUR_COUNT",
3711 GNUNET_CONFIGURATION_get_value_time (cfg,
3713 "MIN_MIGRATION_DELAY",
3714 &min_migration_delay)) )
3716 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3717 _("Configuration fails to specify certain parameters, assuming default values."));
3719 connected_peers = GNUNET_CONTAINER_multihashmap_create (enc);
3720 query_request_map = GNUNET_CONTAINER_multihashmap_create (max_pending_requests);
3721 peer_request_map = GNUNET_CONTAINER_multihashmap_create (enc);
3722 requests_by_expiration_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
3723 core = GNUNET_CORE_connect (sched,
3725 GNUNET_TIME_UNIT_FOREVER_REL,
3728 &peer_connect_handler,
3729 &peer_disconnect_handler,
3736 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3737 _("Failed to connect to `%s' service.\n"),
3739 GNUNET_CONTAINER_multihashmap_destroy (connected_peers);
3740 connected_peers = NULL;
3741 GNUNET_CONTAINER_multihashmap_destroy (query_request_map);
3742 query_request_map = NULL;
3743 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
3744 requests_by_expiration_heap = NULL;
3745 GNUNET_CONTAINER_multihashmap_destroy (peer_request_map);
3746 peer_request_map = NULL;
3749 GNUNET_DATASTORE_disconnect (dsh, GNUNET_NO);
3752 return GNUNET_SYSERR;
3754 /* FIXME: distinguish between sending and storing in options? */
3755 if (active_migration)
3757 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3758 _("Content migration is enabled, will start to gather data\n"));
3759 consider_migration_gathering ();
3761 GNUNET_SERVER_disconnect_notify (server,
3762 &handle_client_disconnect,
3764 GNUNET_assert (GNUNET_OK ==
3765 GNUNET_CONFIGURATION_get_value_filename (cfg,
3769 GNUNET_DISK_directory_create (trustDirectory);
3770 GNUNET_SCHEDULER_add_with_priority (sched,
3771 GNUNET_SCHEDULER_PRIORITY_HIGH,
3772 &cron_flush_trust, NULL);
3775 GNUNET_SERVER_add_handlers (server, handlers);
3776 GNUNET_SCHEDULER_add_delayed (sched,
3777 GNUNET_TIME_UNIT_FOREVER_REL,
3785 * Process fs requests.
3787 * @param cls closure
3788 * @param sched scheduler to use
3789 * @param server the initialized server
3790 * @param cfg configuration to use
3794 struct GNUNET_SCHEDULER_Handle *sched,
3795 struct GNUNET_SERVER_Handle *server,
3796 const struct GNUNET_CONFIGURATION_Handle *cfg)
3798 active_migration = GNUNET_CONFIGURATION_get_value_yesno (cfg,
3801 dsh = GNUNET_DATASTORE_connect (cfg,
3805 GNUNET_SCHEDULER_shutdown (sched);
3808 block_cfg = GNUNET_CONFIGURATION_create ();
3809 GNUNET_CONFIGURATION_set_value_string (block_cfg,
3813 block_ctx = GNUNET_BLOCK_context_create (block_cfg);
3814 GNUNET_assert (NULL != block_ctx);
3815 if ( (GNUNET_OK != GNUNET_FS_indexing_init (sched, cfg, dsh)) ||
3816 (GNUNET_OK != main_init (sched, server, cfg)) )
3818 GNUNET_SCHEDULER_shutdown (sched);
3819 GNUNET_DATASTORE_disconnect (dsh, GNUNET_NO);
3821 GNUNET_BLOCK_context_destroy (block_ctx);
3823 GNUNET_CONFIGURATION_destroy (block_cfg);
3831 * The main function for the fs service.
3833 * @param argc number of arguments from the command line
3834 * @param argv command line arguments
3835 * @return 0 ok, 1 on error
3838 main (int argc, char *const *argv)
3840 return (GNUNET_OK ==
3841 GNUNET_SERVICE_run (argc,
3844 GNUNET_SERVICE_OPTION_NONE,
3845 &run, NULL)) ? 0 : 1;
3848 /* end of gnunet-service-fs.c */