/[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 110 by dpavlin, Sun Aug 2 02:04:00 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  use pxe;  my $debug = server::debug;
   
 my $debug = 1;  
30    
31  if ( ! $server::ip ) {  if ( ! $server::ip ) {
32          my $server_ip = `/sbin/ifconfig`;          my $server_ip = `/sbin/ifconfig`;
# Line 36  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 76  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 86  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);
95          $dhcp->comment( $transaction++ );          $dhcp->comment( $transaction++ );
96    
97          warn "recv: ", $dhcp->toString;          warn "recv: ", $dhcp->toString if $debug;
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);
# Line 109  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 119  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    
129          my $messagetype = $dhcp->getOptionValue(DHO_DHCP_MESSAGE_TYPE());          my $messagetype = $dhcp->getOptionValue(DHO_DHCP_MESSAGE_TYPE());
130    
131          if ($messagetype eq DHCPDISCOVER()) {          if ($messagetype eq DHCPDISCOVER()) {
132                  warn "DHCP DISCOVER";                  log::mac $mac, "DHCP DISCOVER";
133                  $packet->{Comment} = $dhcp->comment();                  $packet->{Comment} = $dhcp->comment();
134                  $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPOFFER();                  $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPOFFER();
135          } elsif ($messagetype eq DHCPREQUEST()) {          } elsif ($messagetype eq DHCPREQUEST()) {
136                  my $requested_ip = $dhcp->getOptionValue(DHO_DHCP_REQUESTED_ADDRESS());                  my $requested_ip = $dhcp->getOptionValue(DHO_DHCP_REQUESTED_ADDRESS());
137                  warn "DHCP REQUEST $requested_ip";                  log::mac $mac, "DHCP REQUEST $requested_ip $ip $file";
138                  if ( $ip eq $requested_ip ) {                  if ( $ip eq $requested_ip ) {
139                          $packet->{DHO_DHCP_MESSAGE_TYPE()}      = DHCPACK();                          $packet->{DHO_DHCP_MESSAGE_TYPE()}      = DHCPACK();
140                          $packet->{DHO_DHCP_LEASE_TIME()}        = 5 * 60; # 5 min                          $packet->{DHO_DHCP_LEASE_TIME()}        = 5 * 60; # 5 min
# Line 145  sub process_packet { Line 149  sub process_packet {
149                          $packet->{DHO_DHCP_MESSAGE()} = "Bad request, expected $ip";                          $packet->{DHO_DHCP_MESSAGE()} = "Bad request, expected $ip";
150                  }                  }
151          } elsif ($messagetype eq DHCPINFORM()) {          } elsif ($messagetype eq DHCPINFORM()) {
152                  warn "DHCP INFORM ignored";                  log::mac $mac, "DHCP INFORM ignored";
153          } else {          } else {
154                  warn "$messagetype igored (bootp?)";                  log::mac $mac, "$messagetype igored (bootp?)";
155          }          }
156    
157          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;
158    
159          $packet = new Net::DHCP::Packet( %$packet );          $packet = new Net::DHCP::Packet( %$packet );
160          warn "## ",$packet->toString(),"\n" if $debug;          warn "send ",$packet->toString() if $debug;
161    
162          my $reply = IO::Socket::INET->new(          my $reply = IO::Socket::INET->new(
163                  LocalAddr => $server::ip,                  LocalAddr => $server::ip,

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

  ViewVC Help
Powered by ViewVC 1.1.26