Project

General

Profile

Download (5.16 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['wait']))) {
58
		$input_errors[] = sprintf(gettext("Count must be between 1 and %s"), MAX_COUNT);
59
	}	
60
	if (($_REQUEST['wait'] < 1) || ($_REQUEST['wait'] > MAX_WAIT) || (!is_numericint($_REQUEST['wait']))) {
61
		$input_errors[] = sprintf(gettext("Wait must be between 1 and %s"), MAX_WAIT);
62
	}	
63
	$host = trim($_REQUEST['host']);
64
	$ipproto = $_REQUEST['ipproto'];
65
	if (($ipproto == "ipv4") && is_ipaddrv6($host)) {
66
		$input_errors[] = gettext("When using IPv4, the target host must be an IPv4 address or hostname.");
67
	}
68
	if (($ipproto == "ipv6") && is_ipaddrv4($host)) {
69
		$input_errors[] = gettext("When using IPv6, the target host must be an IPv6 address or hostname.");
70
	}
71

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

    
84
if ($do_ping) {
85
?>
86
	<script type="text/javascript">
87
	//<![CDATA[
88
	window.onload=function() {
89
		document.getElementById("pingCaptured").wrap='off';
90
	}
91
	//]]>
92
	</script>
93
<?php
94
	$ifscope = '';
95
	$command = "/sbin/ping";
96
	if ($ipproto == "ipv6") {
97
		$command .= "6";
98
		$ifaddr = is_ipaddr($sourceip) ? $sourceip : get_interface_ipv6($sourceip);
99
		if (is_linklocal($ifaddr)) {
100
			$ifscope = get_ll_scope($ifaddr);
101
		}
102
	} else {
103
		$ifaddr = is_ipaddr($sourceip) ? $sourceip : get_interface_ip($sourceip);
104
	}
105

    
106
	if ($ifaddr && (is_ipaddr($host) || is_hostname($host))) {
107
		$srcip = "-S" . escapeshellarg($ifaddr);
108
		if (is_linklocal($host) && !strstr($host, "%") && !empty($ifscope)) {
109
			$host .= "%{$ifscope}";
110
		}
111
	}
112

    
113
	$cmd = "{$command} {$srcip} -c" . escapeshellarg($count) . " -i" . escapeshellarg($wait) . " " . escapeshellarg($host);
114
	//echo "Ping command: {$cmd}\n";
115
	$result = shell_exec($cmd);
116

    
117
	if (empty($result)) {
118
		$input_errors[] = sprintf(gettext('Host "%s" did not respond or could not be resolved.'), $host);
119
	}
120

    
121
}
122

    
123
include('head.inc');
124

    
125
if ($input_errors) {
126
	print_input_errors($input_errors);
127
}
128

    
129
$form = new Form(false);
130

    
131
$section = new Form_Section('Ping');
132

    
133
$section->addInput(new Form_Input(
134
	'host',
135
	'*Hostname',
136
	'text',
137
	$host,
138
	['placeholder' => 'Hostname to ping']
139
));
140

    
141
$section->addInput(new Form_Select(
142
	'ipproto',
143
	'*IP Protocol',
144
	$ipproto,
145
	['ipv4' => 'IPv4', 'ipv6' => 'IPv6']
146
));
147

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

    
155
$section->addInput(new Form_Select(
156
	'count',
157
	'Maximum number of pings',
158
	$count,
159
	array_combine(range(1, MAX_COUNT), range(1, MAX_COUNT))
160
))->setHelp('Select the maximum number of pings.');
161

    
162
$section->addInput(new Form_Select(
163
	'wait',
164
	'Seconds between pings',
165
	$wait,
166
	array_combine(range(1, MAX_WAIT), range(1, MAX_WAIT))
167
))->setHelp('Select the number of seconds to wait between pings.');
168

    
169
$form->add($section);
170

    
171
$form->addGlobal(new Form_Button(
172
	'Submit',
173
	'Ping',
174
	null,
175
	'fa-rss'
176
))->addClass('btn-primary');
177

    
178
print $form;
179

    
180
if ($do_ping && !empty($result) && !$input_errors) {
181
?>
182
	<div class="panel panel-default">
183
		<div class="panel-heading">
184
			<h2 class="panel-title"><?=gettext('Results')?></h2>
185
		</div>
186

    
187
		<div class="panel-body">
188
			<pre><?= $result ?></pre>
189
		</div>
190
	</div>
191
<?php
192
}
193

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