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 * - have non-zero preference / priority for requests we initiate!
28 * - track stats for hot-path routing
29 * - implement hot-path routing decision procedure
30 * - implement: bound_priority, test_load_too_high, validate_skblock
31 * - add content migration support (store locally)
36 #include "gnunet_constants.h"
37 #include "gnunet_core_service.h"
38 #include "gnunet_datastore_service.h"
39 #include "gnunet_peer_lib.h"
40 #include "gnunet_protocols.h"
41 #include "gnunet_signatures.h"
42 #include "gnunet_statistics_service.h"
43 #include "gnunet_util_lib.h"
44 #include "gnunet-service-fs_drq.h"
45 #include "gnunet-service-fs_indexing.h"
51 * Maximum number of outgoing messages we queue per peer.
52 * FIXME: set to a tiny value for testing; make configurable.
54 #define MAX_QUEUE_PER_PEER 2
57 * Inverse of the probability that we will submit the same query
58 * to the same peer again. If the same peer already got the query
59 * repeatedly recently, the probability is multiplied by the inverse
60 * of this number each time.
62 #define RETRY_PROBABILITY_INV 8
65 * What is the maximum delay for a P2P FS message (in our interaction
66 * with core)? FS-internal delays are another story. The value is
67 * chosen based on the 32k block size. Given that peers typcially
68 * have at least 1 kb/s bandwidth, 45s waits give us a chance to
69 * transmit one message even to the lowest-bandwidth peers.
71 #define MAX_TRANSMIT_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 45)
76 * Maximum number of requests (from other peers) that we're
77 * willing to have pending at any given point in time.
78 * FIXME: set from configuration (and 32 is a tiny value for testing only).
80 static uint64_t max_pending_requests = 32;
84 * Information we keep for each pending reply. The
85 * actual message follows at the end of this struct.
87 struct PendingMessage;
91 * Function called upon completion of a transmission.
94 * @param pid ID of receiving peer, 0 on transmission error
96 typedef void (*TransmissionContinuation)(void * cls,
101 * Information we keep for each pending message (GET/PUT). The
102 * actual message follows at the end of this struct.
104 struct PendingMessage
107 * This is a doubly-linked list of messages to the same peer.
109 struct PendingMessage *next;
112 * This is a doubly-linked list of messages to the same peer.
114 struct PendingMessage *prev;
117 * Entry in pending message list for this pending message.
119 struct PendingMessageList *pml;
122 * Function to call immediately once we have transmitted this
125 TransmissionContinuation cont;
133 * Size of the reply; actual reply message follows
134 * at the end of this struct.
139 * How important is this message for us?
147 * Information about a peer that we are connected to.
148 * We track data that is useful for determining which
149 * peers should receive our requests. We also keep
150 * a list of messages to transmit to this peer.
156 * List of the last clients for which this peer successfully
159 struct GNUNET_SERVER_Client *last_client_replies[CS2P_SUCCESS_LIST_SIZE];
162 * List of the last PIDs for which
163 * this peer successfully answered a query;
164 * We use 0 to indicate no successful reply.
166 GNUNET_PEER_Id last_p2p_replies[P2P_SUCCESS_LIST_SIZE];
169 * Average delay between sending the peer a request and
170 * getting a reply (only calculated over the requests for
171 * which we actually got a reply). Calculated
172 * as a moving average: new_delay = ((n-1)*last_delay+curr_delay) / n
174 struct GNUNET_TIME_Relative avg_delay;
177 * Handle for an active request for transmission to this
180 struct GNUNET_CORE_TransmitHandle *cth;
183 * Messages (replies, queries, content migration) we would like to
184 * send to this peer in the near future. Sorted by priority, head.
186 struct PendingMessage *pending_messages_head;
189 * Messages (replies, queries, content migration) we would like to
190 * send to this peer in the near future. Sorted by priority, tail.
192 struct PendingMessage *pending_messages_tail;
195 * Average priority of successful replies. Calculated
196 * as a moving average: new_avg = ((n-1)*last_avg+curr_prio) / n
201 * Increase in traffic preference still to be submitted
202 * to the core service for this peer. FIXME: double or 'uint64_t'?
204 double inc_preference;
207 * The peer's identity.
212 * Size of the linked list of 'pending_messages'.
214 unsigned int pending_requests;
217 * Which offset in "last_p2p_replies" will be updated next?
218 * (we go round-robin).
220 unsigned int last_p2p_replies_woff;
223 * Which offset in "last_client_replies" will be updated next?
224 * (we go round-robin).
226 unsigned int last_client_replies_woff;
232 * Information we keep for each pending request. We should try to
233 * keep this struct as small as possible since its memory consumption
234 * is key to how many requests we can have pending at once.
236 struct PendingRequest;
240 * Doubly-linked list of requests we are performing
241 * on behalf of the same client.
243 struct ClientRequestList
247 * This is a doubly-linked list.
249 struct ClientRequestList *next;
252 * This is a doubly-linked list.
254 struct ClientRequestList *prev;
257 * Request this entry represents.
259 struct PendingRequest *req;
262 * Client list this request belongs to.
264 struct ClientList *client_list;
270 * Replies to be transmitted to the client. The actual
271 * response message is allocated after this struct.
273 struct ClientResponseMessage
276 * This is a doubly-linked list.
278 struct ClientResponseMessage *next;
281 * This is a doubly-linked list.
283 struct ClientResponseMessage *prev;
286 * Client list entry this response belongs to.
288 struct ClientList *client_list;
291 * Number of bytes in the response.
298 * Linked list of clients we are performing requests
304 * This is a linked list.
306 struct ClientList *next;
309 * ID of a client making a request, NULL if this entry is for a
312 struct GNUNET_SERVER_Client *client;
315 * Head of list of requests performed on behalf
316 * of this client right now.
318 struct ClientRequestList *rl_head;
321 * Tail of list of requests performed on behalf
322 * of this client right now.
324 struct ClientRequestList *rl_tail;
327 * Head of linked list of responses.
329 struct ClientResponseMessage *res_head;
332 * Tail of linked list of responses.
334 struct ClientResponseMessage *res_tail;
337 * Context for sending replies.
339 struct GNUNET_CONNECTION_TransmitHandle *th;
345 * Doubly-linked list of messages we are performing
346 * due to a pending request.
348 struct PendingMessageList
352 * This is a doubly-linked list of messages on behalf of the same request.
354 struct PendingMessageList *next;
357 * This is a doubly-linked list of messages on behalf of the same request.
359 struct PendingMessageList *prev;
362 * Message this entry represents.
364 struct PendingMessage *pm;
367 * Request this entry belongs to.
369 struct PendingRequest *req;
372 * Peer this message is targeted for.
374 struct ConnectedPeer *target;
380 * Information we keep for each pending request. We should try to
381 * keep this struct as small as possible since its memory consumption
382 * is key to how many requests we can have pending at once.
384 struct PendingRequest
388 * If this request was made by a client, this is our entry in the
389 * client request list; otherwise NULL.
391 struct ClientRequestList *client_request_list;
394 * Entry of peer responsible for this entry (if this request
395 * was made by a peer).
397 struct ConnectedPeer *cp;
400 * If this is a namespace query, pointer to the hash of the public
401 * key of the namespace; otherwise NULL. Pointer will be to the
402 * end of this struct (so no need to free it).
404 const GNUNET_HashCode *namespace;
407 * Bloomfilter we use to filter out replies that we don't care about
408 * (anymore). NULL as long as we are interested in all replies.
410 struct GNUNET_CONTAINER_BloomFilter *bf;
413 * Context of our GNUNET_CORE_peer_change_preference call.
415 struct GNUNET_CORE_InformationRequestContext *irc;
418 * Hash code of all replies that we have seen so far (only valid
419 * if client is not NULL since we only track replies like this for
422 GNUNET_HashCode *replies_seen;
425 * Node in the heap representing this entry; NULL
426 * if we have no heap node.
428 struct GNUNET_CONTAINER_HeapNode *hnode;
431 * Head of list of messages being performed on behalf of this
434 struct PendingMessageList *pending_head;
437 * Tail of list of messages being performed on behalf of this
440 struct PendingMessageList *pending_tail;
443 * When did we first see this request (form this peer), or, if our
444 * client is initiating, when did we last initiate a search?
446 struct GNUNET_TIME_Absolute start_time;
449 * The query that this request is for.
451 GNUNET_HashCode query;
454 * The task responsible for transmitting queries
457 GNUNET_SCHEDULER_TaskIdentifier task;
460 * (Interned) Peer identifier that identifies a preferred target
463 GNUNET_PEER_Id target_pid;
466 * (Interned) Peer identifiers of peers that have already
467 * received our query for this content.
469 GNUNET_PEER_Id *used_pids;
472 * Our entry in the DRQ (non-NULL while we wait for our
473 * turn to interact with the local database).
475 struct DatastoreRequestQueue *drq;
478 * Size of the 'bf' (in bytes).
483 * Desired anonymity level; only valid for requests from a local client.
485 uint32_t anonymity_level;
488 * How many entries in "used_pids" are actually valid?
490 unsigned int used_pids_off;
493 * How long is the "used_pids" array?
495 unsigned int used_pids_size;
498 * Number of results found for this request.
500 unsigned int results_found;
503 * How many entries in "replies_seen" are actually valid?
505 unsigned int replies_seen_off;
508 * How long is the "replies_seen" array?
510 unsigned int replies_seen_size;
513 * Priority with which this request was made. If one of our clients
514 * made the request, then this is the current priority that we are
515 * using when initiating the request. This value is used when
516 * we decide to reward other peers with trust for providing a reply.
521 * Priority points left for us to spend when forwarding this request
524 uint32_t remaining_priority;
527 * Number to mingle hashes for bloom-filter tests with.
532 * TTL with which we saw this request (or, if we initiated, TTL that
533 * we used for the request).
538 * Type of the content that this request is for.
548 static struct GNUNET_SCHEDULER_Handle *sched;
553 static const struct GNUNET_CONFIGURATION_Handle *cfg;
556 * Map of peer identifiers to "struct ConnectedPeer" (for that peer).
558 static struct GNUNET_CONTAINER_MultiHashMap *connected_peers;
561 * Map of peer identifiers to "struct PendingRequest" (for that peer).
563 static struct GNUNET_CONTAINER_MultiHashMap *peer_request_map;
566 * Map of query identifiers to "struct PendingRequest" (for that query).
568 static struct GNUNET_CONTAINER_MultiHashMap *query_request_map;
571 * Heap with the request that will expire next at the top. Contains
572 * pointers of type "struct PendingRequest*"; these will *also* be
573 * aliased from the "requests_by_peer" data structures and the
574 * "requests_by_query" table. Note that requests from our clients
575 * don't expire and are thus NOT in the "requests_by_expiration"
576 * (or the "requests_by_peer" tables).
578 static struct GNUNET_CONTAINER_Heap *requests_by_expiration_heap;
581 * Handle for reporting statistics.
583 static struct GNUNET_STATISTICS_Handle *stats;
586 * Linked list of clients we are currently processing requests for.
588 static struct ClientList *client_list;
591 * Pointer to handle to the core service (points to NULL until we've
594 static struct GNUNET_CORE_Handle *core;
597 /* ******************* clean up functions ************************ */
601 * We're done with a particular message list entry.
602 * Free all associated resources.
604 * @param pml entry to destroy
607 destroy_pending_message_list_entry (struct PendingMessageList *pml)
609 GNUNET_CONTAINER_DLL_remove (pml->req->pending_head,
610 pml->req->pending_tail,
612 GNUNET_CONTAINER_DLL_remove (pml->target->pending_messages_head,
613 pml->target->pending_messages_tail,
615 pml->target->pending_requests--;
616 GNUNET_free (pml->pm);
622 * Destroy the given pending message (and call the respective
625 * @param pm message to destroy
626 * @param tpid id of peer that the message was delivered to, or 0 for none
629 destroy_pending_message (struct PendingMessage *pm,
632 struct PendingMessageList *pml = pm->pml;
633 TransmissionContinuation cont;
636 GNUNET_assert (pml->pm == pm);
637 GNUNET_assert ( (tpid == 0) || (tpid == pml->target->pid) );
639 cont_cls = pm->cont_cls;
640 destroy_pending_message_list_entry (pml);
641 cont (cont_cls, tpid);
646 * We're done processing a particular request.
647 * Free all associated resources.
649 * @param pr request to destroy
652 destroy_pending_request (struct PendingRequest *pr)
654 struct GNUNET_PeerIdentity pid;
656 if (pr->hnode != NULL)
658 GNUNET_CONTAINER_heap_remove_node (requests_by_expiration_heap,
661 GNUNET_STATISTICS_update (stats,
662 gettext_noop ("# P2P searches active"),
668 GNUNET_STATISTICS_update (stats,
669 gettext_noop ("# client searches active"),
673 /* might have already been removed from map in 'process_reply' (if
674 there was a unique reply) or never inserted if it was a
675 duplicate; hence ignore the return value here */
676 (void) GNUNET_CONTAINER_multihashmap_remove (query_request_map,
681 GNUNET_FS_drq_get_cancel (pr->drq);
684 if (pr->client_request_list != NULL)
686 GNUNET_CONTAINER_DLL_remove (pr->client_request_list->client_list->rl_head,
687 pr->client_request_list->client_list->rl_tail,
688 pr->client_request_list);
689 GNUNET_free (pr->client_request_list);
690 pr->client_request_list = NULL;
694 GNUNET_PEER_resolve (pr->cp->pid,
696 (void) GNUNET_CONTAINER_multihashmap_remove (peer_request_map,
703 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
708 GNUNET_CORE_peer_change_preference_cancel (pr->irc);
711 if (pr->replies_seen != NULL)
713 GNUNET_free (pr->replies_seen);
714 pr->replies_seen = NULL;
716 if (pr->task != GNUNET_SCHEDULER_NO_TASK)
718 GNUNET_SCHEDULER_cancel (sched,
720 pr->task = GNUNET_SCHEDULER_NO_TASK;
722 while (NULL != pr->pending_head)
723 destroy_pending_message_list_entry (pr->pending_head);
724 GNUNET_PEER_change_rc (pr->target_pid, -1);
725 if (pr->used_pids != NULL)
727 GNUNET_PEER_decrement_rcs (pr->used_pids, pr->used_pids_off);
728 GNUNET_free (pr->used_pids);
729 pr->used_pids_off = 0;
730 pr->used_pids_size = 0;
731 pr->used_pids = NULL;
738 * Method called whenever a given peer connects.
740 * @param cls closure, not used
741 * @param peer peer identity this notification is about
742 * @param latency reported latency of the connection with 'other'
743 * @param distance reported distance (DV) to 'other'
746 peer_connect_handler (void *cls,
748 GNUNET_PeerIdentity * peer,
749 struct GNUNET_TIME_Relative latency,
752 struct ConnectedPeer *cp;
754 cp = GNUNET_malloc (sizeof (struct ConnectedPeer));
755 cp->pid = GNUNET_PEER_intern (peer);
756 GNUNET_CONTAINER_multihashmap_put (connected_peers,
759 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
764 * Free (each) request made by the peer.
766 * @param cls closure, points to peer that the request belongs to
767 * @param key current key code
768 * @param value value in the hash map
769 * @return GNUNET_YES (we should continue to iterate)
772 destroy_request (void *cls,
773 const GNUNET_HashCode * key,
776 const struct GNUNET_PeerIdentity * peer = cls;
777 struct PendingRequest *pr = value;
779 GNUNET_CONTAINER_multihashmap_remove (peer_request_map,
782 destroy_pending_request (pr);
788 * Method called whenever a peer disconnects.
790 * @param cls closure, not used
791 * @param peer peer identity this notification is about
794 peer_disconnect_handler (void *cls,
796 GNUNET_PeerIdentity * peer)
798 struct ConnectedPeer *cp;
799 struct PendingMessage *pm;
802 GNUNET_CONTAINER_multihashmap_get_multiple (peer_request_map,
806 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
810 for (i=0;i<CS2P_SUCCESS_LIST_SIZE;i++)
812 if (NULL != cp->last_client_replies[i])
814 GNUNET_SERVER_client_drop (cp->last_client_replies[i]);
815 cp->last_client_replies[i] = NULL;
818 GNUNET_CONTAINER_multihashmap_remove (connected_peers,
821 GNUNET_PEER_change_rc (cp->pid, -1);
822 GNUNET_PEER_decrement_rcs (cp->last_p2p_replies, P2P_SUCCESS_LIST_SIZE);
824 GNUNET_CORE_notify_transmit_ready_cancel (cp->cth);
825 while (NULL != (pm = cp->pending_messages_head))
826 destroy_pending_message (pm, 0 /* delivery failed */);
827 GNUNET_break (0 == cp->pending_requests);
833 * Iterator over hash map entries that removes all occurences
834 * of the given 'client' from the 'last_client_replies' of the
835 * given connected peer.
837 * @param cls closure, the 'struct GNUNET_SERVER_Client*' to remove
838 * @param key current key code (unused)
839 * @param value value in the hash map (the 'struct ConnectedPeer*' to change)
840 * @return GNUNET_YES (we should continue to iterate)
843 remove_client_from_last_client_replies (void *cls,
844 const GNUNET_HashCode * key,
847 struct GNUNET_SERVER_Client *client = cls;
848 struct ConnectedPeer *cp = value;
851 for (i=0;i<CS2P_SUCCESS_LIST_SIZE;i++)
853 if (cp->last_client_replies[i] == client)
855 GNUNET_SERVER_client_drop (cp->last_client_replies[i]);
856 cp->last_client_replies[i] = NULL;
864 * A client disconnected. Remove all of its pending queries.
866 * @param cls closure, NULL
867 * @param client identification of the client
870 handle_client_disconnect (void *cls,
871 struct GNUNET_SERVER_Client
874 struct ClientList *pos;
875 struct ClientList *prev;
876 struct ClientRequestList *rcl;
877 struct ClientResponseMessage *creply;
883 while ( (NULL != pos) &&
884 (pos->client != client) )
890 return; /* no requests pending for this client */
891 while (NULL != (rcl = pos->rl_head))
892 destroy_pending_request (rcl->req);
894 client_list = pos->next;
896 prev->next = pos->next;
899 GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th);
902 while (NULL != (creply = pos->res_head))
904 GNUNET_CONTAINER_DLL_remove (pos->res_head,
907 GNUNET_free (creply);
909 GNUNET_SERVER_client_drop (pos->client);
911 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
912 &remove_client_from_last_client_replies,
918 * Iterator to free peer entries.
920 * @param cls closure, unused
921 * @param key current key code
922 * @param value value in the hash map (peer entry)
923 * @return GNUNET_YES (we should continue to iterate)
926 clean_peer (void *cls,
927 const GNUNET_HashCode * key,
930 peer_disconnect_handler (NULL, (const struct GNUNET_PeerIdentity*) key);
936 * Task run during shutdown.
942 shutdown_task (void *cls,
943 const struct GNUNET_SCHEDULER_TaskContext *tc)
945 while (client_list != NULL)
946 handle_client_disconnect (NULL,
947 client_list->client);
948 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
951 GNUNET_break (0 == GNUNET_CONTAINER_heap_get_size (requests_by_expiration_heap));
952 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
953 requests_by_expiration_heap = 0;
954 GNUNET_CONTAINER_multihashmap_destroy (connected_peers);
955 connected_peers = NULL;
956 GNUNET_break (0 == GNUNET_CONTAINER_multihashmap_size (query_request_map));
957 GNUNET_CONTAINER_multihashmap_destroy (query_request_map);
958 query_request_map = NULL;
959 GNUNET_break (0 == GNUNET_CONTAINER_multihashmap_size (peer_request_map));
960 GNUNET_CONTAINER_multihashmap_destroy (peer_request_map);
961 peer_request_map = NULL;
962 GNUNET_assert (NULL != core);
963 GNUNET_CORE_disconnect (core);
967 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
975 /* ******************* Utility functions ******************** */
979 * Transmit the given message by copying it to the target buffer
980 * "buf". "buf" will be NULL and "size" zero if the socket was closed
981 * for writing in the meantime. In that case, do nothing
982 * (the disconnect or shutdown handler will take care of the rest).
983 * If we were able to transmit messages and there are still more
984 * pending, ask core again for further calls to this function.
986 * @param cls closure, pointer to the 'struct ConnectedPeer*'
987 * @param size number of bytes available in buf
988 * @param buf where the callee should write the message
989 * @return number of bytes written to buf
992 transmit_to_peer (void *cls,
993 size_t size, void *buf)
995 struct ConnectedPeer *cp = cls;
997 struct GNUNET_PeerIdentity pid;
998 struct PendingMessage *pm;
1005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1006 "Dropping message, core too busy.\n");
1011 while ( (NULL != (pm = cp->pending_messages_head) ) &&
1012 (pm->msize <= size) )
1014 memcpy (&cbuf[msize], &pm[1], pm->msize);
1017 destroy_pending_message (pm, cp->pid);
1021 GNUNET_PEER_resolve (cp->pid,
1023 cp->cth = GNUNET_CORE_notify_transmit_ready (core,
1025 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1033 "Transmitting %u bytes to peer %u.\n",
1042 * Add a message to the set of pending messages for the given peer.
1044 * @param cp peer to send message to
1045 * @param pm message to queue
1046 * @param pr request on which behalf this message is being queued
1049 add_to_pending_messages_for_peer (struct ConnectedPeer *cp,
1050 struct PendingMessage *pm,
1051 struct PendingRequest *pr)
1053 struct PendingMessage *pos;
1054 struct PendingMessageList *pml;
1055 struct GNUNET_PeerIdentity pid;
1057 GNUNET_assert (pm->next == NULL);
1058 GNUNET_assert (pm->pml == NULL);
1059 pml = GNUNET_malloc (sizeof (struct PendingMessageList));
1064 GNUNET_CONTAINER_DLL_insert (pr->pending_head,
1067 pos = cp->pending_messages_head;
1068 while ( (pos != NULL) &&
1069 (pm->priority < pos->priority) )
1071 GNUNET_CONTAINER_DLL_insert_after (cp->pending_messages_head,
1072 cp->pending_messages_tail,
1075 cp->pending_requests++;
1076 if (cp->pending_requests > MAX_QUEUE_PER_PEER)
1077 destroy_pending_message (cp->pending_messages_tail, 0);
1078 if (cp->cth == NULL)
1080 /* need to schedule transmission */
1081 GNUNET_PEER_resolve (cp->pid, &pid);
1082 cp->cth = GNUNET_CORE_notify_transmit_ready (core,
1083 cp->pending_messages_head->priority,
1086 cp->pending_messages_head->msize,
1090 if (cp->cth == NULL)
1093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1094 "Failed to schedule transmission with core!\n");
1096 /* FIXME: call stats (rare, bad case) */
1102 * Mingle hash with the mingle_number to produce different bits.
1105 mingle_hash (const GNUNET_HashCode * in,
1106 int32_t mingle_number,
1107 GNUNET_HashCode * hc)
1111 GNUNET_CRYPTO_hash (&mingle_number,
1114 GNUNET_CRYPTO_hash_xor (&m, in, hc);
1119 * Test if the load on this peer is too high
1120 * to even consider processing the query at
1123 * @return GNUNET_YES if the load is too high, GNUNET_NO otherwise
1126 test_load_too_high ()
1128 return GNUNET_NO; // FIXME
1132 /* ******************* Pending Request Refresh Task ******************** */
1137 * We use a random delay to make the timing of requests less
1138 * predictable. This function returns such a random delay. We add a base
1139 * delay of MAX_CORK_DELAY (1s).
1141 * FIXME: make schedule dependent on the specifics of the request?
1142 * Or bandwidth and number of connected peers and load?
1144 * @return random delay to use for some request, between 1s and 1000+TTL_DECREMENT ms
1146 static struct GNUNET_TIME_Relative
1147 get_processing_delay ()
1150 GNUNET_TIME_relative_add (GNUNET_CONSTANTS_MAX_CORK_DELAY,
1151 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1152 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1158 * We're processing a GET request from another peer and have decided
1159 * to forward it to other peers. This function is called periodically
1160 * and should forward the request to other peers until we have all
1161 * possible replies. If we have transmitted the *only* reply to
1162 * the initiator we should destroy the pending request. If we have
1163 * many replies in the queue to the initiator, we should delay sending
1164 * out more queries until the reply queue has shrunk some.
1166 * @param cls our "struct ProcessGetContext *"
1170 forward_request_task (void *cls,
1171 const struct GNUNET_SCHEDULER_TaskContext *tc);
1175 * Function called after we either failed or succeeded
1176 * at transmitting a query to a peer.
1178 * @param cls the requests "struct PendingRequest*"
1179 * @param tpid ID of receiving peer, 0 on transmission error
1182 transmit_query_continuation (void *cls,
1183 GNUNET_PEER_Id tpid)
1185 struct PendingRequest *pr = cls;
1189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1190 "Transmission of request failed, will try again later.\n");
1191 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
1192 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
1193 get_processing_delay (),
1194 &forward_request_task,
1198 GNUNET_STATISTICS_update (stats,
1199 gettext_noop ("# queries forwarded"),
1202 GNUNET_PEER_change_rc (tpid, 1);
1203 if (pr->used_pids_off == pr->used_pids_size)
1204 GNUNET_array_grow (pr->used_pids,
1206 pr->used_pids_size * 2 + 2);
1207 pr->used_pids[pr->used_pids_off++] = tpid;
1208 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
1209 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
1210 get_processing_delay (),
1211 &forward_request_task,
1217 * How many bytes should a bloomfilter be if we have already seen
1218 * entry_count responses? Note that BLOOMFILTER_K gives us the number
1219 * of bits set per entry. Furthermore, we should not re-size the
1220 * filter too often (to keep it cheap).
1222 * Since other peers will also add entries but not resize the filter,
1223 * we should generally pick a slightly larger size than what the
1224 * strict math would suggest.
1226 * @return must be a power of two and smaller or equal to 2^15.
1229 compute_bloomfilter_size (unsigned int entry_count)
1232 unsigned int ideal = (entry_count * BLOOMFILTER_K) / 4;
1233 uint16_t max = 1 << 15;
1235 if (entry_count > max)
1238 while ((size < max) && (size < ideal))
1247 * Recalculate our bloom filter for filtering replies.
1249 * @param count number of entries we are filtering right now
1250 * @param mingle set to our new mingling value
1251 * @param bf_size set to the size of the bloomfilter
1252 * @param entries the entries to filter
1253 * @return updated bloomfilter, NULL for none
1255 static struct GNUNET_CONTAINER_BloomFilter *
1256 refresh_bloomfilter (unsigned int count,
1259 const GNUNET_HashCode *entries)
1261 struct GNUNET_CONTAINER_BloomFilter *bf;
1264 GNUNET_HashCode mhash;
1268 nsize = compute_bloomfilter_size (count);
1269 *mingle = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, -1);
1271 bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
1274 for (i=0;i<count;i++)
1276 mingle_hash (&entries[i], *mingle, &mhash);
1277 GNUNET_CONTAINER_bloomfilter_add (bf, &mhash);
1284 * Function called after we've tried to reserve a certain amount of
1285 * bandwidth for a reply. Check if we succeeded and if so send our
1288 * @param cls the requests "struct PendingRequest*"
1289 * @param peer identifies the peer
1290 * @param bpm_in set to the current bandwidth limit (receiving) for this peer
1291 * @param bpm_out set to the current bandwidth limit (sending) for this peer
1292 * @param amount set to the amount that was actually reserved or unreserved
1293 * @param preference current traffic preference for the given peer
1296 target_reservation_cb (void *cls,
1298 GNUNET_PeerIdentity * peer,
1299 struct GNUNET_BANDWIDTH_Value32NBO bpm_in,
1300 struct GNUNET_BANDWIDTH_Value32NBO bpm_out,
1302 uint64_t preference)
1304 struct PendingRequest *pr = cls;
1305 struct ConnectedPeer *cp;
1306 struct PendingMessage *pm;
1307 struct GetMessage *gm;
1308 GNUNET_HashCode *ext;
1318 /* error in communication with core, try again later */
1319 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
1320 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
1321 get_processing_delay (),
1322 &forward_request_task,
1326 // (3) transmit, update ttl/priority
1327 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
1331 /* Peer must have just left */
1333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1334 "Selected peer disconnected!\n");
1336 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
1337 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
1338 get_processing_delay (),
1339 &forward_request_task,
1343 no_route = GNUNET_NO;
1344 /* FIXME: check against DBLOCK_SIZE and possibly return
1345 amount to reserve; however, this also needs to work
1346 with testcases which currently start out with a far
1347 too low per-peer bw limit, so they would never send
1348 anything. Big issue. */
1354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1355 "Failed to reserve bandwidth for reply (got %d/%u bytes only)!\n",
1359 GNUNET_STATISTICS_update (stats,
1360 gettext_noop ("# reply bandwidth reservation requests failed"),
1363 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
1364 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
1365 get_processing_delay (),
1366 &forward_request_task,
1368 return; /* this target round failed */
1370 /* FIXME: if we are "quite" busy, we may still want to skip
1371 this round; need more load detection code! */
1372 no_route = GNUNET_YES;
1375 GNUNET_STATISTICS_update (stats,
1376 gettext_noop ("# requests forwarded"),
1379 /* build message and insert message into priority queue */
1381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1382 "Forwarding request `%s' to `%4s'!\n",
1383 GNUNET_h2s (&pr->query),
1388 if (GNUNET_YES == no_route)
1390 bm |= GET_MESSAGE_BIT_RETURN_TO;
1393 if (pr->namespace != NULL)
1395 bm |= GET_MESSAGE_BIT_SKS_NAMESPACE;
1398 if (pr->target_pid != 0)
1400 bm |= GET_MESSAGE_BIT_TRANSMIT_TO;
1403 msize = sizeof (struct GetMessage) + pr->bf_size + k * sizeof(GNUNET_HashCode);
1404 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1405 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1407 gm = (struct GetMessage*) &pm[1];
1408 gm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_GET);
1409 gm->header.size = htons (msize);
1410 gm->type = htonl (pr->type);
1411 pr->remaining_priority /= 2;
1412 gm->priority = htonl (pr->remaining_priority);
1413 gm->ttl = htonl (pr->ttl);
1414 gm->filter_mutator = htonl(pr->mingle);
1415 gm->hash_bitmap = htonl (bm);
1416 gm->query = pr->query;
1417 ext = (GNUNET_HashCode*) &gm[1];
1419 if (GNUNET_YES == no_route)
1420 GNUNET_PEER_resolve (pr->cp->pid, (struct GNUNET_PeerIdentity*) &ext[k++]);
1421 if (pr->namespace != NULL)
1422 memcpy (&ext[k++], pr->namespace, sizeof (GNUNET_HashCode));
1423 if (pr->target_pid != 0)
1424 GNUNET_PEER_resolve (pr->target_pid, (struct GNUNET_PeerIdentity*) &ext[k++]);
1425 bfdata = (char *) &ext[k];
1427 GNUNET_CONTAINER_bloomfilter_get_raw_data (pr->bf,
1430 pm->cont = &transmit_query_continuation;
1432 add_to_pending_messages_for_peer (cp, pm, pr);
1437 * Closure used for "target_peer_select_cb".
1439 struct PeerSelectionContext
1442 * The request for which we are selecting
1445 struct PendingRequest *pr;
1448 * Current "prime" target.
1450 struct GNUNET_PeerIdentity target;
1453 * How much do we like this target?
1455 double target_score;
1461 * Function called for each connected peer to determine
1462 * which one(s) would make good targets for forwarding.
1464 * @param cls closure (struct PeerSelectionContext)
1465 * @param key current key code (peer identity)
1466 * @param value value in the hash map (struct ConnectedPeer)
1467 * @return GNUNET_YES if we should continue to
1472 target_peer_select_cb (void *cls,
1473 const GNUNET_HashCode * key,
1476 struct PeerSelectionContext *psc = cls;
1477 struct ConnectedPeer *cp = value;
1478 struct PendingRequest *pr = psc->pr;
1482 /* 1) check if we have already (recently) forwarded to this peer */
1483 for (i=0;i<pr->used_pids_off;i++)
1484 if ( (pr->used_pids[i] == cp->pid) &&
1485 (0 != GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1486 RETRY_PROBABILITY_INV)) )
1487 return GNUNET_YES; /* skip */
1488 // 2) calculate how much we'd like to forward to this peer
1489 score = 42; // FIXME!
1490 // FIXME: also need API to gather data on responsiveness
1491 // of this peer (we have fields for that in 'cp', but
1492 // they are never set!)
1494 /* store best-fit in closure */
1495 if (score > psc->target_score)
1497 psc->target_score = score;
1498 psc->target.hashPubKey = *key;
1505 * We're processing a GET request from another peer and have decided
1506 * to forward it to other peers. This function is called periodically
1507 * and should forward the request to other peers until we have all
1508 * possible replies. If we have transmitted the *only* reply to
1509 * the initiator we should destroy the pending request. If we have
1510 * many replies in the queue to the initiator, we should delay sending
1511 * out more queries until the reply queue has shrunk some.
1513 * @param cls our "struct ProcessGetContext *"
1517 forward_request_task (void *cls,
1518 const struct GNUNET_SCHEDULER_TaskContext *tc)
1520 struct PendingRequest *pr = cls;
1521 struct PeerSelectionContext psc;
1522 struct ConnectedPeer *cp;
1524 pr->task = GNUNET_SCHEDULER_NO_TASK;
1525 if (pr->irc != NULL)
1526 return; /* already pending */
1527 /* (1) select target */
1529 psc.target_score = DBL_MIN;
1530 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
1531 &target_peer_select_cb,
1533 if (psc.target_score == DBL_MIN)
1536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1537 "No peer selected for forwarding of query `%s'!\n",
1538 GNUNET_h2s (&pr->query));
1540 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
1541 get_processing_delay (),
1542 &forward_request_task,
1544 return; /* nobody selected */
1547 /* (2) reserve reply bandwidth */
1548 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
1549 &psc.target.hashPubKey);
1550 pr->irc = GNUNET_CORE_peer_change_preference (sched, cfg,
1552 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1553 GNUNET_BANDWIDTH_value_init ((uint32_t) -1 /* no limit */),
1555 (uint64_t) cp->inc_preference,
1556 &target_reservation_cb,
1558 cp->inc_preference = 0.0;
1562 /* **************************** P2P PUT Handling ************************ */
1566 * Function called after we either failed or succeeded
1567 * at transmitting a reply to a peer.
1569 * @param cls the requests "struct PendingRequest*"
1570 * @param tpid ID of receiving peer, 0 on transmission error
1573 transmit_reply_continuation (void *cls,
1574 GNUNET_PEER_Id tpid)
1576 struct PendingRequest *pr = cls;
1580 case GNUNET_DATASTORE_BLOCKTYPE_DBLOCK:
1581 case GNUNET_DATASTORE_BLOCKTYPE_IBLOCK:
1582 /* only one reply expected, done with the request! */
1583 destroy_pending_request (pr);
1585 case GNUNET_DATASTORE_BLOCKTYPE_ANY:
1586 case GNUNET_DATASTORE_BLOCKTYPE_KBLOCK:
1587 case GNUNET_DATASTORE_BLOCKTYPE_SBLOCK:
1597 * Check if the given KBlock is well-formed.
1599 * @param kb the kblock data (or at least "dsize" bytes claiming to be one)
1600 * @param dsize size of "kb" in bytes; check for < sizeof(struct KBlock)!
1601 * @param query where to store the query that this block answers
1602 * @return GNUNET_OK if this is actually a well-formed KBlock
1605 check_kblock (const struct KBlock *kb,
1607 GNUNET_HashCode *query)
1609 if (dsize < sizeof (struct KBlock))
1611 GNUNET_break_op (0);
1612 return GNUNET_SYSERR;
1614 if (dsize - sizeof (struct KBlock) !=
1615 ntohl (kb->purpose.size)
1616 - sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose)
1617 - sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) )
1619 GNUNET_break_op (0);
1620 return GNUNET_SYSERR;
1623 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_KBLOCK,
1628 GNUNET_break_op (0);
1629 return GNUNET_SYSERR;
1632 GNUNET_CRYPTO_hash (&kb->keyspace,
1633 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1640 * Check if the given SBlock is well-formed.
1642 * @param sb the sblock data (or at least "dsize" bytes claiming to be one)
1643 * @param dsize size of "kb" in bytes; check for < sizeof(struct SBlock)!
1644 * @param query where to store the query that this block answers
1645 * @param namespace where to store the namespace that this block belongs to
1646 * @return GNUNET_OK if this is actually a well-formed SBlock
1649 check_sblock (const struct SBlock *sb,
1651 GNUNET_HashCode *query,
1652 GNUNET_HashCode *namespace)
1654 if (dsize < sizeof (struct SBlock))
1656 GNUNET_break_op (0);
1657 return GNUNET_SYSERR;
1660 ntohl (sb->purpose.size) + sizeof (struct GNUNET_CRYPTO_RsaSignature))
1662 GNUNET_break_op (0);
1663 return GNUNET_SYSERR;
1666 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_SBLOCK,
1671 GNUNET_break_op (0);
1672 return GNUNET_SYSERR;
1675 *query = sb->identifier;
1676 if (namespace != NULL)
1677 GNUNET_CRYPTO_hash (&sb->subspace,
1678 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1685 * Transmit the given message by copying it to the target buffer
1686 * "buf". "buf" will be NULL and "size" zero if the socket was closed
1687 * for writing in the meantime. In that case, do nothing
1688 * (the disconnect or shutdown handler will take care of the rest).
1689 * If we were able to transmit messages and there are still more
1690 * pending, ask core again for further calls to this function.
1692 * @param cls closure, pointer to the 'struct ClientList*'
1693 * @param size number of bytes available in buf
1694 * @param buf where the callee should write the message
1695 * @return number of bytes written to buf
1698 transmit_to_client (void *cls,
1699 size_t size, void *buf)
1701 struct ClientList *cl = cls;
1703 struct ClientResponseMessage *creply;
1710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1711 "Not sending reply, client communication problem.\n");
1716 while ( (NULL != (creply = cl->res_head) ) &&
1717 (creply->msize <= size) )
1719 memcpy (&cbuf[msize], &creply[1], creply->msize);
1720 msize += creply->msize;
1721 size -= creply->msize;
1722 GNUNET_CONTAINER_DLL_remove (cl->res_head,
1725 GNUNET_free (creply);
1728 cl->th = GNUNET_SERVER_notify_transmit_ready (cl->client,
1730 GNUNET_TIME_UNIT_FOREVER_REL,
1731 &transmit_to_client,
1734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1735 "Transmitted %u bytes to client\n",
1736 (unsigned int) msize);
1743 * Closure for "process_reply" function.
1745 struct ProcessReplyClosure
1748 * The data for the reply.
1752 // FIXME: add 'struct ConnectedPeer' to track 'last_xxx_replies' here!
1755 * When the reply expires.
1757 struct GNUNET_TIME_Absolute expiration;
1765 * Namespace that this reply belongs to
1766 * (if it is of type SBLOCK).
1768 GNUNET_HashCode namespace;
1771 * Type of the block.
1776 * How much was this reply worth to us?
1783 * We have received a reply; handle it!
1785 * @param cls response (struct ProcessReplyClosure)
1786 * @param key our query
1787 * @param value value in the hash map (info about the query)
1788 * @return GNUNET_YES (we should continue to iterate)
1791 process_reply (void *cls,
1792 const GNUNET_HashCode * key,
1795 struct ProcessReplyClosure *prq = cls;
1796 struct PendingRequest *pr = value;
1797 struct PendingMessage *reply;
1798 struct ClientResponseMessage *creply;
1799 struct ClientList *cl;
1800 struct PutMessage *pm;
1801 struct ConnectedPeer *cp;
1802 GNUNET_HashCode chash;
1803 GNUNET_HashCode mhash;
1808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1809 "Matched result (type %u) for query `%s' with pending request\n",
1810 (unsigned int) prq->type,
1813 GNUNET_STATISTICS_update (stats,
1814 gettext_noop ("# replies received and matched"),
1817 do_remove = GNUNET_NO;
1818 GNUNET_CRYPTO_hash (prq->data,
1823 case GNUNET_DATASTORE_BLOCKTYPE_DBLOCK:
1824 case GNUNET_DATASTORE_BLOCKTYPE_IBLOCK:
1825 /* only possible reply, stop requesting! */
1826 while (NULL != pr->pending_head)
1827 destroy_pending_message_list_entry (pr->pending_head);
1828 if (pr->drq != NULL)
1830 if (pr->client_request_list != NULL)
1831 GNUNET_SERVER_receive_done (pr->client_request_list->client_list->client,
1833 GNUNET_FS_drq_get_cancel (pr->drq);
1836 do_remove = GNUNET_YES;
1838 case GNUNET_DATASTORE_BLOCKTYPE_SBLOCK:
1839 if (pr->namespace == NULL)
1844 if (0 != memcmp (pr->namespace,
1846 sizeof (GNUNET_HashCode)))
1847 return GNUNET_YES; /* wrong namespace */
1848 /* then: fall-through! */
1849 case GNUNET_DATASTORE_BLOCKTYPE_KBLOCK:
1852 mingle_hash (&chash, pr->mingle, &mhash);
1853 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (pr->bf,
1855 return GNUNET_YES; /* duplicate */
1856 GNUNET_CONTAINER_bloomfilter_add (pr->bf,
1859 if (pr->client_request_list != NULL)
1861 if (pr->replies_seen_size == pr->replies_seen_off)
1863 GNUNET_array_grow (pr->replies_seen,
1864 pr->replies_seen_size,
1865 pr->replies_seen_size * 2 + 4);
1867 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
1868 pr->bf = refresh_bloomfilter (pr->replies_seen_off,
1873 pr->replies_seen[pr->replies_seen_off++] = chash;
1877 case GNUNET_DATASTORE_BLOCKTYPE_NBLOCK:
1878 // FIXME: any checks against duplicates for NBlocks?
1884 prq->priority += pr->remaining_priority;
1885 pr->remaining_priority = 0;
1886 if (pr->client_request_list != NULL)
1889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1890 "Transmitting result for query `%s' to local client\n",
1893 GNUNET_STATISTICS_update (stats,
1894 gettext_noop ("# replies received for local clients"),
1897 cl = pr->client_request_list->client_list;
1898 msize = sizeof (struct PutMessage) + prq->size;
1899 creply = GNUNET_malloc (msize + sizeof (struct ClientResponseMessage));
1900 creply->msize = msize;
1901 creply->client_list = cl;
1902 GNUNET_CONTAINER_DLL_insert_after (cl->res_head,
1906 pm = (struct PutMessage*) &creply[1];
1907 pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
1908 pm->header.size = htons (msize);
1909 pm->type = htonl (prq->type);
1910 pm->expiration = GNUNET_TIME_absolute_hton (prq->expiration);
1911 memcpy (&pm[1], prq->data, prq->size);
1913 cl->th = GNUNET_SERVER_notify_transmit_ready (cl->client,
1915 GNUNET_TIME_UNIT_FOREVER_REL,
1916 &transmit_to_client,
1918 GNUNET_break (cl->th != NULL);
1924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1925 "Transmitting result for query `%s' to other peer (PID=%u)\n",
1927 (unsigned int) cp->pid);
1929 GNUNET_STATISTICS_update (stats,
1930 gettext_noop ("# replies received for other peers"),
1933 msize = sizeof (struct PutMessage) + prq->size;
1934 reply = GNUNET_malloc (msize + sizeof (struct PendingMessage));
1935 reply->cont = &transmit_reply_continuation;
1936 reply->cont_cls = pr;
1937 reply->msize = msize;
1938 reply->priority = (uint32_t) -1; /* send replies first! */
1939 pm = (struct PutMessage*) &reply[1];
1940 pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
1941 pm->header.size = htons (msize);
1942 pm->type = htonl (prq->type);
1943 pm->expiration = GNUNET_TIME_absolute_hton (prq->expiration);
1944 memcpy (&pm[1], prq->data, prq->size);
1945 add_to_pending_messages_for_peer (cp, reply, pr);
1947 if (GNUNET_YES == do_remove)
1949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1950 "Removing request `%s' from request map (has been satisfied)\n",
1952 GNUNET_break (GNUNET_YES ==
1953 GNUNET_CONTAINER_multihashmap_remove (query_request_map,
1956 // FIXME: request somehow does not fully
1957 // disappear; how to fix?
1958 // destroy_pending_request (pr); (not like this!)
1961 // FIXME: implement hot-path routing statistics keeping!
1967 * Handle P2P "PUT" message.
1969 * @param cls closure, always NULL
1970 * @param other the other peer involved (sender or receiver, NULL
1971 * for loopback messages where we are both sender and receiver)
1972 * @param message the actual message
1973 * @param latency reported latency of the connection with 'other'
1974 * @param distance reported distance (DV) to 'other'
1975 * @return GNUNET_OK to keep the connection open,
1976 * GNUNET_SYSERR to close it (signal serious error)
1979 handle_p2p_put (void *cls,
1980 const struct GNUNET_PeerIdentity *other,
1981 const struct GNUNET_MessageHeader *message,
1982 struct GNUNET_TIME_Relative latency,
1985 const struct PutMessage *put;
1989 struct GNUNET_TIME_Absolute expiration;
1990 GNUNET_HashCode query;
1991 struct ProcessReplyClosure prq;
1993 msize = ntohs (message->size);
1994 if (msize < sizeof (struct PutMessage))
1997 return GNUNET_SYSERR;
1999 put = (const struct PutMessage*) message;
2000 dsize = msize - sizeof (struct PutMessage);
2001 type = ntohl (put->type);
2002 expiration = GNUNET_TIME_absolute_ntoh (put->expiration);
2004 /* first, validate! */
2007 case GNUNET_DATASTORE_BLOCKTYPE_DBLOCK:
2008 case GNUNET_DATASTORE_BLOCKTYPE_IBLOCK:
2009 GNUNET_CRYPTO_hash (&put[1], dsize, &query);
2011 case GNUNET_DATASTORE_BLOCKTYPE_KBLOCK:
2013 check_kblock ((const struct KBlock*) &put[1],
2016 return GNUNET_SYSERR;
2018 case GNUNET_DATASTORE_BLOCKTYPE_SBLOCK:
2020 check_sblock ((const struct SBlock*) &put[1],
2024 return GNUNET_SYSERR;
2026 case GNUNET_DATASTORE_BLOCKTYPE_NBLOCK:
2027 // FIXME -- validate NBLOCK!
2031 /* unknown block type */
2032 GNUNET_break_op (0);
2033 return GNUNET_SYSERR;
2037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2038 "Received result for query `%s' from peer `%4s'\n",
2039 GNUNET_h2s (&query),
2040 GNUNET_i2s (other));
2042 GNUNET_STATISTICS_update (stats,
2043 gettext_noop ("# replies received (overall)"),
2046 /* now, lookup 'query' */
2047 prq.data = (const void*) &put[1];
2050 prq.expiration = expiration;
2052 GNUNET_CONTAINER_multihashmap_get_multiple (query_request_map,
2056 // FIXME: if migration is on and load is low,
2057 // queue to store data in datastore;
2058 // use "prq.priority" for that!
2063 /* **************************** P2P GET Handling ************************ */
2067 * Closure for 'check_duplicate_request_{peer,client}'.
2069 struct CheckDuplicateRequestClosure
2072 * The new request we should check if it already exists.
2074 const struct PendingRequest *pr;
2077 * Existing request found by the checker, NULL if none.
2079 struct PendingRequest *have;
2084 * Iterator over entries in the 'query_request_map' that
2085 * tries to see if we have the same request pending from
2086 * the same client already.
2088 * @param cls closure (our 'struct CheckDuplicateRequestClosure')
2089 * @param key current key code (query, ignored, must match)
2090 * @param value value in the hash map (a 'struct PendingRequest'
2091 * that already exists)
2092 * @return GNUNET_YES if we should continue to
2093 * iterate (no match yet)
2094 * GNUNET_NO if not (match found).
2097 check_duplicate_request_client (void *cls,
2098 const GNUNET_HashCode * key,
2101 struct CheckDuplicateRequestClosure *cdc = cls;
2102 struct PendingRequest *have = value;
2104 if (have->client_request_list == NULL)
2106 if ( (cdc->pr->client_request_list->client_list->client == have->client_request_list->client_list->client) &&
2117 * We're processing (local) results for a search request
2118 * from another peer. Pass applicable results to the
2119 * peer and if we are done either clean up (operation
2120 * complete) or forward to other peers (more results possible).
2122 * @param cls our closure (struct LocalGetContext)
2123 * @param key key for the content
2124 * @param size number of bytes in data
2125 * @param data content stored
2126 * @param type type of the content
2127 * @param priority priority of the content
2128 * @param anonymity anonymity-level for the content
2129 * @param expiration expiration time for the content
2130 * @param uid unique identifier for the datum;
2131 * maybe 0 if no unique identifier is available
2134 process_local_reply (void *cls,
2135 const GNUNET_HashCode * key,
2141 struct GNUNET_TIME_Absolute
2145 struct PendingRequest *pr = cls;
2146 struct ProcessReplyClosure prq;
2147 struct CheckDuplicateRequestClosure cdrc;
2148 GNUNET_HashCode dhash;
2149 GNUNET_HashCode mhash;
2150 GNUNET_HashCode query;
2155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2156 "Done processing local replies, forwarding request to other peers.\n");
2159 if (pr->client_request_list != NULL)
2161 GNUNET_SERVER_receive_done (pr->client_request_list->client_list->client,
2163 /* Figure out if this is a duplicate request and possibly
2164 merge 'struct PendingRequest' entries */
2167 GNUNET_CONTAINER_multihashmap_get_multiple (query_request_map,
2169 &check_duplicate_request_client,
2171 if (cdrc.have != NULL)
2174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2175 "Received request for block `%s' twice from client, will only request once.\n",
2176 GNUNET_h2s (&pr->query));
2179 destroy_pending_request (pr);
2184 /* no more results */
2185 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
2186 pr->task = GNUNET_SCHEDULER_add_now (sched,
2187 &forward_request_task,
2191 if (type == GNUNET_DATASTORE_BLOCKTYPE_ONDEMAND)
2194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2195 "Found ONDEMAND block, performing on-demand encoding\n");
2197 GNUNET_STATISTICS_update (stats,
2198 gettext_noop ("# on-demand blocks matched requests"),
2202 GNUNET_FS_handle_on_demand_block (key, size, data, type, priority,
2203 anonymity, expiration, uid,
2204 &process_local_reply,
2206 GNUNET_FS_drq_get_next (GNUNET_YES);
2209 /* check for duplicates */
2210 GNUNET_CRYPTO_hash (data, size, &dhash);
2211 mingle_hash (&dhash,
2214 if ( (pr->bf != NULL) &&
2216 GNUNET_CONTAINER_bloomfilter_test (pr->bf,
2220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2221 "Result from datastore filtered by bloomfilter (duplicate).\n");
2223 GNUNET_STATISTICS_update (stats,
2224 gettext_noop ("# results filtered by query bloomfilter"),
2227 GNUNET_FS_drq_get_next (GNUNET_YES);
2231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2232 "Found result for query `%s' in local datastore\n",
2235 GNUNET_STATISTICS_update (stats,
2236 gettext_noop ("# results found locally"),
2239 pr->results_found++;
2240 if ( (pr->type == GNUNET_DATASTORE_BLOCKTYPE_KBLOCK) ||
2241 (pr->type == GNUNET_DATASTORE_BLOCKTYPE_SBLOCK) ||
2242 (pr->type == GNUNET_DATASTORE_BLOCKTYPE_NBLOCK) )
2247 pr->bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
2251 GNUNET_CONTAINER_bloomfilter_add (pr->bf,
2254 memset (&prq, 0, sizeof (prq));
2256 prq.expiration = expiration;
2258 if ( (type == GNUNET_DATASTORE_BLOCKTYPE_SBLOCK) &&
2259 (GNUNET_OK != check_sblock ((const struct SBlock*) data,
2265 /* FIXME: consider removing the block? */
2266 GNUNET_FS_drq_get_next (GNUNET_YES);
2270 prq.priority = priority;
2271 process_reply (&prq, key, pr);
2273 if ( ( (pr->client_request_list == NULL) &&
2274 ( (GNUNET_YES == test_load_too_high()) ||
2275 (pr->results_found > 5 + 2 * pr->priority) ) ) ||
2276 (type == GNUNET_DATASTORE_BLOCKTYPE_DBLOCK) )
2279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2280 "Unique reply found or load too high, done with request\n");
2282 if (type != GNUNET_DATASTORE_BLOCKTYPE_DBLOCK)
2283 GNUNET_STATISTICS_update (stats,
2284 gettext_noop ("# processing result set cut short due to load"),
2287 GNUNET_FS_drq_get_next (GNUNET_NO);
2290 GNUNET_FS_drq_get_next (GNUNET_YES);
2295 * The priority level imposes a bound on the maximum
2296 * value for the ttl that can be requested.
2298 * @param ttl_in requested ttl
2299 * @param prio given priority
2300 * @return ttl_in if ttl_in is below the limit,
2301 * otherwise the ttl-limit for the given priority
2304 bound_ttl (int32_t ttl_in, uint32_t prio)
2306 unsigned long long allowed;
2310 allowed = ((unsigned long long) prio) * TTL_DECREMENT / 1000;
2311 if (ttl_in > allowed)
2313 if (allowed >= (1 << 30))
2322 * We've received a request with the specified priority. Bound it
2323 * according to how much we trust the given peer.
2325 * @param prio_in requested priority
2326 * @param cp the peer making the request
2327 * @return effective priority
2330 bound_priority (uint32_t prio_in,
2331 struct ConnectedPeer *cp)
2338 * Iterator over entries in the 'query_request_map' that
2339 * tries to see if we have the same request pending from
2340 * the same peer already.
2342 * @param cls closure (our 'struct CheckDuplicateRequestClosure')
2343 * @param key current key code (query, ignored, must match)
2344 * @param value value in the hash map (a 'struct PendingRequest'
2345 * that already exists)
2346 * @return GNUNET_YES if we should continue to
2347 * iterate (no match yet)
2348 * GNUNET_NO if not (match found).
2351 check_duplicate_request_peer (void *cls,
2352 const GNUNET_HashCode * key,
2355 struct CheckDuplicateRequestClosure *cdc = cls;
2356 struct PendingRequest *have = value;
2358 if (cdc->pr->target_pid == have->target_pid)
2368 * Handle P2P "GET" request.
2370 * @param cls closure, always NULL
2371 * @param other the other peer involved (sender or receiver, NULL
2372 * for loopback messages where we are both sender and receiver)
2373 * @param message the actual message
2374 * @param latency reported latency of the connection with 'other'
2375 * @param distance reported distance (DV) to 'other'
2376 * @return GNUNET_OK to keep the connection open,
2377 * GNUNET_SYSERR to close it (signal serious error)
2380 handle_p2p_get (void *cls,
2381 const struct GNUNET_PeerIdentity *other,
2382 const struct GNUNET_MessageHeader *message,
2383 struct GNUNET_TIME_Relative latency,
2386 struct PendingRequest *pr;
2387 struct ConnectedPeer *cp;
2388 struct ConnectedPeer *cps;
2389 struct CheckDuplicateRequestClosure cdc;
2390 struct GNUNET_TIME_Relative timeout;
2392 const struct GetMessage *gm;
2394 const GNUNET_HashCode *opt;
2397 uint32_t ttl_decrement;
2402 msize = ntohs(message->size);
2403 if (msize < sizeof (struct GetMessage))
2405 GNUNET_break_op (0);
2406 return GNUNET_SYSERR;
2408 gm = (const struct GetMessage*) message;
2409 type = ntohl (gm->type);
2412 case GNUNET_DATASTORE_BLOCKTYPE_ANY:
2413 case GNUNET_DATASTORE_BLOCKTYPE_DBLOCK:
2414 case GNUNET_DATASTORE_BLOCKTYPE_IBLOCK:
2415 case GNUNET_DATASTORE_BLOCKTYPE_KBLOCK:
2416 case GNUNET_DATASTORE_BLOCKTYPE_SBLOCK:
2419 GNUNET_break_op (0);
2420 return GNUNET_SYSERR;
2422 bm = ntohl (gm->hash_bitmap);
2430 if (msize < sizeof (struct GetMessage) + bits * sizeof (GNUNET_HashCode))
2432 GNUNET_break_op (0);
2433 return GNUNET_SYSERR;
2435 opt = (const GNUNET_HashCode*) &gm[1];
2436 bfsize = msize - sizeof (struct GetMessage) + bits * sizeof (GNUNET_HashCode);
2437 bm = ntohl (gm->hash_bitmap);
2438 if ( (0 != (bm & GET_MESSAGE_BIT_SKS_NAMESPACE)) &&
2439 (type != GNUNET_DATASTORE_BLOCKTYPE_SBLOCK) )
2441 GNUNET_break_op (0);
2442 return GNUNET_SYSERR;
2445 cps = GNUNET_CONTAINER_multihashmap_get (connected_peers,
2446 &other->hashPubKey);
2447 GNUNET_assert (NULL != cps);
2448 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
2449 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
2456 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
2457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2458 "Failed to find RETURN-TO peer `%4s' in connection set. Dropping query.\n",
2459 GNUNET_i2s ((const struct GNUNET_PeerIdentity*) &opt[bits-1]));
2462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2463 "Failed to find peer `%4s' in connection set. Dropping query.\n",
2464 GNUNET_i2s (other));
2466 GNUNET_STATISTICS_update (stats,
2467 gettext_noop ("# requests dropped due to missing reverse route"),
2470 /* FIXME: try connect? */
2473 /* note that we can really only check load here since otherwise
2474 peers could find out that we are overloaded by not being
2475 disconnected after sending us a malformed query... */
2476 if (GNUNET_YES == test_load_too_high ())
2479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2480 "Dropping query from `%s', this peer is too busy.\n",
2481 GNUNET_i2s (other));
2483 GNUNET_STATISTICS_update (stats,
2484 gettext_noop ("# requests dropped due to high load"),
2491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2492 "Received request for `%s' of type %u from peer `%4s' with flags %u\n",
2493 GNUNET_h2s (&gm->query),
2494 (unsigned int) type,
2498 have_ns = (0 != (bm & GET_MESSAGE_BIT_SKS_NAMESPACE));
2499 pr = GNUNET_malloc (sizeof (struct PendingRequest) +
2500 (have_ns ? sizeof(GNUNET_HashCode) : 0));
2502 pr->namespace = (GNUNET_HashCode*) &pr[1];
2504 pr->mingle = ntohl (gm->filter_mutator);
2505 if (0 != (bm & GET_MESSAGE_BIT_SKS_NAMESPACE))
2506 memcpy (&pr[1], &opt[bits++], sizeof (GNUNET_HashCode));
2507 if (0 != (bm & GET_MESSAGE_BIT_TRANSMIT_TO))
2508 pr->target_pid = GNUNET_PEER_intern ((const struct GNUNET_PeerIdentity*) &opt[bits++]);
2510 pr->anonymity_level = 1;
2511 pr->priority = bound_priority (ntohl (gm->priority), cps);
2512 pr->ttl = bound_ttl (ntohl (gm->ttl), pr->priority);
2513 pr->query = gm->query;
2514 /* decrement ttl (always) */
2515 ttl_decrement = 2 * TTL_DECREMENT +
2516 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2518 if ( (pr->ttl < 0) &&
2519 (pr->ttl - ttl_decrement > 0) )
2522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2523 "Dropping query from `%s' due to TTL underflow.\n",
2524 GNUNET_i2s (other));
2526 GNUNET_STATISTICS_update (stats,
2527 gettext_noop ("# requests dropped due TTL underflow"),
2530 /* integer underflow => drop (should be very rare)! */
2534 pr->ttl -= ttl_decrement;
2535 pr->start_time = GNUNET_TIME_absolute_get ();
2537 /* get bloom filter */
2540 pr->bf = GNUNET_CONTAINER_bloomfilter_init ((const char*) &opt[bits],
2543 pr->bf_size = bfsize;
2548 GNUNET_CONTAINER_multihashmap_get_multiple (query_request_map,
2550 &check_duplicate_request_peer,
2552 if (cdc.have != NULL)
2554 if (cdc.have->start_time.value + cdc.have->ttl >=
2555 pr->start_time.value + pr->ttl)
2557 /* existing request has higher TTL, drop new one! */
2558 cdc.have->priority += pr->priority;
2559 destroy_pending_request (pr);
2561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2562 "Have existing request with higher TTL, dropping new request.\n",
2563 GNUNET_i2s (other));
2565 GNUNET_STATISTICS_update (stats,
2566 gettext_noop ("# requests dropped due to existing request with higher TTL"),
2573 /* existing request has lower TTL, drop old one! */
2574 pr->priority += cdc.have->priority;
2575 /* Possible optimization: if we have applicable pending
2576 replies in 'cdc.have', we might want to move those over
2577 (this is a really rare special-case, so it is not clear
2578 that this would be worth it) */
2579 destroy_pending_request (cdc.have);
2580 /* keep processing 'pr'! */
2585 GNUNET_CONTAINER_multihashmap_put (query_request_map,
2588 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2589 GNUNET_CONTAINER_multihashmap_put (peer_request_map,
2592 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2594 pr->hnode = GNUNET_CONTAINER_heap_insert (requests_by_expiration_heap,
2596 pr->start_time.value + pr->ttl);
2598 GNUNET_STATISTICS_update (stats,
2599 gettext_noop ("# P2P searches active"),
2603 /* calculate change in traffic preference */
2604 preference = (double) pr->priority;
2605 if (preference < QUERY_BANDWIDTH_VALUE)
2606 preference = QUERY_BANDWIDTH_VALUE;
2607 cps->inc_preference += preference;
2609 /* process locally */
2610 if (type == GNUNET_DATASTORE_BLOCKTYPE_DBLOCK)
2611 type = GNUNET_DATASTORE_BLOCKTYPE_ANY; /* to get on-demand as well */
2612 timeout = GNUNET_TIME_relative_multiply (BASIC_DATASTORE_REQUEST_DELAY,
2613 (pr->priority + 1));
2614 pr->drq = GNUNET_FS_drq_get (&gm->query,
2616 &process_local_reply,
2621 /* Are multiple results possible? If so, start processing remotely now! */
2624 case GNUNET_DATASTORE_BLOCKTYPE_DBLOCK:
2625 case GNUNET_DATASTORE_BLOCKTYPE_IBLOCK:
2626 /* only one result, wait for datastore */
2629 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
2630 pr->task = GNUNET_SCHEDULER_add_now (sched,
2631 &forward_request_task,
2635 /* make sure we don't track too many requests */
2636 if (GNUNET_CONTAINER_heap_get_size (requests_by_expiration_heap) > max_pending_requests)
2638 pr = GNUNET_CONTAINER_heap_peek (requests_by_expiration_heap);
2639 destroy_pending_request (pr);
2645 /* **************************** CS GET Handling ************************ */
2649 * Handle START_SEARCH-message (search request from client).
2651 * @param cls closure
2652 * @param client identification of the client
2653 * @param message the actual message
2656 handle_start_search (void *cls,
2657 struct GNUNET_SERVER_Client *client,
2658 const struct GNUNET_MessageHeader *message)
2660 static GNUNET_HashCode all_zeros;
2661 const struct SearchMessage *sm;
2662 struct ClientList *cl;
2663 struct ClientRequestList *crl;
2664 struct PendingRequest *pr;
2669 msize = ntohs (message->size);
2670 if ( (msize < sizeof (struct SearchMessage)) ||
2671 (0 != (msize - sizeof (struct SearchMessage)) % sizeof (GNUNET_HashCode)) )
2674 GNUNET_SERVER_receive_done (client,
2678 sc = (msize - sizeof (struct SearchMessage)) / sizeof (GNUNET_HashCode);
2679 sm = (const struct SearchMessage*) message;
2682 while ( (cl != NULL) &&
2683 (cl->client != client) )
2687 cl = GNUNET_malloc (sizeof (struct ClientList));
2688 cl->client = client;
2689 GNUNET_SERVER_client_keep (client);
2690 cl->next = client_list;
2693 type = ntohl (sm->type);
2695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2696 "Received request for `%s' of type %u from local client\n",
2697 GNUNET_h2s (&sm->query),
2698 (unsigned int) type);
2702 case GNUNET_DATASTORE_BLOCKTYPE_DBLOCK:
2703 case GNUNET_DATASTORE_BLOCKTYPE_IBLOCK:
2704 case GNUNET_DATASTORE_BLOCKTYPE_KBLOCK:
2705 case GNUNET_DATASTORE_BLOCKTYPE_SBLOCK:
2709 GNUNET_SERVER_receive_done (client,
2714 /* detect duplicate KBLOCK requests */
2715 if (type == GNUNET_DATASTORE_BLOCKTYPE_KBLOCK)
2718 while ( (crl != NULL) &&
2719 ( (0 != memcmp (&crl->req->query,
2721 sizeof (GNUNET_HashCode))) ||
2722 (crl->req->type != type) ) )
2727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2728 "Have existing request, merging content-seen lists.\n");
2731 /* Duplicate request (used to send long list of
2732 known/blocked results); merge 'pr->replies_seen'
2733 and update bloom filter */
2734 GNUNET_array_grow (pr->replies_seen,
2735 pr->replies_seen_size,
2736 pr->replies_seen_off + sc);
2737 memcpy (&pr->replies_seen[pr->replies_seen_off],
2739 sc * sizeof (GNUNET_HashCode));
2740 pr->replies_seen_off += sc;
2742 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
2743 pr->bf = refresh_bloomfilter (pr->replies_seen_off,
2747 GNUNET_STATISTICS_update (stats,
2748 gettext_noop ("# client searches updated (merged content seen list)"),
2751 GNUNET_SERVER_receive_done (client,
2756 GNUNET_STATISTICS_update (stats,
2757 gettext_noop ("# client searches active"),
2760 pr = GNUNET_malloc (sizeof (struct PendingRequest) +
2761 ((type == GNUNET_DATASTORE_BLOCKTYPE_SBLOCK)?sizeof(GNUNET_HashCode):0));
2762 crl = GNUNET_malloc (sizeof (struct ClientRequestList));
2763 memset (crl, 0, sizeof (struct ClientRequestList));
2764 crl->client_list = cl;
2765 GNUNET_CONTAINER_DLL_insert (cl->rl_head,
2770 pr->client_request_list = crl;
2771 GNUNET_array_grow (pr->replies_seen,
2772 pr->replies_seen_size,
2774 memcpy (pr->replies_seen,
2776 sc * sizeof (GNUNET_HashCode));
2777 pr->replies_seen_off = sc;
2778 pr->anonymity_level = ntohl (sm->anonymity_level);
2779 pr->bf = refresh_bloomfilter (pr->replies_seen_off,
2783 pr->query = sm->query;
2786 case GNUNET_DATASTORE_BLOCKTYPE_DBLOCK:
2787 case GNUNET_DATASTORE_BLOCKTYPE_IBLOCK:
2788 if (0 != memcmp (&sm->target,
2790 sizeof (GNUNET_HashCode)))
2791 pr->target_pid = GNUNET_PEER_intern ((const struct GNUNET_PeerIdentity*) &sm->target);
2793 case GNUNET_DATASTORE_BLOCKTYPE_SBLOCK:
2794 pr->namespace = (GNUNET_HashCode*) &pr[1];
2795 memcpy (&pr[1], &sm->target, sizeof (GNUNET_HashCode));
2800 GNUNET_CONTAINER_multihashmap_put (query_request_map,
2803 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2804 if (type == GNUNET_DATASTORE_BLOCKTYPE_DBLOCK)
2805 type = GNUNET_DATASTORE_BLOCKTYPE_ANY; /* get on-demand blocks too! */
2806 pr->drq = GNUNET_FS_drq_get (&sm->query,
2808 &process_local_reply,
2810 GNUNET_TIME_UNIT_FOREVER_REL,
2815 /* **************************** Startup ************************ */
2819 * List of handlers for P2P messages
2820 * that we care about.
2822 static struct GNUNET_CORE_MessageHandler p2p_handlers[] =
2825 GNUNET_MESSAGE_TYPE_FS_GET, 0 },
2827 GNUNET_MESSAGE_TYPE_FS_PUT, 0 },
2833 * List of handlers for the messages understood by this
2836 static struct GNUNET_SERVER_MessageHandler handlers[] = {
2837 {&GNUNET_FS_handle_index_start, NULL,
2838 GNUNET_MESSAGE_TYPE_FS_INDEX_START, 0},
2839 {&GNUNET_FS_handle_index_list_get, NULL,
2840 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_GET, sizeof(struct GNUNET_MessageHeader) },
2841 {&GNUNET_FS_handle_unindex, NULL, GNUNET_MESSAGE_TYPE_FS_UNINDEX,
2842 sizeof (struct UnindexMessage) },
2843 {&handle_start_search, NULL, GNUNET_MESSAGE_TYPE_FS_START_SEARCH,
2850 * Process fs requests.
2852 * @param s scheduler to use
2853 * @param server the initialized server
2854 * @param c configuration to use
2857 main_init (struct GNUNET_SCHEDULER_Handle *s,
2858 struct GNUNET_SERVER_Handle *server,
2859 const struct GNUNET_CONFIGURATION_Handle *c)
2863 stats = GNUNET_STATISTICS_create (sched, "fs", cfg);
2864 connected_peers = GNUNET_CONTAINER_multihashmap_create (128); // FIXME: get size from config
2865 query_request_map = GNUNET_CONTAINER_multihashmap_create (128); // FIXME: get size from config
2866 peer_request_map = GNUNET_CONTAINER_multihashmap_create (128); // FIXME: get size from config
2867 requests_by_expiration_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
2868 core = GNUNET_CORE_connect (sched,
2870 GNUNET_TIME_UNIT_FOREVER_REL,
2874 &peer_connect_handler,
2875 &peer_disconnect_handler,
2881 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2882 _("Failed to connect to `%s' service.\n"),
2884 GNUNET_CONTAINER_multihashmap_destroy (connected_peers);
2885 connected_peers = NULL;
2886 GNUNET_CONTAINER_multihashmap_destroy (query_request_map);
2887 query_request_map = NULL;
2888 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
2889 requests_by_expiration_heap = NULL;
2890 GNUNET_CONTAINER_multihashmap_destroy (peer_request_map);
2891 peer_request_map = NULL;
2893 return GNUNET_SYSERR;
2895 GNUNET_SERVER_disconnect_notify (server,
2896 &handle_client_disconnect,
2898 GNUNET_SERVER_add_handlers (server, handlers);
2899 GNUNET_SCHEDULER_add_delayed (sched,
2900 GNUNET_TIME_UNIT_FOREVER_REL,
2908 * Process fs requests.
2910 * @param cls closure
2911 * @param sched scheduler to use
2912 * @param server the initialized server
2913 * @param cfg configuration to use
2917 struct GNUNET_SCHEDULER_Handle *sched,
2918 struct GNUNET_SERVER_Handle *server,
2919 const struct GNUNET_CONFIGURATION_Handle *cfg)
2921 if ( (GNUNET_OK != GNUNET_FS_drq_init (sched, cfg)) ||
2922 (GNUNET_OK != GNUNET_FS_indexing_init (sched, cfg)) ||
2923 (GNUNET_OK != main_init (sched, server, cfg)) )
2925 GNUNET_SCHEDULER_shutdown (sched);
2932 * The main function for the fs service.
2934 * @param argc number of arguments from the command line
2935 * @param argv command line arguments
2936 * @return 0 ok, 1 on error
2939 main (int argc, char *const *argv)
2941 return (GNUNET_OK ==
2942 GNUNET_SERVICE_run (argc,
2945 GNUNET_SERVICE_OPTION_NONE,
2946 &run, NULL)) ? 0 : 1;
2949 /* end of gnunet-service-fs.c */