esp8266 / Arduino

ESP8266 core for Arduino
GNU Lesser General Public License v2.1
15.89k stars 13.35k forks source link

DNS lookup problem #8780

Open brunoudi opened 1 year ago

brunoudi commented 1 year ago

Basic Infos

Platform

Settings in IDE

Problem Description

I have a separate router and modem. After temporary data loss (modem disconnected, internet reconnection, etc), without WiFi connection loss ESP DNS cant solve any IP addresses.

I have enabled the DEBUG level to WIFI.

Sketch


#include <ESP8266WiFi.h>

const char* ssid     = "my-ssid";
const char* password = "my-password";

void setup() {
  Serial.begin(115200);

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

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

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  IPAddress ipResult;
  int result = WiFi.hostByName("www.google.com", ipResult);
  Serial.print("Result: "); Serial.println(result);
  Serial.print("Ip: "); Serial.println(ipResult);
  Serial.println();
  delay(5000);
}

Turn on the router WITHOUT DATA (modem connection), and start the ESP.

The output, as expected, is this:

[hostByName] request IP for: www.google.com
[hostByName] Host: www.google.com lookup error: -5!
Result: 0
Ip: (IP unset)

But after data reestablishing (connecting the router to the modem), ESP still cannot resolve the IP address.

I know there is a DNS cache (I don't know exactly how long it lasts), but I left it running for 24 hours and the output remained the same.

I tried some actions, like reconnecting to WiFi, but is only solves if I restart the device.

mcspr commented 1 year ago

Why do you think of cache first? You sure networking works at all?

DNS servers are printable

Serial.println(WiFi.dnsIP(0));
Serial.println(WiFi.dnsIP(1));
Serial.println(WiFi.dnsIP(2));

Same for our own IP, netmask and gateway

Serial.println(WiFi.localIP());
Serial.println(WiFi.subnetMask());
Serial.println(WiFi.gatewayIP());

I'd expect to see something different when it works and when it doesn't

brunoudi commented 1 year ago

I'm pretty sure the network starts working fine after I connect the router to modem.

I actually did not thing at DNS cache at first, and I also printed the DNS IP as suggested, but when using dhcp the dns usually is the gateway address. So, the IP didn't change when have or not data connection.

If I turn on the ESP with everything Ok (wifi + data), it solves the IP and print.

After removing the conection from router to modem, it continuous printing the IP for a while because of DNS cache (not exactly same time every test), and only after some minutes it begins to show error.

It looks like that when it tries to solve the IP and is not successfull, the device never again tries to actually solve the IP and only returns the error from some kind of cache.

mcspr commented 1 year ago

True, DNS client stores the record based on TTL. My question was - is device actually connected, does anything but DNS work?

Error code -5 means IN_PROGRESS, our code just times out the request (edit ...and we should probably replace it with TIMEOUT, since it is what happens) https://github.com/esp8266/Arduino/search?q=ERR_INPROGRESS&type=code So at least the request is attempted.

d-a-v commented 1 year ago

Network protocol activity can be traced or debugged with included netdump facility

d-a-v commented 1 year ago

@brunoudi a DNS fix was merged. Can you retry using current master of this core ?

nilo85 commented 4 months ago

Edit2: I can confirm it works now that I fixed my non responding IPv4 DNS... (thank you PS5 for not having smart dns solving skills and giving me a second reason to consider something actually being wrong with network)

I seem to have the same issue.

My device gets ip 192.168.68.XX GW, DNS etc set to 192.168.68.1 My router is a google nest wifi if that makes a difference..

Frustrating =) I tried the both git master and latest stable release with same issue

EDIT: I just found out that my issue seems to be that I f*cked up ipv4 configuration on my first router and no DNS for ipV4 was set properly and google nest dns failed for IPV4. and all my other devices seem to somehow managed to use IPV6 dns to resolve IPv4

d-a-v commented 4 months ago

Can we close this issue ?

nilo85 commented 4 months ago

I’m not the original reporting user and don’t know if you have any rules when to close etc, as I found a valid cause to my issue and original reporting user has not replied within a year, I would feel encouraged to close this one :)