]> git.ipfire.org Git - thirdparty/squid.git/blob - src/comm/ConnOpener.cc
Merged from trunk (r12132, v3.2.0.17+)
[thirdparty/squid.git] / src / comm / ConnOpener.cc
1 /*
2 * DEBUG: section 05 Socket Connection Opener
3 */
4
5 #include "squid.h"
6 #include "comm/ConnOpener.h"
7 #include "comm/Connection.h"
8 #include "comm/Loops.h"
9 #include "comm.h"
10 #include "fde.h"
11 #include "icmp/net_db.h"
12 #include "ipcache.h"
13 #include "SquidTime.h"
14
15 namespace Comm
16 {
17 CBDATA_CLASS_INIT(ConnOpener);
18 };
19
20 Comm::ConnOpener::ConnOpener(Comm::ConnectionPointer &c, AsyncCall::Pointer &handler, time_t ctimeout) :
21 AsyncJob("Comm::ConnOpener"),
22 host_(NULL),
23 temporaryFd_(-1),
24 conn_(c),
25 callback_(handler),
26 totalTries_(0),
27 failRetries_(0),
28 connectTimeout_(ctimeout),
29 connectStart_(0)
30 {}
31
32 Comm::ConnOpener::~ConnOpener()
33 {
34 safe_free(host_);
35 }
36
37 bool
38 Comm::ConnOpener::doneAll() const
39 {
40 // is the conn_ to be opened still waiting?
41 if (conn_ == NULL) {
42 return AsyncJob::doneAll();
43 }
44
45 // is the callback still to be called?
46 if (callback_ == NULL || callback_->canceled()) {
47 return AsyncJob::doneAll();
48 }
49
50 return false;
51 }
52
53 void
54 Comm::ConnOpener::swanSong()
55 {
56 // cancel any event watchers
57 // done here to get the "swanSong" mention in cancel debugging.
58 if (calls_.earlyAbort_ != NULL) {
59 calls_.earlyAbort_->cancel("Comm::ConnOpener::swanSong");
60 calls_.earlyAbort_ = NULL;
61 }
62 if (calls_.timeout_ != NULL) {
63 calls_.timeout_->cancel("Comm::ConnOpener::swanSong");
64 calls_.timeout_ = NULL;
65 }
66
67 if (callback_ != NULL) {
68 if (callback_->canceled())
69 callback_ = NULL;
70 else
71 // inform the still-waiting caller we are dying
72 doneConnecting(COMM_ERR_CONNECT, 0);
73 }
74
75 // rollback what we can from the job state
76 if (temporaryFd_ >= 0) {
77 // doneConnecting() handles partial FD connection cleanup
78 doneConnecting(COMM_ERR_CONNECT, 0);
79 }
80
81 AsyncJob::swanSong();
82 }
83
84 void
85 Comm::ConnOpener::setHost(const char * new_host)
86 {
87 // unset and erase if already set.
88 if (host_ != NULL)
89 safe_free(host_);
90
91 // set the new one if given.
92 if (new_host != NULL)
93 host_ = xstrdup(new_host);
94 }
95
96 const char *
97 Comm::ConnOpener::getHost() const
98 {
99 return host_;
100 }
101
102 /**
103 * Connection attempt are completed. One way or the other.
104 * Pass the results back to the external handler.
105 * NP: on errors the earlyAbort call should be cancelled first with a reason.
106 */
107 void
108 Comm::ConnOpener::doneConnecting(comm_err_t status, int xerrno)
109 {
110 // only mark the address good/bad AFTER connect is finished.
111 if (host_ != NULL) {
112 if (xerrno == 0)
113 ipcacheMarkGoodAddr(host_, conn_->remote);
114 else {
115 ipcacheMarkBadAddr(host_, conn_->remote);
116 #if USE_ICMP
117 if (Config.onoff.test_reachability)
118 netdbDeleteAddrNetwork(conn_->remote);
119 #endif
120 }
121 }
122
123 if (callback_ != NULL) {
124 typedef CommConnectCbParams Params;
125 Params &params = GetCommParams<Params>(callback_);
126 params.conn = conn_;
127 params.flag = status;
128 params.xerrno = xerrno;
129 ScheduleCallHere(callback_);
130 callback_ = NULL;
131 }
132
133 if (temporaryFd_ >= 0) {
134 // it never reached fully open, so cleanup the FD handlers
135 // Note that comm_close() sequence does not happen for partially open FD
136 Comm::SetSelect(temporaryFd_, COMM_SELECT_WRITE, NULL, NULL, 0);
137 calls_.earlyAbort_ = NULL;
138 if (calls_.timeout_ != NULL) {
139 calls_.timeout_->cancel("Comm::ConnOpener::doneConnecting");
140 calls_.timeout_ = NULL;
141 }
142 fd_table[temporaryFd_].timeoutHandler = NULL;
143 fd_table[temporaryFd_].timeout = 0;
144 fd_close(temporaryFd_);
145 temporaryFd_ = -1;
146 }
147
148 /* ensure cleared local state, we are done. */
149 conn_ = NULL;
150 }
151
152 void
153 Comm::ConnOpener::start()
154 {
155 Must(conn_ != NULL);
156
157 /* get a socket open ready for connecting with */
158 if (temporaryFd_ < 0) {
159 #if USE_IPV6
160 /* outbound sockets have no need to be protocol agnostic. */
161 if (conn_->remote.IsIPv4()) {
162 conn_->local.SetIPv4();
163 }
164 #endif
165 temporaryFd_ = comm_openex(SOCK_STREAM, IPPROTO_TCP, conn_->local, conn_->flags, conn_->tos, conn_->nfmark, host_);
166 if (temporaryFd_ < 0) {
167 doneConnecting(COMM_ERR_CONNECT, 0);
168 return;
169 }
170 }
171
172 typedef CommCbMemFunT<Comm::ConnOpener, CommCloseCbParams> abortDialer;
173 calls_.earlyAbort_ = JobCallback(5, 4, abortDialer, this, Comm::ConnOpener::earlyAbort);
174 comm_add_close_handler(temporaryFd_, calls_.earlyAbort_);
175
176 typedef CommCbMemFunT<Comm::ConnOpener, CommTimeoutCbParams> timeoutDialer;
177 calls_.timeout_ = JobCallback(5, 4, timeoutDialer, this, Comm::ConnOpener::timeout);
178 debugs(5, 3, HERE << conn_ << " timeout " << connectTimeout_);
179
180 // Update the fd_table directly because conn_ is not yet storing the FD
181 assert(temporaryFd_ < Squid_MaxFD);
182 assert(fd_table[temporaryFd_].flags.open);
183 typedef CommTimeoutCbParams Params;
184 Params &params = GetCommParams<Params>(calls_.timeout_);
185 params.conn = conn_;
186 fd_table[temporaryFd_].timeoutHandler = calls_.timeout_;
187 fd_table[temporaryFd_].timeout = squid_curtime + (time_t) connectTimeout_;
188
189 connectStart_ = squid_curtime;
190 connect();
191 }
192
193 void
194 Comm::ConnOpener::connected()
195 {
196 conn_->fd = temporaryFd_;
197 temporaryFd_ = -1;
198
199 /*
200 * stats.conn_open is used to account for the number of
201 * connections that we have open to the peer, so we can limit
202 * based on the max-conn option. We need to increment here,
203 * even if the connection may fail.
204 */
205 if (conn_->getPeer())
206 conn_->getPeer()->stats.conn_open++;
207
208 lookupLocalAddress();
209
210 /* TODO: remove these fd_table accesses. But old code still depends on fd_table flags to
211 * indicate the state of a raw fd object being passed around.
212 * Also, legacy code still depends on comm_local_port() with no access to Comm::Connection
213 * when those are done comm_local_port can become one of our member functions to do the below.
214 */
215 fd_table[conn_->fd].flags.open = 1;
216 fd_table[conn_->fd].local_addr = conn_->local;
217 }
218
219 /** Make an FD connection attempt.
220 * Handles the case(s) when a partially setup connection gets closed early.
221 */
222 void
223 Comm::ConnOpener::connect()
224 {
225 Must(conn_ != NULL);
226
227 // our parent Jobs signal abort by cancelling their callbacks.
228 if (callback_ == NULL || callback_->canceled())
229 return;
230
231 totalTries_++;
232
233 switch (comm_connect_addr(temporaryFd_, conn_->remote) ) {
234
235 case COMM_INPROGRESS:
236 // check for timeout FIRST.
237 if (squid_curtime - connectStart_ > connectTimeout_) {
238 debugs(5, 5, HERE << conn_ << ": * - ERR took too long already.");
239 calls_.earlyAbort_->cancel("Comm::ConnOpener::connect timed out");
240 doneConnecting(COMM_TIMEOUT, errno);
241 return;
242 } else {
243 debugs(5, 5, HERE << conn_ << ": COMM_INPROGRESS");
244 Comm::SetSelect(temporaryFd_, COMM_SELECT_WRITE, Comm::ConnOpener::InProgressConnectRetry, new Pointer(this), 0);
245 }
246 break;
247
248 case COMM_OK:
249 debugs(5, 5, HERE << conn_ << ": COMM_OK - connected");
250 connected();
251 doneConnecting(COMM_OK, 0);
252 break;
253
254 default:
255 failRetries_++;
256
257 // check for timeout FIRST.
258 if (squid_curtime - connectStart_ > connectTimeout_) {
259 debugs(5, 5, HERE << conn_ << ": * - ERR took too long to receive response.");
260 calls_.earlyAbort_->cancel("Comm::ConnOpener::connect timed out");
261 doneConnecting(COMM_TIMEOUT, errno);
262 } else if (failRetries_ < Config.connect_retries) {
263 debugs(5, 5, HERE << conn_ << ": * - try again");
264 eventAdd("Comm::ConnOpener::DelayedConnectRetry", Comm::ConnOpener::DelayedConnectRetry, new Pointer(this), 0.05, 0);
265 return;
266 } else {
267 // send ERROR back to the upper layer.
268 debugs(5, 5, HERE << conn_ << ": * - ERR tried too many times already.");
269 calls_.earlyAbort_->cancel("Comm::ConnOpener::connect failed");
270 doneConnecting(COMM_ERR_CONNECT, errno);
271 }
272 }
273 }
274
275 /**
276 * Lookup local-end address and port of the TCP link just opened.
277 * This ensure the connection local details are set correctly
278 */
279 void
280 Comm::ConnOpener::lookupLocalAddress()
281 {
282 struct addrinfo *addr = NULL;
283 conn_->local.InitAddrInfo(addr);
284
285 if (getsockname(conn_->fd, addr->ai_addr, &(addr->ai_addrlen)) != 0) {
286 debugs(50, DBG_IMPORTANT, "ERROR: Failed to retrieve TCP/UDP details for socket: " << conn_ << ": " << xstrerror());
287 conn_->local.FreeAddrInfo(addr);
288 return;
289 }
290
291 conn_->local = *addr;
292 conn_->local.FreeAddrInfo(addr);
293 debugs(5, 6, HERE << conn_);
294 }
295
296 /** Abort connection attempt.
297 * Handles the case(s) when a partially setup connection gets closed early.
298 */
299 void
300 Comm::ConnOpener::earlyAbort(const CommCloseCbParams &io)
301 {
302 debugs(5, 3, HERE << io.conn);
303 doneConnecting(COMM_ERR_CLOSING, io.xerrno); // NP: is closing or shutdown better?
304 }
305
306 /**
307 * Handles the case(s) when a partially setup connection gets timed out.
308 * NP: When commSetConnTimeout accepts generic CommCommonCbParams this can die.
309 */
310 void
311 Comm::ConnOpener::timeout(const CommTimeoutCbParams &)
312 {
313 connect();
314 }
315
316 /* Legacy Wrapper for the retry event after COMM_INPROGRESS
317 * XXX: As soon as Comm::SetSelect() accepts Async calls we can use a ConnOpener::connect call
318 */
319 void
320 Comm::ConnOpener::InProgressConnectRetry(int fd, void *data)
321 {
322 Pointer *ptr = static_cast<Pointer*>(data);
323 assert(ptr);
324 if (ConnOpener *cs = ptr->valid()) {
325 // Ew. we are now outside the all AsyncJob protections.
326 // get back inside by scheduling another call...
327 typedef NullaryMemFunT<Comm::ConnOpener> Dialer;
328 AsyncCall::Pointer call = JobCallback(5, 4, Dialer, cs, Comm::ConnOpener::connect);
329 ScheduleCallHere(call);
330 }
331 delete ptr;
332 }
333
334 /* Legacy Wrapper for the retry event with small delay after errors.
335 * XXX: As soon as eventAdd() accepts Async calls we can use a ConnOpener::connect call
336 */
337 void
338 Comm::ConnOpener::DelayedConnectRetry(void *data)
339 {
340 Pointer *ptr = static_cast<Pointer*>(data);
341 assert(ptr);
342 if (ConnOpener *cs = ptr->valid()) {
343 // Ew. we are now outside the all AsyncJob protections.
344 // get back inside by scheduling another call...
345 typedef NullaryMemFunT<Comm::ConnOpener> Dialer;
346 AsyncCall::Pointer call = JobCallback(5, 4, Dialer, cs, Comm::ConnOpener::connect);
347 ScheduleCallHere(call);
348 }
349 delete ptr;
350 }