Socket: Refactor WiiSockMan::Convert

This commit is contained in:
Sepalani 2022-05-26 16:38:14 +04:00
parent d74fb444f4
commit 41ce3a83d5
2 changed files with 30 additions and 27 deletions

View File

@ -16,6 +16,7 @@
#include <sys/select.h> #include <sys/select.h>
#endif #endif
#include "Common/BitUtils.h"
#include "Common/FileUtil.h" #include "Common/FileUtil.h"
#include "Common/IOFile.h" #include "Common/IOFile.h"
#include "Core/Config/MainSettings.h" #include "Core/Config/MainSettings.h"
@ -278,8 +279,8 @@ void WiiSocket::Update(bool read, bool write, bool except)
case IOCTL_SO_BIND: case IOCTL_SO_BIND:
{ {
sockaddr_in local_name; sockaddr_in local_name;
WiiSockAddrIn* wii_name = (WiiSockAddrIn*)Memory::GetPointer(ioctl.buffer_in + 8); const u8* addr = Memory::GetPointer(ioctl.buffer_in + 8);
WiiSockMan::Convert(*wii_name, local_name); WiiSockMan::ToNativeAddrIn(addr, &local_name);
int ret = bind(fd, (sockaddr*)&local_name, sizeof(local_name)); int ret = bind(fd, (sockaddr*)&local_name, sizeof(local_name));
ReturnValue = WiiSockMan::GetNetErrorCode(ret, "SO_BIND", false); ReturnValue = WiiSockMan::GetNetErrorCode(ret, "SO_BIND", false);
@ -291,8 +292,8 @@ void WiiSocket::Update(bool read, bool write, bool except)
case IOCTL_SO_CONNECT: case IOCTL_SO_CONNECT:
{ {
sockaddr_in local_name; sockaddr_in local_name;
WiiSockAddrIn* wii_name = (WiiSockAddrIn*)Memory::GetPointer(ioctl.buffer_in + 8); const u8* addr = Memory::GetPointer(ioctl.buffer_in + 8);
WiiSockMan::Convert(*wii_name, local_name); WiiSockMan::ToNativeAddrIn(addr, &local_name);
int ret = connect(fd, (sockaddr*)&local_name, sizeof(local_name)); int ret = connect(fd, (sockaddr*)&local_name, sizeof(local_name));
ReturnValue = WiiSockMan::GetNetErrorCode(ret, "SO_CONNECT", false); ReturnValue = WiiSockMan::GetNetErrorCode(ret, "SO_CONNECT", false);
@ -307,13 +308,13 @@ void WiiSocket::Update(bool read, bool write, bool except)
if (ioctl.buffer_out_size > 0) if (ioctl.buffer_out_size > 0)
{ {
sockaddr_in local_name; sockaddr_in local_name;
WiiSockAddrIn* wii_name = (WiiSockAddrIn*)Memory::GetPointer(ioctl.buffer_out); u8* addr = Memory::GetPointer(ioctl.buffer_out);
WiiSockMan::Convert(*wii_name, local_name); WiiSockMan::ToNativeAddrIn(addr, &local_name);
socklen_t addrlen = sizeof(sockaddr_in); socklen_t addrlen = sizeof(sockaddr_in);
ret = static_cast<s32>(accept(fd, (sockaddr*)&local_name, &addrlen)); ret = static_cast<s32>(accept(fd, (sockaddr*)&local_name, &addrlen));
WiiSockMan::Convert(local_name, *wii_name, addrlen); WiiSockMan::ToWiiAddrIn(local_name, addr, addrlen);
} }
else else
{ {
@ -564,8 +565,8 @@ void WiiSocket::Update(bool read, bool write, bool except)
sockaddr_in local_name = {0}; sockaddr_in local_name = {0};
if (has_destaddr) if (has_destaddr)
{ {
WiiSockAddrIn* wii_name = (WiiSockAddrIn*)Memory::GetPointer(BufferIn2 + 0x0C); const u8* addr = Memory::GetPointer(BufferIn2 + 0x0C);
WiiSockMan::Convert(*wii_name, local_name); WiiSockMan::ToNativeAddrIn(addr, &local_name);
} }
auto* to = has_destaddr ? reinterpret_cast<sockaddr*>(&local_name) : nullptr; auto* to = has_destaddr ? reinterpret_cast<sockaddr*>(&local_name) : nullptr;
@ -597,8 +598,8 @@ void WiiSocket::Update(bool read, bool write, bool except)
if (BufferOutSize2 != 0) if (BufferOutSize2 != 0)
{ {
WiiSockAddrIn* wii_name = (WiiSockAddrIn*)Memory::GetPointer(BufferOut2); const u8* addr = Memory::GetPointer(BufferOut2);
WiiSockMan::Convert(*wii_name, local_name); WiiSockMan::ToNativeAddrIn(addr, &local_name);
} }
// Act as non blocking when SO_MSG_NONBLOCK is specified // Act as non blocking when SO_MSG_NONBLOCK is specified
@ -634,8 +635,8 @@ void WiiSocket::Update(bool read, bool write, bool except)
if (BufferOutSize2 != 0) if (BufferOutSize2 != 0)
{ {
WiiSockAddrIn* wii_name = (WiiSockAddrIn*)Memory::GetPointer(BufferOut2); u8* addr = Memory::GetPointer(BufferOut2);
WiiSockMan::Convert(local_name, *wii_name, addrlen); WiiSockMan::ToWiiAddrIn(local_name, addr, addrlen);
} }
break; break;
} }
@ -937,11 +938,12 @@ void WiiSockMan::UpdatePollCommands()
pending_polls.end()); pending_polls.end());
} }
void WiiSockMan::Convert(WiiSockAddrIn const& from, sockaddr_in& to) void WiiSockMan::ToNativeAddrIn(const u8* addr, sockaddr_in* to)
{ {
to.sin_addr.s_addr = from.addr.addr; const WiiSockAddrIn from = Common::BitCastPtr<WiiSockAddrIn>(addr);
to.sin_family = from.family; to->sin_addr.s_addr = from.addr.addr;
to.sin_port = from.port; to->sin_family = from.family;
to->sin_port = from.port;
} }
s32 WiiSockMan::ConvertEvents(s32 events, ConvertDirection dir) s32 WiiSockMan::ConvertEvents(s32 events, ConvertDirection dir)
@ -981,15 +983,15 @@ s32 WiiSockMan::ConvertEvents(s32 events, ConvertDirection dir)
return converted_events; return converted_events;
} }
void WiiSockMan::Convert(sockaddr_in const& from, WiiSockAddrIn& to, s32 addrlen) void WiiSockMan::ToWiiAddrIn(const sockaddr_in& from, u8* to, socklen_t addrlen)
{ {
to.addr.addr = from.sin_addr.s_addr; to[offsetof(WiiSockAddrIn, len)] =
to.family = from.sin_family & 0xFF; u8(addrlen > sizeof(WiiSockAddrIn) ? sizeof(WiiSockAddrIn) : addrlen);
to.port = from.sin_port; to[offsetof(WiiSockAddrIn, family)] = u8(from.sin_family & 0xFF);
if (addrlen < 0 || addrlen > static_cast<s32>(sizeof(WiiSockAddrIn))) const u16& from_port = from.sin_port;
to.len = sizeof(WiiSockAddrIn); memcpy(to + offsetof(WiiSockAddrIn, port), &from_port, sizeof(from_port));
else const u32& from_addr = from.sin_addr.s_addr;
to.len = addrlen; memcpy(to + offsetof(WiiSockAddrIn, addr.addr), &from_addr, sizeof(from_addr));
} }
void WiiSockMan::DoState(PointerWrap& p) void WiiSockMan::DoState(PointerWrap& p)

View File

@ -248,8 +248,9 @@ public:
return instance; // Instantiated on first use. return instance; // Instantiated on first use.
} }
void Update(); void Update();
static void Convert(WiiSockAddrIn const& from, sockaddr_in& to); static void ToNativeAddrIn(const u8* from, sockaddr_in* to);
static void Convert(sockaddr_in const& from, WiiSockAddrIn& to, s32 addrlen = -1); static void ToWiiAddrIn(const sockaddr_in& from, u8* to,
socklen_t addrlen = sizeof(WiiSockAddrIn));
static s32 ConvertEvents(s32 events, ConvertDirection dir); static s32 ConvertEvents(s32 events, ConvertDirection dir);
void DoState(PointerWrap& p); void DoState(PointerWrap& p);