GNUnet 0.21.1
scalarproduct_api.c
Go to the documentation of this file.
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 */
27#include "platform.h"
28#include "gnunet_util_lib.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
44typedef void
47 const struct ClientResponseMessage *msg,
49
50
55{
60
65
70
75
79 void *cont_cls;
80
86
91};
92
93
102static int
103check_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
125static void
127 const struct ClientResponseMessage *msg,
129{
130 if (NULL != h->cont_status)
131 h->cont_status (h->cont_cls,
132 status);
134}
135
136
145static void
147 const struct ClientResponseMessage *message)
148{
151
152 status = (enum GNUNET_SCALARPRODUCT_ResponseStatus) ntohl (message->status);
153 h->response_proc (h,
154 message,
155 status);
156}
157
158
166static int
168 uint32_t element_count)
169{
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 !=
179 &elements[i].key,
180 map,
182 {
184 _ ("Keys given to SCALARPRODUCT not unique!\n"));
185 ok = GNUNET_SYSERR;
186 }
188 return ok;
189}
190
191
199static void
201 enum GNUNET_MQ_Error error)
202{
204
206 "Disconnected from SCALARPRODUCT service.\n");
207 h->response_proc (h,
208 NULL,
210}
211
212
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,
220 void *cont_cls)
221{
228 h),
230 };
231 struct GNUNET_MQ_Envelope *env;
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;
249 "scalarproduct-bob",
250 handlers,
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
262 / sizeof(struct GNUNET_SCALARPRODUCT_Element);
263 todo = GNUNET_MIN (possible,
264 element_count);
265 size = todo * sizeof(struct GNUNET_SCALARPRODUCT_Element);
267 size,
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;
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,
288 mmsg->element_count_contained = htonl (todo);
289 GNUNET_memcpy (&mmsg[1],
290 &elements[element_count_transfered],
291 size);
292 element_count_transfered += todo;
294 env);
295 }
296 return h;
297}
298
299
309static void
311 const struct ClientResponseMessage *msg,
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
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 {
334 "gcry_mpi_scan",
335 rc);
336 gcry_mpi_release (result);
337 result = NULL;
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);
357}
358
359
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,
368 void *cont_cls)
369{
376 h),
378 };
379 struct GNUNET_MQ_Envelope *env;
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;
391 "scalarproduct-alice",
392 handlers,
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
410 / sizeof(struct GNUNET_SCALARPRODUCT_Element);
411 todo = GNUNET_MIN (possible,
412 element_count);
413 size = todo * sizeof(struct GNUNET_SCALARPRODUCT_Element);
415 size,
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);
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,
438 mmsg->element_count_contained = htonl (todo);
439 GNUNET_memcpy (&mmsg[1],
440 &elements[element_count_transfered],
441 size);
442 element_count_transfered += todo;
444 env);
445 }
446 return h;
447}
448
449
456void
458{
459 if (NULL != h->mq)
460 {
462 h->mq = NULL;
463 }
464 GNUNET_free (h);
465}
466
467
468/* end of scalarproduct_api.c */
struct GNUNET_MQ_MessageHandlers handlers[]
Definition: 003.c:1
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:99
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:109
static struct MHD_Response * response
Our canonical response.
struct GNUNET_HashCode key
The key used in the DHT.
static int status
The program status; 0 for success.
Definition: gnunet-nse.c:39
static int result
Global testing status.
static struct GNUNET_HashCode session_key
the session key identifying this computation
Constants for network protocols.
API to create, modify and access statistics.
#define GNUNET_MAX_MESSAGE_SIZE
Largest supported message (to be precise, one byte more than the largest possible message,...
struct GNUNET_MQ_Handle * GNUNET_CLIENT_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *service_name, const struct GNUNET_MQ_MessageHandler *handlers, GNUNET_MQ_ErrorHandler error_handler, void *error_handler_cls)
Create a message queue to connect to a GNUnet service.
Definition: client.c:1057
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multihashmap_put(struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key, void *value, enum GNUNET_CONTAINER_MultiHashMapOption opt)
Store a key-value pair in the map.
void GNUNET_CONTAINER_multihashmap_destroy(struct GNUNET_CONTAINER_MultiHashMap *map)
Destroy a hash map.
struct GNUNET_CONTAINER_MultiHashMap * GNUNET_CONTAINER_multihashmap_create(unsigned int len, int do_not_copy_keys)
Create a multi hash map.
@ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY
There must only be one value per key; storing a value should fail if a value under the same key alrea...
#define GNUNET_log(kind,...)
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#define GNUNET_MIN(a, b)
@ GNUNET_OK
@ GNUNET_YES
@ GNUNET_SYSERR
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
@ GNUNET_ERROR_TYPE_ERROR
@ GNUNET_ERROR_TYPE_INFO
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
GNUNET_MQ_Error
Error codes for the queue.
void GNUNET_MQ_send(struct GNUNET_MQ_Handle *mq, struct GNUNET_MQ_Envelope *ev)
Send a message with the given message queue.
Definition: mq.c:304
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
#define GNUNET_MQ_msg_extra(mvar, esize, type)
Allocate an envelope, with extra space allocated after the space needed by the message struct.
Definition: gnunet_mq_lib.h:63
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:683
#define GNUNET_MESSAGE_TYPE_SCALARPRODUCT_RESULT
Alice/Bob -> Client Result.
#define GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_BOB
Client -> Bob.
#define GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MULTIPART_ALICE
Client -> Alice multipart.
#define GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MULTIPART_BOB
Client -> Bob multipart.
#define GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_ALICE
Client -> Alice.
void(* GNUNET_SCALARPRODUCT_DatumProcessor)(void *cls, enum GNUNET_SCALARPRODUCT_ResponseStatus status, gcry_mpi_t result)
Process a datum that was stored in the scalarproduct.
GNUNET_SCALARPRODUCT_ResponseStatus
Result status values for the computation.
struct GNUNET_SCALARPRODUCT_ComputationHandle * GNUNET_SCALARPRODUCT_start_computation(const struct GNUNET_CONFIGURATION_Handle *cfg, const struct GNUNET_HashCode *session_key, const struct GNUNET_PeerIdentity *peer, const struct GNUNET_SCALARPRODUCT_Element *elements, uint32_t element_count, GNUNET_SCALARPRODUCT_DatumProcessor cont, void *cont_cls)
Request by Alice's client for computing a scalar product.
void GNUNET_SCALARPRODUCT_cancel(struct GNUNET_SCALARPRODUCT_ComputationHandle *h)
Cancel an ongoing computation or revoke our collaboration offer.
GNUNET_NETWORK_STRUCT_END typedef void(* GNUNET_SCALARPRODUCT_ContinuationWithStatus)(void *cls, enum GNUNET_SCALARPRODUCT_ResponseStatus status)
Continuation called to notify client about result of the operation.
struct GNUNET_SCALARPRODUCT_ComputationHandle * GNUNET_SCALARPRODUCT_accept_computation(const struct GNUNET_CONFIGURATION_Handle *cfg, const struct GNUNET_HashCode *session_key, const struct GNUNET_SCALARPRODUCT_Element *elements, uint32_t element_count, GNUNET_SCALARPRODUCT_ContinuationWithStatus cont, void *cont_cls)
Used by Bob's client to cooperate with Alice,.
@ GNUNET_SCALARPRODUCT_STATUS_SUCCESS
The computation was successful.
@ GNUNET_SCALARPRODUCT_STATUS_DISCONNECTED
We got disconnected from the SCALARPRODUCT service.
@ GNUNET_SCALARPRODUCT_STATUS_INVALID_RESPONSE
We got an invalid response.
static struct GNUNET_CONTAINER_MultiPeerMap * map
Peermap of PeerIdentities to "struct PeerEntry" (for fast lookup).
Definition: peer.c:63
static unsigned int size
Size of the "table".
Definition: peer.c:68
#define _(String)
GNU gettext support macro.
Definition: platform.h:178
Scalar Product API Message Types.
#define LOG_GCRY(level, cmd, rc)
Log an error message at log-level 'level' that indicates a failure of the command 'cmd' with the mess...
Definition: scalarproduct.h:35
static void process_result_message(struct GNUNET_SCALARPRODUCT_ComputationHandle *h, const struct ClientResponseMessage *msg, enum GNUNET_SCALARPRODUCT_ResponseStatus status)
Handles the RESULT received from the service for a request, should contain a result MPI value.
static int check_unique(const struct GNUNET_SCALARPRODUCT_Element *elements, uint32_t element_count)
Check if the keys for all given elements are unique.
static void mq_error_handler(void *cls, enum GNUNET_MQ_Error error)
We encountered an error communicating with the set service while performing a set operation.
static void process_status_message(struct GNUNET_SCALARPRODUCT_ComputationHandle *h, const struct ClientResponseMessage *msg, enum GNUNET_SCALARPRODUCT_ResponseStatus status)
Handles the STATUS received from the service for a response, does not contain a payload.
static void handle_response(void *cls, const struct ClientResponseMessage *message)
Called when a response is received from the service.
void(* GNUNET_SCALARPRODUCT_ResponseMessageHandler)(struct GNUNET_SCALARPRODUCT_ComputationHandle *h, const struct ClientResponseMessage *msg, enum GNUNET_SCALARPRODUCT_ResponseStatus status)
The abstraction function for our internal callback.
#define LOG(kind,...)
static int check_response(void *cls, const struct ClientResponseMessage *message)
Called when a response is received from the service.
Message type passed from client to service to initiate a request or responder role.
Definition: scalarproduct.h:46
Message type passed from client to service to initiate a request or responder role.
Definition: scalarproduct.h:89
Message type passed from service client to finalize a session as requester or responder.
uint32_t product_length
0 if no product attached
struct GNUNET_MessageHeader header
GNUNET message header.
uint32_t status
Status information about the outcome of this session, An enum GNUNET_SCALARPRODUCT_ResponseStatus (in...
multipart messages following struct ComputationMessage
uint32_t element_count_contained
contained elements the vector in payload contains
struct GNUNET_MQ_Handle * mq
Our connection to the ARM service.
Definition: arm_api.c:107
const struct GNUNET_CONFIGURATION_Handle * cfg
The configuration that we are using.
Definition: arm_api.c:112
Internal representation of the hash map.
A 512-bit hashcode.
Handle to a message queue.
Definition: mq.c:87
Message handler for a specific message type.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format.
The identity of the host (wraps the signing key of the peer).
A handle returned for each computation.
GNUNET_SCALARPRODUCT_ResponseMessageHandler response_proc
API internal callback for results and failures to be forwarded to the client.
struct GNUNET_MQ_Handle * mq
Current connection to the scalarproduct service.
const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
struct GNUNET_HashCode key
The shared session key identifying this computation.
void * cont_cls
Closure for cont_status or cont_datum.
GNUNET_SCALARPRODUCT_ContinuationWithStatus cont_status
Function to call after transmission of the request (Bob).
GNUNET_SCALARPRODUCT_DatumProcessor cont_datum
Function to call after transmission of the request (Alice).
An element key-value pair for scalarproduct.