gnunet-service-set_intersection.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261
  1. /*
  2. This file is part of GNUnet
  3. (C) 2013, 2014 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 set/gnunet-service-set_intersection.c
  19. * @brief two-peer set intersection
  20. * @author Christian Fuchs
  21. * @author Christian Grothoff
  22. */
  23. #include "platform.h"
  24. #include "gnunet_util_lib.h"
  25. #include "gnunet-service-set.h"
  26. #include "gnunet_block_lib.h"
  27. #include "gnunet-service-set_protocol.h"
  28. #include <gcrypt.h>
  29. /**
  30. * Current phase we are in for a intersection operation.
  31. */
  32. enum IntersectionOperationPhase
  33. {
  34. /**
  35. * We are just starting.
  36. */
  37. PHASE_INITIAL,
  38. /**
  39. * We have send the number of our elements to the other
  40. * peer, but did not setup our element set yet.
  41. */
  42. PHASE_COUNT_SENT,
  43. /**
  44. * We have initialized our set and are now reducing it by exchanging
  45. * Bloom filters until one party notices the their element hashes
  46. * are equal.
  47. */
  48. PHASE_BF_EXCHANGE,
  49. /**
  50. * The protocol is over. Results may still have to be sent to the
  51. * client.
  52. */
  53. PHASE_FINISHED
  54. };
  55. /**
  56. * State of an evaluate operation with another peer.
  57. */
  58. struct OperationState
  59. {
  60. /**
  61. * The bf we currently receive
  62. */
  63. struct GNUNET_CONTAINER_BloomFilter *remote_bf;
  64. /**
  65. * BF of the set's element.
  66. */
  67. struct GNUNET_CONTAINER_BloomFilter *local_bf;
  68. /**
  69. * Remaining elements in the intersection operation.
  70. * Maps element-id-hashes to 'elements in our set'.
  71. */
  72. struct GNUNET_CONTAINER_MultiHashMap *my_elements;
  73. /**
  74. * Iterator for sending the final set of @e my_elements to the client.
  75. */
  76. struct GNUNET_CONTAINER_MultiHashMapIterator *full_result_iter;
  77. /**
  78. * Evaluate operations are held in a linked list.
  79. */
  80. struct OperationState *next;
  81. /**
  82. * Evaluate operations are held in a linked list.
  83. */
  84. struct OperationState *prev;
  85. /**
  86. * For multipart BF transmissions, we have to store the
  87. * bloomfilter-data until we fully received it.
  88. */
  89. char *bf_data;
  90. /**
  91. * XOR of the keys of all of the elements (remaining) in my set.
  92. * Always updated when elements are added or removed to
  93. * @e my_elements.
  94. */
  95. struct GNUNET_HashCode my_xor;
  96. /**
  97. * XOR of the keys of all of the elements (remaining) in
  98. * the other peer's set. Updated when we receive the
  99. * other peer's Bloom filter.
  100. */
  101. struct GNUNET_HashCode other_xor;
  102. /**
  103. * How many bytes of @e bf_data are valid?
  104. */
  105. uint32_t bf_data_offset;
  106. /**
  107. * Current element count contained within @e my_elements.
  108. * (May differ briefly during initialization.)
  109. */
  110. uint32_t my_element_count;
  111. /**
  112. * size of the bloomfilter in @e bf_data.
  113. */
  114. uint32_t bf_data_size;
  115. /**
  116. * size of the bloomfilter
  117. */
  118. uint32_t bf_bits_per_element;
  119. /**
  120. * Salt currently used for BF construction (by us or the other peer,
  121. * depending on where we are in the code).
  122. */
  123. uint32_t salt;
  124. /**
  125. * Current state of the operation.
  126. */
  127. enum IntersectionOperationPhase phase;
  128. /**
  129. * Generation in which the operation handle
  130. * was created.
  131. */
  132. unsigned int generation_created;
  133. /**
  134. * Did we send the client that we are done?
  135. */
  136. int client_done_sent;
  137. };
  138. /**
  139. * Extra state required for efficient set intersection.
  140. * Merely tracks the total number of elements.
  141. */
  142. struct SetState
  143. {
  144. /**
  145. * Number of currently valid elements in the set which have not been
  146. * removed.
  147. */
  148. uint32_t current_set_element_count;
  149. };
  150. /**
  151. * If applicable in the current operation mode, send a result message
  152. * to the client indicating we removed an element.
  153. *
  154. * @param op intersection operation
  155. * @param element element to send
  156. */
  157. static void
  158. send_client_removed_element (struct Operation *op,
  159. struct GNUNET_SET_Element *element)
  160. {
  161. struct GNUNET_MQ_Envelope *ev;
  162. struct GNUNET_SET_ResultMessage *rm;
  163. if (GNUNET_SET_RESULT_REMOVED != op->spec->result_mode)
  164. return; /* Wrong mode for transmitting removed elements */
  165. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  166. "Sending removed element (size %u) to client\n",
  167. element->size);
  168. GNUNET_assert (0 != op->spec->client_request_id);
  169. ev = GNUNET_MQ_msg_extra (rm,
  170. element->size,
  171. GNUNET_MESSAGE_TYPE_SET_RESULT);
  172. if (NULL == ev)
  173. {
  174. GNUNET_break (0);
  175. return;
  176. }
  177. rm->result_status = htons (GNUNET_SET_STATUS_OK);
  178. rm->request_id = htonl (op->spec->client_request_id);
  179. rm->element_type = element->element_type;
  180. memcpy (&rm[1],
  181. element->data,
  182. element->size);
  183. GNUNET_MQ_send (op->spec->set->client_mq,
  184. ev);
  185. }
  186. /**
  187. * Fills the "my_elements" hashmap with all relevant elements.
  188. *
  189. * @param cls the `struct Operation *` we are performing
  190. * @param key current key code
  191. * @param value the `struct ElementEntry *` from the hash map
  192. * @return #GNUNET_YES (we should continue to iterate)
  193. */
  194. static int
  195. filtered_map_initialization (void *cls,
  196. const struct GNUNET_HashCode *key,
  197. void *value)
  198. {
  199. struct Operation *op = cls;
  200. struct ElementEntry *ee = value;
  201. struct GNUNET_HashCode mutated_hash;
  202. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  203. "FIMA called for %s:%u\n",
  204. GNUNET_h2s (&ee->element_hash),
  205. ee->element.size);
  206. if ( (op->generation_created < ee->generation_removed) &&
  207. (op->generation_created >= ee->generation_added) )
  208. {
  209. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  210. "Reduced initialization, not starting with %s:%u (wrong generation)\n",
  211. GNUNET_h2s (&ee->element_hash),
  212. ee->element.size);
  213. return GNUNET_YES; /* element not valid in our operation's generation */
  214. }
  215. /* Test if element is in other peer's bloomfilter */
  216. GNUNET_BLOCK_mingle_hash (&ee->element_hash,
  217. op->state->salt,
  218. &mutated_hash);
  219. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  220. "Testing mingled hash %s with salt %u\n",
  221. GNUNET_h2s (&mutated_hash),
  222. op->state->salt);
  223. if (GNUNET_NO ==
  224. GNUNET_CONTAINER_bloomfilter_test (op->state->remote_bf,
  225. &mutated_hash))
  226. {
  227. /* remove this element */
  228. send_client_removed_element (op,
  229. &ee->element);
  230. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  231. "Reduced initialization, not starting with %s:%u\n",
  232. GNUNET_h2s (&ee->element_hash),
  233. ee->element.size);
  234. return GNUNET_YES;
  235. }
  236. op->state->my_element_count++;
  237. GNUNET_CRYPTO_hash_xor (&op->state->my_xor,
  238. &ee->element_hash,
  239. &op->state->my_xor);
  240. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  241. "Filtered initialization of my_elements, adding %s:%u\n",
  242. GNUNET_h2s (&ee->element_hash),
  243. ee->element.size);
  244. GNUNET_break (GNUNET_YES ==
  245. GNUNET_CONTAINER_multihashmap_put (op->state->my_elements,
  246. &ee->element_hash,
  247. ee,
  248. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
  249. return GNUNET_YES;
  250. }
  251. /**
  252. * Removes elements from our hashmap if they are not contained within the
  253. * provided remote bloomfilter.
  254. *
  255. * @param cls closure with the `struct Operation *`
  256. * @param key current key code
  257. * @param value value in the hash map
  258. * @return #GNUNET_YES (we should continue to iterate)
  259. */
  260. static int
  261. iterator_bf_reduce (void *cls,
  262. const struct GNUNET_HashCode *key,
  263. void *value)
  264. {
  265. struct Operation *op = cls;
  266. struct ElementEntry *ee = value;
  267. struct GNUNET_HashCode mutated_hash;
  268. GNUNET_BLOCK_mingle_hash (&ee->element_hash,
  269. op->state->salt,
  270. &mutated_hash);
  271. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  272. "Testing mingled hash %s with salt %u\n",
  273. GNUNET_h2s (&mutated_hash),
  274. op->state->salt);
  275. if (GNUNET_NO ==
  276. GNUNET_CONTAINER_bloomfilter_test (op->state->remote_bf,
  277. &mutated_hash))
  278. {
  279. GNUNET_break (0 < op->state->my_element_count);
  280. op->state->my_element_count--;
  281. GNUNET_CRYPTO_hash_xor (&op->state->my_xor,
  282. &ee->element_hash,
  283. &op->state->my_xor);
  284. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  285. "Bloom filter reduction of my_elements, removing %s:%u\n",
  286. GNUNET_h2s (&ee->element_hash),
  287. ee->element.size);
  288. GNUNET_assert (GNUNET_YES ==
  289. GNUNET_CONTAINER_multihashmap_remove (op->state->my_elements,
  290. &ee->element_hash,
  291. ee));
  292. send_client_removed_element (op,
  293. &ee->element);
  294. }
  295. else
  296. {
  297. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  298. "Bloom filter reduction of my_elements, keeping %s:%u\n",
  299. GNUNET_h2s (&ee->element_hash),
  300. ee->element.size);
  301. }
  302. return GNUNET_YES;
  303. }
  304. /**
  305. * Create initial bloomfilter based on all the elements given.
  306. *
  307. * @param cls the `struct Operation *`
  308. * @param key current key code
  309. * @param value the `struct ElementEntry` to process
  310. * @return #GNUNET_YES (we should continue to iterate)
  311. */
  312. static int
  313. iterator_bf_create (void *cls,
  314. const struct GNUNET_HashCode *key,
  315. void *value)
  316. {
  317. struct Operation *op = cls;
  318. struct ElementEntry *ee = value;
  319. struct GNUNET_HashCode mutated_hash;
  320. GNUNET_BLOCK_mingle_hash (&ee->element_hash,
  321. op->state->salt,
  322. &mutated_hash);
  323. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  324. "Initializing BF with hash %s with salt %u\n",
  325. GNUNET_h2s (&mutated_hash),
  326. op->state->salt);
  327. GNUNET_CONTAINER_bloomfilter_add (op->state->local_bf,
  328. &mutated_hash);
  329. return GNUNET_YES;
  330. }
  331. /**
  332. * Inform the client that the intersection operation has failed,
  333. * and proceed to destroy the evaluate operation.
  334. *
  335. * @param op the intersection operation to fail
  336. */
  337. static void
  338. fail_intersection_operation (struct Operation *op)
  339. {
  340. struct GNUNET_MQ_Envelope *ev;
  341. struct GNUNET_SET_ResultMessage *msg;
  342. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  343. "Intersection operation failed\n");
  344. if (NULL != op->state->my_elements)
  345. {
  346. GNUNET_CONTAINER_multihashmap_destroy (op->state->my_elements);
  347. op->state->my_elements = NULL;
  348. }
  349. ev = GNUNET_MQ_msg (msg,
  350. GNUNET_MESSAGE_TYPE_SET_RESULT);
  351. msg->result_status = htons (GNUNET_SET_STATUS_FAILURE);
  352. msg->request_id = htonl (op->spec->client_request_id);
  353. msg->element_type = htons (0);
  354. GNUNET_MQ_send (op->spec->set->client_mq,
  355. ev);
  356. _GSS_operation_destroy (op,
  357. GNUNET_YES);
  358. }
  359. /**
  360. * Send a bloomfilter to our peer. After the result done message has
  361. * been sent to the client, destroy the evaluate operation.
  362. *
  363. * @param op intersection operation
  364. */
  365. static void
  366. send_bloomfilter (struct Operation *op)
  367. {
  368. struct GNUNET_MQ_Envelope *ev;
  369. struct BFMessage *msg;
  370. uint32_t bf_size;
  371. uint32_t bf_elementbits;
  372. uint32_t chunk_size;
  373. char *bf_data;
  374. uint32_t offset;
  375. /* We consider the ratio of the set sizes to determine
  376. the number of bits per element, as the smaller set
  377. should use more bits to maximize its set reduction
  378. potential and minimize overall bandwidth consumption. */
  379. bf_elementbits = 2 + ceil (log2((double)
  380. (op->spec->remote_element_count /
  381. (double) op->state->my_element_count)));
  382. if (bf_elementbits < 1)
  383. bf_elementbits = 1; /* make sure k is not 0 */
  384. /* optimize BF-size to ~50% of bits set */
  385. bf_size = ceil ((double) (op->state->my_element_count
  386. * bf_elementbits / log(2)));
  387. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  388. "Sending Bloom filter (%u) of size %u bytes\n",
  389. (unsigned int) bf_elementbits,
  390. (unsigned int) bf_size);
  391. op->state->local_bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
  392. bf_size,
  393. bf_elementbits);
  394. op->state->salt = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
  395. UINT32_MAX);
  396. GNUNET_CONTAINER_multihashmap_iterate (op->state->my_elements,
  397. &iterator_bf_create,
  398. op);
  399. /* send our Bloom filter */
  400. chunk_size = 60 * 1024 - sizeof (struct BFMessage);
  401. if (bf_size <= chunk_size)
  402. {
  403. /* singlepart */
  404. chunk_size = bf_size;
  405. ev = GNUNET_MQ_msg_extra (msg,
  406. chunk_size,
  407. GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_BF);
  408. GNUNET_assert (GNUNET_SYSERR !=
  409. GNUNET_CONTAINER_bloomfilter_get_raw_data (op->state->local_bf,
  410. (char*) &msg[1],
  411. bf_size));
  412. msg->sender_element_count = htonl (op->state->my_element_count);
  413. msg->bloomfilter_total_length = htonl (bf_size);
  414. msg->bits_per_element = htonl (bf_elementbits);
  415. msg->sender_mutator = htonl (op->state->salt);
  416. msg->element_xor_hash = op->state->my_xor;
  417. GNUNET_MQ_send (op->mq, ev);
  418. }
  419. else
  420. {
  421. /* multipart */
  422. bf_data = GNUNET_malloc (bf_size);
  423. GNUNET_assert (GNUNET_SYSERR !=
  424. GNUNET_CONTAINER_bloomfilter_get_raw_data (op->state->local_bf,
  425. bf_data,
  426. bf_size));
  427. offset = 0;
  428. while (offset < bf_size)
  429. {
  430. if (bf_size - chunk_size < offset)
  431. chunk_size = bf_size - offset;
  432. ev = GNUNET_MQ_msg_extra (msg,
  433. chunk_size,
  434. GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_BF);
  435. memcpy (&msg[1],
  436. &bf_data[offset],
  437. chunk_size);
  438. offset += chunk_size;
  439. msg->sender_element_count = htonl (op->state->my_element_count);
  440. msg->bloomfilter_total_length = htonl (bf_size);
  441. msg->bits_per_element = htonl (bf_elementbits);
  442. msg->sender_mutator = htonl (op->state->salt);
  443. msg->element_xor_hash = op->state->my_xor;
  444. GNUNET_MQ_send (op->mq, ev);
  445. }
  446. GNUNET_free (bf_data);
  447. }
  448. GNUNET_CONTAINER_bloomfilter_free (op->state->local_bf);
  449. op->state->local_bf = NULL;
  450. }
  451. /**
  452. * Signal to the client that the operation has finished and
  453. * destroy the operation.
  454. *
  455. * @param cls operation to destroy
  456. */
  457. static void
  458. send_client_done_and_destroy (void *cls)
  459. {
  460. struct Operation *op = cls;
  461. struct GNUNET_MQ_Envelope *ev;
  462. struct GNUNET_SET_ResultMessage *rm;
  463. ev = GNUNET_MQ_msg (rm,
  464. GNUNET_MESSAGE_TYPE_SET_RESULT);
  465. rm->request_id = htonl (op->spec->client_request_id);
  466. rm->result_status = htons (GNUNET_SET_STATUS_DONE);
  467. rm->element_type = htons (0);
  468. GNUNET_MQ_send (op->spec->set->client_mq,
  469. ev);
  470. _GSS_operation_destroy (op,
  471. GNUNET_YES);
  472. }
  473. /**
  474. * Send all elements in the full result iterator.
  475. *
  476. * @param cls the `struct Operation *`
  477. */
  478. static void
  479. send_remaining_elements (void *cls)
  480. {
  481. struct Operation *op = cls;
  482. const void *nxt;
  483. const struct ElementEntry *ee;
  484. struct GNUNET_MQ_Envelope *ev;
  485. struct GNUNET_SET_ResultMessage *rm;
  486. const struct GNUNET_SET_Element *element;
  487. int res;
  488. res = GNUNET_CONTAINER_multihashmap_iterator_next (op->state->full_result_iter,
  489. NULL,
  490. &nxt);
  491. if (GNUNET_NO == res)
  492. {
  493. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  494. "Sending done and destroy because iterator ran out\n");
  495. op->keep--;
  496. send_client_done_and_destroy (op);
  497. return;
  498. }
  499. ee = nxt;
  500. element = &ee->element;
  501. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  502. "Sending element %s:%u to client (full set)\n",
  503. GNUNET_h2s (&ee->element_hash),
  504. element->size);
  505. GNUNET_assert (0 != op->spec->client_request_id);
  506. ev = GNUNET_MQ_msg_extra (rm,
  507. element->size,
  508. GNUNET_MESSAGE_TYPE_SET_RESULT);
  509. GNUNET_assert (NULL != ev);
  510. rm->result_status = htons (GNUNET_SET_STATUS_OK);
  511. rm->request_id = htonl (op->spec->client_request_id);
  512. rm->element_type = element->element_type;
  513. memcpy (&rm[1],
  514. element->data,
  515. element->size);
  516. GNUNET_MQ_notify_sent (ev,
  517. &send_remaining_elements,
  518. op);
  519. GNUNET_MQ_send (op->spec->set->client_mq,
  520. ev);
  521. }
  522. /**
  523. * Inform the peer that this operation is complete.
  524. *
  525. * @param op the intersection operation to fail
  526. */
  527. static void
  528. send_peer_done (struct Operation *op)
  529. {
  530. struct GNUNET_MQ_Envelope *ev;
  531. struct IntersectionDoneMessage *idm;
  532. op->state->phase = PHASE_FINISHED;
  533. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  534. "Intersection succeeded, sending DONE\n");
  535. GNUNET_CONTAINER_bloomfilter_free (op->state->local_bf);
  536. op->state->local_bf = NULL;
  537. ev = GNUNET_MQ_msg (idm,
  538. GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_DONE);
  539. idm->final_element_count = htonl (op->state->my_element_count);
  540. idm->element_xor_hash = op->state->my_xor;
  541. GNUNET_MQ_send (op->mq,
  542. ev);
  543. }
  544. /**
  545. * Process a Bloomfilter once we got all the chunks.
  546. *
  547. * @param op the intersection operation
  548. */
  549. static void
  550. process_bf (struct Operation *op)
  551. {
  552. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  553. "Received BF in phase %u, foreign count is %u, my element count is %u/%u\n",
  554. op->state->phase,
  555. op->spec->remote_element_count,
  556. op->state->my_element_count,
  557. GNUNET_CONTAINER_multihashmap_size (op->spec->set->elements));
  558. switch (op->state->phase)
  559. {
  560. case PHASE_INITIAL:
  561. GNUNET_break_op (0);
  562. fail_intersection_operation(op);
  563. return;
  564. case PHASE_COUNT_SENT:
  565. /* This is the first BF being sent, build our initial map with
  566. filtering in place */
  567. op->state->my_elements
  568. = GNUNET_CONTAINER_multihashmap_create (op->spec->remote_element_count,
  569. GNUNET_YES);
  570. op->state->my_element_count = 0;
  571. GNUNET_CONTAINER_multihashmap_iterate (op->spec->set->elements,
  572. &filtered_map_initialization,
  573. op);
  574. break;
  575. case PHASE_BF_EXCHANGE:
  576. /* Update our set by reduction */
  577. GNUNET_CONTAINER_multihashmap_iterate (op->state->my_elements,
  578. &iterator_bf_reduce,
  579. op);
  580. break;
  581. case PHASE_FINISHED:
  582. GNUNET_break_op (0);
  583. fail_intersection_operation(op);
  584. return;
  585. }
  586. GNUNET_CONTAINER_bloomfilter_free (op->state->remote_bf);
  587. op->state->remote_bf = NULL;
  588. if ( (0 == op->state->my_element_count) || /* fully disjoint */
  589. ( (op->state->my_element_count == op->spec->remote_element_count) &&
  590. (0 == memcmp (&op->state->my_xor,
  591. &op->state->other_xor,
  592. sizeof (struct GNUNET_HashCode))) ) )
  593. {
  594. /* we are done */
  595. send_peer_done (op);
  596. return;
  597. }
  598. op->state->phase = PHASE_BF_EXCHANGE;
  599. send_bloomfilter (op);
  600. }
  601. /**
  602. * Handle an BF message from a remote peer.
  603. *
  604. * @param cls the intersection operation
  605. * @param mh the header of the message
  606. */
  607. static void
  608. handle_p2p_bf (void *cls,
  609. const struct GNUNET_MessageHeader *mh)
  610. {
  611. struct Operation *op = cls;
  612. const struct BFMessage *msg;
  613. uint32_t bf_size;
  614. uint32_t chunk_size;
  615. uint32_t bf_bits_per_element;
  616. uint16_t msize;
  617. msize = htons (mh->size);
  618. if (msize < sizeof (struct BFMessage))
  619. {
  620. GNUNET_break_op (0);
  621. fail_intersection_operation (op);
  622. return;
  623. }
  624. msg = (const struct BFMessage *) mh;
  625. switch (op->state->phase)
  626. {
  627. case PHASE_INITIAL:
  628. GNUNET_break_op (0);
  629. fail_intersection_operation (op);
  630. break;
  631. case PHASE_COUNT_SENT:
  632. case PHASE_BF_EXCHANGE:
  633. bf_size = ntohl (msg->bloomfilter_total_length);
  634. bf_bits_per_element = ntohl (msg->bits_per_element);
  635. chunk_size = msize - sizeof (struct BFMessage);
  636. op->state->other_xor = msg->element_xor_hash;
  637. if (bf_size == chunk_size)
  638. {
  639. if (NULL != op->state->bf_data)
  640. {
  641. GNUNET_break_op (0);
  642. fail_intersection_operation (op);
  643. return;
  644. }
  645. /* single part, done here immediately */
  646. op->state->remote_bf
  647. = GNUNET_CONTAINER_bloomfilter_init ((const char*) &msg[1],
  648. bf_size,
  649. bf_bits_per_element);
  650. op->state->salt = ntohl (msg->sender_mutator);
  651. op->spec->remote_element_count = ntohl (msg->sender_element_count);
  652. process_bf (op);
  653. return;
  654. }
  655. /* multipart chunk */
  656. if (NULL == op->state->bf_data)
  657. {
  658. /* first chunk, initialize */
  659. op->state->bf_data = GNUNET_malloc (bf_size);
  660. op->state->bf_data_size = bf_size;
  661. op->state->bf_bits_per_element = bf_bits_per_element;
  662. op->state->bf_data_offset = 0;
  663. op->state->salt = ntohl (msg->sender_mutator);
  664. op->spec->remote_element_count = ntohl (msg->sender_element_count);
  665. }
  666. else
  667. {
  668. /* increment */
  669. if ( (op->state->bf_data_size != bf_size) ||
  670. (op->state->bf_bits_per_element != bf_bits_per_element) ||
  671. (op->state->bf_data_offset + chunk_size > bf_size) ||
  672. (op->state->salt != ntohl (msg->sender_mutator)) ||
  673. (op->spec->remote_element_count != ntohl (msg->sender_element_count)) )
  674. {
  675. GNUNET_break_op (0);
  676. fail_intersection_operation (op);
  677. return;
  678. }
  679. }
  680. memcpy (&op->state->bf_data[op->state->bf_data_offset],
  681. (const char*) &msg[1],
  682. chunk_size);
  683. op->state->bf_data_offset += chunk_size;
  684. if (op->state->bf_data_offset == bf_size)
  685. {
  686. /* last chunk, run! */
  687. op->state->remote_bf
  688. = GNUNET_CONTAINER_bloomfilter_init (op->state->bf_data,
  689. bf_size,
  690. bf_bits_per_element);
  691. GNUNET_free (op->state->bf_data);
  692. op->state->bf_data = NULL;
  693. op->state->bf_data_size = 0;
  694. process_bf (op);
  695. }
  696. break;
  697. default:
  698. GNUNET_break_op (0);
  699. fail_intersection_operation (op);
  700. break;
  701. }
  702. }
  703. /**
  704. * Fills the "my_elements" hashmap with the initial set of
  705. * (non-deleted) elements from the set of the specification.
  706. *
  707. * @param cls closure with the `struct Operation *`
  708. * @param key current key code for the element
  709. * @param value value in the hash map with the `struct ElementEntry *`
  710. * @return #GNUNET_YES (we should continue to iterate)
  711. */
  712. static int
  713. initialize_map_unfiltered (void *cls,
  714. const struct GNUNET_HashCode *key,
  715. void *value)
  716. {
  717. struct ElementEntry *ee = value;
  718. struct Operation *op = cls;
  719. if ( (op->generation_created < ee->generation_removed) &&
  720. (op->generation_created >= ee->generation_added) )
  721. return GNUNET_YES; /* element not live in operation's generation */
  722. GNUNET_CRYPTO_hash_xor (&op->state->my_xor,
  723. &ee->element_hash,
  724. &op->state->my_xor);
  725. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  726. "Initial full initialization of my_elements, adding %s:%u\n",
  727. GNUNET_h2s (&ee->element_hash),
  728. ee->element.size);
  729. GNUNET_break (GNUNET_YES ==
  730. GNUNET_CONTAINER_multihashmap_put (op->state->my_elements,
  731. &ee->element_hash,
  732. ee,
  733. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
  734. return GNUNET_YES;
  735. }
  736. /**
  737. * Send our element count to the peer, in case our element count is
  738. * lower than his.
  739. *
  740. * @param op intersection operation
  741. */
  742. static void
  743. send_element_count (struct Operation *op)
  744. {
  745. struct GNUNET_MQ_Envelope *ev;
  746. struct IntersectionElementInfoMessage *msg;
  747. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  748. "Sending our element count (%u)\n",
  749. op->state->my_element_count);
  750. ev = GNUNET_MQ_msg (msg,
  751. GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_ELEMENT_INFO);
  752. msg->sender_element_count = htonl (op->state->my_element_count);
  753. GNUNET_MQ_send (op->mq, ev);
  754. }
  755. /**
  756. * We go first, initialize our map with all elements and
  757. * send the first Bloom filter.
  758. *
  759. * @param op operation to start exchange for
  760. */
  761. static void
  762. begin_bf_exchange (struct Operation *op)
  763. {
  764. op->state->phase = PHASE_BF_EXCHANGE;
  765. op->state->my_elements
  766. = GNUNET_CONTAINER_multihashmap_create (op->state->my_element_count,
  767. GNUNET_YES);
  768. GNUNET_CONTAINER_multihashmap_iterate (op->spec->set->elements,
  769. &initialize_map_unfiltered,
  770. op);
  771. send_bloomfilter (op);
  772. }
  773. /**
  774. * Handle the initial `struct IntersectionElementInfoMessage` from a
  775. * remote peer.
  776. *
  777. * @param cls the intersection operation
  778. * @param mh the header of the message
  779. */
  780. static void
  781. handle_p2p_element_info (void *cls,
  782. const struct GNUNET_MessageHeader *mh)
  783. {
  784. struct Operation *op = cls;
  785. const struct IntersectionElementInfoMessage *msg;
  786. if (ntohs (mh->size) != sizeof (struct IntersectionElementInfoMessage))
  787. {
  788. GNUNET_break_op (0);
  789. fail_intersection_operation(op);
  790. return;
  791. }
  792. msg = (const struct IntersectionElementInfoMessage *) mh;
  793. op->spec->remote_element_count = ntohl (msg->sender_element_count);
  794. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  795. "Received remote element count (%u), I have %u\n",
  796. op->spec->remote_element_count,
  797. op->state->my_element_count);
  798. if ( ( (PHASE_INITIAL != op->state->phase) &&
  799. (PHASE_COUNT_SENT != op->state->phase) ) ||
  800. (op->state->my_element_count > op->spec->remote_element_count) ||
  801. (0 == op->state->my_element_count) ||
  802. (0 == op->spec->remote_element_count) )
  803. {
  804. GNUNET_break_op (0);
  805. fail_intersection_operation(op);
  806. return;
  807. }
  808. GNUNET_break (NULL == op->state->remote_bf);
  809. begin_bf_exchange (op);
  810. }
  811. /**
  812. * Send a result message to the client indicating that the operation
  813. * is over. After the result done message has been sent to the
  814. * client, destroy the evaluate operation.
  815. *
  816. * @param op intersection operation
  817. */
  818. static void
  819. finish_and_destroy (struct Operation *op)
  820. {
  821. GNUNET_assert (GNUNET_NO == op->state->client_done_sent);
  822. if (GNUNET_SET_RESULT_FULL == op->spec->result_mode)
  823. {
  824. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  825. "Sending full result set (%u elements)\n",
  826. GNUNET_CONTAINER_multihashmap_size (op->state->my_elements));
  827. op->state->full_result_iter
  828. = GNUNET_CONTAINER_multihashmap_iterator_create (op->state->my_elements);
  829. op->keep++;
  830. send_remaining_elements (op);
  831. return;
  832. }
  833. send_client_done_and_destroy (op);
  834. }
  835. /**
  836. * Remove all elements from our hashmap.
  837. *
  838. * @param cls closure with the `struct Operation *`
  839. * @param key current key code
  840. * @param value value in the hash map
  841. * @return #GNUNET_YES (we should continue to iterate)
  842. */
  843. static int
  844. filter_all (void *cls,
  845. const struct GNUNET_HashCode *key,
  846. void *value)
  847. {
  848. struct Operation *op = cls;
  849. struct ElementEntry *ee = value;
  850. GNUNET_break (0 < op->state->my_element_count);
  851. op->state->my_element_count--;
  852. GNUNET_CRYPTO_hash_xor (&op->state->my_xor,
  853. &ee->element_hash,
  854. &op->state->my_xor);
  855. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  856. "Final reduction of my_elements, removing %s:%u\n",
  857. GNUNET_h2s (&ee->element_hash),
  858. ee->element.size);
  859. GNUNET_assert (GNUNET_YES ==
  860. GNUNET_CONTAINER_multihashmap_remove (op->state->my_elements,
  861. &ee->element_hash,
  862. ee));
  863. send_client_removed_element (op,
  864. &ee->element);
  865. return GNUNET_YES;
  866. }
  867. /**
  868. * Handle a done message from a remote peer
  869. *
  870. * @param cls the intersection operation
  871. * @param mh the message
  872. */
  873. static void
  874. handle_p2p_done (void *cls,
  875. const struct GNUNET_MessageHeader *mh)
  876. {
  877. struct Operation *op = cls;
  878. const struct IntersectionDoneMessage *idm;
  879. if (PHASE_BF_EXCHANGE != op->state->phase)
  880. {
  881. /* wrong phase to conclude? FIXME: Or should we allow this
  882. if the other peer has _initially_ already an empty set? */
  883. GNUNET_break_op (0);
  884. fail_intersection_operation (op);
  885. return;
  886. }
  887. if (ntohs (mh->size) != sizeof (struct IntersectionDoneMessage))
  888. {
  889. GNUNET_break_op (0);
  890. fail_intersection_operation (op);
  891. return;
  892. }
  893. idm = (const struct IntersectionDoneMessage *) mh;
  894. if (0 == ntohl (idm->final_element_count))
  895. {
  896. /* other peer determined empty set is the intersection,
  897. remove all elements */
  898. GNUNET_CONTAINER_multihashmap_iterate (op->state->my_elements,
  899. &filter_all,
  900. op);
  901. }
  902. if ( (op->state->my_element_count != ntohl (idm->final_element_count)) ||
  903. (0 != memcmp (&op->state->my_xor,
  904. &idm->element_xor_hash,
  905. sizeof (struct GNUNET_HashCode))) )
  906. {
  907. /* Other peer thinks we are done, but we disagree on the result! */
  908. GNUNET_break_op (0);
  909. fail_intersection_operation (op);
  910. return;
  911. }
  912. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  913. "Got IntersectionDoneMessage, have %u elements in intersection\n",
  914. op->state->my_element_count);
  915. op->state->phase = PHASE_FINISHED;
  916. finish_and_destroy (op);
  917. }
  918. /**
  919. * Initiate a set intersection operation with a remote peer.
  920. *
  921. * @param op operation that is created, should be initialized to
  922. * begin the evaluation
  923. * @param opaque_context message to be transmitted to the listener
  924. * to convince him to accept, may be NULL
  925. */
  926. static void
  927. intersection_evaluate (struct Operation *op,
  928. const struct GNUNET_MessageHeader *opaque_context)
  929. {
  930. struct GNUNET_MQ_Envelope *ev;
  931. struct OperationRequestMessage *msg;
  932. op->state = GNUNET_new (struct OperationState);
  933. /* we started the operation, thus we have to send the operation request */
  934. op->state->phase = PHASE_INITIAL;
  935. op->state->my_element_count = op->spec->set->state->current_set_element_count;
  936. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  937. "Initiating intersection operation evaluation\n");
  938. ev = GNUNET_MQ_msg_nested_mh (msg,
  939. GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST,
  940. opaque_context);
  941. if (NULL == ev)
  942. {
  943. /* the context message is too large!? */
  944. GNUNET_break (0);
  945. GNUNET_SERVER_client_disconnect (op->spec->set->client);
  946. return;
  947. }
  948. msg->operation = htonl (GNUNET_SET_OPERATION_INTERSECTION);
  949. msg->app_id = op->spec->app_id;
  950. msg->element_count = htonl (op->state->my_element_count);
  951. GNUNET_MQ_send (op->mq,
  952. ev);
  953. op->state->phase = PHASE_COUNT_SENT;
  954. if (NULL != opaque_context)
  955. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  956. "Sent op request with context message\n");
  957. else
  958. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  959. "Sent op request without context message\n");
  960. }
  961. /**
  962. * Accept an intersection operation request from a remote peer. Only
  963. * initializes the private operation state.
  964. *
  965. * @param op operation that will be accepted as an intersection operation
  966. */
  967. static void
  968. intersection_accept (struct Operation *op)
  969. {
  970. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  971. "Accepting set intersection operation\n");
  972. op->state = GNUNET_new (struct OperationState);
  973. op->state->phase = PHASE_INITIAL;
  974. op->state->my_element_count
  975. = op->spec->set->state->current_set_element_count;
  976. op->state->my_elements
  977. = GNUNET_CONTAINER_multihashmap_create
  978. (GNUNET_MIN (op->state->my_element_count,
  979. op->spec->remote_element_count),
  980. GNUNET_YES);
  981. if (op->spec->remote_element_count < op->state->my_element_count)
  982. {
  983. /* If the other peer (Alice) has fewer elements than us (Bob),
  984. we just send the count as Alice should send the first BF */
  985. send_element_count (op);
  986. op->state->phase = PHASE_COUNT_SENT;
  987. return;
  988. }
  989. /* We have fewer elements, so we start with the BF */
  990. begin_bf_exchange (op);
  991. }
  992. /**
  993. * Dispatch messages for a intersection operation.
  994. *
  995. * @param op the state of the intersection evaluate operation
  996. * @param mh the received message
  997. * @return #GNUNET_SYSERR if the tunnel should be disconnected,
  998. * #GNUNET_OK otherwise
  999. */
  1000. static int
  1001. intersection_handle_p2p_message (struct Operation *op,
  1002. const struct GNUNET_MessageHeader *mh)
  1003. {
  1004. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1005. "Received p2p message (t: %u, s: %u)\n",
  1006. ntohs (mh->type), ntohs (mh->size));
  1007. switch (ntohs (mh->type))
  1008. {
  1009. /* this message handler is not active until after we received an
  1010. * operation request message, thus the ops request is not handled here
  1011. */
  1012. case GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_ELEMENT_INFO:
  1013. handle_p2p_element_info (op, mh);
  1014. break;
  1015. case GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_BF:
  1016. handle_p2p_bf (op, mh);
  1017. break;
  1018. case GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_DONE:
  1019. handle_p2p_done (op, mh);
  1020. break;
  1021. default:
  1022. /* something wrong with cadet's message handlers? */
  1023. GNUNET_assert (0);
  1024. }
  1025. return GNUNET_OK;
  1026. }
  1027. /**
  1028. * Handler for peer-disconnects, notifies the client about the aborted
  1029. * operation. If we did not expect anything from the other peer, we
  1030. * gracefully terminate the operation.
  1031. *
  1032. * @param op the destroyed operation
  1033. */
  1034. static void
  1035. intersection_peer_disconnect (struct Operation *op)
  1036. {
  1037. if (PHASE_FINISHED != op->state->phase)
  1038. {
  1039. fail_intersection_operation (op);
  1040. return;
  1041. }
  1042. /* the session has already been concluded */
  1043. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1044. "Other peer disconnected (finished)\n");
  1045. if (GNUNET_NO == op->state->client_done_sent)
  1046. finish_and_destroy (op);
  1047. }
  1048. /**
  1049. * Destroy the intersection operation. Only things specific to the
  1050. * intersection operation are destroyed.
  1051. *
  1052. * @param op intersection operation to destroy
  1053. */
  1054. static void
  1055. intersection_op_cancel (struct Operation *op)
  1056. {
  1057. /* check if the op was canceled twice */
  1058. GNUNET_assert (NULL != op->state);
  1059. if (NULL != op->state->remote_bf)
  1060. {
  1061. GNUNET_CONTAINER_bloomfilter_free (op->state->remote_bf);
  1062. op->state->remote_bf = NULL;
  1063. }
  1064. if (NULL != op->state->local_bf)
  1065. {
  1066. GNUNET_CONTAINER_bloomfilter_free (op->state->local_bf);
  1067. op->state->local_bf = NULL;
  1068. }
  1069. if (NULL != op->state->my_elements)
  1070. {
  1071. GNUNET_CONTAINER_multihashmap_destroy (op->state->my_elements);
  1072. op->state->my_elements = NULL;
  1073. }
  1074. GNUNET_free (op->state);
  1075. op->state = NULL;
  1076. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1077. "Destroying intersection op state done\n");
  1078. }
  1079. /**
  1080. * Create a new set supporting the intersection operation.
  1081. *
  1082. * @return the newly created set
  1083. */
  1084. static struct SetState *
  1085. intersection_set_create ()
  1086. {
  1087. struct SetState *set_state;
  1088. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1089. "Intersection set created\n");
  1090. set_state = GNUNET_new (struct SetState);
  1091. set_state->current_set_element_count = 0;
  1092. return set_state;
  1093. }
  1094. /**
  1095. * Add the element from the given element message to the set.
  1096. *
  1097. * @param set_state state of the set want to add to
  1098. * @param ee the element to add to the set
  1099. */
  1100. static void
  1101. intersection_add (struct SetState *set_state,
  1102. struct ElementEntry *ee)
  1103. {
  1104. set_state->current_set_element_count++;
  1105. }
  1106. /**
  1107. * Destroy a set that supports the intersection operation
  1108. *
  1109. * @param set_state the set to destroy
  1110. */
  1111. static void
  1112. intersection_set_destroy (struct SetState *set_state)
  1113. {
  1114. GNUNET_free (set_state);
  1115. }
  1116. /**
  1117. * Remove the element given in the element message from the set.
  1118. *
  1119. * @param set_state state of the set to remove from
  1120. * @param element set element to remove
  1121. */
  1122. static void
  1123. intersection_remove (struct SetState *set_state,
  1124. struct ElementEntry *element)
  1125. {
  1126. GNUNET_assert (0 < set_state->current_set_element_count);
  1127. set_state->current_set_element_count--;
  1128. }
  1129. /**
  1130. * Get the table with implementing functions for set intersection.
  1131. *
  1132. * @return the operation specific VTable
  1133. */
  1134. const struct SetVT *
  1135. _GSS_intersection_vt ()
  1136. {
  1137. static const struct SetVT intersection_vt = {
  1138. .create = &intersection_set_create,
  1139. .msg_handler = &intersection_handle_p2p_message,
  1140. .add = &intersection_add,
  1141. .remove = &intersection_remove,
  1142. .destroy_set = &intersection_set_destroy,
  1143. .evaluate = &intersection_evaluate,
  1144. .accept = &intersection_accept,
  1145. .peer_disconnect = &intersection_peer_disconnect,
  1146. .cancel = &intersection_op_cancel,
  1147. };
  1148. return &intersection_vt;
  1149. }