Project

General

Profile

Download (5.38 KB) Statistics
| Branch: | Tag: | Revision:
1 5b237745 Scott Ullrich
<?php
2
/*
3 c5d81585 Renato Botelho
 * diag_ping.php
4 9da2cf1c Stephen Beaver
 *
5 c5d81585 Renato Botelho
 * part of pfSense (https://www.pfsense.org)
6 38809d47 Renato Botelho do Couto
 * Copyright (c) 2004-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8 0284d79e jim-p
 * Copyright (c) 2014-2020 Rubicon Communications, LLC (Netgate)
9 c5d81585 Renato Botelho
 * Copyright (c) 2003-2005 Bob Zoller (bob@kludgebox.com)
10
 * All rights reserved.
11 fd9ebcd5 Stephen Beaver
 *
12 c5d81585 Renato Botelho
 * originally based on m0n0wall (http://m0n0.ch/wall)
13
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
14
 * All rights reserved.
15 fd9ebcd5 Stephen Beaver
 *
16 b12ea3fb Renato Botelho
 * Licensed under the Apache License, Version 2.0 (the "License");
17
 * you may not use this file except in compliance with the License.
18
 * You may obtain a copy of the License at
19 fd9ebcd5 Stephen Beaver
 *
20 b12ea3fb Renato Botelho
 * http://www.apache.org/licenses/LICENSE-2.0
21 fd9ebcd5 Stephen Beaver
 *
22 b12ea3fb Renato Botelho
 * Unless required by applicable law or agreed to in writing, software
23
 * distributed under the License is distributed on an "AS IS" BASIS,
24
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25
 * See the License for the specific language governing permissions and
26
 * limitations under the License.
27 fd9ebcd5 Stephen Beaver
 */
28 5b237745 Scott Ullrich
29 6b07c15a Matthew Grooms
##|+PRIV
30
##|*IDENT=page-diagnostics-ping
31 5230f468 jim-p
##|*NAME=Diagnostics: Ping
32 6b07c15a Matthew Grooms
##|*DESCR=Allow access to the 'Diagnostics: Ping' page.
33
##|*MATCH=diag_ping.php*
34
##|-PRIV
35
36 0d56c06b jim-p
$allowautocomplete = true;
37 36678039 Carlos Eduardo Ramos
$pgtitle = array(gettext("Diagnostics"), gettext("Ping"));
38 53483463 jim-p
require_once("guiconfig.inc");
39
40 5b237745 Scott Ullrich
define('MAX_COUNT', 10);
41
define('DEFAULT_COUNT', 3);
42 2d0b01e0 Godwottery
define('MAX_WAIT', 10);
43 740e289b Godwottery
define('DEFAULT_WAIT', 1);
44 ed69a496 jim-p
$do_ping = false;
45
$host = '';
46
$count = DEFAULT_COUNT;
47 2d0b01e0 Godwottery
$wait = DEFAULT_WAIT;
48 5b237745 Scott Ullrich
49 b8cc74ed sullrich
if ($_POST || $_REQUEST['host']) {
50 5b237745 Scott Ullrich
	unset($input_errors);
51
	unset($do_ping);
52
53
	/* input validation */
54
	$reqdfields = explode(" ", "host count");
55 699737d9 Phil Davis
	$reqdfieldsn = array(gettext("Host"), gettext("Count"));
56 1e9b4611 Renato Botelho
	do_input_validation($_REQUEST, $reqdfields, $reqdfieldsn, $input_errors);
57 337cacac Viktor G
	if (($_REQUEST['count'] < 1) || ($_REQUEST['count'] > MAX_COUNT) || (!is_numericint($_REQUEST['count']))) {
58 4a5430e3 cadu
		$input_errors[] = sprintf(gettext("Count must be between 1 and %s"), MAX_COUNT);
59 e00d0c0c Godwottery
	}	
60 337cacac Viktor G
	if (isset($_REQUEST['wait']) && (($_REQUEST['wait'] < 1) ||
61
	    ($_REQUEST['wait'] > MAX_WAIT) || (!is_numericint($_REQUEST['wait'])))) {
62 2d0b01e0 Godwottery
		$input_errors[] = sprintf(gettext("Wait must be between 1 and %s"), MAX_WAIT);
63 e00d0c0c Godwottery
	}	
64 c8a39f1b Viktor G
	$host = idn_to_ascii(trim($_REQUEST['host']));
65 4f3bb062 jim-p
	$ipproto = $_REQUEST['ipproto'];
66 5f601060 Phil Davis
	if (($ipproto == "ipv4") && is_ipaddrv6($host)) {
67 4f3bb062 jim-p
		$input_errors[] = gettext("When using IPv4, the target host must be an IPv4 address or hostname.");
68 5f601060 Phil Davis
	}
69
	if (($ipproto == "ipv6") && is_ipaddrv4($host)) {
70 4f3bb062 jim-p
		$input_errors[] = gettext("When using IPv6, the target host must be an IPv6 address or hostname.");
71 5f601060 Phil Davis
	}
72 cc3990a3 jim-p
	if (!is_ipaddr($host) && !is_hostname($host)) {
73
		$input_errors[] = gettext("Hostname must be a valid hostname or IP address.");
74
	}
75 4f3bb062 jim-p
76 5b237745 Scott Ullrich
	if (!$input_errors) {
77 ed69a496 jim-p
		if ($_POST) {
78
			$do_ping = true;
79
		}
80 947141fd Phil Davis
		if (isset($_REQUEST['sourceip'])) {
81 5e1cfe9e heper
			$sourceip = $_REQUEST['sourceip'];
82
		}
83 e00d0c0c Godwottery
		$count = (empty($_REQUEST['count'])) ? DEFAULT_WAIT : $_REQUEST['count'];
84
		$wait = (empty($_REQUEST['wait'])) ? DEFAULT_WAIT : $_REQUEST['wait'];
85 5b237745 Scott Ullrich
	}
86
}
87 45d6ada5 Sjon Hortensius
88 288a2a0f Phil Davis
if ($do_ping) {
89 f1a9b09d Colin Fleming
?>
90 45d6ada5 Sjon Hortensius
	<script type="text/javascript">
91
	//<![CDATA[
92 947141fd Phil Davis
	window.onload=function() {
93 45d6ada5 Sjon Hortensius
		document.getElementById("pingCaptured").wrap='off';
94
	}
95
	//]]>
96
	</script>
97 f1a9b09d Colin Fleming
<?php
98 45d6ada5 Sjon Hortensius
	$ifscope = '';
99
	$command = "/sbin/ping";
100
	if ($ipproto == "ipv6") {
101
		$command .= "6";
102
		$ifaddr = is_ipaddr($sourceip) ? $sourceip : get_interface_ipv6($sourceip);
103 947141fd Phil Davis
		if (is_linklocal($ifaddr)) {
104 45d6ada5 Sjon Hortensius
			$ifscope = get_ll_scope($ifaddr);
105 947141fd Phil Davis
		}
106 45d6ada5 Sjon Hortensius
	} else {
107
		$ifaddr = is_ipaddr($sourceip) ? $sourceip : get_interface_ip($sourceip);
108
	}
109
110
	if ($ifaddr && (is_ipaddr($host) || is_hostname($host))) {
111
		$srcip = "-S" . escapeshellarg($ifaddr);
112 947141fd Phil Davis
		if (is_linklocal($host) && !strstr($host, "%") && !empty($ifscope)) {
113 45d6ada5 Sjon Hortensius
			$host .= "%{$ifscope}";
114 947141fd Phil Davis
		}
115 3c98b3f7 jim-p
	}
116 45d6ada5 Sjon Hortensius
117 0a6222e5 Godwottery
	$cmd = "{$command} {$srcip} -c" . escapeshellarg($count) . " -i" . escapeshellarg($wait) . " " . escapeshellarg($host);
118 45d6ada5 Sjon Hortensius
	//echo "Ping command: {$cmd}\n";
119
	$result = shell_exec($cmd);
120
121 947141fd Phil Davis
	if (empty($result)) {
122 c8a39f1b Viktor G
		$input_errors[] = sprintf(gettext('Host "%s" did not respond or could not be resolved.'), idn_to_utf8($host));
123 947141fd Phil Davis
	}
124 45d6ada5 Sjon Hortensius
125
}
126
127
include('head.inc');
128
129 947141fd Phil Davis
if ($input_errors) {
130 45d6ada5 Sjon Hortensius
	print_input_errors($input_errors);
131 947141fd Phil Davis
}
132 45d6ada5 Sjon Hortensius
133 37676f4e jim-p
$form = new Form(false);
134 45d6ada5 Sjon Hortensius
135
$section = new Form_Section('Ping');
136
137
$section->addInput(new Form_Input(
138
	'host',
139 3e2028f4 Phil Davis
	'*Hostname',
140 45d6ada5 Sjon Hortensius
	'text',
141 c8a39f1b Viktor G
	idn_to_utf8($host),
142 45d6ada5 Sjon Hortensius
	['placeholder' => 'Hostname to ping']
143
));
144
145 214e79b0 Stephen Beaver
$section->addInput(new Form_Select(
146 45d6ada5 Sjon Hortensius
	'ipproto',
147 fe54f091 Phil Davis
	'*IP Protocol',
148 214e79b0 Stephen Beaver
	$ipproto,
149
	['ipv4' => 'IPv4', 'ipv6' => 'IPv6']
150
));
151 45d6ada5 Sjon Hortensius
152
$section->addInput(new Form_Select(
153
	'sourceip',
154 fe54f091 Phil Davis
	'*Source address',
155 3f91d34e Stephen Beaver
	$sourceip,
156 430cee65 stilez
	array('' => gettext('Automatically selected (default)')) + get_possible_traffic_source_addresses(true)
157 3728bbe5 NOYB
))->setHelp('Select source address for the ping.');
158 45d6ada5 Sjon Hortensius
159
$section->addInput(new Form_Select(
160
	'count',
161
	'Maximum number of pings',
162
	$count,
163
	array_combine(range(1, MAX_COUNT), range(1, MAX_COUNT))
164 3728bbe5 NOYB
))->setHelp('Select the maximum number of pings.');
165 45d6ada5 Sjon Hortensius
166 2d0b01e0 Godwottery
$section->addInput(new Form_Select(
167
	'wait',
168 e00d0c0c Godwottery
	'Seconds between pings',
169 2d0b01e0 Godwottery
	$wait,
170
	array_combine(range(1, MAX_WAIT), range(1, MAX_WAIT))
171 83bf2511 Godwottery
))->setHelp('Select the number of seconds to wait between pings.');
172 2d0b01e0 Godwottery
173 45d6ada5 Sjon Hortensius
$form->add($section);
174 37676f4e jim-p
175
$form->addGlobal(new Form_Button(
176
	'Submit',
177 faab522f Renato Botelho
	'Ping',
178 37676f4e jim-p
	null,
179
	'fa-rss'
180
))->addClass('btn-primary');
181
182 45d6ada5 Sjon Hortensius
print $form;
183
184 288a2a0f Phil Davis
if ($do_ping && !empty($result) && !$input_errors) {
185 45d6ada5 Sjon Hortensius
?>
186
	<div class="panel panel-default">
187
		<div class="panel-heading">
188 babf5d85 Phil Davis
			<h2 class="panel-title"><?=gettext('Results')?></h2>
189 45d6ada5 Sjon Hortensius
		</div>
190
191
		<div class="panel-body">
192 cc3990a3 jim-p
			<pre><?= htmlspecialchars($result) ?></pre>
193 45d6ada5 Sjon Hortensius
		</div>
194
	</div>
195
<?php
196
}
197
198
include('foot.inc');