--- bin/dhcpd.pl 2009/07/26 14:21:48 4 +++ bin/dhcpd.pl 2009/07/27 22:18:51 12 @@ -14,8 +14,7 @@ my $debug = shift @ARGV; -our ( $server_ip, $next_file ); -require "config.pl"; +our ( $server_ip, $file, $next_file ); my $sock = IO::Socket::INET->new( LocalPort => 67, @@ -31,7 +30,6 @@ my $_ip = 10; my $_mac2ip; -my $_ip_file; sub client_ip { my ( $mac ) = @_; @@ -39,7 +37,8 @@ my $ip = $_mac2ip->{$mac}; return $ip if $ip; - $ip = "10.0.0.$_ip"; + $ip = $server_ip; + $ip =~ s{\.\d+$}{.$_ip}; $_mac2ip->{$mac} = $ip; $_ip++; @@ -53,6 +52,8 @@ while (1) { + require "config.pl"; # refresh config + print "waiting for DHCP requests on ",$sock->sockhost,":",$sock->sockport,"\n"; my $buf; @@ -73,6 +74,14 @@ my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2); my $ip = client_ip($mac); + if ( ! $file ) { + if ( $dhcp->getOptionValue(DHO_USER_CLASS()) ne 'gPXE' ) { + $file = 'undionly.kpxe'; + } else { + $file = $next_file; + } + } + my $packet = new Net::DHCP::Packet( Op => BOOTREPLY(), Hops => $dhcp->hops(), @@ -83,13 +92,11 @@ Siaddr => $server_ip, Giaddr => $dhcp->giaddr(), Chaddr => $dhcp->chaddr(), - File => $_ip_file->{$ip} || 'undionly.kpxe', + File => $file, # DHO_DHCP_MESSAGE_TYPE() => DHCPACK(), DHO_SUBNET_MASK() => '255.0.0.0', ); - $_ip_file->{$ip} = $next_file; - warn ">> $mac == $ip server $server_ip\n"; warn "## ",$packet->toString(),"\n" if $debug;