/[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 3 by dpavlin, Sun Jul 26 13:58:39 2009 UTC revision 22 by dpavlin, Tue Jul 28 16:35:49 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 Net::DHCP::Packet;
12  use Net::DHCP::Constants;  use Net::DHCP::Constants;
13    use File::Slurp;
14  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
15    
16  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;
17    
18  my $debug = shift @ARGV;  my $debug = shift @ARGV;
19    
20  our $server_ip;  our ( $file, $gpxe_file );
21  require "config.pl";  our ( $ip_from, $ip_to ) = ( 10, 100 );
22    
23    our $server_ip = readlink 'conf/server.ip' if -l 'conf/server.ip';
24    
25    if ( ! $server_ip ) {
26            $server_ip = `/sbin/ifconfig`;
27            $server_ip =~ s/^.+?addr:([\d\.]+).*$/$1/gs;
28            warn "auto-configure server ip to $server_ip\n";
29    } else {
30            warn "server ip $server_ip\n";
31    }
32    
33  my $sock = IO::Socket::INET->new(  my $sock = IO::Socket::INET->new(
34          LocalPort       => 67,          LocalPort       => 67,
# Line 29  my $sock = IO::Socket::INET->new( Line 42  my $sock = IO::Socket::INET->new(
42          Type            => SOCK_DGRAM,          Type            => SOCK_DGRAM,
43  ) or die "Failed to bind to socket: $@";  ) or die "Failed to bind to socket: $@";
44    
45  my $_ip = 10;  
46  my $_mac2ip;  my $addr = $ip_from;
 my $_ip_file;  
47    
48  sub client_ip {  sub client_ip {
49          my ( $mac ) = @_;          my ( $mac ) = @_;
50    
51          my $ip = $_mac2ip->{$mac};          my $conf = "conf/$server_ip";
52          return $ip if $ip;          mkdir $conf unless -e $conf;
53    
54            if ( -e "$conf/mac/$mac" ) {
55                    my $ip = read_file "$conf/mac/$mac";
56                    print "$mac old $ip\n";
57                    return $ip;
58            }
59    
60          $ip = "10.0.0.$_ip";          mkdir $_ foreach grep { ! -e $_ } map { "$conf/$_" } ( 'ip', 'mac' );
         $_mac2ip->{$mac} = $ip;  
61    
62          $_ip++;          my $prefix = $server_ip;
63          if ( $_ip == 100 ) {          $prefix =~ s{\.\d+$}{.};
64                  warn "IP roll-over to 10\n";          my $ip = $prefix . $addr;
65                  $_ip = 10;          while ( -e "conf/ip/$ip" ) {
66                    $ip = $prefix . $addr++;
67                    die "all addresses allocated!" if $addr == $ip_to;
68          }          }
69    
70            write_file "$conf/mac/$mac", $ip;
71            unlink     "$conf/ip/$ip" if -e "$conf/ip/$ip";
72            symlink    "$conf/mac/$mac", "$conf/ip/$ip";
73    
74            print "$mac NEW $ip\n";
75    
76          return $ip;          return $ip;
77  }  }
78    
79  while (1) {  while (1) {
80    
81            require "config.pl"; # refresh config
82    
83          print "waiting for DHCP requests on ",$sock->sockhost,":",$sock->sockport,"\n";          print "waiting for DHCP requests on ",$sock->sockhost,":",$sock->sockport,"\n";
84    
85          my $buf;          my $buf;
86          $sock->recv($buf, 1024);          $sock->recv($buf, 1024);
87          print "<< peer:",$sock->peerhost,":",$sock->peerport,"\n";          print "<< ",$sock->peerhost,":",$sock->peerport,"\n";
88    
89          if (defined $buf) {          if (defined $buf) {
90    
# Line 72  while (1) { Line 99  while (1) {
99    
100                  my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2);                  my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2);
101                  my $ip = client_ip($mac);                  my $ip = client_ip($mac);
102                    my $user_class = $dhcp->getOptionValue(DHO_USER_CLASS());
103    
104                    if ( $user_class eq 'gPXE' ) {
105                            $file = $gpxe_file;
106                    } elsif ( ! $file ) {
107                            $file = 'undionly.kpxe';
108                    }
109    
110                  my $packet = new Net::DHCP::Packet(                  my $packet = new Net::DHCP::Packet(
111                          Op              => BOOTREPLY(),                          Op              => BOOTREPLY(),
# Line 83  while (1) { Line 117  while (1) {
117                          Siaddr  => $server_ip,                          Siaddr  => $server_ip,
118                          Giaddr  => $dhcp->giaddr(),                          Giaddr  => $dhcp->giaddr(),
119                          Chaddr  => $dhcp->chaddr(),                          Chaddr  => $dhcp->chaddr(),
120                          File    => 'undionly.kpxe',                          File    => $file,
121  #                       DHO_DHCP_MESSAGE_TYPE() => DHCPACK(),  #                       DHO_DHCP_MESSAGE_TYPE() => DHCPACK(),
122                  DHO_SUBNET_MASK() => '255.0.0.0',                  DHO_SUBNET_MASK() => '255.0.0.0',
123                  );                  );
124    
125                  warn ">> $mac == $ip server $server_ip\n";                  warn ">> $mac == $ip server: $server_ip file: $file\n";
126                    
127                  warn "## ",$packet->toString(),"\n" if $debug;                  warn "## ",$packet->toString(),"\n" if $debug;
128    
129                  my $reply = IO::Socket::INET->new(                  my $reply = IO::Socket::INET->new(

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

  ViewVC Help
Powered by ViewVC 1.1.26