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 * - collect traffic data for anonymity levels > 1
28 * - implement transmission restrictions for anonymity level > 1
33 #include "gnunet_constants.h"
34 #include "gnunet_core_service.h"
35 #include "gnunet_dht_service.h"
36 #include "gnunet_datastore_service.h"
37 #include "gnunet_load_lib.h"
38 #include "gnunet_peer_lib.h"
39 #include "gnunet_protocols.h"
40 #include "gnunet_signatures.h"
41 #include "gnunet_statistics_service.h"
42 #include "gnunet_util_lib.h"
43 #include "gnunet-service-fs_indexing.h"
46 #define DEBUG_FS GNUNET_NO
49 * Should we introduce random latency in processing? Required for proper
50 * implementation of GAP, but can be disabled for performance evaluation of
51 * the basic routing algorithm.
53 * Note that with delays enabled, performance can be significantly lower
54 * (several orders of magnitude in 2-peer test runs); if you want to
55 * measure throughput of other components, set this to NO. Also, you
56 * might want to consider changing 'RETRY_PROBABILITY_INV' to 1 for
57 * a rather wasteful mode of operation (that might still get the highest
58 * throughput overall).
60 * Performance measurements (for 50 MB file, 2 peers):
62 * - Without delays: 3300 kb/s
63 * - With delays: 101 kb/s
65 #define SUPPORT_DELAYS GNUNET_NO
68 * Size for the hash map for DHT requests from the FS
69 * service. Should be about the number of concurrent
70 * DHT requests we plan to make.
72 #define FS_DHT_HT_SIZE 1024
75 * At what frequency should our datastore load decrease
76 * automatically (since if we don't use it, clearly the
77 * load must be going down).
79 #define DATASTORE_LOAD_AUTODECLINE GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 250)
82 * How often do we flush trust values to disk?
84 #define TRUST_FLUSH_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
87 * How often do we at most PUT content into the DHT?
89 #define MAX_DHT_PUT_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
92 * Inverse of the probability that we will submit the same query
93 * to the same peer again. If the same peer already got the query
94 * repeatedly recently, the probability is multiplied by the inverse
95 * of this number each time. Note that we only try about every TTL_DECREMENT/2
96 * plus MAX_CORK_DELAY (so roughly every 3.5s).
98 * Note that this factor is a key influence to performance in small
99 * networks (especially test networks of 2 peers) because if there is
100 * only a single peer with the data, this value will determine how
101 * soon we might re-try. For example, a value of 3 can result in
102 * 1.7 MB/s transfer rates for a 10 MB file when a value of 1 would
103 * give us 5 MB/s. OTOH, obviously re-trying the same peer can be
104 * rather inefficient in larger networks, hence picking 1 is in
105 * general not the best choice.
107 * Performance measurements (for 50 MB file, 2 peers, no delays):
109 * - 1: 3300 kb/s (consistently)
110 * - 3: 2046 kb/s, 754 kb/s, 3490 kb/s
111 * - 5: 759 kb/s, 968 kb/s, 1160 kb/s
113 * Note that this does NOT mean that the value should be 1 since
114 * a 2-peer network is far from representative here (and this fails
115 * to take into consideration bandwidth wasted by repeatedly
116 * sending queries to peers that don't have the content). Also,
117 * it is expected that higher values lead to more inconsistent
118 * measurements since this only affects lost messages towards the
119 * end of the download.
121 * Finally, we should probably consider changing this and making
122 * it dependent on the number of connected peers or a related
123 * metric (bad magic constants...).
125 #define RETRY_PROBABILITY_INV 1
128 * What is the maximum delay for a P2P FS message (in our interaction
129 * with core)? FS-internal delays are another story. The value is
130 * chosen based on the 32k block size. Given that peers typcially
131 * have at least 1 kb/s bandwidth, 45s waits give us a chance to
132 * transmit one message even to the lowest-bandwidth peers.
134 #define MAX_TRANSMIT_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 45)
137 * Maximum number of requests (from other peers, overall) that we're
138 * willing to have pending at any given point in time. Can be changed
139 * via the configuration file (32k is just the default).
141 static unsigned long long max_pending_requests = (32 * 1024);
145 * Information we keep for each pending reply. The
146 * actual message follows at the end of this struct.
148 struct PendingMessage;
151 * Function called upon completion of a transmission.
154 * @param pid ID of receiving peer, 0 on transmission error
156 typedef void (*TransmissionContinuation)(void * cls,
157 GNUNET_PEER_Id tpid);
161 * Information we keep for each pending message (GET/PUT). The
162 * actual message follows at the end of this struct.
164 struct PendingMessage
167 * This is a doubly-linked list of messages to the same peer.
169 struct PendingMessage *next;
172 * This is a doubly-linked list of messages to the same peer.
174 struct PendingMessage *prev;
177 * Entry in pending message list for this pending message.
179 struct PendingMessageList *pml;
182 * Function to call immediately once we have transmitted this
185 TransmissionContinuation cont;
193 * Do not transmit this pending message until this deadline.
195 struct GNUNET_TIME_Absolute delay_until;
198 * Size of the reply; actual reply message follows
199 * at the end of this struct.
204 * How important is this message for us?
212 * Information about a peer that we are connected to.
213 * We track data that is useful for determining which
214 * peers should receive our requests. We also keep
215 * a list of messages to transmit to this peer.
221 * List of the last clients for which this peer successfully
224 struct GNUNET_SERVER_Client *last_client_replies[CS2P_SUCCESS_LIST_SIZE];
227 * List of the last PIDs for which
228 * this peer successfully answered a query;
229 * We use 0 to indicate no successful reply.
231 GNUNET_PEER_Id last_p2p_replies[P2P_SUCCESS_LIST_SIZE];
234 * Average delay between sending the peer a request and
235 * getting a reply (only calculated over the requests for
236 * which we actually got a reply). Calculated
237 * as a moving average: new_delay = ((n-1)*last_delay+curr_delay) / n
239 struct GNUNET_TIME_Relative avg_delay;
242 * Point in time until which this peer does not want us to migrate content
245 struct GNUNET_TIME_Absolute migration_blocked;
248 * Time until when we blocked this peer from migrating
251 struct GNUNET_TIME_Absolute last_migration_block;
254 * Transmission times for the last MAX_QUEUE_PER_PEER
255 * requests for this peer. Used as a ring buffer, current
256 * offset is stored in 'last_request_times_off'. If the
257 * oldest entry is more recent than the 'avg_delay', we should
258 * not send any more requests right now.
260 struct GNUNET_TIME_Absolute last_request_times[MAX_QUEUE_PER_PEER];
263 * Handle for an active request for transmission to this
266 struct GNUNET_CORE_TransmitHandle *cth;
269 * Messages (replies, queries, content migration) we would like to
270 * send to this peer in the near future. Sorted by priority, head.
272 struct PendingMessage *pending_messages_head;
275 * Messages (replies, queries, content migration) we would like to
276 * send to this peer in the near future. Sorted by priority, tail.
278 struct PendingMessage *pending_messages_tail;
281 * How long does it typically take for us to transmit a message
282 * to this peer? (delay between the request being issued and
283 * the callback being invoked).
285 struct GNUNET_LOAD_Value *transmission_delay;
288 * Context of our GNUNET_CORE_peer_change_preference call (or NULL).
290 struct GNUNET_CORE_InformationRequestContext *irc;
293 * Request for which 'irc' is currently active (or NULL).
295 struct PendingRequest *pr;
298 * Time when the last transmission request was issued.
300 struct GNUNET_TIME_Absolute last_transmission_request_start;
303 * ID of delay task for scheduling transmission.
305 GNUNET_SCHEDULER_TaskIdentifier delayed_transmission_request_task;
308 * Average priority of successful replies. Calculated
309 * as a moving average: new_avg = ((n-1)*last_avg+curr_prio) / n
314 * Increase in traffic preference still to be submitted
315 * to the core service for this peer.
317 uint64_t inc_preference;
320 * Trust rating for this peer
325 * Trust rating for this peer on disk.
330 * The peer's identity.
335 * Size of the linked list of 'pending_messages'.
337 unsigned int pending_requests;
340 * Which offset in "last_p2p_replies" will be updated next?
341 * (we go round-robin).
343 unsigned int last_p2p_replies_woff;
346 * Which offset in "last_client_replies" will be updated next?
347 * (we go round-robin).
349 unsigned int last_client_replies_woff;
352 * Current offset into 'last_request_times' ring buffer.
354 unsigned int last_request_times_off;
360 * Information we keep for each pending request. We should try to
361 * keep this struct as small as possible since its memory consumption
362 * is key to how many requests we can have pending at once.
364 struct PendingRequest;
368 * Doubly-linked list of requests we are performing
369 * on behalf of the same client.
371 struct ClientRequestList
375 * This is a doubly-linked list.
377 struct ClientRequestList *next;
380 * This is a doubly-linked list.
382 struct ClientRequestList *prev;
385 * Request this entry represents.
387 struct PendingRequest *req;
390 * Client list this request belongs to.
392 struct ClientList *client_list;
398 * Replies to be transmitted to the client. The actual
399 * response message is allocated after this struct.
401 struct ClientResponseMessage
404 * This is a doubly-linked list.
406 struct ClientResponseMessage *next;
409 * This is a doubly-linked list.
411 struct ClientResponseMessage *prev;
414 * Client list entry this response belongs to.
416 struct ClientList *client_list;
419 * Number of bytes in the response.
426 * Linked list of clients we are performing requests
432 * This is a linked list.
434 struct ClientList *next;
437 * ID of a client making a request, NULL if this entry is for a
440 struct GNUNET_SERVER_Client *client;
443 * Head of list of requests performed on behalf
444 * of this client right now.
446 struct ClientRequestList *rl_head;
449 * Tail of list of requests performed on behalf
450 * of this client right now.
452 struct ClientRequestList *rl_tail;
455 * Head of linked list of responses.
457 struct ClientResponseMessage *res_head;
460 * Tail of linked list of responses.
462 struct ClientResponseMessage *res_tail;
465 * Context for sending replies.
467 struct GNUNET_CONNECTION_TransmitHandle *th;
473 * Information about a peer that we have forwarded this
474 * request to already.
476 struct UsedTargetEntry
479 * What was the last time we have transmitted this request to this
482 struct GNUNET_TIME_Absolute last_request_time;
485 * How often have we transmitted this request to this peer?
487 unsigned int num_requests;
490 * PID of the target peer.
501 * Doubly-linked list of messages we are performing
502 * due to a pending request.
504 struct PendingMessageList
508 * This is a doubly-linked list of messages on behalf of the same request.
510 struct PendingMessageList *next;
513 * This is a doubly-linked list of messages on behalf of the same request.
515 struct PendingMessageList *prev;
518 * Message this entry represents.
520 struct PendingMessage *pm;
523 * Request this entry belongs to.
525 struct PendingRequest *req;
528 * Peer this message is targeted for.
530 struct ConnectedPeer *target;
536 * Information we keep for each pending request. We should try to
537 * keep this struct as small as possible since its memory consumption
538 * is key to how many requests we can have pending at once.
540 struct PendingRequest
544 * If this request was made by a client, this is our entry in the
545 * client request list; otherwise NULL.
547 struct ClientRequestList *client_request_list;
550 * Entry of peer responsible for this entry (if this request
551 * was made by a peer).
553 struct ConnectedPeer *cp;
556 * If this is a namespace query, pointer to the hash of the public
557 * key of the namespace; otherwise NULL. Pointer will be to the
558 * end of this struct (so no need to free it).
560 const GNUNET_HashCode *namespace;
563 * Bloomfilter we use to filter out replies that we don't care about
564 * (anymore). NULL as long as we are interested in all replies.
566 struct GNUNET_CONTAINER_BloomFilter *bf;
569 * Reference to DHT get operation for this request (or NULL).
571 struct GNUNET_DHT_GetHandle *dht_get;
574 * Context of our GNUNET_CORE_peer_change_preference call.
576 struct ConnectedPeer *pirc;
579 * Hash code of all replies that we have seen so far (only valid
580 * if client is not NULL since we only track replies like this for
583 GNUNET_HashCode *replies_seen;
586 * Node in the heap representing this entry; NULL
587 * if we have no heap node.
589 struct GNUNET_CONTAINER_HeapNode *hnode;
592 * Head of list of messages being performed on behalf of this
595 struct PendingMessageList *pending_head;
598 * Tail of list of messages being performed on behalf of this
601 struct PendingMessageList *pending_tail;
604 * When did we first see this request (form this peer), or, if our
605 * client is initiating, when did we last initiate a search?
607 struct GNUNET_TIME_Absolute start_time;
610 * The query that this request is for.
612 GNUNET_HashCode query;
615 * The task responsible for transmitting queries
618 GNUNET_SCHEDULER_TaskIdentifier task;
621 * (Interned) Peer identifier that identifies a preferred target
624 GNUNET_PEER_Id target_pid;
627 * (Interned) Peer identifiers of peers that have already
628 * received our query for this content.
630 struct UsedTargetEntry *used_targets;
633 * Our entry in the queue (non-NULL while we wait for our
634 * turn to interact with the local database).
636 struct GNUNET_DATASTORE_QueueEntry *qe;
639 * Size of the 'bf' (in bytes).
644 * Desired anonymity level; only valid for requests from a local client.
646 uint32_t anonymity_level;
649 * How many entries in "used_targets" are actually valid?
651 unsigned int used_targets_off;
654 * How long is the "used_targets" array?
656 unsigned int used_targets_size;
659 * Number of results found for this request.
661 unsigned int results_found;
664 * How many entries in "replies_seen" are actually valid?
666 unsigned int replies_seen_off;
669 * How long is the "replies_seen" array?
671 unsigned int replies_seen_size;
674 * Priority with which this request was made. If one of our clients
675 * made the request, then this is the current priority that we are
676 * using when initiating the request. This value is used when
677 * we decide to reward other peers with trust for providing a reply.
682 * Priority points left for us to spend when forwarding this request
685 uint32_t remaining_priority;
688 * Number to mingle hashes for bloom-filter tests with.
693 * TTL with which we saw this request (or, if we initiated, TTL that
694 * we used for the request).
699 * Type of the content that this request is for.
701 enum GNUNET_BLOCK_Type type;
704 * Remove this request after transmission of the current response.
709 * GNUNET_YES if we should not forward this request to other peers.
714 * GNUNET_YES if we should not forward this request to other peers.
722 * Block that is ready for migration to other peers. Actual data is at the end of the block.
724 struct MigrationReadyBlock
728 * This is a doubly-linked list.
730 struct MigrationReadyBlock *next;
733 * This is a doubly-linked list.
735 struct MigrationReadyBlock *prev;
738 * Query for the block.
740 GNUNET_HashCode query;
743 * When does this block expire?
745 struct GNUNET_TIME_Absolute expiration;
748 * Peers we would consider forwarding this
749 * block to. Zero for empty entries.
751 GNUNET_PEER_Id target_list[MIGRATION_LIST_SIZE];
759 * Number of targets already used.
761 unsigned int used_targets;
766 enum GNUNET_BLOCK_Type type;
771 * Our connection to the datastore.
773 static struct GNUNET_DATASTORE_Handle *dsh;
778 static struct GNUNET_BLOCK_Context *block_ctx;
781 * Our block configuration.
783 static struct GNUNET_CONFIGURATION_Handle *block_cfg;
788 static const struct GNUNET_CONFIGURATION_Handle *cfg;
791 * Map of peer identifiers to "struct ConnectedPeer" (for that peer).
793 static struct GNUNET_CONTAINER_MultiHashMap *connected_peers;
796 * Map of peer identifiers to "struct PendingRequest" (for that peer).
798 static struct GNUNET_CONTAINER_MultiHashMap *peer_request_map;
801 * Map of query identifiers to "struct PendingRequest" (for that query).
803 static struct GNUNET_CONTAINER_MultiHashMap *query_request_map;
806 * Heap with the request that will expire next at the top. Contains
807 * pointers of type "struct PendingRequest*"; these will *also* be
808 * aliased from the "requests_by_peer" data structures and the
809 * "requests_by_query" table. Note that requests from our clients
810 * don't expire and are thus NOT in the "requests_by_expiration"
811 * (or the "requests_by_peer" tables).
813 static struct GNUNET_CONTAINER_Heap *requests_by_expiration_heap;
816 * Handle for reporting statistics.
818 static struct GNUNET_STATISTICS_Handle *stats;
821 * Linked list of clients we are currently processing requests for.
823 static struct ClientList *client_list;
826 * Pointer to handle to the core service (points to NULL until we've
829 static struct GNUNET_CORE_Handle *core;
832 * Head of linked list of blocks that can be migrated.
834 static struct MigrationReadyBlock *mig_head;
837 * Tail of linked list of blocks that can be migrated.
839 static struct MigrationReadyBlock *mig_tail;
842 * Request to datastore for migration (or NULL).
844 static struct GNUNET_DATASTORE_QueueEntry *mig_qe;
847 * Request to datastore for DHT PUTs (or NULL).
849 static struct GNUNET_DATASTORE_QueueEntry *dht_qe;
852 * Type we will request for the next DHT PUT round from the datastore.
854 static enum GNUNET_BLOCK_Type dht_put_type = GNUNET_BLOCK_TYPE_FS_KBLOCK;
857 * Where do we store trust information?
859 static char *trustDirectory;
862 * ID of task that collects blocks for migration.
864 static GNUNET_SCHEDULER_TaskIdentifier mig_task;
867 * ID of task that collects blocks for DHT PUTs.
869 static GNUNET_SCHEDULER_TaskIdentifier dht_task;
872 * What is the maximum frequency at which we are allowed to
873 * poll the datastore for migration content?
875 static struct GNUNET_TIME_Relative min_migration_delay;
878 * Handle for DHT operations.
880 static struct GNUNET_DHT_Handle *dht_handle;
883 * Size of the doubly-linked list of migration blocks.
885 static unsigned int mig_size;
888 * Are we allowed to migrate content to this peer.
890 static int active_to_migration;
893 * Are we allowed to push out content from this peer.
895 static int active_from_migration;
898 * How many entires with zero anonymity do we currently estimate
899 * to have in the database?
901 static unsigned int zero_anonymity_count_estimate;
904 * Typical priorities we're seeing from other peers right now. Since
905 * most priorities will be zero, this value is the weighted average of
906 * non-zero priorities seen "recently". In order to ensure that new
907 * values do not dramatically change the ratio, values are first
908 * "capped" to a reasonable range (+N of the current value) and then
909 * averaged into the existing value by a ratio of 1:N. Hence
910 * receiving the largest possible priority can still only raise our
911 * "current_priorities" by at most 1.
913 static double current_priorities;
916 * Datastore 'GET' load tracking.
918 static struct GNUNET_LOAD_Value *datastore_get_load;
921 * Datastore 'PUT' load tracking.
923 static struct GNUNET_LOAD_Value *datastore_put_load;
926 * How long do requests typically stay in the routing table?
928 static struct GNUNET_LOAD_Value *rt_entry_lifetime;
931 * We've just now completed a datastore request. Update our
932 * datastore load calculations.
934 * @param start time when the datastore request was issued
937 update_datastore_delays (struct GNUNET_TIME_Absolute start)
939 struct GNUNET_TIME_Relative delay;
941 delay = GNUNET_TIME_absolute_get_duration (start);
942 GNUNET_LOAD_update (datastore_get_load,
948 * Get the filename under which we would store the GNUNET_HELLO_Message
949 * for the given host and protocol.
950 * @return filename of the form DIRECTORY/HOSTID
953 get_trust_filename (const struct GNUNET_PeerIdentity *id)
955 struct GNUNET_CRYPTO_HashAsciiEncoded fil;
958 GNUNET_CRYPTO_hash_to_enc (&id->hashPubKey, &fil);
959 GNUNET_asprintf (&fn, "%s%s%s", trustDirectory, DIR_SEPARATOR_STR, &fil);
966 * Transmit messages by copying it to the target buffer
967 * "buf". "buf" will be NULL and "size" zero if the socket was closed
968 * for writing in the meantime. In that case, do nothing
969 * (the disconnect or shutdown handler will take care of the rest).
970 * If we were able to transmit messages and there are still more
971 * pending, ask core again for further calls to this function.
973 * @param cls closure, pointer to the 'struct ConnectedPeer*'
974 * @param size number of bytes available in buf
975 * @param buf where the callee should write the message
976 * @return number of bytes written to buf
979 transmit_to_peer (void *cls,
980 size_t size, void *buf);
983 /* ******************* clean up functions ************************ */
986 * Delete the given migration block.
988 * @param mb block to delete
991 delete_migration_block (struct MigrationReadyBlock *mb)
993 GNUNET_CONTAINER_DLL_remove (mig_head,
996 GNUNET_PEER_decrement_rcs (mb->target_list,
997 MIGRATION_LIST_SIZE);
1004 * Compare the distance of two peers to a key.
1007 * @param p1 first peer
1008 * @param p2 second peer
1009 * @return GNUNET_YES if P1 is closer to key than P2
1012 is_closer (const GNUNET_HashCode *key,
1013 const struct GNUNET_PeerIdentity *p1,
1014 const struct GNUNET_PeerIdentity *p2)
1016 return GNUNET_CRYPTO_hash_xorcmp (&p1->hashPubKey,
1023 * Consider migrating content to a given peer.
1025 * @param cls 'struct MigrationReadyBlock*' to select
1026 * targets for (or NULL for none)
1027 * @param key ID of the peer
1028 * @param value 'struct ConnectedPeer' of the peer
1029 * @return GNUNET_YES (always continue iteration)
1032 consider_migration (void *cls,
1033 const GNUNET_HashCode *key,
1036 struct MigrationReadyBlock *mb = cls;
1037 struct ConnectedPeer *cp = value;
1038 struct MigrationReadyBlock *pos;
1039 struct GNUNET_PeerIdentity cppid;
1040 struct GNUNET_PeerIdentity otherpid;
1041 struct GNUNET_PeerIdentity worstpid;
1046 /* consider 'cp' as a migration target for mb */
1047 if (GNUNET_TIME_absolute_get_remaining (cp->migration_blocked).rel_value > 0)
1048 return GNUNET_YES; /* peer has requested no migration! */
1051 GNUNET_PEER_resolve (cp->pid,
1053 repl = MIGRATION_LIST_SIZE;
1054 for (i=0;i<MIGRATION_LIST_SIZE;i++)
1056 if (mb->target_list[i] == 0)
1058 mb->target_list[i] = cp->pid;
1059 GNUNET_PEER_change_rc (mb->target_list[i], 1);
1060 repl = MIGRATION_LIST_SIZE;
1063 GNUNET_PEER_resolve (mb->target_list[i],
1065 if ( (repl == MIGRATION_LIST_SIZE) &&
1066 is_closer (&mb->query,
1071 worstpid = otherpid;
1073 else if ( (repl != MIGRATION_LIST_SIZE) &&
1074 (is_closer (&mb->query,
1079 worstpid = otherpid;
1082 if (repl != MIGRATION_LIST_SIZE)
1084 GNUNET_PEER_change_rc (mb->target_list[repl], -1);
1085 mb->target_list[repl] = cp->pid;
1086 GNUNET_PEER_change_rc (mb->target_list[repl], 1);
1090 /* consider scheduling transmission to cp for content migration */
1091 if (cp->cth != NULL)
1097 for (i=0;i<MIGRATION_LIST_SIZE;i++)
1099 if (cp->pid == pos->target_list[i])
1104 msize = GNUNET_MIN (msize,
1112 return GNUNET_YES; /* no content available */
1114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1115 "Trying to migrate at least %u bytes to peer `%s'\n",
1119 if (cp->delayed_transmission_request_task != GNUNET_SCHEDULER_NO_TASK)
1121 GNUNET_SCHEDULER_cancel (cp->delayed_transmission_request_task);
1122 cp->delayed_transmission_request_task = GNUNET_SCHEDULER_NO_TASK;
1125 = GNUNET_CORE_notify_transmit_ready (core,
1126 0, GNUNET_TIME_UNIT_FOREVER_REL,
1127 (const struct GNUNET_PeerIdentity*) key,
1128 msize + sizeof (struct PutMessage),
1136 * Task that is run periodically to obtain blocks for content
1140 * @param tc scheduler context (also unused)
1143 gather_migration_blocks (void *cls,
1144 const struct GNUNET_SCHEDULER_TaskContext *tc);
1150 * Task that is run periodically to obtain blocks for DHT PUTs.
1152 * @param cls type of blocks to gather
1153 * @param tc scheduler context (unused)
1156 gather_dht_put_blocks (void *cls,
1157 const struct GNUNET_SCHEDULER_TaskContext *tc);
1161 * If the migration task is not currently running, consider
1162 * (re)scheduling it with the appropriate delay.
1165 consider_migration_gathering ()
1167 struct GNUNET_TIME_Relative delay;
1173 if (mig_task != GNUNET_SCHEDULER_NO_TASK)
1175 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
1177 delay = GNUNET_TIME_relative_divide (delay,
1178 MAX_MIGRATION_QUEUE);
1179 delay = GNUNET_TIME_relative_max (delay,
1180 min_migration_delay);
1181 mig_task = GNUNET_SCHEDULER_add_delayed (delay,
1182 &gather_migration_blocks,
1188 * If the DHT PUT gathering task is not currently running, consider
1189 * (re)scheduling it with the appropriate delay.
1192 consider_dht_put_gathering (void *cls)
1194 struct GNUNET_TIME_Relative delay;
1200 if (dht_task != GNUNET_SCHEDULER_NO_TASK)
1202 if (zero_anonymity_count_estimate > 0)
1204 delay = GNUNET_TIME_relative_divide (GNUNET_DHT_DEFAULT_REPUBLISH_FREQUENCY,
1205 zero_anonymity_count_estimate);
1206 delay = GNUNET_TIME_relative_min (delay,
1211 /* if we have NO zero-anonymity content yet, wait 5 minutes for some to
1212 (hopefully) appear */
1213 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5);
1215 dht_task = GNUNET_SCHEDULER_add_delayed (delay,
1216 &gather_dht_put_blocks,
1222 * Process content offered for migration.
1224 * @param cls closure
1225 * @param key key for the content
1226 * @param size number of bytes in data
1227 * @param data content stored
1228 * @param type type of the content
1229 * @param priority priority of the content
1230 * @param anonymity anonymity-level for the content
1231 * @param expiration expiration time for the content
1232 * @param uid unique identifier for the datum;
1233 * maybe 0 if no unique identifier is available
1236 process_migration_content (void *cls,
1237 const GNUNET_HashCode * key,
1240 enum GNUNET_BLOCK_Type type,
1243 struct GNUNET_TIME_Absolute
1244 expiration, uint64_t uid)
1246 struct MigrationReadyBlock *mb;
1251 if (mig_size < MAX_MIGRATION_QUEUE)
1252 consider_migration_gathering ();
1255 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
1258 GNUNET_FS_handle_on_demand_block (key, size, data,
1259 type, priority, anonymity,
1261 &process_migration_content,
1264 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
1269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1270 "Retrieved block `%s' of type %u for migration\n",
1274 mb = GNUNET_malloc (sizeof (struct MigrationReadyBlock) + size);
1276 mb->expiration = expiration;
1279 memcpy (&mb[1], data, size);
1280 GNUNET_CONTAINER_DLL_insert_after (mig_head,
1285 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
1286 &consider_migration,
1288 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
1293 * Function called upon completion of the DHT PUT operation.
1296 dht_put_continuation (void *cls,
1297 const struct GNUNET_SCHEDULER_TaskContext *tc)
1299 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
1304 * Store content in DHT.
1306 * @param cls closure
1307 * @param key key for the content
1308 * @param size number of bytes in data
1309 * @param data content stored
1310 * @param type type of the content
1311 * @param priority priority of the content
1312 * @param anonymity anonymity-level for the content
1313 * @param expiration expiration time for the content
1314 * @param uid unique identifier for the datum;
1315 * maybe 0 if no unique identifier is available
1318 process_dht_put_content (void *cls,
1319 const GNUNET_HashCode * key,
1322 enum GNUNET_BLOCK_Type type,
1325 struct GNUNET_TIME_Absolute
1326 expiration, uint64_t uid)
1328 static unsigned int counter;
1329 static GNUNET_HashCode last_vhash;
1330 static GNUNET_HashCode vhash;
1335 consider_dht_put_gathering (cls);
1338 /* slightly funky code to estimate the total number of values with zero
1339 anonymity from the maximum observed length of a monotonically increasing
1340 sequence of hashes over the contents */
1341 GNUNET_CRYPTO_hash (data, size, &vhash);
1342 if (GNUNET_CRYPTO_hash_cmp (&vhash, &last_vhash) <= 0)
1344 if (zero_anonymity_count_estimate > 0)
1345 zero_anonymity_count_estimate /= 2;
1351 if (zero_anonymity_count_estimate < (1 << counter))
1352 zero_anonymity_count_estimate = (1 << counter);
1354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1355 "Retrieved block `%s' of type %u for DHT PUT\n",
1359 GNUNET_DHT_put (dht_handle,
1361 DEFAULT_PUT_REPLICATION,
1367 GNUNET_TIME_UNIT_FOREVER_REL,
1368 &dht_put_continuation,
1374 * Task that is run periodically to obtain blocks for content
1378 * @param tc scheduler context (also unused)
1381 gather_migration_blocks (void *cls,
1382 const struct GNUNET_SCHEDULER_TaskContext *tc)
1384 mig_task = GNUNET_SCHEDULER_NO_TASK;
1387 mig_qe = GNUNET_DATASTORE_get_random (dsh, 0, UINT_MAX,
1388 GNUNET_TIME_UNIT_FOREVER_REL,
1389 &process_migration_content, NULL);
1390 GNUNET_assert (mig_qe != NULL);
1396 * Task that is run periodically to obtain blocks for DHT PUTs.
1398 * @param cls type of blocks to gather
1399 * @param tc scheduler context (unused)
1402 gather_dht_put_blocks (void *cls,
1403 const struct GNUNET_SCHEDULER_TaskContext *tc)
1405 dht_task = GNUNET_SCHEDULER_NO_TASK;
1408 if (dht_put_type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
1409 dht_put_type = GNUNET_BLOCK_TYPE_FS_KBLOCK;
1410 dht_qe = GNUNET_DATASTORE_get_zero_anonymity (dsh, 0, UINT_MAX,
1411 GNUNET_TIME_UNIT_FOREVER_REL,
1413 &process_dht_put_content, NULL);
1414 GNUNET_assert (dht_qe != NULL);
1420 * We're done with a particular message list entry.
1421 * Free all associated resources.
1423 * @param pml entry to destroy
1426 destroy_pending_message_list_entry (struct PendingMessageList *pml)
1428 GNUNET_CONTAINER_DLL_remove (pml->req->pending_head,
1429 pml->req->pending_tail,
1431 GNUNET_CONTAINER_DLL_remove (pml->target->pending_messages_head,
1432 pml->target->pending_messages_tail,
1434 pml->target->pending_requests--;
1435 GNUNET_free (pml->pm);
1441 * Destroy the given pending message (and call the respective
1444 * @param pm message to destroy
1445 * @param tpid id of peer that the message was delivered to, or 0 for none
1448 destroy_pending_message (struct PendingMessage *pm,
1449 GNUNET_PEER_Id tpid)
1451 struct PendingMessageList *pml = pm->pml;
1452 TransmissionContinuation cont;
1456 cont_cls = pm->cont_cls;
1459 GNUNET_assert (pml->pm == pm);
1460 GNUNET_assert ( (tpid == 0) || (tpid == pml->target->pid) );
1461 destroy_pending_message_list_entry (pml);
1468 cont (cont_cls, tpid);
1473 * We're done processing a particular request.
1474 * Free all associated resources.
1476 * @param pr request to destroy
1479 destroy_pending_request (struct PendingRequest *pr)
1481 struct GNUNET_PeerIdentity pid;
1484 if (pr->hnode != NULL)
1486 GNUNET_CONTAINER_heap_remove_node (requests_by_expiration_heap,
1490 if (NULL == pr->client_request_list)
1492 GNUNET_STATISTICS_update (stats,
1493 gettext_noop ("# P2P searches active"),
1499 GNUNET_STATISTICS_update (stats,
1500 gettext_noop ("# client searches active"),
1505 GNUNET_CONTAINER_multihashmap_remove (query_request_map,
1509 GNUNET_LOAD_update (rt_entry_lifetime,
1510 GNUNET_TIME_absolute_get_duration (pr->start_time).rel_value);
1514 GNUNET_DATASTORE_cancel (pr->qe);
1517 if (pr->dht_get != NULL)
1519 GNUNET_DHT_get_stop (pr->dht_get);
1522 if (pr->client_request_list != NULL)
1524 GNUNET_CONTAINER_DLL_remove (pr->client_request_list->client_list->rl_head,
1525 pr->client_request_list->client_list->rl_tail,
1526 pr->client_request_list);
1527 GNUNET_free (pr->client_request_list);
1528 pr->client_request_list = NULL;
1532 GNUNET_PEER_resolve (pr->cp->pid,
1534 (void) GNUNET_CONTAINER_multihashmap_remove (peer_request_map,
1541 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
1544 if (pr->pirc != NULL)
1546 GNUNET_CORE_peer_change_preference_cancel (pr->pirc->irc);
1547 pr->pirc->irc = NULL;
1550 if (pr->replies_seen != NULL)
1552 GNUNET_free (pr->replies_seen);
1553 pr->replies_seen = NULL;
1555 if (pr->task != GNUNET_SCHEDULER_NO_TASK)
1557 GNUNET_SCHEDULER_cancel (pr->task);
1558 pr->task = GNUNET_SCHEDULER_NO_TASK;
1560 while (NULL != pr->pending_head)
1561 destroy_pending_message_list_entry (pr->pending_head);
1562 GNUNET_PEER_change_rc (pr->target_pid, -1);
1563 if (pr->used_targets != NULL)
1565 for (i=0;i<pr->used_targets_off;i++)
1566 GNUNET_PEER_change_rc (pr->used_targets[i].pid, -1);
1567 GNUNET_free (pr->used_targets);
1568 pr->used_targets_off = 0;
1569 pr->used_targets_size = 0;
1570 pr->used_targets = NULL;
1577 * Find latency information in 'atsi'.
1579 * @param atsi performance data
1580 * @return connection latency
1582 static struct GNUNET_TIME_Relative
1583 get_latency (const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1585 /* FIXME: extract latency data from 'atsi' */
1586 return GNUNET_TIME_UNIT_SECONDS;
1591 * Method called whenever a given peer connects.
1593 * @param cls closure, not used
1594 * @param peer peer identity this notification is about
1595 * @param atsi performance information
1598 peer_connect_handler (void *cls,
1600 GNUNET_PeerIdentity * peer,
1601 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1603 struct ConnectedPeer *cp;
1604 struct MigrationReadyBlock *pos;
1607 struct GNUNET_TIME_Relative latency;
1609 latency = get_latency (atsi);
1610 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
1617 cp = GNUNET_malloc (sizeof (struct ConnectedPeer));
1618 cp->transmission_delay = GNUNET_LOAD_value_init (latency);
1619 cp->pid = GNUNET_PEER_intern (peer);
1621 fn = get_trust_filename (peer);
1622 if ((GNUNET_DISK_file_test (fn) == GNUNET_YES) &&
1623 (sizeof (trust) == GNUNET_DISK_fn_read (fn, &trust, sizeof (trust))))
1624 cp->disk_trust = cp->trust = ntohl (trust);
1627 GNUNET_break (GNUNET_OK ==
1628 GNUNET_CONTAINER_multihashmap_put (connected_peers,
1631 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1636 (void) consider_migration (pos, &peer->hashPubKey, cp);
1643 * Method called whenever a given peer has a status change.
1645 * @param cls closure
1646 * @param peer peer identity this notification is about
1647 * @param bandwidth_in available amount of inbound bandwidth
1648 * @param bandwidth_out available amount of outbound bandwidth
1649 * @param timeout absolute time when this peer will time out
1650 * unless we see some further activity from it
1651 * @param atsi status information
1654 peer_status_handler (void *cls,
1656 GNUNET_PeerIdentity * peer,
1657 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
1658 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
1659 struct GNUNET_TIME_Absolute timeout,
1660 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1662 struct ConnectedPeer *cp;
1663 struct GNUNET_TIME_Relative latency;
1665 latency = get_latency (atsi);
1666 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
1673 GNUNET_LOAD_value_set_decline (cp->transmission_delay,
1680 * Increase the host credit by a value.
1682 * @param host which peer to change the trust value on
1683 * @param value is the int value by which the
1684 * host credit is to be increased or decreased
1685 * @returns the actual change in trust (positive or negative)
1688 change_host_trust (struct ConnectedPeer *host, int value)
1692 GNUNET_assert (host != NULL);
1695 if (host->trust + value < host->trust)
1697 value = UINT32_MAX - host->trust;
1698 host->trust = UINT32_MAX;
1701 host->trust += value;
1705 if (host->trust < -value)
1707 value = -host->trust;
1711 host->trust += value;
1718 * Write host-trust information to a file - flush the buffer entry!
1721 flush_trust (void *cls,
1722 const GNUNET_HashCode *key,
1725 struct ConnectedPeer *host = value;
1728 struct GNUNET_PeerIdentity pid;
1730 if (host->trust == host->disk_trust)
1731 return GNUNET_OK; /* unchanged */
1732 GNUNET_PEER_resolve (host->pid,
1734 fn = get_trust_filename (&pid);
1735 if (host->trust == 0)
1737 if ((0 != UNLINK (fn)) && (errno != ENOENT))
1738 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
1739 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
1743 trust = htonl (host->trust);
1744 if (sizeof(uint32_t) == GNUNET_DISK_fn_write (fn, &trust,
1746 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE
1747 | GNUNET_DISK_PERM_GROUP_READ | GNUNET_DISK_PERM_OTHER_READ))
1748 host->disk_trust = host->trust;
1755 * Call this method periodically to scan data/hosts for new hosts.
1758 cron_flush_trust (void *cls,
1759 const struct GNUNET_SCHEDULER_TaskContext *tc)
1762 if (NULL == connected_peers)
1764 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
1769 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1771 GNUNET_SCHEDULER_add_delayed (TRUST_FLUSH_FREQ, &cron_flush_trust, NULL);
1776 * Free (each) request made by the peer.
1778 * @param cls closure, points to peer that the request belongs to
1779 * @param key current key code
1780 * @param value value in the hash map
1781 * @return GNUNET_YES (we should continue to iterate)
1784 destroy_request (void *cls,
1785 const GNUNET_HashCode * key,
1788 const struct GNUNET_PeerIdentity * peer = cls;
1789 struct PendingRequest *pr = value;
1791 GNUNET_break (GNUNET_YES ==
1792 GNUNET_CONTAINER_multihashmap_remove (peer_request_map,
1795 destroy_pending_request (pr);
1801 * Method called whenever a peer disconnects.
1803 * @param cls closure, not used
1804 * @param peer peer identity this notification is about
1807 peer_disconnect_handler (void *cls,
1809 GNUNET_PeerIdentity * peer)
1811 struct ConnectedPeer *cp;
1812 struct PendingMessage *pm;
1814 struct MigrationReadyBlock *pos;
1815 struct MigrationReadyBlock *next;
1817 GNUNET_CONTAINER_multihashmap_get_multiple (peer_request_map,
1821 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
1825 for (i=0;i<CS2P_SUCCESS_LIST_SIZE;i++)
1827 if (NULL != cp->last_client_replies[i])
1829 GNUNET_SERVER_client_drop (cp->last_client_replies[i]);
1830 cp->last_client_replies[i] = NULL;
1833 GNUNET_break (GNUNET_YES ==
1834 GNUNET_CONTAINER_multihashmap_remove (connected_peers,
1837 if (cp->irc != NULL)
1839 GNUNET_CORE_peer_change_preference_cancel (cp->irc);
1841 cp->pr->pirc = NULL;
1845 /* remove this peer from migration considerations; schedule
1848 while (NULL != (pos = next))
1851 for (i=0;i<MIGRATION_LIST_SIZE;i++)
1853 if (pos->target_list[i] == cp->pid)
1855 GNUNET_PEER_change_rc (pos->target_list[i], -1);
1856 pos->target_list[i] = 0;
1859 if (pos->used_targets >= GNUNET_CONTAINER_multihashmap_size (connected_peers))
1861 delete_migration_block (pos);
1862 consider_migration_gathering ();
1865 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
1866 &consider_migration,
1869 GNUNET_PEER_change_rc (cp->pid, -1);
1870 GNUNET_PEER_decrement_rcs (cp->last_p2p_replies, P2P_SUCCESS_LIST_SIZE);
1871 if (NULL != cp->cth)
1873 GNUNET_CORE_notify_transmit_ready_cancel (cp->cth);
1876 if (cp->delayed_transmission_request_task != GNUNET_SCHEDULER_NO_TASK)
1878 GNUNET_SCHEDULER_cancel (cp->delayed_transmission_request_task);
1879 cp->delayed_transmission_request_task = GNUNET_SCHEDULER_NO_TASK;
1881 while (NULL != (pm = cp->pending_messages_head))
1882 destroy_pending_message (pm, 0 /* delivery failed */);
1883 GNUNET_LOAD_value_free (cp->transmission_delay);
1884 GNUNET_break (0 == cp->pending_requests);
1890 * Iterator over hash map entries that removes all occurences
1891 * of the given 'client' from the 'last_client_replies' of the
1892 * given connected peer.
1894 * @param cls closure, the 'struct GNUNET_SERVER_Client*' to remove
1895 * @param key current key code (unused)
1896 * @param value value in the hash map (the 'struct ConnectedPeer*' to change)
1897 * @return GNUNET_YES (we should continue to iterate)
1900 remove_client_from_last_client_replies (void *cls,
1901 const GNUNET_HashCode * key,
1904 struct GNUNET_SERVER_Client *client = cls;
1905 struct ConnectedPeer *cp = value;
1908 for (i=0;i<CS2P_SUCCESS_LIST_SIZE;i++)
1910 if (cp->last_client_replies[i] == client)
1912 GNUNET_SERVER_client_drop (cp->last_client_replies[i]);
1913 cp->last_client_replies[i] = NULL;
1921 * A client disconnected. Remove all of its pending queries.
1923 * @param cls closure, NULL
1924 * @param client identification of the client
1927 handle_client_disconnect (void *cls,
1928 struct GNUNET_SERVER_Client
1931 struct ClientList *pos;
1932 struct ClientList *prev;
1933 struct ClientRequestList *rcl;
1934 struct ClientResponseMessage *creply;
1940 while ( (NULL != pos) &&
1941 (pos->client != client) )
1947 return; /* no requests pending for this client */
1948 while (NULL != (rcl = pos->rl_head))
1950 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1951 "Destroying pending request `%s' on disconnect\n",
1952 GNUNET_h2s (&rcl->req->query));
1953 destroy_pending_request (rcl->req);
1956 client_list = pos->next;
1958 prev->next = pos->next;
1959 if (pos->th != NULL)
1961 GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th);
1964 while (NULL != (creply = pos->res_head))
1966 GNUNET_CONTAINER_DLL_remove (pos->res_head,
1969 GNUNET_free (creply);
1971 GNUNET_SERVER_client_drop (pos->client);
1973 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
1974 &remove_client_from_last_client_replies,
1980 * Iterator to free peer entries.
1982 * @param cls closure, unused
1983 * @param key current key code
1984 * @param value value in the hash map (peer entry)
1985 * @return GNUNET_YES (we should continue to iterate)
1988 clean_peer (void *cls,
1989 const GNUNET_HashCode * key,
1992 peer_disconnect_handler (NULL, (const struct GNUNET_PeerIdentity*) key);
1998 * Task run during shutdown.
2004 shutdown_task (void *cls,
2005 const struct GNUNET_SCHEDULER_TaskContext *tc)
2009 GNUNET_DATASTORE_cancel (mig_qe);
2014 GNUNET_DATASTORE_cancel (dht_qe);
2017 if (GNUNET_SCHEDULER_NO_TASK != mig_task)
2019 GNUNET_SCHEDULER_cancel (mig_task);
2020 mig_task = GNUNET_SCHEDULER_NO_TASK;
2022 if (GNUNET_SCHEDULER_NO_TASK != dht_task)
2024 GNUNET_SCHEDULER_cancel (dht_task);
2025 dht_task = GNUNET_SCHEDULER_NO_TASK;
2027 while (client_list != NULL)
2028 handle_client_disconnect (NULL,
2029 client_list->client);
2030 cron_flush_trust (NULL, NULL);
2031 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
2034 GNUNET_break (0 == GNUNET_CONTAINER_heap_get_size (requests_by_expiration_heap));
2035 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
2036 requests_by_expiration_heap = 0;
2037 GNUNET_CONTAINER_multihashmap_destroy (connected_peers);
2038 connected_peers = NULL;
2039 GNUNET_break (0 == GNUNET_CONTAINER_multihashmap_size (query_request_map));
2040 GNUNET_CONTAINER_multihashmap_destroy (query_request_map);
2041 query_request_map = NULL;
2042 GNUNET_LOAD_value_free (rt_entry_lifetime);
2043 rt_entry_lifetime = NULL;
2044 GNUNET_break (0 == GNUNET_CONTAINER_multihashmap_size (peer_request_map));
2045 GNUNET_CONTAINER_multihashmap_destroy (peer_request_map);
2046 peer_request_map = NULL;
2047 GNUNET_assert (NULL != core);
2048 GNUNET_CORE_disconnect (core);
2052 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
2057 GNUNET_DATASTORE_disconnect (dsh,
2061 while (mig_head != NULL)
2062 delete_migration_block (mig_head);
2063 GNUNET_assert (0 == mig_size);
2064 GNUNET_DHT_disconnect (dht_handle);
2066 GNUNET_LOAD_value_free (datastore_get_load);
2067 datastore_get_load = NULL;
2068 GNUNET_LOAD_value_free (datastore_put_load);
2069 datastore_put_load = NULL;
2070 GNUNET_BLOCK_context_destroy (block_ctx);
2072 GNUNET_CONFIGURATION_destroy (block_cfg);
2075 GNUNET_free_non_null (trustDirectory);
2076 trustDirectory = NULL;
2080 /* ******************* Utility functions ******************** */
2084 * We've had to delay a request for transmission to core, but now
2085 * we should be ready. Run it.
2087 * @param cls the 'struct ConnectedPeer' for which a request was delayed
2088 * @param tc task context (unused)
2091 delayed_transmission_request (void *cls,
2092 const struct GNUNET_SCHEDULER_TaskContext *tc)
2094 struct ConnectedPeer *cp = cls;
2095 struct GNUNET_PeerIdentity pid;
2096 struct PendingMessage *pm;
2098 pm = cp->pending_messages_head;
2099 cp->delayed_transmission_request_task = GNUNET_SCHEDULER_NO_TASK;
2100 GNUNET_assert (cp->cth == NULL);
2103 GNUNET_PEER_resolve (cp->pid,
2105 cp->last_transmission_request_start = GNUNET_TIME_absolute_get ();
2106 cp->cth = GNUNET_CORE_notify_transmit_ready (core,
2108 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
2117 * Transmit messages by copying it to the target buffer
2118 * "buf". "buf" will be NULL and "size" zero if the socket was closed
2119 * for writing in the meantime. In that case, do nothing
2120 * (the disconnect or shutdown handler will take care of the rest).
2121 * If we were able to transmit messages and there are still more
2122 * pending, ask core again for further calls to this function.
2124 * @param cls closure, pointer to the 'struct ConnectedPeer*'
2125 * @param size number of bytes available in buf
2126 * @param buf where the callee should write the message
2127 * @return number of bytes written to buf
2130 transmit_to_peer (void *cls,
2131 size_t size, void *buf)
2133 struct ConnectedPeer *cp = cls;
2135 struct PendingMessage *pm;
2136 struct PendingMessage *next_pm;
2137 struct GNUNET_TIME_Absolute now;
2138 struct GNUNET_TIME_Relative min_delay;
2139 struct MigrationReadyBlock *mb;
2140 struct MigrationReadyBlock *next;
2141 struct PutMessage migm;
2144 struct GNUNET_PeerIdentity pid;
2150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2151 "Dropping message, core too busy.\n");
2153 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2154 "Dropping message, core too busy.\n");
2155 GNUNET_LOAD_update (cp->transmission_delay,
2158 if (NULL != (pm = cp->pending_messages_head))
2160 GNUNET_CONTAINER_DLL_remove (cp->pending_messages_head,
2161 cp->pending_messages_tail,
2163 cp->pending_requests--;
2164 destroy_pending_message (pm, 0);
2166 if (NULL != (pm = cp->pending_messages_head))
2168 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == cp->delayed_transmission_request_task);
2169 min_delay = GNUNET_TIME_absolute_get_remaining (pm->delay_until);
2170 cp->delayed_transmission_request_task
2171 = GNUNET_SCHEDULER_add_delayed (min_delay,
2172 &delayed_transmission_request,
2177 GNUNET_LOAD_update (cp->transmission_delay,
2178 GNUNET_TIME_absolute_get_duration (cp->last_transmission_request_start).rel_value);
2179 now = GNUNET_TIME_absolute_get ();
2181 min_delay = GNUNET_TIME_UNIT_FOREVER_REL;
2182 next_pm = cp->pending_messages_head;
2183 while ( (NULL != (pm = next_pm) ) &&
2184 (pm->msize <= size) )
2187 if (pm->delay_until.abs_value > now.abs_value)
2189 min_delay = GNUNET_TIME_relative_min (min_delay,
2190 GNUNET_TIME_absolute_get_remaining (pm->delay_until));
2193 memcpy (&cbuf[msize], &pm[1], pm->msize);
2196 if (NULL == pm->pml)
2198 GNUNET_CONTAINER_DLL_remove (cp->pending_messages_head,
2199 cp->pending_messages_tail,
2201 cp->pending_requests--;
2203 destroy_pending_message (pm, cp->pid);
2206 min_delay = GNUNET_TIME_UNIT_ZERO;
2207 if (NULL != cp->pending_messages_head)
2209 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == cp->delayed_transmission_request_task);
2210 cp->delayed_transmission_request_task
2211 = GNUNET_SCHEDULER_add_delayed (min_delay,
2212 &delayed_transmission_request,
2217 GNUNET_PEER_resolve (cp->pid,
2220 while (NULL != (mb = next))
2223 for (i=0;i<MIGRATION_LIST_SIZE;i++)
2225 if ( (cp->pid == mb->target_list[i]) &&
2226 (mb->size + sizeof (migm) <= size) )
2228 GNUNET_PEER_change_rc (mb->target_list[i], -1);
2229 mb->target_list[i] = 0;
2231 memset (&migm, 0, sizeof (migm));
2232 migm.header.size = htons (sizeof (migm) + mb->size);
2233 migm.header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
2234 migm.type = htonl (mb->type);
2235 migm.expiration = GNUNET_TIME_absolute_hton (mb->expiration);
2236 memcpy (&cbuf[msize], &migm, sizeof (migm));
2237 msize += sizeof (migm);
2238 size -= sizeof (migm);
2239 memcpy (&cbuf[msize], &mb[1], mb->size);
2243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2244 "Pushing migration block `%s' (%u bytes) to `%s'\n",
2245 GNUNET_h2s (&mb->query),
2246 (unsigned int) mb->size,
2254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2255 "Migration block `%s' (%u bytes) is not on migration list for peer `%s'\n",
2256 GNUNET_h2s (&mb->query),
2257 (unsigned int) mb->size,
2262 if ( (mb->used_targets >= MIGRATION_TARGET_COUNT) ||
2263 (mb->used_targets >= GNUNET_CONTAINER_multihashmap_size (connected_peers)) )
2265 delete_migration_block (mb);
2266 consider_migration_gathering ();
2269 consider_migration (NULL,
2274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2275 "Transmitting %u bytes to peer with PID %u\n",
2276 (unsigned int) msize,
2277 (unsigned int) cp->pid);
2284 * Add a message to the set of pending messages for the given peer.
2286 * @param cp peer to send message to
2287 * @param pm message to queue
2288 * @param pr request on which behalf this message is being queued
2291 add_to_pending_messages_for_peer (struct ConnectedPeer *cp,
2292 struct PendingMessage *pm,
2293 struct PendingRequest *pr)
2295 struct PendingMessage *pos;
2296 struct PendingMessageList *pml;
2297 struct GNUNET_PeerIdentity pid;
2299 GNUNET_assert (pm->next == NULL);
2300 GNUNET_assert (pm->pml == NULL);
2303 pml = GNUNET_malloc (sizeof (struct PendingMessageList));
2308 GNUNET_CONTAINER_DLL_insert (pr->pending_head,
2312 pos = cp->pending_messages_head;
2313 while ( (pos != NULL) &&
2314 (pm->priority < pos->priority) )
2316 GNUNET_CONTAINER_DLL_insert_after (cp->pending_messages_head,
2317 cp->pending_messages_tail,
2320 cp->pending_requests++;
2321 if (cp->pending_requests > MAX_QUEUE_PER_PEER)
2323 GNUNET_STATISTICS_update (stats,
2324 gettext_noop ("# P2P searches discarded (queue length bound)"),
2327 destroy_pending_message (cp->pending_messages_tail, 0);
2329 GNUNET_PEER_resolve (cp->pid, &pid);
2330 if (NULL != cp->cth)
2332 GNUNET_CORE_notify_transmit_ready_cancel (cp->cth);
2335 if (cp->delayed_transmission_request_task != GNUNET_SCHEDULER_NO_TASK)
2337 GNUNET_SCHEDULER_cancel (cp->delayed_transmission_request_task);
2338 cp->delayed_transmission_request_task = GNUNET_SCHEDULER_NO_TASK;
2340 /* need to schedule transmission */
2341 cp->last_transmission_request_start = GNUNET_TIME_absolute_get ();
2342 cp->cth = GNUNET_CORE_notify_transmit_ready (core,
2343 cp->pending_messages_head->priority,
2346 cp->pending_messages_head->msize,
2349 if (cp->cth == NULL)
2352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2353 "Failed to schedule transmission with core!\n");
2355 GNUNET_STATISTICS_update (stats,
2356 gettext_noop ("# CORE transmission failures"),
2364 * Test if the DATABASE (GET) load on this peer is too high
2365 * to even consider processing the query at
2368 * @return GNUNET_YES if the load is too high to do anything (load high)
2369 * GNUNET_NO to process normally (load normal)
2370 * GNUNET_SYSERR to process for free (load low)
2373 test_get_load_too_high (uint32_t priority)
2377 ld = GNUNET_LOAD_get_load (datastore_get_load);
2379 return GNUNET_SYSERR;
2389 * Test if the DATABASE (PUT) load on this peer is too high
2390 * to even consider processing the query at
2393 * @return GNUNET_YES if the load is too high to do anything (load high)
2394 * GNUNET_NO to process normally (load normal or low)
2397 test_put_load_too_high (uint32_t priority)
2401 if (GNUNET_LOAD_get_average (datastore_put_load) < 50)
2402 return GNUNET_NO; /* very fast */
2403 ld = GNUNET_LOAD_get_load (datastore_put_load);
2404 if (ld < 2.0 * (1 + priority))
2406 GNUNET_STATISTICS_update (stats,
2407 gettext_noop ("# storage requests dropped due to high load"),
2414 /* ******************* Pending Request Refresh Task ******************** */
2419 * We use a random delay to make the timing of requests less
2420 * predictable. This function returns such a random delay. We add a base
2421 * delay of MAX_CORK_DELAY (1s).
2423 * FIXME: make schedule dependent on the specifics of the request?
2424 * Or bandwidth and number of connected peers and load?
2426 * @return random delay to use for some request, between 1s and 1000+TTL_DECREMENT ms
2428 static struct GNUNET_TIME_Relative
2429 get_processing_delay ()
2432 GNUNET_TIME_relative_add (GNUNET_CONSTANTS_MAX_CORK_DELAY,
2433 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
2434 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2440 * We're processing a GET request from another peer and have decided
2441 * to forward it to other peers. This function is called periodically
2442 * and should forward the request to other peers until we have all
2443 * possible replies. If we have transmitted the *only* reply to
2444 * the initiator we should destroy the pending request. If we have
2445 * many replies in the queue to the initiator, we should delay sending
2446 * out more queries until the reply queue has shrunk some.
2448 * @param cls our "struct ProcessGetContext *"
2452 forward_request_task (void *cls,
2453 const struct GNUNET_SCHEDULER_TaskContext *tc);
2457 * Function called after we either failed or succeeded
2458 * at transmitting a query to a peer.
2460 * @param cls the requests "struct PendingRequest*"
2461 * @param tpid ID of receiving peer, 0 on transmission error
2464 transmit_query_continuation (void *cls,
2465 GNUNET_PEER_Id tpid)
2467 struct PendingRequest *pr = cls;
2470 GNUNET_STATISTICS_update (stats,
2471 gettext_noop ("# queries scheduled for forwarding"),
2477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2478 "Transmission of request failed, will try again later.\n");
2480 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
2481 pr->task = GNUNET_SCHEDULER_add_delayed (get_processing_delay (),
2482 &forward_request_task,
2487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2488 "Transmitted query `%s'\n",
2489 GNUNET_h2s (&pr->query));
2491 GNUNET_STATISTICS_update (stats,
2492 gettext_noop ("# queries forwarded"),
2495 for (i=0;i<pr->used_targets_off;i++)
2496 if (pr->used_targets[i].pid == tpid)
2497 break; /* found match! */
2498 if (i == pr->used_targets_off)
2500 /* need to create new entry */
2501 if (pr->used_targets_off == pr->used_targets_size)
2502 GNUNET_array_grow (pr->used_targets,
2503 pr->used_targets_size,
2504 pr->used_targets_size * 2 + 2);
2505 GNUNET_PEER_change_rc (tpid, 1);
2506 pr->used_targets[pr->used_targets_off].pid = tpid;
2507 pr->used_targets[pr->used_targets_off].num_requests = 0;
2508 i = pr->used_targets_off++;
2510 pr->used_targets[i].last_request_time = GNUNET_TIME_absolute_get ();
2511 pr->used_targets[i].num_requests++;
2512 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
2513 pr->task = GNUNET_SCHEDULER_add_delayed (get_processing_delay (),
2514 &forward_request_task,
2520 * How many bytes should a bloomfilter be if we have already seen
2521 * entry_count responses? Note that BLOOMFILTER_K gives us the number
2522 * of bits set per entry. Furthermore, we should not re-size the
2523 * filter too often (to keep it cheap).
2525 * Since other peers will also add entries but not resize the filter,
2526 * we should generally pick a slightly larger size than what the
2527 * strict math would suggest.
2529 * @return must be a power of two and smaller or equal to 2^15.
2532 compute_bloomfilter_size (unsigned int entry_count)
2535 unsigned int ideal = (entry_count * BLOOMFILTER_K) / 4;
2536 uint16_t max = 1 << 15;
2538 if (entry_count > max)
2541 while ((size < max) && (size < ideal))
2550 * Recalculate our bloom filter for filtering replies. This function
2551 * will create a new bloom filter from scratch, so it should only be
2552 * called if we have no bloomfilter at all (and hence can create a
2553 * fresh one of minimal size without problems) OR if our peer is the
2554 * initiator (in which case we may resize to larger than mimimum size).
2556 * @param pr request for which the BF is to be recomputed
2559 refresh_bloomfilter (struct PendingRequest *pr)
2563 GNUNET_HashCode mhash;
2565 nsize = compute_bloomfilter_size (pr->replies_seen_off);
2566 if (nsize == pr->bf_size)
2567 return; /* size not changed */
2569 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
2570 pr->bf_size = nsize;
2571 pr->mingle = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, -1);
2572 pr->bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
2575 for (i=0;i<pr->replies_seen_off;i++)
2577 GNUNET_BLOCK_mingle_hash (&pr->replies_seen[i],
2580 GNUNET_CONTAINER_bloomfilter_add (pr->bf, &mhash);
2586 * Function called after we've tried to reserve a certain amount of
2587 * bandwidth for a reply. Check if we succeeded and if so send our
2590 * @param cls the requests "struct PendingRequest*"
2591 * @param peer identifies the peer
2592 * @param bpm_out set to the current bandwidth limit (sending) for this peer
2593 * @param amount set to the amount that was actually reserved or unreserved
2594 * @param preference current traffic preference for the given peer
2597 target_reservation_cb (void *cls,
2599 GNUNET_PeerIdentity * peer,
2600 struct GNUNET_BANDWIDTH_Value32NBO bpm_out,
2602 uint64_t preference)
2604 struct PendingRequest *pr = cls;
2605 struct ConnectedPeer *cp;
2606 struct PendingMessage *pm;
2607 struct GetMessage *gm;
2608 GNUNET_HashCode *ext;
2616 /* (3) transmit, update ttl/priority */
2617 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
2621 /* Peer must have just left */
2623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2624 "Selected peer disconnected!\n");
2626 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
2627 pr->task = GNUNET_SCHEDULER_add_delayed (get_processing_delay (),
2628 &forward_request_task,
2636 /* error in communication with core, try again later */
2637 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
2638 pr->task = GNUNET_SCHEDULER_add_delayed (get_processing_delay (),
2639 &forward_request_task,
2643 no_route = GNUNET_NO;
2649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2650 "Failed to reserve bandwidth for reply (got %d/%u bytes only)!\n",
2654 GNUNET_STATISTICS_update (stats,
2655 gettext_noop ("# reply bandwidth reservation requests failed"),
2658 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
2659 pr->task = GNUNET_SCHEDULER_add_delayed (get_processing_delay (),
2660 &forward_request_task,
2662 return; /* this target round failed */
2664 no_route = GNUNET_YES;
2667 GNUNET_STATISTICS_update (stats,
2668 gettext_noop ("# queries scheduled for forwarding"),
2671 for (i=0;i<pr->used_targets_off;i++)
2672 if (pr->used_targets[i].pid == cp->pid)
2674 GNUNET_STATISTICS_update (stats,
2675 gettext_noop ("# queries retransmitted to same target"),
2681 /* build message and insert message into priority queue */
2683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2684 "Forwarding request `%s' to `%4s'!\n",
2685 GNUNET_h2s (&pr->query),
2690 if (GNUNET_YES == no_route)
2692 bm |= GET_MESSAGE_BIT_RETURN_TO;
2695 if (pr->namespace != NULL)
2697 bm |= GET_MESSAGE_BIT_SKS_NAMESPACE;
2700 if (pr->target_pid != 0)
2702 bm |= GET_MESSAGE_BIT_TRANSMIT_TO;
2705 msize = sizeof (struct GetMessage) + pr->bf_size + k * sizeof(GNUNET_HashCode);
2706 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
2707 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
2709 gm = (struct GetMessage*) &pm[1];
2710 gm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_GET);
2711 gm->header.size = htons (msize);
2712 gm->type = htonl (pr->type);
2713 pr->remaining_priority /= 2;
2714 gm->priority = htonl (pr->remaining_priority);
2715 gm->ttl = htonl (pr->ttl);
2716 gm->filter_mutator = htonl(pr->mingle);
2717 gm->hash_bitmap = htonl (bm);
2718 gm->query = pr->query;
2719 ext = (GNUNET_HashCode*) &gm[1];
2721 if (GNUNET_YES == no_route)
2722 GNUNET_PEER_resolve (pr->cp->pid, (struct GNUNET_PeerIdentity*) &ext[k++]);
2723 if (pr->namespace != NULL)
2724 memcpy (&ext[k++], pr->namespace, sizeof (GNUNET_HashCode));
2725 if (pr->target_pid != 0)
2726 GNUNET_PEER_resolve (pr->target_pid, (struct GNUNET_PeerIdentity*) &ext[k++]);
2727 bfdata = (char *) &ext[k];
2729 GNUNET_CONTAINER_bloomfilter_get_raw_data (pr->bf,
2732 pm->cont = &transmit_query_continuation;
2734 cp->last_request_times[(cp->last_request_times_off++) % MAX_QUEUE_PER_PEER] = GNUNET_TIME_absolute_get ();
2735 add_to_pending_messages_for_peer (cp, pm, pr);
2740 * Closure used for "target_peer_select_cb".
2742 struct PeerSelectionContext
2745 * The request for which we are selecting
2748 struct PendingRequest *pr;
2751 * Current "prime" target.
2753 struct GNUNET_PeerIdentity target;
2756 * How much do we like this target?
2758 double target_score;
2761 * Does it make sense to we re-try quickly again?
2769 * Function called for each connected peer to determine
2770 * which one(s) would make good targets for forwarding.
2772 * @param cls closure (struct PeerSelectionContext)
2773 * @param key current key code (peer identity)
2774 * @param value value in the hash map (struct ConnectedPeer)
2775 * @return GNUNET_YES if we should continue to
2780 target_peer_select_cb (void *cls,
2781 const GNUNET_HashCode * key,
2784 struct PeerSelectionContext *psc = cls;
2785 struct ConnectedPeer *cp = value;
2786 struct PendingRequest *pr = psc->pr;
2787 struct GNUNET_TIME_Relative delay;
2792 /* 1) check that this peer is not the initiator */
2796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2797 "Skipping initiator in forwarding selection\n");
2799 return GNUNET_YES; /* skip */
2801 if (cp->irc != NULL)
2803 psc->fast_retry = GNUNET_YES;
2804 return GNUNET_YES; /* skip: already querying core about this peer for other reasons */
2807 /* 2) check if we have already (recently) forwarded to this peer */
2808 /* 2a) this particular request */
2810 for (i=0;i<pr->used_targets_off;i++)
2811 if (pr->used_targets[i].pid == cp->pid)
2813 pc = pr->used_targets[i].num_requests;
2814 GNUNET_assert (pc > 0);
2815 /* FIXME: make re-enabling a peer independent of how often
2816 this function is called??? */
2817 if (0 != GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2818 RETRY_PROBABILITY_INV * pc))
2821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2822 "NOT re-trying query that was previously transmitted %u times\n",
2825 return GNUNET_YES; /* skip */
2832 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2833 "Re-trying query that was previously transmitted %u times to this peer\n",
2837 /* 2b) many other requests to this peer */
2838 delay = GNUNET_TIME_absolute_get_duration (cp->last_request_times[cp->last_request_times_off % MAX_QUEUE_PER_PEER]);
2839 if (delay.rel_value <= cp->avg_delay.rel_value)
2842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2843 "NOT sending query since we send %u others to this peer in the last %llums\n",
2845 cp->avg_delay.rel_value);
2847 return GNUNET_YES; /* skip */
2850 /* 3) calculate how much we'd like to forward to this peer,
2851 starting with a random value that is strong enough
2852 to at least give any peer a chance sometimes
2853 (compared to the other factors that come later) */
2854 /* 3a) count successful (recent) routes from cp for same source */
2857 score = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2858 P2P_SUCCESS_LIST_SIZE);
2859 for (i=0;i<P2P_SUCCESS_LIST_SIZE;i++)
2860 if (cp->last_p2p_replies[i] == pr->cp->pid)
2861 score += 1.0; /* likely successful based on hot path */
2865 score = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2866 CS2P_SUCCESS_LIST_SIZE);
2867 for (i=0;i<CS2P_SUCCESS_LIST_SIZE;i++)
2868 if (cp->last_client_replies[i] == pr->client_request_list->client_list->client)
2869 score += 1.0; /* likely successful based on hot path */
2871 /* 3b) include latency */
2872 if (cp->avg_delay.rel_value < 4 * TTL_DECREMENT)
2873 score += 1.0; /* likely fast based on latency */
2874 /* 3c) include priorities */
2875 if (cp->avg_priority <= pr->remaining_priority / 2.0)
2876 score += 1.0; /* likely successful based on priorities */
2877 /* 3d) penalize for queue size */
2878 score -= (2.0 * cp->pending_requests / (double) MAX_QUEUE_PER_PEER);
2879 /* 3e) include peer proximity */
2880 score -= (2.0 * (GNUNET_CRYPTO_hash_distance_u32 (key,
2881 &pr->query)) / (double) UINT32_MAX);
2882 /* 4) super-bonus for being the known target */
2883 if (pr->target_pid == cp->pid)
2885 /* store best-fit in closure */
2886 score++; /* avoid zero */
2887 if (score > psc->target_score)
2889 psc->target_score = score;
2890 psc->target.hashPubKey = *key;
2893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2894 "Peer `%s' gets score %f for forwarding query, max is %8f\n",
2904 * The priority level imposes a bound on the maximum
2905 * value for the ttl that can be requested.
2907 * @param ttl_in requested ttl
2908 * @param prio given priority
2909 * @return ttl_in if ttl_in is below the limit,
2910 * otherwise the ttl-limit for the given priority
2913 bound_ttl (int32_t ttl_in, uint32_t prio)
2915 unsigned long long allowed;
2919 allowed = ((unsigned long long) prio) * TTL_DECREMENT / 1000;
2920 if (ttl_in > allowed)
2922 if (allowed >= (1 << 30))
2931 * Iterator called on each result obtained for a DHT
2932 * operation that expects a reply
2934 * @param cls closure
2935 * @param exp when will this value expire
2936 * @param key key of the result
2937 * @param get_path NULL-terminated array of pointers
2938 * to the peers on reverse GET path (or NULL if not recorded)
2939 * @param put_path NULL-terminated array of pointers
2940 * to the peers on the PUT path (or NULL if not recorded)
2941 * @param type type of the result
2942 * @param size number of bytes in data
2943 * @param data pointer to the result data
2946 process_dht_reply (void *cls,
2947 struct GNUNET_TIME_Absolute exp,
2948 const GNUNET_HashCode * key,
2949 const struct GNUNET_PeerIdentity * const *get_path,
2950 const struct GNUNET_PeerIdentity * const *put_path,
2951 enum GNUNET_BLOCK_Type type,
2957 * We're processing a GET request and have decided
2958 * to forward it to other peers. This function is called periodically
2959 * and should forward the request to other peers until we have all
2960 * possible replies. If we have transmitted the *only* reply to
2961 * the initiator we should destroy the pending request. If we have
2962 * many replies in the queue to the initiator, we should delay sending
2963 * out more queries until the reply queue has shrunk some.
2965 * @param cls our "struct ProcessGetContext *"
2969 forward_request_task (void *cls,
2970 const struct GNUNET_SCHEDULER_TaskContext *tc)
2972 struct PendingRequest *pr = cls;
2973 struct PeerSelectionContext psc;
2974 struct ConnectedPeer *cp;
2975 struct GNUNET_TIME_Relative delay;
2977 pr->task = GNUNET_SCHEDULER_NO_TASK;
2978 if (pr->pirc != NULL)
2981 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2982 "Forwarding of query `%s' not attempted due to pending local lookup!\n",
2983 GNUNET_h2s (&pr->query));
2985 return; /* already pending */
2987 if (GNUNET_YES == pr->local_only)
2988 return; /* configured to not do P2P search */
2990 if ( (0 == pr->anonymity_level) &&
2991 (GNUNET_YES != pr->forward_only) &&
2992 (pr->type != GNUNET_BLOCK_TYPE_FS_DBLOCK) &&
2993 (pr->type != GNUNET_BLOCK_TYPE_FS_IBLOCK) )
2995 pr->dht_get = GNUNET_DHT_get_start (dht_handle,
2996 GNUNET_TIME_UNIT_FOREVER_REL,
2999 DEFAULT_GET_REPLICATION,
3004 (pr->namespace != NULL) ? sizeof (GNUNET_HashCode) : 0,
3008 /* (1) select target */
3010 psc.target_score = -DBL_MAX;
3011 psc.fast_retry = GNUNET_NO;
3012 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
3013 &target_peer_select_cb,
3015 if (psc.target_score == -DBL_MAX)
3017 if (psc.fast_retry == GNUNET_YES)
3018 delay = GNUNET_TIME_UNIT_MILLISECONDS; /* FIXME: store adaptive fast-retry value in 'pr' */
3020 delay = get_processing_delay ();
3022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3023 "No peer selected for forwarding of query `%s', will try again in %llu ms!\n",
3024 GNUNET_h2s (&pr->query),
3027 pr->task = GNUNET_SCHEDULER_add_delayed (delay,
3028 &forward_request_task,
3030 return; /* nobody selected */
3032 /* (3) update TTL/priority */
3033 if (pr->client_request_list != NULL)
3035 /* FIXME: use better algorithm!? */
3036 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
3039 /* bound priority we use by priorities we see from other peers
3040 rounded up (must round up so that we can see non-zero
3041 priorities, but round up as little as possible to make it
3042 plausible that we forwarded another peers request) */
3043 if (pr->priority > current_priorities + 1.0)
3044 pr->priority = (uint32_t) current_priorities + 1.0;
3045 pr->ttl = bound_ttl (pr->ttl + TTL_DECREMENT * 2,
3048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3049 "Trying query `%s' with priority %u and TTL %d.\n",
3050 GNUNET_h2s (&pr->query),
3056 /* (3) reserve reply bandwidth */
3057 if (GNUNET_NO == pr->forward_only)
3059 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
3060 &psc.target.hashPubKey);
3061 GNUNET_assert (NULL != cp);
3062 GNUNET_assert (cp->irc == NULL);
3065 cp->irc = GNUNET_CORE_peer_change_preference (core,
3067 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
3068 GNUNET_BANDWIDTH_value_init (UINT32_MAX),
3071 &target_reservation_cb,
3073 GNUNET_assert (cp->irc != NULL);
3074 cp->inc_preference = 0;
3078 /* force forwarding */
3079 static struct GNUNET_BANDWIDTH_Value32NBO zerobw;
3080 target_reservation_cb (pr, &psc.target,
3086 /* **************************** P2P PUT Handling ************************ */
3090 * Function called after we either failed or succeeded
3091 * at transmitting a reply to a peer.
3093 * @param cls the requests "struct PendingRequest*"
3094 * @param tpid ID of receiving peer, 0 on transmission error
3097 transmit_reply_continuation (void *cls,
3098 GNUNET_PEER_Id tpid)
3100 struct PendingRequest *pr = cls;
3104 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
3105 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
3106 /* only one reply expected, done with the request! */
3107 destroy_pending_request (pr);
3109 case GNUNET_BLOCK_TYPE_ANY:
3110 case GNUNET_BLOCK_TYPE_FS_KBLOCK:
3111 case GNUNET_BLOCK_TYPE_FS_SBLOCK:
3121 * Transmit the given message by copying it to the target buffer
3122 * "buf". "buf" will be NULL and "size" zero if the socket was closed
3123 * for writing in the meantime. In that case, do nothing
3124 * (the disconnect or shutdown handler will take care of the rest).
3125 * If we were able to transmit messages and there are still more
3126 * pending, ask core again for further calls to this function.
3128 * @param cls closure, pointer to the 'struct ClientList*'
3129 * @param size number of bytes available in buf
3130 * @param buf where the callee should write the message
3131 * @return number of bytes written to buf
3134 transmit_to_client (void *cls,
3135 size_t size, void *buf)
3137 struct ClientList *cl = cls;
3139 struct ClientResponseMessage *creply;
3146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3147 "Not sending reply, client communication problem.\n");
3152 while ( (NULL != (creply = cl->res_head) ) &&
3153 (creply->msize <= size) )
3155 memcpy (&cbuf[msize], &creply[1], creply->msize);
3156 msize += creply->msize;
3157 size -= creply->msize;
3158 GNUNET_CONTAINER_DLL_remove (cl->res_head,
3161 GNUNET_free (creply);
3164 cl->th = GNUNET_SERVER_notify_transmit_ready (cl->client,
3166 GNUNET_TIME_UNIT_FOREVER_REL,
3167 &transmit_to_client,
3170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3171 "Transmitted %u bytes to client\n",
3172 (unsigned int) msize);
3179 * Closure for "process_reply" function.
3181 struct ProcessReplyClosure
3184 * The data for the reply.
3189 * Who gave us this reply? NULL for local host (or DHT)
3191 struct ConnectedPeer *sender;
3194 * When the reply expires.
3196 struct GNUNET_TIME_Absolute expiration;
3204 * Type of the block.
3206 enum GNUNET_BLOCK_Type type;
3209 * How much was this reply worth to us?
3214 * Evaluation result (returned).
3216 enum GNUNET_BLOCK_EvaluationResult eval;
3219 * Did we finish processing the associated request?
3224 * Did we find a matching request?
3231 * We have received a reply; handle it!
3233 * @param cls response (struct ProcessReplyClosure)
3234 * @param key our query
3235 * @param value value in the hash map (info about the query)
3236 * @return GNUNET_YES (we should continue to iterate)
3239 process_reply (void *cls,
3240 const GNUNET_HashCode * key,
3243 struct ProcessReplyClosure *prq = cls;
3244 struct PendingRequest *pr = value;
3245 struct PendingMessage *reply;
3246 struct ClientResponseMessage *creply;
3247 struct ClientList *cl;
3248 struct PutMessage *pm;
3249 struct ConnectedPeer *cp;
3250 struct GNUNET_TIME_Relative cur_delay;
3252 struct GNUNET_TIME_Relative art_delay;
3258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3259 "Matched result (type %u) for query `%s' with pending request\n",
3260 (unsigned int) prq->type,
3263 GNUNET_STATISTICS_update (stats,
3264 gettext_noop ("# replies received and matched"),
3267 if (prq->sender != NULL)
3269 for (i=0;i<pr->used_targets_off;i++)
3270 if (pr->used_targets[i].pid == prq->sender->pid)
3272 if (i < pr->used_targets_off)
3274 cur_delay = GNUNET_TIME_absolute_get_duration (pr->used_targets[i].last_request_time);
3275 prq->sender->avg_delay.rel_value
3276 = (prq->sender->avg_delay.rel_value *
3277 (RUNAVG_DELAY_N - 1) + cur_delay.rel_value) / RUNAVG_DELAY_N;
3278 prq->sender->avg_priority
3279 = (prq->sender->avg_priority *
3280 (RUNAVG_DELAY_N - 1) + pr->priority) / (double) RUNAVG_DELAY_N;
3284 GNUNET_PEER_change_rc (prq->sender->last_p2p_replies
3285 [prq->sender->last_p2p_replies_woff % P2P_SUCCESS_LIST_SIZE],
3287 GNUNET_PEER_change_rc (pr->cp->pid, 1);
3288 prq->sender->last_p2p_replies
3289 [(prq->sender->last_p2p_replies_woff++) % P2P_SUCCESS_LIST_SIZE]
3294 if (NULL != prq->sender->last_client_replies
3295 [(prq->sender->last_client_replies_woff) % CS2P_SUCCESS_LIST_SIZE])
3296 GNUNET_SERVER_client_drop (prq->sender->last_client_replies
3297 [(prq->sender->last_client_replies_woff) % CS2P_SUCCESS_LIST_SIZE]);
3298 prq->sender->last_client_replies
3299 [(prq->sender->last_client_replies_woff++) % CS2P_SUCCESS_LIST_SIZE]
3300 = pr->client_request_list->client_list->client;
3301 GNUNET_SERVER_client_keep (pr->client_request_list->client_list->client);
3304 prq->eval = GNUNET_BLOCK_evaluate (block_ctx,
3309 pr->namespace, (pr->namespace != NULL) ? sizeof (GNUNET_HashCode) : 0,
3314 case GNUNET_BLOCK_EVALUATION_OK_MORE:
3316 case GNUNET_BLOCK_EVALUATION_OK_LAST:
3317 while (NULL != pr->pending_head)
3318 destroy_pending_message_list_entry (pr->pending_head);
3321 if (pr->client_request_list != NULL)
3322 GNUNET_SERVER_receive_done (pr->client_request_list->client_list->client,
3324 GNUNET_DATASTORE_cancel (pr->qe);
3327 pr->do_remove = GNUNET_YES;
3328 if (pr->task != GNUNET_SCHEDULER_NO_TASK)
3330 GNUNET_SCHEDULER_cancel (pr->task);
3331 pr->task = GNUNET_SCHEDULER_NO_TASK;
3333 GNUNET_break (GNUNET_YES ==
3334 GNUNET_CONTAINER_multihashmap_remove (query_request_map,
3337 GNUNET_LOAD_update (rt_entry_lifetime,
3338 GNUNET_TIME_absolute_get_duration (pr->start_time).rel_value);
3340 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
3341 GNUNET_STATISTICS_update (stats,
3342 gettext_noop ("# duplicate replies discarded (bloomfilter)"),
3346 /* GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3347 "Duplicate response `%s', discarding.\n",
3348 GNUNET_h2s (&mhash));*/
3350 return GNUNET_YES; /* duplicate */
3351 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
3352 return GNUNET_YES; /* wrong namespace */
3353 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
3356 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
3359 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
3360 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3361 _("Unsupported block type %u\n"),
3365 if (pr->client_request_list != NULL)
3367 if (pr->replies_seen_size == pr->replies_seen_off)
3368 GNUNET_array_grow (pr->replies_seen,
3369 pr->replies_seen_size,
3370 pr->replies_seen_size * 2 + 4);
3371 GNUNET_CRYPTO_hash (prq->data,
3373 &pr->replies_seen[pr->replies_seen_off++]);
3374 refresh_bloomfilter (pr);
3376 if (NULL == prq->sender)
3379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3380 "Found result for query `%s' in local datastore\n",
3383 GNUNET_STATISTICS_update (stats,
3384 gettext_noop ("# results found locally"),
3388 prq->priority += pr->remaining_priority;
3389 pr->remaining_priority = 0;
3390 pr->results_found++;
3391 prq->request_found = GNUNET_YES;
3392 if (NULL != pr->client_request_list)
3394 GNUNET_STATISTICS_update (stats,
3395 gettext_noop ("# replies received for local clients"),
3398 cl = pr->client_request_list->client_list;
3399 msize = sizeof (struct PutMessage) + prq->size;
3400 creply = GNUNET_malloc (msize + sizeof (struct ClientResponseMessage));
3401 creply->msize = msize;
3402 creply->client_list = cl;
3403 GNUNET_CONTAINER_DLL_insert_after (cl->res_head,
3407 pm = (struct PutMessage*) &creply[1];
3408 pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
3409 pm->header.size = htons (msize);
3410 pm->type = htonl (prq->type);
3411 pm->expiration = GNUNET_TIME_absolute_hton (prq->expiration);
3412 memcpy (&pm[1], prq->data, prq->size);
3416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3417 "Transmitting result for query `%s' to client\n",
3420 cl->th = GNUNET_SERVER_notify_transmit_ready (cl->client,
3422 GNUNET_TIME_UNIT_FOREVER_REL,
3423 &transmit_to_client,
3426 GNUNET_break (cl->th != NULL);
3429 prq->finished = GNUNET_YES;
3430 destroy_pending_request (pr);
3437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3438 "Transmitting result for query `%s' to other peer (PID=%u)\n",
3440 (unsigned int) cp->pid);
3442 GNUNET_STATISTICS_update (stats,
3443 gettext_noop ("# replies received for other peers"),
3446 msize = sizeof (struct PutMessage) + prq->size;
3447 reply = GNUNET_malloc (msize + sizeof (struct PendingMessage));
3448 reply->cont = &transmit_reply_continuation;
3449 reply->cont_cls = pr;
3451 art_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
3452 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
3455 = GNUNET_TIME_relative_to_absolute (art_delay);
3456 GNUNET_STATISTICS_update (stats,
3457 gettext_noop ("cummulative artificial delay introduced (ms)"),
3458 art_delay.abs_value,
3461 reply->msize = msize;
3462 reply->priority = UINT32_MAX; /* send replies first! */
3463 pm = (struct PutMessage*) &reply[1];
3464 pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
3465 pm->header.size = htons (msize);
3466 pm->type = htonl (prq->type);
3467 pm->expiration = GNUNET_TIME_absolute_hton (prq->expiration);
3468 memcpy (&pm[1], prq->data, prq->size);
3469 add_to_pending_messages_for_peer (cp, reply, pr);
3476 * Iterator called on each result obtained for a DHT
3477 * operation that expects a reply
3479 * @param cls closure
3480 * @param exp when will this value expire
3481 * @param key key of the result
3482 * @param get_path NULL-terminated array of pointers
3483 * to the peers on reverse GET path (or NULL if not recorded)
3484 * @param put_path NULL-terminated array of pointers
3485 * to the peers on the PUT path (or NULL if not recorded)
3486 * @param type type of the result
3487 * @param size number of bytes in data
3488 * @param data pointer to the result data
3491 process_dht_reply (void *cls,
3492 struct GNUNET_TIME_Absolute exp,
3493 const GNUNET_HashCode * key,
3494 const struct GNUNET_PeerIdentity * const *get_path,
3495 const struct GNUNET_PeerIdentity * const *put_path,
3496 enum GNUNET_BLOCK_Type type,
3500 struct PendingRequest *pr = cls;
3501 struct ProcessReplyClosure prq;
3503 memset (&prq, 0, sizeof (prq));
3505 prq.expiration = exp;
3508 process_reply (&prq, key, pr);
3514 * Continuation called to notify client about result of the
3517 * @param cls closure
3518 * @param success GNUNET_SYSERR on failure
3519 * @param msg NULL on success, otherwise an error message
3522 put_migration_continuation (void *cls,
3526 struct GNUNET_TIME_Absolute *start = cls;
3527 struct GNUNET_TIME_Relative delay;
3529 delay = GNUNET_TIME_absolute_get_duration (*start);
3530 GNUNET_free (start);
3531 GNUNET_LOAD_update (datastore_put_load,
3533 if (GNUNET_OK == success)
3535 GNUNET_STATISTICS_update (stats,
3536 gettext_noop ("# datastore 'put' failures"),
3543 * Handle P2P "PUT" message.
3545 * @param cls closure, always NULL
3546 * @param other the other peer involved (sender or receiver, NULL
3547 * for loopback messages where we are both sender and receiver)
3548 * @param message the actual message
3549 * @param atsi performance information
3550 * @return GNUNET_OK to keep the connection open,
3551 * GNUNET_SYSERR to close it (signal serious error)
3554 handle_p2p_put (void *cls,
3555 const struct GNUNET_PeerIdentity *other,
3556 const struct GNUNET_MessageHeader *message,
3557 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
3559 const struct PutMessage *put;
3562 enum GNUNET_BLOCK_Type type;
3563 struct GNUNET_TIME_Absolute expiration;
3564 GNUNET_HashCode query;
3565 struct ProcessReplyClosure prq;
3566 struct GNUNET_TIME_Absolute *start;
3567 struct GNUNET_TIME_Relative block_time;
3569 struct ConnectedPeer *cp;
3570 struct PendingMessage *pm;
3571 struct MigrationStopMessage *msm;
3573 msize = ntohs (message->size);
3574 if (msize < sizeof (struct PutMessage))
3577 return GNUNET_SYSERR;
3579 put = (const struct PutMessage*) message;
3580 dsize = msize - sizeof (struct PutMessage);
3581 type = ntohl (put->type);
3582 expiration = GNUNET_TIME_absolute_ntoh (put->expiration);
3584 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
3585 return GNUNET_SYSERR;
3587 GNUNET_BLOCK_get_key (block_ctx,
3593 GNUNET_break_op (0);
3594 return GNUNET_SYSERR;
3597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3598 "Received result for query `%s' from peer `%4s'\n",
3599 GNUNET_h2s (&query),
3600 GNUNET_i2s (other));
3602 GNUNET_STATISTICS_update (stats,
3603 gettext_noop ("# replies received (overall)"),
3606 /* now, lookup 'query' */
3607 prq.data = (const void*) &put[1];
3609 prq.sender = GNUNET_CONTAINER_multihashmap_get (connected_peers,
3610 &other->hashPubKey);
3615 prq.expiration = expiration;
3617 prq.finished = GNUNET_NO;
3618 prq.request_found = GNUNET_NO;
3619 GNUNET_CONTAINER_multihashmap_get_multiple (query_request_map,
3623 if (prq.sender != NULL)
3625 prq.sender->inc_preference += CONTENT_BANDWIDTH_VALUE + 1000 * prq.priority;
3626 change_host_trust (prq.sender, prq.priority);
3628 if ( (GNUNET_YES == active_to_migration) &&
3629 (GNUNET_NO == test_put_load_too_high (prq.priority)) )
3632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3633 "Replicating result for query `%s' with priority %u\n",
3634 GNUNET_h2s (&query),
3637 start = GNUNET_malloc (sizeof (struct GNUNET_TIME_Absolute));
3638 *start = GNUNET_TIME_absolute_get ();
3639 GNUNET_DATASTORE_put (dsh,
3640 0, &query, dsize, &put[1],
3641 type, prq.priority, 1 /* anonymity */,
3643 1 + prq.priority, MAX_DATASTORE_QUEUE,
3644 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
3645 &put_migration_continuation,
3648 putl = GNUNET_LOAD_get_load (datastore_put_load);
3649 if ( (GNUNET_NO == prq.request_found) &&
3650 ( (GNUNET_YES != active_to_migration) ||
3651 (putl > 2.5 * (1 + prq.priority)) ) )
3653 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
3654 &other->hashPubKey);
3655 if (GNUNET_TIME_absolute_get_duration (cp->last_migration_block).rel_value < 5000)
3656 return GNUNET_OK; /* already blocked */
3657 /* We're too busy; send MigrationStop message! */
3658 if (GNUNET_YES != active_to_migration)
3659 putl = 1.0 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 5);
3660 block_time = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
3661 5000 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
3662 (unsigned int) (60000 * putl * putl)));
3664 cp->last_migration_block = GNUNET_TIME_relative_to_absolute (block_time);
3665 pm = GNUNET_malloc (sizeof (struct PendingMessage) +
3666 sizeof (struct MigrationStopMessage));
3667 pm->msize = sizeof (struct MigrationStopMessage);
3668 pm->priority = UINT32_MAX;
3669 msm = (struct MigrationStopMessage*) &pm[1];
3670 msm->header.size = htons (sizeof (struct MigrationStopMessage));
3671 msm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_MIGRATION_STOP);
3672 msm->duration = GNUNET_TIME_relative_hton (block_time);
3673 add_to_pending_messages_for_peer (cp,
3682 * Handle P2P "MIGRATION_STOP" message.
3684 * @param cls closure, always NULL
3685 * @param other the other peer involved (sender or receiver, NULL
3686 * for loopback messages where we are both sender and receiver)
3687 * @param message the actual message
3688 * @param atsi performance information
3689 * @return GNUNET_OK to keep the connection open,
3690 * GNUNET_SYSERR to close it (signal serious error)
3693 handle_p2p_migration_stop (void *cls,
3694 const struct GNUNET_PeerIdentity *other,
3695 const struct GNUNET_MessageHeader *message,
3696 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
3698 struct ConnectedPeer *cp;
3699 const struct MigrationStopMessage *msm;
3701 msm = (const struct MigrationStopMessage*) message;
3702 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
3703 &other->hashPubKey);
3709 cp->migration_blocked = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_ntoh (msm->duration));
3715 /* **************************** P2P GET Handling ************************ */
3719 * Closure for 'check_duplicate_request_{peer,client}'.
3721 struct CheckDuplicateRequestClosure
3724 * The new request we should check if it already exists.
3726 const struct PendingRequest *pr;
3729 * Existing request found by the checker, NULL if none.
3731 struct PendingRequest *have;
3736 * Iterator over entries in the 'query_request_map' that
3737 * tries to see if we have the same request pending from
3738 * the same client already.
3740 * @param cls closure (our 'struct CheckDuplicateRequestClosure')
3741 * @param key current key code (query, ignored, must match)
3742 * @param value value in the hash map (a 'struct PendingRequest'
3743 * that already exists)
3744 * @return GNUNET_YES if we should continue to
3745 * iterate (no match yet)
3746 * GNUNET_NO if not (match found).
3749 check_duplicate_request_client (void *cls,
3750 const GNUNET_HashCode * key,
3753 struct CheckDuplicateRequestClosure *cdc = cls;
3754 struct PendingRequest *have = value;
3756 if (have->client_request_list == NULL)
3758 if ( (cdc->pr->client_request_list->client_list->client == have->client_request_list->client_list->client) &&
3769 * We're processing (local) results for a search request
3770 * from another peer. Pass applicable results to the
3771 * peer and if we are done either clean up (operation
3772 * complete) or forward to other peers (more results possible).
3774 * @param cls our closure (struct LocalGetContext)
3775 * @param key key for the content
3776 * @param size number of bytes in data
3777 * @param data content stored
3778 * @param type type of the content
3779 * @param priority priority of the content
3780 * @param anonymity anonymity-level for the content
3781 * @param expiration expiration time for the content
3782 * @param uid unique identifier for the datum;
3783 * maybe 0 if no unique identifier is available
3786 process_local_reply (void *cls,
3787 const GNUNET_HashCode * key,
3790 enum GNUNET_BLOCK_Type type,
3793 struct GNUNET_TIME_Absolute
3797 struct PendingRequest *pr = cls;
3798 struct ProcessReplyClosure prq;
3799 struct CheckDuplicateRequestClosure cdrc;
3800 GNUNET_HashCode query;
3801 unsigned int old_rf;
3806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3807 "Done processing local replies, forwarding request to other peers.\n");
3810 if (pr->client_request_list != NULL)
3812 GNUNET_SERVER_receive_done (pr->client_request_list->client_list->client,
3814 /* Figure out if this is a duplicate request and possibly
3815 merge 'struct PendingRequest' entries */
3818 GNUNET_CONTAINER_multihashmap_get_multiple (query_request_map,
3820 &check_duplicate_request_client,
3822 if (cdrc.have != NULL)
3825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3826 "Received request for block `%s' twice from client, will only request once.\n",
3827 GNUNET_h2s (&pr->query));
3830 destroy_pending_request (pr);
3834 if (pr->local_only == GNUNET_YES)
3836 destroy_pending_request (pr);
3839 /* no more results */
3840 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
3841 pr->task = GNUNET_SCHEDULER_add_now (&forward_request_task,
3846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3847 "New local response to `%s' of type %u.\n",
3851 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
3854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3855 "Found ONDEMAND block, performing on-demand encoding\n");
3857 GNUNET_STATISTICS_update (stats,
3858 gettext_noop ("# on-demand blocks matched requests"),
3862 GNUNET_FS_handle_on_demand_block (key, size, data, type, priority,
3863 anonymity, expiration, uid,
3864 &process_local_reply,
3868 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
3872 old_rf = pr->results_found;
3873 memset (&prq, 0, sizeof (prq));
3875 prq.expiration = expiration;
3878 GNUNET_BLOCK_get_key (block_ctx,
3885 GNUNET_DATASTORE_remove (dsh,
3889 GNUNET_TIME_UNIT_FOREVER_REL,
3891 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
3895 prq.priority = priority;
3896 prq.finished = GNUNET_NO;
3897 prq.request_found = GNUNET_NO;
3898 if ( (old_rf == 0) &&
3899 (pr->results_found == 0) )
3900 update_datastore_delays (pr->start_time);
3901 process_reply (&prq, key, pr);
3902 if (prq.finished == GNUNET_YES)
3905 return; /* done here */
3906 if (prq.eval == GNUNET_BLOCK_EVALUATION_OK_LAST)
3908 pr->local_only = GNUNET_YES; /* do not forward */
3909 GNUNET_DATASTORE_get_next (dsh, GNUNET_NO);
3912 if ( (pr->client_request_list == NULL) &&
3913 ( (GNUNET_YES == test_get_load_too_high (0)) ||
3914 (pr->results_found > 5 + 2 * pr->priority) ) )
3917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3918 "Load too high, done with request\n");
3920 GNUNET_STATISTICS_update (stats,
3921 gettext_noop ("# processing result set cut short due to load"),
3924 GNUNET_DATASTORE_get_next (dsh, GNUNET_NO);
3927 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
3932 * We've received a request with the specified priority. Bound it
3933 * according to how much we trust the given peer.
3935 * @param prio_in requested priority
3936 * @param cp the peer making the request
3937 * @return effective priority
3940 bound_priority (uint32_t prio_in,
3941 struct ConnectedPeer *cp)
3943 #define N ((double)128.0)
3948 ld = test_get_load_too_high (0);
3949 if (ld == GNUNET_SYSERR)
3951 GNUNET_STATISTICS_update (stats,
3952 gettext_noop ("# requests done for free (low load)"),
3955 return 0; /* excess resources */
3957 if (prio_in > INT32_MAX)
3958 prio_in = INT32_MAX;
3959 ret = - change_host_trust (cp, - (int) prio_in);
3962 if (ret > current_priorities + N)
3963 rret = current_priorities + N;
3967 = (current_priorities * (N-1) + rret)/N;
3969 if ( (ld == GNUNET_YES) && (ret > 0) )
3971 /* try with charging */
3972 ld = test_get_load_too_high (ret);
3974 if (ld == GNUNET_YES)
3976 GNUNET_STATISTICS_update (stats,
3977 gettext_noop ("# request dropped, priority insufficient"),
3981 change_host_trust (cp, (int) ret);
3982 return -1; /* not enough resources */
3986 GNUNET_STATISTICS_update (stats,
3987 gettext_noop ("# requests done for a price (normal load)"),
3997 * Iterator over entries in the 'query_request_map' that
3998 * tries to see if we have the same request pending from
3999 * the same peer already.
4001 * @param cls closure (our 'struct CheckDuplicateRequestClosure')
4002 * @param key current key code (query, ignored, must match)
4003 * @param value value in the hash map (a 'struct PendingRequest'
4004 * that already exists)
4005 * @return GNUNET_YES if we should continue to
4006 * iterate (no match yet)
4007 * GNUNET_NO if not (match found).
4010 check_duplicate_request_peer (void *cls,
4011 const GNUNET_HashCode * key,
4014 struct CheckDuplicateRequestClosure *cdc = cls;
4015 struct PendingRequest *have = value;
4017 if (cdc->pr->target_pid == have->target_pid)
4027 * Handle P2P "GET" request.
4029 * @param cls closure, always NULL
4030 * @param other the other peer involved (sender or receiver, NULL
4031 * for loopback messages where we are both sender and receiver)
4032 * @param message the actual message
4033 * @param atsi performance information
4034 * @return GNUNET_OK to keep the connection open,
4035 * GNUNET_SYSERR to close it (signal serious error)
4038 handle_p2p_get (void *cls,
4039 const struct GNUNET_PeerIdentity *other,
4040 const struct GNUNET_MessageHeader *message,
4041 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
4043 struct PendingRequest *pr;
4044 struct ConnectedPeer *cp;
4045 struct ConnectedPeer *cps;
4046 struct CheckDuplicateRequestClosure cdc;
4047 struct GNUNET_TIME_Relative timeout;
4049 const struct GetMessage *gm;
4051 const GNUNET_HashCode *opt;
4054 uint32_t ttl_decrement;
4056 enum GNUNET_BLOCK_Type type;
4059 msize = ntohs(message->size);
4060 if (msize < sizeof (struct GetMessage))
4062 GNUNET_break_op (0);
4063 return GNUNET_SYSERR;
4065 gm = (const struct GetMessage*) message;
4067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4068 "Received request for `%s'\n",
4069 GNUNET_h2s (&gm->query));
4071 type = ntohl (gm->type);
4072 bm = ntohl (gm->hash_bitmap);
4080 if (msize < sizeof (struct GetMessage) + bits * sizeof (GNUNET_HashCode))
4082 GNUNET_break_op (0);
4083 return GNUNET_SYSERR;
4085 opt = (const GNUNET_HashCode*) &gm[1];
4086 bfsize = msize - sizeof (struct GetMessage) - bits * sizeof (GNUNET_HashCode);
4087 /* bfsize must be power of 2, check! */
4088 if (0 != ( (bfsize - 1) & bfsize))
4090 GNUNET_break_op (0);
4091 return GNUNET_SYSERR;
4093 bm = ntohl (gm->hash_bitmap);
4095 cps = GNUNET_CONTAINER_multihashmap_get (connected_peers,
4096 &other->hashPubKey);
4099 /* peer must have just disconnected */
4100 GNUNET_STATISTICS_update (stats,
4101 gettext_noop ("# requests dropped due to initiator not being connected"),
4104 return GNUNET_SYSERR;
4106 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
4107 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
4114 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
4115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4116 "Failed to find RETURN-TO peer `%4s' in connection set. Dropping query.\n",
4117 GNUNET_i2s ((const struct GNUNET_PeerIdentity*) &opt[bits-1]));
4120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4121 "Failed to find peer `%4s' in connection set. Dropping query.\n",
4122 GNUNET_i2s (other));
4124 GNUNET_STATISTICS_update (stats,
4125 gettext_noop ("# requests dropped due to missing reverse route"),
4128 /* FIXME: try connect? */
4131 /* note that we can really only check load here since otherwise
4132 peers could find out that we are overloaded by not being
4133 disconnected after sending us a malformed query... */
4134 priority = bound_priority (ntohl (gm->priority), cps);
4138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4139 "Dropping query from `%s', this peer is too busy.\n",
4140 GNUNET_i2s (other));
4145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4146 "Received request for `%s' of type %u from peer `%4s' with flags %u\n",
4147 GNUNET_h2s (&gm->query),
4148 (unsigned int) type,
4152 have_ns = (0 != (bm & GET_MESSAGE_BIT_SKS_NAMESPACE));
4153 pr = GNUNET_malloc (sizeof (struct PendingRequest) +
4154 (have_ns ? sizeof(GNUNET_HashCode) : 0));
4157 pr->namespace = (GNUNET_HashCode*) &pr[1];
4158 memcpy (&pr[1], &opt[bits++], sizeof (GNUNET_HashCode));
4160 if ( (GNUNET_LOAD_get_load (cp->transmission_delay) > 3 * (1 + priority)) ||
4161 (GNUNET_LOAD_get_average (cp->transmission_delay) >
4162 GNUNET_CONSTANTS_MAX_CORK_DELAY.rel_value * 2 + GNUNET_LOAD_get_average (rt_entry_lifetime)) )
4164 /* don't have BW to send to peer, or would likely take longer than we have for it,
4165 so at best indirect the query */
4167 pr->forward_only = GNUNET_YES;
4170 pr->mingle = ntohl (gm->filter_mutator);
4171 if (0 != (bm & GET_MESSAGE_BIT_TRANSMIT_TO))
4172 pr->target_pid = GNUNET_PEER_intern ((const struct GNUNET_PeerIdentity*) &opt[bits++]);
4173 pr->anonymity_level = 1;
4174 pr->priority = (uint32_t) priority;
4175 pr->ttl = bound_ttl (ntohl (gm->ttl), pr->priority);
4176 pr->query = gm->query;
4177 /* decrement ttl (always) */
4178 ttl_decrement = 2 * TTL_DECREMENT +
4179 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
4181 if ( (pr->ttl < 0) &&
4182 (((int32_t)(pr->ttl - ttl_decrement)) > 0) )
4185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4186 "Dropping query from `%s' due to TTL underflow (%d - %u).\n",
4191 GNUNET_STATISTICS_update (stats,
4192 gettext_noop ("# requests dropped due TTL underflow"),
4195 /* integer underflow => drop (should be very rare)! */
4199 pr->ttl -= ttl_decrement;
4200 pr->start_time = GNUNET_TIME_absolute_get ();
4202 /* get bloom filter */
4205 pr->bf = GNUNET_CONTAINER_bloomfilter_init ((const char*) &opt[bits],
4208 pr->bf_size = bfsize;
4212 GNUNET_CONTAINER_multihashmap_get_multiple (query_request_map,
4214 &check_duplicate_request_peer,
4216 if (cdc.have != NULL)
4218 if (cdc.have->start_time.abs_value + cdc.have->ttl >=
4219 pr->start_time.abs_value + pr->ttl)
4221 /* existing request has higher TTL, drop new one! */
4222 cdc.have->priority += pr->priority;
4223 destroy_pending_request (pr);
4225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4226 "Have existing request with higher TTL, dropping new request.\n",
4227 GNUNET_i2s (other));
4229 GNUNET_STATISTICS_update (stats,
4230 gettext_noop ("# requests dropped due to higher-TTL request"),
4237 /* existing request has lower TTL, drop old one! */
4238 pr->priority += cdc.have->priority;
4239 /* Possible optimization: if we have applicable pending
4240 replies in 'cdc.have', we might want to move those over
4241 (this is a really rare special-case, so it is not clear
4242 that this would be worth it) */
4243 destroy_pending_request (cdc.have);
4244 /* keep processing 'pr'! */
4249 GNUNET_break (GNUNET_OK ==
4250 GNUNET_CONTAINER_multihashmap_put (query_request_map,
4253 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
4254 GNUNET_break (GNUNET_OK ==
4255 GNUNET_CONTAINER_multihashmap_put (peer_request_map,
4258 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
4260 pr->hnode = GNUNET_CONTAINER_heap_insert (requests_by_expiration_heap,
4262 pr->start_time.abs_value + pr->ttl);
4264 GNUNET_STATISTICS_update (stats,
4265 gettext_noop ("# P2P searches received"),
4268 GNUNET_STATISTICS_update (stats,
4269 gettext_noop ("# P2P searches active"),
4273 /* calculate change in traffic preference */
4274 cps->inc_preference += pr->priority * 1000 + QUERY_BANDWIDTH_VALUE;
4275 /* process locally */
4276 if (type == GNUNET_BLOCK_TYPE_FS_DBLOCK)
4277 type = GNUNET_BLOCK_TYPE_ANY; /* to get on-demand as well */
4278 timeout = GNUNET_TIME_relative_multiply (BASIC_DATASTORE_REQUEST_DELAY,
4279 (pr->priority + 1));
4280 if (GNUNET_YES != pr->forward_only)
4283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4284 "Handing request for `%s' to datastore\n",
4285 GNUNET_h2s (&gm->query));
4287 pr->qe = GNUNET_DATASTORE_get (dsh,
4291 MAX_DATASTORE_QUEUE,
4293 &process_local_reply,
4297 GNUNET_STATISTICS_update (stats,
4298 gettext_noop ("# requests dropped by datastore (queue length limit)"),
4305 GNUNET_STATISTICS_update (stats,
4306 gettext_noop ("# requests forwarded due to high load"),
4311 /* Are multiple results possible (and did we look locally)? If so, start processing remotely now! */
4314 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
4315 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
4316 /* only one result, wait for datastore */
4317 if (GNUNET_YES != pr->forward_only)
4319 GNUNET_STATISTICS_update (stats,
4320 gettext_noop ("# requests not instantly forwarded (waiting for datastore)"),
4326 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
4327 pr->task = GNUNET_SCHEDULER_add_now (&forward_request_task,
4331 /* make sure we don't track too many requests */
4332 if (GNUNET_CONTAINER_heap_get_size (requests_by_expiration_heap) > max_pending_requests)
4334 pr = GNUNET_CONTAINER_heap_peek (requests_by_expiration_heap);
4335 GNUNET_assert (pr != NULL);
4336 destroy_pending_request (pr);
4342 /* **************************** CS GET Handling ************************ */
4346 * Handle START_SEARCH-message (search request from client).
4348 * @param cls closure
4349 * @param client identification of the client
4350 * @param message the actual message
4353 handle_start_search (void *cls,
4354 struct GNUNET_SERVER_Client *client,
4355 const struct GNUNET_MessageHeader *message)
4357 static GNUNET_HashCode all_zeros;
4358 const struct SearchMessage *sm;
4359 struct ClientList *cl;
4360 struct ClientRequestList *crl;
4361 struct PendingRequest *pr;
4364 enum GNUNET_BLOCK_Type type;
4366 msize = ntohs (message->size);
4367 if ( (msize < sizeof (struct SearchMessage)) ||
4368 (0 != (msize - sizeof (struct SearchMessage)) % sizeof (GNUNET_HashCode)) )
4371 GNUNET_SERVER_receive_done (client,
4375 GNUNET_STATISTICS_update (stats,
4376 gettext_noop ("# client searches received"),
4379 sc = (msize - sizeof (struct SearchMessage)) / sizeof (GNUNET_HashCode);
4380 sm = (const struct SearchMessage*) message;
4381 type = ntohl (sm->type);
4383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4384 "Received request for `%s' of type %u from local client\n",
4385 GNUNET_h2s (&sm->query),
4386 (unsigned int) type);
4389 while ( (cl != NULL) &&
4390 (cl->client != client) )
4394 cl = GNUNET_malloc (sizeof (struct ClientList));
4395 cl->client = client;
4396 GNUNET_SERVER_client_keep (client);
4397 cl->next = client_list;
4400 /* detect duplicate KBLOCK requests */
4401 if ( (type == GNUNET_BLOCK_TYPE_FS_KBLOCK) ||
4402 (type == GNUNET_BLOCK_TYPE_FS_NBLOCK) ||
4403 (type == GNUNET_BLOCK_TYPE_ANY) )
4406 while ( (crl != NULL) &&
4407 ( (0 != memcmp (&crl->req->query,
4409 sizeof (GNUNET_HashCode))) ||
4410 (crl->req->type != type) ) )
4415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4416 "Have existing request, merging content-seen lists.\n");
4419 /* Duplicate request (used to send long list of
4420 known/blocked results); merge 'pr->replies_seen'
4421 and update bloom filter */
4422 GNUNET_array_grow (pr->replies_seen,
4423 pr->replies_seen_size,
4424 pr->replies_seen_off + sc);
4425 memcpy (&pr->replies_seen[pr->replies_seen_off],
4427 sc * sizeof (GNUNET_HashCode));
4428 pr->replies_seen_off += sc;
4429 refresh_bloomfilter (pr);
4430 GNUNET_STATISTICS_update (stats,
4431 gettext_noop ("# client searches updated (merged content seen list)"),
4434 GNUNET_SERVER_receive_done (client,
4439 GNUNET_STATISTICS_update (stats,
4440 gettext_noop ("# client searches active"),
4443 pr = GNUNET_malloc (sizeof (struct PendingRequest) +
4444 ((type == GNUNET_BLOCK_TYPE_FS_SBLOCK) ? sizeof(GNUNET_HashCode) : 0));
4445 crl = GNUNET_malloc (sizeof (struct ClientRequestList));
4446 memset (crl, 0, sizeof (struct ClientRequestList));
4447 crl->client_list = cl;
4448 GNUNET_CONTAINER_DLL_insert (cl->rl_head,
4453 pr->client_request_list = crl;
4454 GNUNET_array_grow (pr->replies_seen,
4455 pr->replies_seen_size,
4457 memcpy (pr->replies_seen,
4459 sc * sizeof (GNUNET_HashCode));
4460 pr->replies_seen_off = sc;
4461 pr->anonymity_level = ntohl (sm->anonymity_level);
4462 pr->start_time = GNUNET_TIME_absolute_get ();
4463 refresh_bloomfilter (pr);
4464 pr->query = sm->query;
4465 if (0 == (1 & ntohl (sm->options)))
4466 pr->local_only = GNUNET_NO;
4468 pr->local_only = GNUNET_YES;
4471 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
4472 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
4473 if (0 != memcmp (&sm->target,
4475 sizeof (GNUNET_HashCode)))
4476 pr->target_pid = GNUNET_PEER_intern ((const struct GNUNET_PeerIdentity*) &sm->target);
4478 case GNUNET_BLOCK_TYPE_FS_SBLOCK:
4479 pr->namespace = (GNUNET_HashCode*) &pr[1];
4480 memcpy (&pr[1], &sm->target, sizeof (GNUNET_HashCode));
4485 GNUNET_break (GNUNET_OK ==
4486 GNUNET_CONTAINER_multihashmap_put (query_request_map,
4489 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
4490 if (type == GNUNET_BLOCK_TYPE_FS_DBLOCK)
4491 type = GNUNET_BLOCK_TYPE_ANY; /* get on-demand blocks too! */
4492 pr->qe = GNUNET_DATASTORE_get (dsh,
4496 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
4497 &process_local_reply,
4502 /* **************************** Startup ************************ */
4505 * Process fs requests.
4507 * @param server the initialized server
4508 * @param c configuration to use
4511 main_init (struct GNUNET_SERVER_Handle *server,
4512 const struct GNUNET_CONFIGURATION_Handle *c)
4514 static const struct GNUNET_CORE_MessageHandler p2p_handlers[] =
4517 GNUNET_MESSAGE_TYPE_FS_GET, 0 },
4519 GNUNET_MESSAGE_TYPE_FS_PUT, 0 },
4520 { &handle_p2p_migration_stop,
4521 GNUNET_MESSAGE_TYPE_FS_MIGRATION_STOP,
4522 sizeof (struct MigrationStopMessage) },
4525 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
4526 {&GNUNET_FS_handle_index_start, NULL,
4527 GNUNET_MESSAGE_TYPE_FS_INDEX_START, 0},
4528 {&GNUNET_FS_handle_index_list_get, NULL,
4529 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_GET, sizeof(struct GNUNET_MessageHeader) },
4530 {&GNUNET_FS_handle_unindex, NULL, GNUNET_MESSAGE_TYPE_FS_UNINDEX,
4531 sizeof (struct UnindexMessage) },
4532 {&handle_start_search, NULL, GNUNET_MESSAGE_TYPE_FS_START_SEARCH,
4536 unsigned long long enc = 128;
4539 stats = GNUNET_STATISTICS_create ("fs", cfg);
4540 min_migration_delay = GNUNET_TIME_UNIT_SECONDS;
4542 GNUNET_CONFIGURATION_get_value_number (cfg,
4544 "MAX_PENDING_REQUESTS",
4545 &max_pending_requests)) ||
4547 GNUNET_CONFIGURATION_get_value_number (cfg,
4549 "EXPECTED_NEIGHBOUR_COUNT",
4552 GNUNET_CONFIGURATION_get_value_time (cfg,
4554 "MIN_MIGRATION_DELAY",
4555 &min_migration_delay)) )
4557 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4558 _("Configuration fails to specify certain parameters, assuming default values."));
4560 connected_peers = GNUNET_CONTAINER_multihashmap_create (enc);
4561 query_request_map = GNUNET_CONTAINER_multihashmap_create (max_pending_requests);
4562 rt_entry_lifetime = GNUNET_LOAD_value_init (GNUNET_TIME_UNIT_FOREVER_REL);
4563 peer_request_map = GNUNET_CONTAINER_multihashmap_create (enc);
4564 requests_by_expiration_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
4565 core = GNUNET_CORE_connect (cfg,
4569 &peer_connect_handler,
4570 &peer_disconnect_handler,
4571 &peer_status_handler,
4577 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4578 _("Failed to connect to `%s' service.\n"),
4580 GNUNET_CONTAINER_multihashmap_destroy (connected_peers);
4581 connected_peers = NULL;
4582 GNUNET_CONTAINER_multihashmap_destroy (query_request_map);
4583 query_request_map = NULL;
4584 GNUNET_LOAD_value_free (rt_entry_lifetime);
4585 rt_entry_lifetime = NULL;
4586 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
4587 requests_by_expiration_heap = NULL;
4588 GNUNET_CONTAINER_multihashmap_destroy (peer_request_map);
4589 peer_request_map = NULL;
4592 GNUNET_DATASTORE_disconnect (dsh, GNUNET_NO);
4595 return GNUNET_SYSERR;
4597 if (active_from_migration)
4599 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4600 _("Content migration is enabled, will start to gather data\n"));
4601 consider_migration_gathering ();
4603 consider_dht_put_gathering (NULL);
4604 GNUNET_SERVER_disconnect_notify (server,
4605 &handle_client_disconnect,
4607 GNUNET_assert (GNUNET_OK ==
4608 GNUNET_CONFIGURATION_get_value_filename (cfg,
4612 GNUNET_DISK_directory_create (trustDirectory);
4613 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_HIGH,
4614 &cron_flush_trust, NULL);
4617 GNUNET_SERVER_add_handlers (server, handlers);
4618 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
4626 * Process fs requests.
4628 * @param cls closure
4629 * @param server the initialized server
4630 * @param cfg configuration to use
4634 struct GNUNET_SERVER_Handle *server,
4635 const struct GNUNET_CONFIGURATION_Handle *cfg)
4637 active_to_migration = GNUNET_CONFIGURATION_get_value_yesno (cfg,
4640 active_from_migration = GNUNET_CONFIGURATION_get_value_yesno (cfg,
4643 dsh = GNUNET_DATASTORE_connect (cfg);
4646 GNUNET_SCHEDULER_shutdown ();
4649 datastore_get_load = GNUNET_LOAD_value_init (DATASTORE_LOAD_AUTODECLINE);
4650 datastore_put_load = GNUNET_LOAD_value_init (DATASTORE_LOAD_AUTODECLINE);
4651 block_cfg = GNUNET_CONFIGURATION_create ();
4652 GNUNET_CONFIGURATION_set_value_string (block_cfg,
4656 block_ctx = GNUNET_BLOCK_context_create (block_cfg);
4657 GNUNET_assert (NULL != block_ctx);
4658 dht_handle = GNUNET_DHT_connect (cfg,
4660 if ( (GNUNET_OK != GNUNET_FS_indexing_init (cfg, dsh)) ||
4661 (GNUNET_OK != main_init (server, cfg)) )
4663 GNUNET_SCHEDULER_shutdown ();
4664 GNUNET_DATASTORE_disconnect (dsh, GNUNET_NO);
4666 GNUNET_DHT_disconnect (dht_handle);
4668 GNUNET_BLOCK_context_destroy (block_ctx);
4670 GNUNET_CONFIGURATION_destroy (block_cfg);
4672 GNUNET_LOAD_value_free (datastore_get_load);
4673 datastore_get_load = NULL;
4674 GNUNET_LOAD_value_free (datastore_put_load);
4675 datastore_put_load = NULL;
4682 * The main function for the fs service.
4684 * @param argc number of arguments from the command line
4685 * @param argv command line arguments
4686 * @return 0 ok, 1 on error
4689 main (int argc, char *const *argv)
4691 return (GNUNET_OK ==
4692 GNUNET_SERVICE_run (argc,
4695 GNUNET_SERVICE_OPTION_NONE,
4696 &run, NULL)) ? 0 : 1;
4699 /* end of gnunet-service-fs.c */