diff --git a/clibs/ip.cc b/clibs/ip.cc index 9ec0432..826ac44 100644 --- a/clibs/ip.cc +++ b/clibs/ip.cc @@ -6,10 +6,11 @@ #include #include #include +#include const int inet_sock_fd = socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK, 0); -namespace ioctlNode { +namespace ipNode { int setflags(const char* iface, short flags) { struct ifreq ifr; @@ -19,6 +20,34 @@ namespace ioctlNode { return ioctl(inet_sock_fd, SIOCSIFFLAGS, &ifr); } + int getIfreq(struct ifreq *ifr ) { + return ioctl(inet_sock_fd, SIOCGIFFLAGS, ifr); + } + + void nodeGetFlags(const nodeparam& args) { + v8::Isolate* isolate = args.GetIsolate(); + + if (args.Length() < 1 || !args[0]->IsString()) { + isolate->ThrowException(v8::Exception::TypeError( + v8::String::NewFromUtf8(isolate, "Invalid arguments ip.getflags").ToLocalChecked())); + return; + } + + std::string iface = argToString(args[0]); + + struct ifreq ifr; + strncpy(ifr.ifr_name, iface.c_str(), IFNAMSIZ); + int err = getIfreq(&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(); @@ -36,8 +65,9 @@ namespace ioctlNode { void Initialize(v8::Local exports) { NODE_SET_METHOD(exports, "setFlags", nodeSetFlags); + NODE_SET_METHOD(exports, "getFlags", nodeGetFlags); } - NODE_MODULE(ioctl_api, Initialize) + NODE_MODULE(ip_api, Initialize) } diff --git a/clibs/socket.cc b/clibs/socket.cc deleted file mode 100644 index 39ae133..0000000 --- a/clibs/socket.cc +++ /dev/null @@ -1,32 +0,0 @@ -#include -#include -#include -#include -#include - -#define nodeparam v8::FunctionCallbackInfo - -namespace ioctlNode { - void nodeSocket(const nodeparam& args) { - v8::Isolate* isolate = args.GetIsolate(); - - if (args.Length() != 3 || !args[0]->IsUint32() || !args[1]->IsUint32() || !args[2]->IsUint32()) { - isolate->ThrowException(v8::Exception::TypeError( - v8::String::NewFromUtf8(isolate, "Argument 1, 2 & 3 must be numbers").ToLocalChecked())); - return; - } - - const int domain = args[0].As()->Value(); - const int type = args[1].As()->Value(); - const int protocol = args[2].As()->Value(); - - args.GetReturnValue().Set(socket(domain ,type, protocol)); - } - - - void Initialize(v8::Local exports) { - NODE_SET_METHOD(exports, "socket", nodeSocket); - } - - NODE_MODULE(ioctl_api, Initialize) -} diff --git a/rootfs overrides/core/.gitignore b/rootfs overrides/core/.gitignore index e47f72a..987abd0 100644 --- a/rootfs overrides/core/.gitignore +++ b/rootfs overrides/core/.gitignore @@ -1,6 +1,3 @@ -syscall.node -ioctl.node -mount.node -file.node node_modules -package-lock.json \ No newline at end of file +package-lock.json +*.node \ No newline at end of file diff --git a/rootfs overrides/core/ip.js b/rootfs overrides/core/ip.js index 78ad4fd..4f7b155 100644 --- a/rootfs overrides/core/ip.js +++ b/rootfs overrides/core/ip.js @@ -32,4 +32,12 @@ function setFlags(interface, flags) { return ip.setFlags(interface, flags) } -module.exports = { setFlags, flags } \ No newline at end of file +/** + * @param { string } interface + * @returns { number } + */ +function getFlags(interface) { + return ip.getFlags(interface); +} + +module.exports = { setFlags, getFlags, flags } \ No newline at end of file diff --git a/rootfs overrides/core/ip.node b/rootfs overrides/core/ip.node deleted file mode 100755 index 02da8a3..0000000 Binary files a/rootfs overrides/core/ip.node and /dev/null differ diff --git a/rootfs overrides/core/socket.node b/rootfs overrides/core/socket.node deleted file mode 100755 index ca92f15..0000000 Binary files a/rootfs overrides/core/socket.node and /dev/null differ diff --git a/rootfs overrides/usr/bin/ifflags.js b/rootfs overrides/usr/bin/ifflags.js new file mode 100644 index 0000000..5bbded3 --- /dev/null +++ b/rootfs overrides/usr/bin/ifflags.js @@ -0,0 +1,73 @@ +//@ts-check +const ip = require('../../core/ip') +const fs = require('fs') +const { exit } = require('process') + +function usage() { + console.log('invalid syntax') + console.log('ifup [interface name]') +} + +if ( process.argv.length !== 4 ){ + usage() + exit(1) +} + +if ( !fs.existsSync('/sys/class/net/' + process.argv[3]) ) { + console.log("Interface not found") + exit(1) +} + + +let flagsStr = '' +let flags = ip.getFlags(process.argv[3]) + +if(flags & ip.flags.IFF_ALLMULTI) { + flagsStr += 'IFF_ALLMULTI\n' +} +if(flags & ip.flags.IFF_AUTOMEDIA) { + flagsStr += 'IFF_AUTOMEDIA\n' +} +if(flags & ip.flags.IFF_BROADCAST) { + flagsStr += 'IFF_BROADCAST\n' +} +if(flags & ip.flags.IFF_DEBUG) { + flagsStr += 'IFF_DEBUG\n' +} +if(flags & ip.flags.IFF_DYNAMIC) { + flagsStr += 'IFF_DYNAMIC\n' +} +if(flags & ip.flags.IFF_LOOPBACK) { + flagsStr += 'IFF_LOOPBACK\n' +} +if(flags & ip.flags.IFF_MASTER) { + flagsStr += 'IFF_MASTER\n' +} +if(flags & ip.flags.IFF_MULTICAST) { + flagsStr += 'IFF_MULTICAST\n' +} +if(flags & ip.flags.IFF_NOARP) { + flagsStr += 'IFF_NOARP\n' +} +if(flags & ip.flags.IFF_NOTRAILERS) { + flagsStr += 'IFF_NOTRAILERS\n' +} +if(flags & ip.flags.IFF_POINTOPOINT) { + flagsStr += 'IFF_POINTOPOINT\n' +} +if(flags & ip.flags.IFF_PORTSEL) { + flagsStr += 'IFF_PORTSEL\n' +} +if(flags & ip.flags.IFF_PROMISC) { + flagsStr += 'IFF_PROMISC\n' +} +if(flags & ip.flags.IFF_RUNNING) { + flagsStr += 'IFF_RUNNING\n' +} +if(flags & ip.flags.IFF_SLAVE) { + flagsStr += 'IFF_SLAVE\n' +} +if(flags & ip.flags.IFF_UP) { + flagsStr += 'IFF_SLAVE\n' +} +console.log(flagsStr) \ No newline at end of file diff --git a/rootfs overrides/usr/bin/ifup.js b/rootfs overrides/usr/bin/ifup.js new file mode 100644 index 0000000..289d123 --- /dev/null +++ b/rootfs overrides/usr/bin/ifup.js @@ -0,0 +1,25 @@ +//@ts-check +const ip = require('../../core/ip') +const fs = require('fs') +const { exit } = require('process') + +function usage() { + console.log('invalid syntax') + console.log('ifup [interface name]') +} + +if ( process.argv.length !== 4 ){ + usage() + exit(1) +} + +if ( !fs.existsSync('/sys/class/net/' + process.argv[3]) ) { + console.log("Interface not found") + exit(1) +} + +let flags = ip.getFlags(process.argv[3]) + +flags |= ip.flags.IFF_UP + +ip.setFlags(process.argv[3], flags) \ No newline at end of file