gnunet-service-fs_pr.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009, 2010, 2011 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your 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. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file fs/gnunet-service-fs_pr.h
  18. * @brief API to handle pending requests
  19. * @author Christian Grothoff
  20. */
  21. #ifndef GNUNET_SERVICE_FS_PR_H
  22. #define GNUNET_SERVICE_FS_PR_H
  23. #include "gnunet-service-fs.h"
  24. /**
  25. * Options for pending requests (bits to be ORed).
  26. */
  27. enum GSF_PendingRequestOptions
  28. {
  29. /**
  30. * No special options (P2P-default).
  31. */
  32. GSF_PRO_DEFAULTS = 0,
  33. /**
  34. * Request must only be processed locally.
  35. */
  36. GSF_PRO_LOCAL_ONLY = 1,
  37. /**
  38. * Request must only be forwarded (no routing)
  39. */
  40. GSF_PRO_FORWARD_ONLY = 2,
  41. /**
  42. * Request persists indefinitely (no expiration).
  43. */
  44. GSF_PRO_REQUEST_NEVER_EXPIRES = 4,
  45. /**
  46. * Request is allowed to refresh bloomfilter and change mingle value.
  47. */
  48. GSF_PRO_BLOOMFILTER_FULL_REFRESH = 8,
  49. /**
  50. * Request priority is allowed to be exceeded.
  51. */
  52. GSF_PRO_PRIORITY_UNLIMITED = 16,
  53. /**
  54. * Option mask for typical local requests.
  55. */
  56. GSF_PRO_LOCAL_REQUEST =
  57. (GSF_PRO_BLOOMFILTER_FULL_REFRESH | GSF_PRO_PRIORITY_UNLIMITED
  58. | 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 @a 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 @a data
  153. */
  154. typedef void
  155. (*GSF_PendingRequestReplyHandler) (void *cls,
  156. enum GNUNET_BLOCK_EvaluationResult eval,
  157. struct GSF_PendingRequest *pr,
  158. uint32_t reply_anonymity_level,
  159. struct GNUNET_TIME_Absolute expiration,
  160. struct GNUNET_TIME_Absolute
  161. last_transmission,
  162. enum GNUNET_BLOCK_Type type,
  163. const void *data,
  164. size_t data_len);
  165. /**
  166. * Create a new pending request.
  167. *
  168. * @param options request options
  169. * @param type type of the block that is being requested
  170. * @param query key for the lookup
  171. * @param target preferred target for the request, NULL for none
  172. * @param bf_data raw data for bloom filter for known replies, can be NULL
  173. * @param bf_size number of bytes in bf_data
  174. * @param mingle mingle value for bf
  175. * @param anonymity_level desired anonymity level
  176. * @param priority maximum outgoing cummulative request priority to use
  177. * @param ttl current time-to-live for the request
  178. * @param sender_pid peer ID to use for the sender when forwarding, 0 for none;
  179. * reference counter is taken over by this function
  180. * @param origin_pid peer ID of origin of query (do not loop back)
  181. * @param replies_seen hash codes of known local replies
  182. * @param replies_seen_count size of the 'replies_seen' array
  183. * @param rh handle to call when we get a reply
  184. * @param rh_cls closure for rh
  185. * @return handle for the new pending request
  186. */
  187. struct GSF_PendingRequest *
  188. GSF_pending_request_create_ (enum GSF_PendingRequestOptions options,
  189. enum GNUNET_BLOCK_Type type,
  190. const struct GNUNET_HashCode *query,
  191. const struct GNUNET_PeerIdentity *target,
  192. const char *bf_data,
  193. size_t bf_size,
  194. uint32_t mingle,
  195. uint32_t anonymity_level,
  196. uint32_t priority,
  197. int32_t ttl,
  198. GNUNET_PEER_Id sender_pid,
  199. GNUNET_PEER_Id origin_pid,
  200. const struct GNUNET_HashCode *replies_seen,
  201. unsigned int replies_seen_count,
  202. GSF_PendingRequestReplyHandler rh,
  203. void *rh_cls);
  204. /**
  205. * Update a given pending request with additional replies
  206. * that have been seen.
  207. *
  208. * @param pr request to update
  209. * @param replies_seen hash codes of replies that we've seen
  210. * @param replies_seen_count size of the @a replies_seen array
  211. */
  212. void
  213. GSF_pending_request_update_ (struct GSF_PendingRequest *pr,
  214. const struct GNUNET_HashCode *replies_seen,
  215. unsigned int replies_seen_count);
  216. /**
  217. * Obtain the public data associated with a pending request
  218. *
  219. * @param pr pending request
  220. * @return associated public data
  221. */
  222. struct GSF_PendingRequestData *
  223. GSF_pending_request_get_data_ (struct GSF_PendingRequest *pr);
  224. /**
  225. * Check if the given request is still active.
  226. *
  227. * @param pr pending request
  228. * @return #GNUNET_YES if the request is still active
  229. */
  230. int
  231. GSF_pending_request_test_active_ (struct GSF_PendingRequest *pr);
  232. /**
  233. * Test if two pending requests are compatible (would generate
  234. * the same query modulo filters and should thus be processed
  235. * jointly).
  236. *
  237. * @param pra a pending request
  238. * @param prb another pending request
  239. * @return #GNUNET_OK if the requests are compatible
  240. */
  241. int
  242. GSF_pending_request_is_compatible_ (struct GSF_PendingRequest *pra,
  243. struct GSF_PendingRequest *prb);
  244. /**
  245. * Generate the message corresponding to the given pending request for
  246. * transmission to other peers.
  247. *
  248. * @param pr request to generate the message for
  249. * @return envelope with the request message
  250. */
  251. struct GNUNET_MQ_Envelope *
  252. GSF_pending_request_get_message_ (struct GSF_PendingRequest *pr);
  253. /**
  254. * Explicitly cancel a pending request.
  255. *
  256. * @param pr request to cancel
  257. * @param full_cleanup fully purge the request
  258. */
  259. void
  260. GSF_pending_request_cancel_ (struct GSF_PendingRequest *pr,
  261. int full_cleanup);
  262. /**
  263. * Signature of function called on each request.
  264. * (Note: 'subtype' of GNUNET_CONTAINER_HashMapIterator).
  265. *
  266. * @param cls closure
  267. * @param key query for the request
  268. * @param pr handle to the pending request
  269. * @return #GNUNET_YES to continue to iterate
  270. */
  271. typedef int
  272. (*GSF_PendingRequestIterator) (void *cls,
  273. const struct GNUNET_HashCode *key,
  274. struct GSF_PendingRequest *pr);
  275. /**
  276. * Iterate over all pending requests.
  277. *
  278. * @param it function to call for each request
  279. * @param cls closure for it
  280. */
  281. void
  282. GSF_iterate_pending_requests_ (GSF_PendingRequestIterator it,
  283. void *cls);
  284. /**
  285. * Handle P2P "CONTENT" message. Checks that the message is
  286. * well-formed and then checks if there are any pending requests for
  287. * this content and possibly passes it on (to local clients or other
  288. * peers). Does NOT perform migration (content caching at this peer).
  289. *
  290. * @param cls the other peer involved (sender)
  291. * @param put the actual message
  292. */
  293. void
  294. handle_p2p_put (void *cls,
  295. const struct PutMessage *put);
  296. /**
  297. * Consider looking up the data in the DHT (anonymity-level permitting).
  298. *
  299. * @param pr the pending request to process
  300. */
  301. void
  302. GSF_dht_lookup_ (struct GSF_PendingRequest *pr);
  303. /**
  304. * Consider downloading via cadet (if possible)
  305. *
  306. * @param pr the pending request to process
  307. */
  308. void
  309. GSF_cadet_lookup_ (struct GSF_PendingRequest *pr);
  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
  319. (*GSF_LocalLookupContinuation) (void *cls,
  320. struct GSF_PendingRequest *pr,
  321. enum GNUNET_BLOCK_EvaluationResult result);
  322. /**
  323. * Look up the request in the local datastore.
  324. *
  325. * @param pr the pending request to process
  326. * @param cont function to call at the end
  327. * @param cont_cls closure for @a cont
  328. */
  329. void
  330. GSF_local_lookup_ (struct GSF_PendingRequest *pr,
  331. GSF_LocalLookupContinuation cont,
  332. void *cont_cls);
  333. /**
  334. * Is the given target a legitimate peer for forwarding the given request?
  335. *
  336. * @param pr request
  337. * @param target
  338. * @return #GNUNET_YES if this request could be forwarded to the given peer
  339. */
  340. int
  341. GSF_pending_request_test_target_ (struct GSF_PendingRequest *pr,
  342. const struct GNUNET_PeerIdentity *target);
  343. /**
  344. * Setup the subsystem.
  345. */
  346. void
  347. GSF_pending_request_init_ (void);
  348. /**
  349. * Shutdown the subsystem.
  350. */
  351. void
  352. GSF_pending_request_done_ (void);
  353. #endif
  354. /* end of gnunet-service-fs_pr.h */