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