/[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 67 by dpavlin, Thu Jul 30 21:31:30 2009 UTC revision 114 by dpavlin, Sun Aug 2 03:31:39 2009 UTC
# Line 27  use Net::DHCP::Constants 0.67; Line 27  use Net::DHCP::Constants 0.67;
27    
28  use server;  use server;
29  my $debug = server::debug;  my $debug = server::debug;
 use pxe;  
30    
31  if ( ! $server::ip ) {  if ( ! $server::ip ) {
32          my $server_ip = `/sbin/ifconfig`;          my $server_ip = `/sbin/ifconfig`;
# Line 35  if ( ! $server::ip ) { Line 34  if ( ! $server::ip ) {
34          $server::ip = $server_ip;          $server::ip = $server_ip;
35  }  }
36    
37  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";
38    
39  my $addr = $server::ip_from;  my $addr = $server::ip_from;
40    
41  sub client_ip {  sub client_ip {
42          my ( $mac ) = @_;          my ( $mac ) = @_;
43    
44          my $conf = "conf/$server::ip";          my $conf = "$server::base_dir/conf/$server::ip";
45          mkdir $conf unless -e $conf;          mkdir $conf unless -e $conf;
46    
47          if ( -e "$conf/mac/$mac" ) {          if ( -e "$conf/mac/$mac" ) {
48                  my $ip = read_file "$conf/mac/$mac";                  my $ip = read_file "$conf/mac/$mac";
49                  print "$mac old $ip\n";                  print "RENEW $mac $ip\n";
50                  return $ip;                  return $ip;
51          }          }
52    
# Line 75  sub client_ip { Line 74  sub client_ip {
74          return $ip;          return $ip;
75  }  }
76    
77    use log;
78    use config;
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 {
# Line 85  sub process_packet { Line 88  sub process_packet {
88          my $size = 'empty';          my $size = 'empty';
89          $size = length($buf) . ' bytes' if defined $buf;          $size = length($buf) . ' bytes' if defined $buf;
90    
91          print "packet from ",$sock->peerhost,":",$sock->peerport," $size\n";          print "packet from ",$sock->peerhost,":",$sock->peerport," $size\n" if $debug;
92          return unless $buf;          return unless $buf;
93    
94          my $dhcp = Net::DHCP::Packet->new($buf);          my $dhcp = Net::DHCP::Packet->new($buf);
# Line 108  sub process_packet { Line 111  sub process_packet {
111    
112  =cut  =cut
113    
114            config::for_ip( $ip );
115    
116          my $packet = {          my $packet = {
117                  Op              => BOOTREPLY(),                  Op              => BOOTREPLY(),
118                  Hops    => $dhcp->hops(),                  Hops    => $dhcp->hops(),
# Line 118  sub process_packet { Line 123  sub process_packet {
123                  Siaddr  => $server::ip,                  Siaddr  => $server::ip,
124                  Giaddr  => $dhcp->giaddr(),                  Giaddr  => $dhcp->giaddr(),
125                  Chaddr  => $dhcp->chaddr(),                  Chaddr  => $dhcp->chaddr(),
126                  File    => $pxe::file,                  File    => $file,
         };  
127    
128          pxe::config_for_ip( $ip );                  DHO_PXELINUX_MAGIC()       => 0xF100747E,
129                    DHO_PXELINUX_CONFIG_FILE() => 'pxelinux.cfg/default',
130                    DHO_PXELINUX_PATH_PREFIX() => 'live-helper/tftpboot/',
131                    DHO_PXELINUX_REBOOT_TIME() => 5, # default: 300 s
132            };
133    
134          my $messagetype = $dhcp->getOptionValue(DHO_DHCP_MESSAGE_TYPE());          my $messagetype = $dhcp->getOptionValue(DHO_DHCP_MESSAGE_TYPE());
135    
136          if ($messagetype eq DHCPDISCOVER()) {          if ($messagetype eq DHCPDISCOVER()) {
137                  warn "DHCP DISCOVER";                  log::mac $mac, "DHCP DISCOVER";
138                  $packet->{Comment} = $dhcp->comment();                  $packet->{Comment} = $dhcp->comment();
139                  $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPOFFER();                  $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPOFFER();
140          } elsif ($messagetype eq DHCPREQUEST()) {          } elsif ($messagetype eq DHCPREQUEST()) {
141                  my $requested_ip = $dhcp->getOptionValue(DHO_DHCP_REQUESTED_ADDRESS());                  my $requested_ip = $dhcp->getOptionValue(DHO_DHCP_REQUESTED_ADDRESS());
142                  warn "DHCP REQUEST $requested_ip";                  log::mac $mac, "DHCP REQUEST $requested_ip $ip $file";
143                  if ( $ip eq $requested_ip ) {                  if ( $ip eq $requested_ip ) {
144                          $packet->{DHO_DHCP_MESSAGE_TYPE()}      = DHCPACK();                          $packet->{DHO_DHCP_MESSAGE_TYPE()}      = DHCPACK();
145                          $packet->{DHO_DHCP_LEASE_TIME()}        = 5 * 60; # 5 min                          $packet->{DHO_DHCP_LEASE_TIME()}        = 5 * 60; # 5 min
# Line 146  sub process_packet { Line 154  sub process_packet {
154                          $packet->{DHO_DHCP_MESSAGE()} = "Bad request, expected $ip";                          $packet->{DHO_DHCP_MESSAGE()} = "Bad request, expected $ip";
155                  }                  }
156          } elsif ($messagetype eq DHCPINFORM()) {          } elsif ($messagetype eq DHCPINFORM()) {
157                  warn "DHCP INFORM ignored";                  log::mac $mac, "DHCP INFORM ignored";
158          } else {          } else {
159                  warn "$messagetype igored (bootp?)";                  log::mac $mac, "$messagetype igored (bootp?)";
160          }          }
161    
162          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;
163    
164          $packet = new Net::DHCP::Packet( %$packet );          $packet = new Net::DHCP::Packet( %$packet );
165          warn "send ",$packet->toString() if $debug;          warn "send ",$packet->toString() if $debug;

Legend:
Removed from v.67  
changed lines
  Added in v.114

  ViewVC Help
Powered by ViewVC 1.1.26