Project

General

Profile

Download (6.15 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 Electric Sheep Fencing, LLC
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
 * Redistribution and use in source and binary forms, with or without
15
 * modification, are permitted provided that the following conditions are met:
16
 *
17
 * 1. Redistributions of source code must retain the above copyright notice,
18
 *    this list of conditions and the following disclaimer.
19
 *
20
 * 2. Redistributions in binary form must reproduce the above copyright
21
 *    notice, this list of conditions and the following disclaimer in
22
 *    the documentation and/or other materials provided with the
23
 *    distribution.
24
 *
25
 * 3. All advertising materials mentioning features or use of this software
26
 *    must display the following acknowledgment:
27
 *    "This product includes software developed by the pfSense Project
28
 *    for use in the pfSense® software distribution. (http://www.pfsense.org/).
29
 *
30
 * 4. The names "pfSense" and "pfSense Project" must not be used to
31
 *    endorse or promote products derived from this software without
32
 *    prior written permission. For written permission, please contact
33
 *    coreteam@pfsense.org.
34
 *
35
 * 5. Products derived from this software may not be called "pfSense"
36
 *    nor may "pfSense" appear in their names without prior written
37
 *    permission of the Electric Sheep Fencing, LLC.
38
 *
39
 * 6. Redistributions of any form whatsoever must retain the following
40
 *    acknowledgment:
41
 *
42
 * "This product includes software developed by the pfSense Project
43
 * for use in the pfSense software distribution (http://www.pfsense.org/).
44
 *
45
 * THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
46
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
48
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
49
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
50
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
51
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
52
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
54
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
55
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
56
 * OF THE POSSIBILITY OF SUCH DAMAGE.
57
 */
58

    
59
##|+PRIV
60
##|*IDENT=page-diagnostics-ping
61
##|*NAME=Diagnostics: Ping
62
##|*DESCR=Allow access to the 'Diagnostics: Ping' page.
63
##|*MATCH=diag_ping.php*
64
##|-PRIV
65

    
66
$allowautocomplete = true;
67
$pgtitle = array(gettext("Diagnostics"), gettext("Ping"));
68
require_once("guiconfig.inc");
69

    
70
define('MAX_COUNT', 10);
71
define('DEFAULT_COUNT', 3);
72
$do_ping = false;
73
$host = '';
74
$count = DEFAULT_COUNT;
75

    
76
if ($_POST || $_REQUEST['host']) {
77
	unset($input_errors);
78
	unset($do_ping);
79

    
80
	/* input validation */
81
	$reqdfields = explode(" ", "host count");
82
	$reqdfieldsn = array(gettext("Host"), gettext("Count"));
83
	do_input_validation($_REQUEST, $reqdfields, $reqdfieldsn, $input_errors);
84

    
85
	if (($_REQUEST['count'] < 1) || ($_REQUEST['count'] > MAX_COUNT)) {
86
		$input_errors[] = sprintf(gettext("Count must be between 1 and %s"), MAX_COUNT);
87
	}
88

    
89
	$host = trim($_REQUEST['host']);
90
	$ipproto = $_REQUEST['ipproto'];
91
	if (($ipproto == "ipv4") && is_ipaddrv6($host)) {
92
		$input_errors[] = gettext("When using IPv4, the target host must be an IPv4 address or hostname.");
93
	}
94
	if (($ipproto == "ipv6") && is_ipaddrv4($host)) {
95
		$input_errors[] = gettext("When using IPv6, the target host must be an IPv6 address or hostname.");
96
	}
97

    
98
	if (!$input_errors) {
99
		if ($_POST) {
100
			$do_ping = true;
101
		}
102
		if (isset($_REQUEST['sourceip'])) {
103
			$sourceip = $_REQUEST['sourceip'];
104
		}
105
		$count = $_REQUEST['count'];
106
		if (preg_match('/[^0-9]/', $count)) {
107
			$count = DEFAULT_COUNT;
108
		}
109
	}
110
}
111

    
112
if ($do_ping) {
113
?>
114
	<script type="text/javascript">
115
	//<![CDATA[
116
	window.onload=function() {
117
		document.getElementById("pingCaptured").wrap='off';
118
	}
119
	//]]>
120
	</script>
121
<?php
122
	$ifscope = '';
123
	$command = "/sbin/ping";
124
	if ($ipproto == "ipv6") {
125
		$command .= "6";
126
		$ifaddr = is_ipaddr($sourceip) ? $sourceip : get_interface_ipv6($sourceip);
127
		if (is_linklocal($ifaddr)) {
128
			$ifscope = get_ll_scope($ifaddr);
129
		}
130
	} else {
131
		$ifaddr = is_ipaddr($sourceip) ? $sourceip : get_interface_ip($sourceip);
132
	}
133

    
134
	if ($ifaddr && (is_ipaddr($host) || is_hostname($host))) {
135
		$srcip = "-S" . escapeshellarg($ifaddr);
136
		if (is_linklocal($host) && !strstr($host, "%") && !empty($ifscope)) {
137
			$host .= "%{$ifscope}";
138
		}
139
	}
140

    
141
	$cmd = "{$command} {$srcip} -c" . escapeshellarg($count) . " " . escapeshellarg($host);
142
	//echo "Ping command: {$cmd}\n";
143
	$result = shell_exec($cmd);
144

    
145
	if (empty($result)) {
146
		$input_errors[] = sprintf(gettext('Host "%s" did not respond or could not be resolved.'), $host);
147
	}
148

    
149
}
150

    
151
include('head.inc');
152

    
153
if ($input_errors) {
154
	print_input_errors($input_errors);
155
}
156

    
157
$form = new Form(false);
158

    
159
$section = new Form_Section('Ping');
160

    
161
$section->addInput(new Form_Input(
162
	'host',
163
	'Hostname',
164
	'text',
165
	$host,
166
	['placeholder' => 'Hostname to ping']
167
));
168

    
169
$section->addInput(new Form_Select(
170
	'ipproto',
171
	'IP Protocol',
172
	$ipproto,
173
	['ipv4' => 'IPv4', 'ipv6' => 'IPv6']
174
));
175

    
176
$section->addInput(new Form_Select(
177
	'sourceip',
178
	'Source address',
179
	$sourceip,
180
	array('' => gettext('Automatically selected (default)')) + get_possible_traffic_source_addresses(true)
181
))->setHelp('Select source address for the ping.');
182

    
183
$section->addInput(new Form_Select(
184
	'count',
185
	'Maximum number of pings',
186
	$count,
187
	array_combine(range(1, MAX_COUNT), range(1, MAX_COUNT))
188
))->setHelp('Select the maximum number of pings.');
189

    
190
$form->add($section);
191

    
192
$form->addGlobal(new Form_Button(
193
	'Submit',
194
	'Ping',
195
	null,
196
	'fa-rss'
197
))->addClass('btn-primary');
198

    
199
print $form;
200

    
201
if ($do_ping && !empty($result) && !$input_errors) {
202
?>
203
	<div class="panel panel-default">
204
		<div class="panel-heading">
205
			<h2 class="panel-title"><?=gettext('Results')?></h2>
206
		</div>
207

    
208
		<div class="panel-body">
209
			<pre><?= $result ?></pre>
210
		</div>
211
	</div>
212
<?php
213
}
214

    
215
include('foot.inc');
(22-22/225)