GNUnet debian-0.24.3-28-g4f2a77692
 
Loading...
Searching...
No Matches
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.
 
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.
 
static void shutdown_task (void *cls)
 Task run during shutdown.
 
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.
 
int main (int argc, char *const *argv)
 The main function to the scalarproduct client.
 

Variables

static struct GNUNET_HashCode session_key
 the session key identifying this computation
 
static struct GNUNET_PeerIdentity peer_id
 PeerID we want to compute a scalar product with.
 
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 char * input_elements
 Option -e: vector to calculate a scalarproduct with.
 
static int ret = -1
 Global return value.
 
static struct GNUNET_SCALARPRODUCT_ComputationHandlecomputation
 our Scalarproduct Computation handle
 

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.

84{
85 switch (status)
86 {
88 ret = 0;
90 "Session %s concluded.\n",
92 break;
93
96 "Session %s failed: invalid response\n",
98 break;
99
102 "Session %s failed: service failure\n",
104 break;
105
108 "Session %s failed: service disconnect!\n",
110 break;
111
112 default:
114 "Session %s failed: return code %d\n",
116 status);
117 }
118 computation = NULL;
120}
121
122
130static void
131requester_callback (void *cls,
133 gcry_mpi_t result)
134{
135 unsigned char *buf;
136 gcry_error_t rc;
137
138 switch (status)
139 {
141 if (0 == (rc = gcry_mpi_aprint (GCRYMPI_FMT_HEX, &buf, NULL, result)))
142 {
143 ret = 0;
144 fprintf (stdout,
145 "%s\n",
146 buf);
147 fflush (stdout);
148 }
149 else
151 "gcry_mpi_aprint",
152 rc);
153 break;
154
157 "Session %s with peer %s failed: invalid response received\n",
160 break;
161
164 "Session %s with peer %s failed: API failure\n",
167 break;
168
171 "Session %s with peer %s was disconnected from service.\n",
174 break;
175
176 default:
178 "Session %s with peer %s failed: return code %d\n",
181 status);
182 }
183 computation = NULL;
185}
186
187
193static void
194shutdown_task (void *cls)
195{
196 if (NULL != computation)
197 {
199 ret = 1; /* aborted */
200 }
201}
202
203
212static void
213run (void *cls,
214 char *const *args,
215 const char *cfgfile,
216 const struct GNUNET_CONFIGURATION_Handle *cfg)
217{
218 char *begin = input_elements;
219 char *end;
220 unsigned int i;
221 struct GNUNET_SCALARPRODUCT_Element *elements;
222 uint32_t element_count = 0;
223
224 if (NULL == input_elements)
225 {
227 _ ("You must specify at least one message ID to check!\n"));
228 return;
229 }
230 if ((NULL == input_session_key) ||
231 (0 == strlen (input_session_key)))
232 {
234 _ (
235 "This program needs a session identifier for comparing vectors.\n"));
236 return;
237 }
239 strlen (input_session_key),
240 &session_key);
241 if ((NULL != input_peer_id) &&
242 (GNUNET_OK !=
244 strlen (input_peer_id),
246 {
248 _ ("Tried to set initiator mode, as peer ID was given. "
249 "However, `%s' is not a valid peer identifier.\n"),
251 return;
252 }
253 if (('\'' == *begin) &&
254 ('\'' == begin[strlen (begin) - 1]))
255 {
256 begin[strlen (begin) - 1] = '\0';
257 if (strlen (begin) > 0)
258 begin++;
259 }
260 for (end = begin; 0 != *end; end++)
261 if (*end == ';')
262 element_count++;
263 if (0 == element_count)
264 {
266 _ ("Need elements to compute the scalarproduct, got none.\n"));
267 return;
268 }
269
270 elements = GNUNET_malloc (sizeof(struct GNUNET_SCALARPRODUCT_Element)
271 * element_count);
272
273 for (i = 0; i < element_count; i++)
274 {
275 struct GNUNET_SCALARPRODUCT_Element element;
276 char*separator = NULL;
277
278 /* get the length of the current key,value; tuple */
279 for (end = begin; *end != ';'; end++)
280 if (*end == ',')
281 separator = end;
282
283 /* final element */
284 if ((NULL == separator) ||
285 (begin == separator) ||
286 (separator == end - 1))
287 {
289 _ ("Malformed input, could not parse `%s'\n"),
290 begin);
291 GNUNET_free (elements);
292 return;
293 }
294 *separator = 0;
295 /* read the element's key */
296 GNUNET_CRYPTO_hash (begin,
297 strlen (begin),
298 &element.key);
299
300 /* read the element's value */
301 if (1 !=
302 sscanf (separator + 1,
303 "%" SCNd64 ";",
304 &element.value))
305 {
307 _ ("Could not convert `%s' to int64_t.\n"),
308 begin);
309 GNUNET_free (elements);
310 return;
311 }
312 element.value = GNUNET_htonll (element.value);
313 elements[i] = element;
314 begin = end + 1;
315 }
316
317 if (((NULL != input_peer_id) &&
318 (NULL == (computation
321 &peer_id,
322 elements,
323 element_count,
325 ,
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}
347
348
356int
357main (int argc, char *const *argv)
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 ==
393 argc,
394 argv,
395 "gnunet-scalarproduct",
397 "Calculate the Vectorproduct with a GNUnet peer."),
398 options, &run, NULL)) ? ret : 1;
399}
400
401
402/* end of gnunet-scalarproduct.c */
struct GNUNET_GETOPT_CommandLineOption options[]
Definition 002.c:5
int main()
Program to simulate results from GCP_get_desirability_of_path() for various plausible inputs.
#define gettext_noop(String)
Definition gettext.h:74
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition gnunet-arm.c:108
static int end
Set if we are to shutdown all services (including ARM).
Definition gnunet-arm.c:33
struct GNUNET_SCHEDULER_Task * shutdown_task
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
static char * input_elements
Option -e: vector to calculate a scalarproduct with.
static int ret
Global return value.
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 run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
Main function that will be run by the scheduler.
static struct GNUNET_PeerIdentity peer_id
PeerID we want to compute a scalar product with.
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.
#define LOG(kind,...)
static struct GNUNET_SCALARPRODUCT_ComputationHandle * computation
our Scalarproduct Computation handle
#define GNUNET_GETOPT_OPTION_END
Marker for the end of the list of options.
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.
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:361
uint64_t GNUNET_htonll(uint64_t n)
Convert unsigned 64-bit integer to network byte order.
@ GNUNET_OK
const char * GNUNET_i2s(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
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
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
const struct GNUNET_OS_ProjectData * GNUNET_OS_project_data_gnunet(void)
Return default project data used by 'libgnunetutil' for GNUnet.
enum GNUNET_GenericReturnValue GNUNET_PROGRAM_run(const struct GNUNET_OS_ProjectData *pd, 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:407
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.
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_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
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:1339
#define _(String)
GNU gettext support macro.
Definition platform.h:179
#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 of a command line option.
struct GNUNET_CRYPTO_EddsaPublicKey public_key
An element key-value pair for scalarproduct.

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}

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}

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}

References computation, GNUNET_SCALARPRODUCT_cancel(), and ret.

Here is the call 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 ,
327 NULL)))) ||
328 ((NULL == input_peer_id) &&
329 (NULL == (computation
332 elements,
333 element_count,
334 &
336 NULL)))))
337 {
338 fprintf (stderr,
339 _ ("Failed to initiate computation, were all keys unique?\n"));
340 GNUNET_free (elements);
341 return;
342 }
343 GNUNET_free (elements);
345 NULL);
346 ret = 0;
347}

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 358 of file gnunet-scalarproduct.c.

359{
362 "elements",
363 "\"key1,val1;key2,val2;...,keyn,valn;\"",
365 "A comma separated list of elements to compare as vector with our remote peer."),
367
369 "elements",
370 "\"key1,val1;key2,val2;...,keyn,valn;\"",
372 "A comma separated list of elements to compare as vector with our remote peer."),
374
376 "peer",
377 "PEERID",
379 "[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."),
381
383 "key",
384 "TRANSACTION_ID",
386 "Transaction ID shared with peer."),
388
390 };
391
392 return (GNUNET_OK ==
394 argc,
395 argv,
396 "gnunet-scalarproduct",
398 "Calculate the Vectorproduct with a GNUnet peer."),
399 options, &run, NULL)) ? ret : 1;
400}

References gettext_noop, GNUNET_GETOPT_OPTION_END, GNUNET_GETOPT_option_string(), GNUNET_OK, GNUNET_OS_project_data_gnunet(), 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().