]> git.ipfire.org Git - thirdparty/squid.git/blob - src/icmp/IcmpSquid.cc
Merged from trunk
[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
65 IcmpSquid::IcmpSquid() : Icmp()
66 {
67 ; // nothing new.
68 }
69
70 IcmpSquid::~IcmpSquid()
71 {
72 Close();
73 }
74
75
76 #if USE_ICMP
77
78 void
79 IcmpSquid::SendEcho(Ip::Address &to, int opcode, const char *payload, int len)
80 {
81 static pingerEchoData pecho;
82 int x, slen;
83
84 /** \li Does nothing if the pinger socket is not available. */
85 if (icmp_sock < 0) {
86 debugs(37, 2, HERE << " Socket Closed. Aborted send to " << pecho.to << ", opcode " << opcode << ", len " << pecho.psize);
87 return;
88 }
89
90 /** \li If no payload is given or is set as NULL it will ignore payload and len */
91 if (!payload)
92 len = 0;
93
94 /** \li Otherwise if len is 0, uses strlen() to detect length of payload.
95 \bug This will result in part of the payload being truncated if it contains a NULL character.
96 \bug Or it may result in a buffer over-run if the payload is not nul-terminated properly.
97 */
98 else if (payload && len == 0)
99 len = strlen(payload);
100
101 /** \li
102 \bug If length specified or auto-detected is greater than the possible payload squid will die with an assert.
103 \todo This should perhapse be reduced to a truncated payload? or no payload. A WARNING is due anyway.
104 */
105 assert(len <= PINGER_PAYLOAD_SZ);
106
107 pecho.to = to;
108
109 pecho.opcode = (unsigned char) opcode;
110
111 pecho.psize = len;
112
113 if (len > 0)
114 memcpy(pecho.payload, payload, len);
115
116 slen = sizeof(pingerEchoData) - PINGER_PAYLOAD_SZ + pecho.psize;
117
118 debugs(37, 2, HERE << "to " << pecho.to << ", opcode " << opcode << ", len " << pecho.psize);
119
120 x = comm_udp_send(icmp_sock, (char *)&pecho, slen, 0);
121
122 if (x < 0) {
123 debugs(37, DBG_IMPORTANT, HERE << "send: " << xstrerror());
124
125 /** \li If the send results in ECONNREFUSED or EPIPE errors from helper, will cleanly shutdown the module. */
126 /** \todo This should try restarting the helper a few times?? before giving up? */
127 if (errno == ECONNREFUSED || errno == EPIPE) {
128 Close();
129 return;
130 }
131 /** All other send errors are ignored. */
132 } else if (x != slen) {
133 debugs(37, DBG_IMPORTANT, HERE << "Wrote " << x << " of " << slen << " bytes");
134 }
135 }
136
137 // static Callback to wrap the squid-side ICMP handler.
138 // the IcmpSquid::Recv cannot be declared both static and virtual.
139 static void
140 icmpSquidRecv(int unused1, void *unused2)
141 {
142 icmpEngine.Recv();
143 }
144
145 void
146 IcmpSquid::Recv()
147 {
148 int n;
149 static int fail_count = 0;
150 pingerReplyData preply;
151 static Ip::Address F;
152
153 Comm::SetSelect(icmp_sock, COMM_SELECT_READ, icmpSquidRecv, NULL, 0);
154 memset(&preply, '\0', sizeof(pingerReplyData));
155 n = comm_udp_recv(icmp_sock,
156 (char *) &preply,
157 sizeof(pingerReplyData),
158 0);
159
160 if (n < 0 && EAGAIN != errno) {
161 debugs(37, DBG_IMPORTANT, HERE << "recv: " << xstrerror());
162
163 if (errno == ECONNREFUSED)
164 Close();
165
166 if (errno == ECONNRESET)
167 Close();
168
169 if (++fail_count == 10)
170 Close();
171
172 return;
173 }
174
175 fail_count = 0;
176
177 /** If its a test probe from the pinger. Do nothing. */
178 if (n == 0) {
179 return;
180 }
181
182 F = preply.from;
183
184 F.SetPort(0);
185
186 switch (preply.opcode) {
187
188 case S_ICMP_ECHO:
189 debugs(37,4, HERE << " ICMP_ECHO of " << preply.from << " gave: hops=" << preply.hops <<", rtt=" << preply.rtt);
190 break;
191
192 case S_ICMP_DOM:
193 debugs(37,4, HERE << " DomainPing of " << preply.from << " gave: hops=" << preply.hops <<", rtt=" << preply.rtt);
194 netdbHandlePingReply(F, preply.hops, preply.rtt);
195 break;
196
197 default:
198 debugs(37, DBG_IMPORTANT, HERE << "Bad opcode: " << preply.opcode << " from " << F);
199 break;
200 }
201 }
202
203 #endif /* USE_ICMP */
204
205 void
206 IcmpSquid::DomainPing(Ip::Address &to, const char *domain)
207 {
208 #if USE_ICMP
209 debugs(37, 4, HERE << "'" << domain << "' (" << to << ")");
210 SendEcho(to, S_ICMP_DOM, domain, 0);
211 #endif
212 }
213
214 int
215 IcmpSquid::Open(void)
216 {
217 #if USE_ICMP
218 const char *args[2];
219 int rfd;
220 int wfd;
221 Ip::Address localhost;
222
223 /* User configured disabled. */
224 if (!Config.pinger.enable) {
225 Close();
226 return -1;
227 }
228
229 args[0] = "(pinger)";
230 args[1] = NULL;
231 localhost.SetLocalhost();
232
233 /*
234 * Do NOT use IPC_DGRAM (=IPC_UNIX_DGRAM) here because you can't
235 * send() more than 4096 bytes on a socketpair() socket (at
236 * least on FreeBSD).
237 */
238 pid = ipcCreate(IPC_UDP_SOCKET,
239 Config.pinger.program,
240 args,
241 "Pinger Socket",
242 localhost,
243 &rfd,
244 &wfd,
245 &hIpc);
246
247 if (pid < 0)
248 return -1;
249
250 assert(rfd == wfd);
251
252 icmp_sock = rfd;
253
254 fd_note(icmp_sock, "pinger");
255
256 Comm::SetSelect(icmp_sock, COMM_SELECT_READ, icmpSquidRecv, NULL, 0);
257
258 commUnsetFdTimeout(icmp_sock);
259
260 debugs(37, DBG_IMPORTANT, HERE << "Pinger socket opened on FD " << icmp_sock);
261
262 /* Tests the pinger immediately using localhost */
263 if (Ip::EnableIpv6)
264 SendEcho(localhost, S_ICMP_ECHO, "ip6-localhost");
265 if (localhost.SetIPv4())
266 SendEcho(localhost, S_ICMP_ECHO, "localhost");
267
268 #if _SQUID_MSWIN_
269
270 debugs(37, 4, HERE << "Pinger handle: 0x" << std::hex << hIpc << std::dec << ", PID: " << pid);
271
272 #endif /* _SQUID_MSWIN_ */
273 return icmp_sock;
274 #else /* USE_ICMP */
275 return -1;
276 #endif /* USE_ICMP */
277 }
278
279 void
280 IcmpSquid::Close(void)
281 {
282 #if USE_ICMP
283
284 if (icmp_sock < 0)
285 return;
286
287 debugs(37, DBG_IMPORTANT, HERE << "Closing Pinger socket on FD " << icmp_sock);
288
289 #if _SQUID_MSWIN_
290
291 send(icmp_sock, (const void *) "$shutdown\n", 10, 0);
292
293 #endif
294
295 comm_close(icmp_sock);
296
297 #if _SQUID_MSWIN_
298
299 if (hIpc) {
300 if (WaitForSingleObject(hIpc, 12000) != WAIT_OBJECT_0) {
301 getCurrentTime();
302 debugs(37, DBG_CRITICAL, HERE << "WARNING: (pinger," << pid << ") didn't exit in 12 seconds");
303 }
304
305 CloseHandle(hIpc);
306 }
307
308 #endif
309 icmp_sock = -1;
310
311 #endif
312 }