src/corosio/src/host_name.cpp

66.7% Lines (18/27) 100.0% List of functions (1/1) 43.3% Branches (13/30)
host_name.cpp
f(x) Functions (1)
Line Branch 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 <stdexcept>
14 #include <string>
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 std::string
29 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 if (::gethostname(buf, sizeof(buf)) != 0)
35 {
36 int e = errno;
37 throw std::runtime_error(
38 std::string("gethostname failed: ") + std::strerror(e));
39 }
40
41 // POSIX does not guarantee NUL termination on truncation.
42 if (std::memchr(buf, '\0', sizeof(buf)) == nullptr)
43 throw std::runtime_error("gethostname: hostname truncated");
44
45 return std::string(buf);
46 }
47
48 #elif BOOST_COROSIO_HAS_IOCP
49
50 std::string
51 6x host_name()
52 {
53 // Size query: returns ERROR_MORE_DATA and writes the required
54 // wide-char count (including the trailing NUL) into `size`.
55 6x DWORD size = 0;
56
1/1
✓ Branch 2 → 3 taken 6 times.
6x BOOL ok = ::GetComputerNameExW(
57 ComputerNameDnsHostname, nullptr, &size);
58
1/1
✓ Branch 3 → 4 taken 6 times.
6x DWORD err = ::GetLastError();
59
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 8 taken 6 times.
6x if (ok)
60 {
61 throw std::runtime_error(
62 "GetComputerNameExW (size query) unexpectedly succeeded");
63 }
64
1/2
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 16 taken 6 times.
6x if (err != ERROR_MORE_DATA)
65 {
66 throw std::runtime_error(
67 "GetComputerNameExW (size query) failed: error " +
68 std::to_string(err));
69 }
70
71 // On success, GetComputerNameExW rewrites `size` to the count
72 // without the NUL, so resize(size) below trims to the hostname.
73
1/1
✓ Branch 18 → 19 taken 6 times.
6x std::wstring wide(size, L'\0');
74
2/3
✓ Branch 21 → 22 taken 6 times.
✗ Branch 22 → 23 not taken.
✓ Branch 22 → 31 taken 6 times.
6x if (!::GetComputerNameExW(
75 ComputerNameDnsHostname, wide.data(), &size))
76 {
77 throw std::runtime_error(
78 "GetComputerNameExW failed: error " +
79 std::to_string(::GetLastError()));
80 }
81
1/1
✓ Branch 31 → 32 taken 6 times.
6x wide.resize(size);
82
83
1/1
✓ Branch 34 → 35 taken 6 times.
6x int needed = ::WideCharToMultiByte(
84 6x CP_UTF8, 0, wide.data(), static_cast<int>(wide.size()),
85 nullptr, 0, nullptr, nullptr);
86
1/2
✗ Branch 35 → 36 not taken.
✓ Branch 35 → 44 taken 6 times.
6x if (needed <= 0)
87 {
88 throw std::runtime_error(
89 "WideCharToMultiByte (size query) failed: error " +
90 std::to_string(::GetLastError()));
91 }
92
93
1/1
✓ Branch 46 → 47 taken 6 times.
6x std::string out(static_cast<std::size_t>(needed), '\0');
94
1/1
✓ Branch 51 → 52 taken 6 times.
12x int written = ::WideCharToMultiByte(
95 6x CP_UTF8, 0, wide.data(), static_cast<int>(wide.size()),
96 out.data(), needed, nullptr, nullptr);
97
1/2
✗ Branch 52 → 53 not taken.
✓ Branch 52 → 61 taken 6 times.
6x if (written != needed)
98 {
99 throw std::runtime_error(
100 "WideCharToMultiByte failed: error " +
101 std::to_string(::GetLastError()));
102 }
103 6x return out;
104 6x }
105
106 #endif
107
108 } // namespace boost::corosio
109