-only trigger check config if we actually need it
[oweals/gnunet.git] / src / fs / gnunet-service-fs_pr.h
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2010, 2011 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20
21 /**
22  * @file fs/gnunet-service-fs_pr.h
23  * @brief API to handle pending requests
24  * @author Christian Grothoff
25  */
26 #ifndef GNUNET_SERVICE_FS_PR_H
27 #define GNUNET_SERVICE_FS_PR_H
28
29 #include "gnunet-service-fs.h"
30
31
32 /**
33  * Options for pending requests (bits to be ORed).
34  */
35 enum GSF_PendingRequestOptions
36 {
37
38   /**
39    * No special options (P2P-default).
40    */
41   GSF_PRO_DEFAULTS = 0,
42
43     /**
44      * Request must only be processed locally.
45      */
46   GSF_PRO_LOCAL_ONLY = 1,
47
48     /**
49      * Request must only be forwarded (no routing)
50      */
51   GSF_PRO_FORWARD_ONLY = 2,
52
53     /**
54      * Request persists indefinitely (no expiration).
55      */
56   GSF_PRO_REQUEST_NEVER_EXPIRES = 4,
57
58     /**
59      * Request is allowed to refresh bloomfilter and change mingle value.
60      */
61   GSF_PRO_BLOOMFILTER_FULL_REFRESH = 8,
62
63     /**
64      * Request priority is allowed to be exceeded.
65      */
66   GSF_PRO_PRIORITY_UNLIMITED = 16,
67
68     /**
69      * Option mask for typical local requests.
70      */
71   GSF_PRO_LOCAL_REQUEST =
72       (GSF_PRO_BLOOMFILTER_FULL_REFRESH | GSF_PRO_PRIORITY_UNLIMITED | GSF_PRO_REQUEST_NEVER_EXPIRES)
73 };
74
75
76 /**
77  * Public data (in the sense of not encapsulated within
78  * 'gnunet-service-fs_pr', not in the sense of network-wide
79  * known) associated with each pending request.
80  */
81 struct GSF_PendingRequestData
82 {
83
84   /**
85    * Primary query hash for this request.
86    */
87   struct GNUNET_HashCode query;
88
89   /**
90    * Identity of a peer hosting the content, otherwise NULl.
91    * Allocated after struct only if needed. Do not free!
92    */
93   const struct GNUNET_PeerIdentity *target;
94
95   /**
96    * Fields for the plan module to track a DLL with the request.
97    */
98   struct GSF_PendingRequestPlanBijection *pr_head;
99
100   /**
101    * Fields for the plan module to track a DLL with the request.
102    */
103   struct GSF_PendingRequestPlanBijection *pr_tail;
104
105   /**
106    * Current TTL for the request.
107    */
108   struct GNUNET_TIME_Absolute ttl;
109
110   /**
111    * When did we start with the request.
112    */
113   struct GNUNET_TIME_Absolute start_time;
114
115   /**
116    * Desired anonymity level.
117    */
118   uint32_t anonymity_level;
119
120   /**
121    * Priority that this request (still) has for us.
122    */
123   uint32_t priority;
124
125   /**
126    * Priority that this request (originally) had for us.
127    */
128   uint32_t original_priority;
129
130   /**
131    * Counter for how often this request has been transmitted (estimate,
132    * because we might have the same request pending for multiple clients,
133    * and of course because a transmission may have failed at a lower
134    * layer).
135    */
136   uint32_t num_transmissions;
137
138   /**
139    * How much respect did we (in total) offer for this request so far (estimate,
140    * because we might have the same request pending for multiple clients,
141    * and of course because a transmission may have failed at a lower
142    * layer).
143    */
144   uint32_t respect_offered;
145
146   /**
147    * Options for the request.
148    */
149   enum GSF_PendingRequestOptions options;
150
151   /**
152    * Type of the requested block.
153    */
154   enum GNUNET_BLOCK_Type type;
155
156   /**
157    * Number of results we have found for this request so far.
158    */
159   unsigned int results_found;
160
161   /**
162    * Has this request been started yet (local/p2p operations)?  Or are
163    * we still constructing it?
164    */
165   int has_started;
166
167 };
168
169
170 /**
171  * Handle a reply to a pending request.  Also called if a request
172  * expires (then with data == NULL).  The handler may be called
173  * many times (depending on the request type), but will not be
174  * called during or after a call to GSF_pending_request_cancel
175  * and will also not be called anymore after a call signalling
176  * expiration.
177  *
178  * @param cls user-specified closure
179  * @param eval evaluation of the result
180  * @param pr handle to the original pending request
181  * @param reply_anonymity_level anonymity level for the reply, UINT32_MAX for "unknown"
182  * @param expiration when does @a data expire?
183  * @param last_transmission the last time we've tried to get this block (FOREVER if unknown)
184  * @param type type of the block
185  * @param data response data, NULL on request expiration
186  * @param data_len number of bytes in @a data
187  */
188 typedef void
189 (*GSF_PendingRequestReplyHandler) (void *cls,
190                                    enum GNUNET_BLOCK_EvaluationResult eval,
191                                    struct GSF_PendingRequest *pr,
192                                    uint32_t reply_anonymity_level,
193                                    struct GNUNET_TIME_Absolute expiration,
194                                    struct GNUNET_TIME_Absolute last_transmission,
195                                    enum GNUNET_BLOCK_Type type,
196                                    const void *data,
197                                    size_t data_len);
198
199
200 /**
201  * Create a new pending request.
202  *
203  * @param options request options
204  * @param type type of the block that is being requested
205  * @param query key for the lookup
206  * @param target preferred target for the request, NULL for none
207  * @param bf_data raw data for bloom filter for known replies, can be NULL
208  * @param bf_size number of bytes in bf_data
209  * @param mingle mingle value for bf
210  * @param anonymity_level desired anonymity level
211  * @param priority maximum outgoing cummulative request priority to use
212  * @param ttl current time-to-live for the request
213  * @param sender_pid peer ID to use for the sender when forwarding, 0 for none;
214  *                   reference counter is taken over by this function
215  * @param origin_pid peer ID of origin of query (do not loop back)
216  * @param replies_seen hash codes of known local replies
217  * @param replies_seen_count size of the 'replies_seen' array
218  * @param rh handle to call when we get a reply
219  * @param rh_cls closure for rh
220  * @return handle for the new pending request
221  */
222 struct GSF_PendingRequest *
223 GSF_pending_request_create_ (enum GSF_PendingRequestOptions options,
224                              enum GNUNET_BLOCK_Type type,
225                              const struct GNUNET_HashCode *query,
226                              const struct GNUNET_PeerIdentity *target,
227                              const char *bf_data,
228                              size_t bf_size,
229                              uint32_t mingle,
230                              uint32_t anonymity_level,
231                              uint32_t priority,
232                              int32_t ttl,
233                              GNUNET_PEER_Id sender_pid,
234                              GNUNET_PEER_Id origin_pid,
235                              const struct GNUNET_HashCode *replies_seen,
236                              unsigned int replies_seen_count,
237                              GSF_PendingRequestReplyHandler rh,
238                              void *rh_cls);
239
240
241 /**
242  * Update a given pending request with additional replies
243  * that have been seen.
244  *
245  * @param pr request to update
246  * @param replies_seen hash codes of replies that we've seen
247  * @param replies_seen_count size of the @a replies_seen array
248  */
249 void
250 GSF_pending_request_update_ (struct GSF_PendingRequest *pr,
251                              const struct GNUNET_HashCode *replies_seen,
252                              unsigned int replies_seen_count);
253
254
255 /**
256  * Obtain the public data associated with a pending request
257  *
258  * @param pr pending request
259  * @return associated public data
260  */
261 struct GSF_PendingRequestData *
262 GSF_pending_request_get_data_ (struct GSF_PendingRequest *pr);
263
264
265 /**
266  * Check if the given request is still active.
267  *
268  * @param pr pending request
269  * @return #GNUNET_YES if the request is still active
270  */
271 int
272 GSF_pending_request_test_active_ (struct GSF_PendingRequest *pr);
273
274
275 /**
276  * Test if two pending requests are compatible (would generate
277  * the same query modulo filters and should thus be processed
278  * jointly).
279  *
280  * @param pra a pending request
281  * @param prb another pending request
282  * @return #GNUNET_OK if the requests are compatible
283  */
284 int
285 GSF_pending_request_is_compatible_ (struct GSF_PendingRequest *pra,
286                                     struct GSF_PendingRequest *prb);
287
288
289 /**
290  * Generate the message corresponding to the given pending request for
291  * transmission to other peers (or at least determine its size).
292  *
293  * @param pr request to generate the message for
294  * @param buf_size number of bytes available in @a buf
295  * @param buf where to copy the message (can be NULL)
296  * @return number of bytes needed (if @a buf_size too small) or used
297  */
298 size_t
299 GSF_pending_request_get_message_ (struct GSF_PendingRequest *pr,
300                                   size_t buf_size,
301                                   void *buf);
302
303
304 /**
305  * Explicitly cancel a pending request.
306  *
307  * @param pr request to cancel
308  * @param full_cleanup fully purge the request
309  */
310 void
311 GSF_pending_request_cancel_ (struct GSF_PendingRequest *pr,
312                              int full_cleanup);
313
314
315 /**
316  * Signature of function called on each request.
317  * (Note: 'subtype' of GNUNET_CONTAINER_HashMapIterator).
318  *
319  * @param cls closure
320  * @param key query for the request
321  * @param pr handle to the pending request
322  * @return #GNUNET_YES to continue to iterate
323  */
324 typedef int
325 (*GSF_PendingRequestIterator) (void *cls,
326                                const struct GNUNET_HashCode *key,
327                                struct GSF_PendingRequest *pr);
328
329
330 /**
331  * Iterate over all pending requests.
332  *
333  * @param it function to call for each request
334  * @param cls closure for it
335  */
336 void
337 GSF_iterate_pending_requests_ (GSF_PendingRequestIterator it,
338                                void *cls);
339
340
341 /**
342  * Handle P2P "CONTENT" message.  Checks that the message is
343  * well-formed and then checks if there are any pending requests for
344  * this content and possibly passes it on (to local clients or other
345  * peers).  Does NOT perform migration (content caching at this peer).
346  *
347  * @param cp the other peer involved (sender or receiver, NULL
348  *        for loopback messages where we are both sender and receiver)
349  * @param message the actual message
350  * @return #GNUNET_OK if the message was well-formed,
351  *         #GNUNET_SYSERR if the message was malformed (close connection,
352  *         do not cache under any circumstances)
353  */
354 int
355 GSF_handle_p2p_content_ (struct GSF_ConnectedPeer *cp,
356                          const struct GNUNET_MessageHeader *message);
357
358
359 /**
360  * Consider looking up the data in the DHT (anonymity-level permitting).
361  *
362  * @param pr the pending request to process
363  */
364 void
365 GSF_dht_lookup_ (struct GSF_PendingRequest *pr);
366
367
368 /**
369  * Consider downloading via cadet (if possible)
370  *
371  * @param pr the pending request to process
372  */
373 void
374 GSF_cadet_lookup_ (struct GSF_PendingRequest *pr);
375
376
377 /**
378  * Function to be called after we're done processing
379  * replies from the local lookup.
380  *
381  * @param cls closure
382  * @param pr the pending request we were processing
383  * @param result final datastore lookup result
384  */
385 typedef void
386 (*GSF_LocalLookupContinuation) (void *cls,
387                                 struct GSF_PendingRequest *pr,
388                                 enum GNUNET_BLOCK_EvaluationResult result);
389
390
391 /**
392  * Look up the request in the local datastore.
393  *
394  * @param pr the pending request to process
395  * @param cont function to call at the end
396  * @param cont_cls closure for @a cont
397  */
398 void
399 GSF_local_lookup_ (struct GSF_PendingRequest *pr,
400                    GSF_LocalLookupContinuation cont,
401                    void *cont_cls);
402
403
404 /**
405  * Is the given target a legitimate peer for forwarding the given request?
406  *
407  * @param pr request
408  * @param target
409  * @return #GNUNET_YES if this request could be forwarded to the given peer
410  */
411 int
412 GSF_pending_request_test_target_ (struct GSF_PendingRequest *pr,
413                                   const struct GNUNET_PeerIdentity *target);
414
415
416
417 /**
418  * Setup the subsystem.
419  */
420 void
421 GSF_pending_request_init_ (void);
422
423
424 /**
425  * Shutdown the subsystem.
426  */
427 void
428 GSF_pending_request_done_ (void);
429
430
431 #endif
432 /* end of gnunet-service-fs_pr.h */