6 |
|
|
7 |
use server; |
use server; |
8 |
use File::Slurp; |
use File::Slurp; |
9 |
|
use Net::Ping; |
10 |
|
|
11 |
sub conf { |
sub conf { |
12 |
my $ip = shift; |
my $ip = shift; |
29 |
write_file $path, $default; |
write_file $path, $default; |
30 |
warn "default $path = $default"; |
warn "default $path = $default"; |
31 |
$value = $default; |
$value = $default; |
32 |
} else { |
} elsif ( -e $path ) { |
33 |
$value = read_file $path if -e $path; |
if ( -l $path ) { |
34 |
|
$value = readlink $path; |
35 |
|
$value =~ s{.*/([^/]+)$}{$1}; |
36 |
|
} else { |
37 |
|
$value = read_file $path; |
38 |
|
} |
39 |
} |
} |
40 |
return $value; |
return $value; |
41 |
} |
} |
42 |
|
|
43 |
|
sub mac { |
44 |
|
my ( $ip, $op ) = @_; |
45 |
|
$op ||= 'html'; |
46 |
|
my $mac = client::conf( $ip, 'mac' ); |
47 |
|
return '' unless $mac; |
48 |
|
$mac =~ s{(..)}{$1:}g; |
49 |
|
$mac =~ s{:$}{}; |
50 |
|
$mac = qq|<tt>$mac</tt>| if (caller(1))[3] =~ m{^httpd} && $op ne 'clean'; |
51 |
|
return uc($mac); |
52 |
|
} |
53 |
|
|
54 |
|
sub next_ip { |
55 |
|
|
56 |
|
my $p = Net::Ping->new; |
57 |
|
|
58 |
|
my $prefix = $server::ip; |
59 |
|
$prefix =~ s{\.\d+$}{.}; |
60 |
|
my $addr = $server::ip_from || die; |
61 |
|
my $ip = $prefix . $addr; |
62 |
|
|
63 |
|
while ( -e "$server::conf/ip/$ip" || $p->ping( $ip, 0.7 ) ) { |
64 |
|
$ip = $prefix . $addr++; |
65 |
|
die "all addresses allocated!" if $addr == $server::ip_to; |
66 |
|
warn "skip $ip\n"; |
67 |
|
} |
68 |
|
|
69 |
|
warn "next_ip $ip\n"; |
70 |
|
return $ip; |
71 |
|
|
72 |
|
} |
73 |
|
|
74 |
|
sub ip_from_mac { |
75 |
|
my $mac = shift; |
76 |
|
|
77 |
|
$mac = lc $mac; |
78 |
|
$mac =~ s{:}{}g; |
79 |
|
|
80 |
|
my $mac_path = "$server::conf/mac/$mac"; |
81 |
|
return unless -e $mac_path; |
82 |
|
|
83 |
|
my $ip; |
84 |
|
|
85 |
|
if ( -f $mac_path ) { |
86 |
|
$ip = read_file $mac_path; |
87 |
|
unlink $mac_path; |
88 |
|
symlink "$server::conf/ip/$ip", $mac_path; |
89 |
|
warn "I: upgrade to mac symlink $mac_path\n"; |
90 |
|
} elsif ( -l $mac_path ) { |
91 |
|
$ip = readlink $mac_path; |
92 |
|
$ip =~ s{^.+/([^/]+)$}{$1}; |
93 |
|
} else { |
94 |
|
die "$mac_path not file or symlink"; |
95 |
|
} |
96 |
|
|
97 |
|
return $ip; |
98 |
|
} |
99 |
|
|
100 |
1; |
1; |