A high-performance, multi-interface DHCP server written in Go with REST API support, intelligent caching, and systemd integration.
- Multi-interface Support: Listen on multiple network interfaces simultaneously
- DHCP Relay: Forward DHCP requests to relay servers for complex network topologies
- HTTP REST API: Manage DHCP assignments via HTTP endpoints on port 22227
- Intelligent Caching: Multi-level IP/MAC address caching with TTL for optimal performance
- Worker Pool Architecture: 100 concurrent workers processing DHCP requests
- Systemd Integration: Native systemd service support with watchdog functionality
- Real-time Statistics: Monitor DHCP pool usage and active assignments
- Layer 2 Support: Handle both unicast and broadcast DHCP traffic
- Static Reservations: Support for MAC-to-IP static assignments
- Go 1.15 or later
- Root privileges (required for binding to DHCP port 67)
- Linux system with network interfaces
- Systemd (optional, for service management)
# Clone and build
git clone https://github.com/fdurand/standalone_dhcp.git
cd standalone_dhcp
# Build optimized binary
go build -ldflags "-s -w" -o godhcp .Create or edit godhcp.ini:
[interfaces]
# Interfaces that act as DHCP server
listen=eth1
# Interface:Relay IP - forward DHCP requests to relay address
relay=eth1.2:172.20.0.1,eth1.3:172.21.0.1
[network 192.168.1.0]
dns=8.8.8.8,8.8.4.4
gateway=192.168.1.1
dhcp_start=192.168.1.10
dhcp_end=192.168.1.254
domain-name=example.org
dhcp_max_lease_time=3600
dhcp_default_lease_time=1800
dhcpd=enabled
netmask=255.255.255.0
# Static IP assignments (MAC:IP pairs)
ip_assigned=b8:27:eb:e8:40:43:192.168.1.251,d8:3a:dd:27:67:2c:192.168.1.252# Run directly (requires root privileges)
sudo ./godhcp
# Or install as systemd service
sudo cp godhcp.service /etc/systemd/system/
sudo systemctl enable godhcp
sudo systemctl start godhcpThe project includes pre-configured VS Code tasks accessible via Ctrl+Shift+P β "Tasks: Run Task":
- Build DHCP Server: Create optimized production binary
- Build DHCP Server (Debug): Create debug binary with symbols
- Test DHCP Server: Run all tests
- Clean Build Artifacts: Remove build files
- Check Code Quality: Run
go vetandgo fmt
# Production build (optimized, smaller binary)
go build -ldflags "-s -w" -o godhcp .
# Debug build (with symbols)
go build -o godhcp-debug .
# Run tests
go test ./...
# Code quality checks
go vet ./... && go fmt ./...
# Clean build artifacts
rm -f godhcp godhcp-debug standalone_dhcplisten: Comma-separated list of interfaces to listen for DHCP requestsrelay: Interface-to-relay mappings in formatinterface:relay_ip
dns: DNS servers (comma-separated)gateway: Default gateway for the networkdhcp_start: Start of IP range to distributedhcp_end: End of IP range to distributenetmask: Subnet mask for the networkdomain-name: Domain name provided to DHCP clientsdhcp_default_lease_time: Default lease time in secondsdhcp_max_lease_time: Maximum lease time in secondsdhcpd: Enable/disable DHCP for this network (enabled/disabled)ip_assigned: Static MAC-to-IP assignments (format:mac:ip,mac:ip)
The server provides a comprehensive REST API on 127.0.0.1:22227 for DHCP management and monitoring.
curl http://127.0.0.1:22227/api/v1/dhcp/mac/10:1f:74:b2:f6:a5Response:
{
"result": {
"ip": "192.168.1.100",
"mac": "10:1f:74:b2:f6:a5"
}
}curl http://127.0.0.1:22227/api/v1/dhcp/ip/192.168.1.100Response:
{
"result": {
"ip": "192.168.1.100",
"mac": "10:1f:74:b2:f6:a5"
}
}Remove a MAC-to-IP binding from the cache:
curl -X DELETE http://127.0.0.1:22227/api/v1/dhcp/mac/10:1f:74:b2:f6:a5curl http://127.0.0.1:22227/api/v1/dhcp/statscurl http://127.0.0.1:22227/api/v1/dhcp/stats/eth1curl http://127.0.0.1:22227/api/v1/dhcp/stats/eth1/192.168.1.0Example Response:
[
{
"category": "registration",
"free": 253,
"interface": "eth1",
"members": [
{
"mac": "10:1f:74:b2:f6:a5",
"ip": "192.168.1.100"
}
],
"network": "192.168.1.0/24",
"options": {
"optionDomainName": "example.org",
"optionDomainNameServer": "8.8.8.8",
"optionIPAddressLeaseTime": "1800",
"optionRouter": "192.168.1.1",
"optionSubnetMask": "255.255.255.0"
}
}
]Get detailed debugging information for interface and role:
curl http://127.0.0.1:22227/api/v1/dhcp/debug/eth1/registrationmain.go: Application entry point, HTTP server, and initializationconfig.go: INI-based configuration management and DHCP handlersinterface.go: Network interface handling and DHCP protocol logicapi.go: REST API endpoint handlersserver.go: Core DHCP server functionalitydictionary.go: DHCP option parsing and TLV handlingpool/: IP address pool management and allocationworkers_pool.go: Concurrent request processing
Multi-Level Caching:
- IP Cache: Maps IPs to lease information (5min TTL)
- MAC Cache: Maps MAC addresses to assignments (5min TTL)
- Transaction Cache: Tracks DHCP transactions (5min TTL)
- Request Cache: Prevents duplicate processing (1sec TTL)
Concurrency Model:
- 100 Worker Goroutines: Process DHCP requests concurrently
- 100-Request Buffer: Handle traffic spikes without blocking
- Per-Interface Listeners: Separate goroutines for each network interface
- Transaction Locking: Prevent race conditions in IP assignments
Permission Denied:
# DHCP requires root privileges
sudo ./godhcpPort 67 Already in Use:
# Stop conflicting DHCP services
sudo systemctl stop isc-dhcp-server dhcpcd
sudo pkill dnsmasqInterface Not Found:
# Verify interface names
ip link show
# Update godhcp.ini with correct interface namesNo DHCP Responses:
# Check firewall rules
sudo iptables -I INPUT -p udp --dport 67 -j ACCEPT
sudo iptables -I OUTPUT -p udp --sport 67 -j ACCEPT- Systemd Logs:
journalctl -u godhcp -f - API Health:
curl http://127.0.0.1:22227/api/v1/dhcp/stats - Process Status:
ps aux | grep godhcp
βββ main.go # Application entry point
βββ config.go # Configuration management
βββ interface.go # DHCP protocol handling
βββ api.go # REST API endpoints
βββ server.go # Core DHCP server logic
βββ serverif.go # Server interface utilities
βββ dictionary.go # DHCP option parsing
βββ utils.go # Utility functions
βββ rawClient.go # Raw socket client
βββ workers_pool.go # Worker pool management
βββ pool/ # IP address pool management
β βββ pool.go
β βββ pool_test.go
βββ godhcp.ini # Default configuration
βββ godhcp.service # Systemd service file
βββ .vscode/ # VS Code tasks and settings
β βββ tasks.json
βββ .gitignore # Git ignore patterns
βββ README.md # This documentation
This project is licensed under the terms specified in the LICENSE file.
- Fork the repository
- Create your feature branch:
git checkout -b feature/amazing-feature - Commit your changes:
git commit -m 'Add amazing feature' - Push to the branch:
git push origin feature/amazing-feature - Open a Pull Request
- Built with krolaw/dhcp4 DHCP library
- Uses PacketFence logging framework
- Caching provided by go-cache