4
* DEBUG: section 37 ICMP Routines
5
* AUTHOR: Duane Wessels, Amos Jeffries
7
* SQUID Web Proxy Cache http://www.squid-cache.org/
8
* ----------------------------------------------------------
10
* Squid is the result of efforts by numerous individuals from
11
* the Internet community; see the CONTRIBUTORS file for full
12
* details. Many organizations have provided support for Squid's
13
* development; see the SPONSORS file for full details. Squid is
14
* Copyrighted (C) 2001 by the Regents of the University of
15
* California; see the COPYRIGHT file for full details. Squid
16
* incorporates software developed and/or copyrighted by other
17
* sources; see the CREDITS file for full details.
19
* This program is free software; you can redistribute it and/or modify
20
* it under the terms of the GNU General Public License as published by
21
* the Free Software Foundation; either version 2 of the License, or
22
* (at your option) any later version.
24
* This program is distributed in the hope that it will be useful,
25
* but WITHOUT ANY WARRANTY; without even the implied warranty of
26
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27
* GNU General Public License for more details.
29
* You should have received a copy of the GNU General Public License
30
* along with this program; if not, write to the Free Software
31
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
36
#include "icmp/IcmpSquid.h"
37
#include "icmp/net_db.h"
39
#include "SquidTime.h"
41
// Instance global to be available in main() and elsewhere.
58
IcmpSquid::IcmpSquid() : Icmp()
63
IcmpSquid::~IcmpSquid()
72
IcmpSquid::SendEcho(IpAddress &to, int opcode, const char *payload, int len)
74
static pingerEchoData pecho;
77
/** \li Does nothing if the pinger socket is not available. */
79
debugs(37, 2, HERE << " Socket Closed. Aborted send to " << pecho.to << ", opcode " << opcode << ", len " << pecho.psize);
83
/** \li If no payload is given or is set as NULL it will ignore payload and len */
87
/** \li Otherwise if len is 0, uses strlen() to detect length of payload.
88
\bug This will result in part of the payload being truncated if it contains a NULL character.
89
\bug Or it may result in a buffer over-run if the payload is not nul-terminated properly.
91
else if (payload && len == 0)
92
len = strlen(payload);
95
\bug If length specified or auto-detected is greater than the possible payload squid will die with an assert.
96
\todo This should perhapse be reduced to a truncated payload? or no payload. A WARNING is due anyway.
98
assert(len <= PINGER_PAYLOAD_SZ);
102
pecho.opcode = (unsigned char) opcode;
107
xmemcpy(pecho.payload, payload, len);
109
slen = sizeof(pingerEchoData) - PINGER_PAYLOAD_SZ + pecho.psize;
111
debugs(37, 2, HERE << "to " << pecho.to << ", opcode " << opcode << ", len " << pecho.psize);
113
x = comm_udp_send(icmp_sock, (char *)&pecho, slen, 0);
116
debugs(37, 1, HERE << "send: " << xstrerror());
118
/** \li If the send results in ECONNREFUSED or EPIPE errors from helper, will cleanly shutdown the module. */
119
/** \todo This should try restarting the helper a few times?? before giving up? */
120
if (errno == ECONNREFUSED || errno == EPIPE) {
124
/** All other send errors are ignored. */
125
} else if (x != slen) {
126
debugs(37, 1, HERE << "Wrote " << x << " of " << slen << " bytes");
130
// static Callback to wrap the squid-side ICMP handler.
131
// the IcmpSquid::Recv cannot be declared both static and virtual.
133
icmpSquidRecv(int unused1, void *unused2)
142
static int fail_count = 0;
143
pingerReplyData preply;
146
commSetSelect(icmp_sock, COMM_SELECT_READ, icmpSquidRecv, NULL, 0);
147
memset(&preply, '\0', sizeof(pingerReplyData));
148
n = comm_udp_recv(icmp_sock,
150
sizeof(pingerReplyData),
153
if (n < 0 && EAGAIN != errno) {
154
debugs(37, 1, HERE << "recv: " << xstrerror());
156
if (errno == ECONNREFUSED)
159
if (errno == ECONNRESET)
162
if (++fail_count == 10)
170
/** If its a test probe from the pinger. Do nothing. */
179
switch (preply.opcode) {
182
debugs(37,4, HERE << " ICMP_ECHO of " << preply.from << " gave: hops=" << preply.hops <<", rtt=" << preply.rtt);
186
debugs(37,4, HERE << " DomainPing of " << preply.from << " gave: hops=" << preply.hops <<", rtt=" << preply.rtt);
187
netdbHandlePingReply(F, preply.hops, preply.rtt);
191
debugs(37, 1, HERE << "Bad opcode: " << preply.opcode << " from " << F);
196
#endif /* USE_ICMP */
199
IcmpSquid::DomainPing(IpAddress &to, const char *domain)
202
debugs(37, 4, HERE << "'" << domain << "' (" << to << ")");
203
SendEcho(to, S_ICMP_DOM, domain, 0);
208
IcmpSquid::Open(void)
216
/* User configured disabled. */
217
if (!Config.pinger.enable) {
222
args[0] = "(pinger)";
224
localhost.SetLocalhost();
227
* Do NOT use IPC_DGRAM (=IPC_UNIX_DGRAM) here because you can't
228
* send() more than 4096 bytes on a socketpair() socket (at
231
pid = ipcCreate(IPC_UDP_SOCKET,
232
Config.pinger.program,
247
fd_note(icmp_sock, "pinger");
249
commSetSelect(icmp_sock, COMM_SELECT_READ, icmpSquidRecv, NULL, 0);
251
commSetTimeout(icmp_sock, -1, NULL, NULL);
253
debugs(37, 1, HERE << "Pinger socket opened on FD " << icmp_sock);
255
/* Tests the pinger immediately using localhost */
257
SendEcho(localhost, S_ICMP_ECHO, "ip6-localhost");
259
if (localhost.SetIPv4())
260
SendEcho(localhost, S_ICMP_ECHO, "localhost");
264
debugs(37, 4, HERE << "Pinger handle: 0x" << std::hex << hIpc << std::dec << ", PID: " << pid);
266
#endif /* _SQUID_MSWIN_ */
270
#endif /* USE_ICMP */
274
IcmpSquid::Close(void)
281
debugs(37, 1, HERE << "Closing Pinger socket on FD " << icmp_sock);
285
send(icmp_sock, (const void *) "$shutdown\n", 10, 0);
289
comm_close(icmp_sock);
294
if (WaitForSingleObject(hIpc, 12000) != WAIT_OBJECT_0) {
296
debugs(37, 0, HERE << "WARNING: (pinger," << pid << ") didn't exit in 12 seconds");