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