/[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 260 by dpavlin, Wed Aug 19 10:56:04 2009 UTC revision 456 by dpavlin, Sat Jan 2 15:49:09 2010 UTC
# Line 42  use client; Line 42  use client;
42  sub client_mac_ip {  sub client_mac_ip {
43          my ( $mac, $request_ip ) = @_;          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;
52    
# Line 49  sub client_mac_ip { Line 54  sub client_mac_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          } elsif ( in_our_range( $request_ip ) ) {          } 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 {          } else {
63                  $ip = $request_ip;                  $ip = $request_ip;
64                  client::save_ip_mac( $ip, $mac );                  client::save_ip_mac( $ip, $mac );
65                  warn "W: $ip our of server range $server::ip $server::netmask\n";                  warn "W: $ip out of server range $server::ip/$server::netmask\n";
66          }          }
67    
68          return $ip;          return $ip;
# Line 70  use client; Line 76  use client;
76  our $file;  our $file;
77  our $transaction = 0; # FIXME predictible transaction numbers  our $transaction = 0; # FIXME predictible transaction numbers
78    
 sub ip2bin { pack('C*', split(/\./, $_[0])) };  
 sub in_our_range {  
         my $ip = shift;  
         return 1 if $ip eq '0.0.0.0';  
         return 1 if (  
                 ( ip2bin($ip)         & ip2bin($server::netmask) )  
                 eq  
                 ( ip2bin($server::ip) & ip2bin($server::netmask) )  
         );  
 }  
   
79  sub process_packet {  sub process_packet {
80          my $sock = shift;          my $sock = shift;
81    
         server->refresh;  
   
82          my $buf;          my $buf;
83          $sock->recv($buf, 1024);          $sock->recv($buf, 1024);
84          my $size = 'empty';          my $size = 'empty';
# Line 100  sub process_packet { Line 93  sub process_packet {
93    
94          $dhcp->comment( $transaction++ );          $dhcp->comment( $transaction++ );
95    
96          my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2);          my $mac = format::mac( substr($dhcp->chaddr(),0,$dhcp->hlen()*2) );
97          my $ip = client_mac_ip($mac, $dhcp->ciaddr);          my $ip = client_mac_ip($mac, $dhcp->ciaddr);
98    
99          my $hostname = $dhcp->getOptionValue(DHO_HOST_NAME);          my $hostname = $dhcp->getOptionValue(DHO_HOST_NAME);
100          print "$ip ", client::conf( $ip => 'hostname', default => $hostname ), " >> /etc/hosts\n";          print "$ip ", client::conf( $ip => 'hostname', default => $hostname ), " >> /etc/hosts\n" if $hostname;
101    
102          my $audit = { mac => format::mac($mac), ip => $ip, hostname => $hostname,          my $audit = { mac => $mac, ip => $ip, hostname => $hostname,
103                  options => {                  options => {
104                          map {                          map {
105                                  ( $_ => $dhcp->getOptionValue( $_ ) )                                  ( $_ => $dhcp->getOptionValue( $_ ) )
# Line 128  sub process_packet { Line 121  sub process_packet {
121    
122          config::for_ip( $ip );          config::for_ip( $ip );
123    
124            my $server;
125            map { $server->{ $_ } = eval '$server::' . $_ } ( 'ip', 'netmask', 'bcast', 'domain' );
126    
127            if ( my $force = client::conf( $ip => 'dhcpd.pl' ) ) {
128                    eval $force;
129                    die "$force\n$@" if $@;
130                    warn "force server ", dump $server;
131            }
132    
133          my $packet = {          my $packet = {
134                  Op              => BOOTREPLY(),                  Op              => BOOTREPLY(),
135                  Hops    => $dhcp->hops(),                  Hops    => $dhcp->hops(),
# Line 135  sub process_packet { Line 137  sub process_packet {
137                  Flags   => $dhcp->flags(),                  Flags   => $dhcp->flags(),
138                  Ciaddr  => $dhcp->ciaddr(),                  Ciaddr  => $dhcp->ciaddr(),
139                  Yiaddr  => $ip,                  Yiaddr  => $ip,
140                  Siaddr  => $server::ip,                  Siaddr  => $server->{ip},
141                  Giaddr  => $dhcp->giaddr(),                  Giaddr  => $dhcp->giaddr(),
142                  Chaddr  => $dhcp->chaddr(),                  Chaddr  => $dhcp->chaddr(),
143                  File    => $file,                  File    => $file,
144                  DHO_DHCP_SERVER_IDENTIFIER()    => $server::ip, # busybox/udhcpc needs it but doesn't request                  DHO_DHCP_SERVER_IDENTIFIER()    => $server->{ip},       # busybox/udhcpc needs it but doesn't request
145          };          };
146    
147          my $options = {          my $options = {
148                  DHO_SUBNET_MASK()       => $server::netmask,                  DHO_SUBNET_MASK()       => $server->{netmask},
149                  DHO_ROUTERS()           => $server::ip,                  DHO_ROUTERS()           => $server->{ip},
150                  DHO_DOMAIN_NAME()       => $server::domain_name,                  DHO_DOMAIN_NAME()       => $server->{domain},
151                  DHO_NAME_SERVERS()      => $server::ip,                  DHO_NAME_SERVERS()      => $server->{ip},
152                  DHO_DOMAIN_NAME_SERVERS() => $server::ip,                  DHO_DOMAIN_NAME_SERVERS() => $server->{ip},
153                  DHO_HOST_NAME()         => client::conf( $ip, 'hostname' ),                  DHO_HOST_NAME()         => client::conf( $ip, 'hostname' ),
154                  DHO_BROADCAST_ADDRESS() => $server::bcast,                  DHO_BROADCAST_ADDRESS() => $server->{bcast},
155  #               DHO_NTP_SERVERS() => '',  #               DHO_NTP_SERVERS() => '',
156          };          };
157    
# Line 181  sub process_packet { Line 183  sub process_packet {
183    
184          my $messagetype = $dhcp->getOptionValue(DHO_DHCP_MESSAGE_TYPE());          my $messagetype = $dhcp->getOptionValue(DHO_DHCP_MESSAGE_TYPE());
185    
186            my @type;
187    
188          if ($messagetype eq DHCPDISCOVER()) {          if ($messagetype eq DHCPDISCOVER()) {
                 $audit->{type} = 'discover';  
189                  $packet->{Comment} = $dhcp->comment();                  $packet->{Comment} = $dhcp->comment();
190                  $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPOFFER();                  $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPOFFER();
191                    @type = qw( discover offer );
192          } elsif ($messagetype eq DHCPREQUEST()) {          } elsif ($messagetype eq DHCPREQUEST()) {
193                  my $requested_ip = $dhcp->getOptionValue(DHO_DHCP_REQUESTED_ADDRESS());                  @type = qw( request );
194                  $audit->{type} = 'request';                  my $requested_ip = $dhcp->getOptionValue(DHO_DHCP_REQUESTED_ADDRESS()) || $dhcp->ciaddr();
195                  if ( $ip eq $requested_ip ) {                  if ( $ip eq $requested_ip ) {
196                          $packet->{DHO_DHCP_MESSAGE_TYPE()}      = DHCPACK();                          $packet->{DHO_DHCP_MESSAGE_TYPE()}      = DHCPACK();
197                          $packet->{DHO_DHCP_LEASE_TIME()}        = 5 * 60; # 5 min                          $packet->{DHO_DHCP_LEASE_TIME()}        = 5 * 60; # 5 min
198  #                       $packet->{DHO_ROOT_PATH()}              = '/exports/foobar';  #                       $packet->{DHO_ROOT_PATH()}              = '/exports/foobar';
199                            $type[1] = 'ack';
200                  } else {                  } else {
201                          $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPNAK();                          $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPNAK();
202                          $packet->{DHO_DHCP_MESSAGE()} = "Bad request, expected $ip";                          $packet->{DHO_DHCP_MESSAGE()} = "Bad request, expected $ip got $requested_ip";
203                            $type[1] = 'nak';
204                  }                  }
205          } elsif ($messagetype eq DHCPINFORM()) {          } elsif ($messagetype eq DHCPINFORM()) {
206                  $audit->{type} = 'inform';                  @type = qw( inform ignored );
207          } else {          } else {
208                  $audit->{type} = sprintf('ignored %x', $messagetype);                  @type = ( $messagetype, 'ignored' );
209          }          }
210    
211          warn ">> $mac == $ip server: $server::ip", $file ? " file: $file\n" : "\n" if $debug;          warn "# type ",dump @type;
212            $audit->{type} = [ @type ];
213    
214          $audit->{response} = $packet;          $audit->{response} = $packet;
215    
216          $packet = new Net::DHCP::Packet( %$packet );          $packet = new Net::DHCP::Packet( %$packet );
217          warn "send ",$packet->toString() if $debug;          warn "send ",$packet->toString() if $debug;
218    
219          if ( in_our_range( $ip ) ) {          if ( ip::in_dhcp_range( $ip ) ) {
220                  my $buff = $packet->serialize();                  my $buff = $packet->serialize();
221    
222                  my $reply = IO::Socket::INET->new(                  my $reply = IO::Socket::INET->new(
223                          LocalAddr => $server::ip,                          LocalAddr => $server->{ip},
224                          LocalPort => 67,                          LocalPort => 67,
225                          Proto => "udp",                          Proto => "udp",
226                          Broadcast => 1,                          Broadcast => 1,
227                          PeerAddr => '255.255.255.255',  #                       PeerAddr => '255.255.255.255',
228                            PeerAddr => $server->{bcast},
229                          PeerPort => 68,                          PeerPort => 68,
230                          Reuse => 1,                          Reuse => 1,
231                  ) or die "socket: $@";                  ) or die "socket: $@";
232    
233                  $reply->send( $buff, 0 ) or die "Error sending: $!\n";                  $reply->send( $buff, 0 ) or die "Error sending: $!\n";
234                    warn ">> $mac == $ip server: $server->{ip}", $file ? " file: $file\n" : "\n";
235          } else {          } else {
236                  $audit->{error} = "$ip our of our range $server::ip $server::netmask";                  $audit->{error} = "$ip not in server range $server::ip $server::netmask - no packet sent";
237                    warn $audit->{error};
238          }          }
239    
240          CouchDB::audit( $audit->{type}, $audit );          CouchDB::audit( @type, $audit );
241    
242  #       system("arp -s $ip $mac"),  #       system("arp -s $ip $mac"),
243    
# Line 251  sub start { Line 262  sub start {
262          CouchDB::audit( 'start', { addr => $sock->sockhost, port => $sock->sockport } );          CouchDB::audit( 'start', { addr => $sock->sockhost, port => $sock->sockport } );
263    
264          while (1) {          while (1) {
265                    server->refresh;
266                  process_packet $sock;                  process_packet $sock;
267          }          }
268  }  }

Legend:
Removed from v.260  
changed lines
  Added in v.456

  ViewVC Help
Powered by ViewVC 1.1.26