/[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 232 by dpavlin, Sun Aug 16 22:51:57 2009 UTC revision 313 by dpavlin, Thu Aug 27 18:59:12 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 50  sub client_ip { Line 55  sub client_ip {
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                  return $ip;                  return $ip;
58          } else {          } elsif ( in_our_range( $request_ip ) ) {
59                  $ip = client::next_ip( $mac );                  $ip = client::next_ip( $mac );
60                  print "NEW $mac $ip\n";                  print "NEW $mac $ip\n";
61            } else {
62                    $ip = $request_ip;
63                    client::save_ip_mac( $ip, $mac );
64                    warn "W: $ip our of server range $server::ip $server::netmask\n";
65          }          }
66    
67          return $ip;          return $ip;
# Line 66  use client; Line 75  use client;
75  our $file;  our $file;
76  our $transaction = 0; # FIXME predictible transaction numbers  our $transaction = 0; # FIXME predictible transaction numbers
77    
78    sub ip2bin { pack('C*', split(/\./, $_[0])) };
79    sub in_our_range {
80            my $ip = shift;
81            return 1 if $ip eq '0.0.0.0';
82            return 1 if (
83                    ( ip2bin($ip)         & ip2bin($server::netmask) )
84                    eq
85                    ( ip2bin($server::ip) & ip2bin($server::netmask) )
86            );
87    }
88    
89  sub process_packet {  sub process_packet {
90          my $sock = shift;          my $sock = shift;
91    
# Line 80  sub process_packet { Line 100  sub process_packet {
100          return unless $buf;          return unless $buf;
101    
102          my $dhcp = Net::DHCP::Packet->new($buf);          my $dhcp = Net::DHCP::Packet->new($buf);
         $dhcp->comment( $transaction++ );  
103    
104          warn "recv: ", $dhcp->toString if $debug;          warn "recv: ", $dhcp->toString if $debug;
105    
106          my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2);          $dhcp->comment( $transaction++ );
107          my $ip = client_ip($mac);  
108            my $mac = format::mac( substr($dhcp->chaddr(),0,$dhcp->hlen()*2) );
109            my $ip = client_mac_ip($mac, $dhcp->ciaddr);
110    
111          my $hostname = $dhcp->getOptionValue(DHO_HOST_NAME);          my $hostname = $dhcp->getOptionValue(DHO_HOST_NAME);
112          print "$ip ", client::conf( $ip => 'hostname', default => $hostname ), " >> /etc/hosts\n";          print "$ip ", client::conf( $ip => 'hostname', default => $hostname ), " >> /etc/hosts\n" if $hostname;
113    
114          my $audit = { mac => format::mac($mac), ip => $ip, hostname => $hostname };          my $audit = { mac => $mac, ip => $ip, hostname => $hostname,
115                    options => {
116                            map {
117                                    ( $_ => $dhcp->getOptionValue( $_ ) )
118                            } @{ $dhcp->{options_order} }
119                    },
120            };
121    
122  =for later  =for later
123    
# Line 186  sub process_packet { Line 213  sub process_packet {
213          $packet = new Net::DHCP::Packet( %$packet );          $packet = new Net::DHCP::Packet( %$packet );
214          warn "send ",$packet->toString() if $debug;          warn "send ",$packet->toString() if $debug;
215    
216          CouchDB::audit( $audit->{type}, $audit );          if ( in_our_range( $ip ) ) {
217                    my $buff = $packet->serialize();
218    
219          my $reply = IO::Socket::INET->new(                  my $reply = IO::Socket::INET->new(
220                  LocalAddr => $server::ip,                          LocalAddr => $server::ip,
221                  LocalPort => 67,                          LocalPort => 67,
222                  Proto => "udp",                          Proto => "udp",
223                  Broadcast => 1,                          Broadcast => 1,
224                  PeerAddr => '255.255.255.255',                          PeerAddr => '255.255.255.255',
225                  PeerPort => 68,                          PeerPort => 68,
226                  Reuse => 1,                          Reuse => 1,
227          ) or die "socket: $@";                  ) or die "socket: $@";
228    
229          my $buff = $packet->serialize();                  $reply->send( $buff, 0 ) or die "Error sending: $!\n";
230          $reply->send( $buff, 0 ) or die "Error sending: $!\n";          } else {
231                    $audit->{error} = "$ip our of our range $server::ip $server::netmask";
232            }
233    
234            CouchDB::audit( $audit->{type}, $audit );
235    
236  #       system("arp -s $ip $mac"),  #       system("arp -s $ip $mac"),
237    

Legend:
Removed from v.232  
changed lines
  Added in v.313

  ViewVC Help
Powered by ViewVC 1.1.26