Skip to content

Commit c8bf61a

Browse files
committed
Fix gethostbyname
1 parent b63b7ff commit c8bf61a

3 files changed

Lines changed: 33 additions & 9 deletions

File tree

inc/finalmq/streamconnection/AddressHelpers.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <string>
2929

3030
struct sockaddr;
31+
struct in_addr;
3132

3233
namespace finalmq {
3334

@@ -40,6 +41,7 @@ class SYMBOLEXP AddressHelpers
4041
static ConnectionData endpoint2ConnectionData(const std::string& endpoint);
4142
static void addr2peer(sockaddr* addr, ConnectionData& connectionData);
4243
static std::string makeSocketAddress(const std::string& hostname, int port, int af, bool asyncGetHostByName, bool& doAsyncGetHostByName);
44+
static bool getHostByName(const std::string& hostname, struct in_addr& ipAddress);
4345
};
4446

4547
} // namespace finalmq

src/streamconnection/AddressHelpers.cpp

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
#include <sys/un.h>
3636
#endif
3737

38+
#if defined(WIN32)
39+
#include <ws2tcpip.h>
40+
#endif
41+
3842
#ifdef __QNX__
3943
#include <netinet/in.h>
4044
#include <sys/socket.h>
@@ -144,6 +148,25 @@ ConnectionData AddressHelpers::endpoint2ConnectionData(const std::string& endpoi
144148
return connectionData;
145149
}
146150

151+
bool AddressHelpers::getHostByName(const std::string& hostname, struct in_addr& ipAddress)
152+
{
153+
struct addrinfo hints;
154+
struct addrinfo* result = nullptr;
155+
156+
std::memset(&hints, 0, sizeof(hints));
157+
hints.ai_family = AF_INET;
158+
hints.ai_socktype = SOCK_STREAM;
159+
const int status = getaddrinfo(hostname.c_str(), nullptr, &hints, &result);
160+
if (status == 0 && result)
161+
{
162+
struct sockaddr_in* ipv4 = reinterpret_cast<struct sockaddr_in*>(result->ai_addr);
163+
ipAddress.s_addr = ipv4->sin_addr.s_addr;
164+
return true;
165+
}
166+
return false;
167+
}
168+
169+
147170
std::string AddressHelpers::makeSocketAddress(const std::string& hostname, int port, int af, bool asyncGetHostByName, bool& doAsyncGetHostByName)
148171
{
149172
const sockaddr* addr = nullptr;
@@ -172,16 +195,17 @@ std::string AddressHelpers::makeSocketAddress(const std::string& hostname, int p
172195
else
173196
{
174197
addrlen = sizeof(addrTcp);
175-
struct hostent* hp = gethostbyname(hname.c_str());
176-
if (hp)
198+
struct in_addr ipAddress;
199+
const bool ok = getHostByName(hname, ipAddress);
200+
if (ok)
177201
{
178202
memset(&addrTcp, 0, addrlen);
179203
#ifdef WIN32
180204
addrTcp.sin_family = static_cast<ADDRESS_FAMILY>(af);
181205
#else
182206
addrTcp.sin_family = static_cast<in_port_t>(af);
183207
#endif
184-
addrTcp.sin_addr.s_addr = reinterpret_cast<struct in_addr*>(hp->h_addr)->s_addr;
208+
addrTcp.sin_addr.s_addr = ipAddress.s_addr;
185209
addrTcp.sin_port = htons(static_cast<std::uint16_t>(port));
186210
addr = reinterpret_cast<sockaddr*>(&addrTcp);
187211
}

src/streamconnection/StreamConnectionContainer.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -384,20 +384,18 @@ bool StreamConnectionContainer::connect(const std::string& endpoint, const IStre
384384
ret = true;
385385
std::string hostname = connectionData.hostname;
386386
m_executorWorker->addAction([this, connectionData, connection, connectionPropertiesToUse]() mutable {
387-
bool ok = false;
388-
struct in_addr addr1;
389-
struct hostent* hp = gethostbyname(connectionData.hostname.c_str());
390-
if (hp)
387+
struct in_addr ipAddress;
388+
bool ok = AddressHelpers::getHostByName(connectionData.hostname, ipAddress);
389+
if (ok)
391390
{
392-
addr1 = *(reinterpret_cast<struct in_addr*>(hp->h_addr));
393391
struct sockaddr_in addrTcp;
394392
memset(&addrTcp, 0, sizeof(sockaddr_in));
395393
#ifdef WIN32
396394
addrTcp.sin_family = static_cast<ADDRESS_FAMILY>(connectionData.af);
397395
#else
398396
addrTcp.sin_family = static_cast<in_port_t>(connectionData.af);
399397
#endif
400-
addrTcp.sin_addr.s_addr = addr1.s_addr;
398+
addrTcp.sin_addr.s_addr = ipAddress.s_addr;
401399
addrTcp.sin_port = htons(static_cast<std::int16_t>(connectionData.port));
402400
connectionData.sockaddr = std::string(reinterpret_cast<const char*>(&addrTcp), sizeof(sockaddr_in));
403401
ok = createSocket(connection, connectionData, connectionPropertiesToUse);

0 commit comments

Comments
 (0)