Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[flash-624] replace gethostbyname by a thread-safe version gethostbyname_r #304

Merged
merged 2 commits into from
Oct 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions dbms/src/Storages/Transaction/PDTiKVClient.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <Storages/Transaction/PDTiKVClient.h>

#include <Common/Exception.h>

#include <netdb.h>

namespace DB
Expand All @@ -24,7 +25,7 @@ std::string getIP(const std::string & address)

// convertAddr converts host name to network address.
// We assume the converted net type is AF_NET.
std::string convertAddr(const std::string & address)
std::string IndexReader::convertAddr(const std::string & address)
{
if (address.size() == 0)
return "";
Expand All @@ -33,8 +34,18 @@ std::string convertAddr(const std::string & address)
return "";
auto host = address.substr(0, idx);
auto port = address.substr(idx + 1);
struct hostent * result = gethostbyname(host.data());

char buf[1024];
struct hostent buff_storage;
struct hostent * result;
int err = 0;
gethostbyname_r(host.data(), &buff_storage, buf, sizeof(buf), &result, &err);
// Suppose we always use IPv4 address.
if (result == nullptr || result->h_addr == nullptr || err != 0)
{
LOG_ERROR(log, "Cannot resolve host name: " << address << " error message is :" << hstrerror(err));
return "";
}
std::string addr;
for (int i = 0; i < 4; i++)
{
Expand Down
2 changes: 2 additions & 0 deletions dbms/src/Storages/Transaction/PDTiKVClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ struct IndexReader : public pingcap::kv::RegionClient
const std::vector<metapb::Peer> & learners,
pingcap::kv::RpcCallPtr<kvrpcpb::ReadIndexRequest>
rpc);

std::string convertAddr(const std::string & address);
};

using IndexReaderPtr = std::shared_ptr<IndexReader>;
Expand Down