Project

General

Profile

Download (5.32 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	diag_ping.php
4
	part of m0n0wall (http://m0n0.ch/wall)
5

    
6
	Copyright (C) 2003-2005 Bob Zoller (bob@kludgebox.com) and Manuel Kasper <mk@neon1.net>.
7
	All rights reserved.
8

    
9
	Copyright (C) 2013-2015 Electric Sheep Fencing, LP
10
	All rights reserved.
11

    
12
	Redistribution and use in source and binary forms, with or without
13
	modification, 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 the
20
	documentation and/or other materials provided with the distribution.
21

    
22
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
23
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
24
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
26
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31
	POSSIBILITY OF SUCH DAMAGE.
32
*/
33

    
34
/*
35
	pfSense_BUILDER_BINARIES:	/sbin/ping /sbin/ping6
36
	pfSense_MODULE: routing
37
*/
38

    
39
##|+PRIV
40
##|*IDENT=page-diagnostics-ping
41
##|*NAME=Diagnostics: Ping page
42
##|*DESCR=Allow access to the 'Diagnostics: Ping' page.
43
##|*MATCH=diag_ping.php*
44
##|-PRIV
45

    
46
$allowautocomplete = true;
47
$pgtitle = array(gettext("Diagnostics"), gettext("Ping"));
48
require_once("guiconfig.inc");
49

    
50
define('MAX_COUNT', 10);
51
define('DEFAULT_COUNT', 3);
52

    
53
function create_sourceaddresslist() {
54
	$list = array('any' => 'Any');
55

    
56
	foreach (get_possible_traffic_source_addresses(true) as $sipname)
57
		$list[$sipname['value']] = $sipname['name'];
58

    
59
	return $list;
60
}
61

    
62
if ($_POST || $_REQUEST['host']) {
63
	unset($input_errors);
64
	unset($do_ping);
65

    
66
	/* input validation */
67
	$reqdfields = explode(" ", "host count");
68
	$reqdfieldsn = array(gettext("Host"),gettext("Count"));
69
	do_input_validation($_REQUEST, $reqdfields, $reqdfieldsn, $input_errors);
70

    
71
	if (($_REQUEST['count'] < 1) || ($_REQUEST['count'] > MAX_COUNT)) {
72
		$input_errors[] = sprintf(gettext("Count must be between 1 and %s"), MAX_COUNT);
73
	}
74

    
75
	$host = trim($_REQUEST['host']);
76
	$ipproto = $_REQUEST['ipproto'];
77
	if (($ipproto == "ipv4") && is_ipaddrv6($host))
78
		$input_errors[] = gettext("When using IPv4, the target host must be an IPv4 address or hostname.");
79
	if (($ipproto == "ipv6") && is_ipaddrv4($host))
80
		$input_errors[] = gettext("When using IPv6, the target host must be an IPv6 address or hostname.");
81

    
82
	if (!$input_errors) {
83
		$do_ping = true;
84
		$sourceip = $_REQUEST['sourceip'];
85
		$count = $_POST['count'];
86
		if (preg_match('/[^0-9]/', $count) )
87
			$count = DEFAULT_COUNT;
88
	}
89
}
90

    
91
if (!isset($do_ping)) {
92
	$do_ping = false;
93
	$host = '';
94
	$count = DEFAULT_COUNT;
95
}
96

    
97
if($do_ping) {
98
?>
99
	<script type="text/javascript">
100
	//<![CDATA[
101
	window.onload=function(){
102
		document.getElementById("pingCaptured").wrap='off';
103
	}
104
	//]]>
105
	</script>
106
<?php
107
	$ifscope = '';
108
	$command = "/sbin/ping";
109
	if ($ipproto == "ipv6") {
110
		$command .= "6";
111
		$ifaddr = is_ipaddr($sourceip) ? $sourceip : get_interface_ipv6($sourceip);
112
		if (is_linklocal($ifaddr))
113
			$ifscope = get_ll_scope($ifaddr);
114
	} else {
115
		$ifaddr = is_ipaddr($sourceip) ? $sourceip : get_interface_ip($sourceip);
116
	}
117

    
118
	if ($ifaddr && (is_ipaddr($host) || is_hostname($host))) {
119
		$srcip = "-S" . escapeshellarg($ifaddr);
120
		if (is_linklocal($host) && !strstr($host, "%") && !empty($ifscope))
121
			$host .= "%{$ifscope}";
122
	}
123

    
124
	$cmd = "{$command} {$srcip} -c" . escapeshellarg($count) . " " . escapeshellarg($host);
125
	//echo "Ping command: {$cmd}\n";
126
	$result = shell_exec($cmd);
127

    
128
	if(empty($result))
129
		$input_errors[] = "Host \"" . $host . "\" did not respond or could not be resolved.";
130

    
131
}
132

    
133
include('head.inc');
134

    
135
if ($input_errors)
136
	print_input_errors($input_errors);
137

    
138
require('classes/Form.class.php');
139

    
140
$form = new Form('Ping');
141

    
142
$section = new Form_Section('Ping');
143

    
144
$section->addInput(new Form_Input(
145
	'host',
146
	'Hostname',
147
	'text',
148
	$host,
149
	['placeholder' => 'Hostname to ping']
150
));
151

    
152
$group = new Form_Group('IP Protocol');
153
$group->add(new Form_Checkbox(
154
	'ipproto',
155
	null,
156
	'IPv4',
157
	('ipv6' != $ipproto), # negative check, so this would be checked by default
158
	'ipv4'
159
))->displayAsRadio();
160
$group->add(new Form_Checkbox(
161
	'ipproto',
162
	null,
163
	'IPv6',
164
	('ipv6' == $ipproto),
165
	'ipv6'
166
))->displayAsRadio();
167
$group->setHelp('Select the protocol to use');
168
$section->add($group);
169

    
170
$section->addInput(new Form_Select(
171
	'sourceip',
172
	'Source address',
173
	$pconfig['source'],
174
	create_sourceaddresslist()
175
))->setHelp('Select source address for the ping');
176

    
177
$section->addInput(new Form_Select(
178
	'count',
179
	'Maximum number of pings',
180
	$count,
181
	array_combine(range(1, MAX_COUNT), range(1, MAX_COUNT))
182
))->setHelp('Select the maximum number pings');
183

    
184
$form->add($section);
185
print $form;
186

    
187
if($do_ping && !empty($result) && !$input_errors) {
188
?>
189
	<div class="panel panel-default">
190
		<div class="panel-heading">
191
			<h2 class="panel-title">Results</h2>
192
		</div>
193

    
194
		<div class="panel-body">
195
			<pre><?= $result ?></pre>
196
		</div>
197
	</div>
198
<?php
199
}
200

    
201
include('foot.inc');
(31-31/241)