--- lib/PXElator/client.pm 2009/08/15 13:44:13 219 +++ lib/PXElator/client.pm 2009/08/28 16:41:46 322 @@ -6,10 +6,12 @@ use File::Slurp; use Net::Ping; -use Carp qw/confess/; use server; use format; +use ip; + +our $debug = $server::debug; sub mkbasedir { my $path = shift; @@ -29,7 +31,7 @@ } elsif ( -f $path ) { $value = read_file $path; } else { - confess "$path not file or symlink"; + warn "W: $path not file or symlink\n"; } return $value; } @@ -60,13 +62,25 @@ } elsif ( -f $path ) { $value = read_file $path; } else { - confess "conf $name"; + warn "# $name missing $path\n" if $debug; } return $value; } +sub all_conf { + my $ip = shift; + my $path = ip_path $ip || return; + my $conf; + foreach my $file ( glob("$path/*") ) { + my $name = $file; + $name =~ s{^.+/([^/]+)$}{$1}; + $conf->{ $name } = read_file $file; + } + return $conf; +} sub next_ip($) { my $mac = shift; + $mac = format::mac($mac); my $p = Net::Ping->new; @@ -83,22 +97,26 @@ warn "next_ip $ip\n"; - mkdir ip_path($ip); + save_ip_mac( $ip, $mac ); + + return $ip; +} + +sub save_ip_mac { + my ($ip,$mac) = @_; + $mac = format::mac($mac); + + mkdir ip_path($ip) unless -e ip_path($ip); my $mac_path = mac_path($mac); - unlink $mac_path if -e $mac_path; # XXX audit? + unlink $mac_path if -l $mac_path; # XXX audit? symlink ip_path($ip), $mac_path; write_file ip_path($ip,'mac'), $mac; - - return $ip; - } sub ip_from_mac($) { my $mac = shift; - - $mac = lc $mac; - $mac =~ s{:}{}g; + $mac = format::mac($mac); my $mac_path = mac_path $mac; return unless -e $mac_path; @@ -132,4 +150,13 @@ symlink ip_path($new), mac_path($mac); } +sub all_ips { + sort { ip::to_int($a) cmp ip::to_int($b) } + map { + my $ip = $_; + $ip =~ s{^.+/ip/}{}; + $ip; + } glob("$server::conf/ip/*") +} + 1;