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 ( -f $path ) {
66 $value = read_file $path;
68 warn "# $name missing $path\n" if $debug;
75 my $path = ip_path $ip || return;
77 foreach my $file ( glob("$path/*") ) {
79 $name =~ s{^.+/([^/]+)$}{$1};
80 $conf->{ $name } = read_file $file;
86 $mac = format::mac($mac);
88 if ( $server::new_clients > 0 ) {
89 warn "# clients left: ", --$server::new_clients;
91 warn "W: no new clients accepted";
95 my $prefix = $server::ip;
96 $prefix =~ s{\.\d+$}{.};
97 my $addr = $server::ip_from || die;
98 my $ip = $prefix . $addr;
100 while ( -e ip_path($ip) || ping::host($ip) ) {
101 $ip = $prefix . $addr++;
102 die "all addresses allocated!" if $addr == $server::ip_to;
106 warn "next_ip $ip\n";
108 save_ip_mac( $ip, $mac );
115 $mac = format::mac($mac);
116 return if $mac eq '00:00:00:00:00:00';
118 mkdir ip_path($ip) unless -e ip_path($ip);
120 my $mac_path = mac_path($mac);
121 unlink $mac_path if -l $mac_path; # XXX audit?
122 symlink ip_path($ip), $mac_path;
123 write_file( ip_path($ip,'mac'), $mac );
128 $mac = format::mac($mac);
130 my $mac_path = mac_path $mac;
131 return unless -e $mac_path;
135 if ( -f $mac_path ) {
136 $ip = read_file $mac_path;
138 symlink ip_path($ip), $mac_path;
139 warn "I: upgrade to mac symlink $mac_path\n";
140 } elsif ( -l $mac_path ) {
141 $ip = conf_value $mac_path;
143 die "$mac_path not file or symlink";
151 conf_value ip_path($ip, 'mac');
155 my ($old, $new) = @_;
156 return if $old eq $new;
157 my $mac = mac_from_ip($old) || die "no mac for $old";
158 rename ip_path($old), ip_path($new);
159 unlink mac_path($mac);
160 symlink ip_path($new), mac_path($mac);
165 sort { ip::to_int($a) cmp ip::to_int($b) }
169 autocreate_params( $ip );
171 } glob("$server::conf/ip/*")
176 unlink $_ foreach grep { -e $_ } ( glob "$server::conf/ip/$ip/*" );
177 if ( my $mac = mac_from_ip $ip ) {
178 unlink "$server::conf/mac/$mac";
180 rmtree "$server::conf/ip/$ip";
186 my @c = split(/\s+/,$_);
188 client::save_ip_mac( $c[0], $c[3] );
189 ( uc $c[3] => $c[5] )
192 } read_file('/proc/net/arp')
195 warn "# arp ",dump( $arp );
199 sub rebuild_mac_links {
200 warn "# rebuild mac links";
201 foreach my $ip ( all_ips ) {
202 my $mac = ip_path $ip, 'mac';
204 $mac = read_file $mac;
205 save_ip_mac( $ip, $mac );
206 warn "## $ip $mac\n";
211 sub autocreate_params {
213 my $mac = mac_from_ip $ip;
214 if ( $mac =~ m{^AC:DE:48:00:00} && ! defined conf( $ip, 'kvm' ) ) {
215 conf( $ip, 'kvm', default => kvm::nr_from_mac( $mac ) );
216 warn "# create kvm for $ip";