src/corosio/src/host_name.cpp

83.3% Lines (5/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 #include <boost/corosio/native/detail/make_err.hpp>
13
14 #include <string>
15 #include <system_error>
16
17 #if BOOST_COROSIO_POSIX
18 #include <cerrno>
19 #include <cstring>
20 #include <unistd.h>
21 #elif BOOST_COROSIO_HAS_IOCP
22 #include <windows.h>
23 #endif
24
25 namespace boost::corosio {
26
27 #if BOOST_COROSIO_POSIX
28
29 capy::io_result<std::string>
30 11x host_name()
31 {
32 // 256 exceeds POSIX's _POSIX_HOST_NAME_MAX floor of 255 and
33 // every mainstream OS's actual cap (Linux 64, macOS/BSD 255).
34 char buf[256];
35 11x if (::gethostname(buf, sizeof(buf)) != 0)
36 5x return {detail::make_err(errno), {}};
37
38 // POSIX does not guarantee NUL termination on truncation.
39 6x if (std::memchr(buf, '\0', sizeof(buf)) == nullptr)
40 return {make_error_code(std::errc::value_too_large), {}};
41
42 18x return {std::error_code{}, std::string(buf)};
43 }
44
45 #elif BOOST_COROSIO_HAS_IOCP
46
47 capy::io_result<std::string>
48 host_name()
49 {
50 // Size query: returns ERROR_MORE_DATA and writes the required
51 // wide-char count (including the trailing NUL) into `size`.
52 DWORD size = 0;
53 BOOL ok = ::GetComputerNameExW(
54 ComputerNameDnsHostname, nullptr, &size);
55 DWORD err = ::GetLastError();
56 if (ok)
57 {
58 // Can't-happen guard: a zero-length size query succeeding
59 // would leave `size` meaningless.
60 return {make_error_code(std::errc::protocol_error), {}};
61 }
62 if (err != ERROR_MORE_DATA)
63 return {
64 detail::make_err(static_cast<unsigned long>(err)),
65 {}};
66
67 // On success, GetComputerNameExW rewrites `size` to the count
68 // without the NUL, so resize(size) below trims to the hostname.
69 std::wstring wide(size, L'\0');
70 if (!::GetComputerNameExW(
71 ComputerNameDnsHostname, wide.data(), &size))
72 return {
73 detail::make_err(
74 static_cast<unsigned long>(::GetLastError())),
75 {}};
76 wide.resize(size);
77
78 int needed = ::WideCharToMultiByte(
79 CP_UTF8, 0, wide.data(), static_cast<int>(wide.size()),
80 nullptr, 0, nullptr, nullptr);
81 if (needed <= 0)
82 return {
83 detail::make_err(
84 static_cast<unsigned long>(::GetLastError())),
85 {}};
86
87 std::string out(static_cast<std::size_t>(needed), '\0');
88 int written = ::WideCharToMultiByte(
89 CP_UTF8, 0, wide.data(), static_cast<int>(wide.size()),
90 out.data(), needed, nullptr, nullptr);
91 if (written != needed)
92 return {
93 detail::make_err(
94 static_cast<unsigned long>(::GetLastError())),
95 {}};
96 return {std::error_code{}, std::move(out)};
97 }
98
99 #endif
100
101 } // namespace boost::corosio
102