diff --git a/clibs/chibrax/ip.cc b/clibs/chibrax/ip.cc new file mode 100644 index 0000000..9ec0432 --- /dev/null +++ b/clibs/chibrax/ip.cc @@ -0,0 +1,43 @@ +#include "nodeparams.hpp" +#include +#include +#include +#include +#include +#include +#include + +const int inet_sock_fd = socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK, 0); + +namespace ioctlNode { + int setflags(const char* iface, short flags) { + struct ifreq ifr; + + ifr.ifr_flags = flags; + strncpy(ifr.ifr_name, iface, IFNAMSIZ); + + return ioctl(inet_sock_fd, SIOCSIFFLAGS, &ifr); + } + + void nodeSetFlags(const nodeparam& args) { + v8::Isolate* isolate = args.GetIsolate(); + + if (args.Length() < 2 || !args[0]->IsString() || !args[1]->IsUint32()) { + isolate->ThrowException(v8::Exception::TypeError( + v8::String::NewFromUtf8(isolate, "Invalid arguments ip.srtFlags").ToLocalChecked())); + return; + } + + short flags = (short) argToInt(args[1]); + std::string path = argToString(args[0]); + + args.GetReturnValue().Set(setflags(path.c_str(), flags)); + } + + void Initialize(v8::Local exports) { + NODE_SET_METHOD(exports, "setFlags", nodeSetFlags); + } + + NODE_MODULE(ioctl_api, Initialize) +} + diff --git a/clibs/chibrax/nodeparams.hpp b/clibs/chibrax/nodeparams.hpp new file mode 100644 index 0000000..5acf06b --- /dev/null +++ b/clibs/chibrax/nodeparams.hpp @@ -0,0 +1,7 @@ +#include +#include + +#define nodeparam v8::FunctionCallbackInfo +#define argToInt(arg) arg.As()->Value() +#define argToString(arg) std::string(*v8::String::Utf8Value(isolate, arg)) + diff --git a/clibs/chibrax/socket.cc b/clibs/chibrax/socket.cc new file mode 100644 index 0000000..39ae133 --- /dev/null +++ b/clibs/chibrax/socket.cc @@ -0,0 +1,32 @@ +#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/chibrax/index.js b/rootfs overrides/chibrax/index.js index 26ff3fb..7df3e4a 100644 --- a/rootfs overrides/chibrax/index.js +++ b/rootfs overrides/chibrax/index.js @@ -12,26 +12,6 @@ async function main() { const { fHook } = require("./hook.js") console.clear() - console.log(` - _______ _________ ______ _______ _______ - ( ____ \\|\\ /|\\__ __/( ___ \\ ( ____ )( ___ )|\\ /| - | ( \\/| ) ( | ) ( | ( ) )| ( )|| ( ) |( \\ / ) - | | | (___) | | | | (__/ / | (____)|| (___) | \\ (_) / - | | | ___ | | | | __ ( | __)| ___ | ) _ ( - | | | ( ) | | | | ( \\ \\ | (\\ ( | ( ) | / ( ) \\ - | (____/\\| ) ( |___) (___| )___) )| ) \\ \\__| ) ( |( / \\ ) - (_______/|/ \\|\\_______/|/ \\___/ |/ \\__/|/ \\||/ \\| - - _______ _______ - ( ___ )( ____ \\ - | ( ) || ( \\/ - | | | || (_____ - | | | |(_____ ) - | | | | ) | - | (___) |/\\____) | - (_______)\\_______) - - `) await initd() diff --git a/rootfs overrides/chibrax/ioctl.js b/rootfs overrides/chibrax/ioctl.js new file mode 100644 index 0000000..c3fd2fb --- /dev/null +++ b/rootfs overrides/chibrax/ioctl.js @@ -0,0 +1,6 @@ +//@ts-check +//@ts-expect-error +const { ioctl } = require('./ioctl.node') + + +module.exports = { ioctl } \ No newline at end of file diff --git a/rootfs overrides/chibrax/ip.js b/rootfs overrides/chibrax/ip.js new file mode 100644 index 0000000..78ad4fd --- /dev/null +++ b/rootfs overrides/chibrax/ip.js @@ -0,0 +1,35 @@ +//@ts-check + +//@ts-expect-error +const ip = require('./ip.node') + +const flags = { + IFF_UP: 0x1, /* Interface is up. */ + IFF_BROADCAST: 0x2, /* Broadcast address valid. */ + IFF_DEBUG: 0x4, /* Turn on debugging. */ + IFF_LOOPBACK: 0x8, /* Is a loopback net. */ + IFF_POINTOPOINT: 0x10, /* Interface is point-to-point link. */ + IFF_NOTRAILERS: 0x20, /* Avoid use of trailers. */ + IFF_RUNNING: 0x40, /* Resources allocated. */ + IFF_NOARP: 0x80, /* No address resolution protocol. */ + IFF_PROMISC: 0x100, /* Receive all packets. */ + /* Not supported */ + IFF_ALLMULTI: 0x200, /* Receive all multicast packets. */ + IFF_MASTER: 0x400, /* Master of a load balancer. */ + IFF_SLAVE: 0x800, /* Slave of a load balancer. */ + IFF_MULTICAST: 0x1000, /* Supports multicast. */ + IFF_PORTSEL: 0x2000, /* Can set media type. */ + IFF_AUTOMEDIA: 0x4000, /* Auto media select active. */ + IFF_DYNAMIC: 0x8000 /* Dialup device with changing addresses. */ +} + +/** + * @param { string } interface + * @param { number } flags + * @return { number } + */ +function setFlags(interface, flags) { + return ip.setFlags(interface, flags) +} + +module.exports = { setFlags, flags } \ No newline at end of file diff --git a/rootfs overrides/chibrax/ip.node b/rootfs overrides/chibrax/ip.node new file mode 100755 index 0000000..02da8a3 Binary files /dev/null and b/rootfs overrides/chibrax/ip.node differ diff --git a/rootfs overrides/chibrax/socket.node b/rootfs overrides/chibrax/socket.node new file mode 100755 index 0000000..ca92f15 Binary files /dev/null and b/rootfs overrides/chibrax/socket.node differ diff --git a/rootfs overrides/chibrax/sockets.js b/rootfs overrides/chibrax/sockets.js new file mode 100644 index 0000000..ad89394 --- /dev/null +++ b/rootfs overrides/chibrax/sockets.js @@ -0,0 +1,28 @@ +//@ts-check +//@ts-expect-error +const csocket = require('./socket.node') + +/** + * @param { number } domain + * @param { number } type + * @param { number } protocol + * @return { number } + */ +function socket(domain, type, protocol) { + return csocket.socket(domain, type, protocol) +} + +const socketTypes = { + SOCK_DGRAM : 1, /* Connectionless, unreliable datagrams of fixed maximum length. */ + SOCK_STREAM : 2, /* Sequenced, reliable, connection-based byte streams. */ + SOCK_RAW : 3, /* Raw protocol interface. */ + SOCK_RDM : 4, /* Reliably-delivered messages. */ + SOCK_SEQPACKET : 5, /* Sequenced, reliable, connection-based, datagrams of fixed maximum length. */ + SOCK_DCCP : 6, /* Datagram Congestion Control Protocol. */ + SOCK_PACKET : 10, /* Linux specific way of getting packets at the dev level. For writing rarp and other similar things on the user level. */ + + SOCK_CLOEXEC : 0o2000000, /* Atomically set close-on-exec flag for the new descriptor(s). */ + SOCK_NONBLOCK : 0o0000200 /* Atomically mark descriptor(s) as non-blocking. */ +} + +module.exports = { socketTypes, socket } \ No newline at end of file diff --git a/rootfs overrides/etc/init.d/lo.js b/rootfs overrides/etc/init.d/lo.js new file mode 100644 index 0000000..09cb92e --- /dev/null +++ b/rootfs overrides/etc/init.d/lo.js @@ -0,0 +1,15 @@ +const ip = require('../../chibrax/ip') +//async is optional but recomended for optimisation (optimisations are yet to be implemented) +async function init() { + ip.setFlags('lo', ip.flags.IFF_UP | ip.flags.IFF_LOOPBACK | ip.flags.IFF_RUNNING) +} + +const after = ['filesystem.js'] // list of init script to load before +const before = [] + +module.exports = { + init, + after, + before +} + diff --git a/startQemu.sh b/startQemu.sh index a23e1eb..5a47c84 100755 --- a/startQemu.sh +++ b/startQemu.sh @@ -11,6 +11,4 @@ qemu-system-x86_64\ -k fr\ -vga qxl\ -audiodev pa,id=hda,out.mixing-engine=on \ - -device intel-hda -device hda-output,audiodev=hda \ - -netdev user,id=net0 \ - -device virtio-net-pci,netdev=net0 + -device intel-hda -device hda-output,audiodev=hda