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 -d $path;
56 if ( defined $value ) {
58 write_file $path, $value;
59 warn "update $path = $value";
60 } elsif ( ! -e $path && defined $default ) {
62 write_file $path, $default;
63 warn "default $path = $default";
65 } elsif ( -l $path ) {
66 $value = readlink $path;
67 } elsif ( -f $path ) {
68 $value = read_file $path;
71 warn "# $name missing $path\n" if $debug;
78 my $path = ip_path $ip || return;
80 foreach my $file ( glob("$path/*"), glob("$path/*/*") ) {
82 $name =~ s{^$path/+}{} || die "can't remove $path from $name";
84 -l $file ? readlink $file :
85 -f $file ? read_file $file :
92 $mac = format::mac($mac);
94 if ( $server::new_clients > 0 ) {
95 warn "# clients left: ", --$server::new_clients;
97 warn "W: no new clients accepted";
101 my $prefix = $server::ip;
102 $prefix =~ s{\.\d+$}{.};
103 my $addr = $server::ip_from || die;
104 my $ip = $prefix . $addr;
106 while ( -e ip_path($ip) || ping::host($ip) ) {
107 $ip = $prefix . $addr++;
108 die "all addresses allocated!" if $addr == $server::ip_to;
112 warn "next_ip $ip\n";
114 save_ip_mac( $ip, $mac );
121 $mac = format::mac($mac);
122 return if $mac eq '00:00:00:00:00:00' || $ip eq '0.0.0.0';
124 mkdir ip_path($ip) unless -e ip_path($ip);
126 my $mac_path = mac_path($mac);
127 unlink $mac_path if -l $mac_path; # XXX audit?
128 symlink ip_path($ip), $mac_path;
129 write_file( ip_path($ip,'mac'), $mac );
134 $mac = format::mac($mac);
136 my $mac_path = mac_path $mac;
137 return unless -e $mac_path;
141 if ( -f $mac_path ) {
142 $ip = read_file $mac_path;
144 symlink ip_path($ip), $mac_path;
145 warn "I: upgrade to mac symlink $mac_path\n";
146 } elsif ( -l $mac_path ) {
147 $ip = conf_value $mac_path;
149 die "$mac_path not file or symlink";
157 conf_value ip_path($ip, 'mac');
161 my ($old, $new) = @_;
162 return if $old eq $new;
163 my $mac = mac_from_ip($old) || die "no mac for $old";
164 rename ip_path($old), ip_path($new);
165 unlink mac_path($mac);
166 symlink ip_path($new), mac_path($mac);
171 sort { ip::to_int($a) cmp ip::to_int($b) }
175 autocreate_params( $ip );
177 } glob("$server::conf/ip/*")
182 if ( my $mac = mac_from_ip $ip ) {
183 unlink "$server::conf/mac/$mac";
185 rmtree "$server::conf/ip/$ip";
191 my @c = split(/\s+/,$_);
193 client::save_ip_mac( $c[0], $c[3] );
194 ( uc $c[3] => $c[5] )
197 } read_file('/proc/net/arp')
200 warn "# arp ",dump( $arp );
204 sub rebuild_mac_links {
205 warn "# rebuild mac links";
206 foreach my $ip ( all_ips ) {
207 my $mac = ip_path $ip, 'mac';
209 $mac = read_file $mac;
211 save_ip_mac( $ip, $mac );
212 warn "## $ip $mac\n";
217 sub autocreate_params {
219 my $mac = mac_from_ip $ip;
220 if ( $mac =~ m{^AC:DE:48:00:00} && ! defined conf( $ip, 'kvm' ) ) {
221 conf( $ip, 'kvm', default => kvm::nr_from_mac( $mac ) );
222 warn "# create kvm for $ip";