#include "networkhandler.hpp" #include #include #include #include using namespace boost::asio::ip; using namespace boost::asio; NetworkHandler::NetworkHandler(const ListenSettings& listen_settings) : listen_settings(listen_settings) , io_service() , v4_acceptor(io_service, address_v4::from_string(listen_settings.ipv4_inaddr), listen_settings.port) , v6_acceptor(io_service, address_v6::from_string(listen_settings.ipv6_inaddr), listen_settings.port) , clients() { auto accept_handler = [this](tcp::socket&& socket) { clients.emplace_back(std::move(socket)); auto it = --clients.end(); clients.back().on_done( [this, it]() { clients.erase(it); } ); clients.back().receive(); }; /* * 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_acceptor.get_address().to_v4() == address_v4::any() && listen_settings.enable_ipv4; bool bind_v6_any = v6_acceptor.get_address().to_v6() == address_v6::any() && listen_settings.enable_ipv6; if(bind_v4_any && bind_v6_any) { v6_acceptor.bind_v6_and_v4_any(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) { v4_acceptor.setup_listen_socket(accept_handler); } if(listen_settings.enable_ipv6) { v6_acceptor.setup_listen_socket(accept_handler); } } } void NetworkHandler::run() { io_service.run(); }