8859472950b0069d14e95efa3d9ec6818627df6f
[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 = (GSF_PRO_BLOOMFILTER_FULL_REFRESH | GSF_PRO_PRIORITY_UNLIMITED)
66
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    * Current TTL for the request.
96    */
97   struct GNUNET_TIME_Absolute ttl;
98
99   /**
100    * When did we start with the request.
101    */
102   struct GNUNET_TIME_Absolute start_time;
103
104   /**
105    * Desired anonymity level.
106    */
107   uint32_t anonymity_level;
108
109   /**
110    * Priority that this request (still) has for us.
111    */
112   uint32_t priority;
113
114   /**
115    * Priority that this request (originally) had for us.
116    */
117   uint32_t original_priority;
118
119   /**
120    * Options for the request.
121    */
122   enum GSF_PendingRequestOptions options;
123   
124   /**
125    * Type of the requested block.
126    */
127   enum GNUNET_BLOCK_Type type;
128
129   /**
130    * Number of results we have found for this request so far.
131    */
132   unsigned int results_found;
133
134   /**
135    * Is the 'target' value set to a valid peer identity?
136    */
137   int has_target;
138
139 };
140
141
142 /**
143  * Handle a reply to a pending request.  Also called if a request
144  * expires (then with data == NULL).  The handler may be called
145  * many times (depending on the request type), but will not be
146  * called during or after a call to GSF_pending_request_cancel 
147  * and will also not be called anymore after a call signalling
148  * expiration.
149  *
150  * @param cls user-specified closure
151  * @param pr handle to the original pending request
152  * @param expiration when does 'data' expire?
153  * @param data response data, NULL on request expiration
154  * @param data_len number of bytes in data
155  * @param more GNUNET_YES if the request remains active (may call
156  *             this function again), GNUNET_NO if the request is
157  *             finished (client must not call GSF_pending_request_cancel_)
158  */
159 typedef void (*GSF_PendingRequestReplyHandler)(void *cls,
160                                                struct GSF_PendingRequest *pr,
161                                                struct GNUNET_TIME_Absolute expiration,
162                                                const void *data,
163                                                size_t data_len,
164                                                int more);
165
166
167 /**
168  * Create a new pending request.  
169  *
170  * @param options request options
171  * @param type type of the block that is being requested
172  * @param query key for the lookup
173  * @param namespace namespace to lookup, NULL for no namespace
174  * @param target preferred target for the request, NULL for none
175  * @param bf_data raw data for bloom filter for known replies, can be NULL
176  * @param bf_size number of bytes in bf_data
177  * @param mingle mingle value for bf
178  * @param anonymity_level desired anonymity level
179  * @param priority maximum outgoing cummulative request priority to use
180  * @param ttl current time-to-live for the request
181  * @param sender_pid peer ID to use for the sender when forwarding, 0 for none;
182  *                   reference counter is taken over by this function
183  * @param replies_seen hash codes of known local replies
184  * @param replies_seen_count size of the 'replies_seen' array
185  * @param rh handle to call when we get a reply
186  * @param rh_cls closure for rh
187  * @return handle for the new pending request
188  */
189 struct GSF_PendingRequest *
190 GSF_pending_request_create_ (enum GSF_PendingRequestOptions options,
191                              enum GNUNET_BLOCK_Type type,
192                              const GNUNET_HashCode *query,
193                              const GNUNET_HashCode *namespace,
194                              const struct GNUNET_PeerIdentity *target,
195                              const char *bf_data,
196                              size_t bf_size,
197                              uint32_t mingle,
198                              uint32_t anonymity_level,
199                              uint32_t priority,
200                              int32_t ttl,
201                              GNUNET_PEER_Id sender_pid,
202                              const GNUNET_HashCode *replies_seen,
203                              unsigned int replies_seen_count,
204                              GSF_PendingRequestReplyHandler rh,
205                              void *rh_cls);
206
207
208 /**
209  * Update a given pending request with additional replies
210  * that have been seen.
211  *
212  * @param pr request to update
213  * @param replies_seen hash codes of replies that we've seen
214  * @param replies_seen_count size of the replies_seen array
215  */
216 void
217 GSF_pending_request_update_ (struct GSF_PendingRequest *pr,
218                              const GNUNET_HashCode *replies_seen,
219                              unsigned int replies_seen_count);
220
221
222 /**
223  * Obtain the public data associated with a pending request
224  * 
225  * @param pr pending request
226  * @return associated public data
227  */
228 struct GSF_PendingRequestData *
229 GSF_pending_request_get_data_ (struct GSF_PendingRequest *pr);
230
231
232 /**
233  * Generate the message corresponding to the given pending request for
234  * transmission to other peers (or at least determine its size).
235  *
236  * @param pr request to generate the message for
237  * @param do_route are we routing the reply
238  * @param buf_size number of bytes available in buf
239  * @param buf where to copy the message (can be NULL)
240  * @return number of bytes needed (if buf_size too small) or used
241  */
242 size_t
243 GSF_pending_request_get_message_ (struct GSF_PendingRequest *pr,
244                                   int do_route,
245                                   size_t buf_size,
246                                   void *buf);
247
248
249 /**
250  * Explicitly cancel a pending request.
251  *
252  * @param pr request to cancel
253  */
254 void
255 GSF_pending_request_cancel_ (struct GSF_PendingRequest *pr);
256
257
258 /**
259  * Signature of function called on each request.
260  * (Note: 'subtype' of GNUNET_CONTAINER_HashMapIterator).
261  *
262  * @param cls closure
263  * @param key query for the request
264  * @param pr handle to the pending request
265  * @return GNUNET_YES to continue to iterate
266  */
267 typedef int (*GSF_PendingRequestIterator)(void *cls,
268                                           const GNUNET_HashCode *key,
269                                           struct GSF_PendingRequest *pr);
270
271
272 /**
273  * Iterate over all pending requests.
274  *
275  * @param it function to call for each request
276  * @param cls closure for it
277  */
278 void
279 GSF_iterate_pending_requests_ (GSF_PendingRequestIterator it,
280                                void *cls);
281
282
283 /**
284  * Handle P2P "CONTENT" message.  Checks that the message is
285  * well-formed and then checks if there are any pending requests for
286  * this content and possibly passes it on (to local clients or other
287  * peers).  Does NOT perform migration (content caching at this peer).
288  *
289  * @param cp the other peer involved (sender or receiver, NULL
290  *        for loopback messages where we are both sender and receiver)
291  * @param message the actual message
292  * @return GNUNET_OK if the message was well-formed,
293  *         GNUNET_SYSERR if the message was malformed (close connection,
294  *         do not cache under any circumstances)
295  */
296 int
297 GSF_handle_p2p_content_ (struct GSF_ConnectedPeer *cp,
298                          const struct GNUNET_MessageHeader *message);
299
300
301 /**
302  * Consider looking up the data in the DHT (anonymity-level permitting).
303  *
304  * @param pr the pending request to process
305  */
306 void
307 GSF_dht_lookup_ (struct GSF_PendingRequest *pr);
308
309
310 /**
311  * Function to be called after we're done processing
312  * replies from the local lookup.
313  *
314  * @param cls closure
315  * @param pr the pending request we were processing
316  * @param result final datastore lookup result
317  */
318 typedef void (*GSF_LocalLookupContinuation)(void *cls,
319                                             struct GSF_PendingRequest *pr,
320                                             enum GNUNET_BLOCK_EvaluationResult result);
321
322
323 /**
324  * Look up the request in the local datastore.
325  *
326  * @param pr the pending request to process
327  * @param cont function to call at the end
328  * @param cont_cls closure for cont
329  */
330 void
331 GSF_local_lookup_ (struct GSF_PendingRequest *pr,
332                    GSF_LocalLookupContinuation cont,
333                    void *cont_cls);
334
335
336 /**
337  * Setup the subsystem.
338  *
339  * @param cfg configuration to use
340  */
341 void
342 GSF_pending_request_init_ (void);
343
344
345 /**
346  * Shutdown the subsystem.
347  */
348 void
349 GSF_pending_request_done_ (void);
350
351
352 #endif
353 /* end of gnunet-service-fs_pr.h */