13 our $debug = $server::debug;
17 $path =~ s{(^.*)/[^/]+$}{$1};
18 mkdir $path unless -d $path;
22 sub mac_path { $server::conf . '/mac/' . $_[0] }
23 sub ip_path { $server::conf . '/ip/' . join('/', @_) }
28 $value = readlink $path;
29 $value =~ s{.*/([^/]+)$}{$1};
30 } elsif ( -f $path ) {
31 $value = read_file $path;
33 warn "W: $path not file or symlink\n";
41 my ( $default, $value );
44 } elsif ( $#_ == 1 && $_[0] eq 'default' ) {
48 my $path = ip_path $ip;
49 mkdir $path unless -d $path;
52 if ( defined $value ) {
54 write_file $path, $value;
55 warn "update $path = $value";
56 } elsif ( ! -e $path && defined $default ) {
58 write_file $path, $default;
59 warn "default $path = $default";
61 } elsif ( -f $path ) {
62 $value = read_file $path;
64 warn "# $name missing $path\n" if $debug;
72 my $p = Net::Ping->new;
74 my $prefix = $server::ip;
75 $prefix =~ s{\.\d+$}{.};
76 my $addr = $server::ip_from || die;
77 my $ip = $prefix . $addr;
79 while ( -e ip_path($ip) || $p->ping( $ip, 0.7 ) ) {
80 $ip = $prefix . $addr++;
81 die "all addresses allocated!" if $addr == $server::ip_to;
87 save_ip_mac( $ip, $mac );
95 mkdir ip_path($ip) unless -e $ip;
97 my $mac_path = mac_path($mac);
98 unlink $mac_path if -e $mac_path; # XXX audit?
99 symlink ip_path($ip), $mac_path;
100 write_file ip_path($ip,'mac'), $mac;
109 my $mac_path = mac_path $mac;
110 return unless -e $mac_path;
114 if ( -f $mac_path ) {
115 $ip = read_file $mac_path;
117 symlink ip_path($ip), $mac_path;
118 warn "I: upgrade to mac symlink $mac_path\n";
119 } elsif ( -l $mac_path ) {
120 $ip = conf_value $mac_path;
122 die "$mac_path not file or symlink";
130 conf_value ip_path($ip, 'mac');
134 my ($old, $new) = @_;
135 my $mac = mac_from_ip($old);
136 rename ip_path($old), ip_path($new);
137 unlink mac_path($mac);
138 symlink ip_path($new), mac_path($mac);