/[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 4 by dpavlin, Sun Jul 26 14:21:48 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;
 my $_ip_file;  
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          $ip = "10.0.0.$_ip";          if ( -e "$conf/$mac" ) {
45          $_mac2ip->{$mac} = $ip;                  my $ip = read_file "conf/mac/$mac";
46                    print "$mac old $ip\n";
47                    return $ip;
48            }
49    
50          $_ip++;          mkdir $_ foreach grep { ! -e $_ } map { "$conf/$_" } ( 'ip', 'mac' );
51          if ( $_ip == 100 ) {  
52                  warn "IP roll-over to 10\n";          my $prefix = $server_ip;
53                  $_ip = 10;          $prefix =~ s{\.\d+$}{.};
54            my $ip = $prefix . $addr;
55            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 73  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                    if ( ! $file ) {
93                            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(),
102                          Hops    => $dhcp->hops(),                          Hops    => $dhcp->hops(),
# Line 83  while (1) { Line 107  while (1) {
107                          Siaddr  => $server_ip,                          Siaddr  => $server_ip,
108                          Giaddr  => $dhcp->giaddr(),                          Giaddr  => $dhcp->giaddr(),
109                          Chaddr  => $dhcp->chaddr(),                          Chaddr  => $dhcp->chaddr(),
110                          File    => $_ip_file->{$ip} || 'undionly.kpxe',                          File    => $file,
111  #                       DHO_DHCP_MESSAGE_TYPE() => DHCPACK(),  #                       DHO_DHCP_MESSAGE_TYPE() => DHCPACK(),
112                  DHO_SUBNET_MASK() => '255.0.0.0',                  DHO_SUBNET_MASK() => '255.0.0.0',
113                  );                  );
114    
                 $_ip_file->{$ip} = $next_file;  
   
115                  warn ">> $mac == $ip server $server_ip\n";                  warn ">> $mac == $ip server $server_ip\n";
116                                    
117                  warn "## ",$packet->toString(),"\n" if $debug;                  warn "## ",$packet->toString(),"\n" if $debug;

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

  ViewVC Help
Powered by ViewVC 1.1.26