PHP Classes
elePHPant
Icontem

DHCP Lease Query: Send queries to a DHCP server

Recommend this page to a friend!
  Info   View files Example   View files View files (5)   DownloadInstall with Composer Download .zip   Reputation   Support forum (2)   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2016-05-13 (1 year ago) RSS 2.0 feedStarStarStarStar 73%Total: 770 All time: 4,277 This week: 619Up
Version License PHP version Categories
dhcp-lease-query 1.5GNU General Publi...5.1.2Networking, PHP 5
Description Author

This class can be used to Send queries to a DHCP server.

It can send UDP packets to a DHCP server to retrieve all DHCP lease related information.

Innovation Award
PHP Programming Innovation award nominee
December 2010
Number 3


Prize: One downloadable copy of PhpED Professional
DHCP is a protocol for connecting computers to the Internet via the network of a provider. Computer IP addresses, routes, DNS and more are obtained automatically using DHCP.

This class can query a DHCP server to determine which computers are connected to the Internet via the DHCP server network.

Manuel Lemos
Name: Pat Winn <contact>
Classes: 1 package by
Country: United States United States
Innovation award
Innovation award
Nominee: 1x

Details
This code was written as part of a syslog replacement daemon which I wrote
in PHP. 

The ISP I work for made the decision to migrate away from using
Radius to DHCP. Due to the change, we needed a good way to be able to
capture incoming "subscriber session"/circuit information for DSL subscribers. 
In the past we had always done session logging via Radius -> MySQL. 
Once you switch to DHCP, there is no sense of user authentication if doing 
IP assignment dynamically so there is no real way of knowing who is consuming 
your IP's.

This also presents a problem when big brother comes in and asks what 
subscriber had a given IP address on a certain date/time. 

In order to create a similar means of session logging that could actually
be mapped back to a subscriber, I came up with the idea of putting code
into the dhcpd.conf file to build a syslog string upon capturing a 
DHCPREQUEST with option-82 circuit id info in it. The syslog string is
formed in such a way that it contains the IP address being handed out,
the MAC address that is requesting it, the circuit id in the DSLAM involved,
etc. Some info we wanted to capture for the session record such as the 
lease timers wasn't available via the packets being received so it became
necessary to query the dhcpd daemon for the additional info before a
record could be placed into MySQL. 

After experimenting with the OMAPI tools for ISC's dhcpd, and seeing that 
it occasionally hung dhcpd, I ruled out using that to query the dhcpd with.

I couldn't find anything out there already written to do this in PHP so I set out
to reading RFC's on the DHCP protocol and set out to write it myself.
This code is the product of that work. 

The end result/model looks something like this:

dsl_subsriber -> DSLAM -> ROUTER/RHCP_RELAY -> dhcpd -> rsyslog -> phpSyslogd -> MySQL

When the dhcpd sends out the string with the session info in it, it is formatted
in name=value pairs which are delimited by |'s. It sends it to the local syslog
daemon, which in my case, is rsyslogd. rsyslogd is far more robust than the
syslogd that is distributed with RedHat. rsyslogd then forwards the syslog entry to
a remote syslog daemon...my phpSyslogd. When it receives a syslog message that 
starts with "opt82=1", it parses the line, grabs the required info, then queries
the dhcpd for any additional/needed info..after which it builds a query string and
either inserts it into MySQL as a new subscriber session or updates their existing
one if it finds an active match on the MAC/IP pairing. 

You get the idea.  ;-)

There is of course more than just that to it, but that is the basics of the overall
design/model. This code is but one (albeit, crucial) part of it.

Please be sure to run this code somewhere other than your DHCP server itself.
As is noted in the code, dhcpd will *NOT* listen on the local box for incoming
DHCPLEASEQUERY packets. Those are intended for use by actual DHCP Relays such
as a router or access server so it expects them to come in from elsewhere.

There could no doubt be bugs in this code. I don't know of any at this point
but for all I know it could eat your house, breed with your dog and maybe even
build a taco stand for all I know. Point being, I take no responsibility for what
it may do to your system(s). USE AT YOUR OWN RISK!!!

Aside from that, feel free to send comments, patches, ideas, etc.
to me at pat@patwinn.com and..enjoy!

--Pat

HOW TO USE:

As stated in the code, simply edit the variables in the LeaseQuery.php script,
then from a box other than the dhcp server, run it as: php LeaseQuery.php.
That's it!
  Files folder image Files  
File Role Description
Accessible without login Plain text file dhcpAPI.php Aux. Some basic DHCP related constants as an API
Plain text file dhcpLeaseQuery.php Class The main DHCP Lease Query Class
Accessible without login Plain text file exampleOutput.txt Output Sample return value/output
Accessible without login Plain text file LeaseQuery.php Example Sample Code - How To Use
Accessible without login Plain text file README.txt Doc. Class Instructions/Information

 Version Control Unique User Downloads Download Rankings  
 0%
Total:770
This week:0
All time:4,277
This week:619Up
User Ratings User Comments (1)
 All time
Utility:93%StarStarStarStarStar
Consistency:93%StarStarStarStarStar
Documentation:87%StarStarStarStarStar
Examples:87%StarStarStarStarStar
Tests:-
Videos:-
Overall:73%StarStarStarStar
Rank:183