aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--po/POTFILES.in4
-rw-r--r--src/reclaim/oidc_helper.c78
-rw-r--r--src/reclaim/oidc_helper.h38
-rw-r--r--src/reclaim/plugin_rest_openid_connect.c151
-rw-r--r--src/rps/rps-test_util.c14
-rw-r--r--src/transport/Makefile.am8
-rw-r--r--src/transport/gnunet-communicator-udp.c19
-rw-r--r--src/transport/test_communicator_basic.c2
-rw-r--r--src/transport/transport-testing-communicator.c1164
-rw-r--r--src/transport/transport-testing-communicator.h355
-rw-r--r--src/transport/transport-testing2.c1741
-rw-r--r--src/transport/transport-testing2.h970
-rw-r--r--src/util/Makefile.am9
-rw-r--r--src/util/crypto-test-vectors.json56
-rw-r--r--src/util/crypto_rsa.c4
-rw-r--r--src/util/gnunet-crypto-tvg.c874
-rw-r--r--src/util/perf_crypto_rsa.c14
-rwxr-xr-xsrc/util/test_crypto_vectors.sh3
18 files changed, 4137 insertions, 1367 deletions
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 00bdd7cbc..b0371ba21 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -205,8 +205,8 @@ src/json/json_generator.c
205src/json/json_helper.c 205src/json/json_helper.c
206src/json/json_mhd.c 206src/json/json_mhd.c
207src/messenger/gnunet-messenger.c 207src/messenger/gnunet-messenger.c
208src/messenger/gnunet-service-messenger.c
209src/messenger/gnunet-service-messenger_basement.c 208src/messenger/gnunet-service-messenger_basement.c
209src/messenger/gnunet-service-messenger.c
210src/messenger/gnunet-service-messenger_contact.c 210src/messenger/gnunet-service-messenger_contact.c
211src/messenger/gnunet-service-messenger_handle.c 211src/messenger/gnunet-service-messenger_handle.c
212src/messenger/gnunet-service-messenger_list_handles.c 212src/messenger/gnunet-service-messenger_list_handles.c
@@ -545,7 +545,5 @@ src/vpn/vpn_api.c
545src/zonemaster/gnunet-service-zonemaster.c 545src/zonemaster/gnunet-service-zonemaster.c
546src/zonemaster/gnunet-service-zonemaster-monitor.c 546src/zonemaster/gnunet-service-zonemaster-monitor.c
547src/fs/fs_api.h 547src/fs/fs_api.h
548src/include/gnunet_identity_service.h
549src/include/gnunet_messenger_service.h
550src/testbed/testbed_api.h 548src/testbed/testbed_api.h
551src/testbed/testbed_api_operations.h 549src/testbed/testbed_api_operations.h
diff --git a/src/reclaim/oidc_helper.c b/src/reclaim/oidc_helper.c
index c6d56e02d..1dde7b673 100644
--- a/src/reclaim/oidc_helper.c
+++ b/src/reclaim/oidc_helper.c
@@ -567,6 +567,48 @@ OIDC_build_authz_code (const struct GNUNET_IDENTITY_PrivateKey *issuer,
567} 567}
568 568
569 569
570enum GNUNET_GenericReturnValue
571check_code_challenge (const char *code_challenge,
572 uint32_t code_challenge_len,
573 const char *code_verifier)
574{
575 char *code_verifier_hash;
576 char *expected_code_challenge;
577
578 if (0 == code_challenge_len) /* Only check if this code requires a CV */
579 return GNUNET_OK;
580 if (NULL == code_verifier)
581 {
582 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
583 "Expected code verifier!\n");
584 return GNUNET_SYSERR;
585 }
586 code_verifier_hash = GNUNET_malloc (256 / 8);
587 // hash code verifier
588 gcry_md_hash_buffer (GCRY_MD_SHA256,
589 code_verifier_hash,
590 code_verifier,
591 strlen (code_verifier));
592 // encode code verifier
593 GNUNET_STRINGS_base64url_encode (code_verifier_hash, 256 / 8,
594 &expected_code_challenge);
595 GNUNET_free (code_verifier_hash);
596 if (0 !=
597 strncmp (expected_code_challenge, code_challenge, code_challenge_len))
598 {
599 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
600 "Invalid code verifier! Expected: %s, Got: %.*s\n",
601 expected_code_challenge,
602 code_challenge_len,
603 code_challenge);
604 GNUNET_free (expected_code_challenge);
605 return GNUNET_SYSERR;
606 }
607 GNUNET_free (expected_code_challenge);
608 return GNUNET_OK;
609}
610
611
570/** 612/**
571 * Parse reclaim ticket and nonce from 613 * Parse reclaim ticket and nonce from
572 * authorization code. 614 * authorization code.
@@ -589,16 +631,15 @@ OIDC_parse_authz_code (const struct GNUNET_IDENTITY_PublicKey *audience,
589 struct GNUNET_RECLAIM_Ticket *ticket, 631 struct GNUNET_RECLAIM_Ticket *ticket,
590 struct GNUNET_RECLAIM_AttributeList **attrs, 632 struct GNUNET_RECLAIM_AttributeList **attrs,
591 struct GNUNET_RECLAIM_PresentationList **presentations, 633 struct GNUNET_RECLAIM_PresentationList **presentations,
592 char **nonce_str) 634 char **nonce_str,
635 enum OIDC_VerificationOptions opts)
593{ 636{
594 char *code_payload; 637 char *code_payload;
595 char *ptr; 638 char *ptr;
596 char *plaintext; 639 char *plaintext;
597 char *attrs_ser; 640 char *attrs_ser;
598 char *presentations_ser; 641 char *presentations_ser;
599 char *expected_code_challenge;
600 char *code_challenge; 642 char *code_challenge;
601 char *code_verifier_hash;
602 struct GNUNET_CRYPTO_EccSignaturePurpose *purpose; 643 struct GNUNET_CRYPTO_EccSignaturePurpose *purpose;
603 struct GNUNET_IDENTITY_Signature *signature; 644 struct GNUNET_IDENTITY_Signature *signature;
604 uint32_t code_challenge_len; 645 uint32_t code_challenge_len;
@@ -636,38 +677,15 @@ OIDC_parse_authz_code (const struct GNUNET_IDENTITY_PublicKey *audience,
636 // cmp code_challenge code_verifier 677 // cmp code_challenge code_verifier
637 code_challenge_len = ntohl (params->code_challenge_len); 678 code_challenge_len = ntohl (params->code_challenge_len);
638 code_challenge = ((char *) &params[1]); 679 code_challenge = ((char *) &params[1]);
639 if (0 != code_challenge_len) /* Only check if this code requires a CV */ 680 if (!(opts & OIDC_VERIFICATION_NO_CODE_VERIFIER))
640 { 681 {
641 if (NULL == code_verifier) 682 if (GNUNET_OK != check_code_challenge (code_challenge,
642 { 683 code_challenge_len,
643 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 684 code_verifier))
644 "Expected code verifier!\n");
645 GNUNET_free (code_payload);
646 return GNUNET_SYSERR;
647 }
648 code_verifier_hash = GNUNET_malloc (256 / 8);
649 // hash code verifier
650 gcry_md_hash_buffer (GCRY_MD_SHA256,
651 code_verifier_hash,
652 code_verifier,
653 strlen (code_verifier));
654 // encode code verifier
655 GNUNET_STRINGS_base64url_encode (code_verifier_hash, 256 / 8,
656 &expected_code_challenge);
657 GNUNET_free (code_verifier_hash);
658 if (0 !=
659 strncmp (expected_code_challenge, code_challenge, code_challenge_len))
660 { 685 {
661 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
662 "Invalid code verifier! Expected: %s, Got: %.*s\n",
663 expected_code_challenge,
664 code_challenge_len,
665 code_challenge);
666 GNUNET_free (code_payload); 686 GNUNET_free (code_payload);
667 GNUNET_free (expected_code_challenge);
668 return GNUNET_SYSERR; 687 return GNUNET_SYSERR;
669 } 688 }
670 GNUNET_free (expected_code_challenge);
671 } 689 }
672 nonce_len = ntohl (params->nonce_len); 690 nonce_len = ntohl (params->nonce_len);
673 if (0 != nonce_len) 691 if (0 != nonce_len)
diff --git a/src/reclaim/oidc_helper.h b/src/reclaim/oidc_helper.h
index eb1022423..2a8b7bbae 100644
--- a/src/reclaim/oidc_helper.h
+++ b/src/reclaim/oidc_helper.h
@@ -38,6 +38,19 @@
38 38
39#define SERVER_ADDRESS "https://api.reclaim" 39#define SERVER_ADDRESS "https://api.reclaim"
40 40
41enum OIDC_VerificationOptions
42{
43 /**
44 * Strict verification
45 */
46 OIDC_VERIFICATION_DEFAULT = 0,
47
48 /**
49 * Do not check code verifier even if expected
50 */
51 OIDC_VERIFICATION_NO_CODE_VERIFIER = 1
52};
53
41/** 54/**
42 * Create a JWT from attributes 55 * Create a JWT from attributes
43 * 56 *
@@ -51,12 +64,13 @@
51 */ 64 */
52char* 65char*
53OIDC_generate_id_token (const struct GNUNET_IDENTITY_PublicKey *aud_key, 66OIDC_generate_id_token (const struct GNUNET_IDENTITY_PublicKey *aud_key,
54 const struct GNUNET_IDENTITY_PublicKey *sub_key, 67 const struct GNUNET_IDENTITY_PublicKey *sub_key,
55 const struct GNUNET_RECLAIM_AttributeList *attrs, 68 const struct GNUNET_RECLAIM_AttributeList *attrs,
56 const struct GNUNET_RECLAIM_PresentationList *presentations, 69 const struct
57 const struct GNUNET_TIME_Relative *expiration_time, 70 GNUNET_RECLAIM_PresentationList *presentations,
58 const char *nonce, 71 const struct GNUNET_TIME_Relative *expiration_time,
59 const char *secret_key); 72 const char *nonce,
73 const char *secret_key);
60 74
61/** 75/**
62 * Builds an OIDC authorization code including 76 * Builds an OIDC authorization code including
@@ -68,13 +82,15 @@ OIDC_generate_id_token (const struct GNUNET_IDENTITY_PublicKey *aud_key,
68 * @param presentations credential presentation list 82 * @param presentations credential presentation list
69 * @param nonce the nonce to include in the code 83 * @param nonce the nonce to include in the code
70 * @param code_challenge PKCE code challenge 84 * @param code_challenge PKCE code challenge
85 * @param opts verification options
71 * @return a new authorization code (caller must free) 86 * @return a new authorization code (caller must free)
72 */ 87 */
73char* 88char*
74OIDC_build_authz_code (const struct GNUNET_IDENTITY_PrivateKey *issuer, 89OIDC_build_authz_code (const struct GNUNET_IDENTITY_PrivateKey *issuer,
75 const struct GNUNET_RECLAIM_Ticket *ticket, 90 const struct GNUNET_RECLAIM_Ticket *ticket,
76 const struct GNUNET_RECLAIM_AttributeList *attrs, 91 const struct GNUNET_RECLAIM_AttributeList *attrs,
77 const struct GNUNET_RECLAIM_PresentationList *presentations, 92 const struct
93 GNUNET_RECLAIM_PresentationList *presentations,
78 const char *nonce, 94 const char *nonce,
79 const char *code_challenge); 95 const char *code_challenge);
80 96
@@ -99,7 +115,8 @@ OIDC_parse_authz_code (const struct GNUNET_IDENTITY_PublicKey *ecdsa_pub,
99 struct GNUNET_RECLAIM_Ticket *ticket, 115 struct GNUNET_RECLAIM_Ticket *ticket,
100 struct GNUNET_RECLAIM_AttributeList **attrs, 116 struct GNUNET_RECLAIM_AttributeList **attrs,
101 struct GNUNET_RECLAIM_PresentationList **presentations, 117 struct GNUNET_RECLAIM_PresentationList **presentations,
102 char **nonce); 118 char **nonce,
119 enum OIDC_VerificationOptions opts);
103 120
104/** 121/**
105 * Build a token response for a token request 122 * Build a token response for a token request
@@ -126,7 +143,7 @@ OIDC_access_token_new (const struct GNUNET_RECLAIM_Ticket *ticket);
126 * Parse an access token 143 * Parse an access token
127 */ 144 */
128int 145int
129OIDC_access_token_parse (const char* token, 146OIDC_access_token_parse (const char*token,
130 struct GNUNET_RECLAIM_Ticket **ticket); 147 struct GNUNET_RECLAIM_Ticket **ticket);
131 148
132 149
@@ -154,6 +171,7 @@ OIDC_check_scopes_for_claim_request (const char *scopes,
154char * 171char *
155OIDC_generate_userinfo (const struct GNUNET_IDENTITY_PublicKey *sub_key, 172OIDC_generate_userinfo (const struct GNUNET_IDENTITY_PublicKey *sub_key,
156 const struct GNUNET_RECLAIM_AttributeList *attrs, 173 const struct GNUNET_RECLAIM_AttributeList *attrs,
157 const struct GNUNET_RECLAIM_PresentationList *presentations); 174 const struct
175 GNUNET_RECLAIM_PresentationList *presentations);
158 176
159#endif 177#endif
diff --git a/src/reclaim/plugin_rest_openid_connect.c b/src/reclaim/plugin_rest_openid_connect.c
index 7a8a886bd..8d21a5c99 100644
--- a/src/reclaim/plugin_rest_openid_connect.c
+++ b/src/reclaim/plugin_rest_openid_connect.c
@@ -227,6 +227,11 @@
227 */ 227 */
228#define OIDC_ERROR_KEY_ACCESS_DENIED "access_denied" 228#define OIDC_ERROR_KEY_ACCESS_DENIED "access_denied"
229 229
230/**
231 * How long to wait for a consume in userinfo endpoint
232 */
233#define CONSUME_TIMEOUT GNUNET_TIME_relative_multiply ( \
234 GNUNET_TIME_UNIT_SECONDS,2)
230 235
231/** 236/**
232 * OIDC ignored parameter array 237 * OIDC ignored parameter array
@@ -240,7 +245,12 @@ static char *OIDC_ignored_parameter_array[] = { "display",
240 "acr_values" }; 245 "acr_values" };
241 246
242/** 247/**
243 * OIDC Hash map that keeps track of issued cookies 248 * OIDC hashmap for cached access tokens and codes
249 */
250struct GNUNET_CONTAINER_MultiHashMap *oidc_code_cache;
251
252/**
253 * OIDC hashmap that keeps track of issued cookies
244 */ 254 */
245struct GNUNET_CONTAINER_MultiHashMap *OIDC_cookie_jar_map; 255struct GNUNET_CONTAINER_MultiHashMap *OIDC_cookie_jar_map;
246 256
@@ -460,6 +470,11 @@ struct RequestHandle
460 struct GNUNET_RECLAIM_Operation *idp_op; 470 struct GNUNET_RECLAIM_Operation *idp_op;
461 471
462 /** 472 /**
473 * Timeout task for consume
474 */
475 struct GNUNET_SCHEDULER_Task *consume_timeout_op;
476
477 /**
463 * Attribute iterator 478 * Attribute iterator
464 */ 479 */
465 struct GNUNET_RECLAIM_AttributeIterator *attr_it; 480 struct GNUNET_RECLAIM_AttributeIterator *attr_it;
@@ -506,6 +521,11 @@ struct RequestHandle
506 char *url; 521 char *url;
507 522
508 /** 523 /**
524 * The passed access token
525 */
526 char *access_token;
527
528 /**
509 * The tld for redirect 529 * The tld for redirect
510 */ 530 */
511 char *tld; 531 char *tld;
@@ -571,6 +591,8 @@ cleanup_handle (struct RequestHandle *handle)
571 GNUNET_RECLAIM_ticket_iteration_stop (handle->ticket_it); 591 GNUNET_RECLAIM_ticket_iteration_stop (handle->ticket_it);
572 if (NULL != handle->idp_op) 592 if (NULL != handle->idp_op)
573 GNUNET_RECLAIM_cancel (handle->idp_op); 593 GNUNET_RECLAIM_cancel (handle->idp_op);
594 if (NULL != handle->consume_timeout_op)
595 GNUNET_SCHEDULER_cancel (handle->consume_timeout_op);
574 GNUNET_free (handle->url); 596 GNUNET_free (handle->url);
575 GNUNET_free (handle->tld); 597 GNUNET_free (handle->tld);
576 GNUNET_free (handle->redirect_prefix); 598 GNUNET_free (handle->redirect_prefix);
@@ -601,6 +623,8 @@ cleanup_handle (struct RequestHandle *handle)
601 GNUNET_CONTAINER_DLL_remove (requests_head, 623 GNUNET_CONTAINER_DLL_remove (requests_head,
602 requests_tail, 624 requests_tail,
603 handle); 625 handle);
626 if (NULL != handle->access_token)
627 GNUNET_free (handle->access_token);
604 GNUNET_free (handle); 628 GNUNET_free (handle);
605} 629}
606 630
@@ -1282,8 +1306,8 @@ code_redirect (void *cls)
1282 { 1306 {
1283 if (GNUNET_OK != 1307 if (GNUNET_OK !=
1284 GNUNET_IDENTITY_public_key_from_string (handle->oidc 1308 GNUNET_IDENTITY_public_key_from_string (handle->oidc
1285 ->login_identity, 1309 ->login_identity,
1286 &pubkey)) 1310 &pubkey))
1287 { 1311 {
1288 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_COOKIE); 1312 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_COOKIE);
1289 handle->edesc = 1313 handle->edesc =
@@ -1662,7 +1686,7 @@ authorize_endpoint (struct GNUNET_REST_RequestHandle *con_handle,
1662 1686
1663 if (GNUNET_OK != 1687 if (GNUNET_OK !=
1664 GNUNET_IDENTITY_public_key_from_string (handle->oidc->client_id, 1688 GNUNET_IDENTITY_public_key_from_string (handle->oidc->client_id,
1665 &handle->oidc->client_pkey)) 1689 &handle->oidc->client_pkey))
1666 { 1690 {
1667 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_UNAUTHORIZED_CLIENT); 1691 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_UNAUTHORIZED_CLIENT);
1668 handle->edesc = GNUNET_strdup ("The client is not authorized to request an " 1692 handle->edesc = GNUNET_strdup ("The client is not authorized to request an "
@@ -2071,7 +2095,8 @@ token_endpoint (struct GNUNET_REST_RequestHandle *con_handle,
2071 2095
2072 // decode code 2096 // decode code
2073 if (GNUNET_OK != OIDC_parse_authz_code (&cid, code, code_verifier, &ticket, 2097 if (GNUNET_OK != OIDC_parse_authz_code (&cid, code, code_verifier, &ticket,
2074 &cl, &pl, &nonce)) 2098 &cl, &pl, &nonce,
2099 OIDC_VERIFICATION_DEFAULT))
2075 { 2100 {
2076 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST); 2101 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
2077 handle->edesc = GNUNET_strdup ("invalid code"); 2102 handle->edesc = GNUNET_strdup ("invalid code");
@@ -2080,7 +2105,6 @@ token_endpoint (struct GNUNET_REST_RequestHandle *con_handle,
2080 GNUNET_SCHEDULER_add_now (&do_error, handle); 2105 GNUNET_SCHEDULER_add_now (&do_error, handle);
2081 return; 2106 return;
2082 } 2107 }
2083 GNUNET_free (code);
2084 2108
2085 // create jwt 2109 // create jwt
2086 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, 2110 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg,
@@ -2091,6 +2115,7 @@ token_endpoint (struct GNUNET_REST_RequestHandle *con_handle,
2091 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR); 2115 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
2092 handle->edesc = GNUNET_strdup ("gnunet configuration failed"); 2116 handle->edesc = GNUNET_strdup ("gnunet configuration failed");
2093 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR; 2117 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
2118 GNUNET_free (code);
2094 GNUNET_SCHEDULER_add_now (&do_error, handle); 2119 GNUNET_SCHEDULER_add_now (&do_error, handle);
2095 return; 2120 return;
2096 } 2121 }
@@ -2105,6 +2130,7 @@ token_endpoint (struct GNUNET_REST_RequestHandle *con_handle,
2105 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST); 2130 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
2106 handle->edesc = GNUNET_strdup ("No signing secret configured!"); 2131 handle->edesc = GNUNET_strdup ("No signing secret configured!");
2107 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR; 2132 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
2133 GNUNET_free (code);
2108 GNUNET_SCHEDULER_add_now (&do_error, handle); 2134 GNUNET_SCHEDULER_add_now (&do_error, handle);
2109 return; 2135 return;
2110 } 2136 }
@@ -2116,6 +2142,26 @@ token_endpoint (struct GNUNET_REST_RequestHandle *con_handle,
2116 (NULL != nonce) ? nonce : NULL, 2142 (NULL != nonce) ? nonce : NULL,
2117 jwt_secret); 2143 jwt_secret);
2118 access_token = OIDC_access_token_new (&ticket); 2144 access_token = OIDC_access_token_new (&ticket);
2145 /* Store mapping from access token to code so we can later
2146 * fall back on the provided attributes in userinfo
2147 */
2148 GNUNET_CRYPTO_hash (access_token,
2149 strlen (access_token),
2150 &cache_key);
2151 char *tmp_at = GNUNET_CONTAINER_multihashmap_get (oidc_code_cache,
2152 &cache_key);
2153 GNUNET_CONTAINER_multihashmap_put (oidc_code_cache,
2154 &cache_key,
2155 code,
2156 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
2157 /* If there was a previus code in there, free the old value */
2158 if (NULL != tmp_at)
2159 {
2160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2161 "OIDC access token already issued. Cleanup.\n");
2162 GNUNET_free (tmp_at);
2163 }
2164
2119 OIDC_build_token_response (access_token, 2165 OIDC_build_token_response (access_token,
2120 id_token, 2166 id_token,
2121 &expiration_time, 2167 &expiration_time,
@@ -2149,6 +2195,10 @@ consume_ticket (void *cls,
2149 struct GNUNET_RECLAIM_PresentationListEntry *atle; 2195 struct GNUNET_RECLAIM_PresentationListEntry *atle;
2150 struct MHD_Response *resp; 2196 struct MHD_Response *resp;
2151 char *result_str; 2197 char *result_str;
2198
2199 if (NULL != handle->consume_timeout_op)
2200 GNUNET_SCHEDULER_cancel (handle->consume_timeout_op);
2201 handle->consume_timeout_op = NULL;
2152 handle->idp_op = NULL; 2202 handle->idp_op = NULL;
2153 2203
2154 if (NULL == identity) 2204 if (NULL == identity)
@@ -2180,8 +2230,9 @@ consume_ticket (void *cls,
2180 for (atle = handle->presentations->list_head; 2230 for (atle = handle->presentations->list_head;
2181 NULL != atle; atle = atle->next) 2231 NULL != atle; atle = atle->next)
2182 { 2232 {
2183 if (GNUNET_NO == GNUNET_RECLAIM_id_is_equal (&atle->presentation->credential_id, 2233 if (GNUNET_NO == GNUNET_RECLAIM_id_is_equal (
2184 &pres->credential_id)) 2234 &atle->presentation->credential_id,
2235 &pres->credential_id))
2185 continue; 2236 continue;
2186 break; /** already in list **/ 2237 break; /** already in list **/
2187 } 2238 }
@@ -2190,8 +2241,8 @@ consume_ticket (void *cls,
2190 /** Credential matches for attribute, add **/ 2241 /** Credential matches for attribute, add **/
2191 atle = GNUNET_new (struct GNUNET_RECLAIM_PresentationListEntry); 2242 atle = GNUNET_new (struct GNUNET_RECLAIM_PresentationListEntry);
2192 atle->presentation = GNUNET_RECLAIM_presentation_new (pres->type, 2243 atle->presentation = GNUNET_RECLAIM_presentation_new (pres->type,
2193 pres->data, 2244 pres->data,
2194 pres->data_size); 2245 pres->data_size);
2195 GNUNET_CONTAINER_DLL_insert (handle->presentations->list_head, 2246 GNUNET_CONTAINER_DLL_insert (handle->presentations->list_head,
2196 handle->presentations->list_tail, 2247 handle->presentations->list_tail,
2197 atle); 2248 atle);
@@ -2199,6 +2250,69 @@ consume_ticket (void *cls,
2199} 2250}
2200 2251
2201 2252
2253static void
2254consume_timeout (void*cls)
2255{
2256 struct RequestHandle *handle = cls;
2257 struct GNUNET_HashCode cache_key;
2258 struct GNUNET_RECLAIM_AttributeList *cl = NULL;
2259 struct GNUNET_RECLAIM_PresentationList *pl = NULL;
2260 struct GNUNET_RECLAIM_Ticket ticket;
2261 char *nonce;
2262 char *cached_code;
2263
2264 handle->consume_timeout_op = NULL;
2265 if (NULL != handle->idp_op)
2266 GNUNET_RECLAIM_cancel (handle->idp_op);
2267 handle->idp_op = NULL;
2268
2269 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2270 "Ticket consumptioned timed out. Using cache...\n");
2271 GNUNET_CRYPTO_hash (handle->access_token,
2272 strlen (handle->access_token),
2273 &cache_key);
2274 cached_code = GNUNET_CONTAINER_multihashmap_get (oidc_code_cache,
2275 &cache_key);
2276 if (NULL == cached_code)
2277 {
2278 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_TOKEN);
2279 handle->edesc = GNUNET_strdup ("No Access Token in cache!");
2280 handle->response_code = MHD_HTTP_UNAUTHORIZED;
2281 GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle);
2282 return;
2283 }
2284
2285 // decode code
2286 if (GNUNET_OK != OIDC_parse_authz_code (&handle->ticket.audience,
2287 cached_code, NULL, &ticket,
2288 &cl, &pl, &nonce,
2289 OIDC_VERIFICATION_NO_CODE_VERIFIER))
2290 {
2291 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
2292 handle->edesc = GNUNET_strdup ("invalid code");
2293 handle->response_code = MHD_HTTP_BAD_REQUEST;
2294 GNUNET_free (cached_code);
2295 GNUNET_SCHEDULER_add_now (&do_error, handle);
2296 return;
2297 }
2298
2299 struct MHD_Response *resp;
2300 char *result_str;
2301
2302 result_str = OIDC_generate_userinfo (&handle->ticket.identity,
2303 cl,
2304 pl);
2305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Userinfo: %s\n", result_str);
2306 resp = GNUNET_REST_create_response (result_str);
2307 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
2308 GNUNET_free (result_str);
2309 GNUNET_free (nonce);
2310 GNUNET_RECLAIM_attribute_list_destroy (cl);
2311 GNUNET_RECLAIM_presentation_list_destroy (pl);
2312 cleanup_handle (handle);
2313}
2314
2315
2202/** 2316/**
2203 * Responds to userinfo GET and url-encoded POST request 2317 * Responds to userinfo GET and url-encoded POST request
2204 * 2318 *
@@ -2295,6 +2409,11 @@ userinfo_endpoint (struct GNUNET_REST_RequestHandle *con_handle,
2295 handle->presentations = 2409 handle->presentations =
2296 GNUNET_new (struct GNUNET_RECLAIM_PresentationList); 2410 GNUNET_new (struct GNUNET_RECLAIM_PresentationList);
2297 2411
2412 /* If the consume takes too long, we use values from the cache */
2413 handle->access_token = GNUNET_strdup (authorization_access_token);
2414 handle->consume_timeout_op = GNUNET_SCHEDULER_add_delayed (CONSUME_TIMEOUT,
2415 &consume_timeout,
2416 handle);
2298 handle->idp_op = GNUNET_RECLAIM_ticket_consume (idp, 2417 handle->idp_op = GNUNET_RECLAIM_ticket_consume (idp,
2299 privkey, 2418 privkey,
2300 &handle->ticket, 2419 &handle->ticket,
@@ -2554,6 +2673,10 @@ rest_identity_process_request (struct GNUNET_REST_RequestHandle *rest_handle,
2554 if (NULL == OIDC_cookie_jar_map) 2673 if (NULL == OIDC_cookie_jar_map)
2555 OIDC_cookie_jar_map = GNUNET_CONTAINER_multihashmap_create (10, 2674 OIDC_cookie_jar_map = GNUNET_CONTAINER_multihashmap_create (10,
2556 GNUNET_NO); 2675 GNUNET_NO);
2676 if (NULL == oidc_code_cache)
2677 oidc_code_cache = GNUNET_CONTAINER_multihashmap_create (10,
2678 GNUNET_NO);
2679
2557 handle->response_code = 0; 2680 handle->response_code = 0;
2558 handle->timeout = GNUNET_TIME_UNIT_FOREVER_REL; 2681 handle->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
2559 handle->proc_cls = proc_cls; 2682 handle->proc_cls = proc_cls;
@@ -2646,6 +2769,14 @@ libgnunet_plugin_rest_openid_connect_done (void *cls)
2646 NULL); 2769 NULL);
2647 GNUNET_CONTAINER_multihashmap_destroy (OIDC_cookie_jar_map); 2770 GNUNET_CONTAINER_multihashmap_destroy (OIDC_cookie_jar_map);
2648 } 2771 }
2772 if (NULL != oidc_code_cache)
2773 {
2774 GNUNET_CONTAINER_multihashmap_iterate (oidc_code_cache,
2775 &cleanup_hashmap,
2776 NULL);
2777 GNUNET_CONTAINER_multihashmap_destroy (oidc_code_cache);
2778 }
2779
2649 GNUNET_free (allow_methods); 2780 GNUNET_free (allow_methods);
2650 if (NULL != gns_handle) 2781 if (NULL != gns_handle)
2651 GNUNET_GNS_disconnect (gns_handle); 2782 GNUNET_GNS_disconnect (gns_handle);
diff --git a/src/rps/rps-test_util.c b/src/rps/rps-test_util.c
index b73be3457..497225973 100644
--- a/src/rps/rps-test_util.c
+++ b/src/rps/rps-test_util.c
@@ -186,9 +186,9 @@ to_file_raw (const char *file_name, const char *buf, size_t size_buf)
186 if (size_buf != size_written) 186 if (size_buf != size_written)
187 { 187 {
188 LOG (GNUNET_ERROR_TYPE_WARNING, 188 LOG (GNUNET_ERROR_TYPE_WARNING,
189 "Unable to write to file! (Size: %zu, size_written: %zu)\n", 189 "Unable to write to file! (Size: %llu, size_written: %llu)\n",
190 size_buf, 190 (unsigned long long) size_buf,
191 size_written); 191 (unsigned long long) size_written);
192 192
193 if (GNUNET_YES != GNUNET_DISK_file_close (f)) 193 if (GNUNET_YES != GNUNET_DISK_file_close (f))
194 LOG (GNUNET_ERROR_TYPE_WARNING, 194 LOG (GNUNET_ERROR_TYPE_WARNING,
@@ -197,8 +197,8 @@ to_file_raw (const char *file_name, const char *buf, size_t size_buf)
197 return; 197 return;
198 } 198 }
199 LOG (GNUNET_ERROR_TYPE_WARNING, 199 LOG (GNUNET_ERROR_TYPE_WARNING,
200 "Wrote %zu bytes raw.\n", 200 "Wrote %llu bytes raw.\n",
201 size_written); 201 (unsigned long long) size_written);
202 if (GNUNET_YES != GNUNET_DISK_file_close (f)) 202 if (GNUNET_YES != GNUNET_DISK_file_close (f))
203 LOG (GNUNET_ERROR_TYPE_WARNING, 203 LOG (GNUNET_ERROR_TYPE_WARNING,
204 "Unable to close file\n"); 204 "Unable to close file\n");
@@ -238,8 +238,8 @@ to_file_raw_unaligned (const char *file_name,
238 "num_bits_buf_unaligned: %u\n", 238 "num_bits_buf_unaligned: %u\n",
239 num_bits_buf_unaligned); 239 num_bits_buf_unaligned);
240 LOG (GNUNET_ERROR_TYPE_DEBUG, 240 LOG (GNUNET_ERROR_TYPE_DEBUG,
241 "ua args: size_buf: %zu, bits_needed: %u -> iter: %u\n", 241 "ua args: size_buf: %llu, bits_needed: %u -> iter: %u\n",
242 size_buf, 242 (unsigned long long) size_buf,
243 bits_needed, 243 bits_needed,
244 bytes_iter); 244 bytes_iter);
245 buf_write[0] = buf_unaligned; 245 buf_write[0] = buf_unaligned;
diff --git a/src/transport/Makefile.am b/src/transport/Makefile.am
index df5249275..0f9d40e13 100644
--- a/src/transport/Makefile.am
+++ b/src/transport/Makefile.am
@@ -168,10 +168,16 @@ libgnunettransporttesting_la_LDFLAGS = \
168 $(GN_LIB_LDFLAGS) 168 $(GN_LIB_LDFLAGS)
169 169
170libgnunettransporttesting2_la_SOURCES = \ 170libgnunettransporttesting2_la_SOURCES = \
171 transport-testing2.c transport-testing2.h 171 transport-testing2.c transport-testing2.h \
172 transport-testing-communicator.c transport-testing-communicator.h
172libgnunettransporttesting2_la_LIBADD = \ 173libgnunettransporttesting2_la_LIBADD = \
173 libgnunettransport.la \ 174 libgnunettransport.la \
175 libgnunettransportapplication.la \
176 libgnunettransportcore.la \
177 $(top_builddir)/src/testing/libgnunettesting.la \
178 $(top_builddir)/src/ats/libgnunetats.la \
174 $(top_builddir)/src/hello/libgnunethello.la \ 179 $(top_builddir)/src/hello/libgnunethello.la \
180 $(top_builddir)/src/peerstore/libgnunetpeerstore.la \
175 $(top_builddir)/src/util/libgnunetutil.la 181 $(top_builddir)/src/util/libgnunetutil.la
176libgnunettransporttesting2_la_LDFLAGS = \ 182libgnunettransporttesting2_la_LDFLAGS = \
177 $(GN_LIBINTL) \ 183 $(GN_LIBINTL) \
diff --git a/src/transport/gnunet-communicator-udp.c b/src/transport/gnunet-communicator-udp.c
index 4a91a70a5..1f87ba6d4 100644
--- a/src/transport/gnunet-communicator-udp.c
+++ b/src/transport/gnunet-communicator-udp.c
@@ -1741,8 +1741,7 @@ static void
1741kce_generate_cb (void *cls) 1741kce_generate_cb (void *cls)
1742{ 1742{
1743 struct SharedSecret *ss = cls; 1743 struct SharedSecret *ss = cls;
1744 1744 kce_task = NULL;
1745
1746 1745
1747 if (((GNUNET_NO == ss->sender->rekeying) && (ss->sender->acks_available < 1746 if (((GNUNET_NO == ss->sender->rekeying) && (ss->sender->acks_available <
1748 KCN_TARGET) ) || 1747 KCN_TARGET) ) ||
@@ -1776,6 +1775,7 @@ static void
1776kce_generate_rekey_cb (void *cls) 1775kce_generate_rekey_cb (void *cls)
1777{ 1776{
1778 struct SharedSecret *ss = cls; 1777 struct SharedSecret *ss = cls;
1778 kce_task_rekey = NULL;
1779 1779
1780 if (NULL == kce_task) 1780 if (NULL == kce_task)
1781 { 1781 {
@@ -3261,6 +3261,21 @@ do_shutdown (void *cls)
3261 GNUNET_SCHEDULER_cancel (broadcast_task); 3261 GNUNET_SCHEDULER_cancel (broadcast_task);
3262 broadcast_task = NULL; 3262 broadcast_task = NULL;
3263 } 3263 }
3264 if (NULL != kce_task_rekey)
3265 {
3266 GNUNET_SCHEDULER_cancel (kce_task_rekey);
3267 kce_task_rekey = NULL;
3268 }
3269 if (NULL != kce_task)
3270 {
3271 GNUNET_SCHEDULER_cancel (kce_task);
3272 kce_task = NULL;
3273 }
3274 if (NULL != timeout_task)
3275 {
3276 GNUNET_SCHEDULER_cancel (timeout_task);
3277 timeout_task = NULL;
3278 }
3264 if (NULL != read_task) 3279 if (NULL != read_task)
3265 { 3280 {
3266 GNUNET_SCHEDULER_cancel (read_task); 3281 GNUNET_SCHEDULER_cancel (read_task);
diff --git a/src/transport/test_communicator_basic.c b/src/transport/test_communicator_basic.c
index 46eee2299..21c371f3a 100644
--- a/src/transport/test_communicator_basic.c
+++ b/src/transport/test_communicator_basic.c
@@ -26,7 +26,7 @@
26*/ 26*/
27#include "platform.h" 27#include "platform.h"
28#include "gnunet_util_lib.h" 28#include "gnunet_util_lib.h"
29#include "transport-testing2.h" 29#include "transport-testing-communicator.h"
30#include "gnunet_ats_transport_service.h" 30#include "gnunet_ats_transport_service.h"
31#include "gnunet_signatures.h" 31#include "gnunet_signatures.h"
32#include "gnunet_testing_lib.h" 32#include "gnunet_testing_lib.h"
diff --git a/src/transport/transport-testing-communicator.c b/src/transport/transport-testing-communicator.c
new file mode 100644
index 000000000..6d74b12e8
--- /dev/null
+++ b/src/transport/transport-testing-communicator.c
@@ -0,0 +1,1164 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2019 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21/**
22 * @file transport/transport-testing-communicator.c
23 * @brief functions related to testing-tng
24 * @author Christian Grothoff
25 * @author Julius Bünger
26 */
27#include "platform.h"
28#include "gnunet_util_lib.h"
29#include "gnunet_protocols.h"
30#include "gnunet_constants.h"
31#include "transport-testing-communicator.h"
32#include "gnunet_ats_transport_service.h"
33#include "gnunet_hello_lib.h"
34#include "gnunet_signatures.h"
35#include "transport.h"
36#include <inttypes.h>
37
38#define LOG(kind, ...) GNUNET_log_from (kind, "transport-testing2", __VA_ARGS__)
39
40struct MyClient
41{
42 struct MyClient *prev;
43 struct MyClient *next;
44 /**
45 * @brief Handle to the client
46 */
47 struct GNUNET_SERVICE_Client *client;
48
49 /**
50 * @brief Handle to the client
51 */
52 struct GNUNET_MQ_Handle *c_mq;
53
54 /**
55 * The TCH
56 */
57 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc;
58
59};
60
61/**
62 * @brief Queue of a communicator and some context
63 */
64struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue
65{
66 /**
67 * @brief Handle to the TransportCommunicator
68 */
69 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h;
70
71 /**
72 * @brief Envelope to a message that requests the opening of the queue.
73 *
74 * If the client already requests queue(s), but the communicator is not yet
75 * connected, we cannot send the request to open the queue. Save it until the
76 * communicator becomes available and send it then.
77 */
78 struct GNUNET_MQ_Envelope *open_queue_env;
79
80 /**
81 * @brief Peer ID of the peer on the other side of the queue
82 */
83 struct GNUNET_PeerIdentity peer_id;
84
85 /**
86 * @brief Queue ID
87 */
88 uint32_t qid;
89
90 /**
91 * @brief Current message id
92 */
93 uint64_t mid;
94
95 /**
96 * An `enum GNUNET_NetworkType` in NBO.
97 */
98 uint32_t nt;
99
100 /**
101 * Maximum transmission unit. UINT32_MAX for unlimited.
102 */
103 uint32_t mtu;
104
105 /**
106 * Queue length. UINT64_MAX for unlimited.
107 */
108 uint64_t q_len;
109
110 /**
111 * Queue prio
112 */
113 uint32_t priority;
114
115 /**
116 * An `enum GNUNET_TRANSPORT_ConnectionStatus` in NBO.
117 */
118 uint32_t cs;
119
120 /**
121 * @brief Next element inside a DLL
122 */
123 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *next;
124
125 /**
126 * @brief Previous element inside a DLL
127 */
128 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *prev;
129};
130
131
132/**
133 * @brief Handle/Context to a single transmission
134 */
135struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorTransmission
136{
137};
138
139
140/**
141 * @brief Check whether incoming msg indicating available communicator is
142 * correct
143 *
144 * @param cls Closure
145 * @param msg Message struct
146 *
147 * @return GNUNET_YES in case message is correct
148 */
149static int
150check_communicator_available (
151 void *cls,
152 const struct GNUNET_TRANSPORT_CommunicatorAvailableMessage *msg)
153{
154 uint16_t size;
155
156 size = ntohs (msg->header.size) - sizeof(*msg);
157 if (0 == size)
158 return GNUNET_OK; /* receive-only communicator */
159 GNUNET_MQ_check_zero_termination (msg);
160 return GNUNET_OK;
161}
162
163
164/**
165 * @brief Handle new communicator
166 *
167 * Store characteristics of communicator, call respective client callback.
168 *
169 * @param cls Closure - communicator handle
170 * @param msg Message struct
171 */
172static void
173handle_communicator_available (
174 void *cls,
175 const struct GNUNET_TRANSPORT_CommunicatorAvailableMessage *msg)
176{
177 struct MyClient *client = cls;
178 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h =
179 client->tc;
180 uint16_t size;
181 tc_h->c_mq = client->c_mq;
182
183 size = ntohs (msg->header.size) - sizeof(*msg);
184 if (0 == size)
185 {
186 GNUNET_SERVICE_client_continue (client->client);
187 return; /* receive-only communicator */
188 }
189 tc_h->c_characteristics = ntohl (msg->cc);
190 tc_h->c_addr_prefix = GNUNET_strdup ((const char *) &msg[1]);
191 if (NULL != tc_h->communicator_available_cb)
192 {
193 LOG (GNUNET_ERROR_TYPE_DEBUG, "calling communicator_available_cb()\n");
194 tc_h->communicator_available_cb (tc_h->cb_cls,
195 tc_h,
196 tc_h->c_characteristics,
197 tc_h->c_addr_prefix);
198 }
199 GNUNET_SERVICE_client_continue (client->client);
200 LOG (GNUNET_ERROR_TYPE_DEBUG, "finished communicator_available_cb()\n");
201
202}
203
204
205/**
206 * Incoming message. Test message is well-formed.
207 *
208 * @param cls the client
209 * @param msg the send message that was sent
210 * @return #GNUNET_OK if message is well-formed
211 */
212static int
213check_communicator_backchannel (void *cls,
214 const struct
215 GNUNET_TRANSPORT_CommunicatorBackchannel *msg)
216{
217 // struct TransportClient *tc = cls;
218
219 // if (CT_COMMUNICATOR != tc->type)
220 // {
221 // GNUNET_break (0);
222 // return GNUNET_SYSERR;
223 // }
224 // GNUNET_MQ_check_boxed_message (msg);
225 return GNUNET_OK;
226}
227
228
229/**
230 * @brief Receive an incoming message.
231 *
232 * Pass the message to the client.
233 *
234 * @param cls Closure - communicator handle
235 * @param msg Message
236 */
237static void
238handle_communicator_backchannel (void *cls,
239 const struct
240 GNUNET_TRANSPORT_CommunicatorBackchannel *
241 bc_msg)
242{
243 struct MyClient *client = cls;
244 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h =
245 client->tc;
246 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *other_tc_h;
247 struct GNUNET_MessageHeader *msg;
248 msg = (struct GNUNET_MessageHeader *) &bc_msg[1];
249 uint16_t isize = ntohs (msg->size);
250 const char *target_communicator = ((const char *) msg) + isize;
251 struct GNUNET_TRANSPORT_CommunicatorBackchannelIncoming *cbi;
252 struct GNUNET_MQ_Envelope *env;
253
254 LOG (GNUNET_ERROR_TYPE_DEBUG,
255 "Received backchannel message\n");
256 if (tc_h->bc_enabled != GNUNET_YES)
257 {
258 GNUNET_SERVICE_client_continue (client->client);
259 return;
260 }
261 /* Find client providing this communicator */
262 /* Finally, deliver backchannel message to communicator */
263 LOG (GNUNET_ERROR_TYPE_DEBUG,
264 "Delivering backchannel message of type %u to %s\n",
265 ntohs (msg->type),
266 target_communicator);
267 other_tc_h = tc_h->bc_cb (tc_h, msg, (struct
268 GNUNET_PeerIdentity*) &bc_msg->pid);
269 env = GNUNET_MQ_msg_extra (
270 cbi,
271 isize,
272 GNUNET_MESSAGE_TYPE_TRANSPORT_COMMUNICATOR_BACKCHANNEL_INCOMING);
273 cbi->pid = tc_h->peer_id;
274 memcpy (&cbi[1], msg, isize);
275
276
277 GNUNET_MQ_send (other_tc_h->c_mq, env);
278 GNUNET_SERVICE_client_continue (client->client);
279}
280
281
282/**
283 * Address of our peer added. Test message is well-formed.
284 *
285 * @param cls the client
286 * @param aam the send message that was sent
287 * @return #GNUNET_OK if message is well-formed
288 */
289static int
290check_add_address (void *cls,
291 const struct GNUNET_TRANSPORT_AddAddressMessage *msg)
292{
293 // if (CT_COMMUNICATOR != tc->type)
294 // {
295 // GNUNET_break (0);
296 // return GNUNET_SYSERR;
297 // }
298 GNUNET_MQ_check_zero_termination (msg);
299 return GNUNET_OK;
300}
301
302
303/**
304 * @brief The communicator informs about an address.
305 *
306 * Store address and call client callback.
307 *
308 * @param cls Closure - communicator handle
309 * @param msg Message
310 */
311static void
312handle_add_address (void *cls,
313 const struct GNUNET_TRANSPORT_AddAddressMessage *msg)
314{
315 struct MyClient *client = cls;
316 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h =
317 client->tc;
318 uint16_t size;
319 size = ntohs (msg->header.size) - sizeof(*msg);
320 if (0 == size)
321 return; /* receive-only communicator */
322 LOG (GNUNET_ERROR_TYPE_DEBUG, "received add address cb %u\n", size);
323 tc_h->c_address = GNUNET_strdup ((const char *) &msg[1]);
324 if (NULL != tc_h->add_address_cb)
325 {
326 LOG (GNUNET_ERROR_TYPE_DEBUG, "calling add_address_cb()\n");
327 tc_h->add_address_cb (tc_h->cb_cls,
328 tc_h,
329 tc_h->c_address,
330 GNUNET_TIME_relative_ntoh (msg->expiration),
331 msg->aid,
332 ntohl (msg->nt));
333 }
334 GNUNET_SERVICE_client_continue (client->client);
335}
336
337
338/**
339 * Incoming message. Test message is well-formed.
340 *
341 * @param cls the client
342 * @param msg the send message that was sent
343 * @return #GNUNET_OK if message is well-formed
344 */
345static int
346check_incoming_msg (void *cls,
347 const struct GNUNET_TRANSPORT_IncomingMessage *msg)
348{
349 // struct TransportClient *tc = cls;
350
351 // if (CT_COMMUNICATOR != tc->type)
352 // {
353 // GNUNET_break (0);
354 // return GNUNET_SYSERR;
355 // }
356 GNUNET_MQ_check_boxed_message (msg);
357 return GNUNET_OK;
358}
359
360
361/**
362 * @brief Receive an incoming message.
363 *
364 * Pass the message to the client.
365 *
366 * @param cls Closure - communicator handle
367 * @param msg Message
368 */
369static void
370handle_incoming_msg (void *cls,
371 const struct GNUNET_TRANSPORT_IncomingMessage *inc_msg)
372{
373 struct MyClient *client = cls;
374 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h =
375 client->tc;
376 struct GNUNET_MessageHeader *msg;
377 msg = (struct GNUNET_MessageHeader *) &inc_msg[1];
378 size_t payload_len = ntohs (msg->size) - sizeof (struct
379 GNUNET_MessageHeader);
380 if (NULL != tc_h->incoming_msg_cb)
381 {
382 tc_h->incoming_msg_cb (tc_h->cb_cls,
383 tc_h,
384 (char*) &msg[1],
385 payload_len);
386 }
387 else
388 {
389 LOG (GNUNET_ERROR_TYPE_WARNING,
390 "Incoming message from communicator but no handler!\n");
391 }
392 if (GNUNET_YES == ntohl (inc_msg->fc_on))
393 {
394 /* send ACK when done to communicator for flow control! */
395 struct GNUNET_MQ_Envelope *env;
396 struct GNUNET_TRANSPORT_IncomingMessageAck *ack;
397
398 env = GNUNET_MQ_msg (ack, GNUNET_MESSAGE_TYPE_TRANSPORT_INCOMING_MSG_ACK);
399 GNUNET_assert (NULL != env);
400 ack->reserved = htonl (0);
401 ack->fc_id = inc_msg->fc_id;
402 ack->sender = inc_msg->sender;
403 GNUNET_MQ_send (tc_h->c_mq, env);
404 }
405
406 GNUNET_SERVICE_client_continue (client->client);
407}
408
409
410/**
411 * @brief Communicator informs that it tries to establish requested queue
412 *
413 * @param cls Closure - communicator handle
414 * @param msg Message
415 */
416static void
417handle_queue_create_ok (void *cls,
418 const struct GNUNET_TRANSPORT_CreateQueueResponse *msg)
419{
420 struct MyClient *client = cls;
421 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h =
422 client->tc;
423
424 if (NULL != tc_h->queue_create_reply_cb)
425 {
426 tc_h->queue_create_reply_cb (tc_h->cb_cls, tc_h, GNUNET_YES);
427 }
428 GNUNET_SERVICE_client_continue (client->client);
429}
430
431
432/**
433 * @brief Communicator informs that it wont try establishing requested queue.
434 *
435 * It will not do so probably because the address is bougus (see comment to
436 * #GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_CREATE_FAIL)
437 *
438 * @param cls Closure - communicator handle
439 * @param msg Message
440 */
441static void
442handle_queue_create_fail (
443 void *cls,
444 const struct GNUNET_TRANSPORT_CreateQueueResponse *msg)
445{
446 struct MyClient *client = cls;
447 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h =
448 client->tc;
449
450 if (NULL != tc_h->queue_create_reply_cb)
451 {
452 tc_h->queue_create_reply_cb (tc_h->cb_cls, tc_h, GNUNET_NO);
453 }
454 GNUNET_SERVICE_client_continue (client->client);
455}
456
457
458/**
459 * New queue became available. Check message.
460 *
461 * @param cls the client
462 * @param aqm the send message that was sent
463 */
464static int
465check_add_queue_message (void *cls,
466 const struct GNUNET_TRANSPORT_AddQueueMessage *aqm)
467{
468 GNUNET_MQ_check_zero_termination (aqm);
469 return GNUNET_OK;
470}
471
472
473/**
474 * @brief Handle new queue
475 *
476 * Store context and call client callback.
477 *
478 * @param cls Closure - communicator handle
479 * @param msg Message struct
480 */
481static void
482handle_add_queue_message (void *cls,
483 const struct GNUNET_TRANSPORT_AddQueueMessage *msg)
484{
485 struct MyClient *client = cls;
486 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h =
487 client->tc;
488 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *tc_queue;
489
490 LOG (GNUNET_ERROR_TYPE_DEBUG,
491 "Got queue with ID %u\n", msg->qid);
492 for (tc_queue = tc_h->queue_head; NULL != tc_queue; tc_queue = tc_queue->next)
493 {
494 if (tc_queue->qid == msg->qid)
495 break;
496 }
497 if (NULL == tc_queue)
498 {
499 tc_queue =
500 GNUNET_new (struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue);
501 tc_queue->tc_h = tc_h;
502 tc_queue->qid = msg->qid;
503 tc_queue->peer_id = msg->receiver;
504 GNUNET_CONTAINER_DLL_insert (tc_h->queue_head, tc_h->queue_tail, tc_queue);
505 }
506 GNUNET_assert (tc_queue->qid == msg->qid);
507 GNUNET_assert (0 == GNUNET_memcmp (&tc_queue->peer_id, &msg->receiver));
508 tc_queue->nt = msg->nt;
509 tc_queue->mtu = ntohl (msg->mtu);
510 tc_queue->cs = msg->cs;
511 tc_queue->priority = ntohl (msg->priority);
512 tc_queue->q_len = GNUNET_ntohll (msg->q_len);
513 if (NULL != tc_h->add_queue_cb)
514 {
515 tc_h->add_queue_cb (tc_h->cb_cls, tc_h, tc_queue, tc_queue->mtu);
516 }
517 GNUNET_SERVICE_client_continue (client->client);
518}
519
520
521/**
522 * @brief Handle new queue
523 *
524 * Store context and call client callback.
525 *
526 * @param cls Closure - communicator handle
527 * @param msg Message struct
528 */
529static void
530handle_update_queue_message (void *cls,
531 const struct
532 GNUNET_TRANSPORT_UpdateQueueMessage *msg)
533{
534 struct MyClient *client = cls;
535 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h =
536 client->tc;
537 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *tc_queue;
538
539 LOG (GNUNET_ERROR_TYPE_DEBUG,
540 "Received queue update message for %u with q_len %" PRIu64 "\n",
541 msg->qid, GNUNET_ntohll (msg->q_len));
542 tc_queue = tc_h->queue_head;
543 if (NULL != tc_queue)
544 {
545 while (tc_queue->qid != msg->qid)
546 {
547 tc_queue = tc_queue->next;
548 }
549 }
550 GNUNET_assert (tc_queue->qid == msg->qid);
551 GNUNET_assert (0 == GNUNET_memcmp (&tc_queue->peer_id, &msg->receiver));
552 tc_queue->nt = msg->nt;
553 tc_queue->mtu = ntohl (msg->mtu);
554 tc_queue->cs = msg->cs;
555 tc_queue->priority = ntohl (msg->priority);
556 // Uncomment this for alternativ 1 of backchannel functionality
557 tc_queue->q_len += GNUNET_ntohll (msg->q_len);
558 // Until here for alternativ 1
559 // Uncomment this for alternativ 2 of backchannel functionality
560 // tc_queue->q_len = GNUNET_ntohll (msg->q_len);
561 // Until here for alternativ 2
562 GNUNET_SERVICE_client_continue (client->client);
563}
564
565
566/**
567 * @brief Shut down the service
568 *
569 * @param cls Closure - Handle to the service
570 */
571static void
572shutdown_service (void *cls)
573{
574 struct GNUNET_SERVICE_Handle *h = cls;
575
576 LOG (GNUNET_ERROR_TYPE_DEBUG,
577 "Shutting down service!\n");
578
579 GNUNET_SERVICE_stop (h);
580}
581
582
583/**
584 * @brief Callback called when new Client (Communicator) connects
585 *
586 * @param cls Closure - TransporCommmunicator Handle
587 * @param client Client
588 * @param mq Messagequeue
589 *
590 * @return TransportCommunicator Handle
591 */
592static void *
593connect_cb (void *cls,
594 struct GNUNET_SERVICE_Client *client,
595 struct GNUNET_MQ_Handle *mq)
596{
597 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = cls;
598 struct MyClient *new_c;
599
600 LOG (GNUNET_ERROR_TYPE_DEBUG, "Client %p connected to %p.\n",
601 client, tc_h);
602 new_c = GNUNET_new (struct MyClient);
603 new_c->client = client;
604 new_c->c_mq = mq;
605 new_c->tc = tc_h;
606 GNUNET_CONTAINER_DLL_insert (tc_h->client_head,
607 tc_h->client_tail,
608 new_c);
609
610 if (NULL == tc_h->queue_head)
611 return new_c;
612 /* Iterate over queues. They are yet to be opened. Request opening. */
613 for (struct
614 GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *tc_queue_iter =
615 tc_h->queue_head;
616 NULL != tc_queue_iter;
617 tc_queue_iter = tc_queue_iter->next)
618 {
619 if (NULL == tc_queue_iter->open_queue_env)
620 continue;
621 /* Send the previously created mq envelope to request the creation of the
622 * queue. */
623 GNUNET_MQ_send (tc_h->c_mq,
624 tc_queue_iter->open_queue_env);
625 tc_queue_iter->open_queue_env = NULL;
626 }
627 return new_c;
628}
629
630
631/**
632 * @brief Callback called when Client disconnects
633 *
634 * @param cls Closure - TransportCommunicator Handle
635 * @param client Client
636 * @param internal_cls TransporCommmunicator Handle
637 */
638static void
639disconnect_cb (void *cls,
640 struct GNUNET_SERVICE_Client *client,
641 void *internal_cls)
642{
643 struct MyClient *cl = cls;
644 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = cls;
645
646 for (cl = tc_h->client_head; NULL != cl; cl = cl->next)
647 {
648 if (cl->client != client)
649 continue;
650 GNUNET_CONTAINER_DLL_remove (tc_h->client_head,
651 tc_h->client_tail,
652 cl);
653 if (cl->c_mq == tc_h->c_mq)
654 tc_h->c_mq = NULL;
655 GNUNET_free (cl);
656 break;
657 }
658 LOG (GNUNET_ERROR_TYPE_DEBUG, "Client disconnected.\n");
659}
660
661
662/**
663 * Message was transmitted. Process the request.
664 *
665 * @param cls the client
666 * @param sma the send message that was sent
667 */
668static void
669handle_send_message_ack (void *cls,
670 const struct GNUNET_TRANSPORT_SendMessageToAck *sma)
671{
672 struct MyClient *client = cls;
673 GNUNET_SERVICE_client_continue (client->client);
674 // NOP
675}
676
677
678/**
679 * @brief Start the communicator part of the transport service
680 *
681 * @param communicator_available Callback to be called when a new communicator
682 * becomes available
683 * @param cfg Configuration
684 */
685static void
686transport_communicator_start (
687 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h)
688{
689 struct GNUNET_MQ_MessageHandler mh[] = {
690 GNUNET_MQ_hd_var_size (communicator_available,
691 GNUNET_MESSAGE_TYPE_TRANSPORT_NEW_COMMUNICATOR,
692 struct GNUNET_TRANSPORT_CommunicatorAvailableMessage,
693 tc_h),
694 GNUNET_MQ_hd_var_size (communicator_backchannel,
695 GNUNET_MESSAGE_TYPE_TRANSPORT_COMMUNICATOR_BACKCHANNEL,
696 struct GNUNET_TRANSPORT_CommunicatorBackchannel,
697 tc_h),
698 GNUNET_MQ_hd_var_size (add_address,
699 GNUNET_MESSAGE_TYPE_TRANSPORT_ADD_ADDRESS,
700 struct GNUNET_TRANSPORT_AddAddressMessage,
701 tc_h),
702 // GNUNET_MQ_hd_fixed_size (del_address,
703 // GNUNET_MESSAGE_TYPE_TRANSPORT_DEL_ADDRESS,
704 // struct GNUNET_TRANSPORT_DelAddressMessage,
705 // NULL),
706 GNUNET_MQ_hd_var_size (incoming_msg,
707 GNUNET_MESSAGE_TYPE_TRANSPORT_INCOMING_MSG,
708 struct GNUNET_TRANSPORT_IncomingMessage,
709 tc_h),
710 GNUNET_MQ_hd_fixed_size (queue_create_ok,
711 GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_CREATE_OK,
712 struct GNUNET_TRANSPORT_CreateQueueResponse,
713 tc_h),
714 GNUNET_MQ_hd_fixed_size (queue_create_fail,
715 GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_CREATE_FAIL,
716 struct GNUNET_TRANSPORT_CreateQueueResponse,
717 tc_h),
718 GNUNET_MQ_hd_var_size (add_queue_message,
719 GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_SETUP,
720 struct GNUNET_TRANSPORT_AddQueueMessage,
721 tc_h),
722 GNUNET_MQ_hd_fixed_size (update_queue_message,
723 GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_UPDATE,
724 struct GNUNET_TRANSPORT_UpdateQueueMessage,
725 tc_h),
726 // GNUNET_MQ_hd_fixed_size (del_queue_message,
727 // GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_TEARDOWN,
728 // struct GNUNET_TRANSPORT_DelQueueMessage,
729 // NULL),
730 GNUNET_MQ_hd_fixed_size (send_message_ack,
731 GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_MSG_ACK,
732 struct GNUNET_TRANSPORT_SendMessageToAck,
733 tc_h),
734 GNUNET_MQ_handler_end ()
735 };
736
737
738 tc_h->sh = GNUNET_SERVICE_start ("transport",
739 tc_h->cfg,
740 &connect_cb,
741 &disconnect_cb,
742 tc_h,
743 mh);
744 GNUNET_assert (NULL != tc_h->sh);
745}
746
747
748/**
749 * @brief Task run at shutdown to kill communicator and clean up
750 *
751 * @param cls Closure - Process of communicator
752 */
753static void
754shutdown_process (struct GNUNET_OS_Process *proc)
755{
756 if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
757 {
758 LOG (GNUNET_ERROR_TYPE_WARNING,
759 "Error shutting down process with SIGERM, trying SIGKILL\n");
760 if (0 != GNUNET_OS_process_kill (proc, SIGKILL))
761 {
762 LOG (GNUNET_ERROR_TYPE_ERROR,
763 "Error shutting down process with SIGERM and SIGKILL\n");
764 }
765 }
766 GNUNET_OS_process_destroy (proc);
767}
768
769static void
770shutdown_peerstore (void *cls)
771{
772 struct GNUNET_OS_Process *proc = cls;
773 shutdown_process (proc);
774}
775
776static void
777shutdown_communicator (void *cls)
778{
779 struct GNUNET_OS_Process *proc = cls;
780 shutdown_process (proc);
781}
782
783
784/**
785 * @brief Start the communicator
786 *
787 * @param cfgname Name of the communicator
788 */
789static void
790communicator_start (
791 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h,
792 const char *binary_name)
793{
794 char *binary;
795 char *loprefix;
796 char *section_name;
797
798 LOG (GNUNET_ERROR_TYPE_DEBUG, "communicator_start\n");
799
800 section_name = strchr (binary_name, '-');
801 section_name++;
802
803 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (tc_h->cfg,
804 section_name,
805 "PREFIX",
806 &loprefix))
807 loprefix = GNUNET_strdup ("");
808
809
810 binary = GNUNET_OS_get_libexec_binary_path (binary_name);
811 tc_h->c_proc = GNUNET_OS_start_process_s (GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
812 NULL,
813 loprefix,
814 binary,
815 binary_name,
816 "-c",
817 tc_h->cfg_filename,
818 NULL);
819 if (NULL == tc_h->c_proc)
820 {
821 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start communicator!");
822 return;
823 }
824 LOG (GNUNET_ERROR_TYPE_INFO, "started communicator\n");
825 GNUNET_free (binary);
826}
827
828
829/**
830 * @brief Task run at shutdown to kill communicator and clean up
831 *
832 * @param cls Closure - Process of communicator
833 */
834static void
835shutdown_nat (void *cls)
836{
837 struct GNUNET_OS_Process *proc = cls;
838 shutdown_process (proc);
839}
840
841
842/**
843 * @brief Task run at shutdown to kill the resolver process
844 *
845 * @param cls Closure - Process of communicator
846 */
847static void
848shutdown_resolver (void *cls)
849{
850 struct GNUNET_OS_Process *proc = cls;
851 shutdown_process (proc);
852}
853
854
855static void
856resolver_start (struct
857 GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h)
858{
859 char *binary;
860
861 LOG (GNUNET_ERROR_TYPE_DEBUG, "resolver_start\n");
862 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
863 tc_h->resolver_proc = GNUNET_OS_start_process (
864 GNUNET_OS_INHERIT_STD_OUT_AND_ERR
865 | GNUNET_OS_USE_PIPE_CONTROL,
866 NULL,
867 NULL,
868 NULL,
869 binary,
870 "gnunet-service-resolver",
871 "-c",
872 tc_h->cfg_filename,
873 NULL);
874 if (NULL == tc_h->resolver_proc)
875 {
876 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start resolver service!");
877 return;
878 }
879 LOG (GNUNET_ERROR_TYPE_INFO, "started resolver service\n");
880 GNUNET_free (binary);
881
882}
883
884
885/**
886 * @brief Start Peerstore
887 *
888 */
889static void
890peerstore_start (
891 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h)
892{
893 char *binary;
894
895 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-peerstore");
896 tc_h->ps_proc = GNUNET_OS_start_process (GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
897 NULL,
898 NULL,
899 NULL,
900 binary,
901 "gnunet-service-peerstore",
902 "-c",
903 tc_h->cfg_filename,
904 NULL);
905 if (NULL == tc_h->ps_proc)
906 {
907 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start Peerstore!");
908 return;
909 }
910 LOG (GNUNET_ERROR_TYPE_INFO, "started Peerstore\n");
911 GNUNET_free (binary);
912}
913
914/**
915 * @brief Start NAT
916 *
917 */
918static void
919nat_start (
920 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h)
921{
922 char *binary;
923
924 LOG (GNUNET_ERROR_TYPE_DEBUG, "nat_start\n");
925 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-nat");
926 tc_h->nat_proc = GNUNET_OS_start_process (GNUNET_OS_INHERIT_STD_OUT_AND_ERR
927 | GNUNET_OS_USE_PIPE_CONTROL,
928 NULL,
929 NULL,
930 NULL,
931 binary,
932 "gnunet-service-nat",
933 "-c",
934 tc_h->cfg_filename,
935 NULL);
936 if (NULL == tc_h->nat_proc)
937 {
938 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start NAT!");
939 return;
940 }
941 LOG (GNUNET_ERROR_TYPE_INFO, "started NAT\n");
942 GNUNET_free (binary);
943}
944
945
946/**
947 * @brief Start communicator part of transport service and communicator
948 *
949 * @param service_name Name of the service
950 * @param cfg Configuration handle
951 * @param communicator_available_cb Callback that is called when a new
952 * @param add_address_cb Callback that is called when a new
953 * communicator becomes available
954 * @param cb_cls Closure to @a communicator_available_cb and @a
955 *
956 * @return Handle to the communicator duo
957 */
958struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *
959GNUNET_TRANSPORT_TESTING_transport_communicator_service_start (
960 const char *service_name,
961 const char *binary_name,
962 const char *cfg_filename,
963 const struct GNUNET_PeerIdentity *peer_id,
964 GNUNET_TRANSPORT_TESTING_CommunicatorAvailableCallback
965 communicator_available_cb,
966 GNUNET_TRANSPORT_TESTING_AddAddressCallback add_address_cb,
967 GNUNET_TRANSPORT_TESTING_QueueCreateReplyCallback queue_create_reply_cb,
968 GNUNET_TRANSPORT_TESTING_AddQueueCallback add_queue_cb,
969 GNUNET_TRANSPORT_TESTING_IncomingMessageCallback incoming_message_cb,
970 GNUNET_TRANSPORT_TESTING_BackchannelCallback bc_cb,
971 void *cb_cls)
972{
973 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h;
974
975 LOG (GNUNET_ERROR_TYPE_DEBUG,
976 "Starting new transport/communicator combo with config %s\n",
977 cfg_filename);
978 tc_h =
979 GNUNET_new (struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle);
980 tc_h->cfg_filename = GNUNET_strdup (cfg_filename);
981 tc_h->cfg = GNUNET_CONFIGURATION_create ();
982 if ((GNUNET_SYSERR == GNUNET_CONFIGURATION_load (tc_h->cfg, cfg_filename)))
983 {
984 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
985 _ ("Malformed configuration file `%s', exit ...\n"),
986 cfg_filename);
987 GNUNET_free (tc_h->cfg_filename);
988 GNUNET_CONFIGURATION_destroy (tc_h->cfg);
989 GNUNET_free (tc_h);
990 return NULL;
991 }
992 tc_h->bc_enabled = GNUNET_CONFIGURATION_get_value_yesno (tc_h->cfg,
993 "communicator-test",
994 "BACKCHANNEL_ENABLED");
995 tc_h->communicator_available_cb = communicator_available_cb;
996 tc_h->add_address_cb = add_address_cb;
997 tc_h->queue_create_reply_cb = queue_create_reply_cb;
998 tc_h->add_queue_cb = add_queue_cb;
999 tc_h->incoming_msg_cb = incoming_message_cb;
1000 tc_h->bc_cb = bc_cb;
1001 tc_h->peer_id = *peer_id;
1002 tc_h->cb_cls = cb_cls;
1003
1004 /* Start communicator part of service */
1005 transport_communicator_start (tc_h);
1006 /* Start NAT */
1007 nat_start (tc_h);
1008 /* Start resolver service */
1009 resolver_start (tc_h);
1010 /* Start peerstore service */
1011 peerstore_start (tc_h);
1012 /* Schedule start communicator */
1013 communicator_start (tc_h,
1014 binary_name);
1015 return tc_h;
1016}
1017
1018
1019void
1020GNUNET_TRANSPORT_TESTING_transport_communicator_service_stop (
1021 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h)
1022{
1023 shutdown_communicator (tc_h->c_proc);
1024 shutdown_service (tc_h->sh);
1025 shutdown_nat (tc_h->nat_proc);
1026 shutdown_resolver (tc_h->resolver_proc);
1027 shutdown_peerstore (tc_h->ps_proc);
1028 GNUNET_CONFIGURATION_destroy (tc_h->cfg);
1029 GNUNET_free (tc_h);
1030}
1031
1032
1033/**
1034 * @brief Instruct communicator to open a queue
1035 *
1036 * @param tc_h Handle to communicator which shall open queue
1037 * @param peer_id Towards which peer
1038 * @param address For which address
1039 */
1040void
1041GNUNET_TRANSPORT_TESTING_transport_communicator_open_queue (
1042 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h,
1043 const struct GNUNET_PeerIdentity *peer_id,
1044 const char *address)
1045{
1046 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *tc_queue;
1047 static uint32_t idgen;
1048 char *prefix;
1049 struct GNUNET_TRANSPORT_CreateQueue *msg;
1050 struct GNUNET_MQ_Envelope *env;
1051 size_t alen;
1052
1053 tc_queue =
1054 GNUNET_new (struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue);
1055 tc_queue->tc_h = tc_h;
1056 prefix = GNUNET_HELLO_address_to_prefix (address);
1057 if (NULL == prefix)
1058 {
1059 GNUNET_break (0); /* We got an invalid address!? */
1060 GNUNET_free (tc_queue);
1061 return;
1062 }
1063 GNUNET_free (prefix);
1064 alen = strlen (address) + 1;
1065 env =
1066 GNUNET_MQ_msg_extra (msg, alen, GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_CREATE);
1067 msg->request_id = htonl (idgen++);
1068 tc_queue->qid = msg->request_id;
1069 msg->receiver = *peer_id;
1070 tc_queue->peer_id = *peer_id;
1071 memcpy (&msg[1], address, alen);
1072 if (NULL != tc_h->c_mq)
1073 {
1074 LOG (GNUNET_ERROR_TYPE_DEBUG,
1075 "Sending queue create immediately\n");
1076 GNUNET_MQ_send (tc_h->c_mq, env);
1077 }
1078 else
1079 {
1080 tc_queue->open_queue_env = env;
1081 }
1082 GNUNET_CONTAINER_DLL_insert (tc_h->queue_head, tc_h->queue_tail, tc_queue);
1083}
1084
1085
1086/**
1087 * @brief Instruct communicator to send data
1088 *
1089 * @param tc_queue The queue to use for sending
1090 * @param cont function to call when done sending
1091 * @param cont_cls closure for @a cont
1092 * @param payload Data to send
1093 * @param payload_size Size of the @a payload
1094 */
1095void
1096GNUNET_TRANSPORT_TESTING_transport_communicator_send
1097 (struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h,
1098 GNUNET_SCHEDULER_TaskCallback cont,
1099 void *cont_cls,
1100 const void *payload,
1101 size_t payload_size)
1102{
1103 struct GNUNET_MessageHeader *mh;
1104 struct GNUNET_TRANSPORT_SendMessageTo *msg;
1105 struct GNUNET_MQ_Envelope *env;
1106 size_t inbox_size;
1107 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *tc_queue;
1108 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *tc_queue_tmp;
1109
1110 tc_queue = NULL;
1111 for (tc_queue_tmp = tc_h->queue_head;
1112 NULL != tc_queue_tmp;
1113 tc_queue_tmp = tc_queue_tmp->next)
1114 {
1115 if (tc_queue_tmp->q_len <= 0)
1116 continue;
1117 if (NULL == tc_queue)
1118 {
1119 LOG (GNUNET_ERROR_TYPE_DEBUG,
1120 "Selecting queue with prio %u, len %" PRIu64 " and MTU %u\n",
1121 tc_queue_tmp->priority,
1122 tc_queue_tmp->q_len,
1123 tc_queue_tmp->mtu);
1124 tc_queue = tc_queue_tmp;
1125 continue;
1126 }
1127 if (tc_queue->priority < tc_queue_tmp->priority)
1128 {
1129 LOG (GNUNET_ERROR_TYPE_DEBUG,
1130 "Selecting queue with prio %u, len %" PRIu64 " and MTU %u\n",
1131 tc_queue_tmp->priority,
1132 tc_queue_tmp->q_len,
1133 tc_queue_tmp->mtu);
1134 tc_queue = tc_queue_tmp;
1135 }
1136 }
1137 GNUNET_assert (NULL != tc_queue);
1138 // Uncomment this for alternativ 1 of backchannel functionality
1139 if (tc_queue->q_len != GNUNET_TRANSPORT_QUEUE_LENGTH_UNLIMITED)
1140 tc_queue->q_len--;
1141 // Until here for alternativ 1
1142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1143 "Sending message\n");
1144 inbox_size = sizeof (struct GNUNET_MessageHeader) + payload_size;
1145 env = GNUNET_MQ_msg_extra (msg,
1146 inbox_size,
1147 GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_MSG);
1148 GNUNET_assert (NULL != env);
1149 msg->qid = htonl (tc_queue->qid);
1150 msg->mid = tc_queue->mid++;
1151 msg->receiver = tc_queue->peer_id;
1152 mh = (struct GNUNET_MessageHeader *) &msg[1];
1153 mh->size = htons (inbox_size);
1154 mh->type = GNUNET_MESSAGE_TYPE_DUMMY;
1155 memcpy (&mh[1],
1156 payload,
1157 payload_size);
1158 if (NULL != cont)
1159 GNUNET_MQ_notify_sent (env,
1160 cont,
1161 cont_cls);
1162 GNUNET_MQ_send (tc_queue->tc_h->c_mq,
1163 env);
1164}
diff --git a/src/transport/transport-testing-communicator.h b/src/transport/transport-testing-communicator.h
new file mode 100644
index 000000000..693e61515
--- /dev/null
+++ b/src/transport/transport-testing-communicator.h
@@ -0,0 +1,355 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2019 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21/**
22 * @file transport/transport-testing-communicator.h
23 * @brief functions and structures related to testing-tng
24 * @author Christian Grothoff
25 * @author Julius Bünger
26 */
27#include "platform.h"
28#include "gnunet_util_lib.h"
29#include "gnunet_ats_transport_service.h"
30#include "transport.h"
31
32/**
33 * @brief Queue of a communicator and some context
34 */
35struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue;
36
37
38/**
39 * @brief Handle/Context to a single transmission
40 */
41struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorTransmission;
42
43/**
44 * @brief Function signature for callbacks that are called when new
45 * backchannel message arrived
46 *
47 * @param cls Closure
48 * @param msg Backchannel message
49 * @param pid Target peer
50 */
51typedef struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *
52(*GNUNET_TRANSPORT_TESTING_BackchannelCallback)(void *cls,
53 struct GNUNET_MessageHeader *msg,
54 struct GNUNET_PeerIdentity *pid);
55
56
57/**
58 * @brief Function signature for callbacks that are called when new
59 * communicators become available
60 *
61 * @param cls Closure
62 * @param tc_h Communicator handle
63 * @param cc Characteristics of communicator
64 * @param address_prefix Prefix of the address
65 */
66typedef void
67(*GNUNET_TRANSPORT_TESTING_CommunicatorAvailableCallback)(void *cls,
68 struct
69 GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle
70 *tc_h,
71 enum
72 GNUNET_TRANSPORT_CommunicatorCharacteristics
73 cc,
74 char *address_prefix);
75
76
77/**
78 * @brief Receive information about the address of a communicator.
79 *
80 * @param cls Closure
81 * @param tc_h Communicator handle
82 * @param address Address represented as string
83 * @param expiration Expiration
84 * @param aid Aid
85 * @param nt Network Type
86 */
87typedef void
88(*GNUNET_TRANSPORT_TESTING_AddAddressCallback)(void *cls,
89 struct
90 GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle
91 *tc_h,
92 const char *address,
93 struct GNUNET_TIME_Relative
94 expiration,
95 uint32_t aid,
96 enum GNUNET_NetworkType nt);
97
98
99/**
100 * @brief Get informed about the success of a queue request.
101 *
102 * @param cls Closure
103 * @param tc_h Communicator handle
104 * @param will_try #GNUNET_YES if communicator will try to create queue
105 */
106typedef void
107(*GNUNET_TRANSPORT_TESTING_QueueCreateReplyCallback)(void *cls,
108 struct
109 GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle
110 *tc_h,
111 int will_try);
112
113
114/**
115 * @brief Handle opening of queue
116 *
117 * @param cls Closure
118 * @param tc_h Communicator handle
119 * @param tc_queue Handle to newly opened queue
120 */
121typedef void
122(*GNUNET_TRANSPORT_TESTING_AddQueueCallback)(void *cls,
123 struct
124 GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle
125 *tc_h,
126 struct
127 GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue
128 *tc_queue,
129 size_t mtu);
130
131
132/**
133 * @brief Handle an incoming message
134 *
135 * @param cls Closure
136 * @param tc_h Handle to the receiving communicator
137 * @param msg Received message
138 */
139typedef void
140(*GNUNET_TRANSPORT_TESTING_IncomingMessageCallback)(void *cls,
141 struct
142 GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle
143 *tc_h,
144 const char*payload,
145 size_t payload_len);
146
147/**
148 * @brief Handle to a transport communicator
149 */
150struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle
151{
152 /**
153 * Clients
154 */
155 struct MyClient *client_head;
156 struct MyClient *client_tail;
157
158 /**
159 * @brief Handle to the client
160 */
161 struct GNUNET_MQ_Handle *c_mq;
162
163 /**
164 * @brief Handle to the configuration
165 */
166 struct GNUNET_CONFIGURATION_Handle *cfg;
167
168 /**
169 * @brief File name of configuration file
170 */
171 char *cfg_filename;
172
173 struct GNUNET_PeerIdentity peer_id;
174
175 /**
176 * @brief Handle to the transport service
177 */
178 struct GNUNET_SERVICE_Handle *tsh;
179
180 /**
181 * @brief Task that will be run on shutdown to stop and clean transport
182 * service
183 */
184 struct GNUNET_SCHEDULER_Task *ts_shutdown_task;
185
186
187 /**
188 * @brief Process of the communicator
189 */
190 struct GNUNET_OS_Process *c_proc;
191
192 /**
193 * NAT process
194 */
195 struct GNUNET_OS_Process *nat_proc;
196
197 /**
198 * resolver service process
199 */
200 struct GNUNET_OS_Process *resolver_proc;
201
202 /**
203 * peerstore service process
204 */
205 struct GNUNET_OS_Process *ps_proc;
206
207 /**
208 * @brief Task that will be run on shutdown to stop and clean communicator
209 */
210 struct GNUNET_SCHEDULER_Task *c_shutdown_task;
211
212 /**
213 * @brief Characteristics of the communicator
214 */
215 enum GNUNET_TRANSPORT_CommunicatorCharacteristics c_characteristics;
216
217 /**
218 * @brief Specifies supported addresses
219 */
220 char *c_addr_prefix;
221
222 /**
223 * @brief Specifies supported addresses
224 */
225 char *c_address;
226
227 /**
228 * @brief Head of the DLL of queues associated with this communicator
229 */
230 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *queue_head;
231
232 /**
233 * @brief Tail of the DLL of queues associated with this communicator
234 */
235 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *queue_tail;
236
237 /* Callbacks + Closures */
238 /**
239 * @brief Callback called when a new communicator connects
240 */
241 GNUNET_TRANSPORT_TESTING_CommunicatorAvailableCallback
242 communicator_available_cb;
243
244 /**
245 * @brief Callback called when a new communicator connects
246 */
247 GNUNET_TRANSPORT_TESTING_AddAddressCallback add_address_cb;
248
249 /**
250 * @brief Callback called when a new communicator connects
251 */
252 GNUNET_TRANSPORT_TESTING_QueueCreateReplyCallback queue_create_reply_cb;
253
254 /**
255 * @brief Callback called when a new communicator connects
256 */
257 GNUNET_TRANSPORT_TESTING_AddQueueCallback add_queue_cb;
258
259 /**
260 * @brief Callback called when a new communicator connects
261 */
262 GNUNET_TRANSPORT_TESTING_IncomingMessageCallback incoming_msg_cb;
263
264 /**
265 * @brief Backchannel callback
266 */
267 GNUNET_TRANSPORT_TESTING_BackchannelCallback bc_cb;
268
269 /**
270 * Our service handle
271 */
272 struct GNUNET_SERVICE_Handle *sh;
273
274 /**
275 * @brief Closure to the callback
276 */
277 void *cb_cls;
278
279 /**
280 * Backchannel supported
281 */
282 int bc_enabled;
283};
284
285/**
286 * @brief Start communicator part of transport service and communicator
287 *
288 * @param service_name Name of the service
289 * @param cfg Configuration handle
290 * @param communicator_available Callback that is called when a new
291 * communicator becomes available
292 * @param add_address_cb Callback handling new addresses
293 * @param queue_create_reply_cb Callback handling success of queue requests
294 * @param add_queue_cb Callback handling freshly created queues
295 * @param incoming_message_cb Callback handling incoming messages
296 * @param cb_cls Closure to @p communicator_available
297 *
298 * @return Handle to the communicator duo
299 */
300struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *
301GNUNET_TRANSPORT_TESTING_transport_communicator_service_start (
302 const char *service_name,
303 const char *binary_name,
304 const char *cfg_filename,
305 const struct GNUNET_PeerIdentity *peer_id,
306 GNUNET_TRANSPORT_TESTING_CommunicatorAvailableCallback
307 communicator_available_cb,
308 GNUNET_TRANSPORT_TESTING_AddAddressCallback add_address_cb,
309 GNUNET_TRANSPORT_TESTING_QueueCreateReplyCallback queue_create_reply_cb,
310 GNUNET_TRANSPORT_TESTING_AddQueueCallback add_queue_cb,
311 GNUNET_TRANSPORT_TESTING_IncomingMessageCallback incoming_message_cb,
312 GNUNET_TRANSPORT_TESTING_BackchannelCallback bc_cb,
313 void *cb_cls);
314
315
316void
317GNUNET_TRANSPORT_TESTING_transport_communicator_service_stop (
318 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h);
319
320
321/**
322 * @brief Instruct communicator to open a queue
323 *
324 * @param tc_h Handle to communicator which shall open queue
325 * @param peer_id Towards which peer
326 * @param address For which address
327 */
328void
329GNUNET_TRANSPORT_TESTING_transport_communicator_open_queue (struct
330 GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle
331 *tc_h,
332 const struct
333 GNUNET_PeerIdentity
334 *peer_id,
335 const char *address);
336
337
338/**
339 * @brief Instruct communicator to send data
340 *
341 * @param tc_queue The queue to use for sending
342 * @param cont function to call when done sending
343 * @param cont_cls closure for @a cont
344 * @param payload Data to send
345 * @param payload_size Size of the @a payload
346 */
347void
348GNUNET_TRANSPORT_TESTING_transport_communicator_send (struct
349 GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle
350 *tc_h,
351 GNUNET_SCHEDULER_TaskCallback
352 cont,
353 void *cont_cls,
354 const void *payload,
355 size_t payload_size);
diff --git a/src/transport/transport-testing2.c b/src/transport/transport-testing2.c
index 70f68d3f8..ca338c52f 100644
--- a/src/transport/transport-testing2.c
+++ b/src/transport/transport-testing2.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 Copyright (C) 2019 GNUnet e.V. 3 Copyright (C) 2006, 2009, 2015, 2016 GNUnet e.V.
4 4
5 GNUnet is free software: you can redistribute it and/or modify it 5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published 6 under the terms of the GNU Affero General Public License as published
@@ -17,1194 +17,907 @@
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19 */ 19 */
20
21/** 20/**
22 * @file transport/transport-testing2.c 21 * @file transport-testing.c
23 * @brief functions related to testing-tng 22 * @brief testing lib for transport service
23 * @author Matthias Wachs
24 * @author Christian Grothoff 24 * @author Christian Grothoff
25 * @author Julius Bünger
26 */ 25 */
27#include "platform.h"
28#include "gnunet_util_lib.h"
29#include "gnunet_protocols.h"
30#include "gnunet_constants.h"
31#include "transport-testing2.h" 26#include "transport-testing2.h"
32#include "gnunet_ats_transport_service.h"
33#include "gnunet_hello_lib.h"
34#include "gnunet_signatures.h"
35#include "transport.h"
36#include <inttypes.h>
37
38#define LOG(kind, ...) GNUNET_log_from (kind, "transport-testing2", __VA_ARGS__)
39 27
40struct MyClient
41{
42 struct MyClient *prev;
43 struct MyClient *next;
44 /**
45 * @brief Handle to the client
46 */
47 struct GNUNET_SERVICE_Client *client;
48 28
49 /** 29#define LOG(kind, ...) GNUNET_log_from (kind, "transport-testing", __VA_ARGS__)
50 * @brief Handle to the client
51 */
52 struct GNUNET_MQ_Handle *c_mq;
53 30
54 /**
55 * The TCH
56 */
57 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc;
58 31
59}; 32static struct GNUNET_TRANSPORT_TESTING_PeerContext *
60 33find_peer_context (struct GNUNET_TRANSPORT_TESTING_Handle *tth,
61/** 34 const struct GNUNET_PeerIdentity *peer)
62 * @brief Queue of a communicator and some context
63 */
64struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue
65{ 35{
66 /** 36 struct GNUNET_TRANSPORT_TESTING_PeerContext *t;
67 * @brief Handle to the TransportCommunicator
68 */
69 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h;
70
71 /**
72 * @brief Envelope to a message that requests the opening of the queue.
73 *
74 * If the client already requests queue(s), but the communicator is not yet
75 * connected, we cannot send the request to open the queue. Save it until the
76 * communicator becomes available and send it then.
77 */
78 struct GNUNET_MQ_Envelope *open_queue_env;
79
80 /**
81 * @brief Peer ID of the peer on the other side of the queue
82 */
83 struct GNUNET_PeerIdentity peer_id;
84
85 /**
86 * @brief Queue ID
87 */
88 uint32_t qid;
89
90 /**
91 * @brief Current message id
92 */
93 uint64_t mid;
94
95 /**
96 * An `enum GNUNET_NetworkType` in NBO.
97 */
98 uint32_t nt;
99
100 /**
101 * Maximum transmission unit. UINT32_MAX for unlimited.
102 */
103 uint32_t mtu;
104
105 /**
106 * Queue length. UINT64_MAX for unlimited.
107 */
108 uint64_t q_len;
109
110 /**
111 * Queue prio
112 */
113 uint32_t priority;
114
115 /**
116 * An `enum GNUNET_TRANSPORT_ConnectionStatus` in NBO.
117 */
118 uint32_t cs;
119
120 /**
121 * @brief Next element inside a DLL
122 */
123 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *next;
124
125 /**
126 * @brief Previous element inside a DLL
127 */
128 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *prev;
129};
130
131 37
132/** 38 for (t = tth->p_head; NULL != t; t = t->next)
133 * @brief Handle/Context to a single transmission 39 if (0 == memcmp (&t->id,
134 */ 40 peer,
135struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorTransmission 41 sizeof(struct GNUNET_PeerIdentity)))
136{ 42 return t;
137}; 43 return NULL;
138
139
140/**
141 * @brief Check whether incoming msg indicating available communicator is
142 * correct
143 *
144 * @param cls Closure
145 * @param msg Message struct
146 *
147 * @return GNUNET_YES in case message is correct
148 */
149static int
150check_communicator_available (
151 void *cls,
152 const struct GNUNET_TRANSPORT_CommunicatorAvailableMessage *msg)
153{
154 uint16_t size;
155
156 size = ntohs (msg->header.size) - sizeof(*msg);
157 if (0 == size)
158 return GNUNET_OK; /* receive-only communicator */
159 GNUNET_MQ_check_zero_termination (msg);
160 return GNUNET_OK;
161} 44}
162 45
163 46
164/** 47/**
165 * @brief Handle new communicator 48 * Find any connecting context matching the given pair of peers.
166 * 49 *
167 * Store characteristics of communicator, call respective client callback. 50 * @param p1 first peer
168 * 51 * @param p2 second peer
169 * @param cls Closure - communicator handle 52 * @param cb function to call
170 * @param msg Message struct 53 * @param cb_cls closure for @a cb
171 */ 54 */
172static void 55void
173handle_communicator_available ( 56GNUNET_TRANSPORT_TESTING_find_connecting_context (struct
174 void *cls, 57 GNUNET_TRANSPORT_TESTING_PeerContext
175 const struct GNUNET_TRANSPORT_CommunicatorAvailableMessage *msg) 58 *p1,
176{ 59 struct
177 struct MyClient *client = cls; 60 GNUNET_TRANSPORT_TESTING_PeerContext
178 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = 61 *p2,
179 client->tc; 62 GNUNET_TRANSPORT_TESTING_ConnectContextCallback
180 uint16_t size; 63 cb,
181 tc_h->c_mq = client->c_mq; 64 void *cb_cls)
182 65{
183 size = ntohs (msg->header.size) - sizeof(*msg); 66 struct GNUNET_TRANSPORT_TESTING_Handle *tth = p1->tth;
184 if (0 == size) 67 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc;
185 { 68 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ccn;
186 GNUNET_SERVICE_client_continue (client->client); 69
187 return; /* receive-only communicator */ 70 for (cc = tth->cc_head; NULL != cc; cc = ccn)
188 }
189 tc_h->c_characteristics = ntohl (msg->cc);
190 tc_h->c_addr_prefix = GNUNET_strdup ((const char *) &msg[1]);
191 if (NULL != tc_h->communicator_available_cb)
192 { 71 {
193 LOG (GNUNET_ERROR_TYPE_DEBUG, "calling communicator_available_cb()\n"); 72 ccn = cc->next;
194 tc_h->communicator_available_cb (tc_h->cb_cls, 73 if ((cc->p1 == p1) &&
195 tc_h, 74 (cc->p2 == p2))
196 tc_h->c_characteristics, 75 cb (cb_cls,
197 tc_h->c_addr_prefix); 76 cc);
198 } 77 }
199 GNUNET_SERVICE_client_continue (client->client);
200 LOG (GNUNET_ERROR_TYPE_DEBUG, "finished communicator_available_cb()\n");
201
202} 78}
203 79
204 80
205/** 81static void
206 * Incoming message. Test message is well-formed. 82set_p1c (void *cls,
207 * 83 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cx)
208 * @param cls the client
209 * @param msg the send message that was sent
210 * @return #GNUNET_OK if message is well-formed
211 */
212static int
213check_communicator_backchannel (void *cls,
214 const struct
215 GNUNET_TRANSPORT_CommunicatorBackchannel *msg)
216{ 84{
217 // struct TransportClient *tc = cls; 85 int *found = cls;
218 86
219 // if (CT_COMMUNICATOR != tc->type) 87 if (NULL != found)
220 // { 88 *found = GNUNET_YES;
221 // GNUNET_break (0); 89 cx->p1_c = GNUNET_YES;
222 // return GNUNET_SYSERR;
223 // }
224 // GNUNET_MQ_check_boxed_message (msg);
225 return GNUNET_OK;
226} 90}
227 91
228 92
229/**
230 * @brief Receive an incoming message.
231 *
232 * Pass the message to the client.
233 *
234 * @param cls Closure - communicator handle
235 * @param msg Message
236 */
237static void 93static void
238handle_communicator_backchannel (void *cls, 94set_mq (void *cls,
239 const struct 95 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cx)
240 GNUNET_TRANSPORT_CommunicatorBackchannel *
241 bc_msg)
242{ 96{
243 struct MyClient *client = cls; 97 struct GNUNET_MQ_Handle *mq = cls;
244 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h =
245 client->tc;
246 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *other_tc_h;
247 struct GNUNET_MessageHeader *msg;
248 msg = (struct GNUNET_MessageHeader *) &bc_msg[1];
249 uint16_t isize = ntohs (msg->size);
250 const char *target_communicator = ((const char *) msg) + isize;
251 struct GNUNET_TRANSPORT_CommunicatorBackchannelIncoming *cbi;
252 struct GNUNET_MQ_Envelope *env;
253 98
254 LOG (GNUNET_ERROR_TYPE_DEBUG, 99 cx->mq = mq;
255 "Received backchannel message\n");
256 if (tc_h->bc_enabled != GNUNET_YES)
257 {
258 GNUNET_SERVICE_client_continue (client->client);
259 return;
260 }
261 /* Find client providing this communicator */
262 /* Finally, deliver backchannel message to communicator */
263 LOG (GNUNET_ERROR_TYPE_DEBUG,
264 "Delivering backchannel message of type %u to %s\n",
265 ntohs (msg->type),
266 target_communicator);
267 other_tc_h = tc_h->bc_cb (tc_h, msg, (struct
268 GNUNET_PeerIdentity*) &bc_msg->pid);
269 env = GNUNET_MQ_msg_extra (
270 cbi,
271 isize,
272 GNUNET_MESSAGE_TYPE_TRANSPORT_COMMUNICATOR_BACKCHANNEL_INCOMING);
273 cbi->pid = tc_h->peer_id;
274 memcpy (&cbi[1], msg, isize);
275
276
277 GNUNET_MQ_send (other_tc_h->c_mq, env);
278 GNUNET_SERVICE_client_continue (client->client);
279} 100}
280 101
281 102
282/** 103static void
283 * Address of our peer added. Test message is well-formed. 104set_p2c (void *cls,
284 * 105 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cx)
285 * @param cls the client
286 * @param aam the send message that was sent
287 * @return #GNUNET_OK if message is well-formed
288 */
289static int
290check_add_address (void *cls,
291 const struct GNUNET_TRANSPORT_AddAddressMessage *msg)
292{ 106{
293 // if (CT_COMMUNICATOR != tc->type) 107 int *found = cls;
294 // { 108
295 // GNUNET_break (0); 109 if (NULL != found)
296 // return GNUNET_SYSERR; 110 *found = GNUNET_YES;
297 // } 111 cx->p2_c = GNUNET_YES;
298 GNUNET_MQ_check_zero_termination (msg);
299 return GNUNET_OK;
300} 112}
301 113
302 114
303/**
304 * @brief The communicator informs about an address.
305 *
306 * Store address and call client callback.
307 *
308 * @param cls Closure - communicator handle
309 * @param msg Message
310 */
311static void 115static void
312handle_add_address (void *cls, 116clear_p1c (void *cls,
313 const struct GNUNET_TRANSPORT_AddAddressMessage *msg) 117 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cx)
314{ 118{
315 struct MyClient *client = cls; 119 int *found = cls;
316 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = 120
317 client->tc; 121 if (NULL != found)
318 uint16_t size; 122 *found = GNUNET_YES;
319 size = ntohs (msg->header.size) - sizeof(*msg); 123 cx->p1_c = GNUNET_NO;
320 if (0 == size)
321 return; /* receive-only communicator */
322 LOG (GNUNET_ERROR_TYPE_DEBUG, "received add address cb %u\n", size);
323 tc_h->c_address = GNUNET_strdup ((const char *) &msg[1]);
324 if (NULL != tc_h->add_address_cb)
325 {
326 LOG (GNUNET_ERROR_TYPE_DEBUG, "calling add_address_cb()\n");
327 tc_h->add_address_cb (tc_h->cb_cls,
328 tc_h,
329 tc_h->c_address,
330 GNUNET_TIME_relative_ntoh (msg->expiration),
331 msg->aid,
332 ntohl (msg->nt));
333 }
334 GNUNET_SERVICE_client_continue (client->client);
335} 124}
336 125
337 126
338/** 127static void
339 * Incoming message. Test message is well-formed. 128clear_p2c (void *cls,
340 * 129 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cx)
341 * @param cls the client
342 * @param msg the send message that was sent
343 * @return #GNUNET_OK if message is well-formed
344 */
345static int
346check_incoming_msg (void *cls,
347 const struct GNUNET_TRANSPORT_IncomingMessage *msg)
348{ 130{
349 // struct TransportClient *tc = cls; 131 int *found = cls;
350 132
351 // if (CT_COMMUNICATOR != tc->type) 133 if (NULL != found)
352 // { 134 *found = GNUNET_YES;
353 // GNUNET_break (0); 135 cx->p2_c = GNUNET_NO;
354 // return GNUNET_SYSERR;
355 // }
356 GNUNET_MQ_check_boxed_message (msg);
357 return GNUNET_OK;
358} 136}
359 137
360 138
361/** 139static void *
362 * @brief Receive an incoming message. 140notify_connect (void *cls,
363 * 141 const struct GNUNET_PeerIdentity *peer,
364 * Pass the message to the client. 142 struct GNUNET_MQ_Handle *mq)
365 * 143{
366 * @param cls Closure - communicator handle 144 struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
367 * @param msg Message 145 struct GNUNET_TRANSPORT_TESTING_Handle *tth = p->tth;
368 */ 146 char *p2_s;
369static void 147 struct GNUNET_TRANSPORT_TESTING_PeerContext *p2;
370handle_incoming_msg (void *cls, 148 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc;
371 const struct GNUNET_TRANSPORT_IncomingMessage *inc_msg) 149 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ccn;
372{ 150 int found;
373 struct MyClient *client = cls; 151 void *ret;
374 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = 152
375 client->tc; 153 p2 = find_peer_context (p->tth,
376 struct GNUNET_MessageHeader *msg; 154 peer);
377 msg = (struct GNUNET_MessageHeader *) &inc_msg[1]; 155 if (NULL != p->nc)
378 size_t payload_len = ntohs (msg->size) - sizeof (struct 156 ret = p->nc (p->cb_cls,
379 GNUNET_MessageHeader); 157 peer,
380 if (NULL != tc_h->incoming_msg_cb) 158 mq);
159 else
160 ret = NULL;
161
162 if (NULL != p2)
163 GNUNET_asprintf (&p2_s,
164 "%u (`%s')",
165 p2->no,
166 GNUNET_i2s (&p2->id));
167 else
168 GNUNET_asprintf (&p2_s,
169 "`%s'",
170 GNUNET_i2s (peer));
171 LOG (GNUNET_ERROR_TYPE_DEBUG,
172 "Peers %s connected to peer %u (`%s')\n",
173 p2_s,
174 p->no,
175 GNUNET_i2s (&p->id));
176 GNUNET_free (p2_s);
177 /* update flags in connecting contexts */
178 found = GNUNET_NO;
179 GNUNET_TRANSPORT_TESTING_find_connecting_context (p,
180 p2,
181 &set_p1c,
182 &found);
183 if (GNUNET_NO == found)
381 { 184 {
382 tc_h->incoming_msg_cb (tc_h->cb_cls, 185 cc = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_ConnectRequest);
383 tc_h, 186 cc->p1 = p;
384 (char*) &msg[1], 187 cc->p2 = p2;
385 payload_len); 188 cc->p1_c = GNUNET_YES;
189 GNUNET_CONTAINER_DLL_insert (tth->cc_head,
190 tth->cc_tail,
191 cc);
386 } 192 }
387 else 193 found = GNUNET_NO;
194 GNUNET_TRANSPORT_TESTING_find_connecting_context (p2,
195 p,
196 &set_p2c,
197 &found);
198 if (GNUNET_NO == found)
388 { 199 {
389 LOG (GNUNET_ERROR_TYPE_WARNING, 200 cc = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_ConnectRequest);
390 "Incoming message from communicator but no handler!\n"); 201 cc->p1 = p2;
202 cc->p2 = p;
203 cc->p1_c = GNUNET_YES;
204 GNUNET_CONTAINER_DLL_insert (tth->cc_head,
205 tth->cc_tail,
206 cc);
391 } 207 }
392 if (GNUNET_YES == ntohl (inc_msg->fc_on)) 208 GNUNET_TRANSPORT_TESTING_find_connecting_context (p,
209 p2,
210 &set_mq,
211 mq);
212 /* update set connected flag for all requests */
213 for (cc = tth->cc_head; NULL != cc; cc = cc->next)
393 { 214 {
394 /* send ACK when done to communicator for flow control! */ 215 if (GNUNET_YES == cc->connected)
395 struct GNUNET_MQ_Envelope *env; 216 continue;
396 struct GNUNET_TRANSPORT_IncomingMessageAck *ack; 217 if ((GNUNET_YES == cc->p1_c) &&
397 218 (GNUNET_YES == cc->p2_c))
398 env = GNUNET_MQ_msg (ack, GNUNET_MESSAGE_TYPE_TRANSPORT_INCOMING_MSG_ACK); 219 {
399 GNUNET_assert (NULL != env); 220 cc->connected = GNUNET_YES;
400 ack->reserved = htonl (0); 221 /* stop trying to connect */
401 ack->fc_id = inc_msg->fc_id; 222 if (NULL != cc->tct)
402 ack->sender = inc_msg->sender; 223 {
403 GNUNET_MQ_send (tc_h->c_mq, env); 224 GNUNET_SCHEDULER_cancel (cc->tct);
225 cc->tct = NULL;
226 }
227 if (NULL != cc->ats_sh)
228 {
229 GNUNET_ATS_connectivity_suggest_cancel (cc->ats_sh);
230 cc->ats_sh = NULL;
231 }
232 }
404 } 233 }
405 234 /* then notify application */
406 GNUNET_SERVICE_client_continue (client->client); 235 for (cc = tth->cc_head; NULL != cc; cc = ccn)
236 {
237 ccn = cc->next;
238 if ((GNUNET_YES == cc->connected) &&
239 (NULL != cc->cb))
240 {
241 cc->cb (cc->cb_cls);
242 cc->cb = NULL; /* only notify once! */
243 }
244 }
245 return ret;
407} 246}
408 247
409 248
410/** 249/**
411 * @brief Communicator informs that it tries to establish requested queue 250 * Offer the current HELLO of P2 to P1.
412 * 251 *
413 * @param cls Closure - communicator handle 252 * @param cls our `struct GNUNET_TRANSPORT_TESTING_ConnectRequest`
414 * @param msg Message
415 */ 253 */
416static void 254static void
417handle_queue_create_ok (void *cls, 255offer_hello (void *cls);
418 const struct GNUNET_TRANSPORT_CreateQueueResponse *msg) 256
419{
420 struct MyClient *client = cls;
421 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h =
422 client->tc;
423 257
424 if (NULL != tc_h->queue_create_reply_cb) 258static void
259notify_disconnect (void *cls,
260 const struct GNUNET_PeerIdentity *peer,
261 void *handler_cls)
262{
263 struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
264 struct GNUNET_TRANSPORT_TESTING_Handle *tth = p->tth;
265 char *p2_s;
266 /* Find PeerContext */
267 int no = 0;
268 struct GNUNET_TRANSPORT_TESTING_PeerContext *p2 = NULL;
269 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc;
270
271 p2 = find_peer_context (p->tth,
272 peer);
273 no = p->no;
274 if (NULL != p2)
275 GNUNET_asprintf (&p2_s,
276 "%u (`%s')",
277 p2->no,
278 GNUNET_i2s (&p2->id));
279 else
280 GNUNET_asprintf (&p2_s,
281 "`%s'",
282 GNUNET_i2s (peer));
283 LOG (GNUNET_ERROR_TYPE_DEBUG,
284 "Peers %s disconnected from peer %u (`%s')\n",
285 p2_s,
286 no,
287 GNUNET_i2s (&p->id));
288 GNUNET_free (p2_s);
289 /* notify about disconnect */
290 if (NULL != p->nd)
291 p->nd (p->cb_cls,
292 peer,
293 handler_cls);
294 if (NULL == p2)
295 return;
296 /* clear MQ, it is now invalid */
297 GNUNET_TRANSPORT_TESTING_find_connecting_context (p,
298 p2,
299 &set_mq,
300 NULL);
301 /* update set connected flags for all requests */
302 GNUNET_TRANSPORT_TESTING_find_connecting_context (p,
303 p2,
304 &clear_p1c,
305 NULL);
306 GNUNET_TRANSPORT_TESTING_find_connecting_context (p2,
307 p,
308 &clear_p2c,
309 NULL);
310 /* resume connectivity requests as necessary */
311 for (cc = tth->cc_head; NULL != cc; cc = cc->next)
425 { 312 {
426 tc_h->queue_create_reply_cb (tc_h->cb_cls, tc_h, GNUNET_YES); 313 if (GNUNET_NO == cc->connected)
314 continue;
315 if ((GNUNET_YES != cc->p1_c) ||
316 (GNUNET_YES != cc->p2_c))
317 {
318 cc->connected = GNUNET_NO;
319 /* start trying to connect */
320 if (NULL == cc->tct)
321 cc->tct = GNUNET_SCHEDULER_add_now (&offer_hello,
322 cc);
323 if (NULL == cc->ats_sh)
324 cc->ats_sh = GNUNET_ATS_connectivity_suggest (cc->p1->ats,
325 &p2->id,
326 1);
327 }
427 } 328 }
428 GNUNET_SERVICE_client_continue (client->client);
429} 329}
430 330
431 331
432/**
433 * @brief Communicator informs that it wont try establishing requested queue.
434 *
435 * It will not do so probably because the address is bougus (see comment to
436 * #GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_CREATE_FAIL)
437 *
438 * @param cls Closure - communicator handle
439 * @param msg Message
440 */
441static void 332static void
442handle_queue_create_fail ( 333hello_iter_cb (void *cb_cls,
443 void *cls, 334 const struct GNUNET_PEERSTORE_Record *record,
444 const struct GNUNET_TRANSPORT_CreateQueueResponse *msg) 335 const char *emsg)
445{ 336{
446 struct MyClient *client = cls; 337 struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cb_cls;
447 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = 338 //Check record type et al?
448 client->tc; 339 p->hello = GNUNET_malloc (record->value_size);
340 memcpy (p->hello, record->value, record->value_size);
449 341
450 if (NULL != tc_h->queue_create_reply_cb) 342 GNUNET_PEERSTORE_iterate_cancel (p->pic);
343 if (NULL != p->start_cb)
451 { 344 {
452 tc_h->queue_create_reply_cb (tc_h->cb_cls, tc_h, GNUNET_NO); 345 LOG (GNUNET_ERROR_TYPE_DEBUG,
346 "Peer %u (`%s') successfully started\n",
347 p->no,
348 GNUNET_i2s (&p->id));
349 p->start_cb (p->start_cb_cls);
350 p->start_cb = NULL;
453 } 351 }
454 GNUNET_SERVICE_client_continue (client->client);
455} 352}
456 353
457 354
458/** 355/**
459 * New queue became available. Check message. 356 * Start a peer with the given configuration
460 * 357 * @param tth the testing handle
461 * @param cls the client 358 * @param cfgname configuration file
462 * @param aqm the send message that was sent 359 * @param peer_id a unique number to identify the peer
463 */ 360 * @param handlers functions for receiving messages
464static int 361 * @param nc connect callback
465check_add_queue_message (void *cls, 362 * @param nd disconnect callback
466 const struct GNUNET_TRANSPORT_AddQueueMessage *aqm) 363 * @param cb_cls closure for callback
467{ 364 * @param start_cb start callback
468 GNUNET_MQ_check_zero_termination (aqm); 365 * @param start_cb_cls closure for callback
469 return GNUNET_OK; 366 * @return the peer context
470} 367 */
471 368struct GNUNET_TRANSPORT_TESTING_PeerContext *
472 369GNUNET_TRANSPORT_TESTING_start_peer (struct
473/** 370 GNUNET_TRANSPORT_TESTING_Handle *tth,
474 * @brief Handle new queue 371 const char *cfgname,
475 * 372 int peer_id,
476 * Store context and call client callback. 373 const struct
477 * 374 GNUNET_MQ_MessageHandler *handlers,
478 * @param cls Closure - communicator handle 375 GNUNET_TRANSPORT_NotifyConnect nc,
479 * @param msg Message struct 376 GNUNET_TRANSPORT_NotifyDisconnect nd,
480 */ 377 void *cb_cls,
481static void 378 GNUNET_SCHEDULER_TaskCallback start_cb,
482handle_add_queue_message (void *cls, 379 void *start_cb_cls)
483 const struct GNUNET_TRANSPORT_AddQueueMessage *msg) 380{
484{ 381 char *emsg = NULL;
485 struct MyClient *client = cls; 382 struct GNUNET_TRANSPORT_TESTING_PeerContext *p;
486 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = 383 struct GNUNET_PeerIdentity dummy;
487 client->tc; 384 unsigned int i;
488 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *tc_queue; 385
489 386 if (GNUNET_NO == GNUNET_DISK_file_test (cfgname))
490 LOG (GNUNET_ERROR_TYPE_DEBUG,
491 "Got queue with ID %u\n", msg->qid);
492 for (tc_queue = tc_h->queue_head; NULL != tc_queue; tc_queue = tc_queue->next)
493 { 387 {
494 if (tc_queue->qid == msg->qid) 388 LOG (GNUNET_ERROR_TYPE_ERROR,
495 break; 389 "File not found: `%s'\n",
390 cfgname);
391 return NULL;
496 } 392 }
497 if (NULL == tc_queue) 393
394 p = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_PeerContext);
395 p->tth = tth;
396 p->nc = nc;
397 p->nd = nd;
398 if (NULL != handlers)
498 { 399 {
499 tc_queue = 400 for (i = 0; NULL != handlers[i].cb; i++)
500 GNUNET_new (struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue); 401 ;
501 tc_queue->tc_h = tc_h; 402 p->handlers = GNUNET_new_array (i + 1,
502 tc_queue->qid = msg->qid; 403 struct GNUNET_MQ_MessageHandler);
503 tc_queue->peer_id = msg->receiver; 404 GNUNET_memcpy (p->handlers,
504 GNUNET_CONTAINER_DLL_insert (tc_h->queue_head, tc_h->queue_tail, tc_queue); 405 handlers,
406 i * sizeof(struct GNUNET_MQ_MessageHandler));
505 } 407 }
506 GNUNET_assert (tc_queue->qid == msg->qid); 408 if (NULL != cb_cls)
507 GNUNET_assert (0 == GNUNET_memcmp (&tc_queue->peer_id, &msg->receiver)); 409 p->cb_cls = cb_cls;
508 tc_queue->nt = msg->nt; 410 else
509 tc_queue->mtu = ntohl (msg->mtu); 411 p->cb_cls = p;
510 tc_queue->cs = msg->cs; 412 p->start_cb = start_cb;
511 tc_queue->priority = ntohl (msg->priority); 413 if (NULL != start_cb_cls)
512 tc_queue->q_len = GNUNET_ntohll (msg->q_len); 414 p->start_cb_cls = start_cb_cls;
513 if (NULL != tc_h->add_queue_cb) 415 else
416 p->start_cb_cls = p;
417 GNUNET_CONTAINER_DLL_insert (tth->p_head,
418 tth->p_tail,
419 p);
420
421 /* Create configuration and call testing lib to modify it */
422 p->cfg = GNUNET_CONFIGURATION_create ();
423 GNUNET_assert (GNUNET_OK ==
424 GNUNET_CONFIGURATION_load (p->cfg, cfgname));
425 if (GNUNET_SYSERR ==
426 GNUNET_TESTING_configuration_create (tth->tl_system,
427 p->cfg))
514 { 428 {
515 tc_h->add_queue_cb (tc_h->cb_cls, tc_h, tc_queue, tc_queue->mtu); 429 LOG (GNUNET_ERROR_TYPE_ERROR,
430 "Testing library failed to create unique configuration based on `%s'\n",
431 cfgname);
432 GNUNET_CONFIGURATION_destroy (p->cfg);
433 GNUNET_free (p);
434 return NULL;
516 } 435 }
517 GNUNET_SERVICE_client_continue (client->client);
518}
519
520 436
521/** 437 p->no = peer_id;
522 * @brief Handle new queue 438 /* Configure peer with configuration */
523 * 439 p->peer = GNUNET_TESTING_peer_configure (tth->tl_system,
524 * Store context and call client callback. 440 p->cfg,
525 * 441 p->no,
526 * @param cls Closure - communicator handle 442 NULL,
527 * @param msg Message struct 443 &emsg);
528 */ 444 if (NULL == p->peer)
529static void 445 {
530handle_update_queue_message (void *cls, 446 LOG (GNUNET_ERROR_TYPE_ERROR,
531 const struct 447 "Testing library failed to create unique configuration based on `%s': `%s'\n",
532 GNUNET_TRANSPORT_UpdateQueueMessage *msg) 448 cfgname,
533{ 449 emsg);
534 struct MyClient *client = cls; 450 GNUNET_TRANSPORT_TESTING_stop_peer (p);
535 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = 451 GNUNET_free (emsg);
536 client->tc; 452 return NULL;
537 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *tc_queue; 453 }
454 GNUNET_free (emsg);
455 if (GNUNET_OK != GNUNET_TESTING_peer_start (p->peer))
456 {
457 LOG (GNUNET_ERROR_TYPE_ERROR,
458 "Testing library failed to create unique configuration based on `%s'\n",
459 cfgname);
460 GNUNET_TRANSPORT_TESTING_stop_peer (p);
461 return NULL;
462 }
538 463
464 memset (&dummy,
465 '\0',
466 sizeof(dummy));
467 GNUNET_TESTING_peer_get_identity (p->peer,
468 &p->id);
469 if (0 == memcmp (&dummy,
470 &p->id,
471 sizeof(struct GNUNET_PeerIdentity)))
472 {
473 LOG (GNUNET_ERROR_TYPE_ERROR,
474 "Testing library failed to obtain peer identity for peer %u\n",
475 p->no);
476 GNUNET_TRANSPORT_TESTING_stop_peer (p);
477 return NULL;
478 }
539 LOG (GNUNET_ERROR_TYPE_DEBUG, 479 LOG (GNUNET_ERROR_TYPE_DEBUG,
540 "Received queue update message for %u with q_len %" PRIu64 "\n", 480 "Peer %u configured with identity `%s'\n",
541 msg->qid, GNUNET_ntohll (msg->q_len)); 481 p->no,
542 tc_queue = tc_h->queue_head; 482 GNUNET_i2s_full (&p->id));
543 if (NULL != tc_queue) 483 p->tmh = GNUNET_TRANSPORT_manipulation_connect (p->cfg);
484 p->th = GNUNET_TRANSPORT_core_connect (p->cfg,
485 NULL,
486 handlers,
487 p,
488 &notify_connect,
489 &notify_disconnect);
490 if ((NULL == p->th) ||
491 (NULL == p->tmh))
544 { 492 {
545 while (tc_queue->qid != msg->qid) 493 LOG (GNUNET_ERROR_TYPE_ERROR,
546 { 494 "Failed to connect to transport service for peer `%s': `%s'\n",
547 tc_queue = tc_queue->next; 495 cfgname,
548 } 496 emsg);
497 GNUNET_TRANSPORT_TESTING_stop_peer (p);
498 return NULL;
549 } 499 }
550 GNUNET_assert (tc_queue->qid == msg->qid); 500 p->ats = GNUNET_ATS_connectivity_init (p->cfg);
551 GNUNET_assert (0 == GNUNET_memcmp (&tc_queue->peer_id, &msg->receiver)); 501 if (NULL == p->ats)
552 tc_queue->nt = msg->nt; 502 {
553 tc_queue->mtu = ntohl (msg->mtu); 503 LOG (GNUNET_ERROR_TYPE_ERROR,
554 tc_queue->cs = msg->cs; 504 "Failed to connect to ATS service for peer `%s': `%s'\n",
555 tc_queue->priority = ntohl (msg->priority); 505 cfgname,
556 // Uncomment this for alternativ 1 of backchannel functionality 506 emsg);
557 tc_queue->q_len += GNUNET_ntohll (msg->q_len); 507 GNUNET_TRANSPORT_TESTING_stop_peer (p);
558 // Until here for alternativ 1 508 return NULL;
559 // Uncomment this for alternativ 2 of backchannel functionality 509 }
560 // tc_queue->q_len = GNUNET_ntohll (msg->q_len); 510 p->pic = GNUNET_PEERSTORE_iterate (p->ph,
561 // Until here for alternativ 2 511 "transport",
562 GNUNET_SERVICE_client_continue (client->client); 512 &p->id,
513 GNUNET_PEERSTORE_TRANSPORT_HELLO_KEY,
514 hello_iter_cb,
515 p);
516 GNUNET_assert (NULL != p->pic);
517
518 return p;
563} 519}
564 520
565 521
566/** 522/**
567 * @brief Shut down the service 523 * Stops and restarts the given peer, sleeping (!) for 5s in between.
568 * 524 *
569 * @param cls Closure - Handle to the service 525 * @param p the peer
526 * @param restart_cb callback to call when restarted
527 * @param restart_cb_cls callback closure
528 * @return #GNUNET_OK in success otherwise #GNUNET_SYSERR
570 */ 529 */
571static void 530int
572shutdown_service (void *cls) 531GNUNET_TRANSPORT_TESTING_restart_peer (struct
532 GNUNET_TRANSPORT_TESTING_PeerContext *p,
533 GNUNET_SCHEDULER_TaskCallback restart_cb,
534 void *restart_cb_cls)
573{ 535{
574 struct GNUNET_SERVICE_Handle *h = cls; 536 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc;
537 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ccn;
575 538
539 /* shutdown */
576 LOG (GNUNET_ERROR_TYPE_DEBUG, 540 LOG (GNUNET_ERROR_TYPE_DEBUG,
577 "Shutting down service!\n"); 541 "Stopping peer %u (`%s')\n",
578 542 p->no,
579 GNUNET_SERVICE_stop (h); 543 GNUNET_i2s (&p->id));
580} 544 if (NULL != p->pic)
581
582
583/**
584 * @brief Callback called when new Client (Communicator) connects
585 *
586 * @param cls Closure - TransporCommmunicator Handle
587 * @param client Client
588 * @param mq Messagequeue
589 *
590 * @return TransportCommunicator Handle
591 */
592static void *
593connect_cb (void *cls,
594 struct GNUNET_SERVICE_Client *client,
595 struct GNUNET_MQ_Handle *mq)
596{
597 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = cls;
598 struct MyClient *new_c;
599
600 LOG (GNUNET_ERROR_TYPE_DEBUG, "Client %p connected to %p.\n",
601 client, tc_h);
602 new_c = GNUNET_new (struct MyClient);
603 new_c->client = client;
604 new_c->c_mq = mq;
605 new_c->tc = tc_h;
606 GNUNET_CONTAINER_DLL_insert (tc_h->client_head,
607 tc_h->client_tail,
608 new_c);
609
610 if (NULL == tc_h->queue_head)
611 return new_c;
612 /* Iterate over queues. They are yet to be opened. Request opening. */
613 for (struct
614 GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *tc_queue_iter =
615 tc_h->queue_head;
616 NULL != tc_queue_iter;
617 tc_queue_iter = tc_queue_iter->next)
618 { 545 {
619 if (NULL == tc_queue_iter->open_queue_env) 546 GNUNET_PEERSTORE_iterate_cancel (p->pic);
620 continue; 547 p->pic = NULL;
621 /* Send the previously created mq envelope to request the creation of the 548 }
622 * queue. */ 549 if (NULL != p->th)
623 GNUNET_MQ_send (tc_h->c_mq, 550 {
624 tc_queue_iter->open_queue_env); 551 GNUNET_TRANSPORT_core_disconnect (p->th);
625 tc_queue_iter->open_queue_env = NULL; 552 p->th = NULL;
553 }
554 if (NULL != p->tmh)
555 {
556 GNUNET_TRANSPORT_manipulation_disconnect (p->tmh);
557 p->tmh = NULL;
558 }
559 for (cc = p->tth->cc_head; NULL != cc; cc = ccn)
560 {
561 ccn = cc->next;
562 if ((cc->p1 == p) ||
563 (cc->p2 == p))
564 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc);
565 }
566 if (NULL != p->ats)
567 {
568 GNUNET_ATS_connectivity_done (p->ats);
569 p->ats = NULL;
570 }
571 if (GNUNET_SYSERR ==
572 GNUNET_TESTING_peer_stop (p->peer))
573 {
574 LOG (GNUNET_ERROR_TYPE_ERROR,
575 "Failed to stop peer %u (`%s')\n",
576 p->no,
577 GNUNET_i2s (&p->id));
578 return GNUNET_SYSERR;
626 } 579 }
627 return new_c;
628}
629 580
581 sleep (5); // YUCK!
630 582
631/** 583 LOG (GNUNET_ERROR_TYPE_DEBUG,
632 * @brief Callback called when Client disconnects 584 "Restarting peer %u (`%s')\n",
633 * 585 p->no,
634 * @param cls Closure - TransportCommunicator Handle 586 GNUNET_i2s (&p->id));
635 * @param client Client 587 /* restart */
636 * @param internal_cls TransporCommmunicator Handle 588 if (GNUNET_SYSERR == GNUNET_TESTING_peer_start (p->peer))
637 */
638static void
639disconnect_cb (void *cls,
640 struct GNUNET_SERVICE_Client *client,
641 void *internal_cls)
642{
643 struct MyClient *cl = cls;
644 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = cls;
645
646 for (cl = tc_h->client_head; NULL != cl; cl = cl->next)
647 { 589 {
648 if (cl->client != client) 590 LOG (GNUNET_ERROR_TYPE_ERROR,
649 continue; 591 "Failed to restart peer %u (`%s')\n",
650 GNUNET_CONTAINER_DLL_remove (tc_h->client_head, 592 p->no,
651 tc_h->client_tail, 593 GNUNET_i2s (&p->id));
652 cl); 594 return GNUNET_SYSERR;
653 if (cl->c_mq == tc_h->c_mq)
654 tc_h->c_mq = NULL;
655 GNUNET_free (cl);
656 break;
657 } 595 }
658 LOG (GNUNET_ERROR_TYPE_DEBUG, "Client disconnected.\n");
659}
660
661 596
662/** 597 GNUNET_assert (NULL == p->start_cb);
663 * Message was transmitted. Process the request. 598 p->start_cb = restart_cb;
664 * 599 p->start_cb_cls = restart_cb_cls;
665 * @param cls the client 600
666 * @param sma the send message that was sent 601 p->th = GNUNET_TRANSPORT_core_connect (p->cfg,
667 */ 602 NULL,
668static void 603 p->handlers,
669handle_send_message_ack (void *cls, 604 p,
670 const struct GNUNET_TRANSPORT_SendMessageToAck *sma) 605 &notify_connect,
671{ 606 &notify_disconnect);
672 struct MyClient *client = cls; 607 GNUNET_assert (NULL != p->th);
673 GNUNET_SERVICE_client_continue (client->client); 608 p->ats = GNUNET_ATS_connectivity_init (p->cfg);
674 // NOP 609 p->pic = GNUNET_PEERSTORE_iterate (p->ph,
610 "transport",
611 &p->id,
612 GNUNET_PEERSTORE_TRANSPORT_HELLO_KEY,
613 hello_iter_cb,
614 p);
615 GNUNET_assert (NULL != p->pic);
616 return GNUNET_OK;
675} 617}
676 618
677 619
678/** 620/**
679 * @brief Start the communicator part of the transport service 621 * Shutdown the given peer
680 * 622 *
681 * @param communicator_available Callback to be called when a new communicator 623 * @param p the peer
682 * becomes available
683 * @param cfg Configuration
684 */ 624 */
685static void 625void
686transport_communicator_start ( 626GNUNET_TRANSPORT_TESTING_stop_peer (struct
687 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h) 627 GNUNET_TRANSPORT_TESTING_PeerContext *p)
688{ 628{
689 struct GNUNET_MQ_MessageHandler mh[] = { 629 struct GNUNET_TRANSPORT_TESTING_Handle *tth = p->tth;
690 GNUNET_MQ_hd_var_size (communicator_available, 630 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc;
691 GNUNET_MESSAGE_TYPE_TRANSPORT_NEW_COMMUNICATOR, 631 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ccn;
692 struct GNUNET_TRANSPORT_CommunicatorAvailableMessage,
693 tc_h),
694 GNUNET_MQ_hd_var_size (communicator_backchannel,
695 GNUNET_MESSAGE_TYPE_TRANSPORT_COMMUNICATOR_BACKCHANNEL,
696 struct GNUNET_TRANSPORT_CommunicatorBackchannel,
697 tc_h),
698 GNUNET_MQ_hd_var_size (add_address,
699 GNUNET_MESSAGE_TYPE_TRANSPORT_ADD_ADDRESS,
700 struct GNUNET_TRANSPORT_AddAddressMessage,
701 tc_h),
702 // GNUNET_MQ_hd_fixed_size (del_address,
703 // GNUNET_MESSAGE_TYPE_TRANSPORT_DEL_ADDRESS,
704 // struct GNUNET_TRANSPORT_DelAddressMessage,
705 // NULL),
706 GNUNET_MQ_hd_var_size (incoming_msg,
707 GNUNET_MESSAGE_TYPE_TRANSPORT_INCOMING_MSG,
708 struct GNUNET_TRANSPORT_IncomingMessage,
709 tc_h),
710 GNUNET_MQ_hd_fixed_size (queue_create_ok,
711 GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_CREATE_OK,
712 struct GNUNET_TRANSPORT_CreateQueueResponse,
713 tc_h),
714 GNUNET_MQ_hd_fixed_size (queue_create_fail,
715 GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_CREATE_FAIL,
716 struct GNUNET_TRANSPORT_CreateQueueResponse,
717 tc_h),
718 GNUNET_MQ_hd_var_size (add_queue_message,
719 GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_SETUP,
720 struct GNUNET_TRANSPORT_AddQueueMessage,
721 tc_h),
722 GNUNET_MQ_hd_fixed_size (update_queue_message,
723 GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_UPDATE,
724 struct GNUNET_TRANSPORT_UpdateQueueMessage,
725 tc_h),
726 // GNUNET_MQ_hd_fixed_size (del_queue_message,
727 // GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_TEARDOWN,
728 // struct GNUNET_TRANSPORT_DelQueueMessage,
729 // NULL),
730 GNUNET_MQ_hd_fixed_size (send_message_ack,
731 GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_MSG_ACK,
732 struct GNUNET_TRANSPORT_SendMessageToAck,
733 tc_h),
734 GNUNET_MQ_handler_end ()
735 };
736
737
738 tc_h->sh = GNUNET_SERVICE_start ("transport",
739 tc_h->cfg,
740 &connect_cb,
741 &disconnect_cb,
742 tc_h,
743 mh);
744 GNUNET_assert (NULL != tc_h->sh);
745}
746 632
747 633 for (cc = tth->cc_head; NULL != cc; cc = ccn)
748/** 634 {
749 * @brief Task run at shutdown to kill communicator and clean up 635 ccn = cc->next;
750 * 636 if ((cc->p1 == p) ||
751 * @param cls Closure - Process of communicator 637 (cc->p2 == p))
752 */ 638 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc);
753static void 639 }
754shutdown_process (struct GNUNET_OS_Process *proc) 640 if (NULL != p->pic)
755{ 641 {
756 if (0 != GNUNET_OS_process_kill (proc, SIGTERM)) 642 GNUNET_PEERSTORE_iterate_cancel (p->pic);
643 p->pic = NULL;
644 }
645 if (NULL != p->tmh)
646 {
647 GNUNET_TRANSPORT_manipulation_disconnect (p->tmh);
648 p->tmh = NULL;
649 }
650 if (NULL != p->th)
651 {
652 GNUNET_TRANSPORT_core_disconnect (p->th);
653 p->th = NULL;
654 }
655 if (NULL != p->peer)
757 { 656 {
758 LOG (GNUNET_ERROR_TYPE_WARNING, 657 if (GNUNET_OK !=
759 "Error shutting down process with SIGERM, trying SIGKILL\n"); 658 GNUNET_TESTING_peer_stop (p->peer))
760 if (0 != GNUNET_OS_process_kill (proc, SIGKILL))
761 { 659 {
762 LOG (GNUNET_ERROR_TYPE_ERROR, 660 LOG (GNUNET_ERROR_TYPE_DEBUG,
763 "Error shutting down process with SIGERM and SIGKILL\n"); 661 "Testing lib failed to stop peer %u (`%s')\n",
662 p->no,
663 GNUNET_i2s (&p->id));
764 } 664 }
665 GNUNET_TESTING_peer_destroy (p->peer);
666 p->peer = NULL;
765 } 667 }
766 GNUNET_OS_process_destroy (proc); 668 if (NULL != p->ats)
767}
768
769static void
770shutdown_peerstore (void *cls)
771{
772 struct GNUNET_OS_Process *proc = cls;
773 shutdown_process (proc);
774}
775
776static void
777shutdown_communicator (void *cls)
778{
779 struct GNUNET_OS_Process *proc = cls;
780 shutdown_process (proc);
781}
782
783
784/**
785 * @brief Start the communicator
786 *
787 * @param cfgname Name of the communicator
788 */
789static void
790communicator_start (
791 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h,
792 const char *binary_name)
793{
794 char *binary;
795 char *loprefix;
796 char *section_name;
797
798 LOG (GNUNET_ERROR_TYPE_DEBUG, "communicator_start\n");
799
800 section_name = strchr (binary_name, '-');
801 section_name++;
802
803 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (tc_h->cfg,
804 section_name,
805 "PREFIX",
806 &loprefix))
807 loprefix = GNUNET_strdup ("");
808
809
810 binary = GNUNET_OS_get_libexec_binary_path (binary_name);
811 tc_h->c_proc = GNUNET_OS_start_process_s (GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
812 NULL,
813 loprefix,
814 binary,
815 binary_name,
816 "-c",
817 tc_h->cfg_filename,
818 NULL);
819 if (NULL == tc_h->c_proc)
820 { 669 {
821 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start communicator!"); 670 GNUNET_ATS_connectivity_done (p->ats);
822 return; 671 p->ats = NULL;
823 } 672 }
824 LOG (GNUNET_ERROR_TYPE_INFO, "started communicator\n"); 673 if (NULL != p->hello)
825 GNUNET_free (binary); 674 {
675 GNUNET_free (p->hello);
676 p->hello = NULL;
677 }
678 if (NULL != p->cfg)
679 {
680 GNUNET_CONFIGURATION_destroy (p->cfg);
681 p->cfg = NULL;
682 }
683 if (NULL != p->handlers)
684 {
685 GNUNET_free (p->handlers);
686 p->handlers = NULL;
687 }
688 GNUNET_CONTAINER_DLL_remove (tth->p_head,
689 tth->p_tail,
690 p);
691 LOG (GNUNET_ERROR_TYPE_DEBUG,
692 "Peer %u (`%s') stopped\n",
693 p->no,
694 GNUNET_i2s (&p->id));
695 GNUNET_free (p);
826} 696}
827 697
828 698
829/** 699/**
830 * @brief Task run at shutdown to kill NAT and clean up 700 * Function called after the HELLO was passed to the
831 * 701 * transport service.
832 * @param cls Closure - Process of NAT 702 * FIXME maybe schedule the application_validate somehow
833 */ 703 */
704/*
834static void 705static void
835shutdown_nat (void *cls) 706hello_offered (void *cls)
836{ 707{
837 struct GNUNET_OS_Process *proc = cls; 708 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc = cls;
838 shutdown_process (proc); 709
839} 710 cc->oh = NULL;
711 cc->tct = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
712 &offer_hello,
713 cc);
714}*/
840 715
841/**
842 * @brief Task run at shutdown to kill statistics service and clean up
843 *
844 * @param cls Closure - Process of statistics service
845 */
846static void
847shutdown_statistics (void *cls)
848{
849 struct GNUNET_OS_Process *proc = cls;
850 shutdown_process (proc);
851}
852 716
853/** 717/**
854 * @brief Task run at shutdown to kill the resolver process 718 * Offer the current HELLO of P2 to P1.
855 * 719 *
856 * @param cls Closure - Process of communicator 720 * @param cls our `struct GNUNET_TRANSPORT_TESTING_ConnectRequest`
857 */ 721 */
858static void 722static void
859shutdown_resolver (void *cls) 723offer_hello (void *cls)
860{ 724{
861 struct GNUNET_OS_Process *proc = cls; 725 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc = cls;
862 shutdown_process (proc); 726 struct GNUNET_TRANSPORT_TESTING_PeerContext *p1 = cc->p1;
863} 727 struct GNUNET_TRANSPORT_TESTING_PeerContext *p2 = cc->p2;
864 728 struct GNUNET_TIME_Absolute t;
729 enum GNUNET_NetworkType nt;
730 char *addr;
865 731
866static void 732 cc->tct = NULL;
867resolver_start (struct
868 GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h)
869{
870 char *binary;
871
872 LOG (GNUNET_ERROR_TYPE_DEBUG, "resolver_start\n");
873 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
874 tc_h->resolver_proc = GNUNET_OS_start_process (
875 GNUNET_OS_INHERIT_STD_OUT_AND_ERR
876 | GNUNET_OS_USE_PIPE_CONTROL,
877 NULL,
878 NULL,
879 NULL,
880 binary,
881 "gnunet-service-resolver",
882 "-c",
883 tc_h->cfg_filename,
884 NULL);
885 if (NULL == tc_h->resolver_proc)
886 { 733 {
887 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start resolver service!"); 734 char *p2_s = GNUNET_strdup (GNUNET_i2s (&p2->id));
888 return; 735
736 LOG (GNUNET_ERROR_TYPE_DEBUG,
737 "Asking peer %u (`%s') to connect peer %u (`%s'), providing HELLO with %s\n",
738 p1->no,
739 GNUNET_i2s (&p1->id),
740 p2->no,
741 p2_s,
742 cc->p2->hello);
743 GNUNET_free (p2_s);
889 } 744 }
890 LOG (GNUNET_ERROR_TYPE_INFO, "started resolver service\n");
891 GNUNET_free (binary);
892 745
746 addr = GNUNET_HELLO_extract_address (cc->p2->hello,
747 cc->p2->hello_size,
748 &cc->p2->id,
749 &nt,
750 &t);
751 GNUNET_assert (NULL == addr);
752 GNUNET_TRANSPORT_application_validate (cc->p1->ah,
753 &cc->p2->id,
754 nt,
755 addr);
756 GNUNET_free (addr);
893} 757}
894 758
895 759
896/** 760/**
897 * @brief Start Peerstore 761 * Initiate a connection from p1 to p2 by offering p1 p2's HELLO message
898 * 762 *
899 */ 763 * Remarks: start_peer's notify_connect callback can be called before.
900static void
901peerstore_start (
902 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h)
903{
904 char *binary;
905
906 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-peerstore");
907 tc_h->ps_proc = GNUNET_OS_start_process (GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
908 NULL,
909 NULL,
910 NULL,
911 binary,
912 "gnunet-service-peerstore",
913 "-c",
914 tc_h->cfg_filename,
915 NULL);
916 if (NULL == tc_h->ps_proc)
917 {
918 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start Peerstore!");
919 return;
920 }
921 LOG (GNUNET_ERROR_TYPE_INFO, "started Peerstore\n");
922 GNUNET_free (binary);
923}
924
925/**
926 * @brief Start NAT
927 * 764 *
765 * @param tth transport testing handle
766 * @param p1 peer 1
767 * @param p2 peer 2
768 * @param cb the callback to call when both peers notified that they are connected
769 * @param cls callback cls
770 * @return a connect request handle
928 */ 771 */
929static void 772struct GNUNET_TRANSPORT_TESTING_ConnectRequest *
930nat_start ( 773GNUNET_TRANSPORT_TESTING_connect_peers (struct
931 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h) 774 GNUNET_TRANSPORT_TESTING_PeerContext *p1,
775 struct
776 GNUNET_TRANSPORT_TESTING_PeerContext *p2,
777 GNUNET_SCHEDULER_TaskCallback cb,
778 void *cls)
932{ 779{
933 char *binary; 780 struct GNUNET_TRANSPORT_TESTING_Handle *tth = p1->tth;
934 781 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc;
935 LOG (GNUNET_ERROR_TYPE_DEBUG, "nat_start\n"); 782 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ccn;
936 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-nat"); 783
937 tc_h->nat_proc = GNUNET_OS_start_process (GNUNET_OS_INHERIT_STD_OUT_AND_ERR 784 ccn = NULL;
938 | GNUNET_OS_USE_PIPE_CONTROL, 785 for (cc = tth->cc_head; NULL != cc; cc = cc->next)
939 NULL,
940 NULL,
941 NULL,
942 binary,
943 "gnunet-service-nat",
944 "-c",
945 tc_h->cfg_filename,
946 NULL);
947 if (NULL == tc_h->nat_proc)
948 { 786 {
949 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start NAT!"); 787 if ((cc->p1 == p1) &&
950 return; 788 (cc->p2 == p2))
789 {
790 ccn = cc;
791 break;
792 }
951 } 793 }
952 LOG (GNUNET_ERROR_TYPE_INFO, "started NAT\n");
953 GNUNET_free (binary);
954}
955 794
956/** 795 cc = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_ConnectRequest);
957 * @brief Start statistics 796 cc->p1 = p1;
958 * 797 cc->p2 = p2;
959 */ 798 cc->cb = cb;
960static void 799 if (NULL != cls)
961statistics_start ( 800 cc->cb_cls = cls;
962 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h) 801 else
963{ 802 cc->cb_cls = cc;
964 char *binary; 803 if (NULL != ccn)
965
966 LOG (GNUNET_ERROR_TYPE_DEBUG, "statistics_start\n");
967 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-statistics");
968 tc_h->statistics_proc = GNUNET_OS_start_process (
969 GNUNET_OS_INHERIT_STD_OUT_AND_ERR
970 | GNUNET_OS_USE_PIPE_CONTROL,
971 NULL,
972 NULL,
973 NULL,
974 binary,
975 "gnunet-service-statistics",
976 "-c",
977 tc_h->cfg_filename,
978 NULL);
979 if (NULL == tc_h->statistics_proc)
980 { 804 {
981 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start statistics service!"); 805 cc->p1_c = ccn->p1_c;
982 return; 806 cc->p2_c = ccn->p2_c;
807 cc->connected = ccn->connected;
983 } 808 }
984 LOG (GNUNET_ERROR_TYPE_INFO, "started statistics service\n"); 809 GNUNET_CONTAINER_DLL_insert (tth->cc_head,
985 GNUNET_free (binary); 810 tth->cc_tail,
811 cc);
812 cc->tct = GNUNET_SCHEDULER_add_now (&offer_hello,
813 cc);
814 cc->ats_sh = GNUNET_ATS_connectivity_suggest (cc->p1->ats,
815 &p2->id,
816 1);
817 LOG (GNUNET_ERROR_TYPE_DEBUG,
818 "New connect request %p\n",
819 cc);
820 return cc;
986} 821}
987 822
988 823
989/** 824/**
990 * @brief Start communicator part of transport service and communicator 825 * Cancel the request to connect two peers
991 * 826 * Tou MUST cancel the request if you stop the peers before the peers connected succesfully
992 * @param service_name Name of the service
993 * @param cfg Configuration handle
994 * @param communicator_available_cb Callback that is called when a new
995 * @param add_address_cb Callback that is called when a new
996 * communicator becomes available
997 * @param cb_cls Closure to @a communicator_available_cb and @a
998 * 827 *
999 * @return Handle to the communicator duo 828 * @param tth transport testing handle
829 * @param cc a connect request handle
1000 */ 830 */
1001struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle * 831void
1002GNUNET_TRANSPORT_TESTING_transport_communicator_service_start ( 832GNUNET_TRANSPORT_TESTING_connect_peers_cancel (struct
1003 const char *service_name, 833 GNUNET_TRANSPORT_TESTING_ConnectRequest
1004 const char *binary_name, 834 *cc)
1005 const char *cfg_filename,
1006 const struct GNUNET_PeerIdentity *peer_id,
1007 GNUNET_TRANSPORT_TESTING_CommunicatorAvailableCallback
1008 communicator_available_cb,
1009 GNUNET_TRANSPORT_TESTING_AddAddressCallback add_address_cb,
1010 GNUNET_TRANSPORT_TESTING_QueueCreateReplyCallback queue_create_reply_cb,
1011 GNUNET_TRANSPORT_TESTING_AddQueueCallback add_queue_cb,
1012 GNUNET_TRANSPORT_TESTING_IncomingMessageCallback incoming_message_cb,
1013 GNUNET_TRANSPORT_TESTING_BackchannelCallback bc_cb,
1014 void *cb_cls)
1015{ 835{
1016 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h; 836 struct GNUNET_TRANSPORT_TESTING_Handle *tth = cc->p1->tth;
1017 837
1018 LOG (GNUNET_ERROR_TYPE_DEBUG, 838 LOG (GNUNET_ERROR_TYPE_DEBUG,
1019 "Starting new transport/communicator combo with config %s\n", 839 "Canceling connect request!\n");
1020 cfg_filename); 840 if (NULL != cc->tct)
1021 tc_h =
1022 GNUNET_new (struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle);
1023 tc_h->cfg_filename = GNUNET_strdup (cfg_filename);
1024 tc_h->cfg = GNUNET_CONFIGURATION_create ();
1025 if ((GNUNET_SYSERR == GNUNET_CONFIGURATION_load (tc_h->cfg, cfg_filename)))
1026 { 841 {
1027 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 842 GNUNET_SCHEDULER_cancel (cc->tct);
1028 _ ("Malformed configuration file `%s', exit ...\n"), 843 cc->tct = NULL;
1029 cfg_filename);
1030 GNUNET_free (tc_h->cfg_filename);
1031 GNUNET_CONFIGURATION_destroy (tc_h->cfg);
1032 GNUNET_free (tc_h);
1033 return NULL;
1034 } 844 }
1035 tc_h->bc_enabled = GNUNET_CONFIGURATION_get_value_yesno (tc_h->cfg, 845 if (NULL != cc->ats_sh)
1036 "communicator-test", 846 {
1037 "BACKCHANNEL_ENABLED"); 847 GNUNET_ATS_connectivity_suggest_cancel (cc->ats_sh);
1038 tc_h->communicator_available_cb = communicator_available_cb; 848 cc->ats_sh = NULL;
1039 tc_h->add_address_cb = add_address_cb; 849 }
1040 tc_h->queue_create_reply_cb = queue_create_reply_cb; 850 GNUNET_CONTAINER_DLL_remove (tth->cc_head,
1041 tc_h->add_queue_cb = add_queue_cb; 851 tth->cc_tail,
1042 tc_h->incoming_msg_cb = incoming_message_cb; 852 cc);
1043 tc_h->bc_cb = bc_cb; 853 GNUNET_free (cc);
1044 tc_h->peer_id = *peer_id;
1045 tc_h->cb_cls = cb_cls;
1046
1047 /* Start communicator part of service */
1048 transport_communicator_start (tc_h);
1049 /* Start NAT */
1050 nat_start (tc_h);
1051 /* Start resolver service */
1052 resolver_start (tc_h);
1053 /* Start peerstore service */
1054 peerstore_start (tc_h);
1055 /* Start statistics service */
1056 statistics_start (tc_h);
1057 /* Schedule start communicator */
1058 communicator_start (tc_h,
1059 binary_name);
1060 return tc_h;
1061}
1062
1063
1064void
1065GNUNET_TRANSPORT_TESTING_transport_communicator_service_stop (
1066 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h)
1067{
1068 shutdown_communicator (tc_h->c_proc);
1069 shutdown_service (tc_h->sh);
1070 shutdown_nat (tc_h->nat_proc);
1071 shutdown_resolver (tc_h->resolver_proc);
1072 shutdown_peerstore (tc_h->ps_proc);
1073 shutdown_statistics (tc_h->statistics_proc);
1074 GNUNET_CONFIGURATION_destroy (tc_h->cfg);
1075 GNUNET_free (tc_h);
1076} 854}
1077 855
1078 856
1079/** 857/**
1080 * @brief Instruct communicator to open a queue 858 * Clean up the transport testing
1081 * 859 *
1082 * @param tc_h Handle to communicator which shall open queue 860 * @param tth transport testing handle
1083 * @param peer_id Towards which peer
1084 * @param address For which address
1085 */ 861 */
1086void 862void
1087GNUNET_TRANSPORT_TESTING_transport_communicator_open_queue ( 863GNUNET_TRANSPORT_TESTING_done (struct GNUNET_TRANSPORT_TESTING_Handle *tth)
1088 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h,
1089 const struct GNUNET_PeerIdentity *peer_id,
1090 const char *address)
1091{ 864{
1092 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *tc_queue; 865 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc;
1093 static uint32_t idgen; 866 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ct;
1094 char *prefix; 867 struct GNUNET_TRANSPORT_TESTING_PeerContext *p;
1095 struct GNUNET_TRANSPORT_CreateQueue *msg; 868 struct GNUNET_TRANSPORT_TESTING_PeerContext *t;
1096 struct GNUNET_MQ_Envelope *env; 869
1097 size_t alen; 870 if (NULL == tth)
1098
1099 tc_queue =
1100 GNUNET_new (struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue);
1101 tc_queue->tc_h = tc_h;
1102 prefix = GNUNET_HELLO_address_to_prefix (address);
1103 if (NULL == prefix)
1104 {
1105 GNUNET_break (0); /* We got an invalid address!? */
1106 GNUNET_free (tc_queue);
1107 return; 871 return;
1108 } 872 cc = tth->cc_head;
1109 GNUNET_free (prefix); 873 while (NULL != cc)
1110 alen = strlen (address) + 1;
1111 env =
1112 GNUNET_MQ_msg_extra (msg, alen, GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_CREATE);
1113 msg->request_id = htonl (idgen++);
1114 tc_queue->qid = msg->request_id;
1115 msg->receiver = *peer_id;
1116 tc_queue->peer_id = *peer_id;
1117 memcpy (&msg[1], address, alen);
1118 if (NULL != tc_h->c_mq)
1119 { 874 {
1120 LOG (GNUNET_ERROR_TYPE_DEBUG, 875 ct = cc->next;
1121 "Sending queue create immediately\n"); 876 LOG (GNUNET_ERROR_TYPE_ERROR,
1122 GNUNET_MQ_send (tc_h->c_mq, env); 877 "Developer forgot to cancel connect request!\n");
878 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc);
879 cc = ct;
1123 } 880 }
1124 else 881 p = tth->p_head;
882 while (NULL != p)
1125 { 883 {
1126 tc_queue->open_queue_env = env; 884 t = p->next;
885 LOG (GNUNET_ERROR_TYPE_ERROR,
886 "Developer forgot to stop peer!\n");
887 GNUNET_TRANSPORT_TESTING_stop_peer (p);
888 p = t;
1127 } 889 }
1128 GNUNET_CONTAINER_DLL_insert (tc_h->queue_head, tc_h->queue_tail, tc_queue); 890 GNUNET_TESTING_system_destroy (tth->tl_system,
891 GNUNET_YES);
892
893 GNUNET_free (tth);
1129} 894}
1130 895
1131 896
1132/** 897/**
1133 * @brief Instruct communicator to send data 898 * Initialize the transport testing
1134 * 899 *
1135 * @param tc_queue The queue to use for sending 900 * @return transport testing handle
1136 * @param cont function to call when done sending
1137 * @param cont_cls closure for @a cont
1138 * @param payload Data to send
1139 * @param payload_size Size of the @a payload
1140 */ 901 */
1141void 902struct GNUNET_TRANSPORT_TESTING_Handle *
1142GNUNET_TRANSPORT_TESTING_transport_communicator_send 903GNUNET_TRANSPORT_TESTING_init ()
1143 (struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h,
1144 GNUNET_SCHEDULER_TaskCallback cont,
1145 void *cont_cls,
1146 const void *payload,
1147 size_t payload_size)
1148{ 904{
1149 struct GNUNET_MessageHeader *mh; 905 struct GNUNET_TRANSPORT_TESTING_Handle *tth;
1150 struct GNUNET_TRANSPORT_SendMessageTo *msg; 906
1151 struct GNUNET_MQ_Envelope *env; 907 tth = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_Handle);
1152 size_t inbox_size; 908 tth->tl_system = GNUNET_TESTING_system_create ("transport-testing",
1153 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *tc_queue; 909 NULL,
1154 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *tc_queue_tmp; 910 NULL,
1155 911 NULL);
1156 tc_queue = NULL; 912 if (NULL == tth->tl_system)
1157 for (tc_queue_tmp = tc_h->queue_head;
1158 NULL != tc_queue_tmp;
1159 tc_queue_tmp = tc_queue_tmp->next)
1160 { 913 {
1161 if (tc_queue_tmp->q_len <= 0) 914 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1162 continue; 915 "Failed to initialize testing library!\n");
1163 if (NULL == tc_queue) 916 GNUNET_free (tth);
1164 { 917 return NULL;
1165 LOG (GNUNET_ERROR_TYPE_DEBUG,
1166 "Selecting queue with prio %u, len %" PRIu64 " and MTU %u\n",
1167 tc_queue_tmp->priority,
1168 tc_queue_tmp->q_len,
1169 tc_queue_tmp->mtu);
1170 tc_queue = tc_queue_tmp;
1171 continue;
1172 }
1173 if (tc_queue->priority < tc_queue_tmp->priority)
1174 {
1175 LOG (GNUNET_ERROR_TYPE_DEBUG,
1176 "Selecting queue with prio %u, len %" PRIu64 " and MTU %u\n",
1177 tc_queue_tmp->priority,
1178 tc_queue_tmp->q_len,
1179 tc_queue_tmp->mtu);
1180 tc_queue = tc_queue_tmp;
1181 }
1182 } 918 }
1183 GNUNET_assert (NULL != tc_queue); 919 return tth;
1184 // Uncomment this for alternativ 1 of backchannel functionality
1185 if (tc_queue->q_len != GNUNET_TRANSPORT_QUEUE_LENGTH_UNLIMITED)
1186 tc_queue->q_len--;
1187 // Until here for alternativ 1
1188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1189 "Sending message\n");
1190 inbox_size = sizeof (struct GNUNET_MessageHeader) + payload_size;
1191 env = GNUNET_MQ_msg_extra (msg,
1192 inbox_size,
1193 GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_MSG);
1194 GNUNET_assert (NULL != env);
1195 msg->qid = htonl (tc_queue->qid);
1196 msg->mid = tc_queue->mid++;
1197 msg->receiver = tc_queue->peer_id;
1198 mh = (struct GNUNET_MessageHeader *) &msg[1];
1199 mh->size = htons (inbox_size);
1200 mh->type = GNUNET_MESSAGE_TYPE_DUMMY;
1201 memcpy (&mh[1],
1202 payload,
1203 payload_size);
1204 if (NULL != cont)
1205 GNUNET_MQ_notify_sent (env,
1206 cont,
1207 cont_cls);
1208 GNUNET_MQ_send (tc_queue->tc_h->c_mq,
1209 env);
1210} 920}
921
922
923/* end of transport-testing.c */
diff --git a/src/transport/transport-testing2.h b/src/transport/transport-testing2.h
index a95c0dfa7..3554436d8 100644
--- a/src/transport/transport-testing2.h
+++ b/src/transport/transport-testing2.h
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 Copyright (C) 2019 GNUnet e.V. 3 Copyright (C) 2006, 2009, 2015, 2016 GNUnet e.V.
4 4
5 GNUnet is free software: you can redistribute it and/or modify it 5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published 6 under the terms of the GNU Affero General Public License as published
@@ -19,342 +19,906 @@
19 */ 19 */
20 20
21/** 21/**
22 * @file transport/transport-testing2.h 22 * @file transport-testing.h
23 * @brief functions and structures related to testing-tng 23 * @brief testing lib for transport service
24 * @author Matthias Wachs
24 * @author Christian Grothoff 25 * @author Christian Grothoff
25 * @author Julius Bünger
26 */ 26 */
27#ifndef TRANSPORT_TESTING_H
28#define TRANSPORT_TESTING_H
27#include "platform.h" 29#include "platform.h"
28#include "gnunet_util_lib.h" 30#include "gnunet_util_lib.h"
29#include "gnunet_ats_transport_service.h" 31#include "gnunet_hello_lib.h"
30#include "transport.h" 32#include "gnunet_peerstore_service.h"
33#include "gnunet_transport_core_service.h"
34#include "gnunet_transport_application_service.h"
35#include "gnunet_transport_manipulation_service.h"
36#include "gnunet_testing_lib.h"
37
38
39/* ************* Basic functions for starting/stopping/connecting *********** */
40
41/**
42 * Context for a single peer
43 */
44struct GNUNET_TRANSPORT_TESTING_PeerContext;
31 45
32/** 46/**
33 * @brief Queue of a communicator and some context 47 * Definition for a transport testing handle
34 */ 48 */
35struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue; 49struct GNUNET_TRANSPORT_TESTING_Handle;
36 50
37 51
38/** 52/**
39 * @brief Handle/Context to a single transmission 53 * Context for a single peer
40 */ 54 */
41struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorTransmission; 55struct GNUNET_TRANSPORT_TESTING_PeerContext
56{
57 /**
58 * Next element in the DLL
59 */
60 struct GNUNET_TRANSPORT_TESTING_PeerContext *next;
61
62 /**
63 * Previous element in the DLL
64 */
65 struct GNUNET_TRANSPORT_TESTING_PeerContext *prev;
66
67 /**
68 * Transport testing handle this peer belongs to
69 */
70 struct GNUNET_TRANSPORT_TESTING_Handle *tth;
71
72 /**
73 * Application handle
74 */
75 struct GNUNET_TRANSPORT_ApplicationHandle *ah;
76
77 /**
78 * Peer's configuration
79 */
80 struct GNUNET_CONFIGURATION_Handle *cfg;
81
82 /**
83 * Peer's transport service handle
84 */
85 struct GNUNET_TRANSPORT_CoreHandle *th;
86
87 /**
88 * Peer's transport service manipulation handle
89 */
90 struct GNUNET_TRANSPORT_ManipulationHandle *tmh;
91
92 /**
93 * Peer's ATS handle.
94 */
95 struct GNUNET_ATS_ConnectivityHandle *ats;
96
97 /**
98 * Peer's PEERSTORE Handle
99 */
100 struct GNUNET_PEERSTORE_Handle *ph;
101
102 /**
103 * Peer's transport get hello handle to retrieve peer's HELLO message
104 */
105 struct GNUNET_PEERSTORE_IterateContext *pic;
106
107 /**
108 * Hello
109 */
110 char *hello;
111
112 /**
113 * Hello size
114 */
115 size_t hello_size;
116
117 /**
118 * Peer's testing handle
119 */
120 struct GNUNET_TESTING_Peer *peer;
121
122 /**
123 * Peer identity
124 */
125 struct GNUNET_PeerIdentity id;
126
127 /**
128 * Handle for the peer's ARM process
129 */
130 struct GNUNET_OS_Process *arm_proc;
131
132 /**
133 * Receive callback
134 */
135 struct GNUNET_MQ_MessageHandler *handlers;
136
137 /**
138 * Notify connect callback
139 */
140 GNUNET_TRANSPORT_NotifyConnect nc;
141
142 /**
143 * Notify disconnect callback
144 */
145 GNUNET_TRANSPORT_NotifyDisconnect nd;
146
147 /**
148 * Startup completed callback
149 */
150 GNUNET_SCHEDULER_TaskCallback start_cb;
151
152 /**
153 * Closure for the @a nc and @a nd callbacks
154 */
155 void *cb_cls;
156
157 /**
158 * Closure for @e start_cb.
159 */
160 void *start_cb_cls;
161
162 /**
163 * An unique number to identify the peer
164 */
165 unsigned int no;
166};
167
42 168
43/** 169/**
44 * @brief Function signature for callbacks that are called when new 170 * Handle for a request to connect two peers.
45 * backchannel message arrived 171 */
172struct GNUNET_TRANSPORT_TESTING_ConnectRequest
173{
174 /**
175 * Kept in a DLL.
176 */
177 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *next;
178
179 /**
180 * Kept in a DLL.
181 */
182 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *prev;
183
184 /**
185 * Peer we want to connect.
186 */
187 struct GNUNET_TRANSPORT_TESTING_PeerContext *p1;
188
189 /**
190 * Peer we want to connect.
191 */
192 struct GNUNET_TRANSPORT_TESTING_PeerContext *p2;
193
194 /**
195 * Task by which we accomplish the connection.
196 */
197 struct GNUNET_SCHEDULER_Task *tct;
198
199 /**
200 * Handle by which we ask ATS to faciliate the connection.
201 */
202 struct GNUNET_ATS_ConnectivitySuggestHandle *ats_sh;
203
204 /**
205 * Function to call upon completion.
206 */
207 GNUNET_SCHEDULER_TaskCallback cb;
208
209 /**
210 * Closure for @e cb.
211 */
212 void *cb_cls;
213
214 /**
215 * Message queue for sending from @a p1 to @a p2.
216 */
217 struct GNUNET_MQ_Handle *mq;
218
219 /**
220 * Set if peer1 says the connection is up to peer2.
221 */
222 int p1_c;
223
224 /**
225 * Set if peer2 says the connection is up to peer1.
226 */
227 int p2_c;
228
229 /**
230 * #GNUNET_YES if both @e p1_c and @e p2_c are #GNUNET_YES.
231 */
232 int connected;
233};
234
235
236/**
237 * Handle for a test run.
238 */
239struct GNUNET_TRANSPORT_TESTING_Handle
240{
241 /**
242 * Testing library system handle
243 */
244 struct GNUNET_TESTING_System *tl_system;
245
246 /**
247 * head DLL of connect contexts
248 */
249 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc_head;
250
251 /**
252 * head DLL of connect contexts
253 */
254 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc_tail;
255
256 /**
257 * head DLL of peers
258 */
259 struct GNUNET_TRANSPORT_TESTING_PeerContext *p_head;
260
261 /**
262 * tail DLL of peers
263 */
264 struct GNUNET_TRANSPORT_TESTING_PeerContext *p_tail;
265};
266
267
268/**
269 * Initialize the transport testing
270 *
271 * @return transport testing handle
272 */
273struct GNUNET_TRANSPORT_TESTING_Handle *
274GNUNET_TRANSPORT_TESTING_init (void);
275
276
277/**
278 * Clean up the transport testing
46 * 279 *
47 * @param cls Closure 280 * @param tth transport testing handle
48 * @param msg Backchannel message
49 * @param pid Target peer
50 */ 281 */
51typedef struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle * 282void
52(*GNUNET_TRANSPORT_TESTING_BackchannelCallback)(void *cls, 283GNUNET_TRANSPORT_TESTING_done (struct GNUNET_TRANSPORT_TESTING_Handle *tth);
53 struct GNUNET_MessageHeader *msg,
54 struct GNUNET_PeerIdentity *pid);
55 284
56 285
57/** 286/**
58 * @brief Function signature for callbacks that are called when new 287 * Start a peer with the given configuration
59 * communicators become available
60 * 288 *
61 * @param cls Closure 289 * @param tth the testing handle
62 * @param tc_h Communicator handle 290 * @param cfgname configuration file
63 * @param cc Characteristics of communicator 291 * @param peer_id the peer_id
64 * @param address_prefix Prefix of the address 292 * @param handlers functions for receiving messages
293 * @param nc connect callback
294 * @param nd disconnect callback
295 * @param cb_cls closure for @a nc and @a nd callback
296 * @param start_cb start callback
297 * @param start_cb_cls closure for @a start_cb
298 * @return the peer context
65 */ 299 */
66typedef void 300struct GNUNET_TRANSPORT_TESTING_PeerContext *
67(*GNUNET_TRANSPORT_TESTING_CommunicatorAvailableCallback)(void *cls, 301GNUNET_TRANSPORT_TESTING_start_peer (
68 struct 302 struct GNUNET_TRANSPORT_TESTING_Handle *tth,
69 GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle 303 const char *cfgname,
70 *tc_h, 304 int peer_id,
71 enum 305 const struct GNUNET_MQ_MessageHandler *handlers,
72 GNUNET_TRANSPORT_CommunicatorCharacteristics 306 GNUNET_TRANSPORT_NotifyConnect nc,
73 cc, 307 GNUNET_TRANSPORT_NotifyDisconnect nd,
74 char *address_prefix); 308 void *cb_cls,
309 GNUNET_SCHEDULER_TaskCallback start_cb,
310 void *start_cb_cls);
75 311
76 312
77/** 313/**
78 * @brief Receive information about the address of a communicator. 314 * Shutdown the given peer
79 * 315 *
80 * @param cls Closure 316 * @param p the peer
81 * @param tc_h Communicator handle 317 */
82 * @param address Address represented as string 318void
83 * @param expiration Expiration 319GNUNET_TRANSPORT_TESTING_stop_peer (
84 * @param aid Aid 320 struct GNUNET_TRANSPORT_TESTING_PeerContext *pc);
85 * @param nt Network Type 321
86 */ 322
87typedef void 323/**
88(*GNUNET_TRANSPORT_TESTING_AddAddressCallback)(void *cls, 324 * Stops and restarts the given peer, sleeping (!) for 5s in between.
89 struct
90 GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle
91 *tc_h,
92 const char *address,
93 struct GNUNET_TIME_Relative
94 expiration,
95 uint32_t aid,
96 enum GNUNET_NetworkType nt);
97
98
99/**
100 * @brief Get informed about the success of a queue request.
101 * 325 *
102 * @param cls Closure 326 * @param p the peer
103 * @param tc_h Communicator handle 327 * @param restart_cb restart callback
104 * @param will_try #GNUNET_YES if communicator will try to create queue 328 * @param restart_cb_cls callback closure
329 * @return #GNUNET_OK in success otherwise #GNUNET_SYSERR
105 */ 330 */
106typedef void 331int
107(*GNUNET_TRANSPORT_TESTING_QueueCreateReplyCallback)(void *cls, 332GNUNET_TRANSPORT_TESTING_restart_peer (
108 struct 333 struct GNUNET_TRANSPORT_TESTING_PeerContext *p,
109 GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle 334 GNUNET_SCHEDULER_TaskCallback restart_cb,
110 *tc_h, 335 void *restart_cb_cls);
111 int will_try);
112 336
113 337
114/** 338/**
115 * @brief Handle opening of queue 339 * Connect the given peers and call the callback when both peers
340 * report the inbound connection. Remarks: start_peer's notify_connect
341 * callback can be called before.
116 * 342 *
117 * @param cls Closure 343 * @param p1 peer 1
118 * @param tc_h Communicator handle 344 * @param p2 peer 2
119 * @param tc_queue Handle to newly opened queue 345 * @param cb the callback to call when both peers notified that they are
346 * connected
347 * @param cls callback cls
348 * @return a connect request handle
120 */ 349 */
121typedef void 350struct GNUNET_TRANSPORT_TESTING_ConnectRequest *
122(*GNUNET_TRANSPORT_TESTING_AddQueueCallback)(void *cls, 351GNUNET_TRANSPORT_TESTING_connect_peers (
123 struct 352 struct GNUNET_TRANSPORT_TESTING_PeerContext *p1,
124 GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle 353 struct GNUNET_TRANSPORT_TESTING_PeerContext *p2,
125 *tc_h, 354 GNUNET_SCHEDULER_TaskCallback cb,
126 struct 355 void *cls);
127 GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue
128 *tc_queue,
129 size_t mtu);
130 356
131 357
132/** 358/**
133 * @brief Handle an incoming message 359 * Cancel the request to connect two peers. You MUST cancel the
360 * request if you stop the peers before the peers connected
361 * succesfully.
134 * 362 *
135 * @param cls Closure 363 * @param cc a connect request handle
136 * @param tc_h Handle to the receiving communicator
137 * @param msg Received message
138 */ 364 */
139typedef void 365void
140(*GNUNET_TRANSPORT_TESTING_IncomingMessageCallback)(void *cls, 366GNUNET_TRANSPORT_TESTING_connect_peers_cancel (
141 struct 367 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc);
142 GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle 368
143 *tc_h,
144 const char*payload,
145 size_t payload_len);
146 369
147/** 370/**
148 * @brief Handle to a transport communicator 371 * Function called on matching connect requests.
372 *
373 * @param cls closure
374 * @param cc request matching the query
149 */ 375 */
150struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle 376typedef void (*GNUNET_TRANSPORT_TESTING_ConnectContextCallback) (
377 void *cls,
378 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc);
379
380
381/**
382 * Find any connecting context matching the given pair of peers.
383 *
384 * @param p1 first peer
385 * @param p2 second peer
386 * @param cb function to call
387 * @param cb_cls closure for @a cb
388 */
389void
390GNUNET_TRANSPORT_TESTING_find_connecting_context (
391 struct GNUNET_TRANSPORT_TESTING_PeerContext *p1,
392 struct GNUNET_TRANSPORT_TESTING_PeerContext *p2,
393 GNUNET_TRANSPORT_TESTING_ConnectContextCallback cb,
394 void *cb_cls);
395
396
397/* ********************** high-level process functions *************** */
398
399
400/**
401 * Function called once the peers have been launched and
402 * connected by #GNUNET_TRANSPORT_TESTING_connect_check().
403 *
404 * @param cls closure
405 * @param num_peers size of the @a p array
406 * @param p the peers that were launched
407 */
408typedef void (*GNUNET_TRANSPORT_TESTING_ConnectContinuation) (
409 void *cls,
410 unsigned int num_peers,
411 struct GNUNET_TRANSPORT_TESTING_PeerContext *p[]);
412
413
414/**
415 * Internal data structure.
416 */
417struct GNUNET_TRANSPORT_TESTING_ConnectRequestList;
418
419/**
420 * Internal data structure.
421 */
422struct GNUNET_TRANSPORT_TESTING_InternalPeerContext;
423
424
425GNUNET_NETWORK_STRUCT_BEGIN
426struct GNUNET_TRANSPORT_TESTING_TestMessage
151{ 427{
152 /** 428 /**
153 * Clients 429 * Type is (usually) #GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE.
154 */ 430 */
155 struct MyClient *client_head; 431 struct GNUNET_MessageHeader header;
156 struct MyClient *client_tail;
157 432
158 /** 433 /**
159 * @brief Handle to the client 434 * Monotonically increasing counter throughout the test.
160 */ 435 */
161 struct GNUNET_MQ_Handle *c_mq; 436 uint32_t num GNUNET_PACKED;
437};
438GNUNET_NETWORK_STRUCT_END
439
162 440
441/**
442 * Function called by the transport for each received message.
443 *
444 * @param cls closure
445 * @param receiver receiver of the message
446 * @param sender sender of the message
447 * @param message the message
448 */
449typedef void (*GNUNET_TRANSPORT_TESTING_ReceiveCallback) (
450 void *cls,
451 struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
452 const struct GNUNET_PeerIdentity *sender,
453 const struct GNUNET_TRANSPORT_TESTING_TestMessage *message);
454
455
456/**
457 * Function called to notify transport users that another
458 * peer connected to us.
459 *
460 * @param cls closure
461 * @param me peer experiencing the event
462 * @param other peer that connected to @a me
463 */
464typedef void (*GNUNET_TRANSPORT_TESTING_NotifyConnect) (
465 void *cls,
466 struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
467 const struct GNUNET_PeerIdentity *other);
468
469
470/**
471 * Function called to notify transport users that another
472 * peer disconnected from us.
473 *
474 * @param cls closure
475 * @param me peer experiencing the event
476 * @param other peer that disconnected from @a me
477 */
478typedef void (*GNUNET_TRANSPORT_TESTING_NotifyDisconnect) (
479 void *cls,
480 struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
481 const struct GNUNET_PeerIdentity *other);
482
483
484/**
485 * Closure that must be passed to
486 * #GNUNET_TRANSPORT_TESTING_connect_check.
487 */
488struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext
489{
163 /** 490 /**
164 * @brief Handle to the configuration 491 * How should we continue after the connect?
165 */ 492 */
166 struct GNUNET_CONFIGURATION_Handle *cfg; 493 GNUNET_SCHEDULER_TaskCallback connect_continuation;
167 494
168 /** 495 /**
169 * @brief File name of configuration file 496 * Closure for @e connect_continuation.
170 */ 497 */
171 char *cfg_filename; 498 void *connect_continuation_cls;
172 499
173 struct GNUNET_PeerIdentity peer_id; 500 /**
501 * Which configuration file should we pass to the
502 * #GNUNET_PROGRAM_run() of the testcase?
503 */
504 const char *config_file;
174 505
175 /** 506 /**
176 * @brief Handle to the transport service 507 * Receiver argument to give for peers we start.
177 */ 508 */
178 struct GNUNET_SERVICE_Handle *tsh; 509 GNUNET_TRANSPORT_TESTING_ReceiveCallback rec;
179 510
180 /** 511 /**
181 * @brief Task that will be run on shutdown to stop and clean transport 512 * Notify connect argument to give for peers we start.
182 * service
183 */ 513 */
184 struct GNUNET_SCHEDULER_Task *ts_shutdown_task; 514 GNUNET_TRANSPORT_TESTING_NotifyConnect nc;
185 515
516 /**
517 * Notify disconnect argument to give for peers we start.
518 */
519 GNUNET_TRANSPORT_TESTING_NotifyDisconnect nd;
186 520
187 /** 521 /**
188 * @brief Process of the communicator 522 * Closure for @e rec, @e nc and @e nd.
189 */ 523 */
190 struct GNUNET_OS_Process *c_proc; 524 void *cls;
191 525
192 /** 526 /**
193 * NAT process 527 * Custom task to run on shutdown.
194 */ 528 */
195 struct GNUNET_OS_Process *nat_proc; 529 GNUNET_SCHEDULER_TaskCallback shutdown_task;
196 530
197 /** 531 /**
198 * statistics service process 532 * Closure for @e shutdown_task.
199 */ 533 */
200 struct GNUNET_OS_Process *statistics_proc; 534 void *shutdown_task_cls;
201 535
202 /** 536 /**
203 * resolver service process 537 * Custom task to run after peers were started but before we try to
538 * connect them. If this function is set, we wait ONE second after
539 * running this function until we continue with connecting the
540 * peers.
204 */ 541 */
205 struct GNUNET_OS_Process *resolver_proc; 542 GNUNET_SCHEDULER_TaskCallback pre_connect_task;
206 543
207 /** 544 /**
208 * peerstore service process 545 * Closure for @e shutdown_task.
209 */ 546 */
210 struct GNUNET_OS_Process *ps_proc; 547 void *pre_connect_task_cls;
211 548
212 /** 549 /**
213 * @brief Task that will be run on shutdown to stop and clean communicator 550 * When should the testcase time out?
214 */ 551 */
215 struct GNUNET_SCHEDULER_Task *c_shutdown_task; 552 struct GNUNET_TIME_Relative timeout;
216 553
217 /** 554 /**
218 * @brief Characteristics of the communicator 555 * Should we try to create connections in both directions?
219 */ 556 */
220 enum GNUNET_TRANSPORT_CommunicatorCharacteristics c_characteristics; 557 int bi_directional;
558
559 /* ******* fields set by #GNUNET_TRANSPORT_TESTING_connect_check **** */
221 560
222 /** 561 /**
223 * @brief Specifies supported addresses 562 * Number of peers involved in the test.
224 */ 563 */
225 char *c_addr_prefix; 564 unsigned int num_peers;
226 565
227 /** 566 /**
228 * @brief Specifies supported addresses 567 * Configuration files we have, array with @e num_peers entries.
229 */ 568 */
230 char *c_address; 569 char **cfg_files;
231 570
232 /** 571 /**
233 * @brief Head of the DLL of queues associated with this communicator 572 * Array with @e num_peers entries.
234 */ 573 */
235 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *queue_head; 574 struct GNUNET_TRANSPORT_TESTING_PeerContext **p;
236 575
237 /** 576 /**
238 * @brief Tail of the DLL of queues associated with this communicator 577 * Name of the plugin.
239 */ 578 */
240 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *queue_tail; 579 const char *test_plugin;
241 580
242 /* Callbacks + Closures */
243 /** 581 /**
244 * @brief Callback called when a new communicator connects 582 * Name of the testcase.
245 */ 583 */
246 GNUNET_TRANSPORT_TESTING_CommunicatorAvailableCallback 584 const char *test_name;
247 communicator_available_cb;
248 585
249 /** 586 /**
250 * @brief Callback called when a new communicator connects 587 * Configuration object for the testcase.
251 */ 588 */
252 GNUNET_TRANSPORT_TESTING_AddAddressCallback add_address_cb; 589 const struct GNUNET_CONFIGURATION_Handle *cfg;
253 590
254 /** 591 /**
255 * @brief Callback called when a new communicator connects 592 * Main testing handle.
256 */ 593 */
257 GNUNET_TRANSPORT_TESTING_QueueCreateReplyCallback queue_create_reply_cb; 594 struct GNUNET_TRANSPORT_TESTING_Handle *tth;
258 595
259 /** 596 /**
260 * @brief Callback called when a new communicator connects 597 * Result from the main function, set to #GNUNET_OK on success.
598 * Clients should set to #GNUNET_SYSERR to indicate test failure.
261 */ 599 */
262 GNUNET_TRANSPORT_TESTING_AddQueueCallback add_queue_cb; 600 int global_ret;
263 601
264 /** 602 /**
265 * @brief Callback called when a new communicator connects 603 * Generator for the `num` field in test messages. Incremented each
604 * time #GNUNET_TRANSPORT_TESTING_simple_send or
605 * #GNUNET_TRANSPORT_TESTING_large_send are used to transmit a
606 * message.
266 */ 607 */
267 GNUNET_TRANSPORT_TESTING_IncomingMessageCallback incoming_msg_cb; 608 uint32_t send_num_gen;
609
610 /* ******* internal state, clients should not mess with this **** */
268 611
269 /** 612 /**
270 * @brief Backchannel callback 613 * Task run on timeout.
271 */ 614 */
272 GNUNET_TRANSPORT_TESTING_BackchannelCallback bc_cb; 615 struct GNUNET_SCHEDULER_Task *timeout_task;
273 616
274 /** 617 /**
275 * Our service handle 618 * Task run to connect peers.
276 */ 619 */
277 struct GNUNET_SERVICE_Handle *sh; 620 struct GNUNET_SCHEDULER_Task *connect_task;
278 621
279 /** 622 /**
280 * @brief Closure to the callback 623 * Number of peers that have been started.
281 */ 624 */
282 void *cb_cls; 625 unsigned int started;
283 626
284 /** 627 /**
285 * Backchannel supported 628 * DLL of active connect requests.
286 */ 629 */
287 int bc_enabled; 630 struct GNUNET_TRANSPORT_TESTING_ConnectRequestList *crl_head;
631
632 /**
633 * DLL of active connect requests.
634 */
635 struct GNUNET_TRANSPORT_TESTING_ConnectRequestList *crl_tail;
636
637 /**
638 * Array with @e num_peers entries.
639 */
640 struct GNUNET_TRANSPORT_TESTING_InternalPeerContext *ip;
288}; 641};
289 642
643
290/** 644/**
291 * @brief Start communicator part of transport service and communicator 645 * Find peer by peer ID.
292 * 646 *
293 * @param service_name Name of the service 647 * @param ccc context to search
294 * @param cfg Configuration handle 648 * @param peer peer to look for
295 * @param communicator_available Callback that is called when a new 649 * @return NULL if @a peer was not found
296 * communicator becomes available 650 */
297 * @param add_address_cb Callback handling new addresses 651struct GNUNET_TRANSPORT_TESTING_PeerContext *
298 * @param queue_create_reply_cb Callback handling success of queue requests 652GNUNET_TRANSPORT_TESTING_find_peer (
299 * @param add_queue_cb Callback handling freshly created queues 653 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc,
300 * @param incoming_message_cb Callback handling incoming messages 654 const struct GNUNET_PeerIdentity *peer);
301 * @param cb_cls Closure to @p communicator_available 655
656
657/**
658 * Common implementation of the #GNUNET_TRANSPORT_TESTING_CheckCallback.
659 * Starts and connects the two peers, then invokes the
660 * `connect_continuation` from @a cls. Sets up a timeout to
661 * abort the test, and a shutdown handler to clean up properly
662 * on exit.
302 * 663 *
303 * @return Handle to the communicator duo 664 * @param cls closure of type `struct
304 */ 665 * GNUNET_TRANSPORT_TESTING_ConnectCheckContext`
305struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle * 666 * @param tth_ initialized testing handle
306GNUNET_TRANSPORT_TESTING_transport_communicator_service_start ( 667 * @param test_plugin_ name of the plugin
307 const char *service_name, 668 * @param test_name_ name of the test
308 const char *binary_name, 669 * @param num_peers number of entries in the @a cfg_file array
309 const char *cfg_filename, 670 * @param cfg_files array of names of configuration files for the peers
310 const struct GNUNET_PeerIdentity *peer_id, 671 * @return #GNUNET_SYSERR on error
311 GNUNET_TRANSPORT_TESTING_CommunicatorAvailableCallback 672 */
312 communicator_available_cb, 673int
313 GNUNET_TRANSPORT_TESTING_AddAddressCallback add_address_cb, 674GNUNET_TRANSPORT_TESTING_connect_check (
314 GNUNET_TRANSPORT_TESTING_QueueCreateReplyCallback queue_create_reply_cb, 675 void *cls,
315 GNUNET_TRANSPORT_TESTING_AddQueueCallback add_queue_cb, 676 struct GNUNET_TRANSPORT_TESTING_Handle *tth_,
316 GNUNET_TRANSPORT_TESTING_IncomingMessageCallback incoming_message_cb, 677 const char *test_plugin_,
317 GNUNET_TRANSPORT_TESTING_BackchannelCallback bc_cb, 678 const char *test_name_,
318 void *cb_cls); 679 unsigned int num_peers,
680 char *cfg_files[]);
681
682
683/**
684 * Main function of a testcase. Called with the initial setup data
685 * for the test as derived from the source name and the binary name.
686 *
687 * @param cls closure
688 * @param tth_ initialized testing handle
689 * @param test_plugin_ name of the plugin
690 * @param test_name_ name of the test
691 * @param num_peers number of entries in the @a cfg_file array
692 * @param cfg_files array of names of configuration files for the peers
693 * @return #GNUNET_SYSERR on error
694 */
695typedef int (*GNUNET_TRANSPORT_TESTING_CheckCallback) (
696 void *cls,
697 struct GNUNET_TRANSPORT_TESTING_Handle *tth_,
698 const char *test_plugin_,
699 const char *test_name_,
700 unsigned int num_peers,
701 char *cfg_files[]);
702
703
704/**
705 * Setup testcase. Calls @a check with the data the test needs.
706 *
707 * @param argv0 binary name (argv[0])
708 * @param filename source file name (__FILE__)
709 * @param num_peers number of peers to start
710 * @param check main function to run
711 * @param check_cls closure for @a check
712 * @return #GNUNET_OK on success
713 */
714int
715GNUNET_TRANSPORT_TESTING_main_ (const char *argv0,
716 const char *filename,
717 unsigned int num_peers,
718 GNUNET_TRANSPORT_TESTING_CheckCallback check,
719 void *check_cls);
720
721
722/**
723 * Setup testcase. Calls @a check with the data the test needs.
724 *
725 * @param num_peers number of peers to start
726 * @param check main function to run
727 * @param check_cls closure for @a check
728 * @return #GNUNET_OK on success
729 */
730#define GNUNET_TRANSPORT_TESTING_main(num_peers, check, check_cls) \
731 GNUNET_TRANSPORT_TESTING_main_ (argv[0], \
732 __FILE__, \
733 num_peers, \
734 check, \
735 check_cls)
736
737/* ***************** Convenience functions for sending ********* */
738
739/**
740 * Send a test message of type @a mtype and size @a msize from
741 * peer @a sender to peer @a receiver. The peers should be
742 * connected when this function is called.
743 *
744 * @param sender the sending peer
745 * @param receiver the receiving peer
746 * @param mtype message type to use
747 * @param msize size of the message, at least `sizeof (struct
748 * GNUNET_TRANSPORT_TESTING_TestMessage)`
749 * @param num unique message number
750 * @param cont continuation to call after transmission
751 * @param cont_cls closure for @a cont
752 * @return #GNUNET_OK if message was queued,
753 * #GNUNET_NO if peers are not connected
754 * #GNUNET_SYSERR if @a msize is illegal
755 */
756int
757GNUNET_TRANSPORT_TESTING_send (
758 struct GNUNET_TRANSPORT_TESTING_PeerContext *sender,
759 struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
760 uint16_t mtype,
761 uint16_t msize,
762 uint32_t num,
763 GNUNET_SCHEDULER_TaskCallback cont,
764 void *cont_cls);
765
766
767/**
768 * Message type used by #GNUNET_TRANSPORT_TESTING_simple_send().
769 */
770#define GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE 12345
771
772/**
773 * Alternative message type for tests.
774 */
775#define GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE2 12346
319 776
320 777
778/**
779 * Type of the closure argument to pass to
780 * #GNUNET_TRANSPORT_TESTING_simple_send() and
781 * #GNUNET_TRANSPORT_TESTING_large_send().
782 */
783struct GNUNET_TRANSPORT_TESTING_SendClosure
784{
785 /**
786 * Context for the transmission.
787 */
788 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
789
790 /**
791 * Function that returns the desired message size. Overrides
792 * the message size, can be NULL in which case the message
793 * size is the default.
794 */
795 size_t (*get_size_cb) (unsigned int n);
796
797 /**
798 * Number of messages to be transmitted in a loop.
799 * Use zero for "forever" (until external shutdown).
800 */
801 unsigned int num_messages;
802
803 /**
804 * Function to call after all transmissions, can be NULL.
805 */
806 GNUNET_SCHEDULER_TaskCallback cont;
807
808 /**
809 * Closure for @e cont.
810 */
811 void *cont_cls;
812};
813
814
815/**
816 * Task that sends a minimalistic test message from the
817 * first peer to the second peer.
818 *
819 * @param cls the `struct GNUNET_TRANSPORT_TESTING_SendClosure`
820 * which should contain at least two peers, the first two
821 * of which should be currently connected
822 */
321void 823void
322GNUNET_TRANSPORT_TESTING_transport_communicator_service_stop ( 824GNUNET_TRANSPORT_TESTING_simple_send (void *cls);
323 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h);
324 825
826/**
827 * Size of a message sent with
828 * #GNUNET_TRANSPORT_TESTING_large_send(). Big enough
829 * to usually force defragmentation.
830 */
831#define GNUNET_TRANSPORT_TESTING_LARGE_MESSAGE_SIZE 2600
325 832
326/** 833/**
327 * @brief Instruct communicator to open a queue 834 * Task that sends a large test message from the
835 * first peer to the second peer.
328 * 836 *
329 * @param tc_h Handle to communicator which shall open queue 837 * @param cls the `struct GNUNET_TRANSPORT_TESTING_SendClosure`
330 * @param peer_id Towards which peer 838 * which should contain at least two peers, the first two
331 * @param address For which address 839 * of which should be currently connected
332 */ 840 */
333void 841void
334GNUNET_TRANSPORT_TESTING_transport_communicator_open_queue (struct 842GNUNET_TRANSPORT_TESTING_large_send (void *cls);
335 GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle 843
336 *tc_h, 844
337 const struct 845/* ********************** log-only convenience functions ************* */
338 GNUNET_PeerIdentity
339 *peer_id,
340 const char *address);
341 846
342 847
343/** 848/**
344 * @brief Instruct communicator to send data 849 * Log a connect event.
345 * 850 *
346 * @param tc_queue The queue to use for sending 851 * @param cls NULL
347 * @param cont function to call when done sending 852 * @param me peer that had the event
348 * @param cont_cls closure for @a cont 853 * @param other peer that connected.
349 * @param payload Data to send
350 * @param payload_size Size of the @a payload
351 */ 854 */
352void 855void
353GNUNET_TRANSPORT_TESTING_transport_communicator_send (struct 856GNUNET_TRANSPORT_TESTING_log_connect (
354 GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle 857 void *cls,
355 *tc_h, 858 struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
356 GNUNET_SCHEDULER_TaskCallback 859 const struct GNUNET_PeerIdentity *other);
357 cont, 860
358 void *cont_cls, 861
359 const void *payload, 862/**
360 size_t payload_size); 863 * Log a disconnect event.
864 *
865 * @param cls NULL
866 * @param me peer that had the event
867 * @param other peer that disconnected.
868 */
869void
870GNUNET_TRANSPORT_TESTING_log_disconnect (
871 void *cls,
872 struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
873 const struct GNUNET_PeerIdentity *other);
874
875
876/* ********************** low-level filename functions *************** */
877
878
879/**
880 * Extracts the test filename from an absolute file name and removes
881 * the extension.
882 *
883 * @param file absolute file name
884 * @return resulting test name
885 */
886char *
887GNUNET_TRANSPORT_TESTING_get_test_name (const char *file);
888
889
890/**
891 * This function takes the filename (e.g. argv[0), removes a "lt-"-prefix and
892 * if existing ".exe"-prefix and adds the peer-number
893 *
894 * @param file filename of the test, e.g. argv[0]
895 * @param count peer number
896 * @return configuration name to use
897 */
898char *
899GNUNET_TRANSPORT_TESTING_get_config_name (const char *file, int count);
900
901
902/**
903 * Extracts the plugin anme from an absolute file name and the test name
904 * @param file absolute file name
905 * @param test test name
906 * @return the plugin name
907 */
908char *
909GNUNET_TRANSPORT_TESTING_get_test_plugin_name (const char *executable,
910 const char *testname);
911
912
913/**
914 * Extracts the filename from an absolute file name and removes the
915 * extenstion
916 *
917 * @param file absolute file name
918 * @return the source name
919 */
920char *
921GNUNET_TRANSPORT_TESTING_get_test_source_name (const char *file);
922
923#endif
924/* end of transport_testing.h */
diff --git a/src/util/Makefile.am b/src/util/Makefile.am
index c5059bbb1..d2e4b4d1a 100644
--- a/src/util/Makefile.am
+++ b/src/util/Makefile.am
@@ -138,7 +138,7 @@ libgnunetutil_la_LIBADD = \
138 138
139libgnunetutil_la_LDFLAGS = \ 139libgnunetutil_la_LDFLAGS = \
140 $(GN_LIB_LDFLAGS) \ 140 $(GN_LIB_LDFLAGS) \
141 -version-info 13:2:0 141 -version-info 13:2:1
142 142
143if HAVE_TESTING 143if HAVE_TESTING
144 GNUNET_ECC = gnunet-ecc 144 GNUNET_ECC = gnunet-ecc
@@ -169,7 +169,7 @@ noinst_PROGRAMS = \
169 169
170if ENABLE_TEST_RUN 170if ENABLE_TEST_RUN
171AM_TESTS_ENVIRONMENT=export GNUNET_PREFIX=$${GNUNET_PREFIX:-@libdir@};export PATH=$${GNUNET_PREFIX:-@prefix@}/bin:$$PATH;unset XDG_DATA_HOME;unset XDG_CONFIG_HOME; 171AM_TESTS_ENVIRONMENT=export GNUNET_PREFIX=$${GNUNET_PREFIX:-@libdir@};export PATH=$${GNUNET_PREFIX:-@prefix@}/bin:$$PATH;unset XDG_DATA_HOME;unset XDG_CONFIG_HOME;
172TESTS = $(check_PROGRAMS) 172TESTS = $(check_PROGRAMS) $(check_SCRIPTS)
173endif 173endif
174 174
175gnunet_timeout_SOURCES = \ 175gnunet_timeout_SOURCES = \
@@ -195,7 +195,7 @@ gnunet_crypto_tvg_SOURCES = \
195 gnunet-crypto-tvg.c 195 gnunet-crypto-tvg.c
196gnunet_crypto_tvg_LDADD = \ 196gnunet_crypto_tvg_LDADD = \
197 libgnunetutil.la \ 197 libgnunetutil.la \
198 $(GN_LIBINTL) -lgcrypt 198 $(GN_LIBINTL) -lgcrypt -ljansson
199 199
200gnunet_ecc_SOURCES = \ 200gnunet_ecc_SOURCES = \
201 gnunet-ecc.c 201 gnunet-ecc.c
@@ -255,6 +255,9 @@ if HAVE_SSH_KEY
255# SSH_USING_TESTS = test_socks.nc 255# SSH_USING_TESTS = test_socks.nc
256endif 256endif
257 257
258check_SCRIPTS = \
259 test_crypto_vectors.sh
260
258check_PROGRAMS = \ 261check_PROGRAMS = \
259 test_bio \ 262 test_bio \
260 test_client.nc \ 263 test_client.nc \
diff --git a/src/util/crypto-test-vectors.json b/src/util/crypto-test-vectors.json
new file mode 100644
index 000000000..972b62c3e
--- /dev/null
+++ b/src/util/crypto-test-vectors.json
@@ -0,0 +1,56 @@
1{
2 "encoding": "base32crockford",
3 "producer": "GNUnet 0.14.0 git-64ad3b0a1",
4 "vectors": [
5 {
6 "operation": "hash",
7 "input": "91JPRV3F5GG4EKJNDSJQ8",
8 "output": "D0R24RZ1TPASVQ2NY56CT8AJDYZE9ZGDB0GVZ05E9D4YGZQW2RC5YFPQ0Q86EPW836DY7VYQTNFFJT3ZR2K508F4JVS5JNJKYN2MMFR"
9 },
10 {
11 "operation": "ecc_ecdh",
12 "priv1": "TFA439C75RT2JK9V6GTRRXH3QR3QC4SVJ1KBQ4MNY3S338GT6T50",
13 "pub1": "E19WJDA83485BC8EC8RV7FTAK86BESJG1YNYRENEC0JV7XEZ7M80",
14 "priv2": "2DT3B0TMY6VVP56YZKG5ASRSQAEV0GB4QMT9N6CTPDARNJ905APG",
15 "skm": "GY63DCHR6BGV2AKDM44V7A4H4DA0WJC7D5C2R7DXTWC9D83H7XM0PEQKKZ1K2HWMWBSBNPDWXDN7PA1R1WJKVQ2RDTNF1PBXCFHM9QR"
16 },
17 {
18 "operation": "eddsa_key_derivation",
19 "priv": "8QC2VNF8443S5KPNKMB4XMV58BTHWAKZ7SVW5WG3KRB37567XS90",
20 "pub": "3M9KK1WSNM1RTY5P72HKFA264V4B7MVHVJ08Y90CV06DYHV8XPP0"
21 },
22 {
23 "operation": "eddsa_signing",
24 "priv": "5077XJR9AMH4T97ACKFBVBJD0KFENHPV66B2Y1JBSKXBJKNZJ4E0",
25 "pub": "6E2F03JJ8AEDANTTZZ4SBZDFEEZSF8A9DVGTS6VFBCVZQYQ46RRG",
26 "data": "00000300000000000000",
27 "sig": "XCNJGJ96WPDH60YVMH6C74NGQSGJE3BC1TYMGX6BHY5DMZZZKTB373QTXJ507K5EBSG9YS2EYKHCX3ATRQ6P5MY9MXC4ZB1XSZ2X23G"
28 },
29 {
30 "operation": "kdf",
31 "salt": "94KPT83PCNS7J83KC5P78Y8",
32 "ikm": "94KPT83MD1JJ0WV5CDS6AX10D5Q70XBM41NPAY90DNGQ8SBJD5GPR",
33 "ctx": "94KPT83141HPYVKMCNW78833D1TPWTSC41GPRWVF41NPWVVQDRG62WS04XMPWSKF4WG6JVH0EHM6A82J8S1G",
34 "out_len %u\n": 64,
35 "out": "GTMR4QT05Z9WF5HKVG0WK9RPXGHSMHJNW377G9GJXCA8B0FEKPF4D27RJMSJZYWSQNTBJ5EYVV7ZW18B48Z0JVJJ80RHB706Y96Q358"
36 },
37 {
38 "operation": "eddsa_ecdh",
39 "priv_ecdhe": "5FBRFZY942H2PD96NFNYWZKYXCRFY11JWQ59V7G9B4M8EX1KE100",
40 "pub_ecdhe": "GDX7FC01AZYMG0BY0AMHR6E7KCGX9F6SWES16WZ1QWZ2VSYXKH00",
41 "priv_eddsa": "3HYHM9DZQ3D61APDQNBCSKJE452YEP6JK01DWR1J3VZAASFEA570",
42 "pub_eddsa": "87N7PFAHBX97HRE8XYW8KYN64YZDF4FCBR2BZ5SZN3QE3D2BF0R0",
43 "key_material": "QH0RAXXC9RYDEAXKNTAWM0WXJS25RS67H5T252EGA22RA6JTYRFDAEK8CJY85QSYWGYHQXK5Y1SWSRB3P0NXNXYP237EXMXQ3P2WE00"
44 },
45 {
46 "operation": "rsa_blind_signing",
47 "message_hash": "XKQMJ4CNTXBFE1V2WR6JS063J7PZQE4XMB5JH3RS5X0THQ1JQSQ69Y7KDBC9TYRJEZH48MEPY2SF4QHQ4VHXC0YQX5935MQEGP0AX6R",
48 "rsa_public_key": "040000YRN1NVJ68RS6RJF52PGRCQG19ZKWQPSTJX2G7ZDCKSZFE2VW3HHA81YF5C639JHJF5TX8YTEE2FW2WQCG1PTKNBSPPJEJGA032CN3E8QZ27VWY0K6JFT8ZSYWRH2SKDMXW56A4QKY46JJBWJ6T0ZRVBW6S1HTHXVE2RW8MXRW5T801077MDY13N5F8Z1JZVKBJ06TK3S0YPEDBXK0VEHRHEQJ5X5XYKR4KQTFAZNBMKXY8836VCHBXTK4YNX6AJ1CK29SMJH3Z3QRM16A2TNQGFR0HSMV446BF7FMT2E379ZAT5ST4G3BM2NWZYW545S2SW5MG5S6M88XZZ7SKFD48YVXNZ205GGSEYJPVBMR76WG4ZG30WBCPC1N54XE12RMAG81D8C09WG22PKGGDHYXX68N04002",
49 "rsa_private_key": "50W3MTV5F4PP8RBMC4520A1H60X70XB2DHMP6BBBCNWGM81050SKMWKKC452081050RKMVHJ6MVKM06RN1NVJ68RS6RJF52PGRCQG19ZKWQPSTJX2G7ZDCKSZFE2VW3HHA81YF5C639JHJF5TX8YTEE2FW2WQCG1PTKNBSPPJEJGA032CN3E8QZ27VWY0K6JFT8ZSYWRH2SKDMXW56A4QKY46JJBWJ6T0ZRVBW6S1HTHXVE2RW8MXRW5T801077MDY13N5F8Z1JZVKBJ06TK3S0YPEDBXK0VEHRHEQJ5X5XYKR4KQTFAZNBMKXY8836VCHBXTK4YNX6AJ1CK29SMJH3Z3QRM16A2TNQGFR0HSMV446BF7FMT2E379ZAT5ST4G3BM2NWZYW545S2SW5MG5S6M88XZZ7SKFD48YVXNZ205GGSEYJPVBMR76WG4ZG30WBCPC1N54XE12RMAG81D8C09WG22PKGGDHYXX68N5452081050RKMS9K780G009918G2081918G20A8A40M32C9TE1S6JXK1EHJJTTV5F45208186CX74WV118G2081864X6WCHN6WX01P58DEWHJ669P4KS8NM635W0AFWZ5XPEMQ8M1ZVB4YFVVGPZ0WCAJ0FKSB1GTCMCKSEQA7PKKGKZ0Q5V40DPMXAYDNMKMM2G0RK58VJ5ZRHYZ7G4SMKYJ7YFQ648PCVD7F19JH5WZH1MMJZ4HPG7Y6TZ1P8CEMFEVGP7257E71EJ0081SX3FG8X9BT7RCQYWTWG1PMRY87NKKAZCR6VME4BNWHF9FFMY14XYKTQXAX4ZFJ20SPV4AZEMS7NF9JMGB4RJED4M8ZRXY509JGPNDW3Y04ED6S11JVSVX6GKGSTFTPHEEH40TX0NF7ZQ191E8PF1D41E9N227FZSYCVV927PZDFRG1C46BQMNPTX61SQ417W0R72V5K0D997BG8P52M20BA302F40GNMW43CFQF9J59918G2081864X6ACST04002A8A40G20A1H79J34D9P78SBPDS45EQZVX2D6R1GZ6FHKHDG4ABYZ6FCPSMMGZ29ZY5NS8B304D0QBD800A5SM3D8S92WKS3KFWFXE8X597TNJBR94BGNCFHY8TV63KQZ0RFMRTPHFA2XNFX2V4KECZNTHGKZNJTWH8051XP6FJJ6700A5KWMH24CK1KF9XVH2ATQJ4F1ZJZQM00A00ESTVKM5N58K5FN5TW34B1H1Q1CWRZXJ9QMBG0QKHXHZMFYZSN9ERV59935716NGSX9GBB3R9BY32TBTAJ7K0N391ATSE5263X5NKVDH7XCF1PF0WRTTJYBAJBQWVGJ8P7RGVWB27Q3S4AJJ55FS2T9K341EHATHJZQQTR0JWP5E4NQHR4GN60QMK2SY0VJM56N0NHJXRGAGJW3S9ABJ7M4TAV6YRJJ2H040G2GC9TE0RK4E9T03CET00DAJDXJ1NF511J69JMDDSVARPSFSS6AJTRMVXD10NVA4Z162ER74WNH0J6H2RTA9C9MZM4TEBP8P7AF0NANBH4XZJCNDZV7BEAMJ6R363GHZKGBR0HH1SATGWNJWGW2961MRFPX7S0RJK7CWYE7EX1XQ2683TENRPDKV29D1F1RNDD6P04MZTKSH5YMEZPKGEDRJZXXM9918G2081864X72C9J74X01ZXF256C681JDWRRG7T4AC9JRHP54YM65X9AT17YGC3G4569ZKBQEZ219Y7JG4V10F4AW7Q1XKGPTW3Y3X0WCTB06HM249Q094VHEYD4BTEQ2DF334W8G3VEB7N1CHCMPZEVVTR0XWQSNV6MBJRF5MBY6Z148YA36YA61EXA28PHE3KSN1M4HVF7B06JD736TSK9GC9CAG8F0MMGM81040M32EKN64S3JEG0YCC3PHPNBZAGQ2WE8HJHM28B049N2MQ94G8VPXNHG43C6ADB44DE1DK8ASMYTSXZWMAYKN1SS2GVFTVH8Q4N3TTJ3BKA61Y93QE17XNRF3S0CSSQTY3SVVAFHDA9APFFSMRYD67N4DXQ1X42NA270VF6H0MCRQ87JKVBQAB0CH2WH7RHYMCQ4KN50M2NEFE1YA6F2N2DG7Z9JA8A40G20A8A40G2J2H054500",
50 "blinding_key_secret": "3SWF49XZPHQMENTSBZQR7Z0B8ZSZ2JRARE79Q4VXZMQ7W6QABXMG",
51 "blinded_message": "3KHKZJZ30ABB4E56MA2V0EQWGCWH0QQG9P2ZHYHR186C5HZXJMM4N9WXAQTKS94QSV9Y17GGNXN5MB1PZZFG7Q0FY88QPKKRG4MYCPSMTZK5W59R0MJVNJ4P4AQM96TDG5W7RV8GSNR1QQZ1GNHW3CX6D6ZRTMXB2NKB5SSYTDJS79F5ZFBRZ4HVED9JBBPWSR79KVV5QQ4APBGHBCKGMF9NJJS53A1BVYHDEVYAGFYF2SNEP827ZP50FKJ5GKGV8NQ15ESEZ69AT7GJG0T3TZVENY2YN9CVR98W3BKEZ53J7VTANARG8SJS8AMJQ7S23P5HRJ7XE9KTNRNXKH49MXV9JHHYE5535N7AGWEKR47SBCGNF44Z7XJ9RV5BQV12ZRJKN4HBZQHDNCMH3QKX9Z6G64",
52 "blinded_sig": "ND1V807BK0G73SDXN582BP3Q21MWF4A76EGWD0KA3XGJAWPSVHNHKA44931ZRB9M76SYAFD8ZPTG3A7FH5G2CWGX76VXTCDX5XNRW7EEBNMPDAQ0ZEKF6AHP872SKCGRH89SK4NGC57M8BRA3ZRPDDT9XCBG3XY02VQH4Z0F39DPBS48K0EBMK7B9S3X6QDNR5ND5MV0G7G7T3VPKZRW94MQBKPY1T6K53MQGG4PV81D9YEWNRM3WE04NNQREYDA5ETVDWQ5ZCYV9HF4ZCMWVVGWDBDH732JA3NKZ2B8QK0E6XS0Y4GGGQJS6HFQ4PATGK3TS5GHJEPDF3A6XAFNJQV99CSJW7V1NC504NTQ5NJ8KAVC1758MBBV3SS2BND4YHF0Y4NWJNVH3STV166YWFKR8W",
53 "sig": "EC5MVSPGQMM96N2VT4R2G5104E61V8RY4PR6AK9F614TVVEN7D152T0DP97CDTRDDSQGBV4GZWXQPM90SW30R2RAKKHNDCXHQFAMRSW1XCBEKVKBGC6FP0AQY9S37NVR01VJ2WVX8PN29H2ZFFQBQ9JK96GTJZ3B7DD583S8Y93GH5KWEM41CZJ73QCRT1A2AGVXX5ACFR0T448MC81QB4EGCKP5Z96VCX6RPDD5S9A4295M0E9PPQJCN5G5JKWKG17HWEDF4A26ZMD8YW27EQBZ69GSEZX4PWEV7AXFGG5X0RPKCQEPCX7XDY6NXJ1E2FZBX259RDRCFNDAZS80T0DHD9NVE73QDDESZYEZTM1TM669GHPN8AF4QV8DNW7SFZZKJ67FWR8CZC0PWTEN4ZPTRM"
54 }
55 ]
56}
diff --git a/src/util/crypto_rsa.c b/src/util/crypto_rsa.c
index d5eca6225..fec4d13db 100644
--- a/src/util/crypto_rsa.c
+++ b/src/util/crypto_rsa.c
@@ -262,8 +262,8 @@ GNUNET_CRYPTO_rsa_private_key_decode (const void *buf,
262 * @return NULL on error, otherwise the public key 262 * @return NULL on error, otherwise the public key
263 */ 263 */
264struct GNUNET_CRYPTO_RsaPublicKey * 264struct GNUNET_CRYPTO_RsaPublicKey *
265GNUNET_CRYPTO_rsa_private_key_get_public (const struct 265GNUNET_CRYPTO_rsa_private_key_get_public (
266 GNUNET_CRYPTO_RsaPrivateKey *priv) 266 const struct GNUNET_CRYPTO_RsaPrivateKey *priv)
267{ 267{
268 struct GNUNET_CRYPTO_RsaPublicKey *pub; 268 struct GNUNET_CRYPTO_RsaPublicKey *pub;
269 gcry_mpi_t ne[2]; 269 gcry_mpi_t ne[2];
diff --git a/src/util/gnunet-crypto-tvg.c b/src/util/gnunet-crypto-tvg.c
index c3fead62e..8949f427c 100644
--- a/src/util/gnunet-crypto-tvg.c
+++ b/src/util/gnunet-crypto-tvg.c
@@ -22,11 +22,36 @@
22 * @file util/gnunet-crypto-tgv.c 22 * @file util/gnunet-crypto-tgv.c
23 * @brief Generate test vectors for cryptographic operations. 23 * @brief Generate test vectors for cryptographic operations.
24 * @author Florian Dold 24 * @author Florian Dold
25 *
26 * Note that this program shouldn't depend on code in src/json/,
27 * so we're using raw jansson and no GNUnet JSON helpers.
28 *
29 * Test vectors have the following format (TypeScript pseudo code):
30 *
31 * interface TestVectorFile {
32 * encoding: "base32crockford";
33 * producer?: string;
34 * vectors: TestVector[];
35 * }
36 *
37 * enum Operation {
38 * Hash("hash"),
39 * ...
40 * }
41 *
42 * interface TestVector {
43 * operation: Operation;
44 * // Inputs for the operation
45 * [ k: string]: string | number;
46 * };
47 *
48 *
25 */ 49 */
26#include "platform.h" 50#include "platform.h"
27#include "gnunet_util_lib.h" 51#include "gnunet_util_lib.h"
28#include "gnunet_signatures.h" 52#include "gnunet_signatures.h"
29#include "gnunet_testing_lib.h" 53#include "gnunet_testing_lib.h"
54#include <jansson.h>
30#include <gcrypt.h> 55#include <gcrypt.h>
31 56
32GNUNET_NETWORK_STRUCT_BEGIN 57GNUNET_NETWORK_STRUCT_BEGIN
@@ -46,46 +71,660 @@ GNUNET_NETWORK_STRUCT_END
46 71
47 72
48/** 73/**
49 * Print data base32-crockford with a preceding label. 74 * Should we verify or output test vectors?
75 */
76static int verify_flag = GNUNET_NO;
77
78
79/**
80 * Global exit code.
81 */
82static int global_ret = 0;
83
84
85/**
86 * Create a fresh test vector for a given operation label.
87 *
88 * @param vecs array of vectors to append the new vector to
89 * @param vecname label for the operation of the vector
90 * @returns the fresh test vector
91 */
92static json_t *
93vec_for (json_t *vecs, const char *vecname)
94{
95 json_t *t = json_object ();
96
97 json_object_set_new (t,
98 "operation",
99 json_string (vecname));
100 json_array_append_new (vecs, t);
101 return t;
102}
103
104
105/**
106 * Add a base32crockford encoded value
107 * to a test vector.
50 * 108 *
51 * @param label label to print 109 * @param vec test vector to add to
52 * @param data data to print 110 * @param label label for the value
111 * @param data data to add
53 * @param size size of data 112 * @param size size of data
54 */ 113 */
55static void 114static void
56display_data (char *label, void *data, size_t size) 115d2j (json_t *vec,
116 const char *label,
117 const void *data,
118 size_t size)
57{ 119{
58 char *enc = GNUNET_STRINGS_data_to_string_alloc (data, size); 120 char *buf;
59 printf ("%s %s\n", label, enc); 121 json_t *json;
60 GNUNET_free (enc);
61}
62 122
123 buf = GNUNET_STRINGS_data_to_string_alloc (data, size);
124 json = json_string (buf);
125 GNUNET_free (buf);
126 GNUNET_break (NULL != json);
127
128 json_object_set_new (vec, label, json);
129}
63 130
64/** 131/**
65 * Main function that will be run. 132 * Add a number to a test vector.
66 * 133 *
67 * @param cls closure 134 * @param vec test vector to add to
68 * @param args remaining command-line arguments 135 * @param label label for the value
69 * @param cfgfile name of the configuration file used (for saving, can be NULL!) 136 * @param data data to add
70 * @param cfg configuration 137 * @param size size of data
71 */ 138 */
72static void 139static void
73run (void *cls, 140uint2j (json_t *vec,
74 char *const *args, 141 const char *label,
75 const char *cfgfile, 142 unsigned int num)
76 const struct GNUNET_CONFIGURATION_Handle *cfg) 143{
144 json_t *json = json_integer (num);
145
146 json_object_set_new (vec, label, json);
147}
148
149
150static int
151expect_data_fixed (json_t *vec,
152 const char *name,
153 void *data,
154 size_t expect_len)
155{
156 const char *s = json_string_value (json_object_get (vec, name));
157
158 if (NULL == s)
159 return GNUNET_NO;
160
161 if (GNUNET_OK != GNUNET_STRINGS_string_to_data (s,
162 strlen (s),
163 data,
164 expect_len))
165 return GNUNET_NO;
166 return GNUNET_OK;
167}
168
169static int
170expect_data_dynamic (json_t *vec,
171 const char *name,
172 void **data,
173 size_t *ret_len)
174{
175 const char *s = json_string_value (json_object_get (vec, name));
176 size_t len;
177
178 if (NULL == s)
179 return GNUNET_NO;
180
181 len = (strlen (s) * 5) / 8;
182 if (NULL != ret_len)
183 *ret_len = len;
184 *data = GNUNET_malloc (len);
185
186 if (GNUNET_OK != GNUNET_STRINGS_string_to_data (s, strlen (s), *data, len))
187 return GNUNET_NO;
188 return GNUNET_OK;
189}
190
191
192/**
193 * Check a single vector.
194 *
195 * @param operation operator of the vector
196 * @param vec the vector, a JSON object.
197 *
198 * @returns GNUNET_OK if the vector is okay
199 */
200static int
201checkvec (const char *operation,
202 json_t *vec)
203{
204 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
205 "checking %s\n", operation);
206
207 if (0 == strcmp (operation, "hash"))
208 {
209 void *data;
210 size_t data_len;
211 struct GNUNET_HashCode hash_out;
212 struct GNUNET_HashCode hc;
213
214 if (GNUNET_OK != expect_data_dynamic (vec,
215 "input",
216 &data,
217 &data_len))
218 {
219 GNUNET_break (0);
220 return GNUNET_SYSERR;
221 }
222 if (GNUNET_OK != expect_data_fixed (vec,
223 "output",
224 &hash_out,
225 sizeof (hash_out)))
226 {
227 GNUNET_break (0);
228 return GNUNET_NO;
229 }
230
231 GNUNET_CRYPTO_hash (data, data_len, &hc);
232
233 if (0 != GNUNET_memcmp (&hc, &hash_out))
234 {
235 GNUNET_break (0);
236 return GNUNET_NO;
237 }
238 }
239 else if (0 == strcmp (operation, "ecc_ecdh"))
240 {
241 struct GNUNET_CRYPTO_EcdhePrivateKey priv1;
242 struct GNUNET_CRYPTO_EcdhePublicKey pub1;
243 struct GNUNET_CRYPTO_EcdhePrivateKey priv2;
244 struct GNUNET_HashCode skm;
245 struct GNUNET_HashCode skm_comp;
246
247 if (GNUNET_OK != expect_data_fixed (vec,
248 "priv1",
249 &priv1,
250 sizeof (priv1)))
251 {
252 GNUNET_break (0);
253 return GNUNET_NO;
254 }
255 if (GNUNET_OK != expect_data_fixed (vec,
256 "priv2",
257 &priv2,
258 sizeof (priv2)))
259 {
260 GNUNET_break (0);
261 return GNUNET_NO;
262 }
263 if (GNUNET_OK != expect_data_fixed (vec,
264 "pub1",
265 &pub1,
266 sizeof (pub1)))
267 {
268 GNUNET_break (0);
269 return GNUNET_NO;
270 }
271 if (GNUNET_OK != expect_data_fixed (vec,
272 "skm",
273 &skm,
274 sizeof (skm)))
275 {
276 GNUNET_break (0);
277 return GNUNET_NO;
278 }
279 GNUNET_assert (GNUNET_OK ==
280 GNUNET_CRYPTO_ecc_ecdh (&priv2,
281 &pub1,
282 &skm_comp));
283 if (0 != GNUNET_memcmp (&skm, &skm_comp))
284 {
285 GNUNET_break (0);
286 return GNUNET_NO;
287 }
288 }
289 else if (0 == strcmp (operation, "eddsa_key_derivation"))
290 {
291 struct GNUNET_CRYPTO_EddsaPrivateKey priv;
292 struct GNUNET_CRYPTO_EddsaPublicKey pub;
293 struct GNUNET_CRYPTO_EddsaPublicKey pub_comp;
294
295 if (GNUNET_OK != expect_data_fixed (vec,
296 "priv",
297 &priv,
298 sizeof (priv)))
299 {
300 GNUNET_break (0);
301 return GNUNET_NO;
302 }
303
304 if (GNUNET_OK != expect_data_fixed (vec,
305 "pub",
306 &pub,
307 sizeof (pub)))
308 {
309 GNUNET_break (0);
310 return GNUNET_NO;
311 }
312
313 GNUNET_CRYPTO_eddsa_key_get_public (&priv,
314 &pub_comp);
315 if (0 != GNUNET_memcmp (&pub, &pub_comp))
316 {
317 GNUNET_break (0);
318 return GNUNET_NO;
319 }
320
321 }
322 else if (0 == strcmp (operation, "eddsa_signing"))
323 {
324 struct GNUNET_CRYPTO_EddsaPrivateKey priv;
325 struct GNUNET_CRYPTO_EddsaPublicKey pub;
326 struct TestSignatureDataPS data = { 0 };
327 struct GNUNET_CRYPTO_EddsaSignature sig;
328 struct GNUNET_CRYPTO_EddsaSignature sig_comp;
329
330 if (GNUNET_OK != expect_data_fixed (vec,
331 "priv",
332 &priv,
333 sizeof (priv)))
334 {
335 GNUNET_break (0);
336 return GNUNET_NO;
337 }
338
339 if (GNUNET_OK != expect_data_fixed (vec,
340 "pub",
341 &pub,
342 sizeof (pub)))
343 {
344 GNUNET_break (0);
345 return GNUNET_NO;
346 }
347
348 if (GNUNET_OK != expect_data_fixed (vec,
349 "data",
350 &data,
351 sizeof (data)))
352 {
353 GNUNET_break (0);
354 return GNUNET_NO;
355 }
356
357 if (GNUNET_OK != expect_data_fixed (vec,
358 "sig",
359 &sig,
360 sizeof (sig)))
361 {
362 GNUNET_break (0);
363 return GNUNET_NO;
364 }
365
366 GNUNET_CRYPTO_eddsa_sign (&priv,
367 &data,
368 &sig_comp);
369 GNUNET_assert (GNUNET_OK ==
370 GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_TEST,
371 &data,
372 &sig,
373 &pub));
374 if (0 != GNUNET_memcmp (&sig, &sig_comp))
375 {
376 GNUNET_break (0);
377 return GNUNET_NO;
378 }
379 }
380 else if (0 == strcmp (operation, "kdf"))
381 {
382 size_t out_len;
383 void *out;
384 size_t out_len_comp;
385 void *out_comp;
386 void *ikm;
387 size_t ikm_len;
388 void *salt;
389 size_t salt_len;
390 void *ctx;
391 size_t ctx_len;
392
393 if (GNUNET_OK != expect_data_dynamic (vec,
394 "out",
395 &out,
396 &out_len))
397 {
398 GNUNET_break (0);
399 return GNUNET_SYSERR;
400 }
401
402 out_len_comp = out_len;
403 out_comp = GNUNET_malloc (out_len_comp);
404
405 if (GNUNET_OK != expect_data_dynamic (vec,
406 "ikm",
407 &ikm,
408 &ikm_len))
409 {
410 GNUNET_break (0);
411 return GNUNET_SYSERR;
412 }
413
414 if (GNUNET_OK != expect_data_dynamic (vec,
415 "salt",
416 &salt,
417 &salt_len))
418 {
419 GNUNET_break (0);
420 return GNUNET_SYSERR;
421 }
422
423 if (GNUNET_OK != expect_data_dynamic (vec,
424 "ctx",
425 &ctx,
426 &ctx_len))
427 {
428 GNUNET_break (0);
429 return GNUNET_SYSERR;
430 }
431
432 GNUNET_assert (GNUNET_OK ==
433 GNUNET_CRYPTO_kdf (out_comp,
434 out_len_comp,
435 salt,
436 salt_len,
437 ikm,
438 ikm_len,
439 ctx,
440 ctx_len,
441 NULL));
442
443 if (0 != memcmp (out, out_comp, out_len))
444 {
445 GNUNET_break (0);
446 return GNUNET_NO;
447 }
448
449 }
450 else if (0 == strcmp (operation, "eddsa_ecdh"))
451 {
452 struct GNUNET_CRYPTO_EcdhePrivateKey priv_ecdhe;
453 struct GNUNET_CRYPTO_EcdhePublicKey pub_ecdhe;
454 struct GNUNET_CRYPTO_EddsaPrivateKey priv_eddsa;
455 struct GNUNET_CRYPTO_EddsaPublicKey pub_eddsa;
456 struct GNUNET_HashCode key_material;
457 struct GNUNET_HashCode key_material_comp;
458
459 if (GNUNET_OK != expect_data_fixed (vec,
460 "priv_ecdhe",
461 &priv_ecdhe,
462 sizeof (priv_ecdhe)))
463 {
464 GNUNET_break (0);
465 return GNUNET_NO;
466 }
467
468 if (GNUNET_OK != expect_data_fixed (vec,
469 "pub_ecdhe",
470 &pub_ecdhe,
471 sizeof (pub_ecdhe)))
472 {
473 GNUNET_break (0);
474 return GNUNET_NO;
475 }
476
477 if (GNUNET_OK != expect_data_fixed (vec,
478 "priv_eddsa",
479 &priv_eddsa,
480 sizeof (priv_eddsa)))
481 {
482 GNUNET_break (0);
483 return GNUNET_NO;
484 }
485
486 if (GNUNET_OK != expect_data_fixed (vec,
487 "pub_eddsa",
488 &pub_eddsa,
489 sizeof (pub_eddsa)))
490 {
491 GNUNET_break (0);
492 return GNUNET_NO;
493 }
494
495 if (GNUNET_OK != expect_data_fixed (vec,
496 "key_material",
497 &key_material,
498 sizeof (key_material)))
499 {
500 GNUNET_break (0);
501 return GNUNET_NO;
502 }
503
504 GNUNET_CRYPTO_ecdh_eddsa (&priv_ecdhe, &pub_eddsa, &key_material_comp);
505
506 if (0 != GNUNET_memcmp (&key_material, &key_material_comp))
507 {
508 GNUNET_break (0);
509 return GNUNET_NO;
510 }
511 }
512 else if (0 == strcmp (operation, "rsa_blind_signing"))
513 {
514 struct GNUNET_CRYPTO_RsaPrivateKey *skey;
515 struct GNUNET_CRYPTO_RsaPublicKey *pkey;
516 struct GNUNET_HashCode message_hash;
517 struct GNUNET_CRYPTO_RsaBlindingKeySecret bks;
518 struct GNUNET_CRYPTO_RsaSignature *blinded_sig;
519 struct GNUNET_CRYPTO_RsaSignature *sig;
520 void *blinded_data;
521 size_t blinded_len;
522 void *blinded_data_comp;
523 size_t blinded_len_comp;
524 void *public_enc_data;
525 size_t public_enc_len;
526 void *secret_enc_data;
527 size_t secret_enc_len;
528 void *sig_enc_data;
529 size_t sig_enc_length;
530 void *sig_enc_data_comp;
531 size_t sig_enc_length_comp;
532
533 if (GNUNET_OK != expect_data_fixed (vec,
534 "message_hash",
535 &message_hash,
536 sizeof (message_hash)))
537 {
538 GNUNET_break (0);
539 return GNUNET_SYSERR;
540 }
541
542 if (GNUNET_OK != expect_data_fixed (vec,
543 "blinding_key_secret",
544 &bks,
545 sizeof (bks)))
546 {
547 GNUNET_break (0);
548 return GNUNET_SYSERR;
549 }
550
551 if (GNUNET_OK != expect_data_dynamic (vec,
552 "blinded_message",
553 &blinded_data,
554 &blinded_len))
555 {
556 GNUNET_break (0);
557 return GNUNET_SYSERR;
558 }
559
560 if (GNUNET_OK != expect_data_dynamic (vec,
561 "rsa_public_key",
562 &public_enc_data,
563 &public_enc_len))
564 {
565 GNUNET_break (0);
566 return GNUNET_SYSERR;
567 }
568
569 if (GNUNET_OK != expect_data_dynamic (vec,
570 "rsa_private_key",
571 &secret_enc_data,
572 &secret_enc_len))
573 {
574 GNUNET_break (0);
575 return GNUNET_SYSERR;
576 }
577
578 if (GNUNET_OK != expect_data_dynamic (vec,
579 "sig",
580 &sig_enc_data,
581 &sig_enc_length))
582 {
583 GNUNET_break (0);
584 return GNUNET_SYSERR;
585 }
586
587 pkey = GNUNET_CRYPTO_rsa_public_key_decode (public_enc_data,
588 public_enc_len);
589 GNUNET_assert (NULL != pkey);
590 skey = GNUNET_CRYPTO_rsa_private_key_decode (secret_enc_data,
591 secret_enc_len);
592 GNUNET_assert (NULL != skey);
593
594 GNUNET_assert (GNUNET_YES ==
595 GNUNET_CRYPTO_rsa_blind (&message_hash,
596 &bks,
597 pkey,
598 &blinded_data_comp,
599 &blinded_len_comp));
600 if ( (blinded_len != blinded_len_comp) || (0 != memcmp (blinded_data,
601 blinded_data_comp,
602 blinded_len)) )
603 {
604 GNUNET_break (0);
605 return GNUNET_NO;
606 }
607 blinded_sig = GNUNET_CRYPTO_rsa_sign_blinded (skey, blinded_data,
608 blinded_len);
609 sig = GNUNET_CRYPTO_rsa_unblind (blinded_sig, &bks, pkey);
610 GNUNET_assert (GNUNET_YES == GNUNET_CRYPTO_rsa_verify (&message_hash, sig,
611 pkey));
612 public_enc_len = GNUNET_CRYPTO_rsa_public_key_encode (pkey,
613 &public_enc_data);
614 sig_enc_length_comp = GNUNET_CRYPTO_rsa_signature_encode (sig, &sig_enc_data_comp);
615
616 if ( (sig_enc_length != sig_enc_length_comp) ||
617 (0 != memcmp (sig_enc_data, sig_enc_data_comp, sig_enc_length) ))
618 {
619 GNUNET_break (0);
620 return GNUNET_NO;
621 }
622 }
623 else
624 {
625 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
626 "unsupported operation '%s'\n", operation);
627 }
628
629 return GNUNET_OK;
630}
631
632/**
633 * Check test vectors from stdin.
634 *
635 * @returns global exit code
636 */
637static int
638check_vectors ()
639{
640 json_error_t err;
641 json_t *vecfile = json_loadf (stdin, 0, &err);
642 const char *encoding;
643 json_t *vectors;
644
645 if (NULL == vecfile)
646 {
647 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "unable to parse JSON\n");
648 return 1;
649 }
650 encoding = json_string_value (json_object_get (vecfile,
651 "encoding"));
652 if ( (NULL == encoding) || (0 != strcmp (encoding, "base32crockford")) )
653 {
654 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "unsupported or missing encoding\n");
655 json_decref (vecfile);
656 return 1;
657 }
658 vectors = json_object_get (vecfile, "vectors");
659 if (!json_is_array (vectors))
660 {
661 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bad vectors\n");
662 json_decref (vecfile);
663 return 1;
664 }
665 {
666 /* array is a JSON array */
667 size_t index;
668 json_t *value;
669 int ret;
670
671 json_array_foreach (vectors, index, value) {
672 const char *op = json_string_value (json_object_get (value,
673 "operation"));
674
675 if (NULL == op)
676 {
677 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
678 "missing operation\n");
679 ret = GNUNET_SYSERR;
680 break;
681 }
682 ret = checkvec (op, value);
683 if (GNUNET_OK != ret)
684 {
685 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
686 "bad vector %u\n",
687 (unsigned int) index);
688 break;
689 }
690 }
691 return (ret == GNUNET_OK) ? 0 : 1;
692 }
693}
694
695/**
696 * Output test vectors.
697 *
698 * @returns global exit code
699 */
700static int
701output_vectors ()
77{ 702{
703 json_t *vecfile = json_object ();
704 json_t *vecs = json_array ();
705
706 json_object_set_new (vecfile,
707 "encoding",
708 json_string ("base32crockford"));
709 json_object_set_new (vecfile,
710 "producer",
711 json_string ("GNUnet " PACKAGE_VERSION " " VCS_VERSION));
712 json_object_set_new (vecfile,
713 "vectors",
714 vecs);
715
78 { 716 {
717 json_t *vec = vec_for (vecs, "hash");
79 struct GNUNET_HashCode hc; 718 struct GNUNET_HashCode hc;
80 char *str = "Hello, GNUnet"; 719 char *str = "Hello, GNUnet";
81 720
82 GNUNET_CRYPTO_hash (str, strlen (str), &hc); 721 GNUNET_CRYPTO_hash (str, strlen (str), &hc);
83 722
84 printf ("hash code:\n"); 723 d2j (vec, "input", str, strlen (str));
85 display_data (" input", str, strlen (str)); 724 d2j (vec, "output", &hc, sizeof (struct GNUNET_HashCode));
86 display_data (" output", &hc, sizeof (struct GNUNET_HashCode));
87 } 725 }
88 { 726 {
727 json_t *vec = vec_for (vecs, "ecc_ecdh");
89 struct GNUNET_CRYPTO_EcdhePrivateKey priv1; 728 struct GNUNET_CRYPTO_EcdhePrivateKey priv1;
90 struct GNUNET_CRYPTO_EcdhePublicKey pub1; 729 struct GNUNET_CRYPTO_EcdhePublicKey pub1;
91 struct GNUNET_CRYPTO_EcdhePrivateKey priv2; 730 struct GNUNET_CRYPTO_EcdhePrivateKey priv2;
@@ -100,22 +739,26 @@ run (void *cls,
100 &pub1, 739 &pub1,
101 &skm)); 740 &skm));
102 741
103 printf ("ecdhe key:\n"); 742 d2j (vec,
104 display_data (" priv1", 743 "priv1",
105 &priv1, 744 &priv1,
106 sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey)); 745 sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey));
107 display_data (" pub1", 746 d2j (vec,
108 &pub1, 747 "pub1",
109 sizeof (struct GNUNET_CRYPTO_EcdhePublicKey)); 748 &pub1,
110 display_data (" priv2", 749 sizeof (struct GNUNET_CRYPTO_EcdhePublicKey));
111 &priv2, 750 d2j (vec,
112 sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey)); 751 "priv2",
113 display_data (" skm", 752 &priv2,
114 &skm, 753 sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey));
115 sizeof (struct GNUNET_HashCode)); 754 d2j (vec,
755 "skm",
756 &skm,
757 sizeof (struct GNUNET_HashCode));
116 } 758 }
117 759
118 { 760 {
761 json_t *vec = vec_for (vecs, "eddsa_key_derivation");
119 struct GNUNET_CRYPTO_EddsaPrivateKey priv; 762 struct GNUNET_CRYPTO_EddsaPrivateKey priv;
120 struct GNUNET_CRYPTO_EddsaPublicKey pub; 763 struct GNUNET_CRYPTO_EddsaPublicKey pub;
121 764
@@ -123,15 +766,17 @@ run (void *cls,
123 GNUNET_CRYPTO_eddsa_key_get_public (&priv, 766 GNUNET_CRYPTO_eddsa_key_get_public (&priv,
124 &pub); 767 &pub);
125 768
126 printf ("eddsa key:\n"); 769 d2j (vec,
127 display_data (" priv", 770 "priv",
128 &priv, 771 &priv,
129 sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey)); 772 sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey));
130 display_data (" pub", 773 d2j (vec,
131 &pub, 774 "pub",
132 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey)); 775 &pub,
776 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey));
133 } 777 }
134 { 778 {
779 json_t *vec = vec_for (vecs, "eddsa_signing");
135 struct GNUNET_CRYPTO_EddsaPrivateKey priv; 780 struct GNUNET_CRYPTO_EddsaPrivateKey priv;
136 struct GNUNET_CRYPTO_EddsaPublicKey pub; 781 struct GNUNET_CRYPTO_EddsaPublicKey pub;
137 struct GNUNET_CRYPTO_EddsaSignature sig; 782 struct GNUNET_CRYPTO_EddsaSignature sig;
@@ -151,22 +796,26 @@ run (void *cls,
151 &sig, 796 &sig,
152 &pub)); 797 &pub));
153 798
154 printf ("eddsa sig:\n"); 799 d2j (vec,
155 display_data (" priv", 800 "priv",
156 &priv, 801 &priv,
157 sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey)); 802 sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey));
158 display_data (" pub", 803 d2j (vec,
159 &pub, 804 "pub",
160 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey)); 805 &pub,
161 display_data (" data", 806 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey));
162 &data, 807 d2j (vec,
163 sizeof (struct TestSignatureDataPS)); 808 "data",
164 display_data (" sig", 809 &data,
165 &sig, 810 sizeof (struct TestSignatureDataPS));
166 sizeof (struct GNUNET_CRYPTO_EddsaSignature)); 811 d2j (vec,
812 "sig",
813 &sig,
814 sizeof (struct GNUNET_CRYPTO_EddsaSignature));
167 } 815 }
168 816
169 { 817 {
818 json_t *vec = vec_for (vecs, "kdf");
170 size_t out_len = 64; 819 size_t out_len = 64;
171 char out[out_len]; 820 char out[out_len];
172 char *ikm = "I'm the secret input key material"; 821 char *ikm = "I'm the secret input key material";
@@ -184,14 +833,28 @@ run (void *cls,
184 strlen (ctx), 833 strlen (ctx),
185 NULL)); 834 NULL));
186 835
187 printf ("kdf:\n"); 836 d2j (vec,
188 display_data (" salt", salt, strlen (salt)); 837 "salt",
189 display_data (" ikm", ikm, strlen (ikm)); 838 salt,
190 display_data (" ctx", ctx, strlen (ctx)); 839 strlen (salt));
191 printf (" out_len %u\n", (unsigned int) out_len); 840 d2j (vec,
192 display_data (" out", out, out_len); 841 "ikm",
842 ikm,
843 strlen (ikm));
844 d2j (vec,
845 "ctx",
846 ctx,
847 strlen (ctx));
848 uint2j (vec,
849 "out_len",
850 (unsigned int) out_len);
851 d2j (vec,
852 "out",
853 out,
854 out_len);
193 } 855 }
194 { 856 {
857 json_t *vec = vec_for (vecs, "eddsa_ecdh");
195 struct GNUNET_CRYPTO_EcdhePrivateKey priv_ecdhe; 858 struct GNUNET_CRYPTO_EcdhePrivateKey priv_ecdhe;
196 struct GNUNET_CRYPTO_EcdhePublicKey pub_ecdhe; 859 struct GNUNET_CRYPTO_EcdhePublicKey pub_ecdhe;
197 struct GNUNET_CRYPTO_EddsaPrivateKey priv_eddsa; 860 struct GNUNET_CRYPTO_EddsaPrivateKey priv_eddsa;
@@ -204,25 +867,26 @@ run (void *cls,
204 GNUNET_CRYPTO_eddsa_key_get_public (&priv_eddsa, &pub_eddsa); 867 GNUNET_CRYPTO_eddsa_key_get_public (&priv_eddsa, &pub_eddsa);
205 GNUNET_CRYPTO_ecdh_eddsa (&priv_ecdhe, &pub_eddsa, &key_material); 868 GNUNET_CRYPTO_ecdh_eddsa (&priv_ecdhe, &pub_eddsa, &key_material);
206 869
207 printf ("eddsa_ecdh:\n"); 870 d2j (vec, "priv_ecdhe",
208 display_data (" priv_ecdhe",
209 &priv_ecdhe, 871 &priv_ecdhe,
210 sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey)); 872 sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey));
211 display_data (" pub_ecdhe", 873 d2j (vec, "pub_ecdhe",
212 &pub_ecdhe, 874 &pub_ecdhe,
213 sizeof (struct GNUNET_CRYPTO_EcdhePublicKey)); 875 sizeof (struct GNUNET_CRYPTO_EcdhePublicKey));
214 display_data (" priv_eddsa", 876 d2j (vec, "priv_eddsa",
215 &priv_eddsa, 877 &priv_eddsa,
216 sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey)); 878 sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey));
217 display_data (" pub_eddsa", 879 d2j (vec, "pub_eddsa",
218 &pub_eddsa, 880 &pub_eddsa,
219 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey)); 881 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey));
220 display_data (" key_material", 882 d2j (vec, "key_material",
221 &key_material, 883 &key_material,
222 sizeof (struct GNUNET_HashCode)); 884 sizeof (struct GNUNET_HashCode));
223 } 885 }
224 886
225 { 887 {
888 json_t *vec = vec_for (vecs, "rsa_blind_signing");
889
226 struct GNUNET_CRYPTO_RsaPrivateKey *skey; 890 struct GNUNET_CRYPTO_RsaPrivateKey *skey;
227 struct GNUNET_CRYPTO_RsaPublicKey *pkey; 891 struct GNUNET_CRYPTO_RsaPublicKey *pkey;
228 struct GNUNET_HashCode message_hash; 892 struct GNUNET_HashCode message_hash;
@@ -233,10 +897,13 @@ run (void *cls,
233 size_t blinded_len; 897 size_t blinded_len;
234 void *public_enc_data; 898 void *public_enc_data;
235 size_t public_enc_len; 899 size_t public_enc_len;
900 void *secret_enc_data;
901 size_t secret_enc_len;
236 void *blinded_sig_enc_data; 902 void *blinded_sig_enc_data;
237 size_t blinded_sig_enc_length; 903 size_t blinded_sig_enc_length;
238 void *sig_enc_data; 904 void *sig_enc_data;
239 size_t sig_enc_length; 905 size_t sig_enc_length;
906
240 skey = GNUNET_CRYPTO_rsa_private_key_create (2048); 907 skey = GNUNET_CRYPTO_rsa_private_key_create (2048);
241 pkey = GNUNET_CRYPTO_rsa_private_key_get_public (skey); 908 pkey = GNUNET_CRYPTO_rsa_private_key_get_public (skey);
242 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, 909 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
@@ -259,25 +926,75 @@ run (void *cls,
259 pkey)); 926 pkey));
260 public_enc_len = GNUNET_CRYPTO_rsa_public_key_encode (pkey, 927 public_enc_len = GNUNET_CRYPTO_rsa_public_key_encode (pkey,
261 &public_enc_data); 928 &public_enc_data);
929 secret_enc_len = GNUNET_CRYPTO_rsa_private_key_encode (skey,
930 &secret_enc_data);
262 blinded_sig_enc_length = GNUNET_CRYPTO_rsa_signature_encode (blinded_sig, 931 blinded_sig_enc_length = GNUNET_CRYPTO_rsa_signature_encode (blinded_sig,
263 & 932 &
264 blinded_sig_enc_data); 933 blinded_sig_enc_data);
265 sig_enc_length = GNUNET_CRYPTO_rsa_signature_encode (sig, &sig_enc_data); 934 sig_enc_length = GNUNET_CRYPTO_rsa_signature_encode (sig, &sig_enc_data);
266 printf ("blind signing:\n"); 935 d2j (vec,
267 display_data (" message_hash", &message_hash, sizeof (struct 936 "message_hash",
268 GNUNET_HashCode)); 937 &message_hash,
269 display_data (" rsa_public_key", public_enc_data, public_enc_len); 938 sizeof (struct GNUNET_HashCode));
270 display_data (" blinding_key_secret", &bks, sizeof (struct 939 d2j (vec,
271 GNUNET_CRYPTO_RsaBlindingKeySecret)); 940 "rsa_public_key",
272 display_data (" blinded_message", blinded_data, blinded_len); 941 public_enc_data,
273 display_data (" blinded_sig", blinded_sig_enc_data, 942 public_enc_len);
274 blinded_sig_enc_length); 943 d2j (vec,
275 display_data (" sig", sig_enc_data, sig_enc_length); 944 "rsa_private_key",
945 secret_enc_data,
946 secret_enc_len);
947 d2j (vec,
948 "blinding_key_secret",
949 &bks,
950 sizeof (struct GNUNET_CRYPTO_RsaBlindingKeySecret));
951 d2j (vec,
952 "blinded_message",
953 blinded_data,
954 blinded_len);
955 d2j (vec,
956 "blinded_sig",
957 blinded_sig_enc_data,
958 blinded_sig_enc_length);
959 d2j (vec,
960 "sig",
961 sig_enc_data,
962 sig_enc_length);
276 GNUNET_CRYPTO_rsa_private_key_free (skey); 963 GNUNET_CRYPTO_rsa_private_key_free (skey);
277 GNUNET_CRYPTO_rsa_public_key_free (pkey); 964 GNUNET_CRYPTO_rsa_public_key_free (pkey);
278 GNUNET_CRYPTO_rsa_signature_free (sig); 965 GNUNET_CRYPTO_rsa_signature_free (sig);
279 GNUNET_CRYPTO_rsa_signature_free (blinded_sig); 966 GNUNET_CRYPTO_rsa_signature_free (blinded_sig);
967 GNUNET_free (public_enc_data);
968 GNUNET_free (blinded_data);
969 GNUNET_free (sig_enc_data);
970 GNUNET_free (blinded_sig_enc_data);
280 } 971 }
972
973 json_dumpf (vecfile, stdout, JSON_INDENT (2));
974 json_decref (vecfile);
975 printf ("\n");
976
977 return 0;
978}
979
980/**
981 * Main function that will be run.
982 *
983 * @param cls closure
984 * @param args remaining command-line arguments
985 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
986 * @param cfg configuration
987 */
988static void
989run (void *cls,
990 char *const *args,
991 const char *cfgfile,
992 const struct GNUNET_CONFIGURATION_Handle *cfg)
993{
994 if (GNUNET_YES == verify_flag)
995 global_ret = check_vectors ();
996 else
997 global_ret = output_vectors ();
281} 998}
282 999
283 1000
@@ -293,6 +1010,11 @@ main (int argc,
293 char *const *argv) 1010 char *const *argv)
294{ 1011{
295 const struct GNUNET_GETOPT_CommandLineOption options[] = { 1012 const struct GNUNET_GETOPT_CommandLineOption options[] = {
1013 GNUNET_GETOPT_option_flag ('V',
1014 "verify",
1015 gettext_noop (
1016 "verify a test vector from stdin"),
1017 &verify_flag),
296 GNUNET_GETOPT_OPTION_END 1018 GNUNET_GETOPT_OPTION_END
297 }; 1019 };
298 1020
@@ -307,7 +1029,7 @@ main (int argc,
307 options, 1029 options,
308 &run, NULL)) 1030 &run, NULL))
309 return 1; 1031 return 1;
310 return 0; 1032 return global_ret;
311} 1033}
312 1034
313 1035
diff --git a/src/util/perf_crypto_rsa.c b/src/util/perf_crypto_rsa.c
index d652e8d4e..aba61786f 100644
--- a/src/util/perf_crypto_rsa.c
+++ b/src/util/perf_crypto_rsa.c
@@ -44,7 +44,7 @@ eval (unsigned int len)
44 struct GNUNET_CRYPTO_RsaBlindingKeySecret bsec[10]; 44 struct GNUNET_CRYPTO_RsaBlindingKeySecret bsec[10];
45 unsigned int i; 45 unsigned int i;
46 char sbuf[128]; 46 char sbuf[128];
47 char *bbuf; 47 void *bbuf;
48 size_t bbuf_len; 48 size_t bbuf_len;
49 struct GNUNET_HashCode hc; 49 struct GNUNET_HashCode hc;
50 50
@@ -88,14 +88,16 @@ eval (unsigned int len)
88 64 * 1024 / (1 + 88 64 * 1024 / (1 +
89 GNUNET_TIME_absolute_get_duration 89 GNUNET_TIME_absolute_get_duration
90 (start).rel_value_us / 1000LL), "keys/ms"); 90 (start).rel_value_us / 1000LL), "keys/ms");
91 */start = GNUNET_TIME_absolute_get (); 91 */
92 start = GNUNET_TIME_absolute_get ();
92 GNUNET_CRYPTO_hash ("test", 4, &hc); 93 GNUNET_CRYPTO_hash ("test", 4, &hc);
93 for (i = 0; i < 10; i++) 94 for (i = 0; i < 10; i++)
94 { 95 {
95 GNUNET_CRYPTO_rsa_blind (&hc, 96 GNUNET_CRYPTO_rsa_blind (&hc,
96 &bsec[i], 97 &bsec[i],
97 public_key, 98 public_key,
98 &bbuf, &bbuf_len); 99 &bbuf,
100 &bbuf_len);
99 GNUNET_free (bbuf); 101 GNUNET_free (bbuf);
100 } 102 }
101 printf ("10x %u-blinding took %s\n", 103 printf ("10x %u-blinding took %s\n",
@@ -115,12 +117,14 @@ eval (unsigned int len)
115 GNUNET_CRYPTO_rsa_blind (&hc, 117 GNUNET_CRYPTO_rsa_blind (&hc,
116 &bsec[0], 118 &bsec[0],
117 public_key, 119 public_key,
118 &bbuf, &bbuf_len); 120 &bbuf,
121 &bbuf_len);
119 start = GNUNET_TIME_absolute_get (); 122 start = GNUNET_TIME_absolute_get ();
120 for (i = 0; i < 10; i++) 123 for (i = 0; i < 10; i++)
121 { 124 {
122 sig = GNUNET_CRYPTO_rsa_sign_blinded (private_key, 125 sig = GNUNET_CRYPTO_rsa_sign_blinded (private_key,
123 bbuf, bbuf_len); 126 bbuf,
127 bbuf_len);
124 GNUNET_CRYPTO_rsa_signature_free (sig); 128 GNUNET_CRYPTO_rsa_signature_free (sig);
125 } 129 }
126 printf ("10x %u-signing took %s\n", 130 printf ("10x %u-signing took %s\n",
diff --git a/src/util/test_crypto_vectors.sh b/src/util/test_crypto_vectors.sh
new file mode 100755
index 000000000..40700a324
--- /dev/null
+++ b/src/util/test_crypto_vectors.sh
@@ -0,0 +1,3 @@
1#!/usr/bin/env bash
2
3cat ./crypto-test-vectors.json | ./gnunet-crypto-tvg --verify