fix
[oweals/gnunet.git] / src / fs / fs.h
1
2 /*
3      This file is part of GNUnet.
4      (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 Christian Grothoff (and other contributing authors)
5
6      GNUnet is free software; you can redistribute it and/or modify
7      it under the terms of the GNU General Public License as published
8      by the Free Software Foundation; either version 2, 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    * Connection to the datastore service.
1306    */
1307   struct GNUNET_DATASTORE_Handle *dsh;
1308
1309   /**
1310    * ID of the task performing the upload. NO_TASK if the upload has
1311    * completed.
1312    */
1313   GNUNET_SCHEDULER_TaskIdentifier upload_task;
1314
1315   /**
1316    * Typically GNUNET_NO.  Set to GNUNET_YES if "upload_task" is
1317    * GNUNET_SCHEDULER_NO_TASK and we're waiting for a response from
1318    * the datastore service (in which case this struct must not be
1319    * freed until we have that response).  If someone tries to stop the
1320    * download for good during this period, "in_network_wait" is set to
1321    * GNUNET_SYSERR which will cause the struct to be destroyed right
1322    * after we have the reply (or timeout) from the datastore service.
1323    */
1324   int in_network_wait;
1325
1326   /**
1327    * Options for publishing.
1328    */
1329   enum GNUNET_FS_PublishOptions options;
1330
1331   /**
1332    * Space reservation ID with datastore service
1333    * for this upload.
1334    */
1335   int rid;
1336
1337   /**
1338    * Set to GNUNET_YES if all processing has completed.
1339    */
1340   int all_done;
1341 };
1342
1343
1344 /**
1345  * Phases of unindex processing (state machine).
1346  */ 
1347 enum UnindexState
1348   {
1349     /**
1350      * We're currently hashing the file.
1351      */
1352     UNINDEX_STATE_HASHING = 0,
1353
1354     /**
1355      * We're telling the datastore to delete
1356      * the respective entries.
1357      */
1358     UNINDEX_STATE_DS_REMOVE = 1,
1359
1360     /**
1361      * We're notifying the FS service about
1362      * the unindexing.
1363      */
1364     UNINDEX_STATE_FS_NOTIFY = 2,
1365
1366     /**
1367      * We're done.
1368      */
1369     UNINDEX_STATE_COMPLETE = 3,
1370
1371     /**
1372      * We've encountered a fatal error.
1373      */
1374     UNINDEX_STATE_ERROR = 4
1375
1376   };
1377
1378
1379 /**
1380  * Handle for controlling an unindexing operation.
1381  */
1382 struct GNUNET_FS_UnindexContext
1383 {
1384   
1385   /**
1386    * Global FS context.
1387    */
1388   struct GNUNET_FS_Handle *h;
1389
1390   /**
1391    * Our top-level activity entry.
1392    */
1393   struct TopLevelActivity *top;
1394
1395   /**
1396    * Name of the file that we are unindexing.
1397    */
1398   char *filename;
1399
1400   /**
1401    * Short name under which we are serializing the state of this operation.
1402    */
1403   char *serialization;
1404
1405   /**
1406    * Connection to the FS service, only valid during the
1407    * UNINDEX_STATE_FS_NOTIFY phase.
1408    */
1409   struct GNUNET_CLIENT_Connection *client;
1410
1411   /**
1412    * Connection to the datastore service, only valid during the
1413    * UNINDEX_STATE_DS_NOTIFY phase.
1414    */
1415   struct GNUNET_DATASTORE_Handle *dsh;
1416
1417   /**
1418    * Pointer kept for the client.
1419    */
1420   void *client_info;
1421
1422   /**
1423    * Merkle-ish tree encoder context.
1424    */
1425   struct GNUNET_FS_TreeEncoder *tc;
1426
1427   /**
1428    * Handle used to read the file.
1429    */
1430   struct GNUNET_DISK_FileHandle *fh;
1431
1432   /**
1433    * Error message, NULL on success.
1434    */
1435   char *emsg;
1436
1437   /**
1438    * Context for hashing of the file.
1439    */
1440   struct GNUNET_CRYPTO_FileHashContext *fhc;
1441
1442   /**
1443    * Overall size of the file.
1444    */ 
1445   uint64_t file_size;
1446
1447   /**
1448    * When did we start?
1449    */
1450   struct GNUNET_TIME_Absolute start_time;
1451
1452   /**
1453    * Hash of the file's contents (once computed).
1454    */
1455   GNUNET_HashCode file_id;
1456  
1457   /**
1458    * Current operatinonal phase.
1459    */
1460   enum UnindexState state; 
1461
1462 };
1463
1464
1465 /**
1466  * Information we keep for each keyword in
1467  * a keyword search.
1468  */
1469 struct SearchRequestEntry
1470 {
1471   /**
1472    * Hash of the original keyword, also known as the
1473    * key (for decrypting the KBlock).
1474    */
1475   GNUNET_HashCode key;
1476
1477   /**
1478    * Hash of the public key, also known as the query.
1479    */
1480   GNUNET_HashCode query;  
1481
1482   /**
1483    * Map that contains a "struct GNUNET_FS_SearchResult" for each result that
1484    * was found under this keyword.  Note that the entries will point
1485    * to the same locations as those in the master result map (in
1486    * "struct GNUNET_FS_SearchContext"), so they should not be freed.
1487    * The key for each entry is the XOR of the key and query in the CHK
1488    * URI (as a unique identifier for the search result).
1489    */
1490   struct GNUNET_CONTAINER_MultiHashMap *results;
1491
1492   /**
1493    * Is this keyword a mandatory keyword
1494    * (started with '+')?
1495    */
1496   int mandatory;
1497
1498 };
1499
1500
1501 /**
1502  * Handle for controlling a search.
1503  */
1504 struct GNUNET_FS_SearchContext
1505 {
1506   /**
1507    * Handle to the global FS context.
1508    */
1509   struct GNUNET_FS_Handle *h;
1510
1511   /**
1512    * Our top-level activity entry (if we are top-level, otherwise NULL).
1513    */
1514   struct TopLevelActivity *top;
1515
1516   /**
1517    * List of keywords that we're looking for.
1518    */
1519   struct GNUNET_FS_Uri *uri;
1520
1521   /**
1522    * For update-searches, link to the search result that triggered
1523    * the update search; otherwise NULL.
1524    */
1525   struct GNUNET_FS_SearchResult *psearch_result;
1526
1527   /**
1528    * Connection to the FS service.
1529    */
1530   struct GNUNET_CLIENT_Connection *client;
1531
1532   /**
1533    * Pointer we keep for the client.
1534    */
1535   void *client_info;
1536
1537   /**
1538    * Name of the file on disk we use for persistence.
1539    */
1540   char *serialization;
1541
1542   /**
1543    * Error message (non-NULL if this operation failed).
1544    */
1545   char *emsg;
1546
1547   /**
1548    * Map that contains a "struct GNUNET_FS_SearchResult" for each result that
1549    * was found in the search.  The key for each entry is the XOR of
1550    * the key and query in the CHK URI (as a unique identifier for the
1551    * search result).
1552    */
1553   struct GNUNET_CONTAINER_MultiHashMap *master_result_map;
1554
1555   /**
1556    * Per-keyword information for a keyword search.  This array will
1557    * have exactly as many entries as there were keywords.
1558    */
1559   struct SearchRequestEntry *requests;
1560   
1561   /**
1562    * When did we start?
1563    */
1564   struct GNUNET_TIME_Absolute start_time;
1565
1566   /**
1567    * ID of a task that is using this struct and that must be cancelled
1568    * when the search is being stopped (if not
1569    * GNUNET_SCHEDULER_NO_TASK).  Used for the task that adds some
1570    * artificial delay when trying to reconnect to the FS service.
1571    */
1572   GNUNET_SCHEDULER_TaskIdentifier task;
1573   
1574   /**
1575    * Anonymity level for the search.
1576    */
1577   uint32_t anonymity;
1578
1579   /**
1580    * Number of mandatory keywords in this query.
1581    */
1582   uint32_t mandatory_count;
1583
1584   /**
1585    * Options for the search.
1586    */
1587   enum GNUNET_FS_SearchOptions options;  
1588 };
1589
1590
1591 /**
1592  * Information about an active download request.
1593  */ 
1594 struct DownloadRequest
1595 {
1596   /**
1597    * While pending, we keep all download requests in a linked list.
1598    */
1599   struct DownloadRequest *next;
1600
1601   /**
1602    * CHK for the request.
1603    */
1604   struct ContentHashKey chk;
1605
1606   /**
1607    * Offset of the corresponding block.
1608    */
1609   uint64_t offset;
1610
1611   /**
1612    * Depth of the corresponding block in the tree.
1613    */
1614   unsigned int depth;
1615
1616   /**
1617    * Set if this request is currently in the linked list of pending
1618    * requests.  Needed in case we get a response for a request that we
1619    * have not yet send (i.e. due to two blocks with identical
1620    * content); in this case, we would need to remove the block from
1621    * the pending list (and need a fast way to check if the block is on
1622    * it).
1623    */
1624   int is_pending;
1625
1626 };
1627
1628
1629 /**
1630  * Context for controlling a download.
1631  */
1632 struct GNUNET_FS_DownloadContext
1633 {
1634   
1635   /**
1636    * Global FS context.
1637    */ 
1638   struct GNUNET_FS_Handle *h;
1639
1640   /**
1641    * Our top-level activity entry (if we are top-level, otherwise NULL).
1642    */
1643   struct TopLevelActivity *top;
1644   
1645   /**
1646    * Connection to the FS service.
1647    */
1648   struct GNUNET_CLIENT_Connection *client;
1649
1650   /**
1651    * Parent download (used when downloading files
1652    * in directories).
1653    */
1654   struct GNUNET_FS_DownloadContext *parent;
1655
1656   /**
1657    * Associated search (used when downloading files
1658    * based on search results), or NULL for none.
1659    */
1660   struct GNUNET_FS_SearchResult *search;
1661
1662   /**
1663    * Head of list of child downloads.
1664    */
1665   struct GNUNET_FS_DownloadContext *child_head;
1666
1667   /**
1668    * Tail of list of child downloads.
1669    */
1670   struct GNUNET_FS_DownloadContext *child_tail;
1671
1672   /**
1673    * Previous download belonging to the same parent.
1674    */
1675   struct GNUNET_FS_DownloadContext *prev;
1676
1677   /**
1678    * Next download belonging to the same parent.
1679    */
1680   struct GNUNET_FS_DownloadContext *next;
1681
1682   /**
1683    * Context kept for the client.
1684    */
1685   void *client_info;
1686
1687   /**
1688    * URI that identifies the file that
1689    * we are downloading.
1690    */
1691   struct GNUNET_FS_Uri *uri;
1692
1693   /**
1694    * Known meta-data for the file (can be NULL).
1695    */
1696   struct GNUNET_CONTAINER_MetaData *meta;
1697
1698   /**
1699    * Error message, NULL if we're doing OK.
1700    */
1701   char *emsg;
1702
1703   /**
1704    * Random portion of filename we use for syncing state of this
1705    * download.
1706    */
1707   char *serialization;
1708
1709   /**
1710    * Where are we writing the data (name of the
1711    * file, can be NULL!).
1712    */
1713   char *filename;
1714
1715   /**
1716    * Where are we writing the data temporarily (name of the
1717    * file, can be NULL!); used if we do not have a permanent
1718    * name and we are a directory and we do a recursive download.
1719    */
1720   char *temp_filename;
1721
1722   /**
1723    * Map of active requests (those waiting
1724    * for a response).  The key is the hash
1725    * of the encryped block (aka query).
1726    */
1727   struct GNUNET_CONTAINER_MultiHashMap *active;
1728
1729   /**
1730    * Linked list of pending requests.
1731    */
1732   struct DownloadRequest *pending;
1733
1734   /**
1735    * Non-NULL if we are currently having a request for
1736    * transmission pending with the client handle.
1737    */
1738   struct GNUNET_CLIENT_TransmitHandle *th;
1739
1740   /**
1741    * Our entry in the job queue.
1742    */
1743   struct GNUNET_FS_QueueEntry *job_queue;
1744
1745   /**
1746    * Identity of the peer having the content, or all-zeros
1747    * if we don't know of such a peer.
1748    */
1749   struct GNUNET_PeerIdentity target;
1750
1751   /**
1752    * ID of a task that is using this struct
1753    * and that must be cancelled when the download
1754    * is being stopped (if not GNUNET_SCHEDULER_NO_TASK).
1755    * Used for the task that adds some artificial
1756    * delay when trying to reconnect to the FS
1757    * service.
1758    */
1759   GNUNET_SCHEDULER_TaskIdentifier task;
1760
1761   /**
1762    * What was the size of the file on disk that we're downloading
1763    * before we started?  Used to detect if there is a point in
1764    * checking an existing block on disk for matching the desired
1765    * content.  0 if the file did not exist already.
1766    */
1767   uint64_t old_file_size;
1768
1769   /**
1770    * What is the first offset that we're interested
1771    * in?
1772    */
1773   uint64_t offset;
1774
1775   /**
1776    * How many bytes starting from offset are desired?
1777    * This is NOT the overall length of the file!
1778    */
1779   uint64_t length;
1780
1781   /**
1782    * How many bytes have we already received within
1783    * the specified range (DBlocks only).
1784    */
1785   uint64_t completed;
1786
1787   /**
1788    * Time download was started.
1789    */
1790   struct GNUNET_TIME_Absolute start_time;
1791
1792   /**
1793    * Desired level of anonymity.
1794    */
1795   uint32_t anonymity;
1796
1797   /**
1798    * The depth of the file-tree.
1799    */
1800   unsigned int treedepth;
1801
1802   /**
1803    * Options for the download.
1804    */
1805   enum GNUNET_FS_DownloadOptions options;
1806
1807   /**
1808    * Flag set upon transitive completion (includes child downloads).
1809    * This flag is only set to GNUNET_YES for directories where all
1810    * child-downloads have also completed (and signalled completion).
1811    */
1812   int has_finished;
1813
1814 };
1815
1816 struct GNUNET_FS_Namespace
1817 {
1818
1819   /**
1820    * Private key for the namespace.
1821    */
1822   struct GNUNET_CRYPTO_RsaPrivateKey *key;
1823
1824   /**
1825    * Name of the file with the private key.
1826    */
1827   char *filename;
1828
1829   /**
1830    * Name of the namespace.
1831    */ 
1832   char *name;
1833
1834   /**
1835    * Reference counter.
1836    */
1837   unsigned int rc;
1838 };
1839
1840
1841 /**
1842  * Message sent from a GNUnet (fs) publishing activity to the
1843  * gnunet-fs-service to initiate indexing of a file.  The service is
1844  * supposed to check if the specified file is available and has the
1845  * same cryptographic hash.  It should then respond with either a
1846  * confirmation or a denial.
1847  *
1848  * On OSes where this works, it is considered acceptable if the
1849  * service only checks that the path, device and inode match (it can
1850  * then be assumed that the hash will also match without actually
1851  * computing it; this is an optimization that should be safe given
1852  * that the client is not our adversary).
1853  */
1854 struct IndexStartMessage
1855 {
1856
1857   /**
1858    * Message type will be GNUNET_MESSAGE_TYPE_FS_INDEX_START.
1859    */
1860   struct GNUNET_MessageHeader header;
1861
1862   /**
1863    * ID of device containing the file, as seen by the client.  This
1864    * device ID is obtained using a call like "statvfs" (and converting
1865    * the "f_fsid" field to a 32-bit big-endian number).  Use 0 if the
1866    * OS does not support this, in which case the service must do a
1867    * full hash recomputation.
1868    */
1869   uint32_t device GNUNET_PACKED;
1870   
1871   /**
1872    * Inode of the file on the given device, as seen by the client
1873    * ("st_ino" field from "struct stat").  Use 0 if the OS does not
1874    * support this, in which case the service must do a full hash
1875    * recomputation.
1876    */
1877   uint64_t inode GNUNET_PACKED;
1878
1879   /**
1880    * Hash of the file that we would like to index.
1881    */
1882   GNUNET_HashCode file_id;
1883
1884   /* this is followed by a 0-terminated
1885      filename of a file with the hash
1886      "file_id" as seen by the client */
1887
1888 };
1889
1890
1891 /**
1892  * Message send by FS service in response to a request
1893  * asking for a list of all indexed files.
1894  */
1895 struct IndexInfoMessage
1896 {
1897   /**
1898    * Message type will be 
1899    * GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_ENTRY.
1900    */
1901   struct GNUNET_MessageHeader header;
1902
1903   /**
1904    * Always zero.
1905    */
1906   uint32_t reserved GNUNET_PACKED;
1907
1908   /**
1909    * Hash of the indexed file.
1910    */
1911   GNUNET_HashCode file_id;
1912
1913   /* this is followed by a 0-terminated
1914      filename of a file with the hash
1915      "file_id" as seen by the client */
1916   
1917 };
1918
1919
1920 /**
1921  * Message sent from a GNUnet (fs) unindexing activity to the
1922  * gnunet-service-fs to indicate that a file will be unindexed.  The
1923  * service is supposed to remove the file from the list of indexed
1924  * files and response with a confirmation message (even if the file
1925  * was already not on the list).
1926  */
1927 struct UnindexMessage
1928 {
1929
1930   /**
1931    * Message type will be 
1932    * GNUNET_MESSAGE_TYPE_FS_UNINDEX.
1933    */
1934   struct GNUNET_MessageHeader header;
1935
1936   /**
1937    * Always zero.
1938    */
1939   uint32_t reserved GNUNET_PACKED;
1940
1941   /**
1942    * Hash of the file that we will unindex.
1943    */
1944   GNUNET_HashCode file_id;
1945
1946 };
1947
1948
1949 /**
1950  * Message sent from a GNUnet (fs) search activity to the
1951  * gnunet-service-fs to start a search.
1952  */
1953 struct SearchMessage
1954 {
1955
1956   /**
1957    * Message type will be 
1958    * GNUNET_MESSAGE_TYPE_FS_START_SEARCH.
1959    */
1960   struct GNUNET_MessageHeader header;
1961
1962   /**
1963    * Bitmask with options.  Zero for no options, one for loopback-only.  
1964    * Other bits are currently not defined.
1965    */
1966   int32_t options GNUNET_PACKED;
1967
1968   /**
1969    * Type of the content that we're looking for.
1970    */
1971   uint32_t type GNUNET_PACKED;
1972
1973   /**
1974    * Desired anonymity level, big-endian.
1975    */
1976   uint32_t anonymity_level GNUNET_PACKED;
1977
1978   /**
1979    * If the request is for a DBLOCK or IBLOCK, this is the identity of
1980    * the peer that is known to have a response.  Set to all-zeros if
1981    * such a target is not known (note that even if OUR anonymity
1982    * level is >0 we may happen to know the responder's identity;
1983    * nevertheless, we should probably not use it for a DHT-lookup
1984    * or similar blunt actions in order to avoid exposing ourselves).
1985    * <p>
1986    * If the request is for an SBLOCK, this is the identity of the
1987    * pseudonym to which the SBLOCK belongs. 
1988    * <p>
1989    * If the request is for a KBLOCK, "target" must be all zeros.
1990    */
1991   GNUNET_HashCode target;
1992
1993   /**
1994    * Hash of the keyword (aka query) for KBLOCKs; Hash of
1995    * the CHK-encoded block for DBLOCKS and IBLOCKS (aka query)
1996    * and hash of the identifier XORed with the target for
1997    * SBLOCKS (aka query).
1998    */
1999   GNUNET_HashCode query;
2000
2001   /* this is followed by the hash codes of already-known
2002      results (which should hence be excluded from what
2003      the service returns); naturally, this only applies
2004      to queries that can have multiple results, such as
2005      those for KBLOCKS (KSK) and SBLOCKS (SKS) */
2006 };
2007
2008
2009 /**
2010  * Only the (mandatory) query is included.
2011  */
2012 #define GET_MESSAGE_BIT_QUERY_ONLY 0
2013
2014 /**
2015  * The peer identity of a peer waiting for the
2016  * reply is included (used if the response
2017  * should be transmitted to someone other than
2018  * the sender of the GET).
2019  */
2020 #define GET_MESSAGE_BIT_RETURN_TO 1
2021
2022 /**
2023  * The hash of the public key of the target
2024  * namespace is included (for SKS queries).
2025  */
2026 #define GET_MESSAGE_BIT_SKS_NAMESPACE 2
2027
2028 /**
2029  * The peer identity of a peer that had claimed to have the content
2030  * previously is included (can be used if responder-anonymity is not
2031  * desired; note that the precursor presumably lacked a direct
2032  * connection to the specified peer; still, the receiver is in no way
2033  * required to limit forwarding only to the specified peer, it should
2034  * only prefer it somewhat if possible).
2035  */
2036 #define GET_MESSAGE_BIT_TRANSMIT_TO 4
2037
2038
2039 /**
2040  * Message sent between peers asking for FS-content.
2041  */
2042 struct GetMessage
2043 {
2044
2045   /**
2046    * Message type will be GNUNET_MESSAGE_TYPE_FS_GET.
2047    */
2048   struct GNUNET_MessageHeader header;
2049
2050   /**
2051    * Type of the query (block type).
2052    */
2053   uint32_t type GNUNET_PACKED;
2054
2055   /**
2056    * How important is this request (network byte order)
2057    */
2058   uint32_t priority GNUNET_PACKED;
2059
2060   /**
2061    * Relative time to live in MILLISECONDS (network byte order)
2062    */
2063   int32_t ttl GNUNET_PACKED;
2064
2065   /**
2066    * The content hash should be mutated using this value
2067    * before checking against the bloomfilter (used to
2068    * get many different filters for the same hash codes).
2069    * The number should be in big-endian format when used
2070    * for mingling.
2071    */
2072   int32_t filter_mutator GNUNET_PACKED;
2073
2074   /**
2075    * Which of the optional hash codes are present at the end of the
2076    * message?  See GET_MESSAGE_BIT_xx constants.  For each bit that is
2077    * set, an additional GNUNET_HashCode with the respective content
2078    * (in order of the bits) will be appended to the end of the GET
2079    * message.
2080    */
2081   uint32_t hash_bitmap GNUNET_PACKED;
2082
2083   /**
2084    * Hashcodes of the file(s) we're looking for.
2085    * Details depend on the query type.
2086    */
2087   GNUNET_HashCode query GNUNET_PACKED;
2088
2089   /* this is followed by hash codes
2090      as specified in the  "hash_bitmap";
2091      after that, an optional bloomfilter
2092      (with bits set for replies that should
2093      be suppressed) can be present */
2094 };
2095
2096
2097 /**
2098  * Response from FS service with a result for a previous FS search.
2099  * Note that queries for DBLOCKS and IBLOCKS that have received a
2100  * single response are considered done.  This message is transmitted
2101  * between peers as well as between the service and a client.
2102  */
2103 struct PutMessage
2104 {
2105
2106   /**
2107    * Message type will be GNUNET_MESSAGE_TYPE_FS_PUT.
2108    */
2109   struct GNUNET_MessageHeader header;
2110
2111   /**
2112    * Type of the block (in big endian).  Should never be zero.
2113    */
2114   uint32_t type GNUNET_PACKED;
2115
2116   /**
2117    * When does this result expire? 
2118    */
2119   struct GNUNET_TIME_AbsoluteNBO expiration;
2120
2121   /* this is followed by the actual encrypted content */
2122
2123 };
2124
2125
2126 #endif
2127
2128 /* end of fs.h */