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);
107 GNUNET_SCHEDULER_shutdown();
112 * Callback called if we are initiating a new computation session
117 * @param status if our job was successfully processed
118 * @param size size of the msg returned
119 * @param msg the response we got.
120 * @param type of the message received
123 requester_callback (void *cls,
124 enum GNUNET_SCALARPRODUCT_ResponseStatus status,
127 struct ScalarProductCallbackClosure * closure = cls;
133 case GNUNET_SCALARPRODUCT_Status_Success:
134 if (0 == (rc = gcry_mpi_aprint (GCRYMPI_FMT_HEX, &buf, NULL, result)))
140 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_aprint", rc);
142 case GNUNET_SCALARPRODUCT_Status_InvalidResponse:
143 LOG (GNUNET_ERROR_TYPE_ERROR, "Session %s with peer %s failed: invalid response received\n", GNUNET_h2s (&closure->key), GNUNET_i2s (&closure->peer));
145 case GNUNET_SCALARPRODUCT_Status_Failure:
146 LOG (GNUNET_ERROR_TYPE_ERROR, "Session %s with peer %s failed: API failure\n", GNUNET_h2s (&closure->key), GNUNET_i2s (&closure->peer));
147 case GNUNET_SCALARPRODUCT_Status_ServiceDisconnected:
148 LOG (GNUNET_ERROR_TYPE_ERROR, "Session %s with peer %s was disconnected from service.\n", GNUNET_h2s (&closure->key), GNUNET_i2s (&closure->peer));
151 LOG (GNUNET_ERROR_TYPE_ERROR, "Session %s with peer %s failed: return code %d\n", GNUNET_h2s (&closure->key), GNUNET_i2s (&closure->peer), status);
153 GNUNET_SCHEDULER_shutdown();
157 * Task run during shutdown.
163 shutdown_task (void *cls,
164 const struct GNUNET_SCHEDULER_TaskContext *tc)
166 GNUNET_SCALARPRODUCT_disconnect ();
170 * Main function that will be run by the scheduler.
173 * @param args remaining command-line arguments
174 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
175 * @param cfg configuration
181 const struct GNUNET_CONFIGURATION_Handle *cfg)
183 char * begin = input_elements;
188 unsigned char * mask;
190 uint32_t element_count = 0;
191 struct ScalarProductCallbackClosure * closure;
193 if (NULL == input_elements)
195 LOG (GNUNET_ERROR_TYPE_ERROR, _ ("You must specify at least one message ID to check!\n"));
199 if (NULL == input_key)
201 LOG (GNUNET_ERROR_TYPE_ERROR, _ ("This program needs a session identifier for comparing vectors.\n"));
205 if (1 > strnlen (input_key, sizeof (struct GNUNET_HashCode)))
207 LOG (GNUNET_ERROR_TYPE_ERROR, _ ("Please give a session key for --input_key!\n"));
210 closure = GNUNET_new (struct ScalarProductCallbackClosure);
211 GNUNET_CRYPTO_hash (input_key, strlen (input_key), &closure->key);
213 if (input_peer_id && GNUNET_OK != GNUNET_CRYPTO_hash_from_string (input_peer_id,
214 (struct GNUNET_HashCode *) &closure->peer))
216 LOG (GNUNET_ERROR_TYPE_ERROR, _ ("Tried to set initiator mode, as peer ID was given. "
217 "However, `%s' is not a valid peer identifier.\n"),
222 /* Count input_elements_peer1, and put in elements_peer1 array */
225 // get the length of the current element and replace , with null
226 for (end = begin; *end && *end != ','; end++);
228 if (1 == sscanf (begin, "%" SCNd32 ",", &element))
230 //element in the middle
234 else if (0 == *begin)
240 LOG (GNUNET_ERROR_TYPE_ERROR, _ ("Could not convert `%s' to int32_t.\n"), begin);
245 if (0 == element_count)
247 LOG (GNUNET_ERROR_TYPE_ERROR, _ ("Need elements to compute the vectorproduct, got none.\n"));
251 begin = input_elements;
252 elements = GNUNET_malloc (sizeof (int32_t) * element_count);
254 /* Read input_elements_peer1, and put in elements_peer1 array */
257 // get the length of the current element and replace , with null
258 for (end = begin; *end && *end != ','; end++);
260 if (1 == sscanf (begin, "%" SCNd32 ",", &elements[element_count]))
262 //element in the middle
266 else if (0 == *begin)
273 mask_bytes = element_count / 8 + (element_count % 8 ? 1 : 0);
274 mask = GNUNET_malloc ((element_count / 8) + 1);
276 /* Read input_mask_peer1 and read in mask_peer1 array */
277 if ((NULL != input_peer_id) && (NULL != input_mask))
280 unsigned short mask_count = 0;
284 // get the length of the current element and replace , with null
285 for (end = begin; *end && *end != ','; end++);
287 if (1 == sscanf (begin, "%" SCNd32 ",", &element))
289 //element in the middle
292 else if (*begin == 0)
298 LOG (GNUNET_ERROR_TYPE_ERROR, _ ("Could not convert `%s' to int32_t.\n"), begin);
303 mask[mask_count / 8] = mask[mask_count / 8] | 1 << (mask_count % 8);
306 while (mask_count < element_count);
308 else if (NULL != input_peer_id)
309 for (i = 0; i <= mask_bytes; i++)
310 mask[i] = UCHAR_MAX; // all 1's
312 if (input_peer_id && (NULL == GNUNET_SCALARPRODUCT_request (cfg,
315 elements, element_count,
321 if ((NULL == input_peer_id) && (NULL == GNUNET_SCALARPRODUCT_response (cfg,
323 elements, element_count,
328 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
337 * The main function to the scalarproduct client.
339 * @param argc number of arguments from the command line
340 * @param argv command line arguments
341 * @return 0 ok, 1 on error
344 main (int argc, char *const *argv)
346 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
347 {'e', "elements", "\"val1,val2,...,valn\"",
348 gettext_noop ("A comma separated list of elements to compare as vector with our remote peer."),
349 1, &GNUNET_GETOPT_set_string, &input_elements},
350 {'m', "mask", "\"0,1,...,maskn\"",
351 gettext_noop ("A comma separated mask to select which elements should actually be compared."),
352 1, &GNUNET_GETOPT_set_string, &input_mask},
353 {'p', "peer", "PEERID",
354 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."),
355 1, &GNUNET_GETOPT_set_string, &input_peer_id},
356 {'k', "key", "TRANSACTION_ID",
357 gettext_noop ("Transaction ID shared with peer."),
358 1, &GNUNET_GETOPT_set_string, &input_key},
359 GNUNET_GETOPT_OPTION_END
363 GNUNET_PROGRAM_run (argc,
365 "gnunet-scalarproduct",
366 gettext_noop ("Calculate the Vectorproduct with a GNUnet peer."),
367 options, &run, NULL)) ? ret : 1;