function renderNetCellForAlloc($cell, $needed_mask = NULL) { if (empty($cell['spare_ranges']) and $cell['kidc'] == 0 and $cell['mask'] < 31) { $cell['spare_ranges'][$cell['mask'] + 1][] = $cell['ip_bin']; $cell['spare_ranges'][$cell['mask'] + 1][] = ip_last($cell) & ip4_mask($cell['mask'] + 1); } $ranges = array_keys($cell['spare_ranges']); sort($ranges, SORT_NUMERIC); foreach ($ranges as &$range) { $suffix = count($cell['spare_ranges'][$range]) <= 1 ? '' : '<small> x ' . count($cell['spare_ranges'][$range]) . '</small>'; $range = '<a href="' . makeHref(array('page' => 'ipv4space', 'tab' => 'newrange', 'set-prefix' => ip_format($cell['spare_ranges'][$range][0]) . '/' . $range)) . '">/' . $range . '</a>' . $suffix; } $spare_cidr = NULL; if (isset($needed_mask)) { for ($i = $needed_mask; $i > 0; $i--) { if (!empty($cell['spare_ranges'][$i])) { $spare_cidr = ip_format($cell['spare_ranges'][$i][0]) . '/' . $needed_mask; break; } } } echo "<table class='slbcell vscell'><tr><td rowspan=3 width='5%'>"; printImageHREF('NET'); echo '</td>'; echo "<td><a href='index.php?page={$cell['realm']}&id={$cell['id']}'>{$cell['ip']}/{$cell['mask']}</a>"; echo getRenderedIPNetCapacity($cell); echo '</td></tr>'; echo "<tr><td>"; if (strlen($cell['name'])) { echo "<strong>" . niftyString($cell['name']) . "</strong>"; } else { echo "<span class=sparenetwork>no name</span>"; } // render VLAN echo '<div class="vlan">' . implode(', ', $ranges) . '</div>'; renderNetVLAN($cell); echo "</td></tr>"; echo '<tr><td>'; echo count($cell['etags']) ? "<small>" . serializeTags($cell['etags']) . "</small>" : ' '; if (isset($spare_cidr)) { echo "<div class='vlan'><a href=\"" . makeHref(array('page' => 'ipv4space', 'tab' => 'newrange', 'set-prefix' => $spare_cidr)) . "\">Allocate /{$needed_mask}</a></div>"; } echo "</td></tr></table>"; }
function constructIPRange($ip_bin, $mask) { $node = array(); switch (strlen($ip_bin)) { case 4: // IPv4 if ($mask < 0 || $mask > 32) { throw new InvalidArgException('mask', $mask, "Invalid v4 prefix length"); } $node['mask_bin'] = ip4_mask($mask); $node['mask'] = $mask; $node['ip_bin'] = $ip_bin & $node['mask_bin']; $node['ip'] = ip4_format($node['ip_bin']); break; case 16: // IPv6 if ($mask < 0 || $mask > 128) { throw new InvalidArgException('mask', $mask, "Invalid v6 prefix length"); } $node['mask_bin'] = ip6_mask($mask); $node['mask'] = $mask; $node['ip_bin'] = $ip_bin & $node['mask_bin']; $node['ip'] = ip6_format($node['ip_bin']); break; default: throw new InvalidArgException('ip_bin', $ip_bin, "Invalid binary IP"); } return $node; }