/[pxelator]/lib/PXElator/dhcpd.pm
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 /lib/PXElator/dhcpd.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 45 by dpavlin, Wed Jul 29 22:04:58 2009 UTC revision 129 by dpavlin, Mon Aug 3 20:53:46 2009 UTC
# Line 1  Line 1 
1  #!/usr/bin/perl  package dhcpd;
2    
3  =head1 dhcpd  =head1 dhcpd
4    
# Line 19  use IO::Socket::INET; Line 19  use IO::Socket::INET;
19  use File::Slurp;  use File::Slurp;
20  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
21  use Net::Ping;  use Net::Ping;
 use Module::Refresh;  
22    
23  use lib '..';  use lib '..';
24  use Net::DHCP::Packet;  use Net::DHCP::Packet;
25  use Net::DHCP::Constants 0.67;  use Net::DHCP::Constants 0.67;
26    
27  use server;  use server;
28  use pxe;  my $debug = server::debug;
   
 my $debug = 1;  
29    
30  if ( ! $server::ip ) {  if ( ! $server::ip ) {
31          my $server_ip = `/sbin/ifconfig`;          my $server_ip = `/sbin/ifconfig`;
# Line 36  if ( ! $server::ip ) { Line 33  if ( ! $server::ip ) {
33          $server::ip = $server_ip;          $server::ip = $server_ip;
34  }  }
35    
36  warn "server ip $server::ip file: $pxe::file range: $server::ip_from - $server::ip_to\n";  warn "server ip $server::ip range: $server::ip_from - $server::ip_to\n";
37    
38  my $addr = $server::ip_from;  my $addr = $server::ip_from;
39    
40  sub client_ip {  sub client_ip {
41          my ( $mac ) = @_;          my ( $mac ) = @_;
42    
43          my $conf = "conf/$server::ip";          my $conf = "$server::base_dir/conf/$server::ip";
44          mkdir $conf unless -e $conf;          mkdir $conf unless -e $conf;
45    
46          if ( -e "$conf/mac/$mac" ) {          if ( -e "$conf/mac/$mac" ) {
47                  my $ip = read_file "$conf/mac/$mac";                  my $ip = read_file "$conf/mac/$mac";
48                  print "$mac old $ip\n";                  print "RENEW $mac $ip\n";
49                  return $ip;                  return $ip;
50          }          }
51    
# Line 76  sub client_ip { Line 73  sub client_ip {
73          return $ip;          return $ip;
74  }  }
75    
76    use log;
77    use config;
78    use pxelinux;
79    
80    our $file;
81  our $transaction = 0; # FIXME predictible transaction numbers  our $transaction = 0; # FIXME predictible transaction numbers
82    
83  sub process_packet {  sub process_packet {
84          my $sock = shift;          my $sock = shift;
85    
86            server->refresh;
87    
88          my $buf;          my $buf;
89          $sock->recv($buf, 1024);          $sock->recv($buf, 1024);
90          my $size = 'empty';          my $size = 'empty';
91          $size = length($buf) . ' bytes' if defined $buf;          $size = length($buf) . ' bytes' if defined $buf;
92    
93          print "packet from ",$sock->peerhost,":",$sock->peerport," $size\n";          print "packet from ",$sock->peerhost,":",$sock->peerport," $size\n" if $debug;
94          return unless $buf;          return unless $buf;
95    
96          my $dhcp = Net::DHCP::Packet->new($buf);          my $dhcp = Net::DHCP::Packet->new($buf);
97          $dhcp->comment( $transaction++ );          $dhcp->comment( $transaction++ );
98    
99          warn "recv: ", $dhcp->toString;          warn "recv: ", $dhcp->toString if $debug;
100    
101          my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2);          my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2);
102          my $ip = client_ip($mac);          my $ip = client_ip($mac);
# Line 109  sub process_packet { Line 113  sub process_packet {
113    
114  =cut  =cut
115    
116            config::for_ip( $ip );
117    
118          my $packet = {          my $packet = {
119                  Op              => BOOTREPLY(),                  Op              => BOOTREPLY(),
120                  Hops    => $dhcp->hops(),                  Hops    => $dhcp->hops(),
# Line 119  sub process_packet { Line 125  sub process_packet {
125                  Siaddr  => $server::ip,                  Siaddr  => $server::ip,
126                  Giaddr  => $dhcp->giaddr(),                  Giaddr  => $dhcp->giaddr(),
127                  Chaddr  => $dhcp->chaddr(),                  Chaddr  => $dhcp->chaddr(),
128                  File    => $pxe::file,                  File    => $file,
129          };          };
130    
131            foreach my $opt ( 'magic', 'config_file', 'path_prefix', 'reboot_time' ) {
132                    my $DH0 = eval 'DHO_PXELINUX_' . uc $opt;
133                    warn "DH0: $@" if $@;
134                    my $v = eval "\$pxelinux::$opt";
135                    warn "v: $@" if $@;
136                    next unless defined $v;
137                    warn "pxelinux dhcp option $opt = $DH0 = $v";
138                    $packet->{ $DH0 } = $v;
139            }
140    
141          my $messagetype = $dhcp->getOptionValue(DHO_DHCP_MESSAGE_TYPE());          my $messagetype = $dhcp->getOptionValue(DHO_DHCP_MESSAGE_TYPE());
142    
143          if ($messagetype eq DHCPDISCOVER()) {          if ($messagetype eq DHCPDISCOVER()) {
144                  warn "DHCP DISCOVER";                  log::mac $mac, "DHCP DISCOVER";
145                  $packet->{Comment} = $dhcp->comment();                  $packet->{Comment} = $dhcp->comment();
146                  $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPOFFER();                  $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPOFFER();
147          } elsif ($messagetype eq DHCPREQUEST()) {          } elsif ($messagetype eq DHCPREQUEST()) {
148                  my $requested_ip = $dhcp->getOptionValue(DHO_DHCP_REQUESTED_ADDRESS());                  my $requested_ip = $dhcp->getOptionValue(DHO_DHCP_REQUESTED_ADDRESS());
149                  warn "DHCP REQUEST $requested_ip";                  log::mac $mac, "DHCP REQUEST $requested_ip $ip $file";
150                  if ( $ip eq $requested_ip ) {                  if ( $ip eq $requested_ip ) {
151                          $packet->{DHO_DHCP_MESSAGE_TYPE()}      = DHCPACK();                          $packet->{DHO_DHCP_MESSAGE_TYPE()}      = DHCPACK();
152                          $packet->{DHO_DHCP_LEASE_TIME()}        = 5 * 60; # 5 min                          $packet->{DHO_DHCP_LEASE_TIME()}        = 5 * 60; # 5 min
# Line 145  sub process_packet { Line 161  sub process_packet {
161                          $packet->{DHO_DHCP_MESSAGE()} = "Bad request, expected $ip";                          $packet->{DHO_DHCP_MESSAGE()} = "Bad request, expected $ip";
162                  }                  }
163          } elsif ($messagetype eq DHCPINFORM()) {          } elsif ($messagetype eq DHCPINFORM()) {
164                  warn "DHCP INFORM ignored";                  log::mac $mac, "DHCP INFORM ignored";
165          } else {          } else {
166                  warn "$messagetype igored (bootp?)";                  log::mac $mac, "$messagetype igored (bootp?)";
167          }          }
168    
169          warn ">> $mac == $ip server: $server::ip", $pxe::file ? " pxe file: $pxe::file\n" : "\n";          warn ">> $mac == $ip server: $server::ip", $file ? " file: $file\n" : "\n" if $debug;
170    
171    warn "## created packet ", dump( $packet );
172    
173          $packet = new Net::DHCP::Packet( %$packet );          $packet = new Net::DHCP::Packet( %$packet );
174          warn "## ",$packet->toString(),"\n" if $debug;          warn "send ",$packet->toString() if $debug;
175    
176          my $reply = IO::Socket::INET->new(          my $reply = IO::Socket::INET->new(
177                  LocalAddr => $server::ip,                  LocalAddr => $server::ip,
# Line 189  sub start { Line 207  sub start {
207          print "DHCP listen on ",$sock->sockhost,":",$sock->sockport,"\n";          print "DHCP listen on ",$sock->sockhost,":",$sock->sockport,"\n";
208    
209          while (1) {          while (1) {
                 Module::Refresh->refresh;  
210                  process_packet $sock;                  process_packet $sock;
211          }          }
212  }  }

Legend:
Removed from v.45  
changed lines
  Added in v.129

  ViewVC Help
Powered by ViewVC 1.1.26