/[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 177 by dpavlin, Fri Aug 7 14:08:21 2009 UTC revision 208 by dpavlin, Wed Aug 12 23:59:01 2009 UTC
# Line 23  use lib '..'; Line 23  use lib '..';
23  use Net::DHCP::Packet;  use Net::DHCP::Packet;
24  use Net::DHCP::Constants 0.67;  use Net::DHCP::Constants 0.67;
25    
26    use CouchDB;
27    use format;
28    
29  use server;  use server;
30  my $debug = server::debug;  my $debug = server::debug;
31    
# Line 44  sub client_ip { Line 47  sub client_ip {
47    
48          my $ip;          my $ip;
49    
50          if ( -e "$conf/mac/$mac" ) {          if ( $ip = client::ip_from_mac( $mac ) ) {
                 $ip = read_file "$conf/mac/$mac";  
51                  print "RENEW $mac $ip\n";                  print "RENEW $mac $ip\n";
52                  return $ip;                  return $ip;
53          } else {          } else {
54                  $ip = client::next_ip;                  $ip = client::next_ip( $mac );
55                  print "NEW $mac $ip\n";                  print "NEW $mac $ip\n";
                 write_file "$conf/mac/$mac", $ip;  
56          }          }
57    
         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";  
   
58          return $ip;          return $ip;
59  }  }
60    
# Line 96  sub process_packet { Line 87  sub process_packet {
87          my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2);          my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2);
88          my $ip = client_ip($mac);          my $ip = client_ip($mac);
89    
90            my $hostname = $dhcp->getOptionValue(DHO_HOST_NAME);
91            print "$ip ", client::conf( $ip => 'hostname', default => $hostname ), " >> /etc/hosts\n";
92    
93            my $audit = { mac => format::mac($mac), ip => $ip, hostname => $hostname };
94    
95  =for later  =for later
96    
97          my $user_class = $dhcp->getOptionValue(DHO_USER_CLASS());          my $user_class = $dhcp->getOptionValue(DHO_USER_CLASS());
# Line 148  sub process_packet { Line 144  sub process_packet {
144          }          }
145    
146          warn "W: options requested but missing: ",dump( @missing ),$/;          warn "W: options requested but missing: ",dump( @missing ),$/;
147            $audit->{requested} = [ @requested ];
148            $audit->{missing}   = [ @missing   ];
149    
150          foreach my $opt ( 'magic', 'config_file', 'path_prefix', 'reboot_time' ) {          foreach my $opt ( 'magic', 'config_file', 'path_prefix', 'reboot_time' ) {
151                  my $DH0 = eval 'DHO_PXELINUX_' . uc $opt;                  my $DH0 = eval 'DHO_PXELINUX_' . uc $opt;
# Line 162  sub process_packet { Line 160  sub process_packet {
160          my $messagetype = $dhcp->getOptionValue(DHO_DHCP_MESSAGE_TYPE());          my $messagetype = $dhcp->getOptionValue(DHO_DHCP_MESSAGE_TYPE());
161    
162          if ($messagetype eq DHCPDISCOVER()) {          if ($messagetype eq DHCPDISCOVER()) {
163                  log::mac $mac, "DHCP DISCOVER";                  $audit->{type} = 'discover';
164                  $packet->{Comment} = $dhcp->comment();                  $packet->{Comment} = $dhcp->comment();
165                  $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPOFFER();                  $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPOFFER();
166          } elsif ($messagetype eq DHCPREQUEST()) {          } elsif ($messagetype eq DHCPREQUEST()) {
167                  my $requested_ip = $dhcp->getOptionValue(DHO_DHCP_REQUESTED_ADDRESS());                  my $requested_ip = $dhcp->getOptionValue(DHO_DHCP_REQUESTED_ADDRESS());
168                  log::mac $mac, "DHCP REQUEST $requested_ip $ip $file";                  $audit->{type} = 'request';
169                  if ( $ip eq $requested_ip ) {                  if ( $ip eq $requested_ip ) {
170                          $packet->{DHO_DHCP_MESSAGE_TYPE()}      = DHCPACK();                          $packet->{DHO_DHCP_MESSAGE_TYPE()}      = DHCPACK();
171                          $packet->{DHO_DHCP_LEASE_TIME()}        = 5 * 60; # 5 min                          $packet->{DHO_DHCP_LEASE_TIME()}        = 5 * 60; # 5 min
# Line 177  sub process_packet { Line 175  sub process_packet {
175                          $packet->{DHO_DHCP_MESSAGE()} = "Bad request, expected $ip";                          $packet->{DHO_DHCP_MESSAGE()} = "Bad request, expected $ip";
176                  }                  }
177          } elsif ($messagetype eq DHCPINFORM()) {          } elsif ($messagetype eq DHCPINFORM()) {
178                  log::mac $mac, "DHCP INFORM ignored";                  $audit->{type} = 'inform';
179          } else {          } else {
180                  log::mac $mac, "$messagetype igored (bootp?)";                  $audit->{type} = sprintf('ignored %x', $messagetype);
181          }          }
182    
183          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;
184            $audit->{response} = $packet;
185    
186          $packet = new Net::DHCP::Packet( %$packet );          $packet = new Net::DHCP::Packet( %$packet );
187          warn "send ",$packet->toString() if $debug;          warn "send ",$packet->toString() if $debug;
188    
189            CouchDB::audit( $audit->{type}, $audit );
190    
191          my $reply = IO::Socket::INET->new(          my $reply = IO::Socket::INET->new(
192                  LocalAddr => $server::ip,                  LocalAddr => $server::ip,
193                  LocalPort => 67,                  LocalPort => 67,
# Line 220  sub start { Line 221  sub start {
221    
222          print "DHCP listen on ",$sock->sockhost,":",$sock->sockport,"\n";          print "DHCP listen on ",$sock->sockhost,":",$sock->sockport,"\n";
223    
224            CouchDB::audit( 'start', { 'listen' => { addr => $sock->sockhost, port => $sock->sockport } } );
225    
226          while (1) {          while (1) {
227                  process_packet $sock;                  process_packet $sock;
228          }          }

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

  ViewVC Help
Powered by ViewVC 1.1.26