Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DNS lookup error -5 #4949

Closed
6 tasks done
jsdevel opened this issue Jul 22, 2018 · 12 comments
Closed
6 tasks done

DNS lookup error -5 #4949

jsdevel opened this issue Jul 22, 2018 · 12 comments
Labels
waiting for feedback Waiting on additional info. If it's not received, the issue may be closed.

Comments

@jsdevel
Copy link
Contributor

jsdevel commented Jul 22, 2018

Basic Infos

  • This issue complies with the issue POLICY doc.
  • I have read the documentation at readthedocs and the issue is not addressed there.
  • I have tested that the issue is present in current master branch (aka latest git).
  • I have searched the issue tracker for a similar issue.
  • If there is a stack dump, I have decoded it.
  • I have filled out all fields below.

Platform

  • Hardware: ESP-12 (NodeMCU 1.0)
  • Core Version: bde83e8
  • Development Env: Arduino IDE
  • Operating System: Fedora 27

Settings in IDE

  • Module: NodeMCU (ESP-12E)
  • Flash Mode: Not sure
  • Flash Size: 4MB/1MB
  • lwip Variant: v2 Lower Memory
  • Reset Method: Not sure
  • Flash Frequency: Not sure
  • CPU Frequency: 80Mhz
  • Upload Using: SERIAL
  • Upload Speed: 921600

Problem Description

DNS doesn't consistently resolve when the ESP is connected to my NETGEAR R6300 router. I have no idea why. I've tried wiping flash a few times, all to no avail. I feel like DNS used to resolve hostnames on this network just fine, but now it's not.

I have 3 different Access Points that I test my sketch with:

  • NETGEAR R6300 (SSID NETGEAR61)
  • NETGEAR R7000 (SSID NETGEAR-SP)
  • Nexus 6p Hotspot (SSID AndroidAP)

DNS works on all but the first.

Successful Sketch Run

Connecting.......                                                                    
Connected, IP address: 192.168.5.14                                                  
DNS #1, #2 IP: 192.168.5.1, 0.0.0.0                                                  
yahoo.com IP: 98.137.246.7

Un-successful Sketch Run

Connecting.                                                                          
Connected, IP address: 192.168.4.13                                                  
DNS #1, #2 IP: 192.168.4.1, 0.0.0.0                                                  
DNS lookup failed.  Rebooting... 

MCVE Sketch

#include <ESP8266WiFi.h>

void setup()
{
  Serial.begin(115200);
  Serial.println();
  connectToWifi("NETGEAR61", "XXXXXXXX");
  // connectToWifi("AndroidAP", "XXXXXXXX");
  // connectToWifi("NETGEAR-SP", "XXXXXXXX");
  printDNSServers();
  printIPAddressOfHost("yahoo.com");
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(500);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(500);                       // wait for a second
}

void connectToWifi(const char* ssid, const char* password) {
  WiFi.begin(ssid, password);

  Serial.print("Connecting");
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println();

  Serial.print("Connected, IP address: ");
  Serial.println(WiFi.localIP());
}

void printDNSServers() {
  Serial.print("DNS #1, #2 IP: ");
  WiFi.dnsIP().printTo(Serial);
  Serial.print(", ");
  WiFi.dnsIP(1).printTo(Serial);
  Serial.println();
}

void printIPAddressOfHost(const char* host) {
  IPAddress resolvedIP;
  if (!WiFi.hostByName(host, resolvedIP)) {
    Serial.println("DNS lookup failed.  Rebooting...");
    Serial.flush();
    ESP.reset();
  }
  Serial.print(host);
  Serial.print(" IP: ");
  Serial.println(resolvedIP);
}

Debug Messages

connected with NETGEAR61, channel 11
dhcp client start...
wifi evt: 0
.ip:192.168.4.13,mask:255.255.255.0,gw:192.168.4.1
wifi evt: 3
.
Connected, IP address: 192.168.4.13
DNS #1, #2 IP: 192.168.4.1, 0.0.0.0
[hostByName] request IP for: yahoo.com
[hostByName] Host: yahoo.com lookup error: -5!
DNS lookup failed.  Rebooting...
@Eszartek
Copy link

Clearly you have multiple dhcpd servers on your network, ie: each AP has dhcpd enabled, and you have them set to give out the connected AP's ip address as the DNS server. If the AP does not have a functioning DNS forwarder enabled, or its network/netmask/gateway settings are incorrect, the AP will not forward DNS request out to the internet to be resolved by a real DNS server.

Only one AP or router should have dhcpd enabled (hint: the one that is working).

@jsdevel
Copy link
Contributor Author

jsdevel commented Jul 23, 2018

@Eszartek I need to clarify that these are separate networks i.e. NETGEAR61 (R6300 Model Router) is setup in an entirely different location from NETGEAR-SP (R7000 Model Router).

Here are the internet settings I'm seeing in the R6300 Router:

screen shot 2018-07-23 at 8 23 05 am

I'll upload a screenshot of the settings for the R7000 router in a bit.

Also, for R6300 from my laptop, I'm seeing 192.168.4.1 as the DNS server. DNS lookups on the ESP8266 do not work on this network. Other stations (I.E. laptops, tvs etc.) can access DNS just fine.

@jsdevel
Copy link
Contributor Author

jsdevel commented Jul 23, 2018

Here are the settings for the R7000 router:

screen shot 2018-07-23 at 9 41 20 am

For R7000 from my laptop, I'm seeing 192.168.5.1 as the DNS server. DNS lookups on the ESP8266 work on this network.

@Eszartek
Copy link

Eszartek commented Jul 23, 2018

I can't go into detailed troubleshooting of your equipment, but the fact that your routers are giving 192.168.0.x DNS addresses indicates the problem is in the configuration of your router. The ESP device is not the problem. Look for a setting buried in the menus worded something like "Intercept port 53 DNS" and disable it.

@jsdevel
Copy link
Contributor Author

jsdevel commented Jul 23, 2018

@Eszartek I'm sorry, but I'm not seeing where my routers are returning 192.168.0.x DNS addresses. They both are returning the IP of the router and 0.0.0.0. This works just fine on the 192.168.5.0/24 network but not on the 192.168.4.0/24 network. Also, I don't see a setting anywhere in either router for intercept port 53 DNS.

Just looking through the source, I found where 192.168.4.1 is actually the default IP for mesh networks: https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFiMesh/src/ESP8266WiFiMesh.cpp#L29. Is it possible there's a conflict? I may try changing my 192.168.4.0/24 subnet to 192.168.6.0/24 later on to see if that's the issue.

@Eszartek
Copy link

Eszartek commented Jul 23, 2018

The 192.168.0.x is in here: "DNS #1, #2 IP: 192.168.4.1, 0.0.0.0"

If you have the ESP's AP mode enabled, then yes, you could have a conflict, as it won't connect to your router to perform the DNS lookups, the internal AP of the ESP will try to respond and fail.

@devyte
Copy link
Collaborator

devyte commented Jul 24, 2018

It is possible to set 2 dns servers for the STA interface. If no server is specified, the first is set to the gateway (I think), and the second is left unset, i.e.: 0.0.0.0.

Several things here:

  1. why is the r7000 on a different network? Try all combinations: the r6300 on 192.168.4.1, then on 192.168.5.1, and the r7000 on 192.168.4.1, then on 192.168.5.1. Post the debug messages like in your original post.
  2. 192.168.4.1 is the default IP for the AP interface. If your AP interface is somehow enabled (even if you didn't enable it in the sketch), then it's possible that there is some IP conflict. You should explicitly turn set STA mode and/or turn the AP off.
  3. Given that this issue is reported as specific to your hardware, you will have to do the investigation (including testing and possibly wireshark dumps), code tweaks, and solution. Some guidance could maybe be provided by e.g.: @d-a-v , but the effort is solely yours, unless you come up with something that can be reproduced on other hardware. If you're not willing to do this, please close the issue.

@devyte devyte added the waiting for feedback Waiting on additional info. If it's not received, the issue may be closed. label Jul 24, 2018
@jsdevel
Copy link
Contributor Author

jsdevel commented Jul 24, 2018

@devyte thanks for the reply. The r7000 was at a different location. I was testing my board on several different networks when I encountered this issue.

SUCCESS: so, I changed the network to 192.168.4.1/24 on the r7000 router and was able to reproduce the issue. Then I logged the WiFi mode to Serial and found that STA + softAP was indeed enabled. Adding WiFi.mode(WIFI_STA) "fixed" it and DNS was able to resolve hostnames.

I kind of feel this should be more readily present in the documentation for the Station Class#begin section. The only place I found mention of this in the online docs was in the Soft Access Point Class#softAP section. Thoughts if I follow up with a Pull Request to add this?

@devyte
Copy link
Collaborator

devyte commented Jul 26, 2018

Go ahead with a PR.

jsdevel added a commit to jsdevel/Arduino that referenced this issue Jul 26, 2018
* Also fixing formatting of unrelated link in station class docs.
@devyte devyte closed this as completed in 2ee744e Jul 29, 2018
@tablatronix
Copy link
Contributor

Wait, so why does dns lookups fail when in sta+ap mode? simply documenting doesn't really address this.
There are no references to espressif idf etc.. or regression testing.

@devyte
Copy link
Collaborator

devyte commented Aug 7, 2018

The ESP want to do a dns lookup. To do that, it sends a query to the DNS server, which is 192.168.4.1.
In sta+ap mode, that IP address belongs to the AP side, so lwip prioritizes sending it to that interface, and of course the lookup fails.
In sta mode, the address is found to be in the subnet of the sta interface, so the query is sent out of that interface, and it finds its way to the correct dns server.

In contrast, when the dns server was in different network 192.168.5.1, lwip immediately realized that the address wasn't local, so it sends it out the sta interface, despite being in sta+ap mode.

This is pretty standard, and is the way even laptops and cellphones work. It's not specific to the ESP.

@tablatronix
Copy link
Contributor

oh ok, that does make sense, I guess I was thinking that dns lookups would not do loopbacks for some reason.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
waiting for feedback Waiting on additional info. If it's not received, the issue may be closed.
Projects
None yet
Development

No branches or pull requests

4 participants