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 if ( my $clients_left = server::shared( 'new_clients' ) ) {
87 server::shared( 'new_clients', $clients_left - 1 );
89 warn "W: no new clients accepted";
93 my $prefix = $server::ip;
94 $prefix =~ s{\.\d+$}{.};
95 my $addr = $server::ip_from || die;
96 my $ip = $prefix . $addr;
98 while ( -e ip_path($ip) || ping::host($ip) ) {
99 $ip = $prefix . $addr++;
100 die "all addresses allocated!" if $addr == $server::ip_to;
104 warn "next_ip $ip\n";
106 save_ip_mac( $ip, $mac );
113 $mac = format::mac($mac);
114 return if $mac eq '00:00:00:00:00:00';
116 mkdir ip_path($ip) unless -e ip_path($ip);
118 my $mac_path = mac_path($mac);
119 unlink $mac_path if -l $mac_path; # XXX audit?
120 symlink ip_path($ip), $mac_path;
121 write_file( ip_path($ip,'mac'), $mac );
126 $mac = format::mac($mac);
128 my $mac_path = mac_path $mac;
129 return unless -e $mac_path;
133 if ( -f $mac_path ) {
134 $ip = read_file $mac_path;
136 symlink ip_path($ip), $mac_path;
137 warn "I: upgrade to mac symlink $mac_path\n";
138 } elsif ( -l $mac_path ) {
139 $ip = conf_value $mac_path;
141 die "$mac_path not file or symlink";
149 conf_value ip_path($ip, 'mac');
153 my ($old, $new) = @_;
154 return if $old eq $new;
155 my $mac = mac_from_ip($old) || die "no mac for $old";
156 rename ip_path($old), ip_path($new);
157 unlink mac_path($mac);
158 symlink ip_path($new), mac_path($mac);
163 sort { ip::to_int($a) cmp ip::to_int($b) }
168 } glob("$server::conf/ip/*")
173 unlink $_ foreach grep { -e $_ } ( glob "$server::conf/ip/$ip/*" );
174 if ( my $mac = mac_from_ip $ip ) {
175 unlink "$server::conf/mac/$mac";
177 rmdir "$server::conf/ip/$ip";
183 my @c = split(/\s+/,$_);
185 client::save_ip_mac( $c[0], $c[3] );
186 ( uc $c[3] => $c[5] )
189 } read_file('/proc/net/arp')
192 warn "# arp ",dump( $arp );