summaryrefslogtreecommitdiff
path: root/src/scalarproduct/gnunet-scalarproduct.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/scalarproduct/gnunet-scalarproduct.c')
-rw-r--r--src/scalarproduct/gnunet-scalarproduct.c441
1 files changed, 224 insertions, 217 deletions
diff --git a/src/scalarproduct/gnunet-scalarproduct.c b/src/scalarproduct/gnunet-scalarproduct.c
index 7a86b27d2..fa0e1ccda 100644
--- a/src/scalarproduct/gnunet-scalarproduct.c
+++ b/src/scalarproduct/gnunet-scalarproduct.c
@@ -11,7 +11,7 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
@@ -33,7 +33,7 @@
33#include "gnunet_protocols.h" 33#include "gnunet_protocols.h"
34#include "scalarproduct.h" 34#include "scalarproduct.h"
35 35
36#define LOG(kind,...) GNUNET_log_from (kind, "gnunet-scalarproduct",__VA_ARGS__) 36#define LOG(kind, ...) GNUNET_log_from(kind, "gnunet-scalarproduct", __VA_ARGS__)
37 37
38 38
39/** 39/**
@@ -79,40 +79,44 @@ static struct GNUNET_SCALARPRODUCT_ComputationHandle *computation;
79 * @param status if our job was successfully processed 79 * @param status if our job was successfully processed
80 */ 80 */
81static void 81static void
82responder_callback (void *cls, 82responder_callback(void *cls,
83 enum GNUNET_SCALARPRODUCT_ResponseStatus status) 83 enum GNUNET_SCALARPRODUCT_ResponseStatus status)
84{ 84{
85 switch (status) 85 switch (status)
86 { 86 {
87 case GNUNET_SCALARPRODUCT_STATUS_SUCCESS: 87 case GNUNET_SCALARPRODUCT_STATUS_SUCCESS:
88 ret = 0; 88 ret = 0;
89 LOG (GNUNET_ERROR_TYPE_INFO, 89 LOG(GNUNET_ERROR_TYPE_INFO,
90 "Session %s concluded.\n", 90 "Session %s concluded.\n",
91 GNUNET_h2s (&session_key)); 91 GNUNET_h2s(&session_key));
92 break; 92 break;
93 case GNUNET_SCALARPRODUCT_STATUS_INVALID_RESPONSE: 93
94 LOG (GNUNET_ERROR_TYPE_ERROR, 94 case GNUNET_SCALARPRODUCT_STATUS_INVALID_RESPONSE:
95 "Session %s failed: invalid response\n", 95 LOG(GNUNET_ERROR_TYPE_ERROR,
96 GNUNET_h2s (&session_key)); 96 "Session %s failed: invalid response\n",
97 break; 97 GNUNET_h2s(&session_key));
98 case GNUNET_SCALARPRODUCT_STATUS_FAILURE: 98 break;
99 LOG (GNUNET_ERROR_TYPE_ERROR, 99
100 "Session %s failed: service failure\n", 100 case GNUNET_SCALARPRODUCT_STATUS_FAILURE:
101 GNUNET_h2s (&session_key)); 101 LOG(GNUNET_ERROR_TYPE_ERROR,
102 break; 102 "Session %s failed: service failure\n",
103 case GNUNET_SCALARPRODUCT_STATUS_DISCONNECTED: 103 GNUNET_h2s(&session_key));
104 LOG (GNUNET_ERROR_TYPE_ERROR, 104 break;
105 "Session %s failed: service disconnect!\n", 105
106 GNUNET_h2s (&session_key)); 106 case GNUNET_SCALARPRODUCT_STATUS_DISCONNECTED:
107 break; 107 LOG(GNUNET_ERROR_TYPE_ERROR,
108 default: 108 "Session %s failed: service disconnect!\n",
109 LOG (GNUNET_ERROR_TYPE_ERROR, 109 GNUNET_h2s(&session_key));
110 "Session %s failed: return code %d\n", 110 break;
111 GNUNET_h2s (&session_key), 111
112 status); 112 default:
113 } 113 LOG(GNUNET_ERROR_TYPE_ERROR,
114 "Session %s failed: return code %d\n",
115 GNUNET_h2s(&session_key),
116 status);
117 }
114 computation = NULL; 118 computation = NULL;
115 GNUNET_SCHEDULER_shutdown (); 119 GNUNET_SCHEDULER_shutdown();
116} 120}
117 121
118 122
@@ -124,56 +128,60 @@ responder_callback (void *cls,
124 * @param result the result in gnu/gcry MPI format 128 * @param result the result in gnu/gcry MPI format
125 */ 129 */
126static void 130static void
127requester_callback (void *cls, 131requester_callback(void *cls,
128 enum GNUNET_SCALARPRODUCT_ResponseStatus status, 132 enum GNUNET_SCALARPRODUCT_ResponseStatus status,
129 gcry_mpi_t result) 133 gcry_mpi_t result)
130{ 134{
131 unsigned char *buf; 135 unsigned char *buf;
132 gcry_error_t rc; 136 gcry_error_t rc;
133 137
134 switch (status) 138 switch (status)
135 {
136 case GNUNET_SCALARPRODUCT_STATUS_SUCCESS:
137 if (0 == (rc = gcry_mpi_aprint (GCRYMPI_FMT_HEX, &buf, NULL, result)))
138 { 139 {
139 ret = 0; 140 case GNUNET_SCALARPRODUCT_STATUS_SUCCESS:
140 fprintf (stdout, 141 if (0 == (rc = gcry_mpi_aprint(GCRYMPI_FMT_HEX, &buf, NULL, result)))
141 "%s\n", 142 {
142 buf); 143 ret = 0;
143 fflush (stdout); 144 fprintf(stdout,
145 "%s\n",
146 buf);
147 fflush(stdout);
148 }
149 else
150 LOG_GCRY(GNUNET_ERROR_TYPE_ERROR,
151 "gcry_mpi_aprint",
152 rc);
153 break;
154
155 case GNUNET_SCALARPRODUCT_STATUS_INVALID_RESPONSE:
156 LOG(GNUNET_ERROR_TYPE_ERROR,
157 "Session %s with peer %s failed: invalid response received\n",
158 GNUNET_h2s(&session_key),
159 GNUNET_i2s(&peer_id));
160 break;
161
162 case GNUNET_SCALARPRODUCT_STATUS_FAILURE:
163 LOG(GNUNET_ERROR_TYPE_ERROR,
164 "Session %s with peer %s failed: API failure\n",
165 GNUNET_h2s(&session_key),
166 GNUNET_i2s(&peer_id));
167 break;
168
169 case GNUNET_SCALARPRODUCT_STATUS_DISCONNECTED:
170 LOG(GNUNET_ERROR_TYPE_ERROR,
171 "Session %s with peer %s was disconnected from service.\n",
172 GNUNET_h2s(&session_key),
173 GNUNET_i2s(&peer_id));
174 break;
175
176 default:
177 LOG(GNUNET_ERROR_TYPE_ERROR,
178 "Session %s with peer %s failed: return code %d\n",
179 GNUNET_h2s(&session_key),
180 GNUNET_i2s(&peer_id),
181 status);
144 } 182 }
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; 183 computation = NULL;
176 GNUNET_SCHEDULER_shutdown (); 184 GNUNET_SCHEDULER_shutdown();
177} 185}
178 186
179 187
@@ -184,13 +192,13 @@ requester_callback (void *cls,
184 * @param tc unused 192 * @param tc unused
185 */ 193 */
186static void 194static void
187shutdown_task (void *cls) 195shutdown_task(void *cls)
188{ 196{
189 if (NULL != computation) 197 if (NULL != computation)
190 { 198 {
191 GNUNET_SCALARPRODUCT_cancel (computation); 199 GNUNET_SCALARPRODUCT_cancel(computation);
192 ret = 1; /* aborted */ 200 ret = 1; /* aborted */
193 } 201 }
194} 202}
195 203
196 204
@@ -203,10 +211,10 @@ shutdown_task (void *cls)
203 * @param cfg configuration 211 * @param cfg configuration
204 */ 212 */
205static void 213static void
206run (void *cls, 214run(void *cls,
207 char *const *args, 215 char *const *args,
208 const char *cfgfile, 216 const char *cfgfile,
209 const struct GNUNET_CONFIGURATION_Handle *cfg) 217 const struct GNUNET_CONFIGURATION_Handle *cfg)
210{ 218{
211 char *begin = input_elements; 219 char *begin = input_elements;
212 char *end; 220 char *end;
@@ -215,120 +223,120 @@ run (void *cls,
215 uint32_t element_count = 0; 223 uint32_t element_count = 0;
216 224
217 if (NULL == input_elements) 225 if (NULL == input_elements)
218 { 226 {
219 LOG (GNUNET_ERROR_TYPE_ERROR, 227 LOG(GNUNET_ERROR_TYPE_ERROR,
220 _("You must specify at least one message ID to check!\n")); 228 _("You must specify at least one message ID to check!\n"));
221 return; 229 return;
222 } 230 }
223 if ( (NULL == input_session_key) || 231 if ((NULL == input_session_key) ||
224 (0 == strlen (input_session_key)) ) 232 (0 == strlen(input_session_key)))
225 { 233 {
226 LOG (GNUNET_ERROR_TYPE_ERROR, 234 LOG(GNUNET_ERROR_TYPE_ERROR,
227 _("This program needs a session identifier for comparing vectors.\n")); 235 _("This program needs a session identifier for comparing vectors.\n"));
228 return; 236 return;
229 } 237 }
230 GNUNET_CRYPTO_hash (input_session_key, 238 GNUNET_CRYPTO_hash(input_session_key,
231 strlen (input_session_key), 239 strlen(input_session_key),
232 &session_key); 240 &session_key);
233 if ( (NULL != input_peer_id) && 241 if ((NULL != input_peer_id) &&
234 (GNUNET_OK != 242 (GNUNET_OK !=
235 GNUNET_CRYPTO_eddsa_public_key_from_string (input_peer_id, 243 GNUNET_CRYPTO_eddsa_public_key_from_string(input_peer_id,
236 strlen (input_peer_id), 244 strlen(input_peer_id),
237 &peer_id.public_key)) ) 245 &peer_id.public_key)))
238 { 246 {
239 LOG (GNUNET_ERROR_TYPE_ERROR, 247 LOG(GNUNET_ERROR_TYPE_ERROR,
240 _("Tried to set initiator mode, as peer ID was given. " 248 _("Tried to set initiator mode, as peer ID was given. "
241 "However, `%s' is not a valid peer identifier.\n"), 249 "However, `%s' is not a valid peer identifier.\n"),
242 input_peer_id); 250 input_peer_id);
243 return; 251 return;
244 } 252 }
245 if ( ('\'' == *begin) && 253 if (('\'' == *begin) &&
246 ('\'' == begin[strlen(begin)-1]) ) 254 ('\'' == begin[strlen(begin) - 1]))
247 { 255 {
248 begin[strlen(begin)-1] = '\0'; 256 begin[strlen(begin) - 1] = '\0';
249 if (strlen (begin) > 0) 257 if (strlen(begin) > 0)
250 begin++; 258 begin++;
251 } 259 }
252 for (end = begin; 0 != *end; end++) 260 for (end = begin; 0 != *end; end++)
253 if (*end == ';') 261 if (*end == ';')
254 element_count++; 262 element_count++;
255 if (0 == element_count) 263 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 { 264 {
279 LOG (GNUNET_ERROR_TYPE_ERROR, 265 LOG(GNUNET_ERROR_TYPE_ERROR,
280 _("Malformed input, could not parse `%s'\n"), 266 _("Need elements to compute the scalarproduct, got none.\n"));
281 begin);
282 GNUNET_free (elements);
283 return; 267 return;
284 } 268 }
285 *separator = 0; 269
286 /* read the element's key */ 270 elements = GNUNET_malloc(sizeof(struct GNUNET_SCALARPRODUCT_Element) * element_count);
287 GNUNET_CRYPTO_hash (begin, 271
288 strlen (begin), 272 for (i = 0; i < element_count; i++)
289 &element.key);
290
291 /* read the element's value */
292 if (1 !=
293 sscanf (separator + 1,
294 "%" SCNd64 ";",
295 &element.value) )
296 { 273 {
297 LOG (GNUNET_ERROR_TYPE_ERROR, 274 struct GNUNET_SCALARPRODUCT_Element element;
298 _("Could not convert `%s' to int64_t.\n"), 275 char* separator = NULL;
299 begin); 276
300 GNUNET_free (elements); 277 /* get the length of the current key,value; tupel */
301 return; 278 for (end = begin; *end != ';'; end++)
279 if (*end == ',')
280 separator = end;
281
282 /* final element */
283 if ((NULL == separator) ||
284 (begin == separator) ||
285 (separator == end - 1))
286 {
287 LOG(GNUNET_ERROR_TYPE_ERROR,
288 _("Malformed input, could not parse `%s'\n"),
289 begin);
290 GNUNET_free(elements);
291 return;
292 }
293 *separator = 0;
294 /* read the element's key */
295 GNUNET_CRYPTO_hash(begin,
296 strlen(begin),
297 &element.key);
298
299 /* read the element's value */
300 if (1 !=
301 sscanf(separator + 1,
302 "%" SCNd64 ";",
303 &element.value))
304 {
305 LOG(GNUNET_ERROR_TYPE_ERROR,
306 _("Could not convert `%s' to int64_t.\n"),
307 begin);
308 GNUNET_free(elements);
309 return;
310 }
311 element.value = GNUNET_htonll(element.value);
312 elements[i] = element;
313 begin = end + 1;
302 } 314 }
303 element.value = GNUNET_htonll (element.value); 315
304 elements[i] = element; 316 if (((NULL != input_peer_id) &&
305 begin = end + 1; 317 (NULL == (computation
306 } 318 = GNUNET_SCALARPRODUCT_start_computation(cfg,
307 319 &session_key,
308 if ( ( (NULL != input_peer_id) && 320 &peer_id,
309 (NULL == (computation 321 elements, element_count,
310 = GNUNET_SCALARPRODUCT_start_computation (cfg, 322 &requester_callback,
323 NULL)))) ||
324 ((NULL == input_peer_id) &&
325 (NULL == (computation
326 = GNUNET_SCALARPRODUCT_accept_computation(cfg,
311 &session_key, 327 &session_key,
312 &peer_id,
313 elements, element_count, 328 elements, element_count,
314 &requester_callback, 329 &responder_callback,
315 NULL))) ) || 330 NULL)))))
316 ( (NULL == input_peer_id) && 331 {
317 (NULL == (computation 332 fprintf(stderr,
318 = GNUNET_SCALARPRODUCT_accept_computation (cfg, 333 _("Failed to initiate computation, were all keys unique?\n"));
319 &session_key, 334 GNUNET_free(elements);
320 elements, element_count, 335 return;
321 &responder_callback, 336 }
322 NULL))) ) ) 337 GNUNET_free(elements);
323 { 338 GNUNET_SCHEDULER_add_shutdown(&shutdown_task,
324 fprintf (stderr, 339 NULL);
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; 340 ret = 0;
333} 341}
334 342
@@ -341,43 +349,42 @@ run (void *cls,
341 * @return 0 ok, 1 on error 349 * @return 0 ok, 1 on error
342 */ 350 */
343int 351int
344main (int argc, char *const *argv) 352main(int argc, char *const *argv)
345{ 353{
346 struct GNUNET_GETOPT_CommandLineOption options[] = { 354 struct GNUNET_GETOPT_CommandLineOption options[] = {
347 355 GNUNET_GETOPT_option_string('e',
348 GNUNET_GETOPT_option_string ('e', 356 "elements",
349 "elements", 357 "\"key1,val1;key2,val2;...,keyn,valn;\"",
350 "\"key1,val1;key2,val2;...,keyn,valn;\"", 358 gettext_noop("A comma separated list of elements to compare as vector with our remote peer."),
351 gettext_noop ("A comma separated list of elements to compare as vector with our remote peer."), 359 &input_elements),
352 &input_elements), 360
353 361 GNUNET_GETOPT_option_string('e',
354 GNUNET_GETOPT_option_string ('e', 362 "elements",
355 "elements", 363 "\"key1,val1;key2,val2;...,keyn,valn;\"",
356 "\"key1,val1;key2,val2;...,keyn,valn;\"", 364 gettext_noop("A comma separated list of elements to compare as vector with our remote peer."),
357 gettext_noop ("A comma separated list of elements to compare as vector with our remote peer."), 365 &input_elements),
358 &input_elements), 366
359 367 GNUNET_GETOPT_option_string('p',
360 GNUNET_GETOPT_option_string ('p', 368 "peer",
361 "peer", 369 "PEERID",
362 "PEERID", 370 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."),
363 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."), 371 &input_peer_id),
364 &input_peer_id), 372
365 373 GNUNET_GETOPT_option_string('k',
366 GNUNET_GETOPT_option_string ('k', 374 "key",
367 "key", 375 "TRANSACTION_ID",
368 "TRANSACTION_ID", 376 gettext_noop("Transaction ID shared with peer."),
369 gettext_noop ("Transaction ID shared with peer."), 377 &input_session_key),
370 &input_session_key),
371 378
372 GNUNET_GETOPT_OPTION_END 379 GNUNET_GETOPT_OPTION_END
373 }; 380 };
374 381
375 return (GNUNET_OK == 382 return (GNUNET_OK ==
376 GNUNET_PROGRAM_run (argc, 383 GNUNET_PROGRAM_run(argc,
377 argv, 384 argv,
378 "gnunet-scalarproduct", 385 "gnunet-scalarproduct",
379 gettext_noop ("Calculate the Vectorproduct with a GNUnet peer."), 386 gettext_noop("Calculate the Vectorproduct with a GNUnet peer."),
380 options, &run, NULL)) ? ret : 1; 387 options, &run, NULL)) ? ret : 1;
381} 388}
382 389
383/* end of gnunet-scalarproduct.c */ 390/* end of gnunet-scalarproduct.c */