/[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 160 by dpavlin, Thu Aug 6 18:09:17 2009 UTC revision 201 by dpavlin, Tue Aug 11 23:38:53 2009 UTC
# Line 18  use autodie; Line 18  use autodie;
18  use IO::Socket::INET;  use IO::Socket::INET;
19  use File::Slurp;  use File::Slurp;
20  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
 use Net::Ping;  
21    
22  use lib '..';  use lib '..';
23  use Net::DHCP::Packet;  use Net::DHCP::Packet;
# Line 35  if ( ! $server::ip ) { Line 34  if ( ! $server::ip ) {
34    
35  warn "server ip $server::ip range: $server::ip_from - $server::ip_to\n";  warn "server ip $server::ip range: $server::ip_from - $server::ip_to\n";
36    
37  my $addr = $server::ip_from;  use client;
38    
39  sub client_ip {  sub client_ip {
40          my ( $mac ) = @_;          my ( $mac ) = @_;
# Line 43  sub client_ip { Line 42  sub client_ip {
42          my $conf = $server::conf;          my $conf = $server::conf;
43          mkdir $conf unless -e $conf;          mkdir $conf unless -e $conf;
44    
45          if ( -e "$conf/mac/$mac" ) {          my $ip;
46                  my $ip = read_file "$conf/mac/$mac";  
47            if ( $ip = client::ip_from_mac( $mac ) ) {
48                  print "RENEW $mac $ip\n";                  print "RENEW $mac $ip\n";
49                  return $ip;                  return $ip;
50            } else {
51                    $ip = client::next_ip( $mac );
52                    print "NEW $mac $ip\n";
53          }          }
54    
         my $p = Net::Ping->new;  
   
         my $prefix = $server::ip;  
         $prefix =~ s{\.\d+$}{.};  
         my $ip = $prefix . $addr;  
   
         while ( -e "$conf/ip/$ip" || $p->ping( $ip ) ) {  
                 $ip = $prefix . $addr++;  
                 die "all addresses allocated!" if $addr == $server::ip_to;  
         }  
   
         write_file "$conf/mac/$mac", $ip;  
   
         my $ip_path = "$conf/ip/$ip";  
         mkdir $ip_path unless -e $ip_path;  
   
         if ( -l "$ip_path/mac" && readlink "$ip_path/mac" ne "$conf/mac/$mac" ) {  
                 warn "$mac IP changed from ", readlink "$ip_path/mac", " to $ip";  
                 unlink "$ip_path/mac";  
         };  
         symlink "$conf/mac/$mac", "$ip_path/mac";  
   
         print "$mac NEW $ip\n";  
   
55          return $ip;          return $ip;
56  }  }
57    
# Line 105  sub process_packet { Line 84  sub process_packet {
84          my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2);          my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2);
85          my $ip = client_ip($mac);          my $ip = client_ip($mac);
86    
87            my $hostname = $dhcp->getOptionValue(DHO_HOST_NAME);
88            print "$ip ", client::conf( $ip => 'hostname', default => $hostname ), " >> /etc/hosts\n";
89    
90  =for later  =for later
91    
92          my $user_class = $dhcp->getOptionValue(DHO_USER_CLASS());          my $user_class = $dhcp->getOptionValue(DHO_USER_CLASS());
# Line 145  sub process_packet { Line 127  sub process_packet {
127          };          };
128    
129          my @requested = split(/\s/, $dhcp->getOptionValue(DHO_DHCP_PARAMETER_REQUEST_LIST));          my @requested = split(/\s/, $dhcp->getOptionValue(DHO_DHCP_PARAMETER_REQUEST_LIST));
130          warn "options ",dump( $options ), ' requested: ',dump( @requested );          warn "options ",dump( $options ), ' requested: ',dump( @requested ) if $debug;
131    
132            my @missing;
133          foreach ( @requested ) {          foreach ( @requested ) {
134                  $packet->{$_} = $options->{$_} if defined $options->{$_};                  if ( defined $options->{$_} ) {
135                            $packet->{$_} = $options->{$_};
136                    } else {
137                            push @missing, $_;
138                    }
139          }          }
140    
141            warn "W: options requested but missing: ",dump( @missing ),$/;
142    
143          foreach my $opt ( 'magic', 'config_file', 'path_prefix', 'reboot_time' ) {          foreach my $opt ( 'magic', 'config_file', 'path_prefix', 'reboot_time' ) {
144                  my $DH0 = eval 'DHO_PXELINUX_' . uc $opt;                  my $DH0 = eval 'DHO_PXELINUX_' . uc $opt;
145                  warn "DH0: $@" if $@;                  warn "DH0: $@" if $@;
146                  my $v = eval "\$pxelinux::$opt";                  my $v = eval "\$pxelinux::$opt";
147                  warn "v: $@" if $@;                  warn "v: $@" if $@;
148                  next unless defined $v;                  next unless defined $v;
149                  warn "pxelinux dhcp option $opt = $DH0 = $v";                  warn "pxelinux dhcp option $opt = $DH0 = $v\n" if $debug;
150                  $packet->{ $DH0 } = $v;                  $packet->{ $DH0 } = $v;
151          }          }
152    
# Line 183  sub process_packet { Line 173  sub process_packet {
173                  log::mac $mac, "$messagetype igored (bootp?)";                  log::mac $mac, "$messagetype igored (bootp?)";
174          }          }
175    
         warn "packet ",dump( $packet );  
   
176          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;
177    
178          $packet = new Net::DHCP::Packet( %$packet );          $packet = new Net::DHCP::Packet( %$packet );

Legend:
Removed from v.160  
changed lines
  Added in v.201

  ViewVC Help
Powered by ViewVC 1.1.26