Project

General

Profile

Download (9.64 KB) Statistics
| Branch: | Tag: | Revision:
1 93e1b16c Scott Ullrich
<?php
2
/*
3
	diag_arp.php
4
	part of m0n0wall (http://m0n0.ch/wall)
5
6
	Copyright (C) 2005 Paul Taylor (paultaylor@winndixie.com) and Manuel Kasper <mk@neon1.net>.
7
	All rights reserved.
8
9
	Redistribution and use in source and binary forms, with or without
10
	modification, 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 the
17
	documentation and/or other materials provided with the distribution.
18
19
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
20
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
21
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
23
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
	POSSIBILITY OF SUCH DAMAGE.
29
*/
30
31 6b07c15a Matthew Grooms
##|+PRIV
32
##|*IDENT=page-diagnostics-arptable
33
##|*NAME=Diagnostics: ARP Table page
34
##|*DESCR=Allow access to the 'Diagnostics: ARP Table' page.
35
##|*MATCH=diag_arp.php*
36
##|-PRIV
37
38
39 93e1b16c Scott Ullrich
require("guiconfig.inc");
40 6090e85b Bill Marquette
41 fc259334 Seth Mos
function leasecmp($a, $b) {
42
	return strcmp($a[$_GET['order']], $b[$_GET['order']]);
43
}
44 93e1b16c Scott Ullrich
45 fc259334 Seth Mos
function adjust_gmt($dt) {
46
	$ts = strtotime($dt . " GMT");
47
	return strftime("%Y/%m/%d %H:%M:%S", $ts);
48
}
49 93e1b16c Scott Ullrich
50 fc259334 Seth Mos
function remove_duplicate($array, $field) {
51
foreach ($array as $sub)
52
	$cmp[] = $sub[$field];
53
	$unique = array_unique($cmp);
54
	foreach ($unique as $k => $rien)
55
		$new[] = $array[$k];
56
	return $new;
57
}
58 93e1b16c Scott Ullrich
59 fc259334 Seth Mos
$leasesfile = "{$g['dhcpd_chroot_path']}/var/db/dhcpd.leases";
60
$awk = "/usr/bin/awk";
61
/* this pattern sticks comments into a single array item */
62
$cleanpattern = "'{ gsub(\"#.*\", \"\");} { gsub(\";\", \"\"); print;}'";
63
/* We then split the leases file by } */
64
$splitpattern = "'BEGIN { RS=\"}\";} {for (i=1; i<=NF; i++) printf \"%s \", \$i; printf \"}\\n\";}'";
65 93e1b16c Scott Ullrich
66 fc259334 Seth Mos
/* stuff the leases file in a proper format into a array by line */
67
exec("cat {$leasesfile} | {$awk} {$cleanpattern} | {$awk} {$splitpattern}", $leases_content);
68
$leases_count = count($leases_content);
69
70
$pools = array();
71
$leases = array();
72
$i = 0;
73
$l = 0;
74
$p = 0;
75
// Put everything together again
76
while($i < $leases_count) {
77
        /* split the line by space */
78
        $data = explode(" ", $leases_content[$i]);
79
        /* walk the fields */
80
        $f = 0;
81
        $fcount = count($data);
82
        /* with less then 20 fields there is nothing useful */
83
        if($fcount < 20) {
84
                $i++;
85
                continue;
86
        }
87
        while($f < $fcount) {
88
                switch($data[$f]) {
89
                        case "failover":
90
                                $pools[$p]['name'] = $data[$f+2];
91
                                $pools[$p]['mystate'] = $data[$f+7];
92
                                $pools[$p]['peerstate'] = $data[$f+14];
93
                                $pools[$p]['mydate'] = $data[$f+10];
94
                                $pools[$p]['mydate'] .= " " . $data[$f+11];
95
                                $pools[$p]['peerdate'] = $data[$f+17];
96
                                $pools[$p]['peerdate'] .= " " . $data[$f+18];
97
                                $p++;
98
                                $i++;
99
                                continue 3;
100
                        case "lease":
101
                                $leases[$l]['ip'] = $data[$f+1];
102
                                $leases[$l]['type'] = "dynamic";
103
                                $f = $f+2;
104
                                break;
105
                        case "starts":
106
                                $leases[$l]['start'] = $data[$f+2];
107
                                $leases[$l]['start'] .= " " . $data[$f+3];
108
                                $f = $f+3;
109
                                break;
110
                        case "ends":
111
                                $leases[$l]['end'] = $data[$f+2];
112
                                $leases[$l]['end'] .= " " . $data[$f+3];
113
                                $f = $f+3;
114
                                break;
115
                        case "tstp":
116
                                $f = $f+3;
117
                                break;
118
                        case "tsfp":
119
                                $f = $f+3;
120
                                break;
121
                        case "atsfp":
122
                                $f = $f+3;
123
                                break;
124
                        case "cltt":
125
                                $f = $f+3;
126
                                break;
127
                        case "binding":
128
                                switch($data[$f+2]) {
129
                                        case "active":
130
                                                $leases[$l]['act'] = "active";
131
                                                break;
132
                                        case "free":
133
                                                $leases[$l]['act'] = "expired";
134
                                                $leases[$l]['online'] = "offline";
135
                                                break;
136
                                        case "backup":
137
                                                $leases[$l]['act'] = "reserved";
138
                                                $leases[$l]['online'] = "offline";
139
                                                break;
140
                                }
141
                                $f = $f+1;
142
                                break;
143
                        case "next":
144
                                /* skip the next binding statement */
145
                                $f = $f+3;
146
                                break;
147
                        case "hardware":
148
                                $leases[$l]['mac'] = $data[$f+2];
149
                                /* check if it's online and the lease is active */
150
                                if($leases[$l]['act'] == "active") {
151
                                        $online = exec("/usr/sbin/arp -an |/usr/bin/awk '/{$leases[$l]['ip']}/ {print}'|wc -l");
152
                                        if ($online == 1) {
153
                                                $leases[$l]['online'] = 'online';
154
                                        } else {
155
                                                $leases[$l]['online'] = 'offline';
156
                                        }
157
                                }
158
                                $f = $f+2;
159
                                break;
160
                        case "client-hostname":
161
                                if($data[$f+1] <> "") {
162
                                        $leases[$l]['hostname'] = preg_replace('/"/','',$data[$f+1]);
163
                                } else {
164
                                        $hostname = gethostbyaddr($leases[$l]['ip']);
165
                                        if($hostname <> "") {
166
                                                $leases[$l]['hostname'] = $hostname;
167
                                        }
168
                                }
169
                                $f = $f+1;
170
                                break;
171
                        case "uid":
172
                                $f = $f+1;
173
                                break;
174
                }
175
                $f++;
176
        }
177
        $l++;
178
        $i++;
179
}
180
181
/* remove duplicate items by mac address */
182
if(count($leases) > 0) {
183
        $leases = remove_duplicate($leases,"ip");
184
}
185
186
if(count($pools) > 0) {
187
        $pools = remove_duplicate($pools,"name");
188
        asort($pools);
189
}
190
191
192
// Put this in an easy to use form
193
$dhcpmac = array();
194
$dhcpip = array();
195 93e1b16c Scott Ullrich
	
196 fc259334 Seth Mos
foreach ($leases as $value) {
197
	$dhcpmac[$value['mac']] = $value['hostname'];	
198
	$dhcpip[$value['ip']] = $value['hostname'];	
199 93e1b16c Scott Ullrich
}
200
201
exec("/usr/sbin/arp -an",$rawdata);
202
203
$i = 0; 
204 cbe3ea96 Ermal Luçi
205
/* if list */
206
$ifdescrs = get_configured_interface_with_descr();
207 93e1b16c Scott Ullrich
208
foreach ($ifdescrs as $key =>$interface) {
209
	$hwif[$config['interfaces'][$key]['if']] = $interface;
210
}
211
212
$data = array();
213
foreach ($rawdata as $line) {
214
	$elements = explode(' ',$line);
215
	
216
	if ($elements[3] != "(incomplete)") {
217
		$arpent = array();
218
		$arpent['ip'] = trim(str_replace(array('(',')'),'',$elements[1]));
219
		$arpent['mac'] = trim($elements[3]);
220
		$arpent['interface'] = trim($elements[5]);
221
		$data[] = $arpent;
222
	}
223
}
224
225
function getHostName($mac,$ip)
226
{
227
	global $dhcpmac, $dhcpip;
228
	
229
	if ($dhcpmac[$mac])
230
		return $dhcpmac[$mac];
231
	else if ($dhcpip[$ip])
232
		return $dhcpip[$ip];
233 4dedef58 Scott Ullrich
	else if(gethostbyaddr($ip) <> "" and gethostbyaddr($ip) <> $ip)
234
		return gethostbyaddr($ip);
235 93e1b16c Scott Ullrich
	else 
236
		return "&nbsp;";	
237
}
238
239 d88c6a9f Scott Ullrich
$pgtitle = array("Diagnostics","ARP Table");
240 fc259334 Seth Mos
include("head.inc");
241 93e1b16c Scott Ullrich
?>
242 fc259334 Seth Mos
<body link="#000000" vlink="#000000" alink="#000000">
243
<? include("fbegin.inc"); ?>
244
<table width="100%" border="0" cellpadding="0" cellspacing="0">
245
        <tr>
246
                <td>
247 ff9d6728 Scott Ullrich
<table class="tabcont" width="100%" border="0" cellpadding="0" cellspacing="0">
248 93e1b16c Scott Ullrich
  <tr>
249
    <td class="listhdrr">IP address</td>
250
    <td class="listhdrr">MAC address</td>
251
    <td class="listhdrr">Hostname</td>
252
    <td class="listhdr">Interface</td>
253
    <td class="list"></td>
254
  </tr>
255
<?php foreach ($data as $entry): ?>
256
  <tr>
257
    <td class="listlr"><?=$entry['ip'];?></td>
258
    <td class="listr"><?=$entry['mac'];?></td>
259
    <td class="listr"><?=getHostName($entry['mac'], $entry['ip']);?></td>
260
    <td class="listr"><?=$hwif[$entry['interface']];?></td>
261
  </tr>
262
<?php endforeach; ?>
263
</table>
264 6090e85b Bill Marquette
</td></tr></table>
265 93e1b16c Scott Ullrich
266
<?php include("fend.inc"); ?>