diff options
Diffstat (limited to 'src/contrib/service/scalarproduct/scalarproduct_api.c')
-rw-r--r-- | src/contrib/service/scalarproduct/scalarproduct_api.c | 468 |
1 files changed, 468 insertions, 0 deletions
diff --git a/src/contrib/service/scalarproduct/scalarproduct_api.c b/src/contrib/service/scalarproduct/scalarproduct_api.c new file mode 100644 index 000000000..8c667a72e --- /dev/null +++ b/src/contrib/service/scalarproduct/scalarproduct_api.c | |||
@@ -0,0 +1,468 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2013, 2014, 2016 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | /** | ||
21 | * @file scalarproduct/scalarproduct_api.c | ||
22 | * @brief API for the scalarproduct | ||
23 | * @author Christian Fuchs | ||
24 | * @author Gaurav Kukreja | ||
25 | * @author Christian Grothoff | ||
26 | */ | ||
27 | #include "platform.h" | ||
28 | #include "gnunet_util_lib.h" | ||
29 | #include "gnunet_statistics_service.h" | ||
30 | #include "gnunet_scalarproduct_service.h" | ||
31 | #include "gnunet_protocols.h" | ||
32 | #include "scalarproduct.h" | ||
33 | |||
34 | #define LOG(kind, ...) GNUNET_log_from (kind, "scalarproduct-api", __VA_ARGS__) | ||
35 | |||
36 | |||
37 | /** | ||
38 | * The abstraction function for our internal callback | ||
39 | * | ||
40 | * @param h computation handle | ||
41 | * @param msg response we got, NULL on errors | ||
42 | * @param status processing status code | ||
43 | */ | ||
44 | typedef void | ||
45 | (*GNUNET_SCALARPRODUCT_ResponseMessageHandler) ( | ||
46 | struct GNUNET_SCALARPRODUCT_ComputationHandle *h, | ||
47 | const struct ClientResponseMessage *msg, | ||
48 | enum GNUNET_SCALARPRODUCT_ResponseStatus status); | ||
49 | |||
50 | |||
51 | /** | ||
52 | * A handle returned for each computation | ||
53 | */ | ||
54 | struct GNUNET_SCALARPRODUCT_ComputationHandle | ||
55 | { | ||
56 | /** | ||
57 | * Our configuration. | ||
58 | */ | ||
59 | const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
60 | |||
61 | /** | ||
62 | * Current connection to the scalarproduct service. | ||
63 | */ | ||
64 | struct GNUNET_MQ_Handle *mq; | ||
65 | |||
66 | /** | ||
67 | * Function to call after transmission of the request (Bob). | ||
68 | */ | ||
69 | GNUNET_SCALARPRODUCT_ContinuationWithStatus cont_status; | ||
70 | |||
71 | /** | ||
72 | * Function to call after transmission of the request (Alice). | ||
73 | */ | ||
74 | GNUNET_SCALARPRODUCT_DatumProcessor cont_datum; | ||
75 | |||
76 | /** | ||
77 | * Closure for @e cont_status or @e cont_datum. | ||
78 | */ | ||
79 | void *cont_cls; | ||
80 | |||
81 | /** | ||
82 | * API internal callback for results and failures to be forwarded to | ||
83 | * the client. | ||
84 | */ | ||
85 | GNUNET_SCALARPRODUCT_ResponseMessageHandler response_proc; | ||
86 | |||
87 | /** | ||
88 | * The shared session key identifying this computation | ||
89 | */ | ||
90 | struct GNUNET_HashCode key; | ||
91 | }; | ||
92 | |||
93 | |||
94 | /** | ||
95 | * Called when a response is received from the service. Perform basic | ||
96 | * check that the message is well-formed. | ||
97 | * | ||
98 | * @param cls Pointer to the Master Context | ||
99 | * @param message Pointer to the data received in response | ||
100 | * @return #GNUNET_OK if @a message is well-formed | ||
101 | */ | ||
102 | static int | ||
103 | check_response (void *cls, | ||
104 | const struct ClientResponseMessage *message) | ||
105 | { | ||
106 | if (ntohs (message->header.size) != | ||
107 | ntohl (message->product_length) + sizeof(struct ClientResponseMessage)) | ||
108 | { | ||
109 | GNUNET_break (0); | ||
110 | return GNUNET_SYSERR; | ||
111 | } | ||
112 | return GNUNET_OK; | ||
113 | } | ||
114 | |||
115 | |||
116 | /** | ||
117 | * Handles the STATUS received from the service for a response, does | ||
118 | * not contain a payload. Called when we participate as "Bob" via | ||
119 | * #GNUNET_SCALARPRODUCT_accept_computation(). | ||
120 | * | ||
121 | * @param h our Handle | ||
122 | * @param msg the response received | ||
123 | * @param status the condition the request was terminated with (eg: disconnect) | ||
124 | */ | ||
125 | static void | ||
126 | process_status_message (struct GNUNET_SCALARPRODUCT_ComputationHandle *h, | ||
127 | const struct ClientResponseMessage *msg, | ||
128 | enum GNUNET_SCALARPRODUCT_ResponseStatus status) | ||
129 | { | ||
130 | if (NULL != h->cont_status) | ||
131 | h->cont_status (h->cont_cls, | ||
132 | status); | ||
133 | GNUNET_SCALARPRODUCT_cancel (h); | ||
134 | } | ||
135 | |||
136 | |||
137 | /** | ||
138 | * Called when a response is received from the service. After basic | ||
139 | * check, the handler in `h->response_proc` is called. This functions | ||
140 | * handles the response to the client which used the API. | ||
141 | * | ||
142 | * @param cls Pointer to the Master Context | ||
143 | * @param message Pointer to the data received in response | ||
144 | */ | ||
145 | static void | ||
146 | handle_response (void *cls, | ||
147 | const struct ClientResponseMessage *message) | ||
148 | { | ||
149 | struct GNUNET_SCALARPRODUCT_ComputationHandle *h = cls; | ||
150 | enum GNUNET_SCALARPRODUCT_ResponseStatus status; | ||
151 | |||
152 | status = (enum GNUNET_SCALARPRODUCT_ResponseStatus) ntohl (message->status); | ||
153 | h->response_proc (h, | ||
154 | message, | ||
155 | status); | ||
156 | } | ||
157 | |||
158 | |||
159 | /** | ||
160 | * Check if the keys for all given elements are unique. | ||
161 | * | ||
162 | * @param elements elements to check | ||
163 | * @param element_count size of the @a elements array | ||
164 | * @return #GNUNET_OK if all keys are unique | ||
165 | */ | ||
166 | static int | ||
167 | check_unique (const struct GNUNET_SCALARPRODUCT_Element *elements, | ||
168 | uint32_t element_count) | ||
169 | { | ||
170 | struct GNUNET_CONTAINER_MultiHashMap *map; | ||
171 | int ok; | ||
172 | |||
173 | ok = GNUNET_OK; | ||
174 | map = GNUNET_CONTAINER_multihashmap_create (2 * element_count, | ||
175 | GNUNET_YES); | ||
176 | for (uint32_t i = 0; i < element_count; i++) | ||
177 | if (GNUNET_OK != | ||
178 | GNUNET_CONTAINER_multihashmap_put (map, | ||
179 | &elements[i].key, | ||
180 | map, | ||
181 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)) | ||
182 | { | ||
183 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
184 | _ ("Keys given to SCALARPRODUCT not unique!\n")); | ||
185 | ok = GNUNET_SYSERR; | ||
186 | } | ||
187 | GNUNET_CONTAINER_multihashmap_destroy (map); | ||
188 | return ok; | ||
189 | } | ||
190 | |||
191 | |||
192 | /** | ||
193 | * We encountered an error communicating with the set service while | ||
194 | * performing a set operation. Report to the application. | ||
195 | * | ||
196 | * @param cls the `struct GNUNET_SCALARPRODUCT_ComputationHandle` | ||
197 | * @param error error code | ||
198 | */ | ||
199 | static void | ||
200 | mq_error_handler (void *cls, | ||
201 | enum GNUNET_MQ_Error error) | ||
202 | { | ||
203 | struct GNUNET_SCALARPRODUCT_ComputationHandle *h = cls; | ||
204 | |||
205 | LOG (GNUNET_ERROR_TYPE_INFO, | ||
206 | "Disconnected from SCALARPRODUCT service.\n"); | ||
207 | h->response_proc (h, | ||
208 | NULL, | ||
209 | GNUNET_SCALARPRODUCT_STATUS_DISCONNECTED); | ||
210 | } | ||
211 | |||
212 | |||
213 | struct GNUNET_SCALARPRODUCT_ComputationHandle * | ||
214 | GNUNET_SCALARPRODUCT_accept_computation ( | ||
215 | const struct GNUNET_CONFIGURATION_Handle *cfg, | ||
216 | const struct GNUNET_HashCode *session_key, | ||
217 | const struct GNUNET_SCALARPRODUCT_Element *elements, | ||
218 | uint32_t element_count, | ||
219 | GNUNET_SCALARPRODUCT_ContinuationWithStatus cont, | ||
220 | void *cont_cls) | ||
221 | { | ||
222 | struct GNUNET_SCALARPRODUCT_ComputationHandle *h | ||
223 | = GNUNET_new (struct GNUNET_SCALARPRODUCT_ComputationHandle); | ||
224 | struct GNUNET_MQ_MessageHandler handlers[] = { | ||
225 | GNUNET_MQ_hd_var_size (response, | ||
226 | GNUNET_MESSAGE_TYPE_SCALARPRODUCT_RESULT, | ||
227 | struct ClientResponseMessage, | ||
228 | h), | ||
229 | GNUNET_MQ_handler_end () | ||
230 | }; | ||
231 | struct GNUNET_MQ_Envelope *env; | ||
232 | struct BobComputationMessage *msg; | ||
233 | struct ComputationBobCryptodataMultipartMessage *mmsg; | ||
234 | uint32_t size; | ||
235 | uint16_t possible; | ||
236 | uint16_t todo; | ||
237 | uint32_t element_count_transfered; | ||
238 | |||
239 | |||
240 | if (GNUNET_SYSERR == check_unique (elements, | ||
241 | element_count)) | ||
242 | return NULL; | ||
243 | h->cont_status = cont; | ||
244 | h->cont_cls = cont_cls; | ||
245 | h->response_proc = &process_status_message; | ||
246 | h->cfg = cfg; | ||
247 | h->key = *session_key; | ||
248 | h->mq = GNUNET_CLIENT_connect (cfg, | ||
249 | "scalarproduct-bob", | ||
250 | handlers, | ||
251 | &mq_error_handler, | ||
252 | h); | ||
253 | if (NULL == h->mq) | ||
254 | { | ||
255 | /* scalarproduct configuration error */ | ||
256 | GNUNET_break (0); | ||
257 | GNUNET_free (h); | ||
258 | return NULL; | ||
259 | } | ||
260 | possible = (GNUNET_MAX_MESSAGE_SIZE - 1 - sizeof(struct | ||
261 | BobComputationMessage)) | ||
262 | / sizeof(struct GNUNET_SCALARPRODUCT_Element); | ||
263 | todo = GNUNET_MIN (possible, | ||
264 | element_count); | ||
265 | size = todo * sizeof(struct GNUNET_SCALARPRODUCT_Element); | ||
266 | env = GNUNET_MQ_msg_extra (msg, | ||
267 | size, | ||
268 | GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_BOB); | ||
269 | msg->element_count_total = htonl (element_count); | ||
270 | msg->element_count_contained = htonl (todo); | ||
271 | msg->session_key = *session_key; | ||
272 | GNUNET_memcpy (&msg[1], | ||
273 | elements, | ||
274 | size); | ||
275 | element_count_transfered = todo; | ||
276 | GNUNET_MQ_send (h->mq, | ||
277 | env); | ||
278 | possible = (GNUNET_MAX_MESSAGE_SIZE - 1 - sizeof(*mmsg)) | ||
279 | / sizeof(struct GNUNET_SCALARPRODUCT_Element); | ||
280 | while (element_count_transfered < element_count) | ||
281 | { | ||
282 | todo = GNUNET_MIN (possible, | ||
283 | element_count - element_count_transfered); | ||
284 | size = todo * sizeof(struct GNUNET_SCALARPRODUCT_Element); | ||
285 | env = GNUNET_MQ_msg_extra (mmsg, | ||
286 | size, | ||
287 | GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MULTIPART_BOB); | ||
288 | mmsg->element_count_contained = htonl (todo); | ||
289 | GNUNET_memcpy (&mmsg[1], | ||
290 | &elements[element_count_transfered], | ||
291 | size); | ||
292 | element_count_transfered += todo; | ||
293 | GNUNET_MQ_send (h->mq, | ||
294 | env); | ||
295 | } | ||
296 | return h; | ||
297 | } | ||
298 | |||
299 | |||
300 | /** | ||
301 | * Handles the RESULT received from the service for a request, should | ||
302 | * contain a result MPI value. Called when we participate as "Alice" via | ||
303 | * #GNUNET_SCALARPRODUCT_start_computation(). | ||
304 | * | ||
305 | * @param h our Handle | ||
306 | * @param msg Pointer to the response received | ||
307 | * @param status the condition the request was terminated with (eg: disconnect) | ||
308 | */ | ||
309 | static void | ||
310 | process_result_message (struct GNUNET_SCALARPRODUCT_ComputationHandle *h, | ||
311 | const struct ClientResponseMessage *msg, | ||
312 | enum GNUNET_SCALARPRODUCT_ResponseStatus status) | ||
313 | { | ||
314 | uint32_t product_len; | ||
315 | gcry_mpi_t result = NULL; | ||
316 | gcry_error_t rc; | ||
317 | gcry_mpi_t num; | ||
318 | size_t rsize; | ||
319 | |||
320 | if (GNUNET_SCALARPRODUCT_STATUS_SUCCESS == status) | ||
321 | { | ||
322 | result = gcry_mpi_new (0); | ||
323 | |||
324 | product_len = ntohl (msg->product_length); | ||
325 | if (0 < product_len) | ||
326 | { | ||
327 | rsize = 0; | ||
328 | if (0 != (rc = gcry_mpi_scan (&num, GCRYMPI_FMT_STD, | ||
329 | &msg[1], | ||
330 | product_len, | ||
331 | &rsize))) | ||
332 | { | ||
333 | LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, | ||
334 | "gcry_mpi_scan", | ||
335 | rc); | ||
336 | gcry_mpi_release (result); | ||
337 | result = NULL; | ||
338 | status = GNUNET_SCALARPRODUCT_STATUS_INVALID_RESPONSE; | ||
339 | } | ||
340 | else | ||
341 | { | ||
342 | if (0 < (int32_t) ntohl (msg->range)) | ||
343 | gcry_mpi_add (result, result, num); | ||
344 | else | ||
345 | gcry_mpi_sub (result, result, num); | ||
346 | gcry_mpi_release (num); | ||
347 | } | ||
348 | } | ||
349 | } | ||
350 | if (NULL != h->cont_datum) | ||
351 | h->cont_datum (h->cont_cls, | ||
352 | status, | ||
353 | result); | ||
354 | if (NULL != result) | ||
355 | gcry_mpi_release (result); | ||
356 | GNUNET_SCALARPRODUCT_cancel (h); | ||
357 | } | ||
358 | |||
359 | |||
360 | struct GNUNET_SCALARPRODUCT_ComputationHandle * | ||
361 | GNUNET_SCALARPRODUCT_start_computation ( | ||
362 | const struct GNUNET_CONFIGURATION_Handle *cfg, | ||
363 | const struct GNUNET_HashCode *session_key, | ||
364 | const struct GNUNET_PeerIdentity *peer, | ||
365 | const struct GNUNET_SCALARPRODUCT_Element *elements, | ||
366 | uint32_t element_count, | ||
367 | GNUNET_SCALARPRODUCT_DatumProcessor cont, | ||
368 | void *cont_cls) | ||
369 | { | ||
370 | struct GNUNET_SCALARPRODUCT_ComputationHandle *h | ||
371 | = GNUNET_new (struct GNUNET_SCALARPRODUCT_ComputationHandle); | ||
372 | struct GNUNET_MQ_MessageHandler handlers[] = { | ||
373 | GNUNET_MQ_hd_var_size (response, | ||
374 | GNUNET_MESSAGE_TYPE_SCALARPRODUCT_RESULT, | ||
375 | struct ClientResponseMessage, | ||
376 | h), | ||
377 | GNUNET_MQ_handler_end () | ||
378 | }; | ||
379 | struct GNUNET_MQ_Envelope *env; | ||
380 | struct AliceComputationMessage *msg; | ||
381 | struct ComputationBobCryptodataMultipartMessage *mmsg; | ||
382 | uint32_t size; | ||
383 | uint16_t possible; | ||
384 | uint16_t todo; | ||
385 | uint32_t element_count_transfered; | ||
386 | |||
387 | if (GNUNET_SYSERR == check_unique (elements, | ||
388 | element_count)) | ||
389 | return NULL; | ||
390 | h->mq = GNUNET_CLIENT_connect (cfg, | ||
391 | "scalarproduct-alice", | ||
392 | handlers, | ||
393 | &mq_error_handler, | ||
394 | h); | ||
395 | if (NULL == h->mq) | ||
396 | { | ||
397 | /* misconfigured scalarproduct service */ | ||
398 | GNUNET_break (0); | ||
399 | GNUNET_free (h); | ||
400 | return NULL; | ||
401 | } | ||
402 | h->cont_datum = cont; | ||
403 | h->cont_cls = cont_cls; | ||
404 | h->response_proc = &process_result_message; | ||
405 | h->cfg = cfg; | ||
406 | h->key = *session_key; | ||
407 | |||
408 | possible = (GNUNET_MAX_MESSAGE_SIZE - 1 - sizeof(struct | ||
409 | AliceComputationMessage)) | ||
410 | / sizeof(struct GNUNET_SCALARPRODUCT_Element); | ||
411 | todo = GNUNET_MIN (possible, | ||
412 | element_count); | ||
413 | size = todo * sizeof(struct GNUNET_SCALARPRODUCT_Element); | ||
414 | env = GNUNET_MQ_msg_extra (msg, | ||
415 | size, | ||
416 | GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_ALICE); | ||
417 | msg->element_count_total = htonl (element_count); | ||
418 | msg->element_count_contained = htonl (todo); | ||
419 | msg->reserved = htonl (0); | ||
420 | msg->peer = *peer; | ||
421 | msg->session_key = *session_key; | ||
422 | GNUNET_memcpy (&msg[1], | ||
423 | elements, | ||
424 | size); | ||
425 | GNUNET_MQ_send (h->mq, | ||
426 | env); | ||
427 | element_count_transfered = todo; | ||
428 | possible = (GNUNET_MAX_MESSAGE_SIZE - 1 - sizeof(*mmsg)) | ||
429 | / sizeof(struct GNUNET_SCALARPRODUCT_Element); | ||
430 | while (element_count_transfered < element_count) | ||
431 | { | ||
432 | todo = GNUNET_MIN (possible, | ||
433 | element_count - element_count_transfered); | ||
434 | size = todo * sizeof(struct GNUNET_SCALARPRODUCT_Element); | ||
435 | env = GNUNET_MQ_msg_extra (mmsg, | ||
436 | size, | ||
437 | GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MULTIPART_ALICE); | ||
438 | mmsg->element_count_contained = htonl (todo); | ||
439 | GNUNET_memcpy (&mmsg[1], | ||
440 | &elements[element_count_transfered], | ||
441 | size); | ||
442 | element_count_transfered += todo; | ||
443 | GNUNET_MQ_send (h->mq, | ||
444 | env); | ||
445 | } | ||
446 | return h; | ||
447 | } | ||
448 | |||
449 | |||
450 | /** | ||
451 | * Cancel an ongoing computation or revoke our collaboration offer. | ||
452 | * Closes the connection to the service | ||
453 | * | ||
454 | * @param h computation handle to terminate | ||
455 | */ | ||
456 | void | ||
457 | GNUNET_SCALARPRODUCT_cancel (struct GNUNET_SCALARPRODUCT_ComputationHandle *h) | ||
458 | { | ||
459 | if (NULL != h->mq) | ||
460 | { | ||
461 | GNUNET_MQ_destroy (h->mq); | ||
462 | h->mq = NULL; | ||
463 | } | ||
464 | GNUNET_free (h); | ||
465 | } | ||
466 | |||
467 | |||
468 | /* end of scalarproduct_api.c */ | ||