#include "node.hpp" #include "logging.hpp" #include "bignum.h" #include using namespace boost::asio::ip; Node::Node(ListenSettings const& listen_settings, NodeNetworkSettings network_settings) : io_service() , server(io_service, listen_settings, [this](boost::asio::ip::tcp::socket&& socket){accept_handler(std::move(socket));}) , clients() , network_settings(network_settings) , prev_node(ProtobufClient(tcp::socket(io_service))) , next_node(ProtobufClient(tcp::socket(io_service))) , api(get_implementation()) , keypair(api.create_key_pair()) , network_pub_key() { GOOGLE_PROTOBUF_VERIFY_VERSION; auto on_connect = [this, network_settings](){ next_node.async_send(cmix_proto::ImANode()); if(network_settings.is_first) { start_initialisation(); } }; next_node.async_connect(network_settings.next_host, network_settings.next_port, on_connect); } Node::~Node() { api.free_key_pair(keypair); } void Node::run() { io_service.run(); } void Node::accept_handler(boost::asio::ip::tcp::socket&& socket) { std::list>::iterator it = purgatory.emplace(purgatory.end(), std::move(socket)); purgatory.back().on_done( [this, it]() { purgatory.erase(it); } ); it->receive([this, it](cmix_proto::CMixMessage message) { handle_message(it, message); }); } void Node::start_initialisation() { cmix_proto::Initialization init; init.set_public_share(keypair.pub, keypair.pub_len); next_node.async_send(init); } void Node::handle_initialization(const cmix_proto::Initialization& init) { if(network_settings.is_first) { std::string share = init.public_share(); network_pub_key = std::vector(share.begin(), share.end()); start_precomputation(); } else { Bignum shared = allocate_bignum(init.public_share().size()); std::copy_n(init.public_share().data(), init.public_share().size(), shared.data); Bignum my_share{keypair.pub, keypair.pub_len}; Bignum mod = allocate_bignum(keypair.pub_len); Bignum new_shared = allocate_bignum(keypair.pub_len); if(multiply_mod(&new_shared, shared, my_share, mod) != NoError) { BOOST_LOG_TRIVIAL(fatal) << "Group multiplication failed"; throw std::runtime_error("Group multiplication failed"); } cmix_proto::Initialization init; init.set_public_share(new_shared.data, new_shared.len); next_node.async_send(init); free_bignum(&shared); free_bignum(&mod); free_bignum(&new_shared); } } void Node::handle_node_message(cmix_proto::CMixMessage message) { switch(message.contents_case()) { case cmix_proto::CMixMessage::ContentsCase::kInitialization: { handle_initialization(message.initialization()); break; } case cmix_proto::CMixMessage::ContentsCase::kBye: { prev_node.close(); return; } default: { BOOST_LOG_TRIVIAL(error) << "handle_node_message: CMixMessage contains unknown contents."; } } prev_node.receive([this](cmix_proto::CMixMessage message) { handle_node_message(message); }); } void Node::handle_client_message(std::list::iterator handle, cmix_proto::CMixMessage message) { switch(message.contents_case()) { case cmix_proto::CMixMessage::ContentsCase::kKeyexchange: { BOOST_LOG_TRIVIAL(trace) << "Deriving shared key"; api.derive_shared_key(keypair, reinterpret_cast(message.keyexchange().public_key().c_str()), true); handle->receive([this, handle](cmix_proto::CMixMessage message){ handle_client_message(handle, message); }); return; } case cmix_proto::CMixMessage::ContentsCase::kBye: { BOOST_LOG_TRIVIAL(trace) << "Handling bye"; handle->close(); clients.erase(handle); return; } default: { BOOST_LOG_TRIVIAL(error) << "handle_client_message: CMixMessage contains unknown contents."; } } handle->close(); clients.erase(handle); } void Node::handle_imanode(std::list::iterator handle) { handle->on_done([]{}); prev_node = make_receiver(std::move(*handle)); purgatory.erase(handle); prev_node.receive([this](cmix_proto::CMixMessage message){ handle_node_message(message); }); } void Node::handle_imaclient(std::list::iterator handle) { BOOST_LOG_TRIVIAL(trace) << "Handling imaclient"; std::list::iterator it = clients.emplace(clients.end(), make_sender_receiver(std::move(*handle))); it->on_done([this, it]{ clients.erase(it); }); purgatory.erase(handle); it->receive([this, it](cmix_proto::CMixMessage message) { handle_client_message(it, message); }); } void Node::handle_message(std::list>::iterator handle, cmix_proto::CMixMessage message) { switch(message.contents_case()) { case cmix_proto::CMixMessage::ContentsCase::kImanode: { handle_imanode(handle); return; } case cmix_proto::CMixMessage::ContentsCase::kImaclient: { handle_imaclient(handle); return; } default: { BOOST_LOG_TRIVIAL(error) << "handle_message: CMixMessage contains unknown contents."; } } purgatory.erase(handle); } void Node::start_precomputation() { }