Project

General

Profile

Download (9.55 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
require("guiconfig.inc");
32 6090e85b Bill Marquette
33 fc259334 Seth Mos
function leasecmp($a, $b) {
34
	return strcmp($a[$_GET['order']], $b[$_GET['order']]);
35
}
36 93e1b16c Scott Ullrich
37 fc259334 Seth Mos
function adjust_gmt($dt) {
38
	$ts = strtotime($dt . " GMT");
39
	return strftime("%Y/%m/%d %H:%M:%S", $ts);
40
}
41 93e1b16c Scott Ullrich
42 fc259334 Seth Mos
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 93e1b16c Scott Ullrich
51 fc259334 Seth Mos
$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 93e1b16c Scott Ullrich
58 fc259334 Seth Mos
/* 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 93e1b16c Scott Ullrich
	
188 fc259334 Seth Mos
foreach ($leases as $value) {
189
	$dhcpmac[$value['mac']] = $value['hostname'];	
190
	$dhcpip[$value['ip']] = $value['hostname'];	
191 93e1b16c Scott Ullrich
}
192
193
exec("/usr/sbin/arp -an",$rawdata);
194
195
$i = 0; 
196 cbe3ea96 Ermal Luçi
197
/* if list */
198
$ifdescrs = get_configured_interface_with_descr();
199 93e1b16c Scott Ullrich
200
foreach ($ifdescrs as $key =>$interface) {
201
	$hwif[$config['interfaces'][$key]['if']] = $interface;
202
}
203
204
$data = array();
205
foreach ($rawdata as $line) {
206
	$elements = explode(' ',$line);
207
	
208
	if ($elements[3] != "(incomplete)") {
209
		$arpent = array();
210
		$arpent['ip'] = trim(str_replace(array('(',')'),'',$elements[1]));
211
		$arpent['mac'] = trim($elements[3]);
212
		$arpent['interface'] = trim($elements[5]);
213
		$data[] = $arpent;
214
	}
215
}
216
217
function getHostName($mac,$ip)
218
{
219
	global $dhcpmac, $dhcpip;
220
	
221
	if ($dhcpmac[$mac])
222
		return $dhcpmac[$mac];
223
	else if ($dhcpip[$ip])
224
		return $dhcpip[$ip];
225 4dedef58 Scott Ullrich
	else if(gethostbyaddr($ip) <> "" and gethostbyaddr($ip) <> $ip)
226
		return gethostbyaddr($ip);
227 93e1b16c Scott Ullrich
	else 
228
		return "&nbsp;";	
229
}
230
231 d88c6a9f Scott Ullrich
$pgtitle = array("Diagnostics","ARP Table");
232 fc259334 Seth Mos
include("head.inc");
233 93e1b16c Scott Ullrich
?>
234 fc259334 Seth Mos
<body link="#000000" vlink="#000000" alink="#000000">
235
<script src="/javascript/sorttable.js"></script>
236
<? include("fbegin.inc"); ?>
237
<table width="100%" border="0" cellpadding="0" cellspacing="0">
238
        <tr>
239
                <td>
240 5fd0b016 Scott Ullrich
<table class="sortable" name="sortabletable" id="sortabletable" width="100%" border="0" cellpadding="0" cellspacing="0">
241 93e1b16c Scott Ullrich
  <tr>
242
    <td class="listhdrr">IP address</td>
243
    <td class="listhdrr">MAC address</td>
244
    <td class="listhdrr">Hostname</td>
245
    <td class="listhdr">Interface</td>
246
    <td class="list"></td>
247
  </tr>
248
<?php foreach ($data as $entry): ?>
249
  <tr>
250
    <td class="listlr"><?=$entry['ip'];?></td>
251
    <td class="listr"><?=$entry['mac'];?></td>
252
    <td class="listr"><?=getHostName($entry['mac'], $entry['ip']);?></td>
253
    <td class="listr"><?=$hwif[$entry['interface']];?></td>
254
  </tr>
255
<?php endforeach; ?>
256
</table>
257 6090e85b Bill Marquette
</td></tr></table>
258 93e1b16c Scott Ullrich
259
<?php include("fend.inc"); ?>