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