BaseKea.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. namespace KeaBundle\Services;
  3. use HostBundle\Utils\HostStatus;
  4. use KeaBundle\Interfaces\KeaConfigInterface;
  5. class BaseKea implements KeaConfigInterface
  6. {
  7. /**
  8. * @var array
  9. */
  10. private $subnet4 = [];
  11. /**
  12. * @var array
  13. */
  14. private $hooks_libraries = [];
  15. /**
  16. * @var array
  17. */
  18. private $interfaces_config = [];
  19. /**
  20. * @param array $data
  21. * @param boolean $logging
  22. *
  23. * @return string
  24. */
  25. public function getConfig($data = array(), $logging = false)
  26. {
  27. if (isset($data['subnets'])) {
  28. $this->subnetConfig($data['subnets']);
  29. }
  30. if (isset($data['library'])) {
  31. $this->hooksLibrariesConfig($data);
  32. }
  33. return json_encode([
  34. array(
  35. 'arguments' => array(
  36. 'Dhcp4' => [
  37. 'control-socket' => $this->controlSocketConfig(),
  38. 'lease-database' => $this->leaseDatabaseConfig($data),
  39. 'hosts-database' => $this->leaseDatabaseConfig($data),
  40. 'subnet4' => $this->subnet4,
  41. 'hooks-libraries' => $this->hooks_libraries,
  42. 'interfaces-config' => $this->getInterfacesConfig(),
  43. ],
  44. 'Logging' => $this->loggingConfig(),
  45. ))], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
  46. }
  47. /**
  48. * @param array $subnets
  49. */
  50. private function subnetConfig($subnets)
  51. {
  52. foreach ($subnets as $subnet) {
  53. $pools = [];
  54. foreach ($subnet->getIpPool() as $pool) {
  55. $pools[] = [
  56. 'pool' => $pool->getFirstIp() . ' - ' . $pool->getLastIp(),
  57. ];
  58. }
  59. $hostType = $subnet->getAllowedHostType();
  60. $client_class = '';
  61. if ($hostType) {
  62. $client_class = $hostType->getShortname();
  63. }
  64. if ($subnet->getStatus() != HostStatus::STATE_NONE && $subnet->getStatus() != '') {
  65. if ($client_class != '') {
  66. $client_class .= '-';
  67. }
  68. $client_class .= $subnet->getStatus();
  69. }
  70. $subnetConf = [
  71. 'subnet' => $subnet->getAddress(),
  72. 'pools' => $pools,
  73. ];
  74. $nextServer = $subnet->getNextServer();
  75. if ($nextServer != '') {
  76. $subnetConf['next-server'] = $nextServer;
  77. }
  78. if ($client_class != '') {
  79. $subnetConf['client-class'] = $client_class;
  80. }
  81. $this->subnet4[] = $subnetConf;
  82. }
  83. }
  84. /**
  85. * @param array $data
  86. */
  87. private function hooksLibrariesConfig($data)
  88. {
  89. $dhcp = $data['dhcp'];
  90. $hosts = $data['hosts'];
  91. $hook = [
  92. 'library' => $data['library'],
  93. ];
  94. $dhcpModelParams = $dhcp ? $dhcp->getDhcpModel()->getData('parameters') : null;
  95. $option122 = isset($dhcpModelParams['option122']) ? $dhcpModelParams['option122'] : null;
  96. $ip = isset($dhcpModelParams['ip']) ? $dhcpModelParams['ip'] : null;
  97. if ($option122 && $ip) {
  98. $macs = [];
  99. foreach ($hosts as $host) {
  100. if ($host->getMac()) {
  101. $macs[] = $host->getMac();
  102. }
  103. }
  104. $hook['parameters']['option122'] = [
  105. $ip => $macs
  106. ];
  107. }
  108. $remote_id_map = isset($dhcpModelParams['remote-id-map']) ? $dhcpModelParams['remote-id-map'] : null;
  109. if ($remote_id_map) {
  110. $hostConfig = [];
  111. foreach ($hosts as $host) {
  112. if ($host->getMac()) {
  113. $mac = $host->getMac();
  114. $state = $host->getState();
  115. $shortname = $host->getHostType()->getShortname();
  116. $client_class = $state != HostStatus::STATE_NONE ? $state : $shortname;
  117. $hostConfig[$client_class][] = $mac;
  118. }
  119. }
  120. $hook['parameters']['remote-id-map'] = $hostConfig;
  121. }
  122. $this->hooks_libraries[] = $hook;
  123. }
  124. /**
  125. * @return array
  126. */
  127. private function loggingConfig()
  128. {
  129. return array(
  130. 'loggers' => array(
  131. array(
  132. 'debuglevel' => 0,
  133. 'name' => 'kea-dhcp4',
  134. 'output_options' => array(
  135. array(
  136. 'flush' => true,
  137. 'maxsize' => 10240000,
  138. 'maxver' => 1,
  139. 'output' => '/usr/local/var/log/kea-dhcp4.log'
  140. )
  141. ),
  142. 'severity' => 'INFO'
  143. )
  144. )
  145. );
  146. }
  147. /**
  148. * @return array
  149. */
  150. private function controlSocketConfig()
  151. {
  152. /* "control-socket": {"socket-name": "\/tmp\/kea-dhcp4-ctrl.sock","socket-type": "unix"} */
  153. return array(
  154. 'socket-name' => '/tmp/kea-dhcp4-ctrl.sock',
  155. 'socket-type' => 'unix'
  156. );
  157. }
  158. /**
  159. * @return array
  160. */
  161. private function leaseDatabaseConfig($data, $type = 'mysql')
  162. {
  163. if ($type == 'mysql') {
  164. $config = array(
  165. "host" => $data['db']['host'],
  166. "name" => $data['db']['name'],
  167. "user" => $data['db']['user'],
  168. "password" => $data['db']['password'],
  169. "type" => "mysql",
  170. );
  171. } elseif ($type == 'memfile') {
  172. $config = array(
  173. 'lfc-interval' => 3600,
  174. 'type' => 'memfile'
  175. );
  176. }
  177. return $config;
  178. }
  179. /**
  180. * @return array
  181. */
  182. private function getInterfacesConfig()
  183. {
  184. return ['interfaces' => ['*']];
  185. }
  186. }