/[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 33 by dpavlin, Wed Jul 29 12:23:47 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 68  sub client_ip { Line 69  sub client_ip {
69          }          }
70    
71          write_file "$conf/mac/$mac", $ip;          write_file "$conf/mac/$mac", $ip;
72          unlink     "$conf/ip/$ip" if -e "$conf/ip/$ip";          if ( -l "$conf/ip/$ip" && readlink "$conf/ip/$ip" ne "$conf/mac/$mac") {
73          symlink    "$conf/mac/$mac", "$conf/ip/$ip";                  unlink     "$conf/ip/$ip";
74                    symlink    "$conf/mac/$mac", "$conf/ip/$ip";
75                    warn "$mac IP changed to $ip";
76            }
77    
78          print "$mac NEW $ip\n";          print "$mac NEW $ip\n";
79    
80          return $ip;          return $ip;
81  }  }
82    
83    my $transaction = 0; # FIXME predictible transaction numbers
84    
85  while (1) {  while (1) {
86    
87          require "config.pl"; # refresh config          require "config.pl"; # refresh config
# Line 88  while (1) { Line 94  while (1) {
94    
95          if (defined $buf) {          if (defined $buf) {
96    
97                  my $dhcp;                  my $dhcp = Net::DHCP::Packet->new($buf);
98                    $dhcp->comment( $transaction++ );
                 eval { $dhcp = Net::DHCP::Packet->new($buf); };  
                 die "can't use request", dump( $buf ) if $@;  
99    
100                  if ( $debug ) {                  warn "recv: ", $dhcp->toString, "\n\n";
                         warn "recv: ", $dhcp->toString, "\n\n";  
                 }  
101    
102                  my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2);                  my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2);
103                  my $ip = client_ip($mac);                  my $ip = client_ip($mac);
# Line 107  while (1) { Line 109  while (1) {
109                          $file = 'undionly.kpxe';                          $file = 'undionly.kpxe';
110                  }                  }
111    
112                  my $packet = new Net::DHCP::Packet(                  my $packet = {
113                          Op              => BOOTREPLY(),                          Op              => BOOTREPLY(),
114                          Hops    => $dhcp->hops(),                          Hops    => $dhcp->hops(),
115                          Xid             => $dhcp->xid(),                          Xid             => $dhcp->xid(),
# Line 119  while (1) { Line 121  while (1) {
121                          Chaddr  => $dhcp->chaddr(),                          Chaddr  => $dhcp->chaddr(),
122                          File    => $file,                          File    => $file,
123  #                       DHO_DHCP_MESSAGE_TYPE() => DHCPACK(),  #                       DHO_DHCP_MESSAGE_TYPE() => DHCPACK(),
124                  DHO_SUBNET_MASK() => '255.0.0.0',  #                       DHO_SUBNET_MASK() => '255.255.255.0',
125                  );                  };
126    
127                    my $messagetype = $dhcp->getOptionValue(DHO_DHCP_MESSAGE_TYPE());
128    
129                    if ($messagetype eq DHCPDISCOVER()) {
130                            warn "DHCP DISCOVER";
131                            $packet->{Comment} = $dhcp->comment();
132                            $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPOFFER();
133                    } elsif ($messagetype eq DHCPREQUEST()) {
134                            my $requested_ip = $dhcp->getOptionValue(DHO_DHCP_REQUESTED_ADDRESS());
135                            warn "DHCP REQUEST $requested_ip";
136                            if ( $ip eq $requested_ip ) {
137                                    $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPACK();
138                                    $packet->{DHO_DHCP_LEASE_TIME()}   = 100;
139                            } else {
140                                    $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPNAK();
141                                    $packet->{DHO_DHCP_MESSAGE()} = "Bad request, expected $ip";
142                            }
143                    } elsif ($messagetype eq DHCPINFORM()) {
144                            warn "DHCP INFORM ignored";
145                    } else {
146                            warn "$messagetype igored (bootp?)";
147                    }
148    
149    
150                  warn ">> $mac == $ip server: $server_ip file: $file\n";                  warn ">> $mac == $ip server: $server_ip", $file ? " file: $file\n" : "\n";
151    
152                    $packet = new Net::DHCP::Packet( %$packet );
153                  warn "## ",$packet->toString(),"\n" if $debug;                  warn "## ",$packet->toString(),"\n" if $debug;
154    
155                  my $reply = IO::Socket::INET->new(                  my $reply = IO::Socket::INET->new(

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

  ViewVC Help
Powered by ViewVC 1.1.26