working commit

This commit is contained in:
Олег Бородин
2026-05-07 11:24:50 +02:00
parent 9540ac05d4
commit a5d78c09a6
18 changed files with 2192 additions and 3226 deletions
+391
View File
@@ -0,0 +1,391 @@
extern "C" {
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <linux/if.h>
#include <linux/if_tun.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
}
#include <cstring>
#include <expected>
#include <string>
#include <vector>
#include <span>
#include <iostream>
#include <thread>
#include <chrono>
#include <interface.hpp>
std::expected<void, std::string> Interface::Create(const std::string name) {
if ((tunfd = open("/dev/net/tun", O_RDWR | O_CLOEXEC)) < 0) {
int errnocopy = errno;
std::string error = std::strerror(errnocopy);
return std::unexpected("Create interface error: " + error);
}
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
strncpy(ifr.ifr_name, name.c_str(), IFNAMSIZ - 1);
if (ioctl(tunfd, TUNSETIFF, (void*)(&ifr)) < 0) {
close(tunfd);
int errnocopy = errno;
std::string error = std::strerror(errnocopy);
return std::unexpected("Create interface error: " + error);
}
ifname = ifr.ifr_name;
#if 0
if (ioctl(tunfd, TUNSETPERSIST, 0) < 0) {
close(tunfd);
int errnocopy = errno;
std::string error = std::strerror(errnocopy);
return std::unexpected("Create interface error: " + error);
}
#endif
//struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
int sockfd = 0;
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
close(tunfd);
int errnocopy = errno;
std::string error = std::strerror(errnocopy);
return std::unexpected("Get MTU error: " + error);
}
strncpy(ifr.ifr_name, ifname.c_str(), IFNAMSIZ - 1);
if (ioctl(sockfd, SIOCGIFMTU, &ifr) < 0) {
close(tunfd);
close(sockfd);
int errnocopy = errno;
std::string error = std::strerror(errnocopy);
return std::unexpected("Get MTU error: " + error);
}
close(sockfd);
mtu = ifr.ifr_mtu;
return {};
}
std::expected<void, std::string> Interface::SetMTU(int ifmtu) {
int sockfd = 0;
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
int errnocopy = errno;
std::string error = std::strerror(errnocopy);
return std::unexpected("Set MTU error: " + error);
}
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, ifname.c_str(), IFNAMSIZ - 1);
ifr.ifr_mtu = ifmtu;
if (ioctl(sockfd, SIOCSIFMTU, &ifr) < 0) {
close(sockfd);
int errnocopy = errno;
std::string error = std::strerror(errnocopy);
return std::unexpected("Set MTU error: " + error);
}
close(sockfd);
return {};
}
std::expected<int, std::string> Interface::GetMTU(void) {
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
int sockfd = 0;
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
int errnocopy = errno;
std::string error = std::strerror(errnocopy);
return std::unexpected("Get MTU error: " + error);
}
strncpy(ifr.ifr_name, ifname.c_str(), IFNAMSIZ - 1);
if (ioctl(sockfd, SIOCGIFMTU, &ifr) < 0) {
close(sockfd);
int errnocopy = errno;
std::string error = std::strerror(errnocopy);
return std::unexpected("Get MTU error: " + error);
}
close(sockfd);
return ifr.ifr_mtu;
}
std::expected<std::string, std::string> Interface::GetIP4Address(void) {
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
int sockfd = 0;
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
int errnocopy = errno;
std::string error = std::strerror(errnocopy);
return std::unexpected("Get MTU error: " + error);
}
ifr.ifr_addr.sa_family = AF_INET;
strncpy(ifr.ifr_name, ifname.c_str(), IFNAMSIZ - 1);
if (ioctl(sockfd, SIOCGIFADDR, &ifr) < 0) {
close(sockfd);
int errnocopy = errno;
std::string error = std::strerror(errnocopy);
return std::unexpected("Get MTU error: " + error);
}
close(sockfd);
auto sinaddr = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr;
std::string addr = inet_ntoa(sinaddr);
return addr;
}
std::expected<void, std::string> Interface::SetIP4Address(std::string ipaddr) {
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
int sockfd = 0;
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
int errnocopy = errno;
std::string error = std::strerror(errnocopy);
return std::unexpected("Set address error: " + error);
}
ifr.ifr_addr.sa_family = AF_INET;
strncpy(ifr.ifr_name, ifname.c_str(), IFNAMSIZ - 1);
struct sockaddr_in* addr = (struct sockaddr_in *)&ifr.ifr_addr;
addr->sin_family = AF_INET;
if (inet_pton(AF_INET, ipaddr.c_str(), &addr->sin_addr) <= 0) {
close(sockfd);
int errnocopy = errno;
std::string error = std::strerror(errnocopy);
return std::unexpected("Set address error: " + error);
}
if (ioctl(sockfd, SIOCSIFADDR, &ifr) < 0) {
close(sockfd);
int errnocopy = errno;
std::string error = std::strerror(errnocopy);
return std::unexpected("Set address error: " + error);
}
close(sockfd);
return {};
}
std::expected<void, std::string> Interface::SetIP4Netmask(int prefix) {
if (prefix < 0 || prefix > 32) {
return std::unexpected("Invalid prefix");
}
uint32_t mask = (prefix == 0) ? 0 : (~0U << (32 - prefix));
//htonl(~((1U << (32 - prefix)) - 1));
//struct in_addr addr;
//addr.s_addr = mask;
struct in_addr maskaddr;
maskaddr.s_addr = htonl(mask);
char buffer[INET_ADDRSTRLEN];
if (inet_ntop(AF_INET, &maskaddr, buffer, sizeof(buffer)) == NULL) {
int errnocopy = errno;
std::string error = std::strerror(errnocopy);
return std::unexpected("Set1 netmask error: " + error);
}
auto netmask = std::string(buffer);
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
int sockfd = 0;
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
int errnocopy = errno;
std::string error = std::strerror(errnocopy);
return std::unexpected("Set2 netmask error: " + error);
}
ifr.ifr_addr.sa_family = AF_INET;
strncpy(ifr.ifr_name, ifname.c_str(), IFNAMSIZ - 1);
struct sockaddr_in* addr = (struct sockaddr_in *)&ifr.ifr_addr;
addr->sin_family = AF_INET;
if (inet_pton(AF_INET, netmask.c_str(), &addr->sin_addr) < 0) {
close(sockfd);
int errnocopy = errno;
std::string error = std::strerror(errnocopy);
return std::unexpected("Set3 netmask error: " + error);
}
if (ioctl(sockfd, SIOCSIFNETMASK, &ifr) < 0) {
close(sockfd);
int errnocopy = errno;
std::string error = std::strerror(errnocopy);
return std::unexpected("Set4 netmask error: " + error);
}
close(sockfd);
return {};
}
std::expected<void, std::string> Interface::Up(void) {
int sockfd = 0;
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
int errnocopy = errno;
std::string error = std::strerror(errnocopy);
return std::unexpected("Up interface error: " + error);
}
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, ifname.c_str(), IFNAMSIZ - 1);
if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0) {
close(sockfd);
int errnocopy = errno;
std::string error = std::strerror(errnocopy);
return std::unexpected("Up interface error: " + error);
}
ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
if (ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0) {
close(sockfd);
int errnocopy = errno;
std::string error = std::strerror(errnocopy);
return std::unexpected("Up interface error: " + error);
}
close(sockfd);
return {};
}
std::expected<void, std::string> Interface::Down(void) {
int sockfd = 0;
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
int errnocopy = errno;
std::string error = std::strerror(errnocopy);
return std::unexpected("Up interface error: " + error);
}
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, ifname.c_str(), IFNAMSIZ - 1);
if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0) {
close(sockfd);
int errnocopy = errno;
std::string error = std::strerror(errnocopy);
return std::unexpected("Up interface error: " + error);
}
ifr.ifr_flags &= ~IFF_UP;
if (ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0) {
close(sockfd);
int errnocopy = errno;
std::string error = std::strerror(errnocopy);
return std::unexpected("Up interface error: " + error);
}
close(sockfd);
return {};
}
std::expected<std::string, std::string> Interface::Read() {
char buffer[mtu];
int rsize = 0;
if ((rsize = read(tunfd, buffer, sizeof(buffer))) < 0) {
int errnocopy = errno;
std::string error = std::strerror(errnocopy);
return std::unexpected("Read interface error: " + error);
}
std::string rdata;
rdata.append(buffer, rsize);
return rdata;
}
std::string Interface::Name() {
return ifname;
}
int Interface::MTU() {
return mtu;
}
Interface::~Interface() {
close(tunfd);
}
std::expected<void, std::string> Interface::UpN(void) {
int netlinkfd = 0;
if ((netlinkfd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE)) < 0) {
int errnocopy = errno;
std::string error = std::strerror(errnocopy);
return std::unexpected("Create interface error: " + error);
}
struct {
struct nlmsghdr header;
struct ifinfomsg content;
} request;
memset(&request, 0, sizeof request);
request.header.nlmsg_len = NLMSG_LENGTH(sizeof request.content);
request.header.nlmsg_flags = NLM_F_REQUEST;
request.header.nlmsg_type = RTM_NEWLINK;
request.content.ifi_index = if_nametoindex(ifname.c_str());
request.content.ifi_flags = IFF_UP;
request.content.ifi_change = 1;
if (send(netlinkfd, &request, request.header.nlmsg_len, 0) < 0) {
close(netlinkfd);
int errnocopy = errno;
std::string error = std::strerror(errnocopy);
return std::unexpected("Up interface error: " + error);
}
close(netlinkfd);
return {};
}
std::expected<void, std::string> Interface::SetIP4AddrMask(const std::string address, const int prefix) {
int netlinkfd = 0;
if ((netlinkfd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE)) < 0) {
int errnocopy = errno;
std::string error = std::strerror(errnocopy);
return std::unexpected("Create interface error: " + error);
}
int rc = 0;
struct sockaddr_nl sockaddr;
memset(&sockaddr, 0, sizeof sockaddr);
sockaddr.nl_family = AF_NETLINK;
if ((rc = bind(netlinkfd, (struct sockaddr*) &sockaddr, sizeof sockaddr))< 0) {
close(netlinkfd);
int errnocopy = errno;
std::string error = std::strerror(errnocopy);
return std::unexpected("Set interface address error: " + error);
}
struct {
struct nlmsghdr header;
struct ifaddrmsg content;
char attributes_buf[64];
} request;
struct rtattr *request_attr;
size_t attributes_buf_avail = sizeof request.attributes_buf;
memset(&request, 0, sizeof request);
request.header.nlmsg_len = NLMSG_LENGTH(sizeof request.content);
request.header.nlmsg_flags = NLM_F_REQUEST | NLM_F_EXCL | NLM_F_CREATE;
request.header.nlmsg_type = RTM_NEWADDR;
request.content.ifa_index = if_nametoindex(ifname.c_str());
request.content.ifa_family = AF_INET;
request.content.ifa_prefixlen = prefix;
/* request.attributes[IFA_LOCAL] = address */
request_attr = IFA_RTA(&request.content);
request_attr->rta_type = IFA_LOCAL;
request_attr->rta_len = RTA_LENGTH(sizeof (struct in_addr));
request.header.nlmsg_len += request_attr->rta_len;
inet_pton(AF_INET, address.c_str(), RTA_DATA(request_attr));
/* request.attributes[IFA_ADDRESS] = address */
request_attr = RTA_NEXT(request_attr, attributes_buf_avail);
request_attr->rta_type = IFA_ADDRESS;
request_attr->rta_len = RTA_LENGTH(sizeof (struct in_addr));
request.header.nlmsg_len += request_attr->rta_len;
inet_pton(AF_INET, address.c_str(), RTA_DATA(request_attr));
if (send(netlinkfd, &request, request.header.nlmsg_len, 0) < 0) {
close(netlinkfd);
int errnocopy = errno;
std::string error = std::strerror(errnocopy);
return std::unexpected("Set interface address error: " + error);
}
close(netlinkfd);
return {};
}
+151
View File
@@ -0,0 +1,151 @@
extern "C" {
#include <arpa/inet.h>
#include <linux/rtnetlink.h>
#include <net/route.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <unistd.h>
}
#define BUFSIZE 8192
#include <expected>
#include <vector>
#include <format>
#include <cstring>
#include <iprouter.hpp>
std::expected<void, std::string> Router::AddRoute(std::string address, uint64_t prefix, std::string gateway) {
struct sockaddr_in sa;
if (inet_pton(AF_INET, address.data(), &(sa.sin_addr)) == 1) {
} else {
return std::unexpected(std::format("Incorrect address {}", address));
}
struct rtentry rt;
memset(&rt, 0, sizeof(rt));
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){
int errnoCopy = errno;
std::string error = std::strerror(errnoCopy);
return std::unexpected(std::format("Incorrect address {}, error: {}", address, error));
};
sin = (struct sockaddr_in*) &rt.rt_gateway;
sin->sin_family = AF_INET;
if (inet_pton(AF_INET, gateway.data(), &sin->sin_addr) != 1) {
int errnoCopy = errno;
std::string error = std::strerror(errnoCopy);
return std::unexpected(std::format("Incorrect address {}, error: {}", gateway, error));
};
char buffer[INET_ADDRSTRLEN];
uint32_t mask = (prefix == 0) ? 0 : htonl(~((1U << (32 - prefix)) - 1));
//uint32_t mask = (prefix == 0) ? 0 : (~0U << (32 - prefix));
struct in_addr addr;
addr.s_addr = mask;
const char *dottedmask;
if ((dottedmask = inet_ntop(AF_INET, &addr, buffer, INET_ADDRSTRLEN)) == NULL) {
int errnoCopy = errno;
std::string error = std::strerror(errnoCopy);
return std::unexpected(std::format("Incorrect prefix {}, error: {}", prefix, error));
};
sin = (struct sockaddr_in*) &rt.rt_genmask;
sin->sin_family = AF_INET;
if (inet_pton(AF_INET, dottedmask, &sin->sin_addr) != 1) {
int errnoCopy = errno;
std::string error = std::strerror(errnoCopy);
return std::unexpected(std::format("Incorrect address {}, error: {}", gateway, error));
};
rt.rt_flags = RTF_UP | RTF_GATEWAY;
//rt.rt_dev = std::string("eth0").data();
int sockfd = 0;
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
int errnoCopy = errno;
std::string error = std::strerror(errnoCopy);
return std::unexpected(std::format("Error: {}", error));
}
if (ioctl(sockfd, SIOCADDRT, &rt) < 0) {
close(sockfd);
int errnoCopy = errno;
std::string error = std::strerror(errnoCopy);
return std::unexpected(std::format("Cannot set route, error: {}", error));
}
close(sockfd);
return {};
}
std::expected<Routes, std::string> Router::List(void) {
std::vector<Route> routes;
struct {
struct nlmsghdr n;
struct rtmsg r;
} req;
std::string buffer(1024 * 10, 0);
struct nlmsghdr *nlh;
int sock, len;
if ((sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) < 0) {
return std::unexpected("Create netlink socket error");
}
memset(&req, 0, sizeof(req));
req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
req.n.nlmsg_type = RTM_GETROUTE;
req.r.rtm_family = AF_INET;
send(sock, &req, req.n.nlmsg_len, 0);
while ((len = recv(sock, buffer.data(), buffer.size(), 0)) > 0) {
nlh = (struct nlmsghdr *)buffer.data();
while (NLMSG_OK(nlh, len)) {
if (nlh->nlmsg_type == NLMSG_DONE) {
return {};
};
struct rtmsg *route = (struct rtmsg *)NLMSG_DATA(nlh);
struct rtattr *rta = (struct rtattr *)RTM_RTA(route);
int len = RTM_PAYLOAD(nlh);
char dest_ip[INET_ADDRSTRLEN] = "0.0.0.0";
char gw_ip[INET_ADDRSTRLEN] = "0.0.0.0";
for (; RTA_OK(rta, len); rta = RTA_NEXT(rta, len)) {
switch (rta->rta_type) {
case RTA_DST:
inet_ntop(AF_INET, RTA_DATA(rta), dest_ip, sizeof(dest_ip));
break;
case RTA_GATEWAY:
inet_ntop(AF_INET, RTA_DATA(rta), gw_ip, sizeof(gw_ip));
break;
case RTA_OIF:
int if_idx = *(int *)RTA_DATA(rta);
printf("Interface Index: %d ", if_idx);
break;
}
}
printf("Found route: dest %s / %d, gw %s\n", dest_ip, route->rtm_dst_len, gw_ip);
nlh = NLMSG_NEXT(nlh, len);
}
}
close(sock);
return routes;
}
-11
View File
@@ -1,11 +0,0 @@
#include <expected>
#include <vector>
#include <iprouter.hpp>
std::expected<Routes, std::string> Router::List() {
std::vector<Route> routes;
return routes;
}
-18
View File
@@ -1,18 +0,0 @@
#include <vector>
#include <expected>
#include <string>
class Route {
public:
std::string dest;
int mask;
};
using Routes = std::vector<Route>;
class Router {
std::expected<Routes, std::string> List();
};