/[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 110 by dpavlin, Sun Aug 2 02:04:00 2009 UTC revision 156 by dpavlin, Thu Aug 6 15:15:53 2009 UTC
# Line 19  use IO::Socket::INET; Line 19  use IO::Socket::INET;
19  use File::Slurp;  use File::Slurp;
20  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
21  use Net::Ping;  use Net::Ping;
 use Module::Refresh;  
22    
23  use lib '..';  use lib '..';
24  use Net::DHCP::Packet;  use Net::DHCP::Packet;
# Line 41  my $addr = $server::ip_from; Line 40  my $addr = $server::ip_from;
40  sub client_ip {  sub client_ip {
41          my ( $mac ) = @_;          my ( $mac ) = @_;
42    
43          my $conf = "$server::base_dir/conf/$server::ip";          my $conf = $server::conf;
44          mkdir $conf unless -e $conf;          mkdir $conf unless -e $conf;
45    
46          if ( -e "$conf/mac/$mac" ) {          if ( -e "$conf/mac/$mac" ) {
# Line 50  sub client_ip { Line 49  sub client_ip {
49                  return $ip;                  return $ip;
50          }          }
51    
         mkdir $_ foreach grep { ! -e $_ } map { "$conf/$_" } ( 'ip', 'mac' );  
   
52          my $p = Net::Ping->new;          my $p = Net::Ping->new;
53    
54          my $prefix = $server::ip;          my $prefix = $server::ip;
55          $prefix =~ s{\.\d+$}{.};          $prefix =~ s{\.\d+$}{.};
56          my $ip = $prefix . $addr;          my $ip = $prefix . $addr;
57          while ( -e "conf/ip/$ip" || $p->ping( $ip ) ) {  
58            while ( -e "$conf/ip/$ip" || $p->ping( $ip ) ) {
59                  $ip = $prefix . $addr++;                  $ip = $prefix . $addr++;
60                  die "all addresses allocated!" if $addr == $server::ip_to;                  die "all addresses allocated!" if $addr == $server::ip_to;
61          }          }
62    
63          write_file "$conf/mac/$mac", $ip;          write_file "$conf/mac/$mac", $ip;
64          if ( -l "$conf/ip/$ip" && readlink "$conf/ip/$ip" ne "$conf/mac/$mac") {  
65                  unlink     "$conf/ip/$ip";          my $ip_path = "$conf/ip/$ip";
66                  symlink    "$conf/mac/$mac", "$conf/ip/$ip";          mkdir $ip_path unless -e $ip_path;
67                  warn "$mac IP changed to $ip";  
68          }          if ( -l "$ip_path/mac" && readlink "$ip_path/mac" ne "$conf/mac/$mac" ) {
69                    warn "$mac IP changed from ", readlink "$ip_path/mac", " to $ip";
70                    unlink "$ip_path/mac";
71            };
72            symlink "$conf/mac/$mac", "$ip_path/mac";
73    
74          print "$mac NEW $ip\n";          print "$mac NEW $ip\n";
75    
# Line 76  sub client_ip { Line 78  sub client_ip {
78    
79  use log;  use log;
80  use config;  use config;
81    use pxelinux;
82    
83  our $file;  our $file;
84  our $transaction = 0; # FIXME predictible transaction numbers  our $transaction = 0; # FIXME predictible transaction numbers
# Line 83  our $transaction = 0; # FIXME predictibl Line 86  our $transaction = 0; # FIXME predictibl
86  sub process_packet {  sub process_packet {
87          my $sock = shift;          my $sock = shift;
88    
89            server->refresh;
90    
91          my $buf;          my $buf;
92          $sock->recv($buf, 1024);          $sock->recv($buf, 1024);
93          my $size = 'empty';          my $size = 'empty';
# Line 126  sub process_packet { Line 131  sub process_packet {
131                  File    => $file,                  File    => $file,
132          };          };
133    
134            foreach my $opt ( 'magic', 'config_file', 'path_prefix', 'reboot_time' ) {
135                    my $DH0 = eval 'DHO_PXELINUX_' . uc $opt;
136                    warn "DH0: $@" if $@;
137                    my $v = eval "\$pxelinux::$opt";
138                    warn "v: $@" if $@;
139                    next unless defined $v;
140                    warn "pxelinux dhcp option $opt = $DH0 = $v";
141                    $packet->{ $DH0 } = $v;
142            }
143    
144          my $messagetype = $dhcp->getOptionValue(DHO_DHCP_MESSAGE_TYPE());          my $messagetype = $dhcp->getOptionValue(DHO_DHCP_MESSAGE_TYPE());
145    
146          if ($messagetype eq DHCPDISCOVER()) {          if ($messagetype eq DHCPDISCOVER()) {
# Line 141  sub process_packet { Line 156  sub process_packet {
156  #                       $packet->{DHO_DHCP_SERVER_IDENTIFIER()} = $server::ip;          # FIXME  #                       $packet->{DHO_DHCP_SERVER_IDENTIFIER()} = $server::ip;          # FIXME
157                          $packet->{DHO_SUBNET_MASK()}            = '255.255.255.0';                          $packet->{DHO_SUBNET_MASK()}            = '255.255.255.0';
158                          $packet->{DHO_ROUTERS()}                = $server::ip;                          $packet->{DHO_ROUTERS()}                = $server::ip;
159  #                       $packet->{DHO_DOMAIN_NAME()}            = 'pxelator.lan';                          $packet->{DHO_DOMAIN_NAME()}            = 'pxelator.lan';
160  #                       $packet->{DHO_NAME_SERVERS()}           = $server::ip;                          $packet->{DHO_NAME_SERVERS()}           = $server::ip;
161  #                       $packet->{DHO_ROOT_PATH()}              = '/exports/foobar';  #                       $packet->{DHO_ROOT_PATH()}              = '/exports/foobar';
162                  } else {                  } else {
163                          $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPNAK();                          $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPNAK();
# Line 156  sub process_packet { Line 171  sub process_packet {
171    
172          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;
173    
174    warn "## created packet ", dump( $packet );
175    
176          $packet = new Net::DHCP::Packet( %$packet );          $packet = new Net::DHCP::Packet( %$packet );
177          warn "send ",$packet->toString() if $debug;          warn "send ",$packet->toString() if $debug;
178    
# Line 193  sub start { Line 210  sub start {
210          print "DHCP listen on ",$sock->sockhost,":",$sock->sockport,"\n";          print "DHCP listen on ",$sock->sockhost,":",$sock->sockport,"\n";
211    
212          while (1) {          while (1) {
                 Module::Refresh->refresh;  
213                  process_packet $sock;                  process_packet $sock;
214          }          }
215  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.26