From 879481feab5ccaf12ca48cd46f58fd03839571b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9E=D0=BB=D0=B5=D0=B3=20=D0=91=D0=BE=D1=80=D0=BE=D0=B4?= =?UTF-8?q?=D0=B8=D0=BD?= Date: Thu, 7 May 2026 17:53:17 +0200 Subject: [PATCH] working commit --- works/iprange.c | 39 ++++++++++++++++++++++ works/iprange.cpp | 70 +++++++++++++++++++++++++++++++++++++++ works/iprouter_w_list.cpp | 2 +- 3 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 works/iprange.c create mode 100644 works/iprange.cpp diff --git a/works/iprange.c b/works/iprange.c new file mode 100644 index 0000000..c80005e --- /dev/null +++ b/works/iprange.c @@ -0,0 +1,39 @@ +#include +#include + +void network4(const char* network, int prefix) { + struct in_addr inaddr; + // Convert from human view to network bytes + if (inet_pton(AF_INET, network, &inaddr) == 0) { + printf("Invalid IP address\n"); + return; + } + // Convert from network bytes to local + uint32_t ip = ntohl(inaddr.s_addr); + + // Calculate mask and net + uint32_t mask = (prefix == 0) ? 0 : (~0U << (32 - prefix)); + uint32_t net = ip & mask; + uint32_t fip = net + 1; + + // Convert net address bytes to network order + struct in_addr net_addr; + net_addr.s_addr = htonl(net); + // Convert mask bytes to network order + struct in_addr mask_addr; + mask_addr.s_addr = htonl(mask); + // Convert mask bytes to network order + struct in_addr fip_addr; + fip_addr.s_addr = htonl(fip); + + printf("IP: %s/%d\n", network, prefix); + printf("Netmask: %s\n", inet_ntoa(mask_addr)); + printf("Network: %s\n", inet_ntoa(net_addr)); + printf("First: %s\n", inet_ntoa(fip_addr)); +} + +int main() { + network4("192.168.1.154", 26); + return 0; +} + diff --git a/works/iprange.cpp b/works/iprange.cpp new file mode 100644 index 0000000..12a5601 --- /dev/null +++ b/works/iprange.cpp @@ -0,0 +1,70 @@ + +extern "C" { +#include +#include +} + +#include +#include +#include +#include +#include + +std::expected network6(std::string network, int prefix) { + 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++) { + mask[i / 8] |= (1 << (7 - (i % 8))); + } + for (int i = 0; i < 16; i++) { + addr.s6_addr[i] &= mask[i]; + } + + uint64_t *host_part = (uint64_t *)&addr.s6_addr[8]; + uint64_t hostnum = be64toh(*host_part); + hostnum += 2; + *host_part = htobe64(hostnum); + + char buffer[INET6_ADDRSTRLEN] = {'\0'}; + inet_ntop(AF_INET6, &addr, buffer, INET6_ADDRSTRLEN); + + return std::string(buffer); +} + + +std::expected network4(std::string network, int prefix) { + struct in_addr inaddr; + if (inet_pton(AF_INET, network.data(), &inaddr) != 1) { + return std::unexpected(std::format("Invalid network address {}", network)); + } + uint32_t ip = ntohl(inaddr.s_addr); + uint32_t mask = (prefix == 0) ? 0 : (~0U << (32 - prefix)); + uint32_t fip = (ip & mask) + 1; + struct in_addr ip_addr; + ip_addr.s_addr = htonl(ip); + + return std::string(inet_ntoa(ip_addr)); +} + +int main() { + auto net4Res = network4("192.168.1.154", 26); + if (!net4Res) { + std::cerr << net4Res.error() << std::endl; + return 1; + } + std::cout << net4Res.value() << std::endl; + + auto net6Res = network6("2001:db8:abcd:1234::0", 64); + if (!net6Res) { + std::cerr << net6Res.error() << std::endl; + return 1; + } + std::cout << net6Res.value() << std::endl; + + + return 0; +} + diff --git a/works/iprouter_w_list.cpp b/works/iprouter_w_list.cpp index 7c510de..1535d25 100644 --- a/works/iprouter_w_list.cpp +++ b/works/iprouter_w_list.cpp @@ -34,7 +34,7 @@ std::expected Router::AddRoute(std::string address, uint64_t struct sockaddr_in *sin; sin = (struct sockaddr_in*)&rt.rt_dst; sin->sin_family = AF_INET; - if (inet_pton(AF_INET, address.data(), &sin->sin_addr) != 1){ + if (inet_pton(AF_INET, address.data(), &sin->sin_addr) != 1) { int errnoCopy = errno; std::string error = std::strerror(errnoCopy); return std::unexpected(std::format("Incorrect address {}, error: {}", address, error));