diff --git a/helmetsrv.cpp b/helmetsrv.cpp index 2507c37..857ff37 100644 --- a/helmetsrv.cpp +++ b/helmetsrv.cpp @@ -10,7 +10,7 @@ std::expected Run() { ServConfig config; - std::string confdir(SRV_CONFDIR); + std::string confdir(SRV_CONFDIR); auto readRes = config.Read(confdir + "/" + "helmetsrv.conf"); if (!readRes) { return std::unexpected("Read config error: " + readRes.error()); diff --git a/networkaux.cpp b/networkaux.cpp index 28d04b7..545d20d 100644 --- a/networkaux.cpp +++ b/networkaux.cpp @@ -7,11 +7,26 @@ extern "C" { #include #include #include +#include #include #include -std::expected netprefix(const std::string network) { +uint netcapa6(const uint prefix) { + int hostBits = 128 - prefix; + int totalIPs = std::pow(2, hostBits); + return totalIPs; +} + + +uint netcapa4(const uint prefix) { + int hostBits = 32 - prefix; + int totalIPs = std::pow(2, hostBits); + return totalIPs; +} + + +std::expected netprefix(const std::string network) { auto hostprefix = split(network, "/"); if (hostprefix.size() < 2) { return std::unexpected("Incorrect network definition"); @@ -32,13 +47,13 @@ std::expected network(const std::string network) { } -std::expected nethost6(std::string network, int prefix, uint32_t num) { +std::expected nethost6(std::string network, uint prefix, uint32_t num) { struct in6_addr addr; unsigned char mask[16] = {0}; if (inet_pton(AF_INET6, network.data(), &addr) != 1) { return std::unexpected(std::format("Invalid network address {}", network)); } - for (int i = 0; i < prefix; i++) { + for (uint i = 0; i < prefix; i++) { mask[i / 8] |= (1 << (7 - (i % 8))); } for (int i = 0; i < 16; i++) { @@ -57,7 +72,7 @@ std::expected nethost6(std::string network, int prefix } -std::expected nethost4(std::string network, int prefix, uint32_t num) { +std::expected nethost4(std::string network, uint prefix, uint num) { struct in_addr inaddr; if (inet_pton(AF_INET, network.data(), &inaddr) != 1) { return std::unexpected(std::format("Invalid network address {}", network)); @@ -70,7 +85,7 @@ std::expected nethost4(std::string network, int prefix return std::string(inet_ntoa(ip_addr)); } -std::expected nethost(std::string network, int prefix, uint32_t num) { +std::expected nethost(std::string network, uint prefix, uint num) { struct sockaddr_in sa; if (inet_pton(AF_INET, network.data(), &(sa.sin_addr)) == 1) { return nethost4(network, prefix, num); diff --git a/networkaux.hpp b/networkaux.hpp index 4de6f15..3d9f2ca 100644 --- a/networkaux.hpp +++ b/networkaux.hpp @@ -4,8 +4,8 @@ #include #include -std::expected nethost(std::string network, int prefix, uint32_t num); -std::expected netprefix(const std::string network); +std::expected nethost(std::string network, uint prefix, uint num); +std::expected netprefix(const std::string network); std::expected network(const std::string network); #endif diff --git a/tservice.cpp b/tservice.cpp index 576e6d7..5f1e629 100644 --- a/tservice.cpp +++ b/tservice.cpp @@ -13,7 +13,6 @@ extern "C" { #include #include -using namespace std::chrono_literals; TunService::TunService(int svcport, std::string itunnelnet, std::vector ilocalnets) { listenport = svcport; @@ -21,6 +20,20 @@ TunService::TunService(int svcport, std::string itunnelnet, std::vector TunService::Init(void) { + auto netprefixRes = netprefix(tunnelnet); + if (!netprefixRes) { + return std::unexpected(netprefixRes.error()); + } + auto networkRes = network(tunnelnet); + if (!netprefixRes) { + return std::unexpected(networkRes.error()); + }; + + return {}; +} + + TunService::~TunService() { close(listenport); } diff --git a/tservice.hpp b/tservice.hpp index e3d4a9f..7b6aff3 100644 --- a/tservice.hpp +++ b/tservice.hpp @@ -8,10 +8,7 @@ #include -class ClientSlot { -private: - bool free; - int num; +class NetworkSlot { }; @@ -19,11 +16,12 @@ class TunService { private: std::string tunnelnet; std::vector localnets; + std::vector netslots; int listenport; int sock; - std::vector clientSlots; public: explicit TunService(int port, std::string tunnelnet, std::vector localnets); + std::expected Init(void); std::expected Bind(void); std::expected Listen(void); void Handle(int sock);