aboutsummaryrefslogtreecommitdiff
path: root/src/scalarproduct/gnunet-scalarproduct.c
diff options
context:
space:
mode:
authorChristian Fuchs <christian.fuchs@cfuchs.net>2013-09-03 12:07:52 +0000
committerChristian Fuchs <christian.fuchs@cfuchs.net>2013-09-03 12:07:52 +0000
commitdc91365855b72f0c97c0a7abc6eac7d479704daf (patch)
treea0242025c976f2c23b64cdd3516a84b6681d4c57 /src/scalarproduct/gnunet-scalarproduct.c
parentd4922969a54f83b3c2d7503a9bda2708700869c8 (diff)
downloadgnunet-dc91365855b72f0c97c0a7abc6eac7d479704daf.tar.gz
gnunet-dc91365855b72f0c97c0a7abc6eac7d479704daf.zip
re-added collective disconnect command to the scalarproduct API
adapted the scalarproduct client to use the new API modified scalarproduct.h to include proper logging for gcrypt errors cancel is now quiet added proper gcry logging to the SP-API
Diffstat (limited to 'src/scalarproduct/gnunet-scalarproduct.c')
-rw-r--r--src/scalarproduct/gnunet-scalarproduct.c394
1 files changed, 167 insertions, 227 deletions
diff --git a/src/scalarproduct/gnunet-scalarproduct.c b/src/scalarproduct/gnunet-scalarproduct.c
index 78acb62ce..0bce75085 100644
--- a/src/scalarproduct/gnunet-scalarproduct.c
+++ b/src/scalarproduct/gnunet-scalarproduct.c
@@ -34,6 +34,20 @@
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
38struct ScalarProductCallbackClosure
39{
40 /**
41 * the session key identifying this computation
42 */
43 struct GNUNET_HashCode key;
44
45 /**
46 * PeerID we want to compute a scalar product with
47 */
48 struct GNUNET_PeerIdentity peer;
49};
50
37/** 51/**
38 * Option -p: destination peer identity for checking message-ids with 52 * Option -p: destination peer identity for checking message-ids with
39 */ 53 */
@@ -55,57 +69,10 @@ static char *input_elements = NULL;
55static char *input_mask = NULL; 69static char *input_mask = NULL;
56 70
57/** 71/**
58 * the count of the messages sent to the service for processing
59 */
60static unsigned short element_count;
61
62/**
63 * the count of the mask bytes
64 */
65unsigned short mask_length = 0;
66
67/**
68 * the count of the number of mask bytes
69 */
70unsigned short mask_bytes;
71
72/**
73 * the array of converted message IDs to send to our service
74 */
75static int32_t * elements = NULL;
76
77/**
78 * the array of converted message IDs to send to our service
79 */
80static unsigned char * mask = NULL;
81
82/**
83 * information about the peer we are comparing with
84 */
85struct GNUNET_PeerIdentity peer;
86
87/**
88 * information about the peer we are comparing with
89 */
90struct GNUNET_HashCode key;
91
92/**
93 * Pointer to the GNUNET_SCALARPRODUCT_Handle
94 */
95struct GNUNET_SCALARPRODUCT_Handle *handle;
96
97/**
98 * Global return value 72 * Global return value
99 */ 73 */
100static int ret; 74static int ret;
101 75
102struct GNUNET_SCALARPRODUCT_TestCls
103{
104 struct GNUNET_SCALARPRODUCT_Handle * h;
105};
106
107struct GNUNET_SCALARPRODUCT_TestCls test_cls;
108
109 76
110/** 77/**
111 * Callback called if we are initiating a new computation session 78 * Callback called if we are initiating a new computation session
@@ -115,33 +82,30 @@ struct GNUNET_SCALARPRODUCT_TestCls test_cls;
115 */ 82 */
116static void 83static void
117responder_callback (void *cls, 84responder_callback (void *cls,
118 const struct GNUNET_HashCode * key,
119 enum GNUNET_SCALARPRODUCT_ResponseStatus status) 85 enum GNUNET_SCALARPRODUCT_ResponseStatus status)
120{ 86{
87 struct ScalarProductCallbackClosure * closure = cls;
121 ret = -1; 88 ret = -1;
122 89
123 switch (status) 90 switch (status)
124 { 91 {
125 case GNUNET_SCALARPRODUCT_Status_Success: 92 case GNUNET_SCALARPRODUCT_Status_Success:
126 ret = 0; 93 ret = 0;
127 LOG (GNUNET_ERROR_TYPE_INFO, "Session %s concluded.\n", GNUNET_h2s (key)); 94 LOG (GNUNET_ERROR_TYPE_INFO, "Session %s concluded.\n", GNUNET_h2s (&closure->key));
128 break; 95 break;
129 case GNUNET_SCALARPRODUCT_Status_InvalidResponse: 96 case GNUNET_SCALARPRODUCT_Status_InvalidResponse:
130 LOG (GNUNET_ERROR_TYPE_ERROR, "Session %s failed: invalid response\n", GNUNET_h2s (key)); 97 LOG (GNUNET_ERROR_TYPE_ERROR, "Session %s failed: invalid response\n", GNUNET_h2s (&closure->key));
131 break; 98 break;
132 case GNUNET_SCALARPRODUCT_Status_Timeout: 99 case GNUNET_SCALARPRODUCT_Status_Failure:
133 LOG (GNUNET_ERROR_TYPE_ERROR, "Session %s failed: timeout\n", GNUNET_h2s (key)); 100 LOG (GNUNET_ERROR_TYPE_ERROR, "Session %s failed: service failure\n", GNUNET_h2s (&closure->key));
134 break; 101 case GNUNET_SCALARPRODUCT_Status_ServiceDisconnected:
135 case GNUNET_SCALARPRODUCT_Status_Failure: 102 LOG (GNUNET_ERROR_TYPE_ERROR, "Session %s failed: service disconnect!!\n", GNUNET_h2s (&closure->key));
136 LOG (GNUNET_ERROR_TYPE_ERROR, "Session %s failed: service failure\n", GNUNET_h2s (key)); 103 break;
137 case GNUNET_SCALARPRODUCT_Status_ServiceDisconnected: 104 default:
138 LOG (GNUNET_ERROR_TYPE_ERROR, "Session %s failed: service disconnect!!\n", GNUNET_h2s (key)); 105 LOG (GNUNET_ERROR_TYPE_ERROR, "Session %s failed: return code %d\n", GNUNET_h2s (&closure->key), status);
139 break; 106 }
140 default: 107
141 LOG (GNUNET_ERROR_TYPE_ERROR, "Session %s failed: return code %d\n", GNUNET_h2s (key), (int) status); 108 GNUNET_SCALARPRODUCT_disconnect ();
142 }
143
144 GNUNET_SCALARPRODUCT_cancel (handle);
145 GNUNET_SCHEDULER_shutdown (); 109 GNUNET_SCHEDULER_shutdown ();
146} 110}
147 111
@@ -159,57 +123,37 @@ responder_callback (void *cls,
159 */ 123 */
160static void 124static void
161requester_callback (void *cls, 125requester_callback (void *cls,
162 const struct GNUNET_HashCode * key, 126 enum GNUNET_SCALARPRODUCT_ResponseStatus status,
163 const struct GNUNET_PeerIdentity * peer, 127 gcry_mpi_t result)
164 enum GNUNET_SCALARPRODUCT_ResponseStatus status,
165 const struct GNUNET_SCALARPRODUCT_client_response *msg)
166{ 128{
167 uint32_t product_len; 129 struct ScalarProductCallbackClosure * closure = cls;
130 unsigned char * buf;
131 gcry_error_t rc;
168 ret = -1; 132 ret = -1;
169 133
170 switch (status) 134 switch (status)
171 { 135 {
172 case GNUNET_SCALARPRODUCT_Status_Success: 136 case GNUNET_SCALARPRODUCT_Status_Success:
173 product_len = ntohl (msg->product_length); 137
174 138 if (0 == (rc = gcry_mpi_aprint (GCRYMPI_FMT_HEX, &buf, NULL, result)))
175 LOG (GNUNET_ERROR_TYPE_INFO, "Session %s concluded.\n", GNUNET_h2s (key)); 139 printf ("Successfully computed result for session %s with peer %s: %s\n", GNUNET_h2s (&closure->key), GNUNET_i2s (&closure->peer), buf);
176 140 else {
177 if (0 < product_len && NULL != &msg[1]) 141 printf ("Session %s with peer %s failed\n", GNUNET_h2s (&closure->key), GNUNET_i2s (&closure->peer));
178 { 142 LOG_GCRY(GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_aprint", rc);
179 gcry_mpi_t result;
180 size_t read = 0;
181
182 if (0 != gcry_mpi_scan (&result, GCRYMPI_FMT_USG, &msg[1], product_len, &read))
183 LOG (GNUNET_ERROR_TYPE_ERROR, "Could not convert to mpi to value!\n");
184 else
185 {
186 unsigned char * buf;
187 gcry_mpi_aprint (GCRYMPI_FMT_HEX, &buf, NULL, result);
188
189 printf ("Successfully computed result for session %s: %s\n", GNUNET_h2s (key), buf);
190 ret = 0;
191 }
192 }
193 else
194 { //currently not used, but if we get more info due to MESH we will need this
195 LOG (GNUNET_ERROR_TYPE_ERROR, "Service-side error in session %s, return code: %d\n", GNUNET_h2s (key), product_len);
196 }
197 break;
198 case GNUNET_SCALARPRODUCT_Status_InvalidResponse:
199 LOG (GNUNET_ERROR_TYPE_ERROR, "Session %s failed: invalid response\n", GNUNET_h2s (key));
200 break;
201 case GNUNET_SCALARPRODUCT_Status_Timeout:
202 LOG (GNUNET_ERROR_TYPE_ERROR, "Session %s failed: timeout\n", GNUNET_h2s (key));
203 break;
204 case GNUNET_SCALARPRODUCT_Status_Failure:
205 LOG (GNUNET_ERROR_TYPE_ERROR, "Session %s failed: service failure\n", GNUNET_h2s (key));
206 case GNUNET_SCALARPRODUCT_Status_ServiceDisconnected:
207 LOG (GNUNET_ERROR_TYPE_ERROR, "Disconnected from service.\n", GNUNET_h2s (key));
208 break;
209 default:
210 LOG (GNUNET_ERROR_TYPE_ERROR, "Session %s failed: return code %d\n", GNUNET_h2s (key), (int) status);
211 } 143 }
212 GNUNET_SCALARPRODUCT_cancel (handle); 144 break;
145 case GNUNET_SCALARPRODUCT_Status_InvalidResponse:
146 LOG (GNUNET_ERROR_TYPE_ERROR, "Session %s with peer %s failed: invalid response received\n", GNUNET_h2s (&closure->key), GNUNET_i2s (&closure->peer));
147 break;
148 case GNUNET_SCALARPRODUCT_Status_Failure:
149 LOG (GNUNET_ERROR_TYPE_ERROR, "Session %s with peer %s failed: API failure\n", GNUNET_h2s (&closure->key), GNUNET_i2s (&closure->peer));
150 case GNUNET_SCALARPRODUCT_Status_ServiceDisconnected:
151 LOG (GNUNET_ERROR_TYPE_ERROR, "Session %s with peer %s was disconnected from service.\n", GNUNET_h2s (&closure->key), GNUNET_i2s (&closure->peer));
152 break;
153 default:
154 LOG (GNUNET_ERROR_TYPE_ERROR, "Session %s with peer %s failed: return code %d\n", GNUNET_h2s (&closure->key), GNUNET_i2s (&closure->peer), status);
155 }
156 GNUNET_SCALARPRODUCT_disconnect ();
213 GNUNET_SCHEDULER_shutdown (); 157 GNUNET_SCHEDULER_shutdown ();
214} 158}
215 159
@@ -233,44 +177,90 @@ run (void *cls,
233 int32_t element; 177 int32_t element;
234 int i; 178 int i;
235 ret = -1; 179 ret = -1;
180 int32_t * elements;
181 unsigned char * mask;
182 unsigned short mask_bytes;
183 unsigned short element_count;
184 struct ScalarProductCallbackClosure * closure;
236 185
237 if (NULL == input_elements) 186 if (NULL == input_elements)
238 { 187 {
239 FPRINTF (stderr, "%s", _ ("You must specify at least one message ID to check!\n")); 188 FPRINTF (stderr, "%s", _ ("You must specify at least one message ID to check!\n"));
240 return; 189 return;
241 } 190 }
242 191
243 if (NULL == input_key) 192 if (NULL == input_key)
244 { 193 {
245 FPRINTF (stderr, "%s", _ ("This program needs a session identifier for comparing vectors.\n")); 194 FPRINTF (stderr, "%s", _ ("This program needs a session identifier for comparing vectors.\n"));
246 return; 195 return;
247 } 196 }
248 197
249 if (1 > strnlen (input_key, sizeof (struct GNUNET_HashCode))) 198 if (1 > strnlen (input_key, sizeof (struct GNUNET_HashCode)))
250 { 199 {
251 FPRINTF (stderr, _ ("Please give a session key for --input_key!\n")); 200 FPRINTF (stderr, _ ("Please give a session key for --input_key!\n"));
252 return; 201 return;
253 } 202 }
254 GNUNET_CRYPTO_hash (input_key, strlen (input_key), &key); 203 closure = GNUNET_new(struct ScalarProductCallbackClosure);
204 GNUNET_CRYPTO_hash (input_key, strlen (input_key), &closure->key);
255 205
256 if (input_peer_id && GNUNET_OK != GNUNET_CRYPTO_hash_from_string (input_peer_id, 206 if (input_peer_id && GNUNET_OK != GNUNET_CRYPTO_hash_from_string (input_peer_id,
257 (struct GNUNET_HashCode *) &peer)) 207 (struct GNUNET_HashCode *) &closure->peer))
258 { 208 {
259 FPRINTF (stderr, _ ("Tried to set initiator mode, as peer ID was given. " 209 FPRINTF (stderr, _ ("Tried to set initiator mode, as peer ID was given. "
260 "However, `%s' is not a valid peer identifier.\n"), 210 "However, `%s' is not a valid peer identifier.\n"),
261 input_peer_id); 211 input_peer_id);
262 return; 212 return;
263 } 213 }
264 214
265 int exit_loop = 0; 215 int exit_loop = 0;
266 /* Read input_elements_peer1, and put in elements_peer1 array */ 216 /* Read input_elements_peer1, and put in elements_peer1 array */
267 do 217 do
218 {
219 unsigned int mcount = element_count;
220 //ignore empty rows of ,,,,,,
221 while (*begin == ',')
222 begin++;
223 // get the length of the current element and replace , with null
224 for (end = begin; *end && *end != ','; end++);
225
226 if (*end == '\0')
227 exit_loop = 1;
228
229 if (*end == ',')
230 *end = '\0';
231
232 if (1 != sscanf (begin, "%" SCNd32, &element))
233 {
234 FPRINTF (stderr, _ ("Could not convert `%s' to int32_t.\n"), begin);
235 return;
236 }
237
238 GNUNET_array_append (elements, mcount, element);
239 element_count++;
240
241 begin = ++end;
242 }
243 while (!exit_loop);
244
245 GNUNET_assert (elements != NULL);
246 GNUNET_assert (element_count > 1);
247 mask_bytes = element_count / 8 + (element_count % 8 ? 1 : 0);
248 mask = GNUNET_malloc ((element_count / 8) + 2);
249
250 /* Read input_mask_peer1 and read in mask_peer1 array */
251 if (NULL != input_mask)
252 {
253 begin = input_mask;
254 unsigned short mask_count = 0;
255 int exit_loop = 0;
256
257 do
268 { 258 {
269 unsigned int mcount = element_count;
270 //ignore empty rows of ,,,,,, 259 //ignore empty rows of ,,,,,,
271 while (*begin == ',') 260 while (* begin == ',')
272 begin++; 261 begin++;
273 // get the length of the current element and replace , with null 262 // get the length of the current element and replace , with null
263 // gnunet_ascii-armor uses base32, thus we can use , as separator!
274 for (end = begin; *end && *end != ','; end++); 264 for (end = begin; *end && *end != ','; end++);
275 265
276 if (*end == '\0') 266 if (*end == '\0')
@@ -280,112 +270,62 @@ run (void *cls,
280 *end = '\0'; 270 *end = '\0';
281 271
282 if (1 != sscanf (begin, "%" SCNd32, &element)) 272 if (1 != sscanf (begin, "%" SCNd32, &element))
283 { 273 {
284 FPRINTF (stderr, _ ("Could not convert `%s' to int32_t.\n"), begin); 274 FPRINTF (stderr, _ ("Could not convert `%s' to int32_t.\n"), begin);
285 return; 275 return;
286 } 276 }
287 277
288 GNUNET_array_append (elements, mcount, element); 278 GNUNET_assert (mask_count <= element_count);
289 element_count++;
290 279
291 begin = ++end; 280 if (element)
292 } 281 mask[mask_count / 8] = mask[mask_count / 8] | 1 << (mask_count % 8);
293 while (!exit_loop);
294 282
295 GNUNET_assert (elements != NULL); 283 mask_count++;
296 GNUNET_assert (element_count > 1); 284 begin = ++end;
297 mask_length = element_count / 8 + (element_count % 8 ? 1 : 0);
298 mask = GNUNET_malloc ((element_count / 8) + 2);
299
300 /* Read input_mask_peer1 and read in mask_peer1 array */
301 if (NULL != input_mask)
302 {
303 begin = input_mask;
304 unsigned short mask_count = 0;
305 int exit_loop = 0;
306
307 do
308 {
309 //ignore empty rows of ,,,,,,
310 while (* begin == ',')
311 begin++;
312 // get the length of the current element and replace , with null
313 // gnunet_ascii-armor uses base32, thus we can use , as separator!
314 for (end = begin; *end && *end != ','; end++);
315
316 if (*end == '\0')
317 exit_loop = 1;
318
319 if (*end == ',')
320 *end = '\0';
321
322 if (1 != sscanf (begin, "%" SCNd32, &element))
323 {
324 FPRINTF (stderr, _ ("Could not convert `%s' to int32_t.\n"), begin);
325 return;
326 }
327
328 GNUNET_assert (mask_count <= element_count);
329
330 if (element)
331 mask[mask_count / 8] = mask[mask_count / 8] | 1 << (mask_count % 8);
332
333 mask_count++;
334 begin = ++end;
335 }
336 while (!exit_loop);
337 // +1 to see if we would have more data, which would indicate malformed/superficial input
338 GNUNET_assert (mask_count == element_count);
339 } 285 }
286 while (!exit_loop);
287 // +1 to see if we would have more data, which would indicate malformed/superficial input
288 GNUNET_assert (mask_count == element_count);
289 }
340 else if (input_peer_id) 290 else if (input_peer_id)
341 { 291 {
342 for (i = 0; i <= mask_length; i++) 292 for (i = 0; i <= mask_bytes; i++)
343 mask[i] = UCHAR_MAX; // all 1's 293 mask[i] = UCHAR_MAX; // all 1's
344 } 294 }
345 295
346 handle = GNUNET_SCALARPRODUCT_connect (cfg);
347 if (handle == NULL)
348 {
349 FPRINTF (stderr, _ ("Could not connect to the GNUNET Vector Product Service\n"));
350 return;
351 }
352
353 test_cls.h = handle;
354 296
355 if (input_peer_id && !GNUNET_SCALARPRODUCT_request (handle, 297 if (input_peer_id && !GNUNET_SCALARPRODUCT_request (cfg,
356 &key, 298 &closure->key,
357 &peer, 299 &closure->peer,
358 element_count, 300 elements, element_count,
359 mask_length, 301 mask, mask_bytes,
360 elements, mask,
361 GNUNET_TIME_UNIT_MINUTES,
362 &requester_callback, 302 &requester_callback,
363 (void *) &test_cls)) 303 (void *) &closure))
364 return; 304 return;
365 if ( !input_peer_id && !GNUNET_SCALARPRODUCT_prepare_response (handle, 305
366 &key, 306
367 element_count, 307 if (!input_peer_id && !GNUNET_SCALARPRODUCT_response (cfg,
368 elements, 308 &closure->key,
369 GNUNET_TIME_UNIT_MINUTES, 309 elements, element_count,
370 &responder_callback, 310 &responder_callback,
371 (void *) &test_cls)) 311 (void *) &closure))
372 return; 312 return;
373 313
374 ret = 0; 314 ret = 0;
375} 315}
376 316
377 317
378/** 318 /**
379 * The main function to the scalarproduct client. 319 * The main function to the scalarproduct client.
380 * 320 *
381 * @param argc number of arguments from the command line 321 * @param argc number of arguments from the command line
382 * @param argv command line arguments 322 * @param argv command line arguments
383 * @return 0 ok, 1 on error 323 * @return 0 ok, 1 on error
384 */ 324 */
385int 325 int
386main (int argc, char *const *argv) 326main (int argc, char *const *argv)
387{ 327{
388 static const struct GNUNET_GETOPT_CommandLineOption options[] = { 328 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
389 {'e', "elements", "\"val1,val2,...,valn\"", 329 {'e', "elements", "\"val1,val2,...,valn\"",
390 gettext_noop ("A comma separated list of elements to compare as vector with our remote peer."), 330 gettext_noop ("A comma separated list of elements to compare as vector with our remote peer."),
391 1, &GNUNET_GETOPT_set_string, &input_elements}, 331 1, &GNUNET_GETOPT_set_string, &input_elements},
@@ -398,14 +338,14 @@ main (int argc, char *const *argv)
398 {'k', "key", "TRANSACTION_ID", 338 {'k', "key", "TRANSACTION_ID",
399 gettext_noop ("Transaction ID shared with peer."), 339 gettext_noop ("Transaction ID shared with peer."),
400 1, &GNUNET_GETOPT_set_string, &input_key}, 340 1, &GNUNET_GETOPT_set_string, &input_key},
401 GNUNET_GETOPT_OPTION_END 341 GNUNET_GETOPT_OPTION_END
402 }; 342 };
403 343
404 return (GNUNET_OK == 344 return (GNUNET_OK ==
405 GNUNET_PROGRAM_run (argc, 345 GNUNET_PROGRAM_run (argc,
406 argv, 346 argv,
407 "gnunet-scalarproduct", 347 "gnunet-scalarproduct",
408 gettext_noop ("Calculate the Vectorproduct with a GNUnet peer."), 348 gettext_noop ("Calculate the Vectorproduct with a GNUnet peer."),
409 options, &run, NULL)) ? ret : 1; 349 options, &run, NULL)) ? ret : 1;
410} 350}
411 351