7 |
use server; |
use server; |
8 |
use File::Slurp; |
use File::Slurp; |
9 |
use Net::Ping; |
use Net::Ping; |
10 |
|
use format; |
11 |
|
|
12 |
|
sub mac_path { $server::conf . '/mac/' . $_[0] } |
13 |
|
sub ip_path { $server::conf . '/ip/' . join('/', @_) } |
14 |
|
|
15 |
sub conf { |
sub conf { |
16 |
my $ip = shift; |
my $ip = shift; |
22 |
$default = $_[1] |
$default = $_[1] |
23 |
} |
} |
24 |
|
|
25 |
my $path ="$server::conf/ip/$ip"; |
my $path = ip_path $ip; |
26 |
mkdir $path unless -d $path; |
mkdir $path unless -d $path; |
27 |
$path .= '/' . $name; |
$path .= '/' . $name; |
28 |
|
|
45 |
} |
} |
46 |
|
|
47 |
sub mac { |
sub mac { |
48 |
my ( $ip, $op ) = @_; |
my $ip = shift; |
49 |
$op ||= 'html'; |
my $mac = client::conf( $ip, 'mac' ) || return ''; |
50 |
my $mac = client::conf( $ip, 'mac' ); |
format::mac( $ip, @_ ); |
|
return '' unless $mac; |
|
|
$mac =~ s{(..)}{$1:}g; |
|
|
$mac =~ s{:$}{}; |
|
|
$mac = qq|<tt>$mac</tt>| if (caller(1))[3] =~ m{^httpd} && $op ne 'clean'; |
|
|
return uc($mac); |
|
51 |
} |
} |
52 |
|
|
53 |
sub next_ip { |
sub next_ip($) { |
54 |
|
my $mac = shift; |
55 |
|
|
56 |
my $p = Net::Ping->new; |
my $p = Net::Ping->new; |
57 |
|
|
60 |
my $addr = $server::ip_from || die; |
my $addr = $server::ip_from || die; |
61 |
my $ip = $prefix . $addr; |
my $ip = $prefix . $addr; |
62 |
|
|
63 |
while ( -e "$server::conf/ip/$ip" || $p->ping( $ip, 0.7 ) ) { |
while ( -e ip_path($ip) || $p->ping( $ip, 0.7 ) ) { |
64 |
$ip = $prefix . $addr++; |
$ip = $prefix . $addr++; |
65 |
die "all addresses allocated!" if $addr == $server::ip_to; |
die "all addresses allocated!" if $addr == $server::ip_to; |
66 |
warn "skip $ip\n"; |
warn "skip $ip\n"; |
67 |
} |
} |
68 |
|
|
69 |
warn "next_ip $ip\n"; |
warn "next_ip $ip\n"; |
70 |
|
|
71 |
|
mkdir ip_path($ip); |
72 |
|
|
73 |
|
symlink ip_path($ip), mac_path($mac); |
74 |
|
write_file ip_path($ip,'mac'), $mac; |
75 |
|
|
76 |
return $ip; |
return $ip; |
77 |
|
|
78 |
} |
} |
79 |
|
|
80 |
sub ip_from_mac { |
sub ip_from_mac($) { |
81 |
my $mac = shift; |
my $mac = shift; |
82 |
|
|
83 |
$mac = lc $mac; |
$mac = lc $mac; |
84 |
$mac =~ s{:}{}g; |
$mac =~ s{:}{}g; |
85 |
|
|
86 |
my $mac_path = "$server::conf/mac/$mac"; |
my $mac_path = mac_path $mac; |
87 |
return unless -e $mac_path; |
return unless -e $mac_path; |
88 |
|
|
89 |
my $ip; |
my $ip; |
91 |
if ( -f $mac_path ) { |
if ( -f $mac_path ) { |
92 |
$ip = read_file $mac_path; |
$ip = read_file $mac_path; |
93 |
unlink $mac_path; |
unlink $mac_path; |
94 |
symlink "$server::conf/ip/$ip", $mac_path; |
symlink ip_path($ip), $mac_path; |
95 |
warn "I: upgrade to mac symlink $mac_path\n"; |
warn "I: upgrade to mac symlink $mac_path\n"; |
96 |
} elsif ( -l $mac_path ) { |
} elsif ( -l $mac_path ) { |
97 |
$ip = readlink $mac_path; |
$ip = readlink $mac_path; |
103 |
return $ip; |
return $ip; |
104 |
} |
} |
105 |
|
|
106 |
|
sub mac_from_ip($) { |
107 |
|
my $ip = shift; |
108 |
|
return read_file ip_path($ip, 'mac'); |
109 |
|
} |
110 |
|
|
111 |
|
sub change_ip($$) { |
112 |
|
my ($old, $new) = @_; |
113 |
|
my $mac = mac_from_ip($old); |
114 |
|
rename ip_path($old), ip_path($new); |
115 |
|
unlink mac_path($mac); |
116 |
|
symlink ip_path($new), mac_path($mac); |
117 |
|
} |
118 |
|
|
119 |
1; |
1; |