2 * DEBUG: section 05 Socket Connection Opener
6 #include "comm/ConnOpener.h"
7 #include "comm/Connection.h"
8 #include "comm/Loops.h"
13 #include "icmp/net_db.h"
15 #include "SquidTime.h"
21 CBDATA_NAMESPACED_CLASS_INIT(Comm
, ConnOpener
);
23 Comm::ConnOpener::ConnOpener(Comm::ConnectionPointer
&c
, AsyncCall::Pointer
&handler
, time_t ctimeout
) :
24 AsyncJob("Comm::ConnOpener"),
31 connectTimeout_(ctimeout
),
35 Comm::ConnOpener::~ConnOpener()
41 Comm::ConnOpener::doneAll() const
43 // is the conn_ to be opened still waiting?
45 return AsyncJob::doneAll();
48 // is the callback still to be called?
49 if (callback_
== NULL
|| callback_
->canceled()) {
50 return AsyncJob::doneAll();
57 Comm::ConnOpener::swanSong()
59 // cancel any event watchers
60 // done here to get the "swanSong" mention in cancel debugging.
61 if (calls_
.earlyAbort_
!= NULL
) {
62 calls_
.earlyAbort_
->cancel("Comm::ConnOpener::swanSong");
63 calls_
.earlyAbort_
= NULL
;
65 if (calls_
.timeout_
!= NULL
) {
66 calls_
.timeout_
->cancel("Comm::ConnOpener::swanSong");
67 calls_
.timeout_
= NULL
;
70 if (callback_
!= NULL
) {
71 if (callback_
->canceled())
74 // inform the still-waiting caller we are dying
75 doneConnecting(COMM_ERR_CONNECT
, 0);
78 // rollback what we can from the job state
79 if (temporaryFd_
>= 0) {
80 // doneConnecting() handles partial FD connection cleanup
81 doneConnecting(COMM_ERR_CONNECT
, 0);
88 Comm::ConnOpener::setHost(const char * new_host
)
90 // unset and erase if already set.
94 // set the new one if given.
96 host_
= xstrdup(new_host
);
100 Comm::ConnOpener::getHost() const
106 * Connection attempt are completed. One way or the other.
107 * Pass the results back to the external handler.
108 * NP: on errors the earlyAbort call should be cancelled first with a reason.
111 Comm::ConnOpener::doneConnecting(comm_err_t status
, int xerrno
)
113 // only mark the address good/bad AFTER connect is finished.
116 ipcacheMarkGoodAddr(host_
, conn_
->remote
);
118 ipcacheMarkBadAddr(host_
, conn_
->remote
);
120 if (Config
.onoff
.test_reachability
)
121 netdbDeleteAddrNetwork(conn_
->remote
);
126 if (callback_
!= NULL
) {
127 typedef CommConnectCbParams Params
;
128 Params
¶ms
= GetCommParams
<Params
>(callback_
);
130 params
.flag
= status
;
131 params
.xerrno
= xerrno
;
132 ScheduleCallHere(callback_
);
136 if (temporaryFd_
>= 0) {
137 debugs(5, 4, HERE
<< conn_
<< " closing temp FD " << temporaryFd_
);
138 // it never reached fully open, so cleanup the FD handlers
139 // Note that comm_close() sequence does not happen for partially open FD
140 Comm::SetSelect(temporaryFd_
, COMM_SELECT_WRITE
, NULL
, NULL
, 0);
141 calls_
.earlyAbort_
= NULL
;
142 if (calls_
.timeout_
!= NULL
) {
143 calls_
.timeout_
->cancel("Comm::ConnOpener::doneConnecting");
144 calls_
.timeout_
= NULL
;
146 fd_table
[temporaryFd_
].timeoutHandler
= NULL
;
147 fd_table
[temporaryFd_
].timeout
= 0;
149 fd_close(temporaryFd_
);
153 /* ensure cleared local state, we are done. */
158 Comm::ConnOpener::start()
162 /* get a socket open ready for connecting with */
163 if (temporaryFd_
< 0) {
165 /* outbound sockets have no need to be protocol agnostic. */
166 if (conn_
->remote
.IsIPv4()) {
167 conn_
->local
.SetIPv4();
170 temporaryFd_
= comm_openex(SOCK_STREAM
, IPPROTO_TCP
, conn_
->local
, conn_
->flags
, conn_
->tos
, conn_
->nfmark
, host_
);
171 if (temporaryFd_
< 0) {
172 doneConnecting(COMM_ERR_CONNECT
, 0);
177 typedef CommCbMemFunT
<Comm::ConnOpener
, CommCloseCbParams
> abortDialer
;
178 calls_
.earlyAbort_
= JobCallback(5, 4, abortDialer
, this, Comm::ConnOpener::earlyAbort
);
179 comm_add_close_handler(temporaryFd_
, calls_
.earlyAbort_
);
181 typedef CommCbMemFunT
<Comm::ConnOpener
, CommTimeoutCbParams
> timeoutDialer
;
182 calls_
.timeout_
= JobCallback(5, 4, timeoutDialer
, this, Comm::ConnOpener::timeout
);
183 debugs(5, 3, HERE
<< conn_
<< " timeout " << connectTimeout_
);
185 // Update the fd_table directly because conn_ is not yet storing the FD
186 assert(temporaryFd_
< Squid_MaxFD
);
187 assert(fd_table
[temporaryFd_
].flags
.open
);
188 typedef CommTimeoutCbParams Params
;
189 Params
¶ms
= GetCommParams
<Params
>(calls_
.timeout_
);
191 fd_table
[temporaryFd_
].timeoutHandler
= calls_
.timeout_
;
192 fd_table
[temporaryFd_
].timeout
= squid_curtime
+ (time_t) connectTimeout_
;
194 connectStart_
= squid_curtime
;
199 Comm::ConnOpener::connected()
201 conn_
->fd
= temporaryFd_
;
205 * stats.conn_open is used to account for the number of
206 * connections that we have open to the peer, so we can limit
207 * based on the max-conn option. We need to increment here,
208 * even if the connection may fail.
210 if (peer
*peer
=(conn_
->getPeer()))
211 ++peer
->stats
.conn_open
;
213 lookupLocalAddress();
215 /* TODO: remove these fd_table accesses. But old code still depends on fd_table flags to
216 * indicate the state of a raw fd object being passed around.
217 * Also, legacy code still depends on comm_local_port() with no access to Comm::Connection
218 * when those are done comm_local_port can become one of our member functions to do the below.
220 fd_table
[conn_
->fd
].flags
.open
= 1;
221 fd_table
[conn_
->fd
].local_addr
= conn_
->local
;
224 /** Make an FD connection attempt.
225 * Handles the case(s) when a partially setup connection gets closed early.
228 Comm::ConnOpener::connect()
232 // our parent Jobs signal abort by cancelling their callbacks.
233 if (callback_
== NULL
|| callback_
->canceled())
238 switch (comm_connect_addr(temporaryFd_
, conn_
->remote
) ) {
240 case COMM_INPROGRESS
:
241 // check for timeout FIRST.
242 if (squid_curtime
- connectStart_
> connectTimeout_
) {
243 debugs(5, 5, HERE
<< conn_
<< ": * - ERR took too long already.");
244 calls_
.earlyAbort_
->cancel("Comm::ConnOpener::connect timed out");
245 doneConnecting(COMM_TIMEOUT
, errno
);
248 debugs(5, 5, HERE
<< conn_
<< ": COMM_INPROGRESS");
249 Comm::SetSelect(temporaryFd_
, COMM_SELECT_WRITE
, Comm::ConnOpener::InProgressConnectRetry
, new Pointer(this), 0);
254 debugs(5, 5, HERE
<< conn_
<< ": COMM_OK - connected");
256 doneConnecting(COMM_OK
, 0);
262 // check for timeout FIRST.
263 if (squid_curtime
- connectStart_
> connectTimeout_
) {
264 debugs(5, 5, HERE
<< conn_
<< ": * - ERR took too long to receive response.");
265 calls_
.earlyAbort_
->cancel("Comm::ConnOpener::connect timed out");
266 doneConnecting(COMM_TIMEOUT
, errno
);
267 } else if (failRetries_
< Config
.connect_retries
) {
268 debugs(5, 5, HERE
<< conn_
<< ": * - try again");
269 eventAdd("Comm::ConnOpener::DelayedConnectRetry", Comm::ConnOpener::DelayedConnectRetry
, new Pointer(this), 0.05, 0, false);
272 // send ERROR back to the upper layer.
273 debugs(5, 5, HERE
<< conn_
<< ": * - ERR tried too many times already.");
274 calls_
.earlyAbort_
->cancel("Comm::ConnOpener::connect failed");
275 doneConnecting(COMM_ERR_CONNECT
, errno
);
281 * Lookup local-end address and port of the TCP link just opened.
282 * This ensure the connection local details are set correctly
285 Comm::ConnOpener::lookupLocalAddress()
287 struct addrinfo
*addr
= NULL
;
288 conn_
->local
.InitAddrInfo(addr
);
290 if (getsockname(conn_
->fd
, addr
->ai_addr
, &(addr
->ai_addrlen
)) != 0) {
291 debugs(50, DBG_IMPORTANT
, "ERROR: Failed to retrieve TCP/UDP details for socket: " << conn_
<< ": " << xstrerror());
292 conn_
->local
.FreeAddrInfo(addr
);
296 conn_
->local
= *addr
;
297 conn_
->local
.FreeAddrInfo(addr
);
298 debugs(5, 6, HERE
<< conn_
);
301 /** Abort connection attempt.
302 * Handles the case(s) when a partially setup connection gets closed early.
305 Comm::ConnOpener::earlyAbort(const CommCloseCbParams
&io
)
307 debugs(5, 3, HERE
<< io
.conn
);
308 doneConnecting(COMM_ERR_CLOSING
, io
.xerrno
); // NP: is closing or shutdown better?
312 * Handles the case(s) when a partially setup connection gets timed out.
313 * NP: When commSetConnTimeout accepts generic CommCommonCbParams this can die.
316 Comm::ConnOpener::timeout(const CommTimeoutCbParams
&)
321 /* Legacy Wrapper for the retry event after COMM_INPROGRESS
322 * XXX: As soon as Comm::SetSelect() accepts Async calls we can use a ConnOpener::connect call
325 Comm::ConnOpener::InProgressConnectRetry(int fd
, void *data
)
327 Pointer
*ptr
= static_cast<Pointer
*>(data
);
329 if (ConnOpener
*cs
= ptr
->valid()) {
330 // Ew. we are now outside the all AsyncJob protections.
331 // get back inside by scheduling another call...
332 typedef NullaryMemFunT
<Comm::ConnOpener
> Dialer
;
333 AsyncCall::Pointer call
= JobCallback(5, 4, Dialer
, cs
, Comm::ConnOpener::connect
);
334 ScheduleCallHere(call
);
339 /* Legacy Wrapper for the retry event with small delay after errors.
340 * XXX: As soon as eventAdd() accepts Async calls we can use a ConnOpener::connect call
343 Comm::ConnOpener::DelayedConnectRetry(void *data
)
345 Pointer
*ptr
= static_cast<Pointer
*>(data
);
347 if (ConnOpener
*cs
= ptr
->valid()) {
348 // Ew. we are now outside the all AsyncJob protections.
349 // get back inside by scheduling another call...
350 typedef NullaryMemFunT
<Comm::ConnOpener
> Dialer
;
351 AsyncCall::Pointer call
= JobCallback(5, 4, Dialer
, cs
, Comm::ConnOpener::connect
);
352 ScheduleCallHere(call
);