Project

General

Profile

Download (5.32 KB) Statistics
| Branch: | Tag: | Revision:
1 5b237745 Scott Ullrich
<?php
2
/*
3
	diag_ping.php
4 5543b9ed Scott Ullrich
	part of m0n0wall (http://m0n0.ch/wall)
5 5b237745 Scott Ullrich
6 5543b9ed Scott Ullrich
	Copyright (C) 2003-2005 Bob Zoller (bob@kludgebox.com) and Manuel Kasper <mk@neon1.net>.
7 5b237745 Scott Ullrich
	All rights reserved.
8
9 ce77a9c4 Phil Davis
	Copyright (C) 2013-2015 Electric Sheep Fencing, LP
10 29aef6c4 Jim Thompson
	All rights reserved.
11
12 5b237745 Scott Ullrich
	Redistribution and use in source and binary forms, with or without
13
	modification, 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 the
20
	documentation and/or other materials provided with the distribution.
21
22
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
23
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
24
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
26
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31
	POSSIBILITY OF SUCH DAMAGE.
32
*/
33
34 3c98b3f7 jim-p
/*
35 8bea9639 Seth Mos
	pfSense_BUILDER_BINARIES:	/sbin/ping /sbin/ping6
36 45d6ada5 Sjon Hortensius
	pfSense_MODULE: routing
37 13d193c2 Scott Ullrich
*/
38
39 6b07c15a Matthew Grooms
##|+PRIV
40
##|*IDENT=page-diagnostics-ping
41
##|*NAME=Diagnostics: Ping page
42
##|*DESCR=Allow access to the 'Diagnostics: Ping' page.
43
##|*MATCH=diag_ping.php*
44
##|-PRIV
45
46 0d56c06b jim-p
$allowautocomplete = true;
47 36678039 Carlos Eduardo Ramos
$pgtitle = array(gettext("Diagnostics"), gettext("Ping"));
48 53483463 jim-p
require_once("guiconfig.inc");
49
50 5b237745 Scott Ullrich
define('MAX_COUNT', 10);
51
define('DEFAULT_COUNT', 3);
52
53 45d6ada5 Sjon Hortensius
function create_sourceaddresslist() {
54
	$list = array('any' => 'Any');
55
56
	foreach (get_possible_traffic_source_addresses(true) as $sipname)
57
		$list[$sipname['value']] = $sipname['name'];
58
59
	return $list;
60
}
61
62 b8cc74ed sullrich
if ($_POST || $_REQUEST['host']) {
63 5b237745 Scott Ullrich
	unset($input_errors);
64
	unset($do_ping);
65
66
	/* input validation */
67
	$reqdfields = explode(" ", "host count");
68 9d8eeb2f Vinicius Coque
	$reqdfieldsn = array(gettext("Host"),gettext("Count"));
69 1e9b4611 Renato Botelho
	do_input_validation($_REQUEST, $reqdfields, $reqdfieldsn, $input_errors);
70 5b237745 Scott Ullrich
71 b8cc74ed sullrich
	if (($_REQUEST['count'] < 1) || ($_REQUEST['count'] > MAX_COUNT)) {
72 4a5430e3 cadu
		$input_errors[] = sprintf(gettext("Count must be between 1 and %s"), MAX_COUNT);
73 5b237745 Scott Ullrich
	}
74
75 4f3bb062 jim-p
	$host = trim($_REQUEST['host']);
76
	$ipproto = $_REQUEST['ipproto'];
77
	if (($ipproto == "ipv4") && is_ipaddrv6($host))
78
		$input_errors[] = gettext("When using IPv4, the target host must be an IPv4 address or hostname.");
79
	if (($ipproto == "ipv6") && is_ipaddrv4($host))
80
		$input_errors[] = gettext("When using IPv6, the target host must be an IPv6 address or hostname.");
81
82 5b237745 Scott Ullrich
	if (!$input_errors) {
83
		$do_ping = true;
84 c5e51011 jim-p
		$sourceip = $_REQUEST['sourceip'];
85 61ba386b Scott Ullrich
		$count = $_POST['count'];
86 3a134b53 Erik Fonnesbeck
		if (preg_match('/[^0-9]/', $count) )
87
			$count = DEFAULT_COUNT;
88 5b237745 Scott Ullrich
	}
89
}
90 45d6ada5 Sjon Hortensius
91 5b237745 Scott Ullrich
if (!isset($do_ping)) {
92
	$do_ping = false;
93
	$host = '';
94
	$count = DEFAULT_COUNT;
95
}
96 b63695db Scott Ullrich
97 45d6ada5 Sjon Hortensius
if($do_ping) {
98 f1a9b09d Colin Fleming
?>
99 45d6ada5 Sjon Hortensius
	<script type="text/javascript">
100
	//<![CDATA[
101
	window.onload=function(){
102
		document.getElementById("pingCaptured").wrap='off';
103
	}
104
	//]]>
105
	</script>
106 f1a9b09d Colin Fleming
<?php
107 45d6ada5 Sjon Hortensius
	$ifscope = '';
108
	$command = "/sbin/ping";
109
	if ($ipproto == "ipv6") {
110
		$command .= "6";
111
		$ifaddr = is_ipaddr($sourceip) ? $sourceip : get_interface_ipv6($sourceip);
112
		if (is_linklocal($ifaddr))
113
			$ifscope = get_ll_scope($ifaddr);
114
	} else {
115
		$ifaddr = is_ipaddr($sourceip) ? $sourceip : get_interface_ip($sourceip);
116
	}
117
118
	if ($ifaddr && (is_ipaddr($host) || is_hostname($host))) {
119
		$srcip = "-S" . escapeshellarg($ifaddr);
120
		if (is_linklocal($host) && !strstr($host, "%") && !empty($ifscope))
121
			$host .= "%{$ifscope}";
122 3c98b3f7 jim-p
	}
123 45d6ada5 Sjon Hortensius
124
	$cmd = "{$command} {$srcip} -c" . escapeshellarg($count) . " " . escapeshellarg($host);
125
	//echo "Ping command: {$cmd}\n";
126
	$result = shell_exec($cmd);
127
128
	if(empty($result))
129
		$input_errors[] = "Host \"" . $host . "\" did not respond or could not be resolved.";
130
131
}
132
133
include('head.inc');
134
135
if ($input_errors)
136
	print_input_errors($input_errors);
137
138
require('classes/Form.class.php');
139
140 38e06c66 Sjon Hortensius
$form = new Form('Ping');
141 45d6ada5 Sjon Hortensius
142
$section = new Form_Section('Ping');
143
144
$section->addInput(new Form_Input(
145
	'host',
146
	'Hostname',
147
	'text',
148
	$host,
149
	['placeholder' => 'Hostname to ping']
150
));
151
152
$group = new Form_Group('IP Protocol');
153
$group->add(new Form_Checkbox(
154
	'ipproto',
155
	null,
156
	'IPv4',
157
	('ipv6' != $ipproto), # negative check, so this would be checked by default
158
	'ipv4'
159
))->displayAsRadio();
160
$group->add(new Form_Checkbox(
161
	'ipproto',
162
	null,
163
	'IPv6',
164
	('ipv6' == $ipproto),
165
	'ipv6'
166
))->displayAsRadio();
167
$group->setHelp('Select the protocol to use');
168
$section->add($group);
169
170
$section->addInput(new Form_Select(
171
	'sourceip',
172
	'Source address',
173
	$pconfig['source'],
174
	create_sourceaddresslist()
175
))->setHelp('Select source address for the ping');
176
177
$section->addInput(new Form_Select(
178
	'count',
179
	'Maximum number of pings',
180
	$count,
181
	array_combine(range(1, MAX_COUNT), range(1, MAX_COUNT))
182
))->setHelp('Select the maximum number pings');
183
184
$form->add($section);
185
print $form;
186
187
if($do_ping && !empty($result) && !$input_errors) {
188
?>
189
	<div class="panel panel-default">
190
		<div class="panel-heading">
191
			<h2 class="panel-title">Results</h2>
192
		</div>
193
194
		<div class="panel-body">
195
			<pre><?= $result ?></pre>
196
		</div>
197
	</div>
198
<?php
199
}
200
201
include('foot.inc');