fix(hostname): fix handling of non-ASCII hostnames on Windows (#3382)

This commit is contained in:
Cameron Gutman 2024-11-09 14:05:46 -06:00 committed by GitHub
parent d552073eaf
commit fb1f5b5a89
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 51 additions and 5 deletions

View File

@ -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
};

View File

@ -858,6 +858,13 @@ namespace platf {
[[nodiscard]] std::unique_ptr<deinit_t>
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()`!

View File

@ -15,6 +15,7 @@
// lib includes
#include <arpa/inet.h>
#include <boost/asio/ip/address.hpp>
#include <boost/asio/ip/host_name.hpp>
#include <boost/process/v1.hpp>
#include <dlfcn.h>
#include <fcntl.h>
@ -797,6 +798,17 @@ namespace platf {
return std::make_unique<qos_t>(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

View File

@ -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(

View File

@ -23,6 +23,7 @@
#include "src/platform/common.h"
#include <boost/asio/ip/address.hpp>
#include <boost/asio/ip/host_name.hpp>
#include <boost/process/v1.hpp>
using namespace std::literals;
@ -538,6 +539,17 @@ namespace platf {
return std::make_unique<qos_t>(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

View File

@ -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() {

View File

@ -9,8 +9,6 @@
#include <windns.h>
#include <winerror.h>
#include <boost/asio/ip/host_name.hpp>
#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");

View File

@ -4,6 +4,8 @@
*/
#include <src/platform/common.h>
#include <boost/asio/ip/host_name.hpp>
#include "../../tests_common.h"
struct SetEnvTest: ::testing::TestWithParam<std::tuple<std::string, std::string, int>> {
@ -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());
}