From 8a946bb7c2bc6bb863980ecf16607c57c2b077ec Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sun, 19 Feb 2017 01:44:37 +0100 Subject: towards converting SP to new CADET API; still fails tests though --- .../gnunet-service-scalarproduct_alice.c | 213 ++++++++++----------- 1 file changed, 101 insertions(+), 112 deletions(-) (limited to 'src/scalarproduct/gnunet-service-scalarproduct_alice.c') diff --git a/src/scalarproduct/gnunet-service-scalarproduct_alice.c b/src/scalarproduct/gnunet-service-scalarproduct_alice.c index 45d1f4e2c..779d84b60 100644 --- a/src/scalarproduct/gnunet-service-scalarproduct_alice.c +++ b/src/scalarproduct/gnunet-service-scalarproduct_alice.c @@ -1,6 +1,6 @@ /* This file is part of GNUnet. - Copyright (C) 2013, 2014 GNUnet e.V. + Copyright (C) 2013, 2014, 2017 GNUnet e.V. GNUnet is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published @@ -248,15 +248,10 @@ destroy_service_session (struct AliceServiceSession *s) if (GNUNET_YES == s->in_destroy) return; s->in_destroy = GNUNET_YES; - if (NULL != s->cadet_mq) - { - GNUNET_MQ_destroy (s->cadet_mq); - s->cadet_mq = NULL; - } if (NULL != s->client) { struct GNUNET_SERVICE_Client *c = s->client; - + s->client = NULL; GNUNET_SERVICE_client_drop (c); } @@ -428,17 +423,14 @@ transmit_client_response (struct AliceServiceSession *s) * * It must NOT call #GNUNET_CADET_channel_destroy() on the channel. * - * @param cls closure (set from #GNUNET_CADET_connect()) + * @param cls our `struct AliceServiceSession` * @param channel connection to the other end (henceforth invalid) - * @param channel_ctx place where local state associated - * with the channel is stored */ static void cb_channel_destruction (void *cls, - const struct GNUNET_CADET_Channel *channel, - void *channel_ctx) + const struct GNUNET_CADET_Channel *channel) { - struct AliceServiceSession *s = channel_ctx; + struct AliceServiceSession *s = cls; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer disconnected, terminating session %s with peer %s\n", @@ -450,11 +442,6 @@ cb_channel_destruction (void *cls, s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE; prepare_client_end_notification (s); } - if (NULL != s->cadet_mq) - { - GNUNET_MQ_destroy (s->cadet_mq); - s->cadet_mq = NULL; - } s->channel = NULL; } @@ -633,42 +620,24 @@ compute_scalar_product (struct AliceServiceSession *session) /** - * Handle a multipart chunk of a response we got from another service + * Check a multipart chunk of a response we got from another service * we wanted to calculate a scalarproduct with. * - * @param cls closure (set from #GNUNET_CADET_connect) - * @param channel connection to the other end - * @param channel_ctx place to store local state associated with the @a channel - * @param message the actual message + * @param cls the `struct AliceServiceSession` + * @param msg the actual message * @return #GNUNET_OK to keep the connection open, * #GNUNET_SYSERR to close it (signal serious error) */ static int -handle_bobs_cryptodata_multipart (void *cls, - struct GNUNET_CADET_Channel *channel, - void **channel_ctx, - const struct GNUNET_MessageHeader *message) +check_bobs_cryptodata_multipart (void *cls, + const struct BobCryptodataMultipartMessage *msg) { - struct AliceServiceSession *s = *channel_ctx; - const struct BobCryptodataMultipartMessage *msg; - const struct GNUNET_CRYPTO_PaillierCiphertext *payload; - size_t i; + struct AliceServiceSession *s = cls; uint32_t contained; size_t msg_size; size_t required_size; - if (NULL == s) - { - GNUNET_break_op (0); - return GNUNET_SYSERR; - } - msg_size = ntohs (message->size); - if (sizeof (struct BobCryptodataMultipartMessage) > msg_size) - { - GNUNET_break_op (0); - return GNUNET_SYSERR; - } - msg = (const struct BobCryptodataMultipartMessage *) message; + msg_size = ntohs (msg->header.size); contained = ntohl (msg->contained_element_count); required_size = sizeof (struct BobCryptodataMultipartMessage) + 2 * contained * sizeof (struct GNUNET_CRYPTO_PaillierCiphertext); @@ -678,7 +647,26 @@ handle_bobs_cryptodata_multipart (void *cls, GNUNET_break (0); return GNUNET_SYSERR; } + return GNUNET_OK; +} +/** + * Handle a multipart chunk of a response we got from another service + * we wanted to calculate a scalarproduct with. + * + * @param cls the `struct AliceServiceSession` + * @param msg the actual message + */ +static void +handle_bobs_cryptodata_multipart (void *cls, + const struct BobCryptodataMultipartMessage *msg) +{ + struct AliceServiceSession *s = cls; + const struct GNUNET_CRYPTO_PaillierCiphertext *payload; + size_t i; + uint32_t contained; + + contained = ntohl (msg->contained_element_count); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received %u additional crypto values from Bob\n", (unsigned int) contained); @@ -688,60 +676,41 @@ handle_bobs_cryptodata_multipart (void *cls, for (i = 0; i < contained; i++) { GNUNET_memcpy (&s->r[s->cadet_received_element_count + i], - &payload[2 * i], - sizeof (struct GNUNET_CRYPTO_PaillierCiphertext)); + &payload[2 * i], + sizeof (struct GNUNET_CRYPTO_PaillierCiphertext)); GNUNET_memcpy (&s->r_prime[s->cadet_received_element_count + i], - &payload[2 * i], - sizeof (struct GNUNET_CRYPTO_PaillierCiphertext)); + &payload[2 * i], + sizeof (struct GNUNET_CRYPTO_PaillierCiphertext)); } s->cadet_received_element_count += contained; GNUNET_CADET_receive_done (s->channel); if (s->cadet_received_element_count != s->used_element_count) - return GNUNET_OK; + return; /* more to come */ s->product = compute_scalar_product (s); transmit_client_response (s); - return GNUNET_OK; } /** - * Handle a response we got from another service we wanted to + * Check a response we got from another service we wanted to * calculate a scalarproduct with. * - * @param cls closure (set from #GNUNET_CADET_connect) - * @param channel connection to the other end - * @param channel_ctx place to store local state associated with the channel + * @param cls our `struct AliceServiceSession` * @param message the actual message * @return #GNUNET_OK to keep the connection open, * #GNUNET_SYSERR to close it (we are done) */ static int -handle_bobs_cryptodata_message (void *cls, - struct GNUNET_CADET_Channel *channel, - void **channel_ctx, - const struct GNUNET_MessageHeader *message) +check_bobs_cryptodata_message (void *cls, + const struct BobCryptodataMessage *msg) { - struct AliceServiceSession *s = *channel_ctx; - const struct BobCryptodataMessage *msg; - const struct GNUNET_CRYPTO_PaillierCiphertext *payload; - uint32_t i; + struct AliceServiceSession *s = cls; uint32_t contained; uint16_t msg_size; size_t required_size; - if (NULL == s) - { - GNUNET_break_op (0); - return GNUNET_SYSERR; - } - msg_size = ntohs (message->size); - if (sizeof (struct BobCryptodataMessage) > msg_size) - { - GNUNET_break_op (0); - return GNUNET_SYSERR; - } - msg = (const struct BobCryptodataMessage *) message; + msg_size = ntohs (msg->header.size); contained = ntohl (msg->contained_element_count); required_size = sizeof (struct BobCryptodataMessage) + 2 * contained * sizeof (struct GNUNET_CRYPTO_PaillierCiphertext) @@ -765,29 +734,51 @@ handle_bobs_cryptodata_message (void *cls, GNUNET_break_op (0); return GNUNET_SYSERR; } + return GNUNET_OK; +} + + +/** + * Handle a response we got from another service we wanted to + * calculate a scalarproduct with. + * + * @param cls our `struct AliceServiceSession` + * @param msg the actual message + */ +static void +handle_bobs_cryptodata_message (void *cls, + const struct BobCryptodataMessage *msg) +{ + struct AliceServiceSession *s = cls; + const struct GNUNET_CRYPTO_PaillierCiphertext *payload; + uint32_t i; + uint32_t contained; + + contained = ntohl (msg->contained_element_count); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received %u crypto values from Bob\n", (unsigned int) contained); - payload = (const struct GNUNET_CRYPTO_PaillierCiphertext *) &msg[1]; GNUNET_memcpy (&s->s, - &payload[0], - sizeof (struct GNUNET_CRYPTO_PaillierCiphertext)); + &payload[0], + sizeof (struct GNUNET_CRYPTO_PaillierCiphertext)); GNUNET_memcpy (&s->s_prime, - &payload[1], - sizeof (struct GNUNET_CRYPTO_PaillierCiphertext)); + &payload[1], + sizeof (struct GNUNET_CRYPTO_PaillierCiphertext)); payload = &payload[2]; - s->r = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_PaillierCiphertext) * s->used_element_count); - s->r_prime = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_PaillierCiphertext) * s->used_element_count); + s->r = GNUNET_new_array (s->used_element_count, + struct GNUNET_CRYPTO_PaillierCiphertext); + s->r_prime = GNUNET_new_array (s->used_element_count, + struct GNUNET_CRYPTO_PaillierCiphertext); for (i = 0; i < contained; i++) { GNUNET_memcpy (&s->r[i], - &payload[2 * i], - sizeof (struct GNUNET_CRYPTO_PaillierCiphertext)); + &payload[2 * i], + sizeof (struct GNUNET_CRYPTO_PaillierCiphertext)); GNUNET_memcpy (&s->r_prime[i], - &payload[2 * i + 1], - sizeof (struct GNUNET_CRYPTO_PaillierCiphertext)); + &payload[2 * i + 1], + sizeof (struct GNUNET_CRYPTO_PaillierCiphertext)); } s->cadet_received_element_count = contained; GNUNET_CADET_receive_done (s->channel); @@ -795,12 +786,10 @@ handle_bobs_cryptodata_message (void *cls, if (s->cadet_received_element_count != s->used_element_count) { /* More to come */ - return GNUNET_OK; + return; } - s->product = compute_scalar_product (s); transmit_client_response (s); - return GNUNET_OK; } @@ -1066,6 +1055,17 @@ cb_intersection_request_alice (void *cls, static void client_request_complete_alice (struct AliceServiceSession *s) { + struct GNUNET_MQ_MessageHandler cadet_handlers[] = { + GNUNET_MQ_hd_var_size (bobs_cryptodata_message, + GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_CRYPTODATA, + struct BobCryptodataMessage, + s), + GNUNET_MQ_hd_var_size (bobs_cryptodata_multipart, + GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_CRYPTODATA_MULTIPART, + struct BobCryptodataMultipartMessage, + s), + GNUNET_MQ_handler_end () + }; struct ServiceRequestMessage *msg; struct GNUNET_MQ_Envelope *e; @@ -1073,18 +1073,21 @@ client_request_complete_alice (struct AliceServiceSession *s) "Creating new channel for session with key %s.\n", GNUNET_h2s (&s->session_id)); s->channel - = GNUNET_CADET_channel_create (my_cadet, + = GNUNET_CADET_channel_creatE (my_cadet, s, &s->peer, &s->session_id, - GNUNET_CADET_OPTION_RELIABLE); + GNUNET_CADET_OPTION_RELIABLE, + NULL, + &cb_channel_destruction, + cadet_handlers); if (NULL == s->channel) { s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE; prepare_client_end_notification (s); return; } - s->cadet_mq = GNUNET_CADET_mq_create (s->channel); + s->cadet_mq = GNUNET_CADET_get_mq (s->channel); s->intersection_listen = GNUNET_SET_listen (cfg, GNUNET_SET_OPERATION_INTERSECTION, @@ -1110,7 +1113,7 @@ client_request_complete_alice (struct AliceServiceSession *s) /** - * We're receiving additional set data. Check if + * We're receiving additional set data. Check if * @a msg is well-formed. * * @param cls client identification of the client @@ -1118,7 +1121,7 @@ client_request_complete_alice (struct AliceServiceSession *s) * @return #GNUNET_OK if @a msg is well-formed */ static int -check_alice_client_message_multipart (void *cls, +check_alice_client_message_multipart (void *cls, const struct ComputationBobCryptodataMultipartMessage *msg) { struct AliceServiceSession *s = cls; @@ -1148,7 +1151,7 @@ check_alice_client_message_multipart (void *cls, * @param msg the actual message */ static void -handle_alice_client_message_multipart (void *cls, +handle_alice_client_message_multipart (void *cls, const struct ComputationBobCryptodataMultipartMessage *msg) { struct AliceServiceSession *s = cls; @@ -1203,7 +1206,7 @@ handle_alice_client_message_multipart (void *cls, * @return #GNUNET_OK if @a msg is well-formed */ static int -check_alice_client_message (void *cls, +check_alice_client_message (void *cls, const struct AliceComputationMessage *msg) { struct AliceServiceSession *s = cls; @@ -1232,7 +1235,7 @@ check_alice_client_message (void *cls, return GNUNET_OK; } - + /** * Handler for Alice's client request message. * We are doing request-initiation to compute a scalar product with a peer. @@ -1241,7 +1244,7 @@ check_alice_client_message (void *cls, * @param msg the actual message */ static void -handle_alice_client_message (void *cls, +handle_alice_client_message (void *cls, const struct AliceComputationMessage *msg) { struct AliceServiceSession *s = cls; @@ -1382,16 +1385,6 @@ run (void *cls, const struct GNUNET_CONFIGURATION_Handle *c, struct GNUNET_SERVICE_Handle *service) { - static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = { - { &handle_bobs_cryptodata_message, - GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_CRYPTODATA, - 0}, - { &handle_bobs_cryptodata_multipart, - GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_CRYPTODATA_MULTIPART, - 0}, - { NULL, 0, 0} - }; - cfg = c; /* offset has to be sufficiently small to allow computation of: @@ -1400,13 +1393,9 @@ run (void *cls, my_offset = gcry_mpi_new (GNUNET_CRYPTO_PAILLIER_BITS / 3); gcry_mpi_set_bit (my_offset, GNUNET_CRYPTO_PAILLIER_BITS / 3); - GNUNET_CRYPTO_paillier_create (&my_pubkey, &my_privkey); - my_cadet = GNUNET_CADET_connect (cfg, - NULL, - &cb_channel_destruction, - cadet_handlers); + my_cadet = GNUNET_CADET_connecT (cfg); GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL); if (NULL == my_cadet) @@ -1437,7 +1426,7 @@ GNUNET_SERVICE_MAIN GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MULTIPART_ALICE, struct ComputationBobCryptodataMultipartMessage, NULL), - GNUNET_MQ_handler_end ()); + GNUNET_MQ_handler_end ()); /* end of gnunet-service-scalarproduct_alice.c */ -- cgit v1.2.3