12 sub mac_path { $server::conf . '/mac/' . $_[0] }
13 sub ip_path { $server::conf . '/ip/' . join('/', @_) }
18 my ( $default, $value );
21 } elsif ( $#_ == 1 && $_[0] eq 'default' ) {
25 my $path = ip_path $ip;
26 mkdir $path unless -d $path;
29 if ( defined $value ) {
30 write_file $path, $value;
31 warn "update $path = $value";
32 } elsif ( ! -e $path && defined $default ) {
33 write_file $path, $default;
34 warn "default $path = $default";
36 } elsif ( -e $path ) {
38 $value = readlink $path;
39 $value =~ s{.*/([^/]+)$}{$1};
41 $value = read_file $path;
50 my $p = Net::Ping->new;
52 my $prefix = $server::ip;
53 $prefix =~ s{\.\d+$}{.};
54 my $addr = $server::ip_from || die;
55 my $ip = $prefix . $addr;
57 while ( -e ip_path($ip) || $p->ping( $ip, 0.7 ) ) {
58 $ip = $prefix . $addr++;
59 die "all addresses allocated!" if $addr == $server::ip_to;
67 my $mac_path = mac_path($mac);
68 unlink $mac_path if -e $mac_path; # XXX audit?
69 symlink ip_path($ip), $mac_path;
70 write_file ip_path($ip,'mac'), $mac;
82 my $mac_path = mac_path $mac;
83 return unless -e $mac_path;
88 $ip = read_file $mac_path;
90 symlink ip_path($ip), $mac_path;
91 warn "I: upgrade to mac symlink $mac_path\n";
92 } elsif ( -l $mac_path ) {
93 $ip = readlink $mac_path;
94 $ip =~ s{^.+/([^/]+)$}{$1};
96 die "$mac_path not file or symlink";
104 return read_file ip_path($ip, 'mac');
108 my ($old, $new) = @_;
109 my $mac = mac_from_ip($old);
110 rename ip_path($old), ip_path($new);
111 unlink mac_path($mac);
112 symlink ip_path($new), mac_path($mac);