-
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
101 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,8 @@ | ||
#pragma once | ||
|
||
#include "cct_string.hpp" | ||
|
||
namespace cct { | ||
/// Safe version of gethostname, working in POSIX and Windows with similar behavior. | ||
string GetHostName(); | ||
} // 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,57 @@ | ||
#include "gethostname.hpp" | ||
|
||
#include "cct_config.hpp" | ||
#include "cct_exception.hpp" | ||
#include "cct_string.hpp" | ||
#ifdef CCT_MSVC | ||
#include <Winsock2.h> | ||
#else | ||
#include <errno.h> | ||
#include <unistd.h> | ||
#endif | ||
|
||
namespace cct { | ||
/// Safe version of gethostname, working in POSIX and Windows with similar behavior. | ||
string GetHostName() { | ||
string hostname(16U, '\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 | ||
if (errorCode == 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,12 @@ | ||
#include "gethostname.hpp" | ||
|
||
#include <gtest/gtest.h> | ||
|
||
namespace cct { | ||
TEST(GetHostNameTest, Default) { | ||
auto currentHostName = GetHostName(); | ||
|
||
EXPECT_FALSE(currentHostName.empty()); | ||
EXPECT_EQ(currentHostName.find('\0'), string::npos); | ||
} | ||
} // namespace cct |