diff --git a/nx/include/switch/runtime/devices/socket.h b/nx/include/switch/runtime/devices/socket.h index 4aaf84d9..155eea6f 100644 --- a/nx/include/switch/runtime/devices/socket.h +++ b/nx/include/switch/runtime/devices/socket.h @@ -21,11 +21,6 @@ typedef struct { int dns_timeout; ///< For DNS requests: timeout or 0. } SocketInitConfig; -long gethostid(void); - -// Get host IP formatted -int gethostname(char *name, size_t namelen); - /// Fetch the default configuration for the socket driver. const SocketInitConfig *socketGetDefaultInitConfig(void); /// Initalize the socket driver. diff --git a/nx/include/switch/services/nifm.h b/nx/include/switch/services/nifm.h index 9160175b..a6dceaa8 100644 --- a/nx/include/switch/services/nifm.h +++ b/nx/include/switch/services/nifm.h @@ -16,10 +16,7 @@ typedef struct { Service s; } IGeneralService; -Result nifmInitialize(); +Result nifmInitialize(void); void nifmExit(void); -Result _CreateGeneralService(IGeneralService* out, u64 in); -Result _CreateGeneralServiceOld(IGeneralService* out); - -Result GetCurrentIpAddress(u32* out); \ No newline at end of file +Result nifmGetCurrentIpAddress(u32* out); diff --git a/nx/source/runtime/devices/socket.c b/nx/source/runtime/devices/socket.c index 54de1b01..edde3027 100644 --- a/nx/source/runtime/devices/socket.c +++ b/nx/source/runtime/devices/socket.c @@ -1585,7 +1585,7 @@ cleanup: long gethostid(void) { Result rc; u32 id; - rc = GetCurrentIpAddress(&id); + rc = nifmGetCurrentIpAddress(&id); if(R_SUCCEEDED(rc)) return id; return INADDR_LOOPBACK; @@ -1613,5 +1613,3 @@ struct servent *getservent(void) { h_errno = NO_RECOVERY; errno = ENOSYS; return void sethostent(int a) { (void)a;} void setnetent(int a) { (void)a;} void setprotoent(int a) { (void)a; } - -/************************************************************************************************************************/ diff --git a/nx/source/services/nifm.c b/nx/source/services/nifm.c index 319a3411..9b9933c4 100644 --- a/nx/source/services/nifm.c +++ b/nx/source/services/nifm.c @@ -4,142 +4,148 @@ * @author shadowninja108 * @copyright libnx Authors */ + #include "services/nifm.h" +#include "arm/atomics.h" static Service g_nifmSrv; static IGeneralService g_nifmIGS; +static u64 g_refCnt; -Result nifmInitialize(){ - if (serviceIsActive(&g_nifmSrv)) - return 0; - Result rc; - - rc = smGetService(&g_nifmSrv, "nifm:u"); - - if(R_SUCCEEDED(rc)){ - if(kernelAbove200()) - rc = _CreateGeneralService(&g_nifmIGS, 0); // What does this parameter do? - else - rc = _CreateGeneralServiceOld(&g_nifmIGS); +static Result _nifmCreateGeneralService(IGeneralService * out, u64 in); +static Result _nifmCreateGeneralServiceOld(IGeneralService * 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.s); + serviceClose( & g_nifmSrv); } - - if(R_FAILED(rc)) - nifmExit(); - - return rc; } -void nifmExit(void){ - if(serviceIsActive(&g_nifmIGS.s)) - serviceClose(&g_nifmIGS.s); - if(serviceIsActive(&g_nifmSrv)) - serviceClose(&g_nifmSrv); -} +Result nifmGetCurrentIpAddress(u32 * out) { + IpcCommand c; + ipcInitialize( & c); -Result _CreateGeneralService(IGeneralService* out, u64 in){ - IpcCommand c; - ipcInitialize(&c); - ipcSendPid(&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.s); + + if (R_SUCCEEDED(rc)) { + IpcParsedCommand r; + ipcParse( & r); struct { - u64 magic; - u64 cmd_id; - u64 param; - } PACKED *raw; + u64 magic; + u64 result; + u32 out; + } * resp = r.Raw; - raw = ipcPrepareHeader(&c, sizeof(*raw)); + rc = resp -> result; * out = resp -> out; + } - 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->s, r.Handles[0]); - } - - return rc; + return rc; + } -Result _CreateGeneralServiceOld(IGeneralService* out){ - IpcCommand c; - ipcInitialize(&c); +static Result _nifmCreateGeneralService(IGeneralService * 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 cmd_id; - } PACKED *raw; + u64 magic; + u64 result; + } * resp = r.Raw; - raw = ipcPrepareHeader(&c, sizeof(*raw)); + rc = resp -> result; - raw->magic = SFCI_MAGIC; - raw->cmd_id = 4; + if (R_SUCCEEDED(rc)) + serviceCreate(&out->s, r.Handles[0]); + } - 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->s, r.Handles[0]); - } - } - - return rc; + return rc; } -Result GetCurrentIpAddress(u32* out){ - IpcCommand c; - ipcInitialize(&c); +static Result _nifmCreateGeneralServiceOld(IGeneralService * 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 cmd_id; - } *raw; + u64 magic; + u64 result; + } * resp = r.Raw; - raw = ipcPrepareHeader(&c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 12; - - Result rc = serviceIpcDispatch(&g_nifmIGS.s); + rc = resp -> result; if (R_SUCCEEDED(rc)) - { - IpcParsedCommand r; - ipcParse(&r); + serviceCreate(&out->s, r.Handles[0]); + } - struct - { - u64 magic; - u64 result; - u32 out; - } *resp = r.Raw; - - rc = resp->result; - *out = resp->out; - } - - return rc; - -} \ No newline at end of file + return rc; +}