Project

General

Profile

Download (10.1 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	diag_arp.php
4
*/
5
/* ====================================================================
6
 *  Copyright (c)  2004-2015  Electric Sheep Fencing, LLC. All rights reserved.
7
 *
8
 *  Some or all of this file is based on the m0n0wall project which is
9
 *  Copyright (c)  2004 Manuel Kasper (BSD 2 clause)
10
 *
11
 *  Redistribution and use in source and binary forms, with or without modification,
12
 *  are permitted provided that the following conditions are met:
13
 *
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
 *      distribution.
21
 *
22
 *  3. All advertising materials mentioning features or use of this software
23
 *      must display the following acknowledgment:
24
 *      "This product includes software developed by the pfSense Project
25
 *       for use in the pfSense software distribution. (http://www.pfsense.org/).
26
 *
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
 *
42
 *  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

    
59
##|+PRIV
60
##|*IDENT=page-diagnostics-arptable
61
##|*NAME=Diagnostics: ARP Table
62
##|*DESCR=Allow access to the 'Diagnostics: ARP Table' page.
63
##|*MATCH=diag_arp.php*
64
##|-PRIV
65

    
66
@ini_set('zlib.output_compression', 0);
67
@ini_set('implicit_flush', 1);
68

    
69
require("guiconfig.inc");
70

    
71
function leasecmp($a, $b) {
72
	return strcmp($a[$_GET['order']], $b[$_GET['order']]);
73
}
74

    
75
function adjust_gmt($dt) {
76
	$ts = strtotime($dt . " GMT");
77
	return strftime("%Y/%m/%d %H:%M:%S", $ts);
78
}
79

    
80
function remove_duplicate($array, $field) {
81
	foreach ($array as $sub) {
82
		$cmp[] = $sub[$field];
83
	}
84
	$unique = array_unique($cmp);
85
	foreach ($unique as $k => $rien) {
86
		$new[] = $array[$k];
87
	}
88
	return $new;
89
}
90

    
91
// Define path to AWK
92
$awk = "/usr/bin/awk";
93

    
94
// Read in leases file
95
$leasesfile = "{$g['dhcpd_chroot_path']}/var/db/dhcpd.leases";
96

    
97
/* this pattern sticks comments into a single array item */
98
$cleanpattern = "'{ gsub(\"#.*\", \"\");} { gsub(\";\", \"\"); print;}'";
99

    
100
/* We then split the leases file by } */
101
$splitpattern = "'BEGIN { RS=\"}\";} {for (i=1; i<=NF; i++) printf \"%s \", \$i; printf \"}\\n\";}'";
102

    
103
/* stuff the leases file in a proper format into an array by line */
104
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
while ($i < $leases_count) {
114
	/* 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
	if ($fcount < 20) {
121
		$i++;
122
		continue;
123
	}
124
	while ($f < $fcount) {
125
		switch ($data[$f]) {
126
			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
				switch ($data[$f+2]) {
166
					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
			case "rewind":
185
				/* skip the rewind binding statement */
186
				$f = $f+3;
187
				break;
188
			case "hardware":
189
				$leases[$l]['mac'] = $data[$f+2];
190
				/* check if it's online and the lease is active */
191
				if ($leases[$l]['act'] == "active") {
192
					$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
				if ($data[$f+1] <> "") {
203
					$leases[$l]['hostname'] = preg_replace('/"/', '', $data[$f+1]);
204
				} else {
205
					$hostname = gethostbyaddr($leases[$l]['ip']);
206
					if ($hostname <> "") {
207
						$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
}
221

    
222
/* remove duplicate items by mac address */
223
if (count($leases) > 0) {
224
	$leases = remove_duplicate($leases, "ip");
225
}
226

    
227
if (count($pools) > 0) {
228
	$pools = remove_duplicate($pools, "name");
229
	asort($pools);
230
}
231

    
232
// Put this in an easy to use form
233
$dhcpmac = array();
234
$dhcpip = array();
235

    
236
foreach ($leases as $value) {
237
	$dhcpmac[$value['mac']] = $value['hostname'];
238
	$dhcpip[$value['ip']] = $value['hostname'];
239
}
240

    
241
exec("/usr/sbin/arp -an", $rawdata);
242

    
243
$i = 0;
244

    
245
/* if list */
246
$ifdescrs = get_configured_interface_with_descr();
247

    
248
foreach ($ifdescrs as $key => $interface) {
249
	$thisif = convert_friendly_interface_to_real_interface_name($key);
250
	if (!empty($thisif)) {
251
		$hwif[$thisif] = $interface;
252
	}
253
}
254

    
255
$data = array();
256
foreach ($rawdata as $line) {
257
	$elements = explode(' ', $line);
258

    
259
	if ($elements[3] != "(incomplete)") {
260
		$arpent = array();
261
		$arpent['ip'] = trim(str_replace(array('(', ')'), '', $elements[1]));
262
		$arpent['mac'] = trim($elements[3]);
263
		$arpent['interface'] = trim($elements[5]);
264
		$data[] = $arpent;
265
	}
266
}
267

    
268
function _getHostName($mac, $ip) {
269
	global $dhcpmac, $dhcpip;
270

    
271
	if ($dhcpmac[$mac]) {
272
		return $dhcpmac[$mac];
273
	} else if ($dhcpip[$ip]) {
274
		return $dhcpip[$ip];
275
	} else {
276
		exec("host -W 1 " . escapeshellarg($ip), $output);
277
		if (preg_match('/.*pointer ([A-Za-z_0-9.-]+)\..*/', $output[0], $matches)) {
278
			if ($matches[1] <> $ip) {
279
				return $matches[1];
280
			}
281
		}
282
	}
283
	return "";
284
}
285

    
286
$pgtitle = array(gettext("Diagnostics"), gettext("ARP Table"));
287
include("head.inc");
288

    
289
?>
290

    
291
<!-- On modern hardware the table will load so fast you may never see this! -->
292
<div id="loading">
293
	<?= gettext(" Loading, please wait...")?>
294
</div>
295

    
296
<?php
297

    
298
// Flush buffers out to client so that they see Loading, please wait....
299
for ($i = 0; $i < ob_get_level(); $i++) {
300
	ob_end_flush();
301
}
302

    
303
ob_implicit_flush(1);
304

    
305
// 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
$dnsavailable=1;
309
$dns = trim(_getHostName("", "8.8.8.8"));
310
if ($dns == "") {
311
	$dns = trim(_getHostName("", "8.8.4.4"));
312
	if ($dns == "") {
313
		$dnsavailable = 0;
314
	}
315
}
316

    
317
foreach ($data as &$entry) {
318
	if ($dnsavailable) {
319
		$dns = trim(_getHostName($entry['mac'], $entry['ip']));
320
	} else {
321
		$dns="";
322
	}
323
	if (trim($dns)) {
324
		$entry['dnsresolve'] = "$dns";
325
	} else {
326
		$entry['dnsresolve'] = "Z_ ";
327
	}
328
}
329

    
330
// Sort the data alpha first
331
$data = msort($data, "dnsresolve");
332

    
333
// Load MAC-Manufacturer table
334
$mac_man = load_mac_manufacturer_table();
335
?>
336
<div class="table-responsive">
337
	<table class="sortable-theme-bootstrap table table-striped table-hover" data-sortable>
338
		<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

    
348
<?php
349
		foreach ($data as $entry): ?>
350
			<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
					if (isset($mac_man[$mac_hi])) {
360
						print '<small>('. $mac_man[$mac_hi] .')</small>';
361
					}
362
	?>
363
				</td>
364
				<td><?=trim(str_replace("Z_ ", "", $entry['dnsresolve']))?></td>
365
			</tr>
366
		<?php endforeach?>
367
		</tbody>
368
	</table>
369
</div>
370

    
371
<script type="text/javascript">
372
//<![CDATA[
373
// Clear the "loading" div once the page has loaded"
374
events.push(function() {
375
	$('#loading').empty();
376
});
377
//]]>
378
</script>
379

    
380
<?php
381
print_info_box(gettext("Local IPv6 peers use ") . '<a href="diag_ndp.php">' . gettext("NDP") . '</a>' . gettext(" instead of ARP"), 'info');
382

    
383
include("foot.inc")?>
(5-5/228)