--- lib/PXElator/dhcpd.pm 2009/08/12 22:56:45 207 +++ lib/PXElator/dhcpd.pm 2009/08/28 19:22:24 324 @@ -24,6 +24,7 @@ use Net::DHCP::Constants 0.67; use CouchDB; +use format; use server; my $debug = server::debug; @@ -38,8 +39,13 @@ use client; -sub client_ip { - my ( $mac ) = @_; +sub client_mac_ip { + my ( $mac, $request_ip ) = @_; + + if ( ! $mac ) { + warn "W: no mac in requiest\n"; + return; + } my $conf = $server::conf; mkdir $conf unless -e $conf; @@ -48,10 +54,15 @@ if ( $ip = client::ip_from_mac( $mac ) ) { print "RENEW $mac $ip\n"; + client::save_ip_mac( $ip, $mac ); return $ip; - } else { + } elsif ( in_our_range( $request_ip ) ) { $ip = client::next_ip( $mac ); print "NEW $mac $ip\n"; + } else { + $ip = $request_ip; + client::save_ip_mac( $ip, $mac ); + warn "W: $ip our of server range $server::ip $server::netmask\n"; } return $ip; @@ -65,6 +76,17 @@ our $file; our $transaction = 0; # FIXME predictible transaction numbers +sub ip2bin { pack('C*', split(/\./, $_[0])) }; +sub in_our_range { + my $ip = shift; + return 1 if $ip eq '0.0.0.0'; + return 1 if ( + ( ip2bin($ip) & ip2bin($server::netmask) ) + eq + ( ip2bin($server::ip) & ip2bin($server::netmask) ) + ); +} + sub process_packet { my $sock = shift; @@ -79,18 +101,24 @@ return unless $buf; my $dhcp = Net::DHCP::Packet->new($buf); - $dhcp->comment( $transaction++ ); warn "recv: ", $dhcp->toString if $debug; - my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2); - my $ip = client_ip($mac); + $dhcp->comment( $transaction++ ); + + my $mac = format::mac( substr($dhcp->chaddr(),0,$dhcp->hlen()*2) ); + my $ip = client_mac_ip($mac, $dhcp->ciaddr); my $hostname = $dhcp->getOptionValue(DHO_HOST_NAME); - print "$ip ", client::conf( $ip => 'hostname', default => $hostname ), " >> /etc/hosts\n"; + print "$ip ", client::conf( $ip => 'hostname', default => $hostname ), " >> /etc/hosts\n" if $hostname; - my $audit = { mac => $mac, ip => $ip, hostname => $hostname }; - CouchDB::audit( 'request', $audit ); + my $audit = { mac => $mac, ip => $ip, hostname => $hostname, + options => { + map { + ( $_ => $dhcp->getOptionValue( $_ ) ) + } @{ $dhcp->{options_order} } + }, + }; =for later @@ -186,20 +214,25 @@ $packet = new Net::DHCP::Packet( %$packet ); warn "send ",$packet->toString() if $debug; - CouchDB::audit( 'response', $audit ); + if ( in_our_range( $ip ) ) { + my $buff = $packet->serialize(); - my $reply = IO::Socket::INET->new( - LocalAddr => $server::ip, - LocalPort => 67, - Proto => "udp", - Broadcast => 1, - PeerAddr => '255.255.255.255', - PeerPort => 68, - Reuse => 1, - ) or die "socket: $@"; + my $reply = IO::Socket::INET->new( + LocalAddr => $server::ip, + LocalPort => 67, + Proto => "udp", + Broadcast => 1, + PeerAddr => '255.255.255.255', + PeerPort => 68, + Reuse => 1, + ) or die "socket: $@"; + + $reply->send( $buff, 0 ) or die "Error sending: $!\n"; + } else { + $audit->{error} = "$ip our of our range $server::ip $server::netmask"; + } - my $buff = $packet->serialize(); - $reply->send( $buff, 0 ) or die "Error sending: $!\n"; + CouchDB::audit( $audit->{type}, $audit ); # system("arp -s $ip $mac"), @@ -221,7 +254,7 @@ print "DHCP listen on ",$sock->sockhost,":",$sock->sockport,"\n"; - CouchDB::audit( 'start', { 'listen' => { addr => $sock->sockhost, port => $sock->sockport } } ); + CouchDB::audit( 'start', { addr => $sock->sockhost, port => $sock->sockport } ); while (1) { process_packet $sock;