14 |
|
|
15 |
my $debug = shift @ARGV; |
my $debug = shift @ARGV; |
16 |
|
|
17 |
our $server_ip; |
our ( $server_ip, $file, $next_file ); |
|
require "config.pl"; |
|
18 |
|
|
19 |
my $sock = IO::Socket::INET->new( |
my $sock = IO::Socket::INET->new( |
20 |
LocalPort => 67, |
LocalPort => 67, |
30 |
|
|
31 |
my $_ip = 10; |
my $_ip = 10; |
32 |
my $_mac2ip; |
my $_mac2ip; |
|
my $_ip_file; |
|
33 |
|
|
34 |
sub client_ip { |
sub client_ip { |
35 |
my ( $mac ) = @_; |
my ( $mac ) = @_; |
37 |
my $ip = $_mac2ip->{$mac}; |
my $ip = $_mac2ip->{$mac}; |
38 |
return $ip if $ip; |
return $ip if $ip; |
39 |
|
|
40 |
$ip = "10.0.0.$_ip"; |
$ip = $server_ip; |
41 |
|
$ip =~ s{\.\d+$}{.$_ip}; |
42 |
$_mac2ip->{$mac} = $ip; |
$_mac2ip->{$mac} = $ip; |
43 |
|
|
44 |
$_ip++; |
$_ip++; |
52 |
|
|
53 |
while (1) { |
while (1) { |
54 |
|
|
55 |
|
require "config.pl"; # refresh config |
56 |
|
|
57 |
print "waiting for DHCP requests on ",$sock->sockhost,":",$sock->sockport,"\n"; |
print "waiting for DHCP requests on ",$sock->sockhost,":",$sock->sockport,"\n"; |
58 |
|
|
59 |
my $buf; |
my $buf; |
74 |
my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2); |
my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2); |
75 |
my $ip = client_ip($mac); |
my $ip = client_ip($mac); |
76 |
|
|
77 |
|
if ( ! $file ) { |
78 |
|
if ( $dhcp->getOptionValue(DHO_USER_CLASS()) ne 'gPXE' ) { |
79 |
|
$file = 'undionly.kpxe'; |
80 |
|
} else { |
81 |
|
$file = $next_file; |
82 |
|
} |
83 |
|
} |
84 |
|
|
85 |
my $packet = new Net::DHCP::Packet( |
my $packet = new Net::DHCP::Packet( |
86 |
Op => BOOTREPLY(), |
Op => BOOTREPLY(), |
87 |
Hops => $dhcp->hops(), |
Hops => $dhcp->hops(), |
92 |
Siaddr => $server_ip, |
Siaddr => $server_ip, |
93 |
Giaddr => $dhcp->giaddr(), |
Giaddr => $dhcp->giaddr(), |
94 |
Chaddr => $dhcp->chaddr(), |
Chaddr => $dhcp->chaddr(), |
95 |
File => 'undionly.kpxe', |
File => $file, |
96 |
# DHO_DHCP_MESSAGE_TYPE() => DHCPACK(), |
# DHO_DHCP_MESSAGE_TYPE() => DHCPACK(), |
97 |
DHO_SUBNET_MASK() => '255.0.0.0', |
DHO_SUBNET_MASK() => '255.0.0.0', |
98 |
); |
); |