Illustrated DHCP protocol, understand how your computer/mobile phone automatically obtains IP

Illustrated DHCP protocol, understand how your computer/mobile phone automatically obtains IP

Hello everyone, I am Bernie, an IT pre-sales engineer.

This article briefly introduces the technical principles of DHCP in computer networks. Welcome to read.

We know that whether it is a mobile phone, computer, notebook, or other smart terminal, as long as it is connected to WIFI or network cable, it can directly access the Internet. When we check the IP information of the notebook, it will display the current device's IP address, gateway address, subnet mask, etc. This information is necessary in the data packet in the computer network and is placed in the datagram header.

However, we did not manually configure the above information.

How does the device's IP come from?

You may want to check your laptop's Internet options. If nothing goes wrong, it will show "default configuration for automatically obtaining IT addresses". In fact, these IPs are automatically obtained through the DHCP server.

Next, we will use a simple office scenario to explain the concept and technical principles of DHCP services.

What is DHCP?

The full name of DHCP is Dynamic Host Configuration Protocol.

To put it simply: in a computer network, end devices can dynamically obtain IP addresses, subnet masks and gateway addresses from a DHCP server so that they can mark their identities in the network and obtain Internet access.

For example, in a local area network, there is a PC and a mobile phone, both of which are connected to a router. Then, both the PC and the mobile phone can get the IP address, gateway address and subnet mask from the router. Here, the router is their DHCP server.

So how do they get IP addresses? Here we need to focus on

DHCP Service Principle

Step 1: discover

When a terminal device is plugged into a network cable or connected to WIFI, it has no IP address during the initialization phase. The first task is to find a DHCP server in the subnet through broadcasting.

We know that any data packet in a computer network must contain the source IP address and the destination IP address. This is a contradiction, because the purpose of pc3 sending a broadcast message is to obtain an IP address, and it does not know what its own IP address should be, nor does it know what the address of the DHCP server is.

Here, the source IP of this data packet is often assigned as 0.0.0.0 and the destination IP is 255.255.255.255.

One thing to point out is that 0.0.0.0 is not a real IP address. It is actually the default route, that is, it tells the other party that I don’t know my IP address, so I use 0.0.0.0 instead.

255.255.255.255 is not a strict IP address. It is a restricted broadcast address, that is, it tells all end devices on the same network segment, all devices here pay attention! 255.255.255.255 cannot be forwarded by a router because it does not make much sense.

This broadcast message will be sent by the layer 2 switch to all devices under the same VLAN, including pc1, pc2, dhcp1 and dhcp2.

Step 2: Offer

Since PC1 and PC2 are end devices and do not have DHCP capabilities, they will automatically ignore this broadcast message.

When DHCP server 1 and DHCP server 2 receive the request message for obtaining an IP address, they will assign an IP address, gateway address, mask and other information to PC3.

At this time, PC3 will receive two different sets of IP information, and they can both be used normally. If there are several DHCP servers in the same network segment, several sets of IP address information will be allocated to PC3.

Note: This includes not only the IP information, but also the IP usage period. After the period expires, it will be automatically recycled or renewed. For example, the minimum DHCP lease period of a broadband router is 1 minute, the maximum is two days, and the default value is 120 minutes.

Step 3: request

In the above example, pc3 received two different IP addresses in one broadcast. Obviously, it cannot occupy all of them and can only use one. Therefore, pc3 will select one of them (usually the one that arrives first). For example, if IP1 arrives first, pc3 will select IP1 as its own IP address information.

Is this the end?

No!

Because DHCP server 1 does not know that the IP address it provides has been selected, similarly, DHCP server 2 does not know that the IP address it provides has been eliminated.

Therefore, pc3 needs to send a broadcast message to inform the result of its selection.

Some of you may wonder why pc3 still uses 0.0.0.0 in the datagram when pc3 already has its own IP address. This is because it has not been confirmed yet and cannot send a unicast message to dhcp server1. Other dhcp servers also need to know this.

Step 4: ack

Finally we have reached the last step, and the work of this step is confirmation.

After receiving the selection result from PC3, DHCP server1 will update its routing table and MAC address mapping table, and also send a confirmation message to PC3.

Since the IP address previously assigned to pc3 by dhcp2 was not selected, it will reclaim the IP address so that it can be assigned to other access devices in the future. At the same time, it will also update its own mac address table and routing table.

Summarize

The above is the whole process of a computer network terminal device obtaining DHCP services. The whole process consists of four parts: discover, offer, request and ack. They are progressive and interlocking.

Special note: The IP assigned by the DHCP server is a temporary lease! Generally speaking, as long as the lease time is not set to infinite (permanent lease), the end device needs to send a request packet to the DHCP server to renew its lease time when the lease period reaches 50%.

The article comes from: ​​IT Yizhichan ​​. If you wish to reprint this article, please contact [IT Yizhichan] Toutiao account.


<<:  What is Wavelength Division Multiplexing (WDM)? A Beginner's Guide to WDM

>>:  Everything You Should Know About SFP Modules

Recommend

See all the things a service mesh can do

Service mesh adoption continues to grow, and some...

Use what-if analysis to meet data center capacity needs

The authors describe the challenges of capacity r...

...

The Smart Network: Cisco's most disruptive innovation in a decade

A little over a year ago, my colleague David McGr...

12 Ways 5G in Manufacturing Can Boost Industry 4.0

We’re on the cusp of a new era of connectivity, b...

Detailed roadmap for 5G standards

[[429204]] While 5G has been a hot topic for year...

5G will redefine the products and services of telecom operators

[[388868]] There is a seemingly endless list of 5...

4 ways 5G will change our lives

5G technology is being rolled out in many countri...

Let’s talk about HTTP pipelining

HTTP pipelining is a technique for sending multip...

Let’s talk about 6G development

As of now, the fifth generation (5G) mobile commu...

Let’s talk about protocols and hard drives in the Web3 world: IPFS

In the Web2.0 world, the protocol is usually HTTP...