un commited old code

This commit is contained in:
Marc
2022-09-08 11:44:29 +02:00
parent a6f0b3367c
commit 39459df357
6 changed files with 42 additions and 93 deletions
+39
View File
@@ -2,6 +2,8 @@
#include <iostream>
#include <sys/ioctl.h>
#include <net/if.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/route.h>
#include <linux/sockios.h>
#include <unistd.h>
@@ -48,6 +50,42 @@ namespace ipNode {
args.GetReturnValue().Set(ifr.ifr_flags);
}
void nodeSetIpv4(const nodeparam& args) {
v8::Isolate* isolate = args.GetIsolate();
if (args.Length() < 2 || !args[0]->IsString() || !args[1]->IsString()) {
isolate->ThrowException(v8::Exception::TypeError(
v8::String::NewFromUtf8(isolate, "Invalid arguments ip.setIpv4").ToLocalChecked()));
return;
}
const std::string iface = argToString(args[0]);;
const std::string ip = argToString(args[1]);
struct ifreq ifr;
struct sockaddr_in sai;
strncpy(ifr.ifr_name, iface.c_str(), IFNAMSIZ);
memset(&sai, 0, sizeof(struct sockaddr));
sai.sin_family = AF_INET;
sai.sin_port = 0;
sai.sin_addr.s_addr = inet_addr(ip.c_str());
memcpy( &ifr.ifr_ifru, &sai, sizeof(struct sockaddr));
int err = ioctl(inet_sock_fd, SIOCSIFADDR, &ifr);
if(err != 0) {
char errstr[36];
sprintf(errstr, "Error while getting flags %d", errno);
isolate->ThrowException(v8::Exception::TypeError(
v8::String::NewFromUtf8(isolate, errstr).ToLocalChecked()));
}
args.GetReturnValue().Set(ifr.ifr_flags);
}
void nodeSetFlags(const nodeparam& args) {
v8::Isolate* isolate = args.GetIsolate();
@@ -66,6 +104,7 @@ namespace ipNode {
void Initialize(v8::Local<v8::Object> exports) {
NODE_SET_METHOD(exports, "setFlags", nodeSetFlags);
NODE_SET_METHOD(exports, "getFlags", nodeGetFlags);
NODE_SET_METHOD(exports, "setIpv4", nodeSetIpv4);
}
NODE_MODULE(ip_api, Initialize)