6 |
|
|
7 |
use File::Slurp; |
use File::Slurp; |
8 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
9 |
|
use File::Path; |
10 |
|
|
11 |
use server; |
use server; |
12 |
use format; |
use format; |
13 |
use ip; |
use ip; |
14 |
use ping; |
use ping; |
15 |
|
use kvm; |
16 |
|
|
17 |
our $debug = $server::debug; |
our $debug = $server::debug; |
18 |
|
|
50 |
} |
} |
51 |
|
|
52 |
my $path = ip_path $ip; |
my $path = ip_path $ip; |
53 |
mkdir $path unless -d $path; |
mkdir $path unless -e $path; |
54 |
|
warn "WARNING: $path not directory" unless -d $path; |
55 |
$path .= '/' . $name; |
$path .= '/' . $name; |
56 |
|
|
57 |
if ( defined $value ) { |
if ( defined $value ) { |
63 |
write_file $path, $default; |
write_file $path, $default; |
64 |
warn "default $path = $default"; |
warn "default $path = $default"; |
65 |
$value = $default; |
$value = $default; |
66 |
|
} elsif ( -l $path ) { |
67 |
|
$value = readlink $path; |
68 |
} elsif ( -f $path ) { |
} elsif ( -f $path ) { |
69 |
$value = read_file $path; |
$value = read_file $path; |
70 |
|
chomp $value; |
71 |
} else { |
} else { |
72 |
warn "# $name missing $path\n" if $debug; |
warn "# $name missing $path\n" if $debug; |
73 |
} |
} |
78 |
my $ip = shift; |
my $ip = shift; |
79 |
my $path = ip_path $ip || return; |
my $path = ip_path $ip || return; |
80 |
my $conf; |
my $conf; |
81 |
foreach my $file ( glob("$path/*") ) { |
foreach my $file ( glob("$path/*"), glob("$path/*/*") ) { |
82 |
my $name = $file; |
my $name = $file; |
83 |
$name =~ s{^.+/([^/]+)$}{$1}; |
$name =~ s{^$path/+}{} || die "can't remove $path from $name"; |
84 |
$conf->{ $name } = read_file $file; |
$conf->{ $name } = |
85 |
|
-l $file ? readlink $file : |
86 |
|
-f $file ? read_file $file : |
87 |
|
'?'; |
88 |
} |
} |
89 |
return $conf; |
return $conf; |
90 |
} |
} |
92 |
my $mac = shift; |
my $mac = shift; |
93 |
$mac = format::mac($mac); |
$mac = format::mac($mac); |
94 |
|
|
95 |
|
if ( $server::new_clients > 0 ) { |
96 |
|
warn "# clients left: ", --$server::new_clients; |
97 |
|
} else { |
98 |
|
warn "W: no new clients accepted"; |
99 |
|
return '0.0.0.0'; |
100 |
|
} |
101 |
|
|
102 |
my $prefix = $server::ip; |
my $prefix = $server::ip; |
103 |
$prefix =~ s{\.\d+$}{.}; |
$prefix =~ s{\.\d+$}{.}; |
104 |
my $addr = $server::ip_from || die; |
my $addr = $server::ip_from || die; |
120 |
sub save_ip_mac { |
sub save_ip_mac { |
121 |
my ($ip,$mac) = @_; |
my ($ip,$mac) = @_; |
122 |
$mac = format::mac($mac); |
$mac = format::mac($mac); |
123 |
return if $mac eq '00:00:00:00:00:00'; |
return if $mac eq '00:00:00:00:00:00' || $ip eq '0.0.0.0'; |
124 |
|
|
125 |
mkdir ip_path($ip) unless -e ip_path($ip); |
mkdir ip_path($ip) unless -e ip_path($ip); |
126 |
|
|
173 |
map { |
map { |
174 |
my $ip = $_; |
my $ip = $_; |
175 |
$ip =~ s{^.+/ip/}{}; |
$ip =~ s{^.+/ip/}{}; |
176 |
|
autocreate_params( $ip ); |
177 |
$ip; |
$ip; |
178 |
} glob("$server::conf/ip/*") |
} glob("$server::conf/ip/*") |
179 |
} |
} |
180 |
|
|
181 |
sub remove { |
sub remove { |
182 |
my $ip = shift; |
my $ip = shift; |
|
unlink $_ foreach grep { -e $_ } ( glob "$server::conf/ip/$ip/*" ); |
|
183 |
if ( my $mac = mac_from_ip $ip ) { |
if ( my $mac = mac_from_ip $ip ) { |
184 |
unlink "$server::conf/mac/$mac"; |
unlink "$server::conf/mac/$mac"; |
185 |
} |
} |
186 |
rmdir "$server::conf/ip/$ip"; |
rmtree "$server::conf/ip/$ip"; |
187 |
} |
} |
188 |
|
|
189 |
sub arp_mac_dev { |
sub arp_mac_dev { |
202 |
return $arp; |
return $arp; |
203 |
} |
} |
204 |
|
|
205 |
|
sub rebuild_mac_links { |
206 |
|
warn "# rebuild mac links"; |
207 |
|
foreach my $ip ( all_ips ) { |
208 |
|
my $mac = ip_path $ip, 'mac'; |
209 |
|
if ( -e $mac ) { |
210 |
|
$mac = read_file $mac; |
211 |
|
chomp $mac; |
212 |
|
save_ip_mac( $ip, $mac ); |
213 |
|
warn "## $ip $mac\n"; |
214 |
|
} |
215 |
|
} |
216 |
|
} |
217 |
|
|
218 |
|
sub autocreate_params { |
219 |
|
my $ip = shift; |
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"; |
224 |
|
} |
225 |
|
} |
226 |
|
|
227 |
1; |
1; |