简介:动态主机配置协议(Dynamic Host Configuration Protocol,缩写:DHCP)是 RFC 1541(已被 RFC 2131 取代)定义的标准协议,该协议允许服务器向客户端动态分配 IP 地址和配置信息。
工作原理:
DHCP协议支持C/S(客户端/服务器)结构,主要分为两部分:
1、DHCP客户端:通常为网络中的PC、打印机等终端设备,使用从DHCP服务器分配下来的IP信息,包括IP地址、DNS等。
2、DHCP服务器:所有的IP网络设定信息都由DHCP服务器集中管理,并处理客户端的DHCP请求。
DHCP采用UDP作为传输协议,客户端发送消息到DHCP服务器的的67号端口,服务器返回消息给客户端的68号端口。
1. DHCP Discover(发现)
当客户端设备(如计算机、手机等)首次连接到网络时,它会发送一个DHCP Discover消息。这是一种广播消息,目的是在网络中找到可用的DHCP服务器。
- 消息类型:广播(源IP地址为0.0.0.0,目标IP地址为255.255.255.255)
- 内容:包含客户端的MAC地址等信息。
2. DHCP Offer(提供)
DHCP服务器接收到DHCP Discover消息后,会检查其IP地址池,并选择一个可用的IP地址。然后,DHCP服务器会发送一个DHCP Offer消息给客户端,提供一个IP地址和其他网络配置参数。
- 消息类型:广播或单播(目标IP地址为客户端的MAC地址)
- 内容:包含提供的IP地址、子网掩码、网关、DNS服务器等信息。
3. DHCP Request(请求)
客户端接收到一个或多个DHCP Offer消息后,会选择一个DHCP服务器,并发送一个DHCP Request消息,表示接受该服务器提供的IP地址和配置。
- 消息类型:广播(源IP地址为0.0.0.0,目标IP地址为255.255.255.255)
- 内容:包含所选DHCP服务器的标识和提供的IP地址。
4. DHCP Acknowledgment(确认)
DHCP服务器接收到DHCP Request消息后,会发送一个DHCP Acknowledgment(ACK)消息,确认IP地址分配成功,并提供最终的网络配置参数。
- 消息类型:广播或单播(目标IP地址为客户端的MAC地址)
- 内容:包含确认的IP地址、租约时间、子网掩码、网关、DNS服务器等信息。
5. NAK(拒绝)
如果DHCP服务器收到Request请求报文后,没有发现有相应的租约记录或者由于某些原因无法正常分配IP地址,则向DHCP客户端发送NAK应答报文,通知用户无法分配合适的IP地址。
6. Release(释放)
当DHCP客户端不再需要使用分配IP地址时(一般出现在客户端关机、下线等状况)就会主动向DHCP服务器发送RELEASE请求报文,告知服务器用户不再需要分配IP地址,请求DHCP服务器释放对应的IP地址。
7. Decline(冲突)
DHCP客户端收到DHCP服务器ACK应答报文后,通过地址冲突检测发现服务器分配的地址冲突或者由于其他原因导致不能使用,则会向DHCP服务器发送Decline请求报文,通知服务器所分配的IP地址不可用,以期获得新的IP地址。
8. Inform(详情)
DHCP客户端如果需要从DHCP服务器端获取更为详细的配置信息,则向DHCP服务器发送Inform请求报文;DHCP服务器在收到该报文后,将根据租约进行查找到相应的配置信息后,向DHCP客户端发送ACK应答报文。目前基本上不用了。
9. IP地址租约
客户端接收到DHCP ACK消息后,开始使用分配的IP地址。IP地址的使用期限由租约时间决定,租约时间到期前,客户端需要向DHCP服务器续租。
10. 租约续租
在租约时间到期前,客户端会发送一个DHCP Request消息请求续租。DHCP服务器会根据情况发送一个DHCP ACK消息,延长租约时间。
11. 租约释放
当客户端设备从网络中断开连接或关闭时,可以发送一个DHCP Release消息给DHCP服务器,释放分配的IP地址。
DHCP报文结构:
DHCP报文首部:
- 报文操作类型(op): 1byte,分为请求报文和响应报文,1为请求报文;2为响应报文。具体的报文类型在option字段中标识。
- client硬件地址类型(htype): 1byte,1表示以太网类型。
- 硬件地址长度(hlen): 1byte,以太网的硬件地址长度为6bytes。
- dhcp中继数目(hops): 1byte,表示当前dhcp报文经过的DHCP中继的数目,每经过一个DHCP中继这个字段就加1。
- 随机数(xid): 4bytes,由client端产生的随机数,用于匹配请求和应答报文,就是匹配应答报文是对哪个请求报文做出应答。
- 申请时间(secs): 2bytes,客户端进入IP地址申请进程的时间或者更新IP地址进程的时间;由客户端软件根据情况设定。目前没有使用,固定为0。
- 标志字段(flags): 2bytes,16比特中只使用了最高位比特(即最左边的比特),这个个比特是广播响应标识位,用来标识DHCP服务器发出的响应报文是广播还是单播,0是单播,1是广播。其余的比特位保留不用,都为0。
- 客户端IP(ciaddr): 4bytes,是客户端的IP地址,可以是client自己的IP地址,也可以是server分配给client的IP地址。
- 分配的IP(yiaddr):4bytes,是server分配给client的IP地址。
- 服务器的IP(siaddr): 4bytes,是client端获取IP地址等信息的server端的地址。
- 网关地址(giaddr): 4bytes,是client发出请求报文后经过的第一个中继的IP地址。
- 客户端硬件地址(chaddr): 16bytes,是client端的硬件地址,在client发出报文时会把自己网卡的硬件地址写进这个字段。
- 服务器主机名(sname): 64bytes,是client端获取IP地址等信息的服务器名称。
- 客户端启动配置文件名(file): 128bytes,是服务器为client指定的启动配置文件名及路径信息,由服务器填写。
- 可选变长的选项字段(options): 这个字段包含了终端的初始配置信息和网络配置信息,包括报文类型,有效租期,DNS服务器的IP地址等配置信息。
Option:
- Option 1:用于指定客户端所在子网的子网掩码。
- Option 3:用于指定默认网关的IP地址。
- Option 6:用于指定DNS服务器的IP地址。
- Option 12:用于指定客户端设备的主机名。
- Option 15:用于指定本地域名。
- Option 51:IP地址租约时间
- Option 53:DHCP信息类型
- Option 54:服务器标识
- Option 58:更新时间值(用于指定客户端请求续约租约的时间间隔。)
- Option 59:重新链接时间值(用于指定客户端请求重新绑定租约的时间间隔。)
- Option 60:用于指定客户端设备的类型或厂商信息。
基于CH394的DHCP实现:
- 初始化CH394(ip网关子网掩码都设为0)并创建1个UDP的socket本地端口为68;
- link成功后可通过CH394向DHCP服务器发Discover报文,目的端口为67..
- CH394发送Discover报文:
Dynamic Host Configuration Protocol (Discover)
Message type: Boot Request (1) * 01
Hardware type: Ethernet (0x01) * 01
Hardware address length: 6 * 06
Hops: 0 * 00
Transaction ID: 0xabcd0001 * ab cd 00 01
Seconds elapsed: 0 * 00
Bootp flags: 0x0000 (Unicast) * 00 00
Client IP address: 0.0.0.0 (0.0.0.0) * 00 00 00 00
Your (client) IP address: 0.0.0.0 (0.0.0.0) * 00 00 00 00
Next server IP address: 0.0.0.0 (0.0.0.0) * 00 00 00 00
Relay agent IP address: 0.0.0.0 (0.0.0.0) * 00 00 00 00
Client MAC address: NanjingQinhe_89:00:1a (70:19:88:89:00:1a) * 00 70 19 88 89 00 1a
Client hardware address padding: 00000000000000000000 * 00 00 00 00 00 00 00 00 00 00 Server host name not given * 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Boot file name not given * 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Magic cookie: DHCP * 63 82 53 63
Option: (53) DHCP Message Type (Discover) * 35
Length: 1 * 01
DHCP: Discover (1) * 01
Option: (57) Maximum DHCP Message Size * 39
Length: 2 * 02
Maximum DHCP Message Size: 1500 * 05 dc
Option: (55) Parameter Request List * 37
Length: 4 * 04
Parameter Request List Item: (1) Subnet Mask * 01
Parameter Request List Item: (3) Router * 03
Parameter Request List Item: (28) Broadcast Address * 1c
Parameter Request List Item: (6) Domain Name Server * 06
Option: (255) End * ff
Padding:000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 * 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
4.DHCP服务器收到CH394发的Discover报文后会回复Offer报文:
Dynamic Host Configuration Protocol (Offer)
Message type: Boot Reply (2) * 02
Hardware type: Ethernet (0x01) * 01
Hardware address length: 6 * 06
Hops: 0 * 00
Transaction ID: 0xabcd0001 * ab cd 00 01
Seconds elapsed: 0 * 00 00
Bootp flags: 0x0000 (Unicast) * 00 00
Client IP address: 0.0.0.0 (0.0.0.0) * 00 00 00 00
Your (client) IP address: 192.168.2.200 (192.168.2.200) * c0 a8 02 c8
Next server IP address: 192.168.2.1 (192.168.2.1) c0 a8 02 01
Relay agent IP address: 0.0.0.0 (0.0.0.0) * 00 00 00 00
Client MAC address: NanjingQinhe_89:00:1a (70:19:88:89:00:1a) * 70 19 88 89 00 1a
Client hardware address padding: 00000000000000000000 * 00 00 00 00 00 00 00 00 00 00 Server host name not given * 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Boot file name not given * 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Magic cookie: DHCP * 63 82 53 63
Option: (53) DHCP Message Type (Offer) * 35
Length: 1 * 01
DHCP: Offer (2) * 02
Option: (54) DHCP Server Identifier (192.168.2.1) * 36
Length: 4 * 04
DHCP Server Identifier: 192.168.2.1 (192.168.2.1) * c0 a8 02 01
Option: (51) IP Address Lease Time * 33
Length: 4 * 04
IP Address Lease Time: 1 day (86400) * 00 01 51 80
Option: (58) Renewal Time Value * 3a
Length: 4 * 04
Renewal Time Value: 12 hours (43200) * 00 00 a8 c0
Option: (59) Rebinding Time Value * 3b
Length: 4 * 04
Rebinding Time Value: 21 hours (75600) * 00 01 27 50
Option: (1) Subnet Mask (255.255.255.0) * 01
Length: 4 * 04
Subnet Mask: 255.255.255.0 * ff ff ff 00
Option: (28) Broadcast Address (192.168.2.255) * 1c
Length: 4 * 04
Broadcast Address: 192.168.2.255 (192.168.2.255) * c0 a8 02 ff
Option: (3) Router * 03
Length: 4 * 04
Router: 192.168.2.1 (192.168.2.1) c0 a8 02 01
Option: (6) Domain Name Server * 06
Length: 4 * 04
Domain Name Server: 192.168.2.1 (192.168.2.1) * c0 a8 02 01
Option: (255) End * ff
Padding: 0000000000000000 * 00 00 00 00 00 00 00 00
5.CH394收到DHCP服务器发送的Offer报文后会回复Request报文:
Dynamic Host Configuration Protocol (Request)
Message type: Boot Request (1) * 01
Hardware type: Ethernet (0x01) * 01
Hardware address length: 6 * 06
Hops: 0 * 00
Transaction ID: 0xabcd0001 * ab cd 00 01
Seconds elapsed: 0 * 00 00
Bootp flags: 0x0000 (Unicast) * 00 00
Client IP address: 0.0.0.0 (0.0.0.0) * 00 00 00 00
Your (client) IP address: 0.0.0.0 (0.0.0.0) * 00 00 00 00
Next server IP address: 0.0.0.0 (0.0.0.0) * 00 00 00 00
Relay agent IP address: 0.0.0.0 (0.0.0.0) * 00 00 00 00
Client MAC address: NanjingQinhe_89:00:1a (70:19:88:89:00:1a) * 70 19 88 89 00 1a
Client hardware address padding: 00000000000000000000 * 00 00 00 00 00 00 00 00 00 00 Server host name not given * 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Boot file name not given * 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Magic cookie: DHCP * 63 82 53 63
Option: (53) DHCP Message Type (Request) * 35
Length: 1 * 01
DHCP: Request (3) * 03
Option: (61) Client identifier * 3d
Length: 7 * 07
Hardware type: Ethernet (0x01) * 01
Client MAC address: My Computer-2.local (a0:ce:c8:f7:a3:72) * a0 ce c8 f7 a3 72
Option: (50) Requested IP Address (192.168.2.200) * 32
Length: 4 * 04 c0 a8 02 c8
Option: (54) DHCP Server Identifier (192.168.2.1) * 36
Length: 4 * 04
DHCP Server Identifier: 192.168.2.1 (192.168.2.1) * c0 a8 02 01
Option: (12) Host Name * 0c
Length: 4 * 04
Host Name: ���� * b8 c2 b8 c2
Option: (81) Client Fully Qualified Domain Name * 51
Length: 7 * 07
Flags: 0x00 * 00
A-RR result: 0 * 00
PTR-RR result: 0 * 00
Client name: ���� * b8 c2 b8 c2
Option: (60) Vendor class identifier * 3c
Length: 8 * 08
Vendor class identifier: MSFT 5.0 * 4d 53 46 54 20 35 2e 30
Option: (55) Parameter Request List * 37
Length: 14 * 0e
Parameter Request List Item: (1) Subnet Mask * 01
Parameter Request List Item: (3) Router * 03
Parameter Request List Item: (6) Domain Name Server * 06
Parameter Request List Item: (15) Domain Name * 0f
Parameter Request List Item: (31) Perform Router Discover * 1f
Parameter Request List Item: (33) Static Route * 21
Parameter Request List Item: (43) Vendor-Specific Information * 2b
Parameter Request List Item: (44) NetBIOS over TCP/IP Name Server * 2c
Parameter Request List Item: (46) NetBIOS over TCP/IP Node Type * 2e
Parameter Request List Item: (47) NetBIOS over TCP/IP Scope * 2f
Parameter Request List Item: (119) Domain Search * 77
Parameter Request List Item: (121) Classless Static Route * 79
Parameter Request List Item: (249) Private/Classless Static Route (Microsoft) * f9
Parameter Request List Item: (252) Private/Proxy autodiscovery * fc
Option: (255) End * ff
Padding[]:00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 * 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
6.DHCP服务器接收到Request消息后,会回复ACK消息,确认IP地址分配成功,并提供最终的网络配置参数:
Dynamic Host Configuration Protocol (ACK)
Message type: Boot Reply (2) * 02
Hardware type: Ethernet (0x01) * 01
Hardware address length: 6 * 06
Hops: 0 * 00
Transaction ID: 0xabcd0001 * ab cd 00 01
Seconds elapsed: 0 * 00 00
Bootp flags: 0x0000 (Unicast) * 00 00
Client IP address: 0.0.0.0 (0.0.0.0) * 00 00 00 00
Your (client) IP address: 192.168.2.200 (192.168.2.200) * c0 a8 02 c8
Next server IP address: 192.168.2.1 (192.168.2.1) * c0 a8 02 01
Relay agent IP address: 0.0.0.0 (0.0.0.0) * 00 00 00 00
Client MAC address: NanjingQinhe_89:00:1a (70:19:88:89:00:1a) * 70 19 88 89 00 1a
Client hardware address padding: 00000000000000000000 * 00 00 00 00 00 00 00 00 00 00
Server host name not given * 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Boot file name not given
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Magic cookie: DHCP * 63 82 53 63
Option: (53) DHCP Message Type (ACK) * 35
Length: 1 * 01
DHCP: ACK (5) * 05
Option: (54) DHCP Server Identifier (192.168.2.1) * 36
Length: 4 * 04
DHCP Server Identifier: 192.168.2.1 (192.168.2.1) * c0 a8 02 01
Option: (51) IP Address Lease Time * 33
Length: 4 * 04
IP Address Lease Time: 1 day (86400) * 00 01 51 80
Option: (58) Renewal Time Value * 3a
Length: 4 * 04
Renewal Time Value: 12 hours (43200) * 00 00 a8 c0
Option: (59) Rebinding Time Value * 3b
Length: 4 * 04
Rebinding Time Value: 21 hours (75600) * 00 01 27 50
Option: (1) Subnet Mask (255.255.255.0) * 01
Length: 4 * 04
Subnet Mask: 255.255.255.0 * ff ff ff 00
Option: (28) Broadcast Address (192.168.2.255) * 1c
Length: 4 * 04
Broadcast Address: 192.168.2.255 (192.168.2.255) * c0 a8 02 ff
Option: (3) Router * 03
Length: 4 * 04
Router: 192.168.2.1 (192.168.2.1) * c0 a8 02 01
Option: (6) Domain Name Server * 06
Length: 4 * 04
Domain Name Server: 192.168.2.1 (192.168.2.1) * c0 a8 02 01
Option: (15) Domain Name * 0f
Length: 3 * 03
Domain Name: lan * 6c 61 6e
Option: (255) End * ff
Padding: 000000 * 00 00 00