aboutsummaryrefslogtreecommitdiff
path: root/src/reclaim/gnunet-service-reclaim_tickets.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/reclaim/gnunet-service-reclaim_tickets.c')
-rw-r--r--src/reclaim/gnunet-service-reclaim_tickets.c571
1 files changed, 204 insertions, 367 deletions
diff --git a/src/reclaim/gnunet-service-reclaim_tickets.c b/src/reclaim/gnunet-service-reclaim_tickets.c
index a39a4be5a..54e5659a4 100644
--- a/src/reclaim/gnunet-service-reclaim_tickets.c
+++ b/src/reclaim/gnunet-service-reclaim_tickets.c
@@ -28,8 +28,7 @@
28 28
29struct ParallelLookup; 29struct ParallelLookup;
30 30
31struct RECLAIM_TICKETS_ConsumeHandle 31struct RECLAIM_TICKETS_ConsumeHandle {
32{
33 /** 32 /**
34 * Ticket 33 * Ticket
35 */ 34 */
@@ -84,14 +83,12 @@ struct RECLAIM_TICKETS_ConsumeHandle
84 * Callbacl closure 83 * Callbacl closure
85 */ 84 */
86 void *cb_cls; 85 void *cb_cls;
87
88}; 86};
89 87
90/** 88/**
91 * Handle for a parallel GNS lookup job 89 * Handle for a parallel GNS lookup job
92 */ 90 */
93struct ParallelLookup 91struct ParallelLookup {
94{
95 /* DLL */ 92 /* DLL */
96 struct ParallelLookup *next; 93 struct ParallelLookup *next;
97 94
@@ -117,8 +114,7 @@ struct ParallelLookup
117/** 114/**
118 * A reference to a ticket stored in GNS 115 * A reference to a ticket stored in GNS
119 */ 116 */
120struct TicketReference 117struct TicketReference {
121{
122 /** 118 /**
123 * DLL 119 * DLL
124 */ 120 */
@@ -144,8 +140,7 @@ struct TicketReference
144/** 140/**
145 * Ticket issue request handle 141 * Ticket issue request handle
146 */ 142 */
147struct TicketIssueHandle 143struct TicketIssueHandle {
148{
149 /** 144 /**
150 * Attributes to issue 145 * Attributes to issue
151 */ 146 */
@@ -190,14 +185,12 @@ struct TicketIssueHandle
190 * Callback cls 185 * Callback cls
191 */ 186 */
192 void *cb_cls; 187 void *cb_cls;
193
194}; 188};
195 189
196/** 190/**
197 * Ticket iterator 191 * Ticket iterator
198 */ 192 */
199struct RECLAIM_TICKETS_Iterator 193struct RECLAIM_TICKETS_Iterator {
200{
201 /** 194 /**
202 * Issuer Key 195 * Issuer Key
203 */ 196 */
@@ -243,26 +236,25 @@ static struct GNUNET_GNS_Handle *gns;
243/* Handle to the statistics service */ 236/* Handle to the statistics service */
244static struct GNUNET_STATISTICS_Handle *stats; 237static struct GNUNET_STATISTICS_Handle *stats;
245 238
246static int 239static int create_sym_key_from_ecdh (
247create_sym_key_from_ecdh (const struct GNUNET_HashCode *new_key_hash, 240 const struct GNUNET_HashCode *new_key_hash,
248 struct GNUNET_CRYPTO_SymmetricSessionKey *skey, 241 struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
249 struct GNUNET_CRYPTO_SymmetricInitializationVector *iv) 242 struct GNUNET_CRYPTO_SymmetricInitializationVector *iv)
250{ 243{
251 struct GNUNET_CRYPTO_HashAsciiEncoded new_key_hash_str; 244 struct GNUNET_CRYPTO_HashAsciiEncoded new_key_hash_str;
252 245
253 GNUNET_CRYPTO_hash_to_enc (new_key_hash, 246 GNUNET_CRYPTO_hash_to_enc (new_key_hash, &new_key_hash_str);
254 &new_key_hash_str); 247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating symmetric rsa key from %s\n",
255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating symmetric rsa key from %s\n", (char*)&new_key_hash_str); 248 (char *)&new_key_hash_str);
256 static const char ctx_key[] = "gnuid-aes-ctx-key"; 249 static const char ctx_key[] = "gnuid-aes-ctx-key";
257 GNUNET_CRYPTO_kdf (skey, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey), 250 GNUNET_CRYPTO_kdf (skey, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
258 new_key_hash, sizeof (struct GNUNET_HashCode), 251 new_key_hash, sizeof (struct GNUNET_HashCode), ctx_key,
259 ctx_key, strlen (ctx_key), 252 strlen (ctx_key), NULL, 0);
260 NULL, 0);
261 static const char ctx_iv[] = "gnuid-aes-ctx-iv"; 253 static const char ctx_iv[] = "gnuid-aes-ctx-iv";
262 GNUNET_CRYPTO_kdf (iv, sizeof (struct GNUNET_CRYPTO_SymmetricInitializationVector), 254 GNUNET_CRYPTO_kdf (
263 new_key_hash, sizeof (struct GNUNET_HashCode), 255 iv, sizeof (struct GNUNET_CRYPTO_SymmetricInitializationVector),
264 ctx_iv, strlen (ctx_iv), 256 new_key_hash, sizeof (struct GNUNET_HashCode), ctx_iv, strlen (ctx_iv),
265 NULL, 0); 257 NULL, 0);
266 return GNUNET_OK; 258 return GNUNET_OK;
267} 259}
268 260
@@ -271,21 +263,18 @@ create_sym_key_from_ecdh (const struct GNUNET_HashCode *new_key_hash,
271 * Cleanup ticket consume handle 263 * Cleanup ticket consume handle
272 * @param cth the handle to clean up 264 * @param cth the handle to clean up
273 */ 265 */
274static void 266static void cleanup_cth (struct RECLAIM_TICKETS_ConsumeHandle *cth)
275cleanup_cth (struct RECLAIM_TICKETS_ConsumeHandle *cth)
276{ 267{
277 struct ParallelLookup *lu; 268 struct ParallelLookup *lu;
278 struct ParallelLookup *tmp; 269 struct ParallelLookup *tmp;
279 if (NULL != cth->lookup_request) 270 if (NULL != cth->lookup_request)
280 GNUNET_GNS_lookup_cancel (cth->lookup_request); 271 GNUNET_GNS_lookup_cancel (cth->lookup_request);
281 for (lu = cth->parallel_lookups_head; 272 for (lu = cth->parallel_lookups_head; NULL != lu;) {
282 NULL != lu;) {
283 GNUNET_GNS_lookup_cancel (lu->lookup_request); 273 GNUNET_GNS_lookup_cancel (lu->lookup_request);
284 GNUNET_free (lu->label); 274 GNUNET_free (lu->label);
285 tmp = lu->next; 275 tmp = lu->next;
286 GNUNET_CONTAINER_DLL_remove (cth->parallel_lookups_head, 276 GNUNET_CONTAINER_DLL_remove (cth->parallel_lookups_head,
287 cth->parallel_lookups_tail, 277 cth->parallel_lookups_tail, lu);
288 lu);
289 GNUNET_free (lu); 278 GNUNET_free (lu);
290 lu = tmp; 279 lu = tmp;
291 } 280 }
@@ -297,90 +286,69 @@ cleanup_cth (struct RECLAIM_TICKETS_ConsumeHandle *cth)
297 286
298 287
299static void 288static void
300process_parallel_lookup_result (void *cls, 289process_parallel_lookup_result (void *cls, uint32_t rd_count,
301 uint32_t rd_count, 290 const struct GNUNET_GNSRECORD_Data *rd)
302 const struct GNUNET_GNSRECORD_Data *rd)
303{ 291{
304 struct ParallelLookup *parallel_lookup = cls; 292 struct ParallelLookup *parallel_lookup = cls;
305 struct RECLAIM_TICKETS_ConsumeHandle *cth = parallel_lookup->handle; 293 struct RECLAIM_TICKETS_ConsumeHandle *cth = parallel_lookup->handle;
306 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *attr_le; 294 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *attr_le;
307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Parallel lookup finished (count=%u)\n",
308 "Parallel lookup finished (count=%u)\n", rd_count); 296 rd_count);
309 297
310 GNUNET_CONTAINER_DLL_remove (cth->parallel_lookups_head, 298 GNUNET_CONTAINER_DLL_remove (cth->parallel_lookups_head,
311 cth->parallel_lookups_tail, 299 cth->parallel_lookups_tail, parallel_lookup);
312 parallel_lookup);
313 GNUNET_free (parallel_lookup->label); 300 GNUNET_free (parallel_lookup->label);
314 301
315 GNUNET_STATISTICS_update (stats, 302 GNUNET_STATISTICS_update (
316 "attribute_lookup_time_total", 303 stats, "attribute_lookup_time_total",
317 GNUNET_TIME_absolute_get_duration (parallel_lookup->lookup_start_time).rel_value_us, 304 GNUNET_TIME_absolute_get_duration (parallel_lookup->lookup_start_time)
318 GNUNET_YES); 305 .rel_value_us,
319 GNUNET_STATISTICS_update (stats, 306 GNUNET_YES);
320 "attribute_lookups_count", 307 GNUNET_STATISTICS_update (stats, "attribute_lookups_count", 1, GNUNET_YES);
321 1,
322 GNUNET_YES);
323 308
324 309
325 GNUNET_free (parallel_lookup); 310 GNUNET_free (parallel_lookup);
326 if (1 != rd_count) 311 if (1 != rd_count)
327 GNUNET_break(0);//TODO 312 GNUNET_break (0); // TODO
328 if (rd->record_type == GNUNET_GNSRECORD_TYPE_RECLAIM_ATTR) 313 if (rd->record_type == GNUNET_GNSRECORD_TYPE_RECLAIM_ATTR) {
329 {
330 attr_le = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry); 314 attr_le = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry);
331 attr_le->claim = GNUNET_RECLAIM_ATTRIBUTE_deserialize (rd->data, 315 attr_le->claim =
332 rd->data_size); 316 GNUNET_RECLAIM_ATTRIBUTE_deserialize (rd->data, rd->data_size);
333 GNUNET_CONTAINER_DLL_insert (cth->attrs->list_head, 317 GNUNET_CONTAINER_DLL_insert (cth->attrs->list_head, cth->attrs->list_tail,
334 cth->attrs->list_tail,
335 attr_le); 318 attr_le);
336 } 319 }
337 if (NULL != cth->parallel_lookups_head) 320 if (NULL != cth->parallel_lookups_head)
338 return; //Wait for more 321 return; // Wait for more
339 /* Else we are done */ 322 /* Else we are done */
340 323
341 GNUNET_SCHEDULER_cancel (cth->kill_task); 324 GNUNET_SCHEDULER_cancel (cth->kill_task);
342 cth->cb (cth->cb_cls, 325 cth->cb (cth->cb_cls, &cth->ticket.identity, cth->attrs, GNUNET_OK, NULL);
343 &cth->ticket.identity,
344 cth->attrs,
345 GNUNET_OK,
346 NULL);
347 cleanup_cth (cth); 326 cleanup_cth (cth);
348} 327}
349 328
350 329
351static void 330static void abort_parallel_lookups (void *cls)
352abort_parallel_lookups (void *cls)
353{ 331{
354 struct RECLAIM_TICKETS_ConsumeHandle *cth = cls; 332 struct RECLAIM_TICKETS_ConsumeHandle *cth = cls;
355 struct ParallelLookup *lu; 333 struct ParallelLookup *lu;
356 struct ParallelLookup *tmp; 334 struct ParallelLookup *tmp;
357 335
358 cth->kill_task = NULL; 336 cth->kill_task = NULL;
359 for (lu = cth->parallel_lookups_head; 337 for (lu = cth->parallel_lookups_head; NULL != lu;) {
360 NULL != lu;) {
361 GNUNET_GNS_lookup_cancel (lu->lookup_request); 338 GNUNET_GNS_lookup_cancel (lu->lookup_request);
362 GNUNET_free (lu->label); 339 GNUNET_free (lu->label);
363 tmp = lu->next; 340 tmp = lu->next;
364 GNUNET_CONTAINER_DLL_remove (cth->parallel_lookups_head, 341 GNUNET_CONTAINER_DLL_remove (cth->parallel_lookups_head,
365 cth->parallel_lookups_tail, 342 cth->parallel_lookups_tail, lu);
366 lu);
367 GNUNET_free (lu); 343 GNUNET_free (lu);
368 lu = tmp; 344 lu = tmp;
369 } 345 }
370 cth->cb (cth->cb_cls, 346 cth->cb (cth->cb_cls, NULL, NULL, GNUNET_SYSERR, "Aborted");
371 NULL,
372 NULL,
373 GNUNET_SYSERR,
374 "Aborted");
375
376} 347}
377 348
378 349
379 350static void lookup_authz_cb (void *cls, uint32_t rd_count,
380static void 351 const struct GNUNET_GNSRECORD_Data *rd)
381lookup_authz_cb (void *cls,
382 uint32_t rd_count,
383 const struct GNUNET_GNSRECORD_Data *rd)
384{ 352{
385 struct RECLAIM_TICKETS_ConsumeHandle *cth = cls; 353 struct RECLAIM_TICKETS_ConsumeHandle *cth = cls;
386 struct GNUNET_HashCode new_key_hash; 354 struct GNUNET_HashCode new_key_hash;
@@ -394,125 +362,90 @@ lookup_authz_cb (void *cls,
394 char *lbls; 362 char *lbls;
395 363
396 cth->lookup_request = NULL; 364 cth->lookup_request = NULL;
397 if (1 != rd_count) 365 if (1 != rd_count) {
398 { 366 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Number of keys %d != 1.", rd_count);
399 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 367 cth->cb (cth->cb_cls, NULL, NULL, GNUNET_SYSERR, "Number of keys %d != 1.");
400 "Number of keys %d != 1.",
401 rd_count);
402 cth->cb (cth->cb_cls,
403 NULL,
404 NULL,
405 GNUNET_SYSERR,
406 "Number of keys %d != 1.");
407 cleanup_cth (cth); 368 cleanup_cth (cth);
408 return; 369 return;
409 } 370 }
410 371
411 //Decrypt 372 // Decrypt
412 ecdh_key = (struct GNUNET_CRYPTO_EcdhePublicKey *)rd->data; 373 ecdh_key = (struct GNUNET_CRYPTO_EcdhePublicKey *)rd->data;
413 374
414 buf = GNUNET_malloc (rd->data_size 375 buf = GNUNET_malloc (rd->data_size -
415 - sizeof (struct GNUNET_CRYPTO_EcdhePublicKey)); 376 sizeof (struct GNUNET_CRYPTO_EcdhePublicKey));
416 377
417 //Calculate symmetric key from ecdh parameters 378 // Calculate symmetric key from ecdh parameters
418 GNUNET_assert (GNUNET_OK == 379 GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_ecdsa_ecdh (
419 GNUNET_CRYPTO_ecdsa_ecdh (&cth->identity, 380 &cth->identity, ecdh_key, &new_key_hash));
420 ecdh_key, 381 create_sym_key_from_ecdh (&new_key_hash, &enc_key, &enc_iv);
421 &new_key_hash)); 382 size = GNUNET_CRYPTO_symmetric_decrypt (
422 create_sym_key_from_ecdh (&new_key_hash, 383 rd->data + sizeof (struct GNUNET_CRYPTO_EcdhePublicKey),
423 &enc_key, 384 rd->data_size - sizeof (struct GNUNET_CRYPTO_EcdhePublicKey), &enc_key,
424 &enc_iv); 385 &enc_iv, buf);
425 size = GNUNET_CRYPTO_symmetric_decrypt (rd->data
426 + sizeof (struct GNUNET_CRYPTO_EcdhePublicKey),
427 rd->data_size
428 - sizeof (struct GNUNET_CRYPTO_EcdhePublicKey),
429 &enc_key,
430 &enc_iv,
431 buf);
432 386
433 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 387 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
434 "Decrypted bytes: %zd Expected bytes: %zd\n", 388 "Decrypted bytes: %zd Expected bytes: %zd\n", size,
435 size,
436 rd->data_size - sizeof (struct GNUNET_CRYPTO_EcdhePublicKey)); 389 rd->data_size - sizeof (struct GNUNET_CRYPTO_EcdhePublicKey));
437 GNUNET_STATISTICS_update (stats, 390 GNUNET_STATISTICS_update (
438 "reclaim_authz_lookup_time_total", 391 stats, "reclaim_authz_lookup_time_total",
439 GNUNET_TIME_absolute_get_duration (cth->lookup_start_time).rel_value_us, 392 GNUNET_TIME_absolute_get_duration (cth->lookup_start_time).rel_value_us,
440 GNUNET_YES); 393 GNUNET_YES);
441 GNUNET_STATISTICS_update (stats, 394 GNUNET_STATISTICS_update (stats, "reclaim_authz_lookups_count", 1,
442 "reclaim_authz_lookups_count",
443 1,
444 GNUNET_YES); 395 GNUNET_YES);
445 lbls = GNUNET_strdup (buf); 396 lbls = GNUNET_strdup (buf);
446 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 397 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Attributes found %s\n", lbls);
447 "Attributes found %s\n", lbls); 398
448 399 for (attr_lbl = strtok (lbls, ","); NULL != attr_lbl;
449 for (attr_lbl = strtok (lbls, ","); 400 attr_lbl = strtok (NULL, ",")) {
450 NULL != attr_lbl; 401 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Looking up %s\n", attr_lbl);
451 attr_lbl = strtok (NULL, ","))
452 {
453 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
454 "Looking up %s\n", attr_lbl);
455 parallel_lookup = GNUNET_new (struct ParallelLookup); 402 parallel_lookup = GNUNET_new (struct ParallelLookup);
456 parallel_lookup->handle = cth; 403 parallel_lookup->handle = cth;
457 parallel_lookup->label = GNUNET_strdup (attr_lbl); 404 parallel_lookup->label = GNUNET_strdup (attr_lbl);
458 parallel_lookup->lookup_start_time = GNUNET_TIME_absolute_get(); 405 parallel_lookup->lookup_start_time = GNUNET_TIME_absolute_get ();
459 parallel_lookup->lookup_request 406 parallel_lookup->lookup_request = GNUNET_GNS_lookup (
460 = GNUNET_GNS_lookup (gns, 407 gns, attr_lbl, &cth->ticket.identity,
461 attr_lbl, 408 GNUNET_GNSRECORD_TYPE_RECLAIM_ATTR, GNUNET_GNS_LO_DEFAULT,
462 &cth->ticket.identity, 409 &process_parallel_lookup_result, parallel_lookup);
463 GNUNET_GNSRECORD_TYPE_RECLAIM_ATTR,
464 GNUNET_GNS_LO_DEFAULT,
465 &process_parallel_lookup_result,
466 parallel_lookup);
467 GNUNET_CONTAINER_DLL_insert (cth->parallel_lookups_head, 410 GNUNET_CONTAINER_DLL_insert (cth->parallel_lookups_head,
468 cth->parallel_lookups_tail, 411 cth->parallel_lookups_tail, parallel_lookup);
469 parallel_lookup);
470 } 412 }
471 GNUNET_free (lbls); 413 GNUNET_free (lbls);
472 GNUNET_free (buf); 414 GNUNET_free (buf);
473 cth->kill_task = GNUNET_SCHEDULER_add_delayed ( 415 cth->kill_task = GNUNET_SCHEDULER_add_delayed (
474 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES,3), 416 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 3),
475 &abort_parallel_lookups, 417 &abort_parallel_lookups, cth);
476 cth);
477} 418}
478 419
479 420
480 421struct RECLAIM_TICKETS_ConsumeHandle *
481struct RECLAIM_TICKETS_ConsumeHandle*
482RECLAIM_TICKETS_consume (const struct GNUNET_CRYPTO_EcdsaPrivateKey *id, 422RECLAIM_TICKETS_consume (const struct GNUNET_CRYPTO_EcdsaPrivateKey *id,
483 const struct GNUNET_RECLAIM_Ticket *ticket, 423 const struct GNUNET_RECLAIM_Ticket *ticket,
484 RECLAIM_TICKETS_ConsumeCallback cb, 424 RECLAIM_TICKETS_ConsumeCallback cb, void *cb_cls)
485 void* cb_cls)
486{ 425{
487 struct RECLAIM_TICKETS_ConsumeHandle *cth; 426 struct RECLAIM_TICKETS_ConsumeHandle *cth;
488 char *label; 427 char *label;
489 cth = GNUNET_new (struct RECLAIM_TICKETS_ConsumeHandle); 428 cth = GNUNET_new (struct RECLAIM_TICKETS_ConsumeHandle);
490 429
491 cth->identity = *id; 430 cth->identity = *id;
492 GNUNET_CRYPTO_ecdsa_key_get_public (&cth->identity, 431 GNUNET_CRYPTO_ecdsa_key_get_public (&cth->identity, &cth->identity_pub);
493 &cth->identity_pub);
494 cth->attrs = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList); 432 cth->attrs = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList);
495 cth->ticket = *ticket; 433 cth->ticket = *ticket;
496 cth->cb = cb; 434 cth->cb = cb;
497 cth->cb_cls = cb_cls; 435 cth->cb_cls = cb_cls;
498 label = GNUNET_STRINGS_data_to_string_alloc (&cth->ticket.rnd, 436 label =
499 sizeof (uint64_t)); 437 GNUNET_STRINGS_data_to_string_alloc (&cth->ticket.rnd, sizeof (uint64_t));
500 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 438 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Looking for AuthZ info under %s\n",
501 "Looking for AuthZ info under %s\n", label); 439 label);
502 cth->lookup_start_time = GNUNET_TIME_absolute_get (); 440 cth->lookup_start_time = GNUNET_TIME_absolute_get ();
503 cth->lookup_request = GNUNET_GNS_lookup (gns, 441 cth->lookup_request = GNUNET_GNS_lookup (
504 label, 442 gns, label, &cth->ticket.identity, GNUNET_GNSRECORD_TYPE_RECLAIM_AUTHZ,
505 &cth->ticket.identity, 443 GNUNET_GNS_LO_DEFAULT, &lookup_authz_cb, cth);
506 GNUNET_GNSRECORD_TYPE_RECLAIM_AUTHZ,
507 GNUNET_GNS_LO_DEFAULT,
508 &lookup_authz_cb,
509 cth);
510 GNUNET_free (label); 444 GNUNET_free (label);
511 return cth; 445 return cth;
512} 446}
513 447
514void 448void RECLAIM_TICKETS_consume_cancel (struct RECLAIM_TICKETS_ConsumeHandle *cth)
515RECLAIM_TICKETS_consume_cancel (struct RECLAIM_TICKETS_ConsumeHandle *cth)
516{ 449{
517 cleanup_cth (cth); 450 cleanup_cth (cth);
518 return; 451 return;
@@ -527,8 +460,7 @@ RECLAIM_TICKETS_consume_cancel (struct RECLAIM_TICKETS_ConsumeHandle *cth)
527 * Cleanup ticket consume handle 460 * Cleanup ticket consume handle
528 * @param handle the handle to clean up 461 * @param handle the handle to clean up
529 */ 462 */
530static void 463static void cleanup_issue_handle (struct TicketIssueHandle *handle)
531cleanup_issue_handle (struct TicketIssueHandle *handle)
532{ 464{
533 struct TicketReference *tr; 465 struct TicketReference *tr;
534 struct TicketReference *tr_tmp; 466 struct TicketReference *tr_tmp;
@@ -536,8 +468,7 @@ cleanup_issue_handle (struct TicketIssueHandle *handle)
536 GNUNET_RECLAIM_ATTRIBUTE_list_destroy (handle->attrs); 468 GNUNET_RECLAIM_ATTRIBUTE_list_destroy (handle->attrs);
537 if (NULL != handle->ns_qe) 469 if (NULL != handle->ns_qe)
538 GNUNET_NAMESTORE_cancel (handle->ns_qe); 470 GNUNET_NAMESTORE_cancel (handle->ns_qe);
539 for (tr = handle->ticket_refs_head; NULL != tr;) 471 for (tr = handle->ticket_refs_head; NULL != tr;) {
540 {
541 if (NULL != tr->attrs) 472 if (NULL != tr->attrs)
542 GNUNET_RECLAIM_ATTRIBUTE_list_destroy (tr->attrs); 473 GNUNET_RECLAIM_ATTRIBUTE_list_destroy (tr->attrs);
543 tr_tmp = tr; 474 tr_tmp = tr;
@@ -548,156 +479,119 @@ cleanup_issue_handle (struct TicketIssueHandle *handle)
548} 479}
549 480
550 481
551 482static void store_ticket_refs_cont (void *cls, int32_t success,
552static void 483 const char *emsg)
553store_ticket_refs_cont (void *cls,
554 int32_t success,
555 const char *emsg)
556{ 484{
557 struct TicketIssueHandle *handle = cls; 485 struct TicketIssueHandle *handle = cls;
558 handle->ns_qe = NULL; 486 handle->ns_qe = NULL;
559 if (GNUNET_OK != success) 487 if (GNUNET_OK != success) {
560 { 488 handle->cb (handle->cb_cls, NULL, GNUNET_SYSERR,
561 handle->cb (handle->cb_cls,
562 NULL,
563 GNUNET_SYSERR,
564 "Error storing updated ticket refs in GNS"); 489 "Error storing updated ticket refs in GNS");
565 cleanup_issue_handle (handle); 490 cleanup_issue_handle (handle);
566 return; 491 return;
567 } 492 }
568 handle->cb (handle->cb_cls, 493 handle->cb (handle->cb_cls, &handle->ticket, GNUNET_OK, NULL);
569 &handle->ticket,
570 GNUNET_OK,
571 NULL);
572 cleanup_issue_handle (handle); 494 cleanup_issue_handle (handle);
573} 495}
574 496
575 497
576 498static void update_ticket_refs (void *cls)
577static void
578update_ticket_refs (void* cls)
579{ 499{
580 struct TicketIssueHandle *handle = cls; 500 struct TicketIssueHandle *handle = cls;
581 struct GNUNET_GNSRECORD_Data refs_rd[handle->ticket_ref_num]; 501 struct GNUNET_GNSRECORD_Data refs_rd[handle->ticket_ref_num];
582 struct TicketReference *tr; 502 struct TicketReference *tr;
583 char* buf; 503 char *buf;
584 size_t buf_size; 504 size_t buf_size;
585 505
586 tr = handle->ticket_refs_head; 506 tr = handle->ticket_refs_head;
587 for (int i = 0; i < handle->ticket_ref_num; i++) 507 for (int i = 0; i < handle->ticket_ref_num; i++) {
588 {
589 buf_size = GNUNET_RECLAIM_ATTRIBUTE_list_serialize_get_size (tr->attrs); 508 buf_size = GNUNET_RECLAIM_ATTRIBUTE_list_serialize_get_size (tr->attrs);
590 buf_size += sizeof (struct GNUNET_RECLAIM_Ticket); 509 buf_size += sizeof (struct GNUNET_RECLAIM_Ticket);
591 buf = GNUNET_malloc (buf_size); 510 buf = GNUNET_malloc (buf_size);
592 memcpy (buf, &tr->ticket, sizeof (struct GNUNET_RECLAIM_Ticket)); 511 memcpy (buf, &tr->ticket, sizeof (struct GNUNET_RECLAIM_Ticket));
593 GNUNET_RECLAIM_ATTRIBUTE_list_serialize (tr->attrs, 512 GNUNET_RECLAIM_ATTRIBUTE_list_serialize (
594 buf + sizeof (struct GNUNET_RECLAIM_Ticket)); 513 tr->attrs, buf + sizeof (struct GNUNET_RECLAIM_Ticket));
595 refs_rd[i].data = buf; 514 refs_rd[i].data = buf;
596 refs_rd[i].data_size = buf_size; 515 refs_rd[i].data_size = buf_size;
597 refs_rd[i].expiration_time = GNUNET_TIME_UNIT_DAYS.rel_value_us; 516 refs_rd[i].expiration_time = GNUNET_TIME_UNIT_DAYS.rel_value_us;
598 refs_rd[i].record_type = GNUNET_GNSRECORD_TYPE_RECLAIM_TICKETREF; 517 refs_rd[i].record_type = GNUNET_GNSRECORD_TYPE_RECLAIM_TICKETREF;
599 refs_rd[i].flags = GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION | 518 refs_rd[i].flags =
600 GNUNET_GNSRECORD_RF_PRIVATE; 519 GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION | GNUNET_GNSRECORD_RF_PRIVATE;
601 tr = tr->next; 520 tr = tr->next;
602 } 521 }
603 522
604 handle->ns_qe = GNUNET_NAMESTORE_records_store (nsh, 523 handle->ns_qe = GNUNET_NAMESTORE_records_store (
605 &handle->identity, 524 nsh, &handle->identity, GNUNET_GNS_EMPTY_LABEL_AT, handle->ticket_ref_num,
606 GNUNET_GNS_EMPTY_LABEL_AT, 525 refs_rd, &store_ticket_refs_cont, handle);
607 handle->ticket_ref_num,
608 refs_rd,
609 &store_ticket_refs_cont,
610 handle);
611 for (int i = 0; i < handle->ticket_ref_num; i++) 526 for (int i = 0; i < handle->ticket_ref_num; i++)
612 GNUNET_free ((char*)refs_rd[i].data); 527 GNUNET_free ((char *)refs_rd[i].data);
613} 528}
614 529
615 530
616 531static void ticket_lookup_cb (void *cls,
617static void 532 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
618ticket_lookup_cb (void *cls, 533 const char *label, unsigned int rd_count,
619 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, 534 const struct GNUNET_GNSRECORD_Data *rd)
620 const char *label,
621 unsigned int rd_count,
622 const struct GNUNET_GNSRECORD_Data *rd)
623{ 535{
624 struct TicketIssueHandle *handle = cls; 536 struct TicketIssueHandle *handle = cls;
625 struct TicketReference *tr; 537 struct TicketReference *tr;
626 const char* attr_data; 538 const char *attr_data;
627 size_t attr_data_len; 539 size_t attr_data_len;
628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
629 "Received tickets from local namestore.\n"); 541 "Received tickets from local namestore.\n");
630 handle->ns_qe = NULL; 542 handle->ns_qe = NULL;
631 for (int i = 0; i < rd_count; i++) 543 for (int i = 0; i < rd_count; i++) {
632 {
633 if (GNUNET_GNSRECORD_TYPE_RECLAIM_TICKETREF != rd[i].record_type) 544 if (GNUNET_GNSRECORD_TYPE_RECLAIM_TICKETREF != rd[i].record_type)
634 continue; 545 continue;
635 tr = GNUNET_new (struct TicketReference); 546 tr = GNUNET_new (struct TicketReference);
636 memcpy (&tr->ticket, rd[i].data, 547 memcpy (&tr->ticket, rd[i].data, sizeof (struct GNUNET_RECLAIM_Ticket));
637 sizeof (struct GNUNET_RECLAIM_Ticket)); 548 if (0 != memcmp (&tr->ticket.identity, &handle->ticket.identity,
638 if (0 != memcmp (&tr->ticket.identity, 549 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey))) {
639 &handle->ticket.identity, 550 // Not our ticket
640 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)))
641 {
642 //Not our ticket
643 GNUNET_free (tr); 551 GNUNET_free (tr);
644 continue; 552 continue;
645 } 553 }
646 attr_data = rd[i].data + sizeof (struct GNUNET_RECLAIM_Ticket); 554 attr_data = rd[i].data + sizeof (struct GNUNET_RECLAIM_Ticket);
647 attr_data_len = rd[i].data_size - sizeof (struct GNUNET_RECLAIM_Ticket); 555 attr_data_len = rd[i].data_size - sizeof (struct GNUNET_RECLAIM_Ticket);
648 tr->attrs = GNUNET_RECLAIM_ATTRIBUTE_list_deserialize (attr_data, 556 tr->attrs =
649 attr_data_len); 557 GNUNET_RECLAIM_ATTRIBUTE_list_deserialize (attr_data, attr_data_len);
650 GNUNET_CONTAINER_DLL_insert (handle->ticket_refs_head, 558 GNUNET_CONTAINER_DLL_insert (handle->ticket_refs_head,
651 handle->ticket_refs_tail, 559 handle->ticket_refs_tail, tr);
652 tr);
653 handle->ticket_ref_num++; 560 handle->ticket_ref_num++;
654 } 561 }
655 tr = GNUNET_new (struct TicketReference); 562 tr = GNUNET_new (struct TicketReference);
656 tr->ticket = handle->ticket; 563 tr->ticket = handle->ticket;
657 tr->attrs = GNUNET_RECLAIM_ATTRIBUTE_list_dup (handle->attrs); 564 tr->attrs = GNUNET_RECLAIM_ATTRIBUTE_list_dup (handle->attrs);
658 GNUNET_CONTAINER_DLL_insert (handle->ticket_refs_head, 565 GNUNET_CONTAINER_DLL_insert (handle->ticket_refs_head,
659 handle->ticket_refs_tail, 566 handle->ticket_refs_tail, tr);
660 tr);
661 handle->ticket_ref_num++; 567 handle->ticket_ref_num++;
662 GNUNET_SCHEDULER_add_now (&update_ticket_refs, handle); 568 GNUNET_SCHEDULER_add_now (&update_ticket_refs, handle);
663} 569}
664 570
665static void 571static void ticket_lookup_error_cb (void *cls)
666ticket_lookup_error_cb (void *cls)
667{ 572{
668 struct TicketIssueHandle *handle = cls; 573 struct TicketIssueHandle *handle = cls;
669 handle->ns_qe = NULL; 574 handle->ns_qe = NULL;
670 handle->cb (handle->cb_cls, 575 handle->cb (handle->cb_cls, &handle->ticket, GNUNET_SYSERR,
671 &handle->ticket,
672 GNUNET_SYSERR,
673 "Error checking for ticketsin GNS\n"); 576 "Error checking for ticketsin GNS\n");
674 cleanup_issue_handle (handle); 577 cleanup_issue_handle (handle);
675} 578}
676 579
677static void 580static void store_ticket_issue_cont (void *cls, int32_t success,
678store_ticket_issue_cont (void *cls, 581 const char *emsg)
679 int32_t success,
680 const char *emsg)
681{ 582{
682 struct TicketIssueHandle *handle = cls; 583 struct TicketIssueHandle *handle = cls;
683 584
684 handle->ns_qe = NULL; 585 handle->ns_qe = NULL;
685 if (GNUNET_SYSERR == success) 586 if (GNUNET_SYSERR == success) {
686 { 587 handle->cb (handle->cb_cls, &handle->ticket, GNUNET_SYSERR,
687 handle->cb (handle->cb_cls,
688 &handle->ticket,
689 GNUNET_SYSERR,
690 "Error storing AuthZ ticket in GNS"); 588 "Error storing AuthZ ticket in GNS");
691 return; 589 return;
692 } 590 }
693 /* First, local references to tickets */ 591 /* First, local references to tickets */
694 handle->ns_qe = GNUNET_NAMESTORE_records_lookup (nsh, 592 handle->ns_qe = GNUNET_NAMESTORE_records_lookup (
695 &handle->identity, 593 nsh, &handle->identity, GNUNET_GNS_EMPTY_LABEL_AT,
696 GNUNET_GNS_EMPTY_LABEL_AT, 594 &ticket_lookup_error_cb, handle, &ticket_lookup_cb, handle);
697 &ticket_lookup_error_cb,
698 handle,
699 &ticket_lookup_cb,
700 handle);
701} 595}
702 596
703 597
@@ -717,63 +611,53 @@ serialize_authz_record (const struct GNUNET_RECLAIM_Ticket *ticket,
717 char *buf; 611 char *buf;
718 char *write_ptr; 612 char *write_ptr;
719 char attrs_str_len; 613 char attrs_str_len;
720 char* label; 614 char *label;
721 615
722 GNUNET_assert (NULL != attrs->list_head); 616 GNUNET_assert (NULL != attrs->list_head);
723 attrs_str_len = 0; 617 attrs_str_len = 0;
724 for (le = attrs->list_head; NULL != le; le = le->next) { 618 for (le = attrs->list_head; NULL != le; le = le->next) {
725 attrs_str_len += 15 + 1; //TODO propery calculate 619 attrs_str_len += 15 + 1; // TODO propery calculate
726 } 620 }
727 buf = GNUNET_malloc (attrs_str_len); 621 buf = GNUNET_malloc (attrs_str_len);
728 write_ptr = buf; 622 write_ptr = buf;
729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Writing attributes\n");
730 "Writing attributes\n");
731 for (le = attrs->list_head; NULL != le; le = le->next) { 624 for (le = attrs->list_head; NULL != le; le = le->next) {
732 label = GNUNET_STRINGS_data_to_string_alloc (&le->claim->id, 625 label =
733 sizeof (uint64_t)); 626 GNUNET_STRINGS_data_to_string_alloc (&le->claim->id, sizeof (uint64_t));
734 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 627 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Adding attribute to record: %s\n",
735 "Adding attribute to record: %s\n", label); 628 label);
736 629
737 GNUNET_memcpy (write_ptr, 630 GNUNET_memcpy (write_ptr, label, strlen (label));
738 label,
739 strlen (label));
740 write_ptr[strlen (label)] = ','; 631 write_ptr[strlen (label)] = ',';
741 write_ptr += strlen (label) + 1; 632 write_ptr += strlen (label) + 1;
742 GNUNET_free (label); 633 GNUNET_free (label);
743 } 634 }
744 write_ptr--; 635 write_ptr--;
745 write_ptr[0] = '\0'; //replace last , with a 0-terminator 636 write_ptr[0] = '\0'; // replace last , with a 0-terminator
746 // ECDH keypair E = eG 637 // ECDH keypair E = eG
747 *ecdh_privkey = GNUNET_CRYPTO_ecdhe_key_create(); 638 *ecdh_privkey = GNUNET_CRYPTO_ecdhe_key_create ();
748 GNUNET_CRYPTO_ecdhe_key_get_public (*ecdh_privkey, 639 GNUNET_CRYPTO_ecdhe_key_get_public (*ecdh_privkey, &ecdh_pubkey);
749 &ecdh_pubkey);
750 enc_keyinfo = GNUNET_malloc (attrs_str_len); 640 enc_keyinfo = GNUNET_malloc (attrs_str_len);
751 // Derived key K = H(eB) 641 // Derived key K = H(eB)
752 GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_ecdh_ecdsa (*ecdh_privkey, 642 GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_ecdh_ecdsa (*ecdh_privkey,
753 &ticket->audience, 643 &ticket->audience,
754 &new_key_hash)); 644 &new_key_hash));
755 create_sym_key_from_ecdh (&new_key_hash, &skey, &iv); 645 create_sym_key_from_ecdh (&new_key_hash, &skey, &iv);
756 enc_size = GNUNET_CRYPTO_symmetric_encrypt (buf, 646 enc_size = GNUNET_CRYPTO_symmetric_encrypt (buf, attrs_str_len, &skey, &iv,
757 attrs_str_len,
758 &skey, &iv,
759 enc_keyinfo); 647 enc_keyinfo);
760 *result = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_EcdhePublicKey)+ 648 *result =
761 enc_size); 649 GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_EcdhePublicKey) + enc_size);
762 GNUNET_memcpy (*result, 650 GNUNET_memcpy (*result, &ecdh_pubkey,
763 &ecdh_pubkey,
764 sizeof (struct GNUNET_CRYPTO_EcdhePublicKey)); 651 sizeof (struct GNUNET_CRYPTO_EcdhePublicKey));
765 GNUNET_memcpy (*result + sizeof (struct GNUNET_CRYPTO_EcdhePublicKey), 652 GNUNET_memcpy (*result + sizeof (struct GNUNET_CRYPTO_EcdhePublicKey),
766 enc_keyinfo, 653 enc_keyinfo, enc_size);
767 enc_size);
768 GNUNET_free (enc_keyinfo); 654 GNUNET_free (enc_keyinfo);
769 GNUNET_free (buf); 655 GNUNET_free (buf);
770 return sizeof (struct GNUNET_CRYPTO_EcdhePublicKey)+enc_size; 656 return sizeof (struct GNUNET_CRYPTO_EcdhePublicKey) + enc_size;
771} 657}
772 658
773 659
774 660static void issue_ticket (struct TicketIssueHandle *ih)
775static void
776issue_ticket (struct TicketIssueHandle *ih)
777{ 661{
778 struct GNUNET_CRYPTO_EcdhePrivateKey *ecdhe_privkey; 662 struct GNUNET_CRYPTO_EcdhePrivateKey *ecdhe_privkey;
779 struct GNUNET_GNSRECORD_Data code_record[1]; 663 struct GNUNET_GNSRECORD_Data code_record[1];
@@ -781,41 +665,31 @@ issue_ticket (struct TicketIssueHandle *ih)
781 size_t authz_record_len; 665 size_t authz_record_len;
782 char *label; 666 char *label;
783 667
784 //TODO rename function 668 // TODO rename function
785 authz_record_len = serialize_authz_record (&ih->ticket, 669 authz_record_len = serialize_authz_record (
786 ih->attrs, 670 &ih->ticket, ih->attrs, &ecdhe_privkey, &authz_record_data);
787 &ecdhe_privkey,
788 &authz_record_data);
789 code_record[0].data = authz_record_data; 671 code_record[0].data = authz_record_data;
790 code_record[0].data_size = authz_record_len; 672 code_record[0].data_size = authz_record_len;
791 code_record[0].expiration_time = GNUNET_TIME_UNIT_DAYS.rel_value_us; 673 code_record[0].expiration_time = GNUNET_TIME_UNIT_DAYS.rel_value_us;
792 code_record[0].record_type = GNUNET_GNSRECORD_TYPE_RECLAIM_AUTHZ; 674 code_record[0].record_type = GNUNET_GNSRECORD_TYPE_RECLAIM_AUTHZ;
793 code_record[0].flags = GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION; 675 code_record[0].flags = GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
794 676
795 label = GNUNET_STRINGS_data_to_string_alloc (&ih->ticket.rnd, 677 label =
796 sizeof (uint64_t)); 678 GNUNET_STRINGS_data_to_string_alloc (&ih->ticket.rnd, sizeof (uint64_t));
797 //Publish record 679 // Publish record
798 ih->ns_qe = GNUNET_NAMESTORE_records_store (nsh, 680 ih->ns_qe = GNUNET_NAMESTORE_records_store (
799 &ih->identity, 681 nsh, &ih->identity, label, 1, code_record, &store_ticket_issue_cont, ih);
800 label,
801 1,
802 code_record,
803 &store_ticket_issue_cont,
804 ih);
805 GNUNET_free (ecdhe_privkey); 682 GNUNET_free (ecdhe_privkey);
806 GNUNET_free (label); 683 GNUNET_free (label);
807 GNUNET_free (authz_record_data); 684 GNUNET_free (authz_record_data);
808} 685}
809 686
810 687
811 688void RECLAIM_TICKETS_issue (
812 689 const struct GNUNET_CRYPTO_EcdsaPrivateKey *identity,
813void 690 const struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs,
814RECLAIM_TICKETS_issue (const struct GNUNET_CRYPTO_EcdsaPrivateKey *identity, 691 const struct GNUNET_CRYPTO_EcdsaPublicKey *audience,
815 const struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs, 692 RECLAIM_TICKETS_TicketResult cb, void *cb_cls)
816 const struct GNUNET_CRYPTO_EcdsaPublicKey *audience,
817 RECLAIM_TICKETS_TicketResult cb,
818 void* cb_cls)
819{ 693{
820 struct TicketIssueHandle *tih; 694 struct TicketIssueHandle *tih;
821 tih = GNUNET_new (struct TicketIssueHandle); 695 tih = GNUNET_new (struct TicketIssueHandle);
@@ -823,11 +697,9 @@ RECLAIM_TICKETS_issue (const struct GNUNET_CRYPTO_EcdsaPrivateKey *identity,
823 tih->cb_cls = cb_cls; 697 tih->cb_cls = cb_cls;
824 tih->attrs = GNUNET_RECLAIM_ATTRIBUTE_list_dup (attrs); 698 tih->attrs = GNUNET_RECLAIM_ATTRIBUTE_list_dup (attrs);
825 tih->identity = *identity; 699 tih->identity = *identity;
826 GNUNET_CRYPTO_ecdsa_key_get_public (identity, 700 GNUNET_CRYPTO_ecdsa_key_get_public (identity, &tih->ticket.identity);
827 &tih->ticket.identity);
828 tih->ticket.rnd = 701 tih->ticket.rnd =
829 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG, 702 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG, UINT64_MAX);
830 UINT64_MAX);
831 tih->ticket.audience = *audience; 703 tih->ticket.audience = *audience;
832 issue_ticket (tih); 704 issue_ticket (tih);
833} 705}
@@ -836,15 +708,13 @@ RECLAIM_TICKETS_issue (const struct GNUNET_CRYPTO_EcdsaPrivateKey *identity,
836 * Ticket iteration 708 * Ticket iteration
837 ************************************/ 709 ************************************/
838 710
839static void 711static void cleanup_iter (struct RECLAIM_TICKETS_Iterator *iter)
840cleanup_iter (struct RECLAIM_TICKETS_Iterator *iter)
841{ 712{
842 struct TicketReference *tr; 713 struct TicketReference *tr;
843 struct TicketReference *tr_tmp; 714 struct TicketReference *tr_tmp;
844 if (NULL != iter->ns_qe) 715 if (NULL != iter->ns_qe)
845 GNUNET_NAMESTORE_cancel (iter->ns_qe); 716 GNUNET_NAMESTORE_cancel (iter->ns_qe);
846 for (tr = iter->tickets_head; NULL != tr;) 717 for (tr = iter->tickets_head; NULL != tr;) {
847 {
848 if (NULL != tr->attrs) 718 if (NULL != tr->attrs)
849 GNUNET_RECLAIM_ATTRIBUTE_list_destroy (tr->attrs); 719 GNUNET_RECLAIM_ATTRIBUTE_list_destroy (tr->attrs);
850 tr_tmp = tr; 720 tr_tmp = tr;
@@ -854,8 +724,7 @@ cleanup_iter (struct RECLAIM_TICKETS_Iterator *iter)
854 GNUNET_free (iter); 724 GNUNET_free (iter);
855} 725}
856 726
857static void 727static void do_cleanup_iter (void *cls)
858do_cleanup_iter (void* cls)
859{ 728{
860 struct RECLAIM_TICKETS_Iterator *iter = cls; 729 struct RECLAIM_TICKETS_Iterator *iter = cls;
861 cleanup_iter (iter); 730 cleanup_iter (iter);
@@ -866,140 +735,109 @@ do_cleanup_iter (void* cls)
866 * 735 *
867 * @param ti ticket iterator to process 736 * @param ti ticket iterator to process
868 */ 737 */
869static void 738static void run_ticket_iteration_round (struct RECLAIM_TICKETS_Iterator *iter)
870run_ticket_iteration_round (struct RECLAIM_TICKETS_Iterator *iter)
871{ 739{
872 struct TicketReference *tr; 740 struct TicketReference *tr;
873 if (NULL == iter->tickets_head) 741 if (NULL == iter->tickets_head) {
874 { 742 // No more tickets
875 //No more tickets 743 iter->cb (iter->cb_cls, NULL);
876 iter->cb (iter->cb_cls,
877 NULL);
878 GNUNET_SCHEDULER_add_now (&do_cleanup_iter, iter); 744 GNUNET_SCHEDULER_add_now (&do_cleanup_iter, iter);
879 return; 745 return;
880 } 746 }
881 tr = iter->tickets_head; 747 tr = iter->tickets_head;
882 GNUNET_CONTAINER_DLL_remove (iter->tickets_head, 748 GNUNET_CONTAINER_DLL_remove (iter->tickets_head, iter->tickets_tail, tr);
883 iter->tickets_tail, 749 iter->cb (iter->cb_cls, &tr->ticket);
884 tr);
885 iter->cb (iter->cb_cls,
886 &tr->ticket);
887 if (NULL != tr->attrs) 750 if (NULL != tr->attrs)
888 GNUNET_RECLAIM_ATTRIBUTE_list_destroy (tr->attrs); 751 GNUNET_RECLAIM_ATTRIBUTE_list_destroy (tr->attrs);
889 GNUNET_free (tr); 752 GNUNET_free (tr);
890} 753}
891 754
892static void 755static void
893collect_tickets_cb (void *cls, 756collect_tickets_cb (void *cls, const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
894 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, 757 const char *label, unsigned int rd_count,
895 const char *label,
896 unsigned int rd_count,
897 const struct GNUNET_GNSRECORD_Data *rd) 758 const struct GNUNET_GNSRECORD_Data *rd)
898{ 759{
899 struct RECLAIM_TICKETS_Iterator *iter = cls; 760 struct RECLAIM_TICKETS_Iterator *iter = cls;
900 struct TicketReference *tr; 761 struct TicketReference *tr;
901 size_t attr_data_len; 762 size_t attr_data_len;
902 const char* attr_data; 763 const char *attr_data;
903 iter->ns_qe = NULL; 764 iter->ns_qe = NULL;
904 765
905 for (int i = 0; i < rd_count; i++) 766 for (int i = 0; i < rd_count; i++) {
906 {
907 if (GNUNET_GNSRECORD_TYPE_RECLAIM_TICKETREF != rd[i].record_type) 767 if (GNUNET_GNSRECORD_TYPE_RECLAIM_TICKETREF != rd[i].record_type)
908 continue; 768 continue;
909 tr = GNUNET_new (struct TicketReference); 769 tr = GNUNET_new (struct TicketReference);
910 memcpy (&tr->ticket, rd[i].data, 770 memcpy (&tr->ticket, rd[i].data, sizeof (struct GNUNET_RECLAIM_Ticket));
911 sizeof (struct GNUNET_RECLAIM_Ticket)); 771 if (0 != memcmp (&tr->ticket.identity, &iter->identity_pub,
912 if (0 != memcmp (&tr->ticket.identity, 772 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey))) {
913 &iter->identity_pub, 773 // Not our ticket
914 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)))
915 {
916 //Not our ticket
917 GNUNET_free (tr); 774 GNUNET_free (tr);
918 continue; 775 continue;
919 } 776 }
920 attr_data = rd[i].data + sizeof (struct GNUNET_RECLAIM_Ticket); 777 attr_data = rd[i].data + sizeof (struct GNUNET_RECLAIM_Ticket);
921 attr_data_len = rd[i].data_size - sizeof (struct GNUNET_RECLAIM_Ticket); 778 attr_data_len = rd[i].data_size - sizeof (struct GNUNET_RECLAIM_Ticket);
922 tr->attrs = GNUNET_RECLAIM_ATTRIBUTE_list_deserialize (attr_data, 779 tr->attrs =
923 attr_data_len); 780 GNUNET_RECLAIM_ATTRIBUTE_list_deserialize (attr_data, attr_data_len);
924 GNUNET_CONTAINER_DLL_insert (iter->tickets_head, 781 GNUNET_CONTAINER_DLL_insert (iter->tickets_head, iter->tickets_tail, tr);
925 iter->tickets_tail,
926 tr);
927 } 782 }
928 run_ticket_iteration_round (iter); 783 run_ticket_iteration_round (iter);
929} 784}
930 785
931static void 786static void collect_tickets_error_cb (void *cls)
932collect_tickets_error_cb (void *cls)
933{ 787{
934 struct RECLAIM_TICKETS_Iterator *iter = cls; 788 struct RECLAIM_TICKETS_Iterator *iter = cls;
935 iter->ns_qe = NULL; 789 iter->ns_qe = NULL;
936 iter->cb (iter->cb_cls, 790 iter->cb (iter->cb_cls, NULL);
937 NULL);
938 cleanup_iter (iter); 791 cleanup_iter (iter);
939} 792}
940 793
941void 794void RECLAIM_TICKETS_iteration_next (struct RECLAIM_TICKETS_Iterator *iter)
942RECLAIM_TICKETS_iteration_next (struct RECLAIM_TICKETS_Iterator *iter)
943{ 795{
944 run_ticket_iteration_round (iter); 796 run_ticket_iteration_round (iter);
945} 797}
946 798
947void 799void RECLAIM_TICKETS_iteration_stop (struct RECLAIM_TICKETS_Iterator *iter)
948RECLAIM_TICKETS_iteration_stop (struct RECLAIM_TICKETS_Iterator *iter)
949{ 800{
950 cleanup_iter (iter); 801 cleanup_iter (iter);
951} 802}
952 803
953struct RECLAIM_TICKETS_Iterator* 804struct RECLAIM_TICKETS_Iterator *RECLAIM_TICKETS_iteration_start (
954RECLAIM_TICKETS_iteration_start (const struct GNUNET_CRYPTO_EcdsaPrivateKey *identity, 805 const struct GNUNET_CRYPTO_EcdsaPrivateKey *identity,
955 RECLAIM_TICKETS_TicketIter cb, 806 RECLAIM_TICKETS_TicketIter cb, void *cb_cls)
956 void* cb_cls)
957{ 807{
958 struct RECLAIM_TICKETS_Iterator *iter; 808 struct RECLAIM_TICKETS_Iterator *iter;
959 809
960 iter = GNUNET_new (struct RECLAIM_TICKETS_Iterator); 810 iter = GNUNET_new (struct RECLAIM_TICKETS_Iterator);
961 iter->identity = *identity; 811 iter->identity = *identity;
962 GNUNET_CRYPTO_ecdsa_key_get_public (identity, 812 GNUNET_CRYPTO_ecdsa_key_get_public (identity, &iter->identity_pub);
963 &iter->identity_pub);
964 iter->cb = cb; 813 iter->cb = cb;
965 iter->cb_cls = cb_cls; 814 iter->cb_cls = cb_cls;
966 iter->ns_qe = GNUNET_NAMESTORE_records_lookup (nsh, 815 iter->ns_qe = GNUNET_NAMESTORE_records_lookup (
967 identity, 816 nsh, identity, GNUNET_GNS_EMPTY_LABEL_AT, &collect_tickets_error_cb, iter,
968 GNUNET_GNS_EMPTY_LABEL_AT, 817 &collect_tickets_cb, iter);
969 &collect_tickets_error_cb,
970 iter,
971 &collect_tickets_cb,
972 iter);
973 return iter; 818 return iter;
974} 819}
975 820
976 821
977 822int RECLAIM_TICKETS_init (const struct GNUNET_CONFIGURATION_Handle *c)
978
979int
980RECLAIM_TICKETS_init (const struct GNUNET_CONFIGURATION_Handle *c)
981{ 823{
982 //Connect to identity and namestore services 824 // Connect to identity and namestore services
983 nsh = GNUNET_NAMESTORE_connect (c); 825 nsh = GNUNET_NAMESTORE_connect (c);
984 if (NULL == nsh) 826 if (NULL == nsh) {
985 {
986 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, 827 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
987 "error connecting to namestore"); 828 "error connecting to namestore");
988 return GNUNET_SYSERR; 829 return GNUNET_SYSERR;
989 } 830 }
990 gns = GNUNET_GNS_connect (c); 831 gns = GNUNET_GNS_connect (c);
991 if (NULL == gns) 832 if (NULL == gns) {
992 { 833 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "error connecting to gns");
993 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
994 "error connecting to gns");
995 return GNUNET_SYSERR; 834 return GNUNET_SYSERR;
996 } 835 }
997 stats = GNUNET_STATISTICS_create ("reclaim", c); 836 stats = GNUNET_STATISTICS_create ("reclaim", c);
998 return GNUNET_OK; 837 return GNUNET_OK;
999} 838}
1000 839
1001void 840void RECLAIM_TICKETS_deinit (void)
1002RECLAIM_TICKETS_deinit (void)
1003{ 841{
1004 if (NULL != nsh) 842 if (NULL != nsh)
1005 GNUNET_NAMESTORE_disconnect (nsh); 843 GNUNET_NAMESTORE_disconnect (nsh);
@@ -1007,8 +845,7 @@ RECLAIM_TICKETS_deinit (void)
1007 if (NULL != gns) 845 if (NULL != gns)
1008 GNUNET_GNS_disconnect (gns); 846 GNUNET_GNS_disconnect (gns);
1009 gns = NULL; 847 gns = NULL;
1010 if (NULL != stats) 848 if (NULL != stats) {
1011 {
1012 GNUNET_STATISTICS_destroy (stats, GNUNET_NO); 849 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1013 stats = NULL; 850 stats = NULL;
1014 } 851 }