/[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 4 by dpavlin, Sun Jul 26 14:21:48 2009 UTC revision 40 by dpavlin, Wed Jul 29 14:29:55 2009 UTC
# Line 5  Line 5 
5  use strict;  use strict;
6  use warnings;  use warnings;
7    
8    use autodie;
9    
10  use IO::Socket::INET;  use IO::Socket::INET;
11  use Net::DHCP::Packet;  use File::Slurp;
 use Net::DHCP::Constants;  
12  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
13    use Net::Ping;
14    
15    use lib 'lib';
16    use Net::DHCP::Packet;
17    use Net::DHCP::Constants 0.67;
18  die "need to run $0 as root like this\nsudo $0\n" unless $< == 0;  die "need to run $0 as root like this\nsudo $0\n" unless $< == 0;
19    
20  my $debug = shift @ARGV;  my $debug = shift @ARGV;
21    
22  our ( $server_ip, $next_file );  our ( $file, $gpxe_file );
23  require "config.pl";  our ( $ip_from, $ip_to ) = ( 10, 100 );
24    
25    our $server_ip = readlink 'conf/server.ip' if -l 'conf/server.ip';
26    
27    if ( ! $server_ip ) {
28            $server_ip = `/sbin/ifconfig`;
29            $server_ip =~ s/^.+?addr:([\d\.]+).*$/$1/gs;
30            warn "auto-configure server ip to $server_ip\n";
31    } else {
32            warn "server ip $server_ip\n";
33    }
34    
35  my $sock = IO::Socket::INET->new(  my $sock = IO::Socket::INET->new(
36          LocalPort       => 67,          LocalPort       => 67,
# Line 29  my $sock = IO::Socket::INET->new( Line 44  my $sock = IO::Socket::INET->new(
44          Type            => SOCK_DGRAM,          Type            => SOCK_DGRAM,
45  ) or die "Failed to bind to socket: $@";  ) or die "Failed to bind to socket: $@";
46    
47  my $_ip = 10;  
48  my $_mac2ip;  my $addr = $ip_from;
 my $_ip_file;  
49    
50  sub client_ip {  sub client_ip {
51          my ( $mac ) = @_;          my ( $mac ) = @_;
52    
53          my $ip = $_mac2ip->{$mac};          my $conf = "conf/$server_ip";
54          return $ip if $ip;          mkdir $conf unless -e $conf;
55    
56            if ( -e "$conf/mac/$mac" ) {
57                    my $ip = read_file "$conf/mac/$mac";
58                    print "$mac old $ip\n";
59                    return $ip;
60            }
61    
62            mkdir $_ foreach grep { ! -e $_ } map { "$conf/$_" } ( 'ip', 'mac' );
63    
64            my $p = Net::Ping->new;
65    
66          $ip = "10.0.0.$_ip";          my $prefix = $server_ip;
67          $_mac2ip->{$mac} = $ip;          $prefix =~ s{\.\d+$}{.};
68            my $ip = $prefix . $addr;
69            while ( -e "conf/ip/$ip" || $p->ping( $ip ) ) {
70                    $ip = $prefix . $addr++;
71                    die "all addresses allocated!" if $addr == $ip_to;
72            }
73    
74          $_ip++;          write_file "$conf/mac/$mac", $ip;
75          if ( $_ip == 100 ) {          if ( -l "$conf/ip/$ip" && readlink "$conf/ip/$ip" ne "$conf/mac/$mac") {
76                  warn "IP roll-over to 10\n";                  unlink     "$conf/ip/$ip";
77                  $_ip = 10;                  symlink    "$conf/mac/$mac", "$conf/ip/$ip";
78                    warn "$mac IP changed to $ip";
79          }          }
80    
81            print "$mac NEW $ip\n";
82    
83          return $ip;          return $ip;
84  }  }
85    
86    my $transaction = 0; # FIXME predictible transaction numbers
87    
88  while (1) {  while (1) {
89    
90            require "config.pl"; # refresh config
91    
92          print "waiting for DHCP requests on ",$sock->sockhost,":",$sock->sockport,"\n";          print "waiting for DHCP requests on ",$sock->sockhost,":",$sock->sockport,"\n";
93    
94          my $buf;          my $buf;
95          $sock->recv($buf, 1024);          $sock->recv($buf, 1024);
96          print "<< peer:",$sock->peerhost,":",$sock->peerport,"\n";          print "<< ",$sock->peerhost,":",$sock->peerport,"\n";
97    
98          if (defined $buf) {          if (defined $buf) {
99    
100                  my $dhcp;                  my $dhcp = Net::DHCP::Packet->new($buf);
101                    $dhcp->comment( $transaction++ );
102    
103                  eval { $dhcp = Net::DHCP::Packet->new($buf); };                  warn "recv: ", $dhcp->toString, "\n\n";
                 die "can't use request", dump( $buf ) if $@;  
   
                 if ( $debug ) {  
                         warn "recv: ", $dhcp->toString, "\n\n";  
                 }  
104    
105                  my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2);                  my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2);
106                  my $ip = client_ip($mac);                  my $ip = client_ip($mac);
107                    my $user_class = $dhcp->getOptionValue(DHO_USER_CLASS());
108    
109                  my $packet = new Net::DHCP::Packet(                  if ( $user_class eq 'gPXE' ) {
110                            $file = $gpxe_file;
111                    } elsif ( ! $file ) {
112                            $file = 'undionly.kpxe';
113                    }
114    
115                    my $packet = {
116                          Op              => BOOTREPLY(),                          Op              => BOOTREPLY(),
117                          Hops    => $dhcp->hops(),                          Hops    => $dhcp->hops(),
118                          Xid             => $dhcp->xid(),                          Xid             => $dhcp->xid(),
# Line 83  while (1) { Line 122  while (1) {
122                          Siaddr  => $server_ip,                          Siaddr  => $server_ip,
123                          Giaddr  => $dhcp->giaddr(),                          Giaddr  => $dhcp->giaddr(),
124                          Chaddr  => $dhcp->chaddr(),                          Chaddr  => $dhcp->chaddr(),
125                          File    => $_ip_file->{$ip} || 'undionly.kpxe',                          File    => $file,
126  #                       DHO_DHCP_MESSAGE_TYPE() => DHCPACK(),                  };
127                  DHO_SUBNET_MASK() => '255.0.0.0',  
128                  );                  my $messagetype = $dhcp->getOptionValue(DHO_DHCP_MESSAGE_TYPE());
129    
130                    if ($messagetype eq DHCPDISCOVER()) {
131                            warn "DHCP DISCOVER";
132                            $packet->{Comment} = $dhcp->comment();
133                            $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPOFFER();
134                    } elsif ($messagetype eq DHCPREQUEST()) {
135                            my $requested_ip = $dhcp->getOptionValue(DHO_DHCP_REQUESTED_ADDRESS());
136                            warn "DHCP REQUEST $requested_ip";
137                            if ( $ip eq $requested_ip ) {
138                                    $packet->{DHO_DHCP_MESSAGE_TYPE()}      = DHCPACK();
139                                    $packet->{DHO_DHCP_LEASE_TIME()}        = 5 * 60; # 5 min
140    #                               $packet->{DHO_DHCP_SERVER_IDENTIFIER()} = $server_ip;           # FIXME
141                                    $packet->{DHO_SUBNET_MASK()}            = '255.255.255.0';
142                                    $packet->{DHO_ROUTERS()}                = $server_ip;
143    #                               $packet->{DHO_DOMAIN_NAME()}            = 'pxelator.lan';
144    #                               $packet->{DHO_NAME_SERVERS()}           = $server_ip;
145    #                               $packet->{DHO_ROOT_PATH()}              = '/exports/foobar';
146                            } else {
147                                    $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPNAK();
148                                    $packet->{DHO_DHCP_MESSAGE()} = "Bad request, expected $ip";
149                            }
150                    } elsif ($messagetype eq DHCPINFORM()) {
151                            warn "DHCP INFORM ignored";
152                    } else {
153                            warn "$messagetype igored (bootp?)";
154                    }
155    
156    
157                  $_ip_file->{$ip} = $next_file;                  warn ">> $mac == $ip server: $server_ip", $file ? " file: $file\n" : "\n";
158    
159                  warn ">> $mac == $ip server $server_ip\n";                  $packet = new Net::DHCP::Packet( %$packet );
                   
160                  warn "## ",$packet->toString(),"\n" if $debug;                  warn "## ",$packet->toString(),"\n" if $debug;
161    
162                  my $reply = IO::Socket::INET->new(                  my $reply = IO::Socket::INET->new(

Legend:
Removed from v.4  
changed lines
  Added in v.40

  ViewVC Help
Powered by ViewVC 1.1.26