-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract gethostname into a dedicated module. Add unit test and better…
… error management.
- Loading branch information
Showing
7 changed files
with
152 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#pragma once | ||
|
||
#include "cct_config.hpp" | ||
#include "cct_string.hpp" | ||
|
||
namespace cct { | ||
class HostNameGetter { | ||
public: | ||
#ifdef CCT_MSVC | ||
HostNameGetter(); | ||
#else | ||
HostNameGetter() noexcept = default; | ||
#endif | ||
|
||
HostNameGetter(const HostNameGetter &) = delete; | ||
HostNameGetter &operator=(const HostNameGetter &) = delete; | ||
HostNameGetter(HostNameGetter &&) = delete; | ||
HostNameGetter &operator=(HostNameGetter &&) = delete; | ||
|
||
#ifdef CCT_MSVC | ||
~HostNameGetter(); | ||
#else | ||
~HostNameGetter() = default; | ||
#endif | ||
|
||
/// Safe version of gethostname, working in POSIX and Windows with similar behavior. | ||
string getHostName() const; | ||
}; | ||
|
||
} // namespace cct |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#include "gethostname.hpp" | ||
|
||
#include <iostream> | ||
|
||
#include "cct_config.hpp" | ||
#include "cct_exception.hpp" | ||
#include "cct_string.hpp" | ||
#ifdef CCT_MSVC | ||
#include <Winsock2.h> | ||
#include <winsock.h> | ||
#else | ||
#include <errno.h> | ||
#include <unistd.h> | ||
#endif | ||
|
||
namespace cct { | ||
#ifdef CCT_MSVC | ||
HostNameGetter::HostNameGetter() { | ||
WSADATA wsaData; | ||
WORD wVersionRequested = MAKEWORD(2, 2); | ||
auto errCode = WSAStartup(wVersionRequested, &wsaData); | ||
if (errCode != 0) { | ||
throw exception("Error {} in WSAStartup", errCode); | ||
} | ||
} | ||
|
||
HostNameGetter::~HostNameGetter() { WSACleanup(); } | ||
#endif | ||
|
||
string HostNameGetter::getHostName() const { | ||
string hostname(4U, '\0'); | ||
static constexpr std::size_t kMaxHostNameSize = 1024; | ||
std::size_t nullTerminatedCharPos = 0; | ||
do { | ||
auto errorCode = ::gethostname(hostname.data(), hostname.size() - 1U); | ||
if (errorCode != 0) { | ||
#ifdef CCT_MSVC | ||
// In Windows, too small buffer returns an error WSAEFAULT | ||
std::cout << "errorCode = " << errorCode << std::endl; | ||
std::cerr << "WSAGetLastError() = " << WSAGetLastError() << std::endl; | ||
if (WSAGetLastError() == WSAEFAULT) { | ||
#else | ||
// In Posix, too small buffer returns an error ENAMETOOLONG set in errno | ||
if (errno == ENAMETOOLONG) { | ||
#endif | ||
nullTerminatedCharPos = hostname.size() - 1U; | ||
hostname.resize(2 * hostname.size(), '\0'); | ||
continue; | ||
} | ||
throw exception("Error {} in gethostname", errorCode); | ||
} | ||
#ifndef CCT_MSVC | ||
if (hostname.back() != '\0') { | ||
// In POSIX, if the null-terminated hostname is too large to fit, then the name is truncated, and no error is | ||
// returned, meaning that last char has necessarily been written to | ||
nullTerminatedCharPos = hostname.size() - 1U; | ||
hostname.resize(2 * hostname.size(), '\0'); | ||
continue; | ||
} | ||
#endif | ||
if (hostname.size() > kMaxHostNameSize) { | ||
throw exception("Unexpected host name size length {}", hostname.size()); | ||
} | ||
break; | ||
} while (true); | ||
|
||
auto hostnameSize = hostname.find('\0', nullTerminatedCharPos); | ||
if (hostnameSize == string::npos) { | ||
throw exception("Unexpected error in GetHostName algorithm"); | ||
} | ||
hostname.resize(hostnameSize); | ||
return hostname; | ||
} | ||
} // namespace cct |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#include "gethostname.hpp" | ||
|
||
#include <gtest/gtest.h> | ||
|
||
namespace cct { | ||
TEST(GetHostNameTest, Default) { | ||
HostNameGetter hostNameGetter; | ||
auto currentHostName = hostNameGetter.getHostName(); | ||
|
||
EXPECT_FALSE(currentHostName.empty()); | ||
EXPECT_EQ(currentHostName.find('\0'), string::npos); | ||
} | ||
} // namespace cct |