diff options
| author | Dennis Brentjes <d.brentjes@gmail.com> | 2016-08-26 11:21:41 +0200 |
|---|---|---|
| committer | Dennis Brentjes <d.brentjes@gmail.com> | 2016-08-26 11:21:41 +0200 |
| commit | 00ab2cf6add2976b3a4e8f8cc488777ad5c27808 (patch) | |
| tree | b9399bf912d949c289ff31edae8af2303dff7d26 /network-handler | |
| download | cmix-00ab2cf6add2976b3a4e8f8cc488777ad5c27808.tar.gz cmix-00ab2cf6add2976b3a4e8f8cc488777ad5c27808.tar.bz2 cmix-00ab2cf6add2976b3a4e8f8cc488777ad5c27808.zip | |
Initial commit, just some ideas and testing.
Diffstat (limited to 'network-handler')
| -rw-r--r-- | network-handler/CMakeLists.txt | 17 | ||||
| -rw-r--r-- | network-handler/main.cpp | 42 | ||||
| -rw-r--r-- | network-handler/networkhandler.cpp | 89 | ||||
| -rw-r--r-- | network-handler/networkhandler.hpp | 34 |
4 files changed, 182 insertions, 0 deletions
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 <boost/program_options.hpp> + +#include <string> +#include <iostream> +#include <cstdint> + +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<uint16_t>()->default_value(9200), "Set listening port") + ("enable_v4", po::value<bool>()->default_value(true), "Enable/disable ipv4 accept support") + ("interface4,4", po::value<std::string>()->default_value("0.0.0.0"), "Set the ipv4 address to listen on.") + ("enable_v6", po::value<bool>()->default_value(true), "Enable/disable ipv6 accept support") + ("interface6,6", po::value<std::string>()->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<bool>(); + std::string if4 = vm["interface4"].as<std::string>(); + bool en6 = vm["enable_v6"].as<bool>(); + std::string if6 = vm["interface6"].as<std::string>(); + uint16_t port = vm["port"].as<uint16_t>(); + + 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 <boost/bind.hpp> + +#include <memory> +#include <iostream> + +using namespace boost::asio::ip; +using namespace boost::asio; + +void accept_loop(tcp::acceptor& acceptor, std::function<void(tcp::socket&&)> f); + +void accept_connection(tcp::acceptor& acceptor, std::shared_ptr<tcp::socket> socket, boost::system::error_code ec, std::function<void(boost::asio::ip::tcp::socket&&)> 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<void(tcp::socket&&)> f) +{ + std::shared_ptr<tcp::socket> new_socket = std::make_shared<tcp::socket>(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<void(tcp::socket&& socket)> 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 <boost/asio/io_service.hpp> +#include <boost/asio.hpp> + +#include <string> +#include <cstdint> +#include <list> + +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<boost::asio::ip::tcp::socket> accepted_connections; + +public: + NetworkHandler(ListenSettings const& listen_settings); + + void run(); +}; |
