--- bin/dhcpd.pl 2009/07/28 10:29:33 17 +++ bin/dhcpd.pl 2009/07/28 16:35:49 22 @@ -5,6 +5,8 @@ use strict; use warnings; +use autodie; + use IO::Socket::INET; use Net::DHCP::Packet; use Net::DHCP::Constants; @@ -15,10 +17,18 @@ my $debug = shift @ARGV; -our ( $file, $next_file ); +our ( $file, $gpxe_file ); our ( $ip_from, $ip_to ) = ( 10, 100 ); -our $server_ip = readlink 'conf/server.ip'; +our $server_ip = readlink 'conf/server.ip' if -l 'conf/server.ip'; + +if ( ! $server_ip ) { + $server_ip = `/sbin/ifconfig`; + $server_ip =~ s/^.+?addr:([\d\.]+).*$/$1/gs; + warn "auto-configure server ip to $server_ip\n"; +} else { + warn "server ip $server_ip\n"; +} my $sock = IO::Socket::INET->new( LocalPort => 67, @@ -41,8 +51,8 @@ my $conf = "conf/$server_ip"; mkdir $conf unless -e $conf; - if ( -e "$conf/$mac" ) { - my $ip = read_file "conf/mac/$mac"; + if ( -e "$conf/mac/$mac" ) { + my $ip = read_file "$conf/mac/$mac"; print "$mac old $ip\n"; return $ip; } @@ -58,7 +68,8 @@ } write_file "$conf/mac/$mac", $ip; - symlink "$conf/mac/$mac", "conf/ip/$ip"; + unlink "$conf/ip/$ip" if -e "$conf/ip/$ip"; + symlink "$conf/mac/$mac", "$conf/ip/$ip"; print "$mac NEW $ip\n"; @@ -88,13 +99,12 @@ my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2); my $ip = client_ip($mac); + my $user_class = $dhcp->getOptionValue(DHO_USER_CLASS()); - if ( ! $file ) { - if ( $dhcp->getOptionValue(DHO_USER_CLASS()) ne 'gPXE' ) { - $file = 'undionly.kpxe'; - } else { - $file = $next_file; - } + if ( $user_class eq 'gPXE' ) { + $file = $gpxe_file; + } elsif ( ! $file ) { + $file = 'undionly.kpxe'; } my $packet = new Net::DHCP::Packet( @@ -112,8 +122,8 @@ DHO_SUBNET_MASK() => '255.0.0.0', ); - warn ">> $mac == $ip server $server_ip\n"; - + warn ">> $mac == $ip server: $server_ip file: $file\n"; + warn "## ",$packet->toString(),"\n" if $debug; my $reply = IO::Socket::INET->new(