--- lib/PXElator/dhcpd.pm 2009/07/29 22:04:58 45 +++ lib/PXElator/dhcpd.pm 2009/07/31 20:07:53 94 @@ -1,4 +1,4 @@ -#!/usr/bin/perl +package dhcpd; =head1 dhcpd @@ -26,10 +26,9 @@ use Net::DHCP::Constants 0.67; use server; +my $debug = server::debug; use pxe; -my $debug = 1; - if ( ! $server::ip ) { my $server_ip = `/sbin/ifconfig`; $server_ip =~ s/^.+?addr:([\d\.]+).*$/$1/gs; @@ -43,12 +42,12 @@ sub client_ip { my ( $mac ) = @_; - my $conf = "conf/$server::ip"; + my $conf = "$server::base_dir/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"; + print "RENEW $mac $ip\n"; return $ip; } @@ -86,13 +85,13 @@ my $size = 'empty'; $size = length($buf) . ' bytes' if defined $buf; - print "packet from ",$sock->peerhost,":",$sock->peerport," $size\n"; + print "packet from ",$sock->peerhost,":",$sock->peerport," $size\n" if $debug; return unless $buf; my $dhcp = Net::DHCP::Packet->new($buf); $dhcp->comment( $transaction++ ); - warn "recv: ", $dhcp->toString; + warn "recv: ", $dhcp->toString if $debug; my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2); my $ip = client_ip($mac); @@ -122,15 +121,17 @@ File => $pxe::file, }; + pxe::config_for_ip( $ip ); + my $messagetype = $dhcp->getOptionValue(DHO_DHCP_MESSAGE_TYPE()); if ($messagetype eq DHCPDISCOVER()) { - warn "DHCP DISCOVER"; + log::mac $mac, "DHCP DISCOVER"; $packet->{Comment} = $dhcp->comment(); $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPOFFER(); } elsif ($messagetype eq DHCPREQUEST()) { my $requested_ip = $dhcp->getOptionValue(DHO_DHCP_REQUESTED_ADDRESS()); - warn "DHCP REQUEST $requested_ip"; + log::mac $mac, "DHCP REQUEST $requested_ip $ip $pxe::file"; if ( $ip eq $requested_ip ) { $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPACK(); $packet->{DHO_DHCP_LEASE_TIME()} = 5 * 60; # 5 min @@ -145,15 +146,15 @@ $packet->{DHO_DHCP_MESSAGE()} = "Bad request, expected $ip"; } } elsif ($messagetype eq DHCPINFORM()) { - warn "DHCP INFORM ignored"; + log::mac $mac, "DHCP INFORM ignored"; } else { - warn "$messagetype igored (bootp?)"; + log::mac $mac, "$messagetype igored (bootp?)"; } - warn ">> $mac == $ip server: $server::ip", $pxe::file ? " pxe file: $pxe::file\n" : "\n"; + warn ">> $mac == $ip server: $server::ip", $pxe::file ? " pxe file: $pxe::file\n" : "\n" if $debug; $packet = new Net::DHCP::Packet( %$packet ); - warn "## ",$packet->toString(),"\n" if $debug; + warn "send ",$packet->toString() if $debug; my $reply = IO::Socket::INET->new( LocalAddr => $server::ip,