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(new Form_Button(
|
141
|
'submit',
|
142
|
'Ping'
|
143
|
));
|
144
|
|
145
|
$section = new Form_Section('Ping');
|
146
|
|
147
|
$section->addInput(new Form_Input(
|
148
|
'host',
|
149
|
'Hostname',
|
150
|
'text',
|
151
|
$host,
|
152
|
['placeholder' => 'Hostname to ping']
|
153
|
));
|
154
|
|
155
|
$group = new Form_Group('IP Protocol');
|
156
|
$group->add(new Form_Checkbox(
|
157
|
'ipproto',
|
158
|
null,
|
159
|
'IPv4',
|
160
|
('ipv6' != $ipproto), # negative check, so this would be checked by default
|
161
|
'ipv4'
|
162
|
))->displayAsRadio();
|
163
|
$group->add(new Form_Checkbox(
|
164
|
'ipproto',
|
165
|
null,
|
166
|
'IPv6',
|
167
|
('ipv6' == $ipproto),
|
168
|
'ipv6'
|
169
|
))->displayAsRadio();
|
170
|
$group->setHelp('Select the protocol to use');
|
171
|
$section->add($group);
|
172
|
|
173
|
$section->addInput(new Form_Select(
|
174
|
'sourceip',
|
175
|
'Source address',
|
176
|
$pconfig['source'],
|
177
|
create_sourceaddresslist()
|
178
|
))->setHelp('Select source address for the ping');
|
179
|
|
180
|
$section->addInput(new Form_Select(
|
181
|
'count',
|
182
|
'Maximum number of pings',
|
183
|
$count,
|
184
|
array_combine(range(1, MAX_COUNT), range(1, MAX_COUNT))
|
185
|
))->setHelp('Select the maximum number pings');
|
186
|
|
187
|
$form->add($section);
|
188
|
print $form;
|
189
|
|
190
|
if($do_ping && !empty($result) && !$input_errors) {
|
191
|
?>
|
192
|
<div class="panel panel-default">
|
193
|
<div class="panel-heading">
|
194
|
<h2 class="panel-title">Results</h2>
|
195
|
</div>
|
196
|
|
197
|
<div class="panel-body">
|
198
|
<pre><?= $result ?></pre>
|
199
|
</div>
|
200
|
</div>
|
201
|
<?php
|
202
|
}
|
203
|
|
204
|
include('foot.inc');
|