Code

Updated trigger event
[gosa.git] / gosa-si / modules / ArpHandler.pm
index d81ef57d5a6db68d02f95300da152236053c6ec7..3bc46cfd05861f2cd89c61a5bc51aba0bb9cf017 100644 (file)
@@ -14,7 +14,7 @@ use Net::LDAP::Entry;
 use Net::DNS;
 use Switch;
 use Data::Dumper;
-use utf8;
+use Socket;
 
 # Don't start if some of the modules are missing
 my $start_service=1;
@@ -36,7 +36,6 @@ END{}
 my ($timeout, $mailto, $mailfrom, $user, $group);
 my ($arp_enabled, $arp_interface, $ldap_uri, $ldap_base, $ldap_admin_dn, $ldap_admin_password);
 my $hosts_database={};
-my $resolver=Net::DNS::Resolver->new;
 my $ldap;
 
 my %cfg_defaults =
@@ -95,13 +94,13 @@ sub get_module_info {
                                &main::daemon_log("Loading OUI cache file suceeded!", 6);
                        }
                }
+        my $ldap_handle = &main::get_ldap_handle();
                if(defined($ldap_uri) && length($ldap_uri)>0) {
-                       &main::refresh_ldap_handle();
-                       if (!$main::ldap_handle) {
+                       if (!$ldap_handle) {
                                &main::daemon_log("Could not connect to LDAP Server at $ldap_uri!\n$@", 1);
                        }
                } else {
-                       $main::ldap_handle= undef;
+                       $ldap_handle= undef;
                }
 
                # When interface is not configured (or 'all'), start arpwatch on all possible interfaces
@@ -178,9 +177,9 @@ sub got_packet {
        
        my $capture_device = sprintf "%s", $kernel->alias_list($sender) =~ /^arp_watch_(.*)$/;
 
-       if(!exists($hosts_database->{$packet->{source_haddr}})) {
-               my $dnsresult= $resolver->search($packet->{source_ipaddr});
-               my $dnsname= (defined($dnsresult))?$dnsresult->{answer}[0]->{ptrdname}:$packet->{source_ipaddr};
+       my $ldap_handle = &main::get_ldap_handle(); 
+    if(!exists($hosts_database->{$packet->{source_haddr}})) {
+               my $dnsname= gethostbyaddr(inet_aton($packet->{source_ipaddr}), AF_INET) || $packet->{source_ipaddr};
                my $ldap_result=&get_host_from_ldap($packet->{source_haddr});
                if(exists($ldap_result->{dn})) {
                        $hosts_database->{$packet->{source_haddr}}=$ldap_result;
@@ -211,7 +210,7 @@ sub got_packet {
                                ": ".$hosts_database->{$packet->{source_haddr}}->{ipHostNumber}.
                                "/".$hosts_database->{$packet->{source_haddr}}->{macAddress},4);
                        &add_ldap_entry(
-                               $main::ldap_handle, 
+                               $ldap_handle, 
                                $ldap_base, 
                                $hosts_database->{$packet->{source_haddr}}->{macAddress},
                                'new-system',
@@ -228,7 +227,7 @@ sub got_packet {
                                "->".$packet->{source_ipaddr}, 4);
                        $hosts_database->{$packet->{source_haddr}}->{ipHostNumber}= $packet->{source_ipaddr};
                        &change_ldap_entry(
-                               $main::ldap_handle, 
+                               $ldap_handle, 
                                $ldap_base, 
                                $hosts_database->{$packet->{source_haddr}}->{macAddress},
                                'ip-changed',
@@ -244,9 +243,10 @@ sub get_host_from_ldap {
        my $mac=shift;
        my $result={};
                
-       if(defined($main::ldap_handle)) {
+    my $ldap_handle = &get_ldap_handle();     
+       if(defined($ldap_handle)) {
                my $ldap_result= &search_ldap_entry(
-                       $main::ldap_handle,
+                       $ldap_handle,
                        $ldap_base,
                        "(|(macAddress=$mac)(dhcpHWAddress=ethernet $mac))"
                );
@@ -442,7 +442,7 @@ sub change_ldap_entry {
                if (defined($ip)) {
                        $replace->{'ipHostNumber'} = $ip;
                }
-               my $result = $main::ldap_handle->modify( $dn, replace => $replace );
+               my $result = $ldap_tree->modify( $dn, replace => $replace );
 
                # for $result->code constants please look at Net::LDAP::Constant
                if($result->code == 32) {   # entry doesnt exists