Project

General

Profile

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

    
58
/*
59
	pfSense_BUILDER_BINARIES:	/sbin/ping /sbin/ping6
60
	pfSense_MODULE: routing
61
*/
62

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

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

    
74
define('MAX_COUNT', 10);
75
define('DEFAULT_COUNT', 3);
76

    
77
function create_sourceaddresslist() {
78
	$list = array('any' => 'Any');
79

    
80
	foreach (get_possible_traffic_source_addresses(true) as $sipname)
81
		$list[$sipname['value']] = $sipname['name'];
82

    
83
	return $list;
84
}
85

    
86
if ($_POST || $_REQUEST['host']) {
87
	unset($input_errors);
88
	unset($do_ping);
89

    
90
	/* input validation */
91
	$reqdfields = explode(" ", "host count");
92
	$reqdfieldsn = array(gettext("Host"), gettext("Count"));
93
	do_input_validation($_REQUEST, $reqdfields, $reqdfieldsn, $input_errors);
94

    
95
	if (($_REQUEST['count'] < 1) || ($_REQUEST['count'] > MAX_COUNT)) {
96
		$input_errors[] = sprintf(gettext("Count must be between 1 and %s"), MAX_COUNT);
97
	}
98

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

    
108
	if (!$input_errors) {
109
		$do_ping = true;
110
		$sourceip = $_REQUEST['sourceip'];
111
		$count = $_POST['count'];
112
		if (preg_match('/[^0-9]/', $count)) {
113
			$count = DEFAULT_COUNT;
114
		}
115
	}
116
}
117

    
118
if (!isset($do_ping)) {
119
	$do_ping = false;
120
	$host = '';
121
	$count = DEFAULT_COUNT;
122
}
123

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

    
145
	if ($ifaddr && (is_ipaddr($host) || is_hostname($host))) {
146
		$srcip = "-S" . escapeshellarg($ifaddr);
147
		if (is_linklocal($host) && !strstr($host, "%") && !empty($ifscope))
148
			$host .= "%{$ifscope}";
149
	}
150

    
151
	$cmd = "{$command} {$srcip} -c" . escapeshellarg($count) . " " . escapeshellarg($host);
152
	//echo "Ping command: {$cmd}\n";
153
	$result = shell_exec($cmd);
154

    
155
	if (empty($result))
156
		$input_errors[] = "Host \"" . $host . "\" did not respond or could not be resolved.";
157

    
158
}
159

    
160
include('head.inc');
161

    
162
if ($input_errors)
163
	print_input_errors($input_errors);
164

    
165
require_once('classes/Form.class.php');
166

    
167
$form = new Form('Ping');
168

    
169
$section = new Form_Section('Ping');
170

    
171
$section->addInput(new Form_Input(
172
	'host',
173
	'Hostname',
174
	'text',
175
	$host,
176
	['placeholder' => 'Hostname to ping']
177
));
178

    
179
$group = new Form_Group('IP Protocol');
180
$group->add(new Form_Checkbox(
181
	'ipproto',
182
	null,
183
	'IPv4',
184
	('ipv6' != $ipproto), # negative check, so this would be checked by default
185
	'ipv4'
186
))->displayAsRadio();
187
$group->add(new Form_Checkbox(
188
	'ipproto',
189
	null,
190
	'IPv6',
191
	('ipv6' == $ipproto),
192
	'ipv6'
193
))->displayAsRadio();
194
$group->setHelp('Select the protocol to use');
195
$section->add($group);
196

    
197
$section->addInput(new Form_Select(
198
	'sourceip',
199
	'Source address',
200
	$pconfig['source'],
201
	create_sourceaddresslist()
202
))->setHelp('Select source address for the ping');
203

    
204
$section->addInput(new Form_Select(
205
	'count',
206
	'Maximum number of pings',
207
	$count,
208
	array_combine(range(1, MAX_COUNT), range(1, MAX_COUNT))
209
))->setHelp('Select the maximum number pings');
210

    
211
$form->add($section);
212
print $form;
213

    
214
if ($do_ping && !empty($result) && !$input_errors) {
215
?>
216
	<div class="panel panel-default">
217
		<div class="panel-heading">
218
			<h2 class="panel-title">Results</h2>
219
		</div>
220

    
221
		<div class="panel-body">
222
			<pre><?= $result ?></pre>
223
		</div>
224
	</div>
225
<?php
226
}
227

    
228
include('foot.inc');
(32-32/235)