2 This file is part of GNUnet.
3 (C) 2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file scalarproduct/gnunet-scalarproduct.c
23 * @brief scalarproduct client
24 * @author Christian M. Fuchs
26 #define GCRYPT_NO_DEPRECATED
31 #include "gnunet_util_lib.h"
32 #include "gnunet_scalarproduct_service.h"
33 #include "gnunet_protocols.h"
34 #include "scalarproduct.h"
36 #define LOG(kind,...) GNUNET_log_from (kind, "gnunet-scalarproduct",__VA_ARGS__)
37 #define INPUTSTRINGLENGTH 1024
40 * A primitive closure structure holding information about our session
42 struct ScalarProductCallbackClosure
45 * the session key identifying this computation
47 struct GNUNET_HashCode key;
50 * PeerID we want to compute a scalar product with
52 struct GNUNET_PeerIdentity peer;
56 * Option -p: destination peer identity for checking message-ids with
58 static char *input_peer_id;
61 * Option -p: destination peer identity for checking message-ids with
63 static char *input_key;
66 * Option -e: vector to calculate a scalarproduct with
68 static char *input_elements;
71 * Option -m: message-ids to calculate a scalarproduct with
73 static char *input_mask;
82 * Callback called if we are initiating a new computation session
85 * @param status if our job was successfully processed
88 responder_callback (void *cls,
89 enum GNUNET_SCALARPRODUCT_ResponseStatus status)
91 struct ScalarProductCallbackClosure * closure = cls;
95 case GNUNET_SCALARPRODUCT_Status_Success:
97 LOG (GNUNET_ERROR_TYPE_INFO,
98 "Session %s concluded.\n",
99 GNUNET_h2s (&closure->key));
101 case GNUNET_SCALARPRODUCT_Status_InvalidResponse:
102 LOG (GNUNET_ERROR_TYPE_ERROR,
103 "Session %s failed: invalid response\n",
104 GNUNET_h2s (&closure->key));
106 case GNUNET_SCALARPRODUCT_Status_Failure:
107 LOG (GNUNET_ERROR_TYPE_ERROR,
108 "Session %s failed: service failure\n",
109 GNUNET_h2s (&closure->key));
111 case GNUNET_SCALARPRODUCT_Status_ServiceDisconnected:
112 LOG (GNUNET_ERROR_TYPE_ERROR,
113 "Session %s failed: service disconnect!\n",
114 GNUNET_h2s (&closure->key));
117 LOG (GNUNET_ERROR_TYPE_ERROR,
118 "Session %s failed: return code %d\n",
119 GNUNET_h2s (&closure->key),
122 GNUNET_SCHEDULER_shutdown();
127 * Callback called if we are initiating a new computation session
130 * @param status if our job was successfully processed
131 * @param result the result in gnu/gcry MPI format
134 requester_callback (void *cls,
135 enum GNUNET_SCALARPRODUCT_ResponseStatus status,
138 struct ScalarProductCallbackClosure * closure = cls;
144 case GNUNET_SCALARPRODUCT_Status_Success:
145 if (0 == (rc = gcry_mpi_aprint (GCRYMPI_FMT_HEX, &buf, NULL, result)))
151 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR,
155 case GNUNET_SCALARPRODUCT_Status_InvalidResponse:
156 LOG (GNUNET_ERROR_TYPE_ERROR,
157 "Session %s with peer %s failed: invalid response received\n",
158 GNUNET_h2s (&closure->key),
159 GNUNET_i2s (&closure->peer));
161 case GNUNET_SCALARPRODUCT_Status_Failure:
162 LOG (GNUNET_ERROR_TYPE_ERROR,
163 "Session %s with peer %s failed: API failure\n",
164 GNUNET_h2s (&closure->key),
165 GNUNET_i2s (&closure->peer));
167 case GNUNET_SCALARPRODUCT_Status_ServiceDisconnected:
168 LOG (GNUNET_ERROR_TYPE_ERROR,
169 "Session %s with peer %s was disconnected from service.\n",
170 GNUNET_h2s (&closure->key),
171 GNUNET_i2s (&closure->peer));
174 LOG (GNUNET_ERROR_TYPE_ERROR,
175 "Session %s with peer %s failed: return code %d\n",
176 GNUNET_h2s (&closure->key),
177 GNUNET_i2s (&closure->peer),
180 GNUNET_SCHEDULER_shutdown();
185 * Task run during shutdown.
191 shutdown_task (void *cls,
192 const struct GNUNET_SCHEDULER_TaskContext *tc)
194 GNUNET_SCALARPRODUCT_disconnect ();
199 * Main function that will be run by the scheduler.
202 * @param args remaining command-line arguments
203 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
204 * @param cfg configuration
210 const struct GNUNET_CONFIGURATION_Handle *cfg)
212 char *begin = input_elements;
219 uint32_t element_count = 0;
220 struct ScalarProductCallbackClosure * closure;
222 if (NULL == input_elements)
224 LOG (GNUNET_ERROR_TYPE_ERROR,
225 _ ("You must specify at least one message ID to check!\n"));
229 if (NULL == input_key)
231 LOG (GNUNET_ERROR_TYPE_ERROR,
232 _ ("This program needs a session identifier for comparing vectors.\n"));
236 if (1 > strnlen (input_key, sizeof (struct GNUNET_HashCode)))
238 LOG (GNUNET_ERROR_TYPE_ERROR,
239 _ ("Please give a session key for --input_key!\n"));
242 closure = GNUNET_new (struct ScalarProductCallbackClosure);
243 GNUNET_CRYPTO_hash (input_key, strlen (input_key), &closure->key);
247 GNUNET_CRYPTO_hash_from_string (input_peer_id,
248 (struct GNUNET_HashCode *) &closure->peer)))
250 LOG (GNUNET_ERROR_TYPE_ERROR,
251 _ ("Tried to set initiator mode, as peer ID was given. "
252 "However, `%s' is not a valid peer identifier.\n"),
257 /* Count input_elements_peer1, and put in elements_peer1 array */
260 // get the length of the current element and replace , with null
261 for (end = begin; *end && *end != ','; end++);
263 if (1 == sscanf (begin, "%" SCNd32 ",", &element))
265 //element in the middle
269 else if (0 == *begin)
275 LOG (GNUNET_ERROR_TYPE_ERROR,
276 _ ("Could not convert `%s' to int32_t.\n"), begin);
281 if (0 == element_count)
283 LOG (GNUNET_ERROR_TYPE_ERROR,
284 _ ("Need elements to compute the vectorproduct, got none.\n"));
288 begin = input_elements;
289 elements = GNUNET_malloc (sizeof (int32_t) * element_count);
291 /* Read input_elements_peer1, and put in elements_peer1 array */
294 // get the length of the current element and replace , with null
295 for (end = begin; *end && *end != ','; end++);
297 if (1 == sscanf (begin, "%" SCNd32 ",", &elements[element_count]))
299 //element in the middle
303 else if (0 == *begin)
310 mask_bytes = element_count / 8 + ( (element_count % 8) ? 1 : 0);
311 mask = GNUNET_malloc ((element_count / 8) + 1);
313 /* Read input_mask_peer1 and read in mask_peer1 array */
314 if ((NULL != input_peer_id) && (NULL != input_mask))
317 unsigned short mask_count = 0;
321 // get the length of the current element and replace , with null
322 for (end = begin; *end && *end != ','; end++);
324 if (1 == sscanf (begin, "%" SCNd32 ",", &element))
326 //element in the middle
329 else if (*begin == 0)
335 LOG (GNUNET_ERROR_TYPE_ERROR,
336 _ ("Could not convert `%s' to integer.\n"), begin);
341 mask[mask_count / 8] = mask[mask_count / 8] | 1 << (mask_count % 8);
344 while (mask_count < element_count);
346 else if (NULL != input_peer_id)
347 for (i = 0; i <= mask_bytes; i++)
348 mask[i] = UCHAR_MAX; // all 1's
351 (NULL == GNUNET_SCALARPRODUCT_request (cfg,
354 elements, element_count,
359 GNUNET_free (elements);
364 if ((NULL == input_peer_id) &&
365 (NULL == GNUNET_SCALARPRODUCT_response (cfg,
367 elements, element_count,
371 GNUNET_free (elements);
375 GNUNET_free (elements);
377 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
386 * The main function to the scalarproduct client.
388 * @param argc number of arguments from the command line
389 * @param argv command line arguments
390 * @return 0 ok, 1 on error
393 main (int argc, char *const *argv)
395 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
396 {'e', "elements", "\"val1,val2,...,valn\"",
397 gettext_noop ("A comma separated list of elements to compare as vector with our remote peer."),
398 1, &GNUNET_GETOPT_set_string, &input_elements},
399 {'m', "mask", "\"0,1,...,maskn\"",
400 gettext_noop ("A comma separated mask to select which elements should actually be compared."),
401 1, &GNUNET_GETOPT_set_string, &input_mask},
402 {'p', "peer", "PEERID",
403 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."),
404 1, &GNUNET_GETOPT_set_string, &input_peer_id},
405 {'k', "key", "TRANSACTION_ID",
406 gettext_noop ("Transaction ID shared with peer."),
407 1, &GNUNET_GETOPT_set_string, &input_key},
408 GNUNET_GETOPT_OPTION_END
412 GNUNET_PROGRAM_run (argc,
414 "gnunet-scalarproduct",
415 gettext_noop ("Calculate the Vectorproduct with a GNUnet peer."),
416 options, &run, NULL)) ? ret : 1;