diff --git a/src/config.cpp b/src/config.cpp index a61b69b96f1..77531cc06b7 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -410,7 +410,7 @@ namespace config { PRIVATE_KEY_FILE, CERTIFICATE_FILE, - boost::asio::ip::host_name(), // sunshine_name, + platf::get_host_name(), // sunshine_name, "sunshine_state.json"s, // file_state {}, // external_ip }; diff --git a/src/platform/common.h b/src/platform/common.h index ee48ed3909a..4b2ca66a06b 100644 --- a/src/platform/common.h +++ b/src/platform/common.h @@ -858,6 +858,13 @@ namespace platf { [[nodiscard]] std::unique_ptr init(); + /** + * @brief Returns the current computer name in UTF-8. + * @return Computer name or a placeholder upon failure. + */ + std::string + get_host_name(); + /** * @brief Gets the supported gamepads for this platform backend. * @details This may be called prior to `platf::input()`! diff --git a/src/platform/linux/misc.cpp b/src/platform/linux/misc.cpp index 8926261935f..05c5a264e76 100644 --- a/src/platform/linux/misc.cpp +++ b/src/platform/linux/misc.cpp @@ -15,6 +15,7 @@ // lib includes #include #include +#include #include #include #include @@ -797,6 +798,17 @@ namespace platf { return std::make_unique(sockfd, reset_options); } + std::string + get_host_name() { + try { + return boost::asio::ip::host_name(); + } + catch (boost::system::system_error &err) { + BOOST_LOG(error) << "Failed to get hostname: "sv << err.what(); + return "Sunshine"s; + } + } + namespace source { enum source_e : std::size_t { #ifdef SUNSHINE_BUILD_CUDA diff --git a/src/platform/linux/publish.cpp b/src/platform/linux/publish.cpp index 91d49248fbe..9b7dbbc542d 100644 --- a/src/platform/linux/publish.cpp +++ b/src/platform/linux/publish.cpp @@ -426,7 +426,7 @@ namespace platf::publish { return nullptr; } - auto instance_name = net::mdns_instance_name(boost::asio::ip::host_name()); + auto instance_name = net::mdns_instance_name(platf::get_host_name()); name.reset(avahi::strdup(instance_name.c_str())); client.reset( diff --git a/src/platform/macos/misc.mm b/src/platform/macos/misc.mm index 30b8dacdf1a..9f11669f0b9 100644 --- a/src/platform/macos/misc.mm +++ b/src/platform/macos/misc.mm @@ -23,6 +23,7 @@ #include "src/platform/common.h" #include +#include #include using namespace std::literals; @@ -538,6 +539,17 @@ return std::make_unique(sockfd, reset_options); } + std::string + get_host_name() { + try { + return boost::asio::ip::host_name(); + } + catch (boost::system::system_error &err) { + BOOST_LOG(error) << "Failed to get hostname: "sv << err.what(); + return "Sunshine"s; + } + } + class macos_high_precision_timer: public high_precision_timer { public: void diff --git a/src/platform/windows/misc.cpp b/src/platform/windows/misc.cpp index 21fe8b9071b..419ae749261 100644 --- a/src/platform/windows/misc.cpp +++ b/src/platform/windows/misc.cpp @@ -1846,6 +1846,16 @@ namespace platf { return output; } + std::string + get_host_name() { + WCHAR hostname[256]; + if (GetHostNameW(hostname, ARRAYSIZE(hostname)) == SOCKET_ERROR) { + BOOST_LOG(error) << "GetHostNameW() failed: "sv << WSAGetLastError(); + return "Sunshine"s; + } + return to_utf8(hostname); + } + class win32_high_precision_timer: public high_precision_timer { public: win32_high_precision_timer() { diff --git a/src/platform/windows/publish.cpp b/src/platform/windows/publish.cpp index 05208a9cd9d..780d31eb9a8 100644 --- a/src/platform/windows/publish.cpp +++ b/src/platform/windows/publish.cpp @@ -9,8 +9,6 @@ #include #include -#include - #include "misc.h" #include "src/config.h" #include "src/logging.h" @@ -108,7 +106,7 @@ namespace platf::publish { std::wstring domain { SERVICE_TYPE_DOMAIN.data(), SERVICE_TYPE_DOMAIN.size() }; - auto hostname = boost::asio::ip::host_name(); + auto hostname = platf::get_host_name(); auto name = from_utf8(net::mdns_instance_name(hostname) + '.') + domain; auto host = from_utf8(hostname + ".local"); diff --git a/tests/unit/platform/test_common.cpp b/tests/unit/platform/test_common.cpp index 6f1c9becfb6..8424b4bafa7 100644 --- a/tests/unit/platform/test_common.cpp +++ b/tests/unit/platform/test_common.cpp @@ -4,6 +4,8 @@ */ #include +#include + #include "../../tests_common.h" struct SetEnvTest: ::testing::TestWithParam> { @@ -47,3 +49,8 @@ INSTANTIATE_TEST_SUITE_P( std::make_tuple("SUNSHINE_UNIT_TEST_ENV_VAR", "test_value_0", 0), std::make_tuple("SUNSHINE_UNIT_TEST_ENV_VAR", "test_value_1", 0), std::make_tuple("", "test_value", -1))); + +TEST(HostnameTests, TestAsioEquality) { + // These should be equivalent on all platforms for ASCII hostnames + ASSERT_EQ(platf::get_host_name(), boost::asio::ip::host_name()); +}