Support broadcast address on switch (and better service calls) (#4498)

* Support broadcast address on switch. Also use R_SUCCEEDED for service calls

* servers_manager: fallback to default broadcast address
This commit is contained in:
Mary 2021-03-03 22:23:03 -05:00 committed by GitHub
parent d9b8b7acad
commit 7509e1bc0a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 8 deletions

View File

@ -175,6 +175,7 @@ extern "C" {
#include <switch/services/ssl.h>
#define Event libnx_Event
#include <switch/services/set.h>
#include <switch/services/nifm.h>
#include <switch/runtime/pad.h>
#undef Event
#include <switch/runtime/devices/socket.h>
@ -2070,9 +2071,11 @@ int main(int argc, char *argv[])
// Initialize socket, needed for networking and nxlink stdio
socketInitialize(&socketConfig);
// Initialize settings, needed to grab language
setInitialize();
// Needed to get ip address
nifmInitialize(NifmServiceType_User);
// Crashes on Reujinx
#ifdef DEBUG_NXLINK
nxlinkStdio();
@ -2548,6 +2551,7 @@ int main(int argc, char *argv[])
// De-initialize stuff!
setExit();
socketExit();
nifmExit();
#endif
#ifdef IOS_STK

View File

@ -46,6 +46,20 @@
#else
#ifndef __SWITCH__
# include <ifaddrs.h>
#else
extern "C" {
#define u64 uint64_t
#define u32 uint32_t
#define s64 int64_t
#define s32 int32_t
#define Event libnx_Event
#include <switch/services/nifm.h>
#undef Event
#undef u64
#undef u32
#undef s32
#undef s64
}
#endif
# include <net/if.h>
#endif
@ -391,6 +405,26 @@ std::vector<SocketAddress> ServersManager::getBroadcastAddresses(bool ipv6)
{
std::vector<SocketAddress> result;
#ifdef __SWITCH__
uint32_t addr;
uint32_t u;
Result resultCode = nifmGetCurrentIpConfigInfo(&addr, &u, NULL, NULL, NULL);
if(R_SUCCEEDED(resultCode))
{
u = htonl(u);
// Convert mask to #bits: SWAT algorithm
u = u - ((u >> 1) & 0x55555555);
u = (u & 0x33333333) + ((u >> 2) & 0x33333333);
u = (((u + (u >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;
SocketAddress saddr(htonl(addr));
addAllBroadcastAddresses(saddr, u, &result);
}
else
{
Log::warn("ServersManager", "Failed to get broadcast address! Error 0x%x", resultCode);
result = getDefaultBroadcastAddresses();
}
return result;
#elif !defined(WIN32)
struct ifaddrs *addresses, *p;

View File

@ -333,9 +333,11 @@ bool SocketAddress::isPublicAddressLocalhost() const
if (m_family == AF_INET)
{
uint32_t currentIp = 0;
nifmGetCurrentIpAddress(&currentIp);
// Unsure how Result works so this is the best I have
if(currentIp)
if(R_FAILED(nifmGetCurrentIpAddress(&currentIp)))
{
Log::warn("SocketAddress", "Failed to get current address!");
}
else if(currentIp)
return htonl(currentIp) == getIP();
}
return false;

View File

@ -118,18 +118,21 @@ void RegisterScreen::init()
AccountUid uid;
// It's possible the user is using an app that doesn't need a user selection
// We try the last opened user as well
if(!accountInitialize(AccountServiceType_Application))
if(!accountGetPreselectedUser(&uid) || !accountGetLastOpenedUser(&uid))
if(R_SUCCEEDED(accountInitialize(AccountServiceType_Application)))
{
if(R_SUCCEEDED(accountGetPreselectedUser(&uid)) || R_SUCCEEDED(accountGetLastOpenedUser(&uid)))
{
AccountProfile profile;
if(!accountGetProfile(&profile, uid))
if(R_SUCCEEDED(accountGetProfile(&profile, uid)))
{
AccountProfileBase profileBase;
if(!accountProfileGet(&profile, NULL, &profileBase))
if(R_SUCCEEDED(accountProfileGet(&profile, NULL, &profileBase)))
username = profileBase.nickname;
accountProfileClose(&profile);
}
}
accountExit();
}
#else
if (getenv("USER") != NULL) // Linux, Macs
username = getenv("USER");