# DHCP Protocol 🌐

## Introduction 📝

DHCP is an application layer protocol that plays a crucial role in configuring the network layer. In this markdown, we will explore how DHCP works and its communication process. 🤓

## DHCP Discovery Process 🔍

The DHCP discovery process consists of four steps. Let's break it down! 👇

### 1. Server Discovery Step 📡

The first step is the server discovery process, where the DHCP client broadcasts a DHCP discover message 📢 onto the network. Since the client doesn't have an IP address and is unaware of the DHCP server's IP, it crafts a specially designed broadcast message. The DHCP discover message is encapsulated in a UDP datagram with a destination port of 67 and a source port of 68. It is then encapsulated inside an IP datagram with a destination IP of 255.255.255.255 and a source IP of 0.0.0.0. This broadcast message reaches every node on the local area network, allowing the DHCP server to receive it if present. 📨

### 2. Offer from DHCP Server 🎁

After receiving the DHCP discover message, the DHCP server examines its own configuration and decides on an IP address to offer to the client. The server sends a DHCP offer message 💌 with a destination port of 68, a source port of 67, a destination broadcast IP of 255.255.255.255, and its actual IP as the source. As the DHCP offer is also a broadcast, it reaches every machine on the network. The client identifies the offer intended for itself by checking the MAC address specified in the DHCP discover message. 📩

### 3. Client Request 🙋‍♂️

Upon receiving the DHCP offer, the client processes it to determine the IP address being offered. The client can technically reject the offer, especially in cases where multiple DHCP servers are running on the same network, and the client is configured to respond only to offers within a specific IP range. However, this scenario is rare. More commonly, the DHCP client responds to the DHCP offer message with a DHCP request message ✉️, indicating its acceptance of the offered IP. Since the client hasn't been assigned an IP yet, the request is sent from an IP of 0.0.0.0 and to the broadcast IP of 255.255.255.255.

### 4. Acknowledgement from DHCP Server ✅

In the final step, the DHCP server receives the DHCP request message and responds with a DHCPACK or DHCP Acknowledgement message ✅. This message is sent to the broadcast IP of 255.255.255.255, with a source IP corresponding to the actual IP of the DHCP server. Similar to before, the client recognizes this message as intended for itself by checking its MAC address included in one of the message fields. The networking stack on the client computer can now utilize the configuration information provided by the DHCP server to set up its own network layer configuration. 🖥️

## DHCP Lease and Renewal 🔄

Once the client has obtained an IP address through the DHCP process, it is referred to as a DHCP lease. A DHCP lease has an expiration time and can last for days or a short period. When the lease expires, the client needs to negotiate a new lease by repeating the entire DHCP discovery process. Additionally, the client can release its lease to the DHCP server when disconnecting from the network, allowing the server to return the IP address to its pool of available IPs. ♻️

That's it! Now you have a better understanding of the DHCP protocol and its role in network configuration. 🎉
