/[pxelator]/lib/PXElator/dhcpd.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /lib/PXElator/dhcpd.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 324 by dpavlin, Fri Aug 28 19:22:24 2009 UTC revision 424 by dpavlin, Sun Sep 13 09:10:46 2009 UTC
# Line 56  sub client_mac_ip { Line 56  sub client_mac_ip {
56                  print "RENEW $mac $ip\n";                  print "RENEW $mac $ip\n";
57                  client::save_ip_mac( $ip, $mac );                  client::save_ip_mac( $ip, $mac );
58                  return $ip;                  return $ip;
59          } elsif ( in_our_range( $request_ip ) ) {          } elsif ( ip::in_dhcp_range( $request_ip ) || $request_ip eq '0.0.0.0' ) {
60                  $ip = client::next_ip( $mac );                  $ip = client::next_ip( $mac );
61                  print "NEW $mac $ip\n";                  print "NEW $mac $ip\n";
62          } else {          } else {
# Line 76  use client; Line 76  use client;
76  our $file;  our $file;
77  our $transaction = 0; # FIXME predictible transaction numbers  our $transaction = 0; # FIXME predictible transaction numbers
78    
 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) )  
         );  
 }  
   
79  sub process_packet {  sub process_packet {
80          my $sock = shift;          my $sock = shift;
81    
# Line 151  sub process_packet { Line 140  sub process_packet {
140          my $options = {          my $options = {
141                  DHO_SUBNET_MASK()       => $server::netmask,                  DHO_SUBNET_MASK()       => $server::netmask,
142                  DHO_ROUTERS()           => $server::ip,                  DHO_ROUTERS()           => $server::ip,
143                  DHO_DOMAIN_NAME()       => $server::domain_name,                  DHO_DOMAIN_NAME()       => $server::domain,
144                  DHO_NAME_SERVERS()      => $server::ip,                  DHO_NAME_SERVERS()      => $server::ip,
145                  DHO_DOMAIN_NAME_SERVERS() => $server::ip,                  DHO_DOMAIN_NAME_SERVERS() => $server::ip,
146                  DHO_HOST_NAME()         => client::conf( $ip, 'hostname' ),                  DHO_HOST_NAME()         => client::conf( $ip, 'hostname' ),
# Line 187  sub process_packet { Line 176  sub process_packet {
176    
177          my $messagetype = $dhcp->getOptionValue(DHO_DHCP_MESSAGE_TYPE());          my $messagetype = $dhcp->getOptionValue(DHO_DHCP_MESSAGE_TYPE());
178    
179            my @type;
180    
181          if ($messagetype eq DHCPDISCOVER()) {          if ($messagetype eq DHCPDISCOVER()) {
                 $audit->{type} = 'discover';  
182                  $packet->{Comment} = $dhcp->comment();                  $packet->{Comment} = $dhcp->comment();
183                  $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPOFFER();                  $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPOFFER();
184                    @type = qw( discover offer );
185          } elsif ($messagetype eq DHCPREQUEST()) {          } elsif ($messagetype eq DHCPREQUEST()) {
186                  my $requested_ip = $dhcp->getOptionValue(DHO_DHCP_REQUESTED_ADDRESS());                  @type = qw( request );
187                  $audit->{type} = 'request';                  my $requested_ip = $dhcp->getOptionValue(DHO_DHCP_REQUESTED_ADDRESS()) || $dhcp->ciaddr();
188                  if ( $ip eq $requested_ip ) {                  if ( $ip eq $requested_ip ) {
189                          $packet->{DHO_DHCP_MESSAGE_TYPE()}      = DHCPACK();                          $packet->{DHO_DHCP_MESSAGE_TYPE()}      = DHCPACK();
190                          $packet->{DHO_DHCP_LEASE_TIME()}        = 5 * 60; # 5 min                          $packet->{DHO_DHCP_LEASE_TIME()}        = 5 * 60; # 5 min
191  #                       $packet->{DHO_ROOT_PATH()}              = '/exports/foobar';  #                       $packet->{DHO_ROOT_PATH()}              = '/exports/foobar';
192                            $type[1] = 'ack';
193                  } else {                  } else {
194                          $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPNAK();                          $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPNAK();
195                          $packet->{DHO_DHCP_MESSAGE()} = "Bad request, expected $ip";                          $packet->{DHO_DHCP_MESSAGE()} = "Bad request, expected $ip got $requested_ip";
196                            $type[1] = 'nak';
197                  }                  }
198          } elsif ($messagetype eq DHCPINFORM()) {          } elsif ($messagetype eq DHCPINFORM()) {
199                  $audit->{type} = 'inform';                  @type = qw( inform ignored );
200          } else {          } else {
201                  $audit->{type} = sprintf('ignored %x', $messagetype);                  @type = ( $messagetype, 'ignored' );
202          }          }
203    
204            warn "# type ",dump @type;
205            $audit->{type} = [ @type ];
206    
207          warn ">> $mac == $ip server: $server::ip", $file ? " file: $file\n" : "\n" if $debug;          warn ">> $mac == $ip server: $server::ip", $file ? " file: $file\n" : "\n" if $debug;
208          $audit->{response} = $packet;          $audit->{response} = $packet;
209    
210          $packet = new Net::DHCP::Packet( %$packet );          $packet = new Net::DHCP::Packet( %$packet );
211          warn "send ",$packet->toString() if $debug;          warn "send ",$packet->toString() if $debug;
212    
213          if ( in_our_range( $ip ) ) {          if ( ip::in_dhcp_range( $ip ) ) {
214                  my $buff = $packet->serialize();                  my $buff = $packet->serialize();
215    
216                  my $reply = IO::Socket::INET->new(                  my $reply = IO::Socket::INET->new(
# Line 230  sub process_packet { Line 226  sub process_packet {
226                  $reply->send( $buff, 0 ) or die "Error sending: $!\n";                  $reply->send( $buff, 0 ) or die "Error sending: $!\n";
227          } else {          } else {
228                  $audit->{error} = "$ip our of our range $server::ip $server::netmask";                  $audit->{error} = "$ip our of our range $server::ip $server::netmask";
229                    warn $audit->{error};
230          }          }
231    
232          CouchDB::audit( $audit->{type}, $audit );          CouchDB::audit( @type, $audit );
233    
234  #       system("arp -s $ip $mac"),  #       system("arp -s $ip $mac"),
235    

Legend:
Removed from v.324  
changed lines
  Added in v.424

  ViewVC Help
Powered by ViewVC 1.1.26