]> git.ipfire.org Git - thirdparty/squid.git/blob - src/comm/ConnOpener.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / comm / ConnOpener.cc
1 /*
2 * DEBUG: section 05 Socket Connection Opener
3 */
4
5 #include "squid.h"
6 #include "CachePeer.h"
7 #include "comm.h"
8 #include "comm/Connection.h"
9 #include "comm/ConnOpener.h"
10 #include "comm/Loops.h"
11 #include "fd.h"
12 #include "fde.h"
13 #include "globals.h"
14 #include "icmp/net_db.h"
15 #include "ip/tools.h"
16 #include "ipcache.h"
17 #include "SquidConfig.h"
18 #include "SquidTime.h"
19
20 #if HAVE_ERRNO_H
21 #include <errno.h>
22 #endif
23
24 class CachePeer;
25
26 CBDATA_NAMESPACED_CLASS_INIT(Comm, ConnOpener);
27
28 Comm::ConnOpener::ConnOpener(Comm::ConnectionPointer &c, AsyncCall::Pointer &handler, time_t ctimeout) :
29 AsyncJob("Comm::ConnOpener"),
30 host_(NULL),
31 temporaryFd_(-1),
32 conn_(c),
33 callback_(handler),
34 totalTries_(0),
35 failRetries_(0),
36 deadline_(squid_curtime + static_cast<time_t>(ctimeout))
37 {}
38
39 Comm::ConnOpener::~ConnOpener()
40 {
41 safe_free(host_);
42 }
43
44 bool
45 Comm::ConnOpener::doneAll() const
46 {
47 // is the conn_ to be opened still waiting?
48 if (conn_ == NULL) {
49 return AsyncJob::doneAll();
50 }
51
52 // is the callback still to be called?
53 if (callback_ == NULL || callback_->canceled()) {
54 return AsyncJob::doneAll();
55 }
56
57 // otherwise, we must be waiting for something
58 Must(temporaryFd_ >= 0 || calls_.sleep_);
59 return false;
60 }
61
62 void
63 Comm::ConnOpener::swanSong()
64 {
65 if (callback_ != NULL) {
66 // inform the still-waiting caller we are dying
67 sendAnswer(COMM_ERR_CONNECT, 0, "Comm::ConnOpener::swanSong");
68 }
69
70 // did we abort with a temporary FD assigned?
71 if (temporaryFd_ >= 0)
72 closeFd();
73
74 // did we abort while waiting between retries?
75 if (calls_.sleep_)
76 cancelSleep();
77
78 AsyncJob::swanSong();
79 }
80
81 void
82 Comm::ConnOpener::setHost(const char * new_host)
83 {
84 // unset and erase if already set.
85 if (host_ != NULL)
86 safe_free(host_);
87
88 // set the new one if given.
89 if (new_host != NULL)
90 host_ = xstrdup(new_host);
91 }
92
93 const char *
94 Comm::ConnOpener::getHost() const
95 {
96 return host_;
97 }
98
99 /**
100 * Connection attempt are completed. One way or the other.
101 * Pass the results back to the external handler.
102 */
103 void
104 Comm::ConnOpener::sendAnswer(comm_err_t errFlag, int xerrno, const char *why)
105 {
106 // only mark the address good/bad AFTER connect is finished.
107 if (host_ != NULL) {
108 if (xerrno == 0) // XXX: should not we use errFlag instead?
109 ipcacheMarkGoodAddr(host_, conn_->remote);
110 else {
111 ipcacheMarkBadAddr(host_, conn_->remote);
112 #if USE_ICMP
113 if (Config.onoff.test_reachability)
114 netdbDeleteAddrNetwork(conn_->remote);
115 #endif
116 }
117 }
118
119 if (callback_ != NULL) {
120 // avoid scheduling cancelled callbacks, assuming they are common
121 // enough to make this extra check an optimization
122 if (callback_->canceled()) {
123 debugs(5, 4, conn_ << " not calling canceled " << *callback_ <<
124 " [" << callback_->id << ']' );
125 // TODO save the pconn to the pconnPool ?
126 } else {
127 typedef CommConnectCbParams Params;
128 Params &params = GetCommParams<Params>(callback_);
129 params.conn = conn_;
130 params.flag = errFlag;
131 params.xerrno = xerrno;
132 ScheduleCallHere(callback_);
133 }
134 callback_ = NULL;
135 }
136
137 // The job will stop without this call because nil callback_ makes
138 // doneAll() true, but this explicit call creates nicer debugging.
139 mustStop(why);
140 }
141
142 /// cleans up this job I/O state without closing temporaryFd
143 /// required before closing temporaryFd or keeping it in conn_
144 /// leaves FD bare so must only be called via closeFd() or keepFd()
145 void
146 Comm::ConnOpener::cleanFd()
147 {
148 debugs(5, 4, HERE << conn_ << " closing temp FD " << temporaryFd_);
149
150 Must(temporaryFd_ >= 0);
151 fde &f = fd_table[temporaryFd_];
152
153 // Our write_handler was set without using Comm::Write API, so we cannot
154 // use a cancellable Pointer-free job callback and simply cancel it here.
155 if (f.write_handler) {
156
157 /* XXX: We are about to remove write_handler, which was responsible
158 * for deleting write_data, so we have to delete write_data
159 * ourselves. Comm currently calls SetSelect handlers synchronously
160 * so if write_handler is set, we know it has not been called yet.
161 * ConnOpener converts that sync call into an async one, but only
162 * after deleting ptr, so that is not a problem.
163 */
164
165 delete static_cast<Pointer*>(f.write_data);
166 f.write_data = NULL;
167 f.write_handler = NULL;
168 }
169 // Comm::DoSelect does not do this when calling and resetting write_handler
170 // (because it expects more writes to come?). We could mimic that
171 // optimization by resetting Comm "Select" state only when the FD is
172 // actually closed.
173 Comm::SetSelect(temporaryFd_, COMM_SELECT_WRITE, NULL, NULL, 0);
174
175 if (calls_.timeout_ != NULL) {
176 calls_.timeout_->cancel("Comm::ConnOpener::cleanFd");
177 calls_.timeout_ = NULL;
178 }
179 // Comm checkTimeouts() and commCloseAllSockets() do not clear .timeout
180 // when calling timeoutHandler (XXX fix them), so we clear unconditionally.
181 f.timeoutHandler = NULL;
182 f.timeout = 0;
183
184 if (calls_.earlyAbort_ != NULL) {
185 comm_remove_close_handler(temporaryFd_, calls_.earlyAbort_);
186 calls_.earlyAbort_ = NULL;
187 }
188 }
189
190 /// cleans I/O state and ends I/O for temporaryFd_
191 void
192 Comm::ConnOpener::closeFd()
193 {
194 if (temporaryFd_ < 0)
195 return;
196
197 cleanFd();
198
199 // comm_close() below uses COMMIO_FD_WRITECB(fd)->active() to clear Comm
200 // "Select" state. It will not clear ours. XXX: It should always clear
201 // because a callback may have been active but was called before comm_close
202 // Update: we now do this in cleanFd()
203 // Comm::SetSelect(temporaryFd_, COMM_SELECT_WRITE, NULL, NULL, 0);
204
205 comm_close(temporaryFd_);
206 temporaryFd_ = -1;
207 }
208
209 /// cleans I/O state and moves temporaryFd_ to the conn_ for long-term use
210 void
211 Comm::ConnOpener::keepFd()
212 {
213 Must(conn_ != NULL);
214 Must(temporaryFd_ >= 0);
215
216 cleanFd();
217
218 conn_->fd = temporaryFd_;
219 temporaryFd_ = -1;
220 }
221
222 void
223 Comm::ConnOpener::start()
224 {
225 Must(conn_ != NULL);
226
227 /* outbound sockets have no need to be protocol agnostic. */
228 if (!(Ip::EnableIpv6&IPV6_SPECIAL_V4MAPPING) && conn_->remote.isIPv4()) {
229 conn_->local.setIPv4();
230 }
231
232 if (createFd())
233 connect();
234 }
235
236 /// called at the end of Comm::ConnOpener::DelayedConnectRetry event
237 void
238 Comm::ConnOpener::restart()
239 {
240 debugs(5, 5, conn_ << " restarting after sleep");
241 calls_.sleep_ = false;
242
243 if (createFd())
244 connect();
245 }
246
247 /// Create a socket for the future connection or return false.
248 /// If false is returned, done() is guaranteed to return true and end the job.
249 bool
250 Comm::ConnOpener::createFd()
251 {
252 Must(temporaryFd_ < 0);
253
254 // our initators signal abort by cancelling their callbacks
255 if (callback_ == NULL || callback_->canceled())
256 return false;
257
258 temporaryFd_ = comm_openex(SOCK_STREAM, IPPROTO_TCP, conn_->local, conn_->flags, conn_->tos, conn_->nfmark, host_);
259 if (temporaryFd_ < 0) {
260 sendAnswer(COMM_ERR_CONNECT, 0, "Comm::ConnOpener::createFd");
261 return false;
262 }
263
264 typedef CommCbMemFunT<Comm::ConnOpener, CommCloseCbParams> abortDialer;
265 calls_.earlyAbort_ = JobCallback(5, 4, abortDialer, this, Comm::ConnOpener::earlyAbort);
266 comm_add_close_handler(temporaryFd_, calls_.earlyAbort_);
267
268 typedef CommCbMemFunT<Comm::ConnOpener, CommTimeoutCbParams> timeoutDialer;
269 calls_.timeout_ = JobCallback(5, 4, timeoutDialer, this, Comm::ConnOpener::timeout);
270 debugs(5, 3, conn_ << " will timeout in " << (deadline_ - squid_curtime));
271
272 // Update the fd_table directly because commSetConnTimeout() needs open conn_
273 assert(temporaryFd_ < Squid_MaxFD);
274 assert(fd_table[temporaryFd_].flags.open);
275 typedef CommTimeoutCbParams Params;
276 Params &params = GetCommParams<Params>(calls_.timeout_);
277 params.conn = conn_;
278 fd_table[temporaryFd_].timeoutHandler = calls_.timeout_;
279 fd_table[temporaryFd_].timeout = deadline_;
280
281 return true;
282 }
283
284 void
285 Comm::ConnOpener::connected()
286 {
287 Must(temporaryFd_ >= 0);
288 keepFd();
289
290 /*
291 * stats.conn_open is used to account for the number of
292 * connections that we have open to the CachePeer, so we can limit
293 * based on the max-conn option. We need to increment here,
294 * even if the connection may fail.
295 */
296 if (CachePeer *peer=(conn_->getPeer()))
297 ++peer->stats.conn_open;
298
299 lookupLocalAddress();
300
301 /* TODO: remove these fd_table accesses. But old code still depends on fd_table flags to
302 * indicate the state of a raw fd object being passed around.
303 * Also, legacy code still depends on comm_local_port() with no access to Comm::Connection
304 * when those are done comm_local_port can become one of our member functions to do the below.
305 */
306 Must(fd_table[conn_->fd].flags.open);
307 fd_table[conn_->fd].local_addr = conn_->local;
308
309 sendAnswer(COMM_OK, 0, "Comm::ConnOpener::connected");
310 }
311
312 /// Make an FD connection attempt.
313 void
314 Comm::ConnOpener::connect()
315 {
316 Must(conn_ != NULL);
317 Must(temporaryFd_ >= 0);
318
319 ++ totalTries_;
320
321 switch (comm_connect_addr(temporaryFd_, conn_->remote) ) {
322
323 case COMM_INPROGRESS:
324 debugs(5, 5, HERE << conn_ << ": COMM_INPROGRESS");
325 Comm::SetSelect(temporaryFd_, COMM_SELECT_WRITE, Comm::ConnOpener::InProgressConnectRetry, new Pointer(this), 0);
326 break;
327
328 case COMM_OK:
329 debugs(5, 5, HERE << conn_ << ": COMM_OK - connected");
330 connected();
331 break;
332
333 default: {
334 const int xerrno = errno;
335
336 ++failRetries_;
337 debugs(5, 7, conn_ << ": failure #" << failRetries_ << " <= " <<
338 Config.connect_retries << ": " << xstrerr(xerrno));
339
340 if (failRetries_ < Config.connect_retries) {
341 debugs(5, 5, HERE << conn_ << ": * - try again");
342 retrySleep();
343 return;
344 } else {
345 // send ERROR back to the upper layer.
346 debugs(5, 5, HERE << conn_ << ": * - ERR tried too many times already.");
347 sendAnswer(COMM_ERR_CONNECT, xerrno, "Comm::ConnOpener::connect");
348 }
349 }
350 }
351 }
352
353 /// Close and wait a little before trying to open and connect again.
354 void
355 Comm::ConnOpener::retrySleep()
356 {
357 Must(!calls_.sleep_);
358 closeFd();
359 calls_.sleep_ = true;
360 eventAdd("Comm::ConnOpener::DelayedConnectRetry",
361 Comm::ConnOpener::DelayedConnectRetry,
362 new Pointer(this), 0.05, 0, false);
363 }
364
365 /// cleans up this job sleep state
366 void
367 Comm::ConnOpener::cancelSleep()
368 {
369 if (calls_.sleep_) {
370 // It would be nice to delete the sleep event, but it might be out of
371 // the event queue and in the async queue already, so (a) we do not know
372 // whether we can safely delete the call ptr here and (b) eventDelete()
373 // will assert if the event went async. Thus, we let the event run so
374 // that it deletes the call ptr [after this job is gone]. Note that we
375 // are called only when the job ends so this "hanging event" will do
376 // nothing but deleting the call ptr. TODO: Revise eventDelete() API.
377 // eventDelete(Comm::ConnOpener::DelayedConnectRetry, calls_.sleep);
378 calls_.sleep_ = false;
379 debugs(5, 9, conn_ << " stops sleeping");
380 }
381 }
382
383 /**
384 * Lookup local-end address and port of the TCP link just opened.
385 * This ensure the connection local details are set correctly
386 */
387 void
388 Comm::ConnOpener::lookupLocalAddress()
389 {
390 struct addrinfo *addr = NULL;
391 Ip::Address::InitAddrInfo(addr);
392
393 if (getsockname(conn_->fd, addr->ai_addr, &(addr->ai_addrlen)) != 0) {
394 debugs(50, DBG_IMPORTANT, "ERROR: Failed to retrieve TCP/UDP details for socket: " << conn_ << ": " << xstrerror());
395 Ip::Address::FreeAddrInfo(addr);
396 return;
397 }
398
399 conn_->local = *addr;
400 Ip::Address::FreeAddrInfo(addr);
401 debugs(5, 6, HERE << conn_);
402 }
403
404 /** Abort connection attempt.
405 * Handles the case(s) when a partially setup connection gets closed early.
406 */
407 void
408 Comm::ConnOpener::earlyAbort(const CommCloseCbParams &io)
409 {
410 debugs(5, 3, HERE << io.conn);
411 calls_.earlyAbort_ = NULL;
412 // NP: is closing or shutdown better?
413 sendAnswer(COMM_ERR_CLOSING, io.xerrno, "Comm::ConnOpener::earlyAbort");
414 }
415
416 /**
417 * Handles the case(s) when a partially setup connection gets timed out.
418 * NP: When commSetConnTimeout accepts generic CommCommonCbParams this can die.
419 */
420 void
421 Comm::ConnOpener::timeout(const CommTimeoutCbParams &)
422 {
423 debugs(5, 5, HERE << conn_ << ": * - ERR took too long to receive response.");
424 calls_.timeout_ = NULL;
425 sendAnswer(COMM_TIMEOUT, ETIMEDOUT, "Comm::ConnOpener::timeout");
426 }
427
428 /* Legacy Wrapper for the retry event after COMM_INPROGRESS
429 * XXX: As soon as Comm::SetSelect() accepts Async calls we can use a ConnOpener::connect call
430 */
431 void
432 Comm::ConnOpener::InProgressConnectRetry(int fd, void *data)
433 {
434 Pointer *ptr = static_cast<Pointer*>(data);
435 assert(ptr);
436 if (ConnOpener *cs = ptr->valid()) {
437 // Ew. we are now outside the all AsyncJob protections.
438 // get back inside by scheduling another call...
439 typedef NullaryMemFunT<Comm::ConnOpener> Dialer;
440 AsyncCall::Pointer call = JobCallback(5, 4, Dialer, cs, Comm::ConnOpener::connect);
441 ScheduleCallHere(call);
442 }
443 delete ptr;
444 }
445
446 /* Legacy Wrapper for the retry event with small delay after errors.
447 * XXX: As soon as eventAdd() accepts Async calls we can use a ConnOpener::restart call
448 */
449 void
450 Comm::ConnOpener::DelayedConnectRetry(void *data)
451 {
452 Pointer *ptr = static_cast<Pointer*>(data);
453 assert(ptr);
454 if (ConnOpener *cs = ptr->valid()) {
455 // Ew. we are now outside the all AsyncJob protections.
456 // get back inside by scheduling another call...
457 typedef NullaryMemFunT<Comm::ConnOpener> Dialer;
458 AsyncCall::Pointer call = JobCallback(5, 4, Dialer, cs, Comm::ConnOpener::restart);
459 ScheduleCallHere(call);
460 }
461 delete ptr;
462 }