]>
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"
37 #include "icmp/IcmpSquid.h"
38 #include "icmp/net_db.h"
40 #include "SquidConfig.h"
42 #include "SquidTime.h"
48 // Instance global to be available in main() and elsewhere.
64 IcmpSquid::IcmpSquid() : Icmp()
69 IcmpSquid::~IcmpSquid()
77 IcmpSquid::SendEcho(Ip::Address
&to
, int opcode
, const char *payload
, int len
)
79 static pingerEchoData pecho
;
82 /** \li Does nothing if the pinger socket is not available. */
84 debugs(37, 2, HERE
<< " Socket Closed. Aborted send to " << pecho
.to
<< ", opcode " << opcode
<< ", len " << pecho
.psize
);
88 /** \li If no payload is given or is set as NULL it will ignore payload and len */
92 /** \li Otherwise if len is 0, uses strlen() to detect length of payload.
93 \bug This will result in part of the payload being truncated if it contains a NULL character.
94 \bug Or it may result in a buffer over-run if the payload is not nul-terminated properly.
96 else if (payload
&& len
== 0)
97 len
= strlen(payload
);
100 \bug If length specified or auto-detected is greater than the possible payload squid will die with an assert.
101 \todo This should perhapse be reduced to a truncated payload? or no payload. A WARNING is due anyway.
103 assert(len
<= PINGER_PAYLOAD_SZ
);
107 pecho
.opcode
= (unsigned char) opcode
;
112 memcpy(pecho
.payload
, payload
, len
);
114 slen
= sizeof(pingerEchoData
) - PINGER_PAYLOAD_SZ
+ pecho
.psize
;
116 debugs(37, 2, HERE
<< "to " << pecho
.to
<< ", opcode " << opcode
<< ", len " << pecho
.psize
);
118 x
= comm_udp_send(icmp_sock
, (char *)&pecho
, slen
, 0);
121 debugs(37, DBG_IMPORTANT
, HERE
<< "send: " << xstrerror());
123 /** \li If the send results in ECONNREFUSED or EPIPE errors from helper, will cleanly shutdown the module. */
124 /** \todo This should try restarting the helper a few times?? before giving up? */
125 if (errno
== ECONNREFUSED
|| errno
== EPIPE
) {
129 /** All other send errors are ignored. */
130 } else if (x
!= slen
) {
131 debugs(37, DBG_IMPORTANT
, HERE
<< "Wrote " << x
<< " of " << slen
<< " bytes");
135 // static Callback to wrap the squid-side ICMP handler.
136 // the IcmpSquid::Recv cannot be declared both static and virtual.
138 icmpSquidRecv(int unused1
, void *unused2
)
147 static int fail_count
= 0;
148 pingerReplyData preply
;
149 static Ip::Address F
;
151 Comm::SetSelect(icmp_sock
, COMM_SELECT_READ
, icmpSquidRecv
, NULL
, 0);
152 memset(&preply
, '\0', sizeof(pingerReplyData
));
153 n
= comm_udp_recv(icmp_sock
,
155 sizeof(pingerReplyData
),
158 if (n
< 0 && EAGAIN
!= errno
) {
159 debugs(37, DBG_IMPORTANT
, HERE
<< "recv: " << xstrerror());
161 if (errno
== ECONNREFUSED
)
164 if (errno
== ECONNRESET
)
167 if (++fail_count
== 10)
175 /** If its a test probe from the pinger. Do nothing. */
184 switch (preply
.opcode
) {
187 debugs(37,4, HERE
<< " ICMP_ECHO of " << preply
.from
<< " gave: hops=" << preply
.hops
<<", rtt=" << preply
.rtt
);
191 debugs(37,4, HERE
<< " DomainPing of " << preply
.from
<< " gave: hops=" << preply
.hops
<<", rtt=" << preply
.rtt
);
192 netdbHandlePingReply(F
, preply
.hops
, preply
.rtt
);
196 debugs(37, DBG_IMPORTANT
, HERE
<< "Bad opcode: " << preply
.opcode
<< " from " << F
);
201 #endif /* USE_ICMP */
204 IcmpSquid::DomainPing(Ip::Address
&to
, const char *domain
)
207 debugs(37, 4, HERE
<< "'" << domain
<< "' (" << to
<< ")");
208 SendEcho(to
, S_ICMP_DOM
, domain
, 0);
213 IcmpSquid::Open(void)
219 Ip::Address localhost
;
221 /* User configured disabled. */
222 if (!Config
.pinger
.enable
) {
227 args
[0] = "(pinger)";
229 localhost
.SetLocalhost();
232 * Do NOT use IPC_DGRAM (=IPC_UNIX_DGRAM) here because you can't
233 * send() more than 4096 bytes on a socketpair() socket (at
236 pid
= ipcCreate(IPC_UDP_SOCKET
,
237 Config
.pinger
.program
,
252 fd_note(icmp_sock
, "pinger");
254 Comm::SetSelect(icmp_sock
, COMM_SELECT_READ
, icmpSquidRecv
, NULL
, 0);
256 commUnsetFdTimeout(icmp_sock
);
258 debugs(37, DBG_IMPORTANT
, HERE
<< "Pinger socket opened on FD " << icmp_sock
);
260 /* Tests the pinger immediately using localhost */
262 SendEcho(localhost
, S_ICMP_ECHO
, "ip6-localhost");
263 if (localhost
.SetIPv4())
264 SendEcho(localhost
, S_ICMP_ECHO
, "localhost");
268 debugs(37, 4, HERE
<< "Pinger handle: 0x" << std::hex
<< hIpc
<< std::dec
<< ", PID: " << pid
);
270 #endif /* _SQUID_MSWIN_ */
274 #endif /* USE_ICMP */
278 IcmpSquid::Close(void)
285 debugs(37, DBG_IMPORTANT
, HERE
<< "Closing Pinger socket on FD " << icmp_sock
);
289 send(icmp_sock
, (const void *) "$shutdown\n", 10, 0);
293 comm_close(icmp_sock
);
298 if (WaitForSingleObject(hIpc
, 12000) != WAIT_OBJECT_0
) {
300 debugs(37, DBG_CRITICAL
, HERE
<< "WARNING: (pinger," << pid
<< ") didn't exit in 12 seconds");