Skip to content

Commit

Permalink
Merge pull request #68 from Doekin/master
Browse files Browse the repository at this point in the history
ParseSocketAddress 使用 uv_getaddrinfo 对 host 进行DNS 解析
  • Loading branch information
CppCXY authored Jun 18, 2024
2 parents 6f8bf46 + 3cfb9a9 commit ac5aa98
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions emmy_debugger/src/transporter/transporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,21 +245,18 @@ int Transporter::Stop()
return 0;
}

bool Transporter::ParseSocketAddress(const std::string &host, int port, sockaddr_storage *addr, std::string &err)
bool Transporter::ParseSocketAddress(const std::string &host, int port, sockaddr_storage *addr, std::string &err)
{
const int ip6_addr_res = uv_ip6_addr(host.c_str(), port, reinterpret_cast<sockaddr_in6 *>(addr));// 尝试使用IPv6地址
if (ip6_addr_res == 0) {
return true;
auto const loop = uv_default_loop();
uv_getaddrinfo_t resolver;
int res = uv_getaddrinfo(loop, &resolver, nullptr, host.c_str(),
std::to_string(port).c_str(), nullptr);
if (res != 0) {
err = "Invalid host: ";
err += uv_strerror(res);
return false;
}

const int ip4_addr_res = uv_ip4_addr(host.c_str(), port, reinterpret_cast<sockaddr_in *>(addr));// 如果失败,改用IPv4地址
if (ip4_addr_res == 0) {
return true;
}

err = "Invalid host. IPv6 error: ";
err += uv_strerror(ip6_addr_res);
err += ". IPv4 error: ";
err += uv_strerror(ip4_addr_res);
return false;
memcpy(addr, resolver.addrinfo->ai_addr, resolver.addrinfo->ai_addrlen);
uv_freeaddrinfo(resolver.addrinfo);
return true;
}

0 comments on commit ac5aa98

Please sign in to comment.