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
39 struct ScalarProductCallbackClosure
42 * the session key identifying this computation
44 struct GNUNET_HashCode key;
47 * PeerID we want to compute a scalar product with
49 struct GNUNET_PeerIdentity peer;
53 * Option -p: destination peer identity for checking message-ids with
55 static char *input_peer_id = NULL;
58 * Option -p: destination peer identity for checking message-ids with
60 static char *input_key = NULL;
63 * Option -e: vector to calculate a scalarproduct with
65 static char *input_elements = NULL;
68 * Option -m: message-ids to calculate a scalarproduct with
70 static char *input_mask = NULL;
79 * Callback called if we are initiating a new computation session
82 * @param status if our job was successfully processed
85 responder_callback (void *cls,
86 enum GNUNET_SCALARPRODUCT_ResponseStatus status)
88 struct ScalarProductCallbackClosure * closure = cls;
92 case GNUNET_SCALARPRODUCT_Status_Success:
94 LOG (GNUNET_ERROR_TYPE_INFO, "Session %s concluded.\n", GNUNET_h2s (&closure->key));
96 case GNUNET_SCALARPRODUCT_Status_InvalidResponse:
97 LOG (GNUNET_ERROR_TYPE_ERROR, "Session %s failed: invalid response\n", GNUNET_h2s (&closure->key));
99 case GNUNET_SCALARPRODUCT_Status_Failure:
100 LOG (GNUNET_ERROR_TYPE_ERROR, "Session %s failed: service failure\n", GNUNET_h2s (&closure->key));
101 case GNUNET_SCALARPRODUCT_Status_ServiceDisconnected:
102 LOG (GNUNET_ERROR_TYPE_ERROR, "Session %s failed: service disconnect!!\n", GNUNET_h2s (&closure->key));
105 LOG (GNUNET_ERROR_TYPE_ERROR, "Session %s failed: return code %d\n", GNUNET_h2s (&closure->key), status);
111 * Callback called if we are initiating a new computation session
116 * @param status if our job was successfully processed
117 * @param size size of the msg returned
118 * @param msg the response we got.
119 * @param type of the message received
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);
155 * Task run during shutdown.
161 shutdown_task (void *cls,
162 const struct GNUNET_SCHEDULER_TaskContext *tc)
164 GNUNET_SCALARPRODUCT_disconnect ();
168 * Main function that will be run by the scheduler.
171 * @param args remaining command-line arguments
172 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
173 * @param cfg configuration
179 const struct GNUNET_CONFIGURATION_Handle *cfg)
181 char * begin = input_elements;
186 unsigned char * mask;
188 uint32_t element_count = 0;
189 struct ScalarProductCallbackClosure * closure;
191 if (NULL == input_elements)
193 LOG (GNUNET_ERROR_TYPE_ERROR, _ ("You must specify at least one message ID to check!\n"));
197 if (NULL == input_key)
199 LOG (GNUNET_ERROR_TYPE_ERROR, _ ("This program needs a session identifier for comparing vectors.\n"));
203 if (1 > strnlen (input_key, sizeof (struct GNUNET_HashCode)))
205 LOG (GNUNET_ERROR_TYPE_ERROR, _ ("Please give a session key for --input_key!\n"));
208 closure = GNUNET_new (struct ScalarProductCallbackClosure);
209 GNUNET_CRYPTO_hash (input_key, strlen (input_key), &closure->key);
211 if (input_peer_id && GNUNET_OK != GNUNET_CRYPTO_hash_from_string (input_peer_id,
212 (struct GNUNET_HashCode *) &closure->peer))
214 LOG (GNUNET_ERROR_TYPE_ERROR, _ ("Tried to set initiator mode, as peer ID was given. "
215 "However, `%s' is not a valid peer identifier.\n"),
220 /* Count input_elements_peer1, and put in elements_peer1 array */
223 // get the length of the current element and replace , with null
224 for (end = begin; *end && *end != ','; end++);
226 if (1 == sscanf (begin, "%" SCNd32",", &element))
228 //element in the middle
232 else if (*begin == 0)
238 LOG (GNUNET_ERROR_TYPE_ERROR, _ ("Could not convert `%s' to int32_t.\n"), begin);
243 if (0 == element_count)
249 begin = input_elements;
250 elements = GNUNET_malloc (sizeof (int32_t) * element_count);
251 /* Read input_elements_peer1, and put in elements_peer1 array */
254 // get the length of the current element and replace , with null
255 for (end = begin; *end && *end != ','; end++);
257 if (1 == sscanf (begin, "%" SCNd32",", &element))
259 //element in the middle
263 else if (*begin == 0)
270 mask_bytes = element_count / 8 + (element_count % 8 ? 1 : 0);
271 mask = GNUNET_malloc ((element_count / 8) + 2);
273 /* Read input_mask_peer1 and read in mask_peer1 array */
274 if ((NULL != input_peer_id) && (NULL != input_mask))
277 unsigned short mask_count = 0;
281 // get the length of the current element and replace , with null
282 for (end = begin; *end && *end != ','; end++);
284 if (1 == sscanf (begin, "%" SCNd32 ",", &element))
286 //element in the middle
289 else if (*begin == 0)
295 LOG (GNUNET_ERROR_TYPE_ERROR, _ ("Could not convert `%s' to int32_t.\n"), begin);
300 mask[mask_count / 8] = mask[mask_count / 8] | 1 << (mask_count % 8);
303 while (mask_count < element_count);
305 else if (NULL != input_peer_id)
306 for (i = 0; i <= mask_bytes; i++)
307 mask[i] = UCHAR_MAX; // all 1's
309 if (input_peer_id && (NULL == GNUNET_SCALARPRODUCT_request (cfg,
312 elements, element_count,
318 if ((NULL == input_peer_id) && (NULL == GNUNET_SCALARPRODUCT_response (cfg,
320 elements, element_count,
325 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
334 * The main function to the scalarproduct client.
336 * @param argc number of arguments from the command line
337 * @param argv command line arguments
338 * @return 0 ok, 1 on error
341 main (int argc, char *const *argv)
343 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
344 {'e', "elements", "\"val1,val2,...,valn\"",
345 gettext_noop ("A comma separated list of elements to compare as vector with our remote peer."),
346 1, &GNUNET_GETOPT_set_string, &input_elements},
347 {'m', "mask", "\"0,1,...,maskn\"",
348 gettext_noop ("A comma separated mask to select which elements should actually be compared."),
349 1, &GNUNET_GETOPT_set_string, &input_mask},
350 {'p', "peer", "PEERID",
351 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."),
352 1, &GNUNET_GETOPT_set_string, &input_peer_id},
353 {'k', "key", "TRANSACTION_ID",
354 gettext_noop ("Transaction ID shared with peer."),
355 1, &GNUNET_GETOPT_set_string, &input_key},
356 GNUNET_GETOPT_OPTION_END
360 GNUNET_PROGRAM_run (argc,
362 "gnunet-scalarproduct",
363 gettext_noop ("Calculate the Vectorproduct with a GNUnet peer."),
364 options, &run, NULL)) ? ret : 1;