Lightweight DHCP server written in C# (.NET 10 / C# 14). Designed for clarity and ease of use with a clean, modular structure for configuration, lease management, logging, and server logic.
This server implements core DHCP operations and provides a straightforward configuration and deployment experience.
Key capabilities:
- DHCP Discover / Offer / Request / Ack / Release / Decline handling
- IP assignment from a configurable pool
- Static MAC-to-IP reservations
- Lease persistence to disk (
DHCP_Leases.json) - Basic IP conflict detection using ICMP ping
- Console and file logging (
DHCP_Server.log)
- Language: C# 14.0
- Target: .NET 10
- Default config file:
DHCP_Config.json - Default log file:
DHCP_Server.log - Lease file:
DHCP_Leases.json - UDP server port: 67
- .NET 10 SDK
- Elevated privileges to bind to UDP port 67 (Administrator or root) when running locally
From the repository root:
- Build:
dotnet build
- Run:
dotnet run --project DHCP-Server
On platforms that require privileges to bind privileged ports, run with appropriate elevation (e.g., sudo on Linux).
The server loads configuration from DHCP_Config.json in the working directory. If the file does not exist the server will create a default DHCP_Config.json that you can edit.
Example DHCP_Config.json:
{
"ServerIp": "192.168.2.1",
"SubnetMask": "255.255.255.0",
"Gateway": "192.168.2.1",
"DnsServer": "192.168.2.1",
"SecondaryDns": "8.8.8.8",
"StartIp": "192.168.2.100",
"EndIp": "192.168.2.200",
"LeaseDuration": 3600,
"DomainName": "local.net",
"NtpServer": "",
"Reservations": {
"08-00-27-85-FC-2B": "192.168.2.150"
}
}Reservationskeys are MAC addresses in hex format (e.g.,08-00-27-85-FC-2B).
Edit DHCP_Config.json and restart the server to apply changes.
Program.cs— entry pointConfig/ConfigLoader.cs— reads and writes configurationConfig/DhcpConfig.cs— configuration modelServer/DhcpServer.cs— DHCP server and packet handlingLeases/LeaseManager.cs— lease persistence and managementLogging/Logger.cs— console and file loggingModels/— models such asDhcpLeaseandDhcpMessageType
Contributions are welcome. Please follow the project style and open a pull request with your changes.