From f80c7c4cd5c090b9a31f89a0eb3d86cbe928c50b Mon Sep 17 00:00:00 2001 From: FearlessTobi Date: Sat, 30 Jul 2022 05:58:23 +0200 Subject: core, network: Add ability to proxy socket packets --- src/core/hle/service/nifm/nifm.cpp | 341 +++++++++++---------- src/core/hle/service/nifm/nifm.h | 27 ++ src/core/hle/service/sockets/bsd.cpp | 40 ++- src/core/hle/service/sockets/bsd.h | 13 +- src/core/hle/service/sockets/sockets.h | 6 +- src/core/hle/service/sockets/sockets_translate.cpp | 2 + 6 files changed, 257 insertions(+), 172 deletions(-) (limited to 'src/core/hle') diff --git a/src/core/hle/service/nifm/nifm.cpp b/src/core/hle/service/nifm/nifm.cpp index 2889973e4..42ed17187 100644 --- a/src/core/hle/service/nifm/nifm.cpp +++ b/src/core/hle/service/nifm/nifm.cpp @@ -6,7 +6,6 @@ #include "core/hle/kernel/k_event.h" #include "core/hle/service/kernel_helpers.h" #include "core/hle/service/nifm/nifm.h" -#include "core/hle/service/service.h" namespace { @@ -271,213 +270,227 @@ public: } }; -class IGeneralService final : public ServiceFramework { -public: - explicit IGeneralService(Core::System& system_); +void IGeneralService::GetClientId(Kernel::HLERequestContext& ctx) { + static constexpr u32 client_id = 1; + LOG_WARNING(Service_NIFM, "(STUBBED) called"); -private: - void GetClientId(Kernel::HLERequestContext& ctx) { - static constexpr u32 client_id = 1; - LOG_WARNING(Service_NIFM, "(STUBBED) called"); + IPC::ResponseBuilder rb{ctx, 4}; + rb.Push(ResultSuccess); + rb.Push(client_id); // Client ID needs to be non zero otherwise it's considered invalid +} - IPC::ResponseBuilder rb{ctx, 4}; - rb.Push(ResultSuccess); - rb.Push(client_id); // Client ID needs to be non zero otherwise it's considered invalid - } +void IGeneralService::CreateScanRequest(Kernel::HLERequestContext& ctx) { + LOG_DEBUG(Service_NIFM, "called"); - void CreateScanRequest(Kernel::HLERequestContext& ctx) { - LOG_DEBUG(Service_NIFM, "called"); + IPC::ResponseBuilder rb{ctx, 2, 0, 1}; - IPC::ResponseBuilder rb{ctx, 2, 0, 1}; + rb.Push(ResultSuccess); + rb.PushIpcInterface(system); +} - rb.Push(ResultSuccess); - rb.PushIpcInterface(system); - } +void IGeneralService::CreateRequest(Kernel::HLERequestContext& ctx) { + LOG_DEBUG(Service_NIFM, "called"); - void CreateRequest(Kernel::HLERequestContext& ctx) { - LOG_DEBUG(Service_NIFM, "called"); + IPC::ResponseBuilder rb{ctx, 2, 0, 1}; - IPC::ResponseBuilder rb{ctx, 2, 0, 1}; + rb.Push(ResultSuccess); + rb.PushIpcInterface(system); +} - rb.Push(ResultSuccess); - rb.PushIpcInterface(system); - } +void IGeneralService::GetCurrentNetworkProfile(Kernel::HLERequestContext& ctx) { + LOG_WARNING(Service_NIFM, "(STUBBED) called"); - void GetCurrentNetworkProfile(Kernel::HLERequestContext& ctx) { - LOG_WARNING(Service_NIFM, "(STUBBED) called"); + const auto net_iface = Network::GetSelectedNetworkInterface(); - const auto net_iface = Network::GetSelectedNetworkInterface(); - - const SfNetworkProfileData network_profile_data = [&net_iface] { - if (!net_iface) { - return SfNetworkProfileData{}; - } - - return SfNetworkProfileData{ - .ip_setting_data{ - .ip_address_setting{ - .is_automatic{true}, - .current_address{Network::TranslateIPv4(net_iface->ip_address)}, - .subnet_mask{Network::TranslateIPv4(net_iface->subnet_mask)}, - .gateway{Network::TranslateIPv4(net_iface->gateway)}, - }, - .dns_setting{ - .is_automatic{true}, - .primary_dns{1, 1, 1, 1}, - .secondary_dns{1, 0, 0, 1}, - }, - .proxy_setting{ - .enabled{false}, - .port{}, - .proxy_server{}, - .automatic_auth_enabled{}, - .user{}, - .password{}, - }, - .mtu{1500}, + SfNetworkProfileData network_profile_data = [&net_iface] { + if (!net_iface) { + return SfNetworkProfileData{}; + } + + return SfNetworkProfileData{ + .ip_setting_data{ + .ip_address_setting{ + .is_automatic{true}, + .current_address{Network::TranslateIPv4(net_iface->ip_address)}, + .subnet_mask{Network::TranslateIPv4(net_iface->subnet_mask)}, + .gateway{Network::TranslateIPv4(net_iface->gateway)}, }, - .uuid{0xdeadbeef, 0xdeadbeef}, - .network_name{"yuzu Network"}, - .wireless_setting_data{ - .ssid_length{12}, - .ssid{"yuzu Network"}, - .passphrase{"yuzupassword"}, + .dns_setting{ + .is_automatic{true}, + .primary_dns{1, 1, 1, 1}, + .secondary_dns{1, 0, 0, 1}, }, - }; - }(); - - ctx.WriteBuffer(network_profile_data); + .proxy_setting{ + .enabled{false}, + .port{}, + .proxy_server{}, + .automatic_auth_enabled{}, + .user{}, + .password{}, + }, + .mtu{1500}, + }, + .uuid{0xdeadbeef, 0xdeadbeef}, + .network_name{"yuzu Network"}, + .wireless_setting_data{ + .ssid_length{12}, + .ssid{"yuzu Network"}, + .passphrase{"yuzupassword"}, + }, + }; + }(); - IPC::ResponseBuilder rb{ctx, 2}; - rb.Push(ResultSuccess); + // When we're connected to a room, spoof the hosts IP address + if (auto room_member = network.GetRoomMember().lock()) { + if (room_member->IsConnected()) { + network_profile_data.ip_setting_data.ip_address_setting.current_address = + room_member->GetFakeIpAddress(); + } } - void RemoveNetworkProfile(Kernel::HLERequestContext& ctx) { - LOG_WARNING(Service_NIFM, "(STUBBED) called"); + ctx.WriteBuffer(network_profile_data); - IPC::ResponseBuilder rb{ctx, 2}; - rb.Push(ResultSuccess); - } + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(ResultSuccess); +} - void GetCurrentIpAddress(Kernel::HLERequestContext& ctx) { - LOG_WARNING(Service_NIFM, "(STUBBED) called"); +void IGeneralService::RemoveNetworkProfile(Kernel::HLERequestContext& ctx) { + LOG_WARNING(Service_NIFM, "(STUBBED) called"); - auto ipv4 = Network::GetHostIPv4Address(); - if (!ipv4) { - LOG_ERROR(Service_NIFM, "Couldn't get host IPv4 address, defaulting to 0.0.0.0"); - ipv4.emplace(Network::IPv4Address{0, 0, 0, 0}); - } + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(ResultSuccess); +} - IPC::ResponseBuilder rb{ctx, 3}; - rb.Push(ResultSuccess); - rb.PushRaw(*ipv4); +void IGeneralService::GetCurrentIpAddress(Kernel::HLERequestContext& ctx) { + LOG_WARNING(Service_NIFM, "(STUBBED) called"); + + auto ipv4 = Network::GetHostIPv4Address(); + if (!ipv4) { + LOG_ERROR(Service_NIFM, "Couldn't get host IPv4 address, defaulting to 0.0.0.0"); + ipv4.emplace(Network::IPv4Address{0, 0, 0, 0}); } - void CreateTemporaryNetworkProfile(Kernel::HLERequestContext& ctx) { - LOG_DEBUG(Service_NIFM, "called"); + // When we're connected to a room, spoof the hosts IP address + if (auto room_member = network.GetRoomMember().lock()) { + if (room_member->IsConnected()) { + ipv4 = room_member->GetFakeIpAddress(); + } + } - ASSERT_MSG(ctx.GetReadBufferSize() == 0x17c, - "SfNetworkProfileData is not the correct size"); - u128 uuid{}; - auto buffer = ctx.ReadBuffer(); - std::memcpy(&uuid, buffer.data() + 8, sizeof(u128)); + IPC::ResponseBuilder rb{ctx, 3}; + rb.Push(ResultSuccess); + rb.PushRaw(*ipv4); +} +void IGeneralService::CreateTemporaryNetworkProfile(Kernel::HLERequestContext& ctx) { + LOG_DEBUG(Service_NIFM, "called"); - IPC::ResponseBuilder rb{ctx, 6, 0, 1}; + ASSERT_MSG(ctx.GetReadBufferSize() == 0x17c, "SfNetworkProfileData is not the correct size"); + u128 uuid{}; + auto buffer = ctx.ReadBuffer(); + std::memcpy(&uuid, buffer.data() + 8, sizeof(u128)); - rb.Push(ResultSuccess); - rb.PushIpcInterface(system); - rb.PushRaw(uuid); - } + IPC::ResponseBuilder rb{ctx, 6, 0, 1}; - void GetCurrentIpConfigInfo(Kernel::HLERequestContext& ctx) { - LOG_WARNING(Service_NIFM, "(STUBBED) called"); + rb.Push(ResultSuccess); + rb.PushIpcInterface(system); + rb.PushRaw(uuid); +} - struct IpConfigInfo { - IpAddressSetting ip_address_setting{}; - DnsSetting dns_setting{}; - }; - static_assert(sizeof(IpConfigInfo) == sizeof(IpAddressSetting) + sizeof(DnsSetting), - "IpConfigInfo has incorrect size."); +void IGeneralService::GetCurrentIpConfigInfo(Kernel::HLERequestContext& ctx) { + LOG_WARNING(Service_NIFM, "(STUBBED) called"); - const auto net_iface = Network::GetSelectedNetworkInterface(); + struct IpConfigInfo { + IpAddressSetting ip_address_setting{}; + DnsSetting dns_setting{}; + }; + static_assert(sizeof(IpConfigInfo) == sizeof(IpAddressSetting) + sizeof(DnsSetting), + "IpConfigInfo has incorrect size."); - const IpConfigInfo ip_config_info = [&net_iface] { - if (!net_iface) { - return IpConfigInfo{}; - } + const auto net_iface = Network::GetSelectedNetworkInterface(); - return IpConfigInfo{ - .ip_address_setting{ - .is_automatic{true}, - .current_address{Network::TranslateIPv4(net_iface->ip_address)}, - .subnet_mask{Network::TranslateIPv4(net_iface->subnet_mask)}, - .gateway{Network::TranslateIPv4(net_iface->gateway)}, - }, - .dns_setting{ - .is_automatic{true}, - .primary_dns{1, 1, 1, 1}, - .secondary_dns{1, 0, 0, 1}, - }, - }; - }(); + IpConfigInfo ip_config_info = [&net_iface] { + if (!net_iface) { + return IpConfigInfo{}; + } - IPC::ResponseBuilder rb{ctx, 2 + (sizeof(IpConfigInfo) + 3) / sizeof(u32)}; - rb.Push(ResultSuccess); - rb.PushRaw(ip_config_info); + return IpConfigInfo{ + .ip_address_setting{ + .is_automatic{true}, + .current_address{Network::TranslateIPv4(net_iface->ip_address)}, + .subnet_mask{Network::TranslateIPv4(net_iface->subnet_mask)}, + .gateway{Network::TranslateIPv4(net_iface->gateway)}, + }, + .dns_setting{ + .is_automatic{true}, + .primary_dns{1, 1, 1, 1}, + .secondary_dns{1, 0, 0, 1}, + }, + }; + }(); + + // When we're connected to a room, spoof the hosts IP address + if (auto room_member = network.GetRoomMember().lock()) { + if (room_member->IsConnected()) { + ip_config_info.ip_address_setting.current_address = room_member->GetFakeIpAddress(); + } } - void IsWirelessCommunicationEnabled(Kernel::HLERequestContext& ctx) { - LOG_WARNING(Service_NIFM, "(STUBBED) called"); + IPC::ResponseBuilder rb{ctx, 2 + (sizeof(IpConfigInfo) + 3) / sizeof(u32)}; + rb.Push(ResultSuccess); + rb.PushRaw(ip_config_info); +} - IPC::ResponseBuilder rb{ctx, 3}; - rb.Push(ResultSuccess); - rb.Push(0); - } +void IGeneralService::IsWirelessCommunicationEnabled(Kernel::HLERequestContext& ctx) { + LOG_WARNING(Service_NIFM, "(STUBBED) called"); - void GetInternetConnectionStatus(Kernel::HLERequestContext& ctx) { - LOG_WARNING(Service_NIFM, "(STUBBED) called"); + IPC::ResponseBuilder rb{ctx, 3}; + rb.Push(ResultSuccess); + rb.Push(1); +} - struct Output { - InternetConnectionType type{InternetConnectionType::WiFi}; - u8 wifi_strength{3}; - InternetConnectionStatus state{InternetConnectionStatus::Connected}; - }; - static_assert(sizeof(Output) == 0x3, "Output has incorrect size."); +void IGeneralService::GetInternetConnectionStatus(Kernel::HLERequestContext& ctx) { + LOG_WARNING(Service_NIFM, "(STUBBED) called"); - constexpr Output out{}; + struct Output { + InternetConnectionType type{InternetConnectionType::WiFi}; + u8 wifi_strength{3}; + InternetConnectionStatus state{InternetConnectionStatus::Connected}; + }; + static_assert(sizeof(Output) == 0x3, "Output has incorrect size."); - IPC::ResponseBuilder rb{ctx, 3}; - rb.Push(ResultSuccess); - rb.PushRaw(out); - } + constexpr Output out{}; - void IsEthernetCommunicationEnabled(Kernel::HLERequestContext& ctx) { - LOG_WARNING(Service_NIFM, "(STUBBED) called"); + IPC::ResponseBuilder rb{ctx, 3}; + rb.Push(ResultSuccess); + rb.PushRaw(out); +} - IPC::ResponseBuilder rb{ctx, 3}; - rb.Push(ResultSuccess); - if (Network::GetHostIPv4Address().has_value()) { - rb.Push(1); - } else { - rb.Push(0); - } +void IGeneralService::IsEthernetCommunicationEnabled(Kernel::HLERequestContext& ctx) { + LOG_WARNING(Service_NIFM, "(STUBBED) called"); + + IPC::ResponseBuilder rb{ctx, 3}; + rb.Push(ResultSuccess); + if (Network::GetHostIPv4Address().has_value()) { + rb.Push(1); + } else { + rb.Push(0); } +} - void IsAnyInternetRequestAccepted(Kernel::HLERequestContext& ctx) { - LOG_WARNING(Service_NIFM, "(STUBBED) called"); +void IGeneralService::IsAnyInternetRequestAccepted(Kernel::HLERequestContext& ctx) { + LOG_ERROR(Service_NIFM, "(STUBBED) called"); - IPC::ResponseBuilder rb{ctx, 3}; - rb.Push(ResultSuccess); - if (Network::GetHostIPv4Address().has_value()) { - rb.Push(1); - } else { - rb.Push(0); - } + IPC::ResponseBuilder rb{ctx, 3}; + rb.Push(ResultSuccess); + if (Network::GetHostIPv4Address().has_value()) { + rb.Push(1); + } else { + rb.Push(0); } -}; +} IGeneralService::IGeneralService(Core::System& system_) - : ServiceFramework{system_, "IGeneralService"} { + : ServiceFramework{system_, "IGeneralService"}, network{system_.GetRoomNetwork()} { // clang-format off static const FunctionInfo functions[] = { {1, &IGeneralService::GetClientId, "GetClientId"}, @@ -528,6 +541,8 @@ IGeneralService::IGeneralService(Core::System& system_) RegisterHandlers(functions); } +IGeneralService::~IGeneralService() = default; + class NetworkInterface final : public ServiceFramework { public: explicit NetworkInterface(const char* name, Core::System& system_) diff --git a/src/core/hle/service/nifm/nifm.h b/src/core/hle/service/nifm/nifm.h index 5f62d0014..48161be28 100644 --- a/src/core/hle/service/nifm/nifm.h +++ b/src/core/hle/service/nifm/nifm.h @@ -3,6 +3,11 @@ #pragma once +#include "core/hle/service/service.h" +#include "network/network.h" +#include "network/room.h" +#include "network/room_member.h" + namespace Core { class System; } @@ -16,4 +21,26 @@ namespace Service::NIFM { /// Registers all NIFM services with the specified service manager. void InstallInterfaces(SM::ServiceManager& service_manager, Core::System& system); +class IGeneralService final : public ServiceFramework { +public: + explicit IGeneralService(Core::System& system_); + ~IGeneralService() override; + +private: + void GetClientId(Kernel::HLERequestContext& ctx); + void CreateScanRequest(Kernel::HLERequestContext& ctx); + void CreateRequest(Kernel::HLERequestContext& ctx); + void GetCurrentNetworkProfile(Kernel::HLERequestContext& ctx); + void RemoveNetworkProfile(Kernel::HLERequestContext& ctx); + void GetCurrentIpAddress(Kernel::HLERequestContext& ctx); + void CreateTemporaryNetworkProfile(Kernel::HLERequestContext& ctx); + void GetCurrentIpConfigInfo(Kernel::HLERequestContext& ctx); + void IsWirelessCommunicationEnabled(Kernel::HLERequestContext& ctx); + void GetInternetConnectionStatus(Kernel::HLERequestContext& ctx); + void IsEthernetCommunicationEnabled(Kernel::HLERequestContext& ctx); + void IsAnyInternetRequestAccepted(Kernel::HLERequestContext& ctx); + + Network::RoomNetwork& network; +}; + } // namespace Service::NIFM diff --git a/src/core/hle/service/sockets/bsd.cpp b/src/core/hle/service/sockets/bsd.cpp index c7194731e..e08c3cb67 100644 --- a/src/core/hle/service/sockets/bsd.cpp +++ b/src/core/hle/service/sockets/bsd.cpp @@ -9,12 +9,16 @@ #include #include "common/microprofile.h" +#include "common/socket_types.h" +#include "core/core.h" #include "core/hle/ipc_helpers.h" #include "core/hle/kernel/k_thread.h" #include "core/hle/service/sockets/bsd.h" #include "core/hle/service/sockets/sockets_translate.h" #include "core/internal_network/network.h" +#include "core/internal_network/socket_proxy.h" #include "core/internal_network/sockets.h" +#include "network/network.h" namespace Service::Sockets { @@ -472,7 +476,13 @@ std::pair BSD::SocketImpl(Domain domain, Type type, Protocol protoco LOG_INFO(Service, "New socket fd={}", fd); - descriptor.socket = std::make_unique(); + auto room_member = room_network.GetRoomMember().lock(); + if (room_member && room_member->IsConnected()) { + descriptor.socket = std::make_unique(room_network); + } else { + descriptor.socket = std::make_unique(); + } + descriptor.socket->Initialize(Translate(domain), Translate(type), Translate(type, protocol)); descriptor.is_connection_based = IsConnectionBased(type); @@ -648,7 +658,7 @@ std::pair BSD::FcntlImpl(s32 fd, FcntlCmd cmd, s32 arg) { ASSERT(arg == 0); return {descriptor.flags, Errno::SUCCESS}; case FcntlCmd::SETFL: { - const bool enable = (arg & FLAG_O_NONBLOCK) != 0; + const bool enable = (arg & Network::FLAG_O_NONBLOCK) != 0; const Errno bsd_errno = Translate(descriptor.socket->SetNonBlock(enable)); if (bsd_errno != Errno::SUCCESS) { return {-1, bsd_errno}; @@ -669,7 +679,7 @@ Errno BSD::SetSockOptImpl(s32 fd, u32 level, OptName optname, size_t optlen, con return Errno::BADF; } - Network::Socket* const socket = file_descriptors[fd]->socket.get(); + Network::SocketBase* const socket = file_descriptors[fd]->socket.get(); if (optname == OptName::LINGER) { ASSERT(optlen == sizeof(Linger)); @@ -724,6 +734,8 @@ std::pair BSD::RecvImpl(s32 fd, u32 flags, std::vector& message) FileDescriptor& descriptor = *file_descriptors[fd]; // Apply flags + using Network::FLAG_MSG_DONTWAIT; + using Network::FLAG_O_NONBLOCK; if ((flags & FLAG_MSG_DONTWAIT) != 0) { flags &= ~FLAG_MSG_DONTWAIT; if ((descriptor.flags & FLAG_O_NONBLOCK) == 0) { @@ -759,6 +771,8 @@ std::pair BSD::RecvFromImpl(s32 fd, u32 flags, std::vector& mess } // Apply flags + using Network::FLAG_MSG_DONTWAIT; + using Network::FLAG_O_NONBLOCK; if ((flags & FLAG_MSG_DONTWAIT) != 0) { flags &= ~FLAG_MSG_DONTWAIT; if ((descriptor.flags & FLAG_O_NONBLOCK) == 0) { @@ -857,8 +871,19 @@ void BSD::BuildErrnoResponse(Kernel::HLERequestContext& ctx, Errno bsd_errno) co rb.PushEnum(bsd_errno); } +void BSD::OnProxyPacketReceived(const Network::ProxyPacket& packet) { + for (auto& optional_descriptor : file_descriptors) { + if (!optional_descriptor.has_value()) { + continue; + } + FileDescriptor& descriptor = *optional_descriptor; + descriptor.socket.get()->HandleProxyPacket(packet); + } +} + BSD::BSD(Core::System& system_, const char* name) - : ServiceFramework{system_, name, ServiceThreadType::CreateNew} { + : ServiceFramework{system_, name, ServiceThreadType::CreateNew}, room_network{ + system_.GetRoomNetwork()} { // clang-format off static const FunctionInfo functions[] = { {0, &BSD::RegisterClient, "RegisterClient"}, @@ -899,6 +924,13 @@ BSD::BSD(Core::System& system_, const char* name) // clang-format on RegisterHandlers(functions); + + if (auto room_member = room_network.GetRoomMember().lock()) { + proxy_packet_received = room_member->BindOnProxyPacketReceived( + [this](const Network::ProxyPacket& packet) { OnProxyPacketReceived(packet); }); + } else { + LOG_ERROR(Service, "Network isn't initalized"); + } } BSD::~BSD() = default; diff --git a/src/core/hle/service/sockets/bsd.h b/src/core/hle/service/sockets/bsd.h index 9ea36428d..81e855e0f 100644 --- a/src/core/hle/service/sockets/bsd.h +++ b/src/core/hle/service/sockets/bsd.h @@ -7,14 +7,17 @@ #include #include "common/common_types.h" +#include "common/socket_types.h" #include "core/hle/service/service.h" #include "core/hle/service/sockets/sockets.h" +#include "network/network.h" namespace Core { class System; } namespace Network { +class SocketBase; class Socket; } // namespace Network @@ -30,7 +33,7 @@ private: static constexpr size_t MAX_FD = 128; struct FileDescriptor { - std::unique_ptr socket; + std::unique_ptr socket; s32 flags = 0; bool is_connection_based = false; }; @@ -165,6 +168,14 @@ private: void BuildErrnoResponse(Kernel::HLERequestContext& ctx, Errno bsd_errno) const noexcept; std::array, MAX_FD> file_descriptors; + + Network::RoomNetwork& room_network; + + /// Callback to parse and handle a received wifi packet. + void OnProxyPacketReceived(const Network::ProxyPacket& packet); + + // Callback identifier for the OnProxyPacketReceived event. + Network::RoomMember::CallbackHandle proxy_packet_received; }; class BSDCFG final : public ServiceFramework { diff --git a/src/core/hle/service/sockets/sockets.h b/src/core/hle/service/sockets/sockets.h index b735b00fc..31b7dad33 100644 --- a/src/core/hle/service/sockets/sockets.h +++ b/src/core/hle/service/sockets/sockets.h @@ -22,7 +22,9 @@ enum class Errno : u32 { AGAIN = 11, INVAL = 22, MFILE = 24, + MSGSIZE = 90, NOTCONN = 107, + TIMEDOUT = 110, }; enum class Domain : u32 { @@ -96,10 +98,6 @@ struct Linger { u32 linger; }; -constexpr u32 FLAG_MSG_DONTWAIT = 0x80; - -constexpr u32 FLAG_O_NONBLOCK = 0x800; - /// Registers all Sockets services with the specified service manager. void InstallInterfaces(SM::ServiceManager& service_manager, Core::System& system); diff --git a/src/core/hle/service/sockets/sockets_translate.cpp b/src/core/hle/service/sockets/sockets_translate.cpp index 2db10ec81..023aa0486 100644 --- a/src/core/hle/service/sockets/sockets_translate.cpp +++ b/src/core/hle/service/sockets/sockets_translate.cpp @@ -25,6 +25,8 @@ Errno Translate(Network::Errno value) { return Errno::MFILE; case Network::Errno::NOTCONN: return Errno::NOTCONN; + case Network::Errno::TIMEDOUT: + return Errno::TIMEDOUT; default: UNIMPLEMENTED_MSG("Unimplemented errno={}", value); return Errno::SUCCESS; -- cgit v1.2.3 From 6d41088153b4b932b4f2524d4252993a5642f998 Mon Sep 17 00:00:00 2001 From: FearlessTobi Date: Mon, 1 Aug 2022 22:47:39 +0200 Subject: core, yuzu: Address first part of review comments --- src/core/announce_multiplayer_session.cpp | 6 +- src/core/hle/service/nifm/nifm.cpp | 1 + src/core/internal_network/network.cpp | 8 +-- src/core/internal_network/socket_proxy.cpp | 10 +-- src/core/internal_network/socket_proxy.h | 3 +- src/core/internal_network/sockets.h | 6 +- src/dedicated_room/yuzu-room.cpp | 100 ++++++++++++++--------------- src/yuzu/multiplayer/validation.h | 2 +- src/yuzu/uisettings.h | 5 +- 9 files changed, 70 insertions(+), 71 deletions(-) (limited to 'src/core/hle') diff --git a/src/core/announce_multiplayer_session.cpp b/src/core/announce_multiplayer_session.cpp index d73a488cf..6737ce85a 100644 --- a/src/core/announce_multiplayer_session.cpp +++ b/src/core/announce_multiplayer_session.cpp @@ -31,7 +31,7 @@ AnnounceMultiplayerSession::AnnounceMultiplayerSession(Network::RoomNetwork& roo } WebService::WebResult AnnounceMultiplayerSession::Register() { - std::shared_ptr room = room_network.GetRoom().lock(); + auto room = room_network.GetRoom().lock(); if (!room) { return WebService::WebResult{WebService::WebResult::Code::LibError, "Network is not initialized", ""}; @@ -102,7 +102,7 @@ void AnnounceMultiplayerSession::UpdateBackendData(std::shared_ptr lock(callback_mutex); + std::lock_guard lock(callback_mutex); for (auto callback : error_callbacks) { (*callback)(result); } @@ -120,7 +120,7 @@ void AnnounceMultiplayerSession::AnnounceMultiplayerLoop() { std::future future; while (!shutdown_event.WaitUntil(update_time)) { update_time += announce_time_interval; - std::shared_ptr room = room_network.GetRoom().lock(); + auto room = room_network.GetRoom().lock(); if (!room) { break; } diff --git a/src/core/hle/service/nifm/nifm.cpp b/src/core/hle/service/nifm/nifm.cpp index 42ed17187..e3ef06481 100644 --- a/src/core/hle/service/nifm/nifm.cpp +++ b/src/core/hle/service/nifm/nifm.cpp @@ -381,6 +381,7 @@ void IGeneralService::GetCurrentIpAddress(Kernel::HLERequestContext& ctx) { rb.Push(ResultSuccess); rb.PushRaw(*ipv4); } + void IGeneralService::CreateTemporaryNetworkProfile(Kernel::HLERequestContext& ctx) { LOG_DEBUG(Service_NIFM, "called"); diff --git a/src/core/internal_network/network.cpp b/src/core/internal_network/network.cpp index 160cc83e4..3b6906f7d 100644 --- a/src/core/internal_network/network.cpp +++ b/src/core/internal_network/network.cpp @@ -352,8 +352,8 @@ std::optional GetHostIPv4Address() { return {}; } - char ip_addr[16] = {}; - ASSERT(inet_ntop(AF_INET, &interface->ip_address, ip_addr, sizeof(ip_addr)) != nullptr); + std::array ip_addr = {}; + ASSERT(inet_ntop(AF_INET, &interface->ip_address, ip_addr.data(), sizeof(ip_addr)) != nullptr); return TranslateIPv4(interface->ip_address); } @@ -402,9 +402,9 @@ Socket::Socket(Socket&& rhs) noexcept { } template -Errno Socket::SetSockOpt(SOCKET _fd, int option, T value) { +Errno Socket::SetSockOpt(SOCKET fd_, int option, T value) { const int result = - setsockopt(_fd, SOL_SOCKET, option, reinterpret_cast(&value), sizeof(value)); + setsockopt(fd_, SOL_SOCKET, option, reinterpret_cast(&value), sizeof(value)); if (result != SOCKET_ERROR) { return Errno::SUCCESS; } diff --git a/src/core/internal_network/socket_proxy.cpp b/src/core/internal_network/socket_proxy.cpp index b9c50430e..216893ba1 100644 --- a/src/core/internal_network/socket_proxy.cpp +++ b/src/core/internal_network/socket_proxy.cpp @@ -30,19 +30,19 @@ void ProxySocket::HandleProxyPacket(const ProxyPacket& packet) { closed) { return; } - std::lock_guard guard(packets_mutex); + std::lock_guard guard(packets_mutex); received_packets.push(packet); } template -Errno ProxySocket::SetSockOpt(SOCKET _fd, int option, T value) { +Errno ProxySocket::SetSockOpt(SOCKET fd_, int option, T value) { socket_options[option] = reinterpret_cast(&value); return Errno::SUCCESS; } Errno ProxySocket::Initialize(Domain domain, Type type, Protocol socket_protocol) { protocol = socket_protocol; - socket_options[0x1008] = reinterpret_cast(&type); + SetSockOpt(fd, SO_TYPE, type); return Errno::SUCCESS; } @@ -101,7 +101,7 @@ std::pair ProxySocket::RecvFrom(int flags, std::vector& message, ASSERT(message.size() < static_cast(std::numeric_limits::max())); { - std::lock_guard guard(packets_mutex); + std::lock_guard guard(packets_mutex); if (received_packets.size() > 0) { return ReceivePacket(flags, message, addr, message.size()); } @@ -115,7 +115,7 @@ std::pair ProxySocket::RecvFrom(int flags, std::vector& message, return {-1, Errno::AGAIN}; } - std::lock_guard guard(packets_mutex); + std::lock_guard guard(packets_mutex); if (received_packets.size() > 0) { return ReceivePacket(flags, message, addr, message.size()); } diff --git a/src/core/internal_network/socket_proxy.h b/src/core/internal_network/socket_proxy.h index c9155f1af..ad917cac3 100644 --- a/src/core/internal_network/socket_proxy.h +++ b/src/core/internal_network/socket_proxy.h @@ -14,7 +14,7 @@ namespace Network { class ProxySocket : public SocketBase { public: - ProxySocket(RoomNetwork& room_network_) noexcept; + explicit ProxySocket(RoomNetwork& room_network_) noexcept; ~ProxySocket() override; ProxySocket(const ProxySocket&) = delete; @@ -82,6 +82,7 @@ public: bool IsOpened() const override; +private: bool broadcast = false; bool closed = false; u32 send_timeout = 0; diff --git a/src/core/internal_network/sockets.h b/src/core/internal_network/sockets.h index 92dc49993..a70429b19 100644 --- a/src/core/internal_network/sockets.h +++ b/src/core/internal_network/sockets.h @@ -32,7 +32,7 @@ public: std::unique_ptr socket; SockAddrIn sockaddr_in; }; - virtual ~SocketBase() {} + virtual ~SocketBase() = default; virtual SocketBase& operator=(const SocketBase&) = delete; @@ -89,11 +89,7 @@ public: virtual void HandleProxyPacket(const ProxyPacket& packet) = 0; -#if defined(_WIN32) SOCKET fd = INVALID_SOCKET; -#elif YUZU_UNIX - int fd = -1; -#endif }; class Socket : public SocketBase { diff --git a/src/dedicated_room/yuzu-room.cpp b/src/dedicated_room/yuzu-room.cpp index 88645dba7..482e772fb 100644 --- a/src/dedicated_room/yuzu-room.cpp +++ b/src/dedicated_room/yuzu-room.cpp @@ -44,28 +44,30 @@ #endif static void PrintHelp(const char* argv0) { - std::cout << "Usage: " << argv0 - << " [options] \n" - "--room-name The name of the room\n" - "--room-description The room description\n" - "--port The port used for the room\n" - "--max_members The maximum number of players for this room\n" - "--password The password for the room\n" - "--preferred-game The preferred game for this room\n" - "--preferred-game-id The preferred game-id for this room\n" - "--username The username used for announce\n" - "--token The token used for announce\n" - "--web-api-url yuzu Web API url\n" - "--ban-list-file The file for storing the room ban list\n" - "--log-file The file for storing the room log\n" - "--enable-yuzu-mods Allow yuzu Community Moderators to moderate on your room\n" - "-h, --help Display this help and exit\n" - "-v, --version Output version information and exit\n"; + LOG_INFO(Network, + "Usage: {}" + " [options] \n" + "--room-name The name of the room\n" + "--room-description The room description\n" + "--port The port used for the room\n" + "--max_members The maximum number of players for this room\n" + "--password The password for the room\n" + "--preferred-game The preferred game for this room\n" + "--preferred-game-id The preferred game-id for this room\n" + "--username The username used for announce\n" + "--token The token used for announce\n" + "--web-api-url yuzu Web API url\n" + "--ban-list-file The file for storing the room ban list\n" + "--log-file The file for storing the room log\n" + "--enable-yuzu-mods Allow yuzu Community Moderators to moderate on your room\n" + "-h, --help Display this help and exit\n" + "-v, --version Output version information and exit\n", + argv0); } static void PrintVersion() { - std::cout << "yuzu dedicated room " << Common::g_scm_branch << " " << Common::g_scm_desc - << " Libnetwork: " << Network::network_version << std::endl; + LOG_INFO(Network, "yuzu dedicated room {} {} Libnetwork: {}", Common::g_scm_branch, + Common::g_scm_desc, Network::network_version); } /// The magic text at the beginning of a yuzu-room ban list file. @@ -76,7 +78,7 @@ static constexpr char token_delimiter{':'}; static std::string UsernameFromDisplayToken(const std::string& display_token) { std::size_t outlen; - std::array output; + std::array output{}; mbedtls_base64_decode(output.data(), output.size(), &outlen, reinterpret_cast(display_token.c_str()), display_token.length()); @@ -87,7 +89,7 @@ static std::string UsernameFromDisplayToken(const std::string& display_token) { static std::string TokenFromDisplayToken(const std::string& display_token) { std::size_t outlen; - std::array output; + std::array output{}; mbedtls_base64_decode(output.data(), output.size(), &outlen, reinterpret_cast(display_token.c_str()), display_token.length()); @@ -99,13 +101,13 @@ static Network::Room::BanList LoadBanList(const std::string& path) { std::ifstream file; Common::FS::OpenFileStream(file, path, std::ios_base::in); if (!file || file.eof()) { - std::cout << "Could not open ban list!\n\n"; + LOG_ERROR(Network, "Could not open ban list!"); return {}; } std::string magic; std::getline(file, magic); if (magic != BanListMagic) { - std::cout << "Ban list is not valid!\n\n"; + LOG_ERROR(Network, "Ban list is not valid!"); return {}; } @@ -137,7 +139,7 @@ static void SaveBanList(const Network::Room::BanList& ban_list, const std::strin std::ofstream file; Common::FS::OpenFileStream(file, path, std::ios_base::out); if (!file) { - std::cout << "Could not save ban list!\n\n"; + LOG_ERROR(Network, "Could not save ban list!"); return; } @@ -153,8 +155,6 @@ static void SaveBanList(const Network::Room::BanList& ban_list, const std::strin for (const auto& ip : ban_list.second) { file << ip << "\n"; } - - file.flush(); } static void InitializeLogging(const std::string& log_file) { @@ -202,6 +202,8 @@ int main(int argc, char** argv) { {0, 0, 0, 0}, }; + InitializeLogging(log_file); + while (optind < argc) { int arg = getopt_long(argc, argv, "n:d:p:m:w:g:u:t:a:i:l:hv", long_options, &option_index); if (arg != -1) { @@ -256,52 +258,53 @@ int main(int argc, char** argv) { } if (room_name.empty()) { - std::cout << "room name is empty!\n\n"; + LOG_ERROR(Network, "Room name is empty!"); PrintHelp(argv[0]); return -1; } if (preferred_game.empty()) { - std::cout << "preferred game is empty!\n\n"; + LOG_ERROR(Network, "Preferred game is empty!"); PrintHelp(argv[0]); return -1; } if (preferred_game_id == 0) { - std::cout << "preferred-game-id not set!\nThis should get set to allow users to find your " - "room.\nSet with --preferred-game-id id\n\n"; + LOG_ERROR(Network, + "preferred-game-id not set!\nThis should get set to allow users to find your " + "room.\nSet with --preferred-game-id id"); } if (max_members > Network::MaxConcurrentConnections || max_members < 2) { - std::cout << "max_members needs to be in the range 2 - " - << Network::MaxConcurrentConnections << "!\n\n"; + LOG_ERROR(Network, "max_members needs to be in the range 2 - {}!", + Network::MaxConcurrentConnections); PrintHelp(argv[0]); return -1; } - if (port > 65535) { - std::cout << "port needs to be in the range 0 - 65535!\n\n"; + if (port > UINT16_MAX) { + LOG_ERROR(Network, "Port needs to be in the range 0 - 65535!"); PrintHelp(argv[0]); return -1; } if (ban_list_file.empty()) { - std::cout << "Ban list file not set!\nThis should get set to load and save room ban " - "list.\nSet with --ban-list-file \n\n"; + LOG_ERROR(Network, "Ban list file not set!\nThis should get set to load and save room ban " + "list.\nSet with --ban-list-file "); } bool announce = true; if (token.empty() && announce) { announce = false; - std::cout << "token is empty: Hosting a private room\n\n"; + LOG_INFO(Network, "Token is empty: Hosting a private room"); } if (web_api_url.empty() && announce) { announce = false; - std::cout << "endpoint url is empty: Hosting a private room\n\n"; + LOG_INFO(Network, "Endpoint url is empty: Hosting a private room"); } if (announce) { if (username.empty()) { - std::cout << "Hosting a public room\n\n"; + LOG_INFO(Network, "Hosting a public room"); Settings::values.web_api_url = web_api_url; Settings::values.yuzu_username = UsernameFromDisplayToken(token); username = Settings::values.yuzu_username.GetValue(); Settings::values.yuzu_token = TokenFromDisplayToken(token); } else { - std::cout << "Hosting a public room\n\n"; + LOG_INFO(Network, "Hosting a public room"); Settings::values.web_api_url = web_api_url; Settings::values.yuzu_username = username; Settings::values.yuzu_token = token; @@ -309,11 +312,9 @@ int main(int argc, char** argv) { } if (!announce && enable_yuzu_mods) { enable_yuzu_mods = false; - std::cout << "Can not enable yuzu Moderators for private rooms\n\n"; + LOG_INFO(Network, "Can not enable yuzu Moderators for private rooms"); } - InitializeLogging(log_file); - // Load the ban list Network::Room::BanList ban_list; if (!ban_list_file.empty()) { @@ -326,27 +327,26 @@ int main(int argc, char** argv) { verify_backend = std::make_unique(Settings::values.web_api_url.GetValue()); #else - std::cout - << "yuzu Web Services is not available with this build: validation is disabled.\n\n"; + LOG_INFO(Network, + "yuzu Web Services is not available with this build: validation is disabled."); verify_backend = std::make_unique(); #endif } else { verify_backend = std::make_unique(); } - Core::System system{}; - auto& network = system.GetRoomNetwork(); + Network::RoomNetwork network{}; network.Init(); - if (std::shared_ptr room = network.GetRoom().lock()) { + if (auto room = network.GetRoom().lock()) { AnnounceMultiplayerRoom::GameInfo preferred_game_info{.name = preferred_game, .id = preferred_game_id}; if (!room->Create(room_name, room_description, "", port, password, max_members, username, preferred_game_info, std::move(verify_backend), ban_list, enable_yuzu_mods)) { - std::cout << "Failed to create room: \n\n"; + LOG_INFO(Network, "Failed to create room: "); return -1; } - std::cout << "Room is open. Close with Q+Enter...\n\n"; + LOG_INFO(Network, "Room is open. Close with Q+Enter..."); auto announce_session = std::make_unique(network); if (announce) { announce_session->Start(); diff --git a/src/yuzu/multiplayer/validation.h b/src/yuzu/multiplayer/validation.h index 7d48e589d..dabf860be 100644 --- a/src/yuzu/multiplayer/validation.h +++ b/src/yuzu/multiplayer/validation.h @@ -10,7 +10,7 @@ class Validation { public: Validation() - : room_name(room_name_regex), nickname(nickname_regex), ip(ip_regex), port(0, 65535) {} + : room_name(room_name_regex), nickname(nickname_regex), ip(ip_regex), port(0, UINT16_MAX) {} ~Validation() = default; diff --git a/src/yuzu/uisettings.h b/src/yuzu/uisettings.h index 25d1bf1e6..e12d414d9 100644 --- a/src/yuzu/uisettings.h +++ b/src/yuzu/uisettings.h @@ -104,11 +104,12 @@ struct Values { // multiplayer settings Settings::Setting multiplayer_nickname{QStringLiteral("yuzu"), "nickname"}; Settings::Setting multiplayer_ip{{}, "ip"}; - Settings::SwitchableSetting multiplayer_port{24872, 0, 65535, "port"}; + Settings::SwitchableSetting multiplayer_port{24872, 0, UINT16_MAX, "port"}; Settings::Setting multiplayer_room_nickname{{}, "room_nickname"}; Settings::Setting multiplayer_room_name{{}, "room_name"}; Settings::SwitchableSetting multiplayer_max_player{8, 0, 8, "max_player"}; - Settings::SwitchableSetting multiplayer_room_port{24872, 0, 65535, "room_port"}; + Settings::SwitchableSetting multiplayer_room_port{24872, 0, UINT16_MAX, + "room_port"}; Settings::SwitchableSetting multiplayer_host_type{0, 0, 1, "host_type"}; Settings::Setting multiplayer_game_id{{}, "game_id"}; Settings::Setting multiplayer_room_description{{}, "room_description"}; -- cgit v1.2.3