Project

General

Profile

Download (9 KB) Statistics
| Branch: | Tag: | Revision:
1 93e1b16c Scott Ullrich
<?php
2
/*
3
	diag_arp.php
4 20b9b335 Scott Ullrich
	part of the pfSense project	(http://www.pfsense.org)
5 13d193c2 Scott Ullrich
	Copyright (C) 2004-2009 Scott Ullrich <sullrich@gmail.com>
6 93e1b16c Scott Ullrich
7 20b9b335 Scott Ullrich
	originally part of m0n0wall (http://m0n0.ch/wall)
8 93e1b16c Scott Ullrich
	Copyright (C) 2005 Paul Taylor (paultaylor@winndixie.com) and Manuel Kasper <mk@neon1.net>.
9
	All rights reserved.
10
11
	Redistribution and use in source and binary forms, with or without
12
	modification, 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 the
19
	documentation and/or other materials provided with the distribution.
20
21
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
22
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
23
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
25
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
	POSSIBILITY OF SUCH DAMAGE.
31
*/
32
33 13d193c2 Scott Ullrich
/*
34
	pfSense_BUILDER_BINARIES:	/bin/cat		/usr/sbin/arp
35
	pfSense_MODULE:	arp
36
*/
37
38 6b07c15a Matthew Grooms
##|+PRIV
39
##|*IDENT=page-diagnostics-arptable
40
##|*NAME=Diagnostics: ARP Table page
41
##|*DESCR=Allow access to the 'Diagnostics: ARP Table' page.
42
##|*MATCH=diag_arp.php*
43
##|-PRIV
44
45 868ba36d Scott Ullrich
@ini_set('zlib.output_compression', 0);
46
@ini_set('implicit_flush', 1);
47 6b07c15a Matthew Grooms
48 93e1b16c Scott Ullrich
require("guiconfig.inc");
49 6090e85b Bill Marquette
50 fc259334 Seth Mos
function leasecmp($a, $b) {
51
	return strcmp($a[$_GET['order']], $b[$_GET['order']]);
52
}
53 93e1b16c Scott Ullrich
54 fc259334 Seth Mos
function adjust_gmt($dt) {
55
	$ts = strtotime($dt . " GMT");
56
	return strftime("%Y/%m/%d %H:%M:%S", $ts);
57
}
58 93e1b16c Scott Ullrich
59 fc259334 Seth Mos
function remove_duplicate($array, $field) {
60 20b9b335 Scott Ullrich
	foreach ($array as $sub)
61
		$cmp[] = $sub[$field];
62 fc259334 Seth Mos
	$unique = array_unique($cmp);
63
	foreach ($unique as $k => $rien)
64
		$new[] = $array[$k];
65
	return $new;
66
}
67 93e1b16c Scott Ullrich
68 20b9b335 Scott Ullrich
// Define path to AWK
69 fc259334 Seth Mos
$awk = "/usr/bin/awk";
70 20b9b335 Scott Ullrich
71
// Read in leases file
72
$leasesfile = "{$g['dhcpd_chroot_path']}/var/db/dhcpd.leases";
73
74 fc259334 Seth Mos
/* this pattern sticks comments into a single array item */
75
$cleanpattern = "'{ gsub(\"#.*\", \"\");} { gsub(\";\", \"\"); print;}'";
76 20b9b335 Scott Ullrich
77 fc259334 Seth Mos
/* We then split the leases file by } */
78
$splitpattern = "'BEGIN { RS=\"}\";} {for (i=1; i<=NF; i++) printf \"%s \", \$i; printf \"}\\n\";}'";
79 93e1b16c Scott Ullrich
80 fc259334 Seth Mos
/* stuff the leases file in a proper format into a array by line */
81
exec("cat {$leasesfile} | {$awk} {$cleanpattern} | {$awk} {$splitpattern}", $leases_content);
82
$leases_count = count($leases_content);
83
84
$pools = array();
85
$leases = array();
86
$i = 0;
87
$l = 0;
88
$p = 0;
89
// Put everything together again
90
while($i < $leases_count) {
91 b45630bf jim-p
	/* split the line by space */
92
	$data = explode(" ", $leases_content[$i]);
93
	/* walk the fields */
94
	$f = 0;
95
	$fcount = count($data);
96
	/* with less then 20 fields there is nothing useful */
97
	if($fcount < 20) {
98
		$i++;
99
		continue;
100
	}
101
	while($f < $fcount) {
102
		switch($data[$f]) {
103
			case "failover":
104
				$pools[$p]['name'] = $data[$f+2];
105
				$pools[$p]['mystate'] = $data[$f+7];
106
				$pools[$p]['peerstate'] = $data[$f+14];
107
				$pools[$p]['mydate'] = $data[$f+10];
108
				$pools[$p]['mydate'] .= " " . $data[$f+11];
109
				$pools[$p]['peerdate'] = $data[$f+17];
110
				$pools[$p]['peerdate'] .= " " . $data[$f+18];
111
				$p++;
112
				$i++;
113
				continue 3;
114
			case "lease":
115
				$leases[$l]['ip'] = $data[$f+1];
116
				$leases[$l]['type'] = "dynamic";
117
				$f = $f+2;
118
				break;
119
			case "starts":
120
				$leases[$l]['start'] = $data[$f+2];
121
				$leases[$l]['start'] .= " " . $data[$f+3];
122
				$f = $f+3;
123
				break;
124
			case "ends":
125
				$leases[$l]['end'] = $data[$f+2];
126
				$leases[$l]['end'] .= " " . $data[$f+3];
127
				$f = $f+3;
128
				break;
129
			case "tstp":
130
				$f = $f+3;
131
				break;
132
			case "tsfp":
133
				$f = $f+3;
134
				break;
135
			case "atsfp":
136
				$f = $f+3;
137
				break;
138
			case "cltt":
139
				$f = $f+3;
140
				break;
141
			case "binding":
142
				switch($data[$f+2]) {
143
					case "active":
144
						$leases[$l]['act'] = "active";
145
						break;
146
					case "free":
147
						$leases[$l]['act'] = "expired";
148
						$leases[$l]['online'] = "offline";
149
						break;
150
					case "backup":
151
						$leases[$l]['act'] = "reserved";
152
						$leases[$l]['online'] = "offline";
153
						break;
154
				}
155
				$f = $f+1;
156
				break;
157
			case "next":
158
				/* skip the next binding statement */
159
				$f = $f+3;
160
				break;
161 f1273b82 jim-p
			case "rewind":
162
				/* skip the rewind binding statement */
163
				$f = $f+3;
164
				break;
165 b45630bf jim-p
			case "hardware":
166
				$leases[$l]['mac'] = $data[$f+2];
167
				/* check if it's online and the lease is active */
168
				if($leases[$l]['act'] == "active") {
169
					$online = exec("/usr/sbin/arp -an |/usr/bin/awk '/{$leases[$l]['ip']}/ {print}'|wc -l");
170
					if ($online == 1) {
171
						$leases[$l]['online'] = 'online';
172
					} else {
173
						$leases[$l]['online'] = 'offline';
174
					}
175
				}
176
				$f = $f+2;
177
				break;
178
			case "client-hostname":
179
				if($data[$f+1] <> "") {
180
					$leases[$l]['hostname'] = preg_replace('/"/','',$data[$f+1]);
181
				} else {
182
					$hostname = gethostbyaddr($leases[$l]['ip']);
183
					if($hostname <> "") {
184
						$leases[$l]['hostname'] = $hostname;
185
					}
186
				}
187
				$f = $f+1;
188
				break;
189
			case "uid":
190
				$f = $f+1;
191
				break;
192
		}
193
		$f++;
194
	}
195
	$l++;
196
	$i++;
197 fc259334 Seth Mos
}
198
199
/* remove duplicate items by mac address */
200
if(count($leases) > 0) {
201 b45630bf jim-p
	$leases = remove_duplicate($leases,"ip");
202 fc259334 Seth Mos
}
203
204
if(count($pools) > 0) {
205 b45630bf jim-p
	$pools = remove_duplicate($pools,"name");
206
	asort($pools);
207 fc259334 Seth Mos
}
208
209
// Put this in an easy to use form
210
$dhcpmac = array();
211
$dhcpip = array();
212 f8ec8de4 Renato Botelho
213 fc259334 Seth Mos
foreach ($leases as $value) {
214 f8ec8de4 Renato Botelho
	$dhcpmac[$value['mac']] = $value['hostname'];
215
	$dhcpip[$value['ip']] = $value['hostname'];
216 93e1b16c Scott Ullrich
}
217
218
exec("/usr/sbin/arp -an",$rawdata);
219
220 f8ec8de4 Renato Botelho
$i = 0;
221 cbe3ea96 Ermal Luçi
222
/* if list */
223
$ifdescrs = get_configured_interface_with_descr();
224 93e1b16c Scott Ullrich
225 e7237dd0 jim-p
foreach ($ifdescrs as $key => $interface) {
226
	$thisif = convert_friendly_interface_to_real_interface_name($key);
227
	if (!empty($thisif))
228
		$hwif[$thisif] = $interface;
229 93e1b16c Scott Ullrich
}
230
231
$data = array();
232
foreach ($rawdata as $line) {
233
	$elements = explode(' ',$line);
234 f8ec8de4 Renato Botelho
235 93e1b16c Scott Ullrich
	if ($elements[3] != "(incomplete)") {
236
		$arpent = array();
237
		$arpent['ip'] = trim(str_replace(array('(',')'),'',$elements[1]));
238
		$arpent['mac'] = trim($elements[3]);
239
		$arpent['interface'] = trim($elements[5]);
240
		$data[] = $arpent;
241
	}
242
}
243
244 b45630bf jim-p
function _getHostName($mac,$ip) {
245 93e1b16c Scott Ullrich
	global $dhcpmac, $dhcpip;
246 f8ec8de4 Renato Botelho
247 93e1b16c Scott Ullrich
	if ($dhcpmac[$mac])
248
		return $dhcpmac[$mac];
249
	else if ($dhcpip[$ip])
250
		return $dhcpip[$ip];
251 dd4bded7 Evgeny Yurchenko
	else{
252
		exec("host -W 1 $ip", $output);
253
		if (preg_match('/.*pointer ([A-Za-z0-9.-]+)\..*/',$output[0],$matches)) {
254
			if ($matches[1] <> $ip)
255
				return $matches[1]; 
256
		}
257
	}
258
	return "";
259 93e1b16c Scott Ullrich
}
260
261 f8ec8de4 Renato Botelho
$pgtitle = array(gettext("Diagnostics"),gettext("ARP Table"));
262 fc259334 Seth Mos
include("head.inc");
263 20b9b335 Scott Ullrich
264 93e1b16c Scott Ullrich
?>
265 20b9b335 Scott Ullrich
266 fc259334 Seth Mos
<body link="#000000" vlink="#000000" alink="#000000">
267 f8ec8de4 Renato Botelho
268 20b9b335 Scott Ullrich
<?php include("fbegin.inc"); ?>
269 4a993c8f Scott Ullrich
270
<div id="loading">
271 dc9975f4 Warren Baker
	<img src="/themes/<?=$g['theme'];?>/images/misc/loader.gif"><?= gettext("Loading, please wait..."); ?>
272 4a993c8f Scott Ullrich
	<p/>&nbsp;
273
</div>
274
275
<?php
276
277 868ba36d Scott Ullrich
// Flush buffers out to client so that they see Loading, please wait....
278
for ($i = 0; $i < ob_get_level(); $i++) { ob_end_flush(); }
279
ob_implicit_flush(1);
280
281 20b9b335 Scott Ullrich
// Resolve hostnames and replace Z_ with "".  The intention
282
// is to sort the list by hostnames, alpha and then the non
283
// resolvable addresses will appear last in the list.
284 dd4bded7 Evgeny Yurchenko
$dnsavailable=1;
285
$dns = trim(_getHostName("", "8.8.8.8")); 
286
if ($dns == ""){
287
	$dns = trim(_getHostName("", "8.8.4.4")); 
288
	if ($dns == "") $dnsavailable =0;
289
}
290
291 4a993c8f Scott Ullrich
foreach ($data as &$entry) {
292 dd4bded7 Evgeny Yurchenko
	if ($dnsavailable){
293
		$dns = trim(_getHostName($entry['mac'], $entry['ip']));
294
	}else
295
		$dns="";
296 4a993c8f Scott Ullrich
	if(trim($dns))
297
		$entry['dnsresolve'] = "$dns";
298 f8ec8de4 Renato Botelho
	else
299 4a993c8f Scott Ullrich
		$entry['dnsresolve'] = "Z_ ";
300
}
301 20b9b335 Scott Ullrich
302
// Sort the data alpha first
303 4a993c8f Scott Ullrich
$data = msort($data, "dnsresolve");
304
305 57f2840e Evgeny
// Load MAC-Manufacturer table
306
$mac_man = load_mac_manufacturer_table();
307 4a993c8f Scott Ullrich
?>
308 fc259334 Seth Mos
<table width="100%" border="0" cellpadding="0" cellspacing="0">
309 20b9b335 Scott Ullrich
	<tr>
310
		<td>
311
			<table class="tabcont sortable" width="100%" border="0" cellpadding="0" cellspacing="0">
312
				<tr>
313 f8ec8de4 Renato Botelho
					<td class="listhdrr"><?= gettext("IP address"); ?></td>
314
					<td class="listhdrr"><?= gettext("MAC address"); ?></td>
315
					<td class="listhdrr"><?= gettext("Hostname"); ?></td>
316
					<td class="listhdr"><?= gettext("Interface"); ?></td>
317 20b9b335 Scott Ullrich
					<td class="list"></td>
318
				</tr>
319
				<?php foreach ($data as $entry): ?>
320
					<tr>
321
						<td class="listlr"><?=$entry['ip'];?></td>
322 57f2840e Evgeny
						<td class="listr">
323
						<?php
324 34121b0c smos
						$mac=trim($entry['mac']);
325 57f2840e Evgeny
						$mac_hi = strtoupper($mac[0] . $mac[1] . $mac[3] . $mac[4] . $mac[6] . $mac[7]);
326 701989fb jim-p
						print $mac;
327
						if(isset($mac_man[$mac_hi])){ print "<br/><font size=\"-2\"><i>{$mac_man[$mac_hi]}</i></font>"; }
328 57f2840e Evgeny
						?>
329 34121b0c smos
						</td>
330 20b9b335 Scott Ullrich
						<td class="listr">
331
							<?php
332 34121b0c smos
							echo trim(str_replace("Z_ ", "", $entry['dnsresolve']));
333 20b9b335 Scott Ullrich
							?>
334
						</td>
335
						<td class="listr"><?=$hwif[$entry['interface']];?></td>
336
					</tr>
337
				<?php endforeach; ?>
338
			</table>
339
		</td>
340
	</tr>
341 93e1b16c Scott Ullrich
</table>
342
343
<?php include("fend.inc"); ?>
344 4a993c8f Scott Ullrich
345
<script type="text/javascript">
346 ebfc87d6 Vinicius Coque
	jQuery('#loading').html('');
347 4a993c8f Scott Ullrich
</script>