mirror of
https://github.com/switchbrew/libnx.git
synced 2025-07-22 18:32:15 +02:00
Added nifm service (#110)
* Added nifm service * Properly implemented gethostid * Implemented gethostname
This commit is contained in:
parent
3c6344d7f3
commit
ad4af954c2
@ -51,6 +51,7 @@ extern "C" {
|
||||
#include "switch/services/pl.h"
|
||||
#include "switch/services/vi.h"
|
||||
#include "switch/services/nv.h"
|
||||
#include "switch/services/nifm.h"
|
||||
#include "switch/services/ns.h"
|
||||
#include "switch/services/ldr.h"
|
||||
#include "switch/services/pm.h"
|
||||
|
18
nx/include/switch/services/nifm.h
Normal file
18
nx/include/switch/services/nifm.h
Normal file
@ -0,0 +1,18 @@
|
||||
/**
|
||||
* @file nifm.h
|
||||
* @brief Network interface service IPC wrapper.
|
||||
* @author shadowninja108
|
||||
* @copyright libnx Authors
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../kernel/ipc.h"
|
||||
#include "../kernel/detect.h"
|
||||
|
||||
#include "../services/sm.h"
|
||||
|
||||
Result nifmInitialize(void);
|
||||
void nifmExit(void);
|
||||
|
||||
Result nifmGetCurrentIpAddress(u32* out);
|
@ -21,6 +21,7 @@
|
||||
#include "runtime/devices/socket.h"
|
||||
#include "services/bsd.h"
|
||||
#include "services/sfdnsres.h"
|
||||
#include "services/nifm.h"
|
||||
#include "result.h"
|
||||
|
||||
__thread int h_errno;
|
||||
@ -1580,6 +1581,23 @@ cleanup:
|
||||
return gaie;
|
||||
}
|
||||
|
||||
long gethostid(void) {
|
||||
Result rc;
|
||||
u32 id;
|
||||
rc = nifmGetCurrentIpAddress(&id);
|
||||
if(R_SUCCEEDED(rc))
|
||||
return id;
|
||||
return INADDR_LOOPBACK;
|
||||
}
|
||||
|
||||
int gethostname(char *name, size_t namelen) {
|
||||
// The Switch doesn't have a proper name, so let's use its IP
|
||||
struct in_addr in;
|
||||
in.s_addr = gethostid();
|
||||
const char *hostname = inet_ntop(AF_INET, &in, name, namelen);
|
||||
return hostname == NULL ? -1 : 0;
|
||||
}
|
||||
|
||||
// Unimplementable functions, left for compliance:
|
||||
struct hostent *gethostent(void) { h_errno = NO_RECOVERY; errno = ENOSYS; return NULL; }
|
||||
struct netent *getnetbyaddr(uint32_t a, int b) { (void)a; (void)b; h_errno = NO_RECOVERY; errno = ENOSYS; return NULL; }
|
||||
@ -1593,18 +1611,4 @@ struct servent *getservbyport(int a, const char *s) { (void)a; (void)s; h_errno
|
||||
struct servent *getservent(void) { h_errno = NO_RECOVERY; errno = ENOSYS; return NULL; }
|
||||
void sethostent(int a) { (void)a;}
|
||||
void setnetent(int a) { (void)a;}
|
||||
void setprotoent(int a) { (void)a; }
|
||||
|
||||
/************************************************************************************************************************/
|
||||
|
||||
long gethostid(void) {
|
||||
return INADDR_LOOPBACK; //FIXME
|
||||
}
|
||||
|
||||
int gethostname(char *name, size_t namelen) {
|
||||
// The Switch doesn't have a proper name, so let's use its IP
|
||||
struct in_addr in;
|
||||
in.s_addr = gethostid();
|
||||
const char *hostname = inet_ntop(AF_INET, &in, name, namelen);
|
||||
return hostname == NULL ? -1 : 0;
|
||||
}
|
||||
void setprotoent(int a) { (void)a; }
|
149
nx/source/services/nifm.c
Normal file
149
nx/source/services/nifm.c
Normal file
@ -0,0 +1,149 @@
|
||||
/**
|
||||
* @file nifm.h
|
||||
* @brief Network interface service IPC wrapper.
|
||||
* @author shadowninja108
|
||||
* @copyright libnx Authors
|
||||
*/
|
||||
|
||||
#include "services/nifm.h"
|
||||
#include "arm/atomics.h"
|
||||
|
||||
static Service g_nifmSrv;
|
||||
static Service g_nifmIGS;
|
||||
static u64 g_refCnt;
|
||||
|
||||
static Result _nifmCreateGeneralService(Service* out, u64 in);
|
||||
static Result _nifmCreateGeneralServiceOld(Service* out);
|
||||
|
||||
Result nifmInitialize(void) {
|
||||
atomicIncrement64(&g_refCnt);
|
||||
|
||||
if (serviceIsActive(&g_nifmSrv))
|
||||
return 0;
|
||||
|
||||
Result rc;
|
||||
rc = smGetService(&g_nifmSrv, "nifm:u");
|
||||
|
||||
if (R_SUCCEEDED(rc)) {
|
||||
if (kernelAbove200())
|
||||
rc = _nifmCreateGeneralService(&g_nifmIGS, 0); // What does this parameter do?
|
||||
else
|
||||
rc = _nifmCreateGeneralServiceOld(&g_nifmIGS);
|
||||
}
|
||||
|
||||
if (R_FAILED(rc))
|
||||
nifmExit();
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
void nifmExit(void) {
|
||||
if (atomicDecrement64(&g_refCnt) == 0) {
|
||||
serviceClose(&g_nifmIGS);
|
||||
serviceClose(&g_nifmSrv);
|
||||
}
|
||||
}
|
||||
|
||||
Result nifmGetCurrentIpAddress(u32* out) {
|
||||
IpcCommand c;
|
||||
ipcInitialize(&c);
|
||||
|
||||
struct {
|
||||
u64 magic;
|
||||
u64 cmd_id;
|
||||
} * raw;
|
||||
|
||||
raw = ipcPrepareHeader(&c, sizeof(*raw));
|
||||
|
||||
raw->magic = SFCI_MAGIC;
|
||||
raw->cmd_id = 12;
|
||||
|
||||
Result rc = serviceIpcDispatch(&g_nifmIGS);
|
||||
|
||||
if (R_SUCCEEDED(rc)) {
|
||||
IpcParsedCommand r;
|
||||
ipcParse(&r);
|
||||
|
||||
struct {
|
||||
u64 magic;
|
||||
u64 result;
|
||||
u32 out;
|
||||
}* resp = r.Raw;
|
||||
|
||||
rc = resp->result;
|
||||
*out = resp->out;
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
static Result _nifmCreateGeneralService(Service* out, u64 in) {
|
||||
IpcCommand c;
|
||||
ipcInitialize(&c);
|
||||
ipcSendPid(&c);
|
||||
|
||||
struct {
|
||||
u64 magic;
|
||||
u64 cmd_id;
|
||||
u64 param;
|
||||
} PACKED* raw;
|
||||
|
||||
raw = ipcPrepareHeader(&c, sizeof(*raw));
|
||||
|
||||
raw->magic = SFCI_MAGIC;
|
||||
raw->cmd_id = 5;
|
||||
raw->param = in;
|
||||
|
||||
Result rc = serviceIpcDispatch(&g_nifmSrv);
|
||||
|
||||
if (R_SUCCEEDED(rc)) {
|
||||
IpcParsedCommand r;
|
||||
ipcParse(&r);
|
||||
|
||||
struct {
|
||||
u64 magic;
|
||||
u64 result;
|
||||
}* resp = r.Raw;
|
||||
|
||||
rc = resp->result;
|
||||
|
||||
if (R_SUCCEEDED(rc))
|
||||
serviceCreate(out, r.Handles[0]);
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
static Result _nifmCreateGeneralServiceOld(Service* out) {
|
||||
IpcCommand c;
|
||||
ipcInitialize(&c);
|
||||
|
||||
struct {
|
||||
u64 magic;
|
||||
u64 cmd_id;
|
||||
} PACKED* raw;
|
||||
|
||||
raw = ipcPrepareHeader(&c, sizeof(*raw));
|
||||
|
||||
raw->magic = SFCI_MAGIC;
|
||||
raw->cmd_id = 4;
|
||||
|
||||
Result rc = serviceIpcDispatch(&g_nifmSrv);
|
||||
|
||||
if (R_SUCCEEDED(rc)) {
|
||||
IpcParsedCommand r;
|
||||
ipcParse(&r);
|
||||
|
||||
struct {
|
||||
u64 magic;
|
||||
u64 result;
|
||||
}* resp = r.Raw;
|
||||
|
||||
rc = resp->result;
|
||||
|
||||
if (R_SUCCEEDED(rc))
|
||||
serviceCreate(out, r.Handles[0]);
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
Loading…
Reference in New Issue
Block a user