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