Added popup IP chooser
authorJamie Cameron <jcameron@webmin.com>
Fri, 25 Jan 2008 18:41:31 +0000 (18:41 +0000)
committerJamie Cameron <jcameron@webmin.com>
Fri, 25 Jan 2008 18:41:31 +0000 (18:41 +0000)
dhcp-dns/dhcp-dns-lib.pl
dhcp-dns/ip_chooser.cgi [new file with mode: 0644]

index 73fcabb..53671f1 100644 (file)
@@ -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 "<input type=button onClick='ifield = form.$_[0]; chooser = window.open(\"ip_chooser.cgi\", \"chooser\", \"toolbar=no,menubar=no,scrollbars=yes,resizable=yes,width=300,height=600\"); chooser.ifield = ifield; window.ifield = ifield' value=\"...\">\n";
+}
+
 1;
 
diff --git a/dhcp-dns/ip_chooser.cgi b/dhcp-dns/ip_chooser.cgi
new file mode 100644 (file)
index 0000000..2223256
--- /dev/null
@@ -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 <<EOF;
+<script>
+function select(ip)
+{
+top.opener.ifield.value = ip;
+top.close();
+return false;
+}
+</script>
+EOF
+if (@avail) {
+       print &ui_columns_start([ $text{'chooser_ip'} ], 100);
+       foreach $ip (@avail) {
+               print &ui_columns_row([
+                       "<a href='' onClick='return select(\"$ip\")'>$ip</a>"
+                       ]);
+               }
+       print &ui_columns_end();
+       }
+else {
+       print "<b>$text{'chooser_none'}</b><p>\n";
+       }
+
+&popup_footer();
+