--- lib/PXElator/dhcpd.pm 2009/08/06 18:09:17 160 +++ lib/PXElator/dhcpd.pm 2009/08/07 14:08:21 177 @@ -18,7 +18,6 @@ use IO::Socket::INET; use File::Slurp; use Data::Dump qw/dump/; -use Net::Ping; use lib '..'; use Net::DHCP::Packet; @@ -35,7 +34,7 @@ warn "server ip $server::ip range: $server::ip_from - $server::ip_to\n"; -my $addr = $server::ip_from; +use client; sub client_ip { my ( $mac ) = @_; @@ -43,25 +42,18 @@ my $conf = $server::conf; mkdir $conf unless -e $conf; + my $ip; + if ( -e "$conf/mac/$mac" ) { - my $ip = read_file "$conf/mac/$mac"; + $ip = read_file "$conf/mac/$mac"; print "RENEW $mac $ip\n"; return $ip; + } else { + $ip = client::next_ip; + print "NEW $mac $ip\n"; + write_file "$conf/mac/$mac", $ip; } - my $p = Net::Ping->new; - - my $prefix = $server::ip; - $prefix =~ s{\.\d+$}{.}; - my $ip = $prefix . $addr; - - while ( -e "$conf/ip/$ip" || $p->ping( $ip ) ) { - $ip = $prefix . $addr++; - die "all addresses allocated!" if $addr == $server::ip_to; - } - - write_file "$conf/mac/$mac", $ip; - my $ip_path = "$conf/ip/$ip"; mkdir $ip_path unless -e $ip_path; @@ -69,9 +61,8 @@ warn "$mac IP changed from ", readlink "$ip_path/mac", " to $ip"; unlink "$ip_path/mac"; }; - symlink "$conf/mac/$mac", "$ip_path/mac"; - print "$mac NEW $ip\n"; + symlink "$conf/mac/$mac", "$ip_path/mac"; return $ip; } @@ -145,18 +136,26 @@ }; my @requested = split(/\s/, $dhcp->getOptionValue(DHO_DHCP_PARAMETER_REQUEST_LIST)); - warn "options ",dump( $options ), ' requested: ',dump( @requested ); + warn "options ",dump( $options ), ' requested: ',dump( @requested ) if $debug; + + my @missing; foreach ( @requested ) { - $packet->{$_} = $options->{$_} if defined $options->{$_}; + if ( defined $options->{$_} ) { + $packet->{$_} = $options->{$_}; + } else { + push @missing, $_; + } } + warn "W: options requested but missing: ",dump( @missing ),$/; + foreach my $opt ( 'magic', 'config_file', 'path_prefix', 'reboot_time' ) { my $DH0 = eval 'DHO_PXELINUX_' . uc $opt; warn "DH0: $@" if $@; my $v = eval "\$pxelinux::$opt"; warn "v: $@" if $@; next unless defined $v; - warn "pxelinux dhcp option $opt = $DH0 = $v"; + warn "pxelinux dhcp option $opt = $DH0 = $v\n" if $debug; $packet->{ $DH0 } = $v; } @@ -183,8 +182,6 @@ log::mac $mac, "$messagetype igored (bootp?)"; } - warn "packet ",dump( $packet ); - warn ">> $mac == $ip server: $server::ip", $file ? " file: $file\n" : "\n" if $debug; $packet = new Net::DHCP::Packet( %$packet );