Arduino本地DNS问题

Arduino本地DNS问题

本文介绍了Arduino本地DNS问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Arduino以太网库构建一个小项目,但是我遇到了一个奇怪的DNS问题:

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

它无法解析我的网络本地的任何域名,但是解析公共域名没有问题.

我的网络上没有其他系统对这些本地域名有问题.似乎只是Arduino.

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

这是我正在使用的:

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

这是我的测试草图:

#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() {

}

这是它的输出(第二个IP地址不正确):

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

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

以下是从连接到同一网络的Linux机器提供的正确信息:

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

这是怎么回事?

推荐答案

我不知道您是否已经找到解决方案,但以防万一:

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

inet_aton中有一个缺陷,它是DNS库的一部分:

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

这应该将字符串IP地址转换为IPAddress类型.

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') ))

,但任何字母字符均匹配*p >= '0'

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

应该是:

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

您需要在Dns.cpp中进行更改.

这篇关于Arduino本地DNS问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 06:14