PHP Classes

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    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 67%Total: 837 This week: 1All time: 4,144 This week: 560Up
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
Picture of Pat Winn
Name: Pat Winn is available for providing paid consulting. Contact Pat Winn .
Classes: 1 package by
Country: United States United States
Age: ???
All time rank: 2586354 in United States United States
Week rank: 411 Up48 in United States United States Up
Innovation award
Innovation award
Nominee: 1x

Example

<?php

/**
 * A simple test script to demonstrate how to use the dhcpLeaseQuery class.
 * Please read the note below first then edit the variables below as appropriate.
 * Then simply run: php LeaseQuery.php
 *
 * Note: ISC's dhcpd will *NOT* listen for LeaseQuery packets on the same system
 * it is running on. Not even on the local loopback. This is because the LeaseQuery
 * packet is intended to be sent from a true DHCP Relay such as a router or access server.
 * Thus, make certain that you are running this from a different system other than the
 * dhcp server itself and set the $gi variable to the IP of the box you are running this
 * from. (if on a multi-homed system, be sure to set it to the IP you will be sending
 * from).
 *
 * If this file looks funky to you, try setting tab stops=4.
 *
 * @author Pat Winn (pat@patwinn.com)
 * @date 10/16/2010
 * @version 1.0
 */

require_once('dhcpLeaseQuery.php');

$gi = "x.x.x.x"; // dhcp relay ip
$sv = "x.x.x.x"; // dhcp server ip to send query to
$ci = "x.x.x.x"; // IP address to ask server about

$lq = new dhcpLeaseQuery($gi, $sv);

if(
$lq->sendQuery($ci)) {
   
$lease = $lq->receive();
    if(
$lease !== false) {
        echo
"\nLease Info We Got Back:\n";
       
print_r($lease);
    }
}

?>


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:837
This week:1
All time:4,144
This week:560Up
User Ratings User Comments (1)
 All time
Utility:85%StarStarStarStarStar
Consistency:85%StarStarStarStarStar
Documentation:80%StarStarStarStarStar
Examples:80%StarStarStarStarStar
Tests:-
Videos:-
Overall:67%StarStarStarStar
Rank:471