Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;

Check warning on line 549 in src/platform/macos/misc.mm

View check run for this annotation

Codecov / codecov/patch

src/platform/macos/misc.mm#L549

Added line #L549 was not covered by tests
}
}

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

Check warning on line 1854 in src/platform/windows/misc.cpp

View check run for this annotation

Codecov / codecov/patch

src/platform/windows/misc.cpp#L1854

Added line #L1854 was not covered by tests
}
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());
}
Loading