|
35 | 35 | #include <sys/un.h> |
36 | 36 | #endif |
37 | 37 |
|
| 38 | +#if defined(WIN32) |
| 39 | +#include <ws2tcpip.h> |
| 40 | +#endif |
| 41 | + |
38 | 42 | #ifdef __QNX__ |
39 | 43 | #include <netinet/in.h> |
40 | 44 | #include <sys/socket.h> |
@@ -144,6 +148,25 @@ ConnectionData AddressHelpers::endpoint2ConnectionData(const std::string& endpoi |
144 | 148 | return connectionData; |
145 | 149 | } |
146 | 150 |
|
| 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 | + |
147 | 170 | std::string AddressHelpers::makeSocketAddress(const std::string& hostname, int port, int af, bool asyncGetHostByName, bool& doAsyncGetHostByName) |
148 | 171 | { |
149 | 172 | const sockaddr* addr = nullptr; |
@@ -172,16 +195,17 @@ std::string AddressHelpers::makeSocketAddress(const std::string& hostname, int p |
172 | 195 | else |
173 | 196 | { |
174 | 197 | 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) |
177 | 201 | { |
178 | 202 | memset(&addrTcp, 0, addrlen); |
179 | 203 | #ifdef WIN32 |
180 | 204 | addrTcp.sin_family = static_cast<ADDRESS_FAMILY>(af); |
181 | 205 | #else |
182 | 206 | addrTcp.sin_family = static_cast<in_port_t>(af); |
183 | 207 | #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; |
185 | 209 | addrTcp.sin_port = htons(static_cast<std::uint16_t>(port)); |
186 | 210 | addr = reinterpret_cast<sockaddr*>(&addrTcp); |
187 | 211 | } |
|
0 commit comments