9 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 $p = Net::Ping->new;
88 my $prefix = $server::ip;
89 $prefix =~ s{\.\d+$}{.};
90 my $addr = $server::ip_from || die;
91 my $ip = $prefix . $addr;
93 while ( -e ip_path($ip) || $p->ping( $ip, 0.7 ) ) {
94 $ip = $prefix . $addr++;
95 die "all addresses allocated!" if $addr == $server::ip_to;
101 save_ip_mac( $ip, $mac );
108 $mac = format::mac($mac);
109 return if $mac eq '00:00:00:00:00:00';
111 mkdir ip_path($ip) unless -e ip_path($ip);
113 my $mac_path = mac_path($mac);
114 unlink $mac_path if -l $mac_path; # XXX audit?
115 symlink ip_path($ip), $mac_path;
116 write_file( ip_path($ip,'mac'), $mac );
121 $mac = format::mac($mac);
123 my $mac_path = mac_path $mac;
124 return unless -e $mac_path;
128 if ( -f $mac_path ) {
129 $ip = read_file $mac_path;
131 symlink ip_path($ip), $mac_path;
132 warn "I: upgrade to mac symlink $mac_path\n";
133 } elsif ( -l $mac_path ) {
134 $ip = conf_value $mac_path;
136 die "$mac_path not file or symlink";
144 conf_value ip_path($ip, 'mac');
148 my ($old, $new) = @_;
149 return if $old eq $new;
150 my $mac = mac_from_ip($old) || die "no mac for $old";
151 rename ip_path($old), ip_path($new);
152 unlink mac_path($mac);
153 symlink ip_path($new), mac_path($mac);
158 sort { ip::to_int($a) cmp ip::to_int($b) }
163 } glob("$server::conf/ip/*")
168 unlink $_ foreach grep { -e $_ } ( glob "$server::conf/ip/$ip/*" );
169 if ( my $mac = mac_from_ip $ip ) {
170 unlink "$server::conf/mac/$mac";
172 rmdir "$server::conf/ip/$ip";
178 my @c = split(/\s+/,$_);
180 client::save_ip_mac( $c[0], $c[3] );
181 ( uc $c[3] => $c[5] )
184 } read_file('/proc/net/arp')
187 warn "# arp ",dump( $arp );