Project

General

Profile

Download (5.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
require("guiconfig.inc");
32
$pgtitle = "Diagnostics: ARP Table";
33
include("head.inc");
34
?>
35
<body link="#000000" vlink="#000000" alink="#000000">
36
<script src="/javascript/sorttable.js"></script>
37
<? include("fbegin.inc"); ?>
38
<p class="pgtitle"><?=$pgtitle?></p>
39
<table width="100%" border="0" cellpadding="0" cellspacing="0">
40
        <tr>
41
                <td>
42

    
43

    
44
<?php
45

    
46
$fp = @fopen("{$g['dhcpd_chroot_path']}/var/db/dhcpd.leases","r");
47

    
48
if ($fp) {
49

    
50
	$return = array();
51
	
52
	while ($line = fgets($fp)) {
53
		$matches = "";
54
	
55
		// Sort out comments
56
		// C-style comments not supported!
57
		if (preg_match("/^\s*[\r|\n]/", $line, $matches[0]) ||
58
					preg_match("/^([^\"#]*)#.*$/", $line, $matches[1]) ||
59
					preg_match("/^([^\"]*)\/\/.*$/", $line, $matches[2]) ||
60
					preg_match("/\s*#(.*)/", $line, $matches[3]) ||
61
					preg_match("/\\\"\176/", $line, $matches[4])
62
			) {
63
			$line = "";
64
			continue;
65
		}
66
	
67
		if (preg_match("/(.*)#(.*)/", $line, $matches))
68
			$line = $matches[0];
69
	
70
		// Tokenize lines
71
		do {
72
			if (preg_match("/^\s*\"([^\"]*)\"(.*)$/", $line, $matches)) {
73
				$line = $matches[2];
74
				$return[] = array($matches[1], 0);
75
			} else if (preg_match("/^\s*([{};])(.*)$/", $line, $matches)) {
76
				$line = $matches[2];
77
				$return[] = array($matches[0], 1);
78
			} else if (preg_match("/^\s*([^{}; \t]+)(.*)$/", $line, $matches)) {
79
				$line = $matches[2];
80
				$return[] = array($matches[1], 0);
81
			} else
82
				break;
83
	
84
		} while($line);
85
	
86
		$lines++;
87
	}
88
	
89
	fclose($fp);
90
	
91
	$leases = array();
92
	$i = 0;
93
	
94
	// Put everything together again
95
	while ($data = array_shift($return)) {
96
		if ($data[0] == "next") {
97
			$d = array_shift($return);
98
		}
99
		if ($data[0] == "lease") {
100
			$d = array_shift($return);
101
			$leases[$i]['ip'] = $d[0];
102
		}
103
		if ($data[0] == "client-hostname") {
104
			$d = array_shift($return);
105
			$leases[$i]['hostname'] = $d[0];
106
		}
107
		if ($data[0] == "hardware") {
108
			$d = array_shift($return);
109
			if ($d[0] == "ethernet") {
110
				$d = array_shift($return);
111
				$leases[$i]['mac'] = $d[0];
112
			}
113
		} else if ($data[0] == "starts") {
114
			$d = array_shift($return);
115
			$d = array_shift($return);
116
			$leases[$i]['start'] = $d[0];
117
			$d = array_shift($return);
118
			$leases[$i]['start'] .= " " . $d[0];
119
		} else if ($data[0] == "ends") {
120
			$d = array_shift($return);
121
			$d = array_shift($return);
122
			$leases[$i]['end'] = $d[0];
123
			$d = array_shift($return);
124
			$leases[$i]['end'] .= " " . $d[0];
125
		} else if ($data[0] == "binding") {
126
			$d = array_shift($return);
127
			if ($d[0] == "state") {
128
				$d = array_shift($return);
129
				$leases[$i]['act'] = $d[0];
130
			}
131
		} else if (($data[0] == "}") && ($data[1] == 1))		// End of group
132
			$i++;
133
	}
134
	
135
	// Put this in an easy to use form
136
	$dhcpmac = array();
137
	$dhcpip = array();
138
	
139
	foreach ($leases as $value) {
140
		$dhcpmac[$value['mac']] = $value['hostname'];	
141
		$dhcpip[$value['ip']] = $value['hostname'];	
142
	}
143
	
144
	unset($data);
145
}
146

    
147
exec("/usr/sbin/arp -an",$rawdata);
148

    
149
$i = 0; 
150
$ifdescrs = array('wan' => 'WAN', 'lan' => 'LAN');
151
						
152
for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) {
153
	$ifdescrs['opt' . $j] = $config['interfaces']['opt' . $j]['descr'];
154
}
155

    
156
foreach ($ifdescrs as $key =>$interface) {
157
	$hwif[$config['interfaces'][$key]['if']] = $interface;
158
}
159

    
160
$data = array();
161
foreach ($rawdata as $line) {
162
	$elements = explode(' ',$line);
163
	
164
	if ($elements[3] != "(incomplete)") {
165
		$arpent = array();
166
		$arpent['ip'] = trim(str_replace(array('(',')'),'',$elements[1]));
167
		$arpent['mac'] = trim($elements[3]);
168
		$arpent['interface'] = trim($elements[5]);
169
		$data[] = $arpent;
170
	}
171
}
172

    
173
function getHostName($mac,$ip)
174
{
175
	global $dhcpmac, $dhcpip;
176
	
177
	if ($dhcpmac[$mac])
178
		return $dhcpmac[$mac];
179
	else if ($dhcpip[$ip])
180
		return $dhcpip[$ip];
181
	else if(gethostbyaddr($ip) <> "" and gethostbyaddr($ip) <> $ip)
182
		return gethostbyaddr($ip);
183
	else 
184
		return "&nbsp;";	
185
}
186

    
187
?>
188

    
189
<table class="sortable" name="sortabletable" id="sortabletable" width="100%" border="0" cellpadding="0" cellspacing="0">
190
  <tr>
191
    <td class="listhdrr">IP address</td>
192
    <td class="listhdrr">MAC address</td>
193
    <td class="listhdrr">Hostname</td>
194
    <td class="listhdr">Interface</td>
195
    <td class="list"></td>
196
  </tr>
197
<?php foreach ($data as $entry): ?>
198
  <tr>
199
    <td class="listlr"><?=$entry['ip'];?></td>
200
    <td class="listr"><?=$entry['mac'];?></td>
201
    <td class="listr"><?=getHostName($entry['mac'], $entry['ip']);?></td>
202
    <td class="listr"><?=$hwif[$entry['interface']];?></td>
203
  </tr>
204
<?php endforeach; ?>
205
</table>
206
</td></tr></table>
207

    
208
<?php include("fend.inc"); ?>
(4-4/163)