aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDennis Brentjes <d.brentjes@gmail.com>2016-10-16 21:38:43 +0200
committerDennis Brentjes <d.brentjes@gmail.com>2016-10-16 21:38:43 +0200
commitc9082aa07394e15cd800bd5ad396c37a2334c98d (patch)
tree2c14dbbf4faba3b7cc02469d26084486bf7114e5
parent20139a86eddc062fdbaacad0d7d6fdbd999f2e18 (diff)
downloadcmix-c9082aa07394e15cd800bd5ad396c37a2334c98d.tar.gz
cmix-c9082aa07394e15cd800bd5ad396c37a2334c98d.tar.bz2
cmix-c9082aa07394e15cd800bd5ad396c37a2334c98d.zip
made the code base more consistent and fixes some small TU issues.
-rw-r--r--client/cmixclient.cpp17
-rw-r--r--client/cmixclient.hpp6
-rw-r--r--libcmix-bignum/bignum.h4
-rw-r--r--libcmix/cmix.c2
-rw-r--r--libcmix/cmix.h10
-rw-r--r--node/node.cpp33
-rw-r--r--node/node.hpp26
7 files changed, 68 insertions, 30 deletions
diff --git a/client/cmixclient.cpp b/client/cmixclient.cpp
index 06994bf..5c94750 100644
--- a/client/cmixclient.cpp
+++ b/client/cmixclient.cpp
@@ -3,19 +3,19 @@
void CMixClient::key_exchange(int i) {
BOOST_LOG_TRIVIAL(trace) << "Sending KeyExchange for node: " << i;
- shared_keys.resize(network_details.size());
cmix_proto::KeyExchange ke;
ke.set_public_key(keypair.pub, keypair.pub_len);
- network_connections[i].async_send(ke);
-
- cmix_proto::Bye bye;
- network_connections[i].async_send(bye);
+ network_connections.at(i).async_send(ke);
+ network_connections.at(i).async_receive([i, this](cmix_proto::CMixMessage message) {
+ handle_message(i, message);
+ });
}
void CMixClient::initialize_connections() {
network_connections.reserve(network_details.size());
+ data.resize(network_details.size());
for(int i = 0; i < network_details.size(); ++i) {
auto handler = [this, i]() mutable {
@@ -34,17 +34,22 @@ void CMixClient::initialize_connections() {
void CMixClient::handle_key_exchange(int node_id, cmix_proto::KeyExchange const& ke)
{
- shared_keys[node_id] = api.derive_shared_key(keypair, reinterpret_cast<uint8_t const*>(ke.public_key().c_str()), false);
+ data.at(node_id).shared_value = api.derive_shared_key(keypair, reinterpret_cast<uint8_t const*>(ke.public_key().c_str()), false);
+
+ cmix_proto::Bye bye;
+ network_connections.at(node_id).async_send(bye);
}
void CMixClient::handle_message(int node_id, cmix_proto::CMixMessage message)
{
switch(message.contents_case()) {
case cmix_proto::CMixMessage::ContentsCase::kKeyexchange: {
+ BOOST_LOG_TRIVIAL(trace) << "handling keyexchange";
handle_key_exchange(node_id, *message.mutable_keyexchange());
return;
}
case cmix_proto::CMixMessage::ContentsCase::kBye: {
+ BOOST_LOG_TRIVIAL(trace) << "handling bye";
network_connections.at(node_id).close();
if(std::all_of(network_connections.begin(), network_connections.end(), [](SenderReceiver const& c) { return c.is_open(); })) {
break;
diff --git a/client/cmixclient.hpp b/client/cmixclient.hpp
index 71be7c9..1edb08e 100644
--- a/client/cmixclient.hpp
+++ b/client/cmixclient.hpp
@@ -18,14 +18,18 @@ struct NodeDetails {
class CMixClient {
+ struct NodeData {
+ SharedKey shared_value;
+ };
+
boost::asio::io_service io_service;
std::vector<NodeDetails> network_details;
std::vector<SenderReceiver> network_connections;
+ std::vector<NodeData> data;
Api api;
KeyPair keypair;
- std::vector<SharedKey> shared_keys;
void key_exchange(int i);
diff --git a/libcmix-bignum/bignum.h b/libcmix-bignum/bignum.h
index 4c9d3da..fe690cc 100644
--- a/libcmix-bignum/bignum.h
+++ b/libcmix-bignum/bignum.h
@@ -32,7 +32,7 @@ struct Bignum {
* \param len The length of the bignum data array.
* \return The Bignum array just allocated.
*/
-struct Bignum allocate_bignum(size_t len) {
+inline struct Bignum allocate_bignum(size_t len) {
return (struct Bignum){
(unsigned char*) malloc(len),
len
@@ -43,7 +43,7 @@ struct Bignum allocate_bignum(size_t len) {
* \brief free_bignum. Counterpart to allocate_bignum.
* \param b The bignum to free
*/
-void free_bignum(struct Bignum* b) {
+inline void free_bignum(struct Bignum* b) {
free(b->data);
b->data = 0;
b->len = 0;
diff --git a/libcmix/cmix.c b/libcmix/cmix.c
index db385c6..e0256cf 100644
--- a/libcmix/cmix.c
+++ b/libcmix/cmix.c
@@ -29,7 +29,7 @@ enum cmix_error set_message(char const* message, struct CMixBuffer b, unsigned i
return no_error;
}
-enum cmix_error calculate_shared_secret(struct Bignum* result, struct Bignum partial_shared, struct Bignum my_share, struct Bignum mod)
+enum cmix_error calculate_shared_key_part(struct Bignum* result, struct Bignum partial_shared, struct Bignum my_share, struct Bignum mod)
{
if(multiply_mod(result, partial_shared, my_share, mod) != NoError) {
return cmix_bignum_error;
diff --git a/libcmix/cmix.h b/libcmix/cmix.h
index 96d578f..e0d9961 100644
--- a/libcmix/cmix.h
+++ b/libcmix/cmix.h
@@ -53,7 +53,15 @@ enum cmix_error get_message(char* message, struct CMixBuffer b, unsigned int ind
*/
enum cmix_error set_message(char const* message, struct CMixBuffer b, unsigned int index);
-enum cmix_error calculate_shared_secret(struct Bignum* result, struct Bignum partial_shared, struct Bignum my_share, struct Bignum mod);
+/*!
+ * \brief calculate_shared_key_part Calculates (partly) the shared key which is needed by all the nodes.
+ * \param result Storage for the result
+ * \param partial_shared The shared key so far.
+ * \param my_share My part to add to the key.
+ * \param mod The modulus of the group we are in.
+ * \return An error_code.
+ */
+enum cmix_error calculate_shared_key_part(struct Bignum* result, struct Bignum partial_shared, struct Bignum my_share, struct Bignum mod);
#ifdef __cplusplus
} // extern "C"
diff --git a/node/node.cpp b/node/node.cpp
index 73eb4b6..2cbcb19 100644
--- a/node/node.cpp
+++ b/node/node.cpp
@@ -1,9 +1,10 @@
#include "node.hpp"
-#include "logging.hpp"
-
+#include "cmix.h"
#include "bignum.h"
+#include "logging.hpp"
+
#include <iostream>
using namespace boost::asio::ip;
@@ -12,6 +13,7 @@ Node::Node(ListenSettings const& listen_settings, NodeNetworkSettings network_se
: io_service()
, server(io_service, listen_settings, [this](boost::asio::ip::tcp::socket&& socket){accept_handler(std::move(socket));})
, clients()
+, data()
, network_settings(network_settings)
, prev_node(Receiver(tcp::socket(io_service)))
, next_node(Sender(tcp::socket(io_service)))
@@ -77,6 +79,8 @@ void Node::handle_node_initialization(const cmix_proto::Initialization& init)
get_curve25519_mod(&mod);
Bignum new_shared = allocate_bignum(keypair.pub_len);
+ calculate_shared_key_part(&new_shared, shared, my_share, mod);
+
cmix_proto::Initialization init;
init.set_public_share(new_shared.data, new_shared.len);
@@ -96,6 +100,8 @@ void Node::handle_node_bye(cmix_proto::Bye) {
prev_node.async_receive([this](cmix_proto::CMixMessage message){
handle_node_message(message);
});
+ } else {
+ io_service.stop();
}
}
@@ -128,17 +134,24 @@ void Node::handle_node_message(cmix_proto::CMixMessage message)
});
}
-void Node::handle_client_keyexchange(ClientMap::key_type handle, cmix_proto::KeyExchange ke) {
- api.derive_shared_key(keypair, reinterpret_cast<uint8_t const*>(ke.public_key().c_str()), true);
+void Node::handle_client_keyexchange(ClientConnections::key_type handle, cmix_proto::KeyExchange ke) {
+ data[handle].shared_value = api.derive_shared_key(keypair, reinterpret_cast<uint8_t const*>(ke.public_key().c_str()), true);
+
+ cmix_proto::KeyExchange exchange;
+ exchange.set_public_key(keypair.pub, keypair.pub_len);
+ clients.at(handle).async_send(exchange);
}
-void Node::handle_client_bye(ClientMap::key_type handle, cmix_proto::Bye)
+void Node::handle_client_bye(ClientConnections::key_type handle, cmix_proto::Bye)
{
clients.at(handle).close();
clients.erase(handle);
+ if(clients.size() == 0) {
+ send_bye();
+ }
}
-void Node::handle_client_message(ClientMap::key_type handle, cmix_proto::CMixMessage message)
+void Node::handle_client_message(ClientConnections::key_type handle, cmix_proto::CMixMessage message)
{
switch(message.contents_case()) {
case cmix_proto::CMixMessage::ContentsCase::kKeyexchange: {
@@ -160,7 +173,7 @@ void Node::handle_client_message(ClientMap::key_type handle, cmix_proto::CMixMes
});
}
-void Node::handle_imanode(std::list<Receiver>::iterator handle) {
+void Node::handle_imanode(Purgatory::iterator handle) {
handle->on_done([]{});
prev_node = Receiver(std::move(*handle));
purgatory.erase(handle);
@@ -169,8 +182,7 @@ void Node::handle_imanode(std::list<Receiver>::iterator handle) {
});
}
-void Node::handle_imaclient(std::list<Receiver>::iterator handle, cmix_proto::ImAClient c) {
- BOOST_LOG_TRIVIAL(trace) << "Handling imaclient";
+void Node::handle_imaclient(Purgatory::iterator handle, cmix_proto::ImAClient c) {
std::string client_id = c.id();
clients.emplace(c.id(), SenderReceiver(std::move(*handle)));
clients.at(c.id()).on_done([this, client_id]{
@@ -182,7 +194,7 @@ void Node::handle_imaclient(std::list<Receiver>::iterator handle, cmix_proto::Im
});
}
-void Node::handle_message(std::list<Receiver>::iterator handle, cmix_proto::CMixMessage message)
+void Node::handle_message(Purgatory::iterator handle, cmix_proto::CMixMessage message)
{
switch(message.contents_case()) {
case cmix_proto::CMixMessage::ContentsCase::kImanode: {
@@ -200,6 +212,7 @@ void Node::handle_message(std::list<Receiver>::iterator handle, cmix_proto::CMix
}
}
+ handle->close();
purgatory.erase(handle);
}
diff --git a/node/node.hpp b/node/node.hpp
index 76f3c0d..9dfc343 100644
--- a/node/node.hpp
+++ b/node/node.hpp
@@ -31,12 +31,20 @@ struct NodeNetworkSettings {
*/
class Node
{
+ struct CMixClientData {
+ SharedKey shared_value;
+ };
+
boost::asio::io_service io_service;
Server server;
- std::list<Receiver> purgatory;
- typedef std::map<std::string, SenderReceiver> ClientMap;
- ClientMap clients;
+ typedef std::list<Receiver> Purgatory;
+ Purgatory purgatory;
+
+ typedef std::map<std::string, SenderReceiver> ClientConnections;
+ ClientConnections clients;
+ typedef std::map<std::string, CMixClientData> ClientData;
+ ClientData data;
NodeNetworkSettings network_settings;
@@ -60,13 +68,13 @@ class Node
void handle_node_bye(cmix_proto::Bye);
void handle_node_message(cmix_proto::CMixMessage message);
- void handle_client_keyexchange(ClientMap::key_type handle, cmix_proto::KeyExchange ke);
- void handle_client_bye(ClientMap::key_type handle, cmix_proto::Bye);
- void handle_client_message(ClientMap::key_type handle, cmix_proto::CMixMessage message);
+ void handle_client_keyexchange(ClientConnections::key_type handle, cmix_proto::KeyExchange ke);
+ void handle_client_bye(ClientConnections::key_type handle, cmix_proto::Bye);
+ void handle_client_message(ClientConnections::key_type handle, cmix_proto::CMixMessage message);
- void handle_imanode(std::list<Receiver>::iterator handle);
- void handle_imaclient(std::list<Receiver>::iterator handle, cmix_proto::ImAClient c);
- void handle_message(std::list<Receiver>::iterator handle, cmix_proto::CMixMessage message);
+ void handle_imanode(Purgatory::iterator handle);
+ void handle_imaclient(Purgatory::iterator handle, cmix_proto::ImAClient c);
+ void handle_message(Purgatory::iterator handle, cmix_proto::CMixMessage message);
public:
/*!