]> git.ipfire.org Git - thirdparty/squid.git/blob - src/comm/ConnOpener.h
Source Format Enforcement (#1234)
[thirdparty/squid.git] / src / comm / ConnOpener.h
1 /*
2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 #ifndef _SQUID_SRC_COMM_OPENERSTATEDATA_H
10 #define _SQUID_SRC_COMM_OPENERSTATEDATA_H
11
12 #include "base/AsyncCall.h"
13 #include "base/AsyncJob.h"
14 #include "cbdata.h"
15 #include "comm/Flag.h"
16 #include "comm/forward.h"
17 #include "CommCalls.h"
18
19 namespace Comm
20 {
21
22 /// Asynchronously opens a TCP connection. Returns CommConnectCbParams: either
23 /// Comm::OK with an open connection or another Comm::Flag with a closed one.
24 class ConnOpener : public AsyncJob
25 {
26 CBDATA_CHILD(ConnOpener);
27
28 public:
29 typedef CbcPointer<ConnOpener> Pointer;
30
31 bool doneAll() const override;
32
33 ConnOpener(const Comm::ConnectionPointer &, const AsyncCall::Pointer &handler, time_t connect_timeout);
34 ~ConnOpener() override;
35
36 void setHost(const char *); ///< set the hostname note for this connection
37 const char * getHost() const; ///< get the hostname noted for this connection
38
39 protected:
40 void start() override;
41 void swanSong() override;
42
43 private:
44 // Undefined because two openers cannot share a connection
45 ConnOpener(const ConnOpener &);
46 ConnOpener & operator =(const ConnOpener &c);
47
48 void earlyAbort(const CommCloseCbParams &);
49 void timeout(const CommTimeoutCbParams &);
50 void sendAnswer(Comm::Flag errFlag, int xerrno, const char *why);
51 static void InProgressConnectRetry(int fd, void *data);
52 static void DelayedConnectRetry(void *data);
53 void doConnect();
54 void connected();
55 void lookupLocalAddress();
56
57 void retrySleep();
58 void restart();
59
60 bool createFd();
61 void closeFd();
62 void keepFd();
63 void cleanFd();
64
65 void cancelSleep();
66
67 private:
68 char *host_; ///< domain name we are trying to connect to.
69 int temporaryFd_; ///< the FD being opened. Do NOT set conn_->fd until it is fully open.
70 Comm::ConnectionPointer conn_; ///< single connection currently to be opened.
71 AsyncCall::Pointer callback_; ///< handler to be called on connection completion.
72
73 int totalTries_; ///< total number of connection attempts over all destinations so far.
74 int failRetries_; ///< number of retries current destination has been tried.
75
76 /// if we are not done by then, we will call back with Comm::TIMEOUT
77 time_t deadline_;
78
79 /// handles to calls which we may need to cancel.
80 struct Calls {
81 AsyncCall::Pointer earlyAbort_;
82 AsyncCall::Pointer timeout_;
83 /// Whether we are idling before retrying to connect; not yet a call
84 /// [that we can cancel], but it will probably become one eventually.
85 bool sleep_;
86 } calls_;
87 };
88
89 }; // namespace Comm
90
91 #endif /* _SQUID_SRC_COMM_CONNOPENER_H */
92