Project

General

Profile

Download (10.1 KB) Statistics
| Branch: | Tag: | Revision:
1 93e1b16c Scott Ullrich
<?php
2
/*
3
	diag_arp.php
4
*/
5 fd9ebcd5 Stephen Beaver
/* ====================================================================
6 0da0d43e Phil Davis
 *  Copyright (c)  2004-2015  Electric Sheep Fencing, LLC. All rights reserved.
7 9da2cf1c Stephen Beaver
 *
8 c9df279d Stephen Beaver
 *  Some or all of this file is based on the m0n0wall project which is
9 9da2cf1c Stephen Beaver
 *  Copyright (c)  2004 Manuel Kasper (BSD 2 clause)
10 fd9ebcd5 Stephen Beaver
 *
11 0da0d43e Phil Davis
 *  Redistribution and use in source and binary forms, with or without modification,
12
 *  are permitted provided that the following conditions are met:
13 fd9ebcd5 Stephen Beaver
 *
14
 *  1. Redistributions of source code must retain the above copyright notice,
15
 *      this list of conditions and the following disclaimer.
16
 *
17
 *  2. Redistributions in binary form must reproduce the above copyright
18
 *      notice, this list of conditions and the following disclaimer in
19
 *      the documentation and/or other materials provided with the
20 0da0d43e Phil Davis
 *      distribution.
21 fd9ebcd5 Stephen Beaver
 *
22 0da0d43e Phil Davis
 *  3. All advertising materials mentioning features or use of this software
23 fd9ebcd5 Stephen Beaver
 *      must display the following acknowledgment:
24
 *      "This product includes software developed by the pfSense Project
25 0da0d43e Phil Davis
 *       for use in the pfSense software distribution. (http://www.pfsense.org/).
26 fd9ebcd5 Stephen Beaver
 *
27
 *  4. The names "pfSense" and "pfSense Project" must not be used to
28
 *       endorse or promote products derived from this software without
29
 *       prior written permission. For written permission, please contact
30
 *       coreteam@pfsense.org.
31
 *
32
 *  5. Products derived from this software may not be called "pfSense"
33
 *      nor may "pfSense" appear in their names without prior written
34
 *      permission of the Electric Sheep Fencing, LLC.
35
 *
36
 *  6. Redistributions of any form whatsoever must retain the following
37
 *      acknowledgment:
38
 *
39
 *  "This product includes software developed by the pfSense Project
40
 *  for use in the pfSense software distribution (http://www.pfsense.org/).
41 0da0d43e Phil Davis
 *
42 fd9ebcd5 Stephen Beaver
 *  THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
43
 *  EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44
 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
45
 *  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
46
 *  ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
47
 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
48
 *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
49
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50
 *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
51
 *  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
52
 *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
53
 *  OF THE POSSIBILITY OF SUCH DAMAGE.
54
 *
55
 *  ====================================================================
56
 *
57
 */
58 93e1b16c Scott Ullrich
59 6b07c15a Matthew Grooms
##|+PRIV
60
##|*IDENT=page-diagnostics-arptable
61 5230f468 jim-p
##|*NAME=Diagnostics: ARP Table
62 6b07c15a Matthew Grooms
##|*DESCR=Allow access to the 'Diagnostics: ARP Table' page.
63
##|*MATCH=diag_arp.php*
64
##|-PRIV
65
66 868ba36d Scott Ullrich
@ini_set('zlib.output_compression', 0);
67
@ini_set('implicit_flush', 1);
68 6b07c15a Matthew Grooms
69 93e1b16c Scott Ullrich
require("guiconfig.inc");
70 6090e85b Bill Marquette
71 fc259334 Seth Mos
function leasecmp($a, $b) {
72
	return strcmp($a[$_GET['order']], $b[$_GET['order']]);
73
}
74 93e1b16c Scott Ullrich
75 fc259334 Seth Mos
function adjust_gmt($dt) {
76
	$ts = strtotime($dt . " GMT");
77
	return strftime("%Y/%m/%d %H:%M:%S", $ts);
78
}
79 93e1b16c Scott Ullrich
80 fc259334 Seth Mos
function remove_duplicate($array, $field) {
81 5f601060 Phil Davis
	foreach ($array as $sub) {
82 20b9b335 Scott Ullrich
		$cmp[] = $sub[$field];
83 5f601060 Phil Davis
	}
84 fc259334 Seth Mos
	$unique = array_unique($cmp);
85 5f601060 Phil Davis
	foreach ($unique as $k => $rien) {
86 fc259334 Seth Mos
		$new[] = $array[$k];
87 5f601060 Phil Davis
	}
88 fc259334 Seth Mos
	return $new;
89
}
90 93e1b16c Scott Ullrich
91 20b9b335 Scott Ullrich
// Define path to AWK
92 fc259334 Seth Mos
$awk = "/usr/bin/awk";
93 20b9b335 Scott Ullrich
94
// Read in leases file
95
$leasesfile = "{$g['dhcpd_chroot_path']}/var/db/dhcpd.leases";
96
97 fc259334 Seth Mos
/* this pattern sticks comments into a single array item */
98
$cleanpattern = "'{ gsub(\"#.*\", \"\");} { gsub(\";\", \"\"); print;}'";
99 20b9b335 Scott Ullrich
100 fc259334 Seth Mos
/* We then split the leases file by } */
101
$splitpattern = "'BEGIN { RS=\"}\";} {for (i=1; i<=NF; i++) printf \"%s \", \$i; printf \"}\\n\";}'";
102 93e1b16c Scott Ullrich
103 5f601060 Phil Davis
/* stuff the leases file in a proper format into an array by line */
104 fc259334 Seth Mos
exec("cat {$leasesfile} | {$awk} {$cleanpattern} | {$awk} {$splitpattern}", $leases_content);
105
$leases_count = count($leases_content);
106
107
$pools = array();
108
$leases = array();
109
$i = 0;
110
$l = 0;
111
$p = 0;
112
// Put everything together again
113 5f601060 Phil Davis
while ($i < $leases_count) {
114 b45630bf jim-p
	/* split the line by space */
115
	$data = explode(" ", $leases_content[$i]);
116
	/* walk the fields */
117
	$f = 0;
118
	$fcount = count($data);
119
	/* with less then 20 fields there is nothing useful */
120 5f601060 Phil Davis
	if ($fcount < 20) {
121 b45630bf jim-p
		$i++;
122
		continue;
123
	}
124 5f601060 Phil Davis
	while ($f < $fcount) {
125
		switch ($data[$f]) {
126 b45630bf jim-p
			case "failover":
127
				$pools[$p]['name'] = $data[$f+2];
128
				$pools[$p]['mystate'] = $data[$f+7];
129
				$pools[$p]['peerstate'] = $data[$f+14];
130
				$pools[$p]['mydate'] = $data[$f+10];
131
				$pools[$p]['mydate'] .= " " . $data[$f+11];
132
				$pools[$p]['peerdate'] = $data[$f+17];
133
				$pools[$p]['peerdate'] .= " " . $data[$f+18];
134
				$p++;
135
				$i++;
136
				continue 3;
137
			case "lease":
138
				$leases[$l]['ip'] = $data[$f+1];
139
				$leases[$l]['type'] = "dynamic";
140
				$f = $f+2;
141
				break;
142
			case "starts":
143
				$leases[$l]['start'] = $data[$f+2];
144
				$leases[$l]['start'] .= " " . $data[$f+3];
145
				$f = $f+3;
146
				break;
147
			case "ends":
148
				$leases[$l]['end'] = $data[$f+2];
149
				$leases[$l]['end'] .= " " . $data[$f+3];
150
				$f = $f+3;
151
				break;
152
			case "tstp":
153
				$f = $f+3;
154
				break;
155
			case "tsfp":
156
				$f = $f+3;
157
				break;
158
			case "atsfp":
159
				$f = $f+3;
160
				break;
161
			case "cltt":
162
				$f = $f+3;
163
				break;
164
			case "binding":
165 699737d9 Phil Davis
				switch ($data[$f+2]) {
166 b45630bf jim-p
					case "active":
167
						$leases[$l]['act'] = "active";
168
						break;
169
					case "free":
170
						$leases[$l]['act'] = "expired";
171
						$leases[$l]['online'] = "offline";
172
						break;
173
					case "backup":
174
						$leases[$l]['act'] = "reserved";
175
						$leases[$l]['online'] = "offline";
176
						break;
177
				}
178
				$f = $f+1;
179
				break;
180
			case "next":
181
				/* skip the next binding statement */
182
				$f = $f+3;
183
				break;
184 f1273b82 jim-p
			case "rewind":
185
				/* skip the rewind binding statement */
186
				$f = $f+3;
187
				break;
188 b45630bf jim-p
			case "hardware":
189
				$leases[$l]['mac'] = $data[$f+2];
190
				/* check if it's online and the lease is active */
191 5f601060 Phil Davis
				if ($leases[$l]['act'] == "active") {
192 b45630bf jim-p
					$online = exec("/usr/sbin/arp -an |/usr/bin/awk '/{$leases[$l]['ip']}/ {print}'|wc -l");
193
					if ($online == 1) {
194
						$leases[$l]['online'] = 'online';
195
					} else {
196
						$leases[$l]['online'] = 'offline';
197
					}
198
				}
199
				$f = $f+2;
200
				break;
201
			case "client-hostname":
202 5f601060 Phil Davis
				if ($data[$f+1] <> "") {
203 699737d9 Phil Davis
					$leases[$l]['hostname'] = preg_replace('/"/', '', $data[$f+1]);
204 b45630bf jim-p
				} else {
205
					$hostname = gethostbyaddr($leases[$l]['ip']);
206 5f601060 Phil Davis
					if ($hostname <> "") {
207 b45630bf jim-p
						$leases[$l]['hostname'] = $hostname;
208
					}
209
				}
210
				$f = $f+1;
211
				break;
212
			case "uid":
213
				$f = $f+1;
214
				break;
215
		}
216
		$f++;
217
	}
218
	$l++;
219
	$i++;
220 fc259334 Seth Mos
}
221
222
/* remove duplicate items by mac address */
223 5f601060 Phil Davis
if (count($leases) > 0) {
224 699737d9 Phil Davis
	$leases = remove_duplicate($leases, "ip");
225 fc259334 Seth Mos
}
226
227 5f601060 Phil Davis
if (count($pools) > 0) {
228 699737d9 Phil Davis
	$pools = remove_duplicate($pools, "name");
229 b45630bf jim-p
	asort($pools);
230 fc259334 Seth Mos
}
231
232
// Put this in an easy to use form
233
$dhcpmac = array();
234
$dhcpip = array();
235 f8ec8de4 Renato Botelho
236 fc259334 Seth Mos
foreach ($leases as $value) {
237 f8ec8de4 Renato Botelho
	$dhcpmac[$value['mac']] = $value['hostname'];
238
	$dhcpip[$value['ip']] = $value['hostname'];
239 93e1b16c Scott Ullrich
}
240
241 699737d9 Phil Davis
exec("/usr/sbin/arp -an", $rawdata);
242 93e1b16c Scott Ullrich
243 f8ec8de4 Renato Botelho
$i = 0;
244 cbe3ea96 Ermal Luçi
245
/* if list */
246
$ifdescrs = get_configured_interface_with_descr();
247 93e1b16c Scott Ullrich
248 e7237dd0 jim-p
foreach ($ifdescrs as $key => $interface) {
249
	$thisif = convert_friendly_interface_to_real_interface_name($key);
250 5f601060 Phil Davis
	if (!empty($thisif)) {
251 e7237dd0 jim-p
		$hwif[$thisif] = $interface;
252 5f601060 Phil Davis
	}
253 93e1b16c Scott Ullrich
}
254
255
$data = array();
256
foreach ($rawdata as $line) {
257 699737d9 Phil Davis
	$elements = explode(' ', $line);
258 f8ec8de4 Renato Botelho
259 93e1b16c Scott Ullrich
	if ($elements[3] != "(incomplete)") {
260
		$arpent = array();
261 699737d9 Phil Davis
		$arpent['ip'] = trim(str_replace(array('(', ')'), '', $elements[1]));
262 93e1b16c Scott Ullrich
		$arpent['mac'] = trim($elements[3]);
263
		$arpent['interface'] = trim($elements[5]);
264
		$data[] = $arpent;
265
	}
266
}
267
268 699737d9 Phil Davis
function _getHostName($mac, $ip) {
269 93e1b16c Scott Ullrich
	global $dhcpmac, $dhcpip;
270 f8ec8de4 Renato Botelho
271 5f601060 Phil Davis
	if ($dhcpmac[$mac]) {
272 93e1b16c Scott Ullrich
		return $dhcpmac[$mac];
273 5f601060 Phil Davis
	} else if ($dhcpip[$ip]) {
274 93e1b16c Scott Ullrich
		return $dhcpip[$ip];
275 5f601060 Phil Davis
	} else {
276 d31ca336 Renato Botelho
		exec("host -W 1 " . escapeshellarg($ip), $output);
277 699737d9 Phil Davis
		if (preg_match('/.*pointer ([A-Za-z_0-9.-]+)\..*/', $output[0], $matches)) {
278 5f601060 Phil Davis
			if ($matches[1] <> $ip) {
279 7d5b007c Sjon Hortensius
				return $matches[1];
280 5f601060 Phil Davis
			}
281 dd4bded7 Evgeny Yurchenko
		}
282
	}
283
	return "";
284 93e1b16c Scott Ullrich
}
285
286 699737d9 Phil Davis
$pgtitle = array(gettext("Diagnostics"), gettext("ARP Table"));
287 fc259334 Seth Mos
include("head.inc");
288 20b9b335 Scott Ullrich
289 93e1b16c Scott Ullrich
?>
290 20b9b335 Scott Ullrich
291 a4af095c Renato Botelho
<!-- On modern hardware the table will load so fast you may never see this! -->
292 4a993c8f Scott Ullrich
<div id="loading">
293 a4af095c Renato Botelho
	<?= gettext(" Loading, please wait...")?>
294 4a993c8f Scott Ullrich
</div>
295
296
<?php
297
298 868ba36d Scott Ullrich
// Flush buffers out to client so that they see Loading, please wait....
299 5f601060 Phil Davis
for ($i = 0; $i < ob_get_level(); $i++) {
300
	ob_end_flush();
301
}
302 608947a8 Stephen Beaver
303 868ba36d Scott Ullrich
ob_implicit_flush(1);
304
305 20b9b335 Scott Ullrich
// Resolve hostnames and replace Z_ with "".  The intention
306
// is to sort the list by hostnames, alpha and then the non
307
// resolvable addresses will appear last in the list.
308 dd4bded7 Evgeny Yurchenko
$dnsavailable=1;
309 7d5b007c Sjon Hortensius
$dns = trim(_getHostName("", "8.8.8.8"));
310 5f601060 Phil Davis
if ($dns == "") {
311 7d5b007c Sjon Hortensius
	$dns = trim(_getHostName("", "8.8.4.4"));
312 5f601060 Phil Davis
	if ($dns == "") {
313
		$dnsavailable = 0;
314
	}
315 dd4bded7 Evgeny Yurchenko
}
316
317 4a993c8f Scott Ullrich
foreach ($data as &$entry) {
318 5f601060 Phil Davis
	if ($dnsavailable) {
319 dd4bded7 Evgeny Yurchenko
		$dns = trim(_getHostName($entry['mac'], $entry['ip']));
320 5f601060 Phil Davis
	} else {
321 dd4bded7 Evgeny Yurchenko
		$dns="";
322 5f601060 Phil Davis
	}
323
	if (trim($dns)) {
324 4a993c8f Scott Ullrich
		$entry['dnsresolve'] = "$dns";
325 5f601060 Phil Davis
	} else {
326 4a993c8f Scott Ullrich
		$entry['dnsresolve'] = "Z_ ";
327 5f601060 Phil Davis
	}
328 4a993c8f Scott Ullrich
}
329 20b9b335 Scott Ullrich
330
// Sort the data alpha first
331 4a993c8f Scott Ullrich
$data = msort($data, "dnsresolve");
332
333 57f2840e Evgeny
// Load MAC-Manufacturer table
334
$mac_man = load_mac_manufacturer_table();
335 4a993c8f Scott Ullrich
?>
336 89f64f0f Sander van Leeuwen
<div class="table-responsive">
337 608947a8 Stephen Beaver
	<table class="sortable-theme-bootstrap table table-striped table-hover" data-sortable>
338 89f64f0f Sander van Leeuwen
		<thead>
339
			<tr>
340
				<th><?= gettext("Interface")?></th>
341
				<th><?= gettext("IP address")?></th>
342
				<th><?= gettext("MAC address")?></th>
343
				<th><?= gettext("Hostname")?></th>
344
			</tr>
345
		</thead>
346
		<tbody>
347 947141fd Phil Davis
348 82afb104 Stephen Beaver
<?php
349
		foreach ($data as $entry): ?>
350 89f64f0f Sander van Leeuwen
			<tr>
351
				<td><?=$hwif[$entry['interface']]?></td>
352
				<td><?=$entry['ip']?></td>
353
				<td>
354
					<?=trim($entry['mac'])?>
355
				<?php
356
					$mac = trim($entry['mac']);
357
					$mac_hi = strtoupper($mac[0] . $mac[1] . $mac[3] . $mac[4] . $mac[6] . $mac[7]);
358
359 cba48c64 Colin Fleming
					if (isset($mac_man[$mac_hi])) {
360 89f64f0f Sander van Leeuwen
						print '<small>('. $mac_man[$mac_hi] .')</small>';
361 cba48c64 Colin Fleming
					}
362 89f64f0f Sander van Leeuwen
	?>
363
				</td>
364
				<td><?=trim(str_replace("Z_ ", "", $entry['dnsresolve']))?></td>
365
			</tr>
366
		<?php endforeach?>
367
		</tbody>
368
	</table>
369
</div>
370
371 cba48c64 Colin Fleming
<script type="text/javascript">
372 293ceb87 Colin Fleming
//<![CDATA[
373 a4af095c Renato Botelho
// Clear the "loading" div once the page has loaded"
374 947141fd Phil Davis
events.push(function() {
375 a4af095c Renato Botelho
	$('#loading').empty();
376
});
377 0da0d43e Phil Davis
//]]>
378 4a993c8f Scott Ullrich
</script>
379 a4af095c Renato Botelho
380 0da0d43e Phil Davis
<?php
381 a4af095c Renato Botelho
print_info_box(gettext("Local IPv6 peers use ") . '<a href="diag_ndp.php">' . gettext("NDP") . '</a>' . gettext(" instead of ARP"), 'info');
382 93e1b16c Scott Ullrich
383 a4af095c Renato Botelho
include("foot.inc")?>