scalarproduct_api.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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 scalarproduct/scalarproduct_api.c
  19. * @brief API for the scalarproduct
  20. * @author Christian Fuchs
  21. * @author Gaurav Kukreja
  22. * @author Christian Grothoff
  23. */
  24. #include "platform.h"
  25. #include "gnunet_util_lib.h"
  26. #include "gnunet_statistics_service.h"
  27. #include "gnunet_scalarproduct_service.h"
  28. #include "gnunet_protocols.h"
  29. #include "scalarproduct.h"
  30. #define LOG(kind,...) GNUNET_log_from (kind, "scalarproduct-api",__VA_ARGS__)
  31. /**
  32. * The abstraction function for our internal callback
  33. *
  34. * @param h computation handle
  35. * @param msg response we got, NULL on errors
  36. * @param status processing status code
  37. */
  38. typedef void
  39. (*GNUNET_SCALARPRODUCT_ResponseMessageHandler) (struct GNUNET_SCALARPRODUCT_ComputationHandle *h,
  40. const struct ClientResponseMessage *msg,
  41. enum GNUNET_SCALARPRODUCT_ResponseStatus status);
  42. /**
  43. * A handle returned for each computation
  44. */
  45. struct GNUNET_SCALARPRODUCT_ComputationHandle
  46. {
  47. /**
  48. * Our configuration.
  49. */
  50. const struct GNUNET_CONFIGURATION_Handle *cfg;
  51. /**
  52. * Current connection to the scalarproduct service.
  53. */
  54. struct GNUNET_CLIENT_Connection *client;
  55. /**
  56. * Current transmit handle.
  57. */
  58. struct GNUNET_CLIENT_TransmitHandle *th;
  59. /**
  60. * the client's elements which
  61. */
  62. struct GNUNET_SCALARPRODUCT_Element *elements;
  63. /**
  64. * Message to be sent to the scalarproduct service
  65. */
  66. struct GNUNET_MessageHeader *msg;
  67. /**
  68. * Function to call after transmission of the request (Bob).
  69. */
  70. GNUNET_SCALARPRODUCT_ContinuationWithStatus cont_status;
  71. /**
  72. * Function to call after transmission of the request (Alice).
  73. */
  74. GNUNET_SCALARPRODUCT_DatumProcessor cont_datum;
  75. /**
  76. * Closure for @e cont_status or @e cont_datum.
  77. */
  78. void *cont_cls;
  79. /**
  80. * API internal callback for results and failures to be forwarded to
  81. * the client.
  82. */
  83. GNUNET_SCALARPRODUCT_ResponseMessageHandler response_proc;
  84. /**
  85. * The shared session key identifying this computation
  86. */
  87. struct GNUNET_HashCode key;
  88. /**
  89. * count of all @e elements we offer for computation
  90. */
  91. uint32_t element_count_total;
  92. /**
  93. * count of the transfered @e elements we offer for computation
  94. */
  95. uint32_t element_count_transfered;
  96. };
  97. /**
  98. * Handles the STATUS received from the service for a response, does
  99. * not contain a payload. Called when we participate as "Bob" via
  100. * #GNUNET_SCALARPRODUCT_accept_computation().
  101. *
  102. * @param h our Handle
  103. * @param msg the response received
  104. * @param status the condition the request was terminated with (eg: disconnect)
  105. */
  106. static void
  107. process_status_message (struct GNUNET_SCALARPRODUCT_ComputationHandle *h,
  108. const struct ClientResponseMessage *msg,
  109. enum GNUNET_SCALARPRODUCT_ResponseStatus status)
  110. {
  111. if (NULL != h->cont_status)
  112. h->cont_status (h->cont_cls,
  113. status);
  114. GNUNET_SCALARPRODUCT_cancel (h);
  115. }
  116. /**
  117. * Handles the RESULT received from the service for a request, should
  118. * contain a result MPI value. Called when we participate as "Alice" via
  119. * #GNUNET_SCALARPRODUCT_start_computation().
  120. *
  121. * @param h our Handle
  122. * @param msg Pointer to the response received
  123. * @param status the condition the request was terminated with (eg: disconnect)
  124. */
  125. static void
  126. process_result_message (struct GNUNET_SCALARPRODUCT_ComputationHandle *h,
  127. const struct ClientResponseMessage *msg,
  128. enum GNUNET_SCALARPRODUCT_ResponseStatus status)
  129. {
  130. size_t product_len;
  131. gcry_mpi_t result = NULL;
  132. gcry_error_t rc;
  133. gcry_mpi_t num;
  134. size_t rsize;
  135. if ( (GNUNET_SCALARPRODUCT_Status_Success == status) &&
  136. ( (NULL == msg) ||
  137. ( (ntohs (msg->header.size) - sizeof (struct ClientResponseMessage)
  138. != (product_len = ntohl (msg->product_length))) ) ) )
  139. {
  140. GNUNET_break (0);
  141. status = GNUNET_SCALARPRODUCT_Status_InvalidResponse;
  142. }
  143. if (GNUNET_SCALARPRODUCT_Status_Success == status)
  144. {
  145. result = gcry_mpi_new (0);
  146. if (0 < product_len)
  147. {
  148. rsize = 0;
  149. if (0 != (rc = gcry_mpi_scan (&num, GCRYMPI_FMT_STD,
  150. &msg[1],
  151. product_len,
  152. &rsize)))
  153. {
  154. LOG_GCRY (GNUNET_ERROR_TYPE_ERROR,
  155. "gcry_mpi_scan",
  156. rc);
  157. gcry_mpi_release (result);
  158. result = NULL;
  159. status = GNUNET_SCALARPRODUCT_Status_InvalidResponse;
  160. }
  161. else
  162. {
  163. if (0 < ntohl (msg->range))
  164. gcry_mpi_add (result, result, num);
  165. else if (0 > ntohl (msg->range))
  166. gcry_mpi_sub (result, result, num);
  167. gcry_mpi_release (num);
  168. }
  169. }
  170. }
  171. h->cont_datum (h->cont_cls,
  172. status,
  173. result);
  174. if (NULL != result)
  175. gcry_mpi_release (result);
  176. GNUNET_SCALARPRODUCT_cancel (h);
  177. }
  178. /**
  179. * Called when a response is received from the service. After basic check, the
  180. * handler in qe->response_proc is called. This functions handles the response
  181. * to the client which used the API.
  182. *
  183. * @param cls Pointer to the Master Context
  184. * @param msg Pointer to the data received in response
  185. */
  186. static void
  187. receive_cb (void *cls,
  188. const struct GNUNET_MessageHeader *msg)
  189. {
  190. struct GNUNET_SCALARPRODUCT_ComputationHandle *h = cls;
  191. const struct ClientResponseMessage *message;
  192. if (NULL == msg)
  193. {
  194. LOG (GNUNET_ERROR_TYPE_INFO,
  195. "Disconnected from SCALARPRODUCT service.\n");
  196. h->response_proc (h,
  197. NULL,
  198. GNUNET_SCALARPRODUCT_Status_ServiceDisconnected);
  199. return;
  200. }
  201. if (ntohs (msg->size) != sizeof (struct ClientResponseMessage))
  202. {
  203. GNUNET_break (0);
  204. h->response_proc (h,
  205. NULL,
  206. GNUNET_SCALARPRODUCT_Status_InvalidResponse);
  207. return;
  208. }
  209. message = (const struct ClientResponseMessage *) msg;
  210. if (GNUNET_SYSERR == ntohl (message->status))
  211. {
  212. h->response_proc (h,
  213. NULL,
  214. GNUNET_SCALARPRODUCT_Status_Failure);
  215. return;
  216. }
  217. h->response_proc (h,
  218. message,
  219. GNUNET_SCALARPRODUCT_Status_Success);
  220. }
  221. /**
  222. * Transmits the request to the SCALARPRODUCT service
  223. *
  224. * @param cls Closure with the `struct GNUNET_SCALARPRODUCT_ComputationHandle`
  225. * @param size Size of the buffer @a buf
  226. * @param buf Pointer to the buffer
  227. * @return Size of the message sent
  228. */
  229. static size_t
  230. do_send_message (void *cls,
  231. size_t size,
  232. void *buf)
  233. {
  234. struct GNUNET_SCALARPRODUCT_ComputationHandle *h = cls;
  235. struct ComputationMultipartMessage *msg;
  236. size_t ret;
  237. uint32_t nsize;
  238. uint32_t todo;
  239. h->th = NULL;
  240. if (NULL == buf)
  241. {
  242. LOG (GNUNET_ERROR_TYPE_DEBUG,
  243. "Failed to transmit request to SCALARPRODUCT.\n");
  244. /* notify caller about the error, done here */
  245. h->response_proc (h, NULL,
  246. GNUNET_SCALARPRODUCT_Status_Failure);
  247. return 0;
  248. }
  249. ret = ntohs (h->msg->size);
  250. memcpy (buf, h->msg, ret);
  251. GNUNET_free (h->msg);
  252. h->msg = NULL;
  253. /* done sending? */
  254. if (h->element_count_total == h->element_count_transfered)
  255. {
  256. GNUNET_CLIENT_receive (h->client,
  257. &receive_cb, h,
  258. GNUNET_TIME_UNIT_FOREVER_REL);
  259. return ret;
  260. }
  261. todo = h->element_count_total - h->element_count_transfered;
  262. nsize = sizeof (struct ComputationMultipartMessage)
  263. + todo * sizeof (struct GNUNET_SCALARPRODUCT_Element);
  264. if (GNUNET_SERVER_MAX_MESSAGE_SIZE <= size)
  265. {
  266. /* cannot do all of them, limit to what is possible in one message */
  267. todo = (GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - sizeof (struct ComputationMultipartMessage))
  268. / sizeof (struct GNUNET_SCALARPRODUCT_Element);
  269. nsize = sizeof (struct ComputationMultipartMessage)
  270. + todo * sizeof (struct GNUNET_SCALARPRODUCT_Element);
  271. }
  272. msg = GNUNET_malloc (nsize);
  273. h->msg = &msg->header;
  274. msg->header.size = htons (nsize);
  275. msg->header.type = htons (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MUTLIPART);
  276. msg->element_count_contained = htonl (todo);
  277. memcpy (&msg[1],
  278. &h->elements[h->element_count_transfered],
  279. todo * sizeof (struct GNUNET_SCALARPRODUCT_Element));
  280. h->element_count_transfered += todo;
  281. h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, nsize,
  282. GNUNET_TIME_UNIT_FOREVER_REL,
  283. GNUNET_NO,
  284. &do_send_message, h);
  285. GNUNET_assert (NULL != h->th);
  286. return ret;
  287. }
  288. /**
  289. * Used by Bob's client to cooperate with Alice,
  290. *
  291. * @param cfg the gnunet configuration handle
  292. * @param key Session key unique to the requesting client
  293. * @param elements Array of elements of the vector
  294. * @param element_count Number of elements in the @a elements vector
  295. * @param cont Callback function
  296. * @param cont_cls Closure for @a cont
  297. * @return a new handle for this computation
  298. */
  299. struct GNUNET_SCALARPRODUCT_ComputationHandle *
  300. GNUNET_SCALARPRODUCT_accept_computation (const struct GNUNET_CONFIGURATION_Handle *cfg,
  301. const struct GNUNET_HashCode *session_key,
  302. const struct GNUNET_SCALARPRODUCT_Element *elements,
  303. uint32_t element_count,
  304. GNUNET_SCALARPRODUCT_ContinuationWithStatus cont,
  305. void *cont_cls)
  306. {
  307. struct GNUNET_SCALARPRODUCT_ComputationHandle *h;
  308. struct ComputationMessage *msg;
  309. uint32_t size;
  310. uint16_t possible;
  311. h = GNUNET_new (struct GNUNET_SCALARPRODUCT_ComputationHandle);
  312. h->cont_status = cont;
  313. h->cont_cls = cont_cls;
  314. h->response_proc = &process_status_message;
  315. h->cfg = cfg;
  316. h->key = *session_key;
  317. h->client = GNUNET_CLIENT_connect ("scalarproduct", cfg);
  318. h->element_count_total = element_count;
  319. if (NULL == h->client)
  320. {
  321. /* scalarproduct configuration error */
  322. GNUNET_break (0);
  323. GNUNET_free (h);
  324. return NULL;
  325. }
  326. size = sizeof (struct ComputationMessage)
  327. + element_count * sizeof (struct GNUNET_SCALARPRODUCT_Element);
  328. if (GNUNET_SERVER_MAX_MESSAGE_SIZE > size)
  329. {
  330. possible = element_count;
  331. h->element_count_transfered = element_count;
  332. }
  333. else
  334. {
  335. /* create a multipart msg, first we calculate a new msg size for the head msg */
  336. possible = (GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - sizeof (struct ComputationMessage))
  337. / sizeof (struct GNUNET_SCALARPRODUCT_Element);
  338. h->element_count_transfered = possible;
  339. size = sizeof (struct ComputationMessage)
  340. + possible * sizeof (struct GNUNET_SCALARPRODUCT_Element);
  341. h->elements = GNUNET_malloc (sizeof(struct GNUNET_SCALARPRODUCT_Element) * element_count);
  342. memcpy (h->elements,
  343. elements,
  344. sizeof (struct GNUNET_SCALARPRODUCT_Element) * element_count);
  345. }
  346. msg = GNUNET_malloc (size);
  347. h->msg = &msg->header;
  348. msg->header.size = htons (size);
  349. msg->header.type = htons (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_BOB);
  350. msg->element_count_total = htonl (element_count);
  351. msg->element_count_contained = htonl (possible);
  352. msg->session_key = *session_key;
  353. memcpy (&msg[1],
  354. elements,
  355. possible * sizeof (struct GNUNET_SCALARPRODUCT_Element));
  356. h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, size,
  357. GNUNET_TIME_UNIT_FOREVER_REL,
  358. GNUNET_YES, /* retry is OK in the initial stage */
  359. &do_send_message, h);
  360. GNUNET_assert (NULL != h->th);
  361. return h;
  362. }
  363. /**
  364. * Request by Alice's client for computing a scalar product
  365. *
  366. * @param cfg the gnunet configuration handle
  367. * @param session_key Session key should be unique to the requesting client
  368. * @param peer PeerID of the other peer
  369. * @param elements Array of elements of the vector
  370. * @param element_count Number of elements in the @a elements vector
  371. * @param cont Callback function
  372. * @param cont_cls Closure for @a cont
  373. * @return a new handle for this computation
  374. */
  375. struct GNUNET_SCALARPRODUCT_ComputationHandle *
  376. GNUNET_SCALARPRODUCT_start_computation (const struct GNUNET_CONFIGURATION_Handle *cfg,
  377. const struct GNUNET_HashCode *session_key,
  378. const struct GNUNET_PeerIdentity *peer,
  379. const struct GNUNET_SCALARPRODUCT_Element *elements,
  380. uint32_t element_count,
  381. GNUNET_SCALARPRODUCT_DatumProcessor cont,
  382. void *cont_cls)
  383. {
  384. struct GNUNET_SCALARPRODUCT_ComputationHandle *h;
  385. struct ComputationMessage *msg;
  386. uint32_t size;
  387. uint32_t possible;
  388. h = GNUNET_new (struct GNUNET_SCALARPRODUCT_ComputationHandle);
  389. h->client = GNUNET_CLIENT_connect ("scalarproduct", cfg);
  390. if (NULL == h->client)
  391. {
  392. /* missconfigured scalarproduct service */
  393. GNUNET_break (0);
  394. GNUNET_free (h);
  395. return NULL;
  396. }
  397. h->element_count_total = element_count;
  398. h->cont_datum = cont;
  399. h->cont_cls = cont_cls;
  400. h->response_proc = &process_result_message;
  401. h->cfg = cfg;
  402. h->key = *session_key;
  403. size = sizeof (struct ComputationMessage)
  404. + element_count * sizeof (struct GNUNET_SCALARPRODUCT_Element);
  405. if (GNUNET_SERVER_MAX_MESSAGE_SIZE > size)
  406. {
  407. possible = element_count;
  408. h->element_count_transfered = element_count;
  409. }
  410. else
  411. {
  412. /* create a multipart msg, first we calculate a new msg size for the head msg */
  413. possible = (GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - sizeof (struct ComputationMessage))
  414. / sizeof (struct GNUNET_SCALARPRODUCT_Element);
  415. h->element_count_transfered = possible;
  416. size = sizeof (struct ComputationMessage)
  417. + possible * sizeof (struct GNUNET_SCALARPRODUCT_Element);
  418. h->elements = GNUNET_malloc (sizeof(struct GNUNET_SCALARPRODUCT_Element) * element_count);
  419. memcpy (h->elements,
  420. elements,
  421. sizeof (struct GNUNET_SCALARPRODUCT_Element) * element_count);
  422. }
  423. msg = GNUNET_malloc (size);
  424. h->msg = &msg->header;
  425. msg->header.size = htons (size);
  426. msg->header.type = htons (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_ALICE);
  427. msg->element_count_total = htonl (element_count);
  428. msg->element_count_contained = htonl (possible);
  429. msg->reserved = htonl (0);
  430. msg->peer = *peer;
  431. msg->session_key = *session_key;
  432. memcpy (&msg[1],
  433. elements,
  434. sizeof (struct GNUNET_SCALARPRODUCT_Element) * possible);
  435. h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, size,
  436. GNUNET_TIME_UNIT_FOREVER_REL,
  437. GNUNET_YES, /* retry is OK in the initial stage */
  438. &do_send_message, h);
  439. GNUNET_assert (NULL != h->th);
  440. return h;
  441. }
  442. /**
  443. * Cancel an ongoing computation or revoke our collaboration offer.
  444. * Closes the connection to the service
  445. *
  446. * @param h computation handle to terminate
  447. */
  448. void
  449. GNUNET_SCALARPRODUCT_cancel (struct GNUNET_SCALARPRODUCT_ComputationHandle *h)
  450. {
  451. if (NULL != h->th)
  452. {
  453. GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
  454. h->th = NULL;
  455. }
  456. GNUNET_free_non_null (h->elements);
  457. GNUNET_free_non_null (h->msg);
  458. if (NULL != h->client)
  459. {
  460. GNUNET_CLIENT_disconnect (h->client);
  461. h->client = NULL;
  462. }
  463. GNUNET_free (h);
  464. }
  465. /* end of scalarproduct_api.c */