Project

General

Profile

Download (6.88 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
 *	part of m0n0wall (http://m0n0.ch/wall)
9
 *
10
 *  Redistribution and use in source and binary forms, with or without modification, 
11
 *  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
18
 *      the documentation and/or other materials provided with the
19
 *      distribution. 
20
 *
21
 *  3. All advertising materials mentioning features or use of this software 
22
 *      must display the following acknowledgment:
23
 *      "This product includes software developed by the pfSense Project
24
 *       for use in the pfSense software distribution. (http://www.pfsense.org/). 
25
 *
26
 *  4. The names "pfSense" and "pfSense Project" must not be used to
27
 *       endorse or promote products derived from this software without
28
 *       prior written permission. For written permission, please contact
29
 *       coreteam@pfsense.org.
30
 *
31
 *  5. Products derived from this software may not be called "pfSense"
32
 *      nor may "pfSense" appear in their names without prior written
33
 *      permission of the Electric Sheep Fencing, LLC.
34
 *
35
 *  6. Redistributions of any form whatsoever must retain the following
36
 *      acknowledgment:
37
 *
38
 *  "This product includes software developed by the pfSense Project
39
 *  for use in the pfSense software distribution (http://www.pfsense.org/).
40
  *
41
 *  THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
42
 *  EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43
 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
44
 *  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
45
 *  ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46
 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
47
 *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
48
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49
 *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
50
 *  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
51
 *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
52
 *  OF THE POSSIBILITY OF SUCH DAMAGE.
53
 *
54
 *  ====================================================================
55
 *
56
 */
57

    
58
/*
59
	pfSense_BUILDER_BINARIES:	/usr/sbin/traceroute
60
	pfSense_MODULE: routing
61
*/
62

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

    
70
require("guiconfig.inc");
71

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

    
76
define('MAX_TTL', 64);
77
define('DEFAULT_TTL', 18);
78

    
79
$pconfig['ttl'] = DEFAULT_TTL;
80
$pconfig['ipproto'] = 'IPv4';
81
$pconfig['sourceip'] = 'Any';
82

    
83
function create_sourceaddresslist() {
84
	$list = array('any' => 'Any');
85

    
86
	$sourceips = get_possible_traffic_source_addresses(true);
87

    
88
	foreach ($sourceips as $sipvalue => $sipname)
89
		$list[$sipname[value]] = $sipname[name];
90

    
91
	return($list);
92
}
93

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

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

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

    
115
	if (!$input_errors)
116
		$host = $_REQUEST['host'];
117

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

    
124
} else {
125
	$resolve = false;
126
	$useicmp = false;
127
}
128

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

    
135
if ($input_errors)
136
	print_input_errors($input_errors);
137

    
138
require_once('classes/Form.class.php');
139

    
140
$form = new Form('Traceroute');
141

    
142
$section = new Form_Section('Traceroute');
143

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

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

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

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

    
173
$section->addInput(new Form_Checkbox(
174
	'resolve',
175
	'Reverse Address Lookup',
176
	'',
177
	$resolve
178
))->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.');
179

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

    
187
$form->add($section);
188
print $form;
189

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

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

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

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

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

    
224
include("foot.inc");
225
?>
(43-43/235)