Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Querying a DHCP server in C#

I need to get the mapping of MAC to IP stored on DHCP server, either through a program running on the server itself or preferably through a program running on one of the DHCP clients.

I understand netsh utility can be used to get the dump however i have not had much success with that.

Any working examples or hint on that?

I have admin rights on DHCP server

Edit

I dont want to use arp cache as that would require me to either broadcast ping (which is not allowed on windows) or ping the all possible ip address of subnet( which takes lot of time).

I am sure that DHCP server stores the mapping of MAC to IP, how can i use that information, to map MAC to IP address?

like image 516
Kazoom Avatar asked Feb 08 '10 21:02

Kazoom


1 Answers

You can use the DHCP Objects component from the Windows 2000 Resource Kit for this. Even though the component is hard to find, is made for Windows 2000, goes out of life support in July 2010 according to Microsoft and has very little documentation, it does work.

  1. Download the Resource Kit Tool named DHCP Objects from for example here if you can't find it at Microsoft. This will give you an .exe file that in turn will install the DHCP Objects component.
  2. Register the DHCPOBJS.DLL file with regsvr32 or create a COM+ Application for it. Which is applicable depends on how the COM component is going to be used on your system.
  3. Use the Type Library Importer tlbimp.exe to create a managed wrapper around DHCPOBJS.DLL now that it's registered by the system.
  4. In Visual Studio, add a reference to the managed wrapper. Its default generated name is DhcpObjects.dll.

Now you can write code like this against the component:

using DhcpObjects;
class Program {
    static void Main(string[] args) {
        var manager = new Manager();
        var server = dhcpmgr.Servers.Connect("1.2.3.4");
        // query server here
    }
}

The installer also provides a Windows Help File which contains further documentation on how to query and manipulate a DHCP server. The section "The Object Model" is quite helpful.

like image 116
bzlm Avatar answered Sep 26 '22 20:09

bzlm