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 2, 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 * - TTL/priority calculations are absent!
29 * - have non-zero preference / priority for requests we initiate!
30 * - track stats for hot-path routing
31 * - implement hot-path routing decision procedure
32 * - implement: bound_priority, test_load_too_high, validate_nblock
33 * - add content migration support (store locally) [or create new service]
38 #include "gnunet_constants.h"
39 #include "gnunet_core_service.h"
40 #include "gnunet_datastore_service.h"
41 #include "gnunet_peer_lib.h"
42 #include "gnunet_protocols.h"
43 #include "gnunet_signatures.h"
44 #include "gnunet_statistics_service.h"
45 #include "gnunet_util_lib.h"
46 #include "gnunet-service-fs_indexing.h"
49 #define DEBUG_FS GNUNET_NO
52 * Maximum number of outgoing messages we queue per peer.
53 * FIXME: make configurable?
55 #define MAX_QUEUE_PER_PEER 16
58 * Inverse of the probability that we will submit the same query
59 * to the same peer again. If the same peer already got the query
60 * repeatedly recently, the probability is multiplied by the inverse
61 * of this number each time. Note that we only try about every TTL_DECREMENT/2
62 * plus MAX_CORK_DELAY (so roughly every 3.5s).
64 #define RETRY_PROBABILITY_INV 3
67 * What is the maximum delay for a P2P FS message (in our interaction
68 * with core)? FS-internal delays are another story. The value is
69 * chosen based on the 32k block size. Given that peers typcially
70 * have at least 1 kb/s bandwidth, 45s waits give us a chance to
71 * transmit one message even to the lowest-bandwidth peers.
73 #define MAX_TRANSMIT_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 45)
78 * Maximum number of requests (from other peers) that we're
79 * willing to have pending at any given point in time.
80 * FIXME: set from configuration.
82 static uint64_t max_pending_requests = (32 * 1024);
86 * Information we keep for each pending reply. The
87 * actual message follows at the end of this struct.
89 struct PendingMessage;
92 * Our connection to the datastore.
94 static struct GNUNET_DATASTORE_Handle *dsh;
98 * Function called upon completion of a transmission.
101 * @param pid ID of receiving peer, 0 on transmission error
103 typedef void (*TransmissionContinuation)(void * cls,
104 GNUNET_PEER_Id tpid);
108 * Information we keep for each pending message (GET/PUT). The
109 * actual message follows at the end of this struct.
111 struct PendingMessage
114 * This is a doubly-linked list of messages to the same peer.
116 struct PendingMessage *next;
119 * This is a doubly-linked list of messages to the same peer.
121 struct PendingMessage *prev;
124 * Entry in pending message list for this pending message.
126 struct PendingMessageList *pml;
129 * Function to call immediately once we have transmitted this
132 TransmissionContinuation cont;
140 * Size of the reply; actual reply message follows
141 * at the end of this struct.
146 * How important is this message for us?
154 * Information about a peer that we are connected to.
155 * We track data that is useful for determining which
156 * peers should receive our requests. We also keep
157 * a list of messages to transmit to this peer.
163 * List of the last clients for which this peer successfully
166 struct GNUNET_SERVER_Client *last_client_replies[CS2P_SUCCESS_LIST_SIZE];
169 * List of the last PIDs for which
170 * this peer successfully answered a query;
171 * We use 0 to indicate no successful reply.
173 GNUNET_PEER_Id last_p2p_replies[P2P_SUCCESS_LIST_SIZE];
176 * Average delay between sending the peer a request and
177 * getting a reply (only calculated over the requests for
178 * which we actually got a reply). Calculated
179 * as a moving average: new_delay = ((n-1)*last_delay+curr_delay) / n
181 struct GNUNET_TIME_Relative avg_delay;
184 * Handle for an active request for transmission to this
187 struct GNUNET_CORE_TransmitHandle *cth;
190 * Messages (replies, queries, content migration) we would like to
191 * send to this peer in the near future. Sorted by priority, head.
193 struct PendingMessage *pending_messages_head;
196 * Messages (replies, queries, content migration) we would like to
197 * send to this peer in the near future. Sorted by priority, tail.
199 struct PendingMessage *pending_messages_tail;
202 * Average priority of successful replies. Calculated
203 * as a moving average: new_avg = ((n-1)*last_avg+curr_prio) / n
208 * Increase in traffic preference still to be submitted
209 * to the core service for this peer. FIXME: double or 'uint64_t'?
211 double inc_preference;
214 * The peer's identity.
219 * Size of the linked list of 'pending_messages'.
221 unsigned int pending_requests;
224 * Which offset in "last_p2p_replies" will be updated next?
225 * (we go round-robin).
227 unsigned int last_p2p_replies_woff;
230 * Which offset in "last_client_replies" will be updated next?
231 * (we go round-robin).
233 unsigned int last_client_replies_woff;
239 * Information we keep for each pending request. We should try to
240 * keep this struct as small as possible since its memory consumption
241 * is key to how many requests we can have pending at once.
243 struct PendingRequest;
247 * Doubly-linked list of requests we are performing
248 * on behalf of the same client.
250 struct ClientRequestList
254 * This is a doubly-linked list.
256 struct ClientRequestList *next;
259 * This is a doubly-linked list.
261 struct ClientRequestList *prev;
264 * Request this entry represents.
266 struct PendingRequest *req;
269 * Client list this request belongs to.
271 struct ClientList *client_list;
277 * Replies to be transmitted to the client. The actual
278 * response message is allocated after this struct.
280 struct ClientResponseMessage
283 * This is a doubly-linked list.
285 struct ClientResponseMessage *next;
288 * This is a doubly-linked list.
290 struct ClientResponseMessage *prev;
293 * Client list entry this response belongs to.
295 struct ClientList *client_list;
298 * Number of bytes in the response.
305 * Linked list of clients we are performing requests
311 * This is a linked list.
313 struct ClientList *next;
316 * ID of a client making a request, NULL if this entry is for a
319 struct GNUNET_SERVER_Client *client;
322 * Head of list of requests performed on behalf
323 * of this client right now.
325 struct ClientRequestList *rl_head;
328 * Tail of list of requests performed on behalf
329 * of this client right now.
331 struct ClientRequestList *rl_tail;
334 * Head of linked list of responses.
336 struct ClientResponseMessage *res_head;
339 * Tail of linked list of responses.
341 struct ClientResponseMessage *res_tail;
344 * Context for sending replies.
346 struct GNUNET_CONNECTION_TransmitHandle *th;
352 * Doubly-linked list of messages we are performing
353 * due to a pending request.
355 struct PendingMessageList
359 * This is a doubly-linked list of messages on behalf of the same request.
361 struct PendingMessageList *next;
364 * This is a doubly-linked list of messages on behalf of the same request.
366 struct PendingMessageList *prev;
369 * Message this entry represents.
371 struct PendingMessage *pm;
374 * Request this entry belongs to.
376 struct PendingRequest *req;
379 * Peer this message is targeted for.
381 struct ConnectedPeer *target;
387 * Information we keep for each pending request. We should try to
388 * keep this struct as small as possible since its memory consumption
389 * is key to how many requests we can have pending at once.
391 struct PendingRequest
395 * If this request was made by a client, this is our entry in the
396 * client request list; otherwise NULL.
398 struct ClientRequestList *client_request_list;
401 * Entry of peer responsible for this entry (if this request
402 * was made by a peer).
404 struct ConnectedPeer *cp;
407 * If this is a namespace query, pointer to the hash of the public
408 * key of the namespace; otherwise NULL. Pointer will be to the
409 * end of this struct (so no need to free it).
411 const GNUNET_HashCode *namespace;
414 * Bloomfilter we use to filter out replies that we don't care about
415 * (anymore). NULL as long as we are interested in all replies.
417 struct GNUNET_CONTAINER_BloomFilter *bf;
420 * Context of our GNUNET_CORE_peer_change_preference call.
422 struct GNUNET_CORE_InformationRequestContext *irc;
425 * Hash code of all replies that we have seen so far (only valid
426 * if client is not NULL since we only track replies like this for
429 GNUNET_HashCode *replies_seen;
432 * Node in the heap representing this entry; NULL
433 * if we have no heap node.
435 struct GNUNET_CONTAINER_HeapNode *hnode;
438 * Head of list of messages being performed on behalf of this
441 struct PendingMessageList *pending_head;
444 * Tail of list of messages being performed on behalf of this
447 struct PendingMessageList *pending_tail;
450 * When did we first see this request (form this peer), or, if our
451 * client is initiating, when did we last initiate a search?
453 struct GNUNET_TIME_Absolute start_time;
456 * The query that this request is for.
458 GNUNET_HashCode query;
461 * The task responsible for transmitting queries
464 GNUNET_SCHEDULER_TaskIdentifier task;
467 * (Interned) Peer identifier that identifies a preferred target
470 GNUNET_PEER_Id target_pid;
473 * (Interned) Peer identifiers of peers that have already
474 * received our query for this content.
476 GNUNET_PEER_Id *used_pids;
479 * Our entry in the queue (non-NULL while we wait for our
480 * turn to interact with the local database).
482 struct GNUNET_DATASTORE_QueueEntry *qe;
485 * Size of the 'bf' (in bytes).
490 * Desired anonymity level; only valid for requests from a local client.
492 uint32_t anonymity_level;
495 * How many entries in "used_pids" are actually valid?
497 unsigned int used_pids_off;
500 * How long is the "used_pids" array?
502 unsigned int used_pids_size;
505 * Number of results found for this request.
507 unsigned int results_found;
510 * How many entries in "replies_seen" are actually valid?
512 unsigned int replies_seen_off;
515 * How long is the "replies_seen" array?
517 unsigned int replies_seen_size;
520 * Priority with which this request was made. If one of our clients
521 * made the request, then this is the current priority that we are
522 * using when initiating the request. This value is used when
523 * we decide to reward other peers with trust for providing a reply.
528 * Priority points left for us to spend when forwarding this request
531 uint32_t remaining_priority;
534 * Number to mingle hashes for bloom-filter tests with.
539 * TTL with which we saw this request (or, if we initiated, TTL that
540 * we used for the request).
545 * Type of the content that this request is for.
547 enum GNUNET_BLOCK_Type type;
550 * Remove this request after transmission of the current response.
555 * GNUNET_YES if we should not forward this request to other peers.
565 static struct GNUNET_SCHEDULER_Handle *sched;
570 static const struct GNUNET_CONFIGURATION_Handle *cfg;
573 * Map of peer identifiers to "struct ConnectedPeer" (for that peer).
575 static struct GNUNET_CONTAINER_MultiHashMap *connected_peers;
578 * Map of peer identifiers to "struct PendingRequest" (for that peer).
580 static struct GNUNET_CONTAINER_MultiHashMap *peer_request_map;
583 * Map of query identifiers to "struct PendingRequest" (for that query).
585 static struct GNUNET_CONTAINER_MultiHashMap *query_request_map;
588 * Heap with the request that will expire next at the top. Contains
589 * pointers of type "struct PendingRequest*"; these will *also* be
590 * aliased from the "requests_by_peer" data structures and the
591 * "requests_by_query" table. Note that requests from our clients
592 * don't expire and are thus NOT in the "requests_by_expiration"
593 * (or the "requests_by_peer" tables).
595 static struct GNUNET_CONTAINER_Heap *requests_by_expiration_heap;
598 * Handle for reporting statistics.
600 static struct GNUNET_STATISTICS_Handle *stats;
603 * Linked list of clients we are currently processing requests for.
605 static struct ClientList *client_list;
608 * Pointer to handle to the core service (points to NULL until we've
611 static struct GNUNET_CORE_Handle *core;
614 * Are we allowed to migrate content to this peer.
616 static int active_migration;
618 /* ******************* clean up functions ************************ */
622 * We're done with a particular message list entry.
623 * Free all associated resources.
625 * @param pml entry to destroy
628 destroy_pending_message_list_entry (struct PendingMessageList *pml)
630 GNUNET_CONTAINER_DLL_remove (pml->req->pending_head,
631 pml->req->pending_tail,
633 GNUNET_CONTAINER_DLL_remove (pml->target->pending_messages_head,
634 pml->target->pending_messages_tail,
636 pml->target->pending_requests--;
637 GNUNET_free (pml->pm);
643 * Destroy the given pending message (and call the respective
646 * @param pm message to destroy
647 * @param tpid id of peer that the message was delivered to, or 0 for none
650 destroy_pending_message (struct PendingMessage *pm,
653 struct PendingMessageList *pml = pm->pml;
654 TransmissionContinuation cont;
657 GNUNET_assert (pml->pm == pm);
658 GNUNET_assert ( (tpid == 0) || (tpid == pml->target->pid) );
660 cont_cls = pm->cont_cls;
661 destroy_pending_message_list_entry (pml);
662 cont (cont_cls, tpid);
667 * We're done processing a particular request.
668 * Free all associated resources.
670 * @param pr request to destroy
673 destroy_pending_request (struct PendingRequest *pr)
675 struct GNUNET_PeerIdentity pid;
677 if (pr->hnode != NULL)
679 GNUNET_CONTAINER_heap_remove_node (requests_by_expiration_heap,
683 if (NULL == pr->client_request_list)
685 GNUNET_STATISTICS_update (stats,
686 gettext_noop ("# P2P searches active"),
692 GNUNET_STATISTICS_update (stats,
693 gettext_noop ("# client searches active"),
697 /* might have already been removed from map in 'process_reply' (if
698 there was a unique reply) or never inserted if it was a
699 duplicate; hence ignore the return value here */
700 (void) GNUNET_CONTAINER_multihashmap_remove (query_request_map,
705 GNUNET_DATASTORE_cancel (pr->qe);
708 if (pr->client_request_list != NULL)
710 GNUNET_CONTAINER_DLL_remove (pr->client_request_list->client_list->rl_head,
711 pr->client_request_list->client_list->rl_tail,
712 pr->client_request_list);
713 GNUNET_free (pr->client_request_list);
714 pr->client_request_list = NULL;
718 GNUNET_PEER_resolve (pr->cp->pid,
720 (void) GNUNET_CONTAINER_multihashmap_remove (peer_request_map,
727 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
732 GNUNET_CORE_peer_change_preference_cancel (pr->irc);
735 if (pr->replies_seen != NULL)
737 GNUNET_free (pr->replies_seen);
738 pr->replies_seen = NULL;
740 if (pr->task != GNUNET_SCHEDULER_NO_TASK)
742 GNUNET_SCHEDULER_cancel (sched,
744 pr->task = GNUNET_SCHEDULER_NO_TASK;
746 while (NULL != pr->pending_head)
747 destroy_pending_message_list_entry (pr->pending_head);
748 GNUNET_PEER_change_rc (pr->target_pid, -1);
749 if (pr->used_pids != NULL)
751 GNUNET_PEER_decrement_rcs (pr->used_pids, pr->used_pids_off);
752 GNUNET_free (pr->used_pids);
753 pr->used_pids_off = 0;
754 pr->used_pids_size = 0;
755 pr->used_pids = NULL;
762 * Method called whenever a given peer connects.
764 * @param cls closure, not used
765 * @param peer peer identity this notification is about
766 * @param latency reported latency of the connection with 'other'
767 * @param distance reported distance (DV) to 'other'
770 peer_connect_handler (void *cls,
772 GNUNET_PeerIdentity * peer,
773 struct GNUNET_TIME_Relative latency,
776 struct ConnectedPeer *cp;
778 cp = GNUNET_malloc (sizeof (struct ConnectedPeer));
779 cp->pid = GNUNET_PEER_intern (peer);
780 GNUNET_break (GNUNET_OK ==
781 GNUNET_CONTAINER_multihashmap_put (connected_peers,
784 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
789 * Free (each) request made by the peer.
791 * @param cls closure, points to peer that the request belongs to
792 * @param key current key code
793 * @param value value in the hash map
794 * @return GNUNET_YES (we should continue to iterate)
797 destroy_request (void *cls,
798 const GNUNET_HashCode * key,
801 const struct GNUNET_PeerIdentity * peer = cls;
802 struct PendingRequest *pr = value;
804 GNUNET_break (GNUNET_YES ==
805 GNUNET_CONTAINER_multihashmap_remove (peer_request_map,
808 destroy_pending_request (pr);
814 * Method called whenever a peer disconnects.
816 * @param cls closure, not used
817 * @param peer peer identity this notification is about
820 peer_disconnect_handler (void *cls,
822 GNUNET_PeerIdentity * peer)
824 struct ConnectedPeer *cp;
825 struct PendingMessage *pm;
828 GNUNET_CONTAINER_multihashmap_get_multiple (peer_request_map,
832 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
836 for (i=0;i<CS2P_SUCCESS_LIST_SIZE;i++)
838 if (NULL != cp->last_client_replies[i])
840 GNUNET_SERVER_client_drop (cp->last_client_replies[i]);
841 cp->last_client_replies[i] = NULL;
844 GNUNET_break (GNUNET_YES ==
845 GNUNET_CONTAINER_multihashmap_remove (connected_peers,
848 GNUNET_PEER_change_rc (cp->pid, -1);
849 GNUNET_PEER_decrement_rcs (cp->last_p2p_replies, P2P_SUCCESS_LIST_SIZE);
851 GNUNET_CORE_notify_transmit_ready_cancel (cp->cth);
852 while (NULL != (pm = cp->pending_messages_head))
853 destroy_pending_message (pm, 0 /* delivery failed */);
854 GNUNET_break (0 == cp->pending_requests);
860 * Iterator over hash map entries that removes all occurences
861 * of the given 'client' from the 'last_client_replies' of the
862 * given connected peer.
864 * @param cls closure, the 'struct GNUNET_SERVER_Client*' to remove
865 * @param key current key code (unused)
866 * @param value value in the hash map (the 'struct ConnectedPeer*' to change)
867 * @return GNUNET_YES (we should continue to iterate)
870 remove_client_from_last_client_replies (void *cls,
871 const GNUNET_HashCode * key,
874 struct GNUNET_SERVER_Client *client = cls;
875 struct ConnectedPeer *cp = value;
878 for (i=0;i<CS2P_SUCCESS_LIST_SIZE;i++)
880 if (cp->last_client_replies[i] == client)
882 GNUNET_SERVER_client_drop (cp->last_client_replies[i]);
883 cp->last_client_replies[i] = NULL;
891 * A client disconnected. Remove all of its pending queries.
893 * @param cls closure, NULL
894 * @param client identification of the client
897 handle_client_disconnect (void *cls,
898 struct GNUNET_SERVER_Client
901 struct ClientList *pos;
902 struct ClientList *prev;
903 struct ClientRequestList *rcl;
904 struct ClientResponseMessage *creply;
910 while ( (NULL != pos) &&
911 (pos->client != client) )
917 return; /* no requests pending for this client */
918 while (NULL != (rcl = pos->rl_head))
920 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
921 "Destroying pending request `%s' on disconnect\n",
922 GNUNET_h2s (&rcl->req->query));
923 destroy_pending_request (rcl->req);
926 client_list = pos->next;
928 prev->next = pos->next;
931 GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th);
934 while (NULL != (creply = pos->res_head))
936 GNUNET_CONTAINER_DLL_remove (pos->res_head,
939 GNUNET_free (creply);
941 GNUNET_SERVER_client_drop (pos->client);
943 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
944 &remove_client_from_last_client_replies,
950 * Iterator to free peer entries.
952 * @param cls closure, unused
953 * @param key current key code
954 * @param value value in the hash map (peer entry)
955 * @return GNUNET_YES (we should continue to iterate)
958 clean_peer (void *cls,
959 const GNUNET_HashCode * key,
962 peer_disconnect_handler (NULL, (const struct GNUNET_PeerIdentity*) key);
968 * Task run during shutdown.
974 shutdown_task (void *cls,
975 const struct GNUNET_SCHEDULER_TaskContext *tc)
977 while (client_list != NULL)
978 handle_client_disconnect (NULL,
979 client_list->client);
980 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
983 GNUNET_break (0 == GNUNET_CONTAINER_heap_get_size (requests_by_expiration_heap));
984 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
985 requests_by_expiration_heap = 0;
986 GNUNET_CONTAINER_multihashmap_destroy (connected_peers);
987 connected_peers = NULL;
988 GNUNET_break (0 == GNUNET_CONTAINER_multihashmap_size (query_request_map));
989 GNUNET_CONTAINER_multihashmap_destroy (query_request_map);
990 query_request_map = NULL;
991 GNUNET_break (0 == GNUNET_CONTAINER_multihashmap_size (peer_request_map));
992 GNUNET_CONTAINER_multihashmap_destroy (peer_request_map);
993 peer_request_map = NULL;
994 GNUNET_assert (NULL != core);
995 GNUNET_CORE_disconnect (core);
999 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1002 GNUNET_DATASTORE_disconnect (dsh,
1010 /* ******************* Utility functions ******************** */
1014 * Transmit the given message by copying it to the target buffer
1015 * "buf". "buf" will be NULL and "size" zero if the socket was closed
1016 * for writing in the meantime. In that case, do nothing
1017 * (the disconnect or shutdown handler will take care of the rest).
1018 * If we were able to transmit messages and there are still more
1019 * pending, ask core again for further calls to this function.
1021 * @param cls closure, pointer to the 'struct ConnectedPeer*'
1022 * @param size number of bytes available in buf
1023 * @param buf where the callee should write the message
1024 * @return number of bytes written to buf
1027 transmit_to_peer (void *cls,
1028 size_t size, void *buf)
1030 struct ConnectedPeer *cp = cls;
1032 struct GNUNET_PeerIdentity pid;
1033 struct PendingMessage *pm;
1040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1041 "Dropping message, core too busy.\n");
1046 while ( (NULL != (pm = cp->pending_messages_head) ) &&
1047 (pm->msize <= size) )
1049 memcpy (&cbuf[msize], &pm[1], pm->msize);
1052 destroy_pending_message (pm, cp->pid);
1056 GNUNET_PEER_resolve (cp->pid,
1058 cp->cth = GNUNET_CORE_notify_transmit_ready (core,
1060 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1068 "Transmitting %u bytes to peer %u.\n",
1077 * Add a message to the set of pending messages for the given peer.
1079 * @param cp peer to send message to
1080 * @param pm message to queue
1081 * @param pr request on which behalf this message is being queued
1084 add_to_pending_messages_for_peer (struct ConnectedPeer *cp,
1085 struct PendingMessage *pm,
1086 struct PendingRequest *pr)
1088 struct PendingMessage *pos;
1089 struct PendingMessageList *pml;
1090 struct GNUNET_PeerIdentity pid;
1092 GNUNET_assert (pm->next == NULL);
1093 GNUNET_assert (pm->pml == NULL);
1094 pml = GNUNET_malloc (sizeof (struct PendingMessageList));
1099 GNUNET_CONTAINER_DLL_insert (pr->pending_head,
1102 pos = cp->pending_messages_head;
1103 while ( (pos != NULL) &&
1104 (pm->priority < pos->priority) )
1106 GNUNET_CONTAINER_DLL_insert_after (cp->pending_messages_head,
1107 cp->pending_messages_tail,
1110 cp->pending_requests++;
1111 if (cp->pending_requests > MAX_QUEUE_PER_PEER)
1112 destroy_pending_message (cp->pending_messages_tail, 0);
1113 if (cp->cth == NULL)
1115 /* need to schedule transmission */
1116 GNUNET_PEER_resolve (cp->pid, &pid);
1117 cp->cth = GNUNET_CORE_notify_transmit_ready (core,
1118 cp->pending_messages_head->priority,
1121 cp->pending_messages_head->msize,
1125 if (cp->cth == NULL)
1128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1129 "Failed to schedule transmission with core!\n");
1131 /* FIXME: call stats (rare, bad case) */
1137 * Mingle hash with the mingle_number to produce different bits.
1140 mingle_hash (const GNUNET_HashCode * in,
1141 int32_t mingle_number,
1142 GNUNET_HashCode * hc)
1146 GNUNET_CRYPTO_hash (&mingle_number,
1149 GNUNET_CRYPTO_hash_xor (&m, in, hc);
1154 * Test if the load on this peer is too high
1155 * to even consider processing the query at
1158 * @return GNUNET_YES if the load is too high, GNUNET_NO otherwise
1161 test_load_too_high ()
1163 return GNUNET_NO; // FIXME
1167 /* ******************* Pending Request Refresh Task ******************** */
1172 * We use a random delay to make the timing of requests less
1173 * predictable. This function returns such a random delay. We add a base
1174 * delay of MAX_CORK_DELAY (1s).
1176 * FIXME: make schedule dependent on the specifics of the request?
1177 * Or bandwidth and number of connected peers and load?
1179 * @return random delay to use for some request, between 1s and 1000+TTL_DECREMENT ms
1181 static struct GNUNET_TIME_Relative
1182 get_processing_delay ()
1185 GNUNET_TIME_relative_add (GNUNET_CONSTANTS_MAX_CORK_DELAY,
1186 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1187 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1193 * We're processing a GET request from another peer and have decided
1194 * to forward it to other peers. This function is called periodically
1195 * and should forward the request to other peers until we have all
1196 * possible replies. If we have transmitted the *only* reply to
1197 * the initiator we should destroy the pending request. If we have
1198 * many replies in the queue to the initiator, we should delay sending
1199 * out more queries until the reply queue has shrunk some.
1201 * @param cls our "struct ProcessGetContext *"
1205 forward_request_task (void *cls,
1206 const struct GNUNET_SCHEDULER_TaskContext *tc);
1210 * Function called after we either failed or succeeded
1211 * at transmitting a query to a peer.
1213 * @param cls the requests "struct PendingRequest*"
1214 * @param tpid ID of receiving peer, 0 on transmission error
1217 transmit_query_continuation (void *cls,
1218 GNUNET_PEER_Id tpid)
1220 struct PendingRequest *pr = cls;
1222 GNUNET_STATISTICS_update (stats,
1223 gettext_noop ("# queries scheduled for forwarding"),
1229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1230 "Transmission of request failed, will try again later.\n");
1232 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
1233 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
1234 get_processing_delay (),
1235 &forward_request_task,
1239 GNUNET_STATISTICS_update (stats,
1240 gettext_noop ("# queries forwarded"),
1243 GNUNET_PEER_change_rc (tpid, 1);
1244 if (pr->used_pids_off == pr->used_pids_size)
1245 GNUNET_array_grow (pr->used_pids,
1247 pr->used_pids_size * 2 + 2);
1248 pr->used_pids[pr->used_pids_off++] = tpid;
1249 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
1250 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
1251 get_processing_delay (),
1252 &forward_request_task,
1258 * How many bytes should a bloomfilter be if we have already seen
1259 * entry_count responses? Note that BLOOMFILTER_K gives us the number
1260 * of bits set per entry. Furthermore, we should not re-size the
1261 * filter too often (to keep it cheap).
1263 * Since other peers will also add entries but not resize the filter,
1264 * we should generally pick a slightly larger size than what the
1265 * strict math would suggest.
1267 * @return must be a power of two and smaller or equal to 2^15.
1270 compute_bloomfilter_size (unsigned int entry_count)
1273 unsigned int ideal = (entry_count * BLOOMFILTER_K) / 4;
1274 uint16_t max = 1 << 15;
1276 if (entry_count > max)
1279 while ((size < max) && (size < ideal))
1288 * Recalculate our bloom filter for filtering replies. This function
1289 * will create a new bloom filter from scratch, so it should only be
1290 * called if we have no bloomfilter at all (and hence can create a
1291 * fresh one of minimal size without problems) OR if our peer is the
1292 * initiator (in which case we may resize to larger than mimimum size).
1294 * @param pr request for which the BF is to be recomputed
1297 refresh_bloomfilter (struct PendingRequest *pr)
1301 GNUNET_HashCode mhash;
1303 nsize = compute_bloomfilter_size (pr->replies_seen_off);
1304 if (nsize == pr->bf_size)
1305 return; /* size not changed */
1307 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
1308 pr->bf_size = nsize;
1309 pr->mingle = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, -1);
1310 pr->bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
1313 for (i=0;i<pr->replies_seen_off;i++)
1315 mingle_hash (&pr->replies_seen[i], pr->mingle, &mhash);
1316 GNUNET_CONTAINER_bloomfilter_add (pr->bf, &mhash);
1322 * Function called after we've tried to reserve a certain amount of
1323 * bandwidth for a reply. Check if we succeeded and if so send our
1326 * @param cls the requests "struct PendingRequest*"
1327 * @param peer identifies the peer
1328 * @param bpm_in set to the current bandwidth limit (receiving) for this peer
1329 * @param bpm_out set to the current bandwidth limit (sending) for this peer
1330 * @param amount set to the amount that was actually reserved or unreserved
1331 * @param preference current traffic preference for the given peer
1334 target_reservation_cb (void *cls,
1336 GNUNET_PeerIdentity * peer,
1337 struct GNUNET_BANDWIDTH_Value32NBO bpm_in,
1338 struct GNUNET_BANDWIDTH_Value32NBO bpm_out,
1340 uint64_t preference)
1342 struct PendingRequest *pr = cls;
1343 struct ConnectedPeer *cp;
1344 struct PendingMessage *pm;
1345 struct GetMessage *gm;
1346 GNUNET_HashCode *ext;
1356 /* error in communication with core, try again later */
1357 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
1358 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
1359 get_processing_delay (),
1360 &forward_request_task,
1364 // (3) transmit, update ttl/priority
1365 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
1369 /* Peer must have just left */
1371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1372 "Selected peer disconnected!\n");
1374 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
1375 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
1376 get_processing_delay (),
1377 &forward_request_task,
1381 no_route = GNUNET_NO;
1382 /* FIXME: check against DBLOCK_SIZE and possibly return
1383 amount to reserve; however, this also needs to work
1384 with testcases which currently start out with a far
1385 too low per-peer bw limit, so they would never send
1386 anything. Big issue. */
1392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1393 "Failed to reserve bandwidth for reply (got %d/%u bytes only)!\n",
1397 GNUNET_STATISTICS_update (stats,
1398 gettext_noop ("# reply bandwidth reservation requests failed"),
1401 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
1402 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
1403 get_processing_delay (),
1404 &forward_request_task,
1406 return; /* this target round failed */
1408 /* FIXME: if we are "quite" busy, we may still want to skip
1409 this round; need more load detection code! */
1410 no_route = GNUNET_YES;
1413 GNUNET_STATISTICS_update (stats,
1414 gettext_noop ("# queries scheduled for forwarding"),
1417 /* build message and insert message into priority queue */
1419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1420 "Forwarding request `%s' to `%4s'!\n",
1421 GNUNET_h2s (&pr->query),
1426 if (GNUNET_YES == no_route)
1428 bm |= GET_MESSAGE_BIT_RETURN_TO;
1431 if (pr->namespace != NULL)
1433 bm |= GET_MESSAGE_BIT_SKS_NAMESPACE;
1436 if (pr->target_pid != 0)
1438 bm |= GET_MESSAGE_BIT_TRANSMIT_TO;
1441 msize = sizeof (struct GetMessage) + pr->bf_size + k * sizeof(GNUNET_HashCode);
1442 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1443 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1445 gm = (struct GetMessage*) &pm[1];
1446 gm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_GET);
1447 gm->header.size = htons (msize);
1448 gm->type = htonl (pr->type);
1449 pr->remaining_priority /= 2;
1450 gm->priority = htonl (pr->remaining_priority);
1451 gm->ttl = htonl (pr->ttl);
1452 gm->filter_mutator = htonl(pr->mingle);
1453 gm->hash_bitmap = htonl (bm);
1454 gm->query = pr->query;
1455 ext = (GNUNET_HashCode*) &gm[1];
1457 if (GNUNET_YES == no_route)
1458 GNUNET_PEER_resolve (pr->cp->pid, (struct GNUNET_PeerIdentity*) &ext[k++]);
1459 if (pr->namespace != NULL)
1460 memcpy (&ext[k++], pr->namespace, sizeof (GNUNET_HashCode));
1461 if (pr->target_pid != 0)
1462 GNUNET_PEER_resolve (pr->target_pid, (struct GNUNET_PeerIdentity*) &ext[k++]);
1463 bfdata = (char *) &ext[k];
1465 GNUNET_CONTAINER_bloomfilter_get_raw_data (pr->bf,
1468 pm->cont = &transmit_query_continuation;
1470 add_to_pending_messages_for_peer (cp, pm, pr);
1475 * Closure used for "target_peer_select_cb".
1477 struct PeerSelectionContext
1480 * The request for which we are selecting
1483 struct PendingRequest *pr;
1486 * Current "prime" target.
1488 struct GNUNET_PeerIdentity target;
1491 * How much do we like this target?
1493 double target_score;
1499 * Function called for each connected peer to determine
1500 * which one(s) would make good targets for forwarding.
1502 * @param cls closure (struct PeerSelectionContext)
1503 * @param key current key code (peer identity)
1504 * @param value value in the hash map (struct ConnectedPeer)
1505 * @return GNUNET_YES if we should continue to
1510 target_peer_select_cb (void *cls,
1511 const GNUNET_HashCode * key,
1514 struct PeerSelectionContext *psc = cls;
1515 struct ConnectedPeer *cp = value;
1516 struct PendingRequest *pr = psc->pr;
1521 /* 1) check that this peer is not the initiator */
1523 return GNUNET_YES; /* skip */
1525 /* 2) check if we have already (recently) forwarded to this peer */
1527 for (i=0;i<pr->used_pids_off;i++)
1528 if (pr->used_pids[i] == cp->pid)
1531 if (0 != GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1532 RETRY_PROBABILITY_INV))
1535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1536 "NOT re-trying query that was previously transmitted %u times\n",
1537 (unsigned int) pr->used_pids_off);
1539 return GNUNET_YES; /* skip */
1544 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1545 "Re-trying query that was previously transmitted %u times to this peer\n",
1548 // 3) calculate how much we'd like to forward to this peer
1549 score = 42; // FIXME!
1550 // FIXME: also need API to gather data on responsiveness
1551 // of this peer (we have fields for that in 'cp', but
1552 // they are never set!)
1554 /* store best-fit in closure */
1555 if (score > psc->target_score)
1557 psc->target_score = score;
1558 psc->target.hashPubKey = *key;
1565 * The priority level imposes a bound on the maximum
1566 * value for the ttl that can be requested.
1568 * @param ttl_in requested ttl
1569 * @param prio given priority
1570 * @return ttl_in if ttl_in is below the limit,
1571 * otherwise the ttl-limit for the given priority
1574 bound_ttl (int32_t ttl_in, uint32_t prio)
1576 unsigned long long allowed;
1580 allowed = ((unsigned long long) prio) * TTL_DECREMENT / 1000;
1581 if (ttl_in > allowed)
1583 if (allowed >= (1 << 30))
1592 * We're processing a GET request from another peer and have decided
1593 * to forward it to other peers. This function is called periodically
1594 * and should forward the request to other peers until we have all
1595 * possible replies. If we have transmitted the *only* reply to
1596 * the initiator we should destroy the pending request. If we have
1597 * many replies in the queue to the initiator, we should delay sending
1598 * out more queries until the reply queue has shrunk some.
1600 * @param cls our "struct ProcessGetContext *"
1604 forward_request_task (void *cls,
1605 const struct GNUNET_SCHEDULER_TaskContext *tc)
1607 struct PendingRequest *pr = cls;
1608 struct PeerSelectionContext psc;
1609 struct ConnectedPeer *cp;
1610 struct GNUNET_TIME_Relative delay;
1612 pr->task = GNUNET_SCHEDULER_NO_TASK;
1613 if (pr->irc != NULL)
1616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1617 "Forwarding of query `%s' not attempted due to pending local lookup!\n",
1618 GNUNET_h2s (&pr->query));
1620 return; /* already pending */
1622 if (GNUNET_YES == pr->local_only)
1623 return; /* configured to not do P2P search */
1624 /* (1) select target */
1626 psc.target_score = DBL_MIN;
1627 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
1628 &target_peer_select_cb,
1630 if (psc.target_score == DBL_MIN)
1632 delay = get_processing_delay ();
1634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1635 "No peer selected for forwarding of query `%s', will try again in %llu ms!\n",
1636 GNUNET_h2s (&pr->query),
1639 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
1641 &forward_request_task,
1643 return; /* nobody selected */
1645 /* (3) update TTL/priority */
1647 if (pr->client_request_list != NULL)
1649 /* FIXME: use better algorithm!? */
1650 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1653 /* FIXME: bound priority by "customary" priority used by other peers
1655 pr->ttl = bound_ttl (pr->ttl + TTL_DECREMENT * 2,
1658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1659 "Trying query `%s' with priority %u and TTL %d.\n",
1660 GNUNET_h2s (&pr->query),
1667 /* FIXME: should we do something here as well!? */
1670 /* (3) reserve reply bandwidth */
1671 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
1672 &psc.target.hashPubKey);
1673 GNUNET_assert (NULL != cp);
1674 pr->irc = GNUNET_CORE_peer_change_preference (sched, cfg,
1676 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1677 GNUNET_BANDWIDTH_value_init ((uint32_t) -1 /* no limit */),
1679 (uint64_t) cp->inc_preference,
1680 &target_reservation_cb,
1682 cp->inc_preference = 0.0;
1686 /* **************************** P2P PUT Handling ************************ */
1690 * Function called after we either failed or succeeded
1691 * at transmitting a reply to a peer.
1693 * @param cls the requests "struct PendingRequest*"
1694 * @param tpid ID of receiving peer, 0 on transmission error
1697 transmit_reply_continuation (void *cls,
1698 GNUNET_PEER_Id tpid)
1700 struct PendingRequest *pr = cls;
1704 case GNUNET_BLOCK_TYPE_DBLOCK:
1705 case GNUNET_BLOCK_TYPE_IBLOCK:
1706 /* only one reply expected, done with the request! */
1707 destroy_pending_request (pr);
1709 case GNUNET_BLOCK_TYPE_ANY:
1710 case GNUNET_BLOCK_TYPE_KBLOCK:
1711 case GNUNET_BLOCK_TYPE_SBLOCK:
1721 * Transmit the given message by copying it to the target buffer
1722 * "buf". "buf" will be NULL and "size" zero if the socket was closed
1723 * for writing in the meantime. In that case, do nothing
1724 * (the disconnect or shutdown handler will take care of the rest).
1725 * If we were able to transmit messages and there are still more
1726 * pending, ask core again for further calls to this function.
1728 * @param cls closure, pointer to the 'struct ClientList*'
1729 * @param size number of bytes available in buf
1730 * @param buf where the callee should write the message
1731 * @return number of bytes written to buf
1734 transmit_to_client (void *cls,
1735 size_t size, void *buf)
1737 struct ClientList *cl = cls;
1739 struct ClientResponseMessage *creply;
1746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1747 "Not sending reply, client communication problem.\n");
1752 while ( (NULL != (creply = cl->res_head) ) &&
1753 (creply->msize <= size) )
1755 memcpy (&cbuf[msize], &creply[1], creply->msize);
1756 msize += creply->msize;
1757 size -= creply->msize;
1758 GNUNET_CONTAINER_DLL_remove (cl->res_head,
1761 GNUNET_free (creply);
1764 cl->th = GNUNET_SERVER_notify_transmit_ready (cl->client,
1766 GNUNET_TIME_UNIT_FOREVER_REL,
1767 &transmit_to_client,
1770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1771 "Transmitted %u bytes to client\n",
1772 (unsigned int) msize);
1779 * Closure for "process_reply" function.
1781 struct ProcessReplyClosure
1784 * The data for the reply.
1788 // FIXME: add 'struct ConnectedPeer' to track 'last_xxx_replies' here!
1791 * When the reply expires.
1793 struct GNUNET_TIME_Absolute expiration;
1801 * Namespace that this reply belongs to
1802 * (if it is of type SBLOCK).
1804 GNUNET_HashCode namespace;
1807 * Type of the block.
1809 enum GNUNET_BLOCK_Type type;
1812 * How much was this reply worth to us?
1819 * We have received a reply; handle it!
1821 * @param cls response (struct ProcessReplyClosure)
1822 * @param key our query
1823 * @param value value in the hash map (info about the query)
1824 * @return GNUNET_YES (we should continue to iterate)
1827 process_reply (void *cls,
1828 const GNUNET_HashCode * key,
1831 struct ProcessReplyClosure *prq = cls;
1832 struct PendingRequest *pr = value;
1833 struct PendingMessage *reply;
1834 struct ClientResponseMessage *creply;
1835 struct ClientList *cl;
1836 struct PutMessage *pm;
1837 struct ConnectedPeer *cp;
1838 GNUNET_HashCode chash;
1839 GNUNET_HashCode mhash;
1843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1844 "Matched result (type %u) for query `%s' with pending request\n",
1845 (unsigned int) prq->type,
1848 GNUNET_STATISTICS_update (stats,
1849 gettext_noop ("# replies received and matched"),
1852 GNUNET_CRYPTO_hash (prq->data,
1857 case GNUNET_BLOCK_TYPE_DBLOCK:
1858 case GNUNET_BLOCK_TYPE_IBLOCK:
1859 /* only possible reply, stop requesting! */
1860 while (NULL != pr->pending_head)
1861 destroy_pending_message_list_entry (pr->pending_head);
1864 if (pr->client_request_list != NULL)
1865 GNUNET_SERVER_receive_done (pr->client_request_list->client_list->client,
1867 GNUNET_DATASTORE_cancel (pr->qe);
1870 pr->do_remove = GNUNET_YES;
1871 if (pr->task != GNUNET_SCHEDULER_NO_TASK)
1873 GNUNET_SCHEDULER_cancel (sched,
1875 pr->task = GNUNET_SCHEDULER_NO_TASK;
1877 GNUNET_break (GNUNET_YES ==
1878 GNUNET_CONTAINER_multihashmap_remove (query_request_map,
1882 case GNUNET_BLOCK_TYPE_SBLOCK:
1883 if (pr->namespace == NULL)
1888 if (0 != memcmp (pr->namespace,
1890 sizeof (GNUNET_HashCode)))
1892 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1893 _("Reply mismatched in terms of namespace. Discarded.\n"));
1894 return GNUNET_YES; /* wrong namespace */
1896 /* then: fall-through! */
1897 case GNUNET_BLOCK_TYPE_KBLOCK:
1898 case GNUNET_BLOCK_TYPE_NBLOCK:
1901 mingle_hash (&chash, pr->mingle, &mhash);
1902 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (pr->bf,
1905 GNUNET_STATISTICS_update (stats,
1906 gettext_noop ("# duplicate replies discarded (bloomfilter)"),
1910 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1911 "Duplicate response `%s', discarding.\n",
1912 GNUNET_h2s (&mhash));
1914 return GNUNET_YES; /* duplicate */
1917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1918 "New response `%s', adding to filter.\n",
1919 GNUNET_h2s (&mhash));
1922 if (pr->client_request_list != NULL)
1924 if (pr->replies_seen_size == pr->replies_seen_off)
1925 GNUNET_array_grow (pr->replies_seen,
1926 pr->replies_seen_size,
1927 pr->replies_seen_size * 2 + 4);
1928 pr->replies_seen[pr->replies_seen_off++] = chash;
1930 if ( (pr->bf == NULL) ||
1931 (pr->client_request_list != NULL) )
1932 refresh_bloomfilter (pr);
1933 GNUNET_CONTAINER_bloomfilter_add (pr->bf,
1940 prq->priority += pr->remaining_priority;
1941 pr->remaining_priority = 0;
1942 if (pr->client_request_list != NULL)
1944 GNUNET_STATISTICS_update (stats,
1945 gettext_noop ("# replies received for local clients"),
1948 cl = pr->client_request_list->client_list;
1949 msize = sizeof (struct PutMessage) + prq->size;
1950 creply = GNUNET_malloc (msize + sizeof (struct ClientResponseMessage));
1951 creply->msize = msize;
1952 creply->client_list = cl;
1953 GNUNET_CONTAINER_DLL_insert_after (cl->res_head,
1957 pm = (struct PutMessage*) &creply[1];
1958 pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
1959 pm->header.size = htons (msize);
1960 pm->type = htonl (prq->type);
1961 pm->expiration = GNUNET_TIME_absolute_hton (prq->expiration);
1962 memcpy (&pm[1], prq->data, prq->size);
1966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1967 "Transmitting result for query `%s' to client\n",
1970 cl->th = GNUNET_SERVER_notify_transmit_ready (cl->client,
1972 GNUNET_TIME_UNIT_FOREVER_REL,
1973 &transmit_to_client,
1976 GNUNET_break (cl->th != NULL);
1978 destroy_pending_request (pr);
1984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1985 "Transmitting result for query `%s' to other peer (PID=%u)\n",
1987 (unsigned int) cp->pid);
1989 GNUNET_STATISTICS_update (stats,
1990 gettext_noop ("# replies received for other peers"),
1993 msize = sizeof (struct PutMessage) + prq->size;
1994 reply = GNUNET_malloc (msize + sizeof (struct PendingMessage));
1995 reply->cont = &transmit_reply_continuation;
1996 reply->cont_cls = pr;
1997 reply->msize = msize;
1998 reply->priority = (uint32_t) -1; /* send replies first! */
1999 pm = (struct PutMessage*) &reply[1];
2000 pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
2001 pm->header.size = htons (msize);
2002 pm->type = htonl (prq->type);
2003 pm->expiration = GNUNET_TIME_absolute_hton (prq->expiration);
2004 memcpy (&pm[1], prq->data, prq->size);
2005 add_to_pending_messages_for_peer (cp, reply, pr);
2007 // FIXME: implement hot-path routing statistics keeping!
2014 * Continuation called to notify client about result of the
2017 * @param cls closure
2018 * @param success GNUNET_SYSERR on failure
2019 * @param msg NULL on success, otherwise an error message
2022 put_migration_continuation (void *cls,
2031 * Handle P2P "PUT" message.
2033 * @param cls closure, always NULL
2034 * @param other the other peer involved (sender or receiver, NULL
2035 * for loopback messages where we are both sender and receiver)
2036 * @param message the actual message
2037 * @param latency reported latency of the connection with 'other'
2038 * @param distance reported distance (DV) to 'other'
2039 * @return GNUNET_OK to keep the connection open,
2040 * GNUNET_SYSERR to close it (signal serious error)
2043 handle_p2p_put (void *cls,
2044 const struct GNUNET_PeerIdentity *other,
2045 const struct GNUNET_MessageHeader *message,
2046 struct GNUNET_TIME_Relative latency,
2049 const struct PutMessage *put;
2052 enum GNUNET_BLOCK_Type type;
2053 struct GNUNET_TIME_Absolute expiration;
2054 GNUNET_HashCode query;
2055 struct ProcessReplyClosure prq;
2056 const struct SBlock *sb;
2058 msize = ntohs (message->size);
2059 if (msize < sizeof (struct PutMessage))
2062 return GNUNET_SYSERR;
2064 put = (const struct PutMessage*) message;
2065 dsize = msize - sizeof (struct PutMessage);
2066 type = ntohl (put->type);
2067 expiration = GNUNET_TIME_absolute_ntoh (put->expiration);
2070 GNUNET_BLOCK_check_block (type,
2075 GNUNET_break_op (0);
2076 return GNUNET_SYSERR;
2078 if (type == GNUNET_BLOCK_TYPE_ONDEMAND)
2079 return GNUNET_SYSERR;
2080 if (GNUNET_BLOCK_TYPE_SBLOCK == type)
2082 sb = (const struct SBlock*) &put[1];
2083 GNUNET_CRYPTO_hash (&sb->subspace,
2084 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2090 "Received result for query `%s' from peer `%4s'\n",
2091 GNUNET_h2s (&query),
2092 GNUNET_i2s (other));
2094 GNUNET_STATISTICS_update (stats,
2095 gettext_noop ("# replies received (overall)"),
2098 /* now, lookup 'query' */
2099 prq.data = (const void*) &put[1];
2102 prq.expiration = expiration;
2104 GNUNET_CONTAINER_multihashmap_get_multiple (query_request_map,
2108 if (GNUNET_YES == active_migration)
2110 GNUNET_DATASTORE_put (dsh,
2111 0, &query, dsize, &put[1],
2112 type, prq.priority, 1 /* anonymity */,
2114 1 + prq.priority, MAX_DATASTORE_QUEUE,
2115 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
2116 &put_migration_continuation,
2123 /* **************************** P2P GET Handling ************************ */
2127 * Closure for 'check_duplicate_request_{peer,client}'.
2129 struct CheckDuplicateRequestClosure
2132 * The new request we should check if it already exists.
2134 const struct PendingRequest *pr;
2137 * Existing request found by the checker, NULL if none.
2139 struct PendingRequest *have;
2144 * Iterator over entries in the 'query_request_map' that
2145 * tries to see if we have the same request pending from
2146 * the same client already.
2148 * @param cls closure (our 'struct CheckDuplicateRequestClosure')
2149 * @param key current key code (query, ignored, must match)
2150 * @param value value in the hash map (a 'struct PendingRequest'
2151 * that already exists)
2152 * @return GNUNET_YES if we should continue to
2153 * iterate (no match yet)
2154 * GNUNET_NO if not (match found).
2157 check_duplicate_request_client (void *cls,
2158 const GNUNET_HashCode * key,
2161 struct CheckDuplicateRequestClosure *cdc = cls;
2162 struct PendingRequest *have = value;
2164 if (have->client_request_list == NULL)
2166 if ( (cdc->pr->client_request_list->client_list->client == have->client_request_list->client_list->client) &&
2177 * We're processing (local) results for a search request
2178 * from another peer. Pass applicable results to the
2179 * peer and if we are done either clean up (operation
2180 * complete) or forward to other peers (more results possible).
2182 * @param cls our closure (struct LocalGetContext)
2183 * @param key key for the content
2184 * @param size number of bytes in data
2185 * @param data content stored
2186 * @param type type of the content
2187 * @param priority priority of the content
2188 * @param anonymity anonymity-level for the content
2189 * @param expiration expiration time for the content
2190 * @param uid unique identifier for the datum;
2191 * maybe 0 if no unique identifier is available
2194 process_local_reply (void *cls,
2195 const GNUNET_HashCode * key,
2198 enum GNUNET_BLOCK_Type type,
2201 struct GNUNET_TIME_Absolute
2205 struct PendingRequest *pr = cls;
2206 struct ProcessReplyClosure prq;
2207 struct CheckDuplicateRequestClosure cdrc;
2208 const struct SBlock *sb;
2209 GNUNET_HashCode dhash;
2210 GNUNET_HashCode mhash;
2211 GNUNET_HashCode query;
2216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2217 "Done processing local replies, forwarding request to other peers.\n");
2220 if (pr->client_request_list != NULL)
2222 GNUNET_SERVER_receive_done (pr->client_request_list->client_list->client,
2224 /* Figure out if this is a duplicate request and possibly
2225 merge 'struct PendingRequest' entries */
2228 GNUNET_CONTAINER_multihashmap_get_multiple (query_request_map,
2230 &check_duplicate_request_client,
2232 if (cdrc.have != NULL)
2235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2236 "Received request for block `%s' twice from client, will only request once.\n",
2237 GNUNET_h2s (&pr->query));
2240 destroy_pending_request (pr);
2245 /* no more results */
2246 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
2247 pr->task = GNUNET_SCHEDULER_add_now (sched,
2248 &forward_request_task,
2253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2254 "New local response to `%s' of type %u.\n",
2258 if (type == GNUNET_BLOCK_TYPE_ONDEMAND)
2261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2262 "Found ONDEMAND block, performing on-demand encoding\n");
2264 GNUNET_STATISTICS_update (stats,
2265 gettext_noop ("# on-demand blocks matched requests"),
2269 GNUNET_FS_handle_on_demand_block (key, size, data, type, priority,
2270 anonymity, expiration, uid,
2271 &process_local_reply,
2274 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
2277 /* check for duplicates */
2278 GNUNET_CRYPTO_hash (data, size, &dhash);
2279 mingle_hash (&dhash,
2282 if ( (pr->bf != NULL) &&
2284 GNUNET_CONTAINER_bloomfilter_test (pr->bf,
2288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2289 "Result from datastore filtered by bloomfilter (duplicate).\n");
2291 GNUNET_STATISTICS_update (stats,
2292 gettext_noop ("# results filtered by query bloomfilter"),
2296 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
2300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2301 "Found result for query `%s' in local datastore\n",
2304 GNUNET_STATISTICS_update (stats,
2305 gettext_noop ("# results found locally"),
2308 pr->results_found++;
2309 memset (&prq, 0, sizeof (prq));
2311 prq.expiration = expiration;
2313 if (GNUNET_BLOCK_TYPE_SBLOCK == type)
2315 sb = (const struct SBlock*) data;
2316 GNUNET_CRYPTO_hash (&sb->subspace,
2317 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2320 if (GNUNET_OK != GNUNET_BLOCK_check_block (type,
2326 GNUNET_DATASTORE_remove (dsh,
2330 GNUNET_TIME_UNIT_FOREVER_REL,
2332 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
2336 prq.priority = priority;
2337 process_reply (&prq, key, pr);
2339 if ( (type == GNUNET_BLOCK_TYPE_DBLOCK) ||
2340 (type == GNUNET_BLOCK_TYPE_IBLOCK) )
2343 GNUNET_DATASTORE_get_next (dsh, GNUNET_NO);
2346 if ( (pr->client_request_list == NULL) &&
2347 ( (GNUNET_YES == test_load_too_high()) ||
2348 (pr->results_found > 5 + 2 * pr->priority) ) )
2351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2352 "Load too high, done with request\n");
2354 GNUNET_STATISTICS_update (stats,
2355 gettext_noop ("# processing result set cut short due to load"),
2359 GNUNET_DATASTORE_get_next (dsh, GNUNET_NO);
2363 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
2368 * We've received a request with the specified priority. Bound it
2369 * according to how much we trust the given peer.
2371 * @param prio_in requested priority
2372 * @param cp the peer making the request
2373 * @return effective priority
2376 bound_priority (uint32_t prio_in,
2377 struct ConnectedPeer *cp)
2384 * Iterator over entries in the 'query_request_map' that
2385 * tries to see if we have the same request pending from
2386 * the same peer already.
2388 * @param cls closure (our 'struct CheckDuplicateRequestClosure')
2389 * @param key current key code (query, ignored, must match)
2390 * @param value value in the hash map (a 'struct PendingRequest'
2391 * that already exists)
2392 * @return GNUNET_YES if we should continue to
2393 * iterate (no match yet)
2394 * GNUNET_NO if not (match found).
2397 check_duplicate_request_peer (void *cls,
2398 const GNUNET_HashCode * key,
2401 struct CheckDuplicateRequestClosure *cdc = cls;
2402 struct PendingRequest *have = value;
2404 if (cdc->pr->target_pid == have->target_pid)
2414 * Handle P2P "GET" request.
2416 * @param cls closure, always NULL
2417 * @param other the other peer involved (sender or receiver, NULL
2418 * for loopback messages where we are both sender and receiver)
2419 * @param message the actual message
2420 * @param latency reported latency of the connection with 'other'
2421 * @param distance reported distance (DV) to 'other'
2422 * @return GNUNET_OK to keep the connection open,
2423 * GNUNET_SYSERR to close it (signal serious error)
2426 handle_p2p_get (void *cls,
2427 const struct GNUNET_PeerIdentity *other,
2428 const struct GNUNET_MessageHeader *message,
2429 struct GNUNET_TIME_Relative latency,
2432 struct PendingRequest *pr;
2433 struct ConnectedPeer *cp;
2434 struct ConnectedPeer *cps;
2435 struct CheckDuplicateRequestClosure cdc;
2436 struct GNUNET_TIME_Relative timeout;
2438 const struct GetMessage *gm;
2440 const GNUNET_HashCode *opt;
2443 uint32_t ttl_decrement;
2444 enum GNUNET_BLOCK_Type type;
2448 msize = ntohs(message->size);
2449 if (msize < sizeof (struct GetMessage))
2451 GNUNET_break_op (0);
2452 return GNUNET_SYSERR;
2454 gm = (const struct GetMessage*) message;
2455 type = ntohl (gm->type);
2458 case GNUNET_BLOCK_TYPE_ANY:
2459 case GNUNET_BLOCK_TYPE_DBLOCK:
2460 case GNUNET_BLOCK_TYPE_IBLOCK:
2461 case GNUNET_BLOCK_TYPE_KBLOCK:
2462 case GNUNET_BLOCK_TYPE_SBLOCK:
2465 GNUNET_break_op (0);
2466 return GNUNET_SYSERR;
2468 bm = ntohl (gm->hash_bitmap);
2476 if (msize < sizeof (struct GetMessage) + bits * sizeof (GNUNET_HashCode))
2478 GNUNET_break_op (0);
2479 return GNUNET_SYSERR;
2481 opt = (const GNUNET_HashCode*) &gm[1];
2482 bfsize = msize - sizeof (struct GetMessage) + bits * sizeof (GNUNET_HashCode);
2483 bm = ntohl (gm->hash_bitmap);
2484 if ( (0 != (bm & GET_MESSAGE_BIT_SKS_NAMESPACE)) &&
2485 (type != GNUNET_BLOCK_TYPE_SBLOCK) )
2487 GNUNET_break_op (0);
2488 return GNUNET_SYSERR;
2491 cps = GNUNET_CONTAINER_multihashmap_get (connected_peers,
2492 &other->hashPubKey);
2495 /* peer must have just disconnected */
2496 GNUNET_STATISTICS_update (stats,
2497 gettext_noop ("# requests dropped due to initiator not being connected"),
2500 return GNUNET_SYSERR;
2502 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
2503 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
2510 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
2511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2512 "Failed to find RETURN-TO peer `%4s' in connection set. Dropping query.\n",
2513 GNUNET_i2s ((const struct GNUNET_PeerIdentity*) &opt[bits-1]));
2516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2517 "Failed to find peer `%4s' in connection set. Dropping query.\n",
2518 GNUNET_i2s (other));
2520 GNUNET_STATISTICS_update (stats,
2521 gettext_noop ("# requests dropped due to missing reverse route"),
2524 /* FIXME: try connect? */
2527 /* note that we can really only check load here since otherwise
2528 peers could find out that we are overloaded by not being
2529 disconnected after sending us a malformed query... */
2530 if (GNUNET_YES == test_load_too_high ())
2533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2534 "Dropping query from `%s', this peer is too busy.\n",
2535 GNUNET_i2s (other));
2537 GNUNET_STATISTICS_update (stats,
2538 gettext_noop ("# requests dropped due to high load"),
2545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2546 "Received request for `%s' of type %u from peer `%4s' with flags %u\n",
2547 GNUNET_h2s (&gm->query),
2548 (unsigned int) type,
2552 have_ns = (0 != (bm & GET_MESSAGE_BIT_SKS_NAMESPACE));
2553 pr = GNUNET_malloc (sizeof (struct PendingRequest) +
2554 (have_ns ? sizeof(GNUNET_HashCode) : 0));
2556 pr->namespace = (GNUNET_HashCode*) &pr[1];
2558 pr->mingle = ntohl (gm->filter_mutator);
2559 if (0 != (bm & GET_MESSAGE_BIT_SKS_NAMESPACE))
2560 memcpy (&pr[1], &opt[bits++], sizeof (GNUNET_HashCode));
2561 if (0 != (bm & GET_MESSAGE_BIT_TRANSMIT_TO))
2562 pr->target_pid = GNUNET_PEER_intern ((const struct GNUNET_PeerIdentity*) &opt[bits++]);
2564 pr->anonymity_level = 1;
2565 pr->priority = bound_priority (ntohl (gm->priority), cps);
2566 pr->ttl = bound_ttl (ntohl (gm->ttl), pr->priority);
2567 pr->query = gm->query;
2568 /* decrement ttl (always) */
2569 ttl_decrement = 2 * TTL_DECREMENT +
2570 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2572 if ( (pr->ttl < 0) &&
2573 (((int32_t)(pr->ttl - ttl_decrement)) > 0) )
2576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2577 "Dropping query from `%s' due to TTL underflow (%d - %u).\n",
2582 GNUNET_STATISTICS_update (stats,
2583 gettext_noop ("# requests dropped due TTL underflow"),
2586 /* integer underflow => drop (should be very rare)! */
2590 pr->ttl -= ttl_decrement;
2591 pr->start_time = GNUNET_TIME_absolute_get ();
2593 /* get bloom filter */
2596 pr->bf = GNUNET_CONTAINER_bloomfilter_init ((const char*) &opt[bits],
2599 pr->bf_size = bfsize;
2604 GNUNET_CONTAINER_multihashmap_get_multiple (query_request_map,
2606 &check_duplicate_request_peer,
2608 if (cdc.have != NULL)
2610 if (cdc.have->start_time.value + cdc.have->ttl >=
2611 pr->start_time.value + pr->ttl)
2613 /* existing request has higher TTL, drop new one! */
2614 cdc.have->priority += pr->priority;
2615 destroy_pending_request (pr);
2617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2618 "Have existing request with higher TTL, dropping new request.\n",
2619 GNUNET_i2s (other));
2621 GNUNET_STATISTICS_update (stats,
2622 gettext_noop ("# requests dropped due to higher-TTL request"),
2629 /* existing request has lower TTL, drop old one! */
2630 pr->priority += cdc.have->priority;
2631 /* Possible optimization: if we have applicable pending
2632 replies in 'cdc.have', we might want to move those over
2633 (this is a really rare special-case, so it is not clear
2634 that this would be worth it) */
2635 destroy_pending_request (cdc.have);
2636 /* keep processing 'pr'! */
2641 GNUNET_break (GNUNET_OK ==
2642 GNUNET_CONTAINER_multihashmap_put (query_request_map,
2645 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
2646 GNUNET_break (GNUNET_OK ==
2647 GNUNET_CONTAINER_multihashmap_put (peer_request_map,
2650 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
2652 pr->hnode = GNUNET_CONTAINER_heap_insert (requests_by_expiration_heap,
2654 pr->start_time.value + pr->ttl);
2656 GNUNET_STATISTICS_update (stats,
2657 gettext_noop ("# P2P searches received"),
2660 GNUNET_STATISTICS_update (stats,
2661 gettext_noop ("# P2P searches active"),
2665 /* calculate change in traffic preference */
2666 preference = (double) pr->priority;
2667 if (preference < QUERY_BANDWIDTH_VALUE)
2668 preference = QUERY_BANDWIDTH_VALUE;
2669 cps->inc_preference += preference;
2671 /* process locally */
2672 if (type == GNUNET_BLOCK_TYPE_DBLOCK)
2673 type = GNUNET_BLOCK_TYPE_ANY; /* to get on-demand as well */
2674 timeout = GNUNET_TIME_relative_multiply (BASIC_DATASTORE_REQUEST_DELAY,
2675 (pr->priority + 1));
2676 pr->qe = GNUNET_DATASTORE_get (dsh,
2680 MAX_DATASTORE_QUEUE,
2682 &process_local_reply,
2685 /* Are multiple results possible? If so, start processing remotely now! */
2688 case GNUNET_BLOCK_TYPE_DBLOCK:
2689 case GNUNET_BLOCK_TYPE_IBLOCK:
2690 /* only one result, wait for datastore */
2693 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
2694 pr->task = GNUNET_SCHEDULER_add_now (sched,
2695 &forward_request_task,
2699 /* make sure we don't track too many requests */
2700 if (GNUNET_CONTAINER_heap_get_size (requests_by_expiration_heap) > max_pending_requests)
2702 pr = GNUNET_CONTAINER_heap_peek (requests_by_expiration_heap);
2703 destroy_pending_request (pr);
2709 /* **************************** CS GET Handling ************************ */
2713 * Handle START_SEARCH-message (search request from client).
2715 * @param cls closure
2716 * @param client identification of the client
2717 * @param message the actual message
2720 handle_start_search (void *cls,
2721 struct GNUNET_SERVER_Client *client,
2722 const struct GNUNET_MessageHeader *message)
2724 static GNUNET_HashCode all_zeros;
2725 const struct SearchMessage *sm;
2726 struct ClientList *cl;
2727 struct ClientRequestList *crl;
2728 struct PendingRequest *pr;
2731 enum GNUNET_BLOCK_Type type;
2733 msize = ntohs (message->size);
2734 if ( (msize < sizeof (struct SearchMessage)) ||
2735 (0 != (msize - sizeof (struct SearchMessage)) % sizeof (GNUNET_HashCode)) )
2738 GNUNET_SERVER_receive_done (client,
2742 GNUNET_STATISTICS_update (stats,
2743 gettext_noop ("# client searches received"),
2746 sc = (msize - sizeof (struct SearchMessage)) / sizeof (GNUNET_HashCode);
2747 sm = (const struct SearchMessage*) message;
2748 type = ntohl (sm->type);
2750 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2751 "Received request for `%s' of type %u from local client\n",
2752 GNUNET_h2s (&sm->query),
2753 (unsigned int) type);
2757 case GNUNET_BLOCK_TYPE_ANY:
2758 case GNUNET_BLOCK_TYPE_DBLOCK:
2759 case GNUNET_BLOCK_TYPE_IBLOCK:
2760 case GNUNET_BLOCK_TYPE_KBLOCK:
2761 case GNUNET_BLOCK_TYPE_SBLOCK:
2762 case GNUNET_BLOCK_TYPE_NBLOCK:
2766 GNUNET_SERVER_receive_done (client,
2772 while ( (cl != NULL) &&
2773 (cl->client != client) )
2777 cl = GNUNET_malloc (sizeof (struct ClientList));
2778 cl->client = client;
2779 GNUNET_SERVER_client_keep (client);
2780 cl->next = client_list;
2783 /* detect duplicate KBLOCK requests */
2784 if ( (type == GNUNET_BLOCK_TYPE_KBLOCK) ||
2785 (type == GNUNET_BLOCK_TYPE_NBLOCK) ||
2786 (type == GNUNET_BLOCK_TYPE_ANY) )
2789 while ( (crl != NULL) &&
2790 ( (0 != memcmp (&crl->req->query,
2792 sizeof (GNUNET_HashCode))) ||
2793 (crl->req->type != type) ) )
2798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2799 "Have existing request, merging content-seen lists.\n");
2802 /* Duplicate request (used to send long list of
2803 known/blocked results); merge 'pr->replies_seen'
2804 and update bloom filter */
2805 GNUNET_array_grow (pr->replies_seen,
2806 pr->replies_seen_size,
2807 pr->replies_seen_off + sc);
2808 memcpy (&pr->replies_seen[pr->replies_seen_off],
2810 sc * sizeof (GNUNET_HashCode));
2811 pr->replies_seen_off += sc;
2812 refresh_bloomfilter (pr);
2813 GNUNET_STATISTICS_update (stats,
2814 gettext_noop ("# client searches updated (merged content seen list)"),
2817 GNUNET_SERVER_receive_done (client,
2822 GNUNET_STATISTICS_update (stats,
2823 gettext_noop ("# client searches active"),
2826 pr = GNUNET_malloc (sizeof (struct PendingRequest) +
2827 ((type == GNUNET_BLOCK_TYPE_SBLOCK) ? sizeof(GNUNET_HashCode) : 0));
2828 crl = GNUNET_malloc (sizeof (struct ClientRequestList));
2829 memset (crl, 0, sizeof (struct ClientRequestList));
2830 crl->client_list = cl;
2831 GNUNET_CONTAINER_DLL_insert (cl->rl_head,
2836 pr->client_request_list = crl;
2837 GNUNET_array_grow (pr->replies_seen,
2838 pr->replies_seen_size,
2840 memcpy (pr->replies_seen,
2842 sc * sizeof (GNUNET_HashCode));
2843 pr->replies_seen_off = sc;
2844 pr->anonymity_level = ntohl (sm->anonymity_level);
2845 refresh_bloomfilter (pr);
2846 pr->query = sm->query;
2847 if (0 == (1 & ntohl (sm->options)))
2848 pr->local_only = GNUNET_NO;
2850 pr->local_only = GNUNET_YES;
2853 case GNUNET_BLOCK_TYPE_DBLOCK:
2854 case GNUNET_BLOCK_TYPE_IBLOCK:
2855 if (0 != memcmp (&sm->target,
2857 sizeof (GNUNET_HashCode)))
2858 pr->target_pid = GNUNET_PEER_intern ((const struct GNUNET_PeerIdentity*) &sm->target);
2860 case GNUNET_BLOCK_TYPE_SBLOCK:
2861 pr->namespace = (GNUNET_HashCode*) &pr[1];
2862 memcpy (&pr[1], &sm->target, sizeof (GNUNET_HashCode));
2867 GNUNET_break (GNUNET_OK ==
2868 GNUNET_CONTAINER_multihashmap_put (query_request_map,
2871 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
2872 if (type == GNUNET_BLOCK_TYPE_DBLOCK)
2873 type = GNUNET_BLOCK_TYPE_ANY; /* get on-demand blocks too! */
2874 pr->qe = GNUNET_DATASTORE_get (dsh,
2878 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
2879 &process_local_reply,
2884 /* **************************** Startup ************************ */
2888 * List of handlers for P2P messages
2889 * that we care about.
2891 static struct GNUNET_CORE_MessageHandler p2p_handlers[] =
2894 GNUNET_MESSAGE_TYPE_FS_GET, 0 },
2896 GNUNET_MESSAGE_TYPE_FS_PUT, 0 },
2902 * List of handlers for the messages understood by this
2905 static struct GNUNET_SERVER_MessageHandler handlers[] = {
2906 {&GNUNET_FS_handle_index_start, NULL,
2907 GNUNET_MESSAGE_TYPE_FS_INDEX_START, 0},
2908 {&GNUNET_FS_handle_index_list_get, NULL,
2909 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_GET, sizeof(struct GNUNET_MessageHeader) },
2910 {&GNUNET_FS_handle_unindex, NULL, GNUNET_MESSAGE_TYPE_FS_UNINDEX,
2911 sizeof (struct UnindexMessage) },
2912 {&handle_start_search, NULL, GNUNET_MESSAGE_TYPE_FS_START_SEARCH,
2919 * Process fs requests.
2921 * @param s scheduler to use
2922 * @param server the initialized server
2923 * @param c configuration to use
2926 main_init (struct GNUNET_SCHEDULER_Handle *s,
2927 struct GNUNET_SERVER_Handle *server,
2928 const struct GNUNET_CONFIGURATION_Handle *c)
2932 stats = GNUNET_STATISTICS_create (sched, "fs", cfg);
2933 connected_peers = GNUNET_CONTAINER_multihashmap_create (128); // FIXME: get size from config
2934 query_request_map = GNUNET_CONTAINER_multihashmap_create (128); // FIXME: get size from config
2935 peer_request_map = GNUNET_CONTAINER_multihashmap_create (128); // FIXME: get size from config
2936 requests_by_expiration_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
2937 core = GNUNET_CORE_connect (sched,
2939 GNUNET_TIME_UNIT_FOREVER_REL,
2942 &peer_connect_handler,
2943 &peer_disconnect_handler,
2949 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2950 _("Failed to connect to `%s' service.\n"),
2952 GNUNET_CONTAINER_multihashmap_destroy (connected_peers);
2953 connected_peers = NULL;
2954 GNUNET_CONTAINER_multihashmap_destroy (query_request_map);
2955 query_request_map = NULL;
2956 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
2957 requests_by_expiration_heap = NULL;
2958 GNUNET_CONTAINER_multihashmap_destroy (peer_request_map);
2959 peer_request_map = NULL;
2962 GNUNET_DATASTORE_disconnect (dsh, GNUNET_NO);
2965 return GNUNET_SYSERR;
2967 GNUNET_SERVER_disconnect_notify (server,
2968 &handle_client_disconnect,
2970 GNUNET_SERVER_add_handlers (server, handlers);
2971 GNUNET_SCHEDULER_add_delayed (sched,
2972 GNUNET_TIME_UNIT_FOREVER_REL,
2980 * Process fs requests.
2982 * @param cls closure
2983 * @param sched scheduler to use
2984 * @param server the initialized server
2985 * @param cfg configuration to use
2989 struct GNUNET_SCHEDULER_Handle *sched,
2990 struct GNUNET_SERVER_Handle *server,
2991 const struct GNUNET_CONFIGURATION_Handle *cfg)
2993 active_migration = GNUNET_CONFIGURATION_get_value_yesno (cfg,
2996 dsh = GNUNET_DATASTORE_connect (cfg,
3000 GNUNET_SCHEDULER_shutdown (sched);
3003 if ( (GNUNET_OK != GNUNET_FS_indexing_init (sched, cfg, dsh)) ||
3004 (GNUNET_OK != main_init (sched, server, cfg)) )
3006 GNUNET_SCHEDULER_shutdown (sched);
3007 GNUNET_DATASTORE_disconnect (dsh, GNUNET_NO);
3015 * The main function for the fs service.
3017 * @param argc number of arguments from the command line
3018 * @param argv command line arguments
3019 * @return 0 ok, 1 on error
3022 main (int argc, char *const *argv)
3024 return (GNUNET_OK ==
3025 GNUNET_SERVICE_run (argc,
3028 GNUNET_SERVICE_OPTION_NONE,
3029 &run, NULL)) ? 0 : 1;
3032 /* end of gnunet-service-fs.c */