8 use Data::Dump qw/dump/;
17 our $debug = $server::debug;
21 $path =~ s{(^.*)/[^/]+$}{$1};
22 mkdir $path unless -d $path;
26 sub mac_path { $server::conf . '/mac/' . $_[0] }
27 sub ip_path { $server::conf . '/ip/' . join('/', @_) }
32 $value = readlink $path;
33 $value =~ s{.*/([^/]+)$}{$1};
34 } elsif ( -f $path ) {
35 $value = read_file $path;
37 warn "W: $path not file or symlink\n";
45 my ( $default, $value );
48 } elsif ( $#_ == 1 && $_[0] eq 'default' ) {
52 my $path = ip_path $ip;
53 mkdir $path unless -e $path;
54 warn "WARNING: $path not directory" unless -d $path;
57 if ( defined $value ) {
59 write_file $path, $value;
60 warn "update $path = $value";
61 } elsif ( ! -e $path && defined $default ) {
63 write_file $path, $default;
64 warn "default $path = $default";
66 } elsif ( -l $path ) {
67 $value = readlink $path;
68 } elsif ( -f $path ) {
69 $value = read_file $path;
72 warn "# $name missing $path\n" if $debug;
79 my $path = ip_path $ip || return;
81 foreach my $file ( glob("$path/*"), glob("$path/*/*") ) {
83 $name =~ s{^$path/+}{} || die "can't remove $path from $name";
85 -l $file ? readlink $file :
86 -f $file ? read_file $file :
93 $mac = format::mac($mac);
95 if ( $server::new_clients > 0 ) {
96 warn "# clients left: ", --$server::new_clients;
98 warn "W: no new clients accepted";
102 my $prefix = $server::ip;
103 $prefix =~ s{\.\d+$}{.};
104 my $addr = $server::ip_from || die;
105 my $ip = $prefix . $addr;
107 while ( -e ip_path($ip) || ping::host($ip) ) {
108 $ip = $prefix . $addr++;
109 die "all addresses allocated!" if $addr == $server::ip_to;
113 warn "next_ip $ip\n";
115 save_ip_mac( $ip, $mac );
122 $mac = format::mac($mac);
123 return if $mac eq '00:00:00:00:00:00' || $ip eq '0.0.0.0';
125 mkdir ip_path($ip) unless -e ip_path($ip);
127 my $mac_path = mac_path($mac);
128 unlink $mac_path if -l $mac_path; # XXX audit?
129 symlink ip_path($ip), $mac_path;
130 write_file( ip_path($ip,'mac'), $mac );
135 $mac = format::mac($mac);
137 my $mac_path = mac_path $mac;
138 return unless -e $mac_path;
142 if ( -f $mac_path ) {
143 $ip = read_file $mac_path;
145 symlink ip_path($ip), $mac_path;
146 warn "I: upgrade to mac symlink $mac_path\n";
147 } elsif ( -l $mac_path ) {
148 $ip = conf_value $mac_path;
150 die "$mac_path not file or symlink";
158 conf_value ip_path($ip, 'mac');
162 my ($old, $new) = @_;
163 return if $old eq $new;
164 my $mac = mac_from_ip($old) || die "no mac for $old";
165 rename ip_path($old), ip_path($new);
166 unlink mac_path($mac);
167 symlink ip_path($new), mac_path($mac);
172 sort { ip::to_int($a) cmp ip::to_int($b) }
176 autocreate_params( $ip );
178 } glob("$server::conf/ip/*")
183 if ( my $mac = mac_from_ip $ip ) {
184 unlink "$server::conf/mac/$mac";
186 rmtree "$server::conf/ip/$ip";
192 my @c = split(/\s+/,$_);
194 client::save_ip_mac( $c[0], $c[3] );
195 ( uc $c[3] => $c[5] )
198 } read_file('/proc/net/arp')
201 warn "# arp ",dump( $arp );
205 sub rebuild_mac_links {
206 warn "# rebuild mac links";
207 foreach my $ip ( all_ips ) {
208 my $mac = ip_path $ip, 'mac';
210 $mac = read_file $mac;
212 save_ip_mac( $ip, $mac );
213 warn "## $ip $mac\n";
218 sub autocreate_params {
220 my $mac = mac_from_ip $ip;
221 if ( $mac =~ m{^AC:DE:48:00:00} && ! defined conf( $ip, 'kvm' ) ) {
222 conf( $ip, 'kvm', default => kvm::nr_from_mac( $mac ) );
223 warn "# create kvm for $ip";