Project

General

Profile

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

    
60
/*
61
	pfSense_BUILDER_BINARIES:	/sbin/ping /sbin/ping6
62
	pfSense_MODULE: routing
63
*/
64

    
65
##|+PRIV
66
##|*IDENT=page-diagnostics-ping
67
##|*NAME=Diagnostics: Ping page
68
##|*DESCR=Allow access to the 'Diagnostics: Ping' page.
69
##|*MATCH=diag_ping.php*
70
##|-PRIV
71

    
72
$allowautocomplete = true;
73
$pgtitle = array(gettext("Diagnostics"), gettext("Ping"));
74
require_once("guiconfig.inc");
75

    
76
define('MAX_COUNT', 10);
77
define('DEFAULT_COUNT', 3);
78

    
79
function create_sourceaddresslist() {
80
	$sourceips = get_possible_traffic_source_addresses(true);
81

    
82
	$list = array("" => 'Default');
83

    
84
	foreach ($sourceips as $sipvalue => $sipname) {
85
		$list[$sipvalue] = $sipname;
86
	}
87

    
88
	return $list;
89
}
90

    
91
if ($_POST || $_REQUEST['host']) {
92
	unset($input_errors);
93
	unset($do_ping);
94

    
95
	/* input validation */
96
	$reqdfields = explode(" ", "host count");
97
	$reqdfieldsn = array(gettext("Host"), gettext("Count"));
98
	do_input_validation($_REQUEST, $reqdfields, $reqdfieldsn, $input_errors);
99

    
100
	if (($_REQUEST['count'] < 1) || ($_REQUEST['count'] > MAX_COUNT)) {
101
		$input_errors[] = sprintf(gettext("Count must be between 1 and %s"), MAX_COUNT);
102
	}
103

    
104
	$host = trim($_REQUEST['host']);
105
	$ipproto = $_REQUEST['ipproto'];
106
	if (($ipproto == "ipv4") && is_ipaddrv6($host)) {
107
		$input_errors[] = gettext("When using IPv4, the target host must be an IPv4 address or hostname.");
108
	}
109
	if (($ipproto == "ipv6") && is_ipaddrv4($host)) {
110
		$input_errors[] = gettext("When using IPv6, the target host must be an IPv6 address or hostname.");
111
	}
112

    
113
	if (!$input_errors) {
114
		$do_ping = true;
115
		$sourceip = $_REQUEST['sourceip'];
116
		$count = $_POST['count'];
117
		if (preg_match('/[^0-9]/', $count)) {
118
			$count = DEFAULT_COUNT;
119
		}
120
	}
121
}
122

    
123
if (!isset($do_ping)) {
124
	$do_ping = false;
125
	$host = '';
126
	$count = DEFAULT_COUNT;
127
}
128

    
129
if ($do_ping) {
130
?>
131
	<script type="text/javascript">
132
	//<![CDATA[
133
	window.onload=function(){
134
		document.getElementById("pingCaptured").wrap='off';
135
	}
136
	//]]>
137
	</script>
138
<?php
139
	$ifscope = '';
140
	$command = "/sbin/ping";
141
	if ($ipproto == "ipv6") {
142
		$command .= "6";
143
		$ifaddr = is_ipaddr($sourceip) ? $sourceip : get_interface_ipv6($sourceip);
144
		if (is_linklocal($ifaddr))
145
			$ifscope = get_ll_scope($ifaddr);
146
	} else {
147
		$ifaddr = is_ipaddr($sourceip) ? $sourceip : get_interface_ip($sourceip);
148
	}
149

    
150
	if ($ifaddr && (is_ipaddr($host) || is_hostname($host))) {
151
		$srcip = "-S" . escapeshellarg($ifaddr);
152
		if (is_linklocal($host) && !strstr($host, "%") && !empty($ifscope))
153
			$host .= "%{$ifscope}";
154
	}
155

    
156
	$cmd = "{$command} {$srcip} -c" . escapeshellarg($count) . " " . escapeshellarg($host);
157
	//echo "Ping command: {$cmd}\n";
158
	$result = shell_exec($cmd);
159

    
160
	if (empty($result))
161
		$input_errors[] = "Host \"" . $host . "\" did not respond or could not be resolved.";
162

    
163
}
164

    
165
include('head.inc');
166

    
167
if ($input_errors)
168
	print_input_errors($input_errors);
169

    
170
require_once('classes/Form.class.php');
171

    
172
$form = new Form('Ping');
173

    
174
$section = new Form_Section('Ping');
175

    
176
$section->addInput(new Form_Input(
177
	'host',
178
	'Hostname',
179
	'text',
180
	$host,
181
	['placeholder' => 'Hostname to ping']
182
));
183

    
184
$section->addInput(new Form_Select(
185
	'ipproto',
186
	'IP Protocol',
187
	$ipproto,
188
	['ipv4' => 'IPv4', 'ipv6' => 'IPv6']
189
));
190

    
191
$section->addInput(new Form_Select(
192
	'sourceip',
193
	'Source address',
194
	$sourceip,
195
	create_sourceaddresslist()
196
))->setHelp('Select source address for the ping');
197

    
198
$section->addInput(new Form_Select(
199
	'count',
200
	'Maximum number of pings',
201
	$count,
202
	array_combine(range(1, MAX_COUNT), range(1, MAX_COUNT))
203
))->setHelp('Select the maximum number of pings');
204

    
205
$form->add($section);
206
print $form;
207

    
208
if ($do_ping && !empty($result) && !$input_errors) {
209
?>
210
	<div class="panel panel-default">
211
		<div class="panel-heading">
212
			<h2 class="panel-title">Results</h2>
213
		</div>
214

    
215
		<div class="panel-body">
216
			<pre><?= $result ?></pre>
217
		</div>
218
	</div>
219
<?php
220
}
221

    
222
include('foot.inc');
(31-31/234)