Arduino local DNS problems

独自空忆成欢 提交于 2019-12-11 09:57:55

问题


I'm trying to build a little project using the Arduino Ethernet library, but I'm having a weird DNS issue:

It cannot resolve any domain name that is local to my network, but it has no problem resolving public domain names.

No other system on my network has problems with these local domain names. It just seems to be the Arduino.

Here's what I'm using:

  • Arduino Uno R3
  • Arduino Ethernet Shield R3
  • Arduino IDE 1.0.3
  • Asus RT-N66U Router (provides the DNS server)

Here's my test sketch:

#include <SPI.h>
#include <Ethernet.h>
#include <Dns.h>
#include <EthernetUdp.h>

byte mac[] = {  0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };

EthernetClient client;

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

  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    while(true);
  }

  delay(1000);
  Serial.println("connecting...");
  DNSClient dnsClient;

  // Router IP address
  byte dnsIp[] = {192, 168, 11, 1};

  dnsClient.begin(dnsIp);

  // Regular DNS names work...
  IPAddress ip1;
  dnsClient.getHostByName("www.google.com", ip1);
  Serial.print("www.google.com: ");
  Serial.println(ip1);

  // However local ones defined by my router do not (but they work fine everywhere else)...
  IPAddress ip2;
  dnsClient.getHostByName("Tycho.localnet", ip2);
  Serial.print("Tycho.localnet: ");
  Serial.println(ip2);
}

void loop() {

}

Here's its output (the second IP address is incorrect):

connecting...
www.google.com: 74.125.227.84
Tycho.localnet: 195.158.0.0

Here's the correct information given from a Linux machine connected to the same network:

$ nslookup www.google.com
Server:         192.168.11.1
Address:        192.168.11.1#53

Non-authoritative answer:
Name:   www.google.com
Address: 74.125.227.80
Name:   www.google.com
Address: 74.125.227.84
Name:   www.google.com
Address: 74.125.227.82
Name:   www.google.com
Address: 74.125.227.83
Name:   www.google.com
Address: 74.125.227.81

$ nslookup Tycho.localnet
Server:         192.168.11.1
Address:        192.168.11.1#53

Name:   Tycho.localnet
Address: 192.168.11.2

What's going on?


回答1:


I don't know if you've already found a solution, but just in case:

There's a defect in inet_aton which is part of the DNS library:

This is supposed to convert a string IP address to the IPAddress type.

To find out it tests each character in the string for numerals:

while (*p &&
       ( (*p == '.') || (*p >= '0') || (*p <= '9') ))

but any alphabetic character matches *p >= '0'

It should be:

while (*p &&
       ( (*p == '.') || ((*p >= '0') && (*p <= '9')) ))

You need to change this in Dns.cpp.




回答2:


Many routers will provide their LAN side IP address as the DNS address -- that is typical behavior for DHCP setups. That does not necessarily mean they are actually the server. Some simply forward the DNS requests to the WAN side servers and return the response. This type of configuration where the router is only a proxy explains your symptoms.

When the router is a proxy, the Arduino DNS requests are just being forwarded to the external DNS server which does not know your local names.

The Linux machine is using other network protocols to discover local names. The same thing happens with Windows machines where local computer names are known. If you run a network monitor like Wireshark, you will see computers regularly announcing their presence to other computers. The Arduino only has simple TCP/IP and does not process these broadcasts.

If the router is truly a server, then you must be configuring a table with the entries of name to IP address mapping. To make this work, you cannot use dynamic addresses because what you type into the table one day would be invalid another. To make a local DNS server work with DHCP you would want to lock each computers IP address, either at the computer side, or at the router by linking specific MAC addresses to specific IP addresses.



来源:https://stackoverflow.com/questions/18039895/arduino-local-dns-problems

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!