GNUnet 0.21.1
gnunet-scalarproduct.c File Reference

scalarproduct client More...

#include "platform.h"
#include <gcrypt.h>
#include <inttypes.h>
#include "gnunet_util_lib.h"
#include "gnunet_scalarproduct_service.h"
#include "gnunet_protocols.h"
#include "scalarproduct.h"
Include dependency graph for gnunet-scalarproduct.c:

Go to the source code of this file.

Macros

#define GCRYPT_NO_DEPRECATED
 
#define LOG(kind, ...)
 

Functions

static void responder_callback (void *cls, enum GNUNET_SCALARPRODUCT_ResponseStatus status)
 Callback called if we are initiating a new computation session. More...
 
static void requester_callback (void *cls, enum GNUNET_SCALARPRODUCT_ResponseStatus status, gcry_mpi_t result)
 Callback called if we are initiating a new computation session. More...
 
static void shutdown_task (void *cls)
 Task run during shutdown. More...
 
static void run (void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
 Main function that will be run by the scheduler. More...
 
int main (int argc, char *const *argv)
 The main function to the scalarproduct client. More...
 

Variables

static struct GNUNET_HashCode session_key
 the session key identifying this computation More...
 
static struct GNUNET_PeerIdentity peer_id
 PeerID we want to compute a scalar product with. More...
 
static char * input_peer_id
 Option -p: destination peer identity for checking message-ids with. More...
 
static char * input_session_key
 Option -p: destination peer identity for checking message-ids with. More...
 
static char * input_elements
 Option -e: vector to calculate a scalarproduct with. More...
 
static int ret = -1
 Global return value. More...
 
static struct GNUNET_SCALARPRODUCT_ComputationHandlecomputation
 our Scalarproduct Computation handle More...
 

Detailed Description

scalarproduct client

Author
Christian M. Fuchs

Definition in file gnunet-scalarproduct.c.

Macro Definition Documentation

◆ GCRYPT_NO_DEPRECATED

#define GCRYPT_NO_DEPRECATED

Definition at line 26 of file gnunet-scalarproduct.c.

◆ LOG

#define LOG (   kind,
  ... 
)
Value:
GNUNET_log_from (kind, "gnunet-scalarproduct", \
__VA_ARGS__)
#define GNUNET_log_from(kind, comp,...)

Definition at line 36 of file gnunet-scalarproduct.c.

Function Documentation

◆ responder_callback()

static void responder_callback ( void *  cls,
enum GNUNET_SCALARPRODUCT_ResponseStatus  status 
)
static

Callback called if we are initiating a new computation session.

Parameters
clsunused
statusif our job was successfully processed

Definition at line 83 of file gnunet-scalarproduct.c.

85{
86 switch (status)
87 {
89 ret = 0;
91 "Session %s concluded.\n",
93 break;
94
97 "Session %s failed: invalid response\n",
99 break;
100
103 "Session %s failed: service failure\n",
105 break;
106
109 "Session %s failed: service disconnect!\n",
111 break;
112
113 default:
115 "Session %s failed: return code %d\n",
117 status);
118 }
119 computation = NULL;
121}
static int status
The program status; 0 for success.
Definition: gnunet-nse.c:39
static struct GNUNET_HashCode session_key
the session key identifying this computation
static int ret
Global return value.
#define LOG(kind,...)
static struct GNUNET_SCALARPRODUCT_ComputationHandle * computation
our Scalarproduct Computation handle
const char * GNUNET_h2s(const struct GNUNET_HashCode *hc)
Convert a hash value to a string (for printing debug messages).
@ GNUNET_ERROR_TYPE_ERROR
@ GNUNET_ERROR_TYPE_INFO
@ GNUNET_SCALARPRODUCT_STATUS_SUCCESS
The computation was successful.
@ GNUNET_SCALARPRODUCT_STATUS_FAILURE
We encountered some error.
@ GNUNET_SCALARPRODUCT_STATUS_DISCONNECTED
We got disconnected from the SCALARPRODUCT service.
@ GNUNET_SCALARPRODUCT_STATUS_INVALID_RESPONSE
We got an invalid response.
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:567

References computation, GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_INFO, GNUNET_h2s(), GNUNET_SCALARPRODUCT_STATUS_DISCONNECTED, GNUNET_SCALARPRODUCT_STATUS_FAILURE, GNUNET_SCALARPRODUCT_STATUS_INVALID_RESPONSE, GNUNET_SCALARPRODUCT_STATUS_SUCCESS, GNUNET_SCHEDULER_shutdown(), LOG, ret, session_key, and status.

Referenced by run().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ requester_callback()

static void requester_callback ( void *  cls,
enum GNUNET_SCALARPRODUCT_ResponseStatus  status,
gcry_mpi_t  result 
)
static

Callback called if we are initiating a new computation session.

Parameters
clsunused
statusif our job was successfully processed
resultthe result in gnu/gcry MPI format

Definition at line 132 of file gnunet-scalarproduct.c.

135{
136 unsigned char *buf;
137 gcry_error_t rc;
138
139 switch (status)
140 {
142 if (0 == (rc = gcry_mpi_aprint (GCRYMPI_FMT_HEX, &buf, NULL, result)))
143 {
144 ret = 0;
145 fprintf (stdout,
146 "%s\n",
147 buf);
148 fflush (stdout);
149 }
150 else
152 "gcry_mpi_aprint",
153 rc);
154 break;
155
158 "Session %s with peer %s failed: invalid response received\n",
161 break;
162
165 "Session %s with peer %s failed: API failure\n",
168 break;
169
172 "Session %s with peer %s was disconnected from service.\n",
175 break;
176
177 default:
179 "Session %s with peer %s failed: return code %d\n",
182 status);
183 }
184 computation = NULL;
186}
static int result
Global testing status.
static struct GNUNET_PeerIdentity peer_id
PeerID we want to compute a scalar product with.
const char * GNUNET_i2s(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
#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

References computation, GNUNET_ERROR_TYPE_ERROR, GNUNET_h2s(), GNUNET_i2s(), GNUNET_SCALARPRODUCT_STATUS_DISCONNECTED, GNUNET_SCALARPRODUCT_STATUS_FAILURE, GNUNET_SCALARPRODUCT_STATUS_INVALID_RESPONSE, GNUNET_SCALARPRODUCT_STATUS_SUCCESS, GNUNET_SCHEDULER_shutdown(), LOG, LOG_GCRY, peer_id, result, ret, session_key, and status.

Referenced by run().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ shutdown_task()

static void shutdown_task ( void *  cls)
static

Task run during shutdown.

Parameters
clsunused

Definition at line 195 of file gnunet-scalarproduct.c.

196{
197 if (NULL != computation)
198 {
200 ret = 1; /* aborted */
201 }
202}
void GNUNET_SCALARPRODUCT_cancel(struct GNUNET_SCALARPRODUCT_ComputationHandle *h)
Cancel an ongoing computation or revoke our collaboration offer.

References computation, GNUNET_SCALARPRODUCT_cancel(), and ret.

Referenced by run().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ run()

static void run ( void *  cls,
char *const *  args,
const char *  cfgfile,
const struct GNUNET_CONFIGURATION_Handle cfg 
)
static

Main function that will be run by the scheduler.

Parameters
clsclosure
argsremaining command-line arguments
cfgfilename of the configuration file used (for saving, can be NULL!)
cfgconfiguration

Definition at line 214 of file gnunet-scalarproduct.c.

218{
219 char *begin = input_elements;
220 char *end;
221 unsigned int i;
222 struct GNUNET_SCALARPRODUCT_Element *elements;
223 uint32_t element_count = 0;
224
225 if (NULL == input_elements)
226 {
228 _ ("You must specify at least one message ID to check!\n"));
229 return;
230 }
231 if ((NULL == input_session_key) ||
232 (0 == strlen (input_session_key)))
233 {
235 _ (
236 "This program needs a session identifier for comparing vectors.\n"));
237 return;
238 }
240 strlen (input_session_key),
241 &session_key);
242 if ((NULL != input_peer_id) &&
243 (GNUNET_OK !=
245 strlen (input_peer_id),
247 {
249 _ ("Tried to set initiator mode, as peer ID was given. "
250 "However, `%s' is not a valid peer identifier.\n"),
252 return;
253 }
254 if (('\'' == *begin) &&
255 ('\'' == begin[strlen (begin) - 1]))
256 {
257 begin[strlen (begin) - 1] = '\0';
258 if (strlen (begin) > 0)
259 begin++;
260 }
261 for (end = begin; 0 != *end; end++)
262 if (*end == ';')
263 element_count++;
264 if (0 == element_count)
265 {
267 _ ("Need elements to compute the scalarproduct, got none.\n"));
268 return;
269 }
270
271 elements = GNUNET_malloc (sizeof(struct GNUNET_SCALARPRODUCT_Element)
272 * element_count);
273
274 for (i = 0; i < element_count; i++)
275 {
276 struct GNUNET_SCALARPRODUCT_Element element;
277 char*separator = NULL;
278
279 /* get the length of the current key,value; tuple */
280 for (end = begin; *end != ';'; end++)
281 if (*end == ',')
282 separator = end;
283
284 /* final element */
285 if ((NULL == separator) ||
286 (begin == separator) ||
287 (separator == end - 1))
288 {
290 _ ("Malformed input, could not parse `%s'\n"),
291 begin);
292 GNUNET_free (elements);
293 return;
294 }
295 *separator = 0;
296 /* read the element's key */
297 GNUNET_CRYPTO_hash (begin,
298 strlen (begin),
299 &element.key);
300
301 /* read the element's value */
302 if (1 !=
303 sscanf (separator + 1,
304 "%" SCNd64 ";",
305 &element.value))
306 {
308 _ ("Could not convert `%s' to int64_t.\n"),
309 begin);
310 GNUNET_free (elements);
311 return;
312 }
313 element.value = GNUNET_htonll (element.value);
314 elements[i] = element;
315 begin = end + 1;
316 }
317
318 if (((NULL != input_peer_id) &&
319 (NULL == (computation
322 &peer_id,
323 elements,
324 element_count,
326 NULL)))) ||
327 ((NULL == input_peer_id) &&
328 (NULL == (computation
331 elements,
332 element_count,
333 &
335 NULL)))))
336 {
337 fprintf (stderr,
338 _ ("Failed to initiate computation, were all keys unique?\n"));
339 GNUNET_free (elements);
340 return;
341 }
342 GNUNET_free (elements);
344 NULL);
345 ret = 0;
346}
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:109
static int end
Set if we are to shutdown all services (including ARM).
Definition: gnunet-arm.c:34
static char * input_elements
Option -e: vector to calculate a scalarproduct with.
static void shutdown_task(void *cls)
Task run during shutdown.
static char * input_peer_id
Option -p: destination peer identity for checking message-ids with.
static char * input_session_key
Option -p: destination peer identity for checking message-ids with.
static void responder_callback(void *cls, enum GNUNET_SCALARPRODUCT_ResponseStatus status)
Callback called if we are initiating a new computation session.
static void requester_callback(void *cls, enum GNUNET_SCALARPRODUCT_ResponseStatus status, gcry_mpi_t result)
Callback called if we are initiating a new computation session.
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition: crypto_hash.c:41
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_eddsa_public_key_from_string(const char *enc, size_t enclen, struct GNUNET_CRYPTO_EddsaPublicKey *pub)
Convert a string representing a public key to a public key.
Definition: crypto_ecc.c:358
uint64_t GNUNET_htonll(uint64_t n)
Convert unsigned 64-bit integer to network byte order.
Definition: common_endian.c:37
@ GNUNET_OK
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
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.
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,.
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_shutdown(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run on shutdown, that is when a CTRL-C signal is received,...
Definition: scheduler.c:1340
#define _(String)
GNU gettext support macro.
Definition: platform.h:178
struct GNUNET_CRYPTO_EddsaPublicKey public_key
An element key-value pair for scalarproduct.

References _, cfg, computation, end, GNUNET_CRYPTO_eddsa_public_key_from_string(), GNUNET_CRYPTO_hash(), GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_htonll(), GNUNET_malloc, GNUNET_OK, GNUNET_SCALARPRODUCT_accept_computation(), GNUNET_SCALARPRODUCT_start_computation(), GNUNET_SCHEDULER_add_shutdown(), input_elements, input_peer_id, input_session_key, GNUNET_SCALARPRODUCT_Element::key, LOG, peer_id, GNUNET_PeerIdentity::public_key, requester_callback(), responder_callback(), ret, session_key, shutdown_task(), and GNUNET_SCALARPRODUCT_Element::value.

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

int main ( int  argc,
char *const *  argv 
)

The main function to the scalarproduct client.

Parameters
argcnumber of arguments from the command line
argvcommand line arguments
Returns
0 ok, 1 on error

Definition at line 357 of file gnunet-scalarproduct.c.

358{
361 "elements",
362 "\"key1,val1;key2,val2;...,keyn,valn;\"",
364 "A comma separated list of elements to compare as vector with our remote peer."),
366
368 "elements",
369 "\"key1,val1;key2,val2;...,keyn,valn;\"",
371 "A comma separated list of elements to compare as vector with our remote peer."),
373
375 "peer",
376 "PEERID",
378 "[Optional] peer to calculate our scalarproduct with. If this parameter is not given, the service will wait for a remote peer to compute the request."),
380
382 "key",
383 "TRANSACTION_ID",
385 "Transaction ID shared with peer."),
387
389 };
390
391 return (GNUNET_OK ==
392 GNUNET_PROGRAM_run (argc,
393 argv,
394 "gnunet-scalarproduct",
396 "Calculate the Vectorproduct with a GNUnet peer."),
397 options, &run, NULL)) ? ret : 1;
398}
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
struct GNUNET_GETOPT_CommandLineOption options[]
Definition: 002.c:5
#define gettext_noop(String)
Definition: gettext.h:70
static void run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
Main function that will be run by the scheduler.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_string(char shortName, const char *name, const char *argumentHelp, const char *description, char **str)
Allow user to specify a string.
enum GNUNET_GenericReturnValue GNUNET_PROGRAM_run(int argc, char *const *argv, const char *binaryName, const char *binaryHelp, const struct GNUNET_GETOPT_CommandLineOption *options, GNUNET_PROGRAM_Main task, void *task_cls)
Run a standard GNUnet command startup sequence (initialize loggers and configuration,...
Definition: program.c:400
Definition of a command line option.

References gettext_noop, GNUNET_GETOPT_OPTION_END, GNUNET_GETOPT_option_string(), GNUNET_OK, GNUNET_PROGRAM_run(), input_elements, input_peer_id, input_session_key, options, ret, and run().

Here is the call graph for this function:

Variable Documentation

◆ session_key

struct GNUNET_HashCode session_key
static

the session key identifying this computation

Definition at line 43 of file gnunet-scalarproduct.c.

Referenced by GNUNET_SCALARPRODUCT_accept_computation(), GNUNET_SCALARPRODUCT_start_computation(), requester_callback(), responder_callback(), and run().

◆ peer_id

struct GNUNET_PeerIdentity peer_id
static

PeerID we want to compute a scalar product with.

Definition at line 48 of file gnunet-scalarproduct.c.

Referenced by requester_callback(), and run().

◆ input_peer_id

char* input_peer_id
static

Option -p: destination peer identity for checking message-ids with.

Definition at line 53 of file gnunet-scalarproduct.c.

Referenced by main(), and run().

◆ input_session_key

char* input_session_key
static

Option -p: destination peer identity for checking message-ids with.

Definition at line 58 of file gnunet-scalarproduct.c.

Referenced by main(), and run().

◆ input_elements

char* input_elements
static

Option -e: vector to calculate a scalarproduct with.

Definition at line 63 of file gnunet-scalarproduct.c.

Referenced by main(), and run().

◆ ret

int ret = -1
static

Global return value.

Definition at line 68 of file gnunet-scalarproduct.c.

Referenced by main(), requester_callback(), responder_callback(), run(), and shutdown_task().

◆ computation

struct GNUNET_SCALARPRODUCT_ComputationHandle* computation
static

our Scalarproduct Computation handle

Definition at line 73 of file gnunet-scalarproduct.c.

Referenced by requester_callback(), responder_callback(), run(), and shutdown_task().