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