aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--client/cmixclient.cpp12
-rw-r--r--libcmix-crypto/elgamal/gcrypt/gcrypt_elgamal.c5
-rw-r--r--libcmix-network/acceptor.cpp3
-rw-r--r--libcmix-network/client.hpp5
-rw-r--r--libcmix-network/server.cpp8
-rw-r--r--libcmix/cmix.c36
-rw-r--r--libcmix/cmix.h2
-rw-r--r--node/node.cpp27
-rw-r--r--node/node.hpp2
-rw-r--r--node/node_node.cpp22
10 files changed, 94 insertions, 28 deletions
diff --git a/client/cmixclient.cpp b/client/cmixclient.cpp
index 636398f..dafa869 100644
--- a/client/cmixclient.cpp
+++ b/client/cmixclient.cpp
@@ -83,12 +83,12 @@ void CMixClient::handle_key_exchange(size_t node_id, cmix_proto::KeyExchange con
BOOST_LOG_TRIVIAL(trace) << "sending UserMessage: " << message.ShortDebugString();
network_connections.at(0).async_send(message);
- size_t last_node_id = network_details.node_details.size() - 1;
- network_connections.at(last_node_id).async_receive(
- [this, last_node_id](cmix_proto::CMixMessage const& message) {
- handle_message(last_node_id, message);
- }
- );
+// size_t last_node_id = network_details.node_details.size() - 1;
+// network_connections.at(last_node_id).async_receive(
+// [this, last_node_id](cmix_proto::CMixMessage const& message) {
+// handle_message(last_node_id, message);
+// }
+// );
}
}
diff --git a/libcmix-crypto/elgamal/gcrypt/gcrypt_elgamal.c b/libcmix-crypto/elgamal/gcrypt/gcrypt_elgamal.c
index 21a7618..4e96466 100644
--- a/libcmix-crypto/elgamal/gcrypt/gcrypt_elgamal.c
+++ b/libcmix-crypto/elgamal/gcrypt/gcrypt_elgamal.c
@@ -283,8 +283,13 @@ unsigned int gcrypt_elgamal_get_uniform_int(unsigned int upper) {
void gcrypt_elgamal_deinitialize(void) {
gcry_mpi_release(p);
+ p = NULL;
+
gcry_mpi_release(q);
+ q = NULL;
+
gcry_mpi_release(g);
+ g = NULL;
}
Initializer elgamal_initialize = &gcrypt_elgamal_initialize;
diff --git a/libcmix-network/acceptor.cpp b/libcmix-network/acceptor.cpp
index ee8833a..02a0541 100644
--- a/libcmix-network/acceptor.cpp
+++ b/libcmix-network/acceptor.cpp
@@ -56,5 +56,6 @@ void Acceptor::start_accepting(std::shared_ptr<ssl::context> ctx, SSLAcceptHandl
void Acceptor::close()
{
- acceptor.close();
+ acceptor.get_io_service().post([this]{acceptor.cancel();});
+ acceptor.get_io_service().post([this]{acceptor.close();});
}
diff --git a/libcmix-network/client.hpp b/libcmix-network/client.hpp
index c583628..b43f32b 100644
--- a/libcmix-network/client.hpp
+++ b/libcmix-network/client.hpp
@@ -189,7 +189,7 @@ public:
* \brief close Closes the underlying socket.
*/
void close() {
- socket->close();
+ socket->next_layer().shutdown(boost::asio::ip::tcp::socket::shutdown_type::shutdown_both);
}
/*!
@@ -268,7 +268,8 @@ struct SSLClient : private BaseClient<boost::asio::ssl::stream<boost::asio::ip::
* \brief close Closes the underlying socket.
*/
void close() {
- socket->lowest_layer().cancel();
+ socket->shutdown();
+ BaseClient::close();
}
using BaseClient::on_done;
diff --git a/libcmix-network/server.cpp b/libcmix-network/server.cpp
index 195c5a0..a931c1e 100644
--- a/libcmix-network/server.cpp
+++ b/libcmix-network/server.cpp
@@ -66,6 +66,10 @@ Server::Server(io_service& io_service, const ListenSettings& listen_settings, st
void Server::close()
{
- v4_acceptor.close();
- v6_acceptor.close();
+ if(v4_acceptor.is_open()) {
+ v4_acceptor.close();
+ }
+ if(v6_acceptor.is_open()) {
+ v6_acceptor.close();
+ }
} \ No newline at end of file
diff --git a/libcmix/cmix.c b/libcmix/cmix.c
index d21dab5..c60f441 100644
--- a/libcmix/cmix.c
+++ b/libcmix/cmix.c
@@ -24,6 +24,17 @@ struct CMixContext initialize_cmix_context(struct Api api) {
};
}
+enum cmix_error alloc_mix(struct CMixContext* ctx) {
+ ctx->r = (GroupElement*) calloc(ctx->nr_participants, sizeof(GroupElement));
+ ctx->s = (GroupElement*) calloc(ctx->nr_participants, sizeof(GroupElement));
+ ctx->permutation = (unsigned int*) calloc(ctx->nr_participants, sizeof(unsigned int));
+ ctx->pirs = (GroupElement*) calloc(ctx->nr_participants, sizeof(GroupElement));
+ if(!ctx->r || !ctx->s || !ctx->permutation || !ctx->pirs) {
+ return out_of_memory;
+ }
+ return no_error;
+}
+
void release_mix(struct CMixContext* ctx) {
for(size_t i = 0; i < ctx->nr_participants; ++i) {
ctx->api.free_group_element(ctx->r[i]);
@@ -31,9 +42,18 @@ void release_mix(struct CMixContext* ctx) {
ctx->api.free_group_element(ctx->pirs[i]);
}
free(ctx->r);
+ ctx->r = NULL;
+
free(ctx->s);
+ ctx->s = NULL;
+
free(ctx->permutation);
+ ctx->permutation = NULL;
+
free(ctx->pirs);
+ ctx->pirs = NULL;
+
+ ctx->nr_participants = 0;
}
void deinitialize(struct CMixContext* ctx)
@@ -77,17 +97,6 @@ enum cmix_error add_public_share(struct CMixContext const* ctx, char* buffer, ch
ctx->api.free_group_element(el);
}
-enum cmix_error alloc_mix(struct CMixContext* ctx) {
- ctx->r = (GroupElement*) calloc(ctx->nr_participants, sizeof(GroupElement));
- ctx->s = (GroupElement*) calloc(ctx->nr_participants, sizeof(GroupElement));
- ctx->permutation = (unsigned int*) calloc(ctx->nr_participants, sizeof(unsigned int));
- ctx->pirs = (GroupElement*) calloc(ctx->nr_participants, sizeof(GroupElement));
- if(!ctx->r || !ctx->s || !ctx->permutation || !ctx->pirs) {
- return out_of_memory;
- }
- return no_error;
-}
-
enum cmix_error start_mix(struct CMixContext* ctx, size_t nr_participants) {
release_mix(ctx);
ctx->nr_participants = nr_participants;
@@ -95,7 +104,10 @@ enum cmix_error start_mix(struct CMixContext* ctx, size_t nr_participants) {
}
enum cmix_error generate_permutation(struct CMixContext* ctx) {
- ctx->permutation = (unsigned int*) calloc(ctx->nr_participants, sizeof(unsigned int));
+ if (ctx->nr_participants == 0) {
+ return no_error;
+ }
+
for(unsigned int i = 0; i < ctx->nr_participants; ++i) {
ctx->permutation[i] = i;
}
diff --git a/libcmix/cmix.h b/libcmix/cmix.h
index 057fd60..8e6f598 100644
--- a/libcmix/cmix.h
+++ b/libcmix/cmix.h
@@ -73,6 +73,8 @@ enum cmix_error get_pub_key_hash(struct CMixContext const* ctx, char** buffer);
enum cmix_error start_mix(struct CMixContext* ctx, size_t nr_participants);
+void release_mix(struct CMixContext* ctx);
+
enum cmix_error initialize_mix_randomness(struct CMixContext* ctx);
enum cmix_error generate_random_message(struct CMixContext* ctx, char* buffer);
diff --git a/node/node.cpp b/node/node.cpp
index 3ad4a2d..cc5e47a 100644
--- a/node/node.cpp
+++ b/node/node.cpp
@@ -154,12 +154,12 @@ void Node::handle_message(Purgatory::iterator handle, cmix_proto::CMixMessage me
void Node::start_precomputation() {
BOOST_LOG_TRIVIAL(trace) << "Starting precomputation for " << clients.size() << " clients.";
index_map.clear();
- if(start_mix(&cmix_ctx, clients.size()) != no_error) {
+ if(start_mix(&cmix_ctx, messages.size()) != no_error) {
exit(-1);
}
unsigned int i = 0;
- for(auto&& pair : clients) {
+ for(auto&& pair : messages) {
index_map[pair.first] = i++;
}
@@ -252,3 +252,26 @@ void Node::start_realtime_phase() {
next_node.async_send(realpre);
}
+
+void Node::shutdown()
+{
+ server.close();
+ for(auto&& pair : clients) {
+ pair.second.async_send(cmix_proto::Bye());
+ }
+}
+
+bool Node::send_bye(bool got_bye)
+{
+ if(got_bye) {
+ if(!shutting_down) {
+ next_node.async_send(cmix_proto::Bye());
+ }
+ io_service.stop();
+ return true;
+ } else {
+ next_node.async_send(cmix_proto::Bye());
+ shutting_down = true;
+ return false;
+ }
+}
diff --git a/node/node.hpp b/node/node.hpp
index b166c71..db3c66f 100644
--- a/node/node.hpp
+++ b/node/node.hpp
@@ -83,6 +83,8 @@ class Node
void start_initialisation();
void start_precomputation();
void start_realtime_phase();
+ void shutdown();
+ bool send_bye(bool got_bye);
void handle_node_initialization(cmix_proto::Initialization const& init);
void handle_node_secretkey(cmix_proto::SecretKey const& secret);
diff --git a/node/node_node.cpp b/node/node_node.cpp
index 7990a12..0614914 100644
--- a/node/node_node.cpp
+++ b/node/node_node.cpp
@@ -246,9 +246,14 @@ void Node::handle_node_prepost(cmix_proto::PrePost const& prepost) {
}
void Node::handle_node_realpre(cmix_proto::RealPre const& realpre) {
+ auto final = [this](){
+ deinitialize(&cmix_ctx);
+ shutdown();
+ };
+
if(network_settings.is_first) {
cmix_proto::RealMix n_realmix = fill_realtime_mix_message(cmix_ctx, realpre.m());
- next_node.async_send(n_realmix);
+ next_node.async_send(n_realmix, final);
} else {
cmix_proto::RealPre n_realpre = fill_realtime_pre_message(cmix_ctx, realpre.h(), realpre.m(), data);
next_node.async_send(n_realpre);
@@ -256,6 +261,12 @@ void Node::handle_node_realpre(cmix_proto::RealPre const& realpre) {
}
void Node::handle_node_realmix(cmix_proto::RealMix const& realmix) {
+ auto final = [this](){
+ deinitialize(&cmix_ctx);
+ shutdown();
+ send_bye(false);
+ };
+
if(network_settings.is_last) {
BOOST_LOG_TRIVIAL(trace) << "Doing the last step:";
@@ -277,6 +288,8 @@ void Node::handle_node_realmix(cmix_proto::RealMix const& realmix) {
}
}
+ final();
+
} else {
cmix_proto::RealMix n_realmix = fill_realtime_mix_message(cmix_ctx, realmix.m());
next_node.async_send(n_realmix);
@@ -294,8 +307,11 @@ void Node::handle_node_message(cmix_proto::CMixMessage message)
}
case cmix_proto::CMixMessage::ContentsCase::kBye: {
BOOST_LOG_TRIVIAL(trace) << "Handling bye";
- //Todo: find a nice way to handle network shutdown.
- break;
+ if(send_bye(true)) {
+ return;
+ } else {
+ break;
+ }
}
case cmix_proto::CMixMessage::ContentsCase::kSecretkey: {
BOOST_LOG_TRIVIAL(trace) << "Handling SecretKey";