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
|
$sourceips = get_possible_traffic_source_addresses(true);
|
57
|
|
58
|
foreach ($sourceips as $sipvalue => $sipname)
|
59
|
$list[$sipname[value]] = $sipname[name];
|
60
|
|
61
|
return($list);
|
62
|
}
|
63
|
|
64
|
if ($_POST || $_REQUEST['host']) {
|
65
|
unset($input_errors);
|
66
|
unset($do_ping);
|
67
|
|
68
|
/* input validation */
|
69
|
$reqdfields = explode(" ", "host count");
|
70
|
$reqdfieldsn = array(gettext("Host"),gettext("Count"));
|
71
|
do_input_validation($_REQUEST, $reqdfields, $reqdfieldsn, $input_errors);
|
72
|
|
73
|
if (($_REQUEST['count'] < 1) || ($_REQUEST['count'] > MAX_COUNT)) {
|
74
|
$input_errors[] = sprintf(gettext("Count must be between 1 and %s"), MAX_COUNT);
|
75
|
}
|
76
|
|
77
|
$host = trim($_REQUEST['host']);
|
78
|
$ipproto = $_REQUEST['ipproto'];
|
79
|
if (($ipproto == "ipv4") && is_ipaddrv6($host))
|
80
|
$input_errors[] = gettext("When using IPv4, the target host must be an IPv4 address or hostname.");
|
81
|
if (($ipproto == "ipv6") && is_ipaddrv4($host))
|
82
|
$input_errors[] = gettext("When using IPv6, the target host must be an IPv6 address or hostname.");
|
83
|
|
84
|
if (!$input_errors) {
|
85
|
$do_ping = true;
|
86
|
$sourceip = $_REQUEST['sourceip'];
|
87
|
$count = $_POST['count'];
|
88
|
if (preg_match('/[^0-9]/', $count) )
|
89
|
$count = DEFAULT_COUNT;
|
90
|
}
|
91
|
}
|
92
|
|
93
|
if (!isset($do_ping)) {
|
94
|
$do_ping = false;
|
95
|
$host = '';
|
96
|
$count = DEFAULT_COUNT;
|
97
|
}
|
98
|
|
99
|
if($do_ping) {
|
100
|
?>
|
101
|
<script type="text/javascript">
|
102
|
//<![CDATA[
|
103
|
window.onload=function(){
|
104
|
document.getElementById("pingCaptured").wrap='off';
|
105
|
}
|
106
|
//]]>
|
107
|
</script>
|
108
|
<?php
|
109
|
$ifscope = '';
|
110
|
$command = "/sbin/ping";
|
111
|
if ($ipproto == "ipv6") {
|
112
|
$command .= "6";
|
113
|
$ifaddr = is_ipaddr($sourceip) ? $sourceip : get_interface_ipv6($sourceip);
|
114
|
if (is_linklocal($ifaddr))
|
115
|
$ifscope = get_ll_scope($ifaddr);
|
116
|
} else {
|
117
|
$ifaddr = is_ipaddr($sourceip) ? $sourceip : get_interface_ip($sourceip);
|
118
|
}
|
119
|
|
120
|
if ($ifaddr && (is_ipaddr($host) || is_hostname($host))) {
|
121
|
$srcip = "-S" . escapeshellarg($ifaddr);
|
122
|
if (is_linklocal($host) && !strstr($host, "%") && !empty($ifscope))
|
123
|
$host .= "%{$ifscope}";
|
124
|
}
|
125
|
|
126
|
$cmd = "{$command} {$srcip} -c" . escapeshellarg($count) . " " . escapeshellarg($host);
|
127
|
//echo "Ping command: {$cmd}\n";
|
128
|
$result = shell_exec($cmd);
|
129
|
|
130
|
if(empty($result))
|
131
|
$input_errors[] = "Host \"" . $host . "\" did not respond or could not be resolved.";
|
132
|
|
133
|
}
|
134
|
|
135
|
include("head.inc"); ?>
|
136
|
|
137
|
<?php if ($input_errors)
|
138
|
print_input_errors($input_errors);
|
139
|
|
140
|
require('classes/Form.class.php');
|
141
|
|
142
|
$form = new Form(new Form_Button(
|
143
|
'Submit',
|
144
|
'Ping'
|
145
|
));
|
146
|
|
147
|
$section = new Form_Section('Traceroute');
|
148
|
|
149
|
$section->addInput(new Form_Input(
|
150
|
'host',
|
151
|
'Hostname',
|
152
|
'text',
|
153
|
$host,
|
154
|
['placeholder' => 'Hostname to ping.']
|
155
|
));
|
156
|
|
157
|
$section->addInput(new Form_Select(
|
158
|
'ipproto',
|
159
|
'IP Protocol',
|
160
|
$pconfig['protocol'],
|
161
|
array('ipv4' => 'IPv4', 'ipv6' => 'IPv6')
|
162
|
))->setHelp('Select the protocol to use');
|
163
|
|
164
|
$section->addInput(new Form_Select(
|
165
|
'sourceip',
|
166
|
'Source Address',
|
167
|
$pconfig['source'],
|
168
|
create_sourceaddresslist()
|
169
|
))->setHelp('Select source address for the ping');
|
170
|
|
171
|
$section->addInput(new Form_Select(
|
172
|
'count',
|
173
|
'Maximum nuber of pings',
|
174
|
$count,
|
175
|
array_combine(range(1, MAX_COUNT), range(1, MAX_COUNT))
|
176
|
))->setHelp('Select the maximum number pings');
|
177
|
|
178
|
$form->add($section);
|
179
|
print $form;
|
180
|
|
181
|
if($do_ping && !empty($result) && !$input_errors) {
|
182
|
print("<div class=\"panel panel-info\">" . "<div class=\"panel-heading\">Results</div>");
|
183
|
print(" " . str_replace("\n", "<br /> ", $result));
|
184
|
print('</div></div>');
|
185
|
}
|
186
|
|
187
|
include("foot.inc");
|
188
|
|
189
|
?>
|
190
|
|