]> git.ipfire.org Git - thirdparty/squid.git/blob - src/icmp/IcmpSquid.cc
898f34dc7e0be1146cd18e7213e5da35f15a45ef
[thirdparty/squid.git] / src / icmp / IcmpSquid.cc
1 /*
2 * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
3 *
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.
7 */
8
9 /* DEBUG: section 37 ICMP Routines */
10
11 #include "squid.h"
12 #include "comm.h"
13 #include "comm/Loops.h"
14 #include "defines.h"
15 #include "fd.h"
16 #include "icmp/IcmpConfig.h"
17 #include "icmp/IcmpSquid.h"
18 #include "icmp/net_db.h"
19 #include "ip/tools.h"
20 #include "SquidConfig.h"
21 #include "SquidIpc.h"
22 #include "SquidTime.h"
23
24 #include <cerrno>
25
26 // Instance global to be available in main() and elsewhere.
27 IcmpSquid icmpEngine;
28
29 #if USE_ICMP
30
31 #define S_ICMP_ECHO 1
32 #define S_ICMP_DOM 3
33
34 static void * hIpc;
35 static pid_t pid;
36
37 #endif /* USE_ICMP */
38
39 IcmpSquid::IcmpSquid() : Icmp()
40 {
41 ; // nothing new.
42 }
43
44 IcmpSquid::~IcmpSquid()
45 {
46 Close();
47 }
48
49 #if USE_ICMP
50
51 void
52 IcmpSquid::SendEcho(Ip::Address &to, int opcode, const char *payload, int len)
53 {
54 static pingerEchoData pecho;
55 int x, slen;
56
57 /** \li Does nothing if the pinger socket is not available. */
58 if (icmp_sock < 0) {
59 debugs(37, 2, HERE << " Socket Closed. Aborted send to " << pecho.to << ", opcode " << opcode << ", len " << pecho.psize);
60 return;
61 }
62
63 /** \li If no payload is given or is set as NULL it will ignore payload and len */
64 if (!payload)
65 len = 0;
66
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.
70 */
71 else if (payload && len == 0)
72 len = strlen(payload);
73
74 /** \li
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.
77 */
78 assert(len <= PINGER_PAYLOAD_SZ);
79
80 pecho.to = to;
81
82 pecho.opcode = (unsigned char) opcode;
83
84 pecho.psize = len;
85
86 if (len > 0)
87 memcpy(pecho.payload, payload, len);
88
89 slen = sizeof(pingerEchoData) - PINGER_PAYLOAD_SZ + pecho.psize;
90
91 debugs(37, 2, HERE << "to " << pecho.to << ", opcode " << opcode << ", len " << pecho.psize);
92
93 x = comm_udp_send(icmp_sock, (char *)&pecho, slen, 0);
94
95 if (x < 0) {
96 int xerrno = errno;
97 debugs(37, DBG_IMPORTANT, MYNAME << "send: " << xstrerr(xerrno));
98
99 /** \li If the send results in ECONNREFUSED or EPIPE errors from helper, will cleanly shutdown the module. */
100 /** \todo This should try restarting the helper a few times?? before giving up? */
101 if (xerrno == ECONNREFUSED || xerrno == EPIPE) {
102 Close();
103 return;
104 }
105 /** All other send errors are ignored. */
106 } else if (x != slen) {
107 debugs(37, DBG_IMPORTANT, HERE << "Wrote " << x << " of " << slen << " bytes");
108 }
109 }
110
111 // static Callback to wrap the squid-side ICMP handler.
112 // the IcmpSquid::Recv cannot be declared both static and virtual.
113 static void
114 icmpSquidRecv(int unused1, void *unused2)
115 {
116 icmpEngine.Recv();
117 }
118
119 void
120 IcmpSquid::Recv()
121 {
122 int n;
123 static int fail_count = 0;
124 pingerReplyData preply;
125 static Ip::Address F;
126
127 Comm::SetSelect(icmp_sock, COMM_SELECT_READ, icmpSquidRecv, NULL, 0);
128 memset(&preply, '\0', sizeof(pingerReplyData));
129 n = comm_udp_recv(icmp_sock,
130 (char *) &preply,
131 sizeof(pingerReplyData),
132 0);
133
134 if (n < 0 && EAGAIN != errno) {
135 int xerrno = errno;
136 debugs(37, DBG_IMPORTANT, MYNAME << "recv: " << xstrerr(xerrno));
137
138 if (xerrno == ECONNREFUSED)
139 Close();
140
141 if (xerrno == ECONNRESET)
142 Close();
143
144 if (++fail_count == 10)
145 Close();
146
147 return;
148 }
149
150 fail_count = 0;
151
152 /** If its a test probe from the pinger. Do nothing. */
153 if (n == 0) {
154 return;
155 }
156
157 F = preply.from;
158
159 F.port(0);
160
161 switch (preply.opcode) {
162
163 case S_ICMP_ECHO:
164 debugs(37,4, HERE << " ICMP_ECHO of " << preply.from << " gave: hops=" << preply.hops <<", rtt=" << preply.rtt);
165 break;
166
167 case S_ICMP_DOM:
168 debugs(37,4, HERE << " DomainPing of " << preply.from << " gave: hops=" << preply.hops <<", rtt=" << preply.rtt);
169 netdbHandlePingReply(F, preply.hops, preply.rtt);
170 break;
171
172 default:
173 debugs(37, DBG_IMPORTANT, HERE << "Bad opcode: " << preply.opcode << " from " << F);
174 break;
175 }
176 }
177
178 #endif /* USE_ICMP */
179
180 void
181 IcmpSquid::DomainPing(Ip::Address &to, const char *domain)
182 {
183 #if USE_ICMP
184 debugs(37, 4, HERE << "'" << domain << "' (" << to << ")");
185 SendEcho(to, S_ICMP_DOM, domain, 0);
186 #endif
187 }
188
189 int
190 IcmpSquid::Open(void)
191 {
192 #if USE_ICMP
193 const char *args[2];
194 int rfd;
195 int wfd;
196 Ip::Address localhost;
197
198 /* User configured disabled. */
199 if (!IcmpCfg.enable) {
200 Close();
201 return -1;
202 }
203
204 args[0] = "(pinger)";
205 args[1] = NULL;
206 localhost.setLocalhost();
207
208 /*
209 * Do NOT use IPC_DGRAM (=IPC_UNIX_DGRAM) here because you can't
210 * send() more than 4096 bytes on a socketpair() socket (at
211 * least on FreeBSD).
212 */
213 pid = ipcCreate(IPC_UDP_SOCKET,
214 IcmpCfg.program.c_str(),
215 args,
216 "Pinger Socket",
217 localhost,
218 &rfd,
219 &wfd,
220 &hIpc);
221
222 if (pid < 0)
223 return -1;
224
225 assert(rfd == wfd);
226
227 icmp_sock = rfd;
228
229 fd_note(icmp_sock, "pinger");
230
231 Comm::SetSelect(icmp_sock, COMM_SELECT_READ, icmpSquidRecv, NULL, 0);
232
233 commUnsetFdTimeout(icmp_sock);
234
235 debugs(37, DBG_IMPORTANT, HERE << "Pinger socket opened on FD " << icmp_sock);
236
237 /* Tests the pinger immediately using localhost */
238 if (Ip::EnableIpv6)
239 SendEcho(localhost, S_ICMP_ECHO, "ip6-localhost");
240 if (localhost.setIPv4())
241 SendEcho(localhost, S_ICMP_ECHO, "localhost");
242
243 #if _SQUID_WINDOWS_
244
245 debugs(37, 4, HERE << "Pinger handle: 0x" << std::hex << hIpc << std::dec << ", PID: " << pid);
246
247 #endif /* _SQUID_WINDOWS_ */
248 return icmp_sock;
249 #else /* USE_ICMP */
250 return -1;
251 #endif /* USE_ICMP */
252 }
253
254 void
255 IcmpSquid::Close(void)
256 {
257 #if USE_ICMP
258
259 if (icmp_sock < 0)
260 return;
261
262 debugs(37, DBG_IMPORTANT, HERE << "Closing Pinger socket on FD " << icmp_sock);
263
264 #if _SQUID_WINDOWS_
265
266 send(icmp_sock, (const void *) "$shutdown\n", 10, 0);
267
268 #endif
269
270 comm_close(icmp_sock);
271
272 #if _SQUID_WINDOWS_
273
274 if (hIpc) {
275 if (WaitForSingleObject(hIpc, 12000) != WAIT_OBJECT_0) {
276 getCurrentTime();
277 debugs(37, DBG_CRITICAL, HERE << "WARNING: (pinger," << pid << ") didn't exit in 12 seconds");
278 }
279
280 CloseHandle(hIpc);
281 }
282
283 #endif
284 icmp_sock = -1;
285
286 #endif
287 }
288