--- lib/PXElator/client.pm 2009/08/06 18:43:55 164 +++ lib/PXElator/client.pm 2009/08/10 17:30:01 194 @@ -6,6 +6,7 @@ use server; use File::Slurp; +use Net::Ping; sub conf { my $ip = shift; @@ -39,4 +40,61 @@ return $value; } +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); +} + +sub next_ip { + + my $p = Net::Ping->new; + + my $prefix = $server::ip; + $prefix =~ s{\.\d+$}{.}; + my $addr = $server::ip_from || die; + my $ip = $prefix . $addr; + + while ( -e "$server::conf/ip/$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"; + return $ip; + +} + +sub ip_from_mac { + my $mac = shift; + + $mac = lc $mac; + $mac =~ s{:}{}g; + + my $mac_path = "$server::conf/mac/$mac"; + return unless -e $mac_path; + + my $ip; + + if ( -f $mac_path ) { + $ip = read_file $mac_path; + unlink $mac_path; + symlink "$server::conf/ip/$ip", $mac_path; + warn "I: upgrade to mac symlink $mac_path\n"; + } elsif ( -l $mac_path ) { + $ip = readlink $mac_path; + $ip =~ s{^.+/([^/]+)$}{$1}; + } else { + die "$mac_path not file or symlink"; + } + + return $ip; +} + 1;