Skip to content

Commit

Permalink
fix(hostname): fix handling of non-ASCII hostnames on Windows (#3382)
Browse files Browse the repository at this point in the history
  • Loading branch information
cgutman authored Nov 9, 2024
1 parent d552073 commit fb1f5b5
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
Expand Down
7 changes: 7 additions & 0 deletions src/platform/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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()`!
Expand Down
12 changes: 12 additions & 0 deletions src/platform/linux/misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/platform/linux/publish.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
12 changes: 12 additions & 0 deletions src/platform/macos/misc.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -538,6 +539,17 @@
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
Expand Down
10 changes: 10 additions & 0 deletions src/platform/windows/misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
4 changes: 1 addition & 3 deletions src/platform/windows/publish.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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");

Expand Down
7 changes: 7 additions & 0 deletions tests/unit/platform/test_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>> {
Expand Down Expand Up @@ -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());
}

0 comments on commit fb1f5b5

Please sign in to comment.