]> git.ipfire.org Git - thirdparty/squid.git/blame - src/comm/ConnOpener.h
Source Format Enforcement (#1234)
[thirdparty/squid.git] / src / comm / ConnOpener.h
CommitLineData
bbc27441 1/*
b8ae064d 2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
bbc27441
AJ
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
aed188fd
AJ
9#ifndef _SQUID_SRC_COMM_OPENERSTATEDATA_H
10#define _SQUID_SRC_COMM_OPENERSTATEDATA_H
cfd66529 11
cfd66529 12#include "base/AsyncCall.h"
aed188fd 13#include "base/AsyncJob.h"
cfd66529 14#include "cbdata.h"
c8407295 15#include "comm/Flag.h"
f9b72e0c 16#include "comm/forward.h"
602d9612 17#include "CommCalls.h"
cfd66529 18
dc49061a
A
19namespace Comm
20{
4accd6d8 21
2b6b1bcb
AR
22/// Asynchronously opens a TCP connection. Returns CommConnectCbParams: either
23/// Comm::OK with an open connection or another Comm::Flag with a closed one.
aed188fd 24class ConnOpener : public AsyncJob
cfd66529 25{
337b9aa4 26 CBDATA_CHILD(ConnOpener);
5c2f68b7 27
40d9f0fc 28public:
9e64d84e
AR
29 typedef CbcPointer<ConnOpener> Pointer;
30
337b9aa4 31 bool doneAll() const override;
e52e78c4 32
9b7992d9 33 ConnOpener(const Comm::ConnectionPointer &, const AsyncCall::Pointer &handler, time_t connect_timeout);
337b9aa4 34 ~ConnOpener() override;
e52e78c4
AJ
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
81da7e70 39protected:
337b9aa4
AR
40 void start() override;
41 void swanSong() override;
81da7e70 42
739b352a 43private:
5b67dfa4 44 // Undefined because two openers cannot share a connection
aed188fd 45 ConnOpener(const ConnOpener &);
5229395c 46 ConnOpener & operator =(const ConnOpener &c);
cfd66529 47
2832d7c0 48 void earlyAbort(const CommCloseCbParams &);
418b3087 49 void timeout(const CommTimeoutCbParams &);
c8407295 50 void sendAnswer(Comm::Flag errFlag, int xerrno, const char *why);
418b3087
AJ
51 static void InProgressConnectRetry(int fd, void *data);
52 static void DelayedConnectRetry(void *data);
3d7ecaff 53 void doConnect();
418b3087 54 void connected();
dd829807 55 void lookupLocalAddress();
e52e78c4 56
f6c0d1ab 57 void retrySleep();
923b75ce
AR
58 void restart();
59
60 bool createFd();
61 void closeFd();
62 void keepFd();
63 void cleanFd();
64
65 void cancelSleep();
66
40d9f0fc 67private:
5229395c 68 char *host_; ///< domain name we are trying to connect to.
a95ff429
AJ
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.
5229395c
AJ
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
c8407295 76 /// if we are not done by then, we will call back with Comm::TIMEOUT
923b75ce 77 time_t deadline_;
cfd66529 78
e52e78c4 79 /// handles to calls which we may need to cancel.
5229395c
AJ
80 struct Calls {
81 AsyncCall::Pointer earlyAbort_;
82 AsyncCall::Pointer timeout_;
923b75ce
AR
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_;
5229395c 86 } calls_;
cfd66529
AJ
87};
88
4accd6d8
AJ
89}; // namespace Comm
90
aed188fd 91#endif /* _SQUID_SRC_COMM_CONNOPENER_H */
f53969cc 92