networking - Arduino本地DNS问题

标签 networking dns arduino

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

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

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

这是我正在使用的:

这是我的测试草图:

#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 地址不正确):

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

以下是连接到同一网络的 Linux 计算机给出的正确信息:

$ 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

发生什么事了?

最佳答案

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

作为 DNS 库一部分的 inet_aton 存在缺陷:

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

要找出它测试字符串中每个字符的数字:

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

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

应该是:

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

您需要在 Dns.cpp 中更改此设置。

关于networking - Arduino本地DNS问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18039895/

相关文章:

c# - 通过C++或C#和Windows API创建Wi Fi网络

networking - 复制,验证,然后从网络位置删除文件/子项

nginx - 使用Skydns的Kubernetes的Nginx解析器

dns - 将名称服务器添加到 Kubernetes

c++ - 无参数构造函数

c++ - Qt和Arduino串行通信(读写)

c++ - 客户端/服务器设置仅在同一台计算机上有效

docker - Docker TCP与主机端口

amazon-ec2 - 如何将我的 DreamHost DNS 注册网站指向我的 EC2 实例?

c - 如何使用结构成员动态分配成员数组?