123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- <?php
- namespace KeaBundle\Services;
- use HostBundle\Utils\HostStatus;
- use KeaBundle\Interfaces\KeaConfigInterface;
- class BaseKea implements KeaConfigInterface
- {
- /**
- * @var array
- */
- private $subnet4 = [];
- /**
- * @var array
- */
- private $hooks_libraries = [];
- /**
- * @var array
- */
- private $interfaces_config = [];
- /**
- * @param array $data
- * @param boolean $logging
- *
- * @return string
- */
- public function getConfig($data = array(), $logging = false)
- {
- if (isset($data['subnets'])) {
- $this->subnetConfig($data['subnets']);
- }
- if (isset($data['library'])) {
- $this->hooksLibrariesConfig($data);
- }
- return json_encode([
- array(
- 'arguments' => array(
- 'Dhcp4' => [
- 'control-socket' => $this->controlSocketConfig(),
- 'lease-database' => $this->leaseDatabaseConfig($data),
- 'hosts-database' => $this->leaseDatabaseConfig($data),
- 'subnet4' => $this->subnet4,
- 'hooks-libraries' => $this->hooks_libraries,
- 'interfaces-config' => $this->getInterfacesConfig(),
- ],
- 'Logging' => $this->loggingConfig(),
- ))], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
- }
- /**
- * @param array $subnets
- */
- private function subnetConfig($subnets)
- {
- foreach ($subnets as $subnet) {
- $pools = [];
- foreach ($subnet->getIpPool() as $pool) {
- $pools[] = [
- 'pool' => $pool->getFirstIp() . ' - ' . $pool->getLastIp(),
- ];
- }
- $hostType = $subnet->getAllowedHostType();
- $client_class = '';
- if ($hostType) {
- $client_class = $hostType->getShortname();
- }
- if ($subnet->getStatus() != HostStatus::STATE_NONE && $subnet->getStatus() != '') {
- if ($client_class != '') {
- $client_class .= '-';
- }
- $client_class .= $subnet->getStatus();
- }
- $subnetConf = [
- 'subnet' => $subnet->getAddress(),
- 'pools' => $pools,
- ];
-
- $nextServer = $subnet->getNextServer();
- if ($nextServer != '') {
- $subnetConf['next-server'] = $nextServer;
- }
-
- if ($client_class != '') {
- $subnetConf['client-class'] = $client_class;
- }
- $this->subnet4[] = $subnetConf;
- }
- }
- /**
- * @param array $data
- */
- private function hooksLibrariesConfig($data)
- {
- $dhcp = $data['dhcp'];
- $hosts = $data['hosts'];
- $hook = [
- 'library' => $data['library'],
- ];
- $dhcpModelParams = $dhcp ? $dhcp->getDhcpModel()->getData('parameters') : null;
- $option122 = isset($dhcpModelParams['option122']) ? $dhcpModelParams['option122'] : null;
- $ip = isset($dhcpModelParams['ip']) ? $dhcpModelParams['ip'] : null;
- if ($option122 && $ip) {
- $macs = [];
- foreach ($hosts as $host) {
- if ($host->getMac()) {
- $macs[] = $host->getMac();
- }
- }
- $hook['parameters']['option122'] = [
- $ip => $macs
- ];
- }
- $remote_id_map = isset($dhcpModelParams['remote-id-map']) ? $dhcpModelParams['remote-id-map'] : null;
- if ($remote_id_map) {
- $hostConfig = [];
- foreach ($hosts as $host) {
- if ($host->getMac()) {
- $mac = $host->getMac();
- $state = $host->getState();
- $shortname = $host->getHostType()->getShortname();
-
- $client_class = $state != HostStatus::STATE_NONE ? $state : $shortname;
-
- $hostConfig[$client_class][] = $mac;
- }
- }
- $hook['parameters']['remote-id-map'] = $hostConfig;
- }
- $this->hooks_libraries[] = $hook;
- }
- /**
- * @return array
- */
- private function loggingConfig()
- {
- return array(
- 'loggers' => array(
- array(
- 'debuglevel' => 0,
- 'name' => 'kea-dhcp4',
- 'output_options' => array(
- array(
- 'flush' => true,
- 'maxsize' => 10240000,
- 'maxver' => 1,
- 'output' => '/usr/local/var/log/kea-dhcp4.log'
- )
- ),
- 'severity' => 'INFO'
- )
- )
- );
- }
- /**
- * @return array
- */
- private function controlSocketConfig()
- {
- /* "control-socket": {"socket-name": "\/tmp\/kea-dhcp4-ctrl.sock","socket-type": "unix"} */
- return array(
- 'socket-name' => '/tmp/kea-dhcp4-ctrl.sock',
- 'socket-type' => 'unix'
- );
- }
- /**
- * @return array
- */
- private function leaseDatabaseConfig($data, $type = 'mysql')
- {
- if ($type == 'mysql') {
- $config = array(
- "host" => $data['db']['host'],
- "name" => $data['db']['name'],
- "user" => $data['db']['user'],
- "password" => $data['db']['password'],
- "type" => "mysql",
- );
- } elseif ($type == 'memfile') {
- $config = array(
- 'lfc-interval' => 3600,
- 'type' => 'memfile'
- );
- }
-
- return $config;
- }
- /**
- * @return array
- */
- private function getInterfacesConfig()
- {
- return ['interfaces' => ['*']];
- }
- }
|