src/corosio/src/host_name.cpp

100.0% Lines (6/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 16x 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 16x if (::gethostname(buf, sizeof(buf)) != 0)
36 5x return {detail::make_err(errno), {}};
37
38 // POSIX does not guarantee NUL termination on truncation.
39 11x if (std::memchr(buf, '\0', sizeof(buf)) == nullptr)
40 5x 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(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 {detail::make_err(static_cast<unsigned long>(err)), {}};
63
64 // On success, GetComputerNameExW rewrites `size` to the count
65 // without the NUL, so resize(size) below trims to the hostname.
66 std::wstring wide(size, L'\0');
67 if (!::GetComputerNameExW(ComputerNameDnsHostname, wide.data(), &size))
68 return {
69 detail::make_err(static_cast<unsigned long>(::GetLastError())), {}};
70 wide.resize(size);
71
72 int needed = ::WideCharToMultiByte(
73 CP_UTF8, 0, wide.data(), static_cast<int>(wide.size()), nullptr, 0,
74 nullptr, nullptr);
75 if (needed <= 0)
76 return {
77 detail::make_err(static_cast<unsigned long>(::GetLastError())), {}};
78
79 std::string out(static_cast<std::size_t>(needed), '\0');
80 int written = ::WideCharToMultiByte(
81 CP_UTF8, 0, wide.data(), static_cast<int>(wide.size()), out.data(),
82 needed, nullptr, nullptr);
83 if (written != needed)
84 return {
85 detail::make_err(static_cast<unsigned long>(::GetLastError())), {}};
86 return {std::error_code{}, std::move(out)};
87 }
88
89 #endif
90
91 } // namespace boost::corosio
92