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