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);
109 mkdir ip_path($ip) unless -e ip_path($ip);
111 my $mac_path = mac_path($mac);
112 unlink $mac_path if -l $mac_path; # XXX audit?
113 symlink ip_path($ip), $mac_path;
114 write_file( ip_path($ip,'mac'), $mac );
119 $mac = format::mac($mac);
121 my $mac_path = mac_path $mac;
122 return unless -e $mac_path;
126 if ( -f $mac_path ) {
127 $ip = read_file $mac_path;
129 symlink ip_path($ip), $mac_path;
130 warn "I: upgrade to mac symlink $mac_path\n";
131 } elsif ( -l $mac_path ) {
132 $ip = conf_value $mac_path;
134 die "$mac_path not file or symlink";
142 conf_value ip_path($ip, 'mac');
146 my ($old, $new) = @_;
147 return if $old eq $new;
148 my $mac = mac_from_ip($old) || die "no mac for $old";
149 rename ip_path($old), ip_path($new);
150 unlink mac_path($mac);
151 symlink ip_path($new), mac_path($mac);
156 sort { ip::to_int($a) cmp ip::to_int($b) }
161 } glob("$server::conf/ip/*")
166 unlink $_ foreach grep { -e $_ } ( glob "$server::conf/ip/$ip/*" );
167 if ( my $mac = mac_from_ip $ip ) {
168 unlink "$server::conf/mac/$mac";
170 rmdir "$server::conf/ip/$ip";