6 |
|
|
7 |
use File::Slurp; |
use File::Slurp; |
8 |
use Net::Ping; |
use Net::Ping; |
|
use Carp qw/confess/; |
|
9 |
|
|
10 |
use server; |
use server; |
11 |
use format; |
use format; |
12 |
|
use ip; |
13 |
|
|
14 |
our $debug = $server::debug; |
our $debug = $server::debug; |
15 |
|
|
31 |
} elsif ( -f $path ) { |
} elsif ( -f $path ) { |
32 |
$value = read_file $path; |
$value = read_file $path; |
33 |
} else { |
} else { |
34 |
confess "$path not file or symlink"; |
warn "W: $path not file or symlink\n"; |
35 |
} |
} |
36 |
return $value; |
return $value; |
37 |
} |
} |
67 |
return $value; |
return $value; |
68 |
} |
} |
69 |
|
|
70 |
|
sub all_conf { |
71 |
|
my $ip = shift; |
72 |
|
my $path = ip_path $ip || return; |
73 |
|
my $conf; |
74 |
|
foreach my $file ( glob("$path/*") ) { |
75 |
|
my $name = $file; |
76 |
|
$name =~ s{^.+/([^/]+)$}{$1}; |
77 |
|
$conf->{ $name } = read_file $file; |
78 |
|
} |
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 ); |
|
|
|
|
my $mac_path = mac_path($mac); |
|
|
unlink $mac_path if -e $mac_path; # XXX audit? |
|
|
symlink ip_path($ip), $mac_path; |
|
|
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 |
|
return if $mac eq '00:00:00:00:00:00'; |
109 |
|
|
110 |
|
mkdir ip_path($ip) unless -e ip_path($ip); |
111 |
|
|
112 |
|
my $mac_path = mac_path($mac); |
113 |
|
unlink $mac_path if -l $mac_path; # XXX audit? |
114 |
|
symlink ip_path($ip), $mac_path; |
115 |
|
write_file( ip_path($ip,'mac'), $mac ); |
116 |
} |
} |
117 |
|
|
118 |
sub ip_from_mac($) { |
sub ip_from_mac($) { |
119 |
my $mac = shift; |
my $mac = shift; |
120 |
|
$mac = format::mac($mac); |
|
$mac = lc $mac; |
|
|
$mac =~ s{:}{}g; |
|
121 |
|
|
122 |
my $mac_path = mac_path $mac; |
my $mac_path = mac_path $mac; |
123 |
return unless -e $mac_path; |
return unless -e $mac_path; |
145 |
|
|
146 |
sub change_ip($$) { |
sub change_ip($$) { |
147 |
my ($old, $new) = @_; |
my ($old, $new) = @_; |
148 |
my $mac = mac_from_ip($old); |
return if $old eq $new; |
149 |
|
my $mac = mac_from_ip($old) || die "no mac for $old"; |
150 |
rename ip_path($old), ip_path($new); |
rename ip_path($old), ip_path($new); |
151 |
unlink mac_path($mac); |
unlink mac_path($mac); |
152 |
symlink ip_path($new), mac_path($mac); |
symlink ip_path($new), mac_path($mac); |
153 |
|
return $new; |
154 |
|
} |
155 |
|
|
156 |
|
sub all_ips { |
157 |
|
sort { ip::to_int($a) cmp ip::to_int($b) } |
158 |
|
map { |
159 |
|
my $ip = $_; |
160 |
|
$ip =~ s{^.+/ip/}{}; |
161 |
|
$ip; |
162 |
|
} glob("$server::conf/ip/*") |
163 |
|
} |
164 |
|
|
165 |
|
sub remove { |
166 |
|
my $ip = shift; |
167 |
|
unlink $_ foreach grep { -e $_ } ( glob "$server::conf/ip/$ip/*" ); |
168 |
|
if ( my $mac = mac_from_ip $ip ) { |
169 |
|
unlink "$server::conf/mac/$mac"; |
170 |
|
} |
171 |
|
rmdir "$server::conf/ip/$ip"; |
172 |
} |
} |
173 |
|
|
174 |
1; |
1; |