/[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 200 by dpavlin, Tue Aug 11 15:55:26 2009 UTC revision 244 by dpavlin, Mon Aug 17 13:27:18 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          my $conf = $server::conf;          my $conf = $server::conf;
46          mkdir $conf unless -e $conf;          mkdir $conf unless -e $conf;
# Line 47  sub client_ip { Line 50  sub client_ip {
50          if ( $ip = client::ip_from_mac( $mac ) ) {          if ( $ip = client::ip_from_mac( $mac ) ) {
51                  print "RENEW $mac $ip\n";                  print "RENEW $mac $ip\n";
52                  return $ip;                  return $ip;
53          } else {          } elsif ( in_our_range( $request_ip ) ) {
54                  $ip = client::next_ip( $mac );                  $ip = client::next_ip( $mac );
55                  print "NEW $mac $ip\n";                  print "NEW $mac $ip\n";
56            } else {
57                    $ip = $request_ip;
58                    client::save_ip_mac( $ip, $mac );
59                    warn "W: $ip our of server range $server::ip $server::netmask\n";
60          }          }
61    
62          return $ip;          return $ip;
# Line 63  use client; Line 70  use client;
70  our $file;  our $file;
71  our $transaction = 0; # FIXME predictible transaction numbers  our $transaction = 0; # FIXME predictible transaction numbers
72    
73    sub ip2bin { pack('C*', split(/\./, $_[0])) };
74    sub in_our_range {
75            my $ip = shift;
76            return 1 if $ip eq '0.0.0.0';
77            return 1 if (
78                    ( ip2bin($ip)         & ip2bin($server::netmask) )
79                    eq
80                    ( ip2bin($server::ip) & ip2bin($server::netmask) )
81            );
82    }
83    
84  sub process_packet {  sub process_packet {
85          my $sock = shift;          my $sock = shift;
86    
# Line 82  sub process_packet { Line 100  sub process_packet {
100          warn "recv: ", $dhcp->toString if $debug;          warn "recv: ", $dhcp->toString if $debug;
101    
102          my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2);          my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2);
103          my $ip = client_ip($mac);          my $ip = client_mac_ip($mac, $dhcp->ciaddr);
104    
105            my $hostname = $dhcp->getOptionValue(DHO_HOST_NAME);
106            print "$ip ", client::conf( $ip => 'hostname', default => $hostname ), " >> /etc/hosts\n";
107    
108            my $audit = { mac => format::mac($mac), ip => $ip, hostname => $hostname };
109    
110  =for later  =for later
111    
# Line 136  sub process_packet { Line 159  sub process_packet {
159          }          }
160    
161          warn "W: options requested but missing: ",dump( @missing ),$/;          warn "W: options requested but missing: ",dump( @missing ),$/;
162            $audit->{requested} = [ @requested ];
163            $audit->{missing}   = [ @missing   ];
164    
165          foreach my $opt ( 'magic', 'config_file', 'path_prefix', 'reboot_time' ) {          foreach my $opt ( 'magic', 'config_file', 'path_prefix', 'reboot_time' ) {
166                  my $DH0 = eval 'DHO_PXELINUX_' . uc $opt;                  my $DH0 = eval 'DHO_PXELINUX_' . uc $opt;
# Line 150  sub process_packet { Line 175  sub process_packet {
175          my $messagetype = $dhcp->getOptionValue(DHO_DHCP_MESSAGE_TYPE());          my $messagetype = $dhcp->getOptionValue(DHO_DHCP_MESSAGE_TYPE());
176    
177          if ($messagetype eq DHCPDISCOVER()) {          if ($messagetype eq DHCPDISCOVER()) {
178                  log::mac $mac, "DHCP DISCOVER";                  $audit->{type} = 'discover';
179                  $packet->{Comment} = $dhcp->comment();                  $packet->{Comment} = $dhcp->comment();
180                  $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPOFFER();                  $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPOFFER();
181          } elsif ($messagetype eq DHCPREQUEST()) {          } elsif ($messagetype eq DHCPREQUEST()) {
182                  my $requested_ip = $dhcp->getOptionValue(DHO_DHCP_REQUESTED_ADDRESS());                  my $requested_ip = $dhcp->getOptionValue(DHO_DHCP_REQUESTED_ADDRESS());
183                  log::mac $mac, "DHCP REQUEST $requested_ip $ip $file";                  $audit->{type} = 'request';
184                  if ( $ip eq $requested_ip ) {                  if ( $ip eq $requested_ip ) {
185                          $packet->{DHO_DHCP_MESSAGE_TYPE()}      = DHCPACK();                          $packet->{DHO_DHCP_MESSAGE_TYPE()}      = DHCPACK();
186                          $packet->{DHO_DHCP_LEASE_TIME()}        = 5 * 60; # 5 min                          $packet->{DHO_DHCP_LEASE_TIME()}        = 5 * 60; # 5 min
# Line 165  sub process_packet { Line 190  sub process_packet {
190                          $packet->{DHO_DHCP_MESSAGE()} = "Bad request, expected $ip";                          $packet->{DHO_DHCP_MESSAGE()} = "Bad request, expected $ip";
191                  }                  }
192          } elsif ($messagetype eq DHCPINFORM()) {          } elsif ($messagetype eq DHCPINFORM()) {
193                  log::mac $mac, "DHCP INFORM ignored";                  $audit->{type} = 'inform';
194          } else {          } else {
195                  log::mac $mac, "$messagetype igored (bootp?)";                  $audit->{type} = sprintf('ignored %x', $messagetype);
196          }          }
197    
198          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;
199            $audit->{response} = $packet;
200    
201          $packet = new Net::DHCP::Packet( %$packet );          $packet = new Net::DHCP::Packet( %$packet );
202          warn "send ",$packet->toString() if $debug;          warn "send ",$packet->toString() if $debug;
203    
204          my $reply = IO::Socket::INET->new(          if ( in_our_range( $ip ) ) {
205                  LocalAddr => $server::ip,                  my $buff = $packet->serialize();
206                  LocalPort => 67,  
207                  Proto => "udp",                  my $reply = IO::Socket::INET->new(
208                  Broadcast => 1,                          LocalAddr => $server::ip,
209                  PeerAddr => '255.255.255.255',                          LocalPort => 67,
210                  PeerPort => 68,                          Proto => "udp",
211                  Reuse => 1,                          Broadcast => 1,
212          ) or die "socket: $@";                          PeerAddr => '255.255.255.255',
213                            PeerPort => 68,
214                            Reuse => 1,
215                    ) or die "socket: $@";
216    
217                    $reply->send( $buff, 0 ) or die "Error sending: $!\n";
218            } else {
219                    $audit->{error} = "$ip our of our range $server::ip $server::netmask";
220            }
221    
222          my $buff = $packet->serialize();          CouchDB::audit( $audit->{type}, $audit );
         $reply->send( $buff, 0 ) or die "Error sending: $!\n";  
223    
224  #       system("arp -s $ip $mac"),  #       system("arp -s $ip $mac"),
225    
# Line 208  sub start { Line 241  sub start {
241    
242          print "DHCP listen on ",$sock->sockhost,":",$sock->sockport,"\n";          print "DHCP listen on ",$sock->sockhost,":",$sock->sockport,"\n";
243    
244            CouchDB::audit( 'start', { addr => $sock->sockhost, port => $sock->sockport } );
245    
246          while (1) {          while (1) {
247                  process_packet $sock;                  process_packet $sock;
248          }          }

Legend:
Removed from v.200  
changed lines
  Added in v.244

  ViewVC Help
Powered by ViewVC 1.1.26