/[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 1 by dpavlin, Sun Jul 26 00:38:57 2009 UTC revision 28 by dpavlin, Wed Jul 29 00:59:55 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 File::Slurp;
 use Net::DHCP::Constants;  
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;
20    
21  my $ip_server = '10.0.0.100';  our ( $file, $gpxe_file );
22    our ( $ip_from, $ip_to ) = ( 10, 100 );
23    
24    our $server_ip = readlink 'conf/server.ip' if -l 'conf/server.ip';
25    
26    if ( ! $server_ip ) {
27            $server_ip = `/sbin/ifconfig`;
28            $server_ip =~ s/^.+?addr:([\d\.]+).*$/$1/gs;
29            warn "auto-configure server ip to $server_ip\n";
30    } else {
31            warn "server ip $server_ip\n";
32    }
33    
34  my $sock = IO::Socket::INET->new(  my $sock = IO::Socket::INET->new(
35          LocalPort       => 67,          LocalPort       => 67,
# Line 28  my $sock = IO::Socket::INET->new( Line 43  my $sock = IO::Socket::INET->new(
43          Type            => SOCK_DGRAM,          Type            => SOCK_DGRAM,
44  ) or die "Failed to bind to socket: $@";  ) or die "Failed to bind to socket: $@";
45    
46  my $_ip = 10;  
47  my $_mac2ip;  my $addr = $ip_from;
48    
49  sub client_ip {  sub client_ip {
50          my ( $mac ) = @_;          my ( $mac ) = @_;
51    
52          my $ip = $_mac2ip->{$mac};          my $conf = "conf/$server_ip";
53          return $ip if $ip;          mkdir $conf unless -e $conf;
54    
55            if ( -e "$conf/mac/$mac" ) {
56                    my $ip = read_file "$conf/mac/$mac";
57                    print "$mac old $ip\n";
58                    return $ip;
59            }
60    
61          $ip = "10.0.0.$_ip";          mkdir $_ foreach grep { ! -e $_ } map { "$conf/$_" } ( 'ip', 'mac' );
         $_mac2ip->{$mac} = $ip;  
62    
63          $_ip++;          my $prefix = $server_ip;
64          if ( $_ip == 100 ) {          $prefix =~ s{\.\d+$}{.};
65                  warn "IP roll-over to 10\n";          my $ip = $prefix . $addr;
66                  $_ip = 10;          while ( -e "conf/ip/$ip" ) {
67                    $ip = $prefix . $addr++;
68                    die "all addresses allocated!" if $addr == $ip_to;
69          }          }
70    
71            write_file "$conf/mac/$mac", $ip;
72            unlink     "$conf/ip/$ip" if -e "$conf/ip/$ip";
73            symlink    "$conf/mac/$mac", "$conf/ip/$ip";
74    
75            print "$mac NEW $ip\n";
76    
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
85    
86          print "waiting for DHCP requests on ",$sock->sockhost,":",$sock->sockport,"\n";          print "waiting for DHCP requests on ",$sock->sockhost,":",$sock->sockport,"\n";
87    
88          my $buf;          my $buf;
89          $sock->recv($buf, 1024);          $sock->recv($buf, 1024);
90          print "<< peer:",$sock->peerhost,":",$sock->peerport,"\n";          print "<< ",$sock->peerhost,":",$sock->peerport,"\n";
91    
92          if (defined $buf) {          if (defined $buf) {
93    
94                  my $dhcp;                  my $dhcp = Net::DHCP::Packet->new($buf);
95                    $dhcp->comment( $transaction++ );
                 eval { $dhcp = Net::DHCP::Packet->new($buf); };  
                 die "can't use request", dump( $buf ) if $@;  
96    
97                  if ( $debug ) {                  warn "recv: ", $dhcp->toString, "\n\n";
                         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);
101                    my $user_class = $dhcp->getOptionValue(DHO_USER_CLASS());
102    
103                    if ( $user_class eq 'gPXE' ) {
104                            $file = $gpxe_file;
105                    } elsif ( ! $file ) {
106                            $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(),
113                          Flags   => $dhcp->flags(),                          Flags   => $dhcp->flags(),
114                          Ciaddr  => $dhcp->ciaddr(),                          Ciaddr  => $dhcp->ciaddr(),
115                          Yiaddr  => $ip,                          Yiaddr  => $ip,
116                          Siaddr  => $ip_server,                          Siaddr  => $server_ip,
117                          Giaddr  => $dhcp->giaddr(),                          Giaddr  => $dhcp->giaddr(),
118                          Chaddr  => $dhcp->chaddr(),                          Chaddr  => $dhcp->chaddr(),
119                          File    => 'undionly.kpxe',                          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                                    $packet->{DHO_DHCP_LEASE_TIME()}   = 100;
136                            } else {
137                                    $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPNAK();
138                                    $packet->{DHO_DHCP_MESSAGE()} = "Bad request, expected $ip";
139                            }
140                    } elsif ($messagetype eq DHCPINFORM()) {
141                            warn "DHCP INFORM ignored";
142                    } else {
143                            warn "$messagetype igored (bootp?)";
144                    }
145    
146    
147                    warn ">> $mac == $ip server: $server_ip", $file ? " file: $file\n" : "\n";
148    
149                  warn ">> $mac == $ip server $ip_server\n";                  $packet = new Net::DHCP::Packet( %$packet );
                   
150                  warn "## ",$packet->toString(),"\n" if $debug;                  warn "## ",$packet->toString(),"\n" if $debug;
151    
152                  my $reply = IO::Socket::INET->new(                  my $reply = IO::Socket::INET->new(
153                          LocalAddr => $ip_server,                          LocalAddr => $server_ip,
154                          LocalPort => 67,                          LocalPort => 67,
155                          Proto => "udp",                          Proto => "udp",
156                          Broadcast => 1,                          Broadcast => 1,

Legend:
Removed from v.1  
changed lines
  Added in v.28

  ViewVC Help
Powered by ViewVC 1.1.26