1
|
<?php
|
2
|
/*
|
3
|
* diag_arp.inc
|
4
|
*
|
5
|
* part of pfSense (https://www.pfsense.org)
|
6
|
* Copyright (c) 2004-2013 BSD Perimeter
|
7
|
* Copyright (c) 2013-2016 Electric Sheep Fencing
|
8
|
* Copyright (c) 2014-2021 Rubicon Communications, LLC (Netgate)
|
9
|
* All rights reserved.
|
10
|
*
|
11
|
* originally based on m0n0wall (http://m0n0.ch/wall)
|
12
|
* Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
|
13
|
* All rights reserved.
|
14
|
*
|
15
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
16
|
* you may not use this file except in compliance with the License.
|
17
|
* You may obtain a copy of the License at
|
18
|
*
|
19
|
* http://www.apache.org/licenses/LICENSE-2.0
|
20
|
*
|
21
|
* Unless required by applicable law or agreed to in writing, software
|
22
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
23
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
24
|
* See the License for the specific language governing permissions and
|
25
|
* limitations under the License.
|
26
|
*/
|
27
|
|
28
|
require_once("config.gui.inc");
|
29
|
|
30
|
// Put this in an easy to use form
|
31
|
$dhcpmac = array();
|
32
|
$dhcpip = array();
|
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
|
function prepare_ARP_table($json = false) {
|
53
|
global $dhcpmac, $dhcpip;
|
54
|
|
55
|
$leases = system_get_dhcpleases();
|
56
|
|
57
|
// Put this in an easy to use form
|
58
|
$dhcpmac = array();
|
59
|
$dhcpip = array();
|
60
|
|
61
|
foreach ($leases['lease'] as $value) {
|
62
|
$dhcpmac[$value['mac']] = $value['hostname'];
|
63
|
$dhcpip[$value['ip']] = $value['hostname'];
|
64
|
}
|
65
|
|
66
|
$arp_table = system_get_arp_table();
|
67
|
|
68
|
$i = 0;
|
69
|
|
70
|
// if list
|
71
|
$ifdescrs = get_configured_interface_with_descr();
|
72
|
|
73
|
foreach ($ifdescrs as $key => $interface) {
|
74
|
$thisif = convert_friendly_interface_to_real_interface_name($key);
|
75
|
if (!empty($thisif)) {
|
76
|
$hwif[$thisif] = $interface;
|
77
|
}
|
78
|
}
|
79
|
|
80
|
// Resolve hostnames and replace Z_ with "". The intention
|
81
|
// is to sort the list by hostnames, alpha and then the non
|
82
|
// resolvable addresses will appear last in the list.
|
83
|
$dnsavailable=1;
|
84
|
$dns = trim(_getHostName("", "8.8.8.8"));
|
85
|
if ($dns == "") {
|
86
|
$dns = trim(_getHostName("", "8.8.4.4"));
|
87
|
if ($dns == "") {
|
88
|
$dnsavailable = 0;
|
89
|
}
|
90
|
}
|
91
|
|
92
|
foreach ($arp_table as &$entry) {
|
93
|
// Add dnsresolve
|
94
|
if ($dnsavailable && !empty($entry['mac-address'])) {
|
95
|
$dns = trim(_getHostName($entry['mac-address'],
|
96
|
$entry['ip-address']));
|
97
|
} else {
|
98
|
$dns="";
|
99
|
}
|
100
|
|
101
|
if (trim($dns)) {
|
102
|
$entry['dnsresolve'] = "$dns";
|
103
|
} else {
|
104
|
$entry['dnsresolve'] = "Z_ ";
|
105
|
}
|
106
|
|
107
|
// Fix MAC address
|
108
|
if (empty($entry['mac-address'])) {
|
109
|
$mac = '(' . gettext("Incomplete") .')';
|
110
|
$entry['mac-address'] = $mac;
|
111
|
} else {
|
112
|
$mac = trim($entry['mac-address']);
|
113
|
$entry['mac-address'] = $mac;
|
114
|
|
115
|
$mac_hi = strtoupper($mac[0] . $mac[1] .
|
116
|
$mac[3] . $mac[4] . $mac[6] .
|
117
|
$mac[7]);
|
118
|
|
119
|
if (isset($mac_man[$mac_hi])) {
|
120
|
$entry['mac-address'] .= ' <small>('.
|
121
|
$mac_man[$mac_hi] .
|
122
|
')</small>';
|
123
|
}
|
124
|
}
|
125
|
|
126
|
// Fix expiration
|
127
|
$status = '';
|
128
|
if (!empty($entry['expires'])) {
|
129
|
$status = sprintf(gettext(
|
130
|
"Expires in %d seconds"),
|
131
|
$entry['expires']);
|
132
|
} else if (!empty($entry['permanent'])) {
|
133
|
$status = gettext("Permanent");
|
134
|
}
|
135
|
|
136
|
$entry['expires'] = $status;
|
137
|
|
138
|
// Fix dnsresolve
|
139
|
$entry['dnsresolve'] = trim(str_replace("Z_ ", "", $entry['dnsresolve']));
|
140
|
|
141
|
// Fix interface
|
142
|
$entry['interface'] = $hwif[$entry['interface']];
|
143
|
}
|
144
|
|
145
|
// Sort the data alpha first
|
146
|
$arp_table = msort($arp_table, "dnsresolve");
|
147
|
|
148
|
if ($json) {
|
149
|
$jstr = json_encode($arp_table);
|
150
|
$jstr = str_replace("mac-address", "mac", $jstr);
|
151
|
$jstr = str_replace("ip-address", "ip", $jstr);
|
152
|
|
153
|
return $jstr;
|
154
|
} else {
|
155
|
return $arp_table;
|
156
|
}
|
157
|
|
158
|
}
|
159
|
|
160
|
?>
|