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
/*
60
	pfSense_BUILDER_BINARIES:	/bin/cat		/usr/sbin/arp
61
	pfSense_MODULE:	arp
62
*/
63

    
64
##|+PRIV
65
##|*IDENT=page-diagnostics-arptable
66
##|*NAME=Diagnostics: ARP Table page
67
##|*DESCR=Allow access to the 'Diagnostics: ARP Table' page.
68
##|*MATCH=diag_arp.php*
69
##|-PRIV
70

    
71
@ini_set('zlib.output_compression', 0);
72
@ini_set('implicit_flush', 1);
73

    
74
require("guiconfig.inc");
75

    
76
function leasecmp($a, $b) {
77
	return strcmp($a[$_GET['order']], $b[$_GET['order']]);
78
}
79

    
80
function adjust_gmt($dt) {
81
	$ts = strtotime($dt . " GMT");
82
	return strftime("%Y/%m/%d %H:%M:%S", $ts);
83
}
84

    
85
function remove_duplicate($array, $field) {
86
	foreach ($array as $sub) {
87
		$cmp[] = $sub[$field];
88
	}
89
	$unique = array_unique($cmp);
90
	foreach ($unique as $k => $rien) {
91
		$new[] = $array[$k];
92
	}
93
	return $new;
94
}
95

    
96
// Define path to AWK
97
$awk = "/usr/bin/awk";
98

    
99
// Read in leases file
100
$leasesfile = "{$g['dhcpd_chroot_path']}/var/db/dhcpd.leases";
101

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

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

    
108
/* stuff the leases file in a proper format into an array by line */
109
exec("cat {$leasesfile} | {$awk} {$cleanpattern} | {$awk} {$splitpattern}", $leases_content);
110
$leases_count = count($leases_content);
111

    
112
$pools = array();
113
$leases = array();
114
$i = 0;
115
$l = 0;
116
$p = 0;
117
// Put everything together again
118
while ($i < $leases_count) {
119
	/* split the line by space */
120
	$data = explode(" ", $leases_content[$i]);
121
	/* walk the fields */
122
	$f = 0;
123
	$fcount = count($data);
124
	/* with less then 20 fields there is nothing useful */
125
	if ($fcount < 20) {
126
		$i++;
127
		continue;
128
	}
129
	while ($f < $fcount) {
130
		switch ($data[$f]) {
131
			case "failover":
132
				$pools[$p]['name'] = $data[$f+2];
133
				$pools[$p]['mystate'] = $data[$f+7];
134
				$pools[$p]['peerstate'] = $data[$f+14];
135
				$pools[$p]['mydate'] = $data[$f+10];
136
				$pools[$p]['mydate'] .= " " . $data[$f+11];
137
				$pools[$p]['peerdate'] = $data[$f+17];
138
				$pools[$p]['peerdate'] .= " " . $data[$f+18];
139
				$p++;
140
				$i++;
141
				continue 3;
142
			case "lease":
143
				$leases[$l]['ip'] = $data[$f+1];
144
				$leases[$l]['type'] = "dynamic";
145
				$f = $f+2;
146
				break;
147
			case "starts":
148
				$leases[$l]['start'] = $data[$f+2];
149
				$leases[$l]['start'] .= " " . $data[$f+3];
150
				$f = $f+3;
151
				break;
152
			case "ends":
153
				$leases[$l]['end'] = $data[$f+2];
154
				$leases[$l]['end'] .= " " . $data[$f+3];
155
				$f = $f+3;
156
				break;
157
			case "tstp":
158
				$f = $f+3;
159
				break;
160
			case "tsfp":
161
				$f = $f+3;
162
				break;
163
			case "atsfp":
164
				$f = $f+3;
165
				break;
166
			case "cltt":
167
				$f = $f+3;
168
				break;
169
			case "binding":
170
				switch ($data[$f+2]) {
171
					case "active":
172
						$leases[$l]['act'] = "active";
173
						break;
174
					case "free":
175
						$leases[$l]['act'] = "expired";
176
						$leases[$l]['online'] = "offline";
177
						break;
178
					case "backup":
179
						$leases[$l]['act'] = "reserved";
180
						$leases[$l]['online'] = "offline";
181
						break;
182
				}
183
				$f = $f+1;
184
				break;
185
			case "next":
186
				/* skip the next binding statement */
187
				$f = $f+3;
188
				break;
189
			case "rewind":
190
				/* skip the rewind binding statement */
191
				$f = $f+3;
192
				break;
193
			case "hardware":
194
				$leases[$l]['mac'] = $data[$f+2];
195
				/* check if it's online and the lease is active */
196
				if ($leases[$l]['act'] == "active") {
197
					$online = exec("/usr/sbin/arp -an |/usr/bin/awk '/{$leases[$l]['ip']}/ {print}'|wc -l");
198
					if ($online == 1) {
199
						$leases[$l]['online'] = 'online';
200
					} else {
201
						$leases[$l]['online'] = 'offline';
202
					}
203
				}
204
				$f = $f+2;
205
				break;
206
			case "client-hostname":
207
				if ($data[$f+1] <> "") {
208
					$leases[$l]['hostname'] = preg_replace('/"/', '', $data[$f+1]);
209
				} else {
210
					$hostname = gethostbyaddr($leases[$l]['ip']);
211
					if ($hostname <> "") {
212
						$leases[$l]['hostname'] = $hostname;
213
					}
214
				}
215
				$f = $f+1;
216
				break;
217
			case "uid":
218
				$f = $f+1;
219
				break;
220
		}
221
		$f++;
222
	}
223
	$l++;
224
	$i++;
225
}
226

    
227
/* remove duplicate items by mac address */
228
if (count($leases) > 0) {
229
	$leases = remove_duplicate($leases, "ip");
230
}
231

    
232
if (count($pools) > 0) {
233
	$pools = remove_duplicate($pools, "name");
234
	asort($pools);
235
}
236

    
237
// Put this in an easy to use form
238
$dhcpmac = array();
239
$dhcpip = array();
240

    
241
foreach ($leases as $value) {
242
	$dhcpmac[$value['mac']] = $value['hostname'];
243
	$dhcpip[$value['ip']] = $value['hostname'];
244
}
245

    
246
exec("/usr/sbin/arp -an", $rawdata);
247

    
248
$i = 0;
249

    
250
/* if list */
251
$ifdescrs = get_configured_interface_with_descr();
252

    
253
foreach ($ifdescrs as $key => $interface) {
254
	$thisif = convert_friendly_interface_to_real_interface_name($key);
255
	if (!empty($thisif)) {
256
		$hwif[$thisif] = $interface;
257
	}
258
}
259

    
260
$data = array();
261
foreach ($rawdata as $line) {
262
	$elements = explode(' ', $line);
263

    
264
	if ($elements[3] != "(incomplete)") {
265
		$arpent = array();
266
		$arpent['ip'] = trim(str_replace(array('(', ')'), '', $elements[1]));
267
		$arpent['mac'] = trim($elements[3]);
268
		$arpent['interface'] = trim($elements[5]);
269
		$data[] = $arpent;
270
	}
271
}
272

    
273
function _getHostName($mac, $ip) {
274
	global $dhcpmac, $dhcpip;
275

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

    
291
$pgtitle = array(gettext("Diagnostics"), gettext("ARP Table"));
292
include("head.inc");
293

    
294
?>
295

    
296
<!-- On modern hardware the table will load so fast you may never see this! -->
297
<div id="loading">
298
	<?= gettext(" Loading, please wait...")?>
299
</div>
300

    
301
<?php
302

    
303
// Flush buffers out to client so that they see Loading, please wait....
304
for ($i = 0; $i < ob_get_level(); $i++) {
305
	ob_end_flush();
306
}
307

    
308
ob_implicit_flush(1);
309

    
310
// Resolve hostnames and replace Z_ with "".  The intention
311
// is to sort the list by hostnames, alpha and then the non
312
// resolvable addresses will appear last in the list.
313
$dnsavailable=1;
314
$dns = trim(_getHostName("", "8.8.8.8"));
315
if ($dns == "") {
316
	$dns = trim(_getHostName("", "8.8.4.4"));
317
	if ($dns == "") {
318
		$dnsavailable = 0;
319
	}
320
}
321

    
322
foreach ($data as &$entry) {
323
	if ($dnsavailable) {
324
		$dns = trim(_getHostName($entry['mac'], $entry['ip']));
325
	} else {
326
		$dns="";
327
	}
328
	if (trim($dns)) {
329
		$entry['dnsresolve'] = "$dns";
330
	} else {
331
		$entry['dnsresolve'] = "Z_ ";
332
	}
333
}
334

    
335
// Sort the data alpha first
336
$data = msort($data, "dnsresolve");
337

    
338
// Load MAC-Manufacturer table
339
$mac_man = load_mac_manufacturer_table();
340
?>
341
<div class="table-responsive">
342
	<table class="sortable-theme-bootstrap table table-striped table-hover" data-sortable>
343
		<thead>
344
			<tr>
345
				<th><?= gettext("Interface")?></th>
346
				<th><?= gettext("IP address")?></th>
347
				<th><?= gettext("MAC address")?></th>
348
				<th><?= gettext("Hostname")?></th>
349
			</tr>
350
		</thead>
351
		<tbody>
352
		<?php foreach ($data as $entry): ?>
353
			<tr>
354
				<td><?=$hwif[$entry['interface']]?></td>
355
				<td><?=$entry['ip']?></td>
356
				<td>
357
					<?=trim($entry['mac'])?>
358
				<?php
359
					$mac = trim($entry['mac']);
360
					$mac_hi = strtoupper($mac[0] . $mac[1] . $mac[3] . $mac[4] . $mac[6] . $mac[7]);
361

    
362
					if (isset($mac_man[$mac_hi]))
363
						print '<small>('. $mac_man[$mac_hi] .')</small>';
364
	?>
365
				</td>
366
				<td><?=trim(str_replace("Z_ ", "", $entry['dnsresolve']))?></td>
367
			</tr>
368
		<?php endforeach?>
369
		</tbody>
370
	</table>
371
</div>
372

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

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

    
385
include("foot.inc")?>
(6-6/234)