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