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 <networkaux.hpp>
#include <stringaux.hpp> #include <stringaux.hpp>
uint netcapa6(const uint prefix) { uint64_t netcapa6(const uint prefix);
int hostBits = 128 - prefix; uint64_t netcapa4(const uint prefix);
int totalIPs = std::pow(2, hostBits);
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; return totalIPs;
} }
uint64_t netcapa4(const uint prefix) {
uint netcapa4(const uint prefix) { uint hostBits = 32 - prefix;
int hostBits = 32 - prefix; uint64_t totalIPs = std::pow(2, hostBits);
int totalIPs = std::pow(2, hostBits);
return totalIPs; return totalIPs;
} }
@@ -46,6 +58,18 @@ std::expected<std::string, std::string> network(const std::string network) {
return hostprefix[0]; 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) { std::expected<std::string, std::string> nethost6(std::string network, uint prefix, uint32_t num) {
struct in6_addr addr; struct in6_addr addr;
@@ -71,7 +95,6 @@ std::expected<std::string, std::string> nethost6(std::string network, uint prefi
return std::string(buffer); return std::string(buffer);
} }
std::expected<std::string, std::string> nethost4(std::string network, uint prefix, uint num) { std::expected<std::string, std::string> nethost4(std::string network, uint prefix, uint num) {
struct in_addr inaddr; struct in_addr inaddr;
if (inet_pton(AF_INET, network.data(), &inaddr) != 1) { 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)); 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));
}