ipam/load-networks.php
2025-05-27 02:08:54 +02:00

78 lines
3.1 KiB
PHP

<?php
require_once "load-conf.php";
$networks = [];
if (isset($conf["dhcp-optsfile"]) && file_exists($conf["dhcp-optsfile"])) {
foreach(file($conf["dhcp-optsfile"]) as $line) {
if (preg_match(
"/^(?:tag:(?<tag>$namePtn),)?option:(?<option>$namePtn),(?<value>[^#\r\n]*)$commentPtn$/",
$line, $option
)) {
if (!isset($networks[$option["tag"] ?? "default"])) $networks[$option["tag"] ?? "default"] = [];
$networks[$option["tag"] ?? "default"][$option["option"]] = $option["value"];
}
}
}
if (isset($conf["conf-file"]) && file_exists($conf["conf-file"])) {
foreach(file($conf["conf-file"]) as $line) {
if (preg_match(
"/^dhcp-range=(?:(?:tag|set):(?<tag>$namePtn),)?(?<start_addr>$ipPtn),(?:(?<end_addr>$ipPtn)|$modesPtn)(?:,(?<netmask>$ipPtn)(?:,$ipPtn)?)?(?:,(?<lease_time>$timePtn|infinite))?$commentPtn$/",
$line, $range, PREG_UNMATCHED_AS_NULL
)) {
if (!isset($networks[$range["tag"] ?? "default"])) $networks[$range["tag"] ?? "default"] = [];
$networks[$range["tag"] ?? "default"]["dhcp-range"] = [
"start_addr" => $range["start_addr"],
"end_addr" => $range["end_addr"],
"netmask" => $range["netmask"],
"lease_time" => $range["lease_time"],
];
}
}
}
$long_mask = [
0b00000000000000000000000000000000,
0b10000000000000000000000000000000,
0b11000000000000000000000000000000,
0b11100000000000000000000000000000,
0b11110000000000000000000000000000,
0b11111000000000000000000000000000,
0b11111100000000000000000000000000,
0b11111110000000000000000000000000,
0b11111111000000000000000000000000,
0b11111111100000000000000000000000,
0b11111111110000000000000000000000,
0b11111111111000000000000000000000,
0b11111111111100000000000000000000,
0b11111111111110000000000000000000,
0b11111111111111000000000000000000,
0b11111111111111100000000000000000,
0b11111111111111110000000000000000,
0b11111111111111111000000000000000,
0b11111111111111111100000000000000,
0b11111111111111111110000000000000,
0b11111111111111111111000000000000,
0b11111111111111111111100000000000,
0b11111111111111111111110000000000,
0b11111111111111111111111000000000,
0b11111111111111111111111100000000,
0b11111111111111111111111110000000,
0b11111111111111111111111111000000,
0b11111111111111111111111111100000,
0b11111111111111111111111111110000,
0b11111111111111111111111111111000,
0b11111111111111111111111111111100,
0b11111111111111111111111111111110,
0b11111111111111111111111111111111,
];
foreach ($networks as $tag => &$options) {
$ip_addr = ip2long($network["router"] ?? $options["dhcp-range"]["start_addr"]);
$netmask = ip2long($network["netmask"] ?? $options["dhcp-range"]["netmask"]);
$hostmask = 0xFFFFFFFF & ~$netmask;
$options["network-addr"] = $ip_addr & $netmask;
$options["broadcast"] = $ip_addr | $hostmask;
$options["cidr_mask"] = array_search($netmask, $long_mask);
}