PHP is not usually used to implement network services – and generally for good reasons. Not because it can’t be done, but rather because it’s not what it was meant to be used for.
Although, if you think that PHP simply can’t be used for anything other than serving your blog, think again. Because my DHCP implementation in PHP seems to work quite well!

Yes, DHCP in PHP. If you’re asking WHY?!, the answer is simple: to show it can be done and to learn. This was not meant to be used for anything production-related or anything other than learning. It may be a good way for PHP developers to explore DHCP protocol, find out how it works and experiment.
The code, available on GitHub, is divided into two namespaces:

DHCP – which is a basic representation of a DHCP packet and its options as PHP objects. The class DHCP\DHCPPacket can translate the any binary packet data captured through a network socket into objects. It can also be used to create a completely new packet, set its properties and convert it into a binary data to be sent back over the network.

This can be used to analyse network packets, create DHCP servers and clients. Not all DHCP options are implemented, but they can be easily added by creating an appropriate class inside DHCP\Options

DHCPServer implements a very simple DHCP Server, storing information about leases and clients inside PostgreSQL. It opens standard DHCP ports (67 and 68) for communication and will respond to any DHCPDISCOVER, DHCPREQUEST and DHCPRELEASE packets.
It also supports serving static IP addresses from a configured list (inside PostgreSQL) based on client’s MAC address.

This server was tested with dhclient with its standard (minimal) configuration on Red Hat 7 and also with default clients on Windows 7 and MacOS Sierra – all working!

To start the server, install dependencies with composer install, create PostgreSQL tables (see README.md) and run:

php server.php serve x.x.x.x/y password

inside src/DHCPServer/.
Replace x.x.x.x/y with IP address and mask you want the server to listen on (for example 10.0.0.1/25 and password with password to PostgreSQL (as said before, this is a very simple and basic implementation).

For a demonstration of the server in action, check out the video:

Limitations

As said before, PHP is not meant to be used to create network services, so it’s not best equipped to deal with them. Because of that (as you may have figured out from the examples above), it’s not possible for PHP to listen on a specific interface – it can only bind itself to a specific IP or broadcast on all interfaces. That means you cannot have any other DHCP server or client running on the machine as PHP will not be able to open the sockets it needs.

If you’re interested to learn more about DHCP, see those two basic RFCs: RFC 2131 – Dynamic Host Configuration Protocol and RFC 2132 – DHCP Options and BOOTP Vendor Extensions.


Remember, you can always use our RSS feed to keep up to date, follow us on Twitter or sign up to our newsletter below (no spam, promise):

Was this post helpful to you? Yes!


5 Comments

  1. Derk

    You can use
    socket_set_option($socket, IPPROTO_IP, IP_MULTICAST_IF, “eth0”);
    to broadcast to a specific interface

    • Paulina Budzon

      Thanks for mentioning this, I wonder how I missed that option, but good to know it’s there! Would you care creating a pull request on github with a change using that?

    • Derk

      Hi, I did not plan on making any changes to your code. I was looking if this was possible after playing with SSDP, and it seems it is. I might brew my own implementation sometime, I like to learn these kinds of things by reinventing the wheel.

    • Paulina Budzon

      I took a closer look at the options, it was a while since I worked on that code.
      The option you mentioned allows for directing multicast packets, not broadcasts. To receive multicast packets, the client first needs to subscribe to multicast group. Multicasts are sent to class D IP addresses – 244.0.0.0/4, while broadcasts are sent to broadcast address (255.255.255.255).
      In essence, every client on the network will receive broadcast packets, but if they want to receive multicast, they need to first subscribe to the group. As far as I know, there are no implementations of DHCP using multicast and the RFC specifies broadcast as the way of sending the packets. I couldn’t find a way to sent broadcasts to a specific interface in PHP (it’s possible to do in C I believe and using various tools, and “regular” DHCP servers do it), but if you do find one, please let me know!

  2. VincentPex

    It is my first time visiting your website and I’m very fascinated. Thanks for sharing and keep up 😉


Leave a comment