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 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.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
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:79
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: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 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  uint32_t i;
172  int ok;
173 
174  ok = GNUNET_OK;
175  map = GNUNET_CONTAINER_multihashmap_create (2 * element_count,
176  GNUNET_YES);
177  for (i=0;i<element_count;i++)
178  if (GNUNET_OK !=
180  &elements[i].key,
181  map,
183  {
185  _("Keys given to SCALARPRODUCT not unique!\n"));
186  ok = GNUNET_SYSERR;
187  }
189  return ok;
190 }
static int ok
Return value from &#39;main&#39; (0 == success)
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
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:208
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:79
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:80
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 201 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().

203 {
205 
207  "Disconnected from SCALARPRODUCT service.\n");
208  h->response_proc (h,
209  NULL,
211 }
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 320 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().

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