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

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

  ViewVC Help
Powered by ViewVC 1.1.26