Hi this is John with this week’s Coding Challenge.
🙏 Thank you for being one of the 87,743 software developers who have subscribed, I’m honoured to have you as a reader. 🎉
If there is a Coding Challenge you’d like to see, please let me know by replying to this email📧
Coding Challenge #94 - DHCP Client
This challenge is to build your own DHCP client. Dynamic Host Configuration Protocol (DHCP) is a network management protocol used to assign IP addresses to devices connected to a network.
DHCP removes the need for a user to manually configure network devices. You almost certainly use it on your home or office network when you connect a device to the Wifi or your Internet gateway.
If You Enjoy Coding Challenges Here Are Three Ways You Can Help Support It
Refer a friend or colleague to the newsletter. 🙏
Sign up for a paid subscription - think of it as buying me a coffee ☕️, with the bonus that you also get 20% off any of my courses.
Buy one of my courses that walk you through a Coding Challenge.
The Challenge - Building A DHCP Client
In this coding challenge you will be building a DHCP client and using it to request an IP address from your local DHCP server.
Before you start, use the command line to see what’s going on with your network interface and DHCP on your local network. If you’re on Windows you can use the ipconfig
command, Linux users can use ifconfig
or ip
and Mac OS users networksetup
.
On Windows it’ll look something like this:
c:\\>ipconfig /all
Wireless LAN adapter Wi-Fi:
Connection-specific DNS Suffix . : wifihub
Description . . . . . . . . . . . : Wireless USB Adapter
Physical Address. . . . . . . . . : 33-d8-34-11-10-20
DHCP Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes
IPv4 Address. . . . . . . . . . . : 192.168.1.15(Preferred)
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Lease Obtained. . . . . . . . . . : 13 June 2025 18:31:33
Lease Expires . . . . . . . . . . : 20 July 2161 20:37:32
Default Gateway . . . . . . . . . : fe80::d692:5eff:fe17:85d%3
192.168.1.1
DHCP Server . . . . . . . . . . . : 192.168.1.1
DNS Servers . . . . . . . . . . . : 192.168.1.1
NetBIOS over Tcpip. . . . . . . . : Enabled
On Linux it’ll look something like this:
$ ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:45:83:74 brd ff:ff:ff:ff:ff:ff
inet 172.16.179.129/24 brd 172.16.179.255 scope global dynamic eth0
valid_lft 1651sec preferred_lft 1651sec
inet6 fe80::20c:29ff:fe45:8374/64 scope link
valid_lft forever preferred_lft forever
On Mac OS it’ll look something like this:
% networksetup -listallnetworkservices
An asterisk (*) denotes that a network service is disabled.
Wi-Fi
Thunderbolt Bridge
% networksetup -getinfo Wi-Fi
DHCP Configuration
IP address: 192.168.1.199
Subnet mask: 255.255.255.0
Router: 192.168.1.1
Client ID:
IPv6: Automatic
IPv6 IP address: none
IPv6 Router: none
Wi-Fi ID: a8:19:e3:aa:23:43
Step Zero
In this introductory step you’re going to set your software development environment up ready to begin building and testing your solution.
I’ll leave you to set up your editor and programming language of choice. I’d encourage you to pick a tech stack that you’re comfortable doing network programming with.
Step 1
In this step your goal is to be able to serialise and deserialise DHCP messages. The message format is defined in the DHCP specification in RFC 2131. You will need to be able to serialise and deserialise the DHCPDISCOVER, DHCPOFFER, DHCPREQUEST, DHCPACK, DHCPNAK and DHCPDECLINE messages.
Step 2
In this step your goal is to create the two sockets you’ll need for a DHCP client. Why two sockets? Because DHCP is a way of requesting an IP address, which means that we might not already have an IP address and on top of that we don’t know the IP address of the DHCP server, or even if there is one on the network.
Given these limitations we cannot create a TCP connection between the client and the server so we leverage UDP and the ability to use a broadcast address, sending the message to any “server” that might be listening on the local network. In order to receive a response the DHCP client will also need to be listening (acting like a server) for a broadcast message sent by the DHCP server.
The DHCP specification (RFC 2131) defines the port for the DHCP server as 67 and the port for the client as 68.
Step 3
In this step your goal is to request an IP address and read the response (offer) from the server. Once you can do that and have printed it out / logged it, move on to step 4.
Step 4
In this step your goal is to respond to the offer with a request for the offered IP address. Your DHCP client should then listen for and handle the server’s ACK or NACK.
Step 5
In this final step your goal is to set the local IP address if the server sent an ACK. You should also extend your client to support the ability to release an IP address, by clearing it from the selected NIC and letting the server know that the client no longer requires the IP address.
Having done all of this you should be able to release the IP address on your NIC and request a new one. Be sure to check!
Going Further
The obvious next step for this project is to build a DHCP server.
Two Other Ways I Can Help You:
I write another newsletter Developing Skills that helps you level up the other skills you need to be a great software developer.
I have a YouTube channel sharing advice on software engineering.
Share Your Solutions!
If you think your solution is an example other developers can learn from please share it, put it on GitHub, GitLab or elsewhere. Then let me know via Bluesky or LinkedIn or just post about it there and tag me. Alternately please add a link to it in the Coding Challenges Shared Solutions Github repo
Request for Feedback
I’m writing these challenges to help you develop your skills as a software engineer based on how I’ve approached my own personal learning and development. What works for me, might not be the best way for you - so if you have suggestions for how I can make these challenges more useful to you and others, please get in touch and let me know. All feedback greatly appreciated.
You can reach me on Bluesky, LinkedIn or through SubStack
Thanks and happy coding!
John