/[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 201 by dpavlin, Tue Aug 11 23:38:53 2009 UTC revision 423 by dpavlin, Sat Sep 12 22:18:34 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;
# Line 46  sub client_ip { Line 54  sub client_ip {
54    
55          if ( $ip = client::ip_from_mac( $mac ) ) {          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          } else {          } elsif ( ip::in_dhcp_range( $request_ip ) || $request_ip eq '0.0.0.0' ) {
60                  $ip = client::next_ip( $mac );                  $ip = client::next_ip( $mac );
61                  print "NEW $mac $ip\n";                  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    
68          return $ip;          return $ip;
# Line 77  sub process_packet { Line 90  sub process_packet {
90          return unless $buf;          return unless $buf;
91    
92          my $dhcp = Net::DHCP::Packet->new($buf);          my $dhcp = Net::DHCP::Packet->new($buf);
         $dhcp->comment( $transaction++ );  
93    
94          warn "recv: ", $dhcp->toString if $debug;          warn "recv: ", $dhcp->toString if $debug;
95    
96          my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2);          $dhcp->comment( $transaction++ );
97          my $ip = client_ip($mac);  
98            my $mac = format::mac( substr($dhcp->chaddr(),0,$dhcp->hlen()*2) );
99            my $ip = client_mac_ip($mac, $dhcp->ciaddr);
100    
101          my $hostname = $dhcp->getOptionValue(DHO_HOST_NAME);          my $hostname = $dhcp->getOptionValue(DHO_HOST_NAME);
102          print "$ip ", client::conf( $ip => 'hostname', default => $hostname ), " >> /etc/hosts\n";          print "$ip ", client::conf( $ip => 'hostname', default => $hostname ), " >> /etc/hosts\n" if $hostname;
103    
104            my $audit = { mac => $mac, ip => $ip, hostname => $hostname,
105                    options => {
106                            map {
107                                    ( $_ => $dhcp->getOptionValue( $_ ) )
108                            } @{ $dhcp->{options_order} }
109                    },
110            };
111    
112  =for later  =for later
113    
# Line 118  sub process_packet { Line 140  sub process_packet {
140          my $options = {          my $options = {
141                  DHO_SUBNET_MASK()       => $server::netmask,                  DHO_SUBNET_MASK()       => $server::netmask,
142                  DHO_ROUTERS()           => $server::ip,                  DHO_ROUTERS()           => $server::ip,
143                  DHO_DOMAIN_NAME()       => $server::domain_name,                  DHO_DOMAIN_NAME()       => $server::domain,
144                  DHO_NAME_SERVERS()      => $server::ip,                  DHO_NAME_SERVERS()      => $server::ip,
145                  DHO_DOMAIN_NAME_SERVERS() => $server::ip,                  DHO_DOMAIN_NAME_SERVERS() => $server::ip,
146                  DHO_HOST_NAME()         => client::conf( $ip, 'hostname' ),                  DHO_HOST_NAME()         => client::conf( $ip, 'hostname' ),
# Line 139  sub process_packet { Line 161  sub process_packet {
161          }          }
162    
163          warn "W: options requested but missing: ",dump( @missing ),$/;          warn "W: options requested but missing: ",dump( @missing ),$/;
164            $audit->{requested} = [ @requested ];
165            $audit->{missing}   = [ @missing   ];
166    
167          foreach my $opt ( 'magic', 'config_file', 'path_prefix', 'reboot_time' ) {          foreach my $opt ( 'magic', 'config_file', 'path_prefix', 'reboot_time' ) {
168                  my $DH0 = eval 'DHO_PXELINUX_' . uc $opt;                  my $DH0 = eval 'DHO_PXELINUX_' . uc $opt;
# Line 152  sub process_packet { Line 176  sub process_packet {
176    
177          my $messagetype = $dhcp->getOptionValue(DHO_DHCP_MESSAGE_TYPE());          my $messagetype = $dhcp->getOptionValue(DHO_DHCP_MESSAGE_TYPE());
178    
179            my @type;
180    
181          if ($messagetype eq DHCPDISCOVER()) {          if ($messagetype eq DHCPDISCOVER()) {
                 log::mac $mac, "DHCP DISCOVER";  
182                  $packet->{Comment} = $dhcp->comment();                  $packet->{Comment} = $dhcp->comment();
183                  $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPOFFER();                  $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPOFFER();
184                    @type = qw( discover offser );
185          } elsif ($messagetype eq DHCPREQUEST()) {          } elsif ($messagetype eq DHCPREQUEST()) {
186                  my $requested_ip = $dhcp->getOptionValue(DHO_DHCP_REQUESTED_ADDRESS());                  @type = qw( request );
187                  log::mac $mac, "DHCP REQUEST $requested_ip $ip $file";                  my $requested_ip = $dhcp->getOptionValue(DHO_DHCP_REQUESTED_ADDRESS()) || $dhcp->ciaddr();
188                  if ( $ip eq $requested_ip ) {                  if ( $ip eq $requested_ip ) {
189                          $packet->{DHO_DHCP_MESSAGE_TYPE()}      = DHCPACK();                          $packet->{DHO_DHCP_MESSAGE_TYPE()}      = DHCPACK();
190                          $packet->{DHO_DHCP_LEASE_TIME()}        = 5 * 60; # 5 min                          $packet->{DHO_DHCP_LEASE_TIME()}        = 5 * 60; # 5 min
191  #                       $packet->{DHO_ROOT_PATH()}              = '/exports/foobar';  #                       $packet->{DHO_ROOT_PATH()}              = '/exports/foobar';
192                            $type[1] = 'ack';
193                  } else {                  } else {
194                          $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPNAK();                          $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPNAK();
195                          $packet->{DHO_DHCP_MESSAGE()} = "Bad request, expected $ip";                          $packet->{DHO_DHCP_MESSAGE()} = "Bad request, expected $ip got $requested_ip";
196                            $type[1] = 'nak';
197                  }                  }
198          } elsif ($messagetype eq DHCPINFORM()) {          } elsif ($messagetype eq DHCPINFORM()) {
199                  log::mac $mac, "DHCP INFORM ignored";                  @type = qw( inform ignored );
200          } else {          } else {
201                  log::mac $mac, "$messagetype igored (bootp?)";                  @type = ( $messagetype, 'ignored' );
202          }          }
203    
204            warn "# type ",dump @type;
205            $audit->{type} = [ @type ];
206    
207          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;
208            $audit->{response} = $packet;
209    
210          $packet = new Net::DHCP::Packet( %$packet );          $packet = new Net::DHCP::Packet( %$packet );
211          warn "send ",$packet->toString() if $debug;          warn "send ",$packet->toString() if $debug;
212    
213          my $reply = IO::Socket::INET->new(          if ( ip::in_dhcp_range( $ip ) ) {
214                  LocalAddr => $server::ip,                  my $buff = $packet->serialize();
215                  LocalPort => 67,  
216                  Proto => "udp",                  my $reply = IO::Socket::INET->new(
217                  Broadcast => 1,                          LocalAddr => $server::ip,
218                  PeerAddr => '255.255.255.255',                          LocalPort => 67,
219                  PeerPort => 68,                          Proto => "udp",
220                  Reuse => 1,                          Broadcast => 1,
221          ) or die "socket: $@";                          PeerAddr => '255.255.255.255',
222                            PeerPort => 68,
223                            Reuse => 1,
224                    ) or die "socket: $@";
225    
226          my $buff = $packet->serialize();                  $reply->send( $buff, 0 ) or die "Error sending: $!\n";
227          $reply->send( $buff, 0 ) or die "Error sending: $!\n";          } else {
228                    $audit->{error} = "$ip our of our range $server::ip $server::netmask";
229                    warn $audit->{error};
230            }
231    
232            CouchDB::audit( @type, $audit );
233    
234  #       system("arp -s $ip $mac"),  #       system("arp -s $ip $mac"),
235    
# Line 211  sub start { Line 251  sub start {
251    
252          print "DHCP listen on ",$sock->sockhost,":",$sock->sockport,"\n";          print "DHCP listen on ",$sock->sockhost,":",$sock->sockport,"\n";
253    
254            CouchDB::audit( 'start', { addr => $sock->sockhost, port => $sock->sockport } );
255    
256          while (1) {          while (1) {
257                  process_packet $sock;                  process_packet $sock;
258          }          }

Legend:
Removed from v.201  
changed lines
  Added in v.423

  ViewVC Help
Powered by ViewVC 1.1.26