From 3cfb9a9ba728ab333e223dd5ac5a406576cc560f Mon Sep 17 00:00:00 2001 From: Doekin Date: Mon, 17 Jun 2024 20:44:00 +0800 Subject: [PATCH] =?UTF-8?q?ParseSocketAddress=20=E4=BD=BF=E7=94=A8=20uv=5F?= =?UTF-8?q?getaddrinfo=20=E5=AF=B9=20host=20=E8=BF=9B=E8=A1=8CDNS=20?= =?UTF-8?q?=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- emmy_debugger/src/transporter/transporter.cpp | 27 +++++++++---------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/emmy_debugger/src/transporter/transporter.cpp b/emmy_debugger/src/transporter/transporter.cpp index 5aeb28e..4304c8c 100644 --- a/emmy_debugger/src/transporter/transporter.cpp +++ b/emmy_debugger/src/transporter/transporter.cpp @@ -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(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(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; }