diff --git a/networkaux.cpp b/networkaux.cpp index 545d20d..0065fdc 100644 --- a/networkaux.cpp +++ b/networkaux.cpp @@ -12,16 +12,28 @@ extern "C" { #include #include -uint netcapa6(const uint prefix) { - int hostBits = 128 - prefix; - int totalIPs = std::pow(2, hostBits); +uint64_t netcapa6(const uint prefix); +uint64_t netcapa4(const uint prefix); + +std::expected netcapa(std::string network, uint prefix) { + struct sockaddr_in sa; + if (inet_pton(AF_INET, network.data(), &(sa.sin_addr)) == 1) { + return netcapa4(prefix); + } else if (inet_pton(AF_INET6, network.data(), &(sa.sin_addr)) == 1) { + return netcapa6(prefix); + } + return std::unexpected(std::format("Unknown network address {}", network)); +} + +uint64_t netcapa6(const uint prefix) { + uint hostBits = 128 - prefix; + uint64_t totalIPs = std::pow(2, hostBits); return totalIPs; } - -uint netcapa4(const uint prefix) { - int hostBits = 32 - prefix; - int totalIPs = std::pow(2, hostBits); +uint64_t netcapa4(const uint prefix) { + uint hostBits = 32 - prefix; + uint64_t totalIPs = std::pow(2, hostBits); return totalIPs; } @@ -46,6 +58,18 @@ std::expected network(const std::string network) { return hostprefix[0]; } +std::expected nethost6(std::string network, uint prefix, uint32_t num); +std::expected nethost4(std::string network, uint 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); + } else if (inet_pton(AF_INET6, network.data(), &(sa.sin_addr)) == 1) { + return nethost6(network, prefix, num); + } + return std::unexpected(std::format("Unknown network address {}", network)); +} std::expected nethost6(std::string network, uint prefix, uint32_t num) { struct in6_addr addr; @@ -71,7 +95,6 @@ std::expected nethost6(std::string network, uint prefi return std::string(buffer); } - std::expected nethost4(std::string network, uint prefix, uint num) { struct in_addr inaddr; if (inet_pton(AF_INET, network.data(), &inaddr) != 1) { @@ -85,12 +108,3 @@ std::expected nethost4(std::string network, uint prefi return std::string(inet_ntoa(ip_addr)); } -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); - } else if (inet_pton(AF_INET6, network.data(), &(sa.sin_addr)) == 1) { - return nethost6(network, prefix, num); - } - return std::unexpected(std::format("Unknown network address {}", network)); -}