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