Brief DHCP analysis and problem solving

Source: Internet
Author: User

DHCP involves four main processes: Discover, offer, request, and ack.
 

Step 1:
The client (PC or mobile terminal) searches for the server or router that can provide IP addresses in the form of a broadcast package, which is the Discover stage.

 
Step 2:
All servers or routers that receive the Discover broadcast packet and can allocate IP addresses will respond to the broadcast packet. The packet sending method may be broadcast packets, it may also be a single broadcast (this may vary depending on the vro ). This is the offer stage.

 

Step 3:
After the client receives the packet (although there are many offer packets in the network at this time, it will only accept the first offer packet), It extracts the desired IP address, DNS, net gate, calculate the submask Based on the IP address, and then inform all servers or routers with IP addresses in the form of broadcast packets that I have obtained an IP address, in addition, this message contains information about the server or router that assigns an IP address to the client. This is the request stage.
 

Step 4:
After receiving the request broadcast packet, all routers or servers extract the server information from the packet. If the packet is directed to their own, the backend ack (similarly, because different ack methods on the vro may be broadcast packets or unicast packets), other servers or vrouters will not return ack. This is the ACK stage.
========================================================== ==================================

Question 1:
When I debug the board, I encountered this situation: I have two boards that send DHCP requests to the LAN at the same time, and the obtained IP addresses are actually the same. Why?

 

Cause:
The final reason is that when I set the Discover package, I wrote the transaction ID in the DHCP discover package as a fixed value, that is, the transaction ID in the Discover packet sent by the two Boards is the same, and the router then sends the offer packet and responds to the ACK packet in the form of a broadcast packet. When both of my boards receive the broadcast package, and find that the transaction ID in the offer and ACK packets is the same as the one sent by myself, the IP address is assigned to the user, so the IP addresses of the two are the same.

 
Solution:
Change the transaction ID to a random number.

 
Summary:
In the four steps of DHCP, the transaction ID is the same, that is to say, to determine whether the packet matches according to the transaction ID, that is, to determine whether the packet is a required DHCP packet. There is an exception here. If a router sends an offer or ACK packet in the form of a single broadcast, even if the transaction ID is fixed, the same IP address will not be assigned, because it can be distinguished based on the target MAC address in the offer and ACK packets.
--------------------------------------------------------------------------------

Question 2:
After solving the problem 1, I found a new problem: After the Board obtains an IP address, for example, the obtained IP address is "192.168.2.3". At this time, I use a PC together with a vro, manually set the IP address of the PC to "192.168.2.3". The IP address conflict is displayed on the computer immediately. But on my board, how does one detect IP conflict?

 

Solution:
After obtaining the IP address, send an IP detect ARP request message on the board to check whether the IP address is in conflict. In this ARP request message, both the sender IP and destination IP point to the obtained IP address (192.168.2.3. After a PC with the IP address "192.168.2.3" receives the ARP request, or responds to an ARP reply packet, the sender IP address and destination IP address in the packet are the same (192.168.2.3 in this example ). Then you only needProgramTo determine whether the sender IP address and destination IP address in the received ARP reply packet are the same, you can determine whether the Board's IP address conflicts with the PC or mobile terminal.
----------------------------------------------------------------------------------

 
Question 3:
After detecting IP conflicts, How can I obtain an IP address again? At the beginning, I only re-sent the DHCP discover packet in the program and re-performed the DHCP four steps. The results showed that the obtained IP address is the same as the previous one and there will still be conflicts, why is it the same? How can I make the IP address retrieved again different?

 
Cause:
Because there is an ARP cache in the vroarp, when an IP address is obtained first, the ARP cache of the vroarp records the IP address and MAC address of the Board (which correspond to each other ). Then, the MAC address of the PC (with the same IP address as that of the Board) That is manually set is not recorded by the ARP cache, because one IP address in the ARP cache can only correspond to one MAC address. So when the IP address obtained by the Board is re-DHCP, the router finds that the ARP cache contains the IP address corresponding to the MAC address of the Board. Then the router will allocate the IP address to the board. Therefore, even if you use DHCP again, the obtained IP address is the same as the previous one.

 
Solution:
Then we have to find a way to let the router know that we do not want the current IP address, and tell the router to reassign a different IP address to us. How can I make the router work according to this requirement? Packet captured by the packet capture tool on the PC, I found that in addition to the above four types of DHCP packets, there is another type: DHCP decline packets. The purpose of this packet is to tell the router that "my current IP address is no longer used. please delete the correspondence between my mac address and this IP address from your ARP cache ". This packet is sent in the form of a broadcast package. After the DHCP decline packet is sent, the DHCP discover packet is sent again, and the new IP address can be obtained through four DHCP steps.
========================================================== ======================================

The link below is five DHCP packets sent by my board and pc I caught using Ethereal. Data packet analysis is the most intuitive way to learn TCP/IP.
Http://download.csdn.net/source/1986344
Enter BOOTP in the filter box of Ethereal to filter DHCP packets.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.