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); / 用網(wǎng)關(guān)地址替代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); / 默認(rèn)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);
以上代碼為應(yīng)用部分代碼,設(shè)備是接入到正常網(wǎng)絡(luò)中,網(wǎng)絡(luò)中有DHCP服務(wù)器,可以正常獲取到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
手動(dòng)設(shè)置為靜態(tài)IP時(shí),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
可以很明顯的發(fā)現(xiàn) dns server #0:與設(shè)置的并不相同
修改代碼
.dns_address[0] = "1.1.1.1",
發(fā)現(xiàn)能正確修改,故判斷代碼沒(méi)有問(wèn)題,問(wèn)題肯定是網(wǎng)卡層或者協(xié)議棧有限制
發(fā)現(xiàn)代碼問(wèn)題如下:
代碼位于”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;
}
/ 問(wèn)題代碼就在這里
設(shè)置DNS不能兩個(gè)同時(shí)設(shè)置,需要分兩次設(shè)置
由于測(cè)試環(huán)境是動(dòng)態(tài)獲取IP后設(shè)置為靜態(tài),之前DNS #1 是 新設(shè)置的 DNS #2
這里循環(huán)判斷了歷史的兩個(gè)DNS是否與新的相同,只要其中一個(gè)相同就返回
此處這樣寫(xiě):我猜測(cè)有兩個(gè)原因,1是防止兩個(gè)DNS設(shè)置為一樣了,2是相同DNS不重復(fù)設(shè)置
此問(wèn)題可以說(shuō)是一個(gè)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服務(wù)器
+關(guān)注
關(guān)注
0文章
8瀏覽量
7369 -
DNS
+關(guān)注
關(guān)注
0文章
217瀏覽量
19794 -
RT-Thread
+關(guān)注
關(guān)注
31文章
1272瀏覽量
39919
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論