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 |
|
|
13 |
|
our $debug = $server::debug; |
14 |
|
|
15 |
sub mkbasedir { |
sub mkbasedir { |
16 |
my $path = shift; |
my $path = shift; |
17 |
$path =~ s{(^.*)/[^/]+$}{$1}; |
$path =~ s{(^.*)/[^/]+$}{$1}; |
30 |
} elsif ( -f $path ) { |
} elsif ( -f $path ) { |
31 |
$value = read_file $path; |
$value = read_file $path; |
32 |
} else { |
} else { |
33 |
confess "$path not file or symlink"; |
warn "W: $path not file or symlink\n"; |
34 |
} |
} |
35 |
return $value; |
return $value; |
36 |
} |
} |
61 |
} elsif ( -f $path ) { |
} elsif ( -f $path ) { |
62 |
$value = read_file $path; |
$value = read_file $path; |
63 |
} else { |
} else { |
64 |
confess "conf $name"; |
warn "# $name missing $path\n" if $debug; |
65 |
} |
} |
66 |
return $value; |
return $value; |
67 |
} |
} |
68 |
|
|
69 |
|
sub all_conf { |
70 |
|
my $ip = shift; |
71 |
|
my $path = ip_path $ip || return; |
72 |
|
my $conf; |
73 |
|
foreach my $file ( glob("$path/*") ) { |
74 |
|
my $name = $file; |
75 |
|
$name =~ s{^.+/([^/]+)$}{$1}; |
76 |
|
$conf->{ $name } = read_file $file; |
77 |
|
} |
78 |
|
return $conf; |
79 |
|
} |
80 |
sub next_ip($) { |
sub next_ip($) { |
81 |
my $mac = shift; |
my $mac = shift; |
82 |
|
$mac = format::mac($mac); |
83 |
|
|
84 |
my $p = Net::Ping->new; |
my $p = Net::Ping->new; |
85 |
|
|
96 |
|
|
97 |
warn "next_ip $ip\n"; |
warn "next_ip $ip\n"; |
98 |
|
|
99 |
mkdir ip_path($ip); |
save_ip_mac( $ip, $mac ); |
100 |
|
|
101 |
|
return $ip; |
102 |
|
} |
103 |
|
|
104 |
|
sub save_ip_mac { |
105 |
|
my ($ip,$mac) = @_; |
106 |
|
$mac = format::mac($mac); |
107 |
|
|
108 |
|
mkdir ip_path($ip) unless -e ip_path($ip); |
109 |
|
|
110 |
my $mac_path = mac_path($mac); |
my $mac_path = mac_path($mac); |
111 |
unlink $mac_path if -e $mac_path; # XXX audit? |
unlink $mac_path if -l $mac_path; # XXX audit? |
112 |
symlink ip_path($ip), $mac_path; |
symlink ip_path($ip), $mac_path; |
113 |
write_file ip_path($ip,'mac'), $mac; |
write_file ip_path($ip,'mac'), $mac; |
|
|
|
|
return $ip; |
|
|
|
|
114 |
} |
} |
115 |
|
|
116 |
sub ip_from_mac($) { |
sub ip_from_mac($) { |
117 |
my $mac = shift; |
my $mac = shift; |
118 |
|
$mac = format::mac($mac); |
|
$mac = lc $mac; |
|
|
$mac =~ s{:}{}g; |
|
119 |
|
|
120 |
my $mac_path = mac_path $mac; |
my $mac_path = mac_path $mac; |
121 |
return unless -e $mac_path; |
return unless -e $mac_path; |