Project

General

Profile

« Previous | Next » 

Revision e33311fe

Added by Viktor Gurov over 3 years ago

DNS check optimization. Fixes #11512

View differences:

src/etc/inc/system.inc
672 672
	}
673 673
}
674 674

  
675
function system_get_dhcpleases() {
675
function system_get_dhcpleases($dnsavailable=null) {
676 676
	global $config, $g;
677 677

  
678 678
	$leases = array();
......
730 730
	$failover = false;
731 731
	$dedup_lease = false;
732 732
	$dedup_failover = false;
733

  
733 734
	foreach ($leases_content as $line) {
734 735
		/* Skip comments */
735 736
		if (preg_match('/^\s*(|#.*)$/', $line)) {
......
739 740
		if (preg_match('/}$/', $line)) {
740 741
			if ($lease) {
741 742
				if (empty($item['hostname'])) {
742
					$hostname = gethostbyaddr($item['ip']);
743
					if (!empty($hostname)) {
744
						$item['hostname'] = $hostname;
743
					if (is_null($dnsavailable)) {
744
						$dnsavailable = check_dnsavailable();
745
					}
746
					if ($dnsavailable) {
747
						$hostname = gethostbyaddr($item['ip']);
748
						if (!empty($hostname)) {
749
							$item['hostname'] = $hostname;
750
						}
745 751
					}
746 752
				}
747 753
				$leases['lease'][] = $item;
......
2704 2710
	return $arp_table;
2705 2711
}
2706 2712

  
2713
function _getHostName($mac, $ip) {
2714
	global $dhcpmac, $dhcpip;
2715

  
2716
	if ($dhcpmac[$mac]) {
2717
		return $dhcpmac[$mac];
2718
	} else if ($dhcpip[$ip]) {
2719
		return $dhcpip[$ip];
2720
	} else {
2721
		exec("/usr/bin/host -W 1 " . escapeshellarg($ip), $output);
2722
		if (preg_match('/.*pointer ([A-Za-z_0-9.-]+)\..*/', $output[0], $matches)) {
2723
			if ($matches[1] <> $ip) {
2724
				return $matches[1];
2725
			}
2726
		}
2727
	}
2728
	return "";
2729
}
2730

  
2731
function check_dnsavailable() {
2732
	$dns = trim(_getHostName("", "8.8.8.8"));
2733
	if ($dns == "") {
2734
		$dns = trim(_getHostName("", "8.8.4.4"));
2735
		if ($dns == "") {
2736
			return false;
2737
		}
2738
	}
2739
	return true;
2740
}
2741

  
2707 2742
?>
src/usr/local/bin/dhcpd_gather_stats.php
36 36
if (is_array($config['dhcpd'][$argv[1]])) {
37 37

  
38 38
	$dhcpif = $argv[1] ;
39
	$leases = system_get_dhcpleases();
39
	$leases = system_get_dhcpleases(false);
40 40

  
41 41
	$ifcfgip = get_interface_ip($dhcpif);
42 42
	$ifcfgsn = get_interface_subnet($dhcpif);
src/usr/local/pfSense/include/www/diag_arp.inc
31 31
$dhcpmac = array();
32 32
$dhcpip = array();
33 33

  
34
function _getHostName($mac, $ip) {
35
	global $dhcpmac, $dhcpip;
36

  
37
	if ($dhcpmac[$mac]) {
38
		return $dhcpmac[$mac];
39
	} else if ($dhcpip[$ip]) {
40
		return $dhcpip[$ip];
41
	} else {
42
		exec("host -W 1 " . escapeshellarg($ip), $output);
43
		if (preg_match('/.*pointer ([A-Za-z_0-9.-]+)\..*/', $output[0], $matches)) {
44
			if ($matches[1] <> $ip) {
45
				return $matches[1];
46
			}
47
		}
48
	}
49
	return "";
50
}
51

  
52 34
function prepare_ARP_table($json = false) {
53 35
	global $dhcpmac, $dhcpip;
54 36

  
55
	$leases = system_get_dhcpleases();
37
	$leases = system_get_dhcpleases(false);
56 38

  
57 39
	// Put this in an easy to use form
58 40
	$dhcpmac = array();
......
88 70
	// Resolve hostnames and replace Z_ with "".  The intention
89 71
	// is to sort the list by hostnames, alpha and then the non
90 72
	// resolvable addresses will appear last in the list.
91
	$dnsavailable=1;
92
	$dns = trim(_getHostName("", "8.8.8.8"));
93
	if ($dns == "") {
94
		$dns = trim(_getHostName("", "8.8.4.4"));
95
		if ($dns == "") {
96
			$dnsavailable = 0;
97
		}
98
	}
99 73

  
100 74
	foreach ($arp_table as &$entry) {
101 75
		// Add dnsresolve
102
		if ($dnsavailable && !empty($entry['mac-address'])) {
103
			$dns = trim(_getHostName($entry['mac-address'],
104
			    $entry['ip-address']));
76
		if (!empty($entry['mac-address'])) {
77
			if (is_null($dnsavailable)) {
78
				$dnsavailable = check_dnsavailable();
79
			}
80
			if ($dnsavailable) {
81
				$dns = trim(_getHostName($entry['mac-address'],
82
					$entry['ip-address']));
83
			} else {
84
				$dns="";
85
			}
105 86
		} else {
106 87
			$dns="";
107 88
		}
......
165 146

  
166 147
}
167 148

  
168
?>
149
?>

Also available in: Unified diff