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,142 +4,148 @@
* @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;
Result nifmInitialize(){ static Result _nifmCreateGeneralService(IGeneralService * out, u64 in);
if (serviceIsActive(&g_nifmSrv)) static Result _nifmCreateGeneralServiceOld(IGeneralService * out);
return 0;
Result rc;
rc = smGetService(&g_nifmSrv, "nifm:u"); Result nifmInitialize(void) {
atomicIncrement64(&g_refCnt);
if(R_SUCCEEDED(rc)){ if (serviceIsActive( & g_nifmSrv))
if(kernelAbove200()) return 0;
rc = _CreateGeneralService(&g_nifmIGS, 0); // What does this parameter do? Result rc;
else
rc = _CreateGeneralServiceOld(&g_nifmIGS); 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){ Result nifmGetCurrentIpAddress(u32 * out) {
if(serviceIsActive(&g_nifmIGS.s)) IpcCommand c;
serviceClose(&g_nifmIGS.s); ipcInitialize( & c);
if(serviceIsActive(&g_nifmSrv))
serviceClose(&g_nifmSrv);
}
Result _CreateGeneralService(IGeneralService* out, u64 in){ struct {
IpcCommand c; u64 magic;
ipcInitialize(&c); u64 cmd_id;
ipcSendPid(&c); } * 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 { struct {
u64 magic; u64 magic;
u64 cmd_id; u64 result;
u64 param; u32 out;
} PACKED *raw; } * resp = r.Raw;
raw = ipcPrepareHeader(&c, sizeof(*raw)); rc = resp -> result; * out = resp -> out;
}
raw->magic = SFCI_MAGIC; return rc;
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;
} }
Result _CreateGeneralServiceOld(IGeneralService* out){ static Result _nifmCreateGeneralService(IGeneralService * out, u64 in) {
IpcCommand c; IpcCommand c;
ipcInitialize(&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 { struct {
u64 magic; u64 magic;
u64 cmd_id; u64 result;
} PACKED *raw; } * resp = r.Raw;
raw = ipcPrepareHeader(&c, sizeof(*raw)); rc = resp -> result;
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->s, r.Handles[0]);
}
}
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)) if (R_SUCCEEDED(rc))
{ serviceCreate(&out->s, r.Handles[0]);
IpcParsedCommand r; }
ipcParse(&r);
struct
{
u64 magic;
u64 result;
u32 out;
} *resp = r.Raw;
rc = resp->result;
*out = resp->out;
}
return rc;
return rc;
}
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 result;
} * resp = r.Raw;
rc = resp -> result;
if (R_SUCCEEDED(rc))
serviceCreate(&out->s, r.Handles[0]);
}
return rc;
} }