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