mirror of
https://github.com/switchbrew/libnx.git
synced 2025-07-04 10:32:15 +02:00
Cleanup/Refcounting
Added nifm prefixes Added refcounting Removed internal methods from include Cleaned up spacing, comments
This commit is contained in:
parent
b99729142b
commit
84408ba5b9
@ -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.
|
||||
|
@ -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);
|
||||
Result nifmGetCurrentIpAddress(u32* out);
|
||||
|
@ -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; }
|
||||
|
||||
/************************************************************************************************************************/
|
||||
|
@ -4,56 +4,98 @@
|
||||
* @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))
|
||||
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");
|
||||
rc = smGetService( & g_nifmSrv, "nifm:u");
|
||||
|
||||
if(R_SUCCEEDED(rc)){
|
||||
if(kernelAbove200())
|
||||
rc = _CreateGeneralService(&g_nifmIGS, 0); // What does this parameter do?
|
||||
if (R_SUCCEEDED(rc)) {
|
||||
if (kernelAbove200())
|
||||
rc = _nifmCreateGeneralService( & g_nifmIGS, 0); // What does this parameter do?
|
||||
else
|
||||
rc = _CreateGeneralServiceOld(&g_nifmIGS);
|
||||
rc = _nifmCreateGeneralServiceOld( & g_nifmIGS);
|
||||
}
|
||||
|
||||
if(R_FAILED(rc))
|
||||
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);
|
||||
void nifmExit(void) {
|
||||
if (atomicDecrement64(&g_refCnt) == 0){
|
||||
serviceClose( & g_nifmIGS.s);
|
||||
serviceClose( & g_nifmSrv);
|
||||
}
|
||||
}
|
||||
|
||||
Result _CreateGeneralService(IGeneralService* out, u64 in){
|
||||
Result nifmGetCurrentIpAddress(u32 * out) {
|
||||
IpcCommand c;
|
||||
ipcInitialize(&c);
|
||||
ipcSendPid(&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;
|
||||
ipcInitialize( & c);
|
||||
ipcSendPid( & c);
|
||||
|
||||
struct {
|
||||
u64 magic;
|
||||
u64 cmd_id;
|
||||
u64 param;
|
||||
} PACKED *raw;
|
||||
}
|
||||
PACKED * raw;
|
||||
|
||||
raw = ipcPrepareHeader(&c, sizeof(*raw));
|
||||
raw = ipcPrepareHeader( & c, sizeof( * raw));
|
||||
|
||||
raw->magic = SFCI_MAGIC;
|
||||
raw->cmd_id = 5;
|
||||
raw->param = in;
|
||||
raw -> magic = SFCI_MAGIC;
|
||||
raw -> cmd_id = 5;
|
||||
raw -> param = in ;
|
||||
|
||||
Result rc = serviceIpcDispatch(&g_nifmSrv);
|
||||
Result rc = serviceIpcDispatch( & g_nifmSrv);
|
||||
|
||||
if (R_SUCCEEDED(rc)) {
|
||||
IpcParsedCommand r;
|
||||
@ -62,9 +104,9 @@ Result _CreateGeneralService(IGeneralService* out, u64 in){
|
||||
struct {
|
||||
u64 magic;
|
||||
u64 result;
|
||||
} *resp = r.Raw;
|
||||
} * resp = r.Raw;
|
||||
|
||||
rc = resp->result;
|
||||
rc = resp -> result;
|
||||
|
||||
if (R_SUCCEEDED(rc))
|
||||
serviceCreate(&out->s, r.Handles[0]);
|
||||
@ -73,21 +115,22 @@ Result _CreateGeneralService(IGeneralService* out, u64 in){
|
||||
return rc;
|
||||
}
|
||||
|
||||
Result _CreateGeneralServiceOld(IGeneralService* out){
|
||||
static Result _nifmCreateGeneralServiceOld(IGeneralService * out) {
|
||||
IpcCommand c;
|
||||
ipcInitialize(&c);
|
||||
|
||||
struct {
|
||||
u64 magic;
|
||||
u64 cmd_id;
|
||||
} PACKED *raw;
|
||||
}
|
||||
PACKED * raw;
|
||||
|
||||
raw = ipcPrepareHeader(&c, sizeof(*raw));
|
||||
raw = ipcPrepareHeader( & c, sizeof( * raw));
|
||||
|
||||
raw->magic = SFCI_MAGIC;
|
||||
raw->cmd_id = 4;
|
||||
raw -> magic = SFCI_MAGIC;
|
||||
raw -> cmd_id = 4;
|
||||
|
||||
Result rc = serviceIpcDispatch(&g_nifmSrv);
|
||||
Result rc = serviceIpcDispatch( & g_nifmSrv);
|
||||
|
||||
if (R_SUCCEEDED(rc)) {
|
||||
IpcParsedCommand r;
|
||||
@ -96,50 +139,13 @@ Result _CreateGeneralServiceOld(IGeneralService* out){
|
||||
struct {
|
||||
u64 magic;
|
||||
u64 result;
|
||||
} *resp = r.Raw;
|
||||
} * 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);
|
||||
rc = resp -> result;
|
||||
|
||||
if (R_SUCCEEDED(rc))
|
||||
{
|
||||
IpcParsedCommand r;
|
||||
ipcParse(&r);
|
||||
|
||||
struct
|
||||
{
|
||||
u64 magic;
|
||||
u64 result;
|
||||
u32 out;
|
||||
} *resp = r.Raw;
|
||||
|
||||
rc = resp->result;
|
||||
*out = resp->out;
|
||||
serviceCreate(&out->s, r.Handles[0]);
|
||||
}
|
||||
|
||||
return rc;
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user