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";
86 -l $file ? readlink $file :
87 -f $file ? read_file $file :
94 $mac = format::mac($mac);
96 if ( $server::new_clients > 0 ) {
97 warn "# clients left: ", --$server::new_clients;
99 warn "W: no new clients accepted";
103 my $prefix = $server::ip;
104 $prefix =~ s{\.\d+$}{.};
105 my $addr = $server::ip_from || die;
106 my $ip = $prefix . $addr;
108 while ( -e ip_path($ip) || ping::host($ip) ) {
109 $ip = $prefix . $addr++;
110 die "all addresses allocated!" if $addr == $server::ip_to;
114 warn "next_ip $ip\n";
116 save_ip_mac( $ip, $mac );
123 $mac = format::mac($mac);
124 return if $mac eq '00:00:00:00:00:00' || $ip eq '0.0.0.0';
126 mkdir ip_path($ip) unless -e ip_path($ip);
128 my $mac_path = mac_path($mac);
129 unlink $mac_path if -l $mac_path; # XXX audit?
130 symlink ip_path($ip), $mac_path;
131 write_file( ip_path($ip,'mac'), $mac );
136 $mac = format::mac($mac);
138 my $mac_path = mac_path $mac;
139 return unless -e $mac_path;
143 if ( -f $mac_path ) {
144 $ip = read_file $mac_path;
146 symlink ip_path($ip), $mac_path;
147 warn "I: upgrade to mac symlink $mac_path\n";
148 } elsif ( -l $mac_path ) {
149 $ip = conf_value $mac_path;
151 die "$mac_path not file or symlink";
159 conf_value ip_path($ip, 'mac');
163 my ($old, $new) = @_;
164 return if $old eq $new;
165 my $mac = mac_from_ip($old) || die "no mac for $old";
166 rename ip_path($old), ip_path($new);
167 unlink mac_path($mac);
168 symlink ip_path($new), mac_path($mac);
173 sort { ip::to_int($a) cmp ip::to_int($b) }
177 autocreate_params( $ip );
179 } glob("$server::conf/ip/*")
184 if ( my $mac = mac_from_ip $ip ) {
185 unlink mac_path($mac);
187 rmtree "$server::conf/ip/$ip";
193 my @c = split(/\s+/,$_);
195 client::save_ip_mac( $c[0], $c[3] );
196 ( uc $c[3] => $c[5] )
199 } read_file('/proc/net/arp')
202 warn "# arp ",dump( $arp );
206 sub rebuild_mac_links {
207 warn "# rebuild mac links";
208 foreach my $ip ( all_ips ) {
209 my $mac = ip_path $ip, 'mac';
211 $mac = read_file $mac;
213 save_ip_mac( $ip, $mac );
214 warn "## $ip $mac\n";
219 sub autocreate_params {
221 my $mac = mac_from_ip $ip;
222 if ( $mac =~ m{^AC:DE:48:00:00} && ! defined conf( $ip, 'kvm' ) ) {
223 conf( $ip, 'kvm', default => kvm::nr_from_mac( $mac ) );
224 warn "# create kvm for $ip";