GNUnet  0.10.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 101 of file scalarproduct_api.c.

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

103 {
104  if (ntohs(message->header.size) !=
105  ntohl(message->product_length) + sizeof(struct ClientResponseMessage))
106  {
107  GNUNET_break(0);
108  return GNUNET_SYSERR;
109  }
110  return GNUNET_OK;
111 }
Message type passed from service client to finalize a session as requester or responder.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
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.
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
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 124 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().

127 {
128  if (NULL != h->cont_status)
129  h->cont_status(h->cont_cls,
130  status);
132 }
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 144 of file scalarproduct_api.c.

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

146 {
149 
150  status = (enum GNUNET_SCALARPRODUCT_ResponseStatus)ntohl(message->status);
151  h->response_proc(h,
152  message,
153  status);
154 }
A handle returned for each computation.
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:94
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 165 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().

167 {
169  uint32_t i;
170  int ok;
171 
172  ok = GNUNET_OK;
173  map = GNUNET_CONTAINER_multihashmap_create(2 * element_count,
174  GNUNET_YES);
175  for (i = 0; i < element_count; i++)
176  if (GNUNET_OK !=
178  &elements[i].key,
179  map,
181  {
183  _("Keys given to SCALARPRODUCT not unique!\n"));
184  ok = GNUNET_SYSERR;
185  }
187  return ok;
188 }
static int ok
Return value from &#39;main&#39; (0 == success)
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
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:181
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.
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
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.
#define GNUNET_YES
Definition: gnunet_common.h:77
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 199 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().

201 {
203 
205  "Disconnected from SCALARPRODUCT service.\n");
206  h->response_proc(h,
207  NULL,
209 }
A handle returned for each computation.
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:94
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 318 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().

321 {
322  uint32_t product_len;
323  gcry_mpi_t result = NULL;
324  gcry_error_t rc;
325  gcry_mpi_t num;
326  size_t rsize;
327 
329  {
330  result = gcry_mpi_new(0);
331 
332  product_len = ntohl(msg->product_length);
333  if (0 < product_len)
334  {
335  rsize = 0;
336  if (0 != (rc = gcry_mpi_scan(&num, GCRYMPI_FMT_STD,
337  &msg[1],
338  product_len,
339  &rsize)))
340  {
342  "gcry_mpi_scan",
343  rc);
344  gcry_mpi_release(result);
345  result = NULL;
347  }
348  else
349  {
350  if (0 < (int32_t)ntohl(msg->range))
351  gcry_mpi_add(result, result, num);
352  else
353  gcry_mpi_sub(result, result, num);
354  gcry_mpi_release(num);
355  }
356  }
357  }
358  if (NULL != h->cont_datum)
359  h->cont_datum(h->cont_cls,
360  status,
361  result);
362  if (NULL != result)
363  gcry_mpi_release(result);
365 }
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: