Project

General

Profile

Download (9.73 KB) Statistics
| Branch: | Tag: | Revision:
1
<?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
##|+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
require("guiconfig.inc");
40

    
41
function leasecmp($a, $b) {
42
	return strcmp($a[$_GET['order']], $b[$_GET['order']]);
43
}
44

    
45
function adjust_gmt($dt) {
46
	$ts = strtotime($dt . " GMT");
47
	return strftime("%Y/%m/%d %H:%M:%S", $ts);
48
}
49

    
50
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

    
59
$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

    
66
/* 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
	
196
foreach ($leases as $value) {
197
	$dhcpmac[$value['mac']] = $value['hostname'];	
198
	$dhcpip[$value['ip']] = $value['hostname'];	
199
}
200

    
201
exec("/usr/sbin/arp -an",$rawdata);
202

    
203
$i = 0; 
204

    
205
/* if list */
206
$ifdescrs = get_configured_interface_with_descr();
207

    
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
	else if(gethostbyaddr($ip) <> "" and gethostbyaddr($ip) <> $ip)
234
		return gethostbyaddr($ip);
235
	else 
236
		return "&nbsp;";	
237
}
238

    
239
$pgtitle = array("Diagnostics","ARP Table");
240
include("head.inc");
241
?>
242
<body link="#000000" vlink="#000000" alink="#000000">
243
<script src="/javascript/sorttable.js"></script>
244
<? include("fbegin.inc"); ?>
245
<table width="100%" border="0" cellpadding="0" cellspacing="0">
246
        <tr>
247
                <td>
248
<table class="sortable" name="sortabletable" id="sortabletable" width="100%" border="0" cellpadding="0" cellspacing="0">
249
  <tr>
250
    <td class="listhdrr">IP address</td>
251
    <td class="listhdrr">MAC address</td>
252
    <td class="listhdrr">Hostname</td>
253
    <td class="listhdr">Interface</td>
254
    <td class="list"></td>
255
  </tr>
256
<?php foreach ($data as $entry): ?>
257
  <tr>
258
    <td class="listlr"><?=$entry['ip'];?></td>
259
    <td class="listr"><?=$entry['mac'];?></td>
260
    <td class="listr"><?=getHostName($entry['mac'], $entry['ip']);?></td>
261
    <td class="listr"><?=$hwif[$entry['interface']];?></td>
262
  </tr>
263
<?php endforeach; ?>
264
</table>
265
</td></tr></table>
266

    
267
<?php include("fend.inc"); ?>
(3-3/217)