Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed Oct 28, 2023
1 parent 9dd5af9 commit 85ee6e5
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
68 changes: 68 additions & 0 deletions source/nifm.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include "nifm.h"

static JSValue nx_nifm_exit(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
{
nifmExit();
return JS_UNDEFINED;
}

static JSValue nx_nifm_initialize(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
{
Result rc = nifmInitialize(NifmServiceType_User);
if (R_FAILED(rc))
{
diagAbortWithResult(rc);
}
return JS_NewCFunction(ctx, nx_nifm_exit, "", 0);
}

static JSValue nx_network_info(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
{
JSValue info = JS_NewObject(ctx);
char str[INET_ADDRSTRLEN];
struct in_addr ip;
struct in_addr subnet_mask;
struct in_addr gateway;
struct in_addr primary_dns_server;
struct in_addr secondary_dns_server;
Result rc = nifmGetCurrentIpConfigInfo(
&ip.s_addr,
&subnet_mask.s_addr,
&gateway.s_addr,
&primary_dns_server.s_addr,
&secondary_dns_server.s_addr);
if (R_FAILED(rc))
{
// TODO: throw error
}

inet_ntop(AF_INET, &(ip.s_addr), str, INET_ADDRSTRLEN);
JS_SetPropertyStr(ctx, info, "ip", JS_NewString(ctx, str));

inet_ntop(AF_INET, &(subnet_mask.s_addr), str, INET_ADDRSTRLEN);
JS_SetPropertyStr(ctx, info, "subnetMask", JS_NewString(ctx, str));

inet_ntop(AF_INET, &(gateway.s_addr), str, INET_ADDRSTRLEN);
JS_SetPropertyStr(ctx, info, "gateway", JS_NewString(ctx, str));

inet_ntop(AF_INET, &(primary_dns_server.s_addr), str, INET_ADDRSTRLEN);
JS_SetPropertyStr(ctx, info, "primaryDnsServer", JS_NewString(ctx, str));

inet_ntop(AF_INET, &(secondary_dns_server.s_addr), str, INET_ADDRSTRLEN);
JS_SetPropertyStr(ctx, info, "secondaryDnsServer", JS_NewString(ctx, str));

return info;
}

static const JSCFunctionListEntry function_list[] = {
JS_CFUNC_DEF("nifmInitialize", 1, nx_nifm_initialize),
JS_CFUNC_DEF("networkInfo", 1, nx_network_info),
};

void nx_init_nifm(JSContext *ctx, JSValueConst init_obj)
{
JS_SetPropertyFunctionList(ctx, init_obj, function_list, countof(function_list));
}
4 changes: 4 additions & 0 deletions source/nifm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once
#include "types.h"

void nx_init_nifm(JSContext *ctx, JSValueConst init_obj);

0 comments on commit 85ee6e5

Please sign in to comment.