--- bin/dhcpd.pl 2009/07/26 13:58:39 3 +++ bin/dhcpd.pl 2009/07/28 16:35:49 22 @@ -5,17 +5,30 @@ use strict; use warnings; +use autodie; + use IO::Socket::INET; use Net::DHCP::Packet; use Net::DHCP::Constants; +use File::Slurp; use Data::Dump qw/dump/; die "need to run $0 as root like this\nsudo $0\n" unless $< == 0; my $debug = shift @ARGV; -our $server_ip; -require "config.pl"; +our ( $file, $gpxe_file ); +our ( $ip_from, $ip_to ) = ( 10, 100 ); + +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, @@ -29,35 +42,49 @@ Type => SOCK_DGRAM, ) or die "Failed to bind to socket: $@"; -my $_ip = 10; -my $_mac2ip; -my $_ip_file; + +my $addr = $ip_from; sub client_ip { my ( $mac ) = @_; - my $ip = $_mac2ip->{$mac}; - return $ip if $ip; + my $conf = "conf/$server_ip"; + mkdir $conf unless -e $conf; + + if ( -e "$conf/mac/$mac" ) { + my $ip = read_file "$conf/mac/$mac"; + print "$mac old $ip\n"; + return $ip; + } - $ip = "10.0.0.$_ip"; - $_mac2ip->{$mac} = $ip; + mkdir $_ foreach grep { ! -e $_ } map { "$conf/$_" } ( 'ip', 'mac' ); - $_ip++; - if ( $_ip == 100 ) { - warn "IP roll-over to 10\n"; - $_ip = 10; + my $prefix = $server_ip; + $prefix =~ s{\.\d+$}{.}; + my $ip = $prefix . $addr; + while ( -e "conf/ip/$ip" ) { + $ip = $prefix . $addr++; + die "all addresses allocated!" if $addr == $ip_to; } + write_file "$conf/mac/$mac", $ip; + unlink "$conf/ip/$ip" if -e "$conf/ip/$ip"; + symlink "$conf/mac/$mac", "$conf/ip/$ip"; + + print "$mac NEW $ip\n"; + return $ip; } while (1) { + require "config.pl"; # refresh config + print "waiting for DHCP requests on ",$sock->sockhost,":",$sock->sockport,"\n"; my $buf; $sock->recv($buf, 1024); - print "<< peer:",$sock->peerhost,":",$sock->peerport,"\n"; + print "<< ",$sock->peerhost,":",$sock->peerport,"\n"; if (defined $buf) { @@ -72,6 +99,13 @@ my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2); my $ip = client_ip($mac); + my $user_class = $dhcp->getOptionValue(DHO_USER_CLASS()); + + if ( $user_class eq 'gPXE' ) { + $file = $gpxe_file; + } elsif ( ! $file ) { + $file = 'undionly.kpxe'; + } my $packet = new Net::DHCP::Packet( Op => BOOTREPLY(), @@ -83,13 +117,13 @@ Siaddr => $server_ip, Giaddr => $dhcp->giaddr(), Chaddr => $dhcp->chaddr(), - File => 'undionly.kpxe', + File => $file, # DHO_DHCP_MESSAGE_TYPE() => DHCPACK(), 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(