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

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

  ViewVC Help
Powered by ViewVC 1.1.26