14 our $debug = $server::debug;
18 $path =~ s{(^.*)/[^/]+$}{$1};
19 mkdir $path unless -d $path;
23 sub mac_path { $server::conf . '/mac/' . $_[0] }
24 sub ip_path { $server::conf . '/ip/' . join('/', @_) }
29 $value = readlink $path;
30 $value =~ s{.*/([^/]+)$}{$1};
31 } elsif ( -f $path ) {
32 $value = read_file $path;
34 warn "W: $path not file or symlink\n";
42 my ( $default, $value );
45 } elsif ( $#_ == 1 && $_[0] eq 'default' ) {
49 my $path = ip_path $ip;
50 mkdir $path unless -d $path;
53 if ( defined $value ) {
55 write_file $path, $value;
56 warn "update $path = $value";
57 } elsif ( ! -e $path && defined $default ) {
59 write_file $path, $default;
60 warn "default $path = $default";
62 } elsif ( -f $path ) {
63 $value = read_file $path;
65 warn "# $name missing $path\n" if $debug;
72 my $path = ip_path $ip || return;
74 foreach my $file ( glob("$path/*") ) {
76 $name =~ s{^.+/([^/]+)$}{$1};
77 $conf->{ $name } = read_file $file;
83 $mac = format::mac($mac);
85 my $p = Net::Ping->new;
87 my $prefix = $server::ip;
88 $prefix =~ s{\.\d+$}{.};
89 my $addr = $server::ip_from || die;
90 my $ip = $prefix . $addr;
92 while ( -e ip_path($ip) || $p->ping( $ip, 0.7 ) ) {
93 $ip = $prefix . $addr++;
94 die "all addresses allocated!" if $addr == $server::ip_to;
100 save_ip_mac( $ip, $mac );
107 $mac = format::mac($mac);
108 return if $mac eq '00:00:00:00:00:00';
110 mkdir ip_path($ip) unless -e ip_path($ip);
112 my $mac_path = mac_path($mac);
113 unlink $mac_path if -l $mac_path; # XXX audit?
114 symlink ip_path($ip), $mac_path;
115 write_file( ip_path($ip,'mac'), $mac );
120 $mac = format::mac($mac);
122 my $mac_path = mac_path $mac;
123 return unless -e $mac_path;
127 if ( -f $mac_path ) {
128 $ip = read_file $mac_path;
130 symlink ip_path($ip), $mac_path;
131 warn "I: upgrade to mac symlink $mac_path\n";
132 } elsif ( -l $mac_path ) {
133 $ip = conf_value $mac_path;
135 die "$mac_path not file or symlink";
143 conf_value ip_path($ip, 'mac');
147 my ($old, $new) = @_;
148 return if $old eq $new;
149 my $mac = mac_from_ip($old) || die "no mac for $old";
150 rename ip_path($old), ip_path($new);
151 unlink mac_path($mac);
152 symlink ip_path($new), mac_path($mac);
157 sort { ip::to_int($a) cmp ip::to_int($b) }
162 } glob("$server::conf/ip/*")
167 unlink $_ foreach grep { -e $_ } ( glob "$server::conf/ip/$ip/*" );
168 if ( my $mac = mac_from_ip $ip ) {
169 unlink "$server::conf/mac/$mac";
171 rmdir "$server::conf/ip/$ip";