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