src/corosio/src/host_name.cpp
57.1% Lines (4/7)
100.0% List of functions (1/1)
Functions (1)
Function
Calls
Lines
Blocks
boost::corosio::host_name[abi:cxx11]()
:29
6x
57.1%
25.0%
| 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 <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 | 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 | { | ||
| 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 | 6x | if (std::memchr(buf, '\0', sizeof(buf)) == nullptr) | |
| 43 | ✗ | throw std::runtime_error("gethostname: hostname truncated"); | |
| 44 | |||
| 45 | 12x | return std::string(buf); | |
| 46 | } | ||
| 47 | |||
| 48 | #elif BOOST_COROSIO_HAS_IOCP | ||
| 49 | |||
| 50 | std::string | ||
| 51 | 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 | DWORD size = 0; | ||
| 56 | BOOL ok = ::GetComputerNameExW( | ||
| 57 | ComputerNameDnsHostname, nullptr, &size); | ||
| 58 | DWORD err = ::GetLastError(); | ||
| 59 | if (ok) | ||
| 60 | { | ||
| 61 | throw std::runtime_error( | ||
| 62 | "GetComputerNameExW (size query) unexpectedly succeeded"); | ||
| 63 | } | ||
| 64 | 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 | std::wstring wide(size, L'\0'); | ||
| 74 | if (!::GetComputerNameExW( | ||
| 75 | ComputerNameDnsHostname, wide.data(), &size)) | ||
| 76 | { | ||
| 77 | throw std::runtime_error( | ||
| 78 | "GetComputerNameExW failed: error " + | ||
| 79 | std::to_string(::GetLastError())); | ||
| 80 | } | ||
| 81 | wide.resize(size); | ||
| 82 | |||
| 83 | int needed = ::WideCharToMultiByte( | ||
| 84 | CP_UTF8, 0, wide.data(), static_cast<int>(wide.size()), | ||
| 85 | nullptr, 0, nullptr, nullptr); | ||
| 86 | if (needed <= 0) | ||
| 87 | { | ||
| 88 | throw std::runtime_error( | ||
| 89 | "WideCharToMultiByte (size query) failed: error " + | ||
| 90 | std::to_string(::GetLastError())); | ||
| 91 | } | ||
| 92 | |||
| 93 | std::string out(static_cast<std::size_t>(needed), '\0'); | ||
| 94 | int written = ::WideCharToMultiByte( | ||
| 95 | CP_UTF8, 0, wide.data(), static_cast<int>(wide.size()), | ||
| 96 | out.data(), needed, nullptr, nullptr); | ||
| 97 | if (written != needed) | ||
| 98 | { | ||
| 99 | throw std::runtime_error( | ||
| 100 | "WideCharToMultiByte failed: error " + | ||
| 101 | std::to_string(::GetLastError())); | ||
| 102 | } | ||
| 103 | return out; | ||
| 104 | } | ||
| 105 | |||
| 106 | #endif | ||
| 107 | |||
| 108 | } // namespace boost::corosio | ||
| 109 |