working commit
This commit is contained in:
+17
-8
@@ -10,7 +10,7 @@ extern "C" {
|
||||
#include <ostream>
|
||||
#include <iostream>
|
||||
|
||||
std::expected<std::string, std::string> network6(std::string network, int prefix) {
|
||||
std::expected<std::string, std::string> nethost6(std::string network, int prefix, uint32_t num) {
|
||||
struct in6_addr addr;
|
||||
unsigned char mask[16] = {0};
|
||||
if (inet_pton(AF_INET6, network.data(), &addr) != 1) {
|
||||
@@ -25,7 +25,7 @@ std::expected<std::string, std::string> network6(std::string network, int prefix
|
||||
|
||||
uint64_t *host_part = (uint64_t *)&addr.s6_addr[8];
|
||||
uint64_t hostnum = be64toh(*host_part);
|
||||
hostnum += 2;
|
||||
hostnum += num;
|
||||
*host_part = htobe64(hostnum);
|
||||
|
||||
char buffer[INET6_ADDRSTRLEN] = {'\0'};
|
||||
@@ -35,29 +35,38 @@ std::expected<std::string, std::string> network6(std::string network, int prefix
|
||||
}
|
||||
|
||||
|
||||
std::expected<std::string, std::string> network4(std::string network, int prefix) {
|
||||
std::expected<std::string, std::string> nethost4(std::string network, int prefix, uint32_t num) {
|
||||
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;
|
||||
uint32_t fip = (ip & mask) + num;
|
||||
struct in_addr ip_addr;
|
||||
ip_addr.s_addr = htonl(ip);
|
||||
|
||||
ip_addr.s_addr = htonl(fip);
|
||||
return std::string(inet_ntoa(ip_addr));
|
||||
}
|
||||
|
||||
std::expected<std::string, std::string> nethost(std::string network, int prefix, uint32_t 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));
|
||||
}
|
||||
|
||||
int main() {
|
||||
auto net4Res = network4("192.168.1.154", 26);
|
||||
auto net4Res = nethost4("192.168.1.151", 24, 1);
|
||||
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);
|
||||
auto net6Res = nethost6("2001:db8:abcd:1234::0", 64, 1);
|
||||
if (!net6Res) {
|
||||
std::cerr << net6Res.error() << std::endl;
|
||||
return 1;
|
||||
|
||||
Reference in New Issue
Block a user