aboutsummaryrefslogtreecommitdiff
path: root/src/escrow/escrow_api.c
diff options
context:
space:
mode:
authorjospaeth <spaethj@in.tum.de>2020-09-01 19:20:07 +0200
committerjospaeth <spaethj@in.tum.de>2020-09-01 19:20:07 +0200
commitdb199dd2280071adafc14ff66cfa1c21399f06c2 (patch)
tree4304101abda07cab1635936a7c7549b483411e2c /src/escrow/escrow_api.c
parentb3d2c4c9fe8837ad962ed33189b0807c7d634923 (diff)
downloadgnunet-db199dd2280071adafc14ff66cfa1c21399f06c2.tar.gz
gnunet-db199dd2280071adafc14ff66cfa1c21399f06c2.zip
change structure of struct GNUNET_ESCROW_Anchor
encode method name and ego name in anchor string implement anchor_string_to_data and anchor_data_to_string in escrow_api.c (not plugin-specific)
Diffstat (limited to 'src/escrow/escrow_api.c')
-rw-r--r--src/escrow/escrow_api.c173
1 files changed, 156 insertions, 17 deletions
diff --git a/src/escrow/escrow_api.c b/src/escrow/escrow_api.c
index e35ba3e9e..e48dc28e4 100644
--- a/src/escrow/escrow_api.c
+++ b/src/escrow/escrow_api.c
@@ -51,6 +51,30 @@ static int anastasis_initialized;
51 51
52 52
53/** 53/**
54 * Plaintext method string
55 */
56static const char *plaintext_string = "plaintext";
57
58
59/**
60 * GNS method string
61 */
62static const char *gns_string = "gns";
63
64
65/**
66 * Anastasis method string
67 */
68static const char *anastasis_string = "anastasis";
69
70
71/**
72 * None method string
73 */
74static const char *none_string = "INVALID-METHOD";
75
76
77/**
54 * Pointer to the plaintext plugin API 78 * Pointer to the plaintext plugin API
55 */ 79 */
56static struct GNUNET_ESCROW_KeyPluginFunctions *plaintext_api; 80static struct GNUNET_ESCROW_KeyPluginFunctions *plaintext_api;
@@ -414,43 +438,158 @@ GNUNET_ESCROW_get_status (struct GNUNET_ESCROW_Handle *h,
414 * Deserialize an escrow anchor string (e.g. from command line) into a 438 * Deserialize an escrow anchor string (e.g. from command line) into a
415 * GNUNET_ESCROW_Anchor struct 439 * GNUNET_ESCROW_Anchor struct
416 * 440 *
417 * @param h the handle for the escrow component
418 * @param anchorString the encoded escrow anchor string 441 * @param anchorString the encoded escrow anchor string
419 * @param method the escrow method to use
420 * 442 *
421 * @return the deserialized data packed into a GNUNET_ESCROW_Anchor struct, 443 * @return the deserialized data packed into a GNUNET_ESCROW_Anchor struct,
422 * NULL if we failed to parse the string 444 * NULL if we failed to parse the string
423 */ 445 */
424struct GNUNET_ESCROW_Anchor * 446struct GNUNET_ESCROW_Anchor *
425GNUNET_ESCROW_anchor_string_to_data (struct GNUNET_ESCROW_Handle *h, 447GNUNET_ESCROW_anchor_string_to_data (const char *anchorString)
426 char *anchorString,
427 enum GNUNET_ESCROW_Key_Escrow_Method method)
428{ 448{
429 const struct GNUNET_ESCROW_KeyPluginFunctions *api; 449 struct GNUNET_ESCROW_Anchor *anchor;
430 450 uint32_t data_size;
431 api = init_plugin (h, method); 451 char *anchorStringCopy, *ptr;
432 return api->anchor_string_to_data (h, anchorString); 452 char *methodString, *egoNameString, *anchorDataString;
453 char delimiter[] = ":";
454
455 anchorStringCopy = GNUNET_strdup (anchorString);
456 anchor = NULL;
457
458 /* parse and decode method */
459 ptr = strtok (anchorStringCopy, delimiter);
460 if (NULL == ptr)
461 goto END;
462 GNUNET_STRINGS_urldecode (ptr, strlen (ptr), &methodString);
463 /* parse and decode ego name */
464 ptr = strtok (NULL, delimiter);
465 if (NULL == ptr)
466 goto END;
467 GNUNET_STRINGS_urldecode (ptr, strlen (ptr), &egoNameString);
468 /* parse and decode anchor data */
469 ptr = strtok (NULL, delimiter);
470 if (NULL == ptr)
471 goto END;
472 GNUNET_STRINGS_urldecode (ptr, strlen (ptr), &anchorDataString);
473 /* check if string is over */
474 ptr = strtok (NULL, delimiter);
475 if (NULL != ptr)
476 goto END;
477
478 data_size = strlen (anchorDataString); // data is NOT null-terminated
479 anchor = GNUNET_malloc (sizeof (struct GNUNET_ESCROW_Anchor)
480 + data_size
481 + strlen (egoNameString) + 1);
482 anchor->size = data_size;
483 anchor->method = GNUNET_ESCROW_method_string_to_number (methodString);
484
485 // ptr is now used to fill the anchor
486 ptr = (char *)&anchor[1];
487 strcpy (ptr, anchorDataString);
488 ptr += data_size;
489 anchor->egoName = ptr;
490 strcpy (ptr, egoNameString);
491
492 END:
493 /* free all non-NULL strings */
494 if (NULL != anchorStringCopy)
495 GNUNET_free (anchorStringCopy);
496 if (NULL != methodString)
497 GNUNET_free (methodString);
498 if (NULL != egoNameString)
499 GNUNET_free (egoNameString);
500 if (NULL != anchorDataString)
501 GNUNET_free (anchorDataString);
502
503 return anchor;
433} 504}
434 505
435 506
436/** 507/**
437 * Serialize an escrow anchor (struct GNUNET_ESCROW_Anchor) into a string 508 * Serialize an escrow anchor (struct GNUNET_ESCROW_Anchor) into a string
438 * 509 *
439 * @param h the handle for the escrow component
440 * @param escrowAnchor the escrow anchor struct 510 * @param escrowAnchor the escrow anchor struct
441 * @param method the escrow method to use
442 * 511 *
443 * @return the encoded escrow anchor string 512 * @return the encoded escrow anchor string
444 */ 513 */
445char * 514char *
446GNUNET_ESCROW_anchor_data_to_string (struct GNUNET_ESCROW_Handle *h, 515GNUNET_ESCROW_anchor_data_to_string (const struct GNUNET_ESCROW_Anchor *escrowAnchor)
447 struct GNUNET_ESCROW_Anchor *escrowAnchor,
448 enum GNUNET_ESCROW_Key_Escrow_Method method)
449{ 516{
450 const struct GNUNET_ESCROW_KeyPluginFunctions *api; 517 char *anchorString, *ptr;
518 const char *methodString, *egoNameString, *anchorData;
519 char *methodStringEnc, *egoNameStringEnc, *anchorDataEnc;
520
521 methodString = GNUNET_ESCROW_method_number_to_string (escrowAnchor->method);
522 GNUNET_STRINGS_urlencode (methodString, strlen (methodString), &methodStringEnc);
523 egoNameString = escrowAnchor->egoName;
524 GNUNET_STRINGS_urlencode (egoNameString, strlen (egoNameString), &egoNameStringEnc);
525 anchorData = (const char *)&escrowAnchor[1];
526 GNUNET_STRINGS_urlencode (anchorData, escrowAnchor->size, &anchorDataEnc);
527
528 anchorString = GNUNET_malloc (strlen (methodStringEnc) + 1
529 + strlen (egoNameStringEnc) + 1
530 + strlen (anchorDataEnc)
531 + 1);
532
533 ptr = anchorString;
534 GNUNET_memcpy (ptr, methodStringEnc, strlen (methodStringEnc));
535 ptr += strlen (methodStringEnc);
536 GNUNET_free (methodStringEnc);
537 *(ptr++) = ':';
538 GNUNET_memcpy (ptr, egoNameStringEnc, strlen (egoNameStringEnc));
539 ptr += strlen (egoNameStringEnc);
540 GNUNET_free (egoNameStringEnc);
541 *(ptr++) = ':';
542 GNUNET_memcpy (ptr, anchorDataEnc, strlen (anchorDataEnc));
543 ptr += strlen (anchorDataEnc);
544 GNUNET_free (anchorDataEnc);
545 *(ptr++) = '\0';
546
547 return anchorString;
548}
451 549
452 api = init_plugin (h, method); 550
453 return api->anchor_data_to_string (h, escrowAnchor); 551/**
552 * Convert a method name string to the respective enum number
553 *
554 * @param methodString the method name string
555 *
556 * @return the enum number
557 */
558enum GNUNET_ESCROW_Key_Escrow_Method
559GNUNET_ESCROW_method_string_to_number (const char *methodString)
560{
561 if (!strcmp (plaintext_string, methodString))
562 return GNUNET_ESCROW_KEY_PLAINTEXT;
563 else if (!strcmp (gns_string, methodString))
564 return GNUNET_ESCROW_KEY_GNS;
565 else if (!strcmp (anastasis_string, methodString))
566 return GNUNET_ESCROW_KEY_ANASTASIS;
567 else
568 return GNUNET_ESCROW_KEY_NONE;
569}
570
571
572/**
573 * Convert a method enum number to the respective method string
574 *
575 * @param method the method enum number
576 *
577 * @return the method string
578 */
579const char *
580GNUNET_ESCROW_method_number_to_string (enum GNUNET_ESCROW_Key_Escrow_Method method)
581{
582 switch (method)
583 {
584 case GNUNET_ESCROW_KEY_PLAINTEXT:
585 return plaintext_string;
586 case GNUNET_ESCROW_KEY_GNS:
587 return gns_string;
588 case GNUNET_ESCROW_KEY_ANASTASIS:
589 return anastasis_string;
590 default:
591 return none_string;
592 }
454} 593}
455 594
456 595