From 2785f359fd960c42d822383faab9ef86fcda47f6 Mon Sep 17 00:00:00 2001 From: Jamie Cameron Date: Fri, 25 Jan 2008 18:41:31 +0000 Subject: [PATCH] Added popup IP chooser --- dhcp-dns/dhcp-dns-lib.pl | 7 +++++ dhcp-dns/ip_chooser.cgi | 64 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 dhcp-dns/ip_chooser.cgi diff --git a/dhcp-dns/dhcp-dns-lib.pl b/dhcp-dns/dhcp-dns-lib.pl index 73fcabb5f..53671f195 100644 --- a/dhcp-dns/dhcp-dns-lib.pl +++ b/dhcp-dns/dhcp-dns-lib.pl @@ -137,6 +137,7 @@ elsif (@subnets) { } $rv .= &ui_table_row($text{'form_ip'}, &ui_textbox("ip", $fixed ? $fixed->{'values'}->[0] : undef, 20). + ($new ? " ".&ip_chooser_button("ip") : ""). " ".$text{'form_subnet'}." ". &ui_select("subnet", $parsub ? $parsub->{'values'}->[0] : '', [ $parsub ? ( ) : ( [ '', $text{'form_nosubnet'} ] ), @@ -231,5 +232,11 @@ else { return $err; } +# ip_chooser_button(field) +sub ip_chooser_button +{ +return "\n"; +} + 1; diff --git a/dhcp-dns/ip_chooser.cgi b/dhcp-dns/ip_chooser.cgi new file mode 100644 index 000000000..222325668 --- /dev/null +++ b/dhcp-dns/ip_chooser.cgi @@ -0,0 +1,64 @@ +#!/usr/local/bin/perl +# Show a list of free IPs in all subnets + +require './dhcp-dns-lib.pl'; +&popup_header($text{'chooser_title'}); +&foreign_require("net", "net-lib.pl"); + +# Build map of all IPs +@subnets = &list_dhcp_subnets(); +@hosts = &list_dhcp_hosts(); +foreach $s (@subnets) { + $sip = $s->{'values'}->[0]; + $smask = $s->{'values'}->[2]; + $sipnum = &net::ip_to_integer($sip); + $smasknum = &net::ip_to_integer($smask); + $basenum = $sipnum & $smasknum; + $topnum = $basenum + ~$smasknum - 1; + for($i=$basenum; $i<=$topnum; $i++) { + $poss{&net::integer_to_ip($i)} = $s; + } + } + +# Find those that are free +foreach $h (@hosts) { + $fixed = &dhcpd::find("fixed-address", $h->{'members'}); + if ($fixed) { + $used{&to_ipaddress($fixed->{'values'}->[0])} = 1; + } + } +foreach $ip (keys %poss) { + if (!$used{$ip}) { + push(@avail, $ip); + } + } +@avail = sort { @a = split(/\./, $a); + @b = split(/\./, $b); + $a[0] <=> $b[0] || $a[1] <=> $b[1] || + $a[2] <=> $b[2] || $a[3] <=> $b[3] } @avail; + +print < +function select(ip) +{ +top.opener.ifield.value = ip; +top.close(); +return false; +} + +EOF +if (@avail) { + print &ui_columns_start([ $text{'chooser_ip'} ], 100); + foreach $ip (@avail) { + print &ui_columns_row([ + "$ip" + ]); + } + print &ui_columns_end(); + } +else { + print "$text{'chooser_none'}

\n"; + } + +&popup_footer(); +