Projet

Général

Profil

Télécharger (9,26 ko) Statistiques
| Branche: | Tag: | Révision:

univnautes / usr / local / www / diag_arp.php @ 293ceb87

1
<?php
2
/*
3
	diag_arp.php
4
	part of the pfSense project	(https://www.pfsense.org)
5
	Copyright (C) 2004-2009 Scott Ullrich <sullrich@gmail.com>
6

    
7
	originally part of m0n0wall (http://m0n0.ch/wall)
8
	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
/*
34
	pfSense_BUILDER_BINARIES:	/bin/cat		/usr/sbin/arp
35
	pfSense_MODULE:	arp
36
*/
37

    
38
##|+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
@ini_set('zlib.output_compression', 0);
46
@ini_set('implicit_flush', 1);
47

    
48
require("guiconfig.inc");
49

    
50
function leasecmp($a, $b) {
51
	return strcmp($a[$_GET['order']], $b[$_GET['order']]);
52
}
53

    
54
function adjust_gmt($dt) {
55
	$ts = strtotime($dt . " GMT");
56
	return strftime("%Y/%m/%d %H:%M:%S", $ts);
57
}
58

    
59
function remove_duplicate($array, $field) {
60
	foreach ($array as $sub)
61
		$cmp[] = $sub[$field];
62
	$unique = array_unique($cmp);
63
	foreach ($unique as $k => $rien)
64
		$new[] = $array[$k];
65
	return $new;
66
}
67

    
68
// Define path to AWK
69
$awk = "/usr/bin/awk";
70

    
71
// Read in leases file
72
$leasesfile = "{$g['dhcpd_chroot_path']}/var/db/dhcpd.leases";
73

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

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

    
80
/* 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
	/* 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
			case "rewind":
162
				/* skip the rewind binding statement */
163
				$f = $f+3;
164
				break;
165
			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
}
198

    
199
/* remove duplicate items by mac address */
200
if(count($leases) > 0) {
201
	$leases = remove_duplicate($leases,"ip");
202
}
203

    
204
if(count($pools) > 0) {
205
	$pools = remove_duplicate($pools,"name");
206
	asort($pools);
207
}
208

    
209
// Put this in an easy to use form
210
$dhcpmac = array();
211
$dhcpip = array();
212

    
213
foreach ($leases as $value) {
214
	$dhcpmac[$value['mac']] = $value['hostname'];
215
	$dhcpip[$value['ip']] = $value['hostname'];
216
}
217

    
218
exec("/usr/sbin/arp -an",$rawdata);
219

    
220
$i = 0;
221

    
222
/* if list */
223
$ifdescrs = get_configured_interface_with_descr();
224

    
225
foreach ($ifdescrs as $key => $interface) {
226
	$thisif = convert_friendly_interface_to_real_interface_name($key);
227
	if (!empty($thisif))
228
		$hwif[$thisif] = $interface;
229
}
230

    
231
$data = array();
232
foreach ($rawdata as $line) {
233
	$elements = explode(' ',$line);
234

    
235
	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
function _getHostName($mac,$ip) {
245
	global $dhcpmac, $dhcpip;
246

    
247
	if ($dhcpmac[$mac])
248
		return $dhcpmac[$mac];
249
	else if ($dhcpip[$ip])
250
		return $dhcpip[$ip];
251
	else{
252
		exec("host -W 1 " . escapeshellarg($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
}
260

    
261
$pgtitle = array(gettext("Diagnostics"),gettext("ARP Table"));
262
include("head.inc");
263

    
264
?>
265

    
266
<body link="#000000" vlink="#000000" alink="#000000">
267

    
268
<?php include("fbegin.inc"); ?>
269

    
270
<div id="loading">
271
	<img src="/themes/<?=$g['theme'];?>/images/misc/loader.gif" alt="loader" /><?= gettext("Loading, please wait..."); ?>
272
	<p>&nbsp;</p>
273
</div>
274

    
275
<?php
276

    
277
// 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
// 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
$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
foreach ($data as &$entry) {
292
	if ($dnsavailable){
293
		$dns = trim(_getHostName($entry['mac'], $entry['ip']));
294
	}else
295
		$dns="";
296
	if(trim($dns))
297
		$entry['dnsresolve'] = "$dns";
298
	else
299
		$entry['dnsresolve'] = "Z_ ";
300
}
301

    
302
// Sort the data alpha first
303
$data = msort($data, "dnsresolve");
304

    
305
// Load MAC-Manufacturer table
306
$mac_man = load_mac_manufacturer_table();
307
?>
308
<table width="100%" border="0" cellpadding="0" cellspacing="0" summary="diag arp">
309
	<tr>
310
		<td>
311
			<table class="tabcont sortable" width="100%" border="0" cellpadding="0" cellspacing="0" summary="tabcont">
312
				<tr>
313
					<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
					<td class="list"></td>
318
				</tr>
319
				<?php foreach ($data as $entry): ?>
320
					<tr>
321
						<td class="listlr"><?=$entry['ip'];?></td>
322
						<td class="listr">
323
						<?php
324
						$mac=trim($entry['mac']);
325
						$mac_hi = strtoupper($mac[0] . $mac[1] . $mac[3] . $mac[4] . $mac[6] . $mac[7]);
326
						print $mac;
327
						if(isset($mac_man[$mac_hi])){ print "<br /><font size=\"-2\"><i>{$mac_man[$mac_hi]}</i></font>"; }
328
						?>
329
						</td>
330
						<td class="listr">
331
							<?php
332
							echo trim(str_replace("Z_ ", "", $entry['dnsresolve']));
333
							?>
334
						</td>
335
						<td class="listr"><?=$hwif[$entry['interface']];?></td>
336
					</tr>
337
				<?php endforeach; ?>
338
			</table>
339
		</td>
340
	</tr>
341
	<tr>
342
		<td><br /><?= gettext("NOTE: Local IPv6 peers use") ?> <a href="diag_ndp.php"><?= gettext("NDP") ?></a> <?= gettext("instead of ARP") ?>.</td>
343
	</tr>
344
</table>
345

    
346
<?php include("fend.inc"); ?>
347

    
348
<script type="text/javascript">
349
//<![CDATA[
350
	jQuery('#loading').html('');
351
//]]>
352
</script>
353
</body>
354
</html>
(5-5/254)