8 use Data::Dump qw/dump/;
15 our $debug = $server::debug;
19 $path =~ s{(^.*)/[^/]+$}{$1};
20 mkdir $path unless -d $path;
24 sub mac_path { $server::conf . '/mac/' . $_[0] }
25 sub ip_path { $server::conf . '/ip/' . join('/', @_) }
30 $value = readlink $path;
31 $value =~ s{.*/([^/]+)$}{$1};
32 } elsif ( -f $path ) {
33 $value = read_file $path;
35 warn "W: $path not file or symlink\n";
43 my ( $default, $value );
46 } elsif ( $#_ == 1 && $_[0] eq 'default' ) {
50 my $path = ip_path $ip;
51 mkdir $path unless -d $path;
54 if ( defined $value ) {
56 write_file $path, $value;
57 warn "update $path = $value";
58 } elsif ( ! -e $path && defined $default ) {
60 write_file $path, $default;
61 warn "default $path = $default";
63 } elsif ( -f $path ) {
64 $value = read_file $path;
66 warn "# $name missing $path\n" if $debug;
73 my $path = ip_path $ip || return;
75 foreach my $file ( glob("$path/*") ) {
77 $name =~ s{^.+/([^/]+)$}{$1};
78 $conf->{ $name } = read_file $file;
84 $mac = format::mac($mac);
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) || ping::host($ip) ) {
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);
107 return if $mac eq '00:00:00:00:00:00';
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";
176 my @c = split(/\s+/,$_);
178 client::save_ip_mac( $c[0], $c[3] );
179 ( uc $c[3] => $c[5] )
182 } read_file('/proc/net/arp')
185 warn "# arp ",dump( $arp );