Project

General

Profile

Download (5.34 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * diag_ping.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8
 * Copyright (c) 2014-2020 Rubicon Communications, LLC (Netgate)
9
 * Copyright (c) 2003-2005 Bob Zoller (bob@kludgebox.com)
10
 * All rights reserved.
11
 *
12
 * originally based on m0n0wall (http://m0n0.ch/wall)
13
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
14
 * All rights reserved.
15
 *
16
 * 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
 *
20
 * http://www.apache.org/licenses/LICENSE-2.0
21
 *
22
 * 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
 */
28

    
29
##|+PRIV
30
##|*IDENT=page-diagnostics-ping
31
##|*NAME=Diagnostics: Ping
32
##|*DESCR=Allow access to the 'Diagnostics: Ping' page.
33
##|*MATCH=diag_ping.php*
34
##|-PRIV
35

    
36
$allowautocomplete = true;
37
$pgtitle = array(gettext("Diagnostics"), gettext("Ping"));
38
require_once("guiconfig.inc");
39

    
40
define('MAX_COUNT', 10);
41
define('DEFAULT_COUNT', 3);
42
define('MAX_WAIT', 10);
43
define('DEFAULT_WAIT', 1);
44
$do_ping = false;
45
$host = '';
46
$count = DEFAULT_COUNT;
47
$wait = DEFAULT_WAIT;
48

    
49
if ($_POST || $_REQUEST['host']) {
50
	unset($input_errors);
51
	unset($do_ping);
52

    
53
	/* input validation */
54
	$reqdfields = explode(" ", "host count");
55
	$reqdfieldsn = array(gettext("Host"), gettext("Count"));
56
	do_input_validation($_REQUEST, $reqdfields, $reqdfieldsn, $input_errors);
57
	if (($_REQUEST['count'] < 1) || ($_REQUEST['count'] > MAX_COUNT) || (!is_numericint($_REQUEST['count']))) {
58
		$input_errors[] = sprintf(gettext("Count must be between 1 and %s"), MAX_COUNT);
59
	}	
60
	if (isset($_REQUEST['wait']) && (($_REQUEST['wait'] < 1) ||
61
	    ($_REQUEST['wait'] > MAX_WAIT) || (!is_numericint($_REQUEST['wait'])))) {
62
		$input_errors[] = sprintf(gettext("Wait must be between 1 and %s"), MAX_WAIT);
63
	}	
64
	$host = trim($_REQUEST['host']);
65
	$ipproto = $_REQUEST['ipproto'];
66
	if (($ipproto == "ipv4") && is_ipaddrv6($host)) {
67
		$input_errors[] = gettext("When using IPv4, the target host must be an IPv4 address or hostname.");
68
	}
69
	if (($ipproto == "ipv6") && is_ipaddrv4($host)) {
70
		$input_errors[] = gettext("When using IPv6, the target host must be an IPv6 address or hostname.");
71
	}
72
	if (!is_ipaddr($host) && !is_hostname($host)) {
73
		$input_errors[] = gettext("Hostname must be a valid hostname or IP address.");
74
	}
75

    
76
	if (!$input_errors) {
77
		if ($_POST) {
78
			$do_ping = true;
79
		}
80
		if (isset($_REQUEST['sourceip'])) {
81
			$sourceip = $_REQUEST['sourceip'];
82
		}
83
		$count = (empty($_REQUEST['count'])) ? DEFAULT_WAIT : $_REQUEST['count'];
84
		$wait = (empty($_REQUEST['wait'])) ? DEFAULT_WAIT : $_REQUEST['wait'];
85
	}
86
}
87

    
88
if ($do_ping) {
89
?>
90
	<script type="text/javascript">
91
	//<![CDATA[
92
	window.onload=function() {
93
		document.getElementById("pingCaptured").wrap='off';
94
	}
95
	//]]>
96
	</script>
97
<?php
98
	$ifscope = '';
99
	$command = "/sbin/ping";
100
	if ($ipproto == "ipv6") {
101
		$command .= "6";
102
		$ifaddr = is_ipaddr($sourceip) ? $sourceip : get_interface_ipv6($sourceip);
103
		if (is_linklocal($ifaddr)) {
104
			$ifscope = get_ll_scope($ifaddr);
105
		}
106
	} 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
		if (is_linklocal($host) && !strstr($host, "%") && !empty($ifscope)) {
113
			$host .= "%{$ifscope}";
114
		}
115
	}
116

    
117
	$cmd = "{$command} {$srcip} -c" . escapeshellarg($count) . " -i" . escapeshellarg($wait) . " " . escapeshellarg($host);
118
	//echo "Ping command: {$cmd}\n";
119
	$result = shell_exec($cmd);
120

    
121
	if (empty($result)) {
122
		$input_errors[] = sprintf(gettext('Host "%s" did not respond or could not be resolved.'), $host);
123
	}
124

    
125
}
126

    
127
include('head.inc');
128

    
129
if ($input_errors) {
130
	print_input_errors($input_errors);
131
}
132

    
133
$form = new Form(false);
134

    
135
$section = new Form_Section('Ping');
136

    
137
$section->addInput(new Form_Input(
138
	'host',
139
	'*Hostname',
140
	'text',
141
	$host,
142
	['placeholder' => 'Hostname to ping']
143
));
144

    
145
$section->addInput(new Form_Select(
146
	'ipproto',
147
	'*IP Protocol',
148
	$ipproto,
149
	['ipv4' => 'IPv4', 'ipv6' => 'IPv6']
150
));
151

    
152
$section->addInput(new Form_Select(
153
	'sourceip',
154
	'*Source address',
155
	$sourceip,
156
	array('' => gettext('Automatically selected (default)')) + get_possible_traffic_source_addresses(true)
157
))->setHelp('Select source address for the ping.');
158

    
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
))->setHelp('Select the maximum number of pings.');
165

    
166
$section->addInput(new Form_Select(
167
	'wait',
168
	'Seconds between pings',
169
	$wait,
170
	array_combine(range(1, MAX_WAIT), range(1, MAX_WAIT))
171
))->setHelp('Select the number of seconds to wait between pings.');
172

    
173
$form->add($section);
174

    
175
$form->addGlobal(new Form_Button(
176
	'Submit',
177
	'Ping',
178
	null,
179
	'fa-rss'
180
))->addClass('btn-primary');
181

    
182
print $form;
183

    
184
if ($do_ping && !empty($result) && !$input_errors) {
185
?>
186
	<div class="panel panel-default">
187
		<div class="panel-heading">
188
			<h2 class="panel-title"><?=gettext('Results')?></h2>
189
		</div>
190

    
191
		<div class="panel-body">
192
			<pre><?= htmlspecialchars($result) ?></pre>
193
		</div>
194
	</div>
195
<?php
196
}
197

    
198
include('foot.inc');
(25-25/227)