Project

General

Profile

Download (10 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
 *  Copyright (c)  2004, 2005 Scott Ullrich
8
 *
9
 *  Redistribution and use in source and binary forms, with or without modification, 
10
 *  are permitted provided that the following conditions are met: 
11
 *
12
 *  1. Redistributions of source code must retain the above copyright notice,
13
 *      this list of conditions and the following disclaimer.
14
 *
15
 *  2. Redistributions in binary form must reproduce the above copyright
16
 *      notice, this list of conditions and the following disclaimer in
17
 *      the documentation and/or other materials provided with the
18
 *      distribution. 
19
 *
20
 *  3. All advertising materials mentioning features or use of this software 
21
 *      must display the following acknowledgment:
22
 *      "This product includes software developed by the pfSense Project
23
 *       for use in the pfSense software distribution. (http://www.pfsense.org/). 
24
 *
25
 *  4. The names "pfSense" and "pfSense Project" must not be used to
26
 *       endorse or promote products derived from this software without
27
 *       prior written permission. For written permission, please contact
28
 *       coreteam@pfsense.org.
29
 *
30
 *  5. Products derived from this software may not be called "pfSense"
31
 *      nor may "pfSense" appear in their names without prior written
32
 *      permission of the Electric Sheep Fencing, LLC.
33
 *
34
 *  6. Redistributions of any form whatsoever must retain the following
35
 *      acknowledgment:
36
 *
37
 *  "This product includes software developed by the pfSense Project
38
 *  for use in the pfSense software distribution (http://www.pfsense.org/).
39
  *
40
 *  THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
41
 *  EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43
 *  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
44
 *  ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45
 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46
 *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48
 *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49
 *  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50
 *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51
 *  OF THE POSSIBILITY OF SUCH DAMAGE.
52
 *
53
 *  ====================================================================
54
 *
55
 */
56

    
57
/*
58
	pfSense_BUILDER_BINARIES:	/bin/cat		/usr/sbin/arp
59
	pfSense_MODULE:	arp
60
*/
61

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

    
69
@ini_set('zlib.output_compression', 0);
70
@ini_set('implicit_flush', 1);
71

    
72
require("guiconfig.inc");
73

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

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

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

    
94
// Define path to AWK
95
$awk = "/usr/bin/awk";
96

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

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

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

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

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

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

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

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

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

    
244
exec("/usr/sbin/arp -an", $rawdata);
245

    
246
$i = 0;
247

    
248
/* if list */
249
$ifdescrs = get_configured_interface_with_descr();
250

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

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

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

    
271
function _getHostName($mac, $ip) {
272
	global $dhcpmac, $dhcpip;
273

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

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

    
292
?>
293

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

    
299
<?php
300

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

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

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

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

    
335
// Load MAC-Manufacturer table
336
$mac_man = load_mac_manufacturer_table();
337
?>
338
<div class="table-responsive">
339
	<table class="table table-striped table-hover">
340
		<thead>
341
			<tr>
342
				<th><?= gettext("Interface")?></th>
343
				<th><?= gettext("IP address")?></th>
344
				<th><?= gettext("MAC address")?></th>
345
				<th><?= gettext("Hostname")?></th>
346
			</tr>
347
		</thead>
348
		<tbody>
349
		<?php 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
				</td>
363
				<td><?=trim(str_replace("Z_ ", "", $entry['dnsresolve']))?></td>
364
			</tr>
365
		<?php endforeach?>
366
		</tbody>
367
	</table>
368
</div>
369

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

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

    
382
include("foot.inc")?>
(6-6/238)