nanoserv
[ class tree: nanoserv ] [ index: nanoserv ] [ all elements ]

Source for file NS_DHCP_Handler.php

Documentation is available at NS_DHCP_Handler.php

  1. <?php
  2.  
  3. /**
  4.  *
  5.  * nanoserv handlers - DHCP protocol handler
  6.  * 
  7.  * Copyright (C) 2004-2010 Vincent Negrier aka. sIX <six@aegis-corp.org>
  8.  * 
  9.  * This library is free software; you can redistribute it and/or
  10.  * modify it under the terms of the GNU Lesser General Public
  11.  * License as published by the Free Software Foundation; either
  12.  * version 2.1 of the License, or (at your option) any later version.
  13.  * 
  14.  * This library is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17.  * Lesser General Public License for more details.
  18.  * 
  19.  * You should have received a copy of the GNU Lesser General Public
  20.  * License along with this library; if not, write to the Free Software
  21.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  22.  *
  23.  * @package nanoserv
  24.  * @subpackage Handlers
  25.  */
  26.  
  27. /**
  28.  * Require the nanoserv core
  29.  */
  30. require_once "nanoserv-compat/nanoserv.php";
  31.  
  32. /**
  33.  * DHCP message decoding exception
  34.  *
  35.  * @package nanoserv
  36.  * @subpackage Handlers
  37.  */
  38. class NS_DHCP_Exception extends NS_Exception {
  39.  
  40. }
  41.  
  42. /**
  43.  * DCHP message options parser and builder class
  44.  *
  45.  * @package nanoserv
  46.  * @subpackage Handlers
  47.  */
  48. class NS_DHCP_Options {
  49.  
  50.     const MAGIC_COOKIE = 0x63825363;
  51.  
  52.     public $subnet_mask;
  53.     public $time_offset;
  54.     public $routers;
  55.     public $time_servers;
  56.     public $dns_servers;
  57.     public $log_servers;
  58.     public $hostname;
  59.     public $domain_name;
  60.  
  61.     public $address_request;
  62.     public $address_time;
  63.     public $option_overload;
  64.     public $dhcp_msg_type;
  65.     public $dhcp_server_id;
  66.     public $parameter_list;
  67.     public $dhcp_message;
  68.     public $renewal_time;
  69.     public $rebinding_time;
  70.     public $server_name;
  71.     public $bootfile_name;
  72.     public $client_identifier;
  73.     public $client_fqdn;
  74.     public $vendor_class_id;
  75.     public $parameter_request_list;
  76.  
  77.     public $unhandled = array();
  78.  
  79.     public function __construct($data NULL{
  80.  
  81.         if (isset($data)) {
  82.  
  83.             list(,$magicunpack("N"substr($data04));
  84.  
  85.             if ($magic !== self::MAGIC_COOKIEthrow new NS_DHCP_Exception("unable to decode options, wrong magic cookie 0x" dechex($magic));
  86.  
  87.             $cur 4;
  88.             $optlen strlen($data);
  89.  
  90.             while ($cur $optlen{
  91.  
  92.                 $tmp unpack("Ccode/Clen"substr($data$cur2));
  93.  
  94.                 $code $tmp["code"];
  95.                 $len $tmp["len"];
  96.                 
  97.                 if ($code === 0{
  98.  
  99.                     $cur++;
  100.                     continue;
  101.  
  102.                 else if ($code === 255{
  103.  
  104.                     break;
  105.  
  106.                 }
  107.  
  108.                 $opt substr($data$cur 2$len);
  109.                 
  110.                 switch ($code{
  111.  
  112.                     case 12:
  113.                     $this->hostname = $opt;
  114.                     break;
  115.                     
  116.                     case 50:
  117.                     list(,$tmpunpack("N"$opt);
  118.                     $this->address_request = long2ip($tmp);
  119.                     break;
  120.  
  121.                     case 53:
  122.                     list(,$this->dhcp_msg_typeunpack("C"$opt);
  123.                     break;
  124.  
  125.                     case 54:
  126.                     list(,$tmpunpack("N"$opt);
  127.                     $this->dhcp_server_id = long2ip($tmp);
  128.                     break;
  129.  
  130.                     case 55:
  131.                     $this->parameter_request_list = unpack("C*"$opt);
  132.                     break;
  133.                     
  134.                     case 60:
  135.                     $this->vendor_class_id = $opt;
  136.                     break;
  137.  
  138.                     case 61:
  139.                     $this->client_identifier = bin2hex($opt);
  140.                     break;
  141.                     
  142.                     case 81:
  143.                     $this->client_fqdn = $opt;
  144.                     break;
  145.  
  146.                     default:
  147.                     $this->unhandled[array("code" => $code"opt" => bin2hex($opt));
  148.                     break;
  149.  
  150.                 }
  151.             
  152.                 $cur += $len 2;
  153.             
  154.             }
  155.             
  156.         }
  157.     
  158.     }
  159.  
  160.     static public function Decode($data{
  161.  
  162.         return new self($data);
  163.     
  164.     }
  165.  
  166.     static public function Encode({
  167.  
  168.     }
  169.  
  170. }
  171.  
  172. /**
  173.  * DHCP message parser and builder class
  174.  *
  175.  * @package nanoserv
  176.  * @subpackage Handlers
  177.  */
  178. class NS_DHCP_Message {
  179.  
  180.     const BOOTP_REQUEST = 1;
  181.     const BOOTP_REPLY = 2;
  182.  
  183.     const HTYPE_ETHERNET = 1;
  184.     
  185.     const DHCP_DISCOVER = 1;
  186.     const DHCP_OFFER = 2;
  187.     const DHCP_REQUEST = 3;
  188.     const DHCP_DECLINE = 4;
  189.     const DHCP_ACK = 5;
  190.     const DHCP_NAK = 6;
  191.     const DHCP_RELEASE = 7;
  192.     const DHCP_INFORM = 8;
  193.     
  194.     public $op;
  195.     public $htype;
  196.     public $hlen;
  197.     public $hops;
  198.     public $xid;
  199.     public $secs;
  200.     public $flags;
  201.     public $ciaddr;
  202.     public $yiaddr;
  203.     public $siaddr;
  204.     public $giaddr;
  205.     public $chaddr;
  206.     public $sname;
  207.     public $file;
  208.     public $options;
  209.     
  210.     public function __construct($data NULL{
  211.  
  212.         if (isset($data)) {
  213.  
  214.             $tmp unpack("Cop/Chtype/Chlen/Chops/Nxid/nsecs/nflags/Nciaddr/Nyiaddr/Nsiaddr/Ngiaddr"$data);
  215.             
  216.             $this->op = $tmp["op"];
  217.             $this->htype = $tmp["htype"];
  218.             $this->hlen = $tmp["hlen"];
  219.             $this->hops = $tmp["hops"];
  220.             $this->xid = $tmp["xid"];
  221.             $this->secs = $tmp["secs"];
  222.             $this->flags = $tmp["flags"];
  223.             $this->ciaddr = long2ip($tmp["ciaddr"]);
  224.             $this->yiaddr = long2ip($tmp["yiaddr"]);
  225.             $this->siaddr = long2ip($tmp["siaddr"]);
  226.             $this->giaddr = long2ip($tmp["giaddr"]);
  227.             $this->chaddr = bin2hex(substr($data2816));
  228.             $this->sname = substr($data4464);
  229.             $this->file = substr($data108128);
  230.  
  231.             $this->options = NS_DHCP_Options::Decode(substr($data236));
  232.  
  233.         else {
  234.  
  235.             $this->options = new NS_DHCP_Options();
  236.  
  237.         }
  238.     
  239.     }
  240.  
  241.     public function Op_To_String({
  242.  
  243.         switch ($this->op{
  244.  
  245.             case self::BOOTP_REQUEST:    return "BOOTPREQUEST";
  246.             case self::BOOTP_REPLY:        return "BOOTPREPLY";
  247.             default:                    return "unknown";
  248.         
  249.         }
  250.     
  251.     }
  252.  
  253.     public function Htype_To_String({
  254.  
  255.         switch ($this->htype{
  256.  
  257.             case self::HTYPE_ETHERNET:    return "Ethernet";
  258.             default:                    return "unknown";
  259.         
  260.         }
  261.     
  262.     }
  263.  
  264.     public function Msg_Type_To_String({
  265.  
  266.         switch ($this->options->dhcp_msg_type{
  267.  
  268.             case self::DHCP_DISCOVER:    return "DHCPDISCOVER";
  269.             case self::DHCP_OFFER:        return "DHCPOFFER";
  270.             case self::DHCP_REQUEST:    return "DHCPREQUEST";
  271.             case self::DHCP_DECLINE:    return "DHCPDECLINE";
  272.             case self::DHCP_ACK:        return "DHCPACK";
  273.             case self::DHCP_NAK:        return "DHCPNAK";
  274.             case self::DHCP_RELEASE:    return "DHCPRELEASE";
  275.             case self::DHCP_INFORM:        return "DHCPINFORM";
  276.             default:                    return "unknown";
  277.         
  278.         }
  279.     
  280.     }
  281.     
  282.     static public function Decode($data{
  283.  
  284.         return new self($data);
  285.  
  286.     }
  287.  
  288.     static public function Encode({
  289.  
  290.     }
  291.  
  292. }
  293.  
  294. /**
  295.  * DHCP protocol handler handler class
  296.  *
  297.  * @package nanoserv
  298.  * @subpackage Handlers
  299.  */
  300. abstract class NS_DHCP_Handler extends NS_Datagram_Handler {
  301.  
  302.     public function on_Read($from$data{
  303.  
  304.         $this->on_DHCP_Message($fromNS_DHCP_Message::Decode($data));
  305.  
  306.     }
  307.  
  308.     /**
  309.      * Event called when a DHCP message is received
  310.      *
  311.      * @param string $from ip address and port of the sending side
  312.      * @param NS_DHCP_Message $msg 
  313.      */
  314.     abstract public function on_DHCP_Message($fromNS_DHCP_Message $msg);
  315.  
  316. }
  317.  
  318. ?>

Documentation generated on Wed, 30 Nov 2011 22:03:24 +0100 by phpDocumentor 1.4.3