gnunet-service-set.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2013-2017 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 set/gnunet-service-set.h
  18. * @brief common components for the implementation the different set operations
  19. * @author Florian Dold
  20. * @author Christian Grothoff
  21. */
  22. #ifndef GNUNET_SERVICE_SET_H_PRIVATE
  23. #define GNUNET_SERVICE_SET_H_PRIVATE
  24. #include "platform.h"
  25. #include "gnunet_util_lib.h"
  26. #include "gnunet_protocols.h"
  27. #include "gnunet_applications.h"
  28. #include "gnunet_core_service.h"
  29. #include "gnunet_cadet_service.h"
  30. #include "gnunet_set_service.h"
  31. #include "set.h"
  32. /**
  33. * Implementation-specific set state. Used as opaque pointer, and
  34. * specified further in the respective implementation.
  35. */
  36. struct SetState;
  37. /**
  38. * Implementation-specific set operation. Used as opaque pointer, and
  39. * specified further in the respective implementation.
  40. */
  41. struct OperationState;
  42. /**
  43. * A set that supports a specific operation with other peers.
  44. */
  45. struct Set;
  46. /**
  47. * Information about an element element in the set. All elements are
  48. * stored in a hash-table from their hash-code to their 'struct
  49. * Element', so that the remove and add operations are reasonably
  50. * fast.
  51. */
  52. struct ElementEntry;
  53. /**
  54. * Operation context used to execute a set operation.
  55. */
  56. struct Operation;
  57. /**
  58. * Signature of functions that create the implementation-specific
  59. * state for a set supporting a specific operation.
  60. *
  61. * @return a set state specific to the supported operation, NULL on error
  62. */
  63. typedef struct SetState *
  64. (*SetCreateImpl) (void);
  65. /**
  66. * Signature of functions that implement the add/remove functionality
  67. * for a set supporting a specific operation.
  68. *
  69. * @param set implementation-specific set state
  70. * @param ee element message from the client
  71. */
  72. typedef void
  73. (*SetAddRemoveImpl) (struct SetState *state,
  74. struct ElementEntry *ee);
  75. /**
  76. * Make a copy of a set's internal state.
  77. *
  78. * @param state set state to copy
  79. * @return copy of the internal state
  80. */
  81. typedef struct SetState *
  82. (*SetCopyStateImpl) (struct SetState *state);
  83. /**
  84. * Signature of functions that implement the destruction of the
  85. * implementation-specific set state.
  86. *
  87. * @param state the set state, contains implementation-specific data
  88. */
  89. typedef void
  90. (*SetDestroyImpl) (struct SetState *state);
  91. /**
  92. * Signature of functions that implement accepting a set operation.
  93. *
  94. * @param op operation that is created by accepting the operation,
  95. * should be initialized by the implementation
  96. * @return operation-specific state to keep in @a op
  97. */
  98. typedef struct OperationState *
  99. (*OpAcceptImpl) (struct Operation *op);
  100. /**
  101. * Signature of functions that implement starting the evaluation of
  102. * set operations.
  103. *
  104. * @param op operation that is created, should be initialized to
  105. * begin the evaluation
  106. * @param opaque_context message to be transmitted to the listener
  107. * to convince it to accept, may be NULL
  108. * @return operation-specific state to keep in @a op
  109. */
  110. typedef struct OperationState *
  111. (*OpEvaluateImpl) (struct Operation *op,
  112. const struct GNUNET_MessageHeader *opaque_context);
  113. /**
  114. * Signature of functions that implement operation cancelation.
  115. * This includes notifying the client about the operation's final
  116. * state.
  117. *
  118. * @param op operation state
  119. */
  120. typedef void
  121. (*OpCancelImpl) (struct Operation *op);
  122. /**
  123. * Signature of functions called when the CADET channel died.
  124. *
  125. * @param op operation state
  126. */
  127. typedef void
  128. (*OpChannelDeathImpl) (struct Operation *op);
  129. /**
  130. * Dispatch table for a specific set operation. Every set operation
  131. * has to implement the callback in this struct.
  132. */
  133. struct SetVT
  134. {
  135. /**
  136. * Callback for the set creation.
  137. */
  138. SetCreateImpl create;
  139. /**
  140. * Callback for element insertion
  141. */
  142. SetAddRemoveImpl add;
  143. /**
  144. * Callback for element removal.
  145. */
  146. SetAddRemoveImpl remove;
  147. /**
  148. * Callback for making a copy of a set's internal state.
  149. */
  150. SetCopyStateImpl copy_state;
  151. /**
  152. * Callback for destruction of the set state.
  153. */
  154. SetDestroyImpl destroy_set;
  155. /**
  156. * Callback for accepting a set operation request
  157. */
  158. OpAcceptImpl accept;
  159. /**
  160. * Callback for starting evaluation with a remote peer.
  161. */
  162. OpEvaluateImpl evaluate;
  163. /**
  164. * Callback for canceling an operation.
  165. */
  166. OpCancelImpl cancel;
  167. /**
  168. * Callback called in case the CADET channel died.
  169. */
  170. OpChannelDeathImpl channel_death;
  171. };
  172. /**
  173. * MutationEvent gives information about changes
  174. * to an element (removal / addition) in a set content.
  175. */
  176. struct MutationEvent
  177. {
  178. /**
  179. * First generation affected by this mutation event.
  180. *
  181. * If @a generation is 0, this mutation event is a list
  182. * sentinel element.
  183. */
  184. unsigned int generation;
  185. /**
  186. * If @a added is #GNUNET_YES, then this is a
  187. * `remove` event, otherwise it is an `add` event.
  188. */
  189. int added;
  190. };
  191. /**
  192. * Information about an element element in the set. All elements are
  193. * stored in a hash-table from their hash-code to their `struct
  194. * Element`, so that the remove and add operations are reasonably
  195. * fast.
  196. */
  197. struct ElementEntry
  198. {
  199. /**
  200. * The actual element. The data for the element
  201. * should be allocated at the end of this struct.
  202. */
  203. struct GNUNET_SET_Element element;
  204. /**
  205. * Hash of the element. For set union: Will be used to derive the
  206. * different IBF keys for different salts.
  207. */
  208. struct GNUNET_HashCode element_hash;
  209. /**
  210. * If @a mutations is not NULL, it contains
  211. * a list of mutations, ordered by increasing generation.
  212. * The list is terminated by a sentinel event with `generation`
  213. * set to 0.
  214. *
  215. * If @a mutations is NULL, then this element exists in all generations
  216. * of the respective set content this element belongs to.
  217. */
  218. struct MutationEvent *mutations;
  219. /**
  220. * Number of elements in the array @a mutations.
  221. */
  222. unsigned int mutations_size;
  223. /**
  224. * #GNUNET_YES if the element is a remote element, and does not belong
  225. * to the operation's set.
  226. */
  227. int remote;
  228. };
  229. /**
  230. * A listener is inhabited by a client, and waits for evaluation
  231. * requests from remote peers.
  232. */
  233. struct Listener;
  234. /**
  235. * State we keep per client.
  236. */
  237. struct ClientState
  238. {
  239. /**
  240. * Set, if associated with the client, otherwise NULL.
  241. */
  242. struct Set *set;
  243. /**
  244. * Listener, if associated with the client, otherwise NULL.
  245. */
  246. struct Listener *listener;
  247. /**
  248. * Client handle.
  249. */
  250. struct GNUNET_SERVICE_Client *client;
  251. /**
  252. * Message queue.
  253. */
  254. struct GNUNET_MQ_Handle *mq;
  255. };
  256. /**
  257. * Operation context used to execute a set operation.
  258. */
  259. struct Operation
  260. {
  261. /**
  262. * Kept in a DLL of the listener, if @e listener is non-NULL.
  263. */
  264. struct Operation *next;
  265. /**
  266. * Kept in a DLL of the listener, if @e listener is non-NULL.
  267. */
  268. struct Operation *prev;
  269. /**
  270. * Channel to the peer.
  271. */
  272. struct GNUNET_CADET_Channel *channel;
  273. /**
  274. * Port this operation runs on.
  275. */
  276. struct Listener *listener;
  277. /**
  278. * Message queue for the channel.
  279. */
  280. struct GNUNET_MQ_Handle *mq;
  281. /**
  282. * Context message, may be NULL.
  283. */
  284. struct GNUNET_MessageHeader *context_msg;
  285. /**
  286. * Set associated with the operation, NULL until the spec has been
  287. * associated with a set.
  288. */
  289. struct Set *set;
  290. /**
  291. * Operation-specific operation state. Note that the exact
  292. * type depends on this being a union or intersection operation
  293. * (and thus on @e vt).
  294. */
  295. struct OperationState *state;
  296. /**
  297. * The identity of the requesting peer. Needs to
  298. * be stored here as the op spec might not have been created yet.
  299. */
  300. struct GNUNET_PeerIdentity peer;
  301. /**
  302. * Timeout task, if the incoming peer has not been accepted
  303. * after the timeout, it will be disconnected.
  304. */
  305. struct GNUNET_SCHEDULER_Task *timeout_task;
  306. /**
  307. * Salt to use for the operation.
  308. */
  309. uint32_t salt;
  310. /**
  311. * Remote peers element count
  312. */
  313. uint32_t remote_element_count;
  314. /**
  315. * ID used to identify an operation between service and client
  316. */
  317. uint32_t client_request_id;
  318. /**
  319. * When are elements sent to the client, and which elements are sent?
  320. */
  321. enum GNUNET_SET_ResultMode result_mode;
  322. /**
  323. * Always use delta operation instead of sending full sets,
  324. * even it it's less efficient.
  325. */
  326. int force_delta;
  327. /**
  328. * Always send full sets, even if delta operations would
  329. * be more efficient.
  330. */
  331. int force_full;
  332. /**
  333. * #GNUNET_YES to fail operations where Byzantine faults
  334. * are suspected
  335. */
  336. int byzantine;
  337. /**
  338. * Lower bound for the set size, used only when
  339. * byzantine mode is enabled.
  340. */
  341. int byzantine_lower_bound;
  342. /**
  343. * Unique request id for the request from a remote peer, sent to the
  344. * client, which will accept or reject the request. Set to '0' iff
  345. * the request has not been suggested yet.
  346. */
  347. uint32_t suggest_id;
  348. /**
  349. * Generation in which the operation handle
  350. * was created.
  351. */
  352. unsigned int generation_created;
  353. };
  354. /**
  355. * SetContent stores the actual set elements, which may be shared by
  356. * multiple generations derived from one set.
  357. */
  358. struct SetContent
  359. {
  360. /**
  361. * Maps `struct GNUNET_HashCode *` to `struct ElementEntry *`.
  362. */
  363. struct GNUNET_CONTAINER_MultiHashMap *elements;
  364. /**
  365. * Mutations requested by the client that we're
  366. * unable to execute right now because we're iterating
  367. * over the underlying hash map of elements.
  368. */
  369. struct PendingMutation *pending_mutations_head;
  370. /**
  371. * Mutations requested by the client that we're
  372. * unable to execute right now because we're iterating
  373. * over the underlying hash map of elements.
  374. */
  375. struct PendingMutation *pending_mutations_tail;
  376. /**
  377. * Number of references to the content.
  378. */
  379. unsigned int refcount;
  380. /**
  381. * FIXME: document!
  382. */
  383. unsigned int latest_generation;
  384. /**
  385. * Number of concurrently active iterators.
  386. */
  387. int iterator_count;
  388. };
  389. struct GenerationRange
  390. {
  391. /**
  392. * First generation that is excluded.
  393. */
  394. unsigned int start;
  395. /**
  396. * Generation after the last excluded generation.
  397. */
  398. unsigned int end;
  399. };
  400. /**
  401. * Information about a mutation to apply to a set.
  402. */
  403. struct PendingMutation
  404. {
  405. /**
  406. * Mutations are kept in a DLL.
  407. */
  408. struct PendingMutation *prev;
  409. /**
  410. * Mutations are kept in a DLL.
  411. */
  412. struct PendingMutation *next;
  413. /**
  414. * Set this mutation is about.
  415. */
  416. struct Set *set;
  417. /**
  418. * Message that describes the desired mutation.
  419. * May only be a #GNUNET_MESSAGE_TYPE_SET_ADD or
  420. * #GNUNET_MESSAGE_TYPE_SET_REMOVE.
  421. */
  422. struct GNUNET_SET_ElementMessage *msg;
  423. };
  424. /**
  425. * A set that supports a specific operation with other peers.
  426. */
  427. struct Set
  428. {
  429. /**
  430. * Sets are held in a doubly linked list (in `sets_head` and `sets_tail`).
  431. */
  432. struct Set *next;
  433. /**
  434. * Sets are held in a doubly linked list.
  435. */
  436. struct Set *prev;
  437. /**
  438. * Client that owns the set. Only one client may own a set,
  439. * and there can only be one set per client.
  440. */
  441. struct ClientState *cs;
  442. /**
  443. * Content, possibly shared by multiple sets,
  444. * and thus reference counted.
  445. */
  446. struct SetContent *content;
  447. /**
  448. * Virtual table for this set. Determined by the operation type of
  449. * this set.
  450. *
  451. * Used only for Add/remove of elements and when receiving an incoming
  452. * operation from a remote peer.
  453. */
  454. const struct SetVT *vt;
  455. /**
  456. * Implementation-specific state.
  457. */
  458. struct SetState *state;
  459. /**
  460. * Current state of iterating elements for the client.
  461. * NULL if we are not currently iterating.
  462. */
  463. struct GNUNET_CONTAINER_MultiHashMapIterator *iter;
  464. /**
  465. * Evaluate operations are held in a linked list.
  466. */
  467. struct Operation *ops_head;
  468. /**
  469. * Evaluate operations are held in a linked list.
  470. */
  471. struct Operation *ops_tail;
  472. /**
  473. * List of generations we have to exclude, due to lazy copies.
  474. */
  475. struct GenerationRange *excluded_generations;
  476. /**
  477. * Current generation, that is, number of previously executed
  478. * operations and lazy copies on the underlying set content.
  479. */
  480. unsigned int current_generation;
  481. /**
  482. * Number of elements in array @a excluded_generations.
  483. */
  484. unsigned int excluded_generations_size;
  485. /**
  486. * Type of operation supported for this set
  487. */
  488. enum GNUNET_SET_OperationType operation;
  489. /**
  490. * Generation we're currently iteration over.
  491. */
  492. unsigned int iter_generation;
  493. /**
  494. * Each @e iter is assigned a unique number, so that the client
  495. * can distinguish iterations.
  496. */
  497. uint16_t iteration_id;
  498. };
  499. extern struct GNUNET_STATISTICS_Handle *_GSS_statistics;
  500. /**
  501. * Destroy the given operation. Used for any operation where both
  502. * peers were known and that thus actually had a vt and channel. Must
  503. * not be used for operations where 'listener' is still set and we do
  504. * not know the other peer.
  505. *
  506. * Call the implementation-specific cancel function of the operation.
  507. * Disconnects from the remote peer. Does not disconnect the client,
  508. * as there may be multiple operations per set.
  509. *
  510. * @param op operation to destroy
  511. * @param gc #GNUNET_YES to perform garbage collection on the set
  512. */
  513. void
  514. _GSS_operation_destroy (struct Operation *op,
  515. int gc);
  516. /**
  517. * This function probably should not exist
  518. * and be replaced by inlining more specific
  519. * logic in the various places where it is called.
  520. */
  521. void
  522. _GSS_operation_destroy2 (struct Operation *op);
  523. /**
  524. * Get the table with implementing functions for set union.
  525. *
  526. * @return the operation specific VTable
  527. */
  528. const struct SetVT *
  529. _GSS_union_vt (void);
  530. /**
  531. * Get the table with implementing functions for set intersection.
  532. *
  533. * @return the operation specific VTable
  534. */
  535. const struct SetVT *
  536. _GSS_intersection_vt (void);
  537. /**
  538. * Is element @a ee part of the set used by @a op?
  539. *
  540. * @param ee element to test
  541. * @param op operation the defines the set and its generation
  542. * @return #GNUNET_YES if the element is in the set, #GNUNET_NO if not
  543. */
  544. int
  545. _GSS_is_element_of_operation (struct ElementEntry *ee,
  546. struct Operation *op);
  547. #endif