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;
71 my $path = ip_path $ip || return;
73 foreach my $file ( glob("$path/*") ) {
75 $name =~ s{^.+/([^/]+)$}{$1};
76 $conf->{ $name } = read_file $file;
82 $mac = format::mac($mac);
84 my $p = Net::Ping->new;
86 my $prefix = $server::ip;
87 $prefix =~ s{\.\d+$}{.};
88 my $addr = $server::ip_from || die;
89 my $ip = $prefix . $addr;
91 while ( -e ip_path($ip) || $p->ping( $ip, 0.7 ) ) {
92 $ip = $prefix . $addr++;
93 die "all addresses allocated!" if $addr == $server::ip_to;
99 save_ip_mac( $ip, $mac );
106 $mac = format::mac($mac);
108 mkdir ip_path($ip) unless -e ip_path($ip);
110 my $mac_path = mac_path($mac);
111 unlink $mac_path if -l $mac_path; # XXX audit?
112 symlink ip_path($ip), $mac_path;
113 write_file ip_path($ip,'mac'), $mac;
118 $mac = format::mac($mac);
120 my $mac_path = mac_path $mac;
121 return unless -e $mac_path;
125 if ( -f $mac_path ) {
126 $ip = read_file $mac_path;
128 symlink ip_path($ip), $mac_path;
129 warn "I: upgrade to mac symlink $mac_path\n";
130 } elsif ( -l $mac_path ) {
131 $ip = conf_value $mac_path;
133 die "$mac_path not file or symlink";
141 conf_value ip_path($ip, 'mac');
145 my ($old, $new) = @_;
146 my $mac = mac_from_ip($old);
147 rename ip_path($old), ip_path($new);
148 unlink mac_path($mac);
149 symlink ip_path($new), mac_path($mac);