5 |
use strict; |
use strict; |
6 |
use warnings; |
use warnings; |
7 |
|
|
8 |
|
use autodie; |
9 |
|
|
10 |
use IO::Socket::INET; |
use IO::Socket::INET; |
11 |
use Net::DHCP::Packet; |
use Net::DHCP::Packet; |
12 |
use Net::DHCP::Constants; |
use Net::DHCP::Constants; |
17 |
|
|
18 |
my $debug = shift @ARGV; |
my $debug = shift @ARGV; |
19 |
|
|
20 |
our ( $file, $next_file ); |
our ( $file, $gpxe_file ); |
21 |
our ( $ip_from, $ip_to ) = ( 10, 100 ); |
our ( $ip_from, $ip_to ) = ( 10, 100 ); |
22 |
|
|
23 |
our $server_ip = readlink 'conf/server.ip'; |
our $server_ip = readlink 'conf/server.ip' if -l 'conf/server.ip'; |
24 |
|
|
25 |
|
if ( ! $server_ip ) { |
26 |
|
$server_ip = `/sbin/ifconfig`; |
27 |
|
$server_ip =~ s/^.+?addr:([\d\.]+).*$/$1/gs; |
28 |
|
warn "auto-configure server ip to $server_ip\n"; |
29 |
|
} else { |
30 |
|
warn "server ip $server_ip\n"; |
31 |
|
} |
32 |
|
|
33 |
my $sock = IO::Socket::INET->new( |
my $sock = IO::Socket::INET->new( |
34 |
LocalPort => 67, |
LocalPort => 67, |
51 |
my $conf = "conf/$server_ip"; |
my $conf = "conf/$server_ip"; |
52 |
mkdir $conf unless -e $conf; |
mkdir $conf unless -e $conf; |
53 |
|
|
54 |
if ( -e "$conf/$mac" ) { |
if ( -e "$conf/mac/$mac" ) { |
55 |
my $ip = read_file "conf/mac/$mac"; |
my $ip = read_file "$conf/mac/$mac"; |
56 |
print "$mac old $ip\n"; |
print "$mac old $ip\n"; |
57 |
return $ip; |
return $ip; |
58 |
} |
} |
68 |
} |
} |
69 |
|
|
70 |
write_file "$conf/mac/$mac", $ip; |
write_file "$conf/mac/$mac", $ip; |
71 |
symlink "$conf/mac/$mac", "conf/ip/$ip"; |
unlink "$conf/ip/$ip" if -e "$conf/ip/$ip"; |
72 |
|
symlink "$conf/mac/$mac", "$conf/ip/$ip"; |
73 |
|
|
74 |
print "$mac NEW $ip\n"; |
print "$mac NEW $ip\n"; |
75 |
|
|
99 |
|
|
100 |
my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2); |
my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2); |
101 |
my $ip = client_ip($mac); |
my $ip = client_ip($mac); |
102 |
|
my $user_class = $dhcp->getOptionValue(DHO_USER_CLASS()); |
103 |
|
|
104 |
if ( ! $file ) { |
if ( $user_class eq 'gPXE' ) { |
105 |
if ( $dhcp->getOptionValue(DHO_USER_CLASS()) ne 'gPXE' ) { |
$file = $gpxe_file; |
106 |
$file = 'undionly.kpxe'; |
} elsif ( ! $file ) { |
107 |
} else { |
$file = 'undionly.kpxe'; |
|
$file = $next_file; |
|
|
} |
|
108 |
} |
} |
109 |
|
|
110 |
my $packet = new Net::DHCP::Packet( |
my $packet = new Net::DHCP::Packet( |
122 |
DHO_SUBNET_MASK() => '255.0.0.0', |
DHO_SUBNET_MASK() => '255.0.0.0', |
123 |
); |
); |
124 |
|
|
125 |
warn ">> $mac == $ip server $server_ip\n"; |
warn ">> $mac == $ip server: $server_ip file: $file\n"; |
126 |
|
|
127 |
warn "## ",$packet->toString(),"\n" if $debug; |
warn "## ",$packet->toString(),"\n" if $debug; |
128 |
|
|
129 |
my $reply = IO::Socket::INET->new( |
my $reply = IO::Socket::INET->new( |