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