tsIpAddrress staticIp={
.ip_address = "192.168.0.240",
.netmask = "255.255.0.0",
.gw_address = "192.168.1.1",
.dns_address[0] = "8.8.8.8",
.dns_address[1] = "114.114.114.114"};
void setStaticIp(const char *name, tsIpAddrress *ipaddr)
{
struct netdev netdev = RT_NULL;
ip_addr_t addr;
int ret;
netdev = netdev_get_by_name(name);
if (netdev == RT_NULL)
{
rt_kprintf("bad network interface device name(%s).n", name);
return;
}
netdev_dhcp_enabled(netdev,RT_FALSE);
/ set IP address /
if ((ipaddr->ip_address != NULL) && inet_aton(ipaddr->ip_address, &addr))
{
netdev_set_ipaddr(netdev, &addr);
}
/ set netmask address /
if ((ipaddr->netmask != NULL) && inet_aton(ipaddr->netmask, &addr))
{
netdev_set_netmask(netdev, &addr);
}
/ set gateway address /
if ((ipaddr->gw_address != NULL) && inet_aton(ipaddr->gw_address, &addr))
{
netdev_set_gw(netdev, &addr);
}
/ set dns 0 /
if ((ipaddr->dns_address[0] != NULL) && inet_aton(ipaddr->dns_address[0], &addr))
{
rt_kprintf("dns0 aton (%s).n", ipaddr->dns_address[0]);
ret = netdev_set_dns_server(netdev, 0, &addr);
if(ret != RT_EOK)
rt_kprintf("netdev_set_dns_server ERR %d!!!n",ret);
}
else
{
if(ipaddr->dns_address[0] != NULL)
rt_kprintf("dns0 aton err (%s).n", ipaddr->dns_address[0]);
inet_aton(ipaddr->gw_address, &addr); / 用网关地址替代DNS0 /
ret = netdev_set_dns_server(netdev, 0, &addr);
if(ret != RT_EOK)
rt_kprintf("netdev_set_dns_server ERR %d!!!n",ret);
}
/ set dns 1 /
if ((ipaddr->dns_address[1] != NULL) && inet_aton(ipaddr->dns_address[1], &addr))
{
rt_kprintf("dns1 aton (%s).n", ipaddr->dns_address[1]);
ret = netdev_set_dns_server(netdev, 1, &addr);
if(ret != RT_EOK)
rt_kprintf("netdev_set_dns_server ERR %d!!!n",ret);
}
else
{
if(ipaddr->dns_address[1] != NULL)
rt_kprintf("dns1 aton err (%s).n", ipaddr->dns_address[1]);
inet_aton("8.8.8.8", &addr); / 默认DNS1 */
ret = netdev_set_dns_server(netdev, 1, &addr);
if(ret != RT_EOK)
rt_kprintf("netdev_set_dns_server ERR %d!!!n",ret);
}
}
setStaticIp("e0",&staticIp);
以上代码为应用部分代码,设备是接入到正常网络中,网络中有DHCP服务器,可以正常获取到IP和DNS
获取的地址如下:
ip address: 192.168.0.64
gw address: 192.168.0.1
net mask : 255.255.0.0
dns server #0: 192.168.0.1
dns server #1: 8.8.8.8
手动设置为静态IP时,IP如下:
ip address: 192.168.0.240
gw address: 192.168.1.1
net mask : 255.255.0.0
dns server #0: 192.168.0.1
dns server #1: 114.114.114.114
可以很明显的发现 dns server #0:与设置的并不相同
修改代码
.dns_address[0] = "1.1.1.1",
发现能正确修改,故判断代码没有问题,问题肯定是网卡层或者协议栈有限制
发现代码问题如下:
代码位于”netdev下”
void netdev_low_level_set_dns_server(struct netdev *netdev, uint8_t dns_num, const ip_addr_t dns_server)
{
int index;
RT_ASSERT(dns_server);
if (netdev == RT_NULL)
{
return;
}
/ 问题代码就在这里
设置DNS不能两个同时设置,需要分两次设置
由于测试环境是动态获取IP后设置为静态,之前DNS #1 是 新设置的 DNS #2
这里循环判断了历史的两个DNS是否与新的相同,只要其中一个相同就返回
此处这样写:我猜测有两个原因,1是防止两个DNS设置为一样了,2是相同DNS不重复设置
此问题可以说是一个BUG,希望官方能修正
/
/ check DNS servers is exist */
for (index = 0; index < NETDEV_DNS_SERVERS_NUM; index++)
{
if (ip_addr_cmp(&(netdev->dns_servers[index]), dns_server))
{
return;
}
}
if (dns_num < NETDEV_DNS_SERVERS_NUM)
{
ip_addr_copy(netdev->dns_servers[dns_num], dns_server);
/ execute DNS servers address change callback function */
if (netdev->addr_callback)
{
netdev->addr_callback(netdev, NETDEV_CB_ADDR_DNS_SERVER);
}
}
}
-
DHCP服务器
+关注
关注
0文章
8浏览量
7372 -
DNS
+关注
关注
0文章
218浏览量
19828 -
RT-Thread
+关注
关注
31文章
1285浏览量
40076
发布评论请先 登录
相关推荐
评论