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 * - track per-peer request latency (using new load API)
28 * - consider more precise latency estimation (per-peer & request) -- again load API?
29 * - implement test_load_too_high, make decision priority-based, implement forwarding, etc.
30 * - introduce random latency in processing
35 #include "gnunet_constants.h"
36 #include "gnunet_core_service.h"
37 #include "gnunet_dht_service.h"
38 #include "gnunet_datastore_service.h"
39 #include "gnunet_load_lib.h"
40 #include "gnunet_peer_lib.h"
41 #include "gnunet_protocols.h"
42 #include "gnunet_signatures.h"
43 #include "gnunet_statistics_service.h"
44 #include "gnunet_util_lib.h"
45 #include "gnunet-service-fs_indexing.h"
48 #define DEBUG_FS GNUNET_NO
51 * Maximum number of outgoing messages we queue per peer.
53 #define MAX_QUEUE_PER_PEER 16
56 * Size for the hash map for DHT requests from the FS
57 * service. Should be about the number of concurrent
58 * DHT requests we plan to make.
60 #define FS_DHT_HT_SIZE 1024
63 * How often do we flush trust values to disk?
65 #define TRUST_FLUSH_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
68 * Inverse of the probability that we will submit the same query
69 * to the same peer again. If the same peer already got the query
70 * repeatedly recently, the probability is multiplied by the inverse
71 * of this number each time. Note that we only try about every TTL_DECREMENT/2
72 * plus MAX_CORK_DELAY (so roughly every 3.5s).
74 #define RETRY_PROBABILITY_INV 3
77 * What is the maximum delay for a P2P FS message (in our interaction
78 * with core)? FS-internal delays are another story. The value is
79 * chosen based on the 32k block size. Given that peers typcially
80 * have at least 1 kb/s bandwidth, 45s waits give us a chance to
81 * transmit one message even to the lowest-bandwidth peers.
83 #define MAX_TRANSMIT_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 45)
86 * Maximum number of requests (from other peers) that we're
87 * willing to have pending at any given point in time.
89 static unsigned long long max_pending_requests = (32 * 1024);
93 * Information we keep for each pending reply. The
94 * actual message follows at the end of this struct.
96 struct PendingMessage;
99 * Function called upon completion of a transmission.
102 * @param pid ID of receiving peer, 0 on transmission error
104 typedef void (*TransmissionContinuation)(void * cls,
105 GNUNET_PEER_Id tpid);
109 * Information we keep for each pending message (GET/PUT). The
110 * actual message follows at the end of this struct.
112 struct PendingMessage
115 * This is a doubly-linked list of messages to the same peer.
117 struct PendingMessage *next;
120 * This is a doubly-linked list of messages to the same peer.
122 struct PendingMessage *prev;
125 * Entry in pending message list for this pending message.
127 struct PendingMessageList *pml;
130 * Function to call immediately once we have transmitted this
133 TransmissionContinuation cont;
141 * Size of the reply; actual reply message follows
142 * at the end of this struct.
147 * How important is this message for us?
155 * Information about a peer that we are connected to.
156 * We track data that is useful for determining which
157 * peers should receive our requests. We also keep
158 * a list of messages to transmit to this peer.
164 * List of the last clients for which this peer successfully
167 struct GNUNET_SERVER_Client *last_client_replies[CS2P_SUCCESS_LIST_SIZE];
170 * List of the last PIDs for which
171 * this peer successfully answered a query;
172 * We use 0 to indicate no successful reply.
174 GNUNET_PEER_Id last_p2p_replies[P2P_SUCCESS_LIST_SIZE];
177 * Average delay between sending the peer a request and
178 * getting a reply (only calculated over the requests for
179 * which we actually got a reply). Calculated
180 * as a moving average: new_delay = ((n-1)*last_delay+curr_delay) / n
182 struct GNUNET_TIME_Relative avg_delay;
185 * Point in time until which this peer does not want us to migrate content
188 struct GNUNET_TIME_Absolute migration_blocked;
191 * Time until when we blocked this peer from migrating
194 struct GNUNET_TIME_Absolute last_migration_block;
197 * Handle for an active request for transmission to this
200 struct GNUNET_CORE_TransmitHandle *cth;
203 * Messages (replies, queries, content migration) we would like to
204 * send to this peer in the near future. Sorted by priority, head.
206 struct PendingMessage *pending_messages_head;
209 * Messages (replies, queries, content migration) we would like to
210 * send to this peer in the near future. Sorted by priority, tail.
212 struct PendingMessage *pending_messages_tail;
215 * Average priority of successful replies. Calculated
216 * as a moving average: new_avg = ((n-1)*last_avg+curr_prio) / n
221 * Increase in traffic preference still to be submitted
222 * to the core service for this peer.
224 uint64_t inc_preference;
227 * Trust rating for this peer
232 * Trust rating for this peer on disk.
237 * The peer's identity.
242 * Size of the linked list of 'pending_messages'.
244 unsigned int pending_requests;
247 * Which offset in "last_p2p_replies" will be updated next?
248 * (we go round-robin).
250 unsigned int last_p2p_replies_woff;
253 * Which offset in "last_client_replies" will be updated next?
254 * (we go round-robin).
256 unsigned int last_client_replies_woff;
262 * Information we keep for each pending request. We should try to
263 * keep this struct as small as possible since its memory consumption
264 * is key to how many requests we can have pending at once.
266 struct PendingRequest;
270 * Doubly-linked list of requests we are performing
271 * on behalf of the same client.
273 struct ClientRequestList
277 * This is a doubly-linked list.
279 struct ClientRequestList *next;
282 * This is a doubly-linked list.
284 struct ClientRequestList *prev;
287 * Request this entry represents.
289 struct PendingRequest *req;
292 * Client list this request belongs to.
294 struct ClientList *client_list;
300 * Replies to be transmitted to the client. The actual
301 * response message is allocated after this struct.
303 struct ClientResponseMessage
306 * This is a doubly-linked list.
308 struct ClientResponseMessage *next;
311 * This is a doubly-linked list.
313 struct ClientResponseMessage *prev;
316 * Client list entry this response belongs to.
318 struct ClientList *client_list;
321 * Number of bytes in the response.
328 * Linked list of clients we are performing requests
334 * This is a linked list.
336 struct ClientList *next;
339 * ID of a client making a request, NULL if this entry is for a
342 struct GNUNET_SERVER_Client *client;
345 * Head of list of requests performed on behalf
346 * of this client right now.
348 struct ClientRequestList *rl_head;
351 * Tail of list of requests performed on behalf
352 * of this client right now.
354 struct ClientRequestList *rl_tail;
357 * Head of linked list of responses.
359 struct ClientResponseMessage *res_head;
362 * Tail of linked list of responses.
364 struct ClientResponseMessage *res_tail;
367 * Context for sending replies.
369 struct GNUNET_CONNECTION_TransmitHandle *th;
375 * Doubly-linked list of messages we are performing
376 * due to a pending request.
378 struct PendingMessageList
382 * This is a doubly-linked list of messages on behalf of the same request.
384 struct PendingMessageList *next;
387 * This is a doubly-linked list of messages on behalf of the same request.
389 struct PendingMessageList *prev;
392 * Message this entry represents.
394 struct PendingMessage *pm;
397 * Request this entry belongs to.
399 struct PendingRequest *req;
402 * Peer this message is targeted for.
404 struct ConnectedPeer *target;
410 * Information we keep for each pending request. We should try to
411 * keep this struct as small as possible since its memory consumption
412 * is key to how many requests we can have pending at once.
414 struct PendingRequest
418 * If this request was made by a client, this is our entry in the
419 * client request list; otherwise NULL.
421 struct ClientRequestList *client_request_list;
424 * Entry of peer responsible for this entry (if this request
425 * was made by a peer).
427 struct ConnectedPeer *cp;
430 * If this is a namespace query, pointer to the hash of the public
431 * key of the namespace; otherwise NULL. Pointer will be to the
432 * end of this struct (so no need to free it).
434 const GNUNET_HashCode *namespace;
437 * Bloomfilter we use to filter out replies that we don't care about
438 * (anymore). NULL as long as we are interested in all replies.
440 struct GNUNET_CONTAINER_BloomFilter *bf;
443 * Context of our GNUNET_CORE_peer_change_preference call.
445 struct GNUNET_CORE_InformationRequestContext *irc;
448 * Hash code of all replies that we have seen so far (only valid
449 * if client is not NULL since we only track replies like this for
452 GNUNET_HashCode *replies_seen;
455 * Node in the heap representing this entry; NULL
456 * if we have no heap node.
458 struct GNUNET_CONTAINER_HeapNode *hnode;
461 * Head of list of messages being performed on behalf of this
464 struct PendingMessageList *pending_head;
467 * Tail of list of messages being performed on behalf of this
470 struct PendingMessageList *pending_tail;
473 * When did we first see this request (form this peer), or, if our
474 * client is initiating, when did we last initiate a search?
476 struct GNUNET_TIME_Absolute start_time;
479 * The query that this request is for.
481 GNUNET_HashCode query;
484 * The task responsible for transmitting queries
487 GNUNET_SCHEDULER_TaskIdentifier task;
490 * (Interned) Peer identifier that identifies a preferred target
493 GNUNET_PEER_Id target_pid;
496 * (Interned) Peer identifiers of peers that have already
497 * received our query for this content.
499 GNUNET_PEER_Id *used_pids;
502 * Our entry in the queue (non-NULL while we wait for our
503 * turn to interact with the local database).
505 struct GNUNET_DATASTORE_QueueEntry *qe;
508 * Size of the 'bf' (in bytes).
513 * Desired anonymity level; only valid for requests from a local client.
515 uint32_t anonymity_level;
518 * How many entries in "used_pids" are actually valid?
520 unsigned int used_pids_off;
523 * How long is the "used_pids" array?
525 unsigned int used_pids_size;
528 * Number of results found for this request.
530 unsigned int results_found;
533 * How many entries in "replies_seen" are actually valid?
535 unsigned int replies_seen_off;
538 * How long is the "replies_seen" array?
540 unsigned int replies_seen_size;
543 * Priority with which this request was made. If one of our clients
544 * made the request, then this is the current priority that we are
545 * using when initiating the request. This value is used when
546 * we decide to reward other peers with trust for providing a reply.
551 * Priority points left for us to spend when forwarding this request
554 uint32_t remaining_priority;
557 * Number to mingle hashes for bloom-filter tests with.
562 * TTL with which we saw this request (or, if we initiated, TTL that
563 * we used for the request).
568 * Type of the content that this request is for.
570 enum GNUNET_BLOCK_Type type;
573 * Remove this request after transmission of the current response.
578 * GNUNET_YES if we should not forward this request to other peers.
586 * Block that is ready for migration to other peers. Actual data is at the end of the block.
588 struct MigrationReadyBlock
592 * This is a doubly-linked list.
594 struct MigrationReadyBlock *next;
597 * This is a doubly-linked list.
599 struct MigrationReadyBlock *prev;
602 * Query for the block.
604 GNUNET_HashCode query;
607 * When does this block expire?
609 struct GNUNET_TIME_Absolute expiration;
612 * Peers we would consider forwarding this
613 * block to. Zero for empty entries.
615 GNUNET_PEER_Id target_list[MIGRATION_LIST_SIZE];
623 * Number of targets already used.
625 unsigned int used_targets;
630 enum GNUNET_BLOCK_Type type;
635 * Our connection to the datastore.
637 static struct GNUNET_DATASTORE_Handle *dsh;
642 static struct GNUNET_BLOCK_Context *block_ctx;
645 * Our block configuration.
647 static struct GNUNET_CONFIGURATION_Handle *block_cfg;
652 static struct GNUNET_SCHEDULER_Handle *sched;
657 static const struct GNUNET_CONFIGURATION_Handle *cfg;
660 * Map of peer identifiers to "struct ConnectedPeer" (for that peer).
662 static struct GNUNET_CONTAINER_MultiHashMap *connected_peers;
665 * Map of peer identifiers to "struct PendingRequest" (for that peer).
667 static struct GNUNET_CONTAINER_MultiHashMap *peer_request_map;
670 * Map of query identifiers to "struct PendingRequest" (for that query).
672 static struct GNUNET_CONTAINER_MultiHashMap *query_request_map;
675 * Heap with the request that will expire next at the top. Contains
676 * pointers of type "struct PendingRequest*"; these will *also* be
677 * aliased from the "requests_by_peer" data structures and the
678 * "requests_by_query" table. Note that requests from our clients
679 * don't expire and are thus NOT in the "requests_by_expiration"
680 * (or the "requests_by_peer" tables).
682 static struct GNUNET_CONTAINER_Heap *requests_by_expiration_heap;
685 * Handle for reporting statistics.
687 static struct GNUNET_STATISTICS_Handle *stats;
690 * Linked list of clients we are currently processing requests for.
692 static struct ClientList *client_list;
695 * Pointer to handle to the core service (points to NULL until we've
698 static struct GNUNET_CORE_Handle *core;
701 * Head of linked list of blocks that can be migrated.
703 static struct MigrationReadyBlock *mig_head;
706 * Tail of linked list of blocks that can be migrated.
708 static struct MigrationReadyBlock *mig_tail;
711 * Request to datastore for migration (or NULL).
713 static struct GNUNET_DATASTORE_QueueEntry *mig_qe;
716 * Where do we store trust information?
718 static char *trustDirectory;
721 * ID of task that collects blocks for migration.
723 static GNUNET_SCHEDULER_TaskIdentifier mig_task;
726 * What is the maximum frequency at which we are allowed to
727 * poll the datastore for migration content?
729 static struct GNUNET_TIME_Relative min_migration_delay;
732 * Handle for DHT operations.
734 static struct GNUNET_DHT_Handle *dht_handle;
737 * Size of the doubly-linked list of migration blocks.
739 static unsigned int mig_size;
742 * Are we allowed to migrate content to this peer.
744 static int active_migration;
747 * Typical priorities we're seeing from other peers right now. Since
748 * most priorities will be zero, this value is the weighted average of
749 * non-zero priorities seen "recently". In order to ensure that new
750 * values do not dramatically change the ratio, values are first
751 * "capped" to a reasonable range (+N of the current value) and then
752 * averaged into the existing value by a ratio of 1:N. Hence
753 * receiving the largest possible priority can still only raise our
754 * "current_priorities" by at most 1.
756 static double current_priorities;
759 * Datastore 'GET' load tracking.
761 static struct GNUNET_LOAD_Value *datastore_get_load;
764 * Datastore 'PUT' load tracking.
766 static struct GNUNET_LOAD_Value *datastore_put_load;
770 * We've just now completed a datastore request. Update our
771 * datastore load calculations.
773 * @param start time when the datastore request was issued
776 update_datastore_delays (struct GNUNET_TIME_Absolute start)
778 struct GNUNET_TIME_Relative delay;
780 delay = GNUNET_TIME_absolute_get_duration (start);
781 GNUNET_LOAD_update (datastore_get_load,
787 * Get the filename under which we would store the GNUNET_HELLO_Message
788 * for the given host and protocol.
789 * @return filename of the form DIRECTORY/HOSTID
792 get_trust_filename (const struct GNUNET_PeerIdentity *id)
794 struct GNUNET_CRYPTO_HashAsciiEncoded fil;
797 GNUNET_CRYPTO_hash_to_enc (&id->hashPubKey, &fil);
798 GNUNET_asprintf (&fn, "%s%s%s", trustDirectory, DIR_SEPARATOR_STR, &fil);
805 * Transmit messages by copying it to the target buffer
806 * "buf". "buf" will be NULL and "size" zero if the socket was closed
807 * for writing in the meantime. In that case, do nothing
808 * (the disconnect or shutdown handler will take care of the rest).
809 * If we were able to transmit messages and there are still more
810 * pending, ask core again for further calls to this function.
812 * @param cls closure, pointer to the 'struct ConnectedPeer*'
813 * @param size number of bytes available in buf
814 * @param buf where the callee should write the message
815 * @return number of bytes written to buf
818 transmit_to_peer (void *cls,
819 size_t size, void *buf);
822 /* ******************* clean up functions ************************ */
825 * Delete the given migration block.
827 * @param mb block to delete
830 delete_migration_block (struct MigrationReadyBlock *mb)
832 GNUNET_CONTAINER_DLL_remove (mig_head,
835 GNUNET_PEER_decrement_rcs (mb->target_list,
836 MIGRATION_LIST_SIZE);
843 * Compare the distance of two peers to a key.
846 * @param p1 first peer
847 * @param p2 second peer
848 * @return GNUNET_YES if P1 is closer to key than P2
851 is_closer (const GNUNET_HashCode *key,
852 const struct GNUNET_PeerIdentity *p1,
853 const struct GNUNET_PeerIdentity *p2)
855 return GNUNET_CRYPTO_hash_xorcmp (&p1->hashPubKey,
862 * Consider migrating content to a given peer.
864 * @param cls 'struct MigrationReadyBlock*' to select
865 * targets for (or NULL for none)
866 * @param key ID of the peer
867 * @param value 'struct ConnectedPeer' of the peer
868 * @return GNUNET_YES (always continue iteration)
871 consider_migration (void *cls,
872 const GNUNET_HashCode *key,
875 struct MigrationReadyBlock *mb = cls;
876 struct ConnectedPeer *cp = value;
877 struct MigrationReadyBlock *pos;
878 struct GNUNET_PeerIdentity cppid;
879 struct GNUNET_PeerIdentity otherpid;
880 struct GNUNET_PeerIdentity worstpid;
885 /* consider 'cp' as a migration target for mb */
886 if (GNUNET_TIME_absolute_get_remaining (cp->migration_blocked).value > 0)
887 return GNUNET_YES; /* peer has requested no migration! */
890 GNUNET_PEER_resolve (cp->pid,
892 repl = MIGRATION_LIST_SIZE;
893 for (i=0;i<MIGRATION_LIST_SIZE;i++)
895 if (mb->target_list[i] == 0)
897 mb->target_list[i] = cp->pid;
898 GNUNET_PEER_change_rc (mb->target_list[i], 1);
899 repl = MIGRATION_LIST_SIZE;
902 GNUNET_PEER_resolve (mb->target_list[i],
904 if ( (repl == MIGRATION_LIST_SIZE) &&
905 is_closer (&mb->query,
912 else if ( (repl != MIGRATION_LIST_SIZE) &&
913 (is_closer (&mb->query,
921 if (repl != MIGRATION_LIST_SIZE)
923 GNUNET_PEER_change_rc (mb->target_list[repl], -1);
924 mb->target_list[repl] = cp->pid;
925 GNUNET_PEER_change_rc (mb->target_list[repl], 1);
929 /* consider scheduling transmission to cp for content migration */
936 for (i=0;i<MIGRATION_LIST_SIZE;i++)
938 if (cp->pid == pos->target_list[i])
943 msize = GNUNET_MIN (msize,
951 return GNUNET_YES; /* no content available */
953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
954 "Trying to migrate at least %u bytes to peer `%s'\n",
959 = GNUNET_CORE_notify_transmit_ready (core,
960 0, GNUNET_TIME_UNIT_FOREVER_REL,
961 (const struct GNUNET_PeerIdentity*) key,
962 msize + sizeof (struct PutMessage),
970 * Task that is run periodically to obtain blocks for content
974 * @param tc scheduler context (also unused)
977 gather_migration_blocks (void *cls,
978 const struct GNUNET_SCHEDULER_TaskContext *tc);
982 * If the migration task is not currently running, consider
983 * (re)scheduling it with the appropriate delay.
986 consider_migration_gathering ()
988 struct GNUNET_TIME_Relative delay;
994 if (mig_task != GNUNET_SCHEDULER_NO_TASK)
996 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
998 delay = GNUNET_TIME_relative_divide (delay,
999 MAX_MIGRATION_QUEUE);
1000 delay = GNUNET_TIME_relative_max (delay,
1001 min_migration_delay);
1002 mig_task = GNUNET_SCHEDULER_add_delayed (sched,
1004 &gather_migration_blocks,
1010 * Process content offered for migration.
1012 * @param cls closure
1013 * @param key key for the content
1014 * @param size number of bytes in data
1015 * @param data content stored
1016 * @param type type of the content
1017 * @param priority priority of the content
1018 * @param anonymity anonymity-level for the content
1019 * @param expiration expiration time for the content
1020 * @param uid unique identifier for the datum;
1021 * maybe 0 if no unique identifier is available
1024 process_migration_content (void *cls,
1025 const GNUNET_HashCode * key,
1028 enum GNUNET_BLOCK_Type type,
1031 struct GNUNET_TIME_Absolute
1032 expiration, uint64_t uid)
1034 struct MigrationReadyBlock *mb;
1039 if (mig_size < MAX_MIGRATION_QUEUE)
1040 consider_migration_gathering ();
1043 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
1046 GNUNET_FS_handle_on_demand_block (key, size, data,
1047 type, priority, anonymity,
1049 &process_migration_content,
1052 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
1057 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1058 "Retrieved block `%s' of type %u for migration\n",
1062 mb = GNUNET_malloc (sizeof (struct MigrationReadyBlock) + size);
1064 mb->expiration = expiration;
1067 memcpy (&mb[1], data, size);
1068 GNUNET_CONTAINER_DLL_insert_after (mig_head,
1073 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
1074 &consider_migration,
1076 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
1081 * Task that is run periodically to obtain blocks for content
1085 * @param tc scheduler context (also unused)
1088 gather_migration_blocks (void *cls,
1089 const struct GNUNET_SCHEDULER_TaskContext *tc)
1091 mig_task = GNUNET_SCHEDULER_NO_TASK;
1094 mig_qe = GNUNET_DATASTORE_get_random (dsh, 0, -1,
1095 GNUNET_TIME_UNIT_FOREVER_REL,
1096 &process_migration_content, NULL);
1097 GNUNET_assert (mig_qe != NULL);
1103 * We're done with a particular message list entry.
1104 * Free all associated resources.
1106 * @param pml entry to destroy
1109 destroy_pending_message_list_entry (struct PendingMessageList *pml)
1111 GNUNET_CONTAINER_DLL_remove (pml->req->pending_head,
1112 pml->req->pending_tail,
1114 GNUNET_CONTAINER_DLL_remove (pml->target->pending_messages_head,
1115 pml->target->pending_messages_tail,
1117 pml->target->pending_requests--;
1118 GNUNET_free (pml->pm);
1124 * Destroy the given pending message (and call the respective
1127 * @param pm message to destroy
1128 * @param tpid id of peer that the message was delivered to, or 0 for none
1131 destroy_pending_message (struct PendingMessage *pm,
1132 GNUNET_PEER_Id tpid)
1134 struct PendingMessageList *pml = pm->pml;
1135 TransmissionContinuation cont;
1140 GNUNET_assert (pml->pm == pm);
1141 GNUNET_assert ( (tpid == 0) || (tpid == pml->target->pid) );
1143 cont_cls = pm->cont_cls;
1144 destroy_pending_message_list_entry (pml);
1151 cont (cont_cls, tpid);
1156 * We're done processing a particular request.
1157 * Free all associated resources.
1159 * @param pr request to destroy
1162 destroy_pending_request (struct PendingRequest *pr)
1164 struct GNUNET_PeerIdentity pid;
1166 if (pr->hnode != NULL)
1168 GNUNET_CONTAINER_heap_remove_node (requests_by_expiration_heap,
1172 if (NULL == pr->client_request_list)
1174 GNUNET_STATISTICS_update (stats,
1175 gettext_noop ("# P2P searches active"),
1181 GNUNET_STATISTICS_update (stats,
1182 gettext_noop ("# client searches active"),
1186 /* might have already been removed from map in 'process_reply' (if
1187 there was a unique reply) or never inserted if it was a
1188 duplicate; hence ignore the return value here */
1189 (void) GNUNET_CONTAINER_multihashmap_remove (query_request_map,
1194 GNUNET_DATASTORE_cancel (pr->qe);
1197 if (pr->client_request_list != NULL)
1199 GNUNET_CONTAINER_DLL_remove (pr->client_request_list->client_list->rl_head,
1200 pr->client_request_list->client_list->rl_tail,
1201 pr->client_request_list);
1202 GNUNET_free (pr->client_request_list);
1203 pr->client_request_list = NULL;
1207 GNUNET_PEER_resolve (pr->cp->pid,
1209 (void) GNUNET_CONTAINER_multihashmap_remove (peer_request_map,
1216 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
1219 if (pr->irc != NULL)
1221 GNUNET_CORE_peer_change_preference_cancel (pr->irc);
1224 if (pr->replies_seen != NULL)
1226 GNUNET_free (pr->replies_seen);
1227 pr->replies_seen = NULL;
1229 if (pr->task != GNUNET_SCHEDULER_NO_TASK)
1231 GNUNET_SCHEDULER_cancel (sched,
1233 pr->task = GNUNET_SCHEDULER_NO_TASK;
1235 while (NULL != pr->pending_head)
1236 destroy_pending_message_list_entry (pr->pending_head);
1237 GNUNET_PEER_change_rc (pr->target_pid, -1);
1238 if (pr->used_pids != NULL)
1240 GNUNET_PEER_decrement_rcs (pr->used_pids, pr->used_pids_off);
1241 GNUNET_free (pr->used_pids);
1242 pr->used_pids_off = 0;
1243 pr->used_pids_size = 0;
1244 pr->used_pids = NULL;
1251 * Method called whenever a given peer connects.
1253 * @param cls closure, not used
1254 * @param peer peer identity this notification is about
1255 * @param latency reported latency of the connection with 'other'
1256 * @param distance reported distance (DV) to 'other'
1259 peer_connect_handler (void *cls,
1261 GNUNET_PeerIdentity * peer,
1262 struct GNUNET_TIME_Relative latency,
1265 struct ConnectedPeer *cp;
1266 struct MigrationReadyBlock *pos;
1270 cp = GNUNET_malloc (sizeof (struct ConnectedPeer));
1271 cp->pid = GNUNET_PEER_intern (peer);
1273 fn = get_trust_filename (peer);
1274 if ((GNUNET_DISK_file_test (fn) == GNUNET_YES) &&
1275 (sizeof (trust) == GNUNET_DISK_fn_read (fn, &trust, sizeof (trust))))
1276 cp->disk_trust = cp->trust = ntohl (trust);
1279 GNUNET_break (GNUNET_OK ==
1280 GNUNET_CONTAINER_multihashmap_put (connected_peers,
1283 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1288 (void) consider_migration (pos, &peer->hashPubKey, cp);
1295 * Increase the host credit by a value.
1297 * @param host which peer to change the trust value on
1298 * @param value is the int value by which the
1299 * host credit is to be increased or decreased
1300 * @returns the actual change in trust (positive or negative)
1303 change_host_trust (struct ConnectedPeer *host, int value)
1305 unsigned int old_trust;
1309 GNUNET_assert (host != NULL);
1310 old_trust = host->trust;
1313 if (host->trust + value < host->trust)
1315 value = UINT32_MAX - host->trust;
1316 host->trust = UINT32_MAX;
1319 host->trust += value;
1323 if (host->trust < -value)
1325 value = -host->trust;
1329 host->trust += value;
1336 * Write host-trust information to a file - flush the buffer entry!
1339 flush_trust (void *cls,
1340 const GNUNET_HashCode *key,
1343 struct ConnectedPeer *host = value;
1346 struct GNUNET_PeerIdentity pid;
1348 if (host->trust == host->disk_trust)
1349 return GNUNET_OK; /* unchanged */
1350 GNUNET_PEER_resolve (host->pid,
1352 fn = get_trust_filename (&pid);
1353 if (host->trust == 0)
1355 if ((0 != UNLINK (fn)) && (errno != ENOENT))
1356 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
1357 GNUNET_ERROR_TYPE_BULK, "unlink", fn);
1361 trust = htonl (host->trust);
1362 if (sizeof(uint32_t) == GNUNET_DISK_fn_write (fn, &trust,
1364 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE
1365 | GNUNET_DISK_PERM_GROUP_READ | GNUNET_DISK_PERM_OTHER_READ))
1366 host->disk_trust = host->trust;
1373 * Call this method periodically to scan data/hosts for new hosts.
1376 cron_flush_trust (void *cls,
1377 const struct GNUNET_SCHEDULER_TaskContext *tc)
1380 if (NULL == connected_peers)
1382 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
1387 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1389 GNUNET_SCHEDULER_add_delayed (tc->sched,
1390 TRUST_FLUSH_FREQ, &cron_flush_trust, NULL);
1395 * Free (each) request made by the peer.
1397 * @param cls closure, points to peer that the request belongs to
1398 * @param key current key code
1399 * @param value value in the hash map
1400 * @return GNUNET_YES (we should continue to iterate)
1403 destroy_request (void *cls,
1404 const GNUNET_HashCode * key,
1407 const struct GNUNET_PeerIdentity * peer = cls;
1408 struct PendingRequest *pr = value;
1410 GNUNET_break (GNUNET_YES ==
1411 GNUNET_CONTAINER_multihashmap_remove (peer_request_map,
1414 destroy_pending_request (pr);
1420 * Method called whenever a peer disconnects.
1422 * @param cls closure, not used
1423 * @param peer peer identity this notification is about
1426 peer_disconnect_handler (void *cls,
1428 GNUNET_PeerIdentity * peer)
1430 struct ConnectedPeer *cp;
1431 struct PendingMessage *pm;
1433 struct MigrationReadyBlock *pos;
1434 struct MigrationReadyBlock *next;
1436 GNUNET_CONTAINER_multihashmap_get_multiple (peer_request_map,
1440 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
1444 for (i=0;i<CS2P_SUCCESS_LIST_SIZE;i++)
1446 if (NULL != cp->last_client_replies[i])
1448 GNUNET_SERVER_client_drop (cp->last_client_replies[i]);
1449 cp->last_client_replies[i] = NULL;
1452 GNUNET_break (GNUNET_YES ==
1453 GNUNET_CONTAINER_multihashmap_remove (connected_peers,
1456 /* remove this peer from migration considerations; schedule
1459 while (NULL != (pos = next))
1462 for (i=0;i<MIGRATION_LIST_SIZE;i++)
1464 if (pos->target_list[i] == cp->pid)
1466 GNUNET_PEER_change_rc (pos->target_list[i], -1);
1467 pos->target_list[i] = 0;
1470 if (pos->used_targets >= GNUNET_CONTAINER_multihashmap_size (connected_peers))
1472 delete_migration_block (pos);
1473 consider_migration_gathering ();
1476 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
1477 &consider_migration,
1480 GNUNET_PEER_change_rc (cp->pid, -1);
1481 GNUNET_PEER_decrement_rcs (cp->last_p2p_replies, P2P_SUCCESS_LIST_SIZE);
1482 if (NULL != cp->cth)
1483 GNUNET_CORE_notify_transmit_ready_cancel (cp->cth);
1484 while (NULL != (pm = cp->pending_messages_head))
1485 destroy_pending_message (pm, 0 /* delivery failed */);
1486 GNUNET_break (0 == cp->pending_requests);
1492 * Iterator over hash map entries that removes all occurences
1493 * of the given 'client' from the 'last_client_replies' of the
1494 * given connected peer.
1496 * @param cls closure, the 'struct GNUNET_SERVER_Client*' to remove
1497 * @param key current key code (unused)
1498 * @param value value in the hash map (the 'struct ConnectedPeer*' to change)
1499 * @return GNUNET_YES (we should continue to iterate)
1502 remove_client_from_last_client_replies (void *cls,
1503 const GNUNET_HashCode * key,
1506 struct GNUNET_SERVER_Client *client = cls;
1507 struct ConnectedPeer *cp = value;
1510 for (i=0;i<CS2P_SUCCESS_LIST_SIZE;i++)
1512 if (cp->last_client_replies[i] == client)
1514 GNUNET_SERVER_client_drop (cp->last_client_replies[i]);
1515 cp->last_client_replies[i] = NULL;
1523 * A client disconnected. Remove all of its pending queries.
1525 * @param cls closure, NULL
1526 * @param client identification of the client
1529 handle_client_disconnect (void *cls,
1530 struct GNUNET_SERVER_Client
1533 struct ClientList *pos;
1534 struct ClientList *prev;
1535 struct ClientRequestList *rcl;
1536 struct ClientResponseMessage *creply;
1542 while ( (NULL != pos) &&
1543 (pos->client != client) )
1549 return; /* no requests pending for this client */
1550 while (NULL != (rcl = pos->rl_head))
1552 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1553 "Destroying pending request `%s' on disconnect\n",
1554 GNUNET_h2s (&rcl->req->query));
1555 destroy_pending_request (rcl->req);
1558 client_list = pos->next;
1560 prev->next = pos->next;
1561 if (pos->th != NULL)
1563 GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th);
1566 while (NULL != (creply = pos->res_head))
1568 GNUNET_CONTAINER_DLL_remove (pos->res_head,
1571 GNUNET_free (creply);
1573 GNUNET_SERVER_client_drop (pos->client);
1575 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
1576 &remove_client_from_last_client_replies,
1582 * Iterator to free peer entries.
1584 * @param cls closure, unused
1585 * @param key current key code
1586 * @param value value in the hash map (peer entry)
1587 * @return GNUNET_YES (we should continue to iterate)
1590 clean_peer (void *cls,
1591 const GNUNET_HashCode * key,
1594 peer_disconnect_handler (NULL, (const struct GNUNET_PeerIdentity*) key);
1600 * Task run during shutdown.
1606 shutdown_task (void *cls,
1607 const struct GNUNET_SCHEDULER_TaskContext *tc)
1611 GNUNET_DATASTORE_cancel (mig_qe);
1614 if (GNUNET_SCHEDULER_NO_TASK != mig_task)
1616 GNUNET_SCHEDULER_cancel (sched, mig_task);
1617 mig_task = GNUNET_SCHEDULER_NO_TASK;
1619 while (client_list != NULL)
1620 handle_client_disconnect (NULL,
1621 client_list->client);
1622 cron_flush_trust (NULL, NULL);
1623 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
1626 GNUNET_break (0 == GNUNET_CONTAINER_heap_get_size (requests_by_expiration_heap));
1627 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
1628 requests_by_expiration_heap = 0;
1629 GNUNET_CONTAINER_multihashmap_destroy (connected_peers);
1630 connected_peers = NULL;
1631 GNUNET_break (0 == GNUNET_CONTAINER_multihashmap_size (query_request_map));
1632 GNUNET_CONTAINER_multihashmap_destroy (query_request_map);
1633 query_request_map = NULL;
1634 GNUNET_break (0 == GNUNET_CONTAINER_multihashmap_size (peer_request_map));
1635 GNUNET_CONTAINER_multihashmap_destroy (peer_request_map);
1636 peer_request_map = NULL;
1637 GNUNET_assert (NULL != core);
1638 GNUNET_CORE_disconnect (core);
1642 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1647 GNUNET_DATASTORE_disconnect (dsh,
1651 while (mig_head != NULL)
1652 delete_migration_block (mig_head);
1653 GNUNET_assert (0 == mig_size);
1654 GNUNET_DHT_disconnect (dht_handle);
1656 GNUNET_LOAD_value_free (datastore_get_load);
1657 datastore_get_load = NULL;
1658 GNUNET_LOAD_value_free (datastore_put_load);
1659 datastore_put_load = NULL;
1660 GNUNET_BLOCK_context_destroy (block_ctx);
1662 GNUNET_CONFIGURATION_destroy (block_cfg);
1666 GNUNET_free_non_null (trustDirectory);
1667 trustDirectory = NULL;
1671 /* ******************* Utility functions ******************** */
1675 * Transmit messages by copying it to the target buffer
1676 * "buf". "buf" will be NULL and "size" zero if the socket was closed
1677 * for writing in the meantime. In that case, do nothing
1678 * (the disconnect or shutdown handler will take care of the rest).
1679 * If we were able to transmit messages and there are still more
1680 * pending, ask core again for further calls to this function.
1682 * @param cls closure, pointer to the 'struct ConnectedPeer*'
1683 * @param size number of bytes available in buf
1684 * @param buf where the callee should write the message
1685 * @return number of bytes written to buf
1688 transmit_to_peer (void *cls,
1689 size_t size, void *buf)
1691 struct ConnectedPeer *cp = cls;
1693 struct GNUNET_PeerIdentity pid;
1694 struct PendingMessage *pm;
1695 struct MigrationReadyBlock *mb;
1696 struct MigrationReadyBlock *next;
1697 struct PutMessage migm;
1705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1706 "Dropping message, core too busy.\n");
1711 while ( (NULL != (pm = cp->pending_messages_head) ) &&
1712 (pm->msize <= size) )
1714 memcpy (&cbuf[msize], &pm[1], pm->msize);
1717 destroy_pending_message (pm, cp->pid);
1721 GNUNET_PEER_resolve (cp->pid,
1723 cp->cth = GNUNET_CORE_notify_transmit_ready (core,
1725 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1734 while (NULL != (mb = next))
1737 for (i=0;i<MIGRATION_LIST_SIZE;i++)
1739 if ( (cp->pid == mb->target_list[i]) &&
1740 (mb->size + sizeof (migm) <= size) )
1742 GNUNET_PEER_change_rc (mb->target_list[i], -1);
1743 mb->target_list[i] = 0;
1745 memset (&migm, 0, sizeof (migm));
1746 migm.header.size = htons (sizeof (migm) + mb->size);
1747 migm.header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
1748 migm.type = htonl (mb->type);
1749 migm.expiration = GNUNET_TIME_absolute_hton (mb->expiration);
1750 memcpy (&cbuf[msize], &migm, sizeof (migm));
1751 msize += sizeof (migm);
1752 size -= sizeof (migm);
1753 memcpy (&cbuf[msize], &mb[1], mb->size);
1757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1758 "Pushing migration block `%s' (%u bytes) to `%s'\n",
1759 GNUNET_h2s (&mb->query),
1768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1769 "Migration block `%s' (%u bytes) is not on migration list for peer `%s'\n",
1770 GNUNET_h2s (&mb->query),
1776 if ( (mb->used_targets >= MIGRATION_TARGET_COUNT) ||
1777 (mb->used_targets >= GNUNET_CONTAINER_multihashmap_size (connected_peers)) )
1779 delete_migration_block (mb);
1780 consider_migration_gathering ();
1783 consider_migration (NULL,
1788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1789 "Transmitting %u bytes to peer %u\n",
1798 * Add a message to the set of pending messages for the given peer.
1800 * @param cp peer to send message to
1801 * @param pm message to queue
1802 * @param pr request on which behalf this message is being queued
1805 add_to_pending_messages_for_peer (struct ConnectedPeer *cp,
1806 struct PendingMessage *pm,
1807 struct PendingRequest *pr)
1809 struct PendingMessage *pos;
1810 struct PendingMessageList *pml;
1811 struct GNUNET_PeerIdentity pid;
1813 GNUNET_assert (pm->next == NULL);
1814 GNUNET_assert (pm->pml == NULL);
1817 pml = GNUNET_malloc (sizeof (struct PendingMessageList));
1822 GNUNET_CONTAINER_DLL_insert (pr->pending_head,
1826 pos = cp->pending_messages_head;
1827 while ( (pos != NULL) &&
1828 (pm->priority < pos->priority) )
1830 GNUNET_CONTAINER_DLL_insert_after (cp->pending_messages_head,
1831 cp->pending_messages_tail,
1834 cp->pending_requests++;
1835 if (cp->pending_requests > MAX_QUEUE_PER_PEER)
1836 destroy_pending_message (cp->pending_messages_tail, 0);
1837 GNUNET_PEER_resolve (cp->pid, &pid);
1838 if (NULL != cp->cth)
1839 GNUNET_CORE_notify_transmit_ready_cancel (cp->cth);
1840 /* need to schedule transmission */
1841 cp->cth = GNUNET_CORE_notify_transmit_ready (core,
1842 cp->pending_messages_head->priority,
1845 cp->pending_messages_head->msize,
1848 if (cp->cth == NULL)
1851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1852 "Failed to schedule transmission with core!\n");
1854 GNUNET_STATISTICS_update (stats,
1855 gettext_noop ("# CORE transmission failures"),
1863 * Test if the load on this peer is too high
1864 * to even consider processing the query at
1867 * @return GNUNET_YES if the load is too high to do anything, GNUNET_NO to forward (load high, but not too high), GNUNET_SYSERR to indirect (load low)
1870 test_load_too_high ()
1872 return GNUNET_SYSERR; // FIXME
1876 /* ******************* Pending Request Refresh Task ******************** */
1881 * We use a random delay to make the timing of requests less
1882 * predictable. This function returns such a random delay. We add a base
1883 * delay of MAX_CORK_DELAY (1s).
1885 * FIXME: make schedule dependent on the specifics of the request?
1886 * Or bandwidth and number of connected peers and load?
1888 * @return random delay to use for some request, between 1s and 1000+TTL_DECREMENT ms
1890 static struct GNUNET_TIME_Relative
1891 get_processing_delay ()
1894 GNUNET_TIME_relative_add (GNUNET_CONSTANTS_MAX_CORK_DELAY,
1895 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1896 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1902 * We're processing a GET request from another peer and have decided
1903 * to forward it to other peers. This function is called periodically
1904 * and should forward the request to other peers until we have all
1905 * possible replies. If we have transmitted the *only* reply to
1906 * the initiator we should destroy the pending request. If we have
1907 * many replies in the queue to the initiator, we should delay sending
1908 * out more queries until the reply queue has shrunk some.
1910 * @param cls our "struct ProcessGetContext *"
1914 forward_request_task (void *cls,
1915 const struct GNUNET_SCHEDULER_TaskContext *tc);
1919 * Function called after we either failed or succeeded
1920 * at transmitting a query to a peer.
1922 * @param cls the requests "struct PendingRequest*"
1923 * @param tpid ID of receiving peer, 0 on transmission error
1926 transmit_query_continuation (void *cls,
1927 GNUNET_PEER_Id tpid)
1929 struct PendingRequest *pr = cls;
1931 GNUNET_STATISTICS_update (stats,
1932 gettext_noop ("# queries scheduled for forwarding"),
1938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1939 "Transmission of request failed, will try again later.\n");
1941 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
1942 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
1943 get_processing_delay (),
1944 &forward_request_task,
1948 GNUNET_STATISTICS_update (stats,
1949 gettext_noop ("# queries forwarded"),
1952 GNUNET_PEER_change_rc (tpid, 1);
1953 if (pr->used_pids_off == pr->used_pids_size)
1954 GNUNET_array_grow (pr->used_pids,
1956 pr->used_pids_size * 2 + 2);
1957 pr->used_pids[pr->used_pids_off++] = tpid;
1958 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
1959 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
1960 get_processing_delay (),
1961 &forward_request_task,
1967 * How many bytes should a bloomfilter be if we have already seen
1968 * entry_count responses? Note that BLOOMFILTER_K gives us the number
1969 * of bits set per entry. Furthermore, we should not re-size the
1970 * filter too often (to keep it cheap).
1972 * Since other peers will also add entries but not resize the filter,
1973 * we should generally pick a slightly larger size than what the
1974 * strict math would suggest.
1976 * @return must be a power of two and smaller or equal to 2^15.
1979 compute_bloomfilter_size (unsigned int entry_count)
1982 unsigned int ideal = (entry_count * BLOOMFILTER_K) / 4;
1983 uint16_t max = 1 << 15;
1985 if (entry_count > max)
1988 while ((size < max) && (size < ideal))
1997 * Recalculate our bloom filter for filtering replies. This function
1998 * will create a new bloom filter from scratch, so it should only be
1999 * called if we have no bloomfilter at all (and hence can create a
2000 * fresh one of minimal size without problems) OR if our peer is the
2001 * initiator (in which case we may resize to larger than mimimum size).
2003 * @param pr request for which the BF is to be recomputed
2006 refresh_bloomfilter (struct PendingRequest *pr)
2010 GNUNET_HashCode mhash;
2012 nsize = compute_bloomfilter_size (pr->replies_seen_off);
2013 if (nsize == pr->bf_size)
2014 return; /* size not changed */
2016 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
2017 pr->bf_size = nsize;
2018 pr->mingle = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, -1);
2019 pr->bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
2022 for (i=0;i<pr->replies_seen_off;i++)
2024 GNUNET_BLOCK_mingle_hash (&pr->replies_seen[i],
2027 GNUNET_CONTAINER_bloomfilter_add (pr->bf, &mhash);
2033 * Function called after we've tried to reserve a certain amount of
2034 * bandwidth for a reply. Check if we succeeded and if so send our
2037 * @param cls the requests "struct PendingRequest*"
2038 * @param peer identifies the peer
2039 * @param bpm_in set to the current bandwidth limit (receiving) for this peer
2040 * @param bpm_out set to the current bandwidth limit (sending) for this peer
2041 * @param amount set to the amount that was actually reserved or unreserved
2042 * @param preference current traffic preference for the given peer
2045 target_reservation_cb (void *cls,
2047 GNUNET_PeerIdentity * peer,
2048 struct GNUNET_BANDWIDTH_Value32NBO bpm_in,
2049 struct GNUNET_BANDWIDTH_Value32NBO bpm_out,
2051 uint64_t preference)
2053 struct PendingRequest *pr = cls;
2054 struct ConnectedPeer *cp;
2055 struct PendingMessage *pm;
2056 struct GetMessage *gm;
2057 GNUNET_HashCode *ext;
2067 /* error in communication with core, try again later */
2068 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
2069 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
2070 get_processing_delay (),
2071 &forward_request_task,
2075 // (3) transmit, update ttl/priority
2076 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
2080 /* Peer must have just left */
2082 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2083 "Selected peer disconnected!\n");
2085 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
2086 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
2087 get_processing_delay (),
2088 &forward_request_task,
2092 no_route = GNUNET_NO;
2098 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2099 "Failed to reserve bandwidth for reply (got %d/%u bytes only)!\n",
2103 GNUNET_STATISTICS_update (stats,
2104 gettext_noop ("# reply bandwidth reservation requests failed"),
2107 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
2108 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
2109 get_processing_delay (),
2110 &forward_request_task,
2112 return; /* this target round failed */
2114 /* FIXME: if we are "quite" busy, we may still want to skip
2115 this round; need more load detection code! */
2116 no_route = GNUNET_YES;
2119 GNUNET_STATISTICS_update (stats,
2120 gettext_noop ("# queries scheduled for forwarding"),
2123 /* build message and insert message into priority queue */
2125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2126 "Forwarding request `%s' to `%4s'!\n",
2127 GNUNET_h2s (&pr->query),
2132 if (GNUNET_YES == no_route)
2134 bm |= GET_MESSAGE_BIT_RETURN_TO;
2137 if (pr->namespace != NULL)
2139 bm |= GET_MESSAGE_BIT_SKS_NAMESPACE;
2142 if (pr->target_pid != 0)
2144 bm |= GET_MESSAGE_BIT_TRANSMIT_TO;
2147 msize = sizeof (struct GetMessage) + pr->bf_size + k * sizeof(GNUNET_HashCode);
2148 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
2149 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
2151 gm = (struct GetMessage*) &pm[1];
2152 gm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_GET);
2153 gm->header.size = htons (msize);
2154 gm->type = htonl (pr->type);
2155 pr->remaining_priority /= 2;
2156 gm->priority = htonl (pr->remaining_priority);
2157 gm->ttl = htonl (pr->ttl);
2158 gm->filter_mutator = htonl(pr->mingle);
2159 gm->hash_bitmap = htonl (bm);
2160 gm->query = pr->query;
2161 ext = (GNUNET_HashCode*) &gm[1];
2163 if (GNUNET_YES == no_route)
2164 GNUNET_PEER_resolve (pr->cp->pid, (struct GNUNET_PeerIdentity*) &ext[k++]);
2165 if (pr->namespace != NULL)
2166 memcpy (&ext[k++], pr->namespace, sizeof (GNUNET_HashCode));
2167 if (pr->target_pid != 0)
2168 GNUNET_PEER_resolve (pr->target_pid, (struct GNUNET_PeerIdentity*) &ext[k++]);
2169 bfdata = (char *) &ext[k];
2171 GNUNET_CONTAINER_bloomfilter_get_raw_data (pr->bf,
2174 pm->cont = &transmit_query_continuation;
2176 add_to_pending_messages_for_peer (cp, pm, pr);
2181 * Closure used for "target_peer_select_cb".
2183 struct PeerSelectionContext
2186 * The request for which we are selecting
2189 struct PendingRequest *pr;
2192 * Current "prime" target.
2194 struct GNUNET_PeerIdentity target;
2197 * How much do we like this target?
2199 double target_score;
2205 * Function called for each connected peer to determine
2206 * which one(s) would make good targets for forwarding.
2208 * @param cls closure (struct PeerSelectionContext)
2209 * @param key current key code (peer identity)
2210 * @param value value in the hash map (struct ConnectedPeer)
2211 * @return GNUNET_YES if we should continue to
2216 target_peer_select_cb (void *cls,
2217 const GNUNET_HashCode * key,
2220 struct PeerSelectionContext *psc = cls;
2221 struct ConnectedPeer *cp = value;
2222 struct PendingRequest *pr = psc->pr;
2227 /* 1) check that this peer is not the initiator */
2231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2232 "Skipping initiator in forwarding selection\n");
2234 return GNUNET_YES; /* skip */
2237 /* 2) check if we have already (recently) forwarded to this peer */
2239 for (i=0;i<pr->used_pids_off;i++)
2240 if (pr->used_pids[i] == cp->pid)
2243 if (0 != GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2244 RETRY_PROBABILITY_INV))
2247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2248 "NOT re-trying query that was previously transmitted %u times\n",
2249 (unsigned int) pr->used_pids_off);
2251 return GNUNET_YES; /* skip */
2256 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2257 "Re-trying query that was previously transmitted %u times to this peer\n",
2260 /* 3) calculate how much we'd like to forward to this peer,
2261 starting with a random value that is strong enough
2262 to at least give any peer a chance sometimes
2263 (compared to the other factors that come later) */
2264 /* 3a) count successful (recent) routes from cp for same source */
2267 score = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2268 P2P_SUCCESS_LIST_SIZE);
2269 for (i=0;i<P2P_SUCCESS_LIST_SIZE;i++)
2270 if (cp->last_p2p_replies[i] == pr->cp->pid)
2271 score += 1; /* likely successful based on hot path */
2275 score = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2276 CS2P_SUCCESS_LIST_SIZE);
2277 for (i=0;i<CS2P_SUCCESS_LIST_SIZE;i++)
2278 if (cp->last_client_replies[i] == pr->client_request_list->client_list->client)
2279 score += 1; /* likely successful based on hot path */
2281 /* 3b) include latency */
2282 if (cp->avg_delay.value < 4 * TTL_DECREMENT)
2283 score += 1; /* likely fast based on latency */
2284 /* 3c) include priorities */
2285 if (cp->avg_priority <= pr->remaining_priority / 2.0)
2286 score += 1; /* likely successful based on priorities */
2287 /* 3d) penalize for queue size */
2288 score -= (2.0 * cp->pending_requests / (double) MAX_QUEUE_PER_PEER);
2289 /* 3e) include peer proximity */
2290 score -= (2.0 * (GNUNET_CRYPTO_hash_distance_u32 (key,
2291 &pr->query)) / (double) UINT32_MAX);
2292 /* 4) super-bonus for being the known target */
2293 if (pr->target_pid == cp->pid)
2295 /* store best-fit in closure */
2297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2298 "Peer `%s' gets score %f for forwarding query, max is %f\n",
2303 score++; /* avoid zero */
2304 if (score > psc->target_score)
2306 psc->target_score = score;
2307 psc->target.hashPubKey = *key;
2314 * The priority level imposes a bound on the maximum
2315 * value for the ttl that can be requested.
2317 * @param ttl_in requested ttl
2318 * @param prio given priority
2319 * @return ttl_in if ttl_in is below the limit,
2320 * otherwise the ttl-limit for the given priority
2323 bound_ttl (int32_t ttl_in, uint32_t prio)
2325 unsigned long long allowed;
2329 allowed = ((unsigned long long) prio) * TTL_DECREMENT / 1000;
2330 if (ttl_in > allowed)
2332 if (allowed >= (1 << 30))
2341 * We're processing a GET request and have decided
2342 * to forward it to other peers. This function is called periodically
2343 * and should forward the request to other peers until we have all
2344 * possible replies. If we have transmitted the *only* reply to
2345 * the initiator we should destroy the pending request. If we have
2346 * many replies in the queue to the initiator, we should delay sending
2347 * out more queries until the reply queue has shrunk some.
2349 * @param cls our "struct ProcessGetContext *"
2353 forward_request_task (void *cls,
2354 const struct GNUNET_SCHEDULER_TaskContext *tc)
2356 struct PendingRequest *pr = cls;
2357 struct PeerSelectionContext psc;
2358 struct ConnectedPeer *cp;
2359 struct GNUNET_TIME_Relative delay;
2361 pr->task = GNUNET_SCHEDULER_NO_TASK;
2362 if (pr->irc != NULL)
2365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2366 "Forwarding of query `%s' not attempted due to pending local lookup!\n",
2367 GNUNET_h2s (&pr->query));
2369 return; /* already pending */
2371 if (GNUNET_YES == pr->local_only)
2372 return; /* configured to not do P2P search */
2374 if (0 == pr->anonymity_level)
2377 /* DHT API needs fixing... */
2378 pr->dht_get = GNUNET_DHT_get_start (dht_handle,
2379 GNUNET_TIME_UNIT_FOREVER_REL,
2388 /* (1) select target */
2390 psc.target_score = -DBL_MAX;
2391 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
2392 &target_peer_select_cb,
2394 if (psc.target_score == -DBL_MAX)
2396 delay = get_processing_delay ();
2398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2399 "No peer selected for forwarding of query `%s', will try again in %llu ms!\n",
2400 GNUNET_h2s (&pr->query),
2403 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
2405 &forward_request_task,
2407 return; /* nobody selected */
2409 /* (3) update TTL/priority */
2410 if (pr->client_request_list != NULL)
2412 /* FIXME: use better algorithm!? */
2413 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2416 /* bound priority we use by priorities we see from other peers
2417 rounded up (must round up so that we can see non-zero
2418 priorities, but round up as little as possible to make it
2419 plausible that we forwarded another peers request) */
2420 if (pr->priority > current_priorities + 1.0)
2421 pr->priority = (uint32_t) current_priorities + 1.0;
2422 pr->ttl = bound_ttl (pr->ttl + TTL_DECREMENT * 2,
2425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2426 "Trying query `%s' with priority %u and TTL %d.\n",
2427 GNUNET_h2s (&pr->query),
2433 /* (3) reserve reply bandwidth */
2434 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
2435 &psc.target.hashPubKey);
2436 GNUNET_assert (NULL != cp);
2437 pr->irc = GNUNET_CORE_peer_change_preference (sched, cfg,
2439 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
2440 GNUNET_BANDWIDTH_value_init (UINT32_MAX),
2443 &target_reservation_cb,
2445 cp->inc_preference = 0;
2449 /* **************************** P2P PUT Handling ************************ */
2453 * Function called after we either failed or succeeded
2454 * at transmitting a reply to a peer.
2456 * @param cls the requests "struct PendingRequest*"
2457 * @param tpid ID of receiving peer, 0 on transmission error
2460 transmit_reply_continuation (void *cls,
2461 GNUNET_PEER_Id tpid)
2463 struct PendingRequest *pr = cls;
2467 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
2468 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
2469 /* only one reply expected, done with the request! */
2470 destroy_pending_request (pr);
2472 case GNUNET_BLOCK_TYPE_ANY:
2473 case GNUNET_BLOCK_TYPE_FS_KBLOCK:
2474 case GNUNET_BLOCK_TYPE_FS_SBLOCK:
2484 * Transmit the given message by copying it to the target buffer
2485 * "buf". "buf" will be NULL and "size" zero if the socket was closed
2486 * for writing in the meantime. In that case, do nothing
2487 * (the disconnect or shutdown handler will take care of the rest).
2488 * If we were able to transmit messages and there are still more
2489 * pending, ask core again for further calls to this function.
2491 * @param cls closure, pointer to the 'struct ClientList*'
2492 * @param size number of bytes available in buf
2493 * @param buf where the callee should write the message
2494 * @return number of bytes written to buf
2497 transmit_to_client (void *cls,
2498 size_t size, void *buf)
2500 struct ClientList *cl = cls;
2502 struct ClientResponseMessage *creply;
2509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2510 "Not sending reply, client communication problem.\n");
2515 while ( (NULL != (creply = cl->res_head) ) &&
2516 (creply->msize <= size) )
2518 memcpy (&cbuf[msize], &creply[1], creply->msize);
2519 msize += creply->msize;
2520 size -= creply->msize;
2521 GNUNET_CONTAINER_DLL_remove (cl->res_head,
2524 GNUNET_free (creply);
2527 cl->th = GNUNET_SERVER_notify_transmit_ready (cl->client,
2529 GNUNET_TIME_UNIT_FOREVER_REL,
2530 &transmit_to_client,
2533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2534 "Transmitted %u bytes to client\n",
2535 (unsigned int) msize);
2542 * Closure for "process_reply" function.
2544 struct ProcessReplyClosure
2547 * The data for the reply.
2552 * Who gave us this reply? NULL for local host.
2554 struct ConnectedPeer *sender;
2557 * When the reply expires.
2559 struct GNUNET_TIME_Absolute expiration;
2567 * Type of the block.
2569 enum GNUNET_BLOCK_Type type;
2572 * How much was this reply worth to us?
2577 * Evaluation result (returned).
2579 enum GNUNET_BLOCK_EvaluationResult eval;
2582 * Did we finish processing the associated request?
2587 * Did we find a matching request?
2594 * We have received a reply; handle it!
2596 * @param cls response (struct ProcessReplyClosure)
2597 * @param key our query
2598 * @param value value in the hash map (info about the query)
2599 * @return GNUNET_YES (we should continue to iterate)
2602 process_reply (void *cls,
2603 const GNUNET_HashCode * key,
2606 struct ProcessReplyClosure *prq = cls;
2607 struct PendingRequest *pr = value;
2608 struct PendingMessage *reply;
2609 struct ClientResponseMessage *creply;
2610 struct ClientList *cl;
2611 struct PutMessage *pm;
2612 struct ConnectedPeer *cp;
2613 struct GNUNET_TIME_Relative cur_delay;
2617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2618 "Matched result (type %u) for query `%s' with pending request\n",
2619 (unsigned int) prq->type,
2622 GNUNET_STATISTICS_update (stats,
2623 gettext_noop ("# replies received and matched"),
2626 if (prq->sender != NULL)
2628 /* FIXME: should we be more precise here and not use
2629 "start_time" but a peer-specific time stamp? */
2630 cur_delay = GNUNET_TIME_absolute_get_duration (pr->start_time);
2631 prq->sender->avg_delay.value
2632 = (prq->sender->avg_delay.value *
2633 (RUNAVG_DELAY_N - 1) + cur_delay.value) / RUNAVG_DELAY_N;
2634 prq->sender->avg_priority
2635 = (prq->sender->avg_priority *
2636 (RUNAVG_DELAY_N - 1) + pr->priority) / (double) RUNAVG_DELAY_N;
2639 GNUNET_PEER_change_rc (prq->sender->last_p2p_replies
2640 [prq->sender->last_p2p_replies_woff % P2P_SUCCESS_LIST_SIZE],
2642 GNUNET_PEER_change_rc (pr->cp->pid, 1);
2643 prq->sender->last_p2p_replies
2644 [(prq->sender->last_p2p_replies_woff++) % P2P_SUCCESS_LIST_SIZE]
2649 if (NULL != prq->sender->last_client_replies
2650 [(prq->sender->last_client_replies_woff) % CS2P_SUCCESS_LIST_SIZE])
2651 GNUNET_SERVER_client_drop (prq->sender->last_client_replies
2652 [(prq->sender->last_client_replies_woff) % CS2P_SUCCESS_LIST_SIZE]);
2653 prq->sender->last_client_replies
2654 [(prq->sender->last_client_replies_woff++) % CS2P_SUCCESS_LIST_SIZE]
2655 = pr->client_request_list->client_list->client;
2656 GNUNET_SERVER_client_keep (pr->client_request_list->client_list->client);
2659 prq->eval = GNUNET_BLOCK_evaluate (block_ctx,
2664 pr->namespace, (pr->namespace != NULL) ? sizeof (GNUNET_HashCode) : 0,
2669 case GNUNET_BLOCK_EVALUATION_OK_MORE:
2671 case GNUNET_BLOCK_EVALUATION_OK_LAST:
2672 while (NULL != pr->pending_head)
2673 destroy_pending_message_list_entry (pr->pending_head);
2676 if (pr->client_request_list != NULL)
2677 GNUNET_SERVER_receive_done (pr->client_request_list->client_list->client,
2679 GNUNET_DATASTORE_cancel (pr->qe);
2682 pr->do_remove = GNUNET_YES;
2683 if (pr->task != GNUNET_SCHEDULER_NO_TASK)
2685 GNUNET_SCHEDULER_cancel (sched,
2687 pr->task = GNUNET_SCHEDULER_NO_TASK;
2689 GNUNET_break (GNUNET_YES ==
2690 GNUNET_CONTAINER_multihashmap_remove (query_request_map,
2694 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
2695 GNUNET_STATISTICS_update (stats,
2696 gettext_noop ("# duplicate replies discarded (bloomfilter)"),
2700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2701 "Duplicate response `%s', discarding.\n",
2702 GNUNET_h2s (&mhash));
2704 return GNUNET_YES; /* duplicate */
2705 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
2706 return GNUNET_YES; /* wrong namespace */
2707 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
2710 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
2713 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
2714 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2715 _("Unsupported block type %u\n"),
2719 if (pr->client_request_list != NULL)
2721 if (pr->replies_seen_size == pr->replies_seen_off)
2722 GNUNET_array_grow (pr->replies_seen,
2723 pr->replies_seen_size,
2724 pr->replies_seen_size * 2 + 4);
2725 GNUNET_CRYPTO_hash (prq->data,
2727 &pr->replies_seen[pr->replies_seen_off++]);
2728 refresh_bloomfilter (pr);
2730 if (NULL == prq->sender)
2733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2734 "Found result for query `%s' in local datastore\n",
2737 GNUNET_STATISTICS_update (stats,
2738 gettext_noop ("# results found locally"),
2742 prq->priority += pr->remaining_priority;
2743 pr->remaining_priority = 0;
2744 pr->results_found++;
2745 prq->request_found = GNUNET_YES;
2746 if (NULL != pr->client_request_list)
2748 GNUNET_STATISTICS_update (stats,
2749 gettext_noop ("# replies received for local clients"),
2752 cl = pr->client_request_list->client_list;
2753 msize = sizeof (struct PutMessage) + prq->size;
2754 creply = GNUNET_malloc (msize + sizeof (struct ClientResponseMessage));
2755 creply->msize = msize;
2756 creply->client_list = cl;
2757 GNUNET_CONTAINER_DLL_insert_after (cl->res_head,
2761 pm = (struct PutMessage*) &creply[1];
2762 pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
2763 pm->header.size = htons (msize);
2764 pm->type = htonl (prq->type);
2765 pm->expiration = GNUNET_TIME_absolute_hton (prq->expiration);
2766 memcpy (&pm[1], prq->data, prq->size);
2770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2771 "Transmitting result for query `%s' to client\n",
2774 cl->th = GNUNET_SERVER_notify_transmit_ready (cl->client,
2776 GNUNET_TIME_UNIT_FOREVER_REL,
2777 &transmit_to_client,
2780 GNUNET_break (cl->th != NULL);
2783 prq->finished = GNUNET_YES;
2784 destroy_pending_request (pr);
2791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2792 "Transmitting result for query `%s' to other peer (PID=%u)\n",
2794 (unsigned int) cp->pid);
2796 GNUNET_STATISTICS_update (stats,
2797 gettext_noop ("# replies received for other peers"),
2800 msize = sizeof (struct PutMessage) + prq->size;
2801 reply = GNUNET_malloc (msize + sizeof (struct PendingMessage));
2802 reply->cont = &transmit_reply_continuation;
2803 reply->cont_cls = pr;
2804 reply->msize = msize;
2805 reply->priority = UINT32_MAX; /* send replies first! */
2806 pm = (struct PutMessage*) &reply[1];
2807 pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
2808 pm->header.size = htons (msize);
2809 pm->type = htonl (prq->type);
2810 pm->expiration = GNUNET_TIME_absolute_hton (prq->expiration);
2811 memcpy (&pm[1], prq->data, prq->size);
2812 add_to_pending_messages_for_peer (cp, reply, pr);
2819 * Continuation called to notify client about result of the
2822 * @param cls closure
2823 * @param success GNUNET_SYSERR on failure
2824 * @param msg NULL on success, otherwise an error message
2827 put_migration_continuation (void *cls,
2831 struct GNUNET_TIME_Absolute *start = cls;
2832 struct GNUNET_TIME_Relative delay;
2834 delay = GNUNET_TIME_absolute_get_duration (*start);
2835 GNUNET_free (start);
2836 GNUNET_LOAD_update (datastore_put_load,
2838 if (GNUNET_OK == success)
2840 GNUNET_STATISTICS_update (stats,
2841 gettext_noop ("# datastore 'put' failures"),
2848 * Handle P2P "PUT" message.
2850 * @param cls closure, always NULL
2851 * @param other the other peer involved (sender or receiver, NULL
2852 * for loopback messages where we are both sender and receiver)
2853 * @param message the actual message
2854 * @param latency reported latency of the connection with 'other'
2855 * @param distance reported distance (DV) to 'other'
2856 * @return GNUNET_OK to keep the connection open,
2857 * GNUNET_SYSERR to close it (signal serious error)
2860 handle_p2p_put (void *cls,
2861 const struct GNUNET_PeerIdentity *other,
2862 const struct GNUNET_MessageHeader *message,
2863 struct GNUNET_TIME_Relative latency,
2866 const struct PutMessage *put;
2869 enum GNUNET_BLOCK_Type type;
2870 struct GNUNET_TIME_Absolute expiration;
2871 GNUNET_HashCode query;
2872 struct ProcessReplyClosure prq;
2873 struct GNUNET_TIME_Absolute *start;
2874 struct GNUNET_TIME_Relative block_time;
2876 struct ConnectedPeer *cp;
2877 struct PendingMessage *pm;
2878 struct MigrationStopMessage *msm;
2880 msize = ntohs (message->size);
2881 if (msize < sizeof (struct PutMessage))
2884 return GNUNET_SYSERR;
2886 put = (const struct PutMessage*) message;
2887 dsize = msize - sizeof (struct PutMessage);
2888 type = ntohl (put->type);
2889 expiration = GNUNET_TIME_absolute_ntoh (put->expiration);
2891 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
2892 return GNUNET_SYSERR;
2894 GNUNET_BLOCK_get_key (block_ctx,
2900 GNUNET_break_op (0);
2901 return GNUNET_SYSERR;
2904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2905 "Received result for query `%s' from peer `%4s'\n",
2906 GNUNET_h2s (&query),
2907 GNUNET_i2s (other));
2909 GNUNET_STATISTICS_update (stats,
2910 gettext_noop ("# replies received (overall)"),
2913 /* now, lookup 'query' */
2914 prq.data = (const void*) &put[1];
2916 prq.sender = GNUNET_CONTAINER_multihashmap_get (connected_peers,
2917 &other->hashPubKey);
2922 prq.expiration = expiration;
2924 prq.finished = GNUNET_NO;
2925 prq.request_found = GNUNET_NO;
2926 GNUNET_CONTAINER_multihashmap_get_multiple (query_request_map,
2930 if (prq.sender != NULL)
2932 prq.sender->inc_preference += CONTENT_BANDWIDTH_VALUE + 1000 * prq.priority;
2933 prq.sender->trust += prq.priority;
2935 if (GNUNET_YES == active_migration)
2938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2939 "Replicating result for query `%s' with priority %u\n",
2940 GNUNET_h2s (&query),
2943 start = GNUNET_malloc (sizeof (struct GNUNET_TIME_Absolute));
2944 *start = GNUNET_TIME_absolute_get ();
2945 GNUNET_DATASTORE_put (dsh,
2946 0, &query, dsize, &put[1],
2947 type, prq.priority, 1 /* anonymity */,
2949 1 + prq.priority, MAX_DATASTORE_QUEUE,
2950 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
2951 &put_migration_continuation,
2954 putl = GNUNET_LOAD_get_load (datastore_put_load);
2955 if ( (GNUNET_NO == prq.request_found) &&
2956 ( (GNUNET_YES != active_migration) ||
2959 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
2960 &other->hashPubKey);
2961 if (GNUNET_TIME_absolute_get_duration (cp->last_migration_block).value < 5000)
2962 return GNUNET_OK; /* already blocked */
2963 /* We're too busy; send MigrationStop message! */
2964 if (GNUNET_YES != active_migration)
2965 putl = 1.0 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 5);
2966 block_time = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
2967 5000 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2968 (unsigned int) (60000 * putl * putl)));
2970 cp->last_migration_block = GNUNET_TIME_relative_to_absolute (block_time);
2971 pm = GNUNET_malloc (sizeof (struct PendingMessage) +
2972 sizeof (struct MigrationStopMessage));
2973 pm->msize = sizeof (struct MigrationStopMessage);
2974 pm->priority = UINT32_MAX;
2975 msm = (struct MigrationStopMessage*) &pm[1];
2976 msm->header.size = htons (sizeof (struct MigrationStopMessage));
2977 msm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_MIGRATION_STOP);
2978 msm->duration = GNUNET_TIME_relative_hton (block_time);
2979 add_to_pending_messages_for_peer (cp,
2988 * Handle P2P "MIGRATION_STOP" message.
2990 * @param cls closure, always NULL
2991 * @param other the other peer involved (sender or receiver, NULL
2992 * for loopback messages where we are both sender and receiver)
2993 * @param message the actual message
2994 * @param latency reported latency of the connection with 'other'
2995 * @param distance reported distance (DV) to 'other'
2996 * @return GNUNET_OK to keep the connection open,
2997 * GNUNET_SYSERR to close it (signal serious error)
3000 handle_p2p_migration_stop (void *cls,
3001 const struct GNUNET_PeerIdentity *other,
3002 const struct GNUNET_MessageHeader *message,
3003 struct GNUNET_TIME_Relative latency,
3006 struct ConnectedPeer *cp;
3007 const struct MigrationStopMessage *msm;
3009 msm = (const struct MigrationStopMessage*) message;
3010 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
3011 &other->hashPubKey);
3017 cp->migration_blocked = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_ntoh (msm->duration));
3023 /* **************************** P2P GET Handling ************************ */
3027 * Closure for 'check_duplicate_request_{peer,client}'.
3029 struct CheckDuplicateRequestClosure
3032 * The new request we should check if it already exists.
3034 const struct PendingRequest *pr;
3037 * Existing request found by the checker, NULL if none.
3039 struct PendingRequest *have;
3044 * Iterator over entries in the 'query_request_map' that
3045 * tries to see if we have the same request pending from
3046 * the same client already.
3048 * @param cls closure (our 'struct CheckDuplicateRequestClosure')
3049 * @param key current key code (query, ignored, must match)
3050 * @param value value in the hash map (a 'struct PendingRequest'
3051 * that already exists)
3052 * @return GNUNET_YES if we should continue to
3053 * iterate (no match yet)
3054 * GNUNET_NO if not (match found).
3057 check_duplicate_request_client (void *cls,
3058 const GNUNET_HashCode * key,
3061 struct CheckDuplicateRequestClosure *cdc = cls;
3062 struct PendingRequest *have = value;
3064 if (have->client_request_list == NULL)
3066 if ( (cdc->pr->client_request_list->client_list->client == have->client_request_list->client_list->client) &&
3077 * We're processing (local) results for a search request
3078 * from another peer. Pass applicable results to the
3079 * peer and if we are done either clean up (operation
3080 * complete) or forward to other peers (more results possible).
3082 * @param cls our closure (struct LocalGetContext)
3083 * @param key key for the content
3084 * @param size number of bytes in data
3085 * @param data content stored
3086 * @param type type of the content
3087 * @param priority priority of the content
3088 * @param anonymity anonymity-level for the content
3089 * @param expiration expiration time for the content
3090 * @param uid unique identifier for the datum;
3091 * maybe 0 if no unique identifier is available
3094 process_local_reply (void *cls,
3095 const GNUNET_HashCode * key,
3098 enum GNUNET_BLOCK_Type type,
3101 struct GNUNET_TIME_Absolute
3105 struct PendingRequest *pr = cls;
3106 struct ProcessReplyClosure prq;
3107 struct CheckDuplicateRequestClosure cdrc;
3108 GNUNET_HashCode query;
3109 unsigned int old_rf;
3114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3115 "Done processing local replies, forwarding request to other peers.\n");
3118 if (pr->client_request_list != NULL)
3120 GNUNET_SERVER_receive_done (pr->client_request_list->client_list->client,
3122 /* Figure out if this is a duplicate request and possibly
3123 merge 'struct PendingRequest' entries */
3126 GNUNET_CONTAINER_multihashmap_get_multiple (query_request_map,
3128 &check_duplicate_request_client,
3130 if (cdrc.have != NULL)
3133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3134 "Received request for block `%s' twice from client, will only request once.\n",
3135 GNUNET_h2s (&pr->query));
3138 destroy_pending_request (pr);
3143 /* no more results */
3144 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
3145 pr->task = GNUNET_SCHEDULER_add_now (sched,
3146 &forward_request_task,
3151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3152 "New local response to `%s' of type %u.\n",
3156 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
3159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3160 "Found ONDEMAND block, performing on-demand encoding\n");
3162 GNUNET_STATISTICS_update (stats,
3163 gettext_noop ("# on-demand blocks matched requests"),
3167 GNUNET_FS_handle_on_demand_block (key, size, data, type, priority,
3168 anonymity, expiration, uid,
3169 &process_local_reply,
3173 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
3177 old_rf = pr->results_found;
3178 memset (&prq, 0, sizeof (prq));
3180 prq.expiration = expiration;
3183 GNUNET_BLOCK_get_key (block_ctx,
3190 GNUNET_DATASTORE_remove (dsh,
3194 GNUNET_TIME_UNIT_FOREVER_REL,
3196 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
3200 prq.priority = priority;
3201 prq.finished = GNUNET_NO;
3202 prq.request_found = GNUNET_NO;
3203 process_reply (&prq, key, pr);
3204 if ( (old_rf == 0) &&
3205 (pr->results_found == 1) )
3206 update_datastore_delays (pr->start_time);
3207 if (prq.finished == GNUNET_YES)
3210 return; /* done here */
3211 if (prq.eval == GNUNET_BLOCK_EVALUATION_OK_LAST)
3213 GNUNET_DATASTORE_get_next (dsh, GNUNET_NO);
3216 if ( (pr->client_request_list == NULL) &&
3217 ( (GNUNET_YES == test_load_too_high()) ||
3218 (pr->results_found > 5 + 2 * pr->priority) ) )
3221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3222 "Load too high, done with request\n");
3224 GNUNET_STATISTICS_update (stats,
3225 gettext_noop ("# processing result set cut short due to load"),
3228 GNUNET_DATASTORE_get_next (dsh, GNUNET_NO);
3231 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
3236 * We've received a request with the specified priority. Bound it
3237 * according to how much we trust the given peer.
3239 * @param prio_in requested priority
3240 * @param cp the peer making the request
3241 * @return effective priority
3244 bound_priority (uint32_t prio_in,
3245 struct ConnectedPeer *cp)
3247 #define N ((double)128.0)
3252 ld = test_load_too_high ();
3253 if (ld == GNUNET_SYSERR)
3254 return 0; /* excess resources */
3255 ret = change_host_trust (cp, prio_in);
3258 if (ret > current_priorities + N)
3259 rret = current_priorities + N;
3263 = (current_priorities * (N-1) + rret)/N;
3271 * Iterator over entries in the 'query_request_map' that
3272 * tries to see if we have the same request pending from
3273 * the same peer already.
3275 * @param cls closure (our 'struct CheckDuplicateRequestClosure')
3276 * @param key current key code (query, ignored, must match)
3277 * @param value value in the hash map (a 'struct PendingRequest'
3278 * that already exists)
3279 * @return GNUNET_YES if we should continue to
3280 * iterate (no match yet)
3281 * GNUNET_NO if not (match found).
3284 check_duplicate_request_peer (void *cls,
3285 const GNUNET_HashCode * key,
3288 struct CheckDuplicateRequestClosure *cdc = cls;
3289 struct PendingRequest *have = value;
3291 if (cdc->pr->target_pid == have->target_pid)
3301 * Handle P2P "GET" request.
3303 * @param cls closure, always NULL
3304 * @param other the other peer involved (sender or receiver, NULL
3305 * for loopback messages where we are both sender and receiver)
3306 * @param message the actual message
3307 * @param latency reported latency of the connection with 'other'
3308 * @param distance reported distance (DV) to 'other'
3309 * @return GNUNET_OK to keep the connection open,
3310 * GNUNET_SYSERR to close it (signal serious error)
3313 handle_p2p_get (void *cls,
3314 const struct GNUNET_PeerIdentity *other,
3315 const struct GNUNET_MessageHeader *message,
3316 struct GNUNET_TIME_Relative latency,
3319 struct PendingRequest *pr;
3320 struct ConnectedPeer *cp;
3321 struct ConnectedPeer *cps;
3322 struct CheckDuplicateRequestClosure cdc;
3323 struct GNUNET_TIME_Relative timeout;
3325 const struct GetMessage *gm;
3327 const GNUNET_HashCode *opt;
3330 uint32_t ttl_decrement;
3331 enum GNUNET_BLOCK_Type type;
3335 msize = ntohs(message->size);
3336 if (msize < sizeof (struct GetMessage))
3338 GNUNET_break_op (0);
3339 return GNUNET_SYSERR;
3341 gm = (const struct GetMessage*) message;
3342 type = ntohl (gm->type);
3343 bm = ntohl (gm->hash_bitmap);
3351 if (msize < sizeof (struct GetMessage) + bits * sizeof (GNUNET_HashCode))
3353 GNUNET_break_op (0);
3354 return GNUNET_SYSERR;
3356 opt = (const GNUNET_HashCode*) &gm[1];
3357 bfsize = msize - sizeof (struct GetMessage) + bits * sizeof (GNUNET_HashCode);
3358 bm = ntohl (gm->hash_bitmap);
3360 cps = GNUNET_CONTAINER_multihashmap_get (connected_peers,
3361 &other->hashPubKey);
3364 /* peer must have just disconnected */
3365 GNUNET_STATISTICS_update (stats,
3366 gettext_noop ("# requests dropped due to initiator not being connected"),
3369 return GNUNET_SYSERR;
3371 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
3372 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
3379 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
3380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3381 "Failed to find RETURN-TO peer `%4s' in connection set. Dropping query.\n",
3382 GNUNET_i2s ((const struct GNUNET_PeerIdentity*) &opt[bits-1]));
3385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3386 "Failed to find peer `%4s' in connection set. Dropping query.\n",
3387 GNUNET_i2s (other));
3389 GNUNET_STATISTICS_update (stats,
3390 gettext_noop ("# requests dropped due to missing reverse route"),
3393 /* FIXME: try connect? */
3396 /* note that we can really only check load here since otherwise
3397 peers could find out that we are overloaded by not being
3398 disconnected after sending us a malformed query... */
3400 /* FIXME: query priority should play
3401 a major role here! */
3402 ld = test_load_too_high ();
3403 if (GNUNET_YES == ld)
3406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3407 "Dropping query from `%s', this peer is too busy.\n",
3408 GNUNET_i2s (other));
3410 GNUNET_STATISTICS_update (stats,
3411 gettext_noop ("# requests dropped due to high load"),
3416 /* FIXME: if ld == GNUNET_NO, forward
3417 instead of indirecting! */
3420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3421 "Received request for `%s' of type %u from peer `%4s' with flags %u\n",
3422 GNUNET_h2s (&gm->query),
3423 (unsigned int) type,
3427 have_ns = (0 != (bm & GET_MESSAGE_BIT_SKS_NAMESPACE));
3428 pr = GNUNET_malloc (sizeof (struct PendingRequest) +
3429 (have_ns ? sizeof(GNUNET_HashCode) : 0));
3432 pr->namespace = (GNUNET_HashCode*) &pr[1];
3433 memcpy (&pr[1], &opt[bits++], sizeof (GNUNET_HashCode));
3436 pr->mingle = ntohl (gm->filter_mutator);
3437 if (0 != (bm & GET_MESSAGE_BIT_TRANSMIT_TO))
3438 pr->target_pid = GNUNET_PEER_intern ((const struct GNUNET_PeerIdentity*) &opt[bits++]);
3439 pr->anonymity_level = 1;
3440 pr->priority = bound_priority (ntohl (gm->priority), cps);
3441 pr->ttl = bound_ttl (ntohl (gm->ttl), pr->priority);
3442 pr->query = gm->query;
3443 /* decrement ttl (always) */
3444 ttl_decrement = 2 * TTL_DECREMENT +
3445 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
3447 if ( (pr->ttl < 0) &&
3448 (((int32_t)(pr->ttl - ttl_decrement)) > 0) )
3451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3452 "Dropping query from `%s' due to TTL underflow (%d - %u).\n",
3457 GNUNET_STATISTICS_update (stats,
3458 gettext_noop ("# requests dropped due TTL underflow"),
3461 /* integer underflow => drop (should be very rare)! */
3465 pr->ttl -= ttl_decrement;
3466 pr->start_time = GNUNET_TIME_absolute_get ();
3468 /* get bloom filter */
3471 pr->bf = GNUNET_CONTAINER_bloomfilter_init ((const char*) &opt[bits],
3474 pr->bf_size = bfsize;
3479 GNUNET_CONTAINER_multihashmap_get_multiple (query_request_map,
3481 &check_duplicate_request_peer,
3483 if (cdc.have != NULL)
3485 if (cdc.have->start_time.value + cdc.have->ttl >=
3486 pr->start_time.value + pr->ttl)
3488 /* existing request has higher TTL, drop new one! */
3489 cdc.have->priority += pr->priority;
3490 destroy_pending_request (pr);
3492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3493 "Have existing request with higher TTL, dropping new request.\n",
3494 GNUNET_i2s (other));
3496 GNUNET_STATISTICS_update (stats,
3497 gettext_noop ("# requests dropped due to higher-TTL request"),
3504 /* existing request has lower TTL, drop old one! */
3505 pr->priority += cdc.have->priority;
3506 /* Possible optimization: if we have applicable pending
3507 replies in 'cdc.have', we might want to move those over
3508 (this is a really rare special-case, so it is not clear
3509 that this would be worth it) */
3510 destroy_pending_request (cdc.have);
3511 /* keep processing 'pr'! */
3516 GNUNET_break (GNUNET_OK ==
3517 GNUNET_CONTAINER_multihashmap_put (query_request_map,
3520 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
3521 GNUNET_break (GNUNET_OK ==
3522 GNUNET_CONTAINER_multihashmap_put (peer_request_map,
3525 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
3527 pr->hnode = GNUNET_CONTAINER_heap_insert (requests_by_expiration_heap,
3529 pr->start_time.value + pr->ttl);
3531 GNUNET_STATISTICS_update (stats,
3532 gettext_noop ("# P2P searches received"),
3535 GNUNET_STATISTICS_update (stats,
3536 gettext_noop ("# P2P searches active"),
3540 /* calculate change in traffic preference */
3541 cps->inc_preference += pr->priority * 1000 + QUERY_BANDWIDTH_VALUE;
3542 /* process locally */
3543 if (type == GNUNET_BLOCK_TYPE_FS_DBLOCK)
3544 type = GNUNET_BLOCK_TYPE_ANY; /* to get on-demand as well */
3545 timeout = GNUNET_TIME_relative_multiply (BASIC_DATASTORE_REQUEST_DELAY,
3546 (pr->priority + 1));
3547 pr->qe = GNUNET_DATASTORE_get (dsh,
3551 MAX_DATASTORE_QUEUE,
3553 &process_local_reply,
3556 /* Are multiple results possible? If so, start processing remotely now! */
3559 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
3560 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
3561 /* only one result, wait for datastore */
3564 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
3565 pr->task = GNUNET_SCHEDULER_add_now (sched,
3566 &forward_request_task,
3570 /* make sure we don't track too many requests */
3571 if (GNUNET_CONTAINER_heap_get_size (requests_by_expiration_heap) > max_pending_requests)
3573 pr = GNUNET_CONTAINER_heap_peek (requests_by_expiration_heap);
3574 GNUNET_assert (pr != NULL);
3575 destroy_pending_request (pr);
3581 /* **************************** CS GET Handling ************************ */
3585 * Handle START_SEARCH-message (search request from client).
3587 * @param cls closure
3588 * @param client identification of the client
3589 * @param message the actual message
3592 handle_start_search (void *cls,
3593 struct GNUNET_SERVER_Client *client,
3594 const struct GNUNET_MessageHeader *message)
3596 static GNUNET_HashCode all_zeros;
3597 const struct SearchMessage *sm;
3598 struct ClientList *cl;
3599 struct ClientRequestList *crl;
3600 struct PendingRequest *pr;
3603 enum GNUNET_BLOCK_Type type;
3605 msize = ntohs (message->size);
3606 if ( (msize < sizeof (struct SearchMessage)) ||
3607 (0 != (msize - sizeof (struct SearchMessage)) % sizeof (GNUNET_HashCode)) )
3610 GNUNET_SERVER_receive_done (client,
3614 GNUNET_STATISTICS_update (stats,
3615 gettext_noop ("# client searches received"),
3618 sc = (msize - sizeof (struct SearchMessage)) / sizeof (GNUNET_HashCode);
3619 sm = (const struct SearchMessage*) message;
3620 type = ntohl (sm->type);
3622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3623 "Received request for `%s' of type %u from local client\n",
3624 GNUNET_h2s (&sm->query),
3625 (unsigned int) type);
3628 while ( (cl != NULL) &&
3629 (cl->client != client) )
3633 cl = GNUNET_malloc (sizeof (struct ClientList));
3634 cl->client = client;
3635 GNUNET_SERVER_client_keep (client);
3636 cl->next = client_list;
3639 /* detect duplicate KBLOCK requests */
3640 if ( (type == GNUNET_BLOCK_TYPE_FS_KBLOCK) ||
3641 (type == GNUNET_BLOCK_TYPE_FS_NBLOCK) ||
3642 (type == GNUNET_BLOCK_TYPE_ANY) )
3645 while ( (crl != NULL) &&
3646 ( (0 != memcmp (&crl->req->query,
3648 sizeof (GNUNET_HashCode))) ||
3649 (crl->req->type != type) ) )
3654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3655 "Have existing request, merging content-seen lists.\n");
3658 /* Duplicate request (used to send long list of
3659 known/blocked results); merge 'pr->replies_seen'
3660 and update bloom filter */
3661 GNUNET_array_grow (pr->replies_seen,
3662 pr->replies_seen_size,
3663 pr->replies_seen_off + sc);
3664 memcpy (&pr->replies_seen[pr->replies_seen_off],
3666 sc * sizeof (GNUNET_HashCode));
3667 pr->replies_seen_off += sc;
3668 refresh_bloomfilter (pr);
3669 GNUNET_STATISTICS_update (stats,
3670 gettext_noop ("# client searches updated (merged content seen list)"),
3673 GNUNET_SERVER_receive_done (client,
3678 GNUNET_STATISTICS_update (stats,
3679 gettext_noop ("# client searches active"),
3682 pr = GNUNET_malloc (sizeof (struct PendingRequest) +
3683 ((type == GNUNET_BLOCK_TYPE_FS_SBLOCK) ? sizeof(GNUNET_HashCode) : 0));
3684 crl = GNUNET_malloc (sizeof (struct ClientRequestList));
3685 memset (crl, 0, sizeof (struct ClientRequestList));
3686 crl->client_list = cl;
3687 GNUNET_CONTAINER_DLL_insert (cl->rl_head,
3692 pr->client_request_list = crl;
3693 GNUNET_array_grow (pr->replies_seen,
3694 pr->replies_seen_size,
3696 memcpy (pr->replies_seen,
3698 sc * sizeof (GNUNET_HashCode));
3699 pr->replies_seen_off = sc;
3700 pr->anonymity_level = ntohl (sm->anonymity_level);
3701 pr->start_time = GNUNET_TIME_absolute_get ();
3702 refresh_bloomfilter (pr);
3703 pr->query = sm->query;
3704 if (0 == (1 & ntohl (sm->options)))
3705 pr->local_only = GNUNET_NO;
3707 pr->local_only = GNUNET_YES;
3710 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
3711 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
3712 if (0 != memcmp (&sm->target,
3714 sizeof (GNUNET_HashCode)))
3715 pr->target_pid = GNUNET_PEER_intern ((const struct GNUNET_PeerIdentity*) &sm->target);
3717 case GNUNET_BLOCK_TYPE_FS_SBLOCK:
3718 pr->namespace = (GNUNET_HashCode*) &pr[1];
3719 memcpy (&pr[1], &sm->target, sizeof (GNUNET_HashCode));
3724 GNUNET_break (GNUNET_OK ==
3725 GNUNET_CONTAINER_multihashmap_put (query_request_map,
3728 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
3729 if (type == GNUNET_BLOCK_TYPE_FS_DBLOCK)
3730 type = GNUNET_BLOCK_TYPE_ANY; /* get on-demand blocks too! */
3731 pr->qe = GNUNET_DATASTORE_get (dsh,
3735 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
3736 &process_local_reply,
3741 /* **************************** Startup ************************ */
3744 * Process fs requests.
3746 * @param s scheduler to use
3747 * @param server the initialized server
3748 * @param c configuration to use
3751 main_init (struct GNUNET_SCHEDULER_Handle *s,
3752 struct GNUNET_SERVER_Handle *server,
3753 const struct GNUNET_CONFIGURATION_Handle *c)
3755 static const struct GNUNET_CORE_MessageHandler p2p_handlers[] =
3758 GNUNET_MESSAGE_TYPE_FS_GET, 0 },
3760 GNUNET_MESSAGE_TYPE_FS_PUT, 0 },
3761 { &handle_p2p_migration_stop,
3762 GNUNET_MESSAGE_TYPE_FS_MIGRATION_STOP,
3763 sizeof (struct MigrationStopMessage) },
3766 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
3767 {&GNUNET_FS_handle_index_start, NULL,
3768 GNUNET_MESSAGE_TYPE_FS_INDEX_START, 0},
3769 {&GNUNET_FS_handle_index_list_get, NULL,
3770 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_GET, sizeof(struct GNUNET_MessageHeader) },
3771 {&GNUNET_FS_handle_unindex, NULL, GNUNET_MESSAGE_TYPE_FS_UNINDEX,
3772 sizeof (struct UnindexMessage) },
3773 {&handle_start_search, NULL, GNUNET_MESSAGE_TYPE_FS_START_SEARCH,
3777 unsigned long long enc = 128;
3781 stats = GNUNET_STATISTICS_create (sched, "fs", cfg);
3782 min_migration_delay = GNUNET_TIME_UNIT_SECONDS;
3784 GNUNET_CONFIGURATION_get_value_number (cfg,
3786 "MAX_PENDING_REQUESTS",
3787 &max_pending_requests)) ||
3789 GNUNET_CONFIGURATION_get_value_number (cfg,
3791 "EXPECTED_NEIGHBOUR_COUNT",
3794 GNUNET_CONFIGURATION_get_value_time (cfg,
3796 "MIN_MIGRATION_DELAY",
3797 &min_migration_delay)) )
3799 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3800 _("Configuration fails to specify certain parameters, assuming default values."));
3802 connected_peers = GNUNET_CONTAINER_multihashmap_create (enc);
3803 query_request_map = GNUNET_CONTAINER_multihashmap_create (max_pending_requests);
3804 peer_request_map = GNUNET_CONTAINER_multihashmap_create (enc);
3805 requests_by_expiration_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
3806 core = GNUNET_CORE_connect (sched,
3808 GNUNET_TIME_UNIT_FOREVER_REL,
3811 &peer_connect_handler,
3812 &peer_disconnect_handler,
3819 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3820 _("Failed to connect to `%s' service.\n"),
3822 GNUNET_CONTAINER_multihashmap_destroy (connected_peers);
3823 connected_peers = NULL;
3824 GNUNET_CONTAINER_multihashmap_destroy (query_request_map);
3825 query_request_map = NULL;
3826 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
3827 requests_by_expiration_heap = NULL;
3828 GNUNET_CONTAINER_multihashmap_destroy (peer_request_map);
3829 peer_request_map = NULL;
3832 GNUNET_DATASTORE_disconnect (dsh, GNUNET_NO);
3835 return GNUNET_SYSERR;
3837 /* FIXME: distinguish between sending and storing in options? */
3838 if (active_migration)
3840 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3841 _("Content migration is enabled, will start to gather data\n"));
3842 consider_migration_gathering ();
3844 GNUNET_SERVER_disconnect_notify (server,
3845 &handle_client_disconnect,
3847 GNUNET_assert (GNUNET_OK ==
3848 GNUNET_CONFIGURATION_get_value_filename (cfg,
3852 GNUNET_DISK_directory_create (trustDirectory);
3853 GNUNET_SCHEDULER_add_with_priority (sched,
3854 GNUNET_SCHEDULER_PRIORITY_HIGH,
3855 &cron_flush_trust, NULL);
3858 GNUNET_SERVER_add_handlers (server, handlers);
3859 GNUNET_SCHEDULER_add_delayed (sched,
3860 GNUNET_TIME_UNIT_FOREVER_REL,
3868 * Process fs requests.
3870 * @param cls closure
3871 * @param sched scheduler to use
3872 * @param server the initialized server
3873 * @param cfg configuration to use
3877 struct GNUNET_SCHEDULER_Handle *sched,
3878 struct GNUNET_SERVER_Handle *server,
3879 const struct GNUNET_CONFIGURATION_Handle *cfg)
3881 active_migration = GNUNET_CONFIGURATION_get_value_yesno (cfg,
3884 dsh = GNUNET_DATASTORE_connect (cfg,
3888 GNUNET_SCHEDULER_shutdown (sched);
3891 datastore_get_load = GNUNET_LOAD_value_init ();
3892 datastore_put_load = GNUNET_LOAD_value_init ();
3893 block_cfg = GNUNET_CONFIGURATION_create ();
3894 GNUNET_CONFIGURATION_set_value_string (block_cfg,
3898 block_ctx = GNUNET_BLOCK_context_create (block_cfg);
3899 GNUNET_assert (NULL != block_ctx);
3900 dht_handle = GNUNET_DHT_connect (sched,
3903 if ( (GNUNET_OK != GNUNET_FS_indexing_init (sched, cfg, dsh)) ||
3904 (GNUNET_OK != main_init (sched, server, cfg)) )
3906 GNUNET_SCHEDULER_shutdown (sched);
3907 GNUNET_DATASTORE_disconnect (dsh, GNUNET_NO);
3909 GNUNET_DHT_disconnect (dht_handle);
3911 GNUNET_BLOCK_context_destroy (block_ctx);
3913 GNUNET_CONFIGURATION_destroy (block_cfg);
3915 GNUNET_LOAD_value_free (datastore_get_load);
3916 datastore_get_load = NULL;
3917 GNUNET_LOAD_value_free (datastore_put_load);
3918 datastore_put_load = NULL;
3925 * The main function for the fs service.
3927 * @param argc number of arguments from the command line
3928 * @param argv command line arguments
3929 * @return 0 ok, 1 on error
3932 main (int argc, char *const *argv)
3934 return (GNUNET_OK ==
3935 GNUNET_SERVICE_run (argc,
3938 GNUNET_SERVICE_OPTION_NONE,
3939 &run, NULL)) ? 0 : 1;
3942 /* end of gnunet-service-fs.c */