2 This file is part of GNUnet.
3 (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file fs/gnunet-service-fs_pr.h
23 * @brief API to handle pending requests
24 * @author Christian Grothoff
26 #ifndef GNUNET_SERVICE_FS_PR_H
27 #define GNUNET_SERVICE_FS_PR_H
29 #include "gnunet-service-fs.h"
33 * Options for pending requests (bits to be ORed).
35 enum GSF_PendingRequestOptions
39 * No special options (P2P-default).
44 * Request must only be processed locally.
46 GSF_PRO_LOCAL_ONLY = 1,
49 * Request must only be forwarded (no routing)
51 GSF_PRO_FORWARD_ONLY = 2,
54 * Request persists indefinitely (no expiration).
56 GSF_PRO_REQUEST_NEVER_EXPIRES = 4,
59 * Request is allowed to refresh bloomfilter and change mingle value.
61 GSF_PRO_BLOOMFILTER_FULL_REFRESH = 8,
64 * Request priority is allowed to be exceeded.
66 GSF_PRO_PRIORITY_UNLIMITED = 16,
69 * Option mask for typical local requests.
71 GSF_PRO_LOCAL_REQUEST =
72 (GSF_PRO_BLOOMFILTER_FULL_REFRESH | GSF_PRO_PRIORITY_UNLIMITED | GSF_PRO_REQUEST_NEVER_EXPIRES)
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.
81 struct GSF_PendingRequestData
85 * Primary query hash for this request.
87 struct GNUNET_HashCode query;
90 * Identity of a peer hosting the content, otherwise NULl.
91 * Allocated after struct only if needed. Do not free!
93 const struct GNUNET_PeerIdentity *target;
96 * Fields for the plan module to track a DLL with the request.
98 struct GSF_PendingRequestPlanBijection *pr_head;
101 * Fields for the plan module to track a DLL with the request.
103 struct GSF_PendingRequestPlanBijection *pr_tail;
106 * Current TTL for the request.
108 struct GNUNET_TIME_Absolute ttl;
111 * When did we start with the request.
113 struct GNUNET_TIME_Absolute start_time;
116 * Desired anonymity level.
118 uint32_t anonymity_level;
121 * Priority that this request (still) has for us.
126 * Priority that this request (originally) had for us.
128 uint32_t original_priority;
131 * Counter for how often this request has been transmitted (estimate,
132 * because we might have the same request pending for multiple clients,
133 * and of course because a transmission may have failed at a lower
136 uint32_t num_transmissions;
139 * How much respect did we (in total) offer for this request so far (estimate,
140 * because we might have the same request pending for multiple clients,
141 * and of course because a transmission may have failed at a lower
144 uint32_t respect_offered;
147 * Options for the request.
149 enum GSF_PendingRequestOptions options;
152 * Type of the requested block.
154 enum GNUNET_BLOCK_Type type;
157 * Number of results we have found for this request so far.
159 unsigned int results_found;
162 * Has this request been started yet (local/p2p operations)? Or are
163 * we still constructing it?
171 * Handle a reply to a pending request. Also called if a request
172 * expires (then with data == NULL). The handler may be called
173 * many times (depending on the request type), but will not be
174 * called during or after a call to GSF_pending_request_cancel
175 * and will also not be called anymore after a call signalling
178 * @param cls user-specified closure
179 * @param eval evaluation of the result
180 * @param pr handle to the original pending request
181 * @param reply_anonymity_level anonymity level for the reply, UINT32_MAX for "unknown"
182 * @param expiration when does 'data' expire?
183 * @param last_transmission the last time we've tried to get this block (FOREVER if unknown)
184 * @param type type of the block
185 * @param data response data, NULL on request expiration
186 * @param data_len number of bytes in data
188 typedef void (*GSF_PendingRequestReplyHandler) (void *cls,
190 GNUNET_BLOCK_EvaluationResult
192 struct GSF_PendingRequest * pr,
193 uint32_t reply_anonymity_level,
194 struct GNUNET_TIME_Absolute
196 struct GNUNET_TIME_Absolute
198 enum GNUNET_BLOCK_Type type,
204 * Create a new pending request.
206 * @param options request options
207 * @param type type of the block that is being requested
208 * @param query key for the lookup
209 * @param target preferred target for the request, NULL for none
210 * @param bf_data raw data for bloom filter for known replies, can be NULL
211 * @param bf_size number of bytes in bf_data
212 * @param mingle mingle value for bf
213 * @param anonymity_level desired anonymity level
214 * @param priority maximum outgoing cummulative request priority to use
215 * @param ttl current time-to-live for the request
216 * @param sender_pid peer ID to use for the sender when forwarding, 0 for none;
217 * reference counter is taken over by this function
218 * @param origin_pid peer ID of origin of query (do not loop back)
219 * @param replies_seen hash codes of known local replies
220 * @param replies_seen_count size of the 'replies_seen' array
221 * @param rh handle to call when we get a reply
222 * @param rh_cls closure for rh
223 * @return handle for the new pending request
225 struct GSF_PendingRequest *
226 GSF_pending_request_create_ (enum GSF_PendingRequestOptions options,
227 enum GNUNET_BLOCK_Type type,
228 const struct GNUNET_HashCode * query,
229 const struct GNUNET_PeerIdentity *target,
230 const char *bf_data, size_t bf_size,
231 uint32_t mingle, uint32_t anonymity_level,
232 uint32_t priority, int32_t ttl,
233 GNUNET_PEER_Id sender_pid,
234 GNUNET_PEER_Id origin_pid,
235 const struct GNUNET_HashCode * replies_seen,
236 unsigned int replies_seen_count,
237 GSF_PendingRequestReplyHandler rh, void *rh_cls);
241 * Update a given pending request with additional replies
242 * that have been seen.
244 * @param pr request to update
245 * @param replies_seen hash codes of replies that we've seen
246 * @param replies_seen_count size of the replies_seen array
249 GSF_pending_request_update_ (struct GSF_PendingRequest *pr,
250 const struct GNUNET_HashCode * replies_seen,
251 unsigned int replies_seen_count);
255 * Obtain the public data associated with a pending request
257 * @param pr pending request
258 * @return associated public data
260 struct GSF_PendingRequestData *
261 GSF_pending_request_get_data_ (struct GSF_PendingRequest *pr);
265 * Test if two pending requests are compatible (would generate
266 * the same query modulo filters and should thus be processed
269 * @param pra a pending request
270 * @param prb another pending request
271 * @return GNUNET_OK if the requests are compatible
274 GSF_pending_request_is_compatible_ (struct GSF_PendingRequest *pra,
275 struct GSF_PendingRequest *prb);
279 * Generate the message corresponding to the given pending request for
280 * transmission to other peers (or at least determine its size).
282 * @param pr request to generate the message for
283 * @param buf_size number of bytes available in buf
284 * @param buf where to copy the message (can be NULL)
285 * @return number of bytes needed (if buf_size too small) or used
288 GSF_pending_request_get_message_ (struct GSF_PendingRequest *pr,
289 size_t buf_size, void *buf);
293 * Explicitly cancel a pending request.
295 * @param pr request to cancel
296 * @param full_cleanup fully purge the request
299 GSF_pending_request_cancel_ (struct GSF_PendingRequest *pr, int full_cleanup);
303 * Signature of function called on each request.
304 * (Note: 'subtype' of GNUNET_CONTAINER_HashMapIterator).
307 * @param key query for the request
308 * @param pr handle to the pending request
309 * @return GNUNET_YES to continue to iterate
311 typedef int (*GSF_PendingRequestIterator) (void *cls,
312 const struct GNUNET_HashCode * key,
313 struct GSF_PendingRequest * pr);
317 * Iterate over all pending requests.
319 * @param it function to call for each request
320 * @param cls closure for it
323 GSF_iterate_pending_requests_ (GSF_PendingRequestIterator it, void *cls);
327 * Handle P2P "CONTENT" message. Checks that the message is
328 * well-formed and then checks if there are any pending requests for
329 * this content and possibly passes it on (to local clients or other
330 * peers). Does NOT perform migration (content caching at this peer).
332 * @param cp the other peer involved (sender or receiver, NULL
333 * for loopback messages where we are both sender and receiver)
334 * @param message the actual message
335 * @return GNUNET_OK if the message was well-formed,
336 * GNUNET_SYSERR if the message was malformed (close connection,
337 * do not cache under any circumstances)
340 GSF_handle_p2p_content_ (struct GSF_ConnectedPeer *cp,
341 const struct GNUNET_MessageHeader *message);
345 * Consider looking up the data in the DHT (anonymity-level permitting).
347 * @param pr the pending request to process
350 GSF_dht_lookup_ (struct GSF_PendingRequest *pr);
354 * Consider downloading via stream (if possible)
356 * @param pr the pending request to process
359 GSF_stream_lookup_ (struct GSF_PendingRequest *pr);
363 * Function to be called after we're done processing
364 * replies from the local lookup.
367 * @param pr the pending request we were processing
368 * @param result final datastore lookup result
370 typedef void (*GSF_LocalLookupContinuation) (void *cls,
371 struct GSF_PendingRequest * pr,
372 enum GNUNET_BLOCK_EvaluationResult
377 * Look up the request in the local datastore.
379 * @param pr the pending request to process
380 * @param cont function to call at the end
381 * @param cont_cls closure for cont
384 GSF_local_lookup_ (struct GSF_PendingRequest *pr,
385 GSF_LocalLookupContinuation cont, void *cont_cls);
389 * Is the given target a legitimate peer for forwarding the given request?
393 * @return GNUNET_YES if this request could be forwarded to the given peer
396 GSF_pending_request_test_target_ (struct GSF_PendingRequest *pr,
397 const struct GNUNET_PeerIdentity *target);
402 * Setup the subsystem.
405 GSF_pending_request_init_ (void);
409 * Shutdown the subsystem.
412 GSF_pending_request_done_ (void);
416 /* end of gnunet-service-fs_pr.h */