]> git.ipfire.org Git - thirdparty/squid.git/blob - src/icmp/IcmpSquid.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / icmp / IcmpSquid.cc
1 /*
2 * $Id$
3 *
4 * DEBUG: section 37 ICMP Routines
5 * AUTHOR: Duane Wessels, Amos Jeffries
6 *
7 * SQUID Web Proxy Cache http://www.squid-cache.org/
8 * ----------------------------------------------------------
9 *
10 * Squid is the result of efforts by numerous individuals from
11 * the Internet community; see the CONTRIBUTORS file for full
12 * details. Many organizations have provided support for Squid's
13 * development; see the SPONSORS file for full details. Squid is
14 * Copyrighted (C) 2001 by the Regents of the University of
15 * California; see the COPYRIGHT file for full details. Squid
16 * incorporates software developed and/or copyrighted by other
17 * sources; see the CREDITS file for full details.
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
32 *
33 */
34
35 #include "squid.h"
36 #include "comm.h"
37 #include "comm/Loops.h"
38 #include "icmp/IcmpSquid.h"
39 #include "icmp/net_db.h"
40 #include "ip/tools.h"
41 #include "protos.h"
42 #include "SquidTime.h"
43
44 #if HAVE_ERRNO_H
45 #include <errno.h>
46 #endif
47
48 // Instance global to be available in main() and elsewhere.
49 IcmpSquid icmpEngine;
50
51 #if USE_ICMP
52
53 #define S_ICMP_ECHO 1
54 #if DEAD_CODE
55 #define S_ICMP_ICP 2
56 #endif
57 #define S_ICMP_DOM 3
58
59 static void * hIpc;
60 static pid_t pid;
61
62 #endif /* USE_ICMP */
63
64 IcmpSquid::IcmpSquid() : Icmp()
65 {
66 ; // nothing new.
67 }
68
69 IcmpSquid::~IcmpSquid()
70 {
71 Close();
72 }
73
74 #if USE_ICMP
75
76 void
77 IcmpSquid::SendEcho(Ip::Address &to, int opcode, const char *payload, int len)
78 {
79 static pingerEchoData pecho;
80 int x, slen;
81
82 /** \li Does nothing if the pinger socket is not available. */
83 if (icmp_sock < 0) {
84 debugs(37, 2, HERE << " Socket Closed. Aborted send to " << pecho.to << ", opcode " << opcode << ", len " << pecho.psize);
85 return;
86 }
87
88 /** \li If no payload is given or is set as NULL it will ignore payload and len */
89 if (!payload)
90 len = 0;
91
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.
95 */
96 else if (payload && len == 0)
97 len = strlen(payload);
98
99 /** \li
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.
102 */
103 assert(len <= PINGER_PAYLOAD_SZ);
104
105 pecho.to = to;
106
107 pecho.opcode = (unsigned char) opcode;
108
109 pecho.psize = len;
110
111 if (len > 0)
112 memcpy(pecho.payload, payload, len);
113
114 slen = sizeof(pingerEchoData) - PINGER_PAYLOAD_SZ + pecho.psize;
115
116 debugs(37, 2, HERE << "to " << pecho.to << ", opcode " << opcode << ", len " << pecho.psize);
117
118 x = comm_udp_send(icmp_sock, (char *)&pecho, slen, 0);
119
120 if (x < 0) {
121 debugs(37, DBG_IMPORTANT, HERE << "send: " << xstrerror());
122
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) {
126 Close();
127 return;
128 }
129 /** All other send errors are ignored. */
130 } else if (x != slen) {
131 debugs(37, DBG_IMPORTANT, HERE << "Wrote " << x << " of " << slen << " bytes");
132 }
133 }
134
135 // static Callback to wrap the squid-side ICMP handler.
136 // the IcmpSquid::Recv cannot be declared both static and virtual.
137 static void
138 icmpSquidRecv(int unused1, void *unused2)
139 {
140 icmpEngine.Recv();
141 }
142
143 void
144 IcmpSquid::Recv()
145 {
146 int n;
147 static int fail_count = 0;
148 pingerReplyData preply;
149 static Ip::Address F;
150
151 Comm::SetSelect(icmp_sock, COMM_SELECT_READ, icmpSquidRecv, NULL, 0);
152 memset(&preply, '\0', sizeof(pingerReplyData));
153 n = comm_udp_recv(icmp_sock,
154 (char *) &preply,
155 sizeof(pingerReplyData),
156 0);
157
158 if (n < 0 && EAGAIN != errno) {
159 debugs(37, DBG_IMPORTANT, HERE << "recv: " << xstrerror());
160
161 if (errno == ECONNREFUSED)
162 Close();
163
164 if (errno == ECONNRESET)
165 Close();
166
167 if (++fail_count == 10)
168 Close();
169
170 return;
171 }
172
173 fail_count = 0;
174
175 /** If its a test probe from the pinger. Do nothing. */
176 if (n == 0) {
177 return;
178 }
179
180 F = preply.from;
181
182 F.SetPort(0);
183
184 switch (preply.opcode) {
185
186 case S_ICMP_ECHO:
187 debugs(37,4, HERE << " ICMP_ECHO of " << preply.from << " gave: hops=" << preply.hops <<", rtt=" << preply.rtt);
188 break;
189
190 case S_ICMP_DOM:
191 debugs(37,4, HERE << " DomainPing of " << preply.from << " gave: hops=" << preply.hops <<", rtt=" << preply.rtt);
192 netdbHandlePingReply(F, preply.hops, preply.rtt);
193 break;
194
195 default:
196 debugs(37, DBG_IMPORTANT, HERE << "Bad opcode: " << preply.opcode << " from " << F);
197 break;
198 }
199 }
200
201 #endif /* USE_ICMP */
202
203 void
204 IcmpSquid::DomainPing(Ip::Address &to, const char *domain)
205 {
206 #if USE_ICMP
207 debugs(37, 4, HERE << "'" << domain << "' (" << to << ")");
208 SendEcho(to, S_ICMP_DOM, domain, 0);
209 #endif
210 }
211
212 int
213 IcmpSquid::Open(void)
214 {
215 #if USE_ICMP
216 const char *args[2];
217 int rfd;
218 int wfd;
219 Ip::Address localhost;
220
221 /* User configured disabled. */
222 if (!Config.pinger.enable) {
223 Close();
224 return -1;
225 }
226
227 args[0] = "(pinger)";
228 args[1] = NULL;
229 localhost.SetLocalhost();
230
231 /*
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
234 * least on FreeBSD).
235 */
236 pid = ipcCreate(IPC_UDP_SOCKET,
237 Config.pinger.program,
238 args,
239 "Pinger Socket",
240 localhost,
241 &rfd,
242 &wfd,
243 &hIpc);
244
245 if (pid < 0)
246 return -1;
247
248 assert(rfd == wfd);
249
250 icmp_sock = rfd;
251
252 fd_note(icmp_sock, "pinger");
253
254 Comm::SetSelect(icmp_sock, COMM_SELECT_READ, icmpSquidRecv, NULL, 0);
255
256 commUnsetFdTimeout(icmp_sock);
257
258 debugs(37, DBG_IMPORTANT, HERE << "Pinger socket opened on FD " << icmp_sock);
259
260 /* Tests the pinger immediately using localhost */
261 if (Ip::EnableIpv6)
262 SendEcho(localhost, S_ICMP_ECHO, "ip6-localhost");
263 if (localhost.SetIPv4())
264 SendEcho(localhost, S_ICMP_ECHO, "localhost");
265
266 #if _SQUID_MSWIN_
267
268 debugs(37, 4, HERE << "Pinger handle: 0x" << std::hex << hIpc << std::dec << ", PID: " << pid);
269
270 #endif /* _SQUID_MSWIN_ */
271 return icmp_sock;
272 #else /* USE_ICMP */
273 return -1;
274 #endif /* USE_ICMP */
275 }
276
277 void
278 IcmpSquid::Close(void)
279 {
280 #if USE_ICMP
281
282 if (icmp_sock < 0)
283 return;
284
285 debugs(37, DBG_IMPORTANT, HERE << "Closing Pinger socket on FD " << icmp_sock);
286
287 #if _SQUID_MSWIN_
288
289 send(icmp_sock, (const void *) "$shutdown\n", 10, 0);
290
291 #endif
292
293 comm_close(icmp_sock);
294
295 #if _SQUID_MSWIN_
296
297 if (hIpc) {
298 if (WaitForSingleObject(hIpc, 12000) != WAIT_OBJECT_0) {
299 getCurrentTime();
300 debugs(37, DBG_CRITICAL, HERE << "WARNING: (pinger," << pid << ") didn't exit in 12 seconds");
301 }
302
303 CloseHandle(hIpc);
304 }
305
306 #endif
307 icmp_sock = -1;
308
309 #endif
310 }