]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/icmp/IcmpSquid.cc
2 * DEBUG: section 37 ICMP Routines
3 * AUTHOR: Duane Wessels, Amos Jeffries
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
35 #include "comm/Loops.h"
38 #include "icmp/IcmpSquid.h"
39 #include "icmp/net_db.h"
41 #include "SquidConfig.h"
43 #include "SquidTime.h"
49 // Instance global to be available in main() and elsewhere.
62 IcmpSquid::IcmpSquid() : Icmp()
67 IcmpSquid::~IcmpSquid()
75 IcmpSquid::SendEcho(Ip::Address
&to
, int opcode
, const char *payload
, int len
)
77 static pingerEchoData pecho
;
80 /** \li Does nothing if the pinger socket is not available. */
82 debugs(37, 2, HERE
<< " Socket Closed. Aborted send to " << pecho
.to
<< ", opcode " << opcode
<< ", len " << pecho
.psize
);
86 /** \li If no payload is given or is set as NULL it will ignore payload and len */
90 /** \li Otherwise if len is 0, uses strlen() to detect length of payload.
91 \bug This will result in part of the payload being truncated if it contains a NULL character.
92 \bug Or it may result in a buffer over-run if the payload is not nul-terminated properly.
94 else if (payload
&& len
== 0)
95 len
= strlen(payload
);
98 \bug If length specified or auto-detected is greater than the possible payload squid will die with an assert.
99 \todo This should perhapse be reduced to a truncated payload? or no payload. A WARNING is due anyway.
101 assert(len
<= PINGER_PAYLOAD_SZ
);
105 pecho
.opcode
= (unsigned char) opcode
;
110 memcpy(pecho
.payload
, payload
, len
);
112 slen
= sizeof(pingerEchoData
) - PINGER_PAYLOAD_SZ
+ pecho
.psize
;
114 debugs(37, 2, HERE
<< "to " << pecho
.to
<< ", opcode " << opcode
<< ", len " << pecho
.psize
);
116 x
= comm_udp_send(icmp_sock
, (char *)&pecho
, slen
, 0);
119 debugs(37, DBG_IMPORTANT
, HERE
<< "send: " << xstrerror());
121 /** \li If the send results in ECONNREFUSED or EPIPE errors from helper, will cleanly shutdown the module. */
122 /** \todo This should try restarting the helper a few times?? before giving up? */
123 if (errno
== ECONNREFUSED
|| errno
== EPIPE
) {
127 /** All other send errors are ignored. */
128 } else if (x
!= slen
) {
129 debugs(37, DBG_IMPORTANT
, HERE
<< "Wrote " << x
<< " of " << slen
<< " bytes");
133 // static Callback to wrap the squid-side ICMP handler.
134 // the IcmpSquid::Recv cannot be declared both static and virtual.
136 icmpSquidRecv(int unused1
, void *unused2
)
145 static int fail_count
= 0;
146 pingerReplyData preply
;
147 static Ip::Address F
;
149 Comm::SetSelect(icmp_sock
, COMM_SELECT_READ
, icmpSquidRecv
, NULL
, 0);
150 memset(&preply
, '\0', sizeof(pingerReplyData
));
151 n
= comm_udp_recv(icmp_sock
,
153 sizeof(pingerReplyData
),
156 if (n
< 0 && EAGAIN
!= errno
) {
157 debugs(37, DBG_IMPORTANT
, HERE
<< "recv: " << xstrerror());
159 if (errno
== ECONNREFUSED
)
162 if (errno
== ECONNRESET
)
165 if (++fail_count
== 10)
173 /** If its a test probe from the pinger. Do nothing. */
182 switch (preply
.opcode
) {
185 debugs(37,4, HERE
<< " ICMP_ECHO of " << preply
.from
<< " gave: hops=" << preply
.hops
<<", rtt=" << preply
.rtt
);
189 debugs(37,4, HERE
<< " DomainPing of " << preply
.from
<< " gave: hops=" << preply
.hops
<<", rtt=" << preply
.rtt
);
190 netdbHandlePingReply(F
, preply
.hops
, preply
.rtt
);
194 debugs(37, DBG_IMPORTANT
, HERE
<< "Bad opcode: " << preply
.opcode
<< " from " << F
);
199 #endif /* USE_ICMP */
202 IcmpSquid::DomainPing(Ip::Address
&to
, const char *domain
)
205 debugs(37, 4, HERE
<< "'" << domain
<< "' (" << to
<< ")");
206 SendEcho(to
, S_ICMP_DOM
, domain
, 0);
211 IcmpSquid::Open(void)
217 Ip::Address localhost
;
219 /* User configured disabled. */
220 if (!Config
.pinger
.enable
) {
225 args
[0] = "(pinger)";
227 localhost
.setLocalhost();
230 * Do NOT use IPC_DGRAM (=IPC_UNIX_DGRAM) here because you can't
231 * send() more than 4096 bytes on a socketpair() socket (at
234 pid
= ipcCreate(IPC_UDP_SOCKET
,
235 Config
.pinger
.program
,
250 fd_note(icmp_sock
, "pinger");
252 Comm::SetSelect(icmp_sock
, COMM_SELECT_READ
, icmpSquidRecv
, NULL
, 0);
254 commUnsetFdTimeout(icmp_sock
);
256 debugs(37, DBG_IMPORTANT
, HERE
<< "Pinger socket opened on FD " << icmp_sock
);
258 /* Tests the pinger immediately using localhost */
260 SendEcho(localhost
, S_ICMP_ECHO
, "ip6-localhost");
261 if (localhost
.setIPv4())
262 SendEcho(localhost
, S_ICMP_ECHO
, "localhost");
266 debugs(37, 4, HERE
<< "Pinger handle: 0x" << std::hex
<< hIpc
<< std::dec
<< ", PID: " << pid
);
268 #endif /* _SQUID_WINDOWS_ */
272 #endif /* USE_ICMP */
276 IcmpSquid::Close(void)
283 debugs(37, DBG_IMPORTANT
, HERE
<< "Closing Pinger socket on FD " << icmp_sock
);
287 send(icmp_sock
, (const void *) "$shutdown\n", 10, 0);
291 comm_close(icmp_sock
);
296 if (WaitForSingleObject(hIpc
, 12000) != WAIT_OBJECT_0
) {
298 debugs(37, DBG_CRITICAL
, HERE
<< "WARNING: (pinger," << pid
<< ") didn't exit in 12 seconds");