Writing a DHCP Server for Windows
I recently got fed up with an issue with the extremely lame Actiontec router that is build into every Qwest DSL modem. The issue basically has to do with how the NAT port forwarding was being performed. Packets sent to my external IP address from inside the local LAN were being dropped instead of forwarded. Since I host several services on a server inside my home network, and I have a laptop that is regularly connected inside and outside my home network this was a source of much frustration.
My initial workaround was to set up an internal DNS server. Using the Internet Software Consortium's BIND dns server, I created hard coded dns entries for my external domain name, so that my connections would go straight to my server's LAN IP rather than the public IP. All I had to do was configure my router to use my DNS server as the primary when clients requested one via DHCP. Unfortunately, after making this change, rebooting the router, running ipconfig /release and ipconfig /renew; low and behold, my client doesn't get the right DNS server (curse you Actiontec, curse you all to hell).
So then next step became setting up an alternate DHCP server that I could configure properly. Unfortunately my "server" runs Windows Vista Ultimate, and after doing some research online I concluded that unless you're running Linux/Unix/BSD, or Windows Server, your only choice is to use Internet Connection Sharing, which doesn't really meet my needs since it isn't very configurable, and it sets the box up as an internet gateway, which I don't want it to be. I considered using cygwin to build ISC DHCP, but I didn't really want to go to the trouble of running cygwin just so I could have a DHCP server. Finaly I decided, what the hell, there are only four different messages making up the critical piece of the DHCP protocol, surely I can implement this in less time than it's taken me to get this far. Five long nights later, as a sit writing this post, my DHCP server is up and running on my home network.
Writing it was an interesting experience. It was the first UDP socket application I'd written (I'd only written TCP apps before). The Wikipedia article was a huge help, and I'm indebted to the contributors who took the time to digram the message structures and sequences in a much more readable way than the RFCs. The did he final debugging by sniffing the traffic between a PC and my router during a DHCP exchange, which led me to discover that "network byte order" is big endian, not little endian like windows PCs, and that the "magic number" used to indicate the start of the options section is listed incorrectly on wikipedia (something I hope to remedy).
Anyway, I've created a community WinDhcp project on Codeplex, so if you want to see the source code, download the binaries, or contribute new code/bug fixes, please check it out.
I'm a Microsoft alumn since September 2007. At Microsoft I worked in the Connected Systems Division building applications using Windows Communication Foundation. I'm now working for a start up called Positronic in downtown Seattle.