本文主要是介绍IMX6开发板设置DHCP功能和验证DNS功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一 udhcpc命令
root@host:/# udhcpc --help
BusyBox v1.29.3 (2022-11-09 15:51:05 CST) multi-call binary.Usage: udhcpc [-fbqRB] [-a[MSEC]] [-t N] [-T SEC] [-A SEC/-n][-i IFACE] [-s PROG] [-p PIDFILE][-oC] [-r IP] [-V VENDOR] [-F NAME] [-x OPT:VAL]... [-O OPT]...-i IFACE Interface to use (default eth0)-s PROG Run PROG at DHCP events (default /usr/share/udhcpc/default.script)-p FILE Create pidfile-B Request broadcast replies-t N Send up to N discover packets (default 3)-T SEC Pause between packets (default 3)-A SEC Wait if lease is not obtained (default 20)-n Exit if lease is not obtained-q Exit after obtaining lease-R Release IP on exit-f Run in foreground-b Background if lease is not obtained-S Log to syslog too-a[MSEC] Validate offered address with ARP ping-r IP Request this IP address-o Don't request any options (unless -O is given)-O OPT Request option OPT from server (cumulative)-x OPT:VAL Include option OPT in sent packets (cumulative)Examples of string, numeric, and hex byte opts:-x hostname:bbox - option 12-x lease:3600 - option 51 (lease time)-x 0x3d:0100BEEFC0FFEE - option 61 (client id)-x 14:'"dumpfile"' - option 14 (shell-quoted)-F NAME Ask server to update DNS mapping for NAME-V VENDOR Vendor identifier (default 'udhcp VERSION')-C Don't send MAC as client identifier
Signals:USR1 Renew leaseUSR2 Release lease
二 自动获取IP地址
手动设置在IP地址和子网掩码
root@host:~# ifconfig eth1 192.168.250.25 netmask 255.255.0.0
root@host:~# ifconfig eth1
eth1 Link encap:Ethernet HWaddr E0:38:2D:50:00:D4inet addr:192.168.250.25 Bcast:192.168.255.255 Mask:255.255.0.0inet6 addr: fe80::e238:2dff:fe50:d4/64 Scope:LinkUP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1RX packets:12661 errors:0 dropped:0 overruns:0 frame:0TX packets:773 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:1000RX bytes:1310507 (1.2 MiB) TX bytes:66712 (65.1 KiB)root@host:~#
udhcpc -i eth1
root@host:/# udhcpc -i eth1
udhcpc: started, v1.29.3
udhcpc: sending discover
udhcpc: sending select for 192.168.31.68
udhcpc: lease of 192.168.31.68 obtained, lease time 43200
root@host:/#
查询IP地址看看是否有设置成功
root@host:~# ifconfig eth1
eth1 Link encap:Ethernet HWaddr E0:38:2D:50:00:D4inet addr:192.168.250.25 Bcast:192.168.255.255 Mask:255.255.0.0inet6 addr: fe80::e238:2dff:fe50:d4/64 Scope:LinkUP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1RX packets:12661 errors:0 dropped:0 overruns:0 frame:0TX packets:773 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:1000RX bytes:1310507 (1.2 MiB) TX bytes:66712 (65.1 KiB)root@host:~#
没有设置成功,IP地址和子网掩码还是之前通过ifoncifg设置的
三 IP自动获取成功,但是没有设置成功的解决办法
在开发板上创建目录
mkdir /usr/share/udhcpc -p
我使用的buildroot创建的根文件系统,进入buildroot的目录
cd /big/imx/buildroot/buildroot-2022.11.24/output/build/busybox-1.29.3/examples/udhcp
如下所示:将simple.script复制到开发板的目录/usr/share/udhcpc中,更名为default.script
$ ls
sample.bound sample.deconfig sample.nak sample.renew sample.script simple.script udhcpd.conf
#!/bin/sh
# udhcpc script edited by Tim Riker <Tim@Rikers.org>RESOLV_CONF="/etc/resolv.conf"[ -n "$1" ] || { echo "Error: should be called from udhcpc"; exit 1; }NETMASK=""
[ -n "$subnet" ] && NETMASK="netmask $subnet"
BROADCAST="broadcast +"
[ -n "$broadcast" ] && BROADCAST="broadcast $broadcast"case "$1" indeconfig)echo "Setting IP address 0.0.0.0 on $interface"ifconfig $interface 0.0.0.0;;renew|bound)echo "Setting IP address $ip on $interface"ifconfig $interface $ip $NETMASK $BROADCASTif [ -n "$router" ] ; thenecho "Deleting routers"while route del default gw 0.0.0.0 dev $interface ; do:donemetric=0for i in $router ; doecho "Adding router $i"if [ "$subnet" = "255.255.255.255" ]; then# special case for /32 subnets:# /32 instructs kernel to always use routing for all outgoing packets# (they can never be sent to local subnet - there is no local subnet for /32).# Used in datacenters, avoids the need for private ip-addresses between two hops.ip route add $i dev $interfacefiroute add default gw $i dev $interface metric $((metric++))donefiecho "Recreating $RESOLV_CONF"# If the file is a symlink somewhere (like /etc/resolv.conf# pointing to /run/resolv.conf), make sure things work.realconf=$(readlink -f "$RESOLV_CONF" 2>/dev/null || echo "$RESOLV_CONF")tmpfile="$realconf-$$"> "$tmpfile"[ -n "$domain" ] && echo "search $domain" >> "$tmpfile"for i in $dns ; doecho " Adding DNS server $i"echo "nameserver $i" >> "$tmpfile"donemv "$tmpfile" "$realconf";;
esacexit 0
设置后如下所示:
root@host:/usr/share/udhcpc# ls
default.script
root@host:/usr/share/udhcpc#
重新执行命令udhcpc -i eth1
# udhcpc -i eth1
udhcpc: started, v1.29.3
Setting IP address 0.0.0.0 on eth1
udhcpc: sending discover
udhcpc: sending select for 192.168.31.68
udhcpc: sending select for 192.168.31.68
udhcpc: lease of 192.168.31.68 obtained, lease time 43200
Setting IP address 192.168.31.68 on eth1
Deleting routers
route: SIOCDELRT: No such process
Adding router 192.168.31.1
Recreating /etc/resolv.confAdding DNS server 192.168.31.1
查看IP地址:
root@host:/usr/share/udhcpc# ifconfig eth1
eth1 Link encap:Ethernet HWaddr E0:38:2D:50:00:D4inet addr:192.168.31.68 Bcast:192.168.31.255 Mask:255.255.255.0inet6 addr: fe80::e238:2dff:fe50:d4/64 Scope:LinkUP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1RX packets:1979 errors:0 dropped:0 overruns:0 frame:0TX packets:272 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:1000RX bytes:340968 (332.9 KiB) TX bytes:32216 (31.4 KiB)root@host:/usr/share/udhcpc#
设置成功。
四 验证DNS是否正常
验证方法,ping www.baidu.com,如下所示。能够正常ping通。
# ping www.baidu.com
PING www.baidu.com (14.119.104.189): 56 data bytes
64 bytes from 14.119.104.189: seq=0 ttl=55 time=15.677 ms
64 bytes from 14.119.104.189: seq=1 ttl=55 time=13.992 ms
64 bytes from 14.119.104.189: seq=2 ttl=55 time=10.784 ms
64 bytes from 14.119.104.189: seq=3 ttl=55 time=21.834 ms
64 bytes from 14.119.104.189: seq=4 ttl=55 time=7.595 ms
64 bytes from 14.119.104.189: seq=5 ttl=55 time=9.207 ms
64 bytes from 14.119.104.189: seq=6 ttl=55 time=7.723 ms
64 bytes from 14.119.104.189: seq=7 ttl=55 time=9.567 ms
64 bytes from 14.119.104.189: seq=8 ttl=55 time=7.793 ms
64 bytes from 14.119.104.189: seq=9 ttl=55 time=7.218 ms
^C
五 编程获取域名对应的IP地址-gethostbyname
gethostbyname()函数的原型如下,它根据主机名获取主机的信息,例如www.sina. com.cn,使用gethostbyname("www.sina.com.cn")可以获得主机的信息。这个函数的参数name是要查询的主机名,通常是DNS的域名。
#include <netdb.h>
extern int h_errno;struct hostent *gethostbyname(const char *name);
gethostbyname()函数的返回值是一个指向结构struct hostent类型变量的指针,当为NULL时,表示发生错误,错误类型可以通过errno获得,错误的类型及含义如下所述。 HOST_NOT_FOUND:查询的主机不可知,即查不到相关主机的信息。 NO_ADDRESS和NO_DATA:请求的名称合法但是没有合适的IP地址。 NO_RECOVERY:域名服务器不响应。 TRY_AGAIN:域名服务器当前出现临时性错误,稍后再试。
struct hostent {char *h_name; /* official name of host */char **h_aliases; /* alias list */int h_addrtype; /* host address type */int h_length; /* length of address */char **h_addr_list; /* list of addresses */
}
结构structure hostent由成员h_name、h_aliases、h_addrtype、h_length和h_addr_list组成。
成员h_name是主机的官方名称,如新浪的www.sina.com.cn。
成员h_aliases是主机的别名,别名可能有多个,所以用一个链表表示,链表的尾部是一个NULL指针。
成员h_addrtype是主机的地址类型,AF_INET表示IPv4的IP地址,AF_INET6表示IPv6的IP地址。
成员h_length是IP地址的长度,对于IPv4来说为4,即4个字节。
成员h_addr_list是主机的IP地址的链表,每个都为h_length长,链表的尾部是一个NULL指针。
测试代码:
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
// extern int h_errno;#define DEBUG_INFO(format, ...) printf("%s:%d -- " format "\n", __func__, __LINE__,##__VA_ARGS__)struct hostent *gethostbyname(const char *name);
#if 0
struct hostent {char *h_name; /* official name of host */char **h_aliases; /* alias list */int h_addrtype; /* host address type */int h_length; /* length of address */char **h_addr_list; /* list of addresses */
}
#endifvoid test_01(){struct hostent * p = gethostbyname("www.baidu.com");if(p == NULL){perror("gethostbyname");exit(1);}DEBUG_INFO("%s",p->h_name);int i = 0;char *p_h_aliases = p->h_aliases[0];while(p_h_aliases != NULL){DEBUG_INFO("%s",p_h_aliases);i++;p_h_aliases = p->h_aliases[i];}// while()DEBUG_INFO("AF_INET = %d,AF_INET6 = %d",AF_INET,AF_INET6);DEBUG_INFO("%s",p->h_addrtype == AF_INET?"IP V4":"other_protocol");DEBUG_INFO("%d",p->h_length);i = 0;char *p_h_addr_list = p->h_addr_list[0];while(p_h_addr_list != NULL){DEBUG_INFO("%03u,%03u,%03u,%03u",(uint8_t)p_h_addr_list[0],(uint8_t)p_h_addr_list[1],(uint8_t)p_h_addr_list[2],(uint8_t)p_h_addr_list[3]);i++;p_h_addr_list = p->h_addr_list[i];}DEBUG_INFO("bye bye");
} int main(int argc, char **argv)
{test_01();return 0;
}
执行结果:
test_01:25 -- www.a.shifen.com
test_01:29 -- www.baidu.com
test_01:35 -- AF_INET = 2,AF_INET6 = 10
test_01:36 -- IP V4
test_01:37 -- 4
test_01:42 -- 014,119,104,189
test_01:42 -- 014,119,104,254
test_01:46 -- bye bye
六 gethostbyaddr函数
gethostbyaddr()函数的原型如下,它通过查询IP地址来获得主机的信息。gethostbyaddr()函数的第1个参数addr在IPv4的情况下指向一个struct in_addr的地址结构,用户需要查询主机的IP地址填入到这个参数中;第2个参数len表示第一个参数所指区域的大小,在IPv4情况下为sizeof(struct in_addr),即32位;第3个参数type指定需要查询主机IP地址的类型,在IPv4的情况下为AF_INET。其返回值和错误代码含义与函数gethostbyname()相同。
#include <netdb.h>
extern int h_errno;struct hostent *gethostbyname(const char *name);#include <sys/socket.h> /* for AF_INET */
struct hostent *gethostbyaddr(const void *addr,socklen_t len, int type);
注意:函数gethostbyname()和gethostbyaddr()是不可重入的函数,由于传出的值为一块静态的内存地址,当另一次查询到来的时候,这块区域会被占用,所以在使用的时候要小心。
测试代码:
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <arpa/inet.h>
// extern int h_errno;#define DEBUG_INFO(format, ...) printf("%s:%d -- " format "\n", __func__, __LINE__,##__VA_ARGS__)struct hostent *gethostbyname(const char *name);
#if 0
struct hostent {char *h_name; /* official name of host */char **h_aliases; /* alias list */int h_addrtype; /* host address type */int h_length; /* length of address */char **h_addr_list; /* list of addresses */
}
#endifvoid test_01(){struct in_addr addr;//const char *ip = "14.119.104.189";const char *ip = "192.168.31.1";inet_pton(AF_INET,ip,&addr);struct hostent * p = gethostbyaddr(&addr,sizeof(struct in_addr),AF_INET);if(p == NULL){perror("gethostbyaddr");exit(1);}DEBUG_INFO("%s",p->h_name);int i = 0;char *p_h_aliases = p->h_aliases[0];while(p_h_aliases != NULL){DEBUG_INFO("%s",p_h_aliases);i++;p_h_aliases = p->h_aliases[i];}// while()DEBUG_INFO("AF_INET = %d,AF_INET6 = %d",AF_INET,AF_INET6);DEBUG_INFO("%s",p->h_addrtype == AF_INET?"IP V4":"other_protocol");DEBUG_INFO("%d",p->h_length);i = 0;char *p_h_addr_list = p->h_addr_list[0];while(p_h_addr_list != NULL){DEBUG_INFO("%03u,%03u,%03u,%03u",(uint8_t)p_h_addr_list[0],(uint8_t)p_h_addr_list[1],(uint8_t)p_h_addr_list[2],(uint8_t)p_h_addr_list[3]);i++;p_h_addr_list = p->h_addr_list[i];}DEBUG_INFO("%s","bye bye");
} int main(int argc, char **argv)
{test_01();return 0;
}
测试结果:
test_01:30 -- XiaoQiang
test_01:40 -- AF_INET = 2,AF_INET6 = 10
test_01:41 -- IP V4
test_01:42 -- 4
test_01:47 -- 192,168,031,001
test_01:51 -- bye bye
目前的gethostbyname()和gethostbyaddr()函数已经不推荐使用,有代替函数出现,可以用于IPv4和IPv6,并且是线程安全的,即getaddrinfo()和getnameinfo()函数
七 getaddrinfo函数
NAMEgetaddrinfo, freeaddrinfo, gai_strerror - network address and service translationSYNOPSIS#include <sys/types.h>#include <sys/socket.h>#include <netdb.h>int getaddrinfo(const char *node, const char *service,const struct addrinfo *hints,struct addrinfo **res);void freeaddrinfo(struct addrinfo *res);
struct addrinfo {int ai_flags;int ai_family;int ai_socktype;int ai_protocol;socklen_t ai_addrlen;struct sockaddr *ai_addr;char *ai_canonname;struct addrinfo *ai_next;};
八 getnameinfo函数
NAMEgetnameinfo - address-to-name translation in protocol-independent mannerSYNOPSIS#include <sys/socket.h>#include <netdb.h>int getnameinfo(const struct sockaddr *sa, socklen_t salen,char *host, socklen_t hostlen,char *serv, socklen_t servlen, int flags)
小结
这篇关于IMX6开发板设置DHCP功能和验证DNS功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!