diff options
author | Chuck <chuck+github@borboggle.com> | 2021-05-24 20:14:51 +0700 |
---|---|---|
committer | Chuck <chuck+github@borboggle.com> | 2021-06-01 11:24:34 +0700 |
commit | dd8fa11ac182cc5b53746c9148067968d45c5588 (patch) | |
tree | 91354ad635aae874047c7686d44b3008d2858a4c /drivers | |
parent | 635d0c9b449e34267b52a237f7ce21838d90b6ce (diff) |
Support multiple address resolution in DNS requests
Add two new functions to the IP class that returns all addresses/aliases associated with a given address.
This is a cherry-pick merge from 010a3433df43a94fee95474360ffa6662c7441b9 which was merged in 2.1, and has been updated to build with the latest code.
This merge adds two new methods IP.resolve_hostname_addresses and IP.get_resolve_item_addresses that returns a List of all addresses returned from the DNS request.
Diffstat (limited to 'drivers')
-rw-r--r-- | drivers/unix/ip_unix.cpp | 22 | ||||
-rw-r--r-- | drivers/unix/ip_unix.h | 2 |
2 files changed, 17 insertions, 7 deletions
diff --git a/drivers/unix/ip_unix.cpp b/drivers/unix/ip_unix.cpp index 053e3fd9d6..e8f8ae4717 100644 --- a/drivers/unix/ip_unix.cpp +++ b/drivers/unix/ip_unix.cpp @@ -89,7 +89,7 @@ static IPAddress _sockaddr2ip(struct sockaddr *p_addr) { return ip; }; -IPAddress IPUnix::_resolve_hostname(const String &p_hostname, Type p_type) { +void IPUnix::_resolve_hostname(List<IPAddress> &r_addresses, const String &p_hostname, Type p_type) const { struct addrinfo hints; struct addrinfo *result = nullptr; @@ -108,7 +108,7 @@ IPAddress IPUnix::_resolve_hostname(const String &p_hostname, Type p_type) { int s = getaddrinfo(p_hostname.utf8().get_data(), nullptr, &hints, &result); if (s != 0) { ERR_PRINT("getaddrinfo failed! Cannot resolve hostname."); - return IPAddress(); + return; }; if (result == nullptr || result->ai_addr == nullptr) { @@ -116,14 +116,24 @@ IPAddress IPUnix::_resolve_hostname(const String &p_hostname, Type p_type) { if (result) { freeaddrinfo(result); } - return IPAddress(); + return; }; - IPAddress ip = _sockaddr2ip(result->ai_addr); + struct addrinfo *next = result; - freeaddrinfo(result); + do { + if (next->ai_addr == NULL) { + next = next->ai_next; + continue; + } + IPAddress ip = _sockaddr2ip(next->ai_addr); + if (!r_addresses.find(ip)) { + r_addresses.push_back(ip); + } + next = next->ai_next; + } while (next); - return ip; + freeaddrinfo(result); } #if defined(WINDOWS_ENABLED) diff --git a/drivers/unix/ip_unix.h b/drivers/unix/ip_unix.h index e6479be6e5..0d64648b39 100644 --- a/drivers/unix/ip_unix.h +++ b/drivers/unix/ip_unix.h @@ -38,7 +38,7 @@ class IPUnix : public IP { GDCLASS(IPUnix, IP); - virtual IPAddress _resolve_hostname(const String &p_hostname, IP::Type p_type) override; + virtual void _resolve_hostname(List<IPAddress> &r_addresses, const String &p_hostname, Type p_type = TYPE_ANY) const override; static IP *_create_unix(); |