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

Legend:
Removed from v.6  
changed lines
  Added in v.177

  ViewVC Help
Powered by ViewVC 1.1.26