2 This file is part of GNUnet.
3 (C) 2009, 2010 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file fs/gnunet-service-fs.c
23 * @brief gnunet anonymity protocol implementation
24 * @author Christian Grothoff
27 * - TTL/priority calculations are absent!
29 * - have non-zero preference / priority for requests we initiate!
30 * - track stats for hot-path routing
31 * - implement hot-path routing decision procedure
32 * - implement: bound_priority, test_load_too_high, validate_nblock
33 * - add content migration support (store locally) [or create new service]
38 #include "gnunet_constants.h"
39 #include "gnunet_core_service.h"
40 #include "gnunet_datastore_service.h"
41 #include "gnunet_peer_lib.h"
42 #include "gnunet_protocols.h"
43 #include "gnunet_signatures.h"
44 #include "gnunet_statistics_service.h"
45 #include "gnunet_util_lib.h"
46 #include "gnunet-service-fs_drq.h"
47 #include "gnunet-service-fs_indexing.h"
50 #define DEBUG_FS GNUNET_YES
53 * Maximum number of outgoing messages we queue per peer.
54 * FIXME: make configurable?
56 #define MAX_QUEUE_PER_PEER 16
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. Note that we only try about every TTL_DECREMENT/2
63 * plus MAX_CORK_DELAY (so roughly every 3.5s).
65 #define RETRY_PROBABILITY_INV 3
68 * What is the maximum delay for a P2P FS message (in our interaction
69 * with core)? FS-internal delays are another story. The value is
70 * chosen based on the 32k block size. Given that peers typcially
71 * have at least 1 kb/s bandwidth, 45s waits give us a chance to
72 * transmit one message even to the lowest-bandwidth peers.
74 #define MAX_TRANSMIT_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 45)
79 * Maximum number of requests (from other peers) that we're
80 * willing to have pending at any given point in time.
81 * FIXME: set from configuration.
83 static uint64_t max_pending_requests = (32 * 1024);
87 * Information we keep for each pending reply. The
88 * actual message follows at the end of this struct.
90 struct PendingMessage;
94 * Function called upon completion of a transmission.
97 * @param pid ID of receiving peer, 0 on transmission error
99 typedef void (*TransmissionContinuation)(void * cls,
100 GNUNET_PEER_Id tpid);
104 * Information we keep for each pending message (GET/PUT). The
105 * actual message follows at the end of this struct.
107 struct PendingMessage
110 * This is a doubly-linked list of messages to the same peer.
112 struct PendingMessage *next;
115 * This is a doubly-linked list of messages to the same peer.
117 struct PendingMessage *prev;
120 * Entry in pending message list for this pending message.
122 struct PendingMessageList *pml;
125 * Function to call immediately once we have transmitted this
128 TransmissionContinuation cont;
136 * Size of the reply; actual reply message follows
137 * at the end of this struct.
142 * How important is this message for us?
150 * Information about a peer that we are connected to.
151 * We track data that is useful for determining which
152 * peers should receive our requests. We also keep
153 * a list of messages to transmit to this peer.
159 * List of the last clients for which this peer successfully
162 struct GNUNET_SERVER_Client *last_client_replies[CS2P_SUCCESS_LIST_SIZE];
165 * List of the last PIDs for which
166 * this peer successfully answered a query;
167 * We use 0 to indicate no successful reply.
169 GNUNET_PEER_Id last_p2p_replies[P2P_SUCCESS_LIST_SIZE];
172 * Average delay between sending the peer a request and
173 * getting a reply (only calculated over the requests for
174 * which we actually got a reply). Calculated
175 * as a moving average: new_delay = ((n-1)*last_delay+curr_delay) / n
177 struct GNUNET_TIME_Relative avg_delay;
180 * Handle for an active request for transmission to this
183 struct GNUNET_CORE_TransmitHandle *cth;
186 * Messages (replies, queries, content migration) we would like to
187 * send to this peer in the near future. Sorted by priority, head.
189 struct PendingMessage *pending_messages_head;
192 * Messages (replies, queries, content migration) we would like to
193 * send to this peer in the near future. Sorted by priority, tail.
195 struct PendingMessage *pending_messages_tail;
198 * Average priority of successful replies. Calculated
199 * as a moving average: new_avg = ((n-1)*last_avg+curr_prio) / n
204 * Increase in traffic preference still to be submitted
205 * to the core service for this peer. FIXME: double or 'uint64_t'?
207 double inc_preference;
210 * The peer's identity.
215 * Size of the linked list of 'pending_messages'.
217 unsigned int pending_requests;
220 * Which offset in "last_p2p_replies" will be updated next?
221 * (we go round-robin).
223 unsigned int last_p2p_replies_woff;
226 * Which offset in "last_client_replies" will be updated next?
227 * (we go round-robin).
229 unsigned int last_client_replies_woff;
235 * Information we keep for each pending request. We should try to
236 * keep this struct as small as possible since its memory consumption
237 * is key to how many requests we can have pending at once.
239 struct PendingRequest;
243 * Doubly-linked list of requests we are performing
244 * on behalf of the same client.
246 struct ClientRequestList
250 * This is a doubly-linked list.
252 struct ClientRequestList *next;
255 * This is a doubly-linked list.
257 struct ClientRequestList *prev;
260 * Request this entry represents.
262 struct PendingRequest *req;
265 * Client list this request belongs to.
267 struct ClientList *client_list;
273 * Replies to be transmitted to the client. The actual
274 * response message is allocated after this struct.
276 struct ClientResponseMessage
279 * This is a doubly-linked list.
281 struct ClientResponseMessage *next;
284 * This is a doubly-linked list.
286 struct ClientResponseMessage *prev;
289 * Client list entry this response belongs to.
291 struct ClientList *client_list;
294 * Number of bytes in the response.
301 * Linked list of clients we are performing requests
307 * This is a linked list.
309 struct ClientList *next;
312 * ID of a client making a request, NULL if this entry is for a
315 struct GNUNET_SERVER_Client *client;
318 * Head of list of requests performed on behalf
319 * of this client right now.
321 struct ClientRequestList *rl_head;
324 * Tail of list of requests performed on behalf
325 * of this client right now.
327 struct ClientRequestList *rl_tail;
330 * Head of linked list of responses.
332 struct ClientResponseMessage *res_head;
335 * Tail of linked list of responses.
337 struct ClientResponseMessage *res_tail;
340 * Context for sending replies.
342 struct GNUNET_CONNECTION_TransmitHandle *th;
348 * Doubly-linked list of messages we are performing
349 * due to a pending request.
351 struct PendingMessageList
355 * This is a doubly-linked list of messages on behalf of the same request.
357 struct PendingMessageList *next;
360 * This is a doubly-linked list of messages on behalf of the same request.
362 struct PendingMessageList *prev;
365 * Message this entry represents.
367 struct PendingMessage *pm;
370 * Request this entry belongs to.
372 struct PendingRequest *req;
375 * Peer this message is targeted for.
377 struct ConnectedPeer *target;
383 * Information we keep for each pending request. We should try to
384 * keep this struct as small as possible since its memory consumption
385 * is key to how many requests we can have pending at once.
387 struct PendingRequest
391 * If this request was made by a client, this is our entry in the
392 * client request list; otherwise NULL.
394 struct ClientRequestList *client_request_list;
397 * Entry of peer responsible for this entry (if this request
398 * was made by a peer).
400 struct ConnectedPeer *cp;
403 * If this is a namespace query, pointer to the hash of the public
404 * key of the namespace; otherwise NULL. Pointer will be to the
405 * end of this struct (so no need to free it).
407 const GNUNET_HashCode *namespace;
410 * Bloomfilter we use to filter out replies that we don't care about
411 * (anymore). NULL as long as we are interested in all replies.
413 struct GNUNET_CONTAINER_BloomFilter *bf;
416 * Context of our GNUNET_CORE_peer_change_preference call.
418 struct GNUNET_CORE_InformationRequestContext *irc;
421 * Hash code of all replies that we have seen so far (only valid
422 * if client is not NULL since we only track replies like this for
425 GNUNET_HashCode *replies_seen;
428 * Node in the heap representing this entry; NULL
429 * if we have no heap node.
431 struct GNUNET_CONTAINER_HeapNode *hnode;
434 * Head of list of messages being performed on behalf of this
437 struct PendingMessageList *pending_head;
440 * Tail of list of messages being performed on behalf of this
443 struct PendingMessageList *pending_tail;
446 * When did we first see this request (form this peer), or, if our
447 * client is initiating, when did we last initiate a search?
449 struct GNUNET_TIME_Absolute start_time;
452 * The query that this request is for.
454 GNUNET_HashCode query;
457 * The task responsible for transmitting queries
460 GNUNET_SCHEDULER_TaskIdentifier task;
463 * (Interned) Peer identifier that identifies a preferred target
466 GNUNET_PEER_Id target_pid;
469 * (Interned) Peer identifiers of peers that have already
470 * received our query for this content.
472 GNUNET_PEER_Id *used_pids;
475 * Our entry in the DRQ (non-NULL while we wait for our
476 * turn to interact with the local database).
478 struct DatastoreRequestQueue *drq;
481 * Size of the 'bf' (in bytes).
486 * Desired anonymity level; only valid for requests from a local client.
488 uint32_t anonymity_level;
491 * How many entries in "used_pids" are actually valid?
493 unsigned int used_pids_off;
496 * How long is the "used_pids" array?
498 unsigned int used_pids_size;
501 * Number of results found for this request.
503 unsigned int results_found;
506 * How many entries in "replies_seen" are actually valid?
508 unsigned int replies_seen_off;
511 * How long is the "replies_seen" array?
513 unsigned int replies_seen_size;
516 * Priority with which this request was made. If one of our clients
517 * made the request, then this is the current priority that we are
518 * using when initiating the request. This value is used when
519 * we decide to reward other peers with trust for providing a reply.
524 * Priority points left for us to spend when forwarding this request
527 uint32_t remaining_priority;
530 * Number to mingle hashes for bloom-filter tests with.
535 * TTL with which we saw this request (or, if we initiated, TTL that
536 * we used for the request).
541 * Type of the content that this request is for.
543 enum GNUNET_BLOCK_Type type;
546 * Remove this request after transmission of the current response.
551 * GNUNET_YES if we should not forward this request to other peers.
561 static struct GNUNET_SCHEDULER_Handle *sched;
566 static const struct GNUNET_CONFIGURATION_Handle *cfg;
569 * Map of peer identifiers to "struct ConnectedPeer" (for that peer).
571 static struct GNUNET_CONTAINER_MultiHashMap *connected_peers;
574 * Map of peer identifiers to "struct PendingRequest" (for that peer).
576 static struct GNUNET_CONTAINER_MultiHashMap *peer_request_map;
579 * Map of query identifiers to "struct PendingRequest" (for that query).
581 static struct GNUNET_CONTAINER_MultiHashMap *query_request_map;
584 * Heap with the request that will expire next at the top. Contains
585 * pointers of type "struct PendingRequest*"; these will *also* be
586 * aliased from the "requests_by_peer" data structures and the
587 * "requests_by_query" table. Note that requests from our clients
588 * don't expire and are thus NOT in the "requests_by_expiration"
589 * (or the "requests_by_peer" tables).
591 static struct GNUNET_CONTAINER_Heap *requests_by_expiration_heap;
594 * Handle for reporting statistics.
596 static struct GNUNET_STATISTICS_Handle *stats;
599 * Linked list of clients we are currently processing requests for.
601 static struct ClientList *client_list;
604 * Pointer to handle to the core service (points to NULL until we've
607 static struct GNUNET_CORE_Handle *core;
610 * Are we allowed to migrate content to this peer.
612 static int active_migration;
614 /* ******************* clean up functions ************************ */
618 * We're done with a particular message list entry.
619 * Free all associated resources.
621 * @param pml entry to destroy
624 destroy_pending_message_list_entry (struct PendingMessageList *pml)
626 GNUNET_CONTAINER_DLL_remove (pml->req->pending_head,
627 pml->req->pending_tail,
629 GNUNET_CONTAINER_DLL_remove (pml->target->pending_messages_head,
630 pml->target->pending_messages_tail,
632 pml->target->pending_requests--;
633 GNUNET_free (pml->pm);
639 * Destroy the given pending message (and call the respective
642 * @param pm message to destroy
643 * @param tpid id of peer that the message was delivered to, or 0 for none
646 destroy_pending_message (struct PendingMessage *pm,
649 struct PendingMessageList *pml = pm->pml;
650 TransmissionContinuation cont;
653 GNUNET_assert (pml->pm == pm);
654 GNUNET_assert ( (tpid == 0) || (tpid == pml->target->pid) );
656 cont_cls = pm->cont_cls;
657 destroy_pending_message_list_entry (pml);
658 cont (cont_cls, tpid);
663 * We're done processing a particular request.
664 * Free all associated resources.
666 * @param pr request to destroy
669 destroy_pending_request (struct PendingRequest *pr)
671 struct GNUNET_PeerIdentity pid;
673 if (pr->hnode != NULL)
675 GNUNET_CONTAINER_heap_remove_node (requests_by_expiration_heap,
679 if (NULL == pr->client_request_list)
681 GNUNET_STATISTICS_update (stats,
682 gettext_noop ("# P2P searches active"),
688 GNUNET_STATISTICS_update (stats,
689 gettext_noop ("# client searches active"),
693 /* might have already been removed from map in 'process_reply' (if
694 there was a unique reply) or never inserted if it was a
695 duplicate; hence ignore the return value here */
696 (void) GNUNET_CONTAINER_multihashmap_remove (query_request_map,
701 GNUNET_FS_drq_get_cancel (pr->drq);
704 if (pr->client_request_list != NULL)
706 GNUNET_CONTAINER_DLL_remove (pr->client_request_list->client_list->rl_head,
707 pr->client_request_list->client_list->rl_tail,
708 pr->client_request_list);
709 GNUNET_free (pr->client_request_list);
710 pr->client_request_list = NULL;
714 GNUNET_PEER_resolve (pr->cp->pid,
716 (void) GNUNET_CONTAINER_multihashmap_remove (peer_request_map,
723 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
728 GNUNET_CORE_peer_change_preference_cancel (pr->irc);
731 if (pr->replies_seen != NULL)
733 GNUNET_free (pr->replies_seen);
734 pr->replies_seen = NULL;
736 if (pr->task != GNUNET_SCHEDULER_NO_TASK)
738 GNUNET_SCHEDULER_cancel (sched,
740 pr->task = GNUNET_SCHEDULER_NO_TASK;
742 while (NULL != pr->pending_head)
743 destroy_pending_message_list_entry (pr->pending_head);
744 GNUNET_PEER_change_rc (pr->target_pid, -1);
745 if (pr->used_pids != NULL)
747 GNUNET_PEER_decrement_rcs (pr->used_pids, pr->used_pids_off);
748 GNUNET_free (pr->used_pids);
749 pr->used_pids_off = 0;
750 pr->used_pids_size = 0;
751 pr->used_pids = NULL;
758 * Method called whenever a given peer connects.
760 * @param cls closure, not used
761 * @param peer peer identity this notification is about
762 * @param latency reported latency of the connection with 'other'
763 * @param distance reported distance (DV) to 'other'
766 peer_connect_handler (void *cls,
768 GNUNET_PeerIdentity * peer,
769 struct GNUNET_TIME_Relative latency,
772 struct ConnectedPeer *cp;
774 cp = GNUNET_malloc (sizeof (struct ConnectedPeer));
775 cp->pid = GNUNET_PEER_intern (peer);
776 GNUNET_break (GNUNET_OK ==
777 GNUNET_CONTAINER_multihashmap_put (connected_peers,
780 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
785 * Free (each) request made by the peer.
787 * @param cls closure, points to peer that the request belongs to
788 * @param key current key code
789 * @param value value in the hash map
790 * @return GNUNET_YES (we should continue to iterate)
793 destroy_request (void *cls,
794 const GNUNET_HashCode * key,
797 const struct GNUNET_PeerIdentity * peer = cls;
798 struct PendingRequest *pr = value;
800 GNUNET_break (GNUNET_YES ==
801 GNUNET_CONTAINER_multihashmap_remove (peer_request_map,
804 destroy_pending_request (pr);
810 * Method called whenever a peer disconnects.
812 * @param cls closure, not used
813 * @param peer peer identity this notification is about
816 peer_disconnect_handler (void *cls,
818 GNUNET_PeerIdentity * peer)
820 struct ConnectedPeer *cp;
821 struct PendingMessage *pm;
824 GNUNET_CONTAINER_multihashmap_get_multiple (peer_request_map,
828 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
832 for (i=0;i<CS2P_SUCCESS_LIST_SIZE;i++)
834 if (NULL != cp->last_client_replies[i])
836 GNUNET_SERVER_client_drop (cp->last_client_replies[i]);
837 cp->last_client_replies[i] = NULL;
840 GNUNET_break (GNUNET_YES ==
841 GNUNET_CONTAINER_multihashmap_remove (connected_peers,
844 GNUNET_PEER_change_rc (cp->pid, -1);
845 GNUNET_PEER_decrement_rcs (cp->last_p2p_replies, P2P_SUCCESS_LIST_SIZE);
847 GNUNET_CORE_notify_transmit_ready_cancel (cp->cth);
848 while (NULL != (pm = cp->pending_messages_head))
849 destroy_pending_message (pm, 0 /* delivery failed */);
850 GNUNET_break (0 == cp->pending_requests);
856 * Iterator over hash map entries that removes all occurences
857 * of the given 'client' from the 'last_client_replies' of the
858 * given connected peer.
860 * @param cls closure, the 'struct GNUNET_SERVER_Client*' to remove
861 * @param key current key code (unused)
862 * @param value value in the hash map (the 'struct ConnectedPeer*' to change)
863 * @return GNUNET_YES (we should continue to iterate)
866 remove_client_from_last_client_replies (void *cls,
867 const GNUNET_HashCode * key,
870 struct GNUNET_SERVER_Client *client = cls;
871 struct ConnectedPeer *cp = value;
874 for (i=0;i<CS2P_SUCCESS_LIST_SIZE;i++)
876 if (cp->last_client_replies[i] == client)
878 GNUNET_SERVER_client_drop (cp->last_client_replies[i]);
879 cp->last_client_replies[i] = NULL;
887 * A client disconnected. Remove all of its pending queries.
889 * @param cls closure, NULL
890 * @param client identification of the client
893 handle_client_disconnect (void *cls,
894 struct GNUNET_SERVER_Client
897 struct ClientList *pos;
898 struct ClientList *prev;
899 struct ClientRequestList *rcl;
900 struct ClientResponseMessage *creply;
906 while ( (NULL != pos) &&
907 (pos->client != client) )
913 return; /* no requests pending for this client */
914 while (NULL != (rcl = pos->rl_head))
916 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
917 "Destroying pending request `%s' on disconnect\n",
918 GNUNET_h2s (&rcl->req->query));
919 destroy_pending_request (rcl->req);
922 client_list = pos->next;
924 prev->next = pos->next;
927 GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th);
930 while (NULL != (creply = pos->res_head))
932 GNUNET_CONTAINER_DLL_remove (pos->res_head,
935 GNUNET_free (creply);
937 GNUNET_SERVER_client_drop (pos->client);
939 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
940 &remove_client_from_last_client_replies,
946 * Iterator to free peer entries.
948 * @param cls closure, unused
949 * @param key current key code
950 * @param value value in the hash map (peer entry)
951 * @return GNUNET_YES (we should continue to iterate)
954 clean_peer (void *cls,
955 const GNUNET_HashCode * key,
958 peer_disconnect_handler (NULL, (const struct GNUNET_PeerIdentity*) key);
964 * Task run during shutdown.
970 shutdown_task (void *cls,
971 const struct GNUNET_SCHEDULER_TaskContext *tc)
973 while (client_list != NULL)
974 handle_client_disconnect (NULL,
975 client_list->client);
976 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
979 GNUNET_break (0 == GNUNET_CONTAINER_heap_get_size (requests_by_expiration_heap));
980 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
981 requests_by_expiration_heap = 0;
982 GNUNET_CONTAINER_multihashmap_destroy (connected_peers);
983 connected_peers = NULL;
984 GNUNET_break (0 == GNUNET_CONTAINER_multihashmap_size (query_request_map));
985 GNUNET_CONTAINER_multihashmap_destroy (query_request_map);
986 query_request_map = NULL;
987 GNUNET_break (0 == GNUNET_CONTAINER_multihashmap_size (peer_request_map));
988 GNUNET_CONTAINER_multihashmap_destroy (peer_request_map);
989 peer_request_map = NULL;
990 GNUNET_assert (NULL != core);
991 GNUNET_CORE_disconnect (core);
995 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1003 /* ******************* Utility functions ******************** */
1007 * Transmit the given message by copying it to the target buffer
1008 * "buf". "buf" will be NULL and "size" zero if the socket was closed
1009 * for writing in the meantime. In that case, do nothing
1010 * (the disconnect or shutdown handler will take care of the rest).
1011 * If we were able to transmit messages and there are still more
1012 * pending, ask core again for further calls to this function.
1014 * @param cls closure, pointer to the 'struct ConnectedPeer*'
1015 * @param size number of bytes available in buf
1016 * @param buf where the callee should write the message
1017 * @return number of bytes written to buf
1020 transmit_to_peer (void *cls,
1021 size_t size, void *buf)
1023 struct ConnectedPeer *cp = cls;
1025 struct GNUNET_PeerIdentity pid;
1026 struct PendingMessage *pm;
1033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1034 "Dropping message, core too busy.\n");
1039 while ( (NULL != (pm = cp->pending_messages_head) ) &&
1040 (pm->msize <= size) )
1042 memcpy (&cbuf[msize], &pm[1], pm->msize);
1045 destroy_pending_message (pm, cp->pid);
1049 GNUNET_PEER_resolve (cp->pid,
1051 cp->cth = GNUNET_CORE_notify_transmit_ready (core,
1053 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1060 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1061 "Transmitting %u bytes to peer %u.\n",
1070 * Add a message to the set of pending messages for the given peer.
1072 * @param cp peer to send message to
1073 * @param pm message to queue
1074 * @param pr request on which behalf this message is being queued
1077 add_to_pending_messages_for_peer (struct ConnectedPeer *cp,
1078 struct PendingMessage *pm,
1079 struct PendingRequest *pr)
1081 struct PendingMessage *pos;
1082 struct PendingMessageList *pml;
1083 struct GNUNET_PeerIdentity pid;
1085 GNUNET_assert (pm->next == NULL);
1086 GNUNET_assert (pm->pml == NULL);
1087 pml = GNUNET_malloc (sizeof (struct PendingMessageList));
1092 GNUNET_CONTAINER_DLL_insert (pr->pending_head,
1095 pos = cp->pending_messages_head;
1096 while ( (pos != NULL) &&
1097 (pm->priority < pos->priority) )
1099 GNUNET_CONTAINER_DLL_insert_after (cp->pending_messages_head,
1100 cp->pending_messages_tail,
1103 cp->pending_requests++;
1104 if (cp->pending_requests > MAX_QUEUE_PER_PEER)
1105 destroy_pending_message (cp->pending_messages_tail, 0);
1106 if (cp->cth == NULL)
1108 /* need to schedule transmission */
1109 GNUNET_PEER_resolve (cp->pid, &pid);
1110 cp->cth = GNUNET_CORE_notify_transmit_ready (core,
1111 cp->pending_messages_head->priority,
1114 cp->pending_messages_head->msize,
1118 if (cp->cth == NULL)
1121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1122 "Failed to schedule transmission with core!\n");
1124 /* FIXME: call stats (rare, bad case) */
1130 * Mingle hash with the mingle_number to produce different bits.
1133 mingle_hash (const GNUNET_HashCode * in,
1134 int32_t mingle_number,
1135 GNUNET_HashCode * hc)
1139 GNUNET_CRYPTO_hash (&mingle_number,
1142 GNUNET_CRYPTO_hash_xor (&m, in, hc);
1147 * Test if the load on this peer is too high
1148 * to even consider processing the query at
1151 * @return GNUNET_YES if the load is too high, GNUNET_NO otherwise
1154 test_load_too_high ()
1156 return GNUNET_NO; // FIXME
1160 /* ******************* Pending Request Refresh Task ******************** */
1165 * We use a random delay to make the timing of requests less
1166 * predictable. This function returns such a random delay. We add a base
1167 * delay of MAX_CORK_DELAY (1s).
1169 * FIXME: make schedule dependent on the specifics of the request?
1170 * Or bandwidth and number of connected peers and load?
1172 * @return random delay to use for some request, between 1s and 1000+TTL_DECREMENT ms
1174 static struct GNUNET_TIME_Relative
1175 get_processing_delay ()
1178 GNUNET_TIME_relative_add (GNUNET_CONSTANTS_MAX_CORK_DELAY,
1179 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1180 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1186 * We're processing a GET request from another peer and have decided
1187 * to forward it to other peers. This function is called periodically
1188 * and should forward the request to other peers until we have all
1189 * possible replies. If we have transmitted the *only* reply to
1190 * the initiator we should destroy the pending request. If we have
1191 * many replies in the queue to the initiator, we should delay sending
1192 * out more queries until the reply queue has shrunk some.
1194 * @param cls our "struct ProcessGetContext *"
1198 forward_request_task (void *cls,
1199 const struct GNUNET_SCHEDULER_TaskContext *tc);
1203 * Function called after we either failed or succeeded
1204 * at transmitting a query to a peer.
1206 * @param cls the requests "struct PendingRequest*"
1207 * @param tpid ID of receiving peer, 0 on transmission error
1210 transmit_query_continuation (void *cls,
1211 GNUNET_PEER_Id tpid)
1213 struct PendingRequest *pr = cls;
1215 GNUNET_STATISTICS_update (stats,
1216 gettext_noop ("# queries scheduled for forwarding"),
1222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1223 "Transmission of request failed, will try again later.\n");
1225 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
1226 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
1227 get_processing_delay (),
1228 &forward_request_task,
1232 GNUNET_STATISTICS_update (stats,
1233 gettext_noop ("# queries forwarded"),
1236 GNUNET_PEER_change_rc (tpid, 1);
1237 if (pr->used_pids_off == pr->used_pids_size)
1238 GNUNET_array_grow (pr->used_pids,
1240 pr->used_pids_size * 2 + 2);
1241 pr->used_pids[pr->used_pids_off++] = tpid;
1242 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
1243 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
1244 get_processing_delay (),
1245 &forward_request_task,
1251 * How many bytes should a bloomfilter be if we have already seen
1252 * entry_count responses? Note that BLOOMFILTER_K gives us the number
1253 * of bits set per entry. Furthermore, we should not re-size the
1254 * filter too often (to keep it cheap).
1256 * Since other peers will also add entries but not resize the filter,
1257 * we should generally pick a slightly larger size than what the
1258 * strict math would suggest.
1260 * @return must be a power of two and smaller or equal to 2^15.
1263 compute_bloomfilter_size (unsigned int entry_count)
1266 unsigned int ideal = (entry_count * BLOOMFILTER_K) / 4;
1267 uint16_t max = 1 << 15;
1269 if (entry_count > max)
1272 while ((size < max) && (size < ideal))
1281 * Recalculate our bloom filter for filtering replies. This function
1282 * will create a new bloom filter from scratch, so it should only be
1283 * called if we have no bloomfilter at all (and hence can create a
1284 * fresh one of minimal size without problems) OR if our peer is the
1285 * initiator (in which case we may resize to larger than mimimum size).
1287 * @param pr request for which the BF is to be recomputed
1290 refresh_bloomfilter (struct PendingRequest *pr)
1294 GNUNET_HashCode mhash;
1296 nsize = compute_bloomfilter_size (pr->replies_seen_off);
1297 if (nsize == pr->bf_size)
1298 return; /* size not changed */
1300 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
1301 pr->bf_size = nsize;
1302 pr->mingle = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, -1);
1303 pr->bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
1306 for (i=0;i<pr->replies_seen_off;i++)
1308 mingle_hash (&pr->replies_seen[i], pr->mingle, &mhash);
1309 GNUNET_CONTAINER_bloomfilter_add (pr->bf, &mhash);
1315 * Function called after we've tried to reserve a certain amount of
1316 * bandwidth for a reply. Check if we succeeded and if so send our
1319 * @param cls the requests "struct PendingRequest*"
1320 * @param peer identifies the peer
1321 * @param bpm_in set to the current bandwidth limit (receiving) for this peer
1322 * @param bpm_out set to the current bandwidth limit (sending) for this peer
1323 * @param amount set to the amount that was actually reserved or unreserved
1324 * @param preference current traffic preference for the given peer
1327 target_reservation_cb (void *cls,
1329 GNUNET_PeerIdentity * peer,
1330 struct GNUNET_BANDWIDTH_Value32NBO bpm_in,
1331 struct GNUNET_BANDWIDTH_Value32NBO bpm_out,
1333 uint64_t preference)
1335 struct PendingRequest *pr = cls;
1336 struct ConnectedPeer *cp;
1337 struct PendingMessage *pm;
1338 struct GetMessage *gm;
1339 GNUNET_HashCode *ext;
1349 /* error in communication with core, try again later */
1350 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
1351 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
1352 get_processing_delay (),
1353 &forward_request_task,
1357 // (3) transmit, update ttl/priority
1358 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
1362 /* Peer must have just left */
1364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1365 "Selected peer disconnected!\n");
1367 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
1368 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
1369 get_processing_delay (),
1370 &forward_request_task,
1374 no_route = GNUNET_NO;
1375 /* FIXME: check against DBLOCK_SIZE and possibly return
1376 amount to reserve; however, this also needs to work
1377 with testcases which currently start out with a far
1378 too low per-peer bw limit, so they would never send
1379 anything. Big issue. */
1385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1386 "Failed to reserve bandwidth for reply (got %d/%u bytes only)!\n",
1390 GNUNET_STATISTICS_update (stats,
1391 gettext_noop ("# reply bandwidth reservation requests failed"),
1394 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
1395 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
1396 get_processing_delay (),
1397 &forward_request_task,
1399 return; /* this target round failed */
1401 /* FIXME: if we are "quite" busy, we may still want to skip
1402 this round; need more load detection code! */
1403 no_route = GNUNET_YES;
1406 GNUNET_STATISTICS_update (stats,
1407 gettext_noop ("# queries scheduled for forwarding"),
1410 /* build message and insert message into priority queue */
1412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1413 "Forwarding request `%s' to `%4s'!\n",
1414 GNUNET_h2s (&pr->query),
1419 if (GNUNET_YES == no_route)
1421 bm |= GET_MESSAGE_BIT_RETURN_TO;
1424 if (pr->namespace != NULL)
1426 bm |= GET_MESSAGE_BIT_SKS_NAMESPACE;
1429 if (pr->target_pid != 0)
1431 bm |= GET_MESSAGE_BIT_TRANSMIT_TO;
1434 msize = sizeof (struct GetMessage) + pr->bf_size + k * sizeof(GNUNET_HashCode);
1435 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1436 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1438 gm = (struct GetMessage*) &pm[1];
1439 gm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_GET);
1440 gm->header.size = htons (msize);
1441 gm->type = htonl (pr->type);
1442 pr->remaining_priority /= 2;
1443 gm->priority = htonl (pr->remaining_priority);
1444 gm->ttl = htonl (pr->ttl);
1445 gm->filter_mutator = htonl(pr->mingle);
1446 gm->hash_bitmap = htonl (bm);
1447 gm->query = pr->query;
1448 ext = (GNUNET_HashCode*) &gm[1];
1450 if (GNUNET_YES == no_route)
1451 GNUNET_PEER_resolve (pr->cp->pid, (struct GNUNET_PeerIdentity*) &ext[k++]);
1452 if (pr->namespace != NULL)
1453 memcpy (&ext[k++], pr->namespace, sizeof (GNUNET_HashCode));
1454 if (pr->target_pid != 0)
1455 GNUNET_PEER_resolve (pr->target_pid, (struct GNUNET_PeerIdentity*) &ext[k++]);
1456 bfdata = (char *) &ext[k];
1458 GNUNET_CONTAINER_bloomfilter_get_raw_data (pr->bf,
1461 pm->cont = &transmit_query_continuation;
1463 add_to_pending_messages_for_peer (cp, pm, pr);
1468 * Closure used for "target_peer_select_cb".
1470 struct PeerSelectionContext
1473 * The request for which we are selecting
1476 struct PendingRequest *pr;
1479 * Current "prime" target.
1481 struct GNUNET_PeerIdentity target;
1484 * How much do we like this target?
1486 double target_score;
1492 * Function called for each connected peer to determine
1493 * which one(s) would make good targets for forwarding.
1495 * @param cls closure (struct PeerSelectionContext)
1496 * @param key current key code (peer identity)
1497 * @param value value in the hash map (struct ConnectedPeer)
1498 * @return GNUNET_YES if we should continue to
1503 target_peer_select_cb (void *cls,
1504 const GNUNET_HashCode * key,
1507 struct PeerSelectionContext *psc = cls;
1508 struct ConnectedPeer *cp = value;
1509 struct PendingRequest *pr = psc->pr;
1514 /* 1) check that this peer is not the initiator */
1516 return GNUNET_YES; /* skip */
1518 /* 2) check if we have already (recently) forwarded to this peer */
1520 for (i=0;i<pr->used_pids_off;i++)
1521 if (pr->used_pids[i] == cp->pid)
1524 if (0 != GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1525 RETRY_PROBABILITY_INV))
1528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1529 "NOT re-trying query that was previously transmitted %u times\n",
1530 (unsigned int) pr->used_pids_off);
1532 return GNUNET_YES; /* skip */
1537 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1538 "Re-trying query that was previously transmitted %u times to this peer\n",
1541 // 3) calculate how much we'd like to forward to this peer
1542 score = 42; // FIXME!
1543 // FIXME: also need API to gather data on responsiveness
1544 // of this peer (we have fields for that in 'cp', but
1545 // they are never set!)
1547 /* store best-fit in closure */
1548 if (score > psc->target_score)
1550 psc->target_score = score;
1551 psc->target.hashPubKey = *key;
1558 * The priority level imposes a bound on the maximum
1559 * value for the ttl that can be requested.
1561 * @param ttl_in requested ttl
1562 * @param prio given priority
1563 * @return ttl_in if ttl_in is below the limit,
1564 * otherwise the ttl-limit for the given priority
1567 bound_ttl (int32_t ttl_in, uint32_t prio)
1569 unsigned long long allowed;
1573 allowed = ((unsigned long long) prio) * TTL_DECREMENT / 1000;
1574 if (ttl_in > allowed)
1576 if (allowed >= (1 << 30))
1585 * We're processing a GET request from another peer and have decided
1586 * to forward it to other peers. This function is called periodically
1587 * and should forward the request to other peers until we have all
1588 * possible replies. If we have transmitted the *only* reply to
1589 * the initiator we should destroy the pending request. If we have
1590 * many replies in the queue to the initiator, we should delay sending
1591 * out more queries until the reply queue has shrunk some.
1593 * @param cls our "struct ProcessGetContext *"
1597 forward_request_task (void *cls,
1598 const struct GNUNET_SCHEDULER_TaskContext *tc)
1600 struct PendingRequest *pr = cls;
1601 struct PeerSelectionContext psc;
1602 struct ConnectedPeer *cp;
1603 struct GNUNET_TIME_Relative delay;
1605 pr->task = GNUNET_SCHEDULER_NO_TASK;
1606 if (pr->irc != NULL)
1609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1610 "Forwarding of query `%s' not attempted due to pending local lookup!\n",
1611 GNUNET_h2s (&pr->query));
1613 return; /* already pending */
1615 if (GNUNET_YES == pr->local_only)
1616 return; /* configured to not do P2P search */
1617 /* (1) select target */
1619 psc.target_score = DBL_MIN;
1620 GNUNET_CONTAINER_multihashmap_iterate (connected_peers,
1621 &target_peer_select_cb,
1623 if (psc.target_score == DBL_MIN)
1625 delay = get_processing_delay ();
1627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1628 "No peer selected for forwarding of query `%s', will try again in %llu ms!\n",
1629 GNUNET_h2s (&pr->query),
1632 pr->task = GNUNET_SCHEDULER_add_delayed (sched,
1634 &forward_request_task,
1636 return; /* nobody selected */
1638 /* (3) update TTL/priority */
1640 if (pr->client_request_list != NULL)
1642 /* FIXME: use better algorithm!? */
1643 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1646 /* FIXME: bound priority by "customary" priority used by other peers
1648 pr->ttl = bound_ttl (pr->ttl + TTL_DECREMENT * 2,
1651 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1652 "Trying query `%s' with priority %u and TTL %d.\n",
1653 GNUNET_h2s (&pr->query),
1660 /* FIXME: should we do something here as well!? */
1663 /* (3) reserve reply bandwidth */
1664 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
1665 &psc.target.hashPubKey);
1666 GNUNET_assert (NULL != cp);
1667 pr->irc = GNUNET_CORE_peer_change_preference (sched, cfg,
1669 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1670 GNUNET_BANDWIDTH_value_init ((uint32_t) -1 /* no limit */),
1672 (uint64_t) cp->inc_preference,
1673 &target_reservation_cb,
1675 cp->inc_preference = 0.0;
1679 /* **************************** P2P PUT Handling ************************ */
1683 * Function called after we either failed or succeeded
1684 * at transmitting a reply to a peer.
1686 * @param cls the requests "struct PendingRequest*"
1687 * @param tpid ID of receiving peer, 0 on transmission error
1690 transmit_reply_continuation (void *cls,
1691 GNUNET_PEER_Id tpid)
1693 struct PendingRequest *pr = cls;
1697 case GNUNET_BLOCK_TYPE_DBLOCK:
1698 case GNUNET_BLOCK_TYPE_IBLOCK:
1699 /* only one reply expected, done with the request! */
1700 destroy_pending_request (pr);
1702 case GNUNET_BLOCK_TYPE_ANY:
1703 case GNUNET_BLOCK_TYPE_KBLOCK:
1704 case GNUNET_BLOCK_TYPE_SBLOCK:
1714 * Transmit the given message by copying it to the target buffer
1715 * "buf". "buf" will be NULL and "size" zero if the socket was closed
1716 * for writing in the meantime. In that case, do nothing
1717 * (the disconnect or shutdown handler will take care of the rest).
1718 * If we were able to transmit messages and there are still more
1719 * pending, ask core again for further calls to this function.
1721 * @param cls closure, pointer to the 'struct ClientList*'
1722 * @param size number of bytes available in buf
1723 * @param buf where the callee should write the message
1724 * @return number of bytes written to buf
1727 transmit_to_client (void *cls,
1728 size_t size, void *buf)
1730 struct ClientList *cl = cls;
1732 struct ClientResponseMessage *creply;
1739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1740 "Not sending reply, client communication problem.\n");
1745 while ( (NULL != (creply = cl->res_head) ) &&
1746 (creply->msize <= size) )
1748 memcpy (&cbuf[msize], &creply[1], creply->msize);
1749 msize += creply->msize;
1750 size -= creply->msize;
1751 GNUNET_CONTAINER_DLL_remove (cl->res_head,
1754 GNUNET_free (creply);
1757 cl->th = GNUNET_SERVER_notify_transmit_ready (cl->client,
1759 GNUNET_TIME_UNIT_FOREVER_REL,
1760 &transmit_to_client,
1763 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1764 "Transmitted %u bytes to client\n",
1765 (unsigned int) msize);
1772 * Closure for "process_reply" function.
1774 struct ProcessReplyClosure
1777 * The data for the reply.
1781 // FIXME: add 'struct ConnectedPeer' to track 'last_xxx_replies' here!
1784 * When the reply expires.
1786 struct GNUNET_TIME_Absolute expiration;
1794 * Namespace that this reply belongs to
1795 * (if it is of type SBLOCK).
1797 GNUNET_HashCode namespace;
1800 * Type of the block.
1802 enum GNUNET_BLOCK_Type type;
1805 * How much was this reply worth to us?
1812 * We have received a reply; handle it!
1814 * @param cls response (struct ProcessReplyClosure)
1815 * @param key our query
1816 * @param value value in the hash map (info about the query)
1817 * @return GNUNET_YES (we should continue to iterate)
1820 process_reply (void *cls,
1821 const GNUNET_HashCode * key,
1824 struct ProcessReplyClosure *prq = cls;
1825 struct PendingRequest *pr = value;
1826 struct PendingMessage *reply;
1827 struct ClientResponseMessage *creply;
1828 struct ClientList *cl;
1829 struct PutMessage *pm;
1830 struct ConnectedPeer *cp;
1831 GNUNET_HashCode chash;
1832 GNUNET_HashCode mhash;
1836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1837 "Matched result (type %u) for query `%s' with pending request\n",
1838 (unsigned int) prq->type,
1841 GNUNET_STATISTICS_update (stats,
1842 gettext_noop ("# replies received and matched"),
1845 GNUNET_CRYPTO_hash (prq->data,
1850 case GNUNET_BLOCK_TYPE_DBLOCK:
1851 case GNUNET_BLOCK_TYPE_IBLOCK:
1852 /* only possible reply, stop requesting! */
1853 while (NULL != pr->pending_head)
1854 destroy_pending_message_list_entry (pr->pending_head);
1855 if (pr->drq != NULL)
1857 if (pr->client_request_list != NULL)
1858 GNUNET_SERVER_receive_done (pr->client_request_list->client_list->client,
1860 GNUNET_FS_drq_get_cancel (pr->drq);
1863 pr->do_remove = GNUNET_YES;
1864 if (pr->task != GNUNET_SCHEDULER_NO_TASK)
1866 GNUNET_SCHEDULER_cancel (sched,
1868 pr->task = GNUNET_SCHEDULER_NO_TASK;
1870 GNUNET_break (GNUNET_YES ==
1871 GNUNET_CONTAINER_multihashmap_remove (query_request_map,
1875 case GNUNET_BLOCK_TYPE_SBLOCK:
1876 if (pr->namespace == NULL)
1881 if (0 != memcmp (pr->namespace,
1883 sizeof (GNUNET_HashCode)))
1885 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1886 _("Reply mismatched in terms of namespace. Discarded.\n"));
1887 return GNUNET_YES; /* wrong namespace */
1889 /* then: fall-through! */
1890 case GNUNET_BLOCK_TYPE_KBLOCK:
1891 case GNUNET_BLOCK_TYPE_NBLOCK:
1894 mingle_hash (&chash, pr->mingle, &mhash);
1895 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (pr->bf,
1898 GNUNET_STATISTICS_update (stats,
1899 gettext_noop ("# duplicate replies discarded (bloomfilter)"),
1903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1904 "Duplicate response `%s', discarding.\n",
1905 GNUNET_h2s (&mhash));
1907 return GNUNET_YES; /* duplicate */
1910 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1911 "New response `%s', adding to filter.\n",
1912 GNUNET_h2s (&mhash));
1915 if (pr->client_request_list != NULL)
1917 if (pr->replies_seen_size == pr->replies_seen_off)
1918 GNUNET_array_grow (pr->replies_seen,
1919 pr->replies_seen_size,
1920 pr->replies_seen_size * 2 + 4);
1921 pr->replies_seen[pr->replies_seen_off++] = chash;
1923 if ( (pr->bf == NULL) ||
1924 (pr->client_request_list != NULL) )
1925 refresh_bloomfilter (pr);
1926 GNUNET_CONTAINER_bloomfilter_add (pr->bf,
1933 prq->priority += pr->remaining_priority;
1934 pr->remaining_priority = 0;
1935 if (pr->client_request_list != NULL)
1937 GNUNET_STATISTICS_update (stats,
1938 gettext_noop ("# replies received for local clients"),
1941 cl = pr->client_request_list->client_list;
1942 msize = sizeof (struct PutMessage) + prq->size;
1943 creply = GNUNET_malloc (msize + sizeof (struct ClientResponseMessage));
1944 creply->msize = msize;
1945 creply->client_list = cl;
1946 GNUNET_CONTAINER_DLL_insert_after (cl->res_head,
1950 pm = (struct PutMessage*) &creply[1];
1951 pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
1952 pm->header.size = htons (msize);
1953 pm->type = htonl (prq->type);
1954 pm->expiration = GNUNET_TIME_absolute_hton (prq->expiration);
1955 memcpy (&pm[1], prq->data, prq->size);
1959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1960 "Transmitting result for query `%s' to client\n",
1963 cl->th = GNUNET_SERVER_notify_transmit_ready (cl->client,
1965 GNUNET_TIME_UNIT_FOREVER_REL,
1966 &transmit_to_client,
1969 GNUNET_break (cl->th != NULL);
1971 destroy_pending_request (pr);
1977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1978 "Transmitting result for query `%s' to other peer (PID=%u)\n",
1980 (unsigned int) cp->pid);
1982 GNUNET_STATISTICS_update (stats,
1983 gettext_noop ("# replies received for other peers"),
1986 msize = sizeof (struct PutMessage) + prq->size;
1987 reply = GNUNET_malloc (msize + sizeof (struct PendingMessage));
1988 reply->cont = &transmit_reply_continuation;
1989 reply->cont_cls = pr;
1990 reply->msize = msize;
1991 reply->priority = (uint32_t) -1; /* send replies first! */
1992 pm = (struct PutMessage*) &reply[1];
1993 pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
1994 pm->header.size = htons (msize);
1995 pm->type = htonl (prq->type);
1996 pm->expiration = GNUNET_TIME_absolute_hton (prq->expiration);
1997 memcpy (&pm[1], prq->data, prq->size);
1998 add_to_pending_messages_for_peer (cp, reply, pr);
2000 // FIXME: implement hot-path routing statistics keeping!
2007 * Continuation called to notify client about result of the
2010 * @param cls closure
2011 * @param success GNUNET_SYSERR on failure
2012 * @param msg NULL on success, otherwise an error message
2015 put_migration_continuation (void *cls,
2024 * Handle P2P "PUT" message.
2026 * @param cls closure, always NULL
2027 * @param other the other peer involved (sender or receiver, NULL
2028 * for loopback messages where we are both sender and receiver)
2029 * @param message the actual message
2030 * @param latency reported latency of the connection with 'other'
2031 * @param distance reported distance (DV) to 'other'
2032 * @return GNUNET_OK to keep the connection open,
2033 * GNUNET_SYSERR to close it (signal serious error)
2036 handle_p2p_put (void *cls,
2037 const struct GNUNET_PeerIdentity *other,
2038 const struct GNUNET_MessageHeader *message,
2039 struct GNUNET_TIME_Relative latency,
2042 const struct PutMessage *put;
2045 enum GNUNET_BLOCK_Type type;
2046 struct GNUNET_TIME_Absolute expiration;
2047 GNUNET_HashCode query;
2048 struct ProcessReplyClosure prq;
2049 const struct SBlock *sb;
2051 msize = ntohs (message->size);
2052 if (msize < sizeof (struct PutMessage))
2055 return GNUNET_SYSERR;
2057 put = (const struct PutMessage*) message;
2058 dsize = msize - sizeof (struct PutMessage);
2059 type = ntohl (put->type);
2060 expiration = GNUNET_TIME_absolute_ntoh (put->expiration);
2063 GNUNET_BLOCK_check_block (type,
2068 GNUNET_break_op (0);
2069 return GNUNET_SYSERR;
2071 if (type == GNUNET_BLOCK_TYPE_ONDEMAND)
2072 return GNUNET_SYSERR;
2073 if (GNUNET_BLOCK_TYPE_SBLOCK == type)
2075 sb = (const struct SBlock*) &put[1];
2076 GNUNET_CRYPTO_hash (&sb->subspace,
2077 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2082 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2083 "Received result for query `%s' from peer `%4s'\n",
2084 GNUNET_h2s (&query),
2085 GNUNET_i2s (other));
2087 GNUNET_STATISTICS_update (stats,
2088 gettext_noop ("# replies received (overall)"),
2091 /* now, lookup 'query' */
2092 prq.data = (const void*) &put[1];
2095 prq.expiration = expiration;
2097 GNUNET_CONTAINER_multihashmap_get_multiple (query_request_map,
2101 if (GNUNET_YES == active_migration)
2103 GNUNET_DATASTORE_put (NULL /* FIXME */,
2104 0, &query, dsize, &put[1],
2105 type, prq.priority, 1 /* anonymity */,
2107 0, 64 /* FIXME: use define */,
2108 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
2109 &put_migration_continuation,
2116 /* **************************** P2P GET Handling ************************ */
2120 * Closure for 'check_duplicate_request_{peer,client}'.
2122 struct CheckDuplicateRequestClosure
2125 * The new request we should check if it already exists.
2127 const struct PendingRequest *pr;
2130 * Existing request found by the checker, NULL if none.
2132 struct PendingRequest *have;
2137 * Iterator over entries in the 'query_request_map' that
2138 * tries to see if we have the same request pending from
2139 * the same client already.
2141 * @param cls closure (our 'struct CheckDuplicateRequestClosure')
2142 * @param key current key code (query, ignored, must match)
2143 * @param value value in the hash map (a 'struct PendingRequest'
2144 * that already exists)
2145 * @return GNUNET_YES if we should continue to
2146 * iterate (no match yet)
2147 * GNUNET_NO if not (match found).
2150 check_duplicate_request_client (void *cls,
2151 const GNUNET_HashCode * key,
2154 struct CheckDuplicateRequestClosure *cdc = cls;
2155 struct PendingRequest *have = value;
2157 if (have->client_request_list == NULL)
2159 if ( (cdc->pr->client_request_list->client_list->client == have->client_request_list->client_list->client) &&
2170 * We're processing (local) results for a search request
2171 * from another peer. Pass applicable results to the
2172 * peer and if we are done either clean up (operation
2173 * complete) or forward to other peers (more results possible).
2175 * @param cls our closure (struct LocalGetContext)
2176 * @param key key for the content
2177 * @param size number of bytes in data
2178 * @param data content stored
2179 * @param type type of the content
2180 * @param priority priority of the content
2181 * @param anonymity anonymity-level for the content
2182 * @param expiration expiration time for the content
2183 * @param uid unique identifier for the datum;
2184 * maybe 0 if no unique identifier is available
2187 process_local_reply (void *cls,
2188 const GNUNET_HashCode * key,
2191 enum GNUNET_BLOCK_Type type,
2194 struct GNUNET_TIME_Absolute
2198 struct PendingRequest *pr = cls;
2199 struct ProcessReplyClosure prq;
2200 struct CheckDuplicateRequestClosure cdrc;
2201 const struct SBlock *sb;
2202 GNUNET_HashCode dhash;
2203 GNUNET_HashCode mhash;
2204 GNUNET_HashCode query;
2209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2210 "Done processing local replies, forwarding request to other peers.\n");
2213 if (pr->client_request_list != NULL)
2215 GNUNET_SERVER_receive_done (pr->client_request_list->client_list->client,
2217 /* Figure out if this is a duplicate request and possibly
2218 merge 'struct PendingRequest' entries */
2221 GNUNET_CONTAINER_multihashmap_get_multiple (query_request_map,
2223 &check_duplicate_request_client,
2225 if (cdrc.have != NULL)
2228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2229 "Received request for block `%s' twice from client, will only request once.\n",
2230 GNUNET_h2s (&pr->query));
2233 destroy_pending_request (pr);
2238 /* no more results */
2239 if (pr->task == GNUNET_SCHEDULER_NO_TASK)
2240 pr->task = GNUNET_SCHEDULER_add_now (sched,
2241 &forward_request_task,
2246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2247 "New local response to `%s' of type %u.\n",
2251 if (type == GNUNET_BLOCK_TYPE_ONDEMAND)
2254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2255 "Found ONDEMAND block, performing on-demand encoding\n");
2257 GNUNET_STATISTICS_update (stats,
2258 gettext_noop ("# on-demand blocks matched requests"),
2262 GNUNET_FS_handle_on_demand_block (key, size, data, type, priority,
2263 anonymity, expiration, uid,
2264 &process_local_reply,
2266 GNUNET_FS_drq_get_next (GNUNET_YES);
2269 /* check for duplicates */
2270 GNUNET_CRYPTO_hash (data, size, &dhash);
2271 mingle_hash (&dhash,
2274 if ( (pr->bf != NULL) &&
2276 GNUNET_CONTAINER_bloomfilter_test (pr->bf,
2280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2281 "Result from datastore filtered by bloomfilter (duplicate).\n");
2283 GNUNET_STATISTICS_update (stats,
2284 gettext_noop ("# results filtered by query bloomfilter"),
2287 GNUNET_FS_drq_get_next (GNUNET_YES);
2291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2292 "Found result for query `%s' in local datastore\n",
2295 GNUNET_STATISTICS_update (stats,
2296 gettext_noop ("# results found locally"),
2299 pr->results_found++;
2300 memset (&prq, 0, sizeof (prq));
2302 prq.expiration = expiration;
2304 if (GNUNET_BLOCK_TYPE_SBLOCK == type)
2306 sb = (const struct SBlock*) data;
2307 GNUNET_CRYPTO_hash (&sb->subspace,
2308 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2311 if (GNUNET_OK != GNUNET_BLOCK_check_block (type,
2317 /* FIXME: consider removing the block? */
2318 GNUNET_FS_drq_get_next (GNUNET_YES);
2322 prq.priority = priority;
2323 process_reply (&prq, key, pr);
2325 if ( (type == GNUNET_BLOCK_TYPE_DBLOCK) ||
2326 (type == GNUNET_BLOCK_TYPE_IBLOCK) )
2328 GNUNET_FS_drq_get_next (GNUNET_NO);
2331 if ( (pr->client_request_list == NULL) &&
2332 ( (GNUNET_YES == test_load_too_high()) ||
2333 (pr->results_found > 5 + 2 * pr->priority) ) )
2336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2337 "Load too high, done with request\n");
2339 GNUNET_STATISTICS_update (stats,
2340 gettext_noop ("# processing result set cut short due to load"),
2343 GNUNET_FS_drq_get_next (GNUNET_NO);
2346 GNUNET_FS_drq_get_next (GNUNET_YES);
2351 * We've received a request with the specified priority. Bound it
2352 * according to how much we trust the given peer.
2354 * @param prio_in requested priority
2355 * @param cp the peer making the request
2356 * @return effective priority
2359 bound_priority (uint32_t prio_in,
2360 struct ConnectedPeer *cp)
2367 * Iterator over entries in the 'query_request_map' that
2368 * tries to see if we have the same request pending from
2369 * the same peer already.
2371 * @param cls closure (our 'struct CheckDuplicateRequestClosure')
2372 * @param key current key code (query, ignored, must match)
2373 * @param value value in the hash map (a 'struct PendingRequest'
2374 * that already exists)
2375 * @return GNUNET_YES if we should continue to
2376 * iterate (no match yet)
2377 * GNUNET_NO if not (match found).
2380 check_duplicate_request_peer (void *cls,
2381 const GNUNET_HashCode * key,
2384 struct CheckDuplicateRequestClosure *cdc = cls;
2385 struct PendingRequest *have = value;
2387 if (cdc->pr->target_pid == have->target_pid)
2397 * Handle P2P "GET" request.
2399 * @param cls closure, always NULL
2400 * @param other the other peer involved (sender or receiver, NULL
2401 * for loopback messages where we are both sender and receiver)
2402 * @param message the actual message
2403 * @param latency reported latency of the connection with 'other'
2404 * @param distance reported distance (DV) to 'other'
2405 * @return GNUNET_OK to keep the connection open,
2406 * GNUNET_SYSERR to close it (signal serious error)
2409 handle_p2p_get (void *cls,
2410 const struct GNUNET_PeerIdentity *other,
2411 const struct GNUNET_MessageHeader *message,
2412 struct GNUNET_TIME_Relative latency,
2415 struct PendingRequest *pr;
2416 struct ConnectedPeer *cp;
2417 struct ConnectedPeer *cps;
2418 struct CheckDuplicateRequestClosure cdc;
2419 struct GNUNET_TIME_Relative timeout;
2421 const struct GetMessage *gm;
2423 const GNUNET_HashCode *opt;
2426 uint32_t ttl_decrement;
2427 enum GNUNET_BLOCK_Type type;
2431 msize = ntohs(message->size);
2432 if (msize < sizeof (struct GetMessage))
2434 GNUNET_break_op (0);
2435 return GNUNET_SYSERR;
2437 gm = (const struct GetMessage*) message;
2438 type = ntohl (gm->type);
2441 case GNUNET_BLOCK_TYPE_ANY:
2442 case GNUNET_BLOCK_TYPE_DBLOCK:
2443 case GNUNET_BLOCK_TYPE_IBLOCK:
2444 case GNUNET_BLOCK_TYPE_KBLOCK:
2445 case GNUNET_BLOCK_TYPE_SBLOCK:
2448 GNUNET_break_op (0);
2449 return GNUNET_SYSERR;
2451 bm = ntohl (gm->hash_bitmap);
2459 if (msize < sizeof (struct GetMessage) + bits * sizeof (GNUNET_HashCode))
2461 GNUNET_break_op (0);
2462 return GNUNET_SYSERR;
2464 opt = (const GNUNET_HashCode*) &gm[1];
2465 bfsize = msize - sizeof (struct GetMessage) + bits * sizeof (GNUNET_HashCode);
2466 bm = ntohl (gm->hash_bitmap);
2467 if ( (0 != (bm & GET_MESSAGE_BIT_SKS_NAMESPACE)) &&
2468 (type != GNUNET_BLOCK_TYPE_SBLOCK) )
2470 GNUNET_break_op (0);
2471 return GNUNET_SYSERR;
2474 cps = GNUNET_CONTAINER_multihashmap_get (connected_peers,
2475 &other->hashPubKey);
2478 /* peer must have just disconnected */
2479 GNUNET_STATISTICS_update (stats,
2480 gettext_noop ("# requests dropped due to initiator not being connected"),
2483 return GNUNET_SYSERR;
2485 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
2486 cp = GNUNET_CONTAINER_multihashmap_get (connected_peers,
2493 if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
2494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2495 "Failed to find RETURN-TO peer `%4s' in connection set. Dropping query.\n",
2496 GNUNET_i2s ((const struct GNUNET_PeerIdentity*) &opt[bits-1]));
2499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2500 "Failed to find peer `%4s' in connection set. Dropping query.\n",
2501 GNUNET_i2s (other));
2503 GNUNET_STATISTICS_update (stats,
2504 gettext_noop ("# requests dropped due to missing reverse route"),
2507 /* FIXME: try connect? */
2510 /* note that we can really only check load here since otherwise
2511 peers could find out that we are overloaded by not being
2512 disconnected after sending us a malformed query... */
2513 if (GNUNET_YES == test_load_too_high ())
2516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2517 "Dropping query from `%s', this peer is too busy.\n",
2518 GNUNET_i2s (other));
2520 GNUNET_STATISTICS_update (stats,
2521 gettext_noop ("# requests dropped due to high load"),
2528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2529 "Received request for `%s' of type %u from peer `%4s' with flags %u\n",
2530 GNUNET_h2s (&gm->query),
2531 (unsigned int) type,
2535 have_ns = (0 != (bm & GET_MESSAGE_BIT_SKS_NAMESPACE));
2536 pr = GNUNET_malloc (sizeof (struct PendingRequest) +
2537 (have_ns ? sizeof(GNUNET_HashCode) : 0));
2539 pr->namespace = (GNUNET_HashCode*) &pr[1];
2541 pr->mingle = ntohl (gm->filter_mutator);
2542 if (0 != (bm & GET_MESSAGE_BIT_SKS_NAMESPACE))
2543 memcpy (&pr[1], &opt[bits++], sizeof (GNUNET_HashCode));
2544 if (0 != (bm & GET_MESSAGE_BIT_TRANSMIT_TO))
2545 pr->target_pid = GNUNET_PEER_intern ((const struct GNUNET_PeerIdentity*) &opt[bits++]);
2547 pr->anonymity_level = 1;
2548 pr->priority = bound_priority (ntohl (gm->priority), cps);
2549 pr->ttl = bound_ttl (ntohl (gm->ttl), pr->priority);
2550 pr->query = gm->query;
2551 /* decrement ttl (always) */
2552 ttl_decrement = 2 * TTL_DECREMENT +
2553 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2555 if ( (pr->ttl < 0) &&
2556 (((int32_t)(pr->ttl - ttl_decrement)) > 0) )
2559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2560 "Dropping query from `%s' due to TTL underflow (%d - %u).\n",
2565 GNUNET_STATISTICS_update (stats,
2566 gettext_noop ("# requests dropped due TTL underflow"),
2569 /* integer underflow => drop (should be very rare)! */
2573 pr->ttl -= ttl_decrement;
2574 pr->start_time = GNUNET_TIME_absolute_get ();
2576 /* get bloom filter */
2579 pr->bf = GNUNET_CONTAINER_bloomfilter_init ((const char*) &opt[bits],
2582 pr->bf_size = bfsize;
2587 GNUNET_CONTAINER_multihashmap_get_multiple (query_request_map,
2589 &check_duplicate_request_peer,
2591 if (cdc.have != NULL)
2593 if (cdc.have->start_time.value + cdc.have->ttl >=
2594 pr->start_time.value + pr->ttl)
2596 /* existing request has higher TTL, drop new one! */
2597 cdc.have->priority += pr->priority;
2598 destroy_pending_request (pr);
2600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2601 "Have existing request with higher TTL, dropping new request.\n",
2602 GNUNET_i2s (other));
2604 GNUNET_STATISTICS_update (stats,
2605 gettext_noop ("# requests dropped due to higher-TTL request"),
2612 /* existing request has lower TTL, drop old one! */
2613 pr->priority += cdc.have->priority;
2614 /* Possible optimization: if we have applicable pending
2615 replies in 'cdc.have', we might want to move those over
2616 (this is a really rare special-case, so it is not clear
2617 that this would be worth it) */
2618 destroy_pending_request (cdc.have);
2619 /* keep processing 'pr'! */
2624 GNUNET_break (GNUNET_OK ==
2625 GNUNET_CONTAINER_multihashmap_put (query_request_map,
2628 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
2629 GNUNET_break (GNUNET_OK ==
2630 GNUNET_CONTAINER_multihashmap_put (peer_request_map,
2633 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
2635 pr->hnode = GNUNET_CONTAINER_heap_insert (requests_by_expiration_heap,
2637 pr->start_time.value + pr->ttl);
2639 GNUNET_STATISTICS_update (stats,
2640 gettext_noop ("# P2P searches received"),
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_BLOCK_TYPE_DBLOCK)
2656 type = GNUNET_BLOCK_TYPE_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_BLOCK_TYPE_DBLOCK:
2670 case GNUNET_BLOCK_TYPE_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;
2712 enum GNUNET_BLOCK_Type type;
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 GNUNET_STATISTICS_update (stats,
2724 gettext_noop ("# client searches received"),
2727 sc = (msize - sizeof (struct SearchMessage)) / sizeof (GNUNET_HashCode);
2728 sm = (const struct SearchMessage*) message;
2729 type = ntohl (sm->type);
2731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2732 "Received request for `%s' of type %u from local client\n",
2733 GNUNET_h2s (&sm->query),
2734 (unsigned int) type);
2738 case GNUNET_BLOCK_TYPE_ANY:
2739 case GNUNET_BLOCK_TYPE_DBLOCK:
2740 case GNUNET_BLOCK_TYPE_IBLOCK:
2741 case GNUNET_BLOCK_TYPE_KBLOCK:
2742 case GNUNET_BLOCK_TYPE_SBLOCK:
2743 case GNUNET_BLOCK_TYPE_NBLOCK:
2747 GNUNET_SERVER_receive_done (client,
2753 while ( (cl != NULL) &&
2754 (cl->client != client) )
2758 cl = GNUNET_malloc (sizeof (struct ClientList));
2759 cl->client = client;
2760 GNUNET_SERVER_client_keep (client);
2761 cl->next = client_list;
2764 /* detect duplicate KBLOCK requests */
2765 if ( (type == GNUNET_BLOCK_TYPE_KBLOCK) ||
2766 (type == GNUNET_BLOCK_TYPE_NBLOCK) ||
2767 (type == GNUNET_BLOCK_TYPE_ANY) )
2770 while ( (crl != NULL) &&
2771 ( (0 != memcmp (&crl->req->query,
2773 sizeof (GNUNET_HashCode))) ||
2774 (crl->req->type != type) ) )
2779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2780 "Have existing request, merging content-seen lists.\n");
2783 /* Duplicate request (used to send long list of
2784 known/blocked results); merge 'pr->replies_seen'
2785 and update bloom filter */
2786 GNUNET_array_grow (pr->replies_seen,
2787 pr->replies_seen_size,
2788 pr->replies_seen_off + sc);
2789 memcpy (&pr->replies_seen[pr->replies_seen_off],
2791 sc * sizeof (GNUNET_HashCode));
2792 pr->replies_seen_off += sc;
2793 refresh_bloomfilter (pr);
2794 GNUNET_STATISTICS_update (stats,
2795 gettext_noop ("# client searches updated (merged content seen list)"),
2798 GNUNET_SERVER_receive_done (client,
2803 GNUNET_STATISTICS_update (stats,
2804 gettext_noop ("# client searches active"),
2807 pr = GNUNET_malloc (sizeof (struct PendingRequest) +
2808 ((type == GNUNET_BLOCK_TYPE_SBLOCK) ? sizeof(GNUNET_HashCode) : 0));
2809 crl = GNUNET_malloc (sizeof (struct ClientRequestList));
2810 memset (crl, 0, sizeof (struct ClientRequestList));
2811 crl->client_list = cl;
2812 GNUNET_CONTAINER_DLL_insert (cl->rl_head,
2817 pr->client_request_list = crl;
2818 GNUNET_array_grow (pr->replies_seen,
2819 pr->replies_seen_size,
2821 memcpy (pr->replies_seen,
2823 sc * sizeof (GNUNET_HashCode));
2824 pr->replies_seen_off = sc;
2825 pr->anonymity_level = ntohl (sm->anonymity_level);
2826 refresh_bloomfilter (pr);
2827 pr->query = sm->query;
2828 if (0 == (1 & ntohl (sm->options)))
2829 pr->local_only = GNUNET_NO;
2831 pr->local_only = GNUNET_YES;
2834 case GNUNET_BLOCK_TYPE_DBLOCK:
2835 case GNUNET_BLOCK_TYPE_IBLOCK:
2836 if (0 != memcmp (&sm->target,
2838 sizeof (GNUNET_HashCode)))
2839 pr->target_pid = GNUNET_PEER_intern ((const struct GNUNET_PeerIdentity*) &sm->target);
2841 case GNUNET_BLOCK_TYPE_SBLOCK:
2842 pr->namespace = (GNUNET_HashCode*) &pr[1];
2843 memcpy (&pr[1], &sm->target, sizeof (GNUNET_HashCode));
2848 GNUNET_break (GNUNET_OK ==
2849 GNUNET_CONTAINER_multihashmap_put (query_request_map,
2852 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
2853 if (type == GNUNET_BLOCK_TYPE_DBLOCK)
2854 type = GNUNET_BLOCK_TYPE_ANY; /* get on-demand blocks too! */
2855 pr->drq = GNUNET_FS_drq_get (&sm->query,
2857 &process_local_reply,
2859 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
2864 /* **************************** Startup ************************ */
2868 * List of handlers for P2P messages
2869 * that we care about.
2871 static struct GNUNET_CORE_MessageHandler p2p_handlers[] =
2874 GNUNET_MESSAGE_TYPE_FS_GET, 0 },
2876 GNUNET_MESSAGE_TYPE_FS_PUT, 0 },
2882 * List of handlers for the messages understood by this
2885 static struct GNUNET_SERVER_MessageHandler handlers[] = {
2886 {&GNUNET_FS_handle_index_start, NULL,
2887 GNUNET_MESSAGE_TYPE_FS_INDEX_START, 0},
2888 {&GNUNET_FS_handle_index_list_get, NULL,
2889 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_GET, sizeof(struct GNUNET_MessageHeader) },
2890 {&GNUNET_FS_handle_unindex, NULL, GNUNET_MESSAGE_TYPE_FS_UNINDEX,
2891 sizeof (struct UnindexMessage) },
2892 {&handle_start_search, NULL, GNUNET_MESSAGE_TYPE_FS_START_SEARCH,
2899 * Process fs requests.
2901 * @param s scheduler to use
2902 * @param server the initialized server
2903 * @param c configuration to use
2906 main_init (struct GNUNET_SCHEDULER_Handle *s,
2907 struct GNUNET_SERVER_Handle *server,
2908 const struct GNUNET_CONFIGURATION_Handle *c)
2912 stats = GNUNET_STATISTICS_create (sched, "fs", cfg);
2913 connected_peers = GNUNET_CONTAINER_multihashmap_create (128); // FIXME: get size from config
2914 query_request_map = GNUNET_CONTAINER_multihashmap_create (128); // FIXME: get size from config
2915 peer_request_map = GNUNET_CONTAINER_multihashmap_create (128); // FIXME: get size from config
2916 requests_by_expiration_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
2917 core = GNUNET_CORE_connect (sched,
2919 GNUNET_TIME_UNIT_FOREVER_REL,
2922 &peer_connect_handler,
2923 &peer_disconnect_handler,
2929 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2930 _("Failed to connect to `%s' service.\n"),
2932 GNUNET_CONTAINER_multihashmap_destroy (connected_peers);
2933 connected_peers = NULL;
2934 GNUNET_CONTAINER_multihashmap_destroy (query_request_map);
2935 query_request_map = NULL;
2936 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
2937 requests_by_expiration_heap = NULL;
2938 GNUNET_CONTAINER_multihashmap_destroy (peer_request_map);
2939 peer_request_map = NULL;
2941 return GNUNET_SYSERR;
2943 GNUNET_SERVER_disconnect_notify (server,
2944 &handle_client_disconnect,
2946 GNUNET_SERVER_add_handlers (server, handlers);
2947 GNUNET_SCHEDULER_add_delayed (sched,
2948 GNUNET_TIME_UNIT_FOREVER_REL,
2956 * Process fs requests.
2958 * @param cls closure
2959 * @param sched scheduler to use
2960 * @param server the initialized server
2961 * @param cfg configuration to use
2965 struct GNUNET_SCHEDULER_Handle *sched,
2966 struct GNUNET_SERVER_Handle *server,
2967 const struct GNUNET_CONFIGURATION_Handle *cfg)
2969 active_migration = GNUNET_CONFIGURATION_get_value_yesno (cfg,
2972 if ( (GNUNET_OK != GNUNET_FS_drq_init (sched, cfg)) ||
2973 (GNUNET_OK != GNUNET_FS_indexing_init (sched, cfg)) ||
2974 (GNUNET_OK != main_init (sched, server, cfg)) )
2976 GNUNET_SCHEDULER_shutdown (sched);
2983 * The main function for the fs service.
2985 * @param argc number of arguments from the command line
2986 * @param argv command line arguments
2987 * @return 0 ok, 1 on error
2990 main (int argc, char *const *argv)
2992 return (GNUNET_OK ==
2993 GNUNET_SERVICE_run (argc,
2996 GNUNET_SERVICE_OPTION_NONE,
2997 &run, NULL)) ? 0 : 1;
3000 /* end of gnunet-service-fs.c */