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