working commit
This commit is contained in:
+20
-5
@@ -7,11 +7,26 @@ extern "C" {
|
|||||||
#include <format>
|
#include <format>
|
||||||
#include <expected>
|
#include <expected>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
#include <networkaux.hpp>
|
#include <networkaux.hpp>
|
||||||
#include <stringaux.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, "/");
|
auto hostprefix = split(network, "/");
|
||||||
if (hostprefix.size() < 2) {
|
if (hostprefix.size() < 2) {
|
||||||
return std::unexpected("Incorrect network definition");
|
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;
|
struct in6_addr addr;
|
||||||
unsigned char mask[16] = {0};
|
unsigned char mask[16] = {0};
|
||||||
if (inet_pton(AF_INET6, network.data(), &addr) != 1) {
|
if (inet_pton(AF_INET6, network.data(), &addr) != 1) {
|
||||||
return std::unexpected(std::format("Invalid network address {}", network));
|
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)));
|
mask[i / 8] |= (1 << (7 - (i % 8)));
|
||||||
}
|
}
|
||||||
for (int i = 0; i < 16; i++) {
|
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;
|
struct in_addr inaddr;
|
||||||
if (inet_pton(AF_INET, network.data(), &inaddr) != 1) {
|
if (inet_pton(AF_INET, network.data(), &inaddr) != 1) {
|
||||||
return std::unexpected(std::format("Invalid network address {}", network));
|
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));
|
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;
|
struct sockaddr_in sa;
|
||||||
if (inet_pton(AF_INET, network.data(), &(sa.sin_addr)) == 1) {
|
if (inet_pton(AF_INET, network.data(), &(sa.sin_addr)) == 1) {
|
||||||
return nethost4(network, prefix, num);
|
return nethost4(network, prefix, num);
|
||||||
|
|||||||
+2
-2
@@ -4,8 +4,8 @@
|
|||||||
#include <expected>
|
#include <expected>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
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);
|
||||||
std::expected<uint32_t, std::string> netprefix(const std::string network);
|
std::expected<uint, std::string> netprefix(const std::string network);
|
||||||
std::expected<std::string, std::string> network(const std::string network);
|
std::expected<std::string, std::string> network(const std::string network);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+14
-1
@@ -13,7 +13,6 @@ extern "C" {
|
|||||||
#include <uxlogger.hpp>
|
#include <uxlogger.hpp>
|
||||||
#include <networkaux.hpp>
|
#include <networkaux.hpp>
|
||||||
|
|
||||||
using namespace std::chrono_literals;
|
|
||||||
|
|
||||||
TunService::TunService(int svcport, std::string itunnelnet, std::vector<std::string> ilocalnets) {
|
TunService::TunService(int svcport, std::string itunnelnet, std::vector<std::string> ilocalnets) {
|
||||||
listenport = svcport;
|
listenport = svcport;
|
||||||
@@ -21,6 +20,20 @@ TunService::TunService(int svcport, std::string itunnelnet, std::vector<std::str
|
|||||||
localnets = ilocalnets;
|
localnets = ilocalnets;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::expected<void, std::string> TunService::Init(void) {
|
||||||
|
auto netprefixRes = netprefix(tunnelnet);
|
||||||
|
if (!netprefixRes) {
|
||||||
|
return std::unexpected(netprefixRes.error());
|
||||||
|
}
|
||||||
|
auto networkRes = network(tunnelnet);
|
||||||
|
if (!netprefixRes) {
|
||||||
|
return std::unexpected(networkRes.error());
|
||||||
|
};
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
TunService::~TunService() {
|
TunService::~TunService() {
|
||||||
close(listenport);
|
close(listenport);
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-5
@@ -8,10 +8,7 @@
|
|||||||
|
|
||||||
#include <sockhand.hpp>
|
#include <sockhand.hpp>
|
||||||
|
|
||||||
class ClientSlot {
|
class NetworkSlot {
|
||||||
private:
|
|
||||||
bool free;
|
|
||||||
int num;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@@ -19,11 +16,12 @@ class TunService {
|
|||||||
private:
|
private:
|
||||||
std::string tunnelnet;
|
std::string tunnelnet;
|
||||||
std::vector<std::string> localnets;
|
std::vector<std::string> localnets;
|
||||||
|
std::vector<NetworkSlot> netslots;
|
||||||
int listenport;
|
int listenport;
|
||||||
int sock;
|
int sock;
|
||||||
std::vector<ClientSlot> clientSlots;
|
|
||||||
public:
|
public:
|
||||||
explicit TunService(int port, std::string tunnelnet, std::vector<std::string> localnets);
|
explicit TunService(int port, std::string tunnelnet, std::vector<std::string> localnets);
|
||||||
|
std::expected<void, std::string> Init(void);
|
||||||
std::expected<void, std::string> Bind(void);
|
std::expected<void, std::string> Bind(void);
|
||||||
std::expected<void, std::string> Listen(void);
|
std::expected<void, std::string> Listen(void);
|
||||||
void Handle(int sock);
|
void Handle(int sock);
|
||||||
|
|||||||
Reference in New Issue
Block a user