diff options
author | Martin Schanzenbach <mschanzenbach@posteo.de> | 2020-08-06 14:40:17 +0200 |
---|---|---|
committer | Martin Schanzenbach <mschanzenbach@posteo.de> | 2020-08-06 14:40:17 +0200 |
commit | 6ab14a20690a499ad32e3f2ad448d64d4e6b65fc (patch) | |
tree | 7866fb31e10cd49b2bb00eaf23134945b506624d | |
parent | 1d4f5263ae72c12a42ec166ec8b1769620baaeda (diff) | |
download | gnunet-6ab14a20690a499ad32e3f2ad448d64d4e6b65fc.tar.gz gnunet-6ab14a20690a499ad32e3f2ad448d64d4e6b65fc.zip |
-fix various bugs
-rw-r--r-- | src/namestore/namestore_api.c | 2 | ||||
-rw-r--r-- | src/reclaim/gnunet-service-reclaim.c | 4 | ||||
-rw-r--r-- | src/reclaim/oidc_helper.c | 33 | ||||
-rw-r--r-- | src/reclaim/plugin_rest_openid_connect.c | 8 | ||||
-rw-r--r-- | src/reclaim/plugin_rest_reclaim.c | 3 | ||||
-rw-r--r-- | src/reclaim/reclaim_api.c | 2 | ||||
-rw-r--r-- | src/rest/gnunet-rest-server.c | 241 |
7 files changed, 233 insertions, 60 deletions
diff --git a/src/namestore/namestore_api.c b/src/namestore/namestore_api.c index 2a085cf04..f383f8b4a 100644 --- a/src/namestore/namestore_api.c +++ b/src/namestore/namestore_api.c | |||
@@ -373,6 +373,8 @@ handle_record_store_response (void *cls, | |||
373 | emsg = _ ("Namestore failed to store record\n"); | 373 | emsg = _ ("Namestore failed to store record\n"); |
374 | else | 374 | else |
375 | emsg = NULL; | 375 | emsg = NULL; |
376 | if (NULL == qe) | ||
377 | return; | ||
376 | if (NULL != qe->cont) | 378 | if (NULL != qe->cont) |
377 | qe->cont (qe->cont_cls, res, emsg); | 379 | qe->cont (qe->cont_cls, res, emsg); |
378 | free_qe (qe); | 380 | free_qe (qe); |
diff --git a/src/reclaim/gnunet-service-reclaim.c b/src/reclaim/gnunet-service-reclaim.c index d4d44c3fc..0cd8c10a5 100644 --- a/src/reclaim/gnunet-service-reclaim.c +++ b/src/reclaim/gnunet-service-reclaim.c | |||
@@ -850,8 +850,8 @@ consume_result_cb (void *cls, | |||
850 | struct ConsumeTicketResultMessage *crm; | 850 | struct ConsumeTicketResultMessage *crm; |
851 | struct GNUNET_MQ_Envelope *env; | 851 | struct GNUNET_MQ_Envelope *env; |
852 | char *data_tmp; | 852 | char *data_tmp; |
853 | size_t attrs_len; | 853 | size_t attrs_len = 0; |
854 | size_t attests_len; | 854 | size_t attests_len = 0; |
855 | 855 | ||
856 | if (GNUNET_OK != success) | 856 | if (GNUNET_OK != success) |
857 | { | 857 | { |
diff --git a/src/reclaim/oidc_helper.c b/src/reclaim/oidc_helper.c index cb99a749d..9b5938c43 100644 --- a/src/reclaim/oidc_helper.c +++ b/src/reclaim/oidc_helper.c | |||
@@ -162,8 +162,6 @@ generate_userinfo_json(const struct GNUNET_CRYPTO_EcdsaPublicKey *sub_key, | |||
162 | struct GNUNET_RECLAIM_AttributeListEntry *le; | 162 | struct GNUNET_RECLAIM_AttributeListEntry *le; |
163 | struct GNUNET_RECLAIM_AttestationListEntry *ale; | 163 | struct GNUNET_RECLAIM_AttestationListEntry *ale; |
164 | char *subject; | 164 | char *subject; |
165 | char *aggr_names_str; | ||
166 | char *aggr_sources_str; | ||
167 | char *source_name; | 165 | char *source_name; |
168 | char *attr_val_str; | 166 | char *attr_val_str; |
169 | char *attest_val_str; | 167 | char *attest_val_str; |
@@ -171,7 +169,7 @@ generate_userinfo_json(const struct GNUNET_CRYPTO_EcdsaPublicKey *sub_key, | |||
171 | json_t *aggr_names; | 169 | json_t *aggr_names; |
172 | json_t *aggr_sources; | 170 | json_t *aggr_sources; |
173 | json_t *aggr_sources_jwt; | 171 | json_t *aggr_sources_jwt; |
174 | json_t *addr_claim; | 172 | json_t *addr_claim = NULL; |
175 | int num_attestations = 0; | 173 | int num_attestations = 0; |
176 | for (le = attrs->list_head; NULL != le; le = le->next) | 174 | for (le = attrs->list_head; NULL != le; le = le->next) |
177 | { | 175 | { |
@@ -194,8 +192,6 @@ generate_userinfo_json(const struct GNUNET_CRYPTO_EcdsaPublicKey *sub_key, | |||
194 | // sub REQUIRED public key identity, not exceed 255 ASCII length | 192 | // sub REQUIRED public key identity, not exceed 255 ASCII length |
195 | json_object_set_new (body, "sub", json_string (subject)); | 193 | json_object_set_new (body, "sub", json_string (subject)); |
196 | attest_val_str = NULL; | 194 | attest_val_str = NULL; |
197 | aggr_names_str = NULL; | ||
198 | aggr_sources_str = NULL; | ||
199 | source_name = NULL; | 195 | source_name = NULL; |
200 | int i = 0; | 196 | int i = 0; |
201 | for (ale = attests->list_head; NULL != ale; ale = ale->next) | 197 | for (ale = attests->list_head; NULL != ale; ale = ale->next) |
@@ -237,8 +233,6 @@ generate_userinfo_json(const struct GNUNET_CRYPTO_EcdsaPublicKey *sub_key, | |||
237 | if (NULL == addr_claim) | 233 | if (NULL == addr_claim) |
238 | { | 234 | { |
239 | addr_claim = json_object (); | 235 | addr_claim = json_object (); |
240 | json_object_set_new (body, "address", | ||
241 | addr_claim); | ||
242 | } | 236 | } |
243 | json_object_set_new (addr_claim, le->attribute->name, | 237 | json_object_set_new (addr_claim, le->attribute->name, |
244 | json_string (attr_val_str)); | 238 | json_string (attr_val_str)); |
@@ -273,21 +267,17 @@ generate_userinfo_json(const struct GNUNET_CRYPTO_EcdsaPublicKey *sub_key, | |||
273 | GNUNET_free (source_name); | 267 | GNUNET_free (source_name); |
274 | } | 268 | } |
275 | } | 269 | } |
270 | if (NULL != addr_claim) | ||
271 | json_object_set_new (body, "address", addr_claim); | ||
276 | 272 | ||
277 | if (NULL != attest_val_str) | 273 | if (NULL != attest_val_str) |
278 | GNUNET_free (attest_val_str); | 274 | GNUNET_free (attest_val_str); |
279 | if (0 != i) | 275 | if (0 != i) |
280 | { | 276 | { |
281 | aggr_names_str = json_dumps (aggr_names, JSON_INDENT (0) | JSON_COMPACT); | 277 | json_object_set_new (body, "_claim_names", aggr_names); |
282 | aggr_sources_str = json_dumps (aggr_sources, JSON_INDENT (0) | 278 | json_object_set_new (body, "_claim_sources", aggr_sources); |
283 | | JSON_COMPACT); | ||
284 | json_object_set_new (body, "_claim_names", json_string (aggr_names_str)); | ||
285 | json_object_set_new (body, "_claim_sources", json_string ( | ||
286 | aggr_sources_str)); | ||
287 | } | 279 | } |
288 | 280 | ||
289 | json_decref (aggr_names); | ||
290 | json_decref (aggr_sources); | ||
291 | return body; | 281 | return body; |
292 | } | 282 | } |
293 | 283 | ||
@@ -611,7 +601,6 @@ OIDC_parse_authz_code (const struct GNUNET_CRYPTO_EcdsaPublicKey *audience, | |||
611 | GNUNET_STRINGS_base64url_decode (code, strlen (code), | 601 | GNUNET_STRINGS_base64url_decode (code, strlen (code), |
612 | (void **) &code_payload); | 602 | (void **) &code_payload); |
613 | if (code_payload_len < sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose) | 603 | if (code_payload_len < sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose) |
614 | + sizeof(struct GNUNET_CRYPTO_EcdhePublicKey) | ||
615 | + sizeof(struct OIDC_Parameters) | 604 | + sizeof(struct OIDC_Parameters) |
616 | + sizeof(struct GNUNET_CRYPTO_EcdsaSignature)) | 605 | + sizeof(struct GNUNET_CRYPTO_EcdsaSignature)) |
617 | { | 606 | { |
@@ -624,8 +613,6 @@ OIDC_parse_authz_code (const struct GNUNET_CRYPTO_EcdsaPublicKey *audience, | |||
624 | plaintext_len = code_payload_len; | 613 | plaintext_len = code_payload_len; |
625 | plaintext_len -= sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose); | 614 | plaintext_len -= sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose); |
626 | ptr = (char *) &purpose[1]; | 615 | ptr = (char *) &purpose[1]; |
627 | plaintext_len -= sizeof(struct GNUNET_CRYPTO_EcdhePublicKey); | ||
628 | |||
629 | plaintext_len -= sizeof(struct GNUNET_CRYPTO_EcdsaSignature); | 616 | plaintext_len -= sizeof(struct GNUNET_CRYPTO_EcdsaSignature); |
630 | plaintext = ptr; | 617 | plaintext = ptr; |
631 | ptr += plaintext_len; | 618 | ptr += plaintext_len; |
@@ -683,8 +670,8 @@ OIDC_parse_authz_code (const struct GNUNET_CRYPTO_EcdsaPublicKey *audience, | |||
683 | if (0 != GNUNET_memcmp (audience, &ticket->audience)) | 670 | if (0 != GNUNET_memcmp (audience, &ticket->audience)) |
684 | { | 671 | { |
685 | GNUNET_free (code_payload); | 672 | GNUNET_free (code_payload); |
686 | if (NULL != nonce_str) | 673 | if (NULL != *nonce_str) |
687 | GNUNET_free (nonce_str); | 674 | GNUNET_free (*nonce_str); |
688 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 675 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
689 | "Audience in ticket does not match client!\n"); | 676 | "Audience in ticket does not match client!\n"); |
690 | return GNUNET_SYSERR; | 677 | return GNUNET_SYSERR; |
@@ -696,8 +683,8 @@ OIDC_parse_authz_code (const struct GNUNET_CRYPTO_EcdsaPublicKey *audience, | |||
696 | &ticket->identity)) | 683 | &ticket->identity)) |
697 | { | 684 | { |
698 | GNUNET_free (code_payload); | 685 | GNUNET_free (code_payload); |
699 | if (NULL != nonce_str) | 686 | if (NULL != *nonce_str) |
700 | GNUNET_free (nonce_str); | 687 | GNUNET_free (*nonce_str); |
701 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Signature of AuthZ code invalid!\n"); | 688 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Signature of AuthZ code invalid!\n"); |
702 | return GNUNET_SYSERR; | 689 | return GNUNET_SYSERR; |
703 | } | 690 | } |
@@ -768,7 +755,7 @@ OIDC_access_token_new (const struct GNUNET_RECLAIM_Ticket *ticket) | |||
768 | * Parse an access token | 755 | * Parse an access token |
769 | */ | 756 | */ |
770 | int | 757 | int |
771 | OIDC_access_token_parse (const char*token, | 758 | OIDC_access_token_parse (const char *token, |
772 | struct GNUNET_RECLAIM_Ticket **ticket) | 759 | struct GNUNET_RECLAIM_Ticket **ticket) |
773 | { | 760 | { |
774 | if (sizeof (struct GNUNET_RECLAIM_Ticket) != | 761 | if (sizeof (struct GNUNET_RECLAIM_Ticket) != |
diff --git a/src/reclaim/plugin_rest_openid_connect.c b/src/reclaim/plugin_rest_openid_connect.c index b294ba166..06e1b0061 100644 --- a/src/reclaim/plugin_rest_openid_connect.c +++ b/src/reclaim/plugin_rest_openid_connect.c | |||
@@ -631,6 +631,8 @@ do_userinfo_error (void *cls) | |||
631 | struct MHD_Response *resp; | 631 | struct MHD_Response *resp; |
632 | char *error; | 632 | char *error; |
633 | 633 | ||
634 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
635 | "Error: %s\n", handle->edesc); | ||
634 | GNUNET_asprintf (&error, | 636 | GNUNET_asprintf (&error, |
635 | "error=\"%s\", error_description=\"%s\"", | 637 | "error=\"%s\", error_description=\"%s\"", |
636 | handle->emsg, | 638 | handle->emsg, |
@@ -2129,12 +2131,13 @@ consume_ticket (void *cls, | |||
2129 | char *result_str; | 2131 | char *result_str; |
2130 | handle->idp_op = NULL; | 2132 | handle->idp_op = NULL; |
2131 | 2133 | ||
2134 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Attr: %s\n", attr->name); | ||
2132 | if (NULL == identity) | 2135 | if (NULL == identity) |
2133 | { | 2136 | { |
2134 | result_str = OIDC_generate_userinfo (&handle->ticket.identity, | 2137 | result_str = OIDC_generate_userinfo (&handle->ticket.identity, |
2135 | handle->attr_userinfo_list, | 2138 | handle->attr_userinfo_list, |
2136 | handle->attests_list); | 2139 | handle->attests_list); |
2137 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Userinfo: %s\n", result_str); | 2140 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Userinfo: %s\n", result_str); |
2138 | resp = GNUNET_REST_create_response (result_str); | 2141 | resp = GNUNET_REST_create_response (result_str); |
2139 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); | 2142 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); |
2140 | GNUNET_free (result_str); | 2143 | GNUNET_free (result_str); |
@@ -2198,6 +2201,7 @@ userinfo_endpoint (struct GNUNET_REST_RequestHandle *con_handle, | |||
2198 | const struct EgoEntry *aud_ego; | 2201 | const struct EgoEntry *aud_ego; |
2199 | const struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey; | 2202 | const struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey; |
2200 | 2203 | ||
2204 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Getting userinfo\n"); | ||
2201 | GNUNET_CRYPTO_hash (OIDC_AUTHORIZATION_HEADER_KEY, | 2205 | GNUNET_CRYPTO_hash (OIDC_AUTHORIZATION_HEADER_KEY, |
2202 | strlen (OIDC_AUTHORIZATION_HEADER_KEY), | 2206 | strlen (OIDC_AUTHORIZATION_HEADER_KEY), |
2203 | &cache_key); | 2207 | &cache_key); |
@@ -2263,7 +2267,7 @@ userinfo_endpoint (struct GNUNET_REST_RequestHandle *con_handle, | |||
2263 | GNUNET_free (authorization); | 2267 | GNUNET_free (authorization); |
2264 | return; | 2268 | return; |
2265 | } | 2269 | } |
2266 | 2270 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Consuming ticket\n"); | |
2267 | privkey = GNUNET_IDENTITY_ego_get_private_key (aud_ego->ego); | 2271 | privkey = GNUNET_IDENTITY_ego_get_private_key (aud_ego->ego); |
2268 | handle->attr_userinfo_list = | 2272 | handle->attr_userinfo_list = |
2269 | GNUNET_new (struct GNUNET_RECLAIM_AttributeList); | 2273 | GNUNET_new (struct GNUNET_RECLAIM_AttributeList); |
diff --git a/src/reclaim/plugin_rest_reclaim.c b/src/reclaim/plugin_rest_reclaim.c index eb442bc3b..c2d14825e 100644 --- a/src/reclaim/plugin_rest_reclaim.c +++ b/src/reclaim/plugin_rest_reclaim.c | |||
@@ -331,6 +331,7 @@ finished_cont (void *cls, int32_t success, const char *emsg) | |||
331 | 331 | ||
332 | resp = GNUNET_REST_create_response (emsg); | 332 | resp = GNUNET_REST_create_response (emsg); |
333 | MHD_add_response_header (resp, "Content-Type", "application/json"); | 333 | MHD_add_response_header (resp, "Content-Type", "application/json"); |
334 | MHD_add_response_header (resp, "Access-Control-Allow-Methods", allow_methods); | ||
334 | if (GNUNET_OK != success) | 335 | if (GNUNET_OK != success) |
335 | { | 336 | { |
336 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 337 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
@@ -348,6 +349,7 @@ delete_finished_cb (void *cls, int32_t success, const char *emsg) | |||
348 | struct MHD_Response *resp; | 349 | struct MHD_Response *resp; |
349 | 350 | ||
350 | resp = GNUNET_REST_create_response (emsg); | 351 | resp = GNUNET_REST_create_response (emsg); |
352 | MHD_add_response_header (resp, "Access-Control-Allow-Methods", allow_methods); | ||
351 | if (GNUNET_OK != success) | 353 | if (GNUNET_OK != success) |
352 | { | 354 | { |
353 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 355 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
@@ -373,6 +375,7 @@ return_response (void *cls) | |||
373 | result_str = json_dumps (handle->resp_object, 0); | 375 | result_str = json_dumps (handle->resp_object, 0); |
374 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str); | 376 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str); |
375 | resp = GNUNET_REST_create_response (result_str); | 377 | resp = GNUNET_REST_create_response (result_str); |
378 | MHD_add_response_header (resp, "Access-Control-Allow-Methods", allow_methods); | ||
376 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); | 379 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); |
377 | GNUNET_free (result_str); | 380 | GNUNET_free (result_str); |
378 | cleanup_handle (handle); | 381 | cleanup_handle (handle); |
diff --git a/src/reclaim/reclaim_api.c b/src/reclaim/reclaim_api.c index ff549fa71..d73241a6f 100644 --- a/src/reclaim/reclaim_api.c +++ b/src/reclaim/reclaim_api.c | |||
@@ -1572,6 +1572,8 @@ GNUNET_RECLAIM_ticket_consume ( | |||
1572 | ctm->ticket = *ticket; | 1572 | ctm->ticket = *ticket; |
1573 | if (NULL != h->mq) | 1573 | if (NULL != h->mq) |
1574 | GNUNET_MQ_send_copy (h->mq, op->env); | 1574 | GNUNET_MQ_send_copy (h->mq, op->env); |
1575 | else | ||
1576 | reconnect(h); | ||
1575 | return op; | 1577 | return op; |
1576 | } | 1578 | } |
1577 | 1579 | ||
diff --git a/src/rest/gnunet-rest-server.c b/src/rest/gnunet-rest-server.c index 44d4f345e..436b5b205 100644 --- a/src/rest/gnunet-rest-server.c +++ b/src/rest/gnunet-rest-server.c | |||
@@ -184,6 +184,42 @@ struct MhdConnectionHandle | |||
184 | int state; | 184 | int state; |
185 | }; | 185 | }; |
186 | 186 | ||
187 | /** | ||
188 | * Accepted requests | ||
189 | */ | ||
190 | struct AcceptedRequest | ||
191 | { | ||
192 | /** | ||
193 | * DLL | ||
194 | */ | ||
195 | struct AcceptedRequest *next; | ||
196 | |||
197 | /** | ||
198 | * DLL | ||
199 | */ | ||
200 | struct AcceptedRequest *prev; | ||
201 | |||
202 | /** | ||
203 | * Socket | ||
204 | */ | ||
205 | struct GNUNET_NETWORK_Handle *sock; | ||
206 | |||
207 | /** | ||
208 | * Connection | ||
209 | */ | ||
210 | struct MhdConnectionHandle *con_handle; | ||
211 | }; | ||
212 | |||
213 | /** | ||
214 | * AcceptedRequest list head | ||
215 | */ | ||
216 | static struct AcceptedRequest *req_list_head; | ||
217 | |||
218 | /** | ||
219 | * AcceptedRequest list tail | ||
220 | */ | ||
221 | static struct AcceptedRequest *req_list_tail; | ||
222 | |||
187 | /* ************************* Global helpers ********************* */ | 223 | /* ************************* Global helpers ********************* */ |
188 | 224 | ||
189 | 225 | ||
@@ -238,7 +274,6 @@ cleanup_url_map (void *cls, const struct GNUNET_HashCode *key, void *value) | |||
238 | return GNUNET_YES; | 274 | return GNUNET_YES; |
239 | } | 275 | } |
240 | 276 | ||
241 | |||
242 | static void | 277 | static void |
243 | cleanup_handle (struct MhdConnectionHandle *handle) | 278 | cleanup_handle (struct MhdConnectionHandle *handle) |
244 | { | 279 | { |
@@ -268,6 +303,19 @@ cleanup_handle (struct MhdConnectionHandle *handle) | |||
268 | GNUNET_free (handle); | 303 | GNUNET_free (handle); |
269 | } | 304 | } |
270 | 305 | ||
306 | static void | ||
307 | cleanup_ar (struct AcceptedRequest *ar) | ||
308 | { | ||
309 | if (NULL != ar->con_handle) | ||
310 | { | ||
311 | cleanup_handle (ar->con_handle); | ||
312 | } | ||
313 | GNUNET_NETWORK_socket_free_memory_only_ (ar->sock); | ||
314 | GNUNET_CONTAINER_DLL_remove (req_list_head, | ||
315 | req_list_tail, | ||
316 | ar); | ||
317 | GNUNET_free (ar); | ||
318 | } | ||
271 | 319 | ||
272 | static int | 320 | static int |
273 | header_iterator (void *cls, | 321 | header_iterator (void *cls, |
@@ -404,22 +452,29 @@ create_response (void *cls, | |||
404 | void **con_cls) | 452 | void **con_cls) |
405 | { | 453 | { |
406 | char *origin; | 454 | char *origin; |
455 | struct AcceptedRequest *ar; | ||
407 | struct GNUNET_HashCode key; | 456 | struct GNUNET_HashCode key; |
408 | struct MhdConnectionHandle *con_handle; | 457 | struct MhdConnectionHandle *con_handle; |
409 | struct GNUNET_REST_RequestHandle *rest_conndata_handle; | 458 | struct GNUNET_REST_RequestHandle *rest_conndata_handle; |
410 | struct PluginListEntry *ple; | 459 | struct PluginListEntry *ple; |
411 | 460 | ||
412 | con_handle = *con_cls; | 461 | ar = *con_cls; |
462 | if (NULL == ar) | ||
463 | { | ||
464 | GNUNET_break (0); | ||
465 | return MHD_NO; | ||
466 | } | ||
413 | 467 | ||
414 | if (NULL == *con_cls) | 468 | if (NULL == ar->con_handle) |
415 | { | 469 | { |
416 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New connection %s\n", url); | 470 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New connection %s\n", url); |
417 | con_handle = GNUNET_new (struct MhdConnectionHandle); | 471 | con_handle = GNUNET_new (struct MhdConnectionHandle); |
418 | con_handle->con = con; | 472 | con_handle->con = con; |
419 | con_handle->state = GN_REST_STATE_INIT; | 473 | con_handle->state = GN_REST_STATE_INIT; |
420 | *con_cls = con_handle; | 474 | ar->con_handle = con_handle; |
421 | return MHD_YES; | 475 | return MHD_YES; |
422 | } | 476 | } |
477 | con_handle = ar->con_handle; | ||
423 | if (GN_REST_STATE_INIT == con_handle->state) | 478 | if (GN_REST_STATE_INIT == con_handle->state) |
424 | { | 479 | { |
425 | rest_conndata_handle = GNUNET_new (struct GNUNET_REST_RequestHandle); | 480 | rest_conndata_handle = GNUNET_new (struct GNUNET_REST_RequestHandle); |
@@ -535,7 +590,7 @@ create_response (void *cls, | |||
535 | MHD_RESULT ret = MHD_queue_response (con, | 590 | MHD_RESULT ret = MHD_queue_response (con, |
536 | con_handle->status, | 591 | con_handle->status, |
537 | con_handle->response); | 592 | con_handle->response); |
538 | cleanup_handle (con_handle); | 593 | //cleanup_handle (con_handle); |
539 | return ret; | 594 | return ret; |
540 | } | 595 | } |
541 | } | 596 | } |
@@ -543,27 +598,6 @@ create_response (void *cls, | |||
543 | 598 | ||
544 | /* ******************** MHD HTTP setup and event loop ******************** */ | 599 | /* ******************** MHD HTTP setup and event loop ******************** */ |
545 | 600 | ||
546 | /** | ||
547 | * Function called when MHD decides that we are done with a connection. | ||
548 | * | ||
549 | * @param cls NULL | ||
550 | * @param connection connection handle | ||
551 | * @param con_cls value as set by the last call to | ||
552 | * the MHD_AccessHandlerCallback, should be our handle | ||
553 | * @param toe reason for request termination (ignored) | ||
554 | */ | ||
555 | static void | ||
556 | mhd_completed_cb (void *cls, | ||
557 | struct MHD_Connection *connection, | ||
558 | void **con_cls, | ||
559 | enum MHD_RequestTerminationCode toe) | ||
560 | { | ||
561 | if (MHD_REQUEST_TERMINATED_COMPLETED_OK != toe) | ||
562 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
563 | "MHD encountered error handling request: %d\n", | ||
564 | toe); | ||
565 | } | ||
566 | |||
567 | 601 | ||
568 | /** | 602 | /** |
569 | * Kill the MHD daemon. | 603 | * Kill the MHD daemon. |
@@ -671,6 +705,135 @@ schedule_httpd () | |||
671 | GNUNET_NETWORK_fdset_destroy (wws); | 705 | GNUNET_NETWORK_fdset_destroy (wws); |
672 | } | 706 | } |
673 | 707 | ||
708 | /** | ||
709 | * Function called when MHD first processes an incoming connection. | ||
710 | * Gives us the respective URI information. | ||
711 | * | ||
712 | * We use this to associate the `struct MHD_Connection` with our | ||
713 | * internal `struct AcceptedRequest` data structure (by checking | ||
714 | * for matching sockets). | ||
715 | * | ||
716 | * @param cls the HTTP server handle (a `struct MhdHttpList`) | ||
717 | * @param url the URL that is being requested | ||
718 | * @param connection MHD connection object for the request | ||
719 | * @return the `struct Socks5Request` that this @a connection is for | ||
720 | */ | ||
721 | static void * | ||
722 | mhd_log_callback (void *cls, | ||
723 | const char *url, | ||
724 | struct MHD_Connection *connection) | ||
725 | { | ||
726 | struct AcceptedRequest *ar; | ||
727 | const union MHD_ConnectionInfo *ci; | ||
728 | |||
729 | ci = MHD_get_connection_info (connection, | ||
730 | MHD_CONNECTION_INFO_SOCKET_CONTEXT); | ||
731 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Processing %s\n", url); | ||
732 | if (NULL == ci) | ||
733 | { | ||
734 | GNUNET_break (0); | ||
735 | return NULL; | ||
736 | } | ||
737 | ar = ci->socket_context; | ||
738 | return ar; | ||
739 | } | ||
740 | |||
741 | |||
742 | |||
743 | /** | ||
744 | * Function called when MHD decides that we are done with a connection. | ||
745 | * | ||
746 | * @param cls NULL | ||
747 | * @param connection connection handle | ||
748 | * @param con_cls value as set by the last call to | ||
749 | * the MHD_AccessHandlerCallback, should be our handle | ||
750 | * @param toe reason for request termination (ignored) | ||
751 | */ | ||
752 | static void | ||
753 | mhd_completed_cb (void *cls, | ||
754 | struct MHD_Connection *connection, | ||
755 | void **con_cls, | ||
756 | enum MHD_RequestTerminationCode toe) | ||
757 | { | ||
758 | struct AcceptedRequest *ar = *con_cls; | ||
759 | if (MHD_REQUEST_TERMINATED_COMPLETED_OK != toe) | ||
760 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
761 | "MHD encountered error handling request: %d\n", | ||
762 | toe); | ||
763 | if (NULL == ar) | ||
764 | return; | ||
765 | if (NULL != ar->con_handle) | ||
766 | { | ||
767 | cleanup_handle (ar->con_handle); | ||
768 | ar->con_handle = NULL; | ||
769 | } | ||
770 | schedule_httpd (); | ||
771 | *con_cls = NULL; | ||
772 | } | ||
773 | |||
774 | /** | ||
775 | * Function called when MHD connection is opened or closed. | ||
776 | * | ||
777 | * @param cls NULL | ||
778 | * @param connection connection handle | ||
779 | * @param con_cls value as set by the last call to | ||
780 | * the MHD_AccessHandlerCallback, should be our `struct Socks5Request *` | ||
781 | * @param toe connection notification type | ||
782 | */ | ||
783 | static void | ||
784 | mhd_connection_cb (void *cls, | ||
785 | struct MHD_Connection *connection, | ||
786 | void **con_cls, | ||
787 | enum MHD_ConnectionNotificationCode cnc) | ||
788 | { | ||
789 | struct AcceptedRequest *ar; | ||
790 | const union MHD_ConnectionInfo *ci; | ||
791 | int sock; | ||
792 | |||
793 | switch (cnc) | ||
794 | { | ||
795 | case MHD_CONNECTION_NOTIFY_STARTED: | ||
796 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connection started...\n"); | ||
797 | ci = MHD_get_connection_info (connection, | ||
798 | MHD_CONNECTION_INFO_CONNECTION_FD); | ||
799 | if (NULL == ci) | ||
800 | { | ||
801 | GNUNET_break (0); | ||
802 | return; | ||
803 | } | ||
804 | sock = ci->connect_fd; | ||
805 | for (ar = req_list_head; NULL != ar; ar = ar->next) | ||
806 | { | ||
807 | if (GNUNET_NETWORK_get_fd (ar->sock) == sock) | ||
808 | { | ||
809 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
810 | "Context set...\n"); | ||
811 | *con_cls = ar; | ||
812 | break; | ||
813 | } | ||
814 | } | ||
815 | break; | ||
816 | |||
817 | case MHD_CONNECTION_NOTIFY_CLOSED: | ||
818 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
819 | "Connection closed... cleaning up\n"); | ||
820 | ar = *con_cls; | ||
821 | if (NULL == ar) | ||
822 | { | ||
823 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
824 | "Connection stale!\n"); | ||
825 | return; | ||
826 | } | ||
827 | cleanup_ar (ar); | ||
828 | *con_cls = NULL; | ||
829 | break; | ||
830 | |||
831 | default: | ||
832 | GNUNET_break (0); | ||
833 | } | ||
834 | } | ||
835 | |||
836 | |||
674 | 837 | ||
675 | /** | 838 | /** |
676 | * Task run whenever HTTP server operations are pending. | 839 | * Task run whenever HTTP server operations are pending. |
@@ -696,7 +859,7 @@ static void | |||
696 | do_accept (void *cls) | 859 | do_accept (void *cls) |
697 | { | 860 | { |
698 | struct GNUNET_NETWORK_Handle *lsock = cls; | 861 | struct GNUNET_NETWORK_Handle *lsock = cls; |
699 | struct GNUNET_NETWORK_Handle *s; | 862 | struct AcceptedRequest *ar; |
700 | int fd; | 863 | int fd; |
701 | const struct sockaddr *addr; | 864 | const struct sockaddr *addr; |
702 | socklen_t len; | 865 | socklen_t len; |
@@ -718,24 +881,30 @@ do_accept (void *cls) | |||
718 | } | 881 | } |
719 | else | 882 | else |
720 | GNUNET_assert (0); | 883 | GNUNET_assert (0); |
721 | s = GNUNET_NETWORK_socket_accept (lsock, NULL, NULL); | 884 | ar = GNUNET_new (struct AcceptedRequest); |
722 | if (NULL == s) | 885 | ar->sock = GNUNET_NETWORK_socket_accept (lsock, NULL, NULL); |
886 | if (NULL == ar->sock) | ||
723 | { | 887 | { |
888 | GNUNET_free (ar); | ||
724 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "accept"); | 889 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "accept"); |
725 | return; | 890 | return; |
726 | } | 891 | } |
727 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 892 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
728 | "Got an inbound connection, waiting for data\n"); | 893 | "Got an inbound connection, waiting for data\n"); |
729 | fd = GNUNET_NETWORK_get_fd (s); | 894 | fd = GNUNET_NETWORK_get_fd (ar->sock); |
730 | addr = GNUNET_NETWORK_get_addr (s); | 895 | addr = GNUNET_NETWORK_get_addr (ar->sock); |
731 | len = GNUNET_NETWORK_get_addrlen (s); | 896 | len = GNUNET_NETWORK_get_addrlen (ar->sock); |
897 | GNUNET_CONTAINER_DLL_insert (req_list_head, | ||
898 | req_list_tail, | ||
899 | ar); | ||
732 | if (MHD_YES != MHD_add_connection (httpd, fd, addr, len)) | 900 | if (MHD_YES != MHD_add_connection (httpd, fd, addr, len)) |
733 | { | 901 | { |
902 | GNUNET_NETWORK_socket_close (ar->sock); | ||
903 | GNUNET_free (ar); | ||
734 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | 904 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, |
735 | _ ("Failed to pass client to MHD\n")); | 905 | _ ("Failed to pass client to MHD\n")); |
736 | return; | 906 | return; |
737 | } | 907 | } |
738 | GNUNET_free (s); | ||
739 | schedule_httpd (); | 908 | schedule_httpd (); |
740 | } | 909 | } |
741 | 910 | ||
@@ -1032,6 +1201,12 @@ run (void *cls, | |||
1032 | NULL, | 1201 | NULL, |
1033 | MHD_OPTION_CONNECTION_TIMEOUT, | 1202 | MHD_OPTION_CONNECTION_TIMEOUT, |
1034 | (unsigned int) 16, | 1203 | (unsigned int) 16, |
1204 | MHD_OPTION_NOTIFY_CONNECTION, | ||
1205 | &mhd_connection_cb, | ||
1206 | NULL, | ||
1207 | MHD_OPTION_URI_LOG_CALLBACK, | ||
1208 | mhd_log_callback, | ||
1209 | NULL, | ||
1035 | MHD_OPTION_NOTIFY_COMPLETED, | 1210 | MHD_OPTION_NOTIFY_COMPLETED, |
1036 | &mhd_completed_cb, | 1211 | &mhd_completed_cb, |
1037 | NULL, | 1212 | NULL, |