Skip to content

Commit

Permalink
forgot to check that one lol
Browse files Browse the repository at this point in the history
  • Loading branch information
Random06457 committed Sep 2, 2018
1 parent 0ee17cf commit cd61d44
Showing 1 changed file with 48 additions and 68 deletions.
116 changes: 48 additions & 68 deletions sysmodule/source/TcpCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,77 +5,62 @@ const int CLT_MAGIC = 0x33221100;
const int CONTROL_NACP_SIZE = 0x4000;
const int CONTROL_FULL_SIZE = 0x24000;

const int SERVER_VERSION = 1 << 16 | 0 << 8 | 2;
const int SERVER_VERSION = 1 << 16 | 0 << 8 | 3;


//returns cmd id
void SendRaw(int socket, void* buff, size_t size)
{
size_t total = 0;
while (total < size)
{
size_t count = send(socket, (char*)buff + total, size - total, 0);
if (count <= 0)
fatalSimple(MAKERESULT(Module_Discord, Error_SendData));
total += count;
}
}
void ReceiveRaw(int socket, void* buff, size_t size)
{
size_t total = 0;
while (total < size)
{
size_t count = recv(socket, (char*)buff + total, size - total, 0);
if (count <= 0)
fatalSimple(MAKERESULT(Module_Discord, Error_RecData));
total += count;
}
}

ClientCommand ReceiveCommand(int socket)
{
int ret;
char* buff = new char[256];

int len = recv(socket , buff, 256, 0);

if(len < 0)
{
return ClientCommand::Disconnect;
}

ret = *((int*)buff);

ReceiveRaw(socket, &ret, 4);
if ((ret & 0xFFFFFF00) != CLT_MAGIC)
{
return ClientCommand::Disconnect;
}

delete[] buff;

return (ClientCommand)(ret & 0xFF);
}

void SendBuffer(int socket, ServerCommand cmd, void* data, size_t size)
void SendBuffer(int socket, void* data, size_t size)
{
int header = SRV_MAGIC | (u8)ServerCommand::Normal;

u8* buff = new u8[size + 4];

*((int*)buff) = SRV_MAGIC | (u8)cmd;

if (data != nullptr)
memcpy(buff + 4, data, size);


size_t total = 0;
while (total < size + 4) {
size_t count = send(socket, buff + total, (size + 4) - total, 0);
if (count <= 0)
fatalSimple(MAKERESULT(Module_Discord, Error_SendData));
total += count;
}

delete[] buff;
SendRaw(socket, &header, 4);
if (data != NULL)
SendRaw(socket, data, size);
}

void SendConfirm(int socket)
{
SendBuffer(socket, ServerCommand::Confirm, nullptr, 0);
int header = SRV_MAGIC | (u8)ServerCommand::Confirm;
SendRaw(socket, &header, 4);
}

void ReceiveBuffer(int socket, void* out_buff, size_t size)
{
int ret = 0;
u8* buff = new u8[size+4];

size_t total = 0;

while (total < size+4)
{
size_t count = recv(socket, buff + total, (size + 4) - total, 0);
if (count <= 0)
fatalSimple(MAKERESULT(Module_Discord, Error_RecData));
total += count;
}

ret = *((int*)buff);
int ret;
ReceiveRaw(socket, &ret, 4);

if ((ret & 0xFFFFFF00) != CLT_MAGIC)
{
Expand All @@ -87,20 +72,13 @@ void ReceiveBuffer(int socket, void* out_buff, size_t size)
fatalSimple(MAKERESULT(Module_Discord, Error_CmdIdNotSendBuff));
}

memcpy(out_buff, buff + 4, size);

delete[] buff;
ReceiveRaw(socket, out_buff, size);
}

void ReceiveConfirm(int socket)
{
int ret = 0;
int len = recv(socket , &ret, 4, 0);

if(len < 0)
{
fatalSimple(MAKERESULT(Module_Discord, Error_RecData));
}
int ret;
ReceiveRaw(socket, &ret, 4);

if ((ret & 0xFFFFFF00) != CLT_MAGIC)
{
Expand All @@ -113,6 +91,9 @@ void ReceiveConfirm(int socket)
}
}




void SendAppList(int socket)
{
Result rc;
Expand All @@ -125,9 +106,9 @@ void SendAppList(int socket)
fatalSimple(MAKERESULT(Module_Discord, Error_ListAppFailed));
}

SendBuffer(socket, ServerCommand::Normal, &count, 4);
SendBuffer(socket, &count, 4);
ReceiveConfirm(socket);
SendBuffer(socket, ServerCommand::Normal, list, sizeof(NsApplicationRecord) * count);
SendBuffer(socket, list, sizeof(NsApplicationRecord) * count);

delete[] list;
}
Expand Down Expand Up @@ -171,7 +152,6 @@ void SendCurrentApp(int socket)
//applications processes always have PIDs > 0x80
//but atmosphere's pm don't recalculates the pids when a process is removed from the boot list
//so I put 70 to be safe (it now only might bne a problem is more than 10 processes are not booted or were killed)
//this can be removed but i'll be slower
if (pids[i] >= 0x70)
{
//try debugging each application process
Expand Down Expand Up @@ -209,15 +189,15 @@ void SendCurrentApp(int socket)
}

exit_send_current:
SendBuffer(socket, ServerCommand::Normal, &tid, 8);
SendBuffer(socket, &tid, 8);

delete[] pids;
}

void SendVersion(int socket)
{
int ver = SERVER_VERSION;
SendBuffer(socket, ServerCommand::Normal, &ver, 4);
SendBuffer(socket, &ver, 4);
}

void SendActiveUser(int socket)
Expand All @@ -232,7 +212,7 @@ void SendActiveUser(int socket)
if(R_FAILED(rc))
fatalSimple(MAKERESULT(Module_Discord, Error_GetAciveUser));

SendBuffer(socket, ServerCommand::Normal, &account_selected, 1);
SendBuffer(socket, &account_selected, 1);

if(account_selected)
{
Expand All @@ -248,7 +228,7 @@ void SendActiveUser(int socket)

accountProfileClose(&profile);

SendBuffer(socket, ServerCommand::Normal, profilebase.username, 0x20);
SendBuffer(socket, profilebase.username, 0x20);
}
}

Expand All @@ -273,7 +253,7 @@ void SendControlData(int socket)
fatalSimple(MAKERESULT(Module_Discord, Error_InvalidControlSize));
}

SendBuffer(socket, ServerCommand::Normal, control, sizeof(NsApplicationControlData));
SendBuffer(socket, control, sizeof(NsApplicationControlData));

delete control;
}
Expand Down Expand Up @@ -334,4 +314,4 @@ void StartReceiving(int client)
accountExit();
pmdmntExit();
pminfoExit();
}
}

0 comments on commit cd61d44

Please sign in to comment.