Handle hostnames with upper-case letters
[webmin.git] / dhcp-dns / ip_chooser.cgi
1 #!/usr/local/bin/perl
2 # Show a list of free IPs in all subnets
3
4 $trust_unknown_referers = 1;
5 require './dhcp-dns-lib.pl';
6 &popup_header($text{'chooser_title'});
7 &foreign_require("net", "net-lib.pl");
8
9 # Build map of all IPs
10 @subnets = &list_dhcp_subnets();
11 @hosts = &list_dhcp_hosts();
12 foreach $s (@subnets) {
13         $sip = $s->{'values'}->[0];
14         $smask = $s->{'values'}->[2];
15         $sipnum = &net::ip_to_integer($sip);
16         $smasknum = &net::ip_to_integer($smask);
17         $basenum = $sipnum & $smasknum;
18         $topnum = $basenum + ~$smasknum - 1;
19         for($i=$basenum; $i<=$topnum; $i++) {
20                 $poss{&net::integer_to_ip($i)} = $s;
21                 }
22         }
23
24 # Find those that are free
25 foreach $h (@hosts) {
26         $fixed = &dhcpd::find("fixed-address", $h->{'members'});
27         if ($fixed) {
28                 $used{&to_ipaddress($fixed->{'values'}->[0])} = 1;
29                 }
30         }
31 foreach $ip (keys %poss) {
32         if (!$used{$ip}) {
33                 push(@avail, $ip);
34                 }
35         }
36 @avail = sort { @a = split(/\./, $a);
37                 @b = split(/\./, $b);
38                 $a[0] <=> $b[0] || $a[1] <=> $b[1] ||
39                  $a[2] <=> $b[2] || $a[3] <=> $b[3] } @avail;
40
41 print <<EOF;
42 <script>
43 function select(ip)
44 {
45 top.opener.ifield.value = ip;
46 top.close();
47 return false;
48 }
49 </script>
50 EOF
51 if (@avail) {
52         print &ui_columns_start([ $text{'chooser_ip'} ], 100);
53         foreach $ip (@avail) {
54                 print &ui_columns_row([
55                         "<a href='' onClick='return select(\"$ip\")'>$ip</a>"
56                         ]);
57                 }
58         print &ui_columns_end();
59         }
60 else {
61         print "<b>$text{'chooser_none'}</b><p>\n";
62         }
63
64 &popup_footer();
65