Browse Source

Update kea config service

Guillermo Espinoza 6 năm trước cách đây
mục cha
commit
4f4f6f990a

+ 75 - 10
src/KeaBundle/Services/BaseKea.php

@@ -100,6 +100,7 @@ class BaseKea implements KeaConfigInterface
 
     /**
      * @param array $subnets
+     * @param array $reservations
      */
     private function subnetConfig($subnets, $reservations)
     {
@@ -112,10 +113,11 @@ class BaseKea implements KeaConfigInterface
             }
 
             $hostType = $subnet->getAllowedHostType();
-            $client_class = '';
+            $client_class = 'cm';
             if ($hostType != 'Cablemodem') {
                 $client_class = $hostType->getShortname();
             }
+
             if ($subnet->getStatus() != HostStatus::STATE_NONE && $subnet->getStatus() != '') {
                 if ($client_class != '') {
                     $client_class .= '-';
@@ -231,29 +233,33 @@ class BaseKea implements KeaConfigInterface
         //     }
         //     $hook['parameters']['remote-id-map'] = $hostConfig;
         // }
-
+        //
         // $this->hooks_libraries[] = $hook;
     }
 
     /**
+     * @param int $debugLevel
+     * @param string $severity
+     * @param string $output
+     *
      * @return array
      */
-    private function loggingConfig()
+    private function loggingConfig($debugLevel = 0, $severity = 'INFO', $output = '/usr/local/var/log/kea-dhcp4.log')
     {
         return array(
             'loggers' => array(
                 array(
-                    'debuglevel' => 0,
+                    'debuglevel' => $debugLevel,
                     'name' => 'kea-dhcp4',
                     'output_options' => array(
                         array(
                             'flush' => true,
                             'maxsize' => 10240000,
                             'maxver' => 1,
-                            'output' => '/usr/local/var/log/kea-dhcp4.log'
+                            'output' => $output
                         )
                     ),
-                    'severity' => 'INFO'
+                    'severity' => $severity
                 )
             )
         );
@@ -272,6 +278,9 @@ class BaseKea implements KeaConfigInterface
     }
 
     /**
+     * @param array $data
+     * @param string $type
+     *
      * @return array
      */
     private function leaseDatabaseConfig($data, $type = 'mysql')
@@ -304,6 +313,7 @@ class BaseKea implements KeaConfigInterface
 
     /**
      * @param Subnet $subnet
+     * @param array $subnetConf
      *
      * @return array
      */
@@ -317,6 +327,8 @@ class BaseKea implements KeaConfigInterface
             "time-offset",
             "broadcast-address" => ["always-send" => true],
             "domain-name-servers",
+            "domain-name",
+            "tftp-server-name",
         ];
 
         foreach ($fields as $key => $options) {
@@ -340,20 +352,32 @@ class BaseKea implements KeaConfigInterface
 
         $option_122_data = [];
         if ($subnet->getOption122ProvisioningServer() && $subnet->getOption122ProvisioningType()) {
+            $provisioningServer = $subnet->getOption122ProvisioningServer();
+            $binary = array_map(function($piece) {
+                return self::int2hex(strlen($piece)) . self::string2hex($piece);
+            }, explode('.', $provisioningServer));
+            $provisioningServerBinary = self::int2hex(0) . implode('', $binary) . self::int2hex(0);
+
+            $provisioningType = $subnet->getOption122ProvisioningType();
+            $binary = array_map(function($piece) {
+                return self::int2hex(strlen($piece)) . self::string2hex($piece);
+            }, explode('.', $provisioningType));
+            $provisioningTypeBinary = '' . implode('', $binary) . self::int2hex(0);
+
             $option_122_data = [
                 [
                     "name" => "subopt1",
                     "space" => "pcc",
                     "code" => 3,
-                    "csv-format" => true,
-                    "data" => bin2hex($subnet->getOption122ProvisioningServer()),
+                    "csv-format" => false,
+                    "data" => $provisioningServerBinary,
                 ],
                 [
                     "name" => "subopt2",
                     "space" => "pcc",
                     "code" => 6,
-                    "csv-format" => true,
-                    "data" => bin2hex($subnet->getOption122ProvisioningType()),
+                    "csv-format" => false,
+                    "data" => $provisioningTypeBinary,
                 ],
                 [
                     "name" => "clabs",
@@ -372,4 +396,45 @@ class BaseKea implements KeaConfigInterface
         return $option_data;
     }
 
+    /**
+     * @param string $str
+     *
+     * @return string
+     */
+    public static function string2hex($str)
+    {
+        $hex = '';
+        for ($iter = 0; $iter < strlen($str); $iter++) {
+            $hex .= dechex(ord($str[$iter]));
+        }
+
+        return $hex;
+    }
+
+    /**
+     * @param int $int
+     *
+     * @return string
+     */
+    public static function int2hex($int)
+    {
+        if ($int < 0) {
+            $hex = substr(dechex($int), -8);
+        } else {
+            $hex = base_convert($int, 10, 16);
+            // TODO: This is a quick hack. Fix this.
+            if (strlen($hex) == 1) {
+                $hex = '0' . $hex;
+            } else if (strlen($hex) == 3) {
+                $hex = '0' . $hex;
+            } else if (strlen($hex) == 5) {
+                $hex = '000' . $hex;
+            } else if (strlen($hex) == 7) {
+                $hex = '0' . $hex;
+            }
+        }
+
+        return '' . $hex;
+    }
+
 }

+ 8 - 8
src/KeaBundle/Services/KeaConfigService.php

@@ -274,34 +274,34 @@ class KeaConfigService
         $subnets = $this->subnetRepository->findAll();
         $reservations = [];
 
-        if(is_null($subnets) || empty($subnets)) {
+        if (is_null($subnets) || empty($subnets)) {
             return $reservations;
         }
 
         foreach ($subnets as $subnet) {
-
             foreach ($subnet->getIpPool() as $pool) {
-
                 $start = ip2long($pool->getFirstIp());
                 $end = ip2long($pool->getLastIp());
 
                 $qb = $this->em->createQueryBuilder();
-
                 $qb->select(array('h'))
                     ->from('HostBundle:Host', 'h')
                     ->where(
                         $qb->expr()->andX(
                             $qb->expr()->isNotNull('h.ipv4Address'),
-                            $qb->expr()->between('h.ipv4Address',$start,$end)
+                            $qb->expr()->between('h.ipv4Address', $start, $end)
                         )
                     );
 
                 $hosts = $qb->getQuery()->getResult();
 
                 $results = array();
-                if($hosts) {
-                    foreach($hosts as $host) {
-                        $results[] = ['hw-address' => "{$host->getMac()}",'ip-address' => "{$host->getFixedAddress()}"];
+                if ($hosts) {
+                    foreach ($hosts as $host) {
+                        $results[] = [
+                            'hw-address' => "{$host->getMac()}",
+                            'ip-address' => "{$host->getFixedAddress()}"
+                        ];
                     }
 
                     $reservations[$subnet->getId()] = $results;