/[pxelator]/bin/dhcpd.pl
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 /bin/dhcpd.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 22 by dpavlin, Tue Jul 28 16:35:49 2009 UTC revision 27 by dpavlin, Wed Jul 29 00:46:29 2009 UTC
# Line 8  use warnings; Line 8  use warnings;
8  use autodie;  use autodie;
9    
10  use IO::Socket::INET;  use IO::Socket::INET;
 use Net::DHCP::Packet;  
 use Net::DHCP::Constants;  
11  use File::Slurp;  use File::Slurp;
12  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
13    
14    use lib 'lib';
15    use Net::DHCP::Packet;
16    use Net::DHCP::Constants 0.67;
17  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;
18    
19  my $debug = shift @ARGV;  my $debug = shift @ARGV;
# Line 76  sub client_ip { Line 77  sub client_ip {
77          return $ip;          return $ip;
78  }  }
79    
80    my $transaction = 0; # FIXME predictible transaction numbers
81    
82  while (1) {  while (1) {
83    
84          require "config.pl"; # refresh config          require "config.pl"; # refresh config
# Line 88  while (1) { Line 91  while (1) {
91    
92          if (defined $buf) {          if (defined $buf) {
93    
94                  my $dhcp;                  my $dhcp = Net::DHCP::Packet->new($buf);
95                    $dhcp->comment( $transaction++ );
96    
97                  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";  
                 }  
98    
99                  my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2);                  my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2);
100                  my $ip = client_ip($mac);                  my $ip = client_ip($mac);
# Line 107  while (1) { Line 106  while (1) {
106                          $file = 'undionly.kpxe';                          $file = 'undionly.kpxe';
107                  }                  }
108    
109                  my $packet = new Net::DHCP::Packet(                  my $packet = {
110                          Op              => BOOTREPLY(),                          Op              => BOOTREPLY(),
111                          Hops    => $dhcp->hops(),                          Hops    => $dhcp->hops(),
112                          Xid             => $dhcp->xid(),                          Xid             => $dhcp->xid(),
# Line 119  while (1) { Line 118  while (1) {
118                          Chaddr  => $dhcp->chaddr(),                          Chaddr  => $dhcp->chaddr(),
119                          File    => $file,                          File    => $file,
120  #                       DHO_DHCP_MESSAGE_TYPE() => DHCPACK(),  #                       DHO_DHCP_MESSAGE_TYPE() => DHCPACK(),
121                  DHO_SUBNET_MASK() => '255.0.0.0',  #                       DHO_SUBNET_MASK() => '255.255.255.0',
122                  );                  };
123    
124                    my $messagetype = $dhcp->getOptionValue(DHO_DHCP_MESSAGE_TYPE());
125    
126                    if ($messagetype eq DHCPDISCOVER()) {
127                            warn "DHCP DISCOVER";
128                            $packet->{Comment} = $dhcp->comment();
129                            $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPOFFER();
130                    } elsif ($messagetype eq DHCPREQUEST()) {
131                            my $requested_ip = $dhcp->getOptionValue(DHO_DHCP_REQUESTED_ADDRESS());
132                            warn "DHCP REQUEST $requested_ip";
133                            if ( $ip eq $requested_ip ) {
134                                    $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPACK();
135                            } else {
136                                    $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPNAK();
137                                    $packet->{DHO_DHCP_MESSAGE()} = "Bad request, expected $ip";
138                            }
139                    } elsif ($messagetype eq DHCPINFORM()) {
140                            warn "DHCP INFORM ignored";
141                    } else {
142                            warn "$messagetype igored (bootp?)";
143                    }
144    
145    
146                  warn ">> $mac == $ip server: $server_ip file: $file\n";                  warn ">> $mac == $ip server: $server_ip", $file ? " file: $file\n" : "\n";
147    
148                    $packet = new Net::DHCP::Packet( %$packet );
149                  warn "## ",$packet->toString(),"\n" if $debug;                  warn "## ",$packet->toString(),"\n" if $debug;
150    
151                  my $reply = IO::Socket::INET->new(                  my $reply = IO::Socket::INET->new(

Legend:
Removed from v.22  
changed lines
  Added in v.27

  ViewVC Help
Powered by ViewVC 1.1.26