/[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 153 by dpavlin, Wed Aug 5 23:22:17 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 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 ) ) {          while ( -e "$conf/ip/$ip" || $p->ping( $ip ) ) {
58                  $ip = $prefix . $addr++;                  $ip = $prefix . $addr++;
59                  die "all addresses allocated!" if $addr == $server::ip_to;                  die "all addresses allocated!" if $addr == $server::ip_to;
60          }          }
61    
62          write_file "$conf/mac/$mac", $ip;          write_file "$conf/mac/$mac", $ip;
63          if ( -l "$conf/ip/$ip" && readlink "$conf/ip/$ip" ne "$conf/mac/$mac") {  
64            if ( -l "$conf/ip/$ip" && readlink "$conf/ip/$ip" ne "$conf/mac/$mac" ) {
65                  unlink     "$conf/ip/$ip";                  unlink     "$conf/ip/$ip";
66                  symlink    "$conf/mac/$mac", "$conf/ip/$ip";                  warn "$mac IP changed from ", readlink "$conf/ip/$ip", " to $ip";
67                  warn "$mac IP changed to $ip";          };
68          }          symlink    "$conf/mac/$mac", "$conf/ip/$ip";
69    
70          print "$mac NEW $ip\n";          print "$mac NEW $ip\n";
71    
# Line 76  sub client_ip { Line 74  sub client_ip {
74    
75  use log;  use log;
76  use config;  use config;
77    use pxelinux;
78    
79  our $file;  our $file;
80  our $transaction = 0; # FIXME predictible transaction numbers  our $transaction = 0; # FIXME predictible transaction numbers
# Line 83  our $transaction = 0; # FIXME predictibl Line 82  our $transaction = 0; # FIXME predictibl
82  sub process_packet {  sub process_packet {
83          my $sock = shift;          my $sock = shift;
84    
85            server->refresh;
86    
87          my $buf;          my $buf;
88          $sock->recv($buf, 1024);          $sock->recv($buf, 1024);
89          my $size = 'empty';          my $size = 'empty';
# Line 126  sub process_packet { Line 127  sub process_packet {
127                  File    => $file,                  File    => $file,
128          };          };
129    
130            foreach my $opt ( 'magic', 'config_file', 'path_prefix', 'reboot_time' ) {
131                    my $DH0 = eval 'DHO_PXELINUX_' . uc $opt;
132                    warn "DH0: $@" if $@;
133                    my $v = eval "\$pxelinux::$opt";
134                    warn "v: $@" if $@;
135                    next unless defined $v;
136                    warn "pxelinux dhcp option $opt = $DH0 = $v";
137                    $packet->{ $DH0 } = $v;
138            }
139    
140          my $messagetype = $dhcp->getOptionValue(DHO_DHCP_MESSAGE_TYPE());          my $messagetype = $dhcp->getOptionValue(DHO_DHCP_MESSAGE_TYPE());
141    
142          if ($messagetype eq DHCPDISCOVER()) {          if ($messagetype eq DHCPDISCOVER()) {
# Line 141  sub process_packet { Line 152  sub process_packet {
152  #                       $packet->{DHO_DHCP_SERVER_IDENTIFIER()} = $server::ip;          # FIXME  #                       $packet->{DHO_DHCP_SERVER_IDENTIFIER()} = $server::ip;          # FIXME
153                          $packet->{DHO_SUBNET_MASK()}            = '255.255.255.0';                          $packet->{DHO_SUBNET_MASK()}            = '255.255.255.0';
154                          $packet->{DHO_ROUTERS()}                = $server::ip;                          $packet->{DHO_ROUTERS()}                = $server::ip;
155  #                       $packet->{DHO_DOMAIN_NAME()}            = 'pxelator.lan';                          $packet->{DHO_DOMAIN_NAME()}            = 'pxelator.lan';
156  #                       $packet->{DHO_NAME_SERVERS()}           = $server::ip;                          $packet->{DHO_NAME_SERVERS()}           = $server::ip;
157  #                       $packet->{DHO_ROOT_PATH()}              = '/exports/foobar';  #                       $packet->{DHO_ROOT_PATH()}              = '/exports/foobar';
158                  } else {                  } else {
159                          $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPNAK();                          $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPNAK();
# Line 156  sub process_packet { Line 167  sub process_packet {
167    
168          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;
169    
170    warn "## created packet ", dump( $packet );
171    
172          $packet = new Net::DHCP::Packet( %$packet );          $packet = new Net::DHCP::Packet( %$packet );
173          warn "send ",$packet->toString() if $debug;          warn "send ",$packet->toString() if $debug;
174    
# Line 193  sub start { Line 206  sub start {
206          print "DHCP listen on ",$sock->sockhost,":",$sock->sockport,"\n";          print "DHCP listen on ",$sock->sockhost,":",$sock->sockport,"\n";
207    
208          while (1) {          while (1) {
                 Module::Refresh->refresh;  
209                  process_packet $sock;                  process_packet $sock;
210          }          }
211  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.26