/[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

bin/dhcpd.pl revision 3 by dpavlin, Sun Jul 26 13:58:39 2009 UTC lib/PXElator/dhcpd.pm revision 207 by dpavlin, Wed Aug 12 22:56:45 2009 UTC
# Line 1  Line 1 
1  #!/usr/bin/perl  package dhcpd;
2    
3  # based on http://www.perlmonks.org/index.pl?node_id=325248  =head1 dhcpd
4    
5    start with:
6    
7     perl -Ilib/PXElator -Ilib -Mdhcpd -e start
8    
9    based on L<http://www.perlmonks.org/index.pl?node_id=325248>
10    
11    =cut
12    
13  use strict;  use strict;
14  use warnings;  use warnings;
15    
16    use autodie;
17    
18  use IO::Socket::INET;  use IO::Socket::INET;
19  use Net::DHCP::Packet;  use File::Slurp;
 use Net::DHCP::Constants;  
20  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
21    
22  die "need to run $0 as root like this\nsudo $0\n" unless $< == 0;  use lib '..';
23    use Net::DHCP::Packet;
24    use Net::DHCP::Constants 0.67;
25    
26    use CouchDB;
27    
28  my $debug = shift @ARGV;  use server;
29    my $debug = server::debug;
30    
31    if ( ! $server::ip ) {
32            my $server_ip = `/sbin/ifconfig`;
33            $server_ip =~ s/^.+?addr:([\d\.]+).*$/$1/gs;
34            $server::ip = $server_ip;
35    }
36    
37  our $server_ip;  warn "server ip $server::ip range: $server::ip_from - $server::ip_to\n";
 require "config.pl";  
38    
39  my $sock = IO::Socket::INET->new(  use client;
         LocalPort       => 67,  
 #       LocalAddr       => 'localhost',  
 #       LocalAddr       => '10.0.0.100',  
         LocalAddr       => '0.0.0.0',  
         Proto           => 'udp',  
         ReuseAddr       => 1,  
 #       PeerPort        => getservbyname('bootpc', 'udp'),  
         Broadcast       => 1,  
         Type            => SOCK_DGRAM,  
 ) or die "Failed to bind to socket: $@";  
   
 my $_ip = 10;  
 my $_mac2ip;  
 my $_ip_file;  
40    
41  sub client_ip {  sub client_ip {
42          my ( $mac ) = @_;          my ( $mac ) = @_;
43    
44          my $ip = $_mac2ip->{$mac};          my $conf = $server::conf;
45          return $ip if $ip;          mkdir $conf unless -e $conf;
46    
47          $ip = "10.0.0.$_ip";          my $ip;
         $_mac2ip->{$mac} = $ip;  
48    
49          $_ip++;          if ( $ip = client::ip_from_mac( $mac ) ) {
50          if ( $_ip == 100 ) {                  print "RENEW $mac $ip\n";
51                  warn "IP roll-over to 10\n";                  return $ip;
52                  $_ip = 10;          } else {
53                    $ip = client::next_ip( $mac );
54                    print "NEW $mac $ip\n";
55          }          }
56    
57          return $ip;          return $ip;
58  }  }
59    
60  while (1) {  use log;
61    use config;
62    use pxelinux;
63    use client;
64    
65    our $file;
66    our $transaction = 0; # FIXME predictible transaction numbers
67    
68    sub process_packet {
69            my $sock = shift;
70    
71          print "waiting for DHCP requests on ",$sock->sockhost,":",$sock->sockport,"\n";          server->refresh;
72    
73          my $buf;          my $buf;
74          $sock->recv($buf, 1024);          $sock->recv($buf, 1024);
75          print "<< peer:",$sock->peerhost,":",$sock->peerport,"\n";          my $size = 'empty';
76            $size = length($buf) . ' bytes' if defined $buf;
77    
78          if (defined $buf) {          print "packet from ",$sock->peerhost,":",$sock->peerport," $size\n" if $debug;
79            return unless $buf;
80    
81                  my $dhcp;          my $dhcp = Net::DHCP::Packet->new($buf);
82            $dhcp->comment( $transaction++ );
83    
84                  eval { $dhcp = Net::DHCP::Packet->new($buf); };          warn "recv: ", $dhcp->toString if $debug;
                 die "can't use request", dump( $buf ) if $@;  
85    
86                  if ( $debug ) {          my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2);
87                          warn "recv: ", $dhcp->toString, "\n\n";          my $ip = client_ip($mac);
88                  }  
89            my $hostname = $dhcp->getOptionValue(DHO_HOST_NAME);
90            print "$ip ", client::conf( $ip => 'hostname', default => $hostname ), " >> /etc/hosts\n";
91    
92            my $audit = { mac => $mac, ip => $ip, hostname => $hostname };
93            CouchDB::audit( 'request', $audit );
94    
95    =for later
96    
97            my $user_class = $dhcp->getOptionValue(DHO_USER_CLASS());
98    
99            if ( $user_class eq 'gPXE' ) {
100                    $file = $gpxe_file;
101            } elsif ( ! $file ) {
102                    $file = 'undionly.kpxe';
103            }
104    
105                  my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2);  =cut
                 my $ip = client_ip($mac);  
106    
107                  my $packet = new Net::DHCP::Packet(          config::for_ip( $ip );
                         Op              => BOOTREPLY(),  
                         Hops    => $dhcp->hops(),  
                         Xid             => $dhcp->xid(),  
                         Flags   => $dhcp->flags(),  
                         Ciaddr  => $dhcp->ciaddr(),  
                         Yiaddr  => $ip,  
                         Siaddr  => $server_ip,  
                         Giaddr  => $dhcp->giaddr(),  
                         Chaddr  => $dhcp->chaddr(),  
                         File    => 'undionly.kpxe',  
 #                       DHO_DHCP_MESSAGE_TYPE() => DHCPACK(),  
                 DHO_SUBNET_MASK() => '255.0.0.0',  
                 );  
   
                 warn ">> $mac == $ip server $server_ip\n";  
                   
                 warn "## ",$packet->toString(),"\n" if $debug;  
   
                 my $reply = IO::Socket::INET->new(  
                         LocalAddr => $server_ip,  
                         LocalPort => 67,  
                         Proto => "udp",  
                         Broadcast => 1,  
                         PeerAddr => '255.255.255.255',  
                         PeerPort => 68,  
                         Reuse => 1,  
                 ) or die "socket: $@";  
108    
109                  my $buff = $packet->serialize();          my $packet = {
110                  $reply->send( $buff, 0 ) or die "Error sending: $!\n";                  Op              => BOOTREPLY(),
111                    Hops    => $dhcp->hops(),
112                    Xid             => $dhcp->xid(),
113                    Flags   => $dhcp->flags(),
114                    Ciaddr  => $dhcp->ciaddr(),
115                    Yiaddr  => $ip,
116                    Siaddr  => $server::ip,
117                    Giaddr  => $dhcp->giaddr(),
118                    Chaddr  => $dhcp->chaddr(),
119                    File    => $file,
120                    DHO_DHCP_SERVER_IDENTIFIER()    => $server::ip, # busybox/udhcpc needs it but doesn't request
121            };
122    
123            my $options = {
124                    DHO_SUBNET_MASK()       => $server::netmask,
125                    DHO_ROUTERS()           => $server::ip,
126                    DHO_DOMAIN_NAME()       => $server::domain_name,
127                    DHO_NAME_SERVERS()      => $server::ip,
128                    DHO_DOMAIN_NAME_SERVERS() => $server::ip,
129                    DHO_HOST_NAME()         => client::conf( $ip, 'hostname' ),
130                    DHO_BROADCAST_ADDRESS() => $server::bcast,
131    #               DHO_NTP_SERVERS() => '',
132            };
133    
134            my @requested = split(/\s/, $dhcp->getOptionValue(DHO_DHCP_PARAMETER_REQUEST_LIST));
135            warn "options ",dump( $options ), ' requested: ',dump( @requested ) if $debug;
136    
137            my @missing;
138            foreach ( @requested ) {
139                    if ( defined $options->{$_} ) {
140                            $packet->{$_} = $options->{$_};
141                    } else {
142                            push @missing, $_;
143                    }
144            }
145    
146  #               system("arp -s $ip $mac"),          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' ) {
151                    my $DH0 = eval 'DHO_PXELINUX_' . uc $opt;
152                    warn "DH0: $@" if $@;
153                    my $v = eval "\$pxelinux::$opt";
154                    warn "v: $@" if $@;
155                    next unless defined $v;
156                    warn "pxelinux dhcp option $opt = $DH0 = $v\n" if $debug;
157                    $packet->{ $DH0 } = $v;
158            }
159    
160            my $messagetype = $dhcp->getOptionValue(DHO_DHCP_MESSAGE_TYPE());
161    
162            if ($messagetype eq DHCPDISCOVER()) {
163                    $audit->{type} = 'discover';
164                    $packet->{Comment} = $dhcp->comment();
165                    $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPOFFER();
166            } elsif ($messagetype eq DHCPREQUEST()) {
167                    my $requested_ip = $dhcp->getOptionValue(DHO_DHCP_REQUESTED_ADDRESS());
168                    $audit->{type} = 'request';
169                    if ( $ip eq $requested_ip ) {
170                            $packet->{DHO_DHCP_MESSAGE_TYPE()}      = DHCPACK();
171                            $packet->{DHO_DHCP_LEASE_TIME()}        = 5 * 60; # 5 min
172    #                       $packet->{DHO_ROOT_PATH()}              = '/exports/foobar';
173                    } else {
174                            $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPNAK();
175                            $packet->{DHO_DHCP_MESSAGE()} = "Bad request, expected $ip";
176                    }
177            } elsif ($messagetype eq DHCPINFORM()) {
178                    $audit->{type} = 'inform';
179          } else {          } else {
180                  print "No bootp request.\n";                  $audit->{type} = sprintf('ignored %x', $messagetype);
181          }          }
182    
183            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 );
187            warn "send ",$packet->toString() if $debug;
188    
189            CouchDB::audit( 'response', $audit );
190    
191            my $reply = IO::Socket::INET->new(
192                    LocalAddr => $server::ip,
193                    LocalPort => 67,
194                    Proto => "udp",
195                    Broadcast => 1,
196                    PeerAddr => '255.255.255.255',
197                    PeerPort => 68,
198                    Reuse => 1,
199            ) or die "socket: $@";
200    
201            my $buff = $packet->serialize();
202            $reply->send( $buff, 0 ) or die "Error sending: $!\n";
203    
204    #       system("arp -s $ip $mac"),
205    
206    }
207    
208    sub start {
209    
210            my $sock = IO::Socket::INET->new(
211                    LocalPort       => 67,
212    #               LocalAddr       => 'localhost',
213    #               LocalAddr       => '10.0.0.100',
214                    LocalAddr       => '0.0.0.0',
215                    Proto           => 'udp',
216                    ReuseAddr       => 1,
217    #               PeerPort        => getservbyname('bootpc', 'udp'),
218                    Broadcast       => 1,
219                    Type            => SOCK_DGRAM,
220            ) or die "Failed to bind to socket: $@";
221    
222            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) {
227                    process_packet $sock;
228            }
229  }  }
230    
231    warn "loaded";
232    
233    1;

Legend:
Removed from v.3  
changed lines
  Added in v.207

  ViewVC Help
Powered by ViewVC 1.1.26