123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375 |
- <?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 = [];
- /**
- * @var boolean
- */
- private $echo_client_id = false;
- /**
- * @var array
- */
- private $option_def = [
- [
- "name" => "subopt1",
- "code" => 3,
- "space" => "pcc",
- "type" => "binary",
- "record-types" => "",
- "array" => false,
- "encapsulate" => "",
- ],
- [
- "name" => "subopt2",
- "code" => 6,
- "space" => "pcc",
- "type" => "binary",
- "record-types" => "",
- "array" => false,
- "encapsulate" => "",
- ],
- [
- "name" =>"clabs",
- "code" => 122,
- "space" => "dhcp4",
- "type" => "empty",
- "array" => false,
- "record-types" => "",
- "encapsulate" => "pcc",
- ],
- ];
- private $client_classes = [];
- /**
- * @param array $data
- * @param boolean $logging
- *
- * @return string
- */
- public function getConfig($data = array(), $logging = false)
- {
- if (isset($data['subnets'])) {
- $this->subnetConfig($data['subnets'], $data['reservations']);
- }
- if (isset($data['library'])) {
- $this->hooksLibrariesConfig($data);
- }
- return json_encode([
- array(
- 'arguments' => array(
- 'Dhcp4' => [
- 'echo-client-id' => $this->echo_client_id,
- 'option-def' => $this->option_def,
- 'control-socket' => $this->controlSocketConfig(),
- 'lease-database' => $this->leaseDatabaseConfig($data),
- 'hosts-database' => $this->leaseDatabaseConfig($data),
- 'client-classes' => $this->client_classes,
- '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, $reservations)
- {
- foreach ($subnets as $subnet) {
- $pools = [];
- foreach ($subnet->getIpPool() as $pool) {
- $pools[] = [
- 'pool' => $pool->getFirstIp() . ' - ' . $pool->getLastIp(),
- ];
- }
- $hostType = $subnet->getAllowedHostType();
- $client_class = '';
- if ($hostType != 'Cablemodem') {
- $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,
- ];
- if(isset($reservations[$subnet->getId()])) {
- $subnetConf['reservations'] = $reservations[$subnet->getId()];
- }
- $nextServer = $subnet->getNextServer();
- if ($nextServer != '') {
- $subnetConf['next-server'] = $nextServer;
- }
- $filename = $subnet->getFilename();
- if ($filename != '') {
- $subnetConf['boot-file-name'] = $filename;
- }
- if ($client_class != '') {
- $subnetConf['client-class'] = $client_class;
- $this->client_classes [] = [
- 'name' => $client_class,
- ];
- }
- $netgroup = $subnet->getNetGroup();
- if ($netgroup && count($netgroup->getRelay())) {
- $relay = [];
- foreach ($netgroup->getRelay() as $ip) {
- $relay[] = $ip;
- }
- $subnetConf['relay'] = [
- 'ip-addresses' => $relay,
- ];
- }
- $this->setOptionData($subnet, $subnetConf);
- $this->subnet4[] = $subnetConf;
- }
- }
- /**
- * @param array $data
- */
- private function hooksLibrariesConfig($data)
- {
- $this->hooks_libraries = [
- [
- 'library' => '/opt/hooks/mysql/kea-hook-flowdat3-mysql.so',
- 'parameters' => [
- "host" => $data['db']['host'],
- "database" => $data['db']['database'],
- "user" => $data['db']['user'],
- "password" => $data['db']['password'],
- ]
- ],
- [
- "library" => "/opt/hooks/amqp/kea-hook-flowdat3.so",
- "parameters" => [
- "host" => $data['amqp']['host'],
- "user" => $data['amqp']['user'],
- "password" => $data['amqp']['password'],
- ],
- ],
- ];
- // $dhcp = $data['dhcp'];
- // $hosts = $data['hosts'];
- //
- // $hook = [
- // 'library' => $data['library'],
- // ];
- //
- // $dhcpModelParams = $dhcp && $dhcp->getDhcpModel() ? $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' => ['*']];
- }
- /**
- * @param Subnet $subnet
- *
- * @return array
- */
- private function setOptionData($subnet, &$subnetConf)
- {
- $option_data = [];
- $fields = [
- "routers",
- "time-servers" => ["always-send" => true],
- "log-servers",
- "time-offset",
- "broadcast-address" => ["always-send" => true],
- "domain-name-servers",
- ];
- foreach ($fields as $key => $options) {
- $name = is_array($options) ? $key : $options;
- $field = implode('', array_map(function ($piece) {
- return ucfirst($piece);
- }, explode('-', $name)));
- $method = "get{$field}";
- $value = $subnet->$method();
- if ($value) {
- $data = [
- 'name' => $name,
- 'data' => $subnet->$method(),
- ];
- if (is_array($options)) {
- $data = array_merge($data, $options);
- }
- $option_data[] = $data;
- }
- }
- $option_122_data = [];
- if ($subnet->getOption122ProvisioningServer() && $subnet->getOption122ProvisioningType()) {
- $option_122_data = [
- [
- "name" => "subopt1",
- "space" => "pcc",
- "code" => 3,
- "csv-format" => true,
- "data" => bin2hex($subnet->getOption122ProvisioningServer()),
- ],
- [
- "name" => "subopt2",
- "space" => "pcc",
- "code" => 6,
- "csv-format" => true,
- "data" => bin2hex($subnet->getOption122ProvisioningType()),
- ],
- [
- "name" => "clabs",
- "space" => "dhcp4",
- "code" => 122,
- "csv-format" => false,
- "data" => "",
- ],
- ];
- }
- $option_data = array_merge($option_data, $option_122_data);
- $subnetConf['option-data'] = $option_data;
- return $option_data;
- }
- }
|