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 |
|
|
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 |
|
|
104 |
|
|
105 |
sub save_ip_mac { |
sub save_ip_mac { |
106 |
my ($ip,$mac) = @_; |
my ($ip,$mac) = @_; |
107 |
|
$mac = format::mac($mac); |
108 |
|
|
109 |
mkdir ip_path($ip) unless -e $ip; |
mkdir ip_path($ip) unless -e ip_path($ip); |
110 |
|
|
111 |
my $mac_path = mac_path($mac); |
my $mac_path = mac_path($mac); |
112 |
unlink $mac_path if -e $mac_path; # XXX audit? |
unlink $mac_path if -l $mac_path; # XXX audit? |
113 |
symlink ip_path($ip), $mac_path; |
symlink ip_path($ip), $mac_path; |
114 |
write_file ip_path($ip,'mac'), $mac; |
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; |
144 |
|
|
145 |
sub change_ip($$) { |
sub change_ip($$) { |
146 |
my ($old, $new) = @_; |
my ($old, $new) = @_; |
147 |
my $mac = mac_from_ip($old); |
return if $old eq $new; |
148 |
|
my $mac = mac_from_ip($old) || die "no mac for $old"; |
149 |
rename ip_path($old), ip_path($new); |
rename ip_path($old), ip_path($new); |
150 |
unlink mac_path($mac); |
unlink mac_path($mac); |
151 |
symlink ip_path($new), mac_path($mac); |
symlink ip_path($new), mac_path($mac); |
152 |
|
return $new; |
153 |
|
} |
154 |
|
|
155 |
|
sub all_ips { |
156 |
|
sort { ip::to_int($a) cmp ip::to_int($b) } |
157 |
|
map { |
158 |
|
my $ip = $_; |
159 |
|
$ip =~ s{^.+/ip/}{}; |
160 |
|
$ip; |
161 |
|
} glob("$server::conf/ip/*") |
162 |
|
} |
163 |
|
|
164 |
|
sub remove { |
165 |
|
my $ip = shift; |
166 |
|
unlink $_ foreach grep { -e $_ } ( glob "$server::conf/ip/$ip/*" ); |
167 |
|
if ( my $mac = mac_from_ip $ip ) { |
168 |
|
unlink "$server::conf/mac/$mac"; |
169 |
|
} |
170 |
|
rmdir "$server::conf/ip/$ip"; |
171 |
} |
} |
172 |
|
|
173 |
1; |
1; |