A DHCP Server in PHP, why not?

Over this past long Labor Day weekend I decided to attempt something just plain silly: write a DHCP server in PHP. You may be asking yourself what would ever make me think doing such a thing in PHP would be a good idea? I’ll tell you: why not? Also, when it comes to languages which run on a server — well PHP is the best I’ve got. I know enough Python, C/C++, Ruby, Java, Obj-C, blah, blah to be dangerous — but PHP and Javascript are my tried and true loves. So I figured why not. And then the question of why a DHCP server? There was an idea on Computer Science House last year to do a project to write a DHCP server which better fits the way we want to use our network (and the project never really went anywhere). I wanted to see if in a weekend I could do more then they did in weeks / months.

The objective for the weekend was to get a server which could accept, decode, process and respond to DHCP requests on the network. My first goal wasn’t to build a robust server — really it was to learn something new and see if it could be reasonably done. After pretty much two days of solid work (Saturday and Sunday) I had a working server. It assigns the same IP to every machine that requests an IP — but I was able to get a PHP program to handle sockets and UDP packet parsing to read and write DHCP packets correctly. That was arguably the hard part. The rest is just standard application logic.

The code is available on GitHub here: http://github.com/adinardi/phpdhcp.

What’s the plan for the future? I’m thinking of at least wrapping up the packet parsing class and have that available as a library for people to use. I don’t think it’s worth my time to finish the rest of the server. Being that I’ve already graduated and this is a critical service — there’s a good chance I won’t have the time to support it when they need it. As such it probably isn’t the best idea to actually put it in to production use there. Of course, if there are people interested in seeing this project completed — by all means let me know. I had planned on making the DHCP server part have a plug in system of changing the storage system backing the server so that it could run off a database and do things that the normal ISC DHCP server can’t be configured to do. When there’s no strict config (just an API that calls your storage system and asks for the info for some MAC address)  you can make your system return whatever IP / config that makes sense for you.

CSH was interested in a system where you could have statically and dynamically assigned IPs in the same pool. We’re running out of IPs so if we can have static assignments and dynamic mixed you can conserve space by not having to segment the network arbitrarily.

I think I need to get back to doing crazy things with javascript. Did you know there’s an implementation of the javascript runtime in javascript? Yeah.

2 Responses to “A DHCP Server in PHP, why not?”

  1. Jon Parise Says:

    This is much less retarded than the caching DNS/WINS server that I wrote in Visual Basic for my network programming class.

    http://www.csh.rit.edu/~jon/projects/caching_dns/

  2. DaveRandom Says:

    This is brilliant – exactly what I was looking for! I must admit I haven’t tested it yet, but it sound like exactly the thing I need – a basic framework for allowing PHP to “parse” DHCP requests.

    And here’s the (potential) use it can be put too… I have written a (very much work in progress) provisioning server, designed to be used with IP telephone handsets but i suppose it could really be used with anything that requests a configuration file. The body of this server is written in PHP – orginally running under an Apache server.

    Then I was asked to make this work with a phone that only supports FTP/TFTP for provisioning purposes. After much hunting on the internet, I was unable to find an FTP server that could adequately create files dynamically on the server side as they are requested, so I did a similar thing to the process described above, and spent a weekend building a rudimentary FTP server in PHP, so I could use the same routines I had written to be used over HTTP. The next weekend I got bored, and did the same thing, only this time building an HTTP server that is pure PHP, and does not require Apache/IIS… Slightly pointless I know, but I want the application to be as standalone as possible. Also, this removes the added complication of having to write some very complex mod_rewrite rules to distinguish between devices (and reorganise Polycom’s bloody stupid directory layout for the provisioning data).

    Then I realised the majority of such devices will use DHCP option 66 to determine where the boot server can be found, but how do you only respond with option 66, not a full configuration? And how do you only respond to the devices in the database, and not every DHCP request you recieve?

    Using this, thats how…

    :-)

Leave a Reply