Project

General

Profile

Download (10 KB) Statistics
| Branch: | Tag: | Revision:
1 93e1b16c Scott Ullrich
<?php
2
/*
3
	diag_arp.php
4
*/
5 fd9ebcd5 Stephen Beaver
/* ====================================================================
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 93e1b16c Scott Ullrich
57 13d193c2 Scott Ullrich
/*
58
	pfSense_BUILDER_BINARIES:	/bin/cat		/usr/sbin/arp
59
	pfSense_MODULE:	arp
60
*/
61
62 6b07c15a Matthew Grooms
##|+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 868ba36d Scott Ullrich
@ini_set('zlib.output_compression', 0);
70
@ini_set('implicit_flush', 1);
71 6b07c15a Matthew Grooms
72 93e1b16c Scott Ullrich
require("guiconfig.inc");
73 6090e85b Bill Marquette
74 fc259334 Seth Mos
function leasecmp($a, $b) {
75
	return strcmp($a[$_GET['order']], $b[$_GET['order']]);
76
}
77 93e1b16c Scott Ullrich
78 fc259334 Seth Mos
function adjust_gmt($dt) {
79
	$ts = strtotime($dt . " GMT");
80
	return strftime("%Y/%m/%d %H:%M:%S", $ts);
81
}
82 93e1b16c Scott Ullrich
83 fc259334 Seth Mos
function remove_duplicate($array, $field) {
84 5f601060 Phil Davis
	foreach ($array as $sub) {
85 20b9b335 Scott Ullrich
		$cmp[] = $sub[$field];
86 5f601060 Phil Davis
	}
87 fc259334 Seth Mos
	$unique = array_unique($cmp);
88 5f601060 Phil Davis
	foreach ($unique as $k => $rien) {
89 fc259334 Seth Mos
		$new[] = $array[$k];
90 5f601060 Phil Davis
	}
91 fc259334 Seth Mos
	return $new;
92
}
93 93e1b16c Scott Ullrich
94 20b9b335 Scott Ullrich
// Define path to AWK
95 fc259334 Seth Mos
$awk = "/usr/bin/awk";
96 20b9b335 Scott Ullrich
97
// Read in leases file
98
$leasesfile = "{$g['dhcpd_chroot_path']}/var/db/dhcpd.leases";
99
100 fc259334 Seth Mos
/* this pattern sticks comments into a single array item */
101
$cleanpattern = "'{ gsub(\"#.*\", \"\");} { gsub(\";\", \"\"); print;}'";
102 20b9b335 Scott Ullrich
103 fc259334 Seth Mos
/* We then split the leases file by } */
104
$splitpattern = "'BEGIN { RS=\"}\";} {for (i=1; i<=NF; i++) printf \"%s \", \$i; printf \"}\\n\";}'";
105 93e1b16c Scott Ullrich
106 5f601060 Phil Davis
/* stuff the leases file in a proper format into an array by line */
107 fc259334 Seth Mos
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 5f601060 Phil Davis
while ($i < $leases_count) {
117 b45630bf jim-p
	/* 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 5f601060 Phil Davis
	if ($fcount < 20) {
124 b45630bf jim-p
		$i++;
125
		continue;
126
	}
127 5f601060 Phil Davis
	while ($f < $fcount) {
128
		switch ($data[$f]) {
129 b45630bf jim-p
			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 699737d9 Phil Davis
				switch ($data[$f+2]) {
169 b45630bf jim-p
					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 f1273b82 jim-p
			case "rewind":
188
				/* skip the rewind binding statement */
189
				$f = $f+3;
190
				break;
191 b45630bf jim-p
			case "hardware":
192
				$leases[$l]['mac'] = $data[$f+2];
193
				/* check if it's online and the lease is active */
194 5f601060 Phil Davis
				if ($leases[$l]['act'] == "active") {
195 b45630bf jim-p
					$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 5f601060 Phil Davis
				if ($data[$f+1] <> "") {
206 699737d9 Phil Davis
					$leases[$l]['hostname'] = preg_replace('/"/', '', $data[$f+1]);
207 b45630bf jim-p
				} else {
208
					$hostname = gethostbyaddr($leases[$l]['ip']);
209 5f601060 Phil Davis
					if ($hostname <> "") {
210 b45630bf jim-p
						$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 fc259334 Seth Mos
}
224
225
/* remove duplicate items by mac address */
226 5f601060 Phil Davis
if (count($leases) > 0) {
227 699737d9 Phil Davis
	$leases = remove_duplicate($leases, "ip");
228 fc259334 Seth Mos
}
229
230 5f601060 Phil Davis
if (count($pools) > 0) {
231 699737d9 Phil Davis
	$pools = remove_duplicate($pools, "name");
232 b45630bf jim-p
	asort($pools);
233 fc259334 Seth Mos
}
234
235
// Put this in an easy to use form
236
$dhcpmac = array();
237
$dhcpip = array();
238 f8ec8de4 Renato Botelho
239 fc259334 Seth Mos
foreach ($leases as $value) {
240 f8ec8de4 Renato Botelho
	$dhcpmac[$value['mac']] = $value['hostname'];
241
	$dhcpip[$value['ip']] = $value['hostname'];
242 93e1b16c Scott Ullrich
}
243
244 699737d9 Phil Davis
exec("/usr/sbin/arp -an", $rawdata);
245 93e1b16c Scott Ullrich
246 f8ec8de4 Renato Botelho
$i = 0;
247 cbe3ea96 Ermal Luçi
248
/* if list */
249
$ifdescrs = get_configured_interface_with_descr();
250 93e1b16c Scott Ullrich
251 e7237dd0 jim-p
foreach ($ifdescrs as $key => $interface) {
252
	$thisif = convert_friendly_interface_to_real_interface_name($key);
253 5f601060 Phil Davis
	if (!empty($thisif)) {
254 e7237dd0 jim-p
		$hwif[$thisif] = $interface;
255 5f601060 Phil Davis
	}
256 93e1b16c Scott Ullrich
}
257
258
$data = array();
259
foreach ($rawdata as $line) {
260 699737d9 Phil Davis
	$elements = explode(' ', $line);
261 f8ec8de4 Renato Botelho
262 93e1b16c Scott Ullrich
	if ($elements[3] != "(incomplete)") {
263
		$arpent = array();
264 699737d9 Phil Davis
		$arpent['ip'] = trim(str_replace(array('(', ')'), '', $elements[1]));
265 93e1b16c Scott Ullrich
		$arpent['mac'] = trim($elements[3]);
266
		$arpent['interface'] = trim($elements[5]);
267
		$data[] = $arpent;
268
	}
269
}
270
271 699737d9 Phil Davis
function _getHostName($mac, $ip) {
272 93e1b16c Scott Ullrich
	global $dhcpmac, $dhcpip;
273 f8ec8de4 Renato Botelho
274 5f601060 Phil Davis
	if ($dhcpmac[$mac]) {
275 93e1b16c Scott Ullrich
		return $dhcpmac[$mac];
276 5f601060 Phil Davis
	} else if ($dhcpip[$ip]) {
277 93e1b16c Scott Ullrich
		return $dhcpip[$ip];
278 5f601060 Phil Davis
	} else {
279 d31ca336 Renato Botelho
		exec("host -W 1 " . escapeshellarg($ip), $output);
280 699737d9 Phil Davis
		if (preg_match('/.*pointer ([A-Za-z_0-9.-]+)\..*/', $output[0], $matches)) {
281 5f601060 Phil Davis
			if ($matches[1] <> $ip) {
282 7d5b007c Sjon Hortensius
				return $matches[1];
283 5f601060 Phil Davis
			}
284 dd4bded7 Evgeny Yurchenko
		}
285
	}
286
	return "";
287 93e1b16c Scott Ullrich
}
288
289 699737d9 Phil Davis
$pgtitle = array(gettext("Diagnostics"), gettext("ARP Table"));
290 fc259334 Seth Mos
include("head.inc");
291 20b9b335 Scott Ullrich
292 93e1b16c Scott Ullrich
?>
293 20b9b335 Scott Ullrich
294 a4af095c Renato Botelho
<!-- On modern hardware the table will load so fast you may never see this! -->
295 4a993c8f Scott Ullrich
<div id="loading">
296 a4af095c Renato Botelho
	<?= gettext(" Loading, please wait...")?>
297 4a993c8f Scott Ullrich
</div>
298
299
<?php
300
301 868ba36d Scott Ullrich
// Flush buffers out to client so that they see Loading, please wait....
302 5f601060 Phil Davis
for ($i = 0; $i < ob_get_level(); $i++) {
303
	ob_end_flush();
304
}
305 868ba36d Scott Ullrich
ob_implicit_flush(1);
306
307 20b9b335 Scott Ullrich
// 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 dd4bded7 Evgeny Yurchenko
$dnsavailable=1;
311 7d5b007c Sjon Hortensius
$dns = trim(_getHostName("", "8.8.8.8"));
312 5f601060 Phil Davis
if ($dns == "") {
313 7d5b007c Sjon Hortensius
	$dns = trim(_getHostName("", "8.8.4.4"));
314 5f601060 Phil Davis
	if ($dns == "") {
315
		$dnsavailable = 0;
316
	}
317 dd4bded7 Evgeny Yurchenko
}
318
319 4a993c8f Scott Ullrich
foreach ($data as &$entry) {
320 5f601060 Phil Davis
	if ($dnsavailable) {
321 dd4bded7 Evgeny Yurchenko
		$dns = trim(_getHostName($entry['mac'], $entry['ip']));
322 5f601060 Phil Davis
	} else {
323 dd4bded7 Evgeny Yurchenko
		$dns="";
324 5f601060 Phil Davis
	}
325
	if (trim($dns)) {
326 4a993c8f Scott Ullrich
		$entry['dnsresolve'] = "$dns";
327 5f601060 Phil Davis
	} else {
328 4a993c8f Scott Ullrich
		$entry['dnsresolve'] = "Z_ ";
329 5f601060 Phil Davis
	}
330 4a993c8f Scott Ullrich
}
331 20b9b335 Scott Ullrich
332
// Sort the data alpha first
333 4a993c8f Scott Ullrich
$data = msort($data, "dnsresolve");
334
335 57f2840e Evgeny
// Load MAC-Manufacturer table
336
$mac_man = load_mac_manufacturer_table();
337 4a993c8f Scott Ullrich
?>
338 89f64f0f Sander van Leeuwen
<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 a4af095c Renato Botelho
<script>
371 293ceb87 Colin Fleming
//<![CDATA[
372 a4af095c Renato Botelho
// Clear the "loading" div once the page has loaded"
373
events.push(function(){   
374
	$('#loading').empty();
375
});
376
//]]>  
377 4a993c8f Scott Ullrich
</script>
378 a4af095c Renato Botelho
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 93e1b16c Scott Ullrich
382 a4af095c Renato Botelho
include("foot.inc")?>