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__)
38 struct ScalarProductCallbackClosure
41 * the session key identifying this computation
43 struct GNUNET_HashCode key;
46 * PeerID we want to compute a scalar product with
48 struct GNUNET_PeerIdentity peer;
52 * Option -p: destination peer identity for checking message-ids with
54 static char *input_peer_id = NULL;
57 * Option -p: destination peer identity for checking message-ids with
59 static char *input_key = NULL;
62 * Option -e: vector to calculate a scalarproduct with
64 static char *input_elements = NULL;
67 * Option -m: message-ids to calculate a scalarproduct with
69 static char *input_mask = NULL;
78 * Callback called if we are initiating a new computation session
81 * @param status if our job was successfully processed
84 responder_callback (void *cls,
85 enum GNUNET_SCALARPRODUCT_ResponseStatus status)
87 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);
108 GNUNET_SCALARPRODUCT_disconnect ();
109 GNUNET_SCHEDULER_shutdown ();
114 * Callback called if we are initiating a new computation session
119 * @param status if our job was successfully processed
120 * @param size size of the msg returned
121 * @param msg the response we got.
122 * @param type of the message received
125 requester_callback (void *cls,
126 enum GNUNET_SCALARPRODUCT_ResponseStatus status,
129 struct ScalarProductCallbackClosure * closure = cls;
136 case GNUNET_SCALARPRODUCT_Status_Success:
138 if (0 == (rc = gcry_mpi_aprint (GCRYMPI_FMT_HEX, &buf, NULL, result)))
141 LOG_GCRY(GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_aprint", rc);
143 case GNUNET_SCALARPRODUCT_Status_InvalidResponse:
144 LOG (GNUNET_ERROR_TYPE_ERROR, "Session %s with peer %s failed: invalid response received\n", GNUNET_h2s (&closure->key), GNUNET_i2s (&closure->peer));
146 case GNUNET_SCALARPRODUCT_Status_Failure:
147 LOG (GNUNET_ERROR_TYPE_ERROR, "Session %s with peer %s failed: API failure\n", GNUNET_h2s (&closure->key), GNUNET_i2s (&closure->peer));
148 case GNUNET_SCALARPRODUCT_Status_ServiceDisconnected:
149 LOG (GNUNET_ERROR_TYPE_ERROR, "Session %s with peer %s was disconnected from service.\n", GNUNET_h2s (&closure->key), GNUNET_i2s (&closure->peer));
152 LOG (GNUNET_ERROR_TYPE_ERROR, "Session %s with peer %s failed: return code %d\n", GNUNET_h2s (&closure->key), GNUNET_i2s (&closure->peer), status);
154 GNUNET_SCALARPRODUCT_disconnect ();
155 GNUNET_SCHEDULER_shutdown ();
160 * Main function that will be run by the scheduler.
163 * @param args remaining command-line arguments
164 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
165 * @param cfg configuration
171 const struct GNUNET_CONFIGURATION_Handle *cfg)
173 char * begin = input_elements;
179 unsigned char * mask;
180 unsigned short mask_bytes;
181 unsigned short element_count;
182 struct ScalarProductCallbackClosure * closure;
184 if (NULL == input_elements)
186 FPRINTF (stderr, "%s", _ ("You must specify at least one message ID to check!\n"));
190 if (NULL == input_key)
192 FPRINTF (stderr, "%s", _ ("This program needs a session identifier for comparing vectors.\n"));
196 if (1 > strnlen (input_key, sizeof (struct GNUNET_HashCode)))
198 FPRINTF (stderr, _ ("Please give a session key for --input_key!\n"));
201 closure = GNUNET_new(struct ScalarProductCallbackClosure);
202 GNUNET_CRYPTO_hash (input_key, strlen (input_key), &closure->key);
204 if (input_peer_id && GNUNET_OK != GNUNET_CRYPTO_hash_from_string (input_peer_id,
205 (struct GNUNET_HashCode *) &closure->peer))
207 FPRINTF (stderr, _ ("Tried to set initiator mode, as peer ID was given. "
208 "However, `%s' is not a valid peer identifier.\n"),
214 /* Read input_elements_peer1, and put in elements_peer1 array */
217 unsigned int mcount = element_count;
218 //ignore empty rows of ,,,,,,
219 while (*begin == ',')
221 // get the length of the current element and replace , with null
222 for (end = begin; *end && *end != ','; end++);
230 if (1 != sscanf (begin, "%" SCNd32, &element))
232 FPRINTF (stderr, _ ("Could not convert `%s' to int32_t.\n"), begin);
236 GNUNET_array_append (elements, mcount, element);
243 GNUNET_assert (elements != NULL);
244 GNUNET_assert (element_count > 1);
245 mask_bytes = element_count / 8 + (element_count % 8 ? 1 : 0);
246 mask = GNUNET_malloc ((element_count / 8) + 2);
248 /* Read input_mask_peer1 and read in mask_peer1 array */
249 if (NULL != input_mask)
252 unsigned short mask_count = 0;
257 //ignore empty rows of ,,,,,,
258 while (* begin == ',')
260 // get the length of the current element and replace , with null
261 // gnunet_ascii-armor uses base32, thus we can use , as separator!
262 for (end = begin; *end && *end != ','; end++);
270 if (1 != sscanf (begin, "%" SCNd32, &element))
272 FPRINTF (stderr, _ ("Could not convert `%s' to int32_t.\n"), begin);
276 GNUNET_assert (mask_count <= element_count);
279 mask[mask_count / 8] = mask[mask_count / 8] | 1 << (mask_count % 8);
285 // +1 to see if we would have more data, which would indicate malformed/superficial input
286 GNUNET_assert (mask_count == element_count);
288 else if (input_peer_id)
290 for (i = 0; i <= mask_bytes; i++)
291 mask[i] = UCHAR_MAX; // all 1's
295 if (input_peer_id && !GNUNET_SCALARPRODUCT_request (cfg,
298 elements, element_count,
305 if (!input_peer_id && !GNUNET_SCALARPRODUCT_response (cfg,
307 elements, element_count,
317 * The main function to the scalarproduct client.
319 * @param argc number of arguments from the command line
320 * @param argv command line arguments
321 * @return 0 ok, 1 on error
324 main (int argc, char *const *argv)
326 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
327 {'e', "elements", "\"val1,val2,...,valn\"",
328 gettext_noop ("A comma separated list of elements to compare as vector with our remote peer."),
329 1, &GNUNET_GETOPT_set_string, &input_elements},
330 {'m', "mask", "\"0,1,...,maskn\"",
331 gettext_noop ("A comma separated mask to select which elements should actually be compared."),
332 1, &GNUNET_GETOPT_set_string, &input_mask},
333 {'p', "peer", "PEERID",
334 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."),
335 1, &GNUNET_GETOPT_set_string, &input_peer_id},
336 {'k', "key", "TRANSACTION_ID",
337 gettext_noop ("Transaction ID shared with peer."),
338 1, &GNUNET_GETOPT_set_string, &input_key},
339 GNUNET_GETOPT_OPTION_END
343 GNUNET_PROGRAM_run (argc,
345 "gnunet-scalarproduct",
346 gettext_noop ("Calculate the Vectorproduct with a GNUnet peer."),
347 options, &run, NULL)) ? ret : 1;