initial commit
This commit is contained in:
+374
@@ -0,0 +1,374 @@
|
||||
|
||||
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 <tunnel.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));
|
||||
|
||||
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::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 {};
|
||||
}
|
||||
Reference in New Issue
Block a user