4 |
use strict; |
use strict; |
5 |
use autodie; |
use autodie; |
6 |
|
|
|
use server; |
|
7 |
use File::Slurp; |
use File::Slurp; |
8 |
use Net::Ping; |
use Net::Ping; |
9 |
|
|
10 |
|
use server; |
11 |
|
use format; |
12 |
|
use ip; |
13 |
|
|
14 |
|
our $debug = $server::debug; |
15 |
|
|
16 |
|
sub mkbasedir { |
17 |
|
my $path = shift; |
18 |
|
$path =~ s{(^.*)/[^/]+$}{$1}; |
19 |
|
mkdir $path unless -d $path; |
20 |
|
return $path; |
21 |
|
} |
22 |
|
|
23 |
sub mac_path { $server::conf . '/mac/' . $_[0] } |
sub mac_path { $server::conf . '/mac/' . $_[0] } |
24 |
sub ip_path { $server::conf . '/ip/' . join('/', @_) } |
sub ip_path { $server::conf . '/ip/' . join('/', @_) } |
25 |
|
sub conf_value { |
26 |
|
my $path = shift; |
27 |
|
my $value; |
28 |
|
if ( -l $path ) { |
29 |
|
$value = readlink $path; |
30 |
|
$value =~ s{.*/([^/]+)$}{$1}; |
31 |
|
} elsif ( -f $path ) { |
32 |
|
$value = read_file $path; |
33 |
|
} else { |
34 |
|
warn "W: $path not file or symlink\n"; |
35 |
|
} |
36 |
|
return $value; |
37 |
|
} |
38 |
|
|
39 |
sub conf { |
sub conf { |
40 |
my $ip = shift; |
my $ip = shift; |
51 |
$path .= '/' . $name; |
$path .= '/' . $name; |
52 |
|
|
53 |
if ( defined $value ) { |
if ( defined $value ) { |
54 |
|
mkbasedir $path; |
55 |
write_file $path, $value; |
write_file $path, $value; |
56 |
warn "update $path = $value"; |
warn "update $path = $value"; |
57 |
} elsif ( ! -e $path && defined $default ) { |
} elsif ( ! -e $path && defined $default ) { |
58 |
|
mkbasedir $path; |
59 |
write_file $path, $default; |
write_file $path, $default; |
60 |
warn "default $path = $default"; |
warn "default $path = $default"; |
61 |
$value = $default; |
$value = $default; |
62 |
} elsif ( -e $path ) { |
} elsif ( -f $path ) { |
63 |
if ( -l $path ) { |
$value = read_file $path; |
64 |
$value = readlink $path; |
} else { |
65 |
$value =~ s{.*/([^/]+)$}{$1}; |
warn "# $name missing $path\n" if $debug; |
|
} else { |
|
|
$value = read_file $path; |
|
|
} |
|
66 |
} |
} |
67 |
return $value; |
return $value; |
68 |
} |
} |
69 |
|
|
70 |
sub mac { |
sub all_conf { |
71 |
my ( $ip, $op ) = @_; |
my $ip = shift; |
72 |
$op ||= 'html'; |
my $path = ip_path $ip || return; |
73 |
my $mac = client::conf( $ip, 'mac' ); |
my $conf; |
74 |
return '' unless $mac; |
foreach my $file ( glob("$path/*") ) { |
75 |
$mac =~ s{(..)}{$1:}g; |
my $name = $file; |
76 |
$mac =~ s{:$}{}; |
$name =~ s{^.+/([^/]+)$}{$1}; |
77 |
$mac = qq|<tt>$mac</tt>| if (caller(1))[3] =~ m{^httpd} && $op ne 'clean'; |
$conf->{ $name } = read_file $file; |
78 |
return uc($mac); |
} |
79 |
|
return $conf; |
80 |
} |
} |
|
|
|
81 |
sub next_ip($) { |
sub next_ip($) { |
82 |
my $mac = shift; |
my $mac = shift; |
83 |
|
$mac = format::mac($mac); |
84 |
|
|
85 |
my $p = Net::Ping->new; |
my $p = Net::Ping->new; |
86 |
|
|
97 |
|
|
98 |
warn "next_ip $ip\n"; |
warn "next_ip $ip\n"; |
99 |
|
|
100 |
mkdir ip_path($ip); |
save_ip_mac( $ip, $mac ); |
|
|
|
|
symlink ip_path($ip), mac_path($mac); |
|
|
write_file ip_path($ip,'mac'), $mac; |
|
101 |
|
|
102 |
return $ip; |
return $ip; |
103 |
|
} |
104 |
|
|
105 |
|
sub save_ip_mac { |
106 |
|
my ($ip,$mac) = @_; |
107 |
|
$mac = format::mac($mac); |
108 |
|
|
109 |
|
mkdir ip_path($ip) unless -e ip_path($ip); |
110 |
|
|
111 |
|
my $mac_path = mac_path($mac); |
112 |
|
unlink $mac_path if -l $mac_path; # XXX audit? |
113 |
|
symlink ip_path($ip), $mac_path; |
114 |
|
write_file ip_path($ip,'mac'), $mac; |
115 |
} |
} |
116 |
|
|
117 |
sub ip_from_mac($) { |
sub ip_from_mac($) { |
118 |
my $mac = shift; |
my $mac = shift; |
119 |
|
$mac = format::mac($mac); |
|
$mac = lc $mac; |
|
|
$mac =~ s{:}{}g; |
|
120 |
|
|
121 |
my $mac_path = mac_path $mac; |
my $mac_path = mac_path $mac; |
122 |
return unless -e $mac_path; |
return unless -e $mac_path; |
129 |
symlink ip_path($ip), $mac_path; |
symlink ip_path($ip), $mac_path; |
130 |
warn "I: upgrade to mac symlink $mac_path\n"; |
warn "I: upgrade to mac symlink $mac_path\n"; |
131 |
} elsif ( -l $mac_path ) { |
} elsif ( -l $mac_path ) { |
132 |
$ip = readlink $mac_path; |
$ip = conf_value $mac_path; |
|
$ip =~ s{^.+/([^/]+)$}{$1}; |
|
133 |
} else { |
} else { |
134 |
die "$mac_path not file or symlink"; |
die "$mac_path not file or symlink"; |
135 |
} |
} |
139 |
|
|
140 |
sub mac_from_ip($) { |
sub mac_from_ip($) { |
141 |
my $ip = shift; |
my $ip = shift; |
142 |
return read_file ip_path($ip, 'mac'); |
conf_value ip_path($ip, 'mac'); |
143 |
} |
} |
144 |
|
|
145 |
sub change_ip($$) { |
sub change_ip($$) { |
150 |
symlink ip_path($new), mac_path($mac); |
symlink ip_path($new), mac_path($mac); |
151 |
} |
} |
152 |
|
|
153 |
|
sub all_ips { |
154 |
|
sort { ip::to_int($a) cmp ip::to_int($b) } |
155 |
|
map { |
156 |
|
my $ip = $_; |
157 |
|
$ip =~ s{^.+/ip/}{}; |
158 |
|
$ip; |
159 |
|
} glob("$server::conf/ip/*") |
160 |
|
} |
161 |
|
|
162 |
1; |
1; |