Project

General

Profile

Download (4.5 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-2016 Rubicon Communications, LLC (Netgate)
7
 * Copyright (c) 2003-2005 Bob Zoller (bob@kludgebox.com)
8
 * All rights reserved.
9
 *
10
 * originally based on m0n0wall (http://m0n0.ch/wall)
11
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
12
 * All rights reserved.
13
 *
14
 * Licensed under the Apache License, Version 2.0 (the "License");
15
 * you may not use this file except in compliance with the License.
16
 * You may obtain a copy of the License at
17
 *
18
 * http://www.apache.org/licenses/LICENSE-2.0
19
 *
20
 * Unless required by applicable law or agreed to in writing, software
21
 * distributed under the License is distributed on an "AS IS" BASIS,
22
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23
 * See the License for the specific language governing permissions and
24
 * limitations under the License.
25
 */
26

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

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

    
38
define('MAX_COUNT', 10);
39
define('DEFAULT_COUNT', 3);
40
$do_ping = false;
41
$host = '';
42
$count = DEFAULT_COUNT;
43

    
44
if ($_POST || $_REQUEST['host']) {
45
	unset($input_errors);
46
	unset($do_ping);
47

    
48
	/* input validation */
49
	$reqdfields = explode(" ", "host count");
50
	$reqdfieldsn = array(gettext("Host"), gettext("Count"));
51
	do_input_validation($_REQUEST, $reqdfields, $reqdfieldsn, $input_errors);
52

    
53
	if (($_REQUEST['count'] < 1) || ($_REQUEST['count'] > MAX_COUNT)) {
54
		$input_errors[] = sprintf(gettext("Count must be between 1 and %s"), MAX_COUNT);
55
	}
56

    
57
	$host = trim($_REQUEST['host']);
58
	$ipproto = $_REQUEST['ipproto'];
59
	if (($ipproto == "ipv4") && is_ipaddrv6($host)) {
60
		$input_errors[] = gettext("When using IPv4, the target host must be an IPv4 address or hostname.");
61
	}
62
	if (($ipproto == "ipv6") && is_ipaddrv4($host)) {
63
		$input_errors[] = gettext("When using IPv6, the target host must be an IPv6 address or hostname.");
64
	}
65

    
66
	if (!$input_errors) {
67
		if ($_POST) {
68
			$do_ping = true;
69
		}
70
		if (isset($_REQUEST['sourceip'])) {
71
			$sourceip = $_REQUEST['sourceip'];
72
		}
73
		$count = $_REQUEST['count'];
74
		if (preg_match('/[^0-9]/', $count)) {
75
			$count = DEFAULT_COUNT;
76
		}
77
	}
78
}
79

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

    
102
	if ($ifaddr && (is_ipaddr($host) || is_hostname($host))) {
103
		$srcip = "-S" . escapeshellarg($ifaddr);
104
		if (is_linklocal($host) && !strstr($host, "%") && !empty($ifscope)) {
105
			$host .= "%{$ifscope}";
106
		}
107
	}
108

    
109
	$cmd = "{$command} {$srcip} -c" . escapeshellarg($count) . " " . escapeshellarg($host);
110
	//echo "Ping command: {$cmd}\n";
111
	$result = shell_exec($cmd);
112

    
113
	if (empty($result)) {
114
		$input_errors[] = sprintf(gettext('Host "%s" did not respond or could not be resolved.'), $host);
115
	}
116

    
117
}
118

    
119
include('head.inc');
120

    
121
if ($input_errors) {
122
	print_input_errors($input_errors);
123
}
124

    
125
$form = new Form(false);
126

    
127
$section = new Form_Section('Ping');
128

    
129
$section->addInput(new Form_Input(
130
	'host',
131
	'Hostname',
132
	'text',
133
	$host,
134
	['placeholder' => 'Hostname to ping']
135
));
136

    
137
$section->addInput(new Form_Select(
138
	'ipproto',
139
	'IP Protocol',
140
	$ipproto,
141
	['ipv4' => 'IPv4', 'ipv6' => 'IPv6']
142
));
143

    
144
$section->addInput(new Form_Select(
145
	'sourceip',
146
	'Source address',
147
	$sourceip,
148
	array('' => gettext('Automatically selected (default)')) + get_possible_traffic_source_addresses(true)
149
))->setHelp('Select source address for the ping.');
150

    
151
$section->addInput(new Form_Select(
152
	'count',
153
	'Maximum number of pings',
154
	$count,
155
	array_combine(range(1, MAX_COUNT), range(1, MAX_COUNT))
156
))->setHelp('Select the maximum number of pings.');
157

    
158
$form->add($section);
159

    
160
$form->addGlobal(new Form_Button(
161
	'Submit',
162
	'Ping',
163
	null,
164
	'fa-rss'
165
))->addClass('btn-primary');
166

    
167
print $form;
168

    
169
if ($do_ping && !empty($result) && !$input_errors) {
170
?>
171
	<div class="panel panel-default">
172
		<div class="panel-heading">
173
			<h2 class="panel-title"><?=gettext('Results')?></h2>
174
		</div>
175

    
176
		<div class="panel-body">
177
			<pre><?= $result ?></pre>
178
		</div>
179
	</div>
180
<?php
181
}
182

    
183
include('foot.inc');
(21-21/225)