working commit

This commit is contained in:
Олег Бородин
2026-05-20 17:15:26 +02:00
parent 0d37d45543
commit 3f261b0922
5 changed files with 40 additions and 14 deletions
+20 -5
View File
@@ -7,11 +7,26 @@ extern "C" {
#include <format>
#include <expected>
#include <string>
#include <cmath>
#include <networkaux.hpp>
#include <stringaux.hpp>
std::expected<uint32_t, std::string> 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<uint, std::string> 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<std::string, std::string> network(const std::string network) {
}
std::expected<std::string, std::string> nethost6(std::string network, int prefix, uint32_t num) {
std::expected<std::string, std::string> 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<std::string, std::string> nethost6(std::string network, int prefix
}
std::expected<std::string, std::string> nethost4(std::string network, int prefix, uint32_t num) {
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) {
return std::unexpected(std::format("Invalid network address {}", network));
@@ -70,7 +85,7 @@ std::expected<std::string, std::string> nethost4(std::string network, int prefix
return std::string(inet_ntoa(ip_addr));
}
std::expected<std::string, std::string> nethost(std::string network, int 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);