2 This file is part of GNUnet.
3 (C) 2003, 2004, 2005, 2006, 2007, 2008, 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.
23 * @brief definitions for the entire fs module
24 * @author Igor Wronsky, Christian Grothoff
29 #include "gnunet_datastore_service.h"
30 #include "gnunet_fs_service.h"
33 * Size of the individual blocks used for file-sharing.
35 #define DBLOCK_SIZE (32*1024)
39 * Pick a multiple of 2 here to achive 8-byte alignment!
40 * We also probably want DBlocks to have (roughly) the
41 * same size as IBlocks. With SHA-512, the optimal
42 * value is 32768 byte / 128 byte = 256
43 * (128 byte = 2 * 512 bits). DO NOT CHANGE!
45 #define CHK_PER_INODE 256
49 * Maximum size for a file to be considered for
50 * inlining in a directory.
52 #define MAX_INLINE_SIZE 65536
56 * Blocksize to use when hashing files
57 * for indexing (blocksize for IO, not for
58 * the DBlocks). Larger blocksizes can
59 * be more efficient but will be more disruptive
60 * as far as the scheduler is concerned.
62 #define HASHING_BLOCKSIZE (1024 * 1024)
66 * @brief content hash key
71 GNUNET_HashCode query;
76 * @brief complete information needed
83 * Total size of the file in bytes. (network byte order (!))
88 * Query and key of the top GNUNET_EC_IBlock.
90 struct ContentHashKey chk;
96 * Information about a file and its location
97 * (peer claiming to share the file).
102 * Information about the shared file.
104 struct FileIdentifier fi;
107 * Identity of the peer sharing the file.
109 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded peer;
112 * Time when this location URI expires.
114 struct GNUNET_TIME_Absolute expirationTime;
117 * RSA signature over the GNUNET_EC_FileIdentifier,
118 * GNUNET_hash of the peer and expiration time.
120 struct GNUNET_CRYPTO_RsaSignature contentSignature;
125 { chk, sks, ksk, loc };
128 * A Universal Resource Identifier (URI), opaque.
138 * Keywords start with a '+' if they are
139 * mandatory (in which case the '+' is NOT
140 * part of the keyword) and with a
141 * simple space if they are optional
142 * (in which case the space is ALSO not
143 * part of the actual keyword).
145 * Double-quotes to protect spaces and
146 * %-encoding are NOT used internally
147 * (only in URI-strings).
152 * Size of the keywords array.
154 unsigned int keywordCount;
160 * Hash of the public key for the namespace.
162 GNUNET_HashCode namespace;
165 * Human-readable identifier chosen for this
166 * entry in the namespace.
172 * Information needed to retrieve a file (content-hash-key
175 struct FileIdentifier chk;
178 * Information needed to retrieve a file including signed
179 * location (identity of a peer) of the content.
188 * Information for a file or directory that is
189 * about to be published.
191 struct GNUNET_FS_FileInformation
195 * Files in a directory are kept as a linked list.
197 struct GNUNET_FS_FileInformation *next;
200 * If this is a file in a directory, "dir" refers to
201 * the directory; otherwise NULL.
203 struct GNUNET_FS_FileInformation *dir;
206 * Pointer kept for the client.
211 * Metadata to use for the file.
213 struct GNUNET_CONTAINER_MetaData *meta;
216 * Keywords to use for KBlocks.
218 struct GNUNET_FS_Uri *keywords;
221 * CHK for this file or directory. NULL if
222 * we have not yet computed it.
224 struct GNUNET_FS_Uri *chk_uri;
227 * At what time should the content expire?
229 struct GNUNET_TIME_Absolute expirationTime;
232 * At what time did we start this upload?
234 struct GNUNET_TIME_Absolute start_time;
237 * Under what filename is this struct serialized
238 * (for operational persistence).
243 * Encoder being used to publish this file.
245 struct GNUNET_FS_TreeEncoder *te;
248 * Error message (non-NULL if this operation
254 * Data describing either the file or the directory.
265 * Function that can be used to read the data for the file.
267 GNUNET_FS_DataReader reader;
270 * Closure for reader.
275 * Name of the file (must be an absolute path).
276 * Only required for indexing. FIXME: not yet
282 * If this file is being indexed, this value
283 * is set to the hash over the entire file
284 * (when the indexing process is started).
285 * Otherwise this field is not used.
287 GNUNET_HashCode file_id;
290 * Size of the file (in bytes).
295 * Should the file be indexed or inserted?
302 * Data for a directory.
307 * Name of the directory.
312 * Linked list of entries in the directory.
314 struct GNUNET_FS_FileInformation *entries;
317 * Size of the directory itself (in bytes); 0 if the
318 * size has not yet been calculated.
323 * Pointer to the data for the directory (or NULL if not
333 * Is this struct for a file or directory?
338 * Desired anonymity level.
343 * Desired priority (for keeping the content in the DB).
351 * Master context for most FS operations.
353 struct GNUNET_FS_Handle
358 struct GNUNET_SCHEDULER_Handle *sched;
361 * Configuration to use.
363 const struct GNUNET_CONFIGURATION_Handle *cfg;
366 * Name of our client.
371 * Function to call with updates on our progress.
373 GNUNET_FS_ProgressCallback upcb;
381 * Connection to the FS service.
383 struct GNUNET_CLIENT_Connection *client;
386 * How many downloads probing availability
387 * of search results do we have running
390 unsigned int active_probes;
396 * Handle for controlling an upload.
398 struct GNUNET_FS_PublishContext
401 * Handle to the global fs context.
403 struct GNUNET_FS_Handle *h;
406 * Argument to pass to the client in callbacks.
411 * File-structure that is being shared.
413 struct GNUNET_FS_FileInformation *fi;
416 * Namespace that we are publishing in, NULL if we have no namespace.
418 struct GNUNET_FS_Namespace *namespace;
421 * ID of the content in the namespace, NULL if we have no namespace.
426 * ID for future updates, NULL if we have no namespace or no updates.
431 * Our own client handle for the FS service;
432 * only briefly used when we start to index a
433 * file, otherwise NULL.
435 struct GNUNET_CLIENT_Connection *client;
438 * Current position in the file-tree for the
441 struct GNUNET_FS_FileInformation *fi_pos;
444 * Connection to the datastore service.
446 struct GNUNET_DATASTORE_Handle *dsh;
449 * ID of the task performing the upload. NO_TASK
450 * if the upload has completed.
452 GNUNET_SCHEDULER_TaskIdentifier upload_task;
455 * Typically GNUNET_NO. Set to GNUNET_YES if
456 * "upload_task" is GNUNET_SCHEDULER_NO_TASK
457 * and we're waiting for a response from the
458 * datastore service (in which case this
459 * struct must not be freed until we have that
460 * response). If someone tries to stop the
461 * download for good during this period,
462 * "in_network_wait" is set to GNUNET_SYSERR
463 * which will cause the struct to be destroyed
464 * right after we have the reply (or timeout)
465 * from the datastore service.
470 * Options for publishing.
472 enum GNUNET_FS_PublishOptions options;
475 * Space reservation ID with datastore service
483 * Phases of unindex processing (state machine).
488 * We're currently hashing the file.
490 UNINDEX_STATE_HASHING = 0,
493 * We're notifying the FS service about
496 UNINDEX_STATE_FS_NOTIFY = 1,
499 * We're telling the datastore to delete
500 * the respective entries.
502 UNINDEX_STATE_DS_REMOVE = 2,
507 UNINDEX_STATE_COMPLETE = 3,
510 * We've encountered a fatal error.
512 UNINDEX_STATE_ERROR = 4,
515 * We've been aborted. The next callback should clean up the
518 UNINDEX_STATE_ABORTED = 5
523 * Handle for controlling an unindexing operation.
525 struct GNUNET_FS_UnindexContext
531 struct GNUNET_FS_Handle *h;
534 * Name of the file that we are unindexing.
539 * Connection to the FS service,
540 * only valid during the UNINDEX_STATE_FS_NOTIFY
543 struct GNUNET_CLIENT_Connection *client;
546 * Connection to the datastore service,
547 * only valid during the UNINDEX_STATE_DS_NOTIFY
550 struct GNUNET_DATASTORE_Handle *dsh;
553 * Pointer kept for the client.
558 * Merkle-ish tree encoder context.
560 struct GNUNET_FS_TreeEncoder *tc;
563 * Handle used to read the file.
565 struct GNUNET_DISK_FileHandle *fh;
568 * Overall size of the file.
575 struct GNUNET_TIME_Absolute start_time;
578 * Hash of the file's contents (once
581 GNUNET_HashCode file_id;
584 * Current operatinonal phase.
586 enum UnindexState state;
592 * Information we store for each search result.
598 * URI to which this search result
601 struct GNUNET_FS_Uri *uri;
604 * Metadata for the search result.
606 struct GNUNET_CONTAINER_MetaData *meta;
609 * Client info for this search result.
614 * ID of a job that is currently probing
615 * this results' availability (NULL if we
616 * are not currently probing).
618 struct GNUNET_FS_DownloadContext *probe_ctx;
621 * ID of the task that will clean up the probe_ctx
622 * should it not complete on time (and that will
623 * need to be cancelled if we clean up the search
624 * result before then).
626 GNUNET_SCHEDULER_TaskIdentifier probe_cancel_task;
629 * Number of mandatory keywords for which
630 * we have NOT yet found the search result;
631 * when this value hits zero, the search
632 * result is given to the callback.
634 uint32_t mandatory_missing;
637 * Number of optional keywords under which
638 * this result was also found.
640 uint32_t optional_support;
643 * Number of availability tests that
644 * have succeeded for this result.
646 uint32_t availability_success;
649 * Number of availability trials that we
650 * have performed for this search result.
652 uint32_t availability_trials;
658 * Information we keep for each keyword in
661 struct SearchRequestEntry
664 * Hash of the original keyword, also known as the
665 * key (for decrypting the KBlock).
670 * Hash of the public key, also known as the query.
672 GNUNET_HashCode query;
675 * Map that contains a "struct SearchResult" for each result that
676 * was found under this keyword. Note that the entries will point
677 * to the same locations as those in the master result map (in
678 * "struct GNUNET_FS_SearchContext"), so they should not be freed.
679 * The key for each entry is the XOR of the key and query in the CHK
680 * URI (as a unique identifier for the search result).
682 struct GNUNET_CONTAINER_MultiHashMap *results;
685 * Is this keyword a mandatory keyword
686 * (started with '+')?
694 * Handle for controlling a search.
696 struct GNUNET_FS_SearchContext
699 * Handle to the global FS context.
701 struct GNUNET_FS_Handle *h;
704 * List of keywords that we're looking for.
706 struct GNUNET_FS_Uri *uri;
709 * For update-searches, link to the
710 * base-SKS search that triggered the
711 * update search; otherwise NULL.
713 struct GNUNET_FS_SearchContext *parent;
716 * Connection to the FS service.
718 struct GNUNET_CLIENT_Connection *client;
721 * Pointer we keep for the client.
726 * Map that contains a "struct SearchResult" for each result that
727 * was found in the search. The key for each entry is the XOR of
728 * the key and query in the CHK URI (as a unique identifier for the
731 struct GNUNET_CONTAINER_MultiHashMap *master_result_map;
734 * Per-keyword information for a keyword search.
735 * This array will have exactly as many entries
736 * as there were keywords.
738 struct SearchRequestEntry *requests;
743 struct GNUNET_TIME_Absolute start_time;
746 * ID of a task that is using this struct
747 * and that must be cancelled when the search
748 * is being stopped (if not GNUNET_SCHEDULER_NO_TASK).
749 * Used for the task that adds some artificial
750 * delay when trying to reconnect to the FS
753 GNUNET_SCHEDULER_TaskIdentifier task;
756 * Anonymity level for the search.
761 * Number of mandatory keywords in this query.
763 uint32_t mandatory_count;
768 * Information about an active download request.
770 struct DownloadRequest
773 * While pending, we keep all download requests
776 struct DownloadRequest *next;
779 * CHK for the request.
781 struct ContentHashKey chk;
784 * Offset of the corresponding block.
789 * Depth of the corresponding block in the tree.
794 * Set if this request is currently in the linked list of pending
795 * requests. Needed in case we get a response for a request that we
796 * have not yet send (due to FS bug or two blocks with identical
797 * content); in this case, we would need to remove the block from
798 * the pending list (and need a fast way to check if the block is on
807 * Context for controlling a download.
809 struct GNUNET_FS_DownloadContext
815 struct GNUNET_FS_Handle *h;
818 * Connection to the FS service.
820 struct GNUNET_CLIENT_Connection *client;
823 * Parent download (used when downloading files
826 struct GNUNET_FS_DownloadContext *parent;
829 * Context kept for the client.
834 * URI that identifies the file that
835 * we are downloading.
837 struct GNUNET_FS_Uri *uri;
840 * Where are we writing the data (name of the
841 * file, can be NULL!).
846 * Map of active requests (those waiting
847 * for a response). The key is the hash
848 * of the encryped block (aka query).
850 struct GNUNET_CONTAINER_MultiHashMap *active;
853 * Linked list of pending requests.
855 struct DownloadRequest *pending;
858 * The file handle, NULL if we don't create
861 struct GNUNET_DISK_FileHandle *handle;
864 * Identity of the peer having the content, or all-zeros
865 * if we don't know of such a peer.
867 struct GNUNET_PeerIdentity target;
870 * ID of a task that is using this struct
871 * and that must be cancelled when the download
872 * is being stopped (if not GNUNET_SCHEDULER_NO_TASK).
873 * Used for the task that adds some artificial
874 * delay when trying to reconnect to the FS
877 GNUNET_SCHEDULER_TaskIdentifier task;
880 * What was the size of the file on disk that we're downloading
881 * before we started? Used to detect if there is a point in
882 * checking an existing block on disk for matching the desired
883 * content. 0 if the file did not exist already.
885 uint64_t old_file_size;
888 * What is the first offset that we're interested
894 * How many bytes starting from offset are desired?
895 * This is NOT the overall length of the file!
900 * How many bytes have we already received within
901 * the specified range (DBlocks only).
906 * Time download was started.
908 struct GNUNET_TIME_Absolute start_time;
911 * Desired level of anonymity.
916 * The depth of the file-tree.
918 unsigned int treedepth;
921 * Options for the download.
923 enum GNUNET_FS_DownloadOptions options;
927 struct GNUNET_FS_Namespace
931 * Private key for the namespace.
933 struct GNUNET_CRYPTO_RsaPrivateKey *key;
943 * @brief index block (indexing a DBlock that
944 * can be obtained directly from reading
945 * the plaintext file)
950 * Hash code of the entire content of the
951 * file that was indexed (used to uniquely
952 * identify the plaintext file).
954 GNUNET_HashCode file_id;
957 * At which offset should we be able to find
958 * this on-demand encoded block?
960 uint64_t offset GNUNET_PACKED;
966 * @brief keyword block (advertising data under a keyword)
972 * GNUNET_RSA_Signature using RSA-key generated from search keyword.
974 struct GNUNET_CRYPTO_RsaSignature signature;
977 * What is being signed and why?
979 struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
982 * Key generated (!) from the H(keyword) as the seed!
984 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded keyspace;
986 /* 0-terminated URI here */
988 /* variable-size Meta-Data follows here */
993 * @brief namespace content block (advertising data under an identifier in a namespace)
999 * GNUNET_RSA_Signature using RSA-key of the namespace
1001 struct GNUNET_CRYPTO_RsaSignature signature;
1004 * What is being signed and why?
1006 struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
1009 * Hash of the hash of the human-readable identifier used for
1010 * this entry (the hash of the human-readable identifier is
1011 * used as the key for decryption; the xor of this identifier
1012 * and the hash of the "keyspace" is the datastore-query hash).
1014 GNUNET_HashCode identifier;
1017 * Public key of the namespace.
1019 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded subspace;
1021 /* 0-terminated update-identifier here */
1023 /* 0-terminated URI here */
1025 /* variable-size Meta-Data follows here */
1031 * Message sent from a GNUnet (fs) publishing
1032 * activity to the gnunet-fs-service to
1033 * initiate indexing of a file. The service
1034 * is supposed to check if the specified file
1035 * is available and has the same cryptographic
1036 * hash. It should then respond with either
1037 * a confirmation or a denial.
1039 * On OSes where this works, it is considered
1040 * acceptable if the service only checks that
1041 * the path, device and inode match (it can
1042 * then be assumed that the hash will also match
1043 * without actually computing it; this is an
1044 * optimization that should be safe given that
1045 * the client is not our adversary).
1047 struct IndexStartMessage
1051 * Message type will be
1052 * GNUNET_MESSAGE_TYPE_FS_INDEX_START.
1054 struct GNUNET_MessageHeader header;
1057 * ID of device containing the file, as seen by the client. This
1058 * device ID is obtained using a call like "statvfs" (and converting
1059 * the "f_fsid" field to a 32-bit big-endian number). Use 0 if the
1060 * OS does not support this, in which case the service must do a
1061 * full hash recomputation.
1063 uint32_t device GNUNET_PACKED;
1066 * Inode of the file on the given device, as seen by the client
1067 * ("st_ino" field from "struct stat"). Use 0 if the OS does not
1068 * support this, in which case the service must do a full hash
1071 uint64_t inode GNUNET_PACKED;
1074 * Hash of the file that we would like to index.
1076 GNUNET_HashCode file_id;
1078 /* this is followed by a 0-terminated
1079 filename of a file with the hash
1080 "file_id" as seen by the client */
1086 * Message send by FS service in response to a request
1087 * asking for a list of all indexed files.
1089 struct IndexInfoMessage
1092 * Message type will be
1093 * GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_ENTRY.
1095 struct GNUNET_MessageHeader header;
1100 uint32_t reserved GNUNET_PACKED;
1103 * Hash of the indexed file.
1105 GNUNET_HashCode file_id;
1107 /* this is followed by a 0-terminated
1108 filename of a file with the hash
1109 "file_id" as seen by the client */
1115 * Message sent from a GNUnet (fs) unindexing
1116 * activity to the gnunet-fs-service to
1117 * indicate that a file will be unindexed. The service
1118 * is supposed to remove the file from the
1119 * list of indexed files and response with
1120 * a confirmation message (even if the file
1121 * was already not on the list).
1123 struct UnindexMessage
1127 * Message type will be
1128 * GNUNET_MESSAGE_TYPE_FS_UNINDEX.
1130 struct GNUNET_MessageHeader header;
1135 uint32_t reserved GNUNET_PACKED;
1138 * Hash of the file that we will unindex.
1140 GNUNET_HashCode file_id;
1146 * Message sent from a GNUnet (fs) search
1147 * activity to the gnunet-fs-service to
1150 struct SearchMessage
1154 * Message type will be
1155 * GNUNET_MESSAGE_TYPE_FS_START_SEARCH.
1157 struct GNUNET_MessageHeader header;
1162 int32_t reserved GNUNET_PACKED;
1165 * Type of the content that we're looking for.
1168 uint32_t type GNUNET_PACKED;
1171 * Desired anonymity level, big-endian.
1173 uint32_t anonymity_level GNUNET_PACKED;
1176 * If the request is for a DBLOCK or IBLOCK, this is the identity of
1177 * the peer that is known to have a response. Set to all-zeros if
1178 * such a target is not known (note that even if OUR anonymity
1179 * level is >0 we may happen to know the responder's identity;
1180 * nevertheless, we should probably not use it for a DHT-lookup
1181 * or similar blunt actions in order to avoid exposing ourselves).
1183 * If the request is for an SBLOCK, this is the identity of the
1184 * pseudonym to which the SBLOCK belongs.
1186 * If the request is for a KBLOCK, "target" must be all zeros.
1188 GNUNET_HashCode target;
1191 * Hash of the keyword (aka query) for KBLOCKs; Hash of
1192 * the CHK-encoded block for DBLOCKS and IBLOCKS (aka query)
1193 * and hash of the identifier XORed with the target for
1194 * SBLOCKS (aka query).
1196 GNUNET_HashCode query;
1202 * Response from FS service with a result for
1203 * a previous FS search. Note that queries
1204 * for DBLOCKS and IBLOCKS that have received
1205 * a single response are considered done.
1207 struct ContentMessage
1211 * Message type will be
1212 * GNUNET_MESSAGE_TYPE_FS_CONTENT.
1214 struct GNUNET_MessageHeader header;
1217 * Type of the content that was found,
1218 * should never be 0.
1220 uint32_t type GNUNET_PACKED;
1223 * When will this result expire?
1225 struct GNUNET_TIME_AbsoluteNBO expiration;
1227 /* followed by the actual block of data */