8 use Data::Dump qw/dump/;
16 our $debug = $server::debug;
20 $path =~ s{(^.*)/[^/]+$}{$1};
21 mkdir $path unless -d $path;
25 sub mac_path { $server::conf . '/mac/' . $_[0] }
26 sub ip_path { $server::conf . '/ip/' . join('/', @_) }
31 $value = readlink $path;
32 $value =~ s{.*/([^/]+)$}{$1};
33 } elsif ( -f $path ) {
34 $value = read_file $path;
36 warn "W: $path not file or symlink\n";
44 my ( $default, $value );
47 } elsif ( $#_ == 1 && $_[0] eq 'default' ) {
51 my $path = ip_path $ip;
52 mkdir $path unless -d $path;
55 if ( defined $value ) {
57 write_file $path, $value;
58 warn "update $path = $value";
59 } elsif ( ! -e $path && defined $default ) {
61 write_file $path, $default;
62 warn "default $path = $default";
64 } elsif ( -f $path ) {
65 $value = read_file $path;
67 warn "# $name missing $path\n" if $debug;
74 my $path = ip_path $ip || return;
76 foreach my $file ( glob("$path/*") ) {
78 $name =~ s{^.+/([^/]+)$}{$1};
79 $conf->{ $name } = read_file $file;
85 $mac = format::mac($mac);
87 if ( $server::new_clients-- ) {
88 warn "# clients left: $server::new_clients\n";
90 warn "W: no new clients accepted";
94 my $prefix = $server::ip;
95 $prefix =~ s{\.\d+$}{.};
96 my $addr = $server::ip_from || die;
97 my $ip = $prefix . $addr;
99 while ( -e ip_path($ip) || ping::host($ip) ) {
100 $ip = $prefix . $addr++;
101 die "all addresses allocated!" if $addr == $server::ip_to;
105 warn "next_ip $ip\n";
107 save_ip_mac( $ip, $mac );
114 $mac = format::mac($mac);
115 return if $mac eq '00:00:00:00:00:00';
117 mkdir ip_path($ip) unless -e ip_path($ip);
119 my $mac_path = mac_path($mac);
120 unlink $mac_path if -l $mac_path; # XXX audit?
121 symlink ip_path($ip), $mac_path;
122 write_file( ip_path($ip,'mac'), $mac );
127 $mac = format::mac($mac);
129 my $mac_path = mac_path $mac;
130 return unless -e $mac_path;
134 if ( -f $mac_path ) {
135 $ip = read_file $mac_path;
137 symlink ip_path($ip), $mac_path;
138 warn "I: upgrade to mac symlink $mac_path\n";
139 } elsif ( -l $mac_path ) {
140 $ip = conf_value $mac_path;
142 die "$mac_path not file or symlink";
150 conf_value ip_path($ip, 'mac');
154 my ($old, $new) = @_;
155 return if $old eq $new;
156 my $mac = mac_from_ip($old) || die "no mac for $old";
157 rename ip_path($old), ip_path($new);
158 unlink mac_path($mac);
159 symlink ip_path($new), mac_path($mac);
164 sort { ip::to_int($a) cmp ip::to_int($b) }
169 } glob("$server::conf/ip/*")
174 unlink $_ foreach grep { -e $_ } ( glob "$server::conf/ip/$ip/*" );
175 if ( my $mac = mac_from_ip $ip ) {
176 unlink "$server::conf/mac/$mac";
178 rmtree "$server::conf/ip/$ip";
184 my @c = split(/\s+/,$_);
186 client::save_ip_mac( $c[0], $c[3] );
187 ( uc $c[3] => $c[5] )
190 } read_file('/proc/net/arp')
193 warn "# arp ",dump( $arp );