/[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 260 by dpavlin, Wed Aug 19 10:56:04 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          my $conf = $server::conf;          my $conf = $server::conf;
46          mkdir $conf unless -e $conf;          mkdir $conf unless -e $conf;
# Line 50  sub client_ip { Line 50  sub client_ip {
50          if ( $ip = client::ip_from_mac( $mac ) ) {          if ( $ip = client::ip_from_mac( $mac ) ) {
51                  print "RENEW $mac $ip\n";                  print "RENEW $mac $ip\n";
52                  return $ip;                  return $ip;
53          } else {          } elsif ( in_our_range( $request_ip ) ) {
54                  $ip = client::next_ip( $mac );                  $ip = client::next_ip( $mac );
55                  print "NEW $mac $ip\n";                  print "NEW $mac $ip\n";
56            } else {
57                    $ip = $request_ip;
58                    client::save_ip_mac( $ip, $mac );
59                    warn "W: $ip our of server range $server::ip $server::netmask\n";
60          }          }
61    
62          return $ip;          return $ip;
# Line 66  use client; Line 70  use client;
70  our $file;  our $file;
71  our $transaction = 0; # FIXME predictible transaction numbers  our $transaction = 0; # FIXME predictible transaction numbers
72    
73    sub ip2bin { pack('C*', split(/\./, $_[0])) };
74    sub in_our_range {
75            my $ip = shift;
76            return 1 if $ip eq '0.0.0.0';
77            return 1 if (
78                    ( ip2bin($ip)         & ip2bin($server::netmask) )
79                    eq
80                    ( ip2bin($server::ip) & ip2bin($server::netmask) )
81            );
82    }
83    
84  sub process_packet {  sub process_packet {
85          my $sock = shift;          my $sock = shift;
86    
# Line 80  sub process_packet { Line 95  sub process_packet {
95          return unless $buf;          return unless $buf;
96    
97          my $dhcp = Net::DHCP::Packet->new($buf);          my $dhcp = Net::DHCP::Packet->new($buf);
         $dhcp->comment( $transaction++ );  
98    
99          warn "recv: ", $dhcp->toString if $debug;          warn "recv: ", $dhcp->toString if $debug;
100    
101            $dhcp->comment( $transaction++ );
102    
103          my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2);          my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2);
104          my $ip = client_ip($mac);          my $ip = client_mac_ip($mac, $dhcp->ciaddr);
105    
106          my $hostname = $dhcp->getOptionValue(DHO_HOST_NAME);          my $hostname = $dhcp->getOptionValue(DHO_HOST_NAME);
107          print "$ip ", client::conf( $ip => 'hostname', default => $hostname ), " >> /etc/hosts\n";          print "$ip ", client::conf( $ip => 'hostname', default => $hostname ), " >> /etc/hosts\n";
108    
109          my $audit = { mac => format::mac($mac), ip => $ip, hostname => $hostname };          my $audit = { mac => format::mac($mac), ip => $ip, hostname => $hostname,
110                    options => {
111                            map {
112                                    ( $_ => $dhcp->getOptionValue( $_ ) )
113                            } @{ $dhcp->{options_order} }
114                    },
115            };
116    
117  =for later  =for later
118    
# Line 186  sub process_packet { Line 208  sub process_packet {
208          $packet = new Net::DHCP::Packet( %$packet );          $packet = new Net::DHCP::Packet( %$packet );
209          warn "send ",$packet->toString() if $debug;          warn "send ",$packet->toString() if $debug;
210    
211          CouchDB::audit( $audit->{type}, $audit );          if ( in_our_range( $ip ) ) {
212                    my $buff = $packet->serialize();
213    
214                    my $reply = IO::Socket::INET->new(
215                            LocalAddr => $server::ip,
216                            LocalPort => 67,
217                            Proto => "udp",
218                            Broadcast => 1,
219                            PeerAddr => '255.255.255.255',
220                            PeerPort => 68,
221                            Reuse => 1,
222                    ) or die "socket: $@";
223    
224          my $reply = IO::Socket::INET->new(                  $reply->send( $buff, 0 ) or die "Error sending: $!\n";
225                  LocalAddr => $server::ip,          } else {
226                  LocalPort => 67,                  $audit->{error} = "$ip our of our range $server::ip $server::netmask";
227                  Proto => "udp",          }
                 Broadcast => 1,  
                 PeerAddr => '255.255.255.255',  
                 PeerPort => 68,  
                 Reuse => 1,  
         ) or die "socket: $@";  
228    
229          my $buff = $packet->serialize();          CouchDB::audit( $audit->{type}, $audit );
         $reply->send( $buff, 0 ) or die "Error sending: $!\n";  
230    
231  #       system("arp -s $ip $mac"),  #       system("arp -s $ip $mac"),
232    
# Line 221  sub start { Line 248  sub start {
248    
249          print "DHCP listen on ",$sock->sockhost,":",$sock->sockport,"\n";          print "DHCP listen on ",$sock->sockhost,":",$sock->sockport,"\n";
250    
251          CouchDB::audit( 'start', { 'listen' => { addr => $sock->sockhost, port => $sock->sockport } } );          CouchDB::audit( 'start', { addr => $sock->sockhost, port => $sock->sockport } );
252    
253          while (1) {          while (1) {
254                  process_packet $sock;                  process_packet $sock;

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

  ViewVC Help
Powered by ViewVC 1.1.26