2 This file is part of GNUnet.
3 (C) 2009 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 program that provides the file-sharing service
24 * @author Christian Grothoff
27 * - tracking of PendingRequests
28 * - setup P2P search on CS request
29 * - setup P2P search on P2P GET
30 * - forward replies based on tracked requests
31 * - validation of KBLOCKS (almost done)
32 * - validation of SBLOCKS
33 * - validation of KSBLOCKS
34 * - content migration (put in local DS)
35 * - possible major issue: we may
36 * queue "gazillions" of (K|S)Blocks for the
37 * core to transmit to another peer; need
38 * to make sure this is bounded overall...
39 * - various load-based actions (can wait)
40 * - remove on-demand blocks if they keep failing (can wait)
43 #include "gnunet_core_service.h"
44 #include "gnunet_datastore_service.h"
45 #include "gnunet_peer_lib.h"
46 #include "gnunet_protocols.h"
47 #include "gnunet_signatures.h"
48 #include "gnunet_util_lib.h"
53 * In-memory information about indexed files (also available
60 * This is a linked list.
62 struct IndexInfo *next;
65 * Name of the indexed file. Memory allocated
66 * at the end of this struct (do not free).
71 * Context for transmitting confirmation to client,
72 * NULL if we've done this already.
74 struct GNUNET_SERVER_TransmitContext *tc;
77 * Hash of the contents of the file.
79 GNUNET_HashCode file_id;
85 * Signature of a function that is called whenever a datastore
86 * request can be processed (or an entry put on the queue times out).
89 * @param ok GNUNET_OK if DS is ready, GNUNET_SYSERR on timeout
91 typedef void (*RequestFunction)(void *cls,
96 * Doubly-linked list of our requests for the datastore.
98 struct DatastoreRequestQueue
102 * This is a doubly-linked list.
104 struct DatastoreRequestQueue *next;
107 * This is a doubly-linked list.
109 struct DatastoreRequestQueue *prev;
112 * Function to call (will issue the request).
122 * When should this request time-out because we don't care anymore?
124 struct GNUNET_TIME_Absolute timeout;
127 * ID of task used for signaling timeout.
129 GNUNET_SCHEDULER_TaskIdentifier task;
135 * Closure for processing START_SEARCH messages from a client.
137 struct LocalGetContext
141 * This is a doubly-linked list.
143 struct LocalGetContext *next;
146 * This is a doubly-linked list.
148 struct LocalGetContext *prev;
151 * Client that initiated the search.
153 struct GNUNET_SERVER_Client *client;
156 * Array of results that we've already received
159 GNUNET_HashCode *results;
162 * Bloomfilter over all results (for fast query construction);
163 * NULL if we don't have any results.
165 struct GNUNET_CONTAINER_BloomFilter *results_bf;
168 * DS request associated with this operation.
170 struct DatastoreRequestQueue *req;
173 * Current result message to transmit to client (or NULL).
175 struct ContentMessage *result;
178 * Type of the content that we're looking for.
184 * Desired anonymity level.
186 uint32_t anonymity_level;
189 * Number of results actually stored in the results array.
191 unsigned int results_used;
194 * Size of the results array in memory.
196 unsigned int results_size;
199 * If the request is for a DBLOCK or IBLOCK, this is the identity of
200 * the peer that is known to have a response. Set to all-zeros if
201 * such a target is not known (note that even if OUR anonymity
202 * level is >0 we may happen to know the responder's identity;
203 * nevertheless, we should probably not use it for a DHT-lookup
204 * or similar blunt actions in order to avoid exposing ourselves).
206 * If the request is for an SBLOCK, this is the identity of the
207 * pseudonym to which the SBLOCK belongs.
209 * If the request is for a KBLOCK, "target" must be all zeros.
211 GNUNET_HashCode target;
214 * Hash of the keyword (aka query) for KBLOCKs; Hash of
215 * the CHK-encoded block for DBLOCKS and IBLOCKS (aka query)
216 * and hash of the identifier XORed with the target for
217 * SBLOCKS (aka query).
219 GNUNET_HashCode query;
225 * Possible routing policies for an FS-GET request.
230 * Simply drop the request.
232 ROUTING_POLICY_NONE = 0,
235 * Answer it if we can from local datastore.
237 ROUTING_POLICY_ANSWER = 1,
240 * Forward the request to other peers (if possible).
242 ROUTING_POLICY_FORWARD = 2,
245 * Forward to other peers, and ask them to route
246 * the response via ourselves.
248 ROUTING_POLICY_INDIRECT = 6,
251 * Do everything we could possibly do (that would
254 ROUTING_POLICY_ALL = 7
259 * Internal context we use for our initial processing
262 struct ProcessGetContext
265 * The search query (used for datastore lookup).
267 GNUNET_HashCode query;
270 * Which peer we should forward the response to.
272 struct GNUNET_PeerIdentity reply_to;
275 * Namespace for the result (only set for SKS requests)
277 GNUNET_HashCode namespace;
280 * Peer that we should forward the query to if possible
281 * (since that peer likely has the content).
283 struct GNUNET_PeerIdentity prime_target;
286 * When did we receive this request?
288 struct GNUNET_TIME_Absolute start_time;
291 * Our entry in the DRQ (non-NULL while we wait for our
292 * turn to interact with the local database).
294 struct DatastoreRequestQueue *drq;
297 * Filter used to eliminate duplicate
298 * results. Can be NULL if we are
299 * not yet filtering any results.
301 struct GNUNET_CONTAINER_BloomFilter *bf;
304 * Bitmap describing which of the optional
305 * hash codes / peer identities were given to us.
310 * Desired block type.
315 * Priority of the request.
320 * In what ways are we going to process
323 enum RoutingPolicy policy;
326 * Time-to-live for the request (value
332 * Number to mingle hashes for bloom-filter
338 * Number of results that were found so far.
340 unsigned int results_found;
345 * All requests from a client are
346 * kept in a doubly-linked list.
348 struct ClientRequestList;
352 * Information we keep for each pending request. We should try to
353 * keep this struct as small as possible since its memory consumption
354 * is key to how many requests we can have pending at once.
356 struct PendingRequest
360 * ID of a client making a request, NULL if this entry is for a
363 struct GNUNET_SERVER_Client *client;
366 * If this request was made by a client,
367 * this is our entry in the client request
368 * list; otherwise NULL.
370 struct ClientRequestList *crl_entry;
373 * If this is a namespace query, pointer to the hash of the public
374 * key of the namespace; otherwise NULL.
376 GNUNET_HashCode *namespace;
379 * Bloomfilter we use to filter out replies that we don't care about
380 * (anymore). NULL as long as we are interested in all replies.
382 struct GNUNET_CONTAINER_BloomFilter *bf;
385 * Hash code of all replies that we have seen so far (only valid
386 * if client is not NULL since we only track replies like this for
389 GNUNET_HashCode *replies_seen;
392 * When did we first see this request (form this peer), or, if our
393 * client is initiating, when did we last initiate a search?
395 struct GNUNET_TIME_Absolute start_time;
398 * The query that this request is for.
400 GNUNET_HashCode query;
403 * (Interned) Peer identifier (only valid if "client" is NULL)
404 * that identifies a peer that gave us this request.
406 GNUNET_PEER_Id source_pid;
409 * (Interned) Peer identifier that identifies a preferred target
412 GNUNET_PEER_Id target_pid;
415 * (Interned) Peer identifiers of peers that have already
416 * received our query for this content.
418 GNUNET_PEER_Id *used_pids;
421 * How many entries in "used_pids" are actually valid?
423 unsigned int used_pids_off;
426 * How long is the "used_pids" array?
428 unsigned int used_pids_size;
431 * How many entries in "replies_seen" are actually valid?
433 unsigned int replies_seen_off;
436 * How long is the "replies_seen" array?
438 unsigned int replies_seen_size;
441 * Priority with which this request was made. If one of our clients
442 * made the request, then this is the current priority that we are
443 * using when initiating the request. This value is used when
444 * we decide to reward other peers with trust for providing a reply.
449 * Priority points left for us to spend when forwarding this request
452 uint32_t remaining_priority;
455 * TTL with which we saw this request (or, if we initiated, TTL that
456 * we used for the request).
461 * Type of the content that this request is for.
469 * All requests from a client are
470 * kept in a doubly-linked list.
472 struct ClientRequestList
475 * This is a doubly-linked list.
477 struct ClientRequestList *next;
480 * This is a doubly-linked list.
482 struct ClientRequestList *prev;
485 * A request from this client.
487 struct PendingRequest *req;
493 * Linked list of all clients that we are
494 * currently processing requests for.
500 * This is a linked list.
502 struct ClientList *next;
505 * What client is this entry for?
507 struct GNUNET_SERVER_Client* client;
510 * Head of the DLL of requests from this client.
512 struct ClientRequestList *head;
515 * Tail of the DLL of requests from this client.
517 struct ClientRequestList *tail;
523 * Closure for "process_reply" function.
525 struct ProcessReplyClosure
528 * The data for the reply.
533 * When the reply expires.
535 struct GNUNET_TIME_Absolute expiration;
548 * How much was this reply worth to us?
555 * Map from queries to pending requests ("struct PendingRequest") for
558 static struct GNUNET_CONTAINER_MultiHashMap *request_map;
561 * Our connection to the datastore.
563 static struct GNUNET_DATASTORE_Handle *dsh;
568 static struct GNUNET_SCHEDULER_Handle *sched;
573 const struct GNUNET_CONFIGURATION_Handle *cfg;
576 * Handle to the core service (NULL until we've connected to it).
578 struct GNUNET_CORE_Handle *core;
581 * Head of doubly-linked LGC list.
583 static struct LocalGetContext *lgc_head;
586 * Tail of doubly-linked LGC list.
588 static struct LocalGetContext *lgc_tail;
591 * Head of request queue for the datastore, sorted by timeout.
593 static struct DatastoreRequestQueue *drq_head;
596 * Tail of request queue for the datastore.
598 static struct DatastoreRequestQueue *drq_tail;
601 * Linked list of indexed files.
603 static struct IndexInfo *indexed_files;
606 * Maps hash over content of indexed files
607 * to the respective filename. The filenames
608 * are pointers into the indexed_files linked
609 * list and do not need to be freed.
611 static struct GNUNET_CONTAINER_MultiHashMap *ifm;
614 * Map of query hash codes to requests.
616 static struct GNUNET_CONTAINER_MultiHashMap *requests_by_query;
619 * Map of peer IDs to requests (for those requests coming
622 static struct GNUNET_CONTAINER_MultiHashMap *requests_by_peer;
625 * Linked list of all of our clients and their requests.
627 static struct ClientList *clients;
630 * Heap with the request that will expire next at the top.
632 static struct GNUNET_CONTAINER_Heap *requests_by_expiration;
636 * Write the current index information list to disk.
641 struct GNUNET_BIO_WriteHandle *wh;
643 struct IndexInfo *pos;
646 GNUNET_CONFIGURATION_get_value_filename (cfg,
651 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
652 _("Configuration option `%s' in section `%s' missing.\n"),
657 wh = GNUNET_BIO_write_open (fn);
660 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
661 _("Could not open `%s'.\n"),
670 GNUNET_BIO_write (wh,
672 sizeof (GNUNET_HashCode))) ||
674 GNUNET_BIO_write_string (wh,
680 GNUNET_BIO_write_close (wh))
682 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
683 _("Error writing `%s'.\n"),
693 * Read index information from disk.
698 struct GNUNET_BIO_ReadHandle *rh;
700 struct IndexInfo *pos;
707 GNUNET_CONFIGURATION_get_value_filename (cfg,
712 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
713 _("Configuration option `%s' in section `%s' missing.\n"),
718 rh = GNUNET_BIO_read_open (fn);
721 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
722 _("Could not open `%s'.\n"),
728 while ( (GNUNET_OK ==
730 "Hash of indexed file",
732 sizeof (GNUNET_HashCode))) &&
734 GNUNET_BIO_read_string (rh,
735 "Name of indexed file",
739 slen = strlen (fname) + 1;
740 pos = GNUNET_malloc (sizeof (struct IndexInfo) + slen);
742 pos->filename = (const char *) &pos[1];
743 memcpy (&pos[1], fname, slen);
745 GNUNET_CONTAINER_multihashmap_put (ifm,
747 (void*) pos->filename,
748 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
754 pos->next = indexed_files;
759 GNUNET_BIO_read_close (rh, &emsg))
766 * We've validated the hash of the file we're about to
767 * index. Signal success to the client and update
768 * our internal data structures.
770 * @param ii the index info entry for the request
773 signal_index_ok (struct IndexInfo *ii)
776 GNUNET_CONTAINER_multihashmap_put (ifm,
778 (void*) ii->filename,
779 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
781 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
782 _("Index request received for file `%s' is indexed as `%s'. Permitting anyway.\n"),
784 (const char*) GNUNET_CONTAINER_multihashmap_get (ifm,
786 GNUNET_SERVER_transmit_context_append (ii->tc,
788 GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK);
789 GNUNET_SERVER_transmit_context_run (ii->tc,
790 GNUNET_TIME_UNIT_MINUTES);
794 ii->next = indexed_files;
797 GNUNET_SERVER_transmit_context_append (ii->tc,
799 GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK);
800 GNUNET_SERVER_transmit_context_run (ii->tc,
801 GNUNET_TIME_UNIT_MINUTES);
807 * Function called once the hash computation over an
808 * indexed file has completed.
810 * @param cls closure, our publishing context
811 * @param res resulting hash, NULL on error
814 hash_for_index_val (void *cls,
815 const GNUNET_HashCode *
818 struct IndexInfo *ii = cls;
820 if ( (res == NULL) ||
823 sizeof(GNUNET_HashCode))) )
825 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
826 _("Hash mismatch trying to index file `%s'\n"),
828 GNUNET_SERVER_transmit_context_append (ii->tc,
830 GNUNET_MESSAGE_TYPE_FS_INDEX_START_FAILED);
831 GNUNET_SERVER_transmit_context_run (ii->tc,
832 GNUNET_TIME_UNIT_MINUTES);
836 signal_index_ok (ii);
841 * Handle INDEX_START-message.
844 * @param client identification of the client
845 * @param message the actual message
848 handle_index_start (void *cls,
849 struct GNUNET_SERVER_Client *client,
850 const struct GNUNET_MessageHeader *message)
852 const struct IndexStartMessage *ism;
855 struct IndexInfo *ii;
862 msize = ntohs(message->size);
863 if ( (msize <= sizeof (struct IndexStartMessage)) ||
864 ( ((const char *)message)[msize-1] != '\0') )
867 GNUNET_SERVER_receive_done (client,
871 ism = (const struct IndexStartMessage*) message;
872 fn = (const char*) &ism[1];
873 dev = ntohl (ism->device);
874 ino = GNUNET_ntohll (ism->inode);
875 ism = (const struct IndexStartMessage*) message;
876 slen = strlen (fn) + 1;
877 ii = GNUNET_malloc (sizeof (struct IndexInfo) + slen);
878 ii->filename = (const char*) &ii[1];
879 memcpy (&ii[1], fn, slen);
880 ii->file_id = ism->file_id;
881 ii->tc = GNUNET_SERVER_transmit_context_create (client);
884 (GNUNET_OK == GNUNET_DISK_file_get_identifiers (fn,
890 /* fast validation OK! */
891 signal_index_ok (ii);
894 /* slow validation, need to hash full file (again) */
895 GNUNET_CRYPTO_hash_file (sched,
896 GNUNET_SCHEDULER_PRIORITY_IDLE,
906 * Handle INDEX_LIST_GET-message.
909 * @param client identification of the client
910 * @param message the actual message
913 handle_index_list_get (void *cls,
914 struct GNUNET_SERVER_Client *client,
915 const struct GNUNET_MessageHeader *message)
917 struct GNUNET_SERVER_TransmitContext *tc;
918 struct IndexInfoMessage *iim;
919 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE];
922 struct GNUNET_MessageHeader *msg;
923 struct IndexInfo *pos;
925 tc = GNUNET_SERVER_transmit_context_create (client);
926 iim = (struct IndexInfoMessage*) buf;
932 iim->file_id = pos->file_id;
934 slen = strlen (fn) + 1;
935 if (slen + sizeof (struct IndexInfoMessage) >
936 GNUNET_SERVER_MAX_MESSAGE_SIZE)
941 memcpy (&iim[1], fn, slen);
942 GNUNET_SERVER_transmit_context_append
945 sizeof (struct IndexInfoMessage)
946 - sizeof (struct GNUNET_MessageHeader) + slen,
947 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_ENTRY);
950 GNUNET_SERVER_transmit_context_append (tc,
952 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_END);
953 GNUNET_SERVER_transmit_context_run (tc,
954 GNUNET_TIME_UNIT_MINUTES);
959 * Handle UNINDEX-message.
962 * @param client identification of the client
963 * @param message the actual message
966 handle_unindex (void *cls,
967 struct GNUNET_SERVER_Client *client,
968 const struct GNUNET_MessageHeader *message)
970 const struct UnindexMessage *um;
971 struct IndexInfo *pos;
972 struct IndexInfo *prev;
973 struct IndexInfo *next;
974 struct GNUNET_SERVER_TransmitContext *tc;
977 um = (const struct UnindexMessage*) message;
984 if (0 == memcmp (&pos->file_id,
986 sizeof (GNUNET_HashCode)))
989 indexed_files = pos->next;
991 prev->next = pos->next;
1001 if (GNUNET_YES == found)
1002 write_index_list ();
1003 tc = GNUNET_SERVER_transmit_context_create (client);
1004 GNUNET_SERVER_transmit_context_append (tc,
1006 GNUNET_MESSAGE_TYPE_FS_UNINDEX_OK);
1007 GNUNET_SERVER_transmit_context_run (tc,
1008 GNUNET_TIME_UNIT_MINUTES);
1013 * Run the next DS request in our
1014 * queue, we're done with the current one.
1019 struct DatastoreRequestQueue *e;
1021 while (NULL != (e = drq_head))
1023 if (0 != GNUNET_TIME_absolute_get_remaining (e->timeout).value)
1025 if (e->task != GNUNET_SCHEDULER_NO_TASK)
1026 GNUNET_SCHEDULER_cancel (sched, e->task);
1027 GNUNET_CONTAINER_DLL_remove (drq_head, drq_tail, e);
1028 e->req (e->req_cls, GNUNET_NO);
1033 if (e->task != GNUNET_SCHEDULER_NO_TASK)
1034 GNUNET_SCHEDULER_cancel (sched, e->task);
1035 e->task = GNUNET_SCHEDULER_NO_TASK;
1036 e->req (e->req_cls, GNUNET_YES);
1037 GNUNET_CONTAINER_DLL_remove (drq_head, drq_tail, e);
1043 * A datastore request had to be timed out.
1045 * @param cls closure (of type "struct DatastoreRequestQueue*")
1046 * @param tc task context, unused
1049 timeout_ds_request (void *cls,
1050 const struct GNUNET_SCHEDULER_TaskContext *tc)
1052 struct DatastoreRequestQueue *e = cls;
1054 e->task = GNUNET_SCHEDULER_NO_TASK;
1055 GNUNET_CONTAINER_DLL_remove (drq_head, drq_tail, e);
1056 e->req (e->req_cls, GNUNET_NO);
1062 * Queue a request for the datastore.
1064 * @param deadline by when the request should run
1065 * @param fun function to call once the request can be run
1066 * @param fun_cls closure for fun
1068 static struct DatastoreRequestQueue *
1069 queue_ds_request (struct GNUNET_TIME_Relative deadline,
1070 RequestFunction fun,
1073 struct DatastoreRequestQueue *e;
1074 struct DatastoreRequestQueue *bef;
1076 if (drq_head == NULL)
1078 /* no other requests pending, run immediately */
1079 fun (fun_cls, GNUNET_OK);
1082 e = GNUNET_malloc (sizeof (struct DatastoreRequestQueue));
1083 e->timeout = GNUNET_TIME_relative_to_absolute (deadline);
1085 e->req_cls = fun_cls;
1086 if (deadline.value == GNUNET_TIME_UNIT_FOREVER_REL.value)
1088 /* local request, highest prio, put at head of queue
1089 regardless of deadline */
1095 while ( (NULL != bef) &&
1096 (e->timeout.value < bef->timeout.value) )
1099 GNUNET_CONTAINER_DLL_insert_after (drq_head, drq_tail, bef, e);
1100 if (deadline.value == GNUNET_TIME_UNIT_FOREVER_REL.value)
1102 e->task = GNUNET_SCHEDULER_add_delayed (sched,
1104 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1105 GNUNET_SCHEDULER_NO_TASK,
1107 &timeout_ds_request,
1114 * Free the state associated with a local get context.
1116 * @param lgc the lgc to free
1119 local_get_context_free (struct LocalGetContext *lgc)
1121 GNUNET_CONTAINER_DLL_remove (lgc_head, lgc_tail, lgc);
1122 GNUNET_SERVER_client_drop (lgc->client);
1123 GNUNET_free_non_null (lgc->results);
1124 if (lgc->results_bf != NULL)
1125 GNUNET_CONTAINER_bloomfilter_free (lgc->results_bf);
1126 if (lgc->req != NULL)
1128 if (lgc->req->task != GNUNET_SCHEDULER_NO_TASK)
1129 GNUNET_SCHEDULER_cancel (sched, lgc->req->task);
1130 GNUNET_CONTAINER_DLL_remove (lgc_head, lgc_tail, lgc);
1131 GNUNET_free (lgc->req);
1138 * We're able to transmit the next (local) result to the client.
1139 * Do it and ask the datastore for more. Or, on error, tell
1140 * the datastore to stop giving us more.
1142 * @param cls our closure (struct LocalGetContext)
1143 * @param max maximum number of bytes we can transmit
1144 * @param buf where to copy our message
1145 * @return number of bytes copied to buf
1148 transmit_local_result (void *cls,
1152 struct LocalGetContext *lgc = cls;
1158 GNUNET_free (lgc->result);
1160 GNUNET_DATASTORE_get_next (dsh, GNUNET_NO);
1163 msize = ntohs (lgc->result->header.size);
1164 GNUNET_assert (max >= msize);
1165 memcpy (buf, lgc->result, msize);
1166 GNUNET_free (lgc->result);
1168 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
1174 * Continuation called from datastore's remove
1178 * @param success did the deletion work?
1179 * @param msg error message
1182 remove_cont (void *cls,
1186 if (GNUNET_OK != success)
1187 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1188 _("Failed to delete bogus block: %s\n"),
1190 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
1195 * Mingle hash with the mingle_number to
1196 * produce different bits.
1199 mingle_hash (const GNUNET_HashCode * in,
1200 int32_t mingle_number,
1201 GNUNET_HashCode * hc)
1205 GNUNET_CRYPTO_hash (&mingle_number,
1208 GNUNET_CRYPTO_hash_xor (&m, in, hc);
1213 * We've received an on-demand encoded block
1214 * from the datastore. Attempt to do on-demand
1215 * encoding and (if successful), call the
1216 * continuation with the resulting block. On
1217 * error, clean up and ask the datastore for
1220 * @param key key for the content
1221 * @param size number of bytes in data
1222 * @param data content stored
1223 * @param type type of the content
1224 * @param priority priority of the content
1225 * @param anonymity anonymity-level for the content
1226 * @param expiration expiration time for the content
1227 * @param uid unique identifier for the datum;
1228 * maybe 0 if no unique identifier is available
1229 * @param cont function to call with the actual block
1230 * @param cont_cls closure for cont
1233 handle_on_demand_block (const GNUNET_HashCode * key,
1239 struct GNUNET_TIME_Absolute
1240 expiration, uint64_t uid,
1241 GNUNET_DATASTORE_Iterator cont,
1244 const struct OnDemandBlock *odb;
1245 GNUNET_HashCode nkey;
1246 struct GNUNET_CRYPTO_AesSessionKey skey;
1247 struct GNUNET_CRYPTO_AesInitializationVector iv;
1248 GNUNET_HashCode query;
1250 char ndata[DBLOCK_SIZE];
1251 char edata[DBLOCK_SIZE];
1253 struct GNUNET_DISK_FileHandle *fh;
1256 if (size != sizeof (struct OnDemandBlock))
1259 GNUNET_DATASTORE_remove (dsh,
1265 GNUNET_TIME_UNIT_FOREVER_REL);
1268 odb = (const struct OnDemandBlock*) data;
1269 off = GNUNET_ntohll (odb->offset);
1270 fn = (const char*) GNUNET_CONTAINER_multihashmap_get (ifm,
1273 if ( (NULL == fn) ||
1274 (NULL == (fh = GNUNET_DISK_file_open (fn,
1275 GNUNET_DISK_OPEN_READ))) ||
1277 GNUNET_DISK_file_seek (fh,
1279 GNUNET_DISK_SEEK_SET)) ||
1281 (nsize = GNUNET_DISK_file_read (fh,
1285 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1286 _("Could not access indexed file `%s' at offset %llu: %s\n"),
1287 GNUNET_h2s (&odb->file_id),
1288 (unsigned long long) off,
1291 GNUNET_DISK_file_close (fh);
1292 /* FIXME: if this happens often, we need
1293 to remove the OnDemand block from the DS! */
1294 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
1297 GNUNET_DISK_file_close (fh);
1298 GNUNET_CRYPTO_hash (ndata,
1301 GNUNET_CRYPTO_hash_to_aes_key (&nkey, &skey, &iv);
1302 GNUNET_CRYPTO_aes_encrypt (ndata,
1307 GNUNET_CRYPTO_hash (edata,
1310 if (0 != memcmp (&query,
1312 sizeof (GNUNET_HashCode)))
1314 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1315 _("Indexed file `%s' changed at offset %llu\n"),
1317 (unsigned long long) off);
1318 /* FIXME: if this happens often, we need
1319 to remove the OnDemand block from the DS! */
1320 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
1327 GNUNET_DATASTORE_BLOCKTYPE_DBLOCK,
1336 * We're processing (local) results for a search request
1337 * from a (local) client. Pass applicable results to the
1338 * client and if we are done either clean up (operation
1339 * complete) or switch to P2P search (more results possible).
1341 * @param cls our closure (struct LocalGetContext)
1342 * @param key key for the content
1343 * @param size number of bytes in data
1344 * @param data content stored
1345 * @param type type of the content
1346 * @param priority priority of the content
1347 * @param anonymity anonymity-level for the content
1348 * @param expiration expiration time for the content
1349 * @param uid unique identifier for the datum;
1350 * maybe 0 if no unique identifier is available
1353 process_local_get_result (void *cls,
1354 const GNUNET_HashCode * key,
1360 struct GNUNET_TIME_Absolute
1364 struct LocalGetContext *lgc = cls;
1370 /* no further results from datastore; continue
1371 processing further requests from the client and
1372 allow the next task to use the datastore; also,
1373 switch to P2P requests or clean up our state. */
1375 GNUNET_SERVER_receive_done (lgc->client,
1377 if ( (lgc->results_used == 0) ||
1378 (lgc->type == GNUNET_DATASTORE_BLOCKTYPE_KBLOCK) ||
1379 (lgc->type == GNUNET_DATASTORE_BLOCKTYPE_SBLOCK) ||
1380 (lgc->type == GNUNET_DATASTORE_BLOCKTYPE_SKBLOCK) )
1382 // FIXME: initiate P2P search
1385 /* got all possible results, clean up! */
1386 local_get_context_free (lgc);
1389 if (type == GNUNET_DATASTORE_BLOCKTYPE_ONDEMAND)
1391 handle_on_demand_block (key, size, data, type, priority,
1392 anonymity, expiration, uid,
1393 &process_local_get_result,
1397 if (type != lgc->type)
1399 /* this should be virtually impossible to reach (DBLOCK
1400 query hash being identical to KBLOCK/SBLOCK query hash);
1401 nevertheless, if it happens, the correct thing is to
1402 simply skip the result. */
1403 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
1406 /* check if this is a result we've alredy
1408 for (i=0;i<lgc->results_used;i++)
1409 if (0 == memcmp (key,
1411 sizeof (GNUNET_HashCode)))
1413 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
1416 if (lgc->results_used == lgc->results_size)
1417 GNUNET_array_grow (lgc->results,
1419 lgc->results_size * 2 + 2);
1420 GNUNET_CRYPTO_hash (data,
1422 &lgc->results[lgc->results_used++]);
1423 msize = size + sizeof (struct ContentMessage);
1424 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1425 lgc->result = GNUNET_malloc (msize);
1426 lgc->result->header.size = htons (msize);
1427 lgc->result->header.type = htons (GNUNET_MESSAGE_TYPE_FS_CONTENT);
1428 lgc->result->type = htonl (type);
1429 lgc->result->expiration = GNUNET_TIME_absolute_hton (expiration);
1430 memcpy (&lgc->result[1],
1433 GNUNET_SERVER_notify_transmit_ready (lgc->client,
1435 GNUNET_TIME_UNIT_FOREVER_REL,
1436 &transmit_local_result,
1442 * We're processing a search request from a local
1443 * client. Now it is our turn to query the datastore.
1445 * @param cls our closure (struct LocalGetContext)
1449 transmit_local_get (void *cls,
1450 const struct GNUNET_SCHEDULER_TaskContext *tc)
1452 struct LocalGetContext *lgc = cls;
1456 if (type == GNUNET_DATASTORE_BLOCKTYPE_DBLOCK)
1457 type = GNUNET_DATASTORE_BLOCKTYPE_ANY; /* to get on-demand as well */
1458 GNUNET_DATASTORE_get (dsh,
1461 &process_local_get_result,
1463 GNUNET_TIME_UNIT_FOREVER_REL);
1468 * We're processing a search request from a local
1469 * client. Now it is our turn to query the datastore.
1471 * @param cls our closure (struct LocalGetContext)
1472 * @param ok did we succeed to queue for datastore access, should always be GNUNET_OK
1475 transmit_local_get_ready (void *cls,
1478 struct LocalGetContext *lgc = cls;
1480 GNUNET_assert (GNUNET_OK == ok);
1481 GNUNET_SCHEDULER_add_continuation (sched,
1483 &transmit_local_get,
1485 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1490 * Handle START_SEARCH-message (search request from client).
1492 * @param cls closure
1493 * @param client identification of the client
1494 * @param message the actual message
1497 handle_start_search (void *cls,
1498 struct GNUNET_SERVER_Client *client,
1499 const struct GNUNET_MessageHeader *message)
1501 const struct SearchMessage *sm;
1502 struct LocalGetContext *lgc;
1506 msize = ntohs (message->size);
1507 if ( (msize < sizeof (struct SearchMessage)) ||
1508 (0 != (msize - sizeof (struct SearchMessage)) % sizeof (GNUNET_HashCode)) )
1511 GNUNET_SERVER_receive_done (client,
1515 sc = (msize - sizeof (struct SearchMessage)) / sizeof (GNUNET_HashCode);
1516 sm = (const struct SearchMessage*) message;
1517 GNUNET_SERVER_client_keep (client);
1518 lgc = GNUNET_malloc (sizeof (struct LocalGetContext));
1521 lgc->results_used = sc;
1522 GNUNET_array_grow (lgc->results,
1525 memcpy (lgc->results,
1527 sc * sizeof (GNUNET_HashCode));
1529 lgc->client = client;
1530 lgc->type = ntohl (sm->type);
1531 lgc->anonymity_level = ntohl (sm->anonymity_level);
1532 lgc->target = sm->target;
1533 lgc->query = sm->query;
1534 GNUNET_CONTAINER_DLL_insert (lgc_head, lgc_tail, lgc);
1535 lgc->req = queue_ds_request (GNUNET_TIME_UNIT_FOREVER_REL,
1536 &transmit_local_get_ready,
1542 * List of handlers for the messages understood by this
1545 static struct GNUNET_SERVER_MessageHandler handlers[] = {
1546 {&handle_index_start, NULL,
1547 GNUNET_MESSAGE_TYPE_FS_INDEX_START, 0},
1548 {&handle_index_list_get, NULL,
1549 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_GET, sizeof(struct GNUNET_MessageHeader) },
1550 {&handle_unindex, NULL, GNUNET_MESSAGE_TYPE_FS_UNINDEX,
1551 sizeof (struct UnindexMessage) },
1552 {&handle_start_search, NULL, GNUNET_MESSAGE_TYPE_FS_START_SEARCH,
1559 * Clean up the memory used by the PendingRequest
1560 * structure (except for the client or peer list
1561 * that the request may be part of).
1563 * @param pr request to clean up
1566 destroy_pending_request (struct PendingRequest *pr)
1568 GNUNET_CONTAINER_multihashmap_remove (requests_by_query,
1571 // FIXME: not sure how this can work (efficiently)
1572 // also, what does the return value mean?
1573 GNUNET_CONTAINER_heap_remove_node (requests_by_expiration,
1576 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
1577 GNUNET_PEER_change_rc (pr->source_pid, -1);
1578 GNUNET_PEER_change_rc (pr->target_pid, -1);
1579 GNUNET_PEER_decrement_rcs (pr->used_pids, pr->used_pids_off);
1580 GNUNET_free_non_null (pr->used_pids);
1581 GNUNET_free_non_null (pr->replies_seen);
1582 GNUNET_free_non_null (pr->namespace);
1588 * A client disconnected. Remove all of its pending queries.
1590 * @param cls closure, NULL
1591 * @param client identification of the client
1594 handle_client_disconnect (void *cls,
1595 struct GNUNET_SERVER_Client
1598 struct LocalGetContext *lgc;
1599 struct ClientList *cpos;
1600 struct ClientList *cprev;
1601 struct ClientRequestList *rl;
1604 while ( (NULL != lgc) &&
1605 (lgc->client != client) )
1608 local_get_context_free (lgc);
1611 while ( (NULL != cpos) &&
1612 (clients->client != client) )
1620 clients = cpos->next;
1622 cprev->next = cpos->next;
1623 while (NULL != (rl = cpos->head))
1625 cpos->head = rl->next;
1626 destroy_pending_request (rl->req);
1635 * Task run during shutdown.
1641 shutdown_task (void *cls,
1642 const struct GNUNET_SCHEDULER_TaskContext *tc)
1644 struct IndexInfo *pos;
1647 GNUNET_CORE_disconnect (core);
1648 GNUNET_DATASTORE_disconnect (dsh,
1651 // FIXME: iterate over 'request_map' to free entries!
1652 GNUNET_CONTAINER_multihashmap_destroy (request_map);
1654 GNUNET_CONTAINER_multihashmap_destroy (ifm);
1656 while (NULL != (pos = indexed_files))
1658 indexed_files = pos->next;
1665 * Free (each) request made by the peer.
1667 * @param cls closure, points to peer that the request belongs to
1668 * @param key current key code
1669 * @param value value in the hash map
1670 * @return GNUNET_YES (we should continue to iterate)
1673 destroy_request (void *cls,
1674 const GNUNET_HashCode * key,
1677 const struct GNUNET_PeerIdentity * peer = cls;
1678 struct PendingRequest *pr = value;
1680 GNUNET_CONTAINER_multihashmap_remove (requests_by_peer,
1683 destroy_pending_request (pr);
1689 * Method called whenever a peer disconnects.
1691 * @param cls closure, not used
1692 * @param peer peer identity this notification is about
1695 peer_disconnect_handler (void *cls,
1697 GNUNET_PeerIdentity * peer)
1699 GNUNET_CONTAINER_multihashmap_get_multiple (requests_by_peer,
1707 * We're processing a GET request from
1708 * another peer and have decided to forward
1709 * it to other peers.
1711 * @param cls our "struct ProcessGetContext *"
1715 forward_get_request (void *cls,
1716 const struct GNUNET_SCHEDULER_TaskContext *tc)
1718 struct ProcessGetContext *pgc = cls;
1720 // FIXME: install entry in
1721 // 'request_map' and do actual
1723 if (pgc->bf != NULL)
1724 GNUNET_CONTAINER_bloomfilter_free (pgc->bf);
1730 * Transmit the given message by copying it to
1731 * the target buffer "buf". "buf" will be
1732 * NULL and "size" zero if the socket was closed for
1733 * writing in the meantime. In that case, only
1737 * @param cls closure, pointer to the message
1738 * @param size number of bytes available in buf
1739 * @param buf where the callee should write the message
1740 * @return number of bytes written to buf
1743 transmit_message (void *cls,
1744 size_t size, void *buf)
1746 struct GNUNET_MessageHeader *msg = cls;
1752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1753 "Dropping reply, core too busy.\n");
1758 msize = ntohs (msg->size);
1759 GNUNET_assert (size >= msize);
1760 memcpy (buf, msg, msize);
1767 * Test if the load on this peer is too high
1768 * to even consider processing the query at
1771 * @return GNUNET_YES if the load is too high, GNUNET_NO otherwise
1774 test_load_too_high ()
1776 return GNUNET_NO; // FIXME
1781 * We're processing (local) results for a search request
1782 * from another peer. Pass applicable results to the
1783 * peer and if we are done either clean up (operation
1784 * complete) or forward to other peers (more results possible).
1786 * @param cls our closure (struct LocalGetContext)
1787 * @param key key for the content
1788 * @param size number of bytes in data
1789 * @param data content stored
1790 * @param type type of the content
1791 * @param priority priority of the content
1792 * @param anonymity anonymity-level for the content
1793 * @param expiration expiration time for the content
1794 * @param uid unique identifier for the datum;
1795 * maybe 0 if no unique identifier is available
1798 process_p2p_get_result (void *cls,
1799 const GNUNET_HashCode * key,
1805 struct GNUNET_TIME_Absolute
1809 struct ProcessGetContext *pgc = cls;
1810 GNUNET_HashCode dhash;
1811 GNUNET_HashCode mhash;
1812 struct PutMessage *reply;
1816 /* no more results */
1817 if ( ( (pgc->policy & ROUTING_POLICY_FORWARD) == ROUTING_POLICY_FORWARD) &&
1818 ( (0 == pgc->results_found) ||
1819 (pgc->type == GNUNET_DATASTORE_BLOCKTYPE_KBLOCK) ||
1820 (pgc->type == GNUNET_DATASTORE_BLOCKTYPE_SBLOCK) ||
1821 (pgc->type == GNUNET_DATASTORE_BLOCKTYPE_SKBLOCK) ) )
1823 GNUNET_SCHEDULER_add_continuation (sched,
1825 &forward_get_request,
1827 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1831 if (pgc->bf != NULL)
1832 GNUNET_CONTAINER_bloomfilter_free (pgc->bf);
1838 if (type == GNUNET_DATASTORE_BLOCKTYPE_ONDEMAND)
1840 handle_on_demand_block (key, size, data, type, priority,
1841 anonymity, expiration, uid,
1842 &process_p2p_get_result,
1846 /* check for duplicates */
1847 GNUNET_CRYPTO_hash (data, size, &dhash);
1848 mingle_hash (&dhash,
1851 if ( (pgc->bf != NULL) &&
1853 GNUNET_CONTAINER_bloomfilter_test (pgc->bf,
1857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1858 "Result from datastore filtered by bloomfilter.\n");
1860 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
1863 pgc->results_found++;
1864 if ( (pgc->type == GNUNET_DATASTORE_BLOCKTYPE_KBLOCK) ||
1865 (pgc->type == GNUNET_DATASTORE_BLOCKTYPE_SBLOCK) ||
1866 (pgc->type == GNUNET_DATASTORE_BLOCKTYPE_SKBLOCK) )
1868 if (pgc->bf == NULL)
1869 pgc->bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
1872 GNUNET_CONTAINER_bloomfilter_add (pgc->bf,
1876 reply = GNUNET_malloc (sizeof (struct PutMessage) + size);
1877 reply->header.size = htons (sizeof (struct PutMessage) + size);
1878 reply->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
1879 reply->type = htonl (type);
1880 reply->expiration = GNUNET_TIME_relative_hton (GNUNET_TIME_absolute_get_remaining (expiration));
1881 memcpy (&reply[1], data, size);
1882 GNUNET_CORE_notify_transmit_ready (core,
1884 ACCEPTABLE_REPLY_DELAY,
1886 sizeof (struct PutMessage) + size,
1889 if ( (GNUNET_YES == test_load_too_high()) ||
1890 (pgc->results_found > 5 + 2 * pgc->priority) )
1892 GNUNET_DATASTORE_get_next (dsh, GNUNET_NO);
1893 pgc->policy &= ~ ROUTING_POLICY_FORWARD;
1896 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
1901 * We're processing a GET request from
1902 * another peer. Give it to our local
1905 * @param cls our "struct ProcessGetContext"
1906 * @param ok did we get a datastore slice or not?
1909 ds_get_request (void *cls,
1912 struct ProcessGetContext *pgc = cls;
1914 struct GNUNET_TIME_Relative timeout;
1916 if (GNUNET_OK != ok)
1918 /* no point in doing P2P stuff if we can't even do local */
1923 if (type == GNUNET_DATASTORE_BLOCKTYPE_DBLOCK)
1924 type = GNUNET_DATASTORE_BLOCKTYPE_ANY; /* to get on-demand as well */
1925 timeout = GNUNET_TIME_relative_multiply (BASIC_DATASTORE_REQUEST_DELAY,
1926 (pgc->priority + 1));
1927 GNUNET_DATASTORE_get (dsh,
1930 &process_p2p_get_result,
1937 * The priority level imposes a bound on the maximum
1938 * value for the ttl that can be requested.
1940 * @param ttl_in requested ttl
1941 * @param priority given priority
1942 * @return ttl_in if ttl_in is below the limit,
1943 * otherwise the ttl-limit for the given priority
1946 bound_ttl (int32_t ttl_in, uint32_t prio)
1948 unsigned long long allowed;
1952 allowed = ((unsigned long long) prio) * TTL_DECREMENT / 1000;
1953 if (ttl_in > allowed)
1955 if (allowed >= (1 << 30))
1964 * We've received a request with the specified
1965 * priority. Bound it according to how much
1966 * we trust the given peer.
1968 * @param prio_in requested priority
1969 * @param peer the peer making the request
1970 * @return effective priority
1973 bound_priority (uint32_t prio_in,
1974 const struct GNUNET_PeerIdentity *peer)
1981 * Handle P2P "GET" request.
1983 * @param cls closure, always NULL
1984 * @param peer the other peer involved (sender or receiver, NULL
1985 * for loopback messages where we are both sender and receiver)
1986 * @param message the actual message
1987 * @return GNUNET_OK to keep the connection open,
1988 * GNUNET_SYSERR to close it (signal serious error)
1991 handle_p2p_get (void *cls,
1992 const struct GNUNET_PeerIdentity *other,
1993 const struct GNUNET_MessageHeader *message)
1996 const struct GetMessage *gm;
1998 const GNUNET_HashCode *opt;
1999 struct ProcessGetContext *pgc;
2002 uint32_t ttl_decrement;
2007 msize = ntohs(message->size);
2008 if (msize < sizeof (struct GetMessage))
2010 GNUNET_break_op (0);
2011 return GNUNET_SYSERR;
2013 gm = (const struct GetMessage*) message;
2014 bm = ntohl (gm->hash_bitmap);
2022 if (msize < sizeof (struct GetMessage) + bits * sizeof (GNUNET_HashCode))
2024 GNUNET_break_op (0);
2025 return GNUNET_SYSERR;
2027 opt = (const GNUNET_HashCode*) &gm[1];
2028 bfsize = msize - sizeof (struct GetMessage) + bits * sizeof (GNUNET_HashCode);
2029 pgc = GNUNET_malloc (sizeof (struct ProcessGetContext));
2031 pgc->bf = GNUNET_CONTAINER_bloomfilter_init ((const char*) &pgc[1],
2034 pgc->type = ntohl (gm->type);
2035 pgc->bm = ntohl (gm->hash_bitmap);
2036 pgc->mingle = gm->filter_mutator;
2038 if (0 != (pgc->bm & GET_MESSAGE_BIT_RETURN_TO))
2039 pgc->reply_to.hashPubKey = opt[bits++];
2041 pgc->reply_to = *other;
2042 if (0 != (pgc->bm & GET_MESSAGE_BIT_SKS_NAMESPACE))
2043 pgc->namespace = opt[bits++];
2044 else if (pgc->type == GNUNET_DATASTORE_BLOCKTYPE_SBLOCK)
2046 GNUNET_break_op (0);
2048 return GNUNET_SYSERR;
2050 if (0 != (pgc->bm & GET_MESSAGE_BIT_TRANSMIT_TO))
2051 pgc->prime_target.hashPubKey = opt[bits++];
2052 /* note that we can really only check load here since otherwise
2053 peers could find out that we are overloaded by being disconnected
2054 after sending us a malformed query... */
2055 if (GNUNET_YES == test_load_too_high ())
2057 if (NULL != pgc->bf)
2058 GNUNET_CONTAINER_bloomfilter_free (pgc->bf);
2061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2062 "Dropping query from `%s', this peer is too busy.\n",
2063 GNUNET_h2s (other));
2067 net_load_up = 50; // FIXME
2068 net_load_down = 50; // FIXME
2069 pgc->policy = ROUTING_POLICY_NONE;
2070 if ( (net_load_up < IDLE_LOAD_THRESHOLD) &&
2071 (net_load_down < IDLE_LOAD_THRESHOLD) )
2073 pgc->policy |= ROUTING_POLICY_ALL;
2074 pgc->priority = 0; /* no charge */
2078 pgc->priority = bound_priority (ntohl (gm->priority), other);
2080 IDLE_LOAD_THRESHOLD + pgc->priority * pgc->priority) &&
2082 IDLE_LOAD_THRESHOLD + pgc->priority * pgc->priority) )
2084 pgc->policy |= ROUTING_POLICY_ALL;
2088 // FIXME: is this sound?
2089 if (net_load_up < 90 + 10 * pgc->priority)
2090 pgc->policy |= ROUTING_POLICY_FORWARD;
2091 if (net_load_down < 90 + 10 * pgc->priority)
2092 pgc->policy |= ROUTING_POLICY_ANSWER;
2095 if (pgc->policy == ROUTING_POLICY_NONE)
2098 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2099 "Dropping query from `%s', network saturated.\n",
2100 GNUNET_h2s (other));
2102 if (NULL != pgc->bf)
2103 GNUNET_CONTAINER_bloomfilter_free (pgc->bf);
2105 return GNUNET_OK; /* drop */
2107 if ((pgc->policy & ROUTING_POLICY_INDIRECT) != ROUTING_POLICY_INDIRECT)
2108 pgc->priority = 0; /* kill the priority (we cannot benefit) */
2109 pgc->ttl = bound_ttl (ntohl (gm->ttl), pgc->priority);
2110 /* decrement ttl (always) */
2111 ttl_decrement = 2 * TTL_DECREMENT +
2112 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2114 if ( (pgc->ttl < 0) &&
2115 (pgc->ttl - ttl_decrement > 0) )
2118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2119 "Dropping query from `%s' due to TTL underflow.\n",
2120 GNUNET_h2s (other));
2122 /* integer underflow => drop (should be very rare)! */
2123 if (NULL != pgc->bf)
2124 GNUNET_CONTAINER_bloomfilter_free (pgc->bf);
2128 pgc->ttl -= ttl_decrement;
2129 pgc->start_time = GNUNET_TIME_absolute_get ();
2130 preference = (double) pgc->priority;
2131 if (preference < QUERY_BANDWIDTH_VALUE)
2132 preference = QUERY_BANDWIDTH_VALUE;
2133 // FIXME: also reserve bandwidth for reply?
2134 GNUNET_CORE_peer_configure (core,
2136 GNUNET_TIME_UNIT_FOREVER_REL,
2137 0, 0, preference, NULL, NULL);
2138 if (0 != (pgc->policy & ROUTING_POLICY_ANSWER))
2139 pgc->drq = queue_ds_request (BASIC_DATASTORE_REQUEST_DELAY,
2143 GNUNET_SCHEDULER_add_continuation (sched,
2145 &forward_get_request,
2147 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
2153 * Iterator over pending requests.
2155 * @param cls response (struct ProcessReplyClosure)
2156 * @param key our query
2157 * @param value value in the hash map (meta-info about the query)
2158 * @return GNUNET_YES (we should continue to iterate)
2161 process_reply (void *cls,
2162 const GNUNET_HashCode * key,
2165 struct ProcessReplyClosure *prq = cls;
2166 struct PendingRequest *pr = value;
2168 fprintf (stderr, "FIXME %p %p\n", prq, pr);
2169 // FIXME: forward reply to client
2170 // or other peers (depending on pr...)
2176 * Handle P2P "PUT" request.
2178 * @param cls closure, always NULL
2179 * @param peer the other peer involved (sender or receiver, NULL
2180 * for loopback messages where we are both sender and receiver)
2181 * @param message the actual message
2182 * @return GNUNET_OK to keep the connection open,
2183 * GNUNET_SYSERR to close it (signal serious error)
2186 handle_p2p_put (void *cls,
2187 const struct GNUNET_PeerIdentity *other,
2188 const struct GNUNET_MessageHeader *message)
2190 const struct PutMessage *put;
2194 struct GNUNET_TIME_Absolute expiration;
2195 GNUNET_HashCode query;
2196 const struct KBlock *kb;
2197 struct ProcessReplyClosure prq;
2199 msize = ntohs (message->size);
2200 if (msize < sizeof (struct PutMessage))
2203 return GNUNET_SYSERR;
2205 put = (const struct PutMessage*) message;
2206 dsize = msize - sizeof (struct PutMessage);
2207 type = ntohl (put->type);
2208 expiration = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_ntoh (put->expiration));
2210 /* first, validate! */
2213 case GNUNET_DATASTORE_BLOCKTYPE_DBLOCK:
2214 case GNUNET_DATASTORE_BLOCKTYPE_IBLOCK:
2215 GNUNET_CRYPTO_hash (&put[1], dsize, &query);
2217 case GNUNET_DATASTORE_BLOCKTYPE_KBLOCK:
2218 if (dsize < sizeof (struct KBlock))
2220 GNUNET_break_op (0);
2221 return GNUNET_SYSERR;
2223 kb = (const struct KBlock*) &put[1];
2224 // FIXME -- validation code below broken...
2225 if ( (dsize != ntohs (kb->purpose.size) + 42) ||
2227 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_KBLOCK,
2232 GNUNET_break_op (0);
2233 return GNUNET_SYSERR;
2235 GNUNET_CRYPTO_hash (&kb->keyspace,
2236 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2239 case GNUNET_DATASTORE_BLOCKTYPE_SBLOCK:
2240 // FIXME -- validate SBLOCK!
2243 case GNUNET_DATASTORE_BLOCKTYPE_SKBLOCK:
2244 // FIXME -- validate SKBLOCK!
2248 /* unknown block type */
2249 GNUNET_break_op (0);
2250 return GNUNET_SYSERR;
2253 /* now, lookup 'query' */
2254 prq.data = (const void*) &put[1];
2257 prq.expiration = expiration;
2259 GNUNET_CONTAINER_multihashmap_get_multiple (request_map,
2263 // FIXME: if migration is on and load is low,
2264 // queue to store data in datastore;
2265 // use "prq.priority" for that!
2271 * List of handlers for P2P messages
2272 * that we care about.
2274 static struct GNUNET_CORE_MessageHandler p2p_handlers[] =
2277 GNUNET_MESSAGE_TYPE_FS_GET, 0 },
2279 GNUNET_MESSAGE_TYPE_FS_PUT, 0 },
2285 * Task that will try to initiate a connection with the
2292 core_connect_task (void *cls,
2293 const struct GNUNET_SCHEDULER_TaskContext *tc);
2297 * Function called by the core after we've
2301 core_start_cb (void *cls,
2302 struct GNUNET_CORE_Handle * server,
2303 const struct GNUNET_PeerIdentity *
2306 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
2311 GNUNET_SCHEDULER_add_delayed (sched,
2313 GNUNET_SCHEDULER_PRIORITY_HIGH,
2314 GNUNET_SCHEDULER_NO_TASK,
2315 GNUNET_TIME_UNIT_SECONDS,
2325 * Task that will try to initiate a connection with the
2332 core_connect_task (void *cls,
2333 const struct GNUNET_SCHEDULER_TaskContext *tc)
2335 GNUNET_CORE_connect (sched,
2337 GNUNET_TIME_UNIT_FOREVER_REL,
2341 &peer_disconnect_handler,
2350 * Process fs requests.
2352 * @param cls closure
2353 * @param sched scheduler to use
2354 * @param server the initialized server
2355 * @param cfg configuration to use
2359 struct GNUNET_SCHEDULER_Handle *s,
2360 struct GNUNET_SERVER_Handle *server,
2361 const struct GNUNET_CONFIGURATION_Handle *c)
2366 ifm = GNUNET_CONTAINER_multihashmap_create (128);
2367 request_map = GNUNET_CONTAINER_multihashmap_create (128); // FIXME: get size from config
2369 dsh = GNUNET_DATASTORE_connect (cfg,
2373 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2374 _("Failed to connect to datastore service.\n"));
2377 GNUNET_SERVER_disconnect_notify (server,
2378 &handle_client_disconnect,
2380 GNUNET_SERVER_add_handlers (server, handlers);
2381 core_connect_task (NULL, NULL);
2382 GNUNET_SCHEDULER_add_delayed (sched,
2384 GNUNET_SCHEDULER_PRIORITY_IDLE,
2385 GNUNET_SCHEDULER_NO_TASK,
2386 GNUNET_TIME_UNIT_FOREVER_REL,
2393 * The main function for the fs service.
2395 * @param argc number of arguments from the command line
2396 * @param argv command line arguments
2397 * @return 0 ok, 1 on error
2400 main (int argc, char *const *argv)
2402 return (GNUNET_OK ==
2403 GNUNET_SERVICE_run (argc,
2405 "fs", &run, NULL, NULL, NULL)) ? 0 : 1;
2408 /* end of gnunet-service-fs.c */