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 = NULL;
61 * Option -p: destination peer identity for checking message-ids with
63 static char *input_key = NULL;
66 * Option -e: vector to calculate a scalarproduct with
68 static char *input_elements = NULL;
71 * Option -m: message-ids to calculate a scalarproduct with
73 static char *input_mask = NULL;
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, "Session %s concluded.\n", GNUNET_h2s (&closure->key));
99 case GNUNET_SCALARPRODUCT_Status_InvalidResponse:
100 LOG (GNUNET_ERROR_TYPE_ERROR, "Session %s failed: invalid response\n", GNUNET_h2s (&closure->key));
102 case GNUNET_SCALARPRODUCT_Status_Failure:
103 LOG (GNUNET_ERROR_TYPE_ERROR, "Session %s failed: service failure\n", GNUNET_h2s (&closure->key));
104 case GNUNET_SCALARPRODUCT_Status_ServiceDisconnected:
105 LOG (GNUNET_ERROR_TYPE_ERROR, "Session %s failed: service disconnect!!\n", GNUNET_h2s (&closure->key));
108 LOG (GNUNET_ERROR_TYPE_ERROR, "Session %s failed: return code %d\n", GNUNET_h2s (&closure->key), status);
110 GNUNET_SCHEDULER_shutdown();
115 * Callback called if we are initiating a new computation session
118 * @param status if our job was successfully processed
119 * @param result the result in gnu/gcry MPI format
122 requester_callback (void *cls,
123 enum GNUNET_SCALARPRODUCT_ResponseStatus status,
126 struct ScalarProductCallbackClosure * closure = cls;
132 case GNUNET_SCALARPRODUCT_Status_Success:
133 if (0 == (rc = gcry_mpi_aprint (GCRYMPI_FMT_HEX, &buf, NULL, result)))
139 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_aprint", rc);
141 case GNUNET_SCALARPRODUCT_Status_InvalidResponse:
142 LOG (GNUNET_ERROR_TYPE_ERROR, "Session %s with peer %s failed: invalid response received\n", GNUNET_h2s (&closure->key), GNUNET_i2s (&closure->peer));
144 case GNUNET_SCALARPRODUCT_Status_Failure:
145 LOG (GNUNET_ERROR_TYPE_ERROR, "Session %s with peer %s failed: API failure\n", GNUNET_h2s (&closure->key), GNUNET_i2s (&closure->peer));
146 case GNUNET_SCALARPRODUCT_Status_ServiceDisconnected:
147 LOG (GNUNET_ERROR_TYPE_ERROR, "Session %s with peer %s was disconnected from service.\n", GNUNET_h2s (&closure->key), GNUNET_i2s (&closure->peer));
150 LOG (GNUNET_ERROR_TYPE_ERROR, "Session %s with peer %s failed: return code %d\n", GNUNET_h2s (&closure->key), GNUNET_i2s (&closure->peer), status);
152 GNUNET_SCHEDULER_shutdown();
156 * Task run during shutdown.
162 shutdown_task (void *cls,
163 const struct GNUNET_SCHEDULER_TaskContext *tc)
165 GNUNET_SCALARPRODUCT_disconnect ();
169 * Main function that will be run by the scheduler.
172 * @param args remaining command-line arguments
173 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
174 * @param cfg configuration
180 const struct GNUNET_CONFIGURATION_Handle *cfg)
182 char * begin = input_elements;
187 unsigned char * mask;
189 uint32_t element_count = 0;
190 struct ScalarProductCallbackClosure * closure;
192 if (NULL == input_elements)
194 LOG (GNUNET_ERROR_TYPE_ERROR, _ ("You must specify at least one message ID to check!\n"));
198 if (NULL == input_key)
200 LOG (GNUNET_ERROR_TYPE_ERROR, _ ("This program needs a session identifier for comparing vectors.\n"));
204 if (1 > strnlen (input_key, sizeof (struct GNUNET_HashCode)))
206 LOG (GNUNET_ERROR_TYPE_ERROR, _ ("Please give a session key for --input_key!\n"));
209 closure = GNUNET_new (struct ScalarProductCallbackClosure);
210 GNUNET_CRYPTO_hash (input_key, strlen (input_key), &closure->key);
212 if (input_peer_id && GNUNET_OK != GNUNET_CRYPTO_hash_from_string (input_peer_id,
213 (struct GNUNET_HashCode *) &closure->peer))
215 LOG (GNUNET_ERROR_TYPE_ERROR, _ ("Tried to set initiator mode, as peer ID was given. "
216 "However, `%s' is not a valid peer identifier.\n"),
221 /* Count input_elements_peer1, and put in elements_peer1 array */
224 // get the length of the current element and replace , with null
225 for (end = begin; *end && *end != ','; end++);
227 if (1 == sscanf (begin, "%" SCNd32 ",", &element))
229 //element in the middle
233 else if (0 == *begin)
239 LOG (GNUNET_ERROR_TYPE_ERROR, _ ("Could not convert `%s' to int32_t.\n"), begin);
244 if (0 == element_count)
246 LOG (GNUNET_ERROR_TYPE_ERROR, _ ("Need elements to compute the vectorproduct, got none.\n"));
250 begin = input_elements;
251 elements = GNUNET_malloc (sizeof (int32_t) * element_count);
253 /* Read input_elements_peer1, and put in elements_peer1 array */
256 // get the length of the current element and replace , with null
257 for (end = begin; *end && *end != ','; end++);
259 if (1 == sscanf (begin, "%" SCNd32 ",", &elements[element_count]))
261 //element in the middle
265 else if (0 == *begin)
272 mask_bytes = element_count / 8 + (element_count % 8 ? 1 : 0);
273 mask = GNUNET_malloc ((element_count / 8) + 1);
275 /* Read input_mask_peer1 and read in mask_peer1 array */
276 if ((NULL != input_peer_id) && (NULL != input_mask))
279 unsigned short mask_count = 0;
283 // get the length of the current element and replace , with null
284 for (end = begin; *end && *end != ','; end++);
286 if (1 == sscanf (begin, "%" SCNd32 ",", &element))
288 //element in the middle
291 else if (*begin == 0)
297 LOG (GNUNET_ERROR_TYPE_ERROR, _ ("Could not convert `%s' to int32_t.\n"), begin);
302 mask[mask_count / 8] = mask[mask_count / 8] | 1 << (mask_count % 8);
305 while (mask_count < element_count);
307 else if (NULL != input_peer_id)
308 for (i = 0; i <= mask_bytes; i++)
309 mask[i] = UCHAR_MAX; // all 1's
311 if (input_peer_id && (NULL == GNUNET_SCALARPRODUCT_request (cfg,
314 elements, element_count,
320 if ((NULL == input_peer_id) && (NULL == GNUNET_SCALARPRODUCT_response (cfg,
322 elements, element_count,
327 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
336 * The main function to the scalarproduct client.
338 * @param argc number of arguments from the command line
339 * @param argv command line arguments
340 * @return 0 ok, 1 on error
343 main (int argc, char *const *argv)
345 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
346 {'e', "elements", "\"val1,val2,...,valn\"",
347 gettext_noop ("A comma separated list of elements to compare as vector with our remote peer."),
348 1, &GNUNET_GETOPT_set_string, &input_elements},
349 {'m', "mask", "\"0,1,...,maskn\"",
350 gettext_noop ("A comma separated mask to select which elements should actually be compared."),
351 1, &GNUNET_GETOPT_set_string, &input_mask},
352 {'p', "peer", "PEERID",
353 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."),
354 1, &GNUNET_GETOPT_set_string, &input_peer_id},
355 {'k', "key", "TRANSACTION_ID",
356 gettext_noop ("Transaction ID shared with peer."),
357 1, &GNUNET_GETOPT_set_string, &input_key},
358 GNUNET_GETOPT_OPTION_END
362 GNUNET_PROGRAM_run (argc,
364 "gnunet-scalarproduct",
365 gettext_noop ("Calculate the Vectorproduct with a GNUnet peer."),
366 options, &run, NULL)) ? ret : 1;