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