Cleanup/Refcounting

Added nifm prefixes
Added refcounting
Removed internal methods from include
Cleaned up spacing, comments
This commit is contained in:
shadowninja108 2018-06-03 17:08:46 -07:00
parent b99729142b
commit 84408ba5b9
4 changed files with 120 additions and 124 deletions

View File

@ -21,11 +21,6 @@ typedef struct {
int dns_timeout; ///< For DNS requests: timeout or 0. int dns_timeout; ///< For DNS requests: timeout or 0.
} SocketInitConfig; } SocketInitConfig;
long gethostid(void);
// Get host IP formatted
int gethostname(char *name, size_t namelen);
/// Fetch the default configuration for the socket driver. /// Fetch the default configuration for the socket driver.
const SocketInitConfig *socketGetDefaultInitConfig(void); const SocketInitConfig *socketGetDefaultInitConfig(void);
/// Initalize the socket driver. /// Initalize the socket driver.

View File

@ -16,10 +16,7 @@ typedef struct {
Service s; Service s;
} IGeneralService; } IGeneralService;
Result nifmInitialize(); Result nifmInitialize(void);
void nifmExit(void); void nifmExit(void);
Result _CreateGeneralService(IGeneralService* out, u64 in); Result nifmGetCurrentIpAddress(u32* out);
Result _CreateGeneralServiceOld(IGeneralService* out);
Result GetCurrentIpAddress(u32* out);

View File

@ -1585,7 +1585,7 @@ cleanup:
long gethostid(void) { long gethostid(void) {
Result rc; Result rc;
u32 id; u32 id;
rc = GetCurrentIpAddress(&id); rc = nifmGetCurrentIpAddress(&id);
if(R_SUCCEEDED(rc)) if(R_SUCCEEDED(rc))
return id; return id;
return INADDR_LOOPBACK; 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 sethostent(int a) { (void)a;}
void setnetent(int a) { (void)a;} void setnetent(int a) { (void)a;}
void setprotoent(int a) { (void)a; } void setprotoent(int a) { (void)a; }
/************************************************************************************************************************/

View File

@ -4,12 +4,20 @@
* @author shadowninja108 * @author shadowninja108
* @copyright libnx Authors * @copyright libnx Authors
*/ */
#include "services/nifm.h" #include "services/nifm.h"
#include "arm/atomics.h"
static Service g_nifmSrv; static Service g_nifmSrv;
static IGeneralService g_nifmIGS; static IGeneralService g_nifmIGS;
static u64 g_refCnt;
static Result _nifmCreateGeneralService(IGeneralService * out, u64 in);
static Result _nifmCreateGeneralServiceOld(IGeneralService * out);
Result nifmInitialize(void) {
atomicIncrement64(&g_refCnt);
Result nifmInitialize(){
if (serviceIsActive( & g_nifmSrv)) if (serviceIsActive( & g_nifmSrv))
return 0; return 0;
Result rc; Result rc;
@ -18,9 +26,9 @@ Result nifmInitialize(){
if (R_SUCCEEDED(rc)) { if (R_SUCCEEDED(rc)) {
if (kernelAbove200()) if (kernelAbove200())
rc = _CreateGeneralService(&g_nifmIGS, 0); // What does this parameter do? rc = _nifmCreateGeneralService( & g_nifmIGS, 0); // What does this parameter do?
else else
rc = _CreateGeneralServiceOld(&g_nifmIGS); rc = _nifmCreateGeneralServiceOld( & g_nifmIGS);
} }
if (R_FAILED(rc)) if (R_FAILED(rc))
@ -30,13 +38,46 @@ Result nifmInitialize(){
} }
void nifmExit(void) { void nifmExit(void) {
if(serviceIsActive(&g_nifmIGS.s)) if (atomicDecrement64(&g_refCnt) == 0){
serviceClose( & g_nifmIGS.s); serviceClose( & g_nifmIGS.s);
if(serviceIsActive(&g_nifmSrv))
serviceClose( & g_nifmSrv); serviceClose( & g_nifmSrv);
} }
}
Result _CreateGeneralService(IGeneralService* out, u64 in){ 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.s);
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(IGeneralService * out, u64 in) {
IpcCommand c; IpcCommand c;
ipcInitialize( & c); ipcInitialize( & c);
ipcSendPid( & c); ipcSendPid( & c);
@ -45,7 +86,8 @@ Result _CreateGeneralService(IGeneralService* out, u64 in){
u64 magic; u64 magic;
u64 cmd_id; u64 cmd_id;
u64 param; u64 param;
} PACKED *raw; }
PACKED * raw;
raw = ipcPrepareHeader( & c, sizeof( * raw)); raw = ipcPrepareHeader( & c, sizeof( * raw));
@ -73,14 +115,15 @@ Result _CreateGeneralService(IGeneralService* out, u64 in){
return rc; return rc;
} }
Result _CreateGeneralServiceOld(IGeneralService* out){ static Result _nifmCreateGeneralServiceOld(IGeneralService * out) {
IpcCommand c; IpcCommand c;
ipcInitialize(&c); ipcInitialize(&c);
struct { struct {
u64 magic; u64 magic;
u64 cmd_id; u64 cmd_id;
} PACKED *raw; }
PACKED * raw;
raw = ipcPrepareHeader( & c, sizeof( * raw)); raw = ipcPrepareHeader( & c, sizeof( * raw));
@ -100,46 +143,9 @@ Result _CreateGeneralServiceOld(IGeneralService* out){
rc = resp -> result; rc = resp -> result;
if (R_SUCCEEDED(rc)) { if (R_SUCCEEDED(rc))
serviceCreate(&out->s, r.Handles[0]); serviceCreate(&out->s, r.Handles[0]);
} }
}
return rc; return rc;
} }
Result GetCurrentIpAddress(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.s);
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;
}