diff --git a/libraries/libstratosphere/source/htc/server/rpc/htc_rpc_client.hpp b/libraries/libstratosphere/source/htc/server/rpc/htc_rpc_client.hpp index 11a712b51..9b44488c4 100644 --- a/libraries/libstratosphere/source/htc/server/rpc/htc_rpc_client.hpp +++ b/libraries/libstratosphere/source/htc/server/rpc/htc_rpc_client.hpp @@ -37,13 +37,13 @@ namespace ams::htc::server::rpc { using RpcTaskArgumentsType = decltype(RpcTaskFunctionTraits::GetSetArgumentsImpl(&T::SetArguments)); template requires IsRpcTask - using RpcTaskResultType = decltype(RpcTaskFunctionTraits::GetGetResultImpl(&T::GetResult)); + using RpcTaskResultsType = decltype(RpcTaskFunctionTraits::GetGetResultImpl(&T::GetResult)); - template - concept IsRpcTaskArgumentsType = IsRpcTask && std::same_as, RpcTaskArgumentsType>; + template requires IsRpcTask + using RpcTaskArgumentType = typename std::tuple_element>::type; - template - concept IsRpcTaskResultType = IsRpcTask && std::same_as, RpcTaskResultType>; + template requires IsRpcTask + using RpcTaskResultType = typename std::tuple_element>::type; class RpcClient { private: @@ -91,13 +91,9 @@ namespace ams::htc::server::rpc { Result ReceiveHeader(RpcPacket *header); Result ReceiveBody(char *dst, size_t size); Result SendRequest(const char *src, size_t size); - public: - void Wait(u32 task_id) { - os::WaitEvent(m_task_table.Get(task_id)->GetEvent()); - } - - template requires IsRpcTaskArgumentsType - Result Begin(u32 *out_task_id, Args... args) { + private: + template requires IsRpcTask + ALWAYS_INLINE Result BeginImpl(std::index_sequence, u32 *out_task_id, RpcTaskArgumentType... args) { /* Lock ourselves. */ std::scoped_lock lk(m_mutex); @@ -138,8 +134,8 @@ namespace ams::htc::server::rpc { return ResultSuccess(); } - template requires IsRpcTaskResultType - Result End(u32 task_id, Args... args) { + template requires IsRpcTask + ALWAYS_INLINE Result EndImpl(std::index_sequence, u32 task_id, RpcTaskResultType... args) { /* Lock ourselves. */ std::scoped_lock lk(m_mutex); @@ -173,6 +169,20 @@ namespace ams::htc::server::rpc { return ResultSuccess(); } + public: + void Wait(u32 task_id) { + os::WaitEvent(m_task_table.Get(task_id)->GetEvent()); + } + + template requires (IsRpcTask && sizeof...(Args) == std::tuple_size>::value) + Result Begin(u32 *out_task_id, Args &&... args) { + return this->BeginImpl(std::make_index_sequence>::value>(), out_task_id, std::forward(args)...); + } + + template requires (IsRpcTask && sizeof...(Args) == std::tuple_size>::value) + Result End(u32 task_id, Args &&... args) { + return this->EndImpl(std::make_index_sequence>::value>(), task_id, std::forward(args)...); + } }; } diff --git a/libraries/libstratosphere/source/htcs/impl/htcs_manager.cpp b/libraries/libstratosphere/source/htcs/impl/htcs_manager.cpp index 2d20fd85a..6b5aff2e3 100644 --- a/libraries/libstratosphere/source/htcs/impl/htcs_manager.cpp +++ b/libraries/libstratosphere/source/htcs/impl/htcs_manager.cpp @@ -58,8 +58,7 @@ namespace ams::htcs::impl { void HtcsManager::Close(s32 *out_err, s32 *out_res, s32 desc) { /* Invoke our implementation. */ - s32 err; - const Result result = m_impl->DestroySocket(std::addressof(err), desc); + const Result result = m_impl->DestroySocket(desc); /* Set output. */ *out_err = ConvertResultToErrorCode(result); diff --git a/libraries/libstratosphere/source/htcs/impl/htcs_manager_impl.cpp b/libraries/libstratosphere/source/htcs/impl/htcs_manager_impl.cpp index 75312e134..37644a727 100644 --- a/libraries/libstratosphere/source/htcs/impl/htcs_manager_impl.cpp +++ b/libraries/libstratosphere/source/htcs/impl/htcs_manager_impl.cpp @@ -50,8 +50,8 @@ namespace ams::htcs::impl { return m_service.CreateSocket(out_err, out_desc, enable_disconnection_emulation); } - Result HtcsManagerImpl::DestroySocket(s32 *out_err, s32 desc) { - return m_service.DestroySocket(out_err, desc); + Result HtcsManagerImpl::DestroySocket(s32 desc) { + return m_service.DestroySocket(desc); } Result HtcsManagerImpl::Connect(s32 *out_err, s32 desc, const SockAddrHtcs &address) { diff --git a/libraries/libstratosphere/source/htcs/impl/htcs_manager_impl.hpp b/libraries/libstratosphere/source/htcs/impl/htcs_manager_impl.hpp index 68047eb94..5b2926864 100644 --- a/libraries/libstratosphere/source/htcs/impl/htcs_manager_impl.hpp +++ b/libraries/libstratosphere/source/htcs/impl/htcs_manager_impl.hpp @@ -43,7 +43,7 @@ namespace ams::htcs::impl { bool IsServiceAvailable(); public: Result CreateSocket(s32 *out_err, s32 *out_desc, bool enable_disconnection_emulation); - Result DestroySocket(s32 *out_err, s32 desc); + Result DestroySocket(s32 desc); Result Connect(s32 *out_err, s32 desc, const SockAddrHtcs &address); Result Bind(s32 *out_err, s32 desc, const SockAddrHtcs &address); Result Listen(s32 *out_err, s32 desc, s32 backlog_count); diff --git a/libraries/libstratosphere/source/htcs/impl/htcs_service.cpp b/libraries/libstratosphere/source/htcs/impl/htcs_service.cpp index 893ce65ed..5f4f3697a 100644 --- a/libraries/libstratosphere/source/htcs/impl/htcs_service.cpp +++ b/libraries/libstratosphere/source/htcs/impl/htcs_service.cpp @@ -16,6 +16,7 @@ #include #include "htcs_service.hpp" #include "rpc/htcs_rpc_tasks.hpp" +#include "htcs_util.hpp" namespace ams::htcs::impl { @@ -45,4 +46,92 @@ namespace ams::htcs::impl { return ResultSuccess(); } + Result HtcsService::DestroySocket(s32 desc) { + /* Begin the task. */ + u32 task_id; + R_TRY(m_rpc_client->Begin(std::addressof(task_id), desc)); + + /* Wait for the task to complete. */ + this->WaitTask(task_id); + + /* Cancel the socket. */ + m_rpc_client->CancelBySocket(desc); + + /* Finish the task. */ + htcs::SocketError err; + R_TRY(m_rpc_client->End(task_id, std::addressof(err))); + + return ResultSuccess(); + } + + Result HtcsService::Connect(s32 *out_err, s32 desc, const SockAddrHtcs &address) { + /* Validate the address. */ + R_UNLESS(address.family == 0, htcs::ResultInvalidArgument()); + R_UNLESS(IsValidName(address.peer_name), htcs::ResultInvalidArgument()); + R_UNLESS(IsValidName(address.port_name), htcs::ResultInvalidArgument()); + + /* Begin the task. */ + u32 task_id; + R_TRY(m_rpc_client->Begin(std::addressof(task_id), desc, address.peer_name, address.port_name)); + + /* Wait for the task to complete. */ + this->WaitTask(task_id); + + /* Finish the task. */ + htcs::SocketError err; + R_TRY(m_rpc_client->End(task_id, std::addressof(err))); + + /* Set output. */ + *out_err = err; + return ResultSuccess(); + } + + Result HtcsService::Bind(s32 *out_err, s32 desc, const SockAddrHtcs &address) { + /* Validate the address. */ + R_UNLESS(address.family == 0, htcs::ResultInvalidArgument()); + R_UNLESS(IsValidName(address.peer_name), htcs::ResultInvalidArgument()); + R_UNLESS(IsValidName(address.port_name), htcs::ResultInvalidArgument()); + + /* Begin the task. */ + u32 task_id; + R_TRY(m_rpc_client->Begin(std::addressof(task_id), desc, address.peer_name, address.port_name)); + + /* Wait for the task to complete. */ + this->WaitTask(task_id); + + /* Finish the task. */ + htcs::SocketError err; + R_TRY(m_rpc_client->End(task_id, std::addressof(err))); + + /* Set output. */ + *out_err = err; + return ResultSuccess(); + } + + Result HtcsService::Listen(s32 *out_err, s32 desc, s32 backlog_count); + Result HtcsService::Receive(s32 *out_err, s64 *out_size, char *buffer, size_t size, s32 desc, s32 flags); + Result HtcsService::Send(s32 *out_err, s64 *out_size, const char *buffer, size_t size, s32 desc, s32 flags); + Result HtcsService::Shutdown(s32 *out_err, s32 desc, s32 how); + Result HtcsService::Fcntl(s32 *out_err, s32 *out_res, s32 desc, s32 command, s32 value); + + Result HtcsService::AcceptStart(u32 *out_task_id, Handle *out_handle, s32 desc); + Result HtcsService::AcceptResults(s32 *out_err, s32 *out_desc, SockAddrHtcs *out_address, u32 task_id, s32 desc); + + Result HtcsService::ReceiveSmallStart(u32 *out_task_id, Handle *out_handle, s64 size, s32 desc, s32 flags); + Result HtcsService::ReceiveSmallResults(s32 *out_err, s64 *out_size, char *buffer, s64 buffer_size, u32 task_id, s32 desc); + + Result HtcsService::SendSmallStart(u32 *out_task_id, Handle *out_handle, s32 desc, s64 size, s32 flags); + Result HtcsService::SendSmallContinue(s64 *out_size, const char *buffer, s64 buffer_size, u32 task_id, s32 desc); + Result HtcsService::SendSmallResults(s32 *out_err, s64 *out_size, u32 task_id, s32 desc); + + Result HtcsService::SendStart(u32 *out_task_id, Handle *out_handle, s32 desc, s64 size, s32 flags); + Result HtcsService::SendContinue(s64 *out_size, const char *buffer, s64 buffer_size, u32 task_id, s32 desc); + Result HtcsService::SendResults(s32 *out_err, s64 *out_size, u32 task_id, s32 desc); + + Result HtcsService::ReceiveStart(u32 *out_task_id, Handle *out_handle, s64 size, s32 desc, s32 flags); + Result HtcsService::ReceiveResults(s32 *out_err, s64 *out_size, char *buffer, s64 buffer_size, u32 task_id, s32 desc); + + Result HtcsService::SelectStart(u32 *out_task_id, Handle *out_handle, Span read_handles, Span write_handles, Span exception_handles, s64 tv_sec, s64 tv_usec); + Result HtcsService::SelectEnd(s32 *out_err, s32 *out_res, Span read_handles, Span write_handles, Span exception_handles, u32 task_id); + } diff --git a/libraries/libstratosphere/source/htcs/impl/htcs_service.hpp b/libraries/libstratosphere/source/htcs/impl/htcs_service.hpp index 2ae89cf55..5546ca128 100644 --- a/libraries/libstratosphere/source/htcs/impl/htcs_service.hpp +++ b/libraries/libstratosphere/source/htcs/impl/htcs_service.hpp @@ -34,7 +34,7 @@ namespace ams::htcs::impl { /* TODO */ public: Result CreateSocket(s32 *out_err, s32 *out_desc, bool enable_disconnection_emulation); - Result DestroySocket(s32 *out_err, s32 desc); + Result DestroySocket(s32 desc); Result Connect(s32 *out_err, s32 desc, const SockAddrHtcs &address); Result Bind(s32 *out_err, s32 desc, const SockAddrHtcs &address); Result Listen(s32 *out_err, s32 desc, s32 backlog_count); diff --git a/libraries/libstratosphere/source/htcs/impl/htcs_util.hpp b/libraries/libstratosphere/source/htcs/impl/htcs_util.hpp index 9f7684cfe..8189c4a20 100644 --- a/libraries/libstratosphere/source/htcs/impl/htcs_util.hpp +++ b/libraries/libstratosphere/source/htcs/impl/htcs_util.hpp @@ -20,4 +20,17 @@ namespace ams::htcs::impl { s32 ConvertResultToErrorCode(const Result result); + constexpr bool IsValidName(const char *name) { + static_assert(PeerNameBufferLength == PortNameBufferLength); + return util::Strnlen(name, PeerNameBufferLength) < PeerNameBufferLength; + } + + constexpr bool IsValidName(const HtcsPeerName &name) { + return IsValidName(name.name); + } + + constexpr bool IsValidName(const HtcsPortName &name) { + return IsValidName(name.name); + } + } diff --git a/libraries/libvapours/include/vapours/results/htcs_results.hpp b/libraries/libvapours/include/vapours/results/htcs_results.hpp index 14683cdcc..7903cb4bd 100644 --- a/libraries/libvapours/include/vapours/results/htcs_results.hpp +++ b/libraries/libvapours/include/vapours/results/htcs_results.hpp @@ -22,8 +22,9 @@ namespace ams::htcs { R_DEFINE_ERROR_RESULT(InvalidHandle, 9); - R_DEFINE_ERROR_RESULT(InvalidSize, 2014); - R_DEFINE_ERROR_RESULT(Unknown2021, 2021); - R_DEFINE_ERROR_RESULT(Unknown2023, 2023); + R_DEFINE_ERROR_RESULT(InvalidArgument, 2001); + R_DEFINE_ERROR_RESULT(InvalidSize, 2014); + R_DEFINE_ERROR_RESULT(Unknown2021, 2021); + R_DEFINE_ERROR_RESULT(Unknown2023, 2023); }