GNUnet  0.11.x
Data Structures | Macros | Typedefs | Functions
scalarproduct_api.c File Reference

API for the scalarproduct. More...

#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_statistics_service.h"
#include "gnunet_scalarproduct_service.h"
#include "gnunet_protocols.h"
#include "scalarproduct.h"
Include dependency graph for scalarproduct_api.c:

Go to the source code of this file.

Data Structures

struct  GNUNET_SCALARPRODUCT_ComputationHandle
 A handle returned for each computation. More...
 

Macros

#define LOG(kind, ...)   GNUNET_log_from (kind, "scalarproduct-api", __VA_ARGS__)
 

Typedefs

typedef 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. More...
 

Functions

static int check_response (void *cls, const struct ClientResponseMessage *message)
 Called when a response is received from the service. More...
 
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. More...
 
static void handle_response (void *cls, const struct ClientResponseMessage *message)
 Called when a response is received from the service. More...
 
static int check_unique (const struct GNUNET_SCALARPRODUCT_Element *elements, uint32_t element_count)
 Check if the keys for all given elements are unique. More...
 
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. More...
 
struct GNUNET_SCALARPRODUCT_ComputationHandleGNUNET_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,. More...
 
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. More...
 
struct GNUNET_SCALARPRODUCT_ComputationHandleGNUNET_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. More...
 
void GNUNET_SCALARPRODUCT_cancel (struct GNUNET_SCALARPRODUCT_ComputationHandle *h)
 Cancel an ongoing computation or revoke our collaboration offer. More...
 

Detailed Description

API for the scalarproduct.

Author
Christian Fuchs
Gaurav Kukreja
Christian Grothoff

Definition in file scalarproduct_api.c.

Macro Definition Documentation

◆ LOG

#define LOG (   kind,
  ... 
)    GNUNET_log_from (kind, "scalarproduct-api", __VA_ARGS__)

Definition at line 34 of file scalarproduct_api.c.

Referenced by mq_error_handler().

Typedef Documentation

◆ GNUNET_SCALARPRODUCT_ResponseMessageHandler

typedef 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.

Parameters
hcomputation handle
msgresponse we got, NULL on errors
statusprocessing status code

Definition at line 45 of file scalarproduct_api.c.

Function Documentation

◆ check_response()

static int check_response ( void *  cls,
const struct ClientResponseMessage message 
)
static

Called when a response is received from the service.

Perform basic check that the message is well-formed.

Parameters
clsPointer to the Master Context
messagePointer to the data received in response
Returns
GNUNET_OK if message is well-formed

Definition at line 103 of file scalarproduct_api.c.

References GNUNET_break, GNUNET_OK, GNUNET_SYSERR, ClientResponseMessage::header, ClientResponseMessage::product_length, and GNUNET_MessageHeader::size.

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 }
Message type passed from service client to finalize a session as requester or responder.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format...
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
struct GNUNET_MessageHeader header
GNUNET message header.
uint32_t product_length
0 if no product attached

◆ process_status_message()

static void process_status_message ( struct GNUNET_SCALARPRODUCT_ComputationHandle h,
const struct ClientResponseMessage msg,
enum GNUNET_SCALARPRODUCT_ResponseStatus  status 
)
static

Handles the STATUS received from the service for a response, does not contain a payload.

Called when we participate as "Bob" via GNUNET_SCALARPRODUCT_accept_computation().

Parameters
hour Handle
msgthe response received
statusthe condition the request was terminated with (eg: disconnect)

Definition at line 126 of file scalarproduct_api.c.

References GNUNET_SCALARPRODUCT_ComputationHandle::cont_cls, GNUNET_SCALARPRODUCT_ComputationHandle::cont_status, and GNUNET_SCALARPRODUCT_cancel().

Referenced by GNUNET_SCALARPRODUCT_accept_computation().

129 {
130  if (NULL != h->cont_status)
131  h->cont_status (h->cont_cls,
132  status);
134 }
void GNUNET_SCALARPRODUCT_cancel(struct GNUNET_SCALARPRODUCT_ComputationHandle *h)
Cancel an ongoing computation or revoke our collaboration offer.
uint16_t status
See PRISM_STATUS_*-constants.
GNUNET_SCALARPRODUCT_ContinuationWithStatus cont_status
Function to call after transmission of the request (Bob).
void * cont_cls
Closure for cont_status or cont_datum.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ handle_response()

static void handle_response ( void *  cls,
const struct ClientResponseMessage message 
)
static

Called when a response is received from the service.

After basic check, the handler in h->response_proc is called. This functions handles the response to the client which used the API.

Parameters
clsPointer to the Master Context
msgPointer to the data received in response

Definition at line 146 of file scalarproduct_api.c.

References GNUNET_SCALARPRODUCT_ComputationHandle::response_proc, status, and ClientResponseMessage::status.

148 {
151 
152  status = (enum GNUNET_SCALARPRODUCT_ResponseStatus) ntohl (message->status);
153  h->response_proc (h,
154  message,
155  status);
156 }
A handle returned for each computation.
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:99
uint32_t status
Status information about the outcome of this session, An enum GNUNET_SCALARPRODUCT_ResponseStatus (in...
uint16_t status
See PRISM_STATUS_*-constants.
GNUNET_SCALARPRODUCT_ResponseStatus
Result status values for the computation.
GNUNET_SCALARPRODUCT_ResponseMessageHandler response_proc
API internal callback for results and failures to be forwarded to the client.

◆ check_unique()

static int check_unique ( const struct GNUNET_SCALARPRODUCT_Element elements,
uint32_t  element_count 
)
static

Check if the keys for all given elements are unique.

Parameters
elementselements to check
element_countsize of the elements array
Returns
GNUNET_OK if all keys are unique

Definition at line 167 of file scalarproduct_api.c.

References _, GNUNET_CONTAINER_multihashmap_create(), GNUNET_CONTAINER_multihashmap_destroy(), GNUNET_CONTAINER_multihashmap_put(), GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY, GNUNET_ERROR_TYPE_ERROR, GNUNET_log, GNUNET_OK, GNUNET_SYSERR, GNUNET_YES, map, and ok.

Referenced by GNUNET_SCALARPRODUCT_accept_computation(), and GNUNET_SCALARPRODUCT_start_computation().

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 }
static int ok
Return value from &#39;main&#39; (0 == success)
Internal representation of the hash map.
static struct GNUNET_CONTAINER_MultiPeerMap * map
Handle to the map used to store old latency values for peers.
#define _(String)
GNU gettext support macro.
Definition: platform.h:178
void GNUNET_CONTAINER_multihashmap_destroy(struct GNUNET_CONTAINER_MultiHashMap *map)
Destroy a hash map.
There must only be one value per key; storing a value should fail if a value under the same key alrea...
struct GNUNET_HashCode key
The key used in the DHT.
int 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.
#define GNUNET_log(kind,...)
struct GNUNET_CONTAINER_MultiHashMap * GNUNET_CONTAINER_multihashmap_create(unsigned int len, int do_not_copy_keys)
Create a multi hash map.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mq_error_handler()

static void mq_error_handler ( void *  cls,
enum GNUNET_MQ_Error  error 
)
static

We encountered an error communicating with the set service while performing a set operation.

Report to the application.

Parameters
clsthe struct GNUNET_SCALARPRODUCT_ComputationHandle
errorerror code

Definition at line 200 of file scalarproduct_api.c.

References GNUNET_ERROR_TYPE_INFO, GNUNET_SCALARPRODUCT_STATUS_DISCONNECTED, LOG, and GNUNET_SCALARPRODUCT_ComputationHandle::response_proc.

Referenced by GNUNET_SCALARPRODUCT_accept_computation(), and GNUNET_SCALARPRODUCT_start_computation().

202 {
204 
206  "Disconnected from SCALARPRODUCT service.\n");
207  h->response_proc (h,
208  NULL,
210 }
A handle returned for each computation.
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:99
We got disconnected from the SCALARPRODUCT service.
#define LOG(kind,...)
GNUNET_SCALARPRODUCT_ResponseMessageHandler response_proc
API internal callback for results and failures to be forwarded to the client.
Here is the caller graph for this function:

◆ process_result_message()

static void process_result_message ( struct GNUNET_SCALARPRODUCT_ComputationHandle h,
const struct ClientResponseMessage msg,
enum GNUNET_SCALARPRODUCT_ResponseStatus  status 
)
static

Handles the RESULT received from the service for a request, should contain a result MPI value.

Called when we participate as "Alice" via GNUNET_SCALARPRODUCT_start_computation().

Parameters
hour Handle
msgPointer to the response received
statusthe condition the request was terminated with (eg: disconnect)

Definition at line 321 of file scalarproduct_api.c.

References GNUNET_SCALARPRODUCT_ComputationHandle::cont_cls, GNUNET_SCALARPRODUCT_ComputationHandle::cont_datum, GNUNET_ERROR_TYPE_ERROR, GNUNET_SCALARPRODUCT_cancel(), GNUNET_SCALARPRODUCT_STATUS_INVALID_RESPONSE, GNUNET_SCALARPRODUCT_STATUS_SUCCESS, LOG_GCRY, ClientResponseMessage::product_length, ClientResponseMessage::range, and result.

Referenced by GNUNET_SCALARPRODUCT_start_computation().

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 
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  {
345  "gcry_mpi_scan",
346  rc);
347  gcry_mpi_release (result);
348  result = NULL;
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);
368 }
GNUNET_SCALARPRODUCT_DatumProcessor cont_datum
Function to call after transmission of the request (Alice).
int32_t range
Workaround for libgcrypt: -1 if negative, 0 if zero, else 1.
#define LOG_GCRY(level, cmd, rc)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; with the mess...
Definition: scalarproduct.h:35
void GNUNET_SCALARPRODUCT_cancel(struct GNUNET_SCALARPRODUCT_ComputationHandle *h)
Cancel an ongoing computation or revoke our collaboration offer.
uint16_t status
See PRISM_STATUS_*-constants.
static int result
Global testing status.
uint32_t product_length
0 if no product attached
void * cont_cls
Closure for cont_status or cont_datum.
Here is the call graph for this function:
Here is the caller graph for this function: