fix(HostEntry): DNS HostEntry returns multiple entries #3303

This commit is contained in:
Alex Fabijanic 2021-06-09 07:12:55 +02:00
parent 96a645e95b
commit 11bb74a847
3 changed files with 25 additions and 2 deletions

View File

@ -76,6 +76,14 @@ public:
/// for the host.
private:
template <typename C>
void removeDuplicates(C& list)
{
std::sort(list.begin(), list.end());
auto last = std::unique(list.begin(), list.end());
list.erase(last, list.end());
}
std::string _name;
AliasList _aliases;
AddressList _addresses;

View File

@ -25,11 +25,11 @@ HostEntry::HostEntry()
{
}
HostEntry::HostEntry(struct hostent* entry)
{
poco_check_ptr (entry);
_name = entry->h_name;
char** alias = entry->h_aliases;
if (alias)
@ -40,6 +40,8 @@ HostEntry::HostEntry(struct hostent* entry)
++alias;
}
}
removeDuplicates(_aliases);
char** address = entry->h_addr_list;
if (address)
{
@ -49,6 +51,7 @@ HostEntry::HostEntry(struct hostent* entry)
++address;
}
}
removeDuplicates(_addresses);
}
@ -80,6 +83,7 @@ HostEntry::HostEntry(struct addrinfo* ainfo)
}
}
}
removeDuplicates(_addresses);
}

View File

@ -88,6 +88,17 @@ void DNSTest::testHostByAddress()
void DNSTest::testResolve()
{
HostEntry he1 = DNS::hostByName("localhost");
auto a = he1.addresses();
sort(a.begin(), a.end());
auto itA = std::unique(a.begin(), a.end());
assertTrue (itA == a.end());
auto b = he1.aliases();
sort(b.begin(), b.end());
auto itB = std::unique(b.begin(), b.end());
assertTrue (itB == b.end());
}