gnunet-service-fs_pr.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file fs/gnunet-service-fs_pr.h
  19. * @brief API to handle pending requests
  20. * @author Christian Grothoff
  21. */
  22. #ifndef GNUNET_SERVICE_FS_PR_H
  23. #define GNUNET_SERVICE_FS_PR_H
  24. #include "gnunet-service-fs.h"
  25. /**
  26. * Options for pending requests (bits to be ORed).
  27. */
  28. enum GSF_PendingRequestOptions
  29. {
  30. /**
  31. * No special options (P2P-default).
  32. */
  33. GSF_PRO_DEFAULTS = 0,
  34. /**
  35. * Request must only be processed locally.
  36. */
  37. GSF_PRO_LOCAL_ONLY = 1,
  38. /**
  39. * Request must only be forwarded (no routing)
  40. */
  41. GSF_PRO_FORWARD_ONLY = 2,
  42. /**
  43. * Request persists indefinitely (no expiration).
  44. */
  45. GSF_PRO_REQUEST_NEVER_EXPIRES = 4,
  46. /**
  47. * Request is allowed to refresh bloomfilter and change mingle value.
  48. */
  49. GSF_PRO_BLOOMFILTER_FULL_REFRESH = 8,
  50. /**
  51. * Request priority is allowed to be exceeded.
  52. */
  53. GSF_PRO_PRIORITY_UNLIMITED = 16,
  54. /**
  55. * Option mask for typical local requests.
  56. */
  57. GSF_PRO_LOCAL_REQUEST =
  58. (GSF_PRO_BLOOMFILTER_FULL_REFRESH | GSF_PRO_PRIORITY_UNLIMITED | GSF_PRO_REQUEST_NEVER_EXPIRES)
  59. };
  60. /**
  61. * Public data (in the sense of not encapsulated within
  62. * 'gnunet-service-fs_pr', not in the sense of network-wide
  63. * known) associated with each pending request.
  64. */
  65. struct GSF_PendingRequestData
  66. {
  67. /**
  68. * Primary query hash for this request.
  69. */
  70. struct GNUNET_HashCode query;
  71. /**
  72. * Identity of a peer hosting the content, otherwise NULl.
  73. * Allocated after struct only if needed. Do not free!
  74. */
  75. const struct GNUNET_PeerIdentity *target;
  76. /**
  77. * Fields for the plan module to track a DLL with the request.
  78. */
  79. struct GSF_PendingRequestPlanBijection *pr_head;
  80. /**
  81. * Fields for the plan module to track a DLL with the request.
  82. */
  83. struct GSF_PendingRequestPlanBijection *pr_tail;
  84. /**
  85. * Current TTL for the request.
  86. */
  87. struct GNUNET_TIME_Absolute ttl;
  88. /**
  89. * When did we start with the request.
  90. */
  91. struct GNUNET_TIME_Absolute start_time;
  92. /**
  93. * Desired anonymity level.
  94. */
  95. uint32_t anonymity_level;
  96. /**
  97. * Priority that this request (still) has for us.
  98. */
  99. uint32_t priority;
  100. /**
  101. * Priority that this request (originally) had for us.
  102. */
  103. uint32_t original_priority;
  104. /**
  105. * Counter for how often this request has been transmitted (estimate,
  106. * because we might have the same request pending for multiple clients,
  107. * and of course because a transmission may have failed at a lower
  108. * layer).
  109. */
  110. uint32_t num_transmissions;
  111. /**
  112. * How much respect did we (in total) offer for this request so far (estimate,
  113. * because we might have the same request pending for multiple clients,
  114. * and of course because a transmission may have failed at a lower
  115. * layer).
  116. */
  117. uint32_t respect_offered;
  118. /**
  119. * Options for the request.
  120. */
  121. enum GSF_PendingRequestOptions options;
  122. /**
  123. * Type of the requested block.
  124. */
  125. enum GNUNET_BLOCK_Type type;
  126. /**
  127. * Number of results we have found for this request so far.
  128. */
  129. unsigned int results_found;
  130. /**
  131. * Has this request been started yet (local/p2p operations)? Or are
  132. * we still constructing it?
  133. */
  134. int has_started;
  135. };
  136. /**
  137. * Handle a reply to a pending request. Also called if a request
  138. * expires (then with data == NULL). The handler may be called
  139. * many times (depending on the request type), but will not be
  140. * called during or after a call to GSF_pending_request_cancel
  141. * and will also not be called anymore after a call signalling
  142. * expiration.
  143. *
  144. * @param cls user-specified closure
  145. * @param eval evaluation of the result
  146. * @param pr handle to the original pending request
  147. * @param reply_anonymity_level anonymity level for the reply, UINT32_MAX for "unknown"
  148. * @param expiration when does 'data' expire?
  149. * @param last_transmission the last time we've tried to get this block (FOREVER if unknown)
  150. * @param type type of the block
  151. * @param data response data, NULL on request expiration
  152. * @param data_len number of bytes in data
  153. */
  154. typedef void (*GSF_PendingRequestReplyHandler) (void *cls,
  155. enum
  156. GNUNET_BLOCK_EvaluationResult
  157. eval,
  158. struct GSF_PendingRequest * pr,
  159. uint32_t reply_anonymity_level,
  160. struct GNUNET_TIME_Absolute
  161. expiration,
  162. struct GNUNET_TIME_Absolute
  163. last_transmission,
  164. enum GNUNET_BLOCK_Type type,
  165. const void *data,
  166. size_t data_len);
  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 target preferred target for the request, NULL for none
  174. * @param bf_data raw data for bloom filter for known replies, can be NULL
  175. * @param bf_size number of bytes in bf_data
  176. * @param mingle mingle value for bf
  177. * @param anonymity_level desired anonymity level
  178. * @param priority maximum outgoing cummulative request priority to use
  179. * @param ttl current time-to-live for the request
  180. * @param sender_pid peer ID to use for the sender when forwarding, 0 for none;
  181. * reference counter is taken over by this function
  182. * @param origin_pid peer ID of origin of query (do not loop back)
  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 struct GNUNET_HashCode * query,
  193. const struct GNUNET_PeerIdentity *target,
  194. const char *bf_data, size_t bf_size,
  195. uint32_t mingle, uint32_t anonymity_level,
  196. uint32_t priority, int32_t ttl,
  197. GNUNET_PEER_Id sender_pid,
  198. GNUNET_PEER_Id origin_pid,
  199. const struct GNUNET_HashCode * replies_seen,
  200. unsigned int replies_seen_count,
  201. GSF_PendingRequestReplyHandler rh, void *rh_cls);
  202. /**
  203. * Update a given pending request with additional replies
  204. * that have been seen.
  205. *
  206. * @param pr request to update
  207. * @param replies_seen hash codes of replies that we've seen
  208. * @param replies_seen_count size of the replies_seen array
  209. */
  210. void
  211. GSF_pending_request_update_ (struct GSF_PendingRequest *pr,
  212. const struct GNUNET_HashCode * replies_seen,
  213. unsigned int replies_seen_count);
  214. /**
  215. * Obtain the public data associated with a pending request
  216. *
  217. * @param pr pending request
  218. * @return associated public data
  219. */
  220. struct GSF_PendingRequestData *
  221. GSF_pending_request_get_data_ (struct GSF_PendingRequest *pr);
  222. /**
  223. * Test if two pending requests are compatible (would generate
  224. * the same query modulo filters and should thus be processed
  225. * jointly).
  226. *
  227. * @param pra a pending request
  228. * @param prb another pending request
  229. * @return GNUNET_OK if the requests are compatible
  230. */
  231. int
  232. GSF_pending_request_is_compatible_ (struct GSF_PendingRequest *pra,
  233. struct GSF_PendingRequest *prb);
  234. /**
  235. * Generate the message corresponding to the given pending request for
  236. * transmission to other peers (or at least determine its size).
  237. *
  238. * @param pr request to generate the message for
  239. * @param buf_size number of bytes available in buf
  240. * @param buf where to copy the message (can be NULL)
  241. * @return number of bytes needed (if buf_size too small) or used
  242. */
  243. size_t
  244. GSF_pending_request_get_message_ (struct GSF_PendingRequest *pr,
  245. size_t buf_size, void *buf);
  246. /**
  247. * Explicitly cancel a pending request.
  248. *
  249. * @param pr request to cancel
  250. * @param full_cleanup fully purge the request
  251. */
  252. void
  253. GSF_pending_request_cancel_ (struct GSF_PendingRequest *pr, int full_cleanup);
  254. /**
  255. * Signature of function called on each request.
  256. * (Note: 'subtype' of GNUNET_CONTAINER_HashMapIterator).
  257. *
  258. * @param cls closure
  259. * @param key query for the request
  260. * @param pr handle to the pending request
  261. * @return GNUNET_YES to continue to iterate
  262. */
  263. typedef int (*GSF_PendingRequestIterator) (void *cls,
  264. const struct GNUNET_HashCode * key,
  265. struct GSF_PendingRequest * pr);
  266. /**
  267. * Iterate over all pending requests.
  268. *
  269. * @param it function to call for each request
  270. * @param cls closure for it
  271. */
  272. void
  273. GSF_iterate_pending_requests_ (GSF_PendingRequestIterator it, void *cls);
  274. /**
  275. * Handle P2P "CONTENT" message. Checks that the message is
  276. * well-formed and then checks if there are any pending requests for
  277. * this content and possibly passes it on (to local clients or other
  278. * peers). Does NOT perform migration (content caching at this peer).
  279. *
  280. * @param cp the other peer involved (sender or receiver, NULL
  281. * for loopback messages where we are both sender and receiver)
  282. * @param message the actual message
  283. * @return GNUNET_OK if the message was well-formed,
  284. * GNUNET_SYSERR if the message was malformed (close connection,
  285. * do not cache under any circumstances)
  286. */
  287. int
  288. GSF_handle_p2p_content_ (struct GSF_ConnectedPeer *cp,
  289. const struct GNUNET_MessageHeader *message);
  290. /**
  291. * Consider looking up the data in the DHT (anonymity-level permitting).
  292. *
  293. * @param pr the pending request to process
  294. */
  295. void
  296. GSF_dht_lookup_ (struct GSF_PendingRequest *pr);
  297. /**
  298. * Consider downloading via cadet (if possible)
  299. *
  300. * @param pr the pending request to process
  301. */
  302. void
  303. GSF_cadet_lookup_ (struct GSF_PendingRequest *pr);
  304. /**
  305. * Function to be called after we're done processing
  306. * replies from the local lookup.
  307. *
  308. * @param cls closure
  309. * @param pr the pending request we were processing
  310. * @param result final datastore lookup result
  311. */
  312. typedef void (*GSF_LocalLookupContinuation) (void *cls,
  313. struct GSF_PendingRequest * pr,
  314. enum GNUNET_BLOCK_EvaluationResult
  315. result);
  316. /**
  317. * Look up the request in the local datastore.
  318. *
  319. * @param pr the pending request to process
  320. * @param cont function to call at the end
  321. * @param cont_cls closure for cont
  322. */
  323. void
  324. GSF_local_lookup_ (struct GSF_PendingRequest *pr,
  325. GSF_LocalLookupContinuation cont, void *cont_cls);
  326. /**
  327. * Is the given target a legitimate peer for forwarding the given request?
  328. *
  329. * @param pr request
  330. * @param target
  331. * @return GNUNET_YES if this request could be forwarded to the given peer
  332. */
  333. int
  334. GSF_pending_request_test_target_ (struct GSF_PendingRequest *pr,
  335. const struct GNUNET_PeerIdentity *target);
  336. /**
  337. * Setup the subsystem.
  338. */
  339. void
  340. GSF_pending_request_init_ (void);
  341. /**
  342. * Shutdown the subsystem.
  343. */
  344. void
  345. GSF_pending_request_done_ (void);
  346. #endif
  347. /* end of gnunet-service-fs_pr.h */