]>
Commit | Line | Data |
---|---|---|
cc192b50 | 1 | /* |
262a0e14 | 2 | * $Id$ |
cc192b50 | 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. | |
26ac0430 | 23 | * |
cc192b50 | 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. | |
26ac0430 | 28 | * |
cc192b50 | 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 | ||
582c2af2 | 35 | #include "squid.h" |
d841c88d AJ |
36 | #include "comm.h" |
37 | #include "comm/Loops.h" | |
9b5c4a9a AJ |
38 | #include "icmp/IcmpSquid.h" |
39 | #include "icmp/net_db.h" | |
055421ee | 40 | #include "ip/tools.h" |
582c2af2 | 41 | #include "protos.h" |
cc192b50 | 42 | #include "SquidTime.h" |
43 | ||
21d845b1 FC |
44 | #if HAVE_ERRNO_H |
45 | #include <errno.h> | |
46 | #endif | |
47 | ||
cc192b50 | 48 | // Instance global to be available in main() and elsewhere. |
b826ffb5 | 49 | IcmpSquid icmpEngine; |
cc192b50 | 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 | ||
b826ffb5 | 64 | IcmpSquid::IcmpSquid() : Icmp() |
cc192b50 | 65 | { |
66 | ; // nothing new. | |
67 | } | |
68 | ||
b826ffb5 | 69 | IcmpSquid::~IcmpSquid() |
cc192b50 | 70 | { |
71 | Close(); | |
72 | } | |
73 | ||
cc192b50 | 74 | #if USE_ICMP |
75 | ||
76 | void | |
b7ac5457 | 77 | IcmpSquid::SendEcho(Ip::Address &to, int opcode, const char *payload, int len) |
cc192b50 | 78 | { |
79 | static pingerEchoData pecho; | |
80 | int x, slen; | |
81 | ||
82 | /** \li Does nothing if the pinger socket is not available. */ | |
26ac0430 | 83 | if (icmp_sock < 0) { |
cc192b50 | 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 */ | |
26ac0430 | 89 | if (!payload) |
cc192b50 | 90 | len = 0; |
26ac0430 | 91 | |
cc192b50 | 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 | ||
26ac0430 | 111 | if (len > 0) |
41d00cd3 | 112 | memcpy(pecho.payload, payload, len); |
cc192b50 | 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) { | |
e0236918 | 121 | debugs(37, DBG_IMPORTANT, HERE << "send: " << xstrerror()); |
cc192b50 | 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. */ | |
9166e142 | 130 | } else if (x != slen) { |
e0236918 | 131 | debugs(37, DBG_IMPORTANT, HERE << "Wrote " << x << " of " << slen << " bytes"); |
cc192b50 | 132 | } |
133 | } | |
134 | ||
135 | // static Callback to wrap the squid-side ICMP handler. | |
b826ffb5 | 136 | // the IcmpSquid::Recv cannot be declared both static and virtual. |
cc192b50 | 137 | static void |
138 | icmpSquidRecv(int unused1, void *unused2) | |
139 | { | |
140 | icmpEngine.Recv(); | |
141 | } | |
142 | ||
143 | void | |
b826ffb5 | 144 | IcmpSquid::Recv() |
cc192b50 | 145 | { |
146 | int n; | |
147 | static int fail_count = 0; | |
148 | pingerReplyData preply; | |
b7ac5457 | 149 | static Ip::Address F; |
cc192b50 | 150 | |
d841c88d | 151 | Comm::SetSelect(icmp_sock, COMM_SELECT_READ, icmpSquidRecv, NULL, 0); |
cc192b50 | 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) { | |
e0236918 | 159 | debugs(37, DBG_IMPORTANT, HERE << "recv: " << xstrerror()); |
cc192b50 | 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: | |
e0236918 | 196 | debugs(37, DBG_IMPORTANT, HERE << "Bad opcode: " << preply.opcode << " from " << F); |
cc192b50 | 197 | break; |
198 | } | |
199 | } | |
200 | ||
201 | #endif /* USE_ICMP */ | |
202 | ||
203 | void | |
b7ac5457 | 204 | IcmpSquid::DomainPing(Ip::Address &to, const char *domain) |
cc192b50 | 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 | |
b826ffb5 | 213 | IcmpSquid::Open(void) |
cc192b50 | 214 | { |
215 | #if USE_ICMP | |
216 | const char *args[2]; | |
217 | int rfd; | |
218 | int wfd; | |
b7ac5457 | 219 | Ip::Address localhost; |
cc192b50 | 220 | |
221 | /* User configured disabled. */ | |
26ac0430 | 222 | if (!Config.pinger.enable) { |
cc192b50 | 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 | ||
d841c88d | 254 | Comm::SetSelect(icmp_sock, COMM_SELECT_READ, icmpSquidRecv, NULL, 0); |
cc192b50 | 255 | |
933dd095 | 256 | commUnsetFdTimeout(icmp_sock); |
cc192b50 | 257 | |
e0236918 | 258 | debugs(37, DBG_IMPORTANT, HERE << "Pinger socket opened on FD " << icmp_sock); |
cc192b50 | 259 | |
260 | /* Tests the pinger immediately using localhost */ | |
055421ee AJ |
261 | if (Ip::EnableIpv6) |
262 | SendEcho(localhost, S_ICMP_ECHO, "ip6-localhost"); | |
26ac0430 | 263 | if (localhost.SetIPv4()) |
cc192b50 | 264 | SendEcho(localhost, S_ICMP_ECHO, "localhost"); |
265 | ||
1191b93b | 266 | #if _SQUID_MSWIN_ |
cc192b50 | 267 | |
268 | debugs(37, 4, HERE << "Pinger handle: 0x" << std::hex << hIpc << std::dec << ", PID: " << pid); | |
269 | ||
270 | #endif /* _SQUID_MSWIN_ */ | |
cc192b50 | 271 | return icmp_sock; |
b8e19d32 HN |
272 | #else /* USE_ICMP */ |
273 | return -1; | |
274 | #endif /* USE_ICMP */ | |
cc192b50 | 275 | } |
276 | ||
277 | void | |
b826ffb5 | 278 | IcmpSquid::Close(void) |
cc192b50 | 279 | { |
280 | #if USE_ICMP | |
281 | ||
282 | if (icmp_sock < 0) | |
283 | return; | |
284 | ||
e0236918 | 285 | debugs(37, DBG_IMPORTANT, HERE << "Closing Pinger socket on FD " << icmp_sock); |
cc192b50 | 286 | |
1191b93b | 287 | #if _SQUID_MSWIN_ |
cc192b50 | 288 | |
289 | send(icmp_sock, (const void *) "$shutdown\n", 10, 0); | |
290 | ||
291 | #endif | |
292 | ||
293 | comm_close(icmp_sock); | |
294 | ||
1191b93b | 295 | #if _SQUID_MSWIN_ |
cc192b50 | 296 | |
297 | if (hIpc) { | |
298 | if (WaitForSingleObject(hIpc, 12000) != WAIT_OBJECT_0) { | |
299 | getCurrentTime(); | |
fa84c01d | 300 | debugs(37, DBG_CRITICAL, HERE << "WARNING: (pinger," << pid << ") didn't exit in 12 seconds"); |
cc192b50 | 301 | } |
302 | ||
303 | CloseHandle(hIpc); | |
304 | } | |
305 | ||
306 | #endif | |
307 | icmp_sock = -1; | |
308 | ||
309 | #endif | |
310 | } |