/[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 156 by dpavlin, Thu Aug 6 15:15:53 2009 UTC revision 177 by dpavlin, Fri Aug 7 14:08:21 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            my $ip;
46    
47          if ( -e "$conf/mac/$mac" ) {          if ( -e "$conf/mac/$mac" ) {
48                  my $ip = read_file "$conf/mac/$mac";                  $ip = read_file "$conf/mac/$mac";
49                  print "RENEW $mac $ip\n";                  print "RENEW $mac $ip\n";
50                  return $ip;                  return $ip;
51            } else {
52                    $ip = client::next_ip;
53                    print "NEW $mac $ip\n";
54                    write_file "$conf/mac/$mac", $ip;
55          }          }
56    
         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;  
   
57          my $ip_path = "$conf/ip/$ip";          my $ip_path = "$conf/ip/$ip";
58          mkdir $ip_path unless -e $ip_path;          mkdir $ip_path unless -e $ip_path;
59    
# Line 69  sub client_ip { Line 61  sub client_ip {
61                  warn "$mac IP changed from ", readlink "$ip_path/mac", " to $ip";                  warn "$mac IP changed from ", readlink "$ip_path/mac", " to $ip";
62                  unlink "$ip_path/mac";                  unlink "$ip_path/mac";
63          };          };
         symlink "$conf/mac/$mac", "$ip_path/mac";  
64    
65          print "$mac NEW $ip\n";          symlink "$conf/mac/$mac", "$ip_path/mac";
66    
67          return $ip;          return $ip;
68  }  }
# Line 79  sub client_ip { Line 70  sub client_ip {
70  use log;  use log;
71  use config;  use config;
72  use pxelinux;  use pxelinux;
73    use client;
74    
75  our $file;  our $file;
76  our $transaction = 0; # FIXME predictible transaction numbers  our $transaction = 0; # FIXME predictible transaction numbers
# Line 129  sub process_packet { Line 121  sub process_packet {
121                  Giaddr  => $dhcp->giaddr(),                  Giaddr  => $dhcp->giaddr(),
122                  Chaddr  => $dhcp->chaddr(),                  Chaddr  => $dhcp->chaddr(),
123                  File    => $file,                  File    => $file,
124                    DHO_DHCP_SERVER_IDENTIFIER()    => $server::ip, # busybox/udhcpc needs it but doesn't request
125          };          };
126    
127            my $options = {
128                    DHO_SUBNET_MASK()       => $server::netmask,
129                    DHO_ROUTERS()           => $server::ip,
130                    DHO_DOMAIN_NAME()       => $server::domain_name,
131                    DHO_NAME_SERVERS()      => $server::ip,
132                    DHO_DOMAIN_NAME_SERVERS() => $server::ip,
133                    DHO_HOST_NAME()         => client::conf( $ip, 'hostname' ),
134                    DHO_BROADCAST_ADDRESS() => $server::bcast,
135    #               DHO_NTP_SERVERS() => '',
136            };
137    
138            my @requested = split(/\s/, $dhcp->getOptionValue(DHO_DHCP_PARAMETER_REQUEST_LIST));
139            warn "options ",dump( $options ), ' requested: ',dump( @requested ) if $debug;
140    
141            my @missing;
142            foreach ( @requested ) {
143                    if ( defined $options->{$_} ) {
144                            $packet->{$_} = $options->{$_};
145                    } else {
146                            push @missing, $_;
147                    }
148            }
149    
150            warn "W: options requested but missing: ",dump( @missing ),$/;
151    
152          foreach my $opt ( 'magic', 'config_file', 'path_prefix', 'reboot_time' ) {          foreach my $opt ( 'magic', 'config_file', 'path_prefix', 'reboot_time' ) {
153                  my $DH0 = eval 'DHO_PXELINUX_' . uc $opt;                  my $DH0 = eval 'DHO_PXELINUX_' . uc $opt;
154                  warn "DH0: $@" if $@;                  warn "DH0: $@" if $@;
155                  my $v = eval "\$pxelinux::$opt";                  my $v = eval "\$pxelinux::$opt";
156                  warn "v: $@" if $@;                  warn "v: $@" if $@;
157                  next unless defined $v;                  next unless defined $v;
158                  warn "pxelinux dhcp option $opt = $DH0 = $v";                  warn "pxelinux dhcp option $opt = $DH0 = $v\n" if $debug;
159                  $packet->{ $DH0 } = $v;                  $packet->{ $DH0 } = $v;
160          }          }
161    
# Line 153  sub process_packet { Line 171  sub process_packet {
171                  if ( $ip eq $requested_ip ) {                  if ( $ip eq $requested_ip ) {
172                          $packet->{DHO_DHCP_MESSAGE_TYPE()}      = DHCPACK();                          $packet->{DHO_DHCP_MESSAGE_TYPE()}      = DHCPACK();
173                          $packet->{DHO_DHCP_LEASE_TIME()}        = 5 * 60; # 5 min                          $packet->{DHO_DHCP_LEASE_TIME()}        = 5 * 60; # 5 min
 #                       $packet->{DHO_DHCP_SERVER_IDENTIFIER()} = $server::ip;          # FIXME  
                         $packet->{DHO_SUBNET_MASK()}            = '255.255.255.0';  
                         $packet->{DHO_ROUTERS()}                = $server::ip;  
                         $packet->{DHO_DOMAIN_NAME()}            = 'pxelator.lan';  
                         $packet->{DHO_NAME_SERVERS()}           = $server::ip;  
174  #                       $packet->{DHO_ROOT_PATH()}              = '/exports/foobar';  #                       $packet->{DHO_ROOT_PATH()}              = '/exports/foobar';
175                  } else {                  } else {
176                          $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPNAK();                          $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPNAK();
# Line 171  sub process_packet { Line 184  sub process_packet {
184    
185          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;
186    
 warn "## created packet ", dump( $packet );  
   
187          $packet = new Net::DHCP::Packet( %$packet );          $packet = new Net::DHCP::Packet( %$packet );
188          warn "send ",$packet->toString() if $debug;          warn "send ",$packet->toString() if $debug;
189    

Legend:
Removed from v.156  
changed lines
  Added in v.177

  ViewVC Help
Powered by ViewVC 1.1.26