diff options
Diffstat (limited to 'src/scalarproduct/scalarproduct_api.c')
-rw-r--r-- | src/scalarproduct/scalarproduct_api.c | 491 |
1 files changed, 0 insertions, 491 deletions
diff --git a/src/scalarproduct/scalarproduct_api.c b/src/scalarproduct/scalarproduct_api.c deleted file mode 100644 index 44d6bd812..000000000 --- a/src/scalarproduct/scalarproduct_api.c +++ /dev/null | |||
@@ -1,491 +0,0 @@ | |||
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 msg 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 | /** | ||
214 | * Used by Bob's client to cooperate with Alice, | ||
215 | * | ||
216 | * @param cfg the gnunet configuration handle | ||
217 | * @param key Session key unique to the requesting client | ||
218 | * @param elements Array of elements of the vector | ||
219 | * @param element_count Number of elements in the @a elements vector | ||
220 | * @param cont Callback function | ||
221 | * @param cont_cls Closure for @a cont | ||
222 | * @return a new handle for this computation | ||
223 | */ | ||
224 | struct GNUNET_SCALARPRODUCT_ComputationHandle * | ||
225 | GNUNET_SCALARPRODUCT_accept_computation ( | ||
226 | const struct GNUNET_CONFIGURATION_Handle *cfg, | ||
227 | const struct GNUNET_HashCode *session_key, | ||
228 | const struct GNUNET_SCALARPRODUCT_Element *elements, | ||
229 | uint32_t element_count, | ||
230 | GNUNET_SCALARPRODUCT_ContinuationWithStatus cont, | ||
231 | void *cont_cls) | ||
232 | { | ||
233 | struct GNUNET_SCALARPRODUCT_ComputationHandle *h | ||
234 | = GNUNET_new (struct GNUNET_SCALARPRODUCT_ComputationHandle); | ||
235 | struct GNUNET_MQ_MessageHandler handlers[] = { | ||
236 | GNUNET_MQ_hd_var_size (response, | ||
237 | GNUNET_MESSAGE_TYPE_SCALARPRODUCT_RESULT, | ||
238 | struct ClientResponseMessage, | ||
239 | h), | ||
240 | GNUNET_MQ_handler_end () | ||
241 | }; | ||
242 | struct GNUNET_MQ_Envelope *env; | ||
243 | struct BobComputationMessage *msg; | ||
244 | struct ComputationBobCryptodataMultipartMessage *mmsg; | ||
245 | uint32_t size; | ||
246 | uint16_t possible; | ||
247 | uint16_t todo; | ||
248 | uint32_t element_count_transfered; | ||
249 | |||
250 | |||
251 | if (GNUNET_SYSERR == check_unique (elements, | ||
252 | element_count)) | ||
253 | return NULL; | ||
254 | h->cont_status = cont; | ||
255 | h->cont_cls = cont_cls; | ||
256 | h->response_proc = &process_status_message; | ||
257 | h->cfg = cfg; | ||
258 | h->key = *session_key; | ||
259 | h->mq = GNUNET_CLIENT_connect (cfg, | ||
260 | "scalarproduct-bob", | ||
261 | handlers, | ||
262 | &mq_error_handler, | ||
263 | h); | ||
264 | if (NULL == h->mq) | ||
265 | { | ||
266 | /* scalarproduct configuration error */ | ||
267 | GNUNET_break (0); | ||
268 | GNUNET_free (h); | ||
269 | return NULL; | ||
270 | } | ||
271 | possible = (GNUNET_MAX_MESSAGE_SIZE - 1 - sizeof(struct | ||
272 | BobComputationMessage)) | ||
273 | / sizeof(struct GNUNET_SCALARPRODUCT_Element); | ||
274 | todo = GNUNET_MIN (possible, | ||
275 | element_count); | ||
276 | size = todo * sizeof(struct GNUNET_SCALARPRODUCT_Element); | ||
277 | env = GNUNET_MQ_msg_extra (msg, | ||
278 | size, | ||
279 | GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_BOB); | ||
280 | msg->element_count_total = htonl (element_count); | ||
281 | msg->element_count_contained = htonl (todo); | ||
282 | msg->session_key = *session_key; | ||
283 | GNUNET_memcpy (&msg[1], | ||
284 | elements, | ||
285 | size); | ||
286 | element_count_transfered = todo; | ||
287 | GNUNET_MQ_send (h->mq, | ||
288 | env); | ||
289 | possible = (GNUNET_MAX_MESSAGE_SIZE - 1 - sizeof(*mmsg)) | ||
290 | / sizeof(struct GNUNET_SCALARPRODUCT_Element); | ||
291 | while (element_count_transfered < element_count) | ||
292 | { | ||
293 | todo = GNUNET_MIN (possible, | ||
294 | element_count - element_count_transfered); | ||
295 | size = todo * sizeof(struct GNUNET_SCALARPRODUCT_Element); | ||
296 | env = GNUNET_MQ_msg_extra (mmsg, | ||
297 | size, | ||
298 | GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MULTIPART_BOB); | ||
299 | mmsg->element_count_contained = htonl (todo); | ||
300 | GNUNET_memcpy (&mmsg[1], | ||
301 | &elements[element_count_transfered], | ||
302 | size); | ||
303 | element_count_transfered += todo; | ||
304 | GNUNET_MQ_send (h->mq, | ||
305 | env); | ||
306 | } | ||
307 | return h; | ||
308 | } | ||
309 | |||
310 | |||
311 | /** | ||
312 | * Handles the RESULT received from the service for a request, should | ||
313 | * contain a result MPI value. Called when we participate as "Alice" via | ||
314 | * #GNUNET_SCALARPRODUCT_start_computation(). | ||
315 | * | ||
316 | * @param h our Handle | ||
317 | * @param msg Pointer to the response received | ||
318 | * @param status the condition the request was terminated with (eg: disconnect) | ||
319 | */ | ||
320 | static void | ||
321 | process_result_message (struct GNUNET_SCALARPRODUCT_ComputationHandle *h, | ||
322 | const struct ClientResponseMessage *msg, | ||
323 | enum GNUNET_SCALARPRODUCT_ResponseStatus status) | ||
324 | { | ||
325 | uint32_t product_len; | ||
326 | gcry_mpi_t result = NULL; | ||
327 | gcry_error_t rc; | ||
328 | gcry_mpi_t num; | ||
329 | size_t rsize; | ||
330 | |||
331 | if (GNUNET_SCALARPRODUCT_STATUS_SUCCESS == status) | ||
332 | { | ||
333 | result = gcry_mpi_new (0); | ||
334 | |||
335 | product_len = ntohl (msg->product_length); | ||
336 | if (0 < product_len) | ||
337 | { | ||
338 | rsize = 0; | ||
339 | if (0 != (rc = gcry_mpi_scan (&num, GCRYMPI_FMT_STD, | ||
340 | &msg[1], | ||
341 | product_len, | ||
342 | &rsize))) | ||
343 | { | ||
344 | LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, | ||
345 | "gcry_mpi_scan", | ||
346 | rc); | ||
347 | gcry_mpi_release (result); | ||
348 | result = NULL; | ||
349 | status = GNUNET_SCALARPRODUCT_STATUS_INVALID_RESPONSE; | ||
350 | } | ||
351 | else | ||
352 | { | ||
353 | if (0 < (int32_t) ntohl (msg->range)) | ||
354 | gcry_mpi_add (result, result, num); | ||
355 | else | ||
356 | gcry_mpi_sub (result, result, num); | ||
357 | gcry_mpi_release (num); | ||
358 | } | ||
359 | } | ||
360 | } | ||
361 | if (NULL != h->cont_datum) | ||
362 | h->cont_datum (h->cont_cls, | ||
363 | status, | ||
364 | result); | ||
365 | if (NULL != result) | ||
366 | gcry_mpi_release (result); | ||
367 | GNUNET_SCALARPRODUCT_cancel (h); | ||
368 | } | ||
369 | |||
370 | |||
371 | /** | ||
372 | * Request by Alice's client for computing a scalar product | ||
373 | * | ||
374 | * @param cfg the gnunet configuration handle | ||
375 | * @param session_key Session key should be unique to the requesting client | ||
376 | * @param peer PeerID of the other peer | ||
377 | * @param elements Array of elements of the vector | ||
378 | * @param element_count Number of elements in the @a elements vector | ||
379 | * @param cont Callback function | ||
380 | * @param cont_cls Closure for @a cont | ||
381 | * @return a new handle for this computation | ||
382 | */ | ||
383 | struct GNUNET_SCALARPRODUCT_ComputationHandle * | ||
384 | GNUNET_SCALARPRODUCT_start_computation ( | ||
385 | const struct GNUNET_CONFIGURATION_Handle *cfg, | ||
386 | const struct GNUNET_HashCode *session_key, | ||
387 | const struct GNUNET_PeerIdentity *peer, | ||
388 | const struct GNUNET_SCALARPRODUCT_Element *elements, | ||
389 | uint32_t element_count, | ||
390 | GNUNET_SCALARPRODUCT_DatumProcessor cont, | ||
391 | void *cont_cls) | ||
392 | { | ||
393 | struct GNUNET_SCALARPRODUCT_ComputationHandle *h | ||
394 | = GNUNET_new (struct GNUNET_SCALARPRODUCT_ComputationHandle); | ||
395 | struct GNUNET_MQ_MessageHandler handlers[] = { | ||
396 | GNUNET_MQ_hd_var_size (response, | ||
397 | GNUNET_MESSAGE_TYPE_SCALARPRODUCT_RESULT, | ||
398 | struct ClientResponseMessage, | ||
399 | h), | ||
400 | GNUNET_MQ_handler_end () | ||
401 | }; | ||
402 | struct GNUNET_MQ_Envelope *env; | ||
403 | struct AliceComputationMessage *msg; | ||
404 | struct ComputationBobCryptodataMultipartMessage *mmsg; | ||
405 | uint32_t size; | ||
406 | uint16_t possible; | ||
407 | uint16_t todo; | ||
408 | uint32_t element_count_transfered; | ||
409 | |||
410 | if (GNUNET_SYSERR == check_unique (elements, | ||
411 | element_count)) | ||
412 | return NULL; | ||
413 | h->mq = GNUNET_CLIENT_connect (cfg, | ||
414 | "scalarproduct-alice", | ||
415 | handlers, | ||
416 | &mq_error_handler, | ||
417 | h); | ||
418 | if (NULL == h->mq) | ||
419 | { | ||
420 | /* misconfigured scalarproduct service */ | ||
421 | GNUNET_break (0); | ||
422 | GNUNET_free (h); | ||
423 | return NULL; | ||
424 | } | ||
425 | h->cont_datum = cont; | ||
426 | h->cont_cls = cont_cls; | ||
427 | h->response_proc = &process_result_message; | ||
428 | h->cfg = cfg; | ||
429 | h->key = *session_key; | ||
430 | |||
431 | possible = (GNUNET_MAX_MESSAGE_SIZE - 1 - sizeof(struct | ||
432 | AliceComputationMessage)) | ||
433 | / sizeof(struct GNUNET_SCALARPRODUCT_Element); | ||
434 | todo = GNUNET_MIN (possible, | ||
435 | element_count); | ||
436 | size = todo * sizeof(struct GNUNET_SCALARPRODUCT_Element); | ||
437 | env = GNUNET_MQ_msg_extra (msg, | ||
438 | size, | ||
439 | GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_ALICE); | ||
440 | msg->element_count_total = htonl (element_count); | ||
441 | msg->element_count_contained = htonl (todo); | ||
442 | msg->reserved = htonl (0); | ||
443 | msg->peer = *peer; | ||
444 | msg->session_key = *session_key; | ||
445 | GNUNET_memcpy (&msg[1], | ||
446 | elements, | ||
447 | size); | ||
448 | GNUNET_MQ_send (h->mq, | ||
449 | env); | ||
450 | element_count_transfered = todo; | ||
451 | possible = (GNUNET_MAX_MESSAGE_SIZE - 1 - sizeof(*mmsg)) | ||
452 | / sizeof(struct GNUNET_SCALARPRODUCT_Element); | ||
453 | while (element_count_transfered < element_count) | ||
454 | { | ||
455 | todo = GNUNET_MIN (possible, | ||
456 | element_count - element_count_transfered); | ||
457 | size = todo * sizeof(struct GNUNET_SCALARPRODUCT_Element); | ||
458 | env = GNUNET_MQ_msg_extra (mmsg, | ||
459 | size, | ||
460 | GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MULTIPART_ALICE); | ||
461 | mmsg->element_count_contained = htonl (todo); | ||
462 | GNUNET_memcpy (&mmsg[1], | ||
463 | &elements[element_count_transfered], | ||
464 | size); | ||
465 | element_count_transfered += todo; | ||
466 | GNUNET_MQ_send (h->mq, | ||
467 | env); | ||
468 | } | ||
469 | return h; | ||
470 | } | ||
471 | |||
472 | |||
473 | /** | ||
474 | * Cancel an ongoing computation or revoke our collaboration offer. | ||
475 | * Closes the connection to the service | ||
476 | * | ||
477 | * @param h computation handle to terminate | ||
478 | */ | ||
479 | void | ||
480 | GNUNET_SCALARPRODUCT_cancel (struct GNUNET_SCALARPRODUCT_ComputationHandle *h) | ||
481 | { | ||
482 | if (NULL != h->mq) | ||
483 | { | ||
484 | GNUNET_MQ_destroy (h->mq); | ||
485 | h->mq = NULL; | ||
486 | } | ||
487 | GNUNET_free (h); | ||
488 | } | ||
489 | |||
490 | |||
491 | /* end of scalarproduct_api.c */ | ||