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 * - code not clear in terms of which function initializes bloomfilter when!
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_skblock
33 * - add content migration support (store locally)
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_drq.h"
47 #include "gnunet-service-fs_indexing.h"
50 #define DEBUG_FS GNUNET_NO
53 * Maximum number of outgoing messages we queue per peer.
54 * FIXME: set to a tiny value for testing; make configurable.
56 #define MAX_QUEUE_PER_PEER 2
59 * Inverse of the probability that we will submit the same query
60 * to the same peer again. If the same peer already got the query
61 * repeatedly recently, the probability is multiplied by the inverse
62 * of this number each time.
64 #define RETRY_PROBABILITY_INV 8
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 (and 32 is a tiny value for testing only).
82 static uint64_t max_pending_requests = 32;
86 * Information we keep for each pending reply. The
87 * actual message follows at the end of this struct.
89 struct PendingMessage;
93 * Function called upon completion of a transmission.
96 * @param pid ID of receiving peer, 0 on transmission error
98 typedef void (*TransmissionContinuation)(void * cls,
103 * Information we keep for each pending message (GET/PUT). The
104 * actual message follows at the end of this struct.
106 struct PendingMessage
109 * This is a doubly-linked list of messages to the same peer.
111 struct PendingMessage *next;
114 * This is a doubly-linked list of messages to the same peer.
116 struct PendingMessage *prev;
119 * Entry in pending message list for this pending message.
121 struct PendingMessageList *pml;
124 * Function to call immediately once we have transmitted this
127 TransmissionContinuation cont;
135 * Size of the reply; actual reply message follows
136 * at the end of this struct.
141 * How important is this message for us?
149 * Information about a peer that we are connected to.
150 * We track data that is useful for determining which
151 * peers should receive our requests. We also keep
152 * a list of messages to transmit to this peer.
158 * List of the last clients for which this peer successfully
161 struct GNUNET_SERVER_Client *last_client_replies[CS2P_SUCCESS_LIST_SIZE];
164 * List of the last PIDs for which
165 * this peer successfully answered a query;
166 * We use 0 to indicate no successful reply.
168 GNUNET_PEER_Id last_p2p_replies[P2P_SUCCESS_LIST_SIZE];
171 * Average delay between sending the peer a request and
172 * getting a reply (only calculated over the requests for
173 * which we actually got a reply). Calculated
174 * as a moving average: new_delay = ((n-1)*last_delay+curr_delay) / n
176 struct GNUNET_TIME_Relative avg_delay;
179 * Handle for an active request for transmission to this
182 struct GNUNET_CORE_TransmitHandle *cth;
185 * Messages (replies, queries, content migration) we would like to
186 * send to this peer in the near future. Sorted by priority, head.
188 struct PendingMessage *pending_messages_head;
191 * Messages (replies, queries, content migration) we would like to
192 * send to this peer in the near future. Sorted by priority, tail.
194 struct PendingMessage *pending_messages_tail;
197 * Average priority of successful replies. Calculated
198 * as a moving average: new_avg = ((n-1)*last_avg+curr_prio) / n
203 * Increase in traffic preference still to be submitted
204 * to the core service for this peer. FIXME: double or 'uint64_t'?
206 double inc_preference;
209 * The peer's identity.
214 * Size of the linked list of 'pending_messages'.
216 unsigned int pending_requests;
219 * Which offset in "last_p2p_replies" will be updated next?
220 * (we go round-robin).
222 unsigned int last_p2p_replies_woff;
225 * Which offset in "last_client_replies" will be updated next?
226 * (we go round-robin).
228 unsigned int last_client_replies_woff;
234 * Information we keep for each pending request. We should try to
235 * keep this struct as small as possible since its memory consumption
236 * is key to how many requests we can have pending at once.
238 struct PendingRequest;
242 * Doubly-linked list of requests we are performing
243 * on behalf of the same client.
245 struct ClientRequestList
249 * This is a doubly-linked list.
251 struct ClientRequestList *next;
254 * This is a doubly-linked list.
256 struct ClientRequestList *prev;
259 * Request this entry represents.
261 struct PendingRequest *req;
264 * Client list this request belongs to.
266 struct ClientList *client_list;
272 * Replies to be transmitted to the client. The actual
273 * response message is allocated after this struct.
275 struct ClientResponseMessage
278 * This is a doubly-linked list.
280 struct ClientResponseMessage *next;
283 * This is a doubly-linked list.
285 struct ClientResponseMessage *prev;
288 * Client list entry this response belongs to.
290 struct ClientList *client_list;
293 * Number of bytes in the response.
300 * Linked list of clients we are performing requests
306 * This is a linked list.
308 struct ClientList *next;
311 * ID of a client making a request, NULL if this entry is for a
314 struct GNUNET_SERVER_Client *client;
317 * Head of list of requests performed on behalf
318 * of this client right now.
320 struct ClientRequestList *rl_head;
323 * Tail of list of requests performed on behalf
324 * of this client right now.
326 struct ClientRequestList *rl_tail;
329 * Head of linked list of responses.
331 struct ClientResponseMessage *res_head;
334 * Tail of linked list of responses.
336 struct ClientResponseMessage *res_tail;
339 * Context for sending replies.
341 struct GNUNET_CONNECTION_TransmitHandle *th;
347 * Doubly-linked list of messages we are performing
348 * due to a pending request.
350 struct PendingMessageList
354 * This is a doubly-linked list of messages on behalf of the same request.
356 struct PendingMessageList *next;
359 * This is a doubly-linked list of messages on behalf of the same request.
361 struct PendingMessageList *prev;
364 * Message this entry represents.
366 struct PendingMessage *pm;
369 * Request this entry belongs to.
371 struct PendingRequest *req;
374 * Peer this message is targeted for.
376 struct ConnectedPeer *target;
382 * Information we keep for each pending request. We should try to
383 * keep this struct as small as possible since its memory consumption
384 * is key to how many requests we can have pending at once.
386 struct PendingRequest
390 * If this request was made by a client, this is our entry in the
391 * client request list; otherwise NULL.
393 struct ClientRequestList *client_request_list;
396 * Entry of peer responsible for this entry (if this request
397 * was made by a peer).
399 struct ConnectedPeer *cp;
402 * If this is a namespace query, pointer to the hash of the public
403 * key of the namespace; otherwise NULL. Pointer will be to the
404 * end of this struct (so no need to free it).
406 const GNUNET_HashCode *namespace;
409 * Bloomfilter we use to filter out replies that we don't care about
410 * (anymore). NULL as long as we are interested in all replies.
412 struct GNUNET_CONTAINER_BloomFilter *bf;
415 * Context of our GNUNET_CORE_peer_change_preference call.
417 struct GNUNET_CORE_InformationRequestContext *irc;
420 * Hash code of all replies that we have seen so far (only valid
421 * if client is not NULL since we only track replies like this for
424 GNUNET_HashCode *replies_seen;
427 * Node in the heap representing this entry; NULL
428 * if we have no heap node.
430 struct GNUNET_CONTAINER_HeapNode *hnode;
433 * Head of list of messages being performed on behalf of this
436 struct PendingMessageList *pending_head;
439 * Tail of list of messages being performed on behalf of this
442 struct PendingMessageList *pending_tail;
445 * When did we first see this request (form this peer), or, if our
446 * client is initiating, when did we last initiate a search?
448 struct GNUNET_TIME_Absolute start_time;
451 * The query that this request is for.
453 GNUNET_HashCode query;
456 * The task responsible for transmitting queries
459 GNUNET_SCHEDULER_TaskIdentifier task;
462 * (Interned) Peer identifier that identifies a preferred target
465 GNUNET_PEER_Id target_pid;
468 * (Interned) Peer identifiers of peers that have already
469 * received our query for this content.
471 GNUNET_PEER_Id *used_pids;
474 * Our entry in the DRQ (non-NULL while we wait for our
475 * turn to interact with the local database).
477 struct DatastoreRequestQueue *drq;
480 * Size of the 'bf' (in bytes).
485 * Desired anonymity level; only valid for requests from a local client.
487 uint32_t anonymity_level;
490 * How many entries in "used_pids" are actually valid?
492 unsigned int used_pids_off;
495 * How long is the "used_pids" array?
497 unsigned int used_pids_size;
500 * Number of results found for this request.
502 unsigned int results_found;
505 * How many entries in "replies_seen" are actually valid?
507 unsigned int replies_seen_off;
510 * How long is the "replies_seen" array?
512 unsigned int replies_seen_size;
515 * Priority with which this request was made. If one of our clients
516 * made the request, then this is the current priority that we are
517 * using when initiating the request. This value is used when
518 * we decide to reward other peers with trust for providing a reply.
523 * Priority points left for us to spend when forwarding this request
526 uint32_t remaining_priority;
529 * Number to mingle hashes for bloom-filter tests with.
534 * TTL with which we saw this request (or, if we initiated, TTL that
535 * we used for the request).
540 * Type of the content that this request is for.
550 static struct GNUNET_SCHEDULER_Handle *sched;
555 static const struct GNUNET_CONFIGURATION_Handle *cfg;
558 * Map of peer identifiers to "struct ConnectedPeer" (for that peer).
560 static struct GNUNET_CONTAINER_MultiHashMap *connected_peers;
563 * Map of peer identifiers to "struct PendingRequest" (for that peer).
565 static struct GNUNET_CONTAINER_MultiHashMap *peer_request_map;
568 * Map of query identifiers to "struct PendingRequest" (for that query).
570 static struct GNUNET_CONTAINER_MultiHashMap *query_request_map;
573 * Heap with the request that will expire next at the top. Contains
574 * pointers of type "struct PendingRequest*"; these will *also* be
575 * aliased from the "requests_by_peer" data structures and the
576 * "requests_by_query" table. Note that requests from our clients
577 * don't expire and are thus NOT in the "requests_by_expiration"
578 * (or the "requests_by_peer" tables).
580 static struct GNUNET_CONTAINER_Heap *requests_by_expiration_heap;
583 * Handle for reporting statistics.
585 static struct GNUNET_STATISTICS_Handle *stats;
588 * Linked list of clients we are currently processing requests for.
590 static struct ClientList *client_list;
593 * Pointer to handle to the core service (points to NULL until we've
596 static struct GNUNET_CORE_Handle *core;
599 /* ******************* clean up functions ************************ */
603 * We're done with a particular message list entry.
604 * Free all associated resources.
606 * @param pml entry to destroy
609 destroy_pending_message_list_entry (struct PendingMessageList *pml)
611 GNUNET_CONTAINER_DLL_remove (pml->req->pending_head,
612 pml->req->pending_tail,
614 GNUNET_CONTAINER_DLL_remove (pml->target->pending_messages_head,
615 pml->target->pending_messages_tail,
617 pml->target->pending_requests--;
618 GNUNET_free (pml->pm);
624 * Destroy the given pending message (and call the respective
627 * @param pm message to destroy
628 * @param tpid id of peer that the message was delivered to, or 0 for none
631 destroy_pending_message (struct PendingMessage *pm,
634 struct PendingMessageList *pml = pm->pml;
635 TransmissionContinuation cont;
638 GNUNET_assert (pml->pm == pm);
639 GNUNET_assert ( (tpid == 0) || (tpid == pml->target->pid) );
641 cont_cls = pm->cont_cls;
642 destroy_pending_message_list_entry (pml);
643 cont (cont_cls, tpid);
648 * We're done processing a particular request.
649 * Free all associated resources.
651 * @param pr request to destroy
654 destroy_pending_request (struct PendingRequest *pr)
656 struct GNUNET_PeerIdentity pid;
658 if (pr->hnode != NULL)
660 GNUNET_CONTAINER_heap_remove_node (requests_by_expiration_heap,
663 GNUNET_STATISTICS_update (stats,
664 gettext_noop ("# P2P searches active"),
670 GNUNET_STATISTICS_update (stats,
671 gettext_noop ("# client searches active"),
675 /* might have already been removed from map in 'process_reply' (if
676 there was a unique reply) or never inserted if it was a
677 duplicate; hence ignore the return value here */
678 (void) GNUNET_CONTAINER_multihashmap_remove (query_request_map,
683 GNUNET_FS_drq_get_cancel (pr->drq);
686 if (pr->client_request_list != NULL)
688 GNUNET_CONTAINER_DLL_remove (pr->client_request_list->client_list->rl_head,
689 pr->client_request_list->client_list->rl_tail,
690 pr->client_request_list);
691 GNUNET_free (pr->client_request_list);
692 pr->client_request_list = NULL;
696 GNUNET_PEER_resolve (pr->cp->pid,
698 (void) GNUNET_CONTAINER_multihashmap_remove (peer_request_map,
705 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
710 GNUNET_CORE_peer_change_preference_cancel (pr->irc);
713 if (pr->replies_seen != NULL)
715 GNUNET_free (pr->replies_seen);
716 pr->replies_seen = NULL;
718 if (pr->task != GNUNET_SCHEDULER_NO_TASK)
720 GNUNET_SCHEDULER_cancel (sched,
722 pr->task = GNUNET_SCHEDULER_NO_TASK;
724 while (NULL != pr->pending_head)
725 destroy_pending_message_list_entry (pr->pending_head);
726 GNUNET_PEER_change_rc (pr->target_pid, -1);
727 if (pr->used_pids != NULL)
729 GNUNET_PEER_decrement_rcs (pr->used_pids, pr->used_pids_off);
730 GNUNET_free (pr->used_pids);
731 pr->used_pids_off = 0;
732 pr->used_pids_size = 0;
733 pr->used_pids = NULL;
740 * Method called whenever a given peer connects.
742 * @param cls closure, not used
743 * @param peer peer identity this notification is about
744 * @param latency reported latency of the connection with 'other'
745 * @param distance reported distance (DV) to 'other'
748 peer_connect_handler (void *cls,
750 GNUNET_PeerIdentity * peer,
751 struct GNUNET_TIME_Relative latency,
754 struct ConnectedPeer *cp;
756 cp = GNUNET_malloc (sizeof (struct ConnectedPeer));
757 cp->pid = GNUNET_PEER_intern (peer);
758 GNUNET_CONTAINER_multihashmap_put (connected_peers,
761 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
766 * Free (each) request made by the peer.
768 * @param cls closure, points to peer that the request belongs to
769 * @param key current key code
770 * @param value value in the hash map
771 * @return GNUNET_YES (we should continue to iterate)
774 destroy_request (void *cls,
775 const GNUNET_HashCode * key,
778 const struct GNUNET_PeerIdentity * peer = cls;
779 struct PendingRequest *pr = value;
781 GNUNET_CONTAINER_multihashmap_remove (peer_request_map,
784 destroy_pending_request (pr);
790 * Method called whenever a peer disconnects.
792 * @param cls closure, not used
793 * @param peer peer identity this notification is about
796 peer_disconnect_handler (void *cls,
798 GNUNET_PeerIdentity * peer)
800 struct ConnectedPeer *cp;
801 struct PendingMessage *pm;
804 GNUNET_CONTAINER_multihashmap_get_multiple (peer_request_map,
808 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
812 for (i=0;i<CS2P_SUCCESS_LIST_SIZE;i++)
814 if (NULL != cp->last_client_replies[i])
816 GNUNET_SERVER_client_drop (cp->last_client_replies[i]);
817 cp->last_client_replies[i] = NULL;
820 GNUNET_CONTAINER_multihashmap_remove (connected_peers,
823 GNUNET_PEER_change_rc (cp->pid, -1);
824 GNUNET_PEER_decrement_rcs (cp->last_p2p_replies, P2P_SUCCESS_LIST_SIZE);
826 GNUNET_CORE_notify_transmit_ready_cancel (cp->cth);
827 while (NULL != (pm = cp->pending_messages_head))
828 destroy_pending_message (pm, 0 /* delivery failed */);
829 GNUNET_break (0 == cp->pending_requests);
835 * Iterator over hash map entries that removes all occurences
836 * of the given 'client' from the 'last_client_replies' of the
837 * given connected peer.
839 * @param cls closure, the 'struct GNUNET_SERVER_Client*' to remove
840 * @param key current key code (unused)
841 * @param value value in the hash map (the 'struct ConnectedPeer*' to change)
842 * @return GNUNET_YES (we should continue to iterate)
845 remove_client_from_last_client_replies (void *cls,
846 const GNUNET_HashCode * key,
849 struct GNUNET_SERVER_Client *client = cls;
850 struct ConnectedPeer *cp = value;
853 for (i=0;i<CS2P_SUCCESS_LIST_SIZE;i++)
855 if (cp->last_client_replies[i] == client)
857 GNUNET_SERVER_client_drop (cp->last_client_replies[i]);
858 cp->last_client_replies[i] = NULL;
866 * A client disconnected. Remove all of its pending queries.
868 * @param cls closure, NULL
869 * @param client identification of the client
872 handle_client_disconnect (void *cls,
873 struct GNUNET_SERVER_Client
876 struct ClientList *pos;
877 struct ClientList *prev;
878 struct ClientRequestList *rcl;
879 struct ClientResponseMessage *creply;
885 while ( (NULL != pos) &&
886 (pos->client != client) )
892 return; /* no requests pending for this client */
893 while (NULL != (rcl = pos->rl_head))
894 destroy_pending_request (rcl->req);
896 client_list = pos->next;
898 prev->next = pos->next;
901 GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th);
904 while (NULL != (creply = pos->res_head))
906 GNUNET_CONTAINER_DLL_remove (pos->res_head,
909 GNUNET_free (creply);
911 GNUNET_SERVER_client_drop (pos->client);
913 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
914 &remove_client_from_last_client_replies,
920 * Iterator to free peer entries.
922 * @param cls closure, unused
923 * @param key current key code
924 * @param value value in the hash map (peer entry)
925 * @return GNUNET_YES (we should continue to iterate)
928 clean_peer (void *cls,
929 const GNUNET_HashCode * key,
932 peer_disconnect_handler (NULL, (const struct GNUNET_PeerIdentity*) key);
938 * Task run during shutdown.
944 shutdown_task (void *cls,
945 const struct GNUNET_SCHEDULER_TaskContext *tc)
947 while (client_list != NULL)
948 handle_client_disconnect (NULL,
949 client_list->client);
950 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
953 GNUNET_break (0 == GNUNET_CONTAINER_heap_get_size (requests_by_expiration_heap));
954 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
955 requests_by_expiration_heap = 0;
956 GNUNET_CONTAINER_multihashmap_destroy (connected_peers);
957 connected_peers = NULL;
958 GNUNET_break (0 == GNUNET_CONTAINER_multihashmap_size (query_request_map));
959 GNUNET_CONTAINER_multihashmap_destroy (query_request_map);
960 query_request_map = NULL;
961 GNUNET_break (0 == GNUNET_CONTAINER_multihashmap_size (peer_request_map));
962 GNUNET_CONTAINER_multihashmap_destroy (peer_request_map);
963 peer_request_map = NULL;
964 GNUNET_assert (NULL != core);
965 GNUNET_CORE_disconnect (core);
969 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
977 /* ******************* Utility functions ******************** */
981 * Transmit the given message by copying it to the target buffer
982 * "buf". "buf" will be NULL and "size" zero if the socket was closed
983 * for writing in the meantime. In that case, do nothing
984 * (the disconnect or shutdown handler will take care of the rest).
985 * If we were able to transmit messages and there are still more
986 * pending, ask core again for further calls to this function.
988 * @param cls closure, pointer to the 'struct ConnectedPeer*'
989 * @param size number of bytes available in buf
990 * @param buf where the callee should write the message
991 * @return number of bytes written to buf
994 transmit_to_peer (void *cls,
995 size_t size, void *buf)
997 struct ConnectedPeer *cp = cls;
999 struct GNUNET_PeerIdentity pid;
1000 struct PendingMessage *pm;
1007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1008 "Dropping message, core too busy.\n");
1013 while ( (NULL != (pm = cp->pending_messages_head) ) &&
1014 (pm->msize <= size) )
1016 memcpy (&cbuf[msize], &pm[1], pm->msize);
1019 destroy_pending_message (pm, cp->pid);
1023 GNUNET_PEER_resolve (cp->pid,
1025 cp->cth = GNUNET_CORE_notify_transmit_ready (core,
1027 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1035 "Transmitting %u bytes to peer %u.\n",
1044 * Add a message to the set of pending messages for the given peer.
1046 * @param cp peer to send message to
1047 * @param pm message to queue
1048 * @param pr request on which behalf this message is being queued
1051 add_to_pending_messages_for_peer (struct ConnectedPeer *cp,
1052 struct PendingMessage *pm,
1053 struct PendingRequest *pr)
1055 struct PendingMessage *pos;
1056 struct PendingMessageList *pml;
1057 struct GNUNET_PeerIdentity pid;
1059 GNUNET_assert (pm->next == NULL);
1060 GNUNET_assert (pm->pml == NULL);
1061 pml = GNUNET_malloc (sizeof (struct PendingMessageList));
1066 GNUNET_CONTAINER_DLL_insert (pr->pending_head,
1069 pos = cp->pending_messages_head;
1070 while ( (pos != NULL) &&
1071 (pm->priority < pos->priority) )
1073 GNUNET_CONTAINER_DLL_insert_after (cp->pending_messages_head,
1074 cp->pending_messages_tail,
1077 cp->pending_requests++;
1078 if (cp->pending_requests > MAX_QUEUE_PER_PEER)
1079 destroy_pending_message (cp->pending_messages_tail, 0);
1080 if (cp->cth == NULL)
1082 /* need to schedule transmission */
1083 GNUNET_PEER_resolve (cp->pid, &pid);
1084 cp->cth = GNUNET_CORE_notify_transmit_ready (core,
1085 cp->pending_messages_head->priority,
1088 cp->pending_messages_head->msize,
1092 if (cp->cth == NULL)
1095 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1096 "Failed to schedule transmission with core!\n");
1098 /* FIXME: call stats (rare, bad case) */
1104 * Mingle hash with the mingle_number to produce different bits.
1107 mingle_hash (const GNUNET_HashCode * in,
1108 int32_t mingle_number,
1109 GNUNET_HashCode * hc)
1113 GNUNET_CRYPTO_hash (&mingle_number,
1116 GNUNET_CRYPTO_hash_xor (&m, in, hc);
1121 * Test if the load on this peer is too high
1122 * to even consider processing the query at
1125 * @return GNUNET_YES if the load is too high, GNUNET_NO otherwise
1128 test_load_too_high ()
1130 return GNUNET_NO; // FIXME
1134 /* ******************* Pending Request Refresh Task ******************** */
1139 * We use a random delay to make the timing of requests less
1140 * predictable. This function returns such a random delay. We add a base
1141 * delay of MAX_CORK_DELAY (1s).
1143 * FIXME: make schedule dependent on the specifics of the request?
1144 * Or bandwidth and number of connected peers and load?
1146 * @return random delay to use for some request, between 1s and 1000+TTL_DECREMENT ms
1148 static struct GNUNET_TIME_Relative
1149 get_processing_delay ()
1152 GNUNET_TIME_relative_add (GNUNET_CONSTANTS_MAX_CORK_DELAY,
1153 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1154 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1160 * We're processing a GET request from another peer and have decided
1161 * to forward it to other peers. This function is called periodically
1162 * and should forward the request to other peers until we have all
1163 * possible replies. If we have transmitted the *only* reply to
1164 * the initiator we should destroy the pending request. If we have
1165 * many replies in the queue to the initiator, we should delay sending
1166 * out more queries until the reply queue has shrunk some.
1168 * @param cls our "struct ProcessGetContext *"
1172 forward_request_task (void *cls,
1173 const struct GNUNET_SCHEDULER_TaskContext *tc);
1177 * Function called after we either failed or succeeded
1178 * at transmitting a query to a peer.
1180 * @param cls the requests "struct PendingRequest*"
1181 * @param tpid ID of receiving peer, 0 on transmission error
1184 transmit_query_continuation (void *cls,
1185 GNUNET_PEER_Id tpid)
1187 struct PendingRequest *pr = cls;
1192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1193 "Transmission of request failed, will try again later.\n");
1195 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
1196 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
1197 get_processing_delay (),
1198 &forward_request_task,
1202 GNUNET_STATISTICS_update (stats,
1203 gettext_noop ("# queries forwarded"),
1206 GNUNET_PEER_change_rc (tpid, 1);
1207 if (pr->used_pids_off == pr->used_pids_size)
1208 GNUNET_array_grow (pr->used_pids,
1210 pr->used_pids_size * 2 + 2);
1211 pr->used_pids[pr->used_pids_off++] = tpid;
1212 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
1213 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
1214 get_processing_delay (),
1215 &forward_request_task,
1221 * How many bytes should a bloomfilter be if we have already seen
1222 * entry_count responses? Note that BLOOMFILTER_K gives us the number
1223 * of bits set per entry. Furthermore, we should not re-size the
1224 * filter too often (to keep it cheap).
1226 * Since other peers will also add entries but not resize the filter,
1227 * we should generally pick a slightly larger size than what the
1228 * strict math would suggest.
1230 * @return must be a power of two and smaller or equal to 2^15.
1233 compute_bloomfilter_size (unsigned int entry_count)
1236 unsigned int ideal = (entry_count * BLOOMFILTER_K) / 4;
1237 uint16_t max = 1 << 15;
1239 if (entry_count > max)
1242 while ((size < max) && (size < ideal))
1251 * Recalculate our bloom filter for filtering replies.
1253 * @param count number of entries we are filtering right now
1254 * @param mingle set to our new mingling value
1255 * @param bf_size set to the size of the bloomfilter
1256 * @param entries the entries to filter
1257 * @return updated bloomfilter, NULL for none
1259 static struct GNUNET_CONTAINER_BloomFilter *
1260 refresh_bloomfilter (unsigned int count,
1263 const GNUNET_HashCode *entries)
1265 struct GNUNET_CONTAINER_BloomFilter *bf;
1268 GNUNET_HashCode mhash;
1272 nsize = compute_bloomfilter_size (count);
1273 *mingle = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, -1);
1275 bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
1278 for (i=0;i<count;i++)
1280 mingle_hash (&entries[i], *mingle, &mhash);
1281 GNUNET_CONTAINER_bloomfilter_add (bf, &mhash);
1288 * Function called after we've tried to reserve a certain amount of
1289 * bandwidth for a reply. Check if we succeeded and if so send our
1292 * @param cls the requests "struct PendingRequest*"
1293 * @param peer identifies the peer
1294 * @param bpm_in set to the current bandwidth limit (receiving) for this peer
1295 * @param bpm_out set to the current bandwidth limit (sending) for this peer
1296 * @param amount set to the amount that was actually reserved or unreserved
1297 * @param preference current traffic preference for the given peer
1300 target_reservation_cb (void *cls,
1302 GNUNET_PeerIdentity * peer,
1303 struct GNUNET_BANDWIDTH_Value32NBO bpm_in,
1304 struct GNUNET_BANDWIDTH_Value32NBO bpm_out,
1306 uint64_t preference)
1308 struct PendingRequest *pr = cls;
1309 struct ConnectedPeer *cp;
1310 struct PendingMessage *pm;
1311 struct GetMessage *gm;
1312 GNUNET_HashCode *ext;
1322 /* error in communication with core, try again later */
1323 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
1324 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
1325 get_processing_delay (),
1326 &forward_request_task,
1330 // (3) transmit, update ttl/priority
1331 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
1335 /* Peer must have just left */
1337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1338 "Selected peer disconnected!\n");
1340 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
1341 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
1342 get_processing_delay (),
1343 &forward_request_task,
1347 no_route = GNUNET_NO;
1348 /* FIXME: check against DBLOCK_SIZE and possibly return
1349 amount to reserve; however, this also needs to work
1350 with testcases which currently start out with a far
1351 too low per-peer bw limit, so they would never send
1352 anything. Big issue. */
1358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1359 "Failed to reserve bandwidth for reply (got %d/%u bytes only)!\n",
1363 GNUNET_STATISTICS_update (stats,
1364 gettext_noop ("# reply bandwidth reservation requests failed"),
1367 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
1368 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
1369 get_processing_delay (),
1370 &forward_request_task,
1372 return; /* this target round failed */
1374 /* FIXME: if we are "quite" busy, we may still want to skip
1375 this round; need more load detection code! */
1376 no_route = GNUNET_YES;
1379 GNUNET_STATISTICS_update (stats,
1380 gettext_noop ("# requests forwarded"),
1383 /* build message and insert message into priority queue */
1385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1386 "Forwarding request `%s' to `%4s'!\n",
1387 GNUNET_h2s (&pr->query),
1392 if (GNUNET_YES == no_route)
1394 bm |= GET_MESSAGE_BIT_RETURN_TO;
1397 if (pr->namespace != NULL)
1399 bm |= GET_MESSAGE_BIT_SKS_NAMESPACE;
1402 if (pr->target_pid != 0)
1404 bm |= GET_MESSAGE_BIT_TRANSMIT_TO;
1407 msize = sizeof (struct GetMessage) + pr->bf_size + k * sizeof(GNUNET_HashCode);
1408 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1409 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1411 gm = (struct GetMessage*) &pm[1];
1412 gm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_GET);
1413 gm->header.size = htons (msize);
1414 gm->type = htonl (pr->type);
1415 pr->remaining_priority /= 2;
1416 gm->priority = htonl (pr->remaining_priority);
1417 gm->ttl = htonl (pr->ttl);
1418 gm->filter_mutator = htonl(pr->mingle);
1419 gm->hash_bitmap = htonl (bm);
1420 gm->query = pr->query;
1421 ext = (GNUNET_HashCode*) &gm[1];
1423 if (GNUNET_YES == no_route)
1424 GNUNET_PEER_resolve (pr->cp->pid, (struct GNUNET_PeerIdentity*) &ext[k++]);
1425 if (pr->namespace != NULL)
1426 memcpy (&ext[k++], pr->namespace, sizeof (GNUNET_HashCode));
1427 if (pr->target_pid != 0)
1428 GNUNET_PEER_resolve (pr->target_pid, (struct GNUNET_PeerIdentity*) &ext[k++]);
1429 bfdata = (char *) &ext[k];
1431 GNUNET_CONTAINER_bloomfilter_get_raw_data (pr->bf,
1434 pm->cont = &transmit_query_continuation;
1436 add_to_pending_messages_for_peer (cp, pm, pr);
1441 * Closure used for "target_peer_select_cb".
1443 struct PeerSelectionContext
1446 * The request for which we are selecting
1449 struct PendingRequest *pr;
1452 * Current "prime" target.
1454 struct GNUNET_PeerIdentity target;
1457 * How much do we like this target?
1459 double target_score;
1465 * Function called for each connected peer to determine
1466 * which one(s) would make good targets for forwarding.
1468 * @param cls closure (struct PeerSelectionContext)
1469 * @param key current key code (peer identity)
1470 * @param value value in the hash map (struct ConnectedPeer)
1471 * @return GNUNET_YES if we should continue to
1476 target_peer_select_cb (void *cls,
1477 const GNUNET_HashCode * key,
1480 struct PeerSelectionContext *psc = cls;
1481 struct ConnectedPeer *cp = value;
1482 struct PendingRequest *pr = psc->pr;
1486 /* 1) check if we have already (recently) forwarded to this peer */
1487 for (i=0;i<pr->used_pids_off;i++)
1488 if ( (pr->used_pids[i] == cp->pid) &&
1489 (0 != GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1490 RETRY_PROBABILITY_INV)) )
1491 return GNUNET_YES; /* skip */
1492 // 2) calculate how much we'd like to forward to this peer
1493 score = 42; // FIXME!
1494 // FIXME: also need API to gather data on responsiveness
1495 // of this peer (we have fields for that in 'cp', but
1496 // they are never set!)
1498 /* store best-fit in closure */
1499 if (score > psc->target_score)
1501 psc->target_score = score;
1502 psc->target.hashPubKey = *key;
1509 * We're processing a GET request from another peer and have decided
1510 * to forward it to other peers. This function is called periodically
1511 * and should forward the request to other peers until we have all
1512 * possible replies. If we have transmitted the *only* reply to
1513 * the initiator we should destroy the pending request. If we have
1514 * many replies in the queue to the initiator, we should delay sending
1515 * out more queries until the reply queue has shrunk some.
1517 * @param cls our "struct ProcessGetContext *"
1521 forward_request_task (void *cls,
1522 const struct GNUNET_SCHEDULER_TaskContext *tc)
1524 struct PendingRequest *pr = cls;
1525 struct PeerSelectionContext psc;
1526 struct ConnectedPeer *cp;
1528 pr->task = GNUNET_SCHEDULER_NO_TASK;
1529 if (pr->irc != NULL)
1530 return; /* already pending */
1531 /* (1) select target */
1533 psc.target_score = DBL_MIN;
1534 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
1535 &target_peer_select_cb,
1537 if (psc.target_score == DBL_MIN)
1540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1541 "No peer selected for forwarding of query `%s'!\n",
1542 GNUNET_h2s (&pr->query));
1544 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
1545 get_processing_delay (),
1546 &forward_request_task,
1548 return; /* nobody selected */
1551 /* (2) reserve reply bandwidth */
1552 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
1553 &psc.target.hashPubKey);
1554 pr->irc = GNUNET_CORE_peer_change_preference (sched, cfg,
1556 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1557 GNUNET_BANDWIDTH_value_init ((uint32_t) -1 /* no limit */),
1559 (uint64_t) cp->inc_preference,
1560 &target_reservation_cb,
1562 cp->inc_preference = 0.0;
1566 /* **************************** P2P PUT Handling ************************ */
1570 * Function called after we either failed or succeeded
1571 * at transmitting a reply to a peer.
1573 * @param cls the requests "struct PendingRequest*"
1574 * @param tpid ID of receiving peer, 0 on transmission error
1577 transmit_reply_continuation (void *cls,
1578 GNUNET_PEER_Id tpid)
1580 struct PendingRequest *pr = cls;
1584 case GNUNET_DATASTORE_BLOCKTYPE_DBLOCK:
1585 case GNUNET_DATASTORE_BLOCKTYPE_IBLOCK:
1586 /* only one reply expected, done with the request! */
1587 destroy_pending_request (pr);
1589 case GNUNET_DATASTORE_BLOCKTYPE_ANY:
1590 case GNUNET_DATASTORE_BLOCKTYPE_KBLOCK:
1591 case GNUNET_DATASTORE_BLOCKTYPE_SBLOCK:
1601 * Check if the given KBlock is well-formed.
1603 * @param kb the kblock data (or at least "dsize" bytes claiming to be one)
1604 * @param dsize size of "kb" in bytes; check for < sizeof(struct KBlock)!
1605 * @param query where to store the query that this block answers
1606 * @return GNUNET_OK if this is actually a well-formed KBlock
1609 check_kblock (const struct KBlock *kb,
1611 GNUNET_HashCode *query)
1613 if (dsize < sizeof (struct KBlock))
1615 GNUNET_break_op (0);
1616 return GNUNET_SYSERR;
1618 if (dsize - sizeof (struct KBlock) !=
1619 ntohl (kb->purpose.size)
1620 - sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose)
1621 - sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) )
1623 GNUNET_break_op (0);
1624 return GNUNET_SYSERR;
1627 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_KBLOCK,
1632 GNUNET_break_op (0);
1633 return GNUNET_SYSERR;
1636 GNUNET_CRYPTO_hash (&kb->keyspace,
1637 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1644 * Check if the given SBlock is well-formed.
1646 * @param sb the sblock data (or at least "dsize" bytes claiming to be one)
1647 * @param dsize size of "kb" in bytes; check for < sizeof(struct SBlock)!
1648 * @param query where to store the query that this block answers
1649 * @param namespace where to store the namespace that this block belongs to
1650 * @return GNUNET_OK if this is actually a well-formed SBlock
1653 check_sblock (const struct SBlock *sb,
1655 GNUNET_HashCode *query,
1656 GNUNET_HashCode *namespace)
1658 if (dsize < sizeof (struct SBlock))
1660 GNUNET_break_op (0);
1661 return GNUNET_SYSERR;
1664 ntohl (sb->purpose.size) + sizeof (struct GNUNET_CRYPTO_RsaSignature))
1666 GNUNET_break_op (0);
1667 return GNUNET_SYSERR;
1670 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_SBLOCK,
1675 GNUNET_break_op (0);
1676 return GNUNET_SYSERR;
1679 *query = sb->identifier;
1680 if (namespace != NULL)
1681 GNUNET_CRYPTO_hash (&sb->subspace,
1682 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1689 * Transmit the given message by copying it to the target buffer
1690 * "buf". "buf" will be NULL and "size" zero if the socket was closed
1691 * for writing in the meantime. In that case, do nothing
1692 * (the disconnect or shutdown handler will take care of the rest).
1693 * If we were able to transmit messages and there are still more
1694 * pending, ask core again for further calls to this function.
1696 * @param cls closure, pointer to the 'struct ClientList*'
1697 * @param size number of bytes available in buf
1698 * @param buf where the callee should write the message
1699 * @return number of bytes written to buf
1702 transmit_to_client (void *cls,
1703 size_t size, void *buf)
1705 struct ClientList *cl = cls;
1707 struct ClientResponseMessage *creply;
1714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1715 "Not sending reply, client communication problem.\n");
1720 while ( (NULL != (creply = cl->res_head) ) &&
1721 (creply->msize <= size) )
1723 memcpy (&cbuf[msize], &creply[1], creply->msize);
1724 msize += creply->msize;
1725 size -= creply->msize;
1726 GNUNET_CONTAINER_DLL_remove (cl->res_head,
1729 GNUNET_free (creply);
1732 cl->th = GNUNET_SERVER_notify_transmit_ready (cl->client,
1734 GNUNET_TIME_UNIT_FOREVER_REL,
1735 &transmit_to_client,
1738 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1739 "Transmitted %u bytes to client\n",
1740 (unsigned int) msize);
1747 * Closure for "process_reply" function.
1749 struct ProcessReplyClosure
1752 * The data for the reply.
1756 // FIXME: add 'struct ConnectedPeer' to track 'last_xxx_replies' here!
1759 * When the reply expires.
1761 struct GNUNET_TIME_Absolute expiration;
1769 * Namespace that this reply belongs to
1770 * (if it is of type SBLOCK).
1772 GNUNET_HashCode namespace;
1775 * Type of the block.
1780 * How much was this reply worth to us?
1787 * We have received a reply; handle it!
1789 * @param cls response (struct ProcessReplyClosure)
1790 * @param key our query
1791 * @param value value in the hash map (info about the query)
1792 * @return GNUNET_YES (we should continue to iterate)
1795 process_reply (void *cls,
1796 const GNUNET_HashCode * key,
1799 struct ProcessReplyClosure *prq = cls;
1800 struct PendingRequest *pr = value;
1801 struct PendingMessage *reply;
1802 struct ClientResponseMessage *creply;
1803 struct ClientList *cl;
1804 struct PutMessage *pm;
1805 struct ConnectedPeer *cp;
1806 GNUNET_HashCode chash;
1807 GNUNET_HashCode mhash;
1812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1813 "Matched result (type %u) for query `%s' with pending request\n",
1814 (unsigned int) prq->type,
1817 GNUNET_STATISTICS_update (stats,
1818 gettext_noop ("# replies received and matched"),
1821 do_remove = GNUNET_NO;
1822 GNUNET_CRYPTO_hash (prq->data,
1827 case GNUNET_DATASTORE_BLOCKTYPE_DBLOCK:
1828 case GNUNET_DATASTORE_BLOCKTYPE_IBLOCK:
1829 /* only possible reply, stop requesting! */
1830 while (NULL != pr->pending_head)
1831 destroy_pending_message_list_entry (pr->pending_head);
1832 if (pr->drq != NULL)
1834 if (pr->client_request_list != NULL)
1835 GNUNET_SERVER_receive_done (pr->client_request_list->client_list->client,
1837 GNUNET_FS_drq_get_cancel (pr->drq);
1840 do_remove = GNUNET_YES;
1842 case GNUNET_DATASTORE_BLOCKTYPE_SBLOCK:
1843 if (pr->namespace == NULL)
1848 if (0 != memcmp (pr->namespace,
1850 sizeof (GNUNET_HashCode)))
1852 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1853 _("Reply mismatched in terms of namespace. Discarded.\n"));
1854 return GNUNET_YES; /* wrong namespace */
1856 /* then: fall-through! */
1857 case GNUNET_DATASTORE_BLOCKTYPE_KBLOCK:
1860 mingle_hash (&chash, pr->mingle, &mhash);
1861 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (pr->bf,
1864 GNUNET_STATISTICS_update (stats,
1865 gettext_noop ("# duplicate replies discarded (bloomfilter)"),
1869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1870 "Duplicate response `%s', discarding.\n",
1871 GNUNET_h2s (&mhash));
1873 return GNUNET_YES; /* duplicate */
1876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1877 "New response `%s', adding to filter.\n",
1878 GNUNET_h2s (&mhash));
1880 GNUNET_CONTAINER_bloomfilter_add (pr->bf,
1883 if (pr->client_request_list != NULL)
1885 if (pr->replies_seen_size == pr->replies_seen_off)
1887 GNUNET_array_grow (pr->replies_seen,
1888 pr->replies_seen_size,
1889 pr->replies_seen_size * 2 + 4);
1891 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
1892 pr->bf = refresh_bloomfilter (pr->replies_seen_off,
1897 pr->replies_seen[pr->replies_seen_off++] = chash;
1900 case GNUNET_DATASTORE_BLOCKTYPE_NBLOCK:
1901 // FIXME: any checks against duplicates for NBlocks?
1907 prq->priority += pr->remaining_priority;
1908 pr->remaining_priority = 0;
1909 if (pr->client_request_list != NULL)
1912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1913 "Transmitting result for query `%s' to local client\n",
1916 GNUNET_STATISTICS_update (stats,
1917 gettext_noop ("# replies received for local clients"),
1920 cl = pr->client_request_list->client_list;
1921 msize = sizeof (struct PutMessage) + prq->size;
1922 creply = GNUNET_malloc (msize + sizeof (struct ClientResponseMessage));
1923 creply->msize = msize;
1924 creply->client_list = cl;
1925 GNUNET_CONTAINER_DLL_insert_after (cl->res_head,
1929 pm = (struct PutMessage*) &creply[1];
1930 pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
1931 pm->header.size = htons (msize);
1932 pm->type = htonl (prq->type);
1933 pm->expiration = GNUNET_TIME_absolute_hton (prq->expiration);
1934 memcpy (&pm[1], prq->data, prq->size);
1938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1939 "Transmitting result for query `%s' to client\n",
1942 cl->th = GNUNET_SERVER_notify_transmit_ready (cl->client,
1944 GNUNET_TIME_UNIT_FOREVER_REL,
1945 &transmit_to_client,
1948 GNUNET_break (cl->th != NULL);
1954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1955 "Transmitting result for query `%s' to other peer (PID=%u)\n",
1957 (unsigned int) cp->pid);
1959 GNUNET_STATISTICS_update (stats,
1960 gettext_noop ("# replies received for other peers"),
1963 msize = sizeof (struct PutMessage) + prq->size;
1964 reply = GNUNET_malloc (msize + sizeof (struct PendingMessage));
1965 reply->cont = &transmit_reply_continuation;
1966 reply->cont_cls = pr;
1967 reply->msize = msize;
1968 reply->priority = (uint32_t) -1; /* send replies first! */
1969 pm = (struct PutMessage*) &reply[1];
1970 pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
1971 pm->header.size = htons (msize);
1972 pm->type = htonl (prq->type);
1973 pm->expiration = GNUNET_TIME_absolute_hton (prq->expiration);
1974 memcpy (&pm[1], prq->data, prq->size);
1975 add_to_pending_messages_for_peer (cp, reply, pr);
1977 if (GNUNET_YES == do_remove)
1980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1981 "Removing request `%s' from request map (has been satisfied)\n",
1984 GNUNET_break (GNUNET_YES ==
1985 GNUNET_CONTAINER_multihashmap_remove (query_request_map,
1988 // FIXME: request somehow does not fully disappear; how to fix?
1989 // destroy_pending_request (pr); (not like this!)
1991 // FIXME: implement hot-path routing statistics keeping!
1997 * Handle P2P "PUT" message.
1999 * @param cls closure, always NULL
2000 * @param other the other peer involved (sender or receiver, NULL
2001 * for loopback messages where we are both sender and receiver)
2002 * @param message the actual message
2003 * @param latency reported latency of the connection with 'other'
2004 * @param distance reported distance (DV) to 'other'
2005 * @return GNUNET_OK to keep the connection open,
2006 * GNUNET_SYSERR to close it (signal serious error)
2009 handle_p2p_put (void *cls,
2010 const struct GNUNET_PeerIdentity *other,
2011 const struct GNUNET_MessageHeader *message,
2012 struct GNUNET_TIME_Relative latency,
2015 const struct PutMessage *put;
2019 struct GNUNET_TIME_Absolute expiration;
2020 GNUNET_HashCode query;
2021 struct ProcessReplyClosure prq;
2023 msize = ntohs (message->size);
2024 if (msize < sizeof (struct PutMessage))
2027 return GNUNET_SYSERR;
2029 put = (const struct PutMessage*) message;
2030 dsize = msize - sizeof (struct PutMessage);
2031 type = ntohl (put->type);
2032 expiration = GNUNET_TIME_absolute_ntoh (put->expiration);
2034 /* first, validate! */
2037 case GNUNET_DATASTORE_BLOCKTYPE_DBLOCK:
2038 case GNUNET_DATASTORE_BLOCKTYPE_IBLOCK:
2039 GNUNET_CRYPTO_hash (&put[1], dsize, &query);
2041 case GNUNET_DATASTORE_BLOCKTYPE_KBLOCK:
2043 check_kblock ((const struct KBlock*) &put[1],
2046 return GNUNET_SYSERR;
2048 case GNUNET_DATASTORE_BLOCKTYPE_SBLOCK:
2050 check_sblock ((const struct SBlock*) &put[1],
2054 return GNUNET_SYSERR;
2056 case GNUNET_DATASTORE_BLOCKTYPE_NBLOCK:
2057 // FIXME -- validate NBLOCK!
2061 /* unknown block type */
2062 GNUNET_break_op (0);
2063 return GNUNET_SYSERR;
2067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2068 "Received result for query `%s' from peer `%4s'\n",
2069 GNUNET_h2s (&query),
2070 GNUNET_i2s (other));
2072 GNUNET_STATISTICS_update (stats,
2073 gettext_noop ("# replies received (overall)"),
2076 /* now, lookup 'query' */
2077 prq.data = (const void*) &put[1];
2080 prq.expiration = expiration;
2082 GNUNET_CONTAINER_multihashmap_get_multiple (query_request_map,
2086 // FIXME: if migration is on and load is low,
2087 // queue to store data in datastore;
2088 // use "prq.priority" for that!
2093 /* **************************** P2P GET Handling ************************ */
2097 * Closure for 'check_duplicate_request_{peer,client}'.
2099 struct CheckDuplicateRequestClosure
2102 * The new request we should check if it already exists.
2104 const struct PendingRequest *pr;
2107 * Existing request found by the checker, NULL if none.
2109 struct PendingRequest *have;
2114 * Iterator over entries in the 'query_request_map' that
2115 * tries to see if we have the same request pending from
2116 * the same client already.
2118 * @param cls closure (our 'struct CheckDuplicateRequestClosure')
2119 * @param key current key code (query, ignored, must match)
2120 * @param value value in the hash map (a 'struct PendingRequest'
2121 * that already exists)
2122 * @return GNUNET_YES if we should continue to
2123 * iterate (no match yet)
2124 * GNUNET_NO if not (match found).
2127 check_duplicate_request_client (void *cls,
2128 const GNUNET_HashCode * key,
2131 struct CheckDuplicateRequestClosure *cdc = cls;
2132 struct PendingRequest *have = value;
2134 if (have->client_request_list == NULL)
2136 if ( (cdc->pr->client_request_list->client_list->client == have->client_request_list->client_list->client) &&
2147 * We're processing (local) results for a search request
2148 * from another peer. Pass applicable results to the
2149 * peer and if we are done either clean up (operation
2150 * complete) or forward to other peers (more results possible).
2152 * @param cls our closure (struct LocalGetContext)
2153 * @param key key for the content
2154 * @param size number of bytes in data
2155 * @param data content stored
2156 * @param type type of the content
2157 * @param priority priority of the content
2158 * @param anonymity anonymity-level for the content
2159 * @param expiration expiration time for the content
2160 * @param uid unique identifier for the datum;
2161 * maybe 0 if no unique identifier is available
2164 process_local_reply (void *cls,
2165 const GNUNET_HashCode * key,
2171 struct GNUNET_TIME_Absolute
2175 struct PendingRequest *pr = cls;
2176 struct ProcessReplyClosure prq;
2177 struct CheckDuplicateRequestClosure cdrc;
2178 GNUNET_HashCode dhash;
2179 GNUNET_HashCode mhash;
2180 GNUNET_HashCode query;
2185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2186 "Done processing local replies, forwarding request to other peers.\n");
2189 if (pr->client_request_list != NULL)
2191 GNUNET_SERVER_receive_done (pr->client_request_list->client_list->client,
2193 /* Figure out if this is a duplicate request and possibly
2194 merge 'struct PendingRequest' entries */
2197 GNUNET_CONTAINER_multihashmap_get_multiple (query_request_map,
2199 &check_duplicate_request_client,
2201 if (cdrc.have != NULL)
2204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2205 "Received request for block `%s' twice from client, will only request once.\n",
2206 GNUNET_h2s (&pr->query));
2209 destroy_pending_request (pr);
2214 /* no more results */
2215 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
2216 pr->task = GNUNET_SCHEDULER_add_now (sched,
2217 &forward_request_task,
2222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2223 "New local response to `%s' of type %u.\n",
2227 if (type == GNUNET_DATASTORE_BLOCKTYPE_ONDEMAND)
2230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2231 "Found ONDEMAND block, performing on-demand encoding\n");
2233 GNUNET_STATISTICS_update (stats,
2234 gettext_noop ("# on-demand blocks matched requests"),
2238 GNUNET_FS_handle_on_demand_block (key, size, data, type, priority,
2239 anonymity, expiration, uid,
2240 &process_local_reply,
2242 GNUNET_FS_drq_get_next (GNUNET_YES);
2245 /* check for duplicates */
2246 GNUNET_CRYPTO_hash (data, size, &dhash);
2247 mingle_hash (&dhash,
2250 if ( (pr->bf != NULL) &&
2252 GNUNET_CONTAINER_bloomfilter_test (pr->bf,
2256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2257 "Result from datastore filtered by bloomfilter (duplicate).\n");
2259 GNUNET_STATISTICS_update (stats,
2260 gettext_noop ("# results filtered by query bloomfilter"),
2263 GNUNET_FS_drq_get_next (GNUNET_YES);
2267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2268 "Found result for query `%s' in local datastore\n",
2271 GNUNET_STATISTICS_update (stats,
2272 gettext_noop ("# results found locally"),
2275 pr->results_found++;
2276 if ( (pr->type == GNUNET_DATASTORE_BLOCKTYPE_KBLOCK) ||
2277 (pr->type == GNUNET_DATASTORE_BLOCKTYPE_SBLOCK) ||
2278 (pr->type == GNUNET_DATASTORE_BLOCKTYPE_NBLOCK) )
2283 pr->bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
2288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2289 "New local response `%s', adding to filter.\n",
2290 GNUNET_h2s (&mhash));
2293 /* this would break stuff since we will check the bf later
2294 again (and would then discard the reply!) */
2295 GNUNET_CONTAINER_bloomfilter_add (pr->bf,
2299 memset (&prq, 0, sizeof (prq));
2301 prq.expiration = expiration;
2303 if ( (type == GNUNET_DATASTORE_BLOCKTYPE_SBLOCK) &&
2304 (GNUNET_OK != check_sblock ((const struct SBlock*) data,
2310 /* FIXME: consider removing the block? */
2311 GNUNET_FS_drq_get_next (GNUNET_YES);
2315 prq.priority = priority;
2316 process_reply (&prq, key, pr);
2318 if ( ( (pr->client_request_list == NULL) &&
2319 ( (GNUNET_YES == test_load_too_high()) ||
2320 (pr->results_found > 5 + 2 * pr->priority) ) ) ||
2321 (type == GNUNET_DATASTORE_BLOCKTYPE_DBLOCK) )
2324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2325 "Unique reply found or load too high, done with request\n");
2327 if (type != GNUNET_DATASTORE_BLOCKTYPE_DBLOCK)
2328 GNUNET_STATISTICS_update (stats,
2329 gettext_noop ("# processing result set cut short due to load"),
2332 GNUNET_FS_drq_get_next (GNUNET_NO);
2335 GNUNET_FS_drq_get_next (GNUNET_YES);
2340 * The priority level imposes a bound on the maximum
2341 * value for the ttl that can be requested.
2343 * @param ttl_in requested ttl
2344 * @param prio given priority
2345 * @return ttl_in if ttl_in is below the limit,
2346 * otherwise the ttl-limit for the given priority
2349 bound_ttl (int32_t ttl_in, uint32_t prio)
2351 unsigned long long allowed;
2355 allowed = ((unsigned long long) prio) * TTL_DECREMENT / 1000;
2356 if (ttl_in > allowed)
2358 if (allowed >= (1 << 30))
2367 * We've received a request with the specified priority. Bound it
2368 * according to how much we trust the given peer.
2370 * @param prio_in requested priority
2371 * @param cp the peer making the request
2372 * @return effective priority
2375 bound_priority (uint32_t prio_in,
2376 struct ConnectedPeer *cp)
2383 * Iterator over entries in the 'query_request_map' that
2384 * tries to see if we have the same request pending from
2385 * the same peer already.
2387 * @param cls closure (our 'struct CheckDuplicateRequestClosure')
2388 * @param key current key code (query, ignored, must match)
2389 * @param value value in the hash map (a 'struct PendingRequest'
2390 * that already exists)
2391 * @return GNUNET_YES if we should continue to
2392 * iterate (no match yet)
2393 * GNUNET_NO if not (match found).
2396 check_duplicate_request_peer (void *cls,
2397 const GNUNET_HashCode * key,
2400 struct CheckDuplicateRequestClosure *cdc = cls;
2401 struct PendingRequest *have = value;
2403 if (cdc->pr->target_pid == have->target_pid)
2413 * Handle P2P "GET" request.
2415 * @param cls closure, always NULL
2416 * @param other the other peer involved (sender or receiver, NULL
2417 * for loopback messages where we are both sender and receiver)
2418 * @param message the actual message
2419 * @param latency reported latency of the connection with 'other'
2420 * @param distance reported distance (DV) to 'other'
2421 * @return GNUNET_OK to keep the connection open,
2422 * GNUNET_SYSERR to close it (signal serious error)
2425 handle_p2p_get (void *cls,
2426 const struct GNUNET_PeerIdentity *other,
2427 const struct GNUNET_MessageHeader *message,
2428 struct GNUNET_TIME_Relative latency,
2431 struct PendingRequest *pr;
2432 struct ConnectedPeer *cp;
2433 struct ConnectedPeer *cps;
2434 struct CheckDuplicateRequestClosure cdc;
2435 struct GNUNET_TIME_Relative timeout;
2437 const struct GetMessage *gm;
2439 const GNUNET_HashCode *opt;
2442 uint32_t ttl_decrement;
2447 msize = ntohs(message->size);
2448 if (msize < sizeof (struct GetMessage))
2450 GNUNET_break_op (0);
2451 return GNUNET_SYSERR;
2453 gm = (const struct GetMessage*) message;
2454 type = ntohl (gm->type);
2457 case GNUNET_DATASTORE_BLOCKTYPE_ANY:
2458 case GNUNET_DATASTORE_BLOCKTYPE_DBLOCK:
2459 case GNUNET_DATASTORE_BLOCKTYPE_IBLOCK:
2460 case GNUNET_DATASTORE_BLOCKTYPE_KBLOCK:
2461 case GNUNET_DATASTORE_BLOCKTYPE_SBLOCK:
2464 GNUNET_break_op (0);
2465 return GNUNET_SYSERR;
2467 bm = ntohl (gm->hash_bitmap);
2475 if (msize < sizeof (struct GetMessage) + bits * sizeof (GNUNET_HashCode))
2477 GNUNET_break_op (0);
2478 return GNUNET_SYSERR;
2480 opt = (const GNUNET_HashCode*) &gm[1];
2481 bfsize = msize - sizeof (struct GetMessage) + bits * sizeof (GNUNET_HashCode);
2482 bm = ntohl (gm->hash_bitmap);
2483 if ( (0 != (bm & GET_MESSAGE_BIT_SKS_NAMESPACE)) &&
2484 (type != GNUNET_DATASTORE_BLOCKTYPE_SBLOCK) )
2486 GNUNET_break_op (0);
2487 return GNUNET_SYSERR;
2490 cps = GNUNET_CONTAINER_multihashmap_get (connected_peers,
2491 &other->hashPubKey);
2492 GNUNET_assert (NULL != cps);
2493 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
2494 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
2501 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
2502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2503 "Failed to find RETURN-TO peer `%4s' in connection set. Dropping query.\n",
2504 GNUNET_i2s ((const struct GNUNET_PeerIdentity*) &opt[bits-1]));
2507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2508 "Failed to find peer `%4s' in connection set. Dropping query.\n",
2509 GNUNET_i2s (other));
2511 GNUNET_STATISTICS_update (stats,
2512 gettext_noop ("# requests dropped due to missing reverse route"),
2515 /* FIXME: try connect? */
2518 /* note that we can really only check load here since otherwise
2519 peers could find out that we are overloaded by not being
2520 disconnected after sending us a malformed query... */
2521 if (GNUNET_YES == test_load_too_high ())
2524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2525 "Dropping query from `%s', this peer is too busy.\n",
2526 GNUNET_i2s (other));
2528 GNUNET_STATISTICS_update (stats,
2529 gettext_noop ("# requests dropped due to high load"),
2536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2537 "Received request for `%s' of type %u from peer `%4s' with flags %u\n",
2538 GNUNET_h2s (&gm->query),
2539 (unsigned int) type,
2543 have_ns = (0 != (bm & GET_MESSAGE_BIT_SKS_NAMESPACE));
2544 pr = GNUNET_malloc (sizeof (struct PendingRequest) +
2545 (have_ns ? sizeof(GNUNET_HashCode) : 0));
2547 pr->namespace = (GNUNET_HashCode*) &pr[1];
2549 pr->mingle = ntohl (gm->filter_mutator);
2550 if (0 != (bm & GET_MESSAGE_BIT_SKS_NAMESPACE))
2551 memcpy (&pr[1], &opt[bits++], sizeof (GNUNET_HashCode));
2552 if (0 != (bm & GET_MESSAGE_BIT_TRANSMIT_TO))
2553 pr->target_pid = GNUNET_PEER_intern ((const struct GNUNET_PeerIdentity*) &opt[bits++]);
2555 pr->anonymity_level = 1;
2556 pr->priority = bound_priority (ntohl (gm->priority), cps);
2557 pr->ttl = bound_ttl (ntohl (gm->ttl), pr->priority);
2558 pr->query = gm->query;
2559 /* decrement ttl (always) */
2560 ttl_decrement = 2 * TTL_DECREMENT +
2561 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2563 if ( (pr->ttl < 0) &&
2564 (pr->ttl - ttl_decrement > 0) )
2567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2568 "Dropping query from `%s' due to TTL underflow.\n",
2569 GNUNET_i2s (other));
2571 GNUNET_STATISTICS_update (stats,
2572 gettext_noop ("# requests dropped due TTL underflow"),
2575 /* integer underflow => drop (should be very rare)! */
2579 pr->ttl -= ttl_decrement;
2580 pr->start_time = GNUNET_TIME_absolute_get ();
2582 /* get bloom filter */
2585 pr->bf = GNUNET_CONTAINER_bloomfilter_init ((const char*) &opt[bits],
2588 pr->bf_size = bfsize;
2593 GNUNET_CONTAINER_multihashmap_get_multiple (query_request_map,
2595 &check_duplicate_request_peer,
2597 if (cdc.have != NULL)
2599 if (cdc.have->start_time.value + cdc.have->ttl >=
2600 pr->start_time.value + pr->ttl)
2602 /* existing request has higher TTL, drop new one! */
2603 cdc.have->priority += pr->priority;
2604 destroy_pending_request (pr);
2606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2607 "Have existing request with higher TTL, dropping new request.\n",
2608 GNUNET_i2s (other));
2610 GNUNET_STATISTICS_update (stats,
2611 gettext_noop ("# requests dropped due to existing request with higher TTL"),
2618 /* existing request has lower TTL, drop old one! */
2619 pr->priority += cdc.have->priority;
2620 /* Possible optimization: if we have applicable pending
2621 replies in 'cdc.have', we might want to move those over
2622 (this is a really rare special-case, so it is not clear
2623 that this would be worth it) */
2624 destroy_pending_request (cdc.have);
2625 /* keep processing 'pr'! */
2630 GNUNET_CONTAINER_multihashmap_put (query_request_map,
2633 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2634 GNUNET_CONTAINER_multihashmap_put (peer_request_map,
2637 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2639 pr->hnode = GNUNET_CONTAINER_heap_insert (requests_by_expiration_heap,
2641 pr->start_time.value + pr->ttl);
2643 GNUNET_STATISTICS_update (stats,
2644 gettext_noop ("# P2P searches active"),
2648 /* calculate change in traffic preference */
2649 preference = (double) pr->priority;
2650 if (preference < QUERY_BANDWIDTH_VALUE)
2651 preference = QUERY_BANDWIDTH_VALUE;
2652 cps->inc_preference += preference;
2654 /* process locally */
2655 if (type == GNUNET_DATASTORE_BLOCKTYPE_DBLOCK)
2656 type = GNUNET_DATASTORE_BLOCKTYPE_ANY; /* to get on-demand as well */
2657 timeout = GNUNET_TIME_relative_multiply (BASIC_DATASTORE_REQUEST_DELAY,
2658 (pr->priority + 1));
2659 pr->drq = GNUNET_FS_drq_get (&gm->query,
2661 &process_local_reply,
2666 /* Are multiple results possible? If so, start processing remotely now! */
2669 case GNUNET_DATASTORE_BLOCKTYPE_DBLOCK:
2670 case GNUNET_DATASTORE_BLOCKTYPE_IBLOCK:
2671 /* only one result, wait for datastore */
2674 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
2675 pr->task = GNUNET_SCHEDULER_add_now (sched,
2676 &forward_request_task,
2680 /* make sure we don't track too many requests */
2681 if (GNUNET_CONTAINER_heap_get_size (requests_by_expiration_heap) > max_pending_requests)
2683 pr = GNUNET_CONTAINER_heap_peek (requests_by_expiration_heap);
2684 destroy_pending_request (pr);
2690 /* **************************** CS GET Handling ************************ */
2694 * Handle START_SEARCH-message (search request from client).
2696 * @param cls closure
2697 * @param client identification of the client
2698 * @param message the actual message
2701 handle_start_search (void *cls,
2702 struct GNUNET_SERVER_Client *client,
2703 const struct GNUNET_MessageHeader *message)
2705 static GNUNET_HashCode all_zeros;
2706 const struct SearchMessage *sm;
2707 struct ClientList *cl;
2708 struct ClientRequestList *crl;
2709 struct PendingRequest *pr;
2714 msize = ntohs (message->size);
2715 if ( (msize < sizeof (struct SearchMessage)) ||
2716 (0 != (msize - sizeof (struct SearchMessage)) % sizeof (GNUNET_HashCode)) )
2719 GNUNET_SERVER_receive_done (client,
2723 sc = (msize - sizeof (struct SearchMessage)) / sizeof (GNUNET_HashCode);
2724 sm = (const struct SearchMessage*) message;
2727 while ( (cl != NULL) &&
2728 (cl->client != client) )
2732 cl = GNUNET_malloc (sizeof (struct ClientList));
2733 cl->client = client;
2734 GNUNET_SERVER_client_keep (client);
2735 cl->next = client_list;
2738 type = ntohl (sm->type);
2740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2741 "Received request for `%s' of type %u from local client\n",
2742 GNUNET_h2s (&sm->query),
2743 (unsigned int) type);
2747 case GNUNET_DATASTORE_BLOCKTYPE_DBLOCK:
2748 case GNUNET_DATASTORE_BLOCKTYPE_IBLOCK:
2749 case GNUNET_DATASTORE_BLOCKTYPE_KBLOCK:
2750 case GNUNET_DATASTORE_BLOCKTYPE_SBLOCK:
2754 GNUNET_SERVER_receive_done (client,
2759 /* detect duplicate KBLOCK requests */
2760 if (type == GNUNET_DATASTORE_BLOCKTYPE_KBLOCK)
2763 while ( (crl != NULL) &&
2764 ( (0 != memcmp (&crl->req->query,
2766 sizeof (GNUNET_HashCode))) ||
2767 (crl->req->type != type) ) )
2772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2773 "Have existing request, merging content-seen lists.\n");
2776 /* Duplicate request (used to send long list of
2777 known/blocked results); merge 'pr->replies_seen'
2778 and update bloom filter */
2779 GNUNET_array_grow (pr->replies_seen,
2780 pr->replies_seen_size,
2781 pr->replies_seen_off + sc);
2782 memcpy (&pr->replies_seen[pr->replies_seen_off],
2784 sc * sizeof (GNUNET_HashCode));
2785 pr->replies_seen_off += sc;
2787 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
2788 pr->bf = refresh_bloomfilter (pr->replies_seen_off,
2792 GNUNET_STATISTICS_update (stats,
2793 gettext_noop ("# client searches updated (merged content seen list)"),
2796 GNUNET_SERVER_receive_done (client,
2801 GNUNET_STATISTICS_update (stats,
2802 gettext_noop ("# client searches active"),
2805 pr = GNUNET_malloc (sizeof (struct PendingRequest) +
2806 ((type == GNUNET_DATASTORE_BLOCKTYPE_SBLOCK)?sizeof(GNUNET_HashCode):0));
2807 crl = GNUNET_malloc (sizeof (struct ClientRequestList));
2808 memset (crl, 0, sizeof (struct ClientRequestList));
2809 crl->client_list = cl;
2810 GNUNET_CONTAINER_DLL_insert (cl->rl_head,
2815 pr->client_request_list = crl;
2816 GNUNET_array_grow (pr->replies_seen,
2817 pr->replies_seen_size,
2819 memcpy (pr->replies_seen,
2821 sc * sizeof (GNUNET_HashCode));
2822 pr->replies_seen_off = sc;
2823 pr->anonymity_level = ntohl (sm->anonymity_level);
2824 pr->bf = refresh_bloomfilter (pr->replies_seen_off,
2828 pr->query = sm->query;
2831 case GNUNET_DATASTORE_BLOCKTYPE_DBLOCK:
2832 case GNUNET_DATASTORE_BLOCKTYPE_IBLOCK:
2833 if (0 != memcmp (&sm->target,
2835 sizeof (GNUNET_HashCode)))
2836 pr->target_pid = GNUNET_PEER_intern ((const struct GNUNET_PeerIdentity*) &sm->target);
2838 case GNUNET_DATASTORE_BLOCKTYPE_SBLOCK:
2839 pr->namespace = (GNUNET_HashCode*) &pr[1];
2840 memcpy (&pr[1], &sm->target, sizeof (GNUNET_HashCode));
2845 GNUNET_CONTAINER_multihashmap_put (query_request_map,
2848 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2849 if (type == GNUNET_DATASTORE_BLOCKTYPE_DBLOCK)
2850 type = GNUNET_DATASTORE_BLOCKTYPE_ANY; /* get on-demand blocks too! */
2851 pr->drq = GNUNET_FS_drq_get (&sm->query,
2853 &process_local_reply,
2855 GNUNET_TIME_UNIT_FOREVER_REL,
2860 /* **************************** Startup ************************ */
2864 * List of handlers for P2P messages
2865 * that we care about.
2867 static struct GNUNET_CORE_MessageHandler p2p_handlers[] =
2870 GNUNET_MESSAGE_TYPE_FS_GET, 0 },
2872 GNUNET_MESSAGE_TYPE_FS_PUT, 0 },
2878 * List of handlers for the messages understood by this
2881 static struct GNUNET_SERVER_MessageHandler handlers[] = {
2882 {&GNUNET_FS_handle_index_start, NULL,
2883 GNUNET_MESSAGE_TYPE_FS_INDEX_START, 0},
2884 {&GNUNET_FS_handle_index_list_get, NULL,
2885 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_GET, sizeof(struct GNUNET_MessageHeader) },
2886 {&GNUNET_FS_handle_unindex, NULL, GNUNET_MESSAGE_TYPE_FS_UNINDEX,
2887 sizeof (struct UnindexMessage) },
2888 {&handle_start_search, NULL, GNUNET_MESSAGE_TYPE_FS_START_SEARCH,
2895 * Process fs requests.
2897 * @param s scheduler to use
2898 * @param server the initialized server
2899 * @param c configuration to use
2902 main_init (struct GNUNET_SCHEDULER_Handle *s,
2903 struct GNUNET_SERVER_Handle *server,
2904 const struct GNUNET_CONFIGURATION_Handle *c)
2908 stats = GNUNET_STATISTICS_create (sched, "fs", cfg);
2909 connected_peers = GNUNET_CONTAINER_multihashmap_create (128); // FIXME: get size from config
2910 query_request_map = GNUNET_CONTAINER_multihashmap_create (128); // FIXME: get size from config
2911 peer_request_map = GNUNET_CONTAINER_multihashmap_create (128); // FIXME: get size from config
2912 requests_by_expiration_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
2913 core = GNUNET_CORE_connect (sched,
2915 GNUNET_TIME_UNIT_FOREVER_REL,
2919 &peer_connect_handler,
2920 &peer_disconnect_handler,
2926 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2927 _("Failed to connect to `%s' service.\n"),
2929 GNUNET_CONTAINER_multihashmap_destroy (connected_peers);
2930 connected_peers = NULL;
2931 GNUNET_CONTAINER_multihashmap_destroy (query_request_map);
2932 query_request_map = NULL;
2933 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
2934 requests_by_expiration_heap = NULL;
2935 GNUNET_CONTAINER_multihashmap_destroy (peer_request_map);
2936 peer_request_map = NULL;
2938 return GNUNET_SYSERR;
2940 GNUNET_SERVER_disconnect_notify (server,
2941 &handle_client_disconnect,
2943 GNUNET_SERVER_add_handlers (server, handlers);
2944 GNUNET_SCHEDULER_add_delayed (sched,
2945 GNUNET_TIME_UNIT_FOREVER_REL,
2953 * Process fs requests.
2955 * @param cls closure
2956 * @param sched scheduler to use
2957 * @param server the initialized server
2958 * @param cfg configuration to use
2962 struct GNUNET_SCHEDULER_Handle *sched,
2963 struct GNUNET_SERVER_Handle *server,
2964 const struct GNUNET_CONFIGURATION_Handle *cfg)
2966 if ( (GNUNET_OK != GNUNET_FS_drq_init (sched, cfg)) ||
2967 (GNUNET_OK != GNUNET_FS_indexing_init (sched, cfg)) ||
2968 (GNUNET_OK != main_init (sched, server, cfg)) )
2970 GNUNET_SCHEDULER_shutdown (sched);
2977 * The main function for the fs service.
2979 * @param argc number of arguments from the command line
2980 * @param argv command line arguments
2981 * @return 0 ok, 1 on error
2984 main (int argc, char *const *argv)
2986 return (GNUNET_OK ==
2987 GNUNET_SERVICE_run (argc,
2990 GNUNET_SERVICE_OPTION_NONE,
2991 &run, NULL)) ? 0 : 1;
2994 /* end of gnunet-service-fs.c */