/[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 168 by dpavlin, Thu Aug 6 21:31:10 2009 UTC revision 267 by dpavlin, Wed Aug 19 17:01:20 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 36  warn "server ip $server::ip range: $serv Line 39  warn "server ip $server::ip range: $serv
39    
40  use client;  use client;
41    
42  sub client_ip {  sub client_mac_ip {
43          my ( $mac ) = @_;          my ( $mac, $request_ip ) = @_;
44    
45            if ( ! $mac ) {
46                    warn "W: no mac in requiest\n";
47                    return;
48            }
49    
50          my $conf = $server::conf;          my $conf = $server::conf;
51          mkdir $conf unless -e $conf;          mkdir $conf unless -e $conf;
52    
53          if ( -e "$conf/mac/$mac" ) {          my $ip;
54                  my $ip = read_file "$conf/mac/$mac";  
55            if ( $ip = client::ip_from_mac( $mac ) ) {
56                  print "RENEW $mac $ip\n";                  print "RENEW $mac $ip\n";
57                  return $ip;                  return $ip;
58            } elsif ( in_our_range( $request_ip ) ) {
59                    $ip = client::next_ip( $mac );
60                    print "NEW $mac $ip\n";
61            } else {
62                    $ip = $request_ip;
63                    client::save_ip_mac( $ip, $mac );
64                    warn "W: $ip our of server range $server::ip $server::netmask\n";
65          }          }
66    
         my $ip = client::next_ip;  
   
         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";  
   
67          return $ip;          return $ip;
68  }  }
69    
# Line 74  use client; Line 75  use client;
75  our $file;  our $file;
76  our $transaction = 0; # FIXME predictible transaction numbers  our $transaction = 0; # FIXME predictible transaction numbers
77    
78    sub ip2bin { pack('C*', split(/\./, $_[0])) };
79    sub in_our_range {
80            my $ip = shift;
81            return 1 if $ip eq '0.0.0.0';
82            return 1 if (
83                    ( ip2bin($ip)         & ip2bin($server::netmask) )
84                    eq
85                    ( ip2bin($server::ip) & ip2bin($server::netmask) )
86            );
87    }
88    
89  sub process_packet {  sub process_packet {
90          my $sock = shift;          my $sock = shift;
91    
# Line 88  sub process_packet { Line 100  sub process_packet {
100          return unless $buf;          return unless $buf;
101    
102          my $dhcp = Net::DHCP::Packet->new($buf);          my $dhcp = Net::DHCP::Packet->new($buf);
         $dhcp->comment( $transaction++ );  
103    
104          warn "recv: ", $dhcp->toString if $debug;          warn "recv: ", $dhcp->toString if $debug;
105    
106            $dhcp->comment( $transaction++ );
107    
108          my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2);          my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2);
109          my $ip = client_ip($mac);          my $ip = client_mac_ip($mac, $dhcp->ciaddr);
110    
111            my $hostname = $dhcp->getOptionValue(DHO_HOST_NAME);
112            print "$ip ", client::conf( $ip => 'hostname', default => $hostname ), " >> /etc/hosts\n";
113    
114            my $audit = { mac => format::mac($mac), ip => $ip, hostname => $hostname,
115                    options => {
116                            map {
117                                    ( $_ => $dhcp->getOptionValue( $_ ) )
118                            } @{ $dhcp->{options_order} }
119                    },
120            };
121    
122  =for later  =for later
123    
# Line 147  sub process_packet { Line 171  sub process_packet {
171          }          }
172    
173          warn "W: options requested but missing: ",dump( @missing ),$/;          warn "W: options requested but missing: ",dump( @missing ),$/;
174            $audit->{requested} = [ @requested ];
175            $audit->{missing}   = [ @missing   ];
176    
177          foreach my $opt ( 'magic', 'config_file', 'path_prefix', 'reboot_time' ) {          foreach my $opt ( 'magic', 'config_file', 'path_prefix', 'reboot_time' ) {
178                  my $DH0 = eval 'DHO_PXELINUX_' . uc $opt;                  my $DH0 = eval 'DHO_PXELINUX_' . uc $opt;
# Line 161  sub process_packet { Line 187  sub process_packet {
187          my $messagetype = $dhcp->getOptionValue(DHO_DHCP_MESSAGE_TYPE());          my $messagetype = $dhcp->getOptionValue(DHO_DHCP_MESSAGE_TYPE());
188    
189          if ($messagetype eq DHCPDISCOVER()) {          if ($messagetype eq DHCPDISCOVER()) {
190                  log::mac $mac, "DHCP DISCOVER";                  $audit->{type} = 'discover';
191                  $packet->{Comment} = $dhcp->comment();                  $packet->{Comment} = $dhcp->comment();
192                  $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPOFFER();                  $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPOFFER();
193          } elsif ($messagetype eq DHCPREQUEST()) {          } elsif ($messagetype eq DHCPREQUEST()) {
194                  my $requested_ip = $dhcp->getOptionValue(DHO_DHCP_REQUESTED_ADDRESS());                  my $requested_ip = $dhcp->getOptionValue(DHO_DHCP_REQUESTED_ADDRESS());
195                  log::mac $mac, "DHCP REQUEST $requested_ip $ip $file";                  $audit->{type} = 'request';
196                  if ( $ip eq $requested_ip ) {                  if ( $ip eq $requested_ip ) {
197                          $packet->{DHO_DHCP_MESSAGE_TYPE()}      = DHCPACK();                          $packet->{DHO_DHCP_MESSAGE_TYPE()}      = DHCPACK();
198                          $packet->{DHO_DHCP_LEASE_TIME()}        = 5 * 60; # 5 min                          $packet->{DHO_DHCP_LEASE_TIME()}        = 5 * 60; # 5 min
# Line 176  sub process_packet { Line 202  sub process_packet {
202                          $packet->{DHO_DHCP_MESSAGE()} = "Bad request, expected $ip";                          $packet->{DHO_DHCP_MESSAGE()} = "Bad request, expected $ip";
203                  }                  }
204          } elsif ($messagetype eq DHCPINFORM()) {          } elsif ($messagetype eq DHCPINFORM()) {
205                  log::mac $mac, "DHCP INFORM ignored";                  $audit->{type} = 'inform';
206          } else {          } else {
207                  log::mac $mac, "$messagetype igored (bootp?)";                  $audit->{type} = sprintf('ignored %x', $messagetype);
208          }          }
209    
210          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;
211            $audit->{response} = $packet;
212    
213          $packet = new Net::DHCP::Packet( %$packet );          $packet = new Net::DHCP::Packet( %$packet );
214          warn "send ",$packet->toString() if $debug;          warn "send ",$packet->toString() if $debug;
215    
216          my $reply = IO::Socket::INET->new(          if ( in_our_range( $ip ) ) {
217                  LocalAddr => $server::ip,                  my $buff = $packet->serialize();
218                  LocalPort => 67,  
219                  Proto => "udp",                  my $reply = IO::Socket::INET->new(
220                  Broadcast => 1,                          LocalAddr => $server::ip,
221                  PeerAddr => '255.255.255.255',                          LocalPort => 67,
222                  PeerPort => 68,                          Proto => "udp",
223                  Reuse => 1,                          Broadcast => 1,
224          ) or die "socket: $@";                          PeerAddr => '255.255.255.255',
225                            PeerPort => 68,
226                            Reuse => 1,
227                    ) or die "socket: $@";
228    
229                    $reply->send( $buff, 0 ) or die "Error sending: $!\n";
230            } else {
231                    $audit->{error} = "$ip our of our range $server::ip $server::netmask";
232            }
233    
234          my $buff = $packet->serialize();          CouchDB::audit( $audit->{type}, $audit );
         $reply->send( $buff, 0 ) or die "Error sending: $!\n";  
235    
236  #       system("arp -s $ip $mac"),  #       system("arp -s $ip $mac"),
237    
# Line 219  sub start { Line 253  sub start {
253    
254          print "DHCP listen on ",$sock->sockhost,":",$sock->sockport,"\n";          print "DHCP listen on ",$sock->sockhost,":",$sock->sockport,"\n";
255    
256            CouchDB::audit( 'start', { addr => $sock->sockhost, port => $sock->sockport } );
257    
258          while (1) {          while (1) {
259                  process_packet $sock;                  process_packet $sock;
260          }          }

Legend:
Removed from v.168  
changed lines
  Added in v.267

  ViewVC Help
Powered by ViewVC 1.1.26