]>
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/IcmpSquid.h"
17 #include "icmp/net_db.h"
19 #include "SquidConfig.h"
21 #include "SquidTime.h"
25 // Instance global to be available in main() and elsewhere.
38 IcmpSquid::IcmpSquid() : Icmp()
43 IcmpSquid::~IcmpSquid()
51 IcmpSquid::SendEcho(Ip::Address
&to
, int opcode
, const char *payload
, int len
)
53 static pingerEchoData pecho
;
56 /** \li Does nothing if the pinger socket is not available. */
58 debugs(37, 2, HERE
<< " Socket Closed. Aborted send to " << pecho
.to
<< ", opcode " << opcode
<< ", len " << pecho
.psize
);
62 /** \li If no payload is given or is set as NULL it will ignore payload and len */
66 /** \li Otherwise if len is 0, uses strlen() to detect length of payload.
67 \bug This will result in part of the payload being truncated if it contains a NULL character.
68 \bug Or it may result in a buffer over-run if the payload is not nul-terminated properly.
70 else if (payload
&& len
== 0)
71 len
= strlen(payload
);
74 \bug If length specified or auto-detected is greater than the possible payload squid will die with an assert.
75 \todo This should perhapse be reduced to a truncated payload? or no payload. A WARNING is due anyway.
77 assert(len
<= PINGER_PAYLOAD_SZ
);
81 pecho
.opcode
= (unsigned char) opcode
;
86 memcpy(pecho
.payload
, payload
, len
);
88 slen
= sizeof(pingerEchoData
) - PINGER_PAYLOAD_SZ
+ pecho
.psize
;
90 debugs(37, 2, HERE
<< "to " << pecho
.to
<< ", opcode " << opcode
<< ", len " << pecho
.psize
);
92 x
= comm_udp_send(icmp_sock
, (char *)&pecho
, slen
, 0);
95 debugs(37, DBG_IMPORTANT
, HERE
<< "send: " << xstrerror());
97 /** \li If the send results in ECONNREFUSED or EPIPE errors from helper, will cleanly shutdown the module. */
98 /** \todo This should try restarting the helper a few times?? before giving up? */
99 if (errno
== ECONNREFUSED
|| errno
== EPIPE
) {
103 /** All other send errors are ignored. */
104 } else if (x
!= slen
) {
105 debugs(37, DBG_IMPORTANT
, HERE
<< "Wrote " << x
<< " of " << slen
<< " bytes");
109 // static Callback to wrap the squid-side ICMP handler.
110 // the IcmpSquid::Recv cannot be declared both static and virtual.
112 icmpSquidRecv(int unused1
, void *unused2
)
121 static int fail_count
= 0;
122 pingerReplyData preply
;
123 static Ip::Address F
;
125 Comm::SetSelect(icmp_sock
, COMM_SELECT_READ
, icmpSquidRecv
, NULL
, 0);
126 memset(&preply
, '\0', sizeof(pingerReplyData
));
127 n
= comm_udp_recv(icmp_sock
,
129 sizeof(pingerReplyData
),
132 if (n
< 0 && EAGAIN
!= errno
) {
133 debugs(37, DBG_IMPORTANT
, HERE
<< "recv: " << xstrerror());
135 if (errno
== ECONNREFUSED
)
138 if (errno
== ECONNRESET
)
141 if (++fail_count
== 10)
149 /** If its a test probe from the pinger. Do nothing. */
158 switch (preply
.opcode
) {
161 debugs(37,4, HERE
<< " ICMP_ECHO of " << preply
.from
<< " gave: hops=" << preply
.hops
<<", rtt=" << preply
.rtt
);
165 debugs(37,4, HERE
<< " DomainPing of " << preply
.from
<< " gave: hops=" << preply
.hops
<<", rtt=" << preply
.rtt
);
166 netdbHandlePingReply(F
, preply
.hops
, preply
.rtt
);
170 debugs(37, DBG_IMPORTANT
, HERE
<< "Bad opcode: " << preply
.opcode
<< " from " << F
);
175 #endif /* USE_ICMP */
178 IcmpSquid::DomainPing(Ip::Address
&to
, const char *domain
)
181 debugs(37, 4, HERE
<< "'" << domain
<< "' (" << to
<< ")");
182 SendEcho(to
, S_ICMP_DOM
, domain
, 0);
187 IcmpSquid::Open(void)
193 Ip::Address localhost
;
195 /* User configured disabled. */
196 if (!Config
.pinger
.enable
) {
201 args
[0] = "(pinger)";
203 localhost
.setLocalhost();
206 * Do NOT use IPC_DGRAM (=IPC_UNIX_DGRAM) here because you can't
207 * send() more than 4096 bytes on a socketpair() socket (at
210 pid
= ipcCreate(IPC_UDP_SOCKET
,
211 Config
.pinger
.program
,
226 fd_note(icmp_sock
, "pinger");
228 Comm::SetSelect(icmp_sock
, COMM_SELECT_READ
, icmpSquidRecv
, NULL
, 0);
230 commUnsetFdTimeout(icmp_sock
);
232 debugs(37, DBG_IMPORTANT
, HERE
<< "Pinger socket opened on FD " << icmp_sock
);
234 /* Tests the pinger immediately using localhost */
236 SendEcho(localhost
, S_ICMP_ECHO
, "ip6-localhost");
237 if (localhost
.setIPv4())
238 SendEcho(localhost
, S_ICMP_ECHO
, "localhost");
242 debugs(37, 4, HERE
<< "Pinger handle: 0x" << std::hex
<< hIpc
<< std::dec
<< ", PID: " << pid
);
244 #endif /* _SQUID_WINDOWS_ */
248 #endif /* USE_ICMP */
252 IcmpSquid::Close(void)
259 debugs(37, DBG_IMPORTANT
, HERE
<< "Closing Pinger socket on FD " << icmp_sock
);
263 send(icmp_sock
, (const void *) "$shutdown\n", 10, 0);
267 comm_close(icmp_sock
);
272 if (WaitForSingleObject(hIpc
, 12000) != WAIT_OBJECT_0
) {
274 debugs(37, DBG_CRITICAL
, HERE
<< "WARNING: (pinger," << pid
<< ") didn't exit in 12 seconds");