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"
48 #define DEBUG_FS GNUNET_NO
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
58 * What is the maximum delay for a P2P FS message (in our interaction
59 * with core)? FS-internal delays are another story. The value is
60 * chosen based on the 32k block size. Given that peers typcially
61 * have at least 1 kb/s bandwidth, 45s waits give us a chance to
62 * transmit one message even to the lowest-bandwidth peers.
64 #define MAX_TRANSMIT_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 45)
69 * Maximum number of requests (from other peers) that we're
70 * willing to have pending at any given point in time.
71 * FIXME: set from configuration (and 32 is a tiny value for testing only).
73 static uint64_t max_pending_requests = 32;
77 * Information we keep for each pending reply. The
78 * actual message follows at the end of this struct.
80 struct PendingMessage;
84 * Function called upon completion of a transmission.
87 * @param pid ID of receiving peer, 0 on transmission error
89 typedef void (*TransmissionContinuation)(void * cls,
94 * Information we keep for each pending message (GET/PUT). The
95 * actual message follows at the end of this struct.
100 * This is a doubly-linked list of messages to the same peer.
102 struct PendingMessage *next;
105 * This is a doubly-linked list of messages to the same peer.
107 struct PendingMessage *prev;
110 * Entry in pending message list for this pending message.
112 struct PendingMessageList *pml;
115 * Function to call immediately once we have transmitted this
118 TransmissionContinuation cont;
126 * Size of the reply; actual reply message follows
127 * at the end of this struct.
132 * How important is this message for us?
140 * Information about a peer that we are connected to.
141 * We track data that is useful for determining which
142 * peers should receive our requests. We also keep
143 * a list of messages to transmit to this peer.
149 * List of the last clients for which this peer successfully
152 struct GNUNET_SERVER_Client *last_client_replies[CS2P_SUCCESS_LIST_SIZE];
155 * List of the last PIDs for which
156 * this peer successfully answered a query;
157 * We use 0 to indicate no successful reply.
159 GNUNET_PEER_Id last_p2p_replies[P2P_SUCCESS_LIST_SIZE];
162 * Average delay between sending the peer a request and
163 * getting a reply (only calculated over the requests for
164 * which we actually got a reply). Calculated
165 * as a moving average: new_delay = ((n-1)*last_delay+curr_delay) / n
167 struct GNUNET_TIME_Relative avg_delay;
170 * Handle for an active request for transmission to this
173 struct GNUNET_CORE_TransmitHandle *cth;
176 * Messages (replies, queries, content migration) we would like to
177 * send to this peer in the near future. Sorted by priority, head.
179 struct PendingMessage *pending_messages_head;
182 * Messages (replies, queries, content migration) we would like to
183 * send to this peer in the near future. Sorted by priority, tail.
185 struct PendingMessage *pending_messages_tail;
188 * Average priority of successful replies. Calculated
189 * as a moving average: new_avg = ((n-1)*last_avg+curr_prio) / n
194 * Increase in traffic preference still to be submitted
195 * to the core service for this peer. FIXME: double or 'uint64_t'?
197 double inc_preference;
200 * The peer's identity.
205 * Size of the linked list of 'pending_messages'.
207 unsigned int pending_requests;
210 * Which offset in "last_p2p_replies" will be updated next?
211 * (we go round-robin).
213 unsigned int last_p2p_replies_woff;
216 * Which offset in "last_client_replies" will be updated next?
217 * (we go round-robin).
219 unsigned int last_client_replies_woff;
225 * Information we keep for each pending request. We should try to
226 * keep this struct as small as possible since its memory consumption
227 * is key to how many requests we can have pending at once.
229 struct PendingRequest;
233 * Doubly-linked list of requests we are performing
234 * on behalf of the same client.
236 struct ClientRequestList
240 * This is a doubly-linked list.
242 struct ClientRequestList *next;
245 * This is a doubly-linked list.
247 struct ClientRequestList *prev;
250 * Request this entry represents.
252 struct PendingRequest *req;
255 * Client list this request belongs to.
257 struct ClientList *client_list;
263 * Replies to be transmitted to the client. The actual
264 * response message is allocated after this struct.
266 struct ClientResponseMessage
269 * This is a doubly-linked list.
271 struct ClientResponseMessage *next;
274 * This is a doubly-linked list.
276 struct ClientResponseMessage *prev;
279 * Client list entry this response belongs to.
281 struct ClientList *client_list;
284 * Number of bytes in the response.
291 * Linked list of clients we are performing requests
297 * This is a linked list.
299 struct ClientList *next;
302 * ID of a client making a request, NULL if this entry is for a
305 struct GNUNET_SERVER_Client *client;
308 * Head of list of requests performed on behalf
309 * of this client right now.
311 struct ClientRequestList *rl_head;
314 * Tail of list of requests performed on behalf
315 * of this client right now.
317 struct ClientRequestList *rl_tail;
320 * Head of linked list of responses.
322 struct ClientResponseMessage *res_head;
325 * Tail of linked list of responses.
327 struct ClientResponseMessage *res_tail;
330 * Context for sending replies.
332 struct GNUNET_CONNECTION_TransmitHandle *th;
338 * Doubly-linked list of messages we are performing
339 * due to a pending request.
341 struct PendingMessageList
345 * This is a doubly-linked list of messages on behalf of the same request.
347 struct PendingMessageList *next;
350 * This is a doubly-linked list of messages on behalf of the same request.
352 struct PendingMessageList *prev;
355 * Message this entry represents.
357 struct PendingMessage *pm;
360 * Request this entry belongs to.
362 struct PendingRequest *req;
365 * Peer this message is targeted for.
367 struct ConnectedPeer *target;
373 * Information we keep for each pending request. We should try to
374 * keep this struct as small as possible since its memory consumption
375 * is key to how many requests we can have pending at once.
377 struct PendingRequest
381 * If this request was made by a client, this is our entry in the
382 * client request list; otherwise NULL.
384 struct ClientRequestList *client_request_list;
387 * Entry of peer responsible for this entry (if this request
388 * was made by a peer).
390 struct ConnectedPeer *cp;
393 * If this is a namespace query, pointer to the hash of the public
394 * key of the namespace; otherwise NULL. Pointer will be to the
395 * end of this struct (so no need to free it).
397 const GNUNET_HashCode *namespace;
400 * Bloomfilter we use to filter out replies that we don't care about
401 * (anymore). NULL as long as we are interested in all replies.
403 struct GNUNET_CONTAINER_BloomFilter *bf;
406 * Context of our GNUNET_CORE_peer_change_preference call.
408 struct GNUNET_CORE_InformationRequestContext *irc;
411 * Hash code of all replies that we have seen so far (only valid
412 * if client is not NULL since we only track replies like this for
415 GNUNET_HashCode *replies_seen;
418 * Node in the heap representing this entry; NULL
419 * if we have no heap node.
421 struct GNUNET_CONTAINER_HeapNode *hnode;
424 * Head of list of messages being performed on behalf of this
427 struct PendingMessageList *pending_head;
430 * Tail of list of messages being performed on behalf of this
433 struct PendingMessageList *pending_tail;
436 * When did we first see this request (form this peer), or, if our
437 * client is initiating, when did we last initiate a search?
439 struct GNUNET_TIME_Absolute start_time;
442 * The query that this request is for.
444 GNUNET_HashCode query;
447 * The task responsible for transmitting queries
450 GNUNET_SCHEDULER_TaskIdentifier task;
453 * (Interned) Peer identifier that identifies a preferred target
456 GNUNET_PEER_Id target_pid;
459 * (Interned) Peer identifiers of peers that have already
460 * received our query for this content.
462 GNUNET_PEER_Id *used_pids;
465 * Our entry in the DRQ (non-NULL while we wait for our
466 * turn to interact with the local database).
468 struct DatastoreRequestQueue *drq;
471 * Size of the 'bf' (in bytes).
476 * Desired anonymity level; only valid for requests from a local client.
478 uint32_t anonymity_level;
481 * How many entries in "used_pids" are actually valid?
483 unsigned int used_pids_off;
486 * How long is the "used_pids" array?
488 unsigned int used_pids_size;
491 * Number of results found for this request.
493 unsigned int results_found;
496 * How many entries in "replies_seen" are actually valid?
498 unsigned int replies_seen_off;
501 * How long is the "replies_seen" array?
503 unsigned int replies_seen_size;
506 * Priority with which this request was made. If one of our clients
507 * made the request, then this is the current priority that we are
508 * using when initiating the request. This value is used when
509 * we decide to reward other peers with trust for providing a reply.
514 * Priority points left for us to spend when forwarding this request
517 uint32_t remaining_priority;
520 * Number to mingle hashes for bloom-filter tests with.
525 * TTL with which we saw this request (or, if we initiated, TTL that
526 * we used for the request).
531 * Type of the content that this request is for.
541 static struct GNUNET_SCHEDULER_Handle *sched;
546 static const struct GNUNET_CONFIGURATION_Handle *cfg;
549 * Map of peer identifiers to "struct ConnectedPeer" (for that peer).
551 static struct GNUNET_CONTAINER_MultiHashMap *connected_peers;
554 * Map of peer identifiers to "struct PendingRequest" (for that peer).
556 static struct GNUNET_CONTAINER_MultiHashMap *peer_request_map;
559 * Map of query identifiers to "struct PendingRequest" (for that query).
561 static struct GNUNET_CONTAINER_MultiHashMap *query_request_map;
564 * Heap with the request that will expire next at the top. Contains
565 * pointers of type "struct PendingRequest*"; these will *also* be
566 * aliased from the "requests_by_peer" data structures and the
567 * "requests_by_query" table. Note that requests from our clients
568 * don't expire and are thus NOT in the "requests_by_expiration"
569 * (or the "requests_by_peer" tables).
571 static struct GNUNET_CONTAINER_Heap *requests_by_expiration_heap;
574 * Handle for reporting statistics.
576 static struct GNUNET_STATISTICS_Handle *stats;
579 * Linked list of clients we are currently processing requests for.
581 static struct ClientList *client_list;
584 * Pointer to handle to the core service (points to NULL until we've
587 static struct GNUNET_CORE_Handle *core;
590 /* ******************* clean up functions ************************ */
594 * We're done with a particular message list entry.
595 * Free all associated resources.
597 * @param pml entry to destroy
600 destroy_pending_message_list_entry (struct PendingMessageList *pml)
602 GNUNET_CONTAINER_DLL_remove (pml->req->pending_head,
603 pml->req->pending_tail,
605 GNUNET_CONTAINER_DLL_remove (pml->target->pending_messages_head,
606 pml->target->pending_messages_tail,
608 pml->target->pending_requests--;
609 GNUNET_free (pml->pm);
615 * Destroy the given pending message (and call the respective
618 * @param pm message to destroy
619 * @param tpid id of peer that the message was delivered to, or 0 for none
622 destroy_pending_message (struct PendingMessage *pm,
625 struct PendingMessageList *pml = pm->pml;
626 TransmissionContinuation cont;
629 GNUNET_assert (pml->pm == pm);
630 GNUNET_assert ( (tpid == 0) || (tpid == pml->target->pid) );
632 cont_cls = pm->cont_cls;
633 destroy_pending_message_list_entry (pml);
639 * We're done processing a particular request.
640 * Free all associated resources.
642 * @param pr request to destroy
645 destroy_pending_request (struct PendingRequest *pr)
647 struct GNUNET_PeerIdentity pid;
649 if (pr->hnode != NULL)
651 GNUNET_CONTAINER_heap_remove_node (requests_by_expiration_heap,
654 GNUNET_STATISTICS_update (stats,
655 gettext_noop ("# P2P searches active"),
661 GNUNET_STATISTICS_update (stats,
662 gettext_noop ("# client searches active"),
666 /* might have already been removed from map in 'process_reply' (if
667 there was a unique reply) or never inserted if it was a
668 duplicate; hence ignore the return value here */
669 (void) GNUNET_CONTAINER_multihashmap_remove (query_request_map,
674 GNUNET_FS_drq_get_cancel (pr->drq);
677 if (pr->client_request_list != NULL)
679 GNUNET_CONTAINER_DLL_remove (pr->client_request_list->client_list->rl_head,
680 pr->client_request_list->client_list->rl_tail,
681 pr->client_request_list);
682 GNUNET_free (pr->client_request_list);
683 pr->client_request_list = NULL;
687 GNUNET_PEER_resolve (pr->cp->pid,
689 (void) GNUNET_CONTAINER_multihashmap_remove (peer_request_map,
696 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
701 GNUNET_CORE_peer_change_preference_cancel (pr->irc);
704 if (pr->replies_seen != NULL)
706 GNUNET_free (pr->replies_seen);
707 pr->replies_seen = NULL;
709 if (pr->task != GNUNET_SCHEDULER_NO_TASK)
711 GNUNET_SCHEDULER_cancel (sched,
713 pr->task = GNUNET_SCHEDULER_NO_TASK;
715 while (NULL != pr->pending_head)
716 destroy_pending_message_list_entry (pr->pending_head);
717 GNUNET_PEER_change_rc (pr->target_pid, -1);
718 if (pr->used_pids != NULL)
720 GNUNET_PEER_decrement_rcs (pr->used_pids, pr->used_pids_off);
721 GNUNET_free (pr->used_pids);
722 pr->used_pids_off = 0;
723 pr->used_pids_size = 0;
724 pr->used_pids = NULL;
731 * Method called whenever a given peer connects.
733 * @param cls closure, not used
734 * @param peer peer identity this notification is about
735 * @param latency reported latency of the connection with 'other'
736 * @param distance reported distance (DV) to 'other'
739 peer_connect_handler (void *cls,
741 GNUNET_PeerIdentity * peer,
742 struct GNUNET_TIME_Relative latency,
745 struct ConnectedPeer *cp;
747 cp = GNUNET_malloc (sizeof (struct ConnectedPeer));
748 cp->pid = GNUNET_PEER_intern (peer);
749 GNUNET_CONTAINER_multihashmap_put (connected_peers,
752 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
757 * Free (each) request made by the peer.
759 * @param cls closure, points to peer that the request belongs to
760 * @param key current key code
761 * @param value value in the hash map
762 * @return GNUNET_YES (we should continue to iterate)
765 destroy_request (void *cls,
766 const GNUNET_HashCode * key,
769 const struct GNUNET_PeerIdentity * peer = cls;
770 struct PendingRequest *pr = value;
772 GNUNET_CONTAINER_multihashmap_remove (peer_request_map,
775 destroy_pending_request (pr);
781 * Method called whenever a peer disconnects.
783 * @param cls closure, not used
784 * @param peer peer identity this notification is about
787 peer_disconnect_handler (void *cls,
789 GNUNET_PeerIdentity * peer)
791 struct ConnectedPeer *cp;
792 struct PendingMessage *pm;
795 GNUNET_CONTAINER_multihashmap_get_multiple (peer_request_map,
799 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
803 for (i=0;i<CS2P_SUCCESS_LIST_SIZE;i++)
805 if (NULL != cp->last_client_replies[i])
807 GNUNET_SERVER_client_drop (cp->last_client_replies[i]);
808 cp->last_client_replies[i] = NULL;
811 GNUNET_CONTAINER_multihashmap_remove (connected_peers,
814 GNUNET_PEER_change_rc (cp->pid, -1);
815 GNUNET_PEER_decrement_rcs (cp->last_p2p_replies, P2P_SUCCESS_LIST_SIZE);
817 GNUNET_CORE_notify_transmit_ready_cancel (cp->cth);
818 while (NULL != (pm = cp->pending_messages_head))
819 destroy_pending_message (pm, 0 /* delivery failed */);
820 GNUNET_break (0 == cp->pending_requests);
826 * Iterator over hash map entries that removes all occurences
827 * of the given 'client' from the 'last_client_replies' of the
828 * given connected peer.
830 * @param cls closure, the 'struct GNUNET_SERVER_Client*' to remove
831 * @param key current key code (unused)
832 * @param value value in the hash map (the 'struct ConnectedPeer*' to change)
833 * @return GNUNET_YES (we should continue to iterate)
836 remove_client_from_last_client_replies (void *cls,
837 const GNUNET_HashCode * key,
840 struct GNUNET_SERVER_Client *client = cls;
841 struct ConnectedPeer *cp = value;
844 for (i=0;i<CS2P_SUCCESS_LIST_SIZE;i++)
846 if (cp->last_client_replies[i] == client)
848 GNUNET_SERVER_client_drop (cp->last_client_replies[i]);
849 cp->last_client_replies[i] = NULL;
857 * A client disconnected. Remove all of its pending queries.
859 * @param cls closure, NULL
860 * @param client identification of the client
863 handle_client_disconnect (void *cls,
864 struct GNUNET_SERVER_Client
867 struct ClientList *pos;
868 struct ClientList *prev;
869 struct ClientRequestList *rcl;
870 struct ClientResponseMessage *creply;
876 while ( (NULL != pos) &&
877 (pos->client != client) )
883 return; /* no requests pending for this client */
884 while (NULL != (rcl = pos->rl_head))
885 destroy_pending_request (rcl->req);
887 client_list = pos->next;
889 prev->next = pos->next;
892 GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th);
895 while (NULL != (creply = pos->res_head))
897 GNUNET_CONTAINER_DLL_remove (pos->res_head,
900 GNUNET_free (creply);
902 GNUNET_SERVER_client_drop (pos->client);
904 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
905 &remove_client_from_last_client_replies,
911 * Iterator to free peer entries.
913 * @param cls closure, unused
914 * @param key current key code
915 * @param value value in the hash map (peer entry)
916 * @return GNUNET_YES (we should continue to iterate)
919 clean_peer (void *cls,
920 const GNUNET_HashCode * key,
923 peer_disconnect_handler (NULL, (const struct GNUNET_PeerIdentity*) key);
929 * Task run during shutdown.
935 shutdown_task (void *cls,
936 const struct GNUNET_SCHEDULER_TaskContext *tc)
938 while (client_list != NULL)
939 handle_client_disconnect (NULL,
940 client_list->client);
941 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
944 GNUNET_break (0 == GNUNET_CONTAINER_heap_get_size (requests_by_expiration_heap));
945 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
946 requests_by_expiration_heap = 0;
947 GNUNET_CONTAINER_multihashmap_destroy (connected_peers);
948 connected_peers = NULL;
949 GNUNET_break (0 == GNUNET_CONTAINER_multihashmap_size (query_request_map));
950 GNUNET_CONTAINER_multihashmap_destroy (query_request_map);
951 query_request_map = NULL;
952 GNUNET_break (0 == GNUNET_CONTAINER_multihashmap_size (peer_request_map));
953 GNUNET_CONTAINER_multihashmap_destroy (peer_request_map);
954 peer_request_map = NULL;
955 GNUNET_assert (NULL != core);
956 GNUNET_CORE_disconnect (core);
960 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
968 /* ******************* Utility functions ******************** */
972 * Transmit the given message by copying it to the target buffer
973 * "buf". "buf" will be NULL and "size" zero if the socket was closed
974 * for writing in the meantime. In that case, do nothing
975 * (the disconnect or shutdown handler will take care of the rest).
976 * If we were able to transmit messages and there are still more
977 * pending, ask core again for further calls to this function.
979 * @param cls closure, pointer to the 'struct ConnectedPeer*'
980 * @param size number of bytes available in buf
981 * @param buf where the callee should write the message
982 * @return number of bytes written to buf
985 transmit_to_peer (void *cls,
986 size_t size, void *buf)
988 struct ConnectedPeer *cp = cls;
990 struct GNUNET_PeerIdentity pid;
991 struct PendingMessage *pm;
998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
999 "Dropping message, core too busy.\n");
1004 while ( (NULL != (pm = cp->pending_messages_head) ) &&
1005 (pm->msize <= size) )
1007 memcpy (&cbuf[msize], &pm[1], pm->msize);
1010 destroy_pending_message (pm, cp->pid);
1014 GNUNET_PEER_resolve (cp->pid,
1016 cp->cth = GNUNET_CORE_notify_transmit_ready (core,
1018 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1026 "Transmitting %u bytes to peer %u.\n",
1035 * Add a message to the set of pending messages for the given peer.
1037 * @param cp peer to send message to
1038 * @param pm message to queue
1039 * @param pr request on which behalf this message is being queued
1042 add_to_pending_messages_for_peer (struct ConnectedPeer *cp,
1043 struct PendingMessage *pm,
1044 struct PendingRequest *pr)
1046 struct PendingMessage *pos;
1047 struct PendingMessageList *pml;
1048 struct GNUNET_PeerIdentity pid;
1050 GNUNET_assert (pm->next == NULL);
1051 GNUNET_assert (pm->pml == NULL);
1052 pml = GNUNET_malloc (sizeof (struct PendingMessageList));
1057 GNUNET_CONTAINER_DLL_insert (pr->pending_head,
1060 pos = cp->pending_messages_head;
1061 while ( (pos != NULL) &&
1062 (pm->priority < pos->priority) )
1064 GNUNET_CONTAINER_DLL_insert_after (cp->pending_messages_head,
1065 cp->pending_messages_tail,
1068 cp->pending_requests++;
1069 if (cp->pending_requests > MAX_QUEUE_PER_PEER)
1070 destroy_pending_message (cp->pending_messages_tail, 0);
1071 if (cp->cth == NULL)
1073 /* need to schedule transmission */
1074 GNUNET_PEER_resolve (cp->pid, &pid);
1075 cp->cth = GNUNET_CORE_notify_transmit_ready (core,
1076 cp->pending_messages_head->priority,
1079 cp->pending_messages_head->msize,
1083 if (cp->cth == NULL)
1086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1087 "Failed to schedule transmission with core!\n");
1089 /* FIXME: call stats (rare, bad case) */
1095 * Mingle hash with the mingle_number to produce different bits.
1098 mingle_hash (const GNUNET_HashCode * in,
1099 int32_t mingle_number,
1100 GNUNET_HashCode * hc)
1104 GNUNET_CRYPTO_hash (&mingle_number,
1107 GNUNET_CRYPTO_hash_xor (&m, in, hc);
1112 * Test if the load on this peer is too high
1113 * to even consider processing the query at
1116 * @return GNUNET_YES if the load is too high, GNUNET_NO otherwise
1119 test_load_too_high ()
1121 return GNUNET_NO; // FIXME
1125 /* ******************* Pending Request Refresh Task ******************** */
1130 * We use a random delay to make the timing of requests less
1131 * predictable. This function returns such a random delay.
1133 * FIXME: make schedule dependent on the specifics of the request?
1134 * Or bandwidth and number of connected peers and load?
1136 * @return random delay to use for some request, between 0 and TTL_DECREMENT ms
1138 static struct GNUNET_TIME_Relative
1139 get_processing_delay ()
1141 return GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1142 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1148 * We're processing a GET request from another peer and have decided
1149 * to forward it to other peers. This function is called periodically
1150 * and should forward the request to other peers until we have all
1151 * possible replies. If we have transmitted the *only* reply to
1152 * the initiator we should destroy the pending request. If we have
1153 * many replies in the queue to the initiator, we should delay sending
1154 * out more queries until the reply queue has shrunk some.
1156 * @param cls our "struct ProcessGetContext *"
1160 forward_request_task (void *cls,
1161 const struct GNUNET_SCHEDULER_TaskContext *tc);
1165 * Function called after we either failed or succeeded
1166 * at transmitting a query to a peer.
1168 * @param cls the requests "struct PendingRequest*"
1169 * @param tpid ID of receiving peer, 0 on transmission error
1172 transmit_query_continuation (void *cls,
1173 GNUNET_PEER_Id tpid)
1175 struct PendingRequest *pr = cls;
1179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1180 "Transmission of request failed, will try again later.\n");
1181 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
1182 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
1183 get_processing_delay (),
1184 &forward_request_task,
1188 GNUNET_STATISTICS_update (stats,
1189 gettext_noop ("# queries forwarded"),
1192 GNUNET_PEER_change_rc (tpid, 1);
1193 if (pr->used_pids_off == pr->used_pids_size)
1194 GNUNET_array_grow (pr->used_pids,
1196 pr->used_pids_size * 2 + 2);
1197 pr->used_pids[pr->used_pids_off++] = tpid;
1198 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
1199 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
1200 get_processing_delay (),
1201 &forward_request_task,
1207 * How many bytes should a bloomfilter be if we have already seen
1208 * entry_count responses? Note that BLOOMFILTER_K gives us the number
1209 * of bits set per entry. Furthermore, we should not re-size the
1210 * filter too often (to keep it cheap).
1212 * Since other peers will also add entries but not resize the filter,
1213 * we should generally pick a slightly larger size than what the
1214 * strict math would suggest.
1216 * @return must be a power of two and smaller or equal to 2^15.
1219 compute_bloomfilter_size (unsigned int entry_count)
1222 unsigned int ideal = (entry_count * BLOOMFILTER_K) / 4;
1223 uint16_t max = 1 << 15;
1225 if (entry_count > max)
1228 while ((size < max) && (size < ideal))
1237 * Recalculate our bloom filter for filtering replies.
1239 * @param count number of entries we are filtering right now
1240 * @param mingle set to our new mingling value
1241 * @param bf_size set to the size of the bloomfilter
1242 * @param entries the entries to filter
1243 * @return updated bloomfilter, NULL for none
1245 static struct GNUNET_CONTAINER_BloomFilter *
1246 refresh_bloomfilter (unsigned int count,
1249 const GNUNET_HashCode *entries)
1251 struct GNUNET_CONTAINER_BloomFilter *bf;
1254 GNUNET_HashCode mhash;
1258 nsize = compute_bloomfilter_size (count);
1259 *mingle = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, -1);
1261 bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
1264 for (i=0;i<count;i++)
1266 mingle_hash (&entries[i], *mingle, &mhash);
1267 GNUNET_CONTAINER_bloomfilter_add (bf, &mhash);
1274 * Function called after we've tried to reserve a certain amount of
1275 * bandwidth for a reply. Check if we succeeded and if so send our
1278 * @param cls the requests "struct PendingRequest*"
1279 * @param peer identifies the peer
1280 * @param bpm_in set to the current bandwidth limit (receiving) for this peer
1281 * @param bpm_out set to the current bandwidth limit (sending) for this peer
1282 * @param amount set to the amount that was actually reserved or unreserved
1283 * @param preference current traffic preference for the given peer
1286 target_reservation_cb (void *cls,
1288 GNUNET_PeerIdentity * peer,
1289 struct GNUNET_BANDWIDTH_Value32NBO bpm_in,
1290 struct GNUNET_BANDWIDTH_Value32NBO bpm_out,
1292 uint64_t preference)
1294 struct PendingRequest *pr = cls;
1295 struct ConnectedPeer *cp;
1296 struct PendingMessage *pm;
1297 struct GetMessage *gm;
1298 GNUNET_HashCode *ext;
1306 GNUNET_assert (peer != NULL);
1307 // (3) transmit, update ttl/priority
1308 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
1312 /* Peer must have just left */
1314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1315 "Selected peer disconnected!\n");
1317 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
1318 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
1319 get_processing_delay (),
1320 &forward_request_task,
1324 no_route = GNUNET_NO;
1325 /* FIXME: check against DBLOCK_SIZE and possibly return
1326 amount to reserve; however, this also needs to work
1327 with testcases which currently start out with a far
1328 too low per-peer bw limit, so they would never send
1329 anything. Big issue. */
1335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1336 "Failed to reserve bandwidth for reply (got %d/%u bytes only)!\n",
1340 GNUNET_STATISTICS_update (stats,
1341 gettext_noop ("# reply bandwidth reservation requests failed"),
1344 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
1345 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
1346 get_processing_delay (),
1347 &forward_request_task,
1349 return; /* this target round failed */
1351 /* FIXME: if we are "quite" busy, we may still want to skip
1352 this round; need more load detection code! */
1353 no_route = GNUNET_YES;
1356 GNUNET_STATISTICS_update (stats,
1357 gettext_noop ("# requests forwarded"),
1360 /* build message and insert message into priority queue */
1362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1363 "Forwarding request `%s' to `%4s'!\n",
1364 GNUNET_h2s (&pr->query),
1369 if (GNUNET_YES == no_route)
1371 bm |= GET_MESSAGE_BIT_RETURN_TO;
1374 if (pr->namespace != NULL)
1376 bm |= GET_MESSAGE_BIT_SKS_NAMESPACE;
1379 if (pr->target_pid != 0)
1381 bm |= GET_MESSAGE_BIT_TRANSMIT_TO;
1384 msize = sizeof (struct GetMessage) + pr->bf_size + k * sizeof(GNUNET_HashCode);
1385 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1386 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1388 gm = (struct GetMessage*) &pm[1];
1389 gm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_GET);
1390 gm->header.size = htons (msize);
1391 gm->type = htonl (pr->type);
1392 pr->remaining_priority /= 2;
1393 gm->priority = htonl (pr->remaining_priority);
1394 gm->ttl = htonl (pr->ttl);
1395 gm->filter_mutator = htonl(pr->mingle);
1396 gm->hash_bitmap = htonl (bm);
1397 gm->query = pr->query;
1398 ext = (GNUNET_HashCode*) &gm[1];
1400 if (GNUNET_YES == no_route)
1401 GNUNET_PEER_resolve (pr->cp->pid, (struct GNUNET_PeerIdentity*) &ext[k++]);
1402 if (pr->namespace != NULL)
1403 memcpy (&ext[k++], pr->namespace, sizeof (GNUNET_HashCode));
1404 if (pr->target_pid != 0)
1405 GNUNET_PEER_resolve (pr->target_pid, (struct GNUNET_PeerIdentity*) &ext[k++]);
1406 bfdata = (char *) &ext[k];
1408 GNUNET_CONTAINER_bloomfilter_get_raw_data (pr->bf,
1411 pm->cont = &transmit_query_continuation;
1413 add_to_pending_messages_for_peer (cp, pm, pr);
1418 * Closure used for "target_peer_select_cb".
1420 struct PeerSelectionContext
1423 * The request for which we are selecting
1426 struct PendingRequest *pr;
1429 * Current "prime" target.
1431 struct GNUNET_PeerIdentity target;
1434 * How much do we like this target?
1436 double target_score;
1442 * Function called for each connected peer to determine
1443 * which one(s) would make good targets for forwarding.
1445 * @param cls closure (struct PeerSelectionContext)
1446 * @param key current key code (peer identity)
1447 * @param value value in the hash map (struct ConnectedPeer)
1448 * @return GNUNET_YES if we should continue to
1453 target_peer_select_cb (void *cls,
1454 const GNUNET_HashCode * key,
1457 struct PeerSelectionContext *psc = cls;
1458 struct ConnectedPeer *cp = value;
1459 struct PendingRequest *pr = psc->pr;
1463 /* 1) check if we have already (recently) forwarded to this peer */
1464 for (i=0;i<pr->used_pids_off;i++)
1465 if (pr->used_pids[i] == cp->pid)
1466 return GNUNET_YES; /* skip */
1467 // 2) calculate how much we'd like to forward to this peer
1468 score = 42; // FIXME!
1469 // FIXME: also need API to gather data on responsiveness
1470 // of this peer (we have fields for that in 'cp', but
1471 // they are never set!)
1473 /* store best-fit in closure */
1474 if (score > psc->target_score)
1476 psc->target_score = score;
1477 psc->target.hashPubKey = *key;
1484 * We're processing a GET request from another peer and have decided
1485 * to forward it to other peers. This function is called periodically
1486 * and should forward the request to other peers until we have all
1487 * possible replies. If we have transmitted the *only* reply to
1488 * the initiator we should destroy the pending request. If we have
1489 * many replies in the queue to the initiator, we should delay sending
1490 * out more queries until the reply queue has shrunk some.
1492 * @param cls our "struct ProcessGetContext *"
1496 forward_request_task (void *cls,
1497 const struct GNUNET_SCHEDULER_TaskContext *tc)
1499 struct PendingRequest *pr = cls;
1500 struct PeerSelectionContext psc;
1501 struct ConnectedPeer *cp;
1503 pr->task = GNUNET_SCHEDULER_NO_TASK;
1504 if (pr->irc != NULL)
1505 return; /* already pending */
1506 /* (1) select target */
1508 psc.target_score = DBL_MIN;
1509 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
1510 &target_peer_select_cb,
1512 if (psc.target_score == DBL_MIN)
1515 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1516 "No peer selected for forwarding!\n");
1518 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
1519 get_processing_delay (),
1520 &forward_request_task,
1522 return; /* nobody selected */
1525 /* (2) reserve reply bandwidth */
1526 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
1527 &psc.target.hashPubKey);
1528 pr->irc = GNUNET_CORE_peer_change_preference (sched, cfg,
1530 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1531 GNUNET_BANDWIDTH_value_init ((uint32_t) -1 /* no limit */),
1533 (uint64_t) cp->inc_preference,
1534 &target_reservation_cb,
1536 cp->inc_preference = 0.0;
1540 /* **************************** P2P PUT Handling ************************ */
1544 * Function called after we either failed or succeeded
1545 * at transmitting a reply to a peer.
1547 * @param cls the requests "struct PendingRequest*"
1548 * @param tpid ID of receiving peer, 0 on transmission error
1551 transmit_reply_continuation (void *cls,
1552 GNUNET_PEER_Id tpid)
1554 struct PendingRequest *pr = cls;
1558 case GNUNET_DATASTORE_BLOCKTYPE_DBLOCK:
1559 case GNUNET_DATASTORE_BLOCKTYPE_IBLOCK:
1560 /* only one reply expected, done with the request! */
1561 destroy_pending_request (pr);
1563 case GNUNET_DATASTORE_BLOCKTYPE_KBLOCK:
1564 case GNUNET_DATASTORE_BLOCKTYPE_SBLOCK:
1574 * Check if the given KBlock is well-formed.
1576 * @param kb the kblock data (or at least "dsize" bytes claiming to be one)
1577 * @param dsize size of "kb" in bytes; check for < sizeof(struct KBlock)!
1578 * @param query where to store the query that this block answers
1579 * @return GNUNET_OK if this is actually a well-formed KBlock
1582 check_kblock (const struct KBlock *kb,
1584 GNUNET_HashCode *query)
1586 if (dsize < sizeof (struct KBlock))
1588 GNUNET_break_op (0);
1589 return GNUNET_SYSERR;
1591 if (dsize - sizeof (struct KBlock) !=
1592 ntohs (kb->purpose.size)
1593 - sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose)
1594 - sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) )
1596 GNUNET_break_op (0);
1597 return GNUNET_SYSERR;
1600 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_KBLOCK,
1605 GNUNET_break_op (0);
1606 return GNUNET_SYSERR;
1609 GNUNET_CRYPTO_hash (&kb->keyspace,
1610 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1617 * Check if the given SBlock is well-formed.
1619 * @param sb the sblock data (or at least "dsize" bytes claiming to be one)
1620 * @param dsize size of "kb" in bytes; check for < sizeof(struct SBlock)!
1621 * @param query where to store the query that this block answers
1622 * @param namespace where to store the namespace that this block belongs to
1623 * @return GNUNET_OK if this is actually a well-formed SBlock
1626 check_sblock (const struct SBlock *sb,
1628 GNUNET_HashCode *query,
1629 GNUNET_HashCode *namespace)
1631 if (dsize < sizeof (struct SBlock))
1633 GNUNET_break_op (0);
1634 return GNUNET_SYSERR;
1637 ntohs (sb->purpose.size) + sizeof (struct GNUNET_CRYPTO_RsaSignature))
1639 GNUNET_break_op (0);
1640 return GNUNET_SYSERR;
1643 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_SBLOCK,
1648 GNUNET_break_op (0);
1649 return GNUNET_SYSERR;
1652 *query = sb->identifier;
1653 if (namespace != NULL)
1654 GNUNET_CRYPTO_hash (&sb->subspace,
1655 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1662 * Transmit the given message by copying it to the target buffer
1663 * "buf". "buf" will be NULL and "size" zero if the socket was closed
1664 * for writing in the meantime. In that case, do nothing
1665 * (the disconnect or shutdown handler will take care of the rest).
1666 * If we were able to transmit messages and there are still more
1667 * pending, ask core again for further calls to this function.
1669 * @param cls closure, pointer to the 'struct ClientList*'
1670 * @param size number of bytes available in buf
1671 * @param buf where the callee should write the message
1672 * @return number of bytes written to buf
1675 transmit_to_client (void *cls,
1676 size_t size, void *buf)
1678 struct ClientList *cl = cls;
1680 struct ClientResponseMessage *creply;
1687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1688 "Not sending reply, client communication problem.\n");
1693 while ( (NULL != (creply = cl->res_head) ) &&
1694 (creply->msize <= size) )
1696 memcpy (&cbuf[msize], &creply[1], creply->msize);
1697 msize += creply->msize;
1698 size -= creply->msize;
1699 GNUNET_CONTAINER_DLL_remove (cl->res_head,
1702 GNUNET_free (creply);
1705 cl->th = GNUNET_SERVER_notify_transmit_ready (cl->client,
1707 GNUNET_TIME_UNIT_FOREVER_REL,
1708 &transmit_to_client,
1711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1712 "Transmitted %u bytes to client\n",
1713 (unsigned int) msize);
1720 * Closure for "process_reply" function.
1722 struct ProcessReplyClosure
1725 * The data for the reply.
1729 // FIXME: add 'struct ConnectedPeer' to track 'last_xxx_replies' here!
1732 * When the reply expires.
1734 struct GNUNET_TIME_Absolute expiration;
1742 * Namespace that this reply belongs to
1743 * (if it is of type SBLOCK).
1745 GNUNET_HashCode namespace;
1748 * Type of the block.
1753 * How much was this reply worth to us?
1760 * We have received a reply; handle it!
1762 * @param cls response (struct ProcessReplyClosure)
1763 * @param key our query
1764 * @param value value in the hash map (info about the query)
1765 * @return GNUNET_YES (we should continue to iterate)
1768 process_reply (void *cls,
1769 const GNUNET_HashCode * key,
1772 struct ProcessReplyClosure *prq = cls;
1773 struct PendingRequest *pr = value;
1774 struct PendingMessage *reply;
1775 struct ClientResponseMessage *creply;
1776 struct ClientList *cl;
1777 struct PutMessage *pm;
1778 struct ConnectedPeer *cp;
1779 GNUNET_HashCode chash;
1780 GNUNET_HashCode mhash;
1785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1786 "Matched result (type %u) for query `%s' with pending request\n",
1787 (unsigned int) prq->type,
1790 GNUNET_STATISTICS_update (stats,
1791 gettext_noop ("# replies received and matched"),
1794 do_remove = GNUNET_NO;
1795 GNUNET_CRYPTO_hash (prq->data,
1800 case GNUNET_DATASTORE_BLOCKTYPE_DBLOCK:
1801 case GNUNET_DATASTORE_BLOCKTYPE_IBLOCK:
1802 /* only possible reply, stop requesting! */
1803 while (NULL != pr->pending_head)
1804 destroy_pending_message_list_entry (pr->pending_head);
1805 if (pr->drq != NULL)
1807 if (pr->client_request_list != NULL)
1808 GNUNET_SERVER_receive_done (pr->client_request_list->client_list->client,
1810 GNUNET_FS_drq_get_cancel (pr->drq);
1813 do_remove = GNUNET_YES;
1815 case GNUNET_DATASTORE_BLOCKTYPE_SBLOCK:
1816 if (0 != memcmp (pr->namespace,
1818 sizeof (GNUNET_HashCode)))
1819 return GNUNET_YES; /* wrong namespace */
1820 /* then: fall-through! */
1821 case GNUNET_DATASTORE_BLOCKTYPE_KBLOCK:
1824 mingle_hash (&chash, pr->mingle, &mhash);
1825 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (pr->bf,
1827 return GNUNET_YES; /* duplicate */
1828 GNUNET_CONTAINER_bloomfilter_add (pr->bf,
1831 if (pr->client_request_list != NULL)
1833 if (pr->replies_seen_size == pr->replies_seen_off)
1835 GNUNET_array_grow (pr->replies_seen,
1836 pr->replies_seen_size,
1837 pr->replies_seen_size * 2 + 4);
1839 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
1840 pr->bf = refresh_bloomfilter (pr->replies_seen_off,
1845 pr->replies_seen[pr->replies_seen_off++] = chash;
1849 case GNUNET_DATASTORE_BLOCKTYPE_SKBLOCK:
1850 // FIXME: any checks against duplicates for SKBlocks?
1856 prq->priority += pr->remaining_priority;
1857 pr->remaining_priority = 0;
1858 if (pr->client_request_list != NULL)
1861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1862 "Transmitting result for query `%s' to local client\n",
1865 GNUNET_STATISTICS_update (stats,
1866 gettext_noop ("# replies received for local clients"),
1869 cl = pr->client_request_list->client_list;
1870 msize = sizeof (struct PutMessage) + prq->size;
1871 creply = GNUNET_malloc (msize + sizeof (struct ClientResponseMessage));
1872 creply->msize = msize;
1873 creply->client_list = cl;
1874 GNUNET_CONTAINER_DLL_insert_after (cl->res_head,
1878 pm = (struct PutMessage*) &creply[1];
1879 pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
1880 pm->header.size = htons (msize);
1881 pm->type = htonl (prq->type);
1882 pm->expiration = GNUNET_TIME_absolute_hton (prq->expiration);
1883 memcpy (&pm[1], prq->data, prq->size);
1885 cl->th = GNUNET_SERVER_notify_transmit_ready (cl->client,
1887 GNUNET_TIME_UNIT_FOREVER_REL,
1888 &transmit_to_client,
1890 GNUNET_break (cl->th != NULL);
1896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1897 "Transmitting result for query `%s' to other peer (PID=%u)\n",
1899 (unsigned int) cp->pid);
1901 GNUNET_STATISTICS_update (stats,
1902 gettext_noop ("# replies received for other peers"),
1905 msize = sizeof (struct PutMessage) + prq->size;
1906 reply = GNUNET_malloc (msize + sizeof (struct PendingMessage));
1907 reply->cont = &transmit_reply_continuation;
1908 reply->cont_cls = pr;
1909 reply->msize = msize;
1910 reply->priority = (uint32_t) -1; /* send replies first! */
1911 pm = (struct PutMessage*) &reply[1];
1912 pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
1913 pm->header.size = htons (msize);
1914 pm->type = htonl (prq->type);
1915 pm->expiration = GNUNET_TIME_absolute_hton (prq->expiration);
1916 memcpy (&pm[1], prq->data, prq->size);
1917 add_to_pending_messages_for_peer (cp, reply, pr);
1919 if (GNUNET_YES == do_remove)
1921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1922 "Removing request `%s' from request map (has been satisfied)\n",
1924 GNUNET_break (GNUNET_YES ==
1925 GNUNET_CONTAINER_multihashmap_remove (query_request_map,
1928 // FIXME: request somehow does not fully
1929 // disappear; how to fix?
1930 // destroy_pending_request (pr); (not like this!)
1933 // FIXME: implement hot-path routing statistics keeping!
1939 * Handle P2P "PUT" message.
1941 * @param cls closure, always NULL
1942 * @param other the other peer involved (sender or receiver, NULL
1943 * for loopback messages where we are both sender and receiver)
1944 * @param message the actual message
1945 * @param latency reported latency of the connection with 'other'
1946 * @param distance reported distance (DV) to 'other'
1947 * @return GNUNET_OK to keep the connection open,
1948 * GNUNET_SYSERR to close it (signal serious error)
1951 handle_p2p_put (void *cls,
1952 const struct GNUNET_PeerIdentity *other,
1953 const struct GNUNET_MessageHeader *message,
1954 struct GNUNET_TIME_Relative latency,
1957 const struct PutMessage *put;
1961 struct GNUNET_TIME_Absolute expiration;
1962 GNUNET_HashCode query;
1963 struct ProcessReplyClosure prq;
1965 msize = ntohs (message->size);
1966 if (msize < sizeof (struct PutMessage))
1969 return GNUNET_SYSERR;
1971 put = (const struct PutMessage*) message;
1972 dsize = msize - sizeof (struct PutMessage);
1973 type = ntohl (put->type);
1974 expiration = GNUNET_TIME_absolute_ntoh (put->expiration);
1976 /* first, validate! */
1979 case GNUNET_DATASTORE_BLOCKTYPE_DBLOCK:
1980 case GNUNET_DATASTORE_BLOCKTYPE_IBLOCK:
1981 GNUNET_CRYPTO_hash (&put[1], dsize, &query);
1983 case GNUNET_DATASTORE_BLOCKTYPE_KBLOCK:
1985 check_kblock ((const struct KBlock*) &put[1],
1988 return GNUNET_SYSERR;
1990 case GNUNET_DATASTORE_BLOCKTYPE_SBLOCK:
1992 check_sblock ((const struct SBlock*) &put[1],
1996 return GNUNET_SYSERR;
1998 case GNUNET_DATASTORE_BLOCKTYPE_SKBLOCK:
1999 // FIXME -- validate SKBLOCK!
2003 /* unknown block type */
2004 GNUNET_break_op (0);
2005 return GNUNET_SYSERR;
2009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2010 "Received result for query `%s' from peer `%4s'\n",
2011 GNUNET_h2s (&query),
2012 GNUNET_i2s (other));
2014 GNUNET_STATISTICS_update (stats,
2015 gettext_noop ("# replies received (overall)"),
2018 /* now, lookup 'query' */
2019 prq.data = (const void*) &put[1];
2022 prq.expiration = expiration;
2024 GNUNET_CONTAINER_multihashmap_get_multiple (query_request_map,
2028 // FIXME: if migration is on and load is low,
2029 // queue to store data in datastore;
2030 // use "prq.priority" for that!
2035 /* **************************** P2P GET Handling ************************ */
2039 * Closure for 'check_duplicate_request_{peer,client}'.
2041 struct CheckDuplicateRequestClosure
2044 * The new request we should check if it already exists.
2046 const struct PendingRequest *pr;
2049 * Existing request found by the checker, NULL if none.
2051 struct PendingRequest *have;
2056 * Iterator over entries in the 'query_request_map' that
2057 * tries to see if we have the same request pending from
2058 * the same client already.
2060 * @param cls closure (our 'struct CheckDuplicateRequestClosure')
2061 * @param key current key code (query, ignored, must match)
2062 * @param value value in the hash map (a 'struct PendingRequest'
2063 * that already exists)
2064 * @return GNUNET_YES if we should continue to
2065 * iterate (no match yet)
2066 * GNUNET_NO if not (match found).
2069 check_duplicate_request_client (void *cls,
2070 const GNUNET_HashCode * key,
2073 struct CheckDuplicateRequestClosure *cdc = cls;
2074 struct PendingRequest *have = value;
2076 if (have->client_request_list == NULL)
2078 if ( (cdc->pr->client_request_list->client_list->client == have->client_request_list->client_list->client) &&
2089 * We're processing (local) results for a search request
2090 * from another peer. Pass applicable results to the
2091 * peer and if we are done either clean up (operation
2092 * complete) or forward to other peers (more results possible).
2094 * @param cls our closure (struct LocalGetContext)
2095 * @param key key for the content
2096 * @param size number of bytes in data
2097 * @param data content stored
2098 * @param type type of the content
2099 * @param priority priority of the content
2100 * @param anonymity anonymity-level for the content
2101 * @param expiration expiration time for the content
2102 * @param uid unique identifier for the datum;
2103 * maybe 0 if no unique identifier is available
2106 process_local_reply (void *cls,
2107 const GNUNET_HashCode * key,
2113 struct GNUNET_TIME_Absolute
2117 struct PendingRequest *pr = cls;
2118 struct ProcessReplyClosure prq;
2119 struct CheckDuplicateRequestClosure cdrc;
2120 GNUNET_HashCode dhash;
2121 GNUNET_HashCode mhash;
2122 GNUNET_HashCode query;
2127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2128 "Done processing local replies, forwarding request to other peers.\n");
2131 if (pr->client_request_list != NULL)
2133 GNUNET_SERVER_receive_done (pr->client_request_list->client_list->client,
2135 /* Figure out if this is a duplicate request and possibly
2136 merge 'struct PendingRequest' entries */
2139 GNUNET_CONTAINER_multihashmap_get_multiple (query_request_map,
2141 &check_duplicate_request_client,
2143 if (cdrc.have != NULL)
2146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2147 "Received request for block `%s' twice from client, will only request once.\n",
2148 GNUNET_h2s (&pr->query));
2151 destroy_pending_request (pr);
2156 /* no more results */
2157 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
2158 pr->task = GNUNET_SCHEDULER_add_now (sched,
2159 &forward_request_task,
2163 if (type == GNUNET_DATASTORE_BLOCKTYPE_ONDEMAND)
2166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2167 "Found ONDEMAND block, performing on-demand encoding\n");
2169 GNUNET_STATISTICS_update (stats,
2170 gettext_noop ("# on-demand blocks matched requests"),
2174 GNUNET_FS_handle_on_demand_block (key, size, data, type, priority,
2175 anonymity, expiration, uid,
2176 &process_local_reply,
2178 GNUNET_FS_drq_get_next (GNUNET_YES);
2181 /* check for duplicates */
2182 GNUNET_CRYPTO_hash (data, size, &dhash);
2183 mingle_hash (&dhash,
2186 if ( (pr->bf != NULL) &&
2188 GNUNET_CONTAINER_bloomfilter_test (pr->bf,
2192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2193 "Result from datastore filtered by bloomfilter (duplicate).\n");
2195 GNUNET_STATISTICS_update (stats,
2196 gettext_noop ("# results filtered by query bloomfilter"),
2199 GNUNET_FS_drq_get_next (GNUNET_YES);
2203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2204 "Found result for query `%s' in local datastore\n",
2207 GNUNET_STATISTICS_update (stats,
2208 gettext_noop ("# results found locally"),
2211 pr->results_found++;
2212 if ( (pr->type == GNUNET_DATASTORE_BLOCKTYPE_KBLOCK) ||
2213 (pr->type == GNUNET_DATASTORE_BLOCKTYPE_SBLOCK) ||
2214 (pr->type == GNUNET_DATASTORE_BLOCKTYPE_SKBLOCK) )
2219 pr->bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
2223 GNUNET_CONTAINER_bloomfilter_add (pr->bf,
2226 memset (&prq, 0, sizeof (prq));
2228 prq.expiration = expiration;
2230 if ( (type == GNUNET_DATASTORE_BLOCKTYPE_SBLOCK) &&
2231 (GNUNET_OK != check_sblock ((const struct SBlock*) data,
2237 /* FIXME: consider removing the block? */
2238 GNUNET_FS_drq_get_next (GNUNET_YES);
2242 prq.priority = priority;
2243 process_reply (&prq, key, pr);
2245 if ( ( (pr->client_request_list == NULL) &&
2246 ( (GNUNET_YES == test_load_too_high()) ||
2247 (pr->results_found > 5 + 2 * pr->priority) ) ) ||
2248 (type == GNUNET_DATASTORE_BLOCKTYPE_DBLOCK) )
2251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2252 "Unique reply found or load too high, done with request\n");
2254 if (type != GNUNET_DATASTORE_BLOCKTYPE_DBLOCK)
2255 GNUNET_STATISTICS_update (stats,
2256 gettext_noop ("# processing result set cut short due to load"),
2259 GNUNET_FS_drq_get_next (GNUNET_NO);
2262 GNUNET_FS_drq_get_next (GNUNET_YES);
2267 * The priority level imposes a bound on the maximum
2268 * value for the ttl that can be requested.
2270 * @param ttl_in requested ttl
2271 * @param prio given priority
2272 * @return ttl_in if ttl_in is below the limit,
2273 * otherwise the ttl-limit for the given priority
2276 bound_ttl (int32_t ttl_in, uint32_t prio)
2278 unsigned long long allowed;
2282 allowed = ((unsigned long long) prio) * TTL_DECREMENT / 1000;
2283 if (ttl_in > allowed)
2285 if (allowed >= (1 << 30))
2294 * We've received a request with the specified priority. Bound it
2295 * according to how much we trust the given peer.
2297 * @param prio_in requested priority
2298 * @param cp the peer making the request
2299 * @return effective priority
2302 bound_priority (uint32_t prio_in,
2303 struct ConnectedPeer *cp)
2310 * Iterator over entries in the 'query_request_map' that
2311 * tries to see if we have the same request pending from
2312 * the same peer already.
2314 * @param cls closure (our 'struct CheckDuplicateRequestClosure')
2315 * @param key current key code (query, ignored, must match)
2316 * @param value value in the hash map (a 'struct PendingRequest'
2317 * that already exists)
2318 * @return GNUNET_YES if we should continue to
2319 * iterate (no match yet)
2320 * GNUNET_NO if not (match found).
2323 check_duplicate_request_peer (void *cls,
2324 const GNUNET_HashCode * key,
2327 struct CheckDuplicateRequestClosure *cdc = cls;
2328 struct PendingRequest *have = value;
2330 if (cdc->pr->target_pid == have->target_pid)
2340 * Handle P2P "GET" request.
2342 * @param cls closure, always NULL
2343 * @param other the other peer involved (sender or receiver, NULL
2344 * for loopback messages where we are both sender and receiver)
2345 * @param message the actual message
2346 * @param latency reported latency of the connection with 'other'
2347 * @param distance reported distance (DV) to 'other'
2348 * @return GNUNET_OK to keep the connection open,
2349 * GNUNET_SYSERR to close it (signal serious error)
2352 handle_p2p_get (void *cls,
2353 const struct GNUNET_PeerIdentity *other,
2354 const struct GNUNET_MessageHeader *message,
2355 struct GNUNET_TIME_Relative latency,
2358 struct PendingRequest *pr;
2359 struct ConnectedPeer *cp;
2360 struct ConnectedPeer *cps;
2361 struct CheckDuplicateRequestClosure cdc;
2362 struct GNUNET_TIME_Relative timeout;
2364 const struct GetMessage *gm;
2366 const GNUNET_HashCode *opt;
2369 uint32_t ttl_decrement;
2374 msize = ntohs(message->size);
2375 if (msize < sizeof (struct GetMessage))
2377 GNUNET_break_op (0);
2378 return GNUNET_SYSERR;
2380 gm = (const struct GetMessage*) message;
2381 type = ntohl (gm->type);
2384 case GNUNET_DATASTORE_BLOCKTYPE_ANY:
2385 case GNUNET_DATASTORE_BLOCKTYPE_DBLOCK:
2386 case GNUNET_DATASTORE_BLOCKTYPE_IBLOCK:
2387 case GNUNET_DATASTORE_BLOCKTYPE_KBLOCK:
2388 case GNUNET_DATASTORE_BLOCKTYPE_SBLOCK:
2391 GNUNET_break_op (0);
2392 return GNUNET_SYSERR;
2394 bm = ntohl (gm->hash_bitmap);
2402 if (msize < sizeof (struct GetMessage) + bits * sizeof (GNUNET_HashCode))
2404 GNUNET_break_op (0);
2405 return GNUNET_SYSERR;
2407 opt = (const GNUNET_HashCode*) &gm[1];
2408 bfsize = msize - sizeof (struct GetMessage) + bits * sizeof (GNUNET_HashCode);
2409 bm = ntohl (gm->hash_bitmap);
2410 if ( (0 != (bm & GET_MESSAGE_BIT_SKS_NAMESPACE)) &&
2411 (type != GNUNET_DATASTORE_BLOCKTYPE_SBLOCK) )
2413 GNUNET_break_op (0);
2414 return GNUNET_SYSERR;
2417 cps = GNUNET_CONTAINER_multihashmap_get (connected_peers,
2418 &other->hashPubKey);
2419 GNUNET_assert (NULL != cps);
2420 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
2421 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
2428 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
2429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2430 "Failed to find RETURN-TO peer `%4s' in connection set. Dropping query.\n",
2431 GNUNET_i2s ((const struct GNUNET_PeerIdentity*) &opt[bits-1]));
2434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2435 "Failed to find peer `%4s' in connection set. Dropping query.\n",
2436 GNUNET_i2s (other));
2438 GNUNET_STATISTICS_update (stats,
2439 gettext_noop ("# requests dropped due to missing reverse route"),
2442 /* FIXME: try connect? */
2445 /* note that we can really only check load here since otherwise
2446 peers could find out that we are overloaded by not being
2447 disconnected after sending us a malformed query... */
2448 if (GNUNET_YES == test_load_too_high ())
2451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2452 "Dropping query from `%s', this peer is too busy.\n",
2453 GNUNET_i2s (other));
2455 GNUNET_STATISTICS_update (stats,
2456 gettext_noop ("# requests dropped due to high load"),
2463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2464 "Received request for `%s' of type %u from peer `%4s'\n",
2465 GNUNET_h2s (&gm->query),
2466 (unsigned int) type,
2467 GNUNET_i2s (other));
2469 have_ns = (0 != (bm & GET_MESSAGE_BIT_SKS_NAMESPACE));
2470 pr = GNUNET_malloc (sizeof (struct PendingRequest) +
2471 (have_ns ? sizeof(GNUNET_HashCode) : 0));
2473 pr->namespace = (GNUNET_HashCode*) &pr[1];
2475 pr->mingle = ntohl (gm->filter_mutator);
2476 if (0 != (bm & GET_MESSAGE_BIT_SKS_NAMESPACE))
2477 memcpy (&pr[1], &opt[bits++], sizeof (GNUNET_HashCode));
2478 if (0 != (bm & GET_MESSAGE_BIT_TRANSMIT_TO))
2479 pr->target_pid = GNUNET_PEER_intern ((const struct GNUNET_PeerIdentity*) &opt[bits++]);
2481 pr->anonymity_level = 1;
2482 pr->priority = bound_priority (ntohl (gm->priority), cps);
2483 pr->ttl = bound_ttl (ntohl (gm->ttl), pr->priority);
2484 pr->query = gm->query;
2485 /* decrement ttl (always) */
2486 ttl_decrement = 2 * TTL_DECREMENT +
2487 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2489 if ( (pr->ttl < 0) &&
2490 (pr->ttl - ttl_decrement > 0) )
2493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2494 "Dropping query from `%s' due to TTL underflow.\n",
2495 GNUNET_i2s (other));
2497 GNUNET_STATISTICS_update (stats,
2498 gettext_noop ("# requests dropped due TTL underflow"),
2501 /* integer underflow => drop (should be very rare)! */
2505 pr->ttl -= ttl_decrement;
2506 pr->start_time = GNUNET_TIME_absolute_get ();
2508 /* get bloom filter */
2511 pr->bf = GNUNET_CONTAINER_bloomfilter_init ((const char*) &opt[bits],
2514 pr->bf_size = bfsize;
2519 GNUNET_CONTAINER_multihashmap_get_multiple (query_request_map,
2521 &check_duplicate_request_peer,
2523 if (cdc.have != NULL)
2525 if (cdc.have->start_time.value + cdc.have->ttl >=
2526 pr->start_time.value + pr->ttl)
2528 /* existing request has higher TTL, drop new one! */
2529 cdc.have->priority += pr->priority;
2530 destroy_pending_request (pr);
2532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2533 "Have existing request with higher TTL, dropping new request.\n",
2534 GNUNET_i2s (other));
2536 GNUNET_STATISTICS_update (stats,
2537 gettext_noop ("# requests dropped due to existing request with higher TTL"),
2544 /* existing request has lower TTL, drop old one! */
2545 pr->priority += cdc.have->priority;
2546 /* Possible optimization: if we have applicable pending
2547 replies in 'cdc.have', we might want to move those over
2548 (this is a really rare special-case, so it is not clear
2549 that this would be worth it) */
2550 destroy_pending_request (cdc.have);
2551 /* keep processing 'pr'! */
2556 GNUNET_CONTAINER_multihashmap_put (query_request_map,
2559 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2560 GNUNET_CONTAINER_multihashmap_put (peer_request_map,
2563 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2565 pr->hnode = GNUNET_CONTAINER_heap_insert (requests_by_expiration_heap,
2567 pr->start_time.value + pr->ttl);
2569 GNUNET_STATISTICS_update (stats,
2570 gettext_noop ("# P2P searches active"),
2574 /* calculate change in traffic preference */
2575 preference = (double) pr->priority;
2576 if (preference < QUERY_BANDWIDTH_VALUE)
2577 preference = QUERY_BANDWIDTH_VALUE;
2578 cps->inc_preference += preference;
2580 /* process locally */
2581 if (type == GNUNET_DATASTORE_BLOCKTYPE_DBLOCK)
2582 type = GNUNET_DATASTORE_BLOCKTYPE_ANY; /* to get on-demand as well */
2583 timeout = GNUNET_TIME_relative_multiply (BASIC_DATASTORE_REQUEST_DELAY,
2584 (pr->priority + 1));
2585 pr->drq = GNUNET_FS_drq_get (&gm->query,
2587 &process_local_reply,
2592 /* Are multiple results possible? If so, start processing remotely now! */
2595 case GNUNET_DATASTORE_BLOCKTYPE_DBLOCK:
2596 case GNUNET_DATASTORE_BLOCKTYPE_IBLOCK:
2597 /* only one result, wait for datastore */
2600 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
2601 pr->task = GNUNET_SCHEDULER_add_now (sched,
2602 &forward_request_task,
2606 /* make sure we don't track too many requests */
2607 if (GNUNET_CONTAINER_heap_get_size (requests_by_expiration_heap) > max_pending_requests)
2609 pr = GNUNET_CONTAINER_heap_peek (requests_by_expiration_heap);
2610 destroy_pending_request (pr);
2616 /* **************************** CS GET Handling ************************ */
2620 * Handle START_SEARCH-message (search request from client).
2622 * @param cls closure
2623 * @param client identification of the client
2624 * @param message the actual message
2627 handle_start_search (void *cls,
2628 struct GNUNET_SERVER_Client *client,
2629 const struct GNUNET_MessageHeader *message)
2631 static GNUNET_HashCode all_zeros;
2632 const struct SearchMessage *sm;
2633 struct ClientList *cl;
2634 struct ClientRequestList *crl;
2635 struct PendingRequest *pr;
2640 msize = ntohs (message->size);
2641 if ( (msize < sizeof (struct SearchMessage)) ||
2642 (0 != (msize - sizeof (struct SearchMessage)) % sizeof (GNUNET_HashCode)) )
2645 GNUNET_SERVER_receive_done (client,
2649 sc = (msize - sizeof (struct SearchMessage)) / sizeof (GNUNET_HashCode);
2650 sm = (const struct SearchMessage*) message;
2653 while ( (cl != NULL) &&
2654 (cl->client != client) )
2658 cl = GNUNET_malloc (sizeof (struct ClientList));
2659 cl->client = client;
2660 GNUNET_SERVER_client_keep (client);
2661 cl->next = client_list;
2664 type = ntohl (sm->type);
2667 case GNUNET_DATASTORE_BLOCKTYPE_ANY:
2668 case GNUNET_DATASTORE_BLOCKTYPE_DBLOCK:
2669 case GNUNET_DATASTORE_BLOCKTYPE_IBLOCK:
2670 case GNUNET_DATASTORE_BLOCKTYPE_KBLOCK:
2671 case GNUNET_DATASTORE_BLOCKTYPE_SBLOCK:
2675 GNUNET_SERVER_receive_done (client,
2680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2681 "Received request for `%s' of type %u from local client\n",
2682 GNUNET_h2s (&sm->query),
2683 (unsigned int) type);
2686 /* detect duplicate KBLOCK requests */
2687 if (type == GNUNET_DATASTORE_BLOCKTYPE_KBLOCK)
2690 while ( (crl != NULL) &&
2691 ( (0 != memcmp (&crl->req->query,
2693 sizeof (GNUNET_HashCode))) ||
2694 (crl->req->type != type) ) )
2699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2700 "Have existing request, merging content-seen lists.\n");
2703 /* Duplicate request (used to send long list of
2704 known/blocked results); merge 'pr->replies_seen'
2705 and update bloom filter */
2706 GNUNET_array_grow (pr->replies_seen,
2707 pr->replies_seen_size,
2708 pr->replies_seen_off + sc);
2709 memcpy (&pr->replies_seen[pr->replies_seen_off],
2711 sc * sizeof (GNUNET_HashCode));
2712 pr->replies_seen_off += sc;
2714 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
2715 pr->bf = refresh_bloomfilter (pr->replies_seen_off,
2719 GNUNET_STATISTICS_update (stats,
2720 gettext_noop ("# client searches updated (merged content seen list)"),
2723 GNUNET_SERVER_receive_done (client,
2728 GNUNET_STATISTICS_update (stats,
2729 gettext_noop ("# client searches active"),
2732 pr = GNUNET_malloc (sizeof (struct PendingRequest) +
2733 ((type == GNUNET_DATASTORE_BLOCKTYPE_SBLOCK)?sizeof(GNUNET_HashCode):0));
2734 crl = GNUNET_malloc (sizeof (struct ClientRequestList));
2735 memset (crl, 0, sizeof (struct ClientRequestList));
2736 crl->client_list = cl;
2737 GNUNET_CONTAINER_DLL_insert (cl->rl_head,
2742 pr->client_request_list = crl;
2743 GNUNET_array_grow (pr->replies_seen,
2744 pr->replies_seen_size,
2746 memcpy (pr->replies_seen,
2748 sc * sizeof (GNUNET_HashCode));
2749 pr->replies_seen_off = sc;
2750 pr->anonymity_level = ntohl (sm->anonymity_level);
2751 pr->bf = refresh_bloomfilter (pr->replies_seen_off,
2755 pr->query = sm->query;
2758 case GNUNET_DATASTORE_BLOCKTYPE_DBLOCK:
2759 case GNUNET_DATASTORE_BLOCKTYPE_IBLOCK:
2760 if (0 != memcmp (&sm->target,
2762 sizeof (GNUNET_HashCode)))
2763 pr->target_pid = GNUNET_PEER_intern ((const struct GNUNET_PeerIdentity*) &sm->target);
2765 case GNUNET_DATASTORE_BLOCKTYPE_SBLOCK:
2766 pr->namespace = (GNUNET_HashCode*) &pr[1];
2767 memcpy (&pr[1], &sm->target, sizeof (GNUNET_HashCode));
2772 GNUNET_CONTAINER_multihashmap_put (query_request_map,
2775 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2776 if (type == GNUNET_DATASTORE_BLOCKTYPE_DBLOCK)
2777 type = GNUNET_DATASTORE_BLOCKTYPE_ANY; /* get on-demand blocks too! */
2778 pr->drq = GNUNET_FS_drq_get (&sm->query,
2780 &process_local_reply,
2782 GNUNET_TIME_UNIT_FOREVER_REL,
2787 /* **************************** Startup ************************ */
2791 * List of handlers for P2P messages
2792 * that we care about.
2794 static struct GNUNET_CORE_MessageHandler p2p_handlers[] =
2797 GNUNET_MESSAGE_TYPE_FS_GET, 0 },
2799 GNUNET_MESSAGE_TYPE_FS_PUT, 0 },
2805 * List of handlers for the messages understood by this
2808 static struct GNUNET_SERVER_MessageHandler handlers[] = {
2809 {&GNUNET_FS_handle_index_start, NULL,
2810 GNUNET_MESSAGE_TYPE_FS_INDEX_START, 0},
2811 {&GNUNET_FS_handle_index_list_get, NULL,
2812 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_GET, sizeof(struct GNUNET_MessageHeader) },
2813 {&GNUNET_FS_handle_unindex, NULL, GNUNET_MESSAGE_TYPE_FS_UNINDEX,
2814 sizeof (struct UnindexMessage) },
2815 {&handle_start_search, NULL, GNUNET_MESSAGE_TYPE_FS_START_SEARCH,
2822 * Process fs requests.
2824 * @param s scheduler to use
2825 * @param server the initialized server
2826 * @param c configuration to use
2829 main_init (struct GNUNET_SCHEDULER_Handle *s,
2830 struct GNUNET_SERVER_Handle *server,
2831 const struct GNUNET_CONFIGURATION_Handle *c)
2835 stats = GNUNET_STATISTICS_create (sched, "fs", cfg);
2836 connected_peers = GNUNET_CONTAINER_multihashmap_create (128); // FIXME: get size from config
2837 query_request_map = GNUNET_CONTAINER_multihashmap_create (128); // FIXME: get size from config
2838 peer_request_map = GNUNET_CONTAINER_multihashmap_create (128); // FIXME: get size from config
2839 requests_by_expiration_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
2840 core = GNUNET_CORE_connect (sched,
2842 GNUNET_TIME_UNIT_FOREVER_REL,
2846 &peer_connect_handler,
2847 &peer_disconnect_handler,
2853 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2854 _("Failed to connect to `%s' service.\n"),
2856 GNUNET_CONTAINER_multihashmap_destroy (connected_peers);
2857 connected_peers = NULL;
2858 GNUNET_CONTAINER_multihashmap_destroy (query_request_map);
2859 query_request_map = NULL;
2860 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
2861 requests_by_expiration_heap = NULL;
2862 GNUNET_CONTAINER_multihashmap_destroy (peer_request_map);
2863 peer_request_map = NULL;
2865 return GNUNET_SYSERR;
2867 GNUNET_SERVER_disconnect_notify (server,
2868 &handle_client_disconnect,
2870 GNUNET_SERVER_add_handlers (server, handlers);
2871 GNUNET_SCHEDULER_add_delayed (sched,
2872 GNUNET_TIME_UNIT_FOREVER_REL,
2880 * Process fs requests.
2882 * @param cls closure
2883 * @param sched scheduler to use
2884 * @param server the initialized server
2885 * @param cfg configuration to use
2889 struct GNUNET_SCHEDULER_Handle *sched,
2890 struct GNUNET_SERVER_Handle *server,
2891 const struct GNUNET_CONFIGURATION_Handle *cfg)
2893 if ( (GNUNET_OK != GNUNET_FS_drq_init (sched, cfg)) ||
2894 (GNUNET_OK != GNUNET_FS_indexing_init (sched, cfg)) ||
2895 (GNUNET_OK != main_init (sched, server, cfg)) )
2897 GNUNET_SCHEDULER_shutdown (sched);
2904 * The main function for the fs service.
2906 * @param argc number of arguments from the command line
2907 * @param argv command line arguments
2908 * @return 0 ok, 1 on error
2911 main (int argc, char *const *argv)
2913 return (GNUNET_OK ==
2914 GNUNET_SERVICE_run (argc,
2917 GNUNET_SERVICE_OPTION_NONE,
2918 &run, NULL)) ? 0 : 1;
2921 /* end of gnunet-service-fs.c */