working commit
This commit is contained in:
+43
-43
@@ -284,26 +284,26 @@ Interface::~Interface() {
|
||||
|
||||
std::expected<void, std::string> Interface::UpN(void) {
|
||||
int netlinkfd = 0;
|
||||
if ((netlinkfd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE)) < 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;
|
||||
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;
|
||||
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) {
|
||||
if (send(netlinkfd, &request, request.header.nlmsg_len, 0) < 0) {
|
||||
close(netlinkfd);
|
||||
int errnocopy = errno;
|
||||
std::string error = std::strerror(errnocopy);
|
||||
@@ -316,7 +316,7 @@ std::expected<void, std::string> Interface::UpN(void) {
|
||||
|
||||
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) {
|
||||
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);
|
||||
@@ -324,46 +324,46 @@ std::expected<void, std::string> Interface::SetIP4AddrMask(const std::string add
|
||||
|
||||
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) {
|
||||
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 nlmsghdr header;
|
||||
struct ifaddrmsg content;
|
||||
char attributes_buf[64];
|
||||
} request;
|
||||
|
||||
struct rtattr *request_attr;
|
||||
size_t attributes_buf_avail = sizeof request.attributes_buf;
|
||||
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;
|
||||
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_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));
|
||||
/* 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) {
|
||||
if (send(netlinkfd, &request, request.header.nlmsg_len, 0) < 0) {
|
||||
close(netlinkfd);
|
||||
int errnocopy = errno;
|
||||
std::string error = std::strerror(errnocopy);
|
||||
|
||||
Reference in New Issue
Block a user