src: for every AGPL3.0 file, add SPDX identifier.
[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 Affero 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      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
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.
292  *
293  * @param pr request to generate the message for
294  * @return envelope with the request message
295  */
296 struct GNUNET_MQ_Envelope *
297 GSF_pending_request_get_message_ (struct GSF_PendingRequest *pr);
298
299
300 /**
301  * Explicitly cancel a pending request.
302  *
303  * @param pr request to cancel
304  * @param full_cleanup fully purge the request
305  */
306 void
307 GSF_pending_request_cancel_ (struct GSF_PendingRequest *pr,
308                              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
321 (*GSF_PendingRequestIterator) (void *cls,
322                                const struct GNUNET_HashCode *key,
323                                struct GSF_PendingRequest *pr);
324
325
326 /**
327  * Iterate over all pending requests.
328  *
329  * @param it function to call for each request
330  * @param cls closure for it
331  */
332 void
333 GSF_iterate_pending_requests_ (GSF_PendingRequestIterator it,
334                                void *cls);
335
336
337 /**
338  * Handle P2P "CONTENT" message.  Checks that the message is
339  * well-formed and then checks if there are any pending requests for
340  * this content and possibly passes it on (to local clients or other
341  * peers).  Does NOT perform migration (content caching at this peer).
342  *
343  * @param cls the other peer involved (sender)
344  * @param put the actual message
345  */
346 void
347 handle_p2p_put (void *cls,
348                 const struct PutMessage *put);
349
350
351 /**
352  * Consider looking up the data in the DHT (anonymity-level permitting).
353  *
354  * @param pr the pending request to process
355  */
356 void
357 GSF_dht_lookup_ (struct GSF_PendingRequest *pr);
358
359
360 /**
361  * Consider downloading via cadet (if possible)
362  *
363  * @param pr the pending request to process
364  */
365 void
366 GSF_cadet_lookup_ (struct GSF_PendingRequest *pr);
367
368
369 /**
370  * Function to be called after we're done processing
371  * replies from the local lookup.
372  *
373  * @param cls closure
374  * @param pr the pending request we were processing
375  * @param result final datastore lookup result
376  */
377 typedef void
378 (*GSF_LocalLookupContinuation) (void *cls,
379                                 struct GSF_PendingRequest *pr,
380                                 enum GNUNET_BLOCK_EvaluationResult result);
381
382
383 /**
384  * Look up the request in the local datastore.
385  *
386  * @param pr the pending request to process
387  * @param cont function to call at the end
388  * @param cont_cls closure for @a cont
389  */
390 void
391 GSF_local_lookup_ (struct GSF_PendingRequest *pr,
392                    GSF_LocalLookupContinuation cont,
393                    void *cont_cls);
394
395
396 /**
397  * Is the given target a legitimate peer for forwarding the given request?
398  *
399  * @param pr request
400  * @param target
401  * @return #GNUNET_YES if this request could be forwarded to the given peer
402  */
403 int
404 GSF_pending_request_test_target_ (struct GSF_PendingRequest *pr,
405                                   const struct GNUNET_PeerIdentity *target);
406
407
408
409 /**
410  * Setup the subsystem.
411  */
412 void
413 GSF_pending_request_init_ (void);
414
415
416 /**
417  * Shutdown the subsystem.
418  */
419 void
420 GSF_pending_request_done_ (void);
421
422
423 #endif
424 /* end of gnunet-service-fs_pr.h */