aboutsummaryrefslogtreecommitdiff
path: root/src/scalarproduct/gnunet-scalarproduct.c
diff options
context:
space:
mode:
authorChristian Fuchs <christian.fuchs@cfuchs.net>2013-09-09 16:15:32 +0000
committerChristian Fuchs <christian.fuchs@cfuchs.net>2013-09-09 16:15:32 +0000
commitaba599ddd94139578193c9bcdc774ac558a0f15f (patch)
treefcd27722abc5968b8b5ad21f38a3139093468ad8 /src/scalarproduct/gnunet-scalarproduct.c
parent19933122ffe6ff84cb0dcccb2782704f89df54bb (diff)
downloadgnunet-aba599ddd94139578193c9bcdc774ac558a0f15f.tar.gz
gnunet-aba599ddd94139578193c9bcdc774ac558a0f15f.zip
sanitized the scalarproduct client
fixed a typo in the SP API changed SP-service to always use GCRYMPI_FMT_STD when sending the result back to client. Even though gcry currently can not print negative numbers, we can just remove the if-else block above it once that's supported
Diffstat (limited to 'src/scalarproduct/gnunet-scalarproduct.c')
-rw-r--r--src/scalarproduct/gnunet-scalarproduct.c177
1 files changed, 89 insertions, 88 deletions
diff --git a/src/scalarproduct/gnunet-scalarproduct.c b/src/scalarproduct/gnunet-scalarproduct.c
index 5939d755e..f56fd7625 100644
--- a/src/scalarproduct/gnunet-scalarproduct.c
+++ b/src/scalarproduct/gnunet-scalarproduct.c
@@ -34,6 +34,7 @@
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#define INPUTSTRINGLENGTH 1024
37 38
38struct ScalarProductCallbackClosure 39struct ScalarProductCallbackClosure
39{ 40{
@@ -71,7 +72,7 @@ static char *input_mask = NULL;
71/** 72/**
72 * Global return value 73 * Global return value
73 */ 74 */
74static int ret; 75static int ret = -1;
75 76
76 77
77/** 78/**
@@ -85,7 +86,6 @@ responder_callback (void *cls,
85 enum GNUNET_SCALARPRODUCT_ResponseStatus status) 86 enum GNUNET_SCALARPRODUCT_ResponseStatus status)
86{ 87{
87 struct ScalarProductCallbackClosure * closure = cls; 88 struct ScalarProductCallbackClosure * closure = cls;
88 ret = -1;
89 89
90 switch (status) 90 switch (status)
91 { 91 {
@@ -104,9 +104,6 @@ responder_callback (void *cls,
104 default: 104 default:
105 LOG (GNUNET_ERROR_TYPE_ERROR, "Session %s failed: return code %d\n", GNUNET_h2s (&closure->key), status); 105 LOG (GNUNET_ERROR_TYPE_ERROR, "Session %s failed: return code %d\n", GNUNET_h2s (&closure->key), status);
106 } 106 }
107
108 GNUNET_SCALARPRODUCT_disconnect ();
109 GNUNET_SCHEDULER_shutdown ();
110} 107}
111 108
112 109
@@ -129,16 +126,17 @@ requester_callback (void *cls,
129 struct ScalarProductCallbackClosure * closure = cls; 126 struct ScalarProductCallbackClosure * closure = cls;
130 unsigned char * buf; 127 unsigned char * buf;
131 gcry_error_t rc; 128 gcry_error_t rc;
132 ret = -1;
133 129
134 switch (status) 130 switch (status)
135 { 131 {
136 case GNUNET_SCALARPRODUCT_Status_Success: 132 case GNUNET_SCALARPRODUCT_Status_Success:
137
138 if (0 == (rc = gcry_mpi_aprint (GCRYMPI_FMT_HEX, &buf, NULL, result))) 133 if (0 == (rc = gcry_mpi_aprint (GCRYMPI_FMT_HEX, &buf, NULL, result)))
139 printf("%s", buf); 134 {
135 ret = 0;
136 printf ("%s", buf);
137 }
140 else 138 else
141 LOG_GCRY(GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_aprint", rc); 139 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_aprint", rc);
142 break; 140 break;
143 case GNUNET_SCALARPRODUCT_Status_InvalidResponse: 141 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)); 142 LOG (GNUNET_ERROR_TYPE_ERROR, "Session %s with peer %s failed: invalid response received\n", GNUNET_h2s (&closure->key), GNUNET_i2s (&closure->peer));
@@ -151,8 +149,6 @@ requester_callback (void *cls,
151 default: 149 default:
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); 150 LOG (GNUNET_ERROR_TYPE_ERROR, "Session %s with peer %s failed: return code %d\n", GNUNET_h2s (&closure->key), GNUNET_i2s (&closure->peer), status);
153 } 151 }
154 GNUNET_SCALARPRODUCT_disconnect ();
155 GNUNET_SCHEDULER_shutdown ();
156} 152}
157 153
158 154
@@ -174,156 +170,161 @@ run (void *cls,
174 char * end; 170 char * end;
175 int32_t element; 171 int32_t element;
176 int i; 172 int i;
177 ret = -1;
178 int32_t * elements; 173 int32_t * elements;
179 unsigned char * mask; 174 unsigned char * mask;
180 unsigned short mask_bytes; 175 unsigned short mask_bytes;
181 unsigned short element_count; 176 unsigned short element_count = 0;
182 struct ScalarProductCallbackClosure * closure; 177 struct ScalarProductCallbackClosure * closure;
183 178
184 if (NULL == input_elements) 179 if (NULL == input_elements)
185 { 180 {
186 FPRINTF (stderr, "%s", _ ("You must specify at least one message ID to check!\n")); 181 LOG (GNUNET_ERROR_TYPE_ERROR, _ ("You must specify at least one message ID to check!\n"));
187 return; 182 return;
188 } 183 }
189 184
190 if (NULL == input_key) 185 if (NULL == input_key)
191 { 186 {
192 FPRINTF (stderr, "%s", _ ("This program needs a session identifier for comparing vectors.\n")); 187 LOG (GNUNET_ERROR_TYPE_ERROR, _ ("This program needs a session identifier for comparing vectors.\n"));
193 return; 188 return;
194 } 189 }
195 190
196 if (1 > strnlen (input_key, sizeof (struct GNUNET_HashCode))) 191 if (1 > strnlen (input_key, sizeof (struct GNUNET_HashCode)))
197 { 192 {
198 FPRINTF (stderr, _ ("Please give a session key for --input_key!\n")); 193 LOG (GNUNET_ERROR_TYPE_ERROR, _ ("Please give a session key for --input_key!\n"));
199 return; 194 return;
200 } 195 }
201 closure = GNUNET_new(struct ScalarProductCallbackClosure); 196 closure = GNUNET_new (struct ScalarProductCallbackClosure);
202 GNUNET_CRYPTO_hash (input_key, strlen (input_key), &closure->key); 197 GNUNET_CRYPTO_hash (input_key, strlen (input_key), &closure->key);
203 198
204 if (input_peer_id && GNUNET_OK != GNUNET_CRYPTO_hash_from_string (input_peer_id, 199 if (input_peer_id && GNUNET_OK != GNUNET_CRYPTO_hash_from_string (input_peer_id,
205 (struct GNUNET_HashCode *) &closure->peer)) 200 (struct GNUNET_HashCode *) &closure->peer))
206 { 201 {
207 FPRINTF (stderr, _ ("Tried to set initiator mode, as peer ID was given. " 202 LOG (GNUNET_ERROR_TYPE_ERROR, _ ("Tried to set initiator mode, as peer ID was given. "
208 "However, `%s' is not a valid peer identifier.\n"), 203 "However, `%s' is not a valid peer identifier.\n"),
209 input_peer_id); 204 input_peer_id);
210 return; 205 return;
211 } 206 }
212 207
213 int exit_loop = 0; 208 /* Count input_elements_peer1, and put in elements_peer1 array */
214 /* Read input_elements_peer1, and put in elements_peer1 array */
215 do 209 do
216 { 210 {
217 unsigned int mcount = element_count;
218 //ignore empty rows of ,,,,,,
219 while (*begin == ',')
220 begin++;
221 // get the length of the current element and replace , with null 211 // get the length of the current element and replace , with null
222 for (end = begin; *end && *end != ','; end++); 212 for (end = begin; *end && *end != ','; end++);
223 213
224 if (*end == '\0') 214 if (1 == sscanf (begin, "%" SCNd32",", &element))
225 exit_loop = 1; 215 {
226 216 //element in the middle
227 if (*end == ',') 217 element_count++;
228 *end = '\0'; 218 begin = end + 1;
229 219 }
230 if (1 != sscanf (begin, "%" SCNd32, &element)) 220 else if (*begin == 0)
221 {
222 break;
223 }
224 else
231 { 225 {
232 FPRINTF (stderr, _ ("Could not convert `%s' to int32_t.\n"), begin); 226 LOG (GNUNET_ERROR_TYPE_ERROR, _ ("Could not convert `%s' to int32_t.\n"), begin);
233 return; 227 return;
234 } 228 }
229 }
230 while (1);
231 if (0 == element_count)
232 {
235 233
236 GNUNET_array_append (elements, mcount, element); 234 return;
237 element_count++; 235 }
238 236
239 begin = ++end; 237 begin = input_elements;
238 elements = GNUNET_malloc (sizeof (int32_t) * element_count);
239 /* Read input_elements_peer1, and put in elements_peer1 array */
240 do
241 {
242 // get the length of the current element and replace , with null
243 for (end = begin; *end && *end != ','; end++);
244
245 if (1 == sscanf (begin, "%" SCNd32",", &element))
246 {
247 //element in the middle
248 element_count++;
249 begin = end + 1;
250 }
251 else if (*begin == 0)
252 {
253 break;
254 }
240 } 255 }
241 while (!exit_loop); 256 while (1);
242 257
243 GNUNET_assert (elements != NULL);
244 GNUNET_assert (element_count > 1);
245 mask_bytes = element_count / 8 + (element_count % 8 ? 1 : 0); 258 mask_bytes = element_count / 8 + (element_count % 8 ? 1 : 0);
246 mask = GNUNET_malloc ((element_count / 8) + 2); 259 mask = GNUNET_malloc ((element_count / 8) + 2);
247 260
248 /* Read input_mask_peer1 and read in mask_peer1 array */ 261 /* Read input_mask_peer1 and read in mask_peer1 array */
249 if (NULL != input_mask) 262 if ((NULL != input_peer_id) && (NULL != input_mask))
250 { 263 {
251 begin = input_mask; 264 begin = input_mask;
252 unsigned short mask_count = 0; 265 unsigned short mask_count = 0;
253 int exit_loop = 0;
254 266
255 do 267 do
256 { 268 {
257 //ignore empty rows of ,,,,,,
258 while (* begin == ',')
259 begin++;
260 // get the length of the current element and replace , with null 269 // 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 for (end = begin; *end && *end != ','; end++);
263 271
264 if (*end == '\0') 272 if (1 == sscanf (begin, "%" SCNd32 ",", &element))
265 exit_loop = 1; 273 {
266 274 //element in the middle
267 if (*end == ',') 275 begin = end + 1;
268 *end = '\0'; 276 }
269 277 else if (*begin == 0)
270 if (1 != sscanf (begin, "%" SCNd32, &element))
271 { 278 {
272 FPRINTF (stderr, _ ("Could not convert `%s' to int32_t.\n"), begin); 279 break;
280 }
281 else
282 {
283 LOG (GNUNET_ERROR_TYPE_ERROR, _ ("Could not convert `%s' to int32_t.\n"), begin);
273 return; 284 return;
274 } 285 }
275 286
276 GNUNET_assert (mask_count <= element_count);
277
278 if (element) 287 if (element)
279 mask[mask_count / 8] = mask[mask_count / 8] | 1 << (mask_count % 8); 288 mask[mask_count / 8] = mask[mask_count / 8] | 1 << (mask_count % 8);
280
281 mask_count++; 289 mask_count++;
282 begin = ++end;
283 } 290 }
284 while (!exit_loop); 291 while (mask_count < element_count);
285 // +1 to see if we would have more data, which would indicate malformed/superficial input
286 GNUNET_assert (mask_count == element_count);
287 } 292 }
288 else if (input_peer_id) 293 else if (NULL != input_peer_id)
289 {
290 for (i = 0; i <= mask_bytes; i++) 294 for (i = 0; i <= mask_bytes; i++)
291 mask[i] = UCHAR_MAX; // all 1's 295 mask[i] = UCHAR_MAX; // all 1's
292 }
293
294 296
295 if (input_peer_id && !GNUNET_SCALARPRODUCT_request (cfg, 297 if (input_peer_id && (NULL == GNUNET_SCALARPRODUCT_request (cfg,
296 &closure->key, 298 &closure->key,
297 &closure->peer, 299 &closure->peer,
298 elements, element_count, 300 elements, element_count,
299 mask, mask_bytes, 301 mask, mask_bytes,
300 &requester_callback, 302 &requester_callback,
301 (void *) &closure)) 303 (void *) &closure)))
302 return; 304 return;
303 305
304 306 if ((NULL == input_peer_id) && (NULL == GNUNET_SCALARPRODUCT_response (cfg,
305 if (!input_peer_id && !GNUNET_SCALARPRODUCT_response (cfg, 307 &closure->key,
306 &closure->key, 308 elements, element_count,
307 elements, element_count, 309 &responder_callback,
308 &responder_callback, 310 (void *) &closure)))
309 (void *) &closure))
310 return; 311 return;
311 312
312 ret = 0; 313 ret = 0;
313} 314}
314 315
315 316
316 /** 317/**
317 * The main function to the scalarproduct client. 318 * The main function to the scalarproduct client.
318 * 319 *
319 * @param argc number of arguments from the command line 320 * @param argc number of arguments from the command line
320 * @param argv command line arguments 321 * @param argv command line arguments
321 * @return 0 ok, 1 on error 322 * @return 0 ok, 1 on error
322 */ 323 */
323 int 324int
324main (int argc, char *const *argv) 325main (int argc, char *const *argv)
325{ 326{
326 static const struct GNUNET_GETOPT_CommandLineOption options[] = { 327 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
327 {'e', "elements", "\"val1,val2,...,valn\"", 328 {'e', "elements", "\"val1,val2,...,valn\"",
328 gettext_noop ("A comma separated list of elements to compare as vector with our remote peer."), 329 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 1, &GNUNET_GETOPT_set_string, &input_elements},
@@ -336,14 +337,14 @@ main (int argc, char *const *argv)
336 {'k', "key", "TRANSACTION_ID", 337 {'k', "key", "TRANSACTION_ID",
337 gettext_noop ("Transaction ID shared with peer."), 338 gettext_noop ("Transaction ID shared with peer."),
338 1, &GNUNET_GETOPT_set_string, &input_key}, 339 1, &GNUNET_GETOPT_set_string, &input_key},
339 GNUNET_GETOPT_OPTION_END 340 GNUNET_GETOPT_OPTION_END
340 }; 341 };
341 342
342 return (GNUNET_OK == 343 return (GNUNET_OK ==
343 GNUNET_PROGRAM_run (argc, 344 GNUNET_PROGRAM_run (argc,
344 argv, 345 argv,
345 "gnunet-scalarproduct", 346 "gnunet-scalarproduct",
346 gettext_noop ("Calculate the Vectorproduct with a GNUnet peer."), 347 gettext_noop ("Calculate the Vectorproduct with a GNUnet peer."),
347 options, &run, NULL)) ? ret : 1; 348 options, &run, NULL)) ? ret : 1;
348} 349}
349 350