Merge branch 'master' of ssh://gnunet.org/gnunet
[oweals/gnunet.git] / src / scalarproduct / gnunet-scalarproduct.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2013, 2014 GNUnet e.V.
4
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.
9
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.
14
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19  */
20
21 /**
22  * @file scalarproduct/gnunet-scalarproduct.c
23  * @brief scalarproduct client
24  * @author Christian M. Fuchs
25  */
26 #define GCRYPT_NO_DEPRECATED
27 #include <gcrypt.h>
28 #include <inttypes.h>
29
30 #include "platform.h"
31 #include "gnunet_util_lib.h"
32 #include "gnunet_scalarproduct_service.h"
33 #include "gnunet_protocols.h"
34 #include "scalarproduct.h"
35
36 #define LOG(kind,...) GNUNET_log_from (kind, "gnunet-scalarproduct",__VA_ARGS__)
37
38
39 /**
40  * the session key identifying this computation
41  */
42 static struct GNUNET_HashCode session_key;
43
44 /**
45  * PeerID we want to compute a scalar product with
46  */
47 static struct GNUNET_PeerIdentity peer_id;
48
49 /**
50  * Option -p: destination peer identity for checking message-ids with
51  */
52 static char *input_peer_id;
53
54 /**
55  * Option -p: destination peer identity for checking message-ids with
56  */
57 static char *input_session_key;
58
59 /**
60  * Option -e: vector to calculate a scalarproduct with
61  */
62 static char *input_elements;
63
64 /**
65  * Global return value
66  */
67 static int ret = -1;
68
69 /**
70  * our Scalarproduct Computation handle
71  */
72 static struct GNUNET_SCALARPRODUCT_ComputationHandle *computation;
73
74
75 /**
76  * Callback called if we are initiating a new computation session
77  *
78  * @param cls unused
79  * @param status if our job was successfully processed
80  */
81 static void
82 responder_callback (void *cls,
83                     enum GNUNET_SCALARPRODUCT_ResponseStatus status)
84 {
85   switch (status)
86   {
87   case GNUNET_SCALARPRODUCT_STATUS_SUCCESS:
88     ret = 0;
89     LOG (GNUNET_ERROR_TYPE_INFO,
90          "Session %s concluded.\n",
91          GNUNET_h2s (&session_key));
92     break;
93   case GNUNET_SCALARPRODUCT_STATUS_INVALID_RESPONSE:
94     LOG (GNUNET_ERROR_TYPE_ERROR,
95          "Session %s failed: invalid response\n",
96          GNUNET_h2s (&session_key));
97     break;
98   case GNUNET_SCALARPRODUCT_STATUS_FAILURE:
99     LOG (GNUNET_ERROR_TYPE_ERROR,
100          "Session %s failed: service failure\n",
101          GNUNET_h2s (&session_key));
102     break;
103   case GNUNET_SCALARPRODUCT_STATUS_DISCONNECTED:
104     LOG (GNUNET_ERROR_TYPE_ERROR,
105          "Session %s failed: service disconnect!\n",
106          GNUNET_h2s (&session_key));
107     break;
108   default:
109     LOG (GNUNET_ERROR_TYPE_ERROR,
110          "Session %s failed: return code %d\n",
111          GNUNET_h2s (&session_key),
112          status);
113   }
114   computation = NULL;
115   GNUNET_SCHEDULER_shutdown ();
116 }
117
118
119 /**
120  * Callback called if we are initiating a new computation session
121  *
122  * @param cls unused
123  * @param status if our job was successfully processed
124  * @param result the result in gnu/gcry MPI format
125  */
126 static void
127 requester_callback (void *cls,
128                     enum GNUNET_SCALARPRODUCT_ResponseStatus status,
129                     gcry_mpi_t result)
130 {
131   unsigned char *buf;
132   gcry_error_t rc;
133
134   switch (status)
135   {
136   case GNUNET_SCALARPRODUCT_STATUS_SUCCESS:
137     if (0 == (rc = gcry_mpi_aprint (GCRYMPI_FMT_HEX, &buf, NULL, result)))
138     {
139       ret = 0;
140       fprintf (stdout,
141                "%s\n",
142                buf);
143       fflush (stdout);
144     }
145     else
146       LOG_GCRY (GNUNET_ERROR_TYPE_ERROR,
147                 "gcry_mpi_aprint",
148                 rc);
149     break;
150   case GNUNET_SCALARPRODUCT_STATUS_INVALID_RESPONSE:
151     LOG (GNUNET_ERROR_TYPE_ERROR,
152          "Session %s with peer %s failed: invalid response received\n",
153          GNUNET_h2s (&session_key),
154          GNUNET_i2s (&peer_id));
155     break;
156   case GNUNET_SCALARPRODUCT_STATUS_FAILURE:
157     LOG (GNUNET_ERROR_TYPE_ERROR,
158          "Session %s with peer %s failed: API failure\n",
159          GNUNET_h2s (&session_key),
160          GNUNET_i2s (&peer_id));
161     break;
162   case GNUNET_SCALARPRODUCT_STATUS_DISCONNECTED:
163     LOG (GNUNET_ERROR_TYPE_ERROR,
164          "Session %s with peer %s was disconnected from service.\n",
165          GNUNET_h2s (&session_key),
166          GNUNET_i2s (&peer_id));
167     break;
168   default:
169     LOG (GNUNET_ERROR_TYPE_ERROR,
170          "Session %s with peer %s failed: return code %d\n",
171          GNUNET_h2s (&session_key),
172          GNUNET_i2s (&peer_id),
173          status);
174   }
175   computation = NULL;
176   GNUNET_SCHEDULER_shutdown ();
177 }
178
179
180 /**
181  * Task run during shutdown.
182  *
183  * @param cls unused
184  * @param tc unused
185  */
186 static void
187 shutdown_task (void *cls)
188 {
189   if (NULL != computation)
190   {
191     GNUNET_SCALARPRODUCT_cancel (computation);
192     ret = 1; /* aborted */
193   }
194 }
195
196
197 /**
198  * Main function that will be run by the scheduler.
199  *
200  * @param cls closure
201  * @param args remaining command-line arguments
202  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
203  * @param cfg configuration
204  */
205 static void
206 run (void *cls,
207      char *const *args,
208      const char *cfgfile,
209      const struct GNUNET_CONFIGURATION_Handle *cfg)
210 {
211   char *begin = input_elements;
212   char *end;
213   unsigned int i;
214   struct GNUNET_SCALARPRODUCT_Element *elements;
215   uint32_t element_count = 0;
216
217   if (NULL == input_elements)
218   {
219     LOG (GNUNET_ERROR_TYPE_ERROR,
220          _("You must specify at least one message ID to check!\n"));
221     return;
222   }
223   if ( (NULL == input_session_key) ||
224        (0 == strlen (input_session_key)) )
225   {
226     LOG (GNUNET_ERROR_TYPE_ERROR,
227          _("This program needs a session identifier for comparing vectors.\n"));
228     return;
229   }
230   GNUNET_CRYPTO_hash (input_session_key,
231                       strlen (input_session_key),
232                       &session_key);
233   if ( (NULL != input_peer_id) &&
234        (GNUNET_OK !=
235         GNUNET_CRYPTO_eddsa_public_key_from_string (input_peer_id,
236                                                     strlen (input_peer_id),
237                                                     &peer_id.public_key)) )
238   {
239     LOG (GNUNET_ERROR_TYPE_ERROR,
240          _("Tried to set initiator mode, as peer ID was given. "
241            "However, `%s' is not a valid peer identifier.\n"),
242          input_peer_id);
243     return;
244   }
245   if ( ('\'' == *begin) &&
246        ('\'' == begin[strlen(begin)-1]) )
247   {
248     begin[strlen(begin)-1] = '\0';
249     if (strlen (begin) > 0)
250       begin++;
251   }
252   for (end = begin; 0 != *end; end++)
253     if (*end == ';')
254       element_count++;
255   if (0 == element_count)
256   {
257     LOG (GNUNET_ERROR_TYPE_ERROR,
258          _("Need elements to compute the scalarproduct, got none.\n"));
259     return;
260   }
261
262   elements = GNUNET_malloc (sizeof(struct GNUNET_SCALARPRODUCT_Element) * element_count);
263
264   for (i = 0; i < element_count;i++)
265   {
266     struct GNUNET_SCALARPRODUCT_Element element;
267     char* separator = NULL;
268
269     /* get the length of the current key,value; tupel */
270     for (end = begin; *end != ';'; end++)
271       if (*end == ',')
272         separator = end;
273
274     /* final element */
275     if ( (NULL == separator) ||
276          (begin == separator) ||
277          (separator == end - 1) )
278     {
279       LOG (GNUNET_ERROR_TYPE_ERROR,
280            _("Malformed input, could not parse `%s'\n"),
281            begin);
282       GNUNET_free (elements);
283       return;
284     }
285     *separator = 0;
286     /* read the element's key */
287     GNUNET_CRYPTO_hash (begin,
288                         strlen (begin),
289                         &element.key);
290
291     /* read the element's value */
292     if (1 !=
293         sscanf (separator + 1,
294                 "%" SCNd64 ";",
295                 &element.value) )
296     {
297       LOG (GNUNET_ERROR_TYPE_ERROR,
298            _("Could not convert `%s' to int64_t.\n"),
299            begin);
300       GNUNET_free (elements);
301       return;
302     }
303     element.value = GNUNET_htonll (element.value);
304     elements[i] = element;
305     begin = end + 1;
306   }
307
308   if ( ( (NULL != input_peer_id) &&
309          (NULL == (computation
310                    = GNUNET_SCALARPRODUCT_start_computation (cfg,
311                                                              &session_key,
312                                                              &peer_id,
313                                                              elements, element_count,
314                                                              &requester_callback,
315                                                              NULL))) ) ||
316        ( (NULL == input_peer_id) &&
317          (NULL == (computation
318                    = GNUNET_SCALARPRODUCT_accept_computation (cfg,
319                                                               &session_key,
320                                                               elements, element_count,
321                                                               &responder_callback,
322                                                               NULL))) ) )
323   {
324     fprintf (stderr,
325              _("Failed to initiate computation, were all keys unique?\n"));
326     GNUNET_free (elements);
327     return;
328   }
329   GNUNET_free (elements);
330   GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
331                                  NULL);
332   ret = 0;
333 }
334
335
336 /**
337  * The main function to the scalarproduct client.
338  *
339  * @param argc number of arguments from the command line
340  * @param argv command line arguments
341  * @return 0 ok, 1 on error
342  */
343 int
344 main (int argc, char *const *argv)
345 {
346   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
347     {'e', "elements", "\"key1,val1;key2,val2;...,keyn,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     {'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_session_key},
356     GNUNET_GETOPT_OPTION_END
357   };
358
359   return (GNUNET_OK ==
360           GNUNET_PROGRAM_run (argc,
361                               argv,
362                               "gnunet-scalarproduct",
363                               gettext_noop ("Calculate the Vectorproduct with a GNUnet peer."),
364                               options, &run, NULL)) ? ret : 1;
365 }
366
367 /* end of gnunet-scalarproduct.c */