working commit
This commit is contained in:
@@ -0,0 +1,126 @@
|
||||
|
||||
#include <expected>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <cstring>
|
||||
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
std::vector<std::string> split(std::string s, std::string delimiters) {
|
||||
std::vector<std::string> tokens;
|
||||
size_t last = 0, next = 0;
|
||||
while ((next = s.find_first_of(delimiters, last)) != std::string::npos) {
|
||||
if (next != last) tokens.push_back(s.substr(last, next - last));
|
||||
last = next + 1;
|
||||
}
|
||||
if (last < s.length()) tokens.push_back(s.substr(last));
|
||||
return tokens;
|
||||
}
|
||||
|
||||
std::string trim(std::string& source) {
|
||||
auto line = source;
|
||||
std::string whitespaces(" \t\n\r\f\v");
|
||||
line.erase(0, line.find_first_not_of(whitespaces));
|
||||
line.erase(line.find_last_not_of(whitespaces) + 1);
|
||||
return line;
|
||||
}
|
||||
|
||||
std::expected<int, std::string> str2int (std::string source) {
|
||||
std::size_t pos{};
|
||||
int res;
|
||||
try {
|
||||
res = std::stoi(source, &pos);
|
||||
}
|
||||
catch (std::invalid_argument const& ex) {
|
||||
return std::unexpected(std::format("invalid argument:{}", ex.what()));
|
||||
}
|
||||
catch (std::out_of_range const& ex) {
|
||||
return std::unexpected(std::format("out of range", ex.what()));
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
class Registry {
|
||||
private:
|
||||
std::map<std::string, std::string> kvmap;
|
||||
std::vector<std::string> localnets;
|
||||
std::string tunnelnet;
|
||||
int listenport;
|
||||
public:
|
||||
std::expected<void, std::string> Read(std::string filename);
|
||||
int Listenport(void);
|
||||
std::string Tunnelnet(void);
|
||||
std::vector<std::string> Localnets(void);
|
||||
|
||||
};
|
||||
|
||||
int Registry::Listenport(void){
|
||||
return listenport;
|
||||
}
|
||||
|
||||
std::vector<std::string> Registry::Localnets(void){
|
||||
return localnets;
|
||||
}
|
||||
|
||||
std::string Registry::Tunnelnet(void){
|
||||
return tunnelnet;
|
||||
}
|
||||
|
||||
std::expected<void, std::string> Registry::Read(std::string filename) {
|
||||
std::ifstream file;
|
||||
file.open(filename);
|
||||
if (file.fail()) {
|
||||
auto state = file.rdstate();
|
||||
if (state & std::ios_base::badbit) {
|
||||
return std::unexpected("Read/writing error on i/o operation");
|
||||
} else if (state & std::ios_base::failbit) {
|
||||
return std::unexpected("Logical error on i/o operation");
|
||||
}
|
||||
}
|
||||
|
||||
std::string line;
|
||||
while (std::getline(file, line)) {
|
||||
auto tokens = split(line, "#;");
|
||||
auto keyval = trim(tokens[0]);
|
||||
if (keyval.size() == 0) continue;
|
||||
tokens = split(keyval, "=");
|
||||
if (tokens.size() < 2) continue;
|
||||
auto key = trim(tokens[0]);
|
||||
auto val = trim(tokens[1]);
|
||||
kvmap[key] = val;
|
||||
if (key == "localnet") {
|
||||
localnets.push_back(val);
|
||||
} else if (key == "tunnelnet") {
|
||||
tunnelnet = val;
|
||||
} else if (key == "listenport") {
|
||||
auto convRes = str2int(val);
|
||||
if (!convRes) {
|
||||
auto msg = std::format("listenport: {}", convRes.error());
|
||||
return std::unexpected(msg);
|
||||
}
|
||||
listenport = convRes.value();
|
||||
}
|
||||
}
|
||||
file.close();
|
||||
//for (auto const& [key, val] : kvmap) {
|
||||
// std::cout << key << ":" << val << std::endl;
|
||||
//}
|
||||
return {};
|
||||
}
|
||||
|
||||
int main() {
|
||||
Registry config;
|
||||
auto openRes = config.Read("example.conf");
|
||||
if (!openRes) {
|
||||
std::cerr << openRes.error() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
# foo
|
||||
# foobare
|
||||
localnet == 10.1.1.0/24 #qwert
|
||||
localnet == 10.1.2.0/24 #qwert
|
||||
ddd =
|
||||
+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