DNS resolution hangs for 10 sec on IPV6 networks

@pennam
I'd like to report an apparent bug in the mbed/GIGA UDP and HttpClient classes, suggest a solution and offer the community some simple code for a DNS resolver for the GIGA.

I test my app on two different internet services and observed that on one service the UDP class 100% consistently hangs for just over 10 seconds at Udp.beginPacket. I discovered the issue relates to IPV6 in the DNS responses.

I believe the issue centers around the use of NSAPI_UNSPEC in the call to gethostbyname in SocketHelpers.cpp This says that the desired IPV4 or IPV6 response type is Unspecified. And apparently if IPV6 responses are received then the UDP class hangs for 10 seconds and then times out. Since the code is buried in the mbed classes I extracted the DNS resolution code into my sketch and updated it there.

SocketAddress _host;  // Host to be used to send data, returned from gethostbyname
WiFi.getNetwork()->gethostbyname(host, &_host, NSAPI_IPv4); // NSAPI_UNSPEC, NSAPI_IPv4, NSAPI_IPv6

Udp.beginPacket(_host.get_ip_address(), 123);  // NTP requests are to port 123

host contains the DNS name to be resolved eg "pool.ntp.org" or "time.google.com".

If I use the NSAPI_UNSPEC enum I consistently have the same 10 sec hang
If I use the NSAPI_IPv4 enum the whole enclosing UDP function completes in 100 ms.

So of note, the first two lines of code are an easy to use DNS resolver. They resolve a DNS name to an IPV4 address that's available as a null terminated string via a call to
_host.get_ip_address()
This should work anywhere in a GIGA after the WiFi has been connected.

EDIT: FYI The same problem occurs when using the HttpClient class too. Both of these problems are 100% repeatable on multiple GIGAs on the AT&T Fiber optic network which supports both IPV4 and IPV6.

Good find and solution Joe. I think it's deserving of a git issue.

Hi Steve Thanks...
I don't know how nor where to report bugs on GitHub. A quick seach of the forum didn't turn up anything. And a google search says to report them on the Forum.... so sort of a circular situation. :wink:

Issue #1022 raised. DNS resolution hangs UDP for 10 sec on IPV6 networks · Issue #1022 · arduino/ArduinoCore-mbed · GitHub

1 Like

Hi @steve9 and @JoeHuber thanks! This seems to be related also to this Reduce nsapi dns response timeout: 5 seconds 3 retires by fabik111 · Pull Request #1009 · arduino/ArduinoCore-mbed · GitHub
I will check if your proposal can be integrated!

@pennam

Excellent. Thank you!

I notice that NSAPI_UNSPEC appears at several places in the DNS related code. I think all of them should be investigated and replaced with NSAPI_IPv4 unless there is an intention to support both IPV4 and IPV6. As mentioned elsewhere I encountered this same problem with both the UDP and HttpClient classes. There may be other networking code dependent on either these same or similar DNS calls.