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;
623 static struct GNUNET_SCHEDULER_Handle *sched;
628 static const struct GNUNET_CONFIGURATION_Handle *cfg;
631 * Map of peer identifiers to "struct ConnectedPeer" (for that peer).
633 static struct GNUNET_CONTAINER_MultiHashMap *connected_peers;
636 * Map of peer identifiers to "struct PendingRequest" (for that peer).
638 static struct GNUNET_CONTAINER_MultiHashMap *peer_request_map;
641 * Map of query identifiers to "struct PendingRequest" (for that query).
643 static struct GNUNET_CONTAINER_MultiHashMap *query_request_map;
646 * Heap with the request that will expire next at the top. Contains
647 * pointers of type "struct PendingRequest*"; these will *also* be
648 * aliased from the "requests_by_peer" data structures and the
649 * "requests_by_query" table. Note that requests from our clients
650 * don't expire and are thus NOT in the "requests_by_expiration"
651 * (or the "requests_by_peer" tables).
653 static struct GNUNET_CONTAINER_Heap *requests_by_expiration_heap;
656 * Handle for reporting statistics.
658 static struct GNUNET_STATISTICS_Handle *stats;
661 * Linked list of clients we are currently processing requests for.
663 static struct ClientList *client_list;
666 * Pointer to handle to the core service (points to NULL until we've
669 static struct GNUNET_CORE_Handle *core;
672 * Head of linked list of blocks that can be migrated.
674 static struct MigrationReadyBlock *mig_head;
677 * Tail of linked list of blocks that can be migrated.
679 static struct MigrationReadyBlock *mig_tail;
682 * Request to datastore for migration (or NULL).
684 static struct GNUNET_DATASTORE_QueueEntry *mig_qe;
687 * Where do we store trust information?
689 static char *trustDirectory;
692 * ID of task that collects blocks for migration.
694 static GNUNET_SCHEDULER_TaskIdentifier mig_task;
697 * What is the maximum frequency at which we are allowed to
698 * poll the datastore for migration content?
700 static struct GNUNET_TIME_Relative min_migration_delay;
703 * Size of the doubly-linked list of migration blocks.
705 static unsigned int mig_size;
708 * Are we allowed to migrate content to this peer.
710 static int active_migration;
713 * Typical priorities we're seeing from other peers right now. Since
714 * most priorities will be zero, this value is the weighted average of
715 * non-zero priorities seen "recently". In order to ensure that new
716 * values do not dramatically change the ratio, values are first
717 * "capped" to a reasonable range (+N of the current value) and then
718 * averaged into the existing value by a ratio of 1:N. Hence
719 * receiving the largest possible priority can still only raise our
720 * "current_priorities" by at most 1.
722 static double current_priorities;
725 * Get the filename under which we would store the GNUNET_HELLO_Message
726 * for the given host and protocol.
727 * @return filename of the form DIRECTORY/HOSTID
730 get_trust_filename (const struct GNUNET_PeerIdentity *id)
732 struct GNUNET_CRYPTO_HashAsciiEncoded fil;
735 GNUNET_CRYPTO_hash_to_enc (&id->hashPubKey, &fil);
736 GNUNET_asprintf (&fn, "%s%s%s", trustDirectory, DIR_SEPARATOR_STR, &fil);
743 * Transmit messages by copying it to the target buffer
744 * "buf". "buf" will be NULL and "size" zero if the socket was closed
745 * for writing in the meantime. In that case, do nothing
746 * (the disconnect or shutdown handler will take care of the rest).
747 * If we were able to transmit messages and there are still more
748 * pending, ask core again for further calls to this function.
750 * @param cls closure, pointer to the 'struct ConnectedPeer*'
751 * @param size number of bytes available in buf
752 * @param buf where the callee should write the message
753 * @return number of bytes written to buf
756 transmit_to_peer (void *cls,
757 size_t size, void *buf);
760 /* ******************* clean up functions ************************ */
764 * Delete the given migration block.
766 * @param mb block to delete
769 delete_migration_block (struct MigrationReadyBlock *mb)
771 GNUNET_CONTAINER_DLL_remove (mig_head,
774 GNUNET_PEER_decrement_rcs (mb->target_list,
775 MIGRATION_LIST_SIZE);
782 * Compare the distance of two peers to a key.
785 * @param p1 first peer
786 * @param p2 second peer
787 * @return GNUNET_YES if P1 is closer to key than P2
790 is_closer (const GNUNET_HashCode *key,
791 const struct GNUNET_PeerIdentity *p1,
792 const struct GNUNET_PeerIdentity *p2)
794 return GNUNET_CRYPTO_hash_xorcmp (&p1->hashPubKey,
801 * Consider migrating content to a given peer.
803 * @param cls 'struct MigrationReadyBlock*' to select
804 * targets for (or NULL for none)
805 * @param key ID of the peer
806 * @param value 'struct ConnectedPeer' of the peer
807 * @return GNUNET_YES (always continue iteration)
810 consider_migration (void *cls,
811 const GNUNET_HashCode *key,
814 struct MigrationReadyBlock *mb = cls;
815 struct ConnectedPeer *cp = value;
816 struct MigrationReadyBlock *pos;
817 struct GNUNET_PeerIdentity cppid;
818 struct GNUNET_PeerIdentity otherpid;
819 struct GNUNET_PeerIdentity worstpid;
824 /* consider 'cp' as a migration target for mb */
827 GNUNET_PEER_resolve (cp->pid,
829 repl = MIGRATION_LIST_SIZE;
830 for (i=0;i<MIGRATION_LIST_SIZE;i++)
832 if (mb->target_list[i] == 0)
834 mb->target_list[i] = cp->pid;
835 GNUNET_PEER_change_rc (mb->target_list[i], 1);
836 repl = MIGRATION_LIST_SIZE;
839 GNUNET_PEER_resolve (mb->target_list[i],
841 if ( (repl == MIGRATION_LIST_SIZE) &&
842 is_closer (&mb->query,
849 else if ( (repl != MIGRATION_LIST_SIZE) &&
850 (is_closer (&mb->query,
858 if (repl != MIGRATION_LIST_SIZE)
860 GNUNET_PEER_change_rc (mb->target_list[repl], -1);
861 mb->target_list[repl] = cp->pid;
862 GNUNET_PEER_change_rc (mb->target_list[repl], 1);
866 /* consider scheduling transmission to cp for content migration */
873 for (i=0;i<MIGRATION_LIST_SIZE;i++)
875 if (cp->pid == pos->target_list[i])
880 msize = GNUNET_MIN (msize,
888 return GNUNET_YES; /* no content available */
890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
891 "Trying to migrate at least %u bytes to peer `%s'\n",
896 = GNUNET_CORE_notify_transmit_ready (core,
897 0, GNUNET_TIME_UNIT_FOREVER_REL,
898 (const struct GNUNET_PeerIdentity*) key,
899 msize + sizeof (struct PutMessage),
907 * Task that is run periodically to obtain blocks for content
911 * @param tc scheduler context (also unused)
914 gather_migration_blocks (void *cls,
915 const struct GNUNET_SCHEDULER_TaskContext *tc);
919 * If the migration task is not currently running, consider
920 * (re)scheduling it with the appropriate delay.
923 consider_migration_gathering ()
925 struct GNUNET_TIME_Relative delay;
929 if (mig_task != GNUNET_SCHEDULER_NO_TASK)
931 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
933 delay = GNUNET_TIME_relative_divide (delay,
934 MAX_MIGRATION_QUEUE);
935 delay = GNUNET_TIME_relative_max (delay,
936 min_migration_delay);
937 mig_task = GNUNET_SCHEDULER_add_delayed (sched,
939 &gather_migration_blocks,
945 * Process content offered for migration.
948 * @param key key for the content
949 * @param size number of bytes in data
950 * @param data content stored
951 * @param type type of the content
952 * @param priority priority of the content
953 * @param anonymity anonymity-level for the content
954 * @param expiration expiration time for the content
955 * @param uid unique identifier for the datum;
956 * maybe 0 if no unique identifier is available
959 process_migration_content (void *cls,
960 const GNUNET_HashCode * key,
963 enum GNUNET_BLOCK_Type type,
966 struct GNUNET_TIME_Absolute
967 expiration, uint64_t uid)
969 struct MigrationReadyBlock *mb;
974 if (mig_size < MAX_MIGRATION_QUEUE)
975 consider_migration_gathering ();
978 if (type == GNUNET_BLOCK_TYPE_ONDEMAND)
981 GNUNET_FS_handle_on_demand_block (key, size, data,
982 type, priority, anonymity,
984 &process_migration_content,
986 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
991 "Retrieved block `%s' of type %u for migration\n",
995 mb = GNUNET_malloc (sizeof (struct MigrationReadyBlock) + size);
997 mb->expiration = expiration;
1000 memcpy (&mb[1], data, size);
1001 GNUNET_CONTAINER_DLL_insert_after (mig_head,
1006 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
1007 &consider_migration,
1009 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
1014 * Task that is run periodically to obtain blocks for content
1018 * @param tc scheduler context (also unused)
1021 gather_migration_blocks (void *cls,
1022 const struct GNUNET_SCHEDULER_TaskContext *tc)
1024 mig_task = GNUNET_SCHEDULER_NO_TASK;
1025 mig_qe = GNUNET_DATASTORE_get_random (dsh, 0, -1,
1026 GNUNET_TIME_UNIT_FOREVER_REL,
1027 &process_migration_content, NULL);
1028 GNUNET_assert (mig_qe != NULL);
1033 * We're done with a particular message list entry.
1034 * Free all associated resources.
1036 * @param pml entry to destroy
1039 destroy_pending_message_list_entry (struct PendingMessageList *pml)
1041 GNUNET_CONTAINER_DLL_remove (pml->req->pending_head,
1042 pml->req->pending_tail,
1044 GNUNET_CONTAINER_DLL_remove (pml->target->pending_messages_head,
1045 pml->target->pending_messages_tail,
1047 pml->target->pending_requests--;
1048 GNUNET_free (pml->pm);
1054 * Destroy the given pending message (and call the respective
1057 * @param pm message to destroy
1058 * @param tpid id of peer that the message was delivered to, or 0 for none
1061 destroy_pending_message (struct PendingMessage *pm,
1062 GNUNET_PEER_Id tpid)
1064 struct PendingMessageList *pml = pm->pml;
1065 TransmissionContinuation cont;
1068 GNUNET_assert (pml->pm == pm);
1069 GNUNET_assert ( (tpid == 0) || (tpid == pml->target->pid) );
1071 cont_cls = pm->cont_cls;
1072 destroy_pending_message_list_entry (pml);
1073 cont (cont_cls, tpid);
1078 * We're done processing a particular request.
1079 * Free all associated resources.
1081 * @param pr request to destroy
1084 destroy_pending_request (struct PendingRequest *pr)
1086 struct GNUNET_PeerIdentity pid;
1088 if (pr->hnode != NULL)
1090 GNUNET_CONTAINER_heap_remove_node (requests_by_expiration_heap,
1094 if (NULL == pr->client_request_list)
1096 GNUNET_STATISTICS_update (stats,
1097 gettext_noop ("# P2P searches active"),
1103 GNUNET_STATISTICS_update (stats,
1104 gettext_noop ("# client searches active"),
1108 /* might have already been removed from map in 'process_reply' (if
1109 there was a unique reply) or never inserted if it was a
1110 duplicate; hence ignore the return value here */
1111 (void) GNUNET_CONTAINER_multihashmap_remove (query_request_map,
1116 GNUNET_DATASTORE_cancel (pr->qe);
1119 if (pr->client_request_list != NULL)
1121 GNUNET_CONTAINER_DLL_remove (pr->client_request_list->client_list->rl_head,
1122 pr->client_request_list->client_list->rl_tail,
1123 pr->client_request_list);
1124 GNUNET_free (pr->client_request_list);
1125 pr->client_request_list = NULL;
1129 GNUNET_PEER_resolve (pr->cp->pid,
1131 (void) GNUNET_CONTAINER_multihashmap_remove (peer_request_map,
1138 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
1141 if (pr->irc != NULL)
1143 GNUNET_CORE_peer_change_preference_cancel (pr->irc);
1146 if (pr->replies_seen != NULL)
1148 GNUNET_free (pr->replies_seen);
1149 pr->replies_seen = NULL;
1151 if (pr->task != GNUNET_SCHEDULER_NO_TASK)
1153 GNUNET_SCHEDULER_cancel (sched,
1155 pr->task = GNUNET_SCHEDULER_NO_TASK;
1157 while (NULL != pr->pending_head)
1158 destroy_pending_message_list_entry (pr->pending_head);
1159 GNUNET_PEER_change_rc (pr->target_pid, -1);
1160 if (pr->used_pids != NULL)
1162 GNUNET_PEER_decrement_rcs (pr->used_pids, pr->used_pids_off);
1163 GNUNET_free (pr->used_pids);
1164 pr->used_pids_off = 0;
1165 pr->used_pids_size = 0;
1166 pr->used_pids = NULL;
1173 * Method called whenever a given peer connects.
1175 * @param cls closure, not used
1176 * @param peer peer identity this notification is about
1177 * @param latency reported latency of the connection with 'other'
1178 * @param distance reported distance (DV) to 'other'
1181 peer_connect_handler (void *cls,
1183 GNUNET_PeerIdentity * peer,
1184 struct GNUNET_TIME_Relative latency,
1187 struct ConnectedPeer *cp;
1188 struct MigrationReadyBlock *pos;
1192 cp = GNUNET_malloc (sizeof (struct ConnectedPeer));
1193 cp->pid = GNUNET_PEER_intern (peer);
1195 fn = get_trust_filename (peer);
1196 if ((GNUNET_DISK_file_test (fn) == GNUNET_YES) &&
1197 (sizeof (trust) == GNUNET_DISK_fn_read (fn, &trust, sizeof (trust))))
1198 cp->disk_trust = cp->trust = ntohl (trust);
1201 GNUNET_break (GNUNET_OK ==
1202 GNUNET_CONTAINER_multihashmap_put (connected_peers,
1205 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1210 (void) consider_migration (pos, &peer->hashPubKey, cp);
1217 * Increase the host credit by a value.
1219 * @param host which peer to change the trust value on
1220 * @param value is the int value by which the
1221 * host credit is to be increased or decreased
1222 * @returns the actual change in trust (positive or negative)
1225 change_host_trust (struct ConnectedPeer *host, int value)
1227 unsigned int old_trust;
1231 GNUNET_assert (host != NULL);
1232 old_trust = host->trust;
1235 if (host->trust + value < host->trust)
1237 value = UINT32_MAX - host->trust;
1238 host->trust = UINT32_MAX;
1241 host->trust += value;
1245 if (host->trust < -value)
1247 value = -host->trust;
1251 host->trust += value;
1258 * Write host-trust information to a file - flush the buffer entry!
1261 flush_trust (void *cls,
1262 const GNUNET_HashCode *key,
1265 struct ConnectedPeer *host = value;
1268 struct GNUNET_PeerIdentity pid;
1270 if (host->trust == host->disk_trust)
1271 return GNUNET_OK; /* unchanged */
1272 GNUNET_PEER_resolve (host->pid,
1274 fn = get_trust_filename (&pid);
1275 if (host->trust == 0)
1277 if ((0 != UNLINK (fn)) && (errno != ENOENT))
1278 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
1279 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
1283 trust = htonl (host->trust);
1284 if (sizeof(uint32_t) == GNUNET_DISK_fn_write (fn, &trust,
1286 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE
1287 | GNUNET_DISK_PERM_GROUP_READ | GNUNET_DISK_PERM_OTHER_READ))
1288 host->disk_trust = host->trust;
1295 * Call this method periodically to scan data/hosts for new hosts.
1298 cron_flush_trust (void *cls,
1299 const struct GNUNET_SCHEDULER_TaskContext *tc)
1302 if (NULL == connected_peers)
1304 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
1309 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1311 GNUNET_SCHEDULER_add_delayed (tc->sched,
1312 TRUST_FLUSH_FREQ, &cron_flush_trust, NULL);
1317 * Free (each) request made by the peer.
1319 * @param cls closure, points to peer that the request belongs to
1320 * @param key current key code
1321 * @param value value in the hash map
1322 * @return GNUNET_YES (we should continue to iterate)
1325 destroy_request (void *cls,
1326 const GNUNET_HashCode * key,
1329 const struct GNUNET_PeerIdentity * peer = cls;
1330 struct PendingRequest *pr = value;
1332 GNUNET_break (GNUNET_YES ==
1333 GNUNET_CONTAINER_multihashmap_remove (peer_request_map,
1336 destroy_pending_request (pr);
1342 * Method called whenever a peer disconnects.
1344 * @param cls closure, not used
1345 * @param peer peer identity this notification is about
1348 peer_disconnect_handler (void *cls,
1350 GNUNET_PeerIdentity * peer)
1352 struct ConnectedPeer *cp;
1353 struct PendingMessage *pm;
1355 struct MigrationReadyBlock *pos;
1356 struct MigrationReadyBlock *next;
1358 GNUNET_CONTAINER_multihashmap_get_multiple (peer_request_map,
1362 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
1366 for (i=0;i<CS2P_SUCCESS_LIST_SIZE;i++)
1368 if (NULL != cp->last_client_replies[i])
1370 GNUNET_SERVER_client_drop (cp->last_client_replies[i]);
1371 cp->last_client_replies[i] = NULL;
1374 GNUNET_break (GNUNET_YES ==
1375 GNUNET_CONTAINER_multihashmap_remove (connected_peers,
1378 /* remove this peer from migration considerations; schedule
1381 while (NULL != (pos = next))
1384 for (i=0;i<MIGRATION_LIST_SIZE;i++)
1386 if (pos->target_list[i] == cp->pid)
1388 GNUNET_PEER_change_rc (pos->target_list[i], -1);
1389 pos->target_list[i] = 0;
1392 if (pos->used_targets >= GNUNET_CONTAINER_multihashmap_size (connected_peers))
1394 delete_migration_block (pos);
1395 consider_migration_gathering ();
1398 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
1399 &consider_migration,
1402 GNUNET_PEER_change_rc (cp->pid, -1);
1403 GNUNET_PEER_decrement_rcs (cp->last_p2p_replies, P2P_SUCCESS_LIST_SIZE);
1404 if (NULL != cp->cth)
1405 GNUNET_CORE_notify_transmit_ready_cancel (cp->cth);
1406 while (NULL != (pm = cp->pending_messages_head))
1407 destroy_pending_message (pm, 0 /* delivery failed */);
1408 GNUNET_break (0 == cp->pending_requests);
1414 * Iterator over hash map entries that removes all occurences
1415 * of the given 'client' from the 'last_client_replies' of the
1416 * given connected peer.
1418 * @param cls closure, the 'struct GNUNET_SERVER_Client*' to remove
1419 * @param key current key code (unused)
1420 * @param value value in the hash map (the 'struct ConnectedPeer*' to change)
1421 * @return GNUNET_YES (we should continue to iterate)
1424 remove_client_from_last_client_replies (void *cls,
1425 const GNUNET_HashCode * key,
1428 struct GNUNET_SERVER_Client *client = cls;
1429 struct ConnectedPeer *cp = value;
1432 for (i=0;i<CS2P_SUCCESS_LIST_SIZE;i++)
1434 if (cp->last_client_replies[i] == client)
1436 GNUNET_SERVER_client_drop (cp->last_client_replies[i]);
1437 cp->last_client_replies[i] = NULL;
1445 * A client disconnected. Remove all of its pending queries.
1447 * @param cls closure, NULL
1448 * @param client identification of the client
1451 handle_client_disconnect (void *cls,
1452 struct GNUNET_SERVER_Client
1455 struct ClientList *pos;
1456 struct ClientList *prev;
1457 struct ClientRequestList *rcl;
1458 struct ClientResponseMessage *creply;
1464 while ( (NULL != pos) &&
1465 (pos->client != client) )
1471 return; /* no requests pending for this client */
1472 while (NULL != (rcl = pos->rl_head))
1474 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1475 "Destroying pending request `%s' on disconnect\n",
1476 GNUNET_h2s (&rcl->req->query));
1477 destroy_pending_request (rcl->req);
1480 client_list = pos->next;
1482 prev->next = pos->next;
1483 if (pos->th != NULL)
1485 GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th);
1488 while (NULL != (creply = pos->res_head))
1490 GNUNET_CONTAINER_DLL_remove (pos->res_head,
1493 GNUNET_free (creply);
1495 GNUNET_SERVER_client_drop (pos->client);
1497 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
1498 &remove_client_from_last_client_replies,
1504 * Iterator to free peer entries.
1506 * @param cls closure, unused
1507 * @param key current key code
1508 * @param value value in the hash map (peer entry)
1509 * @return GNUNET_YES (we should continue to iterate)
1512 clean_peer (void *cls,
1513 const GNUNET_HashCode * key,
1516 peer_disconnect_handler (NULL, (const struct GNUNET_PeerIdentity*) key);
1522 * Task run during shutdown.
1528 shutdown_task (void *cls,
1529 const struct GNUNET_SCHEDULER_TaskContext *tc)
1533 GNUNET_DATASTORE_cancel (mig_qe);
1536 if (GNUNET_SCHEDULER_NO_TASK != mig_task)
1538 GNUNET_SCHEDULER_cancel (sched, mig_task);
1539 mig_task = GNUNET_SCHEDULER_NO_TASK;
1541 while (client_list != NULL)
1542 handle_client_disconnect (NULL,
1543 client_list->client);
1544 cron_flush_trust (NULL, NULL);
1545 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
1548 GNUNET_break (0 == GNUNET_CONTAINER_heap_get_size (requests_by_expiration_heap));
1549 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
1550 requests_by_expiration_heap = 0;
1551 GNUNET_CONTAINER_multihashmap_destroy (connected_peers);
1552 connected_peers = NULL;
1553 GNUNET_break (0 == GNUNET_CONTAINER_multihashmap_size (query_request_map));
1554 GNUNET_CONTAINER_multihashmap_destroy (query_request_map);
1555 query_request_map = NULL;
1556 GNUNET_break (0 == GNUNET_CONTAINER_multihashmap_size (peer_request_map));
1557 GNUNET_CONTAINER_multihashmap_destroy (peer_request_map);
1558 peer_request_map = NULL;
1559 GNUNET_assert (NULL != core);
1560 GNUNET_CORE_disconnect (core);
1564 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1567 GNUNET_DATASTORE_disconnect (dsh,
1569 while (mig_head != NULL)
1570 delete_migration_block (mig_head);
1571 GNUNET_assert (0 == mig_size);
1575 GNUNET_free_non_null (trustDirectory);
1576 trustDirectory = NULL;
1580 /* ******************* Utility functions ******************** */
1584 * Transmit messages by copying it to the target buffer
1585 * "buf". "buf" will be NULL and "size" zero if the socket was closed
1586 * for writing in the meantime. In that case, do nothing
1587 * (the disconnect or shutdown handler will take care of the rest).
1588 * If we were able to transmit messages and there are still more
1589 * pending, ask core again for further calls to this function.
1591 * @param cls closure, pointer to the 'struct ConnectedPeer*'
1592 * @param size number of bytes available in buf
1593 * @param buf where the callee should write the message
1594 * @return number of bytes written to buf
1597 transmit_to_peer (void *cls,
1598 size_t size, void *buf)
1600 struct ConnectedPeer *cp = cls;
1602 struct GNUNET_PeerIdentity pid;
1603 struct PendingMessage *pm;
1604 struct MigrationReadyBlock *mb;
1605 struct MigrationReadyBlock *next;
1606 struct PutMessage migm;
1614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1615 "Dropping message, core too busy.\n");
1620 while ( (NULL != (pm = cp->pending_messages_head) ) &&
1621 (pm->msize <= size) )
1623 memcpy (&cbuf[msize], &pm[1], pm->msize);
1626 destroy_pending_message (pm, cp->pid);
1630 GNUNET_PEER_resolve (cp->pid,
1632 cp->cth = GNUNET_CORE_notify_transmit_ready (core,
1634 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1643 while (NULL != (mb = next))
1646 for (i=0;i<MIGRATION_LIST_SIZE;i++)
1648 if ( (cp->pid == mb->target_list[i]) &&
1649 (mb->size + sizeof (migm) <= size) )
1651 GNUNET_PEER_change_rc (mb->target_list[i], -1);
1652 mb->target_list[i] = 0;
1654 migm.header.size = htons (sizeof (migm) + mb->size);
1655 migm.header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
1656 migm.type = htonl (mb->type);
1657 migm.expiration = GNUNET_TIME_absolute_hton (mb->expiration);
1658 memcpy (&cbuf[msize], &migm, sizeof (migm));
1659 msize += sizeof (migm);
1660 size -= sizeof (migm);
1661 memcpy (&cbuf[msize], &mb[1], mb->size);
1665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1666 "Pushing migration block `%s' (%u bytes) to `%s'\n",
1667 GNUNET_h2s (&mb->query),
1676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1677 "Migration block `%s' (%u bytes) is not on migration list for peer `%s'\n",
1678 GNUNET_h2s (&mb->query),
1684 if ( (mb->used_targets >= MIGRATION_TARGET_COUNT) ||
1685 (mb->used_targets >= GNUNET_CONTAINER_multihashmap_size (connected_peers)) )
1687 delete_migration_block (mb);
1688 consider_migration_gathering ();
1691 consider_migration (NULL,
1696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1697 "Transmitting %u bytes to peer %u\n",
1706 * Add a message to the set of pending messages for the given peer.
1708 * @param cp peer to send message to
1709 * @param pm message to queue
1710 * @param pr request on which behalf this message is being queued
1713 add_to_pending_messages_for_peer (struct ConnectedPeer *cp,
1714 struct PendingMessage *pm,
1715 struct PendingRequest *pr)
1717 struct PendingMessage *pos;
1718 struct PendingMessageList *pml;
1719 struct GNUNET_PeerIdentity pid;
1721 GNUNET_assert (pm->next == NULL);
1722 GNUNET_assert (pm->pml == NULL);
1723 pml = GNUNET_malloc (sizeof (struct PendingMessageList));
1728 GNUNET_CONTAINER_DLL_insert (pr->pending_head,
1731 pos = cp->pending_messages_head;
1732 while ( (pos != NULL) &&
1733 (pm->priority < pos->priority) )
1735 GNUNET_CONTAINER_DLL_insert_after (cp->pending_messages_head,
1736 cp->pending_messages_tail,
1739 cp->pending_requests++;
1740 if (cp->pending_requests > MAX_QUEUE_PER_PEER)
1741 destroy_pending_message (cp->pending_messages_tail, 0);
1742 GNUNET_PEER_resolve (cp->pid, &pid);
1743 if (NULL != cp->cth)
1744 GNUNET_CORE_notify_transmit_ready_cancel (cp->cth);
1745 /* need to schedule transmission */
1746 cp->cth = GNUNET_CORE_notify_transmit_ready (core,
1747 cp->pending_messages_head->priority,
1750 cp->pending_messages_head->msize,
1753 if (cp->cth == NULL)
1756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1757 "Failed to schedule transmission with core!\n");
1759 GNUNET_STATISTICS_update (stats,
1760 gettext_noop ("# CORE transmission failures"),
1768 * Mingle hash with the mingle_number to produce different bits.
1771 mingle_hash (const GNUNET_HashCode * in,
1772 int32_t mingle_number,
1773 GNUNET_HashCode * hc)
1777 GNUNET_CRYPTO_hash (&mingle_number,
1780 GNUNET_CRYPTO_hash_xor (&m, in, hc);
1785 * Test if the load on this peer is too high
1786 * to even consider processing the query at
1789 * @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)
1792 test_load_too_high ()
1794 return GNUNET_SYSERR; // FIXME
1798 /* ******************* Pending Request Refresh Task ******************** */
1803 * We use a random delay to make the timing of requests less
1804 * predictable. This function returns such a random delay. We add a base
1805 * delay of MAX_CORK_DELAY (1s).
1807 * FIXME: make schedule dependent on the specifics of the request?
1808 * Or bandwidth and number of connected peers and load?
1810 * @return random delay to use for some request, between 1s and 1000+TTL_DECREMENT ms
1812 static struct GNUNET_TIME_Relative
1813 get_processing_delay ()
1816 GNUNET_TIME_relative_add (GNUNET_CONSTANTS_MAX_CORK_DELAY,
1817 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1818 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1824 * We're processing a GET request from another peer and have decided
1825 * to forward it to other peers. This function is called periodically
1826 * and should forward the request to other peers until we have all
1827 * possible replies. If we have transmitted the *only* reply to
1828 * the initiator we should destroy the pending request. If we have
1829 * many replies in the queue to the initiator, we should delay sending
1830 * out more queries until the reply queue has shrunk some.
1832 * @param cls our "struct ProcessGetContext *"
1836 forward_request_task (void *cls,
1837 const struct GNUNET_SCHEDULER_TaskContext *tc);
1841 * Function called after we either failed or succeeded
1842 * at transmitting a query to a peer.
1844 * @param cls the requests "struct PendingRequest*"
1845 * @param tpid ID of receiving peer, 0 on transmission error
1848 transmit_query_continuation (void *cls,
1849 GNUNET_PEER_Id tpid)
1851 struct PendingRequest *pr = cls;
1853 GNUNET_STATISTICS_update (stats,
1854 gettext_noop ("# queries scheduled for forwarding"),
1860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1861 "Transmission of request failed, will try again later.\n");
1863 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
1864 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
1865 get_processing_delay (),
1866 &forward_request_task,
1870 GNUNET_STATISTICS_update (stats,
1871 gettext_noop ("# queries forwarded"),
1874 GNUNET_PEER_change_rc (tpid, 1);
1875 if (pr->used_pids_off == pr->used_pids_size)
1876 GNUNET_array_grow (pr->used_pids,
1878 pr->used_pids_size * 2 + 2);
1879 pr->used_pids[pr->used_pids_off++] = tpid;
1880 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
1881 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
1882 get_processing_delay (),
1883 &forward_request_task,
1889 * How many bytes should a bloomfilter be if we have already seen
1890 * entry_count responses? Note that BLOOMFILTER_K gives us the number
1891 * of bits set per entry. Furthermore, we should not re-size the
1892 * filter too often (to keep it cheap).
1894 * Since other peers will also add entries but not resize the filter,
1895 * we should generally pick a slightly larger size than what the
1896 * strict math would suggest.
1898 * @return must be a power of two and smaller or equal to 2^15.
1901 compute_bloomfilter_size (unsigned int entry_count)
1904 unsigned int ideal = (entry_count * BLOOMFILTER_K) / 4;
1905 uint16_t max = 1 << 15;
1907 if (entry_count > max)
1910 while ((size < max) && (size < ideal))
1919 * Recalculate our bloom filter for filtering replies. This function
1920 * will create a new bloom filter from scratch, so it should only be
1921 * called if we have no bloomfilter at all (and hence can create a
1922 * fresh one of minimal size without problems) OR if our peer is the
1923 * initiator (in which case we may resize to larger than mimimum size).
1925 * @param pr request for which the BF is to be recomputed
1928 refresh_bloomfilter (struct PendingRequest *pr)
1932 GNUNET_HashCode mhash;
1934 nsize = compute_bloomfilter_size (pr->replies_seen_off);
1935 if (nsize == pr->bf_size)
1936 return; /* size not changed */
1938 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
1939 pr->bf_size = nsize;
1940 pr->mingle = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, -1);
1941 pr->bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
1944 for (i=0;i<pr->replies_seen_off;i++)
1946 mingle_hash (&pr->replies_seen[i], pr->mingle, &mhash);
1947 GNUNET_CONTAINER_bloomfilter_add (pr->bf, &mhash);
1953 * Function called after we've tried to reserve a certain amount of
1954 * bandwidth for a reply. Check if we succeeded and if so send our
1957 * @param cls the requests "struct PendingRequest*"
1958 * @param peer identifies the peer
1959 * @param bpm_in set to the current bandwidth limit (receiving) for this peer
1960 * @param bpm_out set to the current bandwidth limit (sending) for this peer
1961 * @param amount set to the amount that was actually reserved or unreserved
1962 * @param preference current traffic preference for the given peer
1965 target_reservation_cb (void *cls,
1967 GNUNET_PeerIdentity * peer,
1968 struct GNUNET_BANDWIDTH_Value32NBO bpm_in,
1969 struct GNUNET_BANDWIDTH_Value32NBO bpm_out,
1971 uint64_t preference)
1973 struct PendingRequest *pr = cls;
1974 struct ConnectedPeer *cp;
1975 struct PendingMessage *pm;
1976 struct GetMessage *gm;
1977 GNUNET_HashCode *ext;
1987 /* error in communication with core, try again later */
1988 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
1989 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
1990 get_processing_delay (),
1991 &forward_request_task,
1995 // (3) transmit, update ttl/priority
1996 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
2000 /* Peer must have just left */
2002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2003 "Selected peer disconnected!\n");
2005 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
2006 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
2007 get_processing_delay (),
2008 &forward_request_task,
2012 no_route = GNUNET_NO;
2018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2019 "Failed to reserve bandwidth for reply (got %d/%u bytes only)!\n",
2023 GNUNET_STATISTICS_update (stats,
2024 gettext_noop ("# reply bandwidth reservation requests failed"),
2027 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
2028 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
2029 get_processing_delay (),
2030 &forward_request_task,
2032 return; /* this target round failed */
2034 /* FIXME: if we are "quite" busy, we may still want to skip
2035 this round; need more load detection code! */
2036 no_route = GNUNET_YES;
2039 GNUNET_STATISTICS_update (stats,
2040 gettext_noop ("# queries scheduled for forwarding"),
2043 /* build message and insert message into priority queue */
2045 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2046 "Forwarding request `%s' to `%4s'!\n",
2047 GNUNET_h2s (&pr->query),
2052 if (GNUNET_YES == no_route)
2054 bm |= GET_MESSAGE_BIT_RETURN_TO;
2057 if (pr->namespace != NULL)
2059 bm |= GET_MESSAGE_BIT_SKS_NAMESPACE;
2062 if (pr->target_pid != 0)
2064 bm |= GET_MESSAGE_BIT_TRANSMIT_TO;
2067 msize = sizeof (struct GetMessage) + pr->bf_size + k * sizeof(GNUNET_HashCode);
2068 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
2069 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
2071 gm = (struct GetMessage*) &pm[1];
2072 gm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_GET);
2073 gm->header.size = htons (msize);
2074 gm->type = htonl (pr->type);
2075 pr->remaining_priority /= 2;
2076 gm->priority = htonl (pr->remaining_priority);
2077 gm->ttl = htonl (pr->ttl);
2078 gm->filter_mutator = htonl(pr->mingle);
2079 gm->hash_bitmap = htonl (bm);
2080 gm->query = pr->query;
2081 ext = (GNUNET_HashCode*) &gm[1];
2083 if (GNUNET_YES == no_route)
2084 GNUNET_PEER_resolve (pr->cp->pid, (struct GNUNET_PeerIdentity*) &ext[k++]);
2085 if (pr->namespace != NULL)
2086 memcpy (&ext[k++], pr->namespace, sizeof (GNUNET_HashCode));
2087 if (pr->target_pid != 0)
2088 GNUNET_PEER_resolve (pr->target_pid, (struct GNUNET_PeerIdentity*) &ext[k++]);
2089 bfdata = (char *) &ext[k];
2091 GNUNET_CONTAINER_bloomfilter_get_raw_data (pr->bf,
2094 pm->cont = &transmit_query_continuation;
2096 add_to_pending_messages_for_peer (cp, pm, pr);
2101 * Closure used for "target_peer_select_cb".
2103 struct PeerSelectionContext
2106 * The request for which we are selecting
2109 struct PendingRequest *pr;
2112 * Current "prime" target.
2114 struct GNUNET_PeerIdentity target;
2117 * How much do we like this target?
2119 double target_score;
2125 * Function called for each connected peer to determine
2126 * which one(s) would make good targets for forwarding.
2128 * @param cls closure (struct PeerSelectionContext)
2129 * @param key current key code (peer identity)
2130 * @param value value in the hash map (struct ConnectedPeer)
2131 * @return GNUNET_YES if we should continue to
2136 target_peer_select_cb (void *cls,
2137 const GNUNET_HashCode * key,
2140 struct PeerSelectionContext *psc = cls;
2141 struct ConnectedPeer *cp = value;
2142 struct PendingRequest *pr = psc->pr;
2147 /* 1) check that this peer is not the initiator */
2151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2152 "Skipping initiator in forwarding selection\n");
2154 return GNUNET_YES; /* skip */
2157 /* 2) check if we have already (recently) forwarded to this peer */
2159 for (i=0;i<pr->used_pids_off;i++)
2160 if (pr->used_pids[i] == cp->pid)
2163 if (0 != GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2164 RETRY_PROBABILITY_INV))
2167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2168 "NOT re-trying query that was previously transmitted %u times\n",
2169 (unsigned int) pr->used_pids_off);
2171 return GNUNET_YES; /* skip */
2176 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2177 "Re-trying query that was previously transmitted %u times to this peer\n",
2180 /* 3) calculate how much we'd like to forward to this peer,
2181 starting with a random value that is strong enough
2182 to at least give any peer a chance sometimes
2183 (compared to the other factors that come later) */
2184 /* 3a) count successful (recent) routes from cp for same source */
2187 score = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2188 P2P_SUCCESS_LIST_SIZE);
2189 for (i=0;i<P2P_SUCCESS_LIST_SIZE;i++)
2190 if (cp->last_p2p_replies[i] == pr->cp->pid)
2191 score += 1; /* likely successful based on hot path */
2195 score = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2196 CS2P_SUCCESS_LIST_SIZE);
2197 for (i=0;i<CS2P_SUCCESS_LIST_SIZE;i++)
2198 if (cp->last_client_replies[i] == pr->client_request_list->client_list->client)
2199 score += 1; /* likely successful based on hot path */
2201 /* 3b) include latency */
2202 if (cp->avg_delay.value < 4 * TTL_DECREMENT)
2203 score += 1; /* likely fast based on latency */
2204 /* 3c) include priorities */
2205 if (cp->avg_priority <= pr->remaining_priority / 2.0)
2206 score += 1; /* likely successful based on priorities */
2207 /* 3d) penalize for queue size */
2208 score -= (2.0 * cp->pending_requests / (double) MAX_QUEUE_PER_PEER);
2209 /* 3e) include peer proximity */
2210 score -= (2.0 * (GNUNET_CRYPTO_hash_distance_u32 (key,
2211 &pr->query)) / (double) UINT32_MAX);
2212 /* 4) super-bonus for being the known target */
2213 if (pr->target_pid == cp->pid)
2215 /* store best-fit in closure */
2217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2218 "Peer `%s' gets score %f for forwarding query, max is %f\n",
2223 score++; /* avoid zero */
2224 if (score > psc->target_score)
2226 psc->target_score = score;
2227 psc->target.hashPubKey = *key;
2234 * The priority level imposes a bound on the maximum
2235 * value for the ttl that can be requested.
2237 * @param ttl_in requested ttl
2238 * @param prio given priority
2239 * @return ttl_in if ttl_in is below the limit,
2240 * otherwise the ttl-limit for the given priority
2243 bound_ttl (int32_t ttl_in, uint32_t prio)
2245 unsigned long long allowed;
2249 allowed = ((unsigned long long) prio) * TTL_DECREMENT / 1000;
2250 if (ttl_in > allowed)
2252 if (allowed >= (1 << 30))
2261 * We're processing a GET request and have decided
2262 * to forward it to other peers. This function is called periodically
2263 * and should forward the request to other peers until we have all
2264 * possible replies. If we have transmitted the *only* reply to
2265 * the initiator we should destroy the pending request. If we have
2266 * many replies in the queue to the initiator, we should delay sending
2267 * out more queries until the reply queue has shrunk some.
2269 * @param cls our "struct ProcessGetContext *"
2273 forward_request_task (void *cls,
2274 const struct GNUNET_SCHEDULER_TaskContext *tc)
2276 struct PendingRequest *pr = cls;
2277 struct PeerSelectionContext psc;
2278 struct ConnectedPeer *cp;
2279 struct GNUNET_TIME_Relative delay;
2281 pr->task = GNUNET_SCHEDULER_NO_TASK;
2282 if (pr->irc != NULL)
2285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2286 "Forwarding of query `%s' not attempted due to pending local lookup!\n",
2287 GNUNET_h2s (&pr->query));
2289 return; /* already pending */
2291 if (GNUNET_YES == pr->local_only)
2292 return; /* configured to not do P2P search */
2293 /* (1) select target */
2295 psc.target_score = -DBL_MAX;
2296 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
2297 &target_peer_select_cb,
2299 if (psc.target_score == -DBL_MAX)
2301 delay = get_processing_delay ();
2303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2304 "No peer selected for forwarding of query `%s', will try again in %llu ms!\n",
2305 GNUNET_h2s (&pr->query),
2308 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
2310 &forward_request_task,
2312 return; /* nobody selected */
2314 /* (3) update TTL/priority */
2315 if (pr->client_request_list != NULL)
2317 /* FIXME: use better algorithm!? */
2318 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2321 /* bound priority we use by priorities we see from other peers
2322 rounded up (must round up so that we can see non-zero
2323 priorities, but round up as little as possible to make it
2324 plausible that we forwarded another peers request) */
2325 if (pr->priority > current_priorities + 1.0)
2326 pr->priority = (uint32_t) current_priorities + 1.0;
2327 pr->ttl = bound_ttl (pr->ttl + TTL_DECREMENT * 2,
2330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2331 "Trying query `%s' with priority %u and TTL %d.\n",
2332 GNUNET_h2s (&pr->query),
2338 /* (3) reserve reply bandwidth */
2339 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
2340 &psc.target.hashPubKey);
2341 GNUNET_assert (NULL != cp);
2342 pr->irc = GNUNET_CORE_peer_change_preference (sched, cfg,
2344 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
2345 GNUNET_BANDWIDTH_value_init (UINT32_MAX),
2348 &target_reservation_cb,
2350 cp->inc_preference = 0;
2354 /* **************************** P2P PUT Handling ************************ */
2358 * Function called after we either failed or succeeded
2359 * at transmitting a reply to a peer.
2361 * @param cls the requests "struct PendingRequest*"
2362 * @param tpid ID of receiving peer, 0 on transmission error
2365 transmit_reply_continuation (void *cls,
2366 GNUNET_PEER_Id tpid)
2368 struct PendingRequest *pr = cls;
2372 case GNUNET_BLOCK_TYPE_DBLOCK:
2373 case GNUNET_BLOCK_TYPE_IBLOCK:
2374 /* only one reply expected, done with the request! */
2375 destroy_pending_request (pr);
2377 case GNUNET_BLOCK_TYPE_ANY:
2378 case GNUNET_BLOCK_TYPE_KBLOCK:
2379 case GNUNET_BLOCK_TYPE_SBLOCK:
2389 * Transmit the given message by copying it to the target buffer
2390 * "buf". "buf" will be NULL and "size" zero if the socket was closed
2391 * for writing in the meantime. In that case, do nothing
2392 * (the disconnect or shutdown handler will take care of the rest).
2393 * If we were able to transmit messages and there are still more
2394 * pending, ask core again for further calls to this function.
2396 * @param cls closure, pointer to the 'struct ClientList*'
2397 * @param size number of bytes available in buf
2398 * @param buf where the callee should write the message
2399 * @return number of bytes written to buf
2402 transmit_to_client (void *cls,
2403 size_t size, void *buf)
2405 struct ClientList *cl = cls;
2407 struct ClientResponseMessage *creply;
2414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2415 "Not sending reply, client communication problem.\n");
2420 while ( (NULL != (creply = cl->res_head) ) &&
2421 (creply->msize <= size) )
2423 memcpy (&cbuf[msize], &creply[1], creply->msize);
2424 msize += creply->msize;
2425 size -= creply->msize;
2426 GNUNET_CONTAINER_DLL_remove (cl->res_head,
2429 GNUNET_free (creply);
2432 cl->th = GNUNET_SERVER_notify_transmit_ready (cl->client,
2434 GNUNET_TIME_UNIT_FOREVER_REL,
2435 &transmit_to_client,
2438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2439 "Transmitted %u bytes to client\n",
2440 (unsigned int) msize);
2447 * Closure for "process_reply" function.
2449 struct ProcessReplyClosure
2452 * The data for the reply.
2457 * Who gave us this reply? NULL for local host.
2459 struct ConnectedPeer *sender;
2462 * When the reply expires.
2464 struct GNUNET_TIME_Absolute expiration;
2472 * Namespace that this reply belongs to
2473 * (if it is of type SBLOCK).
2475 GNUNET_HashCode namespace;
2478 * Type of the block.
2480 enum GNUNET_BLOCK_Type type;
2483 * How much was this reply worth to us?
2488 * Did we finish processing the associated request?
2495 * We have received a reply; handle it!
2497 * @param cls response (struct ProcessReplyClosure)
2498 * @param key our query
2499 * @param value value in the hash map (info about the query)
2500 * @return GNUNET_YES (we should continue to iterate)
2503 process_reply (void *cls,
2504 const GNUNET_HashCode * key,
2507 struct ProcessReplyClosure *prq = cls;
2508 struct PendingRequest *pr = value;
2509 struct PendingMessage *reply;
2510 struct ClientResponseMessage *creply;
2511 struct ClientList *cl;
2512 struct PutMessage *pm;
2513 struct ConnectedPeer *cp;
2514 struct GNUNET_TIME_Relative cur_delay;
2515 GNUNET_HashCode chash;
2516 GNUNET_HashCode mhash;
2520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2521 "Matched result (type %u) for query `%s' with pending request\n",
2522 (unsigned int) prq->type,
2525 GNUNET_STATISTICS_update (stats,
2526 gettext_noop ("# replies received and matched"),
2529 if (prq->sender != NULL)
2531 /* FIXME: should we be more precise here and not use
2532 "start_time" but a peer-specific time stamp? */
2533 cur_delay = GNUNET_TIME_absolute_get_duration (pr->start_time);
2534 prq->sender->avg_delay.value
2535 = (prq->sender->avg_delay.value *
2536 (RUNAVG_DELAY_N - 1) + cur_delay.value) / RUNAVG_DELAY_N;
2537 prq->sender->avg_priority
2538 = (prq->sender->avg_priority *
2539 (RUNAVG_DELAY_N - 1) + pr->priority) / (double) RUNAVG_DELAY_N;
2542 GNUNET_PEER_change_rc (prq->sender->last_p2p_replies
2543 [prq->sender->last_p2p_replies_woff % P2P_SUCCESS_LIST_SIZE],
2545 GNUNET_PEER_change_rc (pr->cp->pid, 1);
2546 prq->sender->last_p2p_replies
2547 [(prq->sender->last_p2p_replies_woff++) % P2P_SUCCESS_LIST_SIZE]
2552 if (NULL != prq->sender->last_client_replies
2553 [(prq->sender->last_client_replies_woff) % CS2P_SUCCESS_LIST_SIZE])
2554 GNUNET_SERVER_client_drop (prq->sender->last_client_replies
2555 [(prq->sender->last_client_replies_woff) % CS2P_SUCCESS_LIST_SIZE]);
2556 prq->sender->last_client_replies
2557 [(prq->sender->last_client_replies_woff++) % CS2P_SUCCESS_LIST_SIZE]
2558 = pr->client_request_list->client_list->client;
2559 GNUNET_SERVER_client_keep (pr->client_request_list->client_list->client);
2562 GNUNET_CRYPTO_hash (prq->data,
2567 case GNUNET_BLOCK_TYPE_DBLOCK:
2568 case GNUNET_BLOCK_TYPE_IBLOCK:
2569 /* only possible reply, stop requesting! */
2570 while (NULL != pr->pending_head)
2571 destroy_pending_message_list_entry (pr->pending_head);
2574 if (pr->client_request_list != NULL)
2575 GNUNET_SERVER_receive_done (pr->client_request_list->client_list->client,
2577 GNUNET_DATASTORE_cancel (pr->qe);
2580 pr->do_remove = GNUNET_YES;
2581 if (pr->task != GNUNET_SCHEDULER_NO_TASK)
2583 GNUNET_SCHEDULER_cancel (sched,
2585 pr->task = GNUNET_SCHEDULER_NO_TASK;
2587 GNUNET_break (GNUNET_YES ==
2588 GNUNET_CONTAINER_multihashmap_remove (query_request_map,
2592 case GNUNET_BLOCK_TYPE_SBLOCK:
2593 if (pr->namespace == NULL)
2598 if (0 != memcmp (pr->namespace,
2600 sizeof (GNUNET_HashCode)))
2602 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2603 _("Reply mismatched in terms of namespace. Discarded.\n"));
2604 return GNUNET_YES; /* wrong namespace */
2606 /* then: fall-through! */
2607 case GNUNET_BLOCK_TYPE_KBLOCK:
2608 case GNUNET_BLOCK_TYPE_NBLOCK:
2611 mingle_hash (&chash, pr->mingle, &mhash);
2612 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (pr->bf,
2615 GNUNET_STATISTICS_update (stats,
2616 gettext_noop ("# duplicate replies discarded (bloomfilter)"),
2620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2621 "Duplicate response `%s', discarding.\n",
2622 GNUNET_h2s (&mhash));
2624 return GNUNET_YES; /* duplicate */
2627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2628 "New response `%s', adding to filter.\n",
2629 GNUNET_h2s (&mhash));
2632 if (pr->client_request_list != NULL)
2634 if (pr->replies_seen_size == pr->replies_seen_off)
2635 GNUNET_array_grow (pr->replies_seen,
2636 pr->replies_seen_size,
2637 pr->replies_seen_size * 2 + 4);
2638 pr->replies_seen[pr->replies_seen_off++] = chash;
2640 if ( (pr->bf == NULL) ||
2641 (pr->client_request_list != NULL) )
2642 refresh_bloomfilter (pr);
2643 GNUNET_CONTAINER_bloomfilter_add (pr->bf,
2650 prq->priority += pr->remaining_priority;
2651 pr->remaining_priority = 0;
2652 if (NULL != pr->client_request_list)
2654 GNUNET_STATISTICS_update (stats,
2655 gettext_noop ("# replies received for local clients"),
2658 cl = pr->client_request_list->client_list;
2659 msize = sizeof (struct PutMessage) + prq->size;
2660 creply = GNUNET_malloc (msize + sizeof (struct ClientResponseMessage));
2661 creply->msize = msize;
2662 creply->client_list = cl;
2663 GNUNET_CONTAINER_DLL_insert_after (cl->res_head,
2667 pm = (struct PutMessage*) &creply[1];
2668 pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
2669 pm->header.size = htons (msize);
2670 pm->type = htonl (prq->type);
2671 pm->expiration = GNUNET_TIME_absolute_hton (prq->expiration);
2672 memcpy (&pm[1], prq->data, prq->size);
2676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2677 "Transmitting result for query `%s' to client\n",
2680 cl->th = GNUNET_SERVER_notify_transmit_ready (cl->client,
2682 GNUNET_TIME_UNIT_FOREVER_REL,
2683 &transmit_to_client,
2686 GNUNET_break (cl->th != NULL);
2689 prq->finished = GNUNET_YES;
2690 destroy_pending_request (pr);
2697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2698 "Transmitting result for query `%s' to other peer (PID=%u)\n",
2700 (unsigned int) cp->pid);
2702 GNUNET_STATISTICS_update (stats,
2703 gettext_noop ("# replies received for other peers"),
2706 msize = sizeof (struct PutMessage) + prq->size;
2707 reply = GNUNET_malloc (msize + sizeof (struct PendingMessage));
2708 reply->cont = &transmit_reply_continuation;
2709 reply->cont_cls = pr;
2710 reply->msize = msize;
2711 reply->priority = UINT32_MAX; /* send replies first! */
2712 pm = (struct PutMessage*) &reply[1];
2713 pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
2714 pm->header.size = htons (msize);
2715 pm->type = htonl (prq->type);
2716 pm->expiration = GNUNET_TIME_absolute_hton (prq->expiration);
2717 memcpy (&pm[1], prq->data, prq->size);
2718 add_to_pending_messages_for_peer (cp, reply, pr);
2725 * Continuation called to notify client about result of the
2728 * @param cls closure
2729 * @param success GNUNET_SYSERR on failure
2730 * @param msg NULL on success, otherwise an error message
2733 put_migration_continuation (void *cls,
2742 * Handle P2P "PUT" message.
2744 * @param cls closure, always NULL
2745 * @param other the other peer involved (sender or receiver, NULL
2746 * for loopback messages where we are both sender and receiver)
2747 * @param message the actual message
2748 * @param latency reported latency of the connection with 'other'
2749 * @param distance reported distance (DV) to 'other'
2750 * @return GNUNET_OK to keep the connection open,
2751 * GNUNET_SYSERR to close it (signal serious error)
2754 handle_p2p_put (void *cls,
2755 const struct GNUNET_PeerIdentity *other,
2756 const struct GNUNET_MessageHeader *message,
2757 struct GNUNET_TIME_Relative latency,
2760 const struct PutMessage *put;
2763 enum GNUNET_BLOCK_Type type;
2764 struct GNUNET_TIME_Absolute expiration;
2765 GNUNET_HashCode query;
2766 struct ProcessReplyClosure prq;
2767 const struct SBlock *sb;
2769 msize = ntohs (message->size);
2770 if (msize < sizeof (struct PutMessage))
2773 return GNUNET_SYSERR;
2775 put = (const struct PutMessage*) message;
2776 dsize = msize - sizeof (struct PutMessage);
2777 type = ntohl (put->type);
2778 expiration = GNUNET_TIME_absolute_ntoh (put->expiration);
2781 GNUNET_BLOCK_check_block (type,
2786 GNUNET_break_op (0);
2787 return GNUNET_SYSERR;
2789 if (type == GNUNET_BLOCK_TYPE_ONDEMAND)
2790 return GNUNET_SYSERR;
2791 if (GNUNET_BLOCK_TYPE_SBLOCK == type)
2793 sb = (const struct SBlock*) &put[1];
2794 GNUNET_CRYPTO_hash (&sb->subspace,
2795 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2801 "Received result for query `%s' from peer `%4s'\n",
2802 GNUNET_h2s (&query),
2803 GNUNET_i2s (other));
2805 GNUNET_STATISTICS_update (stats,
2806 gettext_noop ("# replies received (overall)"),
2809 /* now, lookup 'query' */
2810 prq.data = (const void*) &put[1];
2812 prq.sender = GNUNET_CONTAINER_multihashmap_get (connected_peers,
2813 &other->hashPubKey);
2818 prq.expiration = expiration;
2820 prq.finished = GNUNET_NO;
2821 GNUNET_CONTAINER_multihashmap_get_multiple (query_request_map,
2825 if (prq.sender != NULL)
2827 prq.sender->inc_preference += CONTENT_BANDWIDTH_VALUE + 1000 * prq.priority;
2828 prq.sender->trust += prq.priority;
2830 if (GNUNET_YES == active_migration)
2833 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2834 "Replicating result for query `%s' with priority %u\n",
2835 GNUNET_h2s (&query),
2838 GNUNET_DATASTORE_put (dsh,
2839 0, &query, dsize, &put[1],
2840 type, prq.priority, 1 /* anonymity */,
2842 1 + prq.priority, MAX_DATASTORE_QUEUE,
2843 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
2844 &put_migration_continuation,
2851 /* **************************** P2P GET Handling ************************ */
2855 * Closure for 'check_duplicate_request_{peer,client}'.
2857 struct CheckDuplicateRequestClosure
2860 * The new request we should check if it already exists.
2862 const struct PendingRequest *pr;
2865 * Existing request found by the checker, NULL if none.
2867 struct PendingRequest *have;
2872 * Iterator over entries in the 'query_request_map' that
2873 * tries to see if we have the same request pending from
2874 * the same client already.
2876 * @param cls closure (our 'struct CheckDuplicateRequestClosure')
2877 * @param key current key code (query, ignored, must match)
2878 * @param value value in the hash map (a 'struct PendingRequest'
2879 * that already exists)
2880 * @return GNUNET_YES if we should continue to
2881 * iterate (no match yet)
2882 * GNUNET_NO if not (match found).
2885 check_duplicate_request_client (void *cls,
2886 const GNUNET_HashCode * key,
2889 struct CheckDuplicateRequestClosure *cdc = cls;
2890 struct PendingRequest *have = value;
2892 if (have->client_request_list == NULL)
2894 if ( (cdc->pr->client_request_list->client_list->client == have->client_request_list->client_list->client) &&
2905 * We're processing (local) results for a search request
2906 * from another peer. Pass applicable results to the
2907 * peer and if we are done either clean up (operation
2908 * complete) or forward to other peers (more results possible).
2910 * @param cls our closure (struct LocalGetContext)
2911 * @param key key for the content
2912 * @param size number of bytes in data
2913 * @param data content stored
2914 * @param type type of the content
2915 * @param priority priority of the content
2916 * @param anonymity anonymity-level for the content
2917 * @param expiration expiration time for the content
2918 * @param uid unique identifier for the datum;
2919 * maybe 0 if no unique identifier is available
2922 process_local_reply (void *cls,
2923 const GNUNET_HashCode * key,
2926 enum GNUNET_BLOCK_Type type,
2929 struct GNUNET_TIME_Absolute
2933 struct PendingRequest *pr = cls;
2934 struct ProcessReplyClosure prq;
2935 struct CheckDuplicateRequestClosure cdrc;
2936 const struct SBlock *sb;
2937 GNUNET_HashCode dhash;
2938 GNUNET_HashCode mhash;
2939 GNUNET_HashCode query;
2944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2945 "Done processing local replies, forwarding request to other peers.\n");
2948 if (pr->client_request_list != NULL)
2950 GNUNET_SERVER_receive_done (pr->client_request_list->client_list->client,
2952 /* Figure out if this is a duplicate request and possibly
2953 merge 'struct PendingRequest' entries */
2956 GNUNET_CONTAINER_multihashmap_get_multiple (query_request_map,
2958 &check_duplicate_request_client,
2960 if (cdrc.have != NULL)
2963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2964 "Received request for block `%s' twice from client, will only request once.\n",
2965 GNUNET_h2s (&pr->query));
2968 destroy_pending_request (pr);
2973 /* no more results */
2974 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
2975 pr->task = GNUNET_SCHEDULER_add_now (sched,
2976 &forward_request_task,
2981 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2982 "New local response to `%s' of type %u.\n",
2986 if (type == GNUNET_BLOCK_TYPE_ONDEMAND)
2989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2990 "Found ONDEMAND block, performing on-demand encoding\n");
2992 GNUNET_STATISTICS_update (stats,
2993 gettext_noop ("# on-demand blocks matched requests"),
2997 GNUNET_FS_handle_on_demand_block (key, size, data, type, priority,
2998 anonymity, expiration, uid,
2999 &process_local_reply,
3002 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
3005 /* check for duplicates */
3006 GNUNET_CRYPTO_hash (data, size, &dhash);
3007 mingle_hash (&dhash,
3010 if ( (pr->bf != NULL) &&
3012 GNUNET_CONTAINER_bloomfilter_test (pr->bf,
3016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3017 "Result from datastore filtered by bloomfilter (duplicate).\n");
3019 GNUNET_STATISTICS_update (stats,
3020 gettext_noop ("# results filtered by query bloomfilter"),
3024 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
3028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3029 "Found result for query `%s' in local datastore\n",
3032 GNUNET_STATISTICS_update (stats,
3033 gettext_noop ("# results found locally"),
3036 pr->results_found++;
3037 memset (&prq, 0, sizeof (prq));
3039 prq.expiration = expiration;
3041 if (GNUNET_BLOCK_TYPE_SBLOCK == type)
3043 sb = (const struct SBlock*) data;
3044 GNUNET_CRYPTO_hash (&sb->subspace,
3045 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3048 if (GNUNET_OK != GNUNET_BLOCK_check_block (type,
3054 GNUNET_DATASTORE_remove (dsh,
3058 GNUNET_TIME_UNIT_FOREVER_REL,
3060 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
3064 prq.priority = priority;
3065 prq.finished = GNUNET_NO;
3066 process_reply (&prq, key, pr);
3067 if (prq.finished == GNUNET_YES)
3070 return; /* done here */
3071 if ( (type == GNUNET_BLOCK_TYPE_DBLOCK) ||
3072 (type == GNUNET_BLOCK_TYPE_IBLOCK) )
3074 GNUNET_DATASTORE_get_next (dsh, GNUNET_NO);
3077 if ( (pr->client_request_list == NULL) &&
3078 ( (GNUNET_YES == test_load_too_high()) ||
3079 (pr->results_found > 5 + 2 * pr->priority) ) )
3082 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3083 "Load too high, done with request\n");
3085 GNUNET_STATISTICS_update (stats,
3086 gettext_noop ("# processing result set cut short due to load"),
3089 GNUNET_DATASTORE_get_next (dsh, GNUNET_NO);
3092 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
3097 * We've received a request with the specified priority. Bound it
3098 * according to how much we trust the given peer.
3100 * @param prio_in requested priority
3101 * @param cp the peer making the request
3102 * @return effective priority
3105 bound_priority (uint32_t prio_in,
3106 struct ConnectedPeer *cp)
3108 #define N ((double)128.0)
3113 ld = test_load_too_high ();
3114 if (ld == GNUNET_SYSERR)
3115 return 0; /* excess resources */
3116 ret = change_host_trust (cp, prio_in);
3119 if (ret > current_priorities + N)
3120 rret = current_priorities + N;
3124 = (current_priorities * (N-1) + rret)/N;
3132 * Iterator over entries in the 'query_request_map' that
3133 * tries to see if we have the same request pending from
3134 * the same peer already.
3136 * @param cls closure (our 'struct CheckDuplicateRequestClosure')
3137 * @param key current key code (query, ignored, must match)
3138 * @param value value in the hash map (a 'struct PendingRequest'
3139 * that already exists)
3140 * @return GNUNET_YES if we should continue to
3141 * iterate (no match yet)
3142 * GNUNET_NO if not (match found).
3145 check_duplicate_request_peer (void *cls,
3146 const GNUNET_HashCode * key,
3149 struct CheckDuplicateRequestClosure *cdc = cls;
3150 struct PendingRequest *have = value;
3152 if (cdc->pr->target_pid == have->target_pid)
3162 * Handle P2P "GET" request.
3164 * @param cls closure, always NULL
3165 * @param other the other peer involved (sender or receiver, NULL
3166 * for loopback messages where we are both sender and receiver)
3167 * @param message the actual message
3168 * @param latency reported latency of the connection with 'other'
3169 * @param distance reported distance (DV) to 'other'
3170 * @return GNUNET_OK to keep the connection open,
3171 * GNUNET_SYSERR to close it (signal serious error)
3174 handle_p2p_get (void *cls,
3175 const struct GNUNET_PeerIdentity *other,
3176 const struct GNUNET_MessageHeader *message,
3177 struct GNUNET_TIME_Relative latency,
3180 struct PendingRequest *pr;
3181 struct ConnectedPeer *cp;
3182 struct ConnectedPeer *cps;
3183 struct CheckDuplicateRequestClosure cdc;
3184 struct GNUNET_TIME_Relative timeout;
3186 const struct GetMessage *gm;
3188 const GNUNET_HashCode *opt;
3191 uint32_t ttl_decrement;
3192 enum GNUNET_BLOCK_Type type;
3196 msize = ntohs(message->size);
3197 if (msize < sizeof (struct GetMessage))
3199 GNUNET_break_op (0);
3200 return GNUNET_SYSERR;
3202 gm = (const struct GetMessage*) message;
3203 type = ntohl (gm->type);
3206 case GNUNET_BLOCK_TYPE_ANY:
3207 case GNUNET_BLOCK_TYPE_DBLOCK:
3208 case GNUNET_BLOCK_TYPE_IBLOCK:
3209 case GNUNET_BLOCK_TYPE_KBLOCK:
3210 case GNUNET_BLOCK_TYPE_SBLOCK:
3213 GNUNET_break_op (0);
3214 return GNUNET_SYSERR;
3216 bm = ntohl (gm->hash_bitmap);
3224 if (msize < sizeof (struct GetMessage) + bits * sizeof (GNUNET_HashCode))
3226 GNUNET_break_op (0);
3227 return GNUNET_SYSERR;
3229 opt = (const GNUNET_HashCode*) &gm[1];
3230 bfsize = msize - sizeof (struct GetMessage) + bits * sizeof (GNUNET_HashCode);
3231 bm = ntohl (gm->hash_bitmap);
3232 if ( (0 != (bm & GET_MESSAGE_BIT_SKS_NAMESPACE)) &&
3233 (type != GNUNET_BLOCK_TYPE_SBLOCK) )
3235 GNUNET_break_op (0);
3236 return GNUNET_SYSERR;
3239 cps = GNUNET_CONTAINER_multihashmap_get (connected_peers,
3240 &other->hashPubKey);
3243 /* peer must have just disconnected */
3244 GNUNET_STATISTICS_update (stats,
3245 gettext_noop ("# requests dropped due to initiator not being connected"),
3248 return GNUNET_SYSERR;
3250 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
3251 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
3258 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
3259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3260 "Failed to find RETURN-TO peer `%4s' in connection set. Dropping query.\n",
3261 GNUNET_i2s ((const struct GNUNET_PeerIdentity*) &opt[bits-1]));
3264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3265 "Failed to find peer `%4s' in connection set. Dropping query.\n",
3266 GNUNET_i2s (other));
3268 GNUNET_STATISTICS_update (stats,
3269 gettext_noop ("# requests dropped due to missing reverse route"),
3272 /* FIXME: try connect? */
3275 /* note that we can really only check load here since otherwise
3276 peers could find out that we are overloaded by not being
3277 disconnected after sending us a malformed query... */
3279 /* FIXME: query priority should play
3280 a major role here! */
3281 ld = test_load_too_high ();
3282 if (GNUNET_YES == ld)
3285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3286 "Dropping query from `%s', this peer is too busy.\n",
3287 GNUNET_i2s (other));
3289 GNUNET_STATISTICS_update (stats,
3290 gettext_noop ("# requests dropped due to high load"),
3295 /* FIXME: if ld == GNUNET_NO, forward
3296 instead of indirecting! */
3299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3300 "Received request for `%s' of type %u from peer `%4s' with flags %u\n",
3301 GNUNET_h2s (&gm->query),
3302 (unsigned int) type,
3306 have_ns = (0 != (bm & GET_MESSAGE_BIT_SKS_NAMESPACE));
3307 pr = GNUNET_malloc (sizeof (struct PendingRequest) +
3308 (have_ns ? sizeof(GNUNET_HashCode) : 0));
3311 pr->namespace = (GNUNET_HashCode*) &pr[1];
3312 memcpy (&pr[1], &opt[bits++], sizeof (GNUNET_HashCode));
3315 pr->mingle = ntohl (gm->filter_mutator);
3316 if (0 != (bm & GET_MESSAGE_BIT_TRANSMIT_TO))
3317 pr->target_pid = GNUNET_PEER_intern ((const struct GNUNET_PeerIdentity*) &opt[bits++]);
3319 pr->anonymity_level = 1;
3320 pr->priority = bound_priority (ntohl (gm->priority), cps);
3321 pr->ttl = bound_ttl (ntohl (gm->ttl), pr->priority);
3322 pr->query = gm->query;
3323 /* decrement ttl (always) */
3324 ttl_decrement = 2 * TTL_DECREMENT +
3325 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
3327 if ( (pr->ttl < 0) &&
3328 (((int32_t)(pr->ttl - ttl_decrement)) > 0) )
3331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3332 "Dropping query from `%s' due to TTL underflow (%d - %u).\n",
3337 GNUNET_STATISTICS_update (stats,
3338 gettext_noop ("# requests dropped due TTL underflow"),
3341 /* integer underflow => drop (should be very rare)! */
3345 pr->ttl -= ttl_decrement;
3346 pr->start_time = GNUNET_TIME_absolute_get ();
3348 /* get bloom filter */
3351 pr->bf = GNUNET_CONTAINER_bloomfilter_init ((const char*) &opt[bits],
3354 pr->bf_size = bfsize;
3359 GNUNET_CONTAINER_multihashmap_get_multiple (query_request_map,
3361 &check_duplicate_request_peer,
3363 if (cdc.have != NULL)
3365 if (cdc.have->start_time.value + cdc.have->ttl >=
3366 pr->start_time.value + pr->ttl)
3368 /* existing request has higher TTL, drop new one! */
3369 cdc.have->priority += pr->priority;
3370 destroy_pending_request (pr);
3372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3373 "Have existing request with higher TTL, dropping new request.\n",
3374 GNUNET_i2s (other));
3376 GNUNET_STATISTICS_update (stats,
3377 gettext_noop ("# requests dropped due to higher-TTL request"),
3384 /* existing request has lower TTL, drop old one! */
3385 pr->priority += cdc.have->priority;
3386 /* Possible optimization: if we have applicable pending
3387 replies in 'cdc.have', we might want to move those over
3388 (this is a really rare special-case, so it is not clear
3389 that this would be worth it) */
3390 destroy_pending_request (cdc.have);
3391 /* keep processing 'pr'! */
3396 GNUNET_break (GNUNET_OK ==
3397 GNUNET_CONTAINER_multihashmap_put (query_request_map,
3400 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
3401 GNUNET_break (GNUNET_OK ==
3402 GNUNET_CONTAINER_multihashmap_put (peer_request_map,
3405 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
3407 pr->hnode = GNUNET_CONTAINER_heap_insert (requests_by_expiration_heap,
3409 pr->start_time.value + pr->ttl);
3411 GNUNET_STATISTICS_update (stats,
3412 gettext_noop ("# P2P searches received"),
3415 GNUNET_STATISTICS_update (stats,
3416 gettext_noop ("# P2P searches active"),
3420 /* calculate change in traffic preference */
3421 cps->inc_preference += pr->priority * 1000 + QUERY_BANDWIDTH_VALUE;
3422 /* process locally */
3423 if (type == GNUNET_BLOCK_TYPE_DBLOCK)
3424 type = GNUNET_BLOCK_TYPE_ANY; /* to get on-demand as well */
3425 timeout = GNUNET_TIME_relative_multiply (BASIC_DATASTORE_REQUEST_DELAY,
3426 (pr->priority + 1));
3427 pr->qe = GNUNET_DATASTORE_get (dsh,
3431 MAX_DATASTORE_QUEUE,
3433 &process_local_reply,
3436 /* Are multiple results possible? If so, start processing remotely now! */
3439 case GNUNET_BLOCK_TYPE_DBLOCK:
3440 case GNUNET_BLOCK_TYPE_IBLOCK:
3441 /* only one result, wait for datastore */
3444 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
3445 pr->task = GNUNET_SCHEDULER_add_now (sched,
3446 &forward_request_task,
3450 /* make sure we don't track too many requests */
3451 if (GNUNET_CONTAINER_heap_get_size (requests_by_expiration_heap) > max_pending_requests)
3453 pr = GNUNET_CONTAINER_heap_peek (requests_by_expiration_heap);
3454 destroy_pending_request (pr);
3460 /* **************************** CS GET Handling ************************ */
3464 * Handle START_SEARCH-message (search request from client).
3466 * @param cls closure
3467 * @param client identification of the client
3468 * @param message the actual message
3471 handle_start_search (void *cls,
3472 struct GNUNET_SERVER_Client *client,
3473 const struct GNUNET_MessageHeader *message)
3475 static GNUNET_HashCode all_zeros;
3476 const struct SearchMessage *sm;
3477 struct ClientList *cl;
3478 struct ClientRequestList *crl;
3479 struct PendingRequest *pr;
3482 enum GNUNET_BLOCK_Type type;
3484 msize = ntohs (message->size);
3485 if ( (msize < sizeof (struct SearchMessage)) ||
3486 (0 != (msize - sizeof (struct SearchMessage)) % sizeof (GNUNET_HashCode)) )
3489 GNUNET_SERVER_receive_done (client,
3493 GNUNET_STATISTICS_update (stats,
3494 gettext_noop ("# client searches received"),
3497 sc = (msize - sizeof (struct SearchMessage)) / sizeof (GNUNET_HashCode);
3498 sm = (const struct SearchMessage*) message;
3499 type = ntohl (sm->type);
3501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3502 "Received request for `%s' of type %u from local client\n",
3503 GNUNET_h2s (&sm->query),
3504 (unsigned int) type);
3508 case GNUNET_BLOCK_TYPE_ANY:
3509 case GNUNET_BLOCK_TYPE_DBLOCK:
3510 case GNUNET_BLOCK_TYPE_IBLOCK:
3511 case GNUNET_BLOCK_TYPE_KBLOCK:
3512 case GNUNET_BLOCK_TYPE_SBLOCK:
3513 case GNUNET_BLOCK_TYPE_NBLOCK:
3517 GNUNET_SERVER_receive_done (client,
3523 while ( (cl != NULL) &&
3524 (cl->client != client) )
3528 cl = GNUNET_malloc (sizeof (struct ClientList));
3529 cl->client = client;
3530 GNUNET_SERVER_client_keep (client);
3531 cl->next = client_list;
3534 /* detect duplicate KBLOCK requests */
3535 if ( (type == GNUNET_BLOCK_TYPE_KBLOCK) ||
3536 (type == GNUNET_BLOCK_TYPE_NBLOCK) ||
3537 (type == GNUNET_BLOCK_TYPE_ANY) )
3540 while ( (crl != NULL) &&
3541 ( (0 != memcmp (&crl->req->query,
3543 sizeof (GNUNET_HashCode))) ||
3544 (crl->req->type != type) ) )
3549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3550 "Have existing request, merging content-seen lists.\n");
3553 /* Duplicate request (used to send long list of
3554 known/blocked results); merge 'pr->replies_seen'
3555 and update bloom filter */
3556 GNUNET_array_grow (pr->replies_seen,
3557 pr->replies_seen_size,
3558 pr->replies_seen_off + sc);
3559 memcpy (&pr->replies_seen[pr->replies_seen_off],
3561 sc * sizeof (GNUNET_HashCode));
3562 pr->replies_seen_off += sc;
3563 refresh_bloomfilter (pr);
3564 GNUNET_STATISTICS_update (stats,
3565 gettext_noop ("# client searches updated (merged content seen list)"),
3568 GNUNET_SERVER_receive_done (client,
3573 GNUNET_STATISTICS_update (stats,
3574 gettext_noop ("# client searches active"),
3577 pr = GNUNET_malloc (sizeof (struct PendingRequest) +
3578 ((type == GNUNET_BLOCK_TYPE_SBLOCK) ? sizeof(GNUNET_HashCode) : 0));
3579 crl = GNUNET_malloc (sizeof (struct ClientRequestList));
3580 memset (crl, 0, sizeof (struct ClientRequestList));
3581 crl->client_list = cl;
3582 GNUNET_CONTAINER_DLL_insert (cl->rl_head,
3587 pr->client_request_list = crl;
3588 GNUNET_array_grow (pr->replies_seen,
3589 pr->replies_seen_size,
3591 memcpy (pr->replies_seen,
3593 sc * sizeof (GNUNET_HashCode));
3594 pr->replies_seen_off = sc;
3595 pr->anonymity_level = ntohl (sm->anonymity_level);
3596 refresh_bloomfilter (pr);
3597 pr->query = sm->query;
3598 if (0 == (1 & ntohl (sm->options)))
3599 pr->local_only = GNUNET_NO;
3601 pr->local_only = GNUNET_YES;
3604 case GNUNET_BLOCK_TYPE_DBLOCK:
3605 case GNUNET_BLOCK_TYPE_IBLOCK:
3606 if (0 != memcmp (&sm->target,
3608 sizeof (GNUNET_HashCode)))
3609 pr->target_pid = GNUNET_PEER_intern ((const struct GNUNET_PeerIdentity*) &sm->target);
3611 case GNUNET_BLOCK_TYPE_SBLOCK:
3612 pr->namespace = (GNUNET_HashCode*) &pr[1];
3613 memcpy (&pr[1], &sm->target, sizeof (GNUNET_HashCode));
3618 GNUNET_break (GNUNET_OK ==
3619 GNUNET_CONTAINER_multihashmap_put (query_request_map,
3622 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
3623 if (type == GNUNET_BLOCK_TYPE_DBLOCK)
3624 type = GNUNET_BLOCK_TYPE_ANY; /* get on-demand blocks too! */
3625 pr->qe = GNUNET_DATASTORE_get (dsh,
3629 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
3630 &process_local_reply,
3635 /* **************************** Startup ************************ */
3638 * Process fs requests.
3640 * @param s scheduler to use
3641 * @param server the initialized server
3642 * @param c configuration to use
3645 main_init (struct GNUNET_SCHEDULER_Handle *s,
3646 struct GNUNET_SERVER_Handle *server,
3647 const struct GNUNET_CONFIGURATION_Handle *c)
3649 static const struct GNUNET_CORE_MessageHandler p2p_handlers[] =
3652 GNUNET_MESSAGE_TYPE_FS_GET, 0 },
3654 GNUNET_MESSAGE_TYPE_FS_PUT, 0 },
3657 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
3658 {&GNUNET_FS_handle_index_start, NULL,
3659 GNUNET_MESSAGE_TYPE_FS_INDEX_START, 0},
3660 {&GNUNET_FS_handle_index_list_get, NULL,
3661 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_GET, sizeof(struct GNUNET_MessageHeader) },
3662 {&GNUNET_FS_handle_unindex, NULL, GNUNET_MESSAGE_TYPE_FS_UNINDEX,
3663 sizeof (struct UnindexMessage) },
3664 {&handle_start_search, NULL, GNUNET_MESSAGE_TYPE_FS_START_SEARCH,
3668 unsigned long long enc = 128;
3672 stats = GNUNET_STATISTICS_create (sched, "fs", cfg);
3673 min_migration_delay = GNUNET_TIME_UNIT_SECONDS;
3675 GNUNET_CONFIGURATION_get_value_number (cfg,
3677 "MAX_PENDING_REQUESTS",
3678 &max_pending_requests)) ||
3680 GNUNET_CONFIGURATION_get_value_number (cfg,
3682 "EXPECTED_NEIGHBOUR_COUNT",
3685 GNUNET_CONFIGURATION_get_value_time (cfg,
3687 "MIN_MIGRATION_DELAY",
3688 &min_migration_delay)) )
3690 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3691 _("Configuration fails to specify certain parameters, assuming default values."));
3693 connected_peers = GNUNET_CONTAINER_multihashmap_create (enc);
3694 query_request_map = GNUNET_CONTAINER_multihashmap_create (max_pending_requests);
3695 peer_request_map = GNUNET_CONTAINER_multihashmap_create (enc);
3696 requests_by_expiration_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
3697 core = GNUNET_CORE_connect (sched,
3699 GNUNET_TIME_UNIT_FOREVER_REL,
3702 &peer_connect_handler,
3703 &peer_disconnect_handler,
3710 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3711 _("Failed to connect to `%s' service.\n"),
3713 GNUNET_CONTAINER_multihashmap_destroy (connected_peers);
3714 connected_peers = NULL;
3715 GNUNET_CONTAINER_multihashmap_destroy (query_request_map);
3716 query_request_map = NULL;
3717 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
3718 requests_by_expiration_heap = NULL;
3719 GNUNET_CONTAINER_multihashmap_destroy (peer_request_map);
3720 peer_request_map = NULL;
3723 GNUNET_DATASTORE_disconnect (dsh, GNUNET_NO);
3726 return GNUNET_SYSERR;
3728 /* FIXME: distinguish between sending and storing in options? */
3729 if (active_migration)
3731 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3732 _("Content migration is enabled, will start to gather data\n"));
3733 consider_migration_gathering ();
3735 GNUNET_SERVER_disconnect_notify (server,
3736 &handle_client_disconnect,
3738 GNUNET_assert (GNUNET_OK ==
3739 GNUNET_CONFIGURATION_get_value_filename (cfg,
3743 GNUNET_DISK_directory_create (trustDirectory);
3744 GNUNET_SCHEDULER_add_with_priority (sched,
3745 GNUNET_SCHEDULER_PRIORITY_HIGH,
3746 &cron_flush_trust, NULL);
3749 GNUNET_SERVER_add_handlers (server, handlers);
3750 GNUNET_SCHEDULER_add_delayed (sched,
3751 GNUNET_TIME_UNIT_FOREVER_REL,
3759 * Process fs requests.
3761 * @param cls closure
3762 * @param sched scheduler to use
3763 * @param server the initialized server
3764 * @param cfg configuration to use
3768 struct GNUNET_SCHEDULER_Handle *sched,
3769 struct GNUNET_SERVER_Handle *server,
3770 const struct GNUNET_CONFIGURATION_Handle *cfg)
3772 active_migration = GNUNET_CONFIGURATION_get_value_yesno (cfg,
3775 dsh = GNUNET_DATASTORE_connect (cfg,
3779 GNUNET_SCHEDULER_shutdown (sched);
3782 if ( (GNUNET_OK != GNUNET_FS_indexing_init (sched, cfg, dsh)) ||
3783 (GNUNET_OK != main_init (sched, server, cfg)) )
3785 GNUNET_SCHEDULER_shutdown (sched);
3786 GNUNET_DATASTORE_disconnect (dsh, GNUNET_NO);
3794 * The main function for the fs service.
3796 * @param argc number of arguments from the command line
3797 * @param argv command line arguments
3798 * @return 0 ok, 1 on error
3801 main (int argc, char *const *argv)
3803 return (GNUNET_OK ==
3804 GNUNET_SERVICE_run (argc,
3807 GNUNET_SERVICE_OPTION_NONE,
3808 &run, NULL)) ? 0 : 1;
3811 /* end of gnunet-service-fs.c */