!1553 fix bind localhost

Merge pull request !1553 from maosiping/master
This commit is contained in:
openharmony_ci 2024-09-12 04:51:46 +00:00 committed by Gitee
commit 96a456a65f
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F

View File

@ -17,6 +17,7 @@
#include "net_address.h"
#include "netstack_log.h"
#include "socket_exec_common.h"
#include "securec.h"
namespace OHOS::NetStack::Socket {
@ -32,6 +33,14 @@ void NetAddress::SetIpAddress(const std::string &address)
if (address.empty()) {
return;
}
if (address == "localhost") {
if (family_ == Family::IPv4) {
address_ = ConvertAddressToIp(address, AF_INET);
} else if (family_ == Family::IPv6) {
address_ = ConvertAddressToIp(address, AF_INET6);
}
return;
}
if (family_ == Family::IPv4) {
in6_addr ipv6{};
if (inet_pton(AF_INET6, address.c_str(), &ipv6) > 0) {