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

SocketWrapper - use DNS of specific netif, not global #849

Merged
merged 1 commit into from
Apr 19, 2024

Conversation

JAndrassy
Copy link
Contributor

@JAndrassy JAndrassy commented Mar 5, 2024

DNS IP address assigned by DHCP was not used.
Instead DNS servers hardcoded in Mbed were used.

error report https://forum.arduino.cc/t/ethernet-dhcp-issue/1231893

DNS IP address assigned by DHCP was not used.
Instead DNS serves hardcoded in Mbed were used.
@pennam
Copy link
Contributor

pennam commented Mar 6, 2024

@manchoz

@manchoz
Copy link
Contributor

manchoz commented Mar 6, 2024

Uh, thanks. I will check it ASAP.

@JAndrassy
Copy link
Contributor Author

JAndrassy commented Mar 6, 2024

@manchoz it is the same thing as in your PR #809 but here to really use the DNS servers we set and get

@manchoz
Copy link
Contributor

manchoz commented Mar 6, 2024

@JAndrassy Did you try it?

Currently, you need to use the following code to apply the DNS received from the DHCP server to the network stack.

void onNetworkConnect()
{
  Serial.println(">>>> CONNECTED to network");
  Serial.println("Setting Ntp Time");
  setNtpTime(); // Everytime the network is connected, we sync the system time
  Serial.print("Assigned IP:");
  Serial.println(Ethernet.localIP());

/* It requires core patch from #809 
  auto dnsAddress = Ethernet.dnsServerIP();
  Serial.println(dnsAddress);

  auto primaryDnsAddress = Ethernet.dnsIP(0);
  Serial.println(primaryDnsAddress);
  auto secondaryDnsAddress = Ethernet.dnsIP(1);
  Serial.println(secondaryDnsAddress);
*/

  auto net = Ethernet.getNetwork();

  char name[5] {};
  net->get_interface_name(name);
  Serial.print("IF name: ");
  Serial.println(name);

  SocketAddress primaryDNS;
  SocketAddress secondaryDNS;

  net->get_dns_server(0, &primaryDNS, name);
  if (primaryDNS) {
    Serial.print("Primary DNS Server: ");
    Serial.println(ipAddressFromSocketAddress(primaryDNS));
    Serial.println("Setting Primary DNS Server...");
    net->add_dns_server(primaryDNS, nullptr);
  }

  net->get_dns_server(1, &secondaryDNS, name);
  if (secondaryDNS) {
    Serial.print("Secondary DNS Server: ");
    Serial.println(ipAddressFromSocketAddress(secondaryDNS));
    Serial.println("Setting Secondary DNS Server...");
    net->add_dns_server(secondaryDNS, nullptr);
  }
}
  

IPAddress ipAddressFromSocketAddress(SocketAddress socketAddress) {
  nsapi_addr_t address = socketAddress.get_addr();
  return IPAddress(address.bytes[0], address.bytes[1], address.bytes[2], address.bytes[3]);
}

The patch that we would need should be able to do this stuff when we receive the DNS configuration from the DHCP server, otherwise we will end using https://github.com/manchoz/mbed-os/blob/f6d6cdfcefc77d0bf333cf60812a1d0b7282a5f5/connectivity/netsocket/source/nsapi_dns.cpp#L116.

@JAndrassy
Copy link
Contributor Author

JAndrassy commented Mar 6, 2024

I tried it with WiFi. before and after the fix. I didn't try Ethernet.
in WiFi.begin wifi_if->set_dhcp the DNS servers are set to the specific interface. that was the reason for your PR #809 too.
it should work the same in Ethernet.begin.

you wan to copy the DNS servers specific for the netif to global DNS servers list?

EDIT: @manchoz sorry, maybe you missed that this is a PR, not an issue?

@papakpmartin
Copy link

Is this still broken in Release 4.1.1? I'm having a heckuva time on an Opta with very simple stuff that Just Works™ on ESP32. I'm attaching a simple case of what I think should be working fine but isn't. This code does print the correct DNS server address (so #809 is fixed) but it can't find the MQTT broker (which can by found by other devices on the same network by the same hostname using identical code).

broken-dns-example.txt

@markwestm
Copy link

markwestm commented Apr 2, 2024

I am also having DNS issues running 4.1.1 on the Opta. Specifically, I cannot get a local DNS via DHCP. The IP's I get are shown below. My code file is attached below. I even updated the four files associated with this PR manually in the packages folder. Same issue. I also tried the onNetworkConnect() function recommended by @manchoz as well as the work-around in https://forum.arduino.cc/t/ethernet-dhcp-issue/1231893.

ETH: Local IP: 192.168.0.179
ETH: DNS Server: 68.105.28.11

dns-code.txt

@markwestm
Copy link

This is an update to my comment from two days ago. I discovered that setting the DNS server IP is not possible via a DHCP ethernet connection. I found a work-around to my problem by doing the following.

  1. Get and store the device IP address and gateway IP address from the original DHCP ethernet connection.
  2. Terminate the DHCP ethernet connection.
  3. Establish a new non-DHCP ethernet connection by passing the MAC address, device IP, and gateway IP to the ethernet begin function.
  4. Set the DNS server IP address to the gateway IP address retrieved in step 1.

Regards, Mark

@markwestm
Copy link

@JAndrassy Unfortunately, that did not work.

@JAndrassy
Copy link
Contributor Author

@manchoz which way?

  1. write and read DNS server IP with netif name parameter everywhere?
  2. copy DHCP set DNS IP to global DNS server IPs?

your PR #809 would indicate 1) and the Arduino API DNS functions are methods of the specific network interface so it is simple to manage them that way.

with 2) there are a few problems:

  • there are limited number of DNS servers in the global registry (2?)
  • the DNS server IP management from the sketch with Arduino function would not be possible, because WiFi and Ethernet would overwrite each others records if used at the same time

@JAndrassy
Copy link
Contributor Author

@markwestm I think the changes from this PR solve your problem but for some reason they where not applied to your test

@markwestm
Copy link

@JAndrassy Is there any information (code snippets, serial monitor output, etc.) I can provide to help determine if these changes were applied to my test. Thanks for your support.

Regards, Mark

@JAndrassy
Copy link
Contributor Author

s there any information (code snippets, serial monitor output, etc.) I can provide to help determine if these changes were applied to my test.

without these changes you can see in Wireshark that it uses 8.8.8.8 for a DNS request. With the changes it uses the DHCP assigned DNS server IP

@papakpmartin
Copy link

@JAndrassy I can confirm that the changes in this PR are working. Thanks so much!

@pennam and @manchoz, it would be awesome if this could get merged in... thanks! :)

@pennam pennam merged commit ffe6b03 into arduino:main Apr 19, 2024
11 checks passed
@JAndrassy JAndrassy deleted the lwip_dns_fix branch April 19, 2024 07:43
@markwestm
Copy link

markwestm commented Apr 19, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants