DHCP动态分配ip原理
一、 概念
动态主机配置协议(Dynamic Host Configuration Protocol,DHCP)是一个使用 UDP 的局域网网络协议。
1.1. 作用
为内网用户动态分配 IP 地址,或 ISP 为用户动态分配 IP 地址
1.2. 为什么要使用 DHCP
网络上的设备需要有 IP 才能访问网络及其资源。如果没有 DHCP:需要手动为设备配置 IP 地址,需要手动回收从网络移除的设备的 IP 地址
二. DHCP 原理
DHCP 服务器维护一个 IP 地址池,为启用 DHCP 的客户端动态分配 IP 地址,客户端可以在预定义的期限内使用该 IP 地址。
2.1. DHCP 分配 IP 过程
- DHCP 发现(DISCOVER)
DHCP Client 启动时没有 IP,会发送源地址为 0.0.0.0,目的地址为 255.255.255.255 的 DHCPDISCOVER 广播报文。网络上支持 TCP/IP 的机器都会收到该报文,但只有 DHCP Server 会对该报文做出相应。
- DHCP 提供(OFFER)
DHCP Server 收到 DHCPDISCOVER 报文后,通过解析报文,查询 dhcpd.conf 配置文件,如果在地址池中能找到合适的 IP 地址,DHCP Server 会给 DHCP Client 发送 DHCPOFFER 报文。
- DHCP 请求(REQUEST)
当 DHCP Client 收到 DHCPOFFER 报文时,它会发送一个 DHCPREQUEST 请求报文。
如果 Client 收到多个 DHCP Server 返回的 DHCPOFFER,可以选择等待多个响应。Client 根据 DHCPOFFER 中提供的配置参数选择一个 DHCP Server,广播 DHCPREQUEST 消息,该消息必须包含“服务器标识符”选项以指示它选择了哪个服务器。
DHCPREQUEST 消息是通过 DHCP/BOOTP 中继代理进行广播,为了确保 BOOTP 将 DHCPREQUEST 消息转发到接收原始 DHCPDISCOVER 消息的同一组 DHCP Server,DHCPREQUEST 消息必须在 DHCP 消息头的 secs 字段中使用相同的值并发送到相同的 IP 广播地址作为原始DHCPDISCOVER 消息。
如果 Client 没有收到 DHCPOFFER 消息,则 Client 超时并重新发送 DHCPDISCOVER 消息。
- DHCP 确认(ACK)
DHCP Server 接收 DHCPREQUEST 广播报文,DHCPREQUEST 消息中选择的 DHCP Server 将 Client 的绑定提交到持久存储,并使用包含 Client 请求的配置参数的 DHCPACK 消息进行响应。
如果所选的 DHCP Server 不能满足 DHCPREQUEST 消息,则响应 DHCPNAK 消息。
- 完成配置
Client 收到带有配置参数的 DHCPACK 消息,检查消息中的租约时间。如果 Client 收到 DHCPNAK 消息,则重新进行配置过程。
三、DHCP Client 和 Server 交换消息图
Server Client Server
(not selected) (selected)
v v v
| | |
| Begins initialization |
| | |
| _____________/|\____________ |
|/DHCPDISCOVER | DHCPDISCOVER \|
| | |
Determines | Determines
configuration | configuration
| | |
|\ | ____________/|
| \________ | /DHCPOFFER |
| DHCPOFFER\ |/ |
| \ | |
| Collects replies |
| \ | |
| Selects configuration |
| | |
| _____________/|\____________ |
|/ DHCPREQUEST | DHCPREQUEST\ |
| | |
| | Commits configuration
| | |
| | _____________/|
| |/ DHCPACK |
| | |
| Initialization complete |
| | |
. . .
. . .
| | |
| Graceful shutdown |
| | |
| |\ ____________ |
| | DHCPRELEASE \|
| | |
| | Discards lease
| | |
v v v
分配新 IP 时 DHCP Client 和 Server 之间交换消息的时间图
四. 续约
Client 的租约到期后 IP 会被 DHCP Server 收回,所以 Client 如果想继续使用该 IP,需要在到达租约期限的一半时向 DHCP Server 发起 DHCPREQUEST 消息来延长租约。
五、android dhcp分配的流程
Line 22577: 04-20 07:27:58.812 2564 3123 E DhcpClient: Error retrieving network attributes: ILLEGAL ARGUMENT
Line 22578: 04-20 07:27:58.813 2564 4518 D DhcpClient: Broadcasting DHCPDISCOVER
Line 22580: 04-20 07:27:58.815 2564 4518 D DhcpClient: Received packet: 08:26:ae:38:1d:dd OFFER, ip /192.168.1.105, mask /255.255.255.0, DNS servers: /192.168.20.12 /192.168.1.11 , gateways [/192.168.1.1], lease time 3776, domain null
Line 22581: 04-20 07:27:58.816 2564 4518 D DhcpClient: Got pending lease: android.net.networkstack.DhcpResults@580bec9 DHCP server /192.168.1.1 Vendor info null lease 3776 seconds Servername
Line 22582: 04-20 07:27:58.817 2564 4518 D DhcpClient: Broadcasting DHCPREQUEST ciaddr=0.0.0.0 request=192.168.1.105 serverid=192.168.1.1
Line 22587: 04-20 07:27:58.818 2564 4518 D DhcpClient: Received packet: 08:26:ae:38:1d:dd ACK: your new IP /192.168.1.105, netmask /255.255.255.0, gateways [/192.168.1.1] DNS servers: /192.168.20.12 /192.168.1.11 , lease time 7200
Line 22588: 04-20 07:27:58.819 2564 4518 D DhcpClient: Confirmed lease: android.net.networkstack.DhcpResults@5a140ef DHCP server /192.168.1.1 Vendor info null lease 7200 seconds Servername
Line 22592: 04-20 07:27:58.826 2564 4518 D DhcpClient: Scheduling renewal in 3599s
Line 22593: 04-20 07:27:58.826 2564 4518 D DhcpClient: Scheduling rebind in 6299s
Line 22594: 04-20 07:27:58.826 2564 4518 D DhcpClient: Scheduling expiry in 7199s
Line 24438: 04-20 07:28:04.292 2564 3123 E DhcpClient: Error retrieving network attributes: ILLEGAL ARGUMENT
Line 24439: 04-20 07:28:04.293 2564 4584 D DhcpClient: Broadcasting DHCPDISCOVER
Line 24440: 04-20 07:28:04.294 2564 4584 D DhcpClient: Received packet: 08:26:ae:38:1d:5b OFFER, ip /192.168.1.100, mask /255.255.255.0, DNS servers: /192.168.20.12 /192.168.1.11 , gateways [/192.168.1.1], lease time 171, domain null
Line 24441: 04-20 07:28:04.295 2564 4584 D DhcpClient: Got pending lease: android.net.networkstack.DhcpResults@e24023c DHCP server /192.168.1.1 Vendor info null lease 171 seconds Servername
Line 24442: 04-20 07:28:04.296 2564 4584 D DhcpClient: Broadcasting DHCPREQUEST ciaddr=0.0.0.0 request=192.168.1.100 serverid=192.168.1.1
Line 24444: 04-20 07:28:04.297 2564 4584 D DhcpClient: Received packet: 08:26:ae:38:1d:5b ACK: your new IP /192.168.1.100, netmask /255.255.255.0, gateways [/192.168.1.1] DNS servers: /192.168.20.12 /192.168.1.11 , lease time 7200
Line 24445: 04-20 07:28:04.297 2564 4584 D DhcpClient: Confirmed lease: android.net.networkstack.DhcpResults@bf1191a DHCP server /192.168.1.1 Vendor info null lease 7200 seconds Servername
Line 24449: 04-20 07:28:04.306 2564 4584 D DhcpClient: Scheduling renewal in 3599s
Line 24450: 04-20 07:28:04.306 2564 4584 D DhcpClient: Scheduling rebind in 6299s
Line 24451: 04-20 07:28:04.306 2564 4584 D DhcpClient: Scheduling expiry in 7199s
Line 25634: 04-20 07:28:09.342 2564 4584 D DhcpClient: doQuit
Line 25657: 04-20 07:28:09.389 2564 4584 D DhcpClient: DHCP Packet Handler stopped
Line 25658: 04-20 07:28:09.390 2564 4584 D DhcpClient: onQuitting