/[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 44 by dpavlin, Wed Jul 29 20:55:44 2009 UTC revision 69 by dpavlin, Thu Jul 30 22:44:21 2009 UTC
# Line 1  Line 1 
1  #!/usr/bin/perl  package dhcpd;
2    
3  =head1 dhcpd  =head1 dhcpd
4    
# Line 26  use Net::DHCP::Packet; Line 26  use Net::DHCP::Packet;
26  use Net::DHCP::Constants 0.67;  use Net::DHCP::Constants 0.67;
27    
28  use server;  use server;
29    my $debug = server::debug;
30  my $debug = 1;  use pxe;
   
 our ( $file, $gpxe_file );  
 our ( $ip_from, $ip_to ) = ( 10, 100 );  
31    
32  if ( ! $server::ip ) {  if ( ! $server::ip ) {
33          my $server_ip = `/sbin/ifconfig`;          my $server_ip = `/sbin/ifconfig`;
# Line 38  if ( ! $server::ip ) { Line 35  if ( ! $server::ip ) {
35          $server::ip = $server_ip;          $server::ip = $server_ip;
36  }  }
37    
38  warn "server ip $server::ip\n";  warn "server ip $server::ip file: $pxe::file range: $server::ip_from - $server::ip_to\n";
39    
40  my $addr = $ip_from;  my $addr = $server::ip_from;
41    
42  sub client_ip {  sub client_ip {
43          my ( $mac ) = @_;          my ( $mac ) = @_;
44    
45          my $conf = "conf/$server::ip";          my $conf = "$server::base_dir/conf/$server::ip";
46          mkdir $conf unless -e $conf;          mkdir $conf unless -e $conf;
47    
48          if ( -e "$conf/mac/$mac" ) {          if ( -e "$conf/mac/$mac" ) {
# Line 63  sub client_ip { Line 60  sub client_ip {
60          my $ip = $prefix . $addr;          my $ip = $prefix . $addr;
61          while ( -e "conf/ip/$ip" || $p->ping( $ip ) ) {          while ( -e "conf/ip/$ip" || $p->ping( $ip ) ) {
62                  $ip = $prefix . $addr++;                  $ip = $prefix . $addr++;
63                  die "all addresses allocated!" if $addr == $ip_to;                  die "all addresses allocated!" if $addr == $server::ip_to;
64          }          }
65    
66          write_file "$conf/mac/$mac", $ip;          write_file "$conf/mac/$mac", $ip;
# Line 94  sub process_packet { Line 91  sub process_packet {
91          my $dhcp = Net::DHCP::Packet->new($buf);          my $dhcp = Net::DHCP::Packet->new($buf);
92          $dhcp->comment( $transaction++ );          $dhcp->comment( $transaction++ );
93    
94          warn "recv: ", $dhcp->toString;          warn "recv: ", $dhcp->toString if $debug;
95    
96          my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2);          my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2);
97          my $ip = client_ip($mac);          my $ip = client_ip($mac);
# Line 121  sub process_packet { Line 118  sub process_packet {
118                  Siaddr  => $server::ip,                  Siaddr  => $server::ip,
119                  Giaddr  => $dhcp->giaddr(),                  Giaddr  => $dhcp->giaddr(),
120                  Chaddr  => $dhcp->chaddr(),                  Chaddr  => $dhcp->chaddr(),
121                  File    => $file,                  File    => $pxe::file,
122          };          };
123    
124            pxe::config_for_ip( $ip );
125    
126          my $messagetype = $dhcp->getOptionValue(DHO_DHCP_MESSAGE_TYPE());          my $messagetype = $dhcp->getOptionValue(DHO_DHCP_MESSAGE_TYPE());
127    
128          if ($messagetype eq DHCPDISCOVER()) {          if ($messagetype eq DHCPDISCOVER()) {
# Line 152  sub process_packet { Line 151  sub process_packet {
151                  warn "$messagetype igored (bootp?)";                  warn "$messagetype igored (bootp?)";
152          }          }
153    
154          warn ">> $mac == $ip server: $server::ip", $file ? " file: $file\n" : "\n";          warn ">> $mac == $ip server: $server::ip", $pxe::file ? " pxe file: $pxe::file\n" : "\n";
155    
156          $packet = new Net::DHCP::Packet( %$packet );          $packet = new Net::DHCP::Packet( %$packet );
157          warn "## ",$packet->toString(),"\n" if $debug;          warn "send ",$packet->toString() if $debug;
158    
159          my $reply = IO::Socket::INET->new(          my $reply = IO::Socket::INET->new(
160                  LocalAddr => $server::ip,                  LocalAddr => $server::ip,
# Line 196  sub start { Line 195  sub start {
195          }          }
196  }  }
197    
198    warn "loaded";
199    
200  1;  1;

Legend:
Removed from v.44  
changed lines
  Added in v.69

  ViewVC Help
Powered by ViewVC 1.1.26