working commit

This commit is contained in:
Олег Бородин
2026-05-20 17:30:41 +02:00
parent 3f261b0922
commit ee4cee5809
+31 -17
View File
@@ -12,16 +12,28 @@ extern "C" {
#include <networkaux.hpp>
#include <stringaux.hpp>
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<uint, std::string> 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<std::string, std::string> network(const std::string network) {
return hostprefix[0];
}
std::expected<std::string, std::string> nethost6(std::string network, uint prefix, uint32_t num);
std::expected<std::string, std::string> nethost4(std::string network, uint prefix, uint32_t num);
std::expected<std::string, std::string> 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<std::string, std::string> nethost6(std::string network, uint prefix, uint32_t num) {
struct in6_addr addr;
@@ -71,7 +95,6 @@ std::expected<std::string, std::string> nethost6(std::string network, uint prefi
return std::string(buffer);
}
std::expected<std::string, std::string> 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<std::string, std::string> nethost4(std::string network, uint prefi
return std::string(inet_ntoa(ip_addr));
}
std::expected<std::string, std::string> 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));
}