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