/[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 6 by dpavlin, Sun Jul 26 15:16:52 2009 UTC revision 17 by dpavlin, Tue Jul 28 10:29:33 2009 UTC
# Line 8  use warnings; Line 8  use warnings;
8  use IO::Socket::INET;  use IO::Socket::INET;
9  use Net::DHCP::Packet;  use Net::DHCP::Packet;
10  use Net::DHCP::Constants;  use Net::DHCP::Constants;
11    use File::Slurp;
12  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
13    
14  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;
15    
16  my $debug = shift @ARGV;  my $debug = shift @ARGV;
17    
18  our ( $server_ip, $next_file );  our ( $file, $next_file );
19  require "config.pl";  our ( $ip_from, $ip_to ) = ( 10, 100 );
20    
21    our $server_ip = readlink 'conf/server.ip';
22    
23  my $sock = IO::Socket::INET->new(  my $sock = IO::Socket::INET->new(
24          LocalPort       => 67,          LocalPort       => 67,
# Line 29  my $sock = IO::Socket::INET->new( Line 32  my $sock = IO::Socket::INET->new(
32          Type            => SOCK_DGRAM,          Type            => SOCK_DGRAM,
33  ) or die "Failed to bind to socket: $@";  ) or die "Failed to bind to socket: $@";
34    
35  my $_ip = 10;  
36  my $_mac2ip;  my $addr = $ip_from;
37    
38  sub client_ip {  sub client_ip {
39          my ( $mac ) = @_;          my ( $mac ) = @_;
40    
41          my $ip = $_mac2ip->{$mac};          my $conf = "conf/$server_ip";
42          return $ip if $ip;          mkdir $conf unless -e $conf;
43    
44            if ( -e "$conf/$mac" ) {
45                    my $ip = read_file "conf/mac/$mac";
46                    print "$mac old $ip\n";
47                    return $ip;
48            }
49    
50          $ip = "10.0.0.$_ip";          mkdir $_ foreach grep { ! -e $_ } map { "$conf/$_" } ( 'ip', 'mac' );
         $_mac2ip->{$mac} = $ip;  
51    
52          $_ip++;          my $prefix = $server_ip;
53          if ( $_ip == 100 ) {          $prefix =~ s{\.\d+$}{.};
54                  warn "IP roll-over to 10\n";          my $ip = $prefix . $addr;
55                  $_ip = 10;          while ( -e "conf/ip/$ip" ) {
56                    $ip = $prefix . $addr++;
57                    die "all addresses allocated!" if $addr == $ip_to;
58          }          }
59    
60            write_file "$conf/mac/$mac", $ip;
61            symlink    "$conf/mac/$mac", "conf/ip/$ip";
62    
63            print "$mac NEW $ip\n";
64    
65          return $ip;          return $ip;
66  }  }
67    
68  while (1) {  while (1) {
69    
70            require "config.pl"; # refresh config
71    
72          print "waiting for DHCP requests on ",$sock->sockhost,":",$sock->sockport,"\n";          print "waiting for DHCP requests on ",$sock->sockhost,":",$sock->sockport,"\n";
73    
74          my $buf;          my $buf;
75          $sock->recv($buf, 1024);          $sock->recv($buf, 1024);
76          print "<< peer:",$sock->peerhost,":",$sock->peerport,"\n";          print "<< ",$sock->peerhost,":",$sock->peerport,"\n";
77    
78          if (defined $buf) {          if (defined $buf) {
79    
# Line 72  while (1) { Line 89  while (1) {
89                  my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2);                  my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2);
90                  my $ip = client_ip($mac);                  my $ip = client_ip($mac);
91    
92                  my $file =  $next_file;                  if ( ! $file ) {
93                  $file = 'undionly.kpxe' if ! $file || $dhcp->getOptionValue(DHO_USER_CLASS()) ne 'gPXE';                          if ( $dhcp->getOptionValue(DHO_USER_CLASS()) ne 'gPXE' ) {
94                                    $file = 'undionly.kpxe';
95                            } else {
96                                    $file = $next_file;
97                            }
98                    }
99    
100                  my $packet = new Net::DHCP::Packet(                  my $packet = new Net::DHCP::Packet(
101                          Op              => BOOTREPLY(),                          Op              => BOOTREPLY(),

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

  ViewVC Help
Powered by ViewVC 1.1.26