Skip to content
Jean-Paul Mikkers edited this page Apr 16, 2020 · 6 revisions

Welcome to the DHCPServer wiki!

How to embed a DHCP server

If you want to embed the DHCP server in your application, you only need the DHCPLibrary.dll that my .sln creates. Then to start up your own server from within your program, look at the code in DHCPResurrector.cs starting line 75. Essentially it constructs a server, configures it, then starts it.

Also don't forget to disable your windows firewall or configure some firewall rules for your application, the window firewall blocks DHCP UDP traffic by default.

I've added some comments below to clarify

    // construct dhcp server, give it a writable path on disk where it can write some administration file containing the active leases
    m_Server = new DHCPServer(Program.GetClientInfoPath(m_Config.Name,m_Config.Address));

    // set the endpoint where the DHCP server should be listening, this is the address of your local network card (e.g. 192.168.1.1)
    m_Server.EndPoint = new IPEndPoint(IPAddress.Parse(m_Config.Address),67);

    // typically 255.255.255.0
    m_Server.SubnetMask = IPAddress.Parse(m_Config.NetMask);
     
    // pool start range, e.g. 192.168.1.10
    m_Server.PoolStart = IPAddress.Parse(m_Config.PoolStart);
   
    // pool end range, e.g. 192.168.1.50
    m_Server.PoolEnd = IPAddress.Parse(m_Config.PoolEnd);

    // how long before an IP lease expires, 1 hour is a good default
    m_Server.LeaseTime = (m_Config.LeaseTime>0) ? TimeSpan.FromSeconds(m_Config.LeaseTime) : Utils.InfiniteTimeSpan;

    // 30 seconds
    m_Server.OfferExpirationTime = TimeSpan.FromSeconds(Math.Max(1, m_Config.OfferTime));

    // 576 bytes
    m_Server.MinimumPacketSize = m_Config.MinimumPacketSize;

    // configure a list of DHCP options, you can use an empty list to start with
    List <OptionItem> options = new List<OptionItem>();
    foreach(OptionConfiguration optionConfiguration in m_Config.Options)
    {
        options.Add(optionConfiguration.ConstructOptionItem());
    }
    m_Server.Options = options;

    // configure a list of IP address reservations, you can use an empty list to start with
    List<ReservationItem> reservations = new List<ReservationItem>();
    foreach (ReservationConfiguration reservationConfiguration in m_Config.Reservations)
    {
        reservations.Add(reservationConfiguration.ConstructReservationItem());
    }
    m_Server.Reservations = reservations;
                       

    // connect some event handles so you can listen to what the server is doing
    m_Server.OnStatusChange += server_OnStatusChange;
    m_Server.OnTrace += server_OnTrace;

    // and start it!
    m_Server.Start();

How to add firewall rules

If your DHCP server is running on a real network you should add a firewall rule to allow DHCP UDP traffic. This can be done by running the following commands as admin:

netsh advfirewall firewall add rule name="DotNetDHCPServer" dir=in action=allow protocol=UDP localport=67
netsh advfirewall firewall add rule name="DotNetDHCPServer" dir=out action=allow protocol=UDP localport=67 
Clone this wiki locally