(no commit message)
[oweals/gnunet.git] / src / fs / fs.h
1
2 /*
3      This file is part of GNUnet.
4      (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 Christian Grothoff (and other contributing authors)
5
6      GNUnet is free software; you can redistribute it and/or modify
7      it under the terms of the GNU General Public License as published
8      by the Free Software Foundation; either version 3, or (at your
9      option) any later version.
10
11      GNUnet is distributed in the hope that it will be useful, but
12      WITHOUT ANY WARRANTY; without even the implied warranty of
13      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14      General Public License for more details.
15
16      You should have received a copy of the GNU General Public License
17      along with GNUnet; see the file COPYING.  If not, write to the
18      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19      Boston, MA 02111-1307, USA.
20 */
21
22 /**
23  * @file fs/fs.h
24  * @brief definitions for the entire fs module
25  * @author Igor Wronsky, Christian Grothoff
26  */
27 #ifndef FS_H
28 #define FS_H
29
30 #include "gnunet_constants.h"
31 #include "gnunet_datastore_service.h"
32 #include "gnunet_dht_service.h"
33 #include "gnunet_fs_service.h"
34 #include "gnunet_block_lib.h"
35 #include "block_fs.h"
36
37
38 /**
39  * Maximum number of outgoing messages we queue per peer.
40  *
41  * Performance measurements for 2 peer setup for 50 MB file
42  * (with MAX_DATASTORE_QUEUE = 1 and RETRY_PROBABILITY_INV = 1):
43  *
44  *   2: 1700 kb/s, 1372 kb/s
45  *   8: 2117 kb/s, 1284 kb/s, 1112 kb/s
46  *  16: 3500 kb/s, 3200 kb/s, 3388 kb/s
47  *  32: 3441 kb/s, 3163 kb/s, 3277 kb/s
48  * 128: 1700 kb/s; 2010 kb/s, 3383 kb/s, 1156 kb/s
49  *
50  * Conclusion: 16 seems to be a pretty good value (stable
51  * and high performance, no excessive memory use).
52  */
53 #define MAX_QUEUE_PER_PEER 16
54
55 /**
56  * Maximum size of the datastore queue for P2P operations.
57  * Needs to be large enough to queue MAX_QUEUE_PER_PEER
58  * operations for roughly the number of active (connected)
59  * peers.
60  */
61 #define MAX_DATASTORE_QUEUE (16 * MAX_QUEUE_PER_PEER)
62
63 /**
64  * Maximum number of blocks we keep in memory for migration.
65  */
66 #define MAX_MIGRATION_QUEUE 32
67
68 /**
69  * How many peers do we select as possible
70  * targets per block obtained for migration?
71  */
72 #define MIGRATION_LIST_SIZE 4
73
74 /**
75  * To how many peers do we forward each migration block ultimately?
76  * This number must be smaller or equal to MIGRATION_LIST_SIZE.  Using
77  * a smaller value allows for variation in available bandwidth (for
78  * migration) between the peers.
79  */
80 #define MIGRATION_TARGET_COUNT 2
81
82 /**
83  * Ratio for moving average delay calculation.  The previous
84  * average goes in with a factor of (n-1) into the calculation.
85  * Must be > 0.
86  */
87 #define RUNAVG_DELAY_N 16
88
89 /**
90  * Size of the individual blocks used for file-sharing.
91  */
92 #define DBLOCK_SIZE (32*1024)
93
94 /**
95  * Maximum legal size for a kblock.
96  */
97 #define MAX_KBLOCK_SIZE (60 * 1024)
98
99 /**
100  * Maximum legal size for an sblock.
101  */
102 #define MAX_SBLOCK_SIZE (60 * 1024)
103
104 /**
105  * Maximum legal size for an nblock.
106  */
107 #define MAX_NBLOCK_SIZE (60 * 1024)
108
109 /**
110  * Pick a multiple of 2 here to achive 8-byte alignment!
111  * We also probably want DBlocks to have (roughly) the
112  * same size as IBlocks.  With SHA-512, the optimal
113  * value is 32768 byte / 128 byte = 256
114  * (128 byte = 2 * 512 bits).  DO NOT CHANGE!
115  */
116 #define CHK_PER_INODE 256
117
118
119 /**
120  * Maximum size for a file to be considered for
121  * inlining in a directory.
122  */
123 #define MAX_INLINE_SIZE 65536
124
125
126 /**
127  * Blocksize to use when hashing files
128  * for indexing (blocksize for IO, not for
129  * the DBlocks).  Larger blocksizes can
130  * be more efficient but will be more disruptive
131  * as far as the scheduler is concerned.
132  */
133 #define HASHING_BLOCKSIZE (1024 * 128)
134
135 /**
136  * Number of bits we set per entry in the bloomfilter.
137  * Do not change!
138  */
139 #define BLOOMFILTER_K GNUNET_DHT_GET_BLOOMFILTER_K
140
141 /**
142  * Number of availability trials we perform per search result.
143  */
144 #define AVAILABILITY_TRIALS_MAX 8
145
146 /**
147  * Length of the P2P success tracker.  Note that
148  * having a very long list can also hurt performance.
149  */
150 #define P2P_SUCCESS_LIST_SIZE 8
151
152
153 /**
154  * Length of the CS-2-P success tracker.  Note that
155  * having a very long list can also hurt performance.
156  */
157 #define CS2P_SUCCESS_LIST_SIZE 8
158
159 /**
160  * How long are we willing to wait for the datastore to be ready to
161  * process a request for a query without priority?
162  */
163 #define BASIC_DATASTORE_REQUEST_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
164
165
166 /**
167  * How long are we willing to wait for the core to be ready to
168  * transmit a reply to the target peer (if we can not transmit
169  * until then, we will discard the reply).
170  */
171 #define ACCEPTABLE_REPLY_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
172
173
174 /**
175  * Bandwidth value of an (effectively) 0-priority query.
176  */
177 #define QUERY_BANDWIDTH_VALUE 1
178
179 /**
180  * Bandwidth value of a 0-priority content (must be
181  * fairly high compared to query since content is
182  * typically significantly larger -- and more valueable
183  * since it can take many queries to get one piece of
184  * content).
185  */
186 #define CONTENT_BANDWIDTH_VALUE 800
187
188 /**
189  * By which amount do we decrement the TTL for simple forwarding /
190  * indirection of the query; in milli-seconds.  Set somewhat in
191  * accordance to your network latency (above the time it'll take you
192  * to send a packet and get a reply).
193  */
194 #define TTL_DECREMENT 5000
195
196 /**
197  * Until which load do we consider the peer idle and do not
198  * charge at all? (should be larger than GNUNET_IDLE_LOAD_THRESHOLD used
199  * by the rest of the code)!
200  */
201 #define IDLE_LOAD_THRESHOLD ((100 + GNUNET_CONSTANTS_IDLE_LOAD_THRESHOLD) / 2)
202
203 /**
204  * Name of the directory with top-level searches.
205  */
206 #define GNUNET_FS_SYNC_PATH_MASTER_SEARCH "search"
207
208 /**
209  * Name of the directory with sub-searches (namespace-updates).
210  */
211 #define GNUNET_FS_SYNC_PATH_CHILD_SEARCH "search-child"
212
213 /**
214  * Name of the directory with master downloads (not associated
215  * with search or part of another download).
216  */
217 #define GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD "download"
218
219 /**
220  * Name of the directory with downloads that are part of another
221  * download or a search.
222  */
223 #define GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD "download-child"
224
225 /**
226  * Name of the directory with publishing operations.
227  */
228 #define GNUNET_FS_SYNC_PATH_MASTER_PUBLISH "publish"
229
230 /**
231  * Name of the directory with files that are being published
232  */
233 #define GNUNET_FS_SYNC_PATH_FILE_INFO "publish-file"
234
235 /**
236  * Name of the directory with unindex operations.
237  */
238 #define GNUNET_FS_SYNC_PATH_MASTER_UNINDEX "unindex"
239
240
241 /**
242  * @brief content hash key
243  */
244 struct ContentHashKey 
245 {
246   /**
247    * Hash of the original content, used for encryption.
248    */
249   GNUNET_HashCode key;
250
251   /**
252    * Hash of the encrypted content, used for querying.
253    */
254   GNUNET_HashCode query;
255 };
256
257
258 /**
259  * @brief complete information needed
260  * to download a file.
261  */
262 struct FileIdentifier
263 {
264
265   /**
266    * Total size of the file in bytes. (network byte order (!))
267    */
268   uint64_t file_length;
269
270   /**
271    * Query and key of the top GNUNET_EC_IBlock.
272    */
273   struct ContentHashKey chk;
274
275 };
276
277
278 /**
279  * Information about a file and its location
280  * (peer claiming to share the file).
281  */
282 struct Location
283 {
284   /**
285    * Information about the shared file.
286    */
287   struct FileIdentifier fi;
288
289   /**
290    * Identity of the peer sharing the file.
291    */
292   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded peer;
293
294   /**
295    * Time when this location URI expires.
296    */
297   struct GNUNET_TIME_Absolute expirationTime;
298
299   /**
300    * RSA signature over the GNUNET_EC_FileIdentifier,
301    * GNUNET_hash of the peer and expiration time.
302    */
303   struct GNUNET_CRYPTO_RsaSignature contentSignature;
304
305 };
306
307 /**
308  * Types of URIs.
309  */
310 enum uri_types
311   { 
312     /**
313      * Content-hash-key (simple file).
314      */
315     chk, 
316
317     /**
318      * Signed key space (file in namespace).
319      */
320     sks,
321
322     /**
323      * Keyword search key (query with keywords).
324      */
325     ksk,
326
327     /**
328      * Location (chk with identity of hosting peer).
329      */
330     loc 
331   };
332
333 /**
334  * A Universal Resource Identifier (URI), opaque.
335  */
336 struct GNUNET_FS_Uri
337 {
338   /**
339    * Type of the URI.
340    */
341   enum uri_types type;
342
343   union
344   {
345     struct
346     {
347       /**
348        * Keywords start with a '+' if they are
349        * mandatory (in which case the '+' is NOT
350        * part of the keyword) and with a
351        * simple space if they are optional
352        * (in which case the space is ALSO not
353        * part of the actual keyword).
354        *
355        * Double-quotes to protect spaces and
356        * %-encoding are NOT used internally
357        * (only in URI-strings).
358        */
359       char **keywords;
360       
361       /**
362        * Size of the keywords array.
363        */
364       unsigned int keywordCount;
365     } ksk;
366
367     struct
368     {
369       /**
370        * Hash of the public key for the namespace.
371        */
372       GNUNET_HashCode namespace;
373
374       /**
375        * Human-readable identifier chosen for this
376        * entry in the namespace.
377        */
378       char *identifier;
379     } sks;
380
381     /**
382      * Information needed to retrieve a file (content-hash-key
383      * plus file size).
384      */
385     struct FileIdentifier chk;
386
387     /**
388      * Information needed to retrieve a file including signed
389      * location (identity of a peer) of the content.
390      */
391     struct Location loc;
392   } data;
393
394 };
395
396
397 /**
398  * Information for a file or directory that is
399  * about to be published.
400  */
401 struct GNUNET_FS_FileInformation
402 {
403
404   /**
405    * Files in a directory are kept as a linked list.
406    */
407   struct GNUNET_FS_FileInformation *next;
408
409   /**
410    * If this is a file in a directory, "dir" refers to
411    * the directory; otherwise NULL.
412    */
413   struct GNUNET_FS_FileInformation *dir;
414
415   /**
416    * Handle to the master context.
417    */
418   struct GNUNET_FS_Handle *h;
419
420   /**
421    * Pointer kept for the client.
422    */
423   void *client_info;
424
425   /**
426    * Metadata to use for the file.
427    */
428   struct GNUNET_CONTAINER_MetaData *meta;
429
430   /**
431    * Keywords to use for KBlocks.
432    */
433   struct GNUNET_FS_Uri *keywords;
434
435   /**
436    * CHK for this file or directory. NULL if
437    * we have not yet computed it.
438    */
439   struct GNUNET_FS_Uri *chk_uri;
440
441   /**
442    * At what time should the content expire?
443    */
444   struct GNUNET_TIME_Absolute expirationTime;
445
446   /**
447    * At what time did we start this upload?
448    */
449   struct GNUNET_TIME_Absolute start_time;
450
451   /**
452    * Under what filename is this struct serialized
453    * (for operational persistence).  Should be determined
454    * using 'mktemp'.
455    */
456   char *serialization;
457   
458   /**
459    * Encoder being used to publish this file.
460    */
461   struct GNUNET_FS_TreeEncoder *te;
462
463   /**
464    * Error message (non-NULL if this operation failed).
465    */
466   char *emsg;
467
468   /**
469    * Name of the file or directory (must be an absolute path). 
470    */
471   char *filename;
472
473   /**
474    * Data describing either the file or the directory.
475    */
476   union
477   {
478
479     /**
480      * Data for a file.
481      */
482     struct {
483
484       /**
485        * Function that can be used to read the data for the file.
486        */
487       GNUNET_FS_DataReader reader;
488
489       /**
490        * Closure for reader.
491        */
492       void *reader_cls;
493
494       /**
495        * If this file is being indexed, this value is set to the hash
496        * over the entire file (when the indexing process is started).
497        * Otherwise this field is not used.
498        */
499       GNUNET_HashCode file_id;
500
501       /**
502        * Size of the file (in bytes).
503        */
504       uint64_t file_size;
505
506       /**
507        * Should the file be indexed or inserted?
508        */
509       int do_index;
510
511       /**
512        * Is "file_id" already valid?  Set to GNUNET_YES once the hash
513        * has been calculated.
514        */
515       int have_hash;
516
517       /**
518        * Has the service confirmed our INDEX_START request?
519        * GNUNET_YES if this step has been completed.
520        */
521       int index_start_confirmed;
522
523     } file;
524
525     /**
526      * Data for a directory.
527      */
528     struct {
529       
530       /**
531        * Linked list of entries in the directory.
532        */
533       struct GNUNET_FS_FileInformation *entries;
534
535       /**
536        * Size of the directory itself (in bytes); 0 if the
537        * size has not yet been calculated.
538        */
539       size_t dir_size;
540
541       /**
542        * Pointer to the data for the directory (or NULL if not
543        * available).
544        */
545       void *dir_data;
546
547     } dir;
548
549   } data;
550
551   /**
552    * Desired anonymity level.
553    */
554   uint32_t anonymity;
555
556   /**
557    * Desired priority (for keeping the content in the DB).
558    */
559   uint32_t priority;
560
561   /**
562    * Is this struct for a file or directory?
563    */
564   int is_directory;
565
566   /**
567    * Are we done publishing this file?
568    */
569   int is_published;
570
571 };
572
573
574 /**
575  * The job is now ready to run and should use the given client
576  * handle to communicate with the FS service.
577  *
578  * @param cls closure
579  * @param client handle to use for FS communication
580  */
581 typedef void (*GNUNET_FS_QueueStart)(void *cls,
582                                      struct GNUNET_CLIENT_Connection *client);
583
584
585 /**
586  * The job must now stop to run and should destry the client handle as
587  * soon as possible (ideally prior to returning).
588  */
589 typedef void (*GNUNET_FS_QueueStop)(void *cls);
590
591
592 /**
593  * Entry in the job queue.
594  */
595 struct GNUNET_FS_QueueEntry
596 {
597   /**
598    * This is a linked list.
599    */
600   struct GNUNET_FS_QueueEntry *next;
601
602   /**
603    * This is a linked list.
604    */
605   struct GNUNET_FS_QueueEntry *prev;
606
607   /**
608    * Function to call when the job is started.
609    */
610   GNUNET_FS_QueueStart start;
611
612   /**
613    * Function to call when the job needs to stop (or is done / dequeued).
614    */
615   GNUNET_FS_QueueStop stop;
616
617   /**
618    * Closure for start and stop.
619    */
620   void *cls;
621
622   /**
623    * Handle to FS primary context.
624    */ 
625   struct GNUNET_FS_Handle *h;
626
627   /**
628    * Client handle, or NULL if job is not running.
629    */
630   struct GNUNET_CLIENT_Connection *client;
631
632   /**
633    * Time the job was originally queued.
634    */
635   struct GNUNET_TIME_Absolute queue_time;
636
637   /**
638    * Time the job was started last.
639    */
640   struct GNUNET_TIME_Absolute start_time;
641
642   /**
643    * Total amount of time the job has been running (except for the
644    * current run).
645    */
646   struct GNUNET_TIME_Relative run_time;
647
648   /**
649    * How many blocks do the active downloads have?
650    */
651   unsigned int blocks;
652
653   /**
654    * How often have we (re)started this download?
655    */
656   unsigned int start_times;
657
658 };
659
660
661
662
663 /**
664  * Information we store for each search result.
665  */
666 struct GNUNET_FS_SearchResult
667 {
668
669   /**
670    * Search context this result belongs to.
671    */
672   struct GNUNET_FS_SearchContext *sc;
673
674   /**
675    * URI to which this search result refers to.
676    */
677   struct GNUNET_FS_Uri *uri;
678
679   /**
680    * Metadata for the search result.
681    */
682   struct GNUNET_CONTAINER_MetaData *meta;
683
684   /**
685    * Client info for this search result.
686    */
687   void *client_info;
688
689   /**
690    * ID of a job that is currently probing this results' availability
691    * (NULL if we are not currently probing).
692    */
693   struct GNUNET_FS_DownloadContext *probe_ctx;
694
695   /**
696    * ID of an associated download based on this search result (or
697    * NULL for none).
698    */
699   struct GNUNET_FS_DownloadContext *download;
700
701   /**
702    * If this search result triggered an update search, this field
703    * links to the update search.
704    */
705   struct GNUNET_FS_SearchContext *update_search;
706
707   /**
708    * Name under which this search result is stored on disk.
709    */
710   char *serialization;
711
712   /**
713    * Key for the search result
714    */
715   GNUNET_HashCode key;
716
717   /**
718    * ID of the task that will clean up the probe_ctx should it not
719    * complete on time (and that will need to be cancelled if we clean
720    * up the search result before then).
721    */
722   GNUNET_SCHEDULER_TaskIdentifier probe_cancel_task;
723
724   /**
725    * When did the current probe become active?
726    */
727   struct GNUNET_TIME_Absolute probe_active_time;
728
729   /**
730    * How much longer should we run the current probe before giving up?
731    */
732   struct GNUNET_TIME_Relative remaining_probe_time;
733
734   /**
735    * Number of mandatory keywords for which we have NOT yet found the
736    * search result; when this value hits zero, the search result is
737    * given to the callback.
738    */
739   uint32_t mandatory_missing;
740
741   /**
742    * Number of optional keywords under which this result was also
743    * found.
744    */
745   uint32_t optional_support;
746
747   /**
748    * Number of availability tests that have succeeded for this result.
749    */
750   uint32_t availability_success;
751
752   /**
753    * Number of availability trials that we have performed for this
754    * search result.
755    */
756   uint32_t availability_trials;
757
758 };
759
760
761 /**
762  * Add a job to the queue.
763  *
764  * @param h handle to the overall FS state
765  * @param start function to call to begin the job
766  * @param stop function to call to pause the job, or on dequeue (if the job was running)
767  * @param cls closure for start and stop
768  * @param blocks number of blocks this download has
769  * @return queue handle
770  */
771 struct GNUNET_FS_QueueEntry *
772 GNUNET_FS_queue_ (struct GNUNET_FS_Handle *h,
773                   GNUNET_FS_QueueStart start,
774                   GNUNET_FS_QueueStop stop,
775                   void *cls,
776                   unsigned int blocks);
777
778
779 /**
780  * Dequeue a job from the queue.
781  * @param qh handle for the job
782  */
783 void
784 GNUNET_FS_dequeue_ (struct GNUNET_FS_QueueEntry *qh);
785
786
787 /**
788  * Function that provides data by reading from a file.
789  *
790  * @param cls closure (points to the file information)
791  * @param offset offset to read from; it is possible
792  *            that the caller might need to go backwards
793  *            a bit at times
794  * @param max maximum number of bytes that should be 
795  *            copied to buf; readers are not allowed
796  *            to provide less data unless there is an error;
797  *            a value of "0" will be used at the end to allow
798  *            the reader to clean up its internal state
799  * @param buf where the reader should write the data
800  * @param emsg location for the reader to store an error message
801  * @return number of bytes written, usually "max", 0 on error
802  */
803 size_t
804 GNUNET_FS_data_reader_file_(void *cls, 
805                             uint64_t offset,
806                             size_t max, 
807                             void *buf,
808                             char **emsg);
809
810
811 /**
812  * Create the closure for the 'GNUNET_FS_data_reader_file_' callback.
813  *
814  * @param filename file to read
815  * @return closure to use
816  */
817 void *
818 GNUNET_FS_make_file_reader_context_ (const char *filename);
819
820
821
822 /**
823  * Function that provides data by copying from a buffer.
824  *
825  * @param cls closure (points to the buffer)
826  * @param offset offset to read from; it is possible
827  *            that the caller might need to go backwards
828  *            a bit at times
829  * @param max maximum number of bytes that should be 
830  *            copied to buf; readers are not allowed
831  *            to provide less data unless there is an error;
832  *            a value of "0" will be used at the end to allow
833  *            the reader to clean up its internal state
834  * @param buf where the reader should write the data
835  * @param emsg location for the reader to store an error message
836  * @return number of bytes written, usually "max", 0 on error
837  */
838 size_t
839 GNUNET_FS_data_reader_copy_(void *cls, 
840                             uint64_t offset,
841                             size_t max, 
842                             void *buf,
843                             char **emsg);
844
845 /**
846  * Notification of FS that a search probe has made progress.
847  * This function is used INSTEAD of the client's event handler
848  * for downloads where the GNUNET_FS_DOWNLOAD_IS_PROBE flag is set.
849  *
850  * @param cls closure, always NULL (!), actual closure
851  *        is in the client-context of the info struct
852  * @param info details about the event, specifying the event type
853  *        and various bits about the event
854  * @return client-context (for the next progress call
855  *         for this operation; should be set to NULL for
856  *         SUSPEND and STOPPED events).  The value returned
857  *         will be passed to future callbacks in the respective
858  *         field in the GNUNET_FS_ProgressInfo struct.
859  */
860 void*
861 GNUNET_FS_search_probe_progress_ (void *cls,
862                                   const struct GNUNET_FS_ProgressInfo *info);
863
864
865 /**
866  * Main function that performs the upload.
867  *
868  * @param cls "struct GNUNET_FS_PublishContext" identifies the upload
869  * @param tc task context
870  */
871 void
872 GNUNET_FS_publish_main_ (void *cls,
873                          const struct GNUNET_SCHEDULER_TaskContext *tc);
874
875
876 /**
877  * Function called once the hash of the file
878  * that is being unindexed has been computed.
879  *
880  * @param cls closure, unindex context
881  * @param file_id computed hash, NULL on error
882  */
883 void 
884 GNUNET_FS_unindex_process_hash_ (void *cls,
885                                  const GNUNET_HashCode *file_id);
886
887
888 /**
889  * Fill in all of the generic fields for a publish event and call the
890  * callback.
891  *
892  * @param pi structure to fill in
893  * @param sc overall publishing context
894  * @param p file information for the file being published
895  * @param offset where in the file are we so far
896  * @return value returned from callback
897  */
898 void *
899 GNUNET_FS_publish_make_status_ (struct GNUNET_FS_ProgressInfo *pi,
900                                 struct GNUNET_FS_PublishContext *sc,
901                                 const struct GNUNET_FS_FileInformation *p,
902                                 uint64_t offset);
903
904
905 /**
906  * Fill in all of the generic fields for a download event and call the
907  * callback.
908  *
909  * @param pi structure to fill in
910  * @param dc overall download context
911  */
912 void
913 GNUNET_FS_download_make_status_ (struct GNUNET_FS_ProgressInfo *pi,
914                                  struct GNUNET_FS_DownloadContext *dc);
915
916
917 /**
918  * Fill in all of the generic fields for 
919  * an unindex event and call the callback.
920  *
921  * @param pi structure to fill in
922  * @param uc overall unindex context
923  * @param offset where we are in the file (for progress)
924  */
925 void
926 GNUNET_FS_unindex_make_status_ (struct GNUNET_FS_ProgressInfo *pi,
927                                 struct GNUNET_FS_UnindexContext *uc,
928                                 uint64_t offset);
929
930 /**
931  * Fill in all of the generic fields for a search event and
932  * call the callback.
933  *
934  * @param pi structure to fill in
935  * @param sc overall search context
936  * @return value returned by the callback
937  */
938 void *
939 GNUNET_FS_search_make_status_ (struct GNUNET_FS_ProgressInfo *pi,
940                                struct GNUNET_FS_SearchContext *sc);
941
942
943 /**
944  * Connect to the datastore and remove the blocks.
945  *
946  * @param uc context for the unindex operation.
947  */
948 void 
949 GNUNET_FS_unindex_do_remove_ (struct GNUNET_FS_UnindexContext *uc);
950
951 /**
952  * Build the request and actually initiate the search using the
953  * GNUnet FS service.
954  *
955  * @param sc search context
956  * @return GNUNET_OK on success, GNUNET_SYSERR on error
957  */
958 int
959 GNUNET_FS_search_start_searching_ (struct GNUNET_FS_SearchContext *sc);
960
961 /**
962  * Start the downloading process (by entering the queue).
963  *
964  * @param dc our download context
965  */
966 void
967 GNUNET_FS_download_start_downloading_ (struct GNUNET_FS_DownloadContext *dc);
968
969
970 /**
971  * Start download probes for the given search result.
972  *
973  * @param sr the search result
974  */
975 void
976 GNUNET_FS_search_start_probe_ (struct GNUNET_FS_SearchResult *sr);
977
978 /**
979  * Remove serialization/deserialization file from disk.
980  *
981  * @param h master context
982  * @param ext component of the path 
983  * @param ent entity identifier 
984  */
985 void
986 GNUNET_FS_remove_sync_file_ (struct GNUNET_FS_Handle *h,
987                              const char *ext,
988                              const char *ent);
989
990
991 /**
992  * Remove serialization/deserialization directory from disk.
993  *
994  * @param h master context
995  * @param ext component of the path 
996  * @param uni unique name of parent 
997  */
998 void
999 GNUNET_FS_remove_sync_dir_ (struct GNUNET_FS_Handle *h,
1000                             const char *ext,
1001                             const char *uni);
1002
1003
1004 /**
1005  * Synchronize this file-information struct with its mirror
1006  * on disk.  Note that all internal FS-operations that change
1007  * file information data should already call "sync" internally,
1008  * so this function is likely not useful for clients.
1009  * 
1010  * @param fi the struct to sync
1011  */
1012 void
1013 GNUNET_FS_file_information_sync_ (struct GNUNET_FS_FileInformation *f);
1014
1015 /**
1016  * Synchronize this publishing struct with its mirror
1017  * on disk.  Note that all internal FS-operations that change
1018  * publishing structs should already call "sync" internally,
1019  * so this function is likely not useful for clients.
1020  * 
1021  * @param pc the struct to sync
1022  */
1023 void
1024 GNUNET_FS_publish_sync_ (struct GNUNET_FS_PublishContext *pc);
1025
1026 /**
1027  * Synchronize this unindex struct with its mirror
1028  * on disk.  Note that all internal FS-operations that change
1029  * publishing structs should already call "sync" internally,
1030  * so this function is likely not useful for clients.
1031  * 
1032  * @param uc the struct to sync
1033  */
1034 void
1035 GNUNET_FS_unindex_sync_ (struct GNUNET_FS_UnindexContext *uc);
1036
1037 /**
1038  * Synchronize this search struct with its mirror
1039  * on disk.  Note that all internal FS-operations that change
1040  * publishing structs should already call "sync" internally,
1041  * so this function is likely not useful for clients.
1042  * 
1043  * @param sc the struct to sync
1044  */
1045 void
1046 GNUNET_FS_search_sync_ (struct GNUNET_FS_SearchContext *sc);
1047
1048 /**
1049  * Synchronize this search result with its mirror
1050  * on disk.  Note that all internal FS-operations that change
1051  * publishing structs should already call "sync" internally,
1052  * so this function is likely not useful for clients.
1053  * 
1054  * @param sr the struct to sync
1055  */
1056 void
1057 GNUNET_FS_search_result_sync_ (struct GNUNET_FS_SearchResult *sr);
1058
1059 /**
1060  * Synchronize this download struct with its mirror
1061  * on disk.  Note that all internal FS-operations that change
1062  * publishing structs should already call "sync" internally,
1063  * so this function is likely not useful for clients.
1064  * 
1065  * @param dc the struct to sync
1066  */
1067 void
1068 GNUNET_FS_download_sync_ (struct GNUNET_FS_DownloadContext *dc);
1069
1070 /**
1071  * Create SUSPEND event for the given publish operation
1072  * and then clean up our state (without stop signal).
1073  *
1074  * @param cls the 'struct GNUNET_FS_PublishContext' to signal for
1075  */
1076 void
1077 GNUNET_FS_publish_signal_suspend_ (void *cls);
1078
1079 /**
1080  * Create SUSPEND event for the given search operation
1081  * and then clean up our state (without stop signal).
1082  *
1083  * @param cls the 'struct GNUNET_FS_SearchContext' to signal for
1084  */
1085 void
1086 GNUNET_FS_search_signal_suspend_ (void *cls);
1087
1088 /**
1089  * Create SUSPEND event for the given download operation
1090  * and then clean up our state (without stop signal).
1091  *
1092  * @param cls the 'struct GNUNET_FS_DownloadContext' to signal for
1093  */
1094 void
1095 GNUNET_FS_download_signal_suspend_ (void *cls);
1096
1097 /**
1098  * Create SUSPEND event for the given unindex operation
1099  * and then clean up our state (without stop signal).
1100  *
1101  * @param cls the 'struct GNUNET_FS_UnindexContext' to signal for
1102  */
1103 void
1104 GNUNET_FS_unindex_signal_suspend_ (void *cls);
1105
1106 /**
1107  * Function signature of the functions that can be called
1108  * to trigger suspend signals and clean-up for top-level
1109  * activities.
1110  *
1111  * @param cls closure
1112  */
1113 typedef void (*SuspendSignalFunction)(void *cls);                                     
1114
1115 /**
1116  * We track all of the top-level activities of FS
1117  * so that we can signal 'suspend' on shutdown.
1118  */
1119 struct TopLevelActivity
1120 {
1121   /**
1122    * This is a doubly-linked list.
1123    */ 
1124   struct TopLevelActivity *next;
1125
1126   /**
1127    * This is a doubly-linked list.
1128    */  
1129   struct TopLevelActivity *prev;
1130
1131   /**
1132    * Function to call for suspend-signalling and clean up.
1133    */
1134   SuspendSignalFunction ssf;
1135
1136   /**
1137    * Closure for 'ssf' (some struct GNUNET_FS_XXXHandle*)
1138    */
1139   void *ssf_cls;
1140 };
1141
1142
1143 /**
1144  * Create a top-level activity entry.
1145  *
1146  * @param h global fs handle
1147  * @param ssf suspend signal function to use
1148  * @param ssf_cls closure for ssf
1149  * @return fresh top-level activity handle
1150  */
1151 struct TopLevelActivity *
1152 GNUNET_FS_make_top (struct GNUNET_FS_Handle *h,
1153                     SuspendSignalFunction ssf,
1154                     void *ssf_cls);
1155
1156
1157 /**
1158  * Destroy a top-level activity entry.
1159  * 
1160  * @param h global fs handle
1161  * @param top top level activity entry
1162  */
1163 void
1164 GNUNET_FS_end_top (struct GNUNET_FS_Handle *h,
1165                    struct TopLevelActivity *top);
1166
1167
1168
1169 /**
1170  * Master context for most FS operations.
1171  */
1172 struct GNUNET_FS_Handle
1173 {
1174   /**
1175    * Configuration to use.
1176    */
1177   const struct GNUNET_CONFIGURATION_Handle *cfg;
1178
1179   /**
1180    * Name of our client.
1181    */
1182   char *client_name;
1183
1184   /**
1185    * Function to call with updates on our progress.
1186    */
1187   GNUNET_FS_ProgressCallback upcb;
1188
1189   /**
1190    * Closure for upcb.
1191    */
1192   void *upcb_cls;
1193
1194   /**
1195    * Head of DLL of top-level activities.
1196    */
1197   struct TopLevelActivity *top_head;
1198
1199   /**
1200    * Tail of DLL of top-level activities.
1201    */
1202   struct TopLevelActivity *top_tail;
1203
1204   /**
1205    * Head of DLL of running jobs.
1206    */
1207   struct GNUNET_FS_QueueEntry *running_head;
1208
1209   /**
1210    * Tail of DLL of running jobs.
1211    */
1212   struct GNUNET_FS_QueueEntry *running_tail;
1213
1214   /**
1215    * Head of DLL of pending jobs.
1216    */
1217   struct GNUNET_FS_QueueEntry *pending_head;
1218
1219   /**
1220    * Tail of DLL of pending jobs.
1221    */
1222   struct GNUNET_FS_QueueEntry *pending_tail;
1223
1224   /**
1225    * Task that processes the jobs in the running and pending queues
1226    * (and moves jobs around as needed).
1227    */
1228   GNUNET_SCHEDULER_TaskIdentifier queue_job;
1229
1230   /**
1231    * Average time we take for a single request to be satisfied.
1232    * FIXME: not yet calcualted properly...
1233    */
1234   struct GNUNET_TIME_Relative avg_block_latency;
1235
1236   /**
1237    * How many actual downloads do we have running right now?
1238    */
1239   unsigned int active_downloads;
1240
1241   /**
1242    * How many blocks do the active downloads have?
1243    */
1244   unsigned int active_blocks;
1245
1246   /**
1247    * General flags.
1248    */
1249   enum GNUNET_FS_Flags flags;
1250
1251   /**
1252    * Maximum number of parallel downloads.
1253    */
1254   unsigned int max_parallel_downloads;
1255
1256   /**
1257    * Maximum number of parallel requests.
1258    */
1259   unsigned int max_parallel_requests;
1260
1261 };
1262
1263
1264 /**
1265  * Handle for controlling a publication process.
1266  */
1267 struct GNUNET_FS_PublishContext
1268 {
1269   /**
1270    * Handle to the global fs context.
1271    */ 
1272   struct GNUNET_FS_Handle *h;
1273
1274   /**
1275    * Our top-level activity entry (if we are top-level, otherwise NULL).
1276    */
1277   struct TopLevelActivity *top;
1278
1279   /**
1280    * File-structure that is being shared.
1281    */
1282   struct GNUNET_FS_FileInformation *fi;
1283
1284   /**
1285    * Namespace that we are publishing in, NULL if we have no namespace.
1286    */
1287   struct GNUNET_FS_Namespace *namespace;
1288
1289   /**
1290    * ID of the content in the namespace, NULL if we have no namespace.
1291    */
1292   char *nid;
1293
1294   /**
1295    * ID for future updates, NULL if we have no namespace or no updates.
1296    */
1297   char *nuid;
1298
1299   /**
1300    * Filename used for serializing information about this operation
1301    * (should be determined using 'mktemp').
1302    */
1303   char *serialization;
1304
1305   /**
1306    * Our own client handle for the FS service; only briefly used when
1307    * we start to index a file, otherwise NULL.
1308    */
1309   struct GNUNET_CLIENT_Connection *client;
1310
1311   /**
1312    * Current position in the file-tree for the upload.
1313    */
1314   struct GNUNET_FS_FileInformation *fi_pos;
1315
1316   /**
1317    * Non-null if we are currently hashing a file.
1318    */
1319   struct GNUNET_CRYPTO_FileHashContext *fhc;
1320
1321   /**
1322    * Connection to the datastore service.
1323    */
1324   struct GNUNET_DATASTORE_Handle *dsh;
1325
1326   /**
1327    * Queue entry for reservation/unreservation.
1328    */
1329   struct GNUNET_DATASTORE_QueueEntry *qre;
1330
1331   /**
1332    * ID of the task performing the upload. NO_TASK if the upload has
1333    * completed.
1334    */
1335   GNUNET_SCHEDULER_TaskIdentifier upload_task;
1336
1337   /**
1338    * Storage space to reserve for the operation.
1339    */
1340   uint64_t reserve_space;
1341
1342   /**
1343    * Overall number of entries to reserve for the
1344    * publish operation.
1345    */
1346   uint32_t reserve_entries;
1347
1348   /**
1349    * Typically GNUNET_NO.  Set to GNUNET_YES if "upload_task" is
1350    * GNUNET_SCHEDULER_NO_TASK and we're waiting for a response from
1351    * the datastore service (in which case this struct must not be
1352    * freed until we have that response).  If someone tries to stop the
1353    * download for good during this period, "in_network_wait" is set to
1354    * GNUNET_SYSERR which will cause the struct to be destroyed right
1355    * after we have the reply (or timeout) from the datastore service.
1356    */
1357   int in_network_wait;
1358
1359   /**
1360    * Options for publishing.
1361    */
1362   enum GNUNET_FS_PublishOptions options;
1363
1364   /**
1365    * Space reservation ID with datastore service
1366    * for this upload.
1367    */
1368   int rid;
1369
1370   /**
1371    * Set to GNUNET_YES if all processing has completed.
1372    */
1373   int all_done;
1374 };
1375
1376
1377 /**
1378  * Phases of unindex processing (state machine).
1379  */ 
1380 enum UnindexState
1381   {
1382     /**
1383      * We're currently hashing the file.
1384      */
1385     UNINDEX_STATE_HASHING = 0,
1386
1387     /**
1388      * We're telling the datastore to delete
1389      * the respective entries.
1390      */
1391     UNINDEX_STATE_DS_REMOVE = 1,
1392
1393     /**
1394      * We're notifying the FS service about
1395      * the unindexing.
1396      */
1397     UNINDEX_STATE_FS_NOTIFY = 2,
1398
1399     /**
1400      * We're done.
1401      */
1402     UNINDEX_STATE_COMPLETE = 3,
1403
1404     /**
1405      * We've encountered a fatal error.
1406      */
1407     UNINDEX_STATE_ERROR = 4
1408
1409   };
1410
1411
1412 /**
1413  * Handle for controlling an unindexing operation.
1414  */
1415 struct GNUNET_FS_UnindexContext
1416 {
1417   
1418   /**
1419    * Global FS context.
1420    */
1421   struct GNUNET_FS_Handle *h;
1422
1423   /**
1424    * Our top-level activity entry.
1425    */
1426   struct TopLevelActivity *top;
1427
1428   /**
1429    * Name of the file that we are unindexing.
1430    */
1431   char *filename;
1432
1433   /**
1434    * Short name under which we are serializing the state of this operation.
1435    */
1436   char *serialization;
1437
1438   /**
1439    * Connection to the FS service, only valid during the
1440    * UNINDEX_STATE_FS_NOTIFY phase.
1441    */
1442   struct GNUNET_CLIENT_Connection *client;
1443
1444   /**
1445    * Connection to the datastore service, only valid during the
1446    * UNINDEX_STATE_DS_NOTIFY phase.
1447    */
1448   struct GNUNET_DATASTORE_Handle *dsh;
1449
1450   /**
1451    * Pointer kept for the client.
1452    */
1453   void *client_info;
1454
1455   /**
1456    * Merkle-ish tree encoder context.
1457    */
1458   struct GNUNET_FS_TreeEncoder *tc;
1459
1460   /**
1461    * Handle used to read the file.
1462    */
1463   struct GNUNET_DISK_FileHandle *fh;
1464
1465   /**
1466    * Error message, NULL on success.
1467    */
1468   char *emsg;
1469
1470   /**
1471    * Context for hashing of the file.
1472    */
1473   struct GNUNET_CRYPTO_FileHashContext *fhc;
1474
1475   /**
1476    * Overall size of the file.
1477    */ 
1478   uint64_t file_size;
1479
1480   /**
1481    * When did we start?
1482    */
1483   struct GNUNET_TIME_Absolute start_time;
1484
1485   /**
1486    * Hash of the file's contents (once computed).
1487    */
1488   GNUNET_HashCode file_id;
1489  
1490   /**
1491    * Current operatinonal phase.
1492    */
1493   enum UnindexState state; 
1494
1495 };
1496
1497
1498 /**
1499  * Information we keep for each keyword in
1500  * a keyword search.
1501  */
1502 struct SearchRequestEntry
1503 {
1504   /**
1505    * Hash of the original keyword, also known as the
1506    * key (for decrypting the KBlock).
1507    */
1508   GNUNET_HashCode key;
1509
1510   /**
1511    * Hash of the public key, also known as the query.
1512    */
1513   GNUNET_HashCode query;  
1514
1515   /**
1516    * Map that contains a "struct GNUNET_FS_SearchResult" for each result that
1517    * was found under this keyword.  Note that the entries will point
1518    * to the same locations as those in the master result map (in
1519    * "struct GNUNET_FS_SearchContext"), so they should not be freed.
1520    * The key for each entry is the XOR of the key and query in the CHK
1521    * URI (as a unique identifier for the search result).
1522    */
1523   struct GNUNET_CONTAINER_MultiHashMap *results;
1524
1525   /**
1526    * Is this keyword a mandatory keyword
1527    * (started with '+')?
1528    */
1529   int mandatory;
1530
1531 };
1532
1533
1534 /**
1535  * Handle for controlling a search.
1536  */
1537 struct GNUNET_FS_SearchContext
1538 {
1539   /**
1540    * Handle to the global FS context.
1541    */
1542   struct GNUNET_FS_Handle *h;
1543
1544   /**
1545    * Our top-level activity entry (if we are top-level, otherwise NULL).
1546    */
1547   struct TopLevelActivity *top;
1548
1549   /**
1550    * List of keywords that we're looking for.
1551    */
1552   struct GNUNET_FS_Uri *uri;
1553
1554   /**
1555    * For update-searches, link to the search result that triggered
1556    * the update search; otherwise NULL.
1557    */
1558   struct GNUNET_FS_SearchResult *psearch_result;
1559
1560   /**
1561    * Connection to the FS service.
1562    */
1563   struct GNUNET_CLIENT_Connection *client;
1564
1565   /**
1566    * Pointer we keep for the client.
1567    */
1568   void *client_info;
1569
1570   /**
1571    * Name of the file on disk we use for persistence.
1572    */
1573   char *serialization;
1574
1575   /**
1576    * Error message (non-NULL if this operation failed).
1577    */
1578   char *emsg;
1579
1580   /**
1581    * Map that contains a "struct GNUNET_FS_SearchResult" for each result that
1582    * was found in the search.  The key for each entry is the XOR of
1583    * the key and query in the CHK URI (as a unique identifier for the
1584    * search result).
1585    */
1586   struct GNUNET_CONTAINER_MultiHashMap *master_result_map;
1587
1588   /**
1589    * Per-keyword information for a keyword search.  This array will
1590    * have exactly as many entries as there were keywords.
1591    */
1592   struct SearchRequestEntry *requests;
1593   
1594   /**
1595    * When did we start?
1596    */
1597   struct GNUNET_TIME_Absolute start_time;
1598
1599   /**
1600    * ID of a task that is using this struct and that must be cancelled
1601    * when the search is being stopped (if not
1602    * GNUNET_SCHEDULER_NO_TASK).  Used for the task that adds some
1603    * artificial delay when trying to reconnect to the FS service.
1604    */
1605   GNUNET_SCHEDULER_TaskIdentifier task;
1606   
1607   /**
1608    * Anonymity level for the search.
1609    */
1610   uint32_t anonymity;
1611
1612   /**
1613    * Number of mandatory keywords in this query.
1614    */
1615   uint32_t mandatory_count;
1616
1617   /**
1618    * Options for the search.
1619    */
1620   enum GNUNET_FS_SearchOptions options;  
1621 };
1622
1623
1624 /**
1625  * Information about an active download request.
1626  */ 
1627 struct DownloadRequest
1628 {
1629   /**
1630    * While pending, we keep all download requests in a linked list.
1631    */
1632   struct DownloadRequest *next;
1633
1634   /**
1635    * CHK for the request.
1636    */
1637   struct ContentHashKey chk;
1638
1639   /**
1640    * Offset of the corresponding block.
1641    */
1642   uint64_t offset;
1643
1644   /**
1645    * Depth of the corresponding block in the tree.
1646    */
1647   unsigned int depth;
1648
1649   /**
1650    * Set if this request is currently in the linked list of pending
1651    * requests.  Needed in case we get a response for a request that we
1652    * have not yet send (i.e. due to two blocks with identical
1653    * content); in this case, we would need to remove the block from
1654    * the pending list (and need a fast way to check if the block is on
1655    * it).
1656    */
1657   int is_pending;
1658
1659 };
1660
1661
1662 /**
1663  * Context for controlling a download.
1664  */
1665 struct GNUNET_FS_DownloadContext
1666 {
1667   
1668   /**
1669    * Global FS context.
1670    */ 
1671   struct GNUNET_FS_Handle *h;
1672
1673   /**
1674    * Our top-level activity entry (if we are top-level, otherwise NULL).
1675    */
1676   struct TopLevelActivity *top;
1677   
1678   /**
1679    * Connection to the FS service.
1680    */
1681   struct GNUNET_CLIENT_Connection *client;
1682
1683   /**
1684    * Parent download (used when downloading files
1685    * in directories).
1686    */
1687   struct GNUNET_FS_DownloadContext *parent;
1688
1689   /**
1690    * Associated search (used when downloading files
1691    * based on search results), or NULL for none.
1692    */
1693   struct GNUNET_FS_SearchResult *search;
1694
1695   /**
1696    * Head of list of child downloads.
1697    */
1698   struct GNUNET_FS_DownloadContext *child_head;
1699
1700   /**
1701    * Tail of list of child downloads.
1702    */
1703   struct GNUNET_FS_DownloadContext *child_tail;
1704
1705   /**
1706    * Previous download belonging to the same parent.
1707    */
1708   struct GNUNET_FS_DownloadContext *prev;
1709
1710   /**
1711    * Next download belonging to the same parent.
1712    */
1713   struct GNUNET_FS_DownloadContext *next;
1714
1715   /**
1716    * Context kept for the client.
1717    */
1718   void *client_info;
1719
1720   /**
1721    * URI that identifies the file that
1722    * we are downloading.
1723    */
1724   struct GNUNET_FS_Uri *uri;
1725
1726   /**
1727    * Known meta-data for the file (can be NULL).
1728    */
1729   struct GNUNET_CONTAINER_MetaData *meta;
1730
1731   /**
1732    * Error message, NULL if we're doing OK.
1733    */
1734   char *emsg;
1735
1736   /**
1737    * Random portion of filename we use for syncing state of this
1738    * download.
1739    */
1740   char *serialization;
1741
1742   /**
1743    * Where are we writing the data (name of the
1744    * file, can be NULL!).
1745    */
1746   char *filename;
1747
1748   /**
1749    * Where are we writing the data temporarily (name of the
1750    * file, can be NULL!); used if we do not have a permanent
1751    * name and we are a directory and we do a recursive download.
1752    */
1753   char *temp_filename;
1754
1755   /**
1756    * Map of active requests (those waiting
1757    * for a response).  The key is the hash
1758    * of the encryped block (aka query).
1759    */
1760   struct GNUNET_CONTAINER_MultiHashMap *active;
1761
1762   /**
1763    * Linked list of pending requests.
1764    */
1765   struct DownloadRequest *pending;
1766
1767   /**
1768    * Non-NULL if we are currently having a request for
1769    * transmission pending with the client handle.
1770    */
1771   struct GNUNET_CLIENT_TransmitHandle *th;
1772
1773   /**
1774    * Our entry in the job queue.
1775    */
1776   struct GNUNET_FS_QueueEntry *job_queue;
1777
1778   /**
1779    * Identity of the peer having the content, or all-zeros
1780    * if we don't know of such a peer.
1781    */
1782   struct GNUNET_PeerIdentity target;
1783
1784   /**
1785    * ID of a task that is using this struct
1786    * and that must be cancelled when the download
1787    * is being stopped (if not GNUNET_SCHEDULER_NO_TASK).
1788    * Used for the task that adds some artificial
1789    * delay when trying to reconnect to the FS
1790    * service.
1791    */
1792   GNUNET_SCHEDULER_TaskIdentifier task;
1793
1794   /**
1795    * What was the size of the file on disk that we're downloading
1796    * before we started?  Used to detect if there is a point in
1797    * checking an existing block on disk for matching the desired
1798    * content.  0 if the file did not exist already.
1799    */
1800   uint64_t old_file_size;
1801
1802   /**
1803    * What is the first offset that we're interested
1804    * in?
1805    */
1806   uint64_t offset;
1807
1808   /**
1809    * How many bytes starting from offset are desired?
1810    * This is NOT the overall length of the file!
1811    */
1812   uint64_t length;
1813
1814   /**
1815    * How many bytes have we already received within
1816    * the specified range (DBlocks only).
1817    */
1818   uint64_t completed;
1819
1820   /**
1821    * Time download was started.
1822    */
1823   struct GNUNET_TIME_Absolute start_time;
1824
1825   /**
1826    * Desired level of anonymity.
1827    */
1828   uint32_t anonymity;
1829
1830   /**
1831    * The depth of the file-tree.
1832    */
1833   unsigned int treedepth;
1834
1835   /**
1836    * Options for the download.
1837    */
1838   enum GNUNET_FS_DownloadOptions options;
1839
1840   /**
1841    * Flag set upon transitive completion (includes child downloads).
1842    * This flag is only set to GNUNET_YES for directories where all
1843    * child-downloads have also completed (and signalled completion).
1844    */
1845   int has_finished;
1846
1847   /**
1848    * Have we tried (and failed) to find matching full
1849    * data from the meta data yet?
1850    */
1851   int tried_full_data;
1852 };
1853
1854
1855 /**
1856  * Information about an (updateable) node in the
1857  * namespace.
1858  */
1859 struct NamespaceUpdateNode
1860 {
1861   /**
1862    * Identifier for this node.
1863    */
1864   char *id;
1865
1866   /**
1867    * Identifier of children of this node.
1868    */
1869   char *update;
1870
1871   /**
1872    * Metadata for this entry.
1873    */
1874   struct GNUNET_CONTAINER_MetaData *md;
1875
1876   /**
1877    * URI of this entry in the namespace.
1878    */
1879   struct GNUNET_FS_Uri *uri;
1880
1881   /**
1882    * Namespace update generation ID.  Used to ensure
1883    * freshness of the scc_id.
1884    */
1885   unsigned int nug;
1886   
1887   /**
1888    * SCC this entry belongs to (if nug is current).
1889    */
1890   unsigned int scc_id;
1891
1892 };
1893
1894
1895 struct GNUNET_FS_Namespace
1896 {
1897
1898   /**
1899    * Handle to the FS service context.
1900    */
1901   struct GNUNET_FS_Handle *h;
1902   
1903   /**
1904    * Array with information about nodes in the namespace.
1905    */
1906   struct NamespaceUpdateNode **update_nodes;
1907
1908   /**
1909    * Private key for the namespace.
1910    */
1911   struct GNUNET_CRYPTO_RsaPrivateKey *key;
1912
1913   /**
1914    * Hash map mapping identifiers of update nodes
1915    * to the update nodes (initialized on-demand).
1916    */
1917   struct GNUNET_CONTAINER_MultiHashMap *update_map;
1918
1919   /**
1920    * Name of the file with the private key.
1921    */
1922   char *filename;
1923
1924   /**
1925    * Name of the namespace.
1926    */ 
1927   char *name;
1928
1929   /**
1930    * Size of the update nodes array.
1931    */
1932   unsigned int update_node_count;
1933
1934   /**
1935    * Reference counter.
1936    */
1937   unsigned int rc;
1938
1939   /**
1940    * Generator for unique nug numbers.
1941    */
1942   unsigned int nug_gen;
1943 };
1944
1945
1946 /**
1947  * Message sent from a GNUnet (fs) publishing activity to the
1948  * gnunet-fs-service to initiate indexing of a file.  The service is
1949  * supposed to check if the specified file is available and has the
1950  * same cryptographic hash.  It should then respond with either a
1951  * confirmation or a denial.
1952  *
1953  * On OSes where this works, it is considered acceptable if the
1954  * service only checks that the path, device and inode match (it can
1955  * then be assumed that the hash will also match without actually
1956  * computing it; this is an optimization that should be safe given
1957  * that the client is not our adversary).
1958  */
1959 struct IndexStartMessage
1960 {
1961
1962   /**
1963    * Message type will be GNUNET_MESSAGE_TYPE_FS_INDEX_START.
1964    */
1965   struct GNUNET_MessageHeader header;
1966
1967   /**
1968    * For alignment.
1969    */
1970   uint32_t reserved GNUNET_PACKED;
1971
1972   /**
1973    * ID of device containing the file, as seen by the client.  This
1974    * device ID is obtained using a call like "statvfs" (and converting
1975    * the "f_fsid" field to a 32-bit big-endian number).  Use 0 if the
1976    * OS does not support this, in which case the service must do a
1977    * full hash recomputation.
1978    */
1979   uint64_t device GNUNET_PACKED;
1980   
1981   /**
1982    * Inode of the file on the given device, as seen by the client
1983    * ("st_ino" field from "struct stat").  Use 0 if the OS does not
1984    * support this, in which case the service must do a full hash
1985    * recomputation.
1986    */
1987   uint64_t inode GNUNET_PACKED;
1988
1989   /**
1990    * Hash of the file that we would like to index.
1991    */
1992   GNUNET_HashCode file_id;
1993
1994   /* this is followed by a 0-terminated
1995      filename of a file with the hash
1996      "file_id" as seen by the client */
1997
1998 };
1999
2000
2001 /**
2002  * Message send by FS service in response to a request
2003  * asking for a list of all indexed files.
2004  */
2005 struct IndexInfoMessage
2006 {
2007   /**
2008    * Message type will be 
2009    * GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_ENTRY.
2010    */
2011   struct GNUNET_MessageHeader header;
2012
2013   /**
2014    * Always zero.
2015    */
2016   uint32_t reserved GNUNET_PACKED;
2017
2018   /**
2019    * Hash of the indexed file.
2020    */
2021   GNUNET_HashCode file_id;
2022
2023   /* this is followed by a 0-terminated
2024      filename of a file with the hash
2025      "file_id" as seen by the client */
2026   
2027 };
2028
2029
2030 /**
2031  * Message sent from a GNUnet (fs) unindexing activity to the
2032  * gnunet-service-fs to indicate that a file will be unindexed.  The
2033  * service is supposed to remove the file from the list of indexed
2034  * files and response with a confirmation message (even if the file
2035  * was already not on the list).
2036  */
2037 struct UnindexMessage
2038 {
2039
2040   /**
2041    * Message type will be 
2042    * GNUNET_MESSAGE_TYPE_FS_UNINDEX.
2043    */
2044   struct GNUNET_MessageHeader header;
2045
2046   /**
2047    * Always zero.
2048    */
2049   uint32_t reserved GNUNET_PACKED;
2050
2051   /**
2052    * Hash of the file that we will unindex.
2053    */
2054   GNUNET_HashCode file_id;
2055
2056 };
2057
2058
2059 /**
2060  * Message sent from a GNUnet (fs) search activity to the
2061  * gnunet-service-fs to start a search.
2062  */
2063 struct SearchMessage
2064 {
2065
2066   /**
2067    * Message type will be 
2068    * GNUNET_MESSAGE_TYPE_FS_START_SEARCH.
2069    */
2070   struct GNUNET_MessageHeader header;
2071
2072   /**
2073    * Bitmask with options.  Zero for no options, one for loopback-only.  
2074    * Other bits are currently not defined.
2075    */
2076   int32_t options GNUNET_PACKED;
2077
2078   /**
2079    * Type of the content that we're looking for.
2080    */
2081   uint32_t type GNUNET_PACKED;
2082
2083   /**
2084    * Desired anonymity level, big-endian.
2085    */
2086   uint32_t anonymity_level GNUNET_PACKED;
2087
2088   /**
2089    * If the request is for a DBLOCK or IBLOCK, this is the identity of
2090    * the peer that is known to have a response.  Set to all-zeros if
2091    * such a target is not known (note that even if OUR anonymity
2092    * level is >0 we may happen to know the responder's identity;
2093    * nevertheless, we should probably not use it for a DHT-lookup
2094    * or similar blunt actions in order to avoid exposing ourselves).
2095    * <p>
2096    * If the request is for an SBLOCK, this is the identity of the
2097    * pseudonym to which the SBLOCK belongs. 
2098    * <p>
2099    * If the request is for a KBLOCK, "target" must be all zeros.
2100    */
2101   GNUNET_HashCode target;
2102
2103   /**
2104    * Hash of the keyword (aka query) for KBLOCKs; Hash of
2105    * the CHK-encoded block for DBLOCKS and IBLOCKS (aka query)
2106    * and hash of the identifier XORed with the target for
2107    * SBLOCKS (aka query).
2108    */
2109   GNUNET_HashCode query;
2110
2111   /* this is followed by the hash codes of already-known
2112      results (which should hence be excluded from what
2113      the service returns); naturally, this only applies
2114      to queries that can have multiple results, such as
2115      those for KBLOCKS (KSK) and SBLOCKS (SKS) */
2116 };
2117
2118
2119 /**
2120  * Only the (mandatory) query is included.
2121  */
2122 #define GET_MESSAGE_BIT_QUERY_ONLY 0
2123
2124 /**
2125  * The peer identity of a peer waiting for the
2126  * reply is included (used if the response
2127  * should be transmitted to someone other than
2128  * the sender of the GET).
2129  */
2130 #define GET_MESSAGE_BIT_RETURN_TO 1
2131
2132 /**
2133  * The hash of the public key of the target
2134  * namespace is included (for SKS queries).
2135  */
2136 #define GET_MESSAGE_BIT_SKS_NAMESPACE 2
2137
2138 /**
2139  * The peer identity of a peer that had claimed to have the content
2140  * previously is included (can be used if responder-anonymity is not
2141  * desired; note that the precursor presumably lacked a direct
2142  * connection to the specified peer; still, the receiver is in no way
2143  * required to limit forwarding only to the specified peer, it should
2144  * only prefer it somewhat if possible).
2145  */
2146 #define GET_MESSAGE_BIT_TRANSMIT_TO 4
2147
2148
2149 /**
2150  * Message sent between peers asking for FS-content.
2151  */
2152 struct GetMessage
2153 {
2154
2155   /**
2156    * Message type will be GNUNET_MESSAGE_TYPE_FS_GET.
2157    */
2158   struct GNUNET_MessageHeader header;
2159
2160   /**
2161    * Type of the query (block type).
2162    */
2163   uint32_t type GNUNET_PACKED;
2164
2165   /**
2166    * How important is this request (network byte order)
2167    */
2168   uint32_t priority GNUNET_PACKED;
2169
2170   /**
2171    * Relative time to live in MILLISECONDS (network byte order)
2172    */
2173   int32_t ttl GNUNET_PACKED;
2174
2175   /**
2176    * The content hash should be mutated using this value
2177    * before checking against the bloomfilter (used to
2178    * get many different filters for the same hash codes).
2179    * The number should be in big-endian format when used
2180    * for mingling.
2181    */
2182   int32_t filter_mutator GNUNET_PACKED;
2183
2184   /**
2185    * Which of the optional hash codes are present at the end of the
2186    * message?  See GET_MESSAGE_BIT_xx constants.  For each bit that is
2187    * set, an additional GNUNET_HashCode with the respective content
2188    * (in order of the bits) will be appended to the end of the GET
2189    * message.
2190    */
2191   uint32_t hash_bitmap GNUNET_PACKED;
2192
2193   /**
2194    * Hashcodes of the file(s) we're looking for.
2195    * Details depend on the query type.
2196    */
2197   GNUNET_HashCode query GNUNET_PACKED;
2198
2199   /* this is followed by hash codes
2200      as specified in the  "hash_bitmap";
2201      after that, an optional bloomfilter
2202      (with bits set for replies that should
2203      be suppressed) can be present */
2204 };
2205
2206
2207 /**
2208  * Response from FS service with a result for a previous FS search.
2209  * Note that queries for DBLOCKS and IBLOCKS that have received a
2210  * single response are considered done.  This message is transmitted
2211  * between peers as well as between the service and a client.
2212  */
2213 struct PutMessage
2214 {
2215
2216   /**
2217    * Message type will be GNUNET_MESSAGE_TYPE_FS_PUT.
2218    */
2219   struct GNUNET_MessageHeader header;
2220
2221   /**
2222    * Type of the block (in big endian).  Should never be zero.
2223    */
2224   uint32_t type GNUNET_PACKED;
2225
2226   /**
2227    * When does this result expire? 
2228    */
2229   struct GNUNET_TIME_AbsoluteNBO expiration;
2230
2231   /* this is followed by the actual encrypted content */
2232
2233 };
2234
2235
2236 /**
2237  * Message send by a peer that wants to be excluded
2238  * from migration for a while.
2239  */
2240 struct MigrationStopMessage
2241 {
2242   /**
2243    * Message type will be 
2244    * GNUNET_MESSAGE_TYPE_FS_MIGRATION_STOP.
2245    */
2246   struct GNUNET_MessageHeader header;
2247
2248   /**
2249    * Always zero.
2250    */
2251   uint32_t reserved GNUNET_PACKED;
2252
2253   /**
2254    * How long should the block last?
2255    */
2256   struct GNUNET_TIME_RelativeNBO duration;
2257
2258 };
2259
2260
2261
2262 #endif
2263
2264 /* end of fs.h */