DHCP implementation in PHP
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
docs Generated documentation Apr 28, 2015
src
tests Refactor of DHCP classes and tests Dec 11, 2016
.gitignore Added empty tests Dec 7, 2016
.travis.yml Continued refactor Dec 11, 2016
LICENSE Create LICENSE Apr 18, 2015
README.md
composer.json Try travis testing on php 7, updated composer Dec 9, 2016
composer.lock Try travis testing on php 7, updated composer Dec 9, 2016
phpcs.xml
phpunit.xml.dist Started code refactor and tests Dec 9, 2016

README.md

dhcp

DHCP implementation in PHP

Build Status

src/DHCP/ contains an implementation of DHCP protocol in PHP. It can be used to translate any DHCP packet from the network into PHP objects and vice versa.

src/DHCPServer/ is a simple DHCP Server using the DHCP implementation, using PostgreSQL as a backend for storing lease information. It also supports assigning static ip addresses to clients and was tested with default DHCP Clients on Windows 7, MacOS Sierra and Red Hat Linux 7 (dhclient).

Why?

Simply to show it can be done. This code is not intended for production use, but as a learning exercise. It may be an easy way for PHP developers to learn more about DHCP protocol.

For more details and to see the code in action, check out our blog post: mysteriouscode.io/blog/dhcp-implementation-in-php/

Running DHCPServer

To start the server, run php src/DHCPServer/server.php serve x.x.x.x/y password_to_postgresql Replace x.x.x.x/y with an IP and mask for the server (for example, 10.0.0.0/25) and password_to_postgres with a password to PostgreSQL.

To change database IP address, database name and user, go to src/DHCPServer/Postgresql.php line 12.

PostgreSQL tables for DHCPServer

Two PostreSQL tables are required and have to be created before the server can run:

dhcp_leases holds information about leases given to clients and their expiry dates:

CREATE TABLE dhcp_leases
(
  id serial NOT NULL,
  ip inet,
  mac macaddr,
  reason text,
  assigned_on timestamp without time zone,
  expires_on timestamp without time zone,
  CONSTRAINT dhcp_leases_id_pk PRIMARY KEY (id)
)
WITH (
  OIDS=FALSE
);

dhcp_static holds a list of static IP addresses for each client based on its MAC address:

CREATE TABLE dhcp_static
(
  id serial NOT NULL,
  ip inet,
  dns inet[],
  lease_time integer,
  mac macaddr,
  router inet,
  CONSTRAINT dhcp_static_id_pk PRIMARY KEY (id),
  CONSTRAINT dhcp_static_ip_pk UNIQUE (ip)
)
WITH (
  OIDS=FALSE
);