]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/icmp/IcmpSquid.cc
2 * Copyright (C) 1996-2023 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"
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, " 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 * XXX: This will result in part of the payload being truncated if it contains a NUL character.
68 * Or it may result in a buffer over-read if the payload is not NUL-terminated properly.
70 else if (payload
&& len
== 0)
71 len
= strlen(payload
);
73 // XXX: If length specified or auto-detected is greater than the possible payload squid will die with an assert.
74 // TODO: This should perhapse be reduced to a truncated payload? or no payload. A WARNING is due anyway.
75 assert(len
<= PINGER_PAYLOAD_SZ
);
79 pecho
.opcode
= (unsigned char) opcode
;
84 memcpy(pecho
.payload
, payload
, len
);
86 slen
= sizeof(pingerEchoData
) - PINGER_PAYLOAD_SZ
+ pecho
.psize
;
88 debugs(37, 2, "to " << pecho
.to
<< ", opcode " << opcode
<< ", len " << pecho
.psize
);
90 x
= comm_udp_send(icmp_sock
, (char *)&pecho
, slen
, 0);
94 debugs(37, DBG_IMPORTANT
, MYNAME
<< "send: " << xstrerr(xerrno
));
96 /** \li If the send results in ECONNREFUSED or EPIPE errors from helper, will cleanly shutdown the module. */
97 // TODO: try restarting the helper a few times before giving up?
98 if (xerrno
== ECONNREFUSED
|| xerrno
== EPIPE
) {
102 /** All other send errors are ignored. */
103 } else if (x
!= slen
) {
104 debugs(37, DBG_IMPORTANT
, "Wrote " << x
<< " of " << slen
<< " bytes");
108 // static Callback to wrap the squid-side ICMP handler.
109 // the IcmpSquid::Recv cannot be declared both static and virtual.
111 icmpSquidRecv(int, void *)
120 static int fail_count
= 0;
121 pingerReplyData preply
;
122 static Ip::Address F
;
124 Comm::SetSelect(icmp_sock
, COMM_SELECT_READ
, icmpSquidRecv
, nullptr, 0);
125 n
= comm_udp_recv(icmp_sock
,
127 sizeof(pingerReplyData
),
130 if (n
< 0 && EAGAIN
!= errno
) {
132 debugs(37, DBG_IMPORTANT
, MYNAME
<< "recv: " << xstrerr(xerrno
));
134 if (xerrno
== ECONNREFUSED
)
137 if (xerrno
== ECONNRESET
)
140 if (++fail_count
== 10)
148 /** If its a test probe from the pinger. Do nothing. */
157 switch (preply
.opcode
) {
160 debugs(37,4, " ICMP_ECHO of " << preply
.from
<< " gave: hops=" << preply
.hops
<<", rtt=" << preply
.rtt
);
164 debugs(37,4, " DomainPing of " << preply
.from
<< " gave: hops=" << preply
.hops
<<", rtt=" << preply
.rtt
);
165 netdbHandlePingReply(F
, preply
.hops
, preply
.rtt
);
169 debugs(37, DBG_IMPORTANT
, "ERROR: Bad opcode: " << preply
.opcode
<< " from " << F
);
174 #endif /* USE_ICMP */
177 IcmpSquid::DomainPing(Ip::Address
&to
, const char *domain
)
180 debugs(37, 4, "'" << domain
<< "' (" << to
<< ")");
181 SendEcho(to
, S_ICMP_DOM
, domain
, 0);
189 IcmpSquid::Open(void)
195 Ip::Address localhost
;
197 /* User configured disabled. */
198 if (!IcmpCfg
.enable
) {
203 args
[0] = "(pinger)";
205 localhost
.setLocalhost();
208 * Do NOT use IPC_DGRAM (=IPC_UNIX_DGRAM) here because you can't
209 * send() more than 4096 bytes on a socketpair() socket (at
212 pid
= ipcCreate(IPC_UDP_SOCKET
,
213 IcmpCfg
.program
.c_str(),
228 fd_note(icmp_sock
, "pinger");
230 Comm::SetSelect(icmp_sock
, COMM_SELECT_READ
, icmpSquidRecv
, nullptr, 0);
232 commUnsetFdTimeout(icmp_sock
);
234 debugs(37, DBG_IMPORTANT
, "Pinger socket opened on FD " << icmp_sock
);
236 /* Tests the pinger immediately using localhost */
238 SendEcho(localhost
, S_ICMP_ECHO
, "ip6-localhost");
239 if (localhost
.setIPv4())
240 SendEcho(localhost
, S_ICMP_ECHO
, "localhost");
244 debugs(37, 4, "Pinger handle: 0x" << std::hex
<< hIpc
<< std::dec
<< ", PID: " << pid
);
246 #endif /* _SQUID_WINDOWS_ */
250 #endif /* USE_ICMP */
254 IcmpSquid::Close(void)
261 debugs(37, DBG_IMPORTANT
, "Closing Pinger socket on FD " << icmp_sock
);
265 send(icmp_sock
, (const void *) "$shutdown\n", 10, 0);
269 comm_close(icmp_sock
);
274 if (WaitForSingleObject(hIpc
, 12000) != WAIT_OBJECT_0
) {
276 debugs(37, DBG_CRITICAL
, "WARNING: (pinger," << pid
<< ") didn't exit in 12 seconds");