Project

General

Profile

Download (5.6 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(new Form_Button(
113
	'traceroute',
114
	'Traceroute'
115
));
116

    
117
$section = new Form_Section('Traceroute');
118

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

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

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

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

    
148
$section->addInput(new Form_Checkbox(
149
	'resolve',
150
	'Reverse Address Lookup',
151
	'',
152
	$resolve
153
))->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.');
154

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

    
162
$form->add($section);
163
print $form;
164

    
165
/* Show the traceroute results */
166
if (!$input_errors && $do_traceroute) {
167

    
168
	$useicmp = isset($_REQUEST['useicmp']) ? "-I" : "";
169
	$n = isset($resolve) ? "" : "-n";
170

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

    
179
	if ($ifaddr && (is_ipaddr($host) || is_hostname($host)))
180
		$srcip = "-s " . escapeshellarg($ifaddr);
181

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

    
198
include("foot.inc");
199
?>
(42-42/241)