Project

General

Profile

Download (5.57 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	diag_traceroute.php
4
	part of m0n0wall (http://m0n0.ch/wall)
5

    
6
	Copyright (C) 2013-2015 Electric Sheep Fencing, LP
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
/*
33
	pfSense_BUILDER_BINARIES:	/usr/sbin/traceroute
34
	pfSense_MODULE: routing
35
*/
36

    
37
##|+PRIV
38
##|*IDENT=page-diagnostics-traceroute
39
##|*NAME=Diagnostics: Traceroute page
40
##|*DESCR=Allow access to the 'Diagnostics: Traceroute' page.
41
##|*MATCH=diag_traceroute.php*
42
##|-PRIV
43

    
44
require("guiconfig.inc");
45

    
46
$allowautocomplete = true;
47
$pgtitle = array(gettext("Diagnostics"),gettext("Traceroute"));
48
include("head.inc");
49

    
50
define('MAX_TTL', 64);
51
define('DEFAULT_TTL', 18);
52

    
53
$pconfig['ttl'] = DEFAULT_TTL;
54
$pconfig['ipproto'] = 'IPv4';
55
$pconfig['sourceip'] = 'Any';
56

    
57
function create_sourceaddresslist() {
58
	$list = array('any' => 'Any');
59

    
60
	$sourceips = get_possible_traffic_source_addresses(true);
61

    
62
	foreach ($sourceips as $sipvalue => $sipname)
63
		$list[$sipname[value]] = $sipname[name];
64

    
65
	return($list);
66
}
67

    
68
if ($_POST || $_REQUEST['host']) {
69
	unset($input_errors);
70
	unset($do_traceroute);
71

    
72
	/* input validation */
73
	$reqdfields = explode(" ", "host ttl");
74
	$reqdfieldsn = array(gettext("Host"),gettext("ttl"));
75
	do_input_validation($_REQUEST, $reqdfields, $reqdfieldsn, $input_errors);
76

    
77
	if (($_REQUEST['ttl'] < 1) || ($_REQUEST['ttl'] > MAX_TTL)) {
78
		$input_errors[] = sprintf(gettext("Maximum number of hops must be between 1 and %s"), MAX_TTL);
79
	}
80
	$host = trim($_REQUEST['host']);
81
	$ipproto = $_REQUEST['ipproto'];
82
	if (($ipproto == "ipv4") && is_ipaddrv6($host))
83
		$input_errors[] = gettext("When using IPv4, the target host must be an IPv4 address or hostname.");
84
	if (($ipproto == "ipv6") && is_ipaddrv4($host))
85
		$input_errors[] = gettext("When using IPv6, the target host must be an IPv6 address or hostname.");
86

    
87
	if (!$input_errors)
88
		$host = $_REQUEST['host'];
89

    
90
	$sourceip = $_REQUEST['sourceip'];
91
	$do_traceroute = true;
92
	$ttl = $_REQUEST['ttl'];
93
	$resolve = $_REQUEST['resolve'];
94
	$useicmp = $_REQUEST['useicmp'];
95

    
96
} else {
97
	$resolve = false;
98
	$useicmp = false;
99
}
100

    
101
if (!isset($do_traceroute)) {
102
	$do_traceroute = false;
103
	$host = '';
104
	$ttl = DEFAULT_TTL;
105
}
106

    
107
if ($input_errors)
108
	print_input_errors($input_errors);
109

    
110
require('classes/Form.class.php');
111

    
112
$form = new Form('Traceroute');
113

    
114
$section = new Form_Section('Traceroute');
115

    
116
$section->addInput(new Form_Input(
117
	'host',
118
	'Hostname',
119
	'text',
120
	$host,
121
	['placeholder' => 'Hostname to trace.']
122
));
123

    
124
$section->addInput(new Form_Select(
125
	'ipproto',
126
	'IP Protocol',
127
	$pconfig['protocol'],
128
	array('ipv4' => 'IPv4', 'ipv6' => 'IPv6')
129
))->setHelp('Select the protocol to use');
130

    
131
$section->addInput(new Form_Select(
132
	'sourceip',
133
	'Source Address',
134
	$pconfig['source'],
135
	create_sourceaddresslist()
136
))->setHelp('Select source address for the trace');
137

    
138
$section->addInput(new Form_Select(
139
	'ttl',
140
	'Maximum nuber of hops',
141
	$ttl,
142
	array_combine(range(1, MAX_TTL), range(1, MAX_TTL))
143
))->setHelp('Select the maximum number of network hops to trace');
144

    
145
$section->addInput(new Form_Checkbox(
146
	'resolve',
147
	'Reverse Address Lookup',
148
	'',
149
	$resolve
150
))->setHelp('When checked, traceroute will attempt to perform a PTR lookup to locate hostnames for hops along the path. Will slow down the process as it has to wait for DNS replies.');
151

    
152
$section->addInput(new Form_Checkbox(
153
	'useicmp',
154
	gettext("Use ICMP"),
155
	'',
156
	$useicmp
157
))->setHelp('By default, traceroute uses UDP but that may be blocked by some routers. Check this box to use ICMP instead, which may succeed. ');
158

    
159
$form->add($section);
160
print $form;
161

    
162
/* Show the traceroute results */
163
if (!$input_errors && $do_traceroute) {
164

    
165
	$useicmp = isset($_REQUEST['useicmp']) ? "-I" : "";
166
	$n = isset($resolve) ? "" : "-n";
167

    
168
	$command = "/usr/sbin/traceroute";
169
	if ($ipproto == "ipv6") {
170
		$command .= "6";
171
		$ifaddr = is_ipaddr($sourceip) ? $sourceip : get_interface_ipv6($sourceip);
172
	} else {
173
		$ifaddr = is_ipaddr($sourceip) ? $sourceip : get_interface_ip($sourceip);
174
	}
175

    
176
	if ($ifaddr && (is_ipaddr($host) || is_hostname($host)))
177
		$srcip = "-s " . escapeshellarg($ifaddr);
178

    
179
	$cmd = "{$command} {$n} {$srcip} -w 2 {$useicmp} -m " . escapeshellarg($ttl) . " " . escapeshellarg($host);
180
?>
181
	<div class="panel panel-default">
182
		<div class="panel-heading">Results</div>
183
		<div class="panel-body">
184
<?php
185
		if ($result = shell_exec($cmd))
186
			print(nl2br($result));
187
		else
188
			print('Error: ' . $host . ' ' . gettext("could not be traced/resolved"));
189
?>
190
		</div>
191
	</div>
192
<?php
193
}
194

    
195
include("foot.inc");
196
?>
(42-42/241)