]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/icmp/IcmpSquid.cc
2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
9 /* DEBUG: section 37 ICMP Routines */
13 #include "comm/Loops.h"
16 #include "icmp/IcmpConfig.h"
17 #include "icmp/IcmpSquid.h"
18 #include "icmp/net_db.h"
20 #include "SquidConfig.h"
22 #include "SquidTime.h"
26 // Instance global to be available in main() and elsewhere.
39 IcmpSquid::IcmpSquid() : Icmp()
44 IcmpSquid::~IcmpSquid()
52 IcmpSquid::SendEcho(Ip::Address
&to
, int opcode
, const char *payload
, int len
)
54 static pingerEchoData pecho
;
57 /** \li Does nothing if the pinger socket is not available. */
59 debugs(37, 2, HERE
<< " Socket Closed. Aborted send to " << pecho
.to
<< ", opcode " << opcode
<< ", len " << pecho
.psize
);
63 /** \li If no payload is given or is set as NULL it will ignore payload and len */
67 /** \li Otherwise if len is 0, uses strlen() to detect length of payload.
68 \bug This will result in part of the payload being truncated if it contains a NULL character.
69 \bug Or it may result in a buffer over-run if the payload is not nul-terminated properly.
71 else if (payload
&& len
== 0)
72 len
= strlen(payload
);
75 \bug If length specified or auto-detected is greater than the possible payload squid will die with an assert.
76 \todo This should perhapse be reduced to a truncated payload? or no payload. A WARNING is due anyway.
78 assert(len
<= PINGER_PAYLOAD_SZ
);
82 pecho
.opcode
= (unsigned char) opcode
;
87 memcpy(pecho
.payload
, payload
, len
);
89 slen
= sizeof(pingerEchoData
) - PINGER_PAYLOAD_SZ
+ pecho
.psize
;
91 debugs(37, 2, HERE
<< "to " << pecho
.to
<< ", opcode " << opcode
<< ", len " << pecho
.psize
);
93 x
= comm_udp_send(icmp_sock
, (char *)&pecho
, slen
, 0);
96 debugs(37, DBG_IMPORTANT
, HERE
<< "send: " << xstrerror());
98 /** \li If the send results in ECONNREFUSED or EPIPE errors from helper, will cleanly shutdown the module. */
99 /** \todo This should try restarting the helper a few times?? before giving up? */
100 if (errno
== ECONNREFUSED
|| errno
== EPIPE
) {
104 /** All other send errors are ignored. */
105 } else if (x
!= slen
) {
106 debugs(37, DBG_IMPORTANT
, HERE
<< "Wrote " << x
<< " of " << slen
<< " bytes");
110 // static Callback to wrap the squid-side ICMP handler.
111 // the IcmpSquid::Recv cannot be declared both static and virtual.
113 icmpSquidRecv(int unused1
, void *unused2
)
122 static int fail_count
= 0;
123 pingerReplyData preply
;
124 static Ip::Address F
;
126 Comm::SetSelect(icmp_sock
, COMM_SELECT_READ
, icmpSquidRecv
, NULL
, 0);
127 memset(&preply
, '\0', sizeof(pingerReplyData
));
128 n
= comm_udp_recv(icmp_sock
,
130 sizeof(pingerReplyData
),
133 if (n
< 0 && EAGAIN
!= errno
) {
134 debugs(37, DBG_IMPORTANT
, HERE
<< "recv: " << xstrerror());
136 if (errno
== ECONNREFUSED
)
139 if (errno
== ECONNRESET
)
142 if (++fail_count
== 10)
150 /** If its a test probe from the pinger. Do nothing. */
159 switch (preply
.opcode
) {
162 debugs(37,4, HERE
<< " ICMP_ECHO of " << preply
.from
<< " gave: hops=" << preply
.hops
<<", rtt=" << preply
.rtt
);
166 debugs(37,4, HERE
<< " DomainPing of " << preply
.from
<< " gave: hops=" << preply
.hops
<<", rtt=" << preply
.rtt
);
167 netdbHandlePingReply(F
, preply
.hops
, preply
.rtt
);
171 debugs(37, DBG_IMPORTANT
, HERE
<< "Bad opcode: " << preply
.opcode
<< " from " << F
);
176 #endif /* USE_ICMP */
179 IcmpSquid::DomainPing(Ip::Address
&to
, const char *domain
)
182 debugs(37, 4, HERE
<< "'" << domain
<< "' (" << to
<< ")");
183 SendEcho(to
, S_ICMP_DOM
, domain
, 0);
188 IcmpSquid::Open(void)
194 Ip::Address localhost
;
196 /* User configured disabled. */
197 if (!IcmpCfg
.enable
) {
202 args
[0] = "(pinger)";
204 localhost
.setLocalhost();
207 * Do NOT use IPC_DGRAM (=IPC_UNIX_DGRAM) here because you can't
208 * send() more than 4096 bytes on a socketpair() socket (at
211 pid
= ipcCreate(IPC_UDP_SOCKET
,
212 IcmpCfg
.program
.c_str(),
227 fd_note(icmp_sock
, "pinger");
229 Comm::SetSelect(icmp_sock
, COMM_SELECT_READ
, icmpSquidRecv
, NULL
, 0);
231 commUnsetFdTimeout(icmp_sock
);
233 debugs(37, DBG_IMPORTANT
, HERE
<< "Pinger socket opened on FD " << icmp_sock
);
235 /* Tests the pinger immediately using localhost */
237 SendEcho(localhost
, S_ICMP_ECHO
, "ip6-localhost");
238 if (localhost
.setIPv4())
239 SendEcho(localhost
, S_ICMP_ECHO
, "localhost");
243 debugs(37, 4, HERE
<< "Pinger handle: 0x" << std::hex
<< hIpc
<< std::dec
<< ", PID: " << pid
);
245 #endif /* _SQUID_WINDOWS_ */
249 #endif /* USE_ICMP */
253 IcmpSquid::Close(void)
260 debugs(37, DBG_IMPORTANT
, HERE
<< "Closing Pinger socket on FD " << icmp_sock
);
264 send(icmp_sock
, (const void *) "$shutdown\n", 10, 0);
268 comm_close(icmp_sock
);
273 if (WaitForSingleObject(hIpc
, 12000) != WAIT_OBJECT_0
) {
275 debugs(37, DBG_CRITICAL
, HERE
<< "WARNING: (pinger," << pid
<< ") didn't exit in 12 seconds");