--- lib/PXElator/client.pm 2009/08/10 17:30:01 194 +++ lib/PXElator/client.pm 2009/08/12 23:59:01 208 @@ -7,6 +7,10 @@ use server; use File::Slurp; use Net::Ping; +use format; + +sub mac_path { $server::conf . '/mac/' . $_[0] } +sub ip_path { $server::conf . '/ip/' . join('/', @_) } sub conf { my $ip = shift; @@ -18,7 +22,7 @@ $default = $_[1] } - my $path ="$server::conf/ip/$ip"; + my $path = ip_path $ip; mkdir $path unless -d $path; $path .= '/' . $name; @@ -41,17 +45,13 @@ } sub mac { - my ( $ip, $op ) = @_; - $op ||= 'html'; - my $mac = client::conf( $ip, 'mac' ); - return '' unless $mac; - $mac =~ s{(..)}{$1:}g; - $mac =~ s{:$}{}; - $mac = qq|$mac| if (caller(1))[3] =~ m{^httpd} && $op ne 'clean'; - return uc($mac); + my $ip = shift; + my $mac = client::conf( $ip, 'mac' ) || return ''; + format::mac( $ip, @_ ); } -sub next_ip { +sub next_ip($) { + my $mac = shift; my $p = Net::Ping->new; @@ -60,24 +60,30 @@ my $addr = $server::ip_from || die; my $ip = $prefix . $addr; - while ( -e "$server::conf/ip/$ip" || $p->ping( $ip, 0.7 ) ) { + while ( -e ip_path($ip) || $p->ping( $ip, 0.7 ) ) { $ip = $prefix . $addr++; die "all addresses allocated!" if $addr == $server::ip_to; warn "skip $ip\n"; } warn "next_ip $ip\n"; + + mkdir ip_path($ip); + + symlink ip_path($ip), mac_path($mac); + write_file ip_path($ip,'mac'), $mac; + return $ip; } -sub ip_from_mac { +sub ip_from_mac($) { my $mac = shift; $mac = lc $mac; $mac =~ s{:}{}g; - my $mac_path = "$server::conf/mac/$mac"; + my $mac_path = mac_path $mac; return unless -e $mac_path; my $ip; @@ -85,7 +91,7 @@ if ( -f $mac_path ) { $ip = read_file $mac_path; unlink $mac_path; - symlink "$server::conf/ip/$ip", $mac_path; + symlink ip_path($ip), $mac_path; warn "I: upgrade to mac symlink $mac_path\n"; } elsif ( -l $mac_path ) { $ip = readlink $mac_path; @@ -97,4 +103,17 @@ return $ip; } +sub mac_from_ip($) { + my $ip = shift; + return read_file ip_path($ip, 'mac'); +} + +sub change_ip($$) { + my ($old, $new) = @_; + my $mac = mac_from_ip($old); + rename ip_path($old), ip_path($new); + unlink mac_path($mac); + symlink ip_path($new), mac_path($mac); +} + 1;