Project

General

Profile

Download (6.95 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	diag_traceroute.php
4
*/
5
/* ====================================================================
6
 *  Copyright (c)  2004-2015  Electric Sheep Fencing, LLC. All rights reserved.
7
 *  Copyright (c)  2005 Paul Taylor (paultaylor@winndixie.com) and Manuel Kasper <mk@neon1.net>
8
 *
9
 *  Some or all of this file is based on the m0n0wall project which is
10
 *  Copyright (c)  2004 Manuel Kasper (BSD 2 clause)
11
 *
12
 *  Redistribution and use in source and binary forms, with or without modification,
13
 *  are permitted provided that the following conditions are met:
14
 *
15
 *  1. Redistributions of source code must retain the above copyright notice,
16
 *      this list of conditions and the following disclaimer.
17
 *
18
 *  2. Redistributions in binary form must reproduce the above copyright
19
 *      notice, this list of conditions and the following disclaimer in
20
 *      the documentation and/or other materials provided with the
21
 *      distribution.
22
 *
23
 *  3. All advertising materials mentioning features or use of this software
24
 *      must display the following acknowledgment:
25
 *      "This product includes software developed by the pfSense Project
26
 *       for use in the pfSense software distribution. (http://www.pfsense.org/).
27
 *
28
 *  4. The names "pfSense" and "pfSense Project" must not be used to
29
 *       endorse or promote products derived from this software without
30
 *       prior written permission. For written permission, please contact
31
 *       coreteam@pfsense.org.
32
 *
33
 *  5. Products derived from this software may not be called "pfSense"
34
 *      nor may "pfSense" appear in their names without prior written
35
 *      permission of the Electric Sheep Fencing, LLC.
36
 *
37
 *  6. Redistributions of any form whatsoever must retain the following
38
 *      acknowledgment:
39
 *
40
 *  "This product includes software developed by the pfSense Project
41
 *  for use in the pfSense software distribution (http://www.pfsense.org/).
42
 *
43
 *  THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
44
 *  EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45
 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
46
 *  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
47
 *  ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48
 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
49
 *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
50
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51
 *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
52
 *  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
53
 *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
54
 *  OF THE POSSIBILITY OF SUCH DAMAGE.
55
 *
56
 *  ====================================================================
57
 *
58
 */
59

    
60
/*
61
	pfSense_BUILDER_BINARIES:	/usr/sbin/traceroute
62
	pfSense_MODULE: routing
63
*/
64

    
65
##|+PRIV
66
##|*IDENT=page-diagnostics-traceroute
67
##|*NAME=Diagnostics: Traceroute page
68
##|*DESCR=Allow access to the 'Diagnostics: Traceroute' page.
69
##|*MATCH=diag_traceroute.php*
70
##|-PRIV
71

    
72
require("guiconfig.inc");
73

    
74
$allowautocomplete = true;
75
$pgtitle = array(gettext("Diagnostics"), gettext("Traceroute"));
76
include("head.inc");
77

    
78
define('MAX_TTL', 64);
79
define('DEFAULT_TTL', 18);
80

    
81
$pconfig['ttl'] = DEFAULT_TTL;
82
$pconfig['ipproto'] = 'IPv4';
83
$pconfig['sourceip'] = 'Any';
84

    
85
function create_sourceaddresslist() {
86
	$list = array('any' => 'Any');
87

    
88
	$sourceips = get_possible_traffic_source_addresses(true);
89

    
90
	foreach ($sourceips as $sipvalue => $sipname)
91
		$list[$sipvalue] = $sipname;
92

    
93
	return($list);
94
}
95

    
96
if ($_POST || $_REQUEST['host']) {
97
	unset($input_errors);
98
	unset($do_traceroute);
99

    
100
	/* input validation */
101
	$reqdfields = explode(" ", "host ttl");
102
	$reqdfieldsn = array(gettext("Host"), gettext("ttl"));
103
	do_input_validation($_REQUEST, $reqdfields, $reqdfieldsn, $input_errors);
104

    
105
	if (($_REQUEST['ttl'] < 1) || ($_REQUEST['ttl'] > MAX_TTL)) {
106
		$input_errors[] = sprintf(gettext("Maximum number of hops must be between 1 and %s"), MAX_TTL);
107
	}
108
	$host = trim($_REQUEST['host']);
109
	$ipproto = $_REQUEST['ipproto'];
110
	if (($ipproto == "ipv4") && is_ipaddrv6($host)) {
111
		$input_errors[] = gettext("When using IPv4, the target host must be an IPv4 address or hostname.");
112
	}
113
	if (($ipproto == "ipv6") && is_ipaddrv4($host)) {
114
		$input_errors[] = gettext("When using IPv6, the target host must be an IPv6 address or hostname.");
115
	}
116

    
117
	if (!$input_errors)
118
		$host = $_REQUEST['host'];
119

    
120
	$sourceip = $_REQUEST['sourceip'];
121
	$do_traceroute = true;
122
	$ttl = $_REQUEST['ttl'];
123
	$resolve = $_REQUEST['resolve'];
124
	$useicmp = $_REQUEST['useicmp'];
125

    
126
} else {
127
	$resolve = false;
128
	$useicmp = false;
129
}
130

    
131
if (!isset($do_traceroute)) {
132
	$do_traceroute = false;
133
	$host = '';
134
	$ttl = DEFAULT_TTL;
135
}
136

    
137
if ($input_errors)
138
	print_input_errors($input_errors);
139

    
140
require_once('classes/Form.class.php');
141

    
142
$form = new Form('Traceroute');
143

    
144
$section = new Form_Section('Traceroute');
145

    
146
$section->addInput(new Form_Input(
147
	'host',
148
	'Hostname',
149
	'text',
150
	$host,
151
	['placeholder' => 'Hostname to trace.']
152
));
153

    
154
$section->addInput(new Form_Select(
155
	'ipproto',
156
	'IP Protocol',
157
	$pconfig['ipproto'],
158
	array('ipv4' => 'IPv4', 'ipv6' => 'IPv6')
159
))->setHelp('Select the protocol to use');
160

    
161
$section->addInput(new Form_Select(
162
	'sourceip',
163
	'Source Address',
164
	$pconfig['sourceip'],
165
	create_sourceaddresslist()
166
))->setHelp('Select source address for the trace');
167

    
168
$section->addInput(new Form_Select(
169
	'ttl',
170
	'Maximum nuber of hops',
171
	$ttl,
172
	array_combine(range(1, MAX_TTL), range(1, MAX_TTL))
173
))->setHelp('Select the maximum number of network hops to trace');
174

    
175
$section->addInput(new Form_Checkbox(
176
	'resolve',
177
	'Reverse Address Lookup',
178
	'',
179
	$resolve
180
))->setHelp('When checked, traceroute will attempt to perform a PTR lookup to locate hostnames for hops along the path. This will slow down the process as it has to wait for DNS replies.');
181

    
182
$section->addInput(new Form_Checkbox(
183
	'useicmp',
184
	gettext("Use ICMP"),
185
	'',
186
	$useicmp
187
))->setHelp('By default, traceroute uses UDP but that may be blocked by some routers. Check this box to use ICMP instead, which may succeed. ');
188

    
189
$form->add($section);
190
print $form;
191

    
192
/* Show the traceroute results */
193
if (!$input_errors && $do_traceroute) {
194

    
195
	$useicmp = isset($_REQUEST['useicmp']) ? "-I" : "";
196
	$n = isset($resolve) ? "" : "-n";
197

    
198
	$command = "/usr/sbin/traceroute";
199
	if ($ipproto == "ipv6") {
200
		$command .= "6";
201
		$ifaddr = is_ipaddr($sourceip) ? $sourceip : get_interface_ipv6($sourceip);
202
	} else {
203
		$ifaddr = is_ipaddr($sourceip) ? $sourceip : get_interface_ip($sourceip);
204
	}
205

    
206
	if ($ifaddr && (is_ipaddr($host) || is_hostname($host))) {
207
		$srcip = "-s " . escapeshellarg($ifaddr);
208
	}
209

    
210
	$cmd = "{$command} {$n} {$srcip} -w 2 {$useicmp} -m " . escapeshellarg($ttl) . " " . escapeshellarg($host);
211
?>
212
	<div class="panel panel-default">
213
		<div class="panel-heading"><h2 class="panel-title">Results</h2></div>
214
		<div class="panel-body">
215
<?php
216
		if ($result = shell_exec($cmd))
217
			print(nl2br($result));
218
		else
219
			print('Error: ' . $host . ' ' . gettext("could not be traced/resolved"));
220
?>
221
		</div>
222
	</div>
223
<?php
224
}
225

    
226
include("foot.inc");
227
?>
(42-42/234)