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