GNUnet  0.10.x
gnunet-scalarproduct.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2013, 2014 GNUnet e.V.
4 
5  GNUnet is free software: you can redistribute it and/or modify it
6  under the terms of the GNU Affero General Public License as published
7  by the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  GNUnet is distributed in the hope that it will be useful, but
11  WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Affero General Public License for more details.
14 
15  You should have received a copy of the GNU Affero General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 
26 #define GCRYPT_NO_DEPRECATED
27 #include <gcrypt.h>
28 #include <inttypes.h>
29 
30 #include "platform.h"
31 #include "gnunet_util_lib.h"
33 #include "gnunet_protocols.h"
34 #include "scalarproduct.h"
35 
36 #define LOG(kind, ...) GNUNET_log_from(kind, "gnunet-scalarproduct", __VA_ARGS__)
37 
38 
43 
48 
52 static char *input_peer_id;
53 
57 static char *input_session_key;
58 
62 static char *input_elements;
63 
67 static int ret = -1;
68 
73 
74 
81 static void
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 
130 static void
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",
159  GNUNET_i2s(&peer_id));
160  break;
161 
164  "Session %s with peer %s failed: API failure\n",
166  GNUNET_i2s(&peer_id));
167  break;
168 
171  "Session %s with peer %s was disconnected from service.\n",
173  GNUNET_i2s(&peer_id));
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 
194 static void
195 shutdown_task(void *cls)
196 {
197  if (NULL != computation)
198  {
199  GNUNET_SCALARPRODUCT_cancel(computation);
200  ret = 1; /* aborted */
201  }
202 }
203 
204 
213 static void
214 run(void *cls,
215  char *const *args,
216  const char *cfgfile,
217  const struct GNUNET_CONFIGURATION_Handle *cfg)
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  _("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),
245  &peer_id.public_key)))
246  {
248  _("Tried to set initiator mode, as peer ID was given. "
249  "However, `%s' is not a valid peer identifier.\n"),
250  input_peer_id);
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) * element_count);
271 
272  for (i = 0; i < element_count; i++)
273  {
274  struct GNUNET_SCALARPRODUCT_Element element;
275  char* separator = NULL;
276 
277  /* get the length of the current key,value; tupel */
278  for (end = begin; *end != ';'; end++)
279  if (*end == ',')
280  separator = end;
281 
282  /* final element */
283  if ((NULL == separator) ||
284  (begin == separator) ||
285  (separator == end - 1))
286  {
288  _("Malformed input, could not parse `%s'\n"),
289  begin);
290  GNUNET_free(elements);
291  return;
292  }
293  *separator = 0;
294  /* read the element's key */
295  GNUNET_CRYPTO_hash(begin,
296  strlen(begin),
297  &element.key);
298 
299  /* read the element's value */
300  if (1 !=
301  sscanf(separator + 1,
302  "%" SCNd64 ";",
303  &element.value))
304  {
306  _("Could not convert `%s' to int64_t.\n"),
307  begin);
308  GNUNET_free(elements);
309  return;
310  }
311  element.value = GNUNET_htonll(element.value);
312  elements[i] = element;
313  begin = end + 1;
314  }
315 
316  if (((NULL != input_peer_id) &&
317  (NULL == (computation
319  &session_key,
320  &peer_id,
321  elements, element_count,
323  NULL)))) ||
324  ((NULL == input_peer_id) &&
325  (NULL == (computation
327  &session_key,
328  elements, element_count,
330  NULL)))))
331  {
332  fprintf(stderr,
333  _("Failed to initiate computation, were all keys unique?\n"));
334  GNUNET_free(elements);
335  return;
336  }
337  GNUNET_free(elements);
339  NULL);
340  ret = 0;
341 }
342 
343 
351 int
352 main(int argc, char *const *argv)
353 {
354  struct GNUNET_GETOPT_CommandLineOption options[] = {
356  "elements",
357  "\"key1,val1;key2,val2;...,keyn,valn;\"",
358  gettext_noop("A comma separated list of elements to compare as vector with our remote peer."),
359  &input_elements),
360 
362  "elements",
363  "\"key1,val1;key2,val2;...,keyn,valn;\"",
364  gettext_noop("A comma separated list of elements to compare as vector with our remote peer."),
365  &input_elements),
366 
368  "peer",
369  "PEERID",
370  gettext_noop("[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."),
371  &input_peer_id),
372 
374  "key",
375  "TRANSACTION_ID",
376  gettext_noop("Transaction ID shared with peer."),
378 
380  };
381 
382  return (GNUNET_OK ==
383  GNUNET_PROGRAM_run(argc,
384  argv,
385  "gnunet-scalarproduct",
386  gettext_noop("Calculate the Vectorproduct with a GNUnet peer."),
387  options, &run, NULL)) ? ret : 1;
388 }
389 
390 /* end of gnunet-scalarproduct.c */
A handle returned for each computation.
static int ret
Global return value.
static char * input_peer_id
Option -p: destination peer identity for checking message-ids with.
static void shutdown_task(void *cls)
Task run during shutdown.
static int end
Set if we are to shutdown all services (including ARM).
Definition: gnunet-arm.c:34
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, or when GNUNET_SCHEDULER_shutdown() is being invoked.
Definition: scheduler.c:1284
struct GNUNET_HashCode key
Key used to identify matching pairs of values to multiply.
#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
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_SCALARPRODUCT_ComputationHandle * GNUNET_SCALARPRODUCT_accept_computation(const struct GNUNET_CONFIGURATION_Handle *cfg, const struct GNUNET_HashCode *key, const struct GNUNET_SCALARPRODUCT_Element *elements, uint32_t element_count, GNUNET_SCALARPRODUCT_ContinuationWithStatus cont, void *cont_cls)
Used by Bob&#39;s client to cooperate with Alice,.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
const char * GNUNET_h2s(const struct GNUNET_HashCode *hc)
Convert a hash value to a string (for printing debug messages).
Definition of a command line option.
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:517
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
static char * input_session_key
Option -p: destination peer identity for checking message-ids with.
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.
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
void GNUNET_SCALARPRODUCT_cancel(struct GNUNET_SCALARPRODUCT_ComputationHandle *h)
Cancel an ongoing computation or revoke our collaboration offer.
static struct GNUNET_HashCode session_key
the session key identifying this computation
Scalar Product API Message Types.
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition: crypto_hash.c:44
static struct GNUNET_SCALARPRODUCT_ComputationHandle * computation
our Scalarproduct Computation handle
static void responder_callback(void *cls, enum GNUNET_SCALARPRODUCT_ResponseStatus status)
Callback called if we are initiating a new computation session.
uint16_t status
See PRISM_STATUS_*-constants.
static char buf[2048]
static struct GNUNET_PeerIdentity peer_id
PeerID we want to compute a scalar product with.
static int result
Global testing status.
We got disconnected from the SCALARPRODUCT service.
A 512-bit hashcode.
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:104
uint64_t GNUNET_htonll(uint64_t n)
Convert unsigned 64-bit integer to network byte order.
Definition: common_endian.c:35
int64_t value
Value to multiply in scalar product, in NBO.
int main(int argc, char *const *argv)
The main function to the scalarproduct client.
int 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:501
The identity of the host (wraps the signing key of the peer).
configuration data
Definition: configuration.c:83
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.
int 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, parse options).
Definition: program.c:367
static char * input_elements
Option -e: vector to calculate a scalarproduct with.
#define LOG(kind,...)
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&#39;s client for computing a scalar product.
const char * GNUNET_i2s(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
#define GNUNET_malloc(size)
Wrapper around malloc.
An element key-value pair for scalarproduct.
#define GNUNET_free(ptr)
Wrapper around free.
struct GNUNET_CRYPTO_EddsaPublicKey public_key
#define gettext_noop(String)
Definition: gettext.h:69