src/corosio/src/host_name.cpp

66.7% Lines (4/0/6) 100.0% List of functions (1/0/1)
host_name.cpp
f(x) Functions (1)
Line TLA Hits Source Code
1 //
2 // Copyright (c) 2026 Steve Gerbino
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // Official repository: https://github.com/cppalliance/corosio
8 //
9
10 #include <boost/corosio/host_name.hpp>
11 #include <boost/corosio/detail/platform.hpp>
12
13 #include <string>
14 #include <system_error>
15
16 #if BOOST_COROSIO_POSIX
17 #include <cerrno>
18 #include <cstring>
19 #include <unistd.h>
20 #elif BOOST_COROSIO_HAS_IOCP
21 #include <windows.h>
22 #endif
23
24 namespace boost::corosio {
25
26 #if BOOST_COROSIO_POSIX
27
28 capy::io_result<std::string>
29 6x host_name()
30 {
31 // 256 exceeds POSIX's _POSIX_HOST_NAME_MAX floor of 255 and
32 // every mainstream OS's actual cap (Linux 64, macOS/BSD 255).
33 char buf[256];
34 6x if (::gethostname(buf, sizeof(buf)) != 0)
35 return {std::error_code(errno, std::system_category()), {}};
36
37 // POSIX does not guarantee NUL termination on truncation.
38 6x if (std::memchr(buf, '\0', sizeof(buf)) == nullptr)
39 return {make_error_code(std::errc::value_too_large), {}};
40
41 18x return {std::error_code{}, std::string(buf)};
42 }
43
44 #elif BOOST_COROSIO_HAS_IOCP
45
46 capy::io_result<std::string>
47 host_name()
48 {
49 // Size query: returns ERROR_MORE_DATA and writes the required
50 // wide-char count (including the trailing NUL) into `size`.
51 DWORD size = 0;
52 BOOL ok = ::GetComputerNameExW(
53 ComputerNameDnsHostname, nullptr, &size);
54 DWORD err = ::GetLastError();
55 if (ok)
56 {
57 // Can't-happen guard: a zero-length size query succeeding
58 // would leave `size` meaningless.
59 return {make_error_code(std::errc::protocol_error), {}};
60 }
61 if (err != ERROR_MORE_DATA)
62 return {
63 std::error_code(static_cast<int>(err), std::system_category()),
64 {}};
65
66 // On success, GetComputerNameExW rewrites `size` to the count
67 // without the NUL, so resize(size) below trims to the hostname.
68 std::wstring wide(size, L'\0');
69 if (!::GetComputerNameExW(
70 ComputerNameDnsHostname, wide.data(), &size))
71 return {
72 std::error_code(
73 static_cast<int>(::GetLastError()), std::system_category()),
74 {}};
75 wide.resize(size);
76
77 int needed = ::WideCharToMultiByte(
78 CP_UTF8, 0, wide.data(), static_cast<int>(wide.size()),
79 nullptr, 0, nullptr, nullptr);
80 if (needed <= 0)
81 return {
82 std::error_code(
83 static_cast<int>(::GetLastError()), std::system_category()),
84 {}};
85
86 std::string out(static_cast<std::size_t>(needed), '\0');
87 int written = ::WideCharToMultiByte(
88 CP_UTF8, 0, wide.data(), static_cast<int>(wide.size()),
89 out.data(), needed, nullptr, nullptr);
90 if (written != needed)
91 return {
92 std::error_code(
93 static_cast<int>(::GetLastError()), std::system_category()),
94 {}};
95 return {std::error_code{}, std::move(out)};
96 }
97
98 #endif
99
100 } // namespace boost::corosio
101