Project

General

Profile

Download (5.38 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php
2
<?php
3
/*
4
	diag_arp.php
5
	part of m0n0wall (http://m0n0.ch/wall)
6

    
7
	Copyright (C) 2005 Paul Taylor (paultaylor@winndixie.com) and Manuel Kasper <mk@neon1.net>.
8
	All rights reserved.
9

    
10
	Redistribution and use in source and binary forms, with or without
11
	modification, are permitted provided that the following conditions are met:
12

    
13
	1. Redistributions of source code must retain the above copyright notice,
14
	this list of conditions and the following disclaimer.
15

    
16
	2. Redistributions in binary form must reproduce the above copyright
17
	notice, this list of conditions and the following disclaimer in the
18
	documentation and/or other materials provided with the distribution.
19

    
20
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
21
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
24
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
	POSSIBILITY OF SUCH DAMAGE.
30
*/
31

    
32
require("guiconfig.inc");
33
$pgtitle = "Diagnostics: ARP Table";
34
include("head.inc");
35
include("fbegin.inc"); 
36
?>
37
<body link="#000000" vlink="#000000" alink="#000000">
38

    
39
<?php
40

    
41
$fp = @fopen("{$g['vardb_path']}/dhcpd.leases","r");
42

    
43
if ($fp) {
44

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

    
142
exec("/usr/sbin/arp -an",$rawdata);
143

    
144
$i = 0; 
145
$ifdescrs = array('wan' => 'WAN', 'lan' => 'LAN');
146
						
147
for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) {
148
	$ifdescrs['opt' . $j] = $config['interfaces']['opt' . $j]['descr'];
149
}
150

    
151
foreach ($ifdescrs as $key =>$interface) {
152
	$hwif[$config['interfaces'][$key]['if']] = $interface;
153
}
154

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

    
168
function getHostName($mac,$ip)
169
{
170
	global $dhcpmac, $dhcpip;
171
	
172
	if ($dhcpmac[$mac])
173
		return $dhcpmac[$mac];
174
	else if ($dhcpip[$ip])
175
		return $dhcpip[$ip];
176
	else 
177
		return "&nbsp;";	
178
}
179

    
180
?>
181

    
182
<table width="100%" border="0" cellpadding="0" cellspacing="0">
183
  <tr>
184
    <td class="listhdrr">IP address</td>
185
    <td class="listhdrr">MAC address</td>
186
    <td class="listhdrr">Hostname</td>
187
    <td class="listhdr">Interface</td>
188
    <td class="list"></td>
189
  </tr>
190
<?php foreach ($data as $entry): ?>
191
  <tr>
192
    <td class="listlr"><?=$entry['ip'];?></td>
193
    <td class="listr"><?=$entry['mac'];?></td>
194
    <td class="listr"><?=getHostName($entry['mac'], $entry['ip']);?></td>
195
    <td class="listr"><?=$hwif[$entry['interface']];?></td>
196
  </tr>
197
<?php endforeach; ?>
198
</table>
199

    
200
<?php include("fend.inc"); ?>
(4-4/146)