.NET/C# DHCP Offer Monitor

Created on: 05.01.2011 3:57 PM
Edited on: 03.12.2012 5:38 PM

This page provides an overview and source code for a .NET/C# DHCP Discover/Offer checker utility.

Project Background


As part of my employment, I manage a large number of DHCP servers all across the world and while we have monitoring in place for things like the server and specific services, due to the broadcast nature of the protocol, sometimes that is not enough to know whether leases are being issued or whether another rogue server exists on the network.

For a long period of time, I was utilizing NAGIOS's check_dhcp binary to fill this need, though it was slightly modified to work independent of NAGIOS and provide some useful feedback. That was all well and good but the solution isn't portable to other, non-UNIX based operating systems.

I searched the web for days trying to find something similar to use on Windows (or even a cross-platform solution) but none really existed. At least, nothing that wasn't vendor specific. (Not every Windows server is running Microsoft DHCP.)

What I did stumble upon was a Powershell script (see Acknowledgments) that did most of what i wanted to do but I wanted it in a non-Powershell form.

Overview


What this program is, in a nutshell, is a small UDP client that will broadcast DHCPDISCOVER requests to the local subnet and report back any OFFERs that it received, where they came from, and what the lease time was. (It does not actually accept the offer and thus should not tie up any additional resources on your DHCP server.)

Sample output:

K:\bin>dhcp_check.exe
bytes sent: 243
bytes rec: 403
Added unique offer of 10.1.3.182 from 10.1.5.6 with lease time of 86400 seconds.


Using the offer list output, it's possible to check and make sure that you are indeed getting a lease from your DHCP server and if you see a different IP than what you expect, chances are it's a potentially rogue DHCP server also offering addresses.

While this program is written in C#, I was still targeting multiple operating systems (using Mono), so this program will work under Windows, Linux, and Solaris without issue. The binary is compiled with .NET4 but you can use the source and recompile if you'd prefer an earlier run time.

Disclaimer: Though I've tried to make this program as stable and generic as possible, I don't claim to be a C# guru, so you'll have to excuse any perceived poor coding techniques that might jump out at you. I've tested this on over 50 DHCP servers of varying vendors and operating systems and it works well across the board.


Acknowledgments


  • http://www.indented.co.uk/index.php/2010/02/17/dhcp-discovery/

  • This is a DHCP Discovery client written in Powershell, which helped get me started. I definitely lifted a few key pieces of code and converted them, so without this script, I probably wouldn't have gotten as far as I did.


    Source Code / Downloads


    Source code
  • http://rpiz.com/dl/dhcp_check_src.zip


  • Compiled .NET4 Binary
  • http://rpiz.com/dl/dhcp_check.exe



  • Contact


  • I can be reached at: rp [at] rpiz.com