/[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 208 by dpavlin, Wed Aug 12 23:59:01 2009 UTC revision 424 by dpavlin, Sun Sep 13 09:10:46 2009 UTC
# Line 39  warn "server ip $server::ip range: $serv Line 39  warn "server ip $server::ip range: $serv
39    
40  use client;  use client;
41    
42  sub client_ip {  sub client_mac_ip {
43          my ( $mac ) = @_;          my ( $mac, $request_ip ) = @_;
44    
45            if ( ! $mac ) {
46                    warn "W: no mac in requiest\n";
47                    return;
48            }
49    
50          my $conf = $server::conf;          my $conf = $server::conf;
51          mkdir $conf unless -e $conf;          mkdir $conf unless -e $conf;
# Line 49  sub client_ip { Line 54  sub client_ip {
54    
55          if ( $ip = client::ip_from_mac( $mac ) ) {          if ( $ip = client::ip_from_mac( $mac ) ) {
56                  print "RENEW $mac $ip\n";                  print "RENEW $mac $ip\n";
57                    client::save_ip_mac( $ip, $mac );
58                  return $ip;                  return $ip;
59          } else {          } elsif ( ip::in_dhcp_range( $request_ip ) || $request_ip eq '0.0.0.0' ) {
60                  $ip = client::next_ip( $mac );                  $ip = client::next_ip( $mac );
61                  print "NEW $mac $ip\n";                  print "NEW $mac $ip\n";
62            } else {
63                    $ip = $request_ip;
64                    client::save_ip_mac( $ip, $mac );
65                    warn "W: $ip our of server range $server::ip $server::netmask\n";
66          }          }
67    
68          return $ip;          return $ip;
# Line 80  sub process_packet { Line 90  sub process_packet {
90          return unless $buf;          return unless $buf;
91    
92          my $dhcp = Net::DHCP::Packet->new($buf);          my $dhcp = Net::DHCP::Packet->new($buf);
         $dhcp->comment( $transaction++ );  
93    
94          warn "recv: ", $dhcp->toString if $debug;          warn "recv: ", $dhcp->toString if $debug;
95    
96          my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2);          $dhcp->comment( $transaction++ );
97          my $ip = client_ip($mac);  
98            my $mac = format::mac( substr($dhcp->chaddr(),0,$dhcp->hlen()*2) );
99            my $ip = client_mac_ip($mac, $dhcp->ciaddr);
100    
101          my $hostname = $dhcp->getOptionValue(DHO_HOST_NAME);          my $hostname = $dhcp->getOptionValue(DHO_HOST_NAME);
102          print "$ip ", client::conf( $ip => 'hostname', default => $hostname ), " >> /etc/hosts\n";          print "$ip ", client::conf( $ip => 'hostname', default => $hostname ), " >> /etc/hosts\n" if $hostname;
103    
104          my $audit = { mac => format::mac($mac), ip => $ip, hostname => $hostname };          my $audit = { mac => $mac, ip => $ip, hostname => $hostname,
105                    options => {
106                            map {
107                                    ( $_ => $dhcp->getOptionValue( $_ ) )
108                            } @{ $dhcp->{options_order} }
109                    },
110            };
111    
112  =for later  =for later
113    
# Line 123  sub process_packet { Line 140  sub process_packet {
140          my $options = {          my $options = {
141                  DHO_SUBNET_MASK()       => $server::netmask,                  DHO_SUBNET_MASK()       => $server::netmask,
142                  DHO_ROUTERS()           => $server::ip,                  DHO_ROUTERS()           => $server::ip,
143                  DHO_DOMAIN_NAME()       => $server::domain_name,                  DHO_DOMAIN_NAME()       => $server::domain,
144                  DHO_NAME_SERVERS()      => $server::ip,                  DHO_NAME_SERVERS()      => $server::ip,
145                  DHO_DOMAIN_NAME_SERVERS() => $server::ip,                  DHO_DOMAIN_NAME_SERVERS() => $server::ip,
146                  DHO_HOST_NAME()         => client::conf( $ip, 'hostname' ),                  DHO_HOST_NAME()         => client::conf( $ip, 'hostname' ),
# Line 159  sub process_packet { Line 176  sub process_packet {
176    
177          my $messagetype = $dhcp->getOptionValue(DHO_DHCP_MESSAGE_TYPE());          my $messagetype = $dhcp->getOptionValue(DHO_DHCP_MESSAGE_TYPE());
178    
179            my @type;
180    
181          if ($messagetype eq DHCPDISCOVER()) {          if ($messagetype eq DHCPDISCOVER()) {
                 $audit->{type} = 'discover';  
182                  $packet->{Comment} = $dhcp->comment();                  $packet->{Comment} = $dhcp->comment();
183                  $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPOFFER();                  $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPOFFER();
184                    @type = qw( discover offer );
185          } elsif ($messagetype eq DHCPREQUEST()) {          } elsif ($messagetype eq DHCPREQUEST()) {
186                  my $requested_ip = $dhcp->getOptionValue(DHO_DHCP_REQUESTED_ADDRESS());                  @type = qw( request );
187                  $audit->{type} = 'request';                  my $requested_ip = $dhcp->getOptionValue(DHO_DHCP_REQUESTED_ADDRESS()) || $dhcp->ciaddr();
188                  if ( $ip eq $requested_ip ) {                  if ( $ip eq $requested_ip ) {
189                          $packet->{DHO_DHCP_MESSAGE_TYPE()}      = DHCPACK();                          $packet->{DHO_DHCP_MESSAGE_TYPE()}      = DHCPACK();
190                          $packet->{DHO_DHCP_LEASE_TIME()}        = 5 * 60; # 5 min                          $packet->{DHO_DHCP_LEASE_TIME()}        = 5 * 60; # 5 min
191  #                       $packet->{DHO_ROOT_PATH()}              = '/exports/foobar';  #                       $packet->{DHO_ROOT_PATH()}              = '/exports/foobar';
192                            $type[1] = 'ack';
193                  } else {                  } else {
194                          $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPNAK();                          $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPNAK();
195                          $packet->{DHO_DHCP_MESSAGE()} = "Bad request, expected $ip";                          $packet->{DHO_DHCP_MESSAGE()} = "Bad request, expected $ip got $requested_ip";
196                            $type[1] = 'nak';
197                  }                  }
198          } elsif ($messagetype eq DHCPINFORM()) {          } elsif ($messagetype eq DHCPINFORM()) {
199                  $audit->{type} = 'inform';                  @type = qw( inform ignored );
200          } else {          } else {
201                  $audit->{type} = sprintf('ignored %x', $messagetype);                  @type = ( $messagetype, 'ignored' );
202          }          }
203    
204            warn "# type ",dump @type;
205            $audit->{type} = [ @type ];
206    
207          warn ">> $mac == $ip server: $server::ip", $file ? " file: $file\n" : "\n" if $debug;          warn ">> $mac == $ip server: $server::ip", $file ? " file: $file\n" : "\n" if $debug;
208          $audit->{response} = $packet;          $audit->{response} = $packet;
209    
210          $packet = new Net::DHCP::Packet( %$packet );          $packet = new Net::DHCP::Packet( %$packet );
211          warn "send ",$packet->toString() if $debug;          warn "send ",$packet->toString() if $debug;
212    
213          CouchDB::audit( $audit->{type}, $audit );          if ( ip::in_dhcp_range( $ip ) ) {
214                    my $buff = $packet->serialize();
215    
216          my $reply = IO::Socket::INET->new(                  my $reply = IO::Socket::INET->new(
217                  LocalAddr => $server::ip,                          LocalAddr => $server::ip,
218                  LocalPort => 67,                          LocalPort => 67,
219                  Proto => "udp",                          Proto => "udp",
220                  Broadcast => 1,                          Broadcast => 1,
221                  PeerAddr => '255.255.255.255',                          PeerAddr => '255.255.255.255',
222                  PeerPort => 68,                          PeerPort => 68,
223                  Reuse => 1,                          Reuse => 1,
224          ) or die "socket: $@";                  ) or die "socket: $@";
225    
226                    $reply->send( $buff, 0 ) or die "Error sending: $!\n";
227            } else {
228                    $audit->{error} = "$ip our of our range $server::ip $server::netmask";
229                    warn $audit->{error};
230            }
231    
232          my $buff = $packet->serialize();          CouchDB::audit( @type, $audit );
         $reply->send( $buff, 0 ) or die "Error sending: $!\n";  
233    
234  #       system("arp -s $ip $mac"),  #       system("arp -s $ip $mac"),
235    
# Line 221  sub start { Line 251  sub start {
251    
252          print "DHCP listen on ",$sock->sockhost,":",$sock->sockport,"\n";          print "DHCP listen on ",$sock->sockhost,":",$sock->sockport,"\n";
253    
254          CouchDB::audit( 'start', { 'listen' => { addr => $sock->sockhost, port => $sock->sockport } } );          CouchDB::audit( 'start', { addr => $sock->sockhost, port => $sock->sockport } );
255    
256          while (1) {          while (1) {
257                  process_packet $sock;                  process_packet $sock;

Legend:
Removed from v.208  
changed lines
  Added in v.424

  ViewVC Help
Powered by ViewVC 1.1.26