Project

General

Profile

Download (9.71 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
require("guiconfig.inc");
32

    
33
function leasecmp($a, $b) {
34
	return strcmp($a[$_GET['order']], $b[$_GET['order']]);
35
}
36

    
37
function adjust_gmt($dt) {
38
	$ts = strtotime($dt . " GMT");
39
	return strftime("%Y/%m/%d %H:%M:%S", $ts);
40
}
41

    
42
function remove_duplicate($array, $field) {
43
foreach ($array as $sub)
44
	$cmp[] = $sub[$field];
45
	$unique = array_unique($cmp);
46
	foreach ($unique as $k => $rien)
47
		$new[] = $array[$k];
48
	return $new;
49
}
50

    
51
$leasesfile = "{$g['dhcpd_chroot_path']}/var/db/dhcpd.leases";
52
$awk = "/usr/bin/awk";
53
/* this pattern sticks comments into a single array item */
54
$cleanpattern = "'{ gsub(\"#.*\", \"\");} { gsub(\";\", \"\"); print;}'";
55
/* We then split the leases file by } */
56
$splitpattern = "'BEGIN { RS=\"}\";} {for (i=1; i<=NF; i++) printf \"%s \", \$i; printf \"}\\n\";}'";
57

    
58
/* stuff the leases file in a proper format into a array by line */
59
exec("cat {$leasesfile} | {$awk} {$cleanpattern} | {$awk} {$splitpattern}", $leases_content);
60
$leases_count = count($leases_content);
61

    
62
$pools = array();
63
$leases = array();
64
$i = 0;
65
$l = 0;
66
$p = 0;
67
// Put everything together again
68
while($i < $leases_count) {
69
        /* split the line by space */
70
        $data = explode(" ", $leases_content[$i]);
71
        /* walk the fields */
72
        $f = 0;
73
        $fcount = count($data);
74
        /* with less then 20 fields there is nothing useful */
75
        if($fcount < 20) {
76
                $i++;
77
                continue;
78
        }
79
        while($f < $fcount) {
80
                switch($data[$f]) {
81
                        case "failover":
82
                                $pools[$p]['name'] = $data[$f+2];
83
                                $pools[$p]['mystate'] = $data[$f+7];
84
                                $pools[$p]['peerstate'] = $data[$f+14];
85
                                $pools[$p]['mydate'] = $data[$f+10];
86
                                $pools[$p]['mydate'] .= " " . $data[$f+11];
87
                                $pools[$p]['peerdate'] = $data[$f+17];
88
                                $pools[$p]['peerdate'] .= " " . $data[$f+18];
89
                                $p++;
90
                                $i++;
91
                                continue 3;
92
                        case "lease":
93
                                $leases[$l]['ip'] = $data[$f+1];
94
                                $leases[$l]['type'] = "dynamic";
95
                                $f = $f+2;
96
                                break;
97
                        case "starts":
98
                                $leases[$l]['start'] = $data[$f+2];
99
                                $leases[$l]['start'] .= " " . $data[$f+3];
100
                                $f = $f+3;
101
                                break;
102
                        case "ends":
103
                                $leases[$l]['end'] = $data[$f+2];
104
                                $leases[$l]['end'] .= " " . $data[$f+3];
105
                                $f = $f+3;
106
                                break;
107
                        case "tstp":
108
                                $f = $f+3;
109
                                break;
110
                        case "tsfp":
111
                                $f = $f+3;
112
                                break;
113
                        case "atsfp":
114
                                $f = $f+3;
115
                                break;
116
                        case "cltt":
117
                                $f = $f+3;
118
                                break;
119
                        case "binding":
120
                                switch($data[$f+2]) {
121
                                        case "active":
122
                                                $leases[$l]['act'] = "active";
123
                                                break;
124
                                        case "free":
125
                                                $leases[$l]['act'] = "expired";
126
                                                $leases[$l]['online'] = "offline";
127
                                                break;
128
                                        case "backup":
129
                                                $leases[$l]['act'] = "reserved";
130
                                                $leases[$l]['online'] = "offline";
131
                                                break;
132
                                }
133
                                $f = $f+1;
134
                                break;
135
                        case "next":
136
                                /* skip the next binding statement */
137
                                $f = $f+3;
138
                                break;
139
                        case "hardware":
140
                                $leases[$l]['mac'] = $data[$f+2];
141
                                /* check if it's online and the lease is active */
142
                                if($leases[$l]['act'] == "active") {
143
                                        $online = exec("/usr/sbin/arp -an |/usr/bin/awk '/{$leases[$l]['ip']}/ {print}'|wc -l");
144
                                        if ($online == 1) {
145
                                                $leases[$l]['online'] = 'online';
146
                                        } else {
147
                                                $leases[$l]['online'] = 'offline';
148
                                        }
149
                                }
150
                                $f = $f+2;
151
                                break;
152
                        case "client-hostname":
153
                                if($data[$f+1] <> "") {
154
                                        $leases[$l]['hostname'] = preg_replace('/"/','',$data[$f+1]);
155
                                } else {
156
                                        $hostname = gethostbyaddr($leases[$l]['ip']);
157
                                        if($hostname <> "") {
158
                                                $leases[$l]['hostname'] = $hostname;
159
                                        }
160
                                }
161
                                $f = $f+1;
162
                                break;
163
                        case "uid":
164
                                $f = $f+1;
165
                                break;
166
                }
167
                $f++;
168
        }
169
        $l++;
170
        $i++;
171
}
172

    
173
/* remove duplicate items by mac address */
174
if(count($leases) > 0) {
175
        $leases = remove_duplicate($leases,"ip");
176
}
177

    
178
if(count($pools) > 0) {
179
        $pools = remove_duplicate($pools,"name");
180
        asort($pools);
181
}
182

    
183

    
184
// Put this in an easy to use form
185
$dhcpmac = array();
186
$dhcpip = array();
187
	
188
foreach ($leases as $value) {
189
	$dhcpmac[$value['mac']] = $value['hostname'];	
190
	$dhcpip[$value['ip']] = $value['hostname'];	
191
}
192

    
193
exec("/usr/sbin/arp -an",$rawdata);
194

    
195
$i = 0; 
196
$ifdescrs = array('wan' => 'WAN', 'lan' => 'LAN');
197
						
198
for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) {
199
	$ifdescrs['opt' . $j] = $config['interfaces']['opt' . $j]['descr'];
200
}
201

    
202
foreach ($ifdescrs as $key =>$interface) {
203
	$hwif[$config['interfaces'][$key]['if']] = $interface;
204
}
205

    
206
$data = array();
207
foreach ($rawdata as $line) {
208
	$elements = explode(' ',$line);
209
	
210
	if ($elements[3] != "(incomplete)") {
211
		$arpent = array();
212
		$arpent['ip'] = trim(str_replace(array('(',')'),'',$elements[1]));
213
		$arpent['mac'] = trim($elements[3]);
214
		$arpent['interface'] = trim($elements[5]);
215
		$data[] = $arpent;
216
	}
217
}
218

    
219
function getHostName($mac,$ip)
220
{
221
	global $dhcpmac, $dhcpip;
222
	
223
	if ($dhcpmac[$mac])
224
		return $dhcpmac[$mac];
225
	else if ($dhcpip[$ip])
226
		return $dhcpip[$ip];
227
	else if(gethostbyaddr($ip) <> "" and gethostbyaddr($ip) <> $ip)
228
		return gethostbyaddr($ip);
229
	else 
230
		return "&nbsp;";	
231
}
232

    
233
$pgtitle = "Diagnostics: ARP Table";
234
include("head.inc");
235

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

    
263
<?php include("fend.inc"); ?>
(4-4/175)