DHCP协议

DHCP,Dynamic Host Configuration Protocol,动态主机配置协议。采用DHCP的好处在于减少网络管理员和用户的负担。可以减少手动配置IP地址导致的地址冲冲突,以及网关地址或DNS地址错误导致的不能访问网络等问题。

DHCP原理

DHCP服务器有一个IP地址池,当任何启用DHCP的客户机连接到网络时,可从服务器那里租借一个IP地址,不再使用的IP地址自动回收到地址池中,供再次分配使用。

DHCP保证在同一时刻的任何IP地址只能分给一个客户机使用。当DHCP客户机重启时,应配置为相同的IP地址。在DHCP服务器重启情况下,也应当给每个客户机分配相同的IP地址,并且和手动分配的IP地址共存。这要求DHCP服务器对已经分配的IP地址进行保存,并且在客户端不使用时进行回收。

DHCP是一种动态地向网络终端提供配置参数的协议。在终端提出申请后,DHCP服务器可以向终端提供IP地址及子网掩码、网关和DNS服务器地址等参数。

DHCP协议基于UDP协议,客户端的端口号是68,服务器端口号是67。

DHCP在分配IP地址时有两种方法,一种是由DHCP服务器再特定的IP地址中自动选出一个进行分配。另一种方法是针对MAC地址分配一个固定的IP地址,而且这两种方法可以并用。

为了检查所要分配的IP地址以及已经分配了的IP地址是否可用,DHCP服务器或DHCP客户端必须具备以下功能:

DHCP服务器:在分配IP地址前发送ICMP回送请求包,确认没有返回应答。

DHCP客户端:针对从DHCP那里获得到的IP地址发送ARP请求包,确认没有返回应答。

DHCP工作流程

DHCP共分为4个阶段:

 

各阶段报文截图如下:

若DHCP服务器分配的IP已被占用(通过arp判断),client发送decline报文:

协议分析

通过wireshark来获取上述4个阶段报文,过滤方式:

1. 基于端口过滤     udp.port==67
2. 基于协议过滤    bootp
     在wireshark里,bootp就是指的DHCP,因为DHCP是根据bootp改进过来的。如果要写filter的时候,记得用bootp,而不是dhcp。

DHCP应用

dnsmasq是轻量级的DHCP、TFTP和DNS缓存服务器,给小型网络提供DNS和DHCP服务。它的设计目标是轻量级的DNS,并且占用空间小,适用于资源受限的路由器和防火墙,以及智能手机、便携式热点设备等。官网:http://dnsmasq.org/

dnsmasq的配置文件/etc/config/dhcp控制着DNS和DHCP服务选项。默认配置包含一个通用的配置节来指定全局选项,还有一个或多个DHCP来定义动态主机配置服务的网络接口和地址池等。还可以包含多个域名和主机配置,并且提供客户端地址列表来查询。

嵌入式中应用udhcp多,服务器运行命令:

udhcpd -fS /etc/udhcpd.conf &

客户端运行:

udhcpc -R -b -i wlan0

配置文件/etc/udhcpd.conf配置:

# Sample udhcpd configuration file (/etc/udhcpd.conf)

# The start and end of the IP lease block

start        192.168.111.20    #default: 192.168.0.20
end        192.168.111.254    #default: 192.168.0.254


# The interface that udhcpd will use

interface    wlp4s0        #default: eth0


# The maximim number of leases (includes addressesd reserved
# by OFFER's, DECLINE's, and ARP conficts

#max_leases    254        #default: 254


# If remaining is true (default), udhcpd will store the time
# remaining for each lease in the udhcpd leases file. This is
# for embedded systems that cannot keep time between reboots.
# If you set remaining to no, the absolute time that the lease
# expires at will be stored in the dhcpd.leases file.

#remaining    yes        #default: yes


# The time period at which udhcpd will write out a dhcpd.leases
# file. If this is 0, udhcpd will never automatically write a
# lease file. (specified in seconds)

#auto_time    7200        #default: 7200 (2 hours)


# The amount of time that an IP will be reserved (leased) for if a
# DHCP decline message is received (seconds).

#decline_time    3600        #default: 3600 (1 hour)


# The amount of time that an IP will be reserved (leased) for if an
# ARP conflct occurs. (seconds

#conflict_time    3600        #default: 3600 (1 hour)


# How long an offered address is reserved (leased) in seconds

#offer_time    60        #default: 60 (1 minute)

# If a lease to be given is below this value, the full lease time is
# instead used (seconds).

#min_lease    60        #defult: 60


# The location of the leases file

#lease_file    /var/lib/misc/udhcpd.leases    #defualt: /var/lib/misc/udhcpd.leases

# The location of the pid file
#pidfile    /var/run/udhcpd.pid    #default: /var/run/udhcpd.pid

# Everytime udhcpd writes a leases file, the below script will be called.
# Useful for writing the lease file to flash every few hours.

#notify_file                #default: (no script)

#notify_file    dumpleases    # <--- useful for debugging

# The following are bootp specific options, setable by udhcpd.

#siaddr        192.168.0.22        #default: 0.0.0.0

#sname        zorak            #default: (none)

#boot_file    /var/nfs_root        #default: (none)

# The remainer of options are DHCP options and can be specifed with the
# keyword 'opt' or 'option'. If an option can take multiple items, such
# as the dns option, they can be listed on the same line, or multiple
# lines. The only option with a default is 'lease'.

#Examles
opt    dns    114.114.114.114 8.8.8.8 
option    subnet    255.255.255.0
opt    router    192.168.111.11
opt    wins    192.168.111.10
option    dns    129.219.13.81    # appened to above DNS servers for a total of 3
option    domain    local
option    lease    864000        # 10 days of seconds


# Currently supported options, for more info, see options.c
#opt subnet
#opt timezone
#opt router
#opt timesrv
#opt namesrv
#opt dns
#opt logsrv
#opt cookiesrv
#opt lprsrv
#opt bootsize
#opt domain
#opt swapsrv
#opt rootpath
#opt ipttl
#opt mtu
#opt broadcast
#opt wins
#opt lease
#opt ntpsrv
#opt tftp
#opt bootfile
#opt wpad

# Static leases map
#static_lease 00:60:08:11:CE:4E 192.168.0.54
#static_lease 00:60:08:11:CE:3E 192.168.0.44
udhcpd.conf
start        192.168.111.20    #default: 192.168.0.20
end        192.168.111.254    #default: 192.168.0.254

interface    wlp4s0        #default: eth0

opt    dns    114.114.114.114 8.8.8.8 
option    subnet    255.255.255.0
opt    router    192.168.111.11
opt    wins    192.168.111.10
option    dns    129.219.13.81    # appened to above DNS servers for a total of 3
option    domain    local
option    lease    864000        # 10 days of seconds

DNS查询 

域名解析时,电脑首先向 Local DNS发送解析请求;如果 Local DNS缓存过期或无缓存,Local dns 会依次向根 DNS、顶级域 DNS、以及我们的万网权威 DNS 发送解析请求。我们负责的是权威DNS解析,如果Local dns不稳定也有可能导致解析异常。

如果您觉得仍有问题,您请在解析不正常的环境下提供一下下面几个命令的结果:

Windows CMD:

nslookup dns24.hichina.com

nslookup -qt=CNAME live-push.run.com

nslookup -qt=CNAME live-push.run.com dns24.hichina.com

Linux CMD:

dig dns24.hichina.com

dig live-push.run.com CNAME

dig live-push.run.com CNAME @dns24.hichina.com

 

参考:

1. 跟我学TCP/IP系列4 --DNS、ARP、ICMP以及DHCP

2. 智能路由器开发指南

3. 图解TCP/IP

4. Wireshark实战分析之DHCP协议(一)

5. udhcpc和udhcpd移植

6. wifi的linux应用及配置

7. 一文读懂DHCP协议 && DHCPv6协议

8. Linux C以太网管理(2)udhcpc介绍及使用

posted @ 2017-08-06 09:46  yuxi_o  阅读(1827)  评论(0编辑  收藏  举报