2 * DEBUG: section 05 Socket Connection Opener
6 #include "comm/ConnOpener.h"
7 #include "comm/Connection.h"
8 #include "comm/Loops.h"
11 #include "icmp/net_db.h"
13 #include "SquidTime.h"
17 CBDATA_CLASS_INIT(ConnOpener
);
20 Comm::ConnOpener::ConnOpener(Comm::ConnectionPointer
&c
, AsyncCall::Pointer
&handler
, time_t ctimeout
) :
21 AsyncJob("Comm::ConnOpener"),
28 connectTimeout_(ctimeout
),
32 Comm::ConnOpener::~ConnOpener()
38 Comm::ConnOpener::doneAll() const
40 // is the conn_ to be opened still waiting?
42 return AsyncJob::doneAll();
45 // is the callback still to be called?
46 if (callback_
== NULL
|| callback_
->canceled()) {
47 return AsyncJob::doneAll();
54 Comm::ConnOpener::swanSong()
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
;
62 if (calls_
.timeout_
!= NULL
) {
63 calls_
.timeout_
->cancel("Comm::ConnOpener::swanSong");
64 calls_
.timeout_
= NULL
;
67 if (callback_
!= NULL
) {
68 if (callback_
->canceled())
71 // inform the still-waiting caller we are dying
72 doneConnecting(COMM_ERR_CONNECT
, 0);
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);
85 Comm::ConnOpener::setHost(const char * new_host
)
87 // unset and erase if already set.
91 // set the new one if given.
93 host_
= xstrdup(new_host
);
97 Comm::ConnOpener::getHost() const
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.
108 Comm::ConnOpener::doneConnecting(comm_err_t status
, int xerrno
)
110 // only mark the address good/bad AFTER connect is finished.
113 ipcacheMarkGoodAddr(host_
, conn_
->remote
);
115 ipcacheMarkBadAddr(host_
, conn_
->remote
);
117 if (Config
.onoff
.test_reachability
)
118 netdbDeleteAddrNetwork(conn_
->remote
);
123 if (callback_
!= NULL
) {
124 typedef CommConnectCbParams Params
;
125 Params
¶ms
= GetCommParams
<Params
>(callback_
);
127 params
.flag
= status
;
128 params
.xerrno
= xerrno
;
129 ScheduleCallHere(callback_
);
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
;
142 fd_table
[temporaryFd_
].timeoutHandler
= NULL
;
143 fd_table
[temporaryFd_
].timeout
= 0;
144 fd_close(temporaryFd_
);
148 /* ensure cleared local state, we are done. */
153 Comm::ConnOpener::start()
157 /* get a socket open ready for connecting with */
158 if (temporaryFd_
< 0) {
160 /* outbound sockets have no need to be protocol agnostic. */
161 if (conn_
->remote
.IsIPv4()) {
162 conn_
->local
.SetIPv4();
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);
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_
);
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_
);
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
¶ms
= GetCommParams
<Params
>(calls_
.timeout_
);
186 fd_table
[temporaryFd_
].timeoutHandler
= calls_
.timeout_
;
187 fd_table
[temporaryFd_
].timeout
= squid_curtime
+ (time_t) connectTimeout_
;
189 connectStart_
= squid_curtime
;
194 Comm::ConnOpener::connected()
196 conn_
->fd
= temporaryFd_
;
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.
205 if (conn_
->getPeer())
206 conn_
->getPeer()->stats
.conn_open
++;
208 lookupLocalAddress();
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.
215 fd_table
[conn_
->fd
].flags
.open
= 1;
216 fd_table
[conn_
->fd
].local_addr
= conn_
->local
;
219 /** Make an FD connection attempt.
220 * Handles the case(s) when a partially setup connection gets closed early.
223 Comm::ConnOpener::connect()
227 // our parent Jobs signal abort by cancelling their callbacks.
228 if (callback_
== NULL
|| callback_
->canceled())
233 switch (comm_connect_addr(temporaryFd_
, conn_
->remote
) ) {
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
);
243 debugs(5, 5, HERE
<< conn_
<< ": COMM_INPROGRESS");
244 Comm::SetSelect(temporaryFd_
, COMM_SELECT_WRITE
, Comm::ConnOpener::InProgressConnectRetry
, this, 0);
249 debugs(5, 5, HERE
<< conn_
<< ": COMM_OK - connected");
251 doneConnecting(COMM_OK
, 0);
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
, this, 0.05, 0);
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
);
276 * Lookup local-end address and port of the TCP link just opened.
277 * This ensure the connection local details are set correctly
280 Comm::ConnOpener::lookupLocalAddress()
282 struct addrinfo
*addr
= NULL
;
283 conn_
->local
.InitAddrInfo(addr
);
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
);
291 conn_
->local
= *addr
;
292 conn_
->local
.FreeAddrInfo(addr
);
293 debugs(5, 6, HERE
<< conn_
);
296 /** Abort connection attempt.
297 * Handles the case(s) when a partially setup connection gets closed early.
300 Comm::ConnOpener::earlyAbort(const CommCloseCbParams
&io
)
302 debugs(5, 3, HERE
<< io
.conn
);
303 doneConnecting(COMM_ERR_CLOSING
, io
.xerrno
); // NP: is closing or shutdown better?
307 * Handles the case(s) when a partially setup connection gets timed out.
308 * NP: When commSetConnTimeout accepts generic CommCommonCbParams this can die.
311 Comm::ConnOpener::timeout(const CommTimeoutCbParams
&)
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
320 Comm::ConnOpener::InProgressConnectRetry(int fd
, void *data
)
322 ConnOpener
*cs
= static_cast<Comm::ConnOpener
*>(data
);
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
);
332 /* Legacy Wrapper for the retry event with small delay after errors.
333 * XXX: As soon as eventAdd() accepts Async calls we can use a ConnOpener::connect call
336 Comm::ConnOpener::DelayedConnectRetry(void *data
)
338 ConnOpener
*cs
= static_cast<Comm::ConnOpener
*>(data
);
341 // Ew. we are now outside the all AsyncJob protections.
342 // get back inside by scheduling another call...
343 typedef NullaryMemFunT
<Comm::ConnOpener
> Dialer
;
344 AsyncCall::Pointer call
= JobCallback(5, 4, Dialer
, cs
, Comm::ConnOpener::connect
);
345 ScheduleCallHere(call
);