--- lib/PXElator/client.pm 2009/08/10 17:30:01 194 +++ lib/PXElator/client.pm 2009/08/28 21:55:41 329 @@ -4,9 +4,38 @@ use strict; use autodie; -use server; use File::Slurp; use Net::Ping; +use Data::Dump qw/dump/; + +use server; +use format; +use ip; + +our $debug = $server::debug; + +sub mkbasedir { + my $path = shift; + $path =~ s{(^.*)/[^/]+$}{$1}; + mkdir $path unless -d $path; + return $path; +} + +sub mac_path { $server::conf . '/mac/' . $_[0] } +sub ip_path { $server::conf . '/ip/' . join('/', @_) } +sub conf_value { + my $path = shift; + my $value; + if ( -l $path ) { + $value = readlink $path; + $value =~ s{.*/([^/]+)$}{$1}; + } elsif ( -f $path ) { + $value = read_file $path; + } else { + warn "W: $path not file or symlink\n"; + } + return $value; +} sub conf { my $ip = shift; @@ -18,40 +47,41 @@ $default = $_[1] } - my $path ="$server::conf/ip/$ip"; + my $path = ip_path $ip; mkdir $path unless -d $path; $path .= '/' . $name; if ( defined $value ) { + mkbasedir $path; write_file $path, $value; warn "update $path = $value"; } elsif ( ! -e $path && defined $default ) { + mkbasedir $path; write_file $path, $default; warn "default $path = $default"; $value = $default; - } elsif ( -e $path ) { - if ( -l $path ) { - $value = readlink $path; - $value =~ s{.*/([^/]+)$}{$1}; - } else { - $value = read_file $path; - } + } elsif ( -f $path ) { + $value = read_file $path; + } else { + warn "# $name missing $path\n" if $debug; } 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 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 { +sub next_ip($) { + my $mac = shift; + $mac = format::mac($mac); my $p = Net::Ping->new; @@ -60,24 +90,37 @@ 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"; + + save_ip_mac( $ip, $mac ); + return $ip; +} +sub save_ip_mac { + my ($ip,$mac) = @_; + $mac = format::mac($mac); + return if $mac eq '00:00:00:00:00:00'; + + mkdir ip_path($ip) unless -e ip_path($ip); + + my $mac_path = mac_path($mac); + unlink $mac_path if -l $mac_path; # XXX audit? + symlink ip_path($ip), $mac_path; + write_file( ip_path($ip,'mac'), $mac ); } -sub ip_from_mac { +sub ip_from_mac($) { my $mac = shift; + $mac = format::mac($mac); - $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,11 +128,10 @@ 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; - $ip =~ s{^.+/([^/]+)$}{$1}; + $ip = conf_value $mac_path; } else { die "$mac_path not file or symlink"; } @@ -97,4 +139,53 @@ return $ip; } +sub mac_from_ip($) { + my $ip = shift; + conf_value ip_path($ip, 'mac'); +} + +sub change_ip($$) { + my ($old, $new) = @_; + return if $old eq $new; + my $mac = mac_from_ip($old) || die "no mac for $old"; + rename ip_path($old), ip_path($new); + unlink mac_path($mac); + symlink ip_path($new), mac_path($mac); + return $new; +} + +sub all_ips { + sort { ip::to_int($a) cmp ip::to_int($b) } + map { + my $ip = $_; + $ip =~ s{^.+/ip/}{}; + $ip; + } glob("$server::conf/ip/*") +} + +sub remove { + my $ip = shift; + unlink $_ foreach grep { -e $_ } ( glob "$server::conf/ip/$ip/*" ); + if ( my $mac = mac_from_ip $ip ) { + unlink "$server::conf/mac/$mac"; + } + rmdir "$server::conf/ip/$ip"; +} + +sub arp_mac_dev { + my $arp = { + map { + my @c = split(/\s+/,$_); + if ( $#c == 5 ) { + client::save_ip_mac( $c[0], $c[3] ); + ( uc $c[3] => $c[5] ) + } else { + } + } read_file('/proc/net/arp') + }; + + warn "# arp ",dump( $arp ); + return $arp; +} + 1;