From 00ab2cf6add2976b3a4e8f8cc488777ad5c27808 Mon Sep 17 00:00:00 2001 From: Dennis Brentjes Date: Fri, 26 Aug 2016 11:21:41 +0200 Subject: Initial commit, just some ideas and testing. --- .gitignore | 2 + CMakeLists.txt | 20 +++++++++ Doxyfile.in | 1 + libcmix-crypto/CMakeLists.txt | 12 +++++ libcmix-crypto/message.c | 0 libcmix-crypto/message.h | 85 ++++++++++++++++++++++++++++++++++++ libcmix/CMakeLists.txt | 8 ++++ libcmix/cmix.c | 28 ++++++++++++ libcmix/cmix.h | 51 ++++++++++++++++++++++ network-handler/CMakeLists.txt | 17 ++++++++ network-handler/main.cpp | 42 ++++++++++++++++++ network-handler/networkhandler.cpp | 89 ++++++++++++++++++++++++++++++++++++++ network-handler/networkhandler.hpp | 34 +++++++++++++++ scratchpad/CMakeLists.txt | 9 ++++ scratchpad/scratchpad.c | 26 +++++++++++ 15 files changed, 424 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 Doxyfile.in create mode 100644 libcmix-crypto/CMakeLists.txt create mode 100644 libcmix-crypto/message.c create mode 100644 libcmix-crypto/message.h create mode 100644 libcmix/CMakeLists.txt create mode 100644 libcmix/cmix.c create mode 100644 libcmix/cmix.h create mode 100644 network-handler/CMakeLists.txt create mode 100644 network-handler/main.cpp create mode 100644 network-handler/networkhandler.cpp create mode 100644 network-handler/networkhandler.hpp create mode 100644 scratchpad/CMakeLists.txt create mode 100644 scratchpad/scratchpad.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..394decf --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +CMakeLists.txt.user +build* diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..0378b04 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,20 @@ +cmake_minimum_required(VERSION 3.6.1) + +project(cmix) + +find_package(Doxygen) +if(DOXYGEN_FOUND) + configure_file(${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile.in ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile @ONLY) + add_custom_target(doc + ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + COMMENT "Generating API documentation with Doxygen" VERBATIM + ) +endif(DOXYGEN_FOUND) + +add_subdirectory(libcmix) +add_subdirectory(libcmix-crypto) + +add_subdirectory(network-handler) + +add_subdirectory(scratchpad) diff --git a/Doxyfile.in b/Doxyfile.in new file mode 100644 index 0000000..0b77583 --- /dev/null +++ b/Doxyfile.in @@ -0,0 +1 @@ +INPUT = @CMAKE_CURRENT_SOURCE_DIR@/libcmix @CMAKE_CURRENT_SOURCE_DIR@/libcmix-crypto diff --git a/libcmix-crypto/CMakeLists.txt b/libcmix-crypto/CMakeLists.txt new file mode 100644 index 0000000..3bbbae6 --- /dev/null +++ b/libcmix-crypto/CMakeLists.txt @@ -0,0 +1,12 @@ + +add_library(cmix-crypto + message.h message.c +) + +target_include_directories(cmix-crypto + PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} +) + +target_compile_options(cmix-crypto + PUBLIC "-std=c99" +) \ No newline at end of file diff --git a/libcmix-crypto/message.c b/libcmix-crypto/message.c new file mode 100644 index 0000000..e69de29 diff --git a/libcmix-crypto/message.h b/libcmix-crypto/message.h new file mode 100644 index 0000000..98cdb9a --- /dev/null +++ b/libcmix-crypto/message.h @@ -0,0 +1,85 @@ + +#include +#include + +typedef char*(*CmixBufferAllocator)(size_t); +typedef void(*CmixBufferDeallocator)(void*); +typedef size_t(*CmixBufferMessageLength)(); + +struct CmixBufferImpl { + CmixBufferAllocator allocate_cmix_buffer; + CmixBufferDeallocator deallocate_cmix_buffer; + CmixBufferMessageLength message_length; +}; + +#define DEFINE_CIPHER(NAME, MESSAGE_SIZE)\ +typedef char NAME ## Message[MESSAGE_SIZE];\ +\ +char* allocate_ ## NAME ## _cmix_buffer(size_t size){\ + return (char*) calloc(size, sizeof(NAME ## Message));\ +}\ +\ +void deallocate_ ## NAME ## _cmix_buffer(void* buffer) {\ + free(buffer);\ +}\ +\ +size_t NAME ## _message_length() {\ + return sizeof(NAME ## Message);\ +}\ +\ +struct CmixBufferImpl get_cmix_ ## NAME ## _buffer_implementation() {\ + return (struct CmixBufferImpl) {\ + allocate_ ## NAME ## _cmix_buffer,\ + deallocate_ ## NAME ## _cmix_buffer,\ + NAME ## _message_length\ + };\ +} + +DEFINE_CIPHER(Null, 0) +DEFINE_CIPHER(Curve25519, 20); + +/* +typedef char NullMessage[0]; + +char* allocate_null_cmix_buffer(size_t size) { + return (char*) calloc(size, sizeof(NullMessage)); +} + +void deallocate_null_cmix_buffer(void* buffer) { + free(buffer); +} + +size_t null_message_length() { + return sizeof(NullMessage); +} + +struct CmixBufferImpl get_cmix_null_buffer_implementation() { + return (struct CmixBufferImpl) { + allocate_null_cmix_buffer, + deallocate_null_cmix_buffer, + null_message_length + }; +} + +typedef char Curve25519Message[20]; + +char* allocate_curve25519_cmix_buffer(size_t size) { + return (char*) calloc(size, sizeof(Curve25519Message)); +} + +void deallocate_curve25519_cmix_buffer(void* buffer) { + free(buffer); +} + +size_t curve25519_message_length() { + return sizeof(Curve25519Message); +} + +struct CmixBufferImpl get_cmix_curve25519_buffer_implementation() { + return (struct CmixBufferImpl) { + allocate_curve25519_cmix_buffer, + deallocate_curve25519_cmix_buffer, + curve25519_message_length + }; +} +*/ diff --git a/libcmix/CMakeLists.txt b/libcmix/CMakeLists.txt new file mode 100644 index 0000000..5d87a56 --- /dev/null +++ b/libcmix/CMakeLists.txt @@ -0,0 +1,8 @@ + +add_library(cmix + cmix.h cmix.c +) + +target_include_directories(cmix + PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} +) \ No newline at end of file diff --git a/libcmix/cmix.c b/libcmix/cmix.c new file mode 100644 index 0000000..944ac9c --- /dev/null +++ b/libcmix/cmix.c @@ -0,0 +1,28 @@ + +#include "cmix.h" + +#include + +enum cmix_error permutation(struct CMixBuffer b) { + return no_error; +} + +enum cmix_error get_message(char* message, struct CMixBuffer b, unsigned int index) +{ + if(index >= b.message_length) { + return index_out_of_range; + } + + strncpy(message, b.buffer + index * b.message_length, b.message_length); + + return no_error; +} + +enum cmix_error set_message(char const* message, struct CMixBuffer b, unsigned int index) +{ + if(index >= b.message_length) { + return index_out_of_range; + } + + strncpy(b.buffer + index * b.message_length, message, b.message_length); +} diff --git a/libcmix/cmix.h b/libcmix/cmix.h new file mode 100644 index 0000000..5fe2e13 --- /dev/null +++ b/libcmix/cmix.h @@ -0,0 +1,51 @@ +/** + * \file + */ + +struct message { + char buffer[2048/8]; +}; + +/** + * \struct + * \brief The CMixBuffer struct is the temporary storage of messages in each Node. + * On this buffer, operations like decrypt and permute are performed. + */ +struct CMixBuffer { + char* buffer; ///< The actual buffer + unsigned int nr_messages; ///< The number of messages in the buffer + unsigned int message_length; ///< The length of each message in the buffer +}; + +/** + * \brief The cmix_error enum describes the output state of a each of the cmix functions + */ +enum cmix_error { + no_error = 0, + index_out_of_range = 1000 +}; + +/** + * \brief permutate mixes the messages before sending the messages. + * \param[in,out] b A buffer of \p nr_messages messages, each message of length *message_len* + * \return no_error + */ +enum cmix_error permute(struct CMixBuffer b); + +/** + * \brief get_message takes the \p index message of the buffer \p b copies it into \p message + * \param[out] message is the output buffer that has to hold at least \p b.message_length bytes. + * \param[in] b is the buffer from which to copy the message. + * \param[in] index is the index of the message to copy. + * \return index_out_of_range if \p index is greater than or equal to \p b.nr_messages , otherwise no_error. + */ +enum cmix_error get_message(char* message, struct CMixBuffer b, unsigned int index); + +/** + * \brief set_message set the \p index message of the buffer to a copy of \p message. + * \param[in] message The message to set the part of the buffer to. + * \param[in,out] b The buffer + * \param[in] index The index into the buffer + * \return index_out_of_range if \p index is greater than or equal to \p b.nr_messages , otherwise no_error. + */ +enum cmix_error set_message(char const* message, struct CMixBuffer b, unsigned int index); \ No newline at end of file diff --git a/network-handler/CMakeLists.txt b/network-handler/CMakeLists.txt new file mode 100644 index 0000000..9f3366c --- /dev/null +++ b/network-handler/CMakeLists.txt @@ -0,0 +1,17 @@ + +find_package(Boost COMPONENTS system program_options REQUIRED) + +add_executable(network-handler + main.cpp + networkhandler.hpp networkhandler.cpp +) + +target_compile_options(network-handler + PRIVATE "-std=c++11" +) + +target_link_libraries(network-handler + PRIVATE Boost::boost + PRIVATE Boost::program_options + PRIVATE Boost::system +) \ No newline at end of file diff --git a/network-handler/main.cpp b/network-handler/main.cpp new file mode 100644 index 0000000..128c0e8 --- /dev/null +++ b/network-handler/main.cpp @@ -0,0 +1,42 @@ + +#include "networkhandler.hpp" + +#include + +#include +#include +#include + +int main(int argc, char* argv[]) { + namespace po = boost::program_options; + + po::options_description desc("Allowed options"); + desc.add_options() + ("help,h", "produce help message") + ("port,p", po::value()->default_value(9200), "Set listening port") + ("enable_v4", po::value()->default_value(true), "Enable/disable ipv4 accept support") + ("interface4,4", po::value()->default_value("0.0.0.0"), "Set the ipv4 address to listen on.") + ("enable_v6", po::value()->default_value(true), "Enable/disable ipv6 accept support") + ("interface6,6", po::value()->default_value("::"), "Set the ipv6 address to listen on") + ; + + po::variables_map vm; + po::store(po::parse_command_line(argc, argv, desc), vm); + po::notify(vm); + + if (vm.count("help")) { + std::cout << desc << "\n"; + return 0; + } + + bool en4 = vm["enable_v4"].as(); + std::string if4 = vm["interface4"].as(); + bool en6 = vm["enable_v6"].as(); + std::string if6 = vm["interface6"].as(); + uint16_t port = vm["port"].as(); + + ListenSettings lsettings{en4, if4, en6, if6, port}; + + NetworkHandler handler(lsettings); + handler.run(); +} diff --git a/network-handler/networkhandler.cpp b/network-handler/networkhandler.cpp new file mode 100644 index 0000000..987d2a1 --- /dev/null +++ b/network-handler/networkhandler.cpp @@ -0,0 +1,89 @@ +#include "networkhandler.hpp" + +#include + +#include +#include + +using namespace boost::asio::ip; +using namespace boost::asio; + +void accept_loop(tcp::acceptor& acceptor, std::function f); + +void accept_connection(tcp::acceptor& acceptor, std::shared_ptr socket, boost::system::error_code ec, std::function f) +{ + if(!bool(ec)) + { + f(std::move(*socket)); + accept_loop(acceptor, f); + } else { + std::stringstream ss; + ss << ec; + throw std::runtime_error(ss.str()); + } + +} + +void accept_loop(tcp::acceptor& acceptor, std::function f) +{ + std::shared_ptr new_socket = std::make_shared(acceptor.get_io_service()); + + acceptor.async_accept(*new_socket, boost::bind(accept_connection, boost::ref(acceptor), new_socket, boost::asio::placeholders::error, f)); +} + +void setup_listen_socket(tcp::acceptor& acceptor, tcp::endpoint& endpoint, std::function f) { + acceptor.open(endpoint.protocol()); + acceptor.bind(endpoint); + acceptor.listen(); + + accept_loop(acceptor, f); +} + +NetworkHandler::NetworkHandler(const ListenSettings& listen_settings) +: listen_settings(listen_settings) +, io_service() +, v4_acceptor(io_service) +, v6_acceptor(io_service) +, v4_endpoint(address_v4::from_string(listen_settings.ipv4_inaddr), listen_settings.port) +, v6_endpoint(address_v6::from_string(listen_settings.ipv6_inaddr), listen_settings.port) +{ + auto accept_handler = [this](tcp::socket&& socket) { + accepted_connections.push_back(std::move(socket)); + std::cout << "Yippie" << std::endl; + }; + + /* + * We can't bind both a v4 and v6 socket to both inaddr4_any and inaddr6_any. + * So in case both ipv4 and ipv6 are enabled and both want to bind to their + * respective inaddr_any we need to bind to v6 any and disable ipv6 only on + * the acceptor socket, else we can just bind to the interfaces normally. + */ + bool bind_v4_any = v4_endpoint.address().to_v4() == address_v4::any() && listen_settings.enable_ipv4; + bool bind_v6_any = v6_endpoint.address().to_v6() == address_v6::any() && listen_settings.enable_ipv6; + if(bind_v4_any && bind_v6_any) { + v6_acceptor.open(v6_endpoint.protocol()); + + v6_only option(false); + v6_acceptor.set_option(option); + + v6_acceptor.bind(v6_endpoint); + v6_acceptor.listen(); + + accept_loop(v6_acceptor, accept_handler); + } else if(bind_v4_any || bind_v6_any) { + throw std::runtime_error("Cannot bind an INADDR_ANY and a non INADDR_ANY address on ipv4 and ipv6"); + } else { + if(listen_settings.enable_ipv4) { + setup_listen_socket(v4_acceptor, v4_endpoint, accept_handler); + } + if(listen_settings.enable_ipv6) { + setup_listen_socket(v6_acceptor, v6_endpoint, accept_handler); + } + } +} + +void NetworkHandler::run() { + io_service.run(); +} + + diff --git a/network-handler/networkhandler.hpp b/network-handler/networkhandler.hpp new file mode 100644 index 0000000..d0eab6f --- /dev/null +++ b/network-handler/networkhandler.hpp @@ -0,0 +1,34 @@ +#pragma once + +#include +#include + +#include +#include +#include + +struct ListenSettings { + bool enable_ipv4; + std::string ipv4_inaddr; + bool enable_ipv6; + std::string ipv6_inaddr; + uint16_t port; +}; + +class NetworkHandler +{ + ListenSettings const& listen_settings; + + boost::asio::io_service io_service; + boost::asio::ip::tcp::acceptor v4_acceptor; + boost::asio::ip::tcp::acceptor v6_acceptor; + boost::asio::ip::tcp::endpoint v4_endpoint; + boost::asio::ip::tcp::endpoint v6_endpoint; + + std::list accepted_connections; + +public: + NetworkHandler(ListenSettings const& listen_settings); + + void run(); +}; diff --git a/scratchpad/CMakeLists.txt b/scratchpad/CMakeLists.txt new file mode 100644 index 0000000..1109ca6 --- /dev/null +++ b/scratchpad/CMakeLists.txt @@ -0,0 +1,9 @@ + +add_executable(scratchpad + scratchpad.c +) + +target_link_libraries(scratchpad + PRIVATE cmix + PRIVATE cmix-crypto +) \ No newline at end of file diff --git a/scratchpad/scratchpad.c b/scratchpad/scratchpad.c new file mode 100644 index 0000000..848493d --- /dev/null +++ b/scratchpad/scratchpad.c @@ -0,0 +1,26 @@ + +#include "cmix.h" +#include "message.h" + +#include + +int main(int argc, char* argv[]) { + + struct CmixBufferImpl buffer_impl = get_cmix_Curve25519_buffer_implementation(); + char* buffer = buffer_impl.allocate_cmix_buffer(3); + + int message_size = buffer_impl.message_length(); + + for(int i=0; i < 3; i++) { + buffer[i*message_size] = 'h'; + } + + for(int i=0; i < 3; i++) { + putc(buffer[i*message_size], stdout); + putc('\n', stdout); + } + + buffer_impl.deallocate_cmix_buffer(buffer); + + return 0; +} -- cgit v1.2.3-70-g09d2